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