Home | History | Annotate | Line # | Download | only in ep93xx
eppcic.c revision 1.8.10.1
      1  1.8.10.1   thorpej /*	$NetBSD: eppcic.c,v 1.8.10.1 2021/03/23 07:14:43 thorpej 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.8.10.1   thorpej __KERNEL_RCSID(0, "$NetBSD: eppcic.c,v 1.8.10.1 2021/03/23 07:14:43 thorpej 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.7       chs 	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.7       chs 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.7       chs 	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.7       chs 		printf("%s: epgpio requires\n", device_xname(self));
    158       1.1  hamajima 		return;
    159       1.1  hamajima 	}
    160       1.7       chs 	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.7       chs 		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.8       chs 	ph = malloc(sizeof(struct eppcic_handle), M_DEVBUF, M_WAITOK);
    179       1.1  hamajima 	sc->sc_ph[0] = ph;
    180       1.1  hamajima 	ph->ph_sc = sc;
    181       1.1  hamajima 	ph->ph_socket = 0;
    182       1.1  hamajima 	ph->ph_port = PORT_F;
    183       1.1  hamajima 	ph->ph_cd[0] = SOCKET0_MCCD1;
    184       1.1  hamajima 	ph->ph_cd[1] = SOCKET0_MCCD2;
    185       1.1  hamajima 	ph->ph_vs[0] = SOCKET0_VS1;
    186       1.1  hamajima 	ph->ph_vs[1] = SOCKET0_VS2;
    187       1.1  hamajima 	ph->ph_ireq = SOCKET0_IREQ;
    188       1.1  hamajima 	ph->ph_space[IO].reg = EP93XX_PCMCIA0_IO;
    189       1.1  hamajima 	ph->ph_space[IO].base = EP93XX_PCMCIA0_HWBASE + EP93XX_PCMCIA_IO;
    190       1.1  hamajima 	ph->ph_space[IO].size = EP93XX_PCMCIA_IO_SIZE;
    191       1.1  hamajima 	ph->ph_space[COMMON].reg = EP93XX_PCMCIA0_Common;
    192       1.1  hamajima 	ph->ph_space[COMMON].base = EP93XX_PCMCIA0_HWBASE
    193       1.1  hamajima 				    + EP93XX_PCMCIA_COMMON;
    194       1.1  hamajima 	ph->ph_space[COMMON].size = EP93XX_PCMCIA_COMMON_SIZE;
    195       1.1  hamajima 	ph->ph_space[ATTRIBUTE].reg = EP93XX_PCMCIA0_Attribute;
    196       1.1  hamajima 	ph->ph_space[ATTRIBUTE].base = EP93XX_PCMCIA0_HWBASE
    197       1.1  hamajima 				       + EP93XX_PCMCIA_ATTRIBUTE;
    198       1.1  hamajima 	ph->ph_space[ATTRIBUTE].size = EP93XX_PCMCIA_ATTRIBUTE_SIZE;
    199       1.1  hamajima 	eppcic_attach_socket(ph);
    200       1.1  hamajima 
    201       1.1  hamajima 	reg = EP93XX_PCMCIA_WEN | (pcic->socket_type)(sc, 0);
    202       1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_PCMCIA_Ctrl,
    203       1.1  hamajima 			  EP93XX_PCMCIA_RST | reg);
    204       1.1  hamajima 	delay(10);
    205       1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_PCMCIA_Ctrl, reg);
    206       1.1  hamajima 	delay(500);
    207       1.1  hamajima 
    208       1.1  hamajima 	for (i = 0; i < EP93XX_PCMCIA_NSOCKET; i++)
    209       1.1  hamajima 		eppcic_config_socket(sc->sc_ph[i]);
    210       1.1  hamajima #if NEPLED > 0
    211       1.1  hamajima 	epled_green_off();
    212       1.1  hamajima #endif
    213       1.1  hamajima }
    214       1.1  hamajima 
    215       1.1  hamajima static void
    216       1.1  hamajima eppcic_attach_socket(struct eppcic_handle *ph)
    217       1.1  hamajima {
    218       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    219       1.1  hamajima 
    220       1.1  hamajima 	ph->ph_width = 16;
    221       1.1  hamajima 	ph->ph_vcc = 3;
    222       1.1  hamajima 	ph->ph_event_thread = NULL;
    223       1.1  hamajima 	ph->ph_run = 0;
    224       1.1  hamajima 	ph->ph_ih_func = NULL;
    225       1.1  hamajima 	ph->ph_ih_arg = NULL;
    226       1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    227       1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    228       1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_vs[0]);
    229       1.1  hamajima 	epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_vs[1]);
    230       1.1  hamajima 	ph->ph_status[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    231       1.1  hamajima 	ph->ph_status[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    232       1.1  hamajima }
    233       1.1  hamajima 
    234       1.1  hamajima static void
    235       1.1  hamajima eppcic_config_socket(struct eppcic_handle *ph)
    236       1.1  hamajima {
    237       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    238       1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    239       1.1  hamajima 	struct pcmciabus_attach_args paa;
    240       1.1  hamajima 	int wait;
    241       1.1  hamajima 
    242       1.1  hamajima 	paa.paa_busname = "pcmcia";
    243       1.1  hamajima 	paa.pct = (pcmcia_chipset_tag_t)&eppcic_functions;
    244       1.1  hamajima 	paa.pch = (pcmcia_chipset_handle_t)ph;
    245  1.8.10.1   thorpej 	ph->ph_card = config_found((void*)sc, &paa, eppcic_print, CFARG_EOL);
    246       1.1  hamajima 
    247       1.1  hamajima 	epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_cd[0],
    248       1.1  hamajima 			      EDGE_TRIGGER | FALLING_EDGE | DEBOUNCE,
    249       1.1  hamajima 			      IPL_TTY, eppcic_intr_carddetect, ph);
    250       1.1  hamajima 	epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_cd[1],
    251       1.1  hamajima 			      EDGE_TRIGGER | RISING_EDGE | DEBOUNCE,
    252       1.1  hamajima 			      IPL_TTY, eppcic_intr_carddetect, ph);
    253       1.1  hamajima 	wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_OFF);
    254       1.1  hamajima 	delay(wait);
    255       1.1  hamajima 
    256       1.1  hamajima 
    257       1.1  hamajima 	ph->ph_status[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    258       1.1  hamajima 	ph->ph_status[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    259       1.1  hamajima 
    260       1.2        ad 	DPRINTFN(1, ("eppcic_config_socket: cd1=%d, cd2=%d\n",ph->ph_status[0],ph->ph_status[1]));
    261       1.1  hamajima 
    262       1.1  hamajima 	ph->ph_run = 1;
    263       1.2        ad 	kthread_create(PRI_NONE, 0, NULL, eppcic_event_thread, ph,
    264       1.7       chs 	    &ph->ph_event_thread, "%s,%d", device_xname(sc->sc_dev),
    265       1.2        ad 	    ph->ph_socket);
    266       1.1  hamajima }
    267       1.1  hamajima 
    268       1.2        ad static int
    269       1.2        ad eppcic_print(void *arg, const char *pnp)
    270       1.2        ad {
    271       1.2        ad 	return (UNCONF);
    272       1.2        ad }
    273       1.2        ad 
    274       1.1  hamajima static void
    275       1.1  hamajima eppcic_event_thread(void *arg)
    276       1.1  hamajima {
    277       1.1  hamajima 	struct eppcic_handle *ph = arg;
    278       1.1  hamajima 
    279       1.4  hamajima 	if (!(ph->ph_status[0] | ph->ph_status[1]))
    280       1.4  hamajima 		pcmcia_card_attach(ph->ph_card);
    281       1.4  hamajima 
    282       1.1  hamajima 	for (;;) {
    283       1.1  hamajima 		tsleep(ph, PWAIT, "CSC wait", 0);
    284       1.1  hamajima 		if (!ph->ph_run)
    285       1.1  hamajima 			break;
    286       1.1  hamajima 
    287       1.1  hamajima 		DPRINTFN(1, ("eppcic_event_thread: cd1=%d, cd2=%d\n",ph->ph_status[0],ph->ph_status[1]));
    288       1.1  hamajima 
    289       1.1  hamajima 		if (!ph->ph_status[0] && !ph->ph_status[1])
    290       1.1  hamajima 			pcmcia_card_attach(ph->ph_card);
    291       1.1  hamajima 		else if (ph->ph_status[0] && ph->ph_status[1])
    292       1.1  hamajima 			pcmcia_card_detach(ph->ph_card, DETACH_FORCE);
    293       1.1  hamajima 	}
    294       1.1  hamajima 
    295       1.1  hamajima 	DPRINTFN(1, ("eppcic_event_thread: run=%d\n",ph->ph_run));
    296       1.1  hamajima 	ph->ph_event_thread = NULL;
    297       1.1  hamajima 	kthread_exit(0);
    298       1.1  hamajima }
    299       1.1  hamajima 
    300       1.1  hamajima void
    301       1.1  hamajima eppcic_shutdown(void *arg)
    302       1.1  hamajima {
    303       1.1  hamajima 	struct eppcic_handle *ph = arg;
    304       1.1  hamajima 
    305       1.1  hamajima 	DPRINTFN(1, ("eppcic_shutdown\n"));
    306       1.1  hamajima 	ph->ph_run = 0;
    307       1.1  hamajima 	wakeup(ph);
    308       1.1  hamajima }
    309       1.1  hamajima 
    310       1.1  hamajima static int
    311       1.1  hamajima eppcic_intr_carddetect(void *arg)
    312       1.1  hamajima {
    313       1.1  hamajima 	struct eppcic_handle *ph = arg;
    314       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    315       1.1  hamajima 	int nstatus[2];
    316       1.1  hamajima 
    317       1.1  hamajima 	nstatus[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
    318       1.1  hamajima 	nstatus[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
    319       1.1  hamajima 
    320       1.1  hamajima 	DPRINTFN(1, ("eppcic_intr: cd1=%#x, cd2=%#x\n",nstatus[0],nstatus[1]));
    321       1.1  hamajima 
    322       1.4  hamajima 	if (nstatus[0] != ph->ph_status[0] || nstatus[1] != ph->ph_status[1]) {
    323       1.4  hamajima 		ph->ph_status[0] = nstatus[0];
    324       1.4  hamajima 		ph->ph_status[1] = nstatus[1];
    325       1.1  hamajima 		wakeup(ph);
    326       1.4  hamajima 	}
    327       1.1  hamajima 	return 0;
    328       1.1  hamajima }
    329       1.1  hamajima 
    330       1.1  hamajima static int
    331       1.1  hamajima eppcic_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    332       1.1  hamajima 		 struct pcmcia_mem_handle *pmh)
    333       1.1  hamajima {
    334       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    335       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    336       1.1  hamajima 
    337       1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_alloc: size=%#x\n",(unsigned)size));
    338       1.1  hamajima 
    339       1.1  hamajima 	pmh->memt = sc->sc_iot;
    340       1.1  hamajima 	return 0;
    341       1.1  hamajima }
    342       1.1  hamajima 
    343       1.1  hamajima static void
    344       1.1  hamajima eppcic_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
    345       1.1  hamajima {
    346       1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_free\n"));
    347       1.1  hamajima }
    348       1.1  hamajima 
    349       1.1  hamajima static int
    350       1.1  hamajima eppcic_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
    351       1.1  hamajima 	       bus_size_t size, struct pcmcia_mem_handle *pmh,
    352       1.1  hamajima 	       bus_size_t *offsetp, int *windowp)
    353       1.1  hamajima {
    354       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    355       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    356       1.1  hamajima 	bus_addr_t pa;
    357       1.1  hamajima 	int err;
    358       1.1  hamajima 
    359       1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_map: kind=%d, addr=%#x, size=%#x\n",kind,(unsigned)addr,(unsigned)size));
    360       1.1  hamajima 
    361       1.1  hamajima 	pa = addr;
    362       1.1  hamajima 	*offsetp = 0;
    363       1.1  hamajima 	size = round_page(size);
    364       1.1  hamajima 	pmh->realsize = size;
    365       1.1  hamajima 	if (kind & PCMCIA_WIDTH_MEM8)
    366       1.1  hamajima 		ph->ph_width = 8;
    367       1.1  hamajima 	else
    368       1.1  hamajima 		ph->ph_width = 16;
    369       1.1  hamajima 	switch (kind & ~PCMCIA_WIDTH_MEM_MASK) {
    370       1.1  hamajima 	case PCMCIA_MEM_ATTR:
    371       1.4  hamajima 		eppcic_set_pcreg(ph, ATTRIBUTE);
    372       1.1  hamajima 		pa += ph->ph_space[ATTRIBUTE].base;
    373       1.1  hamajima 		break;
    374       1.1  hamajima 	case PCMCIA_MEM_COMMON:
    375       1.4  hamajima 		eppcic_set_pcreg(ph, COMMON);
    376       1.1  hamajima 		pa += ph->ph_space[COMMON].base;
    377       1.1  hamajima 		break;
    378       1.1  hamajima 	default:
    379       1.1  hamajima 		return -1;
    380       1.1  hamajima 	}
    381       1.1  hamajima 
    382       1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_map: pa=%#x, *offsetp=%#x, size=%#x\n",(unsigned)pa,(unsigned)addr,(unsigned)size));
    383       1.1  hamajima 
    384       1.1  hamajima 	if (!(err = bus_space_map(sc->sc_iot, pa, size, 0, &pmh->memh)))
    385       1.1  hamajima 		*windowp = (int)pmh->memh;
    386       1.1  hamajima 	return err;
    387       1.1  hamajima }
    388       1.1  hamajima 
    389       1.1  hamajima static void
    390       1.1  hamajima eppcic_mem_unmap(pcmcia_chipset_handle_t pch, int window)
    391       1.1  hamajima {
    392       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    393       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    394       1.1  hamajima 
    395       1.1  hamajima 	DPRINTFN(1, ("eppcic_mem_unmap: window=%#x\n",window));
    396       1.1  hamajima 
    397       1.1  hamajima 	bus_space_unmap(sc->sc_iot, (bus_addr_t)window, 0x400);
    398       1.1  hamajima }
    399       1.1  hamajima 
    400       1.1  hamajima static int
    401       1.1  hamajima eppcic_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
    402       1.1  hamajima 		bus_size_t align, struct pcmcia_io_handle *pih)
    403       1.1  hamajima {
    404       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    405       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    406       1.1  hamajima 	bus_addr_t pa;
    407       1.1  hamajima 
    408       1.1  hamajima 	DPRINTFN(1, ("eppcic_io_alloc: start=%#x, size=%#x, align=%#x\n",(unsigned)start,(unsigned)size,(unsigned)align));
    409       1.1  hamajima 
    410       1.1  hamajima 	pih->iot = sc->sc_iot;
    411       1.1  hamajima 	pih->addr = start;
    412       1.1  hamajima 	pih->size = size;
    413       1.1  hamajima 	pa = pih->addr + ph->ph_space[IO].base;
    414       1.1  hamajima 	return bus_space_map(sc->sc_iot, pa, size, 0, &pih->ioh);
    415       1.1  hamajima }
    416       1.1  hamajima 
    417       1.1  hamajima static void
    418       1.1  hamajima eppcic_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
    419       1.1  hamajima {
    420       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    421       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    422       1.1  hamajima 
    423       1.1  hamajima 	DPRINTFN(1, ("eppcic_io_free\n"));
    424       1.1  hamajima 
    425       1.1  hamajima 	bus_space_unmap(sc->sc_iot, pih->ioh, pih->size);
    426       1.1  hamajima }
    427       1.1  hamajima 
    428       1.1  hamajima static int
    429       1.1  hamajima eppcic_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
    430       1.1  hamajima 	      bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
    431       1.1  hamajima {
    432       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    433       1.1  hamajima 
    434       1.1  hamajima 	DPRINTFN(1, ("eppcic_io_map: offset=%#x, size=%#x, width=%d",(unsigned)offset,(unsigned)size,width));
    435       1.1  hamajima 
    436       1.1  hamajima 	switch (width) {
    437       1.1  hamajima 	case PCMCIA_WIDTH_IO8:
    438       1.1  hamajima 		DPRINTFN(1, ("(8bit)\n"));
    439       1.1  hamajima 		ph->ph_width = 8;
    440       1.1  hamajima 		break;
    441       1.1  hamajima 	case PCMCIA_WIDTH_IO16:
    442       1.1  hamajima 	case PCMCIA_WIDTH_AUTO:	/* I don't understand how I check it */
    443       1.1  hamajima 		DPRINTFN(1, ("(16bit)\n"));
    444       1.1  hamajima 		ph->ph_width = 16;
    445       1.1  hamajima 		break;
    446       1.1  hamajima 	default:
    447       1.1  hamajima 		DPRINTFN(1, ("(unknown)\n"));
    448       1.1  hamajima 		return -1;
    449       1.1  hamajima 	}
    450       1.4  hamajima 	eppcic_set_pcreg(ph, IO);
    451       1.1  hamajima 	*windowp = 0; /* unused */
    452       1.1  hamajima 	return 0;
    453       1.1  hamajima }
    454       1.1  hamajima 
    455       1.1  hamajima static void
    456       1.1  hamajima eppcic_io_unmap(pcmcia_chipset_handle_t pch, int window)
    457       1.1  hamajima {
    458       1.1  hamajima 	DPRINTFN(1, ("eppcic_io_unmap: window=%#x\n",window));
    459       1.1  hamajima }
    460       1.1  hamajima 
    461       1.1  hamajima static void *
    462       1.1  hamajima eppcic_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
    463       1.1  hamajima 		      int ipl, int (*ih_func)(void *), void *ih_arg)
    464       1.1  hamajima {
    465       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    466       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    467       1.1  hamajima 
    468       1.1  hamajima 	DPRINTFN(1, ("eppcic_intr_establish\n"));
    469       1.1  hamajima 
    470       1.1  hamajima 	if (ph->ph_ih_func)
    471       1.1  hamajima 		return 0;
    472       1.1  hamajima 
    473       1.1  hamajima 	ph->ph_ih_func = ih_func;
    474       1.1  hamajima 	ph->ph_ih_arg = ih_arg;
    475       1.1  hamajima 	return epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_ireq,
    476       1.1  hamajima 				     LEVEL_SENSE | LOW_LEVEL,
    477       1.1  hamajima 				     ipl, eppcic_intr_socket, ph);
    478       1.1  hamajima }
    479       1.1  hamajima 
    480       1.1  hamajima static void
    481       1.1  hamajima eppcic_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
    482       1.1  hamajima {
    483       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    484       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    485       1.1  hamajima 
    486       1.1  hamajima 	DPRINTFN(1, ("eppcic_intr_disestablish\n"));
    487       1.1  hamajima 
    488       1.1  hamajima 	ph->ph_ih_func = NULL;
    489       1.1  hamajima 	ph->ph_ih_arg = NULL;
    490       1.1  hamajima 	epgpio_intr_disestablish(sc->sc_gpio, ph->ph_port, ph->ph_ireq);
    491       1.1  hamajima }
    492       1.1  hamajima 
    493       1.1  hamajima static int
    494       1.1  hamajima eppcic_intr_socket(void *arg)
    495       1.1  hamajima {
    496       1.1  hamajima 	struct eppcic_handle *ph = arg;
    497       1.1  hamajima 	int err = 0;
    498       1.1  hamajima 
    499       1.1  hamajima 	if (ph->ph_ih_func) {
    500       1.1  hamajima #if NEPLED > 0
    501       1.1  hamajima 		epled_red_on();
    502       1.1  hamajima #endif
    503       1.1  hamajima 		err = (*ph->ph_ih_func)(ph->ph_ih_arg);
    504       1.1  hamajima #if NEPLED > 0
    505       1.1  hamajima 		epled_red_off();
    506       1.1  hamajima #endif
    507       1.1  hamajima 	}
    508       1.1  hamajima 	return err;
    509       1.1  hamajima }
    510       1.1  hamajima 
    511       1.1  hamajima 
    512       1.1  hamajima static void
    513       1.1  hamajima eppcic_socket_enable(pcmcia_chipset_handle_t pch)
    514       1.1  hamajima {
    515       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    516       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    517       1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    518       1.1  hamajima 	int wait;
    519       1.1  hamajima 
    520       1.1  hamajima 	DPRINTFN(1, ("eppcic_socket_enable\n"));
    521       1.1  hamajima 
    522       1.1  hamajima 	wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_ON);
    523       1.1  hamajima 	delay(wait);
    524       1.1  hamajima #if NEPLED > 0
    525       1.1  hamajima 	if (!sc->sc_enable++)
    526       1.1  hamajima 		epled_green_on();
    527       1.1  hamajima #endif
    528       1.1  hamajima 	ph->ph_vcc = eppcic_get_voltage(ph);
    529       1.1  hamajima }
    530       1.1  hamajima 
    531       1.1  hamajima static void
    532       1.1  hamajima eppcic_socket_disable(pcmcia_chipset_handle_t pch)
    533       1.1  hamajima {
    534       1.1  hamajima 	struct eppcic_handle *ph = (struct eppcic_handle *)pch;
    535       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    536       1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    537       1.1  hamajima 	int wait;
    538       1.1  hamajima 
    539       1.1  hamajima 	DPRINTFN(1, ("eppcic_socket_disable\n"));
    540       1.1  hamajima 
    541       1.1  hamajima 	wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_OFF);
    542       1.1  hamajima 	delay(wait);
    543       1.1  hamajima #if NEPLED > 0
    544       1.1  hamajima 	if (!--sc->sc_enable)
    545       1.1  hamajima 		epled_green_off();
    546       1.1  hamajima #endif
    547       1.1  hamajima }
    548       1.1  hamajima 
    549       1.1  hamajima static void
    550       1.1  hamajima eppcic_socket_settype(pcmcia_chipset_handle_t pch, int type)
    551       1.1  hamajima {
    552       1.1  hamajima 	DPRINTFN(1, ("eppcic_socket_settype: type=%d",type));
    553       1.1  hamajima 
    554       1.1  hamajima 	switch (type) {
    555       1.1  hamajima 	case PCMCIA_IFTYPE_MEMORY:
    556       1.1  hamajima 		DPRINTFN(1, ("(Memory)\n"));
    557       1.1  hamajima 		break;
    558       1.1  hamajima 	case PCMCIA_IFTYPE_IO:
    559       1.1  hamajima 		DPRINTFN(1, ("(I/O)\n"));
    560       1.1  hamajima 		break;
    561       1.1  hamajima 	default:
    562       1.1  hamajima 		DPRINTFN(1, ("(unknown)\n"));
    563       1.1  hamajima 		return;
    564       1.1  hamajima 	}
    565       1.1  hamajima }
    566       1.1  hamajima 
    567       1.1  hamajima static int
    568       1.1  hamajima eppcic_get_voltage(struct eppcic_handle *ph)
    569       1.1  hamajima {
    570       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    571       1.1  hamajima 	eppcic_chipset_tag_t pcic = sc->sc_pcic;
    572       1.1  hamajima 	int cap, vcc = 0;
    573       1.1  hamajima 
    574       1.1  hamajima 	cap = (pcic->power_capability)(sc, ph->ph_socket);
    575       1.1  hamajima 	if (epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_vs[0])) {
    576       1.1  hamajima 		if (cap | VCC_5V)
    577       1.1  hamajima 			vcc = 5;
    578       1.1  hamajima 		else
    579       1.1  hamajima 			printf("%s: unsupported Vcc 5 Volts",
    580       1.7       chs 			       device_xname(sc->sc_dev));
    581       1.1  hamajima 	} else {
    582       1.1  hamajima 		if (cap | VCC_3V)
    583       1.1  hamajima 			vcc = 3;
    584       1.1  hamajima 		else
    585       1.1  hamajima 			printf("%s: unsupported Vcc 3.3 Volts",
    586       1.7       chs 			       device_xname(sc->sc_dev));
    587       1.1  hamajima 	}
    588       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));
    589       1.1  hamajima 	return vcc;
    590       1.1  hamajima }
    591       1.1  hamajima 
    592       1.1  hamajima #define	EXTRA_DELAY	40
    593       1.1  hamajima 
    594       1.1  hamajima static void
    595       1.1  hamajima eppcic_set_pcreg(struct eppcic_handle *ph, int kind)
    596       1.1  hamajima {
    597       1.1  hamajima 	struct eppcic_softc *sc = ph->ph_sc;
    598       1.1  hamajima 	int atiming, htiming, ptiming;
    599       1.1  hamajima 	int period = 1000000000 / sc->sc_hclk;
    600       1.1  hamajima 	int width;
    601       1.1  hamajima 
    602       1.1  hamajima 	switch (ph->ph_width) {
    603       1.1  hamajima 	case 8:
    604       1.1  hamajima 		width = 0;
    605       1.1  hamajima 		break;
    606       1.1  hamajima 	case 16:
    607       1.1  hamajima 		width = EP93XX_PCMCIA_WIDTH_16;
    608       1.1  hamajima 		break;
    609       1.1  hamajima 	default:
    610       1.1  hamajima 		return;
    611       1.1  hamajima 	}
    612       1.1  hamajima 	switch (kind) {
    613       1.1  hamajima 	case IO:
    614       1.1  hamajima 		atiming = 165; htiming = 20; ptiming = 70;
    615       1.1  hamajima 		break;
    616       1.1  hamajima 	case COMMON:
    617       1.1  hamajima #if linux_timing!=hamajima20050816
    618       1.1  hamajima 		switch (ph->ph_vcc) {
    619       1.1  hamajima 		case 3:
    620       1.1  hamajima 			atiming = 465; htiming = 35; ptiming = 100;
    621       1.1  hamajima 			break;
    622       1.1  hamajima 		case 5:
    623       1.1  hamajima 			atiming = 200; htiming = 20; ptiming = 30;
    624       1.1  hamajima 			break;
    625       1.1  hamajima 		default:
    626       1.1  hamajima 			return;
    627       1.1  hamajima 		}
    628       1.1  hamajima 		break;
    629       1.1  hamajima #endif
    630       1.1  hamajima 	case ATTRIBUTE:
    631       1.1  hamajima 		switch (ph->ph_vcc) {
    632       1.1  hamajima 		case 3:
    633       1.1  hamajima #if linux_timing!=hamajima20050816
    634       1.1  hamajima 			atiming = 465; htiming = 35; ptiming = 100;
    635       1.1  hamajima #else
    636       1.1  hamajima 			atiming = 600; htiming = 35; ptiming = 100;
    637       1.1  hamajima #endif
    638       1.1  hamajima 			break;
    639       1.1  hamajima 		case 5:
    640       1.1  hamajima #if linux_timing!=hamajima20050816
    641       1.1  hamajima 			atiming = 250; htiming = 20; ptiming = 30;
    642       1.1  hamajima #else
    643       1.1  hamajima 			atiming = 300; htiming = 20; ptiming = 30;
    644       1.1  hamajima #endif
    645       1.1  hamajima 			break;
    646       1.1  hamajima 		default:
    647       1.1  hamajima 			return;
    648       1.1  hamajima 		}
    649       1.1  hamajima 		break;
    650       1.1  hamajima 	default:
    651       1.1  hamajima 		return;
    652       1.1  hamajima 	}
    653       1.1  hamajima 
    654       1.1  hamajima #if linux_timing!=hamajima20050816
    655       1.1  hamajima 	period = 1000000000 / 50000000;
    656       1.1  hamajima 	width = EP93XX_PCMCIA_WIDTH_16;
    657       1.1  hamajima #endif
    658       1.1  hamajima 
    659       1.1  hamajima 	atiming = (atiming + EXTRA_DELAY) / period;
    660       1.1  hamajima 	if (atiming>0xff)
    661       1.1  hamajima 		atiming = 0xff;
    662       1.1  hamajima 	htiming = ((htiming + EXTRA_DELAY) / period) + 1;
    663       1.1  hamajima 	if (htiming>0xf)
    664       1.1  hamajima 		htiming = 0xf;
    665       1.1  hamajima 	ptiming = (ptiming + EXTRA_DELAY) / period;
    666       1.1  hamajima 	if (ptiming>0xff)
    667       1.1  hamajima 		ptiming = 0xff;
    668       1.1  hamajima 
    669       1.1  hamajima 	DPRINTFN(1, ("eppcic_set_pcreg: width=%d, access=%d, hold=%d, pre-charge=%d\n",ph->ph_width,atiming,htiming,ptiming));
    670       1.1  hamajima 
    671       1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, ph->ph_space[kind].reg,
    672       1.1  hamajima 			  width
    673       1.1  hamajima 			  | (atiming<<EP93XX_PCMCIA_ACCESS_SHIFT)
    674       1.1  hamajima 			  | (htiming<<EP93XX_PCMCIA_HOLD_SHIFT)
    675       1.1  hamajima 			  | (ptiming<<EP93XX_PCMCIA_PRECHARGE_SHIFT));
    676       1.4  hamajima 	tsleep(ph->ph_space, PWAIT, "eppcic_set_pcreg", hz / 4);
    677       1.1  hamajima }
    678