2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * Synopsys DesignWare Mobile Storage Host Controller
33 * Chapter 14, Altera Cyclone V Device Handbook (CV-5V2 2014.07.22)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
46 #include <sys/timeet.h>
47 #include <sys/timetc.h>
49 #include <dev/mmc/bridge.h>
50 #include <dev/mmc/mmcreg.h>
51 #include <dev/mmc/mmcbrvar.h>
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
58 #include <machine/bus.h>
59 #include <machine/fdt.h>
60 #include <machine/cpu.h>
61 #include <machine/intr.h>
63 #include <dev/mmc/host/dwmmc.h>
67 #define dprintf(x, arg...)
69 #define READ4(_sc, _reg) \
70 bus_read_4((_sc)->res[0], _reg)
71 #define WRITE4(_sc, _reg, _val) \
72 bus_write_4((_sc)->res[0], _reg, _val)
74 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
76 #define DWMMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
77 #define DWMMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
78 #define DWMMC_LOCK_INIT(_sc) \
79 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
81 #define DWMMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
82 #define DWMMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
83 #define DWMMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
85 #define PENDING_CMD 0x01
86 #define PENDING_STOP 0x02
87 #define CARD_INIT_DONE 0x04
89 #define DWMMC_DATA_ERR_FLAGS (SDMMC_INTMASK_DRT | SDMMC_INTMASK_DCRC \
90 |SDMMC_INTMASK_HTO | SDMMC_INTMASK_SBE \
92 #define DWMMC_CMD_ERR_FLAGS (SDMMC_INTMASK_RTO | SDMMC_INTMASK_RCRC \
94 #define DWMMC_ERR_FLAGS (DWMMC_DATA_ERR_FLAGS | DWMMC_CMD_ERR_FLAGS \
97 #define DES0_DIC (1 << 1)
98 #define DES0_LD (1 << 2)
99 #define DES0_FS (1 << 3)
100 #define DES0_CH (1 << 4)
101 #define DES0_ER (1 << 5)
102 #define DES0_CES (1 << 30)
103 #define DES0_OWN (1 << 31)
105 #define DES1_BS1_MASK 0xfff
106 #define DES1_BS1_SHIFT 0
109 uint32_t des0; /* control */
110 uint32_t des1; /* bufsize */
111 uint32_t des2; /* buf1 phys addr */
112 uint32_t des3; /* buf2 phys addr or next descr */
115 #define DESC_COUNT 256
116 #define DESC_SIZE (sizeof(struct idmac_desc) * DESC_COUNT)
117 #define DEF_MSIZE 0x2 /* Burst size of multiple transaction */
120 struct resource *res[2];
122 bus_space_handle_t bsh;
125 struct mmc_host host;
127 struct mmc_request *req;
128 struct mmc_command *curcmd;
131 uint32_t use_auto_stop;
133 bus_dma_tag_t desc_tag;
134 bus_dmamap_t desc_map;
135 struct idmac_desc *desc_ring;
136 bus_addr_t desc_ring_paddr;
137 bus_dma_tag_t buf_tag;
138 bus_dmamap_t buf_map;
151 static void dwmmc_next_operation(struct dwmmc_softc *);
152 static int dwmmc_setup_bus(struct dwmmc_softc *, int);
153 static int dma_done(struct dwmmc_softc *, struct mmc_command *);
154 static int dma_stop(struct dwmmc_softc *);
156 static struct resource_spec dwmmc_spec[] = {
157 { SYS_RES_MEMORY, 0, RF_ACTIVE },
158 { SYS_RES_IRQ, 0, RF_ACTIVE },
168 #define HWTYPE_MASK (0x0000ffff)
169 #define HWFLAG_MASK (0xffff << 16)
171 static struct ofw_compat_data compat_data[] = {
172 {"altr,socfpga-dw-mshc", HWTYPE_ALTERA},
173 {"samsung,exynos5420-dw-mshc", HWTYPE_EXYNOS},
178 dwmmc_get1paddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
183 *(bus_addr_t *)arg = segs[0].ds_addr;
187 dwmmc_ring_setup(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
189 struct dwmmc_softc *sc;
197 dprintf("nsegs %d seg0len %lu\n", nsegs, segs[0].ds_len);
199 for (idx = 0; idx < nsegs; idx++) {
200 sc->desc_ring[idx].des0 = (DES0_OWN | DES0_DIC | DES0_CH);
201 sc->desc_ring[idx].des1 = segs[idx].ds_len;
202 sc->desc_ring[idx].des2 = segs[idx].ds_addr;
205 sc->desc_ring[idx].des0 |= DES0_FS;
207 if (idx == (nsegs - 1)) {
208 sc->desc_ring[idx].des0 &= ~(DES0_DIC | DES0_CH);
209 sc->desc_ring[idx].des0 |= DES0_LD;
215 dwmmc_ctrl_reset(struct dwmmc_softc *sc, int reset_bits)
220 reg = READ4(sc, SDMMC_CTRL);
222 WRITE4(sc, SDMMC_CTRL, reg);
224 /* Wait reset done */
225 for (i = 0; i < 100; i++) {
226 if (!(READ4(sc, SDMMC_CTRL) & reset_bits))
231 device_printf(sc->dev, "Reset failed\n");
237 dma_setup(struct dwmmc_softc *sc)
244 * Set up TX descriptor ring, descriptors, and dma maps.
246 error = bus_dma_tag_create(
247 bus_get_dma_tag(sc->dev), /* Parent tag. */
248 4096, 0, /* alignment, boundary */
249 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
250 BUS_SPACE_MAXADDR, /* highaddr */
251 NULL, NULL, /* filter, filterarg */
252 DESC_SIZE, 1, /* maxsize, nsegments */
253 DESC_SIZE, /* maxsegsize */
255 NULL, NULL, /* lockfunc, lockarg */
258 device_printf(sc->dev,
259 "could not create ring DMA tag.\n");
263 error = bus_dmamem_alloc(sc->desc_tag, (void**)&sc->desc_ring,
264 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
267 device_printf(sc->dev,
268 "could not allocate descriptor ring.\n");
272 error = bus_dmamap_load(sc->desc_tag, sc->desc_map,
273 sc->desc_ring, DESC_SIZE, dwmmc_get1paddr,
274 &sc->desc_ring_paddr, 0);
276 device_printf(sc->dev,
277 "could not load descriptor ring map.\n");
281 for (idx = 0; idx < DESC_COUNT; idx++) {
282 sc->desc_ring[idx].des0 = DES0_CH;
283 sc->desc_ring[idx].des1 = 0;
284 nidx = (idx + 1) % DESC_COUNT;
285 sc->desc_ring[idx].des3 = sc->desc_ring_paddr + \
286 (nidx * sizeof(struct idmac_desc));
289 error = bus_dma_tag_create(
290 bus_get_dma_tag(sc->dev), /* Parent tag. */
291 4096, 0, /* alignment, boundary */
292 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
293 BUS_SPACE_MAXADDR, /* highaddr */
294 NULL, NULL, /* filter, filterarg */
295 DESC_COUNT*MMC_SECTOR_SIZE, /* maxsize */
296 DESC_COUNT, /* nsegments */
297 MMC_SECTOR_SIZE, /* maxsegsize */
299 NULL, NULL, /* lockfunc, lockarg */
302 device_printf(sc->dev,
303 "could not create ring DMA tag.\n");
307 error = bus_dmamap_create(sc->buf_tag, 0,
310 device_printf(sc->dev,
311 "could not create TX buffer DMA map.\n");
319 dwmmc_cmd_done(struct dwmmc_softc *sc)
321 struct mmc_command *cmd;
327 if (cmd->flags & MMC_RSP_PRESENT) {
328 if (cmd->flags & MMC_RSP_136) {
329 cmd->resp[3] = READ4(sc, SDMMC_RESP0);
330 cmd->resp[2] = READ4(sc, SDMMC_RESP1);
331 cmd->resp[1] = READ4(sc, SDMMC_RESP2);
332 cmd->resp[0] = READ4(sc, SDMMC_RESP3);
337 cmd->resp[0] = READ4(sc, SDMMC_RESP0);
343 dwmmc_tasklet(struct dwmmc_softc *sc)
345 struct mmc_command *cmd;
351 if (cmd->error != MMC_ERR_NONE) {
352 dwmmc_next_operation(sc);
353 } else if (!cmd->data && sc->cmd_done) {
354 dwmmc_next_operation(sc);
355 } else if (cmd->data && sc->dto_rcvd) {
356 if ((cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
357 cmd->opcode == MMC_READ_MULTIPLE_BLOCK) &&
360 dwmmc_next_operation(sc);
362 dwmmc_next_operation(sc);
368 dwmmc_intr(void *arg)
370 struct mmc_command *cmd;
371 struct dwmmc_softc *sc;
380 /* First handle SDMMC controller interrupts */
381 reg = READ4(sc, SDMMC_MINTSTS);
383 dprintf("%s 0x%08x\n", __func__, reg);
385 if (reg & DWMMC_CMD_ERR_FLAGS) {
386 WRITE4(sc, SDMMC_RINTSTS, DWMMC_CMD_ERR_FLAGS);
387 dprintf("cmd err 0x%08x cmd 0x%08x\n",
389 cmd->error = MMC_ERR_TIMEOUT;
392 if (reg & DWMMC_DATA_ERR_FLAGS) {
393 WRITE4(sc, SDMMC_RINTSTS, DWMMC_DATA_ERR_FLAGS);
394 dprintf("data err 0x%08x cmd 0x%08x\n",
396 cmd->error = MMC_ERR_FAILED;
404 if (reg & SDMMC_INTMASK_CMD_DONE) {
407 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_CMD_DONE);
410 if (reg & SDMMC_INTMASK_ACD) {
412 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_ACD);
415 if (reg & SDMMC_INTMASK_DTO) {
417 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_DTO);
420 if (reg & SDMMC_INTMASK_CD) {
421 /* XXX: Handle card detect */
422 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_CD);
426 /* Now handle DMA interrupts */
427 reg = READ4(sc, SDMMC_IDSTS);
429 dprintf("dma intr 0x%08x\n", reg);
430 if (reg & (SDMMC_IDINTEN_TI | SDMMC_IDINTEN_RI)) {
431 WRITE4(sc, SDMMC_IDSTS, (SDMMC_IDINTEN_TI |
433 WRITE4(sc, SDMMC_IDSTS, SDMMC_IDINTEN_NI);
444 parse_fdt(struct dwmmc_softc *sc)
446 pcell_t dts_value[3];
450 if ((node = ofw_bus_get_node(sc->dev)) == -1)
454 if ((len = OF_getproplen(node, "fifo-depth")) <= 0)
456 OF_getencprop(node, "fifo-depth", dts_value, len);
457 sc->fifo_depth = dts_value[0];
460 if ((len = OF_getproplen(node, "num-slots")) <= 0)
462 OF_getencprop(node, "num-slots", dts_value, len);
463 sc->num_slots = dts_value[0];
466 * We need some platform-specific code to know
467 * what the clock is supplied for our device.
468 * For now rely on the value specified in FDT.
470 if ((len = OF_getproplen(node, "bus-frequency")) <= 0)
472 OF_getencprop(node, "bus-frequency", dts_value, len);
473 sc->bus_hz = dts_value[0];
476 * Platform-specific stuff
477 * XXX: Move to separate file
480 if ((sc->hwtype & HWTYPE_MASK) != HWTYPE_EXYNOS)
483 if ((len = OF_getproplen(node, "samsung,dw-mshc-ciu-div")) <= 0)
485 OF_getencprop(node, "samsung,dw-mshc-ciu-div", dts_value, len);
486 sc->sdr_timing = (dts_value[0] << SDMMC_CLKSEL_DIVIDER_SHIFT);
487 sc->ddr_timing = (dts_value[0] << SDMMC_CLKSEL_DIVIDER_SHIFT);
489 if ((len = OF_getproplen(node, "samsung,dw-mshc-sdr-timing")) <= 0)
491 OF_getencprop(node, "samsung,dw-mshc-sdr-timing", dts_value, len);
492 sc->sdr_timing |= ((dts_value[0] << SDMMC_CLKSEL_SAMPLE_SHIFT) |
493 (dts_value[1] << SDMMC_CLKSEL_DRIVE_SHIFT));
495 if ((len = OF_getproplen(node, "samsung,dw-mshc-ddr-timing")) <= 0)
497 OF_getencprop(node, "samsung,dw-mshc-ddr-timing", dts_value, len);
498 sc->ddr_timing |= ((dts_value[0] << SDMMC_CLKSEL_SAMPLE_SHIFT) |
499 (dts_value[1] << SDMMC_CLKSEL_DRIVE_SHIFT));
505 dwmmc_probe(device_t dev)
509 if (!ofw_bus_status_okay(dev))
512 hwtype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
513 if (hwtype == HWTYPE_NONE)
516 device_set_desc(dev, "Synopsys DesignWare Mobile "
517 "Storage Host Controller");
518 return (BUS_PROBE_DEFAULT);
522 dwmmc_attach(device_t dev)
524 struct dwmmc_softc *sc;
529 sc = device_get_softc(dev);
532 sc->hwtype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
534 /* Why not to use Auto Stop? It save a hundred of irq per second */
535 sc->use_auto_stop = 1;
537 error = parse_fdt(sc);
539 device_printf(dev, "Can't get FDT property.\n");
545 if (bus_alloc_resources(dev, dwmmc_spec, sc->res)) {
546 device_printf(dev, "could not allocate resources\n");
550 /* Memory interface */
551 sc->bst = rman_get_bustag(sc->res[0]);
552 sc->bsh = rman_get_bushandle(sc->res[0]);
554 /* Setup interrupt handler. */
555 error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_NET | INTR_MPSAFE,
556 NULL, dwmmc_intr, sc, &sc->intr_cookie);
558 device_printf(dev, "could not setup interrupt handler.\n");
562 device_printf(dev, "Hardware version ID is %04x\n",
563 READ4(sc, SDMMC_VERID) & 0xffff);
565 WRITE4(sc, EMMCP_MPSBEGIN0, 0);
566 WRITE4(sc, EMMCP_SEND0, 0);
567 WRITE4(sc, EMMCP_CTRL0, (MPSCTRL_SECURE_READ_BIT |
568 MPSCTRL_SECURE_WRITE_BIT |
569 MPSCTRL_NON_SECURE_READ_BIT |
570 MPSCTRL_NON_SECURE_WRITE_BIT |
573 /* XXX: we support operation for slot index 0 only */
575 WRITE4(sc, SDMMC_PWREN, (1 << slot));
578 if (dwmmc_ctrl_reset(sc, (SDMMC_CTRL_RESET |
579 SDMMC_CTRL_FIFO_RESET |
580 SDMMC_CTRL_DMA_RESET)))
583 dwmmc_setup_bus(sc, sc->host.f_min);
588 /* Install desc base */
589 WRITE4(sc, SDMMC_DBADDR, sc->desc_ring_paddr);
591 /* Enable DMA interrupts */
592 WRITE4(sc, SDMMC_IDSTS, SDMMC_IDINTEN_MASK);
593 WRITE4(sc, SDMMC_IDINTEN, (SDMMC_IDINTEN_NI |
597 /* Clear and disable interrups for a while */
598 WRITE4(sc, SDMMC_RINTSTS, 0xffffffff);
599 WRITE4(sc, SDMMC_INTMASK, 0);
601 /* Maximum timeout */
602 WRITE4(sc, SDMMC_TMOUT, 0xffffffff);
604 /* Enable interrupts */
605 WRITE4(sc, SDMMC_RINTSTS, 0xffffffff);
606 WRITE4(sc, SDMMC_INTMASK, (SDMMC_INTMASK_CMD_DONE |
613 WRITE4(sc, SDMMC_CTRL, SDMMC_CTRL_INT_ENABLE);
615 sc->host.f_min = 400000;
616 sc->host.f_max = 200000000;
617 sc->host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340;
618 sc->host.caps = MMC_CAP_4_BIT_DATA;
620 child = device_add_child(dev, "mmc", 0);
621 return (bus_generic_attach(dev));
625 dwmmc_setup_bus(struct dwmmc_softc *sc, int freq)
631 WRITE4(sc, SDMMC_CLKENA, 0);
632 WRITE4(sc, SDMMC_CMD, (SDMMC_CMD_WAIT_PRVDATA |
633 SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START));
638 device_printf(sc->dev, "Failed update clk\n");
641 } while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
646 WRITE4(sc, SDMMC_CLKENA, 0);
647 WRITE4(sc, SDMMC_CLKSRC, 0);
649 div = (sc->bus_hz != freq) ? DIV_ROUND_UP(sc->bus_hz, 2 * freq) : 0;
651 WRITE4(sc, SDMMC_CLKDIV, div);
652 WRITE4(sc, SDMMC_CMD, (SDMMC_CMD_WAIT_PRVDATA |
653 SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START));
658 device_printf(sc->dev, "Failed to update clk");
661 } while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
663 WRITE4(sc, SDMMC_CLKENA, (SDMMC_CLKENA_CCLK_EN | SDMMC_CLKENA_LP));
664 WRITE4(sc, SDMMC_CMD, SDMMC_CMD_WAIT_PRVDATA |
665 SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START);
670 device_printf(sc->dev, "Failed to enable clk\n");
673 } while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
679 dwmmc_update_ios(device_t brdev, device_t reqdev)
681 struct dwmmc_softc *sc;
684 sc = device_get_softc(brdev);
687 dprintf("Setting up clk %u bus_width %d\n",
688 ios->clock, ios->bus_width);
690 dwmmc_setup_bus(sc, ios->clock);
692 if (ios->bus_width == bus_width_8)
693 WRITE4(sc, SDMMC_CTYPE, SDMMC_CTYPE_8BIT);
694 else if (ios->bus_width == bus_width_4)
695 WRITE4(sc, SDMMC_CTYPE, SDMMC_CTYPE_4BIT);
697 WRITE4(sc, SDMMC_CTYPE, 0);
699 if ((sc->hwtype & HWTYPE_MASK) == HWTYPE_EXYNOS) {
700 /* XXX: take care about DDR or SDR use here */
701 WRITE4(sc, SDMMC_CLKSEL, sc->sdr_timing);
705 * XXX: take care about DDR bit
707 * reg = READ4(sc, SDMMC_UHS_REG);
708 * reg |= (SDMMC_UHS_REG_DDR);
709 * WRITE4(sc, SDMMC_UHS_REG, reg);
716 dma_done(struct dwmmc_softc *sc, struct mmc_command *cmd)
718 struct mmc_data *data;
722 if (data->flags & MMC_DATA_WRITE)
723 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
724 BUS_DMASYNC_POSTWRITE);
726 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
727 BUS_DMASYNC_POSTREAD);
729 bus_dmamap_unload(sc->buf_tag, sc->buf_map);
735 dma_stop(struct dwmmc_softc *sc)
739 reg = READ4(sc, SDMMC_CTRL);
740 reg &= ~(SDMMC_CTRL_USE_IDMAC);
741 reg |= (SDMMC_CTRL_DMA_RESET);
742 WRITE4(sc, SDMMC_CTRL, reg);
744 reg = READ4(sc, SDMMC_BMOD);
745 reg &= ~(SDMMC_BMOD_DE | SDMMC_BMOD_FB);
746 reg |= (SDMMC_BMOD_SWR);
747 WRITE4(sc, SDMMC_BMOD, reg);
753 dma_prepare(struct dwmmc_softc *sc, struct mmc_command *cmd)
755 struct mmc_data *data;
763 reg = READ4(sc, SDMMC_INTMASK);
764 reg &= ~(SDMMC_INTMASK_TXDR | SDMMC_INTMASK_RXDR);
765 WRITE4(sc, SDMMC_INTMASK, reg);
767 err = bus_dmamap_load(sc->buf_tag, sc->buf_map,
768 data->data, data->len, dwmmc_ring_setup,
771 panic("dmamap_load failed\n");
773 if (data->flags & MMC_DATA_WRITE)
774 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
775 BUS_DMASYNC_PREWRITE);
777 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
778 BUS_DMASYNC_PREREAD);
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;
784 WRITE4(sc, SDMMC_FIFOTH, reg);
787 reg = READ4(sc, SDMMC_CTRL);
788 reg |= (SDMMC_CTRL_USE_IDMAC | SDMMC_CTRL_DMA_ENABLE);
789 WRITE4(sc, SDMMC_CTRL, reg);
792 reg = READ4(sc, SDMMC_BMOD);
793 reg |= (SDMMC_BMOD_DE | SDMMC_BMOD_FB);
794 WRITE4(sc, SDMMC_BMOD, reg);
797 WRITE4(sc, SDMMC_PLDMND, 1);
803 dwmmc_start_cmd(struct dwmmc_softc *sc, struct mmc_command *cmd)
805 struct mmc_data *data;
812 /* XXX Upper layers don't always set this */
815 /* Begin setting up command register. */
819 dprintf("cmd->opcode 0x%08x\n", cmd->opcode);
821 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
822 cmd->opcode == MMC_GO_IDLE_STATE ||
823 cmd->opcode == MMC_GO_INACTIVE_STATE)
824 cmdr |= SDMMC_CMD_STOP_ABORT;
825 else if (cmd->opcode != MMC_SEND_STATUS && data)
826 cmdr |= SDMMC_CMD_WAIT_PRVDATA;
828 /* Set up response handling. */
829 if (MMC_RSP(cmd->flags) != MMC_RSP_NONE) {
830 cmdr |= SDMMC_CMD_RESP_EXP;
831 if (cmd->flags & MMC_RSP_136)
832 cmdr |= SDMMC_CMD_RESP_LONG;
835 if (cmd->flags & MMC_RSP_CRC)
836 cmdr |= SDMMC_CMD_RESP_CRC;
839 * XXX: Not all platforms want this.
841 cmdr |= SDMMC_CMD_USE_HOLD_REG;
843 if ((sc->flags & CARD_INIT_DONE) == 0) {
844 sc->flags |= (CARD_INIT_DONE);
845 cmdr |= SDMMC_CMD_SEND_INIT;
849 if ((cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
850 cmd->opcode == MMC_READ_MULTIPLE_BLOCK) &&
852 cmdr |= SDMMC_CMD_SEND_ASTOP;
854 cmdr |= SDMMC_CMD_DATA_EXP;
855 if (data->flags & MMC_DATA_STREAM)
856 cmdr |= SDMMC_CMD_MODE_STREAM;
857 if (data->flags & MMC_DATA_WRITE)
858 cmdr |= SDMMC_CMD_DATA_WRITE;
860 WRITE4(sc, SDMMC_TMOUT, 0xffffffff);
861 WRITE4(sc, SDMMC_BYTCNT, data->len);
862 blksz = (data->len < MMC_SECTOR_SIZE) ? \
863 data->len : MMC_SECTOR_SIZE;
864 WRITE4(sc, SDMMC_BLKSIZ, blksz);
866 dma_prepare(sc, cmd);
870 dprintf("cmdr 0x%08x\n", cmdr);
872 WRITE4(sc, SDMMC_CMDARG, cmd->arg);
874 WRITE4(sc, SDMMC_CMD, cmdr | SDMMC_CMD_START);
878 dwmmc_next_operation(struct dwmmc_softc *sc)
880 struct mmc_request *req;
891 * XXX: Wait until card is still busy.
892 * We do need this to prevent data timeouts,
893 * mostly caused by multi-block write command
894 * followed by single-read.
896 while(READ4(sc, SDMMC_STATUS) & (SDMMC_STATUS_DATA_BUSY))
899 if (sc->flags & PENDING_CMD) {
900 sc->flags &= ~PENDING_CMD;
901 dwmmc_start_cmd(sc, req->cmd);
903 } else if (sc->flags & PENDING_STOP && !sc->use_auto_stop) {
904 sc->flags &= ~PENDING_STOP;
905 dwmmc_start_cmd(sc, req->stop);
915 dwmmc_request(device_t brdev, device_t reqdev, struct mmc_request *req)
917 struct dwmmc_softc *sc;
919 sc = device_get_softc(brdev);
921 dprintf("%s\n", __func__);
925 if (sc->req != NULL) {
931 sc->flags |= PENDING_CMD;
933 sc->flags |= PENDING_STOP;
934 dwmmc_next_operation(sc);
941 dwmmc_get_ro(device_t brdev, device_t reqdev)
944 dprintf("%s\n", __func__);
950 dwmmc_acquire_host(device_t brdev, device_t reqdev)
952 struct dwmmc_softc *sc;
954 sc = device_get_softc(brdev);
958 msleep(sc, &sc->sc_mtx, PZERO, "dwmmcah", hz / 5);
965 dwmmc_release_host(device_t brdev, device_t reqdev)
967 struct dwmmc_softc *sc;
969 sc = device_get_softc(brdev);
979 dwmmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
981 struct dwmmc_softc *sc;
983 sc = device_get_softc(bus);
988 case MMCBR_IVAR_BUS_MODE:
989 *(int *)result = sc->host.ios.bus_mode;
991 case MMCBR_IVAR_BUS_WIDTH:
992 *(int *)result = sc->host.ios.bus_width;
994 case MMCBR_IVAR_CHIP_SELECT:
995 *(int *)result = sc->host.ios.chip_select;
997 case MMCBR_IVAR_CLOCK:
998 *(int *)result = sc->host.ios.clock;
1000 case MMCBR_IVAR_F_MIN:
1001 *(int *)result = sc->host.f_min;
1003 case MMCBR_IVAR_F_MAX:
1004 *(int *)result = sc->host.f_max;
1006 case MMCBR_IVAR_HOST_OCR:
1007 *(int *)result = sc->host.host_ocr;
1009 case MMCBR_IVAR_MODE:
1010 *(int *)result = sc->host.mode;
1012 case MMCBR_IVAR_OCR:
1013 *(int *)result = sc->host.ocr;
1015 case MMCBR_IVAR_POWER_MODE:
1016 *(int *)result = sc->host.ios.power_mode;
1018 case MMCBR_IVAR_VDD:
1019 *(int *)result = sc->host.ios.vdd;
1021 case MMCBR_IVAR_CAPS:
1022 sc->host.caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
1023 *(int *)result = sc->host.caps;
1025 case MMCBR_IVAR_MAX_DATA:
1026 *(int *)result = DESC_COUNT;
1032 dwmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1034 struct dwmmc_softc *sc;
1036 sc = device_get_softc(bus);
1041 case MMCBR_IVAR_BUS_MODE:
1042 sc->host.ios.bus_mode = value;
1044 case MMCBR_IVAR_BUS_WIDTH:
1045 sc->host.ios.bus_width = value;
1047 case MMCBR_IVAR_CHIP_SELECT:
1048 sc->host.ios.chip_select = value;
1050 case MMCBR_IVAR_CLOCK:
1051 sc->host.ios.clock = value;
1053 case MMCBR_IVAR_MODE:
1054 sc->host.mode = value;
1056 case MMCBR_IVAR_OCR:
1057 sc->host.ocr = value;
1059 case MMCBR_IVAR_POWER_MODE:
1060 sc->host.ios.power_mode = value;
1062 case MMCBR_IVAR_VDD:
1063 sc->host.ios.vdd = value;
1065 /* These are read-only */
1066 case MMCBR_IVAR_CAPS:
1067 case MMCBR_IVAR_HOST_OCR:
1068 case MMCBR_IVAR_F_MIN:
1069 case MMCBR_IVAR_F_MAX:
1070 case MMCBR_IVAR_MAX_DATA:
1076 static device_method_t dwmmc_methods[] = {
1077 DEVMETHOD(device_probe, dwmmc_probe),
1078 DEVMETHOD(device_attach, dwmmc_attach),
1081 DEVMETHOD(bus_read_ivar, dwmmc_read_ivar),
1082 DEVMETHOD(bus_write_ivar, dwmmc_write_ivar),
1085 DEVMETHOD(mmcbr_update_ios, dwmmc_update_ios),
1086 DEVMETHOD(mmcbr_request, dwmmc_request),
1087 DEVMETHOD(mmcbr_get_ro, dwmmc_get_ro),
1088 DEVMETHOD(mmcbr_acquire_host, dwmmc_acquire_host),
1089 DEVMETHOD(mmcbr_release_host, dwmmc_release_host),
1094 static driver_t dwmmc_driver = {
1097 sizeof(struct dwmmc_softc),
1100 static devclass_t dwmmc_devclass;
1102 DRIVER_MODULE(dwmmc, simplebus, dwmmc_driver, dwmmc_devclass, 0, 0);