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.
31 * $FreeBSD: src/sys/dev/pccard/pccard.c,v 1.108 2005/07/15 01:43:08 imp Exp $
32 * $DragonFly: src/sys/bus/pccard/pccard.c,v 1.21 2007/08/14 15:32:32 sephe Exp $
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>
47 #include <net/ethernet.h>
49 #include <bus/pccard/pccardreg.h>
50 #include <bus/pccard/pccardvar.h>
51 #include <bus/pccard/pccard_cis.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) kprintf arg
74 #define DEVPRINTF(arg) if (pccard_debug) device_printf arg
75 #define PRVERBOSE(arg) kprintf arg
76 #define DEVPRVERBOSE(arg) device_printf arg
79 #define DEVPRINTF(arg)
80 #define PRVERBOSE(arg) if (bootverbose) kprintf 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 void pccard_function_init(struct pccard_function
*pf
);
89 static void pccard_function_free(struct pccard_function
*pf
);
90 static int pccard_function_enable(struct pccard_function
*pf
);
91 static void pccard_function_disable(struct pccard_function
*pf
);
92 static int pccard_compat_do_probe(device_t bus
, device_t dev
);
93 static int pccard_compat_do_attach(device_t bus
, device_t dev
);
94 static int pccard_add_children(device_t dev
, int busno
);
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 void pccard_intr(void *arg
);
122 static int pccard_setup_intr(device_t dev
, device_t child
,
123 struct resource
*irq
, int flags
, driver_intr_t
*intr
,
124 void *arg
, void **cookiep
, lwkt_serialize_t serializer
);
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 vendorstr
= pccard_get_vendor_str(dev
);
158 prodstr
= pccard_get_product_str(dev
);
159 if (vendorstr
!= NULL
&& prodstr
!= NULL
) {
160 str
= kmalloc(strlen(vendorstr
) + strlen(prodstr
) + 2, M_DEVBUF
,
162 ksprintf(str
, "%s %s", vendorstr
, prodstr
);
163 device_set_desc_copy(dev
, str
);
164 kfree(str
, M_DEVBUF
);
166 vendor
= pccard_get_vendor(dev
);
167 prod
= pccard_get_product(dev
);
169 str
= kmalloc(100, M_DEVBUF
, M_WAITOK
);
170 ksnprintf(str
, 100, "vendor=0x%x product=0x%x", vendor
, prod
);
171 device_set_desc_copy(dev
, str
);
172 kfree(str
, M_DEVBUF
);
178 pccard_attach_card(device_t dev
)
180 struct pccard_softc
*sc
= PCCARD_SOFTC(dev
);
181 struct pccard_function
*pf
;
182 struct pccard_ivar
*ivar
;
187 * this is here so that when socket_enable calls gettype, trt happens
189 STAILQ_INIT(&sc
->card
.pf_head
);
191 DEVPRINTF((dev
, "chip_socket_enable\n"));
192 POWER_ENABLE_SOCKET(device_get_parent(dev
), dev
);
194 DEVPRINTF((dev
, "read_cis\n"));
197 DEVPRINTF((dev
, "check_cis_quirks\n"));
198 pccard_check_cis_quirks(dev
);
201 * bail now if the card has no functions, or if there was an error in
205 if (sc
->card
.error
) {
206 device_printf(dev
, "CARD ERROR!\n");
209 if (STAILQ_EMPTY(&sc
->card
.pf_head
)) {
210 device_printf(dev
, "Card has no functions!\n");
214 if (bootverbose
|| pccard_debug
)
215 pccard_print_cis(dev
);
217 DEVPRINTF((dev
, "functions scanning\n"));
219 STAILQ_FOREACH(pf
, &sc
->card
.pf_head
, pf_list
) {
221 if (STAILQ_EMPTY(&pf
->cfe_head
)) {
223 "Function %d has no config entries.!\n", i
);
230 DEVPRINTF((dev
, "Card has %d functions. pccard_mfc is %d\n", i
+ 1,
233 STAILQ_FOREACH(pf
, &sc
->card
.pf_head
, pf_list
) {
234 if (STAILQ_EMPTY(&pf
->cfe_head
))
237 * In NetBSD, the drivers are responsible for activating
238 * each function of a card. I think that in FreeBSD we
239 * want to activate them enough for the usual bus_*_resource
240 * routines will do the right thing. This many mean a
241 * departure from the current NetBSD model.
243 * This seems to work well in practice for most cards.
244 * However, there are two cases that are problematic.
245 * If a driver wishes to pick and chose which config
246 * entry to use, then this method falls down. These
247 * are usually older cards. In addition, there are
248 * some cards that have multiple hardware units on the
249 * cards, but presents only one CIS chain. These cards
250 * are combination cards, but only one of these units
251 * can be on at a time.
253 ivar
= kmalloc(sizeof(struct pccard_ivar
), M_DEVBUF
,
255 resource_list_init(&ivar
->resources
);
256 child
= device_add_child(dev
, NULL
, -1);
257 device_set_ivars(child
, ivar
);
261 * XXX We might want to move the next three lines into
262 * XXX the pccard interface layer. For the moment, this
263 * XXX is OK, but some drivers want to pick the config
264 * XXX entry to use as well as some address tweaks (mostly
265 * XXX due to bugs in decode logic that makes some
266 * XXX addresses illegal or broken).
268 pccard_function_init(pf
);
269 if (sc
->sc_enabled_count
== 0)
270 POWER_ENABLE_SOCKET(device_get_parent(dev
), dev
);
271 if (pccard_function_enable(pf
) == 0 &&
272 pccard_set_default_descr(child
) == 0 &&
273 device_probe_and_attach(child
) == 0) {
274 DEVPRINTF((sc
->dev
, "function %d CCR at %d "
275 "offset %x mask %x: "
276 "%x %x %x %x, %x %x %x %x, %x\n",
277 pf
->number
, pf
->pf_ccr_window
, pf
->pf_ccr_offset
,
278 pf
->ccr_mask
, pccard_ccr_read(pf
, 0x00),
279 pccard_ccr_read(pf
, 0x02), pccard_ccr_read(pf
, 0x04),
280 pccard_ccr_read(pf
, 0x06), pccard_ccr_read(pf
, 0x0A),
281 pccard_ccr_read(pf
, 0x0C), pccard_ccr_read(pf
, 0x0E),
282 pccard_ccr_read(pf
, 0x10), pccard_ccr_read(pf
, 0x12)));
285 pccard_function_disable(pf
);
292 pccard_detach_card(device_t dev
)
294 struct pccard_softc
*sc
= PCCARD_SOFTC(dev
);
295 struct pccard_function
*pf
;
296 struct pccard_config_entry
*cfe
;
300 * We are running on either the PCCARD socket's event thread
301 * or in user context detaching a device by user request.
303 STAILQ_FOREACH(pf
, &sc
->card
.pf_head
, pf_list
) {
306 state
= device_get_state(pf
->dev
);
307 if (state
== DS_ATTACHED
|| state
== DS_BUSY
)
308 device_detach(pf
->dev
);
310 pccard_function_disable(pf
);
311 pccard_function_free(pf
);
312 device_delete_child(dev
, pf
->dev
);
314 if (sc
->sc_enabled_count
== 0)
315 POWER_DISABLE_SOCKET(device_get_parent(dev
), dev
);
317 while (NULL
!= (pf
= STAILQ_FIRST(&sc
->card
.pf_head
))) {
318 while (NULL
!= (cfe
= STAILQ_FIRST(&pf
->cfe_head
))) {
319 STAILQ_REMOVE_HEAD(&pf
->cfe_head
, cfe_list
);
320 kfree(cfe
, M_DEVBUF
);
322 STAILQ_REMOVE_HEAD(&sc
->card
.pf_head
, pf_list
);
328 static const struct pccard_product
*
329 pccard_do_product_lookup(device_t bus
, device_t dev
,
330 const struct pccard_product
*tab
, size_t ent_size
,
331 pccard_product_match_fn matchfn
)
333 const struct pccard_product
*ent
;
337 const char *vendorstr
;
343 if (sizeof *ent
> ent_size
)
344 panic("pccard_product_lookup: bogus ent_size %jd",
345 (intmax_t) ent_size
);
347 vendor
= pccard_get_vendor(dev
);
348 prod
= pccard_get_product(dev
);
349 vendorstr
= pccard_get_vendor_str(dev
);
350 prodstr
= pccard_get_product_str(dev
);
351 cis3str
= pccard_get_cis3_str(dev
);
352 cis4str
= pccard_get_cis4_str(dev
);
354 for (ent
= tab
; ent
->pp_vendor
!= 0; ent
=
355 (const struct pccard_product
*) ((const char *) ent
+ ent_size
)) {
357 if (ent
->pp_vendor
== PCCARD_VENDOR_ANY
&&
358 ent
->pp_product
== PCCARD_PRODUCT_ANY
&&
359 ent
->pp_cis
[0] == NULL
&&
360 ent
->pp_cis
[1] == NULL
) {
363 "Total wildcard entry ignored for %s\n",
367 if (matches
&& ent
->pp_vendor
!= PCCARD_VENDOR_ANY
&&
368 vendor
!= ent
->pp_vendor
)
370 if (matches
&& ent
->pp_product
!= PCCARD_PRODUCT_ANY
&&
371 prod
!= ent
->pp_product
)
373 if (matches
&& ent
->pp_cis
[0] &&
374 (vendorstr
== NULL
||
375 strcmp(ent
->pp_cis
[0], vendorstr
) != 0))
377 if (matches
&& ent
->pp_cis
[1] &&
379 strcmp(ent
->pp_cis
[1], prodstr
) != 0))
381 if (matches
&& ent
->pp_cis
[2] &&
383 strcmp(ent
->pp_cis
[2], cis3str
) != 0))
385 if (matches
&& ent
->pp_cis
[3] &&
387 strcmp(ent
->pp_cis
[3], cis4str
) != 0))
390 matches
= (*matchfn
)(dev
, ent
, matches
);
398 * Initialize a PCCARD function. May be called as long as the function is
401 * Note: pccard_function_init should not keep resources allocated. It should
402 * only set them up ala isa pnp, set the values in the rl lists, and return.
403 * Any resource held after pccard_function_init is called is a bug. However,
404 * the bus routines to get the resources also assume that pccard_function_init
405 * does this, so they need to be fixed too.
408 pccard_function_init(struct pccard_function
*pf
)
410 struct pccard_config_entry
*cfe
;
411 struct pccard_ivar
*devi
= PCCARD_IVAR(pf
->dev
);
412 struct resource_list
*rl
= &devi
->resources
;
413 struct resource_list_entry
*rle
;
414 struct resource
*r
= 0;
416 u_long start
, end
, len
;
419 if (pf
->pf_flags
& PFF_ENABLED
) {
420 kprintf("pccard_function_init: function is enabled");
423 bus
= device_get_parent(pf
->dev
);
424 /* Remember which configuration entry we are using. */
425 STAILQ_FOREACH(cfe
, &pf
->cfe_head
, cfe_list
) {
426 if (cfe
->iftype
!= PCCARD_IFTYPE_IO
)
429 for (i
= 0; i
< cfe
->num_iospace
; i
++) {
430 start
= cfe
->iospace
[i
].start
;
432 end
= start
+ cfe
->iospace
[i
].length
- 1;
435 DEVPRINTF((bus
, "I/O rid %d start %lx end %lx\n",
438 len
= cfe
->iospace
[i
].length
;
439 r
= bus_alloc_resource(bus
, SYS_RES_IOPORT
, &rid
,
440 start
, end
, len
, rman_make_alignment_flags(len
));
443 resource_list_add(rl
, SYS_RES_IOPORT
,
444 rid
, rman_get_start(r
), rman_get_end(r
),
445 cfe
->iospace
[i
].length
);
446 rle
= resource_list_find(rl
, SYS_RES_IOPORT
, rid
);
448 panic("Cannot add resource rid %d IOPORT", rid
);
452 for (i
= 0; i
< cfe
->num_memspace
; i
++) {
453 start
= cfe
->memspace
[i
].hostaddr
;
455 end
= start
+ cfe
->memspace
[i
].length
- 1;
458 DEVPRINTF((bus
, "Memory rid %d start %lx end %lx\n",
461 len
= cfe
->memspace
[i
].length
;
462 r
= bus_alloc_resource(bus
, SYS_RES_MEMORY
, &rid
,
463 start
, end
, len
, rman_make_alignment_flags(len
));
466 resource_list_add(rl
, SYS_RES_MEMORY
,
467 rid
, rman_get_start(r
), rman_get_end(r
),
468 cfe
->memspace
[i
].length
);
469 rle
= resource_list_find(rl
, SYS_RES_MEMORY
, rid
);
471 panic("Cannot add resource rid %d MEM", rid
);
476 DEVPRINTF((bus
, "Neither memory nor I/O mapped\n"));
481 r
= bus_alloc_resource_any(bus
, SYS_RES_IRQ
, &rid
,
485 resource_list_add(rl
, SYS_RES_IRQ
, rid
,
486 rman_get_start(r
), rman_get_end(r
), 1);
487 rle
= resource_list_find(rl
, SYS_RES_IRQ
, rid
);
489 panic("Cannot add resource rid %d IRQ", rid
);
492 /* If we get to here, we've allocated all we need */
496 DEVPRVERBOSE((bus
, "Allocation failed for cfe %d\n",
499 resource_list_purge(rl
);
505 * Free resources allocated by pccard_function_init(), May be called as long
506 * as the function is disabled.
508 * NOTE: This function should be unnecessary. pccard_function_init should
509 * never keep resources initialized.
512 pccard_function_free(struct pccard_function
*pf
)
514 struct pccard_ivar
*devi
= PCCARD_IVAR(pf
->dev
);
515 struct resource_list_entry
*rle
;
517 if (pf
->pf_flags
& PFF_ENABLED
) {
518 kprintf("pccard_function_init: function is enabled");
522 SLIST_FOREACH(rle
, &devi
->resources
, link
) {
524 if (rman_get_device(rle
->res
) != pf
->sc
->dev
)
525 device_printf(pf
->sc
->dev
,
526 "function_free: Resource still owned by "
528 "(type=%d, rid=%d, addr=%lx)\n",
530 rman_get_start(rle
->res
));
531 BUS_RELEASE_RESOURCE(device_get_parent(pf
->sc
->dev
),
532 pf
->sc
->dev
, rle
->type
, rle
->rid
, rle
->res
);
536 resource_list_free(&devi
->resources
);
540 pccard_mfc_adjust_iobase(struct pccard_function
*pf
, bus_addr_t addr
,
541 bus_addr_t offset
, bus_size_t size
)
543 bus_size_t iosize
, tmp
;
546 if (pf
->pf_mfc_iomax
== 0) {
547 pf
->pf_mfc_iobase
= addr
+ offset
;
548 pf
->pf_mfc_iomax
= pf
->pf_mfc_iobase
+ size
;
550 /* this makes the assumption that nothing overlaps */
551 if (pf
->pf_mfc_iobase
> addr
+ offset
)
552 pf
->pf_mfc_iobase
= addr
+ offset
;
553 if (pf
->pf_mfc_iomax
< addr
+ offset
+ size
)
554 pf
->pf_mfc_iomax
= addr
+ offset
+ size
;
558 tmp
= pf
->pf_mfc_iomax
- pf
->pf_mfc_iobase
;
559 /* round up to nearest (2^n)-1 */
560 for (iosize
= 1; iosize
< tmp
; iosize
<<= 1)
564 DEVPRINTF((pf
->dev
, "MFC: I/O base %#jx IOSIZE %#jx\n",
565 (uintmax_t)pf
->pf_mfc_iobase
, (uintmax_t)(iosize
+ 1)));
566 pccard_ccr_write(pf
, PCCARD_CCR_IOBASE0
,
567 pf
->pf_mfc_iobase
& 0xff);
568 pccard_ccr_write(pf
, PCCARD_CCR_IOBASE1
,
569 (pf
->pf_mfc_iobase
>> 8) & 0xff);
570 pccard_ccr_write(pf
, PCCARD_CCR_IOBASE2
, 0);
571 pccard_ccr_write(pf
, PCCARD_CCR_IOBASE3
, 0);
572 pccard_ccr_write(pf
, PCCARD_CCR_IOSIZE
, iosize
);
575 /* Enable a PCCARD function */
577 pccard_function_enable(struct pccard_function
*pf
)
579 struct pccard_function
*tmp
;
581 device_t dev
= pf
->sc
->dev
;
583 if (pf
->cfe
== NULL
) {
584 DEVPRVERBOSE((dev
, "No config entry could be allocated.\n"));
589 * Increase the reference count on the socket, enabling power, if
592 pf
->sc
->sc_enabled_count
++;
594 if (pf
->pf_flags
& PFF_ENABLED
) {
596 * Don't do anything if we're already enabled.
602 * it's possible for different functions' CCRs to be in the same
603 * underlying page. Check for that.
605 STAILQ_FOREACH(tmp
, &pf
->sc
->card
.pf_head
, pf_list
) {
606 if ((tmp
->pf_flags
& PFF_ENABLED
) &&
607 (pf
->ccr_base
>= (tmp
->ccr_base
- tmp
->pf_ccr_offset
)) &&
608 ((pf
->ccr_base
+ PCCARD_CCR_SIZE
) <=
609 (tmp
->ccr_base
- tmp
->pf_ccr_offset
+
610 tmp
->pf_ccr_realsize
))) {
611 pf
->pf_ccrt
= tmp
->pf_ccrt
;
612 pf
->pf_ccrh
= tmp
->pf_ccrh
;
613 pf
->pf_ccr_realsize
= tmp
->pf_ccr_realsize
;
616 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
617 * tmp->ccr_base) + pf->ccr_base;
619 /* pf->pf_ccr_offset =
620 (tmp->pf_ccr_offset + pf->ccr_base) -
622 pf
->pf_ccr_window
= tmp
->pf_ccr_window
;
628 pf
->ccr_res
= bus_alloc_resource(dev
, SYS_RES_MEMORY
,
629 &pf
->ccr_rid
, 0, ~0, 1 << 10, RF_ACTIVE
);
632 DEVPRINTF((dev
, "ccr_res == %lx-%lx, base=%x\n",
633 rman_get_start(pf
->ccr_res
), rman_get_end(pf
->ccr_res
),
635 CARD_SET_RES_FLAGS(device_get_parent(dev
), dev
, SYS_RES_MEMORY
,
636 pf
->ccr_rid
, PCCARD_A_MEM_ATTR
);
637 CARD_SET_MEMORY_OFFSET(device_get_parent(dev
), dev
,
638 pf
->ccr_rid
, pf
->ccr_base
, &pf
->pf_ccr_offset
);
639 pf
->pf_ccrt
= rman_get_bustag(pf
->ccr_res
);
640 pf
->pf_ccrh
= rman_get_bushandle(pf
->ccr_res
);
641 pf
->pf_ccr_realsize
= 1;
644 reg
= (pf
->cfe
->number
& PCCARD_CCR_OPTION_CFINDEX
);
645 reg
|= PCCARD_CCR_OPTION_LEVIREQ
;
646 if (pccard_mfc(pf
->sc
)) {
647 reg
|= (PCCARD_CCR_OPTION_FUNC_ENABLE
|
648 PCCARD_CCR_OPTION_ADDR_DECODE
);
649 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
651 pccard_ccr_write(pf
, PCCARD_CCR_OPTION
, reg
);
654 if ((pf
->cfe
->flags
& PCCARD_CFE_IO16
) == 0)
655 reg
|= PCCARD_CCR_STATUS_IOIS8
;
656 if (pf
->cfe
->flags
& PCCARD_CFE_AUDIO
)
657 reg
|= PCCARD_CCR_STATUS_AUDIO
;
658 pccard_ccr_write(pf
, PCCARD_CCR_STATUS
, reg
);
660 pccard_ccr_write(pf
, PCCARD_CCR_SOCKETCOPY
, 0);
662 if (pccard_mfc(pf
->sc
))
663 pccard_mfc_adjust_iobase(pf
, 0, 0, 0);
667 STAILQ_FOREACH(tmp
, &pf
->sc
->card
.pf_head
, pf_list
) {
668 device_printf(tmp
->sc
->dev
,
669 "function %d CCR at %d offset %x: "
670 "%x %x %x %x, %x %x %x %x, %x\n",
671 tmp
->number
, tmp
->pf_ccr_window
,
673 pccard_ccr_read(tmp
, 0x00),
674 pccard_ccr_read(tmp
, 0x02),
675 pccard_ccr_read(tmp
, 0x04),
676 pccard_ccr_read(tmp
, 0x06),
677 pccard_ccr_read(tmp
, 0x0A),
678 pccard_ccr_read(tmp
, 0x0C),
679 pccard_ccr_read(tmp
, 0x0E),
680 pccard_ccr_read(tmp
, 0x10),
681 pccard_ccr_read(tmp
, 0x12));
685 pf
->pf_flags
|= PFF_ENABLED
;
690 * Decrement the reference count, and power down the socket, if
693 pf
->sc
->sc_enabled_count
--;
694 DEVPRINTF((dev
, "bad --enabled_count = %d\n", pf
->sc
->sc_enabled_count
));
699 /* Disable PCCARD function. */
701 pccard_function_disable(struct pccard_function
*pf
)
703 struct pccard_function
*tmp
;
704 device_t dev
= pf
->sc
->dev
;
707 panic("pccard_function_disable: function not initialized");
709 if ((pf
->pf_flags
& PFF_ENABLED
) == 0) {
711 * Don't do anything if we're already disabled.
716 if (pf
->intr_handler
!= NULL
) {
717 struct pccard_ivar
*devi
= PCCARD_IVAR(pf
->dev
);
718 struct resource_list_entry
*rle
=
719 resource_list_find(&devi
->resources
, SYS_RES_IRQ
, 0);
721 panic("Can't disable an interrupt with no IRQ res\n");
722 BUS_TEARDOWN_INTR(dev
, pf
->dev
, rle
->res
,
723 pf
->intr_handler_cookie
);
727 * it's possible for different functions' CCRs to be in the same
728 * underlying page. Check for that. Note we mark us as disabled
729 * first to avoid matching ourself.
732 pf
->pf_flags
&= ~PFF_ENABLED
;
733 STAILQ_FOREACH(tmp
, &pf
->sc
->card
.pf_head
, pf_list
) {
734 if ((tmp
->pf_flags
& PFF_ENABLED
) &&
735 (pf
->ccr_base
>= (tmp
->ccr_base
- tmp
->pf_ccr_offset
)) &&
736 ((pf
->ccr_base
+ PCCARD_CCR_SIZE
) <=
737 (tmp
->ccr_base
- tmp
->pf_ccr_offset
+
738 tmp
->pf_ccr_realsize
)))
742 /* Not used by anyone else; unmap the CCR. */
744 bus_release_resource(dev
, SYS_RES_MEMORY
, pf
->ccr_rid
,
750 * Decrement the reference count, and power down the socket, if
753 pf
->sc
->sc_enabled_count
--;
757 * simulate the old "probe" routine. In the new world order, the driver
758 * needs to grab devices while in the old they were assigned to the device by
759 * the pccardd process. These symbols are exported to the upper layers.
762 pccard_compat_do_probe(device_t bus
, device_t dev
)
764 return (CARD_COMPAT_MATCH(dev
));
768 pccard_compat_do_attach(device_t bus
, device_t dev
)
772 err
= CARD_COMPAT_PROBE(dev
);
774 err
= CARD_COMPAT_ATTACH(dev
);
778 #define PCCARD_NPORT 2
779 #define PCCARD_NMEM 5
780 #define PCCARD_NIRQ 1
781 #define PCCARD_NDRQ 0
784 pccard_add_children(device_t dev
, int busno
)
786 /* Call parent to scan for any current children */
791 pccard_probe(device_t dev
)
793 device_set_desc(dev
, "16-bit PCCard bus");
794 return (pccard_add_children(dev
, device_get_unit(dev
)));
798 pccard_attach(device_t dev
)
800 struct pccard_softc
*sc
= PCCARD_SOFTC(dev
);
803 sc
->sc_enabled_count
= 0;
804 return (bus_generic_attach(dev
));
808 pccard_detach(device_t dev
)
810 pccard_detach_card(dev
);
815 pccard_suspend(device_t self
)
817 pccard_detach_card(self
);
823 pccard_resume(device_t self
)
829 pccard_print_resources(struct resource_list
*rl
, const char *name
, int type
,
830 int count
, const char *format
)
832 struct resource_list_entry
*rle
;
837 for (i
= 0; i
< count
; i
++) {
838 rle
= resource_list_find(rl
, type
, i
);
841 kprintf(" %s ", name
);
842 else if (printed
> 0)
845 kprintf(format
, rle
->start
);
846 if (rle
->count
> 1) {
848 kprintf(format
, rle
->start
+ rle
->count
- 1);
851 /* check the first few regardless */
858 pccard_print_child(device_t dev
, device_t child
)
860 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
861 struct resource_list
*rl
= &devi
->resources
;
864 retval
+= bus_print_child_header(dev
, child
);
865 retval
+= kprintf(" at");
868 pccard_print_resources(rl
, "port", SYS_RES_IOPORT
,
869 PCCARD_NPORT
, "%#lx");
870 pccard_print_resources(rl
, "iomem", SYS_RES_MEMORY
,
871 PCCARD_NMEM
, "%#lx");
872 pccard_print_resources(rl
, "irq", SYS_RES_IRQ
, PCCARD_NIRQ
,
874 pccard_print_resources(rl
, "drq", SYS_RES_DRQ
, PCCARD_NDRQ
,
876 retval
+= kprintf(" function %d config %d", devi
->pf
->number
,
877 devi
->pf
->cfe
->number
);
880 retval
+= bus_print_child_footer(dev
, child
);
886 pccard_set_resource(device_t dev
, device_t child
, int type
, int rid
,
887 u_long start
, u_long count
)
889 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
890 struct resource_list
*rl
= &devi
->resources
;
892 if (type
!= SYS_RES_IOPORT
&& type
!= SYS_RES_MEMORY
893 && type
!= SYS_RES_IRQ
&& type
!= SYS_RES_DRQ
)
897 if (type
== SYS_RES_IOPORT
&& rid
>= PCCARD_NPORT
)
899 if (type
== SYS_RES_MEMORY
&& rid
>= PCCARD_NMEM
)
901 if (type
== SYS_RES_IRQ
&& rid
>= PCCARD_NIRQ
)
903 if (type
== SYS_RES_DRQ
&& rid
>= PCCARD_NDRQ
)
906 resource_list_add(rl
, type
, rid
, start
, start
+ count
- 1, count
);
907 if (NULL
!= resource_list_alloc(rl
, device_get_parent(dev
), dev
,
908 type
, &rid
, start
, start
+ count
- 1, count
, 0))
915 pccard_get_resource(device_t dev
, device_t child
, int type
, int rid
,
916 u_long
*startp
, u_long
*countp
)
918 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
919 struct resource_list
*rl
= &devi
->resources
;
920 struct resource_list_entry
*rle
;
922 rle
= resource_list_find(rl
, type
, rid
);
927 *startp
= rle
->start
;
929 *countp
= rle
->count
;
935 pccard_delete_resource(device_t dev
, device_t child
, int type
, int rid
)
937 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
938 struct resource_list
*rl
= &devi
->resources
;
939 resource_list_delete(rl
, type
, rid
);
943 pccard_set_res_flags(device_t dev
, device_t child
, int type
, int rid
,
946 return (CARD_SET_RES_FLAGS(device_get_parent(dev
), child
, type
,
951 pccard_set_memory_offset(device_t dev
, device_t child
, int rid
,
952 uint32_t offset
, uint32_t *deltap
)
955 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev
), child
, rid
,
960 pccard_probe_nomatch(device_t bus
, device_t child
)
962 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
963 struct pccard_function
*pf
= devi
->pf
;
964 struct pccard_softc
*sc
= PCCARD_SOFTC(bus
);
967 device_printf(bus
, "<unknown card>");
968 kprintf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
969 "at function %d\n", sc
->card
.manufacturer
, sc
->card
.product
,
970 pf
->function
, pf
->number
);
971 device_printf(bus
, " CIS info: ");
972 for (i
= 0; sc
->card
.cis1_info
[i
] != NULL
&& i
< 4; i
++)
973 kprintf("%s%s", i
> 0 ? ", " : "", sc
->card
.cis1_info
[i
]);
979 pccard_child_location_str(device_t bus
, device_t child
, char *buf
,
982 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
983 struct pccard_function
*pf
= devi
->pf
;
985 ksnprintf(buf
, buflen
, "function=%d", pf
->number
);
989 /* XXX Maybe this should be in subr_bus? */
991 pccard_safe_quote(char *dst
, const char *src
, size_t len
)
993 char *walker
= dst
, *ep
= dst
+ len
- 1;
1000 if (ep
- walker
< 2)
1010 pccard_child_pnpinfo_str(device_t bus
, device_t child
, char *buf
,
1013 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
1014 struct pccard_function
*pf
= devi
->pf
;
1015 struct pccard_softc
*sc
= PCCARD_SOFTC(bus
);
1016 char cis0
[128], cis1
[128];
1018 pccard_safe_quote(cis0
, sc
->card
.cis1_info
[0], sizeof(cis0
));
1019 pccard_safe_quote(cis1
, sc
->card
.cis1_info
[1], sizeof(cis1
));
1020 ksnprintf(buf
, buflen
, "manufacturer=0x%04x product=0x%04x "
1021 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1022 sc
->card
.manufacturer
, sc
->card
.product
, cis0
, cis1
, pf
->function
);
1027 pccard_read_ivar(device_t bus
, device_t child
, int which
, u_char
*result
)
1029 struct pccard_ivar
*devi
= PCCARD_IVAR(child
);
1030 struct pccard_function
*pf
= devi
->pf
;
1031 struct pccard_softc
*sc
= PCCARD_SOFTC(bus
);
1034 panic("No pccard function pointer");
1038 case PCCARD_IVAR_ETHADDR
:
1039 *(const uint8_t **)result
= pf
->pf_funce_lan_nid
;
1041 case PCCARD_IVAR_VENDOR
:
1042 *(uint32_t *)result
= sc
->card
.manufacturer
;
1044 case PCCARD_IVAR_PRODUCT
:
1045 *(uint32_t *)result
= sc
->card
.product
;
1047 case PCCARD_IVAR_PRODEXT
:
1048 *(uint16_t *)result
= sc
->card
.prodext
;
1050 case PCCARD_IVAR_FUNCTION
:
1051 *(uint32_t *)result
= pf
->function
;
1053 case PCCARD_IVAR_FUNCTION_NUMBER
:
1054 *(uint32_t *)result
= pf
->number
;
1056 case PCCARD_IVAR_VENDOR_STR
:
1057 *(const char **)result
= sc
->card
.cis1_info
[0];
1059 case PCCARD_IVAR_PRODUCT_STR
:
1060 *(const char **)result
= sc
->card
.cis1_info
[1];
1062 case PCCARD_IVAR_CIS3_STR
:
1063 *(const char **)result
= sc
->card
.cis1_info
[2];
1065 case PCCARD_IVAR_CIS4_STR
:
1066 *(const char **)result
= sc
->card
.cis1_info
[3];
1073 pccard_driver_added(device_t dev
, driver_t
*driver
)
1075 struct pccard_softc
*sc
= PCCARD_SOFTC(dev
);
1076 struct pccard_function
*pf
;
1079 STAILQ_FOREACH(pf
, &sc
->card
.pf_head
, pf_list
) {
1080 if (STAILQ_EMPTY(&pf
->cfe_head
))
1083 if (device_get_state(child
) != DS_NOTPRESENT
)
1085 if (pccard_function_enable(pf
) == 0 &&
1086 device_probe_and_attach(child
) == 0) {
1087 DEVPRINTF((sc
->dev
, "function %d CCR at %d "
1088 "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1089 pf
->number
, pf
->pf_ccr_window
, pf
->pf_ccr_offset
,
1090 pccard_ccr_read(pf
, 0x00),
1091 pccard_ccr_read(pf
, 0x02), pccard_ccr_read(pf
, 0x04),
1092 pccard_ccr_read(pf
, 0x06), pccard_ccr_read(pf
, 0x0A),
1093 pccard_ccr_read(pf
, 0x0C), pccard_ccr_read(pf
, 0x0E),
1094 pccard_ccr_read(pf
, 0x10), pccard_ccr_read(pf
, 0x12)));
1096 if (pf
->cfe
!= NULL
)
1097 pccard_function_disable(pf
);
1103 static struct resource
*
1104 pccard_alloc_resource(device_t dev
, device_t child
, int type
, int *rid
,
1105 u_long start
, u_long end
, u_long count
, u_int flags
)
1107 struct pccard_ivar
*dinfo
;
1108 struct resource_list_entry
*rle
= 0;
1109 int passthrough
= (device_get_parent(child
) != dev
);
1110 int isdefault
= (start
== 0 && end
== ~0UL && count
== 1);
1111 struct resource
*r
= NULL
;
1113 /* XXX I'm no longer sure this is right */
1115 return (BUS_ALLOC_RESOURCE(device_get_parent(dev
), child
,
1116 type
, rid
, start
, end
, count
, flags
));
1119 dinfo
= device_get_ivars(child
);
1120 rle
= resource_list_find(&dinfo
->resources
, type
, *rid
);
1122 if (rle
== NULL
&& isdefault
)
1123 return (NULL
); /* no resource of that type/rid */
1124 if (rle
== NULL
|| rle
->res
== NULL
) {
1125 /* XXX Need to adjust flags */
1126 r
= bus_alloc_resource(dev
, type
, rid
, start
, end
,
1130 resource_list_add(&dinfo
->resources
, type
, *rid
,
1131 rman_get_start(r
), rman_get_end(r
), count
);
1132 rle
= resource_list_find(&dinfo
->resources
, type
, *rid
);
1138 * If dev doesn't own the device, then we can't give this device
1141 if (rman_get_device(rle
->res
) != dev
)
1143 rman_set_device(rle
->res
, child
);
1144 if (flags
& RF_ACTIVE
)
1145 BUS_ACTIVATE_RESOURCE(dev
, child
, type
, *rid
, rle
->res
);
1148 device_printf(dev
, "WARNING: Resource not reserved by pccard\n");
1153 pccard_release_resource(device_t dev
, device_t child
, int type
, int rid
,
1156 struct pccard_ivar
*dinfo
;
1157 int passthrough
= (device_get_parent(child
) != dev
);
1158 struct resource_list_entry
*rle
= 0;
1161 return BUS_RELEASE_RESOURCE(device_get_parent(dev
), child
,
1164 dinfo
= device_get_ivars(child
);
1166 rle
= resource_list_find(&dinfo
->resources
, type
, rid
);
1169 device_printf(dev
, "Allocated resource not found, "
1171 type
, rid
, rman_get_start(r
), rman_get_size(r
));
1175 device_printf(dev
, "Allocated resource not recorded\n");
1179 * Deactivate the resource (since it is being released), and
1180 * assign it to the bus.
1182 BUS_DEACTIVATE_RESOURCE(dev
, child
, type
, rid
, rle
->res
);
1183 rman_set_device(rle
->res
, dev
);
1188 pccard_child_detached(device_t parent
, device_t dev
)
1190 struct pccard_ivar
*ivar
= PCCARD_IVAR(dev
);
1191 struct pccard_function
*pf
= ivar
->pf
;
1193 pccard_function_disable(pf
);
1197 pccard_intr(void *arg
)
1199 struct pccard_function
*pf
= (struct pccard_function
*) arg
;
1204 * MFC cards know if they interrupted, so we have to ack the
1205 * interrupt and call the ISR. Non-MFC cards don't have these
1206 * bits, so they always get called. Many non-MFC cards have
1207 * this bit set always upon read, but some do not.
1209 * We always ack the interrupt, even if there's no ISR
1210 * for the card. This is done on the theory that acking
1211 * the interrupt will pacify the card enough to keep an
1212 * interrupt storm from happening. Of course this won't
1213 * help in the non-MFC case.
1215 * This has no impact for MPSAFEness of the client drivers.
1216 * We register this with whatever flags the intr_handler
1217 * was registered with. All these functions are MPSAFE.
1219 if (pccard_mfc(pf
->sc
)) {
1220 reg
= pccard_ccr_read(pf
, PCCARD_CCR_STATUS
);
1221 if (reg
& PCCARD_CCR_STATUS_INTR
)
1222 pccard_ccr_write(pf
, PCCARD_CCR_STATUS
,
1223 reg
& ~PCCARD_CCR_STATUS_INTR
);
1227 if (pf
->intr_handler
!= NULL
&& doisr
)
1228 pf
->intr_handler(pf
->intr_handler_arg
);
1232 pccard_setup_intr(device_t dev
, device_t child
, struct resource
*irq
,
1233 int flags
, driver_intr_t
*intr
, void *arg
,
1234 void **cookiep
, lwkt_serialize_t serializer
)
1236 struct pccard_softc
*sc
= PCCARD_SOFTC(dev
);
1237 struct pccard_ivar
*ivar
= PCCARD_IVAR(child
);
1238 struct pccard_function
*pf
= ivar
->pf
;
1241 if (pf
->intr_handler
!= NULL
)
1242 panic("Only one interrupt handler per function allowed");
1243 err
= bus_generic_setup_intr(dev
, child
, irq
, flags
, pccard_intr
,
1244 pf
, cookiep
, serializer
);
1247 pf
->intr_handler
= intr
;
1248 pf
->intr_handler_arg
= arg
;
1249 pf
->intr_handler_cookie
= *cookiep
;
1250 if (pccard_mfc(sc
)) {
1251 pccard_ccr_write(pf
, PCCARD_CCR_OPTION
,
1252 pccard_ccr_read(pf
, PCCARD_CCR_OPTION
) |
1253 PCCARD_CCR_OPTION_IREQ_ENABLE
);
1259 pccard_teardown_intr(device_t dev
, device_t child
, struct resource
*r
,
1262 struct pccard_softc
*sc
= PCCARD_SOFTC(dev
);
1263 struct pccard_ivar
*ivar
= PCCARD_IVAR(child
);
1264 struct pccard_function
*pf
= ivar
->pf
;
1267 if (pccard_mfc(sc
)) {
1268 pccard_ccr_write(pf
, PCCARD_CCR_OPTION
,
1269 pccard_ccr_read(pf
, PCCARD_CCR_OPTION
) &
1270 ~PCCARD_CCR_OPTION_IREQ_ENABLE
);
1272 ret
= bus_generic_teardown_intr(dev
, child
, r
, cookie
);
1274 pf
->intr_handler
= NULL
;
1275 pf
->intr_handler_arg
= NULL
;
1276 pf
->intr_handler_cookie
= NULL
;
1283 pccard_activate_resource(device_t brdev
, device_t child
, int type
, int rid
,
1286 struct pccard_ivar
*ivar
= PCCARD_IVAR(child
);
1287 struct pccard_function
*pf
= ivar
->pf
;
1290 case SYS_RES_IOPORT
:
1292 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1295 if (pccard_mfc(pf
->sc
))
1296 pccard_mfc_adjust_iobase(pf
, rman_get_start(r
), 0,
1302 return (bus_generic_activate_resource(brdev
, child
, type
, rid
, r
));
1306 pccard_deactivate_resource(device_t brdev
, device_t child
, int type
,
1307 int rid
, struct resource
*r
)
1309 /* XXX undo pccard_activate_resource? XXX */
1310 return (bus_generic_deactivate_resource(brdev
, child
, type
, rid
, r
));
1313 static device_method_t pccard_methods
[] = {
1314 /* Device interface */
1315 DEVMETHOD(device_probe
, pccard_probe
),
1316 DEVMETHOD(device_attach
, pccard_attach
),
1317 DEVMETHOD(device_detach
, pccard_detach
),
1318 DEVMETHOD(device_shutdown
, bus_generic_shutdown
),
1319 DEVMETHOD(device_suspend
, pccard_suspend
),
1320 DEVMETHOD(device_resume
, pccard_resume
),
1323 DEVMETHOD(bus_print_child
, pccard_print_child
),
1324 DEVMETHOD(bus_driver_added
, pccard_driver_added
),
1325 DEVMETHOD(bus_child_detached
, pccard_child_detached
),
1326 DEVMETHOD(bus_alloc_resource
, pccard_alloc_resource
),
1327 DEVMETHOD(bus_release_resource
, pccard_release_resource
),
1328 DEVMETHOD(bus_activate_resource
, pccard_activate_resource
),
1329 DEVMETHOD(bus_deactivate_resource
, pccard_deactivate_resource
),
1330 DEVMETHOD(bus_setup_intr
, pccard_setup_intr
),
1331 DEVMETHOD(bus_teardown_intr
, pccard_teardown_intr
),
1332 DEVMETHOD(bus_set_resource
, pccard_set_resource
),
1333 DEVMETHOD(bus_get_resource
, pccard_get_resource
),
1334 DEVMETHOD(bus_delete_resource
, pccard_delete_resource
),
1335 DEVMETHOD(bus_probe_nomatch
, pccard_probe_nomatch
),
1336 DEVMETHOD(bus_read_ivar
, pccard_read_ivar
),
1337 DEVMETHOD(bus_child_pnpinfo_str
, pccard_child_pnpinfo_str
),
1338 DEVMETHOD(bus_child_location_str
, pccard_child_location_str
),
1340 /* Card Interface */
1341 DEVMETHOD(card_set_res_flags
, pccard_set_res_flags
),
1342 DEVMETHOD(card_set_memory_offset
, pccard_set_memory_offset
),
1343 DEVMETHOD(card_attach_card
, pccard_attach_card
),
1344 DEVMETHOD(card_detach_card
, pccard_detach_card
),
1345 DEVMETHOD(card_compat_do_probe
, pccard_compat_do_probe
),
1346 DEVMETHOD(card_compat_do_attach
, pccard_compat_do_attach
),
1347 DEVMETHOD(card_do_product_lookup
, pccard_do_product_lookup
),
1348 DEVMETHOD(card_cis_scan
, pccard_scan_cis
),
1353 static driver_t pccard_driver
= {
1356 sizeof(struct pccard_softc
)
1359 devclass_t pccard_devclass
;
1361 /* Maybe we need to have a slot device? */
1362 DRIVER_MODULE(pccard
, pcic
, pccard_driver
, pccard_devclass
, 0, 0);
1363 DRIVER_MODULE(pccard
, cbb
, pccard_driver
, pccard_devclass
, 0, 0);
1364 MODULE_VERSION(pccard
, 1);