imx-sdma.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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/types.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/clk.h>
  24. #include <linux/wait.h>
  25. #include <linux/sched.h>
  26. #include <linux/semaphore.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/device.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/firmware.h>
  31. #include <linux/slab.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/dmaengine.h>
  34. #include <linux/of.h>
  35. #include <linux/of_device.h>
  36. #include <asm/irq.h>
  37. #include <mach/sdma.h>
  38. #include <mach/dma.h>
  39. #include <mach/hardware.h>
  40. /* SDMA registers */
  41. #define SDMA_H_C0PTR 0x000
  42. #define SDMA_H_INTR 0x004
  43. #define SDMA_H_STATSTOP 0x008
  44. #define SDMA_H_START 0x00c
  45. #define SDMA_H_EVTOVR 0x010
  46. #define SDMA_H_DSPOVR 0x014
  47. #define SDMA_H_HOSTOVR 0x018
  48. #define SDMA_H_EVTPEND 0x01c
  49. #define SDMA_H_DSPENBL 0x020
  50. #define SDMA_H_RESET 0x024
  51. #define SDMA_H_EVTERR 0x028
  52. #define SDMA_H_INTRMSK 0x02c
  53. #define SDMA_H_PSW 0x030
  54. #define SDMA_H_EVTERRDBG 0x034
  55. #define SDMA_H_CONFIG 0x038
  56. #define SDMA_ONCE_ENB 0x040
  57. #define SDMA_ONCE_DATA 0x044
  58. #define SDMA_ONCE_INSTR 0x048
  59. #define SDMA_ONCE_STAT 0x04c
  60. #define SDMA_ONCE_CMD 0x050
  61. #define SDMA_EVT_MIRROR 0x054
  62. #define SDMA_ILLINSTADDR 0x058
  63. #define SDMA_CHN0ADDR 0x05c
  64. #define SDMA_ONCE_RTB 0x060
  65. #define SDMA_XTRIG_CONF1 0x070
  66. #define SDMA_XTRIG_CONF2 0x074
  67. #define SDMA_CHNENBL0_IMX35 0x200
  68. #define SDMA_CHNENBL0_IMX31 0x080
  69. #define SDMA_CHNPRI_0 0x100
  70. /*
  71. * Buffer descriptor status values.
  72. */
  73. #define BD_DONE 0x01
  74. #define BD_WRAP 0x02
  75. #define BD_CONT 0x04
  76. #define BD_INTR 0x08
  77. #define BD_RROR 0x10
  78. #define BD_LAST 0x20
  79. #define BD_EXTD 0x80
  80. /*
  81. * Data Node descriptor status values.
  82. */
  83. #define DND_END_OF_FRAME 0x80
  84. #define DND_END_OF_XFER 0x40
  85. #define DND_DONE 0x20
  86. #define DND_UNUSED 0x01
  87. /*
  88. * IPCV2 descriptor status values.
  89. */
  90. #define BD_IPCV2_END_OF_FRAME 0x40
  91. #define IPCV2_MAX_NODES 50
  92. /*
  93. * Error bit set in the CCB status field by the SDMA,
  94. * in setbd routine, in case of a transfer error
  95. */
  96. #define DATA_ERROR 0x10000000
  97. /*
  98. * Buffer descriptor commands.
  99. */
  100. #define C0_ADDR 0x01
  101. #define C0_LOAD 0x02
  102. #define C0_DUMP 0x03
  103. #define C0_SETCTX 0x07
  104. #define C0_GETCTX 0x03
  105. #define C0_SETDM 0x01
  106. #define C0_SETPM 0x04
  107. #define C0_GETDM 0x02
  108. #define C0_GETPM 0x08
  109. /*
  110. * Change endianness indicator in the BD command field
  111. */
  112. #define CHANGE_ENDIANNESS 0x80
  113. /*
  114. * Mode/Count of data node descriptors - IPCv2
  115. */
  116. struct sdma_mode_count {
  117. u32 count : 16; /* size of the buffer pointed by this BD */
  118. u32 status : 8; /* E,R,I,C,W,D status bits stored here */
  119. u32 command : 8; /* command mostlky used for channel 0 */
  120. };
  121. /*
  122. * Buffer descriptor
  123. */
  124. struct sdma_buffer_descriptor {
  125. struct sdma_mode_count mode;
  126. u32 buffer_addr; /* address of the buffer described */
  127. u32 ext_buffer_addr; /* extended buffer address */
  128. } __attribute__ ((packed));
  129. /**
  130. * struct sdma_channel_control - Channel control Block
  131. *
  132. * @current_bd_ptr current buffer descriptor processed
  133. * @base_bd_ptr first element of buffer descriptor array
  134. * @unused padding. The SDMA engine expects an array of 128 byte
  135. * control blocks
  136. */
  137. struct sdma_channel_control {
  138. u32 current_bd_ptr;
  139. u32 base_bd_ptr;
  140. u32 unused[2];
  141. } __attribute__ ((packed));
  142. /**
  143. * struct sdma_state_registers - SDMA context for a channel
  144. *
  145. * @pc: program counter
  146. * @t: test bit: status of arithmetic & test instruction
  147. * @rpc: return program counter
  148. * @sf: source fault while loading data
  149. * @spc: loop start program counter
  150. * @df: destination fault while storing data
  151. * @epc: loop end program counter
  152. * @lm: loop mode
  153. */
  154. struct sdma_state_registers {
  155. u32 pc :14;
  156. u32 unused1: 1;
  157. u32 t : 1;
  158. u32 rpc :14;
  159. u32 unused0: 1;
  160. u32 sf : 1;
  161. u32 spc :14;
  162. u32 unused2: 1;
  163. u32 df : 1;
  164. u32 epc :14;
  165. u32 lm : 2;
  166. } __attribute__ ((packed));
  167. /**
  168. * struct sdma_context_data - sdma context specific to a channel
  169. *
  170. * @channel_state: channel state bits
  171. * @gReg: general registers
  172. * @mda: burst dma destination address register
  173. * @msa: burst dma source address register
  174. * @ms: burst dma status register
  175. * @md: burst dma data register
  176. * @pda: peripheral dma destination address register
  177. * @psa: peripheral dma source address register
  178. * @ps: peripheral dma status register
  179. * @pd: peripheral dma data register
  180. * @ca: CRC polynomial register
  181. * @cs: CRC accumulator register
  182. * @dda: dedicated core destination address register
  183. * @dsa: dedicated core source address register
  184. * @ds: dedicated core status register
  185. * @dd: dedicated core data register
  186. */
  187. struct sdma_context_data {
  188. struct sdma_state_registers channel_state;
  189. u32 gReg[8];
  190. u32 mda;
  191. u32 msa;
  192. u32 ms;
  193. u32 md;
  194. u32 pda;
  195. u32 psa;
  196. u32 ps;
  197. u32 pd;
  198. u32 ca;
  199. u32 cs;
  200. u32 dda;
  201. u32 dsa;
  202. u32 ds;
  203. u32 dd;
  204. u32 scratch0;
  205. u32 scratch1;
  206. u32 scratch2;
  207. u32 scratch3;
  208. u32 scratch4;
  209. u32 scratch5;
  210. u32 scratch6;
  211. u32 scratch7;
  212. } __attribute__ ((packed));
  213. #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
  214. struct sdma_engine;
  215. /**
  216. * struct sdma_channel - housekeeping for a SDMA channel
  217. *
  218. * @sdma pointer to the SDMA engine for this channel
  219. * @channel the channel number, matches dmaengine chan_id + 1
  220. * @direction transfer type. Needed for setting SDMA script
  221. * @peripheral_type Peripheral type. Needed for setting SDMA script
  222. * @event_id0 aka dma request line
  223. * @event_id1 for channels that use 2 events
  224. * @word_size peripheral access size
  225. * @buf_tail ID of the buffer that was processed
  226. * @done channel completion
  227. * @num_bd max NUM_BD. number of descriptors currently handling
  228. */
  229. struct sdma_channel {
  230. struct sdma_engine *sdma;
  231. unsigned int channel;
  232. enum dma_data_direction direction;
  233. enum sdma_peripheral_type peripheral_type;
  234. unsigned int event_id0;
  235. unsigned int event_id1;
  236. enum dma_slave_buswidth word_size;
  237. unsigned int buf_tail;
  238. struct completion done;
  239. unsigned int num_bd;
  240. struct sdma_buffer_descriptor *bd;
  241. dma_addr_t bd_phys;
  242. unsigned int pc_from_device, pc_to_device;
  243. unsigned long flags;
  244. dma_addr_t per_address;
  245. u32 event_mask0, event_mask1;
  246. u32 watermark_level;
  247. u32 shp_addr, per_addr;
  248. struct dma_chan chan;
  249. spinlock_t lock;
  250. struct dma_async_tx_descriptor desc;
  251. dma_cookie_t last_completed;
  252. enum dma_status status;
  253. };
  254. #define IMX_DMA_SG_LOOP (1 << 0)
  255. #define MAX_DMA_CHANNELS 32
  256. #define MXC_SDMA_DEFAULT_PRIORITY 1
  257. #define MXC_SDMA_MIN_PRIORITY 1
  258. #define MXC_SDMA_MAX_PRIORITY 7
  259. #define SDMA_FIRMWARE_MAGIC 0x414d4453
  260. /**
  261. * struct sdma_firmware_header - Layout of the firmware image
  262. *
  263. * @magic "SDMA"
  264. * @version_major increased whenever layout of struct sdma_script_start_addrs
  265. * changes.
  266. * @version_minor firmware minor version (for binary compatible changes)
  267. * @script_addrs_start offset of struct sdma_script_start_addrs in this image
  268. * @num_script_addrs Number of script addresses in this image
  269. * @ram_code_start offset of SDMA ram image in this firmware image
  270. * @ram_code_size size of SDMA ram image
  271. * @script_addrs Stores the start address of the SDMA scripts
  272. * (in SDMA memory space)
  273. */
  274. struct sdma_firmware_header {
  275. u32 magic;
  276. u32 version_major;
  277. u32 version_minor;
  278. u32 script_addrs_start;
  279. u32 num_script_addrs;
  280. u32 ram_code_start;
  281. u32 ram_code_size;
  282. };
  283. enum sdma_devtype {
  284. IMX31_SDMA, /* runs on i.mx31 */
  285. IMX35_SDMA, /* runs on i.mx35 and later */
  286. };
  287. struct sdma_engine {
  288. struct device *dev;
  289. struct device_dma_parameters dma_parms;
  290. struct sdma_channel channel[MAX_DMA_CHANNELS];
  291. struct sdma_channel_control *channel_control;
  292. void __iomem *regs;
  293. enum sdma_devtype devtype;
  294. unsigned int num_events;
  295. struct sdma_context_data *context;
  296. dma_addr_t context_phys;
  297. struct dma_device dma_device;
  298. struct clk *clk;
  299. struct mutex channel_0_lock;
  300. struct sdma_script_start_addrs *script_addrs;
  301. };
  302. static struct platform_device_id sdma_devtypes[] = {
  303. {
  304. .name = "imx31-sdma",
  305. .driver_data = IMX31_SDMA,
  306. }, {
  307. .name = "imx35-sdma",
  308. .driver_data = IMX35_SDMA,
  309. }, {
  310. /* sentinel */
  311. }
  312. };
  313. MODULE_DEVICE_TABLE(platform, sdma_devtypes);
  314. static const struct of_device_id sdma_dt_ids[] = {
  315. { .compatible = "fsl,imx31-sdma", .data = &sdma_devtypes[IMX31_SDMA], },
  316. { .compatible = "fsl,imx35-sdma", .data = &sdma_devtypes[IMX35_SDMA], },
  317. { /* sentinel */ }
  318. };
  319. MODULE_DEVICE_TABLE(of, sdma_dt_ids);
  320. #define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */
  321. #define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */
  322. #define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */
  323. #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
  324. static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
  325. {
  326. u32 chnenbl0 = (sdma->devtype == IMX31_SDMA ? SDMA_CHNENBL0_IMX31 :
  327. SDMA_CHNENBL0_IMX35);
  328. return chnenbl0 + event * 4;
  329. }
  330. static int sdma_config_ownership(struct sdma_channel *sdmac,
  331. bool event_override, bool mcu_override, bool dsp_override)
  332. {
  333. struct sdma_engine *sdma = sdmac->sdma;
  334. int channel = sdmac->channel;
  335. u32 evt, mcu, dsp;
  336. if (event_override && mcu_override && dsp_override)
  337. return -EINVAL;
  338. evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR);
  339. mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR);
  340. dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR);
  341. if (dsp_override)
  342. dsp &= ~(1 << channel);
  343. else
  344. dsp |= (1 << channel);
  345. if (event_override)
  346. evt &= ~(1 << channel);
  347. else
  348. evt |= (1 << channel);
  349. if (mcu_override)
  350. mcu &= ~(1 << channel);
  351. else
  352. mcu |= (1 << channel);
  353. __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR);
  354. __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR);
  355. __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR);
  356. return 0;
  357. }
  358. /*
  359. * sdma_run_channel - run a channel and wait till it's done
  360. */
  361. static int sdma_run_channel(struct sdma_channel *sdmac)
  362. {
  363. struct sdma_engine *sdma = sdmac->sdma;
  364. int channel = sdmac->channel;
  365. int ret;
  366. init_completion(&sdmac->done);
  367. __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
  368. ret = wait_for_completion_timeout(&sdmac->done, HZ);
  369. return ret ? 0 : -ETIMEDOUT;
  370. }
  371. static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
  372. u32 address)
  373. {
  374. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  375. void *buf_virt;
  376. dma_addr_t buf_phys;
  377. int ret;
  378. mutex_lock(&sdma->channel_0_lock);
  379. buf_virt = dma_alloc_coherent(NULL,
  380. size,
  381. &buf_phys, GFP_KERNEL);
  382. if (!buf_virt) {
  383. ret = -ENOMEM;
  384. goto err_out;
  385. }
  386. bd0->mode.command = C0_SETPM;
  387. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  388. bd0->mode.count = size / 2;
  389. bd0->buffer_addr = buf_phys;
  390. bd0->ext_buffer_addr = address;
  391. memcpy(buf_virt, buf, size);
  392. ret = sdma_run_channel(&sdma->channel[0]);
  393. dma_free_coherent(NULL, size, buf_virt, buf_phys);
  394. err_out:
  395. mutex_unlock(&sdma->channel_0_lock);
  396. return ret;
  397. }
  398. static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
  399. {
  400. struct sdma_engine *sdma = sdmac->sdma;
  401. int channel = sdmac->channel;
  402. u32 val;
  403. u32 chnenbl = chnenbl_ofs(sdma, event);
  404. val = __raw_readl(sdma->regs + chnenbl);
  405. val |= (1 << channel);
  406. __raw_writel(val, sdma->regs + chnenbl);
  407. }
  408. static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
  409. {
  410. struct sdma_engine *sdma = sdmac->sdma;
  411. int channel = sdmac->channel;
  412. u32 chnenbl = chnenbl_ofs(sdma, event);
  413. u32 val;
  414. val = __raw_readl(sdma->regs + chnenbl);
  415. val &= ~(1 << channel);
  416. __raw_writel(val, sdma->regs + chnenbl);
  417. }
  418. static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
  419. {
  420. struct sdma_buffer_descriptor *bd;
  421. /*
  422. * loop mode. Iterate over descriptors, re-setup them and
  423. * call callback function.
  424. */
  425. while (1) {
  426. bd = &sdmac->bd[sdmac->buf_tail];
  427. if (bd->mode.status & BD_DONE)
  428. break;
  429. if (bd->mode.status & BD_RROR)
  430. sdmac->status = DMA_ERROR;
  431. else
  432. sdmac->status = DMA_IN_PROGRESS;
  433. bd->mode.status |= BD_DONE;
  434. sdmac->buf_tail++;
  435. sdmac->buf_tail %= sdmac->num_bd;
  436. if (sdmac->desc.callback)
  437. sdmac->desc.callback(sdmac->desc.callback_param);
  438. }
  439. }
  440. static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
  441. {
  442. struct sdma_buffer_descriptor *bd;
  443. int i, error = 0;
  444. /*
  445. * non loop mode. Iterate over all descriptors, collect
  446. * errors and call callback function
  447. */
  448. for (i = 0; i < sdmac->num_bd; i++) {
  449. bd = &sdmac->bd[i];
  450. if (bd->mode.status & (BD_DONE | BD_RROR))
  451. error = -EIO;
  452. }
  453. if (error)
  454. sdmac->status = DMA_ERROR;
  455. else
  456. sdmac->status = DMA_SUCCESS;
  457. if (sdmac->desc.callback)
  458. sdmac->desc.callback(sdmac->desc.callback_param);
  459. sdmac->last_completed = sdmac->desc.cookie;
  460. }
  461. static void mxc_sdma_handle_channel(struct sdma_channel *sdmac)
  462. {
  463. complete(&sdmac->done);
  464. /* not interested in channel 0 interrupts */
  465. if (sdmac->channel == 0)
  466. return;
  467. if (sdmac->flags & IMX_DMA_SG_LOOP)
  468. sdma_handle_channel_loop(sdmac);
  469. else
  470. mxc_sdma_handle_channel_normal(sdmac);
  471. }
  472. static irqreturn_t sdma_int_handler(int irq, void *dev_id)
  473. {
  474. struct sdma_engine *sdma = dev_id;
  475. u32 stat;
  476. stat = __raw_readl(sdma->regs + SDMA_H_INTR);
  477. __raw_writel(stat, sdma->regs + SDMA_H_INTR);
  478. while (stat) {
  479. int channel = fls(stat) - 1;
  480. struct sdma_channel *sdmac = &sdma->channel[channel];
  481. mxc_sdma_handle_channel(sdmac);
  482. stat &= ~(1 << channel);
  483. }
  484. return IRQ_HANDLED;
  485. }
  486. /*
  487. * sets the pc of SDMA script according to the peripheral type
  488. */
  489. static void sdma_get_pc(struct sdma_channel *sdmac,
  490. enum sdma_peripheral_type peripheral_type)
  491. {
  492. struct sdma_engine *sdma = sdmac->sdma;
  493. int per_2_emi = 0, emi_2_per = 0;
  494. /*
  495. * These are needed once we start to support transfers between
  496. * two peripherals or memory-to-memory transfers
  497. */
  498. int per_2_per = 0, emi_2_emi = 0;
  499. sdmac->pc_from_device = 0;
  500. sdmac->pc_to_device = 0;
  501. switch (peripheral_type) {
  502. case IMX_DMATYPE_MEMORY:
  503. emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
  504. break;
  505. case IMX_DMATYPE_DSP:
  506. emi_2_per = sdma->script_addrs->bp_2_ap_addr;
  507. per_2_emi = sdma->script_addrs->ap_2_bp_addr;
  508. break;
  509. case IMX_DMATYPE_FIRI:
  510. per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
  511. emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
  512. break;
  513. case IMX_DMATYPE_UART:
  514. per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
  515. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  516. break;
  517. case IMX_DMATYPE_UART_SP:
  518. per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
  519. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  520. break;
  521. case IMX_DMATYPE_ATA:
  522. per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
  523. emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
  524. break;
  525. case IMX_DMATYPE_CSPI:
  526. case IMX_DMATYPE_EXT:
  527. case IMX_DMATYPE_SSI:
  528. per_2_emi = sdma->script_addrs->app_2_mcu_addr;
  529. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  530. break;
  531. case IMX_DMATYPE_SSI_SP:
  532. case IMX_DMATYPE_MMC:
  533. case IMX_DMATYPE_SDHC:
  534. case IMX_DMATYPE_CSPI_SP:
  535. case IMX_DMATYPE_ESAI:
  536. case IMX_DMATYPE_MSHC_SP:
  537. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  538. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  539. break;
  540. case IMX_DMATYPE_ASRC:
  541. per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
  542. emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
  543. per_2_per = sdma->script_addrs->per_2_per_addr;
  544. break;
  545. case IMX_DMATYPE_MSHC:
  546. per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
  547. emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
  548. break;
  549. case IMX_DMATYPE_CCM:
  550. per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
  551. break;
  552. case IMX_DMATYPE_SPDIF:
  553. per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
  554. emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
  555. break;
  556. case IMX_DMATYPE_IPU_MEMORY:
  557. emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
  558. break;
  559. default:
  560. break;
  561. }
  562. sdmac->pc_from_device = per_2_emi;
  563. sdmac->pc_to_device = emi_2_per;
  564. }
  565. static int sdma_load_context(struct sdma_channel *sdmac)
  566. {
  567. struct sdma_engine *sdma = sdmac->sdma;
  568. int channel = sdmac->channel;
  569. int load_address;
  570. struct sdma_context_data *context = sdma->context;
  571. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  572. int ret;
  573. if (sdmac->direction == DMA_FROM_DEVICE) {
  574. load_address = sdmac->pc_from_device;
  575. } else {
  576. load_address = sdmac->pc_to_device;
  577. }
  578. if (load_address < 0)
  579. return load_address;
  580. dev_dbg(sdma->dev, "load_address = %d\n", load_address);
  581. dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level);
  582. dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
  583. dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
  584. dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0);
  585. dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1);
  586. mutex_lock(&sdma->channel_0_lock);
  587. memset(context, 0, sizeof(*context));
  588. context->channel_state.pc = load_address;
  589. /* Send by context the event mask,base address for peripheral
  590. * and watermark level
  591. */
  592. context->gReg[0] = sdmac->event_mask1;
  593. context->gReg[1] = sdmac->event_mask0;
  594. context->gReg[2] = sdmac->per_addr;
  595. context->gReg[6] = sdmac->shp_addr;
  596. context->gReg[7] = sdmac->watermark_level;
  597. bd0->mode.command = C0_SETDM;
  598. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  599. bd0->mode.count = sizeof(*context) / 4;
  600. bd0->buffer_addr = sdma->context_phys;
  601. bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
  602. ret = sdma_run_channel(&sdma->channel[0]);
  603. mutex_unlock(&sdma->channel_0_lock);
  604. return ret;
  605. }
  606. static void sdma_disable_channel(struct sdma_channel *sdmac)
  607. {
  608. struct sdma_engine *sdma = sdmac->sdma;
  609. int channel = sdmac->channel;
  610. __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP);
  611. sdmac->status = DMA_ERROR;
  612. }
  613. static int sdma_config_channel(struct sdma_channel *sdmac)
  614. {
  615. int ret;
  616. sdma_disable_channel(sdmac);
  617. sdmac->event_mask0 = 0;
  618. sdmac->event_mask1 = 0;
  619. sdmac->shp_addr = 0;
  620. sdmac->per_addr = 0;
  621. if (sdmac->event_id0) {
  622. if (sdmac->event_id0 > 32)
  623. return -EINVAL;
  624. sdma_event_enable(sdmac, sdmac->event_id0);
  625. }
  626. switch (sdmac->peripheral_type) {
  627. case IMX_DMATYPE_DSP:
  628. sdma_config_ownership(sdmac, false, true, true);
  629. break;
  630. case IMX_DMATYPE_MEMORY:
  631. sdma_config_ownership(sdmac, false, true, false);
  632. break;
  633. default:
  634. sdma_config_ownership(sdmac, true, true, false);
  635. break;
  636. }
  637. sdma_get_pc(sdmac, sdmac->peripheral_type);
  638. if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
  639. (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
  640. /* Handle multiple event channels differently */
  641. if (sdmac->event_id1) {
  642. sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32);
  643. if (sdmac->event_id1 > 31)
  644. sdmac->watermark_level |= 1 << 31;
  645. sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32);
  646. if (sdmac->event_id0 > 31)
  647. sdmac->watermark_level |= 1 << 30;
  648. } else {
  649. sdmac->event_mask0 = 1 << sdmac->event_id0;
  650. sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32);
  651. }
  652. /* Watermark Level */
  653. sdmac->watermark_level |= sdmac->watermark_level;
  654. /* Address */
  655. sdmac->shp_addr = sdmac->per_address;
  656. } else {
  657. sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
  658. }
  659. ret = sdma_load_context(sdmac);
  660. return ret;
  661. }
  662. static int sdma_set_channel_priority(struct sdma_channel *sdmac,
  663. unsigned int priority)
  664. {
  665. struct sdma_engine *sdma = sdmac->sdma;
  666. int channel = sdmac->channel;
  667. if (priority < MXC_SDMA_MIN_PRIORITY
  668. || priority > MXC_SDMA_MAX_PRIORITY) {
  669. return -EINVAL;
  670. }
  671. __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
  672. return 0;
  673. }
  674. static int sdma_request_channel(struct sdma_channel *sdmac)
  675. {
  676. struct sdma_engine *sdma = sdmac->sdma;
  677. int channel = sdmac->channel;
  678. int ret = -EBUSY;
  679. sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
  680. if (!sdmac->bd) {
  681. ret = -ENOMEM;
  682. goto out;
  683. }
  684. memset(sdmac->bd, 0, PAGE_SIZE);
  685. sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
  686. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  687. clk_enable(sdma->clk);
  688. sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
  689. init_completion(&sdmac->done);
  690. sdmac->buf_tail = 0;
  691. return 0;
  692. out:
  693. return ret;
  694. }
  695. static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
  696. {
  697. __raw_writel(1 << channel, sdma->regs + SDMA_H_START);
  698. }
  699. static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdmac)
  700. {
  701. dma_cookie_t cookie = sdmac->chan.cookie;
  702. if (++cookie < 0)
  703. cookie = 1;
  704. sdmac->chan.cookie = cookie;
  705. sdmac->desc.cookie = cookie;
  706. return cookie;
  707. }
  708. static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
  709. {
  710. return container_of(chan, struct sdma_channel, chan);
  711. }
  712. static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
  713. {
  714. struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
  715. struct sdma_engine *sdma = sdmac->sdma;
  716. dma_cookie_t cookie;
  717. spin_lock_irq(&sdmac->lock);
  718. cookie = sdma_assign_cookie(sdmac);
  719. sdma_enable_channel(sdma, sdmac->channel);
  720. spin_unlock_irq(&sdmac->lock);
  721. return cookie;
  722. }
  723. static int sdma_alloc_chan_resources(struct dma_chan *chan)
  724. {
  725. struct sdma_channel *sdmac = to_sdma_chan(chan);
  726. struct imx_dma_data *data = chan->private;
  727. int prio, ret;
  728. if (!data)
  729. return -EINVAL;
  730. switch (data->priority) {
  731. case DMA_PRIO_HIGH:
  732. prio = 3;
  733. break;
  734. case DMA_PRIO_MEDIUM:
  735. prio = 2;
  736. break;
  737. case DMA_PRIO_LOW:
  738. default:
  739. prio = 1;
  740. break;
  741. }
  742. sdmac->peripheral_type = data->peripheral_type;
  743. sdmac->event_id0 = data->dma_request;
  744. ret = sdma_set_channel_priority(sdmac, prio);
  745. if (ret)
  746. return ret;
  747. ret = sdma_request_channel(sdmac);
  748. if (ret)
  749. return ret;
  750. dma_async_tx_descriptor_init(&sdmac->desc, chan);
  751. sdmac->desc.tx_submit = sdma_tx_submit;
  752. /* txd.flags will be overwritten in prep funcs */
  753. sdmac->desc.flags = DMA_CTRL_ACK;
  754. return 0;
  755. }
  756. static void sdma_free_chan_resources(struct dma_chan *chan)
  757. {
  758. struct sdma_channel *sdmac = to_sdma_chan(chan);
  759. struct sdma_engine *sdma = sdmac->sdma;
  760. sdma_disable_channel(sdmac);
  761. if (sdmac->event_id0)
  762. sdma_event_disable(sdmac, sdmac->event_id0);
  763. if (sdmac->event_id1)
  764. sdma_event_disable(sdmac, sdmac->event_id1);
  765. sdmac->event_id0 = 0;
  766. sdmac->event_id1 = 0;
  767. sdma_set_channel_priority(sdmac, 0);
  768. dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
  769. clk_disable(sdma->clk);
  770. }
  771. static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
  772. struct dma_chan *chan, struct scatterlist *sgl,
  773. unsigned int sg_len, enum dma_data_direction direction,
  774. unsigned long flags)
  775. {
  776. struct sdma_channel *sdmac = to_sdma_chan(chan);
  777. struct sdma_engine *sdma = sdmac->sdma;
  778. int ret, i, count;
  779. int channel = sdmac->channel;
  780. struct scatterlist *sg;
  781. if (sdmac->status == DMA_IN_PROGRESS)
  782. return NULL;
  783. sdmac->status = DMA_IN_PROGRESS;
  784. sdmac->flags = 0;
  785. dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
  786. sg_len, channel);
  787. sdmac->direction = direction;
  788. ret = sdma_load_context(sdmac);
  789. if (ret)
  790. goto err_out;
  791. if (sg_len > NUM_BD) {
  792. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  793. channel, sg_len, NUM_BD);
  794. ret = -EINVAL;
  795. goto err_out;
  796. }
  797. for_each_sg(sgl, sg, sg_len, i) {
  798. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  799. int param;
  800. bd->buffer_addr = sg->dma_address;
  801. count = sg->length;
  802. if (count > 0xffff) {
  803. dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
  804. channel, count, 0xffff);
  805. ret = -EINVAL;
  806. goto err_out;
  807. }
  808. bd->mode.count = count;
  809. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
  810. ret = -EINVAL;
  811. goto err_out;
  812. }
  813. switch (sdmac->word_size) {
  814. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  815. bd->mode.command = 0;
  816. if (count & 3 || sg->dma_address & 3)
  817. return NULL;
  818. break;
  819. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  820. bd->mode.command = 2;
  821. if (count & 1 || sg->dma_address & 1)
  822. return NULL;
  823. break;
  824. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  825. bd->mode.command = 1;
  826. break;
  827. default:
  828. return NULL;
  829. }
  830. param = BD_DONE | BD_EXTD | BD_CONT;
  831. if (i + 1 == sg_len) {
  832. param |= BD_INTR;
  833. param |= BD_LAST;
  834. param &= ~BD_CONT;
  835. }
  836. dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
  837. i, count, sg->dma_address,
  838. param & BD_WRAP ? "wrap" : "",
  839. param & BD_INTR ? " intr" : "");
  840. bd->mode.status = param;
  841. }
  842. sdmac->num_bd = sg_len;
  843. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  844. return &sdmac->desc;
  845. err_out:
  846. sdmac->status = DMA_ERROR;
  847. return NULL;
  848. }
  849. static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
  850. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  851. size_t period_len, enum dma_data_direction direction)
  852. {
  853. struct sdma_channel *sdmac = to_sdma_chan(chan);
  854. struct sdma_engine *sdma = sdmac->sdma;
  855. int num_periods = buf_len / period_len;
  856. int channel = sdmac->channel;
  857. int ret, i = 0, buf = 0;
  858. dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
  859. if (sdmac->status == DMA_IN_PROGRESS)
  860. return NULL;
  861. sdmac->status = DMA_IN_PROGRESS;
  862. sdmac->flags |= IMX_DMA_SG_LOOP;
  863. sdmac->direction = direction;
  864. ret = sdma_load_context(sdmac);
  865. if (ret)
  866. goto err_out;
  867. if (num_periods > NUM_BD) {
  868. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  869. channel, num_periods, NUM_BD);
  870. goto err_out;
  871. }
  872. if (period_len > 0xffff) {
  873. dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
  874. channel, period_len, 0xffff);
  875. goto err_out;
  876. }
  877. while (buf < buf_len) {
  878. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  879. int param;
  880. bd->buffer_addr = dma_addr;
  881. bd->mode.count = period_len;
  882. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  883. goto err_out;
  884. if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
  885. bd->mode.command = 0;
  886. else
  887. bd->mode.command = sdmac->word_size;
  888. param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
  889. if (i + 1 == num_periods)
  890. param |= BD_WRAP;
  891. dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
  892. i, period_len, dma_addr,
  893. param & BD_WRAP ? "wrap" : "",
  894. param & BD_INTR ? " intr" : "");
  895. bd->mode.status = param;
  896. dma_addr += period_len;
  897. buf += period_len;
  898. i++;
  899. }
  900. sdmac->num_bd = num_periods;
  901. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  902. return &sdmac->desc;
  903. err_out:
  904. sdmac->status = DMA_ERROR;
  905. return NULL;
  906. }
  907. static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  908. unsigned long arg)
  909. {
  910. struct sdma_channel *sdmac = to_sdma_chan(chan);
  911. struct dma_slave_config *dmaengine_cfg = (void *)arg;
  912. switch (cmd) {
  913. case DMA_TERMINATE_ALL:
  914. sdma_disable_channel(sdmac);
  915. return 0;
  916. case DMA_SLAVE_CONFIG:
  917. if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
  918. sdmac->per_address = dmaengine_cfg->src_addr;
  919. sdmac->watermark_level = dmaengine_cfg->src_maxburst;
  920. sdmac->word_size = dmaengine_cfg->src_addr_width;
  921. } else {
  922. sdmac->per_address = dmaengine_cfg->dst_addr;
  923. sdmac->watermark_level = dmaengine_cfg->dst_maxburst;
  924. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  925. }
  926. return sdma_config_channel(sdmac);
  927. default:
  928. return -ENOSYS;
  929. }
  930. return -EINVAL;
  931. }
  932. static enum dma_status sdma_tx_status(struct dma_chan *chan,
  933. dma_cookie_t cookie,
  934. struct dma_tx_state *txstate)
  935. {
  936. struct sdma_channel *sdmac = to_sdma_chan(chan);
  937. dma_cookie_t last_used;
  938. last_used = chan->cookie;
  939. dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0);
  940. return sdmac->status;
  941. }
  942. static void sdma_issue_pending(struct dma_chan *chan)
  943. {
  944. /*
  945. * Nothing to do. We only have a single descriptor
  946. */
  947. }
  948. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
  949. static void sdma_add_scripts(struct sdma_engine *sdma,
  950. const struct sdma_script_start_addrs *addr)
  951. {
  952. s32 *addr_arr = (u32 *)addr;
  953. s32 *saddr_arr = (u32 *)sdma->script_addrs;
  954. int i;
  955. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  956. if (addr_arr[i] > 0)
  957. saddr_arr[i] = addr_arr[i];
  958. }
  959. static int __init sdma_get_firmware(struct sdma_engine *sdma,
  960. const char *fw_name)
  961. {
  962. const struct firmware *fw;
  963. const struct sdma_firmware_header *header;
  964. int ret;
  965. const struct sdma_script_start_addrs *addr;
  966. unsigned short *ram_code;
  967. ret = request_firmware(&fw, fw_name, sdma->dev);
  968. if (ret)
  969. return ret;
  970. if (fw->size < sizeof(*header))
  971. goto err_firmware;
  972. header = (struct sdma_firmware_header *)fw->data;
  973. if (header->magic != SDMA_FIRMWARE_MAGIC)
  974. goto err_firmware;
  975. if (header->ram_code_start + header->ram_code_size > fw->size)
  976. goto err_firmware;
  977. addr = (void *)header + header->script_addrs_start;
  978. ram_code = (void *)header + header->ram_code_start;
  979. clk_enable(sdma->clk);
  980. /* download the RAM image for SDMA */
  981. sdma_load_script(sdma, ram_code,
  982. header->ram_code_size,
  983. addr->ram_code_start_addr);
  984. clk_disable(sdma->clk);
  985. sdma_add_scripts(sdma, addr);
  986. dev_info(sdma->dev, "loaded firmware %d.%d\n",
  987. header->version_major,
  988. header->version_minor);
  989. err_firmware:
  990. release_firmware(fw);
  991. return ret;
  992. }
  993. static int __init sdma_init(struct sdma_engine *sdma)
  994. {
  995. int i, ret;
  996. dma_addr_t ccb_phys;
  997. switch (sdma->devtype) {
  998. case IMX31_SDMA:
  999. sdma->num_events = 32;
  1000. break;
  1001. case IMX35_SDMA:
  1002. sdma->num_events = 48;
  1003. break;
  1004. default:
  1005. dev_err(sdma->dev, "Unknown sdma type %d. aborting\n",
  1006. sdma->devtype);
  1007. return -ENODEV;
  1008. }
  1009. clk_enable(sdma->clk);
  1010. /* Be sure SDMA has not started yet */
  1011. __raw_writel(0, sdma->regs + SDMA_H_C0PTR);
  1012. sdma->channel_control = dma_alloc_coherent(NULL,
  1013. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
  1014. sizeof(struct sdma_context_data),
  1015. &ccb_phys, GFP_KERNEL);
  1016. if (!sdma->channel_control) {
  1017. ret = -ENOMEM;
  1018. goto err_dma_alloc;
  1019. }
  1020. sdma->context = (void *)sdma->channel_control +
  1021. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1022. sdma->context_phys = ccb_phys +
  1023. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1024. /* Zero-out the CCB structures array just allocated */
  1025. memset(sdma->channel_control, 0,
  1026. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
  1027. /* disable all channels */
  1028. for (i = 0; i < sdma->num_events; i++)
  1029. __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i));
  1030. /* All channels have priority 0 */
  1031. for (i = 0; i < MAX_DMA_CHANNELS; i++)
  1032. __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
  1033. ret = sdma_request_channel(&sdma->channel[0]);
  1034. if (ret)
  1035. goto err_dma_alloc;
  1036. sdma_config_ownership(&sdma->channel[0], false, true, false);
  1037. /* Set Command Channel (Channel Zero) */
  1038. __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR);
  1039. /* Set bits of CONFIG register but with static context switching */
  1040. /* FIXME: Check whether to set ACR bit depending on clock ratios */
  1041. __raw_writel(0, sdma->regs + SDMA_H_CONFIG);
  1042. __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR);
  1043. /* Set bits of CONFIG register with given context switching mode */
  1044. __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
  1045. /* Initializes channel's priorities */
  1046. sdma_set_channel_priority(&sdma->channel[0], 7);
  1047. clk_disable(sdma->clk);
  1048. return 0;
  1049. err_dma_alloc:
  1050. clk_disable(sdma->clk);
  1051. dev_err(sdma->dev, "initialisation failed with %d\n", ret);
  1052. return ret;
  1053. }
  1054. static int __init sdma_probe(struct platform_device *pdev)
  1055. {
  1056. const struct of_device_id *of_id =
  1057. of_match_device(sdma_dt_ids, &pdev->dev);
  1058. struct device_node *np = pdev->dev.of_node;
  1059. const char *fw_name;
  1060. int ret;
  1061. int irq;
  1062. struct resource *iores;
  1063. struct sdma_platform_data *pdata = pdev->dev.platform_data;
  1064. int i;
  1065. struct sdma_engine *sdma;
  1066. s32 *saddr_arr;
  1067. sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
  1068. if (!sdma)
  1069. return -ENOMEM;
  1070. mutex_init(&sdma->channel_0_lock);
  1071. sdma->dev = &pdev->dev;
  1072. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1073. irq = platform_get_irq(pdev, 0);
  1074. if (!iores || irq < 0) {
  1075. ret = -EINVAL;
  1076. goto err_irq;
  1077. }
  1078. if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
  1079. ret = -EBUSY;
  1080. goto err_request_region;
  1081. }
  1082. sdma->clk = clk_get(&pdev->dev, NULL);
  1083. if (IS_ERR(sdma->clk)) {
  1084. ret = PTR_ERR(sdma->clk);
  1085. goto err_clk;
  1086. }
  1087. sdma->regs = ioremap(iores->start, resource_size(iores));
  1088. if (!sdma->regs) {
  1089. ret = -ENOMEM;
  1090. goto err_ioremap;
  1091. }
  1092. ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
  1093. if (ret)
  1094. goto err_request_irq;
  1095. sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
  1096. if (!sdma->script_addrs) {
  1097. ret = -ENOMEM;
  1098. goto err_alloc;
  1099. }
  1100. /* initially no scripts available */
  1101. saddr_arr = (s32 *)sdma->script_addrs;
  1102. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  1103. saddr_arr[i] = -EINVAL;
  1104. if (of_id)
  1105. pdev->id_entry = of_id->data;
  1106. sdma->devtype = pdev->id_entry->driver_data;
  1107. dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
  1108. dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
  1109. INIT_LIST_HEAD(&sdma->dma_device.channels);
  1110. /* Initialize channel parameters */
  1111. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1112. struct sdma_channel *sdmac = &sdma->channel[i];
  1113. sdmac->sdma = sdma;
  1114. spin_lock_init(&sdmac->lock);
  1115. sdmac->chan.device = &sdma->dma_device;
  1116. sdmac->channel = i;
  1117. /*
  1118. * Add the channel to the DMAC list. Do not add channel 0 though
  1119. * because we need it internally in the SDMA driver. This also means
  1120. * that channel 0 in dmaengine counting matches sdma channel 1.
  1121. */
  1122. if (i)
  1123. list_add_tail(&sdmac->chan.device_node,
  1124. &sdma->dma_device.channels);
  1125. }
  1126. ret = sdma_init(sdma);
  1127. if (ret)
  1128. goto err_init;
  1129. if (pdata && pdata->script_addrs)
  1130. sdma_add_scripts(sdma, pdata->script_addrs);
  1131. if (pdata) {
  1132. sdma_get_firmware(sdma, pdata->fw_name);
  1133. } else {
  1134. /*
  1135. * Because that device tree does not encode ROM script address,
  1136. * the RAM script in firmware is mandatory for device tree
  1137. * probe, otherwise it fails.
  1138. */
  1139. ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
  1140. &fw_name);
  1141. if (ret) {
  1142. dev_err(&pdev->dev, "failed to get firmware name\n");
  1143. goto err_init;
  1144. }
  1145. ret = sdma_get_firmware(sdma, fw_name);
  1146. if (ret) {
  1147. dev_err(&pdev->dev, "failed to get firmware\n");
  1148. goto err_init;
  1149. }
  1150. }
  1151. sdma->dma_device.dev = &pdev->dev;
  1152. sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
  1153. sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
  1154. sdma->dma_device.device_tx_status = sdma_tx_status;
  1155. sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
  1156. sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
  1157. sdma->dma_device.device_control = sdma_control;
  1158. sdma->dma_device.device_issue_pending = sdma_issue_pending;
  1159. sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
  1160. dma_set_max_seg_size(sdma->dma_device.dev, 65535);
  1161. ret = dma_async_device_register(&sdma->dma_device);
  1162. if (ret) {
  1163. dev_err(&pdev->dev, "unable to register\n");
  1164. goto err_init;
  1165. }
  1166. dev_info(sdma->dev, "initialized\n");
  1167. return 0;
  1168. err_init:
  1169. kfree(sdma->script_addrs);
  1170. err_alloc:
  1171. free_irq(irq, sdma);
  1172. err_request_irq:
  1173. iounmap(sdma->regs);
  1174. err_ioremap:
  1175. clk_put(sdma->clk);
  1176. err_clk:
  1177. release_mem_region(iores->start, resource_size(iores));
  1178. err_request_region:
  1179. err_irq:
  1180. kfree(sdma);
  1181. return ret;
  1182. }
  1183. static int __exit sdma_remove(struct platform_device *pdev)
  1184. {
  1185. return -EBUSY;
  1186. }
  1187. static struct platform_driver sdma_driver = {
  1188. .driver = {
  1189. .name = "imx-sdma",
  1190. .of_match_table = sdma_dt_ids,
  1191. },
  1192. .id_table = sdma_devtypes,
  1193. .remove = __exit_p(sdma_remove),
  1194. };
  1195. static int __init sdma_module_init(void)
  1196. {
  1197. return platform_driver_probe(&sdma_driver, sdma_probe);
  1198. }
  1199. module_init(sdma_module_init);
  1200. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  1201. MODULE_DESCRIPTION("i.MX SDMA driver");
  1202. MODULE_LICENSE("GPL");