xref: /freebsd/sys/dev/mmc/host/dwmmc.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * Synopsys DesignWare Mobile Storage Host Controller
33  * Chapter 14, Altera Cyclone V Device Handbook (CV-5V2 2014.07.22)
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
45 #include <sys/rman.h>
46 
47 #include <dev/mmc/bridge.h>
48 #include <dev/mmc/mmcbrvar.h>
49 
50 #include <dev/fdt/fdt_common.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 
55 #include <machine/bus.h>
56 #include <machine/cpu.h>
57 #include <machine/intr.h>
58 
59 #include <dev/mmc/host/dwmmc_reg.h>
60 #include <dev/mmc/host/dwmmc_var.h>
61 
62 #include "opt_mmccam.h"
63 
64 #include "mmcbr_if.h"
65 
66 #define dprintf(x, arg...)
67 
68 #define	READ4(_sc, _reg) \
69 	bus_read_4((_sc)->res[0], _reg)
70 #define	WRITE4(_sc, _reg, _val) \
71 	bus_write_4((_sc)->res[0], _reg, _val)
72 
73 #define	DIV_ROUND_UP(n, d)		howmany(n, d)
74 
75 #define	DWMMC_LOCK(_sc)			mtx_lock(&(_sc)->sc_mtx)
76 #define	DWMMC_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
77 #define	DWMMC_LOCK_INIT(_sc) \
78 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
79 	    "dwmmc", MTX_DEF)
80 #define	DWMMC_LOCK_DESTROY(_sc)		mtx_destroy(&_sc->sc_mtx);
81 #define	DWMMC_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
82 #define	DWMMC_ASSERT_UNLOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
83 
84 #define	PENDING_CMD	0x01
85 #define	PENDING_STOP	0x02
86 #define	CARD_INIT_DONE	0x04
87 
88 #define	DWMMC_DATA_ERR_FLAGS	(SDMMC_INTMASK_DRT | SDMMC_INTMASK_DCRC \
89 				|SDMMC_INTMASK_HTO | SDMMC_INTMASK_SBE \
90 				|SDMMC_INTMASK_EBE)
91 #define	DWMMC_CMD_ERR_FLAGS	(SDMMC_INTMASK_RTO | SDMMC_INTMASK_RCRC \
92 				|SDMMC_INTMASK_RE)
93 #define	DWMMC_ERR_FLAGS		(DWMMC_DATA_ERR_FLAGS | DWMMC_CMD_ERR_FLAGS \
94 				|SDMMC_INTMASK_HLE)
95 
96 #define	DES0_DIC	(1 << 1)
97 #define	DES0_LD		(1 << 2)
98 #define	DES0_FS		(1 << 3)
99 #define	DES0_CH		(1 << 4)
100 #define	DES0_ER		(1 << 5)
101 #define	DES0_CES	(1 << 30)
102 #define	DES0_OWN	(1 << 31)
103 
104 #define	DES1_BS1_MASK	0xfff
105 #define	DES1_BS1_SHIFT	0
106 
107 struct idmac_desc {
108 	uint32_t	des0;	/* control */
109 	uint32_t	des1;	/* bufsize */
110 	uint32_t	des2;	/* buf1 phys addr */
111 	uint32_t	des3;	/* buf2 phys addr or next descr */
112 };
113 
114 #define	DESC_MAX	256
115 #define	DESC_SIZE	(sizeof(struct idmac_desc) * DESC_MAX)
116 #define	DEF_MSIZE	0x2	/* Burst size of multiple transaction */
117 
118 static void dwmmc_next_operation(struct dwmmc_softc *);
119 static int dwmmc_setup_bus(struct dwmmc_softc *, int);
120 static int dma_done(struct dwmmc_softc *, struct mmc_command *);
121 static int dma_stop(struct dwmmc_softc *);
122 static void pio_read(struct dwmmc_softc *, struct mmc_command *);
123 static void pio_write(struct dwmmc_softc *, struct mmc_command *);
124 
125 static struct resource_spec dwmmc_spec[] = {
126 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
127 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
128 	{ -1, 0 }
129 };
130 
131 #define	HWTYPE_MASK		(0x0000ffff)
132 #define	HWFLAG_MASK		(0xffff << 16)
133 
134 static struct ofw_compat_data compat_data[] = {
135 	{"altr,socfpga-dw-mshc",	HWTYPE_ALTERA},
136 	{"samsung,exynos5420-dw-mshc",	HWTYPE_EXYNOS},
137 	{"rockchip,rk2928-dw-mshc",	HWTYPE_ROCKCHIP},
138 	{NULL,				HWTYPE_NONE},
139 };
140 
141 static void
142 dwmmc_get1paddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
143 {
144 
145 	if (error != 0)
146 		return;
147 	*(bus_addr_t *)arg = segs[0].ds_addr;
148 }
149 
150 static void
151 dwmmc_ring_setup(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
152 {
153 	struct dwmmc_softc *sc;
154 	int idx;
155 
156 	if (error != 0)
157 		return;
158 
159 	sc = arg;
160 
161 	dprintf("nsegs %d seg0len %lu\n", nsegs, segs[0].ds_len);
162 
163 	for (idx = 0; idx < nsegs; idx++) {
164 		sc->desc_ring[idx].des0 = (DES0_OWN | DES0_DIC | DES0_CH);
165 		sc->desc_ring[idx].des1 = segs[idx].ds_len;
166 		sc->desc_ring[idx].des2 = segs[idx].ds_addr;
167 
168 		if (idx == 0)
169 			sc->desc_ring[idx].des0 |= DES0_FS;
170 
171 		if (idx == (nsegs - 1)) {
172 			sc->desc_ring[idx].des0 &= ~(DES0_DIC | DES0_CH);
173 			sc->desc_ring[idx].des0 |= DES0_LD;
174 		}
175 	}
176 }
177 
178 static int
179 dwmmc_ctrl_reset(struct dwmmc_softc *sc, int reset_bits)
180 {
181 	int reg;
182 	int i;
183 
184 	reg = READ4(sc, SDMMC_CTRL);
185 	reg |= (reset_bits);
186 	WRITE4(sc, SDMMC_CTRL, reg);
187 
188 	/* Wait reset done */
189 	for (i = 0; i < 100; i++) {
190 		if (!(READ4(sc, SDMMC_CTRL) & reset_bits))
191 			return (0);
192 		DELAY(10);
193 	}
194 
195 	device_printf(sc->dev, "Reset failed\n");
196 
197 	return (1);
198 }
199 
200 static int
201 dma_setup(struct dwmmc_softc *sc)
202 {
203 	int error;
204 	int nidx;
205 	int idx;
206 
207 	/*
208 	 * Set up TX descriptor ring, descriptors, and dma maps.
209 	 */
210 	error = bus_dma_tag_create(
211 	    bus_get_dma_tag(sc->dev),	/* Parent tag. */
212 	    4096, 0,			/* alignment, boundary */
213 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
214 	    BUS_SPACE_MAXADDR,		/* highaddr */
215 	    NULL, NULL,			/* filter, filterarg */
216 	    DESC_SIZE, 1, 		/* maxsize, nsegments */
217 	    DESC_SIZE,			/* maxsegsize */
218 	    0,				/* flags */
219 	    NULL, NULL,			/* lockfunc, lockarg */
220 	    &sc->desc_tag);
221 	if (error != 0) {
222 		device_printf(sc->dev,
223 		    "could not create ring DMA tag.\n");
224 		return (1);
225 	}
226 
227 	error = bus_dmamem_alloc(sc->desc_tag, (void**)&sc->desc_ring,
228 	    BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
229 	    &sc->desc_map);
230 	if (error != 0) {
231 		device_printf(sc->dev,
232 		    "could not allocate descriptor ring.\n");
233 		return (1);
234 	}
235 
236 	error = bus_dmamap_load(sc->desc_tag, sc->desc_map,
237 	    sc->desc_ring, DESC_SIZE, dwmmc_get1paddr,
238 	    &sc->desc_ring_paddr, 0);
239 	if (error != 0) {
240 		device_printf(sc->dev,
241 		    "could not load descriptor ring map.\n");
242 		return (1);
243 	}
244 
245 	for (idx = 0; idx < sc->desc_count; idx++) {
246 		sc->desc_ring[idx].des0 = DES0_CH;
247 		sc->desc_ring[idx].des1 = 0;
248 		nidx = (idx + 1) % sc->desc_count;
249 		sc->desc_ring[idx].des3 = sc->desc_ring_paddr + \
250 		    (nidx * sizeof(struct idmac_desc));
251 	}
252 
253 	error = bus_dma_tag_create(
254 	    bus_get_dma_tag(sc->dev),	/* Parent tag. */
255 	    4096, 0,			/* alignment, boundary */
256 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
257 	    BUS_SPACE_MAXADDR,		/* highaddr */
258 	    NULL, NULL,			/* filter, filterarg */
259 	    sc->desc_count * MMC_SECTOR_SIZE, /* maxsize */
260 	    sc->desc_count,		/* nsegments */
261 	    MMC_SECTOR_SIZE,		/* maxsegsize */
262 	    0,				/* flags */
263 	    NULL, NULL,			/* lockfunc, lockarg */
264 	    &sc->buf_tag);
265 	if (error != 0) {
266 		device_printf(sc->dev,
267 		    "could not create ring DMA tag.\n");
268 		return (1);
269 	}
270 
271 	error = bus_dmamap_create(sc->buf_tag, 0,
272 	    &sc->buf_map);
273 	if (error != 0) {
274 		device_printf(sc->dev,
275 		    "could not create TX buffer DMA map.\n");
276 		return (1);
277 	}
278 
279 	return (0);
280 }
281 
282 static void
283 dwmmc_cmd_done(struct dwmmc_softc *sc)
284 {
285 	struct mmc_command *cmd;
286 
287 	cmd = sc->curcmd;
288 	if (cmd == NULL)
289 		return;
290 
291 	if (cmd->flags & MMC_RSP_PRESENT) {
292 		if (cmd->flags & MMC_RSP_136) {
293 			cmd->resp[3] = READ4(sc, SDMMC_RESP0);
294 			cmd->resp[2] = READ4(sc, SDMMC_RESP1);
295 			cmd->resp[1] = READ4(sc, SDMMC_RESP2);
296 			cmd->resp[0] = READ4(sc, SDMMC_RESP3);
297 		} else {
298 			cmd->resp[3] = 0;
299 			cmd->resp[2] = 0;
300 			cmd->resp[1] = 0;
301 			cmd->resp[0] = READ4(sc, SDMMC_RESP0);
302 		}
303 	}
304 }
305 
306 static void
307 dwmmc_tasklet(struct dwmmc_softc *sc)
308 {
309 	struct mmc_command *cmd;
310 
311 	cmd = sc->curcmd;
312 	if (cmd == NULL)
313 		return;
314 
315 	if (!sc->cmd_done)
316 		return;
317 
318 	if (cmd->error != MMC_ERR_NONE || !cmd->data) {
319 		dwmmc_next_operation(sc);
320 	} else if (cmd->data && sc->dto_rcvd) {
321 		if ((cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
322 		     cmd->opcode == MMC_READ_MULTIPLE_BLOCK) &&
323 		     sc->use_auto_stop) {
324 			if (sc->acd_rcvd)
325 				dwmmc_next_operation(sc);
326 		} else {
327 			dwmmc_next_operation(sc);
328 		}
329 	}
330 }
331 
332 static void
333 dwmmc_intr(void *arg)
334 {
335 	struct mmc_command *cmd;
336 	struct dwmmc_softc *sc;
337 	uint32_t reg;
338 
339 	sc = arg;
340 
341 	DWMMC_LOCK(sc);
342 
343 	cmd = sc->curcmd;
344 
345 	/* First handle SDMMC controller interrupts */
346 	reg = READ4(sc, SDMMC_MINTSTS);
347 	if (reg) {
348 		dprintf("%s 0x%08x\n", __func__, reg);
349 
350 		if (reg & DWMMC_CMD_ERR_FLAGS) {
351 			WRITE4(sc, SDMMC_RINTSTS, DWMMC_CMD_ERR_FLAGS);
352 			dprintf("cmd err 0x%08x cmd 0x%08x\n",
353 				reg, cmd->opcode);
354 			cmd->error = MMC_ERR_TIMEOUT;
355 		}
356 
357 		if (reg & DWMMC_DATA_ERR_FLAGS) {
358 			WRITE4(sc, SDMMC_RINTSTS, DWMMC_DATA_ERR_FLAGS);
359 			dprintf("data err 0x%08x cmd 0x%08x\n",
360 				reg, cmd->opcode);
361 			cmd->error = MMC_ERR_FAILED;
362 			if (!sc->use_pio) {
363 				dma_done(sc, cmd);
364 				dma_stop(sc);
365 			}
366 		}
367 
368 		if (reg & SDMMC_INTMASK_CMD_DONE) {
369 			dwmmc_cmd_done(sc);
370 			sc->cmd_done = 1;
371 			WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_CMD_DONE);
372 		}
373 
374 		if (reg & SDMMC_INTMASK_ACD) {
375 			sc->acd_rcvd = 1;
376 			WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_ACD);
377 		}
378 
379 		if (reg & SDMMC_INTMASK_DTO) {
380 			sc->dto_rcvd = 1;
381 			WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_DTO);
382 		}
383 
384 		if (reg & SDMMC_INTMASK_CD) {
385 			/* XXX: Handle card detect */
386 			WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_CD);
387 		}
388 	}
389 
390 	if (sc->use_pio) {
391 		if (reg & (SDMMC_INTMASK_RXDR|SDMMC_INTMASK_DTO)) {
392 			pio_read(sc, cmd);
393 		}
394 		if (reg & (SDMMC_INTMASK_TXDR|SDMMC_INTMASK_DTO)) {
395 			pio_write(sc, cmd);
396 		}
397 	} else {
398 		/* Now handle DMA interrupts */
399 		reg = READ4(sc, SDMMC_IDSTS);
400 		if (reg) {
401 			dprintf("dma intr 0x%08x\n", reg);
402 			if (reg & (SDMMC_IDINTEN_TI | SDMMC_IDINTEN_RI)) {
403 				WRITE4(sc, SDMMC_IDSTS, (SDMMC_IDINTEN_TI |
404 							 SDMMC_IDINTEN_RI));
405 				WRITE4(sc, SDMMC_IDSTS, SDMMC_IDINTEN_NI);
406 				dma_done(sc, cmd);
407 			}
408 		}
409 	}
410 
411 	dwmmc_tasklet(sc);
412 
413 	DWMMC_UNLOCK(sc);
414 }
415 
416 static int
417 parse_fdt(struct dwmmc_softc *sc)
418 {
419 	pcell_t dts_value[3];
420 	phandle_t node;
421 	int len;
422 
423 	if ((node = ofw_bus_get_node(sc->dev)) == -1)
424 		return (ENXIO);
425 
426 	/* fifo-depth */
427 	if ((len = OF_getproplen(node, "fifo-depth")) > 0) {
428 		OF_getencprop(node, "fifo-depth", dts_value, len);
429 		sc->fifo_depth = dts_value[0];
430 	}
431 
432 	/* num-slots */
433 	sc->num_slots = 1;
434 	if ((len = OF_getproplen(node, "num-slots")) > 0) {
435 		OF_getencprop(node, "num-slots", dts_value, len);
436 		sc->num_slots = dts_value[0];
437 	}
438 
439 	/*
440 	 * We need some platform-specific code to know
441 	 * what the clock is supplied for our device.
442 	 * For now rely on the value specified in FDT.
443 	 */
444 	if (sc->bus_hz == 0) {
445 		if ((len = OF_getproplen(node, "bus-frequency")) <= 0)
446 			return (ENXIO);
447 		OF_getencprop(node, "bus-frequency", dts_value, len);
448 		sc->bus_hz = dts_value[0];
449 	}
450 
451 	/*
452 	 * Platform-specific stuff
453 	 * XXX: Move to separate file
454 	 */
455 
456 	if ((sc->hwtype & HWTYPE_MASK) != HWTYPE_EXYNOS)
457 		return (0);
458 
459 	if ((len = OF_getproplen(node, "samsung,dw-mshc-ciu-div")) <= 0)
460 		return (ENXIO);
461 	OF_getencprop(node, "samsung,dw-mshc-ciu-div", dts_value, len);
462 	sc->sdr_timing = (dts_value[0] << SDMMC_CLKSEL_DIVIDER_SHIFT);
463 	sc->ddr_timing = (dts_value[0] << SDMMC_CLKSEL_DIVIDER_SHIFT);
464 
465 	if ((len = OF_getproplen(node, "samsung,dw-mshc-sdr-timing")) <= 0)
466 		return (ENXIO);
467 	OF_getencprop(node, "samsung,dw-mshc-sdr-timing", dts_value, len);
468 	sc->sdr_timing |= ((dts_value[0] << SDMMC_CLKSEL_SAMPLE_SHIFT) |
469 			  (dts_value[1] << SDMMC_CLKSEL_DRIVE_SHIFT));
470 
471 	if ((len = OF_getproplen(node, "samsung,dw-mshc-ddr-timing")) <= 0)
472 		return (ENXIO);
473 	OF_getencprop(node, "samsung,dw-mshc-ddr-timing", dts_value, len);
474 	sc->ddr_timing |= ((dts_value[0] << SDMMC_CLKSEL_SAMPLE_SHIFT) |
475 			  (dts_value[1] << SDMMC_CLKSEL_DRIVE_SHIFT));
476 
477 	return (0);
478 }
479 
480 static int
481 dwmmc_probe(device_t dev)
482 {
483 	uintptr_t hwtype;
484 
485 	if (!ofw_bus_status_okay(dev))
486 		return (ENXIO);
487 
488 	hwtype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
489 	if (hwtype == HWTYPE_NONE)
490 		return (ENXIO);
491 
492 	device_set_desc(dev, "Synopsys DesignWare Mobile "
493 				"Storage Host Controller");
494 	return (BUS_PROBE_DEFAULT);
495 }
496 
497 int
498 dwmmc_attach(device_t dev)
499 {
500 	struct dwmmc_softc *sc;
501 	int error;
502 	int slot;
503 
504 	sc = device_get_softc(dev);
505 
506 	sc->dev = dev;
507 	if (sc->hwtype == HWTYPE_NONE) {
508 		sc->hwtype =
509 		    ofw_bus_search_compatible(dev, compat_data)->ocd_data;
510 	}
511 
512 	/* Why not to use Auto Stop? It save a hundred of irq per second */
513 	sc->use_auto_stop = 1;
514 
515 	error = parse_fdt(sc);
516 	if (error != 0) {
517 		device_printf(dev, "Can't get FDT property.\n");
518 		return (ENXIO);
519 	}
520 
521 	DWMMC_LOCK_INIT(sc);
522 
523 	if (bus_alloc_resources(dev, dwmmc_spec, sc->res)) {
524 		device_printf(dev, "could not allocate resources\n");
525 		return (ENXIO);
526 	}
527 
528 	/* Setup interrupt handler. */
529 	error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_NET | INTR_MPSAFE,
530 	    NULL, dwmmc_intr, sc, &sc->intr_cookie);
531 	if (error != 0) {
532 		device_printf(dev, "could not setup interrupt handler.\n");
533 		return (ENXIO);
534 	}
535 
536 	device_printf(dev, "Hardware version ID is %04x\n",
537 		READ4(sc, SDMMC_VERID) & 0xffff);
538 
539 	if (sc->desc_count == 0)
540 		sc->desc_count = DESC_MAX;
541 
542 	if ((sc->hwtype & HWTYPE_MASK) == HWTYPE_ROCKCHIP) {
543 		sc->use_pio = 1;
544 		sc->pwren_inverted = 1;
545 	} else if ((sc->hwtype & HWTYPE_MASK) == HWTYPE_EXYNOS) {
546 		WRITE4(sc, EMMCP_MPSBEGIN0, 0);
547 		WRITE4(sc, EMMCP_SEND0, 0);
548 		WRITE4(sc, EMMCP_CTRL0, (MPSCTRL_SECURE_READ_BIT |
549 					 MPSCTRL_SECURE_WRITE_BIT |
550 					 MPSCTRL_NON_SECURE_READ_BIT |
551 					 MPSCTRL_NON_SECURE_WRITE_BIT |
552 					 MPSCTRL_VALID));
553 	}
554 
555 	/* XXX: we support operation for slot index 0 only */
556 	slot = 0;
557 	if (sc->pwren_inverted) {
558 		WRITE4(sc, SDMMC_PWREN, (0 << slot));
559 	} else {
560 		WRITE4(sc, SDMMC_PWREN, (1 << slot));
561 	}
562 
563 	/* Reset all */
564 	if (dwmmc_ctrl_reset(sc, (SDMMC_CTRL_RESET |
565 				  SDMMC_CTRL_FIFO_RESET |
566 				  SDMMC_CTRL_DMA_RESET)))
567 		return (ENXIO);
568 
569 	dwmmc_setup_bus(sc, sc->host.f_min);
570 
571 	if (sc->fifo_depth == 0) {
572 		sc->fifo_depth = 1 +
573 		    ((READ4(sc, SDMMC_FIFOTH) >> SDMMC_FIFOTH_RXWMARK_S) & 0xfff);
574 		device_printf(dev, "No fifo-depth, using FIFOTH %x\n",
575 		    sc->fifo_depth);
576 	}
577 
578 	if (!sc->use_pio) {
579 		if (dma_setup(sc))
580 			return (ENXIO);
581 
582 		/* Install desc base */
583 		WRITE4(sc, SDMMC_DBADDR, sc->desc_ring_paddr);
584 
585 		/* Enable DMA interrupts */
586 		WRITE4(sc, SDMMC_IDSTS, SDMMC_IDINTEN_MASK);
587 		WRITE4(sc, SDMMC_IDINTEN, (SDMMC_IDINTEN_NI |
588 					   SDMMC_IDINTEN_RI |
589 					   SDMMC_IDINTEN_TI));
590 	}
591 
592 	/* Clear and disable interrups for a while */
593 	WRITE4(sc, SDMMC_RINTSTS, 0xffffffff);
594 	WRITE4(sc, SDMMC_INTMASK, 0);
595 
596 	/* Maximum timeout */
597 	WRITE4(sc, SDMMC_TMOUT, 0xffffffff);
598 
599 	/* Enable interrupts */
600 	WRITE4(sc, SDMMC_RINTSTS, 0xffffffff);
601 	WRITE4(sc, SDMMC_INTMASK, (SDMMC_INTMASK_CMD_DONE |
602 				   SDMMC_INTMASK_DTO |
603 				   SDMMC_INTMASK_ACD |
604 				   SDMMC_INTMASK_TXDR |
605 				   SDMMC_INTMASK_RXDR |
606 				   DWMMC_ERR_FLAGS |
607 				   SDMMC_INTMASK_CD));
608 	WRITE4(sc, SDMMC_CTRL, SDMMC_CTRL_INT_ENABLE);
609 
610 	sc->host.f_min = 400000;
611 	sc->host.f_max = min(200000000, sc->bus_hz);
612 	sc->host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340;
613 	sc->host.caps = MMC_CAP_4_BIT_DATA;
614 
615 	device_add_child(dev, "mmc", -1);
616 	return (bus_generic_attach(dev));
617 }
618 
619 static int
620 dwmmc_setup_bus(struct dwmmc_softc *sc, int freq)
621 {
622 	int tout;
623 	int div;
624 
625 	if (freq == 0) {
626 		WRITE4(sc, SDMMC_CLKENA, 0);
627 		WRITE4(sc, SDMMC_CMD, (SDMMC_CMD_WAIT_PRVDATA |
628 			SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START));
629 
630 		tout = 1000;
631 		do {
632 			if (tout-- < 0) {
633 				device_printf(sc->dev, "Failed update clk\n");
634 				return (1);
635 			}
636 		} while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
637 
638 		return (0);
639 	}
640 
641 	WRITE4(sc, SDMMC_CLKENA, 0);
642 	WRITE4(sc, SDMMC_CLKSRC, 0);
643 
644 	div = (sc->bus_hz != freq) ? DIV_ROUND_UP(sc->bus_hz, 2 * freq) : 0;
645 
646 	WRITE4(sc, SDMMC_CLKDIV, div);
647 	WRITE4(sc, SDMMC_CMD, (SDMMC_CMD_WAIT_PRVDATA |
648 			SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START));
649 
650 	tout = 1000;
651 	do {
652 		if (tout-- < 0) {
653 			device_printf(sc->dev, "Failed to update clk");
654 			return (1);
655 		}
656 	} while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
657 
658 	WRITE4(sc, SDMMC_CLKENA, (SDMMC_CLKENA_CCLK_EN | SDMMC_CLKENA_LP));
659 	WRITE4(sc, SDMMC_CMD, SDMMC_CMD_WAIT_PRVDATA |
660 			SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START);
661 
662 	tout = 1000;
663 	do {
664 		if (tout-- < 0) {
665 			device_printf(sc->dev, "Failed to enable clk\n");
666 			return (1);
667 		}
668 	} while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
669 
670 	return (0);
671 }
672 
673 static int
674 dwmmc_update_ios(device_t brdev, device_t reqdev)
675 {
676 	struct dwmmc_softc *sc;
677 	struct mmc_ios *ios;
678 
679 	sc = device_get_softc(brdev);
680 	ios = &sc->host.ios;
681 
682 	dprintf("Setting up clk %u bus_width %d\n",
683 		ios->clock, ios->bus_width);
684 
685 	dwmmc_setup_bus(sc, ios->clock);
686 
687 	if (ios->bus_width == bus_width_8)
688 		WRITE4(sc, SDMMC_CTYPE, SDMMC_CTYPE_8BIT);
689 	else if (ios->bus_width == bus_width_4)
690 		WRITE4(sc, SDMMC_CTYPE, SDMMC_CTYPE_4BIT);
691 	else
692 		WRITE4(sc, SDMMC_CTYPE, 0);
693 
694 	if ((sc->hwtype & HWTYPE_MASK) == HWTYPE_EXYNOS) {
695 		/* XXX: take care about DDR or SDR use here */
696 		WRITE4(sc, SDMMC_CLKSEL, sc->sdr_timing);
697 	}
698 
699 	/*
700 	 * XXX: take care about DDR bit
701 	 *
702 	 * reg = READ4(sc, SDMMC_UHS_REG);
703 	 * reg |= (SDMMC_UHS_REG_DDR);
704 	 * WRITE4(sc, SDMMC_UHS_REG, reg);
705 	 */
706 
707 	return (0);
708 }
709 
710 static int
711 dma_done(struct dwmmc_softc *sc, struct mmc_command *cmd)
712 {
713 	struct mmc_data *data;
714 
715 	data = cmd->data;
716 
717 	if (data->flags & MMC_DATA_WRITE)
718 		bus_dmamap_sync(sc->buf_tag, sc->buf_map,
719 			BUS_DMASYNC_POSTWRITE);
720 	else
721 		bus_dmamap_sync(sc->buf_tag, sc->buf_map,
722 			BUS_DMASYNC_POSTREAD);
723 
724 	bus_dmamap_sync(sc->desc_tag, sc->desc_map,
725 	    BUS_DMASYNC_POSTWRITE);
726 
727 	bus_dmamap_unload(sc->buf_tag, sc->buf_map);
728 
729 	return (0);
730 }
731 
732 static int
733 dma_stop(struct dwmmc_softc *sc)
734 {
735 	int reg;
736 
737 	reg = READ4(sc, SDMMC_CTRL);
738 	reg &= ~(SDMMC_CTRL_USE_IDMAC);
739 	reg |= (SDMMC_CTRL_DMA_RESET);
740 	WRITE4(sc, SDMMC_CTRL, reg);
741 
742 	reg = READ4(sc, SDMMC_BMOD);
743 	reg &= ~(SDMMC_BMOD_DE | SDMMC_BMOD_FB);
744 	reg |= (SDMMC_BMOD_SWR);
745 	WRITE4(sc, SDMMC_BMOD, reg);
746 
747 	return (0);
748 }
749 
750 static int
751 dma_prepare(struct dwmmc_softc *sc, struct mmc_command *cmd)
752 {
753 	struct mmc_data *data;
754 	int err;
755 	int reg;
756 
757 	data = cmd->data;
758 
759 	reg = READ4(sc, SDMMC_INTMASK);
760 	reg &= ~(SDMMC_INTMASK_TXDR | SDMMC_INTMASK_RXDR);
761 	WRITE4(sc, SDMMC_INTMASK, reg);
762 
763 	err = bus_dmamap_load(sc->buf_tag, sc->buf_map,
764 		data->data, data->len, dwmmc_ring_setup,
765 		sc, BUS_DMA_NOWAIT);
766 	if (err != 0)
767 		panic("dmamap_load failed\n");
768 
769 	/* Ensure the device can see the desc */
770 	bus_dmamap_sync(sc->desc_tag, sc->desc_map,
771 	    BUS_DMASYNC_PREWRITE);
772 
773 	if (data->flags & MMC_DATA_WRITE)
774 		bus_dmamap_sync(sc->buf_tag, sc->buf_map,
775 			BUS_DMASYNC_PREWRITE);
776 	else
777 		bus_dmamap_sync(sc->buf_tag, sc->buf_map,
778 			BUS_DMASYNC_PREREAD);
779 
780 	reg = (DEF_MSIZE << SDMMC_FIFOTH_MSIZE_S);
781 	reg |= ((sc->fifo_depth / 2) - 1) << SDMMC_FIFOTH_RXWMARK_S;
782 	reg |= (sc->fifo_depth / 2) << SDMMC_FIFOTH_TXWMARK_S;
783 
784 	WRITE4(sc, SDMMC_FIFOTH, reg);
785 	wmb();
786 
787 	reg = READ4(sc, SDMMC_CTRL);
788 	reg |= (SDMMC_CTRL_USE_IDMAC | SDMMC_CTRL_DMA_ENABLE);
789 	WRITE4(sc, SDMMC_CTRL, reg);
790 	wmb();
791 
792 	reg = READ4(sc, SDMMC_BMOD);
793 	reg |= (SDMMC_BMOD_DE | SDMMC_BMOD_FB);
794 	WRITE4(sc, SDMMC_BMOD, reg);
795 
796 	/* Start */
797 	WRITE4(sc, SDMMC_PLDMND, 1);
798 
799 	return (0);
800 }
801 
802 static int
803 pio_prepare(struct dwmmc_softc *sc, struct mmc_command *cmd)
804 {
805 	struct mmc_data *data;
806 	int reg;
807 
808 	data = cmd->data;
809 	data->xfer_len = 0;
810 
811 	reg = (DEF_MSIZE << SDMMC_FIFOTH_MSIZE_S);
812 	reg |= ((sc->fifo_depth / 2) - 1) << SDMMC_FIFOTH_RXWMARK_S;
813 	reg |= (sc->fifo_depth / 2) << SDMMC_FIFOTH_TXWMARK_S;
814 
815 	WRITE4(sc, SDMMC_FIFOTH, reg);
816 	wmb();
817 
818 	return (0);
819 }
820 
821 static void
822 pio_read(struct dwmmc_softc *sc, struct mmc_command *cmd)
823 {
824 	struct mmc_data *data;
825 	uint32_t *p, status;
826 
827 	if (cmd == NULL || cmd->data == NULL)
828 		return;
829 
830 	data = cmd->data;
831 	if ((data->flags & MMC_DATA_READ) == 0)
832 		return;
833 
834 	KASSERT((data->xfer_len & 3) == 0, ("xfer_len not aligned"));
835 	p = (uint32_t *)data->data + (data->xfer_len >> 2);
836 
837 	while (data->xfer_len < data->len) {
838 		status = READ4(sc, SDMMC_STATUS);
839 		if (status & SDMMC_STATUS_FIFO_EMPTY)
840 			break;
841 		*p++ = READ4(sc, SDMMC_DATA);
842 		data->xfer_len += 4;
843 	}
844 
845 	WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_RXDR);
846 }
847 
848 static void
849 pio_write(struct dwmmc_softc *sc, struct mmc_command *cmd)
850 {
851 	struct mmc_data *data;
852 	uint32_t *p, status;
853 
854 	if (cmd == NULL || cmd->data == NULL)
855 		return;
856 
857 	data = cmd->data;
858 	if ((data->flags & MMC_DATA_WRITE) == 0)
859 		return;
860 
861 	KASSERT((data->xfer_len & 3) == 0, ("xfer_len not aligned"));
862 	p = (uint32_t *)data->data + (data->xfer_len >> 2);
863 
864 	while (data->xfer_len < data->len) {
865 		status = READ4(sc, SDMMC_STATUS);
866 		if (status & SDMMC_STATUS_FIFO_FULL)
867 			break;
868 		WRITE4(sc, SDMMC_DATA, *p++);
869 		data->xfer_len += 4;
870 	}
871 
872 	WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_TXDR);
873 }
874 
875 static void
876 dwmmc_start_cmd(struct dwmmc_softc *sc, struct mmc_command *cmd)
877 {
878 	struct mmc_data *data;
879 	uint32_t blksz;
880 	uint32_t cmdr;
881 
882 	sc->curcmd = cmd;
883 	data = cmd->data;
884 
885 	if ((sc->hwtype & HWTYPE_MASK) == HWTYPE_ROCKCHIP)
886 		dwmmc_setup_bus(sc, sc->host.ios.clock);
887 
888 	/* XXX Upper layers don't always set this */
889 	cmd->mrq = sc->req;
890 
891 	/* Begin setting up command register. */
892 
893 	cmdr = cmd->opcode;
894 
895 	dprintf("cmd->opcode 0x%08x\n", cmd->opcode);
896 
897 	if (cmd->opcode == MMC_STOP_TRANSMISSION ||
898 	    cmd->opcode == MMC_GO_IDLE_STATE ||
899 	    cmd->opcode == MMC_GO_INACTIVE_STATE)
900 		cmdr |= SDMMC_CMD_STOP_ABORT;
901 	else if (cmd->opcode != MMC_SEND_STATUS && data)
902 		cmdr |= SDMMC_CMD_WAIT_PRVDATA;
903 
904 	/* Set up response handling. */
905 	if (MMC_RSP(cmd->flags) != MMC_RSP_NONE) {
906 		cmdr |= SDMMC_CMD_RESP_EXP;
907 		if (cmd->flags & MMC_RSP_136)
908 			cmdr |= SDMMC_CMD_RESP_LONG;
909 	}
910 
911 	if (cmd->flags & MMC_RSP_CRC)
912 		cmdr |= SDMMC_CMD_RESP_CRC;
913 
914 	/*
915 	 * XXX: Not all platforms want this.
916 	 */
917 	cmdr |= SDMMC_CMD_USE_HOLD_REG;
918 
919 	if ((sc->flags & CARD_INIT_DONE) == 0) {
920 		sc->flags |= (CARD_INIT_DONE);
921 		cmdr |= SDMMC_CMD_SEND_INIT;
922 	}
923 
924 	if (data) {
925 		if ((cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
926 		     cmd->opcode == MMC_READ_MULTIPLE_BLOCK) &&
927 		     sc->use_auto_stop)
928 			cmdr |= SDMMC_CMD_SEND_ASTOP;
929 
930 		cmdr |= SDMMC_CMD_DATA_EXP;
931 		if (data->flags & MMC_DATA_STREAM)
932 			cmdr |= SDMMC_CMD_MODE_STREAM;
933 		if (data->flags & MMC_DATA_WRITE)
934 			cmdr |= SDMMC_CMD_DATA_WRITE;
935 
936 		WRITE4(sc, SDMMC_TMOUT, 0xffffffff);
937 		WRITE4(sc, SDMMC_BYTCNT, data->len);
938 		blksz = (data->len < MMC_SECTOR_SIZE) ? \
939 			 data->len : MMC_SECTOR_SIZE;
940 		WRITE4(sc, SDMMC_BLKSIZ, blksz);
941 
942 		if (sc->use_pio) {
943 			pio_prepare(sc, cmd);
944 		} else {
945 			dma_prepare(sc, cmd);
946 		}
947 		wmb();
948 	}
949 
950 	dprintf("cmdr 0x%08x\n", cmdr);
951 
952 	WRITE4(sc, SDMMC_CMDARG, cmd->arg);
953 	wmb();
954 	WRITE4(sc, SDMMC_CMD, cmdr | SDMMC_CMD_START);
955 };
956 
957 static void
958 dwmmc_next_operation(struct dwmmc_softc *sc)
959 {
960 	struct mmc_request *req;
961 
962 	req = sc->req;
963 	if (req == NULL)
964 		return;
965 
966 	sc->acd_rcvd = 0;
967 	sc->dto_rcvd = 0;
968 	sc->cmd_done = 0;
969 
970 	/*
971 	 * XXX: Wait until card is still busy.
972 	 * We do need this to prevent data timeouts,
973 	 * mostly caused by multi-block write command
974 	 * followed by single-read.
975 	 */
976 	while(READ4(sc, SDMMC_STATUS) & (SDMMC_STATUS_DATA_BUSY))
977 		continue;
978 
979 	if (sc->flags & PENDING_CMD) {
980 		sc->flags &= ~PENDING_CMD;
981 		dwmmc_start_cmd(sc, req->cmd);
982 		return;
983 	} else if (sc->flags & PENDING_STOP && !sc->use_auto_stop) {
984 		sc->flags &= ~PENDING_STOP;
985 		dwmmc_start_cmd(sc, req->stop);
986 		return;
987 	}
988 
989 	sc->req = NULL;
990 	sc->curcmd = NULL;
991 	req->done(req);
992 }
993 
994 static int
995 dwmmc_request(device_t brdev, device_t reqdev, struct mmc_request *req)
996 {
997 	struct dwmmc_softc *sc;
998 
999 	sc = device_get_softc(brdev);
1000 
1001 	dprintf("%s\n", __func__);
1002 
1003 	DWMMC_LOCK(sc);
1004 
1005 	if (sc->req != NULL) {
1006 		DWMMC_UNLOCK(sc);
1007 		return (EBUSY);
1008 	}
1009 
1010 	sc->req = req;
1011 	sc->flags |= PENDING_CMD;
1012 	if (sc->req->stop)
1013 		sc->flags |= PENDING_STOP;
1014 	dwmmc_next_operation(sc);
1015 
1016 	DWMMC_UNLOCK(sc);
1017 	return (0);
1018 }
1019 
1020 static int
1021 dwmmc_get_ro(device_t brdev, device_t reqdev)
1022 {
1023 
1024 	dprintf("%s\n", __func__);
1025 
1026 	return (0);
1027 }
1028 
1029 static int
1030 dwmmc_acquire_host(device_t brdev, device_t reqdev)
1031 {
1032 	struct dwmmc_softc *sc;
1033 
1034 	sc = device_get_softc(brdev);
1035 
1036 	DWMMC_LOCK(sc);
1037 	while (sc->bus_busy)
1038 		msleep(sc, &sc->sc_mtx, PZERO, "dwmmcah", hz / 5);
1039 	sc->bus_busy++;
1040 	DWMMC_UNLOCK(sc);
1041 	return (0);
1042 }
1043 
1044 static int
1045 dwmmc_release_host(device_t brdev, device_t reqdev)
1046 {
1047 	struct dwmmc_softc *sc;
1048 
1049 	sc = device_get_softc(brdev);
1050 
1051 	DWMMC_LOCK(sc);
1052 	sc->bus_busy--;
1053 	wakeup(sc);
1054 	DWMMC_UNLOCK(sc);
1055 	return (0);
1056 }
1057 
1058 static int
1059 dwmmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1060 {
1061 	struct dwmmc_softc *sc;
1062 
1063 	sc = device_get_softc(bus);
1064 
1065 	switch (which) {
1066 	default:
1067 		return (EINVAL);
1068 	case MMCBR_IVAR_BUS_MODE:
1069 		*(int *)result = sc->host.ios.bus_mode;
1070 		break;
1071 	case MMCBR_IVAR_BUS_WIDTH:
1072 		*(int *)result = sc->host.ios.bus_width;
1073 		break;
1074 	case MMCBR_IVAR_CHIP_SELECT:
1075 		*(int *)result = sc->host.ios.chip_select;
1076 		break;
1077 	case MMCBR_IVAR_CLOCK:
1078 		*(int *)result = sc->host.ios.clock;
1079 		break;
1080 	case MMCBR_IVAR_F_MIN:
1081 		*(int *)result = sc->host.f_min;
1082 		break;
1083 	case MMCBR_IVAR_F_MAX:
1084 		*(int *)result = sc->host.f_max;
1085 		break;
1086 	case MMCBR_IVAR_HOST_OCR:
1087 		*(int *)result = sc->host.host_ocr;
1088 		break;
1089 	case MMCBR_IVAR_MODE:
1090 		*(int *)result = sc->host.mode;
1091 		break;
1092 	case MMCBR_IVAR_OCR:
1093 		*(int *)result = sc->host.ocr;
1094 		break;
1095 	case MMCBR_IVAR_POWER_MODE:
1096 		*(int *)result = sc->host.ios.power_mode;
1097 		break;
1098 	case MMCBR_IVAR_VDD:
1099 		*(int *)result = sc->host.ios.vdd;
1100 		break;
1101 	case MMCBR_IVAR_CAPS:
1102 		sc->host.caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
1103 		*(int *)result = sc->host.caps;
1104 		break;
1105 	case MMCBR_IVAR_MAX_DATA:
1106 		*(int *)result = sc->desc_count;
1107 	}
1108 	return (0);
1109 }
1110 
1111 static int
1112 dwmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1113 {
1114 	struct dwmmc_softc *sc;
1115 
1116 	sc = device_get_softc(bus);
1117 
1118 	switch (which) {
1119 	default:
1120 		return (EINVAL);
1121 	case MMCBR_IVAR_BUS_MODE:
1122 		sc->host.ios.bus_mode = value;
1123 		break;
1124 	case MMCBR_IVAR_BUS_WIDTH:
1125 		sc->host.ios.bus_width = value;
1126 		break;
1127 	case MMCBR_IVAR_CHIP_SELECT:
1128 		sc->host.ios.chip_select = value;
1129 		break;
1130 	case MMCBR_IVAR_CLOCK:
1131 		sc->host.ios.clock = value;
1132 		break;
1133 	case MMCBR_IVAR_MODE:
1134 		sc->host.mode = value;
1135 		break;
1136 	case MMCBR_IVAR_OCR:
1137 		sc->host.ocr = value;
1138 		break;
1139 	case MMCBR_IVAR_POWER_MODE:
1140 		sc->host.ios.power_mode = value;
1141 		break;
1142 	case MMCBR_IVAR_VDD:
1143 		sc->host.ios.vdd = value;
1144 		break;
1145 	/* These are read-only */
1146 	case MMCBR_IVAR_CAPS:
1147 	case MMCBR_IVAR_HOST_OCR:
1148 	case MMCBR_IVAR_F_MIN:
1149 	case MMCBR_IVAR_F_MAX:
1150 	case MMCBR_IVAR_MAX_DATA:
1151 		return (EINVAL);
1152 	}
1153 	return (0);
1154 }
1155 
1156 static device_method_t dwmmc_methods[] = {
1157 	DEVMETHOD(device_probe,		dwmmc_probe),
1158 	DEVMETHOD(device_attach,	dwmmc_attach),
1159 
1160 	/* Bus interface */
1161 	DEVMETHOD(bus_read_ivar,	dwmmc_read_ivar),
1162 	DEVMETHOD(bus_write_ivar,	dwmmc_write_ivar),
1163 
1164 	/* mmcbr_if */
1165 	DEVMETHOD(mmcbr_update_ios,	dwmmc_update_ios),
1166 	DEVMETHOD(mmcbr_request,	dwmmc_request),
1167 	DEVMETHOD(mmcbr_get_ro,		dwmmc_get_ro),
1168 	DEVMETHOD(mmcbr_acquire_host,	dwmmc_acquire_host),
1169 	DEVMETHOD(mmcbr_release_host,	dwmmc_release_host),
1170 
1171 	DEVMETHOD_END
1172 };
1173 
1174 driver_t dwmmc_driver = {
1175 	"dwmmc",
1176 	dwmmc_methods,
1177 	sizeof(struct dwmmc_softc),
1178 };
1179 
1180 static devclass_t dwmmc_devclass;
1181 
1182 DRIVER_MODULE(dwmmc, simplebus, dwmmc_driver, dwmmc_devclass, NULL, NULL);
1183 DRIVER_MODULE(dwmmc, ofwbus, dwmmc_driver, dwmmc_devclass, NULL, NULL);
1184 #ifndef MMCCAM
1185 MMC_DECLARE_BRIDGE(dwmmc);
1186 #endif
1187