1 /*- 2 * Copyright (c) 2003-2008 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 86 /************************************************************************/ 87 /* Probe/Attach */ 88 /************************************************************************/ 89 90 static int 91 cardbus_probe(device_t cbdev) 92 { 93 device_set_desc(cbdev, "CardBus bus"); 94 return (0); 95 } 96 97 static int 98 cardbus_attach(device_t cbdev) 99 { 100 struct cardbus_softc *sc; 101 102 sc = device_get_softc(cbdev); 103 sc->sc_dev = cbdev; 104 return (0); 105 } 106 107 static int 108 cardbus_detach(device_t cbdev) 109 { 110 111 cardbus_detach_card(cbdev); 112 return (0); 113 } 114 115 static int 116 cardbus_suspend(device_t self) 117 { 118 119 cardbus_detach_card(self); 120 return (0); 121 } 122 123 static int 124 cardbus_resume(device_t self) 125 { 126 127 return (0); 128 } 129 130 /************************************************************************/ 131 /* Attach/Detach card */ 132 /************************************************************************/ 133 134 static void 135 cardbus_device_setup_regs(pcicfgregs *cfg) 136 { 137 device_t dev = cfg->dev; 138 int i; 139 140 /* 141 * Some cards power up with garbage in their BARs. This 142 * code clears all that junk out. 143 */ 144 for (i = 0; i < PCIR_MAX_BAR_0; i++) 145 pci_write_config(dev, PCIR_BAR(i), 0, 4); 146 147 cfg->intline = 148 pci_get_irq(device_get_parent(device_get_parent(dev))); 149 pci_write_config(dev, PCIR_INTLINE, cfg->intline, 1); 150 pci_write_config(dev, PCIR_CACHELNSZ, 0x08, 1); 151 pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1); 152 pci_write_config(dev, PCIR_MINGNT, 0x14, 1); 153 pci_write_config(dev, PCIR_MAXLAT, 0x14, 1); 154 } 155 156 static int 157 cardbus_attach_card(device_t cbdev) 158 { 159 device_t brdev = device_get_parent(cbdev); 160 device_t child; 161 int bus, domain, slot, func; 162 int cardattached = 0; 163 int cardbusfunchigh = 0; 164 struct cardbus_softc *sc; 165 166 sc = device_get_softc(cbdev); 167 cardbus_detach_card(cbdev); /* detach existing cards */ 168 POWER_ENABLE_SOCKET(brdev, cbdev); 169 domain = pcib_get_domain(cbdev); 170 bus = pcib_get_bus(cbdev); 171 slot = 0; 172 /* For each function, set it up and try to attach a driver to it */ 173 for (func = 0; func <= cardbusfunchigh; func++) { 174 struct cardbus_devinfo *dinfo; 175 176 dinfo = (struct cardbus_devinfo *) 177 pci_read_device(brdev, domain, bus, slot, func, 178 sizeof(struct cardbus_devinfo)); 179 if (dinfo == NULL) 180 continue; 181 if (dinfo->pci.cfg.mfdev) 182 cardbusfunchigh = PCI_FUNCMAX; 183 184 child = device_add_child(cbdev, NULL, -1); 185 if (child == NULL) { 186 DEVPRINTF((cbdev, "Cannot add child!\n")); 187 pci_freecfg((struct pci_devinfo *)dinfo); 188 continue; 189 } 190 dinfo->pci.cfg.dev = child; 191 resource_list_init(&dinfo->pci.resources); 192 device_set_ivars(child, dinfo); 193 cardbus_device_create(sc, dinfo, cbdev, child); 194 if (cardbus_do_cis(cbdev, child) != 0) 195 DEVPRINTF((cbdev, "Warning: Bogus CIS ignored\n")); 196 pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 0); 197 pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci); 198 cardbus_device_setup_regs(&dinfo->pci.cfg); 199 pci_add_resources(cbdev, child, 1, dinfo->mprefetchable); 200 pci_print_verbose(&dinfo->pci); 201 if (device_probe_and_attach(child) == 0) 202 cardattached++; 203 else 204 pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1); 205 } 206 if (cardattached > 0) 207 return (0); 208 /* POWER_DISABLE_SOCKET(brdev, cbdev); */ 209 return (ENOENT); 210 } 211 212 static int 213 cardbus_detach_card(device_t cbdev) 214 { 215 int numdevs; 216 device_t *devlist; 217 int tmp; 218 int err = 0; 219 220 if (device_get_children(cbdev, &devlist, &numdevs) != 0) 221 return (ENOENT); 222 if (numdevs == 0) { 223 free(devlist, M_TEMP); 224 return (ENOENT); 225 } 226 227 for (tmp = 0; tmp < numdevs; tmp++) { 228 struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]); 229 int status = device_get_state(devlist[tmp]); 230 231 if (dinfo->pci.cfg.dev != devlist[tmp]) 232 device_printf(cbdev, "devinfo dev mismatch\n"); 233 if (status == DS_ATTACHED || status == DS_BUSY) 234 device_detach(devlist[tmp]); 235 cardbus_release_all_resources(cbdev, dinfo); 236 cardbus_device_destroy(dinfo); 237 device_delete_child(cbdev, devlist[tmp]); 238 pci_freecfg((struct pci_devinfo *)dinfo); 239 } 240 POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev); 241 free(devlist, M_TEMP); 242 return (err); 243 } 244 245 static void 246 cardbus_driver_added(device_t cbdev, driver_t *driver) 247 { 248 int numdevs; 249 device_t *devlist; 250 device_t dev; 251 int i; 252 struct cardbus_devinfo *dinfo; 253 254 DEVICE_IDENTIFY(driver, cbdev); 255 if (device_get_children(cbdev, &devlist, &numdevs) != 0) 256 return; 257 258 /* 259 * If there are no drivers attached, but there are children, 260 * then power the card up. 261 */ 262 for (i = 0; i < numdevs; i++) { 263 dev = devlist[i]; 264 if (device_get_state(dev) != DS_NOTPRESENT) 265 break; 266 } 267 if (i > 0 && i == numdevs) 268 POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev); 269 for (i = 0; i < numdevs; i++) { 270 dev = devlist[i]; 271 if (device_get_state(dev) != DS_NOTPRESENT) 272 continue; 273 dinfo = device_get_ivars(dev); 274 pci_print_verbose(&dinfo->pci); 275 if (bootverbose) 276 printf("pci%d:%d:%d:%d: reprobing on driver added\n", 277 dinfo->pci.cfg.domain, dinfo->pci.cfg.bus, 278 dinfo->pci.cfg.slot, dinfo->pci.cfg.func); 279 pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci); 280 if (device_probe_and_attach(dev) != 0) 281 pci_cfg_save(dev, &dinfo->pci, 1); 282 } 283 free(devlist, M_TEMP); 284 } 285 286 static void 287 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo) 288 { 289 struct resource_list_entry *rle; 290 device_t dev; 291 292 /* Turn off access to resources we're about to free */ 293 dev = dinfo->pci.cfg.dev; 294 pci_write_config(dev, PCIR_COMMAND, 295 pci_read_config(dev, PCIR_COMMAND, 2) & 296 ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN), 2); 297 /* Free all allocated resources */ 298 STAILQ_FOREACH(rle, &dinfo->pci.resources, link) { 299 if (rle->res) { 300 BUS_RELEASE_RESOURCE(device_get_parent(cbdev), 301 cbdev, rle->type, rle->rid, rle->res); 302 rle->res = NULL; 303 } 304 } 305 resource_list_free(&dinfo->pci.resources); 306 } 307 308 /************************************************************************/ 309 /* Other Bus Methods */ 310 /************************************************************************/ 311 312 static int 313 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result) 314 { 315 struct cardbus_devinfo *dinfo; 316 pcicfgregs *cfg; 317 318 dinfo = device_get_ivars(child); 319 cfg = &dinfo->pci.cfg; 320 321 switch (which) { 322 case PCI_IVAR_ETHADDR: 323 /* 324 * The generic accessor doesn't deal with failure, so 325 * we set the return value, then return an error. 326 */ 327 if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) { 328 *((uint8_t **) result) = dinfo->funce.lan.nid; 329 break; 330 } 331 *((uint8_t **) result) = NULL; 332 return (EINVAL); 333 default: 334 return (pci_read_ivar(cbdev, child, which, result)); 335 } 336 return 0; 337 } 338 339 static device_method_t cardbus_methods[] = { 340 /* Device interface */ 341 DEVMETHOD(device_probe, cardbus_probe), 342 DEVMETHOD(device_attach, cardbus_attach), 343 DEVMETHOD(device_detach, cardbus_detach), 344 DEVMETHOD(device_suspend, cardbus_suspend), 345 DEVMETHOD(device_resume, cardbus_resume), 346 347 /* Bus interface */ 348 DEVMETHOD(bus_read_ivar, cardbus_read_ivar), 349 DEVMETHOD(bus_write_ivar, pci_write_ivar), 350 DEVMETHOD(bus_driver_added, cardbus_driver_added), 351 352 /* Card Interface */ 353 DEVMETHOD(card_attach_card, cardbus_attach_card), 354 DEVMETHOD(card_detach_card, cardbus_detach_card), 355 356 {0,0} 357 }; 358 359 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods, 360 sizeof(struct cardbus_softc), pci_driver); 361 362 static devclass_t cardbus_devclass; 363 364 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0); 365 MODULE_VERSION(cardbus, 1); 366