imx-sdma.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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/delay.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_ipg;
  304. struct clk *clk_ahb;
  305. spinlock_t channel_0_lock;
  306. struct sdma_script_start_addrs *script_addrs;
  307. };
  308. static struct platform_device_id sdma_devtypes[] = {
  309. {
  310. .name = "imx31-sdma",
  311. .driver_data = IMX31_SDMA,
  312. }, {
  313. .name = "imx35-sdma",
  314. .driver_data = IMX35_SDMA,
  315. }, {
  316. /* sentinel */
  317. }
  318. };
  319. MODULE_DEVICE_TABLE(platform, sdma_devtypes);
  320. static const struct of_device_id sdma_dt_ids[] = {
  321. { .compatible = "fsl,imx31-sdma", .data = &sdma_devtypes[IMX31_SDMA], },
  322. { .compatible = "fsl,imx35-sdma", .data = &sdma_devtypes[IMX35_SDMA], },
  323. { /* sentinel */ }
  324. };
  325. MODULE_DEVICE_TABLE(of, sdma_dt_ids);
  326. #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
  327. #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
  328. #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
  329. #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
  330. static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
  331. {
  332. u32 chnenbl0 = (sdma->devtype == IMX31_SDMA ? SDMA_CHNENBL0_IMX31 :
  333. SDMA_CHNENBL0_IMX35);
  334. return chnenbl0 + event * 4;
  335. }
  336. static int sdma_config_ownership(struct sdma_channel *sdmac,
  337. bool event_override, bool mcu_override, bool dsp_override)
  338. {
  339. struct sdma_engine *sdma = sdmac->sdma;
  340. int channel = sdmac->channel;
  341. unsigned long evt, mcu, dsp;
  342. if (event_override && mcu_override && dsp_override)
  343. return -EINVAL;
  344. evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
  345. mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
  346. dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
  347. if (dsp_override)
  348. __clear_bit(channel, &dsp);
  349. else
  350. __set_bit(channel, &dsp);
  351. if (event_override)
  352. __clear_bit(channel, &evt);
  353. else
  354. __set_bit(channel, &evt);
  355. if (mcu_override)
  356. __clear_bit(channel, &mcu);
  357. else
  358. __set_bit(channel, &mcu);
  359. writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
  360. writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
  361. writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
  362. return 0;
  363. }
  364. static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
  365. {
  366. writel(BIT(channel), sdma->regs + SDMA_H_START);
  367. }
  368. /*
  369. * sdma_run_channel0 - run a channel and wait till it's done
  370. */
  371. static int sdma_run_channel0(struct sdma_engine *sdma)
  372. {
  373. int ret;
  374. unsigned long timeout = 500;
  375. sdma_enable_channel(sdma, 0);
  376. while (!(ret = readl_relaxed(sdma->regs + SDMA_H_INTR) & 1)) {
  377. if (timeout-- <= 0)
  378. break;
  379. udelay(1);
  380. }
  381. if (ret) {
  382. /* Clear the interrupt status */
  383. writel_relaxed(ret, sdma->regs + SDMA_H_INTR);
  384. } else {
  385. dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
  386. }
  387. return ret ? 0 : -ETIMEDOUT;
  388. }
  389. static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
  390. u32 address)
  391. {
  392. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  393. void *buf_virt;
  394. dma_addr_t buf_phys;
  395. int ret;
  396. unsigned long flags;
  397. buf_virt = dma_alloc_coherent(NULL,
  398. size,
  399. &buf_phys, GFP_KERNEL);
  400. if (!buf_virt) {
  401. return -ENOMEM;
  402. }
  403. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  404. bd0->mode.command = C0_SETPM;
  405. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  406. bd0->mode.count = size / 2;
  407. bd0->buffer_addr = buf_phys;
  408. bd0->ext_buffer_addr = address;
  409. memcpy(buf_virt, buf, size);
  410. ret = sdma_run_channel0(sdma);
  411. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  412. dma_free_coherent(NULL, size, buf_virt, buf_phys);
  413. return ret;
  414. }
  415. static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
  416. {
  417. struct sdma_engine *sdma = sdmac->sdma;
  418. int channel = sdmac->channel;
  419. unsigned long val;
  420. u32 chnenbl = chnenbl_ofs(sdma, event);
  421. val = readl_relaxed(sdma->regs + chnenbl);
  422. __set_bit(channel, &val);
  423. writel_relaxed(val, sdma->regs + chnenbl);
  424. }
  425. static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
  426. {
  427. struct sdma_engine *sdma = sdmac->sdma;
  428. int channel = sdmac->channel;
  429. u32 chnenbl = chnenbl_ofs(sdma, event);
  430. unsigned long val;
  431. val = readl_relaxed(sdma->regs + chnenbl);
  432. __clear_bit(channel, &val);
  433. writel_relaxed(val, sdma->regs + chnenbl);
  434. }
  435. static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
  436. {
  437. struct sdma_buffer_descriptor *bd;
  438. /*
  439. * loop mode. Iterate over descriptors, re-setup them and
  440. * call callback function.
  441. */
  442. while (1) {
  443. bd = &sdmac->bd[sdmac->buf_tail];
  444. if (bd->mode.status & BD_DONE)
  445. break;
  446. if (bd->mode.status & BD_RROR)
  447. sdmac->status = DMA_ERROR;
  448. else
  449. sdmac->status = DMA_IN_PROGRESS;
  450. bd->mode.status |= BD_DONE;
  451. sdmac->buf_tail++;
  452. sdmac->buf_tail %= sdmac->num_bd;
  453. if (sdmac->desc.callback)
  454. sdmac->desc.callback(sdmac->desc.callback_param);
  455. }
  456. }
  457. static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
  458. {
  459. struct sdma_buffer_descriptor *bd;
  460. int i, error = 0;
  461. sdmac->chn_real_count = 0;
  462. /*
  463. * non loop mode. Iterate over all descriptors, collect
  464. * errors and call callback function
  465. */
  466. for (i = 0; i < sdmac->num_bd; i++) {
  467. bd = &sdmac->bd[i];
  468. if (bd->mode.status & (BD_DONE | BD_RROR))
  469. error = -EIO;
  470. sdmac->chn_real_count += bd->mode.count;
  471. }
  472. if (error)
  473. sdmac->status = DMA_ERROR;
  474. else
  475. sdmac->status = DMA_SUCCESS;
  476. dma_cookie_complete(&sdmac->desc);
  477. if (sdmac->desc.callback)
  478. sdmac->desc.callback(sdmac->desc.callback_param);
  479. }
  480. static void sdma_tasklet(unsigned long data)
  481. {
  482. struct sdma_channel *sdmac = (struct sdma_channel *) data;
  483. complete(&sdmac->done);
  484. if (sdmac->flags & IMX_DMA_SG_LOOP)
  485. sdma_handle_channel_loop(sdmac);
  486. else
  487. mxc_sdma_handle_channel_normal(sdmac);
  488. }
  489. static irqreturn_t sdma_int_handler(int irq, void *dev_id)
  490. {
  491. struct sdma_engine *sdma = dev_id;
  492. unsigned long stat;
  493. stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
  494. /* not interested in channel 0 interrupts */
  495. stat &= ~1;
  496. writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
  497. while (stat) {
  498. int channel = fls(stat) - 1;
  499. struct sdma_channel *sdmac = &sdma->channel[channel];
  500. tasklet_schedule(&sdmac->tasklet);
  501. __clear_bit(channel, &stat);
  502. }
  503. return IRQ_HANDLED;
  504. }
  505. /*
  506. * sets the pc of SDMA script according to the peripheral type
  507. */
  508. static void sdma_get_pc(struct sdma_channel *sdmac,
  509. enum sdma_peripheral_type peripheral_type)
  510. {
  511. struct sdma_engine *sdma = sdmac->sdma;
  512. int per_2_emi = 0, emi_2_per = 0;
  513. /*
  514. * These are needed once we start to support transfers between
  515. * two peripherals or memory-to-memory transfers
  516. */
  517. int per_2_per = 0, emi_2_emi = 0;
  518. sdmac->pc_from_device = 0;
  519. sdmac->pc_to_device = 0;
  520. switch (peripheral_type) {
  521. case IMX_DMATYPE_MEMORY:
  522. emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
  523. break;
  524. case IMX_DMATYPE_DSP:
  525. emi_2_per = sdma->script_addrs->bp_2_ap_addr;
  526. per_2_emi = sdma->script_addrs->ap_2_bp_addr;
  527. break;
  528. case IMX_DMATYPE_FIRI:
  529. per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
  530. emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
  531. break;
  532. case IMX_DMATYPE_UART:
  533. per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
  534. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  535. break;
  536. case IMX_DMATYPE_UART_SP:
  537. per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
  538. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  539. break;
  540. case IMX_DMATYPE_ATA:
  541. per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
  542. emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
  543. break;
  544. case IMX_DMATYPE_CSPI:
  545. case IMX_DMATYPE_EXT:
  546. case IMX_DMATYPE_SSI:
  547. per_2_emi = sdma->script_addrs->app_2_mcu_addr;
  548. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  549. break;
  550. case IMX_DMATYPE_SSI_SP:
  551. case IMX_DMATYPE_MMC:
  552. case IMX_DMATYPE_SDHC:
  553. case IMX_DMATYPE_CSPI_SP:
  554. case IMX_DMATYPE_ESAI:
  555. case IMX_DMATYPE_MSHC_SP:
  556. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  557. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  558. break;
  559. case IMX_DMATYPE_ASRC:
  560. per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
  561. emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
  562. per_2_per = sdma->script_addrs->per_2_per_addr;
  563. break;
  564. case IMX_DMATYPE_MSHC:
  565. per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
  566. emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
  567. break;
  568. case IMX_DMATYPE_CCM:
  569. per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
  570. break;
  571. case IMX_DMATYPE_SPDIF:
  572. per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
  573. emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
  574. break;
  575. case IMX_DMATYPE_IPU_MEMORY:
  576. emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
  577. break;
  578. default:
  579. break;
  580. }
  581. sdmac->pc_from_device = per_2_emi;
  582. sdmac->pc_to_device = emi_2_per;
  583. }
  584. static int sdma_load_context(struct sdma_channel *sdmac)
  585. {
  586. struct sdma_engine *sdma = sdmac->sdma;
  587. int channel = sdmac->channel;
  588. int load_address;
  589. struct sdma_context_data *context = sdma->context;
  590. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  591. int ret;
  592. unsigned long flags;
  593. if (sdmac->direction == DMA_DEV_TO_MEM) {
  594. load_address = sdmac->pc_from_device;
  595. } else {
  596. load_address = sdmac->pc_to_device;
  597. }
  598. if (load_address < 0)
  599. return load_address;
  600. dev_dbg(sdma->dev, "load_address = %d\n", load_address);
  601. dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
  602. dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
  603. dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
  604. dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
  605. dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
  606. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  607. memset(context, 0, sizeof(*context));
  608. context->channel_state.pc = load_address;
  609. /* Send by context the event mask,base address for peripheral
  610. * and watermark level
  611. */
  612. context->gReg[0] = sdmac->event_mask[1];
  613. context->gReg[1] = sdmac->event_mask[0];
  614. context->gReg[2] = sdmac->per_addr;
  615. context->gReg[6] = sdmac->shp_addr;
  616. context->gReg[7] = sdmac->watermark_level;
  617. bd0->mode.command = C0_SETDM;
  618. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  619. bd0->mode.count = sizeof(*context) / 4;
  620. bd0->buffer_addr = sdma->context_phys;
  621. bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
  622. ret = sdma_run_channel0(sdma);
  623. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  624. return ret;
  625. }
  626. static void sdma_disable_channel(struct sdma_channel *sdmac)
  627. {
  628. struct sdma_engine *sdma = sdmac->sdma;
  629. int channel = sdmac->channel;
  630. writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
  631. sdmac->status = DMA_ERROR;
  632. }
  633. static int sdma_config_channel(struct sdma_channel *sdmac)
  634. {
  635. int ret;
  636. sdma_disable_channel(sdmac);
  637. sdmac->event_mask[0] = 0;
  638. sdmac->event_mask[1] = 0;
  639. sdmac->shp_addr = 0;
  640. sdmac->per_addr = 0;
  641. if (sdmac->event_id0) {
  642. if (sdmac->event_id0 >= sdmac->sdma->num_events)
  643. return -EINVAL;
  644. sdma_event_enable(sdmac, sdmac->event_id0);
  645. }
  646. switch (sdmac->peripheral_type) {
  647. case IMX_DMATYPE_DSP:
  648. sdma_config_ownership(sdmac, false, true, true);
  649. break;
  650. case IMX_DMATYPE_MEMORY:
  651. sdma_config_ownership(sdmac, false, true, false);
  652. break;
  653. default:
  654. sdma_config_ownership(sdmac, true, true, false);
  655. break;
  656. }
  657. sdma_get_pc(sdmac, sdmac->peripheral_type);
  658. if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
  659. (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
  660. /* Handle multiple event channels differently */
  661. if (sdmac->event_id1) {
  662. sdmac->event_mask[1] = BIT(sdmac->event_id1 % 32);
  663. if (sdmac->event_id1 > 31)
  664. __set_bit(31, &sdmac->watermark_level);
  665. sdmac->event_mask[0] = BIT(sdmac->event_id0 % 32);
  666. if (sdmac->event_id0 > 31)
  667. __set_bit(30, &sdmac->watermark_level);
  668. } else {
  669. __set_bit(sdmac->event_id0, sdmac->event_mask);
  670. }
  671. /* Watermark Level */
  672. sdmac->watermark_level |= sdmac->watermark_level;
  673. /* Address */
  674. sdmac->shp_addr = sdmac->per_address;
  675. } else {
  676. sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
  677. }
  678. ret = sdma_load_context(sdmac);
  679. return ret;
  680. }
  681. static int sdma_set_channel_priority(struct sdma_channel *sdmac,
  682. unsigned int priority)
  683. {
  684. struct sdma_engine *sdma = sdmac->sdma;
  685. int channel = sdmac->channel;
  686. if (priority < MXC_SDMA_MIN_PRIORITY
  687. || priority > MXC_SDMA_MAX_PRIORITY) {
  688. return -EINVAL;
  689. }
  690. writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
  691. return 0;
  692. }
  693. static int sdma_request_channel(struct sdma_channel *sdmac)
  694. {
  695. struct sdma_engine *sdma = sdmac->sdma;
  696. int channel = sdmac->channel;
  697. int ret = -EBUSY;
  698. sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
  699. if (!sdmac->bd) {
  700. ret = -ENOMEM;
  701. goto out;
  702. }
  703. memset(sdmac->bd, 0, PAGE_SIZE);
  704. sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
  705. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  706. sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
  707. init_completion(&sdmac->done);
  708. sdmac->buf_tail = 0;
  709. return 0;
  710. out:
  711. return ret;
  712. }
  713. static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
  714. {
  715. return container_of(chan, struct sdma_channel, chan);
  716. }
  717. static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
  718. {
  719. unsigned long flags;
  720. struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
  721. dma_cookie_t cookie;
  722. spin_lock_irqsave(&sdmac->lock, flags);
  723. cookie = dma_cookie_assign(tx);
  724. spin_unlock_irqrestore(&sdmac->lock, flags);
  725. return cookie;
  726. }
  727. static int sdma_alloc_chan_resources(struct dma_chan *chan)
  728. {
  729. struct sdma_channel *sdmac = to_sdma_chan(chan);
  730. struct imx_dma_data *data = chan->private;
  731. int prio, ret;
  732. if (!data)
  733. return -EINVAL;
  734. switch (data->priority) {
  735. case DMA_PRIO_HIGH:
  736. prio = 3;
  737. break;
  738. case DMA_PRIO_MEDIUM:
  739. prio = 2;
  740. break;
  741. case DMA_PRIO_LOW:
  742. default:
  743. prio = 1;
  744. break;
  745. }
  746. sdmac->peripheral_type = data->peripheral_type;
  747. sdmac->event_id0 = data->dma_request;
  748. clk_enable(sdmac->sdma->clk_ipg);
  749. clk_enable(sdmac->sdma->clk_ahb);
  750. ret = sdma_request_channel(sdmac);
  751. if (ret)
  752. return ret;
  753. ret = sdma_set_channel_priority(sdmac, prio);
  754. if (ret)
  755. return ret;
  756. dma_async_tx_descriptor_init(&sdmac->desc, chan);
  757. sdmac->desc.tx_submit = sdma_tx_submit;
  758. /* txd.flags will be overwritten in prep funcs */
  759. sdmac->desc.flags = DMA_CTRL_ACK;
  760. return 0;
  761. }
  762. static void sdma_free_chan_resources(struct dma_chan *chan)
  763. {
  764. struct sdma_channel *sdmac = to_sdma_chan(chan);
  765. struct sdma_engine *sdma = sdmac->sdma;
  766. sdma_disable_channel(sdmac);
  767. if (sdmac->event_id0)
  768. sdma_event_disable(sdmac, sdmac->event_id0);
  769. if (sdmac->event_id1)
  770. sdma_event_disable(sdmac, sdmac->event_id1);
  771. sdmac->event_id0 = 0;
  772. sdmac->event_id1 = 0;
  773. sdma_set_channel_priority(sdmac, 0);
  774. dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
  775. clk_disable(sdma->clk_ipg);
  776. clk_disable(sdma->clk_ahb);
  777. }
  778. static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
  779. struct dma_chan *chan, struct scatterlist *sgl,
  780. unsigned int sg_len, enum dma_transfer_direction direction,
  781. unsigned long flags, void *context)
  782. {
  783. struct sdma_channel *sdmac = to_sdma_chan(chan);
  784. struct sdma_engine *sdma = sdmac->sdma;
  785. int ret, i, count;
  786. int channel = sdmac->channel;
  787. struct scatterlist *sg;
  788. if (sdmac->status == DMA_IN_PROGRESS)
  789. return NULL;
  790. sdmac->status = DMA_IN_PROGRESS;
  791. sdmac->flags = 0;
  792. dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
  793. sg_len, channel);
  794. sdmac->direction = direction;
  795. ret = sdma_load_context(sdmac);
  796. if (ret)
  797. goto err_out;
  798. if (sg_len > NUM_BD) {
  799. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  800. channel, sg_len, NUM_BD);
  801. ret = -EINVAL;
  802. goto err_out;
  803. }
  804. sdmac->chn_count = 0;
  805. for_each_sg(sgl, sg, sg_len, i) {
  806. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  807. int param;
  808. bd->buffer_addr = sg->dma_address;
  809. count = sg_dma_len(sg);
  810. if (count > 0xffff) {
  811. dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
  812. channel, count, 0xffff);
  813. ret = -EINVAL;
  814. goto err_out;
  815. }
  816. bd->mode.count = count;
  817. sdmac->chn_count += count;
  818. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
  819. ret = -EINVAL;
  820. goto err_out;
  821. }
  822. switch (sdmac->word_size) {
  823. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  824. bd->mode.command = 0;
  825. if (count & 3 || sg->dma_address & 3)
  826. return NULL;
  827. break;
  828. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  829. bd->mode.command = 2;
  830. if (count & 1 || sg->dma_address & 1)
  831. return NULL;
  832. break;
  833. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  834. bd->mode.command = 1;
  835. break;
  836. default:
  837. return NULL;
  838. }
  839. param = BD_DONE | BD_EXTD | BD_CONT;
  840. if (i + 1 == sg_len) {
  841. param |= BD_INTR;
  842. param |= BD_LAST;
  843. param &= ~BD_CONT;
  844. }
  845. dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
  846. i, count, sg->dma_address,
  847. param & BD_WRAP ? "wrap" : "",
  848. param & BD_INTR ? " intr" : "");
  849. bd->mode.status = param;
  850. }
  851. sdmac->num_bd = sg_len;
  852. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  853. return &sdmac->desc;
  854. err_out:
  855. sdmac->status = DMA_ERROR;
  856. return NULL;
  857. }
  858. static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
  859. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  860. size_t period_len, enum dma_transfer_direction direction,
  861. void *context)
  862. {
  863. struct sdma_channel *sdmac = to_sdma_chan(chan);
  864. struct sdma_engine *sdma = sdmac->sdma;
  865. int num_periods = buf_len / period_len;
  866. int channel = sdmac->channel;
  867. int ret, i = 0, buf = 0;
  868. dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
  869. if (sdmac->status == DMA_IN_PROGRESS)
  870. return NULL;
  871. sdmac->status = DMA_IN_PROGRESS;
  872. sdmac->flags |= IMX_DMA_SG_LOOP;
  873. sdmac->direction = direction;
  874. ret = sdma_load_context(sdmac);
  875. if (ret)
  876. goto err_out;
  877. if (num_periods > NUM_BD) {
  878. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  879. channel, num_periods, NUM_BD);
  880. goto err_out;
  881. }
  882. if (period_len > 0xffff) {
  883. dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
  884. channel, period_len, 0xffff);
  885. goto err_out;
  886. }
  887. while (buf < buf_len) {
  888. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  889. int param;
  890. bd->buffer_addr = dma_addr;
  891. bd->mode.count = period_len;
  892. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  893. goto err_out;
  894. if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
  895. bd->mode.command = 0;
  896. else
  897. bd->mode.command = sdmac->word_size;
  898. param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
  899. if (i + 1 == num_periods)
  900. param |= BD_WRAP;
  901. dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
  902. i, period_len, dma_addr,
  903. param & BD_WRAP ? "wrap" : "",
  904. param & BD_INTR ? " intr" : "");
  905. bd->mode.status = param;
  906. dma_addr += period_len;
  907. buf += period_len;
  908. i++;
  909. }
  910. sdmac->num_bd = num_periods;
  911. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  912. return &sdmac->desc;
  913. err_out:
  914. sdmac->status = DMA_ERROR;
  915. return NULL;
  916. }
  917. static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  918. unsigned long arg)
  919. {
  920. struct sdma_channel *sdmac = to_sdma_chan(chan);
  921. struct dma_slave_config *dmaengine_cfg = (void *)arg;
  922. switch (cmd) {
  923. case DMA_TERMINATE_ALL:
  924. sdma_disable_channel(sdmac);
  925. return 0;
  926. case DMA_SLAVE_CONFIG:
  927. if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
  928. sdmac->per_address = dmaengine_cfg->src_addr;
  929. sdmac->watermark_level = dmaengine_cfg->src_maxburst *
  930. dmaengine_cfg->src_addr_width;
  931. sdmac->word_size = dmaengine_cfg->src_addr_width;
  932. } else {
  933. sdmac->per_address = dmaengine_cfg->dst_addr;
  934. sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
  935. dmaengine_cfg->dst_addr_width;
  936. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  937. }
  938. sdmac->direction = dmaengine_cfg->direction;
  939. return sdma_config_channel(sdmac);
  940. default:
  941. return -ENOSYS;
  942. }
  943. return -EINVAL;
  944. }
  945. static enum dma_status sdma_tx_status(struct dma_chan *chan,
  946. dma_cookie_t cookie,
  947. struct dma_tx_state *txstate)
  948. {
  949. struct sdma_channel *sdmac = to_sdma_chan(chan);
  950. dma_cookie_t last_used;
  951. last_used = chan->cookie;
  952. dma_set_tx_state(txstate, chan->completed_cookie, last_used,
  953. sdmac->chn_count - sdmac->chn_real_count);
  954. return sdmac->status;
  955. }
  956. static void sdma_issue_pending(struct dma_chan *chan)
  957. {
  958. struct sdma_channel *sdmac = to_sdma_chan(chan);
  959. struct sdma_engine *sdma = sdmac->sdma;
  960. if (sdmac->status == DMA_IN_PROGRESS)
  961. sdma_enable_channel(sdma, sdmac->channel);
  962. }
  963. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
  964. static void sdma_add_scripts(struct sdma_engine *sdma,
  965. const struct sdma_script_start_addrs *addr)
  966. {
  967. s32 *addr_arr = (u32 *)addr;
  968. s32 *saddr_arr = (u32 *)sdma->script_addrs;
  969. int i;
  970. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  971. if (addr_arr[i] > 0)
  972. saddr_arr[i] = addr_arr[i];
  973. }
  974. static void sdma_load_firmware(const struct firmware *fw, void *context)
  975. {
  976. struct sdma_engine *sdma = context;
  977. const struct sdma_firmware_header *header;
  978. const struct sdma_script_start_addrs *addr;
  979. unsigned short *ram_code;
  980. if (!fw) {
  981. dev_err(sdma->dev, "firmware not found\n");
  982. return;
  983. }
  984. if (fw->size < sizeof(*header))
  985. goto err_firmware;
  986. header = (struct sdma_firmware_header *)fw->data;
  987. if (header->magic != SDMA_FIRMWARE_MAGIC)
  988. goto err_firmware;
  989. if (header->ram_code_start + header->ram_code_size > fw->size)
  990. goto err_firmware;
  991. addr = (void *)header + header->script_addrs_start;
  992. ram_code = (void *)header + header->ram_code_start;
  993. clk_enable(sdma->clk_ipg);
  994. clk_enable(sdma->clk_ahb);
  995. /* download the RAM image for SDMA */
  996. sdma_load_script(sdma, ram_code,
  997. header->ram_code_size,
  998. addr->ram_code_start_addr);
  999. clk_disable(sdma->clk_ipg);
  1000. clk_disable(sdma->clk_ahb);
  1001. sdma_add_scripts(sdma, addr);
  1002. dev_info(sdma->dev, "loaded firmware %d.%d\n",
  1003. header->version_major,
  1004. header->version_minor);
  1005. err_firmware:
  1006. release_firmware(fw);
  1007. }
  1008. static int __init sdma_get_firmware(struct sdma_engine *sdma,
  1009. const char *fw_name)
  1010. {
  1011. int ret;
  1012. ret = request_firmware_nowait(THIS_MODULE,
  1013. FW_ACTION_HOTPLUG, fw_name, sdma->dev,
  1014. GFP_KERNEL, sdma, sdma_load_firmware);
  1015. return ret;
  1016. }
  1017. static int __init sdma_init(struct sdma_engine *sdma)
  1018. {
  1019. int i, ret;
  1020. dma_addr_t ccb_phys;
  1021. switch (sdma->devtype) {
  1022. case IMX31_SDMA:
  1023. sdma->num_events = 32;
  1024. break;
  1025. case IMX35_SDMA:
  1026. sdma->num_events = 48;
  1027. break;
  1028. default:
  1029. dev_err(sdma->dev, "Unknown sdma type %d. aborting\n",
  1030. sdma->devtype);
  1031. return -ENODEV;
  1032. }
  1033. clk_enable(sdma->clk_ipg);
  1034. clk_enable(sdma->clk_ahb);
  1035. /* Be sure SDMA has not started yet */
  1036. writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
  1037. sdma->channel_control = dma_alloc_coherent(NULL,
  1038. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
  1039. sizeof(struct sdma_context_data),
  1040. &ccb_phys, GFP_KERNEL);
  1041. if (!sdma->channel_control) {
  1042. ret = -ENOMEM;
  1043. goto err_dma_alloc;
  1044. }
  1045. sdma->context = (void *)sdma->channel_control +
  1046. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1047. sdma->context_phys = ccb_phys +
  1048. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1049. /* Zero-out the CCB structures array just allocated */
  1050. memset(sdma->channel_control, 0,
  1051. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
  1052. /* disable all channels */
  1053. for (i = 0; i < sdma->num_events; i++)
  1054. writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
  1055. /* All channels have priority 0 */
  1056. for (i = 0; i < MAX_DMA_CHANNELS; i++)
  1057. writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
  1058. ret = sdma_request_channel(&sdma->channel[0]);
  1059. if (ret)
  1060. goto err_dma_alloc;
  1061. sdma_config_ownership(&sdma->channel[0], false, true, false);
  1062. /* Set Command Channel (Channel Zero) */
  1063. writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
  1064. /* Set bits of CONFIG register but with static context switching */
  1065. /* FIXME: Check whether to set ACR bit depending on clock ratios */
  1066. writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
  1067. writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
  1068. /* Set bits of CONFIG register with given context switching mode */
  1069. writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
  1070. /* Initializes channel's priorities */
  1071. sdma_set_channel_priority(&sdma->channel[0], 7);
  1072. clk_disable(sdma->clk_ipg);
  1073. clk_disable(sdma->clk_ahb);
  1074. return 0;
  1075. err_dma_alloc:
  1076. clk_disable(sdma->clk_ipg);
  1077. clk_disable(sdma->clk_ahb);
  1078. dev_err(sdma->dev, "initialisation failed with %d\n", ret);
  1079. return ret;
  1080. }
  1081. static int __init sdma_probe(struct platform_device *pdev)
  1082. {
  1083. const struct of_device_id *of_id =
  1084. of_match_device(sdma_dt_ids, &pdev->dev);
  1085. struct device_node *np = pdev->dev.of_node;
  1086. const char *fw_name;
  1087. int ret;
  1088. int irq;
  1089. struct resource *iores;
  1090. struct sdma_platform_data *pdata = pdev->dev.platform_data;
  1091. int i;
  1092. struct sdma_engine *sdma;
  1093. s32 *saddr_arr;
  1094. sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
  1095. if (!sdma)
  1096. return -ENOMEM;
  1097. spin_lock_init(&sdma->channel_0_lock);
  1098. sdma->dev = &pdev->dev;
  1099. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1100. irq = platform_get_irq(pdev, 0);
  1101. if (!iores || irq < 0) {
  1102. ret = -EINVAL;
  1103. goto err_irq;
  1104. }
  1105. if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
  1106. ret = -EBUSY;
  1107. goto err_request_region;
  1108. }
  1109. sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1110. if (IS_ERR(sdma->clk_ipg)) {
  1111. ret = PTR_ERR(sdma->clk_ipg);
  1112. goto err_clk;
  1113. }
  1114. sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1115. if (IS_ERR(sdma->clk_ahb)) {
  1116. ret = PTR_ERR(sdma->clk_ahb);
  1117. goto err_clk;
  1118. }
  1119. clk_prepare(sdma->clk_ipg);
  1120. clk_prepare(sdma->clk_ahb);
  1121. sdma->regs = ioremap(iores->start, resource_size(iores));
  1122. if (!sdma->regs) {
  1123. ret = -ENOMEM;
  1124. goto err_ioremap;
  1125. }
  1126. ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
  1127. if (ret)
  1128. goto err_request_irq;
  1129. sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
  1130. if (!sdma->script_addrs) {
  1131. ret = -ENOMEM;
  1132. goto err_alloc;
  1133. }
  1134. /* initially no scripts available */
  1135. saddr_arr = (s32 *)sdma->script_addrs;
  1136. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  1137. saddr_arr[i] = -EINVAL;
  1138. if (of_id)
  1139. pdev->id_entry = of_id->data;
  1140. sdma->devtype = pdev->id_entry->driver_data;
  1141. dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
  1142. dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
  1143. INIT_LIST_HEAD(&sdma->dma_device.channels);
  1144. /* Initialize channel parameters */
  1145. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1146. struct sdma_channel *sdmac = &sdma->channel[i];
  1147. sdmac->sdma = sdma;
  1148. spin_lock_init(&sdmac->lock);
  1149. sdmac->chan.device = &sdma->dma_device;
  1150. dma_cookie_init(&sdmac->chan);
  1151. sdmac->channel = i;
  1152. tasklet_init(&sdmac->tasklet, sdma_tasklet,
  1153. (unsigned long) sdmac);
  1154. /*
  1155. * Add the channel to the DMAC list. Do not add channel 0 though
  1156. * because we need it internally in the SDMA driver. This also means
  1157. * that channel 0 in dmaengine counting matches sdma channel 1.
  1158. */
  1159. if (i)
  1160. list_add_tail(&sdmac->chan.device_node,
  1161. &sdma->dma_device.channels);
  1162. }
  1163. ret = sdma_init(sdma);
  1164. if (ret)
  1165. goto err_init;
  1166. if (pdata && pdata->script_addrs)
  1167. sdma_add_scripts(sdma, pdata->script_addrs);
  1168. if (pdata) {
  1169. ret = sdma_get_firmware(sdma, pdata->fw_name);
  1170. if (ret)
  1171. dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
  1172. } else {
  1173. /*
  1174. * Because that device tree does not encode ROM script address,
  1175. * the RAM script in firmware is mandatory for device tree
  1176. * probe, otherwise it fails.
  1177. */
  1178. ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
  1179. &fw_name);
  1180. if (ret)
  1181. dev_warn(&pdev->dev, "failed to get firmware name\n");
  1182. else {
  1183. ret = sdma_get_firmware(sdma, fw_name);
  1184. if (ret)
  1185. dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
  1186. }
  1187. }
  1188. sdma->dma_device.dev = &pdev->dev;
  1189. sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
  1190. sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
  1191. sdma->dma_device.device_tx_status = sdma_tx_status;
  1192. sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
  1193. sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
  1194. sdma->dma_device.device_control = sdma_control;
  1195. sdma->dma_device.device_issue_pending = sdma_issue_pending;
  1196. sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
  1197. dma_set_max_seg_size(sdma->dma_device.dev, 65535);
  1198. ret = dma_async_device_register(&sdma->dma_device);
  1199. if (ret) {
  1200. dev_err(&pdev->dev, "unable to register\n");
  1201. goto err_init;
  1202. }
  1203. dev_info(sdma->dev, "initialized\n");
  1204. return 0;
  1205. err_init:
  1206. kfree(sdma->script_addrs);
  1207. err_alloc:
  1208. free_irq(irq, sdma);
  1209. err_request_irq:
  1210. iounmap(sdma->regs);
  1211. err_ioremap:
  1212. err_clk:
  1213. release_mem_region(iores->start, resource_size(iores));
  1214. err_request_region:
  1215. err_irq:
  1216. kfree(sdma);
  1217. return ret;
  1218. }
  1219. static int __exit sdma_remove(struct platform_device *pdev)
  1220. {
  1221. return -EBUSY;
  1222. }
  1223. static struct platform_driver sdma_driver = {
  1224. .driver = {
  1225. .name = "imx-sdma",
  1226. .of_match_table = sdma_dt_ids,
  1227. },
  1228. .id_table = sdma_devtypes,
  1229. .remove = __exit_p(sdma_remove),
  1230. };
  1231. static int __init sdma_module_init(void)
  1232. {
  1233. return platform_driver_probe(&sdma_driver, sdma_probe);
  1234. }
  1235. module_init(sdma_module_init);
  1236. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  1237. MODULE_DESCRIPTION("i.MX SDMA driver");
  1238. MODULE_LICENSE("GPL");