blob: de0a59a32c60a0838018f6c8a46e31049d330f1c [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>
14#include <dm.h>
15#include <dm/device.h>
16#include <generic-phy.h>
Jagan Teki21fc42d2018-05-07 13:03:27 +053017#include <phy-sun4i-usb.h>
Jagan Tekid3c38282018-05-07 13:03:26 +053018#include <asm/gpio.h>
19#include <asm/io.h>
20#include <asm/arch/clock.h>
21#include <asm/arch/cpu.h>
22
23#define REG_ISCR 0x00
24#define REG_PHYCTL_A10 0x04
25#define REG_PHYBIST 0x08
26#define REG_PHYTUNE 0x0c
27#define REG_PHYCTL_A33 0x10
28#define REG_PHY_OTGCTL 0x20
29#define REG_PMU_UNK1 0x10
30
31/* Common Control Bits for Both PHYs */
32#define PHY_PLL_BW 0x03
33#define PHY_RES45_CAL_EN 0x0c
34
35/* Private Control Bits for Each PHY */
36#define PHY_TX_AMPLITUDE_TUNE 0x20
37#define PHY_TX_SLEWRATE_TUNE 0x22
38#define PHY_DISCON_TH_SEL 0x2a
39
40#define PHYCTL_DATA BIT(7)
41#define OTGCTL_ROUTE_MUSB BIT(0)
42
43#define PHY_TX_RATE BIT(4)
44#define PHY_TX_MAGNITUDE BIT(2)
45#define PHY_TX_AMPLITUDE_LEN 5
46
47#define PHY_RES45_CAL_DATA BIT(0)
48#define PHY_RES45_CAL_LEN 1
49#define PHY_DISCON_TH_LEN 2
50
51#define SUNXI_AHB_ICHR8_EN BIT(10)
52#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
53#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
54#define SUNXI_ULPI_BYPASS_EN BIT(0)
55
56#define MAX_PHYS 4
57
58enum sun4i_usb_phy_type {
Jagan Tekic1b0e5a2018-05-07 13:03:28 +053059 sun8i_h3_phy,
Jagan Tekiac4bab42018-05-07 13:03:29 +053060 sun8i_v3s_phy,
Jagan Tekid3c38282018-05-07 13:03:26 +053061 sun50i_a64_phy,
62};
63
64struct sun4i_usb_phy_cfg {
65 int num_phys;
66 enum sun4i_usb_phy_type type;
67 u32 disc_thresh;
68 u8 phyctl_offset;
69 bool enable_pmu_unk1;
70 bool phy0_dual_route;
71};
72
73struct sun4i_usb_phy_info {
74 const char *gpio_vbus;
75 const char *gpio_vbus_det;
76 const char *gpio_id_det;
77 int rst_mask;
78} phy_info[] = {
79 {
80 .gpio_vbus = CONFIG_USB0_VBUS_PIN,
81 .gpio_vbus_det = CONFIG_USB0_VBUS_DET,
82 .gpio_id_det = CONFIG_USB0_ID_DET,
83 .rst_mask = (CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK),
84 },
85 {
86 .gpio_vbus = CONFIG_USB1_VBUS_PIN,
87 .gpio_vbus_det = NULL,
88 .gpio_id_det = NULL,
89 .rst_mask = (CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK),
90 },
91 {
92 .gpio_vbus = CONFIG_USB2_VBUS_PIN,
93 .gpio_vbus_det = NULL,
94 .gpio_id_det = NULL,
95 .rst_mask = (CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK),
96 },
97 {
98 .gpio_vbus = CONFIG_USB3_VBUS_PIN,
99 .gpio_vbus_det = NULL,
100 .gpio_id_det = NULL,
101 .rst_mask = (CCM_USB_CTRL_PHY3_RST | CCM_USB_CTRL_PHY3_CLK),
102 },
103};
104
105struct sun4i_usb_phy_plat {
106 void __iomem *pmu;
107 int power_on_count;
108 int gpio_vbus;
109 int gpio_vbus_det;
110 int gpio_id_det;
111 int rst_mask;
112 int id;
113};
114
115struct sun4i_usb_phy_data {
116 void __iomem *base;
117 struct sunxi_ccm_reg *ccm;
118 const struct sun4i_usb_phy_cfg *cfg;
119 struct sun4i_usb_phy_plat *usb_phy;
120};
121
122static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
123
124static void sun4i_usb_phy_write(struct phy *phy, u32 addr, u32 data, int len)
125{
126 struct sun4i_usb_phy_data *phy_data = dev_get_priv(phy->dev);
127 struct sun4i_usb_phy_plat *usb_phy = &phy_data->usb_phy[phy->id];
128 u32 temp, usbc_bit = BIT(usb_phy->id * 2);
129 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
130 int i;
131
132 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
133 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
134 writel(0, phyctl);
135 }
136
137 for (i = 0; i < len; i++) {
138 temp = readl(phyctl);
139
140 /* clear the address portion */
141 temp &= ~(0xff << 8);
142
143 /* set the address */
144 temp |= ((addr + i) << 8);
145 writel(temp, phyctl);
146
147 /* set the data bit and clear usbc bit*/
148 temp = readb(phyctl);
149 if (data & 0x1)
150 temp |= PHYCTL_DATA;
151 else
152 temp &= ~PHYCTL_DATA;
153 temp &= ~usbc_bit;
154 writeb(temp, phyctl);
155
156 /* pulse usbc_bit */
157 temp = readb(phyctl);
158 temp |= usbc_bit;
159 writeb(temp, phyctl);
160
161 temp = readb(phyctl);
162 temp &= ~usbc_bit;
163 writeb(temp, phyctl);
164
165 data >>= 1;
166 }
167}
168
169static void sun4i_usb_phy_passby(struct sun4i_usb_phy_plat *usb_phy,
170 bool enable)
171{
172 u32 bits, reg_value;
173
174 if (!usb_phy->pmu)
175 return;
176
177 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
178 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
179 reg_value = readl(usb_phy->pmu);
180
181 if (enable)
182 reg_value |= bits;
183 else
184 reg_value &= ~bits;
185
186 writel(reg_value, usb_phy->pmu);
187}
188
189static int sun4i_usb_phy_power_on(struct phy *phy)
190{
191 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
192 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
193
194 if (initial_usb_scan_delay) {
195 mdelay(initial_usb_scan_delay);
196 initial_usb_scan_delay = 0;
197 }
198
199 usb_phy->power_on_count++;
200 if (usb_phy->power_on_count != 1)
201 return 0;
202
203 if (usb_phy->gpio_vbus >= 0)
204 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
205
206 return 0;
207}
208
209static int sun4i_usb_phy_power_off(struct phy *phy)
210{
211 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
212 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
213
214 usb_phy->power_on_count--;
215 if (usb_phy->power_on_count != 0)
216 return 0;
217
218 if (usb_phy->gpio_vbus >= 0)
219 gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
220
221 return 0;
222}
223
224static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, bool id_det)
225{
226 u32 regval;
227
228 regval = readl(data->base + REG_PHY_OTGCTL);
229 if (!id_det) {
230 /* Host mode. Route phy0 to EHCI/OHCI */
231 regval &= ~OTGCTL_ROUTE_MUSB;
232 } else {
233 /* Peripheral mode. Route phy0 to MUSB */
234 regval |= OTGCTL_ROUTE_MUSB;
235 }
236 writel(regval, data->base + REG_PHY_OTGCTL);
237}
238
239static int sun4i_usb_phy_init(struct phy *phy)
240{
241 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
242 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
243 u32 val;
244
245 setbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask);
246
247 if (usb_phy->pmu && data->cfg->enable_pmu_unk1) {
248 val = readl(usb_phy->pmu + REG_PMU_UNK1);
249 writel(val & ~2, usb_phy->pmu + REG_PMU_UNK1);
250 }
251
252 if (usb_phy->id == 0)
253 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, PHY_RES45_CAL_DATA,
254 PHY_RES45_CAL_LEN);
255
256 /* Adjust PHY's magnitude and rate */
257 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, PHY_TX_MAGNITUDE |
258 PHY_TX_RATE, PHY_TX_AMPLITUDE_LEN);
259
260 /* Disconnect threshold adjustment */
261 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->cfg->disc_thresh,
262 PHY_DISCON_TH_LEN);
263
264 if (usb_phy->id != 0)
265 sun4i_usb_phy_passby(usb_phy, true);
266
267 sun4i_usb_phy0_reroute(data, true);
268
269 return 0;
270}
271
272static int sun4i_usb_phy_exit(struct phy *phy)
273{
274 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
275 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
276
277 sun4i_usb_phy_passby(usb_phy, false);
278
279 clrbits_le32(&data->ccm->usb_clk_cfg, usb_phy->rst_mask);
280
281 return 0;
282}
283
284static int sun4i_usb_phy_xlate(struct phy *phy,
285 struct ofnode_phandle_args *args)
286{
287 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
288
289 if (args->args_count >= data->cfg->num_phys)
290 return -EINVAL;
291
292 if (args->args_count)
293 phy->id = args->args[0];
294 else
295 phy->id = 0;
296
297 debug("%s: phy_id = %ld\n", __func__, phy->id);
298 return 0;
299}
300
Jagan Teki21fc42d2018-05-07 13:03:27 +0530301int sun4i_usb_phy_vbus_detect(struct phy *phy)
302{
303 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
304 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
305 int err, retries = 3;
306
307 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
308
309 if (usb_phy->gpio_vbus_det < 0)
310 return usb_phy->gpio_vbus_det;
311
312 err = gpio_get_value(usb_phy->gpio_vbus_det);
313 /*
314 * Vbus may have been provided by the board and just been turned of
315 * some milliseconds ago on reset, what we're measuring then is a
316 * residual charge on Vbus, sleep a bit and try again.
317 */
318 while (err > 0 && retries--) {
319 mdelay(100);
320 err = gpio_get_value(usb_phy->gpio_vbus_det);
321 }
322
323 return err;
324}
325
326int sun4i_usb_phy_id_detect(struct phy *phy)
327{
328 struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
329 struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
330
331 debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
332
333 if (usb_phy->gpio_id_det < 0)
334 return usb_phy->gpio_id_det;
335
336 return gpio_get_value(usb_phy->gpio_id_det);
337}
338
Jagan Tekid3c38282018-05-07 13:03:26 +0530339static struct phy_ops sun4i_usb_phy_ops = {
340 .of_xlate = sun4i_usb_phy_xlate,
341 .init = sun4i_usb_phy_init,
342 .power_on = sun4i_usb_phy_power_on,
343 .power_off = sun4i_usb_phy_power_off,
344 .exit = sun4i_usb_phy_exit,
345};
346
347static int sun4i_usb_phy_probe(struct udevice *dev)
348{
349 struct sun4i_usb_phy_plat *plat = dev_get_platdata(dev);
350 struct sun4i_usb_phy_data *data = dev_get_priv(dev);
351 int i, ret;
352
353 data->cfg = (const struct sun4i_usb_phy_cfg *)dev_get_driver_data(dev);
354 if (!data->cfg)
355 return -EINVAL;
356
357 data->base = (void __iomem *)devfdt_get_addr_name(dev, "phy_ctrl");
358 if (IS_ERR(data->base))
359 return PTR_ERR(data->base);
360
361 data->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
362 if (IS_ERR(data->ccm))
363 return PTR_ERR(data->ccm);
364
365 data->usb_phy = plat;
366 for (i = 0; i < data->cfg->num_phys; i++) {
367 struct sun4i_usb_phy_plat *phy = &plat[i];
368 struct sun4i_usb_phy_info *info = &phy_info[i];
369 char name[16];
370
371 phy->gpio_vbus = sunxi_name_to_gpio(info->gpio_vbus);
372 if (phy->gpio_vbus >= 0) {
373 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
374 if (ret)
375 return ret;
376 ret = gpio_direction_output(phy->gpio_vbus, 0);
377 if (ret)
378 return ret;
379 }
380
381 phy->gpio_vbus_det = sunxi_name_to_gpio(info->gpio_vbus_det);
382 if (phy->gpio_vbus_det >= 0) {
383 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
384 if (ret)
385 return ret;
386 ret = gpio_direction_input(phy->gpio_vbus_det);
387 if (ret)
388 return ret;
389 }
390
391 phy->gpio_id_det = sunxi_name_to_gpio(info->gpio_id_det);
392 if (phy->gpio_id_det >= 0) {
393 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
394 if (ret)
395 return ret;
396 ret = gpio_direction_input(phy->gpio_id_det);
397 if (ret)
398 return ret;
399 sunxi_gpio_set_pull(phy->gpio_id_det, SUNXI_GPIO_PULL_UP);
400 }
401
402 if (i || data->cfg->phy0_dual_route) {
403 snprintf(name, sizeof(name), "pmu%d", i);
404 phy->pmu = (void __iomem *)devfdt_get_addr_name(dev, name);
405 if (IS_ERR(phy->pmu))
406 return PTR_ERR(phy->pmu);
407 }
408
409 phy->id = i;
410 phy->rst_mask = info->rst_mask;
411 };
412
413 setbits_le32(&data->ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
414
415 debug("Allwinner Sun4I USB PHY driver loaded\n");
416 return 0;
417}
418
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530419static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
420 .num_phys = 4,
421 .type = sun8i_h3_phy,
422 .disc_thresh = 3,
423 .phyctl_offset = REG_PHYCTL_A33,
424 .enable_pmu_unk1 = true,
425 .phy0_dual_route = true,
426};
427
Jagan Tekiac4bab42018-05-07 13:03:29 +0530428static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
429 .num_phys = 1,
430 .type = sun8i_v3s_phy,
431 .disc_thresh = 3,
432 .phyctl_offset = REG_PHYCTL_A33,
433 .enable_pmu_unk1 = true,
434 .phy0_dual_route = true,
435};
436
Jagan Tekid3c38282018-05-07 13:03:26 +0530437static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
438 .num_phys = 2,
439 .type = sun50i_a64_phy,
440 .disc_thresh = 3,
441 .phyctl_offset = REG_PHYCTL_A33,
442 .enable_pmu_unk1 = true,
443 .phy0_dual_route = true,
444};
445
446static const struct udevice_id sun4i_usb_phy_ids[] = {
Jagan Tekic1b0e5a2018-05-07 13:03:28 +0530447 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = (ulong)&sun8i_h3_cfg },
Jagan Tekiac4bab42018-05-07 13:03:29 +0530448 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = (ulong)&sun8i_v3s_cfg },
Jagan Tekid3c38282018-05-07 13:03:26 +0530449 { .compatible = "allwinner,sun50i-a64-usb-phy", .data = (ulong)&sun50i_a64_cfg},
450 { }
451};
452
453U_BOOT_DRIVER(sun4i_usb_phy) = {
454 .name = "sun4i_usb_phy",
455 .id = UCLASS_PHY,
456 .of_match = sun4i_usb_phy_ids,
457 .ops = &sun4i_usb_phy_ops,
458 .probe = sun4i_usb_phy_probe,
459 .platdata_auto_alloc_size = sizeof(struct sun4i_usb_phy_plat[MAX_PHYS]),
460 .priv_auto_alloc_size = sizeof(struct sun4i_usb_phy_data),
461};