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