blackfin.c 8.3 KB

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