1 /* $NetBSD: ofwpci.c,v 1.4 2007/10/26 00:34:53 garbled Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tim Rightnour 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: ofwpci.c,v 1.4 2007/10/26 00:34:53 garbled Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/device.h> 44 #include <sys/malloc.h> 45 #include <sys/systm.h> 46 47 #include <dev/pci/pcivar.h> 48 #include <dev/ofw/openfirm.h> 49 #include <dev/ofw/ofw_pci.h> 50 51 #include <machine/autoconf.h> 52 #include <machine/pio.h> 53 54 struct ofwpci_softc { 55 struct device sc_dev; 56 struct genppc_pci_chipset sc_pc; 57 struct powerpc_bus_space sc_iot; 58 struct powerpc_bus_space sc_memt; 59 }; 60 61 static void ofwpci_attach(struct device *, struct device *, void *); 62 static int ofwpci_match(struct device *, struct cfdata *, void *); 63 64 CFATTACH_DECL(ofwpci, sizeof(struct ofwpci_softc), 65 ofwpci_match, ofwpci_attach, NULL, NULL); 66 67 extern struct genppc_pci_chipset *genppc_pct; 68 69 static void 70 ofwpci_get_chipset_tag(pci_chipset_tag_t pc) 71 { 72 pc->pc_conf_v = (void *)pc; 73 74 pc->pc_attach_hook = genppc_pci_indirect_attach_hook; 75 pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs; 76 pc->pc_make_tag = genppc_pci_indirect_make_tag; 77 pc->pc_conf_read = genppc_pci_indirect_conf_read; 78 pc->pc_conf_write = genppc_pci_indirect_conf_write; 79 80 pc->pc_intr_v = (void *)pc; 81 82 pc->pc_intr_map = genofw_pci_intr_map; 83 pc->pc_intr_string = genppc_pci_intr_string; 84 pc->pc_intr_evcnt = genppc_pci_intr_evcnt; 85 pc->pc_intr_establish = genppc_pci_intr_establish; 86 pc->pc_intr_disestablish = genppc_pci_intr_disestablish; 87 88 pc->pc_conf_interrupt = genppc_pci_conf_interrupt; 89 pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag; 90 pc->pc_conf_hook = genofw_pci_conf_hook; 91 92 pc->pc_addr = 0; 93 pc->pc_data = 0; 94 pc->pc_bus = 0; 95 pc->pc_node = 0; 96 pc->pc_memt = 0; 97 pc->pc_iot = 0; 98 } 99 100 static int 101 ofwpci_match(struct device *parent, struct cfdata *cf, void *aux) 102 { 103 struct confargs *ca = aux; 104 char name[32]; 105 106 if (strcmp(ca->ca_name, "pci") != 0) 107 return 0; 108 109 memset(name, 0, sizeof(name)); 110 OF_getprop(ca->ca_node, "device_type", name, sizeof(name)); 111 if (strcmp(name, "pci") != 0) 112 return 0; 113 114 return 1; 115 } 116 117 static void 118 ofwpci_attach(struct device *parent, struct device *self, void *aux) 119 { 120 struct ofwpci_softc *sc = (void *)self; 121 pci_chipset_tag_t pc = &sc->sc_pc; 122 struct confargs *ca = aux; 123 struct pcibus_attach_args pba; 124 struct genppc_pci_chipset_businfo *pbi; 125 int node = ca->ca_node; 126 uint32_t reg[2], busrange[2]; 127 #if 0 128 struct ranges { 129 uint32_t pci_hi, pci_mid, pci_lo; 130 uint32_t host; 131 uint32_t size_hi, size_lo; 132 } ranges[6], *rp = ranges; 133 #endif 134 135 aprint_normal("\n"); 136 137 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8) 138 return; 139 140 /* PCI bus number */ 141 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8) 142 return; 143 144 /* bus space map the io ranges */ 145 sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE; 146 sc->sc_iot.pbs_base = 0x00000000; 147 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot, 148 "ofwpci io-space") != 0) 149 panic("Can't init ofwpci io tag"); 150 151 sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE; 152 sc->sc_memt.pbs_base = 0x00000000; 153 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt, 154 "ofwpci mem-space") != 0) 155 panic("Can't init ofwpci mem tag"); 156 157 ofwpci_get_chipset_tag(pc); 158 pc->pc_node = node; 159 pc->pc_addr = mapiodev(reg[0] + 0xcf8, 4); 160 pc->pc_data = mapiodev(reg[0] + 0xcfc, 4); 161 pc->pc_bus = busrange[0]; 162 pc->pc_iot = &sc->sc_iot; 163 pc->pc_memt = &sc->sc_memt; 164 165 pbi = malloc(sizeof(struct genppc_pci_chipset_businfo), 166 M_DEVBUF, M_NOWAIT); 167 KASSERT(pbi != NULL); 168 pbi->pbi_properties = prop_dictionary_create(); 169 KASSERT(pbi->pbi_properties != NULL); 170 SIMPLEQ_INIT(&pc->pc_pbi); 171 SIMPLEQ_INSERT_TAIL(&pc->pc_pbi, pbi, next); 172 173 genofw_setup_pciintr_map((void *)pc, pbi, pc->pc_node); 174 175 memset(&pba, 0, sizeof(pba)); 176 pba.pba_memt = pc->pc_memt; 177 pba.pba_iot = pc->pc_iot; 178 pba.pba_dmat = &pci_bus_dma_tag; 179 pba.pba_dmat64 = NULL; 180 pba.pba_bus = pc->pc_bus; 181 pba.pba_bridgetag = NULL; 182 pba.pba_pc = pc; 183 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED; 184 185 config_found_ia(self, "pcibus", &pba, pcibusprint); 186 } 187