omap-usb3.c 8.9 KB

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