imx-sdma.c 37 KB

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