Home | History | Annotate | Line # | Download | only in ep93xx
eppcic.c revision 1.6.2.1
      1  1.6.2.1      yamt /*	$NetBSD: eppcic.c,v 1.6.2.1 2012/10/30 17:19:01 yamt Exp $	*/
      2      1.1  hamajima 
      3      1.1  hamajima /*
      4      1.1  hamajima  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
      5      1.1  hamajima  *
      6      1.1  hamajima  * Redistribution and use in source and binary forms, with or without
      7      1.1  hamajima  * modification, are permitted provided that the following conditions
      8      1.1  hamajima  * are met:
      9      1.1  hamajima  * 1. Redistributions of source code must retain the above copyright
     10      1.1  hamajima  *    notice, this list of conditions and the following disclaimer.
     11      1.1  hamajima  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  hamajima  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  hamajima  *    documentation and/or other materials provided with the distribution.
     14      1.1  hamajima  *
     15      1.1  hamajima  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16      1.1  hamajima  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17      1.1  hamajima  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18      1.1  hamajima  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19      1.1  hamajima  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20      1.1  hamajima  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21      1.1  hamajima  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22      1.1  hamajima  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23      1.1  hamajima  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24      1.1  hamajima  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25      1.1  hamajima  * SUCH DAMAGE.
     26      1.1  hamajima  */
     27      1.1  hamajima 
     28      1.1  hamajima #include <sys/cdefs.h>
     29  1.6.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: eppcic.c,v 1.6.2.1 2012/10/30 17:19:01 yamt Exp $");
     30      1.1  hamajima 
     31      1.1  hamajima #include <sys/param.h>
     32      1.1  hamajima #include <sys/systm.h>
     33      1.1  hamajima #include <sys/kernel.h>
     34      1.1  hamajima #include <sys/malloc.h>
     35      1.1  hamajima #include <sys/device.h>
     36      1.1  hamajima #include <sys/kthread.h>
     37      1.1  hamajima #include <uvm/uvm_param.h>
     38      1.5    dyoung #include <sys/bus.h>
     39      1.1  hamajima #include <dev/pcmcia/pcmciareg.h>
     40      1.1  hamajima #include <dev/pcmcia/pcmciavar.h>
     41      1.1  hamajima #include <dev/pcmcia/pcmciachip.h>
     42      1.1  hamajima #include <arm/ep93xx/epsocvar.h>
     43      1.1  hamajima #include <arm/ep93xx/epgpiovar.h>
     44      1.1  hamajima #include <arm/ep93xx/eppcicvar.h>
     45      1.1  hamajima #include <arm/ep93xx/ep93xxreg.h>
     46      1.1  hamajima #include <arm/ep93xx/epsmcreg.h>
     47      1.1  hamajima #include "epled.h"
     48      1.1  hamajima #if NEPLED > 0
     49      1.1  hamajima #include <arm/ep93xx/epledvar.h>
     50      1.1  hamajima #endif
     51      1.1  hamajima 
     52      1.1  hamajima #include "epgpio.h"
     53      1.1  hamajima #if NEPGPIO == 0
     54      1.1  hamajima #error "epgpio requires in eppcic"
     55      1.1  hamajima #endif
     56      1.1  hamajima 
     57      1.1  hamajima #ifdef EPPCIC_DEBUG
     58      1.1  hamajima int eppcic_debug = EPPCIC_DEBUG;
     59      1.1  hamajima #define DPRINTFN(n,x)	if (eppcic_debug>(n)) printf x;
     60      1.1  hamajima #else
     61      1.1  hamajima #define DPRINTFN(n,x)
     62      1.1  hamajima #endif
     63      1.1  hamajima 
     64      1.1  hamajima /* Mem & I/O */
     65      1.1  hamajima #define	SOCKET0_MCCD1	1	/* pin36/pin26 (negative) Card Detect 1 */
     66      1.1  hamajima #define	SOCKET0_MCCD2	2	/* pin67/pin25 (negative) Card Detect 2 */
     67      1.1  hamajima #define	SOCKET0_VS1	5	/* pin33/pin43 (negative) Voltage Sense 1 */
     68      1.1  hamajima #define	SOCKET0_VS2	7	/* pin57/pin40 (negative) Voltage Sense 2 */
     69      1.1  hamajima /* Memory */
     70      1.1  hamajima #define	SOCKET0_WP	0	/* pin33/pin24 Write Protect */
     71      1.1  hamajima #define	SOCKET0_MCBVD1	3	/* pin63/pin46 Battery Voltage Detect 1 */
     72      1.1  hamajima #define	SOCKET0_MCBVD2	4	/* pin62/pin45 Battery Voltage Detect 2 */
     73      1.1  hamajima #define	SOCKET0_READY	6	/* pin16/pin37 Ready */
     74      1.1  hamajima /* I/O */
     75      1.1  hamajima #define	SOCKET0_STSCHG	3	/* pin63/pin46 (negative) Status Change */
     76      1.1  hamajima #define	SOCKET0_SPKR	4	/* pin62/pin45 (negative) Speaker */
     77      1.1  hamajima #define	SOCKET0_IREQ	6	/* pin16/pin37 Interrupt Request */
     78      1.1  hamajima 
     79      1.1  hamajima struct eppcic_handle {
     80      1.1  hamajima 	int			ph_socket;	/* socket number */
     81      1.1  hamajima 	struct eppcic_softc	*ph_sc;
     82  1.6.2.1      yamt 	device_t		ph_card;
     83      1.1  hamajima 	int			(*ph_ih_func)(void *);
     84      1.1  hamajima 	void			*ph_ih_arg;
     85      1.2        ad 	lwp_t			*ph_event_thread;
     86      1.1  hamajima 	int			ph_run;		/* ktread running */
     87      1.1  hamajima 	int			ph_width;	/* 8 or 16 */
     88      1.1  hamajima 	int			ph_vcc;		/* 3 or 5 */
     89      1.1  hamajima 	int			ph_status[2];	/* cd1 and cd2 */
     90      1.1  hamajima 	int			ph_port;	/* GPIO port */
     91      1.1  hamajima 	int			ph_cd[2];	/* card detect */
     92      1.1  hamajima 	int			ph_vs[2];	/* voltage sense */
     93      1.1  hamajima 	int			ph_ireq;	/* interrupt request */
     94      1.1  hamajima 	struct {
     95      1.1  hamajima 		bus_size_t	reg;
     96      1.1  hamajima 		bus_addr_t	base;
     97      1.1  hamajima 		bus_size_t	size;
     98      1.1  hamajima 	} ph_space[3];
     99      1.1  hamajima #define	IO		0
    100      1.1  hamajima #define	COMMON		1
    101      1.1  hamajima #define	ATTRIBUTE	2
    102      1.1  hamajima };
    103      1.1  hamajima 
    104      1.1  hamajima static int eppcic_intr_carddetect(void *);
    105      1.1  hamajima static int eppcic_intr_socket(void *);
    106      1.1  hamajima static int eppcic_print(void *, const char *);
    107      1.1  hamajima static void eppcic_event_thread(void *);
    108      1.1  hamajima void eppcic_shutdown(void *);
    109      1.1  hamajima 
    110      1.1  hamajima static int eppcic_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
    111      1.1  hamajima 			    struct pcmcia_mem_handle *);
    112      1.1  hamajima static void eppcic_mem_free(pcmcia_chipset_handle_t,
    113      1.1  hamajima 			    struct pcmcia_mem_handle *);
    114      1.1  hamajima static int eppcic_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
    115      1.1  hamajima 			  struct pcmcia_mem_handle *, bus_size_t *, int *);
    116      1.1  hamajima static void eppcic_mem_unmap(pcmcia_chipset_handle_t, int);
    117      1.1  hamajima static int eppcic_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
    118      1.1  hamajima 			   bus_size_t, struct pcmcia_io_handle *);
    119      1.1  hamajima static void eppcic_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
    120      1.1  hamajima static int eppcic_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
    121      1.1  hamajima 			 struct pcmcia_io_handle *, int *);
    122      1.1  hamajima static void eppcic_io_unmap(pcmcia_chipset_handle_t, int);
    123      1.1  hamajima static void *eppcic_intr_establish(pcmcia_chipset_handle_t,
    124      1.1  hamajima 				   struct pcmcia_function *,
    125      1.1  hamajima 				   int, int (*)(void *), void *);
    126      1.1  hamajima static void eppcic_intr_disestablish(pcmcia_chipset_handle_t, void *);
    127      1.1  hamajima static void eppcic_socket_enable(pcmcia_chipset_handle_t);
    128      1.1  hamajima static void eppcic_socket_disable(pcmcia_chipset_handle_t);
    129      1.1  hamajima static void eppcic_socket_settype(pcmcia_chipset_handle_t, int);
    130      1.1  hamajima 
    131      1.1  hamajima static void eppcic_attach_socket(struct eppcic_handle *);
    132      1.1  hamajima static void eppcic_config_socket(struct eppcic_handle *);
    133      1.1  hamajima static int eppcic_get_voltage(struct eppcic_handle *);
    134      1.1  hamajima static void eppcic_set_pcreg(struct eppcic_handle *, int);
    135      1.1  hamajima 
    136      1.1  hamajima static struct pcmcia_chip_functions eppcic_functions = {
    137      1.1  hamajima 	eppcic_mem_alloc,	eppcic_mem_free,
    138      1.1  hamajima 	eppcic_mem_map,		eppcic_mem_unmap,
    139      1.1  hamajima 	eppcic_io_alloc,	eppcic_io_free,
    140      1.1  hamajima 	eppcic_io_map,		eppcic_io_unmap,
    141      1.1  hamajima 	eppcic_intr_establish,	eppcic_intr_disestablish,
    142      1.1  hamajima 	eppcic_socket_enable,	eppcic_socket_disable,
    143      1.1  hamajima 	eppcic_socket_settype
    144      1.1  hamajima };
    145      1.1  hamajima 
    146      1.1  hamajima void
    147  1.6.2.1      yamt eppcic_attach_common(device_t parent, device_t self, void *aux,
    148      1.1  hamajima 		     eppcic_chipset_tag_t pcic)
    149      1.1  hamajima {
    150  1.6.2.1      yamt 	struct eppcic_softc *sc = device_private(self);
    151      1.1  hamajima 	struct epsoc_attach_args *sa = aux;
    152      1.1  hamajima 	struct eppcic_handle *ph;
    153      1.1  hamajima 	int reg;
    154      1.1  hamajima 	int i;
    155      1.1  hamajima 
    156      1.1  hamajima 	if (!sa->sa_gpio) {
    157  1.6.2.1      yamt 		printf("%s: epgpio requires\n", device_xname(self));
    158      1.1  hamajima 		return;
    159      1.1  hamajima 	}
    160  1.6.2.1      yamt 	sc->sc_dev = self;
    161      1.1  hamajima 	sc->sc_gpio = sa->sa_gpio;
    162      1.1  hamajima 	sc->sc_iot = sa->sa_iot;
    163      1.1  hamajima 	sc->sc_hclk = sa->sa_hclk;
    164      1.1  hamajima 	sc->sc_pcic = pcic;
    165      1.1  hamajima 	sc->sc_enable = 0;
    166      1.1  hamajima 	if (bus_space_map(sa->sa_iot, sa->sa_addr,
    167      1.1  hamajima 			  sa->sa_size, 0, &sc->sc_ioh)){
    168  1.6.2.1      yamt 		printf("%s: Cannot map registers\n", device_xname(self));
    169      1.1  hamajima 		return;
    170      1.1  hamajima 	}
    171      1.1  hamajima 	printf("\n");
    172      1.1  hamajima 
    173      1.1  hamajima #if NEPLED > 0
    174      1.1  hamajima 	epled_green_on();
    175      1.1  hamajima 	epled_red_off();
    176      1.1  hamajima #endif
    177      1.1  hamajima 	/* socket 0 */
    178      1.1  hamajima 	if (!(ph = malloc(sizeof(struct eppcic_handle), M_DEVBUF, M_NOWAIT))) {
    179  1.6.2.1      yamt 		printf("%s: Cannot allocate memory\n", device_xname(self));
    180      1.1  hamajima 		return; /* ENOMEM */
    181      1.1  hamajima 	}
    182      1.1  hamajima 	sc->sc_ph[0] = ph;
    183      1.1  hamajima 	ph->ph_sc = sc;
    184      1.1  hamajima 	ph->ph_socket = 0;
    185      1.1  hamajima 	ph->ph_port = PORT_F;
    186      1.1  hamajima 	ph->ph_cd[0] = SOCKET0_MCCD1;
    187      1.1  hamajima 	ph->ph_cd[1] = SOCKET0_MCCD2;
    188      1.1  hamajima 	ph->ph_vs[0] = SOCKET0_VS1;
    189      1.1  hamajima 	ph->ph_vs[1] = SOCKET0_VS2;
    190      1.1  hamajima 	ph->ph_ireq = SOCKET0_IREQ;
    191      1.1  hamajima 	ph->ph_space[IO].reg = EP93XX_PCMCIA0_IO;
    192      1.1  hamajima 	ph->ph_space[IO].base = EP93XX_PCMCIA0_HWBASE + EP93XX_PCMCIA_IO;
    193      1.1  hamajima 	ph->ph_space[IO].size = EP93XX_PCMCIA_IO_SIZE;
    194      1.1  hamajima 	ph->ph_space[COMMON].reg = EP93XX_PCMCIA0_Common;
    195      1.1  hamajima 	ph->ph_space[COMMON].base = EP93XX_PCMCIA0_HWBASE
    196      1.1  hamajima 				    + EP93XX_PCMCIA_COMMON;
    197      1.1  hamajima 	ph->ph_space[COMMON].size = EP93XX_PCMCIA_COMMON_SIZE;
    198      1.1  hamajima 	ph->ph_space[ATTRIBUTE].reg = EP93XX_PCMCIA0_Attribute;
    199      1.1  hamajima 	ph->ph_space[ATTRIBUTE].base = EP93XX_PCMCIA0_HWBASE
    200      1.1  hamajima 				       + EP93XX_PCMCIA_ATTRIBUTE;
    201      1.1  hamajima 	ph->ph_space[ATTRIBUTE].size = EP93XX_PCMCIA_ATTRIBUTE_SIZE;
    202      1.1  hamajima 	eppcic_attach_socket(ph);
    203      1.1  hamajima 
    204      1.1  hamajima 	reg = EP93XX_PCMCIA_WEN | (pcic->socket_type)(sc, 0);
    205      1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_PCMCIA_Ctrl,
    206      1.1  hamajima 			  EP93XX_PCMCIA_RST | reg);
    207      1.1  hamajima 	delay(10);
    208      1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_PCMCIA_Ctrl, reg);
    209      1.1  hamajima 	delay(500);
    210      1.1  hamajima 
    211      1.1  hamajima 	for (i = 0; i < EP93XX_PCMCIA_NSOCKET; i++)
    212      1.1  hamajima 		eppcic_config_socket(sc->sc_ph[i]);
    213      1.1  hamajima #if NEPLED > 0
    214      1.1  hamajima 	epled_green_off();
    215      1.1  hamajima #endif
    216      1.1  hamajima }
    217      1.1  hamajima 
    218      1.1  hamajima static void
    219      1.1  hamajima eppcic_attach_socket(struct eppcic_handle *ph)
    220      1.1  hamajima {
    221      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    222      1.1  hamajima 
    223      1.1  hamajima 	ph->ph_width = 16;
    224      1.1  hamajima 	ph->ph_vcc = 3;
    225      1.1  hamajima 	ph->ph_event_thread = NULL;
    226      1.1  hamajima 	ph->ph_run = 0;
    227      1.1  hamajima 	ph->ph_ih_func = NULL;
    228      1.1  hamajima 	ph->ph_ih_arg = NULL;
    229      1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    230      1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    231      1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_vs[0]);
    232      1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_vs[1]);
    233      1.1  hamajima 	ph->ph_status[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    234      1.1  hamajima 	ph->ph_status[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    235      1.1  hamajima }
    236      1.1  hamajima 
    237      1.1  hamajima static void
    238      1.1  hamajima eppcic_config_socket(struct eppcic_handle *ph)
    239      1.1  hamajima {
    240      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    241      1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    242      1.1  hamajima 	struct pcmciabus_attach_args paa;
    243      1.1  hamajima 	int wait;
    244      1.1  hamajima 
    245      1.1  hamajima 	paa.paa_busname = "pcmcia";
    246      1.1  hamajima 	paa.pct = (pcmcia_chipset_tag_t)&eppcic_functions;
    247      1.1  hamajima 	paa.pch = (pcmcia_chipset_handle_t)ph;
    248      1.1  hamajima 	ph->ph_card = config_found_ia((void*)sc, "pcmciabus", &paa,
    249      1.1  hamajima 				      eppcic_print);
    250      1.1  hamajima 
    251      1.1  hamajima 	epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_cd[0],
    252      1.1  hamajima 			      EDGE_TRIGGER | FALLING_EDGE | DEBOUNCE,
    253      1.1  hamajima 			      IPL_TTY, eppcic_intr_carddetect, ph);
    254      1.1  hamajima 	epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_cd[1],
    255      1.1  hamajima 			      EDGE_TRIGGER | RISING_EDGE | DEBOUNCE,
    256      1.1  hamajima 			      IPL_TTY, eppcic_intr_carddetect, ph);
    257      1.1  hamajima 	wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_OFF);
    258      1.1  hamajima 	delay(wait);
    259      1.1  hamajima 
    260      1.1  hamajima 
    261      1.1  hamajima 	ph->ph_status[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    262      1.1  hamajima 	ph->ph_status[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    263      1.1  hamajima 
    264      1.2        ad 	DPRINTFN(1, ("eppcic_config_socket: cd1=%d, cd2=%d\n",ph->ph_status[0],ph->ph_status[1]));
    265      1.1  hamajima 
    266      1.1  hamajima 	ph->ph_run = 1;
    267      1.2        ad 	kthread_create(PRI_NONE, 0, NULL, eppcic_event_thread, ph,
    268  1.6.2.1      yamt 	    &ph->ph_event_thread, "%s,%d", device_xname(sc->sc_dev),
    269      1.2        ad 	    ph->ph_socket);
    270      1.1  hamajima }
    271      1.1  hamajima 
    272      1.2        ad static int
    273      1.2        ad eppcic_print(void *arg, const char *pnp)
    274      1.2        ad {
    275      1.2        ad 	return (UNCONF);
    276      1.2        ad }
    277      1.2        ad 
    278      1.1  hamajima static void
    279      1.1  hamajima eppcic_event_thread(void *arg)
    280      1.1  hamajima {
    281      1.1  hamajima 	struct eppcic_handle *ph = arg;
    282      1.1  hamajima 
    283      1.4  hamajima 	if (!(ph->ph_status[0] | ph->ph_status[1]))
    284      1.4  hamajima 		pcmcia_card_attach(ph->ph_card);
    285      1.4  hamajima 
    286      1.1  hamajima 	for (;;) {
    287      1.1  hamajima 		tsleep(ph, PWAIT, "CSC wait", 0);
    288      1.1  hamajima 		if (!ph->ph_run)
    289      1.1  hamajima 			break;
    290      1.1  hamajima 
    291      1.1  hamajima 		DPRINTFN(1, ("eppcic_event_thread: cd1=%d, cd2=%d\n",ph->ph_status[0],ph->ph_status[1]));
    292      1.1  hamajima 
    293      1.1  hamajima 		if (!ph->ph_status[0] && !ph->ph_status[1])
    294      1.1  hamajima 			pcmcia_card_attach(ph->ph_card);
    295      1.1  hamajima 		else if (ph->ph_status[0] && ph->ph_status[1])
    296      1.1  hamajima 			pcmcia_card_detach(ph->ph_card, DETACH_FORCE);
    297      1.1  hamajima 	}
    298      1.1  hamajima 
    299      1.1  hamajima 	DPRINTFN(1, ("eppcic_event_thread: run=%d\n",ph->ph_run));
    300      1.1  hamajima 	ph->ph_event_thread = NULL;
    301      1.1  hamajima 	kthread_exit(0);
    302      1.1  hamajima }
    303      1.1  hamajima 
    304      1.1  hamajima void
    305      1.1  hamajima eppcic_shutdown(void *arg)
    306      1.1  hamajima {
    307      1.1  hamajima 	struct eppcic_handle *ph = arg;
    308      1.1  hamajima 
    309      1.1  hamajima 	DPRINTFN(1, ("eppcic_shutdown\n"));
    310      1.1  hamajima 	ph->ph_run = 0;
    311      1.1  hamajima 	wakeup(ph);
    312      1.1  hamajima }
    313      1.1  hamajima 
    314      1.1  hamajima static int
    315      1.1  hamajima eppcic_intr_carddetect(void *arg)
    316      1.1  hamajima {
    317      1.1  hamajima 	struct eppcic_handle *ph = arg;
    318      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    319      1.1  hamajima 	int nstatus[2];
    320      1.1  hamajima 
    321      1.1  hamajima 	nstatus[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    322      1.1  hamajima 	nstatus[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    323      1.1  hamajima 
    324      1.1  hamajima 	DPRINTFN(1, ("eppcic_intr: cd1=%#x, cd2=%#x\n",nstatus[0],nstatus[1]));
    325      1.1  hamajima 
    326      1.4  hamajima 	if (nstatus[0] != ph->ph_status[0] || nstatus[1] != ph->ph_status[1]) {
    327      1.4  hamajima 		ph->ph_status[0] = nstatus[0];
    328      1.4  hamajima 		ph->ph_status[1] = nstatus[1];
    329      1.1  hamajima 		wakeup(ph);
    330      1.4  hamajima 	}
    331      1.1  hamajima 	return 0;
    332      1.1  hamajima }
    333      1.1  hamajima 
    334      1.1  hamajima static int
    335      1.1  hamajima eppcic_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    336      1.1  hamajima 		 struct pcmcia_mem_handle *pmh)
    337      1.1  hamajima {
    338      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    339      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    340      1.1  hamajima 
    341      1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_alloc: size=%#x\n",(unsigned)size));
    342      1.1  hamajima 
    343      1.1  hamajima 	pmh->memt = sc->sc_iot;
    344      1.1  hamajima 	return 0;
    345      1.1  hamajima }
    346      1.1  hamajima 
    347      1.1  hamajima static void
    348      1.1  hamajima eppcic_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
    349      1.1  hamajima {
    350      1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_free\n"));
    351      1.1  hamajima }
    352      1.1  hamajima 
    353      1.1  hamajima static int
    354      1.1  hamajima eppcic_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
    355      1.1  hamajima 	       bus_size_t size, struct pcmcia_mem_handle *pmh,
    356      1.1  hamajima 	       bus_size_t *offsetp, int *windowp)
    357      1.1  hamajima {
    358      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    359      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    360      1.1  hamajima 	bus_addr_t pa;
    361      1.1  hamajima 	int err;
    362      1.1  hamajima 
    363      1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_map: kind=%d, addr=%#x, size=%#x\n",kind,(unsigned)addr,(unsigned)size));
    364      1.1  hamajima 
    365      1.1  hamajima 	pa = addr;
    366      1.1  hamajima 	*offsetp = 0;
    367      1.1  hamajima 	size = round_page(size);
    368      1.1  hamajima 	pmh->realsize = size;
    369      1.1  hamajima 	if (kind & PCMCIA_WIDTH_MEM8)
    370      1.1  hamajima 		ph->ph_width = 8;
    371      1.1  hamajima 	else
    372      1.1  hamajima 		ph->ph_width = 16;
    373      1.1  hamajima 	switch (kind & ~PCMCIA_WIDTH_MEM_MASK) {
    374      1.1  hamajima 	case PCMCIA_MEM_ATTR:
    375      1.4  hamajima 		eppcic_set_pcreg(ph, ATTRIBUTE);
    376      1.1  hamajima 		pa += ph->ph_space[ATTRIBUTE].base;
    377      1.1  hamajima 		break;
    378      1.1  hamajima 	case PCMCIA_MEM_COMMON:
    379      1.4  hamajima 		eppcic_set_pcreg(ph, COMMON);
    380      1.1  hamajima 		pa += ph->ph_space[COMMON].base;
    381      1.1  hamajima 		break;
    382      1.1  hamajima 	default:
    383      1.1  hamajima 		return -1;
    384      1.1  hamajima 	}
    385      1.1  hamajima 
    386      1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_map: pa=%#x, *offsetp=%#x, size=%#x\n",(unsigned)pa,(unsigned)addr,(unsigned)size));
    387      1.1  hamajima 
    388      1.1  hamajima 	if (!(err = bus_space_map(sc->sc_iot, pa, size, 0, &pmh->memh)))
    389      1.1  hamajima 		*windowp = (int)pmh->memh;
    390      1.1  hamajima 	return err;
    391      1.1  hamajima }
    392      1.1  hamajima 
    393      1.1  hamajima static void
    394      1.1  hamajima eppcic_mem_unmap(pcmcia_chipset_handle_t pch, int window)
    395      1.1  hamajima {
    396      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    397      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    398      1.1  hamajima 
    399      1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_unmap: window=%#x\n",window));
    400      1.1  hamajima 
    401      1.1  hamajima 	bus_space_unmap(sc->sc_iot, (bus_addr_t)window, 0x400);
    402      1.1  hamajima }
    403      1.1  hamajima 
    404      1.1  hamajima static int
    405      1.1  hamajima eppcic_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
    406      1.1  hamajima 		bus_size_t align, struct pcmcia_io_handle *pih)
    407      1.1  hamajima {
    408      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    409      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    410      1.1  hamajima 	bus_addr_t pa;
    411      1.1  hamajima 
    412      1.1  hamajima 	DPRINTFN(1, ("eppcic_io_alloc: start=%#x, size=%#x, align=%#x\n",(unsigned)start,(unsigned)size,(unsigned)align));
    413      1.1  hamajima 
    414      1.1  hamajima 	pih->iot = sc->sc_iot;
    415      1.1  hamajima 	pih->addr = start;
    416      1.1  hamajima 	pih->size = size;
    417      1.1  hamajima 	pa = pih->addr + ph->ph_space[IO].base;
    418      1.1  hamajima 	return bus_space_map(sc->sc_iot, pa, size, 0, &pih->ioh);
    419      1.1  hamajima }
    420      1.1  hamajima 
    421      1.1  hamajima static void
    422      1.1  hamajima eppcic_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
    423      1.1  hamajima {
    424      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    425      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    426      1.1  hamajima 
    427      1.1  hamajima 	DPRINTFN(1, ("eppcic_io_free\n"));
    428      1.1  hamajima 
    429      1.1  hamajima 	bus_space_unmap(sc->sc_iot, pih->ioh, pih->size);
    430      1.1  hamajima }
    431      1.1  hamajima 
    432      1.1  hamajima static int
    433      1.1  hamajima eppcic_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
    434      1.1  hamajima 	      bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
    435      1.1  hamajima {
    436      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    437      1.1  hamajima 
    438      1.1  hamajima 	DPRINTFN(1, ("eppcic_io_map: offset=%#x, size=%#x, width=%d",(unsigned)offset,(unsigned)size,width));
    439      1.1  hamajima 
    440      1.1  hamajima 	switch (width) {
    441      1.1  hamajima 	case PCMCIA_WIDTH_IO8:
    442      1.1  hamajima 		DPRINTFN(1, ("(8bit)\n"));
    443      1.1  hamajima 		ph->ph_width = 8;
    444      1.1  hamajima 		break;
    445      1.1  hamajima 	case PCMCIA_WIDTH_IO16:
    446      1.1  hamajima 	case PCMCIA_WIDTH_AUTO:	/* I don't understand how I check it */
    447      1.1  hamajima 		DPRINTFN(1, ("(16bit)\n"));
    448      1.1  hamajima 		ph->ph_width = 16;
    449      1.1  hamajima 		break;
    450      1.1  hamajima 	default:
    451      1.1  hamajima 		DPRINTFN(1, ("(unknown)\n"));
    452      1.1  hamajima 		return -1;
    453      1.1  hamajima 	}
    454      1.4  hamajima 	eppcic_set_pcreg(ph, IO);
    455      1.1  hamajima 	*windowp = 0; /* unused */
    456      1.1  hamajima 	return 0;
    457      1.1  hamajima }
    458      1.1  hamajima 
    459      1.1  hamajima static void
    460      1.1  hamajima eppcic_io_unmap(pcmcia_chipset_handle_t pch, int window)
    461      1.1  hamajima {
    462      1.1  hamajima 	DPRINTFN(1, ("eppcic_io_unmap: window=%#x\n",window));
    463      1.1  hamajima }
    464      1.1  hamajima 
    465      1.1  hamajima static void *
    466      1.1  hamajima eppcic_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
    467      1.1  hamajima 		      int ipl, int (*ih_func)(void *), void *ih_arg)
    468      1.1  hamajima {
    469      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    470      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    471      1.1  hamajima 
    472      1.1  hamajima 	DPRINTFN(1, ("eppcic_intr_establish\n"));
    473      1.1  hamajima 
    474      1.1  hamajima 	if (ph->ph_ih_func)
    475      1.1  hamajima 		return 0;
    476      1.1  hamajima 
    477      1.1  hamajima 	ph->ph_ih_func = ih_func;
    478      1.1  hamajima 	ph->ph_ih_arg = ih_arg;
    479      1.1  hamajima 	return epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_ireq,
    480      1.1  hamajima 				     LEVEL_SENSE | LOW_LEVEL,
    481      1.1  hamajima 				     ipl, eppcic_intr_socket, ph);
    482      1.1  hamajima }
    483      1.1  hamajima 
    484      1.1  hamajima static void
    485      1.1  hamajima eppcic_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
    486      1.1  hamajima {
    487      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    488      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    489      1.1  hamajima 
    490      1.1  hamajima 	DPRINTFN(1, ("eppcic_intr_disestablish\n"));
    491      1.1  hamajima 
    492      1.1  hamajima 	ph->ph_ih_func = NULL;
    493      1.1  hamajima 	ph->ph_ih_arg = NULL;
    494      1.1  hamajima 	epgpio_intr_disestablish(sc->sc_gpio, ph->ph_port, ph->ph_ireq);
    495      1.1  hamajima }
    496      1.1  hamajima 
    497      1.1  hamajima static int
    498      1.1  hamajima eppcic_intr_socket(void *arg)
    499      1.1  hamajima {
    500      1.1  hamajima 	struct eppcic_handle *ph = arg;
    501      1.1  hamajima 	int err = 0;
    502      1.1  hamajima 
    503      1.1  hamajima 	if (ph->ph_ih_func) {
    504      1.1  hamajima #if NEPLED > 0
    505      1.1  hamajima 		epled_red_on();
    506      1.1  hamajima #endif
    507      1.1  hamajima 		err = (*ph->ph_ih_func)(ph->ph_ih_arg);
    508      1.1  hamajima #if NEPLED > 0
    509      1.1  hamajima 		epled_red_off();
    510      1.1  hamajima #endif
    511      1.1  hamajima 	}
    512      1.1  hamajima 	return err;
    513      1.1  hamajima }
    514      1.1  hamajima 
    515      1.1  hamajima 
    516      1.1  hamajima static void
    517      1.1  hamajima eppcic_socket_enable(pcmcia_chipset_handle_t pch)
    518      1.1  hamajima {
    519      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    520      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    521      1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    522      1.1  hamajima 	int wait;
    523      1.1  hamajima 
    524      1.1  hamajima 	DPRINTFN(1, ("eppcic_socket_enable\n"));
    525      1.1  hamajima 
    526      1.1  hamajima 	wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_ON);
    527      1.1  hamajima 	delay(wait);
    528      1.1  hamajima #if NEPLED > 0
    529      1.1  hamajima 	if (!sc->sc_enable++)
    530      1.1  hamajima 		epled_green_on();
    531      1.1  hamajima #endif
    532      1.1  hamajima 	ph->ph_vcc = eppcic_get_voltage(ph);
    533      1.1  hamajima }
    534      1.1  hamajima 
    535      1.1  hamajima static void
    536      1.1  hamajima eppcic_socket_disable(pcmcia_chipset_handle_t pch)
    537      1.1  hamajima {
    538      1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    539      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    540      1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    541      1.1  hamajima 	int wait;
    542      1.1  hamajima 
    543      1.1  hamajima 	DPRINTFN(1, ("eppcic_socket_disable\n"));
    544      1.1  hamajima 
    545      1.1  hamajima 	wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_OFF);
    546      1.1  hamajima 	delay(wait);
    547      1.1  hamajima #if NEPLED > 0
    548      1.1  hamajima 	if (!--sc->sc_enable)
    549      1.1  hamajima 		epled_green_off();
    550      1.1  hamajima #endif
    551      1.1  hamajima }
    552      1.1  hamajima 
    553      1.1  hamajima static void
    554      1.1  hamajima eppcic_socket_settype(pcmcia_chipset_handle_t pch, int type)
    555      1.1  hamajima {
    556      1.1  hamajima 	DPRINTFN(1, ("eppcic_socket_settype: type=%d",type));
    557      1.1  hamajima 
    558      1.1  hamajima 	switch (type) {
    559      1.1  hamajima 	case PCMCIA_IFTYPE_MEMORY:
    560      1.1  hamajima 		DPRINTFN(1, ("(Memory)\n"));
    561      1.1  hamajima 		break;
    562      1.1  hamajima 	case PCMCIA_IFTYPE_IO:
    563      1.1  hamajima 		DPRINTFN(1, ("(I/O)\n"));
    564      1.1  hamajima 		break;
    565      1.1  hamajima 	default:
    566      1.1  hamajima 		DPRINTFN(1, ("(unknown)\n"));
    567      1.1  hamajima 		return;
    568      1.1  hamajima 	}
    569      1.1  hamajima }
    570      1.1  hamajima 
    571      1.1  hamajima static int
    572      1.1  hamajima eppcic_get_voltage(struct eppcic_handle *ph)
    573      1.1  hamajima {
    574      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    575      1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    576      1.1  hamajima 	int cap, vcc = 0;
    577      1.1  hamajima 
    578      1.1  hamajima 	cap = (pcic->power_capability)(sc, ph->ph_socket);
    579      1.1  hamajima 	if (epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_vs[0])) {
    580      1.1  hamajima 		if (cap | VCC_5V)
    581      1.1  hamajima 			vcc = 5;
    582      1.1  hamajima 		else
    583      1.1  hamajima 			printf("%s: unsupported Vcc 5 Volts",
    584  1.6.2.1      yamt 			       device_xname(sc->sc_dev));
    585      1.1  hamajima 	} else {
    586      1.1  hamajima 		if (cap | VCC_3V)
    587      1.1  hamajima 			vcc = 3;
    588      1.1  hamajima 		else
    589      1.1  hamajima 			printf("%s: unsupported Vcc 3.3 Volts",
    590  1.6.2.1      yamt 			       device_xname(sc->sc_dev));
    591      1.1  hamajima 	}
    592      1.1  hamajima 	DPRINTFN(1, ("eppcic_get_voltage: vs1=%d, vs2=%d (%dV)\n",epgpio_read_bit(sc->sc_gpio, ph->ph_port, ph->ph_vs[0]),epgpio_read_bit(sc->sc_gpio, ph->ph_port, ph->ph_vs[1]),vcc));
    593      1.1  hamajima 	return vcc;
    594      1.1  hamajima }
    595      1.1  hamajima 
    596      1.1  hamajima #define	EXTRA_DELAY	40
    597      1.1  hamajima 
    598      1.1  hamajima static void
    599      1.1  hamajima eppcic_set_pcreg(struct eppcic_handle *ph, int kind)
    600      1.1  hamajima {
    601      1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    602      1.1  hamajima 	int atiming, htiming, ptiming;
    603      1.1  hamajima 	int period = 1000000000 / sc->sc_hclk;
    604      1.1  hamajima 	int width;
    605      1.1  hamajima 
    606      1.1  hamajima 	switch (ph->ph_width) {
    607      1.1  hamajima 	case 8:
    608      1.1  hamajima 		width = 0;
    609      1.1  hamajima 		break;
    610      1.1  hamajima 	case 16:
    611      1.1  hamajima 		width = EP93XX_PCMCIA_WIDTH_16;
    612      1.1  hamajima 		break;
    613      1.1  hamajima 	default:
    614      1.1  hamajima 		return;
    615      1.1  hamajima 	}
    616      1.1  hamajima 	switch (kind) {
    617      1.1  hamajima 	case IO:
    618      1.1  hamajima 		atiming = 165; htiming = 20; ptiming = 70;
    619      1.1  hamajima 		break;
    620      1.1  hamajima 	case COMMON:
    621      1.1  hamajima #if linux_timing!=hamajima20050816
    622      1.1  hamajima 		switch (ph->ph_vcc) {
    623      1.1  hamajima 		case 3:
    624      1.1  hamajima 			atiming = 465; htiming = 35; ptiming = 100;
    625      1.1  hamajima 			break;
    626      1.1  hamajima 		case 5:
    627      1.1  hamajima 			atiming = 200; htiming = 20; ptiming = 30;
    628      1.1  hamajima 			break;
    629      1.1  hamajima 		default:
    630      1.1  hamajima 			return;
    631      1.1  hamajima 		}
    632      1.1  hamajima 		break;
    633      1.1  hamajima #endif
    634      1.1  hamajima 	case ATTRIBUTE:
    635      1.1  hamajima 		switch (ph->ph_vcc) {
    636      1.1  hamajima 		case 3:
    637      1.1  hamajima #if linux_timing!=hamajima20050816
    638      1.1  hamajima 			atiming = 465; htiming = 35; ptiming = 100;
    639      1.1  hamajima #else
    640      1.1  hamajima 			atiming = 600; htiming = 35; ptiming = 100;
    641      1.1  hamajima #endif
    642      1.1  hamajima 			break;
    643      1.1  hamajima 		case 5:
    644      1.1  hamajima #if linux_timing!=hamajima20050816
    645      1.1  hamajima 			atiming = 250; htiming = 20; ptiming = 30;
    646      1.1  hamajima #else
    647      1.1  hamajima 			atiming = 300; htiming = 20; ptiming = 30;
    648      1.1  hamajima #endif
    649      1.1  hamajima 			break;
    650      1.1  hamajima 		default:
    651      1.1  hamajima 			return;
    652      1.1  hamajima 		}
    653      1.1  hamajima 		break;
    654      1.1  hamajima 	default:
    655      1.1  hamajima 		return;
    656      1.1  hamajima 	}
    657      1.1  hamajima 
    658      1.1  hamajima #if linux_timing!=hamajima20050816
    659      1.1  hamajima 	period = 1000000000 / 50000000;
    660      1.1  hamajima 	width = EP93XX_PCMCIA_WIDTH_16;
    661      1.1  hamajima #endif
    662      1.1  hamajima 
    663      1.1  hamajima 	atiming = (atiming + EXTRA_DELAY) / period;
    664      1.1  hamajima 	if (atiming>0xff)
    665      1.1  hamajima 		atiming = 0xff;
    666      1.1  hamajima 	htiming = ((htiming + EXTRA_DELAY) / period) + 1;
    667      1.1  hamajima 	if (htiming>0xf)
    668      1.1  hamajima 		htiming = 0xf;
    669      1.1  hamajima 	ptiming = (ptiming + EXTRA_DELAY) / period;
    670      1.1  hamajima 	if (ptiming>0xff)
    671      1.1  hamajima 		ptiming = 0xff;
    672      1.1  hamajima 
    673      1.1  hamajima 	DPRINTFN(1, ("eppcic_set_pcreg: width=%d, access=%d, hold=%d, pre-charge=%d\n",ph->ph_width,atiming,htiming,ptiming));
    674      1.1  hamajima 
    675      1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ph->ph_space[kind].reg,
    676      1.1  hamajima 			  width
    677      1.1  hamajima 			  | (atiming<<EP93XX_PCMCIA_ACCESS_SHIFT)
    678      1.1  hamajima 			  | (htiming<<EP93XX_PCMCIA_HOLD_SHIFT)
    679      1.1  hamajima 			  | (ptiming<<EP93XX_PCMCIA_PRECHARGE_SHIFT));
    680      1.4  hamajima 	tsleep(ph->ph_space, PWAIT, "eppcic_set_pcreg", hz / 4);
    681      1.1  hamajima }
    682