pxa_dma.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /*
  2. * Copyright 2015 Robert Jarzmik <robert.jarzmik@free.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/slab.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_data/mmp_dma.h>
  19. #include <linux/dmapool.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/of.h>
  23. #include <linux/wait.h>
  24. #include <linux/dma/pxa-dma.h>
  25. #include "dmaengine.h"
  26. #include "virt-dma.h"
  27. #define DCSR(n) (0x0000 + ((n) << 2))
  28. #define DALGN(n) 0x00a0
  29. #define DINT 0x00f0
  30. #define DDADR(n) (0x0200 + ((n) << 4))
  31. #define DSADR(n) (0x0204 + ((n) << 4))
  32. #define DTADR(n) (0x0208 + ((n) << 4))
  33. #define DCMD(n) (0x020c + ((n) << 4))
  34. #define PXA_DCSR_RUN BIT(31) /* Run Bit (read / write) */
  35. #define PXA_DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */
  36. #define PXA_DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (R/W) */
  37. #define PXA_DCSR_REQPEND BIT(8) /* Request Pending (read-only) */
  38. #define PXA_DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */
  39. #define PXA_DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */
  40. #define PXA_DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */
  41. #define PXA_DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */
  42. #define PXA_DCSR_EORIRQEN BIT(28) /* End of Receive IRQ Enable (R/W) */
  43. #define PXA_DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */
  44. #define PXA_DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */
  45. #define PXA_DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */
  46. #define PXA_DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */
  47. #define PXA_DCSR_CMPST BIT(10) /* The Descriptor Compare Status */
  48. #define PXA_DCSR_EORINTR BIT(9) /* The end of Receive */
  49. #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */
  50. #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
  51. #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
  52. #define DDADR_STOP BIT(0) /* Stop (read / write) */
  53. #define PXA_DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */
  54. #define PXA_DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */
  55. #define PXA_DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */
  56. #define PXA_DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */
  57. #define PXA_DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */
  58. #define PXA_DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */
  59. #define PXA_DCMD_ENDIAN BIT(18) /* Device Endian-ness. */
  60. #define PXA_DCMD_BURST8 (1 << 16) /* 8 byte burst */
  61. #define PXA_DCMD_BURST16 (2 << 16) /* 16 byte burst */
  62. #define PXA_DCMD_BURST32 (3 << 16) /* 32 byte burst */
  63. #define PXA_DCMD_WIDTH1 (1 << 14) /* 1 byte width */
  64. #define PXA_DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
  65. #define PXA_DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
  66. #define PXA_DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
  67. #define PDMA_ALIGNMENT 3
  68. #define PDMA_MAX_DESC_BYTES (PXA_DCMD_LENGTH & ~((1 << PDMA_ALIGNMENT) - 1))
  69. struct pxad_desc_hw {
  70. u32 ddadr; /* Points to the next descriptor + flags */
  71. u32 dsadr; /* DSADR value for the current transfer */
  72. u32 dtadr; /* DTADR value for the current transfer */
  73. u32 dcmd; /* DCMD value for the current transfer */
  74. } __aligned(16);
  75. struct pxad_desc_sw {
  76. struct virt_dma_desc vd; /* Virtual descriptor */
  77. int nb_desc; /* Number of hw. descriptors */
  78. size_t len; /* Number of bytes xfered */
  79. dma_addr_t first; /* First descriptor's addr */
  80. /* At least one descriptor has an src/dst address not multiple of 8 */
  81. bool misaligned;
  82. bool cyclic;
  83. struct dma_pool *desc_pool; /* Channel's used allocator */
  84. struct pxad_desc_hw *hw_desc[]; /* DMA coherent descriptors */
  85. };
  86. struct pxad_phy {
  87. int idx;
  88. void __iomem *base;
  89. struct pxad_chan *vchan;
  90. };
  91. struct pxad_chan {
  92. struct virt_dma_chan vc; /* Virtual channel */
  93. u32 drcmr; /* Requestor of the channel */
  94. enum pxad_chan_prio prio; /* Required priority of phy */
  95. /*
  96. * At least one desc_sw in submitted or issued transfers on this channel
  97. * has one address such as: addr % 8 != 0. This implies the DALGN
  98. * setting on the phy.
  99. */
  100. bool misaligned;
  101. struct dma_slave_config cfg; /* Runtime config */
  102. /* protected by vc->lock */
  103. struct pxad_phy *phy;
  104. struct dma_pool *desc_pool; /* Descriptors pool */
  105. dma_cookie_t bus_error;
  106. wait_queue_head_t wq_state;
  107. };
  108. struct pxad_device {
  109. struct dma_device slave;
  110. int nr_chans;
  111. int nr_requestors;
  112. void __iomem *base;
  113. struct pxad_phy *phys;
  114. spinlock_t phy_lock; /* Phy association */
  115. #ifdef CONFIG_DEBUG_FS
  116. struct dentry *dbgfs_root;
  117. struct dentry *dbgfs_state;
  118. struct dentry **dbgfs_chan;
  119. #endif
  120. };
  121. #define tx_to_pxad_desc(tx) \
  122. container_of(tx, struct pxad_desc_sw, async_tx)
  123. #define to_pxad_chan(dchan) \
  124. container_of(dchan, struct pxad_chan, vc.chan)
  125. #define to_pxad_dev(dmadev) \
  126. container_of(dmadev, struct pxad_device, slave)
  127. #define to_pxad_sw_desc(_vd) \
  128. container_of((_vd), struct pxad_desc_sw, vd)
  129. #define _phy_readl_relaxed(phy, _reg) \
  130. readl_relaxed((phy)->base + _reg((phy)->idx))
  131. #define phy_readl_relaxed(phy, _reg) \
  132. ({ \
  133. u32 _v; \
  134. _v = readl_relaxed((phy)->base + _reg((phy)->idx)); \
  135. dev_vdbg(&phy->vchan->vc.chan.dev->device, \
  136. "%s(): readl(%s): 0x%08x\n", __func__, #_reg, \
  137. _v); \
  138. _v; \
  139. })
  140. #define phy_writel(phy, val, _reg) \
  141. do { \
  142. writel((val), (phy)->base + _reg((phy)->idx)); \
  143. dev_vdbg(&phy->vchan->vc.chan.dev->device, \
  144. "%s(): writel(0x%08x, %s)\n", \
  145. __func__, (u32)(val), #_reg); \
  146. } while (0)
  147. #define phy_writel_relaxed(phy, val, _reg) \
  148. do { \
  149. writel_relaxed((val), (phy)->base + _reg((phy)->idx)); \
  150. dev_vdbg(&phy->vchan->vc.chan.dev->device, \
  151. "%s(): writel_relaxed(0x%08x, %s)\n", \
  152. __func__, (u32)(val), #_reg); \
  153. } while (0)
  154. static unsigned int pxad_drcmr(unsigned int line)
  155. {
  156. if (line < 64)
  157. return 0x100 + line * 4;
  158. return 0x1000 + line * 4;
  159. }
  160. /*
  161. * Debug fs
  162. */
  163. #ifdef CONFIG_DEBUG_FS
  164. #include <linux/debugfs.h>
  165. #include <linux/uaccess.h>
  166. #include <linux/seq_file.h>
  167. static int dbg_show_requester_chan(struct seq_file *s, void *p)
  168. {
  169. struct pxad_phy *phy = s->private;
  170. int i;
  171. u32 drcmr;
  172. seq_printf(s, "DMA channel %d requester :\n", phy->idx);
  173. for (i = 0; i < 70; i++) {
  174. drcmr = readl_relaxed(phy->base + pxad_drcmr(i));
  175. if ((drcmr & DRCMR_CHLNUM) == phy->idx)
  176. seq_printf(s, "\tRequester %d (MAPVLD=%d)\n", i,
  177. !!(drcmr & DRCMR_MAPVLD));
  178. }
  179. return 0;
  180. }
  181. static inline int dbg_burst_from_dcmd(u32 dcmd)
  182. {
  183. int burst = (dcmd >> 16) & 0x3;
  184. return burst ? 4 << burst : 0;
  185. }
  186. static int is_phys_valid(unsigned long addr)
  187. {
  188. return pfn_valid(__phys_to_pfn(addr));
  189. }
  190. #define PXA_DCSR_STR(flag) (dcsr & PXA_DCSR_##flag ? #flag" " : "")
  191. #define PXA_DCMD_STR(flag) (dcmd & PXA_DCMD_##flag ? #flag" " : "")
  192. static int dbg_show_descriptors(struct seq_file *s, void *p)
  193. {
  194. struct pxad_phy *phy = s->private;
  195. int i, max_show = 20, burst, width;
  196. u32 dcmd;
  197. unsigned long phys_desc, ddadr;
  198. struct pxad_desc_hw *desc;
  199. phys_desc = ddadr = _phy_readl_relaxed(phy, DDADR);
  200. seq_printf(s, "DMA channel %d descriptors :\n", phy->idx);
  201. seq_printf(s, "[%03d] First descriptor unknown\n", 0);
  202. for (i = 1; i < max_show && is_phys_valid(phys_desc); i++) {
  203. desc = phys_to_virt(phys_desc);
  204. dcmd = desc->dcmd;
  205. burst = dbg_burst_from_dcmd(dcmd);
  206. width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
  207. seq_printf(s, "[%03d] Desc at %08lx(virt %p)\n",
  208. i, phys_desc, desc);
  209. seq_printf(s, "\tDDADR = %08x\n", desc->ddadr);
  210. seq_printf(s, "\tDSADR = %08x\n", desc->dsadr);
  211. seq_printf(s, "\tDTADR = %08x\n", desc->dtadr);
  212. seq_printf(s, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
  213. dcmd,
  214. PXA_DCMD_STR(INCSRCADDR), PXA_DCMD_STR(INCTRGADDR),
  215. PXA_DCMD_STR(FLOWSRC), PXA_DCMD_STR(FLOWTRG),
  216. PXA_DCMD_STR(STARTIRQEN), PXA_DCMD_STR(ENDIRQEN),
  217. PXA_DCMD_STR(ENDIAN), burst, width,
  218. dcmd & PXA_DCMD_LENGTH);
  219. phys_desc = desc->ddadr;
  220. }
  221. if (i == max_show)
  222. seq_printf(s, "[%03d] Desc at %08lx ... max display reached\n",
  223. i, phys_desc);
  224. else
  225. seq_printf(s, "[%03d] Desc at %08lx is %s\n",
  226. i, phys_desc, phys_desc == DDADR_STOP ?
  227. "DDADR_STOP" : "invalid");
  228. return 0;
  229. }
  230. static int dbg_show_chan_state(struct seq_file *s, void *p)
  231. {
  232. struct pxad_phy *phy = s->private;
  233. u32 dcsr, dcmd;
  234. int burst, width;
  235. static const char * const str_prio[] = {
  236. "high", "normal", "low", "invalid"
  237. };
  238. dcsr = _phy_readl_relaxed(phy, DCSR);
  239. dcmd = _phy_readl_relaxed(phy, DCMD);
  240. burst = dbg_burst_from_dcmd(dcmd);
  241. width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
  242. seq_printf(s, "DMA channel %d\n", phy->idx);
  243. seq_printf(s, "\tPriority : %s\n",
  244. str_prio[(phy->idx & 0xf) / 4]);
  245. seq_printf(s, "\tUnaligned transfer bit: %s\n",
  246. _phy_readl_relaxed(phy, DALGN) & BIT(phy->idx) ?
  247. "yes" : "no");
  248. seq_printf(s, "\tDCSR = %08x (%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
  249. dcsr, PXA_DCSR_STR(RUN), PXA_DCSR_STR(NODESC),
  250. PXA_DCSR_STR(STOPIRQEN), PXA_DCSR_STR(EORIRQEN),
  251. PXA_DCSR_STR(EORJMPEN), PXA_DCSR_STR(EORSTOPEN),
  252. PXA_DCSR_STR(SETCMPST), PXA_DCSR_STR(CLRCMPST),
  253. PXA_DCSR_STR(CMPST), PXA_DCSR_STR(EORINTR),
  254. PXA_DCSR_STR(REQPEND), PXA_DCSR_STR(STOPSTATE),
  255. PXA_DCSR_STR(ENDINTR), PXA_DCSR_STR(STARTINTR),
  256. PXA_DCSR_STR(BUSERR));
  257. seq_printf(s, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
  258. dcmd,
  259. PXA_DCMD_STR(INCSRCADDR), PXA_DCMD_STR(INCTRGADDR),
  260. PXA_DCMD_STR(FLOWSRC), PXA_DCMD_STR(FLOWTRG),
  261. PXA_DCMD_STR(STARTIRQEN), PXA_DCMD_STR(ENDIRQEN),
  262. PXA_DCMD_STR(ENDIAN), burst, width, dcmd & PXA_DCMD_LENGTH);
  263. seq_printf(s, "\tDSADR = %08x\n", _phy_readl_relaxed(phy, DSADR));
  264. seq_printf(s, "\tDTADR = %08x\n", _phy_readl_relaxed(phy, DTADR));
  265. seq_printf(s, "\tDDADR = %08x\n", _phy_readl_relaxed(phy, DDADR));
  266. return 0;
  267. }
  268. static int dbg_show_state(struct seq_file *s, void *p)
  269. {
  270. struct pxad_device *pdev = s->private;
  271. /* basic device status */
  272. seq_puts(s, "DMA engine status\n");
  273. seq_printf(s, "\tChannel number: %d\n", pdev->nr_chans);
  274. return 0;
  275. }
  276. #define DBGFS_FUNC_DECL(name) \
  277. static int dbg_open_##name(struct inode *inode, struct file *file) \
  278. { \
  279. return single_open(file, dbg_show_##name, inode->i_private); \
  280. } \
  281. static const struct file_operations dbg_fops_##name = { \
  282. .open = dbg_open_##name, \
  283. .llseek = seq_lseek, \
  284. .read = seq_read, \
  285. .release = single_release, \
  286. }
  287. DBGFS_FUNC_DECL(state);
  288. DBGFS_FUNC_DECL(chan_state);
  289. DBGFS_FUNC_DECL(descriptors);
  290. DBGFS_FUNC_DECL(requester_chan);
  291. static struct dentry *pxad_dbg_alloc_chan(struct pxad_device *pdev,
  292. int ch, struct dentry *chandir)
  293. {
  294. char chan_name[11];
  295. struct dentry *chan, *chan_state = NULL, *chan_descr = NULL;
  296. struct dentry *chan_reqs = NULL;
  297. void *dt;
  298. scnprintf(chan_name, sizeof(chan_name), "%d", ch);
  299. chan = debugfs_create_dir(chan_name, chandir);
  300. dt = (void *)&pdev->phys[ch];
  301. if (chan)
  302. chan_state = debugfs_create_file("state", 0400, chan, dt,
  303. &dbg_fops_chan_state);
  304. if (chan_state)
  305. chan_descr = debugfs_create_file("descriptors", 0400, chan, dt,
  306. &dbg_fops_descriptors);
  307. if (chan_descr)
  308. chan_reqs = debugfs_create_file("requesters", 0400, chan, dt,
  309. &dbg_fops_requester_chan);
  310. if (!chan_reqs)
  311. goto err_state;
  312. return chan;
  313. err_state:
  314. debugfs_remove_recursive(chan);
  315. return NULL;
  316. }
  317. static void pxad_init_debugfs(struct pxad_device *pdev)
  318. {
  319. int i;
  320. struct dentry *chandir;
  321. pdev->dbgfs_root = debugfs_create_dir(dev_name(pdev->slave.dev), NULL);
  322. if (IS_ERR(pdev->dbgfs_root) || !pdev->dbgfs_root)
  323. goto err_root;
  324. pdev->dbgfs_state = debugfs_create_file("state", 0400, pdev->dbgfs_root,
  325. pdev, &dbg_fops_state);
  326. if (!pdev->dbgfs_state)
  327. goto err_state;
  328. pdev->dbgfs_chan =
  329. kmalloc_array(pdev->nr_chans, sizeof(*pdev->dbgfs_state),
  330. GFP_KERNEL);
  331. if (!pdev->dbgfs_chan)
  332. goto err_alloc;
  333. chandir = debugfs_create_dir("channels", pdev->dbgfs_root);
  334. if (!chandir)
  335. goto err_chandir;
  336. for (i = 0; i < pdev->nr_chans; i++) {
  337. pdev->dbgfs_chan[i] = pxad_dbg_alloc_chan(pdev, i, chandir);
  338. if (!pdev->dbgfs_chan[i])
  339. goto err_chans;
  340. }
  341. return;
  342. err_chans:
  343. err_chandir:
  344. kfree(pdev->dbgfs_chan);
  345. err_alloc:
  346. err_state:
  347. debugfs_remove_recursive(pdev->dbgfs_root);
  348. err_root:
  349. pr_err("pxad: debugfs is not available\n");
  350. }
  351. static void pxad_cleanup_debugfs(struct pxad_device *pdev)
  352. {
  353. debugfs_remove_recursive(pdev->dbgfs_root);
  354. }
  355. #else
  356. static inline void pxad_init_debugfs(struct pxad_device *pdev) {}
  357. static inline void pxad_cleanup_debugfs(struct pxad_device *pdev) {}
  358. #endif
  359. /*
  360. * In the transition phase where legacy pxa handling is done at the same time as
  361. * mmp_dma, the DMA physical channel split between the 2 DMA providers is done
  362. * through legacy_reserved. Legacy code reserves DMA channels by settings
  363. * corresponding bits in legacy_reserved.
  364. */
  365. static u32 legacy_reserved;
  366. static u32 legacy_unavailable;
  367. static struct pxad_phy *lookup_phy(struct pxad_chan *pchan)
  368. {
  369. int prio, i;
  370. struct pxad_device *pdev = to_pxad_dev(pchan->vc.chan.device);
  371. struct pxad_phy *phy, *found = NULL;
  372. unsigned long flags;
  373. /*
  374. * dma channel priorities
  375. * ch 0 - 3, 16 - 19 <--> (0)
  376. * ch 4 - 7, 20 - 23 <--> (1)
  377. * ch 8 - 11, 24 - 27 <--> (2)
  378. * ch 12 - 15, 28 - 31 <--> (3)
  379. */
  380. spin_lock_irqsave(&pdev->phy_lock, flags);
  381. for (prio = pchan->prio; prio >= PXAD_PRIO_HIGHEST; prio--) {
  382. for (i = 0; i < pdev->nr_chans; i++) {
  383. if (prio != (i & 0xf) >> 2)
  384. continue;
  385. if ((i < 32) && (legacy_reserved & BIT(i)))
  386. continue;
  387. phy = &pdev->phys[i];
  388. if (!phy->vchan) {
  389. phy->vchan = pchan;
  390. found = phy;
  391. if (i < 32)
  392. legacy_unavailable |= BIT(i);
  393. goto out_unlock;
  394. }
  395. }
  396. }
  397. out_unlock:
  398. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  399. dev_dbg(&pchan->vc.chan.dev->device,
  400. "%s(): phy=%p(%d)\n", __func__, found,
  401. found ? found->idx : -1);
  402. return found;
  403. }
  404. static void pxad_free_phy(struct pxad_chan *chan)
  405. {
  406. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  407. unsigned long flags;
  408. u32 reg;
  409. int i;
  410. dev_dbg(&chan->vc.chan.dev->device,
  411. "%s(): freeing\n", __func__);
  412. if (!chan->phy)
  413. return;
  414. /* clear the channel mapping in DRCMR */
  415. if (chan->drcmr <= pdev->nr_requestors) {
  416. reg = pxad_drcmr(chan->drcmr);
  417. writel_relaxed(0, chan->phy->base + reg);
  418. }
  419. spin_lock_irqsave(&pdev->phy_lock, flags);
  420. for (i = 0; i < 32; i++)
  421. if (chan->phy == &pdev->phys[i])
  422. legacy_unavailable &= ~BIT(i);
  423. chan->phy->vchan = NULL;
  424. chan->phy = NULL;
  425. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  426. }
  427. static bool is_chan_running(struct pxad_chan *chan)
  428. {
  429. u32 dcsr;
  430. struct pxad_phy *phy = chan->phy;
  431. if (!phy)
  432. return false;
  433. dcsr = phy_readl_relaxed(phy, DCSR);
  434. return dcsr & PXA_DCSR_RUN;
  435. }
  436. static bool is_running_chan_misaligned(struct pxad_chan *chan)
  437. {
  438. u32 dalgn;
  439. BUG_ON(!chan->phy);
  440. dalgn = phy_readl_relaxed(chan->phy, DALGN);
  441. return dalgn & (BIT(chan->phy->idx));
  442. }
  443. static void phy_enable(struct pxad_phy *phy, bool misaligned)
  444. {
  445. struct pxad_device *pdev;
  446. u32 reg, dalgn;
  447. if (!phy->vchan)
  448. return;
  449. dev_dbg(&phy->vchan->vc.chan.dev->device,
  450. "%s(); phy=%p(%d) misaligned=%d\n", __func__,
  451. phy, phy->idx, misaligned);
  452. pdev = to_pxad_dev(phy->vchan->vc.chan.device);
  453. if (phy->vchan->drcmr <= pdev->nr_requestors) {
  454. reg = pxad_drcmr(phy->vchan->drcmr);
  455. writel_relaxed(DRCMR_MAPVLD | phy->idx, phy->base + reg);
  456. }
  457. dalgn = phy_readl_relaxed(phy, DALGN);
  458. if (misaligned)
  459. dalgn |= BIT(phy->idx);
  460. else
  461. dalgn &= ~BIT(phy->idx);
  462. phy_writel_relaxed(phy, dalgn, DALGN);
  463. phy_writel(phy, PXA_DCSR_STOPIRQEN | PXA_DCSR_ENDINTR |
  464. PXA_DCSR_BUSERR | PXA_DCSR_RUN, DCSR);
  465. }
  466. static void phy_disable(struct pxad_phy *phy)
  467. {
  468. u32 dcsr;
  469. if (!phy)
  470. return;
  471. dcsr = phy_readl_relaxed(phy, DCSR);
  472. dev_dbg(&phy->vchan->vc.chan.dev->device,
  473. "%s(): phy=%p(%d)\n", __func__, phy, phy->idx);
  474. phy_writel(phy, dcsr & ~PXA_DCSR_RUN & ~PXA_DCSR_STOPIRQEN, DCSR);
  475. }
  476. static void pxad_launch_chan(struct pxad_chan *chan,
  477. struct pxad_desc_sw *desc)
  478. {
  479. dev_dbg(&chan->vc.chan.dev->device,
  480. "%s(): desc=%p\n", __func__, desc);
  481. if (!chan->phy) {
  482. chan->phy = lookup_phy(chan);
  483. if (!chan->phy) {
  484. dev_dbg(&chan->vc.chan.dev->device,
  485. "%s(): no free dma channel\n", __func__);
  486. return;
  487. }
  488. }
  489. chan->bus_error = 0;
  490. /*
  491. * Program the descriptor's address into the DMA controller,
  492. * then start the DMA transaction
  493. */
  494. phy_writel(chan->phy, desc->first, DDADR);
  495. phy_enable(chan->phy, chan->misaligned);
  496. wake_up(&chan->wq_state);
  497. }
  498. static void set_updater_desc(struct pxad_desc_sw *sw_desc,
  499. unsigned long flags)
  500. {
  501. struct pxad_desc_hw *updater =
  502. sw_desc->hw_desc[sw_desc->nb_desc - 1];
  503. dma_addr_t dma = sw_desc->hw_desc[sw_desc->nb_desc - 2]->ddadr;
  504. updater->ddadr = DDADR_STOP;
  505. updater->dsadr = dma;
  506. updater->dtadr = dma + 8;
  507. updater->dcmd = PXA_DCMD_WIDTH4 | PXA_DCMD_BURST32 |
  508. (PXA_DCMD_LENGTH & sizeof(u32));
  509. if (flags & DMA_PREP_INTERRUPT)
  510. updater->dcmd |= PXA_DCMD_ENDIRQEN;
  511. if (sw_desc->cyclic)
  512. sw_desc->hw_desc[sw_desc->nb_desc - 2]->ddadr = sw_desc->first;
  513. }
  514. static bool is_desc_completed(struct virt_dma_desc *vd)
  515. {
  516. struct pxad_desc_sw *sw_desc = to_pxad_sw_desc(vd);
  517. struct pxad_desc_hw *updater =
  518. sw_desc->hw_desc[sw_desc->nb_desc - 1];
  519. return updater->dtadr != (updater->dsadr + 8);
  520. }
  521. static void pxad_desc_chain(struct virt_dma_desc *vd1,
  522. struct virt_dma_desc *vd2)
  523. {
  524. struct pxad_desc_sw *desc1 = to_pxad_sw_desc(vd1);
  525. struct pxad_desc_sw *desc2 = to_pxad_sw_desc(vd2);
  526. dma_addr_t dma_to_chain;
  527. dma_to_chain = desc2->first;
  528. desc1->hw_desc[desc1->nb_desc - 1]->ddadr = dma_to_chain;
  529. }
  530. static bool pxad_try_hotchain(struct virt_dma_chan *vc,
  531. struct virt_dma_desc *vd)
  532. {
  533. struct virt_dma_desc *vd_last_issued = NULL;
  534. struct pxad_chan *chan = to_pxad_chan(&vc->chan);
  535. /*
  536. * Attempt to hot chain the tx if the phy is still running. This is
  537. * considered successful only if either the channel is still running
  538. * after the chaining, or if the chained transfer is completed after
  539. * having been hot chained.
  540. * A change of alignment is not allowed, and forbids hotchaining.
  541. */
  542. if (is_chan_running(chan)) {
  543. BUG_ON(list_empty(&vc->desc_issued));
  544. if (!is_running_chan_misaligned(chan) &&
  545. to_pxad_sw_desc(vd)->misaligned)
  546. return false;
  547. vd_last_issued = list_entry(vc->desc_issued.prev,
  548. struct virt_dma_desc, node);
  549. pxad_desc_chain(vd_last_issued, vd);
  550. if (is_chan_running(chan) || is_desc_completed(vd_last_issued))
  551. return true;
  552. }
  553. return false;
  554. }
  555. static unsigned int clear_chan_irq(struct pxad_phy *phy)
  556. {
  557. u32 dcsr;
  558. u32 dint = readl(phy->base + DINT);
  559. if (!(dint & BIT(phy->idx)))
  560. return PXA_DCSR_RUN;
  561. /* clear irq */
  562. dcsr = phy_readl_relaxed(phy, DCSR);
  563. phy_writel(phy, dcsr, DCSR);
  564. if ((dcsr & PXA_DCSR_BUSERR) && (phy->vchan))
  565. dev_warn(&phy->vchan->vc.chan.dev->device,
  566. "%s(chan=%p): PXA_DCSR_BUSERR\n",
  567. __func__, &phy->vchan);
  568. return dcsr & ~PXA_DCSR_RUN;
  569. }
  570. static irqreturn_t pxad_chan_handler(int irq, void *dev_id)
  571. {
  572. struct pxad_phy *phy = dev_id;
  573. struct pxad_chan *chan = phy->vchan;
  574. struct virt_dma_desc *vd, *tmp;
  575. unsigned int dcsr;
  576. unsigned long flags;
  577. dma_cookie_t last_started = 0;
  578. BUG_ON(!chan);
  579. dcsr = clear_chan_irq(phy);
  580. if (dcsr & PXA_DCSR_RUN)
  581. return IRQ_NONE;
  582. spin_lock_irqsave(&chan->vc.lock, flags);
  583. list_for_each_entry_safe(vd, tmp, &chan->vc.desc_issued, node) {
  584. dev_dbg(&chan->vc.chan.dev->device,
  585. "%s(): checking txd %p[%x]: completed=%d\n",
  586. __func__, vd, vd->tx.cookie, is_desc_completed(vd));
  587. last_started = vd->tx.cookie;
  588. if (to_pxad_sw_desc(vd)->cyclic) {
  589. vchan_cyclic_callback(vd);
  590. break;
  591. }
  592. if (is_desc_completed(vd)) {
  593. list_del(&vd->node);
  594. vchan_cookie_complete(vd);
  595. } else {
  596. break;
  597. }
  598. }
  599. if (dcsr & PXA_DCSR_BUSERR) {
  600. chan->bus_error = last_started;
  601. phy_disable(phy);
  602. }
  603. if (!chan->bus_error && dcsr & PXA_DCSR_STOPSTATE) {
  604. dev_dbg(&chan->vc.chan.dev->device,
  605. "%s(): channel stopped, submitted_empty=%d issued_empty=%d",
  606. __func__,
  607. list_empty(&chan->vc.desc_submitted),
  608. list_empty(&chan->vc.desc_issued));
  609. phy_writel_relaxed(phy, dcsr & ~PXA_DCSR_STOPIRQEN, DCSR);
  610. if (list_empty(&chan->vc.desc_issued)) {
  611. chan->misaligned =
  612. !list_empty(&chan->vc.desc_submitted);
  613. } else {
  614. vd = list_first_entry(&chan->vc.desc_issued,
  615. struct virt_dma_desc, node);
  616. pxad_launch_chan(chan, to_pxad_sw_desc(vd));
  617. }
  618. }
  619. spin_unlock_irqrestore(&chan->vc.lock, flags);
  620. wake_up(&chan->wq_state);
  621. return IRQ_HANDLED;
  622. }
  623. static irqreturn_t pxad_int_handler(int irq, void *dev_id)
  624. {
  625. struct pxad_device *pdev = dev_id;
  626. struct pxad_phy *phy;
  627. u32 dint = readl(pdev->base + DINT);
  628. int i, ret = IRQ_NONE;
  629. while (dint) {
  630. i = __ffs(dint);
  631. dint &= (dint - 1);
  632. phy = &pdev->phys[i];
  633. if ((i < 32) && (legacy_reserved & BIT(i)))
  634. continue;
  635. if (pxad_chan_handler(irq, phy) == IRQ_HANDLED)
  636. ret = IRQ_HANDLED;
  637. }
  638. return ret;
  639. }
  640. static int pxad_alloc_chan_resources(struct dma_chan *dchan)
  641. {
  642. struct pxad_chan *chan = to_pxad_chan(dchan);
  643. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  644. if (chan->desc_pool)
  645. return 1;
  646. chan->desc_pool = dma_pool_create(dma_chan_name(dchan),
  647. pdev->slave.dev,
  648. sizeof(struct pxad_desc_hw),
  649. __alignof__(struct pxad_desc_hw),
  650. 0);
  651. if (!chan->desc_pool) {
  652. dev_err(&chan->vc.chan.dev->device,
  653. "%s(): unable to allocate descriptor pool\n",
  654. __func__);
  655. return -ENOMEM;
  656. }
  657. return 1;
  658. }
  659. static void pxad_free_chan_resources(struct dma_chan *dchan)
  660. {
  661. struct pxad_chan *chan = to_pxad_chan(dchan);
  662. vchan_free_chan_resources(&chan->vc);
  663. dma_pool_destroy(chan->desc_pool);
  664. chan->desc_pool = NULL;
  665. }
  666. static void pxad_free_desc(struct virt_dma_desc *vd)
  667. {
  668. int i;
  669. dma_addr_t dma;
  670. struct pxad_desc_sw *sw_desc = to_pxad_sw_desc(vd);
  671. BUG_ON(sw_desc->nb_desc == 0);
  672. for (i = sw_desc->nb_desc - 1; i >= 0; i--) {
  673. if (i > 0)
  674. dma = sw_desc->hw_desc[i - 1]->ddadr;
  675. else
  676. dma = sw_desc->first;
  677. dma_pool_free(sw_desc->desc_pool,
  678. sw_desc->hw_desc[i], dma);
  679. }
  680. sw_desc->nb_desc = 0;
  681. kfree(sw_desc);
  682. }
  683. static struct pxad_desc_sw *
  684. pxad_alloc_desc(struct pxad_chan *chan, unsigned int nb_hw_desc)
  685. {
  686. struct pxad_desc_sw *sw_desc;
  687. dma_addr_t dma;
  688. int i;
  689. sw_desc = kzalloc(sizeof(*sw_desc) +
  690. nb_hw_desc * sizeof(struct pxad_desc_hw *),
  691. GFP_NOWAIT);
  692. if (!sw_desc)
  693. return NULL;
  694. sw_desc->desc_pool = chan->desc_pool;
  695. for (i = 0; i < nb_hw_desc; i++) {
  696. sw_desc->hw_desc[i] = dma_pool_alloc(sw_desc->desc_pool,
  697. GFP_NOWAIT, &dma);
  698. if (!sw_desc->hw_desc[i]) {
  699. dev_err(&chan->vc.chan.dev->device,
  700. "%s(): Couldn't allocate the %dth hw_desc from dma_pool %p\n",
  701. __func__, i, sw_desc->desc_pool);
  702. goto err;
  703. }
  704. if (i == 0)
  705. sw_desc->first = dma;
  706. else
  707. sw_desc->hw_desc[i - 1]->ddadr = dma;
  708. sw_desc->nb_desc++;
  709. }
  710. return sw_desc;
  711. err:
  712. pxad_free_desc(&sw_desc->vd);
  713. return NULL;
  714. }
  715. static dma_cookie_t pxad_tx_submit(struct dma_async_tx_descriptor *tx)
  716. {
  717. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  718. struct pxad_chan *chan = to_pxad_chan(&vc->chan);
  719. struct virt_dma_desc *vd_chained = NULL,
  720. *vd = container_of(tx, struct virt_dma_desc, tx);
  721. dma_cookie_t cookie;
  722. unsigned long flags;
  723. set_updater_desc(to_pxad_sw_desc(vd), tx->flags);
  724. spin_lock_irqsave(&vc->lock, flags);
  725. cookie = dma_cookie_assign(tx);
  726. if (list_empty(&vc->desc_submitted) && pxad_try_hotchain(vc, vd)) {
  727. list_move_tail(&vd->node, &vc->desc_issued);
  728. dev_dbg(&chan->vc.chan.dev->device,
  729. "%s(): txd %p[%x]: submitted (hot linked)\n",
  730. __func__, vd, cookie);
  731. goto out;
  732. }
  733. /*
  734. * Fallback to placing the tx in the submitted queue
  735. */
  736. if (!list_empty(&vc->desc_submitted)) {
  737. vd_chained = list_entry(vc->desc_submitted.prev,
  738. struct virt_dma_desc, node);
  739. /*
  740. * Only chain the descriptors if no new misalignment is
  741. * introduced. If a new misalignment is chained, let the channel
  742. * stop, and be relaunched in misalign mode from the irq
  743. * handler.
  744. */
  745. if (chan->misaligned || !to_pxad_sw_desc(vd)->misaligned)
  746. pxad_desc_chain(vd_chained, vd);
  747. else
  748. vd_chained = NULL;
  749. }
  750. dev_dbg(&chan->vc.chan.dev->device,
  751. "%s(): txd %p[%x]: submitted (%s linked)\n",
  752. __func__, vd, cookie, vd_chained ? "cold" : "not");
  753. list_move_tail(&vd->node, &vc->desc_submitted);
  754. chan->misaligned |= to_pxad_sw_desc(vd)->misaligned;
  755. out:
  756. spin_unlock_irqrestore(&vc->lock, flags);
  757. return cookie;
  758. }
  759. static void pxad_issue_pending(struct dma_chan *dchan)
  760. {
  761. struct pxad_chan *chan = to_pxad_chan(dchan);
  762. struct virt_dma_desc *vd_first;
  763. unsigned long flags;
  764. spin_lock_irqsave(&chan->vc.lock, flags);
  765. if (list_empty(&chan->vc.desc_submitted))
  766. goto out;
  767. vd_first = list_first_entry(&chan->vc.desc_submitted,
  768. struct virt_dma_desc, node);
  769. dev_dbg(&chan->vc.chan.dev->device,
  770. "%s(): txd %p[%x]", __func__, vd_first, vd_first->tx.cookie);
  771. vchan_issue_pending(&chan->vc);
  772. if (!pxad_try_hotchain(&chan->vc, vd_first))
  773. pxad_launch_chan(chan, to_pxad_sw_desc(vd_first));
  774. out:
  775. spin_unlock_irqrestore(&chan->vc.lock, flags);
  776. }
  777. static inline struct dma_async_tx_descriptor *
  778. pxad_tx_prep(struct virt_dma_chan *vc, struct virt_dma_desc *vd,
  779. unsigned long tx_flags)
  780. {
  781. struct dma_async_tx_descriptor *tx;
  782. struct pxad_chan *chan = container_of(vc, struct pxad_chan, vc);
  783. INIT_LIST_HEAD(&vd->node);
  784. tx = vchan_tx_prep(vc, vd, tx_flags);
  785. tx->tx_submit = pxad_tx_submit;
  786. dev_dbg(&chan->vc.chan.dev->device,
  787. "%s(): vc=%p txd=%p[%x] flags=0x%lx\n", __func__,
  788. vc, vd, vd->tx.cookie,
  789. tx_flags);
  790. return tx;
  791. }
  792. static void pxad_get_config(struct pxad_chan *chan,
  793. enum dma_transfer_direction dir,
  794. u32 *dcmd, u32 *dev_src, u32 *dev_dst)
  795. {
  796. u32 maxburst = 0, dev_addr = 0;
  797. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  798. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  799. *dcmd = 0;
  800. if (dir == DMA_DEV_TO_MEM) {
  801. maxburst = chan->cfg.src_maxburst;
  802. width = chan->cfg.src_addr_width;
  803. dev_addr = chan->cfg.src_addr;
  804. *dev_src = dev_addr;
  805. *dcmd |= PXA_DCMD_INCTRGADDR;
  806. if (chan->drcmr <= pdev->nr_requestors)
  807. *dcmd |= PXA_DCMD_FLOWSRC;
  808. }
  809. if (dir == DMA_MEM_TO_DEV) {
  810. maxburst = chan->cfg.dst_maxburst;
  811. width = chan->cfg.dst_addr_width;
  812. dev_addr = chan->cfg.dst_addr;
  813. *dev_dst = dev_addr;
  814. *dcmd |= PXA_DCMD_INCSRCADDR;
  815. if (chan->drcmr <= pdev->nr_requestors)
  816. *dcmd |= PXA_DCMD_FLOWTRG;
  817. }
  818. if (dir == DMA_MEM_TO_MEM)
  819. *dcmd |= PXA_DCMD_BURST32 | PXA_DCMD_INCTRGADDR |
  820. PXA_DCMD_INCSRCADDR;
  821. dev_dbg(&chan->vc.chan.dev->device,
  822. "%s(): dev_addr=0x%x maxburst=%d width=%d dir=%d\n",
  823. __func__, dev_addr, maxburst, width, dir);
  824. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  825. *dcmd |= PXA_DCMD_WIDTH1;
  826. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  827. *dcmd |= PXA_DCMD_WIDTH2;
  828. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  829. *dcmd |= PXA_DCMD_WIDTH4;
  830. if (maxburst == 8)
  831. *dcmd |= PXA_DCMD_BURST8;
  832. else if (maxburst == 16)
  833. *dcmd |= PXA_DCMD_BURST16;
  834. else if (maxburst == 32)
  835. *dcmd |= PXA_DCMD_BURST32;
  836. /* FIXME: drivers should be ported over to use the filter
  837. * function. Once that's done, the following two lines can
  838. * be removed.
  839. */
  840. if (chan->cfg.slave_id)
  841. chan->drcmr = chan->cfg.slave_id;
  842. }
  843. static struct dma_async_tx_descriptor *
  844. pxad_prep_memcpy(struct dma_chan *dchan,
  845. dma_addr_t dma_dst, dma_addr_t dma_src,
  846. size_t len, unsigned long flags)
  847. {
  848. struct pxad_chan *chan = to_pxad_chan(dchan);
  849. struct pxad_desc_sw *sw_desc;
  850. struct pxad_desc_hw *hw_desc;
  851. u32 dcmd;
  852. unsigned int i, nb_desc = 0;
  853. size_t copy;
  854. if (!dchan || !len)
  855. return NULL;
  856. dev_dbg(&chan->vc.chan.dev->device,
  857. "%s(): dma_dst=0x%lx dma_src=0x%lx len=%zu flags=%lx\n",
  858. __func__, (unsigned long)dma_dst, (unsigned long)dma_src,
  859. len, flags);
  860. pxad_get_config(chan, DMA_MEM_TO_MEM, &dcmd, NULL, NULL);
  861. nb_desc = DIV_ROUND_UP(len, PDMA_MAX_DESC_BYTES);
  862. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  863. if (!sw_desc)
  864. return NULL;
  865. sw_desc->len = len;
  866. if (!IS_ALIGNED(dma_src, 1 << PDMA_ALIGNMENT) ||
  867. !IS_ALIGNED(dma_dst, 1 << PDMA_ALIGNMENT))
  868. sw_desc->misaligned = true;
  869. i = 0;
  870. do {
  871. hw_desc = sw_desc->hw_desc[i++];
  872. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  873. hw_desc->dcmd = dcmd | (PXA_DCMD_LENGTH & copy);
  874. hw_desc->dsadr = dma_src;
  875. hw_desc->dtadr = dma_dst;
  876. len -= copy;
  877. dma_src += copy;
  878. dma_dst += copy;
  879. } while (len);
  880. set_updater_desc(sw_desc, flags);
  881. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  882. }
  883. static struct dma_async_tx_descriptor *
  884. pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  885. unsigned int sg_len, enum dma_transfer_direction dir,
  886. unsigned long flags, void *context)
  887. {
  888. struct pxad_chan *chan = to_pxad_chan(dchan);
  889. struct pxad_desc_sw *sw_desc;
  890. size_t len, avail;
  891. struct scatterlist *sg;
  892. dma_addr_t dma;
  893. u32 dcmd, dsadr = 0, dtadr = 0;
  894. unsigned int nb_desc = 0, i, j = 0;
  895. if ((sgl == NULL) || (sg_len == 0))
  896. return NULL;
  897. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  898. dev_dbg(&chan->vc.chan.dev->device,
  899. "%s(): dir=%d flags=%lx\n", __func__, dir, flags);
  900. for_each_sg(sgl, sg, sg_len, i)
  901. nb_desc += DIV_ROUND_UP(sg_dma_len(sg), PDMA_MAX_DESC_BYTES);
  902. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  903. if (!sw_desc)
  904. return NULL;
  905. for_each_sg(sgl, sg, sg_len, i) {
  906. dma = sg_dma_address(sg);
  907. avail = sg_dma_len(sg);
  908. sw_desc->len += avail;
  909. do {
  910. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  911. if (dma & 0x7)
  912. sw_desc->misaligned = true;
  913. sw_desc->hw_desc[j]->dcmd =
  914. dcmd | (PXA_DCMD_LENGTH & len);
  915. sw_desc->hw_desc[j]->dsadr = dsadr ? dsadr : dma;
  916. sw_desc->hw_desc[j++]->dtadr = dtadr ? dtadr : dma;
  917. dma += len;
  918. avail -= len;
  919. } while (avail);
  920. }
  921. set_updater_desc(sw_desc, flags);
  922. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  923. }
  924. static struct dma_async_tx_descriptor *
  925. pxad_prep_dma_cyclic(struct dma_chan *dchan,
  926. dma_addr_t buf_addr, size_t len, size_t period_len,
  927. enum dma_transfer_direction dir, unsigned long flags)
  928. {
  929. struct pxad_chan *chan = to_pxad_chan(dchan);
  930. struct pxad_desc_sw *sw_desc;
  931. struct pxad_desc_hw **phw_desc;
  932. dma_addr_t dma;
  933. u32 dcmd, dsadr = 0, dtadr = 0;
  934. unsigned int nb_desc = 0;
  935. if (!dchan || !len || !period_len)
  936. return NULL;
  937. if ((dir != DMA_DEV_TO_MEM) && (dir != DMA_MEM_TO_DEV)) {
  938. dev_err(&chan->vc.chan.dev->device,
  939. "Unsupported direction for cyclic DMA\n");
  940. return NULL;
  941. }
  942. /* the buffer length must be a multiple of period_len */
  943. if (len % period_len != 0 || period_len > PDMA_MAX_DESC_BYTES ||
  944. !IS_ALIGNED(period_len, 1 << PDMA_ALIGNMENT))
  945. return NULL;
  946. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  947. dcmd |= PXA_DCMD_ENDIRQEN | (PXA_DCMD_LENGTH & period_len);
  948. dev_dbg(&chan->vc.chan.dev->device,
  949. "%s(): buf_addr=0x%lx len=%zu period=%zu dir=%d flags=%lx\n",
  950. __func__, (unsigned long)buf_addr, len, period_len, dir, flags);
  951. nb_desc = DIV_ROUND_UP(period_len, PDMA_MAX_DESC_BYTES);
  952. nb_desc *= DIV_ROUND_UP(len, period_len);
  953. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  954. if (!sw_desc)
  955. return NULL;
  956. sw_desc->cyclic = true;
  957. sw_desc->len = len;
  958. phw_desc = sw_desc->hw_desc;
  959. dma = buf_addr;
  960. do {
  961. phw_desc[0]->dsadr = dsadr ? dsadr : dma;
  962. phw_desc[0]->dtadr = dtadr ? dtadr : dma;
  963. phw_desc[0]->dcmd = dcmd;
  964. phw_desc++;
  965. dma += period_len;
  966. len -= period_len;
  967. } while (len);
  968. set_updater_desc(sw_desc, flags);
  969. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  970. }
  971. static int pxad_config(struct dma_chan *dchan,
  972. struct dma_slave_config *cfg)
  973. {
  974. struct pxad_chan *chan = to_pxad_chan(dchan);
  975. if (!dchan)
  976. return -EINVAL;
  977. chan->cfg = *cfg;
  978. return 0;
  979. }
  980. static int pxad_terminate_all(struct dma_chan *dchan)
  981. {
  982. struct pxad_chan *chan = to_pxad_chan(dchan);
  983. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  984. struct virt_dma_desc *vd = NULL;
  985. unsigned long flags;
  986. struct pxad_phy *phy;
  987. LIST_HEAD(head);
  988. dev_dbg(&chan->vc.chan.dev->device,
  989. "%s(): vchan %p: terminate all\n", __func__, &chan->vc);
  990. spin_lock_irqsave(&chan->vc.lock, flags);
  991. vchan_get_all_descriptors(&chan->vc, &head);
  992. list_for_each_entry(vd, &head, node) {
  993. dev_dbg(&chan->vc.chan.dev->device,
  994. "%s(): cancelling txd %p[%x] (completed=%d)", __func__,
  995. vd, vd->tx.cookie, is_desc_completed(vd));
  996. }
  997. phy = chan->phy;
  998. if (phy) {
  999. phy_disable(chan->phy);
  1000. pxad_free_phy(chan);
  1001. chan->phy = NULL;
  1002. spin_lock(&pdev->phy_lock);
  1003. phy->vchan = NULL;
  1004. spin_unlock(&pdev->phy_lock);
  1005. }
  1006. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1007. vchan_dma_desc_free_list(&chan->vc, &head);
  1008. return 0;
  1009. }
  1010. static unsigned int pxad_residue(struct pxad_chan *chan,
  1011. dma_cookie_t cookie)
  1012. {
  1013. struct virt_dma_desc *vd = NULL;
  1014. struct pxad_desc_sw *sw_desc = NULL;
  1015. struct pxad_desc_hw *hw_desc = NULL;
  1016. u32 curr, start, len, end, residue = 0;
  1017. unsigned long flags;
  1018. bool passed = false;
  1019. int i;
  1020. /*
  1021. * If the channel does not have a phy pointer anymore, it has already
  1022. * been completed. Therefore, its residue is 0.
  1023. */
  1024. if (!chan->phy)
  1025. return 0;
  1026. spin_lock_irqsave(&chan->vc.lock, flags);
  1027. vd = vchan_find_desc(&chan->vc, cookie);
  1028. if (!vd)
  1029. goto out;
  1030. sw_desc = to_pxad_sw_desc(vd);
  1031. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  1032. curr = phy_readl_relaxed(chan->phy, DSADR);
  1033. else
  1034. curr = phy_readl_relaxed(chan->phy, DTADR);
  1035. /*
  1036. * curr has to be actually read before checking descriptor
  1037. * completion, so that a curr inside a status updater
  1038. * descriptor implies the following test returns true, and
  1039. * preventing reordering of curr load and the test.
  1040. */
  1041. rmb();
  1042. if (is_desc_completed(vd))
  1043. goto out;
  1044. for (i = 0; i < sw_desc->nb_desc - 1; i++) {
  1045. hw_desc = sw_desc->hw_desc[i];
  1046. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  1047. start = hw_desc->dsadr;
  1048. else
  1049. start = hw_desc->dtadr;
  1050. len = hw_desc->dcmd & PXA_DCMD_LENGTH;
  1051. end = start + len;
  1052. /*
  1053. * 'passed' will be latched once we found the descriptor
  1054. * which lies inside the boundaries of the curr
  1055. * pointer. All descriptors that occur in the list
  1056. * _after_ we found that partially handled descriptor
  1057. * are still to be processed and are hence added to the
  1058. * residual bytes counter.
  1059. */
  1060. if (passed) {
  1061. residue += len;
  1062. } else if (curr >= start && curr <= end) {
  1063. residue += end - curr;
  1064. passed = true;
  1065. }
  1066. }
  1067. if (!passed)
  1068. residue = sw_desc->len;
  1069. out:
  1070. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1071. dev_dbg(&chan->vc.chan.dev->device,
  1072. "%s(): txd %p[%x] sw_desc=%p: %d\n",
  1073. __func__, vd, cookie, sw_desc, residue);
  1074. return residue;
  1075. }
  1076. static enum dma_status pxad_tx_status(struct dma_chan *dchan,
  1077. dma_cookie_t cookie,
  1078. struct dma_tx_state *txstate)
  1079. {
  1080. struct pxad_chan *chan = to_pxad_chan(dchan);
  1081. enum dma_status ret;
  1082. if (cookie == chan->bus_error)
  1083. return DMA_ERROR;
  1084. ret = dma_cookie_status(dchan, cookie, txstate);
  1085. if (likely(txstate && (ret != DMA_ERROR)))
  1086. dma_set_residue(txstate, pxad_residue(chan, cookie));
  1087. return ret;
  1088. }
  1089. static void pxad_synchronize(struct dma_chan *dchan)
  1090. {
  1091. struct pxad_chan *chan = to_pxad_chan(dchan);
  1092. wait_event(chan->wq_state, !is_chan_running(chan));
  1093. vchan_synchronize(&chan->vc);
  1094. }
  1095. static void pxad_free_channels(struct dma_device *dmadev)
  1096. {
  1097. struct pxad_chan *c, *cn;
  1098. list_for_each_entry_safe(c, cn, &dmadev->channels,
  1099. vc.chan.device_node) {
  1100. list_del(&c->vc.chan.device_node);
  1101. tasklet_kill(&c->vc.task);
  1102. }
  1103. }
  1104. static int pxad_remove(struct platform_device *op)
  1105. {
  1106. struct pxad_device *pdev = platform_get_drvdata(op);
  1107. pxad_cleanup_debugfs(pdev);
  1108. pxad_free_channels(&pdev->slave);
  1109. dma_async_device_unregister(&pdev->slave);
  1110. return 0;
  1111. }
  1112. static int pxad_init_phys(struct platform_device *op,
  1113. struct pxad_device *pdev,
  1114. unsigned int nb_phy_chans)
  1115. {
  1116. int irq0, irq, nr_irq = 0, i, ret;
  1117. struct pxad_phy *phy;
  1118. irq0 = platform_get_irq(op, 0);
  1119. if (irq0 < 0)
  1120. return irq0;
  1121. pdev->phys = devm_kcalloc(&op->dev, nb_phy_chans,
  1122. sizeof(pdev->phys[0]), GFP_KERNEL);
  1123. if (!pdev->phys)
  1124. return -ENOMEM;
  1125. for (i = 0; i < nb_phy_chans; i++)
  1126. if (platform_get_irq(op, i) > 0)
  1127. nr_irq++;
  1128. for (i = 0; i < nb_phy_chans; i++) {
  1129. phy = &pdev->phys[i];
  1130. phy->base = pdev->base;
  1131. phy->idx = i;
  1132. irq = platform_get_irq(op, i);
  1133. if ((nr_irq > 1) && (irq > 0))
  1134. ret = devm_request_irq(&op->dev, irq,
  1135. pxad_chan_handler,
  1136. IRQF_SHARED, "pxa-dma", phy);
  1137. if ((nr_irq == 1) && (i == 0))
  1138. ret = devm_request_irq(&op->dev, irq0,
  1139. pxad_int_handler,
  1140. IRQF_SHARED, "pxa-dma", pdev);
  1141. if (ret) {
  1142. dev_err(pdev->slave.dev,
  1143. "%s(): can't request irq %d:%d\n", __func__,
  1144. irq, ret);
  1145. return ret;
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. static const struct of_device_id pxad_dt_ids[] = {
  1151. { .compatible = "marvell,pdma-1.0", },
  1152. {}
  1153. };
  1154. MODULE_DEVICE_TABLE(of, pxad_dt_ids);
  1155. static struct dma_chan *pxad_dma_xlate(struct of_phandle_args *dma_spec,
  1156. struct of_dma *ofdma)
  1157. {
  1158. struct pxad_device *d = ofdma->of_dma_data;
  1159. struct dma_chan *chan;
  1160. chan = dma_get_any_slave_channel(&d->slave);
  1161. if (!chan)
  1162. return NULL;
  1163. to_pxad_chan(chan)->drcmr = dma_spec->args[0];
  1164. to_pxad_chan(chan)->prio = dma_spec->args[1];
  1165. return chan;
  1166. }
  1167. static int pxad_init_dmadev(struct platform_device *op,
  1168. struct pxad_device *pdev,
  1169. unsigned int nr_phy_chans,
  1170. unsigned int nr_requestors)
  1171. {
  1172. int ret;
  1173. unsigned int i;
  1174. struct pxad_chan *c;
  1175. pdev->nr_chans = nr_phy_chans;
  1176. pdev->nr_requestors = nr_requestors;
  1177. INIT_LIST_HEAD(&pdev->slave.channels);
  1178. pdev->slave.device_alloc_chan_resources = pxad_alloc_chan_resources;
  1179. pdev->slave.device_free_chan_resources = pxad_free_chan_resources;
  1180. pdev->slave.device_tx_status = pxad_tx_status;
  1181. pdev->slave.device_issue_pending = pxad_issue_pending;
  1182. pdev->slave.device_config = pxad_config;
  1183. pdev->slave.device_synchronize = pxad_synchronize;
  1184. pdev->slave.device_terminate_all = pxad_terminate_all;
  1185. if (op->dev.coherent_dma_mask)
  1186. dma_set_mask(&op->dev, op->dev.coherent_dma_mask);
  1187. else
  1188. dma_set_mask(&op->dev, DMA_BIT_MASK(32));
  1189. ret = pxad_init_phys(op, pdev, nr_phy_chans);
  1190. if (ret)
  1191. return ret;
  1192. for (i = 0; i < nr_phy_chans; i++) {
  1193. c = devm_kzalloc(&op->dev, sizeof(*c), GFP_KERNEL);
  1194. if (!c)
  1195. return -ENOMEM;
  1196. c->vc.desc_free = pxad_free_desc;
  1197. vchan_init(&c->vc, &pdev->slave);
  1198. init_waitqueue_head(&c->wq_state);
  1199. }
  1200. return dma_async_device_register(&pdev->slave);
  1201. }
  1202. static int pxad_probe(struct platform_device *op)
  1203. {
  1204. struct pxad_device *pdev;
  1205. const struct of_device_id *of_id;
  1206. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  1207. struct resource *iores;
  1208. int ret, dma_channels = 0, nb_requestors = 0;
  1209. const enum dma_slave_buswidth widths =
  1210. DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
  1211. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1212. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  1213. if (!pdev)
  1214. return -ENOMEM;
  1215. spin_lock_init(&pdev->phy_lock);
  1216. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  1217. pdev->base = devm_ioremap_resource(&op->dev, iores);
  1218. if (IS_ERR(pdev->base))
  1219. return PTR_ERR(pdev->base);
  1220. of_id = of_match_device(pxad_dt_ids, &op->dev);
  1221. if (of_id) {
  1222. of_property_read_u32(op->dev.of_node, "#dma-channels",
  1223. &dma_channels);
  1224. ret = of_property_read_u32(op->dev.of_node, "#dma-requests",
  1225. &nb_requestors);
  1226. if (ret) {
  1227. dev_warn(pdev->slave.dev,
  1228. "#dma-requests set to default 32 as missing in OF: %d",
  1229. ret);
  1230. nb_requestors = 32;
  1231. };
  1232. } else if (pdata && pdata->dma_channels) {
  1233. dma_channels = pdata->dma_channels;
  1234. nb_requestors = pdata->nb_requestors;
  1235. } else {
  1236. dma_channels = 32; /* default 32 channel */
  1237. }
  1238. dma_cap_set(DMA_SLAVE, pdev->slave.cap_mask);
  1239. dma_cap_set(DMA_MEMCPY, pdev->slave.cap_mask);
  1240. dma_cap_set(DMA_CYCLIC, pdev->slave.cap_mask);
  1241. dma_cap_set(DMA_PRIVATE, pdev->slave.cap_mask);
  1242. pdev->slave.device_prep_dma_memcpy = pxad_prep_memcpy;
  1243. pdev->slave.device_prep_slave_sg = pxad_prep_slave_sg;
  1244. pdev->slave.device_prep_dma_cyclic = pxad_prep_dma_cyclic;
  1245. pdev->slave.copy_align = PDMA_ALIGNMENT;
  1246. pdev->slave.src_addr_widths = widths;
  1247. pdev->slave.dst_addr_widths = widths;
  1248. pdev->slave.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  1249. pdev->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  1250. pdev->slave.descriptor_reuse = true;
  1251. pdev->slave.dev = &op->dev;
  1252. ret = pxad_init_dmadev(op, pdev, dma_channels, nb_requestors);
  1253. if (ret) {
  1254. dev_err(pdev->slave.dev, "unable to register\n");
  1255. return ret;
  1256. }
  1257. if (op->dev.of_node) {
  1258. /* Device-tree DMA controller registration */
  1259. ret = of_dma_controller_register(op->dev.of_node,
  1260. pxad_dma_xlate, pdev);
  1261. if (ret < 0) {
  1262. dev_err(pdev->slave.dev,
  1263. "of_dma_controller_register failed\n");
  1264. return ret;
  1265. }
  1266. }
  1267. platform_set_drvdata(op, pdev);
  1268. pxad_init_debugfs(pdev);
  1269. dev_info(pdev->slave.dev, "initialized %d channels on %d requestors\n",
  1270. dma_channels, nb_requestors);
  1271. return 0;
  1272. }
  1273. static const struct platform_device_id pxad_id_table[] = {
  1274. { "pxa-dma", },
  1275. { },
  1276. };
  1277. static struct platform_driver pxad_driver = {
  1278. .driver = {
  1279. .name = "pxa-dma",
  1280. .of_match_table = pxad_dt_ids,
  1281. },
  1282. .id_table = pxad_id_table,
  1283. .probe = pxad_probe,
  1284. .remove = pxad_remove,
  1285. };
  1286. bool pxad_filter_fn(struct dma_chan *chan, void *param)
  1287. {
  1288. struct pxad_chan *c = to_pxad_chan(chan);
  1289. struct pxad_param *p = param;
  1290. if (chan->device->dev->driver != &pxad_driver.driver)
  1291. return false;
  1292. c->drcmr = p->drcmr;
  1293. c->prio = p->prio;
  1294. return true;
  1295. }
  1296. EXPORT_SYMBOL_GPL(pxad_filter_fn);
  1297. int pxad_toggle_reserved_channel(int legacy_channel)
  1298. {
  1299. if (legacy_unavailable & (BIT(legacy_channel)))
  1300. return -EBUSY;
  1301. legacy_reserved ^= BIT(legacy_channel);
  1302. return 0;
  1303. }
  1304. EXPORT_SYMBOL_GPL(pxad_toggle_reserved_channel);
  1305. module_platform_driver(pxad_driver);
  1306. MODULE_DESCRIPTION("Marvell PXA Peripheral DMA Driver");
  1307. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1308. MODULE_LICENSE("GPL v2");