imx-sdma.c 36 KB

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