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