mvsdio.c revision 1.5 1 1.5 kiyohara /* $NetBSD: mvsdio.c,v 1.5 2014/03/15 13:33:48 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.5 kiyohara __KERNEL_RCSID(0, "$NetBSD: mvsdio.c,v 1.5 2014/03/15 13:33:48 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.5 kiyohara static void mvsdio_wininit(struct mvsdio_softc *, enum marvell_tags *);
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.5 kiyohara mvsdio_wininit(sc, mva->mva_tags);
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.2 kiyohara saa.saa_clkmin = 100; /* XXXX: 100 kHz from SheevaPlug LSP */
240 1.1 kiyohara saa.saa_clkmax = MVSDIO_MAX_CLOCK;
241 1.3 nonaka saa.saa_caps = SMC_CAPS_AUTO_STOP | SMC_CAPS_4BIT_MODE | SMC_CAPS_DMA |
242 1.4 nonaka SMC_CAPS_SD_HIGHSPEED | SMC_CAPS_MMC_HIGHSPEED;
243 1.1 kiyohara #ifndef MVSDIO_CARD_DETECT
244 1.1 kiyohara saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
245 1.1 kiyohara #endif
246 1.1 kiyohara sc->sc_sdmmc = config_found(sc->sc_dev, &saa, NULL);
247 1.1 kiyohara }
248 1.1 kiyohara
249 1.1 kiyohara static int
250 1.1 kiyohara mvsdio_intr(void *arg)
251 1.1 kiyohara {
252 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)arg;
253 1.1 kiyohara struct sdmmc_command *cmd = sc->sc_exec_cmd;
254 1.1 kiyohara uint32_t nis, eis;
255 1.1 kiyohara int handled = 0, error;
256 1.1 kiyohara
257 1.1 kiyohara nis = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NIS);
258 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NIS, nis);
259 1.1 kiyohara
260 1.1 kiyohara DPRINTF(3, ("%s: intr: NIS=0x%x, NISE=0x%x, NISIE=0x%x\n",
261 1.1 kiyohara __func__, nis,
262 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISE),
263 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE)));
264 1.1 kiyohara
265 1.1 kiyohara if (__predict_false(nis & NIS_ERRINT)) {
266 1.1 kiyohara sc->sc_exec_cmd = NULL;
267 1.1 kiyohara eis = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EIS);
268 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EIS, eis);
269 1.1 kiyohara
270 1.1 kiyohara DPRINTF(3, (" EIS=0x%x, EISE=0x%x, EISIE=0x%x\n",
271 1.1 kiyohara eis,
272 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EISE),
273 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_EISIE)));
274 1.1 kiyohara
275 1.1 kiyohara if (eis & (EIS_CMDTIMEOUTERR | EIS_DATATIMEOUTERR)) {
276 1.1 kiyohara error = ETIMEDOUT; /* Timeouts */
277 1.1 kiyohara DPRINTF(2, (" Command/Data Timeout (0x%x)\n",
278 1.1 kiyohara eis & (EIS_CMDTIMEOUTERR | EIS_DATATIMEOUTERR)));
279 1.1 kiyohara } else {
280 1.1 kiyohara
281 1.1 kiyohara #define CRC_ERROR (EIS_CMDCRCERR | \
282 1.1 kiyohara EIS_RDDATACRCERR | \
283 1.1 kiyohara EIS_CRCENDBITERR | \
284 1.1 kiyohara EIS_CRCSTARTBITERR | \
285 1.1 kiyohara EIS_CRCSTATERR)
286 1.1 kiyohara if (eis & CRC_ERROR) {
287 1.1 kiyohara error = EIO; /* CRC errors */
288 1.1 kiyohara aprint_error_dev(sc->sc_dev,
289 1.1 kiyohara "CRC Error (0x%x)\n", eis & CRC_ERROR);
290 1.1 kiyohara }
291 1.1 kiyohara
292 1.1 kiyohara #define COMMAND_ERROR (EIS_CMDENDBITERR | \
293 1.1 kiyohara EIS_CMDINDEXERR | \
294 1.1 kiyohara EIS_CMDSTARTBITERR)
295 1.1 kiyohara if (eis & COMMAND_ERROR) {
296 1.1 kiyohara error = EIO; /*Other command errors*/
297 1.1 kiyohara aprint_error_dev(sc->sc_dev,
298 1.1 kiyohara "Command Error (0x%x)\n",
299 1.1 kiyohara eis & COMMAND_ERROR);
300 1.1 kiyohara }
301 1.1 kiyohara
302 1.1 kiyohara #define MISC_ERROR (EIS_RDDATAENDBITERR | \
303 1.1 kiyohara EIS_AUTOCMD12ERR | \
304 1.1 kiyohara EIS_XFERSIZEERR | \
305 1.1 kiyohara EIS_RESPTBITERR)
306 1.1 kiyohara if (eis & MISC_ERROR) {
307 1.1 kiyohara error = EIO; /* Misc error */
308 1.1 kiyohara aprint_error_dev(sc->sc_dev,
309 1.1 kiyohara "Misc Error (0x%x)\n", eis & MISC_ERROR);
310 1.1 kiyohara }
311 1.1 kiyohara }
312 1.1 kiyohara
313 1.1 kiyohara if (cmd != NULL) {
314 1.1 kiyohara cmd->c_error = error;
315 1.1 kiyohara cv_signal(&sc->sc_cv);
316 1.1 kiyohara }
317 1.1 kiyohara handled = 1;
318 1.1 kiyohara } else if (cmd != NULL &&
319 1.1 kiyohara ((nis & sc->sc_waitintr) || (nis & NIS_UNEXPECTEDRESPDET))) {
320 1.1 kiyohara sc->sc_exec_cmd = NULL;
321 1.1 kiyohara sc->sc_waitintr = 0;
322 1.1 kiyohara if (cmd->c_flags & SCF_RSP_PRESENT) {
323 1.1 kiyohara uint16_t rh[MVSDIO_NRH + 1];
324 1.1 kiyohara int i, j;
325 1.1 kiyohara
326 1.1 kiyohara if (cmd->c_flags & SCF_RSP_136) {
327 1.1 kiyohara for (i = 0; i < MVSDIO_NRH; i++)
328 1.1 kiyohara rh[i + 1] = bus_space_read_4(sc->sc_iot,
329 1.1 kiyohara sc->sc_ioh, MVSDIO_RH(i));
330 1.1 kiyohara rh[0] = 0;
331 1.1 kiyohara for (j = 3, i = 1; j >= 0; j--, i += 2) {
332 1.1 kiyohara cmd->c_resp[j] =
333 1.1 kiyohara rh[i - 1] << 30 |
334 1.1 kiyohara rh[i + 0] << 14 |
335 1.1 kiyohara rh[i + 1] >> 2;
336 1.1 kiyohara }
337 1.1 kiyohara cmd->c_resp[3] &= 0x00ffffff;
338 1.1 kiyohara } else {
339 1.1 kiyohara for (i = 0; i < 3; i++)
340 1.1 kiyohara rh[i] = bus_space_read_4(sc->sc_iot,
341 1.1 kiyohara sc->sc_ioh, MVSDIO_RH(i));
342 1.1 kiyohara cmd->c_resp[0] =
343 1.1 kiyohara ((rh[0] & 0x03ff) << 22) |
344 1.1 kiyohara ((rh[1] ) << 6) |
345 1.1 kiyohara ((rh[2] & 0x003f) << 0);
346 1.1 kiyohara cmd->c_resp[1] = (rh[0] & 0xfc00) >> 10;
347 1.1 kiyohara cmd->c_resp[2] = 0;
348 1.1 kiyohara cmd->c_resp[3] = 0;
349 1.1 kiyohara }
350 1.1 kiyohara }
351 1.1 kiyohara if (nis & NIS_UNEXPECTEDRESPDET)
352 1.1 kiyohara cmd->c_error = EIO;
353 1.1 kiyohara cv_signal(&sc->sc_cv);
354 1.1 kiyohara }
355 1.1 kiyohara
356 1.1 kiyohara if (nis & NIS_CARDINT)
357 1.1 kiyohara if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE) &
358 1.1 kiyohara NIS_CARDINT) {
359 1.1 kiyohara sdmmc_card_intr(sc->sc_sdmmc);
360 1.1 kiyohara handled = 1;
361 1.1 kiyohara }
362 1.1 kiyohara
363 1.1 kiyohara return handled;
364 1.1 kiyohara }
365 1.1 kiyohara
366 1.1 kiyohara static int
367 1.1 kiyohara mvsdio_host_reset(sdmmc_chipset_handle_t sch)
368 1.1 kiyohara {
369 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
370 1.1 kiyohara
371 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_SR, SR_SWRESET);
372 1.1 kiyohara return 0;
373 1.1 kiyohara }
374 1.1 kiyohara
375 1.1 kiyohara static uint32_t
376 1.1 kiyohara mvsdio_host_ocr(sdmmc_chipset_handle_t sch)
377 1.1 kiyohara {
378 1.1 kiyohara
379 1.1 kiyohara return MMC_OCR_3_3V_3_4V | MMC_OCR_3_2V_3_3V;
380 1.1 kiyohara }
381 1.1 kiyohara
382 1.1 kiyohara static int
383 1.1 kiyohara mvsdio_host_maxblklen(sdmmc_chipset_handle_t sch)
384 1.1 kiyohara {
385 1.1 kiyohara
386 1.1 kiyohara return DBS_BLOCKSIZE_MAX;
387 1.1 kiyohara }
388 1.1 kiyohara
389 1.1 kiyohara #ifndef MVSDIO_CARD_DETECT
390 1.1 kiyohara static int
391 1.1 kiyohara mvsdio_card_detect(sdmmc_chipset_handle_t sch)
392 1.1 kiyohara {
393 1.1 kiyohara struct mvsdio_softc *sc __unused = (struct mvsdio_softc *)sch;
394 1.1 kiyohara
395 1.1 kiyohara DPRINTF(2, ("%s: driver lacks card_detect() function.\n",
396 1.1 kiyohara device_xname(sc->sc_dev)));
397 1.1 kiyohara return 1; /* always detect */
398 1.1 kiyohara }
399 1.1 kiyohara #endif
400 1.1 kiyohara
401 1.1 kiyohara #ifndef MVSDIO_WRITE_PROTECT
402 1.1 kiyohara static int
403 1.1 kiyohara mvsdio_write_protect(sdmmc_chipset_handle_t sch)
404 1.1 kiyohara {
405 1.1 kiyohara
406 1.1 kiyohara /* Nothing */
407 1.1 kiyohara
408 1.1 kiyohara return 0;
409 1.1 kiyohara }
410 1.1 kiyohara #endif
411 1.1 kiyohara
412 1.1 kiyohara static int
413 1.1 kiyohara mvsdio_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
414 1.1 kiyohara {
415 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
416 1.1 kiyohara uint32_t reg;
417 1.1 kiyohara
418 1.1 kiyohara /* Initial state is Open Drain on CMD line. */
419 1.1 kiyohara mutex_enter(&sc->sc_mtx);
420 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
421 1.1 kiyohara reg &= ~HC_PUSHPULLEN;
422 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
423 1.1 kiyohara mutex_exit(&sc->sc_mtx);
424 1.1 kiyohara
425 1.1 kiyohara return 0;
426 1.1 kiyohara }
427 1.1 kiyohara
428 1.1 kiyohara static int
429 1.1 kiyohara mvsdio_bus_clock(sdmmc_chipset_handle_t sch, int freq)
430 1.1 kiyohara {
431 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
432 1.1 kiyohara uint32_t reg;
433 1.1 kiyohara int m;
434 1.1 kiyohara
435 1.1 kiyohara mutex_enter(&sc->sc_mtx);
436 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM);
437 1.1 kiyohara
438 1.1 kiyohara /* Just stop the clock. */
439 1.1 kiyohara if (freq == 0) {
440 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM,
441 1.1 kiyohara reg | TM_STOPCLKEN);
442 1.1 kiyohara goto out;
443 1.1 kiyohara }
444 1.1 kiyohara
445 1.1 kiyohara #define FREQ_TO_M(f) (100000 / (f) - 1)
446 1.1 kiyohara
447 1.1 kiyohara m = FREQ_TO_M(freq);
448 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_CDV,
449 1.1 kiyohara m & CDV_CLKDVDRMVALUE_MASK);
450 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM,
451 1.1 kiyohara reg & ~TM_STOPCLKEN);
452 1.1 kiyohara
453 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
454 1.1 kiyohara if (freq > 25000)
455 1.1 kiyohara reg |= HC_HISPEEDEN;
456 1.1 kiyohara else
457 1.1 kiyohara reg &= ~HC_HISPEEDEN; /* up to 25 MHz */
458 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
459 1.1 kiyohara
460 1.1 kiyohara out:
461 1.1 kiyohara mutex_exit(&sc->sc_mtx);
462 1.1 kiyohara
463 1.1 kiyohara return 0;
464 1.1 kiyohara }
465 1.1 kiyohara
466 1.1 kiyohara static int
467 1.1 kiyohara mvsdio_bus_width(sdmmc_chipset_handle_t sch, int width)
468 1.1 kiyohara {
469 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
470 1.1 kiyohara uint32_t reg, v;
471 1.1 kiyohara
472 1.1 kiyohara switch (width) {
473 1.1 kiyohara case 1:
474 1.1 kiyohara v = 0;
475 1.1 kiyohara break;
476 1.1 kiyohara
477 1.1 kiyohara case 4:
478 1.1 kiyohara v = HC_DATAWIDTH;
479 1.1 kiyohara break;
480 1.1 kiyohara
481 1.1 kiyohara default:
482 1.1 kiyohara DPRINTF(0, ("%s: unsupported bus width (%d)\n",
483 1.1 kiyohara device_xname(sc->sc_dev), width));
484 1.1 kiyohara return EINVAL;
485 1.1 kiyohara }
486 1.1 kiyohara
487 1.1 kiyohara mutex_enter(&sc->sc_mtx);
488 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
489 1.1 kiyohara reg &= ~HC_DATAWIDTH;
490 1.1 kiyohara reg |= v;
491 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
492 1.1 kiyohara mutex_exit(&sc->sc_mtx);
493 1.1 kiyohara
494 1.1 kiyohara return 0;
495 1.1 kiyohara }
496 1.1 kiyohara
497 1.1 kiyohara static int
498 1.1 kiyohara mvsdio_bus_rod(sdmmc_chipset_handle_t sch, int on)
499 1.1 kiyohara {
500 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
501 1.1 kiyohara uint32_t reg;
502 1.1 kiyohara
503 1.1 kiyohara /* Change Open-drain/Push-pull. */
504 1.1 kiyohara mutex_enter(&sc->sc_mtx);
505 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
506 1.1 kiyohara if (on)
507 1.1 kiyohara reg &= ~HC_PUSHPULLEN;
508 1.1 kiyohara else
509 1.1 kiyohara reg |= HC_PUSHPULLEN;
510 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, reg);
511 1.1 kiyohara mutex_exit(&sc->sc_mtx);
512 1.1 kiyohara
513 1.1 kiyohara return 0;
514 1.1 kiyohara }
515 1.1 kiyohara
516 1.1 kiyohara static void
517 1.1 kiyohara mvsdio_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
518 1.1 kiyohara {
519 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
520 1.1 kiyohara uint32_t tm, c, hc, aacc, nisie, wait;
521 1.1 kiyohara int blklen;
522 1.1 kiyohara
523 1.1 kiyohara DPRINTF(1, ("%s: start cmd %d arg=%#x data=%p dlen=%d flags=%#x\n",
524 1.1 kiyohara device_xname(sc->sc_dev), cmd->c_opcode, cmd->c_arg, cmd->c_data,
525 1.1 kiyohara cmd->c_datalen, cmd->c_flags));
526 1.1 kiyohara
527 1.1 kiyohara mutex_enter(&sc->sc_mtx);
528 1.1 kiyohara
529 1.1 kiyohara tm = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM);
530 1.1 kiyohara
531 1.1 kiyohara if (cmd->c_datalen > 0) {
532 1.1 kiyohara bus_dma_segment_t *dm_seg =
533 1.1 kiyohara &cmd->c_dmamap->dm_segs[cmd->c_dmaseg];
534 1.1 kiyohara bus_addr_t ds_addr = dm_seg->ds_addr + cmd->c_dmaoff;
535 1.1 kiyohara
536 1.1 kiyohara blklen = MIN(cmd->c_datalen, cmd->c_blklen);
537 1.1 kiyohara
538 1.1 kiyohara if (cmd->c_datalen % blklen > 0) {
539 1.1 kiyohara aprint_error_dev(sc->sc_dev,
540 1.1 kiyohara "data not a multiple of %u bytes\n", blklen);
541 1.1 kiyohara cmd->c_error = EINVAL;
542 1.1 kiyohara goto out;
543 1.1 kiyohara }
544 1.1 kiyohara if ((uint32_t)cmd->c_data & 0x3) {
545 1.1 kiyohara aprint_error_dev(sc->sc_dev,
546 1.1 kiyohara "data not 4byte aligned\n");
547 1.1 kiyohara cmd->c_error = EINVAL;
548 1.1 kiyohara goto out;
549 1.1 kiyohara }
550 1.1 kiyohara
551 1.1 kiyohara /* Set DMA Buffer Address */
552 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DMABA16LSB,
553 1.1 kiyohara ds_addr & 0xffff);
554 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DMABA16MSB,
555 1.1 kiyohara (ds_addr >> 16) & 0xffff);
556 1.1 kiyohara
557 1.1 kiyohara /* Set Data Block Size and Count */
558 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DBS,
559 1.1 kiyohara DBS_BLOCKSIZE(blklen));
560 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_DBC,
561 1.1 kiyohara DBC_BLOCKCOUNT(cmd->c_datalen / blklen));
562 1.1 kiyohara
563 1.1 kiyohara tm &= ~TM_HOSTXFERMODE; /* Always DMA */
564 1.1 kiyohara if (cmd->c_flags & SCF_CMD_READ)
565 1.1 kiyohara tm |= TM_DATAXFERTOWARDHOST;
566 1.1 kiyohara else
567 1.1 kiyohara tm &= ~TM_DATAXFERTOWARDHOST;
568 1.1 kiyohara tm |= TM_HWWRDATAEN;
569 1.1 kiyohara wait = NIS_XFERCOMPLETE;
570 1.1 kiyohara } else {
571 1.1 kiyohara tm &= ~TM_HWWRDATAEN;
572 1.1 kiyohara wait = NIS_CMDCOMPLETE;
573 1.1 kiyohara }
574 1.1 kiyohara
575 1.1 kiyohara /* Set Argument in Command */
576 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AC16LSB,
577 1.1 kiyohara cmd->c_arg & 0xffff);
578 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AC16MSB,
579 1.1 kiyohara (cmd->c_arg >> 16) & 0xffff);
580 1.1 kiyohara
581 1.1 kiyohara /* Set Host Control, exclude PushPullEn, DataWidth, HiSpeedEn. */
582 1.1 kiyohara hc = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC);
583 1.1 kiyohara hc |= (HC_TIMEOUTVALUE_MAX | HC_TIMEOUTEN);
584 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_HC, hc);
585 1.1 kiyohara
586 1.1 kiyohara /* Data Block Gap Control: Resume */
587 1.1 kiyohara
588 1.1 kiyohara /* Clock Control: SclkMasterEn */
589 1.1 kiyohara
590 1.1 kiyohara if (cmd->c_opcode == MMC_READ_BLOCK_MULTIPLE ||
591 1.1 kiyohara cmd->c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
592 1.1 kiyohara aacc = 0;
593 1.1 kiyohara #if 1 /* XXXX: need? */
594 1.1 kiyohara if (cmd->c_opcode == MMC_READ_BLOCK_MULTIPLE) {
595 1.1 kiyohara struct sdmmc_softc *sdmmc =
596 1.1 kiyohara device_private(sc->sc_sdmmc);
597 1.1 kiyohara struct sdmmc_function *sf = sdmmc->sc_card;
598 1.1 kiyohara
599 1.1 kiyohara aacc = MMC_ARG_RCA(sf->rca);
600 1.1 kiyohara }
601 1.1 kiyohara #endif
602 1.1 kiyohara
603 1.1 kiyohara /* Set Argument in Auto Cmd12 Command */
604 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AACC16LSBT,
605 1.1 kiyohara aacc & 0xffff);
606 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_AACC16MSBT,
607 1.1 kiyohara (aacc >> 16) & 0xffff);
608 1.1 kiyohara
609 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_IACCT,
610 1.1 kiyohara IACCT_AUTOCMD12BUSYCHKEN |
611 1.1 kiyohara IACCT_AUTOCMD12INDEXCHKEN |
612 1.1 kiyohara IACCT_AUTOCMD12INDEX);
613 1.1 kiyohara
614 1.1 kiyohara tm |= TM_AUTOCMD12EN;
615 1.1 kiyohara wait = NIS_AUTOCMD12COMPLETE;
616 1.1 kiyohara } else
617 1.1 kiyohara tm &= ~TM_AUTOCMD12EN;
618 1.1 kiyohara
619 1.1 kiyohara tm |= TM_INTCHKEN;
620 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_TM, tm);
621 1.1 kiyohara
622 1.1 kiyohara c = C_CMDINDEX(cmd->c_opcode);
623 1.1 kiyohara if (cmd->c_flags & SCF_RSP_PRESENT) {
624 1.1 kiyohara if (cmd->c_flags & SCF_RSP_136)
625 1.1 kiyohara c |= C_RESPTYPE_136BR;
626 1.1 kiyohara else if (!(cmd->c_flags & SCF_RSP_BSY))
627 1.1 kiyohara c |= C_RESPTYPE_48BR;
628 1.1 kiyohara else
629 1.1 kiyohara c |= C_RESPTYPE_48BRCB;
630 1.1 kiyohara c |= C_UNEXPECTEDRESPEN;
631 1.1 kiyohara } else
632 1.1 kiyohara c |= C_RESPTYPE_NR;
633 1.1 kiyohara if (cmd->c_flags & SCF_RSP_CRC)
634 1.1 kiyohara c |= C_CMDCRCCHKEN;
635 1.1 kiyohara if (cmd->c_flags & SCF_RSP_IDX)
636 1.1 kiyohara c |= C_CMDINDEXCHKEN;
637 1.1 kiyohara if (cmd->c_datalen > 0)
638 1.1 kiyohara c |= (C_DATAPRESENT | C_DATACRC16CHKEN);
639 1.1 kiyohara
640 1.1 kiyohara DPRINTF(2, ("%s: TM=0x%x, C=0x%x, HC=0x%x\n", __func__, tm, c, hc));
641 1.1 kiyohara
642 1.1 kiyohara nisie = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE);
643 1.1 kiyohara nisie &= ~(NIS_CMDCOMPLETE | NIS_XFERCOMPLETE | NIS_AUTOCMD12COMPLETE);
644 1.1 kiyohara nisie |= wait;
645 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE, nisie);
646 1.1 kiyohara
647 1.1 kiyohara /* Execute command */
648 1.1 kiyohara sc->sc_exec_cmd = cmd;
649 1.1 kiyohara sc->sc_waitintr = wait;
650 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_C, c);
651 1.1 kiyohara
652 1.1 kiyohara /* Wait interrupt for complete or error or timeout */
653 1.1 kiyohara while (sc->sc_exec_cmd == cmd)
654 1.1 kiyohara cv_wait(&sc->sc_cv, &sc->sc_mtx);
655 1.1 kiyohara
656 1.1 kiyohara out:
657 1.1 kiyohara mutex_exit(&sc->sc_mtx);
658 1.1 kiyohara
659 1.1 kiyohara DPRINTF(1, ("%s: cmd %d done (flags=%08x error=%d)\n",
660 1.1 kiyohara device_xname(sc->sc_dev),
661 1.1 kiyohara cmd->c_opcode, cmd->c_flags, cmd->c_error));
662 1.1 kiyohara }
663 1.1 kiyohara
664 1.1 kiyohara static void
665 1.1 kiyohara mvsdio_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
666 1.1 kiyohara {
667 1.1 kiyohara struct mvsdio_softc *sc = (struct mvsdio_softc *)sch;
668 1.1 kiyohara uint32_t reg;
669 1.1 kiyohara
670 1.1 kiyohara mutex_enter(&sc->sc_mtx);
671 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE);
672 1.1 kiyohara reg |= NIS_CARDINT;
673 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_NISIE, reg);
674 1.1 kiyohara mutex_exit(&sc->sc_mtx);
675 1.1 kiyohara }
676 1.1 kiyohara
677 1.1 kiyohara static void
678 1.1 kiyohara mvsdio_card_intr_ack(sdmmc_chipset_handle_t sch)
679 1.1 kiyohara {
680 1.1 kiyohara
681 1.1 kiyohara /* Nothing */
682 1.1 kiyohara }
683 1.1 kiyohara
684 1.1 kiyohara
685 1.1 kiyohara static void
686 1.5 kiyohara mvsdio_wininit(struct mvsdio_softc *sc, enum marvell_tags *tags)
687 1.1 kiyohara {
688 1.1 kiyohara uint64_t base;
689 1.1 kiyohara uint32_t size;
690 1.1 kiyohara int window, target, attr, rv, i;
691 1.1 kiyohara
692 1.1 kiyohara for (window = 0, i = 0;
693 1.1 kiyohara tags[i] != MARVELL_TAG_UNDEFINED && window < MVSDIO_NWINDOW; i++) {
694 1.1 kiyohara rv = marvell_winparams_by_tag(sc->sc_dev, tags[i],
695 1.1 kiyohara &target, &attr, &base, &size);
696 1.1 kiyohara if (rv != 0 || size == 0)
697 1.1 kiyohara continue;
698 1.1 kiyohara
699 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_WC(window),
700 1.1 kiyohara WC_WINEN |
701 1.1 kiyohara WC_TARGET(target) |
702 1.1 kiyohara WC_ATTR(attr) |
703 1.1 kiyohara WC_SIZE(size));
704 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_WB(window),
705 1.1 kiyohara WB_BASE(base));
706 1.1 kiyohara window++;
707 1.1 kiyohara }
708 1.1 kiyohara for (; window < MVSDIO_NWINDOW; window++)
709 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSDIO_WC(window), 0);
710 1.1 kiyohara }
711