blob: dbea70f9a5e60254e9027af5304224afe95da90c [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
Andre Przywara8662e7e2022-07-14 23:09:21 -050035
36#define REG_HCI_PHY_CTL 0x10
Jagan Tekid3c38282018-05-07 13:03:26 +053037
38/* Common Control Bits for Both PHYs */
39#define PHY_PLL_BW 0x03
40#define PHY_RES45_CAL_EN 0x0c
41
42/* Private Control Bits for Each PHY */
43#define PHY_TX_AMPLITUDE_TUNE 0x20
44#define PHY_TX_SLEWRATE_TUNE 0x22
45#define PHY_DISCON_TH_SEL 0x2a
Jagan Teki37671e12018-05-07 13:03:37 +053046#define PHY_SQUELCH_DETECT 0x3c
Jagan Tekid3c38282018-05-07 13:03:26 +053047
48#define PHYCTL_DATA BIT(7)
49#define OTGCTL_ROUTE_MUSB BIT(0)
50
51#define PHY_TX_RATE BIT(4)
52#define PHY_TX_MAGNITUDE BIT(2)
53#define PHY_TX_AMPLITUDE_LEN 5
54
55#define PHY_RES45_CAL_DATA BIT(0)
56#define PHY_RES45_CAL_LEN 1
57#define PHY_DISCON_TH_LEN 2
58
59#define SUNXI_AHB_ICHR8_EN BIT(10)
60#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
61#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
62#define SUNXI_ULPI_BYPASS_EN BIT(0)
63
Jagan Teki05a7b9f2018-05-07 13:03:30 +053064/* A83T specific control bits for PHY0 */
65#define PHY_CTL_VBUSVLDEXT BIT(5)
66#define PHY_CTL_SIDDQ BIT(3)
Andre Przywara8662e7e2022-07-14 23:09:21 -050067#define PHY_CTL_H3_SIDDQ BIT(1)
Jagan Teki05a7b9f2018-05-07 13:03:30 +053068
69/* A83T specific control bits for PHY2 HSIC */
70#define SUNXI_EHCI_HS_FORCE BIT(20)
71#define SUNXI_HSIC_CONNECT_INT BIT(16)
72#define SUNXI_HSIC BIT(1)
73
Jagan Tekid3c38282018-05-07 13:03:26 +053074#define MAX_PHYS 4
75
76enum sun4i_usb_phy_type {
Jagan Teki5a3000f2018-05-07 13:03:31 +053077 sun4i_a10_phy,
Jagan Teki1cbc80c2018-05-07 13:03:32 +053078 sun6i_a31_phy,
Jagan Teki0e574bb2018-05-07 13:03:33 +053079 sun8i_a33_phy,
Jagan Teki05a7b9f2018-05-07 13:03:30 +053080 sun8i_a83t_phy,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +053081 sun8i_h3_phy,
Andre Przywara47d49972020-01-01 23:44:48 +000082 sun8i_r40_phy,
Jagan Tekiac4bab42018-05-07 13:03:29 +053083 sun8i_v3s_phy,
Jagan Tekid3c38282018-05-07 13:03:26 +053084 sun50i_a64_phy,
Andre Przywarab2f0f312019-06-23 15:09:49 +010085 sun50i_h6_phy,
Jagan Tekid3c38282018-05-07 13:03:26 +053086};
87
88struct sun4i_usb_phy_cfg {
89 int num_phys;
90 enum sun4i_usb_phy_type type;
91 u32 disc_thresh;
Andre Przywara8662e7e2022-07-14 23:09:21 -050092 u32 hci_phy_ctl_clear;
Jagan Tekid3c38282018-05-07 13:03:26 +053093 u8 phyctl_offset;
Jagan Teki0dc33332018-08-06 12:16:39 +053094 bool dedicated_clocks;
Jagan Tekid3c38282018-05-07 13:03:26 +053095 bool phy0_dual_route;
Andre Przywarab2f0f312019-06-23 15:09:49 +010096 int missing_phys;
Jagan Tekid3c38282018-05-07 13:03:26 +053097};
98
99struct sun4i_usb_phy_info {
100 const char *gpio_vbus;
101 const char *gpio_vbus_det;
102 const char *gpio_id_det;
Jagan Tekid3c38282018-05-07 13:03:26 +0530103} phy_info[] = {
104 {
105 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
106 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
107 .gpio_id_det = CONFIG_USB0_ID_DET,
Jagan Tekid3c38282018-05-07 13:03:26 +0530108 },
109 {
110 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
111 .gpio_vbus_det = NULL,
112 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530113 },
114 {
115 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
116 .gpio_vbus_det = NULL,
117 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530118 },
119 {
120 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
121 .gpio_vbus_det = NULL,
122 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530123 },
124};
125
126struct sun4i_usb_phy_plat {
127 void __iomem *pmu;
Andre Przywara3331d222022-06-07 23:36:18 +0100128 struct gpio_desc gpio_vbus;
129 struct gpio_desc gpio_vbus_det;
130 struct gpio_desc gpio_id_det;
Jagan Teki0dc33332018-08-06 12:16:39 +0530131 struct clk clocks;
132 struct reset_ctl resets;
Jagan Tekid3c38282018-05-07 13:03:26 +0530133 int id;
134};
135
136struct sun4i_usb_phy_data {
137 void __iomem *base;
Jagan Tekid3c38282018-05-07 13:03:26 +0530138 const struct sun4i_usb_phy_cfg *cfg;
139 struct sun4i_usb_phy_plat *usb_phy;
Samuel Hollandc70137c2021-09-12 09:22:42 -0500140 struct udevice *vbus_power_supply;
Jagan Tekid3c38282018-05-07 13:03:26 +0530141};
142
143static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
144
145static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
146{
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;
151 int i;
152
153 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
154 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
155 writel(0, phyctl);
156 }
157
158 for (i = 0; i < len; i++) {
159 temp = readl(phyctl);
160
161 /* clear the address portion */
162 temp &= ~(0xff << 8);
163
164 /* set the address */
165 temp |= ((addr + i) << 8);
166 writel(temp, phyctl);
167
168 /* set the data bit and clear usbc bit*/
169 temp = readb(phyctl);
170 if (data & 0x1)
171 temp |= PHYCTL_DATA;
172 else
173 temp &= ~PHYCTL_DATA;
174 temp &= ~usbc_bit;
175 writeb(temp, phyctl);
176
177 /* pulse usbc_bit */
178 temp = readb(phyctl);
179 temp |= usbc_bit;
180 writeb(temp, phyctl);
181
182 temp = readb(phyctl);
183 temp &= ~usbc_bit;
184 writeb(temp, phyctl);
185
186 data >>= 1;
187 }
188}
189
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530190static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Tekid3c38282018-05-07 13:03:26 +0530191{
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530192 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
193 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Tekid3c38282018-05-07 13:03:26 +0530194 u32 bits, reg_value;
195
196 if (!usb_phy->pmu)
197 return;
198
199 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
200 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530201
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 |
205 SUNXI_HSIC;
206
Jagan Tekid3c38282018-05-07 13:03:26 +0530207 reg_value = readl(usb_phy->pmu);
208
209 if (enable)
210 reg_value |= bits;
211 else
212 reg_value &= ~bits;
213
214 writel(reg_value, usb_phy->pmu);
215}
216
217static int sun4i_usb_phy_power_on(struct phy *phy)
218{
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];
221
222 if (initial_usb_scan_delay) {
223 mdelay(initial_usb_scan_delay);
224 initial_usb_scan_delay = 0;
225 }
226
Samuel Hollandad991492022-07-14 22:34:53 -0500227 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
228 if (phy->id == 0 && sun4i_usb_phy_vbus_detect(phy)) {
229 dev_warn(phy->dev, "External vbus detected, not enabling our own vbus\n");
230 return 0;
231 }
232
Andre Przywara3331d222022-06-07 23:36:18 +0100233 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
234 dm_gpio_set_value(&usb_phy->gpio_vbus, 1);
Jagan Tekid3c38282018-05-07 13:03:26 +0530235
236 return 0;
237}
238
239static int sun4i_usb_phy_power_off(struct phy *phy)
240{
241 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
242 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
243
Andre Przywara3331d222022-06-07 23:36:18 +0100244 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
245 dm_gpio_set_value(&usb_phy->gpio_vbus, 0);
Jagan Tekid3c38282018-05-07 13:03:26 +0530246
247 return 0;
248}
249
250static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
251{
252 u32 regval;
253
254 regval = readl(data->base + REG_PHY_OTGCTL);
255 if (!id_det) {
256 /* Host mode. Route phy0 to EHCI/OHCI */
257 regval &= ~OTGCTL_ROUTE_MUSB;
258 } else {
259 /* Peripheral mode. Route phy0 to MUSB */
260 regval |= OTGCTL_ROUTE_MUSB;
261 }
262 writel(regval, data->base + REG_PHY_OTGCTL);
263}
264
265static int sun4i_usb_phy_init(struct phy *phy)
266{
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];
269 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530270 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530271
Jagan Teki0dc33332018-08-06 12:16:39 +0530272 ret = clk_enable(&usb_phy->clocks);
273 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400274 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
275 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530276 return ret;
277 }
278
279 ret = reset_deassert(&usb_phy->resets);
280 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400281 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
282 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530283 return ret;
284 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530285
Andre Przywara8662e7e2022-07-14 23:09:21 -0500286 if (usb_phy->pmu && data->cfg->hci_phy_ctl_clear) {
287 val = readl(usb_phy->pmu + REG_HCI_PHY_CTL);
288 val &= ~data->cfg->hci_phy_ctl_clear;
289 writel(val, usb_phy->pmu + REG_HCI_PHY_CTL);
290 }
291
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300292 if (data->cfg->type == sun8i_a83t_phy ||
293 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530294 if (phy->id == 0) {
295 val = readl(data->base + data->cfg->phyctl_offset);
296 val |= PHY_CTL_VBUSVLDEXT;
297 val &= ~PHY_CTL_SIDDQ;
298 writel(val, data->base + data->cfg->phyctl_offset);
299 }
300 } else {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530301 if (usb_phy->id == 0)
302 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
303 PHY_RES45_CAL_DATA,
304 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530305
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530306 /* Adjust PHY's magnitude and rate */
307 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
308 PHY_TX_MAGNITUDE | PHY_TX_RATE,
309 PHY_TX_AMPLITUDE_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530310
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530311 /* Disconnect threshold adjustment */
312 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
313 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
314 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530315
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000316#ifdef CONFIG_USB_MUSB_SUNXI
317 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
318 if (usb_phy->id != 0)
319 sun4i_usb_phy_passby(phy, true);
320
321 /* Route PHY0 to MUSB to allow USB gadget */
322 if (data->cfg->phy0_dual_route)
323 sun4i_usb_phy0_reroute(data, true);
324#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530325 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530326
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000327 /* Route PHY0 to HCI to allow USB host */
328 if (data->cfg->phy0_dual_route)
329 sun4i_usb_phy0_reroute(data, false);
330#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530331
332 return 0;
333}
334
335static int sun4i_usb_phy_exit(struct phy *phy)
336{
337 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
338 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530339 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530340
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530341 if (phy->id == 0) {
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300342 if (data->cfg->type == sun8i_a83t_phy ||
343 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530344 void __iomem *phyctl = data->base +
345 data->cfg->phyctl_offset;
346
347 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
348 }
349 }
350
351 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530352
Jagan Teki0dc33332018-08-06 12:16:39 +0530353 ret = clk_disable(&usb_phy->clocks);
354 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400355 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
356 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530357 return ret;
358 }
359
360 ret = reset_assert(&usb_phy->resets);
361 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400362 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
363 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530364 return ret;
365 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530366
367 return 0;
368}
369
370static int sun4i_usb_phy_xlate(struct phy *phy,
371 struct ofnode_phandle_args *args)
372{
373 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
374
Andre Przywara6c53de52023-06-12 00:32:35 +0100375 if (args->args_count != 1)
376 return -EINVAL;
377
378 if (args->args[0] >= data->cfg->num_phys)
Jagan Tekid3c38282018-05-07 13:03:26 +0530379 return -EINVAL;
380
Andre Przywarab2f0f312019-06-23 15:09:49 +0100381 if (data->cfg->missing_phys & BIT(args->args[0]))
382 return -ENODEV;
383
Jagan Tekid3c38282018-05-07 13:03:26 +0530384 if (args->args_count)
385 phy->id = args->args[0];
386 else
387 phy->id = 0;
388
389 debug("%s: phy_id = %ld\n", __func__, phy->id);
390 return 0;
391}
392
Jagan Teki21fc42d2018-05-07 13:03:27 +0530393int sun4i_usb_phy_vbus_detect(struct phy *phy)
394{
395 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
396 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500397 int err = 1, retries = 3;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530398
Andre Przywara3331d222022-06-07 23:36:18 +0100399 if (dm_gpio_is_valid(&usb_phy->gpio_vbus_det)) {
400 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500401 /*
402 * Vbus may have been provided by the board and just turned off
403 * some milliseconds ago on reset. What we're measuring then is
404 * a residual charge on Vbus. Sleep a bit and try again.
405 */
406 while (err > 0 && retries--) {
407 mdelay(100);
Andre Przywara3331d222022-06-07 23:36:18 +0100408 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500409 }
Samuel Hollandc70137c2021-09-12 09:22:42 -0500410 } else if (data->vbus_power_supply) {
411 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530412 }
413
414 return err;
415}
416
417int sun4i_usb_phy_id_detect(struct phy *phy)
418{
419 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
420 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
421
Andre Przywara3331d222022-06-07 23:36:18 +0100422 if (!dm_gpio_is_valid(&usb_phy->gpio_id_det))
423 return -1;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530424
Andre Przywara3331d222022-06-07 23:36:18 +0100425 return dm_gpio_get_value(&usb_phy->gpio_id_det);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530426}
427
Jagan Teki37671e12018-05-07 13:03:37 +0530428void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
429{
430 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
431}
432
Jagan Tekid3c38282018-05-07 13:03:26 +0530433static struct phy_ops sun4i_usb_phy_ops = {
434 .of_xlate = sun4i_usb_phy_xlate,
435 .init = sun4i_usb_phy_init,
436 .power_on = sun4i_usb_phy_power_on,
437 .power_off = sun4i_usb_phy_power_off,
438 .exit = sun4i_usb_phy_exit,
439};
440
441static int sun4i_usb_phy_probe(struct udevice *dev)
442{
Simon Glassfa20e932020-12-03 16:55:20 -0700443 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530444 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
445 int i, ret;
446
447 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
448 if (!data->cfg)
449 return -EINVAL;
450
451 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
452 if (IS_ERR(data->base))
453 return PTR_ERR(data->base);
454
Samuel Hollandc70137c2021-09-12 09:22:42 -0500455 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
456 &data->vbus_power_supply);
457
Jagan Tekid3c38282018-05-07 13:03:26 +0530458 data->usb_phy = plat;
459 for (i = 0; i < data->cfg->num_phys; i++) {
460 struct sun4i_usb_phy_plat *phy = &plat[i];
461 struct sun4i_usb_phy_info *info = &phy_info[i];
462 char name[16];
463
Andre Przywarab2f0f312019-06-23 15:09:49 +0100464 if (data->cfg->missing_phys & BIT(i))
465 continue;
466
Andre Przywara3331d222022-06-07 23:36:18 +0100467 ret = dm_gpio_lookup_name(info->gpio_vbus, &phy->gpio_vbus);
468 if (ret == 0) {
469 ret = dm_gpio_request(&phy->gpio_vbus, "usb_vbus");
Jagan Tekid3c38282018-05-07 13:03:26 +0530470 if (ret)
471 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100472 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus,
473 GPIOD_IS_OUT);
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_value(&phy->gpio_vbus, 0);
477 if (ret)
478 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530479 }
480
Andre Przywara3331d222022-06-07 23:36:18 +0100481 ret = dm_gpio_lookup_name(info->gpio_vbus_det,
482 &phy->gpio_vbus_det);
483 if (ret == 0) {
484 ret = dm_gpio_request(&phy->gpio_vbus_det,
485 "usb_vbus_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530486 if (ret)
487 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100488 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus_det,
489 GPIOD_IS_IN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530490 if (ret)
491 return ret;
492 }
493
Andre Przywara3331d222022-06-07 23:36:18 +0100494 ret = dm_gpio_lookup_name(info->gpio_id_det, &phy->gpio_id_det);
495 if (ret == 0) {
496 ret = dm_gpio_request(&phy->gpio_id_det, "usb_id_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530497 if (ret)
498 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100499 ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,
500 GPIOD_IS_IN | GPIOD_PULL_UP);
Jagan Tekid3c38282018-05-07 13:03:26 +0530501 if (ret)
502 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530503 }
504
Jagan Teki0dc33332018-08-06 12:16:39 +0530505 if (data->cfg->dedicated_clocks)
506 snprintf(name, sizeof(name), "usb%d_phy", i);
507 else
508 strlcpy(name, "usb_phy", sizeof(name));
509
510 ret = clk_get_by_name(dev, name, &phy->clocks);
511 if (ret) {
512 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
513 return ret;
514 }
515
516 snprintf(name, sizeof(name), "usb%d_reset", i);
517 ret = reset_get_by_name(dev, name, &phy->resets);
518 if (ret) {
519 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
520 return ret;
521 }
522
Jagan Tekid3c38282018-05-07 13:03:26 +0530523 if (i || data->cfg->phy0_dual_route) {
524 snprintf(name, sizeof(name), "pmu%d", i);
525 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
526 if (IS_ERR(phy->pmu))
527 return PTR_ERR(phy->pmu);
528 }
529
530 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530531 };
532
Jagan Tekid3c38282018-05-07 13:03:26 +0530533 debug("Allwinner Sun4I USB PHY driver loaded\n");
534 return 0;
535}
536
Jagan Teki5a3000f2018-05-07 13:03:31 +0530537static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
538 .num_phys = 3,
539 .type = sun4i_a10_phy,
540 .disc_thresh = 3,
541 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530542 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530543};
544
545static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
546 .num_phys = 2,
547 .type = sun4i_a10_phy,
548 .disc_thresh = 2,
549 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530550 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530551};
552
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530553static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
554 .num_phys = 3,
555 .type = sun6i_a31_phy,
556 .disc_thresh = 3,
557 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530558 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530559};
560
Jagan Teki5a3000f2018-05-07 13:03:31 +0530561static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
562 .num_phys = 3,
563 .type = sun4i_a10_phy,
564 .disc_thresh = 2,
565 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530566 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530567};
568
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530569static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
570 .num_phys = 2,
571 .type = sun4i_a10_phy,
572 .disc_thresh = 3,
573 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530574 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530575};
576
Jagan Teki0e574bb2018-05-07 13:03:33 +0530577static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
578 .num_phys = 2,
579 .type = sun8i_a33_phy,
580 .disc_thresh = 3,
581 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530582 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530583};
584
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530585static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
586 .num_phys = 3,
587 .type = sun8i_a83t_phy,
588 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530589 .dedicated_clocks = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530590};
591
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530592static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
593 .num_phys = 4,
594 .type = sun8i_h3_phy,
595 .disc_thresh = 3,
596 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530597 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500598 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530599 .phy0_dual_route = true,
600};
601
Andre Przywara47d49972020-01-01 23:44:48 +0000602static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
603 .num_phys = 3,
604 .type = sun8i_r40_phy,
605 .disc_thresh = 3,
606 .phyctl_offset = REG_PHYCTL_A33,
607 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500608 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Andre Przywara47d49972020-01-01 23:44:48 +0000609 .phy0_dual_route = true,
610};
611
Jagan Tekiac4bab42018-05-07 13:03:29 +0530612static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
613 .num_phys = 1,
614 .type = sun8i_v3s_phy,
615 .disc_thresh = 3,
616 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530617 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500618 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530619 .phy0_dual_route = true,
620};
621
Samuel Holland9f30cce2022-07-14 23:09:22 -0500622static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
623 .num_phys = 2,
624 .type = sun50i_h6_phy,
625 .phyctl_offset = REG_PHYCTL_A33,
626 .dedicated_clocks = true,
627 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
628 .phy0_dual_route = true,
629};
630
Jagan Tekid3c38282018-05-07 13:03:26 +0530631static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
632 .num_phys = 2,
633 .type = sun50i_a64_phy,
634 .disc_thresh = 3,
635 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530636 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500637 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekid3c38282018-05-07 13:03:26 +0530638 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100639};
640
641static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
642 .num_phys = 4,
643 .type = sun50i_h6_phy,
644 .disc_thresh = 3,
645 .phyctl_offset = REG_PHYCTL_A33,
646 .dedicated_clocks = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100647 .phy0_dual_route = true,
648 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530649};
650
651static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530652 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
653 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530654 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530655 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530656 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530657 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530658 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530659 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000660 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530661 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Samuel Holland9f30cce2022-07-14 23:09:22 -0500662 { .compatible = "allwinner,sun20i-d1-usb-phy", .data = (ulong)&sun20i_d1_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530663 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100664 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Jagan Tekid3c38282018-05-07 13:03:26 +0530665 { }
666};
667
668U_BOOT_DRIVER(sun4i_usb_phy) = {
669 .name = "sun4i_usb_phy",
670 .id = UCLASS_PHY,
671 .of_match = sun4i_usb_phy_ids,
672 .ops = &sun4i_usb_phy_ops,
673 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700674 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700675 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530676};