Home | History | Annotate | Line # | Download | only in g42xxeb
g42xxeb_mci.c revision 1.1.4.2
      1 /*	$NetBSD: g42xxeb_mci.c,v 1.1.4.2 2010/04/30 14:39:15 uebayasi Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2009  Genetec Corporation.  All rights reserved.
      5  * Written by Hiroyuki Bessho for Genetec Corporation.
      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
     13  *    copyright notice, this list of conditions and the following
     14  *    disclaimer in the documentation and/or other materials provided
     15  *    with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
     18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     20  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS
     21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     27  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /* derived from zaurus/dev/zmci.c */
     32 
     33 /*-
     34  * Copyright (c) 2006-2008 NONAKA Kimihiro <nonaka (at) netbsd.org>
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
     47  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     48  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     49  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS
     50  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     51  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     52  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     53  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     54  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     55  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     56  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  */
     59 
     60 #include <sys/cdefs.h>
     61 __KERNEL_RCSID(0, "$NetBSD: g42xxeb_mci.c,v 1.1.4.2 2010/04/30 14:39:15 uebayasi Exp $");
     62 
     63 #include <sys/param.h>
     64 #include <sys/device.h>
     65 #include <sys/systm.h>
     66 #include <sys/bus.h>
     67 #include <sys/pmf.h>
     68 
     69 #include <machine/intr.h>
     70 
     71 #include <arm/xscale/pxa2x0cpu.h>
     72 #include <arm/xscale/pxa2x0reg.h>
     73 #include <arm/xscale/pxa2x0var.h>
     74 #include <arm/xscale/pxa2x0_gpio.h>
     75 #include <arm/xscale/pxa2x0_mci.h>
     76 
     77 #include <dev/sdmmc/sdmmcreg.h>
     78 
     79 #include <arm/xscale/pxa2x0var.h>
     80 #include <evbarm/g42xxeb/g42xxeb_var.h>
     81 
     82 struct g42xxeb_mci_softc {
     83 	struct pxamci_softc sc_mci;
     84 
     85 	bus_space_tag_t sc_obio_iot;
     86 	bus_space_handle_t sc_obio_ioh;
     87 
     88 	void *sc_detect_ih;
     89 };
     90 
     91 static int pxamci_match(device_t, cfdata_t, void *);
     92 static void pxamci_attach(device_t, device_t, void *);
     93 
     94 CFATTACH_DECL_NEW(pxamci_obio, sizeof(struct g42xxeb_mci_softc),
     95     pxamci_match, pxamci_attach, NULL, NULL);
     96 
     97 static int g42xxeb_mci_intr(void *arg);
     98 
     99 static uint32_t	g42xxeb_mci_get_ocr(void *);
    100 static int g42xxeb_mci_set_power(void *, uint32_t);
    101 static int g42xxeb_mci_card_detect(void *);
    102 static int g42xxeb_mci_write_protect(void *);
    103 
    104 static int
    105 pxamci_match(device_t parent, cfdata_t cf, void *aux)
    106 {
    107 
    108 	if (strcmp(cf->cf_name, "pxamci") == 0)
    109 		return 1;
    110 	return 0;
    111 }
    112 
    113 struct pxa2x0_gpioconf g42xxeb_pxamci_gpioconf[] = {
    114 	{  6, GPIO_CLR | GPIO_ALT_FN_1_OUT },	/* MMCCLK */
    115 	{  8, GPIO_CLR | GPIO_ALT_FN_1_OUT },	/* MMCCS0 */
    116 	{  9, GPIO_CLR | GPIO_ALT_FN_1_OUT },	/* MMCCS1 */
    117 	{  -1 }
    118 };
    119 
    120 
    121 static void
    122 pxamci_attach(device_t parent, device_t self, void *aux)
    123 {
    124 	struct g42xxeb_mci_softc *sc = device_private(self);
    125 	struct obio_attach_args *oba = aux;
    126 	struct obio_softc *osc = device_private(parent);
    127 	struct pxaip_attach_args pxa;
    128 	struct pxa2x0_gpioconf *gpioconf[] = {
    129 		g42xxeb_pxamci_gpioconf,
    130 		NULL
    131 	};
    132 
    133 	bus_space_tag_t iot = oba->oba_iot;
    134 
    135 	sc->sc_mci.sc_dev = self;
    136 
    137 	pxa2x0_gpio_config(gpioconf);
    138 
    139 	/* Establish card detect interrupt */
    140 	sc->sc_detect_ih = obio_intr_establish(osc, G42XXEB_INT_MMCSD,
    141 	    IPL_BIO, IST_EDGE_BOTH, g42xxeb_mci_intr, sc);
    142 	if (sc->sc_detect_ih == NULL) {
    143 		aprint_error_dev(self,
    144 		    "unable to establish SD detect interrupt\n");
    145 		return;
    146 	}
    147 
    148 	sc->sc_mci.sc_tag.cookie = sc;
    149 	sc->sc_mci.sc_tag.get_ocr = g42xxeb_mci_get_ocr;
    150 	sc->sc_mci.sc_tag.set_power = g42xxeb_mci_set_power;
    151 	sc->sc_mci.sc_tag.card_detect = g42xxeb_mci_card_detect;
    152 	sc->sc_mci.sc_tag.write_protect = g42xxeb_mci_write_protect;
    153 	sc->sc_mci.sc_caps = 0;
    154 
    155 	pxa.pxa_iot = iot; 	/* actually, here we want I/O tag for
    156 				   pxaip, not for obio. */
    157 
    158 #if 0
    159 	/* pxamci_attach_sub() ignores following values and uses
    160 	 * constants. */
    161 	pxa.pxa_addr = PXA2X0_MMC_BASE;
    162 	pxa.pxa_size = PXA2X0_MMC_SIZE;
    163 	pxa.pxa_intr = PXA2X0_INT_MMC;
    164 #endif
    165 
    166 	if (pxamci_attach_sub(self, &pxa)) {
    167 		aprint_error_dev(self,
    168 		    "unable to attach MMC controller\n");
    169 		goto free_intr;
    170 	}
    171 
    172 	if (!pmf_device_register(self, NULL, NULL)) {
    173 		aprint_error_dev(self,
    174 		    "couldn't establish power handler\n");
    175 	}
    176 
    177 	sc->sc_obio_iot = iot;
    178 	sc->sc_obio_ioh = osc->sc_obioreg_ioh;
    179 
    180 	return;
    181 
    182 free_intr:
    183 	obio_intr_disestablish(osc, G42XXEB_INT_MMCSD, sc->sc_detect_ih);
    184 	sc->sc_detect_ih = NULL;
    185 }
    186 
    187 static int
    188 g42xxeb_mci_intr(void *arg)
    189 {
    190 	struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
    191 
    192 	pxamci_card_detect_event(&sc->sc_mci);
    193 
    194 	return 1;
    195 }
    196 
    197 static uint32_t
    198 g42xxeb_mci_get_ocr(void *arg)
    199 {
    200 
    201 	return MMC_OCR_3_2V_3_3V;
    202 }
    203 
    204 static int
    205 g42xxeb_mci_set_power(void *arg, uint32_t ocr)
    206 {
    207 	/* nothing to do */
    208 	return 0;
    209 }
    210 
    211 /*
    212  * Return non-zero if the card is currently inserted.
    213  */
    214 static int
    215 g42xxeb_mci_card_detect(void *arg)
    216 {
    217 	struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
    218 	uint16_t reg;
    219 
    220 	reg = bus_space_read_2(sc->sc_obio_iot, sc->sc_obio_ioh,
    221 	    G42XXEB_INTSTS2);
    222 
    223 	return !(reg & (1<<G42XXEB_INT_MMCSD));
    224 }
    225 
    226 /*
    227  * Return non-zero if the card is currently write-protected.
    228  */
    229 static int
    230 g42xxeb_mci_write_protect(void *arg)
    231 {
    232 	struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
    233 	uint16_t reg;
    234 
    235 	reg = bus_space_read_2(sc->sc_obio_iot, sc->sc_obio_ioh,
    236 	    G42XXEB_WP);
    237 
    238 	return reg & 1;
    239 }
    240