1b5be541fSEmmanuel Vadot /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3b091392eSEmmanuel Vadot * 4b091392eSEmmanuel Vadot * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org> 5b5be541fSEmmanuel Vadot * Copyright (c) 2013 Alexander Fedorov 6b5be541fSEmmanuel Vadot * All rights reserved. 7b5be541fSEmmanuel Vadot * 8b5be541fSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 9b5be541fSEmmanuel Vadot * modification, are permitted provided that the following conditions 10b5be541fSEmmanuel Vadot * are met: 11b5be541fSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 12b5be541fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 13b5be541fSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 14b5be541fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 15b5be541fSEmmanuel Vadot * documentation and/or other materials provided with the distribution. 16b5be541fSEmmanuel Vadot * 17b5be541fSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18b5be541fSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19b5be541fSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20b5be541fSEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21b5be541fSEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22b5be541fSEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23b5be541fSEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24b5be541fSEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25b5be541fSEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26b5be541fSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27b5be541fSEmmanuel Vadot * SUCH DAMAGE. 28b5be541fSEmmanuel Vadot */ 29b5be541fSEmmanuel Vadot 30b5be541fSEmmanuel Vadot #include <sys/param.h> 31b5be541fSEmmanuel Vadot #include <sys/systm.h> 32b5be541fSEmmanuel Vadot #include <sys/bus.h> 33af8b51b0SAndriy Gapon #include <sys/conf.h> 34b5be541fSEmmanuel Vadot #include <sys/kernel.h> 35b5be541fSEmmanuel Vadot #include <sys/lock.h> 36b5be541fSEmmanuel Vadot #include <sys/malloc.h> 37b5be541fSEmmanuel Vadot #include <sys/module.h> 38b5be541fSEmmanuel Vadot #include <sys/mutex.h> 39b5be541fSEmmanuel Vadot #include <sys/resource.h> 40b5be541fSEmmanuel Vadot #include <sys/rman.h> 41b5be541fSEmmanuel Vadot #include <sys/sysctl.h> 429ed83210SEmmanuel Vadot #include <sys/queue.h> 439ed83210SEmmanuel Vadot #include <sys/taskqueue.h> 44b5be541fSEmmanuel Vadot 45b5be541fSEmmanuel Vadot #include <machine/bus.h> 46b5be541fSEmmanuel Vadot 47b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 48b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 49b5be541fSEmmanuel Vadot 50b5be541fSEmmanuel Vadot #include <dev/mmc/bridge.h> 51b5be541fSEmmanuel Vadot #include <dev/mmc/mmcbrvar.h> 529ed83210SEmmanuel Vadot #include <dev/mmc/mmc_fdt_helpers.h> 53b5be541fSEmmanuel Vadot 54b5be541fSEmmanuel Vadot #include <arm/allwinner/aw_mmc.h> 55be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h> 561f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h> 57b2f0caf1SEmmanuel Vadot #include <dev/regulator/regulator.h> 58b5be541fSEmmanuel Vadot 595e03278fSIlya Bakulin #include "opt_mmccam.h" 605e03278fSIlya Bakulin 615e03278fSIlya Bakulin #ifdef MMCCAM 625e03278fSIlya Bakulin #include <cam/cam.h> 635e03278fSIlya Bakulin #include <cam/cam_ccb.h> 645e03278fSIlya Bakulin #include <cam/cam_debug.h> 655e03278fSIlya Bakulin #include <cam/cam_sim.h> 665e03278fSIlya Bakulin #include <cam/cam_xpt_sim.h> 672671bdb5SEmmanuel Vadot #include <cam/mmc/mmc_sim.h> 682671bdb5SEmmanuel Vadot 692671bdb5SEmmanuel Vadot #include "mmc_sim_if.h" 705e03278fSIlya Bakulin #endif 715e03278fSIlya Bakulin 726bc8fe8aSEmmanuel Vadot #include "mmc_pwrseq_if.h" 736bc8fe8aSEmmanuel Vadot 74b5be541fSEmmanuel Vadot #define AW_MMC_MEMRES 0 75b5be541fSEmmanuel Vadot #define AW_MMC_IRQRES 1 76b5be541fSEmmanuel Vadot #define AW_MMC_RESSZ 2 77c39ea909SEmmanuel Vadot #define AW_MMC_DMA_SEGS (PAGE_SIZE / sizeof(struct aw_mmc_dma_desc)) 78c39ea909SEmmanuel Vadot #define AW_MMC_DMA_DESC_SIZE (sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS) 79b5be541fSEmmanuel Vadot #define AW_MMC_DMA_FTRGLEVEL 0x20070008 80c39ea909SEmmanuel Vadot 81b5be541fSEmmanuel Vadot #define AW_MMC_RESET_RETRY 1000 82b5be541fSEmmanuel Vadot 83b5be541fSEmmanuel Vadot #define CARD_ID_FREQUENCY 400000 84b5be541fSEmmanuel Vadot 85ce0618beSEmmanuel Vadot struct aw_mmc_conf { 86ce0618beSEmmanuel Vadot uint32_t dma_xferlen; 87ce0618beSEmmanuel Vadot bool mask_data0; 88ce0618beSEmmanuel Vadot bool can_calibrate; 89ce0618beSEmmanuel Vadot bool new_timing; 90ce0618beSEmmanuel Vadot }; 91ce0618beSEmmanuel Vadot 92ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a10_mmc_conf = { 93ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 94ce0618beSEmmanuel Vadot }; 95ce0618beSEmmanuel Vadot 96ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a13_mmc_conf = { 97ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 98ce0618beSEmmanuel Vadot }; 99ce0618beSEmmanuel Vadot 100ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_mmc_conf = { 101ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 102ce0618beSEmmanuel Vadot .mask_data0 = true, 103ce0618beSEmmanuel Vadot .can_calibrate = true, 104ce0618beSEmmanuel Vadot .new_timing = true, 105ce0618beSEmmanuel Vadot }; 106ce0618beSEmmanuel Vadot 107ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_emmc_conf = { 108ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 109ce0618beSEmmanuel Vadot .can_calibrate = true, 110ce0618beSEmmanuel Vadot }; 111ce0618beSEmmanuel Vadot 112b5be541fSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 113ce0618beSEmmanuel Vadot {"allwinner,sun4i-a10-mmc", (uintptr_t)&a10_mmc_conf}, 114ce0618beSEmmanuel Vadot {"allwinner,sun5i-a13-mmc", (uintptr_t)&a13_mmc_conf}, 115ce0618beSEmmanuel Vadot {"allwinner,sun7i-a20-mmc", (uintptr_t)&a13_mmc_conf}, 116ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-mmc", (uintptr_t)&a64_mmc_conf}, 117ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-emmc", (uintptr_t)&a64_emmc_conf}, 118b5be541fSEmmanuel Vadot {NULL, 0} 119b5be541fSEmmanuel Vadot }; 120b5be541fSEmmanuel Vadot 121b5be541fSEmmanuel Vadot struct aw_mmc_softc { 122b5be541fSEmmanuel Vadot device_t aw_dev; 123b5be541fSEmmanuel Vadot clk_t aw_clk_ahb; 124b5be541fSEmmanuel Vadot clk_t aw_clk_mmc; 125b5be541fSEmmanuel Vadot hwreset_t aw_rst_ahb; 126b5be541fSEmmanuel Vadot int aw_bus_busy; 127b5be541fSEmmanuel Vadot int aw_resid; 128b5be541fSEmmanuel Vadot int aw_timeout; 129b5be541fSEmmanuel Vadot struct callout aw_timeoutc; 130b5be541fSEmmanuel Vadot struct mmc_host aw_host; 1318a8166e5SBartlomiej Grzesik struct mmc_helper mmc_helper; 1325e03278fSIlya Bakulin #ifdef MMCCAM 1335e03278fSIlya Bakulin union ccb * ccb; 1342671bdb5SEmmanuel Vadot struct mmc_sim mmc_sim; 1355e03278fSIlya Bakulin #else 136b5be541fSEmmanuel Vadot struct mmc_request * aw_req; 1375e03278fSIlya Bakulin #endif 138b5be541fSEmmanuel Vadot struct mtx aw_mtx; 139b5be541fSEmmanuel Vadot struct resource * aw_res[AW_MMC_RESSZ]; 140ce0618beSEmmanuel Vadot struct aw_mmc_conf * aw_mmc_conf; 141b5be541fSEmmanuel Vadot uint32_t aw_intr; 142b5be541fSEmmanuel Vadot uint32_t aw_intr_wait; 143b5be541fSEmmanuel Vadot void * aw_intrhand; 1440f7a6420SEmmanuel Vadot unsigned int aw_clock; 1459ed83210SEmmanuel Vadot device_t child; 146b5be541fSEmmanuel Vadot 147b5be541fSEmmanuel Vadot /* Fields required for DMA access. */ 148b5be541fSEmmanuel Vadot bus_addr_t aw_dma_desc_phys; 149b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_map; 150b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_tag; 151b5be541fSEmmanuel Vadot void * aw_dma_desc; 152b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_buf_map; 153b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_buf_tag; 154b5be541fSEmmanuel Vadot int aw_dma_map_err; 155b5be541fSEmmanuel Vadot }; 156b5be541fSEmmanuel Vadot 157b5be541fSEmmanuel Vadot static struct resource_spec aw_mmc_res_spec[] = { 158b5be541fSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE }, 159b5be541fSEmmanuel Vadot { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 160b5be541fSEmmanuel Vadot { -1, 0, 0 } 161b5be541fSEmmanuel Vadot }; 162b5be541fSEmmanuel Vadot 163b5be541fSEmmanuel Vadot static int aw_mmc_probe(device_t); 164b5be541fSEmmanuel Vadot static int aw_mmc_attach(device_t); 165b5be541fSEmmanuel Vadot static int aw_mmc_detach(device_t); 166b5be541fSEmmanuel Vadot static int aw_mmc_setup_dma(struct aw_mmc_softc *); 167cbba9a7bSEmmanuel Vadot static void aw_mmc_teardown_dma(struct aw_mmc_softc *sc); 168b5be541fSEmmanuel Vadot static int aw_mmc_reset(struct aw_mmc_softc *); 16935a18619SEmmanuel Vadot static int aw_mmc_init(struct aw_mmc_softc *); 170b5be541fSEmmanuel Vadot static void aw_mmc_intr(void *); 171b5be541fSEmmanuel Vadot static int aw_mmc_update_clock(struct aw_mmc_softc *, uint32_t); 1729ed83210SEmmanuel Vadot static void aw_mmc_helper_cd_handler(device_t, bool); 173b5be541fSEmmanuel Vadot 1745e03278fSIlya Bakulin static void aw_mmc_print_error(uint32_t); 175b5be541fSEmmanuel Vadot static int aw_mmc_update_ios(device_t, device_t); 176b5be541fSEmmanuel Vadot static int aw_mmc_request(device_t, device_t, struct mmc_request *); 1772671bdb5SEmmanuel Vadot 1782671bdb5SEmmanuel Vadot #ifndef MMCCAM 179b5be541fSEmmanuel Vadot static int aw_mmc_get_ro(device_t, device_t); 180b5be541fSEmmanuel Vadot static int aw_mmc_acquire_host(device_t, device_t); 181b5be541fSEmmanuel Vadot static int aw_mmc_release_host(device_t, device_t); 1825e03278fSIlya Bakulin #endif 183b5be541fSEmmanuel Vadot 184b5be541fSEmmanuel Vadot #define AW_MMC_LOCK(_sc) mtx_lock(&(_sc)->aw_mtx) 185b5be541fSEmmanuel Vadot #define AW_MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->aw_mtx) 186b5be541fSEmmanuel Vadot #define AW_MMC_READ_4(_sc, _reg) \ 187b5be541fSEmmanuel Vadot bus_read_4((_sc)->aw_res[AW_MMC_MEMRES], _reg) 188b5be541fSEmmanuel Vadot #define AW_MMC_WRITE_4(_sc, _reg, _value) \ 189b5be541fSEmmanuel Vadot bus_write_4((_sc)->aw_res[AW_MMC_MEMRES], _reg, _value) 190b5be541fSEmmanuel Vadot 191020df509SEmmanuel Vadot SYSCTL_NODE(_hw, OID_AUTO, aw_mmc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 192020df509SEmmanuel Vadot "aw_mmc driver"); 193020df509SEmmanuel Vadot 194020df509SEmmanuel Vadot static int aw_mmc_debug = 0; 195020df509SEmmanuel Vadot SYSCTL_INT(_hw_aw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &aw_mmc_debug, 0, 196020df509SEmmanuel Vadot "Debug level bit0=card changes bit1=ios changes, bit2=interrupts, bit3=commands"); 197020df509SEmmanuel Vadot #define AW_MMC_DEBUG_CARD 0x1 198020df509SEmmanuel Vadot #define AW_MMC_DEBUG_IOS 0x2 199020df509SEmmanuel Vadot #define AW_MMC_DEBUG_INT 0x4 200020df509SEmmanuel Vadot #define AW_MMC_DEBUG_CMD 0x8 201020df509SEmmanuel Vadot 2025e03278fSIlya Bakulin #ifdef MMCCAM 2032671bdb5SEmmanuel Vadot static int 2042671bdb5SEmmanuel Vadot aw_mmc_get_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts) 2055e03278fSIlya Bakulin { 2065e03278fSIlya Bakulin struct aw_mmc_softc *sc; 2075e03278fSIlya Bakulin 2082671bdb5SEmmanuel Vadot sc = device_get_softc(dev); 2095e03278fSIlya Bakulin 2102671bdb5SEmmanuel Vadot cts->host_ocr = sc->aw_host.host_ocr; 2112671bdb5SEmmanuel Vadot cts->host_f_min = sc->aw_host.f_min; 2122671bdb5SEmmanuel Vadot cts->host_f_max = sc->aw_host.f_max; 2132671bdb5SEmmanuel Vadot cts->host_caps = sc->aw_host.caps; 2142671bdb5SEmmanuel Vadot cts->host_max_data = (sc->aw_mmc_conf->dma_xferlen * 2155d20e651SIlya Bakulin AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 2162671bdb5SEmmanuel Vadot memcpy(&cts->ios, &sc->aw_host.ios, sizeof(struct mmc_ios)); 2175e03278fSIlya Bakulin 2182671bdb5SEmmanuel Vadot return (0); 2195e03278fSIlya Bakulin } 2205e03278fSIlya Bakulin 2215e03278fSIlya Bakulin static int 2222671bdb5SEmmanuel Vadot aw_mmc_set_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts) 2235e03278fSIlya Bakulin { 2242671bdb5SEmmanuel Vadot struct aw_mmc_softc *sc; 2255e03278fSIlya Bakulin struct mmc_ios *ios; 2265e03278fSIlya Bakulin struct mmc_ios *new_ios; 2275e03278fSIlya Bakulin 2282671bdb5SEmmanuel Vadot sc = device_get_softc(dev); 2295e03278fSIlya Bakulin ios = &sc->aw_host.ios; 2305e03278fSIlya Bakulin new_ios = &cts->ios; 2315e03278fSIlya Bakulin 2325e03278fSIlya Bakulin /* Update only requested fields */ 2335e03278fSIlya Bakulin if (cts->ios_valid & MMC_CLK) { 2345e03278fSIlya Bakulin ios->clock = new_ios->clock; 235020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2365e03278fSIlya Bakulin device_printf(sc->aw_dev, "Clock => %d\n", ios->clock); 2375e03278fSIlya Bakulin } 2385e03278fSIlya Bakulin if (cts->ios_valid & MMC_VDD) { 2395e03278fSIlya Bakulin ios->vdd = new_ios->vdd; 240020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2415e03278fSIlya Bakulin device_printf(sc->aw_dev, "VDD => %d\n", ios->vdd); 2425e03278fSIlya Bakulin } 2435e03278fSIlya Bakulin if (cts->ios_valid & MMC_CS) { 2445e03278fSIlya Bakulin ios->chip_select = new_ios->chip_select; 245020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2465e03278fSIlya Bakulin device_printf(sc->aw_dev, "CS => %d\n", ios->chip_select); 2475e03278fSIlya Bakulin } 2485e03278fSIlya Bakulin if (cts->ios_valid & MMC_BW) { 2495e03278fSIlya Bakulin ios->bus_width = new_ios->bus_width; 250020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2515e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus width => %d\n", ios->bus_width); 2525e03278fSIlya Bakulin } 2535e03278fSIlya Bakulin if (cts->ios_valid & MMC_PM) { 2545e03278fSIlya Bakulin ios->power_mode = new_ios->power_mode; 255020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2565e03278fSIlya Bakulin device_printf(sc->aw_dev, "Power mode => %d\n", ios->power_mode); 2575e03278fSIlya Bakulin } 2585e03278fSIlya Bakulin if (cts->ios_valid & MMC_BT) { 2595e03278fSIlya Bakulin ios->timing = new_ios->timing; 260020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2615e03278fSIlya Bakulin device_printf(sc->aw_dev, "Timing => %d\n", ios->timing); 2625e03278fSIlya Bakulin } 2635e03278fSIlya Bakulin if (cts->ios_valid & MMC_BM) { 2645e03278fSIlya Bakulin ios->bus_mode = new_ios->bus_mode; 265020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2665e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus mode => %d\n", ios->bus_mode); 2675e03278fSIlya Bakulin } 2685e03278fSIlya Bakulin 2695e03278fSIlya Bakulin return (aw_mmc_update_ios(sc->aw_dev, NULL)); 2705e03278fSIlya Bakulin } 2715e03278fSIlya Bakulin 2725e03278fSIlya Bakulin static int 2732671bdb5SEmmanuel Vadot aw_mmc_cam_request(device_t dev, union ccb *ccb) 2745e03278fSIlya Bakulin { 2752671bdb5SEmmanuel Vadot struct aw_mmc_softc *sc; 2765e03278fSIlya Bakulin struct ccb_mmcio *mmcio; 2775e03278fSIlya Bakulin 2782671bdb5SEmmanuel Vadot sc = device_get_softc(dev); 2795e03278fSIlya Bakulin mmcio = &ccb->mmcio; 2805e03278fSIlya Bakulin 2815e03278fSIlya Bakulin AW_MMC_LOCK(sc); 2825e03278fSIlya Bakulin 283020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) { 2845e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2855e03278fSIlya Bakulin mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2865e03278fSIlya Bakulin mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 2875e03278fSIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0); 2885e03278fSIlya Bakulin } 2895e03278fSIlya Bakulin if (mmcio->cmd.data != NULL) { 2905e03278fSIlya Bakulin if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2915e03278fSIlya Bakulin panic("data->len = %d, data->flags = %d -- something is b0rked", 2925e03278fSIlya Bakulin (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2935e03278fSIlya Bakulin } 2945e03278fSIlya Bakulin if (sc->ccb != NULL) { 2955e03278fSIlya Bakulin device_printf(sc->aw_dev, "Controller still has an active command\n"); 2965e03278fSIlya Bakulin return (EBUSY); 2975e03278fSIlya Bakulin } 2985e03278fSIlya Bakulin sc->ccb = ccb; 2995e03278fSIlya Bakulin /* aw_mmc_request locks again */ 3005e03278fSIlya Bakulin AW_MMC_UNLOCK(sc); 3015e03278fSIlya Bakulin aw_mmc_request(sc->aw_dev, NULL, NULL); 3025e03278fSIlya Bakulin 3035e03278fSIlya Bakulin return (0); 3045e03278fSIlya Bakulin } 305af8b51b0SAndriy Gapon 306af8b51b0SAndriy Gapon static void 307af8b51b0SAndriy Gapon aw_mmc_cam_poll(device_t dev) 308af8b51b0SAndriy Gapon { 309af8b51b0SAndriy Gapon struct aw_mmc_softc *sc; 310af8b51b0SAndriy Gapon 311af8b51b0SAndriy Gapon sc = device_get_softc(dev); 312af8b51b0SAndriy Gapon aw_mmc_intr(sc); 313af8b51b0SAndriy Gapon } 3145e03278fSIlya Bakulin #endif /* MMCCAM */ 3155e03278fSIlya Bakulin 3169ed83210SEmmanuel Vadot static void 3179ed83210SEmmanuel Vadot aw_mmc_helper_cd_handler(device_t dev, bool present) 3189ed83210SEmmanuel Vadot { 3199ed83210SEmmanuel Vadot struct aw_mmc_softc *sc; 3209ed83210SEmmanuel Vadot 3219ed83210SEmmanuel Vadot sc = device_get_softc(dev); 3229bca4667SEmmanuel Vadot #ifdef MMCCAM 3232671bdb5SEmmanuel Vadot mmc_cam_sim_discover(&sc->mmc_sim); 3249bca4667SEmmanuel Vadot #else 3259ed83210SEmmanuel Vadot AW_MMC_LOCK(sc); 3269ed83210SEmmanuel Vadot if (present) { 3279ed83210SEmmanuel Vadot if (sc->child == NULL) { 328020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 3299ed83210SEmmanuel Vadot device_printf(sc->aw_dev, "Card inserted\n"); 3309ed83210SEmmanuel Vadot 331*5b56413dSWarner Losh sc->child = device_add_child(sc->aw_dev, "mmc", DEVICE_UNIT_ANY); 3329ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3339ed83210SEmmanuel Vadot if (sc->child) { 3349ed83210SEmmanuel Vadot device_set_ivars(sc->child, sc); 3359ed83210SEmmanuel Vadot (void)device_probe_and_attach(sc->child); 3369ed83210SEmmanuel Vadot } 3379ed83210SEmmanuel Vadot } else 3389ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3399ed83210SEmmanuel Vadot } else { 3409ed83210SEmmanuel Vadot /* Card isn't present, detach if necessary */ 3419ed83210SEmmanuel Vadot if (sc->child != NULL) { 342020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 3439ed83210SEmmanuel Vadot device_printf(sc->aw_dev, "Card removed\n"); 3449ed83210SEmmanuel Vadot 3459ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3469ed83210SEmmanuel Vadot device_delete_child(sc->aw_dev, sc->child); 3479ed83210SEmmanuel Vadot sc->child = NULL; 3489ed83210SEmmanuel Vadot } else 3499ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3509ed83210SEmmanuel Vadot } 3519bca4667SEmmanuel Vadot #endif /* MMCCAM */ 3529ed83210SEmmanuel Vadot } 3539ed83210SEmmanuel Vadot 354b5be541fSEmmanuel Vadot static int 355b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev) 356b5be541fSEmmanuel Vadot { 357b5be541fSEmmanuel Vadot 358b5be541fSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 359b5be541fSEmmanuel Vadot return (ENXIO); 360b5be541fSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 361b5be541fSEmmanuel Vadot return (ENXIO); 362b5be541fSEmmanuel Vadot 363b5be541fSEmmanuel Vadot device_set_desc(dev, "Allwinner Integrated MMC/SD controller"); 364b5be541fSEmmanuel Vadot 365b5be541fSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 366b5be541fSEmmanuel Vadot } 367b5be541fSEmmanuel Vadot 368b5be541fSEmmanuel Vadot static int 369b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev) 370b5be541fSEmmanuel Vadot { 371b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 372b5be541fSEmmanuel Vadot struct sysctl_ctx_list *ctx; 373b5be541fSEmmanuel Vadot struct sysctl_oid_list *tree; 374b5be541fSEmmanuel Vadot int error; 375b5be541fSEmmanuel Vadot 376b5be541fSEmmanuel Vadot sc = device_get_softc(dev); 377b5be541fSEmmanuel Vadot sc->aw_dev = dev; 378ce0618beSEmmanuel Vadot 379ce0618beSEmmanuel Vadot sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 380ce0618beSEmmanuel Vadot 3815e03278fSIlya Bakulin #ifndef MMCCAM 382b5be541fSEmmanuel Vadot sc->aw_req = NULL; 3835e03278fSIlya Bakulin #endif 384b5be541fSEmmanuel Vadot if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) { 385b5be541fSEmmanuel Vadot device_printf(dev, "cannot allocate device resources\n"); 386b5be541fSEmmanuel Vadot return (ENXIO); 387b5be541fSEmmanuel Vadot } 388b5be541fSEmmanuel Vadot if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES], 3899ed83210SEmmanuel Vadot INTR_TYPE_NET | INTR_MPSAFE, NULL, aw_mmc_intr, sc, 390b5be541fSEmmanuel Vadot &sc->aw_intrhand)) { 391b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 392b5be541fSEmmanuel Vadot device_printf(dev, "cannot setup interrupt handler\n"); 393b5be541fSEmmanuel Vadot return (ENXIO); 394b5be541fSEmmanuel Vadot } 395b5be541fSEmmanuel Vadot mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc", 396b5be541fSEmmanuel Vadot MTX_DEF); 397b5be541fSEmmanuel Vadot callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0); 398b5be541fSEmmanuel Vadot 399b5be541fSEmmanuel Vadot /* De-assert reset */ 400b5be541fSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) { 401b5be541fSEmmanuel Vadot error = hwreset_deassert(sc->aw_rst_ahb); 402b5be541fSEmmanuel Vadot if (error != 0) { 403b5be541fSEmmanuel Vadot device_printf(dev, "cannot de-assert reset\n"); 404b5be541fSEmmanuel Vadot goto fail; 405b5be541fSEmmanuel Vadot } 406b5be541fSEmmanuel Vadot } 407b5be541fSEmmanuel Vadot 408b5be541fSEmmanuel Vadot /* Activate the module clock. */ 409b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb); 410b5be541fSEmmanuel Vadot if (error != 0) { 411b5be541fSEmmanuel Vadot device_printf(dev, "cannot get ahb clock\n"); 412b5be541fSEmmanuel Vadot goto fail; 413b5be541fSEmmanuel Vadot } 414b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_ahb); 415b5be541fSEmmanuel Vadot if (error != 0) { 416b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable ahb clock\n"); 417b5be541fSEmmanuel Vadot goto fail; 418b5be541fSEmmanuel Vadot } 419b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc); 420b5be541fSEmmanuel Vadot if (error != 0) { 421b5be541fSEmmanuel Vadot device_printf(dev, "cannot get mmc clock\n"); 422b5be541fSEmmanuel Vadot goto fail; 423b5be541fSEmmanuel Vadot } 424b5be541fSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY, 425b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 426b5be541fSEmmanuel Vadot if (error != 0) { 427b5be541fSEmmanuel Vadot device_printf(dev, "cannot init mmc clock\n"); 428b5be541fSEmmanuel Vadot goto fail; 429b5be541fSEmmanuel Vadot } 430b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 431b5be541fSEmmanuel Vadot if (error != 0) { 432b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable mmc clock\n"); 433b5be541fSEmmanuel Vadot goto fail; 434b5be541fSEmmanuel Vadot } 435b5be541fSEmmanuel Vadot 436b5be541fSEmmanuel Vadot sc->aw_timeout = 10; 437b5be541fSEmmanuel Vadot ctx = device_get_sysctl_ctx(dev); 438b5be541fSEmmanuel Vadot tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 439b5be541fSEmmanuel Vadot SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW, 440b5be541fSEmmanuel Vadot &sc->aw_timeout, 0, "Request timeout in seconds"); 441b5be541fSEmmanuel Vadot 442b5be541fSEmmanuel Vadot /* Soft Reset controller. */ 443b5be541fSEmmanuel Vadot if (aw_mmc_reset(sc) != 0) { 444b5be541fSEmmanuel Vadot device_printf(dev, "cannot reset the controller\n"); 445b5be541fSEmmanuel Vadot goto fail; 446b5be541fSEmmanuel Vadot } 447b5be541fSEmmanuel Vadot 448b5be541fSEmmanuel Vadot if (aw_mmc_setup_dma(sc) != 0) { 449b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "Couldn't setup DMA!\n"); 450b5be541fSEmmanuel Vadot goto fail; 451b5be541fSEmmanuel Vadot } 452b5be541fSEmmanuel Vadot 4539ed83210SEmmanuel Vadot /* Set some defaults for freq and supported mode */ 454b5be541fSEmmanuel Vadot sc->aw_host.f_min = 400000; 4559ed83210SEmmanuel Vadot sc->aw_host.f_max = 52000000; 456b5be541fSEmmanuel Vadot sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340; 4579ed83210SEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_HSPEED | MMC_CAP_SIGNALING_330; 4589ed83210SEmmanuel Vadot mmc_fdt_parse(dev, 0, &sc->mmc_helper, &sc->aw_host); 4599ed83210SEmmanuel Vadot mmc_fdt_gpio_setup(dev, 0, &sc->mmc_helper, aw_mmc_helper_cd_handler); 460b5be541fSEmmanuel Vadot 4615e03278fSIlya Bakulin #ifdef MMCCAM 4625e03278fSIlya Bakulin sc->ccb = NULL; 4632671bdb5SEmmanuel Vadot 4642671bdb5SEmmanuel Vadot if (mmc_cam_sim_alloc(dev, "aw_mmc", &sc->mmc_sim) != 0) { 4652671bdb5SEmmanuel Vadot device_printf(dev, "cannot alloc cam sim\n"); 4665e03278fSIlya Bakulin goto fail; 4675e03278fSIlya Bakulin } 4685e03278fSIlya Bakulin #endif /* MMCCAM */ 4699ed83210SEmmanuel Vadot 470b5be541fSEmmanuel Vadot return (0); 471b5be541fSEmmanuel Vadot 472b5be541fSEmmanuel Vadot fail: 473b5be541fSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 474b5be541fSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 475b5be541fSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 476b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 477b5be541fSEmmanuel Vadot 478b5be541fSEmmanuel Vadot return (ENXIO); 479b5be541fSEmmanuel Vadot } 480b5be541fSEmmanuel Vadot 481b5be541fSEmmanuel Vadot static int 482b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev) 483b5be541fSEmmanuel Vadot { 484cbba9a7bSEmmanuel Vadot struct aw_mmc_softc *sc; 485cbba9a7bSEmmanuel Vadot device_t d; 486b5be541fSEmmanuel Vadot 487cbba9a7bSEmmanuel Vadot sc = device_get_softc(dev); 488cbba9a7bSEmmanuel Vadot 489cbba9a7bSEmmanuel Vadot clk_disable(sc->aw_clk_mmc); 490cbba9a7bSEmmanuel Vadot clk_disable(sc->aw_clk_ahb); 491cbba9a7bSEmmanuel Vadot hwreset_assert(sc->aw_rst_ahb); 492cbba9a7bSEmmanuel Vadot 493cbba9a7bSEmmanuel Vadot mmc_fdt_gpio_teardown(&sc->mmc_helper); 494cbba9a7bSEmmanuel Vadot 495cbba9a7bSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 496cbba9a7bSEmmanuel Vadot 497cbba9a7bSEmmanuel Vadot AW_MMC_LOCK(sc); 498cbba9a7bSEmmanuel Vadot d = sc->child; 499cbba9a7bSEmmanuel Vadot sc->child = NULL; 500cbba9a7bSEmmanuel Vadot AW_MMC_UNLOCK(sc); 501cbba9a7bSEmmanuel Vadot if (d != NULL) 502cbba9a7bSEmmanuel Vadot device_delete_child(sc->aw_dev, d); 503cbba9a7bSEmmanuel Vadot 504cbba9a7bSEmmanuel Vadot aw_mmc_teardown_dma(sc); 505cbba9a7bSEmmanuel Vadot 506cbba9a7bSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 507cbba9a7bSEmmanuel Vadot 508cbba9a7bSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 509cbba9a7bSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 510cbba9a7bSEmmanuel Vadot 511cbba9a7bSEmmanuel Vadot #ifdef MMCCAM 5122671bdb5SEmmanuel Vadot mmc_cam_sim_free(&sc->mmc_sim); 513cbba9a7bSEmmanuel Vadot #endif 514cbba9a7bSEmmanuel Vadot 515cbba9a7bSEmmanuel Vadot return (0); 516b5be541fSEmmanuel Vadot } 517b5be541fSEmmanuel Vadot 518b5be541fSEmmanuel Vadot static void 519b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 520b5be541fSEmmanuel Vadot { 521b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 522b5be541fSEmmanuel Vadot 523b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 524b5be541fSEmmanuel Vadot if (err) { 525b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 526b5be541fSEmmanuel Vadot return; 527b5be541fSEmmanuel Vadot } 528b5be541fSEmmanuel Vadot sc->aw_dma_desc_phys = segs[0].ds_addr; 529b5be541fSEmmanuel Vadot } 530b5be541fSEmmanuel Vadot 531b5be541fSEmmanuel Vadot static int 532b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc) 533b5be541fSEmmanuel Vadot { 534c39ea909SEmmanuel Vadot int error; 535b5be541fSEmmanuel Vadot 536b5be541fSEmmanuel Vadot /* Allocate the DMA descriptor memory. */ 537c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 538c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 539c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 540c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 541c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 542c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 543c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, 1, /* maxsize, nsegment */ 544c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, /* maxsegsize */ 545c39ea909SEmmanuel Vadot 0, /* flags */ 546c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 547c39ea909SEmmanuel Vadot &sc->aw_dma_tag); 548b5be541fSEmmanuel Vadot if (error) 549b5be541fSEmmanuel Vadot return (error); 550b5be541fSEmmanuel Vadot 551c39ea909SEmmanuel Vadot error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc, 552c39ea909SEmmanuel Vadot BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, 553c39ea909SEmmanuel Vadot &sc->aw_dma_map); 554c39ea909SEmmanuel Vadot if (error) 555c39ea909SEmmanuel Vadot return (error); 556c39ea909SEmmanuel Vadot 557c39ea909SEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_tag, 558c39ea909SEmmanuel Vadot sc->aw_dma_map, 559c39ea909SEmmanuel Vadot sc->aw_dma_desc, AW_MMC_DMA_DESC_SIZE, 560c39ea909SEmmanuel Vadot aw_dma_desc_cb, sc, 0); 561b5be541fSEmmanuel Vadot if (error) 562b5be541fSEmmanuel Vadot return (error); 563b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 564b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 565b5be541fSEmmanuel Vadot 566b5be541fSEmmanuel Vadot /* Create the DMA map for data transfers. */ 567c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 568c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 569c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 570c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 571c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 572c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 573c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen * 574c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS, /* maxsize, nsegments */ 575c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen, /* maxsegsize */ 576c39ea909SEmmanuel Vadot BUS_DMA_ALLOCNOW, /* flags */ 577c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 578b5be541fSEmmanuel Vadot &sc->aw_dma_buf_tag); 579b5be541fSEmmanuel Vadot if (error) 580b5be541fSEmmanuel Vadot return (error); 581b5be541fSEmmanuel Vadot error = bus_dmamap_create(sc->aw_dma_buf_tag, 0, 582b5be541fSEmmanuel Vadot &sc->aw_dma_buf_map); 583b5be541fSEmmanuel Vadot if (error) 584b5be541fSEmmanuel Vadot return (error); 585b5be541fSEmmanuel Vadot 586b5be541fSEmmanuel Vadot return (0); 587b5be541fSEmmanuel Vadot } 588b5be541fSEmmanuel Vadot 589b5be541fSEmmanuel Vadot static void 590cbba9a7bSEmmanuel Vadot aw_mmc_teardown_dma(struct aw_mmc_softc *sc) 591cbba9a7bSEmmanuel Vadot { 592cbba9a7bSEmmanuel Vadot 593cbba9a7bSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_tag, sc->aw_dma_map); 594cbba9a7bSEmmanuel Vadot bus_dmamem_free(sc->aw_dma_tag, sc->aw_dma_desc, sc->aw_dma_map); 595cbba9a7bSEmmanuel Vadot if (bus_dma_tag_destroy(sc->aw_dma_tag) != 0) 596cbba9a7bSEmmanuel Vadot device_printf(sc->aw_dev, "Cannot destroy the dma tag\n"); 597cbba9a7bSEmmanuel Vadot 598cbba9a7bSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 599cbba9a7bSEmmanuel Vadot bus_dmamap_destroy(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 600cbba9a7bSEmmanuel Vadot if (bus_dma_tag_destroy(sc->aw_dma_buf_tag) != 0) 601cbba9a7bSEmmanuel Vadot device_printf(sc->aw_dev, "Cannot destroy the dma buf tag\n"); 602cbba9a7bSEmmanuel Vadot } 603cbba9a7bSEmmanuel Vadot 604cbba9a7bSEmmanuel Vadot static void 605b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 606b5be541fSEmmanuel Vadot { 607b5be541fSEmmanuel Vadot int i; 608b5be541fSEmmanuel Vadot struct aw_mmc_dma_desc *dma_desc; 609b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 610b5be541fSEmmanuel Vadot 611b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 612b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 613b5be541fSEmmanuel Vadot 614b5be541fSEmmanuel Vadot if (err) 615b5be541fSEmmanuel Vadot return; 616b5be541fSEmmanuel Vadot 617b5be541fSEmmanuel Vadot dma_desc = sc->aw_dma_desc; 618b5be541fSEmmanuel Vadot for (i = 0; i < nsegs; i++) { 619c39ea909SEmmanuel Vadot if (segs[i].ds_len == sc->aw_mmc_conf->dma_xferlen) 620c39ea909SEmmanuel Vadot dma_desc[i].buf_size = 0; /* Size of 0 indicate max len */ 621c39ea909SEmmanuel Vadot else 622b5be541fSEmmanuel Vadot dma_desc[i].buf_size = segs[i].ds_len; 623b5be541fSEmmanuel Vadot dma_desc[i].buf_addr = segs[i].ds_addr; 624b5be541fSEmmanuel Vadot dma_desc[i].config = AW_MMC_DMA_CONFIG_CH | 625c39ea909SEmmanuel Vadot AW_MMC_DMA_CONFIG_OWN | AW_MMC_DMA_CONFIG_DIC; 626c39ea909SEmmanuel Vadot 627b5be541fSEmmanuel Vadot dma_desc[i].next = sc->aw_dma_desc_phys + 628b5be541fSEmmanuel Vadot ((i + 1) * sizeof(struct aw_mmc_dma_desc)); 629c39ea909SEmmanuel Vadot } 630c39ea909SEmmanuel Vadot 631c39ea909SEmmanuel Vadot dma_desc[0].config |= AW_MMC_DMA_CONFIG_FD; 632c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config |= AW_MMC_DMA_CONFIG_LD | 633b5be541fSEmmanuel Vadot AW_MMC_DMA_CONFIG_ER; 634c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config &= ~AW_MMC_DMA_CONFIG_DIC; 635c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].next = 0; 636b5be541fSEmmanuel Vadot } 637b5be541fSEmmanuel Vadot 638b5be541fSEmmanuel Vadot static int 639b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc) 640b5be541fSEmmanuel Vadot { 641b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 642b5be541fSEmmanuel Vadot int error; 643b5be541fSEmmanuel Vadot struct mmc_command *cmd; 644b5be541fSEmmanuel Vadot uint32_t val; 645b5be541fSEmmanuel Vadot 6465e03278fSIlya Bakulin #ifdef MMCCAM 6475e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 6485e03278fSIlya Bakulin #else 649b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 6505e03278fSIlya Bakulin #endif 651ce0618beSEmmanuel Vadot if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS)) 652b5be541fSEmmanuel Vadot return (EFBIG); 653b5be541fSEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 654b5be541fSEmmanuel Vadot cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0); 655b5be541fSEmmanuel Vadot if (error) 656b5be541fSEmmanuel Vadot return (error); 657b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 658b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 659b5be541fSEmmanuel Vadot 660b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 661b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREWRITE; 662b5be541fSEmmanuel Vadot else 663b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREREAD; 664b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op); 665b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE); 666b5be541fSEmmanuel Vadot 667b5be541fSEmmanuel Vadot /* Enable DMA */ 668b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 669b091392eSEmmanuel Vadot val &= ~AW_MMC_GCTL_FIFO_AC_MOD; 670b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_ENB; 671b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 672b5be541fSEmmanuel Vadot 673b5be541fSEmmanuel Vadot /* Reset DMA */ 674b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_RST; 675b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 676b5be541fSEmmanuel Vadot 677b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST); 678b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, 679b5be541fSEmmanuel Vadot AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST); 680b5be541fSEmmanuel Vadot 681b5be541fSEmmanuel Vadot /* Enable RX or TX DMA interrupt */ 682a37d59c1SEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_IDIE); 683b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 684b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_TX_INT; 685b5be541fSEmmanuel Vadot else 686b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_RX_INT; 687b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val); 688b5be541fSEmmanuel Vadot 689b5be541fSEmmanuel Vadot /* Set DMA descritptor list address */ 690b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys); 691b5be541fSEmmanuel Vadot 692b5be541fSEmmanuel Vadot /* FIFO trigger level */ 693b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL); 694b5be541fSEmmanuel Vadot 695b5be541fSEmmanuel Vadot return (0); 696b5be541fSEmmanuel Vadot } 697b5be541fSEmmanuel Vadot 698b5be541fSEmmanuel Vadot static int 699b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc) 700b5be541fSEmmanuel Vadot { 701b091392eSEmmanuel Vadot uint32_t reg; 702b5be541fSEmmanuel Vadot int timeout; 703b5be541fSEmmanuel Vadot 704b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 705b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_RESET; 706b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 707c39ea909SEmmanuel Vadot timeout = AW_MMC_RESET_RETRY; 708b5be541fSEmmanuel Vadot while (--timeout > 0) { 709b091392eSEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_GCTL_RESET) == 0) 710b5be541fSEmmanuel Vadot break; 711b5be541fSEmmanuel Vadot DELAY(100); 712b5be541fSEmmanuel Vadot } 713b5be541fSEmmanuel Vadot if (timeout == 0) 714b5be541fSEmmanuel Vadot return (ETIMEDOUT); 715b5be541fSEmmanuel Vadot 71635a18619SEmmanuel Vadot return (0); 71735a18619SEmmanuel Vadot } 71835a18619SEmmanuel Vadot 71935a18619SEmmanuel Vadot static int 72035a18619SEmmanuel Vadot aw_mmc_init(struct aw_mmc_softc *sc) 72135a18619SEmmanuel Vadot { 722b091392eSEmmanuel Vadot uint32_t reg; 72335a18619SEmmanuel Vadot int ret; 72435a18619SEmmanuel Vadot 72535a18619SEmmanuel Vadot ret = aw_mmc_reset(sc); 72635a18619SEmmanuel Vadot if (ret != 0) 72735a18619SEmmanuel Vadot return (ret); 72835a18619SEmmanuel Vadot 729b5be541fSEmmanuel Vadot /* Set the timeout. */ 730b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_TMOR, 731b5be541fSEmmanuel Vadot AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) | 732b5be541fSEmmanuel Vadot AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK)); 733b5be541fSEmmanuel Vadot 73435a18619SEmmanuel Vadot /* Unmask interrupts. */ 73535a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, 0); 73635a18619SEmmanuel Vadot 737b5be541fSEmmanuel Vadot /* Clear pending interrupts. */ 738b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 73935a18619SEmmanuel Vadot 74035a18619SEmmanuel Vadot /* Debug register, undocumented */ 74135a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DBGC, 0xdeb); 74235a18619SEmmanuel Vadot 74335a18619SEmmanuel Vadot /* Function select register */ 74435a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FUNS, 0xceaa0000); 74535a18619SEmmanuel Vadot 746b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff); 74735a18619SEmmanuel Vadot 748b091392eSEmmanuel Vadot /* Enable interrupts and disable AHB access. */ 749b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 750b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_INT_ENB; 751b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_FIFO_AC_MOD; 752b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_WAIT_MEM_ACCESS; 753b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 754b5be541fSEmmanuel Vadot 755b5be541fSEmmanuel Vadot return (0); 756b5be541fSEmmanuel Vadot } 757b5be541fSEmmanuel Vadot 758b5be541fSEmmanuel Vadot static void 759b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc) 760b5be541fSEmmanuel Vadot { 761b5be541fSEmmanuel Vadot struct mmc_command *cmd; 7625e03278fSIlya Bakulin #ifdef MMCCAM 7635e03278fSIlya Bakulin union ccb *ccb; 7645e03278fSIlya Bakulin #else 765b5be541fSEmmanuel Vadot struct mmc_request *req; 7665e03278fSIlya Bakulin #endif 767b5be541fSEmmanuel Vadot uint32_t val, mask; 768b5be541fSEmmanuel Vadot int retry; 769b5be541fSEmmanuel Vadot 7705e03278fSIlya Bakulin #ifdef MMCCAM 7715e03278fSIlya Bakulin ccb = sc->ccb; 7725e03278fSIlya Bakulin cmd = &ccb->mmcio.cmd; 7735e03278fSIlya Bakulin #else 774b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 7755e03278fSIlya Bakulin #endif 776020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) { 7775e03278fSIlya Bakulin device_printf(sc->aw_dev, "%s: cmd %d err %d\n", __func__, cmd->opcode, cmd->error); 7785e03278fSIlya Bakulin } 779b5be541fSEmmanuel Vadot if (cmd->error != MMC_ERR_NONE) { 780b5be541fSEmmanuel Vadot /* Reset the FIFO and DMA engines. */ 781b091392eSEmmanuel Vadot mask = AW_MMC_GCTL_FIFO_RST | AW_MMC_GCTL_DMA_RST; 782b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 783b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask); 784b5be541fSEmmanuel Vadot 785b5be541fSEmmanuel Vadot retry = AW_MMC_RESET_RETRY; 786b5be541fSEmmanuel Vadot while (--retry > 0) { 787c39ea909SEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & 788c39ea909SEmmanuel Vadot AW_MMC_GCTL_RESET) == 0) 789b5be541fSEmmanuel Vadot break; 790c39ea909SEmmanuel Vadot DELAY(100); 791b5be541fSEmmanuel Vadot } 792b5be541fSEmmanuel Vadot if (retry == 0) 793b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 794b5be541fSEmmanuel Vadot "timeout resetting DMA/FIFO\n"); 795b5be541fSEmmanuel Vadot aw_mmc_update_clock(sc, 1); 796b5be541fSEmmanuel Vadot } 797b5be541fSEmmanuel Vadot 798af8b51b0SAndriy Gapon if (!dumping) 799b5be541fSEmmanuel Vadot callout_stop(&sc->aw_timeoutc); 800b5be541fSEmmanuel Vadot sc->aw_intr = 0; 801b5be541fSEmmanuel Vadot sc->aw_resid = 0; 802b5be541fSEmmanuel Vadot sc->aw_dma_map_err = 0; 803b5be541fSEmmanuel Vadot sc->aw_intr_wait = 0; 8045e03278fSIlya Bakulin #ifdef MMCCAM 8055e03278fSIlya Bakulin sc->ccb = NULL; 8065e03278fSIlya Bakulin ccb->ccb_h.status = 8075e03278fSIlya Bakulin (ccb->mmcio.cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 8085e03278fSIlya Bakulin xpt_done(ccb); 8095e03278fSIlya Bakulin #else 8105e03278fSIlya Bakulin req = sc->aw_req; 8115e03278fSIlya Bakulin sc->aw_req = NULL; 812b5be541fSEmmanuel Vadot req->done(req); 8135e03278fSIlya Bakulin #endif 814b5be541fSEmmanuel Vadot } 815b5be541fSEmmanuel Vadot 816b5be541fSEmmanuel Vadot static void 817b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc) 818b5be541fSEmmanuel Vadot { 819b5be541fSEmmanuel Vadot int timeout; 820b5be541fSEmmanuel Vadot struct mmc_command *cmd; 821b5be541fSEmmanuel Vadot uint32_t status; 822b5be541fSEmmanuel Vadot 823b5be541fSEmmanuel Vadot timeout = 1000; 824b5be541fSEmmanuel Vadot while (--timeout > 0) { 825b5be541fSEmmanuel Vadot status = AW_MMC_READ_4(sc, AW_MMC_STAR); 826b5be541fSEmmanuel Vadot if ((status & AW_MMC_STAR_CARD_BUSY) == 0) 827b5be541fSEmmanuel Vadot break; 828b5be541fSEmmanuel Vadot DELAY(1000); 829b5be541fSEmmanuel Vadot } 8305e03278fSIlya Bakulin #ifdef MMCCAM 8315e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 8325e03278fSIlya Bakulin #else 833b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 8345e03278fSIlya Bakulin #endif 835b5be541fSEmmanuel Vadot if (timeout == 0) { 836b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 837b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 838b5be541fSEmmanuel Vadot return; 839b5be541fSEmmanuel Vadot } 840b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) { 841b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) { 842b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3); 843b5be541fSEmmanuel Vadot cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2); 844b5be541fSEmmanuel Vadot cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1); 845b5be541fSEmmanuel Vadot cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 846b5be541fSEmmanuel Vadot } else 847b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 848b5be541fSEmmanuel Vadot } 849b5be541fSEmmanuel Vadot /* All data has been transferred ? */ 850b5be541fSEmmanuel Vadot if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len) 851b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 852b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 853b5be541fSEmmanuel Vadot } 854b5be541fSEmmanuel Vadot 8555e03278fSIlya Bakulin static inline void 8565e03278fSIlya Bakulin set_mmc_error(struct aw_mmc_softc *sc, int error_code) 8575e03278fSIlya Bakulin { 8585e03278fSIlya Bakulin #ifdef MMCCAM 8595e03278fSIlya Bakulin sc->ccb->mmcio.cmd.error = error_code; 8605e03278fSIlya Bakulin #else 8615e03278fSIlya Bakulin sc->aw_req->cmd->error = error_code; 8625e03278fSIlya Bakulin #endif 8635e03278fSIlya Bakulin } 8645e03278fSIlya Bakulin 865b5be541fSEmmanuel Vadot static void 866b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg) 867b5be541fSEmmanuel Vadot { 868b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 869b5be541fSEmmanuel Vadot 870b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 8715e03278fSIlya Bakulin #ifdef MMCCAM 8725e03278fSIlya Bakulin if (sc->ccb != NULL) { 8735e03278fSIlya Bakulin #else 874b5be541fSEmmanuel Vadot if (sc->aw_req != NULL) { 8755e03278fSIlya Bakulin #endif 876b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "controller timeout\n"); 8775e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 878b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 879b5be541fSEmmanuel Vadot } else 880b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 881b5be541fSEmmanuel Vadot "Spurious timeout - no active request\n"); 882b5be541fSEmmanuel Vadot } 883b5be541fSEmmanuel Vadot 884b5be541fSEmmanuel Vadot static void 8855e03278fSIlya Bakulin aw_mmc_print_error(uint32_t err) 8865e03278fSIlya Bakulin { 8875e03278fSIlya Bakulin if(err & AW_MMC_INT_RESP_ERR) 8885e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_ERR "); 8895e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_CRC_ERR) 8905e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_CRC_ERR "); 8915e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_CRC_ERR) 8925e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_CRC_ERR "); 8935e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_TIMEOUT) 8945e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_TIMEOUT "); 8955e03278fSIlya Bakulin if (err & AW_MMC_INT_FIFO_RUN_ERR) 8965e03278fSIlya Bakulin printf("AW_MMC_INT_FIFO_RUN_ERR "); 8975e03278fSIlya Bakulin if (err & AW_MMC_INT_CMD_BUSY) 8985e03278fSIlya Bakulin printf("AW_MMC_INT_CMD_BUSY "); 8995e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_START_ERR) 9005e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_START_ERR "); 9015e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_END_BIT_ERR) 9025e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_END_BIT_ERR"); 9035e03278fSIlya Bakulin printf("\n"); 9045e03278fSIlya Bakulin } 9055e03278fSIlya Bakulin 9065e03278fSIlya Bakulin static void 907b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg) 908b5be541fSEmmanuel Vadot { 909b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 910b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 911b5be541fSEmmanuel Vadot struct mmc_data *data; 912b5be541fSEmmanuel Vadot uint32_t idst, imask, rint; 913b5be541fSEmmanuel Vadot 914b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 915b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 916b5be541fSEmmanuel Vadot rint = AW_MMC_READ_4(sc, AW_MMC_RISR); 917b5be541fSEmmanuel Vadot idst = AW_MMC_READ_4(sc, AW_MMC_IDST); 918b5be541fSEmmanuel Vadot imask = AW_MMC_READ_4(sc, AW_MMC_IMKR); 919b5be541fSEmmanuel Vadot if (idst == 0 && imask == 0 && rint == 0) { 920b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 921b5be541fSEmmanuel Vadot return; 922b5be541fSEmmanuel Vadot } 923020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) { 924b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n", 925b5be541fSEmmanuel Vadot idst, imask, rint); 926020df509SEmmanuel Vadot } 9275e03278fSIlya Bakulin #ifdef MMCCAM 9285e03278fSIlya Bakulin if (sc->ccb == NULL) { 9295e03278fSIlya Bakulin #else 930b5be541fSEmmanuel Vadot if (sc->aw_req == NULL) { 9315e03278fSIlya Bakulin #endif 932b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 933b5be541fSEmmanuel Vadot "Spurious interrupt - no active request, rint: 0x%08X\n", 934b5be541fSEmmanuel Vadot rint); 9355e03278fSIlya Bakulin aw_mmc_print_error(rint); 936b5be541fSEmmanuel Vadot goto end; 937b5be541fSEmmanuel Vadot } 938b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_ERR_BIT) { 939020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) { 940b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint); 9415e03278fSIlya Bakulin aw_mmc_print_error(rint); 942020df509SEmmanuel Vadot } 943b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_RESP_TIMEOUT) 9445e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 945b5be541fSEmmanuel Vadot else 9465e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 947b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 948b5be541fSEmmanuel Vadot goto end; 949b5be541fSEmmanuel Vadot } 950b5be541fSEmmanuel Vadot if (idst & AW_MMC_IDST_ERROR) { 951020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) 952b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst); 9535e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 954b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 955b5be541fSEmmanuel Vadot goto end; 956b5be541fSEmmanuel Vadot } 957b5be541fSEmmanuel Vadot 958b5be541fSEmmanuel Vadot sc->aw_intr |= rint; 9595e03278fSIlya Bakulin #ifdef MMCCAM 9605e03278fSIlya Bakulin data = sc->ccb->mmcio.cmd.data; 9615e03278fSIlya Bakulin #else 962b5be541fSEmmanuel Vadot data = sc->aw_req->cmd->data; 9635e03278fSIlya Bakulin #endif 964b5be541fSEmmanuel Vadot if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) { 965b5be541fSEmmanuel Vadot if (data->flags & MMC_DATA_WRITE) 966b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTWRITE; 967b5be541fSEmmanuel Vadot else 968b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTREAD; 969b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 970b5be541fSEmmanuel Vadot sync_op); 971b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, 972b5be541fSEmmanuel Vadot BUS_DMASYNC_POSTWRITE); 973b5be541fSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 974b5be541fSEmmanuel Vadot sc->aw_resid = data->len >> 2; 975b5be541fSEmmanuel Vadot } 976b5be541fSEmmanuel Vadot if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait) 977b5be541fSEmmanuel Vadot aw_mmc_req_ok(sc); 978b5be541fSEmmanuel Vadot 979b5be541fSEmmanuel Vadot end: 980b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst); 981b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint); 982b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 983b5be541fSEmmanuel Vadot } 984b5be541fSEmmanuel Vadot 985b5be541fSEmmanuel Vadot static int 986b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req) 987b5be541fSEmmanuel Vadot { 988b5be541fSEmmanuel Vadot int blksz; 989b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 990b5be541fSEmmanuel Vadot struct mmc_command *cmd; 99135a18619SEmmanuel Vadot uint32_t cmdreg, imask; 992b5be541fSEmmanuel Vadot int err; 993b5be541fSEmmanuel Vadot 994b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 995c39ea909SEmmanuel Vadot 996b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 9975e03278fSIlya Bakulin #ifdef MMCCAM 9985e03278fSIlya Bakulin KASSERT(req == NULL, ("req should be NULL in MMCCAM case!")); 9995e03278fSIlya Bakulin /* 10005e03278fSIlya Bakulin * For MMCCAM, sc->ccb has been NULL-checked and populated 10015e03278fSIlya Bakulin * by aw_mmc_cam_request() already. 10025e03278fSIlya Bakulin */ 10035e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 10045e03278fSIlya Bakulin #else 1005b5be541fSEmmanuel Vadot if (sc->aw_req) { 1006b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1007b5be541fSEmmanuel Vadot return (EBUSY); 1008b5be541fSEmmanuel Vadot } 1009b5be541fSEmmanuel Vadot sc->aw_req = req; 1010b5be541fSEmmanuel Vadot cmd = req->cmd; 10115e03278fSIlya Bakulin 1012020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) { 10135e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 10145e03278fSIlya Bakulin cmd->opcode, cmd->arg, cmd->flags, 10155e03278fSIlya Bakulin cmd->data != NULL ? (unsigned int)cmd->data->len : 0, 10165e03278fSIlya Bakulin cmd->data != NULL ? cmd->data->flags: 0); 1017020df509SEmmanuel Vadot } 10185e03278fSIlya Bakulin #endif 1019b5be541fSEmmanuel Vadot cmdreg = AW_MMC_CMDR_LOAD; 102035a18619SEmmanuel Vadot imask = AW_MMC_INT_ERR_BIT; 102135a18619SEmmanuel Vadot sc->aw_intr_wait = 0; 102235a18619SEmmanuel Vadot sc->aw_intr = 0; 102335a18619SEmmanuel Vadot sc->aw_resid = 0; 102435a18619SEmmanuel Vadot cmd->error = MMC_ERR_NONE; 102535a18619SEmmanuel Vadot 1026b5be541fSEmmanuel Vadot if (cmd->opcode == MMC_GO_IDLE_STATE) 1027b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ; 102835a18619SEmmanuel Vadot 1029b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) 1030b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_RESP_RCV; 1031b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) 1032b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_LONG_RESP; 1033b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_CRC) 1034b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC; 1035b5be541fSEmmanuel Vadot 103635a18619SEmmanuel Vadot if (cmd->data) { 1037b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER; 103835a18619SEmmanuel Vadot 1039b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_MULTI) { 1040b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG; 104135a18619SEmmanuel Vadot imask |= AW_MMC_INT_AUTO_STOP_DONE; 1042b5be541fSEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE; 104335a18619SEmmanuel Vadot } else { 104435a18619SEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER; 104535a18619SEmmanuel Vadot imask |= AW_MMC_INT_DATA_OVER; 1046b5be541fSEmmanuel Vadot } 1047b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 1048b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DIR_WRITE; 10495d5ae066SIlya Bakulin #ifdef MMCCAM 10505d5ae066SIlya Bakulin if (cmd->data->flags & MMC_DATA_BLOCK_SIZE) { 10515d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BKSR, cmd->data->block_size); 10525d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 10535d5ae066SIlya Bakulin } else 10545d5ae066SIlya Bakulin #endif 10555d5ae066SIlya Bakulin { 1056440565daSBjoern A. Zeeb blksz = min(cmd->data->len, MMC_SECTOR_SIZE); 1057b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz); 1058b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 10595d5ae066SIlya Bakulin } 106035a18619SEmmanuel Vadot } else { 106135a18619SEmmanuel Vadot imask |= AW_MMC_INT_CMD_DONE; 106235a18619SEmmanuel Vadot } 1063b5be541fSEmmanuel Vadot 106435a18619SEmmanuel Vadot /* Enable the interrupts we are interested in */ 106535a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, imask); 106635a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 106735a18619SEmmanuel Vadot 106835a18619SEmmanuel Vadot /* Enable auto stop if needed */ 106935a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_A12A, 107035a18619SEmmanuel Vadot cmdreg & AW_MMC_CMDR_STOP_CMD_FLAG ? 0 : 0xffff); 107135a18619SEmmanuel Vadot 107235a18619SEmmanuel Vadot /* Write the command argument */ 107335a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg); 107435a18619SEmmanuel Vadot 107535a18619SEmmanuel Vadot /* 107635a18619SEmmanuel Vadot * If we don't have data start the request 107735a18619SEmmanuel Vadot * if we do prepare the dma request and start the request 107835a18619SEmmanuel Vadot */ 107935a18619SEmmanuel Vadot if (cmd->data == NULL) { 108035a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 108135a18619SEmmanuel Vadot } else { 1082b5be541fSEmmanuel Vadot err = aw_mmc_prepare_dma(sc); 1083b5be541fSEmmanuel Vadot if (err != 0) 1084b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err); 108535a18619SEmmanuel Vadot 108635a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 1087b5be541fSEmmanuel Vadot } 1088b5be541fSEmmanuel Vadot 1089af8b51b0SAndriy Gapon if (!dumping) { 1090b5be541fSEmmanuel Vadot callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz, 1091b5be541fSEmmanuel Vadot aw_mmc_timeout, sc); 1092af8b51b0SAndriy Gapon } 1093b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1094b5be541fSEmmanuel Vadot 1095b5be541fSEmmanuel Vadot return (0); 1096b5be541fSEmmanuel Vadot } 1097b5be541fSEmmanuel Vadot 1098b5be541fSEmmanuel Vadot static int 1099b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which, 1100b5be541fSEmmanuel Vadot uintptr_t *result) 1101b5be541fSEmmanuel Vadot { 1102b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1103b5be541fSEmmanuel Vadot 1104b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1105b5be541fSEmmanuel Vadot switch (which) { 1106b5be541fSEmmanuel Vadot default: 1107b5be541fSEmmanuel Vadot return (EINVAL); 1108b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1109b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_mode; 1110b5be541fSEmmanuel Vadot break; 1111b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1112b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_width; 1113b5be541fSEmmanuel Vadot break; 1114b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1115b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.chip_select; 1116b5be541fSEmmanuel Vadot break; 1117b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1118b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.clock; 1119b5be541fSEmmanuel Vadot break; 1120b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1121b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_min; 1122b5be541fSEmmanuel Vadot break; 1123b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1124b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_max; 1125b5be541fSEmmanuel Vadot break; 1126b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1127b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.host_ocr; 1128b5be541fSEmmanuel Vadot break; 1129b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1130b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.mode; 1131b5be541fSEmmanuel Vadot break; 1132b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1133b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ocr; 1134b5be541fSEmmanuel Vadot break; 1135b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1136b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.power_mode; 1137b5be541fSEmmanuel Vadot break; 1138b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1139b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.vdd; 1140b5be541fSEmmanuel Vadot break; 1141dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1142dfb8c122SEmmanuel Vadot *(int *)result = sc->aw_host.ios.vccq; 1143dfb8c122SEmmanuel Vadot break; 1144b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1145b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.caps; 1146b5be541fSEmmanuel Vadot break; 1147ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1148ce0618beSEmmanuel Vadot *(int *)result = sc->aw_host.ios.timing; 1149ce0618beSEmmanuel Vadot break; 1150b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1151c39ea909SEmmanuel Vadot *(int *)result = (sc->aw_mmc_conf->dma_xferlen * 1152c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 1153b5be541fSEmmanuel Vadot break; 115455f3f71cSEmmanuel Vadot case MMCBR_IVAR_RETUNE_REQ: 115555f3f71cSEmmanuel Vadot *(int *)result = retune_req_none; 115655f3f71cSEmmanuel Vadot break; 1157b5be541fSEmmanuel Vadot } 1158b5be541fSEmmanuel Vadot 1159b5be541fSEmmanuel Vadot return (0); 1160b5be541fSEmmanuel Vadot } 1161b5be541fSEmmanuel Vadot 1162b5be541fSEmmanuel Vadot static int 1163b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which, 1164b5be541fSEmmanuel Vadot uintptr_t value) 1165b5be541fSEmmanuel Vadot { 1166b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1167b5be541fSEmmanuel Vadot 1168b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1169b5be541fSEmmanuel Vadot switch (which) { 1170b5be541fSEmmanuel Vadot default: 1171b5be541fSEmmanuel Vadot return (EINVAL); 1172b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1173b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_mode = value; 1174b5be541fSEmmanuel Vadot break; 1175b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1176b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_width = value; 1177b5be541fSEmmanuel Vadot break; 1178b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1179b5be541fSEmmanuel Vadot sc->aw_host.ios.chip_select = value; 1180b5be541fSEmmanuel Vadot break; 1181b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1182b5be541fSEmmanuel Vadot sc->aw_host.ios.clock = value; 1183b5be541fSEmmanuel Vadot break; 1184b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1185b5be541fSEmmanuel Vadot sc->aw_host.mode = value; 1186b5be541fSEmmanuel Vadot break; 1187b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1188b5be541fSEmmanuel Vadot sc->aw_host.ocr = value; 1189b5be541fSEmmanuel Vadot break; 1190b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1191b5be541fSEmmanuel Vadot sc->aw_host.ios.power_mode = value; 1192b5be541fSEmmanuel Vadot break; 1193b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1194b5be541fSEmmanuel Vadot sc->aw_host.ios.vdd = value; 1195b5be541fSEmmanuel Vadot break; 1196dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1197dfb8c122SEmmanuel Vadot sc->aw_host.ios.vccq = value; 1198dfb8c122SEmmanuel Vadot break; 1199ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1200ce0618beSEmmanuel Vadot sc->aw_host.ios.timing = value; 1201ce0618beSEmmanuel Vadot break; 1202b5be541fSEmmanuel Vadot /* These are read-only */ 1203b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1204b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1205b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1206b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1207b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1208b5be541fSEmmanuel Vadot return (EINVAL); 1209b5be541fSEmmanuel Vadot } 1210b5be541fSEmmanuel Vadot 1211b5be541fSEmmanuel Vadot return (0); 1212b5be541fSEmmanuel Vadot } 1213b5be541fSEmmanuel Vadot 1214b5be541fSEmmanuel Vadot static int 1215b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon) 1216b5be541fSEmmanuel Vadot { 1217ce0618beSEmmanuel Vadot uint32_t reg; 1218b5be541fSEmmanuel Vadot int retry; 1219b5be541fSEmmanuel Vadot 1220ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1221ffdb1aa8SEmmanuel Vadot reg &= ~(AW_MMC_CKCR_ENB | AW_MMC_CKCR_LOW_POWER | 1222ffdb1aa8SEmmanuel Vadot AW_MMC_CKCR_MASK_DATA0); 1223b5be541fSEmmanuel Vadot 1224b5be541fSEmmanuel Vadot if (clkon) 1225ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_ENB; 1226ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) 1227ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_MASK_DATA0; 1228b5be541fSEmmanuel Vadot 1229ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1230b5be541fSEmmanuel Vadot 1231ce0618beSEmmanuel Vadot reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK | 1232b5be541fSEmmanuel Vadot AW_MMC_CMDR_WAIT_PRE_OVER; 1233ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg); 1234b5be541fSEmmanuel Vadot retry = 0xfffff; 1235ce0618beSEmmanuel Vadot 1236ce0618beSEmmanuel Vadot while (reg & AW_MMC_CMDR_LOAD && --retry > 0) { 1237ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CMDR); 1238b5be541fSEmmanuel Vadot DELAY(10); 1239b5be541fSEmmanuel Vadot } 1240b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 1241b5be541fSEmmanuel Vadot 1242ce0618beSEmmanuel Vadot if (reg & AW_MMC_CMDR_LOAD) { 1243ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "timeout updating clock\n"); 1244b5be541fSEmmanuel Vadot return (ETIMEDOUT); 1245b5be541fSEmmanuel Vadot } 1246b5be541fSEmmanuel Vadot 1247ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) { 1248ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1249ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_MASK_DATA0; 1250ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1251ce0618beSEmmanuel Vadot } 1252ce0618beSEmmanuel Vadot 1253ce0618beSEmmanuel Vadot return (0); 1254ce0618beSEmmanuel Vadot } 1255ce0618beSEmmanuel Vadot 12562671bdb5SEmmanuel Vadot #ifndef MMCCAM 1257623966e1SEmmanuel Vadot static int 1258623966e1SEmmanuel Vadot aw_mmc_switch_vccq(device_t bus, device_t child) 1259ce0618beSEmmanuel Vadot { 1260623966e1SEmmanuel Vadot struct aw_mmc_softc *sc; 1261623966e1SEmmanuel Vadot int uvolt, err; 1262623966e1SEmmanuel Vadot 1263623966e1SEmmanuel Vadot sc = device_get_softc(bus); 1264ce0618beSEmmanuel Vadot 12659ed83210SEmmanuel Vadot if (sc->mmc_helper.vqmmc_supply == NULL) 1266623966e1SEmmanuel Vadot return EOPNOTSUPP; 1267ce0618beSEmmanuel Vadot 1268623966e1SEmmanuel Vadot switch (sc->aw_host.ios.vccq) { 1269dfb8c122SEmmanuel Vadot case vccq_180: 1270dfb8c122SEmmanuel Vadot uvolt = 1800000; 1271ce0618beSEmmanuel Vadot break; 1272dfb8c122SEmmanuel Vadot case vccq_330: 1273dfb8c122SEmmanuel Vadot uvolt = 3300000; 1274ce0618beSEmmanuel Vadot break; 1275dfb8c122SEmmanuel Vadot default: 1276623966e1SEmmanuel Vadot return EINVAL; 1277ce0618beSEmmanuel Vadot } 1278ce0618beSEmmanuel Vadot 12799ed83210SEmmanuel Vadot err = regulator_set_voltage(sc->mmc_helper.vqmmc_supply, uvolt, uvolt); 1280623966e1SEmmanuel Vadot if (err != 0) { 1281ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, 1282ce0618beSEmmanuel Vadot "Cannot set vqmmc to %d<->%d\n", 1283dfb8c122SEmmanuel Vadot uvolt, 1284dfb8c122SEmmanuel Vadot uvolt); 1285623966e1SEmmanuel Vadot return (err); 1286623966e1SEmmanuel Vadot } 1287623966e1SEmmanuel Vadot 1288623966e1SEmmanuel Vadot return (0); 1289ce0618beSEmmanuel Vadot } 12902671bdb5SEmmanuel Vadot #endif 1291ce0618beSEmmanuel Vadot 1292b5be541fSEmmanuel Vadot static int 1293b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child) 1294b5be541fSEmmanuel Vadot { 1295b5be541fSEmmanuel Vadot int error; 1296b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1297b5be541fSEmmanuel Vadot struct mmc_ios *ios; 1298ce0618beSEmmanuel Vadot unsigned int clock; 1299ce0618beSEmmanuel Vadot uint32_t reg, div = 1; 1300115e71a4SEmmanuel Vadot int reg_status; 1301115e71a4SEmmanuel Vadot int rv; 1302b5be541fSEmmanuel Vadot 1303b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1304b5be541fSEmmanuel Vadot 1305b5be541fSEmmanuel Vadot ios = &sc->aw_host.ios; 1306b5be541fSEmmanuel Vadot 1307b5be541fSEmmanuel Vadot /* Set the bus width. */ 1308b5be541fSEmmanuel Vadot switch (ios->bus_width) { 1309b5be541fSEmmanuel Vadot case bus_width_1: 1310b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1); 1311b5be541fSEmmanuel Vadot break; 1312b5be541fSEmmanuel Vadot case bus_width_4: 1313b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4); 1314b5be541fSEmmanuel Vadot break; 1315b5be541fSEmmanuel Vadot case bus_width_8: 1316b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8); 1317b5be541fSEmmanuel Vadot break; 1318b5be541fSEmmanuel Vadot } 1319b5be541fSEmmanuel Vadot 132035a18619SEmmanuel Vadot switch (ios->power_mode) { 132135a18619SEmmanuel Vadot case power_on: 132235a18619SEmmanuel Vadot break; 132335a18619SEmmanuel Vadot case power_off: 1324020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 1325ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "Powering down sd/mmc\n"); 1326dfb8c122SEmmanuel Vadot 1327115e71a4SEmmanuel Vadot if (sc->mmc_helper.vmmc_supply) { 1328115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vmmc_supply, ®_status); 1329115e71a4SEmmanuel Vadot if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 13309ed83210SEmmanuel Vadot regulator_disable(sc->mmc_helper.vmmc_supply); 1331115e71a4SEmmanuel Vadot } 1332115e71a4SEmmanuel Vadot if (sc->mmc_helper.vqmmc_supply) { 1333115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vqmmc_supply, ®_status); 1334115e71a4SEmmanuel Vadot if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 13359ed83210SEmmanuel Vadot regulator_disable(sc->mmc_helper.vqmmc_supply); 1336115e71a4SEmmanuel Vadot } 1337dfb8c122SEmmanuel Vadot 13386bc8fe8aSEmmanuel Vadot if (sc->mmc_helper.mmc_pwrseq) 13396bc8fe8aSEmmanuel Vadot MMC_PWRSEQ_SET_POWER(sc->mmc_helper.mmc_pwrseq, false); 13406bc8fe8aSEmmanuel Vadot 134135a18619SEmmanuel Vadot aw_mmc_reset(sc); 134235a18619SEmmanuel Vadot break; 134335a18619SEmmanuel Vadot case power_up: 1344020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 134535a18619SEmmanuel Vadot device_printf(sc->aw_dev, "Powering up sd/mmc\n"); 1346dfb8c122SEmmanuel Vadot 1347115e71a4SEmmanuel Vadot if (sc->mmc_helper.vmmc_supply) { 1348115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vmmc_supply, ®_status); 1349115e71a4SEmmanuel Vadot if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 13509ed83210SEmmanuel Vadot regulator_enable(sc->mmc_helper.vmmc_supply); 1351115e71a4SEmmanuel Vadot } 1352115e71a4SEmmanuel Vadot if (sc->mmc_helper.vqmmc_supply) { 1353115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vqmmc_supply, ®_status); 1354115e71a4SEmmanuel Vadot if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 13559ed83210SEmmanuel Vadot regulator_enable(sc->mmc_helper.vqmmc_supply); 1356115e71a4SEmmanuel Vadot } 13576bc8fe8aSEmmanuel Vadot 13586bc8fe8aSEmmanuel Vadot if (sc->mmc_helper.mmc_pwrseq) 13596bc8fe8aSEmmanuel Vadot MMC_PWRSEQ_SET_POWER(sc->mmc_helper.mmc_pwrseq, true); 136035a18619SEmmanuel Vadot aw_mmc_init(sc); 136135a18619SEmmanuel Vadot break; 136235a18619SEmmanuel Vadot }; 1363ce0618beSEmmanuel Vadot 1364ce0618beSEmmanuel Vadot /* Enable ddr mode if needed */ 1365ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 1366ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_uhs_ddr50 || 1367ce0618beSEmmanuel Vadot ios->timing == bus_timing_mmc_ddr52) 1368b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_DDR_MOD_SEL; 1369ce0618beSEmmanuel Vadot else 1370b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_DDR_MOD_SEL; 1371ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 1372ce0618beSEmmanuel Vadot 13730f7a6420SEmmanuel Vadot if (ios->clock && ios->clock != sc->aw_clock) { 13740f7a6420SEmmanuel Vadot sc->aw_clock = clock = ios->clock; 1375b5be541fSEmmanuel Vadot 1376b5be541fSEmmanuel Vadot /* Disable clock */ 1377b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 0); 1378b5be541fSEmmanuel Vadot if (error != 0) 1379b5be541fSEmmanuel Vadot return (error); 1380b5be541fSEmmanuel Vadot 1381ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_mmc_ddr52 && 1382ce0618beSEmmanuel Vadot (sc->aw_mmc_conf->new_timing || 1383ce0618beSEmmanuel Vadot ios->bus_width == bus_width_8)) { 1384ce0618beSEmmanuel Vadot div = 2; 1385ce0618beSEmmanuel Vadot clock <<= 1; 1386ce0618beSEmmanuel Vadot } 1387ce0618beSEmmanuel Vadot 1388b5be541fSEmmanuel Vadot /* Reset the divider. */ 1389ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1390ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_DIV; 1391ce0618beSEmmanuel Vadot reg |= div - 1; 1392ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1393ce0618beSEmmanuel Vadot 1394ce0618beSEmmanuel Vadot /* New timing mode if needed */ 1395ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->new_timing) { 1396ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_NTSR); 1397ce0618beSEmmanuel Vadot reg |= AW_MMC_NTSR_MODE_SELECT; 1398ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg); 1399ce0618beSEmmanuel Vadot } 1400b5be541fSEmmanuel Vadot 1401b5be541fSEmmanuel Vadot /* Set the MMC clock. */ 1402101260f3SEmmanuel Vadot error = clk_disable(sc->aw_clk_mmc); 1403101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1404101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1405101260f3SEmmanuel Vadot "failed to disable mmc clock: %d\n", error); 1406ce0618beSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, clock, 1407b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 1408b5be541fSEmmanuel Vadot if (error != 0) { 1409b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 1410b5be541fSEmmanuel Vadot "failed to set frequency to %u Hz: %d\n", 1411ce0618beSEmmanuel Vadot clock, error); 1412b5be541fSEmmanuel Vadot return (error); 1413b5be541fSEmmanuel Vadot } 1414101260f3SEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 1415101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1416101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1417101260f3SEmmanuel Vadot "failed to re-enable mmc clock: %d\n", error); 1418b5be541fSEmmanuel Vadot 1419ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->can_calibrate) 1420ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN); 1421ce0618beSEmmanuel Vadot 1422b5be541fSEmmanuel Vadot /* Enable clock. */ 1423b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 1); 1424b5be541fSEmmanuel Vadot if (error != 0) 1425b5be541fSEmmanuel Vadot return (error); 1426b5be541fSEmmanuel Vadot } 1427b5be541fSEmmanuel Vadot 1428b5be541fSEmmanuel Vadot return (0); 1429b5be541fSEmmanuel Vadot } 1430b5be541fSEmmanuel Vadot 14312671bdb5SEmmanuel Vadot #ifndef MMCCAM 1432b5be541fSEmmanuel Vadot static int 1433b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child) 1434b5be541fSEmmanuel Vadot { 14359ed83210SEmmanuel Vadot struct aw_mmc_softc *sc; 1436b5be541fSEmmanuel Vadot 14379ed83210SEmmanuel Vadot sc = device_get_softc(bus); 14389ed83210SEmmanuel Vadot 14399ed83210SEmmanuel Vadot return (mmc_fdt_gpio_get_readonly(&sc->mmc_helper)); 1440b5be541fSEmmanuel Vadot } 1441b5be541fSEmmanuel Vadot 1442b5be541fSEmmanuel Vadot static int 1443b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child) 1444b5be541fSEmmanuel Vadot { 1445b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1446b5be541fSEmmanuel Vadot int error; 1447b5be541fSEmmanuel Vadot 1448b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1449b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1450b5be541fSEmmanuel Vadot while (sc->aw_bus_busy) { 1451b5be541fSEmmanuel Vadot error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0); 1452b5be541fSEmmanuel Vadot if (error != 0) { 1453b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1454b5be541fSEmmanuel Vadot return (error); 1455b5be541fSEmmanuel Vadot } 1456b5be541fSEmmanuel Vadot } 1457b5be541fSEmmanuel Vadot sc->aw_bus_busy++; 1458b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1459b5be541fSEmmanuel Vadot 1460b5be541fSEmmanuel Vadot return (0); 1461b5be541fSEmmanuel Vadot } 1462b5be541fSEmmanuel Vadot 1463b5be541fSEmmanuel Vadot static int 1464b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child) 1465b5be541fSEmmanuel Vadot { 1466b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1467b5be541fSEmmanuel Vadot 1468b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1469b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1470b5be541fSEmmanuel Vadot sc->aw_bus_busy--; 1471b5be541fSEmmanuel Vadot wakeup(sc); 1472b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1473b5be541fSEmmanuel Vadot 1474b5be541fSEmmanuel Vadot return (0); 1475b5be541fSEmmanuel Vadot } 14762671bdb5SEmmanuel Vadot #endif 1477b5be541fSEmmanuel Vadot 1478b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = { 1479b5be541fSEmmanuel Vadot /* Device interface */ 1480b5be541fSEmmanuel Vadot DEVMETHOD(device_probe, aw_mmc_probe), 1481b5be541fSEmmanuel Vadot DEVMETHOD(device_attach, aw_mmc_attach), 1482b5be541fSEmmanuel Vadot DEVMETHOD(device_detach, aw_mmc_detach), 1483b5be541fSEmmanuel Vadot 1484b5be541fSEmmanuel Vadot /* Bus interface */ 1485b5be541fSEmmanuel Vadot DEVMETHOD(bus_read_ivar, aw_mmc_read_ivar), 1486b5be541fSEmmanuel Vadot DEVMETHOD(bus_write_ivar, aw_mmc_write_ivar), 1487ef546520SIlya Bakulin DEVMETHOD(bus_add_child, bus_generic_add_child), 1488b5be541fSEmmanuel Vadot 14892671bdb5SEmmanuel Vadot #ifndef MMCCAM 1490b5be541fSEmmanuel Vadot /* MMC bridge interface */ 1491b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_update_ios, aw_mmc_update_ios), 1492b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_request, aw_mmc_request), 1493b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_get_ro, aw_mmc_get_ro), 1494623966e1SEmmanuel Vadot DEVMETHOD(mmcbr_switch_vccq, aw_mmc_switch_vccq), 1495b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_acquire_host, aw_mmc_acquire_host), 1496b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_release_host, aw_mmc_release_host), 14972671bdb5SEmmanuel Vadot #endif 14982671bdb5SEmmanuel Vadot 14992671bdb5SEmmanuel Vadot #ifdef MMCCAM 15002671bdb5SEmmanuel Vadot /* MMCCAM interface */ 15012671bdb5SEmmanuel Vadot DEVMETHOD(mmc_sim_get_tran_settings, aw_mmc_get_tran_settings), 15022671bdb5SEmmanuel Vadot DEVMETHOD(mmc_sim_set_tran_settings, aw_mmc_set_tran_settings), 15032671bdb5SEmmanuel Vadot DEVMETHOD(mmc_sim_cam_request, aw_mmc_cam_request), 1504af8b51b0SAndriy Gapon DEVMETHOD(mmc_sim_cam_poll, aw_mmc_cam_poll), 15052671bdb5SEmmanuel Vadot #endif 1506b5be541fSEmmanuel Vadot 1507b5be541fSEmmanuel Vadot DEVMETHOD_END 1508b5be541fSEmmanuel Vadot }; 1509b5be541fSEmmanuel Vadot 1510b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = { 1511b5be541fSEmmanuel Vadot "aw_mmc", 1512b5be541fSEmmanuel Vadot aw_mmc_methods, 1513b5be541fSEmmanuel Vadot sizeof(struct aw_mmc_softc), 1514b5be541fSEmmanuel Vadot }; 1515b5be541fSEmmanuel Vadot 15167e1e2ba1SJohn Baldwin DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, NULL, NULL); 15175e03278fSIlya Bakulin #ifndef MMCCAM 1518b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc); 15195e03278fSIlya Bakulin #endif 1520cbba9a7bSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data); 1521