1/* $NetBSD: u3.c,v 1.4 2011/06/18 08:08:28 matt Exp $ */ 2 3/* 4 * Copyright 2006 Kyma Systems LLC. 5 * All rights reserved. 6 * 7 * Written by Sanjay Lal <sanjayl@kymasys.com> 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Kyma Systems LLC. 21 * 4. The name of Kyma Systems LLC may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY KYMA SYSTEMS LLC ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KYMA SYSTEMS LLC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38#include <sys/cdefs.h> 39 40#include <sys/param.h> 41#include <sys/device.h> 42#include <sys/systm.h> 43 44#include <dev/pci/pcivar.h> 45#include <dev/ofw/openfirm.h> 46#include <dev/ofw/ofw_pci.h> 47 48#include <machine/autoconf.h> 49 50struct ibmcpc_softc 51{ 52 struct device sc_dev; 53 struct genppc_pci_chipset sc_pc[8]; 54 struct powerpc_bus_space sc_iot; 55 struct powerpc_bus_space sc_memt; 56}; 57 58static void ibmcpc_attach(device_t, device_t, void *); 59static int ibmcpc_match(device_t, cfdata_t, void *); 60 61static pcireg_t ibmcpc_conf_read(void *, pcitag_t, int); 62static void ibmcpc_conf_write(void *, pcitag_t, int, pcireg_t); 63 64CFATTACH_DECL(ibmcpc, sizeof(struct ibmcpc_softc), 65 ibmcpc_match, ibmcpc_attach, NULL, NULL); 66 67#define PCI_DEVFN(slot,func) ((((slot) & 0x1f) << 3) | ((func) & 0x07)) 68 69static int 70ibmcpc_match(device_t parent, cfdata_t cf, void *aux) 71{ 72 struct confargs *ca = aux; 73 char compat[32]; 74 75 if (strcmp(ca->ca_name, "ht") != 0) 76 return 0; 77 78 memset(compat, 0, sizeof(compat)); 79 OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat)); 80 81 if (strcmp(compat, "u3-ht") != 0) 82 return 0; 83 84 return 1; 85} 86 87static void 88ibmcpc_attach(device_t parent, device_t self, void *aux) 89{ 90 struct ibmcpc_softc *sc = (void *) self; 91 pci_chipset_tag_t pc = sc->sc_pc; 92 struct confargs *ca = aux; 93 struct pcibus_attach_args pba; 94 int node = ca->ca_node, child; 95 uint32_t reg[6], busrange[2], i; 96 void *pc_data; 97 char name[32]; 98 99 aprint_normal("\n"); 100 101 /* u3 address */ 102 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 24) { 103 return; 104 } 105 aprint_normal("Mapping in config space @ pa 0x%08x, size: 0x%08x\n", 106 reg[1], reg[2]); 107 pc_data = mapiodev(reg[1], reg[2], false); 108 109 for (child = OF_child(OF_finddevice("/ht")), i = 1; child; 110 child = OF_peer(child), i++) { 111 112 memset(name, 0, sizeof(name)); 113 114 if (OF_getprop(child, "name", name, sizeof(name)) == -1) 115 continue; 116 117 if (strcmp(name, "pci") != 0) 118 continue; 119 120 if (OF_getprop(child, "bus-range", busrange, 8) < 8) 121 continue; 122 123 sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN | 124 _BUS_SPACE_IO_TYPE; 125 sc->sc_iot.pbs_base = 0x00000000; 126 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, child, 127 &sc->sc_iot, "ibmcpc io") != 0) 128 panic("Can't init ibmcpc io tag"); 129 130 sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN | 131 _BUS_SPACE_MEM_TYPE; 132 sc->sc_memt.pbs_base = 0x00000000; 133 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, child, 134 &sc->sc_memt, "ibmcpc mem") != 0) 135 panic("Can't init ibmcpc mem tag"); 136 137 macppc_pci_get_chipset_tag(pc); 138 pc->pc_node = child; 139 pc->pc_bus = busrange[0]; 140 pc->pc_addr = 0x0; 141 pc->pc_data = pc_data; 142 pc->pc_conf_read = ibmcpc_conf_read; 143 pc->pc_conf_write = ibmcpc_conf_write; 144 pc->pc_memt = &sc->sc_memt; 145 pc->pc_iot = &sc->sc_iot; 146 147 memset(&pba, 0, sizeof(pba)); 148 pba.pba_memt = pc->pc_memt; 149 pba.pba_iot = pc->pc_iot; 150 pba.pba_dmat = &pci_bus_dma_tag; 151 pba.pba_dmat64 = NULL; 152 pba.pba_bridgetag = NULL; 153 pba.pba_pc = pc; 154 pba.pba_bus = pc->pc_bus; 155 pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY; 156 config_found_ia(self, "pcibus", &pba, pcibusprint); 157 158 pc++; 159 } 160} 161 162static pcireg_t 163ibmcpc_conf_read(void *cookie, pcitag_t tag, int reg) 164{ 165 pci_chipset_tag_t pc = cookie; 166 u_int32_t daddr = (u_int32_t) pc->pc_data; 167 pcireg_t data; 168 u_int32_t bus, dev, func, x, devfn; 169 170 pci_decompose_tag(pc, tag, &bus, &dev, &func); 171 172 devfn = PCI_DEVFN(dev, func); 173 174 if (bus == 0) { 175 176 if (devfn == 0x0) { 177 178 data = 0xffffffff; 179 goto done; 180 } 181 182 x = daddr + ((devfn << 8) | reg); 183 } else 184 x = daddr + ((devfn << 8) | reg) + (bus << 16) + 0x01000000UL; 185 186 data = in32rb(x); 187 188done: 189 return data; 190} 191 192static void 193ibmcpc_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data) 194{ 195 pci_chipset_tag_t pc = cookie; 196 int32_t *daddr = pc->pc_data; 197 u_int32_t bus, dev, func; 198 u_int32_t x, devfn; 199 200 pci_decompose_tag(pc, tag, &bus, &dev, &func); 201 202 devfn = PCI_DEVFN(dev, func); 203 204 if (bus == 0) { 205 206 if (devfn == 0x0) { 207 goto done; 208 } 209 x = (u_int32_t) daddr + ((devfn << 8) | reg); 210 } else 211 x = (u_int32_t) daddr + ((devfn << 8) | reg) + (bus << 16) + 212 0x01000000UL; 213 214 out32rb(x, data); 215 216done: 217 return; 218} 219