Home | History | Annotate | Line # | Download | only in dev
zmci.c revision 1.1.8.1
      1      1.1    nonaka /*	$NetBSD: zmci.c,v 1.1.8.1 2010/04/30 14:40:01 uebayasi Exp $	*/
      2      1.1    nonaka 
      3      1.1    nonaka /*-
      4      1.1    nonaka  * Copyright (c) 2006-2008 NONAKA Kimihiro <nonaka (at) netbsd.org>
      5      1.1    nonaka  * All rights reserved.
      6      1.1    nonaka  *
      7      1.1    nonaka  * Redistribution and use in source and binary forms, with or without
      8      1.1    nonaka  * modification, are permitted provided that the following conditions
      9      1.1    nonaka  * are met:
     10      1.1    nonaka  * 1. Redistributions of source code must retain the above copyright
     11      1.1    nonaka  *    notice, this list of conditions and the following disclaimer.
     12      1.1    nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1    nonaka  *    notice, this list of conditions and the following disclaimer in the
     14      1.1    nonaka  *    documentation and/or other materials provided with the distribution.
     15      1.1    nonaka  *
     16      1.1    nonaka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17      1.1    nonaka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.1    nonaka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.1    nonaka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20      1.1    nonaka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.1    nonaka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.1    nonaka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.1    nonaka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.1    nonaka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1    nonaka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1    nonaka  * SUCH DAMAGE.
     27      1.1    nonaka  */
     28      1.1    nonaka 
     29      1.1    nonaka #include <sys/cdefs.h>
     30      1.1    nonaka __KERNEL_RCSID(0, "$NetBSD: zmci.c,v 1.1.8.1 2010/04/30 14:40:01 uebayasi Exp $");
     31      1.1    nonaka 
     32      1.1    nonaka #include <sys/param.h>
     33      1.1    nonaka #include <sys/device.h>
     34      1.1    nonaka #include <sys/systm.h>
     35      1.1    nonaka #include <sys/bus.h>
     36      1.1    nonaka #include <sys/pmf.h>
     37      1.1    nonaka 
     38      1.1    nonaka #include <machine/intr.h>
     39      1.1    nonaka 
     40      1.1    nonaka #include <arm/xscale/pxa2x0cpu.h>
     41      1.1    nonaka #include <arm/xscale/pxa2x0reg.h>
     42      1.1    nonaka #include <arm/xscale/pxa2x0var.h>
     43      1.1    nonaka #include <arm/xscale/pxa2x0_gpio.h>
     44      1.1    nonaka #include <arm/xscale/pxa2x0_mci.h>
     45      1.1    nonaka 
     46      1.1    nonaka #include <dev/sdmmc/sdmmcreg.h>
     47      1.1    nonaka 
     48      1.1    nonaka #include <zaurus/dev/scoopreg.h>
     49      1.1    nonaka #include <zaurus/dev/scoopvar.h>
     50      1.1    nonaka 
     51      1.1    nonaka #include <zaurus/zaurus/zaurus_reg.h>
     52      1.1    nonaka #include <zaurus/zaurus/zaurus_var.h>
     53      1.1    nonaka 
     54      1.1    nonaka #if defined(PXAMCI_DEBUG)
     55      1.1    nonaka #define	DPRINTF(s)	do { printf s; } while (0)
     56      1.1    nonaka #else
     57      1.1    nonaka #define	DPRINTF(s)	do {} while (0)
     58      1.1    nonaka #endif
     59      1.1    nonaka 
     60      1.1    nonaka struct zmci_softc {
     61      1.1    nonaka 	struct pxamci_softc sc;
     62      1.1    nonaka 
     63      1.1    nonaka 	void *sc_detect_ih;
     64      1.1    nonaka 	int sc_detect_pin;
     65      1.1    nonaka 	int sc_wp_pin;
     66      1.1    nonaka };
     67      1.1    nonaka 
     68      1.1    nonaka static int pxamci_match(device_t, cfdata_t, void *);
     69      1.1    nonaka static void pxamci_attach(device_t, device_t, void *);
     70      1.1    nonaka 
     71      1.1    nonaka CFATTACH_DECL_NEW(zmci, sizeof(struct zmci_softc),
     72      1.1    nonaka     pxamci_match, pxamci_attach, NULL, NULL);
     73      1.1    nonaka 
     74      1.1    nonaka static int zmci_intr(void *arg);
     75      1.1    nonaka 
     76      1.1    nonaka static uint32_t	zmci_get_ocr(void *);
     77      1.1    nonaka static int zmci_set_power(void *, uint32_t);
     78      1.1    nonaka static int zmci_card_detect(void *);
     79      1.1    nonaka static int zmci_write_protect(void *);
     80      1.1    nonaka 
     81      1.1    nonaka static int
     82      1.1    nonaka pxamci_match(device_t parent, cfdata_t cf, void *aux)
     83      1.1    nonaka {
     84      1.1    nonaka 
     85      1.1    nonaka 	if (!ZAURUS_ISC3000)
     86      1.1    nonaka 		return 0;
     87      1.1    nonaka 	return 1;
     88      1.1    nonaka }
     89      1.1    nonaka 
     90      1.1    nonaka static void
     91      1.1    nonaka pxamci_attach(device_t parent, device_t self, void *aux)
     92      1.1    nonaka {
     93      1.1    nonaka 	struct zmci_softc *sc = device_private(self);
     94      1.1    nonaka 	struct pxaip_attach_args *pxa = aux;
     95      1.1    nonaka 
     96      1.1    nonaka 	sc->sc.sc_dev = self;
     97      1.1    nonaka 
     98      1.1    nonaka 	if (ZAURUS_ISC3000) {
     99      1.1    nonaka 		sc->sc_detect_pin = C3000_GPIO_SD_DETECT_PIN;
    100      1.1    nonaka 		sc->sc_wp_pin = C3000_GPIO_SD_WP_PIN;
    101      1.1    nonaka 		pxa2x0_gpio_set_function(sc->sc_detect_pin, GPIO_IN);
    102      1.1    nonaka 		pxa2x0_gpio_set_function(sc->sc_wp_pin, GPIO_IN);
    103      1.1    nonaka 	} else {
    104      1.1    nonaka 		/* XXX: C7x0/C8x0 */
    105      1.1    nonaka 		return;
    106      1.1    nonaka 	}
    107      1.1    nonaka 
    108      1.1    nonaka 	/* Establish SD detect interrupt */
    109      1.1    nonaka 	sc->sc_detect_ih = pxa2x0_gpio_intr_establish(sc->sc_detect_pin,
    110      1.1    nonaka 	    IST_EDGE_BOTH, IPL_BIO, zmci_intr, sc);
    111      1.1    nonaka 	if (sc->sc_detect_ih == NULL) {
    112      1.1    nonaka 		aprint_error_dev(self,
    113      1.1    nonaka 		    "unable to establish nSD_DETECT interrupt\n");
    114      1.1    nonaka 		return;
    115      1.1    nonaka 	}
    116      1.1    nonaka 
    117      1.1    nonaka 	sc->sc.sc_tag.cookie = sc;
    118      1.1    nonaka 	sc->sc.sc_tag.get_ocr = zmci_get_ocr;
    119      1.1    nonaka 	sc->sc.sc_tag.set_power = zmci_set_power;
    120      1.1    nonaka 	sc->sc.sc_tag.card_detect = zmci_card_detect;
    121      1.1    nonaka 	sc->sc.sc_tag.write_protect = zmci_write_protect;
    122  1.1.8.1  uebayasi 	sc->sc.sc_caps = PMC_CAPS_4BIT;
    123      1.1    nonaka 
    124      1.1    nonaka 	if (pxamci_attach_sub(self, pxa)) {
    125      1.1    nonaka 		aprint_error_dev(self, "unable to attach MMC controller\n");
    126      1.1    nonaka 		goto free_intr;
    127      1.1    nonaka 	}
    128      1.1    nonaka 
    129      1.1    nonaka 	if (!pmf_device_register(self, NULL, NULL)) {
    130      1.1    nonaka 		aprint_error_dev(self, "couldn't establish power handler\n");
    131      1.1    nonaka 	}
    132      1.1    nonaka 
    133      1.1    nonaka 	return;
    134      1.1    nonaka 
    135      1.1    nonaka free_intr:
    136      1.1    nonaka 	pxa2x0_gpio_intr_disestablish(sc->sc_detect_ih);
    137      1.1    nonaka 	sc->sc_detect_ih = NULL;
    138      1.1    nonaka }
    139      1.1    nonaka 
    140      1.1    nonaka static int
    141      1.1    nonaka zmci_intr(void *arg)
    142      1.1    nonaka {
    143      1.1    nonaka 	struct zmci_softc *sc = (struct zmci_softc *)arg;
    144      1.1    nonaka 
    145      1.1    nonaka 	pxa2x0_gpio_clear_intr(sc->sc_detect_pin);
    146      1.1    nonaka 
    147      1.1    nonaka 	pxamci_card_detect_event(&sc->sc);
    148      1.1    nonaka 
    149      1.1    nonaka 	return 1;
    150      1.1    nonaka }
    151      1.1    nonaka 
    152      1.1    nonaka static uint32_t
    153      1.1    nonaka zmci_get_ocr(void *arg)
    154      1.1    nonaka {
    155      1.1    nonaka 
    156      1.1    nonaka 	return MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V;
    157      1.1    nonaka }
    158      1.1    nonaka 
    159      1.1    nonaka static int
    160      1.1    nonaka zmci_set_power(void *arg, uint32_t ocr)
    161      1.1    nonaka {
    162      1.1    nonaka 	struct zmci_softc *sc = (struct zmci_softc *)arg;
    163      1.1    nonaka 
    164      1.1    nonaka 	if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
    165      1.1    nonaka 		scoop_set_sdmmc_power(1);
    166      1.1    nonaka 		return 0;
    167      1.1    nonaka 	}
    168      1.1    nonaka 
    169      1.1    nonaka 	if (ocr != 0) {
    170      1.1    nonaka 		printf("%s: zmci_set_power(): unsupported OCR (%#x)\n",
    171      1.1    nonaka 		    device_xname(sc->sc.sc_dev), ocr);
    172      1.1    nonaka 		return EINVAL;
    173      1.1    nonaka 	}
    174      1.1    nonaka 
    175      1.1    nonaka 	/* power off */
    176      1.1    nonaka 	scoop_set_sdmmc_power(0);
    177      1.1    nonaka 	return 0;
    178      1.1    nonaka }
    179      1.1    nonaka 
    180      1.1    nonaka /*
    181      1.1    nonaka  * Return non-zero if the card is currently inserted.
    182      1.1    nonaka  */
    183      1.1    nonaka static int
    184      1.1    nonaka zmci_card_detect(void *arg)
    185      1.1    nonaka {
    186      1.1    nonaka 	struct zmci_softc *sc = (struct zmci_softc *)arg;
    187      1.1    nonaka 
    188      1.1    nonaka 	if (!pxa2x0_gpio_get_bit(sc->sc_detect_pin))
    189      1.1    nonaka 		return 1;
    190      1.1    nonaka 	return 0;
    191      1.1    nonaka }
    192      1.1    nonaka 
    193      1.1    nonaka /*
    194      1.1    nonaka  * Return non-zero if the card is currently write-protected.
    195      1.1    nonaka  */
    196      1.1    nonaka static int
    197      1.1    nonaka zmci_write_protect(void *arg)
    198      1.1    nonaka {
    199      1.1    nonaka 	struct zmci_softc *sc = (struct zmci_softc *)arg;
    200      1.1    nonaka 
    201      1.1    nonaka 	if (pxa2x0_gpio_get_bit(sc->sc_wp_pin))
    202      1.1    nonaka 		return 1;
    203      1.1    nonaka 	return 0;
    204      1.1    nonaka }
    205