Home | History | Annotate | Line # | Download | only in at91
at91cf.c revision 1.1.2.1
      1  1.1.2.1  matt /*	$Id: at91cf.c,v 1.1.2.1 2007/11/10 02:56:29 matt Exp $	*/
      2  1.1.2.1  matt /*	$NetBSD: at91cf.c,v 1.1.2.1 2007/11/10 02:56:29 matt Exp $	*/
      3  1.1.2.1  matt 
      4  1.1.2.1  matt /*
      5  1.1.2.1  matt  * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
      6  1.1.2.1  matt  *
      7  1.1.2.1  matt  * Based on arch/evbarm/ep93xx/eppcic.c,
      8  1.1.2.1  matt  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
      9  1.1.2.1  matt  *
     10  1.1.2.1  matt  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1  matt  * modification, are permitted provided that the following conditions
     12  1.1.2.1  matt  * are met:
     13  1.1.2.1  matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1  matt  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1  matt  *
     19  1.1.2.1  matt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20  1.1.2.1  matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1.2.1  matt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1.2.1  matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  1.1.2.1  matt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1.2.1  matt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1.2.1  matt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1.2.1  matt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1.2.1  matt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1.2.1  matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1.2.1  matt  * SUCH DAMAGE.
     30  1.1.2.1  matt  */
     31  1.1.2.1  matt 
     32  1.1.2.1  matt #include <sys/cdefs.h>
     33  1.1.2.1  matt __KERNEL_RCSID(0, "$NetBSD: at91cf.c,v 1.1.2.1 2007/11/10 02:56:29 matt Exp $");
     34  1.1.2.1  matt 
     35  1.1.2.1  matt #include <sys/param.h>
     36  1.1.2.1  matt #include <sys/systm.h>
     37  1.1.2.1  matt #include <sys/kernel.h>
     38  1.1.2.1  matt #include <sys/malloc.h>
     39  1.1.2.1  matt #include <sys/device.h>
     40  1.1.2.1  matt #include <sys/kthread.h>
     41  1.1.2.1  matt #include <uvm/uvm_param.h>
     42  1.1.2.1  matt #include <machine/bus.h>
     43  1.1.2.1  matt #include <dev/pcmcia/pcmciareg.h>
     44  1.1.2.1  matt #include <dev/pcmcia/pcmciavar.h>
     45  1.1.2.1  matt #include <dev/pcmcia/pcmciachip.h>
     46  1.1.2.1  matt #include <arm/at91/at91reg.h>
     47  1.1.2.1  matt #include <arm/at91/at91var.h>
     48  1.1.2.1  matt #include <arm/at91/at91cfvar.h>
     49  1.1.2.1  matt 
     50  1.1.2.1  matt #include "at91pio.h"
     51  1.1.2.1  matt #if NAT91PIO == 0
     52  1.1.2.1  matt #error "at91cf requires at91pio"
     53  1.1.2.1  matt #endif
     54  1.1.2.1  matt 
     55  1.1.2.1  matt #ifdef AT91CF_DEBUG
     56  1.1.2.1  matt int at91cf_debug = AT91CF_DEBUG;
     57  1.1.2.1  matt #define DPRINTFN(n,x)	if (at91cf_debug>(n)) printf x;
     58  1.1.2.1  matt #else
     59  1.1.2.1  matt #define DPRINTFN(n,x)
     60  1.1.2.1  matt #endif
     61  1.1.2.1  matt 
     62  1.1.2.1  matt struct at91cf_handle {
     63  1.1.2.1  matt 	struct at91cf_softc	*ph_sc;
     64  1.1.2.1  matt 	struct device		*ph_card;
     65  1.1.2.1  matt 	int			(*ph_ih_func)(void *);
     66  1.1.2.1  matt 	void			*ph_ih_arg;
     67  1.1.2.1  matt 	struct proc		*ph_event_thread;
     68  1.1.2.1  matt 	int			ph_type;	/* current access type */
     69  1.1.2.1  matt 	int			ph_run;		/* kthread running */
     70  1.1.2.1  matt 	int			ph_width;	/* 8 or 16 */
     71  1.1.2.1  matt 	int			ph_vcc;		/* 3 or 5 */
     72  1.1.2.1  matt 	int			ph_status;	/* combined cd1 and cd2 */
     73  1.1.2.1  matt 	struct {
     74  1.1.2.1  matt 		bus_size_t	reg;
     75  1.1.2.1  matt 		bus_addr_t	base;
     76  1.1.2.1  matt 		bus_size_t	size;
     77  1.1.2.1  matt 	} ph_space[3];
     78  1.1.2.1  matt #define	IO		0
     79  1.1.2.1  matt #define	COMMON		1
     80  1.1.2.1  matt #define	ATTRIBUTE	2
     81  1.1.2.1  matt };
     82  1.1.2.1  matt 
     83  1.1.2.1  matt static int at91cf_intr_carddetect(void *);
     84  1.1.2.1  matt static int at91cf_intr_socket(void *);
     85  1.1.2.1  matt static int at91cf_print(void *, const char *);
     86  1.1.2.1  matt static void at91cf_create_event_thread(void *);
     87  1.1.2.1  matt static void at91cf_event_thread(void *);
     88  1.1.2.1  matt void at91cf_shutdown(void *);
     89  1.1.2.1  matt 
     90  1.1.2.1  matt static int at91cf_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
     91  1.1.2.1  matt 			    struct pcmcia_mem_handle *);
     92  1.1.2.1  matt static void at91cf_mem_free(pcmcia_chipset_handle_t,
     93  1.1.2.1  matt 			    struct pcmcia_mem_handle *);
     94  1.1.2.1  matt static int at91cf_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
     95  1.1.2.1  matt 			  struct pcmcia_mem_handle *, bus_size_t *, int *);
     96  1.1.2.1  matt static void at91cf_mem_unmap(pcmcia_chipset_handle_t, int);
     97  1.1.2.1  matt static int at91cf_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
     98  1.1.2.1  matt 			   bus_size_t, struct pcmcia_io_handle *);
     99  1.1.2.1  matt static void at91cf_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
    100  1.1.2.1  matt static int at91cf_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
    101  1.1.2.1  matt 			 struct pcmcia_io_handle *, int *);
    102  1.1.2.1  matt static void at91cf_io_unmap(pcmcia_chipset_handle_t, int);
    103  1.1.2.1  matt static void *at91cf_intr_establish(pcmcia_chipset_handle_t,
    104  1.1.2.1  matt 				   struct pcmcia_function *,
    105  1.1.2.1  matt 				   int, int (*)(void *), void *);
    106  1.1.2.1  matt static void at91cf_intr_disestablish(pcmcia_chipset_handle_t, void *);
    107  1.1.2.1  matt static void at91cf_socket_enable(pcmcia_chipset_handle_t);
    108  1.1.2.1  matt static void at91cf_socket_disable(pcmcia_chipset_handle_t);
    109  1.1.2.1  matt static void at91cf_socket_settype(pcmcia_chipset_handle_t, int);
    110  1.1.2.1  matt 
    111  1.1.2.1  matt static void at91cf_attach_socket(struct at91cf_handle *);
    112  1.1.2.1  matt static void at91cf_config_socket(struct at91cf_handle *);
    113  1.1.2.1  matt //static int at91cf_get_voltage(struct at91cf_handle *);
    114  1.1.2.1  matt 
    115  1.1.2.1  matt static struct pcmcia_chip_functions at91cf_functions = {
    116  1.1.2.1  matt 	at91cf_mem_alloc,	at91cf_mem_free,
    117  1.1.2.1  matt 	at91cf_mem_map,	at91cf_mem_unmap,
    118  1.1.2.1  matt 	at91cf_io_alloc,	at91cf_io_free,
    119  1.1.2.1  matt 	at91cf_io_map,		at91cf_io_unmap,
    120  1.1.2.1  matt 	at91cf_intr_establish,	at91cf_intr_disestablish,
    121  1.1.2.1  matt 	at91cf_socket_enable,	at91cf_socket_disable,
    122  1.1.2.1  matt 	at91cf_socket_settype
    123  1.1.2.1  matt };
    124  1.1.2.1  matt 
    125  1.1.2.1  matt #define	MEMORY_BASE	0
    126  1.1.2.1  matt #define	MEMORY_SIZE	0x1000
    127  1.1.2.1  matt #define	COMMON_BASE	0x400000
    128  1.1.2.1  matt #define	COMMON_SIZE	0x1000
    129  1.1.2.1  matt #define	IO_BASE		0x800000
    130  1.1.2.1  matt #define	IO_SIZE		0x1000
    131  1.1.2.1  matt #define	MIN_SIZE	(IO_BASE + IO_SIZE)
    132  1.1.2.1  matt 
    133  1.1.2.1  matt void
    134  1.1.2.1  matt at91cf_attach_common(struct device *parent, struct device *self, void *aux,
    135  1.1.2.1  matt 		      at91cf_chipset_tag_t cscf)
    136  1.1.2.1  matt {
    137  1.1.2.1  matt 	struct at91cf_softc *sc = (struct at91cf_softc *)self;
    138  1.1.2.1  matt 	struct at91bus_attach_args *sa = aux;
    139  1.1.2.1  matt 	struct at91cf_handle *ph;
    140  1.1.2.1  matt 
    141  1.1.2.1  matt 	if (sa->sa_size < MIN_SIZE) {
    142  1.1.2.1  matt 		printf("%s: it's not possible to map registers\n", self->dv_xname);
    143  1.1.2.1  matt 		return;
    144  1.1.2.1  matt 	}
    145  1.1.2.1  matt 
    146  1.1.2.1  matt 	sc->sc_iot = sa->sa_iot;
    147  1.1.2.1  matt 	sc->sc_cscf = cscf;
    148  1.1.2.1  matt 	sc->sc_enable = 0;
    149  1.1.2.1  matt 
    150  1.1.2.1  matt 	if (bus_space_map(sa->sa_iot, sa->sa_addr + MEMORY_BASE,
    151  1.1.2.1  matt 			  MEMORY_SIZE, 0, &sc->sc_memory_ioh)){
    152  1.1.2.1  matt 		printf("%s: Cannot map memory space\n", self->dv_xname);
    153  1.1.2.1  matt 		return;
    154  1.1.2.1  matt 	}
    155  1.1.2.1  matt 
    156  1.1.2.1  matt 	if (bus_space_map(sa->sa_iot, sa->sa_addr + COMMON_BASE,
    157  1.1.2.1  matt 			  COMMON_SIZE, 0, &sc->sc_common_ioh)){
    158  1.1.2.1  matt 		printf("%s: Cannot map common memory space\n", self->dv_xname);
    159  1.1.2.1  matt 		bus_space_unmap(sa->sa_iot, sc->sc_memory_ioh, MEMORY_SIZE);
    160  1.1.2.1  matt 		return;
    161  1.1.2.1  matt 	}
    162  1.1.2.1  matt 
    163  1.1.2.1  matt 	if (bus_space_map(sa->sa_iot, sa->sa_addr + IO_BASE,
    164  1.1.2.1  matt 			  IO_SIZE, 0, &sc->sc_io_ioh)){
    165  1.1.2.1  matt 		printf("%s: Cannot map I/O space\n", self->dv_xname);
    166  1.1.2.1  matt 		bus_space_unmap(sa->sa_iot, sc->sc_memory_ioh, MEMORY_SIZE);
    167  1.1.2.1  matt 		bus_space_unmap(sa->sa_iot, sc->sc_common_ioh, COMMON_SIZE);
    168  1.1.2.1  matt 		return;
    169  1.1.2.1  matt 	}
    170  1.1.2.1  matt 
    171  1.1.2.1  matt 	printf("\n");
    172  1.1.2.1  matt 
    173  1.1.2.1  matt 	/* socket 0 */
    174  1.1.2.1  matt 	if (!(ph = malloc(sizeof(struct at91cf_handle), M_DEVBUF, M_NOWAIT))) {
    175  1.1.2.1  matt 		printf("%s: Cannot allocate memory\n", self->dv_xname);
    176  1.1.2.1  matt 		// @@@@ unmap? @@@@
    177  1.1.2.1  matt 		return; /* ENOMEM */
    178  1.1.2.1  matt 	}
    179  1.1.2.1  matt 	sc->sc_ph = ph;
    180  1.1.2.1  matt 	ph->ph_sc = sc;
    181  1.1.2.1  matt 	ph->ph_space[IO].base = sa->sa_addr + IO_BASE;
    182  1.1.2.1  matt 	ph->ph_space[IO].size = IO_SIZE;
    183  1.1.2.1  matt 	ph->ph_space[COMMON].base = sa->sa_addr + COMMON_BASE;
    184  1.1.2.1  matt 	ph->ph_space[COMMON].size = COMMON_SIZE;
    185  1.1.2.1  matt 	ph->ph_space[ATTRIBUTE].base = sa->sa_addr + MEMORY_BASE;
    186  1.1.2.1  matt 	ph->ph_space[ATTRIBUTE].size = MEMORY_SIZE;
    187  1.1.2.1  matt 	at91cf_attach_socket(ph);
    188  1.1.2.1  matt 
    189  1.1.2.1  matt 	// @@@ reset CF now? @@@@
    190  1.1.2.1  matt 
    191  1.1.2.1  matt 	at91cf_config_socket(sc->sc_ph);
    192  1.1.2.1  matt }
    193  1.1.2.1  matt 
    194  1.1.2.1  matt static void
    195  1.1.2.1  matt at91cf_attach_socket(struct at91cf_handle *ph)
    196  1.1.2.1  matt {
    197  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    198  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    199  1.1.2.1  matt 	int wait;
    200  1.1.2.1  matt 
    201  1.1.2.1  matt 	ph->ph_width = 16;
    202  1.1.2.1  matt 	ph->ph_vcc = 3;
    203  1.1.2.1  matt 	ph->ph_event_thread = NULL;
    204  1.1.2.1  matt 	ph->ph_run = 0;
    205  1.1.2.1  matt 	ph->ph_ih_func = NULL;
    206  1.1.2.1  matt 	ph->ph_ih_arg = NULL;
    207  1.1.2.1  matt 
    208  1.1.2.1  matt 	ph->ph_status = (*cscf->card_detect)(sc);
    209  1.1.2.1  matt 
    210  1.1.2.1  matt 	wait = (cscf->power_ctl)(sc, POWER_OFF);
    211  1.1.2.1  matt 	delay(wait);
    212  1.1.2.1  matt 	wait = (cscf->power_ctl)(sc, POWER_ON);
    213  1.1.2.1  matt 	delay(wait);
    214  1.1.2.1  matt }
    215  1.1.2.1  matt 
    216  1.1.2.1  matt static void
    217  1.1.2.1  matt at91cf_config_socket(struct at91cf_handle *ph)
    218  1.1.2.1  matt {
    219  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    220  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    221  1.1.2.1  matt 	struct pcmciabus_attach_args paa;
    222  1.1.2.1  matt 	int wait;
    223  1.1.2.1  matt 
    224  1.1.2.1  matt 	paa.paa_busname = "pcmcia";
    225  1.1.2.1  matt 	paa.pct = (pcmcia_chipset_tag_t)&at91cf_functions;
    226  1.1.2.1  matt 	paa.pch = (pcmcia_chipset_handle_t)ph;
    227  1.1.2.1  matt 	paa.iobase = ph->ph_space[IO].base;
    228  1.1.2.1  matt 	paa.iosize = ph->ph_space[IO].size;
    229  1.1.2.1  matt 	ph->ph_card = config_found_ia((void*)sc, "pcmciabus", &paa,
    230  1.1.2.1  matt 				      at91cf_print);
    231  1.1.2.1  matt 
    232  1.1.2.1  matt 	(*cscf->intr_establish)(sc, CD_IRQ, IPL_BIO, at91cf_intr_carddetect, ph);
    233  1.1.2.1  matt 	wait = (*cscf->power_ctl)(sc, POWER_OFF);
    234  1.1.2.1  matt 	delay(wait);
    235  1.1.2.1  matt 
    236  1.1.2.1  matt 	kthread_create(at91cf_create_event_thread, ph);
    237  1.1.2.1  matt }
    238  1.1.2.1  matt 
    239  1.1.2.1  matt static int
    240  1.1.2.1  matt at91cf_print(void *arg, const char *pnp)
    241  1.1.2.1  matt {
    242  1.1.2.1  matt 	return (UNCONF);
    243  1.1.2.1  matt }
    244  1.1.2.1  matt 
    245  1.1.2.1  matt static void
    246  1.1.2.1  matt at91cf_create_event_thread(void *arg)
    247  1.1.2.1  matt {
    248  1.1.2.1  matt 	struct at91cf_handle *ph = arg;
    249  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    250  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    251  1.1.2.1  matt 
    252  1.1.2.1  matt 	ph->ph_status = (*cscf->card_detect)(sc);
    253  1.1.2.1  matt 
    254  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_create_event_thread: status=%d\n", ph->ph_status));
    255  1.1.2.1  matt 
    256  1.1.2.1  matt 	if (ph->ph_status)
    257  1.1.2.1  matt 		pcmcia_card_attach(ph->ph_card);
    258  1.1.2.1  matt 
    259  1.1.2.1  matt 	ph->ph_run = 1;
    260  1.1.2.1  matt 	kthread_create1(at91cf_event_thread, ph, &ph->ph_event_thread,
    261  1.1.2.1  matt 			"%s", sc->sc_dev.dv_xname);
    262  1.1.2.1  matt }
    263  1.1.2.1  matt 
    264  1.1.2.1  matt static void
    265  1.1.2.1  matt at91cf_event_thread(void *arg)
    266  1.1.2.1  matt {
    267  1.1.2.1  matt 	struct at91cf_handle *ph = arg;
    268  1.1.2.1  matt 	int status;
    269  1.1.2.1  matt 
    270  1.1.2.1  matt 	for (;;) {
    271  1.1.2.1  matt 		status = ph->ph_status;
    272  1.1.2.1  matt 		tsleep(ph, PWAIT, "CSC wait", 0);
    273  1.1.2.1  matt 		if (!ph->ph_run)
    274  1.1.2.1  matt 			break;
    275  1.1.2.1  matt 
    276  1.1.2.1  matt 		DPRINTFN(1, ("at91cf_event_thread: old status=%d, new status=%d\n", status, ph->ph_status));
    277  1.1.2.1  matt 
    278  1.1.2.1  matt 		if (!status && ph->ph_status)
    279  1.1.2.1  matt 			pcmcia_card_attach(ph->ph_card);
    280  1.1.2.1  matt 		else if (status && !ph->ph_status)
    281  1.1.2.1  matt 			pcmcia_card_detach(ph->ph_card, DETACH_FORCE);
    282  1.1.2.1  matt 	}
    283  1.1.2.1  matt 
    284  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_event_thread: run=%d\n",ph->ph_run));
    285  1.1.2.1  matt 	ph->ph_event_thread = NULL;
    286  1.1.2.1  matt 	kthread_exit(0);
    287  1.1.2.1  matt }
    288  1.1.2.1  matt 
    289  1.1.2.1  matt void
    290  1.1.2.1  matt at91cf_shutdown(void *arg)
    291  1.1.2.1  matt {
    292  1.1.2.1  matt 	struct at91cf_handle *ph = arg;
    293  1.1.2.1  matt 
    294  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_shutdown\n"));
    295  1.1.2.1  matt 	ph->ph_run = 0;
    296  1.1.2.1  matt 	wakeup(ph);
    297  1.1.2.1  matt }
    298  1.1.2.1  matt 
    299  1.1.2.1  matt static int
    300  1.1.2.1  matt at91cf_intr_carddetect(void *arg)
    301  1.1.2.1  matt {
    302  1.1.2.1  matt 	struct at91cf_handle *ph = arg;
    303  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    304  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    305  1.1.2.1  matt 	int nstatus;
    306  1.1.2.1  matt 
    307  1.1.2.1  matt 	nstatus = (*cscf->card_detect)(sc);
    308  1.1.2.1  matt 
    309  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_intr: nstatus=%#x, ostatus=%#x\n", nstatus, ph->ph_status));
    310  1.1.2.1  matt 
    311  1.1.2.1  matt 	if (nstatus != ph->ph_status) {
    312  1.1.2.1  matt 		ph->ph_status = nstatus;
    313  1.1.2.1  matt 		wakeup(ph);
    314  1.1.2.1  matt 	}
    315  1.1.2.1  matt 
    316  1.1.2.1  matt 	return 0;
    317  1.1.2.1  matt }
    318  1.1.2.1  matt 
    319  1.1.2.1  matt static int
    320  1.1.2.1  matt at91cf_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    321  1.1.2.1  matt 		 struct pcmcia_mem_handle *pmh)
    322  1.1.2.1  matt {
    323  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    324  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    325  1.1.2.1  matt 
    326  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_mem_alloc: size=%#x\n",(unsigned)size));
    327  1.1.2.1  matt 
    328  1.1.2.1  matt 	pmh->memt = sc->sc_iot;
    329  1.1.2.1  matt 	return 0;
    330  1.1.2.1  matt }
    331  1.1.2.1  matt 
    332  1.1.2.1  matt static void
    333  1.1.2.1  matt at91cf_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
    334  1.1.2.1  matt {
    335  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_mem_free\n"));
    336  1.1.2.1  matt }
    337  1.1.2.1  matt 
    338  1.1.2.1  matt static int
    339  1.1.2.1  matt at91cf_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
    340  1.1.2.1  matt 	       bus_size_t size, struct pcmcia_mem_handle *pmh,
    341  1.1.2.1  matt 	       bus_size_t *offsetp, int *windowp)
    342  1.1.2.1  matt {
    343  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    344  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    345  1.1.2.1  matt 	bus_addr_t pa;
    346  1.1.2.1  matt 	int err;
    347  1.1.2.1  matt 
    348  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_mem_map: kind=%d, addr=%#x, size=%#x\n",kind,(unsigned)addr,(unsigned)size));
    349  1.1.2.1  matt 
    350  1.1.2.1  matt 	pa = addr;
    351  1.1.2.1  matt 	*offsetp = 0;
    352  1.1.2.1  matt 	size = round_page(size);
    353  1.1.2.1  matt 	pmh->realsize = size;
    354  1.1.2.1  matt 	if (kind & PCMCIA_WIDTH_MEM8)
    355  1.1.2.1  matt 		ph->ph_width = 8;
    356  1.1.2.1  matt 	else
    357  1.1.2.1  matt 		ph->ph_width = 16;
    358  1.1.2.1  matt 	switch (kind & ~PCMCIA_WIDTH_MEM_MASK) {
    359  1.1.2.1  matt 	case PCMCIA_MEM_ATTR:
    360  1.1.2.1  matt 		pa += ph->ph_space[ATTRIBUTE].base;
    361  1.1.2.1  matt 		break;
    362  1.1.2.1  matt 	case PCMCIA_MEM_COMMON:
    363  1.1.2.1  matt 		pa += ph->ph_space[COMMON].base;
    364  1.1.2.1  matt 		break;
    365  1.1.2.1  matt 	default:
    366  1.1.2.1  matt 		return -1;
    367  1.1.2.1  matt 	}
    368  1.1.2.1  matt 
    369  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_mem_map: pa=%#x, *offsetp=%#x, size=%#x\n",(unsigned)pa,(unsigned)addr,(unsigned)size));
    370  1.1.2.1  matt 
    371  1.1.2.1  matt 	if (!(err = bus_space_map(sc->sc_iot, pa, size, 0, &pmh->memh)))
    372  1.1.2.1  matt 		*windowp = (int)pmh->memh;
    373  1.1.2.1  matt 	return err;
    374  1.1.2.1  matt }
    375  1.1.2.1  matt 
    376  1.1.2.1  matt static void
    377  1.1.2.1  matt at91cf_mem_unmap(pcmcia_chipset_handle_t pch, int window)
    378  1.1.2.1  matt {
    379  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    380  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    381  1.1.2.1  matt 
    382  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_mem_unmap: window=%#x\n",window));
    383  1.1.2.1  matt 
    384  1.1.2.1  matt 	bus_space_unmap(sc->sc_iot, (bus_addr_t)window, 0x1000);
    385  1.1.2.1  matt }
    386  1.1.2.1  matt 
    387  1.1.2.1  matt static int
    388  1.1.2.1  matt at91cf_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
    389  1.1.2.1  matt 		bus_size_t align, struct pcmcia_io_handle *pih)
    390  1.1.2.1  matt {
    391  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    392  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    393  1.1.2.1  matt 	bus_addr_t pa;
    394  1.1.2.1  matt 
    395  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_io_alloc: start=%#x, size=%#x, align=%#x\n",(unsigned)start,(unsigned)size,(unsigned)align));
    396  1.1.2.1  matt 
    397  1.1.2.1  matt 	pih->iot = sc->sc_iot;
    398  1.1.2.1  matt 	pih->addr = start;
    399  1.1.2.1  matt 	pih->size = size;
    400  1.1.2.1  matt 	pa = pih->addr + ph->ph_space[IO].base;
    401  1.1.2.1  matt 	return bus_space_map(sc->sc_iot, pa, size, 0, &pih->ioh);
    402  1.1.2.1  matt }
    403  1.1.2.1  matt 
    404  1.1.2.1  matt static void
    405  1.1.2.1  matt at91cf_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
    406  1.1.2.1  matt {
    407  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    408  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    409  1.1.2.1  matt 
    410  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_io_free\n"));
    411  1.1.2.1  matt 
    412  1.1.2.1  matt 	bus_space_unmap(sc->sc_iot, pih->ioh, pih->size);
    413  1.1.2.1  matt }
    414  1.1.2.1  matt 
    415  1.1.2.1  matt static int
    416  1.1.2.1  matt at91cf_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
    417  1.1.2.1  matt 	      bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
    418  1.1.2.1  matt {
    419  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    420  1.1.2.1  matt 
    421  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_io_map: offset=%#x, size=%#x, width=%d",(unsigned)offset,(unsigned)size,width));
    422  1.1.2.1  matt 
    423  1.1.2.1  matt 	switch (width) {
    424  1.1.2.1  matt 	case PCMCIA_WIDTH_IO8:
    425  1.1.2.1  matt 		DPRINTFN(1, ("(8bit)\n"));
    426  1.1.2.1  matt 		ph->ph_width = 8;
    427  1.1.2.1  matt 		break;
    428  1.1.2.1  matt 	case PCMCIA_WIDTH_IO16:
    429  1.1.2.1  matt 	case PCMCIA_WIDTH_AUTO:	/* I don't understand how I check it */
    430  1.1.2.1  matt 		DPRINTFN(1, ("(16bit)\n"));
    431  1.1.2.1  matt 		ph->ph_width = 16;
    432  1.1.2.1  matt 		break;
    433  1.1.2.1  matt 	default:
    434  1.1.2.1  matt 		DPRINTFN(1, ("(unknown)\n"));
    435  1.1.2.1  matt 		return -1;
    436  1.1.2.1  matt 	}
    437  1.1.2.1  matt 	*windowp = 0; /* unused */
    438  1.1.2.1  matt 	return 0;
    439  1.1.2.1  matt }
    440  1.1.2.1  matt 
    441  1.1.2.1  matt static void
    442  1.1.2.1  matt at91cf_io_unmap(pcmcia_chipset_handle_t pch, int window)
    443  1.1.2.1  matt {
    444  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_io_unmap: window=%#x\n",window));
    445  1.1.2.1  matt }
    446  1.1.2.1  matt 
    447  1.1.2.1  matt static void *
    448  1.1.2.1  matt at91cf_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
    449  1.1.2.1  matt 		      int ipl, int (*ih_func)(void *), void *ih_arg)
    450  1.1.2.1  matt {
    451  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    452  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    453  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    454  1.1.2.1  matt 
    455  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_intr_establish\n"));
    456  1.1.2.1  matt 
    457  1.1.2.1  matt 	if (ph->ph_ih_func)
    458  1.1.2.1  matt 		return 0;
    459  1.1.2.1  matt 
    460  1.1.2.1  matt 	ph->ph_ih_func = ih_func;
    461  1.1.2.1  matt 	ph->ph_ih_arg = ih_arg;
    462  1.1.2.1  matt 
    463  1.1.2.1  matt 	return (*cscf->intr_establish)(sc, CF_IRQ, ipl, at91cf_intr_socket, ph);
    464  1.1.2.1  matt //	return (*cscf->intr_establish)(sc, CF_IRQ, ipl, ih_func, ih_arg);
    465  1.1.2.1  matt }
    466  1.1.2.1  matt 
    467  1.1.2.1  matt static void
    468  1.1.2.1  matt at91cf_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
    469  1.1.2.1  matt {
    470  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    471  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    472  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    473  1.1.2.1  matt 
    474  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_intr_disestablish\n"));
    475  1.1.2.1  matt 
    476  1.1.2.1  matt 	ph->ph_ih_func = NULL;
    477  1.1.2.1  matt 	ph->ph_ih_arg = NULL;
    478  1.1.2.1  matt 
    479  1.1.2.1  matt 	(*cscf->intr_disestablish)(sc, CF_IRQ, ih);
    480  1.1.2.1  matt }
    481  1.1.2.1  matt 
    482  1.1.2.1  matt static int
    483  1.1.2.1  matt at91cf_intr_socket(void *arg)
    484  1.1.2.1  matt {
    485  1.1.2.1  matt 	struct at91cf_handle *ph = arg;
    486  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    487  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    488  1.1.2.1  matt 	int err = 0, irq;
    489  1.1.2.1  matt 
    490  1.1.2.1  matt 	if (ph->ph_ih_func) {
    491  1.1.2.1  matt 		irq = (*cscf->irq_line)(sc);
    492  1.1.2.1  matt 		if (ph->ph_type == PCMCIA_IFTYPE_IO)
    493  1.1.2.1  matt 			irq = !irq;
    494  1.1.2.1  matt 		if (irq)
    495  1.1.2.1  matt 			err = (*ph->ph_ih_func)(ph->ph_ih_arg);
    496  1.1.2.1  matt 		else
    497  1.1.2.1  matt 			DPRINTFN(2,("%s: other edge ignored\n", __FUNCTION__));
    498  1.1.2.1  matt 	}
    499  1.1.2.1  matt 
    500  1.1.2.1  matt 	return err;
    501  1.1.2.1  matt }
    502  1.1.2.1  matt 
    503  1.1.2.1  matt static void
    504  1.1.2.1  matt at91cf_socket_enable(pcmcia_chipset_handle_t pch)
    505  1.1.2.1  matt {
    506  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    507  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    508  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    509  1.1.2.1  matt 	int wait;
    510  1.1.2.1  matt 
    511  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_socket_enable\n"));
    512  1.1.2.1  matt 
    513  1.1.2.1  matt 	wait = (cscf->power_ctl)(sc, POWER_ON);
    514  1.1.2.1  matt 	delay(wait);
    515  1.1.2.1  matt }
    516  1.1.2.1  matt 
    517  1.1.2.1  matt static void
    518  1.1.2.1  matt at91cf_socket_disable(pcmcia_chipset_handle_t pch)
    519  1.1.2.1  matt {
    520  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    521  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    522  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    523  1.1.2.1  matt 	int wait;
    524  1.1.2.1  matt 
    525  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_socket_disable\n"));
    526  1.1.2.1  matt 
    527  1.1.2.1  matt 	wait = (cscf->power_ctl)(sc, POWER_OFF);
    528  1.1.2.1  matt 	delay(wait);
    529  1.1.2.1  matt }
    530  1.1.2.1  matt 
    531  1.1.2.1  matt static void
    532  1.1.2.1  matt at91cf_socket_settype(pcmcia_chipset_handle_t pch, int type)
    533  1.1.2.1  matt {
    534  1.1.2.1  matt 	struct at91cf_handle *ph = (struct at91cf_handle *)pch;
    535  1.1.2.1  matt 
    536  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_socket_settype: type=%d",type));
    537  1.1.2.1  matt 
    538  1.1.2.1  matt 	ph->ph_type = type;
    539  1.1.2.1  matt 
    540  1.1.2.1  matt 	switch (type) {
    541  1.1.2.1  matt 	case PCMCIA_IFTYPE_MEMORY:
    542  1.1.2.1  matt 		DPRINTFN(1, ("(Memory)\n"));
    543  1.1.2.1  matt 		break;
    544  1.1.2.1  matt 	case PCMCIA_IFTYPE_IO:
    545  1.1.2.1  matt 		DPRINTFN(1, ("(I/O)\n"));
    546  1.1.2.1  matt 		break;
    547  1.1.2.1  matt 	default:
    548  1.1.2.1  matt 		DPRINTFN(1, ("(unknown)\n"));
    549  1.1.2.1  matt 		return;
    550  1.1.2.1  matt 	}
    551  1.1.2.1  matt }
    552  1.1.2.1  matt 
    553  1.1.2.1  matt #if 0
    554  1.1.2.1  matt static int
    555  1.1.2.1  matt at91cf_get_voltage(struct at91cf_handle *ph)
    556  1.1.2.1  matt {
    557  1.1.2.1  matt 	struct at91cf_softc *sc = ph->ph_sc;
    558  1.1.2.1  matt 	at91cf_chipset_tag_t cscf = sc->sc_cscf;
    559  1.1.2.1  matt 	int cap, vcc = 0;
    560  1.1.2.1  matt 
    561  1.1.2.1  matt 	cap = (cscf->power_capability)(sc);
    562  1.1.2.1  matt 	if (eppio_read(sc->sc_pio, ph->ph_port, ph->ph_vs[0])) {
    563  1.1.2.1  matt 		if (cap | VCC_5V)
    564  1.1.2.1  matt 			vcc = 5;
    565  1.1.2.1  matt 		else
    566  1.1.2.1  matt 			printf("%s: unsupported Vcc 5 Volts",
    567  1.1.2.1  matt 			       sc->sc_dev.dv_xname);
    568  1.1.2.1  matt 	} else {
    569  1.1.2.1  matt 		if (cap | VCC_3V)
    570  1.1.2.1  matt 			vcc = 3;
    571  1.1.2.1  matt 		else
    572  1.1.2.1  matt 			printf("%s: unsupported Vcc 3.3 Volts",
    573  1.1.2.1  matt 			       sc->sc_dev.dv_xname);
    574  1.1.2.1  matt 	}
    575  1.1.2.1  matt 	DPRINTFN(1, ("at91cf_get_voltage: vs1=%d, vs2=%d (%dV)\n",eppio_read_bit(sc->sc_pio, ph->ph_port, ph->ph_vs[0]),eppio_read_bit(sc->sc_pio, ph->ph_port, ph->ph_vs[1]),vcc));
    576  1.1.2.1  matt 	return vcc;
    577  1.1.2.1  matt }
    578  1.1.2.1  matt 
    579  1.1.2.1  matt #endif
    580