puc_pci.c revision 123019
1251607Sdim/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 2251607Sdim 3251607Sdim/*- 4251607Sdim * Copyright (c) 2002 JF Hay. All rights reserved. 5251607Sdim * Copyright (c) 2000 M. Warner Losh. All rights reserved. 6251607Sdim * 7251607Sdim * Redistribution and use in source and binary forms, with or without 8251607Sdim * modification, are permitted provided that the following conditions 9251607Sdim * are met: 10251607Sdim * 1. Redistributions of source code must retain the above copyright 11251607Sdim * notice unmodified, this list of conditions, and the following 12251607Sdim * disclaimer. 13263508Sdim * 2. Redistributions in binary form must reproduce the above copyright 14251607Sdim * notice, this list of conditions and the following disclaimer in the 15251607Sdim * documentation and/or other materials provided with the distribution. 16251607Sdim * 17251607Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18251607Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19251607Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20251607Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21251607Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22251607Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23251607Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24251607Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25251607Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26251607Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27251607Sdim */ 28251607Sdim 29251607Sdim#include <sys/cdefs.h> 30263508Sdim__FBSDID("$FreeBSD: head/sys/dev/puc/puc_pci.c 123019 2003-11-28 05:28:29Z imp $"); 31251607Sdim 32251607Sdim/* 33251607Sdim * Copyright (c) 1996, 1998, 1999 34251607Sdim * Christopher G. Demetriou. All rights reserved. 35263508Sdim * 36251607Sdim * Redistribution and use in source and binary forms, with or without 37251607Sdim * modification, are permitted provided that the following conditions 38263508Sdim * are met: 39263508Sdim * 1. Redistributions of source code must retain the above copyright 40263508Sdim * notice, this list of conditions and the following disclaimer. 41251607Sdim * 2. Redistributions in binary form must reproduce the above copyright 42251607Sdim * notice, this list of conditions and the following disclaimer in the 43251607Sdim * documentation and/or other materials provided with the distribution. 44251607Sdim * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by Christopher G. Demetriou 47 * for the NetBSD Project. 48 * 4. The name of the author may not be used to endorse or promote products 49 * derived from this software without specific prior written permission 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63#include <sys/cdefs.h> 64__FBSDID("$FreeBSD: head/sys/dev/puc/puc_pci.c 123019 2003-11-28 05:28:29Z imp $"); 65 66#include "opt_puc.h" 67 68#include <sys/param.h> 69#include <sys/systm.h> 70#include <sys/kernel.h> 71#include <sys/bus.h> 72#include <sys/conf.h> 73#include <sys/malloc.h> 74 75#include <machine/bus.h> 76#include <machine/resource.h> 77#include <sys/rman.h> 78 79#include <dev/pci/pcireg.h> 80#include <dev/pci/pcivar.h> 81 82#define PUC_ENTRAILS 1 83#include <dev/puc/pucvar.h> 84 85extern const struct puc_device_description puc_devices[]; 86 87int puc_config_win877(struct puc_softc *); 88 89static const struct puc_device_description * 90puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend, 91 uint32_t sprod) 92{ 93 int i; 94 95#define checkreg(val, index) \ 96 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)]) 97 98 for (i = 0; puc_devices[i].name != NULL; i++) { 99 if (checkreg(vend, PUC_REG_VEND) && 100 checkreg(prod, PUC_REG_PROD) && 101 checkreg(svend, PUC_REG_SVEND) && 102 checkreg(sprod, PUC_REG_SPROD)) 103 return (&puc_devices[i]); 104 } 105 106#undef checkreg 107 108 return (NULL); 109} 110 111static int 112puc_pci_probe(device_t dev) 113{ 114 uint32_t v1, v2, d1, d2; 115 const struct puc_device_description *desc; 116 117 if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0) 118 return (ENXIO); 119 120 v1 = pci_read_config(dev, PCIR_VENDOR, 2); 121 d1 = pci_read_config(dev, PCIR_DEVICE, 2); 122 v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2); 123 d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2); 124 125 desc = puc_find_description(v1, d1, v2, d2); 126 if (desc == NULL) 127 return (ENXIO); 128 device_set_desc(dev, desc->name); 129 return (0); 130} 131 132static int 133puc_pci_attach(device_t dev) 134{ 135 uint32_t v1, v2, d1, d2; 136 137 v1 = pci_read_config(dev, PCIR_VENDOR, 2); 138 d1 = pci_read_config(dev, PCIR_DEVICE, 2); 139 v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2); 140 d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2); 141 return (puc_attach(dev, puc_find_description(v1, d1, v2, d2))); 142} 143 144static device_method_t puc_pci_methods[] = { 145 /* Device interface */ 146 DEVMETHOD(device_probe, puc_pci_probe), 147 DEVMETHOD(device_attach, puc_pci_attach), 148 149 DEVMETHOD(bus_alloc_resource, puc_alloc_resource), 150 DEVMETHOD(bus_release_resource, puc_release_resource), 151 DEVMETHOD(bus_get_resource, puc_get_resource), 152 DEVMETHOD(bus_read_ivar, puc_read_ivar), 153 DEVMETHOD(bus_setup_intr, puc_setup_intr), 154 DEVMETHOD(bus_teardown_intr, puc_teardown_intr), 155 DEVMETHOD(bus_print_child, bus_generic_print_child), 156 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 157 { 0, 0 } 158}; 159 160static driver_t puc_pci_driver = { 161 "puc", 162 puc_pci_methods, 163 sizeof(struct puc_softc), 164}; 165 166DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0); 167DRIVER_MODULE(puc, cardbus, puc_pci_driver, puc_devclass, 0, 0); 168 169 170#define rdspio(indx) (bus_space_write_1(bst, bsh, efir, indx), \ 171 bus_space_read_1(bst, bsh, efdr)) 172#define wrspio(indx,data) (bus_space_write_1(bst, bsh, efir, indx), \ 173 bus_space_write_1(bst, bsh, efdr, data)) 174 175#ifdef PUC_DEBUG 176static void 177puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir, 178 u_int efdr) 179{ 180 u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17; 181 u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32; 182 183 cr00 = rdspio(0x00); 184 cr01 = rdspio(0x01); 185 cr04 = rdspio(0x04); 186 cr09 = rdspio(0x09); 187 cr0d = rdspio(0x0d); 188 cr14 = rdspio(0x14); 189 cr15 = rdspio(0x15); 190 cr16 = rdspio(0x16); 191 cr17 = rdspio(0x17); 192 cr18 = rdspio(0x18); 193 cr19 = rdspio(0x19); 194 cr24 = rdspio(0x24); 195 cr25 = rdspio(0x25); 196 cr28 = rdspio(0x28); 197 cr2c = rdspio(0x2c); 198 cr31 = rdspio(0x31); 199 cr32 = rdspio(0x32); 200 printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, " 201 "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, " 202 "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09, 203 cr0d, cr14, cr15, cr16, cr17, 204 cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32); 205} 206#endif 207 208int 209puc_config_win877(struct puc_softc *sc) 210{ 211 u_char val; 212 u_int efir, efdr; 213 bus_space_tag_t bst; 214 bus_space_handle_t bsh; 215 struct resource *res; 216 217 res = sc->sc_bar_mappings[0].res; 218 219 bst = rman_get_bustag(res); 220 bsh = rman_get_bushandle(res); 221 222 /* configure the first W83877TF */ 223 bus_space_write_1(bst, bsh, 0x250, 0x89); 224 efir = 0x251; 225 efdr = 0x252; 226 val = rdspio(0x09) & 0x0f; 227 if (val != 0x0c) { 228 printf("conf_win877: Oops not a W83877TF\n"); 229 return (ENXIO); 230 } 231 232#ifdef PUC_DEBUG 233 printf("before: "); 234 puc_print_win877(bst, bsh, efir, efdr); 235#endif 236 237 val = rdspio(0x16); 238 val |= 0x04; 239 wrspio(0x16, val); 240 val &= ~0x04; 241 wrspio(0x16, val); 242 243 wrspio(0x24, 0x2e8 >> 2); 244 wrspio(0x25, 0x2f8 >> 2); 245 wrspio(0x17, 0x03); 246 wrspio(0x28, 0x43); 247 248#ifdef PUC_DEBUG 249 printf("after: "); 250 puc_print_win877(bst, bsh, efir, efdr); 251#endif 252 253 bus_space_write_1(bst, bsh, 0x250, 0xaa); 254 255 /* configure the second W83877TF */ 256 bus_space_write_1(bst, bsh, 0x3f0, 0x87); 257 bus_space_write_1(bst, bsh, 0x3f0, 0x87); 258 efir = 0x3f0; 259 efdr = 0x3f1; 260 val = rdspio(0x09) & 0x0f; 261 if (val != 0x0c) { 262 printf("conf_win877: Oops not a W83877TF\n"); 263 return(ENXIO); 264 } 265 266#ifdef PUC_DEBUG 267 printf("before: "); 268 puc_print_win877(bst, bsh, efir, efdr); 269#endif 270 271 val = rdspio(0x16); 272 val |= 0x04; 273 wrspio(0x16, val); 274 val &= ~0x04; 275 wrspio(0x16, val); 276 277 wrspio(0x24, 0x3e8 >> 2); 278 wrspio(0x25, 0x3f8 >> 2); 279 wrspio(0x17, 0x03); 280 wrspio(0x28, 0x43); 281 282#ifdef PUC_DEBUG 283 printf("after: "); 284 puc_print_win877(bst, bsh, efir, efdr); 285#endif 286 287 bus_space_write_1(bst, bsh, 0x3f0, 0xaa); 288 return (0); 289} 290 291#undef rdspio 292#undef wrspio 293 294