1 /* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 JF Hay. All rights reserved. 5 * Copyright (c) 2000 M. Warner Losh. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * Copyright (c) 1996, 1998, 1999 34 * Christopher G. Demetriou. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 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$"); 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 85 extern const struct puc_device_description puc_devices[]; 86 87 int puc_config_win877(struct puc_softc *); 88 89 static const struct puc_device_description * 90 puc_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 111 static int 112 puc_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 132 static int 133 puc_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 144 static 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 160 static driver_t puc_pci_driver = { 161 "puc", 162 puc_pci_methods, 163 sizeof(struct puc_softc), 164 }; 165 166 DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0); 167 168 169 #define rdspio(indx) (bus_space_write_1(bst, bsh, efir, indx), \ 170 bus_space_read_1(bst, bsh, efdr)) 171 #define wrspio(indx,data) (bus_space_write_1(bst, bsh, efir, indx), \ 172 bus_space_write_1(bst, bsh, efdr, data)) 173 174 #ifdef PUC_DEBUG 175 static void 176 puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir, 177 u_int efdr) 178 { 179 u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17; 180 u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32; 181 182 cr00 = rdspio(0x00); 183 cr01 = rdspio(0x01); 184 cr04 = rdspio(0x04); 185 cr09 = rdspio(0x09); 186 cr0d = rdspio(0x0d); 187 cr14 = rdspio(0x14); 188 cr15 = rdspio(0x15); 189 cr16 = rdspio(0x16); 190 cr17 = rdspio(0x17); 191 cr18 = rdspio(0x18); 192 cr19 = rdspio(0x19); 193 cr24 = rdspio(0x24); 194 cr25 = rdspio(0x25); 195 cr28 = rdspio(0x28); 196 cr2c = rdspio(0x2c); 197 cr31 = rdspio(0x31); 198 cr32 = rdspio(0x32); 199 printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, " 200 "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, " 201 "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09, 202 cr0d, cr14, cr15, cr16, cr17, 203 cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32); 204 } 205 #endif 206 207 int 208 puc_config_win877(struct puc_softc *sc) 209 { 210 u_char val; 211 u_int efir, efdr; 212 bus_space_tag_t bst; 213 bus_space_handle_t bsh; 214 struct resource *res; 215 216 res = sc->sc_bar_mappings[0].res; 217 218 bst = rman_get_bustag(res); 219 bsh = rman_get_bushandle(res); 220 221 /* configure the first W83877TF */ 222 bus_space_write_1(bst, bsh, 0x250, 0x89); 223 efir = 0x251; 224 efdr = 0x252; 225 val = rdspio(0x09) & 0x0f; 226 if (val != 0x0c) { 227 printf("conf_win877: Oops not a W83877TF\n"); 228 return (ENXIO); 229 } 230 231 #ifdef PUC_DEBUG 232 printf("before: "); 233 puc_print_win877(bst, bsh, efir, efdr); 234 #endif 235 236 val = rdspio(0x16); 237 val |= 0x04; 238 wrspio(0x16, val); 239 val &= ~0x04; 240 wrspio(0x16, val); 241 242 wrspio(0x24, 0x2e8 >> 2); 243 wrspio(0x25, 0x2f8 >> 2); 244 wrspio(0x17, 0x03); 245 wrspio(0x28, 0x43); 246 247 #ifdef PUC_DEBUG 248 printf("after: "); 249 puc_print_win877(bst, bsh, efir, efdr); 250 #endif 251 252 bus_space_write_1(bst, bsh, 0x250, 0xaa); 253 254 /* configure the second W83877TF */ 255 bus_space_write_1(bst, bsh, 0x3f0, 0x87); 256 bus_space_write_1(bst, bsh, 0x3f0, 0x87); 257 efir = 0x3f0; 258 efdr = 0x3f1; 259 val = rdspio(0x09) & 0x0f; 260 if (val != 0x0c) { 261 printf("conf_win877: Oops not a W83877TF\n"); 262 return(ENXIO); 263 } 264 265 #ifdef PUC_DEBUG 266 printf("before: "); 267 puc_print_win877(bst, bsh, efir, efdr); 268 #endif 269 270 val = rdspio(0x16); 271 val |= 0x04; 272 wrspio(0x16, val); 273 val &= ~0x04; 274 wrspio(0x16, val); 275 276 wrspio(0x24, 0x3e8 >> 2); 277 wrspio(0x25, 0x3f8 >> 2); 278 wrspio(0x17, 0x03); 279 wrspio(0x28, 0x43); 280 281 #ifdef PUC_DEBUG 282 printf("after: "); 283 puc_print_win877(bst, bsh, efir, efdr); 284 #endif 285 286 bus_space_write_1(bst, bsh, 0x3f0, 0xaa); 287 return (0); 288 } 289 290 #undef rdspio 291 #undef wrspio 292 293