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