s3c24xx-dma.c 38 KB

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