aupcmcia.c revision 1.2.8.4 1 1.2.8.4 yamt /* $NetBSD: aupcmcia.c,v 1.2.8.4 2008/01/21 09:37:30 yamt Exp $ */
2 1.2.8.2 yamt
3 1.2.8.2 yamt /*-
4 1.2.8.2 yamt * Copyright (c) 2006 Itronix Inc.
5 1.2.8.2 yamt * All rights reserved.
6 1.2.8.2 yamt *
7 1.2.8.2 yamt * Written by Garrett D'Amore for Itronix Inc.
8 1.2.8.2 yamt *
9 1.2.8.2 yamt * Redistribution and use in source and binary forms, with or without
10 1.2.8.2 yamt * modification, are permitted provided that the following conditions
11 1.2.8.2 yamt * are met:
12 1.2.8.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.2.8.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.2.8.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.8.2 yamt * notice, this list of conditions and the following disclaimer in the
16 1.2.8.2 yamt * documentation and/or other materials provided with the distribution.
17 1.2.8.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
18 1.2.8.2 yamt * or promote products derived from this software without specific
19 1.2.8.2 yamt * prior written permission.
20 1.2.8.2 yamt *
21 1.2.8.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 1.2.8.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.2.8.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.2.8.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.2.8.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.2.8.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.2.8.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 1.2.8.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.2.8.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.2.8.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.2.8.2 yamt * POSSIBILITY OF SUCH DAMAGE.
32 1.2.8.2 yamt */
33 1.2.8.2 yamt
34 1.2.8.2 yamt /* #include "opt_pci.h" */
35 1.2.8.2 yamt /* #include "pci.h" */
36 1.2.8.2 yamt
37 1.2.8.2 yamt #include <sys/cdefs.h>
38 1.2.8.4 yamt __KERNEL_RCSID(0, "$NetBSD: aupcmcia.c,v 1.2.8.4 2008/01/21 09:37:30 yamt Exp $");
39 1.2.8.2 yamt
40 1.2.8.2 yamt #include <sys/types.h>
41 1.2.8.2 yamt #include <sys/param.h>
42 1.2.8.2 yamt #include <sys/systm.h>
43 1.2.8.2 yamt #include <sys/errno.h>
44 1.2.8.2 yamt #include <sys/kernel.h>
45 1.2.8.2 yamt #include <sys/kthread.h>
46 1.2.8.4 yamt #include <sys/intr.h>
47 1.2.8.4 yamt #include <sys/device.h>
48 1.2.8.2 yamt
49 1.2.8.2 yamt #include <dev/pcmcia/pcmciareg.h>
50 1.2.8.2 yamt #include <dev/pcmcia/pcmciavar.h>
51 1.2.8.2 yamt #include <dev/pcmcia/pcmciachip.h>
52 1.2.8.2 yamt
53 1.2.8.2 yamt #include <mips/alchemy/include/au_himem_space.h>
54 1.2.8.2 yamt #include <mips/alchemy/include/aubusvar.h>
55 1.2.8.2 yamt #include <mips/alchemy/include/aureg.h>
56 1.2.8.2 yamt #include <mips/alchemy/include/auvar.h>
57 1.2.8.2 yamt
58 1.2.8.2 yamt #include <mips/alchemy/dev/aupcmciareg.h>
59 1.2.8.2 yamt #include <mips/alchemy/dev/aupcmciavar.h>
60 1.2.8.2 yamt
61 1.2.8.2 yamt /*
62 1.2.8.2 yamt * Borrow PCMCIADEBUG for now. Generally aupcmcia is the only PCMCIA
63 1.2.8.2 yamt * host on these machines anyway.
64 1.2.8.2 yamt */
65 1.2.8.2 yamt #ifdef PCMCIADEBUG
66 1.2.8.2 yamt int aupcm_debug = 1;
67 1.2.8.2 yamt #define DPRINTF(arg) if (aupcm_debug) printf arg
68 1.2.8.2 yamt #else
69 1.2.8.2 yamt #define DPRINTF(arg)
70 1.2.8.2 yamt #endif
71 1.2.8.2 yamt
72 1.2.8.2 yamt /*
73 1.2.8.2 yamt * And for information about mappings, etc. use this one.
74 1.2.8.2 yamt */
75 1.2.8.2 yamt #ifdef AUPCMCIANOISY
76 1.2.8.2 yamt #define NOISY(arg) printf arg
77 1.2.8.2 yamt #else
78 1.2.8.2 yamt #define NOISY(arg)
79 1.2.8.2 yamt #endif
80 1.2.8.2 yamt
81 1.2.8.2 yamt /*
82 1.2.8.2 yamt * Note, we use prefix "aupcm" instead of "aupcmcia", even though our
83 1.2.8.2 yamt * driver is the latter, mostly because my fingers have trouble typing
84 1.2.8.2 yamt * the former. "aupcm" should be sufficiently unique to avoid
85 1.2.8.2 yamt * confusion.
86 1.2.8.2 yamt */
87 1.2.8.2 yamt
88 1.2.8.2 yamt static int aupcm_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
89 1.2.8.2 yamt struct pcmcia_mem_handle *);
90 1.2.8.2 yamt static void aupcm_mem_free(pcmcia_chipset_handle_t,
91 1.2.8.2 yamt struct pcmcia_mem_handle *);
92 1.2.8.2 yamt static int aupcm_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
93 1.2.8.2 yamt bus_size_t, struct pcmcia_mem_handle *, bus_size_t *, int *);
94 1.2.8.2 yamt static void aupcm_mem_unmap(pcmcia_chipset_handle_t, int);
95 1.2.8.2 yamt
96 1.2.8.2 yamt static int aupcm_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
97 1.2.8.2 yamt bus_size_t, struct pcmcia_io_handle *);
98 1.2.8.2 yamt static void aupcm_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
99 1.2.8.2 yamt static int aupcm_io_map(pcmcia_chipset_handle_t, int, bus_addr_t,
100 1.2.8.2 yamt bus_size_t, struct pcmcia_io_handle *, int *);
101 1.2.8.2 yamt static void aupcm_io_unmap(pcmcia_chipset_handle_t, int);
102 1.2.8.2 yamt static void *aupcm_intr_establish(pcmcia_chipset_handle_t,
103 1.2.8.2 yamt struct pcmcia_function *, int, int (*)(void *), void *);
104 1.2.8.2 yamt static void aupcm_intr_disestablish(pcmcia_chipset_handle_t, void *);
105 1.2.8.2 yamt
106 1.2.8.2 yamt static void aupcm_slot_enable(pcmcia_chipset_handle_t);
107 1.2.8.2 yamt static void aupcm_slot_disable(pcmcia_chipset_handle_t);
108 1.2.8.2 yamt static void aupcm_slot_settype(pcmcia_chipset_handle_t, int);
109 1.2.8.2 yamt
110 1.2.8.2 yamt static int aupcm_match(struct device *, struct cfdata *, void *);
111 1.2.8.2 yamt static void aupcm_attach(struct device *, struct device *, void *);
112 1.2.8.2 yamt
113 1.2.8.2 yamt static void aupcm_event_thread(void *);
114 1.2.8.2 yamt static int aupcm_card_intr(void *);
115 1.2.8.2 yamt static void aupcm_softintr(void *);
116 1.2.8.2 yamt static int aupcm_print(void *, const char *);
117 1.2.8.2 yamt
118 1.2.8.2 yamt struct aupcm_slot {
119 1.2.8.2 yamt struct aupcm_softc *as_softc;
120 1.2.8.2 yamt int as_slot;
121 1.2.8.2 yamt int as_status;
122 1.2.8.2 yamt int as_enabled;
123 1.2.8.2 yamt int (*as_intr)(void *);
124 1.2.8.2 yamt int as_card_irq;
125 1.2.8.2 yamt int as_status_irq;
126 1.2.8.2 yamt void *as_intrarg;
127 1.2.8.2 yamt void *as_softint;
128 1.2.8.2 yamt void *as_hardint;
129 1.2.8.2 yamt const char *as_name;
130 1.2.8.2 yamt bus_addr_t as_offset;
131 1.2.8.2 yamt struct mips_bus_space as_iot;
132 1.2.8.2 yamt struct mips_bus_space as_attrt;
133 1.2.8.2 yamt struct mips_bus_space as_memt;
134 1.2.8.2 yamt void *as_wins[AUPCMCIA_NWINS];
135 1.2.8.2 yamt
136 1.2.8.2 yamt struct device *as_pcmcia;
137 1.2.8.2 yamt };
138 1.2.8.2 yamt
139 1.2.8.2 yamt /* this structure needs to be exposed... */
140 1.2.8.2 yamt struct aupcm_softc {
141 1.2.8.2 yamt struct device sc_dev;
142 1.2.8.2 yamt pcmcia_chipset_tag_t sc_pct;
143 1.2.8.2 yamt
144 1.2.8.2 yamt void (*sc_slot_enable)(int);
145 1.2.8.2 yamt void (*sc_slot_disable)(int);
146 1.2.8.2 yamt int (*sc_slot_status)(int);
147 1.2.8.2 yamt
148 1.2.8.2 yamt paddr_t sc_base;
149 1.2.8.2 yamt
150 1.2.8.2 yamt int sc_wake;
151 1.2.8.3 yamt lwp_t *sc_thread;
152 1.2.8.2 yamt
153 1.2.8.2 yamt int sc_nslots;
154 1.2.8.2 yamt struct aupcm_slot sc_slots[AUPCMCIA_NSLOTS];
155 1.2.8.2 yamt };
156 1.2.8.2 yamt
157 1.2.8.2 yamt static struct pcmcia_chip_functions aupcm_functions = {
158 1.2.8.2 yamt aupcm_mem_alloc,
159 1.2.8.2 yamt aupcm_mem_free,
160 1.2.8.2 yamt aupcm_mem_map,
161 1.2.8.2 yamt aupcm_mem_unmap,
162 1.2.8.2 yamt
163 1.2.8.2 yamt aupcm_io_alloc,
164 1.2.8.2 yamt aupcm_io_free,
165 1.2.8.2 yamt aupcm_io_map,
166 1.2.8.2 yamt aupcm_io_unmap,
167 1.2.8.2 yamt
168 1.2.8.2 yamt aupcm_intr_establish,
169 1.2.8.2 yamt aupcm_intr_disestablish,
170 1.2.8.2 yamt
171 1.2.8.2 yamt aupcm_slot_enable,
172 1.2.8.2 yamt aupcm_slot_disable,
173 1.2.8.2 yamt aupcm_slot_settype,
174 1.2.8.2 yamt };
175 1.2.8.2 yamt
176 1.2.8.2 yamt static struct mips_bus_space aupcm_memt;
177 1.2.8.2 yamt
178 1.2.8.2 yamt CFATTACH_DECL(aupcmcia, sizeof (struct aupcm_softc),
179 1.2.8.2 yamt aupcm_match, aupcm_attach, NULL, NULL);
180 1.2.8.2 yamt
181 1.2.8.2 yamt int
182 1.2.8.2 yamt aupcm_match(struct device *parent, struct cfdata *cf, void *aux)
183 1.2.8.2 yamt {
184 1.2.8.2 yamt struct aubus_attach_args *aa = aux;
185 1.2.8.2 yamt static int found = 0;
186 1.2.8.2 yamt
187 1.2.8.2 yamt if (found)
188 1.2.8.2 yamt return 0;
189 1.2.8.2 yamt
190 1.2.8.2 yamt if (strcmp(aa->aa_name, "aupcmcia") != 0)
191 1.2.8.2 yamt return 0;
192 1.2.8.2 yamt
193 1.2.8.2 yamt found = 1;
194 1.2.8.2 yamt
195 1.2.8.2 yamt return 1;
196 1.2.8.2 yamt }
197 1.2.8.2 yamt
198 1.2.8.2 yamt void
199 1.2.8.2 yamt aupcm_attach(struct device *parent, struct device *self, void *aux)
200 1.2.8.2 yamt {
201 1.2.8.2 yamt /* struct aubus_attach_args *aa = aux; */
202 1.2.8.2 yamt struct aupcm_softc *sc = (struct aupcm_softc *)self;
203 1.2.8.2 yamt static int done = 0;
204 1.2.8.2 yamt int slot;
205 1.2.8.2 yamt struct aupcmcia_machdep *md;
206 1.2.8.2 yamt
207 1.2.8.2 yamt /* initialize bus space */
208 1.2.8.2 yamt if (done) {
209 1.2.8.2 yamt /* there can be only one. */
210 1.2.8.2 yamt return;
211 1.2.8.2 yamt }
212 1.2.8.2 yamt
213 1.2.8.2 yamt done = 1;
214 1.2.8.2 yamt /*
215 1.2.8.2 yamt * PCMCIA memory can live within pretty much the entire 32-bit
216 1.2.8.2 yamt * space, modulo 64 MB wraps. We don't have to support coexisting
217 1.2.8.2 yamt * DMA.
218 1.2.8.2 yamt */
219 1.2.8.2 yamt au_himem_space_init(&aupcm_memt, "pcmciamem",
220 1.2.8.2 yamt PCMCIA_BASE, AUPCMCIA_ATTR_OFFSET, 0xffffffff,
221 1.2.8.2 yamt AU_HIMEM_SPACE_LITTLE_ENDIAN);
222 1.2.8.2 yamt
223 1.2.8.2 yamt if ((md = aupcmcia_machdep()) == NULL) {
224 1.2.8.2 yamt printf("\n%s:unable to get machdep structure\n",
225 1.2.8.2 yamt sc->sc_dev.dv_xname);
226 1.2.8.2 yamt return;
227 1.2.8.2 yamt }
228 1.2.8.2 yamt
229 1.2.8.2 yamt sc->sc_nslots = md->am_nslots;
230 1.2.8.2 yamt sc->sc_slot_enable = md->am_slot_enable;
231 1.2.8.2 yamt sc->sc_slot_disable = md->am_slot_disable;
232 1.2.8.2 yamt sc->sc_slot_status = md->am_slot_status;
233 1.2.8.2 yamt
234 1.2.8.2 yamt printf(": Alchemy PCMCIA, %d slots\n", sc->sc_nslots);
235 1.2.8.2 yamt
236 1.2.8.2 yamt sc->sc_pct = (pcmcia_chipset_tag_t)&aupcm_functions;
237 1.2.8.2 yamt
238 1.2.8.2 yamt for (slot = 0; slot < sc->sc_nslots; slot++) {
239 1.2.8.2 yamt struct aupcm_slot *sp;
240 1.2.8.2 yamt struct pcmciabus_attach_args paa;
241 1.2.8.2 yamt
242 1.2.8.2 yamt sp = &sc->sc_slots[slot];
243 1.2.8.2 yamt sp->as_softc = sc;
244 1.2.8.2 yamt
245 1.2.8.2 yamt sp->as_slot = slot;
246 1.2.8.2 yamt sp->as_name = md->am_slot_name(slot);
247 1.2.8.2 yamt sp->as_offset = md->am_slot_offset(slot);
248 1.2.8.2 yamt sp->as_card_irq = md->am_slot_irq(slot, AUPCMCIA_IRQ_CARD);
249 1.2.8.2 yamt sp->as_status_irq = md->am_slot_irq(slot,
250 1.2.8.2 yamt AUPCMCIA_IRQ_INSERT);
251 1.2.8.2 yamt
252 1.2.8.2 yamt au_himem_space_init(&sp->as_attrt, "pcmciaattr",
253 1.2.8.2 yamt PCMCIA_BASE + sp->as_offset + AUPCMCIA_ATTR_OFFSET,
254 1.2.8.2 yamt 0, AUPCMCIA_MAP_SIZE, AU_HIMEM_SPACE_LITTLE_ENDIAN);
255 1.2.8.2 yamt
256 1.2.8.2 yamt au_himem_space_init(&sp->as_memt, "pcmciamem",
257 1.2.8.2 yamt PCMCIA_BASE + sp->as_offset + AUPCMCIA_MEM_OFFSET,
258 1.2.8.2 yamt 0, AUPCMCIA_MAP_SIZE, AU_HIMEM_SPACE_LITTLE_ENDIAN);
259 1.2.8.2 yamt
260 1.2.8.2 yamt au_himem_space_init(&sp->as_iot, "pcmciaio",
261 1.2.8.2 yamt PCMCIA_BASE + sp->as_offset + AUPCMCIA_IO_OFFSET,
262 1.2.8.2 yamt 0, AUPCMCIA_MAP_SIZE,
263 1.2.8.2 yamt AU_HIMEM_SPACE_LITTLE_ENDIAN | AU_HIMEM_SPACE_IO);
264 1.2.8.2 yamt
265 1.2.8.2 yamt sp->as_status = 0;
266 1.2.8.2 yamt
267 1.2.8.2 yamt paa.paa_busname = "pcmcia";
268 1.2.8.2 yamt paa.pct = sc->sc_pct;
269 1.2.8.2 yamt paa.pch = (pcmcia_chipset_handle_t)sp;
270 1.2.8.2 yamt
271 1.2.8.2 yamt paa.iobase = 0;
272 1.2.8.2 yamt paa.iosize = AUPCMCIA_MAP_SIZE;
273 1.2.8.2 yamt
274 1.2.8.2 yamt sp->as_pcmcia = config_found(&sc->sc_dev, &paa, aupcm_print);
275 1.2.8.2 yamt
276 1.2.8.2 yamt /* if no pcmcia, make sure slot is powered down */
277 1.2.8.2 yamt if (sp->as_pcmcia == NULL) {
278 1.2.8.2 yamt aupcm_slot_disable(sp);
279 1.2.8.2 yamt continue;
280 1.2.8.2 yamt }
281 1.2.8.2 yamt
282 1.2.8.2 yamt /* this makes sure we probe the slot */
283 1.2.8.2 yamt sc->sc_wake |= (1 << slot);
284 1.2.8.2 yamt }
285 1.2.8.2 yamt
286 1.2.8.2 yamt /*
287 1.2.8.2 yamt * XXX: this would be an excellent time time to establish a handler
288 1.2.8.2 yamt * for the card insertion interrupt, but that's edge triggered, and
289 1.2.8.2 yamt * au_icu.c won't support it right now. We poll in the event thread
290 1.2.8.2 yamt * for now. Start by initializing it now.
291 1.2.8.2 yamt */
292 1.2.8.3 yamt if (kthread_create(PRI_NONE, 0, NULL, aupcm_event_thread, sc,
293 1.2.8.3 yamt &sc->sc_thread, "%s", sc->sc_dev.dv_xname) != 0)
294 1.2.8.3 yamt panic("%s: unable to create event kthread",
295 1.2.8.3 yamt sc->sc_dev.dv_xname);
296 1.2.8.2 yamt }
297 1.2.8.2 yamt
298 1.2.8.2 yamt int
299 1.2.8.2 yamt aupcm_print(void *aux, const char *pnp)
300 1.2.8.2 yamt {
301 1.2.8.2 yamt struct pcmciabus_attach_args *paa = aux;
302 1.2.8.2 yamt struct aupcm_slot *sp = paa->pch;
303 1.2.8.2 yamt
304 1.2.8.2 yamt printf(" socket %d irq %d, %s", sp->as_slot, sp->as_card_irq,
305 1.2.8.2 yamt sp->as_name);
306 1.2.8.2 yamt
307 1.2.8.2 yamt return (UNCONF);
308 1.2.8.2 yamt }
309 1.2.8.2 yamt
310 1.2.8.2 yamt void *
311 1.2.8.2 yamt aupcm_intr_establish(pcmcia_chipset_handle_t pch,
312 1.2.8.2 yamt struct pcmcia_function *pf, int level, int (*handler)(void *), void *arg)
313 1.2.8.2 yamt {
314 1.2.8.2 yamt struct aupcm_slot *sp = (struct aupcm_slot *)pch;
315 1.2.8.2 yamt int s;
316 1.2.8.2 yamt
317 1.2.8.2 yamt /*
318 1.2.8.2 yamt * Hmm. perhaps this intr should be a list. well, PCMCIA
319 1.2.8.2 yamt * devices generally only have one interrupt, and so should
320 1.2.8.2 yamt * generally have only one handler. So we leave it for now.
321 1.2.8.2 yamt * (Other PCMCIA bus drivers do it this way.)
322 1.2.8.2 yamt */
323 1.2.8.2 yamt sp->as_intr = handler;
324 1.2.8.2 yamt sp->as_intrarg = arg;
325 1.2.8.4 yamt sp->as_softint = softint_establish(IPL_SOFTNET, aupcm_softintr, sp);
326 1.2.8.2 yamt
327 1.2.8.2 yamt /* set up hard interrupt handler for the card IRQs */
328 1.2.8.2 yamt s = splhigh();
329 1.2.8.2 yamt sp->as_hardint = au_intr_establish(sp->as_card_irq, 0,
330 1.2.8.2 yamt IPL_TTY, IST_LEVEL_LOW, aupcm_card_intr, sp);
331 1.2.8.2 yamt /* if card is not powered up, then leave the IRQ masked */
332 1.2.8.2 yamt if (!sp->as_enabled) {
333 1.2.8.2 yamt au_intr_disable(sp->as_card_irq);
334 1.2.8.2 yamt }
335 1.2.8.2 yamt splx(s);
336 1.2.8.2 yamt
337 1.2.8.2 yamt return (sp->as_softint);
338 1.2.8.2 yamt }
339 1.2.8.2 yamt
340 1.2.8.2 yamt void
341 1.2.8.2 yamt aupcm_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
342 1.2.8.2 yamt {
343 1.2.8.2 yamt struct aupcm_slot *sp = (struct aupcm_slot *)pch;
344 1.2.8.2 yamt
345 1.2.8.2 yamt KASSERT(sp->as_softint == ih);
346 1.2.8.2 yamt /* KASSERT(sp->as_hardint); */
347 1.2.8.2 yamt /* set up hard interrupt handler for the card IRQs */
348 1.2.8.2 yamt
349 1.2.8.2 yamt au_intr_disestablish(sp->as_hardint);
350 1.2.8.2 yamt sp->as_hardint = 0;
351 1.2.8.2 yamt
352 1.2.8.4 yamt softint_disestablish(ih);
353 1.2.8.2 yamt sp->as_softint = 0;
354 1.2.8.2 yamt sp->as_intr = NULL;
355 1.2.8.2 yamt sp->as_intrarg = NULL;
356 1.2.8.2 yamt }
357 1.2.8.2 yamt
358 1.2.8.2 yamt /*
359 1.2.8.2 yamt * FYI: Hot detach of PCMCIA is supposedly safe because H/W doesn't
360 1.2.8.2 yamt * fault on accesses to missing hardware.
361 1.2.8.2 yamt */
362 1.2.8.2 yamt void
363 1.2.8.2 yamt aupcm_event_thread(void *arg)
364 1.2.8.2 yamt {
365 1.2.8.2 yamt struct aupcm_softc *sc = arg;
366 1.2.8.2 yamt struct aupcm_slot *sp;
367 1.2.8.2 yamt int s, i, attach, detach;
368 1.2.8.2 yamt
369 1.2.8.2 yamt for (;;) {
370 1.2.8.2 yamt s = splhigh();
371 1.2.8.2 yamt if (sc->sc_wake == 0) {
372 1.2.8.2 yamt splx(s);
373 1.2.8.2 yamt /*
374 1.2.8.2 yamt * XXX: Currently, the au_icu.c lacks support
375 1.2.8.2 yamt * for edge-triggered interrupts. So we
376 1.2.8.2 yamt * cannot really use the status change
377 1.2.8.2 yamt * inerrupts. For now we poll (once per sec).
378 1.2.8.2 yamt * FYI, Linux does it this way, and they *do*
379 1.2.8.2 yamt * have support for edge triggered interrupts.
380 1.2.8.2 yamt * Go figure.
381 1.2.8.2 yamt */
382 1.2.8.2 yamt tsleep(&sc->sc_wake, PWAIT, "aupcm_event", hz);
383 1.2.8.2 yamt }
384 1.2.8.2 yamt sc->sc_wake = 0;
385 1.2.8.2 yamt
386 1.2.8.2 yamt attach = detach = 0;
387 1.2.8.2 yamt for (i = 0; i < sc->sc_nslots; i++) {
388 1.2.8.2 yamt sp = &sc->sc_slots[i];
389 1.2.8.2 yamt
390 1.2.8.2 yamt if (sc->sc_slot_status(sp->as_slot) != 0) {
391 1.2.8.2 yamt if (!sp->as_status) {
392 1.2.8.2 yamt DPRINTF(("%s: card %d insertion\n",
393 1.2.8.2 yamt sc->sc_dev.dv_xname, i));
394 1.2.8.2 yamt attach |= (1 << i);
395 1.2.8.2 yamt sp->as_status = 1;
396 1.2.8.2 yamt }
397 1.2.8.2 yamt } else {
398 1.2.8.2 yamt if (sp->as_status) {
399 1.2.8.2 yamt DPRINTF(("%s: card %d removal\n",
400 1.2.8.2 yamt sc->sc_dev.dv_xname, i));
401 1.2.8.2 yamt detach |= (1 << i);
402 1.2.8.2 yamt sp->as_status = 0;
403 1.2.8.2 yamt }
404 1.2.8.2 yamt }
405 1.2.8.2 yamt }
406 1.2.8.2 yamt splx(s);
407 1.2.8.2 yamt
408 1.2.8.2 yamt for (i = 0; i < sc->sc_nslots; i++) {
409 1.2.8.2 yamt sp = &sc->sc_slots[i];
410 1.2.8.2 yamt
411 1.2.8.2 yamt if (detach & (1 << i)) {
412 1.2.8.2 yamt aupcm_slot_disable(sp);
413 1.2.8.2 yamt pcmcia_card_detach(sp->as_pcmcia,
414 1.2.8.2 yamt DETACH_FORCE);
415 1.2.8.2 yamt } else if (attach & (1 << i)) {
416 1.2.8.2 yamt /*
417 1.2.8.2 yamt * until the function is enabled, don't
418 1.2.8.2 yamt * honor interrupts
419 1.2.8.2 yamt */
420 1.2.8.2 yamt sp->as_enabled = 0;
421 1.2.8.2 yamt au_intr_disable(sp->as_card_irq);
422 1.2.8.2 yamt pcmcia_card_attach(sp->as_pcmcia);
423 1.2.8.2 yamt }
424 1.2.8.2 yamt }
425 1.2.8.2 yamt }
426 1.2.8.2 yamt }
427 1.2.8.2 yamt
428 1.2.8.2 yamt #if 0
429 1.2.8.2 yamt void
430 1.2.8.2 yamt aupcm_status_intr(void *arg)
431 1.2.8.2 yamt {
432 1.2.8.2 yamt int s;
433 1.2.8.2 yamt struct aupcm_softc *sc = arg;
434 1.2.8.2 yamt
435 1.2.8.2 yamt s = splhigh();
436 1.2.8.2 yamt
437 1.2.8.2 yamt /* kick the status thread so it does its bit */
438 1.2.8.2 yamt sc->sc_wake = 1;
439 1.2.8.2 yamt wakeup(&sc->sc_wake);
440 1.2.8.2 yamt
441 1.2.8.2 yamt splx(s);
442 1.2.8.2 yamt }
443 1.2.8.2 yamt #endif
444 1.2.8.2 yamt
445 1.2.8.2 yamt int
446 1.2.8.2 yamt aupcm_card_intr(void *arg)
447 1.2.8.2 yamt {
448 1.2.8.2 yamt struct aupcm_slot *sp = arg;
449 1.2.8.2 yamt
450 1.2.8.2 yamt /* disable the hard interrupt for now */
451 1.2.8.2 yamt au_intr_disable(sp->as_card_irq);
452 1.2.8.2 yamt
453 1.2.8.2 yamt if (sp->as_intr != NULL) {
454 1.2.8.4 yamt softint_schedule(sp->as_softint);
455 1.2.8.2 yamt }
456 1.2.8.2 yamt
457 1.2.8.2 yamt return 1;
458 1.2.8.2 yamt }
459 1.2.8.2 yamt
460 1.2.8.2 yamt void
461 1.2.8.2 yamt aupcm_softintr(void *arg)
462 1.2.8.2 yamt {
463 1.2.8.2 yamt struct aupcm_slot *sp = arg;
464 1.2.8.2 yamt int s;
465 1.2.8.2 yamt
466 1.2.8.2 yamt sp->as_intr(sp->as_intrarg);
467 1.2.8.2 yamt
468 1.2.8.2 yamt s = splhigh();
469 1.2.8.2 yamt
470 1.2.8.2 yamt if (sp->as_intr && sp->as_enabled) {
471 1.2.8.2 yamt au_intr_enable(sp->as_card_irq);
472 1.2.8.2 yamt }
473 1.2.8.2 yamt
474 1.2.8.2 yamt splx(s);
475 1.2.8.2 yamt }
476 1.2.8.2 yamt
477 1.2.8.2 yamt void
478 1.2.8.2 yamt aupcm_slot_enable(pcmcia_chipset_handle_t pch)
479 1.2.8.2 yamt {
480 1.2.8.2 yamt struct aupcm_slot *sp = (struct aupcm_slot *)pch;
481 1.2.8.2 yamt int s;
482 1.2.8.2 yamt
483 1.2.8.2 yamt /* no interrupts while we reset the card, please */
484 1.2.8.2 yamt if (sp->as_intr)
485 1.2.8.2 yamt au_intr_disable(sp->as_card_irq);
486 1.2.8.2 yamt
487 1.2.8.2 yamt /*
488 1.2.8.2 yamt * XXX: should probably lock to make sure slot_disable and
489 1.2.8.2 yamt * enable not called together. However, i believe that the
490 1.2.8.2 yamt * event thread basically serializes them anyway.
491 1.2.8.2 yamt */
492 1.2.8.2 yamt
493 1.2.8.2 yamt sp->as_softc->sc_slot_enable(sp->as_slot);
494 1.2.8.2 yamt /* card is powered up now, honor device interrupts */
495 1.2.8.2 yamt
496 1.2.8.2 yamt s = splhigh();
497 1.2.8.2 yamt sp->as_enabled = 1;
498 1.2.8.2 yamt if (sp->as_intr)
499 1.2.8.2 yamt au_intr_enable(sp->as_card_irq);
500 1.2.8.2 yamt splx(s);
501 1.2.8.2 yamt }
502 1.2.8.2 yamt
503 1.2.8.2 yamt void
504 1.2.8.2 yamt aupcm_slot_disable(pcmcia_chipset_handle_t pch)
505 1.2.8.2 yamt {
506 1.2.8.2 yamt struct aupcm_slot *sp = (struct aupcm_slot *)pch;
507 1.2.8.2 yamt int s;
508 1.2.8.2 yamt
509 1.2.8.2 yamt s = splhigh();
510 1.2.8.2 yamt au_intr_disable(sp->as_card_irq);
511 1.2.8.2 yamt sp->as_enabled = 0;
512 1.2.8.2 yamt splx(s);
513 1.2.8.2 yamt
514 1.2.8.2 yamt sp->as_softc->sc_slot_disable(sp->as_slot);
515 1.2.8.2 yamt }
516 1.2.8.2 yamt
517 1.2.8.2 yamt void
518 1.2.8.2 yamt aupcm_slot_settype(pcmcia_chipset_handle_t pch, int type)
519 1.2.8.2 yamt {
520 1.2.8.2 yamt /* we do nothing now : type == PCMCIA_IFTYPE_IO */
521 1.2.8.2 yamt }
522 1.2.8.2 yamt
523 1.2.8.2 yamt int
524 1.2.8.2 yamt aupcm_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
525 1.2.8.2 yamt struct pcmcia_mem_handle *pcmh)
526 1.2.8.2 yamt {
527 1.2.8.2 yamt pcmh->memt = NULL;
528 1.2.8.2 yamt pcmh->size = pcmh->realsize = size;
529 1.2.8.2 yamt pcmh->addr = 0;
530 1.2.8.2 yamt pcmh->mhandle = 0;
531 1.2.8.2 yamt
532 1.2.8.2 yamt return 0;
533 1.2.8.2 yamt }
534 1.2.8.2 yamt
535 1.2.8.2 yamt void
536 1.2.8.2 yamt aupcm_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pcmh)
537 1.2.8.2 yamt {
538 1.2.8.2 yamt /* nothing to do */
539 1.2.8.2 yamt }
540 1.2.8.2 yamt
541 1.2.8.2 yamt int
542 1.2.8.2 yamt aupcm_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
543 1.2.8.2 yamt bus_size_t size, struct pcmcia_mem_handle *pcmh, bus_size_t *offsetp,
544 1.2.8.2 yamt int *windowp)
545 1.2.8.2 yamt {
546 1.2.8.2 yamt struct aupcm_slot *sp = (struct aupcm_slot *)pch;
547 1.2.8.2 yamt int win, err;
548 1.2.8.2 yamt int s;
549 1.2.8.2 yamt
550 1.2.8.2 yamt s = splhigh();
551 1.2.8.2 yamt for (win = 0; win < AUPCMCIA_NWINS; win++) {
552 1.2.8.2 yamt if (sp->as_wins[win] == NULL) {
553 1.2.8.2 yamt sp->as_wins[win] = pcmh;
554 1.2.8.2 yamt break;
555 1.2.8.2 yamt }
556 1.2.8.2 yamt }
557 1.2.8.2 yamt splx(s);
558 1.2.8.2 yamt
559 1.2.8.2 yamt if (win >= AUPCMCIA_NWINS) {
560 1.2.8.2 yamt return ENOMEM;
561 1.2.8.2 yamt }
562 1.2.8.2 yamt
563 1.2.8.2 yamt if (kind & PCMCIA_MEM_ATTR) {
564 1.2.8.2 yamt pcmh->memt = &sp->as_attrt;
565 1.2.8.2 yamt NOISY(("mapping ATTR addr %x size %x\n", (uint32_t)addr,
566 1.2.8.2 yamt (uint32_t)size));
567 1.2.8.2 yamt } else {
568 1.2.8.2 yamt pcmh->memt = &sp->as_memt;
569 1.2.8.2 yamt NOISY(("mapping MEMORY addr %x size %x\n", (uint32_t)addr,
570 1.2.8.2 yamt (uint32_t)size));
571 1.2.8.2 yamt }
572 1.2.8.2 yamt
573 1.2.8.2 yamt if ((size + addr) > (64 * 1024 * 1024))
574 1.2.8.2 yamt return EINVAL;
575 1.2.8.2 yamt
576 1.2.8.2 yamt pcmh->size = size;
577 1.2.8.2 yamt
578 1.2.8.2 yamt err = bus_space_map(pcmh->memt, addr, size, 0, &pcmh->memh);
579 1.2.8.2 yamt if (err != 0) {
580 1.2.8.2 yamt sp->as_wins[win] = NULL;
581 1.2.8.2 yamt return err;
582 1.2.8.2 yamt }
583 1.2.8.2 yamt *offsetp = 0;
584 1.2.8.2 yamt *windowp = win;
585 1.2.8.2 yamt
586 1.2.8.2 yamt return 0;
587 1.2.8.2 yamt }
588 1.2.8.2 yamt
589 1.2.8.2 yamt void
590 1.2.8.2 yamt aupcm_mem_unmap(pcmcia_chipset_handle_t pch, int win)
591 1.2.8.2 yamt {
592 1.2.8.2 yamt struct aupcm_slot *sp = (struct aupcm_slot *)pch;
593 1.2.8.2 yamt struct pcmcia_mem_handle *pcmh;
594 1.2.8.2 yamt
595 1.2.8.2 yamt pcmh = (struct pcmcia_mem_handle *)sp->as_wins[win];
596 1.2.8.2 yamt sp->as_wins[win] = NULL;
597 1.2.8.2 yamt
598 1.2.8.2 yamt NOISY(("memory umap virtual %x\n", (uint32_t)pcmh->memh));
599 1.2.8.2 yamt bus_space_unmap(pcmh->memt, pcmh->memh, pcmh->size);
600 1.2.8.2 yamt pcmh->memt = NULL;
601 1.2.8.2 yamt }
602 1.2.8.2 yamt
603 1.2.8.2 yamt int
604 1.2.8.2 yamt aupcm_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
605 1.2.8.2 yamt bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pih)
606 1.2.8.2 yamt {
607 1.2.8.2 yamt struct aupcm_slot *sp = (struct aupcm_slot *)pch;
608 1.2.8.2 yamt bus_space_handle_t bush;
609 1.2.8.2 yamt int err;
610 1.2.8.2 yamt
611 1.2.8.2 yamt pih->iot = &sp->as_iot;
612 1.2.8.2 yamt pih->size = size;
613 1.2.8.2 yamt pih->flags = 0;
614 1.2.8.2 yamt
615 1.2.8.2 yamt /*
616 1.2.8.2 yamt * start from the initial offset - this gets us a slot
617 1.2.8.2 yamt * specific address, while still leaving the addresses more or
618 1.2.8.2 yamt * less zero-based which is required for x86-style device
619 1.2.8.2 yamt * drivers.
620 1.2.8.2 yamt */
621 1.2.8.2 yamt err = bus_space_alloc(pih->iot, start, 0x100000,
622 1.2.8.2 yamt size, align, 0, 0, &pih->addr, &bush);
623 1.2.8.2 yamt NOISY(("start = %x, addr = %x, size = %x, bush = %x\n",
624 1.2.8.2 yamt (uint32_t)start, (uint32_t)pih->addr, (uint32_t)size,
625 1.2.8.2 yamt (uint32_t)bush));
626 1.2.8.2 yamt
627 1.2.8.2 yamt /* and we convert it back */
628 1.2.8.2 yamt if (err == 0) {
629 1.2.8.2 yamt pih->ihandle = (void *)bush;
630 1.2.8.2 yamt }
631 1.2.8.2 yamt
632 1.2.8.2 yamt return (err);
633 1.2.8.2 yamt }
634 1.2.8.2 yamt
635 1.2.8.2 yamt void
636 1.2.8.2 yamt aupcm_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
637 1.2.8.2 yamt {
638 1.2.8.2 yamt bus_space_free(pih->iot, (bus_space_handle_t)pih->ihandle,
639 1.2.8.2 yamt pih->size);
640 1.2.8.2 yamt }
641 1.2.8.2 yamt
642 1.2.8.2 yamt int
643 1.2.8.2 yamt aupcm_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
644 1.2.8.2 yamt bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
645 1.2.8.2 yamt {
646 1.2.8.2 yamt int err;
647 1.2.8.2 yamt
648 1.2.8.2 yamt err = bus_space_subregion(pih->iot, (bus_space_handle_t)pih->ihandle,
649 1.2.8.2 yamt offset, size, &pih->ioh);
650 1.2.8.2 yamt NOISY(("io map offset = %x, size = %x, ih = %x, hdl=%x\n",
651 1.2.8.2 yamt (uint32_t)offset, (uint32_t)size,
652 1.2.8.2 yamt (uint32_t)pih->ihandle, (uint32_t)pih->ioh));
653 1.2.8.2 yamt
654 1.2.8.2 yamt return err;
655 1.2.8.2 yamt }
656 1.2.8.2 yamt
657 1.2.8.2 yamt void
658 1.2.8.2 yamt aupcm_io_unmap(pcmcia_chipset_handle_t pch, int win)
659 1.2.8.2 yamt {
660 1.2.8.2 yamt /* We mustn't unmap/free subregion bus space! */
661 1.2.8.2 yamt NOISY(("io unmap\n"));
662 1.2.8.2 yamt }
663