ux500.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2010 ST-Ericsson AB
  4. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  5. *
  6. * Based on omap2430.c
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/usb/musb-ux500.h>
  16. #include "musb_core.h"
  17. static const struct musb_hdrc_config ux500_musb_hdrc_config = {
  18. .multipoint = true,
  19. .dyn_fifo = true,
  20. .num_eps = 16,
  21. .ram_bits = 16,
  22. };
  23. struct ux500_glue {
  24. struct device *dev;
  25. struct platform_device *musb;
  26. struct clk *clk;
  27. };
  28. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  29. static void ux500_musb_set_vbus(struct musb *musb, int is_on)
  30. {
  31. u8 devctl;
  32. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  33. /* HDRC controls CPEN, but beware current surges during device
  34. * connect. They can trigger transient overcurrent conditions
  35. * that must be ignored.
  36. */
  37. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  38. if (is_on) {
  39. if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
  40. /* start the session */
  41. devctl |= MUSB_DEVCTL_SESSION;
  42. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  43. /*
  44. * Wait for the musb to set as A device to enable the
  45. * VBUS
  46. */
  47. while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
  48. if (time_after(jiffies, timeout)) {
  49. dev_err(musb->controller,
  50. "configured as A device timeout");
  51. break;
  52. }
  53. }
  54. } else {
  55. musb->is_active = 1;
  56. musb->xceiv->otg->default_a = 1;
  57. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  58. devctl |= MUSB_DEVCTL_SESSION;
  59. MUSB_HST_MODE(musb);
  60. }
  61. } else {
  62. musb->is_active = 0;
  63. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
  64. * right to B_IDLE...
  65. */
  66. musb->xceiv->otg->default_a = 0;
  67. devctl &= ~MUSB_DEVCTL_SESSION;
  68. MUSB_DEV_MODE(musb);
  69. }
  70. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  71. /*
  72. * Devctl values will be updated after vbus goes below
  73. * session_valid. The time taken depends on the capacitance
  74. * on VBUS line. The max discharge time can be upto 1 sec
  75. * as per the spec. Typically on our platform, it is 200ms
  76. */
  77. if (!is_on)
  78. mdelay(200);
  79. dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
  80. usb_otg_state_string(musb->xceiv->otg->state),
  81. musb_readb(musb->mregs, MUSB_DEVCTL));
  82. }
  83. static int musb_otg_notifications(struct notifier_block *nb,
  84. unsigned long event, void *unused)
  85. {
  86. struct musb *musb = container_of(nb, struct musb, nb);
  87. dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
  88. event, usb_otg_state_string(musb->xceiv->otg->state));
  89. switch (event) {
  90. case UX500_MUSB_ID:
  91. dev_dbg(musb->controller, "ID GND\n");
  92. ux500_musb_set_vbus(musb, 1);
  93. break;
  94. case UX500_MUSB_VBUS:
  95. dev_dbg(musb->controller, "VBUS Connect\n");
  96. break;
  97. case UX500_MUSB_NONE:
  98. dev_dbg(musb->controller, "VBUS Disconnect\n");
  99. if (is_host_active(musb))
  100. ux500_musb_set_vbus(musb, 0);
  101. else
  102. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  103. break;
  104. default:
  105. dev_dbg(musb->controller, "ID float\n");
  106. return NOTIFY_DONE;
  107. }
  108. return NOTIFY_OK;
  109. }
  110. static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
  111. {
  112. unsigned long flags;
  113. irqreturn_t retval = IRQ_NONE;
  114. struct musb *musb = __hci;
  115. spin_lock_irqsave(&musb->lock, flags);
  116. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  117. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  118. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  119. if (musb->int_usb || musb->int_tx || musb->int_rx)
  120. retval = musb_interrupt(musb);
  121. spin_unlock_irqrestore(&musb->lock, flags);
  122. return retval;
  123. }
  124. static int ux500_musb_init(struct musb *musb)
  125. {
  126. int status;
  127. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  128. if (IS_ERR_OR_NULL(musb->xceiv)) {
  129. pr_err("HS USB OTG: no transceiver configured\n");
  130. return -EPROBE_DEFER;
  131. }
  132. musb->nb.notifier_call = musb_otg_notifications;
  133. status = usb_register_notifier(musb->xceiv, &musb->nb);
  134. if (status < 0) {
  135. dev_dbg(musb->controller, "notification register failed\n");
  136. return status;
  137. }
  138. musb->isr = ux500_musb_interrupt;
  139. return 0;
  140. }
  141. static int ux500_musb_exit(struct musb *musb)
  142. {
  143. usb_unregister_notifier(musb->xceiv, &musb->nb);
  144. usb_put_phy(musb->xceiv);
  145. return 0;
  146. }
  147. static const struct musb_platform_ops ux500_ops = {
  148. .quirks = MUSB_DMA_UX500 | MUSB_INDEXED_EP,
  149. #ifdef CONFIG_USB_UX500_DMA
  150. .dma_init = ux500_dma_controller_create,
  151. .dma_exit = ux500_dma_controller_destroy,
  152. #endif
  153. .init = ux500_musb_init,
  154. .exit = ux500_musb_exit,
  155. .fifo_mode = 5,
  156. .set_vbus = ux500_musb_set_vbus,
  157. };
  158. static struct musb_hdrc_platform_data *
  159. ux500_of_probe(struct platform_device *pdev, struct device_node *np)
  160. {
  161. struct musb_hdrc_platform_data *pdata;
  162. const char *mode;
  163. int strlen;
  164. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  165. if (!pdata)
  166. return NULL;
  167. mode = of_get_property(np, "dr_mode", &strlen);
  168. if (!mode) {
  169. dev_err(&pdev->dev, "No 'dr_mode' property found\n");
  170. return NULL;
  171. }
  172. if (strlen > 0) {
  173. if (!strcmp(mode, "host"))
  174. pdata->mode = MUSB_HOST;
  175. if (!strcmp(mode, "otg"))
  176. pdata->mode = MUSB_OTG;
  177. if (!strcmp(mode, "peripheral"))
  178. pdata->mode = MUSB_PERIPHERAL;
  179. }
  180. return pdata;
  181. }
  182. static int ux500_probe(struct platform_device *pdev)
  183. {
  184. struct resource musb_resources[2];
  185. struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
  186. struct device_node *np = pdev->dev.of_node;
  187. struct platform_device *musb;
  188. struct ux500_glue *glue;
  189. struct clk *clk;
  190. int ret = -ENOMEM;
  191. if (!pdata) {
  192. if (np) {
  193. pdata = ux500_of_probe(pdev, np);
  194. if (!pdata)
  195. goto err0;
  196. pdev->dev.platform_data = pdata;
  197. } else {
  198. dev_err(&pdev->dev, "no pdata or device tree found\n");
  199. goto err0;
  200. }
  201. }
  202. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  203. if (!glue)
  204. goto err0;
  205. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  206. if (!musb) {
  207. dev_err(&pdev->dev, "failed to allocate musb device\n");
  208. goto err0;
  209. }
  210. clk = devm_clk_get(&pdev->dev, NULL);
  211. if (IS_ERR(clk)) {
  212. dev_err(&pdev->dev, "failed to get clock\n");
  213. ret = PTR_ERR(clk);
  214. goto err1;
  215. }
  216. ret = clk_prepare_enable(clk);
  217. if (ret) {
  218. dev_err(&pdev->dev, "failed to enable clock\n");
  219. goto err1;
  220. }
  221. musb->dev.parent = &pdev->dev;
  222. musb->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  223. musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
  224. glue->dev = &pdev->dev;
  225. glue->musb = musb;
  226. glue->clk = clk;
  227. pdata->platform_ops = &ux500_ops;
  228. pdata->config = &ux500_musb_hdrc_config;
  229. platform_set_drvdata(pdev, glue);
  230. memset(musb_resources, 0x00, sizeof(*musb_resources) *
  231. ARRAY_SIZE(musb_resources));
  232. musb_resources[0].name = pdev->resource[0].name;
  233. musb_resources[0].start = pdev->resource[0].start;
  234. musb_resources[0].end = pdev->resource[0].end;
  235. musb_resources[0].flags = pdev->resource[0].flags;
  236. musb_resources[1].name = pdev->resource[1].name;
  237. musb_resources[1].start = pdev->resource[1].start;
  238. musb_resources[1].end = pdev->resource[1].end;
  239. musb_resources[1].flags = pdev->resource[1].flags;
  240. ret = platform_device_add_resources(musb, musb_resources,
  241. ARRAY_SIZE(musb_resources));
  242. if (ret) {
  243. dev_err(&pdev->dev, "failed to add resources\n");
  244. goto err2;
  245. }
  246. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  247. if (ret) {
  248. dev_err(&pdev->dev, "failed to add platform_data\n");
  249. goto err2;
  250. }
  251. ret = platform_device_add(musb);
  252. if (ret) {
  253. dev_err(&pdev->dev, "failed to register musb device\n");
  254. goto err2;
  255. }
  256. return 0;
  257. err2:
  258. clk_disable_unprepare(clk);
  259. err1:
  260. platform_device_put(musb);
  261. err0:
  262. return ret;
  263. }
  264. static int ux500_remove(struct platform_device *pdev)
  265. {
  266. struct ux500_glue *glue = platform_get_drvdata(pdev);
  267. platform_device_unregister(glue->musb);
  268. clk_disable_unprepare(glue->clk);
  269. return 0;
  270. }
  271. #ifdef CONFIG_PM_SLEEP
  272. static int ux500_suspend(struct device *dev)
  273. {
  274. struct ux500_glue *glue = dev_get_drvdata(dev);
  275. struct musb *musb = glue_to_musb(glue);
  276. if (musb)
  277. usb_phy_set_suspend(musb->xceiv, 1);
  278. clk_disable_unprepare(glue->clk);
  279. return 0;
  280. }
  281. static int ux500_resume(struct device *dev)
  282. {
  283. struct ux500_glue *glue = dev_get_drvdata(dev);
  284. struct musb *musb = glue_to_musb(glue);
  285. int ret;
  286. ret = clk_prepare_enable(glue->clk);
  287. if (ret) {
  288. dev_err(dev, "failed to enable clock\n");
  289. return ret;
  290. }
  291. if (musb)
  292. usb_phy_set_suspend(musb->xceiv, 0);
  293. return 0;
  294. }
  295. #endif
  296. static SIMPLE_DEV_PM_OPS(ux500_pm_ops, ux500_suspend, ux500_resume);
  297. static const struct of_device_id ux500_match[] = {
  298. { .compatible = "stericsson,db8500-musb", },
  299. {}
  300. };
  301. MODULE_DEVICE_TABLE(of, ux500_match);
  302. static struct platform_driver ux500_driver = {
  303. .probe = ux500_probe,
  304. .remove = ux500_remove,
  305. .driver = {
  306. .name = "musb-ux500",
  307. .pm = &ux500_pm_ops,
  308. .of_match_table = ux500_match,
  309. },
  310. };
  311. MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
  312. MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
  313. MODULE_LICENSE("GPL v2");
  314. module_platform_driver(ux500_driver);