phy-stm32-usbphyc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
  4. */
  5. #include <common.h>
  6. #include <clk.h>
  7. #include <div64.h>
  8. #include <dm.h>
  9. #include <fdtdec.h>
  10. #include <generic-phy.h>
  11. #include <reset.h>
  12. #include <syscon.h>
  13. #include <usb.h>
  14. #include <asm/io.h>
  15. #include <linux/bitops.h>
  16. #include <power/regulator.h>
  17. /* USBPHYC registers */
  18. #define STM32_USBPHYC_PLL 0x0
  19. #define STM32_USBPHYC_MISC 0x8
  20. /* STM32_USBPHYC_PLL bit fields */
  21. #define PLLNDIV GENMASK(6, 0)
  22. #define PLLNDIV_SHIFT 0
  23. #define PLLFRACIN GENMASK(25, 10)
  24. #define PLLFRACIN_SHIFT 10
  25. #define PLLEN BIT(26)
  26. #define PLLSTRB BIT(27)
  27. #define PLLSTRBYP BIT(28)
  28. #define PLLFRACCTL BIT(29)
  29. #define PLLDITHEN0 BIT(30)
  30. #define PLLDITHEN1 BIT(31)
  31. /* STM32_USBPHYC_MISC bit fields */
  32. #define SWITHOST BIT(0)
  33. #define MAX_PHYS 2
  34. #define PLL_LOCK_TIME_US 100
  35. #define PLL_PWR_DOWN_TIME_US 5
  36. #define PLL_FVCO 2880 /* in MHz */
  37. #define PLL_INFF_MIN_RATE 19200000 /* in Hz */
  38. #define PLL_INFF_MAX_RATE 38400000 /* in Hz */
  39. struct pll_params {
  40. u8 ndiv;
  41. u16 frac;
  42. };
  43. struct stm32_usbphyc {
  44. fdt_addr_t base;
  45. struct clk clk;
  46. struct stm32_usbphyc_phy {
  47. struct udevice *vdd;
  48. struct udevice *vdda1v1;
  49. struct udevice *vdda1v8;
  50. int index;
  51. bool init;
  52. bool powered;
  53. } phys[MAX_PHYS];
  54. };
  55. void stm32_usbphyc_get_pll_params(u32 clk_rate, struct pll_params *pll_params)
  56. {
  57. unsigned long long fvco, ndiv, frac;
  58. /*
  59. * | FVCO = INFF*2*(NDIV + FRACT/2^16 ) when DITHER_DISABLE[1] = 1
  60. * | FVCO = 2880MHz
  61. * | NDIV = integer part of input bits to set the LDF
  62. * | FRACT = fractional part of input bits to set the LDF
  63. * => PLLNDIV = integer part of (FVCO / (INFF*2))
  64. * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
  65. * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
  66. */
  67. fvco = (unsigned long long)PLL_FVCO * 1000000; /* In Hz */
  68. ndiv = fvco;
  69. do_div(ndiv, (clk_rate * 2));
  70. pll_params->ndiv = (u8)ndiv;
  71. frac = fvco * (1 << 16);
  72. do_div(frac, (clk_rate * 2));
  73. frac = frac - (ndiv * (1 << 16));
  74. pll_params->frac = (u16)frac;
  75. }
  76. static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
  77. {
  78. struct pll_params pll_params;
  79. u32 clk_rate = clk_get_rate(&usbphyc->clk);
  80. u32 usbphyc_pll;
  81. if ((clk_rate < PLL_INFF_MIN_RATE) || (clk_rate > PLL_INFF_MAX_RATE)) {
  82. pr_debug("%s: input clk freq (%dHz) out of range\n",
  83. __func__, clk_rate);
  84. return -EINVAL;
  85. }
  86. stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
  87. usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP;
  88. usbphyc_pll |= ((pll_params.ndiv << PLLNDIV_SHIFT) & PLLNDIV);
  89. if (pll_params.frac) {
  90. usbphyc_pll |= PLLFRACCTL;
  91. usbphyc_pll |= ((pll_params.frac << PLLFRACIN_SHIFT)
  92. & PLLFRACIN);
  93. }
  94. writel(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
  95. pr_debug("%s: input clk freq=%dHz, ndiv=%d, frac=%d\n", __func__,
  96. clk_rate, pll_params.ndiv, pll_params.frac);
  97. return 0;
  98. }
  99. static bool stm32_usbphyc_is_init(struct stm32_usbphyc *usbphyc)
  100. {
  101. int i;
  102. for (i = 0; i < MAX_PHYS; i++) {
  103. if (usbphyc->phys[i].init)
  104. return true;
  105. }
  106. return false;
  107. }
  108. static bool stm32_usbphyc_is_powered(struct stm32_usbphyc *usbphyc)
  109. {
  110. int i;
  111. for (i = 0; i < MAX_PHYS; i++) {
  112. if (usbphyc->phys[i].powered)
  113. return true;
  114. }
  115. return false;
  116. }
  117. static int stm32_usbphyc_phy_init(struct phy *phy)
  118. {
  119. struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
  120. struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
  121. bool pllen = readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN ?
  122. true : false;
  123. int ret;
  124. pr_debug("%s phy ID = %lu\n", __func__, phy->id);
  125. /* Check if one phy port has already configured the pll */
  126. if (pllen && stm32_usbphyc_is_init(usbphyc))
  127. goto initialized;
  128. if (pllen) {
  129. clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
  130. udelay(PLL_PWR_DOWN_TIME_US);
  131. }
  132. ret = stm32_usbphyc_pll_init(usbphyc);
  133. if (ret)
  134. return ret;
  135. setbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
  136. /*
  137. * We must wait PLL_LOCK_TIME_US before checking that PLLEN
  138. * bit is still set
  139. */
  140. udelay(PLL_LOCK_TIME_US);
  141. if (!(readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN))
  142. return -EIO;
  143. initialized:
  144. usbphyc_phy->init = true;
  145. return 0;
  146. }
  147. static int stm32_usbphyc_phy_exit(struct phy *phy)
  148. {
  149. struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
  150. struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
  151. pr_debug("%s phy ID = %lu\n", __func__, phy->id);
  152. usbphyc_phy->init = false;
  153. /* Check if other phy port requires pllen */
  154. if (stm32_usbphyc_is_init(usbphyc))
  155. return 0;
  156. clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
  157. /*
  158. * We must wait PLL_PWR_DOWN_TIME_US before checking that PLLEN
  159. * bit is still clear
  160. */
  161. udelay(PLL_PWR_DOWN_TIME_US);
  162. if (readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN)
  163. return -EIO;
  164. return 0;
  165. }
  166. static int stm32_usbphyc_phy_power_on(struct phy *phy)
  167. {
  168. struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
  169. struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
  170. int ret;
  171. pr_debug("%s phy ID = %lu\n", __func__, phy->id);
  172. if (usbphyc_phy->vdda1v1) {
  173. ret = regulator_set_enable(usbphyc_phy->vdda1v1, true);
  174. if (ret)
  175. return ret;
  176. }
  177. if (usbphyc_phy->vdda1v8) {
  178. ret = regulator_set_enable(usbphyc_phy->vdda1v8, true);
  179. if (ret)
  180. return ret;
  181. }
  182. if (usbphyc_phy->vdd) {
  183. ret = regulator_set_enable(usbphyc_phy->vdd, true);
  184. if (ret)
  185. return ret;
  186. }
  187. usbphyc_phy->powered = true;
  188. return 0;
  189. }
  190. static int stm32_usbphyc_phy_power_off(struct phy *phy)
  191. {
  192. struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
  193. struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
  194. int ret;
  195. pr_debug("%s phy ID = %lu\n", __func__, phy->id);
  196. usbphyc_phy->powered = false;
  197. if (stm32_usbphyc_is_powered(usbphyc))
  198. return 0;
  199. if (usbphyc_phy->vdda1v1) {
  200. ret = regulator_set_enable(usbphyc_phy->vdda1v1, false);
  201. if (ret)
  202. return ret;
  203. }
  204. if (usbphyc_phy->vdda1v8) {
  205. ret = regulator_set_enable(usbphyc_phy->vdda1v8, false);
  206. if (ret)
  207. return ret;
  208. }
  209. if (usbphyc_phy->vdd) {
  210. ret = regulator_set_enable(usbphyc_phy->vdd, false);
  211. if (ret)
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. static int stm32_usbphyc_get_regulator(struct udevice *dev, ofnode node,
  217. char *supply_name,
  218. struct udevice **regulator)
  219. {
  220. struct ofnode_phandle_args regulator_phandle;
  221. int ret;
  222. ret = ofnode_parse_phandle_with_args(node, supply_name,
  223. NULL, 0, 0,
  224. &regulator_phandle);
  225. if (ret) {
  226. dev_err(dev, "Can't find %s property (%d)\n", supply_name, ret);
  227. return ret;
  228. }
  229. ret = uclass_get_device_by_ofnode(UCLASS_REGULATOR,
  230. regulator_phandle.node,
  231. regulator);
  232. if (ret) {
  233. dev_err(dev, "Can't get %s regulator (%d)\n", supply_name, ret);
  234. return ret;
  235. }
  236. return 0;
  237. }
  238. static int stm32_usbphyc_of_xlate(struct phy *phy,
  239. struct ofnode_phandle_args *args)
  240. {
  241. if (args->args_count > 1) {
  242. pr_debug("%s: invalid args_count: %d\n", __func__,
  243. args->args_count);
  244. return -EINVAL;
  245. }
  246. if (args->args[0] >= MAX_PHYS)
  247. return -ENODEV;
  248. if (args->args_count)
  249. phy->id = args->args[0];
  250. else
  251. phy->id = 0;
  252. return 0;
  253. }
  254. static const struct phy_ops stm32_usbphyc_phy_ops = {
  255. .init = stm32_usbphyc_phy_init,
  256. .exit = stm32_usbphyc_phy_exit,
  257. .power_on = stm32_usbphyc_phy_power_on,
  258. .power_off = stm32_usbphyc_phy_power_off,
  259. .of_xlate = stm32_usbphyc_of_xlate,
  260. };
  261. static int stm32_usbphyc_probe(struct udevice *dev)
  262. {
  263. struct stm32_usbphyc *usbphyc = dev_get_priv(dev);
  264. struct reset_ctl reset;
  265. ofnode node;
  266. int i, ret;
  267. usbphyc->base = dev_read_addr(dev);
  268. if (usbphyc->base == FDT_ADDR_T_NONE)
  269. return -EINVAL;
  270. /* Enable clock */
  271. ret = clk_get_by_index(dev, 0, &usbphyc->clk);
  272. if (ret)
  273. return ret;
  274. ret = clk_enable(&usbphyc->clk);
  275. if (ret)
  276. return ret;
  277. /* Reset */
  278. ret = reset_get_by_index(dev, 0, &reset);
  279. if (!ret) {
  280. reset_assert(&reset);
  281. udelay(2);
  282. reset_deassert(&reset);
  283. }
  284. /*
  285. * parse all PHY subnodes in order to populate regulator associated
  286. * to each PHY port
  287. */
  288. node = dev_read_first_subnode(dev);
  289. for (i = 0; i < MAX_PHYS; i++) {
  290. struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + i;
  291. usbphyc_phy->index = i;
  292. usbphyc_phy->init = false;
  293. usbphyc_phy->powered = false;
  294. ret = stm32_usbphyc_get_regulator(dev, node, "phy-supply",
  295. &usbphyc_phy->vdd);
  296. if (ret)
  297. return ret;
  298. ret = stm32_usbphyc_get_regulator(dev, node, "vdda1v1-supply",
  299. &usbphyc_phy->vdda1v1);
  300. if (ret)
  301. return ret;
  302. ret = stm32_usbphyc_get_regulator(dev, node, "vdda1v8-supply",
  303. &usbphyc_phy->vdda1v8);
  304. if (ret)
  305. return ret;
  306. node = dev_read_next_subnode(node);
  307. }
  308. /* Check if second port has to be used for host controller */
  309. if (dev_read_bool(dev, "st,port2-switch-to-host"))
  310. setbits_le32(usbphyc->base + STM32_USBPHYC_MISC, SWITHOST);
  311. return 0;
  312. }
  313. static const struct udevice_id stm32_usbphyc_of_match[] = {
  314. { .compatible = "st,stm32mp1-usbphyc", },
  315. { },
  316. };
  317. U_BOOT_DRIVER(stm32_usb_phyc) = {
  318. .name = "stm32-usbphyc",
  319. .id = UCLASS_PHY,
  320. .of_match = stm32_usbphyc_of_match,
  321. .ops = &stm32_usbphyc_phy_ops,
  322. .probe = stm32_usbphyc_probe,
  323. .priv_auto_alloc_size = sizeof(struct stm32_usbphyc),
  324. };