blackfin.c 14 KB

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