blob: 82713b83815f15c01fece8a07787a526e7e09171 [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>
Jagan Tekid3c38282018-05-07 13:03:26 +053029
30#define REG_ISCR 0x00
31#define REG_PHYCTL_A10 0x04
32#define REG_PHYBIST 0x08
33#define REG_PHYTUNE 0x0c
34#define REG_PHYCTL_A33 0x10
35#define REG_PHY_OTGCTL 0x20
36#define REG_PMU_UNK1 0x10
37
38/* Common Control Bits for Both PHYs */
39#define PHY_PLL_BW 0x03
40#define PHY_RES45_CAL_EN 0x0c
41
42/* Private Control Bits for Each PHY */
43#define PHY_TX_AMPLITUDE_TUNE 0x20
44#define PHY_TX_SLEWRATE_TUNE 0x22
45#define PHY_DISCON_TH_SEL 0x2a
Jagan Teki37671e12018-05-07 13:03:37 +053046#define PHY_SQUELCH_DETECT 0x3c
Jagan Tekid3c38282018-05-07 13:03:26 +053047
48#define PHYCTL_DATA BIT(7)
49#define OTGCTL_ROUTE_MUSB BIT(0)
50
51#define PHY_TX_RATE BIT(4)
52#define PHY_TX_MAGNITUDE BIT(2)
53#define PHY_TX_AMPLITUDE_LEN 5
54
55#define PHY_RES45_CAL_DATA BIT(0)
56#define PHY_RES45_CAL_LEN 1
57#define PHY_DISCON_TH_LEN 2
58
59#define SUNXI_AHB_ICHR8_EN BIT(10)
60#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
61#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
62#define SUNXI_ULPI_BYPASS_EN BIT(0)
63
Jagan Teki05a7b9f2018-05-07 13:03:30 +053064/* A83T specific control bits for PHY0 */
65#define PHY_CTL_VBUSVLDEXT BIT(5)
66#define PHY_CTL_SIDDQ BIT(3)
67
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
75enum sun4i_usb_phy_type {
Jagan Teki5a3000f2018-05-07 13:03:31 +053076 sun4i_a10_phy,
Jagan Teki1cbc80c2018-05-07 13:03:32 +053077 sun6i_a31_phy,
Jagan Teki0e574bb2018-05-07 13:03:33 +053078 sun8i_a33_phy,
Jagan Teki05a7b9f2018-05-07 13:03:30 +053079 sun8i_a83t_phy,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +053080 sun8i_h3_phy,
Andre Przywara47d49972020-01-01 23:44:48 +000081 sun8i_r40_phy,
Jagan Tekiac4bab42018-05-07 13:03:29 +053082 sun8i_v3s_phy,
Jagan Tekid3c38282018-05-07 13:03:26 +053083 sun50i_a64_phy,
Andre Przywarab2f0f312019-06-23 15:09:49 +010084 sun50i_h6_phy,
Jagan Tekid3c38282018-05-07 13:03:26 +053085};
86
87struct sun4i_usb_phy_cfg {
88 int num_phys;
89 enum sun4i_usb_phy_type type;
90 u32 disc_thresh;
91 u8 phyctl_offset;
Jagan Teki0dc33332018-08-06 12:16:39 +053092 bool dedicated_clocks;
Jagan Tekid3c38282018-05-07 13:03:26 +053093 bool enable_pmu_unk1;
94 bool phy0_dual_route;
Andre Przywarab2f0f312019-06-23 15:09:49 +010095 int missing_phys;
Jagan Tekid3c38282018-05-07 13:03:26 +053096};
97
98struct sun4i_usb_phy_info {
99 const char *gpio_vbus;
100 const char *gpio_vbus_det;
101 const char *gpio_id_det;
Jagan Tekid3c38282018-05-07 13:03:26 +0530102} phy_info[] = {
103 {
104 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
105 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
106 .gpio_id_det = CONFIG_USB0_ID_DET,
Jagan Tekid3c38282018-05-07 13:03:26 +0530107 },
108 {
109 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
110 .gpio_vbus_det = NULL,
111 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530112 },
113 {
114 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
115 .gpio_vbus_det = NULL,
116 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530117 },
118 {
119 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
120 .gpio_vbus_det = NULL,
121 .gpio_id_det = NULL,
Jagan Tekid3c38282018-05-07 13:03:26 +0530122 },
123};
124
125struct sun4i_usb_phy_plat {
126 void __iomem *pmu;
127 int power_on_count;
128 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;
140};
141
142static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
143
144static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
145{
146 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
147 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
148 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
149 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
150 int i;
151
152 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
153 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
154 writel(0, phyctl);
155 }
156
157 for (i = 0; i < len; i++) {
158 temp = readl(phyctl);
159
160 /* clear the address portion */
161 temp &= ~(0xff << 8);
162
163 /* set the address */
164 temp |= ((addr + i) << 8);
165 writel(temp, phyctl);
166
167 /* set the data bit and clear usbc bit*/
168 temp = readb(phyctl);
169 if (data & 0x1)
170 temp |= PHYCTL_DATA;
171 else
172 temp &= ~PHYCTL_DATA;
173 temp &= ~usbc_bit;
174 writeb(temp, phyctl);
175
176 /* pulse usbc_bit */
177 temp = readb(phyctl);
178 temp |= usbc_bit;
179 writeb(temp, phyctl);
180
181 temp = readb(phyctl);
182 temp &= ~usbc_bit;
183 writeb(temp, phyctl);
184
185 data >>= 1;
186 }
187}
188
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530189static void sun4i_usb_phy_passby(struct phy *phy, bool enable)
Jagan Tekid3c38282018-05-07 13:03:26 +0530190{
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530191 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
192 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Tekid3c38282018-05-07 13:03:26 +0530193 u32 bits, reg_value;
194
195 if (!usb_phy->pmu)
196 return;
197
198 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
199 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530200
201 /* A83T USB2 is HSIC */
202 if (data->cfg->type == sun8i_a83t_phy && usb_phy->id == 2)
203 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
204 SUNXI_HSIC;
205
Jagan Tekid3c38282018-05-07 13:03:26 +0530206 reg_value = readl(usb_phy->pmu);
207
208 if (enable)
209 reg_value |= bits;
210 else
211 reg_value &= ~bits;
212
213 writel(reg_value, usb_phy->pmu);
214}
215
216static int sun4i_usb_phy_power_on(struct phy *phy)
217{
218 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
219 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
220
221 if (initial_usb_scan_delay) {
222 mdelay(initial_usb_scan_delay);
223 initial_usb_scan_delay = 0;
224 }
225
226 usb_phy->power_on_count++;
227 if (usb_phy->power_on_count != 1)
228 return 0;
229
230 if (usb_phy->gpio_vbus >= 0)
231 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
232
233 return 0;
234}
235
236static int sun4i_usb_phy_power_off(struct phy *phy)
237{
238 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
239 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
240
241 usb_phy->power_on_count--;
242 if (usb_phy->power_on_count != 0)
243 return 0;
244
245 if (usb_phy->gpio_vbus >= 0)
246 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
247
248 return 0;
249}
250
251static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
252{
253 u32 regval;
254
255 regval = readl(data->base + REG_PHY_OTGCTL);
256 if (!id_det) {
257 /* Host mode. Route phy0 to EHCI/OHCI */
258 regval &= ~OTGCTL_ROUTE_MUSB;
259 } else {
260 /* Peripheral mode. Route phy0 to MUSB */
261 regval |= OTGCTL_ROUTE_MUSB;
262 }
263 writel(regval, data->base + REG_PHY_OTGCTL);
264}
265
266static int sun4i_usb_phy_init(struct phy *phy)
267{
268 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
269 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
270 u32 val;
Jagan Teki0dc33332018-08-06 12:16:39 +0530271 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530272
Jagan Teki0dc33332018-08-06 12:16:39 +0530273 ret = clk_enable(&usb_phy->clocks);
274 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400275 dev_err(phy->dev, "failed to enable usb_%ldphy clock\n",
276 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530277 return ret;
278 }
279
280 ret = reset_deassert(&usb_phy->resets);
281 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400282 dev_err(phy->dev, "failed to deassert usb_%ldreset reset\n",
283 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530284 return ret;
285 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530286
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300287 if (data->cfg->type == sun8i_a83t_phy ||
288 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530289 if (phy->id == 0) {
290 val = readl(data->base + data->cfg->phyctl_offset);
291 val |= PHY_CTL_VBUSVLDEXT;
292 val &= ~PHY_CTL_SIDDQ;
293 writel(val, data->base + data->cfg->phyctl_offset);
294 }
295 } else {
296 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
297 val = readl(usb_phy->pmu + REG_PMU_UNK1);
298 writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
299 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530300
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530301 if (usb_phy->id == 0)
302 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN,
303 PHY_RES45_CAL_DATA,
304 PHY_RES45_CAL_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530305
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530306 /* Adjust PHY's magnitude and rate */
307 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE,
308 PHY_TX_MAGNITUDE | PHY_TX_RATE,
309 PHY_TX_AMPLITUDE_LEN);
Jagan Tekid3c38282018-05-07 13:03:26 +0530310
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530311 /* Disconnect threshold adjustment */
312 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
313 data->cfg->disc_thresh, PHY_DISCON_TH_LEN);
314 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530315
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000316#ifdef CONFIG_USB_MUSB_SUNXI
317 /* Needed for HCI and conflicts with MUSB, keep PHY0 on MUSB */
318 if (usb_phy->id != 0)
319 sun4i_usb_phy_passby(phy, true);
320
321 /* Route PHY0 to MUSB to allow USB gadget */
322 if (data->cfg->phy0_dual_route)
323 sun4i_usb_phy0_reroute(data, true);
324#else
Jagan Tekib8cbf9d2018-07-20 12:34:20 +0530325 sun4i_usb_phy_passby(phy, true);
Jagan Tekid3c38282018-05-07 13:03:26 +0530326
Paul Kocialkowski2ee06372019-03-14 10:38:00 +0000327 /* Route PHY0 to HCI to allow USB host */
328 if (data->cfg->phy0_dual_route)
329 sun4i_usb_phy0_reroute(data, false);
330#endif
Jagan Tekid3c38282018-05-07 13:03:26 +0530331
332 return 0;
333}
334
335static int sun4i_usb_phy_exit(struct phy *phy)
336{
337 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
338 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
Jagan Teki0dc33332018-08-06 12:16:39 +0530339 int ret;
Jagan Tekid3c38282018-05-07 13:03:26 +0530340
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530341 if (phy->id == 0) {
Roman Stratiienko6b0f2482020-05-12 21:24:49 +0300342 if (data->cfg->type == sun8i_a83t_phy ||
343 data->cfg->type == sun50i_h6_phy) {
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530344 void __iomem *phyctl = data->base +
345 data->cfg->phyctl_offset;
346
347 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
348 }
349 }
350
351 sun4i_usb_phy_passby(phy, false);
Jagan Tekid3c38282018-05-07 13:03:26 +0530352
Jagan Teki0dc33332018-08-06 12:16:39 +0530353 ret = clk_disable(&usb_phy->clocks);
354 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400355 dev_err(phy->dev, "failed to disable usb_%ldphy clock\n",
356 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530357 return ret;
358 }
359
360 ret = reset_assert(&usb_phy->resets);
361 if (ret) {
Sean Andersone4d3c972020-09-15 10:45:04 -0400362 dev_err(phy->dev, "failed to assert usb_%ldreset reset\n",
363 phy->id);
Jagan Teki0dc33332018-08-06 12:16:39 +0530364 return ret;
365 }
Jagan Tekid3c38282018-05-07 13:03:26 +0530366
367 return 0;
368}
369
370static int sun4i_usb_phy_xlate(struct phy *phy,
371 struct ofnode_phandle_args *args)
372{
373 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
374
375 if (args->args_count >= data->cfg->num_phys)
376 return -EINVAL;
377
Andre Przywarab2f0f312019-06-23 15:09:49 +0100378 if (data->cfg->missing_phys & BIT(args->args[0]))
379 return -ENODEV;
380
Jagan Tekid3c38282018-05-07 13:03:26 +0530381 if (args->args_count)
382 phy->id = args->args[0];
383 else
384 phy->id = 0;
385
386 debug("%s: phy_id = %ld\n", __func__, phy->id);
387 return 0;
388}
389
Jagan Teki21fc42d2018-05-07 13:03:27 +0530390int sun4i_usb_phy_vbus_detect(struct phy *phy)
391{
392 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
393 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
394 int err, retries = 3;
395
396 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
397
398 if (usb_phy->gpio_vbus_det < 0)
399 return usb_phy->gpio_vbus_det;
400
401 err = gpio_get_value(usb_phy->gpio_vbus_det);
402 /*
403 * Vbus may have been provided by the board and just been turned of
404 * some milliseconds ago on reset, what we're measuring then is a
405 * residual charge on Vbus, sleep a bit and try again.
406 */
407 while (err > 0 && retries--) {
408 mdelay(100);
409 err = gpio_get_value(usb_phy->gpio_vbus_det);
410 }
411
412 return err;
413}
414
415int sun4i_usb_phy_id_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];
419
420 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
421
422 if (usb_phy->gpio_id_det < 0)
423 return usb_phy->gpio_id_det;
424
425 return gpio_get_value(usb_phy->gpio_id_det);
426}
427
Jagan Teki37671e12018-05-07 13:03:37 +0530428void sun4i_usb_phy_set_squelch_detect(struct phy *phy, bool enabled)
429{
430 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
431}
432
Jagan Tekid3c38282018-05-07 13:03:26 +0530433static struct phy_ops sun4i_usb_phy_ops = {
434 .of_xlate = sun4i_usb_phy_xlate,
435 .init = sun4i_usb_phy_init,
436 .power_on = sun4i_usb_phy_power_on,
437 .power_off = sun4i_usb_phy_power_off,
438 .exit = sun4i_usb_phy_exit,
439};
440
441static int sun4i_usb_phy_probe(struct udevice *dev)
442{
Simon Glassfa20e932020-12-03 16:55:20 -0700443 struct sun4i_usb_phy_plat *plat = dev_get_plat(dev);
Jagan Tekid3c38282018-05-07 13:03:26 +0530444 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
445 int i, ret;
446
447 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
448 if (!data->cfg)
449 return -EINVAL;
450
451 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
452 if (IS_ERR(data->base))
453 return PTR_ERR(data->base);
454
Jagan Tekid3c38282018-05-07 13:03:26 +0530455 data->usb_phy = plat;
456 for (i = 0; i < data->cfg->num_phys; i++) {
457 struct sun4i_usb_phy_plat *phy = &plat[i];
458 struct sun4i_usb_phy_info *info = &phy_info[i];
459 char name[16];
460
Andre Przywarab2f0f312019-06-23 15:09:49 +0100461 if (data->cfg->missing_phys & BIT(i))
462 continue;
463
Jagan Tekid3c38282018-05-07 13:03:26 +0530464 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
465 if (phy->gpio_vbus >= 0) {
466 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
467 if (ret)
468 return ret;
469 ret = gpio_direction_output(phy->gpio_vbus, 0);
470 if (ret)
471 return ret;
472 }
473
474 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
475 if (phy->gpio_vbus_det >= 0) {
476 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
477 if (ret)
478 return ret;
479 ret = gpio_direction_input(phy->gpio_vbus_det);
480 if (ret)
481 return ret;
482 }
483
484 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
485 if (phy->gpio_id_det >= 0) {
486 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
487 if (ret)
488 return ret;
489 ret = gpio_direction_input(phy->gpio_id_det);
490 if (ret)
491 return ret;
492 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
493 }
494
Jagan Teki0dc33332018-08-06 12:16:39 +0530495 if (data->cfg->dedicated_clocks)
496 snprintf(name, sizeof(name), "usb%d_phy", i);
497 else
498 strlcpy(name, "usb_phy", sizeof(name));
499
500 ret = clk_get_by_name(dev, name, &phy->clocks);
501 if (ret) {
502 dev_err(dev, "failed to get usb%d_phy clock phandle\n", i);
503 return ret;
504 }
505
506 snprintf(name, sizeof(name), "usb%d_reset", i);
507 ret = reset_get_by_name(dev, name, &phy->resets);
508 if (ret) {
509 dev_err(dev, "failed to get usb%d_reset reset phandle\n", i);
510 return ret;
511 }
512
Jagan Tekid3c38282018-05-07 13:03:26 +0530513 if (i || data->cfg->phy0_dual_route) {
514 snprintf(name, sizeof(name), "pmu%d", i);
515 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
516 if (IS_ERR(phy->pmu))
517 return PTR_ERR(phy->pmu);
518 }
519
520 phy->id = i;
Jagan Tekid3c38282018-05-07 13:03:26 +0530521 };
522
Jagan Tekid3c38282018-05-07 13:03:26 +0530523 debug("Allwinner Sun4I USB PHY driver loaded\n");
524 return 0;
525}
526
Jagan Teki5a3000f2018-05-07 13:03:31 +0530527static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
528 .num_phys = 3,
529 .type = sun4i_a10_phy,
530 .disc_thresh = 3,
531 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530532 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530533 .enable_pmu_unk1 = false,
534};
535
536static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
537 .num_phys = 2,
538 .type = sun4i_a10_phy,
539 .disc_thresh = 2,
540 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530541 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530542 .enable_pmu_unk1 = false,
543};
544
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530545static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
546 .num_phys = 3,
547 .type = sun6i_a31_phy,
548 .disc_thresh = 3,
549 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530550 .dedicated_clocks = true,
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530551 .enable_pmu_unk1 = false,
552};
553
Jagan Teki5a3000f2018-05-07 13:03:31 +0530554static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
555 .num_phys = 3,
556 .type = sun4i_a10_phy,
557 .disc_thresh = 2,
558 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530559 .dedicated_clocks = false,
Jagan Teki5a3000f2018-05-07 13:03:31 +0530560 .enable_pmu_unk1 = false,
561};
562
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530563static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
564 .num_phys = 2,
565 .type = sun4i_a10_phy,
566 .disc_thresh = 3,
567 .phyctl_offset = REG_PHYCTL_A10,
Jagan Teki0dc33332018-08-06 12:16:39 +0530568 .dedicated_clocks = true,
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530569 .enable_pmu_unk1 = false,
570};
571
Jagan Teki0e574bb2018-05-07 13:03:33 +0530572static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
573 .num_phys = 2,
574 .type = sun8i_a33_phy,
575 .disc_thresh = 3,
576 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530577 .dedicated_clocks = true,
Jagan Teki0e574bb2018-05-07 13:03:33 +0530578 .enable_pmu_unk1 = false,
579};
580
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530581static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
582 .num_phys = 3,
583 .type = sun8i_a83t_phy,
584 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530585 .dedicated_clocks = true,
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530586};
587
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530588static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
589 .num_phys = 4,
590 .type = sun8i_h3_phy,
591 .disc_thresh = 3,
592 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530593 .dedicated_clocks = true,
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530594 .enable_pmu_unk1 = true,
595 .phy0_dual_route = true,
596};
597
Andre Przywara47d49972020-01-01 23:44:48 +0000598static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
599 .num_phys = 3,
600 .type = sun8i_r40_phy,
601 .disc_thresh = 3,
602 .phyctl_offset = REG_PHYCTL_A33,
603 .dedicated_clocks = true,
604 .enable_pmu_unk1 = true,
605 .phy0_dual_route = true,
606};
607
Jagan Tekiac4bab42018-05-07 13:03:29 +0530608static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
609 .num_phys = 1,
610 .type = sun8i_v3s_phy,
611 .disc_thresh = 3,
612 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530613 .dedicated_clocks = true,
Jagan Tekiac4bab42018-05-07 13:03:29 +0530614 .enable_pmu_unk1 = true,
615 .phy0_dual_route = true,
616};
617
Jagan Tekid3c38282018-05-07 13:03:26 +0530618static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
619 .num_phys = 2,
620 .type = sun50i_a64_phy,
621 .disc_thresh = 3,
622 .phyctl_offset = REG_PHYCTL_A33,
Jagan Teki0dc33332018-08-06 12:16:39 +0530623 .dedicated_clocks = true,
Jagan Tekid3c38282018-05-07 13:03:26 +0530624 .enable_pmu_unk1 = true,
625 .phy0_dual_route = true,
Andre Przywarab2f0f312019-06-23 15:09:49 +0100626};
627
628static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
629 .num_phys = 4,
630 .type = sun50i_h6_phy,
631 .disc_thresh = 3,
632 .phyctl_offset = REG_PHYCTL_A33,
633 .dedicated_clocks = true,
634 .enable_pmu_unk1 = true,
635 .phy0_dual_route = true,
636 .missing_phys = BIT(1) | BIT(2),
Jagan Tekid3c38282018-05-07 13:03:26 +0530637};
638
639static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Teki5a3000f2018-05-07 13:03:31 +0530640 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = (ulong)&sun4i_a10_cfg },
641 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = (ulong)&sun5i_a13_cfg },
Jagan Teki1cbc80c2018-05-07 13:03:32 +0530642 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = (ulong)&sun6i_a31_cfg },
Jagan Teki5a3000f2018-05-07 13:03:31 +0530643 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = (ulong)&sun7i_a20_cfg },
Jagan Teki00f9f6b2018-05-07 13:03:34 +0530644 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = (ulong)&sun8i_a23_cfg },
Jagan Teki0e574bb2018-05-07 13:03:33 +0530645 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = (ulong)&sun8i_a33_cfg },
Jagan Teki05a7b9f2018-05-07 13:03:30 +0530646 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = (ulong)&sun8i_a83t_cfg },
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530647 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Andre Przywara47d49972020-01-01 23:44:48 +0000648 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = (ulong)&sun8i_r40_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530649 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530650 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
Andre Przywarab2f0f312019-06-23 15:09:49 +0100651 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = (ulong)&sun50i_h6_cfg},
Jagan Tekid3c38282018-05-07 13:03:26 +0530652 { }
653};
654
655U_BOOT_DRIVER(sun4i_usb_phy) = {
656 .name = "sun4i_usb_phy",
657 .id = UCLASS_PHY,
658 .of_match = sun4i_usb_phy_ids,
659 .ops = &sun4i_usb_phy_ops,
660 .probe = sun4i_usb_phy_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700661 .plat_auto = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700662 .priv_auto = sizeof(struct sun4i_usb_phy_data),
Jagan Tekid3c38282018-05-07 13:03:26 +0530663};