imx-sdma.c 36 KB

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