blackfin.c 8.6 KB

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