1 /* $NetBSD: mppb.c,v 1.7 2012/10/27 17:17:34 chs Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Radoslaw Kujawa. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* Matay Prometheus Zorro-PCI bridge driver. */ 33 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <sys/time.h> 37 #include <sys/systm.h> 38 #include <sys/errno.h> 39 #include <sys/device.h> 40 #include <sys/malloc.h> 41 #include <sys/extent.h> 42 #include <sys/kmem.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <machine/bus.h> 47 #include <machine/cpu.h> 48 49 #include <m68k/bus_dma.h> 50 #include <amiga/dev/zbusvar.h> 51 #include <amiga/pci/mppbreg.h> 52 53 #include <dev/pci/pcivar.h> 54 #include <dev/pci/pcireg.h> 55 #include <dev/pci/pcidevs.h> 56 #include <dev/pci/pciconf.h> 57 58 #include "opt_pci.h" 59 60 /* Zorro IDs */ 61 #define ZORRO_MANID_MATAY 44359 62 #define ZORRO_PRODID_PROMETHEUS 1 63 64 struct mppb_softc { 65 device_t sc_dev; 66 volatile char *ba; 67 struct bus_space_tag pci_conf_area; 68 struct bus_space_tag pci_io_area; 69 struct bus_space_tag pci_mem_area; 70 struct amiga_pci_chipset apc; 71 }; 72 73 static int mppb_match(device_t, cfdata_t, void *); 74 static void mppb_attach(device_t, device_t, void *); 75 pcireg_t mppb_pci_conf_read(pci_chipset_tag_t, pcitag_t, int); 76 void mppb_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t); 77 int mppb_pci_bus_maxdevs(pci_chipset_tag_t, int); 78 void mppb_pci_attach_hook (device_t, device_t, 79 struct pcibus_attach_args *pba); 80 pcitag_t mppb_pci_make_tag(pci_chipset_tag_t, int, int, int); 81 void mppb_pci_decompose_tag(pci_chipset_tag_t, pcitag_t, 82 int *, int *, int *); 83 int mppb_pci_intr_map(const struct pci_attach_args *, 84 pci_intr_handle_t *); 85 const struct evcnt * mppb_pci_intr_evcnt(pci_chipset_tag_t, 86 pci_intr_handle_t); 87 88 CFATTACH_DECL_NEW(mppb, sizeof(struct mppb_softc), 89 mppb_match, mppb_attach, NULL, NULL); 90 91 static int 92 mppb_match(device_t parent, cfdata_t cf, void *aux) 93 { 94 struct zbus_args *zap; 95 96 zap = aux; 97 98 if (zap->manid != ZORRO_MANID_MATAY) 99 return 0; 100 101 if (zap->prodid != ZORRO_PRODID_PROMETHEUS) 102 return 0; 103 104 return 1; 105 } 106 107 108 static void 109 mppb_attach(device_t parent, device_t self, void *aux) 110 { 111 struct mppb_softc *sc; 112 struct pcibus_attach_args pba; 113 struct zbus_args *zap; 114 pci_chipset_tag_t pc; 115 #ifdef PCI_NETBSD_CONFIGURE 116 struct extent *ioext, *memext; 117 #endif /* PCI_NETBSD_CONFIGURE */ 118 119 zap = aux; 120 sc = device_private(self); 121 pc = &sc->apc; 122 sc->sc_dev = self; 123 sc->ba = zap->va; 124 125 aprint_normal(": Matay Prometheus PCI bridge\n"); 126 127 /* Setup bus space mappings. */ 128 sc->pci_conf_area.base = (bus_addr_t) sc->ba + MPPB_CONF_BASE; 129 sc->pci_conf_area.absm = &amiga_bus_stride_1swap; 130 131 sc->pci_mem_area.base = (bus_addr_t) sc->ba + MPPB_MEM_BASE; 132 sc->pci_mem_area.absm = &amiga_bus_stride_1; 133 134 sc->pci_io_area.base = (bus_addr_t) sc->ba + MPPB_IO_BASE; 135 sc->pci_io_area.absm = &amiga_bus_stride_1; 136 137 #ifdef MPPB_DEBUG 138 aprint_normal("mppb mapped conf %x->%x, mem %x->%x\n, io %x->%x\n", 139 kvtop((void*) sc->pci_conf_area.base), sc->pci_conf_area.base, 140 kvtop((void*) sc->pci_mem_area.base), sc->pci_mem_area.base, 141 kvtop((void*) sc->pci_io_area.base), sc->pci_io_area.base); 142 #endif 143 144 sc->apc.pci_conf_datat = &(sc->pci_conf_area); 145 146 if (bus_space_map(sc->apc.pci_conf_datat, 0, MPPB_CONF_SIZE, 0, 147 &sc->apc.pci_conf_datah)) 148 aprint_error_dev(self, 149 "couldn't map PCI configuration data space\n"); 150 151 /* Initialize the PCI chipset tag. */ 152 sc->apc.pc_conf_v = (void*) pc; 153 sc->apc.pc_bus_maxdevs = mppb_pci_bus_maxdevs; 154 sc->apc.pc_make_tag = amiga_pci_make_tag; 155 sc->apc.pc_decompose_tag = amiga_pci_decompose_tag; 156 sc->apc.pc_conf_read = mppb_pci_conf_read; 157 sc->apc.pc_conf_write = mppb_pci_conf_write; 158 sc->apc.pc_attach_hook = mppb_pci_attach_hook; 159 160 sc->apc.pc_intr_map = mppb_pci_intr_map; 161 sc->apc.pc_intr_string = amiga_pci_intr_string; 162 sc->apc.pc_intr_establish = amiga_pci_intr_establish; 163 sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish; 164 165 sc->apc.pc_conf_hook = amiga_pci_conf_hook; 166 sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt; 167 168 #ifdef PCI_NETBSD_CONFIGURE 169 ioext = extent_create("mppbio", MPPB_IO_BASE, 170 MPPB_IO_BASE + MPPB_IO_SIZE, NULL, 0, EX_NOWAIT); 171 memext = extent_create("mppbmem", MPPB_MEM_BASE, 172 MPPB_MEM_BASE + MPPB_MEM_SIZE, NULL, 0, EX_NOWAIT); 173 174 #ifdef MPPB_DEBUG 175 aprint_normal("mppb: reconfiguring the bus!\n"); 176 #endif /* MPPB_DEBUG */ 177 pci_configure_bus(pc, ioext, memext, NULL, 0, CACHELINE_SIZE); 178 179 extent_destroy(ioext); 180 extent_destroy(memext); 181 #endif /* PCI_NETBSD_CONFIGURE */ 182 183 pba.pba_iot = &(sc->pci_io_area); 184 pba.pba_memt = &(sc->pci_mem_area); 185 pba.pba_dmat = NULL; 186 pba.pba_dmat64 = NULL; 187 pba.pba_pc = pc; 188 pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY; 189 pba.pba_bus = 0; 190 pba.pba_bridgetag = NULL; 191 192 config_found_ia(self, "pcibus", &pba, pcibusprint); 193 } 194 195 pcireg_t 196 mppb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg) 197 { 198 uint32_t data; 199 uint32_t bus, dev, func; 200 201 pci_decompose_tag(pc, tag, &bus, &dev, &func); 202 203 data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah, 204 (MPPB_CONF_STRIDE*dev) + reg); 205 #ifdef MPPB_DEBUG_CONF 206 aprint_normal("mppb conf read va: %lx, bus: %d, dev: %d, " 207 "func: %d, reg: %d -r-> data %x\n", 208 pc->pci_conf_datah, bus, dev, func, reg, data); 209 #endif /* MPPB_DEBUG_CONF */ 210 return data; 211 } 212 213 void 214 mppb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val) 215 { 216 uint32_t bus, dev, func; 217 218 pci_decompose_tag(pc, tag, &bus, &dev, &func); 219 220 bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah, 221 (MPPB_CONF_STRIDE*dev) + reg, val); 222 #ifdef MPPB_DEBUG_CONF 223 aprint_normal("mppb conf write va: %lx, bus: %d, dev: %d, " 224 "func: %d, reg: %d -w-> data %x\n", 225 pc->pci_conf_datah, bus, dev, func, reg, val); 226 #endif /* MPPB_DEBUG_CONF */ 227 228 } 229 230 int 231 mppb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno) 232 { 233 return 4; /* Prometheus has 4 slots */ 234 } 235 236 void 237 mppb_pci_attach_hook(device_t parent, device_t self, 238 struct pcibus_attach_args *pba) 239 { 240 } 241 242 int 243 mppb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 244 { 245 /* TODO: add sanity checking */ 246 247 *ihp = MPPB_INT; 248 return 0; 249 } 250 251 const struct evcnt * 252 mppb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih) 253 { 254 /* TODO: implement */ 255 return NULL; 256 } 257