Home | History | Annotate | Line # | Download | only in sbus
stp4020.c revision 1.19
      1 /*	$NetBSD: stp4020.c,v 1.19 2002/03/10 16:18:44 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * STP4020: SBus/PCMCIA bridge supporting two Type-3 PCMCIA cards.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: stp4020.c,v 1.19 2002/03/10 16:18:44 martin Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/errno.h>
     49 #include <sys/malloc.h>
     50 #include <sys/extent.h>
     51 #include <sys/proc.h>
     52 #include <sys/kernel.h>
     53 #include <sys/kthread.h>
     54 #include <sys/device.h>
     55 
     56 #include <dev/pcmcia/pcmciareg.h>
     57 #include <dev/pcmcia/pcmciavar.h>
     58 #include <dev/pcmcia/pcmciachip.h>
     59 
     60 #include <machine/bus.h>
     61 #include <machine/intr.h>
     62 
     63 #include <dev/sbus/sbusvar.h>
     64 #include <dev/sbus/stp4020reg.h>
     65 
     66 #define STP4020_DEBUG 1	/* XXX-temp */
     67 
     68 /*
     69  * We use the three available windows per socket in a simple, fixed
     70  * arrangement. Each window maps (at full 1 MB size) one of the pcmcia
     71  * spaces into sbus space.
     72  */
     73 #define STP_WIN_ATTR	0	/* index of the attribute memory space window */
     74 #define	STP_WIN_MEM	1	/* index of the common memory space window */
     75 #define	STP_WIN_IO	2	/* index of the io space window */
     76 
     77 
     78 #if defined(STP4020_DEBUG)
     79 int stp4020_debug = 0;
     80 #define DPRINTF(x)	do { if (stp4020_debug) printf x; } while(0)
     81 #else
     82 #define DPRINTF(x)
     83 #endif
     84 
     85 /*
     86  * Event queue; events detected in an interrupt context go here
     87  * awaiting attention from our event handling thread.
     88  */
     89 struct stp4020_event {
     90 	SIMPLEQ_ENTRY(stp4020_event) se_q;
     91 	int	se_type;
     92 	int	se_sock;
     93 };
     94 /* Defined event types */
     95 #define STP4020_EVENT_INSERTION	0
     96 #define STP4020_EVENT_REMOVAL	1
     97 
     98 /*
     99  * Per socket data.
    100  */
    101 struct stp4020_socket {
    102 	struct stp4020_softc	*sc;	/* Back link */
    103 	int		flags;
    104 #define STP4020_SOCKET_BUSY	0x0001
    105 #define STP4020_SOCKET_SHUTDOWN	0x0002
    106 	int		sock;		/* Socket number (0 or 1) */
    107 	bus_space_tag_t	tag;		/* socket control space */
    108 	bus_space_handle_t	regs;	/* 			*/
    109 	struct device	*pcmcia;	/* Associated PCMCIA device */
    110 	int		(*intrhandler)	/* Card driver interrupt handler */
    111 			    __P((void *));
    112 	void		*intrarg;	/* Card interrupt handler argument */
    113 	int		ipl;		/* Interrupt level suggested by card */
    114 	struct {
    115 		bus_space_handle_t	winaddr;/* this window's address */
    116 	} windows[STP4020_NWIN];
    117 
    118 };
    119 
    120 struct stp4020_softc {
    121 	struct device	sc_dev;		/* Base device */
    122 	struct sbusdev	sc_sd;		/* SBus device */
    123 	bus_space_tag_t	sc_bustag;
    124 	bus_dma_tag_t	sc_dmatag;
    125 	pcmcia_chipset_tag_t	sc_pct;	/* Chipset methods */
    126 
    127 	struct proc	*event_thread;		/* event handling thread */
    128 	SIMPLEQ_HEAD(, stp4020_event)	events;	/* Pending events for thread */
    129 
    130 	struct stp4020_socket sc_socks[STP4020_NSOCK];
    131 };
    132 
    133 
    134 static int	stp4020print	__P((void *, const char *));
    135 static int	stp4020match	__P((struct device *, struct cfdata *, void *));
    136 static void	stp4020attach	__P((struct device *, struct device *, void *));
    137 static int	stp4020_iointr	__P((void *));
    138 static int	stp4020_statintr __P((void *));
    139 static void	stp4020_map_window(struct stp4020_socket *h, int win, int speed);
    140 static void	stp4020_calc_speed(int bus_speed, int ns, int *length, int *delay);
    141 static int	dummy_splraise(int ipl);
    142 
    143 struct cfattach nell_ca = {
    144 	sizeof(struct stp4020_softc), stp4020match, stp4020attach
    145 };
    146 
    147 #ifdef STP4020_DEBUG
    148 static void	stp4020_dump_regs __P((struct stp4020_socket *));
    149 #endif
    150 
    151 static int	stp4020_rd_sockctl __P((struct stp4020_socket *, int));
    152 static void	stp4020_wr_sockctl __P((struct stp4020_socket *, int, int));
    153 static int	stp4020_rd_winctl __P((struct stp4020_socket *, int, int));
    154 static void	stp4020_wr_winctl __P((struct stp4020_socket *, int, int, int));
    155 
    156 void	stp4020_delay __P((unsigned int));
    157 void	stp4020_attach_socket __P((struct stp4020_socket *, int));
    158 void	stp4020_create_event_thread __P((void *));
    159 void	stp4020_event_thread __P((void *));
    160 void	stp4020_queue_event __P((struct stp4020_softc *, int, int));
    161 
    162 int	stp4020_chip_mem_alloc __P((pcmcia_chipset_handle_t, bus_size_t,
    163 				    struct pcmcia_mem_handle *));
    164 void	stp4020_chip_mem_free __P((pcmcia_chipset_handle_t,
    165 				   struct pcmcia_mem_handle *));
    166 int	stp4020_chip_mem_map __P((pcmcia_chipset_handle_t, int, bus_addr_t,
    167 				  bus_size_t, struct pcmcia_mem_handle *,
    168 				  bus_size_t *, int *));
    169 void	stp4020_chip_mem_unmap __P((pcmcia_chipset_handle_t, int));
    170 
    171 int	stp4020_chip_io_alloc __P((pcmcia_chipset_handle_t,
    172 				   bus_addr_t, bus_size_t, bus_size_t,
    173 				   struct pcmcia_io_handle *));
    174 void	stp4020_chip_io_free __P((pcmcia_chipset_handle_t,
    175 				  struct pcmcia_io_handle *));
    176 int	stp4020_chip_io_map __P((pcmcia_chipset_handle_t, int, bus_addr_t,
    177 				 bus_size_t, struct pcmcia_io_handle *, int *));
    178 void	stp4020_chip_io_unmap __P((pcmcia_chipset_handle_t, int));
    179 
    180 void	stp4020_chip_socket_enable __P((pcmcia_chipset_handle_t));
    181 void	stp4020_chip_socket_disable __P((pcmcia_chipset_handle_t));
    182 void	*stp4020_chip_intr_establish __P((pcmcia_chipset_handle_t,
    183 					  struct pcmcia_function *, int,
    184 					  int (*) __P((void *)), void *));
    185 void	stp4020_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *));
    186 
    187 /* Our PCMCIA chipset methods */
    188 static struct pcmcia_chip_functions stp4020_functions = {
    189 	stp4020_chip_mem_alloc,
    190 	stp4020_chip_mem_free,
    191 	stp4020_chip_mem_map,
    192 	stp4020_chip_mem_unmap,
    193 
    194 	stp4020_chip_io_alloc,
    195 	stp4020_chip_io_free,
    196 	stp4020_chip_io_map,
    197 	stp4020_chip_io_unmap,
    198 
    199 	stp4020_chip_intr_establish,
    200 	stp4020_chip_intr_disestablish,
    201 
    202 	stp4020_chip_socket_enable,
    203 	stp4020_chip_socket_disable
    204 };
    205 
    206 
    207 static __inline__ int
    208 stp4020_rd_sockctl(h, idx)
    209 	struct stp4020_socket *h;
    210 	int idx;
    211 {
    212 	int o = ((STP4020_SOCKREGS_SIZE * (h->sock)) + idx);
    213 	return (bus_space_read_2(h->tag, h->regs, o));
    214 }
    215 
    216 static __inline__ void
    217 stp4020_wr_sockctl(h, idx, v)
    218 	struct stp4020_socket *h;
    219 	int idx;
    220 	int v;
    221 {
    222 	int o = (STP4020_SOCKREGS_SIZE * (h->sock)) + idx;
    223 	bus_space_write_2(h->tag, h->regs, o, v);
    224 }
    225 
    226 static __inline__ int
    227 stp4020_rd_winctl(h, win, idx)
    228 	struct stp4020_socket *h;
    229 	int win;
    230 	int idx;
    231 {
    232 	int o = (STP4020_SOCKREGS_SIZE * (h->sock)) +
    233 		(STP4020_WINREGS_SIZE * win) + idx;
    234 	return (bus_space_read_2(h->tag, h->regs, o));
    235 }
    236 
    237 static __inline__ void
    238 stp4020_wr_winctl(h, win, idx, v)
    239 	struct stp4020_socket *h;
    240 	int win;
    241 	int idx;
    242 	int v;
    243 {
    244 	int o = (STP4020_SOCKREGS_SIZE * (h->sock)) +
    245 		(STP4020_WINREGS_SIZE * win) + idx;
    246 
    247 	bus_space_write_2(h->tag, h->regs, o, v);
    248 }
    249 
    250 
    251 int
    252 stp4020print(aux, busname)
    253 	void *aux;
    254 	const char *busname;
    255 {
    256 	struct pcmciabus_attach_args *paa = aux;
    257 	struct stp4020_socket *h = paa->pch;
    258 
    259 	printf(" socket %d", h->sock);
    260 	return (UNCONF);
    261 }
    262 
    263 int
    264 stp4020match(parent, cf, aux)
    265 	struct device *parent;
    266 	struct cfdata *cf;
    267 	void *aux;
    268 {
    269 	struct sbus_attach_args *sa = aux;
    270 
    271 	return (strcmp("SUNW,pcmcia", sa->sa_name) == 0);
    272 }
    273 
    274 /*
    275  * Attach all the sub-devices we can find
    276  */
    277 void
    278 stp4020attach(parent, self, aux)
    279 	struct device *parent, *self;
    280 	void *aux;
    281 {
    282 	struct sbus_attach_args *sa = aux;
    283 	struct stp4020_softc *sc = (void *)self;
    284 	int node, rev;
    285 	int i;
    286 	bus_space_handle_t bh;
    287 
    288 	node = sa->sa_node;
    289 
    290 	/* Transfer bus tags */
    291 	sc->sc_bustag = sa->sa_bustag;
    292 	sc->sc_dmatag = sa->sa_dmatag;
    293 
    294 	/* Set up per-socket static initialization */
    295 	sc->sc_socks[0].sc = sc->sc_socks[1].sc = sc;
    296 	sc->sc_socks[0].tag = sc->sc_socks[1].tag = sa->sa_bustag;
    297 
    298 	if (sa->sa_nreg < 8) {
    299 		printf("%s: only %d register sets\n",
    300 			self->dv_xname, sa->sa_nreg);
    301 		return;
    302 	}
    303 
    304 	if (sa->sa_nintr != 2) {
    305 		printf("%s: expect 2 interrupt Sbus levels; got %d\n",
    306 			self->dv_xname, sa->sa_nintr);
    307 		return;
    308 	}
    309 
    310 #define STP4020_BANK_PROM	0
    311 #define STP4020_BANK_CTRL	4
    312 	for (i = 0; i < 8; i++) {
    313 
    314 		/*
    315 		 * STP4020 Register address map:
    316 		 *	bank  0:   Forth PROM
    317 		 *	banks 1-3: socket 0, windows 0-2
    318 		 *	bank  4:   control registers
    319 		 *	banks 5-7: socket 1, windows 0-2
    320 		 */
    321 
    322 		if (i == STP4020_BANK_PROM)
    323 			/* Skip the PROM */
    324 			continue;
    325 
    326 		if (sbus_bus_map(sa->sa_bustag,
    327 				 sa->sa_reg[i].sbr_slot,
    328 				 sa->sa_reg[i].sbr_offset,
    329 				 sa->sa_reg[i].sbr_size,
    330 				 BUS_SPACE_MAP_LINEAR, 0,
    331 				 &bh) != 0) {
    332 			printf("%s: attach: cannot map registers\n",
    333 				self->dv_xname);
    334 			return;
    335 		}
    336 
    337 		if (i == STP4020_BANK_CTRL) {
    338 			/*
    339 			 * Copy tag and handle to both socket structures
    340 			 * for easy access in control/status IO functions.
    341 			 */
    342 			sc->sc_socks[0].regs = sc->sc_socks[1].regs = bh;
    343 		} else if (i < STP4020_BANK_CTRL) {
    344 			/* banks 1-3 */
    345 			sc->sc_socks[0].windows[i-1].winaddr = bh;
    346 		} else {
    347 			/* banks 5-7 */
    348 			sc->sc_socks[1].windows[i-5].winaddr = bh;
    349 		}
    350 	}
    351 
    352 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
    353 
    354 	/*
    355 	 * We get to use two SBus interrupt levels.
    356 	 * The higher level we use for status change interrupts;
    357 	 * the lower level for PC card I/O.
    358 	 */
    359 	if (sa->sa_nintr != 0) {
    360 		bus_intr_establish(sa->sa_bustag, sa->sa_intr[1].sbi_pri,
    361 				   IPL_NONE, 0, stp4020_statintr, sc);
    362 
    363 		bus_intr_establish(sa->sa_bustag, sa->sa_intr[0].sbi_pri,
    364 				   IPL_NONE, 0, stp4020_iointr, sc);
    365 	}
    366 
    367 	rev = stp4020_rd_sockctl(&sc->sc_socks[0], STP4020_ISR1_IDX) &
    368 		STP4020_ISR1_REV_M;
    369 	printf(": rev %x\n", rev);
    370 
    371 	sc->sc_pct = (pcmcia_chipset_tag_t)&stp4020_functions;
    372 
    373 	/*
    374 	 * Arrange that a kernel thread be created to handle
    375 	 * insert/removal events.
    376 	 */
    377 	SIMPLEQ_INIT(&sc->events);
    378 	kthread_create(stp4020_create_event_thread, sc);
    379 
    380 	for (i = 0; i < STP4020_NSOCK; i++) {
    381 		struct stp4020_socket *h = &sc->sc_socks[i];
    382 		h->sock = i;
    383 		h->sc = sc;
    384 #ifdef STP4020_DEBUG
    385 		if (stp4020_debug)
    386 			stp4020_dump_regs(h);
    387 #endif
    388 		stp4020_attach_socket(h, sa->sa_frequency);
    389 	}
    390 }
    391 
    392 void
    393 stp4020_attach_socket(h, speed)
    394 	struct stp4020_socket *h;
    395 	int speed;
    396 {
    397 	struct pcmciabus_attach_args paa;
    398 	int v;
    399 
    400 	/* Map all three windows */
    401 	stp4020_map_window(h, STP_WIN_ATTR, speed);
    402 	stp4020_map_window(h, STP_WIN_MEM, speed);
    403 	stp4020_map_window(h, STP_WIN_IO, speed);
    404 
    405 	/* Configure one pcmcia device per socket */
    406 	paa.paa_busname = "pcmcia";
    407 	paa.pct = (pcmcia_chipset_tag_t)h->sc->sc_pct;
    408 	paa.pch = (pcmcia_chipset_handle_t)h;
    409 	paa.iobase = 0;
    410 	paa.iosize = STP4020_WINDOW_SIZE;
    411 
    412 	h->pcmcia = config_found(&h->sc->sc_dev, &paa, stp4020print);
    413 
    414 	if (h->pcmcia == NULL)
    415 		return;
    416 
    417 	/*
    418 	 * There's actually a pcmcia bus attached; initialize the slot.
    419 	 */
    420 
    421 	/*
    422 	 * Clear things up before we enable status change interrupts.
    423 	 * This seems to not be fully initialized by the PROM.
    424 	 */
    425 	stp4020_wr_sockctl(h, STP4020_ICR1_IDX, 0);
    426 	stp4020_wr_sockctl(h, STP4020_ICR0_IDX, 0);
    427 	stp4020_wr_sockctl(h, STP4020_ISR1_IDX, 0x3fff);
    428 	stp4020_wr_sockctl(h, STP4020_ISR0_IDX, 0x3fff);
    429 
    430 	/*
    431 	 * Enable socket status change interrupts.
    432 	 * We use SB_INT[1] for status change interrupts.
    433 	 */
    434 	v = STP4020_ICR0_ALL_STATUS_IE | STP4020_ICR0_SCILVL_SB1;
    435 	stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
    436 
    437 	/* Get live status bits from ISR0 */
    438 	v = stp4020_rd_sockctl(h, STP4020_ISR0_IDX);
    439 	if ((v & (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)) == 0)
    440 		return;
    441 
    442 	pcmcia_card_attach(h->pcmcia);
    443 	h->flags |= STP4020_SOCKET_BUSY;
    444 }
    445 
    446 
    447 /*
    448  * Deferred thread creation callback.
    449  */
    450 void
    451 stp4020_create_event_thread(arg)
    452 	void *arg;
    453 {
    454 	struct stp4020_softc *sc = arg;
    455 	const char *name = sc->sc_dev.dv_xname;
    456 
    457 	if (kthread_create1(stp4020_event_thread, sc, &sc->event_thread,
    458 			   "%s", name)) {
    459 		panic("%s: unable to create event thread", name);
    460 	}
    461 }
    462 
    463 /*
    464  * The actual event handling thread.
    465  */
    466 void
    467 stp4020_event_thread(arg)
    468 	void *arg;
    469 {
    470 	struct stp4020_softc *sc = arg;
    471 	struct stp4020_event *e;
    472 	int s;
    473 
    474 	while (1) {
    475 		struct stp4020_socket *h;
    476 		int n;
    477 
    478 		s = splhigh();
    479 		if ((e = SIMPLEQ_FIRST(&sc->events)) == NULL) {
    480 			splx(s);
    481 			(void)tsleep(&sc->events, PWAIT, "pcicev", 0);
    482 			continue;
    483 		}
    484 		SIMPLEQ_REMOVE_HEAD(&sc->events, e, se_q);
    485 		splx(s);
    486 
    487 		n = e->se_sock;
    488 		if (n < 0 || n >= STP4020_NSOCK)
    489 			panic("stp4020_event_thread: wayward socket number %d",
    490 			      n);
    491 
    492 		h = &sc->sc_socks[n];
    493 		switch (e->se_type) {
    494 		case STP4020_EVENT_INSERTION:
    495 			pcmcia_card_attach(h->pcmcia);
    496 			break;
    497 		case STP4020_EVENT_REMOVAL:
    498 			pcmcia_card_detach(h->pcmcia, DETACH_FORCE);
    499 			break;
    500 		default:
    501 			panic("stp4020_event_thread: unknown event type %d",
    502 			      e->se_type);
    503 		}
    504 		free(e, M_TEMP);
    505 	}
    506 }
    507 
    508 void
    509 stp4020_queue_event(sc, sock, event)
    510 	struct stp4020_softc *sc;
    511 	int sock, event;
    512 {
    513 	struct stp4020_event *e;
    514 	int s;
    515 
    516 	e = malloc(sizeof(*e), M_TEMP, M_NOWAIT);
    517 	if (e == NULL)
    518 		panic("stp4020_queue_event: can't allocate event");
    519 
    520 	e->se_type = event;
    521 	e->se_sock = sock;
    522 	s = splhigh();
    523 	SIMPLEQ_INSERT_TAIL(&sc->events, e, se_q);
    524 	splx(s);
    525 	wakeup(&sc->events);
    526 }
    527 
    528 int
    529 stp4020_statintr(arg)
    530 	void *arg;
    531 {
    532 	struct stp4020_softc *sc = arg;
    533 	int i, r = 0;
    534 
    535 	/*
    536 	 * Check each socket for pending requests.
    537 	 */
    538 	for (i = 0 ; i < STP4020_NSOCK; i++) {
    539 		struct stp4020_socket *h;
    540 		int v, cd_change = 0;
    541 
    542 		h = &sc->sc_socks[i];
    543 
    544 		/* Read socket's ISR0 for the interrupt status bits */
    545 		v = stp4020_rd_sockctl(h, STP4020_ISR0_IDX);
    546 
    547 #ifdef STP4020_DEBUG
    548 		if (stp4020_debug != 0) {
    549 			char bits[64];
    550 			bitmask_snprintf(v, STP4020_ISR0_IOBITS,
    551 					 bits, sizeof(bits));
    552 			printf("stp4020_statintr: ISR0=%s\n", bits);
    553 		}
    554 #endif
    555 
    556 		/* Ack all interrupts at once */
    557 		stp4020_wr_sockctl(h, STP4020_ISR0_IDX, STP4020_ISR0_ALL_STATUS_IRQ);
    558 
    559 		if ((v & STP4020_ISR0_CDCHG) != 0) {
    560 			/*
    561 			 * Card status change detect
    562 			 */
    563 			cd_change = 1;
    564 			r = 1;
    565 			if ((v & (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)) == (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)){
    566 				if ((h->flags & STP4020_SOCKET_BUSY) == 0) {
    567 					stp4020_queue_event(sc, i,
    568 						STP4020_EVENT_INSERTION);
    569 					h->flags |= STP4020_SOCKET_BUSY;
    570 				}
    571 			}
    572 			if ((v & (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)) == 0){
    573 				if ((h->flags & STP4020_SOCKET_BUSY) != 0) {
    574 					stp4020_queue_event(sc, i,
    575 						STP4020_EVENT_REMOVAL);
    576 					h->flags &= ~STP4020_SOCKET_BUSY;
    577 				}
    578 			}
    579 		}
    580 
    581 		/* informational messages */
    582 		if ((v & STP4020_ISR0_BVD1CHG) != 0) {
    583 			/* ignore if this is caused by insert or removal */
    584 			if (!cd_change)
    585 				printf("stp4020[%d]: Battery change 1\n", h->sock);
    586 			r = 1;
    587 		}
    588 
    589 		if ((v & STP4020_ISR0_BVD2CHG) != 0) {
    590 			/* ignore if this is caused by insert or removal */
    591 			if (!cd_change)
    592 				printf("stp4020[%d]: Battery change 2\n", h->sock);
    593 			r = 1;
    594 		}
    595 
    596 		if ((v & STP4020_ISR0_RDYCHG) != 0) {
    597 			DPRINTF(("stp4020[%d]: Ready/Busy change\n", h->sock));
    598 			r = 1;
    599 		}
    600 
    601 		if ((v & STP4020_ISR0_WPCHG) != 0) {
    602 			DPRINTF(("stp4020[%d]: Write protect change\n", h->sock));
    603 			r = 1;
    604 		}
    605 
    606 		if ((v & STP4020_ISR0_PCTO) != 0) {
    607 			DPRINTF(("stp4020[%d]: Card access timeout\n", h->sock));
    608 			r = 1;
    609 		}
    610 
    611 	}
    612 
    613 	return (r);
    614 }
    615 
    616 static int
    617 dummy_splraise(int ipl)
    618 {
    619 	switch(ipl) {
    620 	case IPL_SOFTCLOCK:
    621 		return splsoftclock();
    622 	case IPL_BIO:
    623 		return splbio();
    624 	case IPL_NET:
    625 		return splnet();
    626 	case IPL_SOFTSERIAL:
    627 		return splserial();	/* XXX ? */
    628 	case IPL_TTY:
    629 		return spltty();
    630 	case IPL_IMP:
    631 		return splhigh();	/* XXX ? */
    632 	case IPL_AUDIO:
    633 		return splaudio();
    634 	case IPL_CLOCK:
    635 		return splclock();
    636 	case IPL_SERIAL:
    637 		return splserial();
    638 	case IPL_HIGH:
    639 		return splhigh();
    640 	}
    641 	panic("illegal pcmcia interrupt level");
    642 }
    643 
    644 int
    645 stp4020_iointr(arg)
    646 	void *arg;
    647 {
    648 	struct stp4020_softc *sc = arg;
    649 	int i, r = 0, s;
    650 
    651 	/*
    652 	 * Check each socket for pending requests.
    653 	 */
    654 	for (i = 0 ; i < STP4020_NSOCK; i++) {
    655 		struct stp4020_socket *h;
    656 		int v;
    657 
    658 		h = &sc->sc_socks[i];
    659 		v = stp4020_rd_sockctl(h, STP4020_ISR0_IDX);
    660 
    661 		if ((v & STP4020_ISR0_IOINT) != 0) {
    662 			/* we can not deny this is ours, no matter what the
    663 			   card driver says. */
    664 			r = 1;
    665 			/* ack interrupt */
    666 			stp4020_wr_sockctl(h, STP4020_ISR0_IDX, v);
    667 
    668 			/* It's a card interrupt */
    669 			if ((h->flags & STP4020_SOCKET_BUSY) == 0) {
    670 				printf("stp4020[%d]: spurious interrupt?\n",
    671 					h->sock);
    672 				continue;
    673 			}
    674 			/* Call card handler, if any */
    675 			if (h->intrhandler != NULL) {
    676 				s = dummy_splraise(h->ipl);
    677 				(*h->intrhandler)(h->intrarg);
    678 				splx(s);
    679 			}
    680 		}
    681 
    682 	}
    683 
    684 	return (r);
    685 }
    686 
    687 /*
    688  * The function gets the sbus speed and a access time and calculates
    689  * values for the CMDLNG and CMDDLAY registers.
    690  */
    691 static void
    692 stp4020_calc_speed(int bus_speed, int ns, int *length, int *delay)
    693 {
    694 	int result;
    695 
    696 	if (ns < STP4020_MEM_SPEED_MIN)
    697 		ns = STP4020_MEM_SPEED_MIN;
    698 	else if (ns > STP4020_MEM_SPEED_MAX)
    699 		ns = STP4020_MEM_SPEED_MAX;
    700 	result = ns*(bus_speed/1000);
    701 	if (result % 1000000)
    702 		result = result/1000000 + 1;
    703 	else
    704 		result /= 1000000;
    705 	*length = result;
    706 
    707 	/* the sbus frequency range is limited, so we can keep this simple */
    708 	*delay = ns <= STP4020_MEM_SPEED_MIN? 1 : 2;
    709 }
    710 
    711 static void
    712 stp4020_map_window(struct stp4020_socket *h, int win, int speed)
    713 {
    714 	int v, length, delay;
    715 
    716 	/*
    717 	 * According to the PC Card standard 300ns access timing should be
    718 	 * used for attribute memory access. Our pcmcia framework does not
    719 	 * seem to propagate timing information, so we use that
    720 	 * everywhere.
    721 	 */
    722 	stp4020_calc_speed(speed, 300, &length, &delay);
    723 
    724 	/*
    725 	 * Fill in the Address Space Select and Base Address
    726 	 * fields of this windows control register 0.
    727 	 */
    728 	v = ((delay << STP4020_WCR0_CMDDLY_S)&STP4020_WCR0_CMDDLY_M)
    729 	    | ((length << STP4020_WCR0_CMDLNG_S)&STP4020_WCR0_CMDLNG_M);
    730 	switch (win) {
    731 	case STP_WIN_ATTR:
    732 		v |= STP4020_WCR0_ASPSEL_AM;
    733 		break;
    734 	case STP_WIN_MEM:
    735 		v |= STP4020_WCR0_ASPSEL_CM;
    736 		break;
    737 	case STP_WIN_IO:
    738 		v |= STP4020_WCR0_ASPSEL_IO;
    739 		break;
    740 	}
    741 	v |= (STP4020_ADDR2PAGE(0) & STP4020_WCR0_BASE_M);
    742 	stp4020_wr_winctl(h, win, STP4020_WCR0_IDX, v);
    743 	stp4020_wr_winctl(h, win, STP4020_WCR1_IDX, 1<<STP4020_WCR1_WAITREQ_S);
    744 }
    745 
    746 int
    747 stp4020_chip_mem_alloc(pch, size, pcmhp)
    748 	pcmcia_chipset_handle_t pch;
    749 	bus_size_t size;
    750 	struct pcmcia_mem_handle *pcmhp;
    751 {
    752 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    753 
    754 	/* we can not do much here, defere work to _mem_map */
    755 	pcmhp->memt = h->tag;
    756 	pcmhp->size = size;
    757 	pcmhp->addr = 0;
    758 	pcmhp->mhandle = 0;
    759 	pcmhp->realsize = size;
    760 
    761 	return (0);
    762 }
    763 
    764 void
    765 stp4020_chip_mem_free(pch, pcmhp)
    766 	pcmcia_chipset_handle_t pch;
    767 	struct pcmcia_mem_handle *pcmhp;
    768 {
    769 }
    770 
    771 int
    772 stp4020_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
    773 	pcmcia_chipset_handle_t pch;
    774 	int kind;
    775 	bus_addr_t card_addr;
    776 	bus_size_t size;
    777 	struct pcmcia_mem_handle *pcmhp;
    778 	bus_size_t *offsetp;
    779 	int *windowp;
    780 {
    781 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    782 	int win = (kind&PCMCIA_MEM_ATTR)? STP_WIN_ATTR : STP_WIN_MEM;
    783 
    784 	pcmhp->memt = h->tag;
    785 	bus_space_subregion(h->tag, h->windows[win].winaddr, card_addr, size, &pcmhp->memh);
    786 	pcmhp->size = size;
    787 	pcmhp->realsize = STP4020_WINDOW_SIZE - card_addr;
    788 	*offsetp = 0;
    789 	*windowp = 0;
    790 
    791 	return (0);
    792 }
    793 
    794 void
    795 stp4020_chip_mem_unmap(pch, win)
    796 	pcmcia_chipset_handle_t pch;
    797 	int win;
    798 {
    799 }
    800 
    801 int
    802 stp4020_chip_io_alloc(pch, start, size, align, pcihp)
    803 	pcmcia_chipset_handle_t pch;
    804 	bus_addr_t start;
    805 	bus_size_t size;
    806 	bus_size_t align;
    807 	struct pcmcia_io_handle *pcihp;
    808 {
    809 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    810 
    811 	pcihp->iot = h->tag;
    812 	pcihp->ioh = h->windows[STP_WIN_IO].winaddr;
    813 	return 0;
    814 }
    815 
    816 void
    817 stp4020_chip_io_free(pch, pcihp)
    818 	pcmcia_chipset_handle_t pch;
    819 	struct pcmcia_io_handle *pcihp;
    820 {
    821 }
    822 
    823 int
    824 stp4020_chip_io_map(pch, width, offset, size, pcihp, windowp)
    825 	pcmcia_chipset_handle_t pch;
    826 	int width;
    827 	bus_addr_t offset;
    828 	bus_size_t size;
    829 	struct pcmcia_io_handle *pcihp;
    830 	int *windowp;
    831 {
    832 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    833 
    834 	pcihp->iot = h->tag;
    835 	bus_space_subregion(h->tag, h->windows[STP_WIN_IO].winaddr, offset, size, &pcihp->ioh);
    836 	*windowp = 0;
    837 	return 0;
    838 }
    839 
    840 void
    841 stp4020_chip_io_unmap(pch, win)
    842 	pcmcia_chipset_handle_t pch;
    843 	int win;
    844 {
    845 }
    846 
    847 void
    848 stp4020_chip_socket_enable(pch)
    849 	pcmcia_chipset_handle_t pch;
    850 {
    851 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    852 	int i, v;
    853 
    854 	/* this bit is mostly stolen from pcic_attach_card */
    855 
    856 	/* Power down the socket to reset it, clear the card reset pin */
    857 	stp4020_wr_sockctl(h, STP4020_ICR1_IDX, 0);
    858 
    859 	/*
    860 	 * wait 300ms until power fails (Tpf).  Then, wait 100ms since
    861 	 * we are changing Vcc (Toff).
    862 	 */
    863 	stp4020_delay((300 + 100) * 1000);
    864 
    865 	/* Power up the socket */
    866 	v = STP4020_ICR1_MSTPWR;
    867 	stp4020_wr_sockctl(h, STP4020_ICR1_IDX, v);
    868 
    869 	/*
    870 	 * wait 100ms until power raise (Tpr) and 20ms to become
    871 	 * stable (Tsu(Vcc)).
    872 	 */
    873 	stp4020_delay((100 + 20) * 1000);
    874 
    875 	v |= STP4020_ICR1_PCIFOE|STP4020_ICR1_VPP1_VCC;
    876 	stp4020_wr_sockctl(h, STP4020_ICR1_IDX, v);
    877 
    878 	/*
    879 	 * hold RESET at least 10us.
    880 	 */
    881 	delay(10);
    882 
    883 	/* Clear reset flag */
    884 	v = stp4020_rd_sockctl(h, STP4020_ICR0_IDX);
    885 	v &= ~STP4020_ICR0_RESET;
    886 	stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
    887 
    888 	/* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
    889 	stp4020_delay(20000);
    890 
    891 	/* Wait for the chip to finish initializing (5 seconds max) */
    892 	for (i = 10000; i > 0; i--) {
    893 		v = stp4020_rd_sockctl(h, STP4020_ISR0_IDX);
    894 		if ((v & STP4020_ISR0_RDYST) != 0)
    895 			break;
    896 		delay(500);
    897 	}
    898 	if (i <= 0) {
    899 		char bits[64];
    900 		bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ISR0_IDX),
    901 				 STP4020_ISR0_IOBITS, bits, sizeof(bits));
    902 		printf("stp4020_chip_socket_enable: not ready: status %s\n",
    903 			bits);
    904 		return;
    905 	}
    906 
    907 	v = stp4020_rd_sockctl(h, STP4020_ICR0_IDX);
    908 
    909 	/*
    910 	 * Check the card type.
    911 	 * Enable socket I/O interrupts for IO cards.
    912 	 * We use level SB_INT[0] for I/O interrupts.
    913 	 */
    914 	if (pcmcia_card_gettype(h->pcmcia) == PCMCIA_IFTYPE_IO) {
    915 		v &= ~(STP4020_ICR0_IOILVL|STP4020_ICR0_IFTYPE);
    916 		v |= STP4020_ICR0_IFTYPE_IO|STP4020_ICR0_IOIE
    917 		    |STP4020_ICR0_IOILVL_SB0|STP4020_ICR0_SPKREN;
    918 		DPRINTF(("%s: configuring card for IO useage\n", h->sc->sc_dev.dv_xname));
    919 	} else {
    920 		v &= ~(STP4020_ICR0_IOILVL|STP4020_ICR0_IFTYPE
    921 		    |STP4020_ICR0_SPKREN|STP4020_ICR0_IOILVL_SB0
    922 		    |STP4020_ICR0_IOILVL_SB1|STP4020_ICR0_SPKREN);
    923 		v |= STP4020_ICR0_IFTYPE_MEM;
    924 		DPRINTF(("%s: configuring card for MEM ONLY useage\n", h->sc->sc_dev.dv_xname));
    925 	}
    926 	stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
    927 }
    928 
    929 void
    930 stp4020_chip_socket_disable(pch)
    931 	pcmcia_chipset_handle_t pch;
    932 {
    933 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    934 	int v;
    935 
    936 	/*
    937 	 * Disable socket I/O interrupts.
    938 	 */
    939 	v = stp4020_rd_sockctl(h, STP4020_ICR0_IDX);
    940 	v &= ~(STP4020_ICR0_IOIE | STP4020_ICR0_IOILVL);
    941 	stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
    942 
    943 	/* Power down the socket */
    944 	stp4020_wr_sockctl(h, STP4020_ICR1_IDX, 0);
    945 
    946 	/*
    947 	 * wait 300ms until power fails (Tpf).
    948 	 */
    949 	stp4020_delay(300 * 1000);
    950 }
    951 
    952 void *
    953 stp4020_chip_intr_establish(pch, pf, ipl, handler, arg)
    954 	pcmcia_chipset_handle_t pch;
    955 	struct pcmcia_function *pf;
    956 	int ipl;
    957 	int (*handler) __P((void *));
    958 	void *arg;
    959 {
    960 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    961 
    962 	h->intrhandler = handler;
    963 	h->intrarg = arg;
    964 	h->ipl = ipl;
    965 	return h;
    966 }
    967 
    968 void
    969 stp4020_chip_intr_disestablish(pch, ih)
    970 	pcmcia_chipset_handle_t pch;
    971 	void *ih;
    972 {
    973 	struct stp4020_socket *h = (struct stp4020_socket *)pch;
    974 
    975 	h->intrhandler = NULL;
    976 	h->intrarg = NULL;
    977 }
    978 
    979 /*
    980  * Delay and possibly yield CPU.
    981  * XXX - assumes a context
    982  */
    983 void
    984 stp4020_delay(ms)
    985 	unsigned int ms;
    986 {
    987 	unsigned int ticks;
    988 
    989 	/* Convert to ticks */
    990 	ticks = (ms * hz ) / 1000000;
    991 
    992 	if (cold || ticks == 0) {
    993 		delay(ms);
    994 		return;
    995 	}
    996 
    997 #ifdef DIAGNOSTIC
    998 	if (ticks > 60*hz)
    999 		panic("stp4020: preposterous delay: %u", ticks);
   1000 #endif
   1001 	tsleep(&ticks, 0, "stp4020_delay", ticks);
   1002 }
   1003 
   1004 #ifdef STP4020_DEBUG
   1005 void
   1006 stp4020_dump_regs(h)
   1007 	struct stp4020_socket *h;
   1008 {
   1009 	char bits[64];
   1010 	/*
   1011 	 * Dump control and status registers.
   1012 	 */
   1013 	printf("socket[%d] registers:\n", h->sock);
   1014 	bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ICR0_IDX),
   1015 			 STP4020_ICR0_BITS, bits, sizeof(bits));
   1016 	printf("\tICR0=%s\n", bits);
   1017 
   1018 	bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ICR1_IDX),
   1019 			 STP4020_ICR1_BITS, bits, sizeof(bits));
   1020 	printf("\tICR1=%s\n", bits);
   1021 
   1022 	bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ISR0_IDX),
   1023 			 STP4020_ISR0_IOBITS, bits, sizeof(bits));
   1024 	printf("\tISR0=%s\n", bits);
   1025 
   1026 	bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ISR1_IDX),
   1027 			 STP4020_ISR1_BITS, bits, sizeof(bits));
   1028 	printf("\tISR1=%s\n", bits);
   1029 }
   1030 #endif /* STP4020_DEBUG */
   1031