omap2430.c 13 KB

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