phy-stm32-usbphyc.c 21 KB

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