s3c24xx-dma.c 38 KB

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