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