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