omap2430.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (C) 2005-2007 by Texas Instruments
  3. * Some code has been taken from tusb6010.c
  4. * Copyrights for that are attributable to:
  5. * Copyright (C) 2006 Nokia Corporation
  6. * Tony Lindgren <tony@atomide.com>
  7. *
  8. * This file is part of the Inventra Controller Driver for Linux.
  9. *
  10. * The Inventra Controller Driver for Linux is free software; you
  11. * can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License version 2 as published by the Free Software
  13. * Foundation.
  14. *
  15. * The Inventra Controller Driver for Linux is distributed in
  16. * the hope that it will be useful, but WITHOUT ANY WARRANTY;
  17. * without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. * License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with The Inventra Controller Driver for Linux ; if not,
  23. * write to the Free Software Foundation, Inc., 59 Temple Place,
  24. * Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/io.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/err.h>
  37. #include <linux/usb/musb-omap.h>
  38. #include "musb_core.h"
  39. #include "omap2430.h"
  40. struct omap2430_glue {
  41. struct device *dev;
  42. struct platform_device *musb;
  43. enum omap_musb_vbus_id_status status;
  44. struct work_struct omap_musb_mailbox_work;
  45. };
  46. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  47. struct omap2430_glue *_glue;
  48. static struct timer_list musb_idle_timer;
  49. static void musb_do_idle(unsigned long _musb)
  50. {
  51. struct musb *musb = (void *)_musb;
  52. unsigned long flags;
  53. u8 power;
  54. u8 devctl;
  55. spin_lock_irqsave(&musb->lock, flags);
  56. switch (musb->xceiv->state) {
  57. case OTG_STATE_A_WAIT_BCON:
  58. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  59. if (devctl & MUSB_DEVCTL_BDEVICE) {
  60. musb->xceiv->state = OTG_STATE_B_IDLE;
  61. MUSB_DEV_MODE(musb);
  62. } else {
  63. musb->xceiv->state = OTG_STATE_A_IDLE;
  64. MUSB_HST_MODE(musb);
  65. }
  66. break;
  67. case OTG_STATE_A_SUSPEND:
  68. /* finish RESUME signaling? */
  69. if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
  70. power = musb_readb(musb->mregs, MUSB_POWER);
  71. power &= ~MUSB_POWER_RESUME;
  72. dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
  73. musb_writeb(musb->mregs, MUSB_POWER, power);
  74. musb->is_active = 1;
  75. musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
  76. | MUSB_PORT_STAT_RESUME);
  77. musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
  78. usb_hcd_poll_rh_status(musb_to_hcd(musb));
  79. /* NOTE: it might really be A_WAIT_BCON ... */
  80. musb->xceiv->state = OTG_STATE_A_HOST;
  81. }
  82. break;
  83. case OTG_STATE_A_HOST:
  84. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  85. if (devctl & MUSB_DEVCTL_BDEVICE)
  86. musb->xceiv->state = OTG_STATE_B_IDLE;
  87. else
  88. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  89. default:
  90. break;
  91. }
  92. spin_unlock_irqrestore(&musb->lock, flags);
  93. }
  94. static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
  95. {
  96. unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
  97. static unsigned long last_timer;
  98. if (timeout == 0)
  99. timeout = default_timeout;
  100. /* Never idle if active, or when VBUS timeout is not set as host */
  101. if (musb->is_active || ((musb->a_wait_bcon == 0)
  102. && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
  103. dev_dbg(musb->controller, "%s active, deleting timer\n",
  104. otg_state_string(musb->xceiv->state));
  105. del_timer(&musb_idle_timer);
  106. last_timer = jiffies;
  107. return;
  108. }
  109. if (time_after(last_timer, timeout)) {
  110. if (!timer_pending(&musb_idle_timer))
  111. last_timer = timeout;
  112. else {
  113. dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
  114. return;
  115. }
  116. }
  117. last_timer = timeout;
  118. dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
  119. otg_state_string(musb->xceiv->state),
  120. (unsigned long)jiffies_to_msecs(timeout - jiffies));
  121. mod_timer(&musb_idle_timer, timeout);
  122. }
  123. static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
  124. {
  125. struct usb_otg *otg = musb->xceiv->otg;
  126. u8 devctl;
  127. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  128. int ret = 1;
  129. /* HDRC controls CPEN, but beware current surges during device
  130. * connect. They can trigger transient overcurrent conditions
  131. * that must be ignored.
  132. */
  133. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  134. if (is_on) {
  135. if (musb->xceiv->state == OTG_STATE_A_IDLE) {
  136. /* start the session */
  137. devctl |= MUSB_DEVCTL_SESSION;
  138. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  139. /*
  140. * Wait for the musb to set as A device to enable the
  141. * VBUS
  142. */
  143. while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
  144. cpu_relax();
  145. if (time_after(jiffies, timeout)) {
  146. dev_err(musb->controller,
  147. "configured as A device timeout");
  148. ret = -EINVAL;
  149. break;
  150. }
  151. }
  152. if (ret && otg->set_vbus)
  153. otg_set_vbus(otg, 1);
  154. } else {
  155. musb->is_active = 1;
  156. otg->default_a = 1;
  157. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  158. devctl |= MUSB_DEVCTL_SESSION;
  159. MUSB_HST_MODE(musb);
  160. }
  161. } else {
  162. musb->is_active = 0;
  163. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
  164. * jumping right to B_IDLE...
  165. */
  166. otg->default_a = 0;
  167. musb->xceiv->state = OTG_STATE_B_IDLE;
  168. devctl &= ~MUSB_DEVCTL_SESSION;
  169. MUSB_DEV_MODE(musb);
  170. }
  171. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  172. dev_dbg(musb->controller, "VBUS %s, devctl %02x "
  173. /* otg %3x conf %08x prcm %08x */ "\n",
  174. otg_state_string(musb->xceiv->state),
  175. musb_readb(musb->mregs, MUSB_DEVCTL));
  176. }
  177. static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
  178. {
  179. u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  180. devctl |= MUSB_DEVCTL_SESSION;
  181. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  182. return 0;
  183. }
  184. static inline void omap2430_low_level_exit(struct musb *musb)
  185. {
  186. u32 l;
  187. /* in any role */
  188. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  189. l |= ENABLEFORCE; /* enable MSTANDBY */
  190. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  191. }
  192. static inline void omap2430_low_level_init(struct musb *musb)
  193. {
  194. u32 l;
  195. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  196. l &= ~ENABLEFORCE; /* disable MSTANDBY */
  197. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  198. }
  199. void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
  200. {
  201. struct omap2430_glue *glue = _glue;
  202. struct musb *musb = glue_to_musb(glue);
  203. glue->status = status;
  204. if (!musb) {
  205. dev_err(glue->dev, "musb core is not yet ready\n");
  206. return;
  207. }
  208. schedule_work(&glue->omap_musb_mailbox_work);
  209. }
  210. EXPORT_SYMBOL_GPL(omap_musb_mailbox);
  211. static void omap_musb_set_mailbox(struct omap2430_glue *glue)
  212. {
  213. struct musb *musb = glue_to_musb(glue);
  214. struct device *dev = musb->controller;
  215. struct musb_hdrc_platform_data *pdata = dev->platform_data;
  216. struct omap_musb_board_data *data = pdata->board_data;
  217. switch (glue->status) {
  218. case OMAP_MUSB_ID_GROUND:
  219. dev_dbg(dev, "ID GND\n");
  220. musb->xceiv->last_event = USB_EVENT_ID;
  221. if (!is_otg_enabled(musb) || musb->gadget_driver) {
  222. pm_runtime_get_sync(dev);
  223. usb_phy_init(musb->xceiv);
  224. omap2430_musb_set_vbus(musb, 1);
  225. }
  226. break;
  227. case OMAP_MUSB_VBUS_VALID:
  228. dev_dbg(dev, "VBUS Connect\n");
  229. musb->xceiv->last_event = USB_EVENT_VBUS;
  230. if (musb->gadget_driver)
  231. pm_runtime_get_sync(dev);
  232. usb_phy_init(musb->xceiv);
  233. break;
  234. case OMAP_MUSB_ID_FLOAT:
  235. case OMAP_MUSB_VBUS_OFF:
  236. dev_dbg(dev, "VBUS Disconnect\n");
  237. musb->xceiv->last_event = USB_EVENT_NONE;
  238. if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
  239. if (musb->gadget_driver) {
  240. pm_runtime_mark_last_busy(dev);
  241. pm_runtime_put_autosuspend(dev);
  242. }
  243. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  244. if (musb->xceiv->otg->set_vbus)
  245. otg_set_vbus(musb->xceiv->otg, 0);
  246. }
  247. usb_phy_shutdown(musb->xceiv);
  248. break;
  249. default:
  250. dev_dbg(dev, "ID float\n");
  251. }
  252. }
  253. static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
  254. {
  255. struct omap2430_glue *glue = container_of(mailbox_work,
  256. struct omap2430_glue, omap_musb_mailbox_work);
  257. omap_musb_set_mailbox(glue);
  258. }
  259. static int omap2430_musb_init(struct musb *musb)
  260. {
  261. u32 l;
  262. int status = 0;
  263. struct device *dev = musb->controller;
  264. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  265. struct musb_hdrc_platform_data *plat = dev->platform_data;
  266. struct omap_musb_board_data *data = plat->board_data;
  267. /* We require some kind of external transceiver, hooked
  268. * up through ULPI. TWL4030-family PMICs include one,
  269. * which needs a driver, drivers aren't always needed.
  270. */
  271. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  272. if (!musb->xceiv) {
  273. pr_err("HS USB OTG: no transceiver configured\n");
  274. return -ENODEV;
  275. }
  276. status = pm_runtime_get_sync(dev);
  277. if (status < 0) {
  278. dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
  279. goto err1;
  280. }
  281. l = musb_readl(musb->mregs, OTG_INTERFSEL);
  282. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  283. /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
  284. l &= ~ULPI_12PIN; /* Disable ULPI */
  285. l |= UTMI_8BIT; /* Enable UTMI */
  286. } else {
  287. l |= ULPI_12PIN;
  288. }
  289. musb_writel(musb->mregs, OTG_INTERFSEL, l);
  290. pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
  291. "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
  292. musb_readl(musb->mregs, OTG_REVISION),
  293. musb_readl(musb->mregs, OTG_SYSCONFIG),
  294. musb_readl(musb->mregs, OTG_SYSSTATUS),
  295. musb_readl(musb->mregs, OTG_INTERFSEL),
  296. musb_readl(musb->mregs, OTG_SIMENABLE));
  297. setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
  298. if (glue->status != OMAP_MUSB_UNKNOWN)
  299. omap_musb_set_mailbox(glue);
  300. pm_runtime_put_noidle(musb->controller);
  301. return 0;
  302. err1:
  303. return status;
  304. }
  305. static void omap2430_musb_enable(struct musb *musb)
  306. {
  307. u8 devctl;
  308. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  309. struct device *dev = musb->controller;
  310. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  311. struct musb_hdrc_platform_data *pdata = dev->platform_data;
  312. struct omap_musb_board_data *data = pdata->board_data;
  313. switch (glue->status) {
  314. case OMAP_MUSB_ID_GROUND:
  315. usb_phy_init(musb->xceiv);
  316. if (data->interface_type != MUSB_INTERFACE_UTMI)
  317. break;
  318. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  319. /* start the session */
  320. devctl |= MUSB_DEVCTL_SESSION;
  321. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  322. while (musb_readb(musb->mregs, MUSB_DEVCTL) &
  323. MUSB_DEVCTL_BDEVICE) {
  324. cpu_relax();
  325. if (time_after(jiffies, timeout)) {
  326. dev_err(dev, "configured as A device timeout");
  327. break;
  328. }
  329. }
  330. break;
  331. case OMAP_MUSB_VBUS_VALID:
  332. usb_phy_init(musb->xceiv);
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. static void omap2430_musb_disable(struct musb *musb)
  339. {
  340. struct device *dev = musb->controller;
  341. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  342. if (glue->status != OMAP_MUSB_UNKNOWN)
  343. usb_phy_shutdown(musb->xceiv);
  344. }
  345. static int omap2430_musb_exit(struct musb *musb)
  346. {
  347. del_timer_sync(&musb_idle_timer);
  348. omap2430_low_level_exit(musb);
  349. usb_put_phy(musb->xceiv);
  350. return 0;
  351. }
  352. static const struct musb_platform_ops omap2430_ops = {
  353. .init = omap2430_musb_init,
  354. .exit = omap2430_musb_exit,
  355. .set_mode = omap2430_musb_set_mode,
  356. .try_idle = omap2430_musb_try_idle,
  357. .set_vbus = omap2430_musb_set_vbus,
  358. .enable = omap2430_musb_enable,
  359. .disable = omap2430_musb_disable,
  360. };
  361. static u64 omap2430_dmamask = DMA_BIT_MASK(32);
  362. static int __devinit omap2430_probe(struct platform_device *pdev)
  363. {
  364. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  365. struct platform_device *musb;
  366. struct omap2430_glue *glue;
  367. int ret = -ENOMEM;
  368. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  369. if (!glue) {
  370. dev_err(&pdev->dev, "failed to allocate glue context\n");
  371. goto err0;
  372. }
  373. musb = platform_device_alloc("musb-hdrc", -1);
  374. if (!musb) {
  375. dev_err(&pdev->dev, "failed to allocate musb device\n");
  376. goto err1;
  377. }
  378. musb->dev.parent = &pdev->dev;
  379. musb->dev.dma_mask = &omap2430_dmamask;
  380. musb->dev.coherent_dma_mask = omap2430_dmamask;
  381. glue->dev = &pdev->dev;
  382. glue->musb = musb;
  383. glue->status = OMAP_MUSB_UNKNOWN;
  384. pdata->platform_ops = &omap2430_ops;
  385. platform_set_drvdata(pdev, glue);
  386. /*
  387. * REVISIT if we ever have two instances of the wrapper, we will be
  388. * in big trouble
  389. */
  390. _glue = glue;
  391. INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
  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 err2;
  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 err2;
  402. }
  403. pm_runtime_enable(&pdev->dev);
  404. ret = platform_device_add(musb);
  405. if (ret) {
  406. dev_err(&pdev->dev, "failed to register musb device\n");
  407. goto err2;
  408. }
  409. return 0;
  410. err2:
  411. platform_device_put(musb);
  412. err1:
  413. kfree(glue);
  414. err0:
  415. return ret;
  416. }
  417. static int __devexit omap2430_remove(struct platform_device *pdev)
  418. {
  419. struct omap2430_glue *glue = platform_get_drvdata(pdev);
  420. cancel_work_sync(&glue->omap_musb_mailbox_work);
  421. platform_device_del(glue->musb);
  422. platform_device_put(glue->musb);
  423. kfree(glue);
  424. return 0;
  425. }
  426. #ifdef CONFIG_PM
  427. static int omap2430_runtime_suspend(struct device *dev)
  428. {
  429. struct omap2430_glue *glue = dev_get_drvdata(dev);
  430. struct musb *musb = glue_to_musb(glue);
  431. if (musb) {
  432. musb->context.otg_interfsel = musb_readl(musb->mregs,
  433. OTG_INTERFSEL);
  434. omap2430_low_level_exit(musb);
  435. usb_phy_set_suspend(musb->xceiv, 1);
  436. }
  437. return 0;
  438. }
  439. static int omap2430_runtime_resume(struct device *dev)
  440. {
  441. struct omap2430_glue *glue = dev_get_drvdata(dev);
  442. struct musb *musb = glue_to_musb(glue);
  443. if (musb) {
  444. omap2430_low_level_init(musb);
  445. musb_writel(musb->mregs, OTG_INTERFSEL,
  446. musb->context.otg_interfsel);
  447. usb_phy_set_suspend(musb->xceiv, 0);
  448. }
  449. return 0;
  450. }
  451. static struct dev_pm_ops omap2430_pm_ops = {
  452. .runtime_suspend = omap2430_runtime_suspend,
  453. .runtime_resume = omap2430_runtime_resume,
  454. };
  455. #define DEV_PM_OPS (&omap2430_pm_ops)
  456. #else
  457. #define DEV_PM_OPS NULL
  458. #endif
  459. static struct platform_driver omap2430_driver = {
  460. .probe = omap2430_probe,
  461. .remove = __devexit_p(omap2430_remove),
  462. .driver = {
  463. .name = "musb-omap2430",
  464. .pm = DEV_PM_OPS,
  465. },
  466. };
  467. MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
  468. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  469. MODULE_LICENSE("GPL v2");
  470. static int __init omap2430_init(void)
  471. {
  472. return platform_driver_register(&omap2430_driver);
  473. }
  474. subsys_initcall(omap2430_init);
  475. static void __exit omap2430_exit(void)
  476. {
  477. platform_driver_unregister(&omap2430_driver);
  478. }
  479. module_exit(omap2430_exit);