blob: 2def87897d4dc490534c6ec1b017414734f76a47 [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 {
Jagan Tekid3c38282018-05-07 13:03:26 +053089 const char *gpio_vbus_det;
90 const char *gpio_id_det;
Jagan Tekid3c38282018-05-07 13:03:26 +053091} phy_info[] = {
92 {
Jagan Tekid3c38282018-05-07 13:03:26 +053093 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
94 .gpio_id_det = CONFIG_USB0_ID_DET,
Jagan Tekid3c38282018-05-07 13:03:26 +053095 },
96 {
Jagan Tekid3c38282018-05-07 13:03:26 +053097 .gpio_vbus_det = NULL,
98 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +053099 },
100 {
Jagan Tekid3c38282018-05-07 13:03:26 +0530101 .gpio_vbus_det = NULL,
102 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530103 },
104 {
Jagan Tekid3c38282018-05-07 13:03:26 +0530105 .gpio_vbus_det = NULL,
106 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530107 },
108};
109
110struct sun4i_usb_phy_plat {
111 void __iomem *pmu;
Andre Przywara3331d222022-06-07 23:36:18 +0100112 struct gpio_desc gpio_vbus_det;
113 struct gpio_desc gpio_id_det;
Jagan Teki0dc33332018-08-06 12:16:39 +0530114 struct clk clocks;
Andre Przywara720f4e42023-06-12 00:32:39 +0100115 struct clk clk2;
Jagan Teki0dc33332018-08-06 12:16:39 +0530116 struct reset_ctl resets;
Samuel Holland9a361ef2023-10-31 01:39:53 -0500117 struct udevice *vbus;
Jagan Tekid3c38282018-05-07 13:03:26 +0530118 int id;
119};
120
121struct sun4i_usb_phy_data {
122 void __iomem *base;
Jagan Tekid3c38282018-05-07 13:03:26 +0530123 const struct sun4i_usb_phy_cfg *cfg;
124 struct sun4i_usb_phy_plat *usb_phy;
Samuel Hollandc70137c2021-09-12 09:22:42 -0500125 struct udevice *vbus_power_supply;
Jagan Tekid3c38282018-05-07 13:03:26 +0530126};
127
128static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
129
130static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
131{
132 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
133 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
134 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
135 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
136 int i;
137
138 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
139 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
140 writel(0, phyctl);
141 }
142
143 for (i = 0; i < len; i++) {
144 temp = readl(phyctl);
145
146 /* clear the address portion */
147 temp &= ~(0xff << 8);
148
149 /* set the address */
150 temp |= ((addr + i) << 8);
151 writel(temp, phyctl);
152
153 /* set the data bit and clear usbc bit*/
154 temp = readb(phyctl);
155 if (data & 0x1)
156 temp |= PHYCTL_DATA;
157 else
158 temp &= ~PHYCTL_DATA;
159 temp &= ~usbc_bit;
160 writeb(temp, phyctl);
161
162 /* pulse usbc_bit */
163 temp = readb(phyctl);
164 temp |= usbc_bit;
165 writeb(temp, phyctl);
166
167 temp = readb(phyctl);
168 temp &= ~usbc_bit;
169 writeb(temp, phyctl);
170
171 data >>= 1;
172 }
173}
174
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530175static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Tekid3c38282018-05-07 13:03:26 +0530176{
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530177 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
178 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Tekid3c38282018-05-07 13:03:26 +0530179 u32 bits, reg_value;
180
181 if (!usb_phy->pmu)
182 return;
183
184 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
185 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530186
187 /* A83T USB2 is HSIC */
Andre Przywarab06b90f2023-06-12 00:32:38 +0100188 if (data->cfg->hsic_index && usb_phy->id == data->cfg->hsic_index)
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530189 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
190 SUNXI_HSIC;
191
Jagan Tekid3c38282018-05-07 13:03:26 +0530192 reg_value = readl(usb_phy->pmu);
193
194 if (enable)
195 reg_value |= bits;
196 else
197 reg_value &= ~bits;
198
199 writel(reg_value, usb_phy->pmu);
200}
201
202static int sun4i_usb_phy_power_on(struct phy *phy)
203{
204 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
205 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Holland9a361ef2023-10-31 01:39:53 -0500206 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530207
208 if (initial_usb_scan_delay) {
209 mdelay(initial_usb_scan_delay);
210 initial_usb_scan_delay = 0;
211 }
212
Samuel Hollandad991492022-07-14 22:34:53 -0500213 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
214 if (phy->id == 0 && sun4i_usb_phy_vbus_detect(phy)) {
215 dev_warn(phy->dev, "External vbus detected, not enabling our own vbus\n");
216 return 0;
217 }
218
Samuel Holland9a361ef2023-10-31 01:39:53 -0500219 ret = regulator_set_enable_if_allowed(usb_phy->vbus, true);
220 if (ret)
221 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530222
223 return 0;
224}
225
226static int sun4i_usb_phy_power_off(struct phy *phy)
227{
228 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
229 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Holland9a361ef2023-10-31 01:39:53 -0500230 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530231
Samuel Holland9a361ef2023-10-31 01:39:53 -0500232 ret = regulator_set_enable_if_allowed(usb_phy->vbus, false);
233 if (ret)
234 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530235
236 return 0;
237}
238
239static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
240{
241 u32 regval;
242
243 regval = readl(data->base + REG_PHY_OTGCTL);
244 if (!id_det) {
245 /* Host mode. Route phy0 to EHCI/OHCI */
246 regval &= ~OTGCTL_ROUTE_MUSB;
247 } else {
248 /* Peripheral mode. Route phy0 to MUSB */
249 regval |= OTGCTL_ROUTE_MUSB;
250 }
251 writel(regval, data->base + REG_PHY_OTGCTL);
252}
253
254static int sun4i_usb_phy_init(struct phy *phy)
255{
256 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
257 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
258 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530259 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530260
Jagan Teki0dc33332018-08-06 12:16:39 +0530261 ret = clk_enable(&usb_phy->clocks);
262 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400263 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
264 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530265 return ret;
266 }
267
268 ret = reset_deassert(&usb_phy->resets);
269 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400270 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
271 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530272 return ret;
273 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530274
Andre Przywara720f4e42023-06-12 00:32:39 +0100275 /* Some PHYs on some SoCs (the H616) need the help of PHY2 to work. */
276 if (data->cfg->needs_phy2_siddq && phy->id != 2) {
277 struct sun4i_usb_phy_plat *phy2 = &data->usb_phy[2];
278
279 ret = clk_enable(&phy2->clocks);
280 if (ret) {
281 dev_err(phy->dev, "failed to enable aux clock\n");
282 return ret;
283 }
284
285 ret = reset_deassert(&phy2->resets);
286 if (ret) {
287 dev_err(phy->dev, "failed to deassert aux reset\n");
288 return ret;
289 }
290
291 /*
292 * This extra clock is just needed to access the
293 * REG_HCI_PHY_CTL PMU register for PHY2.
294 */
295 ret = clk_enable(&phy2->clk2);
296 if (ret) {
297 dev_err(phy->dev, "failed to enable PHY2 clock\n");
298 return ret;
299 }
300
301 if (phy2->pmu && data->cfg->hci_phy_ctl_clear) {
302 val = readl(phy2->pmu + REG_HCI_PHY_CTL);
303 val &= ~data->cfg->hci_phy_ctl_clear;
304 writel(val, phy2->pmu + REG_HCI_PHY_CTL);
305 }
306
307 clk_disable(&phy2->clk2);
308 }
309
Andre Przywara8662e7e2022-07-14 23:09:21 -0500310 if (usb_phy->pmu && data->cfg->hci_phy_ctl_clear) {
311 val = readl(usb_phy->pmu + REG_HCI_PHY_CTL);
312 val &= ~data->cfg->hci_phy_ctl_clear;
313 writel(val, usb_phy->pmu + REG_HCI_PHY_CTL);
314 }
315
Andre Przywarab06b90f2023-06-12 00:32:38 +0100316 if (data->cfg->siddq_in_base) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530317 if (phy->id == 0) {
318 val = readl(data->base + data->cfg->phyctl_offset);
319 val |= PHY_CTL_VBUSVLDEXT;
320 val &= ~PHY_CTL_SIDDQ;
321 writel(val, data->base + data->cfg->phyctl_offset);
322 }
323 } else {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530324 if (usb_phy->id == 0)
325 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
326 PHY_RES45_CAL_DATA,
327 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530328
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530329 /* Adjust PHY's magnitude and rate */
330 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
331 PHY_TX_MAGNITUDE | PHY_TX_RATE,
332 PHY_TX_AMPLITUDE_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530333
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530334 /* Disconnect threshold adjustment */
335 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
336 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
337 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530338
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000339#ifdef CONFIG_USB_MUSB_SUNXI
340 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
341 if (usb_phy->id != 0)
342 sun4i_usb_phy_passby(phy, true);
343
344 /* Route PHY0 to MUSB to allow USB gadget */
345 if (data->cfg->phy0_dual_route)
346 sun4i_usb_phy0_reroute(data, true);
347#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530348 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530349
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000350 /* Route PHY0 to HCI to allow USB host */
351 if (data->cfg->phy0_dual_route)
352 sun4i_usb_phy0_reroute(data, false);
353#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530354
355 return 0;
356}
357
358static int sun4i_usb_phy_exit(struct phy *phy)
359{
360 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
361 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530362 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530363
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530364 if (phy->id == 0) {
Andre Przywarab06b90f2023-06-12 00:32:38 +0100365 if (data->cfg->siddq_in_base) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530366 void __iomem *phyctl = data->base +
367 data->cfg->phyctl_offset;
368
369 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
370 }
371 }
372
373 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530374
Jagan Teki0dc33332018-08-06 12:16:39 +0530375 ret = clk_disable(&usb_phy->clocks);
376 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400377 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
378 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530379 return ret;
380 }
381
382 ret = reset_assert(&usb_phy->resets);
383 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400384 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
385 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530386 return ret;
387 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530388
389 return 0;
390}
391
392static int sun4i_usb_phy_xlate(struct phy *phy,
393 struct ofnode_phandle_args *args)
394{
395 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
396
Andre Przywara6c53de52023-06-12 00:32:35 +0100397 if (args->args_count != 1)
398 return -EINVAL;
399
400 if (args->args[0] >= data->cfg->num_phys)
Jagan Tekid3c38282018-05-07 13:03:26 +0530401 return -EINVAL;
402
Andre Przywarab2f0f312019-06-23 15:09:49 +0100403 if (data->cfg->missing_phys & BIT(args->args[0]))
404 return -ENODEV;
405
Jagan Tekid3c38282018-05-07 13:03:26 +0530406 if (args->args_count)
407 phy->id = args->args[0];
408 else
409 phy->id = 0;
410
411 debug("%s: phy_id = %ld\n", __func__, phy->id);
412 return 0;
413}
414
Jagan Teki21fc42d2018-05-07 13:03:27 +0530415int sun4i_usb_phy_vbus_detect(struct phy *phy)
416{
417 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
418 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500419 int err = 1, retries = 3;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530420
Andre Przywara3331d222022-06-07 23:36:18 +0100421 if (dm_gpio_is_valid(&usb_phy->gpio_vbus_det)) {
422 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500423 /*
424 * Vbus may have been provided by the board and just turned off
425 * some milliseconds ago on reset. What we're measuring then is
426 * a residual charge on Vbus. Sleep a bit and try again.
427 */
428 while (err > 0 && retries--) {
429 mdelay(100);
Andre Przywara3331d222022-06-07 23:36:18 +0100430 err = dm_gpio_get_value(&usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500431 }
Samuel Hollandc70137c2021-09-12 09:22:42 -0500432 } else if (data->vbus_power_supply) {
433 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530434 }
435
436 return err;
437}
438
439int sun4i_usb_phy_id_detect(struct phy *phy)
440{
441 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
442 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
443
Andre Przywara3331d222022-06-07 23:36:18 +0100444 if (!dm_gpio_is_valid(&usb_phy->gpio_id_det))
445 return -1;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530446
Andre Przywara3331d222022-06-07 23:36:18 +0100447 return dm_gpio_get_value(&usb_phy->gpio_id_det);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530448}
449
Jagan Teki37671e12018-05-07 13:03:37 +0530450void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
451{
452 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
453}
454
Jagan Tekid3c38282018-05-07 13:03:26 +0530455static struct phy_ops sun4i_usb_phy_ops = {
456 .of_xlate = sun4i_usb_phy_xlate,
457 .init = sun4i_usb_phy_init,
458 .power_on = sun4i_usb_phy_power_on,
459 .power_off = sun4i_usb_phy_power_off,
460 .exit = sun4i_usb_phy_exit,
461};
462
463static int sun4i_usb_phy_probe(struct udevice *dev)
464{
Simon Glassfa20e932020-12-03 16:55:20 -0700465 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530466 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
467 int i, ret;
468
469 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
470 if (!data->cfg)
471 return -EINVAL;
472
Matthias Schiffer47331932023-09-27 15:33:34 +0200473 data->base = (void __iomem *)dev_read_addr_name_ptr(dev, "phy_ctrl");
474 if (!data->base)
475 return -EINVAL;
Jagan Tekid3c38282018-05-07 13:03:26 +0530476
Samuel Hollandc70137c2021-09-12 09:22:42 -0500477 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
478 &data->vbus_power_supply);
479
Jagan Tekid3c38282018-05-07 13:03:26 +0530480 data->usb_phy = plat;
481 for (i = 0; i < data->cfg->num_phys; i++) {
482 struct sun4i_usb_phy_plat *phy = &plat[i];
483 struct sun4i_usb_phy_info *info = &phy_info[i];
Samuel Holland9a361ef2023-10-31 01:39:53 -0500484 char name[32];
Jagan Tekid3c38282018-05-07 13:03:26 +0530485
Andre Przywarab2f0f312019-06-23 15:09:49 +0100486 if (data->cfg->missing_phys & BIT(i))
487 continue;
488
Samuel Holland9a361ef2023-10-31 01:39:53 -0500489 snprintf(name, sizeof(name), "usb%d_vbus-supply", i);
490 ret = device_get_supply_regulator(dev, name, &phy->vbus);
491 if (phy->vbus) {
492 ret = regulator_set_enable_if_allowed(phy->vbus, false);
Andre Przywara3331d222022-06-07 23:36:18 +0100493 if (ret)
494 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530495 }
496
Andre Przywara3331d222022-06-07 23:36:18 +0100497 ret = dm_gpio_lookup_name(info->gpio_vbus_det,
498 &phy->gpio_vbus_det);
499 if (ret == 0) {
500 ret = dm_gpio_request(&phy->gpio_vbus_det,
501 "usb_vbus_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530502 if (ret)
503 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100504 ret = dm_gpio_set_dir_flags(&phy->gpio_vbus_det,
505 GPIOD_IS_IN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530506 if (ret)
507 return ret;
508 }
509
Andre Przywara3331d222022-06-07 23:36:18 +0100510 ret = dm_gpio_lookup_name(info->gpio_id_det, &phy->gpio_id_det);
511 if (ret == 0) {
512 ret = dm_gpio_request(&phy->gpio_id_det, "usb_id_det");
Jagan Tekid3c38282018-05-07 13:03:26 +0530513 if (ret)
514 return ret;
Andre Przywara3331d222022-06-07 23:36:18 +0100515 ret = dm_gpio_set_dir_flags(&phy->gpio_id_det,
516 GPIOD_IS_IN | GPIOD_PULL_UP);
Jagan Tekid3c38282018-05-07 13:03:26 +0530517 if (ret)
518 return ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530519 }
520
Jagan Teki0dc33332018-08-06 12:16:39 +0530521 if (data->cfg->dedicated_clocks)
522 snprintf(name, sizeof(name), "usb%d_phy", i);
523 else
524 strlcpy(name, "usb_phy", sizeof(name));
525
526 ret = clk_get_by_name(dev, name, &phy->clocks);
527 if (ret) {
528 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
529 return ret;
530 }
531
Andre Przywara720f4e42023-06-12 00:32:39 +0100532 /* Helper clock from PHY2 for the H616 PHY quirk */
533 snprintf(name, sizeof(name), "pmu%d_clk", i);
534 ret = clk_get_by_name_optional(dev, name, &phy->clk2);
535 if (ret) {
536 dev_err(dev, "failed to get pmu%d_clk clock phandle\n",
537 i);
538 return ret;
539 }
540
Jagan Teki0dc33332018-08-06 12:16:39 +0530541 snprintf(name, sizeof(name), "usb%d_reset", i);
542 ret = reset_get_by_name(dev, name, &phy->resets);
543 if (ret) {
544 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
545 return ret;
546 }
547
Jagan Tekid3c38282018-05-07 13:03:26 +0530548 if (i || data->cfg->phy0_dual_route) {
549 snprintf(name, sizeof(name), "pmu%d", i);
Matthias Schiffer47331932023-09-27 15:33:34 +0200550 phy->pmu = (void __iomem *)dev_read_addr_name_ptr(dev, name);
551 if (!phy->pmu)
552 return -EINVAL;
Jagan Tekid3c38282018-05-07 13:03:26 +0530553 }
554
555 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530556 };
557
Jagan Tekid3c38282018-05-07 13:03:26 +0530558 debug("Allwinner Sun4I USB PHY driver loaded\n");
559 return 0;
560}
561
Jagan Teki5a3000f2018-05-07 13:03:31 +0530562static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
563 .num_phys = 3,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530564 .disc_thresh = 3,
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
569static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
570 .num_phys = 2,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530571 .disc_thresh = 2,
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
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530576static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
577 .num_phys = 3,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530578 .disc_thresh = 3,
579 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530580 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530581};
582
Jagan Teki5a3000f2018-05-07 13:03:31 +0530583static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
584 .num_phys = 3,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530585 .disc_thresh = 2,
586 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530587 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530588};
589
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530590static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
591 .num_phys = 2,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530592 .disc_thresh = 3,
593 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530594 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530595};
596
Jagan Teki0e574bb2018-05-07 13:03:33 +0530597static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
598 .num_phys = 2,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530599 .disc_thresh = 3,
600 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530601 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530602};
603
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530604static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
605 .num_phys = 3,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100606 .hsic_index = 2,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530607 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530608 .dedicated_clocks = true,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100609 .siddq_in_base = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530610};
611
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530612static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
613 .num_phys = 4,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530614 .disc_thresh = 3,
615 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530616 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500617 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530618 .phy0_dual_route = true,
619};
620
Andre Przywara47d49972020-01-01 23:44:48 +0000621static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
622 .num_phys = 3,
Andre Przywara47d49972020-01-01 23:44:48 +0000623 .disc_thresh = 3,
624 .phyctl_offset = REG_PHYCTL_A33,
625 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500626 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Andre Przywara47d49972020-01-01 23:44:48 +0000627 .phy0_dual_route = true,
628};
629
Jagan Tekiac4bab42018-05-07 13:03:29 +0530630static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
631 .num_phys = 1,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530632 .disc_thresh = 3,
633 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530634 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500635 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530636 .phy0_dual_route = true,
637};
638
Samuel Holland9f30cce2022-07-14 23:09:22 -0500639static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
640 .num_phys = 2,
Samuel Holland9f30cce2022-07-14 23:09:22 -0500641 .phyctl_offset = REG_PHYCTL_A33,
642 .dedicated_clocks = true,
643 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
644 .phy0_dual_route = true,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100645 .siddq_in_base = true,
Samuel Holland9f30cce2022-07-14 23:09:22 -0500646};
647
Jagan Tekid3c38282018-05-07 13:03:26 +0530648static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
649 .num_phys = 2,
Jagan Tekid3c38282018-05-07 13:03:26 +0530650 .disc_thresh = 3,
651 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530652 .dedicated_clocks = true,
Andre Przywara8662e7e2022-07-14 23:09:21 -0500653 .hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
Jagan Tekid3c38282018-05-07 13:03:26 +0530654 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100655};
656
657static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
658 .num_phys = 4,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100659 .disc_thresh = 3,
660 .phyctl_offset = REG_PHYCTL_A33,
661 .dedicated_clocks = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100662 .phy0_dual_route = true,
Andre Przywarab06b90f2023-06-12 00:32:38 +0100663 .siddq_in_base = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100664 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530665};
666
Andre Przywara8a3292a2023-06-12 00:32:40 +0100667static const struct sun4i_usb_phy_cfg sun50i_h616_cfg = {
668 .num_phys = 4,
669 .disc_thresh = 3,
670 .phyctl_offset = REG_PHYCTL_A33,
671 .dedicated_clocks = true,
672 .phy0_dual_route = true,
673 .hci_phy_ctl_clear = PHY_CTL_SIDDQ,
674 .needs_phy2_siddq = true,
675 .siddq_in_base = true,
676};
677
Andre Przywaraab81b0f2023-06-12 00:32:36 +0100678static const struct sun4i_usb_phy_cfg suniv_f1c100s_cfg = {
679 .num_phys = 1,
Andre Przywaraab81b0f2023-06-12 00:32:36 +0100680 .disc_thresh = 3,
681 .phyctl_offset = REG_PHYCTL_A10,
682 .dedicated_clocks = true,
683};
684
Jagan Tekid3c38282018-05-07 13:03:26 +0530685static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530686 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
687 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530688 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530689 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530690 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530691 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530692 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530693 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000694 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530695 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Samuel Holland9f30cce2022-07-14 23:09:22 -0500696 { .compatible = "allwinner,sun20i-d1-usb-phy", .data = (ulong)&sun20i_d1_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530697 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100698 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Andre Przywara8a3292a2023-06-12 00:32:40 +0100699 { .compatible = "allwinner,sun50i-h616-usb-phy", .data = (ulong)&sun50i_h616_cfg },
Andre Przywaraab81b0f2023-06-12 00:32:36 +0100700 { .compatible = "allwinner,suniv-f1c100s-usb-phy", .data = (ulong)&suniv_f1c100s_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530701 { }
702};
703
704U_BOOT_DRIVER(sun4i_usb_phy) = {
705 .name = "sun4i_usb_phy",
706 .id = UCLASS_PHY,
707 .of_match = sun4i_usb_phy_ids,
708 .ops = &sun4i_usb_phy_ops,
709 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700710 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700711 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530712};