ux500.c 8.8 KB

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