imx-sdma.c 34 KB

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