Home | History | Annotate | Line # | Download | only in vr
bcu_vrip.c revision 1.5
      1  1.5      sato /*	$NetBSD: bcu_vrip.c,v 1.5 2000/01/27 06:25:54 sato Exp $	*/
      2  1.1  takemura 
      3  1.1  takemura /*-
      4  1.1  takemura  * Copyright (c) 1999 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.1  takemura #include <hpcmips/vr/vr.h>
     47  1.1  takemura #include <hpcmips/vr/vripvar.h>
     48  1.1  takemura #include <hpcmips/vr/vripreg.h>
     49  1.1  takemura #include <hpcmips/vr/bcureg.h>
     50  1.1  takemura #include <hpcmips/vr/bcuvar.h>
     51  1.1  takemura 
     52  1.3      sato static int vrbcu_match __P((struct device *, struct cfdata *, void *));
     53  1.3      sato static void vrbcu_attach __P((struct device *, struct device *, void *));
     54  1.1  takemura 
     55  1.3      sato static void vrbcu_write __P((struct vrbcu_softc *, int, unsigned short));
     56  1.3      sato static unsigned short vrbcu_read __P((struct vrbcu_softc *, int));
     57  1.2      sato 
     58  1.5      sato static void vrbcu_dump_regs __P((void));
     59  1.5      sato 
     60  1.2      sato char	*vr_cpuname=NULL;
     61  1.2      sato int	vr_major=-1;
     62  1.2      sato int	vr_minor=-1;
     63  1.2      sato int	vr_cpuid=-1;
     64  1.1  takemura 
     65  1.1  takemura struct cfattach vrbcu_ca = {
     66  1.3      sato 	sizeof(struct vrbcu_softc), vrbcu_match, vrbcu_attach
     67  1.1  takemura };
     68  1.1  takemura 
     69  1.3      sato struct vrbcu_softc *the_bcu_sc = NULL;
     70  1.3      sato 
     71  1.3      sato static inline void
     72  1.3      sato vrbcu_write(sc, port, val)
     73  1.3      sato 	struct vrbcu_softc *sc;
     74  1.3      sato 	int port;
     75  1.3      sato 	unsigned short val;
     76  1.3      sato {
     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.3      sato vrbcu_read(sc, port)
     82  1.3      sato 	struct vrbcu_softc *sc;
     83  1.3      sato 	int port;
     84  1.3      sato {
     85  1.3      sato 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
     86  1.3      sato }
     87  1.3      sato 
     88  1.1  takemura static int
     89  1.3      sato vrbcu_match(parent, cf, aux)
     90  1.1  takemura      struct device *parent;
     91  1.1  takemura      struct cfdata *cf;
     92  1.1  takemura      void *aux;
     93  1.1  takemura {
     94  1.2      sato 	return 2;
     95  1.1  takemura }
     96  1.1  takemura 
     97  1.1  takemura static void
     98  1.3      sato vrbcu_attach(parent, self, aux)
     99  1.1  takemura      struct device *parent;
    100  1.1  takemura      struct device *self;
    101  1.1  takemura      void *aux;
    102  1.1  takemura {
    103  1.1  takemura 	struct vrip_attach_args *va = aux;
    104  1.3      sato 	struct vrbcu_softc *sc = (struct vrbcu_softc *)self;
    105  1.1  takemura 
    106  1.3      sato 	sc->sc_iot = va->va_iot;
    107  1.3      sato 	bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
    108  1.1  takemura 		0, /* no flags */
    109  1.3      sato 		&sc->sc_ioh);
    110  1.1  takemura 
    111  1.1  takemura 	printf("\n");
    112  1.3      sato 	the_bcu_sc = sc;
    113  1.5      sato 	vrbcu_dump_regs();
    114  1.5      sato }
    115  1.5      sato 
    116  1.5      sato static void
    117  1.5      sato vrbcu_dump_regs()
    118  1.5      sato {
    119  1.5      sato 	struct vrbcu_softc *sc = the_bcu_sc;
    120  1.5      sato 	int reg;
    121  1.5      sato 	int cpuclock, tclock, vtclock, cpuid, vt;
    122  1.5      sato 
    123  1.5      sato #ifdef VRBCUDEBUG
    124  1.5      sato 	reg = vrbcu_read(sc, BCUCNT1_REG_W);
    125  1.5      sato 	printf("vrbcu: CNT1 %x: ",  reg);
    126  1.5      sato 	bitdisp16(reg);
    127  1.5      sato 	reg = vrbcu_read(sc, BCUCNT2_REG_W);
    128  1.5      sato 	printf("vrbcu: CNT2 %x: ",  reg);
    129  1.5      sato 	bitdisp16(reg);
    130  1.5      sato 	reg = vrbcu_read(sc, BCUSPEED_REG_W);
    131  1.5      sato 	printf("vrbcu: SPEED %x: ",  reg);
    132  1.5      sato 	bitdisp16(reg);
    133  1.5      sato 	reg = vrbcu_read(sc, BCUERRST_REG_W);
    134  1.5      sato 	printf("vrbcu: ERRST %x: ",  reg);
    135  1.5      sato 	bitdisp16(reg);
    136  1.5      sato 	reg = vrbcu_read(sc, BCURFCNT_REG_W);
    137  1.5      sato 	printf("vrbcu: RFCNT %x\n",  reg);
    138  1.5      sato 	reg = vrbcu_read(sc, BCUREFCOUNT_REG_W);
    139  1.5      sato 	printf("vrbcu: RFCOUNT %x\n",  reg);
    140  1.5      sato #endif /* VRBCUDEBUG */
    141  1.5      sato 	reg = vrbcu_read(sc, BCUCLKSPEED_REG_W);
    142  1.5      sato #ifdef VRBCUDEBUG
    143  1.5      sato 	printf("vrbcu: CLKSPEED %x: \n",  reg);
    144  1.5      sato #endif /* VRBCUDEBUG */
    145  1.5      sato 	cpuclock = vrbcu_vrip_getcpuclock();
    146  1.5      sato 	cpuid = vrbcu_vrip_getcpuid();
    147  1.5      sato 
    148  1.5      sato 	switch (cpuid) {
    149  1.5      sato 	case BCUREVID_RID_4101:
    150  1.5      sato 		/* assume 33MHz */
    151  1.5      sato 		vtclock = tclock = cpuclock/2;	/* XXX */
    152  1.5      sato 		break;
    153  1.5      sato 	case BCUREVID_RID_4102:
    154  1.5      sato 		vtclock = tclock = cpuclock/2;
    155  1.5      sato 		break;
    156  1.5      sato 	case BCUREVID_RID_4111:
    157  1.5      sato 		if ((reg&BCUCLKSPEED_DIVT2B) == 0)
    158  1.5      sato 			vtclock = tclock = cpuclock/2;
    159  1.5      sato 		else if ((reg&BCUCLKSPEED_DIVT3B) == 0)
    160  1.5      sato 			vtclock = tclock = cpuclock/3;
    161  1.5      sato 		else if ((reg&BCUCLKSPEED_DIVT4B) == 0)
    162  1.5      sato 			vtclock = tclock = cpuclock/4;
    163  1.5      sato 		else
    164  1.5      sato 			vtclock = tclock = 0; /* XXX */
    165  1.5      sato 		break;
    166  1.5      sato 	case BCUREVID_RID_4121:
    167  1.5      sato 			tclock = cpuclock / ((reg&BCUCLKSPEED_DIVTMASK)>>BCUCLKSPEED_DIVTSHFT);
    168  1.5      sato 			vt = ((reg&BCUCLKSPEED_DIVVTMASK)>>BCUCLKSPEED_DIVVTSHFT);
    169  1.5      sato 			if (vt == 0)
    170  1.5      sato 				vtclock = 0; /* XXX */
    171  1.5      sato 			else if (vt < 0x9)
    172  1.5      sato 				vtclock = cpuclock / vt;
    173  1.5      sato 			else
    174  1.5      sato 				vtclock = cpuclock / ((vt - 8)*2+1) * 2;
    175  1.5      sato 		break;
    176  1.5      sato 	default:
    177  1.5      sato 		break;
    178  1.5      sato 	}
    179  1.5      sato 	printf("vrbcu: cpu %d.%03dMHz, bus %d.%03dMHz, ram %d.%03dMHz\n",
    180  1.5      sato 		cpuclock/1000000, (cpuclock%1000000)/1000,
    181  1.5      sato 		tclock/1000000, (tclock%1000000)/1000,
    182  1.5      sato 		vtclock/1000000, (vtclock%1000000)/1000);
    183  1.5      sato #ifdef VRBCUDEBUG
    184  1.5      sato 	if (cpuid >= BCUREVID_RID_4111) {
    185  1.5      sato 		reg = vrbcu_read(sc, BCUCNT3_REG_W);
    186  1.5      sato 		printf("vrbcu: CNT3 %x: ",  reg);
    187  1.5      sato 		bitdisp16(reg);
    188  1.5      sato 	}
    189  1.5      sato #endif /* VRBCUDEBUG */
    190  1.5      sato 
    191  1.1  takemura }
    192  1.1  takemura 
    193  1.1  takemura static char *cpuname[] = {
    194  1.1  takemura 	"VR4101",
    195  1.1  takemura 	"VR4102",
    196  1.1  takemura 	"VR4111",
    197  1.1  takemura 	"VR4121",
    198  1.1  takemura 	"UNKNOWN",
    199  1.1  takemura 	"UNKNOWN",
    200  1.1  takemura 	"UNKNOWN",
    201  1.1  takemura 	"UNKNOWN" };
    202  1.1  takemura 
    203  1.2      sato int
    204  1.2      sato vrbcu_vrip_getcpuid(void)
    205  1.2      sato {
    206  1.2      sato 	volatile u_int16_t *revreg;
    207  1.2      sato 
    208  1.2      sato 	if (vr_cpuid != -1)
    209  1.2      sato 		return vr_cpuid;
    210  1.2      sato 
    211  1.2      sato 	if (vr_cpuid == -1) {
    212  1.2      sato 		revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1((VRIP_BCU_ADDR+BCUREVID_REG_W));
    213  1.2      sato 
    214  1.2      sato 		vr_cpuid = *revreg;
    215  1.2      sato 		vr_cpuid = (vr_cpuid&BCUREVID_RIDMASK)>>BCUREVID_RIDSHFT;
    216  1.2      sato 	}
    217  1.2      sato 	return vr_cpuid;
    218  1.2      sato }
    219  1.2      sato 
    220  1.1  takemura char *
    221  1.1  takemura vrbcu_vrip_getcpuname(void)
    222  1.1  takemura {
    223  1.2      sato 	int cpuid;
    224  1.1  takemura 
    225  1.2      sato 	if (vr_cpuname != NULL)
    226  1.2      sato 		return vr_cpuname;
    227  1.1  takemura 
    228  1.2      sato 	cpuid = vrbcu_vrip_getcpuid();
    229  1.2      sato 	vr_cpuname = cpuname[cpuid];
    230  1.2      sato 	return vr_cpuname;
    231  1.1  takemura }
    232  1.1  takemura 
    233  1.2      sato 
    234  1.1  takemura int
    235  1.1  takemura vrbcu_vrip_getcpumajor(void)
    236  1.1  takemura {
    237  1.1  takemura 	volatile u_int16_t *revreg;
    238  1.2      sato 
    239  1.2      sato 	if (vr_major != -1)
    240  1.2      sato 		return vr_major;
    241  1.1  takemura 
    242  1.1  takemura 	revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1((VRIP_BCU_ADDR+BCUREVID_REG_W));
    243  1.1  takemura 
    244  1.2      sato 	vr_major = *revreg;
    245  1.2      sato 	vr_major = (vr_major&BCUREVID_MJREVMASK)>>BCUREVID_MJREVSHFT;
    246  1.2      sato 	return vr_major;
    247  1.1  takemura }
    248  1.1  takemura 
    249  1.1  takemura int
    250  1.1  takemura vrbcu_vrip_getcpuminor(void)
    251  1.1  takemura {
    252  1.1  takemura 	volatile u_int16_t *revreg;
    253  1.2      sato 
    254  1.2      sato 	if (vr_minor != -1)
    255  1.2      sato 		return vr_minor;
    256  1.1  takemura 
    257  1.1  takemura 	revreg = (u_int16_t *)MIPS_PHYS_TO_KSEG1((VRIP_BCU_ADDR+BCUREVID_REG_W));
    258  1.1  takemura 
    259  1.2      sato 	vr_minor = *revreg;
    260  1.2      sato 	vr_minor = (vr_minor&BCUREVID_MNREVMASK)>>BCUREVID_MNREVSHFT;
    261  1.2      sato 	return vr_minor;
    262  1.1  takemura }
    263  1.4      shin 
    264  1.4      shin #define CLKX	18432000	/* CLKX1,CLKX2: 18.432MHz */
    265  1.4      shin #define MHZ	1000000
    266  1.4      shin 
    267  1.4      shin int
    268  1.4      shin vrbcu_vrip_getcpuclock(void)
    269  1.4      shin {
    270  1.4      shin 	u_int16_t clksp;
    271  1.4      shin 	int cpuid, cpuclock;
    272  1.4      shin 
    273  1.4      shin 	clksp = *(u_int16_t *)MIPS_PHYS_TO_KSEG1((VRIP_BCU_ADDR+BCUCLKSPEED_REG_W)) & BCUCLKSPEED_CLKSPMASK;
    274  1.4      shin 	cpuid = vrbcu_vrip_getcpuid();
    275  1.4      shin 
    276  1.4      shin 	switch (cpuid) {
    277  1.4      shin 	case BCUREVID_RID_4101:
    278  1.4      shin 		/* assume 33MHz */
    279  1.4      shin 		cpuclock = 33000000;
    280  1.4      shin 		/* branch delay is 1 clock; 2 clock/loop */
    281  1.4      shin 		cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
    282  1.4      shin 		break;
    283  1.4      shin 	case BCUREVID_RID_4102:
    284  1.4      shin 		cpuclock = CLKX / clksp * 32;
    285  1.4      shin 		/* branch delay is 1 clock; 2 clock/loop */
    286  1.4      shin 		cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
    287  1.4      shin 		break;
    288  1.4      shin 	case BCUREVID_RID_4111:
    289  1.4      shin 		cpuclock = CLKX / clksp * 64;
    290  1.4      shin 		/* branch delay is 1 clock; 2 clock/loop */
    291  1.4      shin 		cpuspeed = (cpuclock / 2 + MHZ / 2) / MHZ;
    292  1.4      shin 		break;
    293  1.4      shin 	case BCUREVID_RID_4121:
    294  1.4      shin 		cpuclock = CLKX / clksp * 64;
    295  1.4      shin 		/* branch delay is 2 clock; 3 clock/loop */
    296  1.4      shin 		cpuspeed = (cpuclock / 3 + MHZ / 2) / MHZ;
    297  1.4      shin 		break;
    298  1.4      shin 	default:
    299  1.4      shin 		panic("unknown CPU type %d\n", cpuid);
    300  1.4      shin 		break;
    301  1.4      shin 	}
    302  1.4      shin 	return cpuclock;
    303  1.4      shin }
    304