Home | History | Annotate | Line # | Download | only in g42xxeb
g42xxeb_mci.c revision 1.2
      1 /*	$NetBSD: g42xxeb_mci.c,v 1.2 2010/06/26 07:51:45 bsh 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.2 2010/06/26 07:51:45 bsh 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 	/* disable DMA for sdmmc for now. */
    167 	SET(sc->sc_mci.sc_caps, PMC_CAPS_NO_DMA);
    168 
    169 	if (pxamci_attach_sub(self, &pxa)) {
    170 		aprint_error_dev(self,
    171 		    "unable to attach MMC controller\n");
    172 		goto free_intr;
    173 	}
    174 
    175 	if (!pmf_device_register(self, NULL, NULL)) {
    176 		aprint_error_dev(self,
    177 		    "couldn't establish power handler\n");
    178 	}
    179 
    180 	sc->sc_obio_iot = iot;
    181 	sc->sc_obio_ioh = osc->sc_obioreg_ioh;
    182 
    183 	return;
    184 
    185 free_intr:
    186 	obio_intr_disestablish(osc, G42XXEB_INT_MMCSD, sc->sc_detect_ih);
    187 	sc->sc_detect_ih = NULL;
    188 }
    189 
    190 static int
    191 g42xxeb_mci_intr(void *arg)
    192 {
    193 	struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
    194 
    195 	pxamci_card_detect_event(&sc->sc_mci);
    196 
    197 	return 1;
    198 }
    199 
    200 static uint32_t
    201 g42xxeb_mci_get_ocr(void *arg)
    202 {
    203 
    204 	return MMC_OCR_3_2V_3_3V;
    205 }
    206 
    207 static int
    208 g42xxeb_mci_set_power(void *arg, uint32_t ocr)
    209 {
    210 	/* nothing to do */
    211 	return 0;
    212 }
    213 
    214 /*
    215  * Return non-zero if the card is currently inserted.
    216  */
    217 static int
    218 g42xxeb_mci_card_detect(void *arg)
    219 {
    220 	struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
    221 	uint16_t reg;
    222 
    223 	reg = bus_space_read_2(sc->sc_obio_iot, sc->sc_obio_ioh,
    224 	    G42XXEB_INTSTS2);
    225 
    226 	return !(reg & (1<<G42XXEB_INT_MMCSD));
    227 }
    228 
    229 /*
    230  * Return non-zero if the card is currently write-protected.
    231  */
    232 static int
    233 g42xxeb_mci_write_protect(void *arg)
    234 {
    235 	struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
    236 	uint16_t reg;
    237 
    238 	reg = bus_space_read_2(sc->sc_obio_iot, sc->sc_obio_ioh,
    239 	    G42XXEB_WP);
    240 
    241 	return reg & 1;
    242 }
    243