blob: b9306c9a8279e0d1b7fae7766cd00384f877aa74 [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
Jagan Teki0dc33332018-08-06 12:16:39 +053013#include <clk.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053016#include <dm/device.h>
17#include <generic-phy.h>
Jagan Teki21fc42d2018-05-07 13:03:27 +053018#include <phy-sun4i-usb.h>
Jagan Teki0dc33332018-08-06 12:16:39 +053019#include <reset.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053020#include <asm/gpio.h>
21#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060023#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <linux/err.h>
Samuel Hollandc70137c2021-09-12 09:22:42 -050026#include <power/regulator.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053027
28#define REG_ISCR 0x00
29#define REG_PHYCTL_A10 0x04
30#define REG_PHYBIST 0x08
31#define REG_PHYTUNE 0x0c
32#define REG_PHYCTL_A33 0x10
33#define REG_PHY_OTGCTL 0x20
Andre Przywara8662e7e2022-07-14 23:09:21 -050034
35#define REG_HCI_PHY_CTL 0x10
Jagan Tekid3c38282018-05-07 13:03:26 +053036
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)
Andre Przywara8662e7e2022-07-14 23:09:21 -050066#define PHY_CTL_H3_SIDDQ BIT(1)
Jagan Teki05a7b9f2018-05-07 13:03:30 +053067
68/* A83T specific control bits for PHY2 HSIC */
69#define SUNXI_EHCI_HS_FORCE BIT(20)
70#define SUNXI_HSIC_CONNECT_INT BIT(16)
71#define SUNXI_HSIC BIT(1)
72
Jagan Tekid3c38282018-05-07 13:03:26 +053073#define MAX_PHYS 4
74
Jagan Tekid3c38282018-05-07 13:03:26 +053075struct sun4i_usb_phy_cfg {
76 int num_phys;
Andre Przywarab06b90f2023-06-12 00:32:38 +010077 int hsic_index;
Jagan Tekid3c38282018-05-07 13:03:26 +053078 u32 disc_thresh;
Andre Przywara8662e7e2022-07-14 23:09:21 -050079 u32 hci_phy_ctl_clear;
Jagan Tekid3c38282018-05-07 13:03:26 +053080 u8 phyctl_offset;
Jagan Teki0dc33332018-08-06 12:16:39 +053081 bool dedicated_clocks;
Jagan Tekid3c38282018-05-07 13:03:26 +053082 bool phy0_dual_route;
Andre Przywarab06b90f2023-06-12 00:32:38 +010083 bool siddq_in_base;
Andre Przywara720f4e42023-06-12 00:32:39 +010084 bool needs_phy2_siddq;
Andre Przywarab2f0f312019-06-23 15:09:49 +010085 int missing_phys;
Jagan Tekid3c38282018-05-07 13:03:26 +053086};
87
88struct sun4i_usb_phy_info {
89 const char *gpio_vbus;
90 const char *gpio_vbus_det;
91 const char *gpio_id_det;
Jagan Tekid3c38282018-05-07 13:03:26 +053092} phy_info[] = {
93 {
94 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
95 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
96 .gpio_id_det = CONFIG_USB0_ID_DET,
Jagan Tekid3c38282018-05-07 13:03:26 +053097 },
98 {
99 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
100 .gpio_vbus_det = NULL,
101 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530102 },
103 {
104 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
105 .gpio_vbus_det = NULL,
106 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530107 },
108 {
109 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
110 .gpio_vbus_det = NULL,
111 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530112 },
113};
114
115struct sun4i_usb_phy_plat {
116 void __iomem *pmu;
Andre Przywara3331d222022-06-07 23:36:18 +0100117 struct gpio_desc gpio_vbus;
118 struct gpio_desc gpio_vbus_det;
119 struct gpio_desc gpio_id_det;
Jagan Teki0dc33332018-08-06 12:16:39 +0530120 struct clk clocks;
Andre Przywara720f4e42023-06-12 00:32:39 +0100121 struct clk clk2;
Jagan Teki0dc33332018-08-06 12:16:39 +0530122 struct reset_ctl resets;
Jagan Tekid3c38282018-05-07 13:03:26 +0530123 int id;
124};
125
126struct sun4i_usb_phy_data {
127 void __iomem *base;
Jagan Tekid3c38282018-05-07 13:03:26 +0530128 const struct sun4i_usb_phy_cfg *cfg;
129 struct sun4i_usb_phy_plat *usb_phy;
Samuel Hollandc70137c2021-09-12 09:22:42 -0500130 struct udevice *vbus_power_supply;
Jagan Tekid3c38282018-05-07 13:03:26 +0530131};
132
133static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
134
135static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
136{
137 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
138 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
139 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
140 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
141 int i;
142
143 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
144 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
145 writel(0, phyctl);
146 }
147
148 for (i = 0; i < len; i++) {
149 temp = readl(phyctl);
150
151 /* clear the address portion */
152 temp &= ~(0xff << 8);
153
154 /* set the address */
155 temp |= ((addr + i) << 8);
156 writel(temp, phyctl);
157
158 /* set the data bit and clear usbc bit*/
159 temp = readb(phyctl);
160 if (data & 0x1)
161 temp |= PHYCTL_DATA;
162 else
163 temp &= ~PHYCTL_DATA;
164 temp &= ~usbc_bit;
165 writeb(temp, phyctl);
166
167 /* pulse usbc_bit */
168 temp = readb(phyctl);
169 temp |= usbc_bit;
170 writeb(temp, phyctl);
171
172 temp = readb(phyctl);
173 temp &= ~usbc_bit;
174 writeb(temp, phyctl);
175
176 data >>= 1;
177 }
178}
179
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530180static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Tekid3c38282018-05-07 13:03:26 +0530181{
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530182 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
183 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Tekid3c38282018-05-07 13:03:26 +0530184 u32 bits, reg_value;
185
186 if (!usb_phy->pmu)
187 return;
188
189 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
190 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530191
192 /* A83T USB2 is HSIC */
Andre Przywarab06b90f2023-06-12 00:32:38 +0100193 if (data->cfg->hsic_index && usb_phy->id == data->cfg->hsic_index)
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530194 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
195 SUNXI_HSIC;
196
Jagan Tekid3c38282018-05-07 13:03:26 +0530197 reg_value = readl(usb_phy->pmu);
198
199 if (enable)
200 reg_value |= bits;
201 else
202 reg_value &= ~bits;
203
204 writel(reg_value, usb_phy->pmu);
205}
206
207static int sun4i_usb_phy_power_on(struct phy *phy)
208{
209 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
210 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
211
212 if (initial_usb_scan_delay) {
213 mdelay(initial_usb_scan_delay);
214 initial_usb_scan_delay = 0;
215 }
216
Samuel Hollandad991492022-07-14 22:34:53 -0500217 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
218 if (phy->id == 0 && sun4i_usb_phy_vbus_detect(phy)) {
219 dev_warn(phy->dev, "External vbus detected, not enabling our own vbus\n");
220 return 0;
221 }
222
Andre Przywara3331d222022-06-07 23:36:18 +0100223 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
224 dm_gpio_set_value(&usb_phy->gpio_vbus, 1);
Jagan Tekid3c38282018-05-07 13:03:26 +0530225
226 return 0;
227}
228
229static int sun4i_usb_phy_power_off(struct phy *phy)
230{
231 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
232 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
233
Andre Przywara3331d222022-06-07 23:36:18 +0100234 if (dm_gpio_is_valid(&usb_phy->gpio_vbus))
235 dm_gpio_set_value(&usb_phy->gpio_vbus, 0);
Jagan Tekid3c38282018-05-07 13:03:26 +0530236
237 return 0;
238}
239
240static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
241{
242 u32 regval;
243
244 regval = readl(data->base + REG_PHY_OTGCTL);
245 if (!id_det) {
246 /* Host mode. Route phy0 to EHCI/OHCI */
247 regval &= ~OTGCTL_ROUTE_MUSB;
248 } else {
249 /* Peripheral mode. Route phy0 to MUSB */
250 regval |= OTGCTL_ROUTE_MUSB;
251 }
252 writel(regval, data->base + REG_PHY_OTGCTL);
253}
254
255static int sun4i_usb_phy_init(struct phy *phy)
256{
257 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
258 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
259 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530260 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530261
Jagan Teki0dc33332018-08-06 12:16:39 +0530262 ret = clk_enable(&usb_phy->clocks);
263 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400264 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
265 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530266 return ret;
267 }
268
269 ret = reset_deassert(&usb_phy->resets);
270 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400271 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
272 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530273 return ret;
274 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530275
Andre Przywara720f4e42023-06-12 00:32:39 +0100276 /* Some PHYs on some SoCs (the H616) need the help of PHY2 to work. */
277 if (data->cfg->needs_phy2_siddq && phy->id != 2) {
278 struct sun4i_usb_phy_plat *phy2 = &data->usb_phy[2];
279
280 ret = clk_enable(&phy2->clocks);
281 if (ret) {
282 dev_err(phy->dev, "failed to enable aux clock\n");
283 return ret;
284 }
285
286 ret = reset_deassert(&phy2->resets);
287 if (ret) {
288 dev_err(phy->dev, "failed to deassert aux reset\n");
289 return ret;
290 }
291
292 /*
293 * This extra clock is just needed to access the
294 * REG_HCI_PHY_CTL PMU register for PHY2.
295 */
296 ret = clk_enable(&phy2->clk2);
297 if (ret) {
298 dev_err(phy->dev, "failed to enable PHY2 clock\n");
299 return ret;
300 }
301
302 if (phy2->pmu && data->cfg->hci_phy_ctl_clear) {
303 val = readl(phy2->pmu + REG_HCI_PHY_CTL);
304 val &= ~data->cfg->hci_phy_ctl_clear;
305 writel(val, phy2->pmu + REG_HCI_PHY_CTL);
306 }
307
308 clk_disable(&phy2->clk2);
309 }
310
Andre Przywara8662e7e2022-07-14 23:09:21 -0500311 if (usb_phy->pmu && data->cfg->hci_phy_ctl_clear) {
312 val = readl(usb_phy->pmu + REG_HCI_PHY_CTL);
313 val &= ~data->cfg->hci_phy_ctl_clear;
314 writel(val, usb_phy->pmu + REG_HCI_PHY_CTL);
315 }
316
Andre Przywarab06b90f2023-06-12 00:32:38 +0100317 if (data->cfg->siddq_in_base) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530318 if (phy->id == 0) {
319 val = readl(data->base + data->cfg->phyctl_offset);
320 val |= PHY_CTL_VBUSVLDEXT;
321 val &= ~PHY_CTL_SIDDQ;
322 writel(val, data->base + data->cfg->phyctl_offset);
323 }
324 } else {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530325 if (usb_phy->id == 0)
326 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
327 PHY_RES45_CAL_DATA,
328 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530329
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530330 /* Adjust PHY's magnitude and rate */
331 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
332 PHY_TX_MAGNITUDE | PHY_TX_RATE,
333 PHY_TX_AMPLITUDE_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530334
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530335 /* Disconnect threshold adjustment */
336 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
337 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
338 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530339
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000340#ifdef CONFIG_USB_MUSB_SUNXI
341 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
342 if (usb_phy->id != 0)
343 sun4i_usb_phy_passby(phy, true);
344
345 /* Route PHY0 to MUSB to allow USB gadget */
346 if (data->cfg->phy0_dual_route)
347 sun4i_usb_phy0_reroute(data, true);
348#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530349 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530350
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000351 /* Route PHY0 to HCI to allow USB host */
352 if (data->cfg->phy0_dual_route)
353 sun4i_usb_phy0_reroute(data, false);
354#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530355
356 return 0;
357}
358
359static int sun4i_usb_phy_exit(struct phy *phy)
360{
361 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
362 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530363 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530364
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530365 if (phy->id == 0) {
Andre Przywarab06b90f2023-06-12 00:32:38 +0100366 if (data->cfg->siddq_in_base) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530367 void __iomem *phyctl = data->base +
368 data->cfg->phyctl_offset;
369
370 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
371 }
372 }
373
374 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530375
Jagan Teki0dc33332018-08-06 12:16:39 +0530376 ret = clk_disable(&usb_phy->clocks);
377 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400378 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
379 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530380 return ret;
381 }
382
383 ret = reset_assert(&usb_phy->resets);
384 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400385 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
386 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530387 return ret;
388 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530389
390 return 0;
391}
392
393static int sun4i_usb_phy_xlate(struct phy *phy,
394 struct ofnode_phandle_args *args)
395{
396 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
397
Andre Przywara6c53de52023-06-12 00:32:35 +0100398 if (args->args_count != 1)
399 return -EINVAL;
400
401 if (args->args[0] >= data->cfg->num_phys)
Jagan Tekid3c38282018-05-07 13:03:26 +0530402 return -EINVAL;
403
Andre Przywarab2f0f312019-06-23 15:09:49 +0100404 if (data->cfg->missing_phys & BIT(args->args[0]))
405 return -ENODEV;
406
Jagan Tekid3c38282018-05-07 13:03:26 +0530407 if (args->args_count)
408 phy->id = args->args[0];
409 else
410 phy->id = 0;
411
412 debug("%s: phy_id = %ld\n", __func__, phy->id);
413 return 0;
414}
415
Jagan Teki21fc42d2018-05-07 13:03:27 +0530416int sun4i_usb_phy_vbus_detect(struct phy *phy)
417{
418 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
419 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500420 int err = 1, retries = 3;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530421
Andre Przywara3331d222022-06-07 23:36:18 +0100422 if (dm_gpio_is_valid(&usb_phy->gpio_vbus_det)) {
423 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500424 /*
425 * Vbus may have been provided by the board and just turned off
426 * some milliseconds ago on reset. What we're measuring then is
427 * a residual charge on Vbus. Sleep a bit and try again.
428 */
429 while (err > 0 && retries--) {
430 mdelay(100);
Andre Przywara3331d222022-06-07 23:36:18 +0100431 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500432 }
Samuel Hollandc70137c2021-09-12 09:22:42 -0500433 } else if (data->vbus_power_supply) {
434 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530435 }
436
437 return err;
438}
439
440int sun4i_usb_phy_id_detect(struct phy *phy)
441{
442 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
443 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
444
Andre Przywara3331d222022-06-07 23:36:18 +0100445 if (!dm_gpio_is_valid(&usb_phy->gpio_id_det))
446 return -1;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530447
Andre Przywara3331d222022-06-07 23:36:18 +0100448 return dm_gpio_get_value(&usb_phy->gpio_id_det);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530449}
450
Jagan Teki37671e12018-05-07 13:03:37 +0530451void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
452{
453 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
454}
455
Jagan Tekid3c38282018-05-07 13:03:26 +0530456static struct phy_ops sun4i_usb_phy_ops = {
457 .of_xlate = sun4i_usb_phy_xlate,
458 .init = sun4i_usb_phy_init,
459 .power_on = sun4i_usb_phy_power_on,
460 .power_off = sun4i_usb_phy_power_off,
461 .exit = sun4i_usb_phy_exit,
462};
463
464static int sun4i_usb_phy_probe(struct udevice *dev)
465{
Simon Glassfa20e932020-12-03 16:55:20 -0700466 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530467 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
468 int i, ret;
469
470 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
471 if (!data->cfg)
472 return -EINVAL;
473
Matthias Schiffer47331932023-09-27 15:33:34 +0200474 data->base = (void __iomem *)dev_read_addr_name_ptr(dev, "phy_ctrl");
475 if (!data->base)
476 return -EINVAL;
Jagan Tekid3c38282018-05-07 13:03:26 +0530477
Samuel Hollandc70137c2021-09-12 09:22:42 -0500478 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
479 &data->vbus_power_supply);
480
Jagan Tekid3c38282018-05-07 13:03:26 +0530481 data->usb_phy = plat;
482 for (i = 0; i < data->cfg->num_phys; i++) {
483 struct sun4i_usb_phy_plat *phy = &plat[i];
484 struct sun4i_usb_phy_info *info = &phy_info[i];
485 char name[16];
486
Andre Przywarab2f0f312019-06-23 15:09:49 +0100487 if (data->cfg->missing_phys & BIT(i))
488 continue;
489
Andre Przywara3331d222022-06-07 23:36:18 +0100490 ret = dm_gpio_lookup_name(info->gpio_vbus, &phy->gpio_vbus);
491 if (ret == 0) {
492 ret = dm_gpio_request(&phy->gpio_vbus, "usb_vbus");
Jagan Tekid3c38282018-05-07 13:03:26 +0530493 if (ret)
494 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100495 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus,
496 GPIOD_IS_OUT);
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_value(&phy->gpio_vbus, 0);
500 if (ret)
501 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530502 }
503
Andre Przywara3331d222022-06-07 23:36:18 +0100504 ret = dm_gpio_lookup_name(info->gpio_vbus_det,
505 &phy->gpio_vbus_det);
506 if (ret == 0) {
507 ret = dm_gpio_request(&phy->gpio_vbus_det,
508 "usb_vbus_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530509 if (ret)
510 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100511 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus_det,
512 GPIOD_IS_IN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530513 if (ret)
514 return ret;
515 }
516
Andre Przywara3331d222022-06-07 23:36:18 +0100517 ret = dm_gpio_lookup_name(info->gpio_id_det, &phy->gpio_id_det);
518 if (ret == 0) {
519 ret = dm_gpio_request(&phy->gpio_id_det, "usb_id_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530520 if (ret)
521 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100522 ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,
523 GPIOD_IS_IN | GPIOD_PULL_UP);
Jagan Tekid3c38282018-05-07 13:03:26 +0530524 if (ret)
525 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530526 }
527
Jagan Teki0dc33332018-08-06 12:16:39 +0530528 if (data->cfg->dedicated_clocks)
529 snprintf(name, sizeof(name), "usb%d_phy", i);
530 else
531 strlcpy(name, "usb_phy", sizeof(name));
532
533 ret = clk_get_by_name(dev, name, &phy->clocks);
534 if (ret) {
535 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
536 return ret;
537 }
538
Andre Przywara720f4e42023-06-12 00:32:39 +0100539 /* Helper clock from PHY2 for the H616 PHY quirk */
540 snprintf(name, sizeof(name), "pmu%d_clk", i);
541 ret = clk_get_by_name_optional(dev, name, &phy->clk2);
542 if (ret) {
543 dev_err(dev, "failed to get pmu%d_clk clock phandle\n",
544 i);
545 return ret;
546 }
547
Jagan Teki0dc33332018-08-06 12:16:39 +0530548 snprintf(name, sizeof(name), "usb%d_reset", i);
549 ret = reset_get_by_name(dev, name, &phy->resets);
550 if (ret) {
551 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
552 return ret;
553 }
554
Jagan Tekid3c38282018-05-07 13:03:26 +0530555 if (i || data->cfg->phy0_dual_route) {
556 snprintf(name, sizeof(name), "pmu%d", i);
Matthias Schiffer47331932023-09-27 15:33:34 +0200557 phy->pmu = (void __iomem *)dev_read_addr_name_ptr(dev, name);
558 if (!phy->pmu)
559 return -EINVAL;
Jagan Tekid3c38282018-05-07 13:03:26 +0530560 }
561
562 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530563 };
564
Jagan Tekid3c38282018-05-07 13:03:26 +0530565 debug("Allwinner Sun4I USB PHY driver loaded\n");
566 return 0;
567}
568
Jagan Teki5a3000f2018-05-07 13:03:31 +0530569static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
570 .num_phys = 3,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530571 .disc_thresh = 3,
572 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530573 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530574};
575
576static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
577 .num_phys = 2,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530578 .disc_thresh = 2,
579 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530580 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530581};
582
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530583static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
584 .num_phys = 3,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530585 .disc_thresh = 3,
586 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530587 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530588};
589
Jagan Teki5a3000f2018-05-07 13:03:31 +0530590static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
591 .num_phys = 3,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530592 .disc_thresh = 2,
593 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530594 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530595};
596
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530597static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
598 .num_phys = 2,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530599 .disc_thresh = 3,
600 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530601 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530602};
603
Jagan Teki0e574bb2018-05-07 13:03:33 +0530604static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
605 .num_phys = 2,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530606 .disc_thresh = 3,
607 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530608 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530609};
610
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530611static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
612 .num_phys = 3,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100613 .hsic_index = 2,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530614 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530615 .dedicated_clocks = true,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100616 .siddq_in_base = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530617};
618
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530619static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
620 .num_phys = 4,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530621 .disc_thresh = 3,
622 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530623 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500624 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530625 .phy0_dual_route = true,
626};
627
Andre Przywara47d49972020-01-01 23:44:48 +0000628static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
629 .num_phys = 3,
Andre Przywara47d49972020-01-01 23:44:48 +0000630 .disc_thresh = 3,
631 .phyctl_offset = REG_PHYCTL_A33,
632 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500633 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Andre Przywara47d49972020-01-01 23:44:48 +0000634 .phy0_dual_route = true,
635};
636
Jagan Tekiac4bab42018-05-07 13:03:29 +0530637static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
638 .num_phys = 1,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530639 .disc_thresh = 3,
640 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530641 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500642 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530643 .phy0_dual_route = true,
644};
645
Samuel Holland9f30cce2022-07-14 23:09:22 -0500646static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
647 .num_phys = 2,
Samuel Holland9f30cce2022-07-14 23:09:22 -0500648 .phyctl_offset = REG_PHYCTL_A33,
649 .dedicated_clocks = true,
650 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
651 .phy0_dual_route = true,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100652 .siddq_in_base = true,
Samuel Holland9f30cce2022-07-14 23:09:22 -0500653};
654
Jagan Tekid3c38282018-05-07 13:03:26 +0530655static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
656 .num_phys = 2,
Jagan Tekid3c38282018-05-07 13:03:26 +0530657 .disc_thresh = 3,
658 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530659 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500660 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekid3c38282018-05-07 13:03:26 +0530661 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100662};
663
664static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
665 .num_phys = 4,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100666 .disc_thresh = 3,
667 .phyctl_offset = REG_PHYCTL_A33,
668 .dedicated_clocks = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100669 .phy0_dual_route = true,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100670 .siddq_in_base = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100671 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530672};
673
Andre Przywara8a3292a2023-06-12 00:32:40 +0100674static const struct sun4i_usb_phy_cfg sun50i_h616_cfg = {
675 .num_phys = 4,
676 .disc_thresh = 3,
677 .phyctl_offset = REG_PHYCTL_A33,
678 .dedicated_clocks = true,
679 .phy0_dual_route = true,
680 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
681 .needs_phy2_siddq = true,
682 .siddq_in_base = true,
683};
684
Andre Przywaraab81b0f2023-06-12 00:32:36 +0100685static const struct sun4i_usb_phy_cfg suniv_f1c100s_cfg = {
686 .num_phys = 1,
Andre Przywaraab81b0f2023-06-12 00:32:36 +0100687 .disc_thresh = 3,
688 .phyctl_offset = REG_PHYCTL_A10,
689 .dedicated_clocks = true,
690};
691
Jagan Tekid3c38282018-05-07 13:03:26 +0530692static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530693 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
694 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530695 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530696 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530697 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530698 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530699 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530700 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000701 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530702 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Samuel Holland9f30cce2022-07-14 23:09:22 -0500703 { .compatible = "allwinner,sun20i-d1-usb-phy", .data = (ulong)&sun20i_d1_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530704 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100705 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Andre Przywara8a3292a2023-06-12 00:32:40 +0100706 { .compatible = "allwinner,sun50i-h616-usb-phy", .data = (ulong)&sun50i_h616_cfg },
Andre Przywaraab81b0f2023-06-12 00:32:36 +0100707 { .compatible = "allwinner,suniv-f1c100s-usb-phy", .data = (ulong)&suniv_f1c100s_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530708 { }
709};
710
711U_BOOT_DRIVER(sun4i_usb_phy) = {
712 .name = "sun4i_usb_phy",
713 .id = UCLASS_PHY,
714 .of_match = sun4i_usb_phy_ids,
715 .ops = &sun4i_usb_phy_ops,
716 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700717 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700718 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530719};