usb-dmac.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * Renesas USB DMA Controller Driver
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. *
  6. * based on rcar-dmac.c
  7. * Copyright (C) 2014 Renesas Electronics Inc.
  8. * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/dmaengine.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include "../dmaengine.h"
  28. #include "../virt-dma.h"
  29. /*
  30. * struct usb_dmac_sg - Descriptor for a hardware transfer
  31. * @mem_addr: memory address
  32. * @size: transfer size in bytes
  33. */
  34. struct usb_dmac_sg {
  35. dma_addr_t mem_addr;
  36. u32 size;
  37. };
  38. /*
  39. * struct usb_dmac_desc - USB DMA Transfer Descriptor
  40. * @vd: base virtual channel DMA transaction descriptor
  41. * @direction: direction of the DMA transfer
  42. * @sg_allocated_len: length of allocated sg
  43. * @sg_len: length of sg
  44. * @sg_index: index of sg
  45. * @residue: residue after the DMAC completed a transfer
  46. * @node: node for desc_got and desc_freed
  47. * @done_cookie: cookie after the DMAC completed a transfer
  48. * @sg: information for the transfer
  49. */
  50. struct usb_dmac_desc {
  51. struct virt_dma_desc vd;
  52. enum dma_transfer_direction direction;
  53. unsigned int sg_allocated_len;
  54. unsigned int sg_len;
  55. unsigned int sg_index;
  56. u32 residue;
  57. struct list_head node;
  58. dma_cookie_t done_cookie;
  59. struct usb_dmac_sg sg[0];
  60. };
  61. #define to_usb_dmac_desc(vd) container_of(vd, struct usb_dmac_desc, vd)
  62. /*
  63. * struct usb_dmac_chan - USB DMA Controller Channel
  64. * @vc: base virtual DMA channel object
  65. * @iomem: channel I/O memory base
  66. * @index: index of this channel in the controller
  67. * @irq: irq number of this channel
  68. * @desc: the current descriptor
  69. * @descs_allocated: number of descriptors allocated
  70. * @desc_got: got descriptors
  71. * @desc_freed: freed descriptors after the DMAC completed a transfer
  72. */
  73. struct usb_dmac_chan {
  74. struct virt_dma_chan vc;
  75. void __iomem *iomem;
  76. unsigned int index;
  77. int irq;
  78. struct usb_dmac_desc *desc;
  79. int descs_allocated;
  80. struct list_head desc_got;
  81. struct list_head desc_freed;
  82. };
  83. #define to_usb_dmac_chan(c) container_of(c, struct usb_dmac_chan, vc.chan)
  84. /*
  85. * struct usb_dmac - USB DMA Controller
  86. * @engine: base DMA engine object
  87. * @dev: the hardware device
  88. * @iomem: remapped I/O memory base
  89. * @n_channels: number of available channels
  90. * @channels: array of DMAC channels
  91. */
  92. struct usb_dmac {
  93. struct dma_device engine;
  94. struct device *dev;
  95. void __iomem *iomem;
  96. unsigned int n_channels;
  97. struct usb_dmac_chan *channels;
  98. };
  99. #define to_usb_dmac(d) container_of(d, struct usb_dmac, engine)
  100. /* -----------------------------------------------------------------------------
  101. * Registers
  102. */
  103. #define USB_DMAC_CHAN_OFFSET(i) (0x20 + 0x20 * (i))
  104. #define USB_DMASWR 0x0008
  105. #define USB_DMASWR_SWR (1 << 0)
  106. #define USB_DMAOR 0x0060
  107. #define USB_DMAOR_AE (1 << 1)
  108. #define USB_DMAOR_DME (1 << 0)
  109. #define USB_DMASAR 0x0000
  110. #define USB_DMADAR 0x0004
  111. #define USB_DMATCR 0x0008
  112. #define USB_DMATCR_MASK 0x00ffffff
  113. #define USB_DMACHCR 0x0014
  114. #define USB_DMACHCR_FTE (1 << 24)
  115. #define USB_DMACHCR_NULLE (1 << 16)
  116. #define USB_DMACHCR_NULL (1 << 12)
  117. #define USB_DMACHCR_TS_8B ((0 << 7) | (0 << 6))
  118. #define USB_DMACHCR_TS_16B ((0 << 7) | (1 << 6))
  119. #define USB_DMACHCR_TS_32B ((1 << 7) | (0 << 6))
  120. #define USB_DMACHCR_IE (1 << 5)
  121. #define USB_DMACHCR_SP (1 << 2)
  122. #define USB_DMACHCR_TE (1 << 1)
  123. #define USB_DMACHCR_DE (1 << 0)
  124. #define USB_DMATEND 0x0018
  125. /* Hardcode the xfer_shift to 5 (32bytes) */
  126. #define USB_DMAC_XFER_SHIFT 5
  127. #define USB_DMAC_XFER_SIZE (1 << USB_DMAC_XFER_SHIFT)
  128. #define USB_DMAC_CHCR_TS USB_DMACHCR_TS_32B
  129. #define USB_DMAC_SLAVE_BUSWIDTH DMA_SLAVE_BUSWIDTH_32_BYTES
  130. /* for descriptors */
  131. #define USB_DMAC_INITIAL_NR_DESC 16
  132. #define USB_DMAC_INITIAL_NR_SG 8
  133. /* -----------------------------------------------------------------------------
  134. * Device access
  135. */
  136. static void usb_dmac_write(struct usb_dmac *dmac, u32 reg, u32 data)
  137. {
  138. writel(data, dmac->iomem + reg);
  139. }
  140. static u32 usb_dmac_read(struct usb_dmac *dmac, u32 reg)
  141. {
  142. return readl(dmac->iomem + reg);
  143. }
  144. static u32 usb_dmac_chan_read(struct usb_dmac_chan *chan, u32 reg)
  145. {
  146. return readl(chan->iomem + reg);
  147. }
  148. static void usb_dmac_chan_write(struct usb_dmac_chan *chan, u32 reg, u32 data)
  149. {
  150. writel(data, chan->iomem + reg);
  151. }
  152. /* -----------------------------------------------------------------------------
  153. * Initialization and configuration
  154. */
  155. static bool usb_dmac_chan_is_busy(struct usb_dmac_chan *chan)
  156. {
  157. u32 chcr = usb_dmac_chan_read(chan, USB_DMACHCR);
  158. return (chcr & (USB_DMACHCR_DE | USB_DMACHCR_TE)) == USB_DMACHCR_DE;
  159. }
  160. static u32 usb_dmac_calc_tend(u32 size)
  161. {
  162. /*
  163. * Please refer to the Figure "Example of Final Transaction Valid
  164. * Data Transfer Enable (EDTEN) Setting" in the data sheet.
  165. */
  166. return 0xffffffff << (32 - (size % USB_DMAC_XFER_SIZE ? :
  167. USB_DMAC_XFER_SIZE));
  168. }
  169. /* This function is already held by vc.lock */
  170. static void usb_dmac_chan_start_sg(struct usb_dmac_chan *chan,
  171. unsigned int index)
  172. {
  173. struct usb_dmac_desc *desc = chan->desc;
  174. struct usb_dmac_sg *sg = desc->sg + index;
  175. dma_addr_t src_addr = 0, dst_addr = 0;
  176. WARN_ON_ONCE(usb_dmac_chan_is_busy(chan));
  177. if (desc->direction == DMA_DEV_TO_MEM)
  178. dst_addr = sg->mem_addr;
  179. else
  180. src_addr = sg->mem_addr;
  181. dev_dbg(chan->vc.chan.device->dev,
  182. "chan%u: queue sg %p: %u@%pad -> %pad\n",
  183. chan->index, sg, sg->size, &src_addr, &dst_addr);
  184. usb_dmac_chan_write(chan, USB_DMASAR, src_addr & 0xffffffff);
  185. usb_dmac_chan_write(chan, USB_DMADAR, dst_addr & 0xffffffff);
  186. usb_dmac_chan_write(chan, USB_DMATCR,
  187. DIV_ROUND_UP(sg->size, USB_DMAC_XFER_SIZE));
  188. usb_dmac_chan_write(chan, USB_DMATEND, usb_dmac_calc_tend(sg->size));
  189. usb_dmac_chan_write(chan, USB_DMACHCR, USB_DMAC_CHCR_TS |
  190. USB_DMACHCR_NULLE | USB_DMACHCR_IE | USB_DMACHCR_DE);
  191. }
  192. /* This function is already held by vc.lock */
  193. static void usb_dmac_chan_start_desc(struct usb_dmac_chan *chan)
  194. {
  195. struct virt_dma_desc *vd;
  196. vd = vchan_next_desc(&chan->vc);
  197. if (!vd) {
  198. chan->desc = NULL;
  199. return;
  200. }
  201. /*
  202. * Remove this request from vc->desc_issued. Otherwise, this driver
  203. * will get the previous value from vchan_next_desc() after a transfer
  204. * was completed.
  205. */
  206. list_del(&vd->node);
  207. chan->desc = to_usb_dmac_desc(vd);
  208. chan->desc->sg_index = 0;
  209. usb_dmac_chan_start_sg(chan, 0);
  210. }
  211. static int usb_dmac_init(struct usb_dmac *dmac)
  212. {
  213. u16 dmaor;
  214. /* Clear all channels and enable the DMAC globally. */
  215. usb_dmac_write(dmac, USB_DMAOR, USB_DMAOR_DME);
  216. dmaor = usb_dmac_read(dmac, USB_DMAOR);
  217. if ((dmaor & (USB_DMAOR_AE | USB_DMAOR_DME)) != USB_DMAOR_DME) {
  218. dev_warn(dmac->dev, "DMAOR initialization failed.\n");
  219. return -EIO;
  220. }
  221. return 0;
  222. }
  223. /* -----------------------------------------------------------------------------
  224. * Descriptors allocation and free
  225. */
  226. static int usb_dmac_desc_alloc(struct usb_dmac_chan *chan, unsigned int sg_len,
  227. gfp_t gfp)
  228. {
  229. struct usb_dmac_desc *desc;
  230. unsigned long flags;
  231. desc = kzalloc(struct_size(desc, sg, sg_len), gfp);
  232. if (!desc)
  233. return -ENOMEM;
  234. desc->sg_allocated_len = sg_len;
  235. INIT_LIST_HEAD(&desc->node);
  236. spin_lock_irqsave(&chan->vc.lock, flags);
  237. list_add_tail(&desc->node, &chan->desc_freed);
  238. spin_unlock_irqrestore(&chan->vc.lock, flags);
  239. return 0;
  240. }
  241. static void usb_dmac_desc_free(struct usb_dmac_chan *chan)
  242. {
  243. struct usb_dmac_desc *desc, *_desc;
  244. LIST_HEAD(list);
  245. list_splice_init(&chan->desc_freed, &list);
  246. list_splice_init(&chan->desc_got, &list);
  247. list_for_each_entry_safe(desc, _desc, &list, node) {
  248. list_del(&desc->node);
  249. kfree(desc);
  250. }
  251. chan->descs_allocated = 0;
  252. }
  253. static struct usb_dmac_desc *usb_dmac_desc_get(struct usb_dmac_chan *chan,
  254. unsigned int sg_len, gfp_t gfp)
  255. {
  256. struct usb_dmac_desc *desc = NULL;
  257. unsigned long flags;
  258. /* Get a freed descritpor */
  259. spin_lock_irqsave(&chan->vc.lock, flags);
  260. list_for_each_entry(desc, &chan->desc_freed, node) {
  261. if (sg_len <= desc->sg_allocated_len) {
  262. list_move_tail(&desc->node, &chan->desc_got);
  263. spin_unlock_irqrestore(&chan->vc.lock, flags);
  264. return desc;
  265. }
  266. }
  267. spin_unlock_irqrestore(&chan->vc.lock, flags);
  268. /* Allocate a new descriptor */
  269. if (!usb_dmac_desc_alloc(chan, sg_len, gfp)) {
  270. /* If allocated the desc, it was added to tail of the list */
  271. spin_lock_irqsave(&chan->vc.lock, flags);
  272. desc = list_last_entry(&chan->desc_freed, struct usb_dmac_desc,
  273. node);
  274. list_move_tail(&desc->node, &chan->desc_got);
  275. spin_unlock_irqrestore(&chan->vc.lock, flags);
  276. return desc;
  277. }
  278. return NULL;
  279. }
  280. static void usb_dmac_desc_put(struct usb_dmac_chan *chan,
  281. struct usb_dmac_desc *desc)
  282. {
  283. unsigned long flags;
  284. spin_lock_irqsave(&chan->vc.lock, flags);
  285. list_move_tail(&desc->node, &chan->desc_freed);
  286. spin_unlock_irqrestore(&chan->vc.lock, flags);
  287. }
  288. /* -----------------------------------------------------------------------------
  289. * Stop and reset
  290. */
  291. static void usb_dmac_soft_reset(struct usb_dmac_chan *uchan)
  292. {
  293. struct dma_chan *chan = &uchan->vc.chan;
  294. struct usb_dmac *dmac = to_usb_dmac(chan->device);
  295. int i;
  296. /* Don't issue soft reset if any one of channels is busy */
  297. for (i = 0; i < dmac->n_channels; ++i) {
  298. if (usb_dmac_chan_is_busy(uchan))
  299. return;
  300. }
  301. usb_dmac_write(dmac, USB_DMAOR, 0);
  302. usb_dmac_write(dmac, USB_DMASWR, USB_DMASWR_SWR);
  303. udelay(100);
  304. usb_dmac_write(dmac, USB_DMASWR, 0);
  305. usb_dmac_write(dmac, USB_DMAOR, 1);
  306. }
  307. static void usb_dmac_chan_halt(struct usb_dmac_chan *chan)
  308. {
  309. u32 chcr = usb_dmac_chan_read(chan, USB_DMACHCR);
  310. chcr &= ~(USB_DMACHCR_IE | USB_DMACHCR_TE | USB_DMACHCR_DE);
  311. usb_dmac_chan_write(chan, USB_DMACHCR, chcr);
  312. usb_dmac_soft_reset(chan);
  313. }
  314. static void usb_dmac_stop(struct usb_dmac *dmac)
  315. {
  316. usb_dmac_write(dmac, USB_DMAOR, 0);
  317. }
  318. /* -----------------------------------------------------------------------------
  319. * DMA engine operations
  320. */
  321. static int usb_dmac_alloc_chan_resources(struct dma_chan *chan)
  322. {
  323. struct usb_dmac_chan *uchan = to_usb_dmac_chan(chan);
  324. int ret;
  325. while (uchan->descs_allocated < USB_DMAC_INITIAL_NR_DESC) {
  326. ret = usb_dmac_desc_alloc(uchan, USB_DMAC_INITIAL_NR_SG,
  327. GFP_KERNEL);
  328. if (ret < 0) {
  329. usb_dmac_desc_free(uchan);
  330. return ret;
  331. }
  332. uchan->descs_allocated++;
  333. }
  334. return pm_runtime_get_sync(chan->device->dev);
  335. }
  336. static void usb_dmac_free_chan_resources(struct dma_chan *chan)
  337. {
  338. struct usb_dmac_chan *uchan = to_usb_dmac_chan(chan);
  339. unsigned long flags;
  340. /* Protect against ISR */
  341. spin_lock_irqsave(&uchan->vc.lock, flags);
  342. usb_dmac_chan_halt(uchan);
  343. spin_unlock_irqrestore(&uchan->vc.lock, flags);
  344. usb_dmac_desc_free(uchan);
  345. vchan_free_chan_resources(&uchan->vc);
  346. pm_runtime_put(chan->device->dev);
  347. }
  348. static struct dma_async_tx_descriptor *
  349. usb_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  350. unsigned int sg_len, enum dma_transfer_direction dir,
  351. unsigned long dma_flags, void *context)
  352. {
  353. struct usb_dmac_chan *uchan = to_usb_dmac_chan(chan);
  354. struct usb_dmac_desc *desc;
  355. struct scatterlist *sg;
  356. int i;
  357. if (!sg_len) {
  358. dev_warn(chan->device->dev,
  359. "%s: bad parameter: len=%d\n", __func__, sg_len);
  360. return NULL;
  361. }
  362. desc = usb_dmac_desc_get(uchan, sg_len, GFP_NOWAIT);
  363. if (!desc)
  364. return NULL;
  365. desc->direction = dir;
  366. desc->sg_len = sg_len;
  367. for_each_sg(sgl, sg, sg_len, i) {
  368. desc->sg[i].mem_addr = sg_dma_address(sg);
  369. desc->sg[i].size = sg_dma_len(sg);
  370. }
  371. return vchan_tx_prep(&uchan->vc, &desc->vd, dma_flags);
  372. }
  373. static int usb_dmac_chan_terminate_all(struct dma_chan *chan)
  374. {
  375. struct usb_dmac_chan *uchan = to_usb_dmac_chan(chan);
  376. struct usb_dmac_desc *desc, *_desc;
  377. unsigned long flags;
  378. LIST_HEAD(head);
  379. LIST_HEAD(list);
  380. spin_lock_irqsave(&uchan->vc.lock, flags);
  381. usb_dmac_chan_halt(uchan);
  382. vchan_get_all_descriptors(&uchan->vc, &head);
  383. if (uchan->desc)
  384. uchan->desc = NULL;
  385. list_splice_init(&uchan->desc_got, &list);
  386. list_for_each_entry_safe(desc, _desc, &list, node)
  387. list_move_tail(&desc->node, &uchan->desc_freed);
  388. spin_unlock_irqrestore(&uchan->vc.lock, flags);
  389. vchan_dma_desc_free_list(&uchan->vc, &head);
  390. return 0;
  391. }
  392. static unsigned int usb_dmac_get_current_residue(struct usb_dmac_chan *chan,
  393. struct usb_dmac_desc *desc,
  394. int sg_index)
  395. {
  396. struct usb_dmac_sg *sg = desc->sg + sg_index;
  397. u32 mem_addr = sg->mem_addr & 0xffffffff;
  398. unsigned int residue = sg->size;
  399. /*
  400. * We cannot use USB_DMATCR to calculate residue because USB_DMATCR
  401. * has unsuited value to calculate.
  402. */
  403. if (desc->direction == DMA_DEV_TO_MEM)
  404. residue -= usb_dmac_chan_read(chan, USB_DMADAR) - mem_addr;
  405. else
  406. residue -= usb_dmac_chan_read(chan, USB_DMASAR) - mem_addr;
  407. return residue;
  408. }
  409. static u32 usb_dmac_chan_get_residue_if_complete(struct usb_dmac_chan *chan,
  410. dma_cookie_t cookie)
  411. {
  412. struct usb_dmac_desc *desc;
  413. u32 residue = 0;
  414. list_for_each_entry_reverse(desc, &chan->desc_freed, node) {
  415. if (desc->done_cookie == cookie) {
  416. residue = desc->residue;
  417. break;
  418. }
  419. }
  420. return residue;
  421. }
  422. static u32 usb_dmac_chan_get_residue(struct usb_dmac_chan *chan,
  423. dma_cookie_t cookie)
  424. {
  425. u32 residue = 0;
  426. struct virt_dma_desc *vd;
  427. struct usb_dmac_desc *desc = chan->desc;
  428. int i;
  429. if (!desc) {
  430. vd = vchan_find_desc(&chan->vc, cookie);
  431. if (!vd)
  432. return 0;
  433. desc = to_usb_dmac_desc(vd);
  434. }
  435. /* Compute the size of all usb_dmac_sg still to be transferred */
  436. for (i = desc->sg_index + 1; i < desc->sg_len; i++)
  437. residue += desc->sg[i].size;
  438. /* Add the residue for the current sg */
  439. residue += usb_dmac_get_current_residue(chan, desc, desc->sg_index);
  440. return residue;
  441. }
  442. static enum dma_status usb_dmac_tx_status(struct dma_chan *chan,
  443. dma_cookie_t cookie,
  444. struct dma_tx_state *txstate)
  445. {
  446. struct usb_dmac_chan *uchan = to_usb_dmac_chan(chan);
  447. enum dma_status status;
  448. unsigned int residue = 0;
  449. unsigned long flags;
  450. status = dma_cookie_status(chan, cookie, txstate);
  451. /* a client driver will get residue after DMA_COMPLETE */
  452. if (!txstate)
  453. return status;
  454. spin_lock_irqsave(&uchan->vc.lock, flags);
  455. if (status == DMA_COMPLETE)
  456. residue = usb_dmac_chan_get_residue_if_complete(uchan, cookie);
  457. else
  458. residue = usb_dmac_chan_get_residue(uchan, cookie);
  459. spin_unlock_irqrestore(&uchan->vc.lock, flags);
  460. dma_set_residue(txstate, residue);
  461. return status;
  462. }
  463. static void usb_dmac_issue_pending(struct dma_chan *chan)
  464. {
  465. struct usb_dmac_chan *uchan = to_usb_dmac_chan(chan);
  466. unsigned long flags;
  467. spin_lock_irqsave(&uchan->vc.lock, flags);
  468. if (vchan_issue_pending(&uchan->vc) && !uchan->desc)
  469. usb_dmac_chan_start_desc(uchan);
  470. spin_unlock_irqrestore(&uchan->vc.lock, flags);
  471. }
  472. static void usb_dmac_virt_desc_free(struct virt_dma_desc *vd)
  473. {
  474. struct usb_dmac_desc *desc = to_usb_dmac_desc(vd);
  475. struct usb_dmac_chan *chan = to_usb_dmac_chan(vd->tx.chan);
  476. usb_dmac_desc_put(chan, desc);
  477. }
  478. /* -----------------------------------------------------------------------------
  479. * IRQ handling
  480. */
  481. static void usb_dmac_isr_transfer_end(struct usb_dmac_chan *chan)
  482. {
  483. struct usb_dmac_desc *desc = chan->desc;
  484. BUG_ON(!desc);
  485. if (++desc->sg_index < desc->sg_len) {
  486. usb_dmac_chan_start_sg(chan, desc->sg_index);
  487. } else {
  488. desc->residue = usb_dmac_get_current_residue(chan, desc,
  489. desc->sg_index - 1);
  490. desc->done_cookie = desc->vd.tx.cookie;
  491. vchan_cookie_complete(&desc->vd);
  492. /* Restart the next transfer if this driver has a next desc */
  493. usb_dmac_chan_start_desc(chan);
  494. }
  495. }
  496. static irqreturn_t usb_dmac_isr_channel(int irq, void *dev)
  497. {
  498. struct usb_dmac_chan *chan = dev;
  499. irqreturn_t ret = IRQ_NONE;
  500. u32 mask = 0;
  501. u32 chcr;
  502. bool xfer_end = false;
  503. spin_lock(&chan->vc.lock);
  504. chcr = usb_dmac_chan_read(chan, USB_DMACHCR);
  505. if (chcr & (USB_DMACHCR_TE | USB_DMACHCR_SP)) {
  506. mask |= USB_DMACHCR_DE | USB_DMACHCR_TE | USB_DMACHCR_SP;
  507. if (chcr & USB_DMACHCR_DE)
  508. xfer_end = true;
  509. ret |= IRQ_HANDLED;
  510. }
  511. if (chcr & USB_DMACHCR_NULL) {
  512. /* An interruption of TE will happen after we set FTE */
  513. mask |= USB_DMACHCR_NULL;
  514. chcr |= USB_DMACHCR_FTE;
  515. ret |= IRQ_HANDLED;
  516. }
  517. if (mask)
  518. usb_dmac_chan_write(chan, USB_DMACHCR, chcr & ~mask);
  519. if (xfer_end)
  520. usb_dmac_isr_transfer_end(chan);
  521. spin_unlock(&chan->vc.lock);
  522. return ret;
  523. }
  524. /* -----------------------------------------------------------------------------
  525. * OF xlate and channel filter
  526. */
  527. static bool usb_dmac_chan_filter(struct dma_chan *chan, void *arg)
  528. {
  529. struct usb_dmac_chan *uchan = to_usb_dmac_chan(chan);
  530. struct of_phandle_args *dma_spec = arg;
  531. if (dma_spec->np != chan->device->dev->of_node)
  532. return false;
  533. /* USB-DMAC should be used with fixed usb controller's FIFO */
  534. if (uchan->index != dma_spec->args[0])
  535. return false;
  536. return true;
  537. }
  538. static struct dma_chan *usb_dmac_of_xlate(struct of_phandle_args *dma_spec,
  539. struct of_dma *ofdma)
  540. {
  541. struct dma_chan *chan;
  542. dma_cap_mask_t mask;
  543. if (dma_spec->args_count != 1)
  544. return NULL;
  545. /* Only slave DMA channels can be allocated via DT */
  546. dma_cap_zero(mask);
  547. dma_cap_set(DMA_SLAVE, mask);
  548. chan = dma_request_channel(mask, usb_dmac_chan_filter, dma_spec);
  549. if (!chan)
  550. return NULL;
  551. return chan;
  552. }
  553. /* -----------------------------------------------------------------------------
  554. * Power management
  555. */
  556. #ifdef CONFIG_PM
  557. static int usb_dmac_runtime_suspend(struct device *dev)
  558. {
  559. struct usb_dmac *dmac = dev_get_drvdata(dev);
  560. int i;
  561. for (i = 0; i < dmac->n_channels; ++i) {
  562. if (!dmac->channels[i].iomem)
  563. break;
  564. usb_dmac_chan_halt(&dmac->channels[i]);
  565. }
  566. return 0;
  567. }
  568. static int usb_dmac_runtime_resume(struct device *dev)
  569. {
  570. struct usb_dmac *dmac = dev_get_drvdata(dev);
  571. return usb_dmac_init(dmac);
  572. }
  573. #endif /* CONFIG_PM */
  574. static const struct dev_pm_ops usb_dmac_pm = {
  575. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  576. pm_runtime_force_resume)
  577. SET_RUNTIME_PM_OPS(usb_dmac_runtime_suspend, usb_dmac_runtime_resume,
  578. NULL)
  579. };
  580. /* -----------------------------------------------------------------------------
  581. * Probe and remove
  582. */
  583. static int usb_dmac_chan_probe(struct usb_dmac *dmac,
  584. struct usb_dmac_chan *uchan,
  585. unsigned int index)
  586. {
  587. struct platform_device *pdev = to_platform_device(dmac->dev);
  588. char pdev_irqname[5];
  589. char *irqname;
  590. int ret;
  591. uchan->index = index;
  592. uchan->iomem = dmac->iomem + USB_DMAC_CHAN_OFFSET(index);
  593. /* Request the channel interrupt. */
  594. sprintf(pdev_irqname, "ch%u", index);
  595. uchan->irq = platform_get_irq_byname(pdev, pdev_irqname);
  596. if (uchan->irq < 0) {
  597. dev_err(dmac->dev, "no IRQ specified for channel %u\n", index);
  598. return -ENODEV;
  599. }
  600. irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u",
  601. dev_name(dmac->dev), index);
  602. if (!irqname)
  603. return -ENOMEM;
  604. ret = devm_request_irq(dmac->dev, uchan->irq, usb_dmac_isr_channel,
  605. IRQF_SHARED, irqname, uchan);
  606. if (ret) {
  607. dev_err(dmac->dev, "failed to request IRQ %u (%d)\n",
  608. uchan->irq, ret);
  609. return ret;
  610. }
  611. uchan->vc.desc_free = usb_dmac_virt_desc_free;
  612. vchan_init(&uchan->vc, &dmac->engine);
  613. INIT_LIST_HEAD(&uchan->desc_freed);
  614. INIT_LIST_HEAD(&uchan->desc_got);
  615. return 0;
  616. }
  617. static int usb_dmac_parse_of(struct device *dev, struct usb_dmac *dmac)
  618. {
  619. struct device_node *np = dev->of_node;
  620. int ret;
  621. ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels);
  622. if (ret < 0) {
  623. dev_err(dev, "unable to read dma-channels property\n");
  624. return ret;
  625. }
  626. if (dmac->n_channels <= 0 || dmac->n_channels >= 100) {
  627. dev_err(dev, "invalid number of channels %u\n",
  628. dmac->n_channels);
  629. return -EINVAL;
  630. }
  631. return 0;
  632. }
  633. static int usb_dmac_probe(struct platform_device *pdev)
  634. {
  635. const enum dma_slave_buswidth widths = USB_DMAC_SLAVE_BUSWIDTH;
  636. struct dma_device *engine;
  637. struct usb_dmac *dmac;
  638. struct resource *mem;
  639. unsigned int i;
  640. int ret;
  641. dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
  642. if (!dmac)
  643. return -ENOMEM;
  644. dmac->dev = &pdev->dev;
  645. platform_set_drvdata(pdev, dmac);
  646. ret = usb_dmac_parse_of(&pdev->dev, dmac);
  647. if (ret < 0)
  648. return ret;
  649. dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels,
  650. sizeof(*dmac->channels), GFP_KERNEL);
  651. if (!dmac->channels)
  652. return -ENOMEM;
  653. /* Request resources. */
  654. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  655. dmac->iomem = devm_ioremap_resource(&pdev->dev, mem);
  656. if (IS_ERR(dmac->iomem))
  657. return PTR_ERR(dmac->iomem);
  658. /* Enable runtime PM and initialize the device. */
  659. pm_runtime_enable(&pdev->dev);
  660. ret = pm_runtime_get_sync(&pdev->dev);
  661. if (ret < 0) {
  662. dev_err(&pdev->dev, "runtime PM get sync failed (%d)\n", ret);
  663. goto error_pm;
  664. }
  665. ret = usb_dmac_init(dmac);
  666. if (ret) {
  667. dev_err(&pdev->dev, "failed to reset device\n");
  668. goto error;
  669. }
  670. /* Initialize the channels. */
  671. INIT_LIST_HEAD(&dmac->engine.channels);
  672. for (i = 0; i < dmac->n_channels; ++i) {
  673. ret = usb_dmac_chan_probe(dmac, &dmac->channels[i], i);
  674. if (ret < 0)
  675. goto error;
  676. }
  677. /* Register the DMAC as a DMA provider for DT. */
  678. ret = of_dma_controller_register(pdev->dev.of_node, usb_dmac_of_xlate,
  679. NULL);
  680. if (ret < 0)
  681. goto error;
  682. /*
  683. * Register the DMA engine device.
  684. *
  685. * Default transfer size of 32 bytes requires 32-byte alignment.
  686. */
  687. engine = &dmac->engine;
  688. dma_cap_set(DMA_SLAVE, engine->cap_mask);
  689. engine->dev = &pdev->dev;
  690. engine->src_addr_widths = widths;
  691. engine->dst_addr_widths = widths;
  692. engine->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  693. engine->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  694. engine->device_alloc_chan_resources = usb_dmac_alloc_chan_resources;
  695. engine->device_free_chan_resources = usb_dmac_free_chan_resources;
  696. engine->device_prep_slave_sg = usb_dmac_prep_slave_sg;
  697. engine->device_terminate_all = usb_dmac_chan_terminate_all;
  698. engine->device_tx_status = usb_dmac_tx_status;
  699. engine->device_issue_pending = usb_dmac_issue_pending;
  700. ret = dma_async_device_register(engine);
  701. if (ret < 0)
  702. goto error;
  703. pm_runtime_put(&pdev->dev);
  704. return 0;
  705. error:
  706. of_dma_controller_free(pdev->dev.of_node);
  707. pm_runtime_put(&pdev->dev);
  708. error_pm:
  709. pm_runtime_disable(&pdev->dev);
  710. return ret;
  711. }
  712. static void usb_dmac_chan_remove(struct usb_dmac *dmac,
  713. struct usb_dmac_chan *uchan)
  714. {
  715. usb_dmac_chan_halt(uchan);
  716. devm_free_irq(dmac->dev, uchan->irq, uchan);
  717. }
  718. static int usb_dmac_remove(struct platform_device *pdev)
  719. {
  720. struct usb_dmac *dmac = platform_get_drvdata(pdev);
  721. int i;
  722. for (i = 0; i < dmac->n_channels; ++i)
  723. usb_dmac_chan_remove(dmac, &dmac->channels[i]);
  724. of_dma_controller_free(pdev->dev.of_node);
  725. dma_async_device_unregister(&dmac->engine);
  726. pm_runtime_disable(&pdev->dev);
  727. return 0;
  728. }
  729. static void usb_dmac_shutdown(struct platform_device *pdev)
  730. {
  731. struct usb_dmac *dmac = platform_get_drvdata(pdev);
  732. usb_dmac_stop(dmac);
  733. }
  734. static const struct of_device_id usb_dmac_of_ids[] = {
  735. { .compatible = "renesas,usb-dmac", },
  736. { /* Sentinel */ }
  737. };
  738. MODULE_DEVICE_TABLE(of, usb_dmac_of_ids);
  739. static struct platform_driver usb_dmac_driver = {
  740. .driver = {
  741. .pm = &usb_dmac_pm,
  742. .name = "usb-dmac",
  743. .of_match_table = usb_dmac_of_ids,
  744. },
  745. .probe = usb_dmac_probe,
  746. .remove = usb_dmac_remove,
  747. .shutdown = usb_dmac_shutdown,
  748. };
  749. module_platform_driver(usb_dmac_driver);
  750. MODULE_DESCRIPTION("Renesas USB DMA Controller Driver");
  751. MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
  752. MODULE_LICENSE("GPL v2");