Home | History | Annotate | Line # | Download | only in vax
ka43.c revision 1.13
      1 /*	$NetBSD: ka43.c,v 1.13 1999/01/19 21:04:49 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 	2,	/* SCB pages */
     82 };
     83 
     84 /*
     85  * ka43_steal_pages() is called with MMU disabled, after that call MMU gets
     86  * enabled. Thus we initialize these four pointers with physical addresses,
     87  * but before leving ka43_steal_pages() we reset them to virtual addresses.
     88  */
     89 struct	ka43_cpu   *ka43_cpu	= (void*)KA43_CPU_BASE;
     90 
     91 u_int	*ka43_creg = (void*)KA43_CH2_CREG;
     92 u_int	*ka43_ctag = (void*)KA43_CT2_BASE;
     93 
     94 #define KA43_MC_RESTART	0x00008000	/* Restart possible*/
     95 #define KA43_PSL_FPDONE	0x00010000	/* First Part Done */
     96 
     97 struct ka43_mcframe {		/* Format of RigelMAX machine check frame: */
     98 	int	mc43_bcnt;	/* byte count, always 24 (0x18) */
     99 	int	mc43_code;	/* machine check type code and restart bit */
    100 	int	mc43_addr;	/* most recent (faulting?) virtual address */
    101 	int	mc43_viba;	/* contents of VIBA register */
    102 	int	mc43_sisr;	/* ICCS bit 6 and SISR bits 15:0 */
    103 	int	mc43_istate;	/* internal state */
    104 	int	mc43_sc;	/* shift count register */
    105 	int	mc43_pc;	/* trapped PC */
    106 	int	mc43_psl;	/* trapped PSL */
    107 };
    108 
    109 static char *ka43_mctype[] = {
    110 	"no error (0)",			/* Code 0: No error */
    111 	"FPA: protocol error",		/* Code 1-5: FPA errors */
    112 	"FPA: illegal opcode",
    113 	"FPA: operand parity error",
    114 	"FPA: unknown status",
    115 	"FPA: result parity error",
    116 	"unused (6)",			/* Code 6-7: Unused */
    117 	"unused (7)",
    118 	"MMU error (TLB miss)",		/* Code 8-9: MMU errors */
    119 	"MMU error (TLB hit)",
    120 	"HW interrupt at unused IPL",	/* Code 10: Interrupt error */
    121 	"MOVCx impossible state",	/* Code 11-13: Microcode errors */
    122 	"undefined trap code (i-box)",
    123 	"undefined control store address",
    124 	"unused (14)",			/* Code 14-15: Unused */
    125 	"unused (15)",
    126 	"PC tag or data parity error",	/* Code 16: Cache error */
    127 	"data bus parity error",	/* Code 17: Read error */
    128 	"data bus error (NXM)",		/* Code 18: Write error */
    129 	"undefined data bus state",	/* Code 19: Bus error */
    130 };
    131 #define MC43_MAX	19
    132 
    133 static int ka43_error_count = 0;
    134 
    135 int
    136 ka43_mchk(addr)
    137 	caddr_t addr;
    138 {
    139 	register struct ka43_mcframe *mcf = (void*)addr;
    140 
    141 	mtpr(0x00, PR_MCESR);	/* Acknowledge the machine check */
    142 	printf("machine check %d (0x%x)\n", mcf->mc43_code, mcf->mc43_code);
    143 	printf("reason: %s\n", ka43_mctype[mcf->mc43_code & 0xff]);
    144 	if (++ka43_error_count > 10) {
    145 		printf("error_count exceeded: %d\n", ka43_error_count);
    146 		return (-1);
    147 	}
    148 
    149 	/*
    150 	 * If either the Restart flag is set or the First-Part-Done flag
    151 	 * is set, and the TRAP2 (double error) bit is not set, the the
    152 	 * error is recoverable.
    153 	 */
    154 	if (mfpr(PR_PCSTS) & KA43_PCS_TRAP2) {
    155 		printf("TRAP2 (double error) in ka43_mchk.\n");
    156 		panic("unrecoverable state in ka43_mchk.\n");
    157 		return (-1);
    158 	}
    159 	if ((mcf->mc43_code & KA43_MC_RESTART) ||
    160 	    (mcf->mc43_psl & KA43_PSL_FPDONE)) {
    161 		printf("ka43_mchk: recovering from machine-check.\n");
    162 		ka43_cache_reset();	/* reset caches */
    163 		return (0);		/* go on; */
    164 	}
    165 
    166 	/*
    167 	 * Unknown error state, panic/halt the machine!
    168 	 */
    169 	printf("ka43_mchk: unknown error state!\n");
    170 	return (-1);
    171 }
    172 
    173 void
    174 ka43_memerr()
    175 {
    176 	/*
    177 	 * Don\'t know what to do here. So just print some messages
    178 	 * and try to go on...
    179 	 */
    180 	printf("memory error!\n");
    181 	printf("primary cache status: %b\n", mfpr(PR_PCSTS), KA43_PCSTS_BITS);
    182 	printf("secondary cache status: %b\n", *ka43_creg, KA43_SESR_BITS);
    183 }
    184 
    185 int
    186 ka43_cache_init()
    187 {
    188 	return (ka43_cache_reset());
    189 }
    190 
    191 void
    192 ka43_clear_errors()
    193 {
    194 	int val = *ka43_creg;
    195 	val |= KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR;
    196 	*ka43_creg = val;
    197 }
    198 
    199 int
    200 ka43_cache_reset()
    201 {
    202 	/*
    203 	 * resetting primary and secondary caches is done in three steps:
    204 	 *	1. disable both caches
    205 	 *	2. manually clear secondary cache
    206 	 *	3. enable both caches
    207 	 */
    208 	ka43_cache_disable();
    209 	ka43_cache_invalidate();
    210 	ka43_cache_enable();
    211 
    212 	printf("primary cache status: %b\n", mfpr(PR_PCSTS), KA43_PCSTS_BITS);
    213 	printf("secondary cache status: %b\n", *ka43_creg, KA43_SESR_BITS);
    214 
    215 	return (0);
    216 }
    217 
    218 int
    219 ka43_cache_disable()
    220 {
    221 	int val;
    222 
    223 	/*
    224 	 * first disable primary cache and clear error flags
    225 	 */
    226 	mtpr(KA43_PCS_REFRESH, PR_PCSTS);	/* disable primary cache */
    227 	val = mfpr(PR_PCSTS);
    228 	mtpr(val, PR_PCSTS);			/* clear error flags */
    229 
    230 	/*
    231 	 * now disable secondary cache and clear error flags
    232 	 */
    233 	val = *ka43_creg & ~KA43_SESR_CENB;	/* BICL !!! */
    234 	*ka43_creg = val;			/* disable secondary cache */
    235 	val = KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR;
    236 	*ka43_creg = val;			/* clear error flags */
    237 
    238 	return (0);
    239 }
    240 
    241 int
    242 ka43_cache_invalidate()
    243 {
    244 	int i, val;
    245 
    246 	val = KA43_PCTAG_PARITY;	/* clear valid flag, set parity bit */
    247 	for (i = 0; i < 256; i++) {	/* 256 Quadword entries */
    248 		mtpr(i*8, PR_PCIDX);	/* write index of tag */
    249 		mtpr(val, PR_PCTAG);	/* write value into tag */
    250 	}
    251 	val = KA43_PCS_FLUSH | KA43_PCS_REFRESH;
    252 	mtpr(val, PR_PCSTS);		/* flush primary cache */
    253 
    254 	/*
    255 	 * Rigel\'s secondary cache doesn\'t implement a valid-flag.
    256 	 * Thus we initialize all entries with out-of-range/dummy
    257 	 * addresses which will never be referenced (ie. never hit).
    258 	 * After enabling cache we also access 128K of memory starting
    259 	 * at 0x00 so that secondary cache will be filled with these
    260 	 * valid addresses...
    261 	 */
    262 	val = 0xff;
    263 	/* if (memory > 28 MB) val = 0x55; */
    264 	for (i = 0; i < KA43_CT2_SIZE; i+= 4) {	/* Quadword entries ?? */
    265 		ka43_ctag[i/4] = val;		/* reset upper and lower */
    266 	}
    267 
    268 	return (0);
    269 }
    270 
    271 
    272 int
    273 ka43_cache_enable()
    274 {
    275 	volatile char *membase = (void*)0x80000000;	/* physical 0x00 */
    276 	int i, val;
    277 
    278 	val = KA43_PCS_FLUSH | KA43_PCS_REFRESH;
    279 	mtpr(val, PR_PCSTS);		/* flush primary cache */
    280 
    281 	/*
    282 	 * now we enable secondary cache and access first 128K of memory
    283 	 * so that secondary cache gets really initialized and holds
    284 	 * valid addresses/data...
    285 	 */
    286 	*ka43_creg = KA43_SESR_CENB;	/* enable secondary cache */
    287 	for (i=0; i<128*1024; i++) {
    288 		val += membase[i];	/* some dummy operation... */
    289 	}
    290 
    291 	val = KA43_PCS_ENABLE | KA43_PCS_REFRESH;
    292 	mtpr(val, PR_PCSTS);		/* enable primary cache */
    293 
    294 	return (0);
    295 }
    296 
    297 void
    298 ka43_conf(parent, self, aux)
    299 	struct	device *parent, *self;
    300 	void	*aux;
    301 {
    302 
    303 	printf(": KA43\n");
    304 	/*
    305 	 * ka43_conf() gets called with MMU enabled, now it's save to
    306 	 * init/reset the caches.
    307 	 */
    308 	ka43_cache_init();
    309 }
    310 
    311 
    312 /*
    313  * The interface for communication with the LANCE ethernet controller
    314  * is setup in the xxx_steal_pages() routine. We decrease highest
    315  * available address by 64K and use this area as communication buffer.
    316  */
    317 
    318 void
    319 ka43_steal_pages()
    320 {
    321 	extern	vm_offset_t avail_start, virtual_avail;
    322         extern  short *clk_page;
    323         extern  int clk_adrshift, clk_tweak;
    324 	int	junk, val;
    325 
    326         /* Interrupt vector number in interrupt mask table */
    327         inr_ni = VS3100_NI;
    328         inr_sr = VS3100_SR;
    329         inr_st = VS3100_ST;
    330         inr_vf = VS3100_VF;
    331 	/*
    332 	 * SCB is already copied/initialized at addr avail_start
    333 	 * by pmap_bootstrap(), but it's not yet mapped. Thus we use
    334 	 * the MAPPHYS() macro to reserve these two pages and to
    335 	 * perform the mapping. The mapped address is assigned to junk.
    336 	 */
    337 	MAPPHYS(junk, 2, VM_PROT_READ|VM_PROT_WRITE);
    338 
    339         clk_adrshift = 1;       /* Addressed at long's... */
    340         clk_tweak = 2;          /* ...and shift two */
    341         MAPVIRT(clk_page, 2);
    342         pmap_map((vm_offset_t)clk_page, (vm_offset_t)KA43_WAT_BASE,
    343             (vm_offset_t)KA43_WAT_BASE + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    344 
    345 	/* LANCE CSR */
    346 	MAPVIRT(lance_csr, 1);
    347 	pmap_map((vm_offset_t)lance_csr, (vm_offset_t)NI_BASE,
    348 	    (vm_offset_t)NI_BASE + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    349 
    350 	MAPVIRT(vs_cpu, 1);
    351 	pmap_map((vm_offset_t)vs_cpu, (vm_offset_t)VS_REGS,
    352 	    (vm_offset_t)VS_REGS + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    353 
    354 	MAPVIRT(dz_regs, 2);
    355 	pmap_map((vm_offset_t)dz_regs, (vm_offset_t)DZ_CSR,
    356 	    (vm_offset_t)DZ_CSR + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    357 
    358 	MAPVIRT(lance_addr, 1);
    359 	pmap_map((vm_offset_t)lance_addr, (vm_offset_t)NI_ADDR,
    360 	    (vm_offset_t)NI_ADDR + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    361 
    362 	/* 2nd level CCR */
    363 	MAPVIRT(ka43_creg, 1);
    364 	pmap_map((vm_offset_t)ka43_creg, (vm_offset_t)KA43_CH2_CREG,
    365 	    (vm_offset_t)KA43_CH2_CREG + VAX_NBPG, VM_PROT_READ|VM_PROT_WRITE);
    366 
    367 	/* 2nd level CTA */
    368 	MAPVIRT(ka43_ctag, KA43_CT2_SIZE/VAX_NBPG);
    369 	pmap_map((vm_offset_t)ka43_ctag, (vm_offset_t)KA43_CT2_BASE,
    370 	    (vm_offset_t)KA43_CT2_BASE + KA43_CT2_SIZE,
    371 	    VM_PROT_READ|VM_PROT_WRITE);
    372 
    373 #if NNCR > 0
    374 	/* SCSI controller */
    375 	MAPVIRT(sca_regs, (KA43_SCS_SIZE / VAX_NBPG));
    376 	pmap_map((vm_offset_t)sca_regs, (vm_offset_t)KA43_SCS_BASE,
    377 	    (vm_offset_t)KA43_SCS_BASE + KA43_SCS_SIZE,
    378 	    VM_PROT_READ|VM_PROT_WRITE);
    379 
    380 	/* SCSI DMA.  Not used right now, untested. */
    381 	MAPVIRT(dma_area, (KA43_DMA_SIZE / VAX_NBPG));
    382 	pmap_map((vm_offset_t)dma_area, (vm_offset_t)KA43_DMA_BASE,
    383 	    (vm_offset_t)KA43_DMA_BASE + KA43_DMA_SIZE,
    384 	    VM_PROT_READ|VM_PROT_WRITE);
    385 #endif
    386 	/*
    387 	 * Oh holy shit! It took me over one year(!) to find out that
    388 	 * the 3100/76 has to use diag-mem instead of physical memory
    389 	 * for communication with LANCE (using phys-mem results in
    390 	 * parity errors and mchk exceptions with code 17 (0x11)).
    391 	 *
    392 	 * Many thanks to Matt Thomas, without his help it could have
    393 	 * been some more years...  ;-)
    394 	 */
    395 #define	LEMEM (((int)le_iomem & ~KERNBASE)|KA43_DIAGMEM)
    396 	MAPPHYS(le_iomem, (NI_IOSIZE/VAX_NBPG), VM_PROT_READ|VM_PROT_WRITE);
    397 	pmap_map((vm_offset_t)le_iomem, LEMEM, LEMEM + NI_IOSIZE,
    398 	    VM_PROT_READ|VM_PROT_WRITE);
    399 
    400 #if NSMG > 0
    401 	if ((vax_confdata & 0x80) == 0) {
    402 		MAPVIRT(sm_addr, (SMSIZE / VAX_NBPG));
    403 		pmap_map((vm_offset_t)sm_addr, (vm_offset_t)SMADDR,
    404 		    (vm_offset_t)SMADDR + SMSIZE, VM_PROT_READ|VM_PROT_WRITE);
    405 		((struct vs_cpu *)VS_REGS)->vc_vdcorg = 0;
    406 	}
    407 #endif
    408 
    409 	/*
    410 	 * if LANCE\'s io-buffer is above 16 MB, then the appropriate flag
    411 	 * in the parity control register has to be set (it works as an
    412 	 * additional address bit). In any case, don\'t enable CPEN and
    413 	 * DPEN in the PARCTL register, somewhow they are internally managed
    414 	 * by the RIGEL chip itself!?!
    415 	 */
    416 	val = ka43_cpu->parctl & 0x03;	/* read the old value */
    417 	if (((int)le_iomem & ~KERNBASE) > 0xffffff)
    418 		val |= KA43_PCTL_DMA;
    419 	ka43_cpu->parctl = val;		/* and write new value */
    420 
    421 	/*
    422 	 * Clear restart and boot in progress flags in the CPMBX.
    423 	 */
    424 	((struct ka43_clock *)KA43_WAT_BASE)->cpmbx =
    425 	    ((struct ka43_clock *)KA43_WAT_BASE)->cpmbx & 0xF0;
    426 
    427 #if 0
    428 	/*
    429 	 * Clear all error flags, not really neccessary here, this will
    430 	 * be done by ka43_cache_init() anyway...
    431 	 */
    432 	ka43_clear_errors();
    433 #endif
    434 
    435 	/*
    436 	 * MM is not yet enabled, thus we still used the physical addresses,
    437 	 * but before leaving this routine, we need to reset them to virtual.
    438 	 */
    439 	ka43_cpu = (void *)vs_cpu;
    440 }
    441