s3c24xx-dma.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /*
  2. * S3C24XX DMA handling
  3. *
  4. * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>
  5. *
  6. * based on amba-pl08x.c
  7. *
  8. * Copyright (c) 2006 ARM Ltd.
  9. * Copyright (c) 2010 ST-Ericsson SA
  10. *
  11. * Author: Peter Pearse <peter.pearse@arm.com>
  12. * Author: Linus Walleij <linus.walleij@stericsson.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. * The DMA controllers in S3C24XX SoCs have a varying number of DMA signals
  20. * that can be routed to any of the 4 to 8 hardware-channels.
  21. *
  22. * Therefore on these DMA controllers the number of channels
  23. * and the number of incoming DMA signals are two totally different things.
  24. * It is usually not possible to theoretically handle all physical signals,
  25. * so a multiplexing scheme with possible denial of use is necessary.
  26. *
  27. * Open items:
  28. * - bursts
  29. */
  30. #include <linux/platform_device.h>
  31. #include <linux/types.h>
  32. #include <linux/dmaengine.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/clk.h>
  36. #include <linux/module.h>
  37. #include <linux/slab.h>
  38. #include <linux/platform_data/dma-s3c24xx.h>
  39. #include "dmaengine.h"
  40. #include "virt-dma.h"
  41. #define MAX_DMA_CHANNELS 8
  42. #define S3C24XX_DISRC 0x00
  43. #define S3C24XX_DISRCC 0x04
  44. #define S3C24XX_DISRCC_INC_INCREMENT 0
  45. #define S3C24XX_DISRCC_INC_FIXED BIT(0)
  46. #define S3C24XX_DISRCC_LOC_AHB 0
  47. #define S3C24XX_DISRCC_LOC_APB BIT(1)
  48. #define S3C24XX_DIDST 0x08
  49. #define S3C24XX_DIDSTC 0x0c
  50. #define S3C24XX_DIDSTC_INC_INCREMENT 0
  51. #define S3C24XX_DIDSTC_INC_FIXED BIT(0)
  52. #define S3C24XX_DIDSTC_LOC_AHB 0
  53. #define S3C24XX_DIDSTC_LOC_APB BIT(1)
  54. #define S3C24XX_DIDSTC_INT_TC0 0
  55. #define S3C24XX_DIDSTC_INT_RELOAD BIT(2)
  56. #define S3C24XX_DCON 0x10
  57. #define S3C24XX_DCON_TC_MASK 0xfffff
  58. #define S3C24XX_DCON_DSZ_BYTE (0 << 20)
  59. #define S3C24XX_DCON_DSZ_HALFWORD (1 << 20)
  60. #define S3C24XX_DCON_DSZ_WORD (2 << 20)
  61. #define S3C24XX_DCON_DSZ_MASK (3 << 20)
  62. #define S3C24XX_DCON_DSZ_SHIFT 20
  63. #define S3C24XX_DCON_AUTORELOAD 0
  64. #define S3C24XX_DCON_NORELOAD BIT(22)
  65. #define S3C24XX_DCON_HWTRIG BIT(23)
  66. #define S3C24XX_DCON_HWSRC_SHIFT 24
  67. #define S3C24XX_DCON_SERV_SINGLE 0
  68. #define S3C24XX_DCON_SERV_WHOLE BIT(27)
  69. #define S3C24XX_DCON_TSZ_UNIT 0
  70. #define S3C24XX_DCON_TSZ_BURST4 BIT(28)
  71. #define S3C24XX_DCON_INT BIT(29)
  72. #define S3C24XX_DCON_SYNC_PCLK 0
  73. #define S3C24XX_DCON_SYNC_HCLK BIT(30)
  74. #define S3C24XX_DCON_DEMAND 0
  75. #define S3C24XX_DCON_HANDSHAKE BIT(31)
  76. #define S3C24XX_DSTAT 0x14
  77. #define S3C24XX_DSTAT_STAT_BUSY BIT(20)
  78. #define S3C24XX_DSTAT_CURRTC_MASK 0xfffff
  79. #define S3C24XX_DMASKTRIG 0x20
  80. #define S3C24XX_DMASKTRIG_SWTRIG BIT(0)
  81. #define S3C24XX_DMASKTRIG_ON BIT(1)
  82. #define S3C24XX_DMASKTRIG_STOP BIT(2)
  83. #define S3C24XX_DMAREQSEL 0x24
  84. #define S3C24XX_DMAREQSEL_HW BIT(0)
  85. /*
  86. * S3C2410, S3C2440 and S3C2442 SoCs cannot select any physical channel
  87. * for a DMA source. Instead only specific channels are valid.
  88. * All of these SoCs have 4 physical channels and the number of request
  89. * source bits is 3. Additionally we also need 1 bit to mark the channel
  90. * as valid.
  91. * Therefore we separate the chansel element of the channel data into 4
  92. * parts of 4 bits each, to hold the information if the channel is valid
  93. * and the hw request source to use.
  94. *
  95. * Example:
  96. * SDI is valid on channels 0, 2 and 3 - with varying hw request sources.
  97. * For it the chansel field would look like
  98. *
  99. * ((BIT(3) | 1) << 3 * 4) | // channel 3, with request source 1
  100. * ((BIT(3) | 2) << 2 * 4) | // channel 2, with request source 2
  101. * ((BIT(3) | 2) << 0 * 4) // channel 0, with request source 2
  102. */
  103. #define S3C24XX_CHANSEL_WIDTH 4
  104. #define S3C24XX_CHANSEL_VALID BIT(3)
  105. #define S3C24XX_CHANSEL_REQ_MASK 7
  106. /*
  107. * struct soc_data - vendor-specific config parameters for individual SoCs
  108. * @stride: spacing between the registers of each channel
  109. * @has_reqsel: does the controller use the newer requestselection mechanism
  110. * @has_clocks: are controllable dma-clocks present
  111. */
  112. struct soc_data {
  113. int stride;
  114. bool has_reqsel;
  115. bool has_clocks;
  116. };
  117. /*
  118. * enum s3c24xx_dma_chan_state - holds the virtual channel states
  119. * @S3C24XX_DMA_CHAN_IDLE: the channel is idle
  120. * @S3C24XX_DMA_CHAN_RUNNING: the channel has allocated a physical transport
  121. * channel and is running a transfer on it
  122. * @S3C24XX_DMA_CHAN_WAITING: the channel is waiting for a physical transport
  123. * channel to become available (only pertains to memcpy channels)
  124. */
  125. enum s3c24xx_dma_chan_state {
  126. S3C24XX_DMA_CHAN_IDLE,
  127. S3C24XX_DMA_CHAN_RUNNING,
  128. S3C24XX_DMA_CHAN_WAITING,
  129. };
  130. /*
  131. * struct s3c24xx_sg - structure containing data per sg
  132. * @src_addr: src address of sg
  133. * @dst_addr: dst address of sg
  134. * @len: transfer len in bytes
  135. * @node: node for txd's dsg_list
  136. */
  137. struct s3c24xx_sg {
  138. dma_addr_t src_addr;
  139. dma_addr_t dst_addr;
  140. size_t len;
  141. struct list_head node;
  142. };
  143. /*
  144. * struct s3c24xx_txd - wrapper for struct dma_async_tx_descriptor
  145. * @vd: virtual DMA descriptor
  146. * @dsg_list: list of children sg's
  147. * @at: sg currently being transfered
  148. * @width: transfer width
  149. * @disrcc: value for source control register
  150. * @didstc: value for destination control register
  151. * @dcon: base value for dcon register
  152. * @cyclic: indicate cyclic transfer
  153. */
  154. struct s3c24xx_txd {
  155. struct virt_dma_desc vd;
  156. struct list_head dsg_list;
  157. struct list_head *at;
  158. u8 width;
  159. u32 disrcc;
  160. u32 didstc;
  161. u32 dcon;
  162. bool cyclic;
  163. };
  164. struct s3c24xx_dma_chan;
  165. /*
  166. * struct s3c24xx_dma_phy - holder for the physical channels
  167. * @id: physical index to this channel
  168. * @valid: does the channel have all required elements
  169. * @base: virtual memory base (remapped) for the this channel
  170. * @irq: interrupt for this channel
  171. * @clk: clock for this channel
  172. * @lock: a lock to use when altering an instance of this struct
  173. * @serving: virtual channel currently being served by this physicalchannel
  174. * @host: a pointer to the host (internal use)
  175. */
  176. struct s3c24xx_dma_phy {
  177. unsigned int id;
  178. bool valid;
  179. void __iomem *base;
  180. int irq;
  181. struct clk *clk;
  182. spinlock_t lock;
  183. struct s3c24xx_dma_chan *serving;
  184. struct s3c24xx_dma_engine *host;
  185. };
  186. /*
  187. * struct s3c24xx_dma_chan - this structure wraps a DMA ENGINE channel
  188. * @id: the id of the channel
  189. * @name: name of the channel
  190. * @vc: wrappped virtual channel
  191. * @phy: the physical channel utilized by this channel, if there is one
  192. * @runtime_addr: address for RX/TX according to the runtime config
  193. * @at: active transaction on this channel
  194. * @lock: a lock for this channel data
  195. * @host: a pointer to the host (internal use)
  196. * @state: whether the channel is idle, running etc
  197. * @slave: whether this channel is a device (slave) or for memcpy
  198. */
  199. struct s3c24xx_dma_chan {
  200. int id;
  201. const char *name;
  202. struct virt_dma_chan vc;
  203. struct s3c24xx_dma_phy *phy;
  204. struct dma_slave_config cfg;
  205. struct s3c24xx_txd *at;
  206. struct s3c24xx_dma_engine *host;
  207. enum s3c24xx_dma_chan_state state;
  208. bool slave;
  209. };
  210. /*
  211. * struct s3c24xx_dma_engine - the local state holder for the S3C24XX
  212. * @pdev: the corresponding platform device
  213. * @pdata: platform data passed in from the platform/machine
  214. * @base: virtual memory base (remapped)
  215. * @slave: slave engine for this instance
  216. * @memcpy: memcpy engine for this instance
  217. * @phy_chans: array of data for the physical channels
  218. */
  219. struct s3c24xx_dma_engine {
  220. struct platform_device *pdev;
  221. const struct s3c24xx_dma_platdata *pdata;
  222. struct soc_data *sdata;
  223. void __iomem *base;
  224. struct dma_device slave;
  225. struct dma_device memcpy;
  226. struct s3c24xx_dma_phy *phy_chans;
  227. };
  228. /*
  229. * Physical channel handling
  230. */
  231. /*
  232. * Check whether a certain channel is busy or not.
  233. */
  234. static int s3c24xx_dma_phy_busy(struct s3c24xx_dma_phy *phy)
  235. {
  236. unsigned int val = readl(phy->base + S3C24XX_DSTAT);
  237. return val & S3C24XX_DSTAT_STAT_BUSY;
  238. }
  239. static bool s3c24xx_dma_phy_valid(struct s3c24xx_dma_chan *s3cchan,
  240. struct s3c24xx_dma_phy *phy)
  241. {
  242. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  243. const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
  244. struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
  245. int phyvalid;
  246. /* every phy is valid for memcopy channels */
  247. if (!s3cchan->slave)
  248. return true;
  249. /* On newer variants all phys can be used for all virtual channels */
  250. if (s3cdma->sdata->has_reqsel)
  251. return true;
  252. phyvalid = (cdata->chansel >> (phy->id * S3C24XX_CHANSEL_WIDTH));
  253. return (phyvalid & S3C24XX_CHANSEL_VALID) ? true : false;
  254. }
  255. /*
  256. * Allocate a physical channel for a virtual channel
  257. *
  258. * Try to locate a physical channel to be used for this transfer. If all
  259. * are taken return NULL and the requester will have to cope by using
  260. * some fallback PIO mode or retrying later.
  261. */
  262. static
  263. struct s3c24xx_dma_phy *s3c24xx_dma_get_phy(struct s3c24xx_dma_chan *s3cchan)
  264. {
  265. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  266. const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
  267. struct s3c24xx_dma_channel *cdata;
  268. struct s3c24xx_dma_phy *phy = NULL;
  269. unsigned long flags;
  270. int i;
  271. int ret;
  272. if (s3cchan->slave)
  273. cdata = &pdata->channels[s3cchan->id];
  274. for (i = 0; i < s3cdma->pdata->num_phy_channels; i++) {
  275. phy = &s3cdma->phy_chans[i];
  276. if (!phy->valid)
  277. continue;
  278. if (!s3c24xx_dma_phy_valid(s3cchan, phy))
  279. continue;
  280. spin_lock_irqsave(&phy->lock, flags);
  281. if (!phy->serving) {
  282. phy->serving = s3cchan;
  283. spin_unlock_irqrestore(&phy->lock, flags);
  284. break;
  285. }
  286. spin_unlock_irqrestore(&phy->lock, flags);
  287. }
  288. /* No physical channel available, cope with it */
  289. if (i == s3cdma->pdata->num_phy_channels) {
  290. dev_warn(&s3cdma->pdev->dev, "no phy channel available\n");
  291. return NULL;
  292. }
  293. /* start the phy clock */
  294. if (s3cdma->sdata->has_clocks) {
  295. ret = clk_enable(phy->clk);
  296. if (ret) {
  297. dev_err(&s3cdma->pdev->dev, "could not enable clock for channel %d, err %d\n",
  298. phy->id, ret);
  299. phy->serving = NULL;
  300. return NULL;
  301. }
  302. }
  303. return phy;
  304. }
  305. /*
  306. * Mark the physical channel as free.
  307. *
  308. * This drops the link between the physical and virtual channel.
  309. */
  310. static inline void s3c24xx_dma_put_phy(struct s3c24xx_dma_phy *phy)
  311. {
  312. struct s3c24xx_dma_engine *s3cdma = phy->host;
  313. if (s3cdma->sdata->has_clocks)
  314. clk_disable(phy->clk);
  315. phy->serving = NULL;
  316. }
  317. /*
  318. * Stops the channel by writing the stop bit.
  319. * This should not be used for an on-going transfer, but as a method of
  320. * shutting down a channel (eg, when it's no longer used) or terminating a
  321. * transfer.
  322. */
  323. static void s3c24xx_dma_terminate_phy(struct s3c24xx_dma_phy *phy)
  324. {
  325. writel(S3C24XX_DMASKTRIG_STOP, phy->base + S3C24XX_DMASKTRIG);
  326. }
  327. /*
  328. * Virtual channel handling
  329. */
  330. static inline
  331. struct s3c24xx_dma_chan *to_s3c24xx_dma_chan(struct dma_chan *chan)
  332. {
  333. return container_of(chan, struct s3c24xx_dma_chan, vc.chan);
  334. }
  335. static u32 s3c24xx_dma_getbytes_chan(struct s3c24xx_dma_chan *s3cchan)
  336. {
  337. struct s3c24xx_dma_phy *phy = s3cchan->phy;
  338. struct s3c24xx_txd *txd = s3cchan->at;
  339. u32 tc = readl(phy->base + S3C24XX_DSTAT) & S3C24XX_DSTAT_CURRTC_MASK;
  340. return tc * txd->width;
  341. }
  342. static int s3c24xx_dma_set_runtime_config(struct s3c24xx_dma_chan *s3cchan,
  343. struct dma_slave_config *config)
  344. {
  345. if (!s3cchan->slave)
  346. return -EINVAL;
  347. /* Reject definitely invalid configurations */
  348. if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  349. config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  350. return -EINVAL;
  351. s3cchan->cfg = *config;
  352. return 0;
  353. }
  354. /*
  355. * Transfer handling
  356. */
  357. static inline
  358. struct s3c24xx_txd *to_s3c24xx_txd(struct dma_async_tx_descriptor *tx)
  359. {
  360. return container_of(tx, struct s3c24xx_txd, vd.tx);
  361. }
  362. static struct s3c24xx_txd *s3c24xx_dma_get_txd(void)
  363. {
  364. struct s3c24xx_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  365. if (txd) {
  366. INIT_LIST_HEAD(&txd->dsg_list);
  367. txd->dcon = S3C24XX_DCON_INT | S3C24XX_DCON_NORELOAD;
  368. }
  369. return txd;
  370. }
  371. static void s3c24xx_dma_free_txd(struct s3c24xx_txd *txd)
  372. {
  373. struct s3c24xx_sg *dsg, *_dsg;
  374. list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
  375. list_del(&dsg->node);
  376. kfree(dsg);
  377. }
  378. kfree(txd);
  379. }
  380. static void s3c24xx_dma_start_next_sg(struct s3c24xx_dma_chan *s3cchan,
  381. struct s3c24xx_txd *txd)
  382. {
  383. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  384. struct s3c24xx_dma_phy *phy = s3cchan->phy;
  385. const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
  386. struct s3c24xx_sg *dsg = list_entry(txd->at, struct s3c24xx_sg, node);
  387. u32 dcon = txd->dcon;
  388. u32 val;
  389. /* transfer-size and -count from len and width */
  390. switch (txd->width) {
  391. case 1:
  392. dcon |= S3C24XX_DCON_DSZ_BYTE | dsg->len;
  393. break;
  394. case 2:
  395. dcon |= S3C24XX_DCON_DSZ_HALFWORD | (dsg->len / 2);
  396. break;
  397. case 4:
  398. dcon |= S3C24XX_DCON_DSZ_WORD | (dsg->len / 4);
  399. break;
  400. }
  401. if (s3cchan->slave) {
  402. struct s3c24xx_dma_channel *cdata =
  403. &pdata->channels[s3cchan->id];
  404. if (s3cdma->sdata->has_reqsel) {
  405. writel_relaxed((cdata->chansel << 1) |
  406. S3C24XX_DMAREQSEL_HW,
  407. phy->base + S3C24XX_DMAREQSEL);
  408. } else {
  409. int csel = cdata->chansel >> (phy->id *
  410. S3C24XX_CHANSEL_WIDTH);
  411. csel &= S3C24XX_CHANSEL_REQ_MASK;
  412. dcon |= csel << S3C24XX_DCON_HWSRC_SHIFT;
  413. dcon |= S3C24XX_DCON_HWTRIG;
  414. }
  415. } else {
  416. if (s3cdma->sdata->has_reqsel)
  417. writel_relaxed(0, phy->base + S3C24XX_DMAREQSEL);
  418. }
  419. writel_relaxed(dsg->src_addr, phy->base + S3C24XX_DISRC);
  420. writel_relaxed(txd->disrcc, phy->base + S3C24XX_DISRCC);
  421. writel_relaxed(dsg->dst_addr, phy->base + S3C24XX_DIDST);
  422. writel_relaxed(txd->didstc, phy->base + S3C24XX_DIDSTC);
  423. writel_relaxed(dcon, phy->base + S3C24XX_DCON);
  424. val = readl_relaxed(phy->base + S3C24XX_DMASKTRIG);
  425. val &= ~S3C24XX_DMASKTRIG_STOP;
  426. val |= S3C24XX_DMASKTRIG_ON;
  427. /* trigger the dma operation for memcpy transfers */
  428. if (!s3cchan->slave)
  429. val |= S3C24XX_DMASKTRIG_SWTRIG;
  430. writel(val, phy->base + S3C24XX_DMASKTRIG);
  431. }
  432. /*
  433. * Set the initial DMA register values and start first sg.
  434. */
  435. static void s3c24xx_dma_start_next_txd(struct s3c24xx_dma_chan *s3cchan)
  436. {
  437. struct s3c24xx_dma_phy *phy = s3cchan->phy;
  438. struct virt_dma_desc *vd = vchan_next_desc(&s3cchan->vc);
  439. struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);
  440. list_del(&txd->vd.node);
  441. s3cchan->at = txd;
  442. /* Wait for channel inactive */
  443. while (s3c24xx_dma_phy_busy(phy))
  444. cpu_relax();
  445. /* point to the first element of the sg list */
  446. txd->at = txd->dsg_list.next;
  447. s3c24xx_dma_start_next_sg(s3cchan, txd);
  448. }
  449. static void s3c24xx_dma_free_txd_list(struct s3c24xx_dma_engine *s3cdma,
  450. struct s3c24xx_dma_chan *s3cchan)
  451. {
  452. LIST_HEAD(head);
  453. vchan_get_all_descriptors(&s3cchan->vc, &head);
  454. vchan_dma_desc_free_list(&s3cchan->vc, &head);
  455. }
  456. /*
  457. * Try to allocate a physical channel. When successful, assign it to
  458. * this virtual channel, and initiate the next descriptor. The
  459. * virtual channel lock must be held at this point.
  460. */
  461. static void s3c24xx_dma_phy_alloc_and_start(struct s3c24xx_dma_chan *s3cchan)
  462. {
  463. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  464. struct s3c24xx_dma_phy *phy;
  465. phy = s3c24xx_dma_get_phy(s3cchan);
  466. if (!phy) {
  467. dev_dbg(&s3cdma->pdev->dev, "no physical channel available for xfer on %s\n",
  468. s3cchan->name);
  469. s3cchan->state = S3C24XX_DMA_CHAN_WAITING;
  470. return;
  471. }
  472. dev_dbg(&s3cdma->pdev->dev, "allocated physical channel %d for xfer on %s\n",
  473. phy->id, s3cchan->name);
  474. s3cchan->phy = phy;
  475. s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;
  476. s3c24xx_dma_start_next_txd(s3cchan);
  477. }
  478. static void s3c24xx_dma_phy_reassign_start(struct s3c24xx_dma_phy *phy,
  479. struct s3c24xx_dma_chan *s3cchan)
  480. {
  481. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  482. dev_dbg(&s3cdma->pdev->dev, "reassigned physical channel %d for xfer on %s\n",
  483. phy->id, s3cchan->name);
  484. /*
  485. * We do this without taking the lock; we're really only concerned
  486. * about whether this pointer is NULL or not, and we're guaranteed
  487. * that this will only be called when it _already_ is non-NULL.
  488. */
  489. phy->serving = s3cchan;
  490. s3cchan->phy = phy;
  491. s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;
  492. s3c24xx_dma_start_next_txd(s3cchan);
  493. }
  494. /*
  495. * Free a physical DMA channel, potentially reallocating it to another
  496. * virtual channel if we have any pending.
  497. */
  498. static void s3c24xx_dma_phy_free(struct s3c24xx_dma_chan *s3cchan)
  499. {
  500. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  501. struct s3c24xx_dma_chan *p, *next;
  502. retry:
  503. next = NULL;
  504. /* Find a waiting virtual channel for the next transfer. */
  505. list_for_each_entry(p, &s3cdma->memcpy.channels, vc.chan.device_node)
  506. if (p->state == S3C24XX_DMA_CHAN_WAITING) {
  507. next = p;
  508. break;
  509. }
  510. if (!next) {
  511. list_for_each_entry(p, &s3cdma->slave.channels,
  512. vc.chan.device_node)
  513. if (p->state == S3C24XX_DMA_CHAN_WAITING &&
  514. s3c24xx_dma_phy_valid(p, s3cchan->phy)) {
  515. next = p;
  516. break;
  517. }
  518. }
  519. /* Ensure that the physical channel is stopped */
  520. s3c24xx_dma_terminate_phy(s3cchan->phy);
  521. if (next) {
  522. bool success;
  523. /*
  524. * Eww. We know this isn't going to deadlock
  525. * but lockdep probably doesn't.
  526. */
  527. spin_lock(&next->vc.lock);
  528. /* Re-check the state now that we have the lock */
  529. success = next->state == S3C24XX_DMA_CHAN_WAITING;
  530. if (success)
  531. s3c24xx_dma_phy_reassign_start(s3cchan->phy, next);
  532. spin_unlock(&next->vc.lock);
  533. /* If the state changed, try to find another channel */
  534. if (!success)
  535. goto retry;
  536. } else {
  537. /* No more jobs, so free up the physical channel */
  538. s3c24xx_dma_put_phy(s3cchan->phy);
  539. }
  540. s3cchan->phy = NULL;
  541. s3cchan->state = S3C24XX_DMA_CHAN_IDLE;
  542. }
  543. static void s3c24xx_dma_desc_free(struct virt_dma_desc *vd)
  544. {
  545. struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);
  546. struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(vd->tx.chan);
  547. if (!s3cchan->slave)
  548. dma_descriptor_unmap(&vd->tx);
  549. s3c24xx_dma_free_txd(txd);
  550. }
  551. static irqreturn_t s3c24xx_dma_irq(int irq, void *data)
  552. {
  553. struct s3c24xx_dma_phy *phy = data;
  554. struct s3c24xx_dma_chan *s3cchan = phy->serving;
  555. struct s3c24xx_txd *txd;
  556. dev_dbg(&phy->host->pdev->dev, "interrupt on channel %d\n", phy->id);
  557. /*
  558. * Interrupts happen to notify the completion of a transfer and the
  559. * channel should have moved into its stop state already on its own.
  560. * Therefore interrupts on channels not bound to a virtual channel
  561. * should never happen. Nevertheless send a terminate command to the
  562. * channel if the unlikely case happens.
  563. */
  564. if (unlikely(!s3cchan)) {
  565. dev_err(&phy->host->pdev->dev, "interrupt on unused channel %d\n",
  566. phy->id);
  567. s3c24xx_dma_terminate_phy(phy);
  568. return IRQ_HANDLED;
  569. }
  570. spin_lock(&s3cchan->vc.lock);
  571. txd = s3cchan->at;
  572. if (txd) {
  573. /* when more sg's are in this txd, start the next one */
  574. if (!list_is_last(txd->at, &txd->dsg_list)) {
  575. txd->at = txd->at->next;
  576. if (txd->cyclic)
  577. vchan_cyclic_callback(&txd->vd);
  578. s3c24xx_dma_start_next_sg(s3cchan, txd);
  579. } else if (!txd->cyclic) {
  580. s3cchan->at = NULL;
  581. vchan_cookie_complete(&txd->vd);
  582. /*
  583. * And start the next descriptor (if any),
  584. * otherwise free this channel.
  585. */
  586. if (vchan_next_desc(&s3cchan->vc))
  587. s3c24xx_dma_start_next_txd(s3cchan);
  588. else
  589. s3c24xx_dma_phy_free(s3cchan);
  590. } else {
  591. vchan_cyclic_callback(&txd->vd);
  592. /* Cyclic: reset at beginning */
  593. txd->at = txd->dsg_list.next;
  594. s3c24xx_dma_start_next_sg(s3cchan, txd);
  595. }
  596. }
  597. spin_unlock(&s3cchan->vc.lock);
  598. return IRQ_HANDLED;
  599. }
  600. /*
  601. * The DMA ENGINE API
  602. */
  603. static int s3c24xx_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  604. unsigned long arg)
  605. {
  606. struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
  607. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  608. unsigned long flags;
  609. int ret = 0;
  610. spin_lock_irqsave(&s3cchan->vc.lock, flags);
  611. switch (cmd) {
  612. case DMA_SLAVE_CONFIG:
  613. ret = s3c24xx_dma_set_runtime_config(s3cchan,
  614. (struct dma_slave_config *)arg);
  615. break;
  616. case DMA_TERMINATE_ALL:
  617. if (!s3cchan->phy && !s3cchan->at) {
  618. dev_err(&s3cdma->pdev->dev, "trying to terminate already stopped channel %d\n",
  619. s3cchan->id);
  620. ret = -EINVAL;
  621. break;
  622. }
  623. s3cchan->state = S3C24XX_DMA_CHAN_IDLE;
  624. /* Mark physical channel as free */
  625. if (s3cchan->phy)
  626. s3c24xx_dma_phy_free(s3cchan);
  627. /* Dequeue current job */
  628. if (s3cchan->at) {
  629. s3c24xx_dma_desc_free(&s3cchan->at->vd);
  630. s3cchan->at = NULL;
  631. }
  632. /* Dequeue jobs not yet fired as well */
  633. s3c24xx_dma_free_txd_list(s3cdma, s3cchan);
  634. break;
  635. default:
  636. /* Unknown command */
  637. ret = -ENXIO;
  638. break;
  639. }
  640. spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
  641. return ret;
  642. }
  643. static int s3c24xx_dma_alloc_chan_resources(struct dma_chan *chan)
  644. {
  645. return 0;
  646. }
  647. static void s3c24xx_dma_free_chan_resources(struct dma_chan *chan)
  648. {
  649. /* Ensure all queued descriptors are freed */
  650. vchan_free_chan_resources(to_virt_chan(chan));
  651. }
  652. static enum dma_status s3c24xx_dma_tx_status(struct dma_chan *chan,
  653. dma_cookie_t cookie, struct dma_tx_state *txstate)
  654. {
  655. struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
  656. struct s3c24xx_txd *txd;
  657. struct s3c24xx_sg *dsg;
  658. struct virt_dma_desc *vd;
  659. unsigned long flags;
  660. enum dma_status ret;
  661. size_t bytes = 0;
  662. spin_lock_irqsave(&s3cchan->vc.lock, flags);
  663. ret = dma_cookie_status(chan, cookie, txstate);
  664. if (ret == DMA_COMPLETE) {
  665. spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
  666. return ret;
  667. }
  668. /*
  669. * There's no point calculating the residue if there's
  670. * no txstate to store the value.
  671. */
  672. if (!txstate) {
  673. spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
  674. return ret;
  675. }
  676. vd = vchan_find_desc(&s3cchan->vc, cookie);
  677. if (vd) {
  678. /* On the issued list, so hasn't been processed yet */
  679. txd = to_s3c24xx_txd(&vd->tx);
  680. list_for_each_entry(dsg, &txd->dsg_list, node)
  681. bytes += dsg->len;
  682. } else {
  683. /*
  684. * Currently running, so sum over the pending sg's and
  685. * the currently active one.
  686. */
  687. txd = s3cchan->at;
  688. dsg = list_entry(txd->at, struct s3c24xx_sg, node);
  689. list_for_each_entry_from(dsg, &txd->dsg_list, node)
  690. bytes += dsg->len;
  691. bytes += s3c24xx_dma_getbytes_chan(s3cchan);
  692. }
  693. spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
  694. /*
  695. * This cookie not complete yet
  696. * Get number of bytes left in the active transactions and queue
  697. */
  698. dma_set_residue(txstate, bytes);
  699. /* Whether waiting or running, we're in progress */
  700. return ret;
  701. }
  702. /*
  703. * Initialize a descriptor to be used by memcpy submit
  704. */
  705. static struct dma_async_tx_descriptor *s3c24xx_dma_prep_memcpy(
  706. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  707. size_t len, unsigned long flags)
  708. {
  709. struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
  710. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  711. struct s3c24xx_txd *txd;
  712. struct s3c24xx_sg *dsg;
  713. int src_mod, dest_mod;
  714. dev_dbg(&s3cdma->pdev->dev, "prepare memcpy of %d bytes from %s\n",
  715. len, s3cchan->name);
  716. if ((len & S3C24XX_DCON_TC_MASK) != len) {
  717. dev_err(&s3cdma->pdev->dev, "memcpy size %d to large\n", len);
  718. return NULL;
  719. }
  720. txd = s3c24xx_dma_get_txd();
  721. if (!txd)
  722. return NULL;
  723. dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
  724. if (!dsg) {
  725. s3c24xx_dma_free_txd(txd);
  726. return NULL;
  727. }
  728. list_add_tail(&dsg->node, &txd->dsg_list);
  729. dsg->src_addr = src;
  730. dsg->dst_addr = dest;
  731. dsg->len = len;
  732. /*
  733. * Determine a suitable transfer width.
  734. * The DMA controller cannot fetch/store information which is not
  735. * naturally aligned on the bus, i.e., a 4 byte fetch must start at
  736. * an address divisible by 4 - more generally addr % width must be 0.
  737. */
  738. src_mod = src % 4;
  739. dest_mod = dest % 4;
  740. switch (len % 4) {
  741. case 0:
  742. txd->width = (src_mod == 0 && dest_mod == 0) ? 4 : 1;
  743. break;
  744. case 2:
  745. txd->width = ((src_mod == 2 || src_mod == 0) &&
  746. (dest_mod == 2 || dest_mod == 0)) ? 2 : 1;
  747. break;
  748. default:
  749. txd->width = 1;
  750. break;
  751. }
  752. txd->disrcc = S3C24XX_DISRCC_LOC_AHB | S3C24XX_DISRCC_INC_INCREMENT;
  753. txd->didstc = S3C24XX_DIDSTC_LOC_AHB | S3C24XX_DIDSTC_INC_INCREMENT;
  754. txd->dcon |= S3C24XX_DCON_DEMAND | S3C24XX_DCON_SYNC_HCLK |
  755. S3C24XX_DCON_SERV_WHOLE;
  756. return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
  757. }
  758. static struct dma_async_tx_descriptor *s3c24xx_dma_prep_dma_cyclic(
  759. struct dma_chan *chan, dma_addr_t addr, size_t size, size_t period,
  760. enum dma_transfer_direction direction, unsigned long flags)
  761. {
  762. struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
  763. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  764. const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
  765. struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
  766. struct s3c24xx_txd *txd;
  767. struct s3c24xx_sg *dsg;
  768. unsigned sg_len;
  769. dma_addr_t slave_addr;
  770. u32 hwcfg = 0;
  771. int i;
  772. dev_dbg(&s3cdma->pdev->dev,
  773. "prepare cyclic transaction of %zu bytes with period %zu from %s\n",
  774. size, period, s3cchan->name);
  775. if (!is_slave_direction(direction)) {
  776. dev_err(&s3cdma->pdev->dev,
  777. "direction %d unsupported\n", direction);
  778. return NULL;
  779. }
  780. txd = s3c24xx_dma_get_txd();
  781. if (!txd)
  782. return NULL;
  783. txd->cyclic = 1;
  784. if (cdata->handshake)
  785. txd->dcon |= S3C24XX_DCON_HANDSHAKE;
  786. switch (cdata->bus) {
  787. case S3C24XX_DMA_APB:
  788. txd->dcon |= S3C24XX_DCON_SYNC_PCLK;
  789. hwcfg |= S3C24XX_DISRCC_LOC_APB;
  790. break;
  791. case S3C24XX_DMA_AHB:
  792. txd->dcon |= S3C24XX_DCON_SYNC_HCLK;
  793. hwcfg |= S3C24XX_DISRCC_LOC_AHB;
  794. break;
  795. }
  796. /*
  797. * Always assume our peripheral desintation is a fixed
  798. * address in memory.
  799. */
  800. hwcfg |= S3C24XX_DISRCC_INC_FIXED;
  801. /*
  802. * Individual dma operations are requested by the slave,
  803. * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).
  804. */
  805. txd->dcon |= S3C24XX_DCON_SERV_SINGLE;
  806. if (direction == DMA_MEM_TO_DEV) {
  807. txd->disrcc = S3C24XX_DISRCC_LOC_AHB |
  808. S3C24XX_DISRCC_INC_INCREMENT;
  809. txd->didstc = hwcfg;
  810. slave_addr = s3cchan->cfg.dst_addr;
  811. txd->width = s3cchan->cfg.dst_addr_width;
  812. } else {
  813. txd->disrcc = hwcfg;
  814. txd->didstc = S3C24XX_DIDSTC_LOC_AHB |
  815. S3C24XX_DIDSTC_INC_INCREMENT;
  816. slave_addr = s3cchan->cfg.src_addr;
  817. txd->width = s3cchan->cfg.src_addr_width;
  818. }
  819. sg_len = size / period;
  820. for (i = 0; i < sg_len; i++) {
  821. dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
  822. if (!dsg) {
  823. s3c24xx_dma_free_txd(txd);
  824. return NULL;
  825. }
  826. list_add_tail(&dsg->node, &txd->dsg_list);
  827. dsg->len = period;
  828. /* Check last period length */
  829. if (i == sg_len - 1)
  830. dsg->len = size - period * i;
  831. if (direction == DMA_MEM_TO_DEV) {
  832. dsg->src_addr = addr + period * i;
  833. dsg->dst_addr = slave_addr;
  834. } else { /* DMA_DEV_TO_MEM */
  835. dsg->src_addr = slave_addr;
  836. dsg->dst_addr = addr + period * i;
  837. }
  838. }
  839. return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
  840. }
  841. static struct dma_async_tx_descriptor *s3c24xx_dma_prep_slave_sg(
  842. struct dma_chan *chan, struct scatterlist *sgl,
  843. unsigned int sg_len, enum dma_transfer_direction direction,
  844. unsigned long flags, void *context)
  845. {
  846. struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
  847. struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
  848. const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
  849. struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
  850. struct s3c24xx_txd *txd;
  851. struct s3c24xx_sg *dsg;
  852. struct scatterlist *sg;
  853. dma_addr_t slave_addr;
  854. u32 hwcfg = 0;
  855. int tmp;
  856. dev_dbg(&s3cdma->pdev->dev, "prepare transaction of %d bytes from %s\n",
  857. sg_dma_len(sgl), s3cchan->name);
  858. txd = s3c24xx_dma_get_txd();
  859. if (!txd)
  860. return NULL;
  861. if (cdata->handshake)
  862. txd->dcon |= S3C24XX_DCON_HANDSHAKE;
  863. switch (cdata->bus) {
  864. case S3C24XX_DMA_APB:
  865. txd->dcon |= S3C24XX_DCON_SYNC_PCLK;
  866. hwcfg |= S3C24XX_DISRCC_LOC_APB;
  867. break;
  868. case S3C24XX_DMA_AHB:
  869. txd->dcon |= S3C24XX_DCON_SYNC_HCLK;
  870. hwcfg |= S3C24XX_DISRCC_LOC_AHB;
  871. break;
  872. }
  873. /*
  874. * Always assume our peripheral desintation is a fixed
  875. * address in memory.
  876. */
  877. hwcfg |= S3C24XX_DISRCC_INC_FIXED;
  878. /*
  879. * Individual dma operations are requested by the slave,
  880. * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).
  881. */
  882. txd->dcon |= S3C24XX_DCON_SERV_SINGLE;
  883. if (direction == DMA_MEM_TO_DEV) {
  884. txd->disrcc = S3C24XX_DISRCC_LOC_AHB |
  885. S3C24XX_DISRCC_INC_INCREMENT;
  886. txd->didstc = hwcfg;
  887. slave_addr = s3cchan->cfg.dst_addr;
  888. txd->width = s3cchan->cfg.dst_addr_width;
  889. } else if (direction == DMA_DEV_TO_MEM) {
  890. txd->disrcc = hwcfg;
  891. txd->didstc = S3C24XX_DIDSTC_LOC_AHB |
  892. S3C24XX_DIDSTC_INC_INCREMENT;
  893. slave_addr = s3cchan->cfg.src_addr;
  894. txd->width = s3cchan->cfg.src_addr_width;
  895. } else {
  896. s3c24xx_dma_free_txd(txd);
  897. dev_err(&s3cdma->pdev->dev,
  898. "direction %d unsupported\n", direction);
  899. return NULL;
  900. }
  901. for_each_sg(sgl, sg, sg_len, tmp) {
  902. dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
  903. if (!dsg) {
  904. s3c24xx_dma_free_txd(txd);
  905. return NULL;
  906. }
  907. list_add_tail(&dsg->node, &txd->dsg_list);
  908. dsg->len = sg_dma_len(sg);
  909. if (direction == DMA_MEM_TO_DEV) {
  910. dsg->src_addr = sg_dma_address(sg);
  911. dsg->dst_addr = slave_addr;
  912. } else { /* DMA_DEV_TO_MEM */
  913. dsg->src_addr = slave_addr;
  914. dsg->dst_addr = sg_dma_address(sg);
  915. }
  916. }
  917. return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
  918. }
  919. /*
  920. * Slave transactions callback to the slave device to allow
  921. * synchronization of slave DMA signals with the DMAC enable
  922. */
  923. static void s3c24xx_dma_issue_pending(struct dma_chan *chan)
  924. {
  925. struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
  926. unsigned long flags;
  927. spin_lock_irqsave(&s3cchan->vc.lock, flags);
  928. if (vchan_issue_pending(&s3cchan->vc)) {
  929. if (!s3cchan->phy && s3cchan->state != S3C24XX_DMA_CHAN_WAITING)
  930. s3c24xx_dma_phy_alloc_and_start(s3cchan);
  931. }
  932. spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
  933. }
  934. /*
  935. * Bringup and teardown
  936. */
  937. /*
  938. * Initialise the DMAC memcpy/slave channels.
  939. * Make a local wrapper to hold required data
  940. */
  941. static int s3c24xx_dma_init_virtual_channels(struct s3c24xx_dma_engine *s3cdma,
  942. struct dma_device *dmadev, unsigned int channels, bool slave)
  943. {
  944. struct s3c24xx_dma_chan *chan;
  945. int i;
  946. INIT_LIST_HEAD(&dmadev->channels);
  947. /*
  948. * Register as many many memcpy as we have physical channels,
  949. * we won't always be able to use all but the code will have
  950. * to cope with that situation.
  951. */
  952. for (i = 0; i < channels; i++) {
  953. chan = devm_kzalloc(dmadev->dev, sizeof(*chan), GFP_KERNEL);
  954. if (!chan) {
  955. dev_err(dmadev->dev,
  956. "%s no memory for channel\n", __func__);
  957. return -ENOMEM;
  958. }
  959. chan->id = i;
  960. chan->host = s3cdma;
  961. chan->state = S3C24XX_DMA_CHAN_IDLE;
  962. if (slave) {
  963. chan->slave = true;
  964. chan->name = kasprintf(GFP_KERNEL, "slave%d", i);
  965. if (!chan->name)
  966. return -ENOMEM;
  967. } else {
  968. chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
  969. if (!chan->name)
  970. return -ENOMEM;
  971. }
  972. dev_dbg(dmadev->dev,
  973. "initialize virtual channel \"%s\"\n",
  974. chan->name);
  975. chan->vc.desc_free = s3c24xx_dma_desc_free;
  976. vchan_init(&chan->vc, dmadev);
  977. }
  978. dev_info(dmadev->dev, "initialized %d virtual %s channels\n",
  979. i, slave ? "slave" : "memcpy");
  980. return i;
  981. }
  982. static void s3c24xx_dma_free_virtual_channels(struct dma_device *dmadev)
  983. {
  984. struct s3c24xx_dma_chan *chan = NULL;
  985. struct s3c24xx_dma_chan *next;
  986. list_for_each_entry_safe(chan,
  987. next, &dmadev->channels, vc.chan.device_node)
  988. list_del(&chan->vc.chan.device_node);
  989. }
  990. /* s3c2410, s3c2440 and s3c2442 have a 0x40 stride without separate clocks */
  991. static struct soc_data soc_s3c2410 = {
  992. .stride = 0x40,
  993. .has_reqsel = false,
  994. .has_clocks = false,
  995. };
  996. /* s3c2412 and s3c2413 have a 0x40 stride and dmareqsel mechanism */
  997. static struct soc_data soc_s3c2412 = {
  998. .stride = 0x40,
  999. .has_reqsel = true,
  1000. .has_clocks = true,
  1001. };
  1002. /* s3c2443 and following have a 0x100 stride and dmareqsel mechanism */
  1003. static struct soc_data soc_s3c2443 = {
  1004. .stride = 0x100,
  1005. .has_reqsel = true,
  1006. .has_clocks = true,
  1007. };
  1008. static struct platform_device_id s3c24xx_dma_driver_ids[] = {
  1009. {
  1010. .name = "s3c2410-dma",
  1011. .driver_data = (kernel_ulong_t)&soc_s3c2410,
  1012. }, {
  1013. .name = "s3c2412-dma",
  1014. .driver_data = (kernel_ulong_t)&soc_s3c2412,
  1015. }, {
  1016. .name = "s3c2443-dma",
  1017. .driver_data = (kernel_ulong_t)&soc_s3c2443,
  1018. },
  1019. { },
  1020. };
  1021. static struct soc_data *s3c24xx_dma_get_soc_data(struct platform_device *pdev)
  1022. {
  1023. return (struct soc_data *)
  1024. platform_get_device_id(pdev)->driver_data;
  1025. }
  1026. static int s3c24xx_dma_probe(struct platform_device *pdev)
  1027. {
  1028. const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);
  1029. struct s3c24xx_dma_engine *s3cdma;
  1030. struct soc_data *sdata;
  1031. struct resource *res;
  1032. int ret;
  1033. int i;
  1034. if (!pdata) {
  1035. dev_err(&pdev->dev, "platform data missing\n");
  1036. return -ENODEV;
  1037. }
  1038. /* Basic sanity check */
  1039. if (pdata->num_phy_channels > MAX_DMA_CHANNELS) {
  1040. dev_err(&pdev->dev, "to many dma channels %d, max %d\n",
  1041. pdata->num_phy_channels, MAX_DMA_CHANNELS);
  1042. return -EINVAL;
  1043. }
  1044. sdata = s3c24xx_dma_get_soc_data(pdev);
  1045. if (!sdata)
  1046. return -EINVAL;
  1047. s3cdma = devm_kzalloc(&pdev->dev, sizeof(*s3cdma), GFP_KERNEL);
  1048. if (!s3cdma)
  1049. return -ENOMEM;
  1050. s3cdma->pdev = pdev;
  1051. s3cdma->pdata = pdata;
  1052. s3cdma->sdata = sdata;
  1053. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1054. s3cdma->base = devm_ioremap_resource(&pdev->dev, res);
  1055. if (IS_ERR(s3cdma->base))
  1056. return PTR_ERR(s3cdma->base);
  1057. s3cdma->phy_chans = devm_kzalloc(&pdev->dev,
  1058. sizeof(struct s3c24xx_dma_phy) *
  1059. pdata->num_phy_channels,
  1060. GFP_KERNEL);
  1061. if (!s3cdma->phy_chans)
  1062. return -ENOMEM;
  1063. /* aquire irqs and clocks for all physical channels */
  1064. for (i = 0; i < pdata->num_phy_channels; i++) {
  1065. struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
  1066. char clk_name[6];
  1067. phy->id = i;
  1068. phy->base = s3cdma->base + (i * sdata->stride);
  1069. phy->host = s3cdma;
  1070. phy->irq = platform_get_irq(pdev, i);
  1071. if (phy->irq < 0) {
  1072. dev_err(&pdev->dev, "failed to get irq %d, err %d\n",
  1073. i, phy->irq);
  1074. continue;
  1075. }
  1076. ret = devm_request_irq(&pdev->dev, phy->irq, s3c24xx_dma_irq,
  1077. 0, pdev->name, phy);
  1078. if (ret) {
  1079. dev_err(&pdev->dev, "Unable to request irq for channel %d, error %d\n",
  1080. i, ret);
  1081. continue;
  1082. }
  1083. if (sdata->has_clocks) {
  1084. sprintf(clk_name, "dma.%d", i);
  1085. phy->clk = devm_clk_get(&pdev->dev, clk_name);
  1086. if (IS_ERR(phy->clk) && sdata->has_clocks) {
  1087. dev_err(&pdev->dev, "unable to aquire clock for channel %d, error %lu",
  1088. i, PTR_ERR(phy->clk));
  1089. continue;
  1090. }
  1091. ret = clk_prepare(phy->clk);
  1092. if (ret) {
  1093. dev_err(&pdev->dev, "clock for phy %d failed, error %d\n",
  1094. i, ret);
  1095. continue;
  1096. }
  1097. }
  1098. spin_lock_init(&phy->lock);
  1099. phy->valid = true;
  1100. dev_dbg(&pdev->dev, "physical channel %d is %s\n",
  1101. i, s3c24xx_dma_phy_busy(phy) ? "BUSY" : "FREE");
  1102. }
  1103. /* Initialize memcpy engine */
  1104. dma_cap_set(DMA_MEMCPY, s3cdma->memcpy.cap_mask);
  1105. dma_cap_set(DMA_PRIVATE, s3cdma->memcpy.cap_mask);
  1106. s3cdma->memcpy.dev = &pdev->dev;
  1107. s3cdma->memcpy.device_alloc_chan_resources =
  1108. s3c24xx_dma_alloc_chan_resources;
  1109. s3cdma->memcpy.device_free_chan_resources =
  1110. s3c24xx_dma_free_chan_resources;
  1111. s3cdma->memcpy.device_prep_dma_memcpy = s3c24xx_dma_prep_memcpy;
  1112. s3cdma->memcpy.device_tx_status = s3c24xx_dma_tx_status;
  1113. s3cdma->memcpy.device_issue_pending = s3c24xx_dma_issue_pending;
  1114. s3cdma->memcpy.device_control = s3c24xx_dma_control;
  1115. /* Initialize slave engine for SoC internal dedicated peripherals */
  1116. dma_cap_set(DMA_SLAVE, s3cdma->slave.cap_mask);
  1117. dma_cap_set(DMA_CYCLIC, s3cdma->slave.cap_mask);
  1118. dma_cap_set(DMA_PRIVATE, s3cdma->slave.cap_mask);
  1119. s3cdma->slave.dev = &pdev->dev;
  1120. s3cdma->slave.device_alloc_chan_resources =
  1121. s3c24xx_dma_alloc_chan_resources;
  1122. s3cdma->slave.device_free_chan_resources =
  1123. s3c24xx_dma_free_chan_resources;
  1124. s3cdma->slave.device_tx_status = s3c24xx_dma_tx_status;
  1125. s3cdma->slave.device_issue_pending = s3c24xx_dma_issue_pending;
  1126. s3cdma->slave.device_prep_slave_sg = s3c24xx_dma_prep_slave_sg;
  1127. s3cdma->slave.device_prep_dma_cyclic = s3c24xx_dma_prep_dma_cyclic;
  1128. s3cdma->slave.device_control = s3c24xx_dma_control;
  1129. /* Register as many memcpy channels as there are physical channels */
  1130. ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->memcpy,
  1131. pdata->num_phy_channels, false);
  1132. if (ret <= 0) {
  1133. dev_warn(&pdev->dev,
  1134. "%s failed to enumerate memcpy channels - %d\n",
  1135. __func__, ret);
  1136. goto err_memcpy;
  1137. }
  1138. /* Register slave channels */
  1139. ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->slave,
  1140. pdata->num_channels, true);
  1141. if (ret <= 0) {
  1142. dev_warn(&pdev->dev,
  1143. "%s failed to enumerate slave channels - %d\n",
  1144. __func__, ret);
  1145. goto err_slave;
  1146. }
  1147. ret = dma_async_device_register(&s3cdma->memcpy);
  1148. if (ret) {
  1149. dev_warn(&pdev->dev,
  1150. "%s failed to register memcpy as an async device - %d\n",
  1151. __func__, ret);
  1152. goto err_memcpy_reg;
  1153. }
  1154. ret = dma_async_device_register(&s3cdma->slave);
  1155. if (ret) {
  1156. dev_warn(&pdev->dev,
  1157. "%s failed to register slave as an async device - %d\n",
  1158. __func__, ret);
  1159. goto err_slave_reg;
  1160. }
  1161. platform_set_drvdata(pdev, s3cdma);
  1162. dev_info(&pdev->dev, "Loaded dma driver with %d physical channels\n",
  1163. pdata->num_phy_channels);
  1164. return 0;
  1165. err_slave_reg:
  1166. dma_async_device_unregister(&s3cdma->memcpy);
  1167. err_memcpy_reg:
  1168. s3c24xx_dma_free_virtual_channels(&s3cdma->slave);
  1169. err_slave:
  1170. s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);
  1171. err_memcpy:
  1172. if (sdata->has_clocks)
  1173. for (i = 0; i < pdata->num_phy_channels; i++) {
  1174. struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
  1175. if (phy->valid)
  1176. clk_unprepare(phy->clk);
  1177. }
  1178. return ret;
  1179. }
  1180. static int s3c24xx_dma_remove(struct platform_device *pdev)
  1181. {
  1182. const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);
  1183. struct s3c24xx_dma_engine *s3cdma = platform_get_drvdata(pdev);
  1184. struct soc_data *sdata = s3c24xx_dma_get_soc_data(pdev);
  1185. int i;
  1186. dma_async_device_unregister(&s3cdma->slave);
  1187. dma_async_device_unregister(&s3cdma->memcpy);
  1188. s3c24xx_dma_free_virtual_channels(&s3cdma->slave);
  1189. s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);
  1190. if (sdata->has_clocks)
  1191. for (i = 0; i < pdata->num_phy_channels; i++) {
  1192. struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
  1193. if (phy->valid)
  1194. clk_unprepare(phy->clk);
  1195. }
  1196. return 0;
  1197. }
  1198. static struct platform_driver s3c24xx_dma_driver = {
  1199. .driver = {
  1200. .name = "s3c24xx-dma",
  1201. .owner = THIS_MODULE,
  1202. },
  1203. .id_table = s3c24xx_dma_driver_ids,
  1204. .probe = s3c24xx_dma_probe,
  1205. .remove = s3c24xx_dma_remove,
  1206. };
  1207. module_platform_driver(s3c24xx_dma_driver);
  1208. bool s3c24xx_dma_filter(struct dma_chan *chan, void *param)
  1209. {
  1210. struct s3c24xx_dma_chan *s3cchan;
  1211. if (chan->device->dev->driver != &s3c24xx_dma_driver.driver)
  1212. return false;
  1213. s3cchan = to_s3c24xx_dma_chan(chan);
  1214. return s3cchan->id == (int)param;
  1215. }
  1216. EXPORT_SYMBOL(s3c24xx_dma_filter);
  1217. MODULE_DESCRIPTION("S3C24XX DMA Driver");
  1218. MODULE_AUTHOR("Heiko Stuebner");
  1219. MODULE_LICENSE("GPL v2");