Home | History | Annotate | Line # | Download | only in dev
ipaq_pcic.c revision 1.3
      1 /*      $NetBSD: ipaq_pcic.c,v 1.3 2001/07/10 17:35:15 toshii 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 Ichiro FUKUHARA (ichiro (at) ichiro.org).
      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 #include <dev/pcmcia/pcmciachip.h>
     51 #include <dev/pcmcia/pcmciavar.h>
     52 
     53 #include <hpcarm/dev/ipaq_saipvar.h>
     54 #include <hpcarm/dev/ipaq_pcicreg.h>
     55 #include <hpcarm/dev/ipaq_gpioreg.h>
     56 #include <hpcarm/sa11x0/sa11x0_gpioreg.h>
     57 #include <hpcarm/sa11x0/sa11x0_reg.h>
     58 #include <hpcarm/sa11x0/sa11x0_var.h>
     59 #include <hpcarm/sa11x0/sa11xx_pcicvar.h>
     60 
     61 #include "ipaqpcic.h"
     62 
     63 static	int	ipaqpcic_match(struct device *, struct cfdata *, void *);
     64 static	void	ipaqpcic_attach(struct device *, struct device *, void *);
     65 static	int	ipaqpcic_print(void *, const char *);
     66 static	int	ipaqpcic_submatch(struct device *, struct cfdata *, void *);
     67 
     68 static	int	ipaqpcic_read(struct sapcic_socket *, int);
     69 static	void	ipaqpcic_write(struct sapcic_socket *, int, int);
     70 static	void	ipaqpcic_set_power(struct sapcic_socket *, int);
     71 static	void	ipaqpcic_clear_intr(int);
     72 static	void	*ipaqpcic_intr_establish(struct sapcic_socket *, int,
     73 				       int (*)(void *), void *);
     74 static	void	ipaqpcic_intr_disestablish(struct sapcic_socket *, void *);
     75 
     76 struct ipaqpcic_softc {
     77 	struct sapcic_softc sc_pc;
     78 	bus_space_handle_t sc_ioh;
     79 	bus_space_handle_t sc_gpioh;
     80 	bus_space_handle_t sc_egpioh;
     81 
     82 	struct sapcic_socket sc_socket[2];
     83 };
     84 
     85 static struct sapcic_tag ipaqpcic_functions = {
     86 	ipaqpcic_read,
     87 	ipaqpcic_write,
     88 	ipaqpcic_set_power,
     89 	ipaqpcic_clear_intr,
     90 	ipaqpcic_intr_establish,
     91 	ipaqpcic_intr_disestablish
     92 };
     93 
     94 struct cfattach ipaqpcic_ca = {
     95 	sizeof(struct ipaqpcic_softc), ipaqpcic_match, ipaqpcic_attach
     96 };
     97 
     98 static int
     99 ipaqpcic_match(parent, cf, aux)
    100 	struct device *parent;
    101 	struct cfdata *cf;
    102 	void *aux;
    103 {
    104 	return (1);
    105 }
    106 
    107 static void
    108 ipaqpcic_attach(parent, self, aux)
    109 	struct device *parent;
    110 	struct device *self;
    111 	void *aux;
    112 {
    113 	int i;
    114 	struct pcmciabus_attach_args paa;
    115 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)self;
    116 	struct ipaq_softc *psc = (struct ipaq_softc *)parent;
    117 
    118 	printf("\n");
    119 
    120 	sc->sc_pc.sc_iot = psc->sc_iot;
    121 	sc->sc_ioh = psc->sc_ioh;
    122 
    123 	sc->sc_gpioh = psc->sc_gpioh;
    124 	sc->sc_egpioh = psc->sc_egpioh;
    125 
    126 	for(i = 0; i < 2; i++) {
    127 		sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
    128 		sc->sc_socket[i].socket = i;
    129 		sc->sc_socket[i].pcictag_cookie = psc;
    130 		sc->sc_socket[i].pcictag = &ipaqpcic_functions;
    131 		sc->sc_socket[i].event_thread = NULL;
    132 		sc->sc_socket[i].event = 0;
    133 		sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
    134 		sc->sc_socket[i].shutdown = 0;
    135 
    136 		paa.paa_busname = "pcmcia";
    137 		paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
    138 		paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
    139 		paa.iobase = 0;
    140 		paa.iosize = 0x4000000;
    141 
    142 		sc->sc_socket[i].pcmcia =
    143 		    (struct device *)config_found_sm(&sc->sc_pc.sc_dev,
    144 		    &paa, ipaqpcic_print, ipaqpcic_submatch);
    145 
    146 		sa11x0_intr_establish((sa11x0_chipset_tag_t)psc,
    147 			    i ? IRQ_CD1 : IRQ_CD0,
    148 			    1, IPL_BIO, sapcic_intr,
    149 			    &sc->sc_socket[i]);
    150 
    151 		/* schedule kthread creation */
    152 		kthread_create(sapcic_kthread_create, &sc->sc_socket[i]);
    153 
    154 #if 0 /* XXX */
    155 		/* establish_intr should be after creating the kthread */
    156 		config_interrupt(&sc->sc_socket[i], ipaqpcic_config_intr);
    157 #endif
    158 	}
    159 }
    160 
    161 static int
    162 ipaqpcic_print(aux, name)
    163 	void *aux;
    164 	const char *name;
    165 {
    166 	return (UNCONF);
    167 }
    168 
    169 static int
    170 ipaqpcic_submatch(parent, cf, aux)
    171 	struct device *parent;
    172 	struct cfdata *cf;
    173 	void *aux;
    174 {
    175 	return (*cf->cf_attach->ca_match)(parent, cf, aux);
    176 }
    177 
    178 
    179 static int
    180 ipaqpcic_read(so, reg)
    181 	struct sapcic_socket *so;
    182 	int reg;
    183 {
    184 	int cr, bit;
    185 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
    186 
    187 	cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_gpioh, SAGPIO_PLR);
    188 
    189 	switch (reg) {
    190 	case SAPCIC_STATUS_CARD:
    191 	case SAPCIC_STATUS_READY:
    192 		bit = (so->socket ? GPIO_H3600_PCMCIA_CD1 :
    193 				GPIO_H3600_PCMCIA_CD0) & cr;
    194 		if (bit)
    195 			return SAPCIC_CARD_INVALID;
    196 		else
    197 			return SAPCIC_CARD_VALID;
    198 	case SAPCIC_STATUS_VS1:
    199         case SAPCIC_STATUS_VS2:
    200 	default:
    201 		panic("ipaqpcic_read: bogus register\n");
    202 	}
    203 }
    204 
    205 static void
    206 ipaqpcic_write(so, reg, arg)
    207 	struct sapcic_socket *so;
    208 	int reg;
    209 	int arg;
    210 {
    211 	int s, value;
    212 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
    213 
    214 	value = 0;
    215 	s = splhigh();
    216 	switch (reg) {
    217 	case SAPCIC_CONTROL_RESET:
    218 		value = EGPIO_H3600_CARD_RESET;
    219 		break;
    220 	case SAPCIC_CONTROL_LINEENABLE:
    221 	case SAPCIC_CONTROL_WAITENABLE:
    222 	case SAPCIC_CONTROL_POWERSELECT:
    223 		break;
    224 
    225 	default:
    226 		splx(s);
    227 		panic("ipaqpcic_write: bogus register");
    228 	}
    229 	value |= bus_space_read_2(sc->sc_pc.sc_iot, sc->sc_egpioh, 0) & 0xFFFF;
    230 #if 0
    231 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_egpioh, 0, value);
    232 #endif
    233 	splx(s);
    234 }
    235 
    236 static void
    237 ipaqpcic_set_power(so, arg)
    238 	struct sapcic_socket *so;
    239 	int arg;
    240 {
    241 	int value, s;
    242 	struct ipaqpcic_softc *sc = so->pcictag_cookie;
    243 
    244 	s = splbio();
    245 	switch (arg) {
    246 	case SAPCIC_POWER_OFF:
    247 		value &= ~(EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON);
    248 		break;
    249 	case SAPCIC_POWER_3V:
    250 	case SAPCIC_POWER_5V:
    251 		value |= EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON;
    252 		break;
    253 	default:
    254 		panic("ipaqpcic_set_power: bogus arg\n");
    255 	}
    256 
    257 	value |= bus_space_read_2(sc->sc_pc.sc_iot, sc->sc_egpioh, 0) & 0xFFFF;
    258 #if 0
    259 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_egpioh, 0, value);
    260 #endif
    261 	splx(s);
    262 }
    263 
    264 static void
    265 ipaqpcic_clear_intr(arg)
    266 {
    267 }
    268 
    269 static void *
    270 ipaqpcic_intr_establish(so, level, ih_fun, ih_arg)
    271 	struct sapcic_socket *so;
    272 	int level;
    273 	int (*ih_fun)(void *);
    274 	void *ih_arg;
    275 {
    276 	int irq;
    277 
    278 	irq = so->socket ? IRQ_IRQ0 : IRQ_IRQ1;
    279 	return (sa11x0_intr_establish((sa11x0_chipset_tag_t)so->pcictag_cookie,
    280 				    irq-16, 1, level, ih_fun, ih_arg));
    281 }
    282 
    283 static void
    284 ipaqpcic_intr_disestablish(so, ih)
    285 	struct sapcic_socket *so;
    286 	void *ih;
    287 {
    288 	sa11x0_intr_disestablish((sa11x0_chipset_tag_t)so->pcictag_cookie, ih);
    289 }
    290