Path: blob/main/sys/arm/broadcom/bcm2835/bcm2835_sdhci.c
39566 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2012 Oleksandr Tymoshenko <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*27*/2829#include <sys/param.h>30#include <sys/systm.h>31#include <sys/bus.h>32#include <sys/conf.h>33#include <sys/kernel.h>34#include <sys/lock.h>35#include <sys/malloc.h>36#include <sys/module.h>37#include <sys/mutex.h>38#include <sys/rman.h>39#include <sys/sysctl.h>40#include <sys/taskqueue.h>4142#include <machine/bus.h>4344#include <dev/ofw/ofw_bus.h>45#include <dev/ofw/ofw_bus_subr.h>4647#include <dev/mmc/bridge.h>48#include <dev/mmc/mmcreg.h>49#include <dev/mmc/mmc_fdt_helpers.h>5051#include <dev/sdhci/sdhci.h>5253#include "mmcbr_if.h"54#include "sdhci_if.h"5556#include "opt_mmccam.h"5758#include "bcm2835_dma.h"59#include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>60#ifdef NOTYET61#include <arm/broadcom/bcm2835/bcm2835_clkman.h>62#endif63#include <arm/broadcom/bcm2835/bcm2835_vcbus.h>6465#define BCM2835_DEFAULT_SDHCI_FREQ 5066#define BCM2838_DEFAULT_SDHCI_FREQ 1006768#define BCM_SDHCI_BUFFER_SIZE 51269/*70* NUM_DMA_SEGS is the number of DMA segments we want to accommodate on average.71* We add in a number of segments based on how much we may need to spill into72* another segment due to crossing page boundaries. e.g. up to PAGE_SIZE, an73* extra page is needed as we can cross a page boundary exactly once.74*/75#define NUM_DMA_SEGS 176#define NUM_DMA_SPILL_SEGS \77((((NUM_DMA_SEGS * BCM_SDHCI_BUFFER_SIZE) - 1) / PAGE_SIZE) + 1)78#define ALLOCATED_DMA_SEGS (NUM_DMA_SEGS + NUM_DMA_SPILL_SEGS)79#define BCM_DMA_MAXSIZE (NUM_DMA_SEGS * BCM_SDHCI_BUFFER_SIZE)8081#define BCM_SDHCI_SLOT_LEFT(slot) \82((slot)->curcmd->data->len - (slot)->offset)8384#define BCM_SDHCI_SEGSZ_LEFT(slot) \85min(BCM_DMA_MAXSIZE, \86rounddown(BCM_SDHCI_SLOT_LEFT(slot), BCM_SDHCI_BUFFER_SIZE))8788#define DATA_PENDING_MASK (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)89#define DATA_XFER_MASK (DATA_PENDING_MASK | SDHCI_INT_DATA_END)9091#ifdef DEBUG92static int bcm2835_sdhci_debug = 0;9394TUNABLE_INT("hw.bcm2835.sdhci.debug", &bcm2835_sdhci_debug);95SYSCTL_INT(_hw_sdhci, OID_AUTO, bcm2835_sdhci_debug, CTLFLAG_RWTUN,96&bcm2835_sdhci_debug, 0, "bcm2835 SDHCI debug level");9798#define dprintf(fmt, args...) \99do { \100if (bcm2835_sdhci_debug) \101printf("%s: " fmt, __func__, ##args); \102} while (0)103#else104#define dprintf(fmt, args...)105#endif106107static int bcm2835_sdhci_hs = 1;108static int bcm2835_sdhci_pio_mode = 0;109110struct bcm_mmc_conf {111int clock_id;112int clock_src;113int default_freq;114int quirks;115int emmc_dreq;116};117118struct bcm_mmc_conf bcm2835_sdhci_conf = {119.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC,120.clock_src = -1,121.default_freq = BCM2835_DEFAULT_SDHCI_FREQ,122.quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |123SDHCI_QUIRK_BROKEN_TIMEOUT_VAL | SDHCI_QUIRK_DONT_SET_HISPD_BIT |124SDHCI_QUIRK_MISSING_CAPS,125.emmc_dreq = BCM_DMA_DREQ_EMMC,126};127128struct bcm_mmc_conf bcm2838_emmc2_conf = {129.clock_id = BCM2838_MBOX_CLOCK_ID_EMMC2,130.clock_src = -1,131.default_freq = BCM2838_DEFAULT_SDHCI_FREQ,132.quirks = 0,133.emmc_dreq = BCM_DMA_DREQ_NONE,134};135136static struct ofw_compat_data compat_data[] = {137{"broadcom,bcm2835-sdhci", (uintptr_t)&bcm2835_sdhci_conf},138{"brcm,bcm2835-sdhci", (uintptr_t)&bcm2835_sdhci_conf},139{"brcm,bcm2835-mmc", (uintptr_t)&bcm2835_sdhci_conf},140{"brcm,bcm2711-emmc2", (uintptr_t)&bcm2838_emmc2_conf},141{"brcm,bcm2838-emmc2", (uintptr_t)&bcm2838_emmc2_conf},142{NULL, 0}143};144145TUNABLE_INT("hw.bcm2835.sdhci.hs", &bcm2835_sdhci_hs);146TUNABLE_INT("hw.bcm2835.sdhci.pio_mode", &bcm2835_sdhci_pio_mode);147148struct bcm_sdhci_softc {149device_t sc_dev;150struct resource * sc_mem_res;151struct resource * sc_irq_res;152bus_space_tag_t sc_bst;153bus_space_handle_t sc_bsh;154void * sc_intrhand;155struct mmc_request * sc_req;156struct sdhci_slot sc_slot;157struct mmc_helper sc_mmc_helper;158int sc_dma_ch;159bus_dma_tag_t sc_dma_tag;160bus_dmamap_t sc_dma_map;161vm_paddr_t sc_sdhci_buffer_phys;162bus_addr_t dmamap_seg_addrs[ALLOCATED_DMA_SEGS];163bus_size_t dmamap_seg_sizes[ALLOCATED_DMA_SEGS];164int dmamap_seg_count;165int dmamap_seg_index;166int dmamap_status;167uint32_t blksz_and_count;168uint32_t cmd_and_mode;169bool need_update_blk;170#ifdef NOTYET171device_t clkman;172#endif173struct bcm_mmc_conf * conf;174};175176static int bcm_sdhci_probe(device_t);177static int bcm_sdhci_attach(device_t);178static int bcm_sdhci_detach(device_t);179static void bcm_sdhci_intr(void *);180181static int bcm_sdhci_get_ro(device_t, device_t);182static void bcm_sdhci_dma_intr(int ch, void *arg);183static void bcm_sdhci_start_dma(struct sdhci_slot *slot);184185static void186bcm_sdhci_dmacb(void *arg, bus_dma_segment_t *segs, int nseg, int err)187{188struct bcm_sdhci_softc *sc = arg;189int i;190191/* Sanity check: we can only ever have one mapping at a time. */192KASSERT(sc->dmamap_seg_count == 0, ("leaked DMA segment"));193sc->dmamap_status = err;194sc->dmamap_seg_count = nseg;195196/* Note nseg is guaranteed to be zero if err is non-zero. */197for (i = 0; i < nseg; i++) {198sc->dmamap_seg_addrs[i] = segs[i].ds_addr;199sc->dmamap_seg_sizes[i] = segs[i].ds_len;200}201}202203static int204bcm_sdhci_probe(device_t dev)205{206207if (!ofw_bus_status_okay(dev))208return (ENXIO);209210if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)211return (ENXIO);212213device_set_desc(dev, "Broadcom 2708 SDHCI controller");214215return (BUS_PROBE_DEFAULT);216}217218static int219bcm_sdhci_attach(device_t dev)220{221struct bcm_sdhci_softc *sc = device_get_softc(dev);222int rid, err;223phandle_t node;224pcell_t cell;225u_int default_freq;226227sc->sc_dev = dev;228sc->sc_req = NULL;229230sc->conf = (struct bcm_mmc_conf *)ofw_bus_search_compatible(dev,231compat_data)->ocd_data;232if (sc->conf == 0)233return (ENXIO);234235err = bcm2835_mbox_set_power_state(BCM2835_MBOX_POWER_ID_EMMC, TRUE);236if (err != 0) {237if (bootverbose)238device_printf(dev, "Unable to enable the power\n");239return (err);240}241242default_freq = 0;243err = bcm2835_mbox_get_clock_rate(sc->conf->clock_id, &default_freq);244if (err == 0) {245/* Convert to MHz */246default_freq /= 1000000;247}248if (default_freq == 0) {249node = ofw_bus_get_node(sc->sc_dev);250if ((OF_getencprop(node, "clock-frequency", &cell,251sizeof(cell))) > 0)252default_freq = cell / 1000000;253}254if (default_freq == 0)255default_freq = sc->conf->default_freq;256257if (bootverbose)258device_printf(dev, "SDHCI frequency: %dMHz\n", default_freq);259#ifdef NOTYET260if (sc->conf->clock_src > 0) {261uint32_t f;262sc->clkman = devclass_get_device(263devclass_find("bcm2835_clkman"), 0);264if (sc->clkman == NULL) {265device_printf(dev, "cannot find Clock Manager\n");266return (ENXIO);267}268269f = bcm2835_clkman_set_frequency(sc->clkman,270sc->conf->clock_src, default_freq);271if (f == 0)272return (EINVAL);273274if (bootverbose)275device_printf(dev, "Clock source frequency: %dMHz\n",276f);277}278#endif279280rid = 0;281sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,282RF_ACTIVE);283if (!sc->sc_mem_res) {284device_printf(dev, "cannot allocate memory window\n");285err = ENXIO;286goto fail;287}288289sc->sc_bst = rman_get_bustag(sc->sc_mem_res);290sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);291292rid = 0;293sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,294RF_ACTIVE | RF_SHAREABLE);295if (!sc->sc_irq_res) {296device_printf(dev, "cannot allocate interrupt\n");297err = ENXIO;298goto fail;299}300301if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,302NULL, bcm_sdhci_intr, sc, &sc->sc_intrhand)) {303device_printf(dev, "cannot setup interrupt handler\n");304err = ENXIO;305goto fail;306}307308if (!bcm2835_sdhci_pio_mode)309sc->sc_slot.opt = SDHCI_PLATFORM_TRANSFER;310311sc->sc_slot.caps = SDHCI_CAN_VDD_330 | SDHCI_CAN_VDD_180;312if (bcm2835_sdhci_hs)313sc->sc_slot.caps |= SDHCI_CAN_DO_HISPD;314sc->sc_slot.caps |= (default_freq << SDHCI_CLOCK_BASE_SHIFT);315sc->sc_slot.quirks = sc->conf->quirks;316317sdhci_init_slot(dev, &sc->sc_slot, 0);318mmc_fdt_parse(dev, 0, &sc->sc_mmc_helper, &sc->sc_slot.host);319320sc->sc_dma_ch = bcm_dma_allocate(BCM_DMA_CH_ANY);321if (sc->sc_dma_ch == BCM_DMA_CH_INVALID)322goto fail;323324err = bcm_dma_setup_intr(sc->sc_dma_ch, bcm_sdhci_dma_intr, sc);325if (err != 0) {326device_printf(dev,327"cannot setup dma interrupt handler\n");328err = ENXIO;329goto fail;330}331332/* Allocate bus_dma resources. */333err = bus_dma_tag_create(bus_get_dma_tag(dev),3341, 0, bcm283x_dmabus_peripheral_lowaddr(),335BUS_SPACE_MAXADDR, NULL, NULL,336BCM_DMA_MAXSIZE, ALLOCATED_DMA_SEGS, BCM_SDHCI_BUFFER_SIZE,337BUS_DMA_ALLOCNOW, NULL, NULL,338&sc->sc_dma_tag);339340if (err) {341device_printf(dev, "failed allocate DMA tag");342goto fail;343}344345err = bus_dmamap_create(sc->sc_dma_tag, 0, &sc->sc_dma_map);346if (err) {347device_printf(dev, "bus_dmamap_create failed\n");348goto fail;349}350351/* FIXME: Fix along with other BUS_SPACE_PHYSADDR instances */352sc->sc_sdhci_buffer_phys = rman_get_start(sc->sc_mem_res) +353SDHCI_BUFFER;354355bus_identify_children(dev);356bus_attach_children(dev);357358sdhci_start_slot(&sc->sc_slot);359360/* Seed our copies. */361sc->blksz_and_count = SDHCI_READ_4(dev, &sc->sc_slot, SDHCI_BLOCK_SIZE);362sc->cmd_and_mode = SDHCI_READ_4(dev, &sc->sc_slot, SDHCI_TRANSFER_MODE);363364return (0);365366fail:367if (sc->sc_intrhand)368bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);369if (sc->sc_irq_res)370bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);371if (sc->sc_mem_res)372bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);373374return (err);375}376377static int378bcm_sdhci_detach(device_t dev)379{380381return (EBUSY);382}383384static void385bcm_sdhci_intr(void *arg)386{387struct bcm_sdhci_softc *sc = arg;388389sdhci_generic_intr(&sc->sc_slot);390}391392static int393bcm_sdhci_update_ios(device_t bus, device_t child)394{395struct bcm_sdhci_softc *sc;396struct mmc_ios *ios;397int rv;398399sc = device_get_softc(bus);400ios = &sc->sc_slot.host.ios;401402if (ios->power_mode == power_up) {403if (sc->sc_mmc_helper.vmmc_supply)404regulator_enable(sc->sc_mmc_helper.vmmc_supply);405if (sc->sc_mmc_helper.vqmmc_supply)406regulator_enable(sc->sc_mmc_helper.vqmmc_supply);407}408409rv = sdhci_generic_update_ios(bus, child);410if (rv != 0)411return (rv);412413if (ios->power_mode == power_off) {414if (sc->sc_mmc_helper.vmmc_supply)415regulator_disable(sc->sc_mmc_helper.vmmc_supply);416if (sc->sc_mmc_helper.vqmmc_supply)417regulator_disable(sc->sc_mmc_helper.vqmmc_supply);418}419420return (0);421}422423static int424bcm_sdhci_get_ro(device_t bus, device_t child)425{426427return (0);428}429430static inline uint32_t431RD4(struct bcm_sdhci_softc *sc, bus_size_t off)432{433uint32_t val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);434return val;435}436437static inline void438WR4(struct bcm_sdhci_softc *sc, bus_size_t off, uint32_t val)439{440441bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, val);442/*443* The Arasan HC has a bug where it may lose the content of444* consecutive writes to registers that are within two SD-card445* clock cycles of each other (a clock domain crossing problem).446*/447if (sc->sc_slot.clock > 0)448DELAY(((2 * 1000000) / sc->sc_slot.clock) + 1);449}450451static uint8_t452bcm_sdhci_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)453{454struct bcm_sdhci_softc *sc = device_get_softc(dev);455uint32_t val = RD4(sc, off & ~3);456457return ((val >> (off & 3)*8) & 0xff);458}459460static uint16_t461bcm_sdhci_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)462{463struct bcm_sdhci_softc *sc = device_get_softc(dev);464uint32_t val32;465466/*467* Standard 32-bit handling of command and transfer mode, as468* well as block size and count.469*/470if ((off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) &&471sc->need_update_blk)472val32 = sc->blksz_and_count;473else if (off == SDHCI_TRANSFER_MODE || off == SDHCI_COMMAND_FLAGS)474val32 = sc->cmd_and_mode;475else476val32 = RD4(sc, off & ~3);477478return ((val32 >> (off & 3)*8) & 0xffff);479}480481static uint32_t482bcm_sdhci_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)483{484struct bcm_sdhci_softc *sc = device_get_softc(dev);485486return RD4(sc, off);487}488489static void490bcm_sdhci_read_multi_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,491uint32_t *data, bus_size_t count)492{493struct bcm_sdhci_softc *sc = device_get_softc(dev);494495bus_space_read_multi_4(sc->sc_bst, sc->sc_bsh, off, data, count);496}497498static void499bcm_sdhci_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off,500uint8_t val)501{502struct bcm_sdhci_softc *sc = device_get_softc(dev);503uint32_t val32 = RD4(sc, off & ~3);504val32 &= ~(0xff << (off & 3)*8);505val32 |= (val << (off & 3)*8);506WR4(sc, off & ~3, val32);507}508509static void510bcm_sdhci_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off,511uint16_t val)512{513struct bcm_sdhci_softc *sc = device_get_softc(dev);514uint32_t val32;515516/*517* If we have a queued up 16bit value for blk size or count, use and518* update the saved value rather than doing any real register access.519* If we did not touch either since the last write, then read from520* register as at least block count can change.521* Similarly, if we are about to issue a command, always use the saved522* value for transfer mode as we can never write that without issuing523* a command.524*/525if ((off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) &&526sc->need_update_blk)527val32 = sc->blksz_and_count;528else if (off == SDHCI_COMMAND_FLAGS)529val32 = sc->cmd_and_mode;530else531val32 = RD4(sc, off & ~3);532533val32 &= ~(0xffff << (off & 3)*8);534val32 |= (val << (off & 3)*8);535536if (off == SDHCI_TRANSFER_MODE)537sc->cmd_and_mode = val32;538else if (off == SDHCI_BLOCK_SIZE || off == SDHCI_BLOCK_COUNT) {539sc->blksz_and_count = val32;540sc->need_update_blk = true;541} else {542if (off == SDHCI_COMMAND_FLAGS) {543/* If we saved blk writes, do them now before cmd. */544if (sc->need_update_blk) {545WR4(sc, SDHCI_BLOCK_SIZE, sc->blksz_and_count);546sc->need_update_blk = false;547}548/* Always save cmd and mode registers. */549sc->cmd_and_mode = val32;550}551WR4(sc, off & ~3, val32);552}553}554555static void556bcm_sdhci_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,557uint32_t val)558{559struct bcm_sdhci_softc *sc = device_get_softc(dev);560WR4(sc, off, val);561}562563static void564bcm_sdhci_write_multi_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,565uint32_t *data, bus_size_t count)566{567struct bcm_sdhci_softc *sc = device_get_softc(dev);568569bus_space_write_multi_4(sc->sc_bst, sc->sc_bsh, off, data, count);570}571572static void573bcm_sdhci_start_dma_seg(struct bcm_sdhci_softc *sc)574{575struct sdhci_slot *slot;576vm_paddr_t pdst, psrc;577int err __diagused, idx, len, sync_op, width;578579slot = &sc->sc_slot;580mtx_assert(&slot->mtx, MA_OWNED);581idx = sc->dmamap_seg_index++;582len = sc->dmamap_seg_sizes[idx];583slot->offset += len;584width = (len & 0xf ? BCM_DMA_32BIT : BCM_DMA_128BIT);585586if (slot->curcmd->data->flags & MMC_DATA_READ) {587/*588* Peripherals on the AXI bus do not need DREQ pacing for reads589* from the ARM core, so we can safely set this to NONE.590*/591bcm_dma_setup_src(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,592BCM_DMA_SAME_ADDR, BCM_DMA_32BIT);593bcm_dma_setup_dst(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,594BCM_DMA_INC_ADDR, width);595psrc = sc->sc_sdhci_buffer_phys;596pdst = sc->dmamap_seg_addrs[idx];597sync_op = BUS_DMASYNC_PREREAD;598} else {599/*600* The ordering here is important, because the last write to601* dst/src in the dma control block writes the real dreq value.602*/603bcm_dma_setup_src(sc->sc_dma_ch, BCM_DMA_DREQ_NONE,604BCM_DMA_INC_ADDR, width);605bcm_dma_setup_dst(sc->sc_dma_ch, sc->conf->emmc_dreq,606BCM_DMA_SAME_ADDR, BCM_DMA_32BIT);607psrc = sc->dmamap_seg_addrs[idx];608pdst = sc->sc_sdhci_buffer_phys;609sync_op = BUS_DMASYNC_PREWRITE;610}611612/*613* When starting a new DMA operation do the busdma sync operation, and614* disable SDCHI data interrrupts because we'll be driven by DMA615* interrupts (or SDHCI error interrupts) until the IO is done.616*/617if (idx == 0) {618bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map, sync_op);619620slot->intmask &= ~DATA_XFER_MASK;621bcm_sdhci_write_4(sc->sc_dev, slot, SDHCI_SIGNAL_ENABLE,622slot->intmask);623}624625/*626* Start the DMA transfer. Only programming errors (like failing to627* allocate a channel) cause a non-zero return from bcm_dma_start().628*/629err = bcm_dma_start(sc->sc_dma_ch, psrc, pdst, len);630KASSERT((err == 0), ("bcm2835_sdhci: failed DMA start"));631}632633static void634bcm_sdhci_dma_exit(struct bcm_sdhci_softc *sc)635{636struct sdhci_slot *slot = &sc->sc_slot;637638mtx_assert(&slot->mtx, MA_OWNED);639640/* Re-enable interrupts */641slot->intmask |= DATA_XFER_MASK;642bcm_sdhci_write_4(slot->bus, slot, SDHCI_SIGNAL_ENABLE,643slot->intmask);644}645646static void647bcm_sdhci_dma_unload(struct bcm_sdhci_softc *sc)648{649struct sdhci_slot *slot = &sc->sc_slot;650651if (sc->dmamap_seg_count == 0)652return;653if ((slot->curcmd->data->flags & MMC_DATA_READ) != 0)654bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map,655BUS_DMASYNC_POSTREAD);656else657bus_dmamap_sync(sc->sc_dma_tag, sc->sc_dma_map,658BUS_DMASYNC_POSTWRITE);659bus_dmamap_unload(sc->sc_dma_tag, sc->sc_dma_map);660661sc->dmamap_seg_count = 0;662sc->dmamap_seg_index = 0;663}664665static void666bcm_sdhci_dma_intr(int ch, void *arg)667{668struct bcm_sdhci_softc *sc = (struct bcm_sdhci_softc *)arg;669struct sdhci_slot *slot = &sc->sc_slot;670uint32_t reg;671672mtx_lock(&slot->mtx);673if (slot->curcmd == NULL)674goto out;675/*676* If there are more segments for the current dma, start the next one.677* Otherwise unload the dma map and decide what to do next based on the678* status of the sdhci controller and whether there's more data left.679*/680if (sc->dmamap_seg_index < sc->dmamap_seg_count) {681bcm_sdhci_start_dma_seg(sc);682goto out;683}684685bcm_sdhci_dma_unload(sc);686687/*688* If we had no further segments pending, we need to determine how to689* proceed next. If the 'data/space pending' bit is already set and we690* can continue via DMA, do so. Otherwise, re-enable interrupts and691* return.692*/693reg = bcm_sdhci_read_4(slot->bus, slot, SDHCI_INT_STATUS) &694DATA_XFER_MASK;695if ((reg & DATA_PENDING_MASK) != 0 &&696BCM_SDHCI_SEGSZ_LEFT(slot) >= BCM_SDHCI_BUFFER_SIZE) {697/* ACK any pending interrupts */698bcm_sdhci_write_4(slot->bus, slot, SDHCI_INT_STATUS,699DATA_PENDING_MASK);700701bcm_sdhci_start_dma(slot);702if (slot->curcmd->error != 0) {703/* We won't recover from this error for this command. */704bcm_sdhci_dma_unload(sc);705bcm_sdhci_dma_exit(sc);706sdhci_finish_data(slot);707}708} else if ((reg & SDHCI_INT_DATA_END) != 0) {709bcm_sdhci_dma_exit(sc);710bcm_sdhci_write_4(slot->bus, slot, SDHCI_INT_STATUS,711reg);712slot->flags &= ~PLATFORM_DATA_STARTED;713sdhci_finish_data(slot);714} else {715bcm_sdhci_dma_exit(sc);716}717out:718mtx_unlock(&slot->mtx);719}720721static void722bcm_sdhci_start_dma(struct sdhci_slot *slot)723{724struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);725uint8_t *buf;726size_t left;727728mtx_assert(&slot->mtx, MA_OWNED);729730left = BCM_SDHCI_SEGSZ_LEFT(slot);731buf = (uint8_t *)slot->curcmd->data->data + slot->offset;732KASSERT(left != 0,733("%s: DMA handling incorrectly indicated", __func__));734735/*736* No need to check segment count here; if we've not yet unloaded737* previous segments, we'll catch that in bcm_sdhci_dmacb.738*/739if (bus_dmamap_load(sc->sc_dma_tag, sc->sc_dma_map, buf, left,740bcm_sdhci_dmacb, sc, BUS_DMA_NOWAIT) != 0 ||741sc->dmamap_status != 0) {742slot->curcmd->error = MMC_ERR_NO_MEMORY;743return;744}745746/* DMA start */747bcm_sdhci_start_dma_seg(sc);748}749750static int751bcm_sdhci_will_handle_transfer(device_t dev, struct sdhci_slot *slot)752{753#ifdef INVARIANTS754struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);755#endif756757/*758* We don't want to perform DMA in this context -- interrupts are759* disabled, and a transaction may already be in progress.760*/761if (dumping)762return (0);763764/*765* This indicates that we somehow let a data interrupt slip by into the766* SDHCI framework, when it should not have. This really needs to be767* caught and fixed ASAP, as it really shouldn't happen.768*/769KASSERT(sc->dmamap_seg_count == 0,770("data pending interrupt pushed through SDHCI framework"));771772/*773* Do not use DMA for transfers less than our block size. Checking774* alignment serves little benefit, as we round transfer sizes down to775* a multiple of the block size and push the transfer back to776* SDHCI-driven PIO once we're below the block size.777*/778if (BCM_SDHCI_SEGSZ_LEFT(slot) < BCM_DMA_BLOCK_SIZE)779return (0);780781return (1);782}783784static void785bcm_sdhci_start_transfer(device_t dev, struct sdhci_slot *slot,786uint32_t *intmask)787{788789/* DMA transfer FIFO 1KB */790bcm_sdhci_start_dma(slot);791}792793static void794bcm_sdhci_finish_transfer(device_t dev, struct sdhci_slot *slot)795{796struct bcm_sdhci_softc *sc = device_get_softc(slot->bus);797798/*799* Clean up. Interrupts are clearly enabled, because we received an800* SDHCI_INT_DATA_END to get this far -- just make sure we don't leave801* anything laying around.802*/803if (sc->dmamap_seg_count != 0) {804/*805* Our segment math should have worked out such that we would806* never finish the transfer without having used up all of the807* segments. If we haven't, that means we must have erroneously808* regressed to SDHCI-driven PIO to finish the operation and809* this is certainly caused by developer-error.810*/811bcm_sdhci_dma_unload(sc);812}813814sdhci_finish_data(slot);815}816817static device_method_t bcm_sdhci_methods[] = {818/* Device interface */819DEVMETHOD(device_probe, bcm_sdhci_probe),820DEVMETHOD(device_attach, bcm_sdhci_attach),821DEVMETHOD(device_detach, bcm_sdhci_detach),822823/* Bus interface */824DEVMETHOD(bus_read_ivar, sdhci_generic_read_ivar),825DEVMETHOD(bus_write_ivar, sdhci_generic_write_ivar),826DEVMETHOD(bus_add_child, bus_generic_add_child),827828/* MMC bridge interface */829DEVMETHOD(mmcbr_update_ios, bcm_sdhci_update_ios),830DEVMETHOD(mmcbr_request, sdhci_generic_request),831DEVMETHOD(mmcbr_get_ro, bcm_sdhci_get_ro),832DEVMETHOD(mmcbr_acquire_host, sdhci_generic_acquire_host),833DEVMETHOD(mmcbr_release_host, sdhci_generic_release_host),834835/* Platform transfer methods */836DEVMETHOD(sdhci_platform_will_handle, bcm_sdhci_will_handle_transfer),837DEVMETHOD(sdhci_platform_start_transfer, bcm_sdhci_start_transfer),838DEVMETHOD(sdhci_platform_finish_transfer, bcm_sdhci_finish_transfer),839/* SDHCI registers accessors */840DEVMETHOD(sdhci_read_1, bcm_sdhci_read_1),841DEVMETHOD(sdhci_read_2, bcm_sdhci_read_2),842DEVMETHOD(sdhci_read_4, bcm_sdhci_read_4),843DEVMETHOD(sdhci_read_multi_4, bcm_sdhci_read_multi_4),844DEVMETHOD(sdhci_write_1, bcm_sdhci_write_1),845DEVMETHOD(sdhci_write_2, bcm_sdhci_write_2),846DEVMETHOD(sdhci_write_4, bcm_sdhci_write_4),847DEVMETHOD(sdhci_write_multi_4, bcm_sdhci_write_multi_4),848849DEVMETHOD_END850};851852static driver_t bcm_sdhci_driver = {853"sdhci_bcm",854bcm_sdhci_methods,855sizeof(struct bcm_sdhci_softc),856};857858DRIVER_MODULE(sdhci_bcm, simplebus, bcm_sdhci_driver, NULL, NULL);859#ifdef NOTYET860MODULE_DEPEND(sdhci_bcm, bcm2835_clkman, 1, 1, 1);861#endif862SDHCI_DEPEND(sdhci_bcm);863#ifndef MMCCAM864MMC_DECLARE_BRIDGE(sdhci_bcm);865#endif866867868