blackfin.c 15 KB

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