Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: wzero3_mci.c,v 1.4 2012/01/21 19:44:29 nonaka Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
      5  * All rights reserved.
      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 copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: wzero3_mci.c,v 1.4 2012/01/21 19:44:29 nonaka Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/device.h>
     33 #include <sys/systm.h>
     34 #include <sys/pmf.h>
     35 #include <sys/bus.h>
     36 
     37 #include <machine/bootinfo.h>
     38 #include <machine/platid.h>
     39 #include <machine/platid_mask.h>
     40 
     41 #include <arm/xscale/pxa2x0cpu.h>
     42 #include <arm/xscale/pxa2x0reg.h>
     43 #include <arm/xscale/pxa2x0var.h>
     44 #include <arm/xscale/pxa2x0_gpio.h>
     45 #include <arm/xscale/pxa2x0_mci.h>
     46 
     47 #include <dev/sdmmc/sdmmcreg.h>
     48 
     49 #include <hpcarm/dev/wzero3_reg.h>
     50 
     51 #if defined(PXAMCI_DEBUG)
     52 #define	DPRINTF(s)	do { printf s; } while (0)
     53 #else
     54 #define	DPRINTF(s)	do {} while (0)
     55 #endif
     56 
     57 struct wzero3mci_softc {
     58 	struct pxamci_softc sc;
     59 
     60 	void *sc_detect_ih;
     61 	int sc_detect_pin;
     62 	int sc_power_pin;
     63 };
     64 
     65 static int pxamci_match(device_t, cfdata_t, void *);
     66 static void pxamci_attach(device_t, device_t, void *);
     67 
     68 CFATTACH_DECL_NEW(wzero3mci, sizeof(struct wzero3mci_softc),
     69     pxamci_match, pxamci_attach, NULL, NULL);
     70 
     71 static int wzero3mci_intr(void *arg);
     72 
     73 static uint32_t	wzero3mci_get_ocr(void *);
     74 static int wzero3mci_set_power(void *, uint32_t);
     75 static int wzero3mci_card_detect(void *);
     76 static int wzero3mci_write_protect(void *);
     77 
     78 struct wzero3mci_model {
     79 	platid_mask_t *platid;
     80 	int detect_pin;	/* card detect pin# */
     81 	int power_pin;	/* card power pin # */
     82 } wzero3mci_table[] = {
     83 	/* WS003SH */
     84 	{
     85 		&platid_mask_MACH_SHARP_WZERO3_WS003SH,
     86 		GPIO_WS003SH_SD_DETECT,
     87 		GPIO_WS003SH_SD_POWER,
     88 	},
     89 	/* WS004SH */
     90 	{
     91 		&platid_mask_MACH_SHARP_WZERO3_WS004SH,
     92 		GPIO_WS003SH_SD_DETECT,
     93 		GPIO_WS003SH_SD_POWER,
     94 	},
     95 	/* WS007SH */
     96 	{
     97 		&platid_mask_MACH_SHARP_WZERO3_WS007SH,
     98 		GPIO_WS007SH_SD_DETECT,
     99 		GPIO_WS007SH_SD_POWER,
    100 	},
    101 	/* WS011SH */
    102 	{
    103 		&platid_mask_MACH_SHARP_WZERO3_WS011SH,
    104 		GPIO_WS011SH_SD_DETECT,
    105 		GPIO_WS011SH_SD_POWER,
    106 	},
    107 	/* WS0020H */
    108 	{
    109 		&platid_mask_MACH_SHARP_WZERO3_WS020SH,
    110 		GPIO_WS020SH_SD_DETECT,
    111 		GPIO_WS020SH_SD_POWER,
    112 	},
    113 
    114 	{
    115 		NULL, -1, -1
    116 	},
    117 };
    118 
    119 
    120 static const struct wzero3mci_model *
    121 wzero3mci_lookup(void)
    122 {
    123 	const struct wzero3mci_model *model;
    124 
    125 	for (model = wzero3mci_table; model->platid != NULL; model++) {
    126 		if (platid_match(&platid, model->platid)) {
    127 			return model;
    128 		}
    129 	}
    130 	return NULL;
    131 }
    132 
    133 static int
    134 pxamci_match(device_t parent, cfdata_t cf, void *aux)
    135 {
    136 
    137 	if (strcmp(cf->cf_name, "pxamci") != 0)
    138 		return 0;
    139 	if (wzero3mci_lookup() == NULL)
    140 		return 0;
    141 	return 1;
    142 }
    143 
    144 static void
    145 pxamci_attach(device_t parent, device_t self, void *aux)
    146 {
    147 	struct wzero3mci_softc *sc = device_private(self);
    148 	struct pxaip_attach_args *pxa = aux;
    149 	const struct wzero3mci_model *model;
    150 
    151 	sc->sc.sc_dev = self;
    152 
    153 	model = wzero3mci_lookup();
    154 	if (model == NULL) {
    155 		aprint_error(": Unknown model.");
    156 		return;
    157 	}
    158 
    159 	sc->sc_detect_pin = model->detect_pin;
    160 	sc->sc_power_pin = model->power_pin;
    161 
    162 	/* Establish SD detect interrupt */
    163 	if (sc->sc_detect_pin >= 0) {
    164 		pxa2x0_gpio_set_function(sc->sc_detect_pin, GPIO_IN);
    165 		sc->sc_detect_ih = pxa2x0_gpio_intr_establish(sc->sc_detect_pin,
    166 		    IST_EDGE_BOTH, IPL_BIO, wzero3mci_intr, sc);
    167 		if (sc->sc_detect_ih == NULL) {
    168 			aprint_error_dev(self,
    169 			    "unable to establish card detect interrupt\n");
    170 			return;
    171 		}
    172 	}
    173 
    174 	sc->sc.sc_tag.cookie = sc;
    175 	sc->sc.sc_tag.get_ocr = wzero3mci_get_ocr;
    176 	sc->sc.sc_tag.set_power = wzero3mci_set_power;
    177 	sc->sc.sc_tag.card_detect = wzero3mci_card_detect;
    178 	sc->sc.sc_tag.write_protect = wzero3mci_write_protect;
    179 	sc->sc.sc_caps = PMC_CAPS_4BIT;
    180 
    181 	if (pxamci_attach_sub(self, pxa)) {
    182 		aprint_error_dev(self, "unable to attach MMC controller\n");
    183 		goto free_intr;
    184 	}
    185 
    186 	if (!pmf_device_register(self, NULL, NULL)) {
    187 		aprint_error_dev(self, "couldn't establish power handler\n");
    188 	}
    189 
    190 	return;
    191 
    192 free_intr:
    193 	pxa2x0_gpio_intr_disestablish(sc->sc_detect_ih);
    194 	sc->sc_detect_ih = NULL;
    195 }
    196 
    197 static int
    198 wzero3mci_intr(void *arg)
    199 {
    200 	struct wzero3mci_softc *sc = (struct wzero3mci_softc *)arg;
    201 
    202 	pxa2x0_gpio_clear_intr(sc->sc_detect_pin);
    203 
    204 	pxamci_card_detect_event(&sc->sc);
    205 
    206 	return 1;
    207 }
    208 
    209 /*ARGSUSED*/
    210 static uint32_t
    211 wzero3mci_get_ocr(void *arg)
    212 {
    213 
    214 	return MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V;
    215 }
    216 
    217 static int
    218 wzero3mci_set_power(void *arg, uint32_t ocr)
    219 {
    220 	struct wzero3mci_softc *sc = (struct wzero3mci_softc *)arg;
    221 	int error = 0;
    222 
    223 	if (sc->sc_power_pin >= 0) {
    224 		if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
    225 			/* power on */
    226 			pxa2x0_gpio_set_bit(sc->sc_power_pin);
    227 		} else if (ocr == 0) {
    228 			/* power off */
    229 			pxa2x0_gpio_clear_bit(sc->sc_power_pin);
    230 		} else {
    231 			aprint_error_dev(sc->sc.sc_dev,
    232 			    "unsupported OCR (%#x)\n", ocr);
    233 			error = EINVAL;
    234 		}
    235 	}
    236 	return error;
    237 }
    238 
    239 /*
    240  * Return non-zero if the card is currently inserted.
    241  */
    242 static int
    243 wzero3mci_card_detect(void *arg)
    244 {
    245 	struct wzero3mci_softc *sc = (struct wzero3mci_softc *)arg;
    246 
    247 	if (sc->sc_detect_pin >= 0) {
    248 		if (pxa2x0_gpio_get_bit(sc->sc_detect_pin))
    249 			return 0;
    250 	}
    251 	return 1;
    252 }
    253 
    254 /*
    255  * Return non-zero if the card is currently write-protected.
    256  */
    257 /*ARGSUSED*/
    258 static int
    259 wzero3mci_write_protect(void *arg)
    260 {
    261 
    262 	return 0;
    263 }
    264