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