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
|
/* $OpenBSD: octuctl.c,v 1.1 2016/03/18 05:38:10 jmatthew Exp $ */
/*
* Copyright (c) 2015 Jonathan Matthew <jmatthew@openbsd.org>
*
* Permission to use, copy, modify, and/or 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/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <machine/intr.h>
#include <machine/bus.h>
#include <machine/octeonreg.h>
#include <machine/octeonvar.h>
#include <machine/octeon_model.h>
#include <octeon/dev/iobusvar.h>
#include <octeon/dev/octuctlreg.h>
#include <octeon/dev/octuctlvar.h>
struct octuctl_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
};
int octuctl_match(struct device *, void *, void *);
void octuctl_attach(struct device *, struct device *, void *);
const struct cfattach octuctl_ca = {
sizeof(struct octuctl_softc), octuctl_match, octuctl_attach,
};
struct cfdriver octuctl_cd = {
NULL, "octuctl", DV_DULL
};
uint8_t octuctl_read_1(bus_space_tag_t, bus_space_handle_t, bus_size_t);
uint16_t octuctl_read_2(bus_space_tag_t, bus_space_handle_t, bus_size_t);
uint32_t octuctl_read_4(bus_space_tag_t, bus_space_handle_t, bus_size_t);
void octuctl_write_1(bus_space_tag_t, bus_space_handle_t, bus_size_t, uint8_t);
void octuctl_write_2(bus_space_tag_t, bus_space_handle_t, bus_size_t, uint16_t);
void octuctl_write_4(bus_space_tag_t, bus_space_handle_t, bus_size_t, uint32_t);
bus_space_t octuctl_tag = {
.bus_base = PHYS_TO_XKPHYS(0, CCA_NC),
.bus_private = NULL,
._space_read_1 = octuctl_read_1,
._space_write_1 = octuctl_write_1,
._space_read_2 = octuctl_read_2,
._space_write_2 = octuctl_write_2,
._space_read_4 = octuctl_read_4,
._space_write_4 = octuctl_write_4,
._space_map = iobus_space_map,
._space_unmap = iobus_space_unmap,
._space_subregion = generic_space_region,
._space_vaddr = generic_space_vaddr
};
uint8_t
octuctl_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
{
return *(volatile uint8_t *)(h + (o^3));
}
uint16_t
octuctl_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
{
return *(volatile uint16_t *)(h + (o^2));
}
uint32_t
octuctl_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
{
return *(volatile uint32_t *)(h + o);
}
void
octuctl_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, uint8_t v)
{
*(volatile uint8_t *)(h + (o^3)) = v;
}
void
octuctl_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, uint16_t v)
{
*(volatile uint16_t *)(h + (o^2)) = v;
}
void
octuctl_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, uint32_t v)
{
*(volatile uint32_t *)(h + o) = v;
}
int
octuctl_match(struct device *parent, void *match, void *aux)
{
int id;
id = octeon_get_chipid();
switch (octeon_model_family(id)) {
case OCTEON_MODEL_FAMILY_CN61XX:
return (1);
default:
return (0);
}
}
int
octuctlprint(void *aux, const char *parentname)
{
return (QUIET);
}
void
octuctl_clock_setup(struct octuctl_softc *sc, uint64_t ctl)
{
int div;
int lastdiv;
int validdiv[] = { 1, 2, 3, 4, 6, 8, 12, INT_MAX };
int i;
div = octeon_ioclock_speed() / UCTL_CLK_TARGET_FREQ;
/* start usb controller reset */
ctl |= UCTL_CLK_RST_CTL_P_POR;
ctl &= ~(UCTL_CLK_RST_CTL_HRST |
UCTL_CLK_RST_CTL_P_PRST |
UCTL_CLK_RST_CTL_O_CLKDIV_EN |
UCTL_CLK_RST_CTL_H_CLKDIV_EN |
UCTL_CLK_RST_CTL_H_CLKDIV_RST |
UCTL_CLK_RST_CTL_O_CLKDIV_RST);
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
/* set up for 12mhz crystal */
ctl &= ~((3 << UCTL_CLK_RST_CTL_P_REFCLK_DIV_SHIFT) |
(3 << UCTL_CLK_RST_CTL_P_REFCLK_SEL_SHIFT));
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
/* set clock divider */
lastdiv = 1;
for (i = 0; i < nitems(validdiv); i++) {
if (div < validdiv[i]) {
div = lastdiv;
break;
}
lastdiv = validdiv[i];
}
ctl &= ~(0xf << UCTL_CLK_RST_CTL_H_DIV_SHIFT);
ctl |= (div << UCTL_CLK_RST_CTL_H_DIV_SHIFT);
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
/* turn hclk on */
ctl = bus_space_read_8(sc->sc_iot, sc->sc_ioh,
UCTL_CLK_RST_CTL);
ctl |= UCTL_CLK_RST_CTL_H_CLKDIV_EN;
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
ctl |= UCTL_CLK_RST_CTL_H_CLKDIV_RST;
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
delay(1);
/* power-on-reset finished */
ctl &= ~UCTL_CLK_RST_CTL_P_POR;
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
delay(1000);
/* set up ohci clocks */
ctl |= UCTL_CLK_RST_CTL_O_CLKDIV_RST;
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
ctl |= UCTL_CLK_RST_CTL_O_CLKDIV_EN;
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
delay(1);
/* phy reset */
ctl |= UCTL_CLK_RST_CTL_P_PRST;
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
delay(1);
/* clear host reset */
ctl |= UCTL_CLK_RST_CTL_HRST;
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL, ctl);
}
void
octuctl_attach(struct device *parent, struct device *self, void *aux)
{
struct octuctl_softc *sc = (struct octuctl_softc *)self;
struct iobus_attach_args *aa = aux;
struct octuctl_attach_args uaa;
uint64_t port_ctl;
uint64_t ctl;
uint64_t preg;
uint64_t txvref;
int rc;
int port;
sc->sc_iot = aa->aa_bust;
rc = bus_space_map(sc->sc_iot, UCTL_BASE, UCTL_SIZE,
0, &sc->sc_ioh);
KASSERT(rc == 0);
/* do clock setup if not already done */
bus_space_write_8(sc->sc_iot, sc->sc_ioh, UCTL_IF_ENA,
UCTL_IF_ENA_EN);
ctl = bus_space_read_8(sc->sc_iot, sc->sc_ioh, UCTL_CLK_RST_CTL);
if ((ctl & UCTL_CLK_RST_CTL_HRST) == 0)
octuctl_clock_setup(sc, ctl);
/* port phy settings */
for (port = 0; port < 2; port++) {
preg = UCTL_UPHY_PORTX_STATUS + (port * 8);
port_ctl = bus_space_read_8(sc->sc_iot, sc->sc_ioh, preg);
txvref = 0xf;
port_ctl |= (UCTL_UPHY_PORTX_STATUS_TXPREEMPHTUNE |
UCTL_UPHY_PORTX_STATUS_TXRISETUNE |
(txvref << UCTL_UPHY_PORTX_STATUS_TXVREF_SHIFT));
bus_space_write_8(sc->sc_iot, sc->sc_ioh, preg, port_ctl);
}
printf("\n");
uaa.aa_octuctl_bust = aa->aa_bust;
uaa.aa_bust = &octuctl_tag;
uaa.aa_dmat = aa->aa_dmat;
uaa.aa_ioh = sc->sc_ioh;
uaa.aa_name = "ehci";
config_found(self, &uaa, octuctlprint);
uaa.aa_name = "ohci";
config_found(self, &uaa, octuctlprint);
}
|