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