Deleted Added
full compact
pccard.c (150097) pccard.c (150098)
1/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */
2
3/*-
4 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
19 *
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.
30 */
31
32#include <sys/cdefs.h>
1/* $NetBSD: pcmcia.c,v 1.23 2000/07/28 19:17:02 drochner Exp $ */
2
3/*-
4 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
19 *
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.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 150097 2005-09-13 17:49:47Z imp $");
33__FBSDID("$FreeBSD: head/sys/dev/pccard/pccard.c 150098 2005-09-13 17:56:36Z imp $");
34
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>
43
44#include <sys/bus.h>
45#include <machine/bus.h>
46#include <sys/rman.h>
47#include <machine/resource.h>
48
49#include <net/ethernet.h>
50
51#include <dev/pccard/pccardreg.h>
52#include <dev/pccard/pccardvar.h>
53#include <dev/pccard/pccard_cis.h>
54
55#include "power_if.h"
56#include "card_if.h"
57
58#define PCCARDDEBUG
59
60/* sysctl vars */
61SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
62
63int pccard_debug = 0;
64TUNABLE_INT("hw.pccard.debug", &pccard_debug);
65SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
66 &pccard_debug, 0,
67 "pccard debug");
68
69int pccard_cis_debug = 0;
70TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
71SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
72 &pccard_cis_debug, 0, "pccard CIS debug");
73
74#ifdef PCCARDDEBUG
75#define DPRINTF(arg) if (pccard_debug) printf arg
76#define DEVPRINTF(arg) if (pccard_debug) device_printf arg
77#define PRVERBOSE(arg) printf arg
78#define DEVPRVERBOSE(arg) device_printf arg
79#else
80#define DPRINTF(arg)
81#define DEVPRINTF(arg)
82#define PRVERBOSE(arg) if (bootverbose) printf arg
83#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
84#endif
85
86static int pccard_ccr_read(struct pccard_function *pf, int ccr);
87static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
88static int pccard_attach_card(device_t dev);
89static int pccard_detach_card(device_t dev);
90static void pccard_function_init(struct pccard_function *pf);
91static void pccard_function_free(struct pccard_function *pf);
92static int pccard_function_enable(struct pccard_function *pf);
93static void pccard_function_disable(struct pccard_function *pf);
94static int pccard_compat_do_probe(device_t bus, device_t dev);
95static int pccard_compat_do_attach(device_t bus, device_t dev);
96static int pccard_add_children(device_t dev, int busno);
97static int pccard_probe(device_t dev);
98static int pccard_attach(device_t dev);
99static int pccard_detach(device_t dev);
100static void pccard_print_resources(struct resource_list *rl,
101 const char *name, int type, int count, const char *format);
102static int pccard_print_child(device_t dev, device_t child);
103static int pccard_set_resource(device_t dev, device_t child, int type,
104 int rid, u_long start, u_long count);
105static int pccard_get_resource(device_t dev, device_t child, int type,
106 int rid, u_long *startp, u_long *countp);
107static void pccard_delete_resource(device_t dev, device_t child, int type,
108 int rid);
109static int pccard_set_res_flags(device_t dev, device_t child, int type,
110 int rid, uint32_t flags);
111static int pccard_set_memory_offset(device_t dev, device_t child, int rid,
112 uint32_t offset, uint32_t *deltap);
113static void pccard_probe_nomatch(device_t cbdev, device_t child);
114static int pccard_read_ivar(device_t bus, device_t child, int which,
115 u_char *result);
116static void pccard_driver_added(device_t dev, driver_t *driver);
117static struct resource *pccard_alloc_resource(device_t dev,
118 device_t child, int type, int *rid, u_long start,
119 u_long end, u_long count, u_int flags);
120static int pccard_release_resource(device_t dev, device_t child, int type,
121 int rid, struct resource *r);
122static void pccard_child_detached(device_t parent, device_t dev);
123static void pccard_intr(void *arg);
124static int pccard_setup_intr(device_t dev, device_t child,
125 struct resource *irq, int flags, driver_intr_t *intr,
126 void *arg, void **cookiep);
127static int pccard_teardown_intr(device_t dev, device_t child,
128 struct resource *r, void *cookie);
129
130static const struct pccard_product *
131pccard_do_product_lookup(device_t bus, device_t dev,
132 const struct pccard_product *tab, size_t ent_size,
133 pccard_product_match_fn matchfn);
134
135
136static int
137pccard_ccr_read(struct pccard_function *pf, int ccr)
138{
139 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
140 pf->pf_ccr_offset + ccr));
141}
142
143static void
144pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
145{
146 if ((pf->ccr_mask) & (1 << (ccr / 2))) {
147 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
148 pf->pf_ccr_offset + ccr, val);
149 }
150}
151
152static int
153pccard_set_default_descr(device_t dev)
154{
155 const char *vendorstr, *prodstr;
156 uint32_t vendor, prod;
157 char *str;
158
159 if (pccard_get_vendor_str(dev, &vendorstr))
160 return (0);
161 if (pccard_get_product_str(dev, &prodstr))
162 return (0);
163 if (vendorstr != NULL && prodstr != NULL) {
164 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
165 M_WAITOK);
166 sprintf(str, "%s %s", vendorstr, prodstr);
167 device_set_desc_copy(dev, str);
168 free(str, M_DEVBUF);
169 } else {
170 if (pccard_get_vendor(dev, &vendor))
171 return (0);
172 if (pccard_get_product(dev, &prod))
173 return (0);
174 str = malloc(100, M_DEVBUF, M_WAITOK);
175 snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod);
176 device_set_desc_copy(dev, str);
177 free(str, M_DEVBUF);
178 }
179 return (0);
180}
181
182static int
183pccard_attach_card(device_t dev)
184{
185 struct pccard_softc *sc = PCCARD_SOFTC(dev);
186 struct pccard_function *pf;
187 struct pccard_ivar *ivar;
188 device_t child;
189 int i;
190
191 /*
192 * this is here so that when socket_enable calls gettype, trt happens
193 */
194 STAILQ_INIT(&sc->card.pf_head);
195
196 DEVPRINTF((dev, "chip_socket_enable\n"));
197 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
198
199 DEVPRINTF((dev, "read_cis\n"));
200 pccard_read_cis(sc);
201
202 DEVPRINTF((dev, "check_cis_quirks\n"));
203 pccard_check_cis_quirks(dev);
204
205 /*
206 * bail now if the card has no functions, or if there was an error in
207 * the cis.
208 */
209
210 if (sc->card.error) {
211 device_printf(dev, "CARD ERROR!\n");
212 return (1);
213 }
214 if (STAILQ_EMPTY(&sc->card.pf_head)) {
215 device_printf(dev, "Card has no functions!\n");
216 return (1);
217 }
218
219 if (bootverbose || pccard_debug)
220 pccard_print_cis(dev);
221
222 DEVPRINTF((dev, "functions scanning\n"));
223 i = -1;
224 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
225 i++;
226 if (STAILQ_EMPTY(&pf->cfe_head)) {
227 device_printf(dev,
228 "Function %d has no config entries.!\n", i);
229 continue;
230 }
231 pf->sc = sc;
232 pf->cfe = NULL;
233 pf->dev = NULL;
234 }
235 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
236 pccard_mfc(sc)));
237
238 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
239 if (STAILQ_EMPTY(&pf->cfe_head))
240 continue;
241 /*
242 * In NetBSD, the drivers are responsible for activating
243 * each function of a card. I think that in FreeBSD we
244 * want to activate them enough for the usual bus_*_resource
245 * routines will do the right thing. This many mean a
246 * departure from the current NetBSD model.
247 *
248 * This seems to work well in practice for most cards.
249 * However, there are two cases that are problematic.
250 * If a driver wishes to pick and chose which config
251 * entry to use, then this method falls down. These
252 * are usually older cards. In addition, there are
253 * some cards that have multiple hardware units on the
254 * cards, but presents only one CIS chain. These cards
255 * are combination cards, but only one of these units
256 * can be on at a time.
257 */
258 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
259 M_WAITOK | M_ZERO);
260 resource_list_init(&ivar->resources);
261 child = device_add_child(dev, NULL, -1);
262 device_set_ivars(child, ivar);
263 ivar->pf = pf;
264 pf->dev = child;
265 /*
266 * XXX We might want to move the next three lines into
267 * XXX the pccard interface layer. For the moment, this
268 * XXX is OK, but some drivers want to pick the config
269 * XXX entry to use as well as some address tweaks (mostly
270 * XXX due to bugs in decode logic that makes some
271 * XXX addresses illegal or broken).
272 */
273 pccard_function_init(pf);
274 if (sc->sc_enabled_count == 0)
275 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
276 if (pccard_function_enable(pf) == 0 &&
277 pccard_set_default_descr(child) == 0 &&
278 device_probe_and_attach(child) == 0) {
279 DEVPRINTF((sc->dev, "function %d CCR at %d "
280 "offset %x mask %x: "
281 "%x %x %x %x, %x %x %x %x, %x\n",
282 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
283 pf->ccr_mask, pccard_ccr_read(pf, 0x00),
284 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
285 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
286 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
287 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
288 } else {
289 if (pf->cfe != NULL)
290 pccard_function_disable(pf);
291 }
292 }
293 return (0);
294}
295
296static int
297pccard_detach_card(device_t dev)
298{
299 struct pccard_softc *sc = PCCARD_SOFTC(dev);
300 struct pccard_function *pf;
301 struct pccard_config_entry *cfe;
302 int state;
303
304 /*
305 * We are running on either the PCCARD socket's event thread
306 * or in user context detaching a device by user request.
307 */
308 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
309 if (pf->dev == NULL)
310 continue;
311 state = device_get_state(pf->dev);
312 if (state == DS_ATTACHED || state == DS_BUSY)
313 device_detach(pf->dev);
314 if (pf->cfe != NULL)
315 pccard_function_disable(pf);
316 pccard_function_free(pf);
317 device_delete_child(dev, pf->dev);
318 }
319 if (sc->sc_enabled_count == 0)
320 POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
321
322 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
323 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
324 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
325 free(cfe, M_DEVBUF);
326 }
327 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
328 free(pf, M_DEVBUF);
329 }
330 return (0);
331}
332
333static const struct pccard_product *
334pccard_do_product_lookup(device_t bus, device_t dev,
335 const struct pccard_product *tab, size_t ent_size,
336 pccard_product_match_fn matchfn)
337{
338 const struct pccard_product *ent;
339 int matches;
340 uint32_t vendor;
341 uint32_t prod;
342 const char *vendorstr;
343 const char *prodstr;
344 const char *cis3str;
345 const char *cis4str;
346
347#ifdef DIAGNOSTIC
348 if (sizeof *ent > ent_size)
349 panic("pccard_product_lookup: bogus ent_size %jd",
350 (intmax_t) ent_size);
351#endif
352 if (pccard_get_vendor(dev, &vendor))
353 return (NULL);
354 if (pccard_get_product(dev, &prod))
355 return (NULL);
356 if (pccard_get_vendor_str(dev, &vendorstr))
357 return (NULL);
358 if (pccard_get_product_str(dev, &prodstr))
359 return (NULL);
360 if (pccard_get_cis3_str(dev, &cis3str))
361 return (NULL);
362 if (pccard_get_cis4_str(dev, &cis4str))
363 return (NULL);
364 for (ent = tab; ent->pp_vendor != 0; ent =
365 (const struct pccard_product *) ((const char *) ent + ent_size)) {
366 matches = 1;
367 if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
368 ent->pp_product == PCCARD_PRODUCT_ANY &&
369 ent->pp_cis[0] == NULL &&
370 ent->pp_cis[1] == NULL) {
371 if (ent->pp_name)
372 device_printf(dev,
373 "Total wildcard entry ignored for %s\n",
374 ent->pp_name);
375 continue;
376 }
377 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
378 vendor != ent->pp_vendor)
379 matches = 0;
380 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
381 prod != ent->pp_product)
382 matches = 0;
383 if (matches && ent->pp_cis[0] &&
384 (vendorstr == NULL ||
385 strcmp(ent->pp_cis[0], vendorstr) != 0))
386 matches = 0;
387 if (matches && ent->pp_cis[1] &&
388 (prodstr == NULL ||
389 strcmp(ent->pp_cis[1], prodstr) != 0))
390 matches = 0;
391 if (matches && ent->pp_cis[2] &&
392 (cis3str == NULL ||
393 strcmp(ent->pp_cis[2], cis3str) != 0))
394 matches = 0;
395 if (matches && ent->pp_cis[3] &&
396 (cis4str == NULL ||
397 strcmp(ent->pp_cis[3], cis4str) != 0))
398 matches = 0;
399 if (matchfn != NULL)
400 matches = (*matchfn)(dev, ent, matches);
401 if (matches)
402 return (ent);
403 }
404 return (NULL);
405}
406
407/*
408 * Initialize a PCCARD function. May be called as long as the function is
409 * disabled.
410 *
411 * Note: pccard_function_init should not keep resources allocated. It should
412 * only set them up ala isa pnp, set the values in the rl lists, and return.
413 * Any resource held after pccard_function_init is called is a bug. However,
414 * the bus routines to get the resources also assume that pccard_function_init
415 * does this, so they need to be fixed too.
416 */
417static void
418pccard_function_init(struct pccard_function *pf)
419{
420 struct pccard_config_entry *cfe;
421 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
422 struct resource_list *rl = &devi->resources;
423 struct resource_list_entry *rle;
424 struct resource *r = 0;
425 device_t bus;
426 u_long start, end, len;
427 int i, rid, spaces;
428
429 if (pf->pf_flags & PFF_ENABLED) {
430 printf("pccard_function_init: function is enabled");
431 return;
432 }
433 bus = device_get_parent(pf->dev);
434 /* Remember which configuration entry we are using. */
435 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
436 if (cfe->iftype != PCCARD_IFTYPE_IO)
437 continue;
438 spaces = 0;
439 for (i = 0; i < cfe->num_iospace; i++) {
440 start = cfe->iospace[i].start;
441 if (start)
442 end = start + cfe->iospace[i].length - 1;
443 else
444 end = ~0UL;
445 DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n",
446 i, start, end));
447 rid = i;
448 len = cfe->iospace[i].length;
449 r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
450 start, end, len, rman_make_alignment_flags(len));
451 if (r == NULL)
452 goto not_this_one;
453 rle = resource_list_add(rl, SYS_RES_IOPORT,
454 rid, rman_get_start(r), rman_get_end(r),
455 cfe->iospace[i].length);
456 if (rle == NULL)
457 panic("Cannot add resource rid %d IOPORT", rid);
458 rle->res = r;
459 spaces++;
460 }
461 for (i = 0; i < cfe->num_memspace; i++) {
462 start = cfe->memspace[i].hostaddr;
463 if (start)
464 end = start + cfe->memspace[i].length - 1;
465 else
466 end = ~0UL;
467 DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n",
468 i, start, end));
469 rid = i;
470 len = cfe->memspace[i].length;
471 r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
472 start, end, len, rman_make_alignment_flags(len));
473 if (r == NULL)
474 goto not_this_one;
475 rle = resource_list_add(rl, SYS_RES_MEMORY,
476 rid, rman_get_start(r), rman_get_end(r),
477 cfe->memspace[i].length);
478 if (rle == NULL)
479 panic("Cannot add resource rid %d MEM", rid);
480 rle->res = r;
481 spaces++;
482 }
483 if (spaces == 0) {
484 DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
485 goto not_this_one;
486 }
487 if (cfe->irqmask) {
488 rid = 0;
489 r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
490 RF_SHAREABLE);
491 if (r == NULL)
492 goto not_this_one;
493 rle = resource_list_add(rl, SYS_RES_IRQ, rid,
494 rman_get_start(r), rman_get_end(r), 1);
495 if (rle == NULL)
496 panic("Cannot add resource rid %d IRQ", rid);
497 rle->res = r;
498 }
499 /* If we get to here, we've allocated all we need */
500 pf->cfe = cfe;
501 break;
502 not_this_one:;
503 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
504 cfe->number));
505 resource_list_purge(rl);
506 }
507}
508
509/*
510 * Free resources allocated by pccard_function_init(), May be called as long
511 * as the function is disabled.
512 *
513 * NOTE: This function should be unnecessary. pccard_function_init should
514 * never keep resources initialized.
515 */
516static void
517pccard_function_free(struct pccard_function *pf)
518{
519 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
520 struct resource_list_entry *rle;
521
522 if (pf->pf_flags & PFF_ENABLED) {
523 printf("pccard_function_init: function is enabled");
524 return;
525 }
526
527 STAILQ_FOREACH(rle, &devi->resources, link) {
528 if (rle->res) {
529 if (rman_get_device(rle->res) != pf->sc->dev)
530 device_printf(pf->sc->dev,
531 "function_free: Resource still owned by "
532 "child, oops. "
533 "(type=%d, rid=%d, addr=%lx)\n",
534 rle->type, rle->rid,
535 rman_get_start(rle->res));
536 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
537 pf->sc->dev, rle->type, rle->rid, rle->res);
538 rle->res = NULL;
539 }
540 }
541 resource_list_free(&devi->resources);
542}
543
544static void
545pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
546 bus_addr_t offset, bus_size_t size)
547{
548 bus_size_t iosize, tmp;
549
550 if (addr != 0) {
551 if (pf->pf_mfc_iomax == 0) {
552 pf->pf_mfc_iobase = addr + offset;
553 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
554 } else {
555 /* this makes the assumption that nothing overlaps */
556 if (pf->pf_mfc_iobase > addr + offset)
557 pf->pf_mfc_iobase = addr + offset;
558 if (pf->pf_mfc_iomax < addr + offset + size)
559 pf->pf_mfc_iomax = addr + offset + size;
560 }
561 }
562
563 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
564 /* round up to nearest (2^n)-1 */
565 for (iosize = 1; iosize < tmp; iosize <<= 1)
566 ;
567 iosize--;
568
569 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
570 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
571 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
572 pf->pf_mfc_iobase & 0xff);
573 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
574 (pf->pf_mfc_iobase >> 8) & 0xff);
575 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
576 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
577 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
578}
579
580/* Enable a PCCARD function */
581static int
582pccard_function_enable(struct pccard_function *pf)
583{
584 struct pccard_function *tmp;
585 int reg;
586 device_t dev = pf->sc->dev;
587
588 if (pf->cfe == NULL) {
589 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
590 return (ENOMEM);
591 }
592
593 /*
594 * Increase the reference count on the socket, enabling power, if
595 * necessary.
596 */
597 pf->sc->sc_enabled_count++;
598
599 if (pf->pf_flags & PFF_ENABLED) {
600 /*
601 * Don't do anything if we're already enabled.
602 */
603 return (0);
604 }
605
606 /*
607 * it's possible for different functions' CCRs to be in the same
608 * underlying page. Check for that.
609 */
610 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
611 if ((tmp->pf_flags & PFF_ENABLED) &&
612 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
613 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
614 (tmp->ccr_base - tmp->pf_ccr_offset +
615 tmp->pf_ccr_realsize))) {
616 pf->pf_ccrt = tmp->pf_ccrt;
617 pf->pf_ccrh = tmp->pf_ccrh;
618 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
619
620 /*
621 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
622 * tmp->ccr_base) + pf->ccr_base;
623 */
624 /* pf->pf_ccr_offset =
625 (tmp->pf_ccr_offset + pf->ccr_base) -
626 tmp->ccr_base; */
627 pf->pf_ccr_window = tmp->pf_ccr_window;
628 break;
629 }
630 }
631 if (tmp == NULL) {
632 pf->ccr_rid = 0;
633 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
634 &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
635 if (!pf->ccr_res)
636 goto bad;
637 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
638 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
639 pf->ccr_base));
640 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
641 pf->ccr_rid, PCCARD_A_MEM_ATTR);
642 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
643 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
644 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
645 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
646 pf->pf_ccr_realsize = 1;
647 }
648
649 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
650 reg |= PCCARD_CCR_OPTION_LEVIREQ;
651 if (pccard_mfc(pf->sc)) {
652 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
653 PCCARD_CCR_OPTION_ADDR_DECODE);
654 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
655 }
656 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
657
658 reg = 0;
659 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
660 reg |= PCCARD_CCR_STATUS_IOIS8;
661 if (pf->cfe->flags & PCCARD_CFE_AUDIO)
662 reg |= PCCARD_CCR_STATUS_AUDIO;
663 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
664
665 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
666
667 if (pccard_mfc(pf->sc))
668 pccard_mfc_adjust_iobase(pf, 0, 0, 0);
669
670#ifdef PCCARDDEBUG
671 if (pccard_debug) {
672 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
673 device_printf(tmp->sc->dev,
674 "function %d CCR at %d offset %x: "
675 "%x %x %x %x, %x %x %x %x, %x\n",
676 tmp->number, tmp->pf_ccr_window,
677 tmp->pf_ccr_offset,
678 pccard_ccr_read(tmp, 0x00),
679 pccard_ccr_read(tmp, 0x02),
680 pccard_ccr_read(tmp, 0x04),
681 pccard_ccr_read(tmp, 0x06),
682 pccard_ccr_read(tmp, 0x0A),
683 pccard_ccr_read(tmp, 0x0C),
684 pccard_ccr_read(tmp, 0x0E),
685 pccard_ccr_read(tmp, 0x10),
686 pccard_ccr_read(tmp, 0x12));
687 }
688 }
689#endif
690 pf->pf_flags |= PFF_ENABLED;
691 return (0);
692
693 bad:
694 /*
695 * Decrement the reference count, and power down the socket, if
696 * necessary.
697 */
698 pf->sc->sc_enabled_count--;
699 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
700
701 return (1);
702}
703
704/* Disable PCCARD function. */
705static void
706pccard_function_disable(struct pccard_function *pf)
707{
708 struct pccard_function *tmp;
709 device_t dev = pf->sc->dev;
710
711 if (pf->cfe == NULL)
712 panic("pccard_function_disable: function not initialized");
713
714 if ((pf->pf_flags & PFF_ENABLED) == 0) {
715 /*
716 * Don't do anything if we're already disabled.
717 */
718 return;
719 }
720
721 if (pf->intr_handler != NULL) {
722 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
723 struct resource_list_entry *rle =
724 resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
725 if (rle == NULL)
726 panic("Can't disable an interrupt with no IRQ res\n");
727 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
728 pf->intr_handler_cookie);
729 }
730
731 /*
732 * it's possible for different functions' CCRs to be in the same
733 * underlying page. Check for that. Note we mark us as disabled
734 * first to avoid matching ourself.
735 */
736
737 pf->pf_flags &= ~PFF_ENABLED;
738 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
739 if ((tmp->pf_flags & PFF_ENABLED) &&
740 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
741 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
742 (tmp->ccr_base - tmp->pf_ccr_offset +
743 tmp->pf_ccr_realsize)))
744 break;
745 }
746
747 /* Not used by anyone else; unmap the CCR. */
748 if (tmp == NULL) {
749 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
750 pf->ccr_res);
751 pf->ccr_res = NULL;
752 }
753
754 /*
755 * Decrement the reference count, and power down the socket, if
756 * necessary.
757 */
758 pf->sc->sc_enabled_count--;
759}
760
761/*
762 * simulate the old "probe" routine. In the new world order, the driver
763 * needs to grab devices while in the old they were assigned to the device by
764 * the pccardd process. These symbols are exported to the upper layers.
765 */
766static int
767pccard_compat_do_probe(device_t bus, device_t dev)
768{
769 return (CARD_COMPAT_MATCH(dev));
770}
771
772static int
773pccard_compat_do_attach(device_t bus, device_t dev)
774{
775 int err;
776
777 err = CARD_COMPAT_PROBE(dev);
778 if (err <= 0)
779 err = CARD_COMPAT_ATTACH(dev);
780 return (err);
781}
782
783#define PCCARD_NPORT 2
784#define PCCARD_NMEM 5
785#define PCCARD_NIRQ 1
786#define PCCARD_NDRQ 0
787
788static int
789pccard_add_children(device_t dev, int busno)
790{
791 /* Call parent to scan for any current children */
792 return (0);
793}
794
795static int
796pccard_probe(device_t dev)
797{
798 device_set_desc(dev, "16-bit PCCard bus");
799 return (pccard_add_children(dev, device_get_unit(dev)));
800}
801
802static int
803pccard_attach(device_t dev)
804{
805 struct pccard_softc *sc = PCCARD_SOFTC(dev);
806
807 sc->dev = dev;
808 sc->sc_enabled_count = 0;
809 return (bus_generic_attach(dev));
810}
811
812static int
813pccard_detach(device_t dev)
814{
815 pccard_detach_card(dev);
816 return 0;
817}
818
819static int
820pccard_suspend(device_t self)
821{
822 pccard_detach_card(self);
823 return (0);
824}
825
826static
827int
828pccard_resume(device_t self)
829{
830 return (0);
831}
832
833static void
834pccard_print_resources(struct resource_list *rl, const char *name, int type,
835 int count, const char *format)
836{
837 struct resource_list_entry *rle;
838 int printed;
839 int i;
840
841 printed = 0;
842 for (i = 0; i < count; i++) {
843 rle = resource_list_find(rl, type, i);
844 if (rle != NULL) {
845 if (printed == 0)
846 printf(" %s ", name);
847 else if (printed > 0)
848 printf(",");
849 printed++;
850 printf(format, rle->start);
851 if (rle->count > 1) {
852 printf("-");
853 printf(format, rle->start + rle->count - 1);
854 }
855 } else if (i > 3) {
856 /* check the first few regardless */
857 break;
858 }
859 }
860}
861
862static int
863pccard_print_child(device_t dev, device_t child)
864{
865 struct pccard_ivar *devi = PCCARD_IVAR(child);
866 struct resource_list *rl = &devi->resources;
867 int retval = 0;
868
869 retval += bus_print_child_header(dev, child);
870 retval += printf(" at");
871
872 if (devi != NULL) {
873 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
874 PCCARD_NPORT, "%#lx");
875 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
876 PCCARD_NMEM, "%#lx");
877 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
878 "%ld");
879 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
880 "%ld");
881 retval += printf(" function %d config %d", devi->pf->number,
882 devi->pf->cfe->number);
883 }
884
885 retval += bus_print_child_footer(dev, child);
886
887 return (retval);
888}
889
890static int
891pccard_set_resource(device_t dev, device_t child, int type, int rid,
892 u_long start, u_long count)
893{
894 struct pccard_ivar *devi = PCCARD_IVAR(child);
895 struct resource_list *rl = &devi->resources;
896
897 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
898 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
899 return (EINVAL);
900 if (rid < 0)
901 return (EINVAL);
902 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
903 return (EINVAL);
904 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
905 return (EINVAL);
906 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
907 return (EINVAL);
908 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
909 return (EINVAL);
910
911 resource_list_add(rl, type, rid, start, start + count - 1, count);
912 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
913 type, &rid, start, start + count - 1, count, 0))
914 return 0;
915 else
916 return ENOMEM;
917}
918
919static int
920pccard_get_resource(device_t dev, device_t child, int type, int rid,
921 u_long *startp, u_long *countp)
922{
923 struct pccard_ivar *devi = PCCARD_IVAR(child);
924 struct resource_list *rl = &devi->resources;
925 struct resource_list_entry *rle;
926
927 rle = resource_list_find(rl, type, rid);
928 if (rle == NULL)
929 return (ENOENT);
930
931 if (startp != NULL)
932 *startp = rle->start;
933 if (countp != NULL)
934 *countp = rle->count;
935
936 return (0);
937}
938
939static void
940pccard_delete_resource(device_t dev, device_t child, int type, int rid)
941{
942 struct pccard_ivar *devi = PCCARD_IVAR(child);
943 struct resource_list *rl = &devi->resources;
944 resource_list_delete(rl, type, rid);
945}
946
947static int
948pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
949 uint32_t flags)
950{
951 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
952 rid, flags));
953}
954
955static int
956pccard_set_memory_offset(device_t dev, device_t child, int rid,
957 uint32_t offset, uint32_t *deltap)
958
959{
960 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
961 offset, deltap));
962}
963
964static void
965pccard_probe_nomatch(device_t bus, device_t child)
966{
967 struct pccard_ivar *devi = PCCARD_IVAR(child);
968 struct pccard_function *pf = devi->pf;
969 struct pccard_softc *sc = PCCARD_SOFTC(bus);
970 int i;
971
972 device_printf(bus, "<unknown card>");
973 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
974 "at function %d\n", sc->card.manufacturer, sc->card.product,
975 pf->function, pf->number);
976 device_printf(bus, " CIS info: ");
977 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
978 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
979 printf("\n");
980 return;
981}
982
983static int
984pccard_child_location_str(device_t bus, device_t child, char *buf,
985 size_t buflen)
986{
987 struct pccard_ivar *devi = PCCARD_IVAR(child);
988 struct pccard_function *pf = devi->pf;
989
990 snprintf(buf, buflen, "function=%d", pf->number);
991 return (0);
992}
993
994/* XXX Maybe this should be in subr_bus? */
995static void
996pccard_safe_quote(char *dst, const char *src, size_t len)
997{
998 char *walker = dst, *ep = dst + len - 1;
999
1000 if (len == 0)
1001 return;
1002 while (walker < ep)
1003 {
1004 if (*src == '"') {
1005 if (ep - walker < 2)
1006 break;
1007 *walker++ = '\\';
1008 }
1009 *walker++ = *src++;
1010 }
1011 *walker = '\0';
1012}
1013
1014static int
1015pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1016 size_t buflen)
1017{
1018 struct pccard_ivar *devi = PCCARD_IVAR(child);
1019 struct pccard_function *pf = devi->pf;
1020 struct pccard_softc *sc = PCCARD_SOFTC(bus);
1021 char cis0[128], cis1[128];
1022
1023 pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1024 pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1025 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1026 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1027 sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1028 return (0);
1029}
1030
1031static int
1032pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
1033{
1034 struct pccard_ivar *devi = PCCARD_IVAR(child);
1035 struct pccard_function *pf = devi->pf;
1036 struct pccard_softc *sc = PCCARD_SOFTC(bus);
1037
1038 if (!pf)
1039 panic("No pccard function pointer");
1040 switch (which) {
1041 default:
1042 return (EINVAL);
1043 case PCCARD_IVAR_ETHADDR:
1044 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1045 break;
1046 case PCCARD_IVAR_VENDOR:
1047 *(uint32_t *)result = sc->card.manufacturer;
1048 break;
1049 case PCCARD_IVAR_PRODUCT:
1050 *(uint32_t *)result = sc->card.product;
1051 break;
1052 case PCCARD_IVAR_PRODEXT:
1053 *(uint16_t *)result = sc->card.prodext;
1054 break;
1055 case PCCARD_IVAR_FUNCTION:
1056 *(uint32_t *)result = pf->function;
1057 break;
1058 case PCCARD_IVAR_FUNCTION_NUMBER:
1059 *(uint32_t *)result = pf->number;
1060 break;
1061 case PCCARD_IVAR_VENDOR_STR:
1062 *(const char **)result = sc->card.cis1_info[0];
1063 break;
1064 case PCCARD_IVAR_PRODUCT_STR:
1065 *(const char **)result = sc->card.cis1_info[1];
1066 break;
1067 case PCCARD_IVAR_CIS3_STR:
1068 *(const char **)result = sc->card.cis1_info[2];
1069 break;
1070 case PCCARD_IVAR_CIS4_STR:
1071 *(const char **)result = sc->card.cis1_info[3];
1072 break;
1073 }
1074 return (0);
1075}
1076
1077static void
1078pccard_driver_added(device_t dev, driver_t *driver)
1079{
1080 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1081 struct pccard_function *pf;
1082 device_t child;
1083
1084 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1085 if (STAILQ_EMPTY(&pf->cfe_head))
1086 continue;
1087 child = pf->dev;
1088 if (device_get_state(child) != DS_NOTPRESENT)
1089 continue;
1090 if (pccard_function_enable(pf) == 0 &&
1091 device_probe_and_attach(child) == 0) {
1092 DEVPRINTF((sc->dev, "function %d CCR at %d "
1093 "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1094 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1095 pccard_ccr_read(pf, 0x00),
1096 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1097 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1098 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1099 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1100 } else {
1101 if (pf->cfe != NULL)
1102 pccard_function_disable(pf);
1103 }
1104 }
1105 return;
1106}
1107
1108static struct resource *
1109pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1110 u_long start, u_long end, u_long count, u_int flags)
1111{
1112 struct pccard_ivar *dinfo;
1113 struct resource_list_entry *rle = 0;
1114 int passthrough = (device_get_parent(child) != dev);
1115 int isdefault = (start == 0 && end == ~0UL && count == 1);
1116 struct resource *r = NULL;
1117
1118 /* XXX I'm no longer sure this is right */
1119 if (passthrough) {
1120 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1121 type, rid, start, end, count, flags));
1122 }
1123
1124 dinfo = device_get_ivars(child);
1125 rle = resource_list_find(&dinfo->resources, type, *rid);
1126
1127 if (rle == NULL && isdefault)
1128 return (NULL); /* no resource of that type/rid */
1129 if (rle == NULL || rle->res == NULL) {
1130 /* XXX Need to adjust flags */
1131 r = bus_alloc_resource(dev, type, rid, start, end,
1132 count, flags);
1133 if (r == NULL)
1134 goto bad;
1135 resource_list_add(&dinfo->resources, type, *rid,
1136 rman_get_start(r), rman_get_end(r), count);
1137 rle = resource_list_find(&dinfo->resources, type, *rid);
1138 if (!rle)
1139 goto bad;
1140 rle->res = r;
1141 }
1142 /*
1143 * If dev doesn't own the device, then we can't give this device
1144 * out.
1145 */
1146 if (rman_get_device(rle->res) != dev)
1147 return (NULL);
1148 rman_set_device(rle->res, child);
1149 if (flags & RF_ACTIVE)
1150 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1151 return (rle->res);
1152bad:;
1153 device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1154 return (NULL);
1155}
1156
1157static int
1158pccard_release_resource(device_t dev, device_t child, int type, int rid,
1159 struct resource *r)
1160{
1161 struct pccard_ivar *dinfo;
1162 int passthrough = (device_get_parent(child) != dev);
1163 struct resource_list_entry *rle = 0;
1164
1165 if (passthrough)
1166 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1167 type, rid, r);
1168
1169 dinfo = device_get_ivars(child);
1170
1171 rle = resource_list_find(&dinfo->resources, type, rid);
1172
1173 if (!rle) {
1174 device_printf(dev, "Allocated resource not found, "
1175 "%d %x %lx %lx\n",
1176 type, rid, rman_get_start(r), rman_get_size(r));
1177 return ENOENT;
1178 }
1179 if (!rle->res) {
1180 device_printf(dev, "Allocated resource not recorded\n");
1181 return ENOENT;
1182 }
1183 /*
1184 * Deactivate the resource (since it is being released), and
1185 * assign it to the bus.
1186 */
1187 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1188 rman_set_device(rle->res, dev);
1189 return (0);
1190}
1191
1192static void
1193pccard_child_detached(device_t parent, device_t dev)
1194{
1195 struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1196 struct pccard_function *pf = ivar->pf;
1197
1198 pccard_function_disable(pf);
1199}
1200
1201static void
1202pccard_intr(void *arg)
1203{
1204 struct pccard_function *pf = (struct pccard_function*) arg;
1205 int reg;
1206 int doisr = 1;
1207
1208 /*
1209 * MFC cards know if they interrupted, so we have to ack the
1210 * interrupt and call the ISR. Non-MFC cards don't have these
1211 * bits, so they always get called. Many non-MFC cards have
1212 * this bit set always upon read, but some do not.
1213 *
1214 * We always ack the interrupt, even if there's no ISR
1215 * for the card. This is done on the theory that acking
1216 * the interrupt will pacify the card enough to keep an
1217 * interrupt storm from happening. Of course this won't
1218 * help in the non-MFC case.
1219 *
1220 * This has no impact for MPSAFEness of the client drivers.
1221 * We register this with whatever flags the intr_handler
1222 * was registered with. All these functions are MPSAFE.
1223 */
1224 if (pccard_mfc(pf->sc)) {
1225 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1226 if (reg & PCCARD_CCR_STATUS_INTR)
1227 pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1228 reg & ~PCCARD_CCR_STATUS_INTR);
1229 else
1230 doisr = 0;
1231 }
1232 if (pf->intr_handler != NULL && doisr)
1233 pf->intr_handler(pf->intr_handler_arg);
1234}
1235
1236static int
1237pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1238 int flags, driver_intr_t *intr, void *arg, void **cookiep)
1239{
1240 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1241 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1242 struct pccard_function *pf = ivar->pf;
1243 int err;
1244
1245 if (pf->intr_handler != NULL)
1246 panic("Only one interrupt handler per function allowed");
1247 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
1248 pf, cookiep);
1249 if (err != 0)
1250 return (err);
1251 pf->intr_handler = intr;
1252 pf->intr_handler_arg = arg;
1253 pf->intr_handler_cookie = *cookiep;
1254 if (pccard_mfc(sc)) {
1255 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1256 pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1257 PCCARD_CCR_OPTION_IREQ_ENABLE);
1258 }
1259 return (0);
1260}
1261
1262static int
1263pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1264 void *cookie)
1265{
1266 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1267 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1268 struct pccard_function *pf = ivar->pf;
1269 int ret;
1270
1271 if (pccard_mfc(sc)) {
1272 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1273 pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1274 ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1275 }
1276 ret = bus_generic_teardown_intr(dev, child, r, cookie);
1277 if (ret == 0) {
1278 pf->intr_handler = NULL;
1279 pf->intr_handler_arg = NULL;
1280 pf->intr_handler_cookie = NULL;
1281 }
1282
1283 return (ret);
1284}
1285
1286static int
1287pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1288 struct resource *r)
1289{
1290 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1291 struct pccard_function *pf = ivar->pf;
1292
1293 switch(type) {
1294 case SYS_RES_IOPORT:
1295 /*
1296 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1297 * card.
1298 */
1299 if (pccard_mfc(pf->sc))
1300 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1301 rman_get_size(r));
1302 break;
1303 default:
1304 break;
1305 }
1306 return (bus_generic_activate_resource(brdev, child, type, rid, r));
1307}
1308
1309static int
1310pccard_deactivate_resource(device_t brdev, device_t child, int type,
1311 int rid, struct resource *r)
1312{
1313 /* XXX undo pccard_activate_resource? XXX */
1314 return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1315}
1316
34
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>
43
44#include <sys/bus.h>
45#include <machine/bus.h>
46#include <sys/rman.h>
47#include <machine/resource.h>
48
49#include <net/ethernet.h>
50
51#include <dev/pccard/pccardreg.h>
52#include <dev/pccard/pccardvar.h>
53#include <dev/pccard/pccard_cis.h>
54
55#include "power_if.h"
56#include "card_if.h"
57
58#define PCCARDDEBUG
59
60/* sysctl vars */
61SYSCTL_NODE(_hw, OID_AUTO, pccard, CTLFLAG_RD, 0, "PCCARD parameters");
62
63int pccard_debug = 0;
64TUNABLE_INT("hw.pccard.debug", &pccard_debug);
65SYSCTL_INT(_hw_pccard, OID_AUTO, debug, CTLFLAG_RW,
66 &pccard_debug, 0,
67 "pccard debug");
68
69int pccard_cis_debug = 0;
70TUNABLE_INT("hw.pccard.cis_debug", &pccard_cis_debug);
71SYSCTL_INT(_hw_pccard, OID_AUTO, cis_debug, CTLFLAG_RW,
72 &pccard_cis_debug, 0, "pccard CIS debug");
73
74#ifdef PCCARDDEBUG
75#define DPRINTF(arg) if (pccard_debug) printf arg
76#define DEVPRINTF(arg) if (pccard_debug) device_printf arg
77#define PRVERBOSE(arg) printf arg
78#define DEVPRVERBOSE(arg) device_printf arg
79#else
80#define DPRINTF(arg)
81#define DEVPRINTF(arg)
82#define PRVERBOSE(arg) if (bootverbose) printf arg
83#define DEVPRVERBOSE(arg) if (bootverbose) device_printf arg
84#endif
85
86static int pccard_ccr_read(struct pccard_function *pf, int ccr);
87static void pccard_ccr_write(struct pccard_function *pf, int ccr, int val);
88static int pccard_attach_card(device_t dev);
89static int pccard_detach_card(device_t dev);
90static void pccard_function_init(struct pccard_function *pf);
91static void pccard_function_free(struct pccard_function *pf);
92static int pccard_function_enable(struct pccard_function *pf);
93static void pccard_function_disable(struct pccard_function *pf);
94static int pccard_compat_do_probe(device_t bus, device_t dev);
95static int pccard_compat_do_attach(device_t bus, device_t dev);
96static int pccard_add_children(device_t dev, int busno);
97static int pccard_probe(device_t dev);
98static int pccard_attach(device_t dev);
99static int pccard_detach(device_t dev);
100static void pccard_print_resources(struct resource_list *rl,
101 const char *name, int type, int count, const char *format);
102static int pccard_print_child(device_t dev, device_t child);
103static int pccard_set_resource(device_t dev, device_t child, int type,
104 int rid, u_long start, u_long count);
105static int pccard_get_resource(device_t dev, device_t child, int type,
106 int rid, u_long *startp, u_long *countp);
107static void pccard_delete_resource(device_t dev, device_t child, int type,
108 int rid);
109static int pccard_set_res_flags(device_t dev, device_t child, int type,
110 int rid, uint32_t flags);
111static int pccard_set_memory_offset(device_t dev, device_t child, int rid,
112 uint32_t offset, uint32_t *deltap);
113static void pccard_probe_nomatch(device_t cbdev, device_t child);
114static int pccard_read_ivar(device_t bus, device_t child, int which,
115 u_char *result);
116static void pccard_driver_added(device_t dev, driver_t *driver);
117static struct resource *pccard_alloc_resource(device_t dev,
118 device_t child, int type, int *rid, u_long start,
119 u_long end, u_long count, u_int flags);
120static int pccard_release_resource(device_t dev, device_t child, int type,
121 int rid, struct resource *r);
122static void pccard_child_detached(device_t parent, device_t dev);
123static void pccard_intr(void *arg);
124static int pccard_setup_intr(device_t dev, device_t child,
125 struct resource *irq, int flags, driver_intr_t *intr,
126 void *arg, void **cookiep);
127static int pccard_teardown_intr(device_t dev, device_t child,
128 struct resource *r, void *cookie);
129
130static const struct pccard_product *
131pccard_do_product_lookup(device_t bus, device_t dev,
132 const struct pccard_product *tab, size_t ent_size,
133 pccard_product_match_fn matchfn);
134
135
136static int
137pccard_ccr_read(struct pccard_function *pf, int ccr)
138{
139 return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
140 pf->pf_ccr_offset + ccr));
141}
142
143static void
144pccard_ccr_write(struct pccard_function *pf, int ccr, int val)
145{
146 if ((pf->ccr_mask) & (1 << (ccr / 2))) {
147 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
148 pf->pf_ccr_offset + ccr, val);
149 }
150}
151
152static int
153pccard_set_default_descr(device_t dev)
154{
155 const char *vendorstr, *prodstr;
156 uint32_t vendor, prod;
157 char *str;
158
159 if (pccard_get_vendor_str(dev, &vendorstr))
160 return (0);
161 if (pccard_get_product_str(dev, &prodstr))
162 return (0);
163 if (vendorstr != NULL && prodstr != NULL) {
164 str = malloc(strlen(vendorstr) + strlen(prodstr) + 2, M_DEVBUF,
165 M_WAITOK);
166 sprintf(str, "%s %s", vendorstr, prodstr);
167 device_set_desc_copy(dev, str);
168 free(str, M_DEVBUF);
169 } else {
170 if (pccard_get_vendor(dev, &vendor))
171 return (0);
172 if (pccard_get_product(dev, &prod))
173 return (0);
174 str = malloc(100, M_DEVBUF, M_WAITOK);
175 snprintf(str, 100, "vendor=0x%x product=0x%x", vendor, prod);
176 device_set_desc_copy(dev, str);
177 free(str, M_DEVBUF);
178 }
179 return (0);
180}
181
182static int
183pccard_attach_card(device_t dev)
184{
185 struct pccard_softc *sc = PCCARD_SOFTC(dev);
186 struct pccard_function *pf;
187 struct pccard_ivar *ivar;
188 device_t child;
189 int i;
190
191 /*
192 * this is here so that when socket_enable calls gettype, trt happens
193 */
194 STAILQ_INIT(&sc->card.pf_head);
195
196 DEVPRINTF((dev, "chip_socket_enable\n"));
197 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
198
199 DEVPRINTF((dev, "read_cis\n"));
200 pccard_read_cis(sc);
201
202 DEVPRINTF((dev, "check_cis_quirks\n"));
203 pccard_check_cis_quirks(dev);
204
205 /*
206 * bail now if the card has no functions, or if there was an error in
207 * the cis.
208 */
209
210 if (sc->card.error) {
211 device_printf(dev, "CARD ERROR!\n");
212 return (1);
213 }
214 if (STAILQ_EMPTY(&sc->card.pf_head)) {
215 device_printf(dev, "Card has no functions!\n");
216 return (1);
217 }
218
219 if (bootverbose || pccard_debug)
220 pccard_print_cis(dev);
221
222 DEVPRINTF((dev, "functions scanning\n"));
223 i = -1;
224 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
225 i++;
226 if (STAILQ_EMPTY(&pf->cfe_head)) {
227 device_printf(dev,
228 "Function %d has no config entries.!\n", i);
229 continue;
230 }
231 pf->sc = sc;
232 pf->cfe = NULL;
233 pf->dev = NULL;
234 }
235 DEVPRINTF((dev, "Card has %d functions. pccard_mfc is %d\n", i + 1,
236 pccard_mfc(sc)));
237
238 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
239 if (STAILQ_EMPTY(&pf->cfe_head))
240 continue;
241 /*
242 * In NetBSD, the drivers are responsible for activating
243 * each function of a card. I think that in FreeBSD we
244 * want to activate them enough for the usual bus_*_resource
245 * routines will do the right thing. This many mean a
246 * departure from the current NetBSD model.
247 *
248 * This seems to work well in practice for most cards.
249 * However, there are two cases that are problematic.
250 * If a driver wishes to pick and chose which config
251 * entry to use, then this method falls down. These
252 * are usually older cards. In addition, there are
253 * some cards that have multiple hardware units on the
254 * cards, but presents only one CIS chain. These cards
255 * are combination cards, but only one of these units
256 * can be on at a time.
257 */
258 ivar = malloc(sizeof(struct pccard_ivar), M_DEVBUF,
259 M_WAITOK | M_ZERO);
260 resource_list_init(&ivar->resources);
261 child = device_add_child(dev, NULL, -1);
262 device_set_ivars(child, ivar);
263 ivar->pf = pf;
264 pf->dev = child;
265 /*
266 * XXX We might want to move the next three lines into
267 * XXX the pccard interface layer. For the moment, this
268 * XXX is OK, but some drivers want to pick the config
269 * XXX entry to use as well as some address tweaks (mostly
270 * XXX due to bugs in decode logic that makes some
271 * XXX addresses illegal or broken).
272 */
273 pccard_function_init(pf);
274 if (sc->sc_enabled_count == 0)
275 POWER_ENABLE_SOCKET(device_get_parent(dev), dev);
276 if (pccard_function_enable(pf) == 0 &&
277 pccard_set_default_descr(child) == 0 &&
278 device_probe_and_attach(child) == 0) {
279 DEVPRINTF((sc->dev, "function %d CCR at %d "
280 "offset %x mask %x: "
281 "%x %x %x %x, %x %x %x %x, %x\n",
282 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
283 pf->ccr_mask, pccard_ccr_read(pf, 0x00),
284 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
285 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
286 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
287 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
288 } else {
289 if (pf->cfe != NULL)
290 pccard_function_disable(pf);
291 }
292 }
293 return (0);
294}
295
296static int
297pccard_detach_card(device_t dev)
298{
299 struct pccard_softc *sc = PCCARD_SOFTC(dev);
300 struct pccard_function *pf;
301 struct pccard_config_entry *cfe;
302 int state;
303
304 /*
305 * We are running on either the PCCARD socket's event thread
306 * or in user context detaching a device by user request.
307 */
308 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
309 if (pf->dev == NULL)
310 continue;
311 state = device_get_state(pf->dev);
312 if (state == DS_ATTACHED || state == DS_BUSY)
313 device_detach(pf->dev);
314 if (pf->cfe != NULL)
315 pccard_function_disable(pf);
316 pccard_function_free(pf);
317 device_delete_child(dev, pf->dev);
318 }
319 if (sc->sc_enabled_count == 0)
320 POWER_DISABLE_SOCKET(device_get_parent(dev), dev);
321
322 while (NULL != (pf = STAILQ_FIRST(&sc->card.pf_head))) {
323 while (NULL != (cfe = STAILQ_FIRST(&pf->cfe_head))) {
324 STAILQ_REMOVE_HEAD(&pf->cfe_head, cfe_list);
325 free(cfe, M_DEVBUF);
326 }
327 STAILQ_REMOVE_HEAD(&sc->card.pf_head, pf_list);
328 free(pf, M_DEVBUF);
329 }
330 return (0);
331}
332
333static const struct pccard_product *
334pccard_do_product_lookup(device_t bus, device_t dev,
335 const struct pccard_product *tab, size_t ent_size,
336 pccard_product_match_fn matchfn)
337{
338 const struct pccard_product *ent;
339 int matches;
340 uint32_t vendor;
341 uint32_t prod;
342 const char *vendorstr;
343 const char *prodstr;
344 const char *cis3str;
345 const char *cis4str;
346
347#ifdef DIAGNOSTIC
348 if (sizeof *ent > ent_size)
349 panic("pccard_product_lookup: bogus ent_size %jd",
350 (intmax_t) ent_size);
351#endif
352 if (pccard_get_vendor(dev, &vendor))
353 return (NULL);
354 if (pccard_get_product(dev, &prod))
355 return (NULL);
356 if (pccard_get_vendor_str(dev, &vendorstr))
357 return (NULL);
358 if (pccard_get_product_str(dev, &prodstr))
359 return (NULL);
360 if (pccard_get_cis3_str(dev, &cis3str))
361 return (NULL);
362 if (pccard_get_cis4_str(dev, &cis4str))
363 return (NULL);
364 for (ent = tab; ent->pp_vendor != 0; ent =
365 (const struct pccard_product *) ((const char *) ent + ent_size)) {
366 matches = 1;
367 if (ent->pp_vendor == PCCARD_VENDOR_ANY &&
368 ent->pp_product == PCCARD_PRODUCT_ANY &&
369 ent->pp_cis[0] == NULL &&
370 ent->pp_cis[1] == NULL) {
371 if (ent->pp_name)
372 device_printf(dev,
373 "Total wildcard entry ignored for %s\n",
374 ent->pp_name);
375 continue;
376 }
377 if (matches && ent->pp_vendor != PCCARD_VENDOR_ANY &&
378 vendor != ent->pp_vendor)
379 matches = 0;
380 if (matches && ent->pp_product != PCCARD_PRODUCT_ANY &&
381 prod != ent->pp_product)
382 matches = 0;
383 if (matches && ent->pp_cis[0] &&
384 (vendorstr == NULL ||
385 strcmp(ent->pp_cis[0], vendorstr) != 0))
386 matches = 0;
387 if (matches && ent->pp_cis[1] &&
388 (prodstr == NULL ||
389 strcmp(ent->pp_cis[1], prodstr) != 0))
390 matches = 0;
391 if (matches && ent->pp_cis[2] &&
392 (cis3str == NULL ||
393 strcmp(ent->pp_cis[2], cis3str) != 0))
394 matches = 0;
395 if (matches && ent->pp_cis[3] &&
396 (cis4str == NULL ||
397 strcmp(ent->pp_cis[3], cis4str) != 0))
398 matches = 0;
399 if (matchfn != NULL)
400 matches = (*matchfn)(dev, ent, matches);
401 if (matches)
402 return (ent);
403 }
404 return (NULL);
405}
406
407/*
408 * Initialize a PCCARD function. May be called as long as the function is
409 * disabled.
410 *
411 * Note: pccard_function_init should not keep resources allocated. It should
412 * only set them up ala isa pnp, set the values in the rl lists, and return.
413 * Any resource held after pccard_function_init is called is a bug. However,
414 * the bus routines to get the resources also assume that pccard_function_init
415 * does this, so they need to be fixed too.
416 */
417static void
418pccard_function_init(struct pccard_function *pf)
419{
420 struct pccard_config_entry *cfe;
421 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
422 struct resource_list *rl = &devi->resources;
423 struct resource_list_entry *rle;
424 struct resource *r = 0;
425 device_t bus;
426 u_long start, end, len;
427 int i, rid, spaces;
428
429 if (pf->pf_flags & PFF_ENABLED) {
430 printf("pccard_function_init: function is enabled");
431 return;
432 }
433 bus = device_get_parent(pf->dev);
434 /* Remember which configuration entry we are using. */
435 STAILQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
436 if (cfe->iftype != PCCARD_IFTYPE_IO)
437 continue;
438 spaces = 0;
439 for (i = 0; i < cfe->num_iospace; i++) {
440 start = cfe->iospace[i].start;
441 if (start)
442 end = start + cfe->iospace[i].length - 1;
443 else
444 end = ~0UL;
445 DEVPRINTF((bus, "I/O rid %d start %lx end %lx\n",
446 i, start, end));
447 rid = i;
448 len = cfe->iospace[i].length;
449 r = bus_alloc_resource(bus, SYS_RES_IOPORT, &rid,
450 start, end, len, rman_make_alignment_flags(len));
451 if (r == NULL)
452 goto not_this_one;
453 rle = resource_list_add(rl, SYS_RES_IOPORT,
454 rid, rman_get_start(r), rman_get_end(r),
455 cfe->iospace[i].length);
456 if (rle == NULL)
457 panic("Cannot add resource rid %d IOPORT", rid);
458 rle->res = r;
459 spaces++;
460 }
461 for (i = 0; i < cfe->num_memspace; i++) {
462 start = cfe->memspace[i].hostaddr;
463 if (start)
464 end = start + cfe->memspace[i].length - 1;
465 else
466 end = ~0UL;
467 DEVPRINTF((bus, "Memory rid %d start %lx end %lx\n",
468 i, start, end));
469 rid = i;
470 len = cfe->memspace[i].length;
471 r = bus_alloc_resource(bus, SYS_RES_MEMORY, &rid,
472 start, end, len, rman_make_alignment_flags(len));
473 if (r == NULL)
474 goto not_this_one;
475 rle = resource_list_add(rl, SYS_RES_MEMORY,
476 rid, rman_get_start(r), rman_get_end(r),
477 cfe->memspace[i].length);
478 if (rle == NULL)
479 panic("Cannot add resource rid %d MEM", rid);
480 rle->res = r;
481 spaces++;
482 }
483 if (spaces == 0) {
484 DEVPRINTF((bus, "Neither memory nor I/O mapped\n"));
485 goto not_this_one;
486 }
487 if (cfe->irqmask) {
488 rid = 0;
489 r = bus_alloc_resource_any(bus, SYS_RES_IRQ, &rid,
490 RF_SHAREABLE);
491 if (r == NULL)
492 goto not_this_one;
493 rle = resource_list_add(rl, SYS_RES_IRQ, rid,
494 rman_get_start(r), rman_get_end(r), 1);
495 if (rle == NULL)
496 panic("Cannot add resource rid %d IRQ", rid);
497 rle->res = r;
498 }
499 /* If we get to here, we've allocated all we need */
500 pf->cfe = cfe;
501 break;
502 not_this_one:;
503 DEVPRVERBOSE((bus, "Allocation failed for cfe %d\n",
504 cfe->number));
505 resource_list_purge(rl);
506 }
507}
508
509/*
510 * Free resources allocated by pccard_function_init(), May be called as long
511 * as the function is disabled.
512 *
513 * NOTE: This function should be unnecessary. pccard_function_init should
514 * never keep resources initialized.
515 */
516static void
517pccard_function_free(struct pccard_function *pf)
518{
519 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
520 struct resource_list_entry *rle;
521
522 if (pf->pf_flags & PFF_ENABLED) {
523 printf("pccard_function_init: function is enabled");
524 return;
525 }
526
527 STAILQ_FOREACH(rle, &devi->resources, link) {
528 if (rle->res) {
529 if (rman_get_device(rle->res) != pf->sc->dev)
530 device_printf(pf->sc->dev,
531 "function_free: Resource still owned by "
532 "child, oops. "
533 "(type=%d, rid=%d, addr=%lx)\n",
534 rle->type, rle->rid,
535 rman_get_start(rle->res));
536 BUS_RELEASE_RESOURCE(device_get_parent(pf->sc->dev),
537 pf->sc->dev, rle->type, rle->rid, rle->res);
538 rle->res = NULL;
539 }
540 }
541 resource_list_free(&devi->resources);
542}
543
544static void
545pccard_mfc_adjust_iobase(struct pccard_function *pf, bus_addr_t addr,
546 bus_addr_t offset, bus_size_t size)
547{
548 bus_size_t iosize, tmp;
549
550 if (addr != 0) {
551 if (pf->pf_mfc_iomax == 0) {
552 pf->pf_mfc_iobase = addr + offset;
553 pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
554 } else {
555 /* this makes the assumption that nothing overlaps */
556 if (pf->pf_mfc_iobase > addr + offset)
557 pf->pf_mfc_iobase = addr + offset;
558 if (pf->pf_mfc_iomax < addr + offset + size)
559 pf->pf_mfc_iomax = addr + offset + size;
560 }
561 }
562
563 tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
564 /* round up to nearest (2^n)-1 */
565 for (iosize = 1; iosize < tmp; iosize <<= 1)
566 ;
567 iosize--;
568
569 DEVPRINTF((pf->dev, "MFC: I/O base %#jx IOSIZE %#jx\n",
570 (uintmax_t)pf->pf_mfc_iobase, (uintmax_t)(iosize + 1)));
571 pccard_ccr_write(pf, PCCARD_CCR_IOBASE0,
572 pf->pf_mfc_iobase & 0xff);
573 pccard_ccr_write(pf, PCCARD_CCR_IOBASE1,
574 (pf->pf_mfc_iobase >> 8) & 0xff);
575 pccard_ccr_write(pf, PCCARD_CCR_IOBASE2, 0);
576 pccard_ccr_write(pf, PCCARD_CCR_IOBASE3, 0);
577 pccard_ccr_write(pf, PCCARD_CCR_IOSIZE, iosize);
578}
579
580/* Enable a PCCARD function */
581static int
582pccard_function_enable(struct pccard_function *pf)
583{
584 struct pccard_function *tmp;
585 int reg;
586 device_t dev = pf->sc->dev;
587
588 if (pf->cfe == NULL) {
589 DEVPRVERBOSE((dev, "No config entry could be allocated.\n"));
590 return (ENOMEM);
591 }
592
593 /*
594 * Increase the reference count on the socket, enabling power, if
595 * necessary.
596 */
597 pf->sc->sc_enabled_count++;
598
599 if (pf->pf_flags & PFF_ENABLED) {
600 /*
601 * Don't do anything if we're already enabled.
602 */
603 return (0);
604 }
605
606 /*
607 * it's possible for different functions' CCRs to be in the same
608 * underlying page. Check for that.
609 */
610 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
611 if ((tmp->pf_flags & PFF_ENABLED) &&
612 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
613 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
614 (tmp->ccr_base - tmp->pf_ccr_offset +
615 tmp->pf_ccr_realsize))) {
616 pf->pf_ccrt = tmp->pf_ccrt;
617 pf->pf_ccrh = tmp->pf_ccrh;
618 pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
619
620 /*
621 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
622 * tmp->ccr_base) + pf->ccr_base;
623 */
624 /* pf->pf_ccr_offset =
625 (tmp->pf_ccr_offset + pf->ccr_base) -
626 tmp->ccr_base; */
627 pf->pf_ccr_window = tmp->pf_ccr_window;
628 break;
629 }
630 }
631 if (tmp == NULL) {
632 pf->ccr_rid = 0;
633 pf->ccr_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
634 &pf->ccr_rid, 0, ~0, PCCARD_MEM_PAGE_SIZE, RF_ACTIVE);
635 if (!pf->ccr_res)
636 goto bad;
637 DEVPRINTF((dev, "ccr_res == %lx-%lx, base=%x\n",
638 rman_get_start(pf->ccr_res), rman_get_end(pf->ccr_res),
639 pf->ccr_base));
640 CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY,
641 pf->ccr_rid, PCCARD_A_MEM_ATTR);
642 CARD_SET_MEMORY_OFFSET(device_get_parent(dev), dev,
643 pf->ccr_rid, pf->ccr_base, &pf->pf_ccr_offset);
644 pf->pf_ccrt = rman_get_bustag(pf->ccr_res);
645 pf->pf_ccrh = rman_get_bushandle(pf->ccr_res);
646 pf->pf_ccr_realsize = 1;
647 }
648
649 reg = (pf->cfe->number & PCCARD_CCR_OPTION_CFINDEX);
650 reg |= PCCARD_CCR_OPTION_LEVIREQ;
651 if (pccard_mfc(pf->sc)) {
652 reg |= (PCCARD_CCR_OPTION_FUNC_ENABLE |
653 PCCARD_CCR_OPTION_ADDR_DECODE);
654 /* PCCARD_CCR_OPTION_IRQ_ENABLE set elsewhere as needed */
655 }
656 pccard_ccr_write(pf, PCCARD_CCR_OPTION, reg);
657
658 reg = 0;
659 if ((pf->cfe->flags & PCCARD_CFE_IO16) == 0)
660 reg |= PCCARD_CCR_STATUS_IOIS8;
661 if (pf->cfe->flags & PCCARD_CFE_AUDIO)
662 reg |= PCCARD_CCR_STATUS_AUDIO;
663 pccard_ccr_write(pf, PCCARD_CCR_STATUS, reg);
664
665 pccard_ccr_write(pf, PCCARD_CCR_SOCKETCOPY, 0);
666
667 if (pccard_mfc(pf->sc))
668 pccard_mfc_adjust_iobase(pf, 0, 0, 0);
669
670#ifdef PCCARDDEBUG
671 if (pccard_debug) {
672 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
673 device_printf(tmp->sc->dev,
674 "function %d CCR at %d offset %x: "
675 "%x %x %x %x, %x %x %x %x, %x\n",
676 tmp->number, tmp->pf_ccr_window,
677 tmp->pf_ccr_offset,
678 pccard_ccr_read(tmp, 0x00),
679 pccard_ccr_read(tmp, 0x02),
680 pccard_ccr_read(tmp, 0x04),
681 pccard_ccr_read(tmp, 0x06),
682 pccard_ccr_read(tmp, 0x0A),
683 pccard_ccr_read(tmp, 0x0C),
684 pccard_ccr_read(tmp, 0x0E),
685 pccard_ccr_read(tmp, 0x10),
686 pccard_ccr_read(tmp, 0x12));
687 }
688 }
689#endif
690 pf->pf_flags |= PFF_ENABLED;
691 return (0);
692
693 bad:
694 /*
695 * Decrement the reference count, and power down the socket, if
696 * necessary.
697 */
698 pf->sc->sc_enabled_count--;
699 DEVPRINTF((dev, "bad --enabled_count = %d\n", pf->sc->sc_enabled_count));
700
701 return (1);
702}
703
704/* Disable PCCARD function. */
705static void
706pccard_function_disable(struct pccard_function *pf)
707{
708 struct pccard_function *tmp;
709 device_t dev = pf->sc->dev;
710
711 if (pf->cfe == NULL)
712 panic("pccard_function_disable: function not initialized");
713
714 if ((pf->pf_flags & PFF_ENABLED) == 0) {
715 /*
716 * Don't do anything if we're already disabled.
717 */
718 return;
719 }
720
721 if (pf->intr_handler != NULL) {
722 struct pccard_ivar *devi = PCCARD_IVAR(pf->dev);
723 struct resource_list_entry *rle =
724 resource_list_find(&devi->resources, SYS_RES_IRQ, 0);
725 if (rle == NULL)
726 panic("Can't disable an interrupt with no IRQ res\n");
727 BUS_TEARDOWN_INTR(dev, pf->dev, rle->res,
728 pf->intr_handler_cookie);
729 }
730
731 /*
732 * it's possible for different functions' CCRs to be in the same
733 * underlying page. Check for that. Note we mark us as disabled
734 * first to avoid matching ourself.
735 */
736
737 pf->pf_flags &= ~PFF_ENABLED;
738 STAILQ_FOREACH(tmp, &pf->sc->card.pf_head, pf_list) {
739 if ((tmp->pf_flags & PFF_ENABLED) &&
740 (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
741 ((pf->ccr_base + PCCARD_CCR_SIZE) <=
742 (tmp->ccr_base - tmp->pf_ccr_offset +
743 tmp->pf_ccr_realsize)))
744 break;
745 }
746
747 /* Not used by anyone else; unmap the CCR. */
748 if (tmp == NULL) {
749 bus_release_resource(dev, SYS_RES_MEMORY, pf->ccr_rid,
750 pf->ccr_res);
751 pf->ccr_res = NULL;
752 }
753
754 /*
755 * Decrement the reference count, and power down the socket, if
756 * necessary.
757 */
758 pf->sc->sc_enabled_count--;
759}
760
761/*
762 * simulate the old "probe" routine. In the new world order, the driver
763 * needs to grab devices while in the old they were assigned to the device by
764 * the pccardd process. These symbols are exported to the upper layers.
765 */
766static int
767pccard_compat_do_probe(device_t bus, device_t dev)
768{
769 return (CARD_COMPAT_MATCH(dev));
770}
771
772static int
773pccard_compat_do_attach(device_t bus, device_t dev)
774{
775 int err;
776
777 err = CARD_COMPAT_PROBE(dev);
778 if (err <= 0)
779 err = CARD_COMPAT_ATTACH(dev);
780 return (err);
781}
782
783#define PCCARD_NPORT 2
784#define PCCARD_NMEM 5
785#define PCCARD_NIRQ 1
786#define PCCARD_NDRQ 0
787
788static int
789pccard_add_children(device_t dev, int busno)
790{
791 /* Call parent to scan for any current children */
792 return (0);
793}
794
795static int
796pccard_probe(device_t dev)
797{
798 device_set_desc(dev, "16-bit PCCard bus");
799 return (pccard_add_children(dev, device_get_unit(dev)));
800}
801
802static int
803pccard_attach(device_t dev)
804{
805 struct pccard_softc *sc = PCCARD_SOFTC(dev);
806
807 sc->dev = dev;
808 sc->sc_enabled_count = 0;
809 return (bus_generic_attach(dev));
810}
811
812static int
813pccard_detach(device_t dev)
814{
815 pccard_detach_card(dev);
816 return 0;
817}
818
819static int
820pccard_suspend(device_t self)
821{
822 pccard_detach_card(self);
823 return (0);
824}
825
826static
827int
828pccard_resume(device_t self)
829{
830 return (0);
831}
832
833static void
834pccard_print_resources(struct resource_list *rl, const char *name, int type,
835 int count, const char *format)
836{
837 struct resource_list_entry *rle;
838 int printed;
839 int i;
840
841 printed = 0;
842 for (i = 0; i < count; i++) {
843 rle = resource_list_find(rl, type, i);
844 if (rle != NULL) {
845 if (printed == 0)
846 printf(" %s ", name);
847 else if (printed > 0)
848 printf(",");
849 printed++;
850 printf(format, rle->start);
851 if (rle->count > 1) {
852 printf("-");
853 printf(format, rle->start + rle->count - 1);
854 }
855 } else if (i > 3) {
856 /* check the first few regardless */
857 break;
858 }
859 }
860}
861
862static int
863pccard_print_child(device_t dev, device_t child)
864{
865 struct pccard_ivar *devi = PCCARD_IVAR(child);
866 struct resource_list *rl = &devi->resources;
867 int retval = 0;
868
869 retval += bus_print_child_header(dev, child);
870 retval += printf(" at");
871
872 if (devi != NULL) {
873 pccard_print_resources(rl, "port", SYS_RES_IOPORT,
874 PCCARD_NPORT, "%#lx");
875 pccard_print_resources(rl, "iomem", SYS_RES_MEMORY,
876 PCCARD_NMEM, "%#lx");
877 pccard_print_resources(rl, "irq", SYS_RES_IRQ, PCCARD_NIRQ,
878 "%ld");
879 pccard_print_resources(rl, "drq", SYS_RES_DRQ, PCCARD_NDRQ,
880 "%ld");
881 retval += printf(" function %d config %d", devi->pf->number,
882 devi->pf->cfe->number);
883 }
884
885 retval += bus_print_child_footer(dev, child);
886
887 return (retval);
888}
889
890static int
891pccard_set_resource(device_t dev, device_t child, int type, int rid,
892 u_long start, u_long count)
893{
894 struct pccard_ivar *devi = PCCARD_IVAR(child);
895 struct resource_list *rl = &devi->resources;
896
897 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
898 && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
899 return (EINVAL);
900 if (rid < 0)
901 return (EINVAL);
902 if (type == SYS_RES_IOPORT && rid >= PCCARD_NPORT)
903 return (EINVAL);
904 if (type == SYS_RES_MEMORY && rid >= PCCARD_NMEM)
905 return (EINVAL);
906 if (type == SYS_RES_IRQ && rid >= PCCARD_NIRQ)
907 return (EINVAL);
908 if (type == SYS_RES_DRQ && rid >= PCCARD_NDRQ)
909 return (EINVAL);
910
911 resource_list_add(rl, type, rid, start, start + count - 1, count);
912 if (NULL != resource_list_alloc(rl, device_get_parent(dev), dev,
913 type, &rid, start, start + count - 1, count, 0))
914 return 0;
915 else
916 return ENOMEM;
917}
918
919static int
920pccard_get_resource(device_t dev, device_t child, int type, int rid,
921 u_long *startp, u_long *countp)
922{
923 struct pccard_ivar *devi = PCCARD_IVAR(child);
924 struct resource_list *rl = &devi->resources;
925 struct resource_list_entry *rle;
926
927 rle = resource_list_find(rl, type, rid);
928 if (rle == NULL)
929 return (ENOENT);
930
931 if (startp != NULL)
932 *startp = rle->start;
933 if (countp != NULL)
934 *countp = rle->count;
935
936 return (0);
937}
938
939static void
940pccard_delete_resource(device_t dev, device_t child, int type, int rid)
941{
942 struct pccard_ivar *devi = PCCARD_IVAR(child);
943 struct resource_list *rl = &devi->resources;
944 resource_list_delete(rl, type, rid);
945}
946
947static int
948pccard_set_res_flags(device_t dev, device_t child, int type, int rid,
949 uint32_t flags)
950{
951 return (CARD_SET_RES_FLAGS(device_get_parent(dev), child, type,
952 rid, flags));
953}
954
955static int
956pccard_set_memory_offset(device_t dev, device_t child, int rid,
957 uint32_t offset, uint32_t *deltap)
958
959{
960 return (CARD_SET_MEMORY_OFFSET(device_get_parent(dev), child, rid,
961 offset, deltap));
962}
963
964static void
965pccard_probe_nomatch(device_t bus, device_t child)
966{
967 struct pccard_ivar *devi = PCCARD_IVAR(child);
968 struct pccard_function *pf = devi->pf;
969 struct pccard_softc *sc = PCCARD_SOFTC(bus);
970 int i;
971
972 device_printf(bus, "<unknown card>");
973 printf(" (manufacturer=0x%04x, product=0x%04x, function_type=%d) "
974 "at function %d\n", sc->card.manufacturer, sc->card.product,
975 pf->function, pf->number);
976 device_printf(bus, " CIS info: ");
977 for (i = 0; sc->card.cis1_info[i] != NULL && i < 4; i++)
978 printf("%s%s", i > 0 ? ", " : "", sc->card.cis1_info[i]);
979 printf("\n");
980 return;
981}
982
983static int
984pccard_child_location_str(device_t bus, device_t child, char *buf,
985 size_t buflen)
986{
987 struct pccard_ivar *devi = PCCARD_IVAR(child);
988 struct pccard_function *pf = devi->pf;
989
990 snprintf(buf, buflen, "function=%d", pf->number);
991 return (0);
992}
993
994/* XXX Maybe this should be in subr_bus? */
995static void
996pccard_safe_quote(char *dst, const char *src, size_t len)
997{
998 char *walker = dst, *ep = dst + len - 1;
999
1000 if (len == 0)
1001 return;
1002 while (walker < ep)
1003 {
1004 if (*src == '"') {
1005 if (ep - walker < 2)
1006 break;
1007 *walker++ = '\\';
1008 }
1009 *walker++ = *src++;
1010 }
1011 *walker = '\0';
1012}
1013
1014static int
1015pccard_child_pnpinfo_str(device_t bus, device_t child, char *buf,
1016 size_t buflen)
1017{
1018 struct pccard_ivar *devi = PCCARD_IVAR(child);
1019 struct pccard_function *pf = devi->pf;
1020 struct pccard_softc *sc = PCCARD_SOFTC(bus);
1021 char cis0[128], cis1[128];
1022
1023 pccard_safe_quote(cis0, sc->card.cis1_info[0], sizeof(cis0));
1024 pccard_safe_quote(cis1, sc->card.cis1_info[1], sizeof(cis1));
1025 snprintf(buf, buflen, "manufacturer=0x%04x product=0x%04x "
1026 "cisvendor=\"%s\" cisproduct=\"%s\" function_type=%d",
1027 sc->card.manufacturer, sc->card.product, cis0, cis1, pf->function);
1028 return (0);
1029}
1030
1031static int
1032pccard_read_ivar(device_t bus, device_t child, int which, u_char *result)
1033{
1034 struct pccard_ivar *devi = PCCARD_IVAR(child);
1035 struct pccard_function *pf = devi->pf;
1036 struct pccard_softc *sc = PCCARD_SOFTC(bus);
1037
1038 if (!pf)
1039 panic("No pccard function pointer");
1040 switch (which) {
1041 default:
1042 return (EINVAL);
1043 case PCCARD_IVAR_ETHADDR:
1044 bcopy(pf->pf_funce_lan_nid, result, ETHER_ADDR_LEN);
1045 break;
1046 case PCCARD_IVAR_VENDOR:
1047 *(uint32_t *)result = sc->card.manufacturer;
1048 break;
1049 case PCCARD_IVAR_PRODUCT:
1050 *(uint32_t *)result = sc->card.product;
1051 break;
1052 case PCCARD_IVAR_PRODEXT:
1053 *(uint16_t *)result = sc->card.prodext;
1054 break;
1055 case PCCARD_IVAR_FUNCTION:
1056 *(uint32_t *)result = pf->function;
1057 break;
1058 case PCCARD_IVAR_FUNCTION_NUMBER:
1059 *(uint32_t *)result = pf->number;
1060 break;
1061 case PCCARD_IVAR_VENDOR_STR:
1062 *(const char **)result = sc->card.cis1_info[0];
1063 break;
1064 case PCCARD_IVAR_PRODUCT_STR:
1065 *(const char **)result = sc->card.cis1_info[1];
1066 break;
1067 case PCCARD_IVAR_CIS3_STR:
1068 *(const char **)result = sc->card.cis1_info[2];
1069 break;
1070 case PCCARD_IVAR_CIS4_STR:
1071 *(const char **)result = sc->card.cis1_info[3];
1072 break;
1073 }
1074 return (0);
1075}
1076
1077static void
1078pccard_driver_added(device_t dev, driver_t *driver)
1079{
1080 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1081 struct pccard_function *pf;
1082 device_t child;
1083
1084 STAILQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
1085 if (STAILQ_EMPTY(&pf->cfe_head))
1086 continue;
1087 child = pf->dev;
1088 if (device_get_state(child) != DS_NOTPRESENT)
1089 continue;
1090 if (pccard_function_enable(pf) == 0 &&
1091 device_probe_and_attach(child) == 0) {
1092 DEVPRINTF((sc->dev, "function %d CCR at %d "
1093 "offset %x: %x %x %x %x, %x %x %x %x, %x\n",
1094 pf->number, pf->pf_ccr_window, pf->pf_ccr_offset,
1095 pccard_ccr_read(pf, 0x00),
1096 pccard_ccr_read(pf, 0x02), pccard_ccr_read(pf, 0x04),
1097 pccard_ccr_read(pf, 0x06), pccard_ccr_read(pf, 0x0A),
1098 pccard_ccr_read(pf, 0x0C), pccard_ccr_read(pf, 0x0E),
1099 pccard_ccr_read(pf, 0x10), pccard_ccr_read(pf, 0x12)));
1100 } else {
1101 if (pf->cfe != NULL)
1102 pccard_function_disable(pf);
1103 }
1104 }
1105 return;
1106}
1107
1108static struct resource *
1109pccard_alloc_resource(device_t dev, device_t child, int type, int *rid,
1110 u_long start, u_long end, u_long count, u_int flags)
1111{
1112 struct pccard_ivar *dinfo;
1113 struct resource_list_entry *rle = 0;
1114 int passthrough = (device_get_parent(child) != dev);
1115 int isdefault = (start == 0 && end == ~0UL && count == 1);
1116 struct resource *r = NULL;
1117
1118 /* XXX I'm no longer sure this is right */
1119 if (passthrough) {
1120 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
1121 type, rid, start, end, count, flags));
1122 }
1123
1124 dinfo = device_get_ivars(child);
1125 rle = resource_list_find(&dinfo->resources, type, *rid);
1126
1127 if (rle == NULL && isdefault)
1128 return (NULL); /* no resource of that type/rid */
1129 if (rle == NULL || rle->res == NULL) {
1130 /* XXX Need to adjust flags */
1131 r = bus_alloc_resource(dev, type, rid, start, end,
1132 count, flags);
1133 if (r == NULL)
1134 goto bad;
1135 resource_list_add(&dinfo->resources, type, *rid,
1136 rman_get_start(r), rman_get_end(r), count);
1137 rle = resource_list_find(&dinfo->resources, type, *rid);
1138 if (!rle)
1139 goto bad;
1140 rle->res = r;
1141 }
1142 /*
1143 * If dev doesn't own the device, then we can't give this device
1144 * out.
1145 */
1146 if (rman_get_device(rle->res) != dev)
1147 return (NULL);
1148 rman_set_device(rle->res, child);
1149 if (flags & RF_ACTIVE)
1150 BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, rle->res);
1151 return (rle->res);
1152bad:;
1153 device_printf(dev, "WARNING: Resource not reserved by pccard\n");
1154 return (NULL);
1155}
1156
1157static int
1158pccard_release_resource(device_t dev, device_t child, int type, int rid,
1159 struct resource *r)
1160{
1161 struct pccard_ivar *dinfo;
1162 int passthrough = (device_get_parent(child) != dev);
1163 struct resource_list_entry *rle = 0;
1164
1165 if (passthrough)
1166 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
1167 type, rid, r);
1168
1169 dinfo = device_get_ivars(child);
1170
1171 rle = resource_list_find(&dinfo->resources, type, rid);
1172
1173 if (!rle) {
1174 device_printf(dev, "Allocated resource not found, "
1175 "%d %x %lx %lx\n",
1176 type, rid, rman_get_start(r), rman_get_size(r));
1177 return ENOENT;
1178 }
1179 if (!rle->res) {
1180 device_printf(dev, "Allocated resource not recorded\n");
1181 return ENOENT;
1182 }
1183 /*
1184 * Deactivate the resource (since it is being released), and
1185 * assign it to the bus.
1186 */
1187 BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, rle->res);
1188 rman_set_device(rle->res, dev);
1189 return (0);
1190}
1191
1192static void
1193pccard_child_detached(device_t parent, device_t dev)
1194{
1195 struct pccard_ivar *ivar = PCCARD_IVAR(dev);
1196 struct pccard_function *pf = ivar->pf;
1197
1198 pccard_function_disable(pf);
1199}
1200
1201static void
1202pccard_intr(void *arg)
1203{
1204 struct pccard_function *pf = (struct pccard_function*) arg;
1205 int reg;
1206 int doisr = 1;
1207
1208 /*
1209 * MFC cards know if they interrupted, so we have to ack the
1210 * interrupt and call the ISR. Non-MFC cards don't have these
1211 * bits, so they always get called. Many non-MFC cards have
1212 * this bit set always upon read, but some do not.
1213 *
1214 * We always ack the interrupt, even if there's no ISR
1215 * for the card. This is done on the theory that acking
1216 * the interrupt will pacify the card enough to keep an
1217 * interrupt storm from happening. Of course this won't
1218 * help in the non-MFC case.
1219 *
1220 * This has no impact for MPSAFEness of the client drivers.
1221 * We register this with whatever flags the intr_handler
1222 * was registered with. All these functions are MPSAFE.
1223 */
1224 if (pccard_mfc(pf->sc)) {
1225 reg = pccard_ccr_read(pf, PCCARD_CCR_STATUS);
1226 if (reg & PCCARD_CCR_STATUS_INTR)
1227 pccard_ccr_write(pf, PCCARD_CCR_STATUS,
1228 reg & ~PCCARD_CCR_STATUS_INTR);
1229 else
1230 doisr = 0;
1231 }
1232 if (pf->intr_handler != NULL && doisr)
1233 pf->intr_handler(pf->intr_handler_arg);
1234}
1235
1236static int
1237pccard_setup_intr(device_t dev, device_t child, struct resource *irq,
1238 int flags, driver_intr_t *intr, void *arg, void **cookiep)
1239{
1240 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1241 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1242 struct pccard_function *pf = ivar->pf;
1243 int err;
1244
1245 if (pf->intr_handler != NULL)
1246 panic("Only one interrupt handler per function allowed");
1247 err = bus_generic_setup_intr(dev, child, irq, flags, pccard_intr,
1248 pf, cookiep);
1249 if (err != 0)
1250 return (err);
1251 pf->intr_handler = intr;
1252 pf->intr_handler_arg = arg;
1253 pf->intr_handler_cookie = *cookiep;
1254 if (pccard_mfc(sc)) {
1255 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1256 pccard_ccr_read(pf, PCCARD_CCR_OPTION) |
1257 PCCARD_CCR_OPTION_IREQ_ENABLE);
1258 }
1259 return (0);
1260}
1261
1262static int
1263pccard_teardown_intr(device_t dev, device_t child, struct resource *r,
1264 void *cookie)
1265{
1266 struct pccard_softc *sc = PCCARD_SOFTC(dev);
1267 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1268 struct pccard_function *pf = ivar->pf;
1269 int ret;
1270
1271 if (pccard_mfc(sc)) {
1272 pccard_ccr_write(pf, PCCARD_CCR_OPTION,
1273 pccard_ccr_read(pf, PCCARD_CCR_OPTION) &
1274 ~PCCARD_CCR_OPTION_IREQ_ENABLE);
1275 }
1276 ret = bus_generic_teardown_intr(dev, child, r, cookie);
1277 if (ret == 0) {
1278 pf->intr_handler = NULL;
1279 pf->intr_handler_arg = NULL;
1280 pf->intr_handler_cookie = NULL;
1281 }
1282
1283 return (ret);
1284}
1285
1286static int
1287pccard_activate_resource(device_t brdev, device_t child, int type, int rid,
1288 struct resource *r)
1289{
1290 struct pccard_ivar *ivar = PCCARD_IVAR(child);
1291 struct pccard_function *pf = ivar->pf;
1292
1293 switch(type) {
1294 case SYS_RES_IOPORT:
1295 /*
1296 * We need to adjust IOBASE[01] and IOSIZE if we're an MFC
1297 * card.
1298 */
1299 if (pccard_mfc(pf->sc))
1300 pccard_mfc_adjust_iobase(pf, rman_get_start(r), 0,
1301 rman_get_size(r));
1302 break;
1303 default:
1304 break;
1305 }
1306 return (bus_generic_activate_resource(brdev, child, type, rid, r));
1307}
1308
1309static int
1310pccard_deactivate_resource(device_t brdev, device_t child, int type,
1311 int rid, struct resource *r)
1312{
1313 /* XXX undo pccard_activate_resource? XXX */
1314 return (bus_generic_deactivate_resource(brdev, child, type, rid, r));
1315}
1316
1317static int
1318pccard_attr_read_impl(device_t brdev, device_t child, uint32_t offset,
1319 uint8_t *val)
1320{
1321 struct pccard_ivar *devi = PCCARD_IVAR(child);
1322 struct pccard_function *pf = devi->pf;
1323
1324 /*
1325 * Optimization. Most of the time, devices want to access
1326 * the same page of the attribute memory that the CCR is in.
1327 * We take advantage of this fact here.
1328 */
1329 if (offset / PCCARD_MEM_PAGE_SIZE ==
1330 pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1331 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
1332 offset % PCCARD_MEM_PAGE_SIZE);
1333 else {
1334 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1335 &offset);
1336 *val = bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh, offset);
1337 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1338 &offset);
1339 }
1340 return 0;
1341}
1342
1343static int
1344pccard_attr_write_impl(device_t brdev, device_t child, uint32_t offset,
1345 uint8_t val)
1346{
1347 struct pccard_ivar *devi = PCCARD_IVAR(child);
1348 struct pccard_function *pf = devi->pf;
1349
1350 /*
1351 * Optimization. Most of the time, devices want to access
1352 * the same page of the attribute memory that the CCR is in.
1353 * We take advantage of this fact here.
1354 */
1355 if (offset / PCCARD_MEM_PAGE_SIZE ==
1356 pf->ccr_base / PCCARD_MEM_PAGE_SIZE)
1357 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
1358 offset % PCCARD_MEM_PAGE_SIZE, val);
1359 else {
1360 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, offset,
1361 &offset);
1362 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, offset, val);
1363 CARD_SET_MEMORY_OFFSET(brdev, child, pf->ccr_rid, pf->ccr_base,
1364 &offset);
1365 }
1366
1367 return 0;
1368}
1369
1370static int
1371pccard_ccr_read_impl(device_t brdev, device_t child, uint32_t offset,
1372 uint8_t *val)
1373{
1374 struct pccard_ivar *devi = PCCARD_IVAR(child);
1375
1376 *val = pccard_ccr_read(devi->pf, offset);
1377 device_printf(child, "ccr_read of %#x (%#x) is %#x\n", offset,
1378 devi->pf->pf_ccr_offset, *val);
1379 return 0;
1380}
1381
1382static int
1383pccard_ccr_write_impl(device_t brdev, device_t child, uint32_t offset,
1384 uint8_t val)
1385{
1386 struct pccard_ivar *devi = PCCARD_IVAR(child);
1387 struct pccard_function *pf = devi->pf;
1388
1389 /*
1390 * Can't use pccard_ccr_write since client drivers may access
1391 * registers not contained in the 'mask' if they are non-standard.
1392 */
1393 device_printf(child, "ccr_write of %#x to %#x (%#x)\n", val, offset,
1394 devi->pf->pf_ccr_offset);
1395 bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh, pf->pf_ccr_offset + offset,
1396 val);
1397 return 0;
1398}
1399
1400
1317static device_method_t pccard_methods[] = {
1318 /* Device interface */
1319 DEVMETHOD(device_probe, pccard_probe),
1320 DEVMETHOD(device_attach, pccard_attach),
1321 DEVMETHOD(device_detach, pccard_detach),
1322 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1323 DEVMETHOD(device_suspend, pccard_suspend),
1324 DEVMETHOD(device_resume, pccard_resume),
1325
1326 /* Bus interface */
1327 DEVMETHOD(bus_print_child, pccard_print_child),
1328 DEVMETHOD(bus_driver_added, pccard_driver_added),
1329 DEVMETHOD(bus_child_detached, pccard_child_detached),
1330 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource),
1331 DEVMETHOD(bus_release_resource, pccard_release_resource),
1332 DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1333 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1334 DEVMETHOD(bus_setup_intr, pccard_setup_intr),
1335 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr),
1336 DEVMETHOD(bus_set_resource, pccard_set_resource),
1337 DEVMETHOD(bus_get_resource, pccard_get_resource),
1338 DEVMETHOD(bus_delete_resource, pccard_delete_resource),
1339 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch),
1340 DEVMETHOD(bus_read_ivar, pccard_read_ivar),
1341 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1342 DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1343
1344 /* Card Interface */
1345 DEVMETHOD(card_set_res_flags, pccard_set_res_flags),
1346 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1347 DEVMETHOD(card_attach_card, pccard_attach_card),
1348 DEVMETHOD(card_detach_card, pccard_detach_card),
1401static device_method_t pccard_methods[] = {
1402 /* Device interface */
1403 DEVMETHOD(device_probe, pccard_probe),
1404 DEVMETHOD(device_attach, pccard_attach),
1405 DEVMETHOD(device_detach, pccard_detach),
1406 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1407 DEVMETHOD(device_suspend, pccard_suspend),
1408 DEVMETHOD(device_resume, pccard_resume),
1409
1410 /* Bus interface */
1411 DEVMETHOD(bus_print_child, pccard_print_child),
1412 DEVMETHOD(bus_driver_added, pccard_driver_added),
1413 DEVMETHOD(bus_child_detached, pccard_child_detached),
1414 DEVMETHOD(bus_alloc_resource, pccard_alloc_resource),
1415 DEVMETHOD(bus_release_resource, pccard_release_resource),
1416 DEVMETHOD(bus_activate_resource, pccard_activate_resource),
1417 DEVMETHOD(bus_deactivate_resource, pccard_deactivate_resource),
1418 DEVMETHOD(bus_setup_intr, pccard_setup_intr),
1419 DEVMETHOD(bus_teardown_intr, pccard_teardown_intr),
1420 DEVMETHOD(bus_set_resource, pccard_set_resource),
1421 DEVMETHOD(bus_get_resource, pccard_get_resource),
1422 DEVMETHOD(bus_delete_resource, pccard_delete_resource),
1423 DEVMETHOD(bus_probe_nomatch, pccard_probe_nomatch),
1424 DEVMETHOD(bus_read_ivar, pccard_read_ivar),
1425 DEVMETHOD(bus_child_pnpinfo_str, pccard_child_pnpinfo_str),
1426 DEVMETHOD(bus_child_location_str, pccard_child_location_str),
1427
1428 /* Card Interface */
1429 DEVMETHOD(card_set_res_flags, pccard_set_res_flags),
1430 DEVMETHOD(card_set_memory_offset, pccard_set_memory_offset),
1431 DEVMETHOD(card_attach_card, pccard_attach_card),
1432 DEVMETHOD(card_detach_card, pccard_detach_card),
1433 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1349 DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1350 DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1434 DEVMETHOD(card_compat_do_probe, pccard_compat_do_probe),
1435 DEVMETHOD(card_compat_do_attach, pccard_compat_do_attach),
1351 DEVMETHOD(card_do_product_lookup, pccard_do_product_lookup),
1352 DEVMETHOD(card_cis_scan, pccard_scan_cis),
1436 DEVMETHOD(card_cis_scan, pccard_scan_cis),
1437 DEVMETHOD(card_attr_read, pccard_attr_read_impl),
1438 DEVMETHOD(card_attr_write, pccard_attr_write_impl),
1439 DEVMETHOD(card_ccr_read, pccard_ccr_read_impl),
1440 DEVMETHOD(card_ccr_write, pccard_ccr_write_impl),
1353
1354 { 0, 0 }
1355};
1356
1357static driver_t pccard_driver = {
1358 "pccard",
1359 pccard_methods,
1360 sizeof(struct pccard_softc)
1361};
1362
1363devclass_t pccard_devclass;
1364
1365/* Maybe we need to have a slot device? */
1366DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1367DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1368MODULE_VERSION(pccard, 1);
1441
1442 { 0, 0 }
1443};
1444
1445static driver_t pccard_driver = {
1446 "pccard",
1447 pccard_methods,
1448 sizeof(struct pccard_softc)
1449};
1450
1451devclass_t pccard_devclass;
1452
1453/* Maybe we need to have a slot device? */
1454DRIVER_MODULE(pccard, pcic, pccard_driver, pccard_devclass, 0, 0);
1455DRIVER_MODULE(pccard, cbb, pccard_driver, pccard_devclass, 0, 0);
1456MODULE_VERSION(pccard, 1);