/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Written by Garrett D'Amore for Itronix Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of Itronix Inc. may not be used to endorse
* or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Borrow PCMCIADEBUG for now. Generally aupcmcia is the only PCMCIA
* host on these machines anyway.
*/
#ifdef PCMCIADEBUG
int aupcm_debug = 1;
#define DPRINTF(arg) if (aupcm_debug) printf arg
#else
#define DPRINTF(arg)
#endif
/*
* And for information about mappings, etc. use this one.
*/
#ifdef AUPCMCIANOISY
#define NOISY(arg) printf arg
#else
#define NOISY(arg)
#endif
/*
* Note, we use prefix "aupcm" instead of "aupcmcia", even though our
* driver is the latter, mostly because my fingers have trouble typing
* the former. "aupcm" should be sufficiently unique to avoid
* confusion.
*/
static int aupcm_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
struct pcmcia_mem_handle *);
static void aupcm_mem_free(pcmcia_chipset_handle_t,
struct pcmcia_mem_handle *);
static int aupcm_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
bus_size_t, struct pcmcia_mem_handle *, bus_size_t *, int *);
static void aupcm_mem_unmap(pcmcia_chipset_handle_t, int);
/* initialize bus space */
if (done) {
/* there can be only one. */
return;
}
done = 1;
/*
* PCMCIA memory can live within pretty much the entire 32-bit
* space, modulo 64 MB wraps. We don't have to support coexisting
* DMA.
*/
au_himem_space_init(&aupcm_memt, "pcmciamem",
PCMCIA_BASE, AUPCMCIA_ATTR_OFFSET, 0xffffffff,
AU_HIMEM_SPACE_LITTLE_ENDIAN);
if ((md = aupcmcia_machdep()) == NULL) {
aprint_error(": unable to get machdep structure\n");
return;
}
/* if no pcmcia, make sure slot is powered down */
if (sp->as_pcmcia == NULL) {
aupcm_slot_disable(sp);
continue;
}
/* this makes sure we probe the slot */
sc->sc_wake |= (1 << slot);
}
/*
* XXX: this would be an excellent time time to establish a handler
* for the card insertion interrupt, but that's edge triggered, and
* au_icu.c won't support it right now. We poll in the event thread
* for now. Start by initializing it now.
*/
if (kthread_create(PRI_NONE, 0, NULL, aupcm_event_thread, sc,
&sc->sc_thread, "%s", device_xname(sc->sc_dev)) != 0)
panic("%s: unable to create event kthread",
device_xname(sc->sc_dev));
}
void *
aupcm_intr_establish(pcmcia_chipset_handle_t pch,
struct pcmcia_function *pf, int level, int (*handler)(void *), void *arg)
{
struct aupcm_slot *sp = (struct aupcm_slot *)pch;
int s;
/*
* Hmm. perhaps this intr should be a list. well, PCMCIA
* devices generally only have one interrupt, and so should
* generally have only one handler. So we leave it for now.
* (Other PCMCIA bus drivers do it this way.)
*/
sp->as_intr = handler;
sp->as_intrarg = arg;
sp->as_softint = softint_establish(IPL_SOFTNET, aupcm_softintr, sp);
/* set up hard interrupt handler for the card IRQs */
s = splhigh();
sp->as_hardint = au_intr_establish(sp->as_card_irq, 0,
IPL_TTY, IST_LEVEL_LOW, aupcm_card_intr, sp);
/* if card is not powered up, then leave the IRQ masked */
if (!sp->as_enabled) {
au_intr_disable(sp->as_card_irq);
}
splx(s);
/*
* FYI: Hot detach of PCMCIA is supposedly safe because H/W doesn't
* fault on accesses to missing hardware.
*/
void
aupcm_event_thread(void *arg)
{
struct aupcm_softc *sc = arg;
struct aupcm_slot *sp;
int s, i, attach, detach;
for (;;) {
s = splhigh();
if (sc->sc_wake == 0) {
splx(s);
/*
* XXX: Currently, the au_icu.c lacks support
* for edge-triggered interrupts. So we
* cannot really use the status change
* interrupts. For now we poll (once per sec).
* FYI, Linux does it this way, and they *do*
* have support for edge triggered interrupts.
* Go figure.
*/
tsleep(&sc->sc_wake, PWAIT, "aupcm_event", hz);
s = splhigh();
}
sc->sc_wake = 0;
attach = detach = 0;
for (i = 0; i < sc->sc_nslots; i++) {
sp = &sc->sc_slots[i];
/* no interrupts while we reset the card, please */
if (sp->as_intr)
au_intr_disable(sp->as_card_irq);
/*
* XXX: should probably lock to make sure slot_disable and
* enable not called together. However, i believe that the
* event thread basically serializes them anyway.
*/
sp->as_softc->sc_slot_enable(sp->as_slot);
/* card is powered up now, honor device interrupts */
s = splhigh();
sp->as_enabled = 1;
if (sp->as_intr)
au_intr_enable(sp->as_card_irq);
splx(s);
}
/*
* start from the initial offset - this gets us a slot
* specific address, while still leaving the addresses more or
* less zero-based which is required for x86-style device
* drivers.
*/
err = bus_space_alloc(pih->iot, start, 0x100000,
size, align, 0, 0, &pih->addr, &bush);
NOISY(("start = %x, addr = %x, size = %x, bush = %x\n",
(uint32_t)start, (uint32_t)pih->addr, (uint32_t)size,
(uint32_t)bush));
/* and we convert it back */
if (err == 0) {
pih->ihandle = (void *)bush;
}