3096f12c1c6e07bfc0701c836c727a86a7947959
[platform/kernel/u-boot.git] / drivers / phy / allwinner / phy-sun4i-usb.c
1 /*
2  * Allwinner sun4i USB PHY driver
3  *
4  * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
5  * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6  * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
7  *
8  * Modelled arch/arm/mach-sunxi/usb_phy.c to compatible with generic-phy.
9  *
10  * SPDX-License-Identifier:     GPL-2.0+
11  */
12
13 #include <common.h>
14 #include <dm.h>
15 #include <dm/device.h>
16 #include <generic-phy.h>
17 #include <phy-sun4i-usb.h>
18 #include <asm/gpio.h>
19 #include <asm/io.h>
20 #include <asm/arch/clock.h>
21 #include <asm/arch/cpu.h>
22
23 #define REG_ISCR                        0x00
24 #define REG_PHYCTL_A10                  0x04
25 #define REG_PHYBIST                     0x08
26 #define REG_PHYTUNE                     0x0c
27 #define REG_PHYCTL_A33                  0x10
28 #define REG_PHY_OTGCTL                  0x20
29 #define REG_PMU_UNK1                    0x10
30
31 /* Common Control Bits for Both PHYs */
32 #define PHY_PLL_BW                      0x03
33 #define PHY_RES45_CAL_EN                0x0c
34
35 /* Private Control Bits for Each PHY */
36 #define PHY_TX_AMPLITUDE_TUNE           0x20
37 #define PHY_TX_SLEWRATE_TUNE            0x22
38 #define PHY_DISCON_TH_SEL               0x2a
39 #define PHY_SQUELCH_DETECT              0x3c
40
41 #define PHYCTL_DATA                     BIT(7)
42 #define OTGCTL_ROUTE_MUSB               BIT(0)
43
44 #define PHY_TX_RATE                     BIT(4)
45 #define PHY_TX_MAGNITUDE                BIT(2)
46 #define PHY_TX_AMPLITUDE_LEN            5
47
48 #define PHY_RES45_CAL_DATA              BIT(0)
49 #define PHY_RES45_CAL_LEN               1
50 #define PHY_DISCON_TH_LEN               2
51
52 #define SUNXI_AHB_ICHR8_EN              BIT(10)
53 #define SUNXI_AHB_INCR4_BURST_EN        BIT(9)
54 #define SUNXI_AHB_INCRX_ALIGN_EN        BIT(8)
55 #define SUNXI_ULPI_BYPASS_EN            BIT(0)
56
57 /* A83T specific control bits for PHY0 */
58 #define PHY_CTL_VBUSVLDEXT              BIT(5)
59 #define PHY_CTL_SIDDQ                   BIT(3)
60
61 /* A83T specific control bits for PHY2 HSIC */
62 #define SUNXI_EHCI_HS_FORCE             BIT(20)
63 #define SUNXI_HSIC_CONNECT_INT          BIT(16)
64 #define SUNXI_HSIC                      BIT(1)
65
66 #define MAX_PHYS                        4
67
68 enum sun4i_usb_phy_type {
69         sun4i_a10_phy,
70         sun6i_a31_phy,
71         sun8i_a33_phy,
72         sun8i_a83t_phy,
73         sun8i_h3_phy,
74         sun8i_v3s_phy,
75         sun50i_a64_phy,
76 };
77
78 struct sun4i_usb_phy_cfg {
79         int num_phys;
80         enum sun4i_usb_phy_type type;
81         u32 disc_thresh;
82         u8 phyctl_offset;
83         bool enable_pmu_unk1;
84         bool phy0_dual_route;
85 };
86
87 struct sun4i_usb_phy_info {
88         const char *gpio_vbus;
89         const char *gpio_vbus_det;
90         const char *gpio_id_det;
91         int rst_mask;
92 } phy_info[] = {
93         {
94                 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
95                 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
96                 .gpio_id_det = CONFIG_USB0_ID_DET,
97                 .rst_mask = (CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK),
98         },
99         {
100                 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
101                 .gpio_vbus_det = NULL,
102                 .gpio_id_det = NULL,
103                 .rst_mask = (CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK),
104         },
105         {
106                 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
107                 .gpio_vbus_det = NULL,
108                 .gpio_id_det = NULL,
109 #ifdef CONFIG_MACH_SUN8I_A83T
110                 .rst_mask = (CCM_USB_CTRL_HSIC_RST | CCM_USB_CTRL_HSIC_CLK |
111                              CCM_USB_CTRL_12M_CLK),
112 #else
113                 .rst_mask = (CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK),
114 #endif
115         },
116         {
117                 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
118                 .gpio_vbus_det = NULL,
119                 .gpio_id_det = NULL,
120 #ifdef CONFIG_MACH_SUN6I
121                 .rst_mask = (CCM_USB_CTRL_PHY3_RST | CCM_USB_CTRL_PHY3_CLK),
122 #endif
123         },
124 };
125
126 struct sun4i_usb_phy_plat {
127         void __iomem *pmu;
128         int power_on_count;
129         int gpio_vbus;
130         int gpio_vbus_det;
131         int gpio_id_det;
132         int rst_mask;
133         int id;
134 };
135
136 struct sun4i_usb_phy_data {
137         void __iomem *base;
138         struct sunxi_ccm_reg *ccm;
139         const struct sun4i_usb_phy_cfg *cfg;
140         struct sun4i_usb_phy_plat *usb_phy;
141 };
142
143 static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
144
145 static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
146 {
147         struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
148         struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
149         u32 temp, usbc_bit = BIT(usb_phy->id * 2);
150         void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
151         int i;
152
153         if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
154                 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
155                 writel(0, phyctl);
156         }
157
158         for (i = 0; i < len; i++) {
159                 temp = readl(phyctl);
160
161                 /* clear the address portion */
162                 temp &= ~(0xff << 8);
163
164                 /* set the address */
165                 temp |= ((addr + i) << 8);
166                 writel(temp, phyctl);
167
168                 /* set the data bit and clear usbc bit*/
169                 temp = readb(phyctl);
170                 if (data & 0x1)
171                         temp |= PHYCTL_DATA;
172                 else
173                         temp &= ~PHYCTL_DATA;
174                 temp &= ~usbc_bit;
175                 writeb(temp, phyctl);
176
177                 /* pulse usbc_bit */
178                 temp = readb(phyctl);
179                 temp |= usbc_bit;
180                 writeb(temp, phyctl);
181
182                 temp = readb(phyctl);
183                 temp &= ~usbc_bit;
184                 writeb(temp, phyctl);
185
186                 data >>= 1;
187         }
188 }
189
190 static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
191 {
192         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
193         struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
194         u32 bits, reg_value;
195
196         if (!usb_phy->pmu)
197                 return;
198
199         bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
200                 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
201
202         /* A83T USB2 is HSIC */
203         if (data->cfg->type == sun8i_a83t_phy && usb_phy->id == 2)
204                 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
205                         SUNXI_HSIC;
206
207         reg_value = readl(usb_phy->pmu);
208
209         if (enable)
210                 reg_value |= bits;
211         else
212                 reg_value &= ~bits;
213
214         writel(reg_value, usb_phy->pmu);
215 }
216
217 static int sun4i_usb_phy_power_on(struct phy *phy)
218 {
219         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
220         struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
221
222         if (initial_usb_scan_delay) {
223                 mdelay(initial_usb_scan_delay);
224                 initial_usb_scan_delay = 0;
225         }
226
227         usb_phy->power_on_count++;
228         if (usb_phy->power_on_count != 1)
229                 return 0;
230
231         if (usb_phy->gpio_vbus >= 0)
232                 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
233
234         return 0;
235 }
236
237 static int sun4i_usb_phy_power_off(struct phy *phy)
238 {
239         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
240         struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
241
242         usb_phy->power_on_count--;
243         if (usb_phy->power_on_count != 0)
244                 return 0;
245
246         if (usb_phy->gpio_vbus >= 0)
247                 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
248
249         return 0;
250 }
251
252 static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
253 {
254         u32 regval;
255
256         regval = readl(data->base + REG_PHY_OTGCTL);
257         if (!id_det) {
258                 /* Host mode. Route phy0 to EHCI/OHCI */
259                 regval &= ~OTGCTL_ROUTE_MUSB;
260         } else {
261                 /* Peripheral mode. Route phy0 to MUSB */
262                 regval |= OTGCTL_ROUTE_MUSB;
263         }
264         writel(regval, data->base + REG_PHY_OTGCTL);
265 }
266
267 static int sun4i_usb_phy_init(struct phy *phy)
268 {
269         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
270         struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
271         u32 val;
272
273         setbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask);
274
275         if (data->cfg->type == sun8i_a83t_phy) {
276                 if (phy->id == 0) {
277                         val = readl(data->base + data->cfg->phyctl_offset);
278                         val |= PHY_CTL_VBUSVLDEXT;
279                         val &= ~PHY_CTL_SIDDQ;
280                         writel(val, data->base + data->cfg->phyctl_offset);
281                 }
282         } else {
283                 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
284                         val = readl(usb_phy->pmu + REG_PMU_UNK1);
285                         writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
286                 }
287
288                 if (usb_phy->id == 0)
289                         sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
290                                             PHY_RES45_CAL_DATA,
291                                             PHY_RES45_CAL_LEN);
292
293                 /* Adjust PHY's magnitude and rate */
294                 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
295                                     PHY_TX_MAGNITUDE | PHY_TX_RATE,
296                                     PHY_TX_AMPLITUDE_LEN);
297
298                 /* Disconnect threshold adjustment */
299                 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
300                                     data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
301         }
302
303         sun4i_usb_phy_passby(phy, true);
304
305         sun4i_usb_phy0_reroute(data, true);
306
307         return 0;
308 }
309
310 static int sun4i_usb_phy_exit(struct phy *phy)
311 {
312         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
313         struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
314
315         if (phy->id == 0) {
316                 if (data->cfg->type == sun8i_a83t_phy) {
317                         void __iomem *phyctl = data->base +
318                                 data->cfg->phyctl_offset;
319
320                         writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
321                 }
322         }
323
324         sun4i_usb_phy_passby(phy, false);
325
326         clrbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask);
327
328         return 0;
329 }
330
331 static int sun4i_usb_phy_xlate(struct phy *phy,
332                                struct ofnode_phandle_args *args)
333 {
334         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
335
336         if (args->args_count >= data->cfg->num_phys)
337                 return -EINVAL;
338
339         if (args->args_count)
340                 phy->id = args->args[0];
341         else
342                 phy->id = 0;
343
344         debug("%s: phy_id = %ld\n", __func__, phy->id);
345         return 0;
346 }
347
348 int sun4i_usb_phy_vbus_detect(struct phy *phy)
349 {
350         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
351         struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
352         int err, retries = 3;
353
354         debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
355
356         if (usb_phy->gpio_vbus_det < 0)
357                 return usb_phy->gpio_vbus_det;
358
359         err = gpio_get_value(usb_phy->gpio_vbus_det);
360         /*
361          * Vbus may have been provided by the board and just been turned of
362          * some milliseconds ago on reset, what we're measuring then is a
363          * residual charge on Vbus, sleep a bit and try again.
364          */
365         while (err > 0 && retries--) {
366                 mdelay(100);
367                 err = gpio_get_value(usb_phy->gpio_vbus_det);
368         }
369
370         return err;
371 }
372
373 int sun4i_usb_phy_id_detect(struct phy *phy)
374 {
375         struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
376         struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
377
378         debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
379
380         if (usb_phy->gpio_id_det < 0)
381                 return usb_phy->gpio_id_det;
382
383         return gpio_get_value(usb_phy->gpio_id_det);
384 }
385
386 void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
387 {
388         sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
389 }
390
391 static struct phy_ops sun4i_usb_phy_ops = {
392         .of_xlate = sun4i_usb_phy_xlate,
393         .init = sun4i_usb_phy_init,
394         .power_on = sun4i_usb_phy_power_on,
395         .power_off = sun4i_usb_phy_power_off,
396         .exit = sun4i_usb_phy_exit,
397 };
398
399 static int sun4i_usb_phy_probe(struct udevice *dev)
400 {
401         struct sun4i_usb_phy_plat *plat = dev_get_platdata(dev);
402         struct sun4i_usb_phy_data *data = dev_get_priv(dev);
403         int i, ret;
404
405         data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
406         if (!data->cfg)
407                 return -EINVAL;
408
409         data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
410         if (IS_ERR(data->base))
411                 return PTR_ERR(data->base);
412
413         data->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
414         if (IS_ERR(data->ccm))
415                 return PTR_ERR(data->ccm);
416
417         data->usb_phy = plat;
418         for (i = 0; i < data->cfg->num_phys; i++) {
419                 struct sun4i_usb_phy_plat *phy = &plat[i];
420                 struct sun4i_usb_phy_info *info = &phy_info[i];
421                 char name[16];
422
423                 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
424                 if (phy->gpio_vbus >= 0) {
425                         ret = gpio_request(phy->gpio_vbus, "usb_vbus");
426                         if (ret)
427                                 return ret;
428                         ret = gpio_direction_output(phy->gpio_vbus, 0);
429                         if (ret)
430                                 return ret;
431                 }
432
433                 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
434                 if (phy->gpio_vbus_det >= 0) {
435                         ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
436                         if (ret)
437                                 return ret;
438                         ret = gpio_direction_input(phy->gpio_vbus_det);
439                         if (ret)
440                                 return ret;
441                 }
442
443                 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
444                 if (phy->gpio_id_det >= 0) {
445                         ret = gpio_request(phy->gpio_id_det, "usb_id_det");
446                         if (ret)
447                                 return ret;
448                         ret = gpio_direction_input(phy->gpio_id_det);
449                         if (ret)
450                                 return ret;
451                         sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
452                 }
453
454                 if (i || data->cfg->phy0_dual_route) {
455                         snprintf(name, sizeof(name), "pmu%d", i);
456                         phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
457                         if (IS_ERR(phy->pmu))
458                                 return PTR_ERR(phy->pmu);
459                 }
460
461                 phy->id = i;
462                 phy->rst_mask = info->rst_mask;
463         };
464
465         debug("Allwinner Sun4I USB PHY driver loaded\n");
466         return 0;
467 }
468
469 static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
470         .num_phys = 3,
471         .type = sun4i_a10_phy,
472         .disc_thresh = 3,
473         .phyctl_offset = REG_PHYCTL_A10,
474         .enable_pmu_unk1 = false,
475 };
476
477 static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
478         .num_phys = 2,
479         .type = sun4i_a10_phy,
480         .disc_thresh = 2,
481         .phyctl_offset = REG_PHYCTL_A10,
482         .enable_pmu_unk1 = false,
483 };
484
485 static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
486         .num_phys = 3,
487         .type = sun6i_a31_phy,
488         .disc_thresh = 3,
489         .phyctl_offset = REG_PHYCTL_A10,
490         .enable_pmu_unk1 = false,
491 };
492
493 static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
494         .num_phys = 3,
495         .type = sun4i_a10_phy,
496         .disc_thresh = 2,
497         .phyctl_offset = REG_PHYCTL_A10,
498         .enable_pmu_unk1 = false,
499 };
500
501 static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
502         .num_phys = 2,
503         .type = sun4i_a10_phy,
504         .disc_thresh = 3,
505         .phyctl_offset = REG_PHYCTL_A10,
506         .enable_pmu_unk1 = false,
507 };
508
509 static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
510         .num_phys = 2,
511         .type = sun8i_a33_phy,
512         .disc_thresh = 3,
513         .phyctl_offset = REG_PHYCTL_A33,
514         .enable_pmu_unk1 = false,
515 };
516
517 static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
518         .num_phys = 3,
519         .type = sun8i_a83t_phy,
520         .phyctl_offset = REG_PHYCTL_A33,
521 };
522
523 static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
524         .num_phys = 4,
525         .type = sun8i_h3_phy,
526         .disc_thresh = 3,
527         .phyctl_offset = REG_PHYCTL_A33,
528         .enable_pmu_unk1 = true,
529         .phy0_dual_route = true,
530 };
531
532 static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
533         .num_phys = 1,
534         .type = sun8i_v3s_phy,
535         .disc_thresh = 3,
536         .phyctl_offset = REG_PHYCTL_A33,
537         .enable_pmu_unk1 = true,
538         .phy0_dual_route = true,
539 };
540
541 static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
542         .num_phys = 2,
543         .type = sun50i_a64_phy,
544         .disc_thresh = 3,
545         .phyctl_offset = REG_PHYCTL_A33,
546         .enable_pmu_unk1 = true,
547         .phy0_dual_route = true,
548 };
549
550 static const struct udevice_id sun4i_usb_phy_ids[] = {
551         { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
552         { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
553         { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
554         { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
555         { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
556         { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
557         { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
558         { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
559         { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
560         { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
561         { }
562 };
563
564 U_BOOT_DRIVER(sun4i_usb_phy) = {
565         .name   = "sun4i_usb_phy",
566         .id     = UCLASS_PHY,
567         .of_match = sun4i_usb_phy_ids,
568         .ops = &sun4i_usb_phy_ops,
569         .probe = sun4i_usb_phy_probe,
570         .platdata_auto_alloc_size = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
571         .priv_auto_alloc_size = sizeof(struct sun4i_usb_phy_data),
572 };