pcmcia.c revision 1.10 1 1.10 thorpej /* $NetBSD: pcmcia.c,v 1.10 1998/11/14 01:54:25 thorpej Exp $ */
2 1.2 thorpej
3 1.2 thorpej #define PCMCIADEBUG
4 1.2 thorpej
5 1.2 thorpej /*
6 1.2 thorpej * Copyright (c) 1997 Marc Horowitz. All rights reserved.
7 1.2 thorpej *
8 1.2 thorpej * Redistribution and use in source and binary forms, with or without
9 1.2 thorpej * modification, are permitted provided that the following conditions
10 1.2 thorpej * are met:
11 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.2 thorpej * notice, this list of conditions and the following disclaimer.
13 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
15 1.2 thorpej * documentation and/or other materials provided with the distribution.
16 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
17 1.2 thorpej * must display the following acknowledgement:
18 1.2 thorpej * This product includes software developed by Marc Horowitz.
19 1.2 thorpej * 4. The name of the author may not be used to endorse or promote products
20 1.2 thorpej * derived from this software without specific prior written permission.
21 1.2 thorpej *
22 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.2 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.2 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.2 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.2 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.2 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.2 thorpej */
33 1.2 thorpej
34 1.2 thorpej #include "opt_pcmciaverbose.h"
35 1.2 thorpej
36 1.2 thorpej #include <sys/types.h>
37 1.2 thorpej #include <sys/param.h>
38 1.2 thorpej #include <sys/systm.h>
39 1.2 thorpej #include <sys/device.h>
40 1.2 thorpej
41 1.2 thorpej /* XXX only needed for intr debugging */
42 1.2 thorpej #include <vm/vm.h>
43 1.2 thorpej
44 1.2 thorpej #include <dev/pcmcia/pcmciareg.h>
45 1.2 thorpej #include <dev/pcmcia/pcmciachip.h>
46 1.2 thorpej #include <dev/pcmcia/pcmciavar.h>
47 1.2 thorpej
48 1.3 enami #include "locators.h"
49 1.3 enami
50 1.2 thorpej #ifdef PCMCIADEBUG
51 1.2 thorpej int pcmcia_debug = 0;
52 1.2 thorpej #define DPRINTF(arg) if (pcmcia_debug) printf arg
53 1.2 thorpej #else
54 1.2 thorpej #define DPRINTF(arg)
55 1.2 thorpej #endif
56 1.2 thorpej
57 1.2 thorpej #ifdef PCMCIAVERBOSE
58 1.2 thorpej int pcmcia_verbose = 1;
59 1.2 thorpej #else
60 1.2 thorpej int pcmcia_verbose = 0;
61 1.2 thorpej #endif
62 1.2 thorpej
63 1.2 thorpej int pcmcia_match __P((struct device *, struct cfdata *, void *));
64 1.2 thorpej int pcmcia_submatch __P((struct device *, struct cfdata *, void *));
65 1.2 thorpej void pcmcia_attach __P((struct device *, struct device *, void *));
66 1.2 thorpej int pcmcia_print __P((void *, const char *));
67 1.2 thorpej
68 1.5 marc static inline void pcmcia_socket_enable __P((pcmcia_chipset_tag_t,
69 1.5 marc pcmcia_chipset_handle_t *));
70 1.5 marc static inline void pcmcia_socket_disable __P((pcmcia_chipset_tag_t,
71 1.5 marc pcmcia_chipset_handle_t *));
72 1.5 marc
73 1.5 marc int pcmcia_card_intr __P((void *));
74 1.2 thorpej
75 1.2 thorpej struct cfattach pcmcia_ca = {
76 1.2 thorpej sizeof(struct pcmcia_softc), pcmcia_match, pcmcia_attach
77 1.2 thorpej };
78 1.2 thorpej
79 1.2 thorpej int
80 1.2 thorpej pcmcia_ccr_read(pf, ccr)
81 1.2 thorpej struct pcmcia_function *pf;
82 1.2 thorpej int ccr;
83 1.2 thorpej {
84 1.2 thorpej
85 1.2 thorpej return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
86 1.2 thorpej pf->pf_ccr_offset + ccr));
87 1.2 thorpej }
88 1.2 thorpej
89 1.2 thorpej void
90 1.2 thorpej pcmcia_ccr_write(pf, ccr, val)
91 1.2 thorpej struct pcmcia_function *pf;
92 1.2 thorpej int ccr;
93 1.2 thorpej int val;
94 1.2 thorpej {
95 1.2 thorpej
96 1.2 thorpej if ((pf->ccr_mask) & (1 << (ccr / 2))) {
97 1.2 thorpej bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
98 1.2 thorpej pf->pf_ccr_offset + ccr, val);
99 1.2 thorpej }
100 1.2 thorpej }
101 1.2 thorpej
102 1.2 thorpej int
103 1.2 thorpej pcmcia_match(parent, match, aux)
104 1.2 thorpej struct device *parent;
105 1.2 thorpej struct cfdata *match;
106 1.2 thorpej void *aux;
107 1.2 thorpej {
108 1.2 thorpej /* if the autoconfiguration got this far, there's a socket here */
109 1.2 thorpej return (1);
110 1.2 thorpej }
111 1.2 thorpej
112 1.2 thorpej void
113 1.2 thorpej pcmcia_attach(parent, self, aux)
114 1.2 thorpej struct device *parent, *self;
115 1.2 thorpej void *aux;
116 1.2 thorpej {
117 1.2 thorpej struct pcmciabus_attach_args *paa = aux;
118 1.2 thorpej struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
119 1.2 thorpej
120 1.2 thorpej printf("\n");
121 1.2 thorpej
122 1.2 thorpej sc->pct = paa->pct;
123 1.2 thorpej sc->pch = paa->pch;
124 1.2 thorpej sc->iobase = paa->iobase;
125 1.2 thorpej sc->iosize = paa->iosize;
126 1.2 thorpej
127 1.2 thorpej sc->ih = NULL;
128 1.2 thorpej }
129 1.2 thorpej
130 1.2 thorpej int
131 1.2 thorpej pcmcia_card_attach(dev)
132 1.2 thorpej struct device *dev;
133 1.2 thorpej {
134 1.2 thorpej struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
135 1.2 thorpej struct pcmcia_function *pf;
136 1.2 thorpej struct pcmcia_attach_args paa;
137 1.2 thorpej int attached;
138 1.2 thorpej
139 1.2 thorpej /*
140 1.2 thorpej * this is here so that when socket_enable calls gettype, trt happens
141 1.2 thorpej */
142 1.2 thorpej sc->card.pf_head.sqh_first = NULL;
143 1.2 thorpej
144 1.2 thorpej pcmcia_chip_socket_enable(sc->pct, sc->pch);
145 1.2 thorpej
146 1.2 thorpej pcmcia_read_cis(sc);
147 1.2 thorpej
148 1.2 thorpej pcmcia_chip_socket_disable(sc->pct, sc->pch);
149 1.2 thorpej
150 1.2 thorpej /*
151 1.2 thorpej * bail now if the card has no functions, or if there was an error in
152 1.2 thorpej * the cis.
153 1.2 thorpej */
154 1.2 thorpej
155 1.2 thorpej if (sc->card.error)
156 1.2 thorpej return (1);
157 1.2 thorpej if (sc->card.pf_head.sqh_first == NULL)
158 1.2 thorpej return (1);
159 1.2 thorpej
160 1.2 thorpej if (pcmcia_verbose)
161 1.2 thorpej pcmcia_print_cis(sc);
162 1.2 thorpej
163 1.2 thorpej attached = 0;
164 1.2 thorpej
165 1.2 thorpej for (pf = sc->card.pf_head.sqh_first; pf != NULL;
166 1.2 thorpej pf = pf->pf_list.sqe_next) {
167 1.2 thorpej if (pf->cfe_head.sqh_first == NULL)
168 1.2 thorpej continue;
169 1.2 thorpej
170 1.2 thorpej pf->sc = sc;
171 1.2 thorpej pf->cfe = NULL;
172 1.2 thorpej pf->ih_fct = NULL;
173 1.2 thorpej pf->ih_arg = NULL;
174 1.2 thorpej }
175 1.2 thorpej
176 1.2 thorpej for (pf = sc->card.pf_head.sqh_first; pf != NULL;
177 1.2 thorpej pf = pf->pf_list.sqe_next) {
178 1.2 thorpej if (pf->cfe_head.sqh_first == NULL)
179 1.2 thorpej continue;
180 1.2 thorpej
181 1.2 thorpej paa.manufacturer = sc->card.manufacturer;
182 1.2 thorpej paa.product = sc->card.product;
183 1.2 thorpej paa.card = &sc->card;
184 1.2 thorpej paa.pf = pf;
185 1.2 thorpej
186 1.2 thorpej if (config_found_sm(&sc->dev, &paa, pcmcia_print,
187 1.2 thorpej pcmcia_submatch)) {
188 1.2 thorpej attached++;
189 1.2 thorpej
190 1.2 thorpej DPRINTF(("%s: function %d CCR at %d "
191 1.2 thorpej "offset %lx: %x %x %x %x, %x %x %x %x, %x\n",
192 1.2 thorpej sc->dev.dv_xname, pf->number,
193 1.2 thorpej pf->pf_ccr_window, pf->pf_ccr_offset,
194 1.2 thorpej pcmcia_ccr_read(pf, 0x00),
195 1.2 thorpej pcmcia_ccr_read(pf, 0x02), pcmcia_ccr_read(pf, 0x04),
196 1.2 thorpej pcmcia_ccr_read(pf, 0x06), pcmcia_ccr_read(pf, 0x0A),
197 1.2 thorpej pcmcia_ccr_read(pf, 0x0C), pcmcia_ccr_read(pf, 0x0E),
198 1.2 thorpej pcmcia_ccr_read(pf, 0x10), pcmcia_ccr_read(pf, 0x12)));
199 1.2 thorpej }
200 1.2 thorpej }
201 1.2 thorpej
202 1.2 thorpej return (attached ? 0 : 1);
203 1.2 thorpej }
204 1.2 thorpej
205 1.2 thorpej void
206 1.2 thorpej pcmcia_card_detach(dev)
207 1.2 thorpej struct device *dev;
208 1.2 thorpej {
209 1.10 thorpej struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
210 1.10 thorpej
211 1.10 thorpej /* XXX Implement me!
212 1.10 thorpej *
213 1.10 thorpej * Should:
214 1.10 thorpej *
215 1.10 thorpej * - ensure child is deactivated
216 1.10 thorpej *
217 1.10 thorpej * - config_detach child
218 1.10 thorpej *
219 1.10 thorpej * We assume we have context (and can block).
220 1.10 thorpej */
221 1.10 thorpej
222 1.10 thorpej printf("%s: pcmcia_card_detach called!\n", sc->dev.dv_xname);
223 1.2 thorpej }
224 1.2 thorpej
225 1.2 thorpej int
226 1.2 thorpej pcmcia_submatch(parent, cf, aux)
227 1.2 thorpej struct device *parent;
228 1.2 thorpej struct cfdata *cf;
229 1.2 thorpej void *aux;
230 1.2 thorpej {
231 1.2 thorpej struct pcmcia_attach_args *paa = aux;
232 1.2 thorpej
233 1.3 enami if (cf->cf_loc[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT &&
234 1.3 enami cf->cf_loc[PCMCIACF_FUNCTION] != paa->pf->number)
235 1.2 thorpej return (0);
236 1.2 thorpej
237 1.2 thorpej return ((*cf->cf_attach->ca_match)(parent, cf, aux));
238 1.2 thorpej }
239 1.2 thorpej
240 1.2 thorpej int
241 1.2 thorpej pcmcia_print(arg, pnp)
242 1.2 thorpej void *arg;
243 1.2 thorpej const char *pnp;
244 1.2 thorpej {
245 1.2 thorpej struct pcmcia_attach_args *pa = arg;
246 1.2 thorpej struct pcmcia_softc *sc = pa->pf->sc;
247 1.2 thorpej struct pcmcia_card *card = &sc->card;
248 1.2 thorpej int i;
249 1.2 thorpej
250 1.2 thorpej if (pnp) {
251 1.2 thorpej for (i = 0; i < 4; i++) {
252 1.2 thorpej if (card->cis1_info[i] == NULL)
253 1.2 thorpej break;
254 1.2 thorpej if (i)
255 1.2 thorpej printf(", ");
256 1.2 thorpej printf("%s", card->cis1_info[i]);
257 1.2 thorpej }
258 1.2 thorpej if (i)
259 1.2 thorpej printf(" ");
260 1.2 thorpej printf("(manufacturer 0x%x, product 0x%x)", card->manufacturer,
261 1.2 thorpej card->product);
262 1.2 thorpej }
263 1.2 thorpej printf(" function %d", pa->pf->number);
264 1.2 thorpej
265 1.2 thorpej return (UNCONF);
266 1.2 thorpej }
267 1.2 thorpej
268 1.2 thorpej int
269 1.2 thorpej pcmcia_card_gettype(dev)
270 1.2 thorpej struct device *dev;
271 1.2 thorpej {
272 1.7 enami struct pcmcia_softc *sc = (struct pcmcia_softc *)dev;
273 1.7 enami struct pcmcia_function *pf;
274 1.2 thorpej
275 1.2 thorpej /*
276 1.2 thorpej * set the iftype to memory if this card has no functions (not yet
277 1.7 enami * probed), or only one function, and that is not initialized yet or
278 1.7 enami * that is memory.
279 1.2 thorpej */
280 1.7 enami pf = SIMPLEQ_FIRST(&sc->card.pf_head);
281 1.7 enami if (pf == NULL ||
282 1.7 enami (SIMPLEQ_NEXT(pf, pf_list) == NULL &&
283 1.7 enami (pf->cfe == NULL || pf->cfe->iftype == PCMCIA_IFTYPE_MEMORY)))
284 1.2 thorpej return (PCMCIA_IFTYPE_MEMORY);
285 1.2 thorpej else
286 1.2 thorpej return (PCMCIA_IFTYPE_IO);
287 1.2 thorpej }
288 1.2 thorpej
289 1.2 thorpej /*
290 1.2 thorpej * Initialize a PCMCIA function. May be called as long as the function is
291 1.2 thorpej * disabled.
292 1.2 thorpej */
293 1.2 thorpej void
294 1.2 thorpej pcmcia_function_init(pf, cfe)
295 1.2 thorpej struct pcmcia_function *pf;
296 1.2 thorpej struct pcmcia_config_entry *cfe;
297 1.2 thorpej {
298 1.2 thorpej if (pf->pf_flags & PFF_ENABLED)
299 1.2 thorpej panic("pcmcia_function_init: function is enabled");
300 1.2 thorpej
301 1.2 thorpej /* Remember which configuration entry we are using. */
302 1.2 thorpej pf->cfe = cfe;
303 1.2 thorpej }
304 1.2 thorpej
305 1.5 marc static inline void pcmcia_socket_enable(pct, pch)
306 1.5 marc pcmcia_chipset_tag_t pct;
307 1.5 marc pcmcia_chipset_handle_t *pch;
308 1.5 marc {
309 1.5 marc pcmcia_chip_socket_enable(pct, pch);
310 1.5 marc }
311 1.5 marc
312 1.5 marc static inline void pcmcia_socket_disable(pct, pch)
313 1.5 marc pcmcia_chipset_tag_t pct;
314 1.5 marc pcmcia_chipset_handle_t *pch;
315 1.5 marc {
316 1.5 marc pcmcia_chip_socket_disable(pct, pch);
317 1.5 marc }
318 1.5 marc
319 1.2 thorpej /* Enable a PCMCIA function */
320 1.2 thorpej int
321 1.2 thorpej pcmcia_function_enable(pf)
322 1.2 thorpej struct pcmcia_function *pf;
323 1.2 thorpej {
324 1.2 thorpej struct pcmcia_function *tmp;
325 1.2 thorpej int reg;
326 1.2 thorpej
327 1.2 thorpej if (pf->cfe == NULL)
328 1.2 thorpej panic("pcmcia_function_enable: function not initialized");
329 1.2 thorpej
330 1.5 marc /*
331 1.5 marc * Increase the reference count on the socket, enabling power, if
332 1.5 marc * necessary.
333 1.5 marc */
334 1.5 marc if (pf->sc->sc_enabled_count++ == 0)
335 1.5 marc pcmcia_chip_socket_enable(pf->sc->pct, pf->sc->pch);
336 1.5 marc DPRINTF(("%s: ++enabled_count = %d\n", pf->sc->dev.dv_xname,
337 1.5 marc pf->sc->sc_enabled_count));
338 1.5 marc
339 1.2 thorpej if (pf->pf_flags & PFF_ENABLED) {
340 1.2 thorpej /*
341 1.2 thorpej * Don't do anything if we're already enabled.
342 1.2 thorpej */
343 1.2 thorpej return (0);
344 1.2 thorpej }
345 1.2 thorpej
346 1.2 thorpej /*
347 1.2 thorpej * it's possible for different functions' CCRs to be in the same
348 1.2 thorpej * underlying page. Check for that.
349 1.2 thorpej */
350 1.2 thorpej
351 1.2 thorpej for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
352 1.2 thorpej tmp = tmp->pf_list.sqe_next) {
353 1.2 thorpej if ((tmp->pf_flags & PFF_ENABLED) &&
354 1.2 thorpej (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
355 1.2 thorpej ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
356 1.2 thorpej (tmp->ccr_base - tmp->pf_ccr_offset +
357 1.2 thorpej tmp->pf_ccr_realsize))) {
358 1.2 thorpej pf->pf_ccrt = tmp->pf_ccrt;
359 1.2 thorpej pf->pf_ccrh = tmp->pf_ccrh;
360 1.2 thorpej pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
361 1.2 thorpej
362 1.2 thorpej /*
363 1.2 thorpej * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
364 1.2 thorpej * tmp->ccr_base) + pf->ccr_base;
365 1.2 thorpej */
366 1.2 thorpej pf->pf_ccr_offset =
367 1.2 thorpej (tmp->pf_ccr_offset + pf->ccr_base) -
368 1.2 thorpej tmp->ccr_base;
369 1.2 thorpej pf->pf_ccr_window = tmp->pf_ccr_window;
370 1.2 thorpej break;
371 1.2 thorpej }
372 1.2 thorpej }
373 1.2 thorpej
374 1.2 thorpej if (tmp == NULL) {
375 1.2 thorpej if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh))
376 1.2 thorpej goto bad;
377 1.2 thorpej
378 1.2 thorpej if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
379 1.2 thorpej PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
380 1.2 thorpej &pf->pf_ccr_window)) {
381 1.2 thorpej pcmcia_mem_free(pf, &pf->pf_pcmh);
382 1.2 thorpej goto bad;
383 1.2 thorpej }
384 1.2 thorpej }
385 1.2 thorpej
386 1.2 thorpej reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
387 1.2 thorpej reg |= PCMCIA_CCR_OPTION_LEVIREQ;
388 1.5 marc if (pcmcia_mfc(pf->sc)) {
389 1.5 marc reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
390 1.5 marc PCMCIA_CCR_OPTION_ADDR_DECODE);
391 1.5 marc if (pf->ih_fct)
392 1.5 marc reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
393 1.5 marc
394 1.5 marc }
395 1.2 thorpej pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
396 1.2 thorpej
397 1.2 thorpej reg = 0;
398 1.2 thorpej
399 1.2 thorpej if ((pf->cfe->flags & PCMCIA_CFE_IO16) == 0)
400 1.2 thorpej reg |= PCMCIA_CCR_STATUS_IOIS8;
401 1.2 thorpej if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
402 1.2 thorpej reg |= PCMCIA_CCR_STATUS_AUDIO;
403 1.2 thorpej pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
404 1.2 thorpej
405 1.2 thorpej pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
406 1.2 thorpej
407 1.5 marc if (pcmcia_mfc(pf->sc)) {
408 1.5 marc long tmp, iosize;
409 1.5 marc
410 1.5 marc tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
411 1.5 marc /* round up to nearest (2^n)-1 */
412 1.5 marc for (iosize = 1; iosize < tmp; iosize <<= 1)
413 1.5 marc ;
414 1.5 marc iosize--;
415 1.5 marc
416 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
417 1.5 marc pf->pf_mfc_iobase & 0xff);
418 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
419 1.5 marc (pf->pf_mfc_iobase >> 8) & 0xff);
420 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
421 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
422 1.5 marc
423 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
424 1.5 marc }
425 1.5 marc
426 1.5 marc DPRINTF(("%s: function %d CCR at %d offset %lx: "
427 1.5 marc "%x %x %x %x, %x %x %x %x, %x\n",
428 1.5 marc pf->sc->dev.dv_xname, pf->number,
429 1.5 marc pf->pf_ccr_window, pf->pf_ccr_offset,
430 1.5 marc pcmcia_ccr_read(pf, 0x00), pcmcia_ccr_read(pf, 0x02),
431 1.5 marc pcmcia_ccr_read(pf, 0x04), pcmcia_ccr_read(pf, 0x06),
432 1.5 marc
433 1.5 marc pcmcia_ccr_read(pf, 0x0A), pcmcia_ccr_read(pf, 0x0C),
434 1.5 marc pcmcia_ccr_read(pf, 0x0E), pcmcia_ccr_read(pf, 0x10),
435 1.5 marc
436 1.5 marc pcmcia_ccr_read(pf, 0x12)));
437 1.5 marc
438 1.2 thorpej pf->pf_flags |= PFF_ENABLED;
439 1.2 thorpej return (0);
440 1.2 thorpej
441 1.2 thorpej bad:
442 1.2 thorpej /*
443 1.2 thorpej * Decrement the reference count, and power down the socket, if
444 1.2 thorpej * necessary.
445 1.2 thorpej */
446 1.5 marc if (--pf->sc->sc_enabled_count == 0)
447 1.2 thorpej pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
448 1.5 marc DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
449 1.5 marc pf->sc->sc_enabled_count));
450 1.5 marc
451 1.2 thorpej return (1);
452 1.2 thorpej }
453 1.2 thorpej
454 1.2 thorpej /* Disable PCMCIA function. */
455 1.2 thorpej void
456 1.2 thorpej pcmcia_function_disable(pf)
457 1.2 thorpej struct pcmcia_function *pf;
458 1.2 thorpej {
459 1.2 thorpej struct pcmcia_function *tmp;
460 1.2 thorpej
461 1.2 thorpej if (pf->cfe == NULL)
462 1.2 thorpej panic("pcmcia_function_enable: function not initialized");
463 1.2 thorpej
464 1.2 thorpej if ((pf->pf_flags & PFF_ENABLED) == 0) {
465 1.2 thorpej /*
466 1.2 thorpej * Don't do anything if we're already disabled.
467 1.2 thorpej */
468 1.2 thorpej return;
469 1.2 thorpej }
470 1.2 thorpej
471 1.2 thorpej /*
472 1.2 thorpej * it's possible for different functions' CCRs to be in the same
473 1.2 thorpej * underlying page. Check for that. Note we mark us as disabled
474 1.2 thorpej * first to avoid matching ourself.
475 1.2 thorpej */
476 1.2 thorpej
477 1.2 thorpej pf->pf_flags &= ~PFF_ENABLED;
478 1.2 thorpej for (tmp = pf->sc->card.pf_head.sqh_first; tmp != NULL;
479 1.2 thorpej tmp = tmp->pf_list.sqe_next) {
480 1.2 thorpej if ((tmp->pf_flags & PFF_ENABLED) &&
481 1.2 thorpej (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
482 1.2 thorpej ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
483 1.2 thorpej (tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
484 1.2 thorpej break;
485 1.2 thorpej }
486 1.2 thorpej
487 1.2 thorpej /* Not used by anyone else; unmap the CCR. */
488 1.2 thorpej if (tmp == NULL) {
489 1.2 thorpej pcmcia_mem_unmap(pf, pf->pf_ccr_window);
490 1.2 thorpej pcmcia_mem_free(pf, &pf->pf_pcmh);
491 1.2 thorpej }
492 1.2 thorpej
493 1.2 thorpej /*
494 1.2 thorpej * Decrement the reference count, and power down the socket, if
495 1.2 thorpej * necessary.
496 1.2 thorpej */
497 1.2 thorpej if (--pf->sc->sc_enabled_count == 0)
498 1.2 thorpej pcmcia_chip_socket_disable(pf->sc->pct, pf->sc->pch);
499 1.5 marc DPRINTF(("%s: --enabled_count = %d\n", pf->sc->dev.dv_xname,
500 1.5 marc pf->sc->sc_enabled_count));
501 1.2 thorpej }
502 1.2 thorpej
503 1.2 thorpej int
504 1.2 thorpej pcmcia_io_map(pf, width, offset, size, pcihp, windowp)
505 1.2 thorpej struct pcmcia_function *pf;
506 1.2 thorpej int width;
507 1.2 thorpej bus_addr_t offset;
508 1.2 thorpej bus_size_t size;
509 1.2 thorpej struct pcmcia_io_handle *pcihp;
510 1.2 thorpej int *windowp;
511 1.2 thorpej {
512 1.2 thorpej int reg;
513 1.2 thorpej
514 1.2 thorpej if (pcmcia_chip_io_map(pf->sc->pct, pf->sc->pch,
515 1.2 thorpej width, offset, size, pcihp, windowp))
516 1.2 thorpej return (1);
517 1.2 thorpej
518 1.2 thorpej /*
519 1.2 thorpej * XXX in the multifunction multi-iospace-per-function case, this
520 1.2 thorpej * needs to cooperate with io_alloc to make sure that the spaces
521 1.2 thorpej * don't overlap, and that the ccr's are set correctly
522 1.2 thorpej */
523 1.2 thorpej
524 1.2 thorpej if (pcmcia_mfc(pf->sc)) {
525 1.5 marc long tmp, iosize;
526 1.5 marc
527 1.5 marc if (pf->pf_mfc_iomax == 0) {
528 1.5 marc pf->pf_mfc_iobase = pcihp->addr + offset;
529 1.5 marc pf->pf_mfc_iomax = pf->pf_mfc_iobase + size;
530 1.5 marc } else {
531 1.5 marc /* this makes the assumption that nothing overlaps */
532 1.5 marc if (pf->pf_mfc_iobase > pcihp->addr + offset)
533 1.5 marc pf->pf_mfc_iobase = pcihp->addr + offset;
534 1.5 marc if (pf->pf_mfc_iomax < pcihp->addr + offset + size)
535 1.5 marc pf->pf_mfc_iomax = pcihp->addr + offset + size;
536 1.5 marc }
537 1.5 marc
538 1.5 marc tmp = pf->pf_mfc_iomax - pf->pf_mfc_iobase;
539 1.5 marc /* round up to nearest (2^n)-1 */
540 1.5 marc for (iosize = 1; iosize >= tmp; iosize <<= 1)
541 1.5 marc ;
542 1.5 marc iosize--;
543 1.5 marc
544 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
545 1.5 marc pf->pf_mfc_iobase & 0xff);
546 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
547 1.5 marc (pf->pf_mfc_iobase >> 8) & 0xff);
548 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2, 0);
549 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3, 0);
550 1.5 marc
551 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_IOSIZE, iosize);
552 1.2 thorpej
553 1.2 thorpej reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
554 1.2 thorpej reg |= PCMCIA_CCR_OPTION_ADDR_DECODE;
555 1.2 thorpej pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
556 1.2 thorpej }
557 1.2 thorpej return (0);
558 1.2 thorpej }
559 1.2 thorpej
560 1.2 thorpej void *
561 1.2 thorpej pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
562 1.2 thorpej struct pcmcia_function *pf;
563 1.2 thorpej int ipl;
564 1.2 thorpej int (*ih_fct) __P((void *));
565 1.2 thorpej void *ih_arg;
566 1.2 thorpej {
567 1.2 thorpej void *ret;
568 1.2 thorpej
569 1.2 thorpej /* behave differently if this is a multifunction card */
570 1.2 thorpej
571 1.2 thorpej if (pcmcia_mfc(pf->sc)) {
572 1.2 thorpej int s, ihcnt, hiipl, reg;
573 1.2 thorpej struct pcmcia_function *pf2;
574 1.2 thorpej
575 1.2 thorpej /*
576 1.2 thorpej * mask all the ipl's which are already used by this card,
577 1.2 thorpej * and find the highest ipl number (lowest priority)
578 1.2 thorpej */
579 1.2 thorpej
580 1.2 thorpej ihcnt = 0;
581 1.5 marc s = 0; /* this is only here to keep the compiler
582 1.2 thorpej happy */
583 1.5 marc hiipl = 0; /* this is only here to keep the compiler
584 1.2 thorpej happy */
585 1.2 thorpej
586 1.2 thorpej for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
587 1.2 thorpej pf2 = pf2->pf_list.sqe_next) {
588 1.2 thorpej if (pf2->ih_fct) {
589 1.5 marc DPRINTF(("%s: function %d has ih_fct %p\n",
590 1.5 marc pf->sc->dev.dv_xname, pf2->number,
591 1.5 marc pf2->ih_fct));
592 1.5 marc
593 1.2 thorpej if (ihcnt == 0) {
594 1.2 thorpej hiipl = pf2->ih_ipl;
595 1.2 thorpej } else {
596 1.2 thorpej if (pf2->ih_ipl > hiipl)
597 1.2 thorpej hiipl = pf2->ih_ipl;
598 1.2 thorpej }
599 1.5 marc
600 1.5 marc ihcnt++;
601 1.2 thorpej }
602 1.2 thorpej }
603 1.2 thorpej
604 1.2 thorpej /*
605 1.2 thorpej * establish the real interrupt, changing the ipl if
606 1.2 thorpej * necessary
607 1.2 thorpej */
608 1.2 thorpej
609 1.2 thorpej if (ihcnt == 0) {
610 1.2 thorpej #ifdef DIAGNOSTIC
611 1.2 thorpej if (pf->sc->ih != NULL)
612 1.2 thorpej panic("card has intr handler, but no function does");
613 1.2 thorpej #endif
614 1.6 christos s = splhigh();
615 1.6 christos
616 1.5 marc /* set up the handler for the new function */
617 1.5 marc
618 1.5 marc pf->ih_fct = ih_fct;
619 1.5 marc pf->ih_arg = ih_arg;
620 1.5 marc pf->ih_ipl = ipl;
621 1.2 thorpej
622 1.2 thorpej pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
623 1.2 thorpej pf->sc->pch, pf, ipl, pcmcia_card_intr, pf->sc);
624 1.6 christos splx(s);
625 1.2 thorpej } else if (ipl > hiipl) {
626 1.2 thorpej #ifdef DIAGNOSTIC
627 1.2 thorpej if (pf->sc->ih == NULL)
628 1.2 thorpej panic("functions have ih, but the card does not");
629 1.2 thorpej #endif
630 1.2 thorpej
631 1.5 marc /* XXX need #ifdef for splserial on x86 */
632 1.5 marc s = splhigh();
633 1.5 marc
634 1.6 christos pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
635 1.6 christos pf->sc->ih);
636 1.6 christos
637 1.5 marc /* set up the handler for the new function */
638 1.5 marc pf->ih_fct = ih_fct;
639 1.5 marc pf->ih_arg = ih_arg;
640 1.5 marc pf->ih_ipl = ipl;
641 1.5 marc
642 1.2 thorpej pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
643 1.2 thorpej pf->sc->pch, pf, ipl, pcmcia_card_intr, pf->sc);
644 1.5 marc
645 1.5 marc splx(s);
646 1.5 marc } else {
647 1.5 marc s = splhigh();
648 1.5 marc
649 1.5 marc /* set up the handler for the new function */
650 1.5 marc
651 1.5 marc pf->ih_fct = ih_fct;
652 1.5 marc pf->ih_arg = ih_arg;
653 1.5 marc pf->ih_ipl = ipl;
654 1.5 marc
655 1.5 marc splx(s);
656 1.2 thorpej }
657 1.2 thorpej
658 1.2 thorpej ret = pf->sc->ih;
659 1.2 thorpej
660 1.2 thorpej if (ret != NULL) {
661 1.2 thorpej reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
662 1.2 thorpej reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
663 1.2 thorpej pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
664 1.2 thorpej
665 1.2 thorpej reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
666 1.2 thorpej reg |= PCMCIA_CCR_STATUS_INTRACK;
667 1.2 thorpej pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
668 1.2 thorpej }
669 1.2 thorpej } else {
670 1.2 thorpej ret = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
671 1.2 thorpej pf, ipl, ih_fct, ih_arg);
672 1.2 thorpej }
673 1.2 thorpej
674 1.2 thorpej return (ret);
675 1.2 thorpej }
676 1.2 thorpej
677 1.2 thorpej void
678 1.2 thorpej pcmcia_intr_disestablish(pf, ih)
679 1.2 thorpej struct pcmcia_function *pf;
680 1.2 thorpej void *ih;
681 1.2 thorpej {
682 1.2 thorpej /* behave differently if this is a multifunction card */
683 1.2 thorpej
684 1.2 thorpej if (pcmcia_mfc(pf->sc)) {
685 1.2 thorpej int s, ihcnt, hiipl;
686 1.2 thorpej struct pcmcia_function *pf2;
687 1.2 thorpej
688 1.2 thorpej /*
689 1.2 thorpej * mask all the ipl's which are already used by this card,
690 1.2 thorpej * and find the highest ipl number (lowest priority). Skip
691 1.2 thorpej * the current function.
692 1.2 thorpej */
693 1.2 thorpej
694 1.2 thorpej ihcnt = 0;
695 1.2 thorpej s = 0; /* this is only here to keep the compipler
696 1.2 thorpej happy */
697 1.2 thorpej hiipl = 0; /* this is only here to keep the compipler
698 1.2 thorpej happy */
699 1.2 thorpej
700 1.2 thorpej for (pf2 = pf->sc->card.pf_head.sqh_first; pf2 != NULL;
701 1.2 thorpej pf2 = pf2->pf_list.sqe_next) {
702 1.2 thorpej if (pf2 == pf)
703 1.2 thorpej continue;
704 1.2 thorpej
705 1.2 thorpej if (pf2->ih_fct) {
706 1.2 thorpej if (ihcnt == 0) {
707 1.2 thorpej hiipl = pf2->ih_ipl;
708 1.2 thorpej } else {
709 1.2 thorpej if (pf2->ih_ipl > hiipl)
710 1.2 thorpej hiipl = pf2->ih_ipl;
711 1.2 thorpej }
712 1.5 marc ihcnt++;
713 1.2 thorpej }
714 1.2 thorpej }
715 1.2 thorpej
716 1.2 thorpej /*
717 1.2 thorpej * if the ih being removed is lower priority than the lowest
718 1.2 thorpej * priority remaining interrupt, up the priority.
719 1.2 thorpej */
720 1.2 thorpej
721 1.5 marc /* ihcnt is the number of interrupt handlers *not* including
722 1.5 marc the one about to be removed. */
723 1.5 marc
724 1.2 thorpej if (ihcnt == 0) {
725 1.5 marc int reg;
726 1.5 marc
727 1.2 thorpej #ifdef DIAGNOSTIC
728 1.2 thorpej if (pf->sc->ih == NULL)
729 1.2 thorpej panic("disestablishing last function, but card has no ih");
730 1.2 thorpej #endif
731 1.2 thorpej pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
732 1.2 thorpej pf->sc->ih);
733 1.5 marc
734 1.5 marc reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
735 1.5 marc reg &= ~PCMCIA_CCR_OPTION_IREQ_ENABLE;
736 1.5 marc pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
737 1.5 marc
738 1.5 marc pf->ih_fct = NULL;
739 1.5 marc pf->ih_arg = NULL;
740 1.5 marc
741 1.2 thorpej pf->sc->ih = NULL;
742 1.2 thorpej } else if (pf->ih_ipl > hiipl) {
743 1.2 thorpej #ifdef DIAGNOSTIC
744 1.2 thorpej if (pf->sc->ih == NULL)
745 1.2 thorpej panic("changing ih ipl, but card has no ih");
746 1.2 thorpej #endif
747 1.5 marc /* XXX need #ifdef for splserial on x86 */
748 1.5 marc s = splhigh();
749 1.5 marc
750 1.2 thorpej pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch,
751 1.2 thorpej pf->sc->ih);
752 1.2 thorpej pf->sc->ih = pcmcia_chip_intr_establish(pf->sc->pct,
753 1.2 thorpej pf->sc->pch, pf, hiipl, pcmcia_card_intr, pf->sc);
754 1.5 marc
755 1.5 marc /* null out the handler for this function */
756 1.5 marc
757 1.5 marc pf->ih_fct = NULL;
758 1.5 marc pf->ih_arg = NULL;
759 1.5 marc
760 1.5 marc splx(s);
761 1.5 marc } else {
762 1.5 marc s = splhigh();
763 1.5 marc
764 1.5 marc pf->ih_fct = NULL;
765 1.5 marc pf->ih_arg = NULL;
766 1.5 marc
767 1.5 marc splx(s);
768 1.2 thorpej }
769 1.2 thorpej } else {
770 1.2 thorpej pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
771 1.2 thorpej }
772 1.2 thorpej }
773 1.2 thorpej
774 1.2 thorpej int
775 1.2 thorpej pcmcia_card_intr(arg)
776 1.2 thorpej void *arg;
777 1.2 thorpej {
778 1.2 thorpej struct pcmcia_softc *sc = arg;
779 1.2 thorpej struct pcmcia_function *pf;
780 1.2 thorpej int reg, ret, ret2;
781 1.2 thorpej
782 1.2 thorpej ret = 0;
783 1.2 thorpej
784 1.2 thorpej for (pf = sc->card.pf_head.sqh_first; pf != NULL;
785 1.2 thorpej pf = pf->pf_list.sqe_next) {
786 1.2 thorpej #if 0
787 1.5 marc printf("%s: intr flags=%x fct=%d physaddr=%lx cor=%02x csr=%02x pin=%02x",
788 1.6 christos sc->dev.dv_xname, pf->pf_flags, pf->number,
789 1.2 thorpej pmap_extract(pmap_kernel(),
790 1.9 eeh (vaddr_t) pf->pf_ccrh) + pf->pf_ccr_offset,
791 1.6 christos pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION),
792 1.5 marc pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS),
793 1.5 marc pcmcia_ccr_read(pf, PCMCIA_CCR_PIN));
794 1.2 thorpej #endif
795 1.2 thorpej if (pf->ih_fct != NULL &&
796 1.2 thorpej (pf->ccr_mask & (1 << (PCMCIA_CCR_STATUS / 2)))) {
797 1.2 thorpej reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
798 1.2 thorpej if (reg & PCMCIA_CCR_STATUS_INTR) {
799 1.2 thorpej ret2 = (*pf->ih_fct)(pf->ih_arg);
800 1.2 thorpej if (ret2 != 0 && ret == 0)
801 1.2 thorpej ret = ret2;
802 1.2 thorpej reg = pcmcia_ccr_read(pf, PCMCIA_CCR_STATUS);
803 1.2 thorpej #if 0
804 1.2 thorpej printf("; csr %02x->%02x",
805 1.2 thorpej reg, reg & ~PCMCIA_CCR_STATUS_INTR);
806 1.2 thorpej #endif
807 1.2 thorpej pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS,
808 1.2 thorpej reg & ~PCMCIA_CCR_STATUS_INTR);
809 1.2 thorpej }
810 1.2 thorpej }
811 1.2 thorpej #if 0
812 1.2 thorpej printf("\n");
813 1.2 thorpej #endif
814 1.2 thorpej }
815 1.2 thorpej
816 1.2 thorpej return (ret);
817 1.2 thorpej }
818