sun4i-dma.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /*
  2. * Copyright (C) 2014 Emilio López
  3. * Emilio López <emilio@elopez.com.ar>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/bitmap.h>
  11. #include <linux/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dmapool.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/of_dma.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include "virt-dma.h"
  22. /** Common macros to normal and dedicated DMA registers **/
  23. #define SUN4I_DMA_CFG_LOADING BIT(31)
  24. #define SUN4I_DMA_CFG_DST_DATA_WIDTH(width) ((width) << 25)
  25. #define SUN4I_DMA_CFG_DST_BURST_LENGTH(len) ((len) << 23)
  26. #define SUN4I_DMA_CFG_DST_ADDR_MODE(mode) ((mode) << 21)
  27. #define SUN4I_DMA_CFG_DST_DRQ_TYPE(type) ((type) << 16)
  28. #define SUN4I_DMA_CFG_SRC_DATA_WIDTH(width) ((width) << 9)
  29. #define SUN4I_DMA_CFG_SRC_BURST_LENGTH(len) ((len) << 7)
  30. #define SUN4I_DMA_CFG_SRC_ADDR_MODE(mode) ((mode) << 5)
  31. #define SUN4I_DMA_CFG_SRC_DRQ_TYPE(type) (type)
  32. /** Normal DMA register values **/
  33. /* Normal DMA source/destination data request type values */
  34. #define SUN4I_NDMA_DRQ_TYPE_SDRAM 0x16
  35. #define SUN4I_NDMA_DRQ_TYPE_LIMIT (0x1F + 1)
  36. /** Normal DMA register layout **/
  37. /* Dedicated DMA source/destination address mode values */
  38. #define SUN4I_NDMA_ADDR_MODE_LINEAR 0
  39. #define SUN4I_NDMA_ADDR_MODE_IO 1
  40. /* Normal DMA configuration register layout */
  41. #define SUN4I_NDMA_CFG_CONT_MODE BIT(30)
  42. #define SUN4I_NDMA_CFG_WAIT_STATE(n) ((n) << 27)
  43. #define SUN4I_NDMA_CFG_DST_NON_SECURE BIT(22)
  44. #define SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN BIT(15)
  45. #define SUN4I_NDMA_CFG_SRC_NON_SECURE BIT(6)
  46. /** Dedicated DMA register values **/
  47. /* Dedicated DMA source/destination address mode values */
  48. #define SUN4I_DDMA_ADDR_MODE_LINEAR 0
  49. #define SUN4I_DDMA_ADDR_MODE_IO 1
  50. #define SUN4I_DDMA_ADDR_MODE_HORIZONTAL_PAGE 2
  51. #define SUN4I_DDMA_ADDR_MODE_VERTICAL_PAGE 3
  52. /* Dedicated DMA source/destination data request type values */
  53. #define SUN4I_DDMA_DRQ_TYPE_SDRAM 0x1
  54. #define SUN4I_DDMA_DRQ_TYPE_LIMIT (0x1F + 1)
  55. /** Dedicated DMA register layout **/
  56. /* Dedicated DMA configuration register layout */
  57. #define SUN4I_DDMA_CFG_BUSY BIT(30)
  58. #define SUN4I_DDMA_CFG_CONT_MODE BIT(29)
  59. #define SUN4I_DDMA_CFG_DST_NON_SECURE BIT(28)
  60. #define SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN BIT(15)
  61. #define SUN4I_DDMA_CFG_SRC_NON_SECURE BIT(12)
  62. /* Dedicated DMA parameter register layout */
  63. #define SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(n) (((n) - 1) << 24)
  64. #define SUN4I_DDMA_PARA_DST_WAIT_CYCLES(n) (((n) - 1) << 16)
  65. #define SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(n) (((n) - 1) << 8)
  66. #define SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(n) (((n) - 1) << 0)
  67. /** DMA register offsets **/
  68. /* General register offsets */
  69. #define SUN4I_DMA_IRQ_ENABLE_REG 0x0
  70. #define SUN4I_DMA_IRQ_PENDING_STATUS_REG 0x4
  71. /* Normal DMA register offsets */
  72. #define SUN4I_NDMA_CHANNEL_REG_BASE(n) (0x100 + (n) * 0x20)
  73. #define SUN4I_NDMA_CFG_REG 0x0
  74. #define SUN4I_NDMA_SRC_ADDR_REG 0x4
  75. #define SUN4I_NDMA_DST_ADDR_REG 0x8
  76. #define SUN4I_NDMA_BYTE_COUNT_REG 0xC
  77. /* Dedicated DMA register offsets */
  78. #define SUN4I_DDMA_CHANNEL_REG_BASE(n) (0x300 + (n) * 0x20)
  79. #define SUN4I_DDMA_CFG_REG 0x0
  80. #define SUN4I_DDMA_SRC_ADDR_REG 0x4
  81. #define SUN4I_DDMA_DST_ADDR_REG 0x8
  82. #define SUN4I_DDMA_BYTE_COUNT_REG 0xC
  83. #define SUN4I_DDMA_PARA_REG 0x18
  84. /** DMA Driver **/
  85. /*
  86. * Normal DMA has 8 channels, and Dedicated DMA has another 8, so
  87. * that's 16 channels. As for endpoints, there's 29 and 21
  88. * respectively. Given that the Normal DMA endpoints (other than
  89. * SDRAM) can be used as tx/rx, we need 78 vchans in total
  90. */
  91. #define SUN4I_NDMA_NR_MAX_CHANNELS 8
  92. #define SUN4I_DDMA_NR_MAX_CHANNELS 8
  93. #define SUN4I_DMA_NR_MAX_CHANNELS \
  94. (SUN4I_NDMA_NR_MAX_CHANNELS + SUN4I_DDMA_NR_MAX_CHANNELS)
  95. #define SUN4I_NDMA_NR_MAX_VCHANS (29 * 2 - 1)
  96. #define SUN4I_DDMA_NR_MAX_VCHANS 21
  97. #define SUN4I_DMA_NR_MAX_VCHANS \
  98. (SUN4I_NDMA_NR_MAX_VCHANS + SUN4I_DDMA_NR_MAX_VCHANS)
  99. /* This set of SUN4I_DDMA timing parameters were found experimentally while
  100. * working with the SPI driver and seem to make it behave correctly */
  101. #define SUN4I_DDMA_MAGIC_SPI_PARAMETERS \
  102. (SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(1) | \
  103. SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(1) | \
  104. SUN4I_DDMA_PARA_DST_WAIT_CYCLES(2) | \
  105. SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(2))
  106. struct sun4i_dma_pchan {
  107. /* Register base of channel */
  108. void __iomem *base;
  109. /* vchan currently being serviced */
  110. struct sun4i_dma_vchan *vchan;
  111. /* Is this a dedicated pchan? */
  112. int is_dedicated;
  113. };
  114. struct sun4i_dma_vchan {
  115. struct virt_dma_chan vc;
  116. struct dma_slave_config cfg;
  117. struct sun4i_dma_pchan *pchan;
  118. struct sun4i_dma_promise *processing;
  119. struct sun4i_dma_contract *contract;
  120. u8 endpoint;
  121. int is_dedicated;
  122. };
  123. struct sun4i_dma_promise {
  124. u32 cfg;
  125. u32 para;
  126. dma_addr_t src;
  127. dma_addr_t dst;
  128. size_t len;
  129. struct list_head list;
  130. };
  131. /* A contract is a set of promises */
  132. struct sun4i_dma_contract {
  133. struct virt_dma_desc vd;
  134. struct list_head demands;
  135. struct list_head completed_demands;
  136. int is_cyclic;
  137. };
  138. struct sun4i_dma_dev {
  139. DECLARE_BITMAP(pchans_used, SUN4I_DMA_NR_MAX_CHANNELS);
  140. struct dma_device slave;
  141. struct sun4i_dma_pchan *pchans;
  142. struct sun4i_dma_vchan *vchans;
  143. void __iomem *base;
  144. struct clk *clk;
  145. int irq;
  146. spinlock_t lock;
  147. };
  148. static struct sun4i_dma_dev *to_sun4i_dma_dev(struct dma_device *dev)
  149. {
  150. return container_of(dev, struct sun4i_dma_dev, slave);
  151. }
  152. static struct sun4i_dma_vchan *to_sun4i_dma_vchan(struct dma_chan *chan)
  153. {
  154. return container_of(chan, struct sun4i_dma_vchan, vc.chan);
  155. }
  156. static struct sun4i_dma_contract *to_sun4i_dma_contract(struct virt_dma_desc *vd)
  157. {
  158. return container_of(vd, struct sun4i_dma_contract, vd);
  159. }
  160. static struct device *chan2dev(struct dma_chan *chan)
  161. {
  162. return &chan->dev->device;
  163. }
  164. static int convert_burst(u32 maxburst)
  165. {
  166. if (maxburst > 8)
  167. return -EINVAL;
  168. /* 1 -> 0, 4 -> 1, 8 -> 2 */
  169. return (maxburst >> 2);
  170. }
  171. static int convert_buswidth(enum dma_slave_buswidth addr_width)
  172. {
  173. if (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES)
  174. return -EINVAL;
  175. /* 8 (1 byte) -> 0, 16 (2 bytes) -> 1, 32 (4 bytes) -> 2 */
  176. return (addr_width >> 1);
  177. }
  178. static void sun4i_dma_free_chan_resources(struct dma_chan *chan)
  179. {
  180. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  181. vchan_free_chan_resources(&vchan->vc);
  182. }
  183. static struct sun4i_dma_pchan *find_and_use_pchan(struct sun4i_dma_dev *priv,
  184. struct sun4i_dma_vchan *vchan)
  185. {
  186. struct sun4i_dma_pchan *pchan = NULL, *pchans = priv->pchans;
  187. unsigned long flags;
  188. int i, max;
  189. /*
  190. * pchans 0-SUN4I_NDMA_NR_MAX_CHANNELS are normal, and
  191. * SUN4I_NDMA_NR_MAX_CHANNELS+ are dedicated ones
  192. */
  193. if (vchan->is_dedicated) {
  194. i = SUN4I_NDMA_NR_MAX_CHANNELS;
  195. max = SUN4I_DMA_NR_MAX_CHANNELS;
  196. } else {
  197. i = 0;
  198. max = SUN4I_NDMA_NR_MAX_CHANNELS;
  199. }
  200. spin_lock_irqsave(&priv->lock, flags);
  201. for_each_clear_bit_from(i, priv->pchans_used, max) {
  202. pchan = &pchans[i];
  203. pchan->vchan = vchan;
  204. set_bit(i, priv->pchans_used);
  205. break;
  206. }
  207. spin_unlock_irqrestore(&priv->lock, flags);
  208. return pchan;
  209. }
  210. static void release_pchan(struct sun4i_dma_dev *priv,
  211. struct sun4i_dma_pchan *pchan)
  212. {
  213. unsigned long flags;
  214. int nr = pchan - priv->pchans;
  215. spin_lock_irqsave(&priv->lock, flags);
  216. pchan->vchan = NULL;
  217. clear_bit(nr, priv->pchans_used);
  218. spin_unlock_irqrestore(&priv->lock, flags);
  219. }
  220. static void configure_pchan(struct sun4i_dma_pchan *pchan,
  221. struct sun4i_dma_promise *d)
  222. {
  223. /*
  224. * Configure addresses and misc parameters depending on type
  225. * SUN4I_DDMA has an extra field with timing parameters
  226. */
  227. if (pchan->is_dedicated) {
  228. writel_relaxed(d->src, pchan->base + SUN4I_DDMA_SRC_ADDR_REG);
  229. writel_relaxed(d->dst, pchan->base + SUN4I_DDMA_DST_ADDR_REG);
  230. writel_relaxed(d->len, pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
  231. writel_relaxed(d->para, pchan->base + SUN4I_DDMA_PARA_REG);
  232. writel_relaxed(d->cfg, pchan->base + SUN4I_DDMA_CFG_REG);
  233. } else {
  234. writel_relaxed(d->src, pchan->base + SUN4I_NDMA_SRC_ADDR_REG);
  235. writel_relaxed(d->dst, pchan->base + SUN4I_NDMA_DST_ADDR_REG);
  236. writel_relaxed(d->len, pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
  237. writel_relaxed(d->cfg, pchan->base + SUN4I_NDMA_CFG_REG);
  238. }
  239. }
  240. static void set_pchan_interrupt(struct sun4i_dma_dev *priv,
  241. struct sun4i_dma_pchan *pchan,
  242. int half, int end)
  243. {
  244. u32 reg;
  245. int pchan_number = pchan - priv->pchans;
  246. unsigned long flags;
  247. spin_lock_irqsave(&priv->lock, flags);
  248. reg = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
  249. if (half)
  250. reg |= BIT(pchan_number * 2);
  251. else
  252. reg &= ~BIT(pchan_number * 2);
  253. if (end)
  254. reg |= BIT(pchan_number * 2 + 1);
  255. else
  256. reg &= ~BIT(pchan_number * 2 + 1);
  257. writel_relaxed(reg, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
  258. spin_unlock_irqrestore(&priv->lock, flags);
  259. }
  260. /**
  261. * Execute pending operations on a vchan
  262. *
  263. * When given a vchan, this function will try to acquire a suitable
  264. * pchan and, if successful, will configure it to fulfill a promise
  265. * from the next pending contract.
  266. *
  267. * This function must be called with &vchan->vc.lock held.
  268. */
  269. static int __execute_vchan_pending(struct sun4i_dma_dev *priv,
  270. struct sun4i_dma_vchan *vchan)
  271. {
  272. struct sun4i_dma_promise *promise = NULL;
  273. struct sun4i_dma_contract *contract = NULL;
  274. struct sun4i_dma_pchan *pchan;
  275. struct virt_dma_desc *vd;
  276. int ret;
  277. lockdep_assert_held(&vchan->vc.lock);
  278. /* We need a pchan to do anything, so secure one if available */
  279. pchan = find_and_use_pchan(priv, vchan);
  280. if (!pchan)
  281. return -EBUSY;
  282. /*
  283. * Channel endpoints must not be repeated, so if this vchan
  284. * has already submitted some work, we can't do anything else
  285. */
  286. if (vchan->processing) {
  287. dev_dbg(chan2dev(&vchan->vc.chan),
  288. "processing something to this endpoint already\n");
  289. ret = -EBUSY;
  290. goto release_pchan;
  291. }
  292. do {
  293. /* Figure out which contract we're working with today */
  294. vd = vchan_next_desc(&vchan->vc);
  295. if (!vd) {
  296. dev_dbg(chan2dev(&vchan->vc.chan),
  297. "No pending contract found");
  298. ret = 0;
  299. goto release_pchan;
  300. }
  301. contract = to_sun4i_dma_contract(vd);
  302. if (list_empty(&contract->demands)) {
  303. /* The contract has been completed so mark it as such */
  304. list_del(&contract->vd.node);
  305. vchan_cookie_complete(&contract->vd);
  306. dev_dbg(chan2dev(&vchan->vc.chan),
  307. "Empty contract found and marked complete");
  308. }
  309. } while (list_empty(&contract->demands));
  310. /* Now find out what we need to do */
  311. promise = list_first_entry(&contract->demands,
  312. struct sun4i_dma_promise, list);
  313. vchan->processing = promise;
  314. /* ... and make it reality */
  315. if (promise) {
  316. vchan->contract = contract;
  317. vchan->pchan = pchan;
  318. set_pchan_interrupt(priv, pchan, contract->is_cyclic, 1);
  319. configure_pchan(pchan, promise);
  320. }
  321. return 0;
  322. release_pchan:
  323. release_pchan(priv, pchan);
  324. return ret;
  325. }
  326. static int sanitize_config(struct dma_slave_config *sconfig,
  327. enum dma_transfer_direction direction)
  328. {
  329. switch (direction) {
  330. case DMA_MEM_TO_DEV:
  331. if ((sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
  332. !sconfig->dst_maxburst)
  333. return -EINVAL;
  334. if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  335. sconfig->src_addr_width = sconfig->dst_addr_width;
  336. if (!sconfig->src_maxburst)
  337. sconfig->src_maxburst = sconfig->dst_maxburst;
  338. break;
  339. case DMA_DEV_TO_MEM:
  340. if ((sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
  341. !sconfig->src_maxburst)
  342. return -EINVAL;
  343. if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  344. sconfig->dst_addr_width = sconfig->src_addr_width;
  345. if (!sconfig->dst_maxburst)
  346. sconfig->dst_maxburst = sconfig->src_maxburst;
  347. break;
  348. default:
  349. return 0;
  350. }
  351. return 0;
  352. }
  353. /**
  354. * Generate a promise, to be used in a normal DMA contract.
  355. *
  356. * A NDMA promise contains all the information required to program the
  357. * normal part of the DMA Engine and get data copied. A non-executed
  358. * promise will live in the demands list on a contract. Once it has been
  359. * completed, it will be moved to the completed demands list for later freeing.
  360. * All linked promises will be freed when the corresponding contract is freed
  361. */
  362. static struct sun4i_dma_promise *
  363. generate_ndma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
  364. size_t len, struct dma_slave_config *sconfig,
  365. enum dma_transfer_direction direction)
  366. {
  367. struct sun4i_dma_promise *promise;
  368. int ret;
  369. ret = sanitize_config(sconfig, direction);
  370. if (ret)
  371. return NULL;
  372. promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
  373. if (!promise)
  374. return NULL;
  375. promise->src = src;
  376. promise->dst = dest;
  377. promise->len = len;
  378. promise->cfg = SUN4I_DMA_CFG_LOADING |
  379. SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN;
  380. dev_dbg(chan2dev(chan),
  381. "src burst %d, dst burst %d, src buswidth %d, dst buswidth %d",
  382. sconfig->src_maxburst, sconfig->dst_maxburst,
  383. sconfig->src_addr_width, sconfig->dst_addr_width);
  384. /* Source burst */
  385. ret = convert_burst(sconfig->src_maxburst);
  386. if (ret < 0)
  387. goto fail;
  388. promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
  389. /* Destination burst */
  390. ret = convert_burst(sconfig->dst_maxburst);
  391. if (ret < 0)
  392. goto fail;
  393. promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
  394. /* Source bus width */
  395. ret = convert_buswidth(sconfig->src_addr_width);
  396. if (ret < 0)
  397. goto fail;
  398. promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
  399. /* Destination bus width */
  400. ret = convert_buswidth(sconfig->dst_addr_width);
  401. if (ret < 0)
  402. goto fail;
  403. promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
  404. return promise;
  405. fail:
  406. kfree(promise);
  407. return NULL;
  408. }
  409. /**
  410. * Generate a promise, to be used in a dedicated DMA contract.
  411. *
  412. * A DDMA promise contains all the information required to program the
  413. * Dedicated part of the DMA Engine and get data copied. A non-executed
  414. * promise will live in the demands list on a contract. Once it has been
  415. * completed, it will be moved to the completed demands list for later freeing.
  416. * All linked promises will be freed when the corresponding contract is freed
  417. */
  418. static struct sun4i_dma_promise *
  419. generate_ddma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
  420. size_t len, struct dma_slave_config *sconfig)
  421. {
  422. struct sun4i_dma_promise *promise;
  423. int ret;
  424. promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
  425. if (!promise)
  426. return NULL;
  427. promise->src = src;
  428. promise->dst = dest;
  429. promise->len = len;
  430. promise->cfg = SUN4I_DMA_CFG_LOADING |
  431. SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN;
  432. /* Source burst */
  433. ret = convert_burst(sconfig->src_maxburst);
  434. if (ret < 0)
  435. goto fail;
  436. promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
  437. /* Destination burst */
  438. ret = convert_burst(sconfig->dst_maxburst);
  439. if (ret < 0)
  440. goto fail;
  441. promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
  442. /* Source bus width */
  443. ret = convert_buswidth(sconfig->src_addr_width);
  444. if (ret < 0)
  445. goto fail;
  446. promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
  447. /* Destination bus width */
  448. ret = convert_buswidth(sconfig->dst_addr_width);
  449. if (ret < 0)
  450. goto fail;
  451. promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
  452. return promise;
  453. fail:
  454. kfree(promise);
  455. return NULL;
  456. }
  457. /**
  458. * Generate a contract
  459. *
  460. * Contracts function as DMA descriptors. As our hardware does not support
  461. * linked lists, we need to implement SG via software. We use a contract
  462. * to hold all the pieces of the request and process them serially one
  463. * after another. Each piece is represented as a promise.
  464. */
  465. static struct sun4i_dma_contract *generate_dma_contract(void)
  466. {
  467. struct sun4i_dma_contract *contract;
  468. contract = kzalloc(sizeof(*contract), GFP_NOWAIT);
  469. if (!contract)
  470. return NULL;
  471. INIT_LIST_HEAD(&contract->demands);
  472. INIT_LIST_HEAD(&contract->completed_demands);
  473. return contract;
  474. }
  475. /**
  476. * Get next promise on a cyclic transfer
  477. *
  478. * Cyclic contracts contain a series of promises which are executed on a
  479. * loop. This function returns the next promise from a cyclic contract,
  480. * so it can be programmed into the hardware.
  481. */
  482. static struct sun4i_dma_promise *
  483. get_next_cyclic_promise(struct sun4i_dma_contract *contract)
  484. {
  485. struct sun4i_dma_promise *promise;
  486. promise = list_first_entry_or_null(&contract->demands,
  487. struct sun4i_dma_promise, list);
  488. if (!promise) {
  489. list_splice_init(&contract->completed_demands,
  490. &contract->demands);
  491. promise = list_first_entry(&contract->demands,
  492. struct sun4i_dma_promise, list);
  493. }
  494. return promise;
  495. }
  496. /**
  497. * Free a contract and all its associated promises
  498. */
  499. static void sun4i_dma_free_contract(struct virt_dma_desc *vd)
  500. {
  501. struct sun4i_dma_contract *contract = to_sun4i_dma_contract(vd);
  502. struct sun4i_dma_promise *promise, *tmp;
  503. /* Free all the demands and completed demands */
  504. list_for_each_entry_safe(promise, tmp, &contract->demands, list)
  505. kfree(promise);
  506. list_for_each_entry_safe(promise, tmp, &contract->completed_demands, list)
  507. kfree(promise);
  508. kfree(contract);
  509. }
  510. static struct dma_async_tx_descriptor *
  511. sun4i_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
  512. dma_addr_t src, size_t len, unsigned long flags)
  513. {
  514. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  515. struct dma_slave_config *sconfig = &vchan->cfg;
  516. struct sun4i_dma_promise *promise;
  517. struct sun4i_dma_contract *contract;
  518. contract = generate_dma_contract();
  519. if (!contract)
  520. return NULL;
  521. /*
  522. * We can only do the copy to bus aligned addresses, so
  523. * choose the best one so we get decent performance. We also
  524. * maximize the burst size for this same reason.
  525. */
  526. sconfig->src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  527. sconfig->dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  528. sconfig->src_maxburst = 8;
  529. sconfig->dst_maxburst = 8;
  530. if (vchan->is_dedicated)
  531. promise = generate_ddma_promise(chan, src, dest, len, sconfig);
  532. else
  533. promise = generate_ndma_promise(chan, src, dest, len, sconfig,
  534. DMA_MEM_TO_MEM);
  535. if (!promise) {
  536. kfree(contract);
  537. return NULL;
  538. }
  539. /* Configure memcpy mode */
  540. if (vchan->is_dedicated) {
  541. promise->cfg |= SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_DDMA_DRQ_TYPE_SDRAM) |
  542. SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_DDMA_DRQ_TYPE_SDRAM);
  543. } else {
  544. promise->cfg |= SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM) |
  545. SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM);
  546. }
  547. /* Fill the contract with our only promise */
  548. list_add_tail(&promise->list, &contract->demands);
  549. /* And add it to the vchan */
  550. return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
  551. }
  552. static struct dma_async_tx_descriptor *
  553. sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
  554. size_t period_len, enum dma_transfer_direction dir,
  555. unsigned long flags)
  556. {
  557. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  558. struct dma_slave_config *sconfig = &vchan->cfg;
  559. struct sun4i_dma_promise *promise;
  560. struct sun4i_dma_contract *contract;
  561. dma_addr_t src, dest;
  562. u32 endpoints;
  563. int nr_periods, offset, plength, i;
  564. if (!is_slave_direction(dir)) {
  565. dev_err(chan2dev(chan), "Invalid DMA direction\n");
  566. return NULL;
  567. }
  568. if (vchan->is_dedicated) {
  569. /*
  570. * As we are using this just for audio data, we need to use
  571. * normal DMA. There is nothing stopping us from supporting
  572. * dedicated DMA here as well, so if a client comes up and
  573. * requires it, it will be simple to implement it.
  574. */
  575. dev_err(chan2dev(chan),
  576. "Cyclic transfers are only supported on Normal DMA\n");
  577. return NULL;
  578. }
  579. contract = generate_dma_contract();
  580. if (!contract)
  581. return NULL;
  582. contract->is_cyclic = 1;
  583. /* Figure out the endpoints and the address we need */
  584. if (dir == DMA_MEM_TO_DEV) {
  585. src = buf;
  586. dest = sconfig->dst_addr;
  587. endpoints = SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM) |
  588. SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
  589. SUN4I_DMA_CFG_DST_ADDR_MODE(SUN4I_NDMA_ADDR_MODE_IO);
  590. } else {
  591. src = sconfig->src_addr;
  592. dest = buf;
  593. endpoints = SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
  594. SUN4I_DMA_CFG_SRC_ADDR_MODE(SUN4I_NDMA_ADDR_MODE_IO) |
  595. SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM);
  596. }
  597. /*
  598. * We will be using half done interrupts to make two periods
  599. * out of a promise, so we need to program the DMA engine less
  600. * often
  601. */
  602. /*
  603. * The engine can interrupt on half-transfer, so we can use
  604. * this feature to program the engine half as often as if we
  605. * didn't use it (keep in mind the hardware doesn't support
  606. * linked lists).
  607. *
  608. * Say you have a set of periods (| marks the start/end, I for
  609. * interrupt, P for programming the engine to do a new
  610. * transfer), the easy but slow way would be to do
  611. *
  612. * |---|---|---|---| (periods / promises)
  613. * P I,P I,P I,P I
  614. *
  615. * Using half transfer interrupts you can do
  616. *
  617. * |-------|-------| (promises as configured on hw)
  618. * |---|---|---|---| (periods)
  619. * P I I,P I I
  620. *
  621. * Which requires half the engine programming for the same
  622. * functionality.
  623. */
  624. nr_periods = DIV_ROUND_UP(len / period_len, 2);
  625. for (i = 0; i < nr_periods; i++) {
  626. /* Calculate the offset in the buffer and the length needed */
  627. offset = i * period_len * 2;
  628. plength = min((len - offset), (period_len * 2));
  629. if (dir == DMA_MEM_TO_DEV)
  630. src = buf + offset;
  631. else
  632. dest = buf + offset;
  633. /* Make the promise */
  634. promise = generate_ndma_promise(chan, src, dest,
  635. plength, sconfig, dir);
  636. if (!promise) {
  637. /* TODO: should we free everything? */
  638. return NULL;
  639. }
  640. promise->cfg |= endpoints;
  641. /* Then add it to the contract */
  642. list_add_tail(&promise->list, &contract->demands);
  643. }
  644. /* And add it to the vchan */
  645. return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
  646. }
  647. static struct dma_async_tx_descriptor *
  648. sun4i_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  649. unsigned int sg_len, enum dma_transfer_direction dir,
  650. unsigned long flags, void *context)
  651. {
  652. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  653. struct dma_slave_config *sconfig = &vchan->cfg;
  654. struct sun4i_dma_promise *promise;
  655. struct sun4i_dma_contract *contract;
  656. u8 ram_type, io_mode, linear_mode;
  657. struct scatterlist *sg;
  658. dma_addr_t srcaddr, dstaddr;
  659. u32 endpoints, para;
  660. int i;
  661. if (!sgl)
  662. return NULL;
  663. if (!is_slave_direction(dir)) {
  664. dev_err(chan2dev(chan), "Invalid DMA direction\n");
  665. return NULL;
  666. }
  667. contract = generate_dma_contract();
  668. if (!contract)
  669. return NULL;
  670. if (vchan->is_dedicated) {
  671. io_mode = SUN4I_DDMA_ADDR_MODE_IO;
  672. linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
  673. ram_type = SUN4I_DDMA_DRQ_TYPE_SDRAM;
  674. } else {
  675. io_mode = SUN4I_NDMA_ADDR_MODE_IO;
  676. linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
  677. ram_type = SUN4I_NDMA_DRQ_TYPE_SDRAM;
  678. }
  679. if (dir == DMA_MEM_TO_DEV)
  680. endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
  681. SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
  682. SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type) |
  683. SUN4I_DMA_CFG_SRC_ADDR_MODE(linear_mode);
  684. else
  685. endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
  686. SUN4I_DMA_CFG_DST_ADDR_MODE(linear_mode) |
  687. SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
  688. SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
  689. for_each_sg(sgl, sg, sg_len, i) {
  690. /* Figure out addresses */
  691. if (dir == DMA_MEM_TO_DEV) {
  692. srcaddr = sg_dma_address(sg);
  693. dstaddr = sconfig->dst_addr;
  694. } else {
  695. srcaddr = sconfig->src_addr;
  696. dstaddr = sg_dma_address(sg);
  697. }
  698. /*
  699. * These are the magic DMA engine timings that keep SPI going.
  700. * I haven't seen any interface on DMAEngine to configure
  701. * timings, and so far they seem to work for everything we
  702. * support, so I've kept them here. I don't know if other
  703. * devices need different timings because, as usual, we only
  704. * have the "para" bitfield meanings, but no comment on what
  705. * the values should be when doing a certain operation :|
  706. */
  707. para = SUN4I_DDMA_MAGIC_SPI_PARAMETERS;
  708. /* And make a suitable promise */
  709. if (vchan->is_dedicated)
  710. promise = generate_ddma_promise(chan, srcaddr, dstaddr,
  711. sg_dma_len(sg),
  712. sconfig);
  713. else
  714. promise = generate_ndma_promise(chan, srcaddr, dstaddr,
  715. sg_dma_len(sg),
  716. sconfig, dir);
  717. if (!promise)
  718. return NULL; /* TODO: should we free everything? */
  719. promise->cfg |= endpoints;
  720. promise->para = para;
  721. /* Then add it to the contract */
  722. list_add_tail(&promise->list, &contract->demands);
  723. }
  724. /*
  725. * Once we've got all the promises ready, add the contract
  726. * to the pending list on the vchan
  727. */
  728. return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
  729. }
  730. static int sun4i_dma_terminate_all(struct dma_chan *chan)
  731. {
  732. struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
  733. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  734. struct sun4i_dma_pchan *pchan = vchan->pchan;
  735. LIST_HEAD(head);
  736. unsigned long flags;
  737. spin_lock_irqsave(&vchan->vc.lock, flags);
  738. vchan_get_all_descriptors(&vchan->vc, &head);
  739. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  740. /*
  741. * Clearing the configuration register will halt the pchan. Interrupts
  742. * may still trigger, so don't forget to disable them.
  743. */
  744. if (pchan) {
  745. if (pchan->is_dedicated)
  746. writel(0, pchan->base + SUN4I_DDMA_CFG_REG);
  747. else
  748. writel(0, pchan->base + SUN4I_NDMA_CFG_REG);
  749. set_pchan_interrupt(priv, pchan, 0, 0);
  750. release_pchan(priv, pchan);
  751. }
  752. spin_lock_irqsave(&vchan->vc.lock, flags);
  753. vchan_dma_desc_free_list(&vchan->vc, &head);
  754. /* Clear these so the vchan is usable again */
  755. vchan->processing = NULL;
  756. vchan->pchan = NULL;
  757. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  758. return 0;
  759. }
  760. static int sun4i_dma_config(struct dma_chan *chan,
  761. struct dma_slave_config *config)
  762. {
  763. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  764. memcpy(&vchan->cfg, config, sizeof(*config));
  765. return 0;
  766. }
  767. static struct dma_chan *sun4i_dma_of_xlate(struct of_phandle_args *dma_spec,
  768. struct of_dma *ofdma)
  769. {
  770. struct sun4i_dma_dev *priv = ofdma->of_dma_data;
  771. struct sun4i_dma_vchan *vchan;
  772. struct dma_chan *chan;
  773. u8 is_dedicated = dma_spec->args[0];
  774. u8 endpoint = dma_spec->args[1];
  775. /* Check if type is Normal or Dedicated */
  776. if (is_dedicated != 0 && is_dedicated != 1)
  777. return NULL;
  778. /* Make sure the endpoint looks sane */
  779. if ((is_dedicated && endpoint >= SUN4I_DDMA_DRQ_TYPE_LIMIT) ||
  780. (!is_dedicated && endpoint >= SUN4I_NDMA_DRQ_TYPE_LIMIT))
  781. return NULL;
  782. chan = dma_get_any_slave_channel(&priv->slave);
  783. if (!chan)
  784. return NULL;
  785. /* Assign the endpoint to the vchan */
  786. vchan = to_sun4i_dma_vchan(chan);
  787. vchan->is_dedicated = is_dedicated;
  788. vchan->endpoint = endpoint;
  789. return chan;
  790. }
  791. static enum dma_status sun4i_dma_tx_status(struct dma_chan *chan,
  792. dma_cookie_t cookie,
  793. struct dma_tx_state *state)
  794. {
  795. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  796. struct sun4i_dma_pchan *pchan = vchan->pchan;
  797. struct sun4i_dma_contract *contract;
  798. struct sun4i_dma_promise *promise;
  799. struct virt_dma_desc *vd;
  800. unsigned long flags;
  801. enum dma_status ret;
  802. size_t bytes = 0;
  803. ret = dma_cookie_status(chan, cookie, state);
  804. if (!state || (ret == DMA_COMPLETE))
  805. return ret;
  806. spin_lock_irqsave(&vchan->vc.lock, flags);
  807. vd = vchan_find_desc(&vchan->vc, cookie);
  808. if (!vd)
  809. goto exit;
  810. contract = to_sun4i_dma_contract(vd);
  811. list_for_each_entry(promise, &contract->demands, list)
  812. bytes += promise->len;
  813. /*
  814. * The hardware is configured to return the remaining byte
  815. * quantity. If possible, replace the first listed element's
  816. * full size with the actual remaining amount
  817. */
  818. promise = list_first_entry_or_null(&contract->demands,
  819. struct sun4i_dma_promise, list);
  820. if (promise && pchan) {
  821. bytes -= promise->len;
  822. if (pchan->is_dedicated)
  823. bytes += readl(pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
  824. else
  825. bytes += readl(pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
  826. }
  827. exit:
  828. dma_set_residue(state, bytes);
  829. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  830. return ret;
  831. }
  832. static void sun4i_dma_issue_pending(struct dma_chan *chan)
  833. {
  834. struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
  835. struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
  836. unsigned long flags;
  837. spin_lock_irqsave(&vchan->vc.lock, flags);
  838. /*
  839. * If there are pending transactions for this vchan, push one of
  840. * them into the engine to get the ball rolling.
  841. */
  842. if (vchan_issue_pending(&vchan->vc))
  843. __execute_vchan_pending(priv, vchan);
  844. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  845. }
  846. static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
  847. {
  848. struct sun4i_dma_dev *priv = dev_id;
  849. struct sun4i_dma_pchan *pchans = priv->pchans, *pchan;
  850. struct sun4i_dma_vchan *vchan;
  851. struct sun4i_dma_contract *contract;
  852. struct sun4i_dma_promise *promise;
  853. unsigned long pendirq, irqs, disableirqs;
  854. int bit, i, free_room, allow_mitigation = 1;
  855. pendirq = readl_relaxed(priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
  856. handle_pending:
  857. disableirqs = 0;
  858. free_room = 0;
  859. for_each_set_bit(bit, &pendirq, 32) {
  860. pchan = &pchans[bit >> 1];
  861. vchan = pchan->vchan;
  862. if (!vchan) /* a terminated channel may still interrupt */
  863. continue;
  864. contract = vchan->contract;
  865. /*
  866. * Disable the IRQ and free the pchan if it's an end
  867. * interrupt (odd bit)
  868. */
  869. if (bit & 1) {
  870. spin_lock(&vchan->vc.lock);
  871. /*
  872. * Move the promise into the completed list now that
  873. * we're done with it
  874. */
  875. list_del(&vchan->processing->list);
  876. list_add_tail(&vchan->processing->list,
  877. &contract->completed_demands);
  878. /*
  879. * Cyclic DMA transfers are special:
  880. * - There's always something we can dispatch
  881. * - We need to run the callback
  882. * - Latency is very important, as this is used by audio
  883. * We therefore just cycle through the list and dispatch
  884. * whatever we have here, reusing the pchan. There's
  885. * no need to run the thread after this.
  886. *
  887. * For non-cyclic transfers we need to look around,
  888. * so we can program some more work, or notify the
  889. * client that their transfers have been completed.
  890. */
  891. if (contract->is_cyclic) {
  892. promise = get_next_cyclic_promise(contract);
  893. vchan->processing = promise;
  894. configure_pchan(pchan, promise);
  895. vchan_cyclic_callback(&contract->vd);
  896. } else {
  897. vchan->processing = NULL;
  898. vchan->pchan = NULL;
  899. free_room = 1;
  900. disableirqs |= BIT(bit);
  901. release_pchan(priv, pchan);
  902. }
  903. spin_unlock(&vchan->vc.lock);
  904. } else {
  905. /* Half done interrupt */
  906. if (contract->is_cyclic)
  907. vchan_cyclic_callback(&contract->vd);
  908. else
  909. disableirqs |= BIT(bit);
  910. }
  911. }
  912. /* Disable the IRQs for events we handled */
  913. spin_lock(&priv->lock);
  914. irqs = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
  915. writel_relaxed(irqs & ~disableirqs,
  916. priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
  917. spin_unlock(&priv->lock);
  918. /* Writing 1 to the pending field will clear the pending interrupt */
  919. writel_relaxed(pendirq, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
  920. /*
  921. * If a pchan was freed, we may be able to schedule something else,
  922. * so have a look around
  923. */
  924. if (free_room) {
  925. for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
  926. vchan = &priv->vchans[i];
  927. spin_lock(&vchan->vc.lock);
  928. __execute_vchan_pending(priv, vchan);
  929. spin_unlock(&vchan->vc.lock);
  930. }
  931. }
  932. /*
  933. * Handle newer interrupts if some showed up, but only do it once
  934. * to avoid a too long a loop
  935. */
  936. if (allow_mitigation) {
  937. pendirq = readl_relaxed(priv->base +
  938. SUN4I_DMA_IRQ_PENDING_STATUS_REG);
  939. if (pendirq) {
  940. allow_mitigation = 0;
  941. goto handle_pending;
  942. }
  943. }
  944. return IRQ_HANDLED;
  945. }
  946. static int sun4i_dma_probe(struct platform_device *pdev)
  947. {
  948. struct sun4i_dma_dev *priv;
  949. struct resource *res;
  950. int i, j, ret;
  951. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  952. if (!priv)
  953. return -ENOMEM;
  954. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  955. priv->base = devm_ioremap_resource(&pdev->dev, res);
  956. if (IS_ERR(priv->base))
  957. return PTR_ERR(priv->base);
  958. priv->irq = platform_get_irq(pdev, 0);
  959. if (priv->irq < 0) {
  960. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  961. return priv->irq;
  962. }
  963. priv->clk = devm_clk_get(&pdev->dev, NULL);
  964. if (IS_ERR(priv->clk)) {
  965. dev_err(&pdev->dev, "No clock specified\n");
  966. return PTR_ERR(priv->clk);
  967. }
  968. platform_set_drvdata(pdev, priv);
  969. spin_lock_init(&priv->lock);
  970. dma_cap_zero(priv->slave.cap_mask);
  971. dma_cap_set(DMA_PRIVATE, priv->slave.cap_mask);
  972. dma_cap_set(DMA_MEMCPY, priv->slave.cap_mask);
  973. dma_cap_set(DMA_CYCLIC, priv->slave.cap_mask);
  974. dma_cap_set(DMA_SLAVE, priv->slave.cap_mask);
  975. INIT_LIST_HEAD(&priv->slave.channels);
  976. priv->slave.device_free_chan_resources = sun4i_dma_free_chan_resources;
  977. priv->slave.device_tx_status = sun4i_dma_tx_status;
  978. priv->slave.device_issue_pending = sun4i_dma_issue_pending;
  979. priv->slave.device_prep_slave_sg = sun4i_dma_prep_slave_sg;
  980. priv->slave.device_prep_dma_memcpy = sun4i_dma_prep_dma_memcpy;
  981. priv->slave.device_prep_dma_cyclic = sun4i_dma_prep_dma_cyclic;
  982. priv->slave.device_config = sun4i_dma_config;
  983. priv->slave.device_terminate_all = sun4i_dma_terminate_all;
  984. priv->slave.copy_align = 2;
  985. priv->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  986. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  987. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  988. priv->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  989. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  990. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  991. priv->slave.directions = BIT(DMA_DEV_TO_MEM) |
  992. BIT(DMA_MEM_TO_DEV);
  993. priv->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  994. priv->slave.dev = &pdev->dev;
  995. priv->pchans = devm_kcalloc(&pdev->dev, SUN4I_DMA_NR_MAX_CHANNELS,
  996. sizeof(struct sun4i_dma_pchan), GFP_KERNEL);
  997. priv->vchans = devm_kcalloc(&pdev->dev, SUN4I_DMA_NR_MAX_VCHANS,
  998. sizeof(struct sun4i_dma_vchan), GFP_KERNEL);
  999. if (!priv->vchans || !priv->pchans)
  1000. return -ENOMEM;
  1001. /*
  1002. * [0..SUN4I_NDMA_NR_MAX_CHANNELS) are normal pchans, and
  1003. * [SUN4I_NDMA_NR_MAX_CHANNELS..SUN4I_DMA_NR_MAX_CHANNELS) are
  1004. * dedicated ones
  1005. */
  1006. for (i = 0; i < SUN4I_NDMA_NR_MAX_CHANNELS; i++)
  1007. priv->pchans[i].base = priv->base +
  1008. SUN4I_NDMA_CHANNEL_REG_BASE(i);
  1009. for (j = 0; i < SUN4I_DMA_NR_MAX_CHANNELS; i++, j++) {
  1010. priv->pchans[i].base = priv->base +
  1011. SUN4I_DDMA_CHANNEL_REG_BASE(j);
  1012. priv->pchans[i].is_dedicated = 1;
  1013. }
  1014. for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
  1015. struct sun4i_dma_vchan *vchan = &priv->vchans[i];
  1016. spin_lock_init(&vchan->vc.lock);
  1017. vchan->vc.desc_free = sun4i_dma_free_contract;
  1018. vchan_init(&vchan->vc, &priv->slave);
  1019. }
  1020. ret = clk_prepare_enable(priv->clk);
  1021. if (ret) {
  1022. dev_err(&pdev->dev, "Couldn't enable the clock\n");
  1023. return ret;
  1024. }
  1025. /*
  1026. * Make sure the IRQs are all disabled and accounted for. The bootloader
  1027. * likes to leave these dirty
  1028. */
  1029. writel(0, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
  1030. writel(0xFFFFFFFF, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
  1031. ret = devm_request_irq(&pdev->dev, priv->irq, sun4i_dma_interrupt,
  1032. 0, dev_name(&pdev->dev), priv);
  1033. if (ret) {
  1034. dev_err(&pdev->dev, "Cannot request IRQ\n");
  1035. goto err_clk_disable;
  1036. }
  1037. ret = dma_async_device_register(&priv->slave);
  1038. if (ret) {
  1039. dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
  1040. goto err_clk_disable;
  1041. }
  1042. ret = of_dma_controller_register(pdev->dev.of_node, sun4i_dma_of_xlate,
  1043. priv);
  1044. if (ret) {
  1045. dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  1046. goto err_dma_unregister;
  1047. }
  1048. dev_dbg(&pdev->dev, "Successfully probed SUN4I_DMA\n");
  1049. return 0;
  1050. err_dma_unregister:
  1051. dma_async_device_unregister(&priv->slave);
  1052. err_clk_disable:
  1053. clk_disable_unprepare(priv->clk);
  1054. return ret;
  1055. }
  1056. static int sun4i_dma_remove(struct platform_device *pdev)
  1057. {
  1058. struct sun4i_dma_dev *priv = platform_get_drvdata(pdev);
  1059. /* Disable IRQ so no more work is scheduled */
  1060. disable_irq(priv->irq);
  1061. of_dma_controller_free(pdev->dev.of_node);
  1062. dma_async_device_unregister(&priv->slave);
  1063. clk_disable_unprepare(priv->clk);
  1064. return 0;
  1065. }
  1066. static const struct of_device_id sun4i_dma_match[] = {
  1067. { .compatible = "allwinner,sun4i-a10-dma" },
  1068. { /* sentinel */ },
  1069. };
  1070. MODULE_DEVICE_TABLE(of, sun4i_dma_match);
  1071. static struct platform_driver sun4i_dma_driver = {
  1072. .probe = sun4i_dma_probe,
  1073. .remove = sun4i_dma_remove,
  1074. .driver = {
  1075. .name = "sun4i-dma",
  1076. .of_match_table = sun4i_dma_match,
  1077. },
  1078. };
  1079. module_platform_driver(sun4i_dma_driver);
  1080. MODULE_DESCRIPTION("Allwinner A10 Dedicated DMA Controller Driver");
  1081. MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
  1082. MODULE_LICENSE("GPL");