musbhsdma.c 12 KB

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