owl-dma.c 32 KB

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