imx-sdma.c 36 KB

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