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