Home | History | Annotate | Line # | Download | only in g42xxeb
gb225_pcic.c revision 1.2.2.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.1       bsh #include <machine/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.1       bsh static int  	opcic_match(struct device *, struct cfdata *, void *);
     89      1.1       bsh static void  	opcic_attach(struct device *, struct device *, 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.1       bsh CFATTACH_DECL(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.2.2.1      yamt 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.2.2.1      yamt 	    (struct opio_softc *) 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.1       bsh opcic_match(struct device *parent, struct cfdata *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.1       bsh opcic_attach(struct device *parent, struct device *self, void *aux)
    138      1.1       bsh {
    139      1.1       bsh 	int i;
    140      1.1       bsh 	struct pcmciabus_attach_args paa;
    141      1.1       bsh 	struct opcic_softc *sc = (struct opcic_softc *)self;
    142      1.1       bsh 	struct opio_softc *psc = (struct opio_softc *)parent;
    143  1.2.2.1      yamt 	struct obio_softc *bsd = (struct obio_softc *)device_parent(parent);
    144  1.2.2.1      yamt 	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 		paa.iobase = 0;
    184      1.1       bsh 		paa.iosize = 0x4000000;
    185      1.1       bsh 
    186      1.1       bsh 		sc->sc_socket[i].ss.pcmcia =
    187      1.2  drochner 		    (struct device *)config_found_ia(&sc->sc_pc.sc_dev,
    188      1.2  drochner 		    "pcmciabus", &paa, opcic_print);
    189      1.1       bsh 
    190      1.1       bsh #ifndef DONT_USE_CARD_DETECT_INTR
    191      1.1       bsh 		/* interrupt for card insertion/removal */
    192      1.1       bsh 		opio_intr_establish(psc,
    193      1.1       bsh 		    i==0 ? OPIO_INTR_CF_INSERT : OPIO_INTR_PCMCIA_INSERT,
    194      1.1       bsh 		    IPL_BIO, opcic_card_detect, &sc->sc_socket[i]);
    195      1.1       bsh #else
    196      1.1       bsh 		bus_space_write_4(iot, ioh, MEMCTL_MECR, MECR_NOS | MECR_CIT);
    197      1.1       bsh 
    198      1.1       bsh #endif
    199      1.1       bsh 
    200      1.1       bsh 		/* schedule kthread creation */
    201      1.1       bsh 		kthread_create(sapcic_kthread_create, &sc->sc_socket[i].ss);
    202      1.1       bsh 	}
    203      1.1       bsh 
    204      1.1       bsh }
    205      1.1       bsh 
    206      1.1       bsh static int
    207      1.1       bsh opcic_print(void *aux, const char *name)
    208      1.1       bsh {
    209      1.1       bsh 	return (UNCONF);
    210      1.1       bsh }
    211      1.1       bsh 
    212      1.1       bsh #ifndef DONT_USE_CARD_DETECT_INTR
    213      1.1       bsh static int
    214      1.1       bsh opcic_card_detect(void *arg, int val)
    215      1.1       bsh {
    216      1.1       bsh 	struct opcic_socket *socket = arg;
    217      1.1       bsh 	struct opcic_softc *sc = (struct opcic_softc *)socket->ss.sc;
    218      1.1       bsh 	bus_space_tag_t iot = sc->sc_pc.sc_iot;
    219      1.1       bsh 	bus_space_handle_t memctl_ioh = sc->sc_memctl_ioh;
    220      1.1       bsh 	int sock_no = socket->ss.socket;
    221      1.1       bsh 	int last, s;
    222      1.1       bsh 
    223      1.1       bsh 	s = splbio();
    224      1.1       bsh 	last = sc->sc_cards;
    225      1.1       bsh 	if (HAVE_CARD(val)) {
    226      1.1       bsh 		sc->sc_cards |= 1<<sock_no;
    227      1.1       bsh 		/* if it is the first card, turn on expansion memory
    228      1.1       bsh 		 * control. */
    229      1.1       bsh 		if (last == 0)
    230      1.1       bsh 			bus_space_write_4(iot, memctl_ioh, MEMCTL_MECR,
    231      1.1       bsh 			    MECR_NOS | MECR_CIT);
    232      1.1       bsh 	}
    233      1.1       bsh 	else {
    234      1.1       bsh 		sc->sc_cards &= ~(1<<sock_no);
    235      1.1       bsh 		/* if we loast all cards, turn off expansion memory
    236      1.1       bsh 		 * control. */
    237      1.1       bsh #if 0
    238      1.1       bsh 		if (sc->sc_cards == 0)
    239      1.1       bsh 			bus_space_write_4(iot, memctl_ioh,
    240      1.1       bsh 			    MEMCTL_MECR, MECR_NOS);
    241      1.1       bsh #endif
    242      1.1       bsh 	}
    243      1.1       bsh 	splx(s);
    244      1.1       bsh 
    245      1.1       bsh 	DPRINTF(("%s: card %d %s\n", sc->sc_pc.sc_dev.dv_xname, sock_no,
    246      1.1       bsh 	    HAVE_CARD(val) ? "inserted" : "removed"));
    247      1.1       bsh 
    248      1.1       bsh 	sapcic_intr(arg);
    249      1.1       bsh 
    250      1.1       bsh 	return 1;
    251      1.1       bsh }
    252      1.1       bsh #endif /* DONT_USE_CARD_DETECT_INTR */
    253      1.1       bsh 
    254      1.1       bsh static int
    255      1.1       bsh opcic_read(struct sapcic_socket *__so, int which)
    256      1.1       bsh {
    257      1.1       bsh 	struct opcic_socket *so = (struct opcic_socket *)__so;
    258      1.1       bsh 	uint8_t reg;
    259      1.1       bsh 
    260      1.1       bsh 	reg = opcic_read_card_status(so);
    261      1.1       bsh 
    262      1.1       bsh 	switch (which) {
    263      1.1       bsh 	case SAPCIC_STATUS_CARD:
    264      1.1       bsh 		return HAVE_CARD(reg) ?
    265      1.1       bsh 		    SAPCIC_CARD_VALID : SAPCIC_CARD_INVALID;
    266      1.1       bsh 
    267      1.1       bsh 	case SAPCIC_STATUS_VS1:
    268      1.1       bsh 		return (reg & CARDDET_NVS1) == 0;
    269      1.1       bsh 
    270      1.1       bsh 	case SAPCIC_STATUS_VS2:
    271      1.1       bsh 		return (reg & CARDDET_NVS2) == 0;
    272      1.1       bsh 
    273      1.1       bsh 	case SAPCIC_STATUS_READY:
    274      1.1       bsh 		return 1;
    275      1.1       bsh 
    276      1.1       bsh 	default:
    277      1.1       bsh 		panic("%s: bogus register", __FUNCTION__);
    278      1.1       bsh 	}
    279      1.1       bsh }
    280      1.1       bsh 
    281      1.1       bsh static void
    282      1.1       bsh opcic_write(struct sapcic_socket *__so, int which, int arg)
    283      1.1       bsh {
    284      1.1       bsh 	struct opcic_socket *so = (struct opcic_socket *)__so;
    285      1.1       bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->ss.sc;
    286      1.1       bsh 	struct opio_softc *psc =
    287  1.2.2.1      yamt 	    (struct opio_softc *) device_parent(&sc->sc_pc.sc_dev);
    288      1.1       bsh 	struct obio_softc *bsc =
    289  1.2.2.1      yamt 	    (struct obio_softc *) device_parent(&psc->sc_dev);
    290      1.1       bsh 
    291      1.1       bsh 	switch (which) {
    292      1.1       bsh 	case SAPCIC_CONTROL_RESET:
    293      1.1       bsh 		obio_peripheral_reset(bsc, so->ss.socket, arg);
    294      1.1       bsh 		delay(500*1000);
    295      1.1       bsh 		break;
    296      1.1       bsh 
    297      1.1       bsh 	case SAPCIC_CONTROL_LINEENABLE:
    298      1.1       bsh 		break;
    299      1.1       bsh 
    300      1.1       bsh 	case SAPCIC_CONTROL_WAITENABLE:
    301      1.1       bsh 		break;
    302      1.1       bsh 
    303      1.1       bsh 	case SAPCIC_CONTROL_POWERSELECT:
    304      1.1       bsh #if 0
    305      1.1       bsh 		so->voltage = arg;
    306      1.1       bsh #endif
    307      1.1       bsh 		break;
    308      1.1       bsh 
    309      1.1       bsh 	default:
    310      1.1       bsh 		panic("%s: bogus register", __FUNCTION__);
    311      1.1       bsh 	}
    312      1.1       bsh }
    313      1.1       bsh 
    314      1.1       bsh static void
    315      1.1       bsh opcic_set_power(struct sapcic_socket *__so, int arg)
    316      1.1       bsh {
    317      1.1       bsh 	struct opcic_socket *so = (struct opcic_socket *)__so;
    318      1.1       bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->ss.sc;
    319      1.1       bsh 	struct opio_softc *psc =
    320  1.2.2.1      yamt 	    (struct opio_softc *) device_parent(&sc->sc_pc.sc_dev);
    321      1.1       bsh  	int shift, save;
    322      1.1       bsh 	volatile uint8_t *p;
    323      1.1       bsh 
    324      1.1       bsh 	if( arg < 0 || SAPCIC_POWER_5V < arg )
    325      1.1       bsh 		panic("sacpcic_set_power: bogus arg\n");
    326      1.1       bsh 
    327      1.1       bsh 	DPRINTF(("card %d: DET=%x\n",
    328      1.1       bsh 	    so->ss.socket,
    329      1.1       bsh 	    bus_space_read_1(psc->sc_iot, psc->sc_ioh,
    330      1.1       bsh 		GB225_CFDET + 2*so->ss.socket)));
    331      1.1       bsh 
    332      1.1       bsh 	p = (volatile uint8_t *)bus_space_vaddr(psc->sc_iot, psc->sc_ioh)
    333      1.1       bsh 	    + GB225_CARDPOW;
    334      1.1       bsh 
    335      1.1       bsh 	shift = 4 * !so->ss.socket;
    336      1.1       bsh 
    337      1.1       bsh 	save = disable_interrupts(I32_bit);
    338      1.1       bsh 	*p = (*p & ~(0x0f << shift)) | ((CARDPOW_VPPVCC | (arg<<2)) << shift);
    339      1.1       bsh 	restore_interrupts(save);
    340      1.1       bsh 
    341      1.1       bsh 	DPRINTF(("card %d power: %x\n", so->ss.socket, *p));
    342      1.1       bsh }
    343      1.1       bsh 
    344      1.1       bsh static void
    345      1.1       bsh opcic_clear_intr(int arg)
    346      1.1       bsh {
    347      1.1       bsh }
    348      1.1       bsh 
    349      1.1       bsh static void *
    350      1.1       bsh opcic_intr_establish(struct sapcic_socket *so, int level,
    351      1.1       bsh     int (* ih_fun)(void *), void *ih_arg)
    352      1.1       bsh {
    353      1.1       bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->sc;
    354      1.1       bsh 	struct opio_softc *psc =
    355  1.2.2.1      yamt 	    (struct opio_softc *) device_parent(&sc->sc_pc.sc_dev);
    356      1.1       bsh 	struct obio_softc *bsc =
    357  1.2.2.1      yamt 	    (struct obio_softc *) device_parent(&psc->sc_dev);
    358      1.1       bsh 	int irq;
    359      1.1       bsh 
    360      1.1       bsh 	DPRINTF(("opcic_intr_establish %d\n", so->socket));
    361      1.1       bsh 
    362      1.1       bsh 	irq = so->socket ? PCMCIA_INT : CF_INT;
    363      1.1       bsh 	return obio_intr_establish(bsc, irq, level, IST_EDGE_FALLING,
    364      1.1       bsh 	    ih_fun, ih_arg);
    365      1.1       bsh }
    366      1.1       bsh 
    367      1.1       bsh static void
    368      1.1       bsh opcic_intr_disestablish(struct sapcic_socket *so, void *ih)
    369      1.1       bsh {
    370      1.1       bsh 	struct opcic_softc *sc = (struct opcic_softc *)so->sc;
    371      1.1       bsh 	struct opio_softc *psc =
    372  1.2.2.1      yamt 	    (struct opio_softc *) device_parent(&sc->sc_pc.sc_dev);
    373      1.1       bsh 	struct obio_softc *bsc =
    374  1.2.2.1      yamt 	    (struct obio_softc *) device_parent(&psc->sc_dev);
    375      1.1       bsh 	int (* func)(void *) = ((struct obio_handler *)ih)->func;
    376      1.1       bsh 
    377      1.1       bsh 	int irq = so->socket ? PCMCIA_INT : CF_INT;
    378      1.1       bsh 
    379      1.1       bsh 	obio_intr_disestablish(bsc, irq, func);
    380      1.1       bsh }
    381