musbhsdma.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MUSB OTG driver - support for Mentor's DMA controller
  4. *
  5. * Copyright 2005 Mentor Graphics Corporation
  6. * Copyright (C) 2005-2007 by Texas Instruments
  7. */
  8. #include <linux/device.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include "musb_core.h"
  13. #include "musbhsdma.h"
  14. static void dma_channel_release(struct dma_channel *channel);
  15. static void dma_controller_stop(struct musb_dma_controller *controller)
  16. {
  17. struct musb *musb = controller->private_data;
  18. struct dma_channel *channel;
  19. u8 bit;
  20. if (controller->used_channels != 0) {
  21. dev_err(musb->controller,
  22. "Stopping DMA controller while channel active\n");
  23. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  24. if (controller->used_channels & (1 << bit)) {
  25. channel = &controller->channel[bit].channel;
  26. dma_channel_release(channel);
  27. if (!controller->used_channels)
  28. break;
  29. }
  30. }
  31. }
  32. }
  33. static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
  34. struct musb_hw_ep *hw_ep, u8 transmit)
  35. {
  36. struct musb_dma_controller *controller = container_of(c,
  37. struct musb_dma_controller, controller);
  38. struct musb_dma_channel *musb_channel = NULL;
  39. struct dma_channel *channel = NULL;
  40. u8 bit;
  41. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  42. if (!(controller->used_channels & (1 << bit))) {
  43. controller->used_channels |= (1 << bit);
  44. musb_channel = &(controller->channel[bit]);
  45. musb_channel->controller = controller;
  46. musb_channel->idx = bit;
  47. musb_channel->epnum = hw_ep->epnum;
  48. musb_channel->transmit = transmit;
  49. channel = &(musb_channel->channel);
  50. channel->private_data = musb_channel;
  51. channel->status = MUSB_DMA_STATUS_FREE;
  52. channel->max_len = 0x100000;
  53. /* Tx => mode 1; Rx => mode 0 */
  54. channel->desired_mode = transmit;
  55. channel->actual_len = 0;
  56. break;
  57. }
  58. }
  59. return channel;
  60. }
  61. static void dma_channel_release(struct dma_channel *channel)
  62. {
  63. struct musb_dma_channel *musb_channel = channel->private_data;
  64. channel->actual_len = 0;
  65. musb_channel->start_addr = 0;
  66. musb_channel->len = 0;
  67. musb_channel->controller->used_channels &=
  68. ~(1 << musb_channel->idx);
  69. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  70. }
  71. static void configure_channel(struct dma_channel *channel,
  72. u16 packet_sz, u8 mode,
  73. dma_addr_t dma_addr, u32 len)
  74. {
  75. struct musb_dma_channel *musb_channel = channel->private_data;
  76. struct musb_dma_controller *controller = musb_channel->controller;
  77. struct musb *musb = controller->private_data;
  78. void __iomem *mbase = controller->base;
  79. u8 bchannel = musb_channel->idx;
  80. u16 csr = 0;
  81. musb_dbg(musb, "%p, pkt_sz %d, addr %pad, len %d, mode %d",
  82. channel, packet_sz, &dma_addr, len, mode);
  83. if (mode) {
  84. csr |= 1 << MUSB_HSDMA_MODE1_SHIFT;
  85. BUG_ON(len < packet_sz);
  86. }
  87. csr |= MUSB_HSDMA_BURSTMODE_INCR16
  88. << MUSB_HSDMA_BURSTMODE_SHIFT;
  89. csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT)
  90. | (1 << MUSB_HSDMA_ENABLE_SHIFT)
  91. | (1 << MUSB_HSDMA_IRQENABLE_SHIFT)
  92. | (musb_channel->transmit
  93. ? (1 << MUSB_HSDMA_TRANSMIT_SHIFT)
  94. : 0);
  95. /* address/count */
  96. musb_write_hsdma_addr(mbase, bchannel, dma_addr);
  97. musb_write_hsdma_count(mbase, bchannel, len);
  98. /* control (this should start things) */
  99. musb_writew(mbase,
  100. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  101. csr);
  102. }
  103. static int dma_channel_program(struct dma_channel *channel,
  104. u16 packet_sz, u8 mode,
  105. dma_addr_t dma_addr, u32 len)
  106. {
  107. struct musb_dma_channel *musb_channel = channel->private_data;
  108. struct musb_dma_controller *controller = musb_channel->controller;
  109. struct musb *musb = controller->private_data;
  110. musb_dbg(musb, "ep%d-%s pkt_sz %d, dma_addr %pad length %d, mode %d",
  111. musb_channel->epnum,
  112. musb_channel->transmit ? "Tx" : "Rx",
  113. packet_sz, &dma_addr, len, mode);
  114. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  115. channel->status == MUSB_DMA_STATUS_BUSY);
  116. /* Let targets check/tweak the arguments */
  117. if (musb->ops->adjust_channel_params) {
  118. int ret = musb->ops->adjust_channel_params(channel,
  119. packet_sz, &mode, &dma_addr, &len);
  120. if (ret)
  121. return ret;
  122. }
  123. /*
  124. * The DMA engine in RTL1.8 and above cannot handle
  125. * DMA addresses that are not aligned to a 4 byte boundary.
  126. * It ends up masking the last two bits of the address
  127. * programmed in DMA_ADDR.
  128. *
  129. * Fail such DMA transfers, so that the backup PIO mode
  130. * can carry out the transfer
  131. */
  132. if ((musb->hwvers >= MUSB_HWVERS_1800) && (dma_addr % 4))
  133. return false;
  134. channel->actual_len = 0;
  135. musb_channel->start_addr = dma_addr;
  136. musb_channel->len = len;
  137. musb_channel->max_packet_sz = packet_sz;
  138. channel->status = MUSB_DMA_STATUS_BUSY;
  139. configure_channel(channel, packet_sz, mode, dma_addr, len);
  140. return true;
  141. }
  142. static int dma_channel_abort(struct dma_channel *channel)
  143. {
  144. struct musb_dma_channel *musb_channel = channel->private_data;
  145. void __iomem *mbase = musb_channel->controller->base;
  146. struct musb *musb = musb_channel->controller->private_data;
  147. u8 bchannel = musb_channel->idx;
  148. int offset;
  149. u16 csr;
  150. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  151. if (musb_channel->transmit) {
  152. offset = musb->io.ep_offset(musb_channel->epnum,
  153. MUSB_TXCSR);
  154. /*
  155. * The programming guide says that we must clear
  156. * the DMAENAB bit before the DMAMODE bit...
  157. */
  158. csr = musb_readw(mbase, offset);
  159. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  160. musb_writew(mbase, offset, csr);
  161. csr &= ~MUSB_TXCSR_DMAMODE;
  162. musb_writew(mbase, offset, csr);
  163. } else {
  164. offset = musb->io.ep_offset(musb_channel->epnum,
  165. MUSB_RXCSR);
  166. csr = musb_readw(mbase, offset);
  167. csr &= ~(MUSB_RXCSR_AUTOCLEAR |
  168. MUSB_RXCSR_DMAENAB |
  169. MUSB_RXCSR_DMAMODE);
  170. musb_writew(mbase, offset, csr);
  171. }
  172. musb_writew(mbase,
  173. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  174. 0);
  175. musb_write_hsdma_addr(mbase, bchannel, 0);
  176. musb_write_hsdma_count(mbase, bchannel, 0);
  177. channel->status = MUSB_DMA_STATUS_FREE;
  178. }
  179. return 0;
  180. }
  181. static irqreturn_t dma_controller_irq(int irq, void *private_data)
  182. {
  183. struct musb_dma_controller *controller = private_data;
  184. struct musb *musb = controller->private_data;
  185. struct musb_dma_channel *musb_channel;
  186. struct dma_channel *channel;
  187. void __iomem *mbase = controller->base;
  188. irqreturn_t retval = IRQ_NONE;
  189. unsigned long flags;
  190. u8 bchannel;
  191. u8 int_hsdma;
  192. u32 addr, count;
  193. u16 csr;
  194. spin_lock_irqsave(&musb->lock, flags);
  195. int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR);
  196. if (!int_hsdma) {
  197. musb_dbg(musb, "spurious DMA irq");
  198. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  199. musb_channel = (struct musb_dma_channel *)
  200. &(controller->channel[bchannel]);
  201. channel = &musb_channel->channel;
  202. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  203. count = musb_read_hsdma_count(mbase, bchannel);
  204. if (count == 0)
  205. int_hsdma |= (1 << bchannel);
  206. }
  207. }
  208. musb_dbg(musb, "int_hsdma = 0x%x", int_hsdma);
  209. if (!int_hsdma)
  210. goto done;
  211. }
  212. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  213. if (int_hsdma & (1 << bchannel)) {
  214. musb_channel = (struct musb_dma_channel *)
  215. &(controller->channel[bchannel]);
  216. channel = &musb_channel->channel;
  217. csr = musb_readw(mbase,
  218. MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
  219. MUSB_HSDMA_CONTROL));
  220. if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
  221. musb_channel->channel.status =
  222. MUSB_DMA_STATUS_BUS_ABORT;
  223. } else {
  224. u8 devctl;
  225. addr = musb_read_hsdma_addr(mbase,
  226. bchannel);
  227. channel->actual_len = addr
  228. - musb_channel->start_addr;
  229. musb_dbg(musb, "ch %p, 0x%x -> 0x%x (%zu / %d) %s",
  230. channel, musb_channel->start_addr,
  231. addr, channel->actual_len,
  232. musb_channel->len,
  233. (channel->actual_len
  234. < musb_channel->len) ?
  235. "=> reconfig 0" : "=> complete");
  236. devctl = musb_readb(mbase, MUSB_DEVCTL);
  237. channel->status = MUSB_DMA_STATUS_FREE;
  238. /* completed */
  239. if ((devctl & MUSB_DEVCTL_HM)
  240. && (musb_channel->transmit)
  241. && ((channel->desired_mode == 0)
  242. || (channel->actual_len &
  243. (musb_channel->max_packet_sz - 1)))
  244. ) {
  245. u8 epnum = musb_channel->epnum;
  246. int offset = musb->io.ep_offset(epnum,
  247. MUSB_TXCSR);
  248. u16 txcsr;
  249. /*
  250. * The programming guide says that we
  251. * must clear DMAENAB before DMAMODE.
  252. */
  253. musb_ep_select(mbase, epnum);
  254. txcsr = musb_readw(mbase, offset);
  255. txcsr &= ~(MUSB_TXCSR_DMAENAB
  256. | MUSB_TXCSR_AUTOSET);
  257. musb_writew(mbase, offset, txcsr);
  258. /* Send out the packet */
  259. txcsr &= ~MUSB_TXCSR_DMAMODE;
  260. txcsr |= MUSB_TXCSR_TXPKTRDY;
  261. musb_writew(mbase, offset, txcsr);
  262. }
  263. musb_dma_completion(musb, musb_channel->epnum,
  264. musb_channel->transmit);
  265. }
  266. }
  267. }
  268. retval = IRQ_HANDLED;
  269. done:
  270. spin_unlock_irqrestore(&musb->lock, flags);
  271. return retval;
  272. }
  273. void musbhs_dma_controller_destroy(struct dma_controller *c)
  274. {
  275. struct musb_dma_controller *controller = container_of(c,
  276. struct musb_dma_controller, controller);
  277. dma_controller_stop(controller);
  278. if (controller->irq)
  279. free_irq(controller->irq, c);
  280. kfree(controller);
  281. }
  282. EXPORT_SYMBOL_GPL(musbhs_dma_controller_destroy);
  283. struct dma_controller *musbhs_dma_controller_create(struct musb *musb,
  284. void __iomem *base)
  285. {
  286. struct musb_dma_controller *controller;
  287. struct device *dev = musb->controller;
  288. struct platform_device *pdev = to_platform_device(dev);
  289. int irq = platform_get_irq_byname(pdev, "dma");
  290. if (irq <= 0) {
  291. dev_err(dev, "No DMA interrupt line!\n");
  292. return NULL;
  293. }
  294. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  295. if (!controller)
  296. return NULL;
  297. controller->channel_count = MUSB_HSDMA_CHANNELS;
  298. controller->private_data = musb;
  299. controller->base = base;
  300. controller->controller.channel_alloc = dma_channel_allocate;
  301. controller->controller.channel_release = dma_channel_release;
  302. controller->controller.channel_program = dma_channel_program;
  303. controller->controller.channel_abort = dma_channel_abort;
  304. if (request_irq(irq, dma_controller_irq, 0,
  305. dev_name(musb->controller), &controller->controller)) {
  306. dev_err(dev, "request_irq %d failed!\n", irq);
  307. musb_dma_controller_destroy(&controller->controller);
  308. return NULL;
  309. }
  310. controller->irq = irq;
  311. return &controller->controller;
  312. }
  313. EXPORT_SYMBOL_GPL(musbhs_dma_controller_create);