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+
16 #include <dm/device.h>
17 #include <generic-phy.h>
18 #include <phy-sun4i-usb.h>
22 #include <asm/arch/clock.h>
23 #include <asm/arch/cpu.h>
26 #define REG_PHYCTL_A10 0x04
27 #define REG_PHYBIST 0x08
28 #define REG_PHYTUNE 0x0c
29 #define REG_PHYCTL_A33 0x10
30 #define REG_PHY_OTGCTL 0x20
31 #define REG_PMU_UNK1 0x10
33 /* Common Control Bits for Both PHYs */
34 #define PHY_PLL_BW 0x03
35 #define PHY_RES45_CAL_EN 0x0c
37 /* Private Control Bits for Each PHY */
38 #define PHY_TX_AMPLITUDE_TUNE 0x20
39 #define PHY_TX_SLEWRATE_TUNE 0x22
40 #define PHY_DISCON_TH_SEL 0x2a
41 #define PHY_SQUELCH_DETECT 0x3c
43 #define PHYCTL_DATA BIT(7)
44 #define OTGCTL_ROUTE_MUSB BIT(0)
46 #define PHY_TX_RATE BIT(4)
47 #define PHY_TX_MAGNITUDE BIT(2)
48 #define PHY_TX_AMPLITUDE_LEN 5
50 #define PHY_RES45_CAL_DATA BIT(0)
51 #define PHY_RES45_CAL_LEN 1
52 #define PHY_DISCON_TH_LEN 2
54 #define SUNXI_AHB_ICHR8_EN BIT(10)
55 #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
56 #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
57 #define SUNXI_ULPI_BYPASS_EN BIT(0)
59 /* A83T specific control bits for PHY0 */
60 #define PHY_CTL_VBUSVLDEXT BIT(5)
61 #define PHY_CTL_SIDDQ BIT(3)
63 /* A83T specific control bits for PHY2 HSIC */
64 #define SUNXI_EHCI_HS_FORCE BIT(20)
65 #define SUNXI_HSIC_CONNECT_INT BIT(16)
66 #define SUNXI_HSIC BIT(1)
70 enum sun4i_usb_phy_type {
81 struct sun4i_usb_phy_cfg {
83 enum sun4i_usb_phy_type type;
86 bool dedicated_clocks;
92 struct sun4i_usb_phy_info {
93 const char *gpio_vbus;
94 const char *gpio_vbus_det;
95 const char *gpio_id_det;
98 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
99 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
100 .gpio_id_det = CONFIG_USB0_ID_DET,
103 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
104 .gpio_vbus_det = NULL,
108 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
109 .gpio_vbus_det = NULL,
113 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
114 .gpio_vbus_det = NULL,
119 struct sun4i_usb_phy_plat {
126 struct reset_ctl resets;
130 struct sun4i_usb_phy_data {
132 const struct sun4i_usb_phy_cfg *cfg;
133 struct sun4i_usb_phy_plat *usb_phy;
136 static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
138 static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
140 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
141 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
142 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
143 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
146 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
147 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
151 for (i = 0; i < len; i++) {
152 temp = readl(phyctl);
154 /* clear the address portion */
155 temp &= ~(0xff << 8);
157 /* set the address */
158 temp |= ((addr + i) << 8);
159 writel(temp, phyctl);
161 /* set the data bit and clear usbc bit*/
162 temp = readb(phyctl);
166 temp &= ~PHYCTL_DATA;
168 writeb(temp, phyctl);
171 temp = readb(phyctl);
173 writeb(temp, phyctl);
175 temp = readb(phyctl);
177 writeb(temp, phyctl);
183 static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
185 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
186 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
192 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
193 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
195 /* A83T USB2 is HSIC */
196 if (data->cfg->type == sun8i_a83t_phy && usb_phy->id == 2)
197 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
200 reg_value = readl(usb_phy->pmu);
207 writel(reg_value, usb_phy->pmu);
210 static int sun4i_usb_phy_power_on(struct phy *phy)
212 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
213 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
215 if (initial_usb_scan_delay) {
216 mdelay(initial_usb_scan_delay);
217 initial_usb_scan_delay = 0;
220 usb_phy->power_on_count++;
221 if (usb_phy->power_on_count != 1)
224 if (usb_phy->gpio_vbus >= 0)
225 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
230 static int sun4i_usb_phy_power_off(struct phy *phy)
232 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
233 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
235 usb_phy->power_on_count--;
236 if (usb_phy->power_on_count != 0)
239 if (usb_phy->gpio_vbus >= 0)
240 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
245 static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
249 regval = readl(data->base + REG_PHY_OTGCTL);
251 /* Host mode. Route phy0 to EHCI/OHCI */
252 regval &= ~OTGCTL_ROUTE_MUSB;
254 /* Peripheral mode. Route phy0 to MUSB */
255 regval |= OTGCTL_ROUTE_MUSB;
257 writel(regval, data->base + REG_PHY_OTGCTL);
260 static int sun4i_usb_phy_init(struct phy *phy)
262 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
263 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
267 ret = clk_enable(&usb_phy->clocks);
269 dev_err(dev, "failed to enable usb_%ldphy clock\n", phy->id);
273 ret = reset_deassert(&usb_phy->resets);
275 dev_err(dev, "failed to deassert usb_%ldreset reset\n", phy->id);
279 if (data->cfg->type == sun8i_a83t_phy) {
281 val = readl(data->base + data->cfg->phyctl_offset);
282 val |= PHY_CTL_VBUSVLDEXT;
283 val &= ~PHY_CTL_SIDDQ;
284 writel(val, data->base + data->cfg->phyctl_offset);
287 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
288 val = readl(usb_phy->pmu + REG_PMU_UNK1);
289 writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
292 if (usb_phy->id == 0)
293 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
297 /* Adjust PHY's magnitude and rate */
298 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
299 PHY_TX_MAGNITUDE | PHY_TX_RATE,
300 PHY_TX_AMPLITUDE_LEN);
302 /* Disconnect threshold adjustment */
303 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
304 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
307 sun4i_usb_phy_passby(phy, true);
309 sun4i_usb_phy0_reroute(data, true);
314 static int sun4i_usb_phy_exit(struct phy *phy)
316 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
317 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
321 if (data->cfg->type == sun8i_a83t_phy) {
322 void __iomem *phyctl = data->base +
323 data->cfg->phyctl_offset;
325 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
329 sun4i_usb_phy_passby(phy, false);
331 ret = clk_disable(&usb_phy->clocks);
333 dev_err(dev, "failed to disable usb_%ldphy clock\n", phy->id);
337 ret = reset_assert(&usb_phy->resets);
339 dev_err(dev, "failed to assert usb_%ldreset reset\n", phy->id);
346 static int sun4i_usb_phy_xlate(struct phy *phy,
347 struct ofnode_phandle_args *args)
349 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
351 if (args->args_count >= data->cfg->num_phys)
354 if (data->cfg->missing_phys & BIT(args->args[0]))
357 if (args->args_count)
358 phy->id = args->args[0];
362 debug("%s: phy_id = %ld\n", __func__, phy->id);
366 int sun4i_usb_phy_vbus_detect(struct phy *phy)
368 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
369 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
370 int err, retries = 3;
372 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
374 if (usb_phy->gpio_vbus_det < 0)
375 return usb_phy->gpio_vbus_det;
377 err = gpio_get_value(usb_phy->gpio_vbus_det);
379 * Vbus may have been provided by the board and just been turned of
380 * some milliseconds ago on reset, what we're measuring then is a
381 * residual charge on Vbus, sleep a bit and try again.
383 while (err > 0 && retries--) {
385 err = gpio_get_value(usb_phy->gpio_vbus_det);
391 int sun4i_usb_phy_id_detect(struct phy *phy)
393 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
394 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
396 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
398 if (usb_phy->gpio_id_det < 0)
399 return usb_phy->gpio_id_det;
401 return gpio_get_value(usb_phy->gpio_id_det);
404 void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
406 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
409 static struct phy_ops sun4i_usb_phy_ops = {
410 .of_xlate = sun4i_usb_phy_xlate,
411 .init = sun4i_usb_phy_init,
412 .power_on = sun4i_usb_phy_power_on,
413 .power_off = sun4i_usb_phy_power_off,
414 .exit = sun4i_usb_phy_exit,
417 static int sun4i_usb_phy_probe(struct udevice *dev)
419 struct sun4i_usb_phy_plat *plat = dev_get_platdata(dev);
420 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
423 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
427 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
428 if (IS_ERR(data->base))
429 return PTR_ERR(data->base);
431 data->usb_phy = plat;
432 for (i = 0; i < data->cfg->num_phys; i++) {
433 struct sun4i_usb_phy_plat *phy = &plat[i];
434 struct sun4i_usb_phy_info *info = &phy_info[i];
437 if (data->cfg->missing_phys & BIT(i))
440 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
441 if (phy->gpio_vbus >= 0) {
442 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
445 ret = gpio_direction_output(phy->gpio_vbus, 0);
450 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
451 if (phy->gpio_vbus_det >= 0) {
452 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
455 ret = gpio_direction_input(phy->gpio_vbus_det);
460 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
461 if (phy->gpio_id_det >= 0) {
462 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
465 ret = gpio_direction_input(phy->gpio_id_det);
468 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
471 if (data->cfg->dedicated_clocks)
472 snprintf(name, sizeof(name), "usb%d_phy", i);
474 strlcpy(name, "usb_phy", sizeof(name));
476 ret = clk_get_by_name(dev, name, &phy->clocks);
478 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
482 snprintf(name, sizeof(name), "usb%d_reset", i);
483 ret = reset_get_by_name(dev, name, &phy->resets);
485 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
489 if (i || data->cfg->phy0_dual_route) {
490 snprintf(name, sizeof(name), "pmu%d", i);
491 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
492 if (IS_ERR(phy->pmu))
493 return PTR_ERR(phy->pmu);
499 debug("Allwinner Sun4I USB PHY driver loaded\n");
503 static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
505 .type = sun4i_a10_phy,
507 .phyctl_offset = REG_PHYCTL_A10,
508 .dedicated_clocks = false,
509 .enable_pmu_unk1 = false,
512 static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
514 .type = sun4i_a10_phy,
516 .phyctl_offset = REG_PHYCTL_A10,
517 .dedicated_clocks = false,
518 .enable_pmu_unk1 = false,
521 static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
523 .type = sun6i_a31_phy,
525 .phyctl_offset = REG_PHYCTL_A10,
526 .dedicated_clocks = true,
527 .enable_pmu_unk1 = false,
530 static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
532 .type = sun4i_a10_phy,
534 .phyctl_offset = REG_PHYCTL_A10,
535 .dedicated_clocks = false,
536 .enable_pmu_unk1 = false,
539 static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
541 .type = sun4i_a10_phy,
543 .phyctl_offset = REG_PHYCTL_A10,
544 .dedicated_clocks = true,
545 .enable_pmu_unk1 = false,
548 static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
550 .type = sun8i_a33_phy,
552 .phyctl_offset = REG_PHYCTL_A33,
553 .dedicated_clocks = true,
554 .enable_pmu_unk1 = false,
557 static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
559 .type = sun8i_a83t_phy,
560 .phyctl_offset = REG_PHYCTL_A33,
561 .dedicated_clocks = true,
564 static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
566 .type = sun8i_h3_phy,
568 .phyctl_offset = REG_PHYCTL_A33,
569 .dedicated_clocks = true,
570 .enable_pmu_unk1 = true,
571 .phy0_dual_route = true,
574 static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
576 .type = sun8i_v3s_phy,
578 .phyctl_offset = REG_PHYCTL_A33,
579 .dedicated_clocks = true,
580 .enable_pmu_unk1 = true,
581 .phy0_dual_route = true,
584 static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
586 .type = sun50i_a64_phy,
588 .phyctl_offset = REG_PHYCTL_A33,
589 .dedicated_clocks = true,
590 .enable_pmu_unk1 = true,
591 .phy0_dual_route = true,
594 static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
596 .type = sun50i_h6_phy,
598 .phyctl_offset = REG_PHYCTL_A33,
599 .dedicated_clocks = true,
600 .enable_pmu_unk1 = true,
601 .phy0_dual_route = true,
602 .missing_phys = BIT(1) | BIT(2),
605 static const struct udevice_id sun4i_usb_phy_ids[] = {
606 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
607 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
608 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
609 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
610 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
611 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
612 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
613 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
614 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
615 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
616 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
620 U_BOOT_DRIVER(sun4i_usb_phy) = {
621 .name = "sun4i_usb_phy",
623 .of_match = sun4i_usb_phy_ids,
624 .ops = &sun4i_usb_phy_ops,
625 .probe = sun4i_usb_phy_probe,
626 .platdata_auto_alloc_size = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
627 .priv_auto_alloc_size = sizeof(struct sun4i_usb_phy_data),