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