Home | History | Annotate | Line # | Download | only in sa11x0
sa11x1_pcic.c revision 1.1
      1 /*      $NetBSD: sa11x1_pcic.c,v 1.1 2001/07/08 23:37:54 rjs Exp $        */
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by IWAMOTO Toshihiro.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/types.h>
     42 #include <sys/conf.h>
     43 #include <sys/file.h>
     44 #include <sys/device.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kthread.h>
     47 #include <sys/malloc.h>
     48 
     49 #include <machine/bus.h>
     50 #ifdef hpcarm
     51 #include <machine/platid.h>
     52 #include <machine/platid_mask.h>
     53 #endif
     54 
     55 #include <dev/pcmcia/pcmciachip.h>
     56 #include <dev/pcmcia/pcmciavar.h>
     57 #include <arm/sa11x0/sa11x0_reg.h>
     58 #include <arm/sa11x0/sa11x0_var.h>
     59 #include <arm/sa11x0/sa1111_reg.h>
     60 #include <arm/sa11x0/sa1111_var.h>
     61 #include <arm/sa11x0/sa11x1_pcicreg.h>
     62 #include <arm/sa11x0/sa11xx_pcicvar.h>
     63 
     64 #include "sacpcic.h"
     65 
     66 static	int	sacpcic_match(struct device *, struct cfdata *, void *);
     67 static	void	sacpcic_attach(struct device *, struct device *, void *);
     68 static	int	sacpcic_print(void *, const char *);
     69 static	int	sacpcic_submatch(struct device *, struct cfdata *, void *);
     70 
     71 static	int	sacpcic_read(struct sapcic_socket *, int);
     72 static	void	sacpcic_write(struct sapcic_socket *, int, int);
     73 static	void	sacpcic_set_power(struct sapcic_socket *, int);
     74 static	void	sacpcic_clear_intr(int);
     75 static	void	*sacpcic_intr_establish(struct sapcic_socket *, int,
     76 				       int (*)(void *), void *);
     77 static	void	sacpcic_intr_disestablish(struct sapcic_socket *, void *);
     78 
     79 struct sacpcic_softc {
     80 	struct sapcic_softc sc_pc;
     81 	bus_space_handle_t sc_ioh;
     82 
     83 	struct sapcic_socket sc_socket[2];
     84 };
     85 
     86 static struct sapcic_tag sacpcic_functions = {
     87 	sacpcic_read,
     88 	sacpcic_write,
     89 	sacpcic_set_power,
     90 	sacpcic_clear_intr,
     91 	sacpcic_intr_establish,
     92 	sacpcic_intr_disestablish
     93 };
     94 
     95 #ifdef hpcarm
     96 static int j720_power_capability[] = {
     97 	SAPCIC_POWER_5V | SAPCIC_POWER_3V, SAPCIC_POWER_3V
     98 };
     99 
    100 static struct platid_data sacpcic_platid_table[] = {
    101 	{ &platid_mask_MACH_HP_JORNADA_720, j720_power_capability },
    102 	{ &platid_mask_MACH_HP_JORNADA_720JP, j720_power_capability },
    103 	{ NULL, NULL }
    104 };
    105 #endif
    106 
    107 struct cfattach sacpcic_ca = {
    108 	sizeof(struct sacpcic_softc), sacpcic_match, sacpcic_attach
    109 };
    110 
    111 static int
    112 sacpcic_match(parent, cf, aux)
    113 	struct device *parent;
    114 	struct cfdata *cf;
    115 	void *aux;
    116 {
    117 	return (1);
    118 }
    119 
    120 static void
    121 sacpcic_attach(parent, self, aux)
    122 	struct device *parent;
    123 	struct device *self;
    124 	void *aux;
    125 {
    126 	int i;
    127 #ifdef hpcarm
    128 	int *ip;
    129 #endif
    130 	struct pcmciabus_attach_args paa;
    131 	struct sacpcic_softc *sc = (struct sacpcic_softc *)self;
    132 	struct sacc_softc *psc = (struct sacc_softc *)parent;
    133 #ifdef hpcarm
    134 	struct platid_data *p;
    135 #endif
    136 
    137 	printf("\n");
    138 
    139 	sc->sc_pc.sc_iot = psc->sc_iot;
    140 	sc->sc_ioh = psc->sc_ioh;
    141 #ifdef hpcarm
    142 	p = platid_search(&platid, sacpcic_platid_table);
    143 #endif
    144 
    145 	for(i = 0; i < 2; i++) {
    146 		sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
    147 		sc->sc_socket[i].socket = i;
    148 		sc->sc_socket[i].saip_sc = NULL;
    149 		sc->sc_socket[i].sacc_sc = psc;
    150 		sc->sc_socket[i].pcictag = &sacpcic_functions;
    151 		sc->sc_socket[i].pddata = NULL;		/* XXX */
    152 		sc->sc_socket[i].event_thread = NULL;
    153 		sc->sc_socket[i].event = 0;
    154 		sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
    155 		sc->sc_socket[i].shutdown = 0;
    156 
    157 #ifdef hpcarm
    158 		if (p == NULL) {
    159 			sc->sc_socket[i].power_capability = SAPCIC_POWER_5V;
    160 		} else {
    161 			ip = (int *)p->data;
    162 			sc->sc_socket[i].power_capability = ip[i];
    163 		}
    164 #else
    165 		/* XXX */
    166 		sc->sc_socket[i].power_capability = SAPCIC_POWER_5V;
    167 #endif
    168 
    169 		paa.paa_busname = "pcmcia";
    170 		paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
    171 		paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
    172 		paa.iobase = 0;
    173 		paa.iosize = 0x4000000;
    174 
    175 		sc->sc_socket[i].pcmcia =
    176 		    (struct device *)config_found_sm(&sc->sc_pc.sc_dev,
    177 		    &paa, sacpcic_print, sacpcic_submatch);
    178 
    179 		sacc_intr_establish((sacc_chipset_tag_t)psc,
    180 				    i ? IRQ_S1_CDVALID : IRQ_S0_CDVALID,
    181 				    IST_EDGE_RAISE, IPL_BIO, sapcic_intr,
    182 				    &sc->sc_socket[i]);
    183 
    184 		/* schedule kthread creation */
    185 		kthread_create(sapcic_kthread_create, &sc->sc_socket[i]);
    186 
    187 #if 0 /* XXX */
    188 		/* establish_intr should be after creating the kthread */
    189 		config_interrupt(&sc->sc_socket[i], sacpcic_config_intr);
    190 #endif
    191 	}
    192 }
    193 
    194 static int
    195 sacpcic_print(aux, name)
    196 	void *aux;
    197 	const char *name;
    198 {
    199 	return (UNCONF);
    200 }
    201 
    202 static int
    203 sacpcic_submatch(parent, cf, aux)
    204 	struct device *parent;
    205 	struct cfdata *cf;
    206 	void *aux;
    207 {
    208 	return (*cf->cf_attach->ca_match)(parent, cf, aux);
    209 }
    210 
    211 
    212 static int
    213 sacpcic_read(so, reg)
    214 	struct sapcic_socket *so;
    215 	int reg;
    216 {
    217 	int cr, bit;
    218 	struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
    219 
    220 	cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_SR);
    221 
    222 	switch (reg) {
    223 	case SAPCIC_STATUS_CARD:
    224 		bit = (so->socket ? SR_S1_CARDDETECT : SR_S0_CARDDETECT) & cr;
    225 		if (bit)
    226 			return SAPCIC_CARD_INVALID;
    227 		else
    228 			return SAPCIC_CARD_VALID;
    229 
    230 	case SAPCIC_STATUS_VS1:
    231 		bit = (so->socket ? SR_S1_VS1 : SR_S0_VS1);
    232 		return (bit & cr);
    233 
    234 	case SAPCIC_STATUS_VS2:
    235 		bit = (so->socket ? SR_S1_VS2 : SR_S0_VS2);
    236 		return (bit & cr);
    237 
    238 	case SAPCIC_STATUS_READY:
    239 		bit = (so->socket ? SR_S1_READY : SR_S0_READY);
    240 		return (bit & cr);
    241 
    242 	default:
    243 		panic("sacpcic_read: bogus register\n");
    244 	}
    245 }
    246 
    247 static void
    248 sacpcic_write(so, reg, arg)
    249 	struct sapcic_socket *so;
    250 	int reg;
    251 	int arg;
    252 {
    253 	int s, oldvalue, newvalue, mask;
    254 	struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
    255 
    256 	s = splhigh();
    257 	oldvalue = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR);
    258 
    259 	switch (reg) {
    260 	case SAPCIC_CONTROL_RESET:
    261 		mask = so->socket ? CR_S1_RST : CR_S0_RST;
    262 
    263 		newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
    264 		break;
    265 
    266 	case SAPCIC_CONTROL_LINEENABLE:
    267 		mask = so->socket ? CR_S1_FLT : CR_S0_FLT;
    268 
    269 		newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
    270 		break;
    271 
    272 	case SAPCIC_CONTROL_WAITENABLE:
    273 		mask = so->socket ? CR_S1_PWAITEN : CR_S0_PWAITEN;
    274 
    275 		newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
    276 		break;
    277 
    278 	case SAPCIC_CONTROL_POWERSELECT:
    279 		mask = so->socket ? CR_S1_PSE : CR_S0_PSE;
    280 		newvalue = oldvalue & ~mask;
    281 
    282 		switch (arg) {
    283 		case SAPCIC_POWER_3V:
    284 			break;
    285 		case SAPCIC_POWER_5V:
    286 			newvalue |= mask;
    287 			break;
    288 		default:
    289 			splx(s);
    290 			panic("sacpcic_write: bogus arg");
    291 		}
    292 		break;
    293 
    294 	default:
    295 		splx(s);
    296 		panic("sacpcic_write: bogus register");
    297 	}
    298 	bus_space_write_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR, newvalue);
    299 	splx(s);
    300 }
    301 
    302 static void
    303 sacpcic_set_power(so, arg)
    304 	struct sapcic_socket *so;
    305 	int arg;
    306 {
    307 	/* XXX this should go to dev/jornada720.c */
    308 	int newval, oldval, s;
    309 
    310 	/* XXX this isn't well confirmed. DANGER DANGER */
    311 	switch (arg) {
    312 	case SAPCIC_POWER_OFF:
    313 		newval = 0;
    314 		break;
    315 	case SAPCIC_POWER_3V:
    316 		newval = 2;
    317 		break;
    318 	case SAPCIC_POWER_5V:
    319 		newval = 1;
    320 		break;
    321 	default:
    322 		panic("sacpcic_set_power: bogus arg\n");
    323 	}
    324 
    325 	s = splbio();
    326 	oldval = bus_space_read_4(so->sacc_sc->sc_iot, so->sacc_sc->sc_ioh,
    327 				  SACCGPIOA_DVR);
    328 	switch (so->socket) {
    329 	case 0:
    330 		newval = newval | (oldval & 0xc);
    331 		break;
    332 	case 1:
    333 		newval = (newval << 2) | (oldval & 3);
    334 		break;
    335 	default:
    336 		splx(s);
    337 		panic("sacpcic_set_power\n");
    338 	}
    339 	bus_space_write_4(so->sacc_sc->sc_iot, so->sacc_sc->sc_ioh,
    340 			  SACCGPIOA_DVR, newval);
    341 	splx(s);
    342 }
    343 
    344 static void
    345 sacpcic_clear_intr(arg)
    346 {
    347 	/* sacc_intr_dispatch takes care of intr status */
    348 }
    349 
    350 static void *
    351 sacpcic_intr_establish(so, level, ih_fun, ih_arg)
    352 	struct sapcic_socket *so;
    353 	int level;
    354 	int (*ih_fun)(void *);
    355 	void *ih_arg;
    356 {
    357 	int irq;
    358 
    359 	irq = so->socket ? IRQ_S1_READY : IRQ_S0_READY;
    360 	return (sacc_intr_establish((sacc_chipset_tag_t)so->sacc_sc, irq,
    361 				    IST_EDGE_FALL, level, ih_fun, ih_arg));
    362 }
    363 
    364 static void
    365 sacpcic_intr_disestablish(so, ih)
    366 	struct sapcic_socket *so;
    367 	void *ih;
    368 {
    369 	sacc_intr_disestablish((sacc_chipset_tag_t)so->sacc_sc, ih);
    370 }
    371