Home | History | Annotate | Line # | Download | only in vax
ka43.c revision 1.12
      1 /*	$NetBSD: ka43.c,v 1.12 1999/01/06 14:54:45 ragge Exp $ */
      2 /*
      3  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to Ludd by Bertram Barth.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed at Ludd, University of
     19  *	Lule}, Sweden and its contributors.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/types.h>
     37 #include <sys/device.h>
     38 #include <sys/kernel.h>
     39 #include <sys/systm.h>
     40 
     41 #include <vm/vm.h>
     42 #include <vm/vm_kern.h>
     43 
     44 #include <machine/pte.h>
     45 #include <machine/cpu.h>
     46 #include <machine/mtpr.h>
     47 #include <machine/sid.h>
     48 #include <machine/pmap.h>
     49 #include <machine/nexus.h>
     50 #include <machine/uvax.h>
     51 #include <machine/vsbus.h>
     52 #include <machine/ka43.h>
     53 #include <machine/clock.h>
     54 
     55 #include "smg.h"
     56 #include "ncr.h"
     57 
     58 void	ka43_conf __P((struct device*, struct device*, void*));
     59 void	ka43_steal_pages __P((void));
     60 
     61 int	ka43_mchk __P((caddr_t));
     62 void	ka43_memerr __P((void));
     63 
     64 void	ka43_clear_errors __P((void));
     65 
     66 int	ka43_cache_init __P((void));	/* "int mapen" as argument? */
     67 int	ka43_cache_reset __P((void));
     68 int	ka43_cache_enable __P((void));
     69 int	ka43_cache_disable __P((void));
     70 int	ka43_cache_invalidate __P((void));
     71 
     72 struct	cpu_dep ka43_calls = {
     73 	ka43_steal_pages,
     74 	no_nicr_clock,
     75 	ka43_mchk,
     76 	ka43_memerr,
     77 	ka43_conf,
     78 	chip_clkread,
     79 	chip_clkwrite,
     80 	7,	/* 7.6 VUP */
     81 };
     82 
     83 /*
     84  * ka43_steal_pages() is called with MMU disabled, after that call MMU gets
     85  * enabled. Thus we initialize these four pointers with physical addresses,
     86  * but before leving ka43_steal_pages() we reset them to virtual addresses.
     87  */
     88 struct	ka43_cpu   *ka43_cpu	= (void*)KA43_CPU_BASE;
     89 
     90 u_int	*ka43_creg = (void*)KA43_CH2_CREG;
     91 u_int	*ka43_ctag = (void*)KA43_CT2_BASE;
     92 
     93 #define KA43_MC_RESTART	0x00008000	/* Restart possible*/
     94 #define KA43_PSL_FPDONE	0x00010000	/* First Part Done */
     95 
     96 struct ka43_mcframe {		/* Format of RigelMAX machine check frame: */
     97 	int	mc43_bcnt;	/* byte count, always 24 (0x18) */
     98 	int	mc43_code;	/* machine check type code and restart bit */
     99 	int	mc43_addr;	/* most recent (faulting?) virtual address */
    100 	int	mc43_viba;	/* contents of VIBA register */
    101 	int	mc43_sisr;	/* ICCS bit 6 and SISR bits 15:0 */
    102 	int	mc43_istate;	/* internal state */
    103 	int	mc43_sc;	/* shift count register */
    104 	int	mc43_pc;	/* trapped PC */
    105 	int	mc43_psl;	/* trapped PSL */
    106 };
    107 
    108 static char *ka43_mctype[] = {
    109 	"no error (0)",			/* Code 0: No error */
    110 	"FPA: protocol error",		/* Code 1-5: FPA errors */
    111 	"FPA: illegal opcode",
    112 	"FPA: operand parity error",
    113 	"FPA: unknown status",
    114 	"FPA: result parity error",
    115 	"unused (6)",			/* Code 6-7: Unused */
    116 	"unused (7)",
    117 	"MMU error (TLB miss)",		/* Code 8-9: MMU errors */
    118 	"MMU error (TLB hit)",
    119 	"HW interrupt at unused IPL",	/* Code 10: Interrupt error */
    120 	"MOVCx impossible state",	/* Code 11-13: Microcode errors */
    121 	"undefined trap code (i-box)",
    122 	"undefined control store address",
    123 	"unused (14)",			/* Code 14-15: Unused */
    124 	"unused (15)",
    125 	"PC tag or data parity error",	/* Code 16: Cache error */
    126 	"data bus parity error",	/* Code 17: Read error */
    127 	"data bus error (NXM)",		/* Code 18: Write error */
    128 	"undefined data bus state",	/* Code 19: Bus error */
    129 };
    130 #define MC43_MAX	19
    131 
    132 static int ka43_error_count = 0;
    133 
    134 int
    135 ka43_mchk(addr)
    136 	caddr_t addr;
    137 {
    138 	register struct ka43_mcframe *mcf = (void*)addr;
    139 
    140 	mtpr(0x00, PR_MCESR);	/* Acknowledge the machine check */
    141 	printf("machine check %d (0x%x)\n", mcf->mc43_code, mcf->mc43_code);
    142 	printf("reason: %s\n", ka43_mctype[mcf->mc43_code & 0xff]);
    143 	if (++ka43_error_count > 10) {
    144 		printf("error_count exceeded: %d\n", ka43_error_count);
    145 		return (-1);
    146 	}
    147 
    148 	/*
    149 	 * If either the Restart flag is set or the First-Part-Done flag
    150 	 * is set, and the TRAP2 (double error) bit is not set, the the
    151 	 * error is recoverable.
    152 	 */
    153 	if (mfpr(PR_PCSTS) & KA43_PCS_TRAP2) {
    154 		printf("TRAP2 (double error) in ka43_mchk.\n");
    155 		panic("unrecoverable state in ka43_mchk.\n");
    156 		return (-1);
    157 	}
    158 	if ((mcf->mc43_code & KA43_MC_RESTART) ||
    159 	    (mcf->mc43_psl & KA43_PSL_FPDONE)) {
    160 		printf("ka43_mchk: recovering from machine-check.\n");
    161 		ka43_cache_reset();	/* reset caches */
    162 		return (0);		/* go on; */
    163 	}
    164 
    165 	/*
    166 	 * Unknown error state, panic/halt the machine!
    167 	 */
    168 	printf("ka43_mchk: unknown error state!\n");
    169 	return (-1);
    170 }
    171 
    172 void
    173 ka43_memerr()
    174 {
    175 	/*
    176 	 * Don\'t know what to do here. So just print some messages
    177 	 * and try to go on...
    178 	 */
    179 	printf("memory error!\n");
    180 	printf("primary cache status: %b\n", mfpr(PR_PCSTS), KA43_PCSTS_BITS);
    181 	printf("secondary cache status: %b\n", *ka43_creg, KA43_SESR_BITS);
    182 }
    183 
    184 int
    185 ka43_cache_init()
    186 {
    187 	return (ka43_cache_reset());
    188 }
    189 
    190 void
    191 ka43_clear_errors()
    192 {
    193 	int val = *ka43_creg;
    194 	val |= KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR;
    195 	*ka43_creg = val;
    196 }
    197 
    198 int
    199 ka43_cache_reset()
    200 {
    201 	/*
    202 	 * resetting primary and secondary caches is done in three steps:
    203 	 *	1. disable both caches
    204 	 *	2. manually clear secondary cache
    205 	 *	3. enable both caches
    206 	 */
    207 	ka43_cache_disable();
    208 	ka43_cache_invalidate();
    209 	ka43_cache_enable();
    210 
    211 	printf("primary cache status: %b\n", mfpr(PR_PCSTS), KA43_PCSTS_BITS);
    212 	printf("secondary cache status: %b\n", *ka43_creg, KA43_SESR_BITS);
    213 
    214 	return (0);
    215 }
    216 
    217 int
    218 ka43_cache_disable()
    219 {
    220 	int val;
    221 
    222 	/*
    223 	 * first disable primary cache and clear error flags
    224 	 */
    225 	mtpr(KA43_PCS_REFRESH, PR_PCSTS);	/* disable primary cache */
    226 	val = mfpr(PR_PCSTS);
    227 	mtpr(val, PR_PCSTS);			/* clear error flags */
    228 
    229 	/*
    230 	 * now disable secondary cache and clear error flags
    231 	 */
    232 	val = *ka43_creg & ~KA43_SESR_CENB;	/* BICL !!! */
    233 	*ka43_creg = val;			/* disable secondary cache */
    234 	val = KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR;
    235 	*ka43_creg = val;			/* clear error flags */
    236 
    237 	return (0);
    238 }
    239 
    240 int
    241 ka43_cache_invalidate()
    242 {
    243 	int i, val;
    244 
    245 	val = KA43_PCTAG_PARITY;	/* clear valid flag, set parity bit */
    246 	for (i = 0; i < 256; i++) {	/* 256 Quadword entries */
    247 		mtpr(i*8, PR_PCIDX);	/* write index of tag */
    248 		mtpr(val, PR_PCTAG);	/* write value into tag */
    249 	}
    250 	val = KA43_PCS_FLUSH | KA43_PCS_REFRESH;
    251 	mtpr(val, PR_PCSTS);		/* flush primary cache */
    252 
    253 	/*
    254 	 * Rigel\'s secondary cache doesn\'t implement a valid-flag.
    255 	 * Thus we initialize all entries with out-of-range/dummy
    256 	 * addresses which will never be referenced (ie. never hit).
    257 	 * After enabling cache we also access 128K of memory starting
    258 	 * at 0x00 so that secondary cache will be filled with these
    259 	 * valid addresses...
    260 	 */
    261 	val = 0xff;
    262 	/* if (memory > 28 MB) val = 0x55; */
    263 	for (i = 0; i < KA43_CT2_SIZE; i+= 4) {	/* Quadword entries ?? */
    264 		ka43_ctag[i/4] = val;		/* reset upper and lower */
    265 	}
    266 
    267 	return (0);
    268 }
    269 
    270 
    271 int
    272 ka43_cache_enable()
    273 {
    274 	volatile char *membase = (void*)0x80000000;	/* physical 0x00 */
    275 	int i, val;
    276 
    277 	val = KA43_PCS_FLUSH | KA43_PCS_REFRESH;
    278 	mtpr(val, PR_PCSTS);		/* flush primary cache */
    279 
    280 	/*
    281 	 * now we enable secondary cache and access first 128K of memory
    282 	 * so that secondary cache gets really initialized and holds
    283 	 * valid addresses/data...
    284 	 */
    285 	*ka43_creg = KA43_SESR_CENB;	/* enable secondary cache */
    286 	for (i=0; i<128*1024; i++) {
    287 		val += membase[i];	/* some dummy operation... */
    288 	}
    289 
    290 	val = KA43_PCS_ENABLE | KA43_PCS_REFRESH;
    291 	mtpr(val, PR_PCSTS);		/* enable primary cache */
    292 
    293 	return (0);
    294 }
    295 
    296 void
    297 ka43_conf(parent, self, aux)
    298 	struct	device *parent, *self;
    299 	void	*aux;
    300 {
    301 
    302 	printf(": KA43\n");
    303 	/*
    304 	 * ka43_conf() gets called with MMU enabled, now it's save to
    305 	 * init/reset the caches.
    306 	 */
    307 	ka43_cache_init();
    308 }
    309 
    310 
    311 /*
    312  * The interface for communication with the LANCE ethernet controller
    313  * is setup in the xxx_steal_pages() routine. We decrease highest
    314  * available address by 64K and use this area as communication buffer.
    315  */
    316 
    317 void
    318 ka43_steal_pages()
    319 {
    320 	extern	vm_offset_t avail_start, virtual_avail;
    321         extern  short *clk_page;
    322         extern  int clk_adrshift, clk_tweak;
    323 	int	junk, val;
    324 
    325         /* Interrupt vector number in interrupt mask table */
    326         inr_ni = VS3100_NI;
    327         inr_sr = VS3100_SR;
    328         inr_st = VS3100_ST;
    329         inr_vf = VS3100_VF;
    330 	/*
    331 	 * SCB is already copied/initialized at addr avail_start
    332 	 * by pmap_bootstrap(), but it's not yet mapped. Thus we use
    333 	 * the MAPPHYS() macro to reserve these two pages and to
    334 	 * perform the mapping. The mapped address is assigned to junk.
    335 	 */
    336 	MAPPHYS(junk, 2, VM_PROT_READ|VM_PROT_WRITE);
    337 
    338         clk_adrshift = 1;       /* Addressed at long's... */
    339         clk_tweak = 2;          /* ...and shift two */
    340         MAPVIRT(clk_page, 2);
    341         pmap_map((vm_offset_t)clk_page, (vm_offset_t)KA43_WAT_BASE,
    342             (vm_offset_t)KA43_WAT_BASE + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    343 
    344 	/* LANCE CSR */
    345 	MAPVIRT(lance_csr, 1);
    346 	pmap_map((vm_offset_t)lance_csr, (vm_offset_t)NI_BASE,
    347 	    (vm_offset_t)NI_BASE + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    348 
    349 	MAPVIRT(vs_cpu, 1);
    350 	pmap_map((vm_offset_t)vs_cpu, (vm_offset_t)VS_REGS,
    351 	    (vm_offset_t)VS_REGS + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    352 
    353 	MAPVIRT(dz_regs, 2);
    354 	pmap_map((vm_offset_t)dz_regs, (vm_offset_t)DZ_CSR,
    355 	    (vm_offset_t)DZ_CSR + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    356 
    357 	MAPVIRT(lance_addr, 1);
    358 	pmap_map((vm_offset_t)lance_addr, (vm_offset_t)NI_ADDR,
    359 	    (vm_offset_t)NI_ADDR + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    360 
    361 	/* 2nd level CCR */
    362 	MAPVIRT(ka43_creg, 1);
    363 	pmap_map((vm_offset_t)ka43_creg, (vm_offset_t)KA43_CH2_CREG,
    364 	    (vm_offset_t)KA43_CH2_CREG + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    365 
    366 	/* 2nd level CTA */
    367 	MAPVIRT(ka43_ctag, KA43_CT2_SIZE/VAX_NBPG);
    368 	pmap_map((vm_offset_t)ka43_ctag, (vm_offset_t)KA43_CT2_BASE,
    369 	    (vm_offset_t)KA43_CT2_BASE + KA43_CT2_SIZE,
    370 	    VM_PROT_READ|VM_PROT_WRITE);
    371 
    372 #if NNCR > 0
    373 	/* SCSI controller */
    374 	MAPVIRT(sca_regs, (KA43_SCS_SIZE / VAX_NBPG));
    375 	pmap_map((vm_offset_t)sca_regs, (vm_offset_t)KA43_SCS_BASE,
    376 	    (vm_offset_t)KA43_SCS_BASE + KA43_SCS_SIZE,
    377 	    VM_PROT_READ|VM_PROT_WRITE);
    378 
    379 	/* SCSI DMA.  Not used right now, untested. */
    380 	MAPVIRT(dma_area, (KA43_DMA_SIZE / VAX_NBPG));
    381 	pmap_map((vm_offset_t)dma_area, (vm_offset_t)KA43_DMA_BASE,
    382 	    (vm_offset_t)KA43_DMA_BASE + KA43_DMA_SIZE,
    383 	    VM_PROT_READ|VM_PROT_WRITE);
    384 #endif
    385 	/*
    386 	 * Oh holy shit! It took me over one year(!) to find out that
    387 	 * the 3100/76 has to use diag-mem instead of physical memory
    388 	 * for communication with LANCE (using phys-mem results in
    389 	 * parity errors and mchk exceptions with code 17 (0x11)).
    390 	 *
    391 	 * Many thanks to Matt Thomas, without his help it could have
    392 	 * been some more years...  ;-)
    393 	 */
    394 #define	LEMEM (((int)le_iomem & ~KERNBASE)|KA43_DIAGMEM)
    395 	MAPPHYS(le_iomem, (NI_IOSIZE/VAX_NBPG), VM_PROT_READ|VM_PROT_WRITE);
    396 	pmap_map((vm_offset_t)le_iomem, LEMEM, LEMEM + NI_IOSIZE,
    397 	    VM_PROT_READ|VM_PROT_WRITE);
    398 
    399 #if NSMG > 0
    400 	if ((vax_confdata & 0x80) == 0) {
    401 		MAPVIRT(sm_addr, (SMSIZE / VAX_NBPG));
    402 		pmap_map((vm_offset_t)sm_addr, (vm_offset_t)SMADDR,
    403 		    (vm_offset_t)SMADDR + SMSIZE, VM_PROT_READ|VM_PROT_WRITE);
    404 		((struct vs_cpu *)VS_REGS)->vc_vdcorg = 0;
    405 	}
    406 #endif
    407 
    408 	/*
    409 	 * if LANCE\'s io-buffer is above 16 MB, then the appropriate flag
    410 	 * in the parity control register has to be set (it works as an
    411 	 * additional address bit). In any case, don\'t enable CPEN and
    412 	 * DPEN in the PARCTL register, somewhow they are internally managed
    413 	 * by the RIGEL chip itself!?!
    414 	 */
    415 	val = ka43_cpu->parctl & 0x03;	/* read the old value */
    416 	if (((int)le_iomem & ~KERNBASE) > 0xffffff)
    417 		val |= KA43_PCTL_DMA;
    418 	ka43_cpu->parctl = val;		/* and write new value */
    419 
    420 	/*
    421 	 * Clear restart and boot in progress flags in the CPMBX.
    422 	 */
    423 	((struct ka43_clock *)KA43_WAT_BASE)->cpmbx =
    424 	    ((struct ka43_clock *)KA43_WAT_BASE)->cpmbx & 0xF0;
    425 
    426 #if 0
    427 	/*
    428 	 * Clear all error flags, not really neccessary here, this will
    429 	 * be done by ka43_cache_init() anyway...
    430 	 */
    431 	ka43_clear_errors();
    432 #endif
    433 
    434 	/*
    435 	 * MM is not yet enabled, thus we still used the physical addresses,
    436 	 * but before leaving this routine, we need to reset them to virtual.
    437 	 */
    438 	ka43_cpu = (void *)vs_cpu;
    439 }
    440