hsu.c 13 KB

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