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