mvsdio.c revision 1.1 1 1.1 kiyohara /* $NetBSD: mvsdio.c,v 1.1 2010/09/23 12:36:01 kiyohara Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2010 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara #include <sys/cdefs.h>
28 1.1 kiyohara __KERNEL_RCSID(0, "$NetBSD: mvsdio.c,v 1.1 2010/09/23 12:36:01 kiyohara Exp $");
29 1.1 kiyohara
30 1.1 kiyohara #include "opt_mvsdio.h"
31 1.1 kiyohara
32 1.1 kiyohara #include <sys/param.h>
33 1.1 kiyohara #include <sys/bus.h>
34 1.1 kiyohara #include <sys/condvar.h>
35 1.1 kiyohara #include <sys/device.h>
36 1.1 kiyohara #include <sys/errno.h>
37 1.1 kiyohara #include <sys/mutex.h>
38 1.1 kiyohara
39 1.1 kiyohara #include <dev/marvell/marvellreg.h>
40 1.1 kiyohara #include <dev/marvell/marvellvar.h>
41 1.1 kiyohara #include <dev/marvell/mvsdioreg.h>
42 1.1 kiyohara
43 1.1 kiyohara #include <dev/sdmmc/sdmmcvar.h>
44 1.1 kiyohara #include <dev/sdmmc/sdmmcchip.h>
45 1.1 kiyohara
46 1.1 kiyohara //#define MVSDIO_DEBUG 1
47 1.1 kiyohara #ifdef MVSDIO_DEBUG
48 1.1 kiyohara #define DPRINTF(n, x) if (mvsdio_debug >= (n)) printf x
49 1.1 kiyohara int mvsdio_debug = MVSDIO_DEBUG;
50 1.1 kiyohara #else
51 1.1 kiyohara #define DPRINTF(n, x)
52 1.1 kiyohara #endif
53 1.1 kiyohara
54 1.1 kiyohara struct mvsdio_softc {
55 1.1 kiyohara device_t sc_dev;
56 1.1 kiyohara device_t sc_sdmmc;
57 1.1 kiyohara
58 1.1 kiyohara bus_space_tag_t sc_iot;
59 1.1 kiyohara bus_space_handle_t sc_ioh;
60 1.1 kiyohara bus_dma_tag_t sc_dmat;
61 1.1 kiyohara
62 1.1 kiyohara struct kmutex sc_mtx;
63 1.1 kiyohara kcondvar_t sc_cv;
64 1.1 kiyohara
65 1.1 kiyohara struct sdmmc_command *sc_exec_cmd;
66 1.1 kiyohara uint32_t sc_waitintr;
67 1.1 kiyohara };
68 1.1 kiyohara
69 1.1 kiyohara static int mvsdio_match(device_t, struct cfdata *, void *);
70 1.1 kiyohara static void mvsdio_attach(device_t, device_t, void *);
71 1.1 kiyohara
72 1.1 kiyohara static int mvsdio_intr(void *);
73 1.1 kiyohara
74 1.1 kiyohara static int mvsdio_host_reset(sdmmc_chipset_handle_t);
75 1.1 kiyohara static uint32_t mvsdio_host_ocr(sdmmc_chipset_handle_t);
76 1.1 kiyohara static int mvsdio_host_maxblklen(sdmmc_chipset_handle_t);
77 1.1 kiyohara #ifdef MVSDIO_CARD_DETECT
78 1.1 kiyohara int MVSDIO_CARD_DETECT(sdmmc_chipset_handle_t);
79 1.1 kiyohara #else
80 1.1 kiyohara static int mvsdio_card_detect(sdmmc_chipset_handle_t);
81 1.1 kiyohara #endif
82 1.1 kiyohara #ifdef MVSDIO_WRITE_PROTECT
83 1.1 kiyohara int MVSDIO_WRITE_PROTECT(sdmmc_chipset_handle_t);
84 1.1 kiyohara #else
85 1.1 kiyohara static int mvsdio_write_protect(sdmmc_chipset_handle_t);
86 1.1 kiyohara #endif
87 1.1 kiyohara static int mvsdio_bus_power(sdmmc_chipset_handle_t, uint32_t);
88 1.1 kiyohara static int mvsdio_bus_clock(sdmmc_chipset_handle_t, int);
89 1.1 kiyohara static int mvsdio_bus_width(sdmmc_chipset_handle_t, int);
90 1.1 kiyohara static int mvsdio_bus_rod(sdmmc_chipset_handle_t, int);
91 1.1 kiyohara static void mvsdio_exec_command(sdmmc_chipset_handle_t, struct sdmmc_command *);
92 1.1 kiyohara static void mvsdio_card_enable_intr(sdmmc_chipset_handle_t, int);
93 1.1 kiyohara static void mvsdio_card_intr_ack(sdmmc_chipset_handle_t);
94 1.1 kiyohara
95 1.1 kiyohara static void mvsdio_wininit(struct mvsdio_softc *);
96 1.1 kiyohara
97 1.1 kiyohara static struct sdmmc_chip_functions mvsdio_chip_functions = {
98 1.1 kiyohara /* host controller reset */
99 1.1 kiyohara .host_reset = mvsdio_host_reset,
100 1.1 kiyohara
101 1.1 kiyohara /* host controller capabilities */
102 1.1 kiyohara .host_ocr = mvsdio_host_ocr,
103 1.1 kiyohara .host_maxblklen = mvsdio_host_maxblklen,
104 1.1 kiyohara
105 1.1 kiyohara /* card detection */
106 1.1 kiyohara #ifdef MVSDIO_CARD_DETECT
107 1.1 kiyohara .card_detect = MVSDIO_CARD_DETECT,
108 1.1 kiyohara #else
109 1.1 kiyohara .card_detect = mvsdio_card_detect,
110 1.1 kiyohara #endif
111 1.1 kiyohara
112 1.1 kiyohara /* write protect */
113 1.1 kiyohara #ifdef MVSDIO_WRITE_PROTECT
114 1.1 kiyohara .write_protect = MVSDIO_WRITE_PROTECT,
115 1.1 kiyohara #else
116 1.1 kiyohara .write_protect = mvsdio_write_protect,
117 1.1 kiyohara #endif
118 1.1 kiyohara
119 1.1 kiyohara /* bus power, clock frequency, width, rod */
120 1.1 kiyohara .bus_power = mvsdio_bus_power,
121 1.1 kiyohara .bus_clock = mvsdio_bus_clock,
122 1.1 kiyohara .bus_width = mvsdio_bus_width,
123 1.1 kiyohara .bus_rod = mvsdio_bus_rod,
124 1.1 kiyohara
125 1.1 kiyohara /* command execution */
126 1.1 kiyohara .exec_command = mvsdio_exec_command,
127 1.1 kiyohara
128 1.1 kiyohara /* card interrupt */
129 1.1 kiyohara .card_enable_intr = mvsdio_card_enable_intr,
130 1.1 kiyohara .card_intr_ack = mvsdio_card_intr_ack,
131 1.1 kiyohara };
132 1.1 kiyohara
133 1.1 kiyohara CFATTACH_DECL_NEW(mvsdio_mbus, sizeof(struct mvsdio_softc),
134 1.1 kiyohara mvsdio_match, mvsdio_attach, NULL, NULL);
135 1.1 kiyohara
136 1.1 kiyohara
137 1.1 kiyohara /* ARGSUSED */
138 1.1 kiyohara static int
139 1.1 kiyohara mvsdio_match(device_t parent, struct cfdata *match, void *aux)
140 1.1 kiyohara {
141 1.1 kiyohara struct marvell_attach_args *mva = aux;
142 1.1 kiyohara
143 1.1 kiyohara if (strcmp(mva->mva_name, match->cf_name) != 0)
144 1.1 kiyohara return 0;
145 1.1 kiyohara if (mva->mva_offset == MVA_OFFSET_DEFAULT)
146 1.1 kiyohara return 0;
147 1.1 kiyohara
148 1.1 kiyohara mva->mva_size = MVSDIO_SIZE;
149 1.1 kiyohara return 1;
150 1.1 kiyohara }
151 1.1 kiyohara
152 1.1 kiyohara /* ARGSUSED */
153 1.1 kiyohara static void
154 1.1 kiyohara mvsdio_attach(device_t parent, device_t self, void *aux)
155 1.1 kiyohara {
156 1.1 kiyohara struct mvsdio_softc *sc = device_private(self);
157 1.1 kiyohara struct marvell_attach_args *mva = aux;
158 1.1 kiyohara struct sdmmcbus_attach_args saa;
159 1.1 kiyohara uint32_t nis, eis;
160 1.1 kiyohara
161 1.1 kiyohara aprint_naive("\n");
162 1.1 kiyohara aprint_normal(": Marvell Secure Digital Input/Output Interface\n");
163 1.1 kiyohara
164 1.1 kiyohara sc->sc_dev = self;
165 1.1 kiyohara sc->sc_iot = mva->mva_iot;
166 1.1 kiyohara if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
167 1.1 kiyohara mva->mva_size, &sc->sc_ioh)) {
168 1.1 kiyohara aprint_error_dev(self, "Cannot map registers\n");
169 1.1 kiyohara return;
170 1.1 kiyohara }
171 1.1 kiyohara sc->sc_dmat = mva->mva_dmat;
172 1.1 kiyohara
173 1.1 kiyohara mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_SDMMC);
174 1.1 kiyohara cv_init(&sc->sc_cv, "mvsdio_intr");
175 1.1 kiyohara
176 1.1 kiyohara sc->sc_exec_cmd = NULL;
177 1.1 kiyohara sc->sc_waitintr = 0;
178 1.1 kiyohara
179 1.1 kiyohara marvell_intr_establish(mva->mva_irq, IPL_SDMMC, mvsdio_intr, sc);
180 1.1 kiyohara
181 1.1 kiyohara mvsdio_wininit(sc);
182 1.1 kiyohara
183 1.1 kiyohara #if BYTE_ORDER == LITTLE_ENDIAN
184 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, HC_BIGENDIAN);
185 1.1 kiyohara #else
186 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, HC_LSBFIRST);
187 1.1 kiyohara #endif
188 1.1 kiyohara nis =
189 1.1 kiyohara NIS_CMDCOMPLETE /* Command Complete */ |
190 1.1 kiyohara NIS_XFERCOMPLETE /* Transfer Complete */ |
191 1.1 kiyohara NIS_BLOCKGAPEV /* Block gap event */ |
192 1.1 kiyohara NIS_DMAINT /* DMA interrupt */ |
193 1.1 kiyohara NIS_CARDINT /* Card interrupt */ |
194 1.1 kiyohara NIS_READWAITON /* Read Wait state is on */ |
195 1.1 kiyohara NIS_SUSPENSEON |
196 1.1 kiyohara NIS_AUTOCMD12COMPLETE /* Auto_cmd12 is comp */|
197 1.1 kiyohara NIS_UNEXPECTEDRESPDET |
198 1.1 kiyohara NIS_ERRINT; /* Error interrupt */
199 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NIS, nis);
200 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISE, nis);
201 1.1 kiyohara
202 1.1 kiyohara #define NIC_DYNAMIC_CONFIG_INTRS (NIS_CMDCOMPLETE | \
203 1.1 kiyohara NIS_XFERCOMPLETE | \
204 1.1 kiyohara NIS_DMAINT | \
205 1.1 kiyohara NIS_CARDINT | \
206 1.1 kiyohara NIS_AUTOCMD12COMPLETE)
207 1.1 kiyohara
208 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE,
209 1.1 kiyohara nis & ~NIC_DYNAMIC_CONFIG_INTRS);
210 1.1 kiyohara
211 1.1 kiyohara eis =
212 1.1 kiyohara EIS_CMDTIMEOUTERR /*Command timeout err*/ |
213 1.1 kiyohara EIS_CMDCRCERR /* Command CRC Error */ |
214 1.1 kiyohara EIS_CMDENDBITERR /*Command end bit err*/ |
215 1.1 kiyohara EIS_CMDINDEXERR /*Command Index Error*/ |
216 1.1 kiyohara EIS_DATATIMEOUTERR /* Data timeout error */ |
217 1.1 kiyohara EIS_RDDATACRCERR /* Read data CRC err */ |
218 1.1 kiyohara EIS_RDDATAENDBITERR /*Rd data end bit err*/ |
219 1.1 kiyohara EIS_AUTOCMD12ERR /* Auto CMD12 error */ |
220 1.1 kiyohara EIS_CMDSTARTBITERR /*Cmd start bit error*/ |
221 1.1 kiyohara EIS_XFERSIZEERR /*Tx size mismatched err*/ |
222 1.1 kiyohara EIS_RESPTBITERR /* Response T bit err */ |
223 1.1 kiyohara EIS_CRCENDBITERR /* CRC end bit error */ |
224 1.1 kiyohara EIS_CRCSTARTBITERR /* CRC start bit err */ |
225 1.1 kiyohara EIS_CRCSTATERR; /* CRC status error */
226 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EIS, eis);
227 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EISE, eis);
228 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EISIE, eis);
229 1.1 kiyohara
230 1.1 kiyohara /*
231 1.1 kiyohara * Attach the generic SD/MMC bus driver. (The bus driver must
232 1.1 kiyohara * not invoke any chipset functions before it is attached.)
233 1.1 kiyohara */
234 1.1 kiyohara memset(&saa, 0, sizeof(saa));
235 1.1 kiyohara saa.saa_busname = "sdmmc";
236 1.1 kiyohara saa.saa_sct = &mvsdio_chip_functions;
237 1.1 kiyohara saa.saa_sch = sc;
238 1.1 kiyohara saa.saa_dmat = sc->sc_dmat;
239 1.1 kiyohara saa.saa_clkmin = 100; /* XXXX: 100 kHz */
240 1.1 kiyohara saa.saa_clkmax = MVSDIO_MAX_CLOCK;
241 1.1 kiyohara saa.saa_caps = SMC_CAPS_AUTO_STOP | SMC_CAPS_4BIT_MODE | SMC_CAPS_DMA;
242 1.1 kiyohara #ifndef MVSDIO_CARD_DETECT
243 1.1 kiyohara saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
244 1.1 kiyohara #endif
245 1.1 kiyohara sc->sc_sdmmc = config_found(sc->sc_dev, &saa, NULL);
246 1.1 kiyohara }
247 1.1 kiyohara
248 1.1 kiyohara static int
249 1.1 kiyohara mvsdio_intr(void *arg)
250 1.1 kiyohara {
251 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)arg;
252 1.1 kiyohara struct sdmmc_command *cmd = sc->sc_exec_cmd;
253 1.1 kiyohara uint32_t nis, eis;
254 1.1 kiyohara int handled = 0, error;
255 1.1 kiyohara
256 1.1 kiyohara nis = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NIS);
257 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NIS, nis);
258 1.1 kiyohara
259 1.1 kiyohara DPRINTF(3, ("%s: intr: NIS=0x%x, NISE=0x%x, NISIE=0x%x\n",
260 1.1 kiyohara __func__, nis,
261 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISE),
262 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE)));
263 1.1 kiyohara
264 1.1 kiyohara if (__predict_false(nis & NIS_ERRINT)) {
265 1.1 kiyohara sc->sc_exec_cmd = NULL;
266 1.1 kiyohara eis = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EIS);
267 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EIS, eis);
268 1.1 kiyohara
269 1.1 kiyohara DPRINTF(3, (" EIS=0x%x, EISE=0x%x, EISIE=0x%x\n",
270 1.1 kiyohara eis,
271 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EISE),
272 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EISIE)));
273 1.1 kiyohara
274 1.1 kiyohara if (eis & (EIS_CMDTIMEOUTERR | EIS_DATATIMEOUTERR)) {
275 1.1 kiyohara error = ETIMEDOUT; /* Timeouts */
276 1.1 kiyohara DPRINTF(2, (" Command/Data Timeout (0x%x)\n",
277 1.1 kiyohara eis & (EIS_CMDTIMEOUTERR | EIS_DATATIMEOUTERR)));
278 1.1 kiyohara } else {
279 1.1 kiyohara
280 1.1 kiyohara #define CRC_ERROR (EIS_CMDCRCERR | \
281 1.1 kiyohara EIS_RDDATACRCERR | \
282 1.1 kiyohara EIS_CRCENDBITERR | \
283 1.1 kiyohara EIS_CRCSTARTBITERR | \
284 1.1 kiyohara EIS_CRCSTATERR)
285 1.1 kiyohara if (eis & CRC_ERROR) {
286 1.1 kiyohara error = EIO; /* CRC errors */
287 1.1 kiyohara aprint_error_dev(sc->sc_dev,
288 1.1 kiyohara "CRC Error (0x%x)\n", eis & CRC_ERROR);
289 1.1 kiyohara }
290 1.1 kiyohara
291 1.1 kiyohara #define COMMAND_ERROR (EIS_CMDENDBITERR | \
292 1.1 kiyohara EIS_CMDINDEXERR | \
293 1.1 kiyohara EIS_CMDSTARTBITERR)
294 1.1 kiyohara if (eis & COMMAND_ERROR) {
295 1.1 kiyohara error = EIO; /*Other command errors*/
296 1.1 kiyohara aprint_error_dev(sc->sc_dev,
297 1.1 kiyohara "Command Error (0x%x)\n",
298 1.1 kiyohara eis & COMMAND_ERROR);
299 1.1 kiyohara }
300 1.1 kiyohara
301 1.1 kiyohara #define MISC_ERROR (EIS_RDDATAENDBITERR | \
302 1.1 kiyohara EIS_AUTOCMD12ERR | \
303 1.1 kiyohara EIS_XFERSIZEERR | \
304 1.1 kiyohara EIS_RESPTBITERR)
305 1.1 kiyohara if (eis & MISC_ERROR) {
306 1.1 kiyohara error = EIO; /* Misc error */
307 1.1 kiyohara aprint_error_dev(sc->sc_dev,
308 1.1 kiyohara "Misc Error (0x%x)\n", eis & MISC_ERROR);
309 1.1 kiyohara }
310 1.1 kiyohara }
311 1.1 kiyohara
312 1.1 kiyohara if (cmd != NULL) {
313 1.1 kiyohara cmd->c_error = error;
314 1.1 kiyohara cv_signal(&sc->sc_cv);
315 1.1 kiyohara }
316 1.1 kiyohara handled = 1;
317 1.1 kiyohara } else if (cmd != NULL &&
318 1.1 kiyohara ((nis & sc->sc_waitintr) || (nis & NIS_UNEXPECTEDRESPDET))) {
319 1.1 kiyohara sc->sc_exec_cmd = NULL;
320 1.1 kiyohara sc->sc_waitintr = 0;
321 1.1 kiyohara if (cmd->c_flags & SCF_RSP_PRESENT) {
322 1.1 kiyohara uint16_t rh[MVSDIO_NRH + 1];
323 1.1 kiyohara int i, j;
324 1.1 kiyohara
325 1.1 kiyohara if (cmd->c_flags & SCF_RSP_136) {
326 1.1 kiyohara for (i = 0; i < MVSDIO_NRH; i++)
327 1.1 kiyohara rh[i + 1] = bus_space_read_4(sc->sc_iot,
328 1.1 kiyohara sc->sc_ioh, MVSDIO_RH(i));
329 1.1 kiyohara rh[0] = 0;
330 1.1 kiyohara for (j = 3, i = 1; j >= 0; j--, i += 2) {
331 1.1 kiyohara cmd->c_resp[j] =
332 1.1 kiyohara rh[i - 1] << 30 |
333 1.1 kiyohara rh[i + 0] << 14 |
334 1.1 kiyohara rh[i + 1] >> 2;
335 1.1 kiyohara }
336 1.1 kiyohara cmd->c_resp[3] &= 0x00ffffff;
337 1.1 kiyohara } else {
338 1.1 kiyohara for (i = 0; i < 3; i++)
339 1.1 kiyohara rh[i] = bus_space_read_4(sc->sc_iot,
340 1.1 kiyohara sc->sc_ioh, MVSDIO_RH(i));
341 1.1 kiyohara cmd->c_resp[0] =
342 1.1 kiyohara ((rh[0] & 0x03ff) << 22) |
343 1.1 kiyohara ((rh[1] ) << 6) |
344 1.1 kiyohara ((rh[2] & 0x003f) << 0);
345 1.1 kiyohara cmd->c_resp[1] = (rh[0] & 0xfc00) >> 10;
346 1.1 kiyohara cmd->c_resp[2] = 0;
347 1.1 kiyohara cmd->c_resp[3] = 0;
348 1.1 kiyohara }
349 1.1 kiyohara }
350 1.1 kiyohara if (nis & NIS_UNEXPECTEDRESPDET)
351 1.1 kiyohara cmd->c_error = EIO;
352 1.1 kiyohara cv_signal(&sc->sc_cv);
353 1.1 kiyohara }
354 1.1 kiyohara
355 1.1 kiyohara if (nis & NIS_CARDINT)
356 1.1 kiyohara if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE) &
357 1.1 kiyohara NIS_CARDINT) {
358 1.1 kiyohara sdmmc_card_intr(sc->sc_sdmmc);
359 1.1 kiyohara handled = 1;
360 1.1 kiyohara }
361 1.1 kiyohara
362 1.1 kiyohara return handled;
363 1.1 kiyohara }
364 1.1 kiyohara
365 1.1 kiyohara static int
366 1.1 kiyohara mvsdio_host_reset(sdmmc_chipset_handle_t sch)
367 1.1 kiyohara {
368 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
369 1.1 kiyohara
370 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_SR, SR_SWRESET);
371 1.1 kiyohara return 0;
372 1.1 kiyohara }
373 1.1 kiyohara
374 1.1 kiyohara static uint32_t
375 1.1 kiyohara mvsdio_host_ocr(sdmmc_chipset_handle_t sch)
376 1.1 kiyohara {
377 1.1 kiyohara
378 1.1 kiyohara return MMC_OCR_3_3V_3_4V | MMC_OCR_3_2V_3_3V;
379 1.1 kiyohara }
380 1.1 kiyohara
381 1.1 kiyohara static int
382 1.1 kiyohara mvsdio_host_maxblklen(sdmmc_chipset_handle_t sch)
383 1.1 kiyohara {
384 1.1 kiyohara
385 1.1 kiyohara return DBS_BLOCKSIZE_MAX;
386 1.1 kiyohara }
387 1.1 kiyohara
388 1.1 kiyohara #ifndef MVSDIO_CARD_DETECT
389 1.1 kiyohara static int
390 1.1 kiyohara mvsdio_card_detect(sdmmc_chipset_handle_t sch)
391 1.1 kiyohara {
392 1.1 kiyohara struct mvsdio_softc *sc __unused = (struct mvsdio_softc *)sch;
393 1.1 kiyohara
394 1.1 kiyohara DPRINTF(2, ("%s: driver lacks card_detect() function.\n",
395 1.1 kiyohara device_xname(sc->sc_dev)));
396 1.1 kiyohara return 1; /* always detect */
397 1.1 kiyohara }
398 1.1 kiyohara #endif
399 1.1 kiyohara
400 1.1 kiyohara #ifndef MVSDIO_WRITE_PROTECT
401 1.1 kiyohara static int
402 1.1 kiyohara mvsdio_write_protect(sdmmc_chipset_handle_t sch)
403 1.1 kiyohara {
404 1.1 kiyohara
405 1.1 kiyohara /* Nothing */
406 1.1 kiyohara
407 1.1 kiyohara return 0;
408 1.1 kiyohara }
409 1.1 kiyohara #endif
410 1.1 kiyohara
411 1.1 kiyohara static int
412 1.1 kiyohara mvsdio_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
413 1.1 kiyohara {
414 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
415 1.1 kiyohara uint32_t reg;
416 1.1 kiyohara
417 1.1 kiyohara /* Initial state is Open Drain on CMD line. */
418 1.1 kiyohara mutex_enter(&sc->sc_mtx);
419 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
420 1.1 kiyohara reg &= ~HC_PUSHPULLEN;
421 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
422 1.1 kiyohara mutex_exit(&sc->sc_mtx);
423 1.1 kiyohara
424 1.1 kiyohara return 0;
425 1.1 kiyohara }
426 1.1 kiyohara
427 1.1 kiyohara static int
428 1.1 kiyohara mvsdio_bus_clock(sdmmc_chipset_handle_t sch, int freq)
429 1.1 kiyohara {
430 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
431 1.1 kiyohara uint32_t reg;
432 1.1 kiyohara int m;
433 1.1 kiyohara
434 1.1 kiyohara mutex_enter(&sc->sc_mtx);
435 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM);
436 1.1 kiyohara
437 1.1 kiyohara /* Just stop the clock. */
438 1.1 kiyohara if (freq == 0) {
439 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM,
440 1.1 kiyohara reg | TM_STOPCLKEN);
441 1.1 kiyohara goto out;
442 1.1 kiyohara }
443 1.1 kiyohara
444 1.1 kiyohara #define FREQ_TO_M(f) (100000 / (f) - 1)
445 1.1 kiyohara
446 1.1 kiyohara m = FREQ_TO_M(freq);
447 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_CDV,
448 1.1 kiyohara m & CDV_CLKDVDRMVALUE_MASK);
449 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM,
450 1.1 kiyohara reg & ~TM_STOPCLKEN);
451 1.1 kiyohara
452 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
453 1.1 kiyohara if (freq > 25000)
454 1.1 kiyohara reg |= HC_HISPEEDEN;
455 1.1 kiyohara else
456 1.1 kiyohara reg &= ~HC_HISPEEDEN; /* up to 25 MHz */
457 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
458 1.1 kiyohara
459 1.1 kiyohara out:
460 1.1 kiyohara mutex_exit(&sc->sc_mtx);
461 1.1 kiyohara
462 1.1 kiyohara return 0;
463 1.1 kiyohara }
464 1.1 kiyohara
465 1.1 kiyohara static int
466 1.1 kiyohara mvsdio_bus_width(sdmmc_chipset_handle_t sch, int width)
467 1.1 kiyohara {
468 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
469 1.1 kiyohara uint32_t reg, v;
470 1.1 kiyohara
471 1.1 kiyohara switch (width) {
472 1.1 kiyohara case 1:
473 1.1 kiyohara v = 0;
474 1.1 kiyohara break;
475 1.1 kiyohara
476 1.1 kiyohara case 4:
477 1.1 kiyohara v = HC_DATAWIDTH;
478 1.1 kiyohara break;
479 1.1 kiyohara
480 1.1 kiyohara default:
481 1.1 kiyohara DPRINTF(0, ("%s: unsupported bus width (%d)\n",
482 1.1 kiyohara device_xname(sc->sc_dev), width));
483 1.1 kiyohara return EINVAL;
484 1.1 kiyohara }
485 1.1 kiyohara
486 1.1 kiyohara mutex_enter(&sc->sc_mtx);
487 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
488 1.1 kiyohara reg &= ~HC_DATAWIDTH;
489 1.1 kiyohara reg |= v;
490 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
491 1.1 kiyohara mutex_exit(&sc->sc_mtx);
492 1.1 kiyohara
493 1.1 kiyohara return 0;
494 1.1 kiyohara }
495 1.1 kiyohara
496 1.1 kiyohara static int
497 1.1 kiyohara mvsdio_bus_rod(sdmmc_chipset_handle_t sch, int on)
498 1.1 kiyohara {
499 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
500 1.1 kiyohara uint32_t reg;
501 1.1 kiyohara
502 1.1 kiyohara /* Change Open-drain/Push-pull. */
503 1.1 kiyohara mutex_enter(&sc->sc_mtx);
504 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
505 1.1 kiyohara if (on)
506 1.1 kiyohara reg &= ~HC_PUSHPULLEN;
507 1.1 kiyohara else
508 1.1 kiyohara reg |= HC_PUSHPULLEN;
509 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
510 1.1 kiyohara mutex_exit(&sc->sc_mtx);
511 1.1 kiyohara
512 1.1 kiyohara return 0;
513 1.1 kiyohara }
514 1.1 kiyohara
515 1.1 kiyohara static void
516 1.1 kiyohara mvsdio_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
517 1.1 kiyohara {
518 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
519 1.1 kiyohara uint32_t tm, c, hc, aacc, nisie, wait;
520 1.1 kiyohara int blklen;
521 1.1 kiyohara
522 1.1 kiyohara DPRINTF(1, ("%s: start cmd %d arg=%#x data=%p dlen=%d flags=%#x\n",
523 1.1 kiyohara device_xname(sc->sc_dev), cmd->c_opcode, cmd->c_arg, cmd->c_data,
524 1.1 kiyohara cmd->c_datalen, cmd->c_flags));
525 1.1 kiyohara
526 1.1 kiyohara mutex_enter(&sc->sc_mtx);
527 1.1 kiyohara
528 1.1 kiyohara tm = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM);
529 1.1 kiyohara
530 1.1 kiyohara if (cmd->c_datalen > 0) {
531 1.1 kiyohara bus_dma_segment_t *dm_seg =
532 1.1 kiyohara &cmd->c_dmamap->dm_segs[cmd->c_dmaseg];
533 1.1 kiyohara bus_addr_t ds_addr = dm_seg->ds_addr + cmd->c_dmaoff;
534 1.1 kiyohara
535 1.1 kiyohara blklen = MIN(cmd->c_datalen, cmd->c_blklen);
536 1.1 kiyohara
537 1.1 kiyohara if (cmd->c_datalen % blklen > 0) {
538 1.1 kiyohara aprint_error_dev(sc->sc_dev,
539 1.1 kiyohara "data not a multiple of %u bytes\n", blklen);
540 1.1 kiyohara cmd->c_error = EINVAL;
541 1.1 kiyohara goto out;
542 1.1 kiyohara }
543 1.1 kiyohara if ((uint32_t)cmd->c_data & 0x3) {
544 1.1 kiyohara aprint_error_dev(sc->sc_dev,
545 1.1 kiyohara "data not 4byte aligned\n");
546 1.1 kiyohara cmd->c_error = EINVAL;
547 1.1 kiyohara goto out;
548 1.1 kiyohara }
549 1.1 kiyohara
550 1.1 kiyohara /* Set DMA Buffer Address */
551 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DMABA16LSB,
552 1.1 kiyohara ds_addr & 0xffff);
553 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DMABA16MSB,
554 1.1 kiyohara (ds_addr >> 16) & 0xffff);
555 1.1 kiyohara
556 1.1 kiyohara /* Set Data Block Size and Count */
557 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DBS,
558 1.1 kiyohara DBS_BLOCKSIZE(blklen));
559 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DBC,
560 1.1 kiyohara DBC_BLOCKCOUNT(cmd->c_datalen / blklen));
561 1.1 kiyohara
562 1.1 kiyohara tm &= ~TM_HOSTXFERMODE; /* Always DMA */
563 1.1 kiyohara if (cmd->c_flags & SCF_CMD_READ)
564 1.1 kiyohara tm |= TM_DATAXFERTOWARDHOST;
565 1.1 kiyohara else
566 1.1 kiyohara tm &= ~TM_DATAXFERTOWARDHOST;
567 1.1 kiyohara tm |= TM_HWWRDATAEN;
568 1.1 kiyohara wait = NIS_XFERCOMPLETE;
569 1.1 kiyohara } else {
570 1.1 kiyohara tm &= ~TM_HWWRDATAEN;
571 1.1 kiyohara wait = NIS_CMDCOMPLETE;
572 1.1 kiyohara }
573 1.1 kiyohara
574 1.1 kiyohara /* Set Argument in Command */
575 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AC16LSB,
576 1.1 kiyohara cmd->c_arg & 0xffff);
577 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AC16MSB,
578 1.1 kiyohara (cmd->c_arg >> 16) & 0xffff);
579 1.1 kiyohara
580 1.1 kiyohara /* Set Host Control, exclude PushPullEn, DataWidth, HiSpeedEn. */
581 1.1 kiyohara hc = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
582 1.1 kiyohara hc |= (HC_TIMEOUTVALUE_MAX | HC_TIMEOUTEN);
583 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, hc);
584 1.1 kiyohara
585 1.1 kiyohara /* Data Block Gap Control: Resume */
586 1.1 kiyohara
587 1.1 kiyohara /* Clock Control: SclkMasterEn */
588 1.1 kiyohara
589 1.1 kiyohara if (cmd->c_opcode == MMC_READ_BLOCK_MULTIPLE ||
590 1.1 kiyohara cmd->c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
591 1.1 kiyohara aacc = 0;
592 1.1 kiyohara #if 1 /* XXXX: need? */
593 1.1 kiyohara if (cmd->c_opcode == MMC_READ_BLOCK_MULTIPLE) {
594 1.1 kiyohara struct sdmmc_softc *sdmmc =
595 1.1 kiyohara device_private(sc->sc_sdmmc);
596 1.1 kiyohara struct sdmmc_function *sf = sdmmc->sc_card;
597 1.1 kiyohara
598 1.1 kiyohara aacc = MMC_ARG_RCA(sf->rca);
599 1.1 kiyohara }
600 1.1 kiyohara #endif
601 1.1 kiyohara
602 1.1 kiyohara /* Set Argument in Auto Cmd12 Command */
603 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AACC16LSBT,
604 1.1 kiyohara aacc & 0xffff);
605 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AACC16MSBT,
606 1.1 kiyohara (aacc >> 16) & 0xffff);
607 1.1 kiyohara
608 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_IACCT,
609 1.1 kiyohara IACCT_AUTOCMD12BUSYCHKEN |
610 1.1 kiyohara IACCT_AUTOCMD12INDEXCHKEN |
611 1.1 kiyohara IACCT_AUTOCMD12INDEX);
612 1.1 kiyohara
613 1.1 kiyohara tm |= TM_AUTOCMD12EN;
614 1.1 kiyohara wait = NIS_AUTOCMD12COMPLETE;
615 1.1 kiyohara } else
616 1.1 kiyohara tm &= ~TM_AUTOCMD12EN;
617 1.1 kiyohara
618 1.1 kiyohara tm |= TM_INTCHKEN;
619 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM, tm);
620 1.1 kiyohara
621 1.1 kiyohara c = C_CMDINDEX(cmd->c_opcode);
622 1.1 kiyohara if (cmd->c_flags & SCF_RSP_PRESENT) {
623 1.1 kiyohara if (cmd->c_flags & SCF_RSP_136)
624 1.1 kiyohara c |= C_RESPTYPE_136BR;
625 1.1 kiyohara else if (!(cmd->c_flags & SCF_RSP_BSY))
626 1.1 kiyohara c |= C_RESPTYPE_48BR;
627 1.1 kiyohara else
628 1.1 kiyohara c |= C_RESPTYPE_48BRCB;
629 1.1 kiyohara c |= C_UNEXPECTEDRESPEN;
630 1.1 kiyohara } else
631 1.1 kiyohara c |= C_RESPTYPE_NR;
632 1.1 kiyohara if (cmd->c_flags & SCF_RSP_CRC)
633 1.1 kiyohara c |= C_CMDCRCCHKEN;
634 1.1 kiyohara if (cmd->c_flags & SCF_RSP_IDX)
635 1.1 kiyohara c |= C_CMDINDEXCHKEN;
636 1.1 kiyohara if (cmd->c_datalen > 0)
637 1.1 kiyohara c |= (C_DATAPRESENT | C_DATACRC16CHKEN);
638 1.1 kiyohara
639 1.1 kiyohara DPRINTF(2, ("%s: TM=0x%x, C=0x%x, HC=0x%x\n", __func__, tm, c, hc));
640 1.1 kiyohara
641 1.1 kiyohara nisie = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE);
642 1.1 kiyohara nisie &= ~(NIS_CMDCOMPLETE | NIS_XFERCOMPLETE | NIS_AUTOCMD12COMPLETE);
643 1.1 kiyohara nisie |= wait;
644 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE, nisie);
645 1.1 kiyohara
646 1.1 kiyohara /* Execute command */
647 1.1 kiyohara sc->sc_exec_cmd = cmd;
648 1.1 kiyohara sc->sc_waitintr = wait;
649 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_C, c);
650 1.1 kiyohara
651 1.1 kiyohara /* Wait interrupt for complete or error or timeout */
652 1.1 kiyohara while (sc->sc_exec_cmd == cmd)
653 1.1 kiyohara cv_wait(&sc->sc_cv, &sc->sc_mtx);
654 1.1 kiyohara
655 1.1 kiyohara out:
656 1.1 kiyohara mutex_exit(&sc->sc_mtx);
657 1.1 kiyohara
658 1.1 kiyohara DPRINTF(1, ("%s: cmd %d done (flags=%08x error=%d)\n",
659 1.1 kiyohara device_xname(sc->sc_dev),
660 1.1 kiyohara cmd->c_opcode, cmd->c_flags, cmd->c_error));
661 1.1 kiyohara }
662 1.1 kiyohara
663 1.1 kiyohara static void
664 1.1 kiyohara mvsdio_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
665 1.1 kiyohara {
666 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
667 1.1 kiyohara uint32_t reg;
668 1.1 kiyohara
669 1.1 kiyohara mutex_enter(&sc->sc_mtx);
670 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE);
671 1.1 kiyohara reg |= NIS_CARDINT;
672 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE, reg);
673 1.1 kiyohara mutex_exit(&sc->sc_mtx);
674 1.1 kiyohara }
675 1.1 kiyohara
676 1.1 kiyohara static void
677 1.1 kiyohara mvsdio_card_intr_ack(sdmmc_chipset_handle_t sch)
678 1.1 kiyohara {
679 1.1 kiyohara
680 1.1 kiyohara /* Nothing */
681 1.1 kiyohara }
682 1.1 kiyohara
683 1.1 kiyohara
684 1.1 kiyohara static void
685 1.1 kiyohara mvsdio_wininit(struct mvsdio_softc *sc)
686 1.1 kiyohara {
687 1.1 kiyohara uint64_t base;
688 1.1 kiyohara uint32_t size;
689 1.1 kiyohara int window, target, attr, rv, i;
690 1.1 kiyohara static int tags[] = {
691 1.1 kiyohara MARVELL_TAG_SDRAM_CS0,
692 1.1 kiyohara MARVELL_TAG_SDRAM_CS1,
693 1.1 kiyohara MARVELL_TAG_SDRAM_CS2,
694 1.1 kiyohara MARVELL_TAG_SDRAM_CS3,
695 1.1 kiyohara
696 1.1 kiyohara MARVELL_TAG_UNDEFINED,
697 1.1 kiyohara };
698 1.1 kiyohara
699 1.1 kiyohara for (window = 0, i = 0;
700 1.1 kiyohara tags[i] != MARVELL_TAG_UNDEFINED && window < MVSDIO_NWINDOW; i++) {
701 1.1 kiyohara rv = marvell_winparams_by_tag(sc->sc_dev, tags[i],
702 1.1 kiyohara &target, &attr, &base, &size);
703 1.1 kiyohara if (rv != 0 || size == 0)
704 1.1 kiyohara continue;
705 1.1 kiyohara
706 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_WC(window),
707 1.1 kiyohara WC_WINEN |
708 1.1 kiyohara WC_TARGET(target) |
709 1.1 kiyohara WC_ATTR(attr) |
710 1.1 kiyohara WC_SIZE(size));
711 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_WB(window),
712 1.1 kiyohara WB_BASE(base));
713 1.1 kiyohara window++;
714 1.1 kiyohara }
715 1.1 kiyohara for (; window < MVSDIO_NWINDOW; window++)
716 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_WC(window), 0);
717 1.1 kiyohara }
718