imx-sdma.c 50 KB

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