g42xxeb_mci.c revision 1.2.2.2 1 1.2.2.2 yamt /* $NetBSD: g42xxeb_mci.c,v 1.2.2.2 2010/08/11 22:51:51 yamt Exp $ */
2 1.2.2.2 yamt
3 1.2.2.2 yamt /*-
4 1.2.2.2 yamt * Copyright (c) 2009 Genetec Corporation. All rights reserved.
5 1.2.2.2 yamt * Written by Hiroyuki Bessho for Genetec Corporation.
6 1.2.2.2 yamt *
7 1.2.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 yamt * modification, are permitted provided that the following conditions
9 1.2.2.2 yamt * are met:
10 1.2.2.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 yamt * 2. Redistributions in binary form must reproduce the above
13 1.2.2.2 yamt * copyright notice, this list of conditions and the following
14 1.2.2.2 yamt * disclaimer in the documentation and/or other materials provided
15 1.2.2.2 yamt * with the distribution.
16 1.2.2.2 yamt *
17 1.2.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 1.2.2.2 yamt * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.2.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 1.2.2.2 yamt * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS
21 1.2.2.2 yamt * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 1.2.2.2 yamt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 1.2.2.2 yamt * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 1.2.2.2 yamt * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 1.2.2.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.2.2.2 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 1.2.2.2 yamt * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.2.2.2 yamt * SUCH DAMAGE.
29 1.2.2.2 yamt */
30 1.2.2.2 yamt
31 1.2.2.2 yamt /* derived from zaurus/dev/zmci.c */
32 1.2.2.2 yamt
33 1.2.2.2 yamt /*-
34 1.2.2.2 yamt * Copyright (c) 2006-2008 NONAKA Kimihiro <nonaka (at) netbsd.org>
35 1.2.2.2 yamt * All rights reserved.
36 1.2.2.2 yamt *
37 1.2.2.2 yamt * Redistribution and use in source and binary forms, with or without
38 1.2.2.2 yamt * modification, are permitted provided that the following conditions
39 1.2.2.2 yamt * are met:
40 1.2.2.2 yamt * 1. Redistributions of source code must retain the above copyright
41 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer.
42 1.2.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
43 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer in the
44 1.2.2.2 yamt * documentation and/or other materials provided with the distribution.
45 1.2.2.2 yamt *
46 1.2.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
47 1.2.2.2 yamt * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 1.2.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
49 1.2.2.2 yamt * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS
50 1.2.2.2 yamt * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 1.2.2.2 yamt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 1.2.2.2 yamt * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
53 1.2.2.2 yamt * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
54 1.2.2.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 1.2.2.2 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
56 1.2.2.2 yamt * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 1.2.2.2 yamt * SUCH DAMAGE.
58 1.2.2.2 yamt */
59 1.2.2.2 yamt
60 1.2.2.2 yamt #include <sys/cdefs.h>
61 1.2.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: g42xxeb_mci.c,v 1.2.2.2 2010/08/11 22:51:51 yamt Exp $");
62 1.2.2.2 yamt
63 1.2.2.2 yamt #include <sys/param.h>
64 1.2.2.2 yamt #include <sys/device.h>
65 1.2.2.2 yamt #include <sys/systm.h>
66 1.2.2.2 yamt #include <sys/bus.h>
67 1.2.2.2 yamt #include <sys/pmf.h>
68 1.2.2.2 yamt
69 1.2.2.2 yamt #include <machine/intr.h>
70 1.2.2.2 yamt
71 1.2.2.2 yamt #include <arm/xscale/pxa2x0cpu.h>
72 1.2.2.2 yamt #include <arm/xscale/pxa2x0reg.h>
73 1.2.2.2 yamt #include <arm/xscale/pxa2x0var.h>
74 1.2.2.2 yamt #include <arm/xscale/pxa2x0_gpio.h>
75 1.2.2.2 yamt #include <arm/xscale/pxa2x0_mci.h>
76 1.2.2.2 yamt
77 1.2.2.2 yamt #include <dev/sdmmc/sdmmcreg.h>
78 1.2.2.2 yamt
79 1.2.2.2 yamt #include <arm/xscale/pxa2x0var.h>
80 1.2.2.2 yamt #include <evbarm/g42xxeb/g42xxeb_var.h>
81 1.2.2.2 yamt
82 1.2.2.2 yamt struct g42xxeb_mci_softc {
83 1.2.2.2 yamt struct pxamci_softc sc_mci;
84 1.2.2.2 yamt
85 1.2.2.2 yamt bus_space_tag_t sc_obio_iot;
86 1.2.2.2 yamt bus_space_handle_t sc_obio_ioh;
87 1.2.2.2 yamt
88 1.2.2.2 yamt void *sc_detect_ih;
89 1.2.2.2 yamt };
90 1.2.2.2 yamt
91 1.2.2.2 yamt static int pxamci_match(device_t, cfdata_t, void *);
92 1.2.2.2 yamt static void pxamci_attach(device_t, device_t, void *);
93 1.2.2.2 yamt
94 1.2.2.2 yamt CFATTACH_DECL_NEW(pxamci_obio, sizeof(struct g42xxeb_mci_softc),
95 1.2.2.2 yamt pxamci_match, pxamci_attach, NULL, NULL);
96 1.2.2.2 yamt
97 1.2.2.2 yamt static int g42xxeb_mci_intr(void *arg);
98 1.2.2.2 yamt
99 1.2.2.2 yamt static uint32_t g42xxeb_mci_get_ocr(void *);
100 1.2.2.2 yamt static int g42xxeb_mci_set_power(void *, uint32_t);
101 1.2.2.2 yamt static int g42xxeb_mci_card_detect(void *);
102 1.2.2.2 yamt static int g42xxeb_mci_write_protect(void *);
103 1.2.2.2 yamt
104 1.2.2.2 yamt static int
105 1.2.2.2 yamt pxamci_match(device_t parent, cfdata_t cf, void *aux)
106 1.2.2.2 yamt {
107 1.2.2.2 yamt
108 1.2.2.2 yamt if (strcmp(cf->cf_name, "pxamci") == 0)
109 1.2.2.2 yamt return 1;
110 1.2.2.2 yamt return 0;
111 1.2.2.2 yamt }
112 1.2.2.2 yamt
113 1.2.2.2 yamt struct pxa2x0_gpioconf g42xxeb_pxamci_gpioconf[] = {
114 1.2.2.2 yamt { 6, GPIO_CLR | GPIO_ALT_FN_1_OUT }, /* MMCCLK */
115 1.2.2.2 yamt { 8, GPIO_CLR | GPIO_ALT_FN_1_OUT }, /* MMCCS0 */
116 1.2.2.2 yamt { 9, GPIO_CLR | GPIO_ALT_FN_1_OUT }, /* MMCCS1 */
117 1.2.2.2 yamt { -1 }
118 1.2.2.2 yamt };
119 1.2.2.2 yamt
120 1.2.2.2 yamt
121 1.2.2.2 yamt static void
122 1.2.2.2 yamt pxamci_attach(device_t parent, device_t self, void *aux)
123 1.2.2.2 yamt {
124 1.2.2.2 yamt struct g42xxeb_mci_softc *sc = device_private(self);
125 1.2.2.2 yamt struct obio_attach_args *oba = aux;
126 1.2.2.2 yamt struct obio_softc *osc = device_private(parent);
127 1.2.2.2 yamt struct pxaip_attach_args pxa;
128 1.2.2.2 yamt struct pxa2x0_gpioconf *gpioconf[] = {
129 1.2.2.2 yamt g42xxeb_pxamci_gpioconf,
130 1.2.2.2 yamt NULL
131 1.2.2.2 yamt };
132 1.2.2.2 yamt
133 1.2.2.2 yamt bus_space_tag_t iot = oba->oba_iot;
134 1.2.2.2 yamt
135 1.2.2.2 yamt sc->sc_mci.sc_dev = self;
136 1.2.2.2 yamt
137 1.2.2.2 yamt pxa2x0_gpio_config(gpioconf);
138 1.2.2.2 yamt
139 1.2.2.2 yamt /* Establish card detect interrupt */
140 1.2.2.2 yamt sc->sc_detect_ih = obio_intr_establish(osc, G42XXEB_INT_MMCSD,
141 1.2.2.2 yamt IPL_BIO, IST_EDGE_BOTH, g42xxeb_mci_intr, sc);
142 1.2.2.2 yamt if (sc->sc_detect_ih == NULL) {
143 1.2.2.2 yamt aprint_error_dev(self,
144 1.2.2.2 yamt "unable to establish SD detect interrupt\n");
145 1.2.2.2 yamt return;
146 1.2.2.2 yamt }
147 1.2.2.2 yamt
148 1.2.2.2 yamt sc->sc_mci.sc_tag.cookie = sc;
149 1.2.2.2 yamt sc->sc_mci.sc_tag.get_ocr = g42xxeb_mci_get_ocr;
150 1.2.2.2 yamt sc->sc_mci.sc_tag.set_power = g42xxeb_mci_set_power;
151 1.2.2.2 yamt sc->sc_mci.sc_tag.card_detect = g42xxeb_mci_card_detect;
152 1.2.2.2 yamt sc->sc_mci.sc_tag.write_protect = g42xxeb_mci_write_protect;
153 1.2.2.2 yamt sc->sc_mci.sc_caps = 0;
154 1.2.2.2 yamt
155 1.2.2.2 yamt pxa.pxa_iot = iot; /* actually, here we want I/O tag for
156 1.2.2.2 yamt pxaip, not for obio. */
157 1.2.2.2 yamt
158 1.2.2.2 yamt #if 0
159 1.2.2.2 yamt /* pxamci_attach_sub() ignores following values and uses
160 1.2.2.2 yamt * constants. */
161 1.2.2.2 yamt pxa.pxa_addr = PXA2X0_MMC_BASE;
162 1.2.2.2 yamt pxa.pxa_size = PXA2X0_MMC_SIZE;
163 1.2.2.2 yamt pxa.pxa_intr = PXA2X0_INT_MMC;
164 1.2.2.2 yamt #endif
165 1.2.2.2 yamt
166 1.2.2.2 yamt /* disable DMA for sdmmc for now. */
167 1.2.2.2 yamt SET(sc->sc_mci.sc_caps, PMC_CAPS_NO_DMA);
168 1.2.2.2 yamt
169 1.2.2.2 yamt if (pxamci_attach_sub(self, &pxa)) {
170 1.2.2.2 yamt aprint_error_dev(self,
171 1.2.2.2 yamt "unable to attach MMC controller\n");
172 1.2.2.2 yamt goto free_intr;
173 1.2.2.2 yamt }
174 1.2.2.2 yamt
175 1.2.2.2 yamt if (!pmf_device_register(self, NULL, NULL)) {
176 1.2.2.2 yamt aprint_error_dev(self,
177 1.2.2.2 yamt "couldn't establish power handler\n");
178 1.2.2.2 yamt }
179 1.2.2.2 yamt
180 1.2.2.2 yamt sc->sc_obio_iot = iot;
181 1.2.2.2 yamt sc->sc_obio_ioh = osc->sc_obioreg_ioh;
182 1.2.2.2 yamt
183 1.2.2.2 yamt return;
184 1.2.2.2 yamt
185 1.2.2.2 yamt free_intr:
186 1.2.2.2 yamt obio_intr_disestablish(osc, G42XXEB_INT_MMCSD, sc->sc_detect_ih);
187 1.2.2.2 yamt sc->sc_detect_ih = NULL;
188 1.2.2.2 yamt }
189 1.2.2.2 yamt
190 1.2.2.2 yamt static int
191 1.2.2.2 yamt g42xxeb_mci_intr(void *arg)
192 1.2.2.2 yamt {
193 1.2.2.2 yamt struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
194 1.2.2.2 yamt
195 1.2.2.2 yamt pxamci_card_detect_event(&sc->sc_mci);
196 1.2.2.2 yamt
197 1.2.2.2 yamt return 1;
198 1.2.2.2 yamt }
199 1.2.2.2 yamt
200 1.2.2.2 yamt static uint32_t
201 1.2.2.2 yamt g42xxeb_mci_get_ocr(void *arg)
202 1.2.2.2 yamt {
203 1.2.2.2 yamt
204 1.2.2.2 yamt return MMC_OCR_3_2V_3_3V;
205 1.2.2.2 yamt }
206 1.2.2.2 yamt
207 1.2.2.2 yamt static int
208 1.2.2.2 yamt g42xxeb_mci_set_power(void *arg, uint32_t ocr)
209 1.2.2.2 yamt {
210 1.2.2.2 yamt /* nothing to do */
211 1.2.2.2 yamt return 0;
212 1.2.2.2 yamt }
213 1.2.2.2 yamt
214 1.2.2.2 yamt /*
215 1.2.2.2 yamt * Return non-zero if the card is currently inserted.
216 1.2.2.2 yamt */
217 1.2.2.2 yamt static int
218 1.2.2.2 yamt g42xxeb_mci_card_detect(void *arg)
219 1.2.2.2 yamt {
220 1.2.2.2 yamt struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
221 1.2.2.2 yamt uint16_t reg;
222 1.2.2.2 yamt
223 1.2.2.2 yamt reg = bus_space_read_2(sc->sc_obio_iot, sc->sc_obio_ioh,
224 1.2.2.2 yamt G42XXEB_INTSTS2);
225 1.2.2.2 yamt
226 1.2.2.2 yamt return !(reg & (1<<G42XXEB_INT_MMCSD));
227 1.2.2.2 yamt }
228 1.2.2.2 yamt
229 1.2.2.2 yamt /*
230 1.2.2.2 yamt * Return non-zero if the card is currently write-protected.
231 1.2.2.2 yamt */
232 1.2.2.2 yamt static int
233 1.2.2.2 yamt g42xxeb_mci_write_protect(void *arg)
234 1.2.2.2 yamt {
235 1.2.2.2 yamt struct g42xxeb_mci_softc *sc = (struct g42xxeb_mci_softc *)arg;
236 1.2.2.2 yamt uint16_t reg;
237 1.2.2.2 yamt
238 1.2.2.2 yamt reg = bus_space_read_2(sc->sc_obio_iot, sc->sc_obio_ioh,
239 1.2.2.2 yamt G42XXEB_WP);
240 1.2.2.2 yamt
241 1.2.2.2 yamt return reg & 1;
242 1.2.2.2 yamt }
243