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