imx-sdma.c 36 KB

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