1 /* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */
2 /* $FreeBSD: src/sys/dev/pccard/pccard.c,v 1.70 2002/11/14 14:02:32 mux Exp $ */
3 /* $DragonFly: src/sys/bus/pccard/pccard.c,v 1.9 2004/02/11 15:05:08 eirikn Exp $ */
6 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Marc Horowitz.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/kernel.h>
39 #include <sys/queue.h>
40 #include <sys/sysctl.h>
41 #include <sys/types.h>
44 #include <machine/bus.h>
46 #include <machine/resource.h>
48 #include <net/ethernet.h>
50 #include <bus/pccard/pccardreg.h>
51 #include <bus/pccard/pccardvar.h>
59 SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
62 TUNABLE_INT("hw.pccard.debug", &pccard_debug);
63 SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
67 int pccard_cis_debug = 0;
68 TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
69 SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
70 &pccard_cis_debug, 0, "pccard CIS debug");
73 #define DPRINTF(arg) if (pccard_debug) printf arg
74 #define DEVPRINTF(arg) if (pccard_debug) device_printf arg
75 #define PRVERBOSE(arg) printf arg
76 #define DEVPRVERBOSE(arg) device_printf arg
79 #define DEVPRINTF(arg)
80 #define PRVERBOSE(arg) if (bootverbose) printf arg
81 #define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
84 static int pccard_ccr_read(struct pccard_function *pf, int ccr);
85 static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
86 static int pccard_attach_card(device_t dev);
87 static int pccard_detach_card(device_t dev);
88 static int pccard_card_gettype(device_t dev, int *type);
89 static void pccard_function_init(struct pccard_function *pf);
90 static void pccard_function_free(struct pccard_function *pf);
91 static int pccard_function_enable(struct pccard_function *pf);
92 static void pccard_function_disable(struct pccard_function *pf);
93 static int pccard_compat_do_probe(device_t bus, device_t dev);
94 static int pccard_compat_do_attach(device_t bus, device_t dev);
95 static int pccard_add_children(device_t dev, int busno);
96 static int pccard_probe(device_t dev);
97 static int pccard_attach(device_t dev);
98 static int pccard_detach(device_t dev);
99 static void pccard_print_resources(struct resource_list *rl,
100 const char *name, int type, int count, const char *format);
101 static int pccard_print_child(device_t dev, device_t child);
102 static int pccard_set_resource(device_t dev, device_t child, int type,
103 int rid, u_long start, u_long count);
104 static int pccard_get_resource(device_t dev, device_t child, int type,
105 int rid, u_long *startp, u_long *countp);
106 static void pccard_delete_resource(device_t dev, device_t child, int type,
108 static int pccard_set_res_flags(device_t dev, device_t child, int type,
109 int rid, u_int32_t flags);
110 static int pccard_set_memory_offset(device_t dev, device_t child, int rid,
111 u_int32_t offset, u_int32_t *deltap);
112 static void pccard_probe_nomatch(device_t cbdev, device_t child);
113 static int pccard_read_ivar(device_t bus, device_t child, int which,
115 static void pccard_driver_added(device_t dev, driver_t *driver);
116 static struct resource *pccard_alloc_resource(device_t dev,
117 device_t child, int type, int *rid, u_long start,
118 u_long end, u_long count, u_int flags);
119 static int pccard_release_resource(device_t dev, device_t child, int type,
120 int rid, struct resource *r);
121 static void pccard_child_detached(device_t parent, device_t dev);
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_intr_t *intr,
125 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_attach_card(device_t dev)
154 struct pccard_softc *sc = PCCARD_SOFTC(dev);
155 struct pccard_function *pf;
156 struct pccard_ivar *ivar;
161 * this is here so that when socket_enable calls gettype, trt happens
163 STAILQ_INIT(&sc->card.pf_head);
165 DEVPRINTF((dev, "chip_socket_enable\n"));
166 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
168 DEVPRINTF((dev, "read_cis\n"));
171 DEVPRINTF((dev, "check_cis_quirks\n"));
172 pccard_check_cis_quirks(dev);
175 * bail now if the card has no functions, or if there was an error in
179 if (sc->card.error) {
180 device_printf(dev, "CARD ERROR!\n");
183 if (STAILQ_EMPTY(&sc->card.pf_head)) {
184 device_printf(dev, "Card has no functions!\n");
188 if (bootverbose || pccard_debug)
189 pccard_print_cis(dev);
191 DEVPRINTF((dev, "functions scanning\n"));
193 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
195 if (STAILQ_EMPTY(&pf->cfe_head)) {
197 "Function %d has no config entries.!\n", i);
204 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
207 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
208 if (STAILQ_EMPTY(&pf->cfe_head))
211 * In NetBSD, the drivers are responsible for activating
212 * each function of a card. I think that in FreeBSD we
213 * want to activate them enough for the usual bus_*_resource
214 * routines will do the right thing. This many mean a
215 * departure from the current NetBSD model.
217 * This seems to work well in practice for most cards.
218 * However, there are two cases that are problematic.
219 * If a driver wishes to pick and chose which config
220 * entry to use, then this method falls down. These
221 * are usually older cards. In addition, there are
222 * some cards that have multiple hardware units on the
223 * cards, but presents only one CIS chain. These cards
224 * are combination cards, but only one of these units
225 * can be on at a time.
227 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
229 child = device_add_child(dev, NULL, -1);
230 device_set_ivars(child, ivar);
234 * XXX We might want to move the next two lines into
235 * XXX the pccard interface layer. For the moment, this
236 * XXX is OK, but some drivers want to pick the config
237 * XXX entry to use as well as some address tweaks (mostly
238 * XXX due to bugs in decode logic that makes some
239 * XXX addresses illegal or broken).
241 pccard_function_init(pf);
242 if (sc->sc_enabled_count == 0)
243 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
244 if (pccard_function_enable(pf) == 0 &&
245 device_probe_and_attach(child) == 0) {
246 DEVPRINTF((sc->dev, "function %d CCR at %d "
247 "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
248 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
249 pccard_ccr_read(pf, 0x00),
250 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
251 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
252 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
253 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
256 pccard_function_disable(pf);
263 pccard_detach_card(device_t dev)
265 struct pccard_softc *sc = PCCARD_SOFTC(dev);
266 struct pccard_function *pf;
267 struct pccard_config_entry *cfe;
270 * We are running on either the PCCARD socket's event thread
271 * or in user context detaching a device by user request.
273 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
274 int state = device_get_state(pf->dev);
276 if (state == DS_ATTACHED || state == DS_BUSY)
277 device_detach(pf->dev);
279 pccard_function_disable(pf);
280 pccard_function_free(pf);
281 device_delete_child(dev, pf->dev);
283 if (sc->sc_enabled_count == 0)
284 POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
286 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
287 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
288 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
291 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
297 static const struct pccard_product *
298 pccard_do_product_lookup(device_t bus, device_t dev,
299 const struct pccard_product *tab, size_t ent_size,
300 pccard_product_match_fn matchfn)
302 const struct pccard_product *ent;
311 if (sizeof *ent > ent_size)
312 panic("pccard_product_lookup: bogus ent_size %ld",
315 if (pccard_get_vendor(dev, &vendor))
317 if (pccard_get_product(dev, &prod))
319 if (pccard_get_function_number(dev, &fcn))
321 if (pccard_get_vendor_str(dev, &vendorstr))
323 if (pccard_get_product_str(dev, &prodstr))
325 for (ent = tab; ent->pp_name != NULL; ent =
326 (const struct pccard_product *) ((const char *) ent + ent_size)) {
328 if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
329 ent->pp_product == PCCARD_VENDOR_ANY &&
330 ent->pp_cis[0] == NULL &&
331 ent->pp_cis[1] == NULL) {
333 "Total wildcard entry ignored for %s\n",
337 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
338 vendor != ent->pp_vendor)
340 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
341 prod != ent->pp_product)
343 if (matches && fcn != ent->pp_expfunc)
345 if (matches && ent->pp_cis[0] &&
346 strcmp(ent->pp_cis[0], vendorstr) != 0)
348 if (matches && ent->pp_cis[1] &&
349 strcmp(ent->pp_cis[1], prodstr) != 0)
351 /* XXX need to match cis[2] and cis[3] also XXX */
353 matches = (*matchfn)(dev, ent, matches);
361 pccard_card_gettype(device_t dev, int *type)
363 struct pccard_softc *sc = PCCARD_SOFTC(dev);
364 struct pccard_function *pf;
367 * set the iftype to memory if this card has no functions (not yet
368 * probed), or only one function, and that is not initialized yet or
371 pf = STAILQ_FIRST(&sc->card.pf_head);
373 (STAILQ_NEXT(pf, pf_list) == NULL &&
374 (pf->cfe == NULL || pf->cfe->iftype == PCCARD_IFTYPE_MEMORY)))
375 *type = PCCARD_IFTYPE_MEMORY;
377 *type = PCCARD_IFTYPE_IO;
382 * Initialize a PCCARD function. May be called as long as the function is
385 * Note: pccard_function_init should not keep resources allocated. It should
386 * only set them up ala isa pnp, set the values in the rl lists, and return.
387 * Any resource held after pccard_function_init is called is a bug. However,
388 * the bus routines to get the resources also assume that pccard_function_init
389 * does this, so they need to be fixed too.
392 pccard_function_init(struct pccard_function *pf)
394 struct pccard_config_entry *cfe;
396 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
397 struct resource_list *rl = &devi->resources;
398 struct resource_list_entry *rle;
399 struct resource *r = 0;
405 if (pf->pf_flags & PFF_ENABLED) {
406 printf("pccard_function_init: function is enabled");
409 bus = device_get_parent(pf->dev);
410 /* Remember which configuration entry we are using. */
411 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
412 for (i = 0; i < cfe->num_iospace; i++)
413 cfe->iores[i] = NULL;
416 for (i = 0; i < cfe->num_iospace; i++) {
417 start = cfe->iospace[i].start;
419 end = start + cfe->iospace[i].length - 1;
423 DEVPRINTF((bus, "I/O rid %d start %x end %x\n",
425 r = cfe->iores[i] = bus_alloc_resource(bus,
426 SYS_RES_IOPORT, &cfe->iorid[i], start, end,
427 cfe->iospace[i].length,
428 rman_make_alignment_flags(cfe->iospace[i].length));
429 if (cfe->iores[i] == NULL)
431 resource_list_add(rl, SYS_RES_IOPORT, cfe->iorid[i],
432 rman_get_start(r), rman_get_end(r),
433 cfe->iospace[i].length);
434 rle = resource_list_find(rl, SYS_RES_IOPORT,
439 if (cfe->num_memspace > 0) {
441 * Not implement yet, Fix me.
443 DEVPRINTF((bus, "Memory space not yet implemented.\n"));
446 DEVPRINTF((bus, "Neither memory nor I/O mampped\n"));
451 r = cfe->irqres = bus_alloc_resource(bus, SYS_RES_IRQ,
452 &cfe->irqrid, 0, ~0, 1, 0);
453 if (cfe->irqres == NULL)
455 resource_list_add(rl, SYS_RES_IRQ, cfe->irqrid,
456 rman_get_start(r), rman_get_end(r), 1);
457 rle = resource_list_find(rl, SYS_RES_IRQ,
461 /* If we get to here, we've allocated all we need */
465 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
468 * Release resources that we partially allocated
469 * from this config entry.
471 for (i = 0; i < cfe->num_iospace; i++) {
472 if (cfe->iores[i] != NULL) {
473 bus_release_resource(bus, SYS_RES_IOPORT,
474 cfe->iorid[i], cfe->iores[i]);
475 rle = resource_list_find(rl, SYS_RES_IOPORT,
478 resource_list_delete(rl, SYS_RES_IOPORT,
481 cfe->iores[i] = NULL;
483 if (cfe->irqmask && cfe->irqres != NULL) {
484 bus_release_resource(bus, SYS_RES_IRQ,
485 cfe->irqrid, cfe->irqres);
486 rle = resource_list_find(rl, SYS_RES_IRQ,
489 resource_list_delete(rl, SYS_RES_IRQ, cfe->irqrid);
496 * Free resources allocated by pccard_function_init(), May be called as long
497 * as the function is disabled.
499 * NOTE: This function should be unnecessary. pccard_function_init should
500 * never keep resources initialized.
503 pccard_function_free(struct pccard_function *pf)
505 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
506 struct resource_list_entry *rle;
508 if (pf->pf_flags & PFF_ENABLED) {
509 printf("pccard_function_init: function is enabled");
513 SLIST_FOREACH(rle, &devi->resources, link) {
515 if (rle->res->r_dev != pf->sc->dev)
516 device_printf(pf->sc->dev,
517 "function_free: Resource still owned by "
519 "(type=%d, rid=%d, addr=%lx)\n",
521 rman_get_start(rle->res));
522 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
523 rle->res->r_dev, rle->type, rle->rid, rle->res);
527 resource_list_free(&devi->resources);
530 /* Enable a PCCARD function */
532 pccard_function_enable(struct pccard_function *pf)
534 struct pccard_function *tmp;
536 device_t dev = pf->sc->dev;
538 if (pf->cfe == NULL) {
539 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
544 * Increase the reference count on the socket, enabling power, if
547 pf->sc->sc_enabled_count++;
549 if (pf->pf_flags & PFF_ENABLED) {
551 * Don't do anything if we're already enabled.
557 * it's possible for different functions' CCRs to be in the same
558 * underlying page. Check for that.
560 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
561 if ((tmp->pf_flags & PFF_ENABLED) &&
562 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
563 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
564 (tmp->ccr_base - tmp->pf_ccr_offset +
565 tmp->pf_ccr_realsize))) {
566 pf->pf_ccrt = tmp->pf_ccrt;
567 pf->pf_ccrh = tmp->pf_ccrh;
568 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
571 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
572 * tmp->ccr_base) + pf->ccr_base;
574 /* pf->pf_ccr_offset =
575 (tmp->pf_ccr_offset + pf->ccr_base) -
577 pf->pf_ccr_window = tmp->pf_ccr_window;
583 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
584 &pf->ccr_rid, 0, ~0, 1 << 10, RF_ACTIVE);
587 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
588 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
590 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
591 pf->ccr_rid, PCCARD_A_MEM_ATTR);
592 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
593 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
594 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
595 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
596 pf->pf_ccr_realsize = 1;
599 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
600 reg |= PCCARD_CCR_OPTION_LEVIREQ;
601 if (pccard_mfc(pf->sc)) {
602 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
603 PCCARD_CCR_OPTION_ADDR_DECODE);
604 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
606 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
609 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
610 reg |= PCCARD_CCR_STATUS_IOIS8;
611 if (pf->cfe->flags & PCCARD_CFE_AUDIO)
612 reg |= PCCARD_CCR_STATUS_AUDIO;
613 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
615 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
617 if (pccard_mfc(pf->sc)) {
620 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
621 /* round up to nearest (2^n)-1 */
622 for (iosize = 1; iosize < tmp; iosize <<= 1)
626 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
627 pf->pf_mfc_iobase & 0xff);
628 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
629 (pf->pf_mfc_iobase >> 8) & 0xff);
630 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
631 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
633 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
638 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
639 device_printf(tmp->sc->dev,
640 "function %d CCR at %d offset %x: "
641 "%x %x %x %x, %x %x %x %x, %x\n",
642 tmp->number, tmp->pf_ccr_window,
644 pccard_ccr_read(tmp, 0x00),
645 pccard_ccr_read(tmp, 0x02),
646 pccard_ccr_read(tmp, 0x04),
647 pccard_ccr_read(tmp, 0x06),
648 pccard_ccr_read(tmp, 0x0A),
649 pccard_ccr_read(tmp, 0x0C),
650 pccard_ccr_read(tmp, 0x0E),
651 pccard_ccr_read(tmp, 0x10),
652 pccard_ccr_read(tmp, 0x12));
656 pf->pf_flags |= PFF_ENABLED;
661 * Decrement the reference count, and power down the socket, if
664 pf->sc->sc_enabled_count--;
665 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
670 /* Disable PCCARD function. */
672 pccard_function_disable(struct pccard_function *pf)
674 struct pccard_function *tmp;
675 device_t dev = pf->sc->dev;
678 panic("pccard_function_disable: function not initialized");
680 if ((pf->pf_flags & PFF_ENABLED) == 0) {
682 * Don't do anything if we're already disabled.
687 if (pf->intr_handler != NULL) {
688 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
689 struct resource_list_entry *rle =
690 resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
691 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
692 pf->intr_handler_cookie);
696 * it's possible for different functions' CCRs to be in the same
697 * underlying page. Check for that. Note we mark us as disabled
698 * first to avoid matching ourself.
701 pf->pf_flags &= ~PFF_ENABLED;
702 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
703 if ((tmp->pf_flags & PFF_ENABLED) &&
704 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
705 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
706 (tmp->ccr_base - tmp->pf_ccr_offset +
707 tmp->pf_ccr_realsize)))
711 /* Not used by anyone else; unmap the CCR. */
713 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
719 * Decrement the reference count, and power down the socket, if
722 pf->sc->sc_enabled_count--;
726 * simulate the old "probe" routine. In the new world order, the driver
727 * needs to grab devices while in the old they were assigned to the device by
728 * the pccardd process. These symbols are exported to the upper layers.
731 pccard_compat_do_probe(device_t bus, device_t dev)
733 return (CARD_COMPAT_MATCH(dev));
737 pccard_compat_do_attach(device_t bus, device_t dev)
741 err = CARD_COMPAT_PROBE(dev);
743 err = CARD_COMPAT_ATTACH(dev);
747 #define PCCARD_NPORT 2
748 #define PCCARD_NMEM 5
749 #define PCCARD_NIRQ 1
750 #define PCCARD_NDRQ 0
753 pccard_add_children(device_t dev, int busno)
755 /* Call parent to scan for any current children */
760 pccard_probe(device_t dev)
762 device_set_desc(dev, "16-bit PCCard bus");
763 return (pccard_add_children(dev, device_get_unit(dev)));
767 pccard_attach(device_t dev)
769 struct pccard_softc *sc = PCCARD_SOFTC(dev);
772 sc->sc_enabled_count = 0;
773 return (bus_generic_attach(dev));
777 pccard_detach(device_t dev)
779 pccard_detach_card(dev);
784 pccard_suspend(device_t self)
786 pccard_detach_card(self);
791 pccard_resume(device_t self)
797 pccard_print_resources(struct resource_list *rl, const char *name, int type,
798 int count, const char *format)
800 struct resource_list_entry *rle;
805 for (i = 0; i < count; i++) {
806 rle = resource_list_find(rl, type, i);
809 printf(" %s ", name);
810 else if (printed > 0)
813 printf(format, rle->start);
814 if (rle->count > 1) {
816 printf(format, rle->start + rle->count - 1);
819 /* check the first few regardless */
826 pccard_print_child(device_t dev, device_t child)
828 struct pccard_ivar *devi = PCCARD_IVAR(child);
829 struct resource_list *rl = &devi->resources;
832 retval += bus_print_child_header(dev, child);
833 retval += printf(" at");
836 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
837 PCCARD_NPORT, "%#lx");
838 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
839 PCCARD_NMEM, "%#lx");
840 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
842 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
844 retval += printf(" function %d config %d", devi->fcn->number,
845 devi->fcn->cfe->number);
848 retval += bus_print_child_footer(dev, child);
854 pccard_set_resource(device_t dev, device_t child, int type, int rid,
855 u_long start, u_long count)
857 struct pccard_ivar *devi = PCCARD_IVAR(child);
858 struct resource_list *rl = &devi->resources;
860 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
861 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
865 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
867 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
869 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
871 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
874 resource_list_add(rl, type, rid, start, start + count - 1, count);
875 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
876 type, &rid, start, start + count - 1, count, 0))
883 pccard_get_resource(device_t dev, device_t child, int type, int rid,
884 u_long *startp, u_long *countp)
886 struct pccard_ivar *devi = PCCARD_IVAR(child);
887 struct resource_list *rl = &devi->resources;
888 struct resource_list_entry *rle;
890 rle = resource_list_find(rl, type, rid);
895 *startp = rle->start;
897 *countp = rle->count;
903 pccard_delete_resource(device_t dev, device_t child, int type, int rid)
905 struct pccard_ivar *devi = PCCARD_IVAR(child);
906 struct resource_list *rl = &devi->resources;
907 resource_list_delete(rl, type, rid);
911 pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
914 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
919 pccard_set_memory_offset(device_t dev, device_t child, int rid,
920 u_int32_t offset, u_int32_t *deltap)
923 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
928 pccard_probe_nomatch(device_t bus, device_t child)
930 struct pccard_ivar *devi = PCCARD_IVAR(child);
931 struct pccard_function *func = devi->fcn;
932 struct pccard_softc *sc = PCCARD_SOFTC(bus);
934 device_printf(bus, "<unknown card>");
935 printf(" (manufacturer=0x%04x, product=0x%04x) at function %d\n",
936 sc->card.manufacturer, sc->card.product, func->number);
937 device_printf(bus, " CIS info: %s, %s, %s\n", sc->card.cis1_info[0],
938 sc->card.cis1_info[1], sc->card.cis1_info[2]);
943 pccard_child_location_str(device_t bus, device_t child, char *buf,
946 struct pccard_ivar *devi = PCCARD_IVAR(child);
947 struct pccard_function *func = devi->fcn;
949 snprintf(buf, buflen, "function=%d", func->number);
954 pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
957 struct pccard_ivar *devi = PCCARD_IVAR(child);
958 struct pccard_function *func = devi->fcn;
959 struct pccard_softc *sc = PCCARD_SOFTC(bus);
961 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
962 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
963 sc->card.manufacturer, sc->card.product, sc->card.cis1_info[0],
964 sc->card.cis1_info[1], func->function);
969 pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
971 struct pccard_ivar *devi = PCCARD_IVAR(child);
972 struct pccard_function *func = devi->fcn;
973 struct pccard_softc *sc = PCCARD_SOFTC(bus);
977 case PCCARD_IVAR_ETHADDR:
978 bcopy(func->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
980 case PCCARD_IVAR_VENDOR:
981 *(u_int32_t *) result = sc->card.manufacturer;
983 case PCCARD_IVAR_PRODUCT:
984 *(u_int32_t *) result = sc->card.product;
986 case PCCARD_IVAR_PRODEXT:
987 *(u_int16_t *) result = sc->card.prodext;
989 case PCCARD_IVAR_FUNCTION:
990 *(u_int32_t *) result = func->function;
992 case PCCARD_IVAR_FUNCTION_NUMBER:
994 device_printf(bus, "No function number, bug!\n");
997 *(u_int32_t *) result = func->number;
999 case PCCARD_IVAR_VENDOR_STR:
1000 *(char **) result = sc->card.cis1_info[0];
1002 case PCCARD_IVAR_PRODUCT_STR:
1003 *(char **) result = sc->card.cis1_info[1];
1005 case PCCARD_IVAR_CIS3_STR:
1006 *(char **) result = sc->card.cis1_info[2];
1008 case PCCARD_IVAR_CIS4_STR:
1009 *(char **) result = sc->card.cis1_info[3];
1016 pccard_driver_added(device_t dev, driver_t *driver)
1018 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1019 struct pccard_function *pf;
1022 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1023 if (STAILQ_EMPTY(&pf->cfe_head))
1026 if (device_get_state(child) != DS_NOTPRESENT)
1028 if (pccard_function_enable(pf) == 0 &&
1029 device_probe_and_attach(child) == 0) {
1030 DEVPRINTF((sc->dev, "function %d CCR at %d "
1031 "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1032 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1033 pccard_ccr_read(pf, 0x00),
1034 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1035 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1036 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1037 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1039 if (pf->cfe != NULL)
1040 pccard_function_disable(pf);
1046 static struct resource *
1047 pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1048 u_long start, u_long end, u_long count, u_int flags)
1050 struct pccard_ivar *dinfo;
1051 struct resource_list_entry *rle = 0;
1052 int passthrough = (device_get_parent(child) != dev);
1053 struct resource *r = NULL;
1056 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1057 type, rid, start, end, count, flags));
1060 dinfo = device_get_ivars(child);
1061 rle = resource_list_find(&dinfo->resources, type, *rid);
1064 return (NULL); /* no resource of that type/rid */
1066 if (rle->res == NULL) {
1068 case SYS_RES_IOPORT:
1069 r = bus_alloc_resource(dev, type, rid, start, end,
1070 count, rman_make_alignment_flags(count));
1073 resource_list_add(&dinfo->resources, type, *rid,
1074 rman_get_start(r), rman_get_end(r), count);
1075 rle = resource_list_find(&dinfo->resources, type, *rid);
1080 case SYS_RES_MEMORY:
1087 if (rle->res->r_dev != dev)
1089 bus_release_resource(dev, type, *rid, rle->res);
1092 case SYS_RES_IOPORT:
1093 case SYS_RES_MEMORY:
1094 if (!(flags & RF_ALIGNMENT_MASK))
1095 flags |= rman_make_alignment_flags(rle->count);
1098 flags |= RF_SHAREABLE;
1101 rle->res = resource_list_alloc(&dinfo->resources, dev, child,
1102 type, rid, rle->start, rle->end, rle->count, flags);
1105 device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1110 pccard_release_resource(device_t dev, device_t child, int type, int rid,
1113 struct pccard_ivar *dinfo;
1114 int passthrough = (device_get_parent(child) != dev);
1115 struct resource_list_entry *rle = 0;
1120 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1123 dinfo = device_get_ivars(child);
1125 rle = resource_list_find(&dinfo->resources, type, rid);
1128 device_printf(dev, "Allocated resource not found, "
1130 type, rid, rman_get_start(r), rman_get_size(r));
1134 device_printf(dev, "Allocated resource not recorded\n");
1138 ret = BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1141 case SYS_RES_IOPORT:
1142 case SYS_RES_MEMORY:
1143 flags = rman_make_alignment_flags(rle->count);
1146 flags = RF_SHAREABLE;
1151 rle->res = bus_alloc_resource(dev, type, &rid,
1152 rle->start, rle->end, rle->count, flags);
1153 if (rle->res == NULL)
1154 device_printf(dev, "release_resource: "
1155 "unable to reaquire resource\n");
1160 pccard_child_detached(device_t parent, device_t dev)
1162 struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1163 struct pccard_function *pf = ivar->fcn;
1165 pccard_function_disable(pf);
1169 pccard_intr(void *arg)
1171 struct pccard_function *pf = (struct pccard_function*) arg;
1176 * MFC cards know if they interrupted, so we have to ack the
1177 * interrupt and call the ISR. Non-MFC cards don't have these
1178 * bits, so they always get called. Many non-MFC cards have
1179 * this bit set always upon read, but some do not.
1181 * We always ack the interrupt, even if there's no ISR
1182 * for the card. This is done on the theory that acking
1183 * the interrupt will pacify the card enough to keep an
1184 * interrupt storm from happening. Of course this won't
1185 * help in the non-MFC case.
1187 if (pccard_mfc(pf->sc)) {
1188 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1189 if (reg & PCCARD_CCR_STATUS_INTR)
1190 pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1191 reg & ~PCCARD_CCR_STATUS_INTR);
1195 if (pf->intr_handler != NULL && doisr)
1196 pf->intr_handler(pf->intr_handler_arg);
1200 pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1201 int flags, driver_intr_t *intr, void *arg, void **cookiep)
1203 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1204 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1205 struct pccard_function *func = ivar->fcn;
1208 if (func->intr_handler != NULL)
1209 panic("Only one interrupt handler per function allowed");
1210 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
1214 func->intr_handler = intr;
1215 func->intr_handler_arg = arg;
1216 func->intr_handler_cookie = *cookiep;
1217 if (pccard_mfc(sc)) {
1218 pccard_ccr_write(func, PCCARD_CCR_OPTION,
1219 pccard_ccr_read(func, PCCARD_CCR_OPTION) |
1220 PCCARD_CCR_OPTION_IREQ_ENABLE);
1226 pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1229 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1230 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1231 struct pccard_function *func = ivar->fcn;
1234 if (pccard_mfc(sc)) {
1235 pccard_ccr_write(func, PCCARD_CCR_OPTION,
1236 pccard_ccr_read(func, PCCARD_CCR_OPTION) &
1237 ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1239 ret = bus_generic_teardown_intr(dev, child, r, cookie);
1241 func->intr_handler = NULL;
1242 func->intr_handler_arg = NULL;
1243 func->intr_handler_cookie = NULL;
1249 static device_method_t pccard_methods[] = {
1250 /* Device interface */
1251 DEVMETHOD(device_probe, pccard_probe),
1252 DEVMETHOD(device_attach, pccard_attach),
1253 DEVMETHOD(device_detach, pccard_detach),
1254 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1255 DEVMETHOD(device_suspend, pccard_suspend),
1256 DEVMETHOD(device_resume, pccard_resume),
1259 DEVMETHOD(bus_print_child, pccard_print_child),
1260 DEVMETHOD(bus_driver_added, pccard_driver_added),
1261 DEVMETHOD(bus_child_detached, pccard_child_detached),
1262 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource),
1263 DEVMETHOD(bus_release_resource, pccard_release_resource),
1264 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1265 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1266 DEVMETHOD(bus_setup_intr, pccard_setup_intr),
1267 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr),
1268 DEVMETHOD(bus_set_resource, pccard_set_resource),
1269 DEVMETHOD(bus_get_resource, pccard_get_resource),
1270 DEVMETHOD(bus_delete_resource, pccard_delete_resource),
1271 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch),
1272 DEVMETHOD(bus_read_ivar, pccard_read_ivar),
1273 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1274 DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1276 /* Card Interface */
1277 DEVMETHOD(card_set_res_flags, pccard_set_res_flags),
1278 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1279 DEVMETHOD(card_get_type, pccard_card_gettype),
1280 DEVMETHOD(card_attach_card, pccard_attach_card),
1281 DEVMETHOD(card_detach_card, pccard_detach_card),
1282 DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1283 DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1284 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1289 static driver_t pccard_driver = {
1292 sizeof(struct pccard_softc)
1295 devclass_t pccard_devclass;
1297 /* Maybe we need to have a slot device? */
1298 DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1299 DRIVER_MODULE(pccard, pc98pcic, pccard_driver, pccard_devclass, 0, 0);
1300 DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1301 DRIVER_MODULE(pccard, tcic, pccard_driver, pccard_devclass, 0, 0);
1302 MODULE_VERSION(pccard, 1);
1303 /*MODULE_DEPEND(pccard, pcic, 1, 1, 1);*/