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