cardbus.c revision 1.57 1 1.57 enami /* $NetBSD: cardbus.c,v 1.57 2004/10/10 22:00:36 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.57 enami __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.57 2004/10/10 22:00:36 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.57 enami if (p->image_size > len)
243 1.57 enami panic("image too large: "
244 1.57 enami "%d > %d",
245 1.57 enami p->image_size, len);
246 1.29 enami bus_space_read_region_1(p->romt,
247 1.29 enami p->romh, CARDBUS_CIS_ADDR(cis_ptr),
248 1.57 enami tuples, p->image_size);
249 1.29 enami found++;
250 1.29 enami }
251 1.29 enami break;
252 1.29 enami }
253 1.29 enami while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
254 1.41 lukem SIMPLEQ_REMOVE_HEAD(&rom_image, next);
255 1.29 enami free(p, M_DEVBUF);
256 1.29 enami }
257 1.29 enami out:
258 1.29 enami exrom = cardbus_conf_read(cc, cf, tag, reg);
259 1.29 enami cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
260 1.29 enami splx(save);
261 1.29 enami } else {
262 1.29 enami command = cardbus_conf_read(cc, cf, tag,
263 1.29 enami CARDBUS_COMMAND_STATUS_REG);
264 1.29 enami cardbus_conf_write(cc, cf, tag,
265 1.29 enami CARDBUS_COMMAND_STATUS_REG,
266 1.29 enami command | CARDBUS_COMMAND_MEM_ENABLE);
267 1.57 enami if (bar_size > len)
268 1.57 enami panic("bar_size too large: %d > %d",
269 1.57 enami (int)bar_size, len);
270 1.29 enami /* XXX byte order? */
271 1.29 enami bus_space_read_region_1(ca->ca_memt, bar_memh,
272 1.57 enami cis_ptr, tuples, bar_size);
273 1.29 enami found++;
274 1.7 joda }
275 1.29 enami command = cardbus_conf_read(cc, cf, tag,
276 1.29 enami CARDBUS_COMMAND_STATUS_REG);
277 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
278 1.29 enami command & ~CARDBUS_COMMAND_MEM_ENABLE);
279 1.29 enami cardbus_conf_write(cc, cf, tag, reg, 0);
280 1.30 enami
281 1.30 enami Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
282 1.30 enami bar_size);
283 1.29 enami break;
284 1.1 haya
285 1.7 joda #ifdef DIAGNOSTIC
286 1.29 enami default:
287 1.29 enami panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
288 1.29 enami cardbus_space);
289 1.7 joda #endif
290 1.29 enami }
291 1.29 enami return (!found);
292 1.7 joda }
293 1.1 haya
294 1.10 joda static void
295 1.10 joda parse_tuple(u_int8_t *tuple, int len, void *data)
296 1.10 joda {
297 1.29 enami struct cardbus_cis_info *cis = data;
298 1.29 enami char *p;
299 1.29 enami int i, bar_index;
300 1.29 enami
301 1.29 enami switch (tuple[0]) {
302 1.29 enami case PCMCIA_CISTPL_MANFID:
303 1.29 enami if (tuple[1] != 5) {
304 1.29 enami DPRINTF(("%s: wrong length manufacturer id (%d)\n",
305 1.40 enami __func__, tuple[1]));
306 1.29 enami break;
307 1.29 enami }
308 1.29 enami cis->manufacturer = tuple[2] | (tuple[3] << 8);
309 1.29 enami cis->product = tuple[4] | (tuple[5] << 8);
310 1.29 enami break;
311 1.29 enami
312 1.29 enami case PCMCIA_CISTPL_VERS_1:
313 1.29 enami memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
314 1.29 enami i = 0;
315 1.29 enami p = cis->cis1_info_buf + 2;
316 1.29 enami while (i <
317 1.29 enami sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
318 1.29 enami cis->cis1_info[i++] = p;
319 1.29 enami while (*p != '\0' && *p != '\xff')
320 1.29 enami p++;
321 1.29 enami if (*p == '\xff')
322 1.29 enami break;
323 1.29 enami p++;
324 1.29 enami }
325 1.10 joda break;
326 1.29 enami
327 1.29 enami case PCMCIA_CISTPL_BAR:
328 1.29 enami if (tuple[1] != 6) {
329 1.29 enami DPRINTF(("%s: BAR with short length (%d)\n",
330 1.40 enami __func__, tuple[1]));
331 1.29 enami break;
332 1.29 enami }
333 1.29 enami bar_index = tuple[2] & 7;
334 1.29 enami if (bar_index == 0) {
335 1.40 enami DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
336 1.29 enami break;
337 1.29 enami }
338 1.29 enami bar_index--;
339 1.29 enami cis->bar[bar_index].flags = tuple[2];
340 1.29 enami cis->bar[bar_index].size =
341 1.29 enami (tuple[4] << 0) |
342 1.29 enami (tuple[5] << 8) |
343 1.29 enami (tuple[6] << 16) |
344 1.29 enami (tuple[7] << 24);
345 1.29 enami break;
346 1.29 enami
347 1.29 enami case PCMCIA_CISTPL_FUNCID:
348 1.29 enami cis->funcid = tuple[2];
349 1.29 enami break;
350 1.29 enami
351 1.29 enami case PCMCIA_CISTPL_FUNCE:
352 1.29 enami switch (cis->funcid) {
353 1.29 enami case PCMCIA_FUNCTION_SERIAL:
354 1.29 enami if (tuple[1] >= 2 &&
355 1.29 enami /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
356 1.29 enami tuple[2] == 0) {
357 1.29 enami cis->funce.serial.uart_type = tuple[3] & 0x1f;
358 1.29 enami cis->funce.serial.uart_present = 1;
359 1.29 enami }
360 1.29 enami break;
361 1.29 enami
362 1.29 enami case PCMCIA_FUNCTION_NETWORK:
363 1.29 enami if (tuple[1] >= 8 &&
364 1.29 enami tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
365 1.29 enami if (tuple[3] >
366 1.29 enami sizeof(cis->funce.network.netid)) {
367 1.29 enami DPRINTF(("%s: unknown network id type "
368 1.29 enami "(len = %d)\n",
369 1.40 enami __func__, tuple[3]));
370 1.29 enami } else {
371 1.29 enami cis->funce.network.netid_present = 1;
372 1.29 enami memcpy(cis->funce.network.netid,
373 1.29 enami tuple + 4, tuple[3]);
374 1.29 enami }
375 1.29 enami }
376 1.29 enami break;
377 1.10 joda }
378 1.29 enami break;
379 1.10 joda }
380 1.10 joda }
381 1.10 joda
382 1.1 haya /*
383 1.1 haya * int cardbus_attach_card(struct cardbus_softc *sc)
384 1.1 haya *
385 1.1 haya * This function attaches the card on the slot: turns on power,
386 1.28 wiz * reads and analyses tuple, sets configuration index.
387 1.1 haya *
388 1.1 haya * This function returns the number of recognised device functions.
389 1.1 haya * If no functions are recognised, return 0.
390 1.1 haya */
391 1.1 haya int
392 1.29 enami cardbus_attach_card(struct cardbus_softc *sc)
393 1.1 haya {
394 1.29 enami cardbus_chipset_tag_t cc;
395 1.29 enami cardbus_function_tag_t cf;
396 1.52 drochner int cdstatus;
397 1.52 drochner static int wildcard[] = {
398 1.53 drochner CARDBUSCF_DEV_DEFAULT, CARDBUSCF_FUNCTION_DEFAULT
399 1.52 drochner };
400 1.52 drochner
401 1.52 drochner cc = sc->sc_cc;
402 1.52 drochner cf = sc->sc_cf;
403 1.52 drochner
404 1.52 drochner DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
405 1.52 drochner
406 1.52 drochner /* inspect initial voltage */
407 1.52 drochner if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
408 1.52 drochner DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
409 1.52 drochner sc->sc_dev.dv_unit));
410 1.52 drochner return (0);
411 1.52 drochner }
412 1.52 drochner
413 1.52 drochner cardbus_rescan(&sc->sc_dev, "cardbus", wildcard);
414 1.52 drochner return (1); /* XXX */
415 1.52 drochner }
416 1.52 drochner
417 1.52 drochner int
418 1.52 drochner cardbus_rescan(struct device *self, const char *ifattr, const int *locators)
419 1.52 drochner {
420 1.52 drochner struct cardbus_softc *sc = (struct cardbus_softc *)self;
421 1.52 drochner cardbus_chipset_tag_t cc;
422 1.52 drochner cardbus_function_tag_t cf;
423 1.29 enami cardbustag_t tag;
424 1.29 enami cardbusreg_t id, class, cis_ptr;
425 1.29 enami cardbusreg_t bhlc;
426 1.29 enami u_int8_t tuple[2048];
427 1.29 enami int cdstatus;
428 1.29 enami int function, nfunction;
429 1.29 enami struct device *csc;
430 1.29 enami cardbus_devfunc_t ct;
431 1.29 enami
432 1.29 enami cc = sc->sc_cc;
433 1.29 enami cf = sc->sc_cf;
434 1.29 enami
435 1.53 drochner /* XXX what a nonsense */
436 1.56 enami if (locators[CARDBUSCF_DEV] != CARDBUSCF_DEV_DEFAULT &&
437 1.56 enami locators[CARDBUSCF_DEV] != sc->sc_device)
438 1.53 drochner return (0);
439 1.53 drochner
440 1.29 enami /* inspect initial voltage */
441 1.29 enami if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
442 1.29 enami DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
443 1.29 enami sc->sc_dev.dv_unit));
444 1.29 enami return (0);
445 1.29 enami }
446 1.29 enami
447 1.29 enami /*
448 1.29 enami * XXX use fake function 8 to keep power on during whole
449 1.29 enami * configuration.
450 1.29 enami */
451 1.29 enami enable_function(sc, cdstatus, 8);
452 1.29 enami function = 0;
453 1.29 enami
454 1.29 enami tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, function);
455 1.29 enami
456 1.29 enami /*
457 1.29 enami * Wait until power comes up. Maxmum 500 ms.
458 1.29 enami */
459 1.29 enami {
460 1.29 enami int i;
461 1.29 enami
462 1.29 enami for (i = 0; i < 5; ++i) {
463 1.29 enami id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
464 1.29 enami if (id != 0xffffffff && id != 0) {
465 1.29 enami break;
466 1.29 enami }
467 1.29 enami if (cold) { /* before kernel thread invoked */
468 1.29 enami delay(100 * 1000);
469 1.29 enami } else { /* thread context */
470 1.29 enami if (tsleep((void *)sc, PCATCH, "cardbus",
471 1.29 enami hz / 10) != EWOULDBLOCK) {
472 1.29 enami break;
473 1.29 enami }
474 1.29 enami }
475 1.29 enami }
476 1.29 enami if (i == 5) {
477 1.52 drochner return (EIO);
478 1.29 enami }
479 1.29 enami }
480 1.29 enami
481 1.34 thorpej bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
482 1.34 thorpej DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
483 1.29 enami nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
484 1.29 enami
485 1.29 enami for (function = 0; function < nfunction; function++) {
486 1.29 enami struct cardbus_attach_args ca;
487 1.52 drochner int help[3];
488 1.52 drochner locdesc_t *ldesc = (void *)&help; /* XXX */
489 1.52 drochner
490 1.56 enami if (locators[CARDBUSCF_FUNCTION] !=
491 1.56 enami CARDBUSCF_FUNCTION_DEFAULT &&
492 1.56 enami locators[CARDBUSCF_FUNCTION] != function)
493 1.53 drochner continue;
494 1.53 drochner
495 1.54 drochner if (sc->sc_funcs[function])
496 1.52 drochner continue;
497 1.13 haya
498 1.29 enami tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device,
499 1.29 enami function);
500 1.1 haya
501 1.29 enami id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
502 1.29 enami class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
503 1.29 enami cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
504 1.1 haya
505 1.29 enami /* Invalid vendor ID value? */
506 1.51 mycroft if (CARDBUS_VENDOR(id) == PCI_VENDOR_INVALID) {
507 1.29 enami continue;
508 1.29 enami }
509 1.1 haya
510 1.29 enami DPRINTF(("cardbus_attach_card: "
511 1.29 enami "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
512 1.29 enami CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
513 1.29 enami
514 1.29 enami enable_function(sc, cdstatus, function);
515 1.29 enami
516 1.29 enami /* clean up every BAR */
517 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
518 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
519 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
520 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
521 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
522 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
523 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
524 1.32 haya
525 1.32 haya /* set initial latency and cacheline size */
526 1.32 haya bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
527 1.32 haya DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
528 1.32 haya function, bhlc));
529 1.32 haya bhlc &= ~((CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT) |
530 1.32 haya (CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT));
531 1.56 enami bhlc |= (sc->sc_cacheline & CARDBUS_CACHELINE_MASK) <<
532 1.56 enami CARDBUS_CACHELINE_SHIFT;
533 1.56 enami bhlc |= (sc->sc_lattimer & CARDBUS_LATTIMER_MASK) <<
534 1.56 enami CARDBUS_LATTIMER_SHIFT;
535 1.32 haya
536 1.32 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
537 1.32 haya bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
538 1.32 haya DPRINTF(("0x%08x\n", bhlc));
539 1.32 haya
540 1.32 haya if (CARDBUS_LATTIMER(bhlc) < 0x10) {
541 1.56 enami bhlc &= ~(CARDBUS_LATTIMER_MASK <<
542 1.56 enami CARDBUS_LATTIMER_SHIFT);
543 1.32 haya bhlc |= (0x10 << CARDBUS_LATTIMER_SHIFT);
544 1.56 enami cardbus_conf_write(cc, cf, tag,
545 1.56 enami CARDBUS_BHLC_REG, bhlc);
546 1.32 haya }
547 1.29 enami
548 1.29 enami /*
549 1.29 enami * We need to allocate the ct here, since we might
550 1.29 enami * need it when reading the CIS
551 1.29 enami */
552 1.29 enami if ((ct = malloc(sizeof(struct cardbus_devfunc),
553 1.29 enami M_DEVBUF, M_NOWAIT)) == NULL) {
554 1.29 enami panic("no room for cardbus_tag");
555 1.29 enami }
556 1.29 enami
557 1.29 enami ct->ct_cc = sc->sc_cc;
558 1.29 enami ct->ct_cf = sc->sc_cf;
559 1.29 enami ct->ct_bus = sc->sc_bus;
560 1.29 enami ct->ct_dev = sc->sc_device;
561 1.29 enami ct->ct_func = function;
562 1.29 enami ct->ct_sc = sc;
563 1.54 drochner sc->sc_funcs[function] = ct;
564 1.29 enami
565 1.29 enami memset(&ca, 0, sizeof(ca));
566 1.29 enami
567 1.29 enami ca.ca_ct = ct;
568 1.29 enami
569 1.29 enami ca.ca_iot = sc->sc_iot;
570 1.29 enami ca.ca_memt = sc->sc_memt;
571 1.29 enami ca.ca_dmat = sc->sc_dmat;
572 1.35 mcr
573 1.35 mcr #if rbus
574 1.35 mcr ca.ca_rbus_iot = sc->sc_rbus_iot;
575 1.35 mcr ca.ca_rbus_memt= sc->sc_rbus_memt;
576 1.35 mcr #endif
577 1.29 enami
578 1.29 enami ca.ca_tag = tag;
579 1.36 augustss ca.ca_bus = sc->sc_bus;
580 1.52 drochner ca.ca_device = sc->sc_device; /* always 0 */
581 1.29 enami ca.ca_function = function;
582 1.29 enami ca.ca_id = id;
583 1.29 enami ca.ca_class = class;
584 1.1 haya
585 1.29 enami ca.ca_intrline = sc->sc_intrline;
586 1.1 haya
587 1.50 mycroft if (cis_ptr != 0) {
588 1.57 enami if (cardbus_read_tuples(&ca, cis_ptr,
589 1.57 enami tuple, sizeof(tuple))) {
590 1.57 enami printf("cardbus_attach_card: "
591 1.57 enami "failed to read CIS\n");
592 1.50 mycroft } else {
593 1.29 enami #ifdef CARDBUS_DEBUG
594 1.57 enami decode_tuples(tuple, sizeof(tuple),
595 1.57 enami print_tuple, NULL);
596 1.29 enami #endif
597 1.57 enami decode_tuples(tuple, sizeof(tuple),
598 1.57 enami parse_tuple, &ca.ca_cis);
599 1.50 mycroft }
600 1.29 enami }
601 1.1 haya
602 1.52 drochner ldesc->len = 2;
603 1.52 drochner ldesc->locs[CARDBUSCF_DEV] = sc->sc_device; /* always 0 */
604 1.52 drochner ldesc->locs[CARDBUSCF_FUNCTION] = function;
605 1.52 drochner
606 1.52 drochner if ((csc = config_found_sm_loc((void *)sc, "cardbus", ldesc,
607 1.56 enami &ca, cardbusprint, cardbussubmatch)) == NULL) {
608 1.29 enami /* do not match */
609 1.29 enami disable_function(sc, function);
610 1.54 drochner sc->sc_funcs[function] = NULL;
611 1.29 enami free(ct, M_DEVBUF);
612 1.29 enami } else {
613 1.29 enami /* found */
614 1.29 enami ct->ct_device = csc;
615 1.29 enami }
616 1.29 enami }
617 1.29 enami /*
618 1.29 enami * XXX power down pseudo function 8 (this will power down the card
619 1.29 enami * if no functions were attached).
620 1.29 enami */
621 1.29 enami disable_function(sc, 8);
622 1.1 haya
623 1.52 drochner return (0);
624 1.1 haya }
625 1.1 haya
626 1.29 enami static int
627 1.52 drochner cardbussubmatch(struct device *parent, struct cfdata *cf,
628 1.52 drochner const locdesc_t *ldesc, void *aux)
629 1.29 enami {
630 1.1 haya
631 1.52 drochner /* ldesc->locs[CARDBUSCF_DEV] is always 0 */
632 1.53 drochner if (cf->cf_loc[CARDBUSCF_DEV] != CARDBUSCF_DEV_DEFAULT &&
633 1.53 drochner cf->cf_loc[CARDBUSCF_DEV] != ldesc->locs[CARDBUSCF_DEV]) {
634 1.29 enami return (0);
635 1.29 enami }
636 1.53 drochner if (cf->cf_loc[CARDBUSCF_FUNCTION] != CARDBUSCF_FUNCTION_DEFAULT &&
637 1.53 drochner cf->cf_loc[CARDBUSCF_FUNCTION] != ldesc->locs[CARDBUSCF_FUNCTION]) {
638 1.29 enami return (0);
639 1.29 enami }
640 1.1 haya
641 1.43 thorpej return (config_match(parent, cf, aux));
642 1.29 enami }
643 1.1 haya
644 1.29 enami static int
645 1.29 enami cardbusprint(void *aux, const char *pnp)
646 1.29 enami {
647 1.29 enami struct cardbus_attach_args *ca = aux;
648 1.29 enami char devinfo[256];
649 1.29 enami int i;
650 1.29 enami
651 1.29 enami if (pnp) {
652 1.48 itojun pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
653 1.48 itojun sizeof(devinfo));
654 1.29 enami for (i = 0; i < 4; i++) {
655 1.29 enami if (ca->ca_cis.cis1_info[i] == NULL)
656 1.29 enami break;
657 1.29 enami if (i)
658 1.47 thorpej aprint_normal(", ");
659 1.47 thorpej aprint_normal("%s", ca->ca_cis.cis1_info[i]);
660 1.29 enami }
661 1.47 thorpej aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
662 1.56 enami i ? " " : "",
663 1.56 enami ca->ca_cis.manufacturer, ca->ca_cis.product);
664 1.47 thorpej aprint_normal(" %s at %s", devinfo, pnp);
665 1.29 enami }
666 1.47 thorpej aprint_normal(" dev %d function %d", ca->ca_device, ca->ca_function);
667 1.1 haya
668 1.29 enami return (UNCONF);
669 1.29 enami }
670 1.1 haya
671 1.1 haya /*
672 1.18 haya * void cardbus_detach_card(struct cardbus_softc *sc)
673 1.18 haya *
674 1.18 haya * This function detaches the card on the slot: detach device data
675 1.18 haya * structure and turns off the power.
676 1.18 haya *
677 1.18 haya * This function must not be called under interrupt context.
678 1.18 haya */
679 1.18 haya void
680 1.29 enami cardbus_detach_card(struct cardbus_softc *sc)
681 1.18 haya {
682 1.54 drochner int f;
683 1.54 drochner struct cardbus_devfunc *ct;
684 1.18 haya
685 1.54 drochner for (f = 0; f < 8; f++) {
686 1.54 drochner ct = sc->sc_funcs[f];
687 1.54 drochner if (!ct)
688 1.54 drochner continue;
689 1.18 haya
690 1.29 enami DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
691 1.54 drochner ct->ct_device->dv_xname));
692 1.29 enami /* call device detach function */
693 1.18 haya
694 1.56 enami if (config_detach(ct->ct_device, 0) != 0) {
695 1.33 augustss printf("%s: cannot detach dev %s, function %d\n",
696 1.54 drochner sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
697 1.54 drochner ct->ct_func);
698 1.52 drochner }
699 1.52 drochner }
700 1.52 drochner
701 1.52 drochner sc->sc_poweron_func = 0;
702 1.52 drochner (*sc->sc_cf->cardbus_power)(sc->sc_cc,
703 1.52 drochner CARDBUS_VCC_0V | CARDBUS_VPP_0V);
704 1.52 drochner }
705 1.52 drochner
706 1.52 drochner void
707 1.52 drochner cardbus_childdetached(struct device *self, struct device *child)
708 1.52 drochner {
709 1.52 drochner struct cardbus_softc *sc = (struct cardbus_softc *)self;
710 1.54 drochner struct cardbus_devfunc *ct;
711 1.52 drochner
712 1.54 drochner ct = sc->sc_funcs[child->dv_locators[CARDBUSCF_FUNCTION]];
713 1.54 drochner KASSERT(ct->ct_device == child);
714 1.19 haya
715 1.54 drochner sc->sc_poweron_func &= ~(1 << ct->ct_func);
716 1.54 drochner sc->sc_funcs[ct->ct_func] = NULL;
717 1.54 drochner free(ct, M_DEVBUF);
718 1.18 haya }
719 1.18 haya
720 1.18 haya /*
721 1.1 haya * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
722 1.1 haya * Interrupt handler of pccard.
723 1.1 haya * args:
724 1.1 haya * cardbus_chipset_tag_t *cc
725 1.29 enami * int irq:
726 1.1 haya */
727 1.1 haya void *
728 1.29 enami cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
729 1.29 enami cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
730 1.1 haya {
731 1.1 haya
732 1.29 enami DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
733 1.29 enami return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
734 1.1 haya }
735 1.1 haya
736 1.1 haya /*
737 1.1 haya * void cardbus_intr_disestablish(cc, cf, handler)
738 1.1 haya * Interrupt handler of pccard.
739 1.1 haya * args:
740 1.1 haya * cardbus_chipset_tag_t *cc
741 1.1 haya */
742 1.1 haya void
743 1.29 enami cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
744 1.29 enami void *handler)
745 1.1 haya {
746 1.1 haya
747 1.29 enami DPRINTF(("- pccard_intr_disestablish\n"));
748 1.29 enami (*cf->cardbus_intr_disestablish)(cc, handler);
749 1.1 haya }
750 1.1 haya
751 1.29 enami /*
752 1.29 enami * XXX this should be merged with cardbus_function_{enable,disable},
753 1.29 enami * but we don't have a ct when these functions are called.
754 1.29 enami */
755 1.8 joda static void
756 1.29 enami enable_function(struct cardbus_softc *sc, int cdstatus, int function)
757 1.8 joda {
758 1.18 haya
759 1.29 enami if (sc->sc_poweron_func == 0) {
760 1.29 enami /* switch to 3V and/or wait for power to stabilize */
761 1.29 enami if (cdstatus & CARDBUS_3V_CARD) {
762 1.31 haya /*
763 1.31 haya * sc_poweron_func must be substituted before
764 1.31 haya * entering sleep, in order to avoid turn on
765 1.31 haya * power twice.
766 1.31 haya */
767 1.31 haya sc->sc_poweron_func |= (1 << function);
768 1.29 enami (*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
769 1.29 enami } else {
770 1.29 enami /* No cards other than 3.3V cards. */
771 1.29 enami return;
772 1.29 enami }
773 1.29 enami (*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
774 1.23 haya }
775 1.29 enami sc->sc_poweron_func |= (1 << function);
776 1.8 joda }
777 1.8 joda
778 1.8 joda static void
779 1.29 enami disable_function(struct cardbus_softc *sc, int function)
780 1.8 joda {
781 1.18 haya
782 1.29 enami sc->sc_poweron_func &= ~(1 << function);
783 1.29 enami if (sc->sc_poweron_func == 0) {
784 1.29 enami /* power-off because no functions are enabled */
785 1.29 enami (*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
786 1.29 enami }
787 1.8 joda }
788 1.8 joda
789 1.1 haya /*
790 1.9 haya * int cardbus_function_enable(struct cardbus_softc *sc, int func)
791 1.1 haya *
792 1.1 haya * This function enables a function on a card. When no power is
793 1.1 haya * applied on the card, power will be applied on it.
794 1.1 haya */
795 1.1 haya int
796 1.29 enami cardbus_function_enable(struct cardbus_softc *sc, int func)
797 1.1 haya {
798 1.29 enami cardbus_chipset_tag_t cc = sc->sc_cc;
799 1.29 enami cardbus_function_tag_t cf = sc->sc_cf;
800 1.29 enami cardbusreg_t command;
801 1.29 enami cardbustag_t tag;
802 1.1 haya
803 1.29 enami DPRINTF(("entering cardbus_function_enable... "));
804 1.1 haya
805 1.29 enami /* entering critical area */
806 1.1 haya
807 1.29 enami /* XXX: sc_vold should be used */
808 1.29 enami enable_function(sc, CARDBUS_3V_CARD, func);
809 1.1 haya
810 1.29 enami /* exiting critical area */
811 1.1 haya
812 1.29 enami tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
813 1.9 haya
814 1.29 enami command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
815 1.29 enami command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
816 1.29 enami CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
817 1.9 haya
818 1.29 enami cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
819 1.1 haya
820 1.29 enami cardbus_free_tag(cc, cf, tag);
821 1.1 haya
822 1.29 enami DPRINTF(("%x\n", sc->sc_poweron_func));
823 1.1 haya
824 1.29 enami return (0);
825 1.1 haya }
826 1.1 haya
827 1.1 haya /*
828 1.9 haya * int cardbus_function_disable(struct cardbus_softc *, int func)
829 1.1 haya *
830 1.1 haya * This function disable a function on a card. When no functions are
831 1.1 haya * enabled, it turns off the power.
832 1.1 haya */
833 1.1 haya int
834 1.29 enami cardbus_function_disable(struct cardbus_softc *sc, int func)
835 1.1 haya {
836 1.1 haya
837 1.29 enami DPRINTF(("entering cardbus_function_disable... "));
838 1.1 haya
839 1.29 enami disable_function(sc, func);
840 1.1 haya
841 1.29 enami return (0);
842 1.1 haya }
843 1.1 haya
844 1.16 thorpej /*
845 1.16 thorpej * int cardbus_get_capability(cardbus_chipset_tag_t cc,
846 1.16 thorpej * cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
847 1.16 thorpej * cardbusreg_t *value)
848 1.16 thorpej *
849 1.16 thorpej * Find the specified PCI capability.
850 1.16 thorpej */
851 1.16 thorpej int
852 1.29 enami cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
853 1.29 enami cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
854 1.16 thorpej {
855 1.16 thorpej cardbusreg_t reg;
856 1.16 thorpej unsigned int ofs;
857 1.16 thorpej
858 1.16 thorpej reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
859 1.16 thorpej if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
860 1.16 thorpej return (0);
861 1.16 thorpej
862 1.16 thorpej ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
863 1.16 thorpej PCI_CAPLISTPTR_REG));
864 1.16 thorpej while (ofs != 0) {
865 1.16 thorpej #ifdef DIAGNOSTIC
866 1.16 thorpej if ((ofs & 3) || (ofs < 0x40))
867 1.16 thorpej panic("cardbus_get_capability");
868 1.16 thorpej #endif
869 1.16 thorpej reg = cardbus_conf_read(cc, cf, tag, ofs);
870 1.16 thorpej if (PCI_CAPLIST_CAP(reg) == capid) {
871 1.16 thorpej if (offset)
872 1.16 thorpej *offset = ofs;
873 1.16 thorpej if (value)
874 1.16 thorpej *value = reg;
875 1.16 thorpej return (1);
876 1.16 thorpej }
877 1.16 thorpej ofs = PCI_CAPLIST_NEXT(reg);
878 1.16 thorpej }
879 1.1 haya
880 1.16 thorpej return (0);
881 1.16 thorpej }
882 1.1 haya
883 1.1 haya /*
884 1.1 haya * below this line, there are some functions for decoding tuples.
885 1.1 haya * They should go out from this file.
886 1.1 haya */
887 1.1 haya
888 1.10 joda static u_int8_t *
889 1.57 enami decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
890 1.1 haya
891 1.1 haya static int
892 1.29 enami decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
893 1.29 enami {
894 1.29 enami u_int8_t *tp = tuple;
895 1.29 enami
896 1.29 enami if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
897 1.29 enami DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
898 1.29 enami return (0);
899 1.29 enami }
900 1.29 enami
901 1.57 enami while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL)
902 1.57 enami ;
903 1.29 enami
904 1.29 enami return (1);
905 1.1 haya }
906 1.1 haya
907 1.1 haya static u_int8_t *
908 1.57 enami decode_tuple(u_int8_t *tuple, u_int8_t *end,
909 1.57 enami tuple_decode_func func, void *data)
910 1.1 haya {
911 1.29 enami u_int8_t type;
912 1.29 enami u_int8_t len;
913 1.1 haya
914 1.29 enami type = tuple[0];
915 1.29 enami len = tuple[1] + 2;
916 1.1 haya
917 1.57 enami if (tuple + len > end)
918 1.57 enami return (NULL);
919 1.57 enami
920 1.29 enami (*func)(tuple, len, data);
921 1.1 haya
922 1.57 enami if (type == PCMCIA_CISTPL_END)
923 1.29 enami return (NULL);
924 1.1 haya
925 1.29 enami return (tuple + len);
926 1.1 haya }
927 1.1 haya
928 1.49 christos /*
929 1.49 christos * XXX: this is another reason why this code should be shared with PCI.
930 1.49 christos */
931 1.49 christos int
932 1.49 christos cardbus_powerstate(cardbus_devfunc_t ct, pcitag_t tag, const int *newstate,
933 1.49 christos int *oldstate)
934 1.49 christos {
935 1.49 christos cardbus_chipset_tag_t cc = ct->ct_cc;
936 1.49 christos cardbus_function_tag_t cf = ct->ct_cf;
937 1.49 christos
938 1.49 christos int offset;
939 1.49 christos pcireg_t value, cap, now;
940 1.49 christos
941 1.49 christos if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset,
942 1.49 christos &value))
943 1.49 christos return EOPNOTSUPP;
944 1.49 christos
945 1.49 christos cap = value >> 16;
946 1.49 christos value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
947 1.49 christos now = value & PCI_PMCSR_STATE_MASK;
948 1.49 christos value &= ~PCI_PMCSR_STATE_MASK;
949 1.49 christos if (oldstate) {
950 1.49 christos switch (now) {
951 1.49 christos case PCI_PMCSR_STATE_D0:
952 1.49 christos *oldstate = PCI_PWR_D0;
953 1.49 christos break;
954 1.49 christos case PCI_PMCSR_STATE_D1:
955 1.49 christos *oldstate = PCI_PWR_D1;
956 1.49 christos break;
957 1.49 christos case PCI_PMCSR_STATE_D2:
958 1.49 christos *oldstate = PCI_PWR_D2;
959 1.49 christos break;
960 1.49 christos case PCI_PMCSR_STATE_D3:
961 1.49 christos *oldstate = PCI_PWR_D3;
962 1.49 christos break;
963 1.49 christos default:
964 1.49 christos return EINVAL;
965 1.49 christos }
966 1.49 christos }
967 1.49 christos if (newstate == NULL)
968 1.49 christos return 0;
969 1.49 christos switch (*newstate) {
970 1.49 christos case PCI_PWR_D0:
971 1.49 christos if (now == PCI_PMCSR_STATE_D0)
972 1.49 christos return 0;
973 1.49 christos value |= PCI_PMCSR_STATE_D0;
974 1.49 christos break;
975 1.49 christos case PCI_PWR_D1:
976 1.49 christos if (now == PCI_PMCSR_STATE_D1)
977 1.49 christos return 0;
978 1.49 christos if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3)
979 1.49 christos return EINVAL;
980 1.49 christos if (!(cap & PCI_PMCR_D1SUPP))
981 1.49 christos return EOPNOTSUPP;
982 1.49 christos value |= PCI_PMCSR_STATE_D1;
983 1.49 christos break;
984 1.49 christos case PCI_PWR_D2:
985 1.49 christos if (now == PCI_PMCSR_STATE_D2)
986 1.49 christos return 0;
987 1.49 christos if (now == PCI_PMCSR_STATE_D3)
988 1.49 christos return EINVAL;
989 1.49 christos if (!(cap & PCI_PMCR_D2SUPP))
990 1.49 christos return EOPNOTSUPP;
991 1.49 christos value |= PCI_PMCSR_STATE_D2;
992 1.49 christos break;
993 1.49 christos case PCI_PWR_D3:
994 1.49 christos if (now == PCI_PMCSR_STATE_D3)
995 1.49 christos return 0;
996 1.49 christos value |= PCI_PMCSR_STATE_D3;
997 1.49 christos break;
998 1.49 christos default:
999 1.49 christos return EINVAL;
1000 1.49 christos }
1001 1.49 christos cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value);
1002 1.49 christos DELAY(1000);
1003 1.49 christos
1004 1.49 christos return 0;
1005 1.49 christos }
1006 1.49 christos
1007 1.49 christos int
1008 1.49 christos cardbus_setpowerstate(const char *dvname, cardbus_devfunc_t ct, pcitag_t tag,
1009 1.49 christos int newpwr)
1010 1.49 christos {
1011 1.49 christos int oldpwr, error;
1012 1.49 christos
1013 1.49 christos if ((error = cardbus_powerstate(ct, tag, &newpwr, &oldpwr)) != 0)
1014 1.49 christos return error;
1015 1.49 christos
1016 1.49 christos if (oldpwr == newpwr)
1017 1.49 christos return 0;
1018 1.49 christos
1019 1.49 christos if (oldpwr > newpwr) {
1020 1.49 christos printf("%s: sleeping to power state D%d\n", dvname, oldpwr);
1021 1.49 christos return 0;
1022 1.49 christos }
1023 1.49 christos
1024 1.49 christos /* oldpwr < newpwr */
1025 1.49 christos switch (oldpwr) {
1026 1.49 christos case PCI_PWR_D3:
1027 1.49 christos /*
1028 1.49 christos * XXX: This is because none of the devices do
1029 1.49 christos * the necessary song and dance for now to wakeup
1030 1.49 christos * Once this
1031 1.49 christos */
1032 1.49 christos printf("%s: cannot wake up from power state D%d\n",
1033 1.49 christos dvname, oldpwr);
1034 1.49 christos return EINVAL;
1035 1.49 christos default:
1036 1.49 christos printf("%s: waking up from power state D%d\n",
1037 1.49 christos dvname, oldpwr);
1038 1.49 christos return 0;
1039 1.49 christos }
1040 1.49 christos }
1041 1.49 christos
1042 1.49 christos
1043 1.3 augustss #ifdef CARDBUS_DEBUG
1044 1.29 enami static const char *tuple_name(int);
1045 1.29 enami static const char *tuple_names[] = {
1046 1.29 enami "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
1047 1.29 enami "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
1048 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
1049 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
1050 1.29 enami "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
1051 1.29 enami "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
1052 1.29 enami "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
1053 1.29 enami "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
1054 1.29 enami "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
1055 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
1056 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
1057 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
1058 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
1059 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
1060 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
1061 1.29 enami "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
1062 1.29 enami "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
1063 1.29 enami "DATE", "BATTERY", "ORG"
1064 1.29 enami };
1065 1.29 enami #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
1066 1.10 joda
1067 1.29 enami static const char *
1068 1.29 enami tuple_name(int type)
1069 1.29 enami {
1070 1.1 haya
1071 1.29 enami if (0 <= type && type < NAME_LEN(tuple_names)) {
1072 1.29 enami return (tuple_names[type]);
1073 1.29 enami } else if (type == 0xff) {
1074 1.29 enami return ("END");
1075 1.29 enami } else {
1076 1.29 enami return ("Reserved");
1077 1.29 enami }
1078 1.1 haya }
1079 1.10 joda
1080 1.10 joda static void
1081 1.29 enami print_tuple(u_int8_t *tuple, int len, void *data)
1082 1.29 enami {
1083 1.29 enami int i;
1084 1.29 enami
1085 1.29 enami printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
1086 1.29 enami
1087 1.29 enami for (i = 0; i < len; ++i) {
1088 1.29 enami if (i % 16 == 0) {
1089 1.29 enami printf(" 0x%2x:", i);
1090 1.29 enami }
1091 1.29 enami printf(" %x", tuple[i]);
1092 1.29 enami if (i % 16 == 15) {
1093 1.29 enami printf("\n");
1094 1.29 enami }
1095 1.29 enami }
1096 1.29 enami if (i % 16 != 0) {
1097 1.29 enami printf("\n");
1098 1.29 enami }
1099 1.10 joda }
1100 1.3 augustss #endif
1101