blob: 2e969ab91e35195300015e5556e8b04d4895d420 [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
Andre Przywara3331d222022-06-07 23:36:18 +0100227 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
228 dm_gpio_set_value(&usb_phy->gpio_vbus, 1);
Jagan Tekid3c38282018-05-07 13:03:26 +0530229
230 return 0;
231}
232
233static int sun4i_usb_phy_power_off(struct phy *phy)
234{
235 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
236 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
237
Andre Przywara3331d222022-06-07 23:36:18 +0100238 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
239 dm_gpio_set_value(&usb_phy->gpio_vbus, 0);
Jagan Tekid3c38282018-05-07 13:03:26 +0530240
241 return 0;
242}
243
244static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
245{
246 u32 regval;
247
248 regval = readl(data->base + REG_PHY_OTGCTL);
249 if (!id_det) {
250 /* Host mode. Route phy0 to EHCI/OHCI */
251 regval &= ~OTGCTL_ROUTE_MUSB;
252 } else {
253 /* Peripheral mode. Route phy0 to MUSB */
254 regval |= OTGCTL_ROUTE_MUSB;
255 }
256 writel(regval, data->base + REG_PHY_OTGCTL);
257}
258
259static int sun4i_usb_phy_init(struct phy *phy)
260{
261 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
262 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
263 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530264 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530265
Jagan Teki0dc33332018-08-06 12:16:39 +0530266 ret = clk_enable(&usb_phy->clocks);
267 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400268 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
269 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530270 return ret;
271 }
272
273 ret = reset_deassert(&usb_phy->resets);
274 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400275 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
276 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530277 return ret;
278 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530279
Andre Przywara8662e7e2022-07-14 23:09:21 -0500280 if (usb_phy->pmu && data->cfg->hci_phy_ctl_clear) {
281 val = readl(usb_phy->pmu + REG_HCI_PHY_CTL);
282 val &= ~data->cfg->hci_phy_ctl_clear;
283 writel(val, usb_phy->pmu + REG_HCI_PHY_CTL);
284 }
285
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300286 if (data->cfg->type == sun8i_a83t_phy ||
287 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530288 if (phy->id == 0) {
289 val = readl(data->base + data->cfg->phyctl_offset);
290 val |= PHY_CTL_VBUSVLDEXT;
291 val &= ~PHY_CTL_SIDDQ;
292 writel(val, data->base + data->cfg->phyctl_offset);
293 }
294 } else {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530295 if (usb_phy->id == 0)
296 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
297 PHY_RES45_CAL_DATA,
298 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530299
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530300 /* Adjust PHY's magnitude and rate */
301 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
302 PHY_TX_MAGNITUDE | PHY_TX_RATE,
303 PHY_TX_AMPLITUDE_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530304
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530305 /* Disconnect threshold adjustment */
306 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
307 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
308 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530309
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000310#ifdef CONFIG_USB_MUSB_SUNXI
311 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
312 if (usb_phy->id != 0)
313 sun4i_usb_phy_passby(phy, true);
314
315 /* Route PHY0 to MUSB to allow USB gadget */
316 if (data->cfg->phy0_dual_route)
317 sun4i_usb_phy0_reroute(data, true);
318#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530319 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530320
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000321 /* Route PHY0 to HCI to allow USB host */
322 if (data->cfg->phy0_dual_route)
323 sun4i_usb_phy0_reroute(data, false);
324#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530325
326 return 0;
327}
328
329static int sun4i_usb_phy_exit(struct phy *phy)
330{
331 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
332 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530333 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530334
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530335 if (phy->id == 0) {
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300336 if (data->cfg->type == sun8i_a83t_phy ||
337 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530338 void __iomem *phyctl = data->base +
339 data->cfg->phyctl_offset;
340
341 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
342 }
343 }
344
345 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530346
Jagan Teki0dc33332018-08-06 12:16:39 +0530347 ret = clk_disable(&usb_phy->clocks);
348 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400349 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
350 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530351 return ret;
352 }
353
354 ret = reset_assert(&usb_phy->resets);
355 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400356 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
357 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530358 return ret;
359 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530360
361 return 0;
362}
363
364static int sun4i_usb_phy_xlate(struct phy *phy,
365 struct ofnode_phandle_args *args)
366{
367 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
368
369 if (args->args_count >= data->cfg->num_phys)
370 return -EINVAL;
371
Andre Przywarab2f0f312019-06-23 15:09:49 +0100372 if (data->cfg->missing_phys & BIT(args->args[0]))
373 return -ENODEV;
374
Jagan Tekid3c38282018-05-07 13:03:26 +0530375 if (args->args_count)
376 phy->id = args->args[0];
377 else
378 phy->id = 0;
379
380 debug("%s: phy_id = %ld\n", __func__, phy->id);
381 return 0;
382}
383
Jagan Teki21fc42d2018-05-07 13:03:27 +0530384int sun4i_usb_phy_vbus_detect(struct phy *phy)
385{
386 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
387 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500388 int err = 1, retries = 3;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530389
Andre Przywara3331d222022-06-07 23:36:18 +0100390 if (dm_gpio_is_valid(&usb_phy->gpio_vbus_det)) {
391 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500392 /*
393 * Vbus may have been provided by the board and just turned off
394 * some milliseconds ago on reset. What we're measuring then is
395 * a residual charge on Vbus. Sleep a bit and try again.
396 */
397 while (err > 0 && retries--) {
398 mdelay(100);
Andre Przywara3331d222022-06-07 23:36:18 +0100399 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500400 }
Samuel Hollandc70137c2021-09-12 09:22:42 -0500401 } else if (data->vbus_power_supply) {
402 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530403 }
404
405 return err;
406}
407
408int sun4i_usb_phy_id_detect(struct phy *phy)
409{
410 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
411 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
412
Andre Przywara3331d222022-06-07 23:36:18 +0100413 if (!dm_gpio_is_valid(&usb_phy->gpio_id_det))
414 return -1;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530415
Andre Przywara3331d222022-06-07 23:36:18 +0100416 return dm_gpio_get_value(&usb_phy->gpio_id_det);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530417}
418
Jagan Teki37671e12018-05-07 13:03:37 +0530419void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
420{
421 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
422}
423
Jagan Tekid3c38282018-05-07 13:03:26 +0530424static struct phy_ops sun4i_usb_phy_ops = {
425 .of_xlate = sun4i_usb_phy_xlate,
426 .init = sun4i_usb_phy_init,
427 .power_on = sun4i_usb_phy_power_on,
428 .power_off = sun4i_usb_phy_power_off,
429 .exit = sun4i_usb_phy_exit,
430};
431
432static int sun4i_usb_phy_probe(struct udevice *dev)
433{
Simon Glassfa20e932020-12-03 16:55:20 -0700434 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530435 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
436 int i, ret;
437
438 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
439 if (!data->cfg)
440 return -EINVAL;
441
442 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
443 if (IS_ERR(data->base))
444 return PTR_ERR(data->base);
445
Samuel Hollandc70137c2021-09-12 09:22:42 -0500446 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
447 &data->vbus_power_supply);
448
Jagan Tekid3c38282018-05-07 13:03:26 +0530449 data->usb_phy = plat;
450 for (i = 0; i < data->cfg->num_phys; i++) {
451 struct sun4i_usb_phy_plat *phy = &plat[i];
452 struct sun4i_usb_phy_info *info = &phy_info[i];
453 char name[16];
454
Andre Przywarab2f0f312019-06-23 15:09:49 +0100455 if (data->cfg->missing_phys & BIT(i))
456 continue;
457
Andre Przywara3331d222022-06-07 23:36:18 +0100458 ret = dm_gpio_lookup_name(info->gpio_vbus, &phy->gpio_vbus);
459 if (ret == 0) {
460 ret = dm_gpio_request(&phy->gpio_vbus, "usb_vbus");
Jagan Tekid3c38282018-05-07 13:03:26 +0530461 if (ret)
462 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100463 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus,
464 GPIOD_IS_OUT);
Jagan Tekid3c38282018-05-07 13:03:26 +0530465 if (ret)
466 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100467 ret = dm_gpio_set_value(&phy->gpio_vbus, 0);
468 if (ret)
469 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530470 }
471
Andre Przywara3331d222022-06-07 23:36:18 +0100472 ret = dm_gpio_lookup_name(info->gpio_vbus_det,
473 &phy->gpio_vbus_det);
474 if (ret == 0) {
475 ret = dm_gpio_request(&phy->gpio_vbus_det,
476 "usb_vbus_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530477 if (ret)
478 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100479 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus_det,
480 GPIOD_IS_IN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530481 if (ret)
482 return ret;
483 }
484
Andre Przywara3331d222022-06-07 23:36:18 +0100485 ret = dm_gpio_lookup_name(info->gpio_id_det, &phy->gpio_id_det);
486 if (ret == 0) {
487 ret = dm_gpio_request(&phy->gpio_id_det, "usb_id_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530488 if (ret)
489 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100490 ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,
491 GPIOD_IS_IN | GPIOD_PULL_UP);
Jagan Tekid3c38282018-05-07 13:03:26 +0530492 if (ret)
493 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530494 }
495
Jagan Teki0dc33332018-08-06 12:16:39 +0530496 if (data->cfg->dedicated_clocks)
497 snprintf(name, sizeof(name), "usb%d_phy", i);
498 else
499 strlcpy(name, "usb_phy", sizeof(name));
500
501 ret = clk_get_by_name(dev, name, &phy->clocks);
502 if (ret) {
503 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
504 return ret;
505 }
506
507 snprintf(name, sizeof(name), "usb%d_reset", i);
508 ret = reset_get_by_name(dev, name, &phy->resets);
509 if (ret) {
510 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
511 return ret;
512 }
513
Jagan Tekid3c38282018-05-07 13:03:26 +0530514 if (i || data->cfg->phy0_dual_route) {
515 snprintf(name, sizeof(name), "pmu%d", i);
516 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
517 if (IS_ERR(phy->pmu))
518 return PTR_ERR(phy->pmu);
519 }
520
521 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530522 };
523
Jagan Tekid3c38282018-05-07 13:03:26 +0530524 debug("Allwinner Sun4I USB PHY driver loaded\n");
525 return 0;
526}
527
Jagan Teki5a3000f2018-05-07 13:03:31 +0530528static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
529 .num_phys = 3,
530 .type = sun4i_a10_phy,
531 .disc_thresh = 3,
532 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530533 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530534};
535
536static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
537 .num_phys = 2,
538 .type = sun4i_a10_phy,
539 .disc_thresh = 2,
540 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530541 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530542};
543
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530544static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
545 .num_phys = 3,
546 .type = sun6i_a31_phy,
547 .disc_thresh = 3,
548 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530549 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530550};
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};
559
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530560static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
561 .num_phys = 2,
562 .type = sun4i_a10_phy,
563 .disc_thresh = 3,
564 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530565 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530566};
567
Jagan Teki0e574bb2018-05-07 13:03:33 +0530568static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
569 .num_phys = 2,
570 .type = sun8i_a33_phy,
571 .disc_thresh = 3,
572 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530573 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530574};
575
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530576static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
577 .num_phys = 3,
578 .type = sun8i_a83t_phy,
579 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530580 .dedicated_clocks = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530581};
582
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530583static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
584 .num_phys = 4,
585 .type = sun8i_h3_phy,
586 .disc_thresh = 3,
587 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530588 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500589 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530590 .phy0_dual_route = true,
591};
592
Andre Przywara47d49972020-01-01 23:44:48 +0000593static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
594 .num_phys = 3,
595 .type = sun8i_r40_phy,
596 .disc_thresh = 3,
597 .phyctl_offset = REG_PHYCTL_A33,
598 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500599 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Andre Przywara47d49972020-01-01 23:44:48 +0000600 .phy0_dual_route = true,
601};
602
Jagan Tekiac4bab42018-05-07 13:03:29 +0530603static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
604 .num_phys = 1,
605 .type = sun8i_v3s_phy,
606 .disc_thresh = 3,
607 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530608 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500609 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530610 .phy0_dual_route = true,
611};
612
Samuel Holland9f30cce2022-07-14 23:09:22 -0500613static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
614 .num_phys = 2,
615 .type = sun50i_h6_phy,
616 .phyctl_offset = REG_PHYCTL_A33,
617 .dedicated_clocks = true,
618 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
619 .phy0_dual_route = true,
620};
621
Jagan Tekid3c38282018-05-07 13:03:26 +0530622static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
623 .num_phys = 2,
624 .type = sun50i_a64_phy,
625 .disc_thresh = 3,
626 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530627 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500628 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekid3c38282018-05-07 13:03:26 +0530629 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100630};
631
632static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
633 .num_phys = 4,
634 .type = sun50i_h6_phy,
635 .disc_thresh = 3,
636 .phyctl_offset = REG_PHYCTL_A33,
637 .dedicated_clocks = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100638 .phy0_dual_route = true,
639 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530640};
641
642static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530643 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
644 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530645 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530646 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530647 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530648 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530649 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530650 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000651 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530652 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Samuel Holland9f30cce2022-07-14 23:09:22 -0500653 { .compatible = "allwinner,sun20i-d1-usb-phy", .data = (ulong)&sun20i_d1_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530654 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100655 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Jagan Tekid3c38282018-05-07 13:03:26 +0530656 { }
657};
658
659U_BOOT_DRIVER(sun4i_usb_phy) = {
660 .name = "sun4i_usb_phy",
661 .id = UCLASS_PHY,
662 .of_match = sun4i_usb_phy_ids,
663 .ops = &sun4i_usb_phy_ops,
664 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700665 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700666 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530667};