1 /* $NetBSD: aupcmcia.c,v 1.5 2007/12/22 01:21:41 ad Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Written by Garrett D'Amore for Itronix Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 /* #include "opt_pci.h" */
35 /* #include "pci.h" */
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: aupcmcia.c,v 1.5 2007/12/22 01:21:41 ad Exp $");
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
47 #include <sys/device.h>
49 #include <dev/pcmcia/pcmciareg.h>
50 #include <dev/pcmcia/pcmciavar.h>
51 #include <dev/pcmcia/pcmciachip.h>
53 #include <mips/alchemy/include/au_himem_space.h>
54 #include <mips/alchemy/include/aubusvar.h>
55 #include <mips/alchemy/include/aureg.h>
56 #include <mips/alchemy/include/auvar.h>
58 #include <mips/alchemy/dev/aupcmciareg.h>
59 #include <mips/alchemy/dev/aupcmciavar.h>
62 * Borrow PCMCIADEBUG for now. Generally aupcmcia is the only PCMCIA
63 * host on these machines anyway.
67 #define DPRINTF(arg) if (aupcm_debug) printf arg
73 * And for information about mappings, etc. use this one.
76 #define NOISY(arg) printf arg
82 * Note, we use prefix "aupcm" instead of "aupcmcia", even though our
83 * driver is the latter, mostly because my fingers have trouble typing
84 * the former. "aupcm" should be sufficiently unique to avoid
88 static int aupcm_mem_alloc(pcmcia_chipset_handle_t
, bus_size_t
,
89 struct pcmcia_mem_handle
*);
90 static void aupcm_mem_free(pcmcia_chipset_handle_t
,
91 struct pcmcia_mem_handle
*);
92 static int aupcm_mem_map(pcmcia_chipset_handle_t
, int, bus_addr_t
,
93 bus_size_t
, struct pcmcia_mem_handle
*, bus_size_t
*, int *);
94 static void aupcm_mem_unmap(pcmcia_chipset_handle_t
, int);
96 static int aupcm_io_alloc(pcmcia_chipset_handle_t
, bus_addr_t
, bus_size_t
,
97 bus_size_t
, struct pcmcia_io_handle
*);
98 static void aupcm_io_free(pcmcia_chipset_handle_t
, struct pcmcia_io_handle
*);
99 static int aupcm_io_map(pcmcia_chipset_handle_t
, int, bus_addr_t
,
100 bus_size_t
, struct pcmcia_io_handle
*, int *);
101 static void aupcm_io_unmap(pcmcia_chipset_handle_t
, int);
102 static void *aupcm_intr_establish(pcmcia_chipset_handle_t
,
103 struct pcmcia_function
*, int, int (*)(void *), void *);
104 static void aupcm_intr_disestablish(pcmcia_chipset_handle_t
, void *);
106 static void aupcm_slot_enable(pcmcia_chipset_handle_t
);
107 static void aupcm_slot_disable(pcmcia_chipset_handle_t
);
108 static void aupcm_slot_settype(pcmcia_chipset_handle_t
, int);
110 static int aupcm_match(struct device
*, struct cfdata
*, void *);
111 static void aupcm_attach(struct device
*, struct device
*, void *);
113 static void aupcm_event_thread(void *);
114 static int aupcm_card_intr(void *);
115 static void aupcm_softintr(void *);
116 static int aupcm_print(void *, const char *);
119 struct aupcm_softc
*as_softc
;
123 int (*as_intr
)(void *);
130 bus_addr_t as_offset
;
131 struct mips_bus_space as_iot
;
132 struct mips_bus_space as_attrt
;
133 struct mips_bus_space as_memt
;
134 void *as_wins
[AUPCMCIA_NWINS
];
136 struct device
*as_pcmcia
;
139 /* this structure needs to be exposed... */
141 struct device sc_dev
;
142 pcmcia_chipset_tag_t sc_pct
;
144 void (*sc_slot_enable
)(int);
145 void (*sc_slot_disable
)(int);
146 int (*sc_slot_status
)(int);
154 struct aupcm_slot sc_slots
[AUPCMCIA_NSLOTS
];
157 static struct pcmcia_chip_functions aupcm_functions
= {
168 aupcm_intr_establish
,
169 aupcm_intr_disestablish
,
176 static struct mips_bus_space aupcm_memt
;
178 CFATTACH_DECL(aupcmcia
, sizeof (struct aupcm_softc
),
179 aupcm_match
, aupcm_attach
, NULL
, NULL
);
182 aupcm_match(struct device
*parent
, struct cfdata
*cf
, void *aux
)
184 struct aubus_attach_args
*aa
= aux
;
185 static int found
= 0;
190 if (strcmp(aa
->aa_name
, "aupcmcia") != 0)
199 aupcm_attach(struct device
*parent
, struct device
*self
, void *aux
)
201 /* struct aubus_attach_args *aa = aux; */
202 struct aupcm_softc
*sc
= (struct aupcm_softc
*)self
;
205 struct aupcmcia_machdep
*md
;
207 /* initialize bus space */
209 /* there can be only one. */
215 * PCMCIA memory can live within pretty much the entire 32-bit
216 * space, modulo 64 MB wraps. We don't have to support coexisting
219 au_himem_space_init(&aupcm_memt
, "pcmciamem",
220 PCMCIA_BASE
, AUPCMCIA_ATTR_OFFSET
, 0xffffffff,
221 AU_HIMEM_SPACE_LITTLE_ENDIAN
);
223 if ((md
= aupcmcia_machdep()) == NULL
) {
224 printf("\n%s:unable to get machdep structure\n",
225 sc
->sc_dev
.dv_xname
);
229 sc
->sc_nslots
= md
->am_nslots
;
230 sc
->sc_slot_enable
= md
->am_slot_enable
;
231 sc
->sc_slot_disable
= md
->am_slot_disable
;
232 sc
->sc_slot_status
= md
->am_slot_status
;
234 printf(": Alchemy PCMCIA, %d slots\n", sc
->sc_nslots
);
236 sc
->sc_pct
= (pcmcia_chipset_tag_t
)&aupcm_functions
;
238 for (slot
= 0; slot
< sc
->sc_nslots
; slot
++) {
239 struct aupcm_slot
*sp
;
240 struct pcmciabus_attach_args paa
;
242 sp
= &sc
->sc_slots
[slot
];
246 sp
->as_name
= md
->am_slot_name(slot
);
247 sp
->as_offset
= md
->am_slot_offset(slot
);
248 sp
->as_card_irq
= md
->am_slot_irq(slot
, AUPCMCIA_IRQ_CARD
);
249 sp
->as_status_irq
= md
->am_slot_irq(slot
,
250 AUPCMCIA_IRQ_INSERT
);
252 au_himem_space_init(&sp
->as_attrt
, "pcmciaattr",
253 PCMCIA_BASE
+ sp
->as_offset
+ AUPCMCIA_ATTR_OFFSET
,
254 0, AUPCMCIA_MAP_SIZE
, AU_HIMEM_SPACE_LITTLE_ENDIAN
);
256 au_himem_space_init(&sp
->as_memt
, "pcmciamem",
257 PCMCIA_BASE
+ sp
->as_offset
+ AUPCMCIA_MEM_OFFSET
,
258 0, AUPCMCIA_MAP_SIZE
, AU_HIMEM_SPACE_LITTLE_ENDIAN
);
260 au_himem_space_init(&sp
->as_iot
, "pcmciaio",
261 PCMCIA_BASE
+ sp
->as_offset
+ AUPCMCIA_IO_OFFSET
,
262 0, AUPCMCIA_MAP_SIZE
,
263 AU_HIMEM_SPACE_LITTLE_ENDIAN
| AU_HIMEM_SPACE_IO
);
267 paa
.paa_busname
= "pcmcia";
268 paa
.pct
= sc
->sc_pct
;
269 paa
.pch
= (pcmcia_chipset_handle_t
)sp
;
272 paa
.iosize
= AUPCMCIA_MAP_SIZE
;
274 sp
->as_pcmcia
= config_found(&sc
->sc_dev
, &paa
, aupcm_print
);
276 /* if no pcmcia, make sure slot is powered down */
277 if (sp
->as_pcmcia
== NULL
) {
278 aupcm_slot_disable(sp
);
282 /* this makes sure we probe the slot */
283 sc
->sc_wake
|= (1 << slot
);
287 * XXX: this would be an excellent time time to establish a handler
288 * for the card insertion interrupt, but that's edge triggered, and
289 * au_icu.c won't support it right now. We poll in the event thread
290 * for now. Start by initializing it now.
292 if (kthread_create(PRI_NONE
, 0, NULL
, aupcm_event_thread
, sc
,
293 &sc
->sc_thread
, "%s", sc
->sc_dev
.dv_xname
) != 0)
294 panic("%s: unable to create event kthread",
295 sc
->sc_dev
.dv_xname
);
299 aupcm_print(void *aux
, const char *pnp
)
301 struct pcmciabus_attach_args
*paa
= aux
;
302 struct aupcm_slot
*sp
= paa
->pch
;
304 printf(" socket %d irq %d, %s", sp
->as_slot
, sp
->as_card_irq
,
311 aupcm_intr_establish(pcmcia_chipset_handle_t pch
,
312 struct pcmcia_function
*pf
, int level
, int (*handler
)(void *), void *arg
)
314 struct aupcm_slot
*sp
= (struct aupcm_slot
*)pch
;
318 * Hmm. perhaps this intr should be a list. well, PCMCIA
319 * devices generally only have one interrupt, and so should
320 * generally have only one handler. So we leave it for now.
321 * (Other PCMCIA bus drivers do it this way.)
323 sp
->as_intr
= handler
;
324 sp
->as_intrarg
= arg
;
325 sp
->as_softint
= softint_establish(IPL_SOFTNET
, aupcm_softintr
, sp
);
327 /* set up hard interrupt handler for the card IRQs */
329 sp
->as_hardint
= au_intr_establish(sp
->as_card_irq
, 0,
330 IPL_TTY
, IST_LEVEL_LOW
, aupcm_card_intr
, sp
);
331 /* if card is not powered up, then leave the IRQ masked */
332 if (!sp
->as_enabled
) {
333 au_intr_disable(sp
->as_card_irq
);
337 return (sp
->as_softint
);
341 aupcm_intr_disestablish(pcmcia_chipset_handle_t pch
, void *ih
)
343 struct aupcm_slot
*sp
= (struct aupcm_slot
*)pch
;
345 KASSERT(sp
->as_softint
== ih
);
346 /* KASSERT(sp->as_hardint); */
347 /* set up hard interrupt handler for the card IRQs */
349 au_intr_disestablish(sp
->as_hardint
);
352 softint_disestablish(ih
);
355 sp
->as_intrarg
= NULL
;
359 * FYI: Hot detach of PCMCIA is supposedly safe because H/W doesn't
360 * fault on accesses to missing hardware.
363 aupcm_event_thread(void *arg
)
365 struct aupcm_softc
*sc
= arg
;
366 struct aupcm_slot
*sp
;
367 int s
, i
, attach
, detach
;
371 if (sc
->sc_wake
== 0) {
374 * XXX: Currently, the au_icu.c lacks support
375 * for edge-triggered interrupts. So we
376 * cannot really use the status change
377 * inerrupts. For now we poll (once per sec).
378 * FYI, Linux does it this way, and they *do*
379 * have support for edge triggered interrupts.
382 tsleep(&sc
->sc_wake
, PWAIT
, "aupcm_event", hz
);
387 for (i
= 0; i
< sc
->sc_nslots
; i
++) {
388 sp
= &sc
->sc_slots
[i
];
390 if (sc
->sc_slot_status(sp
->as_slot
) != 0) {
391 if (!sp
->as_status
) {
392 DPRINTF(("%s: card %d insertion\n",
393 sc
->sc_dev
.dv_xname
, i
));
399 DPRINTF(("%s: card %d removal\n",
400 sc
->sc_dev
.dv_xname
, i
));
408 for (i
= 0; i
< sc
->sc_nslots
; i
++) {
409 sp
= &sc
->sc_slots
[i
];
411 if (detach
& (1 << i
)) {
412 aupcm_slot_disable(sp
);
413 pcmcia_card_detach(sp
->as_pcmcia
,
415 } else if (attach
& (1 << i
)) {
417 * until the function is enabled, don't
421 au_intr_disable(sp
->as_card_irq
);
422 pcmcia_card_attach(sp
->as_pcmcia
);
430 aupcm_status_intr(void *arg
)
433 struct aupcm_softc
*sc
= arg
;
437 /* kick the status thread so it does its bit */
439 wakeup(&sc
->sc_wake
);
446 aupcm_card_intr(void *arg
)
448 struct aupcm_slot
*sp
= arg
;
450 /* disable the hard interrupt for now */
451 au_intr_disable(sp
->as_card_irq
);
453 if (sp
->as_intr
!= NULL
) {
454 softint_schedule(sp
->as_softint
);
461 aupcm_softintr(void *arg
)
463 struct aupcm_slot
*sp
= arg
;
466 sp
->as_intr(sp
->as_intrarg
);
470 if (sp
->as_intr
&& sp
->as_enabled
) {
471 au_intr_enable(sp
->as_card_irq
);
478 aupcm_slot_enable(pcmcia_chipset_handle_t pch
)
480 struct aupcm_slot
*sp
= (struct aupcm_slot
*)pch
;
483 /* no interrupts while we reset the card, please */
485 au_intr_disable(sp
->as_card_irq
);
488 * XXX: should probably lock to make sure slot_disable and
489 * enable not called together. However, i believe that the
490 * event thread basically serializes them anyway.
493 sp
->as_softc
->sc_slot_enable(sp
->as_slot
);
494 /* card is powered up now, honor device interrupts */
499 au_intr_enable(sp
->as_card_irq
);
504 aupcm_slot_disable(pcmcia_chipset_handle_t pch
)
506 struct aupcm_slot
*sp
= (struct aupcm_slot
*)pch
;
510 au_intr_disable(sp
->as_card_irq
);
514 sp
->as_softc
->sc_slot_disable(sp
->as_slot
);
518 aupcm_slot_settype(pcmcia_chipset_handle_t pch
, int type
)
520 /* we do nothing now : type == PCMCIA_IFTYPE_IO */
524 aupcm_mem_alloc(pcmcia_chipset_handle_t pch
, bus_size_t size
,
525 struct pcmcia_mem_handle
*pcmh
)
528 pcmh
->size
= pcmh
->realsize
= size
;
536 aupcm_mem_free(pcmcia_chipset_handle_t pch
, struct pcmcia_mem_handle
*pcmh
)
542 aupcm_mem_map(pcmcia_chipset_handle_t pch
, int kind
, bus_addr_t addr
,
543 bus_size_t size
, struct pcmcia_mem_handle
*pcmh
, bus_size_t
*offsetp
,
546 struct aupcm_slot
*sp
= (struct aupcm_slot
*)pch
;
551 for (win
= 0; win
< AUPCMCIA_NWINS
; win
++) {
552 if (sp
->as_wins
[win
] == NULL
) {
553 sp
->as_wins
[win
] = pcmh
;
559 if (win
>= AUPCMCIA_NWINS
) {
563 if (kind
& PCMCIA_MEM_ATTR
) {
564 pcmh
->memt
= &sp
->as_attrt
;
565 NOISY(("mapping ATTR addr %x size %x\n", (uint32_t)addr
,
568 pcmh
->memt
= &sp
->as_memt
;
569 NOISY(("mapping MEMORY addr %x size %x\n", (uint32_t)addr
,
573 if ((size
+ addr
) > (64 * 1024 * 1024))
578 err
= bus_space_map(pcmh
->memt
, addr
, size
, 0, &pcmh
->memh
);
580 sp
->as_wins
[win
] = NULL
;
590 aupcm_mem_unmap(pcmcia_chipset_handle_t pch
, int win
)
592 struct aupcm_slot
*sp
= (struct aupcm_slot
*)pch
;
593 struct pcmcia_mem_handle
*pcmh
;
595 pcmh
= (struct pcmcia_mem_handle
*)sp
->as_wins
[win
];
596 sp
->as_wins
[win
] = NULL
;
598 NOISY(("memory umap virtual %x\n", (uint32_t)pcmh
->memh
));
599 bus_space_unmap(pcmh
->memt
, pcmh
->memh
, pcmh
->size
);
604 aupcm_io_alloc(pcmcia_chipset_handle_t pch
, bus_addr_t start
,
605 bus_size_t size
, bus_size_t align
, struct pcmcia_io_handle
*pih
)
607 struct aupcm_slot
*sp
= (struct aupcm_slot
*)pch
;
608 bus_space_handle_t bush
;
611 pih
->iot
= &sp
->as_iot
;
616 * start from the initial offset - this gets us a slot
617 * specific address, while still leaving the addresses more or
618 * less zero-based which is required for x86-style device
621 err
= bus_space_alloc(pih
->iot
, start
, 0x100000,
622 size
, align
, 0, 0, &pih
->addr
, &bush
);
623 NOISY(("start = %x, addr = %x, size = %x, bush = %x\n",
624 (uint32_t)start
, (uint32_t)pih
->addr
, (uint32_t)size
,
627 /* and we convert it back */
629 pih
->ihandle
= (void *)bush
;
636 aupcm_io_free(pcmcia_chipset_handle_t pch
, struct pcmcia_io_handle
*pih
)
638 bus_space_free(pih
->iot
, (bus_space_handle_t
)pih
->ihandle
,
643 aupcm_io_map(pcmcia_chipset_handle_t pch
, int width
, bus_addr_t offset
,
644 bus_size_t size
, struct pcmcia_io_handle
*pih
, int *windowp
)
648 err
= bus_space_subregion(pih
->iot
, (bus_space_handle_t
)pih
->ihandle
,
649 offset
, size
, &pih
->ioh
);
650 NOISY(("io map offset = %x, size = %x, ih = %x, hdl=%x\n",
651 (uint32_t)offset
, (uint32_t)size
,
652 (uint32_t)pih
->ihandle
, (uint32_t)pih
->ioh
));
658 aupcm_io_unmap(pcmcia_chipset_handle_t pch
, int win
)
660 /* We mustn't unmap/free subregion bus space! */
661 NOISY(("io unmap\n"));