blackfin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /* Start sampling ID pin, when plug is removed from MUSB */
  140. if ((is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
  141. || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) ||
  142. (musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) {
  143. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  144. musb->a_wait_bcon = TIMER_DELAY;
  145. }
  146. spin_unlock_irqrestore(&musb->lock, flags);
  147. return retval;
  148. }
  149. static void musb_conn_timer_handler(unsigned long _musb)
  150. {
  151. struct musb *musb = (void *)_musb;
  152. unsigned long flags;
  153. u16 val;
  154. static u8 toggle;
  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_DEVCTL_SESSION;
  164. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  165. /* Check if musb is host or peripheral. */
  166. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  167. if (!(val & MUSB_DEVCTL_BDEVICE)) {
  168. gpio_set_value(musb->config->gpio_vrsel, 1);
  169. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  170. } else {
  171. gpio_set_value(musb->config->gpio_vrsel, 0);
  172. /* Ignore VBUSERROR and SUSPEND IRQ */
  173. val = musb_readb(musb->mregs, MUSB_INTRUSBE);
  174. val &= ~MUSB_INTR_VBUSERROR;
  175. musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
  176. val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
  177. musb_writeb(musb->mregs, MUSB_INTRUSB, val);
  178. if (is_otg_enabled(musb))
  179. musb->xceiv->state = OTG_STATE_B_IDLE;
  180. else
  181. musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
  182. }
  183. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  184. break;
  185. case OTG_STATE_B_IDLE:
  186. if (!is_peripheral_enabled(musb))
  187. break;
  188. /* Start a new session. It seems that MUSB needs taking
  189. * some time to recognize the type of the plug inserted?
  190. */
  191. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  192. val |= MUSB_DEVCTL_SESSION;
  193. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  194. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  195. if (!(val & MUSB_DEVCTL_BDEVICE)) {
  196. gpio_set_value(musb->config->gpio_vrsel, 1);
  197. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  198. } else {
  199. gpio_set_value(musb->config->gpio_vrsel, 0);
  200. /* Ignore VBUSERROR and SUSPEND IRQ */
  201. val = musb_readb(musb->mregs, MUSB_INTRUSBE);
  202. val &= ~MUSB_INTR_VBUSERROR;
  203. musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
  204. val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
  205. musb_writeb(musb->mregs, MUSB_INTRUSB, val);
  206. /* Toggle the Soft Conn bit, so that we can response to
  207. * the inserting of either A-plug or B-plug.
  208. */
  209. if (toggle) {
  210. val = musb_readb(musb->mregs, MUSB_POWER);
  211. val &= ~MUSB_POWER_SOFTCONN;
  212. musb_writeb(musb->mregs, MUSB_POWER, val);
  213. toggle = 0;
  214. } else {
  215. val = musb_readb(musb->mregs, MUSB_POWER);
  216. val |= MUSB_POWER_SOFTCONN;
  217. musb_writeb(musb->mregs, MUSB_POWER, val);
  218. toggle = 1;
  219. }
  220. /* The delay time is set to 1/4 second by default,
  221. * shortening it, if accelerating A-plug detection
  222. * is needed in OTG mode.
  223. */
  224. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
  225. }
  226. break;
  227. default:
  228. DBG(1, "%s state not handled\n", otg_state_string(musb));
  229. break;
  230. }
  231. spin_unlock_irqrestore(&musb->lock, flags);
  232. DBG(4, "state is %s\n", otg_state_string(musb));
  233. }
  234. void musb_platform_enable(struct musb *musb)
  235. {
  236. if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
  237. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  238. musb->a_wait_bcon = TIMER_DELAY;
  239. }
  240. }
  241. void musb_platform_disable(struct musb *musb)
  242. {
  243. }
  244. static void bfin_set_vbus(struct musb *musb, int is_on)
  245. {
  246. int value = musb->config->gpio_vrsel_active;
  247. if (!is_on)
  248. value = !value;
  249. gpio_set_value(musb->config->gpio_vrsel, value);
  250. DBG(1, "VBUS %s, devctl %02x "
  251. /* otg %3x conf %08x prcm %08x */ "\n",
  252. otg_state_string(musb),
  253. musb_readb(musb->mregs, MUSB_DEVCTL));
  254. }
  255. static int bfin_set_power(struct otg_transceiver *x, unsigned mA)
  256. {
  257. return 0;
  258. }
  259. void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
  260. {
  261. if (!is_otg_enabled(musb) && is_host_enabled(musb))
  262. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  263. }
  264. int musb_platform_get_vbus_status(struct musb *musb)
  265. {
  266. return 0;
  267. }
  268. int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
  269. {
  270. return -EIO;
  271. }
  272. static void musb_platform_reg_init(struct musb *musb)
  273. {
  274. if (ANOMALY_05000346) {
  275. bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
  276. SSYNC();
  277. }
  278. if (ANOMALY_05000347) {
  279. bfin_write_USB_APHY_CNTRL(0x0);
  280. SSYNC();
  281. }
  282. /* Configure PLL oscillator register */
  283. bfin_write_USB_PLLOSC_CTRL(0x3080 |
  284. ((480/musb->config->clkin) << 1));
  285. SSYNC();
  286. bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
  287. SSYNC();
  288. bfin_write_USB_EP_NI0_RXMAXP(64);
  289. SSYNC();
  290. bfin_write_USB_EP_NI0_TXMAXP(64);
  291. SSYNC();
  292. /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
  293. bfin_write_USB_GLOBINTR(0x7);
  294. SSYNC();
  295. bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
  296. EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
  297. EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
  298. EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
  299. EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
  300. SSYNC();
  301. }
  302. int __init musb_platform_init(struct musb *musb, void *board_data)
  303. {
  304. /*
  305. * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
  306. * and OTG HOST modes, while rev 1.1 and greater require PE7 to
  307. * be low for DEVICE mode and high for HOST mode. We set it high
  308. * here because we are in host mode
  309. */
  310. if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
  311. printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
  312. musb->config->gpio_vrsel);
  313. return -ENODEV;
  314. }
  315. gpio_direction_output(musb->config->gpio_vrsel, 0);
  316. usb_nop_xceiv_register();
  317. musb->xceiv = otg_get_transceiver();
  318. if (!musb->xceiv) {
  319. gpio_free(musb->config->gpio_vrsel);
  320. return -ENODEV;
  321. }
  322. musb_platform_reg_init(musb);
  323. if (is_host_enabled(musb)) {
  324. musb->board_set_vbus = bfin_set_vbus;
  325. setup_timer(&musb_conn_timer,
  326. musb_conn_timer_handler, (unsigned long) musb);
  327. }
  328. if (is_peripheral_enabled(musb))
  329. musb->xceiv->set_power = bfin_set_power;
  330. musb->isr = blackfin_interrupt;
  331. return 0;
  332. }
  333. #ifdef CONFIG_PM
  334. void musb_platform_save_context(struct musb *musb,
  335. struct musb_context_registers *musb_context)
  336. {
  337. if (is_host_active(musb))
  338. /*
  339. * During hibernate gpio_vrsel will change from high to low
  340. * low which will generate wakeup event resume the system
  341. * immediately. Set it to 0 before hibernate to avoid this
  342. * wakeup event.
  343. */
  344. gpio_set_value(musb->config->gpio_vrsel, 0);
  345. }
  346. void musb_platform_restore_context(struct musb *musb,
  347. struct musb_context_registers *musb_context)
  348. {
  349. musb_platform_reg_init(musb);
  350. }
  351. #endif
  352. int musb_platform_exit(struct musb *musb)
  353. {
  354. gpio_free(musb->config->gpio_vrsel);
  355. otg_put_transceiver(musb->xceiv);
  356. usb_nop_xceiv_unregister();
  357. return 0;
  358. }