cardbus.c revision 1.4 1 /* $NetBSD: cardbus.c,v 1.4 1999/10/27 09:29:18 haya Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998 and 1999
5 * HAYAKAWA Koichi. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by HAYAKAWA Koichi.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "opt_cardbus.h"
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44
45 #include <machine/bus.h>
46
47 #include <dev/cardbus/cardbusvar.h>
48 #include <dev/cardbus/pccardcis.h>
49
50 #include <dev/pci/pcivar.h> /* XXX */
51 #include <dev/pci/pcireg.h> /* XXX */
52
53 #if defined CARDBUS_DEBUG
54 #define STATIC
55 #define DPRINTF(a) printf a
56 #define DDELAY(x) delay((x)*1000*1000)
57 #else
58 #define STATIC static
59 #define DPRINTF(a)
60 #endif
61
62
63
64 STATIC void cardbusattach __P((struct device *, struct device *, void *));
65 /* STATIC int cardbusprint __P((void *, const char *)); */
66 int cardbus_attach_card __P((struct cardbus_softc *));
67
68 #if !defined __BROKEN_INDIRECT_CONFIG
69 STATIC int cardbusmatch __P((struct device *, struct cfdata *, void *));
70 static int cardbussubmatch __P((struct device *, struct cfdata *, void *));
71 #else
72 STATIC int cardbusmatch __P((struct device *, void *, void *));
73 static int cardbussubmatch __P((struct device *, void *, void *));
74 #endif
75 static int cardbusprint __P((void *, const char *));
76
77 static int decode_tuples __P((u_int8_t *, int));
78
79
80 struct cfattach cardbus_ca = {
81 sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
82 };
83
84 #ifndef __NetBSD_Version__
85 struct cfdriver cardbus_cd = {
86 NULL, "cardbus", DV_DULL
87 };
88 #endif
89
90
91 STATIC int
92 #if defined __BROKEN_INDIRECT_CONFIG
93 cardbusmatch(parent, match, aux)
94 struct device *parent;
95 void *match;
96 void *aux;
97 #else
98 cardbusmatch(parent, cf, aux)
99 struct device *parent;
100 struct cfdata *cf;
101 void *aux;
102 #endif
103 {
104 #if defined __BROKEN_INDIRECT_CONFIG
105 struct cfdata *cf = match;
106 #endif
107 struct cbslot_attach_args *cba = aux;
108
109 if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
110 DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
111 cba->cba_busname, cf->cf_driver->cd_name));
112 return 0;
113 }
114
115 /* which function? */
116 if (cf->cbslotcf_func != CBSLOT_UNK_FUNC &&
117 cf->cbslotcf_func != cba->cba_function) {
118 DPRINTF(("pccardmatch: function differs %d <=> %d\n",
119 cf->cbslotcf_func, cba->cba_function));
120 return 0;
121 }
122
123 if (cba->cba_function < 0 || cba->cba_function > 255) {
124 return 0;
125 }
126
127 return 1;
128 }
129
130
131
132 STATIC void
133 cardbusattach(parent, self, aux)
134 struct device *parent;
135 struct device *self;
136 void *aux;
137 {
138 struct cardbus_softc *sc = (void *)self;
139 struct cbslot_attach_args *cba = aux;
140 int cdstatus;
141
142 sc->sc_bus = cba->cba_bus;
143 sc->sc_device = cba->cba_function;
144 sc->sc_intrline = cba->cba_intrline;
145 sc->sc_cacheline = cba->cba_cacheline;
146 sc->sc_lattimer = cba->cba_lattimer;
147
148 printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
149 printf(" cacheline 0x%x, lattimer 0x%x\n", sc->sc_cacheline,sc->sc_lattimer);
150
151 sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */
152 sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */
153 sc->sc_dmat = cba->cba_dmat; /* DMA tag */
154 sc->sc_cc = cba->cba_cc;
155 sc->sc_cf = cba->cba_cf;
156
157 #if rbus
158 sc->sc_rbus_iot = cba->cba_rbus_iot;
159 sc->sc_rbus_memt = cba->cba_rbus_memt;
160 #endif
161
162 sc->sc_funcs = NULL;
163
164 cdstatus = 0;
165 }
166
167
168
169
170 /*
171 * int cardbus_attach_card(struct cardbus_softc *sc)
172 *
173 * This function attaches the card on the slot: turns on power,
174 * reads and analyses tuple, sets consifuration index.
175 *
176 * This function returns the number of recognised device functions.
177 * If no functions are recognised, return 0.
178 */
179 int
180 cardbus_attach_card(sc)
181 struct cardbus_softc *sc;
182 {
183 cardbus_chipset_tag_t cc;
184 cardbus_function_tag_t cf;
185 int cdstatus;
186 cardbustag_t tag;
187 cardbusreg_t id, class, cis_ptr;
188 cardbusreg_t bhlc;
189 u_int8_t tuple[2048];
190 int function, nfunction;
191 struct cardbus_devfunc **previous_next = &(sc->sc_funcs);
192 struct device *csc;
193 int no_work_funcs = 0;
194 cardbus_devfunc_t ct;
195
196 cc = sc->sc_cc;
197 cf = sc->sc_cf;
198
199 DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
200
201 /* inspect initial voltage */
202 if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
203 DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit));
204 return 0;
205 }
206
207 if (cdstatus & CARDBUS_3V_CARD) {
208 cf->cardbus_power(cc, CARDBUS_VCC_3V);
209 sc->sc_poweron_func = 1; /* function 0 on */
210 }
211
212 (cf->cardbus_ctrl)(cc, CARDBUS_RESET);
213
214 function = 0;
215
216 tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
217
218 /*
219 * Wait until power comes up. Maxmum 500 ms.
220 */
221 {
222 int i;
223 for (i = 0; i < 5; ++i) {
224 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
225 if (id != 0xffffffff && id != 0) {
226 break;
227 }
228 delay(100*1000); /* or tsleep */
229 }
230 if (i == 5) {
231 return 0;
232 }
233 }
234
235 bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
236 if (CARDBUS_LATTIMER(bhlc) < 0x10) {
237 bhlc &= (CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
238 bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
239 cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
240 }
241
242 nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
243
244 /*
245 * XXX multi-function card
246 *
247 * I don't know how to process CIS information for
248 * multi-function cards.
249 */
250
251 id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
252 class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
253 cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
254
255 DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
256 CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
257
258 bzero(tuple, 2048);
259
260 if (CARDBUS_CIS_ASI_TUPLE == (CARDBUS_CIS_ASI(cis_ptr))) {
261 /* Tuple is in Cardbus config space */
262 int i = cis_ptr & CARDBUS_CIS_ADDRMASK;
263 int j = 0;
264
265 for (; i < 0xff; i += 4) {
266 u_int32_t e = (cf->cardbus_conf_read)(cc, tag, i);
267 tuple[j] = 0xff & e;
268 e >>= 8;
269 tuple[j + 1] = 0xff & e;
270 e >>= 8;
271 tuple[j + 2] = 0xff & e;
272 e >>= 8;
273 tuple[j + 3] = 0xff & e;
274 j += 4;
275 }
276 } else if (CARDBUS_CIS_ASI(cis_ptr) <= CARDBUS_CIS_ASI_BAR5) {
277 /* int bar = CARDBUS_CIS_ASI_BAR(cis_ptr);*/
278 }
279
280
281 decode_tuples(tuple, 2048);
282
283 {
284 struct cardbus_attach_args ca;
285
286 if (NULL == (ct = (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
287 M_DEVBUF, M_NOWAIT))) {
288 panic("no room for cardbus_tag");
289 }
290
291 ct->ct_cc = sc->sc_cc;
292 ct->ct_cf = sc->sc_cf;
293 ct->ct_bus = sc->sc_bus;
294 ct->ct_dev = sc->sc_device;
295 ct->ct_func = function;
296 ct->ct_sc = sc;
297 ct->ct_next = NULL;
298 *previous_next = ct;
299
300 ca.ca_unit = sc->sc_dev.dv_unit;
301 ca.ca_ct = ct;
302
303 ca.ca_iot = sc->sc_iot;
304 ca.ca_memt = sc->sc_memt;
305 ca.ca_dmat = sc->sc_dmat;
306
307 ca.ca_tag = tag;
308 ca.ca_device = sc->sc_device;
309 ca.ca_function = function;
310 ca.ca_id = id;
311 ca.ca_class = class;
312
313 ca.ca_intrline = sc->sc_intrline;
314
315 if (NULL == (csc = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch))) {
316 /* do not match */
317 cf->cardbus_power(cc, CARDBUS_VCC_0V);
318 sc->sc_poweron_func = 0; /* no functions on */
319 free(cc, M_DEVBUF);
320 } else {
321 /* found */
322 previous_next = &(ct->ct_next);
323 ct->ct_device = csc;
324 ++no_work_funcs;
325 }
326 }
327
328 return no_work_funcs;
329 }
330
331
332 static int
333 #ifdef __BROKEN_INDIRECT_CONFIG
334 cardbussubmatch(parent, match, aux)
335 #else
336 cardbussubmatch(parent, cf, aux)
337 #endif
338 struct device *parent;
339 #ifdef __BROKEN_INDIRECT_CONFIG
340 void *match;
341 #else
342 struct cfdata *cf;
343 #endif
344 void *aux;
345 {
346 #ifdef __BROKEN_INDIRECT_CONFIG
347 struct cfdata *cf = match;
348 #endif
349 struct cardbus_attach_args *ca = aux;
350
351 if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
352 cf->cardbuscf_dev != ca->ca_unit) {
353 return 0;
354 }
355 if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
356 cf->cardbuscf_function != ca->ca_function) {
357 return 0;
358 }
359
360 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
361 }
362
363
364
365 static int
366 cardbusprint(aux, pnp)
367 void *aux;
368 const char *pnp;
369 {
370 register struct cardbus_attach_args *ca = aux;
371 char devinfo[256];
372
373 if (pnp) {
374 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo);
375 printf("%s at %s", devinfo, pnp);
376 }
377 printf(" dev %d function %d", ca->ca_device, ca->ca_function);
378
379 return UNCONF;
380 }
381
382
383
384
385
386
387 /*
388 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
389 * Interrupt handler of pccard.
390 * args:
391 * cardbus_chipset_tag_t *cc
392 * int irq:
393 */
394 void *
395 cardbus_intr_establish(cc, cf, irq, level, func, arg)
396 cardbus_chipset_tag_t cc;
397 cardbus_function_tag_t cf;
398 cardbus_intr_handle_t irq;
399 int level;
400 int (*func) __P((void *));
401 void *arg;
402 {
403 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
404
405 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg);
406 }
407
408
409
410 /*
411 * void cardbus_intr_disestablish(cc, cf, handler)
412 * Interrupt handler of pccard.
413 * args:
414 * cardbus_chipset_tag_t *cc
415 */
416 void
417 cardbus_intr_disestablish(cc, cf, handler)
418 cardbus_chipset_tag_t cc;
419 cardbus_function_tag_t cf;
420 void *handler;
421 {
422 DPRINTF(("- pccard_intr_disestablish\n"));
423
424 (*cf->cardbus_intr_disestablish)(cc, handler);
425 return;
426 }
427
428
429
430 /*
431 * int cardbus_function_enable(cardbus_devfunc_t ct)
432 *
433 * This function enables a function on a card. When no power is
434 * applied on the card, power will be applied on it.
435 */
436 int
437 cardbus_function_enable(ct)
438 cardbus_devfunc_t ct;
439 {
440 struct cardbus_softc *sc = ct->ct_sc;
441 int func = ct->ct_func;
442 cardbus_chipset_tag_t cc = sc->sc_cc;
443 cardbus_function_tag_t cf = sc->sc_cf;
444 cardbusreg_t command;
445 cardbustag_t tag;
446
447 DPRINTF(("entering cardbus_function_enable... "));
448
449 /* entering critical area */
450
451 if (sc->sc_poweron_func == 0) {
452 /* no functions are enabled */
453 (*cf->cardbus_power)(cc, CARDBUS_VCC_3V); /* XXX: sc_vold should be used */
454 (*cf->cardbus_ctrl)(cc, CARDBUS_RESET);
455 }
456
457 sc->sc_poweron_func |= (1 << func);
458
459 /* exiting critical area */
460
461 tag = Cardbus_make_tag(ct);
462 command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
463 command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
464 cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
465 Cardbus_free_tag(ct, tag);
466
467
468 DPRINTF(("%x\n", sc->sc_poweron_func));
469
470 return 0;
471 }
472
473
474 /*
475 * int cardbus_function_disable(cardbus_devfunc_t ct)
476 *
477 * This function disable a function on a card. When no functions are
478 * enabled, it turns off the power.
479 */
480 int
481 cardbus_function_disable(ct)
482 cardbus_devfunc_t ct;
483 {
484 struct cardbus_softc *sc = ct->ct_sc;
485 int func = ct->ct_func;
486 cardbus_chipset_tag_t cc = sc->sc_cc;
487 cardbus_function_tag_t cf = sc->sc_cf;
488
489 DPRINTF(("entering cardbus_enable_disable... "));
490
491 sc->sc_poweron_func &= ~(1 << func);
492
493 DPRINTF(("%x\n", sc->sc_poweron_func));
494
495 if (sc->sc_poweron_func == 0) {
496 /* power-off because no functions are enabled */
497 (*cf->cardbus_power)(cc, CARDBUS_VCC_0V);
498 }
499
500 return 0;
501 }
502
503
504
505
506
507
508
509 /*
510 * below this line, there are some functions for decoding tuples.
511 * They should go out from this file.
512 */
513
514 static u_int8_t *decode_tuple __P((u_int8_t *));
515 #ifdef CARDBUS_DEBUG
516 static char *tuple_name __P((int));
517 #endif
518
519 static int
520 decode_tuples(tuple, buflen)
521 u_int8_t *tuple;
522 int buflen;
523 {
524 u_int8_t *tp = tuple;
525
526 if (CISTPL_LINKTARGET != *tuple) {
527 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
528 return 0;
529 }
530
531 while (NULL != (tp = decode_tuple(tp))) {
532 if (tuple + buflen < tp) {
533 break;
534 }
535 }
536
537 return 1;
538 }
539
540
541 static u_int8_t *
542 decode_tuple(tuple)
543 u_int8_t *tuple;
544 {
545 u_int8_t type;
546 u_int8_t len;
547 #ifdef CARDBUS_DEBUG
548 int i;
549 #endif
550
551 type = tuple[0];
552 len = tuple[1] + 2;
553
554 #ifdef CARDBUS_DEBUG
555 printf("tuple: %s len %d\n", tuple_name(type), len);
556 #endif
557 if (CISTPL_END == type) {
558 return NULL;
559 }
560
561 #ifdef CARDBUS_DEBUG
562 for (i = 0; i < len; ++i) {
563 if (i % 16 == 0) {
564 printf(" 0x%2x:", i);
565 }
566 printf(" %x",tuple[i]);
567 if (i % 16 == 15) {
568 printf("\n");
569 }
570 }
571 if (i % 16 != 0) {
572 printf("\n");
573 }
574 #endif
575
576 return tuple + len;
577 }
578
579
580 #ifdef CARDBUS_DEBUG
581 static char *
582 tuple_name(type)
583 int type;
584 {
585 static char *tuple_name_s [] = {
586 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
587 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
588 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
589 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
590 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
591 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
592 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
593 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
594 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
595 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
596 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
597 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
598 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
599 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
600 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
601 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
602 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
603 "DATE", "BATTERY", "ORG"
604 };
605 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
606
607 if (type > 0 && type < NAME_LEN(tuple_name_s)) {
608 return tuple_name_s[type];
609 } else if (0xff == type) {
610 return "END";
611 } else {
612 return "Reserved";
613 }
614 }
615 #endif
616