sun6i-dma.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013-2014 Allwinner Tech Co., Ltd
  4. * Author: Sugar <shuge@allwinnertech.com>
  5. *
  6. * Copyright (C) 2014 Maxime Ripard
  7. * Maxime Ripard <maxime.ripard@free-electrons.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_dma.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/reset.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include "virt-dma.h"
  23. /*
  24. * Common registers
  25. */
  26. #define DMA_IRQ_EN(x) ((x) * 0x04)
  27. #define DMA_IRQ_HALF BIT(0)
  28. #define DMA_IRQ_PKG BIT(1)
  29. #define DMA_IRQ_QUEUE BIT(2)
  30. #define DMA_IRQ_CHAN_NR 8
  31. #define DMA_IRQ_CHAN_WIDTH 4
  32. #define DMA_IRQ_STAT(x) ((x) * 0x04 + 0x10)
  33. #define DMA_STAT 0x30
  34. /* Offset between DMA_IRQ_EN and DMA_IRQ_STAT limits number of channels */
  35. #define DMA_MAX_CHANNELS (DMA_IRQ_CHAN_NR * 0x10 / 4)
  36. /*
  37. * sun8i specific registers
  38. */
  39. #define SUN8I_DMA_GATE 0x20
  40. #define SUN8I_DMA_GATE_ENABLE 0x4
  41. #define SUNXI_H3_SECURE_REG 0x20
  42. #define SUNXI_H3_DMA_GATE 0x28
  43. #define SUNXI_H3_DMA_GATE_ENABLE 0x4
  44. /*
  45. * Channels specific registers
  46. */
  47. #define DMA_CHAN_ENABLE 0x00
  48. #define DMA_CHAN_ENABLE_START BIT(0)
  49. #define DMA_CHAN_ENABLE_STOP 0
  50. #define DMA_CHAN_PAUSE 0x04
  51. #define DMA_CHAN_PAUSE_PAUSE BIT(1)
  52. #define DMA_CHAN_PAUSE_RESUME 0
  53. #define DMA_CHAN_LLI_ADDR 0x08
  54. #define DMA_CHAN_CUR_CFG 0x0c
  55. #define DMA_CHAN_MAX_DRQ_A31 0x1f
  56. #define DMA_CHAN_MAX_DRQ_H6 0x3f
  57. #define DMA_CHAN_CFG_SRC_DRQ_A31(x) ((x) & DMA_CHAN_MAX_DRQ_A31)
  58. #define DMA_CHAN_CFG_SRC_DRQ_H6(x) ((x) & DMA_CHAN_MAX_DRQ_H6)
  59. #define DMA_CHAN_CFG_SRC_MODE_A31(x) (((x) & 0x1) << 5)
  60. #define DMA_CHAN_CFG_SRC_MODE_H6(x) (((x) & 0x1) << 8)
  61. #define DMA_CHAN_CFG_SRC_BURST_A31(x) (((x) & 0x3) << 7)
  62. #define DMA_CHAN_CFG_SRC_BURST_H3(x) (((x) & 0x3) << 6)
  63. #define DMA_CHAN_CFG_SRC_WIDTH(x) (((x) & 0x3) << 9)
  64. #define DMA_CHAN_CFG_DST_DRQ_A31(x) (DMA_CHAN_CFG_SRC_DRQ_A31(x) << 16)
  65. #define DMA_CHAN_CFG_DST_DRQ_H6(x) (DMA_CHAN_CFG_SRC_DRQ_H6(x) << 16)
  66. #define DMA_CHAN_CFG_DST_MODE_A31(x) (DMA_CHAN_CFG_SRC_MODE_A31(x) << 16)
  67. #define DMA_CHAN_CFG_DST_MODE_H6(x) (DMA_CHAN_CFG_SRC_MODE_H6(x) << 16)
  68. #define DMA_CHAN_CFG_DST_BURST_A31(x) (DMA_CHAN_CFG_SRC_BURST_A31(x) << 16)
  69. #define DMA_CHAN_CFG_DST_BURST_H3(x) (DMA_CHAN_CFG_SRC_BURST_H3(x) << 16)
  70. #define DMA_CHAN_CFG_DST_WIDTH(x) (DMA_CHAN_CFG_SRC_WIDTH(x) << 16)
  71. #define DMA_CHAN_CUR_SRC 0x10
  72. #define DMA_CHAN_CUR_DST 0x14
  73. #define DMA_CHAN_CUR_CNT 0x18
  74. #define DMA_CHAN_CUR_PARA 0x1c
  75. /*
  76. * LLI address mangling
  77. *
  78. * The LLI link physical address is also mangled, but we avoid dealing
  79. * with that by allocating LLIs from the DMA32 zone.
  80. */
  81. #define SRC_HIGH_ADDR(x) (((x) & 0x3U) << 16)
  82. #define DST_HIGH_ADDR(x) (((x) & 0x3U) << 18)
  83. /*
  84. * Various hardware related defines
  85. */
  86. #define LLI_LAST_ITEM 0xfffff800
  87. #define NORMAL_WAIT 8
  88. #define DRQ_SDRAM 1
  89. #define LINEAR_MODE 0
  90. #define IO_MODE 1
  91. /* forward declaration */
  92. struct sun6i_dma_dev;
  93. /*
  94. * Hardware channels / ports representation
  95. *
  96. * The hardware is used in several SoCs, with differing numbers
  97. * of channels and endpoints. This structure ties those numbers
  98. * to a certain compatible string.
  99. */
  100. struct sun6i_dma_config {
  101. u32 nr_max_channels;
  102. u32 nr_max_requests;
  103. u32 nr_max_vchans;
  104. /*
  105. * In the datasheets/user manuals of newer Allwinner SoCs, a special
  106. * bit (bit 2 at register 0x20) is present.
  107. * It's named "DMA MCLK interface circuit auto gating bit" in the
  108. * documents, and the footnote of this register says that this bit
  109. * should be set up when initializing the DMA controller.
  110. * Allwinner A23/A33 user manuals do not have this bit documented,
  111. * however these SoCs really have and need this bit, as seen in the
  112. * BSP kernel source code.
  113. */
  114. void (*clock_autogate_enable)(struct sun6i_dma_dev *);
  115. void (*set_burst_length)(u32 *p_cfg, s8 src_burst, s8 dst_burst);
  116. void (*set_drq)(u32 *p_cfg, s8 src_drq, s8 dst_drq);
  117. void (*set_mode)(u32 *p_cfg, s8 src_mode, s8 dst_mode);
  118. u32 src_burst_lengths;
  119. u32 dst_burst_lengths;
  120. u32 src_addr_widths;
  121. u32 dst_addr_widths;
  122. bool has_high_addr;
  123. bool has_mbus_clk;
  124. };
  125. /*
  126. * Hardware representation of the LLI
  127. *
  128. * The hardware will be fed the physical address of this structure,
  129. * and read its content in order to start the transfer.
  130. */
  131. struct sun6i_dma_lli {
  132. u32 cfg;
  133. u32 src;
  134. u32 dst;
  135. u32 len;
  136. u32 para;
  137. u32 p_lli_next;
  138. /*
  139. * This field is not used by the DMA controller, but will be
  140. * used by the CPU to go through the list (mostly for dumping
  141. * or freeing it).
  142. */
  143. struct sun6i_dma_lli *v_lli_next;
  144. };
  145. struct sun6i_desc {
  146. struct virt_dma_desc vd;
  147. dma_addr_t p_lli;
  148. struct sun6i_dma_lli *v_lli;
  149. };
  150. struct sun6i_pchan {
  151. u32 idx;
  152. void __iomem *base;
  153. struct sun6i_vchan *vchan;
  154. struct sun6i_desc *desc;
  155. struct sun6i_desc *done;
  156. };
  157. struct sun6i_vchan {
  158. struct virt_dma_chan vc;
  159. struct list_head node;
  160. struct dma_slave_config cfg;
  161. struct sun6i_pchan *phy;
  162. u8 port;
  163. u8 irq_type;
  164. bool cyclic;
  165. };
  166. struct sun6i_dma_dev {
  167. struct dma_device slave;
  168. void __iomem *base;
  169. struct clk *clk;
  170. struct clk *clk_mbus;
  171. int irq;
  172. spinlock_t lock;
  173. struct reset_control *rstc;
  174. struct tasklet_struct task;
  175. atomic_t tasklet_shutdown;
  176. struct list_head pending;
  177. struct dma_pool *pool;
  178. struct sun6i_pchan *pchans;
  179. struct sun6i_vchan *vchans;
  180. const struct sun6i_dma_config *cfg;
  181. u32 num_pchans;
  182. u32 num_vchans;
  183. u32 max_request;
  184. };
  185. static struct device *chan2dev(struct dma_chan *chan)
  186. {
  187. return &chan->dev->device;
  188. }
  189. static inline struct sun6i_dma_dev *to_sun6i_dma_dev(struct dma_device *d)
  190. {
  191. return container_of(d, struct sun6i_dma_dev, slave);
  192. }
  193. static inline struct sun6i_vchan *to_sun6i_vchan(struct dma_chan *chan)
  194. {
  195. return container_of(chan, struct sun6i_vchan, vc.chan);
  196. }
  197. static inline struct sun6i_desc *
  198. to_sun6i_desc(struct dma_async_tx_descriptor *tx)
  199. {
  200. return container_of(tx, struct sun6i_desc, vd.tx);
  201. }
  202. static inline void sun6i_dma_dump_com_regs(struct sun6i_dma_dev *sdev)
  203. {
  204. dev_dbg(sdev->slave.dev, "Common register:\n"
  205. "\tmask0(%04x): 0x%08x\n"
  206. "\tmask1(%04x): 0x%08x\n"
  207. "\tpend0(%04x): 0x%08x\n"
  208. "\tpend1(%04x): 0x%08x\n"
  209. "\tstats(%04x): 0x%08x\n",
  210. DMA_IRQ_EN(0), readl(sdev->base + DMA_IRQ_EN(0)),
  211. DMA_IRQ_EN(1), readl(sdev->base + DMA_IRQ_EN(1)),
  212. DMA_IRQ_STAT(0), readl(sdev->base + DMA_IRQ_STAT(0)),
  213. DMA_IRQ_STAT(1), readl(sdev->base + DMA_IRQ_STAT(1)),
  214. DMA_STAT, readl(sdev->base + DMA_STAT));
  215. }
  216. static inline void sun6i_dma_dump_chan_regs(struct sun6i_dma_dev *sdev,
  217. struct sun6i_pchan *pchan)
  218. {
  219. dev_dbg(sdev->slave.dev, "Chan %d reg:\n"
  220. "\t___en(%04x): \t0x%08x\n"
  221. "\tpause(%04x): \t0x%08x\n"
  222. "\tstart(%04x): \t0x%08x\n"
  223. "\t__cfg(%04x): \t0x%08x\n"
  224. "\t__src(%04x): \t0x%08x\n"
  225. "\t__dst(%04x): \t0x%08x\n"
  226. "\tcount(%04x): \t0x%08x\n"
  227. "\t_para(%04x): \t0x%08x\n\n",
  228. pchan->idx,
  229. DMA_CHAN_ENABLE,
  230. readl(pchan->base + DMA_CHAN_ENABLE),
  231. DMA_CHAN_PAUSE,
  232. readl(pchan->base + DMA_CHAN_PAUSE),
  233. DMA_CHAN_LLI_ADDR,
  234. readl(pchan->base + DMA_CHAN_LLI_ADDR),
  235. DMA_CHAN_CUR_CFG,
  236. readl(pchan->base + DMA_CHAN_CUR_CFG),
  237. DMA_CHAN_CUR_SRC,
  238. readl(pchan->base + DMA_CHAN_CUR_SRC),
  239. DMA_CHAN_CUR_DST,
  240. readl(pchan->base + DMA_CHAN_CUR_DST),
  241. DMA_CHAN_CUR_CNT,
  242. readl(pchan->base + DMA_CHAN_CUR_CNT),
  243. DMA_CHAN_CUR_PARA,
  244. readl(pchan->base + DMA_CHAN_CUR_PARA));
  245. }
  246. static inline s8 convert_burst(u32 maxburst)
  247. {
  248. switch (maxburst) {
  249. case 1:
  250. return 0;
  251. case 4:
  252. return 1;
  253. case 8:
  254. return 2;
  255. case 16:
  256. return 3;
  257. default:
  258. return -EINVAL;
  259. }
  260. }
  261. static inline s8 convert_buswidth(enum dma_slave_buswidth addr_width)
  262. {
  263. return ilog2(addr_width);
  264. }
  265. static void sun6i_enable_clock_autogate_a23(struct sun6i_dma_dev *sdev)
  266. {
  267. writel(SUN8I_DMA_GATE_ENABLE, sdev->base + SUN8I_DMA_GATE);
  268. }
  269. static void sun6i_enable_clock_autogate_h3(struct sun6i_dma_dev *sdev)
  270. {
  271. writel(SUNXI_H3_DMA_GATE_ENABLE, sdev->base + SUNXI_H3_DMA_GATE);
  272. }
  273. static void sun6i_set_burst_length_a31(u32 *p_cfg, s8 src_burst, s8 dst_burst)
  274. {
  275. *p_cfg |= DMA_CHAN_CFG_SRC_BURST_A31(src_burst) |
  276. DMA_CHAN_CFG_DST_BURST_A31(dst_burst);
  277. }
  278. static void sun6i_set_burst_length_h3(u32 *p_cfg, s8 src_burst, s8 dst_burst)
  279. {
  280. *p_cfg |= DMA_CHAN_CFG_SRC_BURST_H3(src_burst) |
  281. DMA_CHAN_CFG_DST_BURST_H3(dst_burst);
  282. }
  283. static void sun6i_set_drq_a31(u32 *p_cfg, s8 src_drq, s8 dst_drq)
  284. {
  285. *p_cfg |= DMA_CHAN_CFG_SRC_DRQ_A31(src_drq) |
  286. DMA_CHAN_CFG_DST_DRQ_A31(dst_drq);
  287. }
  288. static void sun6i_set_drq_h6(u32 *p_cfg, s8 src_drq, s8 dst_drq)
  289. {
  290. *p_cfg |= DMA_CHAN_CFG_SRC_DRQ_H6(src_drq) |
  291. DMA_CHAN_CFG_DST_DRQ_H6(dst_drq);
  292. }
  293. static void sun6i_set_mode_a31(u32 *p_cfg, s8 src_mode, s8 dst_mode)
  294. {
  295. *p_cfg |= DMA_CHAN_CFG_SRC_MODE_A31(src_mode) |
  296. DMA_CHAN_CFG_DST_MODE_A31(dst_mode);
  297. }
  298. static void sun6i_set_mode_h6(u32 *p_cfg, s8 src_mode, s8 dst_mode)
  299. {
  300. *p_cfg |= DMA_CHAN_CFG_SRC_MODE_H6(src_mode) |
  301. DMA_CHAN_CFG_DST_MODE_H6(dst_mode);
  302. }
  303. static size_t sun6i_get_chan_size(struct sun6i_pchan *pchan)
  304. {
  305. struct sun6i_desc *txd = pchan->desc;
  306. struct sun6i_dma_lli *lli;
  307. size_t bytes;
  308. dma_addr_t pos;
  309. pos = readl(pchan->base + DMA_CHAN_LLI_ADDR);
  310. bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
  311. if (pos == LLI_LAST_ITEM)
  312. return bytes;
  313. for (lli = txd->v_lli; lli; lli = lli->v_lli_next) {
  314. if (lli->p_lli_next == pos) {
  315. for (lli = lli->v_lli_next; lli; lli = lli->v_lli_next)
  316. bytes += lli->len;
  317. break;
  318. }
  319. }
  320. return bytes;
  321. }
  322. static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
  323. struct sun6i_dma_lli *next,
  324. dma_addr_t next_phy,
  325. struct sun6i_desc *txd)
  326. {
  327. if ((!prev && !txd) || !next)
  328. return NULL;
  329. if (!prev) {
  330. txd->p_lli = next_phy;
  331. txd->v_lli = next;
  332. } else {
  333. prev->p_lli_next = next_phy;
  334. prev->v_lli_next = next;
  335. }
  336. next->p_lli_next = LLI_LAST_ITEM;
  337. next->v_lli_next = NULL;
  338. return next;
  339. }
  340. static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
  341. struct sun6i_dma_lli *v_lli,
  342. dma_addr_t p_lli)
  343. {
  344. dev_dbg(chan2dev(&vchan->vc.chan),
  345. "\n\tdesc:\tp - %pad v - 0x%p\n"
  346. "\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
  347. "\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
  348. &p_lli, v_lli,
  349. v_lli->cfg, v_lli->src, v_lli->dst,
  350. v_lli->len, v_lli->para, v_lli->p_lli_next);
  351. }
  352. static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
  353. {
  354. struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
  355. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
  356. struct sun6i_dma_lli *v_lli, *v_next;
  357. dma_addr_t p_lli, p_next;
  358. if (unlikely(!txd))
  359. return;
  360. p_lli = txd->p_lli;
  361. v_lli = txd->v_lli;
  362. while (v_lli) {
  363. v_next = v_lli->v_lli_next;
  364. p_next = v_lli->p_lli_next;
  365. dma_pool_free(sdev->pool, v_lli, p_lli);
  366. v_lli = v_next;
  367. p_lli = p_next;
  368. }
  369. kfree(txd);
  370. }
  371. static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
  372. {
  373. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
  374. struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
  375. struct sun6i_pchan *pchan = vchan->phy;
  376. u32 irq_val, irq_reg, irq_offset;
  377. if (!pchan)
  378. return -EAGAIN;
  379. if (!desc) {
  380. pchan->desc = NULL;
  381. pchan->done = NULL;
  382. return -EAGAIN;
  383. }
  384. list_del(&desc->node);
  385. pchan->desc = to_sun6i_desc(&desc->tx);
  386. pchan->done = NULL;
  387. sun6i_dma_dump_lli(vchan, pchan->desc->v_lli, pchan->desc->p_lli);
  388. irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
  389. irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
  390. vchan->irq_type = vchan->cyclic ? DMA_IRQ_PKG : DMA_IRQ_QUEUE;
  391. irq_val = readl(sdev->base + DMA_IRQ_EN(irq_reg));
  392. irq_val &= ~((DMA_IRQ_HALF | DMA_IRQ_PKG | DMA_IRQ_QUEUE) <<
  393. (irq_offset * DMA_IRQ_CHAN_WIDTH));
  394. irq_val |= vchan->irq_type << (irq_offset * DMA_IRQ_CHAN_WIDTH);
  395. writel(irq_val, sdev->base + DMA_IRQ_EN(irq_reg));
  396. writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
  397. writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
  398. sun6i_dma_dump_com_regs(sdev);
  399. sun6i_dma_dump_chan_regs(sdev, pchan);
  400. return 0;
  401. }
  402. static void sun6i_dma_tasklet(struct tasklet_struct *t)
  403. {
  404. struct sun6i_dma_dev *sdev = from_tasklet(sdev, t, task);
  405. struct sun6i_vchan *vchan;
  406. struct sun6i_pchan *pchan;
  407. unsigned int pchan_alloc = 0;
  408. unsigned int pchan_idx;
  409. list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
  410. spin_lock_irq(&vchan->vc.lock);
  411. pchan = vchan->phy;
  412. if (pchan && pchan->done) {
  413. if (sun6i_dma_start_desc(vchan)) {
  414. /*
  415. * No current txd associated with this channel
  416. */
  417. dev_dbg(sdev->slave.dev, "pchan %u: free\n",
  418. pchan->idx);
  419. /* Mark this channel free */
  420. vchan->phy = NULL;
  421. pchan->vchan = NULL;
  422. }
  423. }
  424. spin_unlock_irq(&vchan->vc.lock);
  425. }
  426. spin_lock_irq(&sdev->lock);
  427. for (pchan_idx = 0; pchan_idx < sdev->num_pchans; pchan_idx++) {
  428. pchan = &sdev->pchans[pchan_idx];
  429. if (pchan->vchan || list_empty(&sdev->pending))
  430. continue;
  431. vchan = list_first_entry(&sdev->pending,
  432. struct sun6i_vchan, node);
  433. /* Remove from pending channels */
  434. list_del_init(&vchan->node);
  435. pchan_alloc |= BIT(pchan_idx);
  436. /* Mark this channel allocated */
  437. pchan->vchan = vchan;
  438. vchan->phy = pchan;
  439. dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
  440. pchan->idx, &vchan->vc);
  441. }
  442. spin_unlock_irq(&sdev->lock);
  443. for (pchan_idx = 0; pchan_idx < sdev->num_pchans; pchan_idx++) {
  444. if (!(pchan_alloc & BIT(pchan_idx)))
  445. continue;
  446. pchan = sdev->pchans + pchan_idx;
  447. vchan = pchan->vchan;
  448. if (vchan) {
  449. spin_lock_irq(&vchan->vc.lock);
  450. sun6i_dma_start_desc(vchan);
  451. spin_unlock_irq(&vchan->vc.lock);
  452. }
  453. }
  454. }
  455. static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
  456. {
  457. struct sun6i_dma_dev *sdev = dev_id;
  458. struct sun6i_vchan *vchan;
  459. struct sun6i_pchan *pchan;
  460. int i, j, ret = IRQ_NONE;
  461. u32 status;
  462. for (i = 0; i < sdev->num_pchans / DMA_IRQ_CHAN_NR; i++) {
  463. status = readl(sdev->base + DMA_IRQ_STAT(i));
  464. if (!status)
  465. continue;
  466. dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
  467. i ? "high" : "low", status);
  468. writel(status, sdev->base + DMA_IRQ_STAT(i));
  469. for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
  470. pchan = sdev->pchans + j;
  471. vchan = pchan->vchan;
  472. if (vchan && (status & vchan->irq_type)) {
  473. if (vchan->cyclic) {
  474. vchan_cyclic_callback(&pchan->desc->vd);
  475. } else {
  476. spin_lock(&vchan->vc.lock);
  477. vchan_cookie_complete(&pchan->desc->vd);
  478. pchan->done = pchan->desc;
  479. spin_unlock(&vchan->vc.lock);
  480. }
  481. }
  482. status = status >> DMA_IRQ_CHAN_WIDTH;
  483. }
  484. if (!atomic_read(&sdev->tasklet_shutdown))
  485. tasklet_schedule(&sdev->task);
  486. ret = IRQ_HANDLED;
  487. }
  488. return ret;
  489. }
  490. static int set_config(struct sun6i_dma_dev *sdev,
  491. struct dma_slave_config *sconfig,
  492. enum dma_transfer_direction direction,
  493. u32 *p_cfg)
  494. {
  495. enum dma_slave_buswidth src_addr_width, dst_addr_width;
  496. u32 src_maxburst, dst_maxburst;
  497. s8 src_width, dst_width, src_burst, dst_burst;
  498. src_addr_width = sconfig->src_addr_width;
  499. dst_addr_width = sconfig->dst_addr_width;
  500. src_maxburst = sconfig->src_maxburst;
  501. dst_maxburst = sconfig->dst_maxburst;
  502. switch (direction) {
  503. case DMA_MEM_TO_DEV:
  504. if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  505. src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  506. src_maxburst = src_maxburst ? src_maxburst : 8;
  507. break;
  508. case DMA_DEV_TO_MEM:
  509. if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  510. dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  511. dst_maxburst = dst_maxburst ? dst_maxburst : 8;
  512. break;
  513. default:
  514. return -EINVAL;
  515. }
  516. if (!(BIT(src_addr_width) & sdev->slave.src_addr_widths))
  517. return -EINVAL;
  518. if (!(BIT(dst_addr_width) & sdev->slave.dst_addr_widths))
  519. return -EINVAL;
  520. if (!(BIT(src_maxburst) & sdev->cfg->src_burst_lengths))
  521. return -EINVAL;
  522. if (!(BIT(dst_maxburst) & sdev->cfg->dst_burst_lengths))
  523. return -EINVAL;
  524. src_width = convert_buswidth(src_addr_width);
  525. dst_width = convert_buswidth(dst_addr_width);
  526. dst_burst = convert_burst(dst_maxburst);
  527. src_burst = convert_burst(src_maxburst);
  528. *p_cfg = DMA_CHAN_CFG_SRC_WIDTH(src_width) |
  529. DMA_CHAN_CFG_DST_WIDTH(dst_width);
  530. sdev->cfg->set_burst_length(p_cfg, src_burst, dst_burst);
  531. return 0;
  532. }
  533. static inline void sun6i_dma_set_addr(struct sun6i_dma_dev *sdev,
  534. struct sun6i_dma_lli *v_lli,
  535. dma_addr_t src, dma_addr_t dst)
  536. {
  537. v_lli->src = lower_32_bits(src);
  538. v_lli->dst = lower_32_bits(dst);
  539. if (sdev->cfg->has_high_addr)
  540. v_lli->para |= SRC_HIGH_ADDR(upper_32_bits(src)) |
  541. DST_HIGH_ADDR(upper_32_bits(dst));
  542. }
  543. static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
  544. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  545. size_t len, unsigned long flags)
  546. {
  547. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  548. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  549. struct sun6i_dma_lli *v_lli;
  550. struct sun6i_desc *txd;
  551. dma_addr_t p_lli;
  552. s8 burst, width;
  553. dev_dbg(chan2dev(chan),
  554. "%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
  555. __func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
  556. if (!len)
  557. return NULL;
  558. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  559. if (!txd)
  560. return NULL;
  561. v_lli = dma_pool_alloc(sdev->pool, GFP_DMA32 | GFP_NOWAIT, &p_lli);
  562. if (!v_lli) {
  563. dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
  564. goto err_txd_free;
  565. }
  566. v_lli->len = len;
  567. v_lli->para = NORMAL_WAIT;
  568. sun6i_dma_set_addr(sdev, v_lli, src, dest);
  569. burst = convert_burst(8);
  570. width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
  571. v_lli->cfg = DMA_CHAN_CFG_SRC_WIDTH(width) |
  572. DMA_CHAN_CFG_DST_WIDTH(width);
  573. sdev->cfg->set_burst_length(&v_lli->cfg, burst, burst);
  574. sdev->cfg->set_drq(&v_lli->cfg, DRQ_SDRAM, DRQ_SDRAM);
  575. sdev->cfg->set_mode(&v_lli->cfg, LINEAR_MODE, LINEAR_MODE);
  576. sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
  577. sun6i_dma_dump_lli(vchan, v_lli, p_lli);
  578. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  579. err_txd_free:
  580. kfree(txd);
  581. return NULL;
  582. }
  583. static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
  584. struct dma_chan *chan, struct scatterlist *sgl,
  585. unsigned int sg_len, enum dma_transfer_direction dir,
  586. unsigned long flags, void *context)
  587. {
  588. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  589. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  590. struct dma_slave_config *sconfig = &vchan->cfg;
  591. struct sun6i_dma_lli *v_lli, *prev = NULL;
  592. struct sun6i_desc *txd;
  593. struct scatterlist *sg;
  594. dma_addr_t p_lli;
  595. u32 lli_cfg;
  596. int i, ret;
  597. if (!sgl)
  598. return NULL;
  599. ret = set_config(sdev, sconfig, dir, &lli_cfg);
  600. if (ret) {
  601. dev_err(chan2dev(chan), "Invalid DMA configuration\n");
  602. return NULL;
  603. }
  604. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  605. if (!txd)
  606. return NULL;
  607. for_each_sg(sgl, sg, sg_len, i) {
  608. v_lli = dma_pool_alloc(sdev->pool, GFP_DMA32 | GFP_NOWAIT, &p_lli);
  609. if (!v_lli)
  610. goto err_lli_free;
  611. v_lli->len = sg_dma_len(sg);
  612. v_lli->para = NORMAL_WAIT;
  613. if (dir == DMA_MEM_TO_DEV) {
  614. sun6i_dma_set_addr(sdev, v_lli,
  615. sg_dma_address(sg),
  616. sconfig->dst_addr);
  617. v_lli->cfg = lli_cfg;
  618. sdev->cfg->set_drq(&v_lli->cfg, DRQ_SDRAM, vchan->port);
  619. sdev->cfg->set_mode(&v_lli->cfg, LINEAR_MODE, IO_MODE);
  620. dev_dbg(chan2dev(chan),
  621. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  622. __func__, vchan->vc.chan.chan_id,
  623. &sconfig->dst_addr, &sg_dma_address(sg),
  624. sg_dma_len(sg), flags);
  625. } else {
  626. sun6i_dma_set_addr(sdev, v_lli,
  627. sconfig->src_addr,
  628. sg_dma_address(sg));
  629. v_lli->cfg = lli_cfg;
  630. sdev->cfg->set_drq(&v_lli->cfg, vchan->port, DRQ_SDRAM);
  631. sdev->cfg->set_mode(&v_lli->cfg, IO_MODE, LINEAR_MODE);
  632. dev_dbg(chan2dev(chan),
  633. "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
  634. __func__, vchan->vc.chan.chan_id,
  635. &sg_dma_address(sg), &sconfig->src_addr,
  636. sg_dma_len(sg), flags);
  637. }
  638. prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
  639. }
  640. dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
  641. for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
  642. p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
  643. sun6i_dma_dump_lli(vchan, v_lli, p_lli);
  644. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  645. err_lli_free:
  646. for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
  647. p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
  648. dma_pool_free(sdev->pool, v_lli, p_lli);
  649. kfree(txd);
  650. return NULL;
  651. }
  652. static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_cyclic(
  653. struct dma_chan *chan,
  654. dma_addr_t buf_addr,
  655. size_t buf_len,
  656. size_t period_len,
  657. enum dma_transfer_direction dir,
  658. unsigned long flags)
  659. {
  660. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  661. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  662. struct dma_slave_config *sconfig = &vchan->cfg;
  663. struct sun6i_dma_lli *v_lli, *prev = NULL;
  664. struct sun6i_desc *txd;
  665. dma_addr_t p_lli;
  666. u32 lli_cfg;
  667. unsigned int i, periods = buf_len / period_len;
  668. int ret;
  669. ret = set_config(sdev, sconfig, dir, &lli_cfg);
  670. if (ret) {
  671. dev_err(chan2dev(chan), "Invalid DMA configuration\n");
  672. return NULL;
  673. }
  674. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  675. if (!txd)
  676. return NULL;
  677. for (i = 0; i < periods; i++) {
  678. v_lli = dma_pool_alloc(sdev->pool, GFP_DMA32 | GFP_NOWAIT, &p_lli);
  679. if (!v_lli) {
  680. dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
  681. goto err_lli_free;
  682. }
  683. v_lli->len = period_len;
  684. v_lli->para = NORMAL_WAIT;
  685. if (dir == DMA_MEM_TO_DEV) {
  686. sun6i_dma_set_addr(sdev, v_lli,
  687. buf_addr + period_len * i,
  688. sconfig->dst_addr);
  689. v_lli->cfg = lli_cfg;
  690. sdev->cfg->set_drq(&v_lli->cfg, DRQ_SDRAM, vchan->port);
  691. sdev->cfg->set_mode(&v_lli->cfg, LINEAR_MODE, IO_MODE);
  692. } else {
  693. sun6i_dma_set_addr(sdev, v_lli,
  694. sconfig->src_addr,
  695. buf_addr + period_len * i);
  696. v_lli->cfg = lli_cfg;
  697. sdev->cfg->set_drq(&v_lli->cfg, vchan->port, DRQ_SDRAM);
  698. sdev->cfg->set_mode(&v_lli->cfg, IO_MODE, LINEAR_MODE);
  699. }
  700. prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
  701. }
  702. prev->p_lli_next = txd->p_lli; /* cyclic list */
  703. vchan->cyclic = true;
  704. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  705. err_lli_free:
  706. for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
  707. p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
  708. dma_pool_free(sdev->pool, v_lli, p_lli);
  709. kfree(txd);
  710. return NULL;
  711. }
  712. static int sun6i_dma_config(struct dma_chan *chan,
  713. struct dma_slave_config *config)
  714. {
  715. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  716. memcpy(&vchan->cfg, config, sizeof(*config));
  717. return 0;
  718. }
  719. static int sun6i_dma_pause(struct dma_chan *chan)
  720. {
  721. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  722. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  723. struct sun6i_pchan *pchan = vchan->phy;
  724. dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
  725. if (pchan) {
  726. writel(DMA_CHAN_PAUSE_PAUSE,
  727. pchan->base + DMA_CHAN_PAUSE);
  728. } else {
  729. spin_lock(&sdev->lock);
  730. list_del_init(&vchan->node);
  731. spin_unlock(&sdev->lock);
  732. }
  733. return 0;
  734. }
  735. static int sun6i_dma_resume(struct dma_chan *chan)
  736. {
  737. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  738. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  739. struct sun6i_pchan *pchan = vchan->phy;
  740. unsigned long flags;
  741. dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
  742. spin_lock_irqsave(&vchan->vc.lock, flags);
  743. if (pchan) {
  744. writel(DMA_CHAN_PAUSE_RESUME,
  745. pchan->base + DMA_CHAN_PAUSE);
  746. } else if (!list_empty(&vchan->vc.desc_issued)) {
  747. spin_lock(&sdev->lock);
  748. list_add_tail(&vchan->node, &sdev->pending);
  749. spin_unlock(&sdev->lock);
  750. }
  751. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  752. return 0;
  753. }
  754. static int sun6i_dma_terminate_all(struct dma_chan *chan)
  755. {
  756. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  757. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  758. struct sun6i_pchan *pchan = vchan->phy;
  759. unsigned long flags;
  760. LIST_HEAD(head);
  761. spin_lock(&sdev->lock);
  762. list_del_init(&vchan->node);
  763. spin_unlock(&sdev->lock);
  764. spin_lock_irqsave(&vchan->vc.lock, flags);
  765. if (vchan->cyclic) {
  766. vchan->cyclic = false;
  767. if (pchan && pchan->desc) {
  768. struct virt_dma_desc *vd = &pchan->desc->vd;
  769. struct virt_dma_chan *vc = &vchan->vc;
  770. list_add_tail(&vd->node, &vc->desc_completed);
  771. }
  772. }
  773. vchan_get_all_descriptors(&vchan->vc, &head);
  774. if (pchan) {
  775. writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
  776. writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
  777. vchan->phy = NULL;
  778. pchan->vchan = NULL;
  779. pchan->desc = NULL;
  780. pchan->done = NULL;
  781. }
  782. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  783. vchan_dma_desc_free_list(&vchan->vc, &head);
  784. return 0;
  785. }
  786. static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
  787. dma_cookie_t cookie,
  788. struct dma_tx_state *state)
  789. {
  790. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  791. struct sun6i_pchan *pchan = vchan->phy;
  792. struct sun6i_dma_lli *lli;
  793. struct virt_dma_desc *vd;
  794. struct sun6i_desc *txd;
  795. enum dma_status ret;
  796. unsigned long flags;
  797. size_t bytes = 0;
  798. ret = dma_cookie_status(chan, cookie, state);
  799. if (ret == DMA_COMPLETE || !state)
  800. return ret;
  801. spin_lock_irqsave(&vchan->vc.lock, flags);
  802. vd = vchan_find_desc(&vchan->vc, cookie);
  803. txd = to_sun6i_desc(&vd->tx);
  804. if (vd) {
  805. for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
  806. bytes += lli->len;
  807. } else if (!pchan || !pchan->desc) {
  808. bytes = 0;
  809. } else {
  810. bytes = sun6i_get_chan_size(pchan);
  811. }
  812. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  813. dma_set_residue(state, bytes);
  814. return ret;
  815. }
  816. static void sun6i_dma_issue_pending(struct dma_chan *chan)
  817. {
  818. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  819. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  820. unsigned long flags;
  821. spin_lock_irqsave(&vchan->vc.lock, flags);
  822. if (vchan_issue_pending(&vchan->vc)) {
  823. spin_lock(&sdev->lock);
  824. if (!vchan->phy && list_empty(&vchan->node)) {
  825. list_add_tail(&vchan->node, &sdev->pending);
  826. tasklet_schedule(&sdev->task);
  827. dev_dbg(chan2dev(chan), "vchan %p: issued\n",
  828. &vchan->vc);
  829. }
  830. spin_unlock(&sdev->lock);
  831. } else {
  832. dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
  833. &vchan->vc);
  834. }
  835. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  836. }
  837. static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
  838. {
  839. struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
  840. struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
  841. unsigned long flags;
  842. spin_lock_irqsave(&sdev->lock, flags);
  843. list_del_init(&vchan->node);
  844. spin_unlock_irqrestore(&sdev->lock, flags);
  845. vchan_free_chan_resources(&vchan->vc);
  846. }
  847. static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
  848. struct of_dma *ofdma)
  849. {
  850. struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
  851. struct sun6i_vchan *vchan;
  852. struct dma_chan *chan;
  853. u8 port = dma_spec->args[0];
  854. if (port > sdev->max_request)
  855. return NULL;
  856. chan = dma_get_any_slave_channel(&sdev->slave);
  857. if (!chan)
  858. return NULL;
  859. vchan = to_sun6i_vchan(chan);
  860. vchan->port = port;
  861. return chan;
  862. }
  863. static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
  864. {
  865. /* Disable all interrupts from DMA */
  866. writel(0, sdev->base + DMA_IRQ_EN(0));
  867. writel(0, sdev->base + DMA_IRQ_EN(1));
  868. /* Prevent spurious interrupts from scheduling the tasklet */
  869. atomic_inc(&sdev->tasklet_shutdown);
  870. /* Make sure we won't have any further interrupts */
  871. devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
  872. /* Actually prevent the tasklet from being scheduled */
  873. tasklet_kill(&sdev->task);
  874. }
  875. static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
  876. {
  877. int i;
  878. for (i = 0; i < sdev->num_vchans; i++) {
  879. struct sun6i_vchan *vchan = &sdev->vchans[i];
  880. list_del(&vchan->vc.chan.device_node);
  881. tasklet_kill(&vchan->vc.task);
  882. }
  883. }
  884. /*
  885. * For A31:
  886. *
  887. * There's 16 physical channels that can work in parallel.
  888. *
  889. * However we have 30 different endpoints for our requests.
  890. *
  891. * Since the channels are able to handle only an unidirectional
  892. * transfer, we need to allocate more virtual channels so that
  893. * everyone can grab one channel.
  894. *
  895. * Some devices can't work in both direction (mostly because it
  896. * wouldn't make sense), so we have a bit fewer virtual channels than
  897. * 2 channels per endpoints.
  898. */
  899. static struct sun6i_dma_config sun6i_a31_dma_cfg = {
  900. .nr_max_channels = 16,
  901. .nr_max_requests = 30,
  902. .nr_max_vchans = 53,
  903. .set_burst_length = sun6i_set_burst_length_a31,
  904. .set_drq = sun6i_set_drq_a31,
  905. .set_mode = sun6i_set_mode_a31,
  906. .src_burst_lengths = BIT(1) | BIT(8),
  907. .dst_burst_lengths = BIT(1) | BIT(8),
  908. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  909. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  910. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  911. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  912. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  913. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  914. };
  915. /*
  916. * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
  917. * and a total of 37 usable source and destination endpoints.
  918. */
  919. static struct sun6i_dma_config sun8i_a23_dma_cfg = {
  920. .nr_max_channels = 8,
  921. .nr_max_requests = 24,
  922. .nr_max_vchans = 37,
  923. .clock_autogate_enable = sun6i_enable_clock_autogate_a23,
  924. .set_burst_length = sun6i_set_burst_length_a31,
  925. .set_drq = sun6i_set_drq_a31,
  926. .set_mode = sun6i_set_mode_a31,
  927. .src_burst_lengths = BIT(1) | BIT(8),
  928. .dst_burst_lengths = BIT(1) | BIT(8),
  929. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  930. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  931. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  932. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  933. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  934. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  935. };
  936. static struct sun6i_dma_config sun8i_a83t_dma_cfg = {
  937. .nr_max_channels = 8,
  938. .nr_max_requests = 28,
  939. .nr_max_vchans = 39,
  940. .clock_autogate_enable = sun6i_enable_clock_autogate_a23,
  941. .set_burst_length = sun6i_set_burst_length_a31,
  942. .set_drq = sun6i_set_drq_a31,
  943. .set_mode = sun6i_set_mode_a31,
  944. .src_burst_lengths = BIT(1) | BIT(8),
  945. .dst_burst_lengths = BIT(1) | BIT(8),
  946. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  947. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  948. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  949. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  950. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  951. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  952. };
  953. /*
  954. * The H3 has 12 physical channels, a maximum DRQ port id of 27,
  955. * and a total of 34 usable source and destination endpoints.
  956. * It also supports additional burst lengths and bus widths,
  957. * and the burst length fields have different offsets.
  958. */
  959. static struct sun6i_dma_config sun8i_h3_dma_cfg = {
  960. .nr_max_channels = 12,
  961. .nr_max_requests = 27,
  962. .nr_max_vchans = 34,
  963. .clock_autogate_enable = sun6i_enable_clock_autogate_h3,
  964. .set_burst_length = sun6i_set_burst_length_h3,
  965. .set_drq = sun6i_set_drq_a31,
  966. .set_mode = sun6i_set_mode_a31,
  967. .src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  968. .dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  969. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  970. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  971. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  972. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  973. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  974. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  975. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  976. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  977. };
  978. /*
  979. * The A64 binding uses the number of dma channels from the
  980. * device tree node.
  981. */
  982. static struct sun6i_dma_config sun50i_a64_dma_cfg = {
  983. .clock_autogate_enable = sun6i_enable_clock_autogate_h3,
  984. .set_burst_length = sun6i_set_burst_length_h3,
  985. .set_drq = sun6i_set_drq_a31,
  986. .set_mode = sun6i_set_mode_a31,
  987. .src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  988. .dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  989. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  990. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  991. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  992. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  993. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  994. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  995. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  996. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  997. };
  998. /*
  999. * The A100 binding uses the number of dma channels from the
  1000. * device tree node.
  1001. */
  1002. static struct sun6i_dma_config sun50i_a100_dma_cfg = {
  1003. .clock_autogate_enable = sun6i_enable_clock_autogate_h3,
  1004. .set_burst_length = sun6i_set_burst_length_h3,
  1005. .set_drq = sun6i_set_drq_h6,
  1006. .set_mode = sun6i_set_mode_h6,
  1007. .src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  1008. .dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  1009. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1010. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1011. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  1012. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  1013. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1014. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1015. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  1016. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  1017. .has_high_addr = true,
  1018. .has_mbus_clk = true,
  1019. };
  1020. /*
  1021. * The H6 binding uses the number of dma channels from the
  1022. * device tree node.
  1023. */
  1024. static struct sun6i_dma_config sun50i_h6_dma_cfg = {
  1025. .clock_autogate_enable = sun6i_enable_clock_autogate_h3,
  1026. .set_burst_length = sun6i_set_burst_length_h3,
  1027. .set_drq = sun6i_set_drq_h6,
  1028. .set_mode = sun6i_set_mode_h6,
  1029. .src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  1030. .dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
  1031. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1032. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1033. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  1034. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  1035. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1036. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1037. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
  1038. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
  1039. .has_mbus_clk = true,
  1040. };
  1041. /*
  1042. * The V3s have only 8 physical channels, a maximum DRQ port id of 23,
  1043. * and a total of 24 usable source and destination endpoints.
  1044. */
  1045. static struct sun6i_dma_config sun8i_v3s_dma_cfg = {
  1046. .nr_max_channels = 8,
  1047. .nr_max_requests = 23,
  1048. .nr_max_vchans = 24,
  1049. .clock_autogate_enable = sun6i_enable_clock_autogate_a23,
  1050. .set_burst_length = sun6i_set_burst_length_a31,
  1051. .set_drq = sun6i_set_drq_a31,
  1052. .set_mode = sun6i_set_mode_a31,
  1053. .src_burst_lengths = BIT(1) | BIT(8),
  1054. .dst_burst_lengths = BIT(1) | BIT(8),
  1055. .src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1056. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1057. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  1058. .dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  1059. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  1060. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
  1061. };
  1062. static const struct of_device_id sun6i_dma_match[] = {
  1063. { .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
  1064. { .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
  1065. { .compatible = "allwinner,sun8i-a83t-dma", .data = &sun8i_a83t_dma_cfg },
  1066. { .compatible = "allwinner,sun8i-h3-dma", .data = &sun8i_h3_dma_cfg },
  1067. { .compatible = "allwinner,sun8i-v3s-dma", .data = &sun8i_v3s_dma_cfg },
  1068. { .compatible = "allwinner,sun20i-d1-dma", .data = &sun50i_a100_dma_cfg },
  1069. { .compatible = "allwinner,sun50i-a64-dma", .data = &sun50i_a64_dma_cfg },
  1070. { .compatible = "allwinner,sun50i-a100-dma", .data = &sun50i_a100_dma_cfg },
  1071. { .compatible = "allwinner,sun50i-h6-dma", .data = &sun50i_h6_dma_cfg },
  1072. { /* sentinel */ }
  1073. };
  1074. MODULE_DEVICE_TABLE(of, sun6i_dma_match);
  1075. static int sun6i_dma_probe(struct platform_device *pdev)
  1076. {
  1077. struct device_node *np = pdev->dev.of_node;
  1078. struct sun6i_dma_dev *sdc;
  1079. int ret, i;
  1080. sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
  1081. if (!sdc)
  1082. return -ENOMEM;
  1083. sdc->cfg = of_device_get_match_data(&pdev->dev);
  1084. if (!sdc->cfg)
  1085. return -ENODEV;
  1086. sdc->base = devm_platform_ioremap_resource(pdev, 0);
  1087. if (IS_ERR(sdc->base))
  1088. return PTR_ERR(sdc->base);
  1089. sdc->irq = platform_get_irq(pdev, 0);
  1090. if (sdc->irq < 0)
  1091. return sdc->irq;
  1092. sdc->clk = devm_clk_get(&pdev->dev, NULL);
  1093. if (IS_ERR(sdc->clk)) {
  1094. dev_err(&pdev->dev, "No clock specified\n");
  1095. return PTR_ERR(sdc->clk);
  1096. }
  1097. if (sdc->cfg->has_mbus_clk) {
  1098. sdc->clk_mbus = devm_clk_get(&pdev->dev, "mbus");
  1099. if (IS_ERR(sdc->clk_mbus)) {
  1100. dev_err(&pdev->dev, "No mbus clock specified\n");
  1101. return PTR_ERR(sdc->clk_mbus);
  1102. }
  1103. }
  1104. sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
  1105. if (IS_ERR(sdc->rstc)) {
  1106. dev_err(&pdev->dev, "No reset controller specified\n");
  1107. return PTR_ERR(sdc->rstc);
  1108. }
  1109. sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  1110. sizeof(struct sun6i_dma_lli), 4, 0);
  1111. if (!sdc->pool) {
  1112. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1113. return -ENOMEM;
  1114. }
  1115. platform_set_drvdata(pdev, sdc);
  1116. INIT_LIST_HEAD(&sdc->pending);
  1117. spin_lock_init(&sdc->lock);
  1118. dma_set_max_seg_size(&pdev->dev, SZ_32M - 1);
  1119. dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
  1120. dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
  1121. dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
  1122. dma_cap_set(DMA_CYCLIC, sdc->slave.cap_mask);
  1123. INIT_LIST_HEAD(&sdc->slave.channels);
  1124. sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
  1125. sdc->slave.device_tx_status = sun6i_dma_tx_status;
  1126. sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
  1127. sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
  1128. sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
  1129. sdc->slave.device_prep_dma_cyclic = sun6i_dma_prep_dma_cyclic;
  1130. sdc->slave.copy_align = DMAENGINE_ALIGN_4_BYTES;
  1131. sdc->slave.device_config = sun6i_dma_config;
  1132. sdc->slave.device_pause = sun6i_dma_pause;
  1133. sdc->slave.device_resume = sun6i_dma_resume;
  1134. sdc->slave.device_terminate_all = sun6i_dma_terminate_all;
  1135. sdc->slave.src_addr_widths = sdc->cfg->src_addr_widths;
  1136. sdc->slave.dst_addr_widths = sdc->cfg->dst_addr_widths;
  1137. sdc->slave.directions = BIT(DMA_DEV_TO_MEM) |
  1138. BIT(DMA_MEM_TO_DEV);
  1139. sdc->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1140. sdc->slave.dev = &pdev->dev;
  1141. sdc->num_pchans = sdc->cfg->nr_max_channels;
  1142. sdc->num_vchans = sdc->cfg->nr_max_vchans;
  1143. sdc->max_request = sdc->cfg->nr_max_requests;
  1144. ret = of_property_read_u32(np, "dma-channels", &sdc->num_pchans);
  1145. if (ret && !sdc->num_pchans) {
  1146. dev_err(&pdev->dev, "Can't get dma-channels.\n");
  1147. return ret;
  1148. }
  1149. ret = of_property_read_u32(np, "dma-requests", &sdc->max_request);
  1150. if (ret && !sdc->max_request) {
  1151. dev_info(&pdev->dev, "Missing dma-requests, using %u.\n",
  1152. DMA_CHAN_MAX_DRQ_A31);
  1153. sdc->max_request = DMA_CHAN_MAX_DRQ_A31;
  1154. }
  1155. /*
  1156. * If the number of vchans is not specified, derive it from the
  1157. * highest port number, at most one channel per port and direction.
  1158. */
  1159. if (!sdc->num_vchans)
  1160. sdc->num_vchans = 2 * (sdc->max_request + 1);
  1161. sdc->pchans = devm_kcalloc(&pdev->dev, sdc->num_pchans,
  1162. sizeof(struct sun6i_pchan), GFP_KERNEL);
  1163. if (!sdc->pchans)
  1164. return -ENOMEM;
  1165. sdc->vchans = devm_kcalloc(&pdev->dev, sdc->num_vchans,
  1166. sizeof(struct sun6i_vchan), GFP_KERNEL);
  1167. if (!sdc->vchans)
  1168. return -ENOMEM;
  1169. tasklet_setup(&sdc->task, sun6i_dma_tasklet);
  1170. for (i = 0; i < sdc->num_pchans; i++) {
  1171. struct sun6i_pchan *pchan = &sdc->pchans[i];
  1172. pchan->idx = i;
  1173. pchan->base = sdc->base + 0x100 + i * 0x40;
  1174. }
  1175. for (i = 0; i < sdc->num_vchans; i++) {
  1176. struct sun6i_vchan *vchan = &sdc->vchans[i];
  1177. INIT_LIST_HEAD(&vchan->node);
  1178. vchan->vc.desc_free = sun6i_dma_free_desc;
  1179. vchan_init(&vchan->vc, &sdc->slave);
  1180. }
  1181. ret = reset_control_deassert(sdc->rstc);
  1182. if (ret) {
  1183. dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
  1184. goto err_chan_free;
  1185. }
  1186. ret = clk_prepare_enable(sdc->clk);
  1187. if (ret) {
  1188. dev_err(&pdev->dev, "Couldn't enable the clock\n");
  1189. goto err_reset_assert;
  1190. }
  1191. if (sdc->cfg->has_mbus_clk) {
  1192. ret = clk_prepare_enable(sdc->clk_mbus);
  1193. if (ret) {
  1194. dev_err(&pdev->dev, "Couldn't enable mbus clock\n");
  1195. goto err_clk_disable;
  1196. }
  1197. }
  1198. ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
  1199. dev_name(&pdev->dev), sdc);
  1200. if (ret) {
  1201. dev_err(&pdev->dev, "Cannot request IRQ\n");
  1202. goto err_mbus_clk_disable;
  1203. }
  1204. ret = dma_async_device_register(&sdc->slave);
  1205. if (ret) {
  1206. dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
  1207. goto err_irq_disable;
  1208. }
  1209. ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
  1210. sdc);
  1211. if (ret) {
  1212. dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  1213. goto err_dma_unregister;
  1214. }
  1215. if (sdc->cfg->clock_autogate_enable)
  1216. sdc->cfg->clock_autogate_enable(sdc);
  1217. return 0;
  1218. err_dma_unregister:
  1219. dma_async_device_unregister(&sdc->slave);
  1220. err_irq_disable:
  1221. sun6i_kill_tasklet(sdc);
  1222. err_mbus_clk_disable:
  1223. clk_disable_unprepare(sdc->clk_mbus);
  1224. err_clk_disable:
  1225. clk_disable_unprepare(sdc->clk);
  1226. err_reset_assert:
  1227. reset_control_assert(sdc->rstc);
  1228. err_chan_free:
  1229. sun6i_dma_free(sdc);
  1230. return ret;
  1231. }
  1232. static void sun6i_dma_remove(struct platform_device *pdev)
  1233. {
  1234. struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
  1235. of_dma_controller_free(pdev->dev.of_node);
  1236. dma_async_device_unregister(&sdc->slave);
  1237. sun6i_kill_tasklet(sdc);
  1238. clk_disable_unprepare(sdc->clk_mbus);
  1239. clk_disable_unprepare(sdc->clk);
  1240. reset_control_assert(sdc->rstc);
  1241. sun6i_dma_free(sdc);
  1242. }
  1243. static struct platform_driver sun6i_dma_driver = {
  1244. .probe = sun6i_dma_probe,
  1245. .remove_new = sun6i_dma_remove,
  1246. .driver = {
  1247. .name = "sun6i-dma",
  1248. .of_match_table = sun6i_dma_match,
  1249. },
  1250. };
  1251. module_platform_driver(sun6i_dma_driver);
  1252. MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
  1253. MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
  1254. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1255. MODULE_LICENSE("GPL");