blackfin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. struct bfin_glue {
  23. struct device *dev;
  24. struct platform_device *musb;
  25. };
  26. /*
  27. * Load an endpoint's FIFO
  28. */
  29. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
  30. {
  31. void __iomem *fifo = hw_ep->fifo;
  32. void __iomem *epio = hw_ep->regs;
  33. u8 epnum = hw_ep->epnum;
  34. prefetch((u8 *)src);
  35. musb_writew(epio, MUSB_TXCOUNT, len);
  36. DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
  37. hw_ep->epnum, fifo, len, src, epio);
  38. dump_fifo_data(src, len);
  39. if (!ANOMALY_05000380 && epnum != 0) {
  40. u16 dma_reg;
  41. flush_dcache_range((unsigned long)src,
  42. (unsigned long)(src + len));
  43. /* Setup DMA address register */
  44. dma_reg = (u32)src;
  45. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
  46. SSYNC();
  47. dma_reg = (u32)src >> 16;
  48. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
  49. SSYNC();
  50. /* Setup DMA count register */
  51. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
  52. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
  53. SSYNC();
  54. /* Enable the DMA */
  55. dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
  56. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
  57. SSYNC();
  58. /* Wait for compelete */
  59. while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
  60. cpu_relax();
  61. /* acknowledge dma interrupt */
  62. bfin_write_USB_DMA_INTERRUPT(1 << epnum);
  63. SSYNC();
  64. /* Reset DMA */
  65. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
  66. SSYNC();
  67. } else {
  68. SSYNC();
  69. if (unlikely((unsigned long)src & 0x01))
  70. outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
  71. else
  72. outsw((unsigned long)fifo, src, (len + 1) >> 1);
  73. }
  74. }
  75. /*
  76. * Unload an endpoint's FIFO
  77. */
  78. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  79. {
  80. void __iomem *fifo = hw_ep->fifo;
  81. u8 epnum = hw_ep->epnum;
  82. if (ANOMALY_05000467 && epnum != 0) {
  83. u16 dma_reg;
  84. invalidate_dcache_range((unsigned long)dst,
  85. (unsigned long)(dst + len));
  86. /* Setup DMA address register */
  87. dma_reg = (u32)dst;
  88. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
  89. SSYNC();
  90. dma_reg = (u32)dst >> 16;
  91. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
  92. SSYNC();
  93. /* Setup DMA count register */
  94. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
  95. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
  96. SSYNC();
  97. /* Enable the DMA */
  98. dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
  99. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
  100. SSYNC();
  101. /* Wait for compelete */
  102. while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
  103. cpu_relax();
  104. /* acknowledge dma interrupt */
  105. bfin_write_USB_DMA_INTERRUPT(1 << epnum);
  106. SSYNC();
  107. /* Reset DMA */
  108. bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
  109. SSYNC();
  110. } else {
  111. SSYNC();
  112. /* Read the last byte of packet with odd size from address fifo + 4
  113. * to trigger 1 byte access to EP0 FIFO.
  114. */
  115. if (len == 1)
  116. *dst = (u8)inw((unsigned long)fifo + 4);
  117. else {
  118. if (unlikely((unsigned long)dst & 0x01))
  119. insw_8((unsigned long)fifo, dst, len >> 1);
  120. else
  121. insw((unsigned long)fifo, dst, len >> 1);
  122. if (len & 0x01)
  123. *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
  124. }
  125. }
  126. DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
  127. 'R', hw_ep->epnum, fifo, len, dst);
  128. dump_fifo_data(dst, len);
  129. }
  130. static irqreturn_t blackfin_interrupt(int irq, void *__hci)
  131. {
  132. unsigned long flags;
  133. irqreturn_t retval = IRQ_NONE;
  134. struct musb *musb = __hci;
  135. spin_lock_irqsave(&musb->lock, flags);
  136. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  137. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  138. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  139. if (musb->int_usb || musb->int_tx || musb->int_rx) {
  140. musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
  141. musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
  142. musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
  143. retval = musb_interrupt(musb);
  144. }
  145. /* Start sampling ID pin, when plug is removed from MUSB */
  146. if (is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
  147. || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
  148. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  149. musb->a_wait_bcon = TIMER_DELAY;
  150. }
  151. spin_unlock_irqrestore(&musb->lock, flags);
  152. return retval;
  153. }
  154. static void musb_conn_timer_handler(unsigned long _musb)
  155. {
  156. struct musb *musb = (void *)_musb;
  157. unsigned long flags;
  158. u16 val;
  159. static u8 toggle;
  160. spin_lock_irqsave(&musb->lock, flags);
  161. switch (musb->xceiv->state) {
  162. case OTG_STATE_A_IDLE:
  163. case OTG_STATE_A_WAIT_BCON:
  164. /* Start a new session */
  165. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  166. val &= ~MUSB_DEVCTL_SESSION;
  167. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  168. val |= MUSB_DEVCTL_SESSION;
  169. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  170. /* Check if musb is host or peripheral. */
  171. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  172. if (!(val & MUSB_DEVCTL_BDEVICE)) {
  173. gpio_set_value(musb->config->gpio_vrsel, 1);
  174. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  175. } else {
  176. gpio_set_value(musb->config->gpio_vrsel, 0);
  177. /* Ignore VBUSERROR and SUSPEND IRQ */
  178. val = musb_readb(musb->mregs, MUSB_INTRUSBE);
  179. val &= ~MUSB_INTR_VBUSERROR;
  180. musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
  181. val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
  182. musb_writeb(musb->mregs, MUSB_INTRUSB, val);
  183. if (is_otg_enabled(musb))
  184. musb->xceiv->state = OTG_STATE_B_IDLE;
  185. else
  186. musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
  187. }
  188. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  189. break;
  190. case OTG_STATE_B_IDLE:
  191. if (!is_peripheral_enabled(musb))
  192. break;
  193. /* Start a new session. It seems that MUSB needs taking
  194. * some time to recognize the type of the plug inserted?
  195. */
  196. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  197. val |= MUSB_DEVCTL_SESSION;
  198. musb_writew(musb->mregs, MUSB_DEVCTL, val);
  199. val = musb_readw(musb->mregs, MUSB_DEVCTL);
  200. if (!(val & MUSB_DEVCTL_BDEVICE)) {
  201. gpio_set_value(musb->config->gpio_vrsel, 1);
  202. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  203. } else {
  204. gpio_set_value(musb->config->gpio_vrsel, 0);
  205. /* Ignore VBUSERROR and SUSPEND IRQ */
  206. val = musb_readb(musb->mregs, MUSB_INTRUSBE);
  207. val &= ~MUSB_INTR_VBUSERROR;
  208. musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
  209. val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
  210. musb_writeb(musb->mregs, MUSB_INTRUSB, val);
  211. /* Toggle the Soft Conn bit, so that we can response to
  212. * the inserting of either A-plug or B-plug.
  213. */
  214. if (toggle) {
  215. val = musb_readb(musb->mregs, MUSB_POWER);
  216. val &= ~MUSB_POWER_SOFTCONN;
  217. musb_writeb(musb->mregs, MUSB_POWER, val);
  218. toggle = 0;
  219. } else {
  220. val = musb_readb(musb->mregs, MUSB_POWER);
  221. val |= MUSB_POWER_SOFTCONN;
  222. musb_writeb(musb->mregs, MUSB_POWER, val);
  223. toggle = 1;
  224. }
  225. /* The delay time is set to 1/4 second by default,
  226. * shortening it, if accelerating A-plug detection
  227. * is needed in OTG mode.
  228. */
  229. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
  230. }
  231. break;
  232. default:
  233. DBG(1, "%s state not handled\n", otg_state_string(musb));
  234. break;
  235. }
  236. spin_unlock_irqrestore(&musb->lock, flags);
  237. DBG(4, "state is %s\n", otg_state_string(musb));
  238. }
  239. static void bfin_musb_enable(struct musb *musb)
  240. {
  241. if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
  242. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  243. musb->a_wait_bcon = TIMER_DELAY;
  244. }
  245. }
  246. static void bfin_musb_disable(struct musb *musb)
  247. {
  248. }
  249. static void bfin_musb_set_vbus(struct musb *musb, int is_on)
  250. {
  251. int value = musb->config->gpio_vrsel_active;
  252. if (!is_on)
  253. value = !value;
  254. gpio_set_value(musb->config->gpio_vrsel, value);
  255. DBG(1, "VBUS %s, devctl %02x "
  256. /* otg %3x conf %08x prcm %08x */ "\n",
  257. otg_state_string(musb),
  258. musb_readb(musb->mregs, MUSB_DEVCTL));
  259. }
  260. static int bfin_musb_set_power(struct otg_transceiver *x, unsigned mA)
  261. {
  262. return 0;
  263. }
  264. static void bfin_musb_try_idle(struct musb *musb, unsigned long timeout)
  265. {
  266. if (!is_otg_enabled(musb) && is_host_enabled(musb))
  267. mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
  268. }
  269. static int bfin_musb_get_vbus_status(struct musb *musb)
  270. {
  271. return 0;
  272. }
  273. static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
  274. {
  275. return -EIO;
  276. }
  277. static void bfin_musb_reg_init(struct musb *musb)
  278. {
  279. if (ANOMALY_05000346) {
  280. bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
  281. SSYNC();
  282. }
  283. if (ANOMALY_05000347) {
  284. bfin_write_USB_APHY_CNTRL(0x0);
  285. SSYNC();
  286. }
  287. /* Configure PLL oscillator register */
  288. bfin_write_USB_PLLOSC_CTRL(0x30a8);
  289. SSYNC();
  290. bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
  291. SSYNC();
  292. bfin_write_USB_EP_NI0_RXMAXP(64);
  293. SSYNC();
  294. bfin_write_USB_EP_NI0_TXMAXP(64);
  295. SSYNC();
  296. /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
  297. bfin_write_USB_GLOBINTR(0x7);
  298. SSYNC();
  299. bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
  300. EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
  301. EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
  302. EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
  303. EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
  304. SSYNC();
  305. }
  306. static int bfin_musb_init(struct musb *musb)
  307. {
  308. /*
  309. * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
  310. * and OTG HOST modes, while rev 1.1 and greater require PE7 to
  311. * be low for DEVICE mode and high for HOST mode. We set it high
  312. * here because we are in host mode
  313. */
  314. if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
  315. printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
  316. musb->config->gpio_vrsel);
  317. return -ENODEV;
  318. }
  319. gpio_direction_output(musb->config->gpio_vrsel, 0);
  320. usb_nop_xceiv_register();
  321. musb->xceiv = otg_get_transceiver();
  322. if (!musb->xceiv) {
  323. gpio_free(musb->config->gpio_vrsel);
  324. return -ENODEV;
  325. }
  326. bfin_musb_reg_init(musb);
  327. if (is_host_enabled(musb)) {
  328. musb->board_set_vbus = bfin_musb_set_vbus;
  329. setup_timer(&musb_conn_timer,
  330. musb_conn_timer_handler, (unsigned long) musb);
  331. }
  332. if (is_peripheral_enabled(musb))
  333. musb->xceiv->set_power = bfin_musb_set_power;
  334. musb->isr = blackfin_interrupt;
  335. return 0;
  336. }
  337. #ifdef CONFIG_PM
  338. void musb_platform_save_context(struct musb *musb,
  339. struct musb_context_registers *musb_context)
  340. {
  341. if (is_host_active(musb))
  342. /*
  343. * During hibernate gpio_vrsel will change from high to low
  344. * low which will generate wakeup event resume the system
  345. * immediately. Set it to 0 before hibernate to avoid this
  346. * wakeup event.
  347. */
  348. gpio_set_value(musb->config->gpio_vrsel, 0);
  349. }
  350. void musb_platform_restore_context(struct musb *musb,
  351. struct musb_context_registers *musb_context)
  352. {
  353. bfin_musb_reg_init(musb);
  354. }
  355. #endif
  356. static int bfin_musb_exit(struct musb *musb)
  357. {
  358. gpio_free(musb->config->gpio_vrsel);
  359. otg_put_transceiver(musb->xceiv);
  360. usb_nop_xceiv_unregister();
  361. return 0;
  362. }
  363. static const struct musb_platform_ops bfin_ops = {
  364. .init = bfin_musb_init,
  365. .exit = bfin_musb_exit,
  366. .enable = bfin_musb_enable,
  367. .disable = bfin_musb_disable,
  368. .set_mode = bfin_musb_set_mode,
  369. .try_idle = bfin_musb_try_idle,
  370. .vbus_status = bfin_musb_vbus_status,
  371. .set_vbus = bfin_musb_set_vbus,
  372. };
  373. static u64 bfin_dmamask = DMA_BIT_MASK(32);
  374. static int __init bfin_probe(struct platform_device *pdev)
  375. {
  376. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  377. struct platform_device *musb;
  378. struct bfin_glue *glue;
  379. int ret = -ENOMEM;
  380. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  381. if (!glue) {
  382. dev_err(&pdev->dev, "failed to allocate glue context\n");
  383. goto err0;
  384. }
  385. musb = platform_device_alloc("musb-hdrc", -1);
  386. if (!musb) {
  387. dev_err(&pdev->dev, "failed to allocate musb device\n");
  388. goto err1;
  389. }
  390. musb->dev.parent = &pdev->dev;
  391. musb->dev.dma_mask = &bfin_dmamask;
  392. musb->dev.coherent_dma_mask = bfin_dmamask;
  393. glue->dev = &pdev->dev;
  394. glue->musb = musb;
  395. pdata->platform_ops = &bfin_ops;
  396. platform_set_drvdata(pdev, glue);
  397. ret = platform_device_add_resources(musb, pdev->resource,
  398. pdev->num_resources);
  399. if (ret) {
  400. dev_err(&pdev->dev, "failed to add resources\n");
  401. goto err2;
  402. }
  403. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  404. if (ret) {
  405. dev_err(&pdev->dev, "failed to add platform_data\n");
  406. goto err2;
  407. }
  408. ret = platform_device_add(musb);
  409. if (ret) {
  410. dev_err(&pdev->dev, "failed to register musb device\n");
  411. goto err2;
  412. }
  413. return 0;
  414. err2:
  415. platform_device_put(musb);
  416. err1:
  417. kfree(glue);
  418. err0:
  419. return ret;
  420. }
  421. static int __exit bfin_remove(struct platform_device *pdev)
  422. {
  423. struct bfin_glue *glue = platform_get_drvdata(pdev);
  424. platform_device_del(glue->musb);
  425. platform_device_put(glue->musb);
  426. kfree(glue);
  427. return 0;
  428. }
  429. static struct platform_driver bfin_driver = {
  430. .remove = __exit_p(bfin_remove),
  431. .driver = {
  432. .name = "musb-bfin",
  433. },
  434. };
  435. MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
  436. MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
  437. MODULE_LICENSE("GPL v2");
  438. static int __init bfin_init(void)
  439. {
  440. return platform_driver_probe(&bfin_driver, bfin_probe);
  441. }
  442. subsys_initcall(bfin_init);
  443. static void __exit bfin_exit(void)
  444. {
  445. platform_driver_unregister(&bfin_driver);
  446. }
  447. module_exit(bfin_exit);