blackfin.c 14 KB

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