owl-dma.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Actions Semi Owl SoCs DMA driver
  4. //
  5. // Copyright (c) 2014 Actions Semi Inc.
  6. // Author: David Liu <liuwei@actions-semi.com>
  7. //
  8. // Copyright (c) 2018 Linaro Ltd.
  9. // Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  10. #include <linux/bitops.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_dma.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include "virt-dma.h"
  27. #define OWL_DMA_FRAME_MAX_LENGTH 0xfffff
  28. /* Global DMA Controller Registers */
  29. #define OWL_DMA_IRQ_PD0 0x00
  30. #define OWL_DMA_IRQ_PD1 0x04
  31. #define OWL_DMA_IRQ_PD2 0x08
  32. #define OWL_DMA_IRQ_PD3 0x0C
  33. #define OWL_DMA_IRQ_EN0 0x10
  34. #define OWL_DMA_IRQ_EN1 0x14
  35. #define OWL_DMA_IRQ_EN2 0x18
  36. #define OWL_DMA_IRQ_EN3 0x1C
  37. #define OWL_DMA_SECURE_ACCESS_CTL 0x20
  38. #define OWL_DMA_NIC_QOS 0x24
  39. #define OWL_DMA_DBGSEL 0x28
  40. #define OWL_DMA_IDLE_STAT 0x2C
  41. /* Channel Registers */
  42. #define OWL_DMA_CHAN_BASE(i) (0x100 + (i) * 0x100)
  43. #define OWL_DMAX_MODE 0x00
  44. #define OWL_DMAX_SOURCE 0x04
  45. #define OWL_DMAX_DESTINATION 0x08
  46. #define OWL_DMAX_FRAME_LEN 0x0C
  47. #define OWL_DMAX_FRAME_CNT 0x10
  48. #define OWL_DMAX_REMAIN_FRAME_CNT 0x14
  49. #define OWL_DMAX_REMAIN_CNT 0x18
  50. #define OWL_DMAX_SOURCE_STRIDE 0x1C
  51. #define OWL_DMAX_DESTINATION_STRIDE 0x20
  52. #define OWL_DMAX_START 0x24
  53. #define OWL_DMAX_PAUSE 0x28
  54. #define OWL_DMAX_CHAINED_CTL 0x2C
  55. #define OWL_DMAX_CONSTANT 0x30
  56. #define OWL_DMAX_LINKLIST_CTL 0x34
  57. #define OWL_DMAX_NEXT_DESCRIPTOR 0x38
  58. #define OWL_DMAX_CURRENT_DESCRIPTOR_NUM 0x3C
  59. #define OWL_DMAX_INT_CTL 0x40
  60. #define OWL_DMAX_INT_STATUS 0x44
  61. #define OWL_DMAX_CURRENT_SOURCE_POINTER 0x48
  62. #define OWL_DMAX_CURRENT_DESTINATION_POINTER 0x4C
  63. /* OWL_DMAX_MODE Bits */
  64. #define OWL_DMA_MODE_TS(x) (((x) & GENMASK(5, 0)) << 0)
  65. #define OWL_DMA_MODE_ST(x) (((x) & GENMASK(1, 0)) << 8)
  66. #define OWL_DMA_MODE_ST_DEV OWL_DMA_MODE_ST(0)
  67. #define OWL_DMA_MODE_ST_DCU OWL_DMA_MODE_ST(2)
  68. #define OWL_DMA_MODE_ST_SRAM OWL_DMA_MODE_ST(3)
  69. #define OWL_DMA_MODE_DT(x) (((x) & GENMASK(1, 0)) << 10)
  70. #define OWL_DMA_MODE_DT_DEV OWL_DMA_MODE_DT(0)
  71. #define OWL_DMA_MODE_DT_DCU OWL_DMA_MODE_DT(2)
  72. #define OWL_DMA_MODE_DT_SRAM OWL_DMA_MODE_DT(3)
  73. #define OWL_DMA_MODE_SAM(x) (((x) & GENMASK(1, 0)) << 16)
  74. #define OWL_DMA_MODE_SAM_CONST OWL_DMA_MODE_SAM(0)
  75. #define OWL_DMA_MODE_SAM_INC OWL_DMA_MODE_SAM(1)
  76. #define OWL_DMA_MODE_SAM_STRIDE OWL_DMA_MODE_SAM(2)
  77. #define OWL_DMA_MODE_DAM(x) (((x) & GENMASK(1, 0)) << 18)
  78. #define OWL_DMA_MODE_DAM_CONST OWL_DMA_MODE_DAM(0)
  79. #define OWL_DMA_MODE_DAM_INC OWL_DMA_MODE_DAM(1)
  80. #define OWL_DMA_MODE_DAM_STRIDE OWL_DMA_MODE_DAM(2)
  81. #define OWL_DMA_MODE_PW(x) (((x) & GENMASK(2, 0)) << 20)
  82. #define OWL_DMA_MODE_CB BIT(23)
  83. #define OWL_DMA_MODE_NDDBW(x) (((x) & 0x1) << 28)
  84. #define OWL_DMA_MODE_NDDBW_32BIT OWL_DMA_MODE_NDDBW(0)
  85. #define OWL_DMA_MODE_NDDBW_8BIT OWL_DMA_MODE_NDDBW(1)
  86. #define OWL_DMA_MODE_CFE BIT(29)
  87. #define OWL_DMA_MODE_LME BIT(30)
  88. #define OWL_DMA_MODE_CME BIT(31)
  89. /* OWL_DMAX_LINKLIST_CTL Bits */
  90. #define OWL_DMA_LLC_SAV(x) (((x) & GENMASK(1, 0)) << 8)
  91. #define OWL_DMA_LLC_SAV_INC OWL_DMA_LLC_SAV(0)
  92. #define OWL_DMA_LLC_SAV_LOAD_NEXT OWL_DMA_LLC_SAV(1)
  93. #define OWL_DMA_LLC_SAV_LOAD_PREV OWL_DMA_LLC_SAV(2)
  94. #define OWL_DMA_LLC_DAV(x) (((x) & GENMASK(1, 0)) << 10)
  95. #define OWL_DMA_LLC_DAV_INC OWL_DMA_LLC_DAV(0)
  96. #define OWL_DMA_LLC_DAV_LOAD_NEXT OWL_DMA_LLC_DAV(1)
  97. #define OWL_DMA_LLC_DAV_LOAD_PREV OWL_DMA_LLC_DAV(2)
  98. #define OWL_DMA_LLC_SUSPEND BIT(16)
  99. /* OWL_DMAX_INT_CTL Bits */
  100. #define OWL_DMA_INTCTL_BLOCK BIT(0)
  101. #define OWL_DMA_INTCTL_SUPER_BLOCK BIT(1)
  102. #define OWL_DMA_INTCTL_FRAME BIT(2)
  103. #define OWL_DMA_INTCTL_HALF_FRAME BIT(3)
  104. #define OWL_DMA_INTCTL_LAST_FRAME BIT(4)
  105. /* OWL_DMAX_INT_STATUS Bits */
  106. #define OWL_DMA_INTSTAT_BLOCK BIT(0)
  107. #define OWL_DMA_INTSTAT_SUPER_BLOCK BIT(1)
  108. #define OWL_DMA_INTSTAT_FRAME BIT(2)
  109. #define OWL_DMA_INTSTAT_HALF_FRAME BIT(3)
  110. #define OWL_DMA_INTSTAT_LAST_FRAME BIT(4)
  111. /* Pack shift and newshift in a single word */
  112. #define BIT_FIELD(val, width, shift, newshift) \
  113. ((((val) >> (shift)) & ((BIT(width)) - 1)) << (newshift))
  114. /* Frame count value is fixed as 1 */
  115. #define FCNT_VAL 0x1
  116. /**
  117. * enum owl_dmadesc_offsets - Describe DMA descriptor, hardware link
  118. * list for dma transfer
  119. * @OWL_DMADESC_NEXT_LLI: physical address of the next link list
  120. * @OWL_DMADESC_SADDR: source physical address
  121. * @OWL_DMADESC_DADDR: destination physical address
  122. * @OWL_DMADESC_FLEN: frame length
  123. * @OWL_DMADESC_SRC_STRIDE: source stride
  124. * @OWL_DMADESC_DST_STRIDE: destination stride
  125. * @OWL_DMADESC_CTRLA: dma_mode and linklist ctrl config
  126. * @OWL_DMADESC_CTRLB: interrupt config
  127. * @OWL_DMADESC_CONST_NUM: data for constant fill
  128. * @OWL_DMADESC_SIZE: max size of this enum
  129. */
  130. enum owl_dmadesc_offsets {
  131. OWL_DMADESC_NEXT_LLI = 0,
  132. OWL_DMADESC_SADDR,
  133. OWL_DMADESC_DADDR,
  134. OWL_DMADESC_FLEN,
  135. OWL_DMADESC_SRC_STRIDE,
  136. OWL_DMADESC_DST_STRIDE,
  137. OWL_DMADESC_CTRLA,
  138. OWL_DMADESC_CTRLB,
  139. OWL_DMADESC_CONST_NUM,
  140. OWL_DMADESC_SIZE
  141. };
  142. enum owl_dma_id {
  143. S900_DMA,
  144. S700_DMA,
  145. };
  146. /**
  147. * struct owl_dma_lli - Link list for dma transfer
  148. * @hw: hardware link list
  149. * @phys: physical address of hardware link list
  150. * @node: node for txd's lli_list
  151. */
  152. struct owl_dma_lli {
  153. u32 hw[OWL_DMADESC_SIZE];
  154. dma_addr_t phys;
  155. struct list_head node;
  156. };
  157. /**
  158. * struct owl_dma_txd - Wrapper for struct dma_async_tx_descriptor
  159. * @vd: virtual DMA descriptor
  160. * @lli_list: link list of lli nodes
  161. * @cyclic: flag to indicate cyclic transfers
  162. */
  163. struct owl_dma_txd {
  164. struct virt_dma_desc vd;
  165. struct list_head lli_list;
  166. bool cyclic;
  167. };
  168. /**
  169. * struct owl_dma_pchan - Holder for the physical channels
  170. * @id: physical index to this channel
  171. * @base: virtual memory base for the dma channel
  172. * @vchan: the virtual channel currently being served by this physical channel
  173. */
  174. struct owl_dma_pchan {
  175. u32 id;
  176. void __iomem *base;
  177. struct owl_dma_vchan *vchan;
  178. };
  179. /**
  180. * struct owl_dma_vchan - Wrapper for DMA ENGINE channel
  181. * @vc: wrapped virtual channel
  182. * @pchan: the physical channel utilized by this channel
  183. * @txd: active transaction on this channel
  184. * @cfg: slave configuration for this channel
  185. * @drq: physical DMA request ID for this channel
  186. */
  187. struct owl_dma_vchan {
  188. struct virt_dma_chan vc;
  189. struct owl_dma_pchan *pchan;
  190. struct owl_dma_txd *txd;
  191. struct dma_slave_config cfg;
  192. u8 drq;
  193. };
  194. /**
  195. * struct owl_dma - Holder for the Owl DMA controller
  196. * @dma: dma engine for this instance
  197. * @base: virtual memory base for the DMA controller
  198. * @clk: clock for the DMA controller
  199. * @lock: a lock to use when change DMA controller global register
  200. * @lli_pool: a pool for the LLI descriptors
  201. * @irq: interrupt ID for the DMA controller
  202. * @nr_pchans: the number of physical channels
  203. * @pchans: array of data for the physical channels
  204. * @nr_vchans: the number of physical channels
  205. * @vchans: array of data for the physical channels
  206. * @devid: device id based on OWL SoC
  207. */
  208. struct owl_dma {
  209. struct dma_device dma;
  210. void __iomem *base;
  211. struct clk *clk;
  212. spinlock_t lock;
  213. struct dma_pool *lli_pool;
  214. int irq;
  215. unsigned int nr_pchans;
  216. struct owl_dma_pchan *pchans;
  217. unsigned int nr_vchans;
  218. struct owl_dma_vchan *vchans;
  219. enum owl_dma_id devid;
  220. };
  221. static void pchan_update(struct owl_dma_pchan *pchan, u32 reg,
  222. u32 val, bool state)
  223. {
  224. u32 regval;
  225. regval = readl(pchan->base + reg);
  226. if (state)
  227. regval |= val;
  228. else
  229. regval &= ~val;
  230. writel(regval, pchan->base + reg);
  231. }
  232. static void pchan_writel(struct owl_dma_pchan *pchan, u32 reg, u32 data)
  233. {
  234. writel(data, pchan->base + reg);
  235. }
  236. static u32 pchan_readl(struct owl_dma_pchan *pchan, u32 reg)
  237. {
  238. return readl(pchan->base + reg);
  239. }
  240. static void dma_update(struct owl_dma *od, u32 reg, u32 val, bool state)
  241. {
  242. u32 regval;
  243. regval = readl(od->base + reg);
  244. if (state)
  245. regval |= val;
  246. else
  247. regval &= ~val;
  248. writel(regval, od->base + reg);
  249. }
  250. static void dma_writel(struct owl_dma *od, u32 reg, u32 data)
  251. {
  252. writel(data, od->base + reg);
  253. }
  254. static u32 dma_readl(struct owl_dma *od, u32 reg)
  255. {
  256. return readl(od->base + reg);
  257. }
  258. static inline struct owl_dma *to_owl_dma(struct dma_device *dd)
  259. {
  260. return container_of(dd, struct owl_dma, dma);
  261. }
  262. static struct device *chan2dev(struct dma_chan *chan)
  263. {
  264. return &chan->dev->device;
  265. }
  266. static inline struct owl_dma_vchan *to_owl_vchan(struct dma_chan *chan)
  267. {
  268. return container_of(chan, struct owl_dma_vchan, vc.chan);
  269. }
  270. static inline struct owl_dma_txd *to_owl_txd(struct dma_async_tx_descriptor *tx)
  271. {
  272. return container_of(tx, struct owl_dma_txd, vd.tx);
  273. }
  274. static inline u32 llc_hw_ctrla(u32 mode, u32 llc_ctl)
  275. {
  276. u32 ctl;
  277. ctl = BIT_FIELD(mode, 4, 28, 28) |
  278. BIT_FIELD(mode, 8, 16, 20) |
  279. BIT_FIELD(mode, 4, 8, 16) |
  280. BIT_FIELD(mode, 6, 0, 10) |
  281. BIT_FIELD(llc_ctl, 2, 10, 8) |
  282. BIT_FIELD(llc_ctl, 2, 8, 6);
  283. return ctl;
  284. }
  285. static inline u32 llc_hw_ctrlb(u32 int_ctl)
  286. {
  287. u32 ctl;
  288. /*
  289. * Irrespective of the SoC, ctrlb value starts filling from
  290. * bit 18.
  291. */
  292. ctl = BIT_FIELD(int_ctl, 7, 0, 18);
  293. return ctl;
  294. }
  295. static u32 llc_hw_flen(struct owl_dma_lli *lli)
  296. {
  297. return lli->hw[OWL_DMADESC_FLEN] & GENMASK(19, 0);
  298. }
  299. static void owl_dma_free_lli(struct owl_dma *od,
  300. struct owl_dma_lli *lli)
  301. {
  302. list_del(&lli->node);
  303. dma_pool_free(od->lli_pool, lli, lli->phys);
  304. }
  305. static struct owl_dma_lli *owl_dma_alloc_lli(struct owl_dma *od)
  306. {
  307. struct owl_dma_lli *lli;
  308. dma_addr_t phys;
  309. lli = dma_pool_alloc(od->lli_pool, GFP_NOWAIT, &phys);
  310. if (!lli)
  311. return NULL;
  312. INIT_LIST_HEAD(&lli->node);
  313. lli->phys = phys;
  314. return lli;
  315. }
  316. static struct owl_dma_lli *owl_dma_add_lli(struct owl_dma_txd *txd,
  317. struct owl_dma_lli *prev,
  318. struct owl_dma_lli *next,
  319. bool is_cyclic)
  320. {
  321. if (!is_cyclic)
  322. list_add_tail(&next->node, &txd->lli_list);
  323. if (prev) {
  324. prev->hw[OWL_DMADESC_NEXT_LLI] = next->phys;
  325. prev->hw[OWL_DMADESC_CTRLA] |=
  326. llc_hw_ctrla(OWL_DMA_MODE_LME, 0);
  327. }
  328. return next;
  329. }
  330. static inline int owl_dma_cfg_lli(struct owl_dma_vchan *vchan,
  331. struct owl_dma_lli *lli,
  332. dma_addr_t src, dma_addr_t dst,
  333. u32 len, enum dma_transfer_direction dir,
  334. struct dma_slave_config *sconfig,
  335. bool is_cyclic)
  336. {
  337. struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
  338. u32 mode, ctrlb;
  339. mode = OWL_DMA_MODE_PW(0);
  340. switch (dir) {
  341. case DMA_MEM_TO_MEM:
  342. mode |= OWL_DMA_MODE_TS(0) | OWL_DMA_MODE_ST_DCU |
  343. OWL_DMA_MODE_DT_DCU | OWL_DMA_MODE_SAM_INC |
  344. OWL_DMA_MODE_DAM_INC;
  345. break;
  346. case DMA_MEM_TO_DEV:
  347. mode |= OWL_DMA_MODE_TS(vchan->drq)
  348. | OWL_DMA_MODE_ST_DCU | OWL_DMA_MODE_DT_DEV
  349. | OWL_DMA_MODE_SAM_INC | OWL_DMA_MODE_DAM_CONST;
  350. /*
  351. * Hardware only supports 32bit and 8bit buswidth. Since the
  352. * default is 32bit, select 8bit only when requested.
  353. */
  354. if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  355. mode |= OWL_DMA_MODE_NDDBW_8BIT;
  356. break;
  357. case DMA_DEV_TO_MEM:
  358. mode |= OWL_DMA_MODE_TS(vchan->drq)
  359. | OWL_DMA_MODE_ST_DEV | OWL_DMA_MODE_DT_DCU
  360. | OWL_DMA_MODE_SAM_CONST | OWL_DMA_MODE_DAM_INC;
  361. /*
  362. * Hardware only supports 32bit and 8bit buswidth. Since the
  363. * default is 32bit, select 8bit only when requested.
  364. */
  365. if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  366. mode |= OWL_DMA_MODE_NDDBW_8BIT;
  367. break;
  368. default:
  369. return -EINVAL;
  370. }
  371. lli->hw[OWL_DMADESC_CTRLA] = llc_hw_ctrla(mode,
  372. OWL_DMA_LLC_SAV_LOAD_NEXT |
  373. OWL_DMA_LLC_DAV_LOAD_NEXT);
  374. if (is_cyclic)
  375. ctrlb = llc_hw_ctrlb(OWL_DMA_INTCTL_BLOCK);
  376. else
  377. ctrlb = llc_hw_ctrlb(OWL_DMA_INTCTL_SUPER_BLOCK);
  378. lli->hw[OWL_DMADESC_NEXT_LLI] = 0; /* One link list by default */
  379. lli->hw[OWL_DMADESC_SADDR] = src;
  380. lli->hw[OWL_DMADESC_DADDR] = dst;
  381. lli->hw[OWL_DMADESC_SRC_STRIDE] = 0;
  382. lli->hw[OWL_DMADESC_DST_STRIDE] = 0;
  383. if (od->devid == S700_DMA) {
  384. /* Max frame length is 1MB */
  385. lli->hw[OWL_DMADESC_FLEN] = len;
  386. /*
  387. * On S700, word starts from offset 0x1C is shared between
  388. * frame count and ctrlb, where first 12 bits are for frame
  389. * count and rest of 20 bits are for ctrlb.
  390. */
  391. lli->hw[OWL_DMADESC_CTRLB] = FCNT_VAL | ctrlb;
  392. } else {
  393. /*
  394. * On S900, word starts from offset 0xC is shared between
  395. * frame length (max frame length is 1MB) and frame count,
  396. * where first 20 bits are for frame length and rest of
  397. * 12 bits are for frame count.
  398. */
  399. lli->hw[OWL_DMADESC_FLEN] = len | FCNT_VAL << 20;
  400. lli->hw[OWL_DMADESC_CTRLB] = ctrlb;
  401. }
  402. return 0;
  403. }
  404. static struct owl_dma_pchan *owl_dma_get_pchan(struct owl_dma *od,
  405. struct owl_dma_vchan *vchan)
  406. {
  407. struct owl_dma_pchan *pchan = NULL;
  408. unsigned long flags;
  409. int i;
  410. for (i = 0; i < od->nr_pchans; i++) {
  411. pchan = &od->pchans[i];
  412. spin_lock_irqsave(&od->lock, flags);
  413. if (!pchan->vchan) {
  414. pchan->vchan = vchan;
  415. spin_unlock_irqrestore(&od->lock, flags);
  416. break;
  417. }
  418. spin_unlock_irqrestore(&od->lock, flags);
  419. }
  420. return pchan;
  421. }
  422. static int owl_dma_pchan_busy(struct owl_dma *od, struct owl_dma_pchan *pchan)
  423. {
  424. unsigned int val;
  425. val = dma_readl(od, OWL_DMA_IDLE_STAT);
  426. return !(val & (1 << pchan->id));
  427. }
  428. static void owl_dma_terminate_pchan(struct owl_dma *od,
  429. struct owl_dma_pchan *pchan)
  430. {
  431. unsigned long flags;
  432. u32 irq_pd;
  433. pchan_writel(pchan, OWL_DMAX_START, 0);
  434. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  435. spin_lock_irqsave(&od->lock, flags);
  436. dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), false);
  437. irq_pd = dma_readl(od, OWL_DMA_IRQ_PD0);
  438. if (irq_pd & (1 << pchan->id)) {
  439. dev_warn(od->dma.dev,
  440. "terminating pchan %d that still has pending irq\n",
  441. pchan->id);
  442. dma_writel(od, OWL_DMA_IRQ_PD0, (1 << pchan->id));
  443. }
  444. pchan->vchan = NULL;
  445. spin_unlock_irqrestore(&od->lock, flags);
  446. }
  447. static void owl_dma_pause_pchan(struct owl_dma_pchan *pchan)
  448. {
  449. pchan_writel(pchan, 1, OWL_DMAX_PAUSE);
  450. }
  451. static void owl_dma_resume_pchan(struct owl_dma_pchan *pchan)
  452. {
  453. pchan_writel(pchan, 0, OWL_DMAX_PAUSE);
  454. }
  455. static int owl_dma_start_next_txd(struct owl_dma_vchan *vchan)
  456. {
  457. struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
  458. struct virt_dma_desc *vd = vchan_next_desc(&vchan->vc);
  459. struct owl_dma_pchan *pchan = vchan->pchan;
  460. struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
  461. struct owl_dma_lli *lli;
  462. unsigned long flags;
  463. u32 int_ctl;
  464. list_del(&vd->node);
  465. vchan->txd = txd;
  466. /* Wait for channel inactive */
  467. while (owl_dma_pchan_busy(od, pchan))
  468. cpu_relax();
  469. lli = list_first_entry(&txd->lli_list,
  470. struct owl_dma_lli, node);
  471. if (txd->cyclic)
  472. int_ctl = OWL_DMA_INTCTL_BLOCK;
  473. else
  474. int_ctl = OWL_DMA_INTCTL_SUPER_BLOCK;
  475. pchan_writel(pchan, OWL_DMAX_MODE, OWL_DMA_MODE_LME);
  476. pchan_writel(pchan, OWL_DMAX_LINKLIST_CTL,
  477. OWL_DMA_LLC_SAV_LOAD_NEXT | OWL_DMA_LLC_DAV_LOAD_NEXT);
  478. pchan_writel(pchan, OWL_DMAX_NEXT_DESCRIPTOR, lli->phys);
  479. pchan_writel(pchan, OWL_DMAX_INT_CTL, int_ctl);
  480. /* Clear IRQ status for this pchan */
  481. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  482. spin_lock_irqsave(&od->lock, flags);
  483. dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), true);
  484. spin_unlock_irqrestore(&od->lock, flags);
  485. dev_dbg(chan2dev(&vchan->vc.chan), "starting pchan %d\n", pchan->id);
  486. /* Start DMA transfer for this pchan */
  487. pchan_writel(pchan, OWL_DMAX_START, 0x1);
  488. return 0;
  489. }
  490. static void owl_dma_phy_free(struct owl_dma *od, struct owl_dma_vchan *vchan)
  491. {
  492. /* Ensure that the physical channel is stopped */
  493. owl_dma_terminate_pchan(od, vchan->pchan);
  494. vchan->pchan = NULL;
  495. }
  496. static irqreturn_t owl_dma_interrupt(int irq, void *dev_id)
  497. {
  498. struct owl_dma *od = dev_id;
  499. struct owl_dma_vchan *vchan;
  500. struct owl_dma_pchan *pchan;
  501. unsigned long pending;
  502. int i;
  503. unsigned int global_irq_pending, chan_irq_pending;
  504. spin_lock(&od->lock);
  505. pending = dma_readl(od, OWL_DMA_IRQ_PD0);
  506. /* Clear IRQ status for each pchan */
  507. for_each_set_bit(i, &pending, od->nr_pchans) {
  508. pchan = &od->pchans[i];
  509. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  510. }
  511. /* Clear pending IRQ */
  512. dma_writel(od, OWL_DMA_IRQ_PD0, pending);
  513. /* Check missed pending IRQ */
  514. for (i = 0; i < od->nr_pchans; i++) {
  515. pchan = &od->pchans[i];
  516. chan_irq_pending = pchan_readl(pchan, OWL_DMAX_INT_CTL) &
  517. pchan_readl(pchan, OWL_DMAX_INT_STATUS);
  518. /* Dummy read to ensure OWL_DMA_IRQ_PD0 value is updated */
  519. dma_readl(od, OWL_DMA_IRQ_PD0);
  520. global_irq_pending = dma_readl(od, OWL_DMA_IRQ_PD0);
  521. if (chan_irq_pending && !(global_irq_pending & BIT(i))) {
  522. dev_dbg(od->dma.dev,
  523. "global and channel IRQ pending match err\n");
  524. /* Clear IRQ status for this pchan */
  525. pchan_update(pchan, OWL_DMAX_INT_STATUS,
  526. 0xff, false);
  527. /* Update global IRQ pending */
  528. pending |= BIT(i);
  529. }
  530. }
  531. spin_unlock(&od->lock);
  532. for_each_set_bit(i, &pending, od->nr_pchans) {
  533. struct owl_dma_txd *txd;
  534. pchan = &od->pchans[i];
  535. vchan = pchan->vchan;
  536. if (!vchan) {
  537. dev_warn(od->dma.dev, "no vchan attached on pchan %d\n",
  538. pchan->id);
  539. continue;
  540. }
  541. spin_lock(&vchan->vc.lock);
  542. txd = vchan->txd;
  543. if (txd) {
  544. vchan->txd = NULL;
  545. vchan_cookie_complete(&txd->vd);
  546. /*
  547. * Start the next descriptor (if any),
  548. * otherwise free this channel.
  549. */
  550. if (vchan_next_desc(&vchan->vc))
  551. owl_dma_start_next_txd(vchan);
  552. else
  553. owl_dma_phy_free(od, vchan);
  554. }
  555. spin_unlock(&vchan->vc.lock);
  556. }
  557. return IRQ_HANDLED;
  558. }
  559. static void owl_dma_free_txd(struct owl_dma *od, struct owl_dma_txd *txd)
  560. {
  561. struct owl_dma_lli *lli, *_lli;
  562. if (unlikely(!txd))
  563. return;
  564. list_for_each_entry_safe(lli, _lli, &txd->lli_list, node)
  565. owl_dma_free_lli(od, lli);
  566. kfree(txd);
  567. }
  568. static void owl_dma_desc_free(struct virt_dma_desc *vd)
  569. {
  570. struct owl_dma *od = to_owl_dma(vd->tx.chan->device);
  571. struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
  572. owl_dma_free_txd(od, txd);
  573. }
  574. static int owl_dma_terminate_all(struct dma_chan *chan)
  575. {
  576. struct owl_dma *od = to_owl_dma(chan->device);
  577. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  578. unsigned long flags;
  579. LIST_HEAD(head);
  580. spin_lock_irqsave(&vchan->vc.lock, flags);
  581. if (vchan->pchan)
  582. owl_dma_phy_free(od, vchan);
  583. if (vchan->txd) {
  584. owl_dma_desc_free(&vchan->txd->vd);
  585. vchan->txd = NULL;
  586. }
  587. vchan_get_all_descriptors(&vchan->vc, &head);
  588. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  589. vchan_dma_desc_free_list(&vchan->vc, &head);
  590. return 0;
  591. }
  592. static int owl_dma_config(struct dma_chan *chan,
  593. struct dma_slave_config *config)
  594. {
  595. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  596. /* Reject definitely invalid configurations */
  597. if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  598. config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  599. return -EINVAL;
  600. memcpy(&vchan->cfg, config, sizeof(struct dma_slave_config));
  601. return 0;
  602. }
  603. static int owl_dma_pause(struct dma_chan *chan)
  604. {
  605. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  606. unsigned long flags;
  607. spin_lock_irqsave(&vchan->vc.lock, flags);
  608. owl_dma_pause_pchan(vchan->pchan);
  609. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  610. return 0;
  611. }
  612. static int owl_dma_resume(struct dma_chan *chan)
  613. {
  614. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  615. unsigned long flags;
  616. if (!vchan->pchan && !vchan->txd)
  617. return 0;
  618. dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
  619. spin_lock_irqsave(&vchan->vc.lock, flags);
  620. owl_dma_resume_pchan(vchan->pchan);
  621. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  622. return 0;
  623. }
  624. static u32 owl_dma_getbytes_chan(struct owl_dma_vchan *vchan)
  625. {
  626. struct owl_dma_pchan *pchan;
  627. struct owl_dma_txd *txd;
  628. struct owl_dma_lli *lli;
  629. unsigned int next_lli_phy;
  630. size_t bytes;
  631. pchan = vchan->pchan;
  632. txd = vchan->txd;
  633. if (!pchan || !txd)
  634. return 0;
  635. /* Get remain count of current node in link list */
  636. bytes = pchan_readl(pchan, OWL_DMAX_REMAIN_CNT);
  637. /* Loop through the preceding nodes to get total remaining bytes */
  638. if (pchan_readl(pchan, OWL_DMAX_MODE) & OWL_DMA_MODE_LME) {
  639. next_lli_phy = pchan_readl(pchan, OWL_DMAX_NEXT_DESCRIPTOR);
  640. list_for_each_entry(lli, &txd->lli_list, node) {
  641. /* Start from the next active node */
  642. if (lli->phys == next_lli_phy) {
  643. list_for_each_entry(lli, &txd->lli_list, node)
  644. bytes += llc_hw_flen(lli);
  645. break;
  646. }
  647. }
  648. }
  649. return bytes;
  650. }
  651. static enum dma_status owl_dma_tx_status(struct dma_chan *chan,
  652. dma_cookie_t cookie,
  653. struct dma_tx_state *state)
  654. {
  655. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  656. struct owl_dma_lli *lli;
  657. struct virt_dma_desc *vd;
  658. struct owl_dma_txd *txd;
  659. enum dma_status ret;
  660. unsigned long flags;
  661. size_t bytes = 0;
  662. ret = dma_cookie_status(chan, cookie, state);
  663. if (ret == DMA_COMPLETE || !state)
  664. return ret;
  665. spin_lock_irqsave(&vchan->vc.lock, flags);
  666. vd = vchan_find_desc(&vchan->vc, cookie);
  667. if (vd) {
  668. txd = to_owl_txd(&vd->tx);
  669. list_for_each_entry(lli, &txd->lli_list, node)
  670. bytes += llc_hw_flen(lli);
  671. } else {
  672. bytes = owl_dma_getbytes_chan(vchan);
  673. }
  674. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  675. dma_set_residue(state, bytes);
  676. return ret;
  677. }
  678. static void owl_dma_phy_alloc_and_start(struct owl_dma_vchan *vchan)
  679. {
  680. struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
  681. struct owl_dma_pchan *pchan;
  682. pchan = owl_dma_get_pchan(od, vchan);
  683. if (!pchan)
  684. return;
  685. dev_dbg(od->dma.dev, "allocated pchan %d\n", pchan->id);
  686. vchan->pchan = pchan;
  687. owl_dma_start_next_txd(vchan);
  688. }
  689. static void owl_dma_issue_pending(struct dma_chan *chan)
  690. {
  691. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  692. unsigned long flags;
  693. spin_lock_irqsave(&vchan->vc.lock, flags);
  694. if (vchan_issue_pending(&vchan->vc)) {
  695. if (!vchan->pchan)
  696. owl_dma_phy_alloc_and_start(vchan);
  697. }
  698. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  699. }
  700. static struct dma_async_tx_descriptor
  701. *owl_dma_prep_memcpy(struct dma_chan *chan,
  702. dma_addr_t dst, dma_addr_t src,
  703. size_t len, unsigned long flags)
  704. {
  705. struct owl_dma *od = to_owl_dma(chan->device);
  706. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  707. struct owl_dma_txd *txd;
  708. struct owl_dma_lli *lli, *prev = NULL;
  709. size_t offset, bytes;
  710. int ret;
  711. if (!len)
  712. return NULL;
  713. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  714. if (!txd)
  715. return NULL;
  716. INIT_LIST_HEAD(&txd->lli_list);
  717. /* Process the transfer as frame by frame */
  718. for (offset = 0; offset < len; offset += bytes) {
  719. lli = owl_dma_alloc_lli(od);
  720. if (!lli) {
  721. dev_warn(chan2dev(chan), "failed to allocate lli\n");
  722. goto err_txd_free;
  723. }
  724. bytes = min_t(size_t, (len - offset), OWL_DMA_FRAME_MAX_LENGTH);
  725. ret = owl_dma_cfg_lli(vchan, lli, src + offset, dst + offset,
  726. bytes, DMA_MEM_TO_MEM,
  727. &vchan->cfg, txd->cyclic);
  728. if (ret) {
  729. dev_warn(chan2dev(chan), "failed to config lli\n");
  730. goto err_txd_free;
  731. }
  732. prev = owl_dma_add_lli(txd, prev, lli, false);
  733. }
  734. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  735. err_txd_free:
  736. owl_dma_free_txd(od, txd);
  737. return NULL;
  738. }
  739. static struct dma_async_tx_descriptor
  740. *owl_dma_prep_slave_sg(struct dma_chan *chan,
  741. struct scatterlist *sgl,
  742. unsigned int sg_len,
  743. enum dma_transfer_direction dir,
  744. unsigned long flags, void *context)
  745. {
  746. struct owl_dma *od = to_owl_dma(chan->device);
  747. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  748. struct dma_slave_config *sconfig = &vchan->cfg;
  749. struct owl_dma_txd *txd;
  750. struct owl_dma_lli *lli, *prev = NULL;
  751. struct scatterlist *sg;
  752. dma_addr_t addr, src = 0, dst = 0;
  753. size_t len;
  754. int ret, i;
  755. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  756. if (!txd)
  757. return NULL;
  758. INIT_LIST_HEAD(&txd->lli_list);
  759. for_each_sg(sgl, sg, sg_len, i) {
  760. addr = sg_dma_address(sg);
  761. len = sg_dma_len(sg);
  762. if (len > OWL_DMA_FRAME_MAX_LENGTH) {
  763. dev_err(od->dma.dev,
  764. "frame length exceeds max supported length");
  765. goto err_txd_free;
  766. }
  767. lli = owl_dma_alloc_lli(od);
  768. if (!lli) {
  769. dev_err(chan2dev(chan), "failed to allocate lli");
  770. goto err_txd_free;
  771. }
  772. if (dir == DMA_MEM_TO_DEV) {
  773. src = addr;
  774. dst = sconfig->dst_addr;
  775. } else {
  776. src = sconfig->src_addr;
  777. dst = addr;
  778. }
  779. ret = owl_dma_cfg_lli(vchan, lli, src, dst, len, dir, sconfig,
  780. txd->cyclic);
  781. if (ret) {
  782. dev_warn(chan2dev(chan), "failed to config lli");
  783. goto err_txd_free;
  784. }
  785. prev = owl_dma_add_lli(txd, prev, lli, false);
  786. }
  787. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  788. err_txd_free:
  789. owl_dma_free_txd(od, txd);
  790. return NULL;
  791. }
  792. static struct dma_async_tx_descriptor
  793. *owl_prep_dma_cyclic(struct dma_chan *chan,
  794. dma_addr_t buf_addr, size_t buf_len,
  795. size_t period_len,
  796. enum dma_transfer_direction dir,
  797. unsigned long flags)
  798. {
  799. struct owl_dma *od = to_owl_dma(chan->device);
  800. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  801. struct dma_slave_config *sconfig = &vchan->cfg;
  802. struct owl_dma_txd *txd;
  803. struct owl_dma_lli *lli, *prev = NULL, *first = NULL;
  804. dma_addr_t src = 0, dst = 0;
  805. unsigned int periods = buf_len / period_len;
  806. int ret, i;
  807. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  808. if (!txd)
  809. return NULL;
  810. INIT_LIST_HEAD(&txd->lli_list);
  811. txd->cyclic = true;
  812. for (i = 0; i < periods; i++) {
  813. lli = owl_dma_alloc_lli(od);
  814. if (!lli) {
  815. dev_warn(chan2dev(chan), "failed to allocate lli");
  816. goto err_txd_free;
  817. }
  818. if (dir == DMA_MEM_TO_DEV) {
  819. src = buf_addr + (period_len * i);
  820. dst = sconfig->dst_addr;
  821. } else if (dir == DMA_DEV_TO_MEM) {
  822. src = sconfig->src_addr;
  823. dst = buf_addr + (period_len * i);
  824. }
  825. ret = owl_dma_cfg_lli(vchan, lli, src, dst, period_len,
  826. dir, sconfig, txd->cyclic);
  827. if (ret) {
  828. dev_warn(chan2dev(chan), "failed to config lli");
  829. goto err_txd_free;
  830. }
  831. if (!first)
  832. first = lli;
  833. prev = owl_dma_add_lli(txd, prev, lli, false);
  834. }
  835. /* close the cyclic list */
  836. owl_dma_add_lli(txd, prev, first, true);
  837. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  838. err_txd_free:
  839. owl_dma_free_txd(od, txd);
  840. return NULL;
  841. }
  842. static void owl_dma_free_chan_resources(struct dma_chan *chan)
  843. {
  844. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  845. /* Ensure all queued descriptors are freed */
  846. vchan_free_chan_resources(&vchan->vc);
  847. }
  848. static inline void owl_dma_free(struct owl_dma *od)
  849. {
  850. struct owl_dma_vchan *vchan = NULL;
  851. struct owl_dma_vchan *next;
  852. list_for_each_entry_safe(vchan,
  853. next, &od->dma.channels, vc.chan.device_node) {
  854. list_del(&vchan->vc.chan.device_node);
  855. tasklet_kill(&vchan->vc.task);
  856. }
  857. }
  858. static struct dma_chan *owl_dma_of_xlate(struct of_phandle_args *dma_spec,
  859. struct of_dma *ofdma)
  860. {
  861. struct owl_dma *od = ofdma->of_dma_data;
  862. struct owl_dma_vchan *vchan;
  863. struct dma_chan *chan;
  864. u8 drq = dma_spec->args[0];
  865. if (drq > od->nr_vchans)
  866. return NULL;
  867. chan = dma_get_any_slave_channel(&od->dma);
  868. if (!chan)
  869. return NULL;
  870. vchan = to_owl_vchan(chan);
  871. vchan->drq = drq;
  872. return chan;
  873. }
  874. static const struct of_device_id owl_dma_match[] = {
  875. { .compatible = "actions,s500-dma", .data = (void *)S900_DMA,},
  876. { .compatible = "actions,s700-dma", .data = (void *)S700_DMA,},
  877. { .compatible = "actions,s900-dma", .data = (void *)S900_DMA,},
  878. { /* sentinel */ },
  879. };
  880. MODULE_DEVICE_TABLE(of, owl_dma_match);
  881. static int owl_dma_probe(struct platform_device *pdev)
  882. {
  883. struct device_node *np = pdev->dev.of_node;
  884. struct owl_dma *od;
  885. int ret, i, nr_channels, nr_requests;
  886. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  887. if (!od)
  888. return -ENOMEM;
  889. od->base = devm_platform_ioremap_resource(pdev, 0);
  890. if (IS_ERR(od->base))
  891. return PTR_ERR(od->base);
  892. ret = of_property_read_u32(np, "dma-channels", &nr_channels);
  893. if (ret) {
  894. dev_err(&pdev->dev, "can't get dma-channels\n");
  895. return ret;
  896. }
  897. ret = of_property_read_u32(np, "dma-requests", &nr_requests);
  898. if (ret) {
  899. dev_err(&pdev->dev, "can't get dma-requests\n");
  900. return ret;
  901. }
  902. dev_info(&pdev->dev, "dma-channels %d, dma-requests %d\n",
  903. nr_channels, nr_requests);
  904. od->devid = (uintptr_t)of_device_get_match_data(&pdev->dev);
  905. od->nr_pchans = nr_channels;
  906. od->nr_vchans = nr_requests;
  907. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  908. platform_set_drvdata(pdev, od);
  909. spin_lock_init(&od->lock);
  910. dma_cap_set(DMA_MEMCPY, od->dma.cap_mask);
  911. dma_cap_set(DMA_SLAVE, od->dma.cap_mask);
  912. dma_cap_set(DMA_CYCLIC, od->dma.cap_mask);
  913. od->dma.dev = &pdev->dev;
  914. od->dma.device_free_chan_resources = owl_dma_free_chan_resources;
  915. od->dma.device_tx_status = owl_dma_tx_status;
  916. od->dma.device_issue_pending = owl_dma_issue_pending;
  917. od->dma.device_prep_dma_memcpy = owl_dma_prep_memcpy;
  918. od->dma.device_prep_slave_sg = owl_dma_prep_slave_sg;
  919. od->dma.device_prep_dma_cyclic = owl_prep_dma_cyclic;
  920. od->dma.device_config = owl_dma_config;
  921. od->dma.device_pause = owl_dma_pause;
  922. od->dma.device_resume = owl_dma_resume;
  923. od->dma.device_terminate_all = owl_dma_terminate_all;
  924. od->dma.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  925. od->dma.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  926. od->dma.directions = BIT(DMA_MEM_TO_MEM);
  927. od->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  928. INIT_LIST_HEAD(&od->dma.channels);
  929. od->clk = devm_clk_get(&pdev->dev, NULL);
  930. if (IS_ERR(od->clk)) {
  931. dev_err(&pdev->dev, "unable to get clock\n");
  932. return PTR_ERR(od->clk);
  933. }
  934. /*
  935. * Even though the DMA controller is capable of generating 4
  936. * IRQ's for DMA priority feature, we only use 1 IRQ for
  937. * simplification.
  938. */
  939. od->irq = platform_get_irq(pdev, 0);
  940. ret = devm_request_irq(&pdev->dev, od->irq, owl_dma_interrupt, 0,
  941. dev_name(&pdev->dev), od);
  942. if (ret) {
  943. dev_err(&pdev->dev, "unable to request IRQ\n");
  944. return ret;
  945. }
  946. /* Init physical channel */
  947. od->pchans = devm_kcalloc(&pdev->dev, od->nr_pchans,
  948. sizeof(struct owl_dma_pchan), GFP_KERNEL);
  949. if (!od->pchans)
  950. return -ENOMEM;
  951. for (i = 0; i < od->nr_pchans; i++) {
  952. struct owl_dma_pchan *pchan = &od->pchans[i];
  953. pchan->id = i;
  954. pchan->base = od->base + OWL_DMA_CHAN_BASE(i);
  955. }
  956. /* Init virtual channel */
  957. od->vchans = devm_kcalloc(&pdev->dev, od->nr_vchans,
  958. sizeof(struct owl_dma_vchan), GFP_KERNEL);
  959. if (!od->vchans)
  960. return -ENOMEM;
  961. for (i = 0; i < od->nr_vchans; i++) {
  962. struct owl_dma_vchan *vchan = &od->vchans[i];
  963. vchan->vc.desc_free = owl_dma_desc_free;
  964. vchan_init(&vchan->vc, &od->dma);
  965. }
  966. /* Create a pool of consistent memory blocks for hardware descriptors */
  967. od->lli_pool = dma_pool_create(dev_name(od->dma.dev), od->dma.dev,
  968. sizeof(struct owl_dma_lli),
  969. __alignof__(struct owl_dma_lli),
  970. 0);
  971. if (!od->lli_pool) {
  972. dev_err(&pdev->dev, "unable to allocate DMA descriptor pool\n");
  973. return -ENOMEM;
  974. }
  975. clk_prepare_enable(od->clk);
  976. ret = dma_async_device_register(&od->dma);
  977. if (ret) {
  978. dev_err(&pdev->dev, "failed to register DMA engine device\n");
  979. goto err_pool_free;
  980. }
  981. /* Device-tree DMA controller registration */
  982. ret = of_dma_controller_register(pdev->dev.of_node,
  983. owl_dma_of_xlate, od);
  984. if (ret) {
  985. dev_err(&pdev->dev, "of_dma_controller_register failed\n");
  986. goto err_dma_unregister;
  987. }
  988. return 0;
  989. err_dma_unregister:
  990. dma_async_device_unregister(&od->dma);
  991. err_pool_free:
  992. clk_disable_unprepare(od->clk);
  993. dma_pool_destroy(od->lli_pool);
  994. return ret;
  995. }
  996. static void owl_dma_remove(struct platform_device *pdev)
  997. {
  998. struct owl_dma *od = platform_get_drvdata(pdev);
  999. of_dma_controller_free(pdev->dev.of_node);
  1000. dma_async_device_unregister(&od->dma);
  1001. /* Mask all interrupts for this execution environment */
  1002. dma_writel(od, OWL_DMA_IRQ_EN0, 0x0);
  1003. /* Make sure we won't have any further interrupts */
  1004. devm_free_irq(od->dma.dev, od->irq, od);
  1005. owl_dma_free(od);
  1006. clk_disable_unprepare(od->clk);
  1007. dma_pool_destroy(od->lli_pool);
  1008. }
  1009. static struct platform_driver owl_dma_driver = {
  1010. .probe = owl_dma_probe,
  1011. .remove_new = owl_dma_remove,
  1012. .driver = {
  1013. .name = "dma-owl",
  1014. .of_match_table = of_match_ptr(owl_dma_match),
  1015. },
  1016. };
  1017. static int owl_dma_init(void)
  1018. {
  1019. return platform_driver_register(&owl_dma_driver);
  1020. }
  1021. subsys_initcall(owl_dma_init);
  1022. static void __exit owl_dma_exit(void)
  1023. {
  1024. platform_driver_unregister(&owl_dma_driver);
  1025. }
  1026. module_exit(owl_dma_exit);
  1027. MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
  1028. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  1029. MODULE_DESCRIPTION("Actions Semi Owl SoCs DMA driver");
  1030. MODULE_LICENSE("GPL");