imx-sdma.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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. enum sdma_devtype {
  287. IMX31_SDMA, /* runs on i.mx31 */
  288. IMX35_SDMA, /* runs on i.mx35 and later */
  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. enum sdma_devtype devtype;
  297. unsigned int num_events;
  298. struct sdma_context_data *context;
  299. dma_addr_t context_phys;
  300. struct dma_device dma_device;
  301. struct clk *clk_ipg;
  302. struct clk *clk_ahb;
  303. spinlock_t channel_0_lock;
  304. struct sdma_script_start_addrs *script_addrs;
  305. };
  306. static struct platform_device_id sdma_devtypes[] = {
  307. {
  308. .name = "imx31-sdma",
  309. .driver_data = IMX31_SDMA,
  310. }, {
  311. .name = "imx35-sdma",
  312. .driver_data = IMX35_SDMA,
  313. }, {
  314. /* sentinel */
  315. }
  316. };
  317. MODULE_DEVICE_TABLE(platform, sdma_devtypes);
  318. static const struct of_device_id sdma_dt_ids[] = {
  319. { .compatible = "fsl,imx31-sdma", .data = &sdma_devtypes[IMX31_SDMA], },
  320. { .compatible = "fsl,imx35-sdma", .data = &sdma_devtypes[IMX35_SDMA], },
  321. { /* sentinel */ }
  322. };
  323. MODULE_DEVICE_TABLE(of, sdma_dt_ids);
  324. #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
  325. #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
  326. #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
  327. #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
  328. static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
  329. {
  330. u32 chnenbl0 = (sdma->devtype == IMX31_SDMA ? SDMA_CHNENBL0_IMX31 :
  331. SDMA_CHNENBL0_IMX35);
  332. return chnenbl0 + event * 4;
  333. }
  334. static int sdma_config_ownership(struct sdma_channel *sdmac,
  335. bool event_override, bool mcu_override, bool dsp_override)
  336. {
  337. struct sdma_engine *sdma = sdmac->sdma;
  338. int channel = sdmac->channel;
  339. unsigned long evt, mcu, dsp;
  340. if (event_override && mcu_override && dsp_override)
  341. return -EINVAL;
  342. evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
  343. mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
  344. dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
  345. if (dsp_override)
  346. __clear_bit(channel, &dsp);
  347. else
  348. __set_bit(channel, &dsp);
  349. if (event_override)
  350. __clear_bit(channel, &evt);
  351. else
  352. __set_bit(channel, &evt);
  353. if (mcu_override)
  354. __clear_bit(channel, &mcu);
  355. else
  356. __set_bit(channel, &mcu);
  357. writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
  358. writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
  359. writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
  360. return 0;
  361. }
  362. static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
  363. {
  364. writel(BIT(channel), sdma->regs + SDMA_H_START);
  365. }
  366. /*
  367. * sdma_run_channel0 - run a channel and wait till it's done
  368. */
  369. static int sdma_run_channel0(struct sdma_engine *sdma)
  370. {
  371. int ret;
  372. unsigned long timeout = 500;
  373. sdma_enable_channel(sdma, 0);
  374. while (!(ret = readl_relaxed(sdma->regs + SDMA_H_INTR) & 1)) {
  375. if (timeout-- <= 0)
  376. break;
  377. udelay(1);
  378. }
  379. if (ret) {
  380. /* Clear the interrupt status */
  381. writel_relaxed(ret, sdma->regs + SDMA_H_INTR);
  382. } else {
  383. dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
  384. }
  385. return ret ? 0 : -ETIMEDOUT;
  386. }
  387. static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
  388. u32 address)
  389. {
  390. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  391. void *buf_virt;
  392. dma_addr_t buf_phys;
  393. int ret;
  394. unsigned long flags;
  395. buf_virt = dma_alloc_coherent(NULL,
  396. size,
  397. &buf_phys, GFP_KERNEL);
  398. if (!buf_virt) {
  399. return -ENOMEM;
  400. }
  401. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  402. bd0->mode.command = C0_SETPM;
  403. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  404. bd0->mode.count = size / 2;
  405. bd0->buffer_addr = buf_phys;
  406. bd0->ext_buffer_addr = address;
  407. memcpy(buf_virt, buf, size);
  408. ret = sdma_run_channel0(sdma);
  409. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  410. dma_free_coherent(NULL, size, buf_virt, buf_phys);
  411. return ret;
  412. }
  413. static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
  414. {
  415. struct sdma_engine *sdma = sdmac->sdma;
  416. int channel = sdmac->channel;
  417. unsigned long val;
  418. u32 chnenbl = chnenbl_ofs(sdma, event);
  419. val = readl_relaxed(sdma->regs + chnenbl);
  420. __set_bit(channel, &val);
  421. writel_relaxed(val, sdma->regs + chnenbl);
  422. }
  423. static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
  424. {
  425. struct sdma_engine *sdma = sdmac->sdma;
  426. int channel = sdmac->channel;
  427. u32 chnenbl = chnenbl_ofs(sdma, event);
  428. unsigned long val;
  429. val = readl_relaxed(sdma->regs + chnenbl);
  430. __clear_bit(channel, &val);
  431. writel_relaxed(val, sdma->regs + chnenbl);
  432. }
  433. static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
  434. {
  435. struct sdma_buffer_descriptor *bd;
  436. /*
  437. * loop mode. Iterate over descriptors, re-setup them and
  438. * call callback function.
  439. */
  440. while (1) {
  441. bd = &sdmac->bd[sdmac->buf_tail];
  442. if (bd->mode.status & BD_DONE)
  443. break;
  444. if (bd->mode.status & BD_RROR)
  445. sdmac->status = DMA_ERROR;
  446. else
  447. sdmac->status = DMA_IN_PROGRESS;
  448. bd->mode.status |= BD_DONE;
  449. sdmac->buf_tail++;
  450. sdmac->buf_tail %= sdmac->num_bd;
  451. if (sdmac->desc.callback)
  452. sdmac->desc.callback(sdmac->desc.callback_param);
  453. }
  454. }
  455. static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
  456. {
  457. struct sdma_buffer_descriptor *bd;
  458. int i, error = 0;
  459. sdmac->chn_real_count = 0;
  460. /*
  461. * non loop mode. Iterate over all descriptors, collect
  462. * errors and call callback function
  463. */
  464. for (i = 0; i < sdmac->num_bd; i++) {
  465. bd = &sdmac->bd[i];
  466. if (bd->mode.status & (BD_DONE | BD_RROR))
  467. error = -EIO;
  468. sdmac->chn_real_count += bd->mode.count;
  469. }
  470. if (error)
  471. sdmac->status = DMA_ERROR;
  472. else
  473. sdmac->status = DMA_SUCCESS;
  474. dma_cookie_complete(&sdmac->desc);
  475. if (sdmac->desc.callback)
  476. sdmac->desc.callback(sdmac->desc.callback_param);
  477. }
  478. static void sdma_tasklet(unsigned long data)
  479. {
  480. struct sdma_channel *sdmac = (struct sdma_channel *) data;
  481. if (sdmac->flags & IMX_DMA_SG_LOOP)
  482. sdma_handle_channel_loop(sdmac);
  483. else
  484. mxc_sdma_handle_channel_normal(sdmac);
  485. }
  486. static irqreturn_t sdma_int_handler(int irq, void *dev_id)
  487. {
  488. struct sdma_engine *sdma = dev_id;
  489. unsigned long stat;
  490. stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
  491. /* not interested in channel 0 interrupts */
  492. stat &= ~1;
  493. writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
  494. while (stat) {
  495. int channel = fls(stat) - 1;
  496. struct sdma_channel *sdmac = &sdma->channel[channel];
  497. tasklet_schedule(&sdmac->tasklet);
  498. __clear_bit(channel, &stat);
  499. }
  500. return IRQ_HANDLED;
  501. }
  502. /*
  503. * sets the pc of SDMA script according to the peripheral type
  504. */
  505. static void sdma_get_pc(struct sdma_channel *sdmac,
  506. enum sdma_peripheral_type peripheral_type)
  507. {
  508. struct sdma_engine *sdma = sdmac->sdma;
  509. int per_2_emi = 0, emi_2_per = 0;
  510. /*
  511. * These are needed once we start to support transfers between
  512. * two peripherals or memory-to-memory transfers
  513. */
  514. int per_2_per = 0, emi_2_emi = 0;
  515. sdmac->pc_from_device = 0;
  516. sdmac->pc_to_device = 0;
  517. switch (peripheral_type) {
  518. case IMX_DMATYPE_MEMORY:
  519. emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
  520. break;
  521. case IMX_DMATYPE_DSP:
  522. emi_2_per = sdma->script_addrs->bp_2_ap_addr;
  523. per_2_emi = sdma->script_addrs->ap_2_bp_addr;
  524. break;
  525. case IMX_DMATYPE_FIRI:
  526. per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
  527. emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
  528. break;
  529. case IMX_DMATYPE_UART:
  530. per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
  531. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  532. break;
  533. case IMX_DMATYPE_UART_SP:
  534. per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
  535. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  536. break;
  537. case IMX_DMATYPE_ATA:
  538. per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
  539. emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
  540. break;
  541. case IMX_DMATYPE_CSPI:
  542. case IMX_DMATYPE_EXT:
  543. case IMX_DMATYPE_SSI:
  544. per_2_emi = sdma->script_addrs->app_2_mcu_addr;
  545. emi_2_per = sdma->script_addrs->mcu_2_app_addr;
  546. break;
  547. case IMX_DMATYPE_SSI_SP:
  548. case IMX_DMATYPE_MMC:
  549. case IMX_DMATYPE_SDHC:
  550. case IMX_DMATYPE_CSPI_SP:
  551. case IMX_DMATYPE_ESAI:
  552. case IMX_DMATYPE_MSHC_SP:
  553. per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
  554. emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
  555. break;
  556. case IMX_DMATYPE_ASRC:
  557. per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
  558. emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
  559. per_2_per = sdma->script_addrs->per_2_per_addr;
  560. break;
  561. case IMX_DMATYPE_MSHC:
  562. per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
  563. emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
  564. break;
  565. case IMX_DMATYPE_CCM:
  566. per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
  567. break;
  568. case IMX_DMATYPE_SPDIF:
  569. per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
  570. emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
  571. break;
  572. case IMX_DMATYPE_IPU_MEMORY:
  573. emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
  574. break;
  575. default:
  576. break;
  577. }
  578. sdmac->pc_from_device = per_2_emi;
  579. sdmac->pc_to_device = emi_2_per;
  580. }
  581. static int sdma_load_context(struct sdma_channel *sdmac)
  582. {
  583. struct sdma_engine *sdma = sdmac->sdma;
  584. int channel = sdmac->channel;
  585. int load_address;
  586. struct sdma_context_data *context = sdma->context;
  587. struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
  588. int ret;
  589. unsigned long flags;
  590. if (sdmac->direction == DMA_DEV_TO_MEM) {
  591. load_address = sdmac->pc_from_device;
  592. } else {
  593. load_address = sdmac->pc_to_device;
  594. }
  595. if (load_address < 0)
  596. return load_address;
  597. dev_dbg(sdma->dev, "load_address = %d\n", load_address);
  598. dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
  599. dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
  600. dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
  601. dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
  602. dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
  603. spin_lock_irqsave(&sdma->channel_0_lock, flags);
  604. memset(context, 0, sizeof(*context));
  605. context->channel_state.pc = load_address;
  606. /* Send by context the event mask,base address for peripheral
  607. * and watermark level
  608. */
  609. context->gReg[0] = sdmac->event_mask[1];
  610. context->gReg[1] = sdmac->event_mask[0];
  611. context->gReg[2] = sdmac->per_addr;
  612. context->gReg[6] = sdmac->shp_addr;
  613. context->gReg[7] = sdmac->watermark_level;
  614. bd0->mode.command = C0_SETDM;
  615. bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
  616. bd0->mode.count = sizeof(*context) / 4;
  617. bd0->buffer_addr = sdma->context_phys;
  618. bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
  619. ret = sdma_run_channel0(sdma);
  620. spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
  621. return ret;
  622. }
  623. static void sdma_disable_channel(struct sdma_channel *sdmac)
  624. {
  625. struct sdma_engine *sdma = sdmac->sdma;
  626. int channel = sdmac->channel;
  627. writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
  628. sdmac->status = DMA_ERROR;
  629. }
  630. static int sdma_config_channel(struct sdma_channel *sdmac)
  631. {
  632. int ret;
  633. sdma_disable_channel(sdmac);
  634. sdmac->event_mask[0] = 0;
  635. sdmac->event_mask[1] = 0;
  636. sdmac->shp_addr = 0;
  637. sdmac->per_addr = 0;
  638. if (sdmac->event_id0) {
  639. if (sdmac->event_id0 >= sdmac->sdma->num_events)
  640. return -EINVAL;
  641. sdma_event_enable(sdmac, sdmac->event_id0);
  642. }
  643. switch (sdmac->peripheral_type) {
  644. case IMX_DMATYPE_DSP:
  645. sdma_config_ownership(sdmac, false, true, true);
  646. break;
  647. case IMX_DMATYPE_MEMORY:
  648. sdma_config_ownership(sdmac, false, true, false);
  649. break;
  650. default:
  651. sdma_config_ownership(sdmac, true, true, false);
  652. break;
  653. }
  654. sdma_get_pc(sdmac, sdmac->peripheral_type);
  655. if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
  656. (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
  657. /* Handle multiple event channels differently */
  658. if (sdmac->event_id1) {
  659. sdmac->event_mask[1] = BIT(sdmac->event_id1 % 32);
  660. if (sdmac->event_id1 > 31)
  661. __set_bit(31, &sdmac->watermark_level);
  662. sdmac->event_mask[0] = BIT(sdmac->event_id0 % 32);
  663. if (sdmac->event_id0 > 31)
  664. __set_bit(30, &sdmac->watermark_level);
  665. } else {
  666. __set_bit(sdmac->event_id0, sdmac->event_mask);
  667. }
  668. /* Watermark Level */
  669. sdmac->watermark_level |= sdmac->watermark_level;
  670. /* Address */
  671. sdmac->shp_addr = sdmac->per_address;
  672. } else {
  673. sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
  674. }
  675. ret = sdma_load_context(sdmac);
  676. return ret;
  677. }
  678. static int sdma_set_channel_priority(struct sdma_channel *sdmac,
  679. unsigned int priority)
  680. {
  681. struct sdma_engine *sdma = sdmac->sdma;
  682. int channel = sdmac->channel;
  683. if (priority < MXC_SDMA_MIN_PRIORITY
  684. || priority > MXC_SDMA_MAX_PRIORITY) {
  685. return -EINVAL;
  686. }
  687. writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
  688. return 0;
  689. }
  690. static int sdma_request_channel(struct sdma_channel *sdmac)
  691. {
  692. struct sdma_engine *sdma = sdmac->sdma;
  693. int channel = sdmac->channel;
  694. int ret = -EBUSY;
  695. sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);
  696. if (!sdmac->bd) {
  697. ret = -ENOMEM;
  698. goto out;
  699. }
  700. memset(sdmac->bd, 0, PAGE_SIZE);
  701. sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
  702. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  703. sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
  704. return 0;
  705. out:
  706. return ret;
  707. }
  708. static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
  709. {
  710. return container_of(chan, struct sdma_channel, chan);
  711. }
  712. static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
  713. {
  714. unsigned long flags;
  715. struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
  716. dma_cookie_t cookie;
  717. spin_lock_irqsave(&sdmac->lock, flags);
  718. cookie = dma_cookie_assign(tx);
  719. spin_unlock_irqrestore(&sdmac->lock, flags);
  720. return cookie;
  721. }
  722. static int sdma_alloc_chan_resources(struct dma_chan *chan)
  723. {
  724. struct sdma_channel *sdmac = to_sdma_chan(chan);
  725. struct imx_dma_data *data = chan->private;
  726. int prio, ret;
  727. if (!data)
  728. return -EINVAL;
  729. switch (data->priority) {
  730. case DMA_PRIO_HIGH:
  731. prio = 3;
  732. break;
  733. case DMA_PRIO_MEDIUM:
  734. prio = 2;
  735. break;
  736. case DMA_PRIO_LOW:
  737. default:
  738. prio = 1;
  739. break;
  740. }
  741. sdmac->peripheral_type = data->peripheral_type;
  742. sdmac->event_id0 = data->dma_request;
  743. clk_enable(sdmac->sdma->clk_ipg);
  744. clk_enable(sdmac->sdma->clk_ahb);
  745. ret = sdma_request_channel(sdmac);
  746. if (ret)
  747. return ret;
  748. ret = sdma_set_channel_priority(sdmac, prio);
  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_ipg);
  771. clk_disable(sdma->clk_ahb);
  772. }
  773. static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
  774. struct dma_chan *chan, struct scatterlist *sgl,
  775. unsigned int sg_len, enum dma_transfer_direction direction,
  776. unsigned long flags, void *context)
  777. {
  778. struct sdma_channel *sdmac = to_sdma_chan(chan);
  779. struct sdma_engine *sdma = sdmac->sdma;
  780. int ret, i, count;
  781. int channel = sdmac->channel;
  782. struct scatterlist *sg;
  783. if (sdmac->status == DMA_IN_PROGRESS)
  784. return NULL;
  785. sdmac->status = DMA_IN_PROGRESS;
  786. sdmac->flags = 0;
  787. sdmac->buf_tail = 0;
  788. dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
  789. sg_len, channel);
  790. sdmac->direction = direction;
  791. ret = sdma_load_context(sdmac);
  792. if (ret)
  793. goto err_out;
  794. if (sg_len > NUM_BD) {
  795. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  796. channel, sg_len, NUM_BD);
  797. ret = -EINVAL;
  798. goto err_out;
  799. }
  800. sdmac->chn_count = 0;
  801. for_each_sg(sgl, sg, sg_len, i) {
  802. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  803. int param;
  804. bd->buffer_addr = sg->dma_address;
  805. count = sg_dma_len(sg);
  806. if (count > 0xffff) {
  807. dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
  808. channel, count, 0xffff);
  809. ret = -EINVAL;
  810. goto err_out;
  811. }
  812. bd->mode.count = count;
  813. sdmac->chn_count += count;
  814. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
  815. ret = -EINVAL;
  816. goto err_out;
  817. }
  818. switch (sdmac->word_size) {
  819. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  820. bd->mode.command = 0;
  821. if (count & 3 || sg->dma_address & 3)
  822. return NULL;
  823. break;
  824. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  825. bd->mode.command = 2;
  826. if (count & 1 || sg->dma_address & 1)
  827. return NULL;
  828. break;
  829. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  830. bd->mode.command = 1;
  831. break;
  832. default:
  833. return NULL;
  834. }
  835. param = BD_DONE | BD_EXTD | BD_CONT;
  836. if (i + 1 == sg_len) {
  837. param |= BD_INTR;
  838. param |= BD_LAST;
  839. param &= ~BD_CONT;
  840. }
  841. dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
  842. i, count, sg->dma_address,
  843. param & BD_WRAP ? "wrap" : "",
  844. param & BD_INTR ? " intr" : "");
  845. bd->mode.status = param;
  846. }
  847. sdmac->num_bd = sg_len;
  848. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  849. return &sdmac->desc;
  850. err_out:
  851. sdmac->status = DMA_ERROR;
  852. return NULL;
  853. }
  854. static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
  855. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  856. size_t period_len, enum dma_transfer_direction direction,
  857. unsigned long flags, void *context)
  858. {
  859. struct sdma_channel *sdmac = to_sdma_chan(chan);
  860. struct sdma_engine *sdma = sdmac->sdma;
  861. int num_periods = buf_len / period_len;
  862. int channel = sdmac->channel;
  863. int ret, i = 0, buf = 0;
  864. dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
  865. if (sdmac->status == DMA_IN_PROGRESS)
  866. return NULL;
  867. sdmac->status = DMA_IN_PROGRESS;
  868. sdmac->buf_tail = 0;
  869. sdmac->flags |= IMX_DMA_SG_LOOP;
  870. sdmac->direction = direction;
  871. ret = sdma_load_context(sdmac);
  872. if (ret)
  873. goto err_out;
  874. if (num_periods > NUM_BD) {
  875. dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
  876. channel, num_periods, NUM_BD);
  877. goto err_out;
  878. }
  879. if (period_len > 0xffff) {
  880. dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
  881. channel, period_len, 0xffff);
  882. goto err_out;
  883. }
  884. while (buf < buf_len) {
  885. struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
  886. int param;
  887. bd->buffer_addr = dma_addr;
  888. bd->mode.count = period_len;
  889. if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
  890. goto err_out;
  891. if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
  892. bd->mode.command = 0;
  893. else
  894. bd->mode.command = sdmac->word_size;
  895. param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
  896. if (i + 1 == num_periods)
  897. param |= BD_WRAP;
  898. dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",
  899. i, period_len, dma_addr,
  900. param & BD_WRAP ? "wrap" : "",
  901. param & BD_INTR ? " intr" : "");
  902. bd->mode.status = param;
  903. dma_addr += period_len;
  904. buf += period_len;
  905. i++;
  906. }
  907. sdmac->num_bd = num_periods;
  908. sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
  909. return &sdmac->desc;
  910. err_out:
  911. sdmac->status = DMA_ERROR;
  912. return NULL;
  913. }
  914. static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  915. unsigned long arg)
  916. {
  917. struct sdma_channel *sdmac = to_sdma_chan(chan);
  918. struct dma_slave_config *dmaengine_cfg = (void *)arg;
  919. switch (cmd) {
  920. case DMA_TERMINATE_ALL:
  921. sdma_disable_channel(sdmac);
  922. return 0;
  923. case DMA_SLAVE_CONFIG:
  924. if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
  925. sdmac->per_address = dmaengine_cfg->src_addr;
  926. sdmac->watermark_level = dmaengine_cfg->src_maxburst *
  927. dmaengine_cfg->src_addr_width;
  928. sdmac->word_size = dmaengine_cfg->src_addr_width;
  929. } else {
  930. sdmac->per_address = dmaengine_cfg->dst_addr;
  931. sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
  932. dmaengine_cfg->dst_addr_width;
  933. sdmac->word_size = dmaengine_cfg->dst_addr_width;
  934. }
  935. sdmac->direction = dmaengine_cfg->direction;
  936. return sdma_config_channel(sdmac);
  937. default:
  938. return -ENOSYS;
  939. }
  940. return -EINVAL;
  941. }
  942. static enum dma_status sdma_tx_status(struct dma_chan *chan,
  943. dma_cookie_t cookie,
  944. struct dma_tx_state *txstate)
  945. {
  946. struct sdma_channel *sdmac = to_sdma_chan(chan);
  947. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  948. sdmac->chn_count - sdmac->chn_real_count);
  949. return sdmac->status;
  950. }
  951. static void sdma_issue_pending(struct dma_chan *chan)
  952. {
  953. struct sdma_channel *sdmac = to_sdma_chan(chan);
  954. struct sdma_engine *sdma = sdmac->sdma;
  955. if (sdmac->status == DMA_IN_PROGRESS)
  956. sdma_enable_channel(sdma, sdmac->channel);
  957. }
  958. #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
  959. static void sdma_add_scripts(struct sdma_engine *sdma,
  960. const struct sdma_script_start_addrs *addr)
  961. {
  962. s32 *addr_arr = (u32 *)addr;
  963. s32 *saddr_arr = (u32 *)sdma->script_addrs;
  964. int i;
  965. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  966. if (addr_arr[i] > 0)
  967. saddr_arr[i] = addr_arr[i];
  968. }
  969. static void sdma_load_firmware(const struct firmware *fw, void *context)
  970. {
  971. struct sdma_engine *sdma = context;
  972. const struct sdma_firmware_header *header;
  973. const struct sdma_script_start_addrs *addr;
  974. unsigned short *ram_code;
  975. if (!fw) {
  976. dev_err(sdma->dev, "firmware not found\n");
  977. return;
  978. }
  979. if (fw->size < sizeof(*header))
  980. goto err_firmware;
  981. header = (struct sdma_firmware_header *)fw->data;
  982. if (header->magic != SDMA_FIRMWARE_MAGIC)
  983. goto err_firmware;
  984. if (header->ram_code_start + header->ram_code_size > fw->size)
  985. goto err_firmware;
  986. addr = (void *)header + header->script_addrs_start;
  987. ram_code = (void *)header + header->ram_code_start;
  988. clk_enable(sdma->clk_ipg);
  989. clk_enable(sdma->clk_ahb);
  990. /* download the RAM image for SDMA */
  991. sdma_load_script(sdma, ram_code,
  992. header->ram_code_size,
  993. addr->ram_code_start_addr);
  994. clk_disable(sdma->clk_ipg);
  995. clk_disable(sdma->clk_ahb);
  996. sdma_add_scripts(sdma, addr);
  997. dev_info(sdma->dev, "loaded firmware %d.%d\n",
  998. header->version_major,
  999. header->version_minor);
  1000. err_firmware:
  1001. release_firmware(fw);
  1002. }
  1003. static int __init sdma_get_firmware(struct sdma_engine *sdma,
  1004. const char *fw_name)
  1005. {
  1006. int ret;
  1007. ret = request_firmware_nowait(THIS_MODULE,
  1008. FW_ACTION_HOTPLUG, fw_name, sdma->dev,
  1009. GFP_KERNEL, sdma, sdma_load_firmware);
  1010. return ret;
  1011. }
  1012. static int __init sdma_init(struct sdma_engine *sdma)
  1013. {
  1014. int i, ret;
  1015. dma_addr_t ccb_phys;
  1016. switch (sdma->devtype) {
  1017. case IMX31_SDMA:
  1018. sdma->num_events = 32;
  1019. break;
  1020. case IMX35_SDMA:
  1021. sdma->num_events = 48;
  1022. break;
  1023. default:
  1024. dev_err(sdma->dev, "Unknown sdma type %d. aborting\n",
  1025. sdma->devtype);
  1026. return -ENODEV;
  1027. }
  1028. clk_enable(sdma->clk_ipg);
  1029. clk_enable(sdma->clk_ahb);
  1030. /* Be sure SDMA has not started yet */
  1031. writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
  1032. sdma->channel_control = dma_alloc_coherent(NULL,
  1033. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
  1034. sizeof(struct sdma_context_data),
  1035. &ccb_phys, GFP_KERNEL);
  1036. if (!sdma->channel_control) {
  1037. ret = -ENOMEM;
  1038. goto err_dma_alloc;
  1039. }
  1040. sdma->context = (void *)sdma->channel_control +
  1041. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1042. sdma->context_phys = ccb_phys +
  1043. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
  1044. /* Zero-out the CCB structures array just allocated */
  1045. memset(sdma->channel_control, 0,
  1046. MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
  1047. /* disable all channels */
  1048. for (i = 0; i < sdma->num_events; i++)
  1049. writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
  1050. /* All channels have priority 0 */
  1051. for (i = 0; i < MAX_DMA_CHANNELS; i++)
  1052. writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
  1053. ret = sdma_request_channel(&sdma->channel[0]);
  1054. if (ret)
  1055. goto err_dma_alloc;
  1056. sdma_config_ownership(&sdma->channel[0], false, true, false);
  1057. /* Set Command Channel (Channel Zero) */
  1058. writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
  1059. /* Set bits of CONFIG register but with static context switching */
  1060. /* FIXME: Check whether to set ACR bit depending on clock ratios */
  1061. writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
  1062. writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
  1063. /* Set bits of CONFIG register with given context switching mode */
  1064. writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
  1065. /* Initializes channel's priorities */
  1066. sdma_set_channel_priority(&sdma->channel[0], 7);
  1067. clk_disable(sdma->clk_ipg);
  1068. clk_disable(sdma->clk_ahb);
  1069. return 0;
  1070. err_dma_alloc:
  1071. clk_disable(sdma->clk_ipg);
  1072. clk_disable(sdma->clk_ahb);
  1073. dev_err(sdma->dev, "initialisation failed with %d\n", ret);
  1074. return ret;
  1075. }
  1076. static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
  1077. {
  1078. struct imx_dma_data *data = fn_param;
  1079. if (!imx_dma_is_general_purpose(chan))
  1080. return false;
  1081. chan->private = data;
  1082. return true;
  1083. }
  1084. static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
  1085. struct of_dma *ofdma)
  1086. {
  1087. struct sdma_engine *sdma = ofdma->of_dma_data;
  1088. dma_cap_mask_t mask = sdma->dma_device.cap_mask;
  1089. struct imx_dma_data data;
  1090. if (dma_spec->args_count != 3)
  1091. return NULL;
  1092. data.dma_request = dma_spec->args[0];
  1093. data.peripheral_type = dma_spec->args[1];
  1094. data.priority = dma_spec->args[2];
  1095. return dma_request_channel(mask, sdma_filter_fn, &data);
  1096. }
  1097. static int __init sdma_probe(struct platform_device *pdev)
  1098. {
  1099. const struct of_device_id *of_id =
  1100. of_match_device(sdma_dt_ids, &pdev->dev);
  1101. struct device_node *np = pdev->dev.of_node;
  1102. const char *fw_name;
  1103. int ret;
  1104. int irq;
  1105. struct resource *iores;
  1106. struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1107. int i;
  1108. struct sdma_engine *sdma;
  1109. s32 *saddr_arr;
  1110. sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);
  1111. if (!sdma)
  1112. return -ENOMEM;
  1113. spin_lock_init(&sdma->channel_0_lock);
  1114. sdma->dev = &pdev->dev;
  1115. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1116. irq = platform_get_irq(pdev, 0);
  1117. if (!iores || irq < 0) {
  1118. ret = -EINVAL;
  1119. goto err_irq;
  1120. }
  1121. if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {
  1122. ret = -EBUSY;
  1123. goto err_request_region;
  1124. }
  1125. sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1126. if (IS_ERR(sdma->clk_ipg)) {
  1127. ret = PTR_ERR(sdma->clk_ipg);
  1128. goto err_clk;
  1129. }
  1130. sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  1131. if (IS_ERR(sdma->clk_ahb)) {
  1132. ret = PTR_ERR(sdma->clk_ahb);
  1133. goto err_clk;
  1134. }
  1135. clk_prepare(sdma->clk_ipg);
  1136. clk_prepare(sdma->clk_ahb);
  1137. sdma->regs = ioremap(iores->start, resource_size(iores));
  1138. if (!sdma->regs) {
  1139. ret = -ENOMEM;
  1140. goto err_ioremap;
  1141. }
  1142. ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);
  1143. if (ret)
  1144. goto err_request_irq;
  1145. sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
  1146. if (!sdma->script_addrs) {
  1147. ret = -ENOMEM;
  1148. goto err_alloc;
  1149. }
  1150. /* initially no scripts available */
  1151. saddr_arr = (s32 *)sdma->script_addrs;
  1152. for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
  1153. saddr_arr[i] = -EINVAL;
  1154. if (of_id)
  1155. pdev->id_entry = of_id->data;
  1156. sdma->devtype = pdev->id_entry->driver_data;
  1157. dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
  1158. dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
  1159. INIT_LIST_HEAD(&sdma->dma_device.channels);
  1160. /* Initialize channel parameters */
  1161. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  1162. struct sdma_channel *sdmac = &sdma->channel[i];
  1163. sdmac->sdma = sdma;
  1164. spin_lock_init(&sdmac->lock);
  1165. sdmac->chan.device = &sdma->dma_device;
  1166. dma_cookie_init(&sdmac->chan);
  1167. sdmac->channel = i;
  1168. tasklet_init(&sdmac->tasklet, sdma_tasklet,
  1169. (unsigned long) sdmac);
  1170. /*
  1171. * Add the channel to the DMAC list. Do not add channel 0 though
  1172. * because we need it internally in the SDMA driver. This also means
  1173. * that channel 0 in dmaengine counting matches sdma channel 1.
  1174. */
  1175. if (i)
  1176. list_add_tail(&sdmac->chan.device_node,
  1177. &sdma->dma_device.channels);
  1178. }
  1179. ret = sdma_init(sdma);
  1180. if (ret)
  1181. goto err_init;
  1182. if (pdata && pdata->script_addrs)
  1183. sdma_add_scripts(sdma, pdata->script_addrs);
  1184. if (pdata) {
  1185. ret = sdma_get_firmware(sdma, pdata->fw_name);
  1186. if (ret)
  1187. dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
  1188. } else {
  1189. /*
  1190. * Because that device tree does not encode ROM script address,
  1191. * the RAM script in firmware is mandatory for device tree
  1192. * probe, otherwise it fails.
  1193. */
  1194. ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
  1195. &fw_name);
  1196. if (ret)
  1197. dev_warn(&pdev->dev, "failed to get firmware name\n");
  1198. else {
  1199. ret = sdma_get_firmware(sdma, fw_name);
  1200. if (ret)
  1201. dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
  1202. }
  1203. }
  1204. sdma->dma_device.dev = &pdev->dev;
  1205. sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
  1206. sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
  1207. sdma->dma_device.device_tx_status = sdma_tx_status;
  1208. sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
  1209. sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
  1210. sdma->dma_device.device_control = sdma_control;
  1211. sdma->dma_device.device_issue_pending = sdma_issue_pending;
  1212. sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
  1213. dma_set_max_seg_size(sdma->dma_device.dev, 65535);
  1214. ret = dma_async_device_register(&sdma->dma_device);
  1215. if (ret) {
  1216. dev_err(&pdev->dev, "unable to register\n");
  1217. goto err_init;
  1218. }
  1219. if (np) {
  1220. ret = of_dma_controller_register(np, sdma_xlate, sdma);
  1221. if (ret) {
  1222. dev_err(&pdev->dev, "failed to register controller\n");
  1223. goto err_register;
  1224. }
  1225. }
  1226. dev_info(sdma->dev, "initialized\n");
  1227. return 0;
  1228. err_register:
  1229. dma_async_device_unregister(&sdma->dma_device);
  1230. err_init:
  1231. kfree(sdma->script_addrs);
  1232. err_alloc:
  1233. free_irq(irq, sdma);
  1234. err_request_irq:
  1235. iounmap(sdma->regs);
  1236. err_ioremap:
  1237. err_clk:
  1238. release_mem_region(iores->start, resource_size(iores));
  1239. err_request_region:
  1240. err_irq:
  1241. kfree(sdma);
  1242. return ret;
  1243. }
  1244. static int sdma_remove(struct platform_device *pdev)
  1245. {
  1246. return -EBUSY;
  1247. }
  1248. static struct platform_driver sdma_driver = {
  1249. .driver = {
  1250. .name = "imx-sdma",
  1251. .of_match_table = sdma_dt_ids,
  1252. },
  1253. .id_table = sdma_devtypes,
  1254. .remove = sdma_remove,
  1255. };
  1256. static int __init sdma_module_init(void)
  1257. {
  1258. return platform_driver_probe(&sdma_driver, sdma_probe);
  1259. }
  1260. module_init(sdma_module_init);
  1261. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  1262. MODULE_DESCRIPTION("i.MX SDMA driver");
  1263. MODULE_LICENSE("GPL");