imx-sdma.c 36 KB

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