Home | History | Annotate | Line # | Download | only in dev
ts102.c revision 1.20
      1 /*	$OpenBSD: ts102.c,v 1.14 2005/01/27 17:03:23 millert Exp $	*/
      2 /*	$NetBSD: ts102.c,v 1.20 2021/06/11 04:58:30 jdc Exp $ */
      3 /*
      4  * Copyright (c) 2003, 2004, Miodrag Vallat.
      5  * Copyright (c) 2005, Michael Lorenz.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Driver for the PCMCIA controller found in Tadpole SPARCbook 3 series
     31  * notebooks.
     32  *
     33  * Based on the information provided in the SPARCbook 3 Technical Reference
     34  * Manual (s3gxtrmb.pdf), chapter 7.  A few ramblings against this document
     35  * and/or the chip itself are scattered across this file.
     36  *
     37  * Implementation notes:
     38  *
     39  * - The TS102 exports its PCMCIA windows as SBus memory ranges: 64MB for
     40  *   the common memory window, and 16MB for the attribute and I/O windows.
     41  *
     42  *   Mapping the whole windows would consume 192MB of address space, which
     43  *   is much more that what the iospace can offer.
     44  *
     45  *   A best-effort solution would be to map the windows on demand. However,
     46  *   due to the wap mapdev() works, the va used for the mappings would be
     47  *   lost after unmapping (although using an extent to register iospace memory
     48  *   usage would fix this). So, instead, we will do a fixed mapping of a subset
     49  *   of each window upon attach - this is similar to what the stp4020 driver
     50  *   does.
     51  *
     52  * Endianness farce:
     53  *
     54  * - The documentation pretends that the endianness settings only affect the
     55  *   common memory window. Gee, thanks a lot. What about other windows, then?
     56  *   As a result, this driver runs with endianness conversions turned off.
     57  *
     58  * - One of the little-endian SBus and big-endian PCMCIA flags has the reverse
     59  *   meaning, actually. To achieve a ``no endianness conversion'' status,
     60  *   one has to be set and the other unset. It does not matter which one,
     61  *   though.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/errno.h>
     69 #include <sys/malloc.h>
     70 #include <sys/extent.h>
     71 #include <sys/proc.h>
     72 #include <sys/kernel.h>
     73 #include <sys/kthread.h>
     74 #include <sys/device.h>
     75 
     76 #include <dev/pcmcia/pcmciareg.h>
     77 #include <dev/pcmcia/pcmciavar.h>
     78 #include <dev/pcmcia/pcmciachip.h>
     79 
     80 #include <sys/bus.h>
     81 #include <machine/intr.h>
     82 #include <machine/autoconf.h>
     83 
     84 #include <dev/sbus/sbusvar.h>
     85 #include <sparc/dev/ts102reg.h>
     86 
     87 #include "tctrl.h"
     88 
     89 #if NTCTRL > 0
     90 #include <machine/tctrl.h>
     91 #include <sparc/dev/tctrlvar.h>
     92 #endif
     93 
     94 #define	TS102_NUM_SLOTS		2
     95 
     96 /*
     97  * Memory ranges
     98  */
     99 #define	TS102_RANGE_COMMON	0
    100 #define	TS102_RANGE_ATTR	1
    101 #define	TS102_RANGE_IO		2
    102 
    103 #define	TS102_RANGE_CNT		3
    104 #define	TS102_NUM_RANGES	(TS102_RANGE_CNT * TS102_NUM_SLOTS)
    105 
    106 #define	TS102_ARBITRARY_MAP_SIZE	(1 * 1024 * 1024)
    107 
    108 struct	tslot_softc;
    109 
    110 #ifdef TSLOT_DEBUG
    111 #define TSPRINTF	printf
    112 #else
    113 #define TSPRINTF	while (0) printf
    114 #endif
    115 
    116 /*
    117  * Per-slot data
    118  */
    119 struct	tslot_data {
    120 	struct tslot_softc	*td_parent;
    121 	device_t		td_pcmcia;
    122 
    123 	volatile uint8_t	*td_regs;
    124 	bus_addr_t		td_space[TS102_RANGE_CNT];
    125 	bus_space_tag_t		td_pcmciat;	/* for accessing cards */
    126 
    127 	/* Interrupt handler */
    128 	int			(*td_intr)(void *);
    129 	void			*td_intrarg;
    130 	void			*td_softint;
    131 
    132 	/* Socket status */
    133 	int			td_slot;
    134 	int			td_status;
    135 #define	TS_CARD			0x0001
    136 };
    137 
    138 struct	tslot_softc {
    139 	device_t	sc_dev;
    140 
    141 	bus_space_tag_t	sc_bustag;		/* socket control io	*/
    142 	bus_space_handle_t	sc_regh;	/*  space		*/
    143 
    144 	pcmcia_chipset_tag_t sc_pct;
    145 
    146 	lwp_t		*sc_thread;	/* event thread */
    147 	uint32_t	sc_events;	/* sockets with pending events */
    148 
    149 	/* bits 0 and 1 are set according to card presence in slot 0 and 1 */
    150 	uint32_t 	sc_active;
    151 
    152 	struct tslot_data sc_slot[TS102_NUM_SLOTS];
    153 };
    154 
    155 static void tslot_attach(device_t, device_t, void *);
    156 static void tslot_event_thread(void *);
    157 static int  tslot_intr(void *);
    158 static void tslot_intr_disestablish(pcmcia_chipset_handle_t, void *);
    159 static void *tslot_intr_establish(pcmcia_chipset_handle_t,
    160     struct pcmcia_function *, int, int (*)(void *), void *);
    161 
    162 const char  *tslot_intr_string(pcmcia_chipset_handle_t, void *);
    163 static int  tslot_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
    164     bus_size_t, struct pcmcia_io_handle *);
    165 static void tslot_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
    166 static int  tslot_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
    167     struct pcmcia_io_handle *, int *);
    168 static void tslot_io_unmap(pcmcia_chipset_handle_t, int);
    169 static int  tslot_match(device_t, struct cfdata *, void *);
    170 static int  tslot_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
    171     struct pcmcia_mem_handle *);
    172 static void tslot_mem_free(pcmcia_chipset_handle_t, struct pcmcia_mem_handle *);
    173 static int  tslot_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
    174     struct pcmcia_mem_handle *, bus_size_t *, int *);
    175 static void tslot_mem_unmap(pcmcia_chipset_handle_t, int);
    176 static int  tslot_print(void *, const char *);
    177 static void tslot_queue_event(struct tslot_softc *, int);
    178 static void tslot_reset(struct tslot_data *, uint32_t);
    179 static void tslot_slot_disable(pcmcia_chipset_handle_t);
    180 static void tslot_slot_enable(pcmcia_chipset_handle_t);
    181 static void tslot_slot_intr(struct tslot_data *, int);
    182 static void tslot_slot_settype(pcmcia_chipset_handle_t, int);
    183 static void tslot_update_lcd(struct tslot_softc *, int, int);
    184 static void tslot_intr_dispatch(void *arg);
    185 void tslot_delay(struct tslot_softc *sc, unsigned int ms);
    186 
    187 CFATTACH_DECL_NEW(tslot, sizeof(struct tslot_softc),
    188     tslot_match, tslot_attach, NULL, NULL);
    189 
    190 extern struct cfdriver tslot_cd;
    191 
    192 /*
    193  * PCMCIA chipset methods
    194  */
    195 struct	pcmcia_chip_functions tslot_functions = {
    196 	tslot_mem_alloc,
    197 	tslot_mem_free,
    198 	tslot_mem_map,
    199 	tslot_mem_unmap,
    200 
    201 	tslot_io_alloc,
    202 	tslot_io_free,
    203 	tslot_io_map,
    204 	tslot_io_unmap,
    205 
    206 	tslot_intr_establish,
    207 	tslot_intr_disestablish,
    208 
    209 	tslot_slot_enable,
    210 	tslot_slot_disable,
    211 	tslot_slot_settype
    212 };
    213 
    214 static	uint16_t ts102_read_2(bus_space_tag_t,
    215 				 bus_space_handle_t,
    216 				 bus_size_t);
    217 static	uint32_t ts102_read_4(bus_space_tag_t,
    218 				 bus_space_handle_t,
    219 				 bus_size_t);
    220 static	uint64_t ts102_read_8(bus_space_tag_t,
    221 				 bus_space_handle_t,
    222 				 bus_size_t);
    223 static	void	ts102_write_2(bus_space_tag_t,
    224 				bus_space_handle_t,
    225 				bus_size_t,
    226 				uint16_t);
    227 static	void	ts102_write_4(bus_space_tag_t,
    228 				bus_space_handle_t,
    229 				bus_size_t,
    230 				uint32_t);
    231 static	void	ts102_write_8(bus_space_tag_t,
    232 				bus_space_handle_t,
    233 				bus_size_t,
    234 				uint64_t);
    235 
    236 static uint16_t
    237 ts102_read_2(bus_space_tag_t space, bus_space_handle_t handle,
    238     bus_size_t offset)
    239 {
    240 	return (le16toh(*(volatile uint16_t *)(handle +
    241 	    offset)));
    242 }
    243 
    244 static uint32_t
    245 ts102_read_4(bus_space_tag_t space, bus_space_handle_t handle,
    246     bus_size_t offset)
    247 {
    248 	return (le32toh(*(volatile uint32_t *)(handle +
    249 	    offset)));
    250 }
    251 
    252 static uint64_t
    253 ts102_read_8(bus_space_tag_t space, bus_space_handle_t handle,
    254     bus_size_t offset)
    255 {
    256 	return (le64toh(*(volatile uint64_t *)(handle +
    257 	    offset)));
    258 }
    259 
    260 static void
    261 ts102_write_2(bus_space_tag_t space, bus_space_handle_t handle,
    262     bus_size_t offset, uint16_t value)
    263 {
    264 	(*(volatile uint16_t *)(handle + offset)) =
    265 	    htole16(value);
    266 }
    267 
    268 static void
    269 ts102_write_4(bus_space_tag_t space, bus_space_handle_t handle,
    270     bus_size_t offset, uint32_t value)
    271 {
    272 	(*(volatile uint32_t *)(handle + offset)) =
    273 	    htole32(value);
    274 }
    275 
    276 static void
    277 ts102_write_8(bus_space_tag_t space, bus_space_handle_t handle,
    278     bus_size_t offset, uint64_t value)
    279 {
    280 	(*(volatile uint64_t *)(handle + offset)) =
    281 	    htole64(value);
    282 }
    283 
    284 
    285 #define	TSLOT_READ(slot, offset) \
    286 	*(volatile uint16_t *)((slot)->td_regs + (offset))
    287 #define	TSLOT_WRITE(slot, offset, value) \
    288 	*(volatile uint16_t *)((slot)->td_regs + (offset)) = (value)
    289 
    290 /*
    291  * Attachment and initialization
    292  */
    293 
    294 static int
    295 tslot_match(device_t parent, struct cfdata *vcf, void *aux)
    296 {
    297 	struct sbus_attach_args *sa = aux;
    298 
    299 	return (strcmp("ts102", sa->sa_name) == 0);
    300 }
    301 
    302 static void
    303 tslot_attach(device_t parent, device_t self, void *args)
    304 {
    305 	struct sbus_attach_args *sa = args;
    306 	struct tslot_softc *sc = device_private(self);
    307 	struct tslot_data *td;
    308 	volatile uint8_t *regs;
    309 	int node, slot, rnum, base, size;
    310 	uint32_t ranges[30];
    311 	void *rptr = ranges;
    312 	bus_space_handle_t hrang = 0;
    313 	bus_space_tag_t tag;
    314 
    315 	sc->sc_dev = self;
    316 	node = sa->sa_node;
    317 	sc->sc_bustag=sa->sa_bustag;
    318 	if (sbus_bus_map(sa->sa_bustag,
    319 			 sa->sa_slot,
    320 			 sa->sa_offset,
    321 			 sa->sa_size,
    322 			 0, &sc->sc_regh) != 0) {
    323 		printf("%s: cannot map registers\n", device_xname(self));
    324 		return;
    325 	}
    326 	regs = (uint8_t *)bus_space_vaddr(sa->sa_bustag, sc->sc_regh);
    327 
    328 	tag = bus_space_tag_alloc(sa->sa_bustag, sc);
    329 	if (tag == NULL) {
    330 		printf("%s: attach: out of memory\n", device_xname(self));
    331 		return;
    332 	}
    333 	tag->sparc_read_2 = ts102_read_2;
    334 	tag->sparc_read_4 = ts102_read_4;
    335 	tag->sparc_read_8 = ts102_read_8;
    336 	tag->sparc_write_2 = ts102_write_2;
    337 	tag->sparc_write_4 = ts102_write_4;
    338 	tag->sparc_write_8 = ts102_write_8;
    339 
    340 	bus_intr_establish(sa->sa_bustag, sa->sa_intr[0].oi_pri,
    341 	    IPL_NONE, tslot_intr, sc);
    342 
    343 	printf(": %d slots\n", TS102_NUM_SLOTS);
    344 
    345 	size = sizeof(ranges);
    346 	if (prom_getprop(node, "ranges", 4, &size, &rptr) != 0) {
    347 		printf("couldn't read ranges\n");
    348 		return;
    349 	}
    350 
    351 	/*
    352 	 * Setup asynchronous event handler
    353 	 */
    354 	sc->sc_events = 0;
    355 
    356 	TSPRINTF("starting event thread...\n");
    357 	if (kthread_create(PRI_NONE, 0, NULL, tslot_event_thread, sc,
    358 	    &sc->sc_thread, "%s", device_xname(self)) != 0) {
    359 		panic("%s: unable to create event kthread",
    360 		    device_xname(self));
    361 	}
    362 
    363 	sc->sc_pct = (pcmcia_chipset_tag_t)&tslot_functions;
    364 	sc->sc_active = 0;
    365 
    366 	/*
    367 	 * Setup slots
    368 	 */
    369 	TSPRINTF("mapping resources...\n");
    370 	for (slot = 0; slot < TS102_NUM_SLOTS; slot++) {
    371 		td = &sc->sc_slot[slot];
    372 		TSPRINTF("slot %d, ",slot);
    373 		for (rnum = 0; rnum < TS102_RANGE_CNT; rnum++) {
    374 			base = (slot * TS102_RANGE_CNT + rnum) * 5;
    375 			TSPRINTF("%d: %08x %08x ",rnum,ranges[base + 3],
    376 			    ranges[base + 4]);
    377 			if(sbus_bus_map(sc->sc_bustag,
    378 					sa->sa_slot,
    379 				 	ranges[base+3],
    380 				 	TS102_ARBITRARY_MAP_SIZE,
    381 					0, &hrang) != 0) {
    382 				printf("%s: cannot map registers\n",
    383 				    device_xname(self));
    384 				return;
    385 			}
    386 			TSPRINTF("%08x: %08x ",(uint32_t)ranges[base + 3],
    387 			    (uint32_t)hrang);
    388 			td->td_space[rnum] = hrang;
    389 		}
    390 		td->td_parent = sc;
    391 		td->td_pcmciat = tag;
    392 		td->td_softint = NULL;
    393 		td->td_regs = regs + slot * (TS102_REG_CARD_B_INT -
    394 		    TS102_REG_CARD_A_INT);
    395 		td->td_slot = slot;
    396 
    397 		TSPRINTF("resetting slot %d %d\n", slot, (int)td->td_regs);
    398 		tslot_reset(td, TS102_ARBITRARY_MAP_SIZE);
    399 	}
    400 }
    401 
    402 static void
    403 tslot_reset(struct tslot_data *td, uint32_t iosize)
    404 {
    405 	struct pcmciabus_attach_args paa;
    406 	int ctl, status;
    407 
    408 	paa.paa_busname = "pcmcia";
    409 	paa.pct = (pcmcia_chipset_tag_t)td->td_parent->sc_pct;
    410 	paa.pch = (pcmcia_chipset_handle_t)td;
    411 
    412 	td->td_pcmcia = config_found(td->td_parent->sc_dev, &paa, tslot_print,
    413 	    CFARG_EOL);
    414 
    415 	if (td->td_pcmcia == NULL) {
    416 		/*
    417 		 * If no pcmcia attachment, power down the slot.
    418 		 */
    419 		tslot_slot_disable((pcmcia_chipset_handle_t)td);
    420 		return;
    421 	}
    422 
    423 	/*
    424 	 * Initialize the slot
    425 	 */
    426 
    427 	ctl = TSLOT_READ(td, TS102_REG_CARD_A_CTL);
    428 
    429 	/* force low addresses */
    430 	ctl &= ~(TS102_CARD_CTL_AA_MASK | TS102_CARD_CTL_IA_MASK);
    431 
    432 	/* Put SBus and PCMCIA in their respective endian mode */
    433 	ctl |= TS102_CARD_CTL_SBLE;	/* this is not what it looks like! */
    434 	ctl &= ~TS102_CARD_CTL_PCMBE;	/* default */
    435 
    436 	/* disable read ahead and address increment */
    437 	ctl &= ~TS102_CARD_CTL_RAHD;
    438 	ctl |= TS102_CARD_CTL_INCDIS;
    439 
    440 	/* power on */
    441 	ctl &= ~TS102_CARD_CTL_PWRD;
    442 	TSLOT_WRITE(td, TS102_REG_CARD_A_CTL, ctl);
    443 	TSPRINTF("ctl: %x\n", ctl);
    444 
    445 	/*
    446 	 * Enable interrupt upon insertion/removal
    447 	 */
    448 
    449 	TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
    450 	    TS102_CARD_INT_MASK_CARDDETECT_STATUS);
    451 
    452 	status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
    453 	if (status & TS102_CARD_STS_PRES) {
    454 		td->td_status = TS_CARD;
    455 		pcmcia_card_attach(td->td_pcmcia);
    456 	} else
    457 		td->td_status = 0;
    458 }
    459 
    460 /* XXX there ought to be a common function for this... */
    461 static int
    462 tslot_print(void *aux, const char *description)
    463 {
    464 	struct pcmciabus_attach_args *paa = aux;
    465 	struct tslot_data *td = (struct tslot_data *)paa->pch;
    466 
    467 	printf(" socket %d", td->td_slot);
    468 	return (UNCONF);
    469 }
    470 
    471 /*
    472  * PCMCIA Helpers
    473  */
    474 
    475 static int
    476 tslot_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
    477     bus_size_t align, struct pcmcia_io_handle *pih)
    478 {
    479 	struct tslot_data *td = (struct tslot_data *)pch;
    480 
    481 #ifdef TSLOT_DEBUG
    482 	printf("[io alloc %x]", (uint32_t)size);
    483 #endif
    484 
    485 	pih->iot = td->td_pcmciat;
    486 	pih->ioh = td->td_space[TS102_RANGE_IO];
    487 	pih->addr = start;
    488 	pih->size = size;
    489 	pih->flags = 0;
    490 
    491 	return (0);
    492 }
    493 
    494 static void
    495 tslot_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
    496 {
    497 #ifdef TSLOT_DEBUG
    498 	printf("[io free]");
    499 #endif
    500 }
    501 
    502 static int
    503 tslot_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
    504     bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
    505 {
    506 	struct tslot_data *td = (struct tslot_data *)pch;
    507 
    508 #ifdef TSLOT_DEBUG
    509 	printf("[io map %x/%x", (uint32_t)offset, (uint32_t)size);
    510 #endif
    511 
    512 	pih->iot = td->td_pcmciat;
    513 	if (bus_space_subregion(pih->iot, td->td_space[TS102_RANGE_IO],
    514 	    offset, size, &pih->ioh) != 0)
    515 		printf("io_map failed, offset %x\n", (uint32_t)offset);
    516 	*windowp = 0; /* TS102_RANGE_IO */
    517 
    518 #ifdef TSLOT_DEBUG
    519 	printf("->%x/%x]", (uint32_t)pih->ioh, (uint32_t)size);
    520 	{
    521 		int addr, line;
    522 		for( addr = offset; addr < (offset + size); addr += 16) {
    523 			printf("%04x:", addr);
    524 			for(line = addr; line < (addr + 16); line += 2) {
    525 				printf(" %04x", bus_space_read_2(pih->iot,
    526 				    pih->ioh, line));
    527 			}
    528 			printf("\n");
    529 		}
    530 	}
    531 #endif
    532 
    533 	return (0);
    534 }
    535 
    536 static void
    537 tslot_io_unmap(pcmcia_chipset_handle_t pch, int win)
    538 {
    539 #ifdef TSLOT_DEBUG
    540 	struct tslot_data *td = (struct tslot_data *)pch;
    541 
    542 	printf("[io unmap]");
    543 	{
    544 		int addr, line, offset = 0, size = 0x80;
    545 		for (addr = offset; addr < (offset + size); addr += 16) {
    546 			printf("%04x:", addr);
    547 			for (line = addr; line < (addr + 16); line += 2){
    548 				printf(" %04x", bus_space_read_2(td->td_pcmciat,
    549 				    td->td_space[2], line));
    550 			}
    551 			printf("\n");
    552 		}
    553 	}
    554 #endif
    555 }
    556 
    557 static int
    558 tslot_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    559     struct pcmcia_mem_handle *pmh)
    560 {
    561 	struct tslot_data *td = (struct tslot_data *)pch;
    562 
    563 #ifdef TSLOT_DEBUG
    564 	printf("[mem alloc %x]", (uint32_t)size);
    565 #endif
    566 	pmh->memt = td->td_pcmciat;
    567 	pmh->size = size;
    568 	pmh->addr = 0;
    569 	pmh->mhandle = 0;
    570 	pmh->realsize = size;	/* nothing so far! */
    571 
    572 	return (0);
    573 }
    574 
    575 static void
    576 tslot_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
    577 {
    578 #ifdef TSLOT_DEBUG
    579 	printf("[mem free]");
    580 #endif
    581 }
    582 
    583 static int
    584 tslot_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
    585     bus_size_t size, struct pcmcia_mem_handle *pmh, bus_size_t *offsetp,
    586     int *windowp)
    587 {
    588 	struct tslot_data *td = (struct tslot_data *)pch;
    589 	int slot;
    590 
    591 	slot = kind & PCMCIA_MEM_ATTR ? TS102_RANGE_ATTR : TS102_RANGE_COMMON;
    592 #ifdef TSLOT_DEBUG
    593 	printf("[mem map %d %x/%x", slot, (uint32_t)addr, (uint32_t)size);
    594 #endif
    595 
    596 	pmh->memt = td->td_parent->sc_bustag;
    597 	if (bus_space_subregion(pmh->memt, td->td_space[slot],
    598 	    addr, size, &pmh->memh) != 0)
    599 		printf("mem_map failed, offset %x\n", (uint32_t)addr);
    600 	pmh->realsize = TS102_ARBITRARY_MAP_SIZE - addr;
    601 	pmh->size = size;
    602 	*offsetp = 0;
    603 	*windowp = 0;
    604 
    605 #ifdef TSLOT_DEBUG
    606 	printf("->%x/%x]", (uint32_t)pmh->memh, (uint32_t)size);
    607 #endif
    608 
    609 	return (0);
    610 }
    611 
    612 static void
    613 tslot_mem_unmap(pcmcia_chipset_handle_t pch, int win)
    614 {
    615 #ifdef TSLOT_DEBUG
    616 	printf("[mem unmap %d]", win);
    617 #endif
    618 }
    619 
    620 static void
    621 tslot_slot_disable(pcmcia_chipset_handle_t pch)
    622 {
    623 	struct tslot_data *td = (struct tslot_data *)pch;
    624 	int status;
    625 
    626 #ifdef TSLOT_DEBUG
    627 	printf("%s: disable slot %d\n",
    628 	    device_xname(td->td_parent->sc_dev), td->td_slot);
    629 #endif
    630 
    631 	status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
    632 
    633 	status &= ~TS102_CARD_STS_ACEN;
    634 
    635 	/*
    636 	 * Disable interrupts, except for insertion.
    637 	 */
    638 	TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
    639 	    TS102_CARD_INT_MASK_CARDDETECT_STATUS);
    640 
    641 	/*
    642 	 * Power down the socket and disable access
    643 	 */
    644 	status &= ~TS102_CARD_STS_ACEN;
    645 	status &= ~(TS102_CARD_STS_VPP1_MASK | TS102_CARD_STS_VPP2_MASK);
    646 	status |= TS102_CARD_STS_VCCEN;
    647 	TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
    648 
    649 	/*
    650 	 * wait 300ms until power fails (Tpf).
    651 	 */
    652 	tslot_delay(td->td_parent, 300);
    653 }
    654 
    655 static void
    656 tslot_slot_enable(pcmcia_chipset_handle_t pch)
    657 {
    658 	struct tslot_data *td = (struct tslot_data *)pch;
    659 	int status, intr, i;
    660 
    661 #ifdef TSLOT_DEBUG
    662 	printf("%s: enable slot %d\n",
    663 	    device_xname(td->td_parent->sc_dev), td->td_slot);
    664 #endif
    665 
    666 	/* Power down the socket to reset it */
    667 	status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
    668 	TSPRINTF("status: %x\n", status);
    669 
    670 	status &= ~TS102_CARD_STS_ACEN;
    671 	status &= ~(TS102_CARD_STS_VPP1_MASK | TS102_CARD_STS_VPP2_MASK);
    672 	status |= TS102_CARD_STS_VCCEN;
    673 	TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
    674 
    675 	/*
    676 	 * wait 300ms until power fails (Tpf).  Then, wait 100ms since we
    677 	 * are changing Vcc (Toff).
    678 	 */
    679 	tslot_delay(td->td_parent, 300 + 100);
    680 
    681 	/*
    682 	 * Power on the card if not already done, and enable card access
    683 	 */
    684 	status |= TS102_CARD_STS_ACEN;
    685 	status |= TS102_CARD_STS_VPP1_VCC;
    686 	status &= ~TS102_CARD_STS_VCCEN;
    687 	TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
    688 
    689 	/*
    690 	 * wait 100ms until power raise (Tpr) and 20ms to become
    691 	 * stable (Tsu(Vcc)).
    692 	 */
    693 	tslot_delay(td->td_parent, 100 + 20);
    694 
    695 	/*
    696 	 * hold RESET at least 20us.
    697 	 */
    698 	intr = TSLOT_READ(td, TS102_REG_CARD_A_INT);
    699 	delay(20);
    700 	TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
    701 	    intr & ~TS102_CARD_INT_SOFT_RESET);
    702 
    703 	/* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
    704 	tslot_delay(td->td_parent, 20);
    705 
    706 	/* We need level-triggered interrupts for PC Card hardware */
    707 	TSLOT_WRITE(td, TS102_REG_CARD_A_STS,
    708 		TSLOT_READ(td, TS102_REG_CARD_A_STS) | TS102_CARD_STS_LVL);
    709 
    710 	/*
    711 	 * Wait until the card is unbusy. If it is still busy after 3 seconds,
    712 	 * give up. We could enable card interrupts and wait for the interrupt
    713 	 * to happen when BUSY is released, but the interrupt could also be
    714 	 * triggered by the card itself if it's an I/O card, so better poll
    715 	 * here.
    716 	 */
    717 	for (i = 30000; i != 0; i--) {
    718 		status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
    719 		/* If the card has been removed, abort */
    720 		if ((status & TS102_CARD_STS_PRES) == 0) {
    721 			tslot_slot_disable(pch);
    722 			return;
    723 		}
    724 		if (status & TS102_CARD_STS_RDY)
    725 			break;
    726 		else
    727 			delay(100);
    728 	}
    729 
    730 	if (i == 0) {
    731 		printf("%s: slot %d still busy after 3 seconds, status 0x%x\n",
    732 		    device_xname(td->td_parent->sc_dev), td->td_slot,
    733 		    TSLOT_READ(td, TS102_REG_CARD_A_STS));
    734 		return;
    735 	}
    736 }
    737 static void
    738 tslot_event_thread(void *v)
    739 {
    740 	struct tslot_softc *sc = v;
    741 	struct tslot_data *td;
    742 	int s, status;
    743 	unsigned int socket;
    744 
    745 #if NTCTRL > 0
    746 	int i;
    747 
    748 	/*
    749 	 * First-time setup of our LCD symbol. When a card is present at boot
    750 	 * time we won't detect a change here and therefore the LCD symbol won't
    751 	 * light up.
    752 	 */
    753 	for (i = 0; i < TS102_NUM_SLOTS; i++) {
    754 		td = &sc->sc_slot[i];
    755 		status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
    756 		tslot_update_lcd(sc, i, status & TS102_CARD_STS_PRES);
    757 	}
    758 #endif
    759 
    760 	for (;;) {
    761 		s = splhigh();
    762 
    763 		if ((socket = ffs(sc->sc_events)) == 0) {
    764 			splx(s);
    765 			tsleep(&sc->sc_events, PWAIT, "tslot_event", hz * 30);
    766 			continue;
    767 		}
    768 		socket--;
    769 		sc->sc_events &= ~(1 << socket);
    770 		splx(s);
    771 
    772 		if (socket >= TS102_NUM_SLOTS) {
    773 #ifdef DEBUG
    774 			printf("%s: invalid slot number %d\n",
    775 			    device_xname(sc->sc_dev), socket);
    776 #endif
    777 			continue;
    778 		}
    779 
    780 		td = &sc->sc_slot[socket];
    781 		status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
    782 
    783 		if (status & TS102_CARD_STS_PRES) {
    784 			/* Card insertion */
    785 			if ((td->td_status & TS_CARD) == 0) {
    786 				td->td_status |= TS_CARD;
    787 				tslot_update_lcd(sc, socket, 1);
    788 				pcmcia_card_attach(td->td_pcmcia);
    789 			}
    790 		} else {
    791 			/* Card removal */
    792 			if ((td->td_status & TS_CARD) != 0) {
    793 				tslot_update_lcd(sc, socket, 0);
    794 				td->td_status &= ~TS_CARD;
    795 				pcmcia_card_detach(td->td_pcmcia,
    796 				    DETACH_FORCE);
    797 			}
    798 		}
    799 	}
    800 }
    801 
    802 /*
    803  * Interrupt handling
    804  */
    805 
    806 static int
    807 tslot_intr(void *v)
    808 {
    809 	struct tslot_softc *sc = v;
    810 	struct tslot_data *td;
    811 	int intregs[TS102_NUM_SLOTS], *intreg;
    812 	int i, s, rc = 0;
    813 
    814 	s = splhigh();
    815 
    816 	/*
    817 	 * Scan slots, and acknowledge the interrupt if necessary first
    818 	 */
    819 	for (i = 0; i < TS102_NUM_SLOTS; i++) {
    820 		td = &sc->sc_slot[i];
    821 		intreg = &intregs[i];
    822 		*intreg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
    823 
    824 		/*
    825 		 * Acknowledge all interrupt situations at once, even if they
    826 		 * did not occur.
    827 		 */
    828 		if ((*intreg & (TS102_CARD_INT_STATUS_IRQ |
    829 		    TS102_CARD_INT_STATUS_WP_STATUS_CHANGED |
    830 		    TS102_CARD_INT_STATUS_BATTERY_STATUS_CHANGED |
    831 		    TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED)) != 0) {
    832 			rc = 1;
    833 			TSLOT_WRITE(td, TS102_REG_CARD_A_INT, *intreg |
    834 			    TS102_CARD_INT_RQST_IRQ |
    835 			    TS102_CARD_INT_RQST_WP_STATUS_CHANGED |
    836 			    TS102_CARD_INT_RQST_BATTERY_STATUS_CHANGED |
    837 			    TS102_CARD_INT_RQST_CARDDETECT_STATUS_CHANGED);
    838 		}
    839 	}
    840 
    841 #ifdef TSLOT_DEBUG
    842 	printf("tslot_intr: %x %x\n", intregs[0], intregs[1]);
    843 #endif
    844 
    845 	/*
    846 	 * Invoke the interrupt handler for each slot
    847 	 */
    848 	for (i = 0; i < TS102_NUM_SLOTS; i++) {
    849 		td = &sc->sc_slot[i];
    850 		intreg = &intregs[i];
    851 
    852 		if ((*intreg & (TS102_CARD_INT_STATUS_IRQ |
    853 		    TS102_CARD_INT_STATUS_WP_STATUS_CHANGED |
    854 		    TS102_CARD_INT_STATUS_BATTERY_STATUS_CHANGED |
    855 		    TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED)) != 0)
    856 			tslot_slot_intr(td, *intreg);
    857 	}
    858 	splx(s);
    859 
    860 	return (rc);
    861 }
    862 
    863 static void
    864 tslot_queue_event(struct tslot_softc *sc, int slot)
    865 {
    866 	int s;
    867 
    868 	s = splhigh();
    869 	sc->sc_events |= (1 << slot);
    870 	splx(s);
    871 	wakeup(&sc->sc_events);
    872 }
    873 
    874 static void
    875 tslot_slot_intr(struct tslot_data *td, int intreg)
    876 {
    877 	struct tslot_softc *sc = td->td_parent;
    878 	int status, sockstat;
    879 	uint32_t ireg;
    880 
    881 	status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
    882 #ifdef TSLOT_DEBUG
    883 	printf("%s: interrupt on socket %d ir %x sts %x\n",
    884 	    device_xname(sc->sc_dev), td->td_slot, intreg, status);
    885 #else
    886 	__USE(status);
    887 #endif
    888 
    889 	sockstat = td->td_status;
    890 
    891 	/*
    892 	 * The TS102 queues interrupt request, and may trigger an interrupt
    893 	 * for a condition the driver does not want to receive anymore (for
    894 	 * example, after a card gets removed).
    895 	 * Thus, only proceed if the driver is currently allowing a particular
    896 	 * condition.
    897 	 */
    898 
    899 	if ((intreg & TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED) != 0 &&
    900 	    (intreg & TS102_CARD_INT_MASK_CARDDETECT_STATUS) != 0) {
    901 		tslot_queue_event(sc, td->td_slot);
    902 #ifdef TSLOT_DEBUG
    903 		printf("%s: slot %d status changed from %d to %d\n",
    904 		    device_xname(sc->sc_dev), td->td_slot, sockstat,
    905 		    td->td_status);
    906 #endif
    907 		/*
    908 		 * Ignore extra interrupt bits, they are part of the change.
    909 		 */
    910 		return;
    911 	}
    912 
    913 	if ((intreg & TS102_CARD_INT_STATUS_IRQ) != 0 &&
    914 	    (intreg & TS102_CARD_INT_MASK_IRQ) != 0) {
    915 		/* ignore interrupts if we have a pending state change */
    916 		if (sc->sc_events & (1 << td->td_slot))
    917 		{
    918 			TSPRINTF("ev: %d\n", sc->sc_events);
    919 			return;
    920 		}
    921 		if ((sockstat & TS_CARD) == 0) {
    922 			printf("%s: spurious interrupt on slot %d isr %x\n",
    923 			    device_xname(sc->sc_dev), td->td_slot, intreg);
    924 			return;
    925 		}
    926 
    927 		if (td->td_intr != NULL) {
    928 
    929 			if (td->td_softint != NULL)
    930 				sparc_softintr_schedule(td->td_softint);
    931 			/*
    932 			 * Disable this sbus interrupt, until the soft-int
    933 			 * handler had a chance to run
    934 			 */
    935 			ireg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
    936 			TSLOT_WRITE(td, TS102_REG_CARD_A_INT, ireg &
    937 			    ~TS102_CARD_INT_MASK_IRQ);
    938 		}
    939 	}
    940 }
    941 
    942 static void
    943 tslot_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
    944 {
    945 	struct tslot_data *td = (struct tslot_data *)pch;
    946 
    947 	td->td_intr = NULL;
    948 	td->td_intrarg = NULL;
    949 	if (td->td_softint) {
    950 		sparc_softintr_disestablish(td->td_softint);
    951 		td->td_softint = NULL;
    952 	}
    953 }
    954 
    955 const char *
    956 tslot_intr_string(pcmcia_chipset_handle_t pch, void *ih)
    957 {
    958 	if (ih == NULL)
    959 		return ("couldn't establish interrupt");
    960 	else
    961 		return ("");	/* nothing for now */
    962 }
    963 
    964 static void *
    965 tslot_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
    966     int ipl, int (*handler)(void *), void *arg)
    967 {
    968 	struct tslot_data *td = (struct tslot_data *)pch;
    969 
    970 	td->td_intr = handler;
    971 	td->td_intrarg = arg;
    972 	td->td_softint = sparc_softintr_establish(ipl, tslot_intr_dispatch, td);
    973 
    974 	return (td);
    975 }
    976 
    977 /*
    978  * Softinterrupt called to invoke the real driver interrupt handler.
    979  */
    980 static void
    981 tslot_intr_dispatch(void *arg)
    982 {
    983 	struct tslot_data *td = arg;
    984 	int s;
    985 	uint32_t ireg;
    986 
    987 	/* invoke driver handler */
    988 	td->td_intr(td->td_intrarg);
    989 
    990 	/* enable SBUS interrupts for pcmcia interrupts again */
    991 	s = splhigh();
    992 	ireg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
    993 	TSLOT_WRITE(td, TS102_REG_CARD_A_INT, ireg | TS102_CARD_INT_MASK_IRQ);
    994 	splx(s);
    995 }
    996 
    997 static void
    998 tslot_slot_settype(pcmcia_chipset_handle_t pch, int type)
    999 {
   1000 	struct tslot_data *td = (struct tslot_data *)pch;
   1001 	uint32_t reg;
   1002 
   1003 	/*
   1004 	 * Enable the card interrupts if this is an I/O card.
   1005 	 * Note that the TS102_CARD_STS_IO bit in the status register will
   1006 	 * never get set, despite what the documentation says!
   1007 	 */
   1008 	TSPRINTF("tslot_slot_settype(%d)\n",type);
   1009 	if (type == PCMCIA_IFTYPE_IO) {
   1010 		TSLOT_WRITE(td, TS102_REG_CARD_A_STS,
   1011 		    TSLOT_READ(td, TS102_REG_CARD_A_STS) | TS102_CARD_STS_IO);
   1012 		TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
   1013 		    TS102_CARD_INT_MASK_CARDDETECT_STATUS |
   1014 		    TS102_CARD_INT_MASK_IRQ);
   1015 		reg=TSLOT_READ(td, TS102_REG_CARD_A_STS);
   1016 		TSPRINTF("status: %x\n", reg);
   1017 	}
   1018 }
   1019 
   1020 static void
   1021 tslot_update_lcd(struct tslot_softc *sc, int socket, int status)
   1022 {
   1023 #if NTCTRL > 0
   1024 	int was = (sc->sc_active != 0), is;
   1025 	int mask = 1 << socket;
   1026 
   1027 	if (status > 0) {
   1028 		sc->sc_active |= mask;
   1029 	} else {
   1030 		sc->sc_active &= (mask ^ 3);
   1031 	}
   1032 	is = (sc->sc_active != 0);
   1033 	if (was != is) {
   1034 		tadpole_set_lcd(is, 0x40);
   1035 	}
   1036 #endif
   1037 }
   1038 
   1039 /*
   1040  * Delay and possibly yield CPU.
   1041  * XXX - assumes a context
   1042  */
   1043 void
   1044 tslot_delay(struct tslot_softc *sc, unsigned int ms)
   1045 {
   1046 	unsigned int ticks = mstohz(ms);
   1047 
   1048 	if (cold || ticks == 0) {
   1049 		delay(ms);
   1050 		return;
   1051 	}
   1052 
   1053 #ifdef DIAGNOSTIC
   1054 	if (ticks > 60*hz)
   1055 		panic("tslot: preposterous delay: %u", ticks);
   1056 #endif
   1057 	tsleep(sc, 0, "tslotdel", ticks);
   1058 }
   1059