phy-omap-usb3.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * omap-usb3 - USB PHY, talking to dwc3 controller in OMAP.
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  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. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb/omap_usb.h>
  22. #include <linux/of.h>
  23. #include <linux/clk.h>
  24. #include <linux/err.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/delay.h>
  27. #include <linux/usb/omap_control_usb.h>
  28. #define PLL_STATUS 0x00000004
  29. #define PLL_GO 0x00000008
  30. #define PLL_CONFIGURATION1 0x0000000C
  31. #define PLL_CONFIGURATION2 0x00000010
  32. #define PLL_CONFIGURATION3 0x00000014
  33. #define PLL_CONFIGURATION4 0x00000020
  34. #define PLL_REGM_MASK 0x001FFE00
  35. #define PLL_REGM_SHIFT 0x9
  36. #define PLL_REGM_F_MASK 0x0003FFFF
  37. #define PLL_REGM_F_SHIFT 0x0
  38. #define PLL_REGN_MASK 0x000001FE
  39. #define PLL_REGN_SHIFT 0x1
  40. #define PLL_SELFREQDCO_MASK 0x0000000E
  41. #define PLL_SELFREQDCO_SHIFT 0x1
  42. #define PLL_SD_MASK 0x0003FC00
  43. #define PLL_SD_SHIFT 0x9
  44. #define SET_PLL_GO 0x1
  45. #define PLL_TICOPWDN 0x10000
  46. #define PLL_LOCK 0x2
  47. #define PLL_IDLE 0x1
  48. /*
  49. * This is an Empirical value that works, need to confirm the actual
  50. * value required for the USB3PHY_PLL_CONFIGURATION2.PLL_IDLE status
  51. * to be correctly reflected in the USB3PHY_PLL_STATUS register.
  52. */
  53. # define PLL_IDLE_TIME 100;
  54. struct usb_dpll_map {
  55. unsigned long rate;
  56. struct usb_dpll_params params;
  57. };
  58. static struct usb_dpll_map dpll_map[] = {
  59. {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
  60. {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
  61. {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
  62. {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
  63. {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
  64. {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
  65. };
  66. static struct usb_dpll_params *omap_usb3_get_dpll_params(unsigned long rate)
  67. {
  68. int i;
  69. for (i = 0; i < ARRAY_SIZE(dpll_map); i++) {
  70. if (rate == dpll_map[i].rate)
  71. return &dpll_map[i].params;
  72. }
  73. return 0;
  74. }
  75. static int omap_usb3_suspend(struct usb_phy *x, int suspend)
  76. {
  77. struct omap_usb *phy = phy_to_omapusb(x);
  78. int val;
  79. int timeout = PLL_IDLE_TIME;
  80. if (suspend && !phy->is_suspended) {
  81. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  82. val |= PLL_IDLE;
  83. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  84. do {
  85. val = omap_usb_readl(phy->pll_ctrl_base, PLL_STATUS);
  86. if (val & PLL_TICOPWDN)
  87. break;
  88. udelay(1);
  89. } while (--timeout);
  90. omap_control_usb3_phy_power(phy->control_dev, 0);
  91. phy->is_suspended = 1;
  92. } else if (!suspend && phy->is_suspended) {
  93. phy->is_suspended = 0;
  94. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  95. val &= ~PLL_IDLE;
  96. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  97. do {
  98. val = omap_usb_readl(phy->pll_ctrl_base, PLL_STATUS);
  99. if (!(val & PLL_TICOPWDN))
  100. break;
  101. udelay(1);
  102. } while (--timeout);
  103. }
  104. return 0;
  105. }
  106. static void omap_usb_dpll_relock(struct omap_usb *phy)
  107. {
  108. u32 val;
  109. unsigned long timeout;
  110. omap_usb_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
  111. timeout = jiffies + msecs_to_jiffies(20);
  112. do {
  113. val = omap_usb_readl(phy->pll_ctrl_base, PLL_STATUS);
  114. if (val & PLL_LOCK)
  115. break;
  116. } while (!WARN_ON(time_after(jiffies, timeout)));
  117. }
  118. static int omap_usb_dpll_lock(struct omap_usb *phy)
  119. {
  120. u32 val;
  121. unsigned long rate;
  122. struct usb_dpll_params *dpll_params;
  123. rate = clk_get_rate(phy->sys_clk);
  124. dpll_params = omap_usb3_get_dpll_params(rate);
  125. if (!dpll_params) {
  126. dev_err(phy->dev,
  127. "No DPLL configuration for %lu Hz SYS CLK\n", rate);
  128. return -EINVAL;
  129. }
  130. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  131. val &= ~PLL_REGN_MASK;
  132. val |= dpll_params->n << PLL_REGN_SHIFT;
  133. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  134. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  135. val &= ~PLL_SELFREQDCO_MASK;
  136. val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
  137. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  138. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  139. val &= ~PLL_REGM_MASK;
  140. val |= dpll_params->m << PLL_REGM_SHIFT;
  141. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  142. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
  143. val &= ~PLL_REGM_F_MASK;
  144. val |= dpll_params->mf << PLL_REGM_F_SHIFT;
  145. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
  146. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
  147. val &= ~PLL_SD_MASK;
  148. val |= dpll_params->sd << PLL_SD_SHIFT;
  149. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
  150. omap_usb_dpll_relock(phy);
  151. return 0;
  152. }
  153. static int omap_usb3_init(struct usb_phy *x)
  154. {
  155. struct omap_usb *phy = phy_to_omapusb(x);
  156. int ret;
  157. ret = omap_usb_dpll_lock(phy);
  158. if (ret)
  159. return ret;
  160. omap_control_usb3_phy_power(phy->control_dev, 1);
  161. return 0;
  162. }
  163. static int omap_usb3_probe(struct platform_device *pdev)
  164. {
  165. struct omap_usb *phy;
  166. struct resource *res;
  167. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  168. if (!phy) {
  169. dev_err(&pdev->dev, "unable to alloc mem for OMAP USB3 PHY\n");
  170. return -ENOMEM;
  171. }
  172. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll_ctrl");
  173. phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res);
  174. if (IS_ERR(phy->pll_ctrl_base))
  175. return PTR_ERR(phy->pll_ctrl_base);
  176. phy->dev = &pdev->dev;
  177. phy->phy.dev = phy->dev;
  178. phy->phy.label = "omap-usb3";
  179. phy->phy.init = omap_usb3_init;
  180. phy->phy.set_suspend = omap_usb3_suspend;
  181. phy->phy.type = USB_PHY_TYPE_USB3;
  182. phy->is_suspended = 1;
  183. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  184. if (IS_ERR(phy->wkupclk)) {
  185. dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
  186. return PTR_ERR(phy->wkupclk);
  187. }
  188. clk_prepare(phy->wkupclk);
  189. phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
  190. if (IS_ERR(phy->optclk)) {
  191. dev_err(&pdev->dev, "unable to get usb_otg_ss_refclk960m\n");
  192. return PTR_ERR(phy->optclk);
  193. }
  194. clk_prepare(phy->optclk);
  195. phy->sys_clk = devm_clk_get(phy->dev, "sys_clkin");
  196. if (IS_ERR(phy->sys_clk)) {
  197. pr_err("%s: unable to get sys_clkin\n", __func__);
  198. return -EINVAL;
  199. }
  200. phy->control_dev = omap_get_control_dev();
  201. if (IS_ERR(phy->control_dev)) {
  202. dev_dbg(&pdev->dev, "Failed to get control device\n");
  203. return -ENODEV;
  204. }
  205. omap_control_usb3_phy_power(phy->control_dev, 0);
  206. usb_add_phy_dev(&phy->phy);
  207. platform_set_drvdata(pdev, phy);
  208. pm_runtime_enable(phy->dev);
  209. pm_runtime_get(&pdev->dev);
  210. return 0;
  211. }
  212. static int omap_usb3_remove(struct platform_device *pdev)
  213. {
  214. struct omap_usb *phy = platform_get_drvdata(pdev);
  215. clk_unprepare(phy->wkupclk);
  216. clk_unprepare(phy->optclk);
  217. usb_remove_phy(&phy->phy);
  218. if (!pm_runtime_suspended(&pdev->dev))
  219. pm_runtime_put(&pdev->dev);
  220. pm_runtime_disable(&pdev->dev);
  221. return 0;
  222. }
  223. #ifdef CONFIG_PM_RUNTIME
  224. static int omap_usb3_runtime_suspend(struct device *dev)
  225. {
  226. struct platform_device *pdev = to_platform_device(dev);
  227. struct omap_usb *phy = platform_get_drvdata(pdev);
  228. clk_disable(phy->wkupclk);
  229. clk_disable(phy->optclk);
  230. return 0;
  231. }
  232. static int omap_usb3_runtime_resume(struct device *dev)
  233. {
  234. u32 ret = 0;
  235. struct platform_device *pdev = to_platform_device(dev);
  236. struct omap_usb *phy = platform_get_drvdata(pdev);
  237. ret = clk_enable(phy->optclk);
  238. if (ret) {
  239. dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
  240. goto err1;
  241. }
  242. ret = clk_enable(phy->wkupclk);
  243. if (ret) {
  244. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  245. goto err2;
  246. }
  247. return 0;
  248. err2:
  249. clk_disable(phy->optclk);
  250. err1:
  251. return ret;
  252. }
  253. static const struct dev_pm_ops omap_usb3_pm_ops = {
  254. SET_RUNTIME_PM_OPS(omap_usb3_runtime_suspend, omap_usb3_runtime_resume,
  255. NULL)
  256. };
  257. #define DEV_PM_OPS (&omap_usb3_pm_ops)
  258. #else
  259. #define DEV_PM_OPS NULL
  260. #endif
  261. #ifdef CONFIG_OF
  262. static const struct of_device_id omap_usb3_id_table[] = {
  263. { .compatible = "ti,omap-usb3" },
  264. {}
  265. };
  266. MODULE_DEVICE_TABLE(of, omap_usb3_id_table);
  267. #endif
  268. static struct platform_driver omap_usb3_driver = {
  269. .probe = omap_usb3_probe,
  270. .remove = omap_usb3_remove,
  271. .driver = {
  272. .name = "omap-usb3",
  273. .owner = THIS_MODULE,
  274. .pm = DEV_PM_OPS,
  275. .of_match_table = of_match_ptr(omap_usb3_id_table),
  276. },
  277. };
  278. module_platform_driver(omap_usb3_driver);
  279. MODULE_ALIAS("platform: omap_usb3");
  280. MODULE_AUTHOR("Texas Instruments Inc.");
  281. MODULE_DESCRIPTION("OMAP USB3 phy driver");
  282. MODULE_LICENSE("GPL v2");