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