phy-omap-usb3.c 9.0 KB

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