bcu_vrip.c revision 1.12 1 1.12 uch /* $NetBSD: bcu_vrip.c,v 1.12 2001/09/16 05:32:20 uch Exp $ */
2 1.1 takemura
3 1.1 takemura /*-
4 1.7 sato * Copyright (c) 1999-2001 SATO Kazumi. All rights reserved.
5 1.1 takemura * Copyright (c) 1999 PocketBSD Project. All rights reserved.
6 1.1 takemura *
7 1.1 takemura * Redistribution and use in source and binary forms, with or without
8 1.1 takemura * modification, are permitted provided that the following conditions
9 1.1 takemura * are met:
10 1.1 takemura * 1. Redistributions of source code must retain the above copyright
11 1.1 takemura * notice, this list of conditions and the following disclaimer.
12 1.1 takemura * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 takemura * notice, this list of conditions and the following disclaimer in the
14 1.1 takemura * documentation and/or other materials provided with the distribution.
15 1.1 takemura * 3. All advertising materials mentioning features or use of this software
16 1.1 takemura * must display the following acknowledgement:
17 1.1 takemura * This product includes software developed by the PocketBSD project
18 1.1 takemura * and its contributors.
19 1.1 takemura * 4. Neither the name of the project nor the names of its contributors
20 1.1 takemura * may be used to endorse or promote products derived from this software
21 1.1 takemura * without specific prior written permission.
22 1.1 takemura *
23 1.1 takemura * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 takemura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 takemura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 takemura * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 takemura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 takemura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 takemura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 takemura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 takemura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 takemura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 takemura * SUCH DAMAGE.
34 1.1 takemura *
35 1.1 takemura */
36 1.1 takemura
37 1.1 takemura #include <sys/param.h>
38 1.1 takemura #include <sys/systm.h>
39 1.1 takemura #include <sys/device.h>
40 1.1 takemura #include <sys/reboot.h>
41 1.1 takemura
42 1.1 takemura #include <machine/bus.h>
43 1.1 takemura
44 1.1 takemura #include <mips/cpuregs.h>
45 1.1 takemura
46 1.7 sato #include "opt_vr41xx.h"
47 1.1 takemura #include <hpcmips/vr/vr.h>
48 1.7 sato #include <hpcmips/vr/vrcpudef.h>
49 1.1 takemura #include <hpcmips/vr/vripvar.h>
50 1.1 takemura #include <hpcmips/vr/vripreg.h>
51 1.1 takemura #include <hpcmips/vr/bcureg.h>
52 1.1 takemura #include <hpcmips/vr/bcuvar.h>
53 1.1 takemura
54 1.12 uch static int vrbcu_match(struct device *, struct cfdata *, void *);
55 1.12 uch static void vrbcu_attach(struct device *, struct device *, void *);
56 1.1 takemura
57 1.12 uch static void vrbcu_write(struct vrbcu_softc *, int, unsigned short);
58 1.12 uch static unsigned short vrbcu_read(struct vrbcu_softc *, int);
59 1.2 sato
60 1.12 uch static void vrbcu_dump_regs(void);
61 1.5 sato
62 1.2 sato char *vr_cpuname=NULL;
63 1.2 sato int vr_major=-1;
64 1.2 sato int vr_minor=-1;
65 1.2 sato int vr_cpuid=-1;
66 1.1 takemura
67 1.1 takemura struct cfattach vrbcu_ca = {
68 1.3 sato sizeof(struct vrbcu_softc), vrbcu_match, vrbcu_attach
69 1.1 takemura };
70 1.1 takemura
71 1.3 sato struct vrbcu_softc *the_bcu_sc = NULL;
72 1.3 sato
73 1.3 sato static inline void
74 1.12 uch vrbcu_write(struct vrbcu_softc *sc, int port, unsigned short val)
75 1.3 sato {
76 1.12 uch
77 1.3 sato bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
78 1.3 sato }
79 1.3 sato
80 1.3 sato static inline unsigned short
81 1.12 uch vrbcu_read(struct vrbcu_softc *sc, int port)
82 1.3 sato {
83 1.12 uch
84 1.12 uch return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
85 1.3 sato }
86 1.3 sato
87 1.1 takemura static int
88 1.12 uch vrbcu_match(struct device *parent, struct cfdata *cf, void *aux)
89 1.1 takemura {
90 1.12 uch
91 1.12 uch return (2);
92 1.1 takemura }
93 1.1 takemura
94 1.1 takemura static void
95 1.12 uch vrbcu_attach(struct device *parent, struct device *self, void *aux)
96 1.1 takemura {
97 1.1 takemura struct vrip_attach_args *va = aux;
98 1.3 sato struct vrbcu_softc *sc = (struct vrbcu_softc *)self;
99 1.1 takemura
100 1.3 sato sc->sc_iot = va->va_iot;
101 1.3 sato bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
102 1.12 uch 0, /* no flags */
103 1.12 uch &sc->sc_ioh);
104 1.1 takemura
105 1.1 takemura printf("\n");
106 1.3 sato the_bcu_sc = sc;
107 1.5 sato vrbcu_dump_regs();
108 1.5 sato }
109 1.5 sato
110 1.5 sato static void
111 1.5 sato vrbcu_dump_regs()
112 1.5 sato {
113 1.5 sato struct vrbcu_softc *sc = the_bcu_sc;
114 1.7 sato int cpuclock = 0, tclock = 0, vtclock = 0, cpuid;
115 1.8 sato #if !defined(ONLY_VR4102)
116 1.7 sato int spdreg;
117 1.7 sato #endif
118 1.7 sato #ifdef VRBCUDEBUG
119 1.5 sato int reg;
120 1.7 sato #endif /* VRBCUDEBUG */
121 1.5 sato
122 1.7 sato cpuid = vrbcu_vrip_getcpuid();
123 1.8 sato #if !defined(ONLY_VR4181) && !defined(ONLY_VR4102)
124 1.7 sato if (cpuid != BCUREVID_FIXRID_4181
125 1.12 uch && cpuid <= BCUREVID_RID_4131
126 1.12 uch && cpuid >= BCUREVID_RID_4111) {
127 1.7 sato spdreg = vrbcu_read(sc, BCUCLKSPEED_REG_W);
128 1.5 sato #ifdef VRBCUDEBUG
129 1.7 sato printf("vrbcu: CLKSPEED %x: \n", spdreg);
130 1.7 sato #endif /* VRBCUDEBUG */
131 1.7 sato }
132 1.7 sato #endif
133 1.8 sato #if defined VR4181
134 1.8 sato if (cpuid == BCUREVID_FIXRID_4181){
135 1.8 sato spdreg = vrbcu_read(sc, BCU81CLKSPEED_REG_W);
136 1.8 sato #ifdef VRBCUDEBUG
137 1.8 sato printf("vrbcu: CLKSPEED %x: \n", spdreg);
138 1.8 sato #endif /* VRBCUDEBUG */
139 1.8 sato }
140 1.8 sato #endif
141 1.7 sato
142 1.5 sato cpuclock = vrbcu_vrip_getcpuclock();
143 1.5 sato
144 1.5 sato switch (cpuid) {
145 1.8 sato #if defined VR4181
146 1.7 sato case BCUREVID_FIXRID_4181:
147 1.12 uch switch ((spdreg & BCU81CLKSPEED_DIVTMASK) >>
148 1.12 uch BCU81CLKSPEED_DIVTSHFT){
149 1.8 sato case BCU81CLKSPEED_DIVT1:
150 1.8 sato vtclock = tclock = cpuclock;
151 1.8 sato break;
152 1.8 sato case BCU81CLKSPEED_DIVT2:
153 1.8 sato vtclock = tclock = cpuclock/2;
154 1.8 sato break;
155 1.8 sato case BCU81CLKSPEED_DIVT3:
156 1.8 sato vtclock = tclock = cpuclock/3;
157 1.8 sato break;
158 1.8 sato case BCU81CLKSPEED_DIVT4:
159 1.8 sato vtclock = tclock = cpuclock/4;
160 1.8 sato break;
161 1.8 sato default:
162 1.8 sato vtclock = tclock = 0;
163 1.8 sato }
164 1.8 sato break;
165 1.8 sato #endif /* VR4181 */
166 1.5 sato case BCUREVID_RID_4101:
167 1.5 sato case BCUREVID_RID_4102:
168 1.5 sato vtclock = tclock = cpuclock/2;
169 1.5 sato break;
170 1.7 sato #if defined VR4111
171 1.5 sato case BCUREVID_RID_4111:
172 1.7 sato if ((spdreg&BCUCLKSPEED_DIVT2B) == 0)
173 1.5 sato vtclock = tclock = cpuclock/2;
174 1.7 sato else if ((spdreg&BCUCLKSPEED_DIVT3B) == 0)
175 1.5 sato vtclock = tclock = cpuclock/3;
176 1.7 sato else if ((spdreg&BCUCLKSPEED_DIVT4B) == 0)
177 1.5 sato vtclock = tclock = cpuclock/4;
178 1.5 sato else
179 1.5 sato vtclock = tclock = 0; /* XXX */
180 1.5 sato break;
181 1.7 sato #endif /* VR4111 */
182 1.7 sato #if defined VR4121
183 1.5 sato case BCUREVID_RID_4121:
184 1.12 uch {
185 1.12 uch int vt;
186 1.12 uch tclock = cpuclock / ((spdreg & BCUCLKSPEED_DIVTMASK) >>
187 1.12 uch BCUCLKSPEED_DIVTSHFT);
188 1.12 uch vt = ((spdreg & BCUCLKSPEED_DIVVTMASK) >>
189 1.12 uch BCUCLKSPEED_DIVVTSHFT);
190 1.12 uch if (vt == 0)
191 1.12 uch vtclock = 0; /* XXX */
192 1.12 uch else if (vt < 0x9)
193 1.12 uch vtclock = cpuclock / vt;
194 1.12 uch else
195 1.12 uch vtclock = cpuclock / ((vt - 8)*2+1) * 2;
196 1.12 uch }
197 1.12 uch break;
198 1.7 sato #endif /* VR4121 */
199 1.11 sato #if defined VR4122 || defined VR4131
200 1.9 enami case BCUREVID_RID_4122:
201 1.11 sato case BCUREVID_RID_4131:
202 1.12 uch {
203 1.12 uch int vtdiv;
204 1.9 enami
205 1.12 uch vtdiv = ((spdreg & BCUCLKSPEED_VTDIVMODE) >>
206 1.12 uch BCUCLKSPEED_VTDIVSHFT);
207 1.12 uch if (vtdiv == 0 || vtdiv > BCUCLKSPEED_VTDIV6)
208 1.12 uch vtclock = 0; /* XXX */
209 1.12 uch else
210 1.12 uch vtclock = cpuclock / vtdiv;
211 1.12 uch tclock = vtclock /
212 1.12 uch (((spdreg & BCUCLKSPEED_TDIVMODE) >>
213 1.12 uch BCUCLKSPEED_TDIVSHFT) ? 4 : 2);
214 1.12 uch }
215 1.12 uch break;
216 1.11 sato #endif /* VR4122 || VR4131 */
217 1.5 sato default:
218 1.5 sato break;
219 1.5 sato }
220 1.7 sato if (tclock)
221 1.7 sato printf("%s: cpu %d.%03dMHz, bus %d.%03dMHz, ram %d.%03dMHz\n",
222 1.12 uch sc->sc_dev.dv_xname,
223 1.12 uch cpuclock/1000000, (cpuclock%1000000)/1000,
224 1.12 uch tclock/1000000, (tclock%1000000)/1000,
225 1.12 uch vtclock/1000000, (vtclock%1000000)/1000);
226 1.7 sato else {
227 1.7 sato printf("%s: cpu %d.%03dMHz\n",
228 1.12 uch sc->sc_dev.dv_xname,
229 1.12 uch cpuclock/1000000, (cpuclock%1000000)/1000);
230 1.12 uch printf("%s: UNKNOWN BUS CLOCK SPEED:"
231 1.12 uch " CPU is UNKNOWN or NOT CONFIGURED\n",
232 1.12 uch sc->sc_dev.dv_xname);
233 1.7 sato }
234 1.5 sato #ifdef VRBCUDEBUG
235 1.7 sato reg = vrbcu_read(sc, BCUCNT1_REG_W);
236 1.7 sato printf("vrbcu: CNT1 %x: ", reg);
237 1.7 sato bitdisp16(reg);
238 1.7 sato #if !defined(ONLY_VR4181)
239 1.7 sato if (cpuid != BCUREVID_FIXRID_4181
240 1.12 uch && cpuid <= BCUREVID_RID_4121
241 1.12 uch && cpuid >= BCUREVID_RID_4102) {
242 1.7 sato reg = vrbcu_read(sc, BCUCNT2_REG_W);
243 1.7 sato printf("vrbcu: CNT2 %x: ", reg);
244 1.7 sato bitdisp16(reg);
245 1.7 sato }
246 1.7 sato #endif /* !defined ONLY_VR4181 */
247 1.11 sato #if !defined(ONLY_VR4181) || !defined(ONLY_VR4122_4131)
248 1.7 sato if (cpuid != BCUREVID_FIXRID_4181
249 1.12 uch && cpuid <= BCUREVID_RID_4121
250 1.12 uch && cpuid >= BCUREVID_RID_4102) {
251 1.7 sato reg = vrbcu_read(sc, BCUSPEED_REG_W);
252 1.7 sato printf("vrbcu: SPEED %x: ", reg);
253 1.7 sato bitdisp16(reg);
254 1.7 sato reg = vrbcu_read(sc, BCUERRST_REG_W);
255 1.7 sato printf("vrbcu: ERRST %x: ", reg);
256 1.7 sato bitdisp16(reg);
257 1.7 sato reg = vrbcu_read(sc, BCURFCNT_REG_W);
258 1.7 sato printf("vrbcu: RFCNT %x\n", reg);
259 1.7 sato reg = vrbcu_read(sc, BCUREFCOUNT_REG_W);
260 1.7 sato printf("vrbcu: RFCOUNT %x\n", reg);
261 1.7 sato }
262 1.11 sato #endif /* !defined(ONLY_VR4181) || !defined(ONLY_VR4122_4131) */
263 1.7 sato #if !defined(ONLY_VR4181)
264 1.7 sato if (cpuid != BCUREVID_FIXRID_4181
265 1.12 uch && cpuid <= BCUREVID_RID_4131
266 1.12 uch && cpuid >= BCUREVID_RID_4111)
267 1.7 sato {
268 1.5 sato reg = vrbcu_read(sc, BCUCNT3_REG_W);
269 1.5 sato printf("vrbcu: CNT3 %x: ", reg);
270 1.5 sato bitdisp16(reg);
271 1.5 sato }
272 1.7 sato #endif /* !defined ONLY_VR4181 */
273 1.5 sato #endif /* VRBCUDEBUG */
274 1.5 sato
275 1.1 takemura }
276 1.1 takemura
277 1.1 takemura static char *cpuname[] = {
278 1.7 sato "VR4101", /* 0 */
279 1.7 sato "VR4102", /* 1 */
280 1.7 sato "VR4111", /* 2 */
281 1.7 sato "VR4121", /* 3 */
282 1.7 sato "VR4122", /* 4 */
283 1.11 sato "VR4131", /* 5 */
284 1.7 sato "UNKNOWN",
285 1.7 sato "UNKNOWN",
286 1.1 takemura "UNKNOWN",
287 1.1 takemura "UNKNOWN",
288 1.1 takemura "UNKNOWN",
289 1.7 sato "UNKNOWN",
290 1.7 sato "UNKNOWN",
291 1.7 sato "UNKNOWN",
292 1.7 sato "UNKNOWN",
293 1.7 sato "UNKNOWN",
294 1.7 sato "VR4181", /* 0x10 + 0 */
295 1.7 sato };
296 1.1 takemura
297 1.2 sato int
298 1.2 sato vrbcu_vrip_getcpuid(void)
299 1.2 sato {
300 1.2 sato volatile u_int16_t *revreg;
301 1.2 sato
302 1.2 sato if (vr_cpuid != -1)
303 1.12 uch return (vr_cpuid);
304 1.2 sato
305 1.2 sato if (vr_cpuid == -1) {
306 1.8 sato if (VRIP_BCU_ADDR == VR4181_BCU_ADDR)
307 1.12 uch revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
308 1.12 uch ((VRIP_BCU_ADDR+BCU81REVID_REG_W));
309 1.8 sato else
310 1.12 uch revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
311 1.12 uch ((VRIP_BCU_ADDR+BCUREVID_REG_W));
312 1.2 sato
313 1.2 sato vr_cpuid = *revreg;
314 1.2 sato vr_cpuid = (vr_cpuid&BCUREVID_RIDMASK)>>BCUREVID_RIDSHFT;
315 1.8 sato if (VRIP_BCU_ADDR == VR4181_BCU_ADDR
316 1.8 sato && vr_cpuid == BCUREVID_RID_4181) /* conflict vr4101 */
317 1.7 sato vr_cpuid = BCUREVID_FIXRID_4181;
318 1.2 sato }
319 1.12 uch return (vr_cpuid);
320 1.2 sato }
321 1.2 sato
322 1.1 takemura char *
323 1.1 takemura vrbcu_vrip_getcpuname(void)
324 1.1 takemura {
325 1.2 sato int cpuid;
326 1.1 takemura
327 1.2 sato if (vr_cpuname != NULL)
328 1.12 uch return (vr_cpuname);
329 1.1 takemura
330 1.2 sato cpuid = vrbcu_vrip_getcpuid();
331 1.2 sato vr_cpuname = cpuname[cpuid];
332 1.12 uch
333 1.12 uch return (vr_cpuname);
334 1.1 takemura }
335 1.1 takemura
336 1.2 sato
337 1.1 takemura int
338 1.1 takemura vrbcu_vrip_getcpumajor(void)
339 1.1 takemura {
340 1.1 takemura volatile u_int16_t *revreg;
341 1.2 sato
342 1.2 sato if (vr_major != -1)
343 1.12 uch return (vr_major);
344 1.1 takemura
345 1.12 uch revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
346 1.12 uch ((VRIP_BCU_ADDR+BCUREVID_REG_W));
347 1.1 takemura
348 1.2 sato vr_major = *revreg;
349 1.2 sato vr_major = (vr_major&BCUREVID_MJREVMASK)>>BCUREVID_MJREVSHFT;
350 1.12 uch
351 1.12 uch return (vr_major);
352 1.1 takemura }
353 1.1 takemura
354 1.1 takemura int
355 1.1 takemura vrbcu_vrip_getcpuminor(void)
356 1.1 takemura {
357 1.1 takemura volatile u_int16_t *revreg;
358 1.2 sato
359 1.2 sato if (vr_minor != -1)
360 1.12 uch return (vr_minor);
361 1.1 takemura
362 1.12 uch revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1
363 1.12 uch ((VRIP_BCU_ADDR+BCUREVID_REG_W));
364 1.1 takemura
365 1.2 sato vr_minor = *revreg;
366 1.2 sato vr_minor = (vr_minor&BCUREVID_MNREVMASK)>>BCUREVID_MNREVSHFT;
367 1.12 uch
368 1.12 uch return (vr_minor);
369 1.1 takemura }
370 1.4 shin
371 1.4 shin #define CLKX 18432000 /* CLKX1,CLKX2: 18.432MHz */
372 1.4 shin #define MHZ 1000000
373 1.4 shin
374 1.4 shin int
375 1.4 shin vrbcu_vrip_getcpuclock(void)
376 1.4 shin {
377 1.4 shin u_int16_t clksp;
378 1.4 shin int cpuid, cpuclock;
379 1.4 shin
380 1.4 shin cpuid = vrbcu_vrip_getcpuid();
381 1.7 sato if (cpuid != BCUREVID_FIXRID_4181 && cpuid >= BCUREVID_RID_4111) {
382 1.12 uch clksp = *(u_int16_t *)MIPS_PHYS_TO_KSEG1
383 1.12 uch ((VRIP_BCU_ADDR+BCUCLKSPEED_REG_W)) &
384 1.12 uch BCUCLKSPEED_CLKSPMASK;
385 1.8 sato } else if (cpuid == BCUREVID_FIXRID_4181) {
386 1.12 uch clksp = *(u_int16_t *)MIPS_PHYS_TO_KSEG1
387 1.12 uch ((VRIP_BCU_ADDR+BCU81CLKSPEED_REG_W)) &
388 1.12 uch BCUCLKSPEED_CLKSPMASK;
389 1.7 sato }
390 1.4 shin
391 1.4 shin switch (cpuid) {
392 1.7 sato case BCUREVID_FIXRID_4181:
393 1.8 sato cpuclock = CLKX / clksp * 64;
394 1.7 sato /* branch delay is 1 clock; 2 clock/loop */
395 1.7 sato cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
396 1.7 sato break;
397 1.4 shin case BCUREVID_RID_4101:
398 1.4 shin /* assume 33MHz */
399 1.4 shin cpuclock = 33000000;
400 1.4 shin /* branch delay is 1 clock; 2 clock/loop */
401 1.4 shin cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
402 1.4 shin break;
403 1.4 shin case BCUREVID_RID_4102:
404 1.4 shin cpuclock = CLKX / clksp * 32;
405 1.4 shin /* branch delay is 1 clock; 2 clock/loop */
406 1.4 shin cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
407 1.4 shin break;
408 1.4 shin case BCUREVID_RID_4111:
409 1.4 shin cpuclock = CLKX / clksp * 64;
410 1.4 shin /* branch delay is 1 clock; 2 clock/loop */
411 1.4 shin cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
412 1.4 shin break;
413 1.4 shin case BCUREVID_RID_4121:
414 1.4 shin cpuclock = CLKX / clksp * 64;
415 1.9 enami /* branch delay is 2 clock; 3 clock/loop */
416 1.9 enami cpuspeed = (cpuclock / 3 + MHZ / 2) / MHZ;
417 1.9 enami break;
418 1.9 enami case BCUREVID_RID_4122:
419 1.11 sato cpuclock = CLKX / clksp * 98;
420 1.11 sato /* branch delay is 2 clock; 3 clock/loop */
421 1.11 sato cpuspeed = (cpuclock / 3 + MHZ / 2) / MHZ;
422 1.11 sato break;
423 1.11 sato case BCUREVID_RID_4131:
424 1.9 enami cpuclock = CLKX / clksp * 98;
425 1.4 shin /* branch delay is 2 clock; 3 clock/loop */
426 1.4 shin cpuspeed = (cpuclock / 3 + MHZ / 2) / MHZ;
427 1.4 shin break;
428 1.4 shin default:
429 1.4 shin panic("unknown CPU type %d\n", cpuid);
430 1.4 shin break;
431 1.4 shin }
432 1.12 uch
433 1.12 uch return (cpuclock);
434 1.4 shin }
435