1 /* $NetBSD: mppb.c,v 1.6 2012/01/29 15:32:52 para 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(struct device *, struct cfdata *, void *); 74 static void mppb_attach(struct device *, struct device *, 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 pc, int busno); 78 void mppb_pci_attach_hook (struct device *parent, 79 struct device *self, struct pcibus_attach_args *pba); 80 pcitag_t mppb_pci_make_tag(pci_chipset_tag_t pc, int bus, int device, 81 int function); 82 void mppb_pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, 83 int *bp, int *dp, int *fp); 84 int mppb_pci_intr_map(const struct pci_attach_args *pa, 85 pci_intr_handle_t *ihp); 86 const struct evcnt * mppb_pci_intr_evcnt(pci_chipset_tag_t pc, 87 pci_intr_handle_t ih); 88 89 CFATTACH_DECL_NEW(mppb, sizeof(struct mppb_softc), 90 mppb_match, mppb_attach, NULL, NULL); 91 92 static int 93 mppb_match(device_t parent, cfdata_t cf, void *aux) 94 { 95 struct zbus_args *zap; 96 97 zap = aux; 98 99 if (zap->manid != ZORRO_MANID_MATAY) 100 return 0; 101 102 if (zap->prodid != ZORRO_PRODID_PROMETHEUS) 103 return 0; 104 105 return 1; 106 } 107 108 109 static void 110 mppb_attach(device_t parent, device_t self, void *aux) 111 { 112 struct mppb_softc *sc; 113 struct pcibus_attach_args pba; 114 struct zbus_args *zap; 115 pci_chipset_tag_t pc; 116 #ifdef PCI_NETBSD_CONFIGURE 117 struct extent *ioext, *memext; 118 #endif /* PCI_NETBSD_CONFIGURE */ 119 120 zap = aux; 121 sc = device_private(self); 122 pc = &sc->apc; 123 sc->sc_dev = self; 124 sc->ba = zap->va; 125 126 aprint_normal(": Matay Prometheus PCI bridge\n"); 127 128 /* Setup bus space mappings. */ 129 sc->pci_conf_area.base = (bus_addr_t) sc->ba + MPPB_CONF_BASE; 130 sc->pci_conf_area.absm = &amiga_bus_stride_1swap; 131 132 sc->pci_mem_area.base = (bus_addr_t) sc->ba + MPPB_MEM_BASE; 133 sc->pci_mem_area.absm = &amiga_bus_stride_1; 134 135 sc->pci_io_area.base = (bus_addr_t) sc->ba + MPPB_IO_BASE; 136 sc->pci_io_area.absm = &amiga_bus_stride_1; 137 138 #ifdef MPPB_DEBUG 139 aprint_normal("mppb mapped conf %x->%x, mem %x->%x\n, io %x->%x\n", 140 kvtop((void*) sc->pci_conf_area.base), sc->pci_conf_area.base, 141 kvtop((void*) sc->pci_mem_area.base), sc->pci_mem_area.base, 142 kvtop((void*) sc->pci_io_area.base), sc->pci_io_area.base); 143 #endif 144 145 sc->apc.pci_conf_datat = &(sc->pci_conf_area); 146 147 if (bus_space_map(sc->apc.pci_conf_datat, 0, MPPB_CONF_SIZE, 0, 148 &sc->apc.pci_conf_datah)) 149 aprint_error_dev(self, 150 "couldn't map PCI configuration data space\n"); 151 152 /* Initialize the PCI chipset tag. */ 153 sc->apc.pc_conf_v = (void*) pc; 154 sc->apc.pc_bus_maxdevs = mppb_pci_bus_maxdevs; 155 sc->apc.pc_make_tag = amiga_pci_make_tag; 156 sc->apc.pc_decompose_tag = amiga_pci_decompose_tag; 157 sc->apc.pc_conf_read = mppb_pci_conf_read; 158 sc->apc.pc_conf_write = mppb_pci_conf_write; 159 sc->apc.pc_attach_hook = mppb_pci_attach_hook; 160 161 sc->apc.pc_intr_map = mppb_pci_intr_map; 162 sc->apc.pc_intr_string = amiga_pci_intr_string; 163 sc->apc.pc_intr_establish = amiga_pci_intr_establish; 164 sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish; 165 166 sc->apc.pc_conf_hook = amiga_pci_conf_hook; 167 sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt; 168 169 #ifdef PCI_NETBSD_CONFIGURE 170 ioext = extent_create("mppbio", MPPB_IO_BASE, 171 MPPB_IO_BASE + MPPB_IO_SIZE, NULL, 0, EX_NOWAIT); 172 memext = extent_create("mppbmem", MPPB_MEM_BASE, 173 MPPB_MEM_BASE + MPPB_MEM_SIZE, NULL, 0, EX_NOWAIT); 174 175 #ifdef MPPB_DEBUG 176 aprint_normal("mppb: reconfiguring the bus!\n"); 177 #endif /* MPPB_DEBUG */ 178 pci_configure_bus(pc, ioext, memext, NULL, 0, CACHELINE_SIZE); 179 180 extent_destroy(ioext); 181 extent_destroy(memext); 182 #endif /* PCI_NETBSD_CONFIGURE */ 183 184 pba.pba_iot = &(sc->pci_io_area); 185 pba.pba_memt = &(sc->pci_mem_area); 186 pba.pba_dmat = NULL; 187 pba.pba_dmat64 = NULL; 188 pba.pba_pc = pc; 189 pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY; 190 pba.pba_bus = 0; 191 pba.pba_bridgetag = NULL; 192 193 config_found_ia(self, "pcibus", &pba, pcibusprint); 194 } 195 196 pcireg_t 197 mppb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg) 198 { 199 uint32_t data; 200 uint32_t bus, dev, func; 201 202 pci_decompose_tag(pc, tag, &bus, &dev, &func); 203 204 data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah, 205 (MPPB_CONF_STRIDE*dev) + reg); 206 #ifdef MPPB_DEBUG_CONF 207 aprint_normal("mppb conf read va: %lx, bus: %d, dev: %d, " 208 "func: %d, reg: %d -r-> data %x\n", 209 pc->pci_conf_datah, bus, dev, func, reg, data); 210 #endif /* MPPB_DEBUG_CONF */ 211 return data; 212 } 213 214 void 215 mppb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val) 216 { 217 uint32_t bus, dev, func; 218 219 pci_decompose_tag(pc, tag, &bus, &dev, &func); 220 221 bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah, 222 (MPPB_CONF_STRIDE*dev) + reg, val); 223 #ifdef MPPB_DEBUG_CONF 224 aprint_normal("mppb conf write va: %lx, bus: %d, dev: %d, " 225 "func: %d, reg: %d -w-> data %x\n", 226 pc->pci_conf_datah, bus, dev, func, reg, val); 227 #endif /* MPPB_DEBUG_CONF */ 228 229 } 230 231 int 232 mppb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno) 233 { 234 return 4; /* Prometheus has 4 slots */ 235 } 236 237 void 238 mppb_pci_attach_hook(struct device *parent, struct device *self, 239 struct pcibus_attach_args *pba) 240 { 241 } 242 243 int 244 mppb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 245 { 246 /* TODO: add sanity checking */ 247 248 *ihp = MPPB_INT; 249 return 0; 250 } 251 252 const struct evcnt * 253 mppb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih) 254 { 255 /* TODO: implement */ 256 return NULL; 257 } 258 259