imx-sdma.c 35 KB

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