omap2430.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 "musb_core.h"
  38. #include "omap2430.h"
  39. struct omap2430_glue {
  40. struct device *dev;
  41. struct platform_device *musb;
  42. };
  43. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  44. static struct timer_list musb_idle_timer;
  45. static void musb_do_idle(unsigned long _musb)
  46. {
  47. struct musb *musb = (void *)_musb;
  48. unsigned long flags;
  49. u8 power;
  50. u8 devctl;
  51. spin_lock_irqsave(&musb->lock, flags);
  52. switch (musb->xceiv->state) {
  53. case OTG_STATE_A_WAIT_BCON:
  54. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  55. if (devctl & MUSB_DEVCTL_BDEVICE) {
  56. musb->xceiv->state = OTG_STATE_B_IDLE;
  57. MUSB_DEV_MODE(musb);
  58. } else {
  59. musb->xceiv->state = OTG_STATE_A_IDLE;
  60. MUSB_HST_MODE(musb);
  61. }
  62. break;
  63. case OTG_STATE_A_SUSPEND:
  64. /* finish RESUME signaling? */
  65. if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
  66. power = musb_readb(musb->mregs, MUSB_POWER);
  67. power &= ~MUSB_POWER_RESUME;
  68. dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
  69. musb_writeb(musb->mregs, MUSB_POWER, power);
  70. musb->is_active = 1;
  71. musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
  72. | MUSB_PORT_STAT_RESUME);
  73. musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
  74. usb_hcd_poll_rh_status(musb_to_hcd(musb));
  75. /* NOTE: it might really be A_WAIT_BCON ... */
  76. musb->xceiv->state = OTG_STATE_A_HOST;
  77. }
  78. break;
  79. case OTG_STATE_A_HOST:
  80. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  81. if (devctl & MUSB_DEVCTL_BDEVICE)
  82. musb->xceiv->state = OTG_STATE_B_IDLE;
  83. else
  84. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  85. default:
  86. break;
  87. }
  88. spin_unlock_irqrestore(&musb->lock, flags);
  89. }
  90. static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
  91. {
  92. unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
  93. static unsigned long last_timer;
  94. if (timeout == 0)
  95. timeout = default_timeout;
  96. /* Never idle if active, or when VBUS timeout is not set as host */
  97. if (musb->is_active || ((musb->a_wait_bcon == 0)
  98. && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
  99. dev_dbg(musb->controller, "%s active, deleting timer\n",
  100. otg_state_string(musb->xceiv->state));
  101. del_timer(&musb_idle_timer);
  102. last_timer = jiffies;
  103. return;
  104. }
  105. if (time_after(last_timer, timeout)) {
  106. if (!timer_pending(&musb_idle_timer))
  107. last_timer = timeout;
  108. else {
  109. dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
  110. return;
  111. }
  112. }
  113. last_timer = timeout;
  114. dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
  115. otg_state_string(musb->xceiv->state),
  116. (unsigned long)jiffies_to_msecs(timeout - jiffies));
  117. mod_timer(&musb_idle_timer, timeout);
  118. }
  119. static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
  120. {
  121. struct usb_otg *otg = musb->xceiv->otg;
  122. u8 devctl;
  123. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  124. int ret = 1;
  125. /* HDRC controls CPEN, but beware current surges during device
  126. * connect. They can trigger transient overcurrent conditions
  127. * that must be ignored.
  128. */
  129. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  130. if (is_on) {
  131. if (musb->xceiv->state == OTG_STATE_A_IDLE) {
  132. /* start the session */
  133. devctl |= MUSB_DEVCTL_SESSION;
  134. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  135. /*
  136. * Wait for the musb to set as A device to enable the
  137. * VBUS
  138. */
  139. while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
  140. cpu_relax();
  141. if (time_after(jiffies, timeout)) {
  142. dev_err(musb->controller,
  143. "configured as A device timeout");
  144. ret = -EINVAL;
  145. break;
  146. }
  147. }
  148. if (ret && otg->set_vbus)
  149. otg_set_vbus(otg, 1);
  150. } else {
  151. musb->is_active = 1;
  152. otg->default_a = 1;
  153. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  154. devctl |= MUSB_DEVCTL_SESSION;
  155. MUSB_HST_MODE(musb);
  156. }
  157. } else {
  158. musb->is_active = 0;
  159. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
  160. * jumping right to B_IDLE...
  161. */
  162. otg->default_a = 0;
  163. musb->xceiv->state = OTG_STATE_B_IDLE;
  164. devctl &= ~MUSB_DEVCTL_SESSION;
  165. MUSB_DEV_MODE(musb);
  166. }
  167. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  168. dev_dbg(musb->controller, "VBUS %s, devctl %02x "
  169. /* otg %3x conf %08x prcm %08x */ "\n",
  170. otg_state_string(musb->xceiv->state),
  171. musb_readb(musb->mregs, MUSB_DEVCTL));
  172. }
  173. static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
  174. {
  175. u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  176. devctl |= MUSB_DEVCTL_SESSION;
  177. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  178. return 0;
  179. }
  180. static inline void omap2430_low_level_exit(struct musb *musb)
  181. {
  182. u32 l;
  183. /* in any role */
  184. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  185. l |= ENABLEFORCE; /* enable MSTANDBY */
  186. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  187. }
  188. static inline void omap2430_low_level_init(struct musb *musb)
  189. {
  190. u32 l;
  191. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  192. l &= ~ENABLEFORCE; /* disable MSTANDBY */
  193. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  194. }
  195. static int musb_otg_notifications(struct notifier_block *nb,
  196. unsigned long event, void *unused)
  197. {
  198. struct musb *musb = container_of(nb, struct musb, nb);
  199. musb->xceiv_event = event;
  200. schedule_work(&musb->otg_notifier_work);
  201. return NOTIFY_OK;
  202. }
  203. static void musb_otg_notifier_work(struct work_struct *data_notifier_work)
  204. {
  205. struct musb *musb = container_of(data_notifier_work, struct musb, otg_notifier_work);
  206. struct device *dev = musb->controller;
  207. struct musb_hdrc_platform_data *pdata = dev->platform_data;
  208. struct omap_musb_board_data *data = pdata->board_data;
  209. switch (musb->xceiv_event) {
  210. case USB_EVENT_ID:
  211. dev_dbg(musb->controller, "ID GND\n");
  212. if (!is_otg_enabled(musb) || musb->gadget_driver) {
  213. pm_runtime_get_sync(musb->controller);
  214. usb_phy_init(musb->xceiv);
  215. omap2430_musb_set_vbus(musb, 1);
  216. }
  217. break;
  218. case USB_EVENT_VBUS:
  219. dev_dbg(musb->controller, "VBUS Connect\n");
  220. if (musb->gadget_driver)
  221. pm_runtime_get_sync(musb->controller);
  222. usb_phy_init(musb->xceiv);
  223. break;
  224. case USB_EVENT_NONE:
  225. dev_dbg(musb->controller, "VBUS Disconnect\n");
  226. if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
  227. if (musb->gadget_driver) {
  228. pm_runtime_mark_last_busy(musb->controller);
  229. pm_runtime_put_autosuspend(musb->controller);
  230. }
  231. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  232. if (musb->xceiv->otg->set_vbus)
  233. otg_set_vbus(musb->xceiv->otg, 0);
  234. }
  235. usb_phy_shutdown(musb->xceiv);
  236. break;
  237. default:
  238. dev_dbg(musb->controller, "ID float\n");
  239. }
  240. }
  241. static int omap2430_musb_init(struct musb *musb)
  242. {
  243. u32 l;
  244. int status = 0;
  245. struct device *dev = musb->controller;
  246. struct musb_hdrc_platform_data *plat = dev->platform_data;
  247. struct omap_musb_board_data *data = plat->board_data;
  248. /* We require some kind of external transceiver, hooked
  249. * up through ULPI. TWL4030-family PMICs include one,
  250. * which needs a driver, drivers aren't always needed.
  251. */
  252. musb->xceiv = usb_get_transceiver();
  253. if (!musb->xceiv) {
  254. pr_err("HS USB OTG: no transceiver configured\n");
  255. return -ENODEV;
  256. }
  257. INIT_WORK(&musb->otg_notifier_work, musb_otg_notifier_work);
  258. status = pm_runtime_get_sync(dev);
  259. if (status < 0) {
  260. dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
  261. goto err1;
  262. }
  263. l = musb_readl(musb->mregs, OTG_INTERFSEL);
  264. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  265. /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
  266. l &= ~ULPI_12PIN; /* Disable ULPI */
  267. l |= UTMI_8BIT; /* Enable UTMI */
  268. } else {
  269. l |= ULPI_12PIN;
  270. }
  271. musb_writel(musb->mregs, OTG_INTERFSEL, l);
  272. pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
  273. "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
  274. musb_readl(musb->mregs, OTG_REVISION),
  275. musb_readl(musb->mregs, OTG_SYSCONFIG),
  276. musb_readl(musb->mregs, OTG_SYSSTATUS),
  277. musb_readl(musb->mregs, OTG_INTERFSEL),
  278. musb_readl(musb->mregs, OTG_SIMENABLE));
  279. musb->nb.notifier_call = musb_otg_notifications;
  280. status = usb_register_notifier(musb->xceiv, &musb->nb);
  281. if (status)
  282. dev_dbg(musb->controller, "notification register failed\n");
  283. setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
  284. pm_runtime_put_noidle(musb->controller);
  285. return 0;
  286. err1:
  287. return status;
  288. }
  289. static void omap2430_musb_enable(struct musb *musb)
  290. {
  291. u8 devctl;
  292. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  293. struct device *dev = musb->controller;
  294. struct musb_hdrc_platform_data *pdata = dev->platform_data;
  295. struct omap_musb_board_data *data = pdata->board_data;
  296. switch (musb->xceiv->last_event) {
  297. case USB_EVENT_ID:
  298. usb_phy_init(musb->xceiv);
  299. if (data->interface_type != MUSB_INTERFACE_UTMI)
  300. break;
  301. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  302. /* start the session */
  303. devctl |= MUSB_DEVCTL_SESSION;
  304. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  305. while (musb_readb(musb->mregs, MUSB_DEVCTL) &
  306. MUSB_DEVCTL_BDEVICE) {
  307. cpu_relax();
  308. if (time_after(jiffies, timeout)) {
  309. dev_err(dev, "configured as A device timeout");
  310. break;
  311. }
  312. }
  313. break;
  314. case USB_EVENT_VBUS:
  315. usb_phy_init(musb->xceiv);
  316. break;
  317. default:
  318. break;
  319. }
  320. }
  321. static void omap2430_musb_disable(struct musb *musb)
  322. {
  323. if (musb->xceiv->last_event)
  324. usb_phy_shutdown(musb->xceiv);
  325. }
  326. static int omap2430_musb_exit(struct musb *musb)
  327. {
  328. del_timer_sync(&musb_idle_timer);
  329. cancel_work_sync(&musb->otg_notifier_work);
  330. omap2430_low_level_exit(musb);
  331. usb_put_transceiver(musb->xceiv);
  332. return 0;
  333. }
  334. static const struct musb_platform_ops omap2430_ops = {
  335. .init = omap2430_musb_init,
  336. .exit = omap2430_musb_exit,
  337. .set_mode = omap2430_musb_set_mode,
  338. .try_idle = omap2430_musb_try_idle,
  339. .set_vbus = omap2430_musb_set_vbus,
  340. .enable = omap2430_musb_enable,
  341. .disable = omap2430_musb_disable,
  342. };
  343. static u64 omap2430_dmamask = DMA_BIT_MASK(32);
  344. static int __devinit omap2430_probe(struct platform_device *pdev)
  345. {
  346. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  347. struct platform_device *musb;
  348. struct omap2430_glue *glue;
  349. int ret = -ENOMEM;
  350. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  351. if (!glue) {
  352. dev_err(&pdev->dev, "failed to allocate glue context\n");
  353. goto err0;
  354. }
  355. musb = platform_device_alloc("musb-hdrc", -1);
  356. if (!musb) {
  357. dev_err(&pdev->dev, "failed to allocate musb device\n");
  358. goto err1;
  359. }
  360. musb->dev.parent = &pdev->dev;
  361. musb->dev.dma_mask = &omap2430_dmamask;
  362. musb->dev.coherent_dma_mask = omap2430_dmamask;
  363. glue->dev = &pdev->dev;
  364. glue->musb = musb;
  365. pdata->platform_ops = &omap2430_ops;
  366. platform_set_drvdata(pdev, glue);
  367. ret = platform_device_add_resources(musb, pdev->resource,
  368. pdev->num_resources);
  369. if (ret) {
  370. dev_err(&pdev->dev, "failed to add resources\n");
  371. goto err2;
  372. }
  373. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  374. if (ret) {
  375. dev_err(&pdev->dev, "failed to add platform_data\n");
  376. goto err2;
  377. }
  378. pm_runtime_enable(&pdev->dev);
  379. ret = platform_device_add(musb);
  380. if (ret) {
  381. dev_err(&pdev->dev, "failed to register musb device\n");
  382. goto err2;
  383. }
  384. return 0;
  385. err2:
  386. platform_device_put(musb);
  387. err1:
  388. kfree(glue);
  389. err0:
  390. return ret;
  391. }
  392. static int __devexit omap2430_remove(struct platform_device *pdev)
  393. {
  394. struct omap2430_glue *glue = platform_get_drvdata(pdev);
  395. platform_device_del(glue->musb);
  396. platform_device_put(glue->musb);
  397. kfree(glue);
  398. return 0;
  399. }
  400. #ifdef CONFIG_PM
  401. static int omap2430_runtime_suspend(struct device *dev)
  402. {
  403. struct omap2430_glue *glue = dev_get_drvdata(dev);
  404. struct musb *musb = glue_to_musb(glue);
  405. if (musb) {
  406. musb->context.otg_interfsel = musb_readl(musb->mregs,
  407. OTG_INTERFSEL);
  408. omap2430_low_level_exit(musb);
  409. usb_phy_set_suspend(musb->xceiv, 1);
  410. }
  411. return 0;
  412. }
  413. static int omap2430_runtime_resume(struct device *dev)
  414. {
  415. struct omap2430_glue *glue = dev_get_drvdata(dev);
  416. struct musb *musb = glue_to_musb(glue);
  417. if (musb) {
  418. omap2430_low_level_init(musb);
  419. musb_writel(musb->mregs, OTG_INTERFSEL,
  420. musb->context.otg_interfsel);
  421. usb_phy_set_suspend(musb->xceiv, 0);
  422. }
  423. return 0;
  424. }
  425. static struct dev_pm_ops omap2430_pm_ops = {
  426. .runtime_suspend = omap2430_runtime_suspend,
  427. .runtime_resume = omap2430_runtime_resume,
  428. };
  429. #define DEV_PM_OPS (&omap2430_pm_ops)
  430. #else
  431. #define DEV_PM_OPS NULL
  432. #endif
  433. static struct platform_driver omap2430_driver = {
  434. .probe = omap2430_probe,
  435. .remove = __devexit_p(omap2430_remove),
  436. .driver = {
  437. .name = "musb-omap2430",
  438. .pm = DEV_PM_OPS,
  439. },
  440. };
  441. MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
  442. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  443. MODULE_LICENSE("GPL v2");
  444. static int __init omap2430_init(void)
  445. {
  446. return platform_driver_register(&omap2430_driver);
  447. }
  448. module_init(omap2430_init);
  449. static void __exit omap2430_exit(void)
  450. {
  451. platform_driver_unregister(&omap2430_driver);
  452. }
  453. module_exit(omap2430_exit);