blob: 6a965e704d433f7c57a0962d7728ff7fb4871b36 [file] [log] [blame]
Jagan Tekid3c38282018-05-07 13:03:26 +05301/*
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>
Jagan Teki0dc33332018-08-06 12:16:39 +053014#include <clk.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053015#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053017#include <dm/device.h>
18#include <generic-phy.h>
Jagan Teki21fc42d2018-05-07 13:03:27 +053019#include <phy-sun4i-usb.h>
Jagan Teki0dc33332018-08-06 12:16:39 +053020#include <reset.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053021#include <asm/gpio.h>
22#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070026#include <linux/err.h>
Samuel Hollandc70137c2021-09-12 09:22:42 -050027#include <power/regulator.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053028
29#define REG_ISCR 0x00
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
36
37/* Common Control Bits for Both PHYs */
38#define PHY_PLL_BW 0x03
39#define PHY_RES45_CAL_EN 0x0c
40
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
Jagan Teki37671e12018-05-07 13:03:37 +053045#define PHY_SQUELCH_DETECT 0x3c
Jagan Tekid3c38282018-05-07 13:03:26 +053046
47#define PHYCTL_DATA BIT(7)
48#define OTGCTL_ROUTE_MUSB BIT(0)
49
50#define PHY_TX_RATE BIT(4)
51#define PHY_TX_MAGNITUDE BIT(2)
52#define PHY_TX_AMPLITUDE_LEN 5
53
54#define PHY_RES45_CAL_DATA BIT(0)
55#define PHY_RES45_CAL_LEN 1
56#define PHY_DISCON_TH_LEN 2
57
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)
62
Jagan Teki05a7b9f2018-05-07 13:03:30 +053063/* A83T specific control bits for PHY0 */
64#define PHY_CTL_VBUSVLDEXT BIT(5)
65#define PHY_CTL_SIDDQ BIT(3)
66
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)
71
Jagan Tekid3c38282018-05-07 13:03:26 +053072#define MAX_PHYS 4
73
74enum sun4i_usb_phy_type {
Jagan Teki5a3000f2018-05-07 13:03:31 +053075 sun4i_a10_phy,
Jagan Teki1cbc80c2018-05-07 13:03:32 +053076 sun6i_a31_phy,
Jagan Teki0e574bb2018-05-07 13:03:33 +053077 sun8i_a33_phy,
Jagan Teki05a7b9f2018-05-07 13:03:30 +053078 sun8i_a83t_phy,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +053079 sun8i_h3_phy,
Andre Przywara47d49972020-01-01 23:44:48 +000080 sun8i_r40_phy,
Jagan Tekiac4bab42018-05-07 13:03:29 +053081 sun8i_v3s_phy,
Jagan Tekid3c38282018-05-07 13:03:26 +053082 sun50i_a64_phy,
Andre Przywarab2f0f312019-06-23 15:09:49 +010083 sun50i_h6_phy,
Jagan Tekid3c38282018-05-07 13:03:26 +053084};
85
86struct sun4i_usb_phy_cfg {
87 int num_phys;
88 enum sun4i_usb_phy_type type;
89 u32 disc_thresh;
90 u8 phyctl_offset;
Jagan Teki0dc33332018-08-06 12:16:39 +053091 bool dedicated_clocks;
Jagan Tekid3c38282018-05-07 13:03:26 +053092 bool enable_pmu_unk1;
93 bool phy0_dual_route;
Andre Przywarab2f0f312019-06-23 15:09:49 +010094 int missing_phys;
Jagan Tekid3c38282018-05-07 13:03:26 +053095};
96
97struct sun4i_usb_phy_info {
98 const char *gpio_vbus;
99 const char *gpio_vbus_det;
100 const char *gpio_id_det;
Jagan Tekid3c38282018-05-07 13:03:26 +0530101} phy_info[] = {
102 {
103 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
104 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
105 .gpio_id_det = CONFIG_USB0_ID_DET,
Jagan Tekid3c38282018-05-07 13:03:26 +0530106 },
107 {
108 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
109 .gpio_vbus_det = NULL,
110 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530111 },
112 {
113 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
114 .gpio_vbus_det = NULL,
115 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530116 },
117 {
118 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
119 .gpio_vbus_det = NULL,
120 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530121 },
122};
123
124struct sun4i_usb_phy_plat {
125 void __iomem *pmu;
Andre Przywara3331d222022-06-07 23:36:18 +0100126 struct gpio_desc gpio_vbus;
127 struct gpio_desc gpio_vbus_det;
128 struct gpio_desc gpio_id_det;
Jagan Teki0dc33332018-08-06 12:16:39 +0530129 struct clk clocks;
130 struct reset_ctl resets;
Jagan Tekid3c38282018-05-07 13:03:26 +0530131 int id;
132};
133
134struct sun4i_usb_phy_data {
135 void __iomem *base;
Jagan Tekid3c38282018-05-07 13:03:26 +0530136 const struct sun4i_usb_phy_cfg *cfg;
137 struct sun4i_usb_phy_plat *usb_phy;
Samuel Hollandc70137c2021-09-12 09:22:42 -0500138 struct udevice *vbus_power_supply;
Jagan Tekid3c38282018-05-07 13:03:26 +0530139};
140
141static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
142
143static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
144{
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;
149 int i;
150
151 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
152 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
153 writel(0, phyctl);
154 }
155
156 for (i = 0; i < len; i++) {
157 temp = readl(phyctl);
158
159 /* clear the address portion */
160 temp &= ~(0xff << 8);
161
162 /* set the address */
163 temp |= ((addr + i) << 8);
164 writel(temp, phyctl);
165
166 /* set the data bit and clear usbc bit*/
167 temp = readb(phyctl);
168 if (data & 0x1)
169 temp |= PHYCTL_DATA;
170 else
171 temp &= ~PHYCTL_DATA;
172 temp &= ~usbc_bit;
173 writeb(temp, phyctl);
174
175 /* pulse usbc_bit */
176 temp = readb(phyctl);
177 temp |= usbc_bit;
178 writeb(temp, phyctl);
179
180 temp = readb(phyctl);
181 temp &= ~usbc_bit;
182 writeb(temp, phyctl);
183
184 data >>= 1;
185 }
186}
187
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530188static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Tekid3c38282018-05-07 13:03:26 +0530189{
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530190 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
191 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Tekid3c38282018-05-07 13:03:26 +0530192 u32 bits, reg_value;
193
194 if (!usb_phy->pmu)
195 return;
196
197 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
198 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530199
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 |
203 SUNXI_HSIC;
204
Jagan Tekid3c38282018-05-07 13:03:26 +0530205 reg_value = readl(usb_phy->pmu);
206
207 if (enable)
208 reg_value |= bits;
209 else
210 reg_value &= ~bits;
211
212 writel(reg_value, usb_phy->pmu);
213}
214
215static int sun4i_usb_phy_power_on(struct phy *phy)
216{
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];
219
220 if (initial_usb_scan_delay) {
221 mdelay(initial_usb_scan_delay);
222 initial_usb_scan_delay = 0;
223 }
224
Andre Przywara3331d222022-06-07 23:36:18 +0100225 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
226 dm_gpio_set_value(&usb_phy->gpio_vbus, 1);
Jagan Tekid3c38282018-05-07 13:03:26 +0530227
228 return 0;
229}
230
231static int sun4i_usb_phy_power_off(struct phy *phy)
232{
233 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
234 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
235
Andre Przywara3331d222022-06-07 23:36:18 +0100236 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
237 dm_gpio_set_value(&usb_phy->gpio_vbus, 0);
Jagan Tekid3c38282018-05-07 13:03:26 +0530238
239 return 0;
240}
241
242static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
243{
244 u32 regval;
245
246 regval = readl(data->base + REG_PHY_OTGCTL);
247 if (!id_det) {
248 /* Host mode. Route phy0 to EHCI/OHCI */
249 regval &= ~OTGCTL_ROUTE_MUSB;
250 } else {
251 /* Peripheral mode. Route phy0 to MUSB */
252 regval |= OTGCTL_ROUTE_MUSB;
253 }
254 writel(regval, data->base + REG_PHY_OTGCTL);
255}
256
257static int sun4i_usb_phy_init(struct phy *phy)
258{
259 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
260 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
261 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530262 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530263
Jagan Teki0dc33332018-08-06 12:16:39 +0530264 ret = clk_enable(&usb_phy->clocks);
265 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400266 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
267 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530268 return ret;
269 }
270
271 ret = reset_deassert(&usb_phy->resets);
272 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400273 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
274 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530275 return ret;
276 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530277
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300278 if (data->cfg->type == sun8i_a83t_phy ||
279 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530280 if (phy->id == 0) {
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);
285 }
286 } else {
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);
290 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530291
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530292 if (usb_phy->id == 0)
293 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
294 PHY_RES45_CAL_DATA,
295 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530296
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530297 /* 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);
Jagan Tekid3c38282018-05-07 13:03:26 +0530301
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530302 /* Disconnect threshold adjustment */
303 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
304 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
305 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530306
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000307#ifdef CONFIG_USB_MUSB_SUNXI
308 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
309 if (usb_phy->id != 0)
310 sun4i_usb_phy_passby(phy, true);
311
312 /* Route PHY0 to MUSB to allow USB gadget */
313 if (data->cfg->phy0_dual_route)
314 sun4i_usb_phy0_reroute(data, true);
315#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530316 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530317
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000318 /* Route PHY0 to HCI to allow USB host */
319 if (data->cfg->phy0_dual_route)
320 sun4i_usb_phy0_reroute(data, false);
321#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530322
323 return 0;
324}
325
326static int sun4i_usb_phy_exit(struct phy *phy)
327{
328 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
329 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530330 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530331
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530332 if (phy->id == 0) {
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300333 if (data->cfg->type == sun8i_a83t_phy ||
334 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530335 void __iomem *phyctl = data->base +
336 data->cfg->phyctl_offset;
337
338 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
339 }
340 }
341
342 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530343
Jagan Teki0dc33332018-08-06 12:16:39 +0530344 ret = clk_disable(&usb_phy->clocks);
345 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400346 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
347 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530348 return ret;
349 }
350
351 ret = reset_assert(&usb_phy->resets);
352 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400353 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
354 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530355 return ret;
356 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530357
358 return 0;
359}
360
361static int sun4i_usb_phy_xlate(struct phy *phy,
362 struct ofnode_phandle_args *args)
363{
364 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
365
366 if (args->args_count >= data->cfg->num_phys)
367 return -EINVAL;
368
Andre Przywarab2f0f312019-06-23 15:09:49 +0100369 if (data->cfg->missing_phys & BIT(args->args[0]))
370 return -ENODEV;
371
Jagan Tekid3c38282018-05-07 13:03:26 +0530372 if (args->args_count)
373 phy->id = args->args[0];
374 else
375 phy->id = 0;
376
377 debug("%s: phy_id = %ld\n", __func__, phy->id);
378 return 0;
379}
380
Jagan Teki21fc42d2018-05-07 13:03:27 +0530381int sun4i_usb_phy_vbus_detect(struct phy *phy)
382{
383 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
384 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500385 int err = 1, retries = 3;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530386
Andre Przywara3331d222022-06-07 23:36:18 +0100387 if (dm_gpio_is_valid(&usb_phy->gpio_vbus_det)) {
388 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500389 /*
390 * Vbus may have been provided by the board and just turned off
391 * some milliseconds ago on reset. What we're measuring then is
392 * a residual charge on Vbus. Sleep a bit and try again.
393 */
394 while (err > 0 && retries--) {
395 mdelay(100);
Andre Przywara3331d222022-06-07 23:36:18 +0100396 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500397 }
Samuel Hollandc70137c2021-09-12 09:22:42 -0500398 } else if (data->vbus_power_supply) {
399 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530400 }
401
402 return err;
403}
404
405int sun4i_usb_phy_id_detect(struct phy *phy)
406{
407 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
408 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
409
Andre Przywara3331d222022-06-07 23:36:18 +0100410 if (!dm_gpio_is_valid(&usb_phy->gpio_id_det))
411 return -1;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530412
Andre Przywara3331d222022-06-07 23:36:18 +0100413 return dm_gpio_get_value(&usb_phy->gpio_id_det);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530414}
415
Jagan Teki37671e12018-05-07 13:03:37 +0530416void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
417{
418 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
419}
420
Jagan Tekid3c38282018-05-07 13:03:26 +0530421static struct phy_ops sun4i_usb_phy_ops = {
422 .of_xlate = sun4i_usb_phy_xlate,
423 .init = sun4i_usb_phy_init,
424 .power_on = sun4i_usb_phy_power_on,
425 .power_off = sun4i_usb_phy_power_off,
426 .exit = sun4i_usb_phy_exit,
427};
428
429static int sun4i_usb_phy_probe(struct udevice *dev)
430{
Simon Glassfa20e932020-12-03 16:55:20 -0700431 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530432 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
433 int i, ret;
434
435 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
436 if (!data->cfg)
437 return -EINVAL;
438
439 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
440 if (IS_ERR(data->base))
441 return PTR_ERR(data->base);
442
Samuel Hollandc70137c2021-09-12 09:22:42 -0500443 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
444 &data->vbus_power_supply);
445
Jagan Tekid3c38282018-05-07 13:03:26 +0530446 data->usb_phy = plat;
447 for (i = 0; i < data->cfg->num_phys; i++) {
448 struct sun4i_usb_phy_plat *phy = &plat[i];
449 struct sun4i_usb_phy_info *info = &phy_info[i];
450 char name[16];
451
Andre Przywarab2f0f312019-06-23 15:09:49 +0100452 if (data->cfg->missing_phys & BIT(i))
453 continue;
454
Andre Przywara3331d222022-06-07 23:36:18 +0100455 ret = dm_gpio_lookup_name(info->gpio_vbus, &phy->gpio_vbus);
456 if (ret == 0) {
457 ret = dm_gpio_request(&phy->gpio_vbus, "usb_vbus");
Jagan Tekid3c38282018-05-07 13:03:26 +0530458 if (ret)
459 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100460 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus,
461 GPIOD_IS_OUT);
Jagan Tekid3c38282018-05-07 13:03:26 +0530462 if (ret)
463 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100464 ret = dm_gpio_set_value(&phy->gpio_vbus, 0);
465 if (ret)
466 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530467 }
468
Andre Przywara3331d222022-06-07 23:36:18 +0100469 ret = dm_gpio_lookup_name(info->gpio_vbus_det,
470 &phy->gpio_vbus_det);
471 if (ret == 0) {
472 ret = dm_gpio_request(&phy->gpio_vbus_det,
473 "usb_vbus_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530474 if (ret)
475 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100476 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus_det,
477 GPIOD_IS_IN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530478 if (ret)
479 return ret;
480 }
481
Andre Przywara3331d222022-06-07 23:36:18 +0100482 ret = dm_gpio_lookup_name(info->gpio_id_det, &phy->gpio_id_det);
483 if (ret == 0) {
484 ret = dm_gpio_request(&phy->gpio_id_det, "usb_id_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530485 if (ret)
486 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100487 ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,
488 GPIOD_IS_IN | GPIOD_PULL_UP);
Jagan Tekid3c38282018-05-07 13:03:26 +0530489 if (ret)
490 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530491 }
492
Jagan Teki0dc33332018-08-06 12:16:39 +0530493 if (data->cfg->dedicated_clocks)
494 snprintf(name, sizeof(name), "usb%d_phy", i);
495 else
496 strlcpy(name, "usb_phy", sizeof(name));
497
498 ret = clk_get_by_name(dev, name, &phy->clocks);
499 if (ret) {
500 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
501 return ret;
502 }
503
504 snprintf(name, sizeof(name), "usb%d_reset", i);
505 ret = reset_get_by_name(dev, name, &phy->resets);
506 if (ret) {
507 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
508 return ret;
509 }
510
Jagan Tekid3c38282018-05-07 13:03:26 +0530511 if (i || data->cfg->phy0_dual_route) {
512 snprintf(name, sizeof(name), "pmu%d", i);
513 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
514 if (IS_ERR(phy->pmu))
515 return PTR_ERR(phy->pmu);
516 }
517
518 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530519 };
520
Jagan Tekid3c38282018-05-07 13:03:26 +0530521 debug("Allwinner Sun4I USB PHY driver loaded\n");
522 return 0;
523}
524
Jagan Teki5a3000f2018-05-07 13:03:31 +0530525static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
526 .num_phys = 3,
527 .type = sun4i_a10_phy,
528 .disc_thresh = 3,
529 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530530 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530531 .enable_pmu_unk1 = false,
532};
533
534static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
535 .num_phys = 2,
536 .type = sun4i_a10_phy,
537 .disc_thresh = 2,
538 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530539 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530540 .enable_pmu_unk1 = false,
541};
542
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530543static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
544 .num_phys = 3,
545 .type = sun6i_a31_phy,
546 .disc_thresh = 3,
547 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530548 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530549 .enable_pmu_unk1 = false,
550};
551
Jagan Teki5a3000f2018-05-07 13:03:31 +0530552static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
553 .num_phys = 3,
554 .type = sun4i_a10_phy,
555 .disc_thresh = 2,
556 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530557 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530558 .enable_pmu_unk1 = false,
559};
560
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530561static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
562 .num_phys = 2,
563 .type = sun4i_a10_phy,
564 .disc_thresh = 3,
565 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530566 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530567 .enable_pmu_unk1 = false,
568};
569
Jagan Teki0e574bb2018-05-07 13:03:33 +0530570static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
571 .num_phys = 2,
572 .type = sun8i_a33_phy,
573 .disc_thresh = 3,
574 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530575 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530576 .enable_pmu_unk1 = false,
577};
578
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530579static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
580 .num_phys = 3,
581 .type = sun8i_a83t_phy,
582 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530583 .dedicated_clocks = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530584};
585
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530586static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
587 .num_phys = 4,
588 .type = sun8i_h3_phy,
589 .disc_thresh = 3,
590 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530591 .dedicated_clocks = true,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530592 .enable_pmu_unk1 = true,
593 .phy0_dual_route = true,
594};
595
Andre Przywara47d49972020-01-01 23:44:48 +0000596static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
597 .num_phys = 3,
598 .type = sun8i_r40_phy,
599 .disc_thresh = 3,
600 .phyctl_offset = REG_PHYCTL_A33,
601 .dedicated_clocks = true,
602 .enable_pmu_unk1 = true,
603 .phy0_dual_route = true,
604};
605
Jagan Tekiac4bab42018-05-07 13:03:29 +0530606static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
607 .num_phys = 1,
608 .type = sun8i_v3s_phy,
609 .disc_thresh = 3,
610 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530611 .dedicated_clocks = true,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530612 .enable_pmu_unk1 = true,
613 .phy0_dual_route = true,
614};
615
Jagan Tekid3c38282018-05-07 13:03:26 +0530616static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
617 .num_phys = 2,
618 .type = sun50i_a64_phy,
619 .disc_thresh = 3,
620 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530621 .dedicated_clocks = true,
Jagan Tekid3c38282018-05-07 13:03:26 +0530622 .enable_pmu_unk1 = true,
623 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100624};
625
626static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
627 .num_phys = 4,
628 .type = sun50i_h6_phy,
629 .disc_thresh = 3,
630 .phyctl_offset = REG_PHYCTL_A33,
631 .dedicated_clocks = true,
632 .enable_pmu_unk1 = true,
633 .phy0_dual_route = true,
634 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530635};
636
637static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530638 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
639 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530640 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530641 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530642 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530643 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530644 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530645 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000646 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530647 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530648 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100649 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Jagan Tekid3c38282018-05-07 13:03:26 +0530650 { }
651};
652
653U_BOOT_DRIVER(sun4i_usb_phy) = {
654 .name = "sun4i_usb_phy",
655 .id = UCLASS_PHY,
656 .of_match = sun4i_usb_phy_ids,
657 .ops = &sun4i_usb_phy_ops,
658 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700659 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700660 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530661};