imx-sdma.c 33 KB

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