blob: 86c589a65fd3959db1f2388897c22dd6d7b7c0b7 [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;
Jagan Tekid3c38282018-05-07 13:03:26 +0530128 int gpio_vbus;
129 int gpio_vbus_det;
130 int gpio_id_det;
Jagan Teki0dc33332018-08-06 12:16:39 +0530131 struct clk clocks;
132 struct reset_ctl resets;
Jagan Tekid3c38282018-05-07 13:03:26 +0530133 int id;
134};
135
136struct sun4i_usb_phy_data {
137 void __iomem *base;
Jagan Tekid3c38282018-05-07 13:03:26 +0530138 const struct sun4i_usb_phy_cfg *cfg;
139 struct sun4i_usb_phy_plat *usb_phy;
Samuel Hollandc70137c2021-09-12 09:22:42 -0500140 struct udevice *vbus_power_supply;
Jagan Tekid3c38282018-05-07 13:03:26 +0530141};
142
143static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
144
145static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
146{
147 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
148 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
149 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
150 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
151 int i;
152
153 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
154 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
155 writel(0, phyctl);
156 }
157
158 for (i = 0; i < len; i++) {
159 temp = readl(phyctl);
160
161 /* clear the address portion */
162 temp &= ~(0xff << 8);
163
164 /* set the address */
165 temp |= ((addr + i) << 8);
166 writel(temp, phyctl);
167
168 /* set the data bit and clear usbc bit*/
169 temp = readb(phyctl);
170 if (data & 0x1)
171 temp |= PHYCTL_DATA;
172 else
173 temp &= ~PHYCTL_DATA;
174 temp &= ~usbc_bit;
175 writeb(temp, phyctl);
176
177 /* pulse usbc_bit */
178 temp = readb(phyctl);
179 temp |= usbc_bit;
180 writeb(temp, phyctl);
181
182 temp = readb(phyctl);
183 temp &= ~usbc_bit;
184 writeb(temp, phyctl);
185
186 data >>= 1;
187 }
188}
189
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530190static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Tekid3c38282018-05-07 13:03:26 +0530191{
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530192 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
193 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Tekid3c38282018-05-07 13:03:26 +0530194 u32 bits, reg_value;
195
196 if (!usb_phy->pmu)
197 return;
198
199 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
200 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530201
202 /* A83T USB2 is HSIC */
203 if (data->cfg->type == sun8i_a83t_phy && usb_phy->id == 2)
204 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
205 SUNXI_HSIC;
206
Jagan Tekid3c38282018-05-07 13:03:26 +0530207 reg_value = readl(usb_phy->pmu);
208
209 if (enable)
210 reg_value |= bits;
211 else
212 reg_value &= ~bits;
213
214 writel(reg_value, usb_phy->pmu);
215}
216
217static int sun4i_usb_phy_power_on(struct phy *phy)
218{
219 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
220 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
221
222 if (initial_usb_scan_delay) {
223 mdelay(initial_usb_scan_delay);
224 initial_usb_scan_delay = 0;
225 }
226
Jagan Tekid3c38282018-05-07 13:03:26 +0530227 if (usb_phy->gpio_vbus >= 0)
228 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
229
230 return 0;
231}
232
233static int sun4i_usb_phy_power_off(struct phy *phy)
234{
235 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
236 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
237
Jagan Tekid3c38282018-05-07 13:03:26 +0530238 if (usb_phy->gpio_vbus >= 0)
239 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
240
241 return 0;
242}
243
244static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
245{
246 u32 regval;
247
248 regval = readl(data->base + REG_PHY_OTGCTL);
249 if (!id_det) {
250 /* Host mode. Route phy0 to EHCI/OHCI */
251 regval &= ~OTGCTL_ROUTE_MUSB;
252 } else {
253 /* Peripheral mode. Route phy0 to MUSB */
254 regval |= OTGCTL_ROUTE_MUSB;
255 }
256 writel(regval, data->base + REG_PHY_OTGCTL);
257}
258
259static int sun4i_usb_phy_init(struct phy *phy)
260{
261 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
262 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
263 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530264 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530265
Jagan Teki0dc33332018-08-06 12:16:39 +0530266 ret = clk_enable(&usb_phy->clocks);
267 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400268 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
269 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530270 return ret;
271 }
272
273 ret = reset_deassert(&usb_phy->resets);
274 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400275 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
276 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530277 return ret;
278 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530279
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300280 if (data->cfg->type == sun8i_a83t_phy ||
281 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530282 if (phy->id == 0) {
283 val = readl(data->base + data->cfg->phyctl_offset);
284 val |= PHY_CTL_VBUSVLDEXT;
285 val &= ~PHY_CTL_SIDDQ;
286 writel(val, data->base + data->cfg->phyctl_offset);
287 }
288 } else {
289 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
290 val = readl(usb_phy->pmu + REG_PMU_UNK1);
291 writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
292 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530293
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530294 if (usb_phy->id == 0)
295 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
296 PHY_RES45_CAL_DATA,
297 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530298
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530299 /* Adjust PHY's magnitude and rate */
300 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
301 PHY_TX_MAGNITUDE | PHY_TX_RATE,
302 PHY_TX_AMPLITUDE_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530303
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530304 /* Disconnect threshold adjustment */
305 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
306 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
307 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530308
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000309#ifdef CONFIG_USB_MUSB_SUNXI
310 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
311 if (usb_phy->id != 0)
312 sun4i_usb_phy_passby(phy, true);
313
314 /* Route PHY0 to MUSB to allow USB gadget */
315 if (data->cfg->phy0_dual_route)
316 sun4i_usb_phy0_reroute(data, true);
317#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530318 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530319
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000320 /* Route PHY0 to HCI to allow USB host */
321 if (data->cfg->phy0_dual_route)
322 sun4i_usb_phy0_reroute(data, false);
323#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530324
325 return 0;
326}
327
328static int sun4i_usb_phy_exit(struct phy *phy)
329{
330 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
331 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530332 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530333
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530334 if (phy->id == 0) {
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300335 if (data->cfg->type == sun8i_a83t_phy ||
336 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530337 void __iomem *phyctl = data->base +
338 data->cfg->phyctl_offset;
339
340 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
341 }
342 }
343
344 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530345
Jagan Teki0dc33332018-08-06 12:16:39 +0530346 ret = clk_disable(&usb_phy->clocks);
347 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400348 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
349 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530350 return ret;
351 }
352
353 ret = reset_assert(&usb_phy->resets);
354 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400355 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
356 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530357 return ret;
358 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530359
360 return 0;
361}
362
363static int sun4i_usb_phy_xlate(struct phy *phy,
364 struct ofnode_phandle_args *args)
365{
366 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
367
368 if (args->args_count >= data->cfg->num_phys)
369 return -EINVAL;
370
Andre Przywarab2f0f312019-06-23 15:09:49 +0100371 if (data->cfg->missing_phys & BIT(args->args[0]))
372 return -ENODEV;
373
Jagan Tekid3c38282018-05-07 13:03:26 +0530374 if (args->args_count)
375 phy->id = args->args[0];
376 else
377 phy->id = 0;
378
379 debug("%s: phy_id = %ld\n", __func__, phy->id);
380 return 0;
381}
382
Jagan Teki21fc42d2018-05-07 13:03:27 +0530383int sun4i_usb_phy_vbus_detect(struct phy *phy)
384{
385 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
386 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500387 int err = 1, retries = 3;
Jagan Teki21fc42d2018-05-07 13:03:27 +0530388
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500389 if (usb_phy->gpio_vbus_det >= 0) {
Jagan Teki21fc42d2018-05-07 13:03:27 +0530390 err = gpio_get_value(usb_phy->gpio_vbus_det);
Samuel Hollandf42dbdf2021-09-12 09:22:41 -0500391 /*
392 * Vbus may have been provided by the board and just turned off
393 * some milliseconds ago on reset. What we're measuring then is
394 * a residual charge on Vbus. Sleep a bit and try again.
395 */
396 while (err > 0 && retries--) {
397 mdelay(100);
398 err = gpio_get_value(usb_phy->gpio_vbus_det);
399 }
Samuel Hollandc70137c2021-09-12 09:22:42 -0500400 } else if (data->vbus_power_supply) {
401 err = regulator_get_enable(data->vbus_power_supply);
Jagan Teki21fc42d2018-05-07 13:03:27 +0530402 }
403
404 return err;
405}
406
407int sun4i_usb_phy_id_detect(struct phy *phy)
408{
409 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
410 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
411
Jagan Teki21fc42d2018-05-07 13:03:27 +0530412 if (usb_phy->gpio_id_det < 0)
413 return usb_phy->gpio_id_det;
414
415 return gpio_get_value(usb_phy->gpio_id_det);
416}
417
Jagan Teki37671e12018-05-07 13:03:37 +0530418void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
419{
420 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
421}
422
Jagan Tekid3c38282018-05-07 13:03:26 +0530423static struct phy_ops sun4i_usb_phy_ops = {
424 .of_xlate = sun4i_usb_phy_xlate,
425 .init = sun4i_usb_phy_init,
426 .power_on = sun4i_usb_phy_power_on,
427 .power_off = sun4i_usb_phy_power_off,
428 .exit = sun4i_usb_phy_exit,
429};
430
431static int sun4i_usb_phy_probe(struct udevice *dev)
432{
Simon Glassfa20e932020-12-03 16:55:20 -0700433 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530434 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
435 int i, ret;
436
437 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
438 if (!data->cfg)
439 return -EINVAL;
440
441 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
442 if (IS_ERR(data->base))
443 return PTR_ERR(data->base);
444
Samuel Hollandc70137c2021-09-12 09:22:42 -0500445 device_get_supply_regulator(dev, "usb0_vbus_power-supply",
446 &data->vbus_power_supply);
447
Jagan Tekid3c38282018-05-07 13:03:26 +0530448 data->usb_phy = plat;
449 for (i = 0; i < data->cfg->num_phys; i++) {
450 struct sun4i_usb_phy_plat *phy = &plat[i];
451 struct sun4i_usb_phy_info *info = &phy_info[i];
452 char name[16];
453
Andre Przywarab2f0f312019-06-23 15:09:49 +0100454 if (data->cfg->missing_phys & BIT(i))
455 continue;
456
Jagan Tekid3c38282018-05-07 13:03:26 +0530457 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
458 if (phy->gpio_vbus >= 0) {
459 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
460 if (ret)
461 return ret;
462 ret = gpio_direction_output(phy->gpio_vbus, 0);
463 if (ret)
464 return ret;
465 }
466
467 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
468 if (phy->gpio_vbus_det >= 0) {
469 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
470 if (ret)
471 return ret;
472 ret = gpio_direction_input(phy->gpio_vbus_det);
473 if (ret)
474 return ret;
475 }
476
477 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
478 if (phy->gpio_id_det >= 0) {
479 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
480 if (ret)
481 return ret;
482 ret = gpio_direction_input(phy->gpio_id_det);
483 if (ret)
484 return ret;
485 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
486 }
487
Jagan Teki0dc33332018-08-06 12:16:39 +0530488 if (data->cfg->dedicated_clocks)
489 snprintf(name, sizeof(name), "usb%d_phy", i);
490 else
491 strlcpy(name, "usb_phy", sizeof(name));
492
493 ret = clk_get_by_name(dev, name, &phy->clocks);
494 if (ret) {
495 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
496 return ret;
497 }
498
499 snprintf(name, sizeof(name), "usb%d_reset", i);
500 ret = reset_get_by_name(dev, name, &phy->resets);
501 if (ret) {
502 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
503 return ret;
504 }
505
Jagan Tekid3c38282018-05-07 13:03:26 +0530506 if (i || data->cfg->phy0_dual_route) {
507 snprintf(name, sizeof(name), "pmu%d", i);
508 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
509 if (IS_ERR(phy->pmu))
510 return PTR_ERR(phy->pmu);
511 }
512
513 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530514 };
515
Jagan Tekid3c38282018-05-07 13:03:26 +0530516 debug("Allwinner Sun4I USB PHY driver loaded\n");
517 return 0;
518}
519
Jagan Teki5a3000f2018-05-07 13:03:31 +0530520static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
521 .num_phys = 3,
522 .type = sun4i_a10_phy,
523 .disc_thresh = 3,
524 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530525 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530526 .enable_pmu_unk1 = false,
527};
528
529static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
530 .num_phys = 2,
531 .type = sun4i_a10_phy,
532 .disc_thresh = 2,
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
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530538static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
539 .num_phys = 3,
540 .type = sun6i_a31_phy,
541 .disc_thresh = 3,
542 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530543 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530544 .enable_pmu_unk1 = false,
545};
546
Jagan Teki5a3000f2018-05-07 13:03:31 +0530547static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
548 .num_phys = 3,
549 .type = sun4i_a10_phy,
550 .disc_thresh = 2,
551 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530552 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530553 .enable_pmu_unk1 = false,
554};
555
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530556static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
557 .num_phys = 2,
558 .type = sun4i_a10_phy,
559 .disc_thresh = 3,
560 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530561 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530562 .enable_pmu_unk1 = false,
563};
564
Jagan Teki0e574bb2018-05-07 13:03:33 +0530565static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
566 .num_phys = 2,
567 .type = sun8i_a33_phy,
568 .disc_thresh = 3,
569 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530570 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530571 .enable_pmu_unk1 = false,
572};
573
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530574static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
575 .num_phys = 3,
576 .type = sun8i_a83t_phy,
577 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530578 .dedicated_clocks = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530579};
580
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530581static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
582 .num_phys = 4,
583 .type = sun8i_h3_phy,
584 .disc_thresh = 3,
585 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530586 .dedicated_clocks = true,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530587 .enable_pmu_unk1 = true,
588 .phy0_dual_route = true,
589};
590
Andre Przywara47d49972020-01-01 23:44:48 +0000591static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
592 .num_phys = 3,
593 .type = sun8i_r40_phy,
594 .disc_thresh = 3,
595 .phyctl_offset = REG_PHYCTL_A33,
596 .dedicated_clocks = true,
597 .enable_pmu_unk1 = true,
598 .phy0_dual_route = true,
599};
600
Jagan Tekiac4bab42018-05-07 13:03:29 +0530601static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
602 .num_phys = 1,
603 .type = sun8i_v3s_phy,
604 .disc_thresh = 3,
605 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530606 .dedicated_clocks = true,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530607 .enable_pmu_unk1 = true,
608 .phy0_dual_route = true,
609};
610
Jagan Tekid3c38282018-05-07 13:03:26 +0530611static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
612 .num_phys = 2,
613 .type = sun50i_a64_phy,
614 .disc_thresh = 3,
615 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530616 .dedicated_clocks = true,
Jagan Tekid3c38282018-05-07 13:03:26 +0530617 .enable_pmu_unk1 = true,
618 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100619};
620
621static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
622 .num_phys = 4,
623 .type = sun50i_h6_phy,
624 .disc_thresh = 3,
625 .phyctl_offset = REG_PHYCTL_A33,
626 .dedicated_clocks = true,
627 .enable_pmu_unk1 = true,
628 .phy0_dual_route = true,
629 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530630};
631
632static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530633 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
634 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530635 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530636 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530637 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530638 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530639 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530640 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000641 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530642 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530643 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100644 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Jagan Tekid3c38282018-05-07 13:03:26 +0530645 { }
646};
647
648U_BOOT_DRIVER(sun4i_usb_phy) = {
649 .name = "sun4i_usb_phy",
650 .id = UCLASS_PHY,
651 .of_match = sun4i_usb_phy_ids,
652 .ops = &sun4i_usb_phy_ops,
653 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700654 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700655 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530656};