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