1 /* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */
4 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Marc Horowitz.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/kernel.h>
40 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 #include <sys/types.h>
45 #include <machine/bus.h>
47 #include <machine/resource.h>
49 #include <net/ethernet.h>
51 #include <dev/pccard/pccardreg.h>
52 #include <dev/pccard/pccardvar.h>
53 #include <dev/pccard/pccardvarp.h>
54 #include <dev/pccard/pccard_cis.h>
62 SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
65 TUNABLE_INT("hw.pccard.debug", &pccard_debug);
66 SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
70 int pccard_cis_debug = 0;
71 TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
72 SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
73 &pccard_cis_debug, 0, "pccard CIS debug");
76 #define DPRINTF(arg) if (pccard_debug) printf arg
77 #define DEVPRINTF(arg) if (pccard_debug) device_printf arg
78 #define PRVERBOSE(arg) printf arg
79 #define DEVPRVERBOSE(arg) device_printf arg
82 #define DEVPRINTF(arg)
83 #define PRVERBOSE(arg) if (bootverbose) printf arg
84 #define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
87 static int pccard_ccr_read(struct pccard_function *pf, int ccr);
88 static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
89 static int pccard_attach_card(device_t dev);
90 static int pccard_detach_card(device_t dev);
91 static void pccard_function_init(struct pccard_function *pf);
92 static void pccard_function_free(struct pccard_function *pf);
93 static int pccard_function_enable(struct pccard_function *pf);
94 static void pccard_function_disable(struct pccard_function *pf);
95 static int pccard_probe(device_t dev);
96 static int pccard_attach(device_t dev);
97 static int pccard_detach(device_t dev);
98 static void pccard_print_resources(struct resource_list *rl,
99 const char *name, int type, int count, const char *format);
100 static int pccard_print_child(device_t dev, device_t child);
101 static int pccard_set_resource(device_t dev, device_t child, int type,
102 int rid, u_long start, u_long count);
103 static int pccard_get_resource(device_t dev, device_t child, int type,
104 int rid, u_long *startp, u_long *countp);
105 static void pccard_delete_resource(device_t dev, device_t child, int type,
107 static int pccard_set_res_flags(device_t dev, device_t child, int type,
108 int rid, uint32_t flags);
109 static int pccard_set_memory_offset(device_t dev, device_t child, int rid,
110 uint32_t offset, uint32_t *deltap);
111 static void pccard_probe_nomatch(device_t cbdev, device_t child);
112 static int pccard_read_ivar(device_t bus, device_t child, int which,
114 static void pccard_driver_added(device_t dev, driver_t *driver);
115 static struct resource *pccard_alloc_resource(device_t dev,
116 device_t child, int type, int *rid, u_long start,
117 u_long end, u_long count, u_int flags);
118 static int pccard_release_resource(device_t dev, device_t child, int type,
119 int rid, struct resource *r);
120 static void pccard_child_detached(device_t parent, device_t dev);
121 static int pccard_intr(void *arg);
122 static int pccard_setup_intr(device_t dev, device_t child,
123 struct resource *irq, int flags, driver_filter_t *filt,
124 driver_intr_t *intr, void *arg, void **cookiep);
125 static int pccard_teardown_intr(device_t dev, device_t child,
126 struct resource *r, void *cookie);
128 static const struct pccard_product *
129 pccard_do_product_lookup(device_t bus, device_t dev,
130 const struct pccard_product *tab, size_t ent_size,
131 pccard_product_match_fn matchfn);
135 pccard_ccr_read(struct pccard_function *pf, int ccr)
137 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
138 pf->pf_ccr_offset + ccr));
142 pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
144 if ((pf->ccr_mask) & (1 << (ccr / 2))) {
145 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
146 pf->pf_ccr_offset + ccr, val);
151 pccard_set_default_descr(device_t dev)
153 const char *vendorstr, *prodstr;
154 uint32_t vendor, prod;
157 if (pccard_get_vendor_str(dev, &vendorstr))
159 if (pccard_get_product_str(dev, &prodstr))
161 if (vendorstr != NULL && prodstr != NULL) {
162 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
164 sprintf(str, "%s %s", vendorstr, prodstr);
165 device_set_desc_copy(dev, str);
168 if (pccard_get_vendor(dev, &vendor))
170 if (pccard_get_product(dev, &prod))
172 str = malloc(100, M_DEVBUF, M_WAITOK);
173 snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod);
174 device_set_desc_copy(dev, str);
181 pccard_attach_card(device_t dev)
183 struct pccard_softc *sc = PCCARD_SOFTC(dev);
184 struct pccard_function *pf;
185 struct pccard_ivar *ivar;
189 if (!STAILQ_EMPTY(&sc->card.pf_head)) {
190 if (bootverbose || pccard_debug)
191 device_printf(dev, "Card already inserted.\n");
194 DEVPRINTF((dev, "chip_socket_enable\n"));
195 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
197 DEVPRINTF((dev, "read_cis\n"));
200 DEVPRINTF((dev, "check_cis_quirks\n"));
201 pccard_check_cis_quirks(dev);
204 * bail now if the card has no functions, or if there was an error in
208 if (sc->card.error) {
209 device_printf(dev, "CARD ERROR!\n");
212 if (STAILQ_EMPTY(&sc->card.pf_head)) {
213 device_printf(dev, "Card has no functions!\n");
217 if (bootverbose || pccard_debug)
218 pccard_print_cis(dev);
220 DEVPRINTF((dev, "functions scanning\n"));
222 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
224 if (STAILQ_EMPTY(&pf->cfe_head)) {
226 "Function %d has no config entries.!\n", i);
233 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
236 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
237 if (STAILQ_EMPTY(&pf->cfe_head))
240 * In NetBSD, the drivers are responsible for activating
241 * each function of a card. I think that in FreeBSD we
242 * want to activate them enough for the usual bus_*_resource
243 * routines will do the right thing. This many mean a
244 * departure from the current NetBSD model.
246 * This seems to work well in practice for most cards.
247 * However, there are two cases that are problematic.
248 * If a driver wishes to pick and chose which config
249 * entry to use, then this method falls down. These
250 * are usually older cards. In addition, there are
251 * some cards that have multiple hardware units on the
252 * cards, but presents only one CIS chain. These cards
253 * are combination cards, but only one of these units
254 * can be on at a time.
256 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
258 resource_list_init(&ivar->resources);
259 child = device_add_child(dev, NULL, -1);
260 device_set_ivars(child, ivar);
264 * XXX We might want to move the next three lines into
265 * XXX the pccard interface layer. For the moment, this
266 * XXX is OK, but some drivers want to pick the config
267 * XXX entry to use as well as some address tweaks (mostly
268 * XXX due to bugs in decode logic that makes some
269 * XXX addresses illegal or broken).
271 pccard_function_init(pf);
272 if (sc->sc_enabled_count == 0)
273 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
274 if (pccard_function_enable(pf) == 0 &&
275 pccard_set_default_descr(child) == 0 &&
276 device_probe_and_attach(child) == 0) {
277 DEVPRINTF((sc->dev, "function %d CCR at %d "
278 "offset %x mask %x: "
279 "%x %x %x %x, %x %x %x %x, %x\n",
280 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
281 pf->ccr_mask, pccard_ccr_read(pf, 0x00),
282 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
283 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
284 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
285 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
288 pccard_function_disable(pf);
295 pccard_detach_card(device_t dev)
297 struct pccard_softc *sc = PCCARD_SOFTC(dev);
298 struct pccard_function *pf;
299 struct pccard_config_entry *cfe;
300 struct pccard_ivar *devi;
304 * We are running on either the PCCARD socket's event thread
305 * or in user context detaching a device by user request.
307 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
310 state = device_get_state(pf->dev);
311 if (state == DS_ATTACHED || state == DS_BUSY)
312 device_detach(pf->dev);
314 pccard_function_disable(pf);
315 pccard_function_free(pf);
316 devi = PCCARD_IVAR(pf->dev);
317 device_delete_child(dev, pf->dev);
318 free(devi, M_DEVBUF);
320 if (sc->sc_enabled_count == 0)
321 POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
323 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
324 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
325 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
328 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
331 STAILQ_INIT(&sc->card.pf_head);
335 static const struct pccard_product *
336 pccard_do_product_lookup(device_t bus, device_t dev,
337 const struct pccard_product *tab, size_t ent_size,
338 pccard_product_match_fn matchfn)
340 const struct pccard_product *ent;
344 const char *vendorstr;
350 if (sizeof *ent > ent_size)
351 panic("pccard_product_lookup: bogus ent_size %jd",
352 (intmax_t) ent_size);
354 if (pccard_get_vendor(dev, &vendor))
356 if (pccard_get_product(dev, &prod))
358 if (pccard_get_vendor_str(dev, &vendorstr))
360 if (pccard_get_product_str(dev, &prodstr))
362 if (pccard_get_cis3_str(dev, &cis3str))
364 if (pccard_get_cis4_str(dev, &cis4str))
366 for (ent = tab; ent->pp_vendor != 0; ent =
367 (const struct pccard_product *) ((const char *) ent + ent_size)) {
369 if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
370 ent->pp_product == PCCARD_PRODUCT_ANY &&
371 ent->pp_cis[0] == NULL &&
372 ent->pp_cis[1] == NULL) {
375 "Total wildcard entry ignored for %s\n",
379 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
380 vendor != ent->pp_vendor)
382 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
383 prod != ent->pp_product)
385 if (matches && ent->pp_cis[0] &&
386 (vendorstr == NULL ||
387 strcmp(ent->pp_cis[0], vendorstr) != 0))
389 if (matches && ent->pp_cis[1] &&
391 strcmp(ent->pp_cis[1], prodstr) != 0))
393 if (matches && ent->pp_cis[2] &&
395 strcmp(ent->pp_cis[2], cis3str) != 0))
397 if (matches && ent->pp_cis[3] &&
399 strcmp(ent->pp_cis[3], cis4str) != 0))
402 matches = (*matchfn)(dev, ent, matches);
410 * Initialize a PCCARD function. May be called as long as the function is
413 * Note: pccard_function_init should not keep resources allocated. It should
414 * only set them up ala isa pnp, set the values in the rl lists, and return.
415 * Any resource held after pccard_function_init is called is a bug. However,
416 * the bus routines to get the resources also assume that pccard_function_init
417 * does this, so they need to be fixed too.
420 pccard_function_init(struct pccard_function *pf)
422 struct pccard_config_entry *cfe;
423 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
424 struct resource_list *rl = &devi->resources;
425 struct resource_list_entry *rle;
426 struct resource *r = 0;
428 u_long start, end, len;
431 if (pf->pf_flags & PFF_ENABLED) {
432 printf("pccard_function_init: function is enabled");
435 bus = device_get_parent(pf->dev);
436 /* Remember which configuration entry we are using. */
437 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
438 if (cfe->iftype != PCCARD_IFTYPE_IO)
441 for (i = 0; i < cfe->num_iospace; i++) {
442 start = cfe->iospace[i].start;
444 end = start + cfe->iospace[i].length - 1;
447 DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n",
450 len = cfe->iospace[i].length;
451 r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
452 start, end, len, rman_make_alignment_flags(len));
455 rle = resource_list_add(rl, SYS_RES_IOPORT,
456 rid, rman_get_start(r), rman_get_end(r),
457 cfe->iospace[i].length);
459 panic("Cannot add resource rid %d IOPORT", rid);
463 for (i = 0; i < cfe->num_memspace; i++) {
464 start = cfe->memspace[i].hostaddr;
466 end = start + cfe->memspace[i].length - 1;
469 DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n",
472 len = cfe->memspace[i].length;
473 r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
474 start, end, len, rman_make_alignment_flags(len));
477 rle = resource_list_add(rl, SYS_RES_MEMORY,
478 rid, rman_get_start(r), rman_get_end(r),
479 cfe->memspace[i].length);
481 panic("Cannot add resource rid %d MEM", rid);
486 DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
491 r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
495 rle = resource_list_add(rl, SYS_RES_IRQ, rid,
496 rman_get_start(r), rman_get_end(r), 1);
498 panic("Cannot add resource rid %d IRQ", rid);
501 /* If we get to here, we've allocated all we need */
505 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
507 resource_list_purge(rl);
512 * Free resources allocated by pccard_function_init(), May be called as long
513 * as the function is disabled.
515 * NOTE: This function should be unnecessary. pccard_function_init should
516 * never keep resources initialized.
519 pccard_function_free(struct pccard_function *pf)
521 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
522 struct resource_list_entry *rle;
524 if (pf->pf_flags & PFF_ENABLED) {
525 printf("pccard_function_init: function is enabled");
529 STAILQ_FOREACH(rle, &devi->resources, link) {
531 if (rman_get_device(rle->res) != pf->sc->dev)
532 device_printf(pf->sc->dev,
533 "function_free: Resource still owned by "
535 "(type=%d, rid=%d, addr=%lx)\n",
537 rman_get_start(rle->res));
538 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
539 pf->sc->dev, rle->type, rle->rid, rle->res);
543 resource_list_free(&devi->resources);
547 pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
548 bus_addr_t offset, bus_size_t size)
550 bus_size_t iosize, tmp;
553 if (pf->pf_mfc_iomax == 0) {
554 pf->pf_mfc_iobase = addr + offset;
555 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
557 /* this makes the assumption that nothing overlaps */
558 if (pf->pf_mfc_iobase > addr + offset)
559 pf->pf_mfc_iobase = addr + offset;
560 if (pf->pf_mfc_iomax < addr + offset + size)
561 pf->pf_mfc_iomax = addr + offset + size;
565 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
566 /* round up to nearest (2^n)-1 */
567 for (iosize = 1; iosize < tmp; iosize <<= 1)
571 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
572 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
573 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
574 pf->pf_mfc_iobase & 0xff);
575 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
576 (pf->pf_mfc_iobase >> 8) & 0xff);
577 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
578 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
579 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
582 /* Enable a PCCARD function */
584 pccard_function_enable(struct pccard_function *pf)
586 struct pccard_function *tmp;
588 device_t dev = pf->sc->dev;
590 if (pf->cfe == NULL) {
591 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
596 * Increase the reference count on the socket, enabling power, if
599 pf->sc->sc_enabled_count++;
601 if (pf->pf_flags & PFF_ENABLED) {
603 * Don't do anything if we're already enabled.
609 * it's possible for different functions' CCRs to be in the same
610 * underlying page. Check for that.
612 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
613 if ((tmp->pf_flags & PFF_ENABLED) &&
614 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
615 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
616 (tmp->ccr_base - tmp->pf_ccr_offset +
617 tmp->pf_ccr_realsize))) {
618 pf->pf_ccrt = tmp->pf_ccrt;
619 pf->pf_ccrh = tmp->pf_ccrh;
620 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
623 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
624 * tmp->ccr_base) + pf->ccr_base;
626 /* pf->pf_ccr_offset =
627 (tmp->pf_ccr_offset + pf->ccr_base) -
629 pf->pf_ccr_window = tmp->pf_ccr_window;
635 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
636 &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
639 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
640 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
642 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
643 pf->ccr_rid, PCCARD_A_MEM_ATTR);
644 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
645 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
646 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
647 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
648 pf->pf_ccr_realsize = 1;
651 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
652 reg |= PCCARD_CCR_OPTION_LEVIREQ;
653 if (pccard_mfc(pf->sc)) {
654 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
655 PCCARD_CCR_OPTION_ADDR_DECODE);
656 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
658 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
661 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
662 reg |= PCCARD_CCR_STATUS_IOIS8;
663 if (pf->cfe->flags & PCCARD_CFE_AUDIO)
664 reg |= PCCARD_CCR_STATUS_AUDIO;
665 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
667 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
669 if (pccard_mfc(pf->sc))
670 pccard_mfc_adjust_iobase(pf, 0, 0, 0);
674 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
675 device_printf(tmp->sc->dev,
676 "function %d CCR at %d offset %x: "
677 "%x %x %x %x, %x %x %x %x, %x\n",
678 tmp->number, tmp->pf_ccr_window,
680 pccard_ccr_read(tmp, 0x00),
681 pccard_ccr_read(tmp, 0x02),
682 pccard_ccr_read(tmp, 0x04),
683 pccard_ccr_read(tmp, 0x06),
684 pccard_ccr_read(tmp, 0x0A),
685 pccard_ccr_read(tmp, 0x0C),
686 pccard_ccr_read(tmp, 0x0E),
687 pccard_ccr_read(tmp, 0x10),
688 pccard_ccr_read(tmp, 0x12));
692 pf->pf_flags |= PFF_ENABLED;
697 * Decrement the reference count, and power down the socket, if
700 pf->sc->sc_enabled_count--;
701 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
706 /* Disable PCCARD function. */
708 pccard_function_disable(struct pccard_function *pf)
710 struct pccard_function *tmp;
711 device_t dev = pf->sc->dev;
714 panic("pccard_function_disable: function not initialized");
716 if ((pf->pf_flags & PFF_ENABLED) == 0) {
718 * Don't do anything if we're already disabled.
723 if (pf->intr_handler != NULL) {
724 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
725 struct resource_list_entry *rle =
726 resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
728 panic("Can't disable an interrupt with no IRQ res\n");
729 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
730 pf->intr_handler_cookie);
734 * it's possible for different functions' CCRs to be in the same
735 * underlying page. Check for that. Note we mark us as disabled
736 * first to avoid matching ourself.
739 pf->pf_flags &= ~PFF_ENABLED;
740 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
741 if ((tmp->pf_flags & PFF_ENABLED) &&
742 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
743 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
744 (tmp->ccr_base - tmp->pf_ccr_offset +
745 tmp->pf_ccr_realsize)))
749 /* Not used by anyone else; unmap the CCR. */
751 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
757 * Decrement the reference count, and power down the socket, if
760 pf->sc->sc_enabled_count--;
763 #define PCCARD_NPORT 2
764 #define PCCARD_NMEM 5
765 #define PCCARD_NIRQ 1
766 #define PCCARD_NDRQ 0
769 pccard_probe(device_t dev)
771 device_set_desc(dev, "16-bit PCCard bus");
776 pccard_attach(device_t dev)
778 struct pccard_softc *sc = PCCARD_SOFTC(dev);
782 sc->sc_enabled_count = 0;
783 if ((err = pccard_device_create(sc)) != 0)
785 STAILQ_INIT(&sc->card.pf_head);
786 return (bus_generic_attach(dev));
790 pccard_detach(device_t dev)
792 pccard_detach_card(dev);
793 pccard_device_destroy(device_get_softc(dev));
798 pccard_suspend(device_t self)
800 pccard_detach_card(self);
806 pccard_resume(device_t self)
812 pccard_print_resources(struct resource_list *rl, const char *name, int type,
813 int count, const char *format)
815 struct resource_list_entry *rle;
820 for (i = 0; i < count; i++) {
821 rle = resource_list_find(rl, type, i);
824 printf(" %s ", name);
825 else if (printed > 0)
828 printf(format, rle->start);
829 if (rle->count > 1) {
831 printf(format, rle->start + rle->count - 1);
834 /* check the first few regardless */
841 pccard_print_child(device_t dev, device_t child)
843 struct pccard_ivar *devi = PCCARD_IVAR(child);
844 struct resource_list *rl = &devi->resources;
847 retval += bus_print_child_header(dev, child);
848 retval += printf(" at");
851 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
852 PCCARD_NPORT, "%#lx");
853 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
854 PCCARD_NMEM, "%#lx");
855 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
857 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
859 retval += printf(" function %d config %d", devi->pf->number,
860 devi->pf->cfe->number);
863 retval += bus_print_child_footer(dev, child);
869 pccard_set_resource(device_t dev, device_t child, int type, int rid,
870 u_long start, u_long count)
872 struct pccard_ivar *devi = PCCARD_IVAR(child);
873 struct resource_list *rl = &devi->resources;
875 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
876 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
880 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
882 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
884 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
886 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
889 resource_list_add(rl, type, rid, start, start + count - 1, count);
890 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
891 type, &rid, start, start + count - 1, count, 0))
898 pccard_get_resource(device_t dev, device_t child, int type, int rid,
899 u_long *startp, u_long *countp)
901 struct pccard_ivar *devi = PCCARD_IVAR(child);
902 struct resource_list *rl = &devi->resources;
903 struct resource_list_entry *rle;
905 rle = resource_list_find(rl, type, rid);
910 *startp = rle->start;
912 *countp = rle->count;
918 pccard_delete_resource(device_t dev, device_t child, int type, int rid)
920 struct pccard_ivar *devi = PCCARD_IVAR(child);
921 struct resource_list *rl = &devi->resources;
922 resource_list_delete(rl, type, rid);
926 pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
929 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
934 pccard_set_memory_offset(device_t dev, device_t child, int rid,
935 uint32_t offset, uint32_t *deltap)
938 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
943 pccard_probe_nomatch(device_t bus, device_t child)
945 struct pccard_ivar *devi = PCCARD_IVAR(child);
946 struct pccard_function *pf = devi->pf;
947 struct pccard_softc *sc = PCCARD_SOFTC(bus);
950 device_printf(bus, "<unknown card>");
951 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
952 "at function %d\n", sc->card.manufacturer, sc->card.product,
953 pf->function, pf->number);
954 device_printf(bus, " CIS info: ");
955 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
956 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
962 pccard_child_location_str(device_t bus, device_t child, char *buf,
965 struct pccard_ivar *devi = PCCARD_IVAR(child);
966 struct pccard_function *pf = devi->pf;
968 snprintf(buf, buflen, "function=%d", pf->number);
972 /* XXX Maybe this should be in subr_bus? */
974 pccard_safe_quote(char *dst, const char *src, size_t len)
976 char *walker = dst, *ep = dst + len - 1;
980 while (src != NULL && walker < ep)
993 pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
996 struct pccard_ivar *devi = PCCARD_IVAR(child);
997 struct pccard_function *pf = devi->pf;
998 struct pccard_softc *sc = PCCARD_SOFTC(bus);
999 char cis0[128], cis1[128];
1001 pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1002 pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1003 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1004 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1005 sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1010 pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
1012 struct pccard_ivar *devi = PCCARD_IVAR(child);
1013 struct pccard_function *pf = devi->pf;
1014 struct pccard_softc *sc = PCCARD_SOFTC(bus);
1017 panic("No pccard function pointer");
1021 case PCCARD_IVAR_ETHADDR:
1022 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1024 case PCCARD_IVAR_VENDOR:
1025 *(uint32_t *)result = sc->card.manufacturer;
1027 case PCCARD_IVAR_PRODUCT:
1028 *(uint32_t *)result = sc->card.product;
1030 case PCCARD_IVAR_PRODEXT:
1031 *(uint16_t *)result = sc->card.prodext;
1033 case PCCARD_IVAR_FUNCTION:
1034 *(uint32_t *)result = pf->function;
1036 case PCCARD_IVAR_FUNCTION_NUMBER:
1037 *(uint32_t *)result = pf->number;
1039 case PCCARD_IVAR_VENDOR_STR:
1040 *(const char **)result = sc->card.cis1_info[0];
1042 case PCCARD_IVAR_PRODUCT_STR:
1043 *(const char **)result = sc->card.cis1_info[1];
1045 case PCCARD_IVAR_CIS3_STR:
1046 *(const char **)result = sc->card.cis1_info[2];
1048 case PCCARD_IVAR_CIS4_STR:
1049 *(const char **)result = sc->card.cis1_info[3];
1056 pccard_driver_added(device_t dev, driver_t *driver)
1058 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1059 struct pccard_function *pf;
1062 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1063 if (STAILQ_EMPTY(&pf->cfe_head))
1066 if (device_get_state(child) != DS_NOTPRESENT)
1068 if (pccard_function_enable(pf) == 0 &&
1069 device_probe_and_attach(child) == 0) {
1070 DEVPRINTF((sc->dev, "function %d CCR at %d "
1071 "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1072 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1073 pccard_ccr_read(pf, 0x00),
1074 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1075 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1076 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1077 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1079 if (pf->cfe != NULL)
1080 pccard_function_disable(pf);
1086 static struct resource *
1087 pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1088 u_long start, u_long end, u_long count, u_int flags)
1090 struct pccard_ivar *dinfo;
1091 struct resource_list_entry *rle = 0;
1092 int passthrough = (device_get_parent(child) != dev);
1093 int isdefault = (start == 0 && end == ~0UL && count == 1);
1094 struct resource *r = NULL;
1096 /* XXX I'm no longer sure this is right */
1098 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1099 type, rid, start, end, count, flags));
1102 dinfo = device_get_ivars(child);
1103 rle = resource_list_find(&dinfo->resources, type, *rid);
1105 if (rle == NULL && isdefault)
1106 return (NULL); /* no resource of that type/rid */
1107 if (rle == NULL || rle->res == NULL) {
1108 /* XXX Need to adjust flags */
1109 r = bus_alloc_resource(dev, type, rid, start, end,
1113 resource_list_add(&dinfo->resources, type, *rid,
1114 rman_get_start(r), rman_get_end(r), count);
1115 rle = resource_list_find(&dinfo->resources, type, *rid);
1121 * If dev doesn't own the device, then we can't give this device
1124 if (rman_get_device(rle->res) != dev)
1126 rman_set_device(rle->res, child);
1127 if (flags & RF_ACTIVE)
1128 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1131 device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1136 pccard_release_resource(device_t dev, device_t child, int type, int rid,
1139 struct pccard_ivar *dinfo;
1140 int passthrough = (device_get_parent(child) != dev);
1141 struct resource_list_entry *rle = 0;
1144 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1147 dinfo = device_get_ivars(child);
1149 rle = resource_list_find(&dinfo->resources, type, rid);
1152 device_printf(dev, "Allocated resource not found, "
1154 type, rid, rman_get_start(r), rman_get_size(r));
1158 device_printf(dev, "Allocated resource not recorded\n");
1162 * Deactivate the resource (since it is being released), and
1163 * assign it to the bus.
1165 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1166 rman_set_device(rle->res, dev);
1171 pccard_child_detached(device_t parent, device_t dev)
1173 struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1174 struct pccard_function *pf = ivar->pf;
1176 pccard_function_disable(pf);
1180 pccard_intr(void *arg)
1182 struct pccard_function *pf = (struct pccard_function*) arg;
1187 * MFC cards know if they interrupted, so we have to ack the
1188 * interrupt and call the ISR. Non-MFC cards don't have these
1189 * bits, so they always get called. Many non-MFC cards have
1190 * this bit set always upon read, but some do not.
1192 * We always ack the interrupt, even if there's no ISR
1193 * for the card. This is done on the theory that acking
1194 * the interrupt will pacify the card enough to keep an
1195 * interrupt storm from happening. Of course this won't
1196 * help in the non-MFC case.
1198 * This has no impact for MPSAFEness of the client drivers.
1199 * We register this with whatever flags the intr_handler
1200 * was registered with. All these functions are MPSAFE.
1202 if (pccard_mfc(pf->sc)) {
1203 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1204 if (reg & PCCARD_CCR_STATUS_INTR)
1205 pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1206 reg & ~PCCARD_CCR_STATUS_INTR);
1211 if (pf->filt_handler != NULL)
1212 pf->filt_handler(pf->intr_handler_arg);
1214 pf->intr_handler(pf->intr_handler_arg);
1216 return (FILTER_HANDLED);
1220 pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1221 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
1224 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1225 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1226 struct pccard_function *pf = ivar->pf;
1229 if (pf->intr_handler != NULL)
1230 panic("Only one interrupt handler per function allowed");
1231 if (filt != NULL && intr != NULL)
1234 err = bus_generic_setup_intr(dev, child, irq, flags,
1235 pccard_intr, NULL, pf, cookiep);
1237 err = bus_generic_setup_intr(dev, child, irq, flags,
1238 NULL, (driver_intr_t *)pccard_intr, pf, cookiep);
1241 pf->filt_handler = filt;
1242 pf->intr_handler = intr;
1243 pf->intr_handler_arg = arg;
1244 pf->intr_handler_cookie = *cookiep;
1245 if (pccard_mfc(sc)) {
1246 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1247 pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1248 PCCARD_CCR_OPTION_IREQ_ENABLE);
1254 pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1257 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1258 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1259 struct pccard_function *pf = ivar->pf;
1262 if (pccard_mfc(sc)) {
1263 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1264 pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1265 ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1267 ret = bus_generic_teardown_intr(dev, child, r, cookie);
1269 pf->intr_handler = NULL;
1270 pf->intr_handler_arg = NULL;
1271 pf->intr_handler_cookie = NULL;
1278 pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1281 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1282 struct pccard_function *pf = ivar->pf;
1285 case SYS_RES_IOPORT:
1287 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1290 if (pccard_mfc(pf->sc))
1291 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1297 return (bus_generic_activate_resource(brdev, child, type, rid, r));
1301 pccard_deactivate_resource(device_t brdev, device_t child, int type,
1302 int rid, struct resource *r)
1304 /* XXX undo pccard_activate_resource? XXX */
1305 return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1309 pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset,
1312 struct pccard_ivar *devi = PCCARD_IVAR(child);
1313 struct pccard_function *pf = devi->pf;
1316 * Optimization. Most of the time, devices want to access
1317 * the same page of the attribute memory that the CCR is in.
1318 * We take advantage of this fact here.
1320 if (offset / PCCARD_MEM_PAGE_SIZE ==
1321 pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1322 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
1323 offset % PCCARD_MEM_PAGE_SIZE);
1325 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1327 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset);
1328 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1335 pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset,
1338 struct pccard_ivar *devi = PCCARD_IVAR(child);
1339 struct pccard_function *pf = devi->pf;
1342 * Optimization. Most of the time, devices want to access
1343 * the same page of the attribute memory that the CCR is in.
1344 * We take advantage of this fact here.
1346 if (offset / PCCARD_MEM_PAGE_SIZE ==
1347 pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1348 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
1349 offset % PCCARD_MEM_PAGE_SIZE, val);
1351 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1353 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val);
1354 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1362 pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset,
1365 struct pccard_ivar *devi = PCCARD_IVAR(child);
1367 *val = pccard_ccr_read(devi->pf, offset);
1368 device_printf(child, "ccr_read of %#x (%#x) is %#x\n", offset,
1369 devi->pf->pf_ccr_offset, *val);
1374 pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset,
1377 struct pccard_ivar *devi = PCCARD_IVAR(child);
1378 struct pccard_function *pf = devi->pf;
1381 * Can't use pccard_ccr_write since client drivers may access
1382 * registers not contained in the 'mask' if they are non-standard.
1384 device_printf(child, "ccr_write of %#x to %#x (%#x)\n", val, offset,
1385 devi->pf->pf_ccr_offset);
1386 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset,
1392 static device_method_t pccard_methods[] = {
1393 /* Device interface */
1394 DEVMETHOD(device_probe, pccard_probe),
1395 DEVMETHOD(device_attach, pccard_attach),
1396 DEVMETHOD(device_detach, pccard_detach),
1397 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1398 DEVMETHOD(device_suspend, pccard_suspend),
1399 DEVMETHOD(device_resume, pccard_resume),
1402 DEVMETHOD(bus_print_child, pccard_print_child),
1403 DEVMETHOD(bus_driver_added, pccard_driver_added),
1404 DEVMETHOD(bus_child_detached, pccard_child_detached),
1405 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource),
1406 DEVMETHOD(bus_release_resource, pccard_release_resource),
1407 DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1408 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1409 DEVMETHOD(bus_setup_intr, pccard_setup_intr),
1410 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr),
1411 DEVMETHOD(bus_set_resource, pccard_set_resource),
1412 DEVMETHOD(bus_get_resource, pccard_get_resource),
1413 DEVMETHOD(bus_delete_resource, pccard_delete_resource),
1414 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch),
1415 DEVMETHOD(bus_read_ivar, pccard_read_ivar),
1416 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1417 DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1419 /* Card Interface */
1420 DEVMETHOD(card_set_res_flags, pccard_set_res_flags),
1421 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1422 DEVMETHOD(card_attach_card, pccard_attach_card),
1423 DEVMETHOD(card_detach_card, pccard_detach_card),
1424 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1425 DEVMETHOD(card_cis_scan, pccard_scan_cis),
1426 DEVMETHOD(card_attr_read, pccard_attr_read_impl),
1427 DEVMETHOD(card_attr_write, pccard_attr_write_impl),
1428 DEVMETHOD(card_ccr_read, pccard_ccr_read_impl),
1429 DEVMETHOD(card_ccr_write, pccard_ccr_write_impl),
1434 static driver_t pccard_driver = {
1437 sizeof(struct pccard_softc)
1440 devclass_t pccard_devclass;
1442 /* Maybe we need to have a slot device? */
1443 DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1444 DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1445 MODULE_VERSION(pccard, 1);