/* $OpenBSD: ts102.c,v 1.14 2005/01/27 17:03:23 millert Exp $ */
/* $NetBSD: ts102.c,v 1.22 2023/12/20 05:33:18 thorpej Exp $ */
/*
* Copyright (c) 2003, 2004, Miodrag Vallat.
* Copyright (c) 2005, Michael Lorenz.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
*/
/*
* Driver for the PCMCIA controller found in Tadpole SPARCbook 3 series
* notebooks.
*
* Based on the information provided in the SPARCbook 3 Technical Reference
* Manual (s3gxtrmb.pdf), chapter 7. A few ramblings against this document
* and/or the chip itself are scattered across this file.
*
* Implementation notes:
*
* - The TS102 exports its PCMCIA windows as SBus memory ranges: 64MB for
* the common memory window, and 16MB for the attribute and I/O windows.
*
* Mapping the whole windows would consume 192MB of address space, which
* is much more that what the iospace can offer.
*
* A best-effort solution would be to map the windows on demand. However,
* due to the wap mapdev() works, the va used for the mappings would be
* lost after unmapping (although using an extent to register iospace memory
* usage would fix this). So, instead, we will do a fixed mapping of a subset
* of each window upon attach - this is similar to what the stp4020 driver
* does.
*
* Endianness farce:
*
* - The documentation pretends that the endianness settings only affect the
* common memory window. Gee, thanks a lot. What about other windows, then?
* As a result, this driver runs with endianness conversions turned off.
*
* - One of the little-endian SBus and big-endian PCMCIA flags has the reverse
* meaning, actually. To achieve a ``no endianness conversion'' status,
* one has to be set and the other unset. It does not matter which one,
* though.
*/
if (td->td_pcmcia == NULL) {
/*
* If no pcmcia attachment, power down the slot.
*/
tslot_slot_disable((pcmcia_chipset_handle_t)td);
return;
}
/*
* Initialize the slot
*/
ctl = TSLOT_READ(td, TS102_REG_CARD_A_CTL);
/* force low addresses */
ctl &= ~(TS102_CARD_CTL_AA_MASK | TS102_CARD_CTL_IA_MASK);
/* Put SBus and PCMCIA in their respective endian mode */
ctl |= TS102_CARD_CTL_SBLE; /* this is not what it looks like! */
ctl &= ~TS102_CARD_CTL_PCMBE; /* default */
status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
if (status & TS102_CARD_STS_PRES) {
td->td_status = TS_CARD;
pcmcia_card_attach(td->td_pcmcia);
} else
td->td_status = 0;
}
/* XXX there ought to be a common function for this... */
static int
tslot_print(void *aux, const char *description)
{
struct pcmciabus_attach_args *paa = aux;
struct tslot_data *td = (struct tslot_data *)paa->pch;
/*
* Power down the socket and disable access
*/
status &= ~TS102_CARD_STS_ACEN;
status &= ~(TS102_CARD_STS_VPP1_MASK | TS102_CARD_STS_VPP2_MASK);
status |= TS102_CARD_STS_VCCEN;
TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
/*
* wait 300ms until power fails (Tpf).
*/
tslot_delay(td->td_parent, 300);
}
/* Power down the socket to reset it */
status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
TSPRINTF("status: %x\n", status);
status &= ~TS102_CARD_STS_ACEN;
status &= ~(TS102_CARD_STS_VPP1_MASK | TS102_CARD_STS_VPP2_MASK);
status |= TS102_CARD_STS_VCCEN;
TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
/*
* wait 300ms until power fails (Tpf). Then, wait 100ms since we
* are changing Vcc (Toff).
*/
tslot_delay(td->td_parent, 300 + 100);
/*
* Power on the card if not already done, and enable card access
*/
status |= TS102_CARD_STS_ACEN;
status |= TS102_CARD_STS_VPP1_VCC;
status &= ~TS102_CARD_STS_VCCEN;
TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
/*
* wait 100ms until power raise (Tpr) and 20ms to become
* stable (Tsu(Vcc)).
*/
tslot_delay(td->td_parent, 100 + 20);
/*
* hold RESET at least 20us.
*/
intr = TSLOT_READ(td, TS102_REG_CARD_A_INT);
delay(20);
TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
intr & ~TS102_CARD_INT_SOFT_RESET);
/* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
tslot_delay(td->td_parent, 20);
/* We need level-triggered interrupts for PC Card hardware */
TSLOT_WRITE(td, TS102_REG_CARD_A_STS,
TSLOT_READ(td, TS102_REG_CARD_A_STS) | TS102_CARD_STS_LVL);
/*
* Wait until the card is unbusy. If it is still busy after 3 seconds,
* give up. We could enable card interrupts and wait for the interrupt
* to happen when BUSY is released, but the interrupt could also be
* triggered by the card itself if it's an I/O card, so better poll
* here.
*/
for (i = 30000; i != 0; i--) {
status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
/* If the card has been removed, abort */
if ((status & TS102_CARD_STS_PRES) == 0) {
tslot_slot_disable(pch);
return;
}
if (status & TS102_CARD_STS_RDY)
break;
else
delay(100);
}
if (i == 0) {
printf("%s: slot %d still busy after 3 seconds, status 0x%x\n",
device_xname(td->td_parent->sc_dev), td->td_slot,
TSLOT_READ(td, TS102_REG_CARD_A_STS));
return;
}
}
static void
tslot_event_thread(void *v)
{
struct tslot_softc *sc = v;
struct tslot_data *td;
int s, status;
unsigned int socket;
#if NTCTRL > 0
int i;
/*
* First-time setup of our LCD symbol. When a card is present at boot
* time we won't detect a change here and therefore the LCD symbol won't
* light up.
*/
for (i = 0; i < TS102_NUM_SLOTS; i++) {
td = &sc->sc_slot[i];
status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
tslot_update_lcd(sc, i, status & TS102_CARD_STS_PRES);
}
#endif
static int
tslot_intr(void *v)
{
struct tslot_softc *sc = v;
struct tslot_data *td;
int intregs[TS102_NUM_SLOTS], *intreg;
int i, s, rc = 0;
s = splhigh();
/*
* Scan slots, and acknowledge the interrupt if necessary first
*/
for (i = 0; i < TS102_NUM_SLOTS; i++) {
td = &sc->sc_slot[i];
intreg = &intregs[i];
*intreg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
/*
* Acknowledge all interrupt situations at once, even if they
* did not occur.
*/
if ((*intreg & (TS102_CARD_INT_STATUS_IRQ |
TS102_CARD_INT_STATUS_WP_STATUS_CHANGED |
TS102_CARD_INT_STATUS_BATTERY_STATUS_CHANGED |
TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED)) != 0) {
rc = 1;
TSLOT_WRITE(td, TS102_REG_CARD_A_INT, *intreg |
TS102_CARD_INT_RQST_IRQ |
TS102_CARD_INT_RQST_WP_STATUS_CHANGED |
TS102_CARD_INT_RQST_BATTERY_STATUS_CHANGED |
TS102_CARD_INT_RQST_CARDDETECT_STATUS_CHANGED);
}
}
static void
tslot_slot_intr(struct tslot_data *td, int intreg)
{
struct tslot_softc *sc = td->td_parent;
int status, sockstat;
uint32_t ireg;
status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
#ifdef TSLOT_DEBUG
printf("%s: interrupt on socket %d ir %x sts %x\n",
device_xname(sc->sc_dev), td->td_slot, intreg, status);
#else
__USE(status);
#endif
sockstat = td->td_status;
/*
* The TS102 queues interrupt request, and may trigger an interrupt
* for a condition the driver does not want to receive anymore (for
* example, after a card gets removed).
* Thus, only proceed if the driver is currently allowing a particular
* condition.
*/
if ((intreg & TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED) != 0 &&
(intreg & TS102_CARD_INT_MASK_CARDDETECT_STATUS) != 0) {
tslot_queue_event(sc, td->td_slot);
#ifdef TSLOT_DEBUG
printf("%s: slot %d status changed from %d to %d\n",
device_xname(sc->sc_dev), td->td_slot, sockstat,
td->td_status);
#endif
/*
* Ignore extra interrupt bits, they are part of the change.
*/
return;
}
if ((intreg & TS102_CARD_INT_STATUS_IRQ) != 0 &&
(intreg & TS102_CARD_INT_MASK_IRQ) != 0) {
/* ignore interrupts if we have a pending state change */
if (sc->sc_events & (1 << td->td_slot))
{
TSPRINTF("ev: %d\n", sc->sc_events);
return;
}
if ((sockstat & TS_CARD) == 0) {
printf("%s: spurious interrupt on slot %d isr %x\n",
device_xname(sc->sc_dev), td->td_slot, intreg);
return;
}
if (td->td_intr != NULL) {
if (td->td_softint != NULL)
sparc_softintr_schedule(td->td_softint);
/*
* Disable this sbus interrupt, until the soft-int
* handler had a chance to run
*/
ireg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
TSLOT_WRITE(td, TS102_REG_CARD_A_INT, ireg &
~TS102_CARD_INT_MASK_IRQ);
}
}
}
/*
* Enable the card interrupts if this is an I/O card.
* Note that the TS102_CARD_STS_IO bit in the status register will
* never get set, despite what the documentation says!
*/
TSPRINTF("tslot_slot_settype(%d)\n",type);
if (type == PCMCIA_IFTYPE_IO) {
TSLOT_WRITE(td, TS102_REG_CARD_A_STS,
TSLOT_READ(td, TS102_REG_CARD_A_STS) | TS102_CARD_STS_IO);
TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
TS102_CARD_INT_MASK_CARDDETECT_STATUS |
TS102_CARD_INT_MASK_IRQ);
reg=TSLOT_READ(td, TS102_REG_CARD_A_STS);
TSPRINTF("status: %x\n", reg);
}
}
static void
tslot_update_lcd(struct tslot_softc *sc, int socket, int status)
{
#if NTCTRL > 0
int was = (sc->sc_active != 0), is;
int mask = 1 << socket;