No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / zaurus / dev / zmci.c
blobc69b7216c02c2e5d29fb6cad7e4f309e1e0b4740
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 2006-2008 NONAKA Kimihiro <nonaka@netbsd.org>
5 * All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD$");
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>
38 #include <machine/intr.h>
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>
46 #include <dev/sdmmc/sdmmcreg.h>
48 #include <zaurus/dev/scoopreg.h>
49 #include <zaurus/dev/scoopvar.h>
51 #include <zaurus/zaurus/zaurus_reg.h>
52 #include <zaurus/zaurus/zaurus_var.h>
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
60 struct zmci_softc {
61 struct pxamci_softc sc;
63 void *sc_detect_ih;
64 int sc_detect_pin;
65 int sc_wp_pin;
68 static int pxamci_match(device_t, cfdata_t, void *);
69 static void pxamci_attach(device_t, device_t, void *);
71 CFATTACH_DECL_NEW(zmci, sizeof(struct zmci_softc),
72 pxamci_match, pxamci_attach, NULL, NULL);
74 static int zmci_intr(void *arg);
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 *);
81 static int
82 pxamci_match(device_t parent, cfdata_t cf, void *aux)
85 if (!ZAURUS_ISC3000)
86 return 0;
87 return 1;
90 static void
91 pxamci_attach(device_t parent, device_t self, void *aux)
93 struct zmci_softc *sc = device_private(self);
94 struct pxaip_attach_args *pxa = aux;
96 sc->sc.sc_dev = self;
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;
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;
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 = 0;
124 if (pxamci_attach_sub(self, pxa)) {
125 aprint_error_dev(self, "unable to attach MMC controller\n");
126 goto free_intr;
129 if (!pmf_device_register(self, NULL, NULL)) {
130 aprint_error_dev(self, "couldn't establish power handler\n");
133 return;
135 free_intr:
136 pxa2x0_gpio_intr_disestablish(sc->sc_detect_ih);
137 sc->sc_detect_ih = NULL;
140 static int
141 zmci_intr(void *arg)
143 struct zmci_softc *sc = (struct zmci_softc *)arg;
145 pxa2x0_gpio_clear_intr(sc->sc_detect_pin);
147 pxamci_card_detect_event(&sc->sc);
149 return 1;
152 static uint32_t
153 zmci_get_ocr(void *arg)
156 return MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V;
159 static int
160 zmci_set_power(void *arg, uint32_t ocr)
162 struct zmci_softc *sc = (struct zmci_softc *)arg;
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;
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;
175 /* power off */
176 scoop_set_sdmmc_power(0);
177 return 0;
181 * Return non-zero if the card is currently inserted.
183 static int
184 zmci_card_detect(void *arg)
186 struct zmci_softc *sc = (struct zmci_softc *)arg;
188 if (!pxa2x0_gpio_get_bit(sc->sc_detect_pin))
189 return 1;
190 return 0;
194 * Return non-zero if the card is currently write-protected.
196 static int
197 zmci_write_protect(void *arg)
199 struct zmci_softc *sc = (struct zmci_softc *)arg;
201 if (pxa2x0_gpio_get_bit(sc->sc_wp_pin))
202 return 1;
203 return 0;