omap2430.c 15 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. struct usb_otg *otg = musb->xceiv->otg;
  218. switch (glue->status) {
  219. case OMAP_MUSB_ID_GROUND:
  220. dev_dbg(dev, "ID GND\n");
  221. otg->default_a = true;
  222. musb->xceiv->state = OTG_STATE_A_IDLE;
  223. musb->xceiv->last_event = USB_EVENT_ID;
  224. if (!is_otg_enabled(musb) || musb->gadget_driver) {
  225. pm_runtime_get_sync(dev);
  226. usb_phy_init(musb->xceiv);
  227. omap2430_musb_set_vbus(musb, 1);
  228. }
  229. break;
  230. case OMAP_MUSB_VBUS_VALID:
  231. dev_dbg(dev, "VBUS Connect\n");
  232. otg->default_a = false;
  233. musb->xceiv->state = OTG_STATE_B_IDLE;
  234. musb->xceiv->last_event = USB_EVENT_VBUS;
  235. if (musb->gadget_driver)
  236. pm_runtime_get_sync(dev);
  237. usb_phy_init(musb->xceiv);
  238. break;
  239. case OMAP_MUSB_ID_FLOAT:
  240. case OMAP_MUSB_VBUS_OFF:
  241. dev_dbg(dev, "VBUS Disconnect\n");
  242. musb->xceiv->last_event = USB_EVENT_NONE;
  243. if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
  244. if (musb->gadget_driver) {
  245. pm_runtime_mark_last_busy(dev);
  246. pm_runtime_put_autosuspend(dev);
  247. }
  248. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  249. if (musb->xceiv->otg->set_vbus)
  250. otg_set_vbus(musb->xceiv->otg, 0);
  251. }
  252. usb_phy_shutdown(musb->xceiv);
  253. break;
  254. default:
  255. dev_dbg(dev, "ID float\n");
  256. }
  257. }
  258. static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
  259. {
  260. struct omap2430_glue *glue = container_of(mailbox_work,
  261. struct omap2430_glue, omap_musb_mailbox_work);
  262. omap_musb_set_mailbox(glue);
  263. }
  264. static int omap2430_musb_init(struct musb *musb)
  265. {
  266. u32 l;
  267. int status = 0;
  268. struct device *dev = musb->controller;
  269. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  270. struct musb_hdrc_platform_data *plat = dev->platform_data;
  271. struct omap_musb_board_data *data = plat->board_data;
  272. /* We require some kind of external transceiver, hooked
  273. * up through ULPI. TWL4030-family PMICs include one,
  274. * which needs a driver, drivers aren't always needed.
  275. */
  276. musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  277. if (IS_ERR_OR_NULL(musb->xceiv)) {
  278. pr_err("HS USB OTG: no transceiver configured\n");
  279. return -ENODEV;
  280. }
  281. status = pm_runtime_get_sync(dev);
  282. if (status < 0) {
  283. dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
  284. goto err1;
  285. }
  286. l = musb_readl(musb->mregs, OTG_INTERFSEL);
  287. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  288. /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
  289. l &= ~ULPI_12PIN; /* Disable ULPI */
  290. l |= UTMI_8BIT; /* Enable UTMI */
  291. } else {
  292. l |= ULPI_12PIN;
  293. }
  294. musb_writel(musb->mregs, OTG_INTERFSEL, l);
  295. pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
  296. "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
  297. musb_readl(musb->mregs, OTG_REVISION),
  298. musb_readl(musb->mregs, OTG_SYSCONFIG),
  299. musb_readl(musb->mregs, OTG_SYSSTATUS),
  300. musb_readl(musb->mregs, OTG_INTERFSEL),
  301. musb_readl(musb->mregs, OTG_SIMENABLE));
  302. setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
  303. if (glue->status != OMAP_MUSB_UNKNOWN)
  304. omap_musb_set_mailbox(glue);
  305. pm_runtime_put_noidle(musb->controller);
  306. return 0;
  307. err1:
  308. return status;
  309. }
  310. static void omap2430_musb_enable(struct musb *musb)
  311. {
  312. u8 devctl;
  313. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  314. struct device *dev = musb->controller;
  315. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  316. struct musb_hdrc_platform_data *pdata = dev->platform_data;
  317. struct omap_musb_board_data *data = pdata->board_data;
  318. switch (glue->status) {
  319. case OMAP_MUSB_ID_GROUND:
  320. usb_phy_init(musb->xceiv);
  321. if (data->interface_type != MUSB_INTERFACE_UTMI)
  322. break;
  323. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  324. /* start the session */
  325. devctl |= MUSB_DEVCTL_SESSION;
  326. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  327. while (musb_readb(musb->mregs, MUSB_DEVCTL) &
  328. MUSB_DEVCTL_BDEVICE) {
  329. cpu_relax();
  330. if (time_after(jiffies, timeout)) {
  331. dev_err(dev, "configured as A device timeout");
  332. break;
  333. }
  334. }
  335. break;
  336. case OMAP_MUSB_VBUS_VALID:
  337. usb_phy_init(musb->xceiv);
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. static void omap2430_musb_disable(struct musb *musb)
  344. {
  345. struct device *dev = musb->controller;
  346. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  347. if (glue->status != OMAP_MUSB_UNKNOWN)
  348. usb_phy_shutdown(musb->xceiv);
  349. }
  350. static int omap2430_musb_exit(struct musb *musb)
  351. {
  352. del_timer_sync(&musb_idle_timer);
  353. omap2430_low_level_exit(musb);
  354. return 0;
  355. }
  356. static const struct musb_platform_ops omap2430_ops = {
  357. .init = omap2430_musb_init,
  358. .exit = omap2430_musb_exit,
  359. .set_mode = omap2430_musb_set_mode,
  360. .try_idle = omap2430_musb_try_idle,
  361. .set_vbus = omap2430_musb_set_vbus,
  362. .enable = omap2430_musb_enable,
  363. .disable = omap2430_musb_disable,
  364. };
  365. static u64 omap2430_dmamask = DMA_BIT_MASK(32);
  366. static int __devinit omap2430_probe(struct platform_device *pdev)
  367. {
  368. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  369. struct platform_device *musb;
  370. struct omap2430_glue *glue;
  371. int ret = -ENOMEM;
  372. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  373. if (!glue) {
  374. dev_err(&pdev->dev, "failed to allocate glue context\n");
  375. goto err0;
  376. }
  377. musb = platform_device_alloc("musb-hdrc", -1);
  378. if (!musb) {
  379. dev_err(&pdev->dev, "failed to allocate musb device\n");
  380. goto err0;
  381. }
  382. musb->dev.parent = &pdev->dev;
  383. musb->dev.dma_mask = &omap2430_dmamask;
  384. musb->dev.coherent_dma_mask = omap2430_dmamask;
  385. glue->dev = &pdev->dev;
  386. glue->musb = musb;
  387. glue->status = OMAP_MUSB_UNKNOWN;
  388. pdata->platform_ops = &omap2430_ops;
  389. platform_set_drvdata(pdev, glue);
  390. /*
  391. * REVISIT if we ever have two instances of the wrapper, we will be
  392. * in big trouble
  393. */
  394. _glue = glue;
  395. INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
  396. ret = platform_device_add_resources(musb, pdev->resource,
  397. pdev->num_resources);
  398. if (ret) {
  399. dev_err(&pdev->dev, "failed to add resources\n");
  400. goto err1;
  401. }
  402. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  403. if (ret) {
  404. dev_err(&pdev->dev, "failed to add platform_data\n");
  405. goto err1;
  406. }
  407. pm_runtime_enable(&pdev->dev);
  408. ret = platform_device_add(musb);
  409. if (ret) {
  410. dev_err(&pdev->dev, "failed to register musb device\n");
  411. goto err1;
  412. }
  413. return 0;
  414. err1:
  415. platform_device_put(musb);
  416. err0:
  417. return ret;
  418. }
  419. static int __devexit omap2430_remove(struct platform_device *pdev)
  420. {
  421. struct omap2430_glue *glue = platform_get_drvdata(pdev);
  422. cancel_work_sync(&glue->omap_musb_mailbox_work);
  423. platform_device_del(glue->musb);
  424. platform_device_put(glue->musb);
  425. return 0;
  426. }
  427. #ifdef CONFIG_PM
  428. static int omap2430_runtime_suspend(struct device *dev)
  429. {
  430. struct omap2430_glue *glue = dev_get_drvdata(dev);
  431. struct musb *musb = glue_to_musb(glue);
  432. if (musb) {
  433. musb->context.otg_interfsel = musb_readl(musb->mregs,
  434. OTG_INTERFSEL);
  435. omap2430_low_level_exit(musb);
  436. usb_phy_set_suspend(musb->xceiv, 1);
  437. }
  438. return 0;
  439. }
  440. static int omap2430_runtime_resume(struct device *dev)
  441. {
  442. struct omap2430_glue *glue = dev_get_drvdata(dev);
  443. struct musb *musb = glue_to_musb(glue);
  444. if (musb) {
  445. omap2430_low_level_init(musb);
  446. musb_writel(musb->mregs, OTG_INTERFSEL,
  447. musb->context.otg_interfsel);
  448. usb_phy_set_suspend(musb->xceiv, 0);
  449. }
  450. return 0;
  451. }
  452. static struct dev_pm_ops omap2430_pm_ops = {
  453. .runtime_suspend = omap2430_runtime_suspend,
  454. .runtime_resume = omap2430_runtime_resume,
  455. };
  456. #define DEV_PM_OPS (&omap2430_pm_ops)
  457. #else
  458. #define DEV_PM_OPS NULL
  459. #endif
  460. static struct platform_driver omap2430_driver = {
  461. .probe = omap2430_probe,
  462. .remove = __devexit_p(omap2430_remove),
  463. .driver = {
  464. .name = "musb-omap2430",
  465. .pm = DEV_PM_OPS,
  466. },
  467. };
  468. MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
  469. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  470. MODULE_LICENSE("GPL v2");
  471. static int __init omap2430_init(void)
  472. {
  473. return platform_driver_register(&omap2430_driver);
  474. }
  475. subsys_initcall(omap2430_init);
  476. static void __exit omap2430_exit(void)
  477. {
  478. platform_driver_unregister(&omap2430_driver);
  479. }
  480. module_exit(omap2430_exit);