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