omap2430.c 15 KB

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