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