Update cardbus/pccard support.
[dragonfly/port-amd64.git] / sys / dev / pccard / cardbus / cardbus.c
blobce16c2806f2d4ebb31c0d41b14ad3eb7d5e00058
1 /*-
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
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.
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.
26 * $FreeBSD: src/sys/dev/cardbus/cardbus.c,v 1.28 2002/11/27 17:30:41 imp Exp $
27 * $DragonFly: src/sys/dev/pccard/cardbus/cardbus.c,v 1.11 2007/07/05 12:08:54 sephe Exp $
31 * Cardbus Bus Driver
33 * much of the bus code was stolen directly from sys/pci/pci.c
34 * (Copyright (c) 1997, Stefan Esser <se@freebsd.org>)
36 * Written by Jonathan Chen <jon@freebsd.org>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
46 #include <sys/bus.h>
47 #include <sys/rman.h>
49 #include <sys/pciio.h>
50 #include <bus/pci/pcivar.h>
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pci_private.h>
54 #include <dev/pccard/cardbus/cardbusreg.h>
55 #include <dev/pccard/cardbus/cardbusvar.h>
56 #include <dev/pccard/cardbus/cardbus_cis.h>
57 #include <bus/pccard/pccard_cis.h>
58 #include <bus/pccard/pccardvar.h>
60 #include "power_if.h"
61 #include "pcib_if.h"
63 /* sysctl vars */
64 SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
66 int cardbus_debug = 0;
67 TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
68 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
69 &cardbus_debug, 0,
70 "CardBus debug");
72 int cardbus_cis_debug = 0;
73 TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug);
74 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
75 &cardbus_cis_debug, 0,
76 "CardBus CIS debug");
78 #define DPRINTF(a) if (cardbus_debug) kprintf a
79 #define DEVPRINTF(x) if (cardbus_debug) device_printf x
82 static void cardbus_add_map(device_t cbdev, device_t child, int reg);
83 static int cardbus_alloc_resources(device_t cbdev, device_t child);
84 static int cardbus_attach(device_t cbdev);
85 static int cardbus_attach_card(device_t cbdev);
86 static int cardbus_barsort(const void *a, const void *b);
87 static int cardbus_detach(device_t cbdev);
88 static int cardbus_detach_card(device_t cbdev);
89 static void cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
90 pcicfgregs *cfg);
91 static void cardbus_driver_added(device_t cbdev, driver_t *driver);
92 static void cardbus_pickup_maps(device_t cbdev, device_t child);
93 static int cardbus_probe(device_t cbdev);
94 static int cardbus_read_ivar(device_t cbdev, device_t child, int which,
95 uintptr_t *result);
96 static void cardbus_release_all_resources(device_t cbdev,
97 struct cardbus_devinfo *dinfo);
98 static int cardbus_write_ivar(device_t cbdev, device_t child, int which,
99 uintptr_t value);
102 * Resource allocation
105 * Adding a memory/io resource (sans CIS)
108 static void
109 cardbus_add_map(device_t cbdev, device_t child, int reg)
111 struct cardbus_devinfo *dinfo = device_get_ivars(child);
112 struct resource_list_entry *rle;
113 uint32_t size;
114 uint32_t testval;
115 int type;
117 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
118 if (rle->rid == reg)
119 return;
122 if (reg == CARDBUS_ROM_REG)
123 testval = CARDBUS_ROM_ADDRMASK;
124 else
125 testval = ~0;
127 pci_write_config(child, reg, testval, 4);
128 testval = pci_read_config(child, reg, 4);
130 if (testval == ~0 || testval == 0)
131 return;
133 if ((testval & 1) == 0)
134 type = SYS_RES_MEMORY;
135 else
136 type = SYS_RES_IOPORT;
138 size = CARDBUS_MAPREG_MEM_SIZE(testval);
139 device_printf(cbdev, "Resource not specified in CIS: id=%x, size=%x\n",
140 reg, size);
141 resource_list_add(&dinfo->pci.resources, type, reg, 0UL, ~0UL, size);
144 static void
145 cardbus_pickup_maps(device_t cbdev, device_t child)
147 struct cardbus_devinfo *dinfo = device_get_ivars(child);
148 int reg;
151 * Try to pick up any resources that was not specified in CIS.
152 * Maybe this isn't any longer necessary now that we have fixed
153 * CIS parsing and we should filter things here? XXX
155 for (reg = 0; reg < dinfo->pci.cfg.nummaps; reg++)
156 cardbus_add_map(cbdev, child, PCIR_BAR(reg));
159 static void
160 cardbus_do_res(struct resource_list_entry *rle, device_t child, uint32_t start)
162 rle->start = start;
163 rle->end = start + rle->count - 1;
164 pci_write_config(child, rle->rid, rle->start, 4);
167 static int
168 cardbus_barsort(const void *a, const void *b)
170 return ((*(const struct resource_list_entry * const *)b)->count -
171 (*(const struct resource_list_entry * const *)a)->count);
174 /* XXX this function is too long */
175 static int
176 cardbus_alloc_resources(device_t cbdev, device_t child)
178 struct cardbus_devinfo *dinfo = device_get_ivars(child);
179 int count;
180 struct resource_list_entry *rle;
181 struct resource_list_entry **barlist;
182 int tmp;
183 uint32_t mem_psize = 0, mem_nsize = 0, io_size = 0;
184 struct resource *res;
185 uint32_t start,end;
186 int rid, flags;
188 count = 0;
189 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
190 count++;
192 if (count == 0)
193 return (0);
194 barlist = kmalloc(sizeof(struct resource_list_entry*) * count, M_DEVBUF,
195 M_WAITOK);
196 count = 0;
197 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
198 barlist[count] = rle;
199 if (rle->type == SYS_RES_IOPORT) {
200 io_size += rle->count;
201 } else if (rle->type == SYS_RES_MEMORY) {
202 if (dinfo->mprefetchable & BARBIT(rle->rid))
203 mem_psize += rle->count;
204 else
205 mem_nsize += rle->count;
207 count++;
211 * We want to allocate the largest resource first, so that our
212 * allocated memory is packed.
214 kqsort(barlist, count, sizeof(struct resource_list_entry *),
215 cardbus_barsort);
217 /* Allocate prefetchable memory */
218 flags = 0;
219 for (tmp = 0; tmp < count; tmp++) {
220 rle = barlist[tmp];
221 if (rle->res == NULL &&
222 rle->type == SYS_RES_MEMORY &&
223 dinfo->mprefetchable & BARBIT(rle->rid)) {
224 flags = rman_make_alignment_flags(rle->count);
225 break;
228 if (flags > 0) { /* If any prefetchable memory is requested... */
230 * First we allocate one big space for all resources of this
231 * type. We do this because our parent, pccbb, needs to open
232 * a window to forward all addresses within the window, and
233 * it would be best if nobody else has resources allocated
234 * within the window.
235 * (XXX: Perhaps there might be a better way to do this?)
237 rid = 0;
238 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
239 (dinfo->mprefetchable & dinfo->mbelow1mb)?0xFFFFF:~0UL,
240 mem_psize, flags);
241 if (res == NULL) {
242 device_printf(cbdev,
243 "Can't get memory for prefetch mem\n");
244 kfree(barlist, M_DEVBUF);
245 return (EIO);
247 start = rman_get_start(res);
248 end = rman_get_end(res);
249 DEVPRINTF((cbdev, "Prefetchable memory at %x-%x\n", start, end));
251 * Now that we know the region is free, release it and hand it
252 * out piece by piece.
254 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
255 for (tmp = 0; tmp < count; tmp++) {
256 rle = barlist[tmp];
257 if (rle->type == SYS_RES_MEMORY &&
258 dinfo->mprefetchable & BARBIT(rle->rid)) {
259 cardbus_do_res(rle, child, start);
260 start += rle->count;
265 /* Allocate non-prefetchable memory */
266 flags = 0;
267 for (tmp = 0; tmp < count; tmp++) {
268 rle = barlist[tmp];
269 if (rle->type == SYS_RES_MEMORY &&
270 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
271 flags = rman_make_alignment_flags(rle->count);
272 break;
275 if (flags > 0) { /* If any non-prefetchable memory is requested... */
277 * First we allocate one big space for all resources of this
278 * type. We do this because our parent, pccbb, needs to open
279 * a window to forward all addresses within the window, and
280 * it would be best if nobody else has resources allocated
281 * within the window.
282 * (XXX: Perhaps there might be a better way to do this?)
284 rid = 0;
285 res = bus_alloc_resource(cbdev, SYS_RES_MEMORY, &rid, 0,
286 ((~dinfo->mprefetchable) & dinfo->mbelow1mb)?0xFFFFF:~0UL,
287 mem_nsize, flags);
288 if (res == NULL) {
289 device_printf(cbdev,
290 "Can't get memory for non-prefetch mem\n");
291 kfree(barlist, M_DEVBUF);
292 return (EIO);
294 start = rman_get_start(res);
295 end = rman_get_end(res);
296 DEVPRINTF((cbdev, "Non-prefetchable memory at %x-%x\n",
297 start, end));
299 * Now that we know the region is free, release it and hand it
300 * out piece by piece.
302 bus_release_resource(cbdev, SYS_RES_MEMORY, rid, res);
303 for (tmp = 0; tmp < count; tmp++) {
304 rle = barlist[tmp];
305 if (rle->type == SYS_RES_MEMORY &&
306 (dinfo->mprefetchable & BARBIT(rle->rid)) == 0) {
307 cardbus_do_res(rle, child, start);
308 start += rle->count;
313 /* Allocate IO ports */
314 flags = 0;
315 for (tmp = 0; tmp < count; tmp++) {
316 rle = barlist[tmp];
317 if (rle->type == SYS_RES_IOPORT) {
318 flags = rman_make_alignment_flags(rle->count);
319 break;
322 if (flags > 0) { /* If any IO port is requested... */
324 * First we allocate one big space for all resources of this
325 * type. We do this because our parent, pccbb, needs to open
326 * a window to forward all addresses within the window, and
327 * it would be best if nobody else has resources allocated
328 * within the window.
329 * (XXX: Perhaps there might be a better way to do this?)
331 rid = 0;
332 res = bus_alloc_resource(cbdev, SYS_RES_IOPORT, &rid, 0,
333 (dinfo->ibelow1mb)?0xFFFFF:~0UL, io_size, flags);
334 if (res == NULL) {
335 device_printf(cbdev,
336 "Can't get memory for IO ports\n");
337 kfree(barlist, M_DEVBUF);
338 return (EIO);
340 start = rman_get_start(res);
341 end = rman_get_end(res);
342 DEVPRINTF((cbdev, "IO port at %x-%x\n", start, end));
344 * Now that we know the region is free, release it and hand it
345 * out piece by piece.
347 bus_release_resource(cbdev, SYS_RES_IOPORT, rid, res);
348 for (tmp = 0; tmp < count; tmp++) {
349 rle = barlist[tmp];
350 if (rle->type == SYS_RES_IOPORT) {
351 cardbus_do_res(rle, child, start);
352 start += rle->count;
357 /* Allocate IRQ */
358 rid = 0;
359 res = bus_alloc_resource_any(cbdev, SYS_RES_IRQ, &rid, RF_SHAREABLE);
360 if (res == NULL) {
361 device_printf(cbdev, "Can't get memory for irq\n");
362 kfree(barlist, M_DEVBUF);
363 return (EIO);
365 start = rman_get_start(res);
366 end = rman_get_end(res);
367 bus_release_resource(cbdev, SYS_RES_IRQ, rid, res);
368 resource_list_add(&dinfo->pci.resources, SYS_RES_IRQ, rid, start, end,
370 dinfo->pci.cfg.intline = rman_get_start(res);
371 pci_write_config(child, PCIR_INTLINE, rman_get_start(res), 1);
373 kfree(barlist, M_DEVBUF);
374 return (0);
377 /************************************************************************/
378 /* Probe/Attach */
379 /************************************************************************/
381 static int
382 cardbus_probe(device_t cbdev)
384 device_set_desc(cbdev, "CardBus bus");
385 return 0;
388 static int
389 cardbus_attach(device_t cbdev)
391 return 0;
394 static int
395 cardbus_detach(device_t cbdev)
397 cardbus_detach_card(cbdev);
398 return 0;
401 static int
402 cardbus_suspend(device_t self)
404 cardbus_detach_card(self);
405 return (0);
408 static int
409 cardbus_resume(device_t self)
411 return (0);
414 /************************************************************************/
415 /* Attach/Detach card */
416 /************************************************************************/
418 static void
419 cardbus_device_setup_regs(device_t brdev, int b, int s, int f, pcicfgregs *cfg)
421 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_INTLINE,
422 pci_get_irq(device_get_parent(brdev)), 1);
423 cfg->intline = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_INTLINE, 1);
425 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1);
426 cfg->cachelnsz = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 1);
428 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 0xa8, 1);
429 cfg->lattimer = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 1);
431 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MINGNT, 0x14, 1);
432 cfg->mingnt = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MINGNT, 1);
434 PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
435 cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
438 static int
439 cardbus_attach_card(device_t cbdev)
441 device_t brdev = device_get_parent(cbdev);
442 device_t child;
443 int cardattached = 0;
444 int bus, slot, func;
446 cardbus_detach_card(cbdev); /* detach existing cards */
447 POWER_ENABLE_SOCKET(brdev, cbdev);
448 bus = pcib_get_bus(brdev);
449 /* For each function, set it up and try to attach a driver to it */
450 for (slot = 0; slot <= CARDBUS_SLOTMAX; slot++) {
451 int cardbusfunchigh = 0;
452 for (func = 0; func <= cardbusfunchigh; func++) {
453 struct cardbus_devinfo *dinfo;
455 dinfo = (struct cardbus_devinfo *)
456 pci_read_device(brdev, bus, slot, func,
457 sizeof(struct cardbus_devinfo));
458 if (dinfo == NULL)
459 continue;
460 if (dinfo->pci.cfg.mfdev)
461 cardbusfunchigh = CARDBUS_FUNCMAX;
463 cardbus_device_setup_regs(brdev, bus, slot, func,
464 &dinfo->pci.cfg);
465 child = device_add_child(cbdev, NULL, -1);
466 if (child == NULL) {
467 DEVPRINTF((cbdev, "Cannot add child!\n"));
468 pci_freecfg((struct pci_devinfo *)dinfo);
469 continue;
471 dinfo->pci.cfg.dev = child;
472 resource_list_init(&dinfo->pci.resources);
473 device_set_ivars(child, dinfo);
474 if (cardbus_do_cis(cbdev, child) != 0) {
475 DEVPRINTF((cbdev, "Can't parse cis\n"));
476 pci_freecfg((struct pci_devinfo *)dinfo);
477 continue;
479 cardbus_pickup_maps(cbdev, child);
480 cardbus_alloc_resources(cbdev, child);
481 pci_print_verbose(&dinfo->pci);
482 if (device_probe_and_attach(child) != 0)
483 cardbus_release_all_resources(cbdev, dinfo);
484 else
485 cardattached++;
489 if (cardattached > 0)
490 return (0);
491 POWER_DISABLE_SOCKET(brdev, cbdev);
492 return (ENOENT);
495 static int
496 cardbus_detach_card(device_t cbdev)
498 int numdevs;
499 device_t *devlist;
500 int tmp;
501 int err = 0;
503 device_get_children(cbdev, &devlist, &numdevs);
505 if (numdevs == 0) {
506 kfree(devlist, M_TEMP);
507 return (ENOENT);
510 for (tmp = 0; tmp < numdevs; tmp++) {
511 struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]);
512 int status = device_get_state(devlist[tmp]);
514 if (dinfo->pci.cfg.dev != devlist[tmp])
515 device_printf(cbdev, "devinfo dev mismatch\n");
516 if (status == DS_ATTACHED || status == DS_BUSY)
517 device_detach(devlist[tmp]);
518 cardbus_release_all_resources(cbdev, dinfo);
519 device_delete_child(cbdev, devlist[tmp]);
520 pci_freecfg((struct pci_devinfo *)dinfo);
522 POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
523 kfree(devlist, M_TEMP);
524 return (err);
527 static void
528 cardbus_driver_added(device_t cbdev, driver_t *driver)
530 int numdevs;
531 device_t *devlist;
532 device_t dev;
533 int i;
534 struct cardbus_devinfo *dinfo;
536 DEVICE_IDENTIFY(driver, cbdev);
537 device_get_children(cbdev, &devlist, &numdevs);
539 * If there are no drivers attached, but there are children,
540 * then power the card up.
542 for (i = 0; i < numdevs; i++) {
543 dev = devlist[i];
544 if (device_get_state(dev) != DS_NOTPRESENT)
545 break;
547 if (i > 0 && i == numdevs)
548 POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
549 for (i = 0; i < numdevs; i++) {
550 dev = devlist[i];
551 if (device_get_state(dev) != DS_NOTPRESENT)
552 continue;
553 dinfo = device_get_ivars(dev);
554 pci_print_verbose(&dinfo->pci);
555 resource_list_init(&dinfo->pci.resources);
556 cardbus_do_cis(cbdev, dev);
557 cardbus_pickup_maps(cbdev, dev);
558 cardbus_alloc_resources(cbdev, dev);
559 if (device_probe_and_attach(dev) != 0)
560 cardbus_release_all_resources(cbdev, dinfo);
562 kfree(devlist, M_TEMP);
565 static void
566 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo)
568 struct resource_list_entry *rle;
570 /* Free all allocated resources */
571 SLIST_FOREACH(rle, &dinfo->pci.resources, link) {
572 if (rle->res) {
573 if (rman_get_device(rle->res) != cbdev)
574 device_printf(cbdev, "release_all_resource: "
575 "Resource still owned by child, oops. "
576 "(type=%d, rid=%d, addr=%lx)\n",
577 rle->type, rle->rid,
578 rman_get_start(rle->res));
579 BUS_RELEASE_RESOURCE(device_get_parent(cbdev),
580 cbdev, rle->type, rle->rid, rle->res);
581 rle->res = NULL;
583 * zero out config so the card won't acknowledge
584 * access to the space anymore
586 pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4);
589 resource_list_free(&dinfo->pci.resources);
592 /************************************************************************/
593 /* Other Bus Methods */
594 /************************************************************************/
596 static int
597 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
599 struct cardbus_devinfo *dinfo;
600 pcicfgregs *cfg;
602 dinfo = device_get_ivars(child);
603 cfg = &dinfo->pci.cfg;
605 switch (which) {
606 case PCI_IVAR_ETHADDR:
608 * The generic accessor doesn't deal with failure, so
609 * we set the return value, then return an error.
611 if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
612 *((uint8_t **) result) = dinfo->funce.lan.nid;
613 break;
615 *((uint8_t **) result) = NULL;
616 return (EINVAL);
617 default:
618 return (pci_read_ivar(cbdev, child, which, result));
620 return 0;
623 static int
624 cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)
626 return(pci_write_ivar(cbdev, child, which, value));
629 static device_method_t cardbus_methods[] = {
630 /* Device interface */
631 DEVMETHOD(device_probe, cardbus_probe),
632 DEVMETHOD(device_attach, cardbus_attach),
633 DEVMETHOD(device_detach, cardbus_detach),
634 DEVMETHOD(device_suspend, cardbus_suspend),
635 DEVMETHOD(device_resume, cardbus_resume),
637 /* Bus interface */
638 DEVMETHOD(bus_read_ivar, cardbus_read_ivar),
639 DEVMETHOD(bus_write_ivar, cardbus_write_ivar),
640 DEVMETHOD(bus_driver_added, cardbus_driver_added),
642 /* Card Interface */
643 DEVMETHOD(card_attach_card, cardbus_attach_card),
644 DEVMETHOD(card_detach_card, cardbus_detach_card),
646 {0,0}
649 DECLARE_CLASS(pci_driver);
650 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods, 0, pci_driver);
652 static devclass_t cardbus_devclass;
654 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
655 MODULE_VERSION(cardbus, 1);