xref: /netbsd-src/sys/arch/hpcarm/dev/wzero3_mci.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: wzero3_mci.c,v 1.2 2010/05/23 11:53:18 nonaka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009 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: wzero3_mci.c,v 1.2 2010/05/23 11:53:18 nonaka Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35 #include <sys/pmf.h>
36 
37 #include <machine/bus.h>
38 #include <machine/bootinfo.h>
39 #include <machine/platid.h>
40 #include <machine/platid_mask.h>
41 
42 #include <arm/xscale/pxa2x0cpu.h>
43 #include <arm/xscale/pxa2x0reg.h>
44 #include <arm/xscale/pxa2x0var.h>
45 #include <arm/xscale/pxa2x0_gpio.h>
46 #include <arm/xscale/pxa2x0_mci.h>
47 
48 #include <dev/sdmmc/sdmmcreg.h>
49 
50 #include <hpcarm/dev/wzero3_reg.h>
51 
52 #if defined(PXAMCI_DEBUG)
53 #define	DPRINTF(s)	do { printf s; } while (0)
54 #else
55 #define	DPRINTF(s)	do {} while (0)
56 #endif
57 
58 struct wzero3mci_softc {
59 	struct pxamci_softc sc;
60 
61 	void *sc_detect_ih;
62 	int sc_detect_pin;
63 	int sc_power_pin;
64 };
65 
66 static int pxamci_match(device_t, cfdata_t, void *);
67 static void pxamci_attach(device_t, device_t, void *);
68 
69 CFATTACH_DECL_NEW(wzero3mci, sizeof(struct wzero3mci_softc),
70     pxamci_match, pxamci_attach, NULL, NULL);
71 
72 static int wzero3mci_intr(void *arg);
73 
74 static uint32_t	wzero3mci_get_ocr(void *);
75 static int wzero3mci_set_power(void *, uint32_t);
76 static int wzero3mci_card_detect(void *);
77 static int wzero3mci_write_protect(void *);
78 
79 struct wzero3mci_model {
80 	platid_mask_t *platid;
81 	int detect_pin;	/* card detect pin# */
82 	int power_pin;	/* card power pin # */
83 } wzero3mci_table[] = {
84 	/* WS003SH */
85 	{
86 		&platid_mask_MACH_SHARP_WZERO3_WS003SH,
87 		GPIO_WS003SH_SD_DETECT,
88 		GPIO_WS003SH_SD_POWER,
89 	},
90 	/* WS004SH */
91 	{
92 		&platid_mask_MACH_SHARP_WZERO3_WS004SH,
93 		GPIO_WS003SH_SD_DETECT,
94 		GPIO_WS003SH_SD_POWER,
95 	},
96 	/* WS007SH */
97 	{
98 		&platid_mask_MACH_SHARP_WZERO3_WS007SH,
99 		GPIO_WS007SH_SD_DETECT,
100 		GPIO_WS007SH_SD_POWER,
101 	},
102 	/* WS011SH */
103 	{
104 		&platid_mask_MACH_SHARP_WZERO3_WS011SH,
105 		GPIO_WS011SH_SD_DETECT,
106 		GPIO_WS011SH_SD_POWER,
107 	},
108 	/* WS0020H */
109 	{
110 		&platid_mask_MACH_SHARP_WZERO3_WS020SH,
111 		GPIO_WS020SH_SD_DETECT,
112 		GPIO_WS020SH_SD_POWER,
113 	},
114 
115 	{
116 		NULL, -1, -1
117 	},
118 };
119 
120 
121 static const struct wzero3mci_model *
122 wzero3mci_lookup(void)
123 {
124 	const struct wzero3mci_model *model;
125 
126 	for (model = wzero3mci_table; model->platid != NULL; model++) {
127 		if (platid_match(&platid, model->platid)) {
128 			return model;
129 		}
130 	}
131 	return NULL;
132 }
133 
134 static int
135 pxamci_match(device_t parent, cfdata_t cf, void *aux)
136 {
137 
138 	if (strcmp(cf->cf_name, "pxamci") != 0)
139 		return 0;
140 	if (wzero3mci_lookup() == NULL)
141 		return 0;
142 	return 1;
143 }
144 
145 static void
146 pxamci_attach(device_t parent, device_t self, void *aux)
147 {
148 	struct wzero3mci_softc *sc = device_private(self);
149 	struct pxaip_attach_args *pxa = aux;
150 	const struct wzero3mci_model *model;
151 
152 	sc->sc.sc_dev = self;
153 
154 	model = wzero3mci_lookup();
155 	if (model == NULL) {
156 		aprint_error(": Unknown model.");
157 		return;
158 	}
159 
160 	sc->sc_detect_pin = model->detect_pin;
161 	sc->sc_power_pin = model->power_pin;
162 
163 	/* Establish SD detect interrupt */
164 	if (sc->sc_detect_pin >= 0) {
165 		pxa2x0_gpio_set_function(sc->sc_detect_pin, GPIO_IN);
166 		sc->sc_detect_ih = pxa2x0_gpio_intr_establish(sc->sc_detect_pin,
167 		    IST_EDGE_BOTH, IPL_BIO, wzero3mci_intr, sc);
168 		if (sc->sc_detect_ih == NULL) {
169 			aprint_error_dev(self,
170 			    "unable to establish card detect interrupt\n");
171 			return;
172 		}
173 	}
174 
175 	sc->sc.sc_tag.cookie = sc;
176 	sc->sc.sc_tag.get_ocr = wzero3mci_get_ocr;
177 	sc->sc.sc_tag.set_power = wzero3mci_set_power;
178 	sc->sc.sc_tag.card_detect = wzero3mci_card_detect;
179 	sc->sc.sc_tag.write_protect = wzero3mci_write_protect;
180 	sc->sc.sc_caps = PMC_CAPS_4BIT;
181 
182 	if (pxamci_attach_sub(self, pxa)) {
183 		aprint_error_dev(self, "unable to attach MMC controller\n");
184 		goto free_intr;
185 	}
186 
187 	if (!pmf_device_register(self, NULL, NULL)) {
188 		aprint_error_dev(self, "couldn't establish power handler\n");
189 	}
190 
191 	return;
192 
193 free_intr:
194 	pxa2x0_gpio_intr_disestablish(sc->sc_detect_ih);
195 	sc->sc_detect_ih = NULL;
196 }
197 
198 static int
199 wzero3mci_intr(void *arg)
200 {
201 	struct wzero3mci_softc *sc = (struct wzero3mci_softc *)arg;
202 
203 	pxa2x0_gpio_clear_intr(sc->sc_detect_pin);
204 
205 	pxamci_card_detect_event(&sc->sc);
206 
207 	return 1;
208 }
209 
210 /*ARGSUSED*/
211 static uint32_t
212 wzero3mci_get_ocr(void *arg)
213 {
214 
215 	return MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V;
216 }
217 
218 static int
219 wzero3mci_set_power(void *arg, uint32_t ocr)
220 {
221 	struct wzero3mci_softc *sc = (struct wzero3mci_softc *)arg;
222 	int error = 0;
223 
224 	if (sc->sc_power_pin >= 0) {
225 		if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
226 			/* power on */
227 			pxa2x0_gpio_set_bit(sc->sc_power_pin);
228 		} else if (ocr == 0) {
229 			/* power off */
230 			pxa2x0_gpio_clear_bit(sc->sc_power_pin);
231 		} else {
232 			aprint_error_dev(sc->sc.sc_dev,
233 			    "unsupported OCR (%#x)\n", ocr);
234 			error = EINVAL;
235 		}
236 	}
237 	return error;
238 }
239 
240 /*
241  * Return non-zero if the card is currently inserted.
242  */
243 static int
244 wzero3mci_card_detect(void *arg)
245 {
246 	struct wzero3mci_softc *sc = (struct wzero3mci_softc *)arg;
247 
248 	if (sc->sc_detect_pin >= 0) {
249 		if (pxa2x0_gpio_get_bit(sc->sc_detect_pin))
250 			return 0;
251 	}
252 	return 1;
253 }
254 
255 /*
256  * Return non-zero if the card is currently write-protected.
257  */
258 /*ARGSUSED*/
259 static int
260 wzero3mci_write_protect(void *arg)
261 {
262 
263 	return 0;
264 }
265