1 /*- 2 * Copyright (c) 2003 M. Warner Losh. All Rights Reserved. 3 * Copyright (c) 2000,2001 Jonathan Chen. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/malloc.h> 33 #include <sys/module.h> 34 #include <sys/kernel.h> 35 #include <sys/sysctl.h> 36 37 #include <sys/bus.h> 38 #include <machine/bus.h> 39 #include <sys/rman.h> 40 #include <machine/resource.h> 41 42 #include <sys/pciio.h> 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pci_private.h> 46 47 #include <dev/cardbus/cardbusreg.h> 48 #include <dev/cardbus/cardbusvar.h> 49 #include <dev/cardbus/cardbus_cis.h> 50 #include <dev/pccard/pccard_cis.h> 51 #include <dev/pccard/pccardvar.h> 52 53 #include "power_if.h" 54 #include "pcib_if.h" 55 56 /* sysctl vars */ 57 SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters"); 58 59 int cardbus_debug = 0; 60 TUNABLE_INT("hw.cardbus.debug", &cardbus_debug); 61 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW, 62 &cardbus_debug, 0, 63 "CardBus debug"); 64 65 int cardbus_cis_debug = 0; 66 TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug); 67 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW, 68 &cardbus_cis_debug, 0, 69 "CardBus CIS debug"); 70 71 #define DPRINTF(a) if (cardbus_debug) printf a 72 #define DEVPRINTF(x) if (cardbus_debug) device_printf x 73 74 static int cardbus_attach(device_t cbdev); 75 static int cardbus_attach_card(device_t cbdev); 76 static int cardbus_detach(device_t cbdev); 77 static int cardbus_detach_card(device_t cbdev); 78 static void cardbus_device_setup_regs(pcicfgregs *cfg); 79 static void cardbus_driver_added(device_t cbdev, driver_t *driver); 80 static int cardbus_probe(device_t cbdev); 81 static int cardbus_read_ivar(device_t cbdev, device_t child, int which, 82 uintptr_t *result); 83 static void cardbus_release_all_resources(device_t cbdev, 84 struct cardbus_devinfo *dinfo); 85 static int cardbus_write_ivar(device_t cbdev, device_t child, int which, 86 uintptr_t value); 87 88 /************************************************************************/ 89 /* Probe/Attach */ 90 /************************************************************************/ 91 92 static int 93 cardbus_probe(device_t cbdev) 94 { 95 device_set_desc(cbdev, "CardBus bus"); 96 return (0); 97 } 98 99 static int 100 cardbus_attach(device_t cbdev) 101 { 102 struct cardbus_softc *sc = device_get_softc(cbdev); 103 104 sc->sc_dev = cbdev; 105 cardbus_device_create(sc); 106 return (0); 107 } 108 109 static int 110 cardbus_detach(device_t cbdev) 111 { 112 struct cardbus_softc *sc = device_get_softc(cbdev); 113 114 cardbus_detach_card(cbdev); 115 cardbus_device_destroy(sc); 116 return (0); 117 } 118 119 static int 120 cardbus_suspend(device_t self) 121 { 122 123 cardbus_detach_card(self); 124 return (0); 125 } 126 127 static int 128 cardbus_resume(device_t self) 129 { 130 131 return (0); 132 } 133 134 /************************************************************************/ 135 /* Attach/Detach card */ 136 /************************************************************************/ 137 138 static void 139 cardbus_device_setup_regs(pcicfgregs *cfg) 140 { 141 device_t dev = cfg->dev; 142 int i; 143 144 /* 145 * Some cards power up with garbage in their BARs. This 146 * code clears all that junk out. 147 */ 148 for (i = 0; i < PCI_MAX_BAR_0; i++) 149 pci_write_config(dev, PCIR_BAR(i), 0, 4); 150 151 cfg->intline = 152 pci_get_irq(device_get_parent(device_get_parent(dev))); 153 pci_write_config(dev, PCIR_INTLINE, cfg->intline, 1); 154 pci_write_config(dev, PCIR_CACHELNSZ, 0x08, 1); 155 pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1); 156 pci_write_config(dev, PCIR_MINGNT, 0x14, 1); 157 pci_write_config(dev, PCIR_MAXLAT, 0x14, 1); 158 } 159 160 static int 161 cardbus_attach_card(device_t cbdev) 162 { 163 device_t brdev = device_get_parent(cbdev); 164 device_t child; 165 int bus, domain, slot, func; 166 int cardattached = 0; 167 int cardbusfunchigh = 0; 168 169 cardbus_detach_card(cbdev); /* detach existing cards */ 170 POWER_ENABLE_SOCKET(brdev, cbdev); 171 domain = pcib_get_domain(cbdev); 172 bus = pcib_get_bus(cbdev); 173 slot = 0; 174 /* For each function, set it up and try to attach a driver to it */ 175 for (func = 0; func <= cardbusfunchigh; func++) { 176 struct cardbus_devinfo *dinfo; 177 178 dinfo = (struct cardbus_devinfo *) 179 pci_read_device(brdev, domain, bus, slot, func, 180 sizeof(struct cardbus_devinfo)); 181 if (dinfo == NULL) 182 continue; 183 if (dinfo->pci.cfg.mfdev) 184 cardbusfunchigh = PCI_FUNCMAX; 185 186 child = device_add_child(cbdev, NULL, -1); 187 if (child == NULL) { 188 DEVPRINTF((cbdev, "Cannot add child!\n")); 189 pci_freecfg((struct pci_devinfo *)dinfo); 190 continue; 191 } 192 dinfo->pci.cfg.dev = child; 193 resource_list_init(&dinfo->pci.resources); 194 device_set_ivars(child, dinfo); 195 if (cardbus_do_cis(cbdev, child) != 0) 196 DEVPRINTF((cbdev, "Warning: Bogus CIS ignored\n")); 197 pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 0); 198 pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci); 199 cardbus_device_setup_regs(&dinfo->pci.cfg); 200 pci_add_resources(cbdev, child, 1, dinfo->mprefetchable); 201 pci_print_verbose(&dinfo->pci); 202 if (device_probe_and_attach(child) == 0) 203 cardattached++; 204 else 205 pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1); 206 } 207 if (cardattached > 0) 208 return (0); 209 POWER_DISABLE_SOCKET(brdev, cbdev); 210 return (ENOENT); 211 } 212 213 static int 214 cardbus_detach_card(device_t cbdev) 215 { 216 int numdevs; 217 device_t *devlist; 218 int tmp; 219 int err = 0; 220 221 if (device_get_children(cbdev, &devlist, &numdevs) != 0) 222 return (ENOENT); 223 224 if (numdevs == 0) { 225 free(devlist, M_TEMP); 226 return (ENOENT); 227 } 228 229 for (tmp = 0; tmp < numdevs; tmp++) { 230 struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]); 231 int status = device_get_state(devlist[tmp]); 232 233 if (dinfo->pci.cfg.dev != devlist[tmp]) 234 device_printf(cbdev, "devinfo dev mismatch\n"); 235 if (status == DS_ATTACHED || status == DS_BUSY) 236 device_detach(devlist[tmp]); 237 cardbus_release_all_resources(cbdev, dinfo); 238 device_delete_child(cbdev, devlist[tmp]); 239 pci_freecfg((struct pci_devinfo *)dinfo); 240 } 241 POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev); 242 free(devlist, M_TEMP); 243 return (err); 244 } 245 246 static void 247 cardbus_driver_added(device_t cbdev, driver_t *driver) 248 { 249 int numdevs; 250 device_t *devlist; 251 device_t dev; 252 int i; 253 struct cardbus_devinfo *dinfo; 254 255 DEVICE_IDENTIFY(driver, cbdev); 256 if (device_get_children(cbdev, &devlist, &numdevs) != 0) 257 return; 258 259 /* 260 * If there are no drivers attached, but there are children, 261 * then power the card up. 262 */ 263 for (i = 0; i < numdevs; i++) { 264 dev = devlist[i]; 265 if (device_get_state(dev) != DS_NOTPRESENT) 266 break; 267 } 268 if (i > 0 && i == numdevs) 269 POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev); 270 for (i = 0; i < numdevs; i++) { 271 dev = devlist[i]; 272 if (device_get_state(dev) != DS_NOTPRESENT) 273 continue; 274 dinfo = device_get_ivars(dev); 275 pci_print_verbose(&dinfo->pci); 276 pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci); 277 if (device_probe_and_attach(dev) != 0) 278 pci_cfg_save(dev, &dinfo->pci, 1); 279 } 280 free(devlist, M_TEMP); 281 } 282 283 static void 284 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo) 285 { 286 struct resource_list_entry *rle; 287 288 /* Free all allocated resources */ 289 STAILQ_FOREACH(rle, &dinfo->pci.resources, link) { 290 if (rle->res) { 291 BUS_RELEASE_RESOURCE(device_get_parent(cbdev), 292 cbdev, rle->type, rle->rid, rle->res); 293 rle->res = NULL; 294 /* 295 * zero out config so the card won't acknowledge 296 * access to the space anymore. XXX doesn't handle 297 * 64-bit bars. 298 */ 299 pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4); 300 } 301 } 302 resource_list_free(&dinfo->pci.resources); 303 } 304 305 /************************************************************************/ 306 /* Other Bus Methods */ 307 /************************************************************************/ 308 309 static int 310 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result) 311 { 312 struct cardbus_devinfo *dinfo; 313 pcicfgregs *cfg; 314 315 dinfo = device_get_ivars(child); 316 cfg = &dinfo->pci.cfg; 317 318 switch (which) { 319 case PCI_IVAR_ETHADDR: 320 /* 321 * The generic accessor doesn't deal with failure, so 322 * we set the return value, then return an error. 323 */ 324 if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) { 325 *((uint8_t **) result) = dinfo->funce.lan.nid; 326 break; 327 } 328 *((uint8_t **) result) = NULL; 329 return (EINVAL); 330 default: 331 return (pci_read_ivar(cbdev, child, which, result)); 332 } 333 return 0; 334 } 335 336 static int 337 cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value) 338 { 339 return(pci_write_ivar(cbdev, child, which, value)); 340 } 341 342 static device_method_t cardbus_methods[] = { 343 /* Device interface */ 344 DEVMETHOD(device_probe, cardbus_probe), 345 DEVMETHOD(device_attach, cardbus_attach), 346 DEVMETHOD(device_detach, cardbus_detach), 347 DEVMETHOD(device_suspend, cardbus_suspend), 348 DEVMETHOD(device_resume, cardbus_resume), 349 350 /* Bus interface */ 351 DEVMETHOD(bus_read_ivar, cardbus_read_ivar), 352 DEVMETHOD(bus_write_ivar, cardbus_write_ivar), 353 DEVMETHOD(bus_driver_added, cardbus_driver_added), 354 355 /* Card Interface */ 356 DEVMETHOD(card_attach_card, cardbus_attach_card), 357 DEVMETHOD(card_detach_card, cardbus_detach_card), 358 359 {0,0} 360 }; 361 362 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods, 363 sizeof(struct cardbus_softc), pci_driver); 364 365 static devclass_t cardbus_devclass; 366 367 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0); 368 MODULE_VERSION(cardbus, 1); 369