ux500.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2010 ST-Ericsson AB
  3. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  4. *
  5. * Based on omap2430.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/usb/musb-ux500.h>
  29. #include "musb_core.h"
  30. static struct musb_hdrc_config ux500_musb_hdrc_config = {
  31. .multipoint = true,
  32. .dyn_fifo = true,
  33. .num_eps = 16,
  34. .ram_bits = 16,
  35. };
  36. struct ux500_glue {
  37. struct device *dev;
  38. struct platform_device *musb;
  39. struct clk *clk;
  40. };
  41. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  42. static void ux500_musb_set_vbus(struct musb *musb, int is_on)
  43. {
  44. u8 devctl;
  45. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  46. /* HDRC controls CPEN, but beware current surges during device
  47. * connect. They can trigger transient overcurrent conditions
  48. * that must be ignored.
  49. */
  50. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  51. if (is_on) {
  52. if (musb->xceiv->state == OTG_STATE_A_IDLE) {
  53. /* start the session */
  54. devctl |= MUSB_DEVCTL_SESSION;
  55. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  56. /*
  57. * Wait for the musb to set as A device to enable the
  58. * VBUS
  59. */
  60. while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
  61. if (time_after(jiffies, timeout)) {
  62. dev_err(musb->controller,
  63. "configured as A device timeout");
  64. break;
  65. }
  66. }
  67. } else {
  68. musb->is_active = 1;
  69. musb->xceiv->otg->default_a = 1;
  70. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  71. devctl |= MUSB_DEVCTL_SESSION;
  72. MUSB_HST_MODE(musb);
  73. }
  74. } else {
  75. musb->is_active = 0;
  76. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
  77. * right to B_IDLE...
  78. */
  79. musb->xceiv->otg->default_a = 0;
  80. devctl &= ~MUSB_DEVCTL_SESSION;
  81. MUSB_DEV_MODE(musb);
  82. }
  83. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  84. /*
  85. * Devctl values will be updated after vbus goes below
  86. * session_valid. The time taken depends on the capacitance
  87. * on VBUS line. The max discharge time can be upto 1 sec
  88. * as per the spec. Typically on our platform, it is 200ms
  89. */
  90. if (!is_on)
  91. mdelay(200);
  92. dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
  93. usb_otg_state_string(musb->xceiv->state),
  94. musb_readb(musb->mregs, MUSB_DEVCTL));
  95. }
  96. static int musb_otg_notifications(struct notifier_block *nb,
  97. unsigned long event, void *unused)
  98. {
  99. struct musb *musb = container_of(nb, struct musb, nb);
  100. dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
  101. event, usb_otg_state_string(musb->xceiv->state));
  102. switch (event) {
  103. case UX500_MUSB_ID:
  104. dev_dbg(musb->controller, "ID GND\n");
  105. ux500_musb_set_vbus(musb, 1);
  106. break;
  107. case UX500_MUSB_VBUS:
  108. dev_dbg(musb->controller, "VBUS Connect\n");
  109. break;
  110. case UX500_MUSB_NONE:
  111. dev_dbg(musb->controller, "VBUS Disconnect\n");
  112. if (is_host_active(musb))
  113. ux500_musb_set_vbus(musb, 0);
  114. else
  115. musb->xceiv->state = OTG_STATE_B_IDLE;
  116. break;
  117. default:
  118. dev_dbg(musb->controller, "ID float\n");
  119. return NOTIFY_DONE;
  120. }
  121. return NOTIFY_OK;
  122. }
  123. static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
  124. {
  125. unsigned long flags;
  126. irqreturn_t retval = IRQ_NONE;
  127. struct musb *musb = __hci;
  128. spin_lock_irqsave(&musb->lock, flags);
  129. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  130. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  131. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  132. if (musb->int_usb || musb->int_tx || musb->int_rx)
  133. retval = musb_interrupt(musb);
  134. spin_unlock_irqrestore(&musb->lock, flags);
  135. return retval;
  136. }
  137. static int ux500_musb_init(struct musb *musb)
  138. {
  139. int status;
  140. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  141. if (IS_ERR_OR_NULL(musb->xceiv)) {
  142. pr_err("HS USB OTG: no transceiver configured\n");
  143. return -EPROBE_DEFER;
  144. }
  145. musb->nb.notifier_call = musb_otg_notifications;
  146. status = usb_register_notifier(musb->xceiv, &musb->nb);
  147. if (status < 0) {
  148. dev_dbg(musb->controller, "notification register failed\n");
  149. return status;
  150. }
  151. musb->isr = ux500_musb_interrupt;
  152. return 0;
  153. }
  154. static int ux500_musb_exit(struct musb *musb)
  155. {
  156. usb_unregister_notifier(musb->xceiv, &musb->nb);
  157. usb_put_phy(musb->xceiv);
  158. return 0;
  159. }
  160. static const struct musb_platform_ops ux500_ops = {
  161. .init = ux500_musb_init,
  162. .exit = ux500_musb_exit,
  163. .set_vbus = ux500_musb_set_vbus,
  164. };
  165. static int ux500_probe(struct platform_device *pdev)
  166. {
  167. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  168. struct platform_device *musb;
  169. struct ux500_glue *glue;
  170. struct clk *clk;
  171. int ret = -ENOMEM;
  172. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  173. if (!glue) {
  174. dev_err(&pdev->dev, "failed to allocate glue context\n");
  175. goto err0;
  176. }
  177. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  178. if (!musb) {
  179. dev_err(&pdev->dev, "failed to allocate musb device\n");
  180. goto err1;
  181. }
  182. clk = clk_get(&pdev->dev, "usb");
  183. if (IS_ERR(clk)) {
  184. dev_err(&pdev->dev, "failed to get clock\n");
  185. ret = PTR_ERR(clk);
  186. goto err3;
  187. }
  188. ret = clk_prepare_enable(clk);
  189. if (ret) {
  190. dev_err(&pdev->dev, "failed to enable clock\n");
  191. goto err4;
  192. }
  193. musb->dev.parent = &pdev->dev;
  194. musb->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  195. musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
  196. glue->dev = &pdev->dev;
  197. glue->musb = musb;
  198. glue->clk = clk;
  199. pdata->platform_ops = &ux500_ops;
  200. pdata->config = &ux500_musb_hdrc_config;
  201. platform_set_drvdata(pdev, glue);
  202. ret = platform_device_add_resources(musb, pdev->resource,
  203. pdev->num_resources);
  204. if (ret) {
  205. dev_err(&pdev->dev, "failed to add resources\n");
  206. goto err5;
  207. }
  208. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  209. if (ret) {
  210. dev_err(&pdev->dev, "failed to add platform_data\n");
  211. goto err5;
  212. }
  213. ret = platform_device_add(musb);
  214. if (ret) {
  215. dev_err(&pdev->dev, "failed to register musb device\n");
  216. goto err5;
  217. }
  218. return 0;
  219. err5:
  220. clk_disable_unprepare(clk);
  221. err4:
  222. clk_put(clk);
  223. err3:
  224. platform_device_put(musb);
  225. err1:
  226. kfree(glue);
  227. err0:
  228. return ret;
  229. }
  230. static int ux500_remove(struct platform_device *pdev)
  231. {
  232. struct ux500_glue *glue = platform_get_drvdata(pdev);
  233. platform_device_unregister(glue->musb);
  234. clk_disable_unprepare(glue->clk);
  235. clk_put(glue->clk);
  236. kfree(glue);
  237. return 0;
  238. }
  239. #ifdef CONFIG_PM
  240. static int ux500_suspend(struct device *dev)
  241. {
  242. struct ux500_glue *glue = dev_get_drvdata(dev);
  243. struct musb *musb = glue_to_musb(glue);
  244. usb_phy_set_suspend(musb->xceiv, 1);
  245. clk_disable_unprepare(glue->clk);
  246. return 0;
  247. }
  248. static int ux500_resume(struct device *dev)
  249. {
  250. struct ux500_glue *glue = dev_get_drvdata(dev);
  251. struct musb *musb = glue_to_musb(glue);
  252. int ret;
  253. ret = clk_prepare_enable(glue->clk);
  254. if (ret) {
  255. dev_err(dev, "failed to enable clock\n");
  256. return ret;
  257. }
  258. usb_phy_set_suspend(musb->xceiv, 0);
  259. return 0;
  260. }
  261. static const struct dev_pm_ops ux500_pm_ops = {
  262. .suspend = ux500_suspend,
  263. .resume = ux500_resume,
  264. };
  265. #define DEV_PM_OPS (&ux500_pm_ops)
  266. #else
  267. #define DEV_PM_OPS NULL
  268. #endif
  269. static struct platform_driver ux500_driver = {
  270. .probe = ux500_probe,
  271. .remove = ux500_remove,
  272. .driver = {
  273. .name = "musb-ux500",
  274. .pm = DEV_PM_OPS,
  275. },
  276. };
  277. MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
  278. MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
  279. MODULE_LICENSE("GPL v2");
  280. module_platform_driver(ux500_driver);