blob: 6624e9134f43baebf0bd612267498bd95a174a25 [file] [log] [blame]
Jagan Teki67685942018-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 Teki089ffd02018-08-06 12:16:39 +053014#include <clk.h>
Jagan Teki67685942018-05-07 13:03:26 +053015#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Jagan Teki67685942018-05-07 13:03:26 +053017#include <dm/device.h>
18#include <generic-phy.h>
Jagan Teki129c45c2018-05-07 13:03:27 +053019#include <phy-sun4i-usb.h>
Jagan Teki089ffd02018-08-06 12:16:39 +053020#include <reset.h>
Jagan Teki67685942018-05-07 13:03:26 +053021#include <asm/gpio.h>
22#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060025#include <linux/delay.h>
Simon Glass61b29b82020-02-03 07:36:15 -070026#include <linux/err.h>
Samuel Holland6fa41cd2021-09-12 09:22:42 -050027#include <power/regulator.h>
Jagan Teki67685942018-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 Przywarab33ee492022-07-14 23:09:21 -050035
36#define REG_HCI_PHY_CTL 0x10
Jagan Teki67685942018-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 Tekiaa29b112018-05-07 13:03:37 +053046#define PHY_SQUELCH_DETECT 0x3c
Jagan Teki67685942018-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 Teki5f646bf2018-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 Przywarab33ee492022-07-14 23:09:21 -050067#define PHY_CTL_H3_SIDDQ BIT(1)
Jagan Teki5f646bf2018-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 Teki67685942018-05-07 13:03:26 +053074#define MAX_PHYS 4
75
Jagan Teki67685942018-05-07 13:03:26 +053076struct sun4i_usb_phy_cfg {
77 int num_phys;
Andre Przywarad7a7fed2023-06-12 00:32:38 +010078 int hsic_index;
Jagan Teki67685942018-05-07 13:03:26 +053079 u32 disc_thresh;
Andre Przywarab33ee492022-07-14 23:09:21 -050080 u32 hci_phy_ctl_clear;
Jagan Teki67685942018-05-07 13:03:26 +053081 u8 phyctl_offset;
Jagan Teki089ffd02018-08-06 12:16:39 +053082 bool dedicated_clocks;
Jagan Teki67685942018-05-07 13:03:26 +053083 bool phy0_dual_route;
Andre Przywarad7a7fed2023-06-12 00:32:38 +010084 bool siddq_in_base;
Andre Przywara730b4522023-06-12 00:32:39 +010085 bool needs_phy2_siddq;
Andre Przywara35fa6732019-06-23 15:09:49 +010086 int missing_phys;
Jagan Teki67685942018-05-07 13:03:26 +053087};
88
89struct sun4i_usb_phy_info {
90 const char *gpio_vbus;
91 const char *gpio_vbus_det;
92 const char *gpio_id_det;
Jagan Teki67685942018-05-07 13:03:26 +053093} phy_info[] = {
94 {
95 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
96 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
97 .gpio_id_det = CONFIG_USB0_ID_DET,
Jagan Teki67685942018-05-07 13:03:26 +053098 },
99 {
100 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
101 .gpio_vbus_det = NULL,
102 .gpio_id_det = NULL,
Jagan Teki67685942018-05-07 13:03:26 +0530103 },
104 {
105 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
106 .gpio_vbus_det = NULL,
107 .gpio_id_det = NULL,
Jagan Teki67685942018-05-07 13:03:26 +0530108 },
109 {
110 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
111 .gpio_vbus_det = NULL,
112 .gpio_id_det = NULL,
Jagan Teki67685942018-05-07 13:03:26 +0530113 },
114};
115
116struct sun4i_usb_phy_plat {
117 void __iomem *pmu;
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100118 struct gpio_desc gpio_vbus;
119 struct gpio_desc gpio_vbus_det;
120 struct gpio_desc gpio_id_det;
Jagan Teki089ffd02018-08-06 12:16:39 +0530121 struct clk clocks;
Andre Przywara730b4522023-06-12 00:32:39 +0100122 struct clk clk2;
Jagan Teki089ffd02018-08-06 12:16:39 +0530123 struct reset_ctl resets;
Jagan Teki67685942018-05-07 13:03:26 +0530124 int id;
125};
126
127struct sun4i_usb_phy_data {
128 void __iomem *base;
Jagan Teki67685942018-05-07 13:03:26 +0530129 const struct sun4i_usb_phy_cfg *cfg;
130 struct sun4i_usb_phy_plat *usb_phy;
Samuel Holland6fa41cd2021-09-12 09:22:42 -0500131 struct udevice *vbus_power_supply;
Jagan Teki67685942018-05-07 13:03:26 +0530132};
133
134static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
135
136static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
137{
138 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
139 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
140 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
141 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
142 int i;
143
144 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
145 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
146 writel(0, phyctl);
147 }
148
149 for (i = 0; i < len; i++) {
150 temp = readl(phyctl);
151
152 /* clear the address portion */
153 temp &= ~(0xff << 8);
154
155 /* set the address */
156 temp |= ((addr + i) << 8);
157 writel(temp, phyctl);
158
159 /* set the data bit and clear usbc bit*/
160 temp = readb(phyctl);
161 if (data & 0x1)
162 temp |= PHYCTL_DATA;
163 else
164 temp &= ~PHYCTL_DATA;
165 temp &= ~usbc_bit;
166 writeb(temp, phyctl);
167
168 /* pulse usbc_bit */
169 temp = readb(phyctl);
170 temp |= usbc_bit;
171 writeb(temp, phyctl);
172
173 temp = readb(phyctl);
174 temp &= ~usbc_bit;
175 writeb(temp, phyctl);
176
177 data >>= 1;
178 }
179}
180
Jagan Teki5f646bf2018-05-07 13:03:30 +0530181static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Teki67685942018-05-07 13:03:26 +0530182{
Jagan Teki5f646bf2018-05-07 13:03:30 +0530183 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
184 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki67685942018-05-07 13:03:26 +0530185 u32 bits, reg_value;
186
187 if (!usb_phy->pmu)
188 return;
189
190 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
191 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki5f646bf2018-05-07 13:03:30 +0530192
193 /* A83T USB2 is HSIC */
Andre Przywarad7a7fed2023-06-12 00:32:38 +0100194 if (data->cfg->hsic_index && usb_phy->id == data->cfg->hsic_index)
Jagan Teki5f646bf2018-05-07 13:03:30 +0530195 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
196 SUNXI_HSIC;
197
Jagan Teki67685942018-05-07 13:03:26 +0530198 reg_value = readl(usb_phy->pmu);
199
200 if (enable)
201 reg_value |= bits;
202 else
203 reg_value &= ~bits;
204
205 writel(reg_value, usb_phy->pmu);
206}
207
208static int sun4i_usb_phy_power_on(struct phy *phy)
209{
210 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
211 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
212
213 if (initial_usb_scan_delay) {
214 mdelay(initial_usb_scan_delay);
215 initial_usb_scan_delay = 0;
216 }
217
Samuel Hollanddf202d92022-07-14 22:34:53 -0500218 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
219 if (phy->id == 0 && sun4i_usb_phy_vbus_detect(phy)) {
220 dev_warn(phy->dev, "External vbus detected, not enabling our own vbus\n");
221 return 0;
222 }
223
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100224 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
225 dm_gpio_set_value(&usb_phy->gpio_vbus, 1);
Jagan Teki67685942018-05-07 13:03:26 +0530226
227 return 0;
228}
229
230static int sun4i_usb_phy_power_off(struct phy *phy)
231{
232 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
233 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
234
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100235 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
236 dm_gpio_set_value(&usb_phy->gpio_vbus, 0);
Jagan Teki67685942018-05-07 13:03:26 +0530237
238 return 0;
239}
240
241static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
242{
243 u32 regval;
244
245 regval = readl(data->base + REG_PHY_OTGCTL);
246 if (!id_det) {
247 /* Host mode. Route phy0 to EHCI/OHCI */
248 regval &= ~OTGCTL_ROUTE_MUSB;
249 } else {
250 /* Peripheral mode. Route phy0 to MUSB */
251 regval |= OTGCTL_ROUTE_MUSB;
252 }
253 writel(regval, data->base + REG_PHY_OTGCTL);
254}
255
256static int sun4i_usb_phy_init(struct phy *phy)
257{
258 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
259 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
260 u32 val;
Jagan Teki089ffd02018-08-06 12:16:39 +0530261 int ret;
Jagan Teki67685942018-05-07 13:03:26 +0530262
Jagan Teki089ffd02018-08-06 12:16:39 +0530263 ret = clk_enable(&usb_phy->clocks);
264 if (ret) {
Sean Anderson73345172020-09-15 10:45:04 -0400265 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
266 phy->id);
Jagan Teki089ffd02018-08-06 12:16:39 +0530267 return ret;
268 }
269
270 ret = reset_deassert(&usb_phy->resets);
271 if (ret) {
Sean Anderson73345172020-09-15 10:45:04 -0400272 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
273 phy->id);
Jagan Teki089ffd02018-08-06 12:16:39 +0530274 return ret;
275 }
Jagan Teki67685942018-05-07 13:03:26 +0530276
Andre Przywara730b4522023-06-12 00:32:39 +0100277 /* Some PHYs on some SoCs (the H616) need the help of PHY2 to work. */
278 if (data->cfg->needs_phy2_siddq && phy->id != 2) {
279 struct sun4i_usb_phy_plat *phy2 = &data->usb_phy[2];
280
281 ret = clk_enable(&phy2->clocks);
282 if (ret) {
283 dev_err(phy->dev, "failed to enable aux clock\n");
284 return ret;
285 }
286
287 ret = reset_deassert(&phy2->resets);
288 if (ret) {
289 dev_err(phy->dev, "failed to deassert aux reset\n");
290 return ret;
291 }
292
293 /*
294 * This extra clock is just needed to access the
295 * REG_HCI_PHY_CTL PMU register for PHY2.
296 */
297 ret = clk_enable(&phy2->clk2);
298 if (ret) {
299 dev_err(phy->dev, "failed to enable PHY2 clock\n");
300 return ret;
301 }
302
303 if (phy2->pmu && data->cfg->hci_phy_ctl_clear) {
304 val = readl(phy2->pmu + REG_HCI_PHY_CTL);
305 val &= ~data->cfg->hci_phy_ctl_clear;
306 writel(val, phy2->pmu + REG_HCI_PHY_CTL);
307 }
308
309 clk_disable(&phy2->clk2);
310 }
311
Andre Przywarab33ee492022-07-14 23:09:21 -0500312 if (usb_phy->pmu && data->cfg->hci_phy_ctl_clear) {
313 val = readl(usb_phy->pmu + REG_HCI_PHY_CTL);
314 val &= ~data->cfg->hci_phy_ctl_clear;
315 writel(val, usb_phy->pmu + REG_HCI_PHY_CTL);
316 }
317
Andre Przywarad7a7fed2023-06-12 00:32:38 +0100318 if (data->cfg->siddq_in_base) {
Jagan Teki5f646bf2018-05-07 13:03:30 +0530319 if (phy->id == 0) {
320 val = readl(data->base + data->cfg->phyctl_offset);
321 val |= PHY_CTL_VBUSVLDEXT;
322 val &= ~PHY_CTL_SIDDQ;
323 writel(val, data->base + data->cfg->phyctl_offset);
324 }
325 } else {
Jagan Teki5f646bf2018-05-07 13:03:30 +0530326 if (usb_phy->id == 0)
327 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
328 PHY_RES45_CAL_DATA,
329 PHY_RES45_CAL_LEN);
330
331 /* Adjust PHY's magnitude and rate */
332 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
333 PHY_TX_MAGNITUDE | PHY_TX_RATE,
334 PHY_TX_AMPLITUDE_LEN);
335
336 /* Disconnect threshold adjustment */
337 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
338 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
Jagan Teki67685942018-05-07 13:03:26 +0530339 }
340
Paul Kocialkowski0d5824c2019-03-14 10:38:00 +0000341#ifdef CONFIG_USB_MUSB_SUNXI
342 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
343 if (usb_phy->id != 0)
344 sun4i_usb_phy_passby(phy, true);
345
346 /* Route PHY0 to MUSB to allow USB gadget */
347 if (data->cfg->phy0_dual_route)
348 sun4i_usb_phy0_reroute(data, true);
349#else
Jagan Teki0bfcb472018-07-20 12:34:20 +0530350 sun4i_usb_phy_passby(phy, true);
Jagan Teki67685942018-05-07 13:03:26 +0530351
Paul Kocialkowski0d5824c2019-03-14 10:38:00 +0000352 /* Route PHY0 to HCI to allow USB host */
353 if (data->cfg->phy0_dual_route)
354 sun4i_usb_phy0_reroute(data, false);
355#endif
Jagan Teki67685942018-05-07 13:03:26 +0530356
357 return 0;
358}
359
360static int sun4i_usb_phy_exit(struct phy *phy)
361{
362 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
363 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki089ffd02018-08-06 12:16:39 +0530364 int ret;
Jagan Teki67685942018-05-07 13:03:26 +0530365
Jagan Teki5f646bf2018-05-07 13:03:30 +0530366 if (phy->id == 0) {
Andre Przywarad7a7fed2023-06-12 00:32:38 +0100367 if (data->cfg->siddq_in_base) {
Jagan Teki5f646bf2018-05-07 13:03:30 +0530368 void __iomem *phyctl = data->base +
369 data->cfg->phyctl_offset;
370
371 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
372 }
373 }
374
375 sun4i_usb_phy_passby(phy, false);
Jagan Teki67685942018-05-07 13:03:26 +0530376
Jagan Teki089ffd02018-08-06 12:16:39 +0530377 ret = clk_disable(&usb_phy->clocks);
378 if (ret) {
Sean Anderson73345172020-09-15 10:45:04 -0400379 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
380 phy->id);
Jagan Teki089ffd02018-08-06 12:16:39 +0530381 return ret;
382 }
383
384 ret = reset_assert(&usb_phy->resets);
385 if (ret) {
Sean Anderson73345172020-09-15 10:45:04 -0400386 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
387 phy->id);
Jagan Teki089ffd02018-08-06 12:16:39 +0530388 return ret;
389 }
Jagan Teki67685942018-05-07 13:03:26 +0530390
391 return 0;
392}
393
394static int sun4i_usb_phy_xlate(struct phy *phy,
395 struct ofnode_phandle_args *args)
396{
397 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
398
Andre Przywara999bc5e2023-06-12 00:32:35 +0100399 if (args->args_count != 1)
400 return -EINVAL;
401
402 if (args->args[0] >= data->cfg->num_phys)
Jagan Teki67685942018-05-07 13:03:26 +0530403 return -EINVAL;
404
Andre Przywara35fa6732019-06-23 15:09:49 +0100405 if (data->cfg->missing_phys & BIT(args->args[0]))
406 return -ENODEV;
407
Jagan Teki67685942018-05-07 13:03:26 +0530408 if (args->args_count)
409 phy->id = args->args[0];
410 else
411 phy->id = 0;
412
413 debug("%s: phy_id = %ld\n", __func__, phy->id);
414 return 0;
415}
416
Jagan Teki129c45c2018-05-07 13:03:27 +0530417int sun4i_usb_phy_vbus_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];
Samuel Holland1da7b882021-09-12 09:22:41 -0500421 int err = 1, retries = 3;
Jagan Teki129c45c2018-05-07 13:03:27 +0530422
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100423 if (dm_gpio_is_valid(&usb_phy->gpio_vbus_det)) {
424 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Holland1da7b882021-09-12 09:22:41 -0500425 /*
426 * Vbus may have been provided by the board and just turned off
427 * some milliseconds ago on reset. What we're measuring then is
428 * a residual charge on Vbus. Sleep a bit and try again.
429 */
430 while (err > 0 && retries--) {
431 mdelay(100);
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100432 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Holland1da7b882021-09-12 09:22:41 -0500433 }
Samuel Holland6fa41cd2021-09-12 09:22:42 -0500434 } else if (data->vbus_power_supply) {
435 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki129c45c2018-05-07 13:03:27 +0530436 }
437
438 return err;
439}
440
441int sun4i_usb_phy_id_detect(struct phy *phy)
442{
443 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
444 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
445
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100446 if (!dm_gpio_is_valid(&usb_phy->gpio_id_det))
447 return -1;
Jagan Teki129c45c2018-05-07 13:03:27 +0530448
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100449 return dm_gpio_get_value(&usb_phy->gpio_id_det);
Jagan Teki129c45c2018-05-07 13:03:27 +0530450}
451
Jagan Tekiaa29b112018-05-07 13:03:37 +0530452void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
453{
454 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
455}
456
Jagan Teki67685942018-05-07 13:03:26 +0530457static struct phy_ops sun4i_usb_phy_ops = {
458 .of_xlate = sun4i_usb_phy_xlate,
459 .init = sun4i_usb_phy_init,
460 .power_on = sun4i_usb_phy_power_on,
461 .power_off = sun4i_usb_phy_power_off,
462 .exit = sun4i_usb_phy_exit,
463};
464
465static int sun4i_usb_phy_probe(struct udevice *dev)
466{
Simon Glassc69cda22020-12-03 16:55:20 -0700467 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Teki67685942018-05-07 13:03:26 +0530468 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
469 int i, ret;
470
471 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
472 if (!data->cfg)
473 return -EINVAL;
474
Matthias Schiffer5fecea12023-09-27 15:33:34 +0200475 data->base = (void __iomem *)dev_read_addr_name_ptr(dev, "phy_ctrl");
476 if (!data->base)
477 return -EINVAL;
Jagan Teki67685942018-05-07 13:03:26 +0530478
Samuel Holland6fa41cd2021-09-12 09:22:42 -0500479 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
480 &data->vbus_power_supply);
481
Jagan Teki67685942018-05-07 13:03:26 +0530482 data->usb_phy = plat;
483 for (i = 0; i < data->cfg->num_phys; i++) {
484 struct sun4i_usb_phy_plat *phy = &plat[i];
485 struct sun4i_usb_phy_info *info = &phy_info[i];
486 char name[16];
487
Andre Przywara35fa6732019-06-23 15:09:49 +0100488 if (data->cfg->missing_phys & BIT(i))
489 continue;
490
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100491 ret = dm_gpio_lookup_name(info->gpio_vbus, &phy->gpio_vbus);
492 if (ret == 0) {
493 ret = dm_gpio_request(&phy->gpio_vbus, "usb_vbus");
Jagan Teki67685942018-05-07 13:03:26 +0530494 if (ret)
495 return ret;
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100496 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus,
497 GPIOD_IS_OUT);
498 if (ret)
499 return ret;
500 ret = dm_gpio_set_value(&phy->gpio_vbus, 0);
Jagan Teki67685942018-05-07 13:03:26 +0530501 if (ret)
502 return ret;
503 }
504
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100505 ret = dm_gpio_lookup_name(info->gpio_vbus_det,
506 &phy->gpio_vbus_det);
507 if (ret == 0) {
508 ret = dm_gpio_request(&phy->gpio_vbus_det,
509 "usb_vbus_det");
Jagan Teki67685942018-05-07 13:03:26 +0530510 if (ret)
511 return ret;
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100512 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus_det,
513 GPIOD_IS_IN);
Jagan Teki67685942018-05-07 13:03:26 +0530514 if (ret)
515 return ret;
516 }
517
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100518 ret = dm_gpio_lookup_name(info->gpio_id_det, &phy->gpio_id_det);
519 if (ret == 0) {
520 ret = dm_gpio_request(&phy->gpio_id_det, "usb_id_det");
Jagan Teki67685942018-05-07 13:03:26 +0530521 if (ret)
522 return ret;
Andre Przywara3c0b9ad2022-06-07 23:36:18 +0100523 ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,
524 GPIOD_IS_IN | GPIOD_PULL_UP);
Jagan Teki67685942018-05-07 13:03:26 +0530525 if (ret)
526 return ret;
Jagan Teki67685942018-05-07 13:03:26 +0530527 }
528
Jagan Teki089ffd02018-08-06 12:16:39 +0530529 if (data->cfg->dedicated_clocks)
530 snprintf(name, sizeof(name), "usb%d_phy", i);
531 else
532 strlcpy(name, "usb_phy", sizeof(name));
533
534 ret = clk_get_by_name(dev, name, &phy->clocks);
535 if (ret) {
536 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
537 return ret;
538 }
539
Andre Przywara730b4522023-06-12 00:32:39 +0100540 /* Helper clock from PHY2 for the H616 PHY quirk */
541 snprintf(name, sizeof(name), "pmu%d_clk", i);
542 ret = clk_get_by_name_optional(dev, name, &phy->clk2);
543 if (ret) {
544 dev_err(dev, "failed to get pmu%d_clk clock phandle\n",
545 i);
546 return ret;
547 }
548
Jagan Teki089ffd02018-08-06 12:16:39 +0530549 snprintf(name, sizeof(name), "usb%d_reset", i);
550 ret = reset_get_by_name(dev, name, &phy->resets);
551 if (ret) {
552 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
553 return ret;
554 }
555
Jagan Teki67685942018-05-07 13:03:26 +0530556 if (i || data->cfg->phy0_dual_route) {
557 snprintf(name, sizeof(name), "pmu%d", i);
Matthias Schiffer5fecea12023-09-27 15:33:34 +0200558 phy->pmu = (void __iomem *)dev_read_addr_name_ptr(dev, name);
559 if (!phy->pmu)
560 return -EINVAL;
Jagan Teki67685942018-05-07 13:03:26 +0530561 }
562
563 phy->id = i;
Jagan Teki67685942018-05-07 13:03:26 +0530564 };
565
Jagan Teki67685942018-05-07 13:03:26 +0530566 debug("Allwinner Sun4I USB PHY driver loaded\n");
567 return 0;
568}
569
Jagan Teki7f90b552018-05-07 13:03:31 +0530570static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
571 .num_phys = 3,
Jagan Teki7f90b552018-05-07 13:03:31 +0530572 .disc_thresh = 3,
573 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki089ffd02018-08-06 12:16:39 +0530574 .dedicated_clocks = false,
Jagan Teki7f90b552018-05-07 13:03:31 +0530575};
576
577static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
578 .num_phys = 2,
Jagan Teki7f90b552018-05-07 13:03:31 +0530579 .disc_thresh = 2,
580 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki089ffd02018-08-06 12:16:39 +0530581 .dedicated_clocks = false,
Jagan Teki7f90b552018-05-07 13:03:31 +0530582};
583
Jagan Tekibf986d12018-05-07 13:03:32 +0530584static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
585 .num_phys = 3,
Jagan Tekibf986d12018-05-07 13:03:32 +0530586 .disc_thresh = 3,
587 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki089ffd02018-08-06 12:16:39 +0530588 .dedicated_clocks = true,
Jagan Tekibf986d12018-05-07 13:03:32 +0530589};
590
Jagan Teki7f90b552018-05-07 13:03:31 +0530591static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
592 .num_phys = 3,
Jagan Teki7f90b552018-05-07 13:03:31 +0530593 .disc_thresh = 2,
594 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki089ffd02018-08-06 12:16:39 +0530595 .dedicated_clocks = false,
Jagan Teki7f90b552018-05-07 13:03:31 +0530596};
597
Jagan Teki194ccb92018-05-07 13:03:34 +0530598static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
599 .num_phys = 2,
Jagan Teki194ccb92018-05-07 13:03:34 +0530600 .disc_thresh = 3,
601 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki089ffd02018-08-06 12:16:39 +0530602 .dedicated_clocks = true,
Jagan Teki194ccb92018-05-07 13:03:34 +0530603};
604
Jagan Teki61bf0ed2018-05-07 13:03:33 +0530605static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
606 .num_phys = 2,
Jagan Teki61bf0ed2018-05-07 13:03:33 +0530607 .disc_thresh = 3,
608 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki089ffd02018-08-06 12:16:39 +0530609 .dedicated_clocks = true,
Jagan Teki61bf0ed2018-05-07 13:03:33 +0530610};
611
Jagan Teki5f646bf2018-05-07 13:03:30 +0530612static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
613 .num_phys = 3,
Andre Przywarad7a7fed2023-06-12 00:32:38 +0100614 .hsic_index = 2,
Jagan Teki5f646bf2018-05-07 13:03:30 +0530615 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki089ffd02018-08-06 12:16:39 +0530616 .dedicated_clocks = true,
Andre Przywarad7a7fed2023-06-12 00:32:38 +0100617 .siddq_in_base = true,
Jagan Teki5f646bf2018-05-07 13:03:30 +0530618};
619
Jagan Teki43519c42018-05-07 13:03:28 +0530620static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
621 .num_phys = 4,
Jagan Teki43519c42018-05-07 13:03:28 +0530622 .disc_thresh = 3,
623 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki089ffd02018-08-06 12:16:39 +0530624 .dedicated_clocks = true,
Andre Przywarab33ee492022-07-14 23:09:21 -0500625 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Teki43519c42018-05-07 13:03:28 +0530626 .phy0_dual_route = true,
627};
628
Andre Przywaraa2f729f2020-01-01 23:44:48 +0000629static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
630 .num_phys = 3,
Andre Przywaraa2f729f2020-01-01 23:44:48 +0000631 .disc_thresh = 3,
632 .phyctl_offset = REG_PHYCTL_A33,
633 .dedicated_clocks = true,
Andre Przywarab33ee492022-07-14 23:09:21 -0500634 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Andre Przywaraa2f729f2020-01-01 23:44:48 +0000635 .phy0_dual_route = true,
636};
637
Jagan Tekibafe5e32018-05-07 13:03:29 +0530638static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
639 .num_phys = 1,
Jagan Tekibafe5e32018-05-07 13:03:29 +0530640 .disc_thresh = 3,
641 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki089ffd02018-08-06 12:16:39 +0530642 .dedicated_clocks = true,
Andre Przywarab33ee492022-07-14 23:09:21 -0500643 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekibafe5e32018-05-07 13:03:29 +0530644 .phy0_dual_route = true,
645};
646
Samuel Holland25ba5be2022-07-14 23:09:22 -0500647static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
648 .num_phys = 2,
Samuel Holland25ba5be2022-07-14 23:09:22 -0500649 .phyctl_offset = REG_PHYCTL_A33,
650 .dedicated_clocks = true,
651 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
652 .phy0_dual_route = true,
Andre Przywarad7a7fed2023-06-12 00:32:38 +0100653 .siddq_in_base = true,
Samuel Holland25ba5be2022-07-14 23:09:22 -0500654};
655
Jagan Teki67685942018-05-07 13:03:26 +0530656static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
657 .num_phys = 2,
Jagan Teki67685942018-05-07 13:03:26 +0530658 .disc_thresh = 3,
659 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki089ffd02018-08-06 12:16:39 +0530660 .dedicated_clocks = true,
Andre Przywarab33ee492022-07-14 23:09:21 -0500661 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Teki67685942018-05-07 13:03:26 +0530662 .phy0_dual_route = true,
663};
664
Andre Przywara35fa6732019-06-23 15:09:49 +0100665static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
666 .num_phys = 4,
Andre Przywara35fa6732019-06-23 15:09:49 +0100667 .disc_thresh = 3,
668 .phyctl_offset = REG_PHYCTL_A33,
669 .dedicated_clocks = true,
Andre Przywara35fa6732019-06-23 15:09:49 +0100670 .phy0_dual_route = true,
Andre Przywarad7a7fed2023-06-12 00:32:38 +0100671 .siddq_in_base = true,
Andre Przywara35fa6732019-06-23 15:09:49 +0100672 .missing_phys = BIT(1) | BIT(2),
673};
674
Andre Przywara830b3a82023-06-12 00:32:40 +0100675static const struct sun4i_usb_phy_cfg sun50i_h616_cfg = {
676 .num_phys = 4,
677 .disc_thresh = 3,
678 .phyctl_offset = REG_PHYCTL_A33,
679 .dedicated_clocks = true,
680 .phy0_dual_route = true,
681 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
682 .needs_phy2_siddq = true,
683 .siddq_in_base = true,
684};
685
Andre Przywara003fbb22023-06-12 00:32:36 +0100686static const struct sun4i_usb_phy_cfg suniv_f1c100s_cfg = {
687 .num_phys = 1,
Andre Przywara003fbb22023-06-12 00:32:36 +0100688 .disc_thresh = 3,
689 .phyctl_offset = REG_PHYCTL_A10,
690 .dedicated_clocks = true,
691};
692
Jagan Teki67685942018-05-07 13:03:26 +0530693static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki7f90b552018-05-07 13:03:31 +0530694 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
695 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Tekibf986d12018-05-07 13:03:32 +0530696 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki7f90b552018-05-07 13:03:31 +0530697 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki194ccb92018-05-07 13:03:34 +0530698 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki61bf0ed2018-05-07 13:03:33 +0530699 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki5f646bf2018-05-07 13:03:30 +0530700 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Teki43519c42018-05-07 13:03:28 +0530701 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywaraa2f729f2020-01-01 23:44:48 +0000702 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekibafe5e32018-05-07 13:03:29 +0530703 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Samuel Holland25ba5be2022-07-14 23:09:22 -0500704 { .compatible = "allwinner,sun20i-d1-usb-phy", .data = (ulong)&sun20i_d1_cfg },
Jagan Teki67685942018-05-07 13:03:26 +0530705 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywara35fa6732019-06-23 15:09:49 +0100706 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Andre Przywara830b3a82023-06-12 00:32:40 +0100707 { .compatible = "allwinner,sun50i-h616-usb-phy", .data = (ulong)&sun50i_h616_cfg },
Andre Przywara003fbb22023-06-12 00:32:36 +0100708 { .compatible = "allwinner,suniv-f1c100s-usb-phy", .data = (ulong)&suniv_f1c100s_cfg },
Jagan Teki67685942018-05-07 13:03:26 +0530709 { }
710};
711
712U_BOOT_DRIVER(sun4i_usb_phy) = {
713 .name = "sun4i_usb_phy",
714 .id = UCLASS_PHY,
715 .of_match = sun4i_usb_phy_ids,
716 .ops = &sun4i_usb_phy_ops,
717 .probe = sun4i_usb_phy_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700718 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass41575d82020-12-03 16:55:17 -0700719 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Teki67685942018-05-07 13:03:26 +0530720};