blob: ab2a5d17fcff15700a944ae0fa17271f8741f35a [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>
23#include <asm/arch/clock.h>
24#include <asm/arch/cpu.h>
Simon Glass9bc15642020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060027#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070028#include <linux/err.h>
Samuel Hollandc70137c2021-09-12 09:22:42 -050029#include <power/regulator.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053030
31#define REG_ISCR 0x00
32#define REG_PHYCTL_A10 0x04
33#define REG_PHYBIST 0x08
34#define REG_PHYTUNE 0x0c
35#define REG_PHYCTL_A33 0x10
36#define REG_PHY_OTGCTL 0x20
37#define REG_PMU_UNK1 0x10
38
39/* Common Control Bits for Both PHYs */
40#define PHY_PLL_BW 0x03
41#define PHY_RES45_CAL_EN 0x0c
42
43/* Private Control Bits for Each PHY */
44#define PHY_TX_AMPLITUDE_TUNE 0x20
45#define PHY_TX_SLEWRATE_TUNE 0x22
46#define PHY_DISCON_TH_SEL 0x2a
Jagan Teki37671e12018-05-07 13:03:37 +053047#define PHY_SQUELCH_DETECT 0x3c
Jagan Tekid3c38282018-05-07 13:03:26 +053048
49#define PHYCTL_DATA BIT(7)
50#define OTGCTL_ROUTE_MUSB BIT(0)
51
52#define PHY_TX_RATE BIT(4)
53#define PHY_TX_MAGNITUDE BIT(2)
54#define PHY_TX_AMPLITUDE_LEN 5
55
56#define PHY_RES45_CAL_DATA BIT(0)
57#define PHY_RES45_CAL_LEN 1
58#define PHY_DISCON_TH_LEN 2
59
60#define SUNXI_AHB_ICHR8_EN BIT(10)
61#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
62#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
63#define SUNXI_ULPI_BYPASS_EN BIT(0)
64
Jagan Teki05a7b9f2018-05-07 13:03:30 +053065/* A83T specific control bits for PHY0 */
66#define PHY_CTL_VBUSVLDEXT BIT(5)
67#define PHY_CTL_SIDDQ BIT(3)
68
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;
92 u8 phyctl_offset;
Jagan Teki0dc33332018-08-06 12:16:39 +053093 bool dedicated_clocks;
Jagan Tekid3c38282018-05-07 13:03:26 +053094 bool enable_pmu_unk1;
95 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;
128 int power_on_count;
129 int gpio_vbus;
130 int gpio_vbus_det;
131 int gpio_id_det;
Jagan Teki0dc33332018-08-06 12:16:39 +0530132 struct clk clocks;
133 struct reset_ctl resets;
Jagan Tekid3c38282018-05-07 13:03:26 +0530134 int id;
135};
136
137struct sun4i_usb_phy_data {
138 void __iomem *base;
Jagan Tekid3c38282018-05-07 13:03:26 +0530139 const struct sun4i_usb_phy_cfg *cfg;
140 struct sun4i_usb_phy_plat *usb_phy;
Samuel Hollandc70137c2021-09-12 09:22:42 -0500141 struct udevice *vbus_power_supply;
Jagan Tekid3c38282018-05-07 13:03:26 +0530142};
143
144static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
145
146static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
147{
148 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
149 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
150 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
151 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
152 int i;
153
154 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
155 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
156 writel(0, phyctl);
157 }
158
159 for (i = 0; i < len; i++) {
160 temp = readl(phyctl);
161
162 /* clear the address portion */
163 temp &= ~(0xff << 8);
164
165 /* set the address */
166 temp |= ((addr + i) << 8);
167 writel(temp, phyctl);
168
169 /* set the data bit and clear usbc bit*/
170 temp = readb(phyctl);
171 if (data & 0x1)
172 temp |= PHYCTL_DATA;
173 else
174 temp &= ~PHYCTL_DATA;
175 temp &= ~usbc_bit;
176 writeb(temp, phyctl);
177
178 /* pulse usbc_bit */
179 temp = readb(phyctl);
180 temp |= usbc_bit;
181 writeb(temp, phyctl);
182
183 temp = readb(phyctl);
184 temp &= ~usbc_bit;
185 writeb(temp, phyctl);
186
187 data >>= 1;
188 }
189}
190
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530191static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Tekid3c38282018-05-07 13:03:26 +0530192{
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530193 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
194 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Tekid3c38282018-05-07 13:03:26 +0530195 u32 bits, reg_value;
196
197 if (!usb_phy->pmu)
198 return;
199
200 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
201 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530202
203 /* A83T USB2 is HSIC */
204 if (data->cfg->type == sun8i_a83t_phy && usb_phy->id == 2)
205 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
206 SUNXI_HSIC;
207
Jagan Tekid3c38282018-05-07 13:03:26 +0530208 reg_value = readl(usb_phy->pmu);
209
210 if (enable)
211 reg_value |= bits;
212 else
213 reg_value &= ~bits;
214
215 writel(reg_value, usb_phy->pmu);
216}
217
218static int sun4i_usb_phy_power_on(struct phy *phy)
219{
220 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
221 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
222
223 if (initial_usb_scan_delay) {
224 mdelay(initial_usb_scan_delay);
225 initial_usb_scan_delay = 0;
226 }
227
228 usb_phy->power_on_count++;
229 if (usb_phy->power_on_count != 1)
230 return 0;
231
232 if (usb_phy->gpio_vbus >= 0)
233 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
234
235 return 0;
236}
237
238static int sun4i_usb_phy_power_off(struct phy *phy)
239{
240 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
241 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
242
243 usb_phy->power_on_count--;
244 if (usb_phy->power_on_count != 0)
245 return 0;
246
247 if (usb_phy->gpio_vbus >= 0)
248 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
249
250 return 0;
251}
252
253static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
254{
255 u32 regval;
256
257 regval = readl(data->base + REG_PHY_OTGCTL);
258 if (!id_det) {
259 /* Host mode. Route phy0 to EHCI/OHCI */
260 regval &= ~OTGCTL_ROUTE_MUSB;
261 } else {
262 /* Peripheral mode. Route phy0 to MUSB */
263 regval |= OTGCTL_ROUTE_MUSB;
264 }
265 writel(regval, data->base + REG_PHY_OTGCTL);
266}
267
268static int sun4i_usb_phy_init(struct phy *phy)
269{
270 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
271 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
272 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530273 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530274
Jagan Teki0dc33332018-08-06 12:16:39 +0530275 ret = clk_enable(&usb_phy->clocks);
276 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400277 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
278 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530279 return ret;
280 }
281
282 ret = reset_deassert(&usb_phy->resets);
283 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400284 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
285 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530286 return ret;
287 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530288
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300289 if (data->cfg->type == sun8i_a83t_phy ||
290 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530291 if (phy->id == 0) {
292 val = readl(data->base + data->cfg->phyctl_offset);
293 val |= PHY_CTL_VBUSVLDEXT;
294 val &= ~PHY_CTL_SIDDQ;
295 writel(val, data->base + data->cfg->phyctl_offset);
296 }
297 } else {
298 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
299 val = readl(usb_phy->pmu + REG_PMU_UNK1);
300 writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
301 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530302
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530303 if (usb_phy->id == 0)
304 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
305 PHY_RES45_CAL_DATA,
306 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530307
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530308 /* Adjust PHY's magnitude and rate */
309 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
310 PHY_TX_MAGNITUDE | PHY_TX_RATE,
311 PHY_TX_AMPLITUDE_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530312
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530313 /* Disconnect threshold adjustment */
314 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
315 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
316 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530317
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000318#ifdef CONFIG_USB_MUSB_SUNXI
319 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
320 if (usb_phy->id != 0)
321 sun4i_usb_phy_passby(phy, true);
322
323 /* Route PHY0 to MUSB to allow USB gadget */
324 if (data->cfg->phy0_dual_route)
325 sun4i_usb_phy0_reroute(data, true);
326#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530327 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530328
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000329 /* Route PHY0 to HCI to allow USB host */
330 if (data->cfg->phy0_dual_route)
331 sun4i_usb_phy0_reroute(data, false);
332#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530333
334 return 0;
335}
336
337static int sun4i_usb_phy_exit(struct phy *phy)
338{
339 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
340 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530341 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530342
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530343 if (phy->id == 0) {
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300344 if (data->cfg->type == sun8i_a83t_phy ||
345 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530346 void __iomem *phyctl = data->base +
347 data->cfg->phyctl_offset;
348
349 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
350 }
351 }
352
353 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530354
Jagan Teki0dc33332018-08-06 12:16:39 +0530355 ret = clk_disable(&usb_phy->clocks);
356 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400357 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
358 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530359 return ret;
360 }
361
362 ret = reset_assert(&usb_phy->resets);
363 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400364 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
365 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530366 return ret;
367 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530368
369 return 0;
370}
371
372static int sun4i_usb_phy_xlate(struct phy *phy,
373 struct ofnode_phandle_args *args)
374{
375 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
376
377 if (args->args_count >= data->cfg->num_phys)
378 return -EINVAL;
379
Andre Przywarab2f0f312019-06-23 15:09:49 +0100380 if (data->cfg->missing_phys & BIT(args->args[0]))
381 return -ENODEV;
382
Jagan Tekid3c38282018-05-07 13:03:26 +0530383 if (args->args_count)
384 phy->id = args->args[0];
385 else
386 phy->id = 0;
387
388 debug("%s: phy_id = %ld\n", __func__, phy->id);
389 return 0;
390}
391
Jagan Teki21fc42d2018-05-07 13:03:27 +0530392int sun4i_usb_phy_vbus_detect(struct phy *phy)
393{
394 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
395 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500396 int err = 1, retries = 3;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530397
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500398 if (usb_phy->gpio_vbus_det >= 0) {
Jagan Teki21fc42d2018-05-07 13:03:27 +0530399 err = gpio_get_value(usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500400 /*
401 * Vbus may have been provided by the board and just turned off
402 * some milliseconds ago on reset. What we're measuring then is
403 * a residual charge on Vbus. Sleep a bit and try again.
404 */
405 while (err > 0 && retries--) {
406 mdelay(100);
407 err = gpio_get_value(usb_phy->gpio_vbus_det);
408 }
Samuel Hollandc70137c2021-09-12 09:22:42 -0500409 } else if (data->vbus_power_supply) {
410 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530411 }
412
413 return err;
414}
415
416int sun4i_usb_phy_id_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];
420
Jagan Teki21fc42d2018-05-07 13:03:27 +0530421 if (usb_phy->gpio_id_det < 0)
422 return usb_phy->gpio_id_det;
423
424 return gpio_get_value(usb_phy->gpio_id_det);
425}
426
Jagan Teki37671e12018-05-07 13:03:37 +0530427void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
428{
429 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
430}
431
Jagan Tekid3c38282018-05-07 13:03:26 +0530432static struct phy_ops sun4i_usb_phy_ops = {
433 .of_xlate = sun4i_usb_phy_xlate,
434 .init = sun4i_usb_phy_init,
435 .power_on = sun4i_usb_phy_power_on,
436 .power_off = sun4i_usb_phy_power_off,
437 .exit = sun4i_usb_phy_exit,
438};
439
440static int sun4i_usb_phy_probe(struct udevice *dev)
441{
Simon Glassfa20e932020-12-03 16:55:20 -0700442 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530443 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
444 int i, ret;
445
446 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
447 if (!data->cfg)
448 return -EINVAL;
449
450 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
451 if (IS_ERR(data->base))
452 return PTR_ERR(data->base);
453
Samuel Hollandc70137c2021-09-12 09:22:42 -0500454 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
455 &data->vbus_power_supply);
456
Jagan Tekid3c38282018-05-07 13:03:26 +0530457 data->usb_phy = plat;
458 for (i = 0; i < data->cfg->num_phys; i++) {
459 struct sun4i_usb_phy_plat *phy = &plat[i];
460 struct sun4i_usb_phy_info *info = &phy_info[i];
461 char name[16];
462
Andre Przywarab2f0f312019-06-23 15:09:49 +0100463 if (data->cfg->missing_phys & BIT(i))
464 continue;
465
Jagan Tekid3c38282018-05-07 13:03:26 +0530466 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
467 if (phy->gpio_vbus >= 0) {
468 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
469 if (ret)
470 return ret;
471 ret = gpio_direction_output(phy->gpio_vbus, 0);
472 if (ret)
473 return ret;
474 }
475
476 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
477 if (phy->gpio_vbus_det >= 0) {
478 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
479 if (ret)
480 return ret;
481 ret = gpio_direction_input(phy->gpio_vbus_det);
482 if (ret)
483 return ret;
484 }
485
486 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
487 if (phy->gpio_id_det >= 0) {
488 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
489 if (ret)
490 return ret;
491 ret = gpio_direction_input(phy->gpio_id_det);
492 if (ret)
493 return ret;
494 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
495 }
496
Jagan Teki0dc33332018-08-06 12:16:39 +0530497 if (data->cfg->dedicated_clocks)
498 snprintf(name, sizeof(name), "usb%d_phy", i);
499 else
500 strlcpy(name, "usb_phy", sizeof(name));
501
502 ret = clk_get_by_name(dev, name, &phy->clocks);
503 if (ret) {
504 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
505 return ret;
506 }
507
508 snprintf(name, sizeof(name), "usb%d_reset", i);
509 ret = reset_get_by_name(dev, name, &phy->resets);
510 if (ret) {
511 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
512 return ret;
513 }
514
Jagan Tekid3c38282018-05-07 13:03:26 +0530515 if (i || data->cfg->phy0_dual_route) {
516 snprintf(name, sizeof(name), "pmu%d", i);
517 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
518 if (IS_ERR(phy->pmu))
519 return PTR_ERR(phy->pmu);
520 }
521
522 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530523 };
524
Jagan Tekid3c38282018-05-07 13:03:26 +0530525 debug("Allwinner Sun4I USB PHY driver loaded\n");
526 return 0;
527}
528
Jagan Teki5a3000f2018-05-07 13:03:31 +0530529static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
530 .num_phys = 3,
531 .type = sun4i_a10_phy,
532 .disc_thresh = 3,
533 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530534 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530535 .enable_pmu_unk1 = false,
536};
537
538static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
539 .num_phys = 2,
540 .type = sun4i_a10_phy,
541 .disc_thresh = 2,
542 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530543 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530544 .enable_pmu_unk1 = false,
545};
546
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530547static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
548 .num_phys = 3,
549 .type = sun6i_a31_phy,
550 .disc_thresh = 3,
551 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530552 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530553 .enable_pmu_unk1 = false,
554};
555
Jagan Teki5a3000f2018-05-07 13:03:31 +0530556static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
557 .num_phys = 3,
558 .type = sun4i_a10_phy,
559 .disc_thresh = 2,
560 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530561 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530562 .enable_pmu_unk1 = false,
563};
564
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530565static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
566 .num_phys = 2,
567 .type = sun4i_a10_phy,
568 .disc_thresh = 3,
569 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530570 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530571 .enable_pmu_unk1 = false,
572};
573
Jagan Teki0e574bb2018-05-07 13:03:33 +0530574static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
575 .num_phys = 2,
576 .type = sun8i_a33_phy,
577 .disc_thresh = 3,
578 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530579 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530580 .enable_pmu_unk1 = false,
581};
582
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530583static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
584 .num_phys = 3,
585 .type = sun8i_a83t_phy,
586 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530587 .dedicated_clocks = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530588};
589
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530590static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
591 .num_phys = 4,
592 .type = sun8i_h3_phy,
593 .disc_thresh = 3,
594 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530595 .dedicated_clocks = true,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530596 .enable_pmu_unk1 = true,
597 .phy0_dual_route = true,
598};
599
Andre Przywara47d49972020-01-01 23:44:48 +0000600static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
601 .num_phys = 3,
602 .type = sun8i_r40_phy,
603 .disc_thresh = 3,
604 .phyctl_offset = REG_PHYCTL_A33,
605 .dedicated_clocks = true,
606 .enable_pmu_unk1 = true,
607 .phy0_dual_route = true,
608};
609
Jagan Tekiac4bab42018-05-07 13:03:29 +0530610static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
611 .num_phys = 1,
612 .type = sun8i_v3s_phy,
613 .disc_thresh = 3,
614 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530615 .dedicated_clocks = true,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530616 .enable_pmu_unk1 = true,
617 .phy0_dual_route = true,
618};
619
Jagan Tekid3c38282018-05-07 13:03:26 +0530620static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
621 .num_phys = 2,
622 .type = sun50i_a64_phy,
623 .disc_thresh = 3,
624 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530625 .dedicated_clocks = true,
Jagan Tekid3c38282018-05-07 13:03:26 +0530626 .enable_pmu_unk1 = true,
627 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100628};
629
630static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
631 .num_phys = 4,
632 .type = sun50i_h6_phy,
633 .disc_thresh = 3,
634 .phyctl_offset = REG_PHYCTL_A33,
635 .dedicated_clocks = true,
636 .enable_pmu_unk1 = true,
637 .phy0_dual_route = true,
638 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530639};
640
641static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530642 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
643 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530644 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530645 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530646 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530647 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530648 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530649 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000650 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530651 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530652 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100653 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Jagan Tekid3c38282018-05-07 13:03:26 +0530654 { }
655};
656
657U_BOOT_DRIVER(sun4i_usb_phy) = {
658 .name = "sun4i_usb_phy",
659 .id = UCLASS_PHY,
660 .of_match = sun4i_usb_phy_ids,
661 .ops = &sun4i_usb_phy_ops,
662 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700663 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700664 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530665};