Home | History | Annotate | Line # | Download | only in dev
scoop_pcic.c revision 1.1
      1 /*	$NetBSD: scoop_pcic.c,v 1.1 2006/12/16 05:17:30 ober Exp $	*/
      2 /*	$OpenBSD: scoop_pcic.c,v 1.1 2005/07/01 23:51:55 uwe Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2005 Uwe Stuehler <uwe (at) bsdx.de>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #include <sys/cdefs.h>
     21 __KERNEL_RCSID(0, "$NetBSD: scoop_pcic.c,v 1.1 2006/12/16 05:17:30 ober Exp $");
     22 
     23 #include <sys/param.h>
     24 #include <sys/systm.h>
     25 #include <sys/device.h>
     26 
     27 #include <uvm/uvm.h>
     28 
     29 #include <arch/arm/xscale/pxa2x0var.h>
     30 #include <arch/arm/xscale/pxa2x0_pcicvar.h>
     31 
     32 #include <zaurus/zaurus/zaurus_reg.h>
     33 #include <zaurus/zaurus/zaurus_var.h>
     34 
     35 #include <zaurus/dev/scoopreg.h>
     36 
     37 static int	scoop_pcic_match(struct device *, struct cfdata *, void *);
     38 static void	scoop_pcic_attach(struct device *, struct device *, void *);
     39 
     40 CFATTACH_DECL(pxapcic_scoop, sizeof(struct pxapcic_softc),
     41 	scoop_pcic_match, scoop_pcic_attach, NULL, NULL);
     42 
     43 static void	scoop_pcic_socket_setup(struct pxapcic_socket *);
     44 static u_int	scoop_pcic_read(struct pxapcic_socket *, int);
     45 static void	scoop_pcic_write(struct pxapcic_socket *, int, u_int);
     46 static void	scoop_pcic_set_power(struct pxapcic_socket *, int);
     47 static void	scoop_pcic_clear_intr(struct pxapcic_socket *);
     48 
     49 struct pxapcic_tag scoop_pcic_functions = {
     50 	scoop_pcic_read,
     51 	scoop_pcic_write,
     52 	scoop_pcic_set_power,
     53 	scoop_pcic_clear_intr,
     54 	0,			/* intr_establish */
     55 	0,			/* intr_disestablish */
     56 	0			/* intr_string */
     57 };
     58 
     59 static int
     60 scoop_pcic_match(struct device *parent, struct cfdata *cf, void *aux)
     61 {
     62 
     63 	return (ZAURUS_ISC860 || ZAURUS_ISC3000);
     64 }
     65 
     66 static void
     67 scoop_pcic_attach(struct device *parent, struct device *self, void *aux)
     68 {
     69 	struct pxapcic_softc *sc = (struct pxapcic_softc *)self;
     70 	struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
     71 
     72 	sc->sc_iot = pxa->pxa_iot;
     73 
     74 	if (ZAURUS_ISC860) {
     75 		sc->sc_nslots = 1;
     76 		sc->sc_irqpin[0] = C860_CF0_IRQ;
     77 		sc->sc_irqcfpin[0] = C860_CF0_IRQ_PIN;
     78 	} else if (ZAURUS_ISC3000) {
     79 		sc->sc_nslots = 2;
     80 		sc->sc_irqpin[0] = C3000_CF0_IRQ;
     81 		sc->sc_irqcfpin[0] = C3000_CF0_IRQ_PIN;
     82 		sc->sc_irqpin[1] = C3000_CF1_IRQ;
     83 		sc->sc_irqcfpin[1] = C3000_CF1_IRQ_PIN;
     84 	}
     85 	sc->sc_flags |= PXAPCIC_FLAG_REVERSE_ATTACH;
     86 
     87 	pxapcic_attach(sc, &scoop_pcic_socket_setup);
     88 }
     89 
     90 static void
     91 scoop_pcic_socket_setup(struct pxapcic_socket *so)
     92 {
     93 	struct pxapcic_softc *sc;
     94 	bus_addr_t pa;
     95 	bus_size_t size = SCOOP_SIZE;
     96 	bus_space_tag_t iot;
     97 	bus_space_handle_t scooph;
     98 	int error;
     99 
    100 	sc = so->sc;
    101 	iot = sc->sc_iot;
    102 
    103 	if (so->socket == 0) {
    104 		pa = C3000_SCOOP0_BASE;
    105 	} else if (so->socket == 1) {
    106 		pa = C3000_SCOOP1_BASE;
    107 	} else {
    108 		panic("%s: invalid CF slot %d", sc->sc_dev.dv_xname,
    109 		    so->socket);
    110 	}
    111 
    112 	error = bus_space_map(iot, trunc_page(pa), round_page(size),
    113 	    0, &scooph);
    114 	if (error) {
    115 		panic("%s: failed to map memory %x for scoop",
    116 		    sc->sc_dev.dv_xname, (uint32_t)pa);
    117 	}
    118 	scooph += pa - trunc_page(pa);
    119 
    120 	bus_space_write_2(iot, scooph, SCOOP_IMR,
    121 	    SCP_IMR_UNKN0 | SCP_IMR_UNKN1);
    122 
    123 	/* setup */
    124 	bus_space_write_2(iot, scooph, SCOOP_MCR, 0x0100);
    125 	bus_space_write_2(iot, scooph, SCOOP_CDR, 0x0000);
    126 	bus_space_write_2(iot, scooph, SCOOP_CPR, 0x0000);
    127 	bus_space_write_2(iot, scooph, SCOOP_IMR, 0x0000);
    128 	bus_space_write_2(iot, scooph, SCOOP_IRM, 0x00ff);
    129 	bus_space_write_2(iot, scooph, SCOOP_ISR, 0x0000);
    130 	bus_space_write_2(iot, scooph, SCOOP_IRM, 0x0000);
    131 
    132 	/* C3000 */
    133 	if (so->socket == 1) {
    134 		bus_space_write_2(iot, scooph, SCOOP_CPR, 0x80c1);
    135 		bus_space_write_2(iot, scooph, SCOOP_IMR, 0x00c4);
    136 		bus_space_write_2(iot, scooph, SCOOP_MCR, 0x0111);
    137 	} else {
    138 		bus_space_write_2(iot, scooph, SCOOP_CPR,
    139 		    SCP_CPR_PWR|SCP_CPR_5V);
    140 	}
    141 
    142 	bus_space_write_2(iot, scooph, SCOOP_IMR, 0x00ce);
    143 	bus_space_write_2(iot, scooph, SCOOP_MCR, 0x0111);
    144 
    145 	/* C3000 */
    146 	so->power_capability = PXAPCIC_POWER_3V;
    147 	if (so->socket == 0)
    148 		so->power_capability |= PXAPCIC_POWER_5V;
    149 
    150 	so->pcictag_cookie = (void *)scooph;
    151 	so->pcictag = &scoop_pcic_functions;
    152 }
    153 
    154 static u_int
    155 scoop_pcic_read(struct pxapcic_socket *so, int reg)
    156 {
    157 	bus_space_tag_t iot = so->sc->sc_iot;
    158 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
    159 	uint16_t csr;
    160 
    161 	csr = bus_space_read_2(iot, ioh, SCOOP_CSR);
    162 
    163 	switch (reg) {
    164 	case PXAPCIC_CARD_STATUS:
    165 		if (csr & SCP_CSR_MISSING)
    166 			return (PXAPCIC_CARD_INVALID);
    167 		else
    168 			return (PXAPCIC_CARD_VALID);
    169 
    170 	case PXAPCIC_CARD_READY:
    171 		return ((bus_space_read_2(iot, ioh, SCOOP_CSR) &
    172 		    SCP_CSR_READY) != 0);
    173 
    174 	default:
    175 		panic("scoop_pcic_read: bogus register");
    176 	}
    177 	/*NOTREACHED*/
    178 }
    179 
    180 static void
    181 scoop_pcic_write(struct pxapcic_socket *so, int reg, u_int val)
    182 {
    183 	bus_space_tag_t iot = so->sc->sc_iot;
    184 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
    185 	uint16_t newval;
    186 	int s;
    187 
    188 	s = splhigh();
    189 
    190 	switch (reg) {
    191 	case PXAPCIC_CARD_POWER:
    192 		newval = bus_space_read_2(iot, ioh, SCOOP_CPR);
    193 		newval &= ~(SCP_CPR_PWR | SCP_CPR_3V | SCP_CPR_5V);
    194 
    195 		if (val == PXAPCIC_POWER_3V)
    196 			newval |= (SCP_CPR_PWR | SCP_CPR_3V);
    197 		else if (val == PXAPCIC_POWER_5V)
    198 			newval |= (SCP_CPR_PWR | SCP_CPR_5V);
    199 
    200 		bus_space_write_2(iot, ioh, SCOOP_CPR, newval);
    201 		break;
    202 
    203 	case PXAPCIC_CARD_RESET:
    204 		bus_space_write_2(iot, ioh, SCOOP_CCR,
    205 		    val ? SCP_CCR_RESET : 0);
    206 		break;
    207 
    208 	default:
    209 		panic("scoop_pcic_write: bogus register");
    210 	}
    211 
    212 	splx(s);
    213 }
    214 
    215 static void
    216 scoop_pcic_set_power(struct pxapcic_socket *so, int pwr)
    217 {
    218 	bus_space_tag_t iot = so->sc->sc_iot;
    219 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
    220 	u_int16_t reg;
    221 	int s;
    222 
    223 	s = splhigh();
    224 
    225 	switch (pwr) {
    226 	case PXAPCIC_POWER_OFF:
    227 #if 0
    228 		/* XXX does this disable power to both sockets? */
    229 		reg = bus_space_read_2(iot, ioh, SCOOP_GPWR);
    230 		bus_space_write_2(iot, ioh, SCOOP_GPWR,
    231 		    reg & ~(1 << SCOOP0_CF_POWER_C3000));
    232 #endif
    233 		break;
    234 
    235 	case PXAPCIC_POWER_3V:
    236 	case PXAPCIC_POWER_5V:
    237 		/* XXX */
    238 		if (so->socket == 0) {
    239 			reg = bus_space_read_2(iot, ioh, SCOOP_GPWR);
    240 			bus_space_write_2(iot, ioh, SCOOP_GPWR,
    241 			    reg | (1 << SCOOP0_CF_POWER_C3000));
    242 		}
    243 		break;
    244 
    245 	default:
    246 		splx(s);
    247 		panic("scoop_pcic_set_power: bogus power state");
    248 	}
    249 
    250 	splx(s);
    251 }
    252 
    253 static void
    254 scoop_pcic_clear_intr(struct pxapcic_socket *so)
    255 {
    256 	bus_space_tag_t iot = so->sc->sc_iot;
    257 	bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
    258 
    259 	bus_space_write_2(iot, ioh, SCOOP_IRM, 0x00ff);
    260 	bus_space_write_2(iot, ioh, SCOOP_ISR, 0x0000);
    261 	bus_space_write_2(iot, ioh, SCOOP_IRM, 0x0000);
    262 }
    263