rcar-dmac.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas R-Car Gen2/Gen3 DMA Controller Driver
  4. *
  5. * Copyright (C) 2014-2019 Renesas Electronics Inc.
  6. *
  7. * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/list.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/of_dma.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include "../dmaengine.h"
  24. /*
  25. * struct rcar_dmac_xfer_chunk - Descriptor for a hardware transfer
  26. * @node: entry in the parent's chunks list
  27. * @src_addr: device source address
  28. * @dst_addr: device destination address
  29. * @size: transfer size in bytes
  30. */
  31. struct rcar_dmac_xfer_chunk {
  32. struct list_head node;
  33. dma_addr_t src_addr;
  34. dma_addr_t dst_addr;
  35. u32 size;
  36. };
  37. /*
  38. * struct rcar_dmac_hw_desc - Hardware descriptor for a transfer chunk
  39. * @sar: value of the SAR register (source address)
  40. * @dar: value of the DAR register (destination address)
  41. * @tcr: value of the TCR register (transfer count)
  42. */
  43. struct rcar_dmac_hw_desc {
  44. u32 sar;
  45. u32 dar;
  46. u32 tcr;
  47. u32 reserved;
  48. } __attribute__((__packed__));
  49. /*
  50. * struct rcar_dmac_desc - R-Car Gen2 DMA Transfer Descriptor
  51. * @async_tx: base DMA asynchronous transaction descriptor
  52. * @direction: direction of the DMA transfer
  53. * @xfer_shift: log2 of the transfer size
  54. * @chcr: value of the channel configuration register for this transfer
  55. * @node: entry in the channel's descriptors lists
  56. * @chunks: list of transfer chunks for this transfer
  57. * @running: the transfer chunk being currently processed
  58. * @nchunks: number of transfer chunks for this transfer
  59. * @hwdescs.use: whether the transfer descriptor uses hardware descriptors
  60. * @hwdescs.mem: hardware descriptors memory for the transfer
  61. * @hwdescs.dma: device address of the hardware descriptors memory
  62. * @hwdescs.size: size of the hardware descriptors in bytes
  63. * @size: transfer size in bytes
  64. * @cyclic: when set indicates that the DMA transfer is cyclic
  65. */
  66. struct rcar_dmac_desc {
  67. struct dma_async_tx_descriptor async_tx;
  68. enum dma_transfer_direction direction;
  69. unsigned int xfer_shift;
  70. u32 chcr;
  71. struct list_head node;
  72. struct list_head chunks;
  73. struct rcar_dmac_xfer_chunk *running;
  74. unsigned int nchunks;
  75. struct {
  76. bool use;
  77. struct rcar_dmac_hw_desc *mem;
  78. dma_addr_t dma;
  79. size_t size;
  80. } hwdescs;
  81. unsigned int size;
  82. bool cyclic;
  83. };
  84. #define to_rcar_dmac_desc(d) container_of(d, struct rcar_dmac_desc, async_tx)
  85. /*
  86. * struct rcar_dmac_desc_page - One page worth of descriptors
  87. * @node: entry in the channel's pages list
  88. * @descs: array of DMA descriptors
  89. * @chunks: array of transfer chunk descriptors
  90. */
  91. struct rcar_dmac_desc_page {
  92. struct list_head node;
  93. union {
  94. DECLARE_FLEX_ARRAY(struct rcar_dmac_desc, descs);
  95. DECLARE_FLEX_ARRAY(struct rcar_dmac_xfer_chunk, chunks);
  96. };
  97. };
  98. #define RCAR_DMAC_DESCS_PER_PAGE \
  99. ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, descs)) / \
  100. sizeof(struct rcar_dmac_desc))
  101. #define RCAR_DMAC_XFER_CHUNKS_PER_PAGE \
  102. ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, chunks)) / \
  103. sizeof(struct rcar_dmac_xfer_chunk))
  104. /*
  105. * struct rcar_dmac_chan_slave - Slave configuration
  106. * @slave_addr: slave memory address
  107. * @xfer_size: size (in bytes) of hardware transfers
  108. */
  109. struct rcar_dmac_chan_slave {
  110. phys_addr_t slave_addr;
  111. unsigned int xfer_size;
  112. };
  113. /*
  114. * struct rcar_dmac_chan_map - Map of slave device phys to dma address
  115. * @addr: slave dma address
  116. * @dir: direction of mapping
  117. * @slave: slave configuration that is mapped
  118. */
  119. struct rcar_dmac_chan_map {
  120. dma_addr_t addr;
  121. enum dma_data_direction dir;
  122. struct rcar_dmac_chan_slave slave;
  123. };
  124. /*
  125. * struct rcar_dmac_chan - R-Car Gen2 DMA Controller Channel
  126. * @chan: base DMA channel object
  127. * @iomem: channel I/O memory base
  128. * @index: index of this channel in the controller
  129. * @irq: channel IRQ
  130. * @src: slave memory address and size on the source side
  131. * @dst: slave memory address and size on the destination side
  132. * @mid_rid: hardware MID/RID for the DMA client using this channel
  133. * @lock: protects the channel CHCR register and the desc members
  134. * @desc.free: list of free descriptors
  135. * @desc.pending: list of pending descriptors (submitted with tx_submit)
  136. * @desc.active: list of active descriptors (activated with issue_pending)
  137. * @desc.done: list of completed descriptors
  138. * @desc.wait: list of descriptors waiting for an ack
  139. * @desc.running: the descriptor being processed (a member of the active list)
  140. * @desc.chunks_free: list of free transfer chunk descriptors
  141. * @desc.pages: list of pages used by allocated descriptors
  142. */
  143. struct rcar_dmac_chan {
  144. struct dma_chan chan;
  145. void __iomem *iomem;
  146. unsigned int index;
  147. int irq;
  148. struct rcar_dmac_chan_slave src;
  149. struct rcar_dmac_chan_slave dst;
  150. struct rcar_dmac_chan_map map;
  151. int mid_rid;
  152. spinlock_t lock;
  153. struct {
  154. struct list_head free;
  155. struct list_head pending;
  156. struct list_head active;
  157. struct list_head done;
  158. struct list_head wait;
  159. struct rcar_dmac_desc *running;
  160. struct list_head chunks_free;
  161. struct list_head pages;
  162. } desc;
  163. };
  164. #define to_rcar_dmac_chan(c) container_of(c, struct rcar_dmac_chan, chan)
  165. /*
  166. * struct rcar_dmac - R-Car Gen2 DMA Controller
  167. * @engine: base DMA engine object
  168. * @dev: the hardware device
  169. * @dmac_base: remapped base register block
  170. * @chan_base: remapped channel register block (optional)
  171. * @n_channels: number of available channels
  172. * @channels: array of DMAC channels
  173. * @channels_mask: bitfield of which DMA channels are managed by this driver
  174. * @modules: bitmask of client modules in use
  175. */
  176. struct rcar_dmac {
  177. struct dma_device engine;
  178. struct device *dev;
  179. void __iomem *dmac_base;
  180. void __iomem *chan_base;
  181. unsigned int n_channels;
  182. struct rcar_dmac_chan *channels;
  183. u32 channels_mask;
  184. DECLARE_BITMAP(modules, 256);
  185. };
  186. #define to_rcar_dmac(d) container_of(d, struct rcar_dmac, engine)
  187. #define for_each_rcar_dmac_chan(i, dmac, chan) \
  188. for (i = 0, chan = &(dmac)->channels[0]; i < (dmac)->n_channels; i++, chan++) \
  189. if (!((dmac)->channels_mask & BIT(i))) continue; else
  190. /*
  191. * struct rcar_dmac_of_data - This driver's OF data
  192. * @chan_offset_base: DMAC channels base offset
  193. * @chan_offset_stride: DMAC channels offset stride
  194. */
  195. struct rcar_dmac_of_data {
  196. u32 chan_offset_base;
  197. u32 chan_offset_stride;
  198. };
  199. /* -----------------------------------------------------------------------------
  200. * Registers
  201. */
  202. #define RCAR_DMAISTA 0x0020
  203. #define RCAR_DMASEC 0x0030
  204. #define RCAR_DMAOR 0x0060
  205. #define RCAR_DMAOR_PRI_FIXED (0 << 8)
  206. #define RCAR_DMAOR_PRI_ROUND_ROBIN (3 << 8)
  207. #define RCAR_DMAOR_AE (1 << 2)
  208. #define RCAR_DMAOR_DME (1 << 0)
  209. #define RCAR_DMACHCLR 0x0080 /* Not on R-Car Gen4 */
  210. #define RCAR_DMADPSEC 0x00a0
  211. #define RCAR_DMASAR 0x0000
  212. #define RCAR_DMADAR 0x0004
  213. #define RCAR_DMATCR 0x0008
  214. #define RCAR_DMATCR_MASK 0x00ffffff
  215. #define RCAR_DMATSR 0x0028
  216. #define RCAR_DMACHCR 0x000c
  217. #define RCAR_DMACHCR_CAE (1 << 31)
  218. #define RCAR_DMACHCR_CAIE (1 << 30)
  219. #define RCAR_DMACHCR_DPM_DISABLED (0 << 28)
  220. #define RCAR_DMACHCR_DPM_ENABLED (1 << 28)
  221. #define RCAR_DMACHCR_DPM_REPEAT (2 << 28)
  222. #define RCAR_DMACHCR_DPM_INFINITE (3 << 28)
  223. #define RCAR_DMACHCR_RPT_SAR (1 << 27)
  224. #define RCAR_DMACHCR_RPT_DAR (1 << 26)
  225. #define RCAR_DMACHCR_RPT_TCR (1 << 25)
  226. #define RCAR_DMACHCR_DPB (1 << 22)
  227. #define RCAR_DMACHCR_DSE (1 << 19)
  228. #define RCAR_DMACHCR_DSIE (1 << 18)
  229. #define RCAR_DMACHCR_TS_1B ((0 << 20) | (0 << 3))
  230. #define RCAR_DMACHCR_TS_2B ((0 << 20) | (1 << 3))
  231. #define RCAR_DMACHCR_TS_4B ((0 << 20) | (2 << 3))
  232. #define RCAR_DMACHCR_TS_16B ((0 << 20) | (3 << 3))
  233. #define RCAR_DMACHCR_TS_32B ((1 << 20) | (0 << 3))
  234. #define RCAR_DMACHCR_TS_64B ((1 << 20) | (1 << 3))
  235. #define RCAR_DMACHCR_TS_8B ((1 << 20) | (3 << 3))
  236. #define RCAR_DMACHCR_DM_FIXED (0 << 14)
  237. #define RCAR_DMACHCR_DM_INC (1 << 14)
  238. #define RCAR_DMACHCR_DM_DEC (2 << 14)
  239. #define RCAR_DMACHCR_SM_FIXED (0 << 12)
  240. #define RCAR_DMACHCR_SM_INC (1 << 12)
  241. #define RCAR_DMACHCR_SM_DEC (2 << 12)
  242. #define RCAR_DMACHCR_RS_AUTO (4 << 8)
  243. #define RCAR_DMACHCR_RS_DMARS (8 << 8)
  244. #define RCAR_DMACHCR_IE (1 << 2)
  245. #define RCAR_DMACHCR_TE (1 << 1)
  246. #define RCAR_DMACHCR_DE (1 << 0)
  247. #define RCAR_DMATCRB 0x0018
  248. #define RCAR_DMATSRB 0x0038
  249. #define RCAR_DMACHCRB 0x001c
  250. #define RCAR_DMACHCRB_DCNT(n) ((n) << 24)
  251. #define RCAR_DMACHCRB_DPTR_MASK (0xff << 16)
  252. #define RCAR_DMACHCRB_DPTR_SHIFT 16
  253. #define RCAR_DMACHCRB_DRST (1 << 15)
  254. #define RCAR_DMACHCRB_DTS (1 << 8)
  255. #define RCAR_DMACHCRB_SLM_NORMAL (0 << 4)
  256. #define RCAR_DMACHCRB_SLM_CLK(n) ((8 | (n)) << 4)
  257. #define RCAR_DMACHCRB_PRI(n) ((n) << 0)
  258. #define RCAR_DMARS 0x0040
  259. #define RCAR_DMABUFCR 0x0048
  260. #define RCAR_DMABUFCR_MBU(n) ((n) << 16)
  261. #define RCAR_DMABUFCR_ULB(n) ((n) << 0)
  262. #define RCAR_DMADPBASE 0x0050
  263. #define RCAR_DMADPBASE_MASK 0xfffffff0
  264. #define RCAR_DMADPBASE_SEL (1 << 0)
  265. #define RCAR_DMADPCR 0x0054
  266. #define RCAR_DMADPCR_DIPT(n) ((n) << 24)
  267. #define RCAR_DMAFIXSAR 0x0010
  268. #define RCAR_DMAFIXDAR 0x0014
  269. #define RCAR_DMAFIXDPBASE 0x0060
  270. /* For R-Car Gen4 */
  271. #define RCAR_GEN4_DMACHCLR 0x0100
  272. /* Hardcode the MEMCPY transfer size to 4 bytes. */
  273. #define RCAR_DMAC_MEMCPY_XFER_SIZE 4
  274. /* -----------------------------------------------------------------------------
  275. * Device access
  276. */
  277. static void rcar_dmac_write(struct rcar_dmac *dmac, u32 reg, u32 data)
  278. {
  279. if (reg == RCAR_DMAOR)
  280. writew(data, dmac->dmac_base + reg);
  281. else
  282. writel(data, dmac->dmac_base + reg);
  283. }
  284. static u32 rcar_dmac_read(struct rcar_dmac *dmac, u32 reg)
  285. {
  286. if (reg == RCAR_DMAOR)
  287. return readw(dmac->dmac_base + reg);
  288. else
  289. return readl(dmac->dmac_base + reg);
  290. }
  291. static u32 rcar_dmac_chan_read(struct rcar_dmac_chan *chan, u32 reg)
  292. {
  293. if (reg == RCAR_DMARS)
  294. return readw(chan->iomem + reg);
  295. else
  296. return readl(chan->iomem + reg);
  297. }
  298. static void rcar_dmac_chan_write(struct rcar_dmac_chan *chan, u32 reg, u32 data)
  299. {
  300. if (reg == RCAR_DMARS)
  301. writew(data, chan->iomem + reg);
  302. else
  303. writel(data, chan->iomem + reg);
  304. }
  305. static void rcar_dmac_chan_clear(struct rcar_dmac *dmac,
  306. struct rcar_dmac_chan *chan)
  307. {
  308. if (dmac->chan_base)
  309. rcar_dmac_chan_write(chan, RCAR_GEN4_DMACHCLR, 1);
  310. else
  311. rcar_dmac_write(dmac, RCAR_DMACHCLR, BIT(chan->index));
  312. }
  313. static void rcar_dmac_chan_clear_all(struct rcar_dmac *dmac)
  314. {
  315. struct rcar_dmac_chan *chan;
  316. unsigned int i;
  317. if (dmac->chan_base) {
  318. for_each_rcar_dmac_chan(i, dmac, chan)
  319. rcar_dmac_chan_write(chan, RCAR_GEN4_DMACHCLR, 1);
  320. } else {
  321. rcar_dmac_write(dmac, RCAR_DMACHCLR, dmac->channels_mask);
  322. }
  323. }
  324. /* -----------------------------------------------------------------------------
  325. * Initialization and configuration
  326. */
  327. static bool rcar_dmac_chan_is_busy(struct rcar_dmac_chan *chan)
  328. {
  329. u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
  330. return !!(chcr & (RCAR_DMACHCR_DE | RCAR_DMACHCR_TE));
  331. }
  332. static void rcar_dmac_chan_start_xfer(struct rcar_dmac_chan *chan)
  333. {
  334. struct rcar_dmac_desc *desc = chan->desc.running;
  335. u32 chcr = desc->chcr;
  336. WARN_ON_ONCE(rcar_dmac_chan_is_busy(chan));
  337. if (chan->mid_rid >= 0)
  338. rcar_dmac_chan_write(chan, RCAR_DMARS, chan->mid_rid);
  339. if (desc->hwdescs.use) {
  340. struct rcar_dmac_xfer_chunk *chunk =
  341. list_first_entry(&desc->chunks,
  342. struct rcar_dmac_xfer_chunk, node);
  343. dev_dbg(chan->chan.device->dev,
  344. "chan%u: queue desc %p: %u@%pad\n",
  345. chan->index, desc, desc->nchunks, &desc->hwdescs.dma);
  346. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  347. rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR,
  348. chunk->src_addr >> 32);
  349. rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR,
  350. chunk->dst_addr >> 32);
  351. rcar_dmac_chan_write(chan, RCAR_DMAFIXDPBASE,
  352. desc->hwdescs.dma >> 32);
  353. #endif
  354. rcar_dmac_chan_write(chan, RCAR_DMADPBASE,
  355. (desc->hwdescs.dma & 0xfffffff0) |
  356. RCAR_DMADPBASE_SEL);
  357. rcar_dmac_chan_write(chan, RCAR_DMACHCRB,
  358. RCAR_DMACHCRB_DCNT(desc->nchunks - 1) |
  359. RCAR_DMACHCRB_DRST);
  360. /*
  361. * Errata: When descriptor memory is accessed through an IOMMU
  362. * the DMADAR register isn't initialized automatically from the
  363. * first descriptor at beginning of transfer by the DMAC like it
  364. * should. Initialize it manually with the destination address
  365. * of the first chunk.
  366. */
  367. rcar_dmac_chan_write(chan, RCAR_DMADAR,
  368. chunk->dst_addr & 0xffffffff);
  369. /*
  370. * Program the descriptor stage interrupt to occur after the end
  371. * of the first stage.
  372. */
  373. rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(1));
  374. chcr |= RCAR_DMACHCR_RPT_SAR | RCAR_DMACHCR_RPT_DAR
  375. | RCAR_DMACHCR_RPT_TCR | RCAR_DMACHCR_DPB;
  376. /*
  377. * If the descriptor isn't cyclic enable normal descriptor mode
  378. * and the transfer completion interrupt.
  379. */
  380. if (!desc->cyclic)
  381. chcr |= RCAR_DMACHCR_DPM_ENABLED | RCAR_DMACHCR_IE;
  382. /*
  383. * If the descriptor is cyclic and has a callback enable the
  384. * descriptor stage interrupt in infinite repeat mode.
  385. */
  386. else if (desc->async_tx.callback)
  387. chcr |= RCAR_DMACHCR_DPM_INFINITE | RCAR_DMACHCR_DSIE;
  388. /*
  389. * Otherwise just select infinite repeat mode without any
  390. * interrupt.
  391. */
  392. else
  393. chcr |= RCAR_DMACHCR_DPM_INFINITE;
  394. } else {
  395. struct rcar_dmac_xfer_chunk *chunk = desc->running;
  396. dev_dbg(chan->chan.device->dev,
  397. "chan%u: queue chunk %p: %u@%pad -> %pad\n",
  398. chan->index, chunk, chunk->size, &chunk->src_addr,
  399. &chunk->dst_addr);
  400. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  401. rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR,
  402. chunk->src_addr >> 32);
  403. rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR,
  404. chunk->dst_addr >> 32);
  405. #endif
  406. rcar_dmac_chan_write(chan, RCAR_DMASAR,
  407. chunk->src_addr & 0xffffffff);
  408. rcar_dmac_chan_write(chan, RCAR_DMADAR,
  409. chunk->dst_addr & 0xffffffff);
  410. rcar_dmac_chan_write(chan, RCAR_DMATCR,
  411. chunk->size >> desc->xfer_shift);
  412. chcr |= RCAR_DMACHCR_DPM_DISABLED | RCAR_DMACHCR_IE;
  413. }
  414. rcar_dmac_chan_write(chan, RCAR_DMACHCR,
  415. chcr | RCAR_DMACHCR_DE | RCAR_DMACHCR_CAIE);
  416. }
  417. static int rcar_dmac_init(struct rcar_dmac *dmac)
  418. {
  419. u16 dmaor;
  420. /* Clear all channels and enable the DMAC globally. */
  421. rcar_dmac_chan_clear_all(dmac);
  422. rcar_dmac_write(dmac, RCAR_DMAOR,
  423. RCAR_DMAOR_PRI_FIXED | RCAR_DMAOR_DME);
  424. dmaor = rcar_dmac_read(dmac, RCAR_DMAOR);
  425. if ((dmaor & (RCAR_DMAOR_AE | RCAR_DMAOR_DME)) != RCAR_DMAOR_DME) {
  426. dev_warn(dmac->dev, "DMAOR initialization failed.\n");
  427. return -EIO;
  428. }
  429. return 0;
  430. }
  431. /* -----------------------------------------------------------------------------
  432. * Descriptors submission
  433. */
  434. static dma_cookie_t rcar_dmac_tx_submit(struct dma_async_tx_descriptor *tx)
  435. {
  436. struct rcar_dmac_chan *chan = to_rcar_dmac_chan(tx->chan);
  437. struct rcar_dmac_desc *desc = to_rcar_dmac_desc(tx);
  438. unsigned long flags;
  439. dma_cookie_t cookie;
  440. spin_lock_irqsave(&chan->lock, flags);
  441. cookie = dma_cookie_assign(tx);
  442. dev_dbg(chan->chan.device->dev, "chan%u: submit #%d@%p\n",
  443. chan->index, tx->cookie, desc);
  444. list_add_tail(&desc->node, &chan->desc.pending);
  445. desc->running = list_first_entry(&desc->chunks,
  446. struct rcar_dmac_xfer_chunk, node);
  447. spin_unlock_irqrestore(&chan->lock, flags);
  448. return cookie;
  449. }
  450. /* -----------------------------------------------------------------------------
  451. * Descriptors allocation and free
  452. */
  453. /*
  454. * rcar_dmac_desc_alloc - Allocate a page worth of DMA descriptors
  455. * @chan: the DMA channel
  456. * @gfp: allocation flags
  457. */
  458. static int rcar_dmac_desc_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
  459. {
  460. struct rcar_dmac_desc_page *page;
  461. unsigned long flags;
  462. LIST_HEAD(list);
  463. unsigned int i;
  464. page = (void *)get_zeroed_page(gfp);
  465. if (!page)
  466. return -ENOMEM;
  467. for (i = 0; i < RCAR_DMAC_DESCS_PER_PAGE; ++i) {
  468. struct rcar_dmac_desc *desc = &page->descs[i];
  469. dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
  470. desc->async_tx.tx_submit = rcar_dmac_tx_submit;
  471. INIT_LIST_HEAD(&desc->chunks);
  472. list_add_tail(&desc->node, &list);
  473. }
  474. spin_lock_irqsave(&chan->lock, flags);
  475. list_splice_tail(&list, &chan->desc.free);
  476. list_add_tail(&page->node, &chan->desc.pages);
  477. spin_unlock_irqrestore(&chan->lock, flags);
  478. return 0;
  479. }
  480. /*
  481. * rcar_dmac_desc_put - Release a DMA transfer descriptor
  482. * @chan: the DMA channel
  483. * @desc: the descriptor
  484. *
  485. * Put the descriptor and its transfer chunk descriptors back in the channel's
  486. * free descriptors lists. The descriptor's chunks list will be reinitialized to
  487. * an empty list as a result.
  488. *
  489. * The descriptor must have been removed from the channel's lists before calling
  490. * this function.
  491. */
  492. static void rcar_dmac_desc_put(struct rcar_dmac_chan *chan,
  493. struct rcar_dmac_desc *desc)
  494. {
  495. unsigned long flags;
  496. spin_lock_irqsave(&chan->lock, flags);
  497. list_splice_tail_init(&desc->chunks, &chan->desc.chunks_free);
  498. list_add(&desc->node, &chan->desc.free);
  499. spin_unlock_irqrestore(&chan->lock, flags);
  500. }
  501. static void rcar_dmac_desc_recycle_acked(struct rcar_dmac_chan *chan)
  502. {
  503. struct rcar_dmac_desc *desc, *_desc;
  504. unsigned long flags;
  505. LIST_HEAD(list);
  506. /*
  507. * We have to temporarily move all descriptors from the wait list to a
  508. * local list as iterating over the wait list, even with
  509. * list_for_each_entry_safe, isn't safe if we release the channel lock
  510. * around the rcar_dmac_desc_put() call.
  511. */
  512. spin_lock_irqsave(&chan->lock, flags);
  513. list_splice_init(&chan->desc.wait, &list);
  514. spin_unlock_irqrestore(&chan->lock, flags);
  515. list_for_each_entry_safe(desc, _desc, &list, node) {
  516. if (async_tx_test_ack(&desc->async_tx)) {
  517. list_del(&desc->node);
  518. rcar_dmac_desc_put(chan, desc);
  519. }
  520. }
  521. if (list_empty(&list))
  522. return;
  523. /* Put the remaining descriptors back in the wait list. */
  524. spin_lock_irqsave(&chan->lock, flags);
  525. list_splice(&list, &chan->desc.wait);
  526. spin_unlock_irqrestore(&chan->lock, flags);
  527. }
  528. /*
  529. * rcar_dmac_desc_get - Allocate a descriptor for a DMA transfer
  530. * @chan: the DMA channel
  531. *
  532. * Locking: This function must be called in a non-atomic context.
  533. *
  534. * Return: A pointer to the allocated descriptor or NULL if no descriptor can
  535. * be allocated.
  536. */
  537. static struct rcar_dmac_desc *rcar_dmac_desc_get(struct rcar_dmac_chan *chan)
  538. {
  539. struct rcar_dmac_desc *desc;
  540. unsigned long flags;
  541. int ret;
  542. /* Recycle acked descriptors before attempting allocation. */
  543. rcar_dmac_desc_recycle_acked(chan);
  544. spin_lock_irqsave(&chan->lock, flags);
  545. while (list_empty(&chan->desc.free)) {
  546. /*
  547. * No free descriptors, allocate a page worth of them and try
  548. * again, as someone else could race us to get the newly
  549. * allocated descriptors. If the allocation fails return an
  550. * error.
  551. */
  552. spin_unlock_irqrestore(&chan->lock, flags);
  553. ret = rcar_dmac_desc_alloc(chan, GFP_NOWAIT);
  554. if (ret < 0)
  555. return NULL;
  556. spin_lock_irqsave(&chan->lock, flags);
  557. }
  558. desc = list_first_entry(&chan->desc.free, struct rcar_dmac_desc, node);
  559. list_del(&desc->node);
  560. spin_unlock_irqrestore(&chan->lock, flags);
  561. return desc;
  562. }
  563. /*
  564. * rcar_dmac_xfer_chunk_alloc - Allocate a page worth of transfer chunks
  565. * @chan: the DMA channel
  566. * @gfp: allocation flags
  567. */
  568. static int rcar_dmac_xfer_chunk_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
  569. {
  570. struct rcar_dmac_desc_page *page;
  571. unsigned long flags;
  572. LIST_HEAD(list);
  573. unsigned int i;
  574. page = (void *)get_zeroed_page(gfp);
  575. if (!page)
  576. return -ENOMEM;
  577. for (i = 0; i < RCAR_DMAC_XFER_CHUNKS_PER_PAGE; ++i) {
  578. struct rcar_dmac_xfer_chunk *chunk = &page->chunks[i];
  579. list_add_tail(&chunk->node, &list);
  580. }
  581. spin_lock_irqsave(&chan->lock, flags);
  582. list_splice_tail(&list, &chan->desc.chunks_free);
  583. list_add_tail(&page->node, &chan->desc.pages);
  584. spin_unlock_irqrestore(&chan->lock, flags);
  585. return 0;
  586. }
  587. /*
  588. * rcar_dmac_xfer_chunk_get - Allocate a transfer chunk for a DMA transfer
  589. * @chan: the DMA channel
  590. *
  591. * Locking: This function must be called in a non-atomic context.
  592. *
  593. * Return: A pointer to the allocated transfer chunk descriptor or NULL if no
  594. * descriptor can be allocated.
  595. */
  596. static struct rcar_dmac_xfer_chunk *
  597. rcar_dmac_xfer_chunk_get(struct rcar_dmac_chan *chan)
  598. {
  599. struct rcar_dmac_xfer_chunk *chunk;
  600. unsigned long flags;
  601. int ret;
  602. spin_lock_irqsave(&chan->lock, flags);
  603. while (list_empty(&chan->desc.chunks_free)) {
  604. /*
  605. * No free descriptors, allocate a page worth of them and try
  606. * again, as someone else could race us to get the newly
  607. * allocated descriptors. If the allocation fails return an
  608. * error.
  609. */
  610. spin_unlock_irqrestore(&chan->lock, flags);
  611. ret = rcar_dmac_xfer_chunk_alloc(chan, GFP_NOWAIT);
  612. if (ret < 0)
  613. return NULL;
  614. spin_lock_irqsave(&chan->lock, flags);
  615. }
  616. chunk = list_first_entry(&chan->desc.chunks_free,
  617. struct rcar_dmac_xfer_chunk, node);
  618. list_del(&chunk->node);
  619. spin_unlock_irqrestore(&chan->lock, flags);
  620. return chunk;
  621. }
  622. static void rcar_dmac_realloc_hwdesc(struct rcar_dmac_chan *chan,
  623. struct rcar_dmac_desc *desc, size_t size)
  624. {
  625. /*
  626. * dma_alloc_coherent() allocates memory in page size increments. To
  627. * avoid reallocating the hardware descriptors when the allocated size
  628. * wouldn't change align the requested size to a multiple of the page
  629. * size.
  630. */
  631. size = PAGE_ALIGN(size);
  632. if (desc->hwdescs.size == size)
  633. return;
  634. if (desc->hwdescs.mem) {
  635. dma_free_coherent(chan->chan.device->dev, desc->hwdescs.size,
  636. desc->hwdescs.mem, desc->hwdescs.dma);
  637. desc->hwdescs.mem = NULL;
  638. desc->hwdescs.size = 0;
  639. }
  640. if (!size)
  641. return;
  642. desc->hwdescs.mem = dma_alloc_coherent(chan->chan.device->dev, size,
  643. &desc->hwdescs.dma, GFP_NOWAIT);
  644. if (!desc->hwdescs.mem)
  645. return;
  646. desc->hwdescs.size = size;
  647. }
  648. static int rcar_dmac_fill_hwdesc(struct rcar_dmac_chan *chan,
  649. struct rcar_dmac_desc *desc)
  650. {
  651. struct rcar_dmac_xfer_chunk *chunk;
  652. struct rcar_dmac_hw_desc *hwdesc;
  653. rcar_dmac_realloc_hwdesc(chan, desc, desc->nchunks * sizeof(*hwdesc));
  654. hwdesc = desc->hwdescs.mem;
  655. if (!hwdesc)
  656. return -ENOMEM;
  657. list_for_each_entry(chunk, &desc->chunks, node) {
  658. hwdesc->sar = chunk->src_addr;
  659. hwdesc->dar = chunk->dst_addr;
  660. hwdesc->tcr = chunk->size >> desc->xfer_shift;
  661. hwdesc++;
  662. }
  663. return 0;
  664. }
  665. /* -----------------------------------------------------------------------------
  666. * Stop and reset
  667. */
  668. static void rcar_dmac_chcr_de_barrier(struct rcar_dmac_chan *chan)
  669. {
  670. u32 chcr;
  671. unsigned int i;
  672. /*
  673. * Ensure that the setting of the DE bit is actually 0 after
  674. * clearing it.
  675. */
  676. for (i = 0; i < 1024; i++) {
  677. chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
  678. if (!(chcr & RCAR_DMACHCR_DE))
  679. return;
  680. udelay(1);
  681. }
  682. dev_err(chan->chan.device->dev, "CHCR DE check error\n");
  683. }
  684. static void rcar_dmac_clear_chcr_de(struct rcar_dmac_chan *chan)
  685. {
  686. u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
  687. /* set DE=0 and flush remaining data */
  688. rcar_dmac_chan_write(chan, RCAR_DMACHCR, (chcr & ~RCAR_DMACHCR_DE));
  689. /* make sure all remaining data was flushed */
  690. rcar_dmac_chcr_de_barrier(chan);
  691. }
  692. static void rcar_dmac_chan_halt(struct rcar_dmac_chan *chan)
  693. {
  694. u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
  695. chcr &= ~(RCAR_DMACHCR_DSE | RCAR_DMACHCR_DSIE | RCAR_DMACHCR_IE |
  696. RCAR_DMACHCR_TE | RCAR_DMACHCR_DE |
  697. RCAR_DMACHCR_CAE | RCAR_DMACHCR_CAIE);
  698. rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr);
  699. rcar_dmac_chcr_de_barrier(chan);
  700. }
  701. static void rcar_dmac_chan_reinit(struct rcar_dmac_chan *chan)
  702. {
  703. struct rcar_dmac_desc *desc, *_desc;
  704. unsigned long flags;
  705. LIST_HEAD(descs);
  706. spin_lock_irqsave(&chan->lock, flags);
  707. /* Move all non-free descriptors to the local lists. */
  708. list_splice_init(&chan->desc.pending, &descs);
  709. list_splice_init(&chan->desc.active, &descs);
  710. list_splice_init(&chan->desc.done, &descs);
  711. list_splice_init(&chan->desc.wait, &descs);
  712. chan->desc.running = NULL;
  713. spin_unlock_irqrestore(&chan->lock, flags);
  714. list_for_each_entry_safe(desc, _desc, &descs, node) {
  715. list_del(&desc->node);
  716. rcar_dmac_desc_put(chan, desc);
  717. }
  718. }
  719. static void rcar_dmac_stop_all_chan(struct rcar_dmac *dmac)
  720. {
  721. struct rcar_dmac_chan *chan;
  722. unsigned int i;
  723. /* Stop all channels. */
  724. for_each_rcar_dmac_chan(i, dmac, chan) {
  725. /* Stop and reinitialize the channel. */
  726. spin_lock_irq(&chan->lock);
  727. rcar_dmac_chan_halt(chan);
  728. spin_unlock_irq(&chan->lock);
  729. }
  730. }
  731. static int rcar_dmac_chan_pause(struct dma_chan *chan)
  732. {
  733. unsigned long flags;
  734. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  735. spin_lock_irqsave(&rchan->lock, flags);
  736. rcar_dmac_clear_chcr_de(rchan);
  737. spin_unlock_irqrestore(&rchan->lock, flags);
  738. return 0;
  739. }
  740. /* -----------------------------------------------------------------------------
  741. * Descriptors preparation
  742. */
  743. static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan *chan,
  744. struct rcar_dmac_desc *desc)
  745. {
  746. static const u32 chcr_ts[] = {
  747. RCAR_DMACHCR_TS_1B, RCAR_DMACHCR_TS_2B,
  748. RCAR_DMACHCR_TS_4B, RCAR_DMACHCR_TS_8B,
  749. RCAR_DMACHCR_TS_16B, RCAR_DMACHCR_TS_32B,
  750. RCAR_DMACHCR_TS_64B,
  751. };
  752. unsigned int xfer_size;
  753. u32 chcr;
  754. switch (desc->direction) {
  755. case DMA_DEV_TO_MEM:
  756. chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_FIXED
  757. | RCAR_DMACHCR_RS_DMARS;
  758. xfer_size = chan->src.xfer_size;
  759. break;
  760. case DMA_MEM_TO_DEV:
  761. chcr = RCAR_DMACHCR_DM_FIXED | RCAR_DMACHCR_SM_INC
  762. | RCAR_DMACHCR_RS_DMARS;
  763. xfer_size = chan->dst.xfer_size;
  764. break;
  765. case DMA_MEM_TO_MEM:
  766. default:
  767. chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_INC
  768. | RCAR_DMACHCR_RS_AUTO;
  769. xfer_size = RCAR_DMAC_MEMCPY_XFER_SIZE;
  770. break;
  771. }
  772. desc->xfer_shift = ilog2(xfer_size);
  773. desc->chcr = chcr | chcr_ts[desc->xfer_shift];
  774. }
  775. /*
  776. * rcar_dmac_chan_prep_sg - prepare transfer descriptors from an SG list
  777. *
  778. * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
  779. * converted to scatter-gather to guarantee consistent locking and a correct
  780. * list manipulation. For slave DMA direction carries the usual meaning, and,
  781. * logically, the SG list is RAM and the addr variable contains slave address,
  782. * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
  783. * and the SG list contains only one element and points at the source buffer.
  784. */
  785. static struct dma_async_tx_descriptor *
  786. rcar_dmac_chan_prep_sg(struct rcar_dmac_chan *chan, struct scatterlist *sgl,
  787. unsigned int sg_len, dma_addr_t dev_addr,
  788. enum dma_transfer_direction dir, unsigned long dma_flags,
  789. bool cyclic)
  790. {
  791. struct rcar_dmac_xfer_chunk *chunk;
  792. struct rcar_dmac_desc *desc;
  793. struct scatterlist *sg;
  794. unsigned int nchunks = 0;
  795. unsigned int max_chunk_size;
  796. unsigned int full_size = 0;
  797. bool cross_boundary = false;
  798. unsigned int i;
  799. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  800. u32 high_dev_addr;
  801. u32 high_mem_addr;
  802. #endif
  803. desc = rcar_dmac_desc_get(chan);
  804. if (!desc)
  805. return NULL;
  806. desc->async_tx.flags = dma_flags;
  807. desc->async_tx.cookie = -EBUSY;
  808. desc->cyclic = cyclic;
  809. desc->direction = dir;
  810. rcar_dmac_chan_configure_desc(chan, desc);
  811. max_chunk_size = RCAR_DMATCR_MASK << desc->xfer_shift;
  812. /*
  813. * Allocate and fill the transfer chunk descriptors. We own the only
  814. * reference to the DMA descriptor, there's no need for locking.
  815. */
  816. for_each_sg(sgl, sg, sg_len, i) {
  817. dma_addr_t mem_addr = sg_dma_address(sg);
  818. unsigned int len = sg_dma_len(sg);
  819. full_size += len;
  820. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  821. if (i == 0) {
  822. high_dev_addr = dev_addr >> 32;
  823. high_mem_addr = mem_addr >> 32;
  824. }
  825. if ((dev_addr >> 32 != high_dev_addr) ||
  826. (mem_addr >> 32 != high_mem_addr))
  827. cross_boundary = true;
  828. #endif
  829. while (len) {
  830. unsigned int size = min(len, max_chunk_size);
  831. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  832. /*
  833. * Prevent individual transfers from crossing 4GB
  834. * boundaries.
  835. */
  836. if (dev_addr >> 32 != (dev_addr + size - 1) >> 32) {
  837. size = ALIGN(dev_addr, 1ULL << 32) - dev_addr;
  838. cross_boundary = true;
  839. }
  840. if (mem_addr >> 32 != (mem_addr + size - 1) >> 32) {
  841. size = ALIGN(mem_addr, 1ULL << 32) - mem_addr;
  842. cross_boundary = true;
  843. }
  844. #endif
  845. chunk = rcar_dmac_xfer_chunk_get(chan);
  846. if (!chunk) {
  847. rcar_dmac_desc_put(chan, desc);
  848. return NULL;
  849. }
  850. if (dir == DMA_DEV_TO_MEM) {
  851. chunk->src_addr = dev_addr;
  852. chunk->dst_addr = mem_addr;
  853. } else {
  854. chunk->src_addr = mem_addr;
  855. chunk->dst_addr = dev_addr;
  856. }
  857. chunk->size = size;
  858. dev_dbg(chan->chan.device->dev,
  859. "chan%u: chunk %p/%p sgl %u@%p, %u/%u %pad -> %pad\n",
  860. chan->index, chunk, desc, i, sg, size, len,
  861. &chunk->src_addr, &chunk->dst_addr);
  862. mem_addr += size;
  863. if (dir == DMA_MEM_TO_MEM)
  864. dev_addr += size;
  865. len -= size;
  866. list_add_tail(&chunk->node, &desc->chunks);
  867. nchunks++;
  868. }
  869. }
  870. desc->nchunks = nchunks;
  871. desc->size = full_size;
  872. /*
  873. * Use hardware descriptor lists if possible when more than one chunk
  874. * needs to be transferred (otherwise they don't make much sense).
  875. *
  876. * Source/Destination address should be located in same 4GiB region
  877. * in the 40bit address space when it uses Hardware descriptor,
  878. * and cross_boundary is checking it.
  879. */
  880. desc->hwdescs.use = !cross_boundary && nchunks > 1;
  881. if (desc->hwdescs.use) {
  882. if (rcar_dmac_fill_hwdesc(chan, desc) < 0)
  883. desc->hwdescs.use = false;
  884. }
  885. return &desc->async_tx;
  886. }
  887. /* -----------------------------------------------------------------------------
  888. * DMA engine operations
  889. */
  890. static int rcar_dmac_alloc_chan_resources(struct dma_chan *chan)
  891. {
  892. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  893. int ret;
  894. INIT_LIST_HEAD(&rchan->desc.chunks_free);
  895. INIT_LIST_HEAD(&rchan->desc.pages);
  896. /* Preallocate descriptors. */
  897. ret = rcar_dmac_xfer_chunk_alloc(rchan, GFP_KERNEL);
  898. if (ret < 0)
  899. return -ENOMEM;
  900. ret = rcar_dmac_desc_alloc(rchan, GFP_KERNEL);
  901. if (ret < 0)
  902. return -ENOMEM;
  903. return pm_runtime_get_sync(chan->device->dev);
  904. }
  905. static void rcar_dmac_free_chan_resources(struct dma_chan *chan)
  906. {
  907. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  908. struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
  909. struct rcar_dmac_chan_map *map = &rchan->map;
  910. struct rcar_dmac_desc_page *page, *_page;
  911. struct rcar_dmac_desc *desc;
  912. LIST_HEAD(list);
  913. /* Protect against ISR */
  914. spin_lock_irq(&rchan->lock);
  915. rcar_dmac_chan_halt(rchan);
  916. spin_unlock_irq(&rchan->lock);
  917. /*
  918. * Now no new interrupts will occur, but one might already be
  919. * running. Wait for it to finish before freeing resources.
  920. */
  921. synchronize_irq(rchan->irq);
  922. if (rchan->mid_rid >= 0) {
  923. /* The caller is holding dma_list_mutex */
  924. clear_bit(rchan->mid_rid, dmac->modules);
  925. rchan->mid_rid = -EINVAL;
  926. }
  927. list_splice_init(&rchan->desc.free, &list);
  928. list_splice_init(&rchan->desc.pending, &list);
  929. list_splice_init(&rchan->desc.active, &list);
  930. list_splice_init(&rchan->desc.done, &list);
  931. list_splice_init(&rchan->desc.wait, &list);
  932. rchan->desc.running = NULL;
  933. list_for_each_entry(desc, &list, node)
  934. rcar_dmac_realloc_hwdesc(rchan, desc, 0);
  935. list_for_each_entry_safe(page, _page, &rchan->desc.pages, node) {
  936. list_del(&page->node);
  937. free_page((unsigned long)page);
  938. }
  939. /* Remove slave mapping if present. */
  940. if (map->slave.xfer_size) {
  941. dma_unmap_resource(chan->device->dev, map->addr,
  942. map->slave.xfer_size, map->dir, 0);
  943. map->slave.xfer_size = 0;
  944. }
  945. pm_runtime_put(chan->device->dev);
  946. }
  947. static struct dma_async_tx_descriptor *
  948. rcar_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
  949. dma_addr_t dma_src, size_t len, unsigned long flags)
  950. {
  951. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  952. struct scatterlist sgl;
  953. if (!len)
  954. return NULL;
  955. sg_init_table(&sgl, 1);
  956. sg_set_page(&sgl, pfn_to_page(PFN_DOWN(dma_src)), len,
  957. offset_in_page(dma_src));
  958. sg_dma_address(&sgl) = dma_src;
  959. sg_dma_len(&sgl) = len;
  960. return rcar_dmac_chan_prep_sg(rchan, &sgl, 1, dma_dest,
  961. DMA_MEM_TO_MEM, flags, false);
  962. }
  963. static int rcar_dmac_map_slave_addr(struct dma_chan *chan,
  964. enum dma_transfer_direction dir)
  965. {
  966. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  967. struct rcar_dmac_chan_map *map = &rchan->map;
  968. phys_addr_t dev_addr;
  969. size_t dev_size;
  970. enum dma_data_direction dev_dir;
  971. if (dir == DMA_DEV_TO_MEM) {
  972. dev_addr = rchan->src.slave_addr;
  973. dev_size = rchan->src.xfer_size;
  974. dev_dir = DMA_TO_DEVICE;
  975. } else {
  976. dev_addr = rchan->dst.slave_addr;
  977. dev_size = rchan->dst.xfer_size;
  978. dev_dir = DMA_FROM_DEVICE;
  979. }
  980. /* Reuse current map if possible. */
  981. if (dev_addr == map->slave.slave_addr &&
  982. dev_size == map->slave.xfer_size &&
  983. dev_dir == map->dir)
  984. return 0;
  985. /* Remove old mapping if present. */
  986. if (map->slave.xfer_size)
  987. dma_unmap_resource(chan->device->dev, map->addr,
  988. map->slave.xfer_size, map->dir, 0);
  989. map->slave.xfer_size = 0;
  990. /* Create new slave address map. */
  991. map->addr = dma_map_resource(chan->device->dev, dev_addr, dev_size,
  992. dev_dir, 0);
  993. if (dma_mapping_error(chan->device->dev, map->addr)) {
  994. dev_err(chan->device->dev,
  995. "chan%u: failed to map %zx@%pap", rchan->index,
  996. dev_size, &dev_addr);
  997. return -EIO;
  998. }
  999. dev_dbg(chan->device->dev, "chan%u: map %zx@%pap to %pad dir: %s\n",
  1000. rchan->index, dev_size, &dev_addr, &map->addr,
  1001. dev_dir == DMA_TO_DEVICE ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE");
  1002. map->slave.slave_addr = dev_addr;
  1003. map->slave.xfer_size = dev_size;
  1004. map->dir = dev_dir;
  1005. return 0;
  1006. }
  1007. static struct dma_async_tx_descriptor *
  1008. rcar_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  1009. unsigned int sg_len, enum dma_transfer_direction dir,
  1010. unsigned long flags, void *context)
  1011. {
  1012. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  1013. /* Someone calling slave DMA on a generic channel? */
  1014. if (rchan->mid_rid < 0 || !sg_len || !sg_dma_len(sgl)) {
  1015. dev_warn(chan->device->dev,
  1016. "%s: bad parameter: len=%d, id=%d\n",
  1017. __func__, sg_len, rchan->mid_rid);
  1018. return NULL;
  1019. }
  1020. if (rcar_dmac_map_slave_addr(chan, dir))
  1021. return NULL;
  1022. return rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr,
  1023. dir, flags, false);
  1024. }
  1025. #define RCAR_DMAC_MAX_SG_LEN 32
  1026. static struct dma_async_tx_descriptor *
  1027. rcar_dmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  1028. size_t buf_len, size_t period_len,
  1029. enum dma_transfer_direction dir, unsigned long flags)
  1030. {
  1031. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  1032. struct dma_async_tx_descriptor *desc;
  1033. struct scatterlist *sgl;
  1034. unsigned int sg_len;
  1035. unsigned int i;
  1036. /* Someone calling slave DMA on a generic channel? */
  1037. if (rchan->mid_rid < 0 || buf_len < period_len) {
  1038. dev_warn(chan->device->dev,
  1039. "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
  1040. __func__, buf_len, period_len, rchan->mid_rid);
  1041. return NULL;
  1042. }
  1043. if (rcar_dmac_map_slave_addr(chan, dir))
  1044. return NULL;
  1045. sg_len = buf_len / period_len;
  1046. if (sg_len > RCAR_DMAC_MAX_SG_LEN) {
  1047. dev_err(chan->device->dev,
  1048. "chan%u: sg length %d exceeds limit %d",
  1049. rchan->index, sg_len, RCAR_DMAC_MAX_SG_LEN);
  1050. return NULL;
  1051. }
  1052. /*
  1053. * Allocate the sg list dynamically as it would consume too much stack
  1054. * space.
  1055. */
  1056. sgl = kmalloc_array(sg_len, sizeof(*sgl), GFP_NOWAIT);
  1057. if (!sgl)
  1058. return NULL;
  1059. sg_init_table(sgl, sg_len);
  1060. for (i = 0; i < sg_len; ++i) {
  1061. dma_addr_t src = buf_addr + (period_len * i);
  1062. sg_set_page(&sgl[i], pfn_to_page(PFN_DOWN(src)), period_len,
  1063. offset_in_page(src));
  1064. sg_dma_address(&sgl[i]) = src;
  1065. sg_dma_len(&sgl[i]) = period_len;
  1066. }
  1067. desc = rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr,
  1068. dir, flags, true);
  1069. kfree(sgl);
  1070. return desc;
  1071. }
  1072. static int rcar_dmac_device_config(struct dma_chan *chan,
  1073. struct dma_slave_config *cfg)
  1074. {
  1075. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  1076. /*
  1077. * We could lock this, but you shouldn't be configuring the
  1078. * channel, while using it...
  1079. */
  1080. rchan->src.slave_addr = cfg->src_addr;
  1081. rchan->dst.slave_addr = cfg->dst_addr;
  1082. rchan->src.xfer_size = cfg->src_addr_width;
  1083. rchan->dst.xfer_size = cfg->dst_addr_width;
  1084. return 0;
  1085. }
  1086. static int rcar_dmac_chan_terminate_all(struct dma_chan *chan)
  1087. {
  1088. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  1089. unsigned long flags;
  1090. spin_lock_irqsave(&rchan->lock, flags);
  1091. rcar_dmac_chan_halt(rchan);
  1092. spin_unlock_irqrestore(&rchan->lock, flags);
  1093. /*
  1094. * FIXME: No new interrupt can occur now, but the IRQ thread might still
  1095. * be running.
  1096. */
  1097. rcar_dmac_chan_reinit(rchan);
  1098. return 0;
  1099. }
  1100. static unsigned int rcar_dmac_chan_get_residue(struct rcar_dmac_chan *chan,
  1101. dma_cookie_t cookie)
  1102. {
  1103. struct rcar_dmac_desc *desc = chan->desc.running;
  1104. struct rcar_dmac_xfer_chunk *running = NULL;
  1105. struct rcar_dmac_xfer_chunk *chunk;
  1106. enum dma_status status;
  1107. unsigned int residue = 0;
  1108. unsigned int dptr = 0;
  1109. unsigned int chcrb;
  1110. unsigned int tcrb;
  1111. unsigned int i;
  1112. if (!desc)
  1113. return 0;
  1114. /*
  1115. * If the cookie corresponds to a descriptor that has been completed
  1116. * there is no residue. The same check has already been performed by the
  1117. * caller but without holding the channel lock, so the descriptor could
  1118. * now be complete.
  1119. */
  1120. status = dma_cookie_status(&chan->chan, cookie, NULL);
  1121. if (status == DMA_COMPLETE)
  1122. return 0;
  1123. /*
  1124. * If the cookie doesn't correspond to the currently running transfer
  1125. * then the descriptor hasn't been processed yet, and the residue is
  1126. * equal to the full descriptor size.
  1127. * Also, a client driver is possible to call this function before
  1128. * rcar_dmac_isr_channel_thread() runs. In this case, the "desc.running"
  1129. * will be the next descriptor, and the done list will appear. So, if
  1130. * the argument cookie matches the done list's cookie, we can assume
  1131. * the residue is zero.
  1132. */
  1133. if (cookie != desc->async_tx.cookie) {
  1134. list_for_each_entry(desc, &chan->desc.done, node) {
  1135. if (cookie == desc->async_tx.cookie)
  1136. return 0;
  1137. }
  1138. list_for_each_entry(desc, &chan->desc.pending, node) {
  1139. if (cookie == desc->async_tx.cookie)
  1140. return desc->size;
  1141. }
  1142. list_for_each_entry(desc, &chan->desc.active, node) {
  1143. if (cookie == desc->async_tx.cookie)
  1144. return desc->size;
  1145. }
  1146. /*
  1147. * No descriptor found for the cookie, there's thus no residue.
  1148. * This shouldn't happen if the calling driver passes a correct
  1149. * cookie value.
  1150. */
  1151. WARN(1, "No descriptor for cookie!");
  1152. return 0;
  1153. }
  1154. /*
  1155. * We need to read two registers.
  1156. * Make sure the control register does not skip to next chunk
  1157. * while reading the counter.
  1158. * Trying it 3 times should be enough: Initial read, retry, retry
  1159. * for the paranoid.
  1160. */
  1161. for (i = 0; i < 3; i++) {
  1162. chcrb = rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
  1163. RCAR_DMACHCRB_DPTR_MASK;
  1164. tcrb = rcar_dmac_chan_read(chan, RCAR_DMATCRB);
  1165. /* Still the same? */
  1166. if (chcrb == (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
  1167. RCAR_DMACHCRB_DPTR_MASK))
  1168. break;
  1169. }
  1170. WARN_ONCE(i >= 3, "residue might be not continuous!");
  1171. /*
  1172. * In descriptor mode the descriptor running pointer is not maintained
  1173. * by the interrupt handler, find the running descriptor from the
  1174. * descriptor pointer field in the CHCRB register. In non-descriptor
  1175. * mode just use the running descriptor pointer.
  1176. */
  1177. if (desc->hwdescs.use) {
  1178. dptr = chcrb >> RCAR_DMACHCRB_DPTR_SHIFT;
  1179. if (dptr == 0)
  1180. dptr = desc->nchunks;
  1181. dptr--;
  1182. WARN_ON(dptr >= desc->nchunks);
  1183. } else {
  1184. running = desc->running;
  1185. }
  1186. /* Compute the size of all chunks still to be transferred. */
  1187. list_for_each_entry_reverse(chunk, &desc->chunks, node) {
  1188. if (chunk == running || ++dptr == desc->nchunks)
  1189. break;
  1190. residue += chunk->size;
  1191. }
  1192. /* Add the residue for the current chunk. */
  1193. residue += tcrb << desc->xfer_shift;
  1194. return residue;
  1195. }
  1196. static enum dma_status rcar_dmac_tx_status(struct dma_chan *chan,
  1197. dma_cookie_t cookie,
  1198. struct dma_tx_state *txstate)
  1199. {
  1200. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  1201. enum dma_status status;
  1202. unsigned long flags;
  1203. unsigned int residue;
  1204. bool cyclic;
  1205. status = dma_cookie_status(chan, cookie, txstate);
  1206. if (status == DMA_COMPLETE || !txstate)
  1207. return status;
  1208. spin_lock_irqsave(&rchan->lock, flags);
  1209. residue = rcar_dmac_chan_get_residue(rchan, cookie);
  1210. cyclic = rchan->desc.running ? rchan->desc.running->cyclic : false;
  1211. spin_unlock_irqrestore(&rchan->lock, flags);
  1212. /* if there's no residue, the cookie is complete */
  1213. if (!residue && !cyclic)
  1214. return DMA_COMPLETE;
  1215. dma_set_residue(txstate, residue);
  1216. return status;
  1217. }
  1218. static void rcar_dmac_issue_pending(struct dma_chan *chan)
  1219. {
  1220. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  1221. unsigned long flags;
  1222. spin_lock_irqsave(&rchan->lock, flags);
  1223. if (list_empty(&rchan->desc.pending))
  1224. goto done;
  1225. /* Append the pending list to the active list. */
  1226. list_splice_tail_init(&rchan->desc.pending, &rchan->desc.active);
  1227. /*
  1228. * If no transfer is running pick the first descriptor from the active
  1229. * list and start the transfer.
  1230. */
  1231. if (!rchan->desc.running) {
  1232. struct rcar_dmac_desc *desc;
  1233. desc = list_first_entry(&rchan->desc.active,
  1234. struct rcar_dmac_desc, node);
  1235. rchan->desc.running = desc;
  1236. rcar_dmac_chan_start_xfer(rchan);
  1237. }
  1238. done:
  1239. spin_unlock_irqrestore(&rchan->lock, flags);
  1240. }
  1241. static void rcar_dmac_device_synchronize(struct dma_chan *chan)
  1242. {
  1243. struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
  1244. synchronize_irq(rchan->irq);
  1245. }
  1246. /* -----------------------------------------------------------------------------
  1247. * IRQ handling
  1248. */
  1249. static irqreturn_t rcar_dmac_isr_desc_stage_end(struct rcar_dmac_chan *chan)
  1250. {
  1251. struct rcar_dmac_desc *desc = chan->desc.running;
  1252. unsigned int stage;
  1253. if (WARN_ON(!desc || !desc->cyclic)) {
  1254. /*
  1255. * This should never happen, there should always be a running
  1256. * cyclic descriptor when a descriptor stage end interrupt is
  1257. * triggered. Warn and return.
  1258. */
  1259. return IRQ_NONE;
  1260. }
  1261. /* Program the interrupt pointer to the next stage. */
  1262. stage = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
  1263. RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT;
  1264. rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(stage));
  1265. return IRQ_WAKE_THREAD;
  1266. }
  1267. static irqreturn_t rcar_dmac_isr_transfer_end(struct rcar_dmac_chan *chan)
  1268. {
  1269. struct rcar_dmac_desc *desc = chan->desc.running;
  1270. irqreturn_t ret = IRQ_WAKE_THREAD;
  1271. if (WARN_ON_ONCE(!desc)) {
  1272. /*
  1273. * This should never happen, there should always be a running
  1274. * descriptor when a transfer end interrupt is triggered. Warn
  1275. * and return.
  1276. */
  1277. return IRQ_NONE;
  1278. }
  1279. /*
  1280. * The transfer end interrupt isn't generated for each chunk when using
  1281. * descriptor mode. Only update the running chunk pointer in
  1282. * non-descriptor mode.
  1283. */
  1284. if (!desc->hwdescs.use) {
  1285. /*
  1286. * If we haven't completed the last transfer chunk simply move
  1287. * to the next one. Only wake the IRQ thread if the transfer is
  1288. * cyclic.
  1289. */
  1290. if (!list_is_last(&desc->running->node, &desc->chunks)) {
  1291. desc->running = list_next_entry(desc->running, node);
  1292. if (!desc->cyclic)
  1293. ret = IRQ_HANDLED;
  1294. goto done;
  1295. }
  1296. /*
  1297. * We've completed the last transfer chunk. If the transfer is
  1298. * cyclic, move back to the first one.
  1299. */
  1300. if (desc->cyclic) {
  1301. desc->running =
  1302. list_first_entry(&desc->chunks,
  1303. struct rcar_dmac_xfer_chunk,
  1304. node);
  1305. goto done;
  1306. }
  1307. }
  1308. /* The descriptor is complete, move it to the done list. */
  1309. list_move_tail(&desc->node, &chan->desc.done);
  1310. /* Queue the next descriptor, if any. */
  1311. if (!list_empty(&chan->desc.active))
  1312. chan->desc.running = list_first_entry(&chan->desc.active,
  1313. struct rcar_dmac_desc,
  1314. node);
  1315. else
  1316. chan->desc.running = NULL;
  1317. done:
  1318. if (chan->desc.running)
  1319. rcar_dmac_chan_start_xfer(chan);
  1320. return ret;
  1321. }
  1322. static irqreturn_t rcar_dmac_isr_channel(int irq, void *dev)
  1323. {
  1324. u32 mask = RCAR_DMACHCR_DSE | RCAR_DMACHCR_TE;
  1325. struct rcar_dmac_chan *chan = dev;
  1326. irqreturn_t ret = IRQ_NONE;
  1327. bool reinit = false;
  1328. u32 chcr;
  1329. spin_lock(&chan->lock);
  1330. chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
  1331. if (chcr & RCAR_DMACHCR_CAE) {
  1332. struct rcar_dmac *dmac = to_rcar_dmac(chan->chan.device);
  1333. /*
  1334. * We don't need to call rcar_dmac_chan_halt()
  1335. * because channel is already stopped in error case.
  1336. * We need to clear register and check DE bit as recovery.
  1337. */
  1338. rcar_dmac_chan_clear(dmac, chan);
  1339. rcar_dmac_chcr_de_barrier(chan);
  1340. reinit = true;
  1341. goto spin_lock_end;
  1342. }
  1343. if (chcr & RCAR_DMACHCR_TE)
  1344. mask |= RCAR_DMACHCR_DE;
  1345. rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr & ~mask);
  1346. if (mask & RCAR_DMACHCR_DE)
  1347. rcar_dmac_chcr_de_barrier(chan);
  1348. if (chcr & RCAR_DMACHCR_DSE)
  1349. ret |= rcar_dmac_isr_desc_stage_end(chan);
  1350. if (chcr & RCAR_DMACHCR_TE)
  1351. ret |= rcar_dmac_isr_transfer_end(chan);
  1352. spin_lock_end:
  1353. spin_unlock(&chan->lock);
  1354. if (reinit) {
  1355. dev_err(chan->chan.device->dev, "Channel Address Error\n");
  1356. rcar_dmac_chan_reinit(chan);
  1357. ret = IRQ_HANDLED;
  1358. }
  1359. return ret;
  1360. }
  1361. static irqreturn_t rcar_dmac_isr_channel_thread(int irq, void *dev)
  1362. {
  1363. struct rcar_dmac_chan *chan = dev;
  1364. struct rcar_dmac_desc *desc;
  1365. struct dmaengine_desc_callback cb;
  1366. spin_lock_irq(&chan->lock);
  1367. /* For cyclic transfers notify the user after every chunk. */
  1368. if (chan->desc.running && chan->desc.running->cyclic) {
  1369. desc = chan->desc.running;
  1370. dmaengine_desc_get_callback(&desc->async_tx, &cb);
  1371. if (dmaengine_desc_callback_valid(&cb)) {
  1372. spin_unlock_irq(&chan->lock);
  1373. dmaengine_desc_callback_invoke(&cb, NULL);
  1374. spin_lock_irq(&chan->lock);
  1375. }
  1376. }
  1377. /*
  1378. * Call the callback function for all descriptors on the done list and
  1379. * move them to the ack wait list.
  1380. */
  1381. while (!list_empty(&chan->desc.done)) {
  1382. desc = list_first_entry(&chan->desc.done, struct rcar_dmac_desc,
  1383. node);
  1384. dma_cookie_complete(&desc->async_tx);
  1385. list_del(&desc->node);
  1386. dmaengine_desc_get_callback(&desc->async_tx, &cb);
  1387. if (dmaengine_desc_callback_valid(&cb)) {
  1388. spin_unlock_irq(&chan->lock);
  1389. /*
  1390. * We own the only reference to this descriptor, we can
  1391. * safely dereference it without holding the channel
  1392. * lock.
  1393. */
  1394. dmaengine_desc_callback_invoke(&cb, NULL);
  1395. spin_lock_irq(&chan->lock);
  1396. }
  1397. list_add_tail(&desc->node, &chan->desc.wait);
  1398. }
  1399. spin_unlock_irq(&chan->lock);
  1400. /* Recycle all acked descriptors. */
  1401. rcar_dmac_desc_recycle_acked(chan);
  1402. return IRQ_HANDLED;
  1403. }
  1404. /* -----------------------------------------------------------------------------
  1405. * OF xlate and channel filter
  1406. */
  1407. static bool rcar_dmac_chan_filter(struct dma_chan *chan, void *arg)
  1408. {
  1409. struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
  1410. struct of_phandle_args *dma_spec = arg;
  1411. /*
  1412. * FIXME: Using a filter on OF platforms is a nonsense. The OF xlate
  1413. * function knows from which device it wants to allocate a channel from,
  1414. * and would be perfectly capable of selecting the channel it wants.
  1415. * Forcing it to call dma_request_channel() and iterate through all
  1416. * channels from all controllers is just pointless.
  1417. */
  1418. if (chan->device->device_config != rcar_dmac_device_config)
  1419. return false;
  1420. return !test_and_set_bit(dma_spec->args[0], dmac->modules);
  1421. }
  1422. static struct dma_chan *rcar_dmac_of_xlate(struct of_phandle_args *dma_spec,
  1423. struct of_dma *ofdma)
  1424. {
  1425. struct rcar_dmac_chan *rchan;
  1426. struct dma_chan *chan;
  1427. dma_cap_mask_t mask;
  1428. if (dma_spec->args_count != 1)
  1429. return NULL;
  1430. /* Only slave DMA channels can be allocated via DT */
  1431. dma_cap_zero(mask);
  1432. dma_cap_set(DMA_SLAVE, mask);
  1433. chan = __dma_request_channel(&mask, rcar_dmac_chan_filter, dma_spec,
  1434. ofdma->of_node);
  1435. if (!chan)
  1436. return NULL;
  1437. rchan = to_rcar_dmac_chan(chan);
  1438. rchan->mid_rid = dma_spec->args[0];
  1439. return chan;
  1440. }
  1441. /* -----------------------------------------------------------------------------
  1442. * Power management
  1443. */
  1444. #ifdef CONFIG_PM
  1445. static int rcar_dmac_runtime_suspend(struct device *dev)
  1446. {
  1447. return 0;
  1448. }
  1449. static int rcar_dmac_runtime_resume(struct device *dev)
  1450. {
  1451. struct rcar_dmac *dmac = dev_get_drvdata(dev);
  1452. return rcar_dmac_init(dmac);
  1453. }
  1454. #endif
  1455. static const struct dev_pm_ops rcar_dmac_pm = {
  1456. /*
  1457. * TODO for system sleep/resume:
  1458. * - Wait for the current transfer to complete and stop the device,
  1459. * - Resume transfers, if any.
  1460. */
  1461. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1462. pm_runtime_force_resume)
  1463. SET_RUNTIME_PM_OPS(rcar_dmac_runtime_suspend, rcar_dmac_runtime_resume,
  1464. NULL)
  1465. };
  1466. /* -----------------------------------------------------------------------------
  1467. * Probe and remove
  1468. */
  1469. static int rcar_dmac_chan_probe(struct rcar_dmac *dmac,
  1470. struct rcar_dmac_chan *rchan)
  1471. {
  1472. struct platform_device *pdev = to_platform_device(dmac->dev);
  1473. struct dma_chan *chan = &rchan->chan;
  1474. char pdev_irqname[5];
  1475. char *irqname;
  1476. int ret;
  1477. rchan->mid_rid = -EINVAL;
  1478. spin_lock_init(&rchan->lock);
  1479. INIT_LIST_HEAD(&rchan->desc.free);
  1480. INIT_LIST_HEAD(&rchan->desc.pending);
  1481. INIT_LIST_HEAD(&rchan->desc.active);
  1482. INIT_LIST_HEAD(&rchan->desc.done);
  1483. INIT_LIST_HEAD(&rchan->desc.wait);
  1484. /* Request the channel interrupt. */
  1485. sprintf(pdev_irqname, "ch%u", rchan->index);
  1486. rchan->irq = platform_get_irq_byname(pdev, pdev_irqname);
  1487. if (rchan->irq < 0)
  1488. return -ENODEV;
  1489. irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u",
  1490. dev_name(dmac->dev), rchan->index);
  1491. if (!irqname)
  1492. return -ENOMEM;
  1493. /*
  1494. * Initialize the DMA engine channel and add it to the DMA engine
  1495. * channels list.
  1496. */
  1497. chan->device = &dmac->engine;
  1498. dma_cookie_init(chan);
  1499. list_add_tail(&chan->device_node, &dmac->engine.channels);
  1500. ret = devm_request_threaded_irq(dmac->dev, rchan->irq,
  1501. rcar_dmac_isr_channel,
  1502. rcar_dmac_isr_channel_thread, 0,
  1503. irqname, rchan);
  1504. if (ret) {
  1505. dev_err(dmac->dev, "failed to request IRQ %u (%d)\n",
  1506. rchan->irq, ret);
  1507. return ret;
  1508. }
  1509. return 0;
  1510. }
  1511. #define RCAR_DMAC_MAX_CHANNELS 32
  1512. static int rcar_dmac_parse_of(struct device *dev, struct rcar_dmac *dmac)
  1513. {
  1514. struct device_node *np = dev->of_node;
  1515. int ret;
  1516. ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels);
  1517. if (ret < 0) {
  1518. dev_err(dev, "unable to read dma-channels property\n");
  1519. return ret;
  1520. }
  1521. /* The hardware and driver don't support more than 32 bits in CHCLR */
  1522. if (dmac->n_channels <= 0 ||
  1523. dmac->n_channels >= RCAR_DMAC_MAX_CHANNELS) {
  1524. dev_err(dev, "invalid number of channels %u\n",
  1525. dmac->n_channels);
  1526. return -EINVAL;
  1527. }
  1528. /*
  1529. * If the driver is unable to read dma-channel-mask property,
  1530. * the driver assumes that it can use all channels.
  1531. */
  1532. dmac->channels_mask = GENMASK(dmac->n_channels - 1, 0);
  1533. of_property_read_u32(np, "dma-channel-mask", &dmac->channels_mask);
  1534. /* If the property has out-of-channel mask, this driver clears it */
  1535. dmac->channels_mask &= GENMASK(dmac->n_channels - 1, 0);
  1536. return 0;
  1537. }
  1538. static int rcar_dmac_probe(struct platform_device *pdev)
  1539. {
  1540. const enum dma_slave_buswidth widths = DMA_SLAVE_BUSWIDTH_1_BYTE |
  1541. DMA_SLAVE_BUSWIDTH_2_BYTES | DMA_SLAVE_BUSWIDTH_4_BYTES |
  1542. DMA_SLAVE_BUSWIDTH_8_BYTES | DMA_SLAVE_BUSWIDTH_16_BYTES |
  1543. DMA_SLAVE_BUSWIDTH_32_BYTES | DMA_SLAVE_BUSWIDTH_64_BYTES;
  1544. const struct rcar_dmac_of_data *data;
  1545. struct rcar_dmac_chan *chan;
  1546. struct dma_device *engine;
  1547. void __iomem *chan_base;
  1548. struct rcar_dmac *dmac;
  1549. unsigned int i;
  1550. int ret;
  1551. data = of_device_get_match_data(&pdev->dev);
  1552. if (!data)
  1553. return -EINVAL;
  1554. dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
  1555. if (!dmac)
  1556. return -ENOMEM;
  1557. dmac->dev = &pdev->dev;
  1558. platform_set_drvdata(pdev, dmac);
  1559. dma_set_max_seg_size(dmac->dev, RCAR_DMATCR_MASK);
  1560. ret = dma_set_mask_and_coherent(dmac->dev, DMA_BIT_MASK(40));
  1561. if (ret)
  1562. return ret;
  1563. ret = rcar_dmac_parse_of(&pdev->dev, dmac);
  1564. if (ret < 0)
  1565. return ret;
  1566. /*
  1567. * A still unconfirmed hardware bug prevents the IPMMU microTLB 0 to be
  1568. * flushed correctly, resulting in memory corruption. DMAC 0 channel 0
  1569. * is connected to microTLB 0 on currently supported platforms, so we
  1570. * can't use it with the IPMMU. As the IOMMU API operates at the device
  1571. * level we can't disable it selectively, so ignore channel 0 for now if
  1572. * the device is part of an IOMMU group.
  1573. */
  1574. if (device_iommu_mapped(&pdev->dev))
  1575. dmac->channels_mask &= ~BIT(0);
  1576. dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels,
  1577. sizeof(*dmac->channels), GFP_KERNEL);
  1578. if (!dmac->channels)
  1579. return -ENOMEM;
  1580. /* Request resources. */
  1581. dmac->dmac_base = devm_platform_ioremap_resource(pdev, 0);
  1582. if (IS_ERR(dmac->dmac_base))
  1583. return PTR_ERR(dmac->dmac_base);
  1584. if (!data->chan_offset_base) {
  1585. dmac->chan_base = devm_platform_ioremap_resource(pdev, 1);
  1586. if (IS_ERR(dmac->chan_base))
  1587. return PTR_ERR(dmac->chan_base);
  1588. chan_base = dmac->chan_base;
  1589. } else {
  1590. chan_base = dmac->dmac_base + data->chan_offset_base;
  1591. }
  1592. for_each_rcar_dmac_chan(i, dmac, chan) {
  1593. chan->index = i;
  1594. chan->iomem = chan_base + i * data->chan_offset_stride;
  1595. }
  1596. /* Enable runtime PM and initialize the device. */
  1597. pm_runtime_enable(&pdev->dev);
  1598. ret = pm_runtime_resume_and_get(&pdev->dev);
  1599. if (ret < 0) {
  1600. dev_err(&pdev->dev, "runtime PM get sync failed (%d)\n", ret);
  1601. goto err_pm_disable;
  1602. }
  1603. ret = rcar_dmac_init(dmac);
  1604. pm_runtime_put(&pdev->dev);
  1605. if (ret) {
  1606. dev_err(&pdev->dev, "failed to reset device\n");
  1607. goto err_pm_disable;
  1608. }
  1609. /* Initialize engine */
  1610. engine = &dmac->engine;
  1611. dma_cap_set(DMA_MEMCPY, engine->cap_mask);
  1612. dma_cap_set(DMA_SLAVE, engine->cap_mask);
  1613. engine->dev = &pdev->dev;
  1614. engine->copy_align = ilog2(RCAR_DMAC_MEMCPY_XFER_SIZE);
  1615. engine->src_addr_widths = widths;
  1616. engine->dst_addr_widths = widths;
  1617. engine->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  1618. engine->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1619. engine->device_alloc_chan_resources = rcar_dmac_alloc_chan_resources;
  1620. engine->device_free_chan_resources = rcar_dmac_free_chan_resources;
  1621. engine->device_prep_dma_memcpy = rcar_dmac_prep_dma_memcpy;
  1622. engine->device_prep_slave_sg = rcar_dmac_prep_slave_sg;
  1623. engine->device_prep_dma_cyclic = rcar_dmac_prep_dma_cyclic;
  1624. engine->device_config = rcar_dmac_device_config;
  1625. engine->device_pause = rcar_dmac_chan_pause;
  1626. engine->device_terminate_all = rcar_dmac_chan_terminate_all;
  1627. engine->device_tx_status = rcar_dmac_tx_status;
  1628. engine->device_issue_pending = rcar_dmac_issue_pending;
  1629. engine->device_synchronize = rcar_dmac_device_synchronize;
  1630. INIT_LIST_HEAD(&engine->channels);
  1631. for_each_rcar_dmac_chan(i, dmac, chan) {
  1632. ret = rcar_dmac_chan_probe(dmac, chan);
  1633. if (ret < 0)
  1634. goto err_pm_disable;
  1635. }
  1636. /* Register the DMAC as a DMA provider for DT. */
  1637. ret = of_dma_controller_register(pdev->dev.of_node, rcar_dmac_of_xlate,
  1638. NULL);
  1639. if (ret < 0)
  1640. goto err_pm_disable;
  1641. /*
  1642. * Register the DMA engine device.
  1643. *
  1644. * Default transfer size of 32 bytes requires 32-byte alignment.
  1645. */
  1646. ret = dma_async_device_register(engine);
  1647. if (ret < 0)
  1648. goto err_dma_free;
  1649. return 0;
  1650. err_dma_free:
  1651. of_dma_controller_free(pdev->dev.of_node);
  1652. err_pm_disable:
  1653. pm_runtime_disable(&pdev->dev);
  1654. return ret;
  1655. }
  1656. static void rcar_dmac_remove(struct platform_device *pdev)
  1657. {
  1658. struct rcar_dmac *dmac = platform_get_drvdata(pdev);
  1659. of_dma_controller_free(pdev->dev.of_node);
  1660. dma_async_device_unregister(&dmac->engine);
  1661. pm_runtime_disable(&pdev->dev);
  1662. }
  1663. static void rcar_dmac_shutdown(struct platform_device *pdev)
  1664. {
  1665. struct rcar_dmac *dmac = platform_get_drvdata(pdev);
  1666. rcar_dmac_stop_all_chan(dmac);
  1667. }
  1668. static const struct rcar_dmac_of_data rcar_dmac_data = {
  1669. .chan_offset_base = 0x8000,
  1670. .chan_offset_stride = 0x80,
  1671. };
  1672. static const struct rcar_dmac_of_data rcar_gen4_dmac_data = {
  1673. .chan_offset_base = 0x0,
  1674. .chan_offset_stride = 0x1000,
  1675. };
  1676. static const struct of_device_id rcar_dmac_of_ids[] = {
  1677. {
  1678. .compatible = "renesas,rcar-dmac",
  1679. .data = &rcar_dmac_data,
  1680. }, {
  1681. .compatible = "renesas,rcar-gen4-dmac",
  1682. .data = &rcar_gen4_dmac_data,
  1683. }, {
  1684. .compatible = "renesas,dmac-r8a779a0",
  1685. .data = &rcar_gen4_dmac_data,
  1686. },
  1687. { /* Sentinel */ }
  1688. };
  1689. MODULE_DEVICE_TABLE(of, rcar_dmac_of_ids);
  1690. static struct platform_driver rcar_dmac_driver = {
  1691. .driver = {
  1692. .pm = &rcar_dmac_pm,
  1693. .name = "rcar-dmac",
  1694. .of_match_table = rcar_dmac_of_ids,
  1695. },
  1696. .probe = rcar_dmac_probe,
  1697. .remove_new = rcar_dmac_remove,
  1698. .shutdown = rcar_dmac_shutdown,
  1699. };
  1700. module_platform_driver(rcar_dmac_driver);
  1701. MODULE_DESCRIPTION("R-Car Gen2 DMA Controller Driver");
  1702. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  1703. MODULE_LICENSE("GPL v2");