2 * Copyright (c) 2003 M. Warner Losh. All Rights Reserved.
3 * Copyright (c) 2000,2001 Jonathan Chen. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
41 #include <machine/bus.h>
43 #include <machine/resource.h>
45 #include <sys/pciio.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pci_private.h>
50 #include <dev/cardbus/cardbusreg.h>
51 #include <dev/cardbus/cardbusvar.h>
52 #include <dev/cardbus/cardbus_cis.h>
53 #include <dev/pccard/pccardvar.h>
59 SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
61 int cardbus_debug = 0;
62 TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
63 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
67 int cardbus_cis_debug = 0;
68 TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug);
69 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
70 &cardbus_cis_debug, 0,
73 #define DPRINTF(a) if (cardbus_debug) printf a
74 #define DEVPRINTF(x) if (cardbus_debug) device_printf x
77 static int cardbus_attach(device_t cbdev);
78 static int cardbus_attach_card(device_t cbdev);
79 static int cardbus_detach(device_t cbdev);
80 static int cardbus_detach_card(device_t cbdev);
81 static void cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
83 static void cardbus_driver_added(device_t cbdev, driver_t *driver);
84 static int cardbus_probe(device_t cbdev);
85 static int cardbus_read_ivar(device_t cbdev, device_t child, int which,
87 static void cardbus_release_all_resources(device_t cbdev,
88 struct cardbus_devinfo *dinfo);
89 static int cardbus_write_ivar(device_t cbdev, device_t child, int which,
92 /************************************************************************/
94 /************************************************************************/
97 cardbus_probe(device_t cbdev)
99 device_set_desc(cbdev, "CardBus bus");
104 cardbus_attach(device_t cbdev)
110 cardbus_detach(device_t cbdev)
112 cardbus_detach_card(cbdev);
117 cardbus_suspend(device_t self)
119 cardbus_detach_card(self);
124 cardbus_resume(device_t self)
129 /************************************************************************/
130 /* Attach/Detach card */
131 /************************************************************************/
134 cardbus_device_setup_regs(device_t brdev, int b, int s, int f, pcicfgregs *cfg)
136 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_INTLINE,
137 pci_get_irq(device_get_parent(brdev)), 1);
138 cfg->intline = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_INTLINE, 1);
140 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1);
141 cfg->cachelnsz = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 1);
143 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 0xa8, 1);
144 cfg->lattimer = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 1);
146 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MINGNT, 0x14, 1);
147 cfg->mingnt = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MINGNT, 1);
149 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
150 cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
154 cardbus_attach_card(device_t cbdev)
156 device_t brdev = device_get_parent(cbdev);
157 int cardattached = 0;
160 cardbus_detach_card(cbdev); /* detach existing cards */
161 POWER_ENABLE_SOCKET(brdev, cbdev);
162 bus = pcib_get_bus(cbdev);
163 /* For each function, set it up and try to attach a driver to it */
164 for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {
165 int cardbusfunchigh = 0;
166 for (func = 0; func <= cardbusfunchigh; func++) {
167 struct cardbus_devinfo *dinfo;
169 dinfo = (struct cardbus_devinfo *)
170 pci_read_device(brdev, bus, slot, func,
171 sizeof(struct cardbus_devinfo));
174 if (dinfo->pci.cfg.mfdev)
175 cardbusfunchigh = CARDBUS_FUNCMAX;
177 cardbus_device_setup_regs(brdev, bus, slot, func,
179 dinfo->pci.cfg.dev = device_add_child(cbdev, NULL, -1);
180 if (!dinfo->pci.cfg.dev) {
181 DEVPRINTF((cbdev, "Cannot add child!\n"));
182 pci_freecfg((struct pci_devinfo *)dinfo);
185 resource_list_init(&dinfo->pci.resources);
186 device_set_ivars(dinfo->pci.cfg.dev, dinfo);
187 cardbus_do_cis(cbdev, dinfo->pci.cfg.dev);
188 pci_print_verbose(&dinfo->pci);
189 if (device_probe_and_attach(dinfo->pci.cfg.dev) != 0)
190 cardbus_release_all_resources(cbdev, dinfo);
196 if (cardattached > 0)
198 POWER_DISABLE_SOCKET(brdev, cbdev);
203 cardbus_detach_card(device_t cbdev)
210 device_get_children(cbdev, &devlist, &numdevs);
213 free(devlist, M_TEMP);
217 for (tmp = 0; tmp < numdevs; tmp++) {
218 struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]);
219 int status = device_get_state(devlist[tmp]);
221 if (dinfo->pci.cfg.dev != devlist[tmp])
222 device_printf(cbdev, "devinfo dev mismatch\n");
223 if (status == DS_ATTACHED || status == DS_BUSY)
224 device_detach(devlist[tmp]);
225 cardbus_release_all_resources(cbdev, dinfo);
226 device_delete_child(cbdev, devlist[tmp]);
227 pci_freecfg((struct pci_devinfo *)dinfo);
229 POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
230 free(devlist, M_TEMP);
235 cardbus_driver_added(device_t cbdev, driver_t *driver)
241 struct cardbus_devinfo *dinfo;
243 DEVICE_IDENTIFY(driver, cbdev);
244 device_get_children(cbdev, &devlist, &numdevs);
246 * If there are no drivers attached, but there are children,
247 * then power the card up.
249 for (i = 0; i < numdevs; i++) {
251 if (device_get_state(dev) != DS_NOTPRESENT)
254 if (i > 0 && i == numdevs)
255 POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
256 for (i = 0; i < numdevs; i++) {
258 if (device_get_state(dev) != DS_NOTPRESENT)
260 dinfo = device_get_ivars(dev);
261 pci_print_verbose(&dinfo->pci);
262 resource_list_init(&dinfo->pci.resources);
263 cardbus_do_cis(cbdev, dev);
264 if (device_probe_and_attach(dev) != 0)
265 cardbus_release_all_resources(cbdev, dinfo);
267 free(devlist, M_TEMP);
271 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo)
273 struct resource_list_entry *rle;
275 /* Free all allocated resources */
276 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
278 if (rman_get_device(rle->res) != cbdev)
279 device_printf(cbdev, "release_all_resource: "
280 "Resource still owned by child, oops. "
281 "(type=%d, rid=%d, addr=%lx)\n",
283 rman_get_start(rle->res));
284 BUS_RELEASE_RESOURCE(device_get_parent(cbdev),
285 cbdev, rle->type, rle->rid, rle->res);
288 * zero out config so the card won't acknowledge
289 * access to the space anymore
291 pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4);
294 resource_list_free(&dinfo->pci.resources);
297 /************************************************************************/
298 /* Other Bus Methods */
299 /************************************************************************/
302 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
304 struct cardbus_devinfo *dinfo;
307 dinfo = device_get_ivars(child);
308 cfg = &dinfo->pci.cfg;
311 case PCI_IVAR_ETHADDR:
313 * The generic accessor doesn't deal with failure, so
314 * we set the return value, then return an error.
316 if ((dinfo->fepresent & (1 << TPL_FUNCE_LAN_NID)) == 0) {
317 *((uint8_t **) result) = NULL;
320 *((uint8_t **) result) = dinfo->funce.lan.nid;
323 return (pci_read_ivar(cbdev, child, which, result));
329 cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)
331 return(pci_write_ivar(cbdev, child, which, value));
334 static device_method_t cardbus_methods[] = {
335 /* Device interface */
336 DEVMETHOD(device_probe, cardbus_probe),
337 DEVMETHOD(device_attach, cardbus_attach),
338 DEVMETHOD(device_detach, cardbus_detach),
339 DEVMETHOD(device_suspend, cardbus_suspend),
340 DEVMETHOD(device_resume, cardbus_resume),
343 DEVMETHOD(bus_read_ivar, cardbus_read_ivar),
344 DEVMETHOD(bus_write_ivar, cardbus_write_ivar),
345 DEVMETHOD(bus_driver_added, cardbus_driver_added),
348 DEVMETHOD(card_attach_card, cardbus_attach_card),
349 DEVMETHOD(card_detach_card, cardbus_detach_card),
354 DECLARE_CLASS(pci_driver);
355 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods, 0, pci_driver);
357 static devclass_t cardbus_devclass;
359 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
360 MODULE_VERSION(cardbus, 1);