imx-sdma.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // drivers/dma/imx-sdma.c
  4. //
  5. // This file contains a driver for the Freescale Smart DMA engine
  6. //
  7. // Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  8. //
  9. // Based on code from Freescale:
  10. //
  11. // Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  12. #include <linux/init.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/bitops.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. #include <linux/semaphore.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmapool.h>
  27. #include <linux/firmware.h>
  28. #include <linux/slab.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of_dma.h>
  35. #include <asm/irq.h>
  36. #include <linux/platform_data/dma-imx-sdma.h>
  37. #include <linux/platform_data/dma-imx.h>
  38. #include <linux/regmap.h>
  39. #include <linux/mfd/syscon.h>
  40. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  41. #include "dmaengine.h"
  42. #include "virt-dma.h"
  43. /* SDMA registers */
  44. #define SDMA_H_C0PTR 0x000
  45. #define SDMA_H_INTR 0x004
  46. #define SDMA_H_STATSTOP 0x008
  47. #define SDMA_H_START 0x00c
  48. #define SDMA_H_EVTOVR 0x010
  49. #define SDMA_H_DSPOVR 0x014
  50. #define SDMA_H_HOSTOVR 0x018
  51. #define SDMA_H_EVTPEND 0x01c
  52. #define SDMA_H_DSPENBL 0x020
  53. #define SDMA_H_RESET 0x024
  54. #define SDMA_H_EVTERR 0x028
  55. #define SDMA_H_INTRMSK 0x02c
  56. #define SDMA_H_PSW 0x030
  57. #define SDMA_H_EVTERRDBG 0x034
  58. #define SDMA_H_CONFIG 0x038
  59. #define SDMA_ONCE_ENB 0x040
  60. #define SDMA_ONCE_DATA 0x044
  61. #define SDMA_ONCE_INSTR 0x048
  62. #define SDMA_ONCE_STAT 0x04c
  63. #define SDMA_ONCE_CMD 0x050
  64. #define SDMA_EVT_MIRROR 0x054
  65. #define SDMA_ILLINSTADDR 0x058
  66. #define SDMA_CHN0ADDR 0x05c
  67. #define SDMA_ONCE_RTB 0x060
  68. #define SDMA_XTRIG_CONF1 0x070
  69. #define SDMA_XTRIG_CONF2 0x074
  70. #define SDMA_CHNENBL0_IMX35 0x200
  71. #define SDMA_CHNENBL0_IMX31 0x080
  72. #define SDMA_CHNPRI_0 0x100
  73. /*
  74. * Buffer descriptor status values.
  75. */
  76. #define BD_DONE 0x01
  77. #define BD_WRAP 0x02
  78. #define BD_CONT 0x04
  79. #define BD_INTR 0x08
  80. #define BD_RROR 0x10
  81. #define BD_LAST 0x20
  82. #define BD_EXTD 0x80
  83. /*
  84. * Data Node descriptor status values.
  85. */
  86. #define DND_END_OF_FRAME 0x80
  87. #define DND_END_OF_XFER 0x40
  88. #define DND_DONE 0x20
  89. #define DND_UNUSED 0x01
  90. /*
  91. * IPCV2 descriptor status values.
  92. */
  93. #define BD_IPCV2_END_OF_FRAME 0x40
  94. #define IPCV2_MAX_NODES 50
  95. /*
  96. * Error bit set in the CCB status field by the SDMA,
  97. * in setbd routine, in case of a transfer error
  98. */
  99. #define DATA_ERROR 0x10000000
  100. /*
  101. * Buffer descriptor commands.
  102. */
  103. #define C0_ADDR 0x01
  104. #define C0_LOAD 0x02
  105. #define C0_DUMP 0x03
  106. #define C0_SETCTX 0x07
  107. #define C0_GETCTX 0x03
  108. #define C0_SETDM 0x01
  109. #define C0_SETPM 0x04
  110. #define C0_GETDM 0x02
  111. #define C0_GETPM 0x08
  112. /*
  113. * Change endianness indicator in the BD command field
  114. */
  115. #define CHANGE_ENDIANNESS 0x80
  116. /*
  117. * p_2_p watermark_level description
  118. * Bits Name Description
  119. * 0-7 Lower WML Lower watermark level
  120. * 8 PS 1: Pad Swallowing
  121. * 0: No Pad Swallowing
  122. * 9 PA 1: Pad Adding
  123. * 0: No Pad Adding
  124. * 10 SPDIF If this bit is set both source
  125. * and destination are on SPBA
  126. * 11 Source Bit(SP) 1: Source on SPBA
  127. * 0: Source on AIPS
  128. * 12 Destination Bit(DP) 1: Destination on SPBA
  129. * 0: Destination on AIPS
  130. * 13-15 --------- MUST BE 0
  131. * 16-23 Higher WML HWML
  132. * 24-27 N Total number of samples after
  133. * which Pad adding/Swallowing
  134. * must be done. It must be odd.
  135. * 28 Lower WML Event(LWE) SDMA events reg to check for
  136. * LWML event mask
  137. * 0: LWE in EVENTS register
  138. * 1: LWE in EVENTS2 register
  139. * 29 Higher WML Event(HWE) SDMA events reg to check for
  140. * HWML event mask
  141. * 0: HWE in EVENTS register
  142. * 1: HWE in EVENTS2 register
  143. * 30 --------- MUST BE 0
  144. * 31 CONT 1: Amount of samples to be
  145. * transferred is unknown and
  146. * script will keep on
  147. * transferring samples as long as
  148. * both events are detected and
  149. * script must be manually stopped
  150. * by the application
  151. * 0: The amount of samples to be
  152. * transferred is equal to the
  153. * count field of mode word
  154. */
  155. #define SDMA_WATERMARK_LEVEL_LWML 0xFF
  156. #define SDMA_WATERMARK_LEVEL_PS BIT(8)
  157. #define SDMA_WATERMARK_LEVEL_PA BIT(9)
  158. #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10)
  159. #define SDMA_WATERMARK_LEVEL_SP BIT(11)
  160. #define SDMA_WATERMARK_LEVEL_DP BIT(12)
  161. #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16)
  162. #define SDMA_WATERMARK_LEVEL_LWE BIT(28)
  163. #define SDMA_WATERMARK_LEVEL_HWE BIT(29)
  164. #define SDMA_WATERMARK_LEVEL_CONT BIT(31)
  165. #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  166. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  167. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  168. #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \
  169. BIT(DMA_MEM_TO_DEV) | \
  170. BIT(DMA_DEV_TO_DEV))
  171. /*
  172. * Mode/Count of data node descriptors - IPCv2
  173. */
  174. struct sdma_mode_count {
  175. #define SDMA_BD_MAX_CNT 0xffff
  176. u32 count : 16; /* size of the buffer pointed by this BD */
  177. u32 status : 8; /* E,R,I,C,W,D status bits stored here */
  178. u32 command : 8; /* command mostly used for channel 0 */
  179. };
  180. /*
  181. * Buffer descriptor
  182. */
  183. struct sdma_buffer_descriptor {
  184. struct sdma_mode_count mode;
  185. u32 buffer_addr; /* address of the buffer described */
  186. u32 ext_buffer_addr; /* extended buffer address */
  187. } __attribute__ ((packed));
  188. /**
  189. * struct sdma_channel_control - Channel control Block
  190. *
  191. * @current_bd_ptr: current buffer descriptor processed
  192. * @base_bd_ptr: first element of buffer descriptor array
  193. * @unused: padding. The SDMA engine expects an array of 128 byte
  194. * control blocks
  195. */
  196. struct sdma_channel_control {
  197. u32 current_bd_ptr;
  198. u32 base_bd_ptr;
  199. u32 unused[2];
  200. } __attribute__ ((packed));
  201. /**
  202. * struct sdma_state_registers - SDMA context for a channel
  203. *
  204. * @pc: program counter
  205. * @unused1: unused
  206. * @t: test bit: status of arithmetic & test instruction
  207. * @rpc: return program counter
  208. * @unused0: unused
  209. * @sf: source fault while loading data
  210. * @spc: loop start program counter
  211. * @unused2: unused
  212. * @df: destination fault while storing data
  213. * @epc: loop end program counter
  214. * @lm: loop mode
  215. */
  216. struct sdma_state_registers {
  217. u32 pc :14;
  218. u32 unused1: 1;
  219. u32 t : 1;
  220. u32 rpc :14;
  221. u32 unused0: 1;
  222. u32 sf : 1;
  223. u32 spc :14;
  224. u32 unused2: 1;
  225. u32 df : 1;
  226. u32 epc :14;
  227. u32 lm : 2;
  228. } __attribute__ ((packed));
  229. /**
  230. * struct sdma_context_data - sdma context specific to a channel
  231. *
  232. * @channel_state: channel state bits
  233. * @gReg: general registers
  234. * @mda: burst dma destination address register
  235. * @msa: burst dma source address register
  236. * @ms: burst dma status register
  237. * @md: burst dma data register
  238. * @pda: peripheral dma destination address register
  239. * @psa: peripheral dma source address register
  240. * @ps: peripheral dma status register
  241. * @pd: peripheral dma data register
  242. * @ca: CRC polynomial register
  243. * @cs: CRC accumulator register
  244. * @dda: dedicated core destination address register
  245. * @dsa: dedicated core source address register
  246. * @ds: dedicated core status register
  247. * @dd: dedicated core data register
  248. * @scratch0: 1st word of dedicated ram for context switch
  249. * @scratch1: 2nd word of dedicated ram for context switch
  250. * @scratch2: 3rd word of dedicated ram for context switch
  251. * @scratch3: 4th word of dedicated ram for context switch
  252. * @scratch4: 5th word of dedicated ram for context switch
  253. * @scratch5: 6th word of dedicated ram for context switch
  254. * @scratch6: 7th word of dedicated ram for context switch
  255. * @scratch7: 8th word of dedicated ram for context switch
  256. */
  257. struct sdma_context_data {
  258. struct sdma_state_registers channel_state;
  259. u32 gReg[8];
  260. u32 mda;
  261. u32 msa;
  262. u32 ms;
  263. u32 md;
  264. u32 pda;
  265. u32 psa;
  266. u32 ps;
  267. u32 pd;
  268. u32 ca;
  269. u32 cs;
  270. u32 dda;
  271. u32 dsa;
  272. u32 ds;
  273. u32 dd;
  274. u32 scratch0;
  275. u32 scratch1;
  276. u32 scratch2;
  277. u32 scratch3;
  278. u32 scratch4;
  279. u32 scratch5;
  280. u32 scratch6;
  281. u32 scratch7;
  282. } __attribute__ ((packed));
  283. struct sdma_engine;
  284. /**
  285. * struct sdma_desc - descriptor structor for one transfer
  286. * @vd: descriptor for virt dma
  287. * @num_bd: number of descriptors currently handling
  288. * @bd_phys: physical address of bd
  289. * @buf_tail: ID of the buffer that was processed
  290. * @buf_ptail: ID of the previous buffer that was processed
  291. * @period_len: period length, used in cyclic.
  292. * @chn_real_count: the real count updated from bd->mode.count
  293. * @chn_count: the transfer count set
  294. * @sdmac: sdma_channel pointer
  295. * @bd: pointer of allocate bd
  296. */
  297. struct sdma_desc {
  298. struct virt_dma_desc vd;
  299. unsigned int num_bd;
  300. dma_addr_t bd_phys;
  301. unsigned int buf_tail;
  302. unsigned int buf_ptail;
  303. unsigned int period_len;
  304. unsigned int chn_real_count;
  305. unsigned int chn_count;
  306. struct sdma_channel *sdmac;
  307. struct sdma_buffer_descriptor *bd;
  308. };
  309. /**
  310. * struct sdma_channel - housekeeping for a SDMA channel
  311. *
  312. * @vc: virt_dma base structure
  313. * @desc: sdma description including vd and other special member
  314. * @sdma: pointer to the SDMA engine for this channel
  315. * @channel: the channel number, matches dmaengine chan_id + 1
  316. * @direction: transfer type. Needed for setting SDMA script
  317. * @peripheral_type: Peripheral type. Needed for setting SDMA script
  318. * @event_id0: aka dma request line
  319. * @event_id1: for channels that use 2 events
  320. * @word_size: peripheral access size
  321. * @pc_from_device: script address for those device_2_memory
  322. * @pc_to_device: script address for those memory_2_device
  323. * @device_to_device: script address for those device_2_device
  324. * @pc_to_pc: script address for those memory_2_memory
  325. * @flags: loop mode or not
  326. * @per_address: peripheral source or destination address in common case
  327. * destination address in p_2_p case
  328. * @per_address2: peripheral source address in p_2_p case
  329. * @event_mask: event mask used in p_2_p script
  330. * @watermark_level: value for gReg[7], some script will extend it from
  331. * basic watermark such as p_2_p
  332. * @shp_addr: value for gReg[6]
  333. * @per_addr: value for gReg[2]
  334. * @status: status of dma channel
  335. * @data: specific sdma interface structure
  336. * @bd_pool: dma_pool for bd
  337. */
  338. struct sdma_channel {
  339. struct virt_dma_chan vc;
  340. struct sdma_desc *desc;
  341. struct sdma_engine *sdma;
  342. unsigned int channel;
  343. enum dma_transfer_direction direction;
  344. enum sdma_peripheral_type peripheral_type;
  345. unsigned int event_id0;
  346. unsigned int event_id1;
  347. enum dma_slave_buswidth word_size;
  348. unsigned int pc_from_device, pc_to_device;
  349. unsigned int device_to_device;
  350. unsigned int pc_to_pc;
  351. unsigned long flags;
  352. dma_addr_t per_address, per_address2;
  353. unsigned long event_mask[2];
  354. unsigned long watermark_level;
  355. u32 shp_addr, per_addr;
  356. enum dma_status status;
  357. struct imx_dma_data data;
  358. struct dma_pool *bd_pool;
  359. };
  360. #define IMX_DMA_SG_LOOP BIT(0)
  361. #define MAX_DMA_CHANNELS 32
  362. #define MXC_SDMA_DEFAULT_PRIORITY 1
  363. #define MXC_SDMA_MIN_PRIORITY 1
  364. #define MXC_SDMA_MAX_PRIORITY 7
  365. #define SDMA_FIRMWARE_MAGIC 0x414d4453
  366. /**
  367. * struct sdma_firmware_header - Layout of the firmware image
  368. *
  369. * @magic: "SDMA"
  370. * @version_major: increased whenever layout of struct
  371. * sdma_script_start_addrs changes.
  372. * @version_minor: firmware minor version (for binary compatible changes)
  373. * @script_addrs_start: offset of struct sdma_script_start_addrs in this image
  374. * @num_script_addrs: Number of script addresses in this image
  375. * @ram_code_start: offset of SDMA ram image in this firmware image
  376. * @ram_code_size: size of SDMA ram image
  377. * @script_addrs: Stores the start address of the SDMA scripts
  378. * (in SDMA memory space)
  379. */
  380. struct sdma_firmware_header {
  381. u32 magic;
  382. u32 version_major;
  383. u32 version_minor;
  384. u32 script_addrs_start;
  385. u32 num_script_addrs;
  386. u32 ram_code_start;
  387. u32 ram_code_size;
  388. };
  389. struct sdma_driver_data {
  390. int chnenbl0;
  391. int num_events;
  392. struct sdma_script_start_addrs *script_addrs;
  393. };
  394. struct sdma_engine {
  395. struct device *dev;
  396. struct device_dma_parameters dma_parms;
  397. struct sdma_channel channel[MAX_DMA_CHANNELS];
  398. struct sdma_channel_control *channel_control;
  399. void __iomem *regs;
  400. struct sdma_context_data *context;
  401. dma_addr_t context_phys;
  402. struct dma_device dma_device;
  403. struct clk *clk_ipg;
  404. struct clk *clk_ahb;
  405. spinlock_t channel_0_lock;
  406. u32 script_number;
  407. struct sdma_script_start_addrs *script_addrs;
  408. const struct sdma_driver_data *drvdata;
  409. u32 spba_start_addr;
  410. u32 spba_end_addr;
  411. unsigned int irq;
  412. dma_addr_t bd0_phys;
  413. struct sdma_buffer_descriptor *bd0;
  414. };
  415. static struct sdma_driver_data sdma_imx31 = {
  416. .chnenbl0 = SDMA_CHNENBL0_IMX31,
  417. .num_events = 32,
  418. };
  419. static struct sdma_script_start_addrs sdma_script_imx25 = {
  420. .ap_2_ap_addr = 729,
  421. .uart_2_mcu_addr = 904,
  422. .per_2_app_addr = 1255,
  423. .mcu_2_app_addr = 834,
  424. .uartsh_2_mcu_addr = 1120,
  425. .per_2_shp_addr = 1329,
  426. .mcu_2_shp_addr = 1048,
  427. .ata_2_mcu_addr = 1560,
  428. .mcu_2_ata_addr = 1479,
  429. .app_2_per_addr = 1189,
  430. .app_2_mcu_addr = 770,
  431. .shp_2_per_addr = 1407,
  432. .shp_2_mcu_addr = 979,
  433. };
  434. static struct sdma_driver_data sdma_imx25 = {
  435. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  436. .num_events = 48,
  437. .script_addrs = &sdma_script_imx25,
  438. };
  439. static struct sdma_driver_data sdma_imx35 = {
  440. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  441. .num_events = 48,
  442. };
  443. static struct sdma_script_start_addrs sdma_script_imx51 = {
  444. .ap_2_ap_addr = 642,
  445. .uart_2_mcu_addr = 817,
  446. .mcu_2_app_addr = 747,
  447. .mcu_2_shp_addr = 961,
  448. .ata_2_mcu_addr = 1473,
  449. .mcu_2_ata_addr = 1392,
  450. .app_2_per_addr = 1033,
  451. .app_2_mcu_addr = 683,
  452. .shp_2_per_addr = 1251,
  453. .shp_2_mcu_addr = 892,
  454. };
  455. static struct sdma_driver_data sdma_imx51 = {
  456. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  457. .num_events = 48,
  458. .script_addrs = &sdma_script_imx51,
  459. };
  460. static struct sdma_script_start_addrs sdma_script_imx53 = {
  461. .ap_2_ap_addr = 642,
  462. .app_2_mcu_addr = 683,
  463. .mcu_2_app_addr = 747,
  464. .uart_2_mcu_addr = 817,
  465. .shp_2_mcu_addr = 891,
  466. .mcu_2_shp_addr = 960,
  467. .uartsh_2_mcu_addr = 1032,
  468. .spdif_2_mcu_addr = 1100,
  469. .mcu_2_spdif_addr = 1134,
  470. .firi_2_mcu_addr = 1193,
  471. .mcu_2_firi_addr = 1290,
  472. };
  473. static struct sdma_driver_data sdma_imx53 = {
  474. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  475. .num_events = 48,
  476. .script_addrs = &sdma_script_imx53,
  477. };
  478. static struct sdma_script_start_addrs sdma_script_imx6q = {
  479. .ap_2_ap_addr = 642,
  480. .uart_2_mcu_addr = 817,
  481. .mcu_2_app_addr = 747,
  482. .per_2_per_addr = 6331,
  483. .uartsh_2_mcu_addr = 1032,
  484. .mcu_2_shp_addr = 960,
  485. .app_2_mcu_addr = 683,
  486. .shp_2_mcu_addr = 891,
  487. .spdif_2_mcu_addr = 1100,
  488. .mcu_2_spdif_addr = 1134,
  489. };
  490. static struct sdma_driver_data sdma_imx6q = {
  491. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  492. .num_events = 48,
  493. .script_addrs = &sdma_script_imx6q,
  494. };
  495. static struct sdma_script_start_addrs sdma_script_imx7d = {
  496. .ap_2_ap_addr = 644,
  497. .uart_2_mcu_addr = 819,
  498. .mcu_2_app_addr = 749,
  499. .uartsh_2_mcu_addr = 1034,
  500. .mcu_2_shp_addr = 962,
  501. .app_2_mcu_addr = 685,
  502. .shp_2_mcu_addr = 893,
  503. .spdif_2_mcu_addr = 1102,
  504. .mcu_2_spdif_addr = 1136,
  505. };
  506. static struct sdma_driver_data sdma_imx7d = {
  507. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  508. .num_events = 48,
  509. .script_addrs = &sdma_script_imx7d,
  510. };
  511. static const struct platform_device_id sdma_devtypes[] = {
  512. {
  513. .name = "imx25-sdma",
  514. .driver_data = (unsigned long)&sdma_imx25,
  515. }, {
  516. .name = "imx31-sdma",
  517. .driver_data = (unsigned long)&sdma_imx31,
  518. }, {
  519. .name = "imx35-sdma",
  520. .driver_data = (unsigned long)&sdma_imx35,
  521. }, {
  522. .name = "imx51-sdma",
  523. .driver_data = (unsigned long)&sdma_imx51,
  524. }, {
  525. .name = "imx53-sdma",
  526. .driver_data = (unsigned long)&sdma_imx53,
  527. }, {
  528. .name = "imx6q-sdma",
  529. .driver_data = (unsigned long)&sdma_imx6q,
  530. }, {
  531. .name = "imx7d-sdma",
  532. .driver_data = (unsigned long)&sdma_imx7d,
  533. }, {
  534. /* sentinel */
  535. }
  536. };
  537. MODULE_DEVICE_TABLE(platform, sdma_devtypes);
  538. static const struct of_device_id sdma_dt_ids[] = {
  539. { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
  540. { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
  541. { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
  542. { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
  543. { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
  544. { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
  545. { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
  546. { /* sentinel */ }
  547. };
  548. MODULE_DEVICE_TABLE(of, sdma_dt_ids);
  549. #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
  550. #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
  551. #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
  552. #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
  553. static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
  554. {
  555. u32 chnenbl0 = sdma->drvdata->chnenbl0;
  556. return chnenbl0 + event * 4;
  557. }
  558. static int sdma_config_ownership(struct sdma_channel *sdmac,
  559. bool event_override, bool mcu_override, bool dsp_override)
  560. {
  561. struct sdma_engine *sdma = sdmac->sdma;
  562. int channel = sdmac->channel;
  563. unsigned long evt, mcu, dsp;
  564. if (event_override && mcu_override && dsp_override)
  565. return -EINVAL;
  566. evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
  567. mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
  568. dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
  569. if (dsp_override)
  570. __clear_bit(channel, &dsp);
  571. else
  572. __set_bit(channel, &dsp);
  573. if (event_override)
  574. __clear_bit(channel, &evt);
  575. else
  576. __set_bit(channel, &evt);
  577. if (mcu_override)
  578. __clear_bit(channel, &mcu);
  579. else
  580. __set_bit(channel, &mcu);
  581. writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
  582. writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
  583. writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
  584. return 0;
  585. }
  586. static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
  587. {
  588. writel(BIT(channel), sdma->regs + SDMA_H_START);
  589. }
  590. /*
  591. * sdma_run_channel0 - run a channel and wait till it's done
  592. */
  593. static int sdma_run_channel0(struct sdma_engine *sdma)
  594. {
  595. int ret;
  596. u32 reg;
  597. sdma_enable_channel(sdma, 0);
  598. ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
  599. reg, !(reg & 1), 1, 500);
  600. if (ret)
  601. dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
  602. /* Set bits of CONFIG register with dynamic context switching */
  603. if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
  604. writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
  605. return ret;
  606. }
  607. static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
  608. u32 address)
  609. {
  610. struct sdma_buffer_descriptor *bd0 = sdma->bd0;
  611. void *buf_virt;
  612. dma_addr_t buf_phys;
  613. int ret;
  614. unsigned long flags;
  615. buf_virt = dma_alloc_coherent(NULL,
  616. size,
  617. &buf_phys, GFP_KERNEL);
  618. if (!buf_virt) {
  619. return -ENOMEM;
  620. }
  621. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  622. bd0->mode.command = C0_SETPM;
  623. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  624. bd0->mode.count = size / 2;
  625. bd0->buffer_addr = buf_phys;
  626. bd0->ext_buffer_addr = address;
  627. memcpy(buf_virt, buf, size);
  628. ret = sdma_run_channel0(sdma);
  629. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  630. dma_free_coherent(NULL, size, buf_virt, buf_phys);
  631. return ret;
  632. }
  633. static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
  634. {
  635. struct sdma_engine *sdma = sdmac->sdma;
  636. int channel = sdmac->channel;
  637. unsigned long val;
  638. u32 chnenbl = chnenbl_ofs(sdma, event);
  639. val = readl_relaxed(sdma->regs + chnenbl);
  640. __set_bit(channel, &val);
  641. writel_relaxed(val, sdma->regs + chnenbl);
  642. }
  643. static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
  644. {
  645. struct sdma_engine *sdma = sdmac->sdma;
  646. int channel = sdmac->channel;
  647. u32 chnenbl = chnenbl_ofs(sdma, event);
  648. unsigned long val;
  649. val = readl_relaxed(sdma->regs + chnenbl);
  650. __clear_bit(channel, &val);
  651. writel_relaxed(val, sdma->regs + chnenbl);
  652. }
  653. static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
  654. {
  655. return container_of(t, struct sdma_desc, vd.tx);
  656. }
  657. static void sdma_start_desc(struct sdma_channel *sdmac)
  658. {
  659. struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
  660. struct sdma_desc *desc;
  661. struct sdma_engine *sdma = sdmac->sdma;
  662. int channel = sdmac->channel;
  663. if (!vd) {
  664. sdmac->desc = NULL;
  665. return;
  666. }
  667. sdmac->desc = desc = to_sdma_desc(&vd->tx);
  668. /*
  669. * Do not delete the node in desc_issued list in cyclic mode, otherwise
  670. * the desc allocated will never be freed in vchan_dma_desc_free_list
  671. */
  672. if (!(sdmac->flags & IMX_DMA_SG_LOOP))
  673. list_del(&vd->node);
  674. sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
  675. sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
  676. sdma_enable_channel(sdma, sdmac->channel);
  677. }
  678. static void sdma_update_channel_loop(struct sdma_channel *sdmac)
  679. {
  680. struct sdma_buffer_descriptor *bd;
  681. int error = 0;
  682. enum dma_status old_status = sdmac->status;
  683. /*
  684. * loop mode. Iterate over descriptors, re-setup them and
  685. * call callback function.
  686. */
  687. while (sdmac->desc) {
  688. struct sdma_desc *desc = sdmac->desc;
  689. bd = &desc->bd[desc->buf_tail];
  690. if (bd->mode.status & BD_DONE)
  691. break;
  692. if (bd->mode.status & BD_RROR) {
  693. bd->mode.status &= ~BD_RROR;
  694. sdmac->status = DMA_ERROR;
  695. error = -EIO;
  696. }
  697. /*
  698. * We use bd->mode.count to calculate the residue, since contains
  699. * the number of bytes present in the current buffer descriptor.
  700. */
  701. desc->chn_real_count = bd->mode.count;
  702. bd->mode.status |= BD_DONE;
  703. bd->mode.count = desc->period_len;
  704. desc->buf_ptail = desc->buf_tail;
  705. desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
  706. /*
  707. * The callback is called from the interrupt context in order
  708. * to reduce latency and to avoid the risk of altering the
  709. * SDMA transaction status by the time the client tasklet is
  710. * executed.
  711. */
  712. spin_unlock(&sdmac->vc.lock);
  713. dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
  714. spin_lock(&sdmac->vc.lock);
  715. if (error)
  716. sdmac->status = old_status;
  717. }
  718. }
  719. static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
  720. {
  721. struct sdma_channel *sdmac = (struct sdma_channel *) data;
  722. struct sdma_buffer_descriptor *bd;
  723. int i, error = 0;
  724. sdmac->desc->chn_real_count = 0;
  725. /*
  726. * non loop mode. Iterate over all descriptors, collect
  727. * errors and call callback function
  728. */
  729. for (i = 0; i < sdmac->desc->num_bd; i++) {
  730. bd = &sdmac->desc->bd[i];
  731. if (bd->mode.status & (BD_DONE | BD_RROR))
  732. error = -EIO;
  733. sdmac->desc->chn_real_count += bd->mode.count;
  734. }
  735. if (error)
  736. sdmac->status = DMA_ERROR;
  737. else
  738. sdmac->status = DMA_COMPLETE;
  739. }
  740. static irqreturn_t sdma_int_handler(int irq, void *dev_id)
  741. {
  742. struct sdma_engine *sdma = dev_id;
  743. unsigned long stat;
  744. stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
  745. writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
  746. /* channel 0 is special and not handled here, see run_channel0() */
  747. stat &= ~1;
  748. while (stat) {
  749. int channel = fls(stat) - 1;
  750. struct sdma_channel *sdmac = &sdma->channel[channel];
  751. struct sdma_desc *desc;
  752. spin_lock(&sdmac->vc.lock);
  753. desc = sdmac->desc;
  754. if (desc) {
  755. if (sdmac->flags & IMX_DMA_SG_LOOP) {
  756. sdma_update_channel_loop(sdmac);
  757. } else {
  758. mxc_sdma_handle_channel_normal(sdmac);
  759. vchan_cookie_complete(&desc->vd);
  760. sdma_start_desc(sdmac);
  761. }
  762. }
  763. spin_unlock(&sdmac->vc.lock);
  764. __clear_bit(channel, &stat);
  765. }
  766. return IRQ_HANDLED;
  767. }
  768. /*
  769. * sets the pc of SDMA script according to the peripheral type
  770. */
  771. static void sdma_get_pc(struct sdma_channel *sdmac,
  772. enum sdma_peripheral_type peripheral_type)
  773. {
  774. struct sdma_engine *sdma = sdmac->sdma;
  775. int per_2_emi = 0, emi_2_per = 0;
  776. /*
  777. * These are needed once we start to support transfers between
  778. * two peripherals or memory-to-memory transfers
  779. */
  780. int per_2_per = 0, emi_2_emi = 0;
  781. sdmac->pc_from_device = 0;
  782. sdmac->pc_to_device = 0;
  783. sdmac->device_to_device = 0;
  784. sdmac->pc_to_pc = 0;
  785. switch (peripheral_type) {
  786. case IMX_DMATYPE_MEMORY:
  787. emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
  788. break;
  789. case IMX_DMATYPE_DSP:
  790. emi_2_per = sdma->script_addrs->bp_2_ap_addr;
  791. per_2_emi = sdma->script_addrs->ap_2_bp_addr;
  792. break;
  793. case IMX_DMATYPE_FIRI:
  794. per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
  795. emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
  796. break;
  797. case IMX_DMATYPE_UART:
  798. per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
  799. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  800. break;
  801. case IMX_DMATYPE_UART_SP:
  802. per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
  803. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  804. break;
  805. case IMX_DMATYPE_ATA:
  806. per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
  807. emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
  808. break;
  809. case IMX_DMATYPE_CSPI:
  810. case IMX_DMATYPE_EXT:
  811. case IMX_DMATYPE_SSI:
  812. case IMX_DMATYPE_SAI:
  813. per_2_emi = sdma->script_addrs->app_2_mcu_addr;
  814. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  815. break;
  816. case IMX_DMATYPE_SSI_DUAL:
  817. per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
  818. emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
  819. break;
  820. case IMX_DMATYPE_SSI_SP:
  821. case IMX_DMATYPE_MMC:
  822. case IMX_DMATYPE_SDHC:
  823. case IMX_DMATYPE_CSPI_SP:
  824. case IMX_DMATYPE_ESAI:
  825. case IMX_DMATYPE_MSHC_SP:
  826. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  827. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  828. break;
  829. case IMX_DMATYPE_ASRC:
  830. per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
  831. emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
  832. per_2_per = sdma->script_addrs->per_2_per_addr;
  833. break;
  834. case IMX_DMATYPE_ASRC_SP:
  835. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  836. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  837. per_2_per = sdma->script_addrs->per_2_per_addr;
  838. break;
  839. case IMX_DMATYPE_MSHC:
  840. per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
  841. emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
  842. break;
  843. case IMX_DMATYPE_CCM:
  844. per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
  845. break;
  846. case IMX_DMATYPE_SPDIF:
  847. per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
  848. emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
  849. break;
  850. case IMX_DMATYPE_IPU_MEMORY:
  851. emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
  852. break;
  853. default:
  854. break;
  855. }
  856. sdmac->pc_from_device = per_2_emi;
  857. sdmac->pc_to_device = emi_2_per;
  858. sdmac->device_to_device = per_2_per;
  859. sdmac->pc_to_pc = emi_2_emi;
  860. }
  861. static int sdma_load_context(struct sdma_channel *sdmac)
  862. {
  863. struct sdma_engine *sdma = sdmac->sdma;
  864. int channel = sdmac->channel;
  865. int load_address;
  866. struct sdma_context_data *context = sdma->context;
  867. struct sdma_buffer_descriptor *bd0 = sdma->bd0;
  868. int ret;
  869. unsigned long flags;
  870. if (sdmac->direction == DMA_DEV_TO_MEM)
  871. load_address = sdmac->pc_from_device;
  872. else if (sdmac->direction == DMA_DEV_TO_DEV)
  873. load_address = sdmac->device_to_device;
  874. else if (sdmac->direction == DMA_MEM_TO_MEM)
  875. load_address = sdmac->pc_to_pc;
  876. else
  877. load_address = sdmac->pc_to_device;
  878. if (load_address < 0)
  879. return load_address;
  880. dev_dbg(sdma->dev, "load_address = %d\n", load_address);
  881. dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
  882. dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
  883. dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
  884. dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
  885. dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
  886. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  887. memset(context, 0, sizeof(*context));
  888. context->channel_state.pc = load_address;
  889. /* Send by context the event mask,base address for peripheral
  890. * and watermark level
  891. */
  892. context->gReg[0] = sdmac->event_mask[1];
  893. context->gReg[1] = sdmac->event_mask[0];
  894. context->gReg[2] = sdmac->per_addr;
  895. context->gReg[6] = sdmac->shp_addr;
  896. context->gReg[7] = sdmac->watermark_level;
  897. bd0->mode.command = C0_SETDM;
  898. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  899. bd0->mode.count = sizeof(*context) / 4;
  900. bd0->buffer_addr = sdma->context_phys;
  901. bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
  902. ret = sdma_run_channel0(sdma);
  903. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  904. return ret;
  905. }
  906. static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
  907. {
  908. return container_of(chan, struct sdma_channel, vc.chan);
  909. }
  910. static int sdma_disable_channel(struct dma_chan *chan)
  911. {
  912. struct sdma_channel *sdmac = to_sdma_chan(chan);
  913. struct sdma_engine *sdma = sdmac->sdma;
  914. int channel = sdmac->channel;
  915. writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
  916. sdmac->status = DMA_ERROR;
  917. return 0;
  918. }
  919. static int sdma_disable_channel_with_delay(struct dma_chan *chan)
  920. {
  921. struct sdma_channel *sdmac = to_sdma_chan(chan);
  922. unsigned long flags;
  923. LIST_HEAD(head);
  924. sdma_disable_channel(chan);
  925. spin_lock_irqsave(&sdmac->vc.lock, flags);
  926. vchan_get_all_descriptors(&sdmac->vc, &head);
  927. sdmac->desc = NULL;
  928. spin_unlock_irqrestore(&sdmac->vc.lock, flags);
  929. vchan_dma_desc_free_list(&sdmac->vc, &head);
  930. /*
  931. * According to NXP R&D team a delay of one BD SDMA cost time
  932. * (maximum is 1ms) should be added after disable of the channel
  933. * bit, to ensure SDMA core has really been stopped after SDMA
  934. * clients call .device_terminate_all.
  935. */
  936. mdelay(1);
  937. return 0;
  938. }
  939. static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
  940. {
  941. struct sdma_engine *sdma = sdmac->sdma;
  942. int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
  943. int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
  944. set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
  945. set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
  946. if (sdmac->event_id0 > 31)
  947. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
  948. if (sdmac->event_id1 > 31)
  949. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
  950. /*
  951. * If LWML(src_maxburst) > HWML(dst_maxburst), we need
  952. * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
  953. * r0(event_mask[1]) and r1(event_mask[0]).
  954. */
  955. if (lwml > hwml) {
  956. sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
  957. SDMA_WATERMARK_LEVEL_HWML);
  958. sdmac->watermark_level |= hwml;
  959. sdmac->watermark_level |= lwml << 16;
  960. swap(sdmac->event_mask[0], sdmac->event_mask[1]);
  961. }
  962. if (sdmac->per_address2 >= sdma->spba_start_addr &&
  963. sdmac->per_address2 <= sdma->spba_end_addr)
  964. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
  965. if (sdmac->per_address >= sdma->spba_start_addr &&
  966. sdmac->per_address <= sdma->spba_end_addr)
  967. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
  968. sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
  969. }
  970. static int sdma_config_channel(struct dma_chan *chan)
  971. {
  972. struct sdma_channel *sdmac = to_sdma_chan(chan);
  973. int ret;
  974. sdma_disable_channel(chan);
  975. sdmac->event_mask[0] = 0;
  976. sdmac->event_mask[1] = 0;
  977. sdmac->shp_addr = 0;
  978. sdmac->per_addr = 0;
  979. if (sdmac->event_id0) {
  980. if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
  981. return -EINVAL;
  982. sdma_event_enable(sdmac, sdmac->event_id0);
  983. }
  984. if (sdmac->event_id1) {
  985. if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
  986. return -EINVAL;
  987. sdma_event_enable(sdmac, sdmac->event_id1);
  988. }
  989. switch (sdmac->peripheral_type) {
  990. case IMX_DMATYPE_DSP:
  991. sdma_config_ownership(sdmac, false, true, true);
  992. break;
  993. case IMX_DMATYPE_MEMORY:
  994. sdma_config_ownership(sdmac, false, true, false);
  995. break;
  996. default:
  997. sdma_config_ownership(sdmac, true, true, false);
  998. break;
  999. }
  1000. sdma_get_pc(sdmac, sdmac->peripheral_type);
  1001. if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
  1002. (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
  1003. /* Handle multiple event channels differently */
  1004. if (sdmac->event_id1) {
  1005. if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
  1006. sdmac->peripheral_type == IMX_DMATYPE_ASRC)
  1007. sdma_set_watermarklevel_for_p2p(sdmac);
  1008. } else
  1009. __set_bit(sdmac->event_id0, sdmac->event_mask);
  1010. /* Address */
  1011. sdmac->shp_addr = sdmac->per_address;
  1012. sdmac->per_addr = sdmac->per_address2;
  1013. } else {
  1014. sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
  1015. }
  1016. ret = sdma_load_context(sdmac);
  1017. return ret;
  1018. }
  1019. static int sdma_set_channel_priority(struct sdma_channel *sdmac,
  1020. unsigned int priority)
  1021. {
  1022. struct sdma_engine *sdma = sdmac->sdma;
  1023. int channel = sdmac->channel;
  1024. if (priority < MXC_SDMA_MIN_PRIORITY
  1025. || priority > MXC_SDMA_MAX_PRIORITY) {
  1026. return -EINVAL;
  1027. }
  1028. writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
  1029. return 0;
  1030. }
  1031. static int sdma_request_channel0(struct sdma_engine *sdma)
  1032. {
  1033. int ret = -EBUSY;
  1034. sdma->bd0 = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdma->bd0_phys,
  1035. GFP_NOWAIT);
  1036. if (!sdma->bd0) {
  1037. ret = -ENOMEM;
  1038. goto out;
  1039. }
  1040. sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
  1041. sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
  1042. sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
  1043. return 0;
  1044. out:
  1045. return ret;
  1046. }
  1047. static int sdma_alloc_bd(struct sdma_desc *desc)
  1048. {
  1049. int ret = 0;
  1050. desc->bd = dma_pool_alloc(desc->sdmac->bd_pool, GFP_NOWAIT,
  1051. &desc->bd_phys);
  1052. if (!desc->bd) {
  1053. ret = -ENOMEM;
  1054. goto out;
  1055. }
  1056. out:
  1057. return ret;
  1058. }
  1059. static void sdma_free_bd(struct sdma_desc *desc)
  1060. {
  1061. dma_pool_free(desc->sdmac->bd_pool, desc->bd, desc->bd_phys);
  1062. }
  1063. static void sdma_desc_free(struct virt_dma_desc *vd)
  1064. {
  1065. struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
  1066. sdma_free_bd(desc);
  1067. kfree(desc);
  1068. }
  1069. static int sdma_alloc_chan_resources(struct dma_chan *chan)
  1070. {
  1071. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1072. struct imx_dma_data *data = chan->private;
  1073. struct imx_dma_data mem_data;
  1074. int prio, ret;
  1075. /*
  1076. * MEMCPY may never setup chan->private by filter function such as
  1077. * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
  1078. * Please note in any other slave case, you have to setup chan->private
  1079. * with 'struct imx_dma_data' in your own filter function if you want to
  1080. * request dma channel by dma_request_channel() rather than
  1081. * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
  1082. * to warn you to correct your filter function.
  1083. */
  1084. if (!data) {
  1085. dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
  1086. mem_data.priority = 2;
  1087. mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
  1088. mem_data.dma_request = 0;
  1089. mem_data.dma_request2 = 0;
  1090. data = &mem_data;
  1091. sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
  1092. }
  1093. switch (data->priority) {
  1094. case DMA_PRIO_HIGH:
  1095. prio = 3;
  1096. break;
  1097. case DMA_PRIO_MEDIUM:
  1098. prio = 2;
  1099. break;
  1100. case DMA_PRIO_LOW:
  1101. default:
  1102. prio = 1;
  1103. break;
  1104. }
  1105. sdmac->peripheral_type = data->peripheral_type;
  1106. sdmac->event_id0 = data->dma_request;
  1107. sdmac->event_id1 = data->dma_request2;
  1108. ret = clk_enable(sdmac->sdma->clk_ipg);
  1109. if (ret)
  1110. return ret;
  1111. ret = clk_enable(sdmac->sdma->clk_ahb);
  1112. if (ret)
  1113. goto disable_clk_ipg;
  1114. ret = sdma_set_channel_priority(sdmac, prio);
  1115. if (ret)
  1116. goto disable_clk_ahb;
  1117. sdmac->bd_pool = dma_pool_create("bd_pool", chan->device->dev,
  1118. sizeof(struct sdma_buffer_descriptor),
  1119. 32, 0);
  1120. return 0;
  1121. disable_clk_ahb:
  1122. clk_disable(sdmac->sdma->clk_ahb);
  1123. disable_clk_ipg:
  1124. clk_disable(sdmac->sdma->clk_ipg);
  1125. return ret;
  1126. }
  1127. static void sdma_free_chan_resources(struct dma_chan *chan)
  1128. {
  1129. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1130. struct sdma_engine *sdma = sdmac->sdma;
  1131. sdma_disable_channel_with_delay(chan);
  1132. if (sdmac->event_id0)
  1133. sdma_event_disable(sdmac, sdmac->event_id0);
  1134. if (sdmac->event_id1)
  1135. sdma_event_disable(sdmac, sdmac->event_id1);
  1136. sdmac->event_id0 = 0;
  1137. sdmac->event_id1 = 0;
  1138. sdma_set_channel_priority(sdmac, 0);
  1139. clk_disable(sdma->clk_ipg);
  1140. clk_disable(sdma->clk_ahb);
  1141. dma_pool_destroy(sdmac->bd_pool);
  1142. sdmac->bd_pool = NULL;
  1143. }
  1144. static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
  1145. enum dma_transfer_direction direction, u32 bds)
  1146. {
  1147. struct sdma_desc *desc;
  1148. desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
  1149. if (!desc)
  1150. goto err_out;
  1151. sdmac->status = DMA_IN_PROGRESS;
  1152. sdmac->direction = direction;
  1153. sdmac->flags = 0;
  1154. desc->chn_count = 0;
  1155. desc->chn_real_count = 0;
  1156. desc->buf_tail = 0;
  1157. desc->buf_ptail = 0;
  1158. desc->sdmac = sdmac;
  1159. desc->num_bd = bds;
  1160. if (sdma_alloc_bd(desc))
  1161. goto err_desc_out;
  1162. /* No slave_config called in MEMCPY case, so do here */
  1163. if (direction == DMA_MEM_TO_MEM)
  1164. sdma_config_ownership(sdmac, false, true, false);
  1165. if (sdma_load_context(sdmac))
  1166. goto err_desc_out;
  1167. return desc;
  1168. err_desc_out:
  1169. kfree(desc);
  1170. err_out:
  1171. return NULL;
  1172. }
  1173. static struct dma_async_tx_descriptor *sdma_prep_memcpy(
  1174. struct dma_chan *chan, dma_addr_t dma_dst,
  1175. dma_addr_t dma_src, size_t len, unsigned long flags)
  1176. {
  1177. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1178. struct sdma_engine *sdma = sdmac->sdma;
  1179. int channel = sdmac->channel;
  1180. size_t count;
  1181. int i = 0, param;
  1182. struct sdma_buffer_descriptor *bd;
  1183. struct sdma_desc *desc;
  1184. if (!chan || !len)
  1185. return NULL;
  1186. dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
  1187. &dma_src, &dma_dst, len, channel);
  1188. desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
  1189. len / SDMA_BD_MAX_CNT + 1);
  1190. if (!desc)
  1191. return NULL;
  1192. do {
  1193. count = min_t(size_t, len, SDMA_BD_MAX_CNT);
  1194. bd = &desc->bd[i];
  1195. bd->buffer_addr = dma_src;
  1196. bd->ext_buffer_addr = dma_dst;
  1197. bd->mode.count = count;
  1198. desc->chn_count += count;
  1199. bd->mode.command = 0;
  1200. dma_src += count;
  1201. dma_dst += count;
  1202. len -= count;
  1203. i++;
  1204. param = BD_DONE | BD_EXTD | BD_CONT;
  1205. /* last bd */
  1206. if (!len) {
  1207. param |= BD_INTR;
  1208. param |= BD_LAST;
  1209. param &= ~BD_CONT;
  1210. }
  1211. dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
  1212. i, count, bd->buffer_addr,
  1213. param & BD_WRAP ? "wrap" : "",
  1214. param & BD_INTR ? " intr" : "");
  1215. bd->mode.status = param;
  1216. } while (len);
  1217. return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
  1218. }
  1219. static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
  1220. struct dma_chan *chan, struct scatterlist *sgl,
  1221. unsigned int sg_len, enum dma_transfer_direction direction,
  1222. unsigned long flags, void *context)
  1223. {
  1224. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1225. struct sdma_engine *sdma = sdmac->sdma;
  1226. int i, count;
  1227. int channel = sdmac->channel;
  1228. struct scatterlist *sg;
  1229. struct sdma_desc *desc;
  1230. desc = sdma_transfer_init(sdmac, direction, sg_len);
  1231. if (!desc)
  1232. goto err_out;
  1233. dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
  1234. sg_len, channel);
  1235. for_each_sg(sgl, sg, sg_len, i) {
  1236. struct sdma_buffer_descriptor *bd = &desc->bd[i];
  1237. int param;
  1238. bd->buffer_addr = sg->dma_address;
  1239. count = sg_dma_len(sg);
  1240. if (count > SDMA_BD_MAX_CNT) {
  1241. dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
  1242. channel, count, SDMA_BD_MAX_CNT);
  1243. goto err_bd_out;
  1244. }
  1245. bd->mode.count = count;
  1246. desc->chn_count += count;
  1247. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  1248. goto err_bd_out;
  1249. switch (sdmac->word_size) {
  1250. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1251. bd->mode.command = 0;
  1252. if (count & 3 || sg->dma_address & 3)
  1253. goto err_bd_out;
  1254. break;
  1255. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1256. bd->mode.command = 2;
  1257. if (count & 1 || sg->dma_address & 1)
  1258. goto err_bd_out;
  1259. break;
  1260. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1261. bd->mode.command = 1;
  1262. break;
  1263. default:
  1264. goto err_bd_out;
  1265. }
  1266. param = BD_DONE | BD_EXTD | BD_CONT;
  1267. if (i + 1 == sg_len) {
  1268. param |= BD_INTR;
  1269. param |= BD_LAST;
  1270. param &= ~BD_CONT;
  1271. }
  1272. dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
  1273. i, count, (u64)sg->dma_address,
  1274. param & BD_WRAP ? "wrap" : "",
  1275. param & BD_INTR ? " intr" : "");
  1276. bd->mode.status = param;
  1277. }
  1278. return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
  1279. err_bd_out:
  1280. sdma_free_bd(desc);
  1281. kfree(desc);
  1282. err_out:
  1283. sdmac->status = DMA_ERROR;
  1284. return NULL;
  1285. }
  1286. static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
  1287. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  1288. size_t period_len, enum dma_transfer_direction direction,
  1289. unsigned long flags)
  1290. {
  1291. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1292. struct sdma_engine *sdma = sdmac->sdma;
  1293. int num_periods = buf_len / period_len;
  1294. int channel = sdmac->channel;
  1295. int i = 0, buf = 0;
  1296. struct sdma_desc *desc;
  1297. dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
  1298. desc = sdma_transfer_init(sdmac, direction, num_periods);
  1299. if (!desc)
  1300. goto err_out;
  1301. desc->period_len = period_len;
  1302. sdmac->flags |= IMX_DMA_SG_LOOP;
  1303. if (period_len > SDMA_BD_MAX_CNT) {
  1304. dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
  1305. channel, period_len, SDMA_BD_MAX_CNT);
  1306. goto err_bd_out;
  1307. }
  1308. while (buf < buf_len) {
  1309. struct sdma_buffer_descriptor *bd = &desc->bd[i];
  1310. int param;
  1311. bd->buffer_addr = dma_addr;
  1312. bd->mode.count = period_len;
  1313. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  1314. goto err_bd_out;
  1315. if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
  1316. bd->mode.command = 0;
  1317. else
  1318. bd->mode.command = sdmac->word_size;
  1319. param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
  1320. if (i + 1 == num_periods)
  1321. param |= BD_WRAP;
  1322. dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
  1323. i, period_len, (u64)dma_addr,
  1324. param & BD_WRAP ? "wrap" : "",
  1325. param & BD_INTR ? " intr" : "");
  1326. bd->mode.status = param;
  1327. dma_addr += period_len;
  1328. buf += period_len;
  1329. i++;
  1330. }
  1331. return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
  1332. err_bd_out:
  1333. sdma_free_bd(desc);
  1334. kfree(desc);
  1335. err_out:
  1336. sdmac->status = DMA_ERROR;
  1337. return NULL;
  1338. }
  1339. static int sdma_config(struct dma_chan *chan,
  1340. struct dma_slave_config *dmaengine_cfg)
  1341. {
  1342. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1343. if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
  1344. sdmac->per_address = dmaengine_cfg->src_addr;
  1345. sdmac->watermark_level = dmaengine_cfg->src_maxburst *
  1346. dmaengine_cfg->src_addr_width;
  1347. sdmac->word_size = dmaengine_cfg->src_addr_width;
  1348. } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) {
  1349. sdmac->per_address2 = dmaengine_cfg->src_addr;
  1350. sdmac->per_address = dmaengine_cfg->dst_addr;
  1351. sdmac->watermark_level = dmaengine_cfg->src_maxburst &
  1352. SDMA_WATERMARK_LEVEL_LWML;
  1353. sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
  1354. SDMA_WATERMARK_LEVEL_HWML;
  1355. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  1356. } else {
  1357. sdmac->per_address = dmaengine_cfg->dst_addr;
  1358. sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
  1359. dmaengine_cfg->dst_addr_width;
  1360. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  1361. }
  1362. sdmac->direction = dmaengine_cfg->direction;
  1363. return sdma_config_channel(chan);
  1364. }
  1365. static enum dma_status sdma_tx_status(struct dma_chan *chan,
  1366. dma_cookie_t cookie,
  1367. struct dma_tx_state *txstate)
  1368. {
  1369. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1370. struct sdma_desc *desc;
  1371. u32 residue;
  1372. struct virt_dma_desc *vd;
  1373. enum dma_status ret;
  1374. unsigned long flags;
  1375. ret = dma_cookie_status(chan, cookie, txstate);
  1376. if (ret == DMA_COMPLETE || !txstate)
  1377. return ret;
  1378. spin_lock_irqsave(&sdmac->vc.lock, flags);
  1379. vd = vchan_find_desc(&sdmac->vc, cookie);
  1380. if (vd) {
  1381. desc = to_sdma_desc(&vd->tx);
  1382. if (sdmac->flags & IMX_DMA_SG_LOOP)
  1383. residue = (desc->num_bd - desc->buf_ptail) *
  1384. desc->period_len - desc->chn_real_count;
  1385. else
  1386. residue = desc->chn_count - desc->chn_real_count;
  1387. } else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie) {
  1388. residue = sdmac->desc->chn_count - sdmac->desc->chn_real_count;
  1389. } else {
  1390. residue = 0;
  1391. }
  1392. spin_unlock_irqrestore(&sdmac->vc.lock, flags);
  1393. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  1394. residue);
  1395. return sdmac->status;
  1396. }
  1397. static void sdma_issue_pending(struct dma_chan *chan)
  1398. {
  1399. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1400. unsigned long flags;
  1401. spin_lock_irqsave(&sdmac->vc.lock, flags);
  1402. if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
  1403. sdma_start_desc(sdmac);
  1404. spin_unlock_irqrestore(&sdmac->vc.lock, flags);
  1405. }
  1406. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
  1407. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
  1408. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
  1409. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 42
  1410. static void sdma_add_scripts(struct sdma_engine *sdma,
  1411. const struct sdma_script_start_addrs *addr)
  1412. {
  1413. s32 *addr_arr = (u32 *)addr;
  1414. s32 *saddr_arr = (u32 *)sdma->script_addrs;
  1415. int i;
  1416. /* use the default firmware in ROM if missing external firmware */
  1417. if (!sdma->script_number)
  1418. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1419. for (i = 0; i < sdma->script_number; i++)
  1420. if (addr_arr[i] > 0)
  1421. saddr_arr[i] = addr_arr[i];
  1422. }
  1423. static void sdma_load_firmware(const struct firmware *fw, void *context)
  1424. {
  1425. struct sdma_engine *sdma = context;
  1426. const struct sdma_firmware_header *header;
  1427. const struct sdma_script_start_addrs *addr;
  1428. unsigned short *ram_code;
  1429. if (!fw) {
  1430. dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
  1431. /* In this case we just use the ROM firmware. */
  1432. return;
  1433. }
  1434. if (fw->size < sizeof(*header))
  1435. goto err_firmware;
  1436. header = (struct sdma_firmware_header *)fw->data;
  1437. if (header->magic != SDMA_FIRMWARE_MAGIC)
  1438. goto err_firmware;
  1439. if (header->ram_code_start + header->ram_code_size > fw->size)
  1440. goto err_firmware;
  1441. switch (header->version_major) {
  1442. case 1:
  1443. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1444. break;
  1445. case 2:
  1446. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
  1447. break;
  1448. case 3:
  1449. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
  1450. break;
  1451. case 4:
  1452. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
  1453. break;
  1454. default:
  1455. dev_err(sdma->dev, "unknown firmware version\n");
  1456. goto err_firmware;
  1457. }
  1458. addr = (void *)header + header->script_addrs_start;
  1459. ram_code = (void *)header + header->ram_code_start;
  1460. clk_enable(sdma->clk_ipg);
  1461. clk_enable(sdma->clk_ahb);
  1462. /* download the RAM image for SDMA */
  1463. sdma_load_script(sdma, ram_code,
  1464. header->ram_code_size,
  1465. addr->ram_code_start_addr);
  1466. clk_disable(sdma->clk_ipg);
  1467. clk_disable(sdma->clk_ahb);
  1468. sdma_add_scripts(sdma, addr);
  1469. dev_info(sdma->dev, "loaded firmware %d.%d\n",
  1470. header->version_major,
  1471. header->version_minor);
  1472. err_firmware:
  1473. release_firmware(fw);
  1474. }
  1475. #define EVENT_REMAP_CELLS 3
  1476. static int sdma_event_remap(struct sdma_engine *sdma)
  1477. {
  1478. struct device_node *np = sdma->dev->of_node;
  1479. struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
  1480. struct property *event_remap;
  1481. struct regmap *gpr;
  1482. char propname[] = "fsl,sdma-event-remap";
  1483. u32 reg, val, shift, num_map, i;
  1484. int ret = 0;
  1485. if (IS_ERR(np) || IS_ERR(gpr_np))
  1486. goto out;
  1487. event_remap = of_find_property(np, propname, NULL);
  1488. num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
  1489. if (!num_map) {
  1490. dev_dbg(sdma->dev, "no event needs to be remapped\n");
  1491. goto out;
  1492. } else if (num_map % EVENT_REMAP_CELLS) {
  1493. dev_err(sdma->dev, "the property %s must modulo %d\n",
  1494. propname, EVENT_REMAP_CELLS);
  1495. ret = -EINVAL;
  1496. goto out;
  1497. }
  1498. gpr = syscon_node_to_regmap(gpr_np);
  1499. if (IS_ERR(gpr)) {
  1500. dev_err(sdma->dev, "failed to get gpr regmap\n");
  1501. ret = PTR_ERR(gpr);
  1502. goto out;
  1503. }
  1504. for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
  1505. ret = of_property_read_u32_index(np, propname, i, &reg);
  1506. if (ret) {
  1507. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1508. propname, i);
  1509. goto out;
  1510. }
  1511. ret = of_property_read_u32_index(np, propname, i + 1, &shift);
  1512. if (ret) {
  1513. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1514. propname, i + 1);
  1515. goto out;
  1516. }
  1517. ret = of_property_read_u32_index(np, propname, i + 2, &val);
  1518. if (ret) {
  1519. dev_err(sdma->dev, "failed to read property %s index %d\n",
  1520. propname, i + 2);
  1521. goto out;
  1522. }
  1523. regmap_update_bits(gpr, reg, BIT(shift), val << shift);
  1524. }
  1525. out:
  1526. if (!IS_ERR(gpr_np))
  1527. of_node_put(gpr_np);
  1528. return ret;
  1529. }
  1530. static int sdma_get_firmware(struct sdma_engine *sdma,
  1531. const char *fw_name)
  1532. {
  1533. int ret;
  1534. ret = request_firmware_nowait(THIS_MODULE,
  1535. FW_ACTION_HOTPLUG, fw_name, sdma->dev,
  1536. GFP_KERNEL, sdma, sdma_load_firmware);
  1537. return ret;
  1538. }
  1539. static int sdma_init(struct sdma_engine *sdma)
  1540. {
  1541. int i, ret;
  1542. dma_addr_t ccb_phys;
  1543. ret = clk_enable(sdma->clk_ipg);
  1544. if (ret)
  1545. return ret;
  1546. ret = clk_enable(sdma->clk_ahb);
  1547. if (ret)
  1548. goto disable_clk_ipg;
  1549. /* Be sure SDMA has not started yet */
  1550. writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
  1551. sdma->channel_control = dma_alloc_coherent(NULL,
  1552. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
  1553. sizeof(struct sdma_context_data),
  1554. &ccb_phys, GFP_KERNEL);
  1555. if (!sdma->channel_control) {
  1556. ret = -ENOMEM;
  1557. goto err_dma_alloc;
  1558. }
  1559. sdma->context = (void *)sdma->channel_control +
  1560. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1561. sdma->context_phys = ccb_phys +
  1562. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1563. /* Zero-out the CCB structures array just allocated */
  1564. memset(sdma->channel_control, 0,
  1565. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
  1566. /* disable all channels */
  1567. for (i = 0; i < sdma->drvdata->num_events; i++)
  1568. writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
  1569. /* All channels have priority 0 */
  1570. for (i = 0; i < MAX_DMA_CHANNELS; i++)
  1571. writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
  1572. ret = sdma_request_channel0(sdma);
  1573. if (ret)
  1574. goto err_dma_alloc;
  1575. sdma_config_ownership(&sdma->channel[0], false, true, false);
  1576. /* Set Command Channel (Channel Zero) */
  1577. writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
  1578. /* Set bits of CONFIG register but with static context switching */
  1579. /* FIXME: Check whether to set ACR bit depending on clock ratios */
  1580. writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
  1581. writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
  1582. /* Initializes channel's priorities */
  1583. sdma_set_channel_priority(&sdma->channel[0], 7);
  1584. clk_disable(sdma->clk_ipg);
  1585. clk_disable(sdma->clk_ahb);
  1586. return 0;
  1587. err_dma_alloc:
  1588. clk_disable(sdma->clk_ahb);
  1589. disable_clk_ipg:
  1590. clk_disable(sdma->clk_ipg);
  1591. dev_err(sdma->dev, "initialisation failed with %d\n", ret);
  1592. return ret;
  1593. }
  1594. static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
  1595. {
  1596. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1597. struct imx_dma_data *data = fn_param;
  1598. if (!imx_dma_is_general_purpose(chan))
  1599. return false;
  1600. sdmac->data = *data;
  1601. chan->private = &sdmac->data;
  1602. return true;
  1603. }
  1604. static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
  1605. struct of_dma *ofdma)
  1606. {
  1607. struct sdma_engine *sdma = ofdma->of_dma_data;
  1608. dma_cap_mask_t mask = sdma->dma_device.cap_mask;
  1609. struct imx_dma_data data;
  1610. if (dma_spec->args_count != 3)
  1611. return NULL;
  1612. data.dma_request = dma_spec->args[0];
  1613. data.peripheral_type = dma_spec->args[1];
  1614. data.priority = dma_spec->args[2];
  1615. /*
  1616. * init dma_request2 to zero, which is not used by the dts.
  1617. * For P2P, dma_request2 is init from dma_request_channel(),
  1618. * chan->private will point to the imx_dma_data, and in
  1619. * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
  1620. * be set to sdmac->event_id1.
  1621. */
  1622. data.dma_request2 = 0;
  1623. return dma_request_channel(mask, sdma_filter_fn, &data);
  1624. }
  1625. static int sdma_probe(struct platform_device *pdev)
  1626. {
  1627. const struct of_device_id *of_id =
  1628. of_match_device(sdma_dt_ids, &pdev->dev);
  1629. struct device_node *np = pdev->dev.of_node;
  1630. struct device_node *spba_bus;
  1631. const char *fw_name;
  1632. int ret;
  1633. int irq;
  1634. struct resource *iores;
  1635. struct resource spba_res;
  1636. struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1637. int i;
  1638. struct sdma_engine *sdma;
  1639. s32 *saddr_arr;
  1640. const struct sdma_driver_data *drvdata = NULL;
  1641. if (of_id)
  1642. drvdata = of_id->data;
  1643. else if (pdev->id_entry)
  1644. drvdata = (void *)pdev->id_entry->driver_data;
  1645. if (!drvdata) {
  1646. dev_err(&pdev->dev, "unable to find driver data\n");
  1647. return -EINVAL;
  1648. }
  1649. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1650. if (ret)
  1651. return ret;
  1652. sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
  1653. if (!sdma)
  1654. return -ENOMEM;
  1655. spin_lock_init(&sdma->channel_0_lock);
  1656. sdma->dev = &pdev->dev;
  1657. sdma->drvdata = drvdata;
  1658. irq = platform_get_irq(pdev, 0);
  1659. if (irq < 0)
  1660. return irq;
  1661. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1662. sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
  1663. if (IS_ERR(sdma->regs))
  1664. return PTR_ERR(sdma->regs);
  1665. sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1666. if (IS_ERR(sdma->clk_ipg))
  1667. return PTR_ERR(sdma->clk_ipg);
  1668. sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1669. if (IS_ERR(sdma->clk_ahb))
  1670. return PTR_ERR(sdma->clk_ahb);
  1671. ret = clk_prepare(sdma->clk_ipg);
  1672. if (ret)
  1673. return ret;
  1674. ret = clk_prepare(sdma->clk_ahb);
  1675. if (ret)
  1676. goto err_clk;
  1677. ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
  1678. sdma);
  1679. if (ret)
  1680. goto err_irq;
  1681. sdma->irq = irq;
  1682. sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
  1683. if (!sdma->script_addrs) {
  1684. ret = -ENOMEM;
  1685. goto err_irq;
  1686. }
  1687. /* initially no scripts available */
  1688. saddr_arr = (s32 *)sdma->script_addrs;
  1689. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  1690. saddr_arr[i] = -EINVAL;
  1691. dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
  1692. dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
  1693. dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
  1694. INIT_LIST_HEAD(&sdma->dma_device.channels);
  1695. /* Initialize channel parameters */
  1696. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1697. struct sdma_channel *sdmac = &sdma->channel[i];
  1698. sdmac->sdma = sdma;
  1699. sdmac->channel = i;
  1700. sdmac->vc.desc_free = sdma_desc_free;
  1701. /*
  1702. * Add the channel to the DMAC list. Do not add channel 0 though
  1703. * because we need it internally in the SDMA driver. This also means
  1704. * that channel 0 in dmaengine counting matches sdma channel 1.
  1705. */
  1706. if (i)
  1707. vchan_init(&sdmac->vc, &sdma->dma_device);
  1708. }
  1709. ret = sdma_init(sdma);
  1710. if (ret)
  1711. goto err_init;
  1712. ret = sdma_event_remap(sdma);
  1713. if (ret)
  1714. goto err_init;
  1715. if (sdma->drvdata->script_addrs)
  1716. sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
  1717. if (pdata && pdata->script_addrs)
  1718. sdma_add_scripts(sdma, pdata->script_addrs);
  1719. if (pdata) {
  1720. ret = sdma_get_firmware(sdma, pdata->fw_name);
  1721. if (ret)
  1722. dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
  1723. } else {
  1724. /*
  1725. * Because that device tree does not encode ROM script address,
  1726. * the RAM script in firmware is mandatory for device tree
  1727. * probe, otherwise it fails.
  1728. */
  1729. ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
  1730. &fw_name);
  1731. if (ret)
  1732. dev_warn(&pdev->dev, "failed to get firmware name\n");
  1733. else {
  1734. ret = sdma_get_firmware(sdma, fw_name);
  1735. if (ret)
  1736. dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
  1737. }
  1738. }
  1739. sdma->dma_device.dev = &pdev->dev;
  1740. sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
  1741. sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
  1742. sdma->dma_device.device_tx_status = sdma_tx_status;
  1743. sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
  1744. sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
  1745. sdma->dma_device.device_config = sdma_config;
  1746. sdma->dma_device.device_terminate_all = sdma_disable_channel_with_delay;
  1747. sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
  1748. sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
  1749. sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
  1750. sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  1751. sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
  1752. sdma->dma_device.device_issue_pending = sdma_issue_pending;
  1753. sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
  1754. dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
  1755. platform_set_drvdata(pdev, sdma);
  1756. ret = dma_async_device_register(&sdma->dma_device);
  1757. if (ret) {
  1758. dev_err(&pdev->dev, "unable to register\n");
  1759. goto err_init;
  1760. }
  1761. if (np) {
  1762. ret = of_dma_controller_register(np, sdma_xlate, sdma);
  1763. if (ret) {
  1764. dev_err(&pdev->dev, "failed to register controller\n");
  1765. goto err_register;
  1766. }
  1767. spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
  1768. ret = of_address_to_resource(spba_bus, 0, &spba_res);
  1769. if (!ret) {
  1770. sdma->spba_start_addr = spba_res.start;
  1771. sdma->spba_end_addr = spba_res.end;
  1772. }
  1773. of_node_put(spba_bus);
  1774. }
  1775. return 0;
  1776. err_register:
  1777. dma_async_device_unregister(&sdma->dma_device);
  1778. err_init:
  1779. kfree(sdma->script_addrs);
  1780. err_irq:
  1781. clk_unprepare(sdma->clk_ahb);
  1782. err_clk:
  1783. clk_unprepare(sdma->clk_ipg);
  1784. return ret;
  1785. }
  1786. static int sdma_remove(struct platform_device *pdev)
  1787. {
  1788. struct sdma_engine *sdma = platform_get_drvdata(pdev);
  1789. int i;
  1790. devm_free_irq(&pdev->dev, sdma->irq, sdma);
  1791. dma_async_device_unregister(&sdma->dma_device);
  1792. kfree(sdma->script_addrs);
  1793. clk_unprepare(sdma->clk_ahb);
  1794. clk_unprepare(sdma->clk_ipg);
  1795. /* Kill the tasklet */
  1796. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1797. struct sdma_channel *sdmac = &sdma->channel[i];
  1798. tasklet_kill(&sdmac->vc.task);
  1799. sdma_free_chan_resources(&sdmac->vc.chan);
  1800. }
  1801. platform_set_drvdata(pdev, NULL);
  1802. return 0;
  1803. }
  1804. static struct platform_driver sdma_driver = {
  1805. .driver = {
  1806. .name = "imx-sdma",
  1807. .of_match_table = sdma_dt_ids,
  1808. },
  1809. .id_table = sdma_devtypes,
  1810. .remove = sdma_remove,
  1811. .probe = sdma_probe,
  1812. };
  1813. module_platform_driver(sdma_driver);
  1814. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  1815. MODULE_DESCRIPTION("i.MX SDMA driver");
  1816. #if IS_ENABLED(CONFIG_SOC_IMX6Q)
  1817. MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
  1818. #endif
  1819. #if IS_ENABLED(CONFIG_SOC_IMX7D)
  1820. MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
  1821. #endif
  1822. MODULE_LICENSE("GPL");