ux500.c 9.5 KB

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