imx-sdma.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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/module.h>
  21. #include <linux/types.h>
  22. #include <linux/bitops.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/clk.h>
  26. #include <linux/delay.h>
  27. #include <linux/sched.h>
  28. #include <linux/semaphore.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/device.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/firmware.h>
  33. #include <linux/slab.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/dmaengine.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/of_dma.h>
  39. #include <asm/irq.h>
  40. #include <linux/platform_data/dma-imx-sdma.h>
  41. #include <linux/platform_data/dma-imx.h>
  42. #include "dmaengine.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. * Mode/Count of data node descriptors - IPCv2
  118. */
  119. struct sdma_mode_count {
  120. u32 count : 16; /* size of the buffer pointed by this BD */
  121. u32 status : 8; /* E,R,I,C,W,D status bits stored here */
  122. u32 command : 8; /* command mostlky used for channel 0 */
  123. };
  124. /*
  125. * Buffer descriptor
  126. */
  127. struct sdma_buffer_descriptor {
  128. struct sdma_mode_count mode;
  129. u32 buffer_addr; /* address of the buffer described */
  130. u32 ext_buffer_addr; /* extended buffer address */
  131. } __attribute__ ((packed));
  132. /**
  133. * struct sdma_channel_control - Channel control Block
  134. *
  135. * @current_bd_ptr current buffer descriptor processed
  136. * @base_bd_ptr first element of buffer descriptor array
  137. * @unused padding. The SDMA engine expects an array of 128 byte
  138. * control blocks
  139. */
  140. struct sdma_channel_control {
  141. u32 current_bd_ptr;
  142. u32 base_bd_ptr;
  143. u32 unused[2];
  144. } __attribute__ ((packed));
  145. /**
  146. * struct sdma_state_registers - SDMA context for a channel
  147. *
  148. * @pc: program counter
  149. * @t: test bit: status of arithmetic & test instruction
  150. * @rpc: return program counter
  151. * @sf: source fault while loading data
  152. * @spc: loop start program counter
  153. * @df: destination fault while storing data
  154. * @epc: loop end program counter
  155. * @lm: loop mode
  156. */
  157. struct sdma_state_registers {
  158. u32 pc :14;
  159. u32 unused1: 1;
  160. u32 t : 1;
  161. u32 rpc :14;
  162. u32 unused0: 1;
  163. u32 sf : 1;
  164. u32 spc :14;
  165. u32 unused2: 1;
  166. u32 df : 1;
  167. u32 epc :14;
  168. u32 lm : 2;
  169. } __attribute__ ((packed));
  170. /**
  171. * struct sdma_context_data - sdma context specific to a channel
  172. *
  173. * @channel_state: channel state bits
  174. * @gReg: general registers
  175. * @mda: burst dma destination address register
  176. * @msa: burst dma source address register
  177. * @ms: burst dma status register
  178. * @md: burst dma data register
  179. * @pda: peripheral dma destination address register
  180. * @psa: peripheral dma source address register
  181. * @ps: peripheral dma status register
  182. * @pd: peripheral dma data register
  183. * @ca: CRC polynomial register
  184. * @cs: CRC accumulator register
  185. * @dda: dedicated core destination address register
  186. * @dsa: dedicated core source address register
  187. * @ds: dedicated core status register
  188. * @dd: dedicated core data register
  189. */
  190. struct sdma_context_data {
  191. struct sdma_state_registers channel_state;
  192. u32 gReg[8];
  193. u32 mda;
  194. u32 msa;
  195. u32 ms;
  196. u32 md;
  197. u32 pda;
  198. u32 psa;
  199. u32 ps;
  200. u32 pd;
  201. u32 ca;
  202. u32 cs;
  203. u32 dda;
  204. u32 dsa;
  205. u32 ds;
  206. u32 dd;
  207. u32 scratch0;
  208. u32 scratch1;
  209. u32 scratch2;
  210. u32 scratch3;
  211. u32 scratch4;
  212. u32 scratch5;
  213. u32 scratch6;
  214. u32 scratch7;
  215. } __attribute__ ((packed));
  216. #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
  217. struct sdma_engine;
  218. /**
  219. * struct sdma_channel - housekeeping for a SDMA channel
  220. *
  221. * @sdma pointer to the SDMA engine for this channel
  222. * @channel the channel number, matches dmaengine chan_id + 1
  223. * @direction transfer type. Needed for setting SDMA script
  224. * @peripheral_type Peripheral type. Needed for setting SDMA script
  225. * @event_id0 aka dma request line
  226. * @event_id1 for channels that use 2 events
  227. * @word_size peripheral access size
  228. * @buf_tail ID of the buffer that was processed
  229. * @num_bd max NUM_BD. number of descriptors currently handling
  230. */
  231. struct sdma_channel {
  232. struct sdma_engine *sdma;
  233. unsigned int channel;
  234. enum dma_transfer_direction direction;
  235. enum sdma_peripheral_type peripheral_type;
  236. unsigned int event_id0;
  237. unsigned int event_id1;
  238. enum dma_slave_buswidth word_size;
  239. unsigned int buf_tail;
  240. unsigned int num_bd;
  241. unsigned int period_len;
  242. struct sdma_buffer_descriptor *bd;
  243. dma_addr_t bd_phys;
  244. unsigned int pc_from_device, pc_to_device;
  245. unsigned long flags;
  246. dma_addr_t per_address;
  247. unsigned long event_mask[2];
  248. unsigned long watermark_level;
  249. u32 shp_addr, per_addr;
  250. struct dma_chan chan;
  251. spinlock_t lock;
  252. struct dma_async_tx_descriptor desc;
  253. enum dma_status status;
  254. unsigned int chn_count;
  255. unsigned int chn_real_count;
  256. struct tasklet_struct tasklet;
  257. struct imx_dma_data data;
  258. };
  259. #define IMX_DMA_SG_LOOP BIT(0)
  260. #define MAX_DMA_CHANNELS 32
  261. #define MXC_SDMA_DEFAULT_PRIORITY 1
  262. #define MXC_SDMA_MIN_PRIORITY 1
  263. #define MXC_SDMA_MAX_PRIORITY 7
  264. #define SDMA_FIRMWARE_MAGIC 0x414d4453
  265. /**
  266. * struct sdma_firmware_header - Layout of the firmware image
  267. *
  268. * @magic "SDMA"
  269. * @version_major increased whenever layout of struct sdma_script_start_addrs
  270. * changes.
  271. * @version_minor firmware minor version (for binary compatible changes)
  272. * @script_addrs_start offset of struct sdma_script_start_addrs in this image
  273. * @num_script_addrs Number of script addresses in this image
  274. * @ram_code_start offset of SDMA ram image in this firmware image
  275. * @ram_code_size size of SDMA ram image
  276. * @script_addrs Stores the start address of the SDMA scripts
  277. * (in SDMA memory space)
  278. */
  279. struct sdma_firmware_header {
  280. u32 magic;
  281. u32 version_major;
  282. u32 version_minor;
  283. u32 script_addrs_start;
  284. u32 num_script_addrs;
  285. u32 ram_code_start;
  286. u32 ram_code_size;
  287. };
  288. struct sdma_driver_data {
  289. int chnenbl0;
  290. int num_events;
  291. struct sdma_script_start_addrs *script_addrs;
  292. };
  293. struct sdma_engine {
  294. struct device *dev;
  295. struct device_dma_parameters dma_parms;
  296. struct sdma_channel channel[MAX_DMA_CHANNELS];
  297. struct sdma_channel_control *channel_control;
  298. void __iomem *regs;
  299. struct sdma_context_data *context;
  300. dma_addr_t context_phys;
  301. struct dma_device dma_device;
  302. struct clk *clk_ipg;
  303. struct clk *clk_ahb;
  304. spinlock_t channel_0_lock;
  305. u32 script_number;
  306. struct sdma_script_start_addrs *script_addrs;
  307. const struct sdma_driver_data *drvdata;
  308. };
  309. static struct sdma_driver_data sdma_imx31 = {
  310. .chnenbl0 = SDMA_CHNENBL0_IMX31,
  311. .num_events = 32,
  312. };
  313. static struct sdma_script_start_addrs sdma_script_imx25 = {
  314. .ap_2_ap_addr = 729,
  315. .uart_2_mcu_addr = 904,
  316. .per_2_app_addr = 1255,
  317. .mcu_2_app_addr = 834,
  318. .uartsh_2_mcu_addr = 1120,
  319. .per_2_shp_addr = 1329,
  320. .mcu_2_shp_addr = 1048,
  321. .ata_2_mcu_addr = 1560,
  322. .mcu_2_ata_addr = 1479,
  323. .app_2_per_addr = 1189,
  324. .app_2_mcu_addr = 770,
  325. .shp_2_per_addr = 1407,
  326. .shp_2_mcu_addr = 979,
  327. };
  328. static struct sdma_driver_data sdma_imx25 = {
  329. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  330. .num_events = 48,
  331. .script_addrs = &sdma_script_imx25,
  332. };
  333. static struct sdma_driver_data sdma_imx35 = {
  334. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  335. .num_events = 48,
  336. };
  337. static struct sdma_script_start_addrs sdma_script_imx51 = {
  338. .ap_2_ap_addr = 642,
  339. .uart_2_mcu_addr = 817,
  340. .mcu_2_app_addr = 747,
  341. .mcu_2_shp_addr = 961,
  342. .ata_2_mcu_addr = 1473,
  343. .mcu_2_ata_addr = 1392,
  344. .app_2_per_addr = 1033,
  345. .app_2_mcu_addr = 683,
  346. .shp_2_per_addr = 1251,
  347. .shp_2_mcu_addr = 892,
  348. };
  349. static struct sdma_driver_data sdma_imx51 = {
  350. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  351. .num_events = 48,
  352. .script_addrs = &sdma_script_imx51,
  353. };
  354. static struct sdma_script_start_addrs sdma_script_imx53 = {
  355. .ap_2_ap_addr = 642,
  356. .app_2_mcu_addr = 683,
  357. .mcu_2_app_addr = 747,
  358. .uart_2_mcu_addr = 817,
  359. .shp_2_mcu_addr = 891,
  360. .mcu_2_shp_addr = 960,
  361. .uartsh_2_mcu_addr = 1032,
  362. .spdif_2_mcu_addr = 1100,
  363. .mcu_2_spdif_addr = 1134,
  364. .firi_2_mcu_addr = 1193,
  365. .mcu_2_firi_addr = 1290,
  366. };
  367. static struct sdma_driver_data sdma_imx53 = {
  368. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  369. .num_events = 48,
  370. .script_addrs = &sdma_script_imx53,
  371. };
  372. static struct sdma_script_start_addrs sdma_script_imx6q = {
  373. .ap_2_ap_addr = 642,
  374. .uart_2_mcu_addr = 817,
  375. .mcu_2_app_addr = 747,
  376. .per_2_per_addr = 6331,
  377. .uartsh_2_mcu_addr = 1032,
  378. .mcu_2_shp_addr = 960,
  379. .app_2_mcu_addr = 683,
  380. .shp_2_mcu_addr = 891,
  381. .spdif_2_mcu_addr = 1100,
  382. .mcu_2_spdif_addr = 1134,
  383. };
  384. static struct sdma_driver_data sdma_imx6q = {
  385. .chnenbl0 = SDMA_CHNENBL0_IMX35,
  386. .num_events = 48,
  387. .script_addrs = &sdma_script_imx6q,
  388. };
  389. static struct platform_device_id sdma_devtypes[] = {
  390. {
  391. .name = "imx25-sdma",
  392. .driver_data = (unsigned long)&sdma_imx25,
  393. }, {
  394. .name = "imx31-sdma",
  395. .driver_data = (unsigned long)&sdma_imx31,
  396. }, {
  397. .name = "imx35-sdma",
  398. .driver_data = (unsigned long)&sdma_imx35,
  399. }, {
  400. .name = "imx51-sdma",
  401. .driver_data = (unsigned long)&sdma_imx51,
  402. }, {
  403. .name = "imx53-sdma",
  404. .driver_data = (unsigned long)&sdma_imx53,
  405. }, {
  406. .name = "imx6q-sdma",
  407. .driver_data = (unsigned long)&sdma_imx6q,
  408. }, {
  409. /* sentinel */
  410. }
  411. };
  412. MODULE_DEVICE_TABLE(platform, sdma_devtypes);
  413. static const struct of_device_id sdma_dt_ids[] = {
  414. { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
  415. { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
  416. { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
  417. { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
  418. { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
  419. { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
  420. { /* sentinel */ }
  421. };
  422. MODULE_DEVICE_TABLE(of, sdma_dt_ids);
  423. #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
  424. #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
  425. #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
  426. #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
  427. static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
  428. {
  429. u32 chnenbl0 = sdma->drvdata->chnenbl0;
  430. return chnenbl0 + event * 4;
  431. }
  432. static int sdma_config_ownership(struct sdma_channel *sdmac,
  433. bool event_override, bool mcu_override, bool dsp_override)
  434. {
  435. struct sdma_engine *sdma = sdmac->sdma;
  436. int channel = sdmac->channel;
  437. unsigned long evt, mcu, dsp;
  438. if (event_override && mcu_override && dsp_override)
  439. return -EINVAL;
  440. evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
  441. mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
  442. dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
  443. if (dsp_override)
  444. __clear_bit(channel, &dsp);
  445. else
  446. __set_bit(channel, &dsp);
  447. if (event_override)
  448. __clear_bit(channel, &evt);
  449. else
  450. __set_bit(channel, &evt);
  451. if (mcu_override)
  452. __clear_bit(channel, &mcu);
  453. else
  454. __set_bit(channel, &mcu);
  455. writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
  456. writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
  457. writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
  458. return 0;
  459. }
  460. static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
  461. {
  462. writel(BIT(channel), sdma->regs + SDMA_H_START);
  463. }
  464. /*
  465. * sdma_run_channel0 - run a channel and wait till it's done
  466. */
  467. static int sdma_run_channel0(struct sdma_engine *sdma)
  468. {
  469. int ret;
  470. unsigned long timeout = 500;
  471. sdma_enable_channel(sdma, 0);
  472. while (!(ret = readl_relaxed(sdma->regs + SDMA_H_INTR) & 1)) {
  473. if (timeout-- <= 0)
  474. break;
  475. udelay(1);
  476. }
  477. if (ret) {
  478. /* Clear the interrupt status */
  479. writel_relaxed(ret, sdma->regs + SDMA_H_INTR);
  480. } else {
  481. dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
  482. }
  483. /* Set bits of CONFIG register with dynamic context switching */
  484. if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
  485. writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
  486. return ret ? 0 : -ETIMEDOUT;
  487. }
  488. static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
  489. u32 address)
  490. {
  491. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  492. void *buf_virt;
  493. dma_addr_t buf_phys;
  494. int ret;
  495. unsigned long flags;
  496. buf_virt = dma_alloc_coherent(NULL,
  497. size,
  498. &buf_phys, GFP_KERNEL);
  499. if (!buf_virt) {
  500. return -ENOMEM;
  501. }
  502. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  503. bd0->mode.command = C0_SETPM;
  504. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  505. bd0->mode.count = size / 2;
  506. bd0->buffer_addr = buf_phys;
  507. bd0->ext_buffer_addr = address;
  508. memcpy(buf_virt, buf, size);
  509. ret = sdma_run_channel0(sdma);
  510. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  511. dma_free_coherent(NULL, size, buf_virt, buf_phys);
  512. return ret;
  513. }
  514. static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
  515. {
  516. struct sdma_engine *sdma = sdmac->sdma;
  517. int channel = sdmac->channel;
  518. unsigned long val;
  519. u32 chnenbl = chnenbl_ofs(sdma, event);
  520. val = readl_relaxed(sdma->regs + chnenbl);
  521. __set_bit(channel, &val);
  522. writel_relaxed(val, sdma->regs + chnenbl);
  523. }
  524. static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
  525. {
  526. struct sdma_engine *sdma = sdmac->sdma;
  527. int channel = sdmac->channel;
  528. u32 chnenbl = chnenbl_ofs(sdma, event);
  529. unsigned long val;
  530. val = readl_relaxed(sdma->regs + chnenbl);
  531. __clear_bit(channel, &val);
  532. writel_relaxed(val, sdma->regs + chnenbl);
  533. }
  534. static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
  535. {
  536. if (sdmac->desc.callback)
  537. sdmac->desc.callback(sdmac->desc.callback_param);
  538. }
  539. static void sdma_update_channel_loop(struct sdma_channel *sdmac)
  540. {
  541. struct sdma_buffer_descriptor *bd;
  542. /*
  543. * loop mode. Iterate over descriptors, re-setup them and
  544. * call callback function.
  545. */
  546. while (1) {
  547. bd = &sdmac->bd[sdmac->buf_tail];
  548. if (bd->mode.status & BD_DONE)
  549. break;
  550. if (bd->mode.status & BD_RROR)
  551. sdmac->status = DMA_ERROR;
  552. bd->mode.status |= BD_DONE;
  553. sdmac->buf_tail++;
  554. sdmac->buf_tail %= sdmac->num_bd;
  555. }
  556. }
  557. static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
  558. {
  559. struct sdma_buffer_descriptor *bd;
  560. int i, error = 0;
  561. sdmac->chn_real_count = 0;
  562. /*
  563. * non loop mode. Iterate over all descriptors, collect
  564. * errors and call callback function
  565. */
  566. for (i = 0; i < sdmac->num_bd; i++) {
  567. bd = &sdmac->bd[i];
  568. if (bd->mode.status & (BD_DONE | BD_RROR))
  569. error = -EIO;
  570. sdmac->chn_real_count += bd->mode.count;
  571. }
  572. if (error)
  573. sdmac->status = DMA_ERROR;
  574. else
  575. sdmac->status = DMA_COMPLETE;
  576. dma_cookie_complete(&sdmac->desc);
  577. if (sdmac->desc.callback)
  578. sdmac->desc.callback(sdmac->desc.callback_param);
  579. }
  580. static void sdma_tasklet(unsigned long data)
  581. {
  582. struct sdma_channel *sdmac = (struct sdma_channel *) data;
  583. if (sdmac->flags & IMX_DMA_SG_LOOP)
  584. sdma_handle_channel_loop(sdmac);
  585. else
  586. mxc_sdma_handle_channel_normal(sdmac);
  587. }
  588. static irqreturn_t sdma_int_handler(int irq, void *dev_id)
  589. {
  590. struct sdma_engine *sdma = dev_id;
  591. unsigned long stat;
  592. stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
  593. /* not interested in channel 0 interrupts */
  594. stat &= ~1;
  595. writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
  596. while (stat) {
  597. int channel = fls(stat) - 1;
  598. struct sdma_channel *sdmac = &sdma->channel[channel];
  599. if (sdmac->flags & IMX_DMA_SG_LOOP)
  600. sdma_update_channel_loop(sdmac);
  601. tasklet_schedule(&sdmac->tasklet);
  602. __clear_bit(channel, &stat);
  603. }
  604. return IRQ_HANDLED;
  605. }
  606. /*
  607. * sets the pc of SDMA script according to the peripheral type
  608. */
  609. static void sdma_get_pc(struct sdma_channel *sdmac,
  610. enum sdma_peripheral_type peripheral_type)
  611. {
  612. struct sdma_engine *sdma = sdmac->sdma;
  613. int per_2_emi = 0, emi_2_per = 0;
  614. /*
  615. * These are needed once we start to support transfers between
  616. * two peripherals or memory-to-memory transfers
  617. */
  618. int per_2_per = 0, emi_2_emi = 0;
  619. sdmac->pc_from_device = 0;
  620. sdmac->pc_to_device = 0;
  621. switch (peripheral_type) {
  622. case IMX_DMATYPE_MEMORY:
  623. emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
  624. break;
  625. case IMX_DMATYPE_DSP:
  626. emi_2_per = sdma->script_addrs->bp_2_ap_addr;
  627. per_2_emi = sdma->script_addrs->ap_2_bp_addr;
  628. break;
  629. case IMX_DMATYPE_FIRI:
  630. per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
  631. emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
  632. break;
  633. case IMX_DMATYPE_UART:
  634. per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
  635. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  636. break;
  637. case IMX_DMATYPE_UART_SP:
  638. per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
  639. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  640. break;
  641. case IMX_DMATYPE_ATA:
  642. per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
  643. emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
  644. break;
  645. case IMX_DMATYPE_CSPI:
  646. case IMX_DMATYPE_EXT:
  647. case IMX_DMATYPE_SSI:
  648. case IMX_DMATYPE_SAI:
  649. per_2_emi = sdma->script_addrs->app_2_mcu_addr;
  650. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  651. break;
  652. case IMX_DMATYPE_SSI_DUAL:
  653. per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
  654. emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
  655. break;
  656. case IMX_DMATYPE_SSI_SP:
  657. case IMX_DMATYPE_MMC:
  658. case IMX_DMATYPE_SDHC:
  659. case IMX_DMATYPE_CSPI_SP:
  660. case IMX_DMATYPE_ESAI:
  661. case IMX_DMATYPE_MSHC_SP:
  662. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  663. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  664. break;
  665. case IMX_DMATYPE_ASRC:
  666. per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
  667. emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
  668. per_2_per = sdma->script_addrs->per_2_per_addr;
  669. break;
  670. case IMX_DMATYPE_ASRC_SP:
  671. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  672. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  673. per_2_per = sdma->script_addrs->per_2_per_addr;
  674. break;
  675. case IMX_DMATYPE_MSHC:
  676. per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
  677. emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
  678. break;
  679. case IMX_DMATYPE_CCM:
  680. per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
  681. break;
  682. case IMX_DMATYPE_SPDIF:
  683. per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
  684. emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
  685. break;
  686. case IMX_DMATYPE_IPU_MEMORY:
  687. emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
  688. break;
  689. default:
  690. break;
  691. }
  692. sdmac->pc_from_device = per_2_emi;
  693. sdmac->pc_to_device = emi_2_per;
  694. }
  695. static int sdma_load_context(struct sdma_channel *sdmac)
  696. {
  697. struct sdma_engine *sdma = sdmac->sdma;
  698. int channel = sdmac->channel;
  699. int load_address;
  700. struct sdma_context_data *context = sdma->context;
  701. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  702. int ret;
  703. unsigned long flags;
  704. if (sdmac->direction == DMA_DEV_TO_MEM) {
  705. load_address = sdmac->pc_from_device;
  706. } else {
  707. load_address = sdmac->pc_to_device;
  708. }
  709. if (load_address < 0)
  710. return load_address;
  711. dev_dbg(sdma->dev, "load_address = %d\n", load_address);
  712. dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
  713. dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
  714. dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
  715. dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
  716. dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
  717. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  718. memset(context, 0, sizeof(*context));
  719. context->channel_state.pc = load_address;
  720. /* Send by context the event mask,base address for peripheral
  721. * and watermark level
  722. */
  723. context->gReg[0] = sdmac->event_mask[1];
  724. context->gReg[1] = sdmac->event_mask[0];
  725. context->gReg[2] = sdmac->per_addr;
  726. context->gReg[6] = sdmac->shp_addr;
  727. context->gReg[7] = sdmac->watermark_level;
  728. bd0->mode.command = C0_SETDM;
  729. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  730. bd0->mode.count = sizeof(*context) / 4;
  731. bd0->buffer_addr = sdma->context_phys;
  732. bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
  733. ret = sdma_run_channel0(sdma);
  734. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  735. return ret;
  736. }
  737. static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
  738. {
  739. return container_of(chan, struct sdma_channel, chan);
  740. }
  741. static int sdma_disable_channel(struct dma_chan *chan)
  742. {
  743. struct sdma_channel *sdmac = to_sdma_chan(chan);
  744. struct sdma_engine *sdma = sdmac->sdma;
  745. int channel = sdmac->channel;
  746. writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
  747. sdmac->status = DMA_ERROR;
  748. return 0;
  749. }
  750. static int sdma_config_channel(struct dma_chan *chan)
  751. {
  752. struct sdma_channel *sdmac = to_sdma_chan(chan);
  753. int ret;
  754. sdma_disable_channel(chan);
  755. sdmac->event_mask[0] = 0;
  756. sdmac->event_mask[1] = 0;
  757. sdmac->shp_addr = 0;
  758. sdmac->per_addr = 0;
  759. if (sdmac->event_id0) {
  760. if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
  761. return -EINVAL;
  762. sdma_event_enable(sdmac, sdmac->event_id0);
  763. }
  764. switch (sdmac->peripheral_type) {
  765. case IMX_DMATYPE_DSP:
  766. sdma_config_ownership(sdmac, false, true, true);
  767. break;
  768. case IMX_DMATYPE_MEMORY:
  769. sdma_config_ownership(sdmac, false, true, false);
  770. break;
  771. default:
  772. sdma_config_ownership(sdmac, true, true, false);
  773. break;
  774. }
  775. sdma_get_pc(sdmac, sdmac->peripheral_type);
  776. if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
  777. (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
  778. /* Handle multiple event channels differently */
  779. if (sdmac->event_id1) {
  780. sdmac->event_mask[1] = BIT(sdmac->event_id1 % 32);
  781. if (sdmac->event_id1 > 31)
  782. __set_bit(31, &sdmac->watermark_level);
  783. sdmac->event_mask[0] = BIT(sdmac->event_id0 % 32);
  784. if (sdmac->event_id0 > 31)
  785. __set_bit(30, &sdmac->watermark_level);
  786. } else {
  787. __set_bit(sdmac->event_id0, sdmac->event_mask);
  788. }
  789. /* Watermark Level */
  790. sdmac->watermark_level |= sdmac->watermark_level;
  791. /* Address */
  792. sdmac->shp_addr = sdmac->per_address;
  793. } else {
  794. sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
  795. }
  796. ret = sdma_load_context(sdmac);
  797. return ret;
  798. }
  799. static int sdma_set_channel_priority(struct sdma_channel *sdmac,
  800. unsigned int priority)
  801. {
  802. struct sdma_engine *sdma = sdmac->sdma;
  803. int channel = sdmac->channel;
  804. if (priority < MXC_SDMA_MIN_PRIORITY
  805. || priority > MXC_SDMA_MAX_PRIORITY) {
  806. return -EINVAL;
  807. }
  808. writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
  809. return 0;
  810. }
  811. static int sdma_request_channel(struct sdma_channel *sdmac)
  812. {
  813. struct sdma_engine *sdma = sdmac->sdma;
  814. int channel = sdmac->channel;
  815. int ret = -EBUSY;
  816. sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys,
  817. GFP_KERNEL);
  818. if (!sdmac->bd) {
  819. ret = -ENOMEM;
  820. goto out;
  821. }
  822. sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
  823. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  824. sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
  825. return 0;
  826. out:
  827. return ret;
  828. }
  829. static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
  830. {
  831. unsigned long flags;
  832. struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
  833. dma_cookie_t cookie;
  834. spin_lock_irqsave(&sdmac->lock, flags);
  835. cookie = dma_cookie_assign(tx);
  836. spin_unlock_irqrestore(&sdmac->lock, flags);
  837. return cookie;
  838. }
  839. static int sdma_alloc_chan_resources(struct dma_chan *chan)
  840. {
  841. struct sdma_channel *sdmac = to_sdma_chan(chan);
  842. struct imx_dma_data *data = chan->private;
  843. int prio, ret;
  844. if (!data)
  845. return -EINVAL;
  846. switch (data->priority) {
  847. case DMA_PRIO_HIGH:
  848. prio = 3;
  849. break;
  850. case DMA_PRIO_MEDIUM:
  851. prio = 2;
  852. break;
  853. case DMA_PRIO_LOW:
  854. default:
  855. prio = 1;
  856. break;
  857. }
  858. sdmac->peripheral_type = data->peripheral_type;
  859. sdmac->event_id0 = data->dma_request;
  860. clk_enable(sdmac->sdma->clk_ipg);
  861. clk_enable(sdmac->sdma->clk_ahb);
  862. ret = sdma_request_channel(sdmac);
  863. if (ret)
  864. return ret;
  865. ret = sdma_set_channel_priority(sdmac, prio);
  866. if (ret)
  867. return ret;
  868. dma_async_tx_descriptor_init(&sdmac->desc, chan);
  869. sdmac->desc.tx_submit = sdma_tx_submit;
  870. /* txd.flags will be overwritten in prep funcs */
  871. sdmac->desc.flags = DMA_CTRL_ACK;
  872. return 0;
  873. }
  874. static void sdma_free_chan_resources(struct dma_chan *chan)
  875. {
  876. struct sdma_channel *sdmac = to_sdma_chan(chan);
  877. struct sdma_engine *sdma = sdmac->sdma;
  878. sdma_disable_channel(chan);
  879. if (sdmac->event_id0)
  880. sdma_event_disable(sdmac, sdmac->event_id0);
  881. if (sdmac->event_id1)
  882. sdma_event_disable(sdmac, sdmac->event_id1);
  883. sdmac->event_id0 = 0;
  884. sdmac->event_id1 = 0;
  885. sdma_set_channel_priority(sdmac, 0);
  886. dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
  887. clk_disable(sdma->clk_ipg);
  888. clk_disable(sdma->clk_ahb);
  889. }
  890. static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
  891. struct dma_chan *chan, struct scatterlist *sgl,
  892. unsigned int sg_len, enum dma_transfer_direction direction,
  893. unsigned long flags, void *context)
  894. {
  895. struct sdma_channel *sdmac = to_sdma_chan(chan);
  896. struct sdma_engine *sdma = sdmac->sdma;
  897. int ret, i, count;
  898. int channel = sdmac->channel;
  899. struct scatterlist *sg;
  900. if (sdmac->status == DMA_IN_PROGRESS)
  901. return NULL;
  902. sdmac->status = DMA_IN_PROGRESS;
  903. sdmac->flags = 0;
  904. sdmac->buf_tail = 0;
  905. dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
  906. sg_len, channel);
  907. sdmac->direction = direction;
  908. ret = sdma_load_context(sdmac);
  909. if (ret)
  910. goto err_out;
  911. if (sg_len > NUM_BD) {
  912. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  913. channel, sg_len, NUM_BD);
  914. ret = -EINVAL;
  915. goto err_out;
  916. }
  917. sdmac->chn_count = 0;
  918. for_each_sg(sgl, sg, sg_len, i) {
  919. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  920. int param;
  921. bd->buffer_addr = sg->dma_address;
  922. count = sg_dma_len(sg);
  923. if (count > 0xffff) {
  924. dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
  925. channel, count, 0xffff);
  926. ret = -EINVAL;
  927. goto err_out;
  928. }
  929. bd->mode.count = count;
  930. sdmac->chn_count += count;
  931. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
  932. ret = -EINVAL;
  933. goto err_out;
  934. }
  935. switch (sdmac->word_size) {
  936. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  937. bd->mode.command = 0;
  938. if (count & 3 || sg->dma_address & 3)
  939. return NULL;
  940. break;
  941. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  942. bd->mode.command = 2;
  943. if (count & 1 || sg->dma_address & 1)
  944. return NULL;
  945. break;
  946. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  947. bd->mode.command = 1;
  948. break;
  949. default:
  950. return NULL;
  951. }
  952. param = BD_DONE | BD_EXTD | BD_CONT;
  953. if (i + 1 == sg_len) {
  954. param |= BD_INTR;
  955. param |= BD_LAST;
  956. param &= ~BD_CONT;
  957. }
  958. dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
  959. i, count, (u64)sg->dma_address,
  960. param & BD_WRAP ? "wrap" : "",
  961. param & BD_INTR ? " intr" : "");
  962. bd->mode.status = param;
  963. }
  964. sdmac->num_bd = sg_len;
  965. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  966. return &sdmac->desc;
  967. err_out:
  968. sdmac->status = DMA_ERROR;
  969. return NULL;
  970. }
  971. static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
  972. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  973. size_t period_len, enum dma_transfer_direction direction,
  974. unsigned long flags)
  975. {
  976. struct sdma_channel *sdmac = to_sdma_chan(chan);
  977. struct sdma_engine *sdma = sdmac->sdma;
  978. int num_periods = buf_len / period_len;
  979. int channel = sdmac->channel;
  980. int ret, i = 0, buf = 0;
  981. dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
  982. if (sdmac->status == DMA_IN_PROGRESS)
  983. return NULL;
  984. sdmac->status = DMA_IN_PROGRESS;
  985. sdmac->buf_tail = 0;
  986. sdmac->period_len = period_len;
  987. sdmac->flags |= IMX_DMA_SG_LOOP;
  988. sdmac->direction = direction;
  989. ret = sdma_load_context(sdmac);
  990. if (ret)
  991. goto err_out;
  992. if (num_periods > NUM_BD) {
  993. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  994. channel, num_periods, NUM_BD);
  995. goto err_out;
  996. }
  997. if (period_len > 0xffff) {
  998. dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
  999. channel, period_len, 0xffff);
  1000. goto err_out;
  1001. }
  1002. while (buf < buf_len) {
  1003. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  1004. int param;
  1005. bd->buffer_addr = dma_addr;
  1006. bd->mode.count = period_len;
  1007. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  1008. goto err_out;
  1009. if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
  1010. bd->mode.command = 0;
  1011. else
  1012. bd->mode.command = sdmac->word_size;
  1013. param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
  1014. if (i + 1 == num_periods)
  1015. param |= BD_WRAP;
  1016. dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
  1017. i, period_len, (u64)dma_addr,
  1018. param & BD_WRAP ? "wrap" : "",
  1019. param & BD_INTR ? " intr" : "");
  1020. bd->mode.status = param;
  1021. dma_addr += period_len;
  1022. buf += period_len;
  1023. i++;
  1024. }
  1025. sdmac->num_bd = num_periods;
  1026. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  1027. return &sdmac->desc;
  1028. err_out:
  1029. sdmac->status = DMA_ERROR;
  1030. return NULL;
  1031. }
  1032. static int sdma_config(struct dma_chan *chan,
  1033. struct dma_slave_config *dmaengine_cfg)
  1034. {
  1035. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1036. if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
  1037. sdmac->per_address = dmaengine_cfg->src_addr;
  1038. sdmac->watermark_level = dmaengine_cfg->src_maxburst *
  1039. dmaengine_cfg->src_addr_width;
  1040. sdmac->word_size = dmaengine_cfg->src_addr_width;
  1041. } else {
  1042. sdmac->per_address = dmaengine_cfg->dst_addr;
  1043. sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
  1044. dmaengine_cfg->dst_addr_width;
  1045. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  1046. }
  1047. sdmac->direction = dmaengine_cfg->direction;
  1048. return sdma_config_channel(chan);
  1049. }
  1050. static enum dma_status sdma_tx_status(struct dma_chan *chan,
  1051. dma_cookie_t cookie,
  1052. struct dma_tx_state *txstate)
  1053. {
  1054. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1055. u32 residue;
  1056. if (sdmac->flags & IMX_DMA_SG_LOOP)
  1057. residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len;
  1058. else
  1059. residue = sdmac->chn_count - sdmac->chn_real_count;
  1060. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  1061. residue);
  1062. return sdmac->status;
  1063. }
  1064. static void sdma_issue_pending(struct dma_chan *chan)
  1065. {
  1066. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1067. struct sdma_engine *sdma = sdmac->sdma;
  1068. if (sdmac->status == DMA_IN_PROGRESS)
  1069. sdma_enable_channel(sdma, sdmac->channel);
  1070. }
  1071. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
  1072. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
  1073. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
  1074. static void sdma_add_scripts(struct sdma_engine *sdma,
  1075. const struct sdma_script_start_addrs *addr)
  1076. {
  1077. s32 *addr_arr = (u32 *)addr;
  1078. s32 *saddr_arr = (u32 *)sdma->script_addrs;
  1079. int i;
  1080. /* use the default firmware in ROM if missing external firmware */
  1081. if (!sdma->script_number)
  1082. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1083. for (i = 0; i < sdma->script_number; i++)
  1084. if (addr_arr[i] > 0)
  1085. saddr_arr[i] = addr_arr[i];
  1086. }
  1087. static void sdma_load_firmware(const struct firmware *fw, void *context)
  1088. {
  1089. struct sdma_engine *sdma = context;
  1090. const struct sdma_firmware_header *header;
  1091. const struct sdma_script_start_addrs *addr;
  1092. unsigned short *ram_code;
  1093. if (!fw) {
  1094. dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
  1095. /* In this case we just use the ROM firmware. */
  1096. return;
  1097. }
  1098. if (fw->size < sizeof(*header))
  1099. goto err_firmware;
  1100. header = (struct sdma_firmware_header *)fw->data;
  1101. if (header->magic != SDMA_FIRMWARE_MAGIC)
  1102. goto err_firmware;
  1103. if (header->ram_code_start + header->ram_code_size > fw->size)
  1104. goto err_firmware;
  1105. switch (header->version_major) {
  1106. case 1:
  1107. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
  1108. break;
  1109. case 2:
  1110. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
  1111. break;
  1112. case 3:
  1113. sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
  1114. break;
  1115. default:
  1116. dev_err(sdma->dev, "unknown firmware version\n");
  1117. goto err_firmware;
  1118. }
  1119. addr = (void *)header + header->script_addrs_start;
  1120. ram_code = (void *)header + header->ram_code_start;
  1121. clk_enable(sdma->clk_ipg);
  1122. clk_enable(sdma->clk_ahb);
  1123. /* download the RAM image for SDMA */
  1124. sdma_load_script(sdma, ram_code,
  1125. header->ram_code_size,
  1126. addr->ram_code_start_addr);
  1127. clk_disable(sdma->clk_ipg);
  1128. clk_disable(sdma->clk_ahb);
  1129. sdma_add_scripts(sdma, addr);
  1130. dev_info(sdma->dev, "loaded firmware %d.%d\n",
  1131. header->version_major,
  1132. header->version_minor);
  1133. err_firmware:
  1134. release_firmware(fw);
  1135. }
  1136. static int sdma_get_firmware(struct sdma_engine *sdma,
  1137. const char *fw_name)
  1138. {
  1139. int ret;
  1140. ret = request_firmware_nowait(THIS_MODULE,
  1141. FW_ACTION_HOTPLUG, fw_name, sdma->dev,
  1142. GFP_KERNEL, sdma, sdma_load_firmware);
  1143. return ret;
  1144. }
  1145. static int sdma_init(struct sdma_engine *sdma)
  1146. {
  1147. int i, ret;
  1148. dma_addr_t ccb_phys;
  1149. clk_enable(sdma->clk_ipg);
  1150. clk_enable(sdma->clk_ahb);
  1151. /* Be sure SDMA has not started yet */
  1152. writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
  1153. sdma->channel_control = dma_alloc_coherent(NULL,
  1154. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
  1155. sizeof(struct sdma_context_data),
  1156. &ccb_phys, GFP_KERNEL);
  1157. if (!sdma->channel_control) {
  1158. ret = -ENOMEM;
  1159. goto err_dma_alloc;
  1160. }
  1161. sdma->context = (void *)sdma->channel_control +
  1162. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1163. sdma->context_phys = ccb_phys +
  1164. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1165. /* Zero-out the CCB structures array just allocated */
  1166. memset(sdma->channel_control, 0,
  1167. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
  1168. /* disable all channels */
  1169. for (i = 0; i < sdma->drvdata->num_events; i++)
  1170. writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
  1171. /* All channels have priority 0 */
  1172. for (i = 0; i < MAX_DMA_CHANNELS; i++)
  1173. writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
  1174. ret = sdma_request_channel(&sdma->channel[0]);
  1175. if (ret)
  1176. goto err_dma_alloc;
  1177. sdma_config_ownership(&sdma->channel[0], false, true, false);
  1178. /* Set Command Channel (Channel Zero) */
  1179. writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
  1180. /* Set bits of CONFIG register but with static context switching */
  1181. /* FIXME: Check whether to set ACR bit depending on clock ratios */
  1182. writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
  1183. writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
  1184. /* Initializes channel's priorities */
  1185. sdma_set_channel_priority(&sdma->channel[0], 7);
  1186. clk_disable(sdma->clk_ipg);
  1187. clk_disable(sdma->clk_ahb);
  1188. return 0;
  1189. err_dma_alloc:
  1190. clk_disable(sdma->clk_ipg);
  1191. clk_disable(sdma->clk_ahb);
  1192. dev_err(sdma->dev, "initialisation failed with %d\n", ret);
  1193. return ret;
  1194. }
  1195. static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
  1196. {
  1197. struct sdma_channel *sdmac = to_sdma_chan(chan);
  1198. struct imx_dma_data *data = fn_param;
  1199. if (!imx_dma_is_general_purpose(chan))
  1200. return false;
  1201. sdmac->data = *data;
  1202. chan->private = &sdmac->data;
  1203. return true;
  1204. }
  1205. static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
  1206. struct of_dma *ofdma)
  1207. {
  1208. struct sdma_engine *sdma = ofdma->of_dma_data;
  1209. dma_cap_mask_t mask = sdma->dma_device.cap_mask;
  1210. struct imx_dma_data data;
  1211. if (dma_spec->args_count != 3)
  1212. return NULL;
  1213. data.dma_request = dma_spec->args[0];
  1214. data.peripheral_type = dma_spec->args[1];
  1215. data.priority = dma_spec->args[2];
  1216. return dma_request_channel(mask, sdma_filter_fn, &data);
  1217. }
  1218. static int sdma_probe(struct platform_device *pdev)
  1219. {
  1220. const struct of_device_id *of_id =
  1221. of_match_device(sdma_dt_ids, &pdev->dev);
  1222. struct device_node *np = pdev->dev.of_node;
  1223. const char *fw_name;
  1224. int ret;
  1225. int irq;
  1226. struct resource *iores;
  1227. struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1228. int i;
  1229. struct sdma_engine *sdma;
  1230. s32 *saddr_arr;
  1231. const struct sdma_driver_data *drvdata = NULL;
  1232. if (of_id)
  1233. drvdata = of_id->data;
  1234. else if (pdev->id_entry)
  1235. drvdata = (void *)pdev->id_entry->driver_data;
  1236. if (!drvdata) {
  1237. dev_err(&pdev->dev, "unable to find driver data\n");
  1238. return -EINVAL;
  1239. }
  1240. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1241. if (ret)
  1242. return ret;
  1243. sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
  1244. if (!sdma)
  1245. return -ENOMEM;
  1246. spin_lock_init(&sdma->channel_0_lock);
  1247. sdma->dev = &pdev->dev;
  1248. sdma->drvdata = drvdata;
  1249. irq = platform_get_irq(pdev, 0);
  1250. if (irq < 0)
  1251. return irq;
  1252. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1253. sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
  1254. if (IS_ERR(sdma->regs))
  1255. return PTR_ERR(sdma->regs);
  1256. sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1257. if (IS_ERR(sdma->clk_ipg))
  1258. return PTR_ERR(sdma->clk_ipg);
  1259. sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1260. if (IS_ERR(sdma->clk_ahb))
  1261. return PTR_ERR(sdma->clk_ahb);
  1262. clk_prepare(sdma->clk_ipg);
  1263. clk_prepare(sdma->clk_ahb);
  1264. ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
  1265. sdma);
  1266. if (ret)
  1267. return ret;
  1268. sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
  1269. if (!sdma->script_addrs)
  1270. return -ENOMEM;
  1271. /* initially no scripts available */
  1272. saddr_arr = (s32 *)sdma->script_addrs;
  1273. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  1274. saddr_arr[i] = -EINVAL;
  1275. dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
  1276. dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
  1277. INIT_LIST_HEAD(&sdma->dma_device.channels);
  1278. /* Initialize channel parameters */
  1279. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1280. struct sdma_channel *sdmac = &sdma->channel[i];
  1281. sdmac->sdma = sdma;
  1282. spin_lock_init(&sdmac->lock);
  1283. sdmac->chan.device = &sdma->dma_device;
  1284. dma_cookie_init(&sdmac->chan);
  1285. sdmac->channel = i;
  1286. tasklet_init(&sdmac->tasklet, sdma_tasklet,
  1287. (unsigned long) sdmac);
  1288. /*
  1289. * Add the channel to the DMAC list. Do not add channel 0 though
  1290. * because we need it internally in the SDMA driver. This also means
  1291. * that channel 0 in dmaengine counting matches sdma channel 1.
  1292. */
  1293. if (i)
  1294. list_add_tail(&sdmac->chan.device_node,
  1295. &sdma->dma_device.channels);
  1296. }
  1297. ret = sdma_init(sdma);
  1298. if (ret)
  1299. goto err_init;
  1300. if (sdma->drvdata->script_addrs)
  1301. sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
  1302. if (pdata && pdata->script_addrs)
  1303. sdma_add_scripts(sdma, pdata->script_addrs);
  1304. if (pdata) {
  1305. ret = sdma_get_firmware(sdma, pdata->fw_name);
  1306. if (ret)
  1307. dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
  1308. } else {
  1309. /*
  1310. * Because that device tree does not encode ROM script address,
  1311. * the RAM script in firmware is mandatory for device tree
  1312. * probe, otherwise it fails.
  1313. */
  1314. ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
  1315. &fw_name);
  1316. if (ret)
  1317. dev_warn(&pdev->dev, "failed to get firmware name\n");
  1318. else {
  1319. ret = sdma_get_firmware(sdma, fw_name);
  1320. if (ret)
  1321. dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
  1322. }
  1323. }
  1324. sdma->dma_device.dev = &pdev->dev;
  1325. sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
  1326. sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
  1327. sdma->dma_device.device_tx_status = sdma_tx_status;
  1328. sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
  1329. sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
  1330. sdma->dma_device.device_config = sdma_config;
  1331. sdma->dma_device.device_terminate_all = sdma_disable_channel;
  1332. sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1333. sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  1334. sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1335. sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1336. sdma->dma_device.device_issue_pending = sdma_issue_pending;
  1337. sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
  1338. dma_set_max_seg_size(sdma->dma_device.dev, 65535);
  1339. platform_set_drvdata(pdev, sdma);
  1340. ret = dma_async_device_register(&sdma->dma_device);
  1341. if (ret) {
  1342. dev_err(&pdev->dev, "unable to register\n");
  1343. goto err_init;
  1344. }
  1345. if (np) {
  1346. ret = of_dma_controller_register(np, sdma_xlate, sdma);
  1347. if (ret) {
  1348. dev_err(&pdev->dev, "failed to register controller\n");
  1349. goto err_register;
  1350. }
  1351. }
  1352. dev_info(sdma->dev, "initialized\n");
  1353. return 0;
  1354. err_register:
  1355. dma_async_device_unregister(&sdma->dma_device);
  1356. err_init:
  1357. kfree(sdma->script_addrs);
  1358. return ret;
  1359. }
  1360. static int sdma_remove(struct platform_device *pdev)
  1361. {
  1362. struct sdma_engine *sdma = platform_get_drvdata(pdev);
  1363. int i;
  1364. dma_async_device_unregister(&sdma->dma_device);
  1365. kfree(sdma->script_addrs);
  1366. /* Kill the tasklet */
  1367. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1368. struct sdma_channel *sdmac = &sdma->channel[i];
  1369. tasklet_kill(&sdmac->tasklet);
  1370. }
  1371. platform_set_drvdata(pdev, NULL);
  1372. dev_info(&pdev->dev, "Removed...\n");
  1373. return 0;
  1374. }
  1375. static struct platform_driver sdma_driver = {
  1376. .driver = {
  1377. .name = "imx-sdma",
  1378. .of_match_table = sdma_dt_ids,
  1379. },
  1380. .id_table = sdma_devtypes,
  1381. .remove = sdma_remove,
  1382. .probe = sdma_probe,
  1383. };
  1384. module_platform_driver(sdma_driver);
  1385. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  1386. MODULE_DESCRIPTION("i.MX SDMA driver");
  1387. MODULE_LICENSE("GPL");