blackfin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 <linux/platform_device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <asm/cacheflush.h>
  20. #include "musb_core.h"
  21. #include "blackfin.h"
  22. /*
  23. * Load an endpoint's FIFO
  24. */
  25. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
  26. {
  27. void __iomem *fifo = hw_ep->fifo;
  28. void __iomem *epio = hw_ep->regs;
  29. u8 epnum = hw_ep->epnum;
  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. u16 dma_reg;
  37. flush_dcache_range((unsigned long)src,
  38. (unsigned long)(src + len));
  39. /* Setup DMA address register */
  40. dma_reg = (u32)src;
  41. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
  42. SSYNC();
  43. dma_reg = (u32)src >> 16;
  44. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
  45. SSYNC();
  46. /* Setup DMA count register */
  47. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
  48. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
  49. SSYNC();
  50. /* Enable the DMA */
  51. dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
  52. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
  53. SSYNC();
  54. /* Wait for compelete */
  55. while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
  56. cpu_relax();
  57. /* acknowledge dma interrupt */
  58. bfin_write_USB_DMA_INTERRUPT(1 << epnum);
  59. SSYNC();
  60. /* Reset DMA */
  61. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
  62. SSYNC();
  63. } else {
  64. SSYNC();
  65. if (unlikely((unsigned long)src & 0x01))
  66. outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
  67. else
  68. outsw((unsigned long)fifo, src, (len + 1) >> 1);
  69. }
  70. }
  71. /*
  72. * Unload an endpoint's FIFO
  73. */
  74. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  75. {
  76. void __iomem *fifo = hw_ep->fifo;
  77. u8 epnum = hw_ep->epnum;
  78. if (ANOMALY_05000467 && epnum != 0) {
  79. u16 dma_reg;
  80. invalidate_dcache_range((unsigned long)dst,
  81. (unsigned long)(dst + len));
  82. /* Setup DMA address register */
  83. dma_reg = (u32)dst;
  84. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
  85. SSYNC();
  86. dma_reg = (u32)dst >> 16;
  87. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
  88. SSYNC();
  89. /* Setup DMA count register */
  90. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
  91. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
  92. SSYNC();
  93. /* Enable the DMA */
  94. dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
  95. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
  96. SSYNC();
  97. /* Wait for compelete */
  98. while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
  99. cpu_relax();
  100. /* acknowledge dma interrupt */
  101. bfin_write_USB_DMA_INTERRUPT(1 << epnum);
  102. SSYNC();
  103. /* Reset DMA */
  104. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
  105. SSYNC();
  106. } else {
  107. SSYNC();
  108. /* Read the last byte of packet with odd size from address fifo + 4
  109. * to trigger 1 byte access to EP0 FIFO.
  110. */
  111. if (len == 1)
  112. *dst = (u8)inw((unsigned long)fifo + 4);
  113. else {
  114. if (unlikely((unsigned long)dst & 0x01))
  115. insw_8((unsigned long)fifo, dst, len >> 1);
  116. else
  117. insw((unsigned long)fifo, dst, len >> 1);
  118. if (len & 0x01)
  119. *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
  120. }
  121. }
  122. DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
  123. 'R', hw_ep->epnum, fifo, len, dst);
  124. dump_fifo_data(dst, len);
  125. }
  126. static irqreturn_t blackfin_interrupt(int irq, void *__hci)
  127. {
  128. unsigned long flags;
  129. irqreturn_t retval = IRQ_NONE;
  130. struct musb *musb = __hci;
  131. spin_lock_irqsave(&musb->lock, flags);
  132. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  133. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  134. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  135. if (musb->int_usb || musb->int_tx || musb->int_rx) {
  136. musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
  137. musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
  138. musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
  139. retval = musb_interrupt(musb);
  140. }
  141. /* Start sampling ID pin, when plug is removed from MUSB */
  142. if (is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
  143. || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
  144. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  145. musb->a_wait_bcon = TIMER_DELAY;
  146. }
  147. spin_unlock_irqrestore(&musb->lock, flags);
  148. return retval;
  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. static u8 toggle;
  156. spin_lock_irqsave(&musb->lock, flags);
  157. switch (musb->xceiv->state) {
  158. case OTG_STATE_A_IDLE:
  159. case OTG_STATE_A_WAIT_BCON:
  160. /* Start a new session */
  161. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  162. val &= ~MUSB_DEVCTL_SESSION;
  163. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  164. val |= MUSB_DEVCTL_SESSION;
  165. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  166. /* Check if musb is host or peripheral. */
  167. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  168. if (!(val & MUSB_DEVCTL_BDEVICE)) {
  169. gpio_set_value(musb->config->gpio_vrsel, 1);
  170. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  171. } else {
  172. gpio_set_value(musb->config->gpio_vrsel, 0);
  173. /* Ignore VBUSERROR and SUSPEND IRQ */
  174. val = musb_readb(musb->mregs, MUSB_INTRUSBE);
  175. val &= ~MUSB_INTR_VBUSERROR;
  176. musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
  177. val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
  178. musb_writeb(musb->mregs, MUSB_INTRUSB, val);
  179. if (is_otg_enabled(musb))
  180. musb->xceiv->state = OTG_STATE_B_IDLE;
  181. else
  182. musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
  183. }
  184. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  185. break;
  186. case OTG_STATE_B_IDLE:
  187. if (!is_peripheral_enabled(musb))
  188. break;
  189. /* Start a new session. It seems that MUSB needs taking
  190. * some time to recognize the type of the plug inserted?
  191. */
  192. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  193. val |= MUSB_DEVCTL_SESSION;
  194. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  195. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  196. if (!(val & MUSB_DEVCTL_BDEVICE)) {
  197. gpio_set_value(musb->config->gpio_vrsel, 1);
  198. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  199. } else {
  200. gpio_set_value(musb->config->gpio_vrsel, 0);
  201. /* Ignore VBUSERROR and SUSPEND IRQ */
  202. val = musb_readb(musb->mregs, MUSB_INTRUSBE);
  203. val &= ~MUSB_INTR_VBUSERROR;
  204. musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
  205. val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
  206. musb_writeb(musb->mregs, MUSB_INTRUSB, val);
  207. /* Toggle the Soft Conn bit, so that we can response to
  208. * the inserting of either A-plug or B-plug.
  209. */
  210. if (toggle) {
  211. val = musb_readb(musb->mregs, MUSB_POWER);
  212. val &= ~MUSB_POWER_SOFTCONN;
  213. musb_writeb(musb->mregs, MUSB_POWER, val);
  214. toggle = 0;
  215. } else {
  216. val = musb_readb(musb->mregs, MUSB_POWER);
  217. val |= MUSB_POWER_SOFTCONN;
  218. musb_writeb(musb->mregs, MUSB_POWER, val);
  219. toggle = 1;
  220. }
  221. /* The delay time is set to 1/4 second by default,
  222. * shortening it, if accelerating A-plug detection
  223. * is needed in OTG mode.
  224. */
  225. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
  226. }
  227. break;
  228. default:
  229. DBG(1, "%s state not handled\n", otg_state_string(musb));
  230. break;
  231. }
  232. spin_unlock_irqrestore(&musb->lock, flags);
  233. DBG(4, "state is %s\n", otg_state_string(musb));
  234. }
  235. static void bfin_musb_enable(struct musb *musb)
  236. {
  237. if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
  238. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  239. musb->a_wait_bcon = TIMER_DELAY;
  240. }
  241. }
  242. static void bfin_musb_disable(struct musb *musb)
  243. {
  244. }
  245. static void bfin_musb_set_vbus(struct musb *musb, int is_on)
  246. {
  247. int value = musb->config->gpio_vrsel_active;
  248. if (!is_on)
  249. value = !value;
  250. gpio_set_value(musb->config->gpio_vrsel, value);
  251. DBG(1, "VBUS %s, devctl %02x "
  252. /* otg %3x conf %08x prcm %08x */ "\n",
  253. otg_state_string(musb),
  254. musb_readb(musb->mregs, MUSB_DEVCTL));
  255. }
  256. static int bfin_musb_set_power(struct otg_transceiver *x, unsigned mA)
  257. {
  258. return 0;
  259. }
  260. static void bfin_musb_try_idle(struct musb *musb, unsigned long timeout)
  261. {
  262. if (!is_otg_enabled(musb) && is_host_enabled(musb))
  263. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  264. }
  265. static int bfin_musb_get_vbus_status(struct musb *musb)
  266. {
  267. return 0;
  268. }
  269. static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
  270. {
  271. return -EIO;
  272. }
  273. static void bfin_musb_reg_init(struct musb *musb)
  274. {
  275. if (ANOMALY_05000346) {
  276. bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
  277. SSYNC();
  278. }
  279. if (ANOMALY_05000347) {
  280. bfin_write_USB_APHY_CNTRL(0x0);
  281. SSYNC();
  282. }
  283. /* Configure PLL oscillator register */
  284. bfin_write_USB_PLLOSC_CTRL(0x30a8);
  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. static int bfin_musb_init(struct musb *musb)
  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. bfin_musb_reg_init(musb);
  323. if (is_host_enabled(musb)) {
  324. musb->board_set_vbus = bfin_musb_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_musb_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. bfin_musb_reg_init(musb);
  350. }
  351. #endif
  352. static int bfin_musb_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. }
  359. const struct musb_platform_ops musb_ops = {
  360. .init = bfin_musb_init,
  361. .exit = bfin_musb_exit,
  362. .enable = bfin_musb_enable,
  363. .disable = bfin_musb_disable,
  364. .set_mode = bfin_musb_set_mode,
  365. .try_idle = bfin_musb_try_idle,
  366. .vbus_status = bfin_musb_vbus_status,
  367. .set_vbus = bfin_musb_set_vbus,
  368. };
  369. static u64 bfin_dmamask = DMA_BIT_MASK(32);
  370. static int __init bfin_probe(struct platform_device *pdev)
  371. {
  372. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  373. struct platform_device *musb;
  374. int ret = -ENOMEM;
  375. musb = platform_device_alloc("musb-hdrc", -1);
  376. if (!musb) {
  377. dev_err(&pdev->dev, "failed to allocate musb device\n");
  378. goto err0;
  379. }
  380. musb->dev.parent = &pdev->dev;
  381. musb->dev.dma_mask = &bfin_dmamask;
  382. musb->dev.coherent_dma_mask = bfin_dmamask;
  383. platform_set_drvdata(pdev, musb);
  384. ret = platform_device_add_resources(musb, pdev->resource,
  385. pdev->num_resources);
  386. if (ret) {
  387. dev_err(&pdev->dev, "failed to add resources\n");
  388. goto err1;
  389. }
  390. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  391. if (ret) {
  392. dev_err(&pdev->dev, "failed to add platform_data\n");
  393. goto err1;
  394. }
  395. ret = platform_device_add(musb);
  396. if (ret) {
  397. dev_err(&pdev->dev, "failed to register musb device\n");
  398. goto err1;
  399. }
  400. return 0;
  401. err1:
  402. platform_device_put(musb);
  403. err0:
  404. return ret;
  405. }
  406. static int __exit bfin_remove(struct platform_device *pdev)
  407. {
  408. struct platform_device *musb = platform_get_drvdata(pdev);
  409. platform_device_del(musb);
  410. platform_device_put(musb);
  411. return 0;
  412. }
  413. static struct platform_driver bfin_driver = {
  414. .remove = __exit_p(bfin_remove),
  415. .driver = {
  416. .name = "musb-bfin",
  417. },
  418. };
  419. MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
  420. MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
  421. MODULE_LICENSE("GPL v2");
  422. static int __init bfin_init(void)
  423. {
  424. return platform_driver_probe(&bfin_driver, bfin_probe);
  425. }
  426. subsys_initcall(bfin_init);
  427. static void __exit bfin_exit(void)
  428. {
  429. platform_driver_unregister(&bfin_driver);
  430. }
  431. module_exit(bfin_exit);