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