omap2430.c 16 KB

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