1 1.23 thorpej /* $NetBSD: uninorth.c,v 1.23 2022/01/21 19:12:28 thorpej Exp $ */ 2 1.1 tsubai 3 1.1 tsubai /*- 4 1.1 tsubai * Copyright (c) 2000 Tsubai Masanari. All rights reserved. 5 1.1 tsubai * 6 1.1 tsubai * Redistribution and use in source and binary forms, with or without 7 1.1 tsubai * modification, are permitted provided that the following conditions 8 1.1 tsubai * are met: 9 1.1 tsubai * 1. Redistributions of source code must retain the above copyright 10 1.1 tsubai * notice, this list of conditions and the following disclaimer. 11 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 tsubai * notice, this list of conditions and the following disclaimer in the 13 1.1 tsubai * documentation and/or other materials provided with the distribution. 14 1.1 tsubai * 3. The name of the author may not be used to endorse or promote products 15 1.1 tsubai * derived from this software without specific prior written permission. 16 1.1 tsubai * 17 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 1.1 tsubai */ 28 1.9 lukem 29 1.9 lukem #include <sys/cdefs.h> 30 1.23 thorpej __KERNEL_RCSID(0, "$NetBSD: uninorth.c,v 1.23 2022/01/21 19:12:28 thorpej Exp $"); 31 1.1 tsubai 32 1.1 tsubai #include <sys/param.h> 33 1.1 tsubai #include <sys/device.h> 34 1.1 tsubai #include <sys/systm.h> 35 1.1 tsubai 36 1.1 tsubai #include <dev/pci/pcivar.h> 37 1.1 tsubai #include <dev/ofw/openfirm.h> 38 1.1 tsubai #include <dev/ofw/ofw_pci.h> 39 1.20 macallan #include <powerpc/oea/cpufeat.h> 40 1.1 tsubai 41 1.1 tsubai #include <machine/autoconf.h> 42 1.12 garbled #include <machine/pio.h> 43 1.1 tsubai 44 1.1 tsubai struct uninorth_softc { 45 1.16 macallan device_t sc_dev; 46 1.12 garbled struct genppc_pci_chipset sc_pc; 47 1.12 garbled struct powerpc_bus_space sc_iot; 48 1.12 garbled struct powerpc_bus_space sc_memt; 49 1.1 tsubai }; 50 1.1 tsubai 51 1.14 matt static void uninorth_attach(device_t, device_t, void *); 52 1.14 matt static int uninorth_match(device_t, cfdata_t, void *); 53 1.1 tsubai 54 1.12 garbled static pcireg_t uninorth_conf_read(void *, pcitag_t, int); 55 1.12 garbled static void uninorth_conf_write(void *, pcitag_t, int, pcireg_t); 56 1.17 macallan static pcireg_t uninorth_conf_read_v3(void *, pcitag_t, int); 57 1.17 macallan static void uninorth_conf_write_v3(void *, pcitag_t, int, pcireg_t); 58 1.1 tsubai 59 1.16 macallan CFATTACH_DECL_NEW(uninorth, sizeof(struct uninorth_softc), 60 1.6 thorpej uninorth_match, uninorth_attach, NULL, NULL); 61 1.1 tsubai 62 1.12 garbled static int 63 1.14 matt uninorth_match(device_t parent, cfdata_t cf, void *aux) 64 1.1 tsubai { 65 1.1 tsubai struct confargs *ca = aux; 66 1.1 tsubai char compat[32]; 67 1.1 tsubai 68 1.1 tsubai if (strcmp(ca->ca_name, "pci") != 0) 69 1.1 tsubai return 0; 70 1.1 tsubai 71 1.2 wiz memset(compat, 0, sizeof(compat)); 72 1.1 tsubai OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat)); 73 1.17 macallan if (strcmp(compat, "uni-north") != 0 && 74 1.17 macallan strcmp(compat, "u3-agp") != 0 && 75 1.17 macallan strcmp(compat, "u4-pcie") != 0) 76 1.1 tsubai return 0; 77 1.1 tsubai 78 1.1 tsubai return 1; 79 1.1 tsubai } 80 1.1 tsubai 81 1.12 garbled static void 82 1.14 matt uninorth_attach(device_t parent, device_t self, void *aux) 83 1.1 tsubai { 84 1.14 matt struct uninorth_softc *sc = device_private(self); 85 1.1 tsubai pci_chipset_tag_t pc = &sc->sc_pc; 86 1.1 tsubai struct confargs *ca = aux; 87 1.1 tsubai struct pcibus_attach_args pba; 88 1.1 tsubai int len, child, node = ca->ca_node; 89 1.12 garbled uint32_t reg[2], busrange[2]; 90 1.17 macallan char compat[32]; 91 1.17 macallan int ver; 92 1.1 tsubai struct ranges { 93 1.12 garbled uint32_t pci_hi, pci_mid, pci_lo; 94 1.12 garbled uint32_t host; 95 1.12 garbled uint32_t size_hi, size_lo; 96 1.1 tsubai } ranges[6], *rp = ranges; 97 1.1 tsubai 98 1.1 tsubai printf("\n"); 99 1.16 macallan sc->sc_dev = self; 100 1.1 tsubai 101 1.17 macallan memset(compat, 0, sizeof(compat)); 102 1.17 macallan OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat)); 103 1.17 macallan if (strcmp(compat, "u3-agp") == 0) 104 1.17 macallan ver = 3; 105 1.17 macallan else if (strcmp(compat, "u4-pcie") == 0) 106 1.17 macallan ver = 4; 107 1.17 macallan else 108 1.17 macallan ver = 0; 109 1.17 macallan 110 1.1 tsubai /* UniNorth address */ 111 1.1 tsubai if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8) 112 1.1 tsubai return; 113 1.1 tsubai 114 1.1 tsubai /* PCI bus number */ 115 1.1 tsubai if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8) 116 1.1 tsubai return; 117 1.1 tsubai 118 1.17 macallan memset(&sc->sc_iot, 0, sizeof(sc->sc_iot)); 119 1.17 macallan 120 1.1 tsubai /* find i/o tag */ 121 1.1 tsubai len = OF_getprop(node, "ranges", ranges, sizeof(ranges)); 122 1.1 tsubai if (len == -1) 123 1.1 tsubai return; 124 1.1 tsubai while (len >= sizeof(ranges[0])) { 125 1.1 tsubai if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) == 126 1.12 garbled OFW_PCI_PHYS_HI_SPACE_IO) { 127 1.12 garbled sc->sc_iot.pbs_base = rp->host; 128 1.12 garbled sc->sc_iot.pbs_limit = rp->host + rp->size_lo; 129 1.12 garbled break; 130 1.12 garbled } 131 1.1 tsubai len -= sizeof(ranges[0]); 132 1.1 tsubai rp++; 133 1.1 tsubai } 134 1.1 tsubai 135 1.1 tsubai /* XXX enable gmac ethernet */ 136 1.1 tsubai for (child = OF_child(node); child; child = OF_peer(child)) { 137 1.1 tsubai volatile int *gmac_gbclock_en = (void *)0xf8000020; 138 1.1 tsubai 139 1.2 wiz memset(compat, 0, sizeof(compat)); 140 1.1 tsubai OF_getprop(child, "compatible", compat, sizeof(compat)); 141 1.1 tsubai if (strcmp(compat, "gmac") == 0) 142 1.1 tsubai *gmac_gbclock_en |= 0x02; 143 1.1 tsubai } 144 1.1 tsubai 145 1.12 garbled sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE; 146 1.12 garbled sc->sc_iot.pbs_offset = 0; 147 1.12 garbled if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, node, &sc->sc_iot, 148 1.12 garbled "uninorth io-space") != 0) 149 1.12 garbled panic("Can't init uninorth io tag"); 150 1.12 garbled 151 1.17 macallan memset(&sc->sc_memt, 0, sizeof(sc->sc_memt)); 152 1.12 garbled sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE; 153 1.12 garbled sc->sc_memt.pbs_base = 0x00000000; 154 1.12 garbled if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, node, &sc->sc_memt, 155 1.12 garbled "uninorth mem-space") != 0) 156 1.12 garbled panic("Can't init uninorth mem tag"); 157 1.12 garbled 158 1.12 garbled macppc_pci_get_chipset_tag(pc); 159 1.12 garbled pc->pc_node = node; 160 1.12 garbled pc->pc_bus = busrange[0]; 161 1.12 garbled pc->pc_iot = &sc->sc_iot; 162 1.12 garbled pc->pc_memt = &sc->sc_memt; 163 1.12 garbled 164 1.17 macallan if (ver < 3) { 165 1.20 macallan pc->pc_addr = oea_mapiodev(reg[0] + 0x800000, 4); 166 1.20 macallan pc->pc_data = oea_mapiodev(reg[0] + 0xc00000, 8); 167 1.17 macallan pc->pc_conf_read = uninorth_conf_read; 168 1.17 macallan pc->pc_conf_write = uninorth_conf_write; 169 1.17 macallan } else { 170 1.20 macallan pc->pc_addr = oea_mapiodev(reg[1] + 0x800000, 4); 171 1.20 macallan pc->pc_data = oea_mapiodev(reg[1] + 0xc00000, 8); 172 1.17 macallan pc->pc_conf_read = uninorth_conf_read_v3; 173 1.17 macallan pc->pc_conf_write = uninorth_conf_write_v3; 174 1.17 macallan } 175 1.17 macallan 176 1.2 wiz memset(&pba, 0, sizeof(pba)); 177 1.12 garbled pba.pba_memt = pc->pc_memt; 178 1.12 garbled pba.pba_iot = pc->pc_iot; 179 1.1 tsubai pba.pba_dmat = &pci_bus_dma_tag; 180 1.8 fvdl pba.pba_dmat64 = NULL; 181 1.12 garbled pba.pba_bus = pc->pc_bus; 182 1.4 thorpej pba.pba_bridgetag = NULL; 183 1.1 tsubai pba.pba_pc = pc; 184 1.13 dyoung pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY; 185 1.1 tsubai 186 1.21 thorpej config_found(self, &pba, pcibusprint, 187 1.23 thorpej CFARGS(.devhandle = device_handle(self))); 188 1.1 tsubai } 189 1.1 tsubai 190 1.12 garbled static pcireg_t 191 1.12 garbled uninorth_conf_read(void *cookie, pcitag_t tag, int reg) 192 1.1 tsubai { 193 1.12 garbled pci_chipset_tag_t pc = cookie; 194 1.12 garbled int32_t *daddr = pc->pc_data; 195 1.1 tsubai pcireg_t data; 196 1.1 tsubai int bus, dev, func, s; 197 1.12 garbled uint32_t x; 198 1.1 tsubai 199 1.18 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE) 200 1.18 msaitoh return (pcireg_t) -1; 201 1.18 msaitoh 202 1.1 tsubai /* UniNorth seems to have a 64bit data port */ 203 1.1 tsubai if (reg & 0x04) 204 1.1 tsubai daddr++; 205 1.1 tsubai 206 1.1 tsubai pci_decompose_tag(pc, tag, &bus, &dev, &func); 207 1.1 tsubai 208 1.1 tsubai /* 209 1.1 tsubai * bandit's minimum device number of the first bus is 11. 210 1.1 tsubai * So we behave as if there is no device when dev < 11. 211 1.1 tsubai */ 212 1.1 tsubai if (func > 7) 213 1.1 tsubai panic("pci_conf_read: func > 7"); 214 1.1 tsubai 215 1.12 garbled if (bus == pc->pc_bus) { 216 1.3 nathanw if (dev < 11) 217 1.3 nathanw return 0xffffffff; 218 1.1 tsubai x = (1 << dev) | (func << 8) | reg; 219 1.1 tsubai } else 220 1.1 tsubai x = tag | reg | 1; 221 1.1 tsubai 222 1.1 tsubai s = splhigh(); 223 1.1 tsubai 224 1.12 garbled out32rb(pc->pc_addr, x); 225 1.12 garbled in32rb(pc->pc_addr); 226 1.1 tsubai data = 0xffffffff; 227 1.1 tsubai if (!badaddr(daddr, 4)) 228 1.1 tsubai data = in32rb(daddr); 229 1.12 garbled out32rb(pc->pc_addr, 0); 230 1.12 garbled in32rb(pc->pc_addr); 231 1.1 tsubai splx(s); 232 1.1 tsubai 233 1.1 tsubai return data; 234 1.1 tsubai } 235 1.1 tsubai 236 1.12 garbled static void 237 1.12 garbled uninorth_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data) 238 1.1 tsubai { 239 1.12 garbled pci_chipset_tag_t pc = cookie; 240 1.12 garbled int32_t *daddr = pc->pc_data; 241 1.1 tsubai int bus, dev, func, s; 242 1.12 garbled uint32_t x; 243 1.1 tsubai 244 1.18 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE) 245 1.18 msaitoh return; 246 1.18 msaitoh 247 1.1 tsubai /* UniNorth seems to have a 64bit data port */ 248 1.1 tsubai if (reg & 0x04) 249 1.1 tsubai daddr++; 250 1.1 tsubai 251 1.1 tsubai pci_decompose_tag(pc, tag, &bus, &dev, &func); 252 1.1 tsubai 253 1.1 tsubai if (func > 7) 254 1.1 tsubai panic("pci_conf_write: func > 7"); 255 1.1 tsubai 256 1.12 garbled if (bus == pc->pc_bus) { 257 1.1 tsubai if (dev < 11) 258 1.1 tsubai panic("pci_conf_write: dev < 11"); 259 1.1 tsubai x = (1 << dev) | (func << 8) | reg; 260 1.1 tsubai } else 261 1.1 tsubai x = tag | reg | 1; 262 1.1 tsubai 263 1.1 tsubai s = splhigh(); 264 1.1 tsubai 265 1.12 garbled out32rb(pc->pc_addr, x); 266 1.12 garbled in32rb(pc->pc_addr); 267 1.1 tsubai out32rb(daddr, data); 268 1.12 garbled out32rb(pc->pc_addr, 0); 269 1.12 garbled in32rb(pc->pc_addr); 270 1.1 tsubai 271 1.1 tsubai splx(s); 272 1.1 tsubai } 273 1.17 macallan 274 1.17 macallan static pcireg_t 275 1.17 macallan uninorth_conf_read_v3(void *cookie, pcitag_t tag, int reg) 276 1.17 macallan { 277 1.17 macallan pci_chipset_tag_t pc = cookie; 278 1.17 macallan int32_t *daddr = pc->pc_data; 279 1.17 macallan pcireg_t data; 280 1.17 macallan int bus, dev, func, s; 281 1.17 macallan uint32_t x; 282 1.17 macallan 283 1.18 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE) 284 1.18 msaitoh return (pcireg_t) -1; 285 1.18 msaitoh 286 1.17 macallan /* UniNorth seems to have a 64bit data port */ 287 1.17 macallan if (reg & 0x04) 288 1.17 macallan daddr++; 289 1.17 macallan 290 1.17 macallan pci_decompose_tag(pc, tag, &bus, &dev, &func); 291 1.17 macallan 292 1.19 macallan if (bus == 0) { 293 1.19 macallan if (dev < 11) return 0xffffffff; 294 1.19 macallan x = (1 << dev) | (func << 8) | reg; 295 1.19 macallan } else 296 1.19 macallan x = (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) | 1; 297 1.17 macallan /* Set extended register bits */ 298 1.17 macallan x |= (reg >> 8) << 28; 299 1.17 macallan 300 1.17 macallan s = splhigh(); 301 1.17 macallan 302 1.17 macallan out32rb(pc->pc_addr, x); 303 1.17 macallan in32rb(pc->pc_addr); 304 1.17 macallan data = 0xffffffff; 305 1.19 macallan if (!badaddr(daddr, 4)) { 306 1.17 macallan data = in32rb(daddr); 307 1.19 macallan } 308 1.17 macallan out32rb(pc->pc_addr, 0); 309 1.17 macallan in32rb(pc->pc_addr); 310 1.17 macallan splx(s); 311 1.17 macallan 312 1.17 macallan return data; 313 1.17 macallan } 314 1.17 macallan 315 1.17 macallan static void 316 1.17 macallan uninorth_conf_write_v3(void *cookie, pcitag_t tag, int reg, pcireg_t data) 317 1.17 macallan { 318 1.17 macallan pci_chipset_tag_t pc = cookie; 319 1.17 macallan int32_t *daddr = pc->pc_data; 320 1.17 macallan int bus, dev, func, s; 321 1.17 macallan uint32_t x; 322 1.17 macallan 323 1.18 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE) 324 1.18 msaitoh return; 325 1.18 msaitoh 326 1.17 macallan /* UniNorth seems to have a 64bit data port */ 327 1.17 macallan if (reg & 0x04) 328 1.17 macallan daddr++; 329 1.17 macallan 330 1.17 macallan pci_decompose_tag(pc, tag, &bus, &dev, &func); 331 1.17 macallan 332 1.19 macallan if (bus == 0) { 333 1.19 macallan if (dev < 11) return; 334 1.19 macallan x = (1 << dev) | (func << 8) | reg; 335 1.19 macallan } else 336 1.19 macallan x = (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) | 1; 337 1.17 macallan /* Set extended register bits */ 338 1.17 macallan x |= (reg >> 8) << 28; 339 1.17 macallan 340 1.17 macallan s = splhigh(); 341 1.17 macallan 342 1.17 macallan out32rb(pc->pc_addr, x); 343 1.17 macallan in32rb(pc->pc_addr); 344 1.17 macallan out32rb(daddr, data); 345 1.17 macallan out32rb(pc->pc_addr, 0); 346 1.17 macallan in32rb(pc->pc_addr); 347 1.17 macallan 348 1.17 macallan splx(s); 349 1.17 macallan } 350