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