Home | History | Annotate | Line # | Download | only in vax
ka43.c revision 1.5
      1 /*	$NetBSD: ka43.c,v 1.5 1997/04/18 18:53:38 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 
     40 #include <vm/vm.h>
     41 #include <vm/vm_kern.h>
     42 
     43 #include <machine/pte.h>
     44 #include <machine/cpu.h>
     45 #include <machine/mtpr.h>
     46 #include <machine/sid.h>
     47 #include <machine/pmap.h>
     48 #include <machine/nexus.h>
     49 #include <machine/uvax.h>
     50 #include <machine/ka43.h>
     51 #include <machine/clock.h>
     52 
     53 void	ka43_conf __P((struct device*, struct device*, void*));
     54 void	ka43_steal_pages __P((void));
     55 
     56 int	ka43_mchk __P((caddr_t));
     57 void	ka43_memerr __P((void));
     58 
     59 int	ka43_clear_errors __P((void));
     60 
     61 int	ka43_cache_init __P((void));	/* "int mapen" as argument? */
     62 int	ka43_cache_reset __P((void));
     63 int	ka43_cache_enable __P((void));
     64 int	ka43_cache_disable __P((void));
     65 int	ka43_cache_invalidate __P((void));
     66 
     67 static struct uc_map ka43_map[] = {
     68 	{ KA43_CFGTST,		KA43_CFGTST,	4,		0 },
     69 	{ KA43_ROM_BASE,	KA43_ROM_END,	KA43_ROM_SIZE,	0 },
     70 	{ KA43_CPU_BASE,	KA43_CPU_END,	KA43_CPU_SIZE,	0 },
     71 	{ KA43_CT2_BASE,	KA43_CT2_END,	KA43_CT2_SIZE,	0 },
     72 	{ KA43_CH2_CREG,	KA43_CH2_CREG,	4,		0 },
     73 	{ KA43_NWA_BASE,	KA43_NWA_END,	KA43_NWA_SIZE,	0 },
     74 	{ KA43_SER_BASE,	KA43_SER_END,	KA43_SER_SIZE,	0 },
     75 	{ KA43_WAT_BASE,	KA43_WAT_END,	KA43_WAT_SIZE,	0 },
     76 	{ KA43_SCS_BASE,	KA43_SCS_END,	KA43_SCS_SIZE,	0 },
     77 	{ KA43_LAN_BASE,	KA43_LAN_END,	KA43_LAN_SIZE,	0 },
     78 	{ KA43_CUR_BASE,	KA43_CUR_END,	KA43_CUR_SIZE,	0 },
     79 	{ KA43_DMA_BASE,	KA43_DMA_END,	KA43_DMA_SIZE,	0 },
     80 	{ KA43_VME_BASE,	KA43_VME_END,	KA43_VME_SIZE,	0 },
     81 	/*
     82 	 * there's more to come, eg. framebuffers (GPX/SPX)
     83 	 */
     84 	{0, 0, 0, 0},
     85 };
     86 
     87 struct	cpu_dep ka43_calls = {
     88 	ka43_steal_pages,
     89 	no_nicr_clock,
     90 	ka43_mchk,
     91 	ka43_memerr,
     92 	ka43_conf,
     93 	chip_clkread,
     94 	chip_clkwrite,
     95 	7,	/* 7.6 VUP */
     96 	(void*)KA43_INTREQ,
     97 	(void*)KA43_INTCLR,
     98 	(void*)KA43_INTMSK,
     99 	ka43_map,
    100 };
    101 
    102 /*
    103  * ka43_steal_pages() is called with MMU disabled, after that call MMU gets
    104  * enabled. Thus we initialize these four pointers with physical addresses,
    105  * but before leving ka43_steal_pages() we reset them to virtual addresses.
    106  */
    107 struct	ka43_cpu   *ka43_cpu	= (void*)KA43_CPU_BASE;
    108 
    109 u_int	*ka43_creg = (void*)KA43_CH2_CREG;
    110 u_int	*ka43_ctag = (void*)KA43_CT2_BASE;
    111 
    112 #define KA43_MC_RESTART	0x00008000	/* Restart possible*/
    113 #define KA43_PSL_FPDONE	0x00010000	/* First Part Done */
    114 
    115 struct ka43_mcframe {		/* Format of RigelMAX machine check frame: */
    116 	int	mc43_bcnt;	/* byte count, always 24 (0x18) */
    117 	int	mc43_code;	/* machine check type code and restart bit */
    118 	int	mc43_addr;	/* most recent (faulting?) virtual address */
    119 	int	mc43_viba;	/* contents of VIBA register */
    120 	int	mc43_sisr;	/* ICCS bit 6 and SISR bits 15:0 */
    121 	int	mc43_istate;	/* internal state */
    122 	int	mc43_sc;	/* shift count register */
    123 	int	mc43_pc;	/* trapped PC */
    124 	int	mc43_psl;	/* trapped PSL */
    125 };
    126 
    127 static char *ka43_mctype[] = {
    128 	"no error (0)",			/* Code 0: No error */
    129 	"FPA: protocol error",		/* Code 1-5: FPA errors */
    130 	"FPA: illegal opcode",
    131 	"FPA: operand parity error",
    132 	"FPA: unknown status",
    133 	"FPA: result parity error",
    134 	"unused (6)",			/* Code 6-7: Unused */
    135 	"unused (7)",
    136 	"MMU error (TLB miss)",		/* Code 8-9: MMU errors */
    137 	"MMU error (TLB hit)",
    138 	"HW interrupt at unused IPL",	/* Code 10: Interrupt error */
    139 	"MOVCx impossible state",	/* Code 11-13: Microcode errors */
    140 	"undefined trap code (i-box)",
    141 	"undefined control store address",
    142 	"unused (14)",			/* Code 14-15: Unused */
    143 	"unused (15)",
    144 	"PC tag or data parity error",	/* Code 16: Cache error */
    145 	"data bus parity error",	/* Code 17: Read error */
    146 	"data bus error (NXM)",		/* Code 18: Write error */
    147 	"undefined data bus state",	/* Code 19: Bus error */
    148 };
    149 #define MC43_MAX	19
    150 
    151 static int ka43_error_count = 0;
    152 
    153 int
    154 ka43_mchk(addr)
    155 	caddr_t addr;
    156 {
    157 	register struct ka43_mcframe *mcf = (void*)addr;
    158 
    159 	mtpr(0x00, PR_MCESR);	/* Acknowledge the machine check */
    160 	printf("machine check %d (0x%x)\n", mcf->mc43_code, mcf->mc43_code);
    161 	printf("reason: %s\n", ka43_mctype[mcf->mc43_code & 0xff]);
    162 	if (++ka43_error_count > 10) {
    163 		printf("error_count exceeded: %d\n", ka43_error_count);
    164 		return (-1);
    165 	}
    166 
    167 	/*
    168 	 * If either the Restart flag is set or the First-Part-Done flag
    169 	 * is set, and the TRAP2 (double error) bit is not set, the the
    170 	 * error is recoverable.
    171 	 */
    172 	if (mfpr(PR_PCSTS) & KA43_PCS_TRAP2) {
    173 		printf("TRAP2 (double error) in ka43_mchk.\n");
    174 		panic("unrecoverable state in ka43_mchk.\n");
    175 		return (-1);
    176 	}
    177 	if ((mcf->mc43_code & KA43_MC_RESTART) ||
    178 	    (mcf->mc43_psl & KA43_PSL_FPDONE)) {
    179 		printf("ka43_mchk: recovering from machine-check.\n");
    180 		ka43_cache_reset();	/* reset caches */
    181 		return (0);		/* go on; */
    182 	}
    183 
    184 	/*
    185 	 * Unknown error state, panic/halt the machine!
    186 	 */
    187 	printf("ka43_mchk: unknown error state!\n");
    188 	return (-1);
    189 }
    190 
    191 void
    192 ka43_memerr()
    193 {
    194 	/*
    195 	 * Don\'t know what to do here. So just print some messages
    196 	 * and try to go on...
    197 	 */
    198 	printf("memory error!\n");
    199 	printf("primary cache status: %b\n", mfpr(PR_PCSTS), KA43_PCSTS_BITS);
    200 	printf("secondary cache status: %b\n", *ka43_creg, KA43_SESR_BITS);
    201 }
    202 
    203 int
    204 ka43_cache_init()
    205 {
    206 	return (ka43_cache_reset());
    207 }
    208 
    209 int
    210 ka43_clear_errors()
    211 {
    212 	int val = *ka43_creg;
    213 	val |= KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR;
    214 	*ka43_creg = val;
    215 }
    216 
    217 int
    218 ka43_cache_reset()
    219 {
    220 	/*
    221 	 * resetting primary and secondary caches is done in three steps:
    222 	 *	1. disable both caches
    223 	 *	2. manually clear secondary cache
    224 	 *	3. enable both caches
    225 	 */
    226 	ka43_cache_disable();
    227 	ka43_cache_invalidate();
    228 	ka43_cache_enable();
    229 
    230 	printf("primary cache status: %b\n", mfpr(PR_PCSTS), KA43_PCSTS_BITS);
    231 	printf("secondary cache status: %b\n", *ka43_creg, KA43_SESR_BITS);
    232 	printf("cpu status: parctl=0x%x, hltcod=0x%x\n",
    233 	       ka43_cpu->parctl, ka43_cpu->hltcod);
    234 
    235 	return (0);
    236 }
    237 
    238 int
    239 ka43_cache_disable()
    240 {
    241 	int i, val;
    242 
    243 	/*
    244 	 * first disable primary cache and clear error flags
    245 	 */
    246 	mtpr(KA43_PCS_REFRESH, PR_PCSTS);	/* disable primary cache */
    247 	val = mfpr(PR_PCSTS);
    248 	mtpr(val, PR_PCSTS);			/* clear error flags */
    249 
    250 	/*
    251 	 * now disable secondary cache and clear error flags
    252 	 */
    253 	val = *ka43_creg & ~KA43_SESR_CENB;	/* BICL !!! */
    254 	*ka43_creg = val;			/* disable secondary cache */
    255 	val = KA43_SESR_SERR | KA43_SESR_LERR | KA43_SESR_CERR;
    256 	*ka43_creg = val;			/* clear error flags */
    257 
    258 	return (0);
    259 }
    260 
    261 int
    262 ka43_cache_invalidate()
    263 {
    264 	int i, val;
    265 
    266 	val = KA43_PCTAG_PARITY;	/* clear valid flag, set parity bit */
    267 	for (i = 0; i < 256; i++) {	/* 256 Quadword entries */
    268 		mtpr(i*8, PR_PCIDX);	/* write index of tag */
    269 		mtpr(val, PR_PCTAG);	/* write value into tag */
    270 	}
    271 	val = KA43_PCS_FLUSH | KA43_PCS_REFRESH;
    272 	mtpr(val, PR_PCSTS);		/* flush primary cache */
    273 
    274 	/*
    275 	 * Rigel\'s secondary cache doesn\'t implement a valid-flag.
    276 	 * Thus we initialize all entries with out-of-range/dummy
    277 	 * addresses which will never be referenced (ie. never hit).
    278 	 * After enabling cache we also access 128K of memory starting
    279 	 * at 0x00 so that secondary cache will be filled with these
    280 	 * valid addresses...
    281 	 */
    282 	val = 0xff;
    283 	/* if (memory > 28 MB) val = 0x55; */
    284 	printf("clearing tags...\n");
    285 	for (i = 0; i < KA43_CT2_SIZE; i+= 4) {	/* Quadword entries ?? */
    286 		ka43_ctag[i/4] = val;		/* reset upper and lower */
    287 	}
    288 
    289 	return (0);
    290 }
    291 
    292 
    293 int
    294 ka43_cache_enable()
    295 {
    296 	volatile char *membase = (void*)0x80000000;	/* physical 0x00 */
    297 	int i, val;
    298 
    299 	val = KA43_PCS_FLUSH | KA43_PCS_REFRESH;
    300 	mtpr(val, PR_PCSTS);		/* flush primary cache */
    301 
    302 	/*
    303 	 * now we enable secondary cache and access first 128K of memory
    304 	 * so that secondary cache gets really initialized and holds
    305 	 * valid addresses/data...
    306 	 */
    307 	*ka43_creg = KA43_SESR_CENB;	/* enable secondary cache */
    308 	for (i=0; i<128*1024; i++) {
    309 		val += membase[i];	/* some dummy operation... */
    310 	}
    311 
    312 	val = KA43_PCS_ENABLE | KA43_PCS_REFRESH;
    313 	mtpr(val, PR_PCSTS);		/* enable primary cache */
    314 
    315 	return (0);
    316 }
    317 
    318 void
    319 ka43_conf(parent, self, aux)
    320 	struct	device *parent, *self;
    321 	void	*aux;
    322 {
    323 	extern char cpu_model[];
    324 	extern int vax_siedata;
    325 
    326 	if (vax_siedata & 0x02)		/* "single-user" flag */
    327 		strcpy(cpu_model,"VAXstation 3100 model 76");
    328 	else if (vax_siedata & 0x01)	/* "multiuser" flag */
    329 		strcpy(cpu_model,"MicroVAX 3100 model 76(?)");
    330 	else
    331 		strcpy(cpu_model, "unknown KA43 board");
    332 
    333 	printf(": %s\n", cpu_model);
    334 
    335 	/*
    336 	 * ka43_conf() gets called with MMU enabled, now it's save to
    337 	 * init/reset the caches.
    338 	 */
    339 	ka43_cache_init();
    340 }
    341 
    342 
    343 /*
    344  * The interface for communication with the LANCE ethernet controller
    345  * is setup in the xxx_steal_pages() routine. We decrease highest
    346  * available address by 64K and use this area as communication buffer.
    347  */
    348 u_long le_iomem;		/* base addr of RAM -- CPU's view */
    349 u_long le_ioaddr;		/* base addr of RAM -- LANCE's view */
    350 
    351 void
    352 ka43_steal_pages()
    353 {
    354 	extern	vm_offset_t avail_start, virtual_avail, avail_end;
    355         extern  short *clk_page;
    356         extern  int clk_adrshift, clk_tweak;
    357 	int	junk, val;
    358 	int	i;
    359 
    360 	printf ("ka43_steal_pages: avail_end=0x%x\n", avail_end);
    361 
    362 	/*
    363 	 * SCB is already copied/initialized at addr avail_start
    364 	 * by pmap_bootstrap(), but it's not yet mapped. Thus we use
    365 	 * the MAPPHYS() macro to reserve these two pages and to
    366 	 * perform the mapping. The mapped address is assigned to junk.
    367 	 */
    368 	MAPPHYS(junk, 2, VM_PROT_READ|VM_PROT_WRITE);
    369 
    370         clk_adrshift = 1;       /* Addressed at long's... */
    371         clk_tweak = 2;          /* ...and shift two */
    372         MAPVIRT(clk_page, 2);
    373         pmap_map((vm_offset_t)clk_page, (vm_offset_t)KA43_WAT_BASE,
    374             (vm_offset_t)KA43_WAT_BASE + NBPG, VM_PROT_READ|VM_PROT_WRITE);
    375 
    376 #if 0
    377 	/*
    378 	 * At top of physical memory there are some console-prom and/or
    379 	 * restart-specific data. Make this area unavailable.
    380 	 */
    381 	avail_end -= 64 * NBPG;		/* scratch RAM ??? */
    382 	avail_end = 0x00FC0000;		/* XXX: for now from ">>> show mem" */
    383 
    384 This is no longer neccessary since the memsize in RPB does not include
    385 these unavailable pages. Only valid/available pages are counted in RPB.
    386 
    387 #endif
    388 
    389 	/*
    390 	 * If we need to map physical areas also, we can decrease avail_end
    391 	 * (the highest available memory-address), copy the stuff into the
    392 	 * gap between and use pmap_map to map it. This is done for LANCE's
    393 	 * 64K communication area.
    394 	 *
    395 	 * Don't use the MAPPHYS macro here, since this uses and changes(!)
    396 	 * the value of avail_start. Use MAPVIRT even if it's name misleads.
    397 	 */
    398 	avail_end -= (64 * 1024);	/* reserve 64K */
    399 	avail_end &= ~0xffff;		/* force proper (quad?) alignment */
    400 
    401 	/*
    402 	 * Oh holy shit! It took me over one year(!) to find out that
    403 	 * the 3100/76 has to use diag-mem instead of physical memory
    404 	 * for communication with LANCE (using phys-mem results in
    405 	 * parity errors and mchk exceptions with code 17 (0x11)).
    406 	 *
    407 	 * Many thanks to Matt Thomas, without his help it could have
    408 	 * been some more years...  ;-)
    409 	 */
    410 	le_ioaddr = avail_end | KA43_DIAGMEM;	/* ioaddr in diag-mem!!! */
    411 	MAPVIRT(le_iomem, (64 * 1024)/NBPG);
    412 	pmap_map((vm_offset_t)le_iomem, le_ioaddr, le_ioaddr + 0xffff,
    413 		 VM_PROT_READ|VM_PROT_WRITE);
    414 
    415 	/*
    416 	 * if LANCE\'s io-buffer is above 16 MB, then the appropriate flag
    417 	 * in the parity control register has to be set (it works as an
    418 	 * additional address bit). In any case, don\'t enable CPEN and
    419 	 * DPEN in the PARCTL register, somewhow they are internally managed
    420 	 * by the RIGEL chip itself!?!
    421 	 */
    422 	val = ka43_cpu->parctl & 0x03;	/* read the old value */
    423 	if (le_ioaddr & (1 << 24))	/* if RAM above 16 MB */
    424 		val |= KA43_PCTL_DMA;	/* set LANCE DMA flag */
    425 	ka43_cpu->parctl = val;		/* and write new value */
    426 	le_ioaddr &= 0xffffff;		/* Lance uses 24-bit addresses */
    427 
    428 	/*
    429 	 * now map in anything listed in ka43_map...
    430 	 */
    431 	uvax_fillmap();
    432 
    433 	/*
    434 	 * Clear restart and boot in progress flags in the CPMBX.
    435 	 */
    436 	((struct ka43_clock *)KA43_WAT_BASE)->cpmbx =
    437 	    ((struct ka43_clock *)KA43_WAT_BASE)->cpmbx & 0xF0;
    438 
    439 #if 0
    440 	/*
    441 	 * Clear all error flags, not really neccessary here, this will
    442 	 * be done by ka43_cache_init() anyway...
    443 	 */
    444 	ka43_clear_errors();
    445 #endif
    446 
    447 	/*
    448 	 * MM is not yet enabled, thus we still used the physical addresses,
    449 	 * but before leaving this routine, we need to reset them to virtual.
    450 	 */
    451 	ka43_cpu    = (void*)uvax_phys2virt(KA43_CPU_BASE);
    452 	ka43_creg   = (void*)uvax_phys2virt(KA43_CH2_CREG);
    453 	ka43_ctag   = (void*)uvax_phys2virt(KA43_CT2_BASE);
    454 }
    455