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/err.h>
29 #define REG_PHYCTL_A10 0x04
30 #define REG_PHYBIST 0x08
31 #define REG_PHYTUNE 0x0c
32 #define REG_PHYCTL_A33 0x10
33 #define REG_PHY_OTGCTL 0x20
34 #define REG_PMU_UNK1 0x10
36 /* Common Control Bits for Both PHYs */
37 #define PHY_PLL_BW 0x03
38 #define PHY_RES45_CAL_EN 0x0c
40 /* Private Control Bits for Each PHY */
41 #define PHY_TX_AMPLITUDE_TUNE 0x20
42 #define PHY_TX_SLEWRATE_TUNE 0x22
43 #define PHY_DISCON_TH_SEL 0x2a
44 #define PHY_SQUELCH_DETECT 0x3c
46 #define PHYCTL_DATA BIT(7)
47 #define OTGCTL_ROUTE_MUSB BIT(0)
49 #define PHY_TX_RATE BIT(4)
50 #define PHY_TX_MAGNITUDE BIT(2)
51 #define PHY_TX_AMPLITUDE_LEN 5
53 #define PHY_RES45_CAL_DATA BIT(0)
54 #define PHY_RES45_CAL_LEN 1
55 #define PHY_DISCON_TH_LEN 2
57 #define SUNXI_AHB_ICHR8_EN BIT(10)
58 #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
59 #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
60 #define SUNXI_ULPI_BYPASS_EN BIT(0)
62 /* A83T specific control bits for PHY0 */
63 #define PHY_CTL_VBUSVLDEXT BIT(5)
64 #define PHY_CTL_SIDDQ BIT(3)
66 /* A83T specific control bits for PHY2 HSIC */
67 #define SUNXI_EHCI_HS_FORCE BIT(20)
68 #define SUNXI_HSIC_CONNECT_INT BIT(16)
69 #define SUNXI_HSIC BIT(1)
73 enum sun4i_usb_phy_type {
85 struct sun4i_usb_phy_cfg {
87 enum sun4i_usb_phy_type type;
90 bool dedicated_clocks;
96 struct sun4i_usb_phy_info {
97 const char *gpio_vbus;
98 const char *gpio_vbus_det;
99 const char *gpio_id_det;
102 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
103 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
104 .gpio_id_det = CONFIG_USB0_ID_DET,
107 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
108 .gpio_vbus_det = NULL,
112 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
113 .gpio_vbus_det = NULL,
117 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
118 .gpio_vbus_det = NULL,
123 struct sun4i_usb_phy_plat {
130 struct reset_ctl resets;
134 struct sun4i_usb_phy_data {
136 const struct sun4i_usb_phy_cfg *cfg;
137 struct sun4i_usb_phy_plat *usb_phy;
140 static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
142 static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
144 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
145 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
146 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
147 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
150 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
151 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
155 for (i = 0; i < len; i++) {
156 temp = readl(phyctl);
158 /* clear the address portion */
159 temp &= ~(0xff << 8);
161 /* set the address */
162 temp |= ((addr + i) << 8);
163 writel(temp, phyctl);
165 /* set the data bit and clear usbc bit*/
166 temp = readb(phyctl);
170 temp &= ~PHYCTL_DATA;
172 writeb(temp, phyctl);
175 temp = readb(phyctl);
177 writeb(temp, phyctl);
179 temp = readb(phyctl);
181 writeb(temp, phyctl);
187 static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
189 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
190 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
196 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
197 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
199 /* A83T USB2 is HSIC */
200 if (data->cfg->type == sun8i_a83t_phy && usb_phy->id == 2)
201 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
204 reg_value = readl(usb_phy->pmu);
211 writel(reg_value, usb_phy->pmu);
214 static int sun4i_usb_phy_power_on(struct phy *phy)
216 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
217 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
219 if (initial_usb_scan_delay) {
220 mdelay(initial_usb_scan_delay);
221 initial_usb_scan_delay = 0;
224 usb_phy->power_on_count++;
225 if (usb_phy->power_on_count != 1)
228 if (usb_phy->gpio_vbus >= 0)
229 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
234 static int sun4i_usb_phy_power_off(struct phy *phy)
236 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
237 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
239 usb_phy->power_on_count--;
240 if (usb_phy->power_on_count != 0)
243 if (usb_phy->gpio_vbus >= 0)
244 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
249 static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
253 regval = readl(data->base + REG_PHY_OTGCTL);
255 /* Host mode. Route phy0 to EHCI/OHCI */
256 regval &= ~OTGCTL_ROUTE_MUSB;
258 /* Peripheral mode. Route phy0 to MUSB */
259 regval |= OTGCTL_ROUTE_MUSB;
261 writel(regval, data->base + REG_PHY_OTGCTL);
264 static int sun4i_usb_phy_init(struct phy *phy)
266 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
267 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
271 ret = clk_enable(&usb_phy->clocks);
273 dev_err(dev, "failed to enable usb_%ldphy clock\n", phy->id);
277 ret = reset_deassert(&usb_phy->resets);
279 dev_err(dev, "failed to deassert usb_%ldreset reset\n", phy->id);
283 if (data->cfg->type == sun8i_a83t_phy) {
285 val = readl(data->base + data->cfg->phyctl_offset);
286 val |= PHY_CTL_VBUSVLDEXT;
287 val &= ~PHY_CTL_SIDDQ;
288 writel(val, data->base + data->cfg->phyctl_offset);
291 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
292 val = readl(usb_phy->pmu + REG_PMU_UNK1);
293 writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
296 if (usb_phy->id == 0)
297 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
301 /* Adjust PHY's magnitude and rate */
302 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
303 PHY_TX_MAGNITUDE | PHY_TX_RATE,
304 PHY_TX_AMPLITUDE_LEN);
306 /* Disconnect threshold adjustment */
307 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
308 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
311 sun4i_usb_phy_passby(phy, true);
313 sun4i_usb_phy0_reroute(data, true);
318 static int sun4i_usb_phy_exit(struct phy *phy)
320 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
321 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
325 if (data->cfg->type == sun8i_a83t_phy) {
326 void __iomem *phyctl = data->base +
327 data->cfg->phyctl_offset;
329 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
333 sun4i_usb_phy_passby(phy, false);
335 ret = clk_disable(&usb_phy->clocks);
337 dev_err(dev, "failed to disable usb_%ldphy clock\n", phy->id);
341 ret = reset_assert(&usb_phy->resets);
343 dev_err(dev, "failed to assert usb_%ldreset reset\n", phy->id);
350 static int sun4i_usb_phy_xlate(struct phy *phy,
351 struct ofnode_phandle_args *args)
353 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
355 if (args->args_count >= data->cfg->num_phys)
358 if (data->cfg->missing_phys & BIT(args->args[0]))
361 if (args->args_count)
362 phy->id = args->args[0];
366 debug("%s: phy_id = %ld\n", __func__, phy->id);
370 int sun4i_usb_phy_vbus_detect(struct phy *phy)
372 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
373 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
374 int err, retries = 3;
376 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
378 if (usb_phy->gpio_vbus_det < 0)
379 return usb_phy->gpio_vbus_det;
381 err = gpio_get_value(usb_phy->gpio_vbus_det);
383 * Vbus may have been provided by the board and just been turned of
384 * some milliseconds ago on reset, what we're measuring then is a
385 * residual charge on Vbus, sleep a bit and try again.
387 while (err > 0 && retries--) {
389 err = gpio_get_value(usb_phy->gpio_vbus_det);
395 int sun4i_usb_phy_id_detect(struct phy *phy)
397 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
398 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
400 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
402 if (usb_phy->gpio_id_det < 0)
403 return usb_phy->gpio_id_det;
405 return gpio_get_value(usb_phy->gpio_id_det);
408 void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
410 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
413 static struct phy_ops sun4i_usb_phy_ops = {
414 .of_xlate = sun4i_usb_phy_xlate,
415 .init = sun4i_usb_phy_init,
416 .power_on = sun4i_usb_phy_power_on,
417 .power_off = sun4i_usb_phy_power_off,
418 .exit = sun4i_usb_phy_exit,
421 static int sun4i_usb_phy_probe(struct udevice *dev)
423 struct sun4i_usb_phy_plat *plat = dev_get_platdata(dev);
424 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
427 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
431 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
432 if (IS_ERR(data->base))
433 return PTR_ERR(data->base);
435 data->usb_phy = plat;
436 for (i = 0; i < data->cfg->num_phys; i++) {
437 struct sun4i_usb_phy_plat *phy = &plat[i];
438 struct sun4i_usb_phy_info *info = &phy_info[i];
441 if (data->cfg->missing_phys & BIT(i))
444 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
445 if (phy->gpio_vbus >= 0) {
446 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
449 ret = gpio_direction_output(phy->gpio_vbus, 0);
454 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
455 if (phy->gpio_vbus_det >= 0) {
456 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
459 ret = gpio_direction_input(phy->gpio_vbus_det);
464 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
465 if (phy->gpio_id_det >= 0) {
466 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
469 ret = gpio_direction_input(phy->gpio_id_det);
472 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
475 if (data->cfg->dedicated_clocks)
476 snprintf(name, sizeof(name), "usb%d_phy", i);
478 strlcpy(name, "usb_phy", sizeof(name));
480 ret = clk_get_by_name(dev, name, &phy->clocks);
482 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
486 snprintf(name, sizeof(name), "usb%d_reset", i);
487 ret = reset_get_by_name(dev, name, &phy->resets);
489 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
493 if (i || data->cfg->phy0_dual_route) {
494 snprintf(name, sizeof(name), "pmu%d", i);
495 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
496 if (IS_ERR(phy->pmu))
497 return PTR_ERR(phy->pmu);
503 debug("Allwinner Sun4I USB PHY driver loaded\n");
507 static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
509 .type = sun4i_a10_phy,
511 .phyctl_offset = REG_PHYCTL_A10,
512 .dedicated_clocks = false,
513 .enable_pmu_unk1 = false,
516 static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
518 .type = sun4i_a10_phy,
520 .phyctl_offset = REG_PHYCTL_A10,
521 .dedicated_clocks = false,
522 .enable_pmu_unk1 = false,
525 static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
527 .type = sun6i_a31_phy,
529 .phyctl_offset = REG_PHYCTL_A10,
530 .dedicated_clocks = true,
531 .enable_pmu_unk1 = false,
534 static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
536 .type = sun4i_a10_phy,
538 .phyctl_offset = REG_PHYCTL_A10,
539 .dedicated_clocks = false,
540 .enable_pmu_unk1 = false,
543 static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
545 .type = sun4i_a10_phy,
547 .phyctl_offset = REG_PHYCTL_A10,
548 .dedicated_clocks = true,
549 .enable_pmu_unk1 = false,
552 static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
554 .type = sun8i_a33_phy,
556 .phyctl_offset = REG_PHYCTL_A33,
557 .dedicated_clocks = true,
558 .enable_pmu_unk1 = false,
561 static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
563 .type = sun8i_a83t_phy,
564 .phyctl_offset = REG_PHYCTL_A33,
565 .dedicated_clocks = true,
568 static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
570 .type = sun8i_h3_phy,
572 .phyctl_offset = REG_PHYCTL_A33,
573 .dedicated_clocks = true,
574 .enable_pmu_unk1 = true,
575 .phy0_dual_route = true,
578 static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
580 .type = sun8i_r40_phy,
582 .phyctl_offset = REG_PHYCTL_A33,
583 .dedicated_clocks = true,
584 .enable_pmu_unk1 = true,
585 .phy0_dual_route = true,
588 static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
590 .type = sun8i_v3s_phy,
592 .phyctl_offset = REG_PHYCTL_A33,
593 .dedicated_clocks = true,
594 .enable_pmu_unk1 = true,
595 .phy0_dual_route = true,
598 static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
600 .type = sun50i_a64_phy,
602 .phyctl_offset = REG_PHYCTL_A33,
603 .dedicated_clocks = true,
604 .enable_pmu_unk1 = true,
605 .phy0_dual_route = true,
608 static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
610 .type = sun50i_h6_phy,
612 .phyctl_offset = REG_PHYCTL_A33,
613 .dedicated_clocks = true,
614 .enable_pmu_unk1 = true,
615 .phy0_dual_route = true,
616 .missing_phys = BIT(1) | BIT(2),
619 static const struct udevice_id sun4i_usb_phy_ids[] = {
620 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
621 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
622 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
623 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
624 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
625 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
626 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
627 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
628 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
629 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
630 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
631 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
635 U_BOOT_DRIVER(sun4i_usb_phy) = {
636 .name = "sun4i_usb_phy",
638 .of_match = sun4i_usb_phy_ids,
639 .ops = &sun4i_usb_phy_ops,
640 .probe = sun4i_usb_phy_probe,
641 .platdata_auto_alloc_size = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
642 .priv_auto_alloc_size = sizeof(struct sun4i_usb_phy_data),