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;
354 if (cmd->error != MMC_ERR_NONE || !cmd->data) {
355 dwmmc_next_operation(sc);
356 } else if (cmd->data && sc->dto_rcvd) {
357 if ((cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
358 cmd->opcode == MMC_READ_MULTIPLE_BLOCK) &&
361 dwmmc_next_operation(sc);
363 dwmmc_next_operation(sc);
369 dwmmc_intr(void *arg)
371 struct mmc_command *cmd;
372 struct dwmmc_softc *sc;
381 /* First handle SDMMC controller interrupts */
382 reg = READ4(sc, SDMMC_MINTSTS);
384 dprintf("%s 0x%08x\n", __func__, reg);
386 if (reg & DWMMC_CMD_ERR_FLAGS) {
387 WRITE4(sc, SDMMC_RINTSTS, DWMMC_CMD_ERR_FLAGS);
388 dprintf("cmd err 0x%08x cmd 0x%08x\n",
390 cmd->error = MMC_ERR_TIMEOUT;
393 if (reg & DWMMC_DATA_ERR_FLAGS) {
394 WRITE4(sc, SDMMC_RINTSTS, DWMMC_DATA_ERR_FLAGS);
395 dprintf("data err 0x%08x cmd 0x%08x\n",
397 cmd->error = MMC_ERR_FAILED;
402 if (reg & SDMMC_INTMASK_CMD_DONE) {
405 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_CMD_DONE);
408 if (reg & SDMMC_INTMASK_ACD) {
410 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_ACD);
413 if (reg & SDMMC_INTMASK_DTO) {
415 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_DTO);
418 if (reg & SDMMC_INTMASK_CD) {
419 /* XXX: Handle card detect */
420 WRITE4(sc, SDMMC_RINTSTS, SDMMC_INTMASK_CD);
424 /* Now handle DMA interrupts */
425 reg = READ4(sc, SDMMC_IDSTS);
427 dprintf("dma intr 0x%08x\n", reg);
428 if (reg & (SDMMC_IDINTEN_TI | SDMMC_IDINTEN_RI)) {
429 WRITE4(sc, SDMMC_IDSTS, (SDMMC_IDINTEN_TI |
431 WRITE4(sc, SDMMC_IDSTS, SDMMC_IDINTEN_NI);
442 parse_fdt(struct dwmmc_softc *sc)
444 pcell_t dts_value[3];
448 if ((node = ofw_bus_get_node(sc->dev)) == -1)
452 if ((len = OF_getproplen(node, "fifo-depth")) <= 0)
454 OF_getencprop(node, "fifo-depth", dts_value, len);
455 sc->fifo_depth = dts_value[0];
458 if ((len = OF_getproplen(node, "num-slots")) <= 0)
460 OF_getencprop(node, "num-slots", dts_value, len);
461 sc->num_slots = dts_value[0];
464 * We need some platform-specific code to know
465 * what the clock is supplied for our device.
466 * For now rely on the value specified in FDT.
468 if ((len = OF_getproplen(node, "bus-frequency")) <= 0)
470 OF_getencprop(node, "bus-frequency", dts_value, len);
471 sc->bus_hz = dts_value[0];
474 * Platform-specific stuff
475 * XXX: Move to separate file
478 if ((sc->hwtype & HWTYPE_MASK) != HWTYPE_EXYNOS)
481 if ((len = OF_getproplen(node, "samsung,dw-mshc-ciu-div")) <= 0)
483 OF_getencprop(node, "samsung,dw-mshc-ciu-div", dts_value, len);
484 sc->sdr_timing = (dts_value[0] << SDMMC_CLKSEL_DIVIDER_SHIFT);
485 sc->ddr_timing = (dts_value[0] << SDMMC_CLKSEL_DIVIDER_SHIFT);
487 if ((len = OF_getproplen(node, "samsung,dw-mshc-sdr-timing")) <= 0)
489 OF_getencprop(node, "samsung,dw-mshc-sdr-timing", dts_value, len);
490 sc->sdr_timing |= ((dts_value[0] << SDMMC_CLKSEL_SAMPLE_SHIFT) |
491 (dts_value[1] << SDMMC_CLKSEL_DRIVE_SHIFT));
493 if ((len = OF_getproplen(node, "samsung,dw-mshc-ddr-timing")) <= 0)
495 OF_getencprop(node, "samsung,dw-mshc-ddr-timing", dts_value, len);
496 sc->ddr_timing |= ((dts_value[0] << SDMMC_CLKSEL_SAMPLE_SHIFT) |
497 (dts_value[1] << SDMMC_CLKSEL_DRIVE_SHIFT));
503 dwmmc_probe(device_t dev)
507 if (!ofw_bus_status_okay(dev))
510 hwtype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
511 if (hwtype == HWTYPE_NONE)
514 device_set_desc(dev, "Synopsys DesignWare Mobile "
515 "Storage Host Controller");
516 return (BUS_PROBE_DEFAULT);
520 dwmmc_attach(device_t dev)
522 struct dwmmc_softc *sc;
527 sc = device_get_softc(dev);
530 sc->hwtype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
532 /* Why not to use Auto Stop? It save a hundred of irq per second */
533 sc->use_auto_stop = 1;
535 error = parse_fdt(sc);
537 device_printf(dev, "Can't get FDT property.\n");
543 if (bus_alloc_resources(dev, dwmmc_spec, sc->res)) {
544 device_printf(dev, "could not allocate resources\n");
548 /* Memory interface */
549 sc->bst = rman_get_bustag(sc->res[0]);
550 sc->bsh = rman_get_bushandle(sc->res[0]);
552 /* Setup interrupt handler. */
553 error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_NET | INTR_MPSAFE,
554 NULL, dwmmc_intr, sc, &sc->intr_cookie);
556 device_printf(dev, "could not setup interrupt handler.\n");
560 device_printf(dev, "Hardware version ID is %04x\n",
561 READ4(sc, SDMMC_VERID) & 0xffff);
563 WRITE4(sc, EMMCP_MPSBEGIN0, 0);
564 WRITE4(sc, EMMCP_SEND0, 0);
565 WRITE4(sc, EMMCP_CTRL0, (MPSCTRL_SECURE_READ_BIT |
566 MPSCTRL_SECURE_WRITE_BIT |
567 MPSCTRL_NON_SECURE_READ_BIT |
568 MPSCTRL_NON_SECURE_WRITE_BIT |
571 /* XXX: we support operation for slot index 0 only */
573 WRITE4(sc, SDMMC_PWREN, (1 << slot));
576 if (dwmmc_ctrl_reset(sc, (SDMMC_CTRL_RESET |
577 SDMMC_CTRL_FIFO_RESET |
578 SDMMC_CTRL_DMA_RESET)))
581 dwmmc_setup_bus(sc, sc->host.f_min);
586 /* Install desc base */
587 WRITE4(sc, SDMMC_DBADDR, sc->desc_ring_paddr);
589 /* Enable DMA interrupts */
590 WRITE4(sc, SDMMC_IDSTS, SDMMC_IDINTEN_MASK);
591 WRITE4(sc, SDMMC_IDINTEN, (SDMMC_IDINTEN_NI |
595 /* Clear and disable interrups for a while */
596 WRITE4(sc, SDMMC_RINTSTS, 0xffffffff);
597 WRITE4(sc, SDMMC_INTMASK, 0);
599 /* Maximum timeout */
600 WRITE4(sc, SDMMC_TMOUT, 0xffffffff);
602 /* Enable interrupts */
603 WRITE4(sc, SDMMC_RINTSTS, 0xffffffff);
604 WRITE4(sc, SDMMC_INTMASK, (SDMMC_INTMASK_CMD_DONE |
611 WRITE4(sc, SDMMC_CTRL, SDMMC_CTRL_INT_ENABLE);
613 sc->host.f_min = 400000;
614 sc->host.f_max = 200000000;
615 sc->host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340;
616 sc->host.caps = MMC_CAP_4_BIT_DATA;
618 child = device_add_child(dev, "mmc", 0);
619 return (bus_generic_attach(dev));
623 dwmmc_setup_bus(struct dwmmc_softc *sc, int freq)
629 WRITE4(sc, SDMMC_CLKENA, 0);
630 WRITE4(sc, SDMMC_CMD, (SDMMC_CMD_WAIT_PRVDATA |
631 SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START));
636 device_printf(sc->dev, "Failed update clk\n");
639 } while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
644 WRITE4(sc, SDMMC_CLKENA, 0);
645 WRITE4(sc, SDMMC_CLKSRC, 0);
647 div = (sc->bus_hz != freq) ? DIV_ROUND_UP(sc->bus_hz, 2 * freq) : 0;
649 WRITE4(sc, SDMMC_CLKDIV, div);
650 WRITE4(sc, SDMMC_CMD, (SDMMC_CMD_WAIT_PRVDATA |
651 SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START));
656 device_printf(sc->dev, "Failed to update clk");
659 } while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
661 WRITE4(sc, SDMMC_CLKENA, (SDMMC_CLKENA_CCLK_EN | SDMMC_CLKENA_LP));
662 WRITE4(sc, SDMMC_CMD, SDMMC_CMD_WAIT_PRVDATA |
663 SDMMC_CMD_UPD_CLK_ONLY | SDMMC_CMD_START);
668 device_printf(sc->dev, "Failed to enable clk\n");
671 } while (READ4(sc, SDMMC_CMD) & SDMMC_CMD_START);
677 dwmmc_update_ios(device_t brdev, device_t reqdev)
679 struct dwmmc_softc *sc;
682 sc = device_get_softc(brdev);
685 dprintf("Setting up clk %u bus_width %d\n",
686 ios->clock, ios->bus_width);
688 dwmmc_setup_bus(sc, ios->clock);
690 if (ios->bus_width == bus_width_8)
691 WRITE4(sc, SDMMC_CTYPE, SDMMC_CTYPE_8BIT);
692 else if (ios->bus_width == bus_width_4)
693 WRITE4(sc, SDMMC_CTYPE, SDMMC_CTYPE_4BIT);
695 WRITE4(sc, SDMMC_CTYPE, 0);
697 if ((sc->hwtype & HWTYPE_MASK) == HWTYPE_EXYNOS) {
698 /* XXX: take care about DDR or SDR use here */
699 WRITE4(sc, SDMMC_CLKSEL, sc->sdr_timing);
703 * XXX: take care about DDR bit
705 * reg = READ4(sc, SDMMC_UHS_REG);
706 * reg |= (SDMMC_UHS_REG_DDR);
707 * WRITE4(sc, SDMMC_UHS_REG, reg);
714 dma_done(struct dwmmc_softc *sc, struct mmc_command *cmd)
716 struct mmc_data *data;
720 if (data->flags & MMC_DATA_WRITE)
721 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
722 BUS_DMASYNC_POSTWRITE);
724 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
725 BUS_DMASYNC_POSTREAD);
727 bus_dmamap_unload(sc->buf_tag, sc->buf_map);
733 dma_stop(struct dwmmc_softc *sc)
737 reg = READ4(sc, SDMMC_CTRL);
738 reg &= ~(SDMMC_CTRL_USE_IDMAC);
739 reg |= (SDMMC_CTRL_DMA_RESET);
740 WRITE4(sc, SDMMC_CTRL, reg);
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);
751 dma_prepare(struct dwmmc_softc *sc, struct mmc_command *cmd)
753 struct mmc_data *data;
761 reg = READ4(sc, SDMMC_INTMASK);
762 reg &= ~(SDMMC_INTMASK_TXDR | SDMMC_INTMASK_RXDR);
763 WRITE4(sc, SDMMC_INTMASK, reg);
765 err = bus_dmamap_load(sc->buf_tag, sc->buf_map,
766 data->data, data->len, dwmmc_ring_setup,
769 panic("dmamap_load failed\n");
771 if (data->flags & MMC_DATA_WRITE)
772 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
773 BUS_DMASYNC_PREWRITE);
775 bus_dmamap_sync(sc->buf_tag, sc->buf_map,
776 BUS_DMASYNC_PREREAD);
778 reg = (DEF_MSIZE << SDMMC_FIFOTH_MSIZE_S);
779 reg |= ((sc->fifo_depth / 2) - 1) << SDMMC_FIFOTH_RXWMARK_S;
780 reg |= (sc->fifo_depth / 2) << SDMMC_FIFOTH_TXWMARK_S;
782 WRITE4(sc, SDMMC_FIFOTH, reg);
785 reg = READ4(sc, SDMMC_CTRL);
786 reg |= (SDMMC_CTRL_USE_IDMAC | SDMMC_CTRL_DMA_ENABLE);
787 WRITE4(sc, SDMMC_CTRL, reg);
790 reg = READ4(sc, SDMMC_BMOD);
791 reg |= (SDMMC_BMOD_DE | SDMMC_BMOD_FB);
792 WRITE4(sc, SDMMC_BMOD, reg);
795 WRITE4(sc, SDMMC_PLDMND, 1);
801 dwmmc_start_cmd(struct dwmmc_softc *sc, struct mmc_command *cmd)
803 struct mmc_data *data;
810 /* XXX Upper layers don't always set this */
813 /* Begin setting up command register. */
817 dprintf("cmd->opcode 0x%08x\n", cmd->opcode);
819 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
820 cmd->opcode == MMC_GO_IDLE_STATE ||
821 cmd->opcode == MMC_GO_INACTIVE_STATE)
822 cmdr |= SDMMC_CMD_STOP_ABORT;
823 else if (cmd->opcode != MMC_SEND_STATUS && data)
824 cmdr |= SDMMC_CMD_WAIT_PRVDATA;
826 /* Set up response handling. */
827 if (MMC_RSP(cmd->flags) != MMC_RSP_NONE) {
828 cmdr |= SDMMC_CMD_RESP_EXP;
829 if (cmd->flags & MMC_RSP_136)
830 cmdr |= SDMMC_CMD_RESP_LONG;
833 if (cmd->flags & MMC_RSP_CRC)
834 cmdr |= SDMMC_CMD_RESP_CRC;
837 * XXX: Not all platforms want this.
839 cmdr |= SDMMC_CMD_USE_HOLD_REG;
841 if ((sc->flags & CARD_INIT_DONE) == 0) {
842 sc->flags |= (CARD_INIT_DONE);
843 cmdr |= SDMMC_CMD_SEND_INIT;
847 if ((cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
848 cmd->opcode == MMC_READ_MULTIPLE_BLOCK) &&
850 cmdr |= SDMMC_CMD_SEND_ASTOP;
852 cmdr |= SDMMC_CMD_DATA_EXP;
853 if (data->flags & MMC_DATA_STREAM)
854 cmdr |= SDMMC_CMD_MODE_STREAM;
855 if (data->flags & MMC_DATA_WRITE)
856 cmdr |= SDMMC_CMD_DATA_WRITE;
858 WRITE4(sc, SDMMC_TMOUT, 0xffffffff);
859 WRITE4(sc, SDMMC_BYTCNT, data->len);
860 blksz = (data->len < MMC_SECTOR_SIZE) ? \
861 data->len : MMC_SECTOR_SIZE;
862 WRITE4(sc, SDMMC_BLKSIZ, blksz);
864 dma_prepare(sc, cmd);
868 dprintf("cmdr 0x%08x\n", cmdr);
870 WRITE4(sc, SDMMC_CMDARG, cmd->arg);
872 WRITE4(sc, SDMMC_CMD, cmdr | SDMMC_CMD_START);
876 dwmmc_next_operation(struct dwmmc_softc *sc)
878 struct mmc_request *req;
889 * XXX: Wait until card is still busy.
890 * We do need this to prevent data timeouts,
891 * mostly caused by multi-block write command
892 * followed by single-read.
894 while(READ4(sc, SDMMC_STATUS) & (SDMMC_STATUS_DATA_BUSY))
897 if (sc->flags & PENDING_CMD) {
898 sc->flags &= ~PENDING_CMD;
899 dwmmc_start_cmd(sc, req->cmd);
901 } else if (sc->flags & PENDING_STOP && !sc->use_auto_stop) {
902 sc->flags &= ~PENDING_STOP;
903 dwmmc_start_cmd(sc, req->stop);
913 dwmmc_request(device_t brdev, device_t reqdev, struct mmc_request *req)
915 struct dwmmc_softc *sc;
917 sc = device_get_softc(brdev);
919 dprintf("%s\n", __func__);
923 if (sc->req != NULL) {
929 sc->flags |= PENDING_CMD;
931 sc->flags |= PENDING_STOP;
932 dwmmc_next_operation(sc);
939 dwmmc_get_ro(device_t brdev, device_t reqdev)
942 dprintf("%s\n", __func__);
948 dwmmc_acquire_host(device_t brdev, device_t reqdev)
950 struct dwmmc_softc *sc;
952 sc = device_get_softc(brdev);
956 msleep(sc, &sc->sc_mtx, PZERO, "dwmmcah", hz / 5);
963 dwmmc_release_host(device_t brdev, device_t reqdev)
965 struct dwmmc_softc *sc;
967 sc = device_get_softc(brdev);
977 dwmmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
979 struct dwmmc_softc *sc;
981 sc = device_get_softc(bus);
986 case MMCBR_IVAR_BUS_MODE:
987 *(int *)result = sc->host.ios.bus_mode;
989 case MMCBR_IVAR_BUS_WIDTH:
990 *(int *)result = sc->host.ios.bus_width;
992 case MMCBR_IVAR_CHIP_SELECT:
993 *(int *)result = sc->host.ios.chip_select;
995 case MMCBR_IVAR_CLOCK:
996 *(int *)result = sc->host.ios.clock;
998 case MMCBR_IVAR_F_MIN:
999 *(int *)result = sc->host.f_min;
1001 case MMCBR_IVAR_F_MAX:
1002 *(int *)result = sc->host.f_max;
1004 case MMCBR_IVAR_HOST_OCR:
1005 *(int *)result = sc->host.host_ocr;
1007 case MMCBR_IVAR_MODE:
1008 *(int *)result = sc->host.mode;
1010 case MMCBR_IVAR_OCR:
1011 *(int *)result = sc->host.ocr;
1013 case MMCBR_IVAR_POWER_MODE:
1014 *(int *)result = sc->host.ios.power_mode;
1016 case MMCBR_IVAR_VDD:
1017 *(int *)result = sc->host.ios.vdd;
1019 case MMCBR_IVAR_CAPS:
1020 sc->host.caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
1021 *(int *)result = sc->host.caps;
1023 case MMCBR_IVAR_MAX_DATA:
1024 *(int *)result = DESC_COUNT;
1030 dwmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1032 struct dwmmc_softc *sc;
1034 sc = device_get_softc(bus);
1039 case MMCBR_IVAR_BUS_MODE:
1040 sc->host.ios.bus_mode = value;
1042 case MMCBR_IVAR_BUS_WIDTH:
1043 sc->host.ios.bus_width = value;
1045 case MMCBR_IVAR_CHIP_SELECT:
1046 sc->host.ios.chip_select = value;
1048 case MMCBR_IVAR_CLOCK:
1049 sc->host.ios.clock = value;
1051 case MMCBR_IVAR_MODE:
1052 sc->host.mode = value;
1054 case MMCBR_IVAR_OCR:
1055 sc->host.ocr = value;
1057 case MMCBR_IVAR_POWER_MODE:
1058 sc->host.ios.power_mode = value;
1060 case MMCBR_IVAR_VDD:
1061 sc->host.ios.vdd = value;
1063 /* These are read-only */
1064 case MMCBR_IVAR_CAPS:
1065 case MMCBR_IVAR_HOST_OCR:
1066 case MMCBR_IVAR_F_MIN:
1067 case MMCBR_IVAR_F_MAX:
1068 case MMCBR_IVAR_MAX_DATA:
1074 static device_method_t dwmmc_methods[] = {
1075 DEVMETHOD(device_probe, dwmmc_probe),
1076 DEVMETHOD(device_attach, dwmmc_attach),
1079 DEVMETHOD(bus_read_ivar, dwmmc_read_ivar),
1080 DEVMETHOD(bus_write_ivar, dwmmc_write_ivar),
1083 DEVMETHOD(mmcbr_update_ios, dwmmc_update_ios),
1084 DEVMETHOD(mmcbr_request, dwmmc_request),
1085 DEVMETHOD(mmcbr_get_ro, dwmmc_get_ro),
1086 DEVMETHOD(mmcbr_acquire_host, dwmmc_acquire_host),
1087 DEVMETHOD(mmcbr_release_host, dwmmc_release_host),
1092 static driver_t dwmmc_driver = {
1095 sizeof(struct dwmmc_softc),
1098 static devclass_t dwmmc_devclass;
1100 DRIVER_MODULE(dwmmc, simplebus, dwmmc_driver, dwmmc_devclass, 0, 0);