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 * 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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
38 #include <machine/bus.h>
40 #include <machine/resource.h>
42 #include <sys/pciio.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pci_private.h>
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>
57 SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
59 int cardbus_debug = 0;
60 TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
61 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
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,
71 #define DPRINTF(a) if (cardbus_debug) printf a
72 #define DEVPRINTF(x) if (cardbus_debug) device_printf x
75 static void cardbus_add_map(device_t cbdev, device_t child, int reg);
76 static int cardbus_alloc_resources(device_t cbdev, device_t child);
77 static int cardbus_attach(device_t cbdev);
78 static int cardbus_attach_card(device_t cbdev);
79 static int cardbus_barsort(const void *a, const void *b);
80 static int cardbus_detach(device_t cbdev);
81 static int cardbus_detach_card(device_t cbdev);
82 static void cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
84 static void cardbus_driver_added(device_t cbdev, driver_t *driver);
85 static void cardbus_pickup_maps(device_t cbdev, device_t child);
86 static int cardbus_probe(device_t cbdev);
87 static int cardbus_read_ivar(device_t cbdev, device_t child, int which,
89 static void cardbus_release_all_resources(device_t cbdev,
90 struct cardbus_devinfo *dinfo);
91 static int cardbus_write_ivar(device_t cbdev, device_t child, int which,
98 * Adding a memory/io resource (sans CIS)
102 cardbus_add_map(device_t cbdev, device_t child, int reg)
104 struct cardbus_devinfo *dinfo = device_get_ivars(child);
105 struct resource_list_entry *rle;
110 STAILQ_FOREACH(rle, &dinfo->pci.resources, link) {
115 if (reg == CARDBUS_ROM_REG)
116 testval = CARDBUS_ROM_ADDRMASK;
120 pci_write_config(child, reg, testval, 4);
121 testval = pci_read_config(child, reg, 4);
123 if (testval == ~0 || testval == 0)
126 if ((testval & 1) == 0)
127 type = SYS_RES_MEMORY;
129 type = SYS_RES_IOPORT;
131 size = CARDBUS_MAPREG_MEM_SIZE(testval);
132 device_printf(cbdev, "Resource not specified in CIS: id=%x, size=%x\n",
134 resource_list_add(&dinfo->pci.resources, type, reg, 0UL, ~0UL, size);
138 cardbus_pickup_maps(device_t cbdev, device_t child)
140 struct cardbus_devinfo *dinfo = device_get_ivars(child);
144 * Try to pick up any resources that was not specified in CIS.
145 * Maybe this isn't any longer necessary now that we have fixed
146 * CIS parsing and we should filter things here? XXX
148 for (reg = 0; reg < dinfo->pci.cfg.nummaps; reg++)
149 cardbus_add_map(cbdev, child, PCIR_BAR(reg));
153 cardbus_do_res(struct resource_list_entry *rle, device_t child, uint32_t start)
156 rle->end = start + rle->count - 1;
157 pci_write_config(child, rle->rid, rle->start, 4);
161 cardbus_barsort(const void *a, const void *b)
163 return ((*(const struct resource_list_entry * const *)b)->count -
164 (*(const struct resource_list_entry * const *)a)->count);
167 /* XXX this function is too long */
169 cardbus_alloc_resources(device_t cbdev, device_t child)
171 struct cardbus_devinfo *dinfo = device_get_ivars(child);
173 struct resource_list_entry *rle;
174 struct resource_list_entry **barlist;
176 uint32_t mem_psize = 0, mem_nsize = 0, io_size = 0;
177 struct resource *res;
182 STAILQ_FOREACH(rle, &dinfo->pci.resources, link) {
187 barlist = malloc(sizeof(struct resource_list_entry*) * count, M_DEVBUF,
190 STAILQ_FOREACH(rle, &dinfo->pci.resources, link) {
191 barlist[count] = rle;
192 if (rle->type == SYS_RES_IOPORT) {
193 io_size += rle->count;
194 } else if (rle->type == SYS_RES_MEMORY) {
195 if (dinfo->mprefetchable & BARBIT(rle->rid))
196 mem_psize += rle->count;
198 mem_nsize += rle->count;
204 * We want to allocate the largest resource first, so that our
205 * allocated memory is packed.
207 qsort(barlist, count, sizeof(struct resource_list_entry *),
210 /* Allocate prefetchable memory */
212 for (tmp = 0; tmp < count; tmp++) {
214 if (rle->res == NULL &&
215 rle->type == SYS_RES_MEMORY &&
216 dinfo->mprefetchable & BARBIT(rle->rid)) {
217 flags = rman_make_alignment_flags(rle->count);
221 if (flags > 0) { /* If any prefetchable memory is requested... */
223 * First we allocate one big space for all resources of this
224 * type. We do this because our parent, pccbb, needs to open
225 * a window to forward all addresses within the window, and
226 * it would be best if nobody else has resources allocated
228 * (XXX: Perhaps there might be a better way to do this?)
231 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
232 (dinfo->mprefetchable & dinfo->mbelow1mb)?0xFFFFF:~0UL,
236 "Can't get memory for prefetch mem\n");
237 free(barlist, M_DEVBUF);
240 start = rman_get_start(res);
241 end = rman_get_end(res);
242 DEVPRINTF((cbdev, "Prefetchable memory at %x-%x\n", start, end));
244 * Now that we know the region is free, release it and hand it
245 * out piece by piece.
247 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
248 for (tmp = 0; tmp < count; tmp++) {
250 if (rle->type == SYS_RES_MEMORY &&
251 dinfo->mprefetchable & BARBIT(rle->rid)) {
252 cardbus_do_res(rle, child, start);
258 /* Allocate non-prefetchable memory */
260 for (tmp = 0; tmp < count; tmp++) {
262 if (rle->type == SYS_RES_MEMORY &&
263 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
264 flags = rman_make_alignment_flags(rle->count);
268 if (flags > 0) { /* If any non-prefetchable memory is requested... */
270 * First we allocate one big space for all resources of this
271 * type. We do this because our parent, pccbb, needs to open
272 * a window to forward all addresses within the window, and
273 * it would be best if nobody else has resources allocated
275 * (XXX: Perhaps there might be a better way to do this?)
278 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
279 ((~dinfo->mprefetchable) & dinfo->mbelow1mb)?0xFFFFF:~0UL,
283 "Can't get memory for non-prefetch mem\n");
284 free(barlist, M_DEVBUF);
287 start = rman_get_start(res);
288 end = rman_get_end(res);
289 DEVPRINTF((cbdev, "Non-prefetchable memory at %x-%x\n",
292 * Now that we know the region is free, release it and hand it
293 * out piece by piece.
295 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
296 for (tmp = 0; tmp < count; tmp++) {
298 if (rle->type == SYS_RES_MEMORY &&
299 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
300 cardbus_do_res(rle, child, start);
306 /* Allocate IO ports */
308 for (tmp = 0; tmp < count; tmp++) {
310 if (rle->type == SYS_RES_IOPORT) {
311 flags = rman_make_alignment_flags(rle->count);
315 if (flags > 0) { /* If any IO port is requested... */
317 * First we allocate one big space for all resources of this
318 * type. We do this because our parent, pccbb, needs to open
319 * a window to forward all addresses within the window, and
320 * it would be best if nobody else has resources allocated
322 * (XXX: Perhaps there might be a better way to do this?)
325 res = bus_alloc_resource(cbdev, SYS_RES_IOPORT, &rid, 0,
326 (dinfo->ibelow1mb)?0xFFFFF:~0UL, io_size, flags);
329 "Can't get memory for IO ports\n");
330 free(barlist, M_DEVBUF);
333 start = rman_get_start(res);
334 end = rman_get_end(res);
335 DEVPRINTF((cbdev, "IO port at %x-%x\n", start, end));
337 * Now that we know the region is free, release it and hand it
338 * out piece by piece.
340 bus_release_resource(cbdev, SYS_RES_IOPORT, rid, res);
341 for (tmp = 0; tmp < count; tmp++) {
343 if (rle->type == SYS_RES_IOPORT) {
344 cardbus_do_res(rle, child, start);
352 res = bus_alloc_resource_any(cbdev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
354 device_printf(cbdev, "Can't get memory for irq\n");
355 free(barlist, M_DEVBUF);
358 start = rman_get_start(res);
359 end = rman_get_end(res);
360 bus_release_resource(cbdev, SYS_RES_IRQ, rid, res);
361 resource_list_add(&dinfo->pci.resources, SYS_RES_IRQ, rid, start, end,
363 dinfo->pci.cfg.intline = rman_get_start(res);
364 pci_write_config(child, PCIR_INTLINE, rman_get_start(res), 1);
366 free(barlist, M_DEVBUF);
370 /************************************************************************/
372 /************************************************************************/
375 cardbus_probe(device_t cbdev)
377 device_set_desc(cbdev, "CardBus bus");
382 cardbus_attach(device_t cbdev)
388 cardbus_detach(device_t cbdev)
390 cardbus_detach_card(cbdev);
395 cardbus_suspend(device_t self)
397 cardbus_detach_card(self);
402 cardbus_resume(device_t self)
407 /************************************************************************/
408 /* Attach/Detach card */
409 /************************************************************************/
412 cardbus_device_setup_regs(device_t brdev, int b, int s, int f, pcicfgregs *cfg)
414 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_INTLINE,
415 pci_get_irq(device_get_parent(brdev)), 1);
416 cfg->intline = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_INTLINE, 1);
418 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1);
419 cfg->cachelnsz = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 1);
421 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 0xa8, 1);
422 cfg->lattimer = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 1);
424 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MINGNT, 0x14, 1);
425 cfg->mingnt = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MINGNT, 1);
427 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
428 cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
432 cardbus_attach_card(device_t cbdev)
434 device_t brdev = device_get_parent(cbdev);
436 int cardattached = 0;
439 cardbus_detach_card(cbdev); /* detach existing cards */
440 POWER_ENABLE_SOCKET(brdev, cbdev);
441 bus = pcib_get_bus(cbdev);
442 /* For each function, set it up and try to attach a driver to it */
443 for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {
444 int cardbusfunchigh = 0;
445 for (func = 0; func <= cardbusfunchigh; func++) {
446 struct cardbus_devinfo *dinfo;
448 dinfo = (struct cardbus_devinfo *)
449 pci_read_device(brdev, bus, slot, func,
450 sizeof(struct cardbus_devinfo));
453 if (dinfo->pci.cfg.mfdev)
454 cardbusfunchigh = CARDBUS_FUNCMAX;
456 cardbus_device_setup_regs(brdev, bus, slot, func,
458 child = device_add_child(cbdev, NULL, -1);
460 DEVPRINTF((cbdev, "Cannot add child!\n"));
461 pci_freecfg((struct pci_devinfo *)dinfo);
464 dinfo->pci.cfg.dev = child;
465 resource_list_init(&dinfo->pci.resources);
466 device_set_ivars(child, dinfo);
467 if (cardbus_do_cis(cbdev, child) != 0) {
468 DEVPRINTF((cbdev, "Can't parse cis\n"));
469 pci_freecfg((struct pci_devinfo *)dinfo);
472 cardbus_pickup_maps(cbdev, child);
473 cardbus_alloc_resources(cbdev, child);
474 pci_print_verbose(&dinfo->pci);
475 if (device_probe_and_attach(child) != 0)
476 cardbus_release_all_resources(cbdev, dinfo);
482 if (cardattached > 0)
484 POWER_DISABLE_SOCKET(brdev, cbdev);
489 cardbus_detach_card(device_t cbdev)
496 device_get_children(cbdev, &devlist, &numdevs);
499 free(devlist, M_TEMP);
503 for (tmp = 0; tmp < numdevs; tmp++) {
504 struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]);
505 int status = device_get_state(devlist[tmp]);
507 if (dinfo->pci.cfg.dev != devlist[tmp])
508 device_printf(cbdev, "devinfo dev mismatch\n");
509 if (status == DS_ATTACHED || status == DS_BUSY)
510 device_detach(devlist[tmp]);
511 cardbus_release_all_resources(cbdev, dinfo);
512 device_delete_child(cbdev, devlist[tmp]);
513 pci_freecfg((struct pci_devinfo *)dinfo);
515 POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
516 free(devlist, M_TEMP);
521 cardbus_driver_added(device_t cbdev, driver_t *driver)
527 struct cardbus_devinfo *dinfo;
529 DEVICE_IDENTIFY(driver, cbdev);
530 device_get_children(cbdev, &devlist, &numdevs);
532 * If there are no drivers attached, but there are children,
533 * then power the card up.
535 for (i = 0; i < numdevs; i++) {
537 if (device_get_state(dev) != DS_NOTPRESENT)
540 if (i > 0 && i == numdevs)
541 POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
542 for (i = 0; i < numdevs; i++) {
544 if (device_get_state(dev) != DS_NOTPRESENT)
546 dinfo = device_get_ivars(dev);
547 pci_print_verbose(&dinfo->pci);
548 resource_list_init(&dinfo->pci.resources);
549 cardbus_do_cis(cbdev, dev);
550 cardbus_pickup_maps(cbdev, dev);
551 cardbus_alloc_resources(cbdev, dev);
552 if (device_probe_and_attach(dev) != 0)
553 cardbus_release_all_resources(cbdev, dinfo);
555 free(devlist, M_TEMP);
559 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo)
561 struct resource_list_entry *rle;
563 /* Free all allocated resources */
564 STAILQ_FOREACH(rle, &dinfo->pci.resources, link) {
566 if (rman_get_device(rle->res) != cbdev)
567 device_printf(cbdev, "release_all_resource: "
568 "Resource still owned by child, oops. "
569 "(type=%d, rid=%d, addr=%lx)\n",
571 rman_get_start(rle->res));
572 BUS_RELEASE_RESOURCE(device_get_parent(cbdev),
573 cbdev, rle->type, rle->rid, rle->res);
576 * zero out config so the card won't acknowledge
577 * access to the space anymore
579 pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4);
582 resource_list_free(&dinfo->pci.resources);
585 /************************************************************************/
586 /* Other Bus Methods */
587 /************************************************************************/
590 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
592 struct cardbus_devinfo *dinfo;
595 dinfo = device_get_ivars(child);
596 cfg = &dinfo->pci.cfg;
599 case PCI_IVAR_ETHADDR:
601 * The generic accessor doesn't deal with failure, so
602 * we set the return value, then return an error.
604 if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
605 *((uint8_t **) result) = dinfo->funce.lan.nid;
608 *((uint8_t **) result) = NULL;
611 return (pci_read_ivar(cbdev, child, which, result));
617 cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)
619 return(pci_write_ivar(cbdev, child, which, value));
622 static device_method_t cardbus_methods[] = {
623 /* Device interface */
624 DEVMETHOD(device_probe, cardbus_probe),
625 DEVMETHOD(device_attach, cardbus_attach),
626 DEVMETHOD(device_detach, cardbus_detach),
627 DEVMETHOD(device_suspend, cardbus_suspend),
628 DEVMETHOD(device_resume, cardbus_resume),
631 DEVMETHOD(bus_read_ivar, cardbus_read_ivar),
632 DEVMETHOD(bus_write_ivar, cardbus_write_ivar),
633 DEVMETHOD(bus_driver_added, cardbus_driver_added),
636 DEVMETHOD(card_attach_card, cardbus_attach_card),
637 DEVMETHOD(card_detach_card, cardbus_detach_card),
642 DECLARE_CLASS(pci_driver);
643 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods, 0, pci_driver);
645 static devclass_t cardbus_devclass;
647 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
648 MODULE_VERSION(cardbus, 1);