musbfsh_hsdma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * MUSB OTG driver - support for Mentor's DMA controller
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2007 by Texas Instruments
  6. *
  7. * Copyright 2015 Mediatek Inc.
  8. * Marvin Lin <marvin.lin@mediatek.com>
  9. * Arvin Wang <arvin.wang@mediatek.com>
  10. * Vincent Fan <vincent.fan@mediatek.com>
  11. * Bryant Lu <bryant.lu@mediatek.com>
  12. * Yu-Chang Wang <yu-chang.wang@mediatek.com>
  13. * Macpaul Lin <macpaul.lin@mediatek.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program.
  26. *
  27. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  30. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  32. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  33. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  34. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  36. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. */
  39. #include <linux/device.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/slab.h>
  43. #include "musbfsh_core.h"
  44. #include "musbfsh_host.h"
  45. #include "musbfsh_dma.h"
  46. #include "musbfsh_hsdma.h"
  47. #include "usb.h"
  48. #ifdef CONFIG_MTK_ICUSB_SUPPORT
  49. #include "musbfsh_icusb.h"
  50. #endif
  51. static int dma_controller_start(struct dma_controller *c)
  52. {
  53. INFO("++\n");
  54. /* nothing to do */
  55. return 0;
  56. }
  57. static void dma_channel_release(struct dma_channel *channel);
  58. static int dma_controller_stop(struct dma_controller *c)
  59. {
  60. struct musbfsh_dma_controller *controller =
  61. container_of(c, struct musbfsh_dma_controller, controller);
  62. struct musbfsh *musbfsh = controller->private_data;
  63. struct dma_channel *channel;
  64. u8 bit;
  65. INFO("++\n");
  66. if (controller->used_channels != 0) {
  67. dev_err(musbfsh->controller,
  68. "Stopping DMA controller while channel active\n");
  69. for (bit = 0; bit < MUSBFSH_HSDMA_CHANNELS; bit++) {
  70. if (controller->used_channels & (1 << bit)) {
  71. channel = &controller->channel[bit].channel;
  72. dma_channel_release(channel);
  73. if (!controller->used_channels)
  74. break;
  75. }
  76. }
  77. }
  78. return 0;
  79. }
  80. static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
  81. struct musbfsh_hw_ep *hw_ep,
  82. u8 transmit)
  83. {
  84. struct musbfsh_dma_controller *controller =
  85. container_of(c, struct musbfsh_dma_controller, controller);
  86. struct musbfsh_dma_channel *musbfsh_channel = NULL;
  87. struct dma_channel *channel = NULL;
  88. u8 bit;
  89. INFO("epnum=%d\n", hw_ep->epnum);
  90. for (bit = 0; bit < MUSBFSH_HSDMA_CHANNELS; bit++) {
  91. if (!(controller->used_channels & (1 << bit))) {
  92. controller->used_channels |= (1 << bit);
  93. musbfsh_channel = &(controller->channel[bit]);
  94. musbfsh_channel->controller = controller;
  95. musbfsh_channel->idx = bit;
  96. musbfsh_channel->epnum = hw_ep->epnum;
  97. musbfsh_channel->transmit = transmit;
  98. channel = &(musbfsh_channel->channel);
  99. channel->private_data = musbfsh_channel;
  100. channel->status = MUSBFSH_DMA_STATUS_FREE;
  101. channel->max_len = 0x10000;
  102. /* Tx => mode 1; Rx => mode 0 */
  103. channel->desired_mode = transmit;
  104. /* wz:set Tx and Rx to mode 0 */
  105. /* channel->desired_mode = 0; */
  106. channel->actual_len = 0;
  107. break;
  108. }
  109. }
  110. if (musbfsh_channel)
  111. INFO("idx=%d\n", musbfsh_channel->idx);
  112. return channel;
  113. }
  114. static void dma_channel_release(struct dma_channel *channel)
  115. {
  116. struct musbfsh_dma_channel *musbfsh_channel = channel->private_data;
  117. INFO("idx=%d\n", musbfsh_channel->idx);
  118. channel->actual_len = 0;
  119. musbfsh_channel->start_addr = 0;
  120. musbfsh_channel->len = 0;
  121. musbfsh_channel->controller->used_channels &=
  122. ~(1 << musbfsh_channel->idx);
  123. channel->status = MUSBFSH_DMA_STATUS_UNKNOWN;
  124. }
  125. static void configure_channel(struct dma_channel *channel,
  126. u16 packet_sz, u8 mode, dma_addr_t dma_addr,
  127. u32 len)
  128. {
  129. struct musbfsh_dma_channel *musbfsh_channel = channel->private_data;
  130. struct musbfsh_dma_controller *controller = musbfsh_channel->controller;
  131. /* struct musbfs *musb = controller->private_data; */
  132. void __iomem *mbase = controller->base;
  133. u8 bchannel = musbfsh_channel->idx;
  134. u16 csr = 0;
  135. INFO("idx=%d\n", musbfsh_channel->idx);
  136. INFO("%p, pkt_sz %d, addr 0x%x, len %d, mode %d\n",
  137. channel, packet_sz, (unsigned int)dma_addr, len, mode);
  138. if (mode) { /* mode 1,multi-packet */
  139. csr |= 1 << MUSBFSH_HSDMA_MODE1_SHIFT;
  140. BUG_ON(len < packet_sz);
  141. }
  142. csr |= MUSBFSH_HSDMA_BURSTMODE_INCR16 << MUSBFSH_HSDMA_BURSTMODE_SHIFT;
  143. csr |= (musbfsh_channel->epnum << MUSBFSH_HSDMA_ENDPOINT_SHIFT)
  144. | (1 << MUSBFSH_HSDMA_ENABLE_SHIFT)
  145. | (1 << MUSBFSH_HSDMA_IRQENABLE_SHIFT)
  146. | (musbfsh_channel->transmit ? (1 << MUSBFSH_HSDMA_TRANSMIT_SHIFT)
  147. : 0);
  148. /* address/count */
  149. musbfsh_write_hsdma_addr(mbase, bchannel, dma_addr);
  150. musbfsh_write_hsdma_count(mbase, bchannel, len);
  151. /* control (this should start things) */
  152. musbfsh_writew(mbase, MUSBFSH_HSDMA_CHANNEL_OFFSET(bchannel,
  153. MUSBFSH_HSDMA_CONTROL),
  154. csr);
  155. }
  156. static int dma_channel_program(struct dma_channel *channel,
  157. u16 packet_sz, u8 mode, dma_addr_t dma_addr,
  158. u32 len)
  159. {
  160. struct musbfsh_dma_channel *musbfsh_channel = channel->private_data;
  161. /* struct musbfsh_dma_controller *controller =
  162. * musbfsh_channel->controller;
  163. */
  164. /* struct musfsh *musbfsh = controller->private_data; */
  165. INFO("ep%d-%s pkt_sz %d, dma_addr 0x%x length %d, mode %d\n",
  166. musbfsh_channel->epnum,
  167. musbfsh_channel->transmit ? "Tx" : "Rx", packet_sz,
  168. (unsigned int)dma_addr, len, mode);
  169. BUG_ON(channel->status == MUSBFSH_DMA_STATUS_UNKNOWN ||
  170. channel->status == MUSBFSH_DMA_STATUS_BUSY);
  171. channel->actual_len = 0;
  172. musbfsh_channel->start_addr = dma_addr;
  173. musbfsh_channel->len = len;
  174. musbfsh_channel->max_packet_sz = packet_sz;
  175. channel->status = MUSBFSH_DMA_STATUS_BUSY;
  176. configure_channel(channel, packet_sz, mode, dma_addr, len);
  177. return true;
  178. }
  179. static int dma_channel_abort(struct dma_channel *channel)
  180. {
  181. struct musbfsh_dma_channel *musbfsh_channel = channel->private_data;
  182. void __iomem *mbase = musbfsh_channel->controller->base;
  183. u8 bchannel = musbfsh_channel->idx;
  184. int offset;
  185. u16 csr;
  186. INFO("%s, idx=%d\r\n", __func__, musbfsh_channel->idx);
  187. if (channel->status == MUSBFSH_DMA_STATUS_BUSY) {
  188. if (musbfsh_channel->transmit) {
  189. offset = MUSBFSH_EP_OFFSET(musbfsh_channel->epnum,
  190. MUSBFSH_TXCSR);
  191. /*
  192. * The programming guide says that we must clear
  193. * the DMAENA bit before the DMAMODE bit...
  194. */
  195. csr = musbfsh_readw(mbase, offset);
  196. csr &= ~(MUSBFSH_TXCSR_AUTOSET | MUSBFSH_TXCSR_DMAENAB);
  197. musbfsh_writew(mbase, offset, csr);
  198. csr &= ~MUSBFSH_TXCSR_DMAMODE;
  199. musbfsh_writew(mbase, offset, csr);
  200. } else {
  201. offset = MUSBFSH_EP_OFFSET(musbfsh_channel->epnum,
  202. MUSBFSH_RXCSR);
  203. csr = musbfsh_readw(mbase, offset);
  204. csr &= ~(MUSBFSH_RXCSR_AUTOCLEAR |
  205. MUSBFSH_RXCSR_DMAENAB | MUSBFSH_RXCSR_DMAMODE);
  206. musbfsh_writew(mbase, offset, csr);
  207. }
  208. musbfsh_writew(mbase,
  209. MUSBFSH_HSDMA_CHANNEL_OFFSET(bchannel,
  210. MUSBFSH_HSDMA_CONTROL),
  211. 0);
  212. musbfsh_write_hsdma_addr(mbase, bchannel, 0);
  213. musbfsh_write_hsdma_count(mbase, bchannel, 0);
  214. channel->status = MUSBFSH_DMA_STATUS_FREE;
  215. }
  216. return 0;
  217. }
  218. irqreturn_t musbfsh_dma_controller_irq(int irq, void *private_data)
  219. {
  220. struct musbfsh_dma_controller *controller = private_data;
  221. struct musbfsh *musbfsh = controller->private_data;
  222. struct musbfsh_dma_channel *musbfsh_chan; /* musbfsh_channel */
  223. struct dma_channel *channel;
  224. void __iomem *mbase = controller->base;
  225. irqreturn_t retval = IRQ_NONE;
  226. /* unsigned long flags; */
  227. u8 bchanl; /* channel */
  228. u8 int_hsdma;
  229. u32 addr, count;
  230. u16 csr;
  231. INFO("++\n");
  232. /*
  233. * This function is called inside generic_interrupt
  234. * We don't need spin_lock_irqsave(&musbfsh->lock, flags) here
  235. */
  236. int_hsdma = musbfsh->int_dma;
  237. /* should not to run here! */
  238. if (!int_hsdma) {
  239. WARNING("spurious DMA irq\n");
  240. for (bchanl = 0; bchanl < MUSBFSH_HSDMA_CHANNELS; bchanl++) {
  241. musbfsh_chan = (struct musbfsh_dma_channel *)
  242. &(controller->channel[bchanl]);
  243. channel = &musbfsh_chan->channel;
  244. if (channel->status == MUSBFSH_DMA_STATUS_BUSY) {
  245. count = musbfsh_read_hsdma_count(mbase, bchanl);
  246. /*
  247. * All of the data have been transferred,
  248. * should notify the CPU to process.
  249. */
  250. if (count == 0)
  251. int_hsdma |= (1 << bchanl);
  252. }
  253. }
  254. INFO("int_hsdma = 0x%x\n", int_hsdma);
  255. if (!int_hsdma)
  256. goto done;
  257. }
  258. for (bchanl = 0; bchanl < MUSBFSH_HSDMA_CHANNELS; bchanl++) {
  259. if (int_hsdma & (1 << bchanl)) {
  260. musbfsh_chan = (struct musbfsh_dma_channel *)
  261. &(controller->channel[bchanl]);
  262. channel = &musbfsh_chan->channel;
  263. csr = musbfsh_readw(mbase,
  264. MUSBFSH_HSDMA_CHANNEL_OFFSET(bchanl,
  265. MUSBFSH_HSDMA_CONTROL));
  266. if (csr & (1 << MUSBFSH_HSDMA_BUSERROR_SHIFT)) {
  267. musbfsh_chan->channel.status =
  268. MUSBFSH_DMA_STATUS_BUS_ABORT;
  269. } else {
  270. u8 devctl;
  271. /*
  272. * the register of address will increase with
  273. * the data transfer.
  274. */
  275. addr = musbfsh_read_hsdma_addr(mbase, bchanl);
  276. channel->actual_len =
  277. addr - musbfsh_chan->start_addr;
  278. INFO("ch %p, 0x%x -> 0x%x (%zu / %d) %s\n",
  279. channel, musbfsh_chan->start_addr,
  280. addr, channel->actual_len,
  281. musbfsh_chan->len,
  282. (channel->actual_len < musbfsh_chan->len) ?
  283. "=> reconfig 0" : "=> complete");
  284. devctl = musbfsh_readb(mbase, MUSBFSH_DEVCTL);
  285. channel->status = MUSBFSH_DMA_STATUS_FREE;
  286. /* completed */
  287. if ((devctl & MUSBFSH_DEVCTL_HM) &&
  288. (musbfsh_chan->transmit) && /* Tx */
  289. ((channel->desired_mode == 0) ||
  290. (channel->actual_len & /* short pkt */
  291. (musbfsh_chan->max_packet_sz - 1)))
  292. ) {
  293. u8 epnum = musbfsh_chan->epnum;
  294. int offset =
  295. MUSBFSH_EP_OFFSET(epnum,
  296. MUSBFSH_TXCSR);
  297. u16 txcsr;
  298. /*
  299. * The programming guide says that we
  300. * must clear DMAENAB before DMAMODE.
  301. */
  302. musbfsh_ep_select(mbase, epnum);
  303. txcsr = musbfsh_readw(mbase, offset);
  304. txcsr &= ~(MUSBFSH_TXCSR_DMAENAB |
  305. MUSBFSH_TXCSR_AUTOSET);
  306. musbfsh_writew(mbase, offset, txcsr);
  307. /* Send out the packet */
  308. txcsr &= ~MUSBFSH_TXCSR_DMAMODE;
  309. /*
  310. * the packet has been in the fifo,
  311. * only need to set TxPktRdy
  312. **/
  313. txcsr |= MUSBFSH_TXCSR_TXPKTRDY;
  314. musbfsh_writew(mbase, offset, txcsr);
  315. }
  316. musbfsh_dma_completion(musbfsh,
  317. musbfsh_chan->epnum,
  318. musbfsh_chan->transmit);
  319. }
  320. }
  321. }
  322. retval = IRQ_HANDLED;
  323. done:
  324. /* spin_unlock_irqrestore(&musbfsh->lock, flags); */
  325. return retval;
  326. }
  327. void musbfsh_dma_controller_destroy(struct dma_controller *c)
  328. {
  329. struct musbfsh_dma_controller *controller =
  330. container_of(c, struct musbfsh_dma_controller, controller);
  331. INFO("++\n");
  332. if (!controller)
  333. return;
  334. if (controller->irq)
  335. free_irq(controller->irq, c);
  336. kfree(controller);
  337. }
  338. struct dma_controller *__init
  339. musbfsh_dma_controller_create(struct musbfsh *musbfsh, void __iomem *base)
  340. {
  341. struct musbfsh_dma_controller *controller;
  342. INFO("++\n");
  343. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  344. if (!controller)
  345. return NULL;
  346. controller->channel_count = MUSBFSH_HSDMA_CHANNELS;
  347. controller->private_data = musbfsh;
  348. controller->base = base;
  349. controller->controller.start = dma_controller_start;
  350. controller->controller.stop = dma_controller_stop;
  351. controller->controller.channel_alloc = dma_channel_allocate;
  352. controller->controller.channel_release = dma_channel_release;
  353. controller->controller.channel_program = dma_channel_program;
  354. controller->controller.channel_abort = dma_channel_abort;
  355. controller->irq = 0;
  356. musbfsh->musbfsh_dma_controller = controller;
  357. /* enable DMA interrupt for all channels */
  358. #ifdef CONFIG_MTK_ICUSB_SUPPORT
  359. if (skip_mac_init_attr.value)
  360. MYDBG("");
  361. else
  362. musbfsh_writeb(base, MUSBFSH_HSDMA_DMA_INTR_UNMASK_SET, 0xff);
  363. #else
  364. musbfsh_writeb(base, MUSBFSH_HSDMA_DMA_INTR_UNMASK_SET, 0xff);
  365. #endif
  366. return &controller->controller;
  367. }