1 /*	$OpenBSD: cardbus.c,v 1.20 2005/05/28 19:46:07 jsg Exp $ */
2 /*	$NetBSD: cardbus.c,v 1.24 2000/04/02 19:11:37 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1997, 1998, 1999 and 2000
6  *     HAYAKAWA Koichi.  All rights reserved.
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.
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 HAYAKAWA Koichi.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/syslog.h>
43 #include <sys/proc.h>
44 
45 #include <machine/bus.h>
46 
47 #include <dev/cardbus/cardbusvar.h>
48 #include <dev/pci/pcidevs.h>
49 
50 #include <dev/cardbus/cardbus_exrom.h>
51 
52 #include <dev/pci/pcivar.h>	/* XXX */
53 #include <dev/pci/pcireg.h>	/* XXX */
54 
55 #include <dev/pcmcia/pcmciareg.h>
56 
57 #if defined CARDBUS_DEBUG
58 #define STATIC
59 #define DPRINTF(a) printf a
60 #else
61 #define STATIC static
62 #define DPRINTF(a)
63 #endif
64 
65 STATIC void cardbusattach(struct device *, struct device *, void *);
66 /* STATIC int cardbusprint(void *, const char *); */
67 int cardbus_attach_card(struct cardbus_softc *);
68 
69 STATIC int cardbusmatch(struct device *, void *, void *);
70 static int cardbussubmatch(struct device *, void *, void *);
71 static int cardbusprint(void *, const char *);
72 
73 typedef void (*tuple_decode_func)(u_int8_t*, int, void *);
74 
75 static int decode_tuples(u_int8_t *, int, tuple_decode_func, void *);
76 #ifdef CARDBUS_DEBUG
77 static void print_tuple(u_int8_t*, int, void *);
78 #endif
79 
80 static int cardbus_read_tuples(struct cardbus_attach_args *,
81 				    cardbusreg_t, u_int8_t *, size_t);
82 
83 static void enable_function(struct cardbus_softc *, int, int);
84 static void disable_function(struct cardbus_softc *, int);
85 
86 
87 struct cfattach cardbus_ca = {
88 	sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
89 };
90 
91 #ifndef __NetBSD_Version__
92 struct cfdriver cardbus_cd = {
93 	NULL, "cardbus", DV_DULL
94 };
95 #endif
96 
97 
98 STATIC int
cardbusmatch(parent,match,aux)99 cardbusmatch(parent, match, aux)
100      struct device *parent;
101      void *match;
102      void *aux;
103 {
104   struct cfdata *cf = match;
105   struct cbslot_attach_args *cba = aux;
106 
107   if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
108     DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
109 	     cba->cba_busname, cf->cf_driver->cd_name));
110     return 0;
111   }
112 
113   return 1;
114 }
115 
116 
117 
118 STATIC void
cardbusattach(parent,self,aux)119 cardbusattach(parent, self, aux)
120      struct device *parent;
121      struct device *self;
122      void *aux;
123 {
124   struct cardbus_softc *sc = (void *)self;
125   struct cbslot_attach_args *cba = aux;
126   int cdstatus;
127 
128   sc->sc_bus = cba->cba_bus;
129   sc->sc_device = 0;
130   sc->sc_intrline = cba->cba_intrline;
131   sc->sc_cacheline = cba->cba_cacheline;
132   sc->sc_lattimer = cba->cba_lattimer;
133 
134   printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
135   printf(" cacheline 0x%x, lattimer 0x%x\n", sc->sc_cacheline,sc->sc_lattimer);
136 
137   sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
138   sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
139   sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
140   sc->sc_cc = cba->cba_cc;
141   sc->sc_cf = cba->cba_cf;
142 
143 #if rbus
144   sc->sc_rbus_iot = cba->cba_rbus_iot;
145   sc->sc_rbus_memt = cba->cba_rbus_memt;
146 #endif
147 
148   sc->sc_funcs = NULL;
149 
150   cdstatus = 0;
151 }
152 
153 static int
cardbus_read_tuples(ca,cis_ptr,tuples,len)154 cardbus_read_tuples(ca, cis_ptr, tuples, len)
155      struct cardbus_attach_args *ca;
156      cardbusreg_t cis_ptr;
157      u_int8_t *tuples;
158      size_t len;
159 {
160     struct cardbus_softc *sc = ca->ca_ct->ct_sc;
161     cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
162     cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
163     cardbustag_t tag = ca->ca_tag;
164     cardbusreg_t command;
165     int found = 0;
166 
167     int i, j;
168     int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
169     bus_space_tag_t bar_tag;
170     bus_space_handle_t bar_memh;
171     bus_size_t bar_size;
172     bus_addr_t bar_addr;
173 
174     int reg;
175 
176     memset(tuples, 0, len);
177 
178     cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
179 
180     switch(cardbus_space) {
181     case CARDBUS_CIS_ASI_TUPLE:
182 	DPRINTF(("%s: reading CIS data from configuration space\n",
183 		 sc->sc_dev.dv_xname));
184 	for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
185 	    u_int32_t e = (cf->cardbus_conf_read)(cc, tag, i);
186 	    tuples[j] = 0xff & e;
187 	    e >>= 8;
188 	    tuples[j + 1] = 0xff & e;
189 	    e >>= 8;
190 	    tuples[j + 2] = 0xff & e;
191 	    e >>= 8;
192 	    tuples[j + 3] = 0xff & e;
193 	    j += 4;
194 	}
195 	found++;
196 	break;
197 
198     case CARDBUS_CIS_ASI_BAR0:
199     case CARDBUS_CIS_ASI_BAR1:
200     case CARDBUS_CIS_ASI_BAR2:
201     case CARDBUS_CIS_ASI_BAR3:
202     case CARDBUS_CIS_ASI_BAR4:
203     case CARDBUS_CIS_ASI_BAR5:
204     case CARDBUS_CIS_ASI_ROM:
205 	if(cardbus_space == CARDBUS_CIS_ASI_ROM) {
206 	    reg = CARDBUS_ROM_REG;
207 	    DPRINTF(("%s: reading CIS data from ROM\n",
208 		     sc->sc_dev.dv_xname));
209 	} else {
210 	    reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
211 	    DPRINTF(("%s: reading CIS data from BAR%d\n",
212 		     sc->sc_dev.dv_xname, cardbus_space - 1));
213 	}
214 
215 	/* XXX zero register so mapreg_map doesn't get confused by old
216            contents */
217 	cardbus_conf_write(cc, cf, tag, reg, 0);
218 	if(Cardbus_mapreg_map(ca->ca_ct, reg,
219 			      CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
220 			      0,
221 			      &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
222 	    printf("%s: failed to map memory\n", sc->sc_dev.dv_xname);
223 	    return 1;
224 	}
225 
226 
227 	if(cardbus_space == CARDBUS_CIS_ASI_ROM) {
228 	    cardbusreg_t exrom;
229 	    int save;
230 	    struct cardbus_rom_image_head rom_image;
231 	    struct cardbus_rom_image *p;
232 
233 	    save = splhigh();
234 	    /* enable rom address decoder */
235 	    exrom = cardbus_conf_read(cc, cf, tag, reg);
236 	    cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
237 
238 	    command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
239 	    cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
240 			       command | CARDBUS_COMMAND_MEM_ENABLE);
241 
242 	    if(cardbus_read_exrom(ca->ca_memt, bar_memh, &rom_image))
243 		goto out;
244 
245 	    for(p = SIMPLEQ_FIRST(&rom_image);
246 		p;
247 		p = SIMPLEQ_NEXT(p, next)) {
248 		if(p->rom_image == CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
249 		    bus_space_read_region_1(p->romt, p->romh,
250 					    CARDBUS_CIS_ADDR(cis_ptr),
251 					    tuples, 256);
252 		    found++;
253 		}
254 		break;
255 	    }
256 	    while((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
257 		SIMPLEQ_REMOVE_HEAD(&rom_image, next);
258 		free(p, M_DEVBUF);
259 	    }
260 	out:
261 	    exrom = cardbus_conf_read(cc, cf, tag, reg);
262 	    cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
263 	    splx(save);
264 	} else {
265 	    command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
266 	    cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
267 			       command | CARDBUS_COMMAND_MEM_ENABLE);
268 	    /* XXX byte order? */
269 	    bus_space_read_region_1(ca->ca_memt, bar_memh,
270 				    cis_ptr, tuples, 256);
271 	    found++;
272 	}
273 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
274 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
275 			   command & ~CARDBUS_COMMAND_MEM_ENABLE);
276 	cardbus_conf_write(cc, cf, tag, reg, 0);
277 
278 	Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
279 	    bar_size);
280 	break;
281 
282 #ifdef DIAGNOSTIC
283     default:
284 	panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname, cardbus_space);
285 #endif
286     }
287     return !found;
288 }
289 
290 static void
parse_tuple(u_int8_t * tuple,int len,void * data)291 parse_tuple(u_int8_t *tuple, int len, void *data)
292 {
293 #ifdef CARDBUS_DEBUG
294     static const char func[] = "parse_tuple";
295 #endif
296     struct cardbus_cis_info *cis = data;
297     int bar_index;
298     int i;
299     char *p;
300     switch(tuple[0]) {
301     case PCMCIA_CISTPL_MANFID:
302 	if(tuple[1] != 5) {
303 	    DPRINTF(("%s: wrong length manufacturer id (%d)\n",
304 		     func, tuple[1]));
305 	    break;
306 	}
307 	cis->manufacturer = tuple[2] | (tuple[3] << 8);
308 	cis->product = tuple[4] | (tuple[5] << 8);
309 	break;
310     case PCMCIA_CISTPL_VERS_1:
311 	bcopy(tuple + 2, cis->cis1_info_buf, tuple[1]);
312 	i = 0;
313 	p = cis->cis1_info_buf + 2;
314 	while(i < sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
315 	    cis->cis1_info[i++] = p;
316 	    while(*p != '\0' && *p != '\xff')
317 		p++;
318 	    if(*p == '\xff')
319 		break;
320 	    p++;
321 	}
322 	break;
323     case PCMCIA_CISTPL_BAR:
324 	if(tuple[1] != 6) {
325 	    DPRINTF(("%s: BAR with short length (%d)\n", func, tuple[1]));
326 	    break;
327 	}
328 	bar_index = tuple[2] & 7;
329 	if(bar_index == 0) {
330 	    DPRINTF(("%s: invalid ASI in BAR tuple\n", func));
331 	    break;
332 	}
333 	bar_index--;
334 	cis->bar[bar_index].flags = tuple[2];
335 	cis->bar[bar_index].size = (tuple[4] << 0) |
336 				    (tuple[5] << 8) |
337 				    (tuple[6] << 16) |
338 				    (tuple[7] << 24);
339 	break;
340     case PCMCIA_CISTPL_FUNCID:
341 	cis->funcid = tuple[2];
342 	break;
343 
344     case PCMCIA_CISTPL_FUNCE:
345 	switch(cis->funcid) {
346 	case PCMCIA_FUNCTION_SERIAL:
347 	    if(tuple[1] >= 2 &&
348 	       tuple[2] == 0 /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */) {
349 		cis->funce.serial.uart_type = tuple[3] & 0x1f;
350 		cis->funce.serial.uart_present = 1;
351 	    }
352 	    break;
353 	case PCMCIA_FUNCTION_NETWORK:
354 	    if(tuple[1] >= 8 && tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
355 		if(tuple[3] > sizeof(cis->funce.network.netid)) {
356 		    DPRINTF(("%s: unknown network id type (len = %d)\n",
357 			     func, tuple[3]));
358 		} else {
359 		    cis->funce.network.netid_present = 1;
360 		    bcopy(tuple + 4, cis->funce.network.netid,
361 			   tuple[3]);
362 		}
363 	    }
364 	}
365 	break;
366     }
367 }
368 
369 /*
370  * int cardbus_attach_card(struct cardbus_softc *sc)
371  *
372  *    This function attaches the card on the slot: turns on power,
373  *    reads and analyses tuple, sets configuration index.
374  *
375  *    This function returns the number of recognised device functions.
376  *    If no functions are recognised, return 0.
377  */
378 int
cardbus_attach_card(sc)379 cardbus_attach_card(sc)
380      struct cardbus_softc *sc;
381 {
382   cardbus_chipset_tag_t cc;
383   cardbus_function_tag_t cf;
384   int cdstatus;
385   cardbustag_t tag;
386   cardbusreg_t id, class, cis_ptr;
387   cardbusreg_t bhlc;
388   u_int8_t tuple[2048];
389   int function, nfunction;
390   struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
391   struct device *csc;
392   int no_work_funcs = 0;
393   cardbus_devfunc_t ct;
394 
395   cc = sc->sc_cc;
396   cf = sc->sc_cf;
397 
398   DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
399 
400   /* inspect initial voltage */
401   if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
402     DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit));
403     return 0;
404   }
405 
406   enable_function(sc, cdstatus, 8); /* XXX use fake function 8 to
407 				       keep power on during whole
408 				       configuration */
409 
410   function = 0;
411 
412   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
413 
414   /*
415    * Wait until power comes up.  Maxmum 500 ms.
416    */
417   {
418     int i;
419     for (i = 0; i < 5; ++i) {
420       id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
421       if (id != 0xffffffff && id != 0) {
422 	break;
423       }
424       if (cold) {		/* before kernel thread invoked */
425 	delay(100*1000);
426       } else {			/* thread context */
427 	if (tsleep((void *)sc, PCATCH, "cardbus", hz/10) != EWOULDBLOCK) {
428 	  break;
429 	}
430       }
431     }
432     if (i == 5) {
433       return 0;
434     }
435   }
436 
437   bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
438   DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
439   nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
440 
441   for(function = 0; function < nfunction; function++) {
442     struct cardbus_attach_args ca;
443 
444     tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
445 
446     id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
447     class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
448     cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
449 
450     /* Invalid vendor ID value? */
451     if (CARDBUS_VENDOR(id) == PCI_VENDOR_INVALID) {
452       continue;
453     }
454 
455     DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
456 	     CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
457 
458     enable_function(sc, cdstatus, function);
459 
460     /* clean up every BAR */
461     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
462     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
463     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
464     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
465     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
466     cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
467     cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
468 
469     /* set initial latency and cacheline size */
470     bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
471     DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
472 	function, bhlc));
473     bhlc &= ~((CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT) |
474 	(CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT));
475     bhlc |= ((sc->sc_cacheline & CARDBUS_CACHELINE_MASK) << CARDBUS_CACHELINE_SHIFT);
476     bhlc |= ((sc->sc_lattimer & CARDBUS_LATTIMER_MASK) << CARDBUS_LATTIMER_SHIFT);
477 
478     cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
479     bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
480     DPRINTF(("0x%08x\n", bhlc));
481 
482     if (CARDBUS_LATTIMER(bhlc) < 0x10) {
483 	bhlc &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
484 	bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
485 	cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
486     }
487 
488     /*
489      * We need to allocate the ct here, since we might
490      * need it when reading the CIS
491      */
492     if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
493 						M_DEVBUF, M_NOWAIT))) {
494       panic("no room for cardbus_tag");
495     }
496 
497     ct->ct_cc = sc->sc_cc;
498     ct->ct_cf = sc->sc_cf;
499     ct->ct_bus = sc->sc_bus;
500     ct->ct_dev = sc->sc_device;
501     ct->ct_func = function;
502     ct->ct_sc = sc;
503     ct->ct_next = NULL;
504     *previous_next = ct;
505 
506     memset(&ca, 0, sizeof(ca));
507 
508     ca.ca_unit = sc->sc_dev.dv_unit;
509     ca.ca_ct = ct;
510 
511     ca.ca_iot = sc->sc_iot;
512     ca.ca_memt = sc->sc_memt;
513     ca.ca_dmat = sc->sc_dmat;
514 
515 #if rbus
516     ca.ca_rbus_iot = sc->sc_rbus_iot;
517     ca.ca_rbus_memt = sc->sc_rbus_memt;
518 #endif
519 
520     ca.ca_tag = tag;
521     ca.ca_bus = sc->sc_bus;
522     ca.ca_device = sc->sc_device;
523     ca.ca_function = function;
524     ca.ca_id = id;
525     ca.ca_class = class;
526 
527     ca.ca_intrline = sc->sc_intrline;
528 
529     if (cis_ptr != 0) {
530 	if(cardbus_read_tuples(&ca, cis_ptr, tuple, sizeof(tuple))) {
531 	   printf("cardbus_attach_card: failed to read CIS\n");
532 	} else {
533 #ifdef CARDBUS_DEBUG
534 	   decode_tuples(tuple, 2048, print_tuple, NULL);
535 #endif
536 	   decode_tuples(tuple, 2048, parse_tuple, &ca.ca_cis);
537 	}
538     }
539 
540     if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
541       /* do not match */
542       disable_function(sc, function);
543       free(ct, M_DEVBUF);
544       *previous_next = NULL;
545     } else {
546       /* found */
547       previous_next = &(ct->ct_next);
548       ct->ct_device = csc;
549       ++no_work_funcs;
550     }
551   }
552   /*
553    * XXX power down pseudo function 8 (this will power down the card
554    * if no functions were attached).
555    */
556   disable_function(sc, 8);
557 
558   return no_work_funcs;
559 }
560 
561 
562 static int
cardbussubmatch(parent,match,aux)563 cardbussubmatch(parent, match, aux)
564      struct device *parent;
565      void *match;
566      void *aux;
567 {
568   struct cfdata *cf = match;
569   struct cardbus_attach_args *ca = aux;
570 
571   if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
572       cf->cardbuscf_dev != ca->ca_unit) {
573     return 0;
574   }
575   if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
576       cf->cardbuscf_function != ca->ca_function) {
577     return 0;
578   }
579 
580   return ((*cf->cf_attach->ca_match)(parent, cf, aux));
581 }
582 
583 
584 
585 static int
cardbusprint(aux,pnp)586 cardbusprint(aux, pnp)
587      void *aux;
588      const char *pnp;
589 {
590     struct cardbus_attach_args *ca = aux;
591     char devinfo[256];
592     int i;
593     if (pnp) {
594 	pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo, sizeof devinfo);
595 	for (i = 0; i < 4; i++) {
596 	    if (ca->ca_cis.cis1_info[i] == NULL)
597 		break;
598 	    if (i)
599 		printf(", ");
600 	    printf("%s", ca->ca_cis.cis1_info[i]);
601 	}
602 	if (i)
603 	    printf(" ");
604 	if (ca->ca_cis.manufacturer)
605 		printf("(manufacturer 0x%x, product 0x%x) ",
606 		    ca->ca_cis.manufacturer,
607 		    ca->ca_cis.product);
608 	printf("%s at %s", devinfo, pnp);
609     }
610     printf(" dev %d function %d", ca->ca_device, ca->ca_function);
611 
612     if (!pnp) {
613 	pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo, sizeof devinfo);
614 	for (i = 0; i < 3 && ca->ca_cis.cis1_info[i]; i++)
615 		printf("%s%s", i ? ", " : " \"", ca->ca_cis.cis1_info[i]);
616 	if (ca->ca_cis.cis1_info[0])
617 		printf("\"");
618     }
619 
620     return UNCONF;
621 }
622 
623 
624 
625 
626 
627 
628 /*
629  * void cardbus_detach_card(struct cardbus_softc *sc)
630  *
631  *    This function detaches the card on the slot: detach device data
632  *    structure and turns off the power.
633  *
634  *    This function must not be called under interrupt context.
635  */
636 void
cardbus_detach_card(sc)637 cardbus_detach_card(sc)
638      struct cardbus_softc *sc;
639 {
640     struct cardbus_devfunc *ct, *ct_next, **prev_next;
641 
642     prev_next = &(sc->sc_funcs->ct_next);
643 
644     for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) {
645 	struct device *fndev = ct->ct_device;
646 	ct_next = ct->ct_next;
647 
648 	DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname, fndev->dv_xname));
649 	/* call device detach function */
650 
651 	if (0 != config_detach(fndev, 0)) {
652 	    printf("%s: cannot detach dev %s, function %d\n",
653 		   sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func);
654 	    prev_next = &(ct->ct_next);
655 	} else {
656 	    sc->sc_poweron_func &= ~(1 << ct->ct_func);
657 	    *prev_next = ct->ct_next;
658 	    free(ct, M_DEVBUF);
659 	}
660     }
661 
662     sc->sc_poweron_func = 0;
663     sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
664 }
665 
666 
667 
668 
669 /*
670  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
671  *   Interrupt handler of pccard.
672  *  args:
673  *   cardbus_chipset_tag_t *cc
674  *   int irq:
675  */
676 void *
cardbus_intr_establish(cc,cf,irq,level,func,arg)677 cardbus_intr_establish(cc, cf, irq, level, func, arg)
678      cardbus_chipset_tag_t cc;
679      cardbus_function_tag_t cf;
680      cardbus_intr_handle_t irq;
681      int level;
682      int (*func)(void *);
683      void *arg;
684 {
685   DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
686 
687   return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
688 }
689 
690 
691 
692 /*
693  * void cardbus_intr_disestablish(cc, cf, handler)
694  *   Interrupt handler of pccard.
695  *  args:
696  *   cardbus_chipset_tag_t *cc
697  */
698 void
cardbus_intr_disestablish(cc,cf,handler)699 cardbus_intr_disestablish(cc, cf, handler)
700      cardbus_chipset_tag_t cc;
701      cardbus_function_tag_t cf;
702      void *handler;
703 {
704   DPRINTF(("- pccard_intr_disestablish\n"));
705 
706  (*cf->cardbus_intr_disestablish)(cc, handler);
707   return;
708 }
709 
710 
711 
712 /* XXX this should be merged with cardbus_function_{enable,disable},
713    but we don't have a ct when these functions are called */
714 
715 static void
enable_function(sc,cdstatus,function)716 enable_function(sc, cdstatus, function)
717      struct cardbus_softc *sc;
718      int cdstatus;
719      int function;
720 {
721 
722     if (sc->sc_poweron_func == 0) {
723 	/* switch to 3V and/or wait for power to stabilize */
724 	if (cdstatus & CARDBUS_3V_CARD) {
725 	    sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
726 	} else {
727 	    /* No cards other than 3.3V cards. */
728 	    return;
729 	}
730 	(sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
731     }
732     sc->sc_poweron_func |= (1 << function);
733 }
734 
735 static void
disable_function(sc,function)736 disable_function(sc, function)
737      struct cardbus_softc *sc;
738      int function;
739 {
740 
741     sc->sc_poweron_func &= ~(1 << function);
742     if (sc->sc_poweron_func == 0) {
743 	/* power-off because no functions are enabled */
744 	sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
745     }
746 }
747 
748 /*
749  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
750  *
751  *   This function enables a function on a card.  When no power is
752  *  applied on the card, power will be applied on it.
753  */
754 int
cardbus_function_enable(sc,func)755 cardbus_function_enable(sc, func)
756      struct cardbus_softc *sc;
757      int func;
758 {
759   cardbus_chipset_tag_t cc = sc->sc_cc;
760   cardbus_function_tag_t cf = sc->sc_cf;
761   cardbusreg_t command;
762   cardbustag_t tag;
763 
764   DPRINTF(("entering cardbus_function_enable...  "));
765 
766   /* entering critical area */
767 
768   enable_function(sc, CARDBUS_3V_CARD, func); /* XXX: sc_vold should be used */
769 
770   /* exiting critical area */
771 
772   tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
773 
774   command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
775   command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
776 
777   cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
778 
779   cardbus_free_tag(cc, cf, tag);
780 
781   DPRINTF(("%x\n", sc->sc_poweron_func));
782 
783   return 0;
784 }
785 
786 
787 /*
788  * int cardbus_function_disable(struct cardbus_softc *, int func)
789  *
790  *   This function disable a function on a card.  When no functions are
791  *  enabled, it turns off the power.
792  */
793 int
cardbus_function_disable(sc,func)794 cardbus_function_disable(sc, func)
795      struct cardbus_softc *sc;
796      int func;
797 {
798 
799   DPRINTF(("entering cardbus_function_disable...  "));
800 
801   disable_function(sc, func);
802 
803   return 0;
804 }
805 
806 
807 /*
808  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
809  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
810  *	cardbusreg_t *value)
811  *
812  *	Find the specified PCI capability.
813  */
814 int
cardbus_get_capability(cc,cf,tag,capid,offset,value)815 cardbus_get_capability(cc, cf, tag, capid, offset, value)
816 	cardbus_chipset_tag_t cc;
817 	cardbus_function_tag_t cf;
818 	cardbustag_t tag;
819 	int capid;
820 	int *offset;
821 	cardbusreg_t *value;
822 {
823 	cardbusreg_t reg;
824 	unsigned int ofs;
825 
826 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
827 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
828 		return (0);
829 
830 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
831 	    PCI_CAPLISTPTR_REG));
832 	while (ofs != 0) {
833 #ifdef DIAGNOSTIC
834 		if ((ofs & 3) || (ofs < 0x40))
835 			panic("cardbus_get_capability 0x%x", ofs);
836 #endif
837 		reg = cardbus_conf_read(cc, cf, tag, ofs);
838 		if (PCI_CAPLIST_CAP(reg) == capid) {
839 			if (offset)
840 				*offset = ofs;
841 			if (value)
842 				*value = reg;
843 			return (1);
844 		}
845 		ofs = PCI_CAPLIST_NEXT(reg);
846 	}
847 
848 	return (0);
849 }
850 
851 int
cardbus_matchbyid(struct cardbus_attach_args * ca,const struct cardbus_matchid * ids,int nent)852 cardbus_matchbyid(struct cardbus_attach_args *ca, const struct cardbus_matchid *ids,
853     int nent)
854 {
855 	const struct cardbus_matchid *cm;
856 	int i;
857 
858 	for (i = 0, cm = ids; i < nent; i++, cm++)
859 		if (CARDBUS_VENDOR(ca->ca_id) == cm->cm_vid &&
860 		    CARDBUS_PRODUCT(ca->ca_id) == cm->cm_pid)
861 			return (1);
862 	return (0);
863 }
864 
865 /*
866  * below this line, there are some functions for decoding tuples.
867  * They should go out from this file.
868  */
869 
870 static u_int8_t *
871 decode_tuple(u_int8_t *tuple, tuple_decode_func func, void *data);
872 
873 static int
decode_tuples(tuple,buflen,func,data)874 decode_tuples(tuple, buflen, func, data)
875      u_int8_t *tuple;
876      int buflen;
877      tuple_decode_func func;
878      void *data;
879 {
880   u_int8_t *tp = tuple;
881 
882   if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
883     DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
884     return 0;
885   }
886 
887   while (NULL != (tp = decode_tuple(tp, func, data))) {
888     if (tuple + buflen < tp) {
889       break;
890     }
891   }
892 
893   return 1;
894 }
895 
896 
897 static u_int8_t *
decode_tuple(tuple,func,data)898 decode_tuple(tuple, func, data)
899      u_int8_t *tuple;
900      tuple_decode_func func;
901      void *data;
902 {
903     u_int8_t type;
904     u_int8_t len;
905 
906     type = tuple[0];
907     len = tuple[1] + 2;
908 
909     (*func)(tuple, len, data);
910 
911     if (PCMCIA_CISTPL_END == type) {
912 	return NULL;
913     }
914 
915     return tuple + len;
916 }
917 
918 
919 #ifdef CARDBUS_DEBUG
920 static char *tuple_name(int type);
921 
922 static char *
tuple_name(type)923 tuple_name(type)
924      int type;
925 {
926   static char *tuple_name_s [] = {
927     "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
928     "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
929     "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
930     "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
931     "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET",	/* 10-13 */
932     "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
933     "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
934     "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
935     "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
936     "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
937     "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
938     "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
939     "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
940     "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
941     "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
942     "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
943     "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
944     "DATE", "BATTERY", "ORG"
945   };
946 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
947 
948   if (type > 0 && type < NAME_LEN(tuple_name_s)) {
949     return tuple_name_s[type];
950   } else if (0xff == type) {
951     return "END";
952   } else {
953     return "Reserved";
954   }
955 }
956 
957 static void
print_tuple(tuple,len,data)958 print_tuple(tuple, len, data)
959      u_int8_t *tuple;
960      int len;
961      void *data;
962 {
963     int i;
964 
965     printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
966 
967     for (i = 0; i < len; ++i) {
968 	if (i % 16 == 0) {
969 	    printf("  0x%2x:", i);
970 	}
971 	printf(" %x",tuple[i]);
972 	if (i % 16 == 15) {
973 	    printf("\n");
974 	}
975     }
976     if (i % 16 != 0) {
977 	printf("\n");
978     }
979 }
980 
981 #endif
982