PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/dma/hsu/hsu.c

https://gitlab.com/chprasanna93/linux
C | 501 lines | 329 code | 99 blank | 73 comment | 49 complexity | 79a72303669f87dedfea9569bbd4b493 MD5 | raw file
  1. /*
  2. * Core driver for the High Speed UART DMA
  3. *
  4. * Copyright (C) 2015 Intel Corporation
  5. * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  6. *
  7. * Partially based on the bits found in drivers/tty/serial/mfd.c.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. /*
  14. * DMA channel allocation:
  15. * 1. Even number chans are used for DMA Read (UART TX), odd chans for DMA
  16. * Write (UART RX).
  17. * 2. 0/1 channel are assigned to port 0, 2/3 chan to port 1, 4/5 chan to
  18. * port 3, and so on.
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include "hsu.h"
  27. #define HSU_DMA_BUSWIDTHS \
  28. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  29. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  30. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  31. BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
  32. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  33. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES) | \
  34. BIT(DMA_SLAVE_BUSWIDTH_16_BYTES)
  35. static inline void hsu_chan_disable(struct hsu_dma_chan *hsuc)
  36. {
  37. hsu_chan_writel(hsuc, HSU_CH_CR, 0);
  38. }
  39. static inline void hsu_chan_enable(struct hsu_dma_chan *hsuc)
  40. {
  41. u32 cr = HSU_CH_CR_CHA;
  42. if (hsuc->direction == DMA_MEM_TO_DEV)
  43. cr &= ~HSU_CH_CR_CHD;
  44. else if (hsuc->direction == DMA_DEV_TO_MEM)
  45. cr |= HSU_CH_CR_CHD;
  46. hsu_chan_writel(hsuc, HSU_CH_CR, cr);
  47. }
  48. static void hsu_dma_chan_start(struct hsu_dma_chan *hsuc)
  49. {
  50. struct dma_slave_config *config = &hsuc->config;
  51. struct hsu_dma_desc *desc = hsuc->desc;
  52. u32 bsr = 0, mtsr = 0; /* to shut the compiler up */
  53. u32 dcr = HSU_CH_DCR_CHSOE | HSU_CH_DCR_CHEI;
  54. unsigned int i, count;
  55. if (hsuc->direction == DMA_MEM_TO_DEV) {
  56. bsr = config->dst_maxburst;
  57. mtsr = config->src_addr_width;
  58. } else if (hsuc->direction == DMA_DEV_TO_MEM) {
  59. bsr = config->src_maxburst;
  60. mtsr = config->dst_addr_width;
  61. }
  62. hsu_chan_disable(hsuc);
  63. hsu_chan_writel(hsuc, HSU_CH_DCR, 0);
  64. hsu_chan_writel(hsuc, HSU_CH_BSR, bsr);
  65. hsu_chan_writel(hsuc, HSU_CH_MTSR, mtsr);
  66. /* Set descriptors */
  67. count = desc->nents - desc->active;
  68. for (i = 0; i < count && i < HSU_DMA_CHAN_NR_DESC; i++) {
  69. hsu_chan_writel(hsuc, HSU_CH_DxSAR(i), desc->sg[i].addr);
  70. hsu_chan_writel(hsuc, HSU_CH_DxTSR(i), desc->sg[i].len);
  71. /* Prepare value for DCR */
  72. dcr |= HSU_CH_DCR_DESCA(i);
  73. dcr |= HSU_CH_DCR_CHTOI(i); /* timeout bit, see HSU Errata 1 */
  74. desc->active++;
  75. }
  76. /* Only for the last descriptor in the chain */
  77. dcr |= HSU_CH_DCR_CHSOD(count - 1);
  78. dcr |= HSU_CH_DCR_CHDI(count - 1);
  79. hsu_chan_writel(hsuc, HSU_CH_DCR, dcr);
  80. hsu_chan_enable(hsuc);
  81. }
  82. static void hsu_dma_stop_channel(struct hsu_dma_chan *hsuc)
  83. {
  84. hsu_chan_disable(hsuc);
  85. hsu_chan_writel(hsuc, HSU_CH_DCR, 0);
  86. }
  87. static void hsu_dma_start_channel(struct hsu_dma_chan *hsuc)
  88. {
  89. hsu_dma_chan_start(hsuc);
  90. }
  91. static void hsu_dma_start_transfer(struct hsu_dma_chan *hsuc)
  92. {
  93. struct virt_dma_desc *vdesc;
  94. /* Get the next descriptor */
  95. vdesc = vchan_next_desc(&hsuc->vchan);
  96. if (!vdesc) {
  97. hsuc->desc = NULL;
  98. return;
  99. }
  100. list_del(&vdesc->node);
  101. hsuc->desc = to_hsu_dma_desc(vdesc);
  102. /* Start the channel with a new descriptor */
  103. hsu_dma_start_channel(hsuc);
  104. }
  105. /*
  106. * hsu_dma_get_status() - get DMA channel status
  107. * @chip: HSUART DMA chip
  108. * @nr: DMA channel number
  109. * @status: pointer for DMA Channel Status Register value
  110. *
  111. * Description:
  112. * The function reads and clears the DMA Channel Status Register, checks
  113. * if it was a timeout interrupt and returns a corresponding value.
  114. *
  115. * Caller should provide a valid pointer for the DMA Channel Status
  116. * Register value that will be returned in @status.
  117. *
  118. * Return:
  119. * 1 for DMA timeout status, 0 for other DMA status, or error code for
  120. * invalid parameters or no interrupt pending.
  121. */
  122. int hsu_dma_get_status(struct hsu_dma_chip *chip, unsigned short nr,
  123. u32 *status)
  124. {
  125. struct hsu_dma_chan *hsuc;
  126. unsigned long flags;
  127. u32 sr;
  128. /* Sanity check */
  129. if (nr >= chip->hsu->nr_channels)
  130. return -EINVAL;
  131. hsuc = &chip->hsu->chan[nr];
  132. /*
  133. * No matter what situation, need read clear the IRQ status
  134. * There is a bug, see Errata 5, HSD 2900918
  135. */
  136. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  137. sr = hsu_chan_readl(hsuc, HSU_CH_SR);
  138. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  139. /* Check if any interrupt is pending */
  140. sr &= ~(HSU_CH_SR_DESCE_ANY | HSU_CH_SR_CDESC_ANY);
  141. if (!sr)
  142. return -EIO;
  143. /* Timeout IRQ, need wait some time, see Errata 2 */
  144. if (sr & HSU_CH_SR_DESCTO_ANY)
  145. udelay(2);
  146. /*
  147. * At this point, at least one of Descriptor Time Out, Channel Error
  148. * or Descriptor Done bits must be set. Clear the Descriptor Time Out
  149. * bits and if sr is still non-zero, it must be channel error or
  150. * descriptor done which are higher priority than timeout and handled
  151. * in hsu_dma_do_irq(). Else, it must be a timeout.
  152. */
  153. sr &= ~HSU_CH_SR_DESCTO_ANY;
  154. *status = sr;
  155. return sr ? 0 : 1;
  156. }
  157. EXPORT_SYMBOL_GPL(hsu_dma_get_status);
  158. /*
  159. * hsu_dma_do_irq() - DMA interrupt handler
  160. * @chip: HSUART DMA chip
  161. * @nr: DMA channel number
  162. * @status: Channel Status Register value
  163. *
  164. * Description:
  165. * This function handles Channel Error and Descriptor Done interrupts.
  166. * This function should be called after determining that the DMA interrupt
  167. * is not a normal timeout interrupt, ie. hsu_dma_get_status() returned 0.
  168. *
  169. * Return:
  170. * IRQ_NONE for invalid channel number, IRQ_HANDLED otherwise.
  171. */
  172. irqreturn_t hsu_dma_do_irq(struct hsu_dma_chip *chip, unsigned short nr,
  173. u32 status)
  174. {
  175. struct hsu_dma_chan *hsuc;
  176. struct hsu_dma_desc *desc;
  177. unsigned long flags;
  178. /* Sanity check */
  179. if (nr >= chip->hsu->nr_channels)
  180. return IRQ_NONE;
  181. hsuc = &chip->hsu->chan[nr];
  182. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  183. desc = hsuc->desc;
  184. if (desc) {
  185. if (status & HSU_CH_SR_CHE) {
  186. desc->status = DMA_ERROR;
  187. } else if (desc->active < desc->nents) {
  188. hsu_dma_start_channel(hsuc);
  189. } else {
  190. vchan_cookie_complete(&desc->vdesc);
  191. desc->status = DMA_COMPLETE;
  192. hsu_dma_start_transfer(hsuc);
  193. }
  194. }
  195. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  196. return IRQ_HANDLED;
  197. }
  198. EXPORT_SYMBOL_GPL(hsu_dma_do_irq);
  199. static struct hsu_dma_desc *hsu_dma_alloc_desc(unsigned int nents)
  200. {
  201. struct hsu_dma_desc *desc;
  202. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  203. if (!desc)
  204. return NULL;
  205. desc->sg = kcalloc(nents, sizeof(*desc->sg), GFP_NOWAIT);
  206. if (!desc->sg) {
  207. kfree(desc);
  208. return NULL;
  209. }
  210. return desc;
  211. }
  212. static void hsu_dma_desc_free(struct virt_dma_desc *vdesc)
  213. {
  214. struct hsu_dma_desc *desc = to_hsu_dma_desc(vdesc);
  215. kfree(desc->sg);
  216. kfree(desc);
  217. }
  218. static struct dma_async_tx_descriptor *hsu_dma_prep_slave_sg(
  219. struct dma_chan *chan, struct scatterlist *sgl,
  220. unsigned int sg_len, enum dma_transfer_direction direction,
  221. unsigned long flags, void *context)
  222. {
  223. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  224. struct hsu_dma_desc *desc;
  225. struct scatterlist *sg;
  226. unsigned int i;
  227. desc = hsu_dma_alloc_desc(sg_len);
  228. if (!desc)
  229. return NULL;
  230. for_each_sg(sgl, sg, sg_len, i) {
  231. desc->sg[i].addr = sg_dma_address(sg);
  232. desc->sg[i].len = sg_dma_len(sg);
  233. desc->length += sg_dma_len(sg);
  234. }
  235. desc->nents = sg_len;
  236. desc->direction = direction;
  237. /* desc->active = 0 by kzalloc */
  238. desc->status = DMA_IN_PROGRESS;
  239. return vchan_tx_prep(&hsuc->vchan, &desc->vdesc, flags);
  240. }
  241. static void hsu_dma_issue_pending(struct dma_chan *chan)
  242. {
  243. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  244. unsigned long flags;
  245. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  246. if (vchan_issue_pending(&hsuc->vchan) && !hsuc->desc)
  247. hsu_dma_start_transfer(hsuc);
  248. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  249. }
  250. static size_t hsu_dma_active_desc_size(struct hsu_dma_chan *hsuc)
  251. {
  252. struct hsu_dma_desc *desc = hsuc->desc;
  253. size_t bytes = 0;
  254. int i;
  255. for (i = desc->active; i < desc->nents; i++)
  256. bytes += desc->sg[i].len;
  257. i = HSU_DMA_CHAN_NR_DESC - 1;
  258. do {
  259. bytes += hsu_chan_readl(hsuc, HSU_CH_DxTSR(i));
  260. } while (--i >= 0);
  261. return bytes;
  262. }
  263. static enum dma_status hsu_dma_tx_status(struct dma_chan *chan,
  264. dma_cookie_t cookie, struct dma_tx_state *state)
  265. {
  266. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  267. struct virt_dma_desc *vdesc;
  268. enum dma_status status;
  269. size_t bytes;
  270. unsigned long flags;
  271. status = dma_cookie_status(chan, cookie, state);
  272. if (status == DMA_COMPLETE)
  273. return status;
  274. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  275. vdesc = vchan_find_desc(&hsuc->vchan, cookie);
  276. if (hsuc->desc && cookie == hsuc->desc->vdesc.tx.cookie) {
  277. bytes = hsu_dma_active_desc_size(hsuc);
  278. dma_set_residue(state, bytes);
  279. status = hsuc->desc->status;
  280. } else if (vdesc) {
  281. bytes = to_hsu_dma_desc(vdesc)->length;
  282. dma_set_residue(state, bytes);
  283. }
  284. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  285. return status;
  286. }
  287. static int hsu_dma_slave_config(struct dma_chan *chan,
  288. struct dma_slave_config *config)
  289. {
  290. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  291. /* Check if chan will be configured for slave transfers */
  292. if (!is_slave_direction(config->direction))
  293. return -EINVAL;
  294. memcpy(&hsuc->config, config, sizeof(hsuc->config));
  295. return 0;
  296. }
  297. static int hsu_dma_pause(struct dma_chan *chan)
  298. {
  299. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  300. unsigned long flags;
  301. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  302. if (hsuc->desc && hsuc->desc->status == DMA_IN_PROGRESS) {
  303. hsu_chan_disable(hsuc);
  304. hsuc->desc->status = DMA_PAUSED;
  305. }
  306. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  307. return 0;
  308. }
  309. static int hsu_dma_resume(struct dma_chan *chan)
  310. {
  311. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  312. unsigned long flags;
  313. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  314. if (hsuc->desc && hsuc->desc->status == DMA_PAUSED) {
  315. hsuc->desc->status = DMA_IN_PROGRESS;
  316. hsu_chan_enable(hsuc);
  317. }
  318. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  319. return 0;
  320. }
  321. static int hsu_dma_terminate_all(struct dma_chan *chan)
  322. {
  323. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  324. unsigned long flags;
  325. LIST_HEAD(head);
  326. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  327. hsu_dma_stop_channel(hsuc);
  328. if (hsuc->desc) {
  329. hsu_dma_desc_free(&hsuc->desc->vdesc);
  330. hsuc->desc = NULL;
  331. }
  332. vchan_get_all_descriptors(&hsuc->vchan, &head);
  333. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  334. vchan_dma_desc_free_list(&hsuc->vchan, &head);
  335. return 0;
  336. }
  337. static void hsu_dma_free_chan_resources(struct dma_chan *chan)
  338. {
  339. vchan_free_chan_resources(to_virt_chan(chan));
  340. }
  341. int hsu_dma_probe(struct hsu_dma_chip *chip)
  342. {
  343. struct hsu_dma *hsu;
  344. void __iomem *addr = chip->regs + chip->offset;
  345. unsigned short i;
  346. int ret;
  347. hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
  348. if (!hsu)
  349. return -ENOMEM;
  350. chip->hsu = hsu;
  351. /* Calculate nr_channels from the IO space length */
  352. hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
  353. hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
  354. sizeof(*hsu->chan), GFP_KERNEL);
  355. if (!hsu->chan)
  356. return -ENOMEM;
  357. INIT_LIST_HEAD(&hsu->dma.channels);
  358. for (i = 0; i < hsu->nr_channels; i++) {
  359. struct hsu_dma_chan *hsuc = &hsu->chan[i];
  360. hsuc->vchan.desc_free = hsu_dma_desc_free;
  361. vchan_init(&hsuc->vchan, &hsu->dma);
  362. hsuc->direction = (i & 0x1) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  363. hsuc->reg = addr + i * HSU_DMA_CHAN_LENGTH;
  364. }
  365. dma_cap_set(DMA_SLAVE, hsu->dma.cap_mask);
  366. dma_cap_set(DMA_PRIVATE, hsu->dma.cap_mask);
  367. hsu->dma.device_free_chan_resources = hsu_dma_free_chan_resources;
  368. hsu->dma.device_prep_slave_sg = hsu_dma_prep_slave_sg;
  369. hsu->dma.device_issue_pending = hsu_dma_issue_pending;
  370. hsu->dma.device_tx_status = hsu_dma_tx_status;
  371. hsu->dma.device_config = hsu_dma_slave_config;
  372. hsu->dma.device_pause = hsu_dma_pause;
  373. hsu->dma.device_resume = hsu_dma_resume;
  374. hsu->dma.device_terminate_all = hsu_dma_terminate_all;
  375. hsu->dma.src_addr_widths = HSU_DMA_BUSWIDTHS;
  376. hsu->dma.dst_addr_widths = HSU_DMA_BUSWIDTHS;
  377. hsu->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  378. hsu->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  379. hsu->dma.dev = chip->dev;
  380. dma_set_max_seg_size(hsu->dma.dev, HSU_CH_DxTSR_MASK);
  381. ret = dma_async_device_register(&hsu->dma);
  382. if (ret)
  383. return ret;
  384. dev_info(chip->dev, "Found HSU DMA, %d channels\n", hsu->nr_channels);
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(hsu_dma_probe);
  388. int hsu_dma_remove(struct hsu_dma_chip *chip)
  389. {
  390. struct hsu_dma *hsu = chip->hsu;
  391. unsigned short i;
  392. dma_async_device_unregister(&hsu->dma);
  393. for (i = 0; i < hsu->nr_channels; i++) {
  394. struct hsu_dma_chan *hsuc = &hsu->chan[i];
  395. tasklet_kill(&hsuc->vchan.task);
  396. }
  397. return 0;
  398. }
  399. EXPORT_SYMBOL_GPL(hsu_dma_remove);
  400. MODULE_LICENSE("GPL v2");
  401. MODULE_DESCRIPTION("High Speed UART DMA core driver");
  402. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");