2 * Copyright (c) 2000 Andrew Gallatin & Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Portions of this file were obtained from Compaq intellectual
27 * property which was made available under the following copyright:
29 * *****************************************************************
31 * * Copyright Compaq Computer Corporation, 2000 *
33 * * Permission to use, copy, modify, distribute, and sell *
34 * * this software and its documentation for any purpose is *
35 * * hereby granted without fee, provided that the above *
36 * * copyright notice appear in all copies and that both *
37 * * that copyright notice and this permission notice appear *
38 * * in supporting documentation, and that the name of *
39 * * Compaq Computer Corporation not be used in advertising *
40 * * or publicity pertaining to distribution of the software *
41 * * without specific, written prior permission. Compaq *
42 * * makes no representations about the suitability of this *
43 * * software for any purpose. It is provided "AS IS" *
44 * * without express or implied warranty. *
46 * *****************************************************************
50 * T2 CBUS to PCI bridge
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #define __RMAN_RESOURCE_VISIBLE
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
61 #include <sys/malloc.h>
62 #include <sys/module.h>
63 #include <sys/mutex.h>
65 #include <machine/bus.h>
68 #include <sys/interrupt.h>
70 #include <dev/pci/pcivar.h>
71 #include <alpha/pci/t2reg.h>
72 #include <alpha/pci/t2var.h>
73 #include <alpha/isa/isavar.h>
75 #include <machine/cpuconf.h>
76 #include <machine/intr.h>
77 #include <machine/intrcnt.h>
78 #include <machine/md_var.h>
79 #include <machine/resource.h>
80 #include <machine/sgmap.h>
81 #include <machine/swiz.h>
84 #include <vm/vm_page.h>
86 #define KV(pa) ALPHA_PHYS_TO_K0SEG(pa + sable_lynx_base)
88 vm_offset_t sable_lynx_base = 0UL;
90 volatile t2_csr_t *t2_csr[2];
91 static int pci_int_type[2];
93 static devclass_t t2_devclass;
94 static device_t t2_0; /* XXX only one for now */
100 #define T2_SOFTC(dev) (struct t2_softc*) device_get_softc(dev)
102 static alpha_chipset_read_hae_t t2_read_hae;
103 static alpha_chipset_write_hae_t t2_write_hae;
105 static alpha_chipset_t t2_chipset = {
110 static u_int32_t t2_hae_mem[2];
112 #define REG1 (1UL << 24)
115 t2_set_hae_mem(void *arg, u_int32_t pa)
124 msb = pa & 0xf8000000;
126 msb >>= 27; /* t2 puts high bits in the bottom of the register */
128 if (msb != t2_hae_mem[hose]) {
129 t2_hae_mem[hose] = msb;
130 t2_csr[hose]->hae0_1 = t2_hae_mem[hose];
132 t2_hae_mem[hose] = t2_csr[hose]->hae0_1;
142 return t2_hae_mem[0] << 27;
146 t2_write_hae(u_int64_t hae)
149 t2_set_hae_mem(0, pa);
152 static int t2_probe(device_t dev);
153 static int t2_attach(device_t dev);
154 static int t2_setup_intr(device_t dev, device_t child,
155 struct resource *irq, int flags,
156 void *intr, void *arg, void **cookiep);
157 static int t2_teardown_intr(device_t dev, device_t child,
158 struct resource *irq, void *cookie);
160 t2_dispatch_intr(void *frame, unsigned long vector);
162 t2_machine_check(unsigned long mces, struct trapframe *framep,
163 unsigned long vector, unsigned long param);
166 static device_method_t t2_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_probe, t2_probe),
169 DEVMETHOD(device_attach, t2_attach),
172 DEVMETHOD(bus_setup_intr, t2_setup_intr),
173 DEVMETHOD(bus_teardown_intr, t2_teardown_intr),
178 static driver_t t2_driver = {
181 sizeof(struct t2_softc),
185 #define T2_SGMAP_BASE (8*1024*1024)
186 #define T2_SGMAP_SIZE (8*1024*1024)
189 t2_sgmap_invalidate(void)
194 val = REGVAL64(T2_IOCSR);
195 val |= T2_IOCSRL_ITLB;
196 REGVAL64(T2_IOCSR) = val;
199 val = REGVAL64(T2_IOCSR);
200 val &= ~T2_IOCSRL_ITLB;
201 REGVAL64(T2_IOCSR) = val;
207 t2_sgmap_map(void *arg, bus_addr_t ba, vm_offset_t pa)
209 u_int64_t *sgtable = arg;
210 int index = alpha_btop(ba - T2_SGMAP_BASE);
214 panic("t2_sgmap_map: can't map address 0x%lx", pa);
215 sgtable[index] = ((pa >> 13) << 1) | 1;
220 t2_sgmap_invalidate();
230 * First setup Window 2 to map 8Mb to 16Mb with an
231 * sgmap. Allocate the map aligned to a 32 boundary.
233 * bits 31..20 of WBASE represent the pci start address
234 * (in units of 1Mb), and bits 11..0 represent the pci
237 t2_csr[h]->wbase2 = T2_WSIZE_8M|T2_WINDOW_ENABLE|T2_WINDOW_SG
238 | ((T2_SGMAP_BASE >> 20) << 20)
239 | ((T2_SGMAP_BASE + T2_SGMAP_SIZE) >> 20);
240 t2_csr[h]->wmask2 = T2_WMASK_8M;
243 sgtable = contigmalloc(8192, M_DEVBUF, M_NOWAIT,
247 panic("t2_init_sgmap: can't allocate page table");
250 (pmap_kextract((vm_offset_t) sgtable) >> T2_TBASE_SHIFT);
252 chipset.sgmap = sgmap_map_create(T2_SGMAP_BASE,
253 T2_SGMAP_BASE + T2_SGMAP_SIZE,
254 t2_sgmap_map, sgtable);
261 * initialize the DMA windows
263 t2_csr[h]->wbase1 = T2_WSIZE_1G|T2_WINDOW_ENABLE|T2_WINDOW_DIRECT|0x7ff;
264 t2_csr[h]->wmask1 = T2_WMASK_1G;
265 t2_csr[h]->tbase1 = 0x0;
267 t2_csr[h]->wbase2 = 0x0;
270 * enable the PCI "Hole" for ISA devices which use memory in
271 * the 512k - 1MB range
273 t2_csr[h]->hbase = 1 << 13;
276 /* initialize the HAEs */
277 t2_csr[h]->hae0_1 = 0x0;
279 t2_csr[h]->hae0_2 = 0x0;
281 t2_csr[h]->hae0_3 = 0x0;
287 * Perform basic chipset init/fixup. Called by various early
288 * consumers to ensure that the system will work before the
289 * bus methods are invoked.
296 static int initted = 0;
297 static struct swiz_space io_space, mem_space;
302 swiz_init_space(&io_space, KV(T2_PCI_SIO));
303 swiz_init_space_hae(&mem_space, KV(T2_PCI_SPARSE),
306 busspace_isa_io = (struct alpha_busspace *) &io_space;
307 busspace_isa_mem = (struct alpha_busspace *) &mem_space;
309 chipset = t2_chipset;
314 t2_probe(device_t dev)
316 int h, t2_num_hoses = 1;
323 device_set_desc(dev, "T2 Core Logic chipset");
324 t2_csr[0] = (t2_csr_t *)
325 ALPHA_PHYS_TO_K0SEG(sable_lynx_base + PCI0_BASE);
326 t2_csr[1] = (t2_csr_t *)
327 ALPHA_PHYS_TO_K0SEG(sable_lynx_base + PCI1_BASE);
329 /* Look at the rev of the chip. If the high bit is set in the
330 * rev field then we have either a T3 or a T4 chip, so use the
331 * new interrupt structure. If it is clear, then we have a T2
332 * so use the old way */
334 platform.mcheck_handler = t2_machine_check;
336 if (((t2_csr[0]->iocsr) >> 35) & 1)
341 device_printf(dev, "using interrupt type %d on pci bus 0\n",
344 if (!badaddr(__DEVOLATILE(void *, &t2_csr[1]->tlbbr), sizeof(long))) {
345 pci_int_type[1] = 1; /* PCI1 always uses the new scheme */
346 /* Clear any errors that the BADADDR probe may have caused */
347 t2_csr[1]->cerr1 |= t2_csr[1]->cerr1;
348 t2_csr[1]->pcierr1 |= t2_csr[1]->pcierr1;
349 device_printf(dev, "found EXT_IO!!!!!\n");
350 /* t2_num_hoses = 2; XXX not ready for this yet */
353 for (h = 0; h < t2_num_hoses; h++)
357 child = device_add_child(dev, "pcib", 0);
358 device_set_ivars(child, 0);
364 t2_attach(device_t dev)
368 set_iointr(t2_dispatch_intr);
369 platform.isa_setup_intr = t2_setup_intr;
370 platform.isa_teardown_intr = t2_teardown_intr;
372 snprintf(chipset_type, sizeof(chipset_type), "t2");
374 bus_generic_attach(dev);
381 * magical mystery table partly obtained from Linux
382 * at least some of their values for PCI masks
383 * were incorrect, and I've filled in my own extrapolations
384 * XXX this needs more testers
387 unsigned long t2_shadow_mask = -1L;
388 static const char irq_to_mask[40] = {
389 -1, 6, -1, 8, 15, 12, 7, 9, /* ISA 0-7 */
390 -1, 16, 17, 18, 3, -1, 21, 22, /* ISA 8-15 */
391 -1, -1, -1, -1, -1, -1, -1, -1, /* ?? EISA XXX */
392 -1, -1, -1, -1, -1, -1, -1, -1, /* ?? EISA XXX */
393 0, 1, 2, 3, 4, 5, 6, 7 /* PCI 0-7 XXX */
398 t2_8259_disable_mask(int mask)
400 t2_shadow_mask |= (1UL << mask);
403 outb(SLAVE0_ICU, t2_shadow_mask);
405 outb(SLAVE1_ICU, t2_shadow_mask >> 8);
407 outb(SLAVE2_ICU, t2_shadow_mask >> 16);
411 t2_8259_enable_mask(int mask)
413 t2_shadow_mask &= ~(1UL << mask);
416 outb(SLAVE0_ICU, t2_shadow_mask);
418 outb(SLAVE1_ICU, t2_shadow_mask >> 8);
420 outb(SLAVE2_ICU, t2_shadow_mask >> 16);
429 hose = (vector >= 0xC00);
430 irq = (vector - 0x800) >> 4;
432 if (pci_int_type[hose]) {
434 /* New interrupt scheme. Both PCI0 and PCI1 can use
435 * the same handler. Dispatching interrupts with the
436 * IC IC chip is easy. We simply write the vector
437 * address register (var) on the T3/T4 (offset
438 * 0x480) with the IRQ level (0 - 63) of what came in. */
439 t2_csr[hose]->var = (u_long) irq;
445 outb(SLAVE0_ICU-1, (0xe0 | (irq)));
446 outb(MASTER_ICU-1, (0xe0 | 1));
449 outb(SLAVE1_ICU-1, (0xe0 | (irq - 8)));
450 outb(MASTER_ICU-1, (0xe0 | 3));
453 outb(SLAVE2_ICU-1, (0xe0 | (irq - 16)));
454 outb(MASTER_ICU-1, (0xe0 | 4));
461 t2_enable_vec(uintptr_t vector)
464 u_long IC_mask, scratch;
466 hose = (vector >= 0xC00);
467 irq = (vector - 0x800) >> 4;
469 mtx_lock_spin(&icu_lock);
470 if (pci_int_type[hose]) {
472 /* Write the air register on the T3/T4 with the
473 * address of the IC IC masks register (offset 0x40) */
474 t2_csr[hose]->air = 0x40;
476 scratch = t2_csr[hose]->air;
478 IC_mask = t2_csr[hose]->dir;
479 IC_mask &= ~(1L << ( (u_long) irq));
480 t2_csr[hose]->dir = IC_mask;
484 * EOI the interrupt we just enabled.
488 /* Old style 8259 (Gack!!!) interrupts */
489 t2_8259_enable_mask(irq);
491 mtx_unlock_spin(&icu_lock);
495 t2_disable_vec(uintptr_t vector)
498 u_long scratch, IC_mask;
500 hose = (vector >= 0xC00);
501 irq = (vector - 0x800) >> 4;
503 mtx_lock_spin(&icu_lock);
504 if (pci_int_type[hose]) {
506 /* Write the air register on the T3/T4 wioth the
507 * address of the IC IC masks register (offset 0x40) */
509 t2_csr[hose]->air = 0x40;
511 scratch = t2_csr[hose]->air;
514 * Read the dir register to fetch the mask data, 'or' in the
515 * new disable bit, and write the data back.
517 IC_mask = t2_csr[hose]->dir;
518 IC_mask |= (1L << ( (u_long) irq));
519 /* Set the disable bit */
520 t2_csr[hose]->dir = IC_mask;
524 /* Old style 8259 (Gack!!!) interrupts */
525 t2_8259_disable_mask(irq);
527 mtx_unlock_spin(&icu_lock);
532 t2_setup_intr(device_t dev, device_t child,
533 struct resource *irq, int flags,
534 void *intr, void *arg, void **cookiep)
536 int error, vector, stdio_irq;
538 device_t bus, parent;
540 name = device_get_nameunit(dev);
541 stdio_irq = irq->r_start;
542 if (strncmp(name, "eisa", 4) == 0) {
543 if ((stdio_irq != 6 ) && (stdio_irq != 3 )) {
545 T2_EISA_IRQ_TO_STDIO_IRQ(stdio_irq);
547 } else if ((strncmp(name, "isa", 3)) == 0) {
548 stdio_irq = irq_to_mask[stdio_irq];
554 parent = device_get_parent(bus);
555 } while (parent && strncmp("t2", device_get_nameunit(parent), 2));
557 if (parent && (device_get_unit(bus) != 0))
558 vector = STDIO_PCI1_IRQ_TO_SCB_VECTOR(stdio_irq);
560 vector = STDIO_PCI0_IRQ_TO_SCB_VECTOR(stdio_irq);
562 error = rman_activate_resource(irq);
566 error = alpha_setup_intr(device_get_nameunit(child ? child : dev),
567 vector, intr, arg, flags, cookiep,
568 &intrcnt[irq->r_start], t2_disable_vec, t2_enable_vec);
573 /* Enable interrupt */
574 t2_enable_vec(vector);
576 if (bootverbose != 0)
578 "interrupting at T2 irq %d (stdio irq %d)\n",
579 (int) irq->r_start, stdio_irq);
584 t2_teardown_intr(device_t dev, device_t child,
585 struct resource *irq, void *cookie)
589 mask = irq_to_mask[irq->r_start];
591 /* Disable interrupt */
594 * XXX this is totally broken!
595 * we don't have enough info to figure out where the interrupt
596 * came from if hose != 0 and pci_int_type[hose] != 0
597 * We should probably carry around the vector someplace --
598 * that would be enough to figure out the hose and the stdio irq
601 t2_shadow_mask |= (1UL << mask);
603 mtx_lock_spin(&icu_lock);
605 outb(SLAVE0_ICU, t2_shadow_mask);
607 outb(SLAVE1_ICU, t2_shadow_mask >> 8);
609 outb(SLAVE2_ICU, t2_shadow_mask >> 16);
610 mtx_unlock_spin(&icu_lock);
612 alpha_teardown_intr(cookie);
613 return rman_deactivate_resource(irq);
619 t2_dispatch_intr(void *frame, unsigned long vector)
621 alpha_dispatch_intr(frame, vector);
622 mtx_lock_spin(&icu_lock);
624 mtx_unlock_spin(&icu_lock);
628 t2_machine_check(unsigned long mces, struct trapframe *framep,
629 unsigned long vector, unsigned long param)
633 expected = mc_expected;
634 machine_check(mces, framep, vector, param);
635 /* for some reason the alpha_pal_wrmces() doesn't clear all
636 pending machine checks & we may take another */
637 mc_expected = expected;
640 DRIVER_MODULE(t2, root, t2_driver, t2_devclass, 0, 0);