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