imx-sdma.c 38 KB

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