w83l518d_sdmmc.c revision 1.1 1 1.1 jmcneill /* $NetBSD: w83l518d_sdmmc.c,v 1.1 2009/09/30 20:44:50 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill #include <sys/cdefs.h>
29 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: w83l518d_sdmmc.c,v 1.1 2009/09/30 20:44:50 jmcneill Exp $");
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/param.h>
32 1.1 jmcneill #include <sys/kernel.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/errno.h>
35 1.1 jmcneill #include <sys/ioctl.h>
36 1.1 jmcneill #include <sys/syslog.h>
37 1.1 jmcneill #include <sys/device.h>
38 1.1 jmcneill #include <sys/proc.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <sys/bus.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h>
43 1.1 jmcneill #include <dev/sdmmc/sdmmcchip.h>
44 1.1 jmcneill #include <dev/sdmmc/sdmmc_ioreg.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <dev/isa/isavar.h>
47 1.1 jmcneill #include <dev/isa/isadmavar.h>
48 1.1 jmcneill
49 1.1 jmcneill #include <dev/ic/w83l518dreg.h>
50 1.1 jmcneill #include <dev/ic/w83l518dvar.h>
51 1.1 jmcneill #include <dev/ic/w83l518d_sdmmc.h>
52 1.1 jmcneill
53 1.1 jmcneill /* #define WB_SDMMC_DEBUG */
54 1.1 jmcneill
55 1.1 jmcneill #ifdef WB_SDMMC_DEBUG
56 1.1 jmcneill static int wb_sdmmc_debug = 1;
57 1.1 jmcneill #else
58 1.1 jmcneill static int wb_sdmmc_debug = 0;
59 1.1 jmcneill #endif
60 1.1 jmcneill
61 1.1 jmcneill #if defined(__NetBSD__) && __NetBSD_Version__ < 599000600
62 1.1 jmcneill #define snprintb(b, l, f, v) bitmask_snprintf((v), (f), (b), (l))
63 1.1 jmcneill #endif
64 1.1 jmcneill
65 1.1 jmcneill #define REPORT(_wb, ...) \
66 1.1 jmcneill if (wb_sdmmc_debug > 0) \
67 1.1 jmcneill aprint_normal_dev(((struct wb_softc *)(_wb))->wb_dev, \
68 1.1 jmcneill __VA_ARGS__)
69 1.1 jmcneill
70 1.1 jmcneill static int wb_sdmmc_host_reset(sdmmc_chipset_handle_t);
71 1.1 jmcneill static uint32_t wb_sdmmc_host_ocr(sdmmc_chipset_handle_t);
72 1.1 jmcneill static int wb_sdmmc_host_maxblklen(sdmmc_chipset_handle_t);
73 1.1 jmcneill static int wb_sdmmc_card_detect(sdmmc_chipset_handle_t);
74 1.1 jmcneill static int wb_sdmmc_write_protect(sdmmc_chipset_handle_t);
75 1.1 jmcneill static int wb_sdmmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
76 1.1 jmcneill static int wb_sdmmc_bus_clock(sdmmc_chipset_handle_t, int);
77 1.1 jmcneill static int wb_sdmmc_bus_width(sdmmc_chipset_handle_t, int);
78 1.1 jmcneill static void wb_sdmmc_exec_command(sdmmc_chipset_handle_t,
79 1.1 jmcneill struct sdmmc_command *);
80 1.1 jmcneill static void wb_sdmmc_card_enable_intr(sdmmc_chipset_handle_t, int);
81 1.1 jmcneill static void wb_sdmmc_card_intr_ack(sdmmc_chipset_handle_t);
82 1.1 jmcneill
83 1.1 jmcneill static struct sdmmc_chip_functions wb_sdmmc_chip_functions = {
84 1.1 jmcneill .host_reset = wb_sdmmc_host_reset,
85 1.1 jmcneill .host_ocr = wb_sdmmc_host_ocr,
86 1.1 jmcneill .host_maxblklen = wb_sdmmc_host_maxblklen,
87 1.1 jmcneill .card_detect = wb_sdmmc_card_detect,
88 1.1 jmcneill .write_protect = wb_sdmmc_write_protect,
89 1.1 jmcneill .bus_power = wb_sdmmc_bus_power,
90 1.1 jmcneill .bus_clock = wb_sdmmc_bus_clock,
91 1.1 jmcneill .bus_width = wb_sdmmc_bus_width,
92 1.1 jmcneill .exec_command = wb_sdmmc_exec_command,
93 1.1 jmcneill .card_enable_intr = wb_sdmmc_card_enable_intr,
94 1.1 jmcneill .card_intr_ack = wb_sdmmc_card_intr_ack,
95 1.1 jmcneill };
96 1.1 jmcneill
97 1.1 jmcneill static void
98 1.1 jmcneill wb_sdmmc_read_data(struct wb_softc *wb, uint8_t *data, int len)
99 1.1 jmcneill {
100 1.1 jmcneill bus_space_read_multi_1(wb->wb_iot, wb->wb_ioh, WB_SD_FIFO, data, len);
101 1.1 jmcneill }
102 1.1 jmcneill
103 1.1 jmcneill static void
104 1.1 jmcneill wb_sdmmc_write_data(struct wb_softc *wb, uint8_t *data, int len)
105 1.1 jmcneill {
106 1.1 jmcneill bus_space_write_multi_1(wb->wb_iot, wb->wb_ioh, WB_SD_FIFO, data, len);
107 1.1 jmcneill }
108 1.1 jmcneill
109 1.1 jmcneill static void
110 1.1 jmcneill wb_sdmmc_discover(void *opaque)
111 1.1 jmcneill {
112 1.1 jmcneill struct wb_softc *wb = opaque;
113 1.1 jmcneill
114 1.1 jmcneill REPORT(wb, "TRACE: discover(wb)\n");
115 1.1 jmcneill
116 1.1 jmcneill sdmmc_needs_discover(wb->wb_sdmmc_dev);
117 1.1 jmcneill }
118 1.1 jmcneill
119 1.1 jmcneill static bool
120 1.1 jmcneill wb_sdmmc_enable(struct wb_softc *wb)
121 1.1 jmcneill {
122 1.1 jmcneill int i = 5000;
123 1.1 jmcneill
124 1.1 jmcneill REPORT(wb, "TRACE: enable(wb)\n");
125 1.1 jmcneill
126 1.1 jmcneill /* put the device in a known state */
127 1.1 jmcneill wb_idx_write(wb, WB_INDEX_SETUP, WB_SETUP_SOFT_RST);
128 1.1 jmcneill while (--i > 0 && wb_idx_read(wb, WB_INDEX_SETUP) & WB_SETUP_SOFT_RST)
129 1.1 jmcneill delay(10);
130 1.1 jmcneill if (i == 0) {
131 1.1 jmcneill aprint_error_dev(wb->wb_dev, "timeout resetting device\n");
132 1.1 jmcneill return false;
133 1.1 jmcneill }
134 1.1 jmcneill wb_idx_write(wb, WB_INDEX_CLK, WB_CLK_375K);
135 1.1 jmcneill wb_idx_write(wb, WB_INDEX_FIFOEN, 0);
136 1.1 jmcneill wb_idx_write(wb, WB_INDEX_DMA, 0);
137 1.1 jmcneill wb_idx_write(wb, WB_INDEX_PBSMSB, 0);
138 1.1 jmcneill wb_idx_write(wb, WB_INDEX_PBSLSB, 0);
139 1.1 jmcneill /* drain FIFO */
140 1.1 jmcneill while ((wb_read(wb, WB_SD_FIFOSTS) & WB_FIFO_EMPTY) == 0)
141 1.1 jmcneill wb_read(wb, WB_SD_FIFO);
142 1.1 jmcneill
143 1.1 jmcneill wb_write(wb, WB_SD_CSR, 0);
144 1.1 jmcneill
145 1.1 jmcneill wb_write(wb, WB_SD_INTCTL, WB_INT_DEFAULT);
146 1.1 jmcneill
147 1.1 jmcneill wb_sdmmc_card_detect(wb);
148 1.1 jmcneill
149 1.1 jmcneill return true;
150 1.1 jmcneill }
151 1.1 jmcneill
152 1.1 jmcneill static bool
153 1.1 jmcneill wb_sdmmc_disable(struct wb_softc *wb)
154 1.1 jmcneill {
155 1.1 jmcneill uint8_t val;
156 1.1 jmcneill
157 1.1 jmcneill REPORT(wb, "TRACE: disable(wb)\n");
158 1.1 jmcneill
159 1.1 jmcneill val = wb_read(wb, WB_SD_CSR);
160 1.1 jmcneill val |= WB_CSR_POWER_N;
161 1.1 jmcneill wb_write(wb, WB_SD_CSR, val);
162 1.1 jmcneill
163 1.1 jmcneill return true;
164 1.1 jmcneill }
165 1.1 jmcneill
166 1.1 jmcneill void
167 1.1 jmcneill wb_sdmmc_attach(struct wb_softc *wb)
168 1.1 jmcneill {
169 1.1 jmcneill struct sdmmcbus_attach_args saa;
170 1.1 jmcneill
171 1.1 jmcneill callout_init(&wb->wb_sdmmc_callout, 0);
172 1.1 jmcneill callout_setfunc(&wb->wb_sdmmc_callout, wb_sdmmc_discover, wb);
173 1.1 jmcneill
174 1.1 jmcneill wb->wb_sdmmc_width = 1;
175 1.1 jmcneill
176 1.1 jmcneill if (wb_sdmmc_enable(wb) == false)
177 1.1 jmcneill return;
178 1.1 jmcneill
179 1.1 jmcneill memset(&saa, 0, sizeof(saa));
180 1.1 jmcneill saa.saa_busname = "sdmmc";
181 1.1 jmcneill saa.saa_sct = &wb_sdmmc_chip_functions;
182 1.1 jmcneill saa.saa_sch = wb;
183 1.1 jmcneill saa.saa_clkmin = 375;
184 1.1 jmcneill saa.saa_clkmax = 24000;
185 1.1 jmcneill saa.saa_caps = SMC_CAPS_4BIT_MODE;
186 1.1 jmcneill
187 1.1 jmcneill wb->wb_sdmmc_dev = config_found(wb->wb_dev, &saa, NULL);
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill int
191 1.1 jmcneill wb_sdmmc_detach(struct wb_softc *wb, int flags)
192 1.1 jmcneill {
193 1.1 jmcneill int rv;
194 1.1 jmcneill
195 1.1 jmcneill if (wb->wb_sdmmc_dev) {
196 1.1 jmcneill rv = config_detach(wb->wb_sdmmc_dev, flags);
197 1.1 jmcneill if (rv)
198 1.1 jmcneill return rv;
199 1.1 jmcneill }
200 1.1 jmcneill wb_sdmmc_disable(wb);
201 1.1 jmcneill
202 1.1 jmcneill callout_halt(&wb->wb_sdmmc_callout, NULL);
203 1.1 jmcneill callout_destroy(&wb->wb_sdmmc_callout);
204 1.1 jmcneill
205 1.1 jmcneill return 0;
206 1.1 jmcneill }
207 1.1 jmcneill
208 1.1 jmcneill /*
209 1.1 jmcneill * SD/MMC interface
210 1.1 jmcneill */
211 1.1 jmcneill static int
212 1.1 jmcneill wb_sdmmc_host_reset(sdmmc_chipset_handle_t sch)
213 1.1 jmcneill {
214 1.1 jmcneill REPORT(sch, "TRACE: sdmmc/host_reset(wb)\n");
215 1.1 jmcneill
216 1.1 jmcneill return 0;
217 1.1 jmcneill }
218 1.1 jmcneill
219 1.1 jmcneill static uint32_t
220 1.1 jmcneill wb_sdmmc_host_ocr(sdmmc_chipset_handle_t sch)
221 1.1 jmcneill {
222 1.1 jmcneill REPORT(sch, "TRACE: sdmmc/host_ocr(wb)\n");
223 1.1 jmcneill
224 1.1 jmcneill return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
225 1.1 jmcneill }
226 1.1 jmcneill
227 1.1 jmcneill static int
228 1.1 jmcneill wb_sdmmc_host_maxblklen(sdmmc_chipset_handle_t sch)
229 1.1 jmcneill {
230 1.1 jmcneill REPORT(sch, "TRACE: sdmmc/host_maxblklen(wb)\n");
231 1.1 jmcneill
232 1.1 jmcneill return 512; /* XXX */
233 1.1 jmcneill }
234 1.1 jmcneill
235 1.1 jmcneill static int
236 1.1 jmcneill wb_sdmmc_card_detect(sdmmc_chipset_handle_t sch)
237 1.1 jmcneill {
238 1.1 jmcneill struct wb_softc *wb = sch;
239 1.1 jmcneill int rv;
240 1.1 jmcneill
241 1.1 jmcneill wb_led(wb, true);
242 1.1 jmcneill rv = (wb_read(wb, WB_SD_CSR) & WB_CSR_CARD_PRESENT) ? 1 : 0;
243 1.1 jmcneill wb_led(wb, false);
244 1.1 jmcneill
245 1.1 jmcneill REPORT(wb, "TRACE: sdmmc/card_detect(wb) -> %d\n", rv);
246 1.1 jmcneill
247 1.1 jmcneill return rv;
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.1 jmcneill static int
251 1.1 jmcneill wb_sdmmc_write_protect(sdmmc_chipset_handle_t sch)
252 1.1 jmcneill {
253 1.1 jmcneill struct wb_softc *wb = sch;
254 1.1 jmcneill int rv;
255 1.1 jmcneill
256 1.1 jmcneill wb_led(wb, true);
257 1.1 jmcneill rv = (wb_read(wb, WB_SD_CSR) & WB_CSR_WRITE_PROTECT) ? 1 : 0;
258 1.1 jmcneill wb_led(wb, false);
259 1.1 jmcneill
260 1.1 jmcneill REPORT(wb, "TRACE: sdmmc/write_protect(wb) -> %d\n", rv);
261 1.1 jmcneill
262 1.1 jmcneill return rv;
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill static int
266 1.1 jmcneill wb_sdmmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
267 1.1 jmcneill {
268 1.1 jmcneill REPORT(sch, "TRACE: sdmmc/bus_power(wb, ocr=%d)\n", ocr);
269 1.1 jmcneill
270 1.1 jmcneill return 0;
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill static int
274 1.1 jmcneill wb_sdmmc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
275 1.1 jmcneill {
276 1.1 jmcneill struct wb_softc *wb = sch;
277 1.1 jmcneill uint8_t clk;
278 1.1 jmcneill
279 1.1 jmcneill REPORT(wb, "TRACE: sdmmc/bus_clock(wb, freq=%d)\n", freq);
280 1.1 jmcneill
281 1.1 jmcneill if (freq >= 24000)
282 1.1 jmcneill clk = WB_CLK_24M;
283 1.1 jmcneill else if (freq >= 16000)
284 1.1 jmcneill clk = WB_CLK_16M;
285 1.1 jmcneill else if (freq >= 12000)
286 1.1 jmcneill clk = WB_CLK_12M;
287 1.1 jmcneill else
288 1.1 jmcneill clk = WB_CLK_375K;
289 1.1 jmcneill
290 1.1 jmcneill if (wb_idx_read(wb, WB_INDEX_CLK) != clk)
291 1.1 jmcneill wb_idx_write(wb, WB_INDEX_CLK, clk);
292 1.1 jmcneill
293 1.1 jmcneill return 0;
294 1.1 jmcneill }
295 1.1 jmcneill
296 1.1 jmcneill static int
297 1.1 jmcneill wb_sdmmc_bus_width(sdmmc_chipset_handle_t sch, int width)
298 1.1 jmcneill {
299 1.1 jmcneill struct wb_softc *wb = sch;
300 1.1 jmcneill
301 1.1 jmcneill REPORT(wb, "TRACE: sdmmc/bus_width(wb, width=%d)\n", width);
302 1.1 jmcneill
303 1.1 jmcneill if (width != 1 && width != 4)
304 1.1 jmcneill return 1;
305 1.1 jmcneill
306 1.1 jmcneill wb->wb_sdmmc_width = width;
307 1.1 jmcneill
308 1.1 jmcneill return 0;
309 1.1 jmcneill }
310 1.1 jmcneill
311 1.1 jmcneill
312 1.1 jmcneill static void
313 1.1 jmcneill wb_sdmmc_rsp_read_long(struct wb_softc *wb, struct sdmmc_command *cmd)
314 1.1 jmcneill {
315 1.1 jmcneill uint8_t *p = (uint8_t *)cmd->c_resp;
316 1.1 jmcneill int i;
317 1.1 jmcneill
318 1.1 jmcneill if (wb_idx_read(wb, WB_INDEX_RESPLEN) != 1) {
319 1.1 jmcneill cmd->c_error = ENXIO;
320 1.1 jmcneill return;
321 1.1 jmcneill }
322 1.1 jmcneill
323 1.1 jmcneill for (i = 12; i >= 0; i -= 4) {
324 1.1 jmcneill p[3] = wb_idx_read(wb, WB_INDEX_RESP(i + 0));
325 1.1 jmcneill p[2] = wb_idx_read(wb, WB_INDEX_RESP(i + 1));
326 1.1 jmcneill p[1] = wb_idx_read(wb, WB_INDEX_RESP(i + 2));
327 1.1 jmcneill p[0] = wb_idx_read(wb, WB_INDEX_RESP(i + 3));
328 1.1 jmcneill p += 4;
329 1.1 jmcneill }
330 1.1 jmcneill }
331 1.1 jmcneill
332 1.1 jmcneill static void
333 1.1 jmcneill wb_sdmmc_rsp_read_short(struct wb_softc *wb, struct sdmmc_command *cmd)
334 1.1 jmcneill {
335 1.1 jmcneill uint8_t *p = (uint8_t *)cmd->c_resp;
336 1.1 jmcneill
337 1.1 jmcneill if (wb_idx_read(wb, WB_INDEX_RESPLEN) != 0) {
338 1.1 jmcneill cmd->c_error = ENXIO;
339 1.1 jmcneill return;
340 1.1 jmcneill }
341 1.1 jmcneill
342 1.1 jmcneill p[3] = wb_idx_read(wb, WB_INDEX_RESP(12));
343 1.1 jmcneill p[2] = wb_idx_read(wb, WB_INDEX_RESP(13));
344 1.1 jmcneill p[1] = wb_idx_read(wb, WB_INDEX_RESP(14));
345 1.1 jmcneill p[0] = wb_idx_read(wb, WB_INDEX_RESP(15));
346 1.1 jmcneill }
347 1.1 jmcneill
348 1.1 jmcneill static int
349 1.1 jmcneill wb_sdmmc_transfer_data(struct wb_softc *wb, struct sdmmc_command *cmd)
350 1.1 jmcneill {
351 1.1 jmcneill uint8_t fifosts;
352 1.1 jmcneill int datalen, retry = 5000;
353 1.1 jmcneill
354 1.1 jmcneill if (wb->wb_sdmmc_intsts & WB_INT_CARD)
355 1.1 jmcneill return EIO;
356 1.1 jmcneill
357 1.1 jmcneill fifosts = wb_read(wb, WB_SD_FIFOSTS);
358 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
359 1.1 jmcneill if (fifosts & WB_FIFO_EMPTY) {
360 1.1 jmcneill while (--retry > 0) {
361 1.1 jmcneill fifosts = wb_read(wb, WB_SD_FIFOSTS);
362 1.1 jmcneill if ((fifosts & WB_FIFO_EMPTY) == 0)
363 1.1 jmcneill break;
364 1.1 jmcneill delay(100);
365 1.1 jmcneill }
366 1.1 jmcneill if (retry == 0)
367 1.1 jmcneill return EBUSY;
368 1.1 jmcneill }
369 1.1 jmcneill
370 1.1 jmcneill if (fifosts & WB_FIFO_FULL)
371 1.1 jmcneill datalen = 16;
372 1.1 jmcneill else
373 1.1 jmcneill datalen = fifosts & WB_FIFO_DEPTH_MASK;
374 1.1 jmcneill } else {
375 1.1 jmcneill if (fifosts & WB_FIFO_FULL) {
376 1.1 jmcneill while (--retry > 0) {
377 1.1 jmcneill fifosts = wb_read(wb, WB_SD_FIFOSTS);
378 1.1 jmcneill if ((fifosts & WB_FIFO_FULL) == 0)
379 1.1 jmcneill break;
380 1.1 jmcneill delay(100);
381 1.1 jmcneill }
382 1.1 jmcneill if (retry == 0)
383 1.1 jmcneill return EBUSY;
384 1.1 jmcneill }
385 1.1 jmcneill
386 1.1 jmcneill if (fifosts & WB_FIFO_EMPTY)
387 1.1 jmcneill datalen = 16;
388 1.1 jmcneill else
389 1.1 jmcneill datalen = 16 - (fifosts & WB_FIFO_DEPTH_MASK);
390 1.1 jmcneill }
391 1.1 jmcneill
392 1.1 jmcneill datalen = MIN(datalen, cmd->c_resid);
393 1.1 jmcneill if (datalen > 0) {
394 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ))
395 1.1 jmcneill wb_sdmmc_read_data(wb, cmd->c_buf, datalen);
396 1.1 jmcneill else
397 1.1 jmcneill wb_sdmmc_write_data(wb, cmd->c_buf, datalen);
398 1.1 jmcneill
399 1.1 jmcneill cmd->c_buf += datalen;
400 1.1 jmcneill cmd->c_resid -= datalen;
401 1.1 jmcneill }
402 1.1 jmcneill
403 1.1 jmcneill return 0;
404 1.1 jmcneill }
405 1.1 jmcneill
406 1.1 jmcneill static void
407 1.1 jmcneill wb_sdmmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
408 1.1 jmcneill {
409 1.1 jmcneill static const int opcodes[] = {
410 1.1 jmcneill 11, 17, 18, 20, 24, 25, 26, 27, 30, 42, 51, 56
411 1.1 jmcneill };
412 1.1 jmcneill struct wb_softc *wb = sch;
413 1.1 jmcneill uint8_t val;
414 1.1 jmcneill int blklen;
415 1.1 jmcneill int error;
416 1.1 jmcneill int i, retry;
417 1.1 jmcneill int s;
418 1.1 jmcneill
419 1.1 jmcneill REPORT(wb, "TRACE: sdmmc/exec_command(wb, cmd) "
420 1.1 jmcneill "opcode %d flags 0x%x data %p datalen %d\n",
421 1.1 jmcneill cmd->c_opcode, cmd->c_flags, cmd->c_data, cmd->c_datalen);
422 1.1 jmcneill
423 1.1 jmcneill if (cmd->c_datalen > 0) {
424 1.1 jmcneill /* controller only supports a select number of data opcodes */
425 1.1 jmcneill for (i = 0; i < __arraycount(opcodes); i++)
426 1.1 jmcneill if (opcodes[i] == cmd->c_opcode)
427 1.1 jmcneill break;
428 1.1 jmcneill if (i == __arraycount(opcodes)) {
429 1.1 jmcneill cmd->c_error = EINVAL;
430 1.1 jmcneill goto done;
431 1.1 jmcneill }
432 1.1 jmcneill
433 1.1 jmcneill /* Fragment the data into proper blocks */
434 1.1 jmcneill blklen = MIN(cmd->c_datalen, cmd->c_blklen);
435 1.1 jmcneill
436 1.1 jmcneill if (cmd->c_datalen % blklen > 0) {
437 1.1 jmcneill aprint_error_dev(wb->wb_dev,
438 1.1 jmcneill "data is not a multiple of %u bytes\n", blklen);
439 1.1 jmcneill cmd->c_error = EINVAL;
440 1.1 jmcneill goto done;
441 1.1 jmcneill }
442 1.1 jmcneill
443 1.1 jmcneill /* setup block size registers */
444 1.1 jmcneill blklen = blklen + 2 * wb->wb_sdmmc_width;
445 1.1 jmcneill wb_idx_write(wb, WB_INDEX_PBSMSB,
446 1.1 jmcneill ((blklen >> 4) & 0xf0) | (wb->wb_sdmmc_width / 4));
447 1.1 jmcneill wb_idx_write(wb, WB_INDEX_PBSLSB, blklen & 0xff);
448 1.1 jmcneill
449 1.1 jmcneill /* clear FIFO */
450 1.1 jmcneill val = wb_idx_read(wb, WB_INDEX_SETUP);
451 1.1 jmcneill val |= WB_SETUP_FIFO_RST;
452 1.1 jmcneill wb_idx_write(wb, WB_INDEX_SETUP, val);
453 1.1 jmcneill while (wb_idx_read(wb, WB_INDEX_SETUP) & WB_SETUP_FIFO_RST)
454 1.1 jmcneill ;
455 1.1 jmcneill
456 1.1 jmcneill cmd->c_resid = cmd->c_datalen;
457 1.1 jmcneill cmd->c_buf = cmd->c_data;
458 1.1 jmcneill
459 1.1 jmcneill /* setup FIFO thresholds */
460 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ))
461 1.1 jmcneill wb_idx_write(wb, WB_INDEX_FIFOEN, WB_FIFOEN_FULL | 8);
462 1.1 jmcneill else {
463 1.1 jmcneill wb_idx_write(wb, WB_INDEX_FIFOEN, WB_FIFOEN_EMPTY | 8);
464 1.1 jmcneill
465 1.1 jmcneill /* pre-fill the FIFO on write */
466 1.1 jmcneill error = wb_sdmmc_transfer_data(wb, cmd);
467 1.1 jmcneill if (error) {
468 1.1 jmcneill cmd->c_error = error;
469 1.1 jmcneill goto done;
470 1.1 jmcneill }
471 1.1 jmcneill }
472 1.1 jmcneill }
473 1.1 jmcneill
474 1.1 jmcneill s = splsdmmc();
475 1.1 jmcneill wb->wb_sdmmc_intsts = 0;
476 1.1 jmcneill wb_write(wb, WB_SD_COMMAND, cmd->c_opcode);
477 1.1 jmcneill wb_write(wb, WB_SD_COMMAND, (cmd->c_arg >> 24) & 0xff);
478 1.1 jmcneill wb_write(wb, WB_SD_COMMAND, (cmd->c_arg >> 16) & 0xff);
479 1.1 jmcneill wb_write(wb, WB_SD_COMMAND, (cmd->c_arg >> 8) & 0xff);
480 1.1 jmcneill wb_write(wb, WB_SD_COMMAND, (cmd->c_arg >> 0) & 0xff);
481 1.1 jmcneill splx(s);
482 1.1 jmcneill
483 1.1 jmcneill retry = 100000;
484 1.1 jmcneill while (wb_idx_read(wb, WB_INDEX_STATUS) & WB_STATUS_CARD_TRAFFIC) {
485 1.1 jmcneill if (--retry == 0)
486 1.1 jmcneill break;
487 1.1 jmcneill delay(1);
488 1.1 jmcneill }
489 1.1 jmcneill if (wb_idx_read(wb, WB_INDEX_STATUS) & WB_STATUS_CARD_TRAFFIC) {
490 1.1 jmcneill REPORT(wb,
491 1.1 jmcneill "command timed out, WB_INDEX_STATUS = 0x%02x\n",
492 1.1 jmcneill wb_idx_read(wb, WB_INDEX_STATUS));
493 1.1 jmcneill cmd->c_error = ETIMEDOUT;
494 1.1 jmcneill goto done;
495 1.1 jmcneill }
496 1.1 jmcneill
497 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
498 1.1 jmcneill if (wb->wb_sdmmc_intsts & WB_INT_TIMEOUT) {
499 1.1 jmcneill cmd->c_error = ETIMEDOUT;
500 1.1 jmcneill goto done;
501 1.1 jmcneill }
502 1.1 jmcneill
503 1.1 jmcneill if (ISSET(cmd->c_flags, SCF_RSP_136))
504 1.1 jmcneill wb_sdmmc_rsp_read_long(wb, cmd);
505 1.1 jmcneill else
506 1.1 jmcneill wb_sdmmc_rsp_read_short(wb, cmd);
507 1.1 jmcneill }
508 1.1 jmcneill
509 1.1 jmcneill if (cmd->c_error == 0 && cmd->c_datalen > 0) {
510 1.1 jmcneill wb_led(wb, true);
511 1.1 jmcneill while (cmd->c_resid > 0) {
512 1.1 jmcneill error = wb_sdmmc_transfer_data(wb, cmd);
513 1.1 jmcneill if (error) {
514 1.1 jmcneill cmd->c_error = error;
515 1.1 jmcneill break;
516 1.1 jmcneill }
517 1.1 jmcneill }
518 1.1 jmcneill wb_led(wb, false);
519 1.1 jmcneill }
520 1.1 jmcneill
521 1.1 jmcneill done:
522 1.1 jmcneill SET(cmd->c_flags, SCF_ITSDONE);
523 1.1 jmcneill
524 1.1 jmcneill if (cmd->c_error) {
525 1.1 jmcneill REPORT(wb,
526 1.1 jmcneill "cmd error = %d, op = %d [%s] "
527 1.1 jmcneill "blklen %d datalen %d resid %d\n",
528 1.1 jmcneill cmd->c_error, cmd->c_opcode,
529 1.1 jmcneill ISSET(cmd->c_flags, SCF_CMD_READ) ? "rd" : "wr",
530 1.1 jmcneill cmd->c_blklen, cmd->c_datalen, cmd->c_resid);
531 1.1 jmcneill }
532 1.1 jmcneill }
533 1.1 jmcneill
534 1.1 jmcneill static void
535 1.1 jmcneill wb_sdmmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
536 1.1 jmcneill {
537 1.1 jmcneill REPORT(sch, "TRACE: sdmmc/card_enable_intr(wb, enable=%d)\n", enable);
538 1.1 jmcneill }
539 1.1 jmcneill
540 1.1 jmcneill static void
541 1.1 jmcneill wb_sdmmc_card_intr_ack(sdmmc_chipset_handle_t sch)
542 1.1 jmcneill {
543 1.1 jmcneill REPORT(sch, "TRACE: sdmmc/card_intr_ack(wb)\n");
544 1.1 jmcneill }
545 1.1 jmcneill
546 1.1 jmcneill /*
547 1.1 jmcneill * intr handler
548 1.1 jmcneill */
549 1.1 jmcneill int
550 1.1 jmcneill wb_sdmmc_intr(struct wb_softc *wb)
551 1.1 jmcneill {
552 1.1 jmcneill uint8_t val;
553 1.1 jmcneill
554 1.1 jmcneill val = wb_read(wb, WB_SD_INTSTS);
555 1.1 jmcneill if (val == 0xff || val == 0x00)
556 1.1 jmcneill return 0;
557 1.1 jmcneill
558 1.1 jmcneill if (wb->wb_sdmmc_dev == NULL)
559 1.1 jmcneill return 1;
560 1.1 jmcneill
561 1.1 jmcneill wb->wb_sdmmc_intsts |= val;
562 1.1 jmcneill
563 1.1 jmcneill if (wb_sdmmc_debug) {
564 1.1 jmcneill char buf[64];
565 1.1 jmcneill snprintb(buf, sizeof(buf),
566 1.1 jmcneill "\20\1TC\2BUSYEND\3PROGEND\4TIMEOUT"
567 1.1 jmcneill "\5CRC\6FIFO\7CARD\010PENDING",
568 1.1 jmcneill val);
569 1.1 jmcneill REPORT(wb, "WB_SD_INTSTS = %s\n", buf);
570 1.1 jmcneill }
571 1.1 jmcneill
572 1.1 jmcneill if (val & WB_INT_CARD)
573 1.1 jmcneill callout_schedule(&wb->wb_sdmmc_callout, hz / 4);
574 1.1 jmcneill
575 1.1 jmcneill return 1;
576 1.1 jmcneill }
577