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