s3c24xx-dma.c 38 KB

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