blob: f23aef4fb01429a962ff00fc76b091e9405b2a27 [file] [log] [blame]
Tom Rini4606fc72018-05-20 09:47:45 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrice Chotard226d87b2018-04-27 11:01:55 +02002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 */
5
6#include <common.h>
7#include <clk.h>
8#include <div64.h>
9#include <dm.h>
10#include <fdtdec.h>
11#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrice Chotard226d87b2018-04-27 11:01:55 +020013#include <reset.h>
14#include <syscon.h>
15#include <usb.h>
16#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Patrice Chotard226d87b2018-04-27 11:01:55 +020018#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Patrice Chotard226d87b2018-04-27 11:01:55 +020020#include <power/regulator.h>
21
22/* USBPHYC registers */
23#define STM32_USBPHYC_PLL 0x0
24#define STM32_USBPHYC_MISC 0x8
25
26/* STM32_USBPHYC_PLL bit fields */
27#define PLLNDIV GENMASK(6, 0)
28#define PLLNDIV_SHIFT 0
29#define PLLFRACIN GENMASK(25, 10)
30#define PLLFRACIN_SHIFT 10
31#define PLLEN BIT(26)
32#define PLLSTRB BIT(27)
33#define PLLSTRBYP BIT(28)
34#define PLLFRACCTL BIT(29)
35#define PLLDITHEN0 BIT(30)
36#define PLLDITHEN1 BIT(31)
37
38/* STM32_USBPHYC_MISC bit fields */
39#define SWITHOST BIT(0)
40
41#define MAX_PHYS 2
42
Patrick Delaunay733b3162019-03-29 15:42:13 +010043/* max 100 us for PLL lock and 100 us for PHY init */
44#define PLL_INIT_TIME_US 200
Patrice Chotard226d87b2018-04-27 11:01:55 +020045#define PLL_PWR_DOWN_TIME_US 5
46#define PLL_FVCO 2880 /* in MHz */
47#define PLL_INFF_MIN_RATE 19200000 /* in Hz */
48#define PLL_INFF_MAX_RATE 38400000 /* in Hz */
49
50struct pll_params {
51 u8 ndiv;
52 u16 frac;
53};
54
55struct stm32_usbphyc {
56 fdt_addr_t base;
57 struct clk clk;
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +010058 struct udevice *vdda1v1;
59 struct udevice *vdda1v8;
Patrice Chotard226d87b2018-04-27 11:01:55 +020060 struct stm32_usbphyc_phy {
61 struct udevice *vdd;
Patrick Delaunayc8a755d2020-10-15 14:50:57 +020062 struct udevice *vbus;
Patrice Chotard226d87b2018-04-27 11:01:55 +020063 bool init;
64 bool powered;
65 } phys[MAX_PHYS];
66};
67
Patrick Delaunay1effca22019-03-29 15:42:12 +010068static void stm32_usbphyc_get_pll_params(u32 clk_rate,
69 struct pll_params *pll_params)
Patrice Chotard226d87b2018-04-27 11:01:55 +020070{
71 unsigned long long fvco, ndiv, frac;
72
73 /*
74 * | FVCO = INFF*2*(NDIV + FRACT/2^16 ) when DITHER_DISABLE[1] = 1
75 * | FVCO = 2880MHz
76 * | NDIV = integer part of input bits to set the LDF
77 * | FRACT = fractional part of input bits to set the LDF
78 * => PLLNDIV = integer part of (FVCO / (INFF*2))
79 * => PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
80 * <=> PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
81 */
82 fvco = (unsigned long long)PLL_FVCO * 1000000; /* In Hz */
83
84 ndiv = fvco;
85 do_div(ndiv, (clk_rate * 2));
86 pll_params->ndiv = (u8)ndiv;
87
88 frac = fvco * (1 << 16);
89 do_div(frac, (clk_rate * 2));
90 frac = frac - (ndiv * (1 << 16));
91 pll_params->frac = (u16)frac;
92}
93
94static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
95{
96 struct pll_params pll_params;
97 u32 clk_rate = clk_get_rate(&usbphyc->clk);
98 u32 usbphyc_pll;
99
100 if ((clk_rate < PLL_INFF_MIN_RATE) || (clk_rate > PLL_INFF_MAX_RATE)) {
101 pr_debug("%s: input clk freq (%dHz) out of range\n",
102 __func__, clk_rate);
103 return -EINVAL;
104 }
105
106 stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
107
108 usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP;
109 usbphyc_pll |= ((pll_params.ndiv << PLLNDIV_SHIFT) & PLLNDIV);
110
111 if (pll_params.frac) {
112 usbphyc_pll |= PLLFRACCTL;
113 usbphyc_pll |= ((pll_params.frac << PLLFRACIN_SHIFT)
114 & PLLFRACIN);
115 }
116
117 writel(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
118
119 pr_debug("%s: input clk freq=%dHz, ndiv=%d, frac=%d\n", __func__,
120 clk_rate, pll_params.ndiv, pll_params.frac);
121
122 return 0;
123}
124
125static bool stm32_usbphyc_is_init(struct stm32_usbphyc *usbphyc)
126{
127 int i;
128
129 for (i = 0; i < MAX_PHYS; i++) {
130 if (usbphyc->phys[i].init)
131 return true;
132 }
133
134 return false;
135}
136
137static bool stm32_usbphyc_is_powered(struct stm32_usbphyc *usbphyc)
138{
139 int i;
140
141 for (i = 0; i < MAX_PHYS; i++) {
142 if (usbphyc->phys[i].powered)
143 return true;
144 }
145
146 return false;
147}
148
149static int stm32_usbphyc_phy_init(struct phy *phy)
150{
151 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
152 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
153 bool pllen = readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN ?
154 true : false;
155 int ret;
156
157 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
158 /* Check if one phy port has already configured the pll */
159 if (pllen && stm32_usbphyc_is_init(usbphyc))
160 goto initialized;
161
Patrick Delaunay1effca22019-03-29 15:42:12 +0100162 if (usbphyc->vdda1v1) {
163 ret = regulator_set_enable(usbphyc->vdda1v1, true);
164 if (ret)
165 return ret;
166 }
167
168 if (usbphyc->vdda1v8) {
169 ret = regulator_set_enable(usbphyc->vdda1v8, true);
170 if (ret)
171 return ret;
172 }
173
Patrice Chotard226d87b2018-04-27 11:01:55 +0200174 if (pllen) {
175 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
176 udelay(PLL_PWR_DOWN_TIME_US);
177 }
178
179 ret = stm32_usbphyc_pll_init(usbphyc);
180 if (ret)
181 return ret;
182
183 setbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
184
Patrick Delaunay733b3162019-03-29 15:42:13 +0100185 /* We must wait PLL_INIT_TIME_US before using PHY */
186 udelay(PLL_INIT_TIME_US);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200187
188 if (!(readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN))
189 return -EIO;
190
191initialized:
192 usbphyc_phy->init = true;
193
194 return 0;
195}
196
197static int stm32_usbphyc_phy_exit(struct phy *phy)
198{
199 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
200 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
Patrick Delaunay1effca22019-03-29 15:42:12 +0100201 int ret;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200202
203 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
204 usbphyc_phy->init = false;
205
206 /* Check if other phy port requires pllen */
207 if (stm32_usbphyc_is_init(usbphyc))
208 return 0;
209
210 clrbits_le32(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
211
212 /*
213 * We must wait PLL_PWR_DOWN_TIME_US before checking that PLLEN
214 * bit is still clear
215 */
216 udelay(PLL_PWR_DOWN_TIME_US);
217
218 if (readl(usbphyc->base + STM32_USBPHYC_PLL) & PLLEN)
219 return -EIO;
220
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100221 if (usbphyc->vdda1v1) {
Patrick Delaunay1effca22019-03-29 15:42:12 +0100222 ret = regulator_set_enable(usbphyc->vdda1v1, false);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200223 if (ret)
224 return ret;
225 }
226
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100227 if (usbphyc->vdda1v8) {
Patrick Delaunay1effca22019-03-29 15:42:12 +0100228 ret = regulator_set_enable(usbphyc->vdda1v8, false);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200229 if (ret)
230 return ret;
231 }
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100232
Patrick Delaunay1effca22019-03-29 15:42:12 +0100233 return 0;
234}
235
236static int stm32_usbphyc_phy_power_on(struct phy *phy)
237{
238 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
239 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
240 int ret;
241
242 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
243 if (usbphyc_phy->vdd) {
244 ret = regulator_set_enable(usbphyc_phy->vdd, true);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200245 if (ret)
246 return ret;
247 }
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200248 if (usbphyc_phy->vbus) {
249 ret = regulator_set_enable(usbphyc_phy->vbus, true);
250 if (ret)
251 return ret;
252 }
Patrice Chotard226d87b2018-04-27 11:01:55 +0200253
254 usbphyc_phy->powered = true;
255
256 return 0;
257}
258
259static int stm32_usbphyc_phy_power_off(struct phy *phy)
260{
261 struct stm32_usbphyc *usbphyc = dev_get_priv(phy->dev);
262 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + phy->id;
263 int ret;
264
265 pr_debug("%s phy ID = %lu\n", __func__, phy->id);
266 usbphyc_phy->powered = false;
267
268 if (stm32_usbphyc_is_powered(usbphyc))
269 return 0;
270
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200271 if (usbphyc_phy->vbus) {
272 ret = regulator_set_enable(usbphyc_phy->vbus, false);
273 if (ret)
274 return ret;
275 }
Patrick Delaunay1effca22019-03-29 15:42:12 +0100276 if (usbphyc_phy->vdd) {
Patrick Delaunay611bba82020-07-03 19:13:02 +0200277 ret = regulator_set_enable_if_allowed(usbphyc_phy->vdd, false);
Patrice Chotard226d87b2018-04-27 11:01:55 +0200278 if (ret)
279 return ret;
280 }
281
282 return 0;
283}
284
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200285static int stm32_usbphyc_get_regulator(ofnode node,
Patrice Chotard226d87b2018-04-27 11:01:55 +0200286 char *supply_name,
287 struct udevice **regulator)
288{
289 struct ofnode_phandle_args regulator_phandle;
290 int ret;
291
292 ret = ofnode_parse_phandle_with_args(node, supply_name,
293 NULL, 0, 0,
294 &regulator_phandle);
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200295 if (ret)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200296 return ret;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200297
298 ret = uclass_get_device_by_ofnode(UCLASS_REGULATOR,
299 regulator_phandle.node,
300 regulator);
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200301 if (ret)
Patrice Chotard226d87b2018-04-27 11:01:55 +0200302 return ret;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200303
304 return 0;
305}
306
307static int stm32_usbphyc_of_xlate(struct phy *phy,
308 struct ofnode_phandle_args *args)
309{
Patrick Delaunay2ad166e2019-03-29 15:42:10 +0100310 if (args->args_count < 1)
311 return -ENODEV;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200312
313 if (args->args[0] >= MAX_PHYS)
314 return -ENODEV;
315
Patrick Delaunay2ad166e2019-03-29 15:42:10 +0100316 phy->id = args->args[0];
317
318 if ((phy->id == 0 && args->args_count != 1) ||
319 (phy->id == 1 && args->args_count != 2)) {
Sean Anderson30080c32020-09-15 10:45:06 -0400320 dev_err(phy->dev, "invalid number of cells for phy port%ld\n",
Patrick Delaunay2ad166e2019-03-29 15:42:10 +0100321 phy->id);
322 return -EINVAL;
323 }
Patrice Chotard226d87b2018-04-27 11:01:55 +0200324
325 return 0;
326}
327
328static const struct phy_ops stm32_usbphyc_phy_ops = {
329 .init = stm32_usbphyc_phy_init,
330 .exit = stm32_usbphyc_phy_exit,
331 .power_on = stm32_usbphyc_phy_power_on,
332 .power_off = stm32_usbphyc_phy_power_off,
333 .of_xlate = stm32_usbphyc_of_xlate,
334};
335
336static int stm32_usbphyc_probe(struct udevice *dev)
337{
338 struct stm32_usbphyc *usbphyc = dev_get_priv(dev);
339 struct reset_ctl reset;
340 ofnode node;
341 int i, ret;
342
343 usbphyc->base = dev_read_addr(dev);
344 if (usbphyc->base == FDT_ADDR_T_NONE)
345 return -EINVAL;
346
347 /* Enable clock */
348 ret = clk_get_by_index(dev, 0, &usbphyc->clk);
349 if (ret)
350 return ret;
351
352 ret = clk_enable(&usbphyc->clk);
353 if (ret)
354 return ret;
355
356 /* Reset */
357 ret = reset_get_by_index(dev, 0, &reset);
358 if (!ret) {
359 reset_assert(&reset);
360 udelay(2);
361 reset_deassert(&reset);
362 }
363
Patrick Delaunay6ba88cd2019-03-29 15:42:11 +0100364 /* get usbphyc regulator */
365 ret = device_get_supply_regulator(dev, "vdda1v1-supply",
366 &usbphyc->vdda1v1);
367 if (ret) {
368 dev_err(dev, "Can't get vdda1v1-supply regulator\n");
369 return ret;
370 }
371
372 ret = device_get_supply_regulator(dev, "vdda1v8-supply",
373 &usbphyc->vdda1v8);
374 if (ret) {
375 dev_err(dev, "Can't get vdda1v8-supply regulator\n");
376 return ret;
377 }
378
Patrice Chotard226d87b2018-04-27 11:01:55 +0200379 /*
380 * parse all PHY subnodes in order to populate regulator associated
381 * to each PHY port
382 */
383 node = dev_read_first_subnode(dev);
384 for (i = 0; i < MAX_PHYS; i++) {
385 struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys + i;
386
Patrice Chotard226d87b2018-04-27 11:01:55 +0200387 usbphyc_phy->init = false;
388 usbphyc_phy->powered = false;
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200389 ret = stm32_usbphyc_get_regulator(node, "phy-supply",
Patrice Chotard226d87b2018-04-27 11:01:55 +0200390 &usbphyc_phy->vdd);
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200391 if (ret) {
392 dev_err(dev, "Can't get phy-supply regulator\n");
Patrice Chotard226d87b2018-04-27 11:01:55 +0200393 return ret;
Patrick Delaunayc8a755d2020-10-15 14:50:57 +0200394 }
395
396 ret = stm32_usbphyc_get_regulator(node, "vbus-supply",
397 &usbphyc_phy->vbus);
398 if (ret)
399 usbphyc_phy->vbus = NULL;
Patrice Chotard226d87b2018-04-27 11:01:55 +0200400
Patrice Chotard226d87b2018-04-27 11:01:55 +0200401 node = dev_read_next_subnode(node);
402 }
403
404 /* Check if second port has to be used for host controller */
405 if (dev_read_bool(dev, "st,port2-switch-to-host"))
406 setbits_le32(usbphyc->base + STM32_USBPHYC_MISC, SWITHOST);
407
408 return 0;
409}
410
411static const struct udevice_id stm32_usbphyc_of_match[] = {
412 { .compatible = "st,stm32mp1-usbphyc", },
413 { },
414};
415
416U_BOOT_DRIVER(stm32_usb_phyc) = {
417 .name = "stm32-usbphyc",
418 .id = UCLASS_PHY,
419 .of_match = stm32_usbphyc_of_match,
420 .ops = &stm32_usbphyc_phy_ops,
421 .probe = stm32_usbphyc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700422 .priv_auto = sizeof(struct stm32_usbphyc),
Patrice Chotard226d87b2018-04-27 11:01:55 +0200423};