ti_sdhc.c revision 1.2 1 /* $NetBSD: ti_sdhc.c,v 1.2 2019/10/27 17:21:23 jmcneill Exp $ */
2 /*-
3 * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Matt Thomas of 3am Software Foundry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ti_sdhc.c,v 1.2 2019/10/27 17:21:23 jmcneill Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/errno.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/queue.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/bus.h>
44
45 #include <arm/ti/ti_prcm.h>
46 #include <arm/ti/ti_edma.h>
47 #include <arm/ti/ti_sdhcreg.h>
48
49 #include <dev/sdmmc/sdhcreg.h>
50 #include <dev/sdmmc/sdhcvar.h>
51 #include <dev/sdmmc/sdmmcvar.h>
52
53 #include <dev/fdt/fdtvar.h>
54
55 #define EDMA_MAX_PARAMS 32
56
57 #ifdef TISDHC_DEBUG
58 int tisdhcdebug = 1;
59 #define DPRINTF(n,s) do { if ((n) <= tisdhcdebug) device_printf s; } while (0)
60 #else
61 #define DPRINTF(n,s) do {} while (0)
62 #endif
63
64
65 #define CLKD(kz) (sc->sc.sc_clkbase / (kz))
66
67 #define SDHC_READ(sc, reg) \
68 bus_space_read_4((sc)->sc_bst, (sc)->sc_sdhc_bsh, (reg))
69 #define SDHC_WRITE(sc, reg, val) \
70 bus_space_write_4((sc)->sc_bst, (sc)->sc_sdhc_bsh, (reg), (val))
71
72 struct ti_sdhc_config {
73 bus_size_t regoff;
74 uint32_t flags;
75 };
76
77 static const struct ti_sdhc_config omap2_hsmmc_config = {
78 };
79
80 static const struct ti_sdhc_config omap3_pre_es3_hsmmc_config = {
81 .flags = SDHC_FLAG_SINGLE_ONLY
82 };
83
84 static const struct ti_sdhc_config omap4_hsmmc_config = {
85 .regoff = 0x100
86 };
87
88 static const struct of_compat_data compat_data[] = {
89 { "ti,omap2-hsmmc", (uintptr_t)&omap2_hsmmc_config },
90 { "ti,omap3-hsmmc", (uintptr_t)&omap2_hsmmc_config },
91 { "ti,omap3-pre-es3-hsmmc", (uintptr_t)&omap3_pre_es3_hsmmc_config },
92 { "ti,omap4-hsmmc", (uintptr_t)&omap4_hsmmc_config },
93 { NULL }
94 };
95
96 enum {
97 EDMA_CHAN_TX,
98 EDMA_CHAN_RX,
99 EDMA_NCHAN
100 };
101
102 struct ti_sdhc_softc {
103 struct sdhc_softc sc;
104 int sc_phandle;
105 bus_addr_t sc_addr;
106 bus_space_tag_t sc_bst;
107 bus_space_handle_t sc_bsh;
108 bus_space_handle_t sc_hl_bsh;
109 bus_space_handle_t sc_sdhc_bsh;
110 struct sdhc_host *sc_hosts[1];
111 void *sc_ih; /* interrupt vectoring */
112
113 int sc_edma_chan[EDMA_NCHAN];
114 struct edma_channel *sc_edma_tx;
115 struct edma_channel *sc_edma_rx;
116 uint16_t sc_edma_param_tx[EDMA_MAX_PARAMS];
117 uint16_t sc_edma_param_rx[EDMA_MAX_PARAMS];
118 kcondvar_t sc_edma_cv;
119 bus_addr_t sc_edma_fifo;
120 bool sc_edma_pending;
121 bus_dmamap_t sc_edma_dmamap;
122 bus_dma_segment_t sc_edma_segs[1];
123 void *sc_edma_bbuf;
124 };
125
126 static int ti_sdhc_match(device_t, cfdata_t, void *);
127 static void ti_sdhc_attach(device_t, device_t, void *);
128
129 static void ti_sdhc_init(struct ti_sdhc_softc *, const struct ti_sdhc_config *);
130
131 static int ti_sdhc_bus_width(struct sdhc_softc *, int);
132 static int ti_sdhc_rod(struct sdhc_softc *, int);
133 static int ti_sdhc_write_protect(struct sdhc_softc *);
134 static int ti_sdhc_card_detect(struct sdhc_softc *);
135
136 static int ti_sdhc_edma_init(struct ti_sdhc_softc *, u_int, u_int);
137 static int ti_sdhc_edma_xfer_data(struct sdhc_softc *, struct sdmmc_command *);
138 static void ti_sdhc_edma_done(void *);
139 static int ti_sdhc_edma_transfer(struct sdhc_softc *, struct sdmmc_command *);
140
141 CFATTACH_DECL_NEW(ti_sdhc, sizeof(struct ti_sdhc_softc),
142 ti_sdhc_match, ti_sdhc_attach, NULL, NULL);
143
144 static int
145 ti_sdhc_match(device_t parent, cfdata_t cf, void *aux)
146 {
147 struct fdt_attach_args * const faa = aux;
148
149 return of_match_compat_data(faa->faa_phandle, compat_data);
150 }
151
152 static void
153 ti_sdhc_attach(device_t parent, device_t self, void *aux)
154 {
155 struct ti_sdhc_softc * const sc = device_private(self);
156 struct fdt_attach_args * const faa = aux;
157 const int phandle = faa->faa_phandle;
158 const struct ti_sdhc_config *conf;
159 bus_addr_t addr;
160 bus_size_t size;
161 u_int bus_width;
162
163 conf = (const void *)of_search_compatible(phandle, compat_data)->data;
164
165 if (ti_prcm_enable_hwmod(OF_parent(phandle), 0) != 0) {
166 aprint_error(": couldn't enable module\n");
167 return;
168 }
169
170 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size <= conf->regoff) {
171 aprint_error(": couldn't get registers\n");
172 return;
173 }
174 addr += conf->regoff;
175 size -= conf->regoff;
176
177 sc->sc.sc_dmat = faa->faa_dmat;
178 sc->sc.sc_dev = self;
179 sc->sc_phandle = phandle;
180 sc->sc_addr = addr;
181 sc->sc_bst = faa->faa_bst;
182
183 #if notyet
184 /* XXX use fdtbus_dma API */
185 int len;
186 const u_int *dmas = fdtbus_get_prop(phandle, "dmas", &len);
187 switch (len) {
188 case 24:
189 sc->sc_edma_chan[EDMA_CHAN_TX] = be32toh(dmas[1]);
190 sc->sc_edma_chan[EDMA_CHAN_RX] = be32toh(dmas[4]);
191 break;
192 case 32:
193 sc->sc_edma_chan[EDMA_CHAN_TX] = be32toh(dmas[1]);
194 sc->sc_edma_chan[EDMA_CHAN_RX] = be32toh(dmas[5]);
195 break;
196 default:
197 sc->sc_edma_chan[EDMA_CHAN_TX] = -1;
198 sc->sc_edma_chan[EDMA_CHAN_RX] = -1;
199 break;
200 }
201 #else
202 sc->sc_edma_chan[EDMA_CHAN_TX] = -1;
203 sc->sc_edma_chan[EDMA_CHAN_RX] = -1;
204 #endif
205
206 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
207 aprint_error(": couldn't map registers\n");
208 return;
209 }
210
211 if (of_getprop_uint32(phandle, "bus-width", &bus_width) != 0)
212 bus_width = 4;
213
214 sc->sc.sc_flags |= conf->flags;
215 sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS;
216 sc->sc.sc_flags |= SDHC_FLAG_NO_LED_ON;
217 sc->sc.sc_flags |= SDHC_FLAG_RSP136_CRC;
218 if (bus_width == 8)
219 sc->sc.sc_flags |= SDHC_FLAG_8BIT_MODE;
220 if (of_hasprop(phandle, "ti,needs-special-reset"))
221 sc->sc.sc_flags |= SDHC_FLAG_WAIT_RESET;
222 if (of_hasprop(phandle, "ti,needs-special-hs-handling"))
223 sc->sc.sc_flags |= SDHC_FLAG_NO_HS_BIT;
224 if (of_hasprop(phandle, "ti,dual-volt"))
225 sc->sc.sc_caps = SDHC_VOLTAGE_SUPP_3_0V;
226
227 sc->sc.sc_host = sc->sc_hosts;
228 sc->sc.sc_clkbase = 96000; /* 96MHZ */
229 sc->sc.sc_clkmsk = 0x0000ffc0;
230 sc->sc.sc_vendor_rod = ti_sdhc_rod;
231 sc->sc.sc_vendor_write_protect = ti_sdhc_write_protect;
232 sc->sc.sc_vendor_card_detect = ti_sdhc_card_detect;
233 sc->sc.sc_vendor_bus_width = ti_sdhc_bus_width;
234
235 if (bus_space_subregion(sc->sc_bst, sc->sc_bsh, 0x100, 0x100,
236 &sc->sc_sdhc_bsh) != 0) {
237 aprint_error(": couldn't map subregion\n");
238 return;
239 }
240
241 aprint_naive("\n");
242 aprint_normal(": MMCHS\n");
243
244 ti_sdhc_init(sc, conf);
245 }
246
247 static void
248 ti_sdhc_init(struct ti_sdhc_softc *sc, const struct ti_sdhc_config *conf)
249 {
250 device_t dev = sc->sc.sc_dev;
251 uint32_t clkd, stat;
252 int error, timo, clksft, n;
253 char intrstr[128];
254
255 const int tx_chan = sc->sc_edma_chan[EDMA_CHAN_TX];
256 const int rx_chan = sc->sc_edma_chan[EDMA_CHAN_RX];
257
258 if (tx_chan != -1 && rx_chan != -1) {
259 aprint_normal_dev(dev,
260 "EDMA tx channel %d, rx channel %d\n",
261 tx_chan, rx_chan);
262
263 if (ti_sdhc_edma_init(sc, tx_chan, rx_chan) != 0) {
264 aprint_error_dev(dev, "EDMA disabled\n");
265 goto no_dma;
266 }
267
268 cv_init(&sc->sc_edma_cv, "sdhcedma");
269 sc->sc_edma_fifo = sc->sc_addr + 0x100 + SDHC_DATA;
270 sc->sc.sc_flags |= SDHC_FLAG_USE_DMA;
271 sc->sc.sc_flags |= SDHC_FLAG_EXTERNAL_DMA;
272 sc->sc.sc_flags |= SDHC_FLAG_EXTDMA_DMAEN;
273 sc->sc.sc_vendor_transfer_data_dma = ti_sdhc_edma_xfer_data;
274 }
275 no_dma:
276
277 /* XXXXXX: Turn-on regulator via I2C. */
278 /* XXXXXX: And enable ICLOCK/FCLOCK. */
279
280 SDHC_WRITE(sc, SDHC_CAPABILITIES,
281 SDHC_READ(sc, SDHC_CAPABILITIES) | SDHC_VOLTAGE_SUPP_1_8V);
282 if (sc->sc.sc_caps & SDHC_VOLTAGE_SUPP_3_0V)
283 SDHC_WRITE(sc, SDHC_CAPABILITIES,
284 SDHC_READ(sc, SDHC_CAPABILITIES) | SDHC_VOLTAGE_SUPP_3_0V);
285
286 /* MMCHS Soft reset */
287 bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_SYSCONFIG,
288 SYSCONFIG_SOFTRESET);
289 timo = 3000000; /* XXXX 3 sec. */
290 while (timo--) {
291 if (bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_SYSSTATUS) &
292 SYSSTATUS_RESETDONE)
293 break;
294 delay(1);
295 }
296 if (timo == 0)
297 aprint_error_dev(dev, "Soft reset timeout\n");
298 bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_SYSCONFIG,
299 SYSCONFIG_ENAWAKEUP |
300 #if notyet
301 SYSCONFIG_AUTOIDLE |
302 SYSCONFIG_SIDLEMODE_AUTO |
303 #else
304 SYSCONFIG_SIDLEMODE_IGNORE |
305 #endif
306 SYSCONFIG_CLOCKACTIVITY_FCLK |
307 SYSCONFIG_CLOCKACTIVITY_ICLK);
308
309 if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof(intrstr))) {
310 aprint_error_dev(dev, "couldn't decode interrupt\n");
311 return;
312 }
313 sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM,
314 0, sdhc_intr, &sc->sc);
315 if (sc->sc_ih == NULL) {
316 aprint_error_dev(dev, "couldn't establish interrupt\n");
317 return;
318 }
319 aprint_normal_dev(dev, "interrupting on %s\n", intrstr);
320
321 error = sdhc_host_found(&sc->sc, sc->sc_bst, sc->sc_sdhc_bsh, 0x100);
322 if (error != 0) {
323 aprint_error_dev(dev, "couldn't initialize host, error=%d\n",
324 error);
325 fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
326 return;
327 }
328
329 clksft = ffs(sc->sc.sc_clkmsk) - 1;
330
331 /* Set SDVS 1.8v and DTW 1bit mode */
332 SDHC_WRITE(sc, SDHC_HOST_CTL,
333 SDHC_VOLTAGE_1_8V << (SDHC_VOLTAGE_SHIFT + 8));
334 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
335 SDHC_READ(sc, SDHC_CLOCK_CTL) | SDHC_INTCLK_ENABLE |
336 SDHC_SDCLK_ENABLE);
337 SDHC_WRITE(sc, SDHC_HOST_CTL,
338 SDHC_READ(sc, SDHC_HOST_CTL) | SDHC_BUS_POWER << 8);
339 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
340 SDHC_READ(sc, SDHC_CLOCK_CTL) | CLKD(150) << clksft);
341
342 /*
343 * 22.6.1.3.1.5 MMCHS Controller INIT Procedure Start
344 * from 'OMAP35x Applications Processor Technical Reference Manual'.
345 *
346 * During the INIT procedure, the MMCHS controller generates 80 clock
347 * periods. In order to keep the 1ms gap, the MMCHS controller should
348 * be configured to generate a clock whose frequency is smaller or
349 * equal to 80 KHz.
350 */
351
352 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
353 SDHC_READ(sc, SDHC_CLOCK_CTL) & ~SDHC_SDCLK_ENABLE);
354 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
355 SDHC_READ(sc, SDHC_CLOCK_CTL) & ~sc->sc.sc_clkmsk);
356 clkd = CLKD(80);
357 n = 1;
358 while (clkd & ~(sc->sc.sc_clkmsk >> clksft)) {
359 clkd >>= 1;
360 n <<= 1;
361 }
362 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
363 SDHC_READ(sc, SDHC_CLOCK_CTL) | (clkd << clksft));
364 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
365 SDHC_READ(sc, SDHC_CLOCK_CTL) | SDHC_SDCLK_ENABLE);
366
367 bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON,
368 bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON) | CON_INIT);
369 SDHC_WRITE(sc, SDHC_TRANSFER_MODE, 0x00000000);
370 delay(1000);
371 stat = SDHC_READ(sc, SDHC_NINTR_STATUS);
372 SDHC_WRITE(sc, SDHC_NINTR_STATUS, stat | SDHC_COMMAND_COMPLETE);
373 bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON,
374 bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON) & ~CON_INIT);
375 SDHC_WRITE(sc, SDHC_NINTR_STATUS, 0xffffffff);
376
377 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
378 SDHC_READ(sc, SDHC_CLOCK_CTL) & ~SDHC_SDCLK_ENABLE);
379 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
380 SDHC_READ(sc, SDHC_CLOCK_CTL) & ~sc->sc.sc_clkmsk);
381 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
382 SDHC_READ(sc, SDHC_CLOCK_CTL) | CLKD(150) << clksft);
383 timo = 3000000; /* XXXX 3 sec. */
384 while (--timo) {
385 if (SDHC_READ(sc, SDHC_CLOCK_CTL) & SDHC_INTCLK_STABLE)
386 break;
387 delay(1);
388 }
389 if (timo == 0)
390 aprint_error_dev(dev, "ICS timeout\n");
391 SDHC_WRITE(sc, SDHC_CLOCK_CTL,
392 SDHC_READ(sc, SDHC_CLOCK_CTL) | SDHC_SDCLK_ENABLE);
393
394 if (sc->sc.sc_flags & SDHC_FLAG_USE_ADMA2)
395 bus_space_write_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON,
396 bus_space_read_4(sc->sc_bst, sc->sc_bsh, MMCHS_CON) |
397 CON_MNS);
398 }
399
400 static int
401 ti_sdhc_rod(struct sdhc_softc *sc, int on)
402 {
403 struct ti_sdhc_softc *hmsc = (struct ti_sdhc_softc *)sc;
404 uint32_t con;
405
406 con = bus_space_read_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON);
407 if (on)
408 con |= CON_OD;
409 else
410 con &= ~CON_OD;
411 bus_space_write_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON, con);
412
413 return 0;
414 }
415
416 static int
417 ti_sdhc_write_protect(struct sdhc_softc *sc)
418 {
419
420 /* Maybe board dependent, using GPIO. Get GPIO-pin from prop? */
421 return 0; /* XXXXXXX */
422 }
423
424 static int
425 ti_sdhc_card_detect(struct sdhc_softc *sc)
426 {
427
428 /* Maybe board dependent, using GPIO. Get GPIO-pin from prop? */
429 return 1; /* XXXXXXXX */
430 }
431
432 static int
433 ti_sdhc_bus_width(struct sdhc_softc *sc, int width)
434 {
435 struct ti_sdhc_softc *hmsc = (struct ti_sdhc_softc *)sc;
436 uint32_t con;
437
438 con = bus_space_read_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON);
439 if (width == 8) {
440 con |= CON_DW8;
441 } else {
442 con &= ~CON_DW8;
443 }
444 bus_space_write_4(hmsc->sc_bst, hmsc->sc_bsh, MMCHS_CON, con);
445
446 return 0;
447 }
448
449 static int
450 ti_sdhc_edma_init(struct ti_sdhc_softc *sc, u_int tx_chan, u_int rx_chan)
451 {
452 int i, error, rseg;
453
454 /* Request tx and rx DMA channels */
455 sc->sc_edma_tx = edma_channel_alloc(EDMA_TYPE_DMA, tx_chan,
456 ti_sdhc_edma_done, sc);
457 KASSERT(sc->sc_edma_tx != NULL);
458 sc->sc_edma_rx = edma_channel_alloc(EDMA_TYPE_DMA, rx_chan,
459 ti_sdhc_edma_done, sc);
460 KASSERT(sc->sc_edma_rx != NULL);
461
462 /* Allocate some PaRAM pages */
463 for (i = 0; i < __arraycount(sc->sc_edma_param_tx); i++) {
464 sc->sc_edma_param_tx[i] = edma_param_alloc(sc->sc_edma_tx);
465 KASSERT(sc->sc_edma_param_tx[i] != 0xffff);
466 }
467 for (i = 0; i < __arraycount(sc->sc_edma_param_rx); i++) {
468 sc->sc_edma_param_rx[i] = edma_param_alloc(sc->sc_edma_rx);
469 KASSERT(sc->sc_edma_param_rx[i] != 0xffff);
470 }
471
472 /* Setup bounce buffer */
473 error = bus_dmamem_alloc(sc->sc.sc_dmat, MAXPHYS, 32, MAXPHYS,
474 sc->sc_edma_segs, 1, &rseg, BUS_DMA_WAITOK);
475 if (error) {
476 aprint_error_dev(sc->sc.sc_dev,
477 "couldn't allocate dmamem: %d\n", error);
478 return error;
479 }
480 KASSERT(rseg == 1);
481 error = bus_dmamem_map(sc->sc.sc_dmat, sc->sc_edma_segs, rseg, MAXPHYS,
482 &sc->sc_edma_bbuf, BUS_DMA_WAITOK);
483 if (error) {
484 aprint_error_dev(sc->sc.sc_dev, "couldn't map dmamem: %d\n",
485 error);
486 return error;
487 }
488 error = bus_dmamap_create(sc->sc.sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
489 BUS_DMA_WAITOK, &sc->sc_edma_dmamap);
490 if (error) {
491 aprint_error_dev(sc->sc.sc_dev, "couldn't create dmamap: %d\n",
492 error);
493 return error;
494 }
495
496 return error;
497 }
498
499 static int
500 ti_sdhc_edma_xfer_data(struct sdhc_softc *sdhc_sc, struct sdmmc_command *cmd)
501 {
502 struct ti_sdhc_softc *sc = device_private(sdhc_sc->sc_dev);
503 const bus_dmamap_t map = cmd->c_dmamap;
504 int seg, error;
505 bool bounce;
506
507 for (bounce = false, seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
508 if ((cmd->c_dmamap->dm_segs[seg].ds_addr & 0x1f) != 0) {
509 bounce = true;
510 break;
511 }
512 }
513
514 if (bounce) {
515 error = bus_dmamap_load(sc->sc.sc_dmat, sc->sc_edma_dmamap,
516 sc->sc_edma_bbuf, MAXPHYS, NULL, BUS_DMA_WAITOK);
517 if (error) {
518 device_printf(sc->sc.sc_dev,
519 "[bounce] bus_dmamap_load failed: %d\n", error);
520 return error;
521 }
522 if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
523 bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
524 MAXPHYS, BUS_DMASYNC_PREREAD);
525 } else {
526 memcpy(sc->sc_edma_bbuf, cmd->c_data, cmd->c_datalen);
527 bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
528 MAXPHYS, BUS_DMASYNC_PREWRITE);
529 }
530
531 cmd->c_dmamap = sc->sc_edma_dmamap;
532 }
533
534 error = ti_sdhc_edma_transfer(sdhc_sc, cmd);
535
536 if (bounce) {
537 if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
538 bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
539 MAXPHYS, BUS_DMASYNC_POSTREAD);
540 } else {
541 bus_dmamap_sync(sc->sc.sc_dmat, sc->sc_edma_dmamap, 0,
542 MAXPHYS, BUS_DMASYNC_POSTWRITE);
543 }
544 bus_dmamap_unload(sc->sc.sc_dmat, sc->sc_edma_dmamap);
545 if (ISSET(cmd->c_flags, SCF_CMD_READ) && error == 0) {
546 memcpy(cmd->c_data, sc->sc_edma_bbuf, cmd->c_datalen);
547 }
548
549 cmd->c_dmamap = map;
550 }
551
552 return error;
553 }
554
555 static int
556 ti_sdhc_edma_transfer(struct sdhc_softc *sdhc_sc, struct sdmmc_command *cmd)
557 {
558 struct ti_sdhc_softc *sc = device_private(sdhc_sc->sc_dev);
559 kmutex_t *plock = sdhc_host_lock(sc->sc_hosts[0]);
560 struct edma_channel *edma;
561 uint16_t *edma_param;
562 struct edma_param ep;
563 size_t seg;
564 int error, resid = cmd->c_datalen;
565 int blksize = MIN(cmd->c_datalen, cmd->c_blklen);
566
567 KASSERT(mutex_owned(plock));
568
569 edma = ISSET(cmd->c_flags, SCF_CMD_READ) ?
570 sc->sc_edma_rx : sc->sc_edma_tx;
571 edma_param = ISSET(cmd->c_flags, SCF_CMD_READ) ?
572 sc->sc_edma_param_rx : sc->sc_edma_param_tx;
573
574 DPRINTF(1, (sc->sc.sc_dev, "edma xfer: nsegs=%d ch# %d\n",
575 cmd->c_dmamap->dm_nsegs, edma_channel_index(edma)));
576
577 if (cmd->c_dmamap->dm_nsegs > EDMA_MAX_PARAMS) {
578 return ENOMEM;
579 }
580
581 for (seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
582 KASSERT(resid > 0);
583 const int xferlen = uimin(resid,
584 cmd->c_dmamap->dm_segs[seg].ds_len);
585 KASSERT(xferlen == cmd->c_dmamap->dm_segs[seg].ds_len ||
586 seg == cmd->c_dmamap->dm_nsegs - 1);
587 resid -= xferlen;
588 KASSERT((xferlen & 0x3) == 0);
589 ep.ep_opt = __SHIFTIN(2, EDMA_PARAM_OPT_FWID) /* 32-bit */;
590 ep.ep_opt |= __SHIFTIN(edma_channel_index(edma),
591 EDMA_PARAM_OPT_TCC);
592 if (seg == cmd->c_dmamap->dm_nsegs - 1) {
593 ep.ep_opt |= EDMA_PARAM_OPT_TCINTEN;
594 ep.ep_link = 0xffff;
595 } else {
596 ep.ep_link = EDMA_PARAM_BASE(edma_param[seg+1]);
597 }
598 if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
599 ep.ep_opt |= EDMA_PARAM_OPT_SAM;
600 ep.ep_src = sc->sc_edma_fifo;
601 ep.ep_dst = cmd->c_dmamap->dm_segs[seg].ds_addr;
602 } else {
603 ep.ep_opt |= EDMA_PARAM_OPT_DAM;
604 ep.ep_src = cmd->c_dmamap->dm_segs[seg].ds_addr;
605 ep.ep_dst = sc->sc_edma_fifo;
606 }
607
608 KASSERT(xferlen <= 65536 * 4);
609
610 /*
611 * In constant addressing mode, the address must be aligned
612 * to 256-bits.
613 */
614 KASSERT((cmd->c_dmamap->dm_segs[seg].ds_addr & 0x1f) == 0);
615
616 /*
617 * For unknown reason, the A-DMA transfers never completes for
618 * transfers larger than 64 butes. So use a AB transfer,
619 * with a 64 bytes A len
620 */
621 ep.ep_bcntrld = 0; /* not used for AB-synchronous mode */
622 ep.ep_opt |= EDMA_PARAM_OPT_SYNCDIM;
623 ep.ep_acnt = uimin(xferlen, 64);
624 ep.ep_bcnt = uimin(xferlen, blksize) / ep.ep_acnt;
625 ep.ep_ccnt = xferlen / (ep.ep_acnt * ep.ep_bcnt);
626 ep.ep_srcbidx = ep.ep_dstbidx = 0;
627 ep.ep_srccidx = ep.ep_dstcidx = 0;
628 if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
629 ep.ep_dstbidx = ep.ep_acnt;
630 ep.ep_dstcidx = ep.ep_acnt * ep.ep_bcnt;
631 } else {
632 ep.ep_srcbidx = ep.ep_acnt;
633 ep.ep_srccidx = ep.ep_acnt * ep.ep_bcnt;
634 }
635
636 edma_set_param(edma, edma_param[seg], &ep);
637 #ifdef TISDHC_DEBUG
638 if (tisdhcdebug >= 1) {
639 printf("target OPT: %08x\n", ep.ep_opt);
640 edma_dump_param(edma, edma_param[seg]);
641 }
642 #endif
643 }
644
645 error = 0;
646 sc->sc_edma_pending = true;
647 edma_transfer_enable(edma, edma_param[0]);
648 while (sc->sc_edma_pending) {
649 error = cv_timedwait(&sc->sc_edma_cv, plock, hz*10);
650 if (error == EWOULDBLOCK) {
651 device_printf(sc->sc.sc_dev, "transfer timeout!\n");
652 edma_dump(edma);
653 edma_dump_param(edma, edma_param[0]);
654 edma_halt(edma);
655 sc->sc_edma_pending = false;
656 error = ETIMEDOUT;
657 break;
658 }
659 }
660 edma_halt(edma);
661
662 return error;
663 }
664
665 static void
666 ti_sdhc_edma_done(void *priv)
667 {
668 struct ti_sdhc_softc *sc = priv;
669 kmutex_t *plock = sdhc_host_lock(sc->sc_hosts[0]);
670
671 mutex_enter(plock);
672 KASSERT(sc->sc_edma_pending == true);
673 sc->sc_edma_pending = false;
674 cv_broadcast(&sc->sc_edma_cv);
675 mutex_exit(plock);
676 }
677