omap2430.c 14 KB

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