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