omap2430.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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/of.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 <linux/delay.h>
  39. #include <linux/usb/musb.h>
  40. #include <linux/phy/omap_control_phy.h>
  41. #include <linux/of_platform.h>
  42. #include "musb_core.h"
  43. #include "omap2430.h"
  44. struct omap2430_glue {
  45. struct device *dev;
  46. struct platform_device *musb;
  47. enum musb_vbus_id_status status;
  48. struct work_struct omap_musb_mailbox_work;
  49. struct device *control_otghs;
  50. };
  51. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  52. static struct omap2430_glue *_glue;
  53. static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
  54. {
  55. struct usb_otg *otg = musb->xceiv->otg;
  56. u8 devctl;
  57. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  58. /* HDRC controls CPEN, but beware current surges during device
  59. * connect. They can trigger transient overcurrent conditions
  60. * that must be ignored.
  61. */
  62. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  63. if (is_on) {
  64. if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
  65. int loops = 100;
  66. /* start the session */
  67. devctl |= MUSB_DEVCTL_SESSION;
  68. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  69. /*
  70. * Wait for the musb to set as A device to enable the
  71. * VBUS
  72. */
  73. while (musb_readb(musb->mregs, MUSB_DEVCTL) &
  74. MUSB_DEVCTL_BDEVICE) {
  75. mdelay(5);
  76. cpu_relax();
  77. if (time_after(jiffies, timeout)
  78. || loops-- <= 0) {
  79. dev_err(musb->controller,
  80. "configured as A device timeout");
  81. break;
  82. }
  83. }
  84. otg_set_vbus(otg, 1);
  85. } else {
  86. musb->is_active = 1;
  87. otg->default_a = 1;
  88. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  89. devctl |= MUSB_DEVCTL_SESSION;
  90. MUSB_HST_MODE(musb);
  91. }
  92. } else {
  93. musb->is_active = 0;
  94. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
  95. * jumping right to B_IDLE...
  96. */
  97. otg->default_a = 0;
  98. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  99. devctl &= ~MUSB_DEVCTL_SESSION;
  100. MUSB_DEV_MODE(musb);
  101. }
  102. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  103. dev_dbg(musb->controller, "VBUS %s, devctl %02x "
  104. /* otg %3x conf %08x prcm %08x */ "\n",
  105. usb_otg_state_string(musb->xceiv->otg->state),
  106. musb_readb(musb->mregs, MUSB_DEVCTL));
  107. }
  108. static inline void omap2430_low_level_exit(struct musb *musb)
  109. {
  110. u32 l;
  111. /* in any role */
  112. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  113. l |= ENABLEFORCE; /* enable MSTANDBY */
  114. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  115. }
  116. static inline void omap2430_low_level_init(struct musb *musb)
  117. {
  118. u32 l;
  119. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  120. l &= ~ENABLEFORCE; /* disable MSTANDBY */
  121. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  122. }
  123. static int omap2430_musb_mailbox(enum musb_vbus_id_status status)
  124. {
  125. struct omap2430_glue *glue = _glue;
  126. if (!glue) {
  127. pr_err("%s: musb core is not yet initialized\n", __func__);
  128. return -EPROBE_DEFER;
  129. }
  130. glue->status = status;
  131. if (!glue_to_musb(glue)) {
  132. pr_err("%s: musb core is not yet ready\n", __func__);
  133. return -EPROBE_DEFER;
  134. }
  135. schedule_work(&glue->omap_musb_mailbox_work);
  136. return 0;
  137. }
  138. static void omap_musb_set_mailbox(struct omap2430_glue *glue)
  139. {
  140. struct musb *musb = glue_to_musb(glue);
  141. struct musb_hdrc_platform_data *pdata =
  142. dev_get_platdata(musb->controller);
  143. struct omap_musb_board_data *data = pdata->board_data;
  144. struct usb_otg *otg = musb->xceiv->otg;
  145. pm_runtime_get_sync(musb->controller);
  146. switch (glue->status) {
  147. case MUSB_ID_GROUND:
  148. dev_dbg(musb->controller, "ID GND\n");
  149. otg->default_a = true;
  150. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  151. musb->xceiv->last_event = USB_EVENT_ID;
  152. if (musb->gadget_driver) {
  153. omap_control_usb_set_mode(glue->control_otghs,
  154. USB_MODE_HOST);
  155. omap2430_musb_set_vbus(musb, 1);
  156. }
  157. break;
  158. case MUSB_VBUS_VALID:
  159. dev_dbg(musb->controller, "VBUS Connect\n");
  160. otg->default_a = false;
  161. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  162. musb->xceiv->last_event = USB_EVENT_VBUS;
  163. omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
  164. break;
  165. case MUSB_ID_FLOAT:
  166. case MUSB_VBUS_OFF:
  167. dev_dbg(musb->controller, "VBUS Disconnect\n");
  168. musb->xceiv->last_event = USB_EVENT_NONE;
  169. if (musb->gadget_driver)
  170. omap2430_musb_set_vbus(musb, 0);
  171. if (data->interface_type == MUSB_INTERFACE_UTMI)
  172. otg_set_vbus(musb->xceiv->otg, 0);
  173. omap_control_usb_set_mode(glue->control_otghs,
  174. USB_MODE_DISCONNECT);
  175. break;
  176. default:
  177. dev_dbg(musb->controller, "ID float\n");
  178. }
  179. pm_runtime_mark_last_busy(musb->controller);
  180. pm_runtime_put_autosuspend(musb->controller);
  181. atomic_notifier_call_chain(&musb->xceiv->notifier,
  182. musb->xceiv->last_event, NULL);
  183. }
  184. static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
  185. {
  186. struct omap2430_glue *glue = container_of(mailbox_work,
  187. struct omap2430_glue, omap_musb_mailbox_work);
  188. omap_musb_set_mailbox(glue);
  189. }
  190. static irqreturn_t omap2430_musb_interrupt(int irq, void *__hci)
  191. {
  192. unsigned long flags;
  193. irqreturn_t retval = IRQ_NONE;
  194. struct musb *musb = __hci;
  195. spin_lock_irqsave(&musb->lock, flags);
  196. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  197. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  198. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  199. if (musb->int_usb || musb->int_tx || musb->int_rx)
  200. retval = musb_interrupt(musb);
  201. spin_unlock_irqrestore(&musb->lock, flags);
  202. return retval;
  203. }
  204. static int omap2430_musb_init(struct musb *musb)
  205. {
  206. u32 l;
  207. int status = 0;
  208. struct device *dev = musb->controller;
  209. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  210. struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
  211. struct omap_musb_board_data *data = plat->board_data;
  212. /* We require some kind of external transceiver, hooked
  213. * up through ULPI. TWL4030-family PMICs include one,
  214. * which needs a driver, drivers aren't always needed.
  215. */
  216. if (dev->parent->of_node) {
  217. musb->phy = devm_phy_get(dev->parent, "usb2-phy");
  218. /* We can't totally remove musb->xceiv as of now because
  219. * musb core uses xceiv.state and xceiv.otg. Once we have
  220. * a separate state machine to handle otg, these can be moved
  221. * out of xceiv and then we can start using the generic PHY
  222. * framework
  223. */
  224. musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent,
  225. "usb-phy", 0);
  226. } else {
  227. musb->xceiv = devm_usb_get_phy_dev(dev, 0);
  228. musb->phy = devm_phy_get(dev, "usb");
  229. }
  230. if (IS_ERR(musb->xceiv)) {
  231. status = PTR_ERR(musb->xceiv);
  232. if (status == -ENXIO)
  233. return status;
  234. pr_err("HS USB OTG: no transceiver configured\n");
  235. return -EPROBE_DEFER;
  236. }
  237. if (IS_ERR(musb->phy)) {
  238. pr_err("HS USB OTG: no PHY configured\n");
  239. return PTR_ERR(musb->phy);
  240. }
  241. musb->isr = omap2430_musb_interrupt;
  242. phy_init(musb->phy);
  243. phy_power_on(musb->phy);
  244. l = musb_readl(musb->mregs, OTG_INTERFSEL);
  245. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  246. /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
  247. l &= ~ULPI_12PIN; /* Disable ULPI */
  248. l |= UTMI_8BIT; /* Enable UTMI */
  249. } else {
  250. l |= ULPI_12PIN;
  251. }
  252. musb_writel(musb->mregs, OTG_INTERFSEL, l);
  253. pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
  254. "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
  255. musb_readl(musb->mregs, OTG_REVISION),
  256. musb_readl(musb->mregs, OTG_SYSCONFIG),
  257. musb_readl(musb->mregs, OTG_SYSSTATUS),
  258. musb_readl(musb->mregs, OTG_INTERFSEL),
  259. musb_readl(musb->mregs, OTG_SIMENABLE));
  260. if (glue->status != MUSB_UNKNOWN)
  261. omap_musb_set_mailbox(glue);
  262. return 0;
  263. }
  264. static void omap2430_musb_enable(struct musb *musb)
  265. {
  266. u8 devctl;
  267. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  268. struct device *dev = musb->controller;
  269. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  270. struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
  271. struct omap_musb_board_data *data = pdata->board_data;
  272. switch (glue->status) {
  273. case MUSB_ID_GROUND:
  274. omap_control_usb_set_mode(glue->control_otghs, USB_MODE_HOST);
  275. if (data->interface_type != MUSB_INTERFACE_UTMI)
  276. break;
  277. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  278. /* start the session */
  279. devctl |= MUSB_DEVCTL_SESSION;
  280. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  281. while (musb_readb(musb->mregs, MUSB_DEVCTL) &
  282. MUSB_DEVCTL_BDEVICE) {
  283. cpu_relax();
  284. if (time_after(jiffies, timeout)) {
  285. dev_err(dev, "configured as A device timeout");
  286. break;
  287. }
  288. }
  289. break;
  290. case MUSB_VBUS_VALID:
  291. omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. static void omap2430_musb_disable(struct musb *musb)
  298. {
  299. struct device *dev = musb->controller;
  300. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  301. if (glue->status != MUSB_UNKNOWN)
  302. omap_control_usb_set_mode(glue->control_otghs,
  303. USB_MODE_DISCONNECT);
  304. }
  305. static int omap2430_musb_exit(struct musb *musb)
  306. {
  307. struct device *dev = musb->controller;
  308. struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
  309. omap2430_low_level_exit(musb);
  310. phy_power_off(musb->phy);
  311. phy_exit(musb->phy);
  312. musb->phy = NULL;
  313. cancel_work_sync(&glue->omap_musb_mailbox_work);
  314. return 0;
  315. }
  316. static const struct musb_platform_ops omap2430_ops = {
  317. .quirks = MUSB_DMA_INVENTRA,
  318. #ifdef CONFIG_USB_INVENTRA_DMA
  319. .dma_init = musbhs_dma_controller_create,
  320. .dma_exit = musbhs_dma_controller_destroy,
  321. #endif
  322. .init = omap2430_musb_init,
  323. .exit = omap2430_musb_exit,
  324. .set_vbus = omap2430_musb_set_vbus,
  325. .enable = omap2430_musb_enable,
  326. .disable = omap2430_musb_disable,
  327. .phy_callback = omap2430_musb_mailbox,
  328. };
  329. static u64 omap2430_dmamask = DMA_BIT_MASK(32);
  330. static int omap2430_probe(struct platform_device *pdev)
  331. {
  332. struct resource musb_resources[3];
  333. struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
  334. struct omap_musb_board_data *data;
  335. struct platform_device *musb;
  336. struct omap2430_glue *glue;
  337. struct device_node *np = pdev->dev.of_node;
  338. struct musb_hdrc_config *config;
  339. int ret = -ENOMEM, val;
  340. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  341. if (!glue)
  342. goto err0;
  343. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  344. if (!musb) {
  345. dev_err(&pdev->dev, "failed to allocate musb device\n");
  346. goto err0;
  347. }
  348. musb->dev.parent = &pdev->dev;
  349. musb->dev.dma_mask = &omap2430_dmamask;
  350. musb->dev.coherent_dma_mask = omap2430_dmamask;
  351. glue->dev = &pdev->dev;
  352. glue->musb = musb;
  353. glue->status = MUSB_UNKNOWN;
  354. glue->control_otghs = ERR_PTR(-ENODEV);
  355. if (np) {
  356. struct device_node *control_node;
  357. struct platform_device *control_pdev;
  358. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  359. if (!pdata)
  360. goto err2;
  361. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  362. if (!data)
  363. goto err2;
  364. config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
  365. if (!config)
  366. goto err2;
  367. of_property_read_u32(np, "mode", (u32 *)&pdata->mode);
  368. of_property_read_u32(np, "interface-type",
  369. (u32 *)&data->interface_type);
  370. of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
  371. of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
  372. of_property_read_u32(np, "power", (u32 *)&pdata->power);
  373. ret = of_property_read_u32(np, "multipoint", &val);
  374. if (!ret && val)
  375. config->multipoint = true;
  376. pdata->board_data = data;
  377. pdata->config = config;
  378. control_node = of_parse_phandle(np, "ctrl-module", 0);
  379. if (control_node) {
  380. control_pdev = of_find_device_by_node(control_node);
  381. if (!control_pdev) {
  382. dev_err(&pdev->dev, "Failed to get control device\n");
  383. ret = -EINVAL;
  384. goto err2;
  385. }
  386. glue->control_otghs = &control_pdev->dev;
  387. }
  388. }
  389. pdata->platform_ops = &omap2430_ops;
  390. platform_set_drvdata(pdev, glue);
  391. /*
  392. * REVISIT if we ever have two instances of the wrapper, we will be
  393. * in big trouble
  394. */
  395. _glue = glue;
  396. INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
  397. memset(musb_resources, 0x00, sizeof(*musb_resources) *
  398. ARRAY_SIZE(musb_resources));
  399. musb_resources[0].name = pdev->resource[0].name;
  400. musb_resources[0].start = pdev->resource[0].start;
  401. musb_resources[0].end = pdev->resource[0].end;
  402. musb_resources[0].flags = pdev->resource[0].flags;
  403. musb_resources[1].name = pdev->resource[1].name;
  404. musb_resources[1].start = pdev->resource[1].start;
  405. musb_resources[1].end = pdev->resource[1].end;
  406. musb_resources[1].flags = pdev->resource[1].flags;
  407. musb_resources[2].name = pdev->resource[2].name;
  408. musb_resources[2].start = pdev->resource[2].start;
  409. musb_resources[2].end = pdev->resource[2].end;
  410. musb_resources[2].flags = pdev->resource[2].flags;
  411. ret = platform_device_add_resources(musb, musb_resources,
  412. ARRAY_SIZE(musb_resources));
  413. if (ret) {
  414. dev_err(&pdev->dev, "failed to add resources\n");
  415. goto err2;
  416. }
  417. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  418. if (ret) {
  419. dev_err(&pdev->dev, "failed to add platform_data\n");
  420. goto err2;
  421. }
  422. pm_runtime_enable(glue->dev);
  423. ret = platform_device_add(musb);
  424. if (ret) {
  425. dev_err(&pdev->dev, "failed to register musb device\n");
  426. goto err3;
  427. }
  428. return 0;
  429. err3:
  430. pm_runtime_disable(glue->dev);
  431. err2:
  432. platform_device_put(musb);
  433. err0:
  434. return ret;
  435. }
  436. static int omap2430_remove(struct platform_device *pdev)
  437. {
  438. struct omap2430_glue *glue = platform_get_drvdata(pdev);
  439. platform_device_unregister(glue->musb);
  440. pm_runtime_disable(glue->dev);
  441. return 0;
  442. }
  443. #ifdef CONFIG_PM
  444. static int omap2430_runtime_suspend(struct device *dev)
  445. {
  446. struct omap2430_glue *glue = dev_get_drvdata(dev);
  447. struct musb *musb = glue_to_musb(glue);
  448. if (!musb)
  449. return 0;
  450. musb->context.otg_interfsel = musb_readl(musb->mregs,
  451. OTG_INTERFSEL);
  452. omap2430_low_level_exit(musb);
  453. return 0;
  454. }
  455. static int omap2430_runtime_resume(struct device *dev)
  456. {
  457. struct omap2430_glue *glue = dev_get_drvdata(dev);
  458. struct musb *musb = glue_to_musb(glue);
  459. if (!musb)
  460. return 0;
  461. omap2430_low_level_init(musb);
  462. musb_writel(musb->mregs, OTG_INTERFSEL,
  463. musb->context.otg_interfsel);
  464. return 0;
  465. }
  466. static struct dev_pm_ops omap2430_pm_ops = {
  467. .runtime_suspend = omap2430_runtime_suspend,
  468. .runtime_resume = omap2430_runtime_resume,
  469. };
  470. #define DEV_PM_OPS (&omap2430_pm_ops)
  471. #else
  472. #define DEV_PM_OPS NULL
  473. #endif
  474. #ifdef CONFIG_OF
  475. static const struct of_device_id omap2430_id_table[] = {
  476. {
  477. .compatible = "ti,omap4-musb"
  478. },
  479. {
  480. .compatible = "ti,omap3-musb"
  481. },
  482. {},
  483. };
  484. MODULE_DEVICE_TABLE(of, omap2430_id_table);
  485. #endif
  486. static struct platform_driver omap2430_driver = {
  487. .probe = omap2430_probe,
  488. .remove = omap2430_remove,
  489. .driver = {
  490. .name = "musb-omap2430",
  491. .pm = DEV_PM_OPS,
  492. .of_match_table = of_match_ptr(omap2430_id_table),
  493. },
  494. };
  495. module_platform_driver(omap2430_driver);
  496. MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
  497. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  498. MODULE_LICENSE("GPL v2");