2 * Allwinner sun4i USB PHY driver
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>
8 * Modelled arch/arm/mach-sunxi/usb_phy.c to compatible with generic-phy.
10 * SPDX-License-Identifier: GPL-2.0+
17 #include <dm/device.h>
18 #include <generic-phy.h>
19 #include <phy-sun4i-usb.h>
23 #include <asm/arch/clock.h>
24 #include <asm/arch/cpu.h>
25 #include <dm/device_compat.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
30 #define REG_PHYCTL_A10 0x04
31 #define REG_PHYBIST 0x08
32 #define REG_PHYTUNE 0x0c
33 #define REG_PHYCTL_A33 0x10
34 #define REG_PHY_OTGCTL 0x20
35 #define REG_PMU_UNK1 0x10
37 /* Common Control Bits for Both PHYs */
38 #define PHY_PLL_BW 0x03
39 #define PHY_RES45_CAL_EN 0x0c
41 /* Private Control Bits for Each PHY */
42 #define PHY_TX_AMPLITUDE_TUNE 0x20
43 #define PHY_TX_SLEWRATE_TUNE 0x22
44 #define PHY_DISCON_TH_SEL 0x2a
45 #define PHY_SQUELCH_DETECT 0x3c
47 #define PHYCTL_DATA BIT(7)
48 #define OTGCTL_ROUTE_MUSB BIT(0)
50 #define PHY_TX_RATE BIT(4)
51 #define PHY_TX_MAGNITUDE BIT(2)
52 #define PHY_TX_AMPLITUDE_LEN 5
54 #define PHY_RES45_CAL_DATA BIT(0)
55 #define PHY_RES45_CAL_LEN 1
56 #define PHY_DISCON_TH_LEN 2
58 #define SUNXI_AHB_ICHR8_EN BIT(10)
59 #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
60 #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
61 #define SUNXI_ULPI_BYPASS_EN BIT(0)
63 /* A83T specific control bits for PHY0 */
64 #define PHY_CTL_VBUSVLDEXT BIT(5)
65 #define PHY_CTL_SIDDQ BIT(3)
67 /* A83T specific control bits for PHY2 HSIC */
68 #define SUNXI_EHCI_HS_FORCE BIT(20)
69 #define SUNXI_HSIC_CONNECT_INT BIT(16)
70 #define SUNXI_HSIC BIT(1)
74 enum sun4i_usb_phy_type {
86 struct sun4i_usb_phy_cfg {
88 enum sun4i_usb_phy_type type;
91 bool dedicated_clocks;
97 struct sun4i_usb_phy_info {
98 const char *gpio_vbus;
99 const char *gpio_vbus_det;
100 const char *gpio_id_det;
103 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
104 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
105 .gpio_id_det = CONFIG_USB0_ID_DET,
108 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
109 .gpio_vbus_det = NULL,
113 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
114 .gpio_vbus_det = NULL,
118 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
119 .gpio_vbus_det = NULL,
124 struct sun4i_usb_phy_plat {
131 struct reset_ctl resets;
135 struct sun4i_usb_phy_data {
137 const struct sun4i_usb_phy_cfg *cfg;
138 struct sun4i_usb_phy_plat *usb_phy;
141 static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
143 static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
145 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
146 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
147 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
148 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
151 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
152 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
156 for (i = 0; i < len; i++) {
157 temp = readl(phyctl);
159 /* clear the address portion */
160 temp &= ~(0xff << 8);
162 /* set the address */
163 temp |= ((addr + i) << 8);
164 writel(temp, phyctl);
166 /* set the data bit and clear usbc bit*/
167 temp = readb(phyctl);
171 temp &= ~PHYCTL_DATA;
173 writeb(temp, phyctl);
176 temp = readb(phyctl);
178 writeb(temp, phyctl);
180 temp = readb(phyctl);
182 writeb(temp, phyctl);
188 static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
190 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
191 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
197 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
198 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
200 /* A83T USB2 is HSIC */
201 if (data->cfg->type == sun8i_a83t_phy && usb_phy->id == 2)
202 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
205 reg_value = readl(usb_phy->pmu);
212 writel(reg_value, usb_phy->pmu);
215 static int sun4i_usb_phy_power_on(struct phy *phy)
217 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
218 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
220 if (initial_usb_scan_delay) {
221 mdelay(initial_usb_scan_delay);
222 initial_usb_scan_delay = 0;
225 usb_phy->power_on_count++;
226 if (usb_phy->power_on_count != 1)
229 if (usb_phy->gpio_vbus >= 0)
230 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
235 static int sun4i_usb_phy_power_off(struct phy *phy)
237 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
238 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
240 usb_phy->power_on_count--;
241 if (usb_phy->power_on_count != 0)
244 if (usb_phy->gpio_vbus >= 0)
245 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
250 static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
254 regval = readl(data->base + REG_PHY_OTGCTL);
256 /* Host mode. Route phy0 to EHCI/OHCI */
257 regval &= ~OTGCTL_ROUTE_MUSB;
259 /* Peripheral mode. Route phy0 to MUSB */
260 regval |= OTGCTL_ROUTE_MUSB;
262 writel(regval, data->base + REG_PHY_OTGCTL);
265 static int sun4i_usb_phy_init(struct phy *phy)
267 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
268 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
272 ret = clk_enable(&usb_phy->clocks);
274 dev_err(dev, "failed to enable usb_%ldphy clock\n", phy->id);
278 ret = reset_deassert(&usb_phy->resets);
280 dev_err(dev, "failed to deassert usb_%ldreset reset\n", phy->id);
284 if (data->cfg->type == sun8i_a83t_phy) {
286 val = readl(data->base + data->cfg->phyctl_offset);
287 val |= PHY_CTL_VBUSVLDEXT;
288 val &= ~PHY_CTL_SIDDQ;
289 writel(val, data->base + data->cfg->phyctl_offset);
292 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
293 val = readl(usb_phy->pmu + REG_PMU_UNK1);
294 writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
297 if (usb_phy->id == 0)
298 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
302 /* Adjust PHY's magnitude and rate */
303 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
304 PHY_TX_MAGNITUDE | PHY_TX_RATE,
305 PHY_TX_AMPLITUDE_LEN);
307 /* Disconnect threshold adjustment */
308 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
309 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
312 sun4i_usb_phy_passby(phy, true);
314 sun4i_usb_phy0_reroute(data, true);
319 static int sun4i_usb_phy_exit(struct phy *phy)
321 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
322 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
326 if (data->cfg->type == sun8i_a83t_phy) {
327 void __iomem *phyctl = data->base +
328 data->cfg->phyctl_offset;
330 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
334 sun4i_usb_phy_passby(phy, false);
336 ret = clk_disable(&usb_phy->clocks);
338 dev_err(dev, "failed to disable usb_%ldphy clock\n", phy->id);
342 ret = reset_assert(&usb_phy->resets);
344 dev_err(dev, "failed to assert usb_%ldreset reset\n", phy->id);
351 static int sun4i_usb_phy_xlate(struct phy *phy,
352 struct ofnode_phandle_args *args)
354 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
356 if (args->args_count >= data->cfg->num_phys)
359 if (data->cfg->missing_phys & BIT(args->args[0]))
362 if (args->args_count)
363 phy->id = args->args[0];
367 debug("%s: phy_id = %ld\n", __func__, phy->id);
371 int sun4i_usb_phy_vbus_detect(struct phy *phy)
373 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
374 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
375 int err, retries = 3;
377 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
379 if (usb_phy->gpio_vbus_det < 0)
380 return usb_phy->gpio_vbus_det;
382 err = gpio_get_value(usb_phy->gpio_vbus_det);
384 * Vbus may have been provided by the board and just been turned of
385 * some milliseconds ago on reset, what we're measuring then is a
386 * residual charge on Vbus, sleep a bit and try again.
388 while (err > 0 && retries--) {
390 err = gpio_get_value(usb_phy->gpio_vbus_det);
396 int sun4i_usb_phy_id_detect(struct phy *phy)
398 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
399 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
401 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
403 if (usb_phy->gpio_id_det < 0)
404 return usb_phy->gpio_id_det;
406 return gpio_get_value(usb_phy->gpio_id_det);
409 void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
411 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
414 static struct phy_ops sun4i_usb_phy_ops = {
415 .of_xlate = sun4i_usb_phy_xlate,
416 .init = sun4i_usb_phy_init,
417 .power_on = sun4i_usb_phy_power_on,
418 .power_off = sun4i_usb_phy_power_off,
419 .exit = sun4i_usb_phy_exit,
422 static int sun4i_usb_phy_probe(struct udevice *dev)
424 struct sun4i_usb_phy_plat *plat = dev_get_platdata(dev);
425 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
428 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
432 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
433 if (IS_ERR(data->base))
434 return PTR_ERR(data->base);
436 data->usb_phy = plat;
437 for (i = 0; i < data->cfg->num_phys; i++) {
438 struct sun4i_usb_phy_plat *phy = &plat[i];
439 struct sun4i_usb_phy_info *info = &phy_info[i];
442 if (data->cfg->missing_phys & BIT(i))
445 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
446 if (phy->gpio_vbus >= 0) {
447 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
450 ret = gpio_direction_output(phy->gpio_vbus, 0);
455 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
456 if (phy->gpio_vbus_det >= 0) {
457 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
460 ret = gpio_direction_input(phy->gpio_vbus_det);
465 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
466 if (phy->gpio_id_det >= 0) {
467 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
470 ret = gpio_direction_input(phy->gpio_id_det);
473 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
476 if (data->cfg->dedicated_clocks)
477 snprintf(name, sizeof(name), "usb%d_phy", i);
479 strlcpy(name, "usb_phy", sizeof(name));
481 ret = clk_get_by_name(dev, name, &phy->clocks);
483 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
487 snprintf(name, sizeof(name), "usb%d_reset", i);
488 ret = reset_get_by_name(dev, name, &phy->resets);
490 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
494 if (i || data->cfg->phy0_dual_route) {
495 snprintf(name, sizeof(name), "pmu%d", i);
496 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
497 if (IS_ERR(phy->pmu))
498 return PTR_ERR(phy->pmu);
504 debug("Allwinner Sun4I USB PHY driver loaded\n");
508 static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
510 .type = sun4i_a10_phy,
512 .phyctl_offset = REG_PHYCTL_A10,
513 .dedicated_clocks = false,
514 .enable_pmu_unk1 = false,
517 static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
519 .type = sun4i_a10_phy,
521 .phyctl_offset = REG_PHYCTL_A10,
522 .dedicated_clocks = false,
523 .enable_pmu_unk1 = false,
526 static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
528 .type = sun6i_a31_phy,
530 .phyctl_offset = REG_PHYCTL_A10,
531 .dedicated_clocks = true,
532 .enable_pmu_unk1 = false,
535 static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
537 .type = sun4i_a10_phy,
539 .phyctl_offset = REG_PHYCTL_A10,
540 .dedicated_clocks = false,
541 .enable_pmu_unk1 = false,
544 static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
546 .type = sun4i_a10_phy,
548 .phyctl_offset = REG_PHYCTL_A10,
549 .dedicated_clocks = true,
550 .enable_pmu_unk1 = false,
553 static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
555 .type = sun8i_a33_phy,
557 .phyctl_offset = REG_PHYCTL_A33,
558 .dedicated_clocks = true,
559 .enable_pmu_unk1 = false,
562 static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
564 .type = sun8i_a83t_phy,
565 .phyctl_offset = REG_PHYCTL_A33,
566 .dedicated_clocks = true,
569 static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
571 .type = sun8i_h3_phy,
573 .phyctl_offset = REG_PHYCTL_A33,
574 .dedicated_clocks = true,
575 .enable_pmu_unk1 = true,
576 .phy0_dual_route = true,
579 static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
581 .type = sun8i_r40_phy,
583 .phyctl_offset = REG_PHYCTL_A33,
584 .dedicated_clocks = true,
585 .enable_pmu_unk1 = true,
586 .phy0_dual_route = true,
589 static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
591 .type = sun8i_v3s_phy,
593 .phyctl_offset = REG_PHYCTL_A33,
594 .dedicated_clocks = true,
595 .enable_pmu_unk1 = true,
596 .phy0_dual_route = true,
599 static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
601 .type = sun50i_a64_phy,
603 .phyctl_offset = REG_PHYCTL_A33,
604 .dedicated_clocks = true,
605 .enable_pmu_unk1 = true,
606 .phy0_dual_route = true,
609 static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
611 .type = sun50i_h6_phy,
613 .phyctl_offset = REG_PHYCTL_A33,
614 .dedicated_clocks = true,
615 .enable_pmu_unk1 = true,
616 .phy0_dual_route = true,
617 .missing_phys = BIT(1) | BIT(2),
620 static const struct udevice_id sun4i_usb_phy_ids[] = {
621 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
622 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
623 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
624 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
625 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
626 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
627 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
628 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
629 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
630 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
631 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
632 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
636 U_BOOT_DRIVER(sun4i_usb_phy) = {
637 .name = "sun4i_usb_phy",
639 .of_match = sun4i_usb_phy_ids,
640 .ops = &sun4i_usb_phy_ops,
641 .probe = sun4i_usb_phy_probe,
642 .platdata_auto_alloc_size = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
643 .priv_auto_alloc_size = sizeof(struct sun4i_usb_phy_data),