Home | History | Annotate | Line # | Download | only in sa11x0
sa11x1_pcic.c revision 1.7
      1 /*      $NetBSD: sa11x1_pcic.c,v 1.7 2002/10/02 05:02:32 thorpej 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 CFATTACH_DECL(sacpcic, sizeof(struct sacpcic_softc),
    108     sacpcic_match, sacpcic_attach, NULL, NULL);
    109 
    110 static int
    111 sacpcic_match(parent, cf, aux)
    112 	struct device *parent;
    113 	struct cfdata *cf;
    114 	void *aux;
    115 {
    116 	return (1);
    117 }
    118 
    119 static void
    120 sacpcic_attach(parent, self, aux)
    121 	struct device *parent;
    122 	struct device *self;
    123 	void *aux;
    124 {
    125 	int i;
    126 #ifdef hpcarm
    127 	int *ip;
    128 #endif
    129 	struct pcmciabus_attach_args paa;
    130 	struct sacpcic_softc *sc = (struct sacpcic_softc *)self;
    131 	struct sacc_softc *psc = (struct sacc_softc *)parent;
    132 #ifdef hpcarm
    133 	struct platid_data *p;
    134 #endif
    135 
    136 	printf("\n");
    137 
    138 	sc->sc_pc.sc_iot = psc->sc_iot;
    139 	sc->sc_ioh = psc->sc_ioh;
    140 #ifdef hpcarm
    141 	p = platid_search_data(&platid, sacpcic_platid_table);
    142 #endif
    143 
    144 	for(i = 0; i < 2; i++) {
    145 		sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
    146 		sc->sc_socket[i].socket = i;
    147 		sc->sc_socket[i].pcictag_cookie = psc;
    148 		sc->sc_socket[i].pcictag = &sacpcic_functions;
    149 		sc->sc_socket[i].event_thread = NULL;
    150 		sc->sc_socket[i].event = 0;
    151 		sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
    152 		sc->sc_socket[i].shutdown = 0;
    153 
    154 #ifdef hpcarm
    155 		if (p == NULL) {
    156 			sc->sc_socket[i].power_capability = SAPCIC_POWER_5V;
    157 		} else {
    158 			ip = (int *)p->data;
    159 			sc->sc_socket[i].power_capability = ip[i];
    160 		}
    161 #else
    162 		/* XXX */
    163 		sc->sc_socket[i].power_capability = SAPCIC_POWER_5V;
    164 #endif
    165 
    166 		paa.paa_busname = "pcmcia";
    167 		paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
    168 		paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
    169 		paa.iobase = 0;
    170 		paa.iosize = 0x4000000;
    171 
    172 		sc->sc_socket[i].pcmcia =
    173 		    (struct device *)config_found_sm(&sc->sc_pc.sc_dev,
    174 		    &paa, sacpcic_print, sacpcic_submatch);
    175 
    176 		sacc_intr_establish((sacc_chipset_tag_t)psc,
    177 				    i ? IRQ_S1_CDVALID : IRQ_S0_CDVALID,
    178 				    IST_EDGE_RAISE, IPL_BIO, sapcic_intr,
    179 				    &sc->sc_socket[i]);
    180 
    181 		/* schedule kthread creation */
    182 		kthread_create(sapcic_kthread_create, &sc->sc_socket[i]);
    183 
    184 #if 0 /* XXX */
    185 		/* establish_intr should be after creating the kthread */
    186 		config_interrupt(&sc->sc_socket[i], sacpcic_config_intr);
    187 #endif
    188 	}
    189 }
    190 
    191 static int
    192 sacpcic_print(aux, name)
    193 	void *aux;
    194 	const char *name;
    195 {
    196 	return (UNCONF);
    197 }
    198 
    199 static int
    200 sacpcic_submatch(parent, cf, aux)
    201 	struct device *parent;
    202 	struct cfdata *cf;
    203 	void *aux;
    204 {
    205 	return config_match(parent, cf, aux);
    206 }
    207 
    208 
    209 static int
    210 sacpcic_read(so, reg)
    211 	struct sapcic_socket *so;
    212 	int reg;
    213 {
    214 	int cr, bit;
    215 	struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
    216 
    217 	cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_SR);
    218 
    219 	switch (reg) {
    220 	case SAPCIC_STATUS_CARD:
    221 		bit = (so->socket ? SR_S1_CARDDETECT : SR_S0_CARDDETECT) & cr;
    222 		if (bit)
    223 			return SAPCIC_CARD_INVALID;
    224 		else
    225 			return SAPCIC_CARD_VALID;
    226 
    227 	case SAPCIC_STATUS_VS1:
    228 		bit = (so->socket ? SR_S1_VS1 : SR_S0_VS1);
    229 		return (bit & cr);
    230 
    231 	case SAPCIC_STATUS_VS2:
    232 		bit = (so->socket ? SR_S1_VS2 : SR_S0_VS2);
    233 		return (bit & cr);
    234 
    235 	case SAPCIC_STATUS_READY:
    236 		bit = (so->socket ? SR_S1_READY : SR_S0_READY);
    237 		return (bit & cr);
    238 
    239 	default:
    240 		panic("sacpcic_read: bogus register");
    241 	}
    242 }
    243 
    244 static void
    245 sacpcic_write(so, reg, arg)
    246 	struct sapcic_socket *so;
    247 	int reg;
    248 	int arg;
    249 {
    250 	int s, oldvalue, newvalue, mask;
    251 	struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
    252 
    253 	s = splhigh();
    254 	oldvalue = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR);
    255 
    256 	switch (reg) {
    257 	case SAPCIC_CONTROL_RESET:
    258 		mask = so->socket ? CR_S1_RST : CR_S0_RST;
    259 
    260 		newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
    261 		break;
    262 
    263 	case SAPCIC_CONTROL_LINEENABLE:
    264 		mask = so->socket ? CR_S1_FLT : CR_S0_FLT;
    265 
    266 		newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
    267 		break;
    268 
    269 	case SAPCIC_CONTROL_WAITENABLE:
    270 		mask = so->socket ? CR_S1_PWAITEN : CR_S0_PWAITEN;
    271 
    272 		newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
    273 		break;
    274 
    275 	case SAPCIC_CONTROL_POWERSELECT:
    276 		mask = so->socket ? CR_S1_PSE : CR_S0_PSE;
    277 		newvalue = oldvalue & ~mask;
    278 
    279 		switch (arg) {
    280 		case SAPCIC_POWER_3V:
    281 			break;
    282 		case SAPCIC_POWER_5V:
    283 			newvalue |= mask;
    284 			break;
    285 		default:
    286 			splx(s);
    287 			panic("sacpcic_write: bogus arg");
    288 		}
    289 		break;
    290 
    291 	default:
    292 		splx(s);
    293 		panic("sacpcic_write: bogus register");
    294 	}
    295 	bus_space_write_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR, newvalue);
    296 	splx(s);
    297 }
    298 
    299 static void
    300 sacpcic_set_power(so, arg)
    301 	struct sapcic_socket *so;
    302 	int arg;
    303 {
    304 	/* XXX this should go to dev/jornada720.c */
    305 	int newval, oldval, s;
    306 	struct sacc_softc *sc = so->pcictag_cookie;
    307 
    308 	/* XXX this isn't well confirmed. DANGER DANGER */
    309 	switch (arg) {
    310 	case SAPCIC_POWER_OFF:
    311 		newval = 0;
    312 		break;
    313 	case SAPCIC_POWER_3V:
    314 		newval = 2;
    315 		break;
    316 	case SAPCIC_POWER_5V:
    317 		newval = 1;
    318 		break;
    319 	default:
    320 		panic("sacpcic_set_power: bogus arg");
    321 	}
    322 
    323 	s = splbio();
    324 	oldval = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    325 				  SACCGPIOA_DVR);
    326 	switch (so->socket) {
    327 	case 0:
    328 		newval = newval | (oldval & 0xc);
    329 		break;
    330 	case 1:
    331 		newval = (newval << 2) | (oldval & 3);
    332 		break;
    333 	default:
    334 		splx(s);
    335 		panic("sacpcic_set_power");
    336 	}
    337 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCGPIOA_DVR, newval);
    338 	splx(s);
    339 }
    340 
    341 static void
    342 sacpcic_clear_intr(arg)
    343 {
    344 	/* sacc_intr_dispatch takes care of intr status */
    345 }
    346 
    347 static void *
    348 sacpcic_intr_establish(so, level, ih_fun, ih_arg)
    349 	struct sapcic_socket *so;
    350 	int level;
    351 	int (*ih_fun)(void *);
    352 	void *ih_arg;
    353 {
    354 	int irq;
    355 
    356 	irq = so->socket ? IRQ_S1_READY : IRQ_S0_READY;
    357 	return (sacc_intr_establish((sacc_chipset_tag_t)so->pcictag_cookie, irq,
    358 				    IST_EDGE_FALL, level, ih_fun, ih_arg));
    359 }
    360 
    361 static void
    362 sacpcic_intr_disestablish(so, ih)
    363 	struct sapcic_socket *so;
    364 	void *ih;
    365 {
    366 	sacc_intr_disestablish((sacc_chipset_tag_t)so->pcictag_cookie, ih);
    367 }
    368