hsu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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->dst_addr_width;
  58. } else if (hsuc->direction == DMA_DEV_TO_MEM) {
  59. bsr = config->src_maxburst;
  60. mtsr = config->src_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. * 0 for invalid channel number, 1 otherwise.
  171. */
  172. int hsu_dma_do_irq(struct hsu_dma_chip *chip, unsigned short nr, u32 status)
  173. {
  174. struct hsu_dma_chan *hsuc;
  175. struct hsu_dma_desc *desc;
  176. unsigned long flags;
  177. /* Sanity check */
  178. if (nr >= chip->hsu->nr_channels)
  179. return 0;
  180. hsuc = &chip->hsu->chan[nr];
  181. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  182. desc = hsuc->desc;
  183. if (desc) {
  184. if (status & HSU_CH_SR_CHE) {
  185. desc->status = DMA_ERROR;
  186. } else if (desc->active < desc->nents) {
  187. hsu_dma_start_channel(hsuc);
  188. } else {
  189. vchan_cookie_complete(&desc->vdesc);
  190. desc->status = DMA_COMPLETE;
  191. hsu_dma_start_transfer(hsuc);
  192. }
  193. }
  194. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  195. return 1;
  196. }
  197. EXPORT_SYMBOL_GPL(hsu_dma_do_irq);
  198. static struct hsu_dma_desc *hsu_dma_alloc_desc(unsigned int nents)
  199. {
  200. struct hsu_dma_desc *desc;
  201. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  202. if (!desc)
  203. return NULL;
  204. desc->sg = kcalloc(nents, sizeof(*desc->sg), GFP_NOWAIT);
  205. if (!desc->sg) {
  206. kfree(desc);
  207. return NULL;
  208. }
  209. return desc;
  210. }
  211. static void hsu_dma_desc_free(struct virt_dma_desc *vdesc)
  212. {
  213. struct hsu_dma_desc *desc = to_hsu_dma_desc(vdesc);
  214. kfree(desc->sg);
  215. kfree(desc);
  216. }
  217. static struct dma_async_tx_descriptor *hsu_dma_prep_slave_sg(
  218. struct dma_chan *chan, struct scatterlist *sgl,
  219. unsigned int sg_len, enum dma_transfer_direction direction,
  220. unsigned long flags, void *context)
  221. {
  222. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  223. struct hsu_dma_desc *desc;
  224. struct scatterlist *sg;
  225. unsigned int i;
  226. desc = hsu_dma_alloc_desc(sg_len);
  227. if (!desc)
  228. return NULL;
  229. for_each_sg(sgl, sg, sg_len, i) {
  230. desc->sg[i].addr = sg_dma_address(sg);
  231. desc->sg[i].len = sg_dma_len(sg);
  232. desc->length += sg_dma_len(sg);
  233. }
  234. desc->nents = sg_len;
  235. desc->direction = direction;
  236. /* desc->active = 0 by kzalloc */
  237. desc->status = DMA_IN_PROGRESS;
  238. return vchan_tx_prep(&hsuc->vchan, &desc->vdesc, flags);
  239. }
  240. static void hsu_dma_issue_pending(struct dma_chan *chan)
  241. {
  242. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  243. unsigned long flags;
  244. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  245. if (vchan_issue_pending(&hsuc->vchan) && !hsuc->desc)
  246. hsu_dma_start_transfer(hsuc);
  247. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  248. }
  249. static size_t hsu_dma_active_desc_size(struct hsu_dma_chan *hsuc)
  250. {
  251. struct hsu_dma_desc *desc = hsuc->desc;
  252. size_t bytes = 0;
  253. int i;
  254. for (i = desc->active; i < desc->nents; i++)
  255. bytes += desc->sg[i].len;
  256. i = HSU_DMA_CHAN_NR_DESC - 1;
  257. do {
  258. bytes += hsu_chan_readl(hsuc, HSU_CH_DxTSR(i));
  259. } while (--i >= 0);
  260. return bytes;
  261. }
  262. static enum dma_status hsu_dma_tx_status(struct dma_chan *chan,
  263. dma_cookie_t cookie, struct dma_tx_state *state)
  264. {
  265. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  266. struct virt_dma_desc *vdesc;
  267. enum dma_status status;
  268. size_t bytes;
  269. unsigned long flags;
  270. status = dma_cookie_status(chan, cookie, state);
  271. if (status == DMA_COMPLETE)
  272. return status;
  273. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  274. vdesc = vchan_find_desc(&hsuc->vchan, cookie);
  275. if (hsuc->desc && cookie == hsuc->desc->vdesc.tx.cookie) {
  276. bytes = hsu_dma_active_desc_size(hsuc);
  277. dma_set_residue(state, bytes);
  278. status = hsuc->desc->status;
  279. } else if (vdesc) {
  280. bytes = to_hsu_dma_desc(vdesc)->length;
  281. dma_set_residue(state, bytes);
  282. }
  283. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  284. return status;
  285. }
  286. static int hsu_dma_slave_config(struct dma_chan *chan,
  287. struct dma_slave_config *config)
  288. {
  289. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  290. /* Check if chan will be configured for slave transfers */
  291. if (!is_slave_direction(config->direction))
  292. return -EINVAL;
  293. memcpy(&hsuc->config, config, sizeof(hsuc->config));
  294. return 0;
  295. }
  296. static int hsu_dma_pause(struct dma_chan *chan)
  297. {
  298. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  299. unsigned long flags;
  300. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  301. if (hsuc->desc && hsuc->desc->status == DMA_IN_PROGRESS) {
  302. hsu_chan_disable(hsuc);
  303. hsuc->desc->status = DMA_PAUSED;
  304. }
  305. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  306. return 0;
  307. }
  308. static int hsu_dma_resume(struct dma_chan *chan)
  309. {
  310. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  311. unsigned long flags;
  312. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  313. if (hsuc->desc && hsuc->desc->status == DMA_PAUSED) {
  314. hsuc->desc->status = DMA_IN_PROGRESS;
  315. hsu_chan_enable(hsuc);
  316. }
  317. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  318. return 0;
  319. }
  320. static int hsu_dma_terminate_all(struct dma_chan *chan)
  321. {
  322. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  323. unsigned long flags;
  324. LIST_HEAD(head);
  325. spin_lock_irqsave(&hsuc->vchan.lock, flags);
  326. hsu_dma_stop_channel(hsuc);
  327. if (hsuc->desc) {
  328. hsu_dma_desc_free(&hsuc->desc->vdesc);
  329. hsuc->desc = NULL;
  330. }
  331. vchan_get_all_descriptors(&hsuc->vchan, &head);
  332. spin_unlock_irqrestore(&hsuc->vchan.lock, flags);
  333. vchan_dma_desc_free_list(&hsuc->vchan, &head);
  334. return 0;
  335. }
  336. static void hsu_dma_free_chan_resources(struct dma_chan *chan)
  337. {
  338. vchan_free_chan_resources(to_virt_chan(chan));
  339. }
  340. static void hsu_dma_synchronize(struct dma_chan *chan)
  341. {
  342. struct hsu_dma_chan *hsuc = to_hsu_dma_chan(chan);
  343. vchan_synchronize(&hsuc->vchan);
  344. }
  345. int hsu_dma_probe(struct hsu_dma_chip *chip)
  346. {
  347. struct hsu_dma *hsu;
  348. void __iomem *addr = chip->regs + chip->offset;
  349. unsigned short i;
  350. int ret;
  351. hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
  352. if (!hsu)
  353. return -ENOMEM;
  354. chip->hsu = hsu;
  355. /* Calculate nr_channels from the IO space length */
  356. hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
  357. hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
  358. sizeof(*hsu->chan), GFP_KERNEL);
  359. if (!hsu->chan)
  360. return -ENOMEM;
  361. INIT_LIST_HEAD(&hsu->dma.channels);
  362. for (i = 0; i < hsu->nr_channels; i++) {
  363. struct hsu_dma_chan *hsuc = &hsu->chan[i];
  364. hsuc->vchan.desc_free = hsu_dma_desc_free;
  365. vchan_init(&hsuc->vchan, &hsu->dma);
  366. hsuc->direction = (i & 0x1) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  367. hsuc->reg = addr + i * HSU_DMA_CHAN_LENGTH;
  368. }
  369. dma_cap_set(DMA_SLAVE, hsu->dma.cap_mask);
  370. dma_cap_set(DMA_PRIVATE, hsu->dma.cap_mask);
  371. hsu->dma.device_free_chan_resources = hsu_dma_free_chan_resources;
  372. hsu->dma.device_prep_slave_sg = hsu_dma_prep_slave_sg;
  373. hsu->dma.device_issue_pending = hsu_dma_issue_pending;
  374. hsu->dma.device_tx_status = hsu_dma_tx_status;
  375. hsu->dma.device_config = hsu_dma_slave_config;
  376. hsu->dma.device_pause = hsu_dma_pause;
  377. hsu->dma.device_resume = hsu_dma_resume;
  378. hsu->dma.device_terminate_all = hsu_dma_terminate_all;
  379. hsu->dma.device_synchronize = hsu_dma_synchronize;
  380. hsu->dma.src_addr_widths = HSU_DMA_BUSWIDTHS;
  381. hsu->dma.dst_addr_widths = HSU_DMA_BUSWIDTHS;
  382. hsu->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  383. hsu->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  384. hsu->dma.dev = chip->dev;
  385. dma_set_max_seg_size(hsu->dma.dev, HSU_CH_DxTSR_MASK);
  386. ret = dma_async_device_register(&hsu->dma);
  387. if (ret)
  388. return ret;
  389. dev_info(chip->dev, "Found HSU DMA, %d channels\n", hsu->nr_channels);
  390. return 0;
  391. }
  392. EXPORT_SYMBOL_GPL(hsu_dma_probe);
  393. int hsu_dma_remove(struct hsu_dma_chip *chip)
  394. {
  395. struct hsu_dma *hsu = chip->hsu;
  396. unsigned short i;
  397. dma_async_device_unregister(&hsu->dma);
  398. for (i = 0; i < hsu->nr_channels; i++) {
  399. struct hsu_dma_chan *hsuc = &hsu->chan[i];
  400. tasklet_kill(&hsuc->vchan.task);
  401. }
  402. return 0;
  403. }
  404. EXPORT_SYMBOL_GPL(hsu_dma_remove);
  405. MODULE_LICENSE("GPL v2");
  406. MODULE_DESCRIPTION("High Speed UART DMA core driver");
  407. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");