phy-omap-usb3.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #include <linux/of_platform.h>
  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. struct usb_dpll_map {
  56. unsigned long rate;
  57. struct usb_dpll_params params;
  58. };
  59. static struct usb_dpll_map dpll_map[] = {
  60. {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
  61. {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
  62. {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
  63. {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
  64. {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
  65. {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
  66. };
  67. static struct usb_dpll_params *omap_usb3_get_dpll_params(unsigned long rate)
  68. {
  69. int i;
  70. for (i = 0; i < ARRAY_SIZE(dpll_map); i++) {
  71. if (rate == dpll_map[i].rate)
  72. return &dpll_map[i].params;
  73. }
  74. return NULL;
  75. }
  76. static int omap_usb3_suspend(struct usb_phy *x, int suspend)
  77. {
  78. struct omap_usb *phy = phy_to_omapusb(x);
  79. int val;
  80. int timeout = PLL_IDLE_TIME;
  81. if (suspend && !phy->is_suspended) {
  82. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  83. val |= PLL_IDLE;
  84. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  85. do {
  86. val = omap_usb_readl(phy->pll_ctrl_base, PLL_STATUS);
  87. if (val & PLL_TICOPWDN)
  88. break;
  89. udelay(1);
  90. } while (--timeout);
  91. omap_control_usb_phy_power(phy->control_dev, 0);
  92. phy->is_suspended = 1;
  93. } else if (!suspend && phy->is_suspended) {
  94. phy->is_suspended = 0;
  95. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  96. val &= ~PLL_IDLE;
  97. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  98. do {
  99. val = omap_usb_readl(phy->pll_ctrl_base, PLL_STATUS);
  100. if (!(val & PLL_TICOPWDN))
  101. break;
  102. udelay(1);
  103. } while (--timeout);
  104. }
  105. return 0;
  106. }
  107. static void omap_usb_dpll_relock(struct omap_usb *phy)
  108. {
  109. u32 val;
  110. unsigned long timeout;
  111. omap_usb_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
  112. timeout = jiffies + msecs_to_jiffies(20);
  113. do {
  114. val = omap_usb_readl(phy->pll_ctrl_base, PLL_STATUS);
  115. if (val & PLL_LOCK)
  116. break;
  117. } while (!WARN_ON(time_after(jiffies, timeout)));
  118. }
  119. static int omap_usb_dpll_lock(struct omap_usb *phy)
  120. {
  121. u32 val;
  122. unsigned long rate;
  123. struct usb_dpll_params *dpll_params;
  124. rate = clk_get_rate(phy->sys_clk);
  125. dpll_params = omap_usb3_get_dpll_params(rate);
  126. if (!dpll_params) {
  127. dev_err(phy->dev,
  128. "No DPLL configuration for %lu Hz SYS CLK\n", rate);
  129. return -EINVAL;
  130. }
  131. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  132. val &= ~PLL_REGN_MASK;
  133. val |= dpll_params->n << PLL_REGN_SHIFT;
  134. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  135. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  136. val &= ~PLL_SELFREQDCO_MASK;
  137. val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
  138. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  139. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  140. val &= ~PLL_REGM_MASK;
  141. val |= dpll_params->m << PLL_REGM_SHIFT;
  142. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  143. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
  144. val &= ~PLL_REGM_F_MASK;
  145. val |= dpll_params->mf << PLL_REGM_F_SHIFT;
  146. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
  147. val = omap_usb_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
  148. val &= ~PLL_SD_MASK;
  149. val |= dpll_params->sd << PLL_SD_SHIFT;
  150. omap_usb_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
  151. omap_usb_dpll_relock(phy);
  152. return 0;
  153. }
  154. static int omap_usb3_init(struct usb_phy *x)
  155. {
  156. struct omap_usb *phy = phy_to_omapusb(x);
  157. int ret;
  158. ret = omap_usb_dpll_lock(phy);
  159. if (ret)
  160. return ret;
  161. omap_control_usb_phy_power(phy->control_dev, 1);
  162. return 0;
  163. }
  164. static int omap_usb3_probe(struct platform_device *pdev)
  165. {
  166. struct omap_usb *phy;
  167. struct resource *res;
  168. struct device_node *node = pdev->dev.of_node;
  169. struct device_node *control_node;
  170. struct platform_device *control_pdev;
  171. if (!node)
  172. return -EINVAL;
  173. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  174. if (!phy) {
  175. dev_err(&pdev->dev, "unable to alloc mem for OMAP USB3 PHY\n");
  176. return -ENOMEM;
  177. }
  178. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll_ctrl");
  179. phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res);
  180. if (IS_ERR(phy->pll_ctrl_base))
  181. return PTR_ERR(phy->pll_ctrl_base);
  182. phy->dev = &pdev->dev;
  183. phy->phy.dev = phy->dev;
  184. phy->phy.label = "omap-usb3";
  185. phy->phy.init = omap_usb3_init;
  186. phy->phy.set_suspend = omap_usb3_suspend;
  187. phy->phy.type = USB_PHY_TYPE_USB3;
  188. phy->is_suspended = 1;
  189. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  190. if (IS_ERR(phy->wkupclk)) {
  191. dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
  192. return PTR_ERR(phy->wkupclk);
  193. }
  194. clk_prepare(phy->wkupclk);
  195. phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
  196. if (IS_ERR(phy->optclk)) {
  197. dev_err(&pdev->dev, "unable to get usb_otg_ss_refclk960m\n");
  198. return PTR_ERR(phy->optclk);
  199. }
  200. clk_prepare(phy->optclk);
  201. phy->sys_clk = devm_clk_get(phy->dev, "sys_clkin");
  202. if (IS_ERR(phy->sys_clk)) {
  203. pr_err("%s: unable to get sys_clkin\n", __func__);
  204. return -EINVAL;
  205. }
  206. control_node = of_parse_phandle(node, "ctrl-module", 0);
  207. if (!control_node) {
  208. dev_err(&pdev->dev, "Failed to get control device phandle\n");
  209. return -EINVAL;
  210. }
  211. control_pdev = of_find_device_by_node(control_node);
  212. if (!control_pdev) {
  213. dev_err(&pdev->dev, "Failed to get control device\n");
  214. return -EINVAL;
  215. }
  216. phy->control_dev = &control_pdev->dev;
  217. omap_control_usb_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");