1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
/* $OpenBSD: xhci_fdt.c,v 1.15 2019/08/29 20:18:11 patrick Exp $ */
/*
* Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/ofw_power.h>
#include <dev/ofw/ofw_regulator.h>
#include <dev/ofw/fdt.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/xhcireg.h>
#include <dev/usb/xhcivar.h>
struct xhci_fdt_softc {
struct xhci_softc sc;
int sc_node;
bus_space_handle_t ph_ioh;
void *sc_ih;
};
int xhci_fdt_match(struct device *, void *, void *);
void xhci_fdt_attach(struct device *, struct device *, void *);
struct cfattach xhci_fdt_ca = {
sizeof(struct xhci_fdt_softc), xhci_fdt_match, xhci_fdt_attach
};
void xhci_dwc3_init(struct xhci_fdt_softc *);
void xhci_init_phys(struct xhci_fdt_softc *);
int
xhci_fdt_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "generic-xhci") ||
OF_is_compatible(faa->fa_node, "cavium,octeon-7130-xhci") ||
OF_is_compatible(faa->fa_node, "snps,dwc3");
}
void
xhci_fdt_attach(struct device *parent, struct device *self, void *aux)
{
struct xhci_fdt_softc *sc = (struct xhci_fdt_softc *)self;
struct fdt_attach_args *faa = aux;
int error;
if (faa->fa_nreg < 1) {
printf(": no registers\n");
return;
}
sc->sc_node = faa->fa_node;
sc->sc.iot = faa->fa_iot;
sc->sc.sc_size = faa->fa_reg[0].size;
sc->sc.sc_bus.dmatag = faa->fa_dmat;
if (bus_space_map(sc->sc.iot, faa->fa_reg[0].addr,
faa->fa_reg[0].size, 0, &sc->sc.ioh)) {
printf(": can't map registers\n");
return;
}
sc->sc_ih = fdt_intr_establish(faa->fa_node, IPL_USB,
xhci_intr, sc, sc->sc.sc_bus.bdev.dv_xname);
if (sc->sc_ih == NULL) {
printf(": can't establish interrupt\n");
goto unmap;
}
/* Set up power and clocks */
power_domain_enable(sc->sc_node);
clock_set_assigned(sc->sc_node);
clock_enable_all(sc->sc_node);
/*
* Synopsys Designware USB3 controller needs some extra
* attention because of the additional OTG functionality.
*/
if (OF_is_compatible(sc->sc_node, "snps,dwc3"))
xhci_dwc3_init(sc);
xhci_init_phys(sc);
strlcpy(sc->sc.sc_vendor, "Generic", sizeof(sc->sc.sc_vendor));
if ((error = xhci_init(&sc->sc)) != 0) {
printf("%s: init failed, error=%d\n",
sc->sc.sc_bus.bdev.dv_xname, error);
goto disestablish_ret;
}
/* Attach usb device. */
config_found(self, &sc->sc.sc_bus, usbctlprint);
/* Now that the stack is ready, config' the HC and enable interrupts. */
xhci_config(&sc->sc);
return;
disestablish_ret:
fdt_intr_disestablish(sc->sc_ih);
unmap:
bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
}
/*
* Synopsys Designware USB3 controller.
*/
#define USB3_GCTL 0xc110
#define USB3_GCTL_PRTCAPDIR_MASK (0x3 << 12)
#define USB3_GCTL_PRTCAPDIR_HOST (0x1 << 12)
#define USB3_GCTL_PRTCAPDIR_DEVICE (0x2 << 12)
#define USB3_GUCTL1 0xc11c
#define USB3_GUCTL1_TX_IPGAP_LINECHECK_DIS (1 << 28)
#define USB3_GUSB2PHYCFG0 0xc200
#define USB3_GUSB2PHYCFG0_U2_FREECLK_EXISTS (1 << 30)
#define USB3_GUSB2PHYCFG0_USBTRDTIM(n) ((n) << 10)
#define USB3_GUSB2PHYCFG0_ENBLSLPM (1 << 8)
#define USB3_GUSB2PHYCFG0_SUSPENDUSB20 (1 << 6)
#define USB3_GUSB2PHYCFG0_PHYIF (1 << 3)
void
xhci_dwc3_init(struct xhci_fdt_softc *sc)
{
char phy_type[16] = { 0 };
int node = sc->sc_node;
uint32_t reg;
/* We don't support device mode, so always force host mode. */
reg = bus_space_read_4(sc->sc.iot, sc->sc.ioh, USB3_GCTL);
reg &= ~USB3_GCTL_PRTCAPDIR_MASK;
reg |= USB3_GCTL_PRTCAPDIR_HOST;
bus_space_write_4(sc->sc.iot, sc->sc.ioh, USB3_GCTL, reg);
/* Configure USB2 PHY type and quirks. */
OF_getprop(node, "phy_type", phy_type, sizeof(phy_type));
reg = bus_space_read_4(sc->sc.iot, sc->sc.ioh, USB3_GUSB2PHYCFG0);
reg &= ~USB3_GUSB2PHYCFG0_USBTRDTIM(0xf);
if (strcmp(phy_type, "utmi_wide") == 0) {
reg |= USB3_GUSB2PHYCFG0_PHYIF;
reg |= USB3_GUSB2PHYCFG0_USBTRDTIM(0x5);
} else {
reg &= ~USB3_GUSB2PHYCFG0_PHYIF;
reg |= USB3_GUSB2PHYCFG0_USBTRDTIM(0x9);
}
if (OF_getproplen(node, "snps,dis-u2-freeclk-exists-quirk") == 0)
reg &= ~USB3_GUSB2PHYCFG0_U2_FREECLK_EXISTS;
if (OF_getproplen(node, "snps,dis_enblslpm_quirk") == 0)
reg &= ~USB3_GUSB2PHYCFG0_ENBLSLPM;
if (OF_getproplen(node, "snps,dis_u2_susphy_quirk") == 0)
reg &= ~USB3_GUSB2PHYCFG0_SUSPENDUSB20;
bus_space_write_4(sc->sc.iot, sc->sc.ioh, USB3_GUSB2PHYCFG0, reg);
/* Configure USB3 quirks. */
reg = bus_space_read_4(sc->sc.iot, sc->sc.ioh, USB3_GUCTL1);
if (OF_getproplen(node, "snps,dis-tx-ipgap-linecheck-quirk") == 0)
reg |= USB3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
bus_space_write_4(sc->sc.iot, sc->sc.ioh, USB3_GUCTL1, reg);
}
/*
* PHY initialization.
*/
struct xhci_phy {
const char *compat;
void (*init)(struct xhci_fdt_softc *, uint32_t *);
};
void exynos5_usbdrd_init(struct xhci_fdt_softc *, uint32_t *);
void imx8mq_usb_init(struct xhci_fdt_softc *, uint32_t *);
void nop_xceiv_init(struct xhci_fdt_softc *, uint32_t *);
struct xhci_phy xhci_phys[] = {
{ "fsl,imx8mq-usb-phy", imx8mq_usb_init },
{ "samsung,exynos5250-usbdrd-phy", exynos5_usbdrd_init },
{ "samsung,exynos5420-usbdrd-phy", exynos5_usbdrd_init },
{ "usb-nop-xceiv", nop_xceiv_init },
};
uint32_t *
xhci_next_phy(uint32_t *cells)
{
uint32_t phandle = cells[0];
int node, ncells;
node = OF_getnodebyphandle(phandle);
if (node == 0)
return NULL;
ncells = OF_getpropint(node, "#phy-cells", 0);
return cells + ncells + 1;
}
void
xhci_init_phy(struct xhci_fdt_softc *sc, uint32_t *cells)
{
int node;
int i;
node = OF_getnodebyphandle(cells[0]);
if (node == 0)
return;
for (i = 0; i < nitems(xhci_phys); i++) {
if (OF_is_compatible(node, xhci_phys[i].compat)) {
xhci_phys[i].init(sc, cells);
return;
}
}
}
void
xhci_init_phys(struct xhci_fdt_softc *sc)
{
uint32_t *phys;
uint32_t *phy;
uint32_t usb_phy;
int len, idx;
/*
* Legacy binding; assume there only is a single USB PHY.
*/
usb_phy = OF_getpropint(sc->sc_node, "usb-phy", 0);
if (usb_phy) {
xhci_init_phy(sc, &usb_phy);
return;
}
/*
* Generic PHY binding; only initialize USB 3 PHY for now.
*/
idx = OF_getindex(sc->sc_node, "usb3-phy", "phy-names");
if (idx < 0)
return;
len = OF_getproplen(sc->sc_node, "phys");
if (len <= 0)
return;
phys = malloc(len, M_TEMP, M_WAITOK);
OF_getpropintarray(sc->sc_node, "phys", phys, len);
phy = phys;
while (phy && phy < phys + (len / sizeof(uint32_t))) {
if (idx == 0) {
xhci_init_phy(sc, phy);
free(phys, M_TEMP, len);
return;
}
phy = xhci_next_phy(phy);
idx--;
}
free(phys, M_TEMP, len);
}
/*
* Samsung Exynos 5 PHYs.
*/
/* Registers */
#define EXYNOS5_PHYUTMI 0x0008
#define EXYNOS5_PHYUTMI_OTGDISABLE (1 << 6)
#define EXYNOS5_PHYCLKRST 0x0010
#define EXYNOS5_PHYCLKRST_SSC_EN (1 << 20)
#define EXYNOS5_PHYCLKRST_REF_SSP_EN (1 << 19)
#define EXYNOS5_PHYCLKRST_PORTRESET (1 << 1)
#define EXYNOS5_PHYCLKRST_COMMONONN (1 << 0)
#define EXYNOS5_PHYTEST 0x0028
#define EXYNOS5_PHYTEST_POWERDOWN_SSP (1 << 3)
#define EXYNOS5_PHYTEST_POWERDOWN_HSP (1 << 2)
/* PMU registers */
#define EXYNOS5_USBDRD0_POWER 0x0704
#define EXYNOS5420_USBDRD1_POWER 0x0708
#define EXYNOS5_USBDRD_POWER_EN (1 << 0)
void
exynos5_usbdrd_init(struct xhci_fdt_softc *sc, uint32_t *cells)
{
uint32_t phy_reg[2];
struct regmap *pmurm;
uint32_t pmureg;
uint32_t val;
bus_size_t offset;
int node;
node = OF_getnodebyphandle(cells[0]);
KASSERT(node != 0);
if (OF_getpropintarray(node, "reg", phy_reg,
sizeof(phy_reg)) != sizeof(phy_reg))
return;
if (bus_space_map(sc->sc.iot, phy_reg[0],
phy_reg[1], 0, &sc->ph_ioh)) {
printf("%s: can't map PHY registers\n",
sc->sc.sc_bus.bdev.dv_xname);
return;
}
/* Power up the PHY block. */
pmureg = OF_getpropint(node, "samsung,pmu-syscon", 0);
pmurm = regmap_byphandle(pmureg);
if (pmurm) {
node = OF_getnodebyphandle(pmureg);
if (sc->sc.sc_bus.bdev.dv_unit == 0)
offset = EXYNOS5_USBDRD0_POWER;
else
offset = EXYNOS5420_USBDRD1_POWER;
val = regmap_read_4(pmurm, offset);
val |= EXYNOS5_USBDRD_POWER_EN;
regmap_write_4(pmurm, offset, val);
}
/* Initialize the PHY. Assumes U-Boot has done initial setup. */
val = bus_space_read_4(sc->sc.iot, sc->ph_ioh, EXYNOS5_PHYTEST);
CLR(val, EXYNOS5_PHYTEST_POWERDOWN_SSP);
CLR(val, EXYNOS5_PHYTEST_POWERDOWN_HSP);
bus_space_write_4(sc->sc.iot, sc->ph_ioh, EXYNOS5_PHYTEST, val);
bus_space_write_4(sc->sc.iot, sc->ph_ioh, EXYNOS5_PHYUTMI,
EXYNOS5_PHYUTMI_OTGDISABLE);
val = bus_space_read_4(sc->sc.iot, sc->ph_ioh, EXYNOS5_PHYCLKRST);
SET(val, EXYNOS5_PHYCLKRST_SSC_EN);
SET(val, EXYNOS5_PHYCLKRST_REF_SSP_EN);
SET(val, EXYNOS5_PHYCLKRST_COMMONONN);
SET(val, EXYNOS5_PHYCLKRST_PORTRESET);
bus_space_write_4(sc->sc.iot, sc->ph_ioh, EXYNOS5_PHYCLKRST, val);
delay(10);
CLR(val, EXYNOS5_PHYCLKRST_PORTRESET);
bus_space_write_4(sc->sc.iot, sc->ph_ioh, EXYNOS5_PHYCLKRST, val);
}
/*
* i.MX8MQ PHYs.
*/
/* Registers */
#define IMX8MQ_PHY_CTRL0 0x0000
#define IMX8MQ_PHY_CTRL0_REF_SSP_EN (1 << 2)
#define IMX8MQ_PHY_CTRL1 0x0004
#define IMX8MQ_PHY_CTRL1_RESET (1 << 0)
#define IMX8MQ_PHY_CTRL1_ATERESET (1 << 3)
#define IMX8MQ_PHY_CTRL1_VDATSRCENB0 (1 << 19)
#define IMX8MQ_PHY_CTRL1_VDATDETENB0 (1 << 20)
#define IMX8MQ_PHY_CTRL2 0x0008
#define IMX8MQ_PHY_CTRL2_TXENABLEN0 (1 << 8)
#define IMX8MQ_PHY_CTRL3 0x000c
void
imx8mq_usb_init(struct xhci_fdt_softc *sc, uint32_t *cells)
{
uint32_t phy_reg[2], reg;
int node, vbus_supply;
node = OF_getnodebyphandle(cells[0]);
KASSERT(node != 0);
if (OF_getpropintarray(node, "reg", phy_reg,
sizeof(phy_reg)) != sizeof(phy_reg))
return;
if (bus_space_map(sc->sc.iot, phy_reg[0],
phy_reg[1], 0, &sc->ph_ioh)) {
printf("%s: can't map PHY registers\n",
sc->sc.sc_bus.bdev.dv_xname);
return;
}
clock_set_assigned(node);
clock_enable_all(node);
reg = bus_space_read_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL1);
reg &= ~(IMX8MQ_PHY_CTRL1_VDATSRCENB0 | IMX8MQ_PHY_CTRL1_VDATDETENB0);
reg |= IMX8MQ_PHY_CTRL1_RESET | IMX8MQ_PHY_CTRL1_ATERESET;
bus_space_write_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL1, reg);
reg = bus_space_read_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL0);
reg |= IMX8MQ_PHY_CTRL0_REF_SSP_EN;
bus_space_write_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL0, reg);
reg = bus_space_read_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL2);
reg |= IMX8MQ_PHY_CTRL2_TXENABLEN0;
bus_space_write_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL2, reg);
reg = bus_space_read_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL1);
reg &= ~(IMX8MQ_PHY_CTRL1_RESET | IMX8MQ_PHY_CTRL1_ATERESET);
bus_space_write_4(sc->sc.iot, sc->ph_ioh, IMX8MQ_PHY_CTRL1, reg);
vbus_supply = OF_getpropint(node, "vbus-supply", 0);
if (vbus_supply)
regulator_enable(vbus_supply);
}
void
nop_xceiv_init(struct xhci_fdt_softc *sc, uint32_t *cells)
{
uint32_t vcc_supply;
int node;
node = OF_getnodebyphandle(cells[0]);
KASSERT(node != 0);
vcc_supply = OF_getpropint(node, "vcc-supply", 0);
if (vcc_supply)
regulator_enable(vcc_supply);
}
|