imx-sdma.c 35 KB

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