imx-sdma.c 36 KB

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