blackfin.c 14 KB

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