Home | History | Annotate | Line # | Download | only in g42xxeb
gb225_pcic.c revision 1.14.8.1
      1       1.1      bsh /*
      2       1.1      bsh  * Copyright (c) 2002, 2003, 2005  Genetec corp.  All rights reserved.
      3       1.1      bsh  *
      4       1.1      bsh  * PCMCIA/CF support for TWINTAIL (G4255EB)
      5       1.1      bsh  * Written by Hiroyuki Bessho for Genetec corp.
      6       1.1      bsh  *
      7       1.1      bsh  * Redistribution and use in source and binary forms, with or without
      8       1.1      bsh  * modification, are permitted provided that the following conditions
      9       1.1      bsh  * are met:
     10       1.1      bsh  * 1. Redistributions of source code must retain the above copyright
     11       1.1      bsh  *    notice, this list of conditions and the following disclaimer.
     12       1.1      bsh  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      bsh  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      bsh  *    documentation and/or other materials provided with the distribution.
     15       1.1      bsh  * 3. The name of Genetec corp. may not be used to endorse
     16       1.1      bsh  *    or promote products derived from this software without specific prior
     17       1.1      bsh  *    written permission.
     18       1.1      bsh  *
     19       1.1      bsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
     20       1.1      bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1      bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1      bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORP.
     23       1.1      bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1      bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1      bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1      bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1      bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1      bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1      bsh  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      bsh  */
     31       1.1      bsh 
     32       1.1      bsh #include <sys/types.h>
     33       1.1      bsh #include <sys/param.h>
     34       1.1      bsh #include <sys/systm.h>
     35       1.1      bsh #include <sys/device.h>
     36       1.1      bsh #include <sys/callout.h>
     37       1.1      bsh #include <sys/kernel.h>
     38       1.1      bsh #include <sys/kthread.h>
     39       1.1      bsh #include <sys/malloc.h>
     40       1.1      bsh #include <uvm/uvm.h>
     41       1.1      bsh 
     42      1.11   dyoung #include <sys/bus.h>
     43       1.1      bsh #include <machine/intr.h>
     44       1.1      bsh 
     45       1.1      bsh #include <dev/pcmcia/pcmciareg.h>
     46       1.1      bsh #include <dev/pcmcia/pcmciavar.h>
     47       1.1      bsh #include <dev/pcmcia/pcmciachip.h>
     48       1.1      bsh 
     49       1.1      bsh #include <arch/arm/xscale/pxa2x0var.h>
     50       1.1      bsh #include <arch/arm/xscale/pxa2x0reg.h>
     51       1.1      bsh #include <arch/arm/sa11x0/sa11xx_pcicvar.h>
     52       1.1      bsh #include <arch/evbarm/g42xxeb/g42xxeb_reg.h>
     53       1.1      bsh #include <arch/evbarm/g42xxeb/g42xxeb_var.h>
     54       1.1      bsh #include <arch/evbarm/g42xxeb/gb225reg.h>
     55       1.1      bsh #include <arch/evbarm/g42xxeb/gb225var.h>
     56       1.1      bsh 
     57       1.1      bsh 
     58       1.1      bsh //#define DONT_USE_CARD_DETECT_INTR
     59       1.1      bsh 
     60       1.1      bsh #define PCMCIA_INT	G42XXEB_INT_EXT1
     61       1.1      bsh #define CF_INT     	G42XXEB_INT_EXT0
     62       1.1      bsh 
     63       1.1      bsh #ifdef DEBUG
     64       1.1      bsh #define DPRINTF(arg)	printf arg
     65       1.1      bsh #else
     66       1.1      bsh #define DPRINTF(arg)
     67       1.1      bsh #endif
     68       1.1      bsh 
     69       1.1      bsh struct opcic_softc;
     70       1.1      bsh 
     71       1.1      bsh struct opcic_socket {
     72       1.1      bsh 	struct sapcic_socket ss;	/* inherit socket for sa11x0 pcic */
     73       1.1      bsh 
     74       1.1      bsh #if 0
     75       1.1      bsh 	int voltage;			/* card power voltage selected by
     76       1.1      bsh 					   upper layer */
     77       1.1      bsh #endif
     78       1.1      bsh };
     79       1.1      bsh 
     80       1.1      bsh struct opcic_softc {
     81       1.1      bsh 	struct sapcic_softc sc_pc;	/* inherit SA11xx pcic */
     82       1.1      bsh 
     83       1.1      bsh 	struct opcic_socket sc_socket[2];
     84       1.1      bsh 	int sc_cards;
     85       1.1      bsh 	bus_space_handle_t sc_memctl_ioh;
     86       1.1      bsh };
     87       1.1      bsh 
     88      1.13      chs static int  	opcic_match(device_t, cfdata_t, void *);
     89      1.13      chs static void  	opcic_attach(device_t, device_t, void *);
     90       1.1      bsh static int 	opcic_print(void *, const char *);
     91       1.1      bsh 
     92       1.1      bsh static	int	opcic_read(struct sapcic_socket *, int);
     93       1.1      bsh static	void	opcic_write(struct sapcic_socket *, int, int);
     94       1.1      bsh static	void	opcic_set_power(struct sapcic_socket *, int);
     95       1.1      bsh static	void	opcic_clear_intr(int);
     96       1.1      bsh static	void	*opcic_intr_establish(struct sapcic_socket *, int,
     97       1.1      bsh 				       int (*)(void *), void *);
     98       1.1      bsh static	void	opcic_intr_disestablish(struct sapcic_socket *, void *);
     99       1.1      bsh #ifndef DONT_USE_CARD_DETECT_INTR
    100       1.1      bsh static	int	opcic_card_detect(void *, int);
    101       1.1      bsh #endif
    102       1.1      bsh 
    103      1.13      chs CFATTACH_DECL_NEW(opcic, sizeof(struct opcic_softc),
    104       1.1      bsh     opcic_match, opcic_attach, NULL, NULL);
    105       1.1      bsh 
    106       1.1      bsh static struct sapcic_tag opcic_tag = {
    107       1.1      bsh 	opcic_read,
    108       1.1      bsh 	opcic_write,
    109       1.1      bsh 	opcic_set_power,
    110       1.1      bsh 	opcic_clear_intr,
    111       1.1      bsh 	opcic_intr_establish,
    112       1.1      bsh 	opcic_intr_disestablish,
    113       1.1      bsh };
    114       1.1      bsh 
    115       1.1      bsh 
    116       1.1      bsh #define HAVE_CARD(r)	(((r)&CARDDET_DET)==0)
    117       1.1      bsh 
    118       1.3    perry static inline uint8_t
    119       1.1      bsh opcic_read_card_status(struct opcic_socket *so)
    120       1.1      bsh {
    121       1.1      bsh 	struct opcic_softc *sc = (struct opcic_softc *)(so->ss.sc);
    122       1.1      bsh 	struct opio_softc *osc =
    123       1.8       he 	    device_private(device_parent(sc->sc_pc.sc_dev));
    124       1.1      bsh 
    125       1.1      bsh 	return bus_space_read_1(osc->sc_iot, osc->sc_ioh,
    126       1.1      bsh 	    GB225_CFDET + 2 * so->ss.socket);
    127       1.1      bsh 
    128       1.1      bsh }
    129       1.1      bsh 
    130       1.1      bsh static int
    131      1.13      chs opcic_match(device_t parent, cfdata_t cf, void *aux)
    132       1.1      bsh {
    133       1.1      bsh 	return	1;
    134       1.1      bsh }
    135       1.1      bsh 
    136       1.1      bsh static void
    137      1.13      chs opcic_attach(device_t parent, device_t self, void *aux)
    138       1.1      bsh {
    139       1.1      bsh 	int i;
    140       1.1      bsh 	struct pcmciabus_attach_args paa;
    141      1.13      chs 	struct opcic_softc *sc = device_private(self);
    142      1.13      chs 	struct opio_softc *psc = device_private(parent);
    143      1.13      chs 	struct obio_softc *bsd = device_private(device_parent(parent));
    144       1.5       he 	bus_space_handle_t memctl_ioh = bsd->sc_memctl_ioh;
    145       1.1      bsh 	bus_space_tag_t iot =  psc->sc_iot;
    146       1.1      bsh 
    147       1.1      bsh 	printf("\n");
    148       1.1      bsh 
    149       1.1      bsh 	/* tell PXA2X0 that we have two sockets */
    150       1.1      bsh #if 0
    151       1.1      bsh 	bus_space_write_4(iot, memctl_ioh, MEMCTL_MECR, MECR_NOS);
    152       1.1      bsh #else
    153       1.1      bsh 	bus_space_write_4(iot, memctl_ioh, MEMCTL_MECR, MECR_CIT|MECR_NOS);
    154       1.1      bsh #endif
    155       1.1      bsh 	sc->sc_pc.sc_iot = psc->sc_iot;
    156       1.1      bsh 	sc->sc_memctl_ioh = memctl_ioh;
    157       1.1      bsh 
    158       1.1      bsh 	sc->sc_cards = 0;
    159       1.1      bsh 
    160       1.1      bsh 	for(i = 0; i < 2; i++) {
    161       1.1      bsh 		sc->sc_socket[i].ss.sc = &sc->sc_pc;
    162       1.1      bsh 		sc->sc_socket[i].ss.socket = i;
    163       1.1      bsh 		sc->sc_socket[i].ss.pcictag_cookie = NULL;
    164       1.1      bsh 		sc->sc_socket[i].ss.pcictag = &opcic_tag;
    165       1.1      bsh 		sc->sc_socket[i].ss.event_thread = NULL;
    166       1.1      bsh 		sc->sc_socket[i].ss.event = 0;
    167       1.1      bsh 		sc->sc_socket[i].ss.laststatus = CARDDET_NOCARD;
    168       1.1      bsh 		sc->sc_socket[i].ss.shutdown = 0;
    169       1.1      bsh 
    170       1.1      bsh 		sc->sc_socket[i].ss.power_capability =
    171       1.1      bsh 		    (SAPCIC_POWER_5V|SAPCIC_POWER_3V);
    172       1.1      bsh 
    173       1.1      bsh 		bus_space_write_4(iot, memctl_ioh, MEMCTL_MCIO(i),
    174       1.1      bsh 		    MC_TIMING_VAL(1,1,1));
    175       1.1      bsh #if 0
    176       1.1      bsh 		bus_space_write_4(iot, memctl_ioh, MEMCTL_MCATT(i),
    177       1.1      bsh 		    MC_TIMING_VAL(31,31,31));
    178       1.1      bsh #endif
    179       1.1      bsh 
    180       1.1      bsh 		paa.paa_busname = "pcmcia";
    181       1.1      bsh 		paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
    182       1.1      bsh 		paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i].ss;
    183       1.1      bsh 
    184       1.1      bsh 		sc->sc_socket[i].ss.pcmcia =
    185      1.14  thorpej 		    config_found(sc->sc_pc.sc_dev, &paa, opcic_print,
    186  1.14.8.1  thorpej 				 CFARGS_NONE);
    187       1.1      bsh 
    188       1.1      bsh #ifndef DONT_USE_CARD_DETECT_INTR
    189       1.1      bsh 		/* interrupt for card insertion/removal */
    190       1.1      bsh 		opio_intr_establish(psc,
    191       1.1      bsh 		    i==0 ? OPIO_INTR_CF_INSERT : OPIO_INTR_PCMCIA_INSERT,
    192       1.1      bsh 		    IPL_BIO, opcic_card_detect, &sc->sc_socket[i]);
    193       1.1      bsh #else
    194       1.1      bsh 		bus_space_write_4(iot, ioh, MEMCTL_MECR, MECR_NOS | MECR_CIT);
    195       1.1      bsh 
    196       1.1      bsh #endif
    197       1.1      bsh 
    198       1.1      bsh 		/* schedule kthread creation */
    199       1.6       ad 		sapcic_kthread_create(&sc->sc_socket[i].ss);
    200       1.1      bsh 	}
    201       1.1      bsh 
    202       1.1      bsh }
    203       1.1      bsh 
    204       1.1      bsh static int
    205       1.1      bsh opcic_print(void *aux, const char *name)
    206       1.1      bsh {
    207       1.1      bsh 	return (UNCONF);
    208       1.1      bsh }
    209       1.1      bsh 
    210       1.1      bsh #ifndef DONT_USE_CARD_DETECT_INTR
    211       1.1      bsh static int
    212       1.1      bsh opcic_card_detect(void *arg, int val)
    213       1.1      bsh {
    214       1.1      bsh 	struct opcic_socket *socket = arg;
    215       1.1      bsh 	struct opcic_softc *sc = (struct opcic_softc *)socket->ss.sc;
    216       1.1      bsh 	bus_space_tag_t iot = sc->sc_pc.sc_iot;
    217       1.1      bsh 	bus_space_handle_t memctl_ioh = sc->sc_memctl_ioh;
    218       1.1      bsh 	int sock_no = socket->ss.socket;
    219       1.1      bsh 	int last, s;
    220       1.1      bsh 
    221       1.1      bsh 	s = splbio();
    222       1.1      bsh 	last = sc->sc_cards;
    223       1.1      bsh 	if (HAVE_CARD(val)) {
    224       1.1      bsh 		sc->sc_cards |= 1<<sock_no;
    225       1.1      bsh 		/* if it is the first card, turn on expansion memory
    226       1.1      bsh 		 * control. */
    227       1.1      bsh 		if (last == 0)
    228       1.1      bsh 			bus_space_write_4(iot, memctl_ioh, MEMCTL_MECR,
    229       1.1      bsh 			    MECR_NOS | MECR_CIT);
    230       1.1      bsh 	}
    231       1.1      bsh 	else {
    232       1.1      bsh 		sc->sc_cards &= ~(1<<sock_no);
    233       1.1      bsh 		/* if we loast all cards, turn off expansion memory
    234       1.1      bsh 		 * control. */
    235       1.1      bsh #if 0
    236       1.1      bsh 		if (sc->sc_cards == 0)
    237       1.1      bsh 			bus_space_write_4(iot, memctl_ioh,
    238       1.1      bsh 			    MEMCTL_MECR, MECR_NOS);
    239       1.1      bsh #endif
    240       1.1      bsh 	}
    241       1.1      bsh 	splx(s);
    242       1.1      bsh 
    243      1.10      bsh 	DPRINTF(("%s: card %d %s\n", device_xname(sc->sc_pc.sc_dev), sock_no,
    244       1.1      bsh 	    HAVE_CARD(val) ? "inserted" : "removed"));
    245       1.1      bsh 
    246       1.1      bsh 	sapcic_intr(arg);
    247       1.1      bsh 
    248       1.1      bsh 	return 1;
    249       1.1      bsh }
    250       1.1      bsh #endif /* DONT_USE_CARD_DETECT_INTR */
    251       1.1      bsh 
    252       1.1      bsh static int
    253       1.1      bsh opcic_read(struct sapcic_socket *__so, int which)
    254       1.1      bsh {
    255       1.1      bsh 	struct opcic_socket *so = (struct opcic_socket *)__so;
    256       1.1      bsh 	uint8_t reg;
    257       1.1      bsh 
    258       1.1      bsh 	reg = opcic_read_card_status(so);
    259       1.1      bsh 
    260       1.1      bsh 	switch (which) {
    261       1.1      bsh 	case SAPCIC_STATUS_CARD:
    262       1.1      bsh 		return HAVE_CARD(reg) ?
    263       1.1      bsh 		    SAPCIC_CARD_VALID : SAPCIC_CARD_INVALID;
    264       1.1      bsh 
    265       1.1      bsh 	case SAPCIC_STATUS_VS1:
    266       1.1      bsh 		return (reg & CARDDET_NVS1) == 0;
    267       1.1      bsh 
    268       1.1      bsh 	case SAPCIC_STATUS_VS2:
    269       1.1      bsh 		return (reg & CARDDET_NVS2) == 0;
    270       1.1      bsh 
    271       1.1      bsh 	case SAPCIC_STATUS_READY:
    272       1.1      bsh 		return 1;
    273       1.1      bsh 
    274       1.1      bsh 	default:
    275       1.7    perry 		panic("%s: bogus register", __func__);
    276       1.1      bsh 	}
    277       1.1      bsh }
    278       1.1      bsh 
    279       1.1      bsh static void
    280       1.1      bsh opcic_write(struct sapcic_socket *__so, int which, int arg)
    281       1.1      bsh {
    282       1.1      bsh 	struct opcic_socket *so = (struct opcic_socket *)__so;
    283       1.1      bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->ss.sc;
    284       1.1      bsh 	struct opio_softc *psc =
    285       1.8       he 	     device_private(device_parent(sc->sc_pc.sc_dev));
    286       1.1      bsh 	struct obio_softc *bsc =
    287       1.9      bsh 	     device_private(device_parent(psc->sc_dev));
    288       1.1      bsh 
    289       1.1      bsh 	switch (which) {
    290       1.1      bsh 	case SAPCIC_CONTROL_RESET:
    291       1.1      bsh 		obio_peripheral_reset(bsc, so->ss.socket, arg);
    292       1.1      bsh 		delay(500*1000);
    293       1.1      bsh 		break;
    294       1.1      bsh 
    295       1.1      bsh 	case SAPCIC_CONTROL_LINEENABLE:
    296       1.1      bsh 		break;
    297       1.1      bsh 
    298       1.1      bsh 	case SAPCIC_CONTROL_WAITENABLE:
    299       1.1      bsh 		break;
    300       1.1      bsh 
    301       1.1      bsh 	case SAPCIC_CONTROL_POWERSELECT:
    302       1.1      bsh #if 0
    303       1.1      bsh 		so->voltage = arg;
    304       1.1      bsh #endif
    305       1.1      bsh 		break;
    306       1.1      bsh 
    307       1.1      bsh 	default:
    308       1.7    perry 		panic("%s: bogus register", __func__);
    309       1.1      bsh 	}
    310       1.1      bsh }
    311       1.1      bsh 
    312       1.1      bsh static void
    313       1.1      bsh opcic_set_power(struct sapcic_socket *__so, int arg)
    314       1.1      bsh {
    315       1.1      bsh 	struct opcic_socket *so = (struct opcic_socket *)__so;
    316       1.1      bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->ss.sc;
    317       1.1      bsh 	struct opio_softc *psc =
    318       1.8       he 	    device_private(device_parent(sc->sc_pc.sc_dev));
    319       1.1      bsh  	int shift, save;
    320       1.1      bsh 	volatile uint8_t *p;
    321       1.1      bsh 
    322       1.1      bsh 	if( arg < 0 || SAPCIC_POWER_5V < arg )
    323       1.1      bsh 		panic("sacpcic_set_power: bogus arg\n");
    324       1.1      bsh 
    325       1.1      bsh 	DPRINTF(("card %d: DET=%x\n",
    326       1.1      bsh 	    so->ss.socket,
    327       1.1      bsh 	    bus_space_read_1(psc->sc_iot, psc->sc_ioh,
    328       1.1      bsh 		GB225_CFDET + 2*so->ss.socket)));
    329       1.1      bsh 
    330       1.1      bsh 	p = (volatile uint8_t *)bus_space_vaddr(psc->sc_iot, psc->sc_ioh)
    331       1.1      bsh 	    + GB225_CARDPOW;
    332       1.1      bsh 
    333       1.1      bsh 	shift = 4 * !so->ss.socket;
    334       1.1      bsh 
    335       1.1      bsh 	save = disable_interrupts(I32_bit);
    336       1.1      bsh 	*p = (*p & ~(0x0f << shift)) | ((CARDPOW_VPPVCC | (arg<<2)) << shift);
    337       1.1      bsh 	restore_interrupts(save);
    338       1.1      bsh 
    339       1.1      bsh 	DPRINTF(("card %d power: %x\n", so->ss.socket, *p));
    340       1.1      bsh }
    341       1.1      bsh 
    342       1.1      bsh static void
    343       1.1      bsh opcic_clear_intr(int arg)
    344       1.1      bsh {
    345       1.1      bsh }
    346       1.1      bsh 
    347       1.1      bsh static void *
    348       1.1      bsh opcic_intr_establish(struct sapcic_socket *so, int level,
    349       1.1      bsh     int (* ih_fun)(void *), void *ih_arg)
    350       1.1      bsh {
    351       1.1      bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->sc;
    352       1.1      bsh 	struct opio_softc *psc =
    353       1.8       he 	    device_private(device_parent(sc->sc_pc.sc_dev));
    354       1.1      bsh 	struct obio_softc *bsc =
    355       1.9      bsh 	    device_private(device_parent(psc->sc_dev));
    356       1.1      bsh 	int irq;
    357       1.1      bsh 
    358       1.1      bsh 	DPRINTF(("opcic_intr_establish %d\n", so->socket));
    359       1.1      bsh 
    360       1.1      bsh 	irq = so->socket ? PCMCIA_INT : CF_INT;
    361       1.1      bsh 	return obio_intr_establish(bsc, irq, level, IST_EDGE_FALLING,
    362       1.1      bsh 	    ih_fun, ih_arg);
    363       1.1      bsh }
    364       1.1      bsh 
    365       1.1      bsh static void
    366       1.1      bsh opcic_intr_disestablish(struct sapcic_socket *so, void *ih)
    367       1.1      bsh {
    368       1.1      bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->sc;
    369       1.1      bsh 	struct opio_softc *psc =
    370       1.8       he 	    device_private(device_parent(sc->sc_pc.sc_dev));
    371       1.1      bsh 	struct obio_softc *bsc =
    372      1.13      chs 	    device_private(device_parent(psc->sc_dev));
    373       1.1      bsh 	int (* func)(void *) = ((struct obio_handler *)ih)->func;
    374       1.1      bsh 
    375       1.1      bsh 	int irq = so->socket ? PCMCIA_INT : CF_INT;
    376       1.1      bsh 
    377       1.1      bsh 	obio_intr_disestablish(bsc, irq, func);
    378       1.1      bsh }
    379