imx-sdma.c 35 KB

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