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