imx-sdma.c 34 KB

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