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