omap2430.c 15 KB

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