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+
15 #include <dm/device.h>
16 #include <generic-phy.h>
17 #include <phy-sun4i-usb.h>
20 #include <asm/arch/clock.h>
21 #include <asm/arch/cpu.h>
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
31 /* Common Control Bits for Both PHYs */
32 #define PHY_PLL_BW 0x03
33 #define PHY_RES45_CAL_EN 0x0c
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
41 #define PHYCTL_DATA BIT(7)
42 #define OTGCTL_ROUTE_MUSB BIT(0)
44 #define PHY_TX_RATE BIT(4)
45 #define PHY_TX_MAGNITUDE BIT(2)
46 #define PHY_TX_AMPLITUDE_LEN 5
48 #define PHY_RES45_CAL_DATA BIT(0)
49 #define PHY_RES45_CAL_LEN 1
50 #define PHY_DISCON_TH_LEN 2
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)
57 /* A83T specific control bits for PHY0 */
58 #define PHY_CTL_VBUSVLDEXT BIT(5)
59 #define PHY_CTL_SIDDQ BIT(3)
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)
68 enum sun4i_usb_phy_type {
78 struct sun4i_usb_phy_cfg {
80 enum sun4i_usb_phy_type type;
87 struct sun4i_usb_phy_info {
88 const char *gpio_vbus;
89 const char *gpio_vbus_det;
90 const char *gpio_id_det;
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),
100 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
101 .gpio_vbus_det = NULL,
103 .rst_mask = (CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK),
106 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
107 .gpio_vbus_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),
113 .rst_mask = (CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK),
117 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
118 .gpio_vbus_det = NULL,
120 #ifdef CONFIG_MACH_SUN6I
121 .rst_mask = (CCM_USB_CTRL_PHY3_RST | CCM_USB_CTRL_PHY3_CLK),
126 struct sun4i_usb_phy_plat {
136 struct sun4i_usb_phy_data {
138 struct sunxi_ccm_reg *ccm;
139 const struct sun4i_usb_phy_cfg *cfg;
140 struct sun4i_usb_phy_plat *usb_phy;
143 static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
145 static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
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;
153 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
154 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
158 for (i = 0; i < len; i++) {
159 temp = readl(phyctl);
161 /* clear the address portion */
162 temp &= ~(0xff << 8);
164 /* set the address */
165 temp |= ((addr + i) << 8);
166 writel(temp, phyctl);
168 /* set the data bit and clear usbc bit*/
169 temp = readb(phyctl);
173 temp &= ~PHYCTL_DATA;
175 writeb(temp, phyctl);
178 temp = readb(phyctl);
180 writeb(temp, phyctl);
182 temp = readb(phyctl);
184 writeb(temp, phyctl);
190 static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
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];
199 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
200 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
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 |
207 reg_value = readl(usb_phy->pmu);
214 writel(reg_value, usb_phy->pmu);
217 static int sun4i_usb_phy_power_on(struct phy *phy)
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];
222 if (initial_usb_scan_delay) {
223 mdelay(initial_usb_scan_delay);
224 initial_usb_scan_delay = 0;
227 usb_phy->power_on_count++;
228 if (usb_phy->power_on_count != 1)
231 if (usb_phy->gpio_vbus >= 0)
232 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
237 static int sun4i_usb_phy_power_off(struct phy *phy)
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];
242 usb_phy->power_on_count--;
243 if (usb_phy->power_on_count != 0)
246 if (usb_phy->gpio_vbus >= 0)
247 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
252 static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
256 regval = readl(data->base + REG_PHY_OTGCTL);
258 /* Host mode. Route phy0 to EHCI/OHCI */
259 regval &= ~OTGCTL_ROUTE_MUSB;
261 /* Peripheral mode. Route phy0 to MUSB */
262 regval |= OTGCTL_ROUTE_MUSB;
264 writel(regval, data->base + REG_PHY_OTGCTL);
267 static int sun4i_usb_phy_init(struct phy *phy)
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];
273 setbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask);
275 if (data->cfg->type == sun8i_a83t_phy) {
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);
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);
288 if (usb_phy->id == 0)
289 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
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);
298 /* Disconnect threshold adjustment */
299 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
300 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
303 sun4i_usb_phy_passby(phy, true);
305 sun4i_usb_phy0_reroute(data, true);
310 static int sun4i_usb_phy_exit(struct phy *phy)
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];
316 if (data->cfg->type == sun8i_a83t_phy) {
317 void __iomem *phyctl = data->base +
318 data->cfg->phyctl_offset;
320 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
324 sun4i_usb_phy_passby(phy, false);
326 clrbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask);
331 static int sun4i_usb_phy_xlate(struct phy *phy,
332 struct ofnode_phandle_args *args)
334 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
336 if (args->args_count >= data->cfg->num_phys)
339 if (args->args_count)
340 phy->id = args->args[0];
344 debug("%s: phy_id = %ld\n", __func__, phy->id);
348 int sun4i_usb_phy_vbus_detect(struct phy *phy)
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;
354 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
356 if (usb_phy->gpio_vbus_det < 0)
357 return usb_phy->gpio_vbus_det;
359 err = gpio_get_value(usb_phy->gpio_vbus_det);
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.
365 while (err > 0 && retries--) {
367 err = gpio_get_value(usb_phy->gpio_vbus_det);
373 int sun4i_usb_phy_id_detect(struct phy *phy)
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];
378 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
380 if (usb_phy->gpio_id_det < 0)
381 return usb_phy->gpio_id_det;
383 return gpio_get_value(usb_phy->gpio_id_det);
386 void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
388 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
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,
399 static int sun4i_usb_phy_probe(struct udevice *dev)
401 struct sun4i_usb_phy_plat *plat = dev_get_platdata(dev);
402 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
405 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
409 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
410 if (IS_ERR(data->base))
411 return PTR_ERR(data->base);
413 data->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
414 if (IS_ERR(data->ccm))
415 return PTR_ERR(data->ccm);
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];
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");
428 ret = gpio_direction_output(phy->gpio_vbus, 0);
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");
438 ret = gpio_direction_input(phy->gpio_vbus_det);
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");
448 ret = gpio_direction_input(phy->gpio_id_det);
451 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
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);
462 phy->rst_mask = info->rst_mask;
465 debug("Allwinner Sun4I USB PHY driver loaded\n");
469 static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
471 .type = sun4i_a10_phy,
473 .phyctl_offset = REG_PHYCTL_A10,
474 .enable_pmu_unk1 = false,
477 static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
479 .type = sun4i_a10_phy,
481 .phyctl_offset = REG_PHYCTL_A10,
482 .enable_pmu_unk1 = false,
485 static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
487 .type = sun6i_a31_phy,
489 .phyctl_offset = REG_PHYCTL_A10,
490 .enable_pmu_unk1 = false,
493 static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
495 .type = sun4i_a10_phy,
497 .phyctl_offset = REG_PHYCTL_A10,
498 .enable_pmu_unk1 = false,
501 static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
503 .type = sun4i_a10_phy,
505 .phyctl_offset = REG_PHYCTL_A10,
506 .enable_pmu_unk1 = false,
509 static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
511 .type = sun8i_a33_phy,
513 .phyctl_offset = REG_PHYCTL_A33,
514 .enable_pmu_unk1 = false,
517 static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
519 .type = sun8i_a83t_phy,
520 .phyctl_offset = REG_PHYCTL_A33,
523 static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
525 .type = sun8i_h3_phy,
527 .phyctl_offset = REG_PHYCTL_A33,
528 .enable_pmu_unk1 = true,
529 .phy0_dual_route = true,
532 static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
534 .type = sun8i_v3s_phy,
536 .phyctl_offset = REG_PHYCTL_A33,
537 .enable_pmu_unk1 = true,
538 .phy0_dual_route = true,
541 static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
543 .type = sun50i_a64_phy,
545 .phyctl_offset = REG_PHYCTL_A33,
546 .enable_pmu_unk1 = true,
547 .phy0_dual_route = true,
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},
564 U_BOOT_DRIVER(sun4i_usb_phy) = {
565 .name = "sun4i_usb_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),