phy-stm32-usbphyc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * STMicroelectronics STM32 USB PHY Controller driver
  4. *
  5. * Copyright (C) 2018 STMicroelectronics
  6. * Author(s): Amelie Delaunay <[email protected]>.
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/delay.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/reset.h>
  18. #include <linux/units.h>
  19. #define STM32_USBPHYC_PLL 0x0
  20. #define STM32_USBPHYC_MISC 0x8
  21. #define STM32_USBPHYC_MONITOR(X) (0x108 + ((X) * 0x100))
  22. #define STM32_USBPHYC_TUNE(X) (0x10C + ((X) * 0x100))
  23. #define STM32_USBPHYC_VERSION 0x3F4
  24. /* STM32_USBPHYC_PLL bit fields */
  25. #define PLLNDIV GENMASK(6, 0)
  26. #define PLLFRACIN GENMASK(25, 10)
  27. #define PLLEN BIT(26)
  28. #define PLLSTRB BIT(27)
  29. #define PLLSTRBYP BIT(28)
  30. #define PLLFRACCTL BIT(29)
  31. #define PLLDITHEN0 BIT(30)
  32. #define PLLDITHEN1 BIT(31)
  33. /* STM32_USBPHYC_MISC bit fields */
  34. #define SWITHOST BIT(0)
  35. /* STM32_USBPHYC_MONITOR bit fields */
  36. #define STM32_USBPHYC_MON_OUT GENMASK(3, 0)
  37. #define STM32_USBPHYC_MON_SEL GENMASK(8, 4)
  38. #define STM32_USBPHYC_MON_SEL_LOCKP 0x1F
  39. #define STM32_USBPHYC_MON_OUT_LOCKP BIT(3)
  40. /* STM32_USBPHYC_TUNE bit fields */
  41. #define INCURREN BIT(0)
  42. #define INCURRINT BIT(1)
  43. #define LFSCAPEN BIT(2)
  44. #define HSDRVSLEW BIT(3)
  45. #define HSDRVDCCUR BIT(4)
  46. #define HSDRVDCLEV BIT(5)
  47. #define HSDRVCURINCR BIT(6)
  48. #define FSDRVRFADJ BIT(7)
  49. #define HSDRVRFRED BIT(8)
  50. #define HSDRVCHKITRM GENMASK(12, 9)
  51. #define HSDRVCHKZTRM GENMASK(14, 13)
  52. #define OTPCOMP GENMASK(19, 15)
  53. #define SQLCHCTL GENMASK(21, 20)
  54. #define HDRXGNEQEN BIT(22)
  55. #define HSRXOFF GENMASK(24, 23)
  56. #define HSFALLPREEM BIT(25)
  57. #define SHTCCTCTLPROT BIT(26)
  58. #define STAGSEL BIT(27)
  59. enum boosting_vals {
  60. BOOST_1000_UA = 1000,
  61. BOOST_2000_UA = 2000,
  62. };
  63. enum dc_level_vals {
  64. DC_NOMINAL,
  65. DC_PLUS_5_TO_7_MV,
  66. DC_PLUS_10_TO_14_MV,
  67. DC_MINUS_5_TO_7_MV,
  68. DC_MAX,
  69. };
  70. enum current_trim {
  71. CUR_NOMINAL,
  72. CUR_PLUS_1_56_PCT,
  73. CUR_PLUS_3_12_PCT,
  74. CUR_PLUS_4_68_PCT,
  75. CUR_PLUS_6_24_PCT,
  76. CUR_PLUS_7_8_PCT,
  77. CUR_PLUS_9_36_PCT,
  78. CUR_PLUS_10_92_PCT,
  79. CUR_PLUS_12_48_PCT,
  80. CUR_PLUS_14_04_PCT,
  81. CUR_PLUS_15_6_PCT,
  82. CUR_PLUS_17_16_PCT,
  83. CUR_PLUS_19_01_PCT,
  84. CUR_PLUS_20_58_PCT,
  85. CUR_PLUS_22_16_PCT,
  86. CUR_PLUS_23_73_PCT,
  87. CUR_MAX,
  88. };
  89. enum impedance_trim {
  90. IMP_NOMINAL,
  91. IMP_MINUS_2_OHMS,
  92. IMP_MINUS_4_OMHS,
  93. IMP_MINUS_6_OHMS,
  94. IMP_MAX,
  95. };
  96. enum squelch_level {
  97. SQLCH_NOMINAL,
  98. SQLCH_PLUS_7_MV,
  99. SQLCH_MINUS_5_MV,
  100. SQLCH_PLUS_14_MV,
  101. SQLCH_MAX,
  102. };
  103. enum rx_offset {
  104. NO_RX_OFFSET,
  105. RX_OFFSET_PLUS_5_MV,
  106. RX_OFFSET_PLUS_10_MV,
  107. RX_OFFSET_MINUS_5_MV,
  108. RX_OFFSET_MAX,
  109. };
  110. /* STM32_USBPHYC_VERSION bit fields */
  111. #define MINREV GENMASK(3, 0)
  112. #define MAJREV GENMASK(7, 4)
  113. #define PLL_FVCO_MHZ 2880
  114. #define PLL_INFF_MIN_RATE_HZ 19200000
  115. #define PLL_INFF_MAX_RATE_HZ 38400000
  116. struct pll_params {
  117. u8 ndiv;
  118. u16 frac;
  119. };
  120. struct stm32_usbphyc_phy {
  121. struct phy *phy;
  122. struct stm32_usbphyc *usbphyc;
  123. struct regulator *vbus;
  124. u32 index;
  125. bool active;
  126. u32 tune;
  127. };
  128. struct stm32_usbphyc {
  129. struct device *dev;
  130. void __iomem *base;
  131. struct clk *clk;
  132. struct reset_control *rst;
  133. struct stm32_usbphyc_phy **phys;
  134. int nphys;
  135. struct regulator *vdda1v1;
  136. struct regulator *vdda1v8;
  137. atomic_t n_pll_cons;
  138. struct clk_hw clk48_hw;
  139. int switch_setup;
  140. };
  141. static inline void stm32_usbphyc_set_bits(void __iomem *reg, u32 bits)
  142. {
  143. writel_relaxed(readl_relaxed(reg) | bits, reg);
  144. }
  145. static inline void stm32_usbphyc_clr_bits(void __iomem *reg, u32 bits)
  146. {
  147. writel_relaxed(readl_relaxed(reg) & ~bits, reg);
  148. }
  149. static int stm32_usbphyc_regulators_enable(struct stm32_usbphyc *usbphyc)
  150. {
  151. int ret;
  152. ret = regulator_enable(usbphyc->vdda1v1);
  153. if (ret)
  154. return ret;
  155. ret = regulator_enable(usbphyc->vdda1v8);
  156. if (ret)
  157. goto vdda1v1_disable;
  158. return 0;
  159. vdda1v1_disable:
  160. regulator_disable(usbphyc->vdda1v1);
  161. return ret;
  162. }
  163. static int stm32_usbphyc_regulators_disable(struct stm32_usbphyc *usbphyc)
  164. {
  165. int ret;
  166. ret = regulator_disable(usbphyc->vdda1v8);
  167. if (ret)
  168. return ret;
  169. ret = regulator_disable(usbphyc->vdda1v1);
  170. if (ret)
  171. return ret;
  172. return 0;
  173. }
  174. static void stm32_usbphyc_get_pll_params(u32 clk_rate,
  175. struct pll_params *pll_params)
  176. {
  177. unsigned long long fvco, ndiv, frac;
  178. /* _
  179. * | FVCO = INFF*2*(NDIV + FRACT/2^16) when DITHER_DISABLE[1] = 1
  180. * | FVCO = 2880MHz
  181. * <
  182. * | NDIV = integer part of input bits to set the LDF
  183. * |_FRACT = fractional part of input bits to set the LDF
  184. * => PLLNDIV = integer part of (FVCO / (INFF*2))
  185. * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
  186. * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
  187. */
  188. fvco = (unsigned long long)PLL_FVCO_MHZ * HZ_PER_MHZ;
  189. ndiv = fvco;
  190. do_div(ndiv, (clk_rate * 2));
  191. pll_params->ndiv = (u8)ndiv;
  192. frac = fvco * (1 << 16);
  193. do_div(frac, (clk_rate * 2));
  194. frac = frac - (ndiv * (1 << 16));
  195. pll_params->frac = (u16)frac;
  196. }
  197. static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
  198. {
  199. struct pll_params pll_params;
  200. u32 clk_rate = clk_get_rate(usbphyc->clk);
  201. u32 ndiv, frac;
  202. u32 usbphyc_pll;
  203. if ((clk_rate < PLL_INFF_MIN_RATE_HZ) ||
  204. (clk_rate > PLL_INFF_MAX_RATE_HZ)) {
  205. dev_err(usbphyc->dev, "input clk freq (%dHz) out of range\n",
  206. clk_rate);
  207. return -EINVAL;
  208. }
  209. stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
  210. ndiv = FIELD_PREP(PLLNDIV, pll_params.ndiv);
  211. frac = FIELD_PREP(PLLFRACIN, pll_params.frac);
  212. usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP | ndiv;
  213. if (pll_params.frac)
  214. usbphyc_pll |= PLLFRACCTL | frac;
  215. writel_relaxed(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
  216. dev_dbg(usbphyc->dev, "input clk freq=%dHz, ndiv=%lu, frac=%lu\n",
  217. clk_rate, FIELD_GET(PLLNDIV, usbphyc_pll),
  218. FIELD_GET(PLLFRACIN, usbphyc_pll));
  219. return 0;
  220. }
  221. static int __stm32_usbphyc_pll_disable(struct stm32_usbphyc *usbphyc)
  222. {
  223. void __iomem *pll_reg = usbphyc->base + STM32_USBPHYC_PLL;
  224. u32 pllen;
  225. stm32_usbphyc_clr_bits(pll_reg, PLLEN);
  226. /* Wait for minimum width of powerdown pulse (ENABLE = Low) */
  227. if (readl_relaxed_poll_timeout(pll_reg, pllen, !(pllen & PLLEN), 5, 50))
  228. dev_err(usbphyc->dev, "PLL not reset\n");
  229. return stm32_usbphyc_regulators_disable(usbphyc);
  230. }
  231. static int stm32_usbphyc_pll_disable(struct stm32_usbphyc *usbphyc)
  232. {
  233. /* Check if a phy port is still active or clk48 in use */
  234. if (atomic_dec_return(&usbphyc->n_pll_cons) > 0)
  235. return 0;
  236. return __stm32_usbphyc_pll_disable(usbphyc);
  237. }
  238. static int stm32_usbphyc_pll_enable(struct stm32_usbphyc *usbphyc)
  239. {
  240. void __iomem *pll_reg = usbphyc->base + STM32_USBPHYC_PLL;
  241. bool pllen = readl_relaxed(pll_reg) & PLLEN;
  242. int ret;
  243. /*
  244. * Check if a phy port or clk48 prepare has configured the pll
  245. * and ensure the PLL is enabled
  246. */
  247. if (atomic_inc_return(&usbphyc->n_pll_cons) > 1 && pllen)
  248. return 0;
  249. if (pllen) {
  250. /*
  251. * PLL shouldn't be enabled without known consumer,
  252. * disable it and reinit n_pll_cons
  253. */
  254. dev_warn(usbphyc->dev, "PLL enabled without known consumers\n");
  255. ret = __stm32_usbphyc_pll_disable(usbphyc);
  256. if (ret)
  257. goto dec_n_pll_cons;
  258. }
  259. ret = stm32_usbphyc_regulators_enable(usbphyc);
  260. if (ret)
  261. goto dec_n_pll_cons;
  262. ret = stm32_usbphyc_pll_init(usbphyc);
  263. if (ret)
  264. goto reg_disable;
  265. stm32_usbphyc_set_bits(pll_reg, PLLEN);
  266. return 0;
  267. reg_disable:
  268. stm32_usbphyc_regulators_disable(usbphyc);
  269. dec_n_pll_cons:
  270. atomic_dec(&usbphyc->n_pll_cons);
  271. return ret;
  272. }
  273. static int stm32_usbphyc_phy_init(struct phy *phy)
  274. {
  275. struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
  276. struct stm32_usbphyc *usbphyc = usbphyc_phy->usbphyc;
  277. u32 reg_mon = STM32_USBPHYC_MONITOR(usbphyc_phy->index);
  278. u32 monsel = FIELD_PREP(STM32_USBPHYC_MON_SEL,
  279. STM32_USBPHYC_MON_SEL_LOCKP);
  280. u32 monout;
  281. int ret;
  282. ret = stm32_usbphyc_pll_enable(usbphyc);
  283. if (ret)
  284. return ret;
  285. /* Check that PLL Lock input to PHY is High */
  286. writel_relaxed(monsel, usbphyc->base + reg_mon);
  287. ret = readl_relaxed_poll_timeout(usbphyc->base + reg_mon, monout,
  288. (monout & STM32_USBPHYC_MON_OUT_LOCKP),
  289. 100, 1000);
  290. if (ret) {
  291. dev_err(usbphyc->dev, "PLL Lock input to PHY is Low (val=%x)\n",
  292. (u32)(monout & STM32_USBPHYC_MON_OUT));
  293. goto pll_disable;
  294. }
  295. usbphyc_phy->active = true;
  296. return 0;
  297. pll_disable:
  298. stm32_usbphyc_pll_disable(usbphyc);
  299. return ret;
  300. }
  301. static int stm32_usbphyc_phy_exit(struct phy *phy)
  302. {
  303. struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
  304. struct stm32_usbphyc *usbphyc = usbphyc_phy->usbphyc;
  305. usbphyc_phy->active = false;
  306. return stm32_usbphyc_pll_disable(usbphyc);
  307. }
  308. static int stm32_usbphyc_phy_power_on(struct phy *phy)
  309. {
  310. struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
  311. if (usbphyc_phy->vbus)
  312. return regulator_enable(usbphyc_phy->vbus);
  313. return 0;
  314. }
  315. static int stm32_usbphyc_phy_power_off(struct phy *phy)
  316. {
  317. struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
  318. if (usbphyc_phy->vbus)
  319. return regulator_disable(usbphyc_phy->vbus);
  320. return 0;
  321. }
  322. static const struct phy_ops stm32_usbphyc_phy_ops = {
  323. .init = stm32_usbphyc_phy_init,
  324. .exit = stm32_usbphyc_phy_exit,
  325. .power_on = stm32_usbphyc_phy_power_on,
  326. .power_off = stm32_usbphyc_phy_power_off,
  327. .owner = THIS_MODULE,
  328. };
  329. static int stm32_usbphyc_clk48_prepare(struct clk_hw *hw)
  330. {
  331. struct stm32_usbphyc *usbphyc = container_of(hw, struct stm32_usbphyc, clk48_hw);
  332. return stm32_usbphyc_pll_enable(usbphyc);
  333. }
  334. static void stm32_usbphyc_clk48_unprepare(struct clk_hw *hw)
  335. {
  336. struct stm32_usbphyc *usbphyc = container_of(hw, struct stm32_usbphyc, clk48_hw);
  337. stm32_usbphyc_pll_disable(usbphyc);
  338. }
  339. static unsigned long stm32_usbphyc_clk48_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  340. {
  341. return 48000000;
  342. }
  343. static const struct clk_ops usbphyc_clk48_ops = {
  344. .prepare = stm32_usbphyc_clk48_prepare,
  345. .unprepare = stm32_usbphyc_clk48_unprepare,
  346. .recalc_rate = stm32_usbphyc_clk48_recalc_rate,
  347. };
  348. static void stm32_usbphyc_clk48_unregister(void *data)
  349. {
  350. struct stm32_usbphyc *usbphyc = data;
  351. of_clk_del_provider(usbphyc->dev->of_node);
  352. clk_hw_unregister(&usbphyc->clk48_hw);
  353. }
  354. static int stm32_usbphyc_clk48_register(struct stm32_usbphyc *usbphyc)
  355. {
  356. struct device_node *node = usbphyc->dev->of_node;
  357. struct clk_init_data init = { };
  358. int ret = 0;
  359. init.name = "ck_usbo_48m";
  360. init.ops = &usbphyc_clk48_ops;
  361. usbphyc->clk48_hw.init = &init;
  362. ret = clk_hw_register(usbphyc->dev, &usbphyc->clk48_hw);
  363. if (ret)
  364. return ret;
  365. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, &usbphyc->clk48_hw);
  366. if (ret)
  367. clk_hw_unregister(&usbphyc->clk48_hw);
  368. return ret;
  369. }
  370. static void stm32_usbphyc_phy_tuning(struct stm32_usbphyc *usbphyc,
  371. struct device_node *np, u32 index)
  372. {
  373. struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys[index];
  374. u32 reg = STM32_USBPHYC_TUNE(index);
  375. u32 otpcomp, val;
  376. int ret;
  377. /* Backup OTP compensation code */
  378. otpcomp = FIELD_GET(OTPCOMP, readl_relaxed(usbphyc->base + reg));
  379. ret = of_property_read_u32(np, "st,current-boost-microamp", &val);
  380. if (ret != -EINVAL) {
  381. if (!ret && (val == BOOST_1000_UA || val == BOOST_2000_UA)) {
  382. val = (val == BOOST_2000_UA) ? 1 : 0;
  383. usbphyc_phy->tune |= INCURREN | FIELD_PREP(INCURRINT, val);
  384. } else {
  385. dev_warn(usbphyc->dev, "phy%d: invalid st,current-boost-microamp\n", index);
  386. }
  387. }
  388. if (!of_property_read_bool(np, "st,no-lsfs-fb-cap"))
  389. usbphyc_phy->tune |= LFSCAPEN;
  390. if (of_property_read_bool(np, "st,decrease-hs-slew-rate"))
  391. usbphyc_phy->tune |= HSDRVSLEW;
  392. ret = of_property_read_u32(np, "st,tune-hs-dc-level", &val);
  393. if (ret != -EINVAL) {
  394. if (!ret && val < DC_MAX) {
  395. if (val == DC_MINUS_5_TO_7_MV) {/* Decreases HS driver DC level */
  396. usbphyc_phy->tune |= HSDRVDCCUR;
  397. } else if (val > 0) { /* Increases HS driver DC level */
  398. val = (val == DC_PLUS_10_TO_14_MV) ? 1 : 0;
  399. usbphyc_phy->tune |= HSDRVCURINCR | FIELD_PREP(HSDRVDCLEV, val);
  400. }
  401. } else {
  402. dev_warn(usbphyc->dev, "phy%d: invalid st,tune-hs-dc-level\n", index);
  403. }
  404. }
  405. if (of_property_read_bool(np, "st,enable-fs-rftime-tuning"))
  406. usbphyc_phy->tune |= FSDRVRFADJ;
  407. if (of_property_read_bool(np, "st,enable-hs-rftime-reduction"))
  408. usbphyc_phy->tune |= HSDRVRFRED;
  409. ret = of_property_read_u32(np, "st,trim-hs-current", &val);
  410. if (ret != -EINVAL) {
  411. if (!ret && val < CUR_MAX)
  412. usbphyc_phy->tune |= FIELD_PREP(HSDRVCHKITRM, val);
  413. else
  414. dev_warn(usbphyc->dev, "phy%d: invalid st,trim-hs-current\n", index);
  415. }
  416. ret = of_property_read_u32(np, "st,trim-hs-impedance", &val);
  417. if (ret != -EINVAL) {
  418. if (!ret && val < IMP_MAX)
  419. usbphyc_phy->tune |= FIELD_PREP(HSDRVCHKZTRM, val);
  420. else
  421. dev_warn(usbphyc->dev, "phy%d: invalid st,trim-hs-impedance\n", index);
  422. }
  423. ret = of_property_read_u32(np, "st,tune-squelch-level", &val);
  424. if (ret != -EINVAL) {
  425. if (!ret && val < SQLCH_MAX)
  426. usbphyc_phy->tune |= FIELD_PREP(SQLCHCTL, val);
  427. else
  428. dev_warn(usbphyc->dev, "phy%d: invalid st,tune-squelch\n", index);
  429. }
  430. if (of_property_read_bool(np, "st,enable-hs-rx-gain-eq"))
  431. usbphyc_phy->tune |= HDRXGNEQEN;
  432. ret = of_property_read_u32(np, "st,tune-hs-rx-offset", &val);
  433. if (ret != -EINVAL) {
  434. if (!ret && val < RX_OFFSET_MAX)
  435. usbphyc_phy->tune |= FIELD_PREP(HSRXOFF, val);
  436. else
  437. dev_warn(usbphyc->dev, "phy%d: invalid st,tune-hs-rx-offset\n", index);
  438. }
  439. if (of_property_read_bool(np, "st,no-hs-ftime-ctrl"))
  440. usbphyc_phy->tune |= HSFALLPREEM;
  441. if (!of_property_read_bool(np, "st,no-lsfs-sc"))
  442. usbphyc_phy->tune |= SHTCCTCTLPROT;
  443. if (of_property_read_bool(np, "st,enable-hs-tx-staggering"))
  444. usbphyc_phy->tune |= STAGSEL;
  445. /* Restore OTP compensation code */
  446. usbphyc_phy->tune |= FIELD_PREP(OTPCOMP, otpcomp);
  447. /*
  448. * By default, if no st,xxx tuning property is used, usbphyc_phy->tune is equal to
  449. * STM32_USBPHYC_TUNE reset value (LFSCAPEN | SHTCCTCTLPROT | OTPCOMP).
  450. */
  451. writel_relaxed(usbphyc_phy->tune, usbphyc->base + reg);
  452. }
  453. static void stm32_usbphyc_switch_setup(struct stm32_usbphyc *usbphyc,
  454. u32 utmi_switch)
  455. {
  456. if (!utmi_switch)
  457. stm32_usbphyc_clr_bits(usbphyc->base + STM32_USBPHYC_MISC,
  458. SWITHOST);
  459. else
  460. stm32_usbphyc_set_bits(usbphyc->base + STM32_USBPHYC_MISC,
  461. SWITHOST);
  462. usbphyc->switch_setup = utmi_switch;
  463. }
  464. static struct phy *stm32_usbphyc_of_xlate(struct device *dev,
  465. struct of_phandle_args *args)
  466. {
  467. struct stm32_usbphyc *usbphyc = dev_get_drvdata(dev);
  468. struct stm32_usbphyc_phy *usbphyc_phy = NULL;
  469. struct device_node *phynode = args->np;
  470. int port = 0;
  471. for (port = 0; port < usbphyc->nphys; port++) {
  472. if (phynode == usbphyc->phys[port]->phy->dev.of_node) {
  473. usbphyc_phy = usbphyc->phys[port];
  474. break;
  475. }
  476. }
  477. if (!usbphyc_phy) {
  478. dev_err(dev, "failed to find phy\n");
  479. return ERR_PTR(-EINVAL);
  480. }
  481. if (((usbphyc_phy->index == 0) && (args->args_count != 0)) ||
  482. ((usbphyc_phy->index == 1) && (args->args_count != 1))) {
  483. dev_err(dev, "invalid number of cells for phy port%d\n",
  484. usbphyc_phy->index);
  485. return ERR_PTR(-EINVAL);
  486. }
  487. /* Configure the UTMI switch for PHY port#2 */
  488. if (usbphyc_phy->index == 1) {
  489. if (usbphyc->switch_setup < 0) {
  490. stm32_usbphyc_switch_setup(usbphyc, args->args[0]);
  491. } else {
  492. if (args->args[0] != usbphyc->switch_setup) {
  493. dev_err(dev, "phy port1 already used\n");
  494. return ERR_PTR(-EBUSY);
  495. }
  496. }
  497. }
  498. return usbphyc_phy->phy;
  499. }
  500. static int stm32_usbphyc_probe(struct platform_device *pdev)
  501. {
  502. struct stm32_usbphyc *usbphyc;
  503. struct device *dev = &pdev->dev;
  504. struct device_node *child, *np = dev->of_node;
  505. struct phy_provider *phy_provider;
  506. u32 pllen, version;
  507. int ret, port = 0;
  508. usbphyc = devm_kzalloc(dev, sizeof(*usbphyc), GFP_KERNEL);
  509. if (!usbphyc)
  510. return -ENOMEM;
  511. usbphyc->dev = dev;
  512. dev_set_drvdata(dev, usbphyc);
  513. usbphyc->base = devm_platform_ioremap_resource(pdev, 0);
  514. if (IS_ERR(usbphyc->base))
  515. return PTR_ERR(usbphyc->base);
  516. usbphyc->clk = devm_clk_get(dev, NULL);
  517. if (IS_ERR(usbphyc->clk))
  518. return dev_err_probe(dev, PTR_ERR(usbphyc->clk), "clk get_failed\n");
  519. ret = clk_prepare_enable(usbphyc->clk);
  520. if (ret) {
  521. dev_err(dev, "clk enable failed: %d\n", ret);
  522. return ret;
  523. }
  524. usbphyc->rst = devm_reset_control_get(dev, NULL);
  525. if (!IS_ERR(usbphyc->rst)) {
  526. reset_control_assert(usbphyc->rst);
  527. udelay(2);
  528. reset_control_deassert(usbphyc->rst);
  529. } else {
  530. ret = PTR_ERR(usbphyc->rst);
  531. if (ret == -EPROBE_DEFER)
  532. goto clk_disable;
  533. stm32_usbphyc_clr_bits(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
  534. }
  535. /*
  536. * Wait for minimum width of powerdown pulse (ENABLE = Low):
  537. * we have to ensure the PLL is disabled before phys initialization.
  538. */
  539. if (readl_relaxed_poll_timeout(usbphyc->base + STM32_USBPHYC_PLL,
  540. pllen, !(pllen & PLLEN), 5, 50)) {
  541. dev_warn(usbphyc->dev, "PLL not reset\n");
  542. ret = -EPROBE_DEFER;
  543. goto clk_disable;
  544. }
  545. usbphyc->switch_setup = -EINVAL;
  546. usbphyc->nphys = of_get_child_count(np);
  547. usbphyc->phys = devm_kcalloc(dev, usbphyc->nphys,
  548. sizeof(*usbphyc->phys), GFP_KERNEL);
  549. if (!usbphyc->phys) {
  550. ret = -ENOMEM;
  551. goto clk_disable;
  552. }
  553. usbphyc->vdda1v1 = devm_regulator_get(dev, "vdda1v1");
  554. if (IS_ERR(usbphyc->vdda1v1)) {
  555. ret = dev_err_probe(dev, PTR_ERR(usbphyc->vdda1v1),
  556. "failed to get vdda1v1 supply\n");
  557. goto clk_disable;
  558. }
  559. usbphyc->vdda1v8 = devm_regulator_get(dev, "vdda1v8");
  560. if (IS_ERR(usbphyc->vdda1v8)) {
  561. ret = dev_err_probe(dev, PTR_ERR(usbphyc->vdda1v8),
  562. "failed to get vdda1v8 supply\n");
  563. goto clk_disable;
  564. }
  565. for_each_child_of_node(np, child) {
  566. struct stm32_usbphyc_phy *usbphyc_phy;
  567. struct phy *phy;
  568. u32 index;
  569. phy = devm_phy_create(dev, child, &stm32_usbphyc_phy_ops);
  570. if (IS_ERR(phy)) {
  571. ret = PTR_ERR(phy);
  572. if (ret != -EPROBE_DEFER)
  573. dev_err(dev, "failed to create phy%d: %d\n",
  574. port, ret);
  575. goto put_child;
  576. }
  577. usbphyc_phy = devm_kzalloc(dev, sizeof(*usbphyc_phy),
  578. GFP_KERNEL);
  579. if (!usbphyc_phy) {
  580. ret = -ENOMEM;
  581. goto put_child;
  582. }
  583. ret = of_property_read_u32(child, "reg", &index);
  584. if (ret || index > usbphyc->nphys) {
  585. dev_err(&phy->dev, "invalid reg property: %d\n", ret);
  586. if (!ret)
  587. ret = -EINVAL;
  588. goto put_child;
  589. }
  590. usbphyc->phys[port] = usbphyc_phy;
  591. phy_set_bus_width(phy, 8);
  592. phy_set_drvdata(phy, usbphyc_phy);
  593. usbphyc->phys[port]->phy = phy;
  594. usbphyc->phys[port]->usbphyc = usbphyc;
  595. usbphyc->phys[port]->index = index;
  596. usbphyc->phys[port]->active = false;
  597. usbphyc->phys[port]->vbus = devm_regulator_get_optional(&phy->dev, "vbus");
  598. if (IS_ERR(usbphyc->phys[port]->vbus)) {
  599. ret = PTR_ERR(usbphyc->phys[port]->vbus);
  600. if (ret == -EPROBE_DEFER)
  601. goto put_child;
  602. usbphyc->phys[port]->vbus = NULL;
  603. }
  604. /* Configure phy tuning */
  605. stm32_usbphyc_phy_tuning(usbphyc, child, index);
  606. port++;
  607. }
  608. phy_provider = devm_of_phy_provider_register(dev,
  609. stm32_usbphyc_of_xlate);
  610. if (IS_ERR(phy_provider)) {
  611. ret = PTR_ERR(phy_provider);
  612. dev_err(dev, "failed to register phy provider: %d\n", ret);
  613. goto clk_disable;
  614. }
  615. ret = stm32_usbphyc_clk48_register(usbphyc);
  616. if (ret) {
  617. dev_err(dev, "failed to register ck_usbo_48m clock: %d\n", ret);
  618. goto clk_disable;
  619. }
  620. version = readl_relaxed(usbphyc->base + STM32_USBPHYC_VERSION);
  621. dev_info(dev, "registered rev:%lu.%lu\n",
  622. FIELD_GET(MAJREV, version), FIELD_GET(MINREV, version));
  623. return 0;
  624. put_child:
  625. of_node_put(child);
  626. clk_disable:
  627. clk_disable_unprepare(usbphyc->clk);
  628. return ret;
  629. }
  630. static int stm32_usbphyc_remove(struct platform_device *pdev)
  631. {
  632. struct stm32_usbphyc *usbphyc = dev_get_drvdata(&pdev->dev);
  633. int port;
  634. /* Ensure PHYs are not active, to allow PLL disabling */
  635. for (port = 0; port < usbphyc->nphys; port++)
  636. if (usbphyc->phys[port]->active)
  637. stm32_usbphyc_phy_exit(usbphyc->phys[port]->phy);
  638. stm32_usbphyc_clk48_unregister(usbphyc);
  639. clk_disable_unprepare(usbphyc->clk);
  640. return 0;
  641. }
  642. static int __maybe_unused stm32_usbphyc_resume(struct device *dev)
  643. {
  644. struct stm32_usbphyc *usbphyc = dev_get_drvdata(dev);
  645. struct stm32_usbphyc_phy *usbphyc_phy;
  646. int port;
  647. if (usbphyc->switch_setup >= 0)
  648. stm32_usbphyc_switch_setup(usbphyc, usbphyc->switch_setup);
  649. for (port = 0; port < usbphyc->nphys; port++) {
  650. usbphyc_phy = usbphyc->phys[port];
  651. writel_relaxed(usbphyc_phy->tune, usbphyc->base + STM32_USBPHYC_TUNE(port));
  652. }
  653. return 0;
  654. }
  655. static SIMPLE_DEV_PM_OPS(stm32_usbphyc_pm_ops, NULL, stm32_usbphyc_resume);
  656. static const struct of_device_id stm32_usbphyc_of_match[] = {
  657. { .compatible = "st,stm32mp1-usbphyc", },
  658. { },
  659. };
  660. MODULE_DEVICE_TABLE(of, stm32_usbphyc_of_match);
  661. static struct platform_driver stm32_usbphyc_driver = {
  662. .probe = stm32_usbphyc_probe,
  663. .remove = stm32_usbphyc_remove,
  664. .driver = {
  665. .of_match_table = stm32_usbphyc_of_match,
  666. .name = "stm32-usbphyc",
  667. .pm = &stm32_usbphyc_pm_ops,
  668. }
  669. };
  670. module_platform_driver(stm32_usbphyc_driver);
  671. MODULE_DESCRIPTION("STMicroelectronics STM32 USBPHYC driver");
  672. MODULE_AUTHOR("Amelie Delaunay <[email protected]>");
  673. MODULE_LICENSE("GPL v2");