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