imx-sdma.c 36 KB

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