ls2x-apb-dma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the Loongson LS2X APB DMA Controller
  4. *
  5. * Copyright (C) 2017-2023 Loongson Corporation
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/dmapool.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/io-64-nonatomic-lo-hi.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_dma.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include "dmaengine.h"
  19. #include "virt-dma.h"
  20. /* Global Configuration Register */
  21. #define LDMA_ORDER_ERG 0x0
  22. /* Bitfield definitions */
  23. /* Bitfields in Global Configuration Register */
  24. #define LDMA_64BIT_EN BIT(0) /* 1: 64 bit support */
  25. #define LDMA_UNCOHERENT_EN BIT(1) /* 0: cache, 1: uncache */
  26. #define LDMA_ASK_VALID BIT(2)
  27. #define LDMA_START BIT(3) /* DMA start operation */
  28. #define LDMA_STOP BIT(4) /* DMA stop operation */
  29. #define LDMA_CONFIG_MASK GENMASK_ULL(4, 0) /* DMA controller config bits mask */
  30. /* Bitfields in ndesc_addr field of HW descriptor */
  31. #define LDMA_DESC_EN BIT(0) /*1: The next descriptor is valid */
  32. #define LDMA_DESC_ADDR_LOW GENMASK(31, 1)
  33. /* Bitfields in cmd field of HW descriptor */
  34. #define LDMA_INT BIT(1) /* Enable DMA interrupts */
  35. #define LDMA_DATA_DIRECTION BIT(12) /* 1: write to device, 0: read from device */
  36. #define LDMA_SLAVE_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  37. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  38. #define LDMA_MAX_TRANS_LEN U32_MAX
  39. /*-- descriptors -----------------------------------------------------*/
  40. /*
  41. * struct ls2x_dma_hw_desc - DMA HW descriptor
  42. * @ndesc_addr: the next descriptor low address.
  43. * @mem_addr: memory low address.
  44. * @apb_addr: device buffer address.
  45. * @len: length of a piece of carried content, in words.
  46. * @step_len: length between two moved memory data blocks.
  47. * @step_times: number of blocks to be carried in a single DMA operation.
  48. * @cmd: descriptor command or state.
  49. * @stats: DMA status.
  50. * @high_ndesc_addr: the next descriptor high address.
  51. * @high_mem_addr: memory high address.
  52. * @reserved: reserved
  53. */
  54. struct ls2x_dma_hw_desc {
  55. u32 ndesc_addr;
  56. u32 mem_addr;
  57. u32 apb_addr;
  58. u32 len;
  59. u32 step_len;
  60. u32 step_times;
  61. u32 cmd;
  62. u32 stats;
  63. u32 high_ndesc_addr;
  64. u32 high_mem_addr;
  65. u32 reserved[2];
  66. } __packed;
  67. /*
  68. * struct ls2x_dma_sg - ls2x dma scatter gather entry
  69. * @hw: the pointer to DMA HW descriptor.
  70. * @llp: physical address of the DMA HW descriptor.
  71. * @phys: destination or source address(mem).
  72. * @len: number of Bytes to read.
  73. */
  74. struct ls2x_dma_sg {
  75. struct ls2x_dma_hw_desc *hw;
  76. dma_addr_t llp;
  77. dma_addr_t phys;
  78. u32 len;
  79. };
  80. /*
  81. * struct ls2x_dma_desc - software descriptor
  82. * @vdesc: pointer to the virtual dma descriptor.
  83. * @cyclic: flag to dma cyclic
  84. * @burst_size: burst size of transaction, in words.
  85. * @desc_num: number of sg entries.
  86. * @direction: transfer direction, to or from device.
  87. * @status: dma controller status.
  88. * @sg: array of sgs.
  89. */
  90. struct ls2x_dma_desc {
  91. struct virt_dma_desc vdesc;
  92. bool cyclic;
  93. size_t burst_size;
  94. u32 desc_num;
  95. enum dma_transfer_direction direction;
  96. enum dma_status status;
  97. struct ls2x_dma_sg sg[] __counted_by(desc_num);
  98. };
  99. /*-- Channels --------------------------------------------------------*/
  100. /*
  101. * struct ls2x_dma_chan - internal representation of an LS2X APB DMA channel
  102. * @vchan: virtual dma channel entry.
  103. * @desc: pointer to the ls2x sw dma descriptor.
  104. * @pool: hw desc table
  105. * @irq: irq line
  106. * @sconfig: configuration for slave transfers, passed via .device_config
  107. */
  108. struct ls2x_dma_chan {
  109. struct virt_dma_chan vchan;
  110. struct ls2x_dma_desc *desc;
  111. void *pool;
  112. int irq;
  113. struct dma_slave_config sconfig;
  114. };
  115. /*-- Controller ------------------------------------------------------*/
  116. /*
  117. * struct ls2x_dma_priv - LS2X APB DMAC specific information
  118. * @ddev: dmaengine dma_device object members
  119. * @dma_clk: DMAC clock source
  120. * @regs: memory mapped register base
  121. * @lchan: channel to store ls2x_dma_chan structures
  122. */
  123. struct ls2x_dma_priv {
  124. struct dma_device ddev;
  125. struct clk *dma_clk;
  126. void __iomem *regs;
  127. struct ls2x_dma_chan lchan;
  128. };
  129. /*-- Helper functions ------------------------------------------------*/
  130. static inline struct ls2x_dma_desc *to_ldma_desc(struct virt_dma_desc *vdesc)
  131. {
  132. return container_of(vdesc, struct ls2x_dma_desc, vdesc);
  133. }
  134. static inline struct ls2x_dma_chan *to_ldma_chan(struct dma_chan *chan)
  135. {
  136. return container_of(chan, struct ls2x_dma_chan, vchan.chan);
  137. }
  138. static inline struct ls2x_dma_priv *to_ldma_priv(struct dma_device *ddev)
  139. {
  140. return container_of(ddev, struct ls2x_dma_priv, ddev);
  141. }
  142. static struct device *chan2dev(struct dma_chan *chan)
  143. {
  144. return &chan->dev->device;
  145. }
  146. static void ls2x_dma_desc_free(struct virt_dma_desc *vdesc)
  147. {
  148. struct ls2x_dma_chan *lchan = to_ldma_chan(vdesc->tx.chan);
  149. struct ls2x_dma_desc *desc = to_ldma_desc(vdesc);
  150. int i;
  151. for (i = 0; i < desc->desc_num; i++) {
  152. if (desc->sg[i].hw)
  153. dma_pool_free(lchan->pool, desc->sg[i].hw,
  154. desc->sg[i].llp);
  155. }
  156. kfree(desc);
  157. }
  158. static void ls2x_dma_write_cmd(struct ls2x_dma_chan *lchan, bool cmd)
  159. {
  160. struct ls2x_dma_priv *priv = to_ldma_priv(lchan->vchan.chan.device);
  161. u64 val;
  162. val = lo_hi_readq(priv->regs + LDMA_ORDER_ERG) & ~LDMA_CONFIG_MASK;
  163. val |= LDMA_64BIT_EN | cmd;
  164. lo_hi_writeq(val, priv->regs + LDMA_ORDER_ERG);
  165. }
  166. static void ls2x_dma_start_transfer(struct ls2x_dma_chan *lchan)
  167. {
  168. struct ls2x_dma_priv *priv = to_ldma_priv(lchan->vchan.chan.device);
  169. struct ls2x_dma_sg *ldma_sg;
  170. struct virt_dma_desc *vdesc;
  171. u64 val;
  172. /* Get the next descriptor */
  173. vdesc = vchan_next_desc(&lchan->vchan);
  174. if (!vdesc) {
  175. lchan->desc = NULL;
  176. return;
  177. }
  178. list_del(&vdesc->node);
  179. lchan->desc = to_ldma_desc(vdesc);
  180. ldma_sg = &lchan->desc->sg[0];
  181. /* Start DMA */
  182. lo_hi_writeq(0, priv->regs + LDMA_ORDER_ERG);
  183. val = (ldma_sg->llp & ~LDMA_CONFIG_MASK) | LDMA_64BIT_EN | LDMA_START;
  184. lo_hi_writeq(val, priv->regs + LDMA_ORDER_ERG);
  185. }
  186. static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)
  187. {
  188. u32 maxburst, buswidth;
  189. /* Reject definitely invalid configurations */
  190. if ((lchan->sconfig.src_addr_width & LDMA_SLAVE_BUSWIDTHS) &&
  191. (lchan->sconfig.dst_addr_width & LDMA_SLAVE_BUSWIDTHS))
  192. return 0;
  193. if (lchan->sconfig.direction == DMA_MEM_TO_DEV) {
  194. maxburst = lchan->sconfig.dst_maxburst;
  195. buswidth = lchan->sconfig.dst_addr_width;
  196. } else {
  197. maxburst = lchan->sconfig.src_maxburst;
  198. buswidth = lchan->sconfig.src_addr_width;
  199. }
  200. /* If maxburst is zero, fallback to LDMA_MAX_TRANS_LEN */
  201. return maxburst ? (maxburst * buswidth) >> 2 : LDMA_MAX_TRANS_LEN;
  202. }
  203. static void ls2x_dma_fill_desc(struct ls2x_dma_chan *lchan, u32 sg_index,
  204. struct ls2x_dma_desc *desc)
  205. {
  206. struct ls2x_dma_sg *ldma_sg = &desc->sg[sg_index];
  207. u32 num_segments, segment_size;
  208. if (desc->direction == DMA_MEM_TO_DEV) {
  209. ldma_sg->hw->cmd = LDMA_INT | LDMA_DATA_DIRECTION;
  210. ldma_sg->hw->apb_addr = lchan->sconfig.dst_addr;
  211. } else {
  212. ldma_sg->hw->cmd = LDMA_INT;
  213. ldma_sg->hw->apb_addr = lchan->sconfig.src_addr;
  214. }
  215. ldma_sg->hw->mem_addr = lower_32_bits(ldma_sg->phys);
  216. ldma_sg->hw->high_mem_addr = upper_32_bits(ldma_sg->phys);
  217. /* Split into multiple equally sized segments if necessary */
  218. num_segments = DIV_ROUND_UP((ldma_sg->len + 3) >> 2, desc->burst_size);
  219. segment_size = DIV_ROUND_UP((ldma_sg->len + 3) >> 2, num_segments);
  220. /* Word count register takes input in words */
  221. ldma_sg->hw->len = segment_size;
  222. ldma_sg->hw->step_times = num_segments;
  223. ldma_sg->hw->step_len = 0;
  224. /* lets make a link list */
  225. if (sg_index) {
  226. desc->sg[sg_index - 1].hw->ndesc_addr = ldma_sg->llp | LDMA_DESC_EN;
  227. desc->sg[sg_index - 1].hw->high_ndesc_addr = upper_32_bits(ldma_sg->llp);
  228. }
  229. }
  230. /*-- DMA Engine API --------------------------------------------------*/
  231. /*
  232. * ls2x_dma_alloc_chan_resources - allocate resources for DMA channel
  233. * @chan: allocate descriptor resources for this channel
  234. *
  235. * return - the number of allocated descriptors
  236. */
  237. static int ls2x_dma_alloc_chan_resources(struct dma_chan *chan)
  238. {
  239. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  240. /* Create a pool of consistent memory blocks for hardware descriptors */
  241. lchan->pool = dma_pool_create(dev_name(chan2dev(chan)),
  242. chan->device->dev, PAGE_SIZE,
  243. __alignof__(struct ls2x_dma_hw_desc), 0);
  244. if (!lchan->pool) {
  245. dev_err(chan2dev(chan), "No memory for descriptors\n");
  246. return -ENOMEM;
  247. }
  248. return 1;
  249. }
  250. /*
  251. * ls2x_dma_free_chan_resources - free all channel resources
  252. * @chan: DMA channel
  253. */
  254. static void ls2x_dma_free_chan_resources(struct dma_chan *chan)
  255. {
  256. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  257. vchan_free_chan_resources(to_virt_chan(chan));
  258. dma_pool_destroy(lchan->pool);
  259. lchan->pool = NULL;
  260. }
  261. /*
  262. * ls2x_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  263. * @chan: DMA channel
  264. * @sgl: scatterlist to transfer to/from
  265. * @sg_len: number of entries in @scatterlist
  266. * @direction: DMA direction
  267. * @flags: tx descriptor status flags
  268. * @context: transaction context (ignored)
  269. *
  270. * Return: Async transaction descriptor on success and NULL on failure
  271. */
  272. static struct dma_async_tx_descriptor *
  273. ls2x_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  274. u32 sg_len, enum dma_transfer_direction direction,
  275. unsigned long flags, void *context)
  276. {
  277. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  278. struct ls2x_dma_desc *desc;
  279. struct scatterlist *sg;
  280. size_t burst_size;
  281. int i;
  282. if (unlikely(!sg_len || !is_slave_direction(direction)))
  283. return NULL;
  284. burst_size = ls2x_dmac_detect_burst(lchan);
  285. if (!burst_size)
  286. return NULL;
  287. desc = kzalloc(struct_size(desc, sg, sg_len), GFP_NOWAIT);
  288. if (!desc)
  289. return NULL;
  290. desc->desc_num = sg_len;
  291. desc->direction = direction;
  292. desc->burst_size = burst_size;
  293. for_each_sg(sgl, sg, sg_len, i) {
  294. struct ls2x_dma_sg *ldma_sg = &desc->sg[i];
  295. /* Allocate DMA capable memory for hardware descriptor */
  296. ldma_sg->hw = dma_pool_alloc(lchan->pool, GFP_NOWAIT, &ldma_sg->llp);
  297. if (!ldma_sg->hw) {
  298. desc->desc_num = i;
  299. ls2x_dma_desc_free(&desc->vdesc);
  300. return NULL;
  301. }
  302. ldma_sg->phys = sg_dma_address(sg);
  303. ldma_sg->len = sg_dma_len(sg);
  304. ls2x_dma_fill_desc(lchan, i, desc);
  305. }
  306. /* Setting the last descriptor enable bit */
  307. desc->sg[sg_len - 1].hw->ndesc_addr &= ~LDMA_DESC_EN;
  308. desc->status = DMA_IN_PROGRESS;
  309. return vchan_tx_prep(&lchan->vchan, &desc->vdesc, flags);
  310. }
  311. /*
  312. * ls2x_dma_prep_dma_cyclic - prepare the cyclic DMA transfer
  313. * @chan: the DMA channel to prepare
  314. * @buf_addr: physical DMA address where the buffer starts
  315. * @buf_len: total number of bytes for the entire buffer
  316. * @period_len: number of bytes for each period
  317. * @direction: transfer direction, to or from device
  318. * @flags: tx descriptor status flags
  319. *
  320. * Return: Async transaction descriptor on success and NULL on failure
  321. */
  322. static struct dma_async_tx_descriptor *
  323. ls2x_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  324. size_t period_len, enum dma_transfer_direction direction,
  325. unsigned long flags)
  326. {
  327. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  328. struct ls2x_dma_desc *desc;
  329. size_t burst_size;
  330. u32 num_periods;
  331. int i;
  332. if (unlikely(!buf_len || !period_len))
  333. return NULL;
  334. if (unlikely(!is_slave_direction(direction)))
  335. return NULL;
  336. burst_size = ls2x_dmac_detect_burst(lchan);
  337. if (!burst_size)
  338. return NULL;
  339. num_periods = buf_len / period_len;
  340. desc = kzalloc(struct_size(desc, sg, num_periods), GFP_NOWAIT);
  341. if (!desc)
  342. return NULL;
  343. desc->desc_num = num_periods;
  344. desc->direction = direction;
  345. desc->burst_size = burst_size;
  346. /* Build cyclic linked list */
  347. for (i = 0; i < num_periods; i++) {
  348. struct ls2x_dma_sg *ldma_sg = &desc->sg[i];
  349. /* Allocate DMA capable memory for hardware descriptor */
  350. ldma_sg->hw = dma_pool_alloc(lchan->pool, GFP_NOWAIT, &ldma_sg->llp);
  351. if (!ldma_sg->hw) {
  352. desc->desc_num = i;
  353. ls2x_dma_desc_free(&desc->vdesc);
  354. return NULL;
  355. }
  356. ldma_sg->phys = buf_addr + period_len * i;
  357. ldma_sg->len = period_len;
  358. ls2x_dma_fill_desc(lchan, i, desc);
  359. }
  360. /* Lets make a cyclic list */
  361. desc->sg[num_periods - 1].hw->ndesc_addr = desc->sg[0].llp | LDMA_DESC_EN;
  362. desc->sg[num_periods - 1].hw->high_ndesc_addr = upper_32_bits(desc->sg[0].llp);
  363. desc->cyclic = true;
  364. desc->status = DMA_IN_PROGRESS;
  365. return vchan_tx_prep(&lchan->vchan, &desc->vdesc, flags);
  366. }
  367. /*
  368. * ls2x_slave_config - set slave configuration for channel
  369. * @chan: dma channel
  370. * @cfg: slave configuration
  371. *
  372. * Sets slave configuration for channel
  373. */
  374. static int ls2x_dma_slave_config(struct dma_chan *chan,
  375. struct dma_slave_config *config)
  376. {
  377. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  378. memcpy(&lchan->sconfig, config, sizeof(*config));
  379. return 0;
  380. }
  381. /*
  382. * ls2x_dma_issue_pending - push pending transactions to the hardware
  383. * @chan: channel
  384. *
  385. * When this function is called, all pending transactions are pushed to the
  386. * hardware and executed.
  387. */
  388. static void ls2x_dma_issue_pending(struct dma_chan *chan)
  389. {
  390. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  391. unsigned long flags;
  392. spin_lock_irqsave(&lchan->vchan.lock, flags);
  393. if (vchan_issue_pending(&lchan->vchan) && !lchan->desc)
  394. ls2x_dma_start_transfer(lchan);
  395. spin_unlock_irqrestore(&lchan->vchan.lock, flags);
  396. }
  397. /*
  398. * ls2x_dma_terminate_all - terminate all transactions
  399. * @chan: channel
  400. *
  401. * Stops all DMA transactions.
  402. */
  403. static int ls2x_dma_terminate_all(struct dma_chan *chan)
  404. {
  405. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  406. unsigned long flags;
  407. LIST_HEAD(head);
  408. spin_lock_irqsave(&lchan->vchan.lock, flags);
  409. /* Setting stop cmd */
  410. ls2x_dma_write_cmd(lchan, LDMA_STOP);
  411. if (lchan->desc) {
  412. vchan_terminate_vdesc(&lchan->desc->vdesc);
  413. lchan->desc = NULL;
  414. }
  415. vchan_get_all_descriptors(&lchan->vchan, &head);
  416. spin_unlock_irqrestore(&lchan->vchan.lock, flags);
  417. vchan_dma_desc_free_list(&lchan->vchan, &head);
  418. return 0;
  419. }
  420. /*
  421. * ls2x_dma_synchronize - Synchronizes the termination of transfers to the
  422. * current context.
  423. * @chan: channel
  424. */
  425. static void ls2x_dma_synchronize(struct dma_chan *chan)
  426. {
  427. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  428. vchan_synchronize(&lchan->vchan);
  429. }
  430. static int ls2x_dma_pause(struct dma_chan *chan)
  431. {
  432. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  433. unsigned long flags;
  434. spin_lock_irqsave(&lchan->vchan.lock, flags);
  435. if (lchan->desc && lchan->desc->status == DMA_IN_PROGRESS) {
  436. ls2x_dma_write_cmd(lchan, LDMA_STOP);
  437. lchan->desc->status = DMA_PAUSED;
  438. }
  439. spin_unlock_irqrestore(&lchan->vchan.lock, flags);
  440. return 0;
  441. }
  442. static int ls2x_dma_resume(struct dma_chan *chan)
  443. {
  444. struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
  445. unsigned long flags;
  446. spin_lock_irqsave(&lchan->vchan.lock, flags);
  447. if (lchan->desc && lchan->desc->status == DMA_PAUSED) {
  448. lchan->desc->status = DMA_IN_PROGRESS;
  449. ls2x_dma_write_cmd(lchan, LDMA_START);
  450. }
  451. spin_unlock_irqrestore(&lchan->vchan.lock, flags);
  452. return 0;
  453. }
  454. /*
  455. * ls2x_dma_isr - LS2X DMA Interrupt handler
  456. * @irq: IRQ number
  457. * @dev_id: Pointer to ls2x_dma_chan
  458. *
  459. * Return: IRQ_HANDLED/IRQ_NONE
  460. */
  461. static irqreturn_t ls2x_dma_isr(int irq, void *dev_id)
  462. {
  463. struct ls2x_dma_chan *lchan = dev_id;
  464. struct ls2x_dma_desc *desc;
  465. spin_lock(&lchan->vchan.lock);
  466. desc = lchan->desc;
  467. if (desc) {
  468. if (desc->cyclic) {
  469. vchan_cyclic_callback(&desc->vdesc);
  470. } else {
  471. desc->status = DMA_COMPLETE;
  472. vchan_cookie_complete(&desc->vdesc);
  473. ls2x_dma_start_transfer(lchan);
  474. }
  475. /* ls2x_dma_start_transfer() updates lchan->desc */
  476. if (!lchan->desc)
  477. ls2x_dma_write_cmd(lchan, LDMA_STOP);
  478. }
  479. spin_unlock(&lchan->vchan.lock);
  480. return IRQ_HANDLED;
  481. }
  482. static int ls2x_dma_chan_init(struct platform_device *pdev,
  483. struct ls2x_dma_priv *priv)
  484. {
  485. struct ls2x_dma_chan *lchan = &priv->lchan;
  486. struct device *dev = &pdev->dev;
  487. int ret;
  488. lchan->irq = platform_get_irq(pdev, 0);
  489. if (lchan->irq < 0)
  490. return lchan->irq;
  491. ret = devm_request_irq(dev, lchan->irq, ls2x_dma_isr, IRQF_TRIGGER_RISING,
  492. dev_name(&pdev->dev), lchan);
  493. if (ret)
  494. return ret;
  495. /* Initialize channels related values */
  496. INIT_LIST_HEAD(&priv->ddev.channels);
  497. lchan->vchan.desc_free = ls2x_dma_desc_free;
  498. vchan_init(&lchan->vchan, &priv->ddev);
  499. return 0;
  500. }
  501. /*
  502. * ls2x_dma_probe - Driver probe function
  503. * @pdev: Pointer to the platform_device structure
  504. *
  505. * Return: '0' on success and failure value on error
  506. */
  507. static int ls2x_dma_probe(struct platform_device *pdev)
  508. {
  509. struct device *dev = &pdev->dev;
  510. struct ls2x_dma_priv *priv;
  511. struct dma_device *ddev;
  512. int ret;
  513. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  514. if (!priv)
  515. return -ENOMEM;
  516. priv->regs = devm_platform_ioremap_resource(pdev, 0);
  517. if (IS_ERR(priv->regs))
  518. return dev_err_probe(dev, PTR_ERR(priv->regs),
  519. "devm_platform_ioremap_resource failed.\n");
  520. priv->dma_clk = devm_clk_get(&pdev->dev, NULL);
  521. if (IS_ERR(priv->dma_clk))
  522. return dev_err_probe(dev, PTR_ERR(priv->dma_clk), "devm_clk_get failed.\n");
  523. ret = clk_prepare_enable(priv->dma_clk);
  524. if (ret)
  525. return dev_err_probe(dev, ret, "clk_prepare_enable failed.\n");
  526. ret = ls2x_dma_chan_init(pdev, priv);
  527. if (ret)
  528. goto disable_clk;
  529. ddev = &priv->ddev;
  530. ddev->dev = dev;
  531. dma_cap_zero(ddev->cap_mask);
  532. dma_cap_set(DMA_SLAVE, ddev->cap_mask);
  533. dma_cap_set(DMA_CYCLIC, ddev->cap_mask);
  534. ddev->device_alloc_chan_resources = ls2x_dma_alloc_chan_resources;
  535. ddev->device_free_chan_resources = ls2x_dma_free_chan_resources;
  536. ddev->device_tx_status = dma_cookie_status;
  537. ddev->device_issue_pending = ls2x_dma_issue_pending;
  538. ddev->device_prep_slave_sg = ls2x_dma_prep_slave_sg;
  539. ddev->device_prep_dma_cyclic = ls2x_dma_prep_dma_cyclic;
  540. ddev->device_config = ls2x_dma_slave_config;
  541. ddev->device_terminate_all = ls2x_dma_terminate_all;
  542. ddev->device_synchronize = ls2x_dma_synchronize;
  543. ddev->device_pause = ls2x_dma_pause;
  544. ddev->device_resume = ls2x_dma_resume;
  545. ddev->src_addr_widths = LDMA_SLAVE_BUSWIDTHS;
  546. ddev->dst_addr_widths = LDMA_SLAVE_BUSWIDTHS;
  547. ddev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  548. ret = dma_async_device_register(&priv->ddev);
  549. if (ret < 0)
  550. goto disable_clk;
  551. ret = of_dma_controller_register(dev->of_node, of_dma_xlate_by_chan_id, priv);
  552. if (ret < 0)
  553. goto unregister_dmac;
  554. platform_set_drvdata(pdev, priv);
  555. dev_info(dev, "Loongson LS2X APB DMA driver registered successfully.\n");
  556. return 0;
  557. unregister_dmac:
  558. dma_async_device_unregister(&priv->ddev);
  559. disable_clk:
  560. clk_disable_unprepare(priv->dma_clk);
  561. return ret;
  562. }
  563. /*
  564. * ls2x_dma_remove - Driver remove function
  565. * @pdev: Pointer to the platform_device structure
  566. */
  567. static void ls2x_dma_remove(struct platform_device *pdev)
  568. {
  569. struct ls2x_dma_priv *priv = platform_get_drvdata(pdev);
  570. of_dma_controller_free(pdev->dev.of_node);
  571. dma_async_device_unregister(&priv->ddev);
  572. clk_disable_unprepare(priv->dma_clk);
  573. }
  574. static const struct of_device_id ls2x_dma_of_match_table[] = {
  575. { .compatible = "loongson,ls2k1000-apbdma" },
  576. { /* sentinel */ }
  577. };
  578. MODULE_DEVICE_TABLE(of, ls2x_dma_of_match_table);
  579. static struct platform_driver ls2x_dmac_driver = {
  580. .probe = ls2x_dma_probe,
  581. .remove_new = ls2x_dma_remove,
  582. .driver = {
  583. .name = "ls2x-apbdma",
  584. .of_match_table = ls2x_dma_of_match_table,
  585. },
  586. };
  587. module_platform_driver(ls2x_dmac_driver);
  588. MODULE_DESCRIPTION("Loongson LS2X APB DMA Controller driver");
  589. MODULE_AUTHOR("Loongson Technology Corporation Limited");
  590. MODULE_LICENSE("GPL");