1*9cc225e6Sandvar /* $NetBSD: aupcmcia.c,v 1.13 2022/09/25 12:41:46 andvar Exp $ */
26dd67e32Sgdamore
36dd67e32Sgdamore /*-
46dd67e32Sgdamore * Copyright (c) 2006 Itronix Inc.
56dd67e32Sgdamore * All rights reserved.
66dd67e32Sgdamore *
76dd67e32Sgdamore * Written by Garrett D'Amore for Itronix Inc.
86dd67e32Sgdamore *
96dd67e32Sgdamore * Redistribution and use in source and binary forms, with or without
106dd67e32Sgdamore * modification, are permitted provided that the following conditions
116dd67e32Sgdamore * are met:
126dd67e32Sgdamore * 1. Redistributions of source code must retain the above copyright
136dd67e32Sgdamore * notice, this list of conditions and the following disclaimer.
146dd67e32Sgdamore * 2. Redistributions in binary form must reproduce the above copyright
156dd67e32Sgdamore * notice, this list of conditions and the following disclaimer in the
166dd67e32Sgdamore * documentation and/or other materials provided with the distribution.
176dd67e32Sgdamore * 3. The name of Itronix Inc. may not be used to endorse
186dd67e32Sgdamore * or promote products derived from this software without specific
196dd67e32Sgdamore * prior written permission.
206dd67e32Sgdamore *
216dd67e32Sgdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
226dd67e32Sgdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
236dd67e32Sgdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
246dd67e32Sgdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
256dd67e32Sgdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
266dd67e32Sgdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
276dd67e32Sgdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286dd67e32Sgdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
296dd67e32Sgdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
306dd67e32Sgdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
316dd67e32Sgdamore * POSSIBILITY OF SUCH DAMAGE.
326dd67e32Sgdamore */
336dd67e32Sgdamore
346dd67e32Sgdamore /* #include "opt_pci.h" */
356dd67e32Sgdamore /* #include "pci.h" */
366dd67e32Sgdamore
376dd67e32Sgdamore #include <sys/cdefs.h>
38*9cc225e6Sandvar __KERNEL_RCSID(0, "$NetBSD: aupcmcia.c,v 1.13 2022/09/25 12:41:46 andvar Exp $");
396dd67e32Sgdamore
406dd67e32Sgdamore #include <sys/types.h>
416dd67e32Sgdamore #include <sys/param.h>
426dd67e32Sgdamore #include <sys/systm.h>
436dd67e32Sgdamore #include <sys/errno.h>
446dd67e32Sgdamore #include <sys/kernel.h>
456dd67e32Sgdamore #include <sys/kthread.h>
468044d721Sad #include <sys/intr.h>
47b24bad57Sdogcow #include <sys/device.h>
486dd67e32Sgdamore
496dd67e32Sgdamore #include <dev/pcmcia/pcmciareg.h>
506dd67e32Sgdamore #include <dev/pcmcia/pcmciavar.h>
516dd67e32Sgdamore #include <dev/pcmcia/pcmciachip.h>
526dd67e32Sgdamore
536dd67e32Sgdamore #include <mips/alchemy/include/au_himem_space.h>
546dd67e32Sgdamore #include <mips/alchemy/include/aubusvar.h>
556dd67e32Sgdamore #include <mips/alchemy/include/aureg.h>
566dd67e32Sgdamore #include <mips/alchemy/include/auvar.h>
576dd67e32Sgdamore
586dd67e32Sgdamore #include <mips/alchemy/dev/aupcmciareg.h>
596dd67e32Sgdamore #include <mips/alchemy/dev/aupcmciavar.h>
606dd67e32Sgdamore
616dd67e32Sgdamore /*
626dd67e32Sgdamore * Borrow PCMCIADEBUG for now. Generally aupcmcia is the only PCMCIA
636dd67e32Sgdamore * host on these machines anyway.
646dd67e32Sgdamore */
656dd67e32Sgdamore #ifdef PCMCIADEBUG
666dd67e32Sgdamore int aupcm_debug = 1;
676dd67e32Sgdamore #define DPRINTF(arg) if (aupcm_debug) printf arg
686dd67e32Sgdamore #else
696dd67e32Sgdamore #define DPRINTF(arg)
706dd67e32Sgdamore #endif
716dd67e32Sgdamore
726dd67e32Sgdamore /*
736dd67e32Sgdamore * And for information about mappings, etc. use this one.
746dd67e32Sgdamore */
756dd67e32Sgdamore #ifdef AUPCMCIANOISY
766dd67e32Sgdamore #define NOISY(arg) printf arg
776dd67e32Sgdamore #else
786dd67e32Sgdamore #define NOISY(arg)
796dd67e32Sgdamore #endif
806dd67e32Sgdamore
816dd67e32Sgdamore /*
826dd67e32Sgdamore * Note, we use prefix "aupcm" instead of "aupcmcia", even though our
836dd67e32Sgdamore * driver is the latter, mostly because my fingers have trouble typing
846dd67e32Sgdamore * the former. "aupcm" should be sufficiently unique to avoid
856dd67e32Sgdamore * confusion.
866dd67e32Sgdamore */
876dd67e32Sgdamore
886dd67e32Sgdamore static int aupcm_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
896dd67e32Sgdamore struct pcmcia_mem_handle *);
906dd67e32Sgdamore static void aupcm_mem_free(pcmcia_chipset_handle_t,
916dd67e32Sgdamore struct pcmcia_mem_handle *);
926dd67e32Sgdamore static int aupcm_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
936dd67e32Sgdamore bus_size_t, struct pcmcia_mem_handle *, bus_size_t *, int *);
946dd67e32Sgdamore static void aupcm_mem_unmap(pcmcia_chipset_handle_t, int);
956dd67e32Sgdamore
966dd67e32Sgdamore static int aupcm_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
976dd67e32Sgdamore bus_size_t, struct pcmcia_io_handle *);
986dd67e32Sgdamore static void aupcm_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
996dd67e32Sgdamore static int aupcm_io_map(pcmcia_chipset_handle_t, int, bus_addr_t,
1006dd67e32Sgdamore bus_size_t, struct pcmcia_io_handle *, int *);
1016dd67e32Sgdamore static void aupcm_io_unmap(pcmcia_chipset_handle_t, int);
1026dd67e32Sgdamore static void *aupcm_intr_establish(pcmcia_chipset_handle_t,
1036dd67e32Sgdamore struct pcmcia_function *, int, int (*)(void *), void *);
1046dd67e32Sgdamore static void aupcm_intr_disestablish(pcmcia_chipset_handle_t, void *);
1056dd67e32Sgdamore
1066dd67e32Sgdamore static void aupcm_slot_enable(pcmcia_chipset_handle_t);
1076dd67e32Sgdamore static void aupcm_slot_disable(pcmcia_chipset_handle_t);
1086dd67e32Sgdamore static void aupcm_slot_settype(pcmcia_chipset_handle_t, int);
1096dd67e32Sgdamore
110f58fcf6aSkiyohara static int aupcm_match(device_t, struct cfdata *, void *);
111f58fcf6aSkiyohara static void aupcm_attach(device_t, device_t, void *);
1126dd67e32Sgdamore
1136dd67e32Sgdamore static void aupcm_event_thread(void *);
1146dd67e32Sgdamore static int aupcm_card_intr(void *);
1156dd67e32Sgdamore static void aupcm_softintr(void *);
1166dd67e32Sgdamore static int aupcm_print(void *, const char *);
1176dd67e32Sgdamore
1186dd67e32Sgdamore struct aupcm_slot {
1196dd67e32Sgdamore struct aupcm_softc *as_softc;
1206dd67e32Sgdamore int as_slot;
1216dd67e32Sgdamore int as_status;
1226dd67e32Sgdamore int as_enabled;
1236dd67e32Sgdamore int (*as_intr)(void *);
1246dd67e32Sgdamore int as_card_irq;
1256dd67e32Sgdamore int as_status_irq;
1266dd67e32Sgdamore void *as_intrarg;
1276dd67e32Sgdamore void *as_softint;
1286dd67e32Sgdamore void *as_hardint;
1296dd67e32Sgdamore const char *as_name;
1306dd67e32Sgdamore bus_addr_t as_offset;
1316dd67e32Sgdamore struct mips_bus_space as_iot;
1326dd67e32Sgdamore struct mips_bus_space as_attrt;
1336dd67e32Sgdamore struct mips_bus_space as_memt;
1346dd67e32Sgdamore void *as_wins[AUPCMCIA_NWINS];
1356dd67e32Sgdamore
136f58fcf6aSkiyohara device_t as_pcmcia;
1376dd67e32Sgdamore };
1386dd67e32Sgdamore
1396dd67e32Sgdamore /* this structure needs to be exposed... */
1406dd67e32Sgdamore struct aupcm_softc {
141f58fcf6aSkiyohara device_t sc_dev;
1426dd67e32Sgdamore pcmcia_chipset_tag_t sc_pct;
1436dd67e32Sgdamore
1446dd67e32Sgdamore void (*sc_slot_enable)(int);
1456dd67e32Sgdamore void (*sc_slot_disable)(int);
1466dd67e32Sgdamore int (*sc_slot_status)(int);
1476dd67e32Sgdamore
1486dd67e32Sgdamore paddr_t sc_base;
1496dd67e32Sgdamore
1506dd67e32Sgdamore int sc_wake;
15188ab7da9Sad lwp_t *sc_thread;
1526dd67e32Sgdamore
1536dd67e32Sgdamore int sc_nslots;
1546dd67e32Sgdamore struct aupcm_slot sc_slots[AUPCMCIA_NSLOTS];
1556dd67e32Sgdamore };
1566dd67e32Sgdamore
1576dd67e32Sgdamore static struct pcmcia_chip_functions aupcm_functions = {
1586dd67e32Sgdamore aupcm_mem_alloc,
1596dd67e32Sgdamore aupcm_mem_free,
1606dd67e32Sgdamore aupcm_mem_map,
1616dd67e32Sgdamore aupcm_mem_unmap,
1626dd67e32Sgdamore
1636dd67e32Sgdamore aupcm_io_alloc,
1646dd67e32Sgdamore aupcm_io_free,
1656dd67e32Sgdamore aupcm_io_map,
1666dd67e32Sgdamore aupcm_io_unmap,
1676dd67e32Sgdamore
1686dd67e32Sgdamore aupcm_intr_establish,
1696dd67e32Sgdamore aupcm_intr_disestablish,
1706dd67e32Sgdamore
1716dd67e32Sgdamore aupcm_slot_enable,
1726dd67e32Sgdamore aupcm_slot_disable,
1736dd67e32Sgdamore aupcm_slot_settype,
1746dd67e32Sgdamore };
1756dd67e32Sgdamore
1766dd67e32Sgdamore static struct mips_bus_space aupcm_memt;
1776dd67e32Sgdamore
178f58fcf6aSkiyohara CFATTACH_DECL_NEW(aupcmcia, sizeof (struct aupcm_softc),
1796dd67e32Sgdamore aupcm_match, aupcm_attach, NULL, NULL);
1806dd67e32Sgdamore
1816dd67e32Sgdamore int
aupcm_match(device_t parent,struct cfdata * cf,void * aux)182f58fcf6aSkiyohara aupcm_match(device_t parent, struct cfdata *cf, void *aux)
1836dd67e32Sgdamore {
1846dd67e32Sgdamore struct aubus_attach_args *aa = aux;
1856dd67e32Sgdamore static int found = 0;
1866dd67e32Sgdamore
1876dd67e32Sgdamore if (found)
1886dd67e32Sgdamore return 0;
1896dd67e32Sgdamore
1906dd67e32Sgdamore if (strcmp(aa->aa_name, "aupcmcia") != 0)
1916dd67e32Sgdamore return 0;
1926dd67e32Sgdamore
1936dd67e32Sgdamore found = 1;
1946dd67e32Sgdamore
1956dd67e32Sgdamore return 1;
1966dd67e32Sgdamore }
1976dd67e32Sgdamore
1986dd67e32Sgdamore void
aupcm_attach(device_t parent,device_t self,void * aux)199f58fcf6aSkiyohara aupcm_attach(device_t parent, device_t self, void *aux)
2006dd67e32Sgdamore {
2016dd67e32Sgdamore /* struct aubus_attach_args *aa = aux; */
202f58fcf6aSkiyohara struct aupcm_softc *sc = device_private(self);
2036dd67e32Sgdamore static int done = 0;
2046dd67e32Sgdamore int slot;
2056dd67e32Sgdamore struct aupcmcia_machdep *md;
2066dd67e32Sgdamore
207f58fcf6aSkiyohara sc->sc_dev = self;
208f58fcf6aSkiyohara
2096dd67e32Sgdamore /* initialize bus space */
2106dd67e32Sgdamore if (done) {
2116dd67e32Sgdamore /* there can be only one. */
2126dd67e32Sgdamore return;
2136dd67e32Sgdamore }
2146dd67e32Sgdamore
2156dd67e32Sgdamore done = 1;
2166dd67e32Sgdamore /*
2176dd67e32Sgdamore * PCMCIA memory can live within pretty much the entire 32-bit
2186dd67e32Sgdamore * space, modulo 64 MB wraps. We don't have to support coexisting
2196dd67e32Sgdamore * DMA.
2206dd67e32Sgdamore */
2216dd67e32Sgdamore au_himem_space_init(&aupcm_memt, "pcmciamem",
2226dd67e32Sgdamore PCMCIA_BASE, AUPCMCIA_ATTR_OFFSET, 0xffffffff,
2236dd67e32Sgdamore AU_HIMEM_SPACE_LITTLE_ENDIAN);
2246dd67e32Sgdamore
2256dd67e32Sgdamore if ((md = aupcmcia_machdep()) == NULL) {
226f58fcf6aSkiyohara aprint_error(": unable to get machdep structure\n");
2276dd67e32Sgdamore return;
2286dd67e32Sgdamore }
2296dd67e32Sgdamore
2306dd67e32Sgdamore sc->sc_nslots = md->am_nslots;
2316dd67e32Sgdamore sc->sc_slot_enable = md->am_slot_enable;
2326dd67e32Sgdamore sc->sc_slot_disable = md->am_slot_disable;
2336dd67e32Sgdamore sc->sc_slot_status = md->am_slot_status;
2346dd67e32Sgdamore
235f58fcf6aSkiyohara aprint_normal(": Alchemy PCMCIA, %d slots\n", sc->sc_nslots);
236f58fcf6aSkiyohara aprint_naive("\n");
2376dd67e32Sgdamore
2386dd67e32Sgdamore sc->sc_pct = (pcmcia_chipset_tag_t)&aupcm_functions;
2396dd67e32Sgdamore
2406dd67e32Sgdamore for (slot = 0; slot < sc->sc_nslots; slot++) {
2416dd67e32Sgdamore struct aupcm_slot *sp;
2426dd67e32Sgdamore struct pcmciabus_attach_args paa;
2436dd67e32Sgdamore
2446dd67e32Sgdamore sp = &sc->sc_slots[slot];
2456dd67e32Sgdamore sp->as_softc = sc;
2466dd67e32Sgdamore
2476dd67e32Sgdamore sp->as_slot = slot;
2486dd67e32Sgdamore sp->as_name = md->am_slot_name(slot);
2496dd67e32Sgdamore sp->as_offset = md->am_slot_offset(slot);
2506dd67e32Sgdamore sp->as_card_irq = md->am_slot_irq(slot, AUPCMCIA_IRQ_CARD);
2516dd67e32Sgdamore sp->as_status_irq = md->am_slot_irq(slot,
2526dd67e32Sgdamore AUPCMCIA_IRQ_INSERT);
2536dd67e32Sgdamore
2546dd67e32Sgdamore au_himem_space_init(&sp->as_attrt, "pcmciaattr",
2556dd67e32Sgdamore PCMCIA_BASE + sp->as_offset + AUPCMCIA_ATTR_OFFSET,
2566dd67e32Sgdamore 0, AUPCMCIA_MAP_SIZE, AU_HIMEM_SPACE_LITTLE_ENDIAN);
2576dd67e32Sgdamore
2586dd67e32Sgdamore au_himem_space_init(&sp->as_memt, "pcmciamem",
2596dd67e32Sgdamore PCMCIA_BASE + sp->as_offset + AUPCMCIA_MEM_OFFSET,
2606dd67e32Sgdamore 0, AUPCMCIA_MAP_SIZE, AU_HIMEM_SPACE_LITTLE_ENDIAN);
2616dd67e32Sgdamore
2626dd67e32Sgdamore au_himem_space_init(&sp->as_iot, "pcmciaio",
2636dd67e32Sgdamore PCMCIA_BASE + sp->as_offset + AUPCMCIA_IO_OFFSET,
2646dd67e32Sgdamore 0, AUPCMCIA_MAP_SIZE,
2656dd67e32Sgdamore AU_HIMEM_SPACE_LITTLE_ENDIAN | AU_HIMEM_SPACE_IO);
2666dd67e32Sgdamore
2676dd67e32Sgdamore sp->as_status = 0;
2686dd67e32Sgdamore
2696dd67e32Sgdamore paa.paa_busname = "pcmcia";
2706dd67e32Sgdamore paa.pct = sc->sc_pct;
2716dd67e32Sgdamore paa.pch = (pcmcia_chipset_handle_t)sp;
2726dd67e32Sgdamore
2732685996bSthorpej sp->as_pcmcia = config_found(self, &paa, aupcm_print,
274c7fb772bSthorpej CFARGS_NONE);
2756dd67e32Sgdamore
2766dd67e32Sgdamore /* if no pcmcia, make sure slot is powered down */
2776dd67e32Sgdamore if (sp->as_pcmcia == NULL) {
2786dd67e32Sgdamore aupcm_slot_disable(sp);
2796dd67e32Sgdamore continue;
2806dd67e32Sgdamore }
2816dd67e32Sgdamore
2826dd67e32Sgdamore /* this makes sure we probe the slot */
2836dd67e32Sgdamore sc->sc_wake |= (1 << slot);
2846dd67e32Sgdamore }
2856dd67e32Sgdamore
2866dd67e32Sgdamore /*
2876dd67e32Sgdamore * XXX: this would be an excellent time time to establish a handler
2886dd67e32Sgdamore * for the card insertion interrupt, but that's edge triggered, and
2896dd67e32Sgdamore * au_icu.c won't support it right now. We poll in the event thread
2906dd67e32Sgdamore * for now. Start by initializing it now.
2916dd67e32Sgdamore */
29288ab7da9Sad if (kthread_create(PRI_NONE, 0, NULL, aupcm_event_thread, sc,
293f58fcf6aSkiyohara &sc->sc_thread, "%s", device_xname(sc->sc_dev)) != 0)
29488ab7da9Sad panic("%s: unable to create event kthread",
295f58fcf6aSkiyohara device_xname(sc->sc_dev));
2966dd67e32Sgdamore }
2976dd67e32Sgdamore
2986dd67e32Sgdamore int
aupcm_print(void * aux,const char * pnp)2996dd67e32Sgdamore aupcm_print(void *aux, const char *pnp)
3006dd67e32Sgdamore {
3016dd67e32Sgdamore struct pcmciabus_attach_args *paa = aux;
3026dd67e32Sgdamore struct aupcm_slot *sp = paa->pch;
3036dd67e32Sgdamore
3046dd67e32Sgdamore printf(" socket %d irq %d, %s", sp->as_slot, sp->as_card_irq,
3056dd67e32Sgdamore sp->as_name);
3066dd67e32Sgdamore
3076dd67e32Sgdamore return (UNCONF);
3086dd67e32Sgdamore }
3096dd67e32Sgdamore
3106dd67e32Sgdamore void *
aupcm_intr_establish(pcmcia_chipset_handle_t pch,struct pcmcia_function * pf,int level,int (* handler)(void *),void * arg)3116dd67e32Sgdamore aupcm_intr_establish(pcmcia_chipset_handle_t pch,
3126dd67e32Sgdamore struct pcmcia_function *pf, int level, int (*handler)(void *), void *arg)
3136dd67e32Sgdamore {
3146dd67e32Sgdamore struct aupcm_slot *sp = (struct aupcm_slot *)pch;
3156dd67e32Sgdamore int s;
3166dd67e32Sgdamore
3176dd67e32Sgdamore /*
3186dd67e32Sgdamore * Hmm. perhaps this intr should be a list. well, PCMCIA
3196dd67e32Sgdamore * devices generally only have one interrupt, and so should
3206dd67e32Sgdamore * generally have only one handler. So we leave it for now.
3216dd67e32Sgdamore * (Other PCMCIA bus drivers do it this way.)
3226dd67e32Sgdamore */
3236dd67e32Sgdamore sp->as_intr = handler;
3246dd67e32Sgdamore sp->as_intrarg = arg;
3258044d721Sad sp->as_softint = softint_establish(IPL_SOFTNET, aupcm_softintr, sp);
3266dd67e32Sgdamore
3276dd67e32Sgdamore /* set up hard interrupt handler for the card IRQs */
3286dd67e32Sgdamore s = splhigh();
3296dd67e32Sgdamore sp->as_hardint = au_intr_establish(sp->as_card_irq, 0,
330a86f56dbSgdamore IPL_TTY, IST_LEVEL_LOW, aupcm_card_intr, sp);
3316dd67e32Sgdamore /* if card is not powered up, then leave the IRQ masked */
3326dd67e32Sgdamore if (!sp->as_enabled) {
3336dd67e32Sgdamore au_intr_disable(sp->as_card_irq);
3346dd67e32Sgdamore }
3356dd67e32Sgdamore splx(s);
3366dd67e32Sgdamore
3376dd67e32Sgdamore return (sp->as_softint);
3386dd67e32Sgdamore }
3396dd67e32Sgdamore
3406dd67e32Sgdamore void
aupcm_intr_disestablish(pcmcia_chipset_handle_t pch,void * ih)3416dd67e32Sgdamore aupcm_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
3426dd67e32Sgdamore {
3436dd67e32Sgdamore struct aupcm_slot *sp = (struct aupcm_slot *)pch;
3446dd67e32Sgdamore
3456dd67e32Sgdamore KASSERT(sp->as_softint == ih);
3466dd67e32Sgdamore /* KASSERT(sp->as_hardint); */
3476dd67e32Sgdamore /* set up hard interrupt handler for the card IRQs */
3486dd67e32Sgdamore
3496dd67e32Sgdamore au_intr_disestablish(sp->as_hardint);
3506dd67e32Sgdamore sp->as_hardint = 0;
3516dd67e32Sgdamore
3528044d721Sad softint_disestablish(ih);
3536dd67e32Sgdamore sp->as_softint = 0;
3546dd67e32Sgdamore sp->as_intr = NULL;
3556dd67e32Sgdamore sp->as_intrarg = NULL;
3566dd67e32Sgdamore }
3576dd67e32Sgdamore
3586dd67e32Sgdamore /*
3596dd67e32Sgdamore * FYI: Hot detach of PCMCIA is supposedly safe because H/W doesn't
3606dd67e32Sgdamore * fault on accesses to missing hardware.
3616dd67e32Sgdamore */
3626dd67e32Sgdamore void
aupcm_event_thread(void * arg)3636dd67e32Sgdamore aupcm_event_thread(void *arg)
3646dd67e32Sgdamore {
3656dd67e32Sgdamore struct aupcm_softc *sc = arg;
3666dd67e32Sgdamore struct aupcm_slot *sp;
3676dd67e32Sgdamore int s, i, attach, detach;
3686dd67e32Sgdamore
3696dd67e32Sgdamore for (;;) {
3706dd67e32Sgdamore s = splhigh();
3716dd67e32Sgdamore if (sc->sc_wake == 0) {
3726dd67e32Sgdamore splx(s);
3736dd67e32Sgdamore /*
3746dd67e32Sgdamore * XXX: Currently, the au_icu.c lacks support
3756dd67e32Sgdamore * for edge-triggered interrupts. So we
3766dd67e32Sgdamore * cannot really use the status change
377*9cc225e6Sandvar * interrupts. For now we poll (once per sec).
3786dd67e32Sgdamore * FYI, Linux does it this way, and they *do*
3796dd67e32Sgdamore * have support for edge triggered interrupts.
3806dd67e32Sgdamore * Go figure.
3816dd67e32Sgdamore */
3826dd67e32Sgdamore tsleep(&sc->sc_wake, PWAIT, "aupcm_event", hz);
383801de81aSdholland s = splhigh();
3846dd67e32Sgdamore }
3856dd67e32Sgdamore sc->sc_wake = 0;
3866dd67e32Sgdamore
3876dd67e32Sgdamore attach = detach = 0;
3886dd67e32Sgdamore for (i = 0; i < sc->sc_nslots; i++) {
3896dd67e32Sgdamore sp = &sc->sc_slots[i];
3906dd67e32Sgdamore
3916dd67e32Sgdamore if (sc->sc_slot_status(sp->as_slot) != 0) {
3926dd67e32Sgdamore if (!sp->as_status) {
3936dd67e32Sgdamore DPRINTF(("%s: card %d insertion\n",
394133bfd25Skiyohara device_xname(sc->sc_dev), i));
3956dd67e32Sgdamore attach |= (1 << i);
3966dd67e32Sgdamore sp->as_status = 1;
3976dd67e32Sgdamore }
3986dd67e32Sgdamore } else {
3996dd67e32Sgdamore if (sp->as_status) {
4006dd67e32Sgdamore DPRINTF(("%s: card %d removal\n",
401133bfd25Skiyohara device_xname(sc->sc_dev), i));
4026dd67e32Sgdamore detach |= (1 << i);
4036dd67e32Sgdamore sp->as_status = 0;
4046dd67e32Sgdamore }
4056dd67e32Sgdamore }
4066dd67e32Sgdamore }
4076dd67e32Sgdamore splx(s);
4086dd67e32Sgdamore
4096dd67e32Sgdamore for (i = 0; i < sc->sc_nslots; i++) {
4106dd67e32Sgdamore sp = &sc->sc_slots[i];
4116dd67e32Sgdamore
4126dd67e32Sgdamore if (detach & (1 << i)) {
4136dd67e32Sgdamore aupcm_slot_disable(sp);
414133bfd25Skiyohara pcmcia_card_detach(sp->as_pcmcia, DETACH_FORCE);
4156dd67e32Sgdamore } else if (attach & (1 << i)) {
4166dd67e32Sgdamore /*
4176dd67e32Sgdamore * until the function is enabled, don't
4186dd67e32Sgdamore * honor interrupts
4196dd67e32Sgdamore */
4206dd67e32Sgdamore sp->as_enabled = 0;
4216dd67e32Sgdamore au_intr_disable(sp->as_card_irq);
4226dd67e32Sgdamore pcmcia_card_attach(sp->as_pcmcia);
4236dd67e32Sgdamore }
4246dd67e32Sgdamore }
4256dd67e32Sgdamore }
4266dd67e32Sgdamore }
4276dd67e32Sgdamore
4286dd67e32Sgdamore #if 0
4296dd67e32Sgdamore void
4306dd67e32Sgdamore aupcm_status_intr(void *arg)
4316dd67e32Sgdamore {
4326dd67e32Sgdamore int s;
4336dd67e32Sgdamore struct aupcm_softc *sc = arg;
4346dd67e32Sgdamore
4356dd67e32Sgdamore s = splhigh();
4366dd67e32Sgdamore
4376dd67e32Sgdamore /* kick the status thread so it does its bit */
4386dd67e32Sgdamore sc->sc_wake = 1;
4396dd67e32Sgdamore wakeup(&sc->sc_wake);
4406dd67e32Sgdamore
4416dd67e32Sgdamore splx(s);
4426dd67e32Sgdamore }
4436dd67e32Sgdamore #endif
4446dd67e32Sgdamore
4456dd67e32Sgdamore int
aupcm_card_intr(void * arg)4466dd67e32Sgdamore aupcm_card_intr(void *arg)
4476dd67e32Sgdamore {
4486dd67e32Sgdamore struct aupcm_slot *sp = arg;
4496dd67e32Sgdamore
4506dd67e32Sgdamore /* disable the hard interrupt for now */
4516dd67e32Sgdamore au_intr_disable(sp->as_card_irq);
4526dd67e32Sgdamore
4536dd67e32Sgdamore if (sp->as_intr != NULL) {
4548044d721Sad softint_schedule(sp->as_softint);
4556dd67e32Sgdamore }
4566dd67e32Sgdamore
4576dd67e32Sgdamore return 1;
4586dd67e32Sgdamore }
4596dd67e32Sgdamore
4606dd67e32Sgdamore void
aupcm_softintr(void * arg)4616dd67e32Sgdamore aupcm_softintr(void *arg)
4626dd67e32Sgdamore {
4636dd67e32Sgdamore struct aupcm_slot *sp = arg;
4646dd67e32Sgdamore int s;
4656dd67e32Sgdamore
4666dd67e32Sgdamore sp->as_intr(sp->as_intrarg);
4676dd67e32Sgdamore
4686dd67e32Sgdamore s = splhigh();
4696dd67e32Sgdamore
4706dd67e32Sgdamore if (sp->as_intr && sp->as_enabled) {
4716dd67e32Sgdamore au_intr_enable(sp->as_card_irq);
4726dd67e32Sgdamore }
4736dd67e32Sgdamore
4746dd67e32Sgdamore splx(s);
4756dd67e32Sgdamore }
4766dd67e32Sgdamore
4776dd67e32Sgdamore void
aupcm_slot_enable(pcmcia_chipset_handle_t pch)4786dd67e32Sgdamore aupcm_slot_enable(pcmcia_chipset_handle_t pch)
4796dd67e32Sgdamore {
4806dd67e32Sgdamore struct aupcm_slot *sp = (struct aupcm_slot *)pch;
4816dd67e32Sgdamore int s;
4826dd67e32Sgdamore
4836dd67e32Sgdamore /* no interrupts while we reset the card, please */
4846dd67e32Sgdamore if (sp->as_intr)
4856dd67e32Sgdamore au_intr_disable(sp->as_card_irq);
4866dd67e32Sgdamore
4876dd67e32Sgdamore /*
4886dd67e32Sgdamore * XXX: should probably lock to make sure slot_disable and
4896dd67e32Sgdamore * enable not called together. However, i believe that the
4906dd67e32Sgdamore * event thread basically serializes them anyway.
4916dd67e32Sgdamore */
4926dd67e32Sgdamore
4936dd67e32Sgdamore sp->as_softc->sc_slot_enable(sp->as_slot);
4946dd67e32Sgdamore /* card is powered up now, honor device interrupts */
4956dd67e32Sgdamore
4966dd67e32Sgdamore s = splhigh();
4976dd67e32Sgdamore sp->as_enabled = 1;
4986dd67e32Sgdamore if (sp->as_intr)
4996dd67e32Sgdamore au_intr_enable(sp->as_card_irq);
5006dd67e32Sgdamore splx(s);
5016dd67e32Sgdamore }
5026dd67e32Sgdamore
5036dd67e32Sgdamore void
aupcm_slot_disable(pcmcia_chipset_handle_t pch)5046dd67e32Sgdamore aupcm_slot_disable(pcmcia_chipset_handle_t pch)
5056dd67e32Sgdamore {
5066dd67e32Sgdamore struct aupcm_slot *sp = (struct aupcm_slot *)pch;
5076dd67e32Sgdamore int s;
5086dd67e32Sgdamore
5096dd67e32Sgdamore s = splhigh();
5106dd67e32Sgdamore au_intr_disable(sp->as_card_irq);
5116dd67e32Sgdamore sp->as_enabled = 0;
5126dd67e32Sgdamore splx(s);
5136dd67e32Sgdamore
5146dd67e32Sgdamore sp->as_softc->sc_slot_disable(sp->as_slot);
5156dd67e32Sgdamore }
5166dd67e32Sgdamore
5176dd67e32Sgdamore void
aupcm_slot_settype(pcmcia_chipset_handle_t pch,int type)5186dd67e32Sgdamore aupcm_slot_settype(pcmcia_chipset_handle_t pch, int type)
5196dd67e32Sgdamore {
5206dd67e32Sgdamore /* we do nothing now : type == PCMCIA_IFTYPE_IO */
5216dd67e32Sgdamore }
5226dd67e32Sgdamore
5236dd67e32Sgdamore int
aupcm_mem_alloc(pcmcia_chipset_handle_t pch,bus_size_t size,struct pcmcia_mem_handle * pcmh)5246dd67e32Sgdamore aupcm_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
5256dd67e32Sgdamore struct pcmcia_mem_handle *pcmh)
5266dd67e32Sgdamore {
5276dd67e32Sgdamore pcmh->memt = NULL;
5286dd67e32Sgdamore pcmh->size = pcmh->realsize = size;
5296dd67e32Sgdamore pcmh->addr = 0;
5306dd67e32Sgdamore pcmh->mhandle = 0;
5316dd67e32Sgdamore
5326dd67e32Sgdamore return 0;
5336dd67e32Sgdamore }
5346dd67e32Sgdamore
5356dd67e32Sgdamore void
aupcm_mem_free(pcmcia_chipset_handle_t pch,struct pcmcia_mem_handle * pcmh)5366dd67e32Sgdamore aupcm_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pcmh)
5376dd67e32Sgdamore {
5386dd67e32Sgdamore /* nothing to do */
5396dd67e32Sgdamore }
5406dd67e32Sgdamore
5416dd67e32Sgdamore int
aupcm_mem_map(pcmcia_chipset_handle_t pch,int kind,bus_addr_t addr,bus_size_t size,struct pcmcia_mem_handle * pcmh,bus_size_t * offsetp,int * windowp)5426dd67e32Sgdamore aupcm_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
5436dd67e32Sgdamore bus_size_t size, struct pcmcia_mem_handle *pcmh, bus_size_t *offsetp,
5446dd67e32Sgdamore int *windowp)
5456dd67e32Sgdamore {
5466dd67e32Sgdamore struct aupcm_slot *sp = (struct aupcm_slot *)pch;
5476dd67e32Sgdamore int win, err;
5486dd67e32Sgdamore int s;
5496dd67e32Sgdamore
5506dd67e32Sgdamore s = splhigh();
5516dd67e32Sgdamore for (win = 0; win < AUPCMCIA_NWINS; win++) {
5526dd67e32Sgdamore if (sp->as_wins[win] == NULL) {
5536dd67e32Sgdamore sp->as_wins[win] = pcmh;
5546dd67e32Sgdamore break;
5556dd67e32Sgdamore }
5566dd67e32Sgdamore }
5576dd67e32Sgdamore splx(s);
5586dd67e32Sgdamore
5596dd67e32Sgdamore if (win >= AUPCMCIA_NWINS) {
5606dd67e32Sgdamore return ENOMEM;
5616dd67e32Sgdamore }
5626dd67e32Sgdamore
5636dd67e32Sgdamore if (kind & PCMCIA_MEM_ATTR) {
5646dd67e32Sgdamore pcmh->memt = &sp->as_attrt;
5656dd67e32Sgdamore NOISY(("mapping ATTR addr %x size %x\n", (uint32_t)addr,
5666dd67e32Sgdamore (uint32_t)size));
5676dd67e32Sgdamore } else {
5686dd67e32Sgdamore pcmh->memt = &sp->as_memt;
5696dd67e32Sgdamore NOISY(("mapping MEMORY addr %x size %x\n", (uint32_t)addr,
5706dd67e32Sgdamore (uint32_t)size));
5716dd67e32Sgdamore }
5726dd67e32Sgdamore
5736dd67e32Sgdamore if ((size + addr) > (64 * 1024 * 1024))
5746dd67e32Sgdamore return EINVAL;
5756dd67e32Sgdamore
5766dd67e32Sgdamore pcmh->size = size;
5776dd67e32Sgdamore
5786dd67e32Sgdamore err = bus_space_map(pcmh->memt, addr, size, 0, &pcmh->memh);
5796dd67e32Sgdamore if (err != 0) {
5806dd67e32Sgdamore sp->as_wins[win] = NULL;
5816dd67e32Sgdamore return err;
5826dd67e32Sgdamore }
5836dd67e32Sgdamore *offsetp = 0;
5846dd67e32Sgdamore *windowp = win;
5856dd67e32Sgdamore
5866dd67e32Sgdamore return 0;
5876dd67e32Sgdamore }
5886dd67e32Sgdamore
5896dd67e32Sgdamore void
aupcm_mem_unmap(pcmcia_chipset_handle_t pch,int win)5906dd67e32Sgdamore aupcm_mem_unmap(pcmcia_chipset_handle_t pch, int win)
5916dd67e32Sgdamore {
5926dd67e32Sgdamore struct aupcm_slot *sp = (struct aupcm_slot *)pch;
5936dd67e32Sgdamore struct pcmcia_mem_handle *pcmh;
5946dd67e32Sgdamore
5956dd67e32Sgdamore pcmh = (struct pcmcia_mem_handle *)sp->as_wins[win];
5966dd67e32Sgdamore sp->as_wins[win] = NULL;
5976dd67e32Sgdamore
5986dd67e32Sgdamore NOISY(("memory umap virtual %x\n", (uint32_t)pcmh->memh));
5996dd67e32Sgdamore bus_space_unmap(pcmh->memt, pcmh->memh, pcmh->size);
6006dd67e32Sgdamore pcmh->memt = NULL;
6016dd67e32Sgdamore }
6026dd67e32Sgdamore
6036dd67e32Sgdamore int
aupcm_io_alloc(pcmcia_chipset_handle_t pch,bus_addr_t start,bus_size_t size,bus_size_t align,struct pcmcia_io_handle * pih)6046dd67e32Sgdamore aupcm_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
6056dd67e32Sgdamore bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pih)
6066dd67e32Sgdamore {
6076dd67e32Sgdamore struct aupcm_slot *sp = (struct aupcm_slot *)pch;
6086dd67e32Sgdamore bus_space_handle_t bush;
6096dd67e32Sgdamore int err;
6106dd67e32Sgdamore
6116dd67e32Sgdamore pih->iot = &sp->as_iot;
6126dd67e32Sgdamore pih->size = size;
6136dd67e32Sgdamore pih->flags = 0;
6146dd67e32Sgdamore
6156dd67e32Sgdamore /*
6166dd67e32Sgdamore * start from the initial offset - this gets us a slot
6176dd67e32Sgdamore * specific address, while still leaving the addresses more or
6186dd67e32Sgdamore * less zero-based which is required for x86-style device
6196dd67e32Sgdamore * drivers.
6206dd67e32Sgdamore */
6216dd67e32Sgdamore err = bus_space_alloc(pih->iot, start, 0x100000,
6226dd67e32Sgdamore size, align, 0, 0, &pih->addr, &bush);
6236dd67e32Sgdamore NOISY(("start = %x, addr = %x, size = %x, bush = %x\n",
6246dd67e32Sgdamore (uint32_t)start, (uint32_t)pih->addr, (uint32_t)size,
6256dd67e32Sgdamore (uint32_t)bush));
6266dd67e32Sgdamore
6276dd67e32Sgdamore /* and we convert it back */
6286dd67e32Sgdamore if (err == 0) {
6296dd67e32Sgdamore pih->ihandle = (void *)bush;
6306dd67e32Sgdamore }
6316dd67e32Sgdamore
6326dd67e32Sgdamore return (err);
6336dd67e32Sgdamore }
6346dd67e32Sgdamore
6356dd67e32Sgdamore void
aupcm_io_free(pcmcia_chipset_handle_t pch,struct pcmcia_io_handle * pih)6366dd67e32Sgdamore aupcm_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
6376dd67e32Sgdamore {
6386dd67e32Sgdamore bus_space_free(pih->iot, (bus_space_handle_t)pih->ihandle,
6396dd67e32Sgdamore pih->size);
6406dd67e32Sgdamore }
6416dd67e32Sgdamore
6426dd67e32Sgdamore int
aupcm_io_map(pcmcia_chipset_handle_t pch,int width,bus_addr_t offset,bus_size_t size,struct pcmcia_io_handle * pih,int * windowp)6436dd67e32Sgdamore aupcm_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
6446dd67e32Sgdamore bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
6456dd67e32Sgdamore {
6466dd67e32Sgdamore int err;
6476dd67e32Sgdamore
6486dd67e32Sgdamore err = bus_space_subregion(pih->iot, (bus_space_handle_t)pih->ihandle,
6496dd67e32Sgdamore offset, size, &pih->ioh);
6506dd67e32Sgdamore NOISY(("io map offset = %x, size = %x, ih = %x, hdl=%x\n",
6516dd67e32Sgdamore (uint32_t)offset, (uint32_t)size,
6526dd67e32Sgdamore (uint32_t)pih->ihandle, (uint32_t)pih->ioh));
6536dd67e32Sgdamore
6546dd67e32Sgdamore return err;
6556dd67e32Sgdamore }
6566dd67e32Sgdamore
6576dd67e32Sgdamore void
aupcm_io_unmap(pcmcia_chipset_handle_t pch,int win)6586dd67e32Sgdamore aupcm_io_unmap(pcmcia_chipset_handle_t pch, int win)
6596dd67e32Sgdamore {
6606dd67e32Sgdamore /* We mustn't unmap/free subregion bus space! */
6616dd67e32Sgdamore NOISY(("io unmap\n"));
6626dd67e32Sgdamore }
663