s3c24xx-dma.c 36 KB

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