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_filter(void *arg);
122 static void pccard_intr(void *arg);
123 static int pccard_setup_intr(device_t dev, device_t child,
124 struct resource *irq, int flags, driver_filter_t *filt,
125 driver_intr_t *intr, void *arg, void **cookiep);
126 static int pccard_teardown_intr(device_t dev, device_t child,
127 struct resource *r, void *cookie);
129 static const struct pccard_product *
130 pccard_do_product_lookup(device_t bus, device_t dev,
131 const struct pccard_product *tab, size_t ent_size,
132 pccard_product_match_fn matchfn);
136 pccard_ccr_read(struct pccard_function *pf, int ccr)
138 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
139 pf->pf_ccr_offset + ccr));
143 pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
145 if ((pf->ccr_mask) & (1 << (ccr / 2))) {
146 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
147 pf->pf_ccr_offset + ccr, val);
152 pccard_set_default_descr(device_t dev)
154 const char *vendorstr, *prodstr;
155 uint32_t vendor, prod;
158 if (pccard_get_vendor_str(dev, &vendorstr))
160 if (pccard_get_product_str(dev, &prodstr))
162 if (vendorstr != NULL && prodstr != NULL) {
163 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
165 sprintf(str, "%s %s", vendorstr, prodstr);
166 device_set_desc_copy(dev, str);
169 if (pccard_get_vendor(dev, &vendor))
171 if (pccard_get_product(dev, &prod))
173 str = malloc(100, M_DEVBUF, M_WAITOK);
174 snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod);
175 device_set_desc_copy(dev, str);
182 pccard_attach_card(device_t dev)
184 struct pccard_softc *sc = PCCARD_SOFTC(dev);
185 struct pccard_function *pf;
186 struct pccard_ivar *ivar;
190 if (!STAILQ_EMPTY(&sc->card.pf_head)) {
191 if (bootverbose || pccard_debug)
192 device_printf(dev, "Card already inserted.\n");
195 DEVPRINTF((dev, "chip_socket_enable\n"));
196 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
198 DEVPRINTF((dev, "read_cis\n"));
201 DEVPRINTF((dev, "check_cis_quirks\n"));
202 pccard_check_cis_quirks(dev);
205 * bail now if the card has no functions, or if there was an error in
209 if (sc->card.error) {
210 device_printf(dev, "CARD ERROR!\n");
213 if (STAILQ_EMPTY(&sc->card.pf_head)) {
214 device_printf(dev, "Card has no functions!\n");
218 if (bootverbose || pccard_debug)
219 pccard_print_cis(dev);
221 DEVPRINTF((dev, "functions scanning\n"));
223 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
225 if (STAILQ_EMPTY(&pf->cfe_head)) {
227 "Function %d has no config entries.!\n", i);
234 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
237 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
238 if (STAILQ_EMPTY(&pf->cfe_head))
241 * In NetBSD, the drivers are responsible for activating
242 * each function of a card. I think that in FreeBSD we
243 * want to activate them enough for the usual bus_*_resource
244 * routines will do the right thing. This many mean a
245 * departure from the current NetBSD model.
247 * This seems to work well in practice for most cards.
248 * However, there are two cases that are problematic.
249 * If a driver wishes to pick and chose which config
250 * entry to use, then this method falls down. These
251 * are usually older cards. In addition, there are
252 * some cards that have multiple hardware units on the
253 * cards, but presents only one CIS chain. These cards
254 * are combination cards, but only one of these units
255 * can be on at a time.
257 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
259 resource_list_init(&ivar->resources);
260 child = device_add_child(dev, NULL, -1);
261 device_set_ivars(child, ivar);
265 * XXX We might want to move the next three lines into
266 * XXX the pccard interface layer. For the moment, this
267 * XXX is OK, but some drivers want to pick the config
268 * XXX entry to use as well as some address tweaks (mostly
269 * XXX due to bugs in decode logic that makes some
270 * XXX addresses illegal or broken).
272 pccard_function_init(pf);
273 if (sc->sc_enabled_count == 0)
274 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
275 if (pccard_function_enable(pf) == 0 &&
276 pccard_set_default_descr(child) == 0 &&
277 device_probe_and_attach(child) == 0) {
278 DEVPRINTF((sc->dev, "function %d CCR at %d "
279 "offset %x mask %x: "
280 "%x %x %x %x, %x %x %x %x, %x\n",
281 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
282 pf->ccr_mask, pccard_ccr_read(pf, 0x00),
283 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
284 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
285 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
286 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
289 pccard_function_disable(pf);
296 pccard_detach_card(device_t dev)
298 struct pccard_softc *sc = PCCARD_SOFTC(dev);
299 struct pccard_function *pf;
300 struct pccard_config_entry *cfe;
301 struct pccard_ivar *devi;
305 * We are running on either the PCCARD socket's event thread
306 * or in user context detaching a device by user request.
308 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
311 state = device_get_state(pf->dev);
312 if (state == DS_ATTACHED || state == DS_BUSY)
313 device_detach(pf->dev);
315 pccard_function_disable(pf);
316 pccard_function_free(pf);
317 devi = PCCARD_IVAR(pf->dev);
318 device_delete_child(dev, pf->dev);
319 free(devi, M_DEVBUF);
321 if (sc->sc_enabled_count == 0)
322 POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
324 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
325 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
326 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
329 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
332 STAILQ_INIT(&sc->card.pf_head);
336 static const struct pccard_product *
337 pccard_do_product_lookup(device_t bus, device_t dev,
338 const struct pccard_product *tab, size_t ent_size,
339 pccard_product_match_fn matchfn)
341 const struct pccard_product *ent;
345 const char *vendorstr;
351 if (sizeof *ent > ent_size)
352 panic("pccard_product_lookup: bogus ent_size %jd",
353 (intmax_t) ent_size);
355 if (pccard_get_vendor(dev, &vendor))
357 if (pccard_get_product(dev, &prod))
359 if (pccard_get_vendor_str(dev, &vendorstr))
361 if (pccard_get_product_str(dev, &prodstr))
363 if (pccard_get_cis3_str(dev, &cis3str))
365 if (pccard_get_cis4_str(dev, &cis4str))
367 for (ent = tab; ent->pp_vendor != 0; ent =
368 (const struct pccard_product *) ((const char *) ent + ent_size)) {
370 if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
371 ent->pp_product == PCCARD_PRODUCT_ANY &&
372 ent->pp_cis[0] == NULL &&
373 ent->pp_cis[1] == NULL) {
376 "Total wildcard entry ignored for %s\n",
380 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
381 vendor != ent->pp_vendor)
383 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
384 prod != ent->pp_product)
386 if (matches && ent->pp_cis[0] &&
387 (vendorstr == NULL ||
388 strcmp(ent->pp_cis[0], vendorstr) != 0))
390 if (matches && ent->pp_cis[1] &&
392 strcmp(ent->pp_cis[1], prodstr) != 0))
394 if (matches && ent->pp_cis[2] &&
396 strcmp(ent->pp_cis[2], cis3str) != 0))
398 if (matches && ent->pp_cis[3] &&
400 strcmp(ent->pp_cis[3], cis4str) != 0))
403 matches = (*matchfn)(dev, ent, matches);
411 * Initialize a PCCARD function. May be called as long as the function is
414 * Note: pccard_function_init should not keep resources allocated. It should
415 * only set them up ala isa pnp, set the values in the rl lists, and return.
416 * Any resource held after pccard_function_init is called is a bug. However,
417 * the bus routines to get the resources also assume that pccard_function_init
418 * does this, so they need to be fixed too.
421 pccard_function_init(struct pccard_function *pf)
423 struct pccard_config_entry *cfe;
424 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
425 struct resource_list *rl = &devi->resources;
426 struct resource_list_entry *rle;
427 struct resource *r = 0;
429 u_long start, end, len;
432 if (pf->pf_flags & PFF_ENABLED) {
433 printf("pccard_function_init: function is enabled");
436 bus = device_get_parent(pf->dev);
437 /* Remember which configuration entry we are using. */
438 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
439 if (cfe->iftype != PCCARD_IFTYPE_IO)
442 for (i = 0; i < cfe->num_iospace; i++) {
443 start = cfe->iospace[i].start;
445 end = start + cfe->iospace[i].length - 1;
448 DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n",
451 len = cfe->iospace[i].length;
452 r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
453 start, end, len, rman_make_alignment_flags(len));
456 rle = resource_list_add(rl, SYS_RES_IOPORT,
457 rid, rman_get_start(r), rman_get_end(r),
458 cfe->iospace[i].length);
460 panic("Cannot add resource rid %d IOPORT", rid);
464 for (i = 0; i < cfe->num_memspace; i++) {
465 start = cfe->memspace[i].hostaddr;
467 end = start + cfe->memspace[i].length - 1;
470 DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n",
473 len = cfe->memspace[i].length;
474 r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
475 start, end, len, rman_make_alignment_flags(len));
478 rle = resource_list_add(rl, SYS_RES_MEMORY,
479 rid, rman_get_start(r), rman_get_end(r),
480 cfe->memspace[i].length);
482 panic("Cannot add resource rid %d MEM", rid);
487 DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
492 r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
496 rle = resource_list_add(rl, SYS_RES_IRQ, rid,
497 rman_get_start(r), rman_get_end(r), 1);
499 panic("Cannot add resource rid %d IRQ", rid);
502 /* If we get to here, we've allocated all we need */
506 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
508 resource_list_purge(rl);
513 * Free resources allocated by pccard_function_init(), May be called as long
514 * as the function is disabled.
516 * NOTE: This function should be unnecessary. pccard_function_init should
517 * never keep resources initialized.
520 pccard_function_free(struct pccard_function *pf)
522 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
523 struct resource_list_entry *rle;
525 if (pf->pf_flags & PFF_ENABLED) {
526 printf("pccard_function_init: function is enabled");
530 STAILQ_FOREACH(rle, &devi->resources, link) {
532 if (rman_get_device(rle->res) != pf->sc->dev)
533 device_printf(pf->sc->dev,
534 "function_free: Resource still owned by "
536 "(type=%d, rid=%d, addr=%lx)\n",
538 rman_get_start(rle->res));
539 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
540 pf->sc->dev, rle->type, rle->rid, rle->res);
544 resource_list_free(&devi->resources);
548 pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
549 bus_addr_t offset, bus_size_t size)
551 bus_size_t iosize, tmp;
554 if (pf->pf_mfc_iomax == 0) {
555 pf->pf_mfc_iobase = addr + offset;
556 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
558 /* this makes the assumption that nothing overlaps */
559 if (pf->pf_mfc_iobase > addr + offset)
560 pf->pf_mfc_iobase = addr + offset;
561 if (pf->pf_mfc_iomax < addr + offset + size)
562 pf->pf_mfc_iomax = addr + offset + size;
566 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
567 /* round up to nearest (2^n)-1 */
568 for (iosize = 1; iosize < tmp; iosize <<= 1)
572 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
573 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
574 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
575 pf->pf_mfc_iobase & 0xff);
576 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
577 (pf->pf_mfc_iobase >> 8) & 0xff);
578 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
579 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
580 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
583 /* Enable a PCCARD function */
585 pccard_function_enable(struct pccard_function *pf)
587 struct pccard_function *tmp;
589 device_t dev = pf->sc->dev;
591 if (pf->cfe == NULL) {
592 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
597 * Increase the reference count on the socket, enabling power, if
600 pf->sc->sc_enabled_count++;
602 if (pf->pf_flags & PFF_ENABLED) {
604 * Don't do anything if we're already enabled.
610 * it's possible for different functions' CCRs to be in the same
611 * underlying page. Check for that.
613 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
614 if ((tmp->pf_flags & PFF_ENABLED) &&
615 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
616 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
617 (tmp->ccr_base - tmp->pf_ccr_offset +
618 tmp->pf_ccr_realsize))) {
619 pf->pf_ccrt = tmp->pf_ccrt;
620 pf->pf_ccrh = tmp->pf_ccrh;
621 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
624 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
625 * tmp->ccr_base) + pf->ccr_base;
627 /* pf->pf_ccr_offset =
628 (tmp->pf_ccr_offset + pf->ccr_base) -
630 pf->pf_ccr_window = tmp->pf_ccr_window;
636 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
637 &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
640 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
641 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
643 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
644 pf->ccr_rid, PCCARD_A_MEM_ATTR);
645 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
646 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
647 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
648 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
649 pf->pf_ccr_realsize = 1;
652 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
653 reg |= PCCARD_CCR_OPTION_LEVIREQ;
654 if (pccard_mfc(pf->sc)) {
655 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
656 PCCARD_CCR_OPTION_ADDR_DECODE);
657 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
659 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
662 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
663 reg |= PCCARD_CCR_STATUS_IOIS8;
664 if (pf->cfe->flags & PCCARD_CFE_AUDIO)
665 reg |= PCCARD_CCR_STATUS_AUDIO;
666 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
668 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
670 if (pccard_mfc(pf->sc))
671 pccard_mfc_adjust_iobase(pf, 0, 0, 0);
675 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
676 device_printf(tmp->sc->dev,
677 "function %d CCR at %d offset %x: "
678 "%x %x %x %x, %x %x %x %x, %x\n",
679 tmp->number, tmp->pf_ccr_window,
681 pccard_ccr_read(tmp, 0x00),
682 pccard_ccr_read(tmp, 0x02),
683 pccard_ccr_read(tmp, 0x04),
684 pccard_ccr_read(tmp, 0x06),
685 pccard_ccr_read(tmp, 0x0A),
686 pccard_ccr_read(tmp, 0x0C),
687 pccard_ccr_read(tmp, 0x0E),
688 pccard_ccr_read(tmp, 0x10),
689 pccard_ccr_read(tmp, 0x12));
693 pf->pf_flags |= PFF_ENABLED;
698 * Decrement the reference count, and power down the socket, if
701 pf->sc->sc_enabled_count--;
702 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
707 /* Disable PCCARD function. */
709 pccard_function_disable(struct pccard_function *pf)
711 struct pccard_function *tmp;
712 device_t dev = pf->sc->dev;
715 panic("pccard_function_disable: function not initialized");
717 if ((pf->pf_flags & PFF_ENABLED) == 0) {
719 * Don't do anything if we're already disabled.
724 if (pf->intr_handler != NULL) {
725 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
726 struct resource_list_entry *rle =
727 resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
729 panic("Can't disable an interrupt with no IRQ res\n");
730 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
731 pf->intr_handler_cookie);
735 * it's possible for different functions' CCRs to be in the same
736 * underlying page. Check for that. Note we mark us as disabled
737 * first to avoid matching ourself.
740 pf->pf_flags &= ~PFF_ENABLED;
741 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
742 if ((tmp->pf_flags & PFF_ENABLED) &&
743 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
744 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
745 (tmp->ccr_base - tmp->pf_ccr_offset +
746 tmp->pf_ccr_realsize)))
750 /* Not used by anyone else; unmap the CCR. */
752 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
758 * Decrement the reference count, and power down the socket, if
761 pf->sc->sc_enabled_count--;
764 #define PCCARD_NPORT 2
765 #define PCCARD_NMEM 5
766 #define PCCARD_NIRQ 1
767 #define PCCARD_NDRQ 0
770 pccard_probe(device_t dev)
772 device_set_desc(dev, "16-bit PCCard bus");
777 pccard_attach(device_t dev)
779 struct pccard_softc *sc = PCCARD_SOFTC(dev);
783 sc->sc_enabled_count = 0;
784 if ((err = pccard_device_create(sc)) != 0)
786 STAILQ_INIT(&sc->card.pf_head);
787 return (bus_generic_attach(dev));
791 pccard_detach(device_t dev)
793 pccard_detach_card(dev);
794 pccard_device_destroy(device_get_softc(dev));
799 pccard_suspend(device_t self)
801 pccard_detach_card(self);
807 pccard_resume(device_t self)
813 pccard_print_resources(struct resource_list *rl, const char *name, int type,
814 int count, const char *format)
816 struct resource_list_entry *rle;
821 for (i = 0; i < count; i++) {
822 rle = resource_list_find(rl, type, i);
825 printf(" %s ", name);
826 else if (printed > 0)
829 printf(format, rle->start);
830 if (rle->count > 1) {
832 printf(format, rle->start + rle->count - 1);
835 /* check the first few regardless */
842 pccard_print_child(device_t dev, device_t child)
844 struct pccard_ivar *devi = PCCARD_IVAR(child);
845 struct resource_list *rl = &devi->resources;
848 retval += bus_print_child_header(dev, child);
849 retval += printf(" at");
852 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
853 PCCARD_NPORT, "%#lx");
854 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
855 PCCARD_NMEM, "%#lx");
856 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
858 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
860 retval += printf(" function %d config %d", devi->pf->number,
861 devi->pf->cfe->number);
864 retval += bus_print_child_footer(dev, child);
870 pccard_set_resource(device_t dev, device_t child, int type, int rid,
871 u_long start, u_long count)
873 struct pccard_ivar *devi = PCCARD_IVAR(child);
874 struct resource_list *rl = &devi->resources;
876 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
877 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
881 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
883 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
885 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
887 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
890 resource_list_add(rl, type, rid, start, start + count - 1, count);
891 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
892 type, &rid, start, start + count - 1, count, 0))
899 pccard_get_resource(device_t dev, device_t child, int type, int rid,
900 u_long *startp, u_long *countp)
902 struct pccard_ivar *devi = PCCARD_IVAR(child);
903 struct resource_list *rl = &devi->resources;
904 struct resource_list_entry *rle;
906 rle = resource_list_find(rl, type, rid);
911 *startp = rle->start;
913 *countp = rle->count;
919 pccard_delete_resource(device_t dev, device_t child, int type, int rid)
921 struct pccard_ivar *devi = PCCARD_IVAR(child);
922 struct resource_list *rl = &devi->resources;
923 resource_list_delete(rl, type, rid);
927 pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
930 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
935 pccard_set_memory_offset(device_t dev, device_t child, int rid,
936 uint32_t offset, uint32_t *deltap)
939 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
944 pccard_probe_nomatch(device_t bus, device_t child)
946 struct pccard_ivar *devi = PCCARD_IVAR(child);
947 struct pccard_function *pf = devi->pf;
948 struct pccard_softc *sc = PCCARD_SOFTC(bus);
951 device_printf(bus, "<unknown card>");
952 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
953 "at function %d\n", sc->card.manufacturer, sc->card.product,
954 pf->function, pf->number);
955 device_printf(bus, " CIS info: ");
956 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
957 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
963 pccard_child_location_str(device_t bus, device_t child, char *buf,
966 struct pccard_ivar *devi = PCCARD_IVAR(child);
967 struct pccard_function *pf = devi->pf;
969 snprintf(buf, buflen, "function=%d", pf->number);
973 /* XXX Maybe this should be in subr_bus? */
975 pccard_safe_quote(char *dst, const char *src, size_t len)
977 char *walker = dst, *ep = dst + len - 1;
981 while (src != NULL && walker < ep)
994 pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
997 struct pccard_ivar *devi = PCCARD_IVAR(child);
998 struct pccard_function *pf = devi->pf;
999 struct pccard_softc *sc = PCCARD_SOFTC(bus);
1000 char cis0[128], cis1[128];
1002 pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1003 pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1004 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1005 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1006 sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1011 pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
1013 struct pccard_ivar *devi = PCCARD_IVAR(child);
1014 struct pccard_function *pf = devi->pf;
1015 struct pccard_softc *sc = PCCARD_SOFTC(bus);
1018 panic("No pccard function pointer");
1022 case PCCARD_IVAR_ETHADDR:
1023 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1025 case PCCARD_IVAR_VENDOR:
1026 *(uint32_t *)result = sc->card.manufacturer;
1028 case PCCARD_IVAR_PRODUCT:
1029 *(uint32_t *)result = sc->card.product;
1031 case PCCARD_IVAR_PRODEXT:
1032 *(uint16_t *)result = sc->card.prodext;
1034 case PCCARD_IVAR_FUNCTION:
1035 *(uint32_t *)result = pf->function;
1037 case PCCARD_IVAR_FUNCTION_NUMBER:
1038 *(uint32_t *)result = pf->number;
1040 case PCCARD_IVAR_VENDOR_STR:
1041 *(const char **)result = sc->card.cis1_info[0];
1043 case PCCARD_IVAR_PRODUCT_STR:
1044 *(const char **)result = sc->card.cis1_info[1];
1046 case PCCARD_IVAR_CIS3_STR:
1047 *(const char **)result = sc->card.cis1_info[2];
1049 case PCCARD_IVAR_CIS4_STR:
1050 *(const char **)result = sc->card.cis1_info[3];
1057 pccard_driver_added(device_t dev, driver_t *driver)
1059 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1060 struct pccard_function *pf;
1063 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1064 if (STAILQ_EMPTY(&pf->cfe_head))
1067 if (device_get_state(child) != DS_NOTPRESENT)
1069 if (pccard_function_enable(pf) == 0 &&
1070 device_probe_and_attach(child) == 0) {
1071 DEVPRINTF((sc->dev, "function %d CCR at %d "
1072 "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1073 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1074 pccard_ccr_read(pf, 0x00),
1075 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1076 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1077 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1078 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1080 if (pf->cfe != NULL)
1081 pccard_function_disable(pf);
1087 static struct resource *
1088 pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1089 u_long start, u_long end, u_long count, u_int flags)
1091 struct pccard_ivar *dinfo;
1092 struct resource_list_entry *rle = 0;
1093 int passthrough = (device_get_parent(child) != dev);
1094 int isdefault = (start == 0 && end == ~0UL && count == 1);
1095 struct resource *r = NULL;
1097 /* XXX I'm no longer sure this is right */
1099 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1100 type, rid, start, end, count, flags));
1103 dinfo = device_get_ivars(child);
1104 rle = resource_list_find(&dinfo->resources, type, *rid);
1106 if (rle == NULL && isdefault)
1107 return (NULL); /* no resource of that type/rid */
1108 if (rle == NULL || rle->res == NULL) {
1109 /* XXX Need to adjust flags */
1110 r = bus_alloc_resource(dev, type, rid, start, end,
1114 resource_list_add(&dinfo->resources, type, *rid,
1115 rman_get_start(r), rman_get_end(r), count);
1116 rle = resource_list_find(&dinfo->resources, type, *rid);
1122 * If dev doesn't own the device, then we can't give this device
1125 if (rman_get_device(rle->res) != dev)
1127 rman_set_device(rle->res, child);
1128 if (flags & RF_ACTIVE)
1129 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1132 device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1137 pccard_release_resource(device_t dev, device_t child, int type, int rid,
1140 struct pccard_ivar *dinfo;
1141 int passthrough = (device_get_parent(child) != dev);
1142 struct resource_list_entry *rle = 0;
1145 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1148 dinfo = device_get_ivars(child);
1150 rle = resource_list_find(&dinfo->resources, type, rid);
1153 device_printf(dev, "Allocated resource not found, "
1155 type, rid, rman_get_start(r), rman_get_size(r));
1159 device_printf(dev, "Allocated resource not recorded\n");
1163 * Deactivate the resource (since it is being released), and
1164 * assign it to the bus.
1166 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1167 rman_set_device(rle->res, dev);
1172 pccard_child_detached(device_t parent, device_t dev)
1174 struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1175 struct pccard_function *pf = ivar->pf;
1177 pccard_function_disable(pf);
1181 pccard_filter(void *arg)
1183 struct pccard_function *pf = (struct pccard_function*) arg;
1188 * MFC cards know if they interrupted, so we have to ack the
1189 * interrupt and call the ISR. Non-MFC cards don't have these
1190 * bits, so they always get called. Many non-MFC cards have
1191 * this bit set always upon read, but some do not.
1193 * We always ack the interrupt, even if there's no ISR
1194 * for the card. This is done on the theory that acking
1195 * the interrupt will pacify the card enough to keep an
1196 * interrupt storm from happening. Of course this won't
1197 * help in the non-MFC case.
1199 * This has no impact for MPSAFEness of the client drivers.
1200 * We register this with whatever flags the intr_handler
1201 * was registered with. All these functions are MPSAFE.
1203 if (pccard_mfc(pf->sc)) {
1204 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1205 if (reg & PCCARD_CCR_STATUS_INTR)
1206 pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1207 reg & ~PCCARD_CCR_STATUS_INTR);
1212 if (pf->intr_filter != NULL)
1213 return (pf->intr_filter(pf->intr_handler_arg));
1214 return (FILTER_SCHEDULE_THREAD);
1216 return (FILTER_STRAY);
1220 pccard_intr(void *arg)
1222 struct pccard_function *pf = (struct pccard_function*) arg;
1224 pf->intr_handler(pf->intr_handler_arg);
1228 pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1229 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
1232 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1233 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1234 struct pccard_function *pf = ivar->pf;
1237 if (pf->intr_filter != NULL || pf->intr_handler != NULL)
1238 panic("Only one interrupt handler per function allowed");
1239 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_filter,
1240 intr ? pccard_intr : NULL, pf, cookiep);
1243 pf->intr_filter = filt;
1244 pf->intr_handler = intr;
1245 pf->intr_handler_arg = arg;
1246 pf->intr_handler_cookie = *cookiep;
1247 if (pccard_mfc(sc)) {
1248 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1249 pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1250 PCCARD_CCR_OPTION_IREQ_ENABLE);
1256 pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1259 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1260 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1261 struct pccard_function *pf = ivar->pf;
1264 if (pccard_mfc(sc)) {
1265 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1266 pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1267 ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1269 ret = bus_generic_teardown_intr(dev, child, r, cookie);
1271 pf->intr_handler = NULL;
1272 pf->intr_handler_arg = NULL;
1273 pf->intr_handler_cookie = NULL;
1280 pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1283 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1284 struct pccard_function *pf = ivar->pf;
1287 case SYS_RES_IOPORT:
1289 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1292 if (pccard_mfc(pf->sc))
1293 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1299 return (bus_generic_activate_resource(brdev, child, type, rid, r));
1303 pccard_deactivate_resource(device_t brdev, device_t child, int type,
1304 int rid, struct resource *r)
1306 /* XXX undo pccard_activate_resource? XXX */
1307 return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1311 pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset,
1314 struct pccard_ivar *devi = PCCARD_IVAR(child);
1315 struct pccard_function *pf = devi->pf;
1318 * Optimization. Most of the time, devices want to access
1319 * the same page of the attribute memory that the CCR is in.
1320 * We take advantage of this fact here.
1322 if (offset / PCCARD_MEM_PAGE_SIZE ==
1323 pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1324 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
1325 offset % PCCARD_MEM_PAGE_SIZE);
1327 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1329 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset);
1330 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1337 pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset,
1340 struct pccard_ivar *devi = PCCARD_IVAR(child);
1341 struct pccard_function *pf = devi->pf;
1344 * Optimization. Most of the time, devices want to access
1345 * the same page of the attribute memory that the CCR is in.
1346 * We take advantage of this fact here.
1348 if (offset / PCCARD_MEM_PAGE_SIZE ==
1349 pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1350 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
1351 offset % PCCARD_MEM_PAGE_SIZE, val);
1353 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1355 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val);
1356 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1364 pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset,
1367 struct pccard_ivar *devi = PCCARD_IVAR(child);
1369 *val = pccard_ccr_read(devi->pf, offset);
1370 device_printf(child, "ccr_read of %#x (%#x) is %#x\n", offset,
1371 devi->pf->pf_ccr_offset, *val);
1376 pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset,
1379 struct pccard_ivar *devi = PCCARD_IVAR(child);
1380 struct pccard_function *pf = devi->pf;
1383 * Can't use pccard_ccr_write since client drivers may access
1384 * registers not contained in the 'mask' if they are non-standard.
1386 device_printf(child, "ccr_write of %#x to %#x (%#x)\n", val, offset,
1387 devi->pf->pf_ccr_offset);
1388 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset,
1394 static device_method_t pccard_methods[] = {
1395 /* Device interface */
1396 DEVMETHOD(device_probe, pccard_probe),
1397 DEVMETHOD(device_attach, pccard_attach),
1398 DEVMETHOD(device_detach, pccard_detach),
1399 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1400 DEVMETHOD(device_suspend, pccard_suspend),
1401 DEVMETHOD(device_resume, pccard_resume),
1404 DEVMETHOD(bus_print_child, pccard_print_child),
1405 DEVMETHOD(bus_driver_added, pccard_driver_added),
1406 DEVMETHOD(bus_child_detached, pccard_child_detached),
1407 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource),
1408 DEVMETHOD(bus_release_resource, pccard_release_resource),
1409 DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1410 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1411 DEVMETHOD(bus_setup_intr, pccard_setup_intr),
1412 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr),
1413 DEVMETHOD(bus_set_resource, pccard_set_resource),
1414 DEVMETHOD(bus_get_resource, pccard_get_resource),
1415 DEVMETHOD(bus_delete_resource, pccard_delete_resource),
1416 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch),
1417 DEVMETHOD(bus_read_ivar, pccard_read_ivar),
1418 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1419 DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1421 /* Card Interface */
1422 DEVMETHOD(card_set_res_flags, pccard_set_res_flags),
1423 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1424 DEVMETHOD(card_attach_card, pccard_attach_card),
1425 DEVMETHOD(card_detach_card, pccard_detach_card),
1426 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1427 DEVMETHOD(card_cis_scan, pccard_scan_cis),
1428 DEVMETHOD(card_attr_read, pccard_attr_read_impl),
1429 DEVMETHOD(card_attr_write, pccard_attr_write_impl),
1430 DEVMETHOD(card_ccr_read, pccard_ccr_read_impl),
1431 DEVMETHOD(card_ccr_write, pccard_ccr_write_impl),
1436 static driver_t pccard_driver = {
1439 sizeof(struct pccard_softc)
1442 devclass_t pccard_devclass;
1444 /* Maybe we need to have a slot device? */
1445 DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1446 DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1447 MODULE_VERSION(pccard, 1);