blackfin.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * MUSB OTG controller driver for Blackfin Processors
  3. *
  4. * Copyright 2006-2008 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/list.h>
  16. #include <linux/gpio.h>
  17. #include <linux/io.h>
  18. #include <asm/cacheflush.h>
  19. #include "musb_core.h"
  20. #include "blackfin.h"
  21. /*
  22. * Load an endpoint's FIFO
  23. */
  24. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
  25. {
  26. void __iomem *fifo = hw_ep->fifo;
  27. void __iomem *epio = hw_ep->regs;
  28. prefetch((u8 *)src);
  29. musb_writew(epio, MUSB_TXCOUNT, len);
  30. DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
  31. hw_ep->epnum, fifo, len, src, epio);
  32. dump_fifo_data(src, len);
  33. if (unlikely((unsigned long)src & 0x01))
  34. outsw_8((unsigned long)fifo, src,
  35. len & 0x01 ? (len >> 1) + 1 : len >> 1);
  36. else
  37. outsw((unsigned long)fifo, src,
  38. len & 0x01 ? (len >> 1) + 1 : len >> 1);
  39. }
  40. /*
  41. * Unload an endpoint's FIFO
  42. */
  43. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  44. {
  45. void __iomem *fifo = hw_ep->fifo;
  46. #ifdef CONFIG_BF52x
  47. u8 epnum = hw_ep->epnum;
  48. u16 dma_reg = 0;
  49. invalidate_dcache_range((unsigned int)dst,
  50. (unsigned int)(dst + len));
  51. /* Setup DMA address register */
  52. dma_reg = (u16) ((u32) dst & 0xFFFF);
  53. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
  54. SSYNC();
  55. dma_reg = (u16) (((u32) dst >> 16) & 0xFFFF);
  56. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
  57. SSYNC();
  58. /* Setup DMA count register */
  59. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
  60. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
  61. SSYNC();
  62. /* Enable the DMA */
  63. dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
  64. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
  65. SSYNC();
  66. /* Wait for compelete */
  67. while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
  68. cpu_relax();
  69. /* acknowledge dma interrupt */
  70. bfin_write_USB_DMA_INTERRUPT(1 << epnum);
  71. SSYNC();
  72. /* Reset DMA */
  73. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
  74. SSYNC();
  75. #else
  76. if (unlikely((unsigned long)dst & 0x01))
  77. insw_8((unsigned long)fifo, dst,
  78. len & 0x01 ? (len >> 1) + 1 : len >> 1);
  79. else
  80. insw((unsigned long)fifo, dst,
  81. len & 0x01 ? (len >> 1) + 1 : len >> 1);
  82. #endif
  83. DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
  84. 'R', hw_ep->epnum, fifo, len, dst);
  85. dump_fifo_data(dst, len);
  86. }
  87. static irqreturn_t blackfin_interrupt(int irq, void *__hci)
  88. {
  89. unsigned long flags;
  90. irqreturn_t retval = IRQ_NONE;
  91. struct musb *musb = __hci;
  92. spin_lock_irqsave(&musb->lock, flags);
  93. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  94. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  95. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  96. if (musb->int_usb || musb->int_tx || musb->int_rx) {
  97. musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
  98. musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
  99. musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
  100. retval = musb_interrupt(musb);
  101. }
  102. spin_unlock_irqrestore(&musb->lock, flags);
  103. /* REVISIT we sometimes get spurious IRQs on g_ep0
  104. * not clear why... fall in BF54x too.
  105. */
  106. if (retval != IRQ_HANDLED)
  107. DBG(5, "spurious?\n");
  108. return IRQ_HANDLED;
  109. }
  110. static void musb_conn_timer_handler(unsigned long _musb)
  111. {
  112. struct musb *musb = (void *)_musb;
  113. unsigned long flags;
  114. u16 val;
  115. spin_lock_irqsave(&musb->lock, flags);
  116. switch (musb->xceiv->state) {
  117. case OTG_STATE_A_IDLE:
  118. case OTG_STATE_A_WAIT_BCON:
  119. /* Start a new session */
  120. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  121. val |= MUSB_DEVCTL_SESSION;
  122. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  123. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  124. if (!(val & MUSB_DEVCTL_BDEVICE)) {
  125. gpio_set_value(musb->config->gpio_vrsel, 1);
  126. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  127. } else {
  128. gpio_set_value(musb->config->gpio_vrsel, 0);
  129. /* Ignore VBUSERROR and SUSPEND IRQ */
  130. val = musb_readb(musb->mregs, MUSB_INTRUSBE);
  131. val &= ~MUSB_INTR_VBUSERROR;
  132. musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
  133. val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
  134. musb_writeb(musb->mregs, MUSB_INTRUSB, val);
  135. val = MUSB_POWER_HSENAB;
  136. musb_writeb(musb->mregs, MUSB_POWER, val);
  137. }
  138. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  139. break;
  140. default:
  141. DBG(1, "%s state not handled\n", otg_state_string(musb));
  142. break;
  143. }
  144. spin_unlock_irqrestore(&musb->lock, flags);
  145. DBG(4, "state is %s\n", otg_state_string(musb));
  146. }
  147. void musb_platform_enable(struct musb *musb)
  148. {
  149. if (is_host_enabled(musb)) {
  150. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  151. musb->a_wait_bcon = TIMER_DELAY;
  152. }
  153. }
  154. void musb_platform_disable(struct musb *musb)
  155. {
  156. }
  157. static void bfin_vbus_power(struct musb *musb, int is_on, int sleeping)
  158. {
  159. }
  160. static void bfin_set_vbus(struct musb *musb, int is_on)
  161. {
  162. if (is_on)
  163. gpio_set_value(musb->config->gpio_vrsel, 1);
  164. else
  165. gpio_set_value(musb->config->gpio_vrsel, 0);
  166. DBG(1, "VBUS %s, devctl %02x "
  167. /* otg %3x conf %08x prcm %08x */ "\n",
  168. otg_state_string(musb),
  169. musb_readb(musb->mregs, MUSB_DEVCTL));
  170. }
  171. static int bfin_set_power(struct otg_transceiver *x, unsigned mA)
  172. {
  173. return 0;
  174. }
  175. void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
  176. {
  177. if (is_host_enabled(musb))
  178. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  179. }
  180. int musb_platform_get_vbus_status(struct musb *musb)
  181. {
  182. return 0;
  183. }
  184. int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
  185. {
  186. return -EIO;
  187. }
  188. int __init musb_platform_init(struct musb *musb)
  189. {
  190. /*
  191. * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
  192. * and OTG HOST modes, while rev 1.1 and greater require PE7 to
  193. * be low for DEVICE mode and high for HOST mode. We set it high
  194. * here because we are in host mode
  195. */
  196. if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
  197. printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d \n",
  198. musb->config->gpio_vrsel);
  199. return -ENODEV;
  200. }
  201. gpio_direction_output(musb->config->gpio_vrsel, 0);
  202. usb_nop_xceiv_register();
  203. musb->xceiv = otg_get_transceiver();
  204. if (!musb->xceiv)
  205. return -ENODEV;
  206. if (ANOMALY_05000346) {
  207. bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
  208. SSYNC();
  209. }
  210. if (ANOMALY_05000347) {
  211. bfin_write_USB_APHY_CNTRL(0x0);
  212. SSYNC();
  213. }
  214. /* Configure PLL oscillator register */
  215. bfin_write_USB_PLLOSC_CTRL(0x30a8);
  216. SSYNC();
  217. bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
  218. SSYNC();
  219. bfin_write_USB_EP_NI0_RXMAXP(64);
  220. SSYNC();
  221. bfin_write_USB_EP_NI0_TXMAXP(64);
  222. SSYNC();
  223. /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
  224. bfin_write_USB_GLOBINTR(0x7);
  225. SSYNC();
  226. bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
  227. EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
  228. EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
  229. EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
  230. EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
  231. SSYNC();
  232. if (is_host_enabled(musb)) {
  233. musb->board_set_vbus = bfin_set_vbus;
  234. setup_timer(&musb_conn_timer,
  235. musb_conn_timer_handler, (unsigned long) musb);
  236. }
  237. if (is_peripheral_enabled(musb))
  238. musb->xceiv->set_power = bfin_set_power;
  239. musb->isr = blackfin_interrupt;
  240. return 0;
  241. }
  242. int musb_platform_suspend(struct musb *musb)
  243. {
  244. return 0;
  245. }
  246. int musb_platform_resume(struct musb *musb)
  247. {
  248. return 0;
  249. }
  250. int musb_platform_exit(struct musb *musb)
  251. {
  252. bfin_vbus_power(musb, 0 /*off*/, 1);
  253. gpio_free(musb->config->gpio_vrsel);
  254. musb_platform_suspend(musb);
  255. return 0;
  256. }