1b5be541fSEmmanuel Vadot /*- 2b091392eSEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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/cdefs.h> 31b5be541fSEmmanuel Vadot __FBSDID("$FreeBSD$"); 32b5be541fSEmmanuel Vadot 33b5be541fSEmmanuel Vadot #include <sys/param.h> 34b5be541fSEmmanuel Vadot #include <sys/systm.h> 35b5be541fSEmmanuel Vadot #include <sys/bus.h> 36b5be541fSEmmanuel Vadot #include <sys/kernel.h> 37b5be541fSEmmanuel Vadot #include <sys/lock.h> 38b5be541fSEmmanuel Vadot #include <sys/malloc.h> 39b5be541fSEmmanuel Vadot #include <sys/module.h> 40b5be541fSEmmanuel Vadot #include <sys/mutex.h> 41b5be541fSEmmanuel Vadot #include <sys/resource.h> 42b5be541fSEmmanuel Vadot #include <sys/rman.h> 43b5be541fSEmmanuel Vadot #include <sys/sysctl.h> 449ed83210SEmmanuel Vadot #include <sys/queue.h> 459ed83210SEmmanuel Vadot #include <sys/taskqueue.h> 46b5be541fSEmmanuel Vadot 47b5be541fSEmmanuel Vadot #include <machine/bus.h> 48b5be541fSEmmanuel Vadot 49b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 50b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 51b5be541fSEmmanuel Vadot 52b5be541fSEmmanuel Vadot #include <dev/mmc/bridge.h> 53b5be541fSEmmanuel Vadot #include <dev/mmc/mmcbrvar.h> 549ed83210SEmmanuel Vadot #include <dev/mmc/mmc_fdt_helpers.h> 55b5be541fSEmmanuel Vadot 56b5be541fSEmmanuel Vadot #include <arm/allwinner/aw_mmc.h> 57b5be541fSEmmanuel Vadot #include <dev/extres/clk/clk.h> 58b5be541fSEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h> 59ce0618beSEmmanuel Vadot #include <dev/extres/regulator/regulator.h> 60b5be541fSEmmanuel Vadot 615e03278fSIlya Bakulin #include "opt_mmccam.h" 625e03278fSIlya Bakulin 635e03278fSIlya Bakulin #ifdef MMCCAM 645e03278fSIlya Bakulin #include <cam/cam.h> 655e03278fSIlya Bakulin #include <cam/cam_ccb.h> 665e03278fSIlya Bakulin #include <cam/cam_debug.h> 675e03278fSIlya Bakulin #include <cam/cam_sim.h> 685e03278fSIlya Bakulin #include <cam/cam_xpt_sim.h> 692671bdb5SEmmanuel Vadot #include <cam/mmc/mmc_sim.h> 702671bdb5SEmmanuel Vadot 712671bdb5SEmmanuel Vadot #include "mmc_sim_if.h" 725e03278fSIlya Bakulin #endif 735e03278fSIlya Bakulin 74*6bc8fe8aSEmmanuel Vadot #include "mmc_pwrseq_if.h" 75*6bc8fe8aSEmmanuel Vadot 76b5be541fSEmmanuel Vadot #define AW_MMC_MEMRES 0 77b5be541fSEmmanuel Vadot #define AW_MMC_IRQRES 1 78b5be541fSEmmanuel Vadot #define AW_MMC_RESSZ 2 79c39ea909SEmmanuel Vadot #define AW_MMC_DMA_SEGS (PAGE_SIZE / sizeof(struct aw_mmc_dma_desc)) 80c39ea909SEmmanuel Vadot #define AW_MMC_DMA_DESC_SIZE (sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS) 81b5be541fSEmmanuel Vadot #define AW_MMC_DMA_FTRGLEVEL 0x20070008 82c39ea909SEmmanuel Vadot 83b5be541fSEmmanuel Vadot #define AW_MMC_RESET_RETRY 1000 84b5be541fSEmmanuel Vadot 85b5be541fSEmmanuel Vadot #define CARD_ID_FREQUENCY 400000 86b5be541fSEmmanuel Vadot 87ce0618beSEmmanuel Vadot struct aw_mmc_conf { 88ce0618beSEmmanuel Vadot uint32_t dma_xferlen; 89ce0618beSEmmanuel Vadot bool mask_data0; 90ce0618beSEmmanuel Vadot bool can_calibrate; 91ce0618beSEmmanuel Vadot bool new_timing; 92ce0618beSEmmanuel Vadot }; 93ce0618beSEmmanuel Vadot 94ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a10_mmc_conf = { 95ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 96ce0618beSEmmanuel Vadot }; 97ce0618beSEmmanuel Vadot 98ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a13_mmc_conf = { 99ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 100ce0618beSEmmanuel Vadot }; 101ce0618beSEmmanuel Vadot 102ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_mmc_conf = { 103ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 104ce0618beSEmmanuel Vadot .mask_data0 = true, 105ce0618beSEmmanuel Vadot .can_calibrate = true, 106ce0618beSEmmanuel Vadot .new_timing = true, 107ce0618beSEmmanuel Vadot }; 108ce0618beSEmmanuel Vadot 109ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_emmc_conf = { 110ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 111ce0618beSEmmanuel Vadot .can_calibrate = true, 112ce0618beSEmmanuel Vadot }; 113ce0618beSEmmanuel Vadot 114b5be541fSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 115ce0618beSEmmanuel Vadot {"allwinner,sun4i-a10-mmc", (uintptr_t)&a10_mmc_conf}, 116ce0618beSEmmanuel Vadot {"allwinner,sun5i-a13-mmc", (uintptr_t)&a13_mmc_conf}, 117ce0618beSEmmanuel Vadot {"allwinner,sun7i-a20-mmc", (uintptr_t)&a13_mmc_conf}, 118ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-mmc", (uintptr_t)&a64_mmc_conf}, 119ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-emmc", (uintptr_t)&a64_emmc_conf}, 120b5be541fSEmmanuel Vadot {NULL, 0} 121b5be541fSEmmanuel Vadot }; 122b5be541fSEmmanuel Vadot 123b5be541fSEmmanuel Vadot struct aw_mmc_softc { 124b5be541fSEmmanuel Vadot device_t aw_dev; 125b5be541fSEmmanuel Vadot clk_t aw_clk_ahb; 126b5be541fSEmmanuel Vadot clk_t aw_clk_mmc; 127b5be541fSEmmanuel Vadot hwreset_t aw_rst_ahb; 128b5be541fSEmmanuel Vadot int aw_bus_busy; 129b5be541fSEmmanuel Vadot int aw_resid; 130b5be541fSEmmanuel Vadot int aw_timeout; 131b5be541fSEmmanuel Vadot struct callout aw_timeoutc; 132b5be541fSEmmanuel Vadot struct mmc_host aw_host; 1339ed83210SEmmanuel Vadot struct mmc_fdt_helper mmc_helper; 1345e03278fSIlya Bakulin #ifdef MMCCAM 1355e03278fSIlya Bakulin union ccb * ccb; 1362671bdb5SEmmanuel Vadot struct mmc_sim mmc_sim; 1375e03278fSIlya Bakulin #else 138b5be541fSEmmanuel Vadot struct mmc_request * aw_req; 1395e03278fSIlya Bakulin #endif 140b5be541fSEmmanuel Vadot struct mtx aw_mtx; 141b5be541fSEmmanuel Vadot struct resource * aw_res[AW_MMC_RESSZ]; 142ce0618beSEmmanuel Vadot struct aw_mmc_conf * aw_mmc_conf; 143b5be541fSEmmanuel Vadot uint32_t aw_intr; 144b5be541fSEmmanuel Vadot uint32_t aw_intr_wait; 145b5be541fSEmmanuel Vadot void * aw_intrhand; 1460f7a6420SEmmanuel Vadot unsigned int aw_clock; 1479ed83210SEmmanuel Vadot device_t child; 148b5be541fSEmmanuel Vadot 149b5be541fSEmmanuel Vadot /* Fields required for DMA access. */ 150b5be541fSEmmanuel Vadot bus_addr_t aw_dma_desc_phys; 151b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_map; 152b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_tag; 153b5be541fSEmmanuel Vadot void * aw_dma_desc; 154b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_buf_map; 155b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_buf_tag; 156b5be541fSEmmanuel Vadot int aw_dma_map_err; 157b5be541fSEmmanuel Vadot }; 158b5be541fSEmmanuel Vadot 159b5be541fSEmmanuel Vadot static struct resource_spec aw_mmc_res_spec[] = { 160b5be541fSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE }, 161b5be541fSEmmanuel Vadot { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 162b5be541fSEmmanuel Vadot { -1, 0, 0 } 163b5be541fSEmmanuel Vadot }; 164b5be541fSEmmanuel Vadot 165b5be541fSEmmanuel Vadot static int aw_mmc_probe(device_t); 166b5be541fSEmmanuel Vadot static int aw_mmc_attach(device_t); 167b5be541fSEmmanuel Vadot static int aw_mmc_detach(device_t); 168b5be541fSEmmanuel Vadot static int aw_mmc_setup_dma(struct aw_mmc_softc *); 169cbba9a7bSEmmanuel Vadot static void aw_mmc_teardown_dma(struct aw_mmc_softc *sc); 170b5be541fSEmmanuel Vadot static int aw_mmc_reset(struct aw_mmc_softc *); 17135a18619SEmmanuel Vadot static int aw_mmc_init(struct aw_mmc_softc *); 172b5be541fSEmmanuel Vadot static void aw_mmc_intr(void *); 173b5be541fSEmmanuel Vadot static int aw_mmc_update_clock(struct aw_mmc_softc *, uint32_t); 1749ed83210SEmmanuel Vadot static void aw_mmc_helper_cd_handler(device_t, bool); 175b5be541fSEmmanuel Vadot 1765e03278fSIlya Bakulin static void aw_mmc_print_error(uint32_t); 177b5be541fSEmmanuel Vadot static int aw_mmc_update_ios(device_t, device_t); 178b5be541fSEmmanuel Vadot static int aw_mmc_request(device_t, device_t, struct mmc_request *); 1792671bdb5SEmmanuel Vadot 1802671bdb5SEmmanuel Vadot #ifndef MMCCAM 181b5be541fSEmmanuel Vadot static int aw_mmc_get_ro(device_t, device_t); 182b5be541fSEmmanuel Vadot static int aw_mmc_acquire_host(device_t, device_t); 183b5be541fSEmmanuel Vadot static int aw_mmc_release_host(device_t, device_t); 1845e03278fSIlya Bakulin #endif 185b5be541fSEmmanuel Vadot 186b5be541fSEmmanuel Vadot #define AW_MMC_LOCK(_sc) mtx_lock(&(_sc)->aw_mtx) 187b5be541fSEmmanuel Vadot #define AW_MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->aw_mtx) 188b5be541fSEmmanuel Vadot #define AW_MMC_READ_4(_sc, _reg) \ 189b5be541fSEmmanuel Vadot bus_read_4((_sc)->aw_res[AW_MMC_MEMRES], _reg) 190b5be541fSEmmanuel Vadot #define AW_MMC_WRITE_4(_sc, _reg, _value) \ 191b5be541fSEmmanuel Vadot bus_write_4((_sc)->aw_res[AW_MMC_MEMRES], _reg, _value) 192b5be541fSEmmanuel Vadot 193020df509SEmmanuel Vadot SYSCTL_NODE(_hw, OID_AUTO, aw_mmc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 194020df509SEmmanuel Vadot "aw_mmc driver"); 195020df509SEmmanuel Vadot 196020df509SEmmanuel Vadot static int aw_mmc_debug = 0; 197020df509SEmmanuel Vadot SYSCTL_INT(_hw_aw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &aw_mmc_debug, 0, 198020df509SEmmanuel Vadot "Debug level bit0=card changes bit1=ios changes, bit2=interrupts, bit3=commands"); 199020df509SEmmanuel Vadot #define AW_MMC_DEBUG_CARD 0x1 200020df509SEmmanuel Vadot #define AW_MMC_DEBUG_IOS 0x2 201020df509SEmmanuel Vadot #define AW_MMC_DEBUG_INT 0x4 202020df509SEmmanuel Vadot #define AW_MMC_DEBUG_CMD 0x8 203020df509SEmmanuel Vadot 2045e03278fSIlya Bakulin #ifdef MMCCAM 2052671bdb5SEmmanuel Vadot static int 2062671bdb5SEmmanuel Vadot aw_mmc_get_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts) 2075e03278fSIlya Bakulin { 2085e03278fSIlya Bakulin struct aw_mmc_softc *sc; 2095e03278fSIlya Bakulin 2102671bdb5SEmmanuel Vadot sc = device_get_softc(dev); 2115e03278fSIlya Bakulin 2122671bdb5SEmmanuel Vadot cts->host_ocr = sc->aw_host.host_ocr; 2132671bdb5SEmmanuel Vadot cts->host_f_min = sc->aw_host.f_min; 2142671bdb5SEmmanuel Vadot cts->host_f_max = sc->aw_host.f_max; 2152671bdb5SEmmanuel Vadot cts->host_caps = sc->aw_host.caps; 2162671bdb5SEmmanuel Vadot cts->host_max_data = (sc->aw_mmc_conf->dma_xferlen * 2175d20e651SIlya Bakulin AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 2182671bdb5SEmmanuel Vadot memcpy(&cts->ios, &sc->aw_host.ios, sizeof(struct mmc_ios)); 2195e03278fSIlya Bakulin 2202671bdb5SEmmanuel Vadot return (0); 2215e03278fSIlya Bakulin } 2225e03278fSIlya Bakulin 2235e03278fSIlya Bakulin static int 2242671bdb5SEmmanuel Vadot aw_mmc_set_tran_settings(device_t dev, struct ccb_trans_settings_mmc *cts) 2255e03278fSIlya Bakulin { 2262671bdb5SEmmanuel Vadot struct aw_mmc_softc *sc; 2275e03278fSIlya Bakulin struct mmc_ios *ios; 2285e03278fSIlya Bakulin struct mmc_ios *new_ios; 2295e03278fSIlya Bakulin 2302671bdb5SEmmanuel Vadot sc = device_get_softc(dev); 2315e03278fSIlya Bakulin ios = &sc->aw_host.ios; 2325e03278fSIlya Bakulin new_ios = &cts->ios; 2335e03278fSIlya Bakulin 2345e03278fSIlya Bakulin /* Update only requested fields */ 2355e03278fSIlya Bakulin if (cts->ios_valid & MMC_CLK) { 2365e03278fSIlya Bakulin ios->clock = new_ios->clock; 237020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2385e03278fSIlya Bakulin device_printf(sc->aw_dev, "Clock => %d\n", ios->clock); 2395e03278fSIlya Bakulin } 2405e03278fSIlya Bakulin if (cts->ios_valid & MMC_VDD) { 2415e03278fSIlya Bakulin ios->vdd = new_ios->vdd; 242020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2435e03278fSIlya Bakulin device_printf(sc->aw_dev, "VDD => %d\n", ios->vdd); 2445e03278fSIlya Bakulin } 2455e03278fSIlya Bakulin if (cts->ios_valid & MMC_CS) { 2465e03278fSIlya Bakulin ios->chip_select = new_ios->chip_select; 247020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2485e03278fSIlya Bakulin device_printf(sc->aw_dev, "CS => %d\n", ios->chip_select); 2495e03278fSIlya Bakulin } 2505e03278fSIlya Bakulin if (cts->ios_valid & MMC_BW) { 2515e03278fSIlya Bakulin ios->bus_width = new_ios->bus_width; 252020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2535e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus width => %d\n", ios->bus_width); 2545e03278fSIlya Bakulin } 2555e03278fSIlya Bakulin if (cts->ios_valid & MMC_PM) { 2565e03278fSIlya Bakulin ios->power_mode = new_ios->power_mode; 257020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2585e03278fSIlya Bakulin device_printf(sc->aw_dev, "Power mode => %d\n", ios->power_mode); 2595e03278fSIlya Bakulin } 2605e03278fSIlya Bakulin if (cts->ios_valid & MMC_BT) { 2615e03278fSIlya Bakulin ios->timing = new_ios->timing; 262020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2635e03278fSIlya Bakulin device_printf(sc->aw_dev, "Timing => %d\n", ios->timing); 2645e03278fSIlya Bakulin } 2655e03278fSIlya Bakulin if (cts->ios_valid & MMC_BM) { 2665e03278fSIlya Bakulin ios->bus_mode = new_ios->bus_mode; 267020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_IOS)) 2685e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus mode => %d\n", ios->bus_mode); 2695e03278fSIlya Bakulin } 2705e03278fSIlya Bakulin 2715e03278fSIlya Bakulin return (aw_mmc_update_ios(sc->aw_dev, NULL)); 2725e03278fSIlya Bakulin } 2735e03278fSIlya Bakulin 2745e03278fSIlya Bakulin static int 2752671bdb5SEmmanuel Vadot aw_mmc_cam_request(device_t dev, union ccb *ccb) 2765e03278fSIlya Bakulin { 2772671bdb5SEmmanuel Vadot struct aw_mmc_softc *sc; 2785e03278fSIlya Bakulin struct ccb_mmcio *mmcio; 2795e03278fSIlya Bakulin 2802671bdb5SEmmanuel Vadot sc = device_get_softc(dev); 2815e03278fSIlya Bakulin mmcio = &ccb->mmcio; 2825e03278fSIlya Bakulin 2835e03278fSIlya Bakulin AW_MMC_LOCK(sc); 2845e03278fSIlya Bakulin 285020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) { 2865e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2875e03278fSIlya Bakulin mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2885e03278fSIlya Bakulin mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 2895e03278fSIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0); 2905e03278fSIlya Bakulin } 2915e03278fSIlya Bakulin if (mmcio->cmd.data != NULL) { 2925e03278fSIlya Bakulin if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2935e03278fSIlya Bakulin panic("data->len = %d, data->flags = %d -- something is b0rked", 2945e03278fSIlya Bakulin (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2955e03278fSIlya Bakulin } 2965e03278fSIlya Bakulin if (sc->ccb != NULL) { 2975e03278fSIlya Bakulin device_printf(sc->aw_dev, "Controller still has an active command\n"); 2985e03278fSIlya Bakulin return (EBUSY); 2995e03278fSIlya Bakulin } 3005e03278fSIlya Bakulin sc->ccb = ccb; 3015e03278fSIlya Bakulin /* aw_mmc_request locks again */ 3025e03278fSIlya Bakulin AW_MMC_UNLOCK(sc); 3035e03278fSIlya Bakulin aw_mmc_request(sc->aw_dev, NULL, NULL); 3045e03278fSIlya Bakulin 3055e03278fSIlya Bakulin return (0); 3065e03278fSIlya Bakulin } 3075e03278fSIlya Bakulin #endif /* MMCCAM */ 3085e03278fSIlya Bakulin 3099ed83210SEmmanuel Vadot static void 3109ed83210SEmmanuel Vadot aw_mmc_helper_cd_handler(device_t dev, bool present) 3119ed83210SEmmanuel Vadot { 3129ed83210SEmmanuel Vadot struct aw_mmc_softc *sc; 3139ed83210SEmmanuel Vadot 3149ed83210SEmmanuel Vadot sc = device_get_softc(dev); 3159bca4667SEmmanuel Vadot #ifdef MMCCAM 3162671bdb5SEmmanuel Vadot mmc_cam_sim_discover(&sc->mmc_sim); 3179bca4667SEmmanuel Vadot #else 3189ed83210SEmmanuel Vadot AW_MMC_LOCK(sc); 3199ed83210SEmmanuel Vadot if (present) { 3209ed83210SEmmanuel Vadot if (sc->child == NULL) { 321020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 3229ed83210SEmmanuel Vadot device_printf(sc->aw_dev, "Card inserted\n"); 3239ed83210SEmmanuel Vadot 3249ed83210SEmmanuel Vadot sc->child = device_add_child(sc->aw_dev, "mmc", -1); 3259ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3269ed83210SEmmanuel Vadot if (sc->child) { 3279ed83210SEmmanuel Vadot device_set_ivars(sc->child, sc); 3289ed83210SEmmanuel Vadot (void)device_probe_and_attach(sc->child); 3299ed83210SEmmanuel Vadot } 3309ed83210SEmmanuel Vadot } else 3319ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3329ed83210SEmmanuel Vadot } else { 3339ed83210SEmmanuel Vadot /* Card isn't present, detach if necessary */ 3349ed83210SEmmanuel Vadot if (sc->child != NULL) { 335020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 3369ed83210SEmmanuel Vadot device_printf(sc->aw_dev, "Card removed\n"); 3379ed83210SEmmanuel Vadot 3389ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3399ed83210SEmmanuel Vadot device_delete_child(sc->aw_dev, sc->child); 3409ed83210SEmmanuel Vadot sc->child = NULL; 3419ed83210SEmmanuel Vadot } else 3429ed83210SEmmanuel Vadot AW_MMC_UNLOCK(sc); 3439ed83210SEmmanuel Vadot } 3449bca4667SEmmanuel Vadot #endif /* MMCCAM */ 3459ed83210SEmmanuel Vadot } 3469ed83210SEmmanuel Vadot 347b5be541fSEmmanuel Vadot static int 348b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev) 349b5be541fSEmmanuel Vadot { 350b5be541fSEmmanuel Vadot 351b5be541fSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 352b5be541fSEmmanuel Vadot return (ENXIO); 353b5be541fSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 354b5be541fSEmmanuel Vadot return (ENXIO); 355b5be541fSEmmanuel Vadot 356b5be541fSEmmanuel Vadot device_set_desc(dev, "Allwinner Integrated MMC/SD controller"); 357b5be541fSEmmanuel Vadot 358b5be541fSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 359b5be541fSEmmanuel Vadot } 360b5be541fSEmmanuel Vadot 361b5be541fSEmmanuel Vadot static int 362b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev) 363b5be541fSEmmanuel Vadot { 364b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 365b5be541fSEmmanuel Vadot struct sysctl_ctx_list *ctx; 366b5be541fSEmmanuel Vadot struct sysctl_oid_list *tree; 367b5be541fSEmmanuel Vadot int error; 368b5be541fSEmmanuel Vadot 369b5be541fSEmmanuel Vadot sc = device_get_softc(dev); 370b5be541fSEmmanuel Vadot sc->aw_dev = dev; 371ce0618beSEmmanuel Vadot 372ce0618beSEmmanuel Vadot sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 373ce0618beSEmmanuel Vadot 3745e03278fSIlya Bakulin #ifndef MMCCAM 375b5be541fSEmmanuel Vadot sc->aw_req = NULL; 3765e03278fSIlya Bakulin #endif 377b5be541fSEmmanuel Vadot if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) { 378b5be541fSEmmanuel Vadot device_printf(dev, "cannot allocate device resources\n"); 379b5be541fSEmmanuel Vadot return (ENXIO); 380b5be541fSEmmanuel Vadot } 381b5be541fSEmmanuel Vadot if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES], 3829ed83210SEmmanuel Vadot INTR_TYPE_NET | INTR_MPSAFE, NULL, aw_mmc_intr, sc, 383b5be541fSEmmanuel Vadot &sc->aw_intrhand)) { 384b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 385b5be541fSEmmanuel Vadot device_printf(dev, "cannot setup interrupt handler\n"); 386b5be541fSEmmanuel Vadot return (ENXIO); 387b5be541fSEmmanuel Vadot } 388b5be541fSEmmanuel Vadot mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc", 389b5be541fSEmmanuel Vadot MTX_DEF); 390b5be541fSEmmanuel Vadot callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0); 391b5be541fSEmmanuel Vadot 392b5be541fSEmmanuel Vadot /* De-assert reset */ 393b5be541fSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) { 394b5be541fSEmmanuel Vadot error = hwreset_deassert(sc->aw_rst_ahb); 395b5be541fSEmmanuel Vadot if (error != 0) { 396b5be541fSEmmanuel Vadot device_printf(dev, "cannot de-assert reset\n"); 397b5be541fSEmmanuel Vadot goto fail; 398b5be541fSEmmanuel Vadot } 399b5be541fSEmmanuel Vadot } 400b5be541fSEmmanuel Vadot 401b5be541fSEmmanuel Vadot /* Activate the module clock. */ 402b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb); 403b5be541fSEmmanuel Vadot if (error != 0) { 404b5be541fSEmmanuel Vadot device_printf(dev, "cannot get ahb clock\n"); 405b5be541fSEmmanuel Vadot goto fail; 406b5be541fSEmmanuel Vadot } 407b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_ahb); 408b5be541fSEmmanuel Vadot if (error != 0) { 409b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable ahb clock\n"); 410b5be541fSEmmanuel Vadot goto fail; 411b5be541fSEmmanuel Vadot } 412b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc); 413b5be541fSEmmanuel Vadot if (error != 0) { 414b5be541fSEmmanuel Vadot device_printf(dev, "cannot get mmc clock\n"); 415b5be541fSEmmanuel Vadot goto fail; 416b5be541fSEmmanuel Vadot } 417b5be541fSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY, 418b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 419b5be541fSEmmanuel Vadot if (error != 0) { 420b5be541fSEmmanuel Vadot device_printf(dev, "cannot init mmc clock\n"); 421b5be541fSEmmanuel Vadot goto fail; 422b5be541fSEmmanuel Vadot } 423b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 424b5be541fSEmmanuel Vadot if (error != 0) { 425b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable mmc clock\n"); 426b5be541fSEmmanuel Vadot goto fail; 427b5be541fSEmmanuel Vadot } 428b5be541fSEmmanuel Vadot 429b5be541fSEmmanuel Vadot sc->aw_timeout = 10; 430b5be541fSEmmanuel Vadot ctx = device_get_sysctl_ctx(dev); 431b5be541fSEmmanuel Vadot tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 432b5be541fSEmmanuel Vadot SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW, 433b5be541fSEmmanuel Vadot &sc->aw_timeout, 0, "Request timeout in seconds"); 434b5be541fSEmmanuel Vadot 435b5be541fSEmmanuel Vadot /* Soft Reset controller. */ 436b5be541fSEmmanuel Vadot if (aw_mmc_reset(sc) != 0) { 437b5be541fSEmmanuel Vadot device_printf(dev, "cannot reset the controller\n"); 438b5be541fSEmmanuel Vadot goto fail; 439b5be541fSEmmanuel Vadot } 440b5be541fSEmmanuel Vadot 441b5be541fSEmmanuel Vadot if (aw_mmc_setup_dma(sc) != 0) { 442b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "Couldn't setup DMA!\n"); 443b5be541fSEmmanuel Vadot goto fail; 444b5be541fSEmmanuel Vadot } 445b5be541fSEmmanuel Vadot 4469ed83210SEmmanuel Vadot /* Set some defaults for freq and supported mode */ 447b5be541fSEmmanuel Vadot sc->aw_host.f_min = 400000; 4489ed83210SEmmanuel Vadot sc->aw_host.f_max = 52000000; 449b5be541fSEmmanuel Vadot sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340; 4509ed83210SEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_HSPEED | MMC_CAP_SIGNALING_330; 4519ed83210SEmmanuel Vadot mmc_fdt_parse(dev, 0, &sc->mmc_helper, &sc->aw_host); 4529ed83210SEmmanuel Vadot mmc_fdt_gpio_setup(dev, 0, &sc->mmc_helper, aw_mmc_helper_cd_handler); 453b5be541fSEmmanuel Vadot 4545e03278fSIlya Bakulin #ifdef MMCCAM 4555e03278fSIlya Bakulin sc->ccb = NULL; 4562671bdb5SEmmanuel Vadot 4572671bdb5SEmmanuel Vadot if (mmc_cam_sim_alloc(dev, "aw_mmc", &sc->mmc_sim) != 0) { 4582671bdb5SEmmanuel Vadot device_printf(dev, "cannot alloc cam sim\n"); 4595e03278fSIlya Bakulin goto fail; 4605e03278fSIlya Bakulin } 4615e03278fSIlya Bakulin #endif /* MMCCAM */ 4629ed83210SEmmanuel Vadot 463b5be541fSEmmanuel Vadot return (0); 464b5be541fSEmmanuel Vadot 465b5be541fSEmmanuel Vadot fail: 466b5be541fSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 467b5be541fSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 468b5be541fSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 469b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 470b5be541fSEmmanuel Vadot 471b5be541fSEmmanuel Vadot return (ENXIO); 472b5be541fSEmmanuel Vadot } 473b5be541fSEmmanuel Vadot 474b5be541fSEmmanuel Vadot static int 475b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev) 476b5be541fSEmmanuel Vadot { 477cbba9a7bSEmmanuel Vadot struct aw_mmc_softc *sc; 478cbba9a7bSEmmanuel Vadot device_t d; 479b5be541fSEmmanuel Vadot 480cbba9a7bSEmmanuel Vadot sc = device_get_softc(dev); 481cbba9a7bSEmmanuel Vadot 482cbba9a7bSEmmanuel Vadot clk_disable(sc->aw_clk_mmc); 483cbba9a7bSEmmanuel Vadot clk_disable(sc->aw_clk_ahb); 484cbba9a7bSEmmanuel Vadot hwreset_assert(sc->aw_rst_ahb); 485cbba9a7bSEmmanuel Vadot 486cbba9a7bSEmmanuel Vadot mmc_fdt_gpio_teardown(&sc->mmc_helper); 487cbba9a7bSEmmanuel Vadot 488cbba9a7bSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 489cbba9a7bSEmmanuel Vadot 490cbba9a7bSEmmanuel Vadot AW_MMC_LOCK(sc); 491cbba9a7bSEmmanuel Vadot d = sc->child; 492cbba9a7bSEmmanuel Vadot sc->child = NULL; 493cbba9a7bSEmmanuel Vadot AW_MMC_UNLOCK(sc); 494cbba9a7bSEmmanuel Vadot if (d != NULL) 495cbba9a7bSEmmanuel Vadot device_delete_child(sc->aw_dev, d); 496cbba9a7bSEmmanuel Vadot 497cbba9a7bSEmmanuel Vadot aw_mmc_teardown_dma(sc); 498cbba9a7bSEmmanuel Vadot 499cbba9a7bSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 500cbba9a7bSEmmanuel Vadot 501cbba9a7bSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 502cbba9a7bSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 503cbba9a7bSEmmanuel Vadot 504cbba9a7bSEmmanuel Vadot #ifdef MMCCAM 5052671bdb5SEmmanuel Vadot mmc_cam_sim_free(&sc->mmc_sim); 506cbba9a7bSEmmanuel Vadot #endif 507cbba9a7bSEmmanuel Vadot 508cbba9a7bSEmmanuel Vadot return (0); 509b5be541fSEmmanuel Vadot } 510b5be541fSEmmanuel Vadot 511b5be541fSEmmanuel Vadot static void 512b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 513b5be541fSEmmanuel Vadot { 514b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 515b5be541fSEmmanuel Vadot 516b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 517b5be541fSEmmanuel Vadot if (err) { 518b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 519b5be541fSEmmanuel Vadot return; 520b5be541fSEmmanuel Vadot } 521b5be541fSEmmanuel Vadot sc->aw_dma_desc_phys = segs[0].ds_addr; 522b5be541fSEmmanuel Vadot } 523b5be541fSEmmanuel Vadot 524b5be541fSEmmanuel Vadot static int 525b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc) 526b5be541fSEmmanuel Vadot { 527c39ea909SEmmanuel Vadot int error; 528b5be541fSEmmanuel Vadot 529b5be541fSEmmanuel Vadot /* Allocate the DMA descriptor memory. */ 530c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 531c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 532c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 533c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 534c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 535c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 536c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, 1, /* maxsize, nsegment */ 537c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, /* maxsegsize */ 538c39ea909SEmmanuel Vadot 0, /* flags */ 539c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 540c39ea909SEmmanuel Vadot &sc->aw_dma_tag); 541b5be541fSEmmanuel Vadot if (error) 542b5be541fSEmmanuel Vadot return (error); 543b5be541fSEmmanuel Vadot 544c39ea909SEmmanuel Vadot error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc, 545c39ea909SEmmanuel Vadot BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, 546c39ea909SEmmanuel Vadot &sc->aw_dma_map); 547c39ea909SEmmanuel Vadot if (error) 548c39ea909SEmmanuel Vadot return (error); 549c39ea909SEmmanuel Vadot 550c39ea909SEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_tag, 551c39ea909SEmmanuel Vadot sc->aw_dma_map, 552c39ea909SEmmanuel Vadot sc->aw_dma_desc, AW_MMC_DMA_DESC_SIZE, 553c39ea909SEmmanuel Vadot aw_dma_desc_cb, sc, 0); 554b5be541fSEmmanuel Vadot if (error) 555b5be541fSEmmanuel Vadot return (error); 556b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 557b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 558b5be541fSEmmanuel Vadot 559b5be541fSEmmanuel Vadot /* Create the DMA map for data transfers. */ 560c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 561c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 562c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 563c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 564c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 565c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 566c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen * 567c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS, /* maxsize, nsegments */ 568c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen, /* maxsegsize */ 569c39ea909SEmmanuel Vadot BUS_DMA_ALLOCNOW, /* flags */ 570c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 571b5be541fSEmmanuel Vadot &sc->aw_dma_buf_tag); 572b5be541fSEmmanuel Vadot if (error) 573b5be541fSEmmanuel Vadot return (error); 574b5be541fSEmmanuel Vadot error = bus_dmamap_create(sc->aw_dma_buf_tag, 0, 575b5be541fSEmmanuel Vadot &sc->aw_dma_buf_map); 576b5be541fSEmmanuel Vadot if (error) 577b5be541fSEmmanuel Vadot return (error); 578b5be541fSEmmanuel Vadot 579b5be541fSEmmanuel Vadot return (0); 580b5be541fSEmmanuel Vadot } 581b5be541fSEmmanuel Vadot 582b5be541fSEmmanuel Vadot static void 583cbba9a7bSEmmanuel Vadot aw_mmc_teardown_dma(struct aw_mmc_softc *sc) 584cbba9a7bSEmmanuel Vadot { 585cbba9a7bSEmmanuel Vadot 586cbba9a7bSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_tag, sc->aw_dma_map); 587cbba9a7bSEmmanuel Vadot bus_dmamem_free(sc->aw_dma_tag, sc->aw_dma_desc, sc->aw_dma_map); 588cbba9a7bSEmmanuel Vadot if (bus_dma_tag_destroy(sc->aw_dma_tag) != 0) 589cbba9a7bSEmmanuel Vadot device_printf(sc->aw_dev, "Cannot destroy the dma tag\n"); 590cbba9a7bSEmmanuel Vadot 591cbba9a7bSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 592cbba9a7bSEmmanuel Vadot bus_dmamap_destroy(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 593cbba9a7bSEmmanuel Vadot if (bus_dma_tag_destroy(sc->aw_dma_buf_tag) != 0) 594cbba9a7bSEmmanuel Vadot device_printf(sc->aw_dev, "Cannot destroy the dma buf tag\n"); 595cbba9a7bSEmmanuel Vadot } 596cbba9a7bSEmmanuel Vadot 597cbba9a7bSEmmanuel Vadot static void 598b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 599b5be541fSEmmanuel Vadot { 600b5be541fSEmmanuel Vadot int i; 601b5be541fSEmmanuel Vadot struct aw_mmc_dma_desc *dma_desc; 602b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 603b5be541fSEmmanuel Vadot 604b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 605b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 606b5be541fSEmmanuel Vadot 607b5be541fSEmmanuel Vadot if (err) 608b5be541fSEmmanuel Vadot return; 609b5be541fSEmmanuel Vadot 610b5be541fSEmmanuel Vadot dma_desc = sc->aw_dma_desc; 611b5be541fSEmmanuel Vadot for (i = 0; i < nsegs; i++) { 612c39ea909SEmmanuel Vadot if (segs[i].ds_len == sc->aw_mmc_conf->dma_xferlen) 613c39ea909SEmmanuel Vadot dma_desc[i].buf_size = 0; /* Size of 0 indicate max len */ 614c39ea909SEmmanuel Vadot else 615b5be541fSEmmanuel Vadot dma_desc[i].buf_size = segs[i].ds_len; 616b5be541fSEmmanuel Vadot dma_desc[i].buf_addr = segs[i].ds_addr; 617b5be541fSEmmanuel Vadot dma_desc[i].config = AW_MMC_DMA_CONFIG_CH | 618c39ea909SEmmanuel Vadot AW_MMC_DMA_CONFIG_OWN | AW_MMC_DMA_CONFIG_DIC; 619c39ea909SEmmanuel Vadot 620b5be541fSEmmanuel Vadot dma_desc[i].next = sc->aw_dma_desc_phys + 621b5be541fSEmmanuel Vadot ((i + 1) * sizeof(struct aw_mmc_dma_desc)); 622c39ea909SEmmanuel Vadot } 623c39ea909SEmmanuel Vadot 624c39ea909SEmmanuel Vadot dma_desc[0].config |= AW_MMC_DMA_CONFIG_FD; 625c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config |= AW_MMC_DMA_CONFIG_LD | 626b5be541fSEmmanuel Vadot AW_MMC_DMA_CONFIG_ER; 627c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config &= ~AW_MMC_DMA_CONFIG_DIC; 628c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].next = 0; 629b5be541fSEmmanuel Vadot } 630b5be541fSEmmanuel Vadot 631b5be541fSEmmanuel Vadot static int 632b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc) 633b5be541fSEmmanuel Vadot { 634b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 635b5be541fSEmmanuel Vadot int error; 636b5be541fSEmmanuel Vadot struct mmc_command *cmd; 637b5be541fSEmmanuel Vadot uint32_t val; 638b5be541fSEmmanuel Vadot 6395e03278fSIlya Bakulin #ifdef MMCCAM 6405e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 6415e03278fSIlya Bakulin #else 642b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 6435e03278fSIlya Bakulin #endif 644ce0618beSEmmanuel Vadot if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS)) 645b5be541fSEmmanuel Vadot return (EFBIG); 646b5be541fSEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 647b5be541fSEmmanuel Vadot cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0); 648b5be541fSEmmanuel Vadot if (error) 649b5be541fSEmmanuel Vadot return (error); 650b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 651b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 652b5be541fSEmmanuel Vadot 653b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 654b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREWRITE; 655b5be541fSEmmanuel Vadot else 656b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREREAD; 657b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op); 658b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE); 659b5be541fSEmmanuel Vadot 660b5be541fSEmmanuel Vadot /* Enable DMA */ 661b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 662b091392eSEmmanuel Vadot val &= ~AW_MMC_GCTL_FIFO_AC_MOD; 663b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_ENB; 664b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 665b5be541fSEmmanuel Vadot 666b5be541fSEmmanuel Vadot /* Reset DMA */ 667b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_RST; 668b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 669b5be541fSEmmanuel Vadot 670b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST); 671b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, 672b5be541fSEmmanuel Vadot AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST); 673b5be541fSEmmanuel Vadot 674b5be541fSEmmanuel Vadot /* Enable RX or TX DMA interrupt */ 675a37d59c1SEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_IDIE); 676b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 677b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_TX_INT; 678b5be541fSEmmanuel Vadot else 679b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_RX_INT; 680b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val); 681b5be541fSEmmanuel Vadot 682b5be541fSEmmanuel Vadot /* Set DMA descritptor list address */ 683b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys); 684b5be541fSEmmanuel Vadot 685b5be541fSEmmanuel Vadot /* FIFO trigger level */ 686b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL); 687b5be541fSEmmanuel Vadot 688b5be541fSEmmanuel Vadot return (0); 689b5be541fSEmmanuel Vadot } 690b5be541fSEmmanuel Vadot 691b5be541fSEmmanuel Vadot static int 692b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc) 693b5be541fSEmmanuel Vadot { 694b091392eSEmmanuel Vadot uint32_t reg; 695b5be541fSEmmanuel Vadot int timeout; 696b5be541fSEmmanuel Vadot 697b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 698b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_RESET; 699b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 700c39ea909SEmmanuel Vadot timeout = AW_MMC_RESET_RETRY; 701b5be541fSEmmanuel Vadot while (--timeout > 0) { 702b091392eSEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_GCTL_RESET) == 0) 703b5be541fSEmmanuel Vadot break; 704b5be541fSEmmanuel Vadot DELAY(100); 705b5be541fSEmmanuel Vadot } 706b5be541fSEmmanuel Vadot if (timeout == 0) 707b5be541fSEmmanuel Vadot return (ETIMEDOUT); 708b5be541fSEmmanuel Vadot 70935a18619SEmmanuel Vadot return (0); 71035a18619SEmmanuel Vadot } 71135a18619SEmmanuel Vadot 71235a18619SEmmanuel Vadot static int 71335a18619SEmmanuel Vadot aw_mmc_init(struct aw_mmc_softc *sc) 71435a18619SEmmanuel Vadot { 715b091392eSEmmanuel Vadot uint32_t reg; 71635a18619SEmmanuel Vadot int ret; 71735a18619SEmmanuel Vadot 71835a18619SEmmanuel Vadot ret = aw_mmc_reset(sc); 71935a18619SEmmanuel Vadot if (ret != 0) 72035a18619SEmmanuel Vadot return (ret); 72135a18619SEmmanuel Vadot 722b5be541fSEmmanuel Vadot /* Set the timeout. */ 723b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_TMOR, 724b5be541fSEmmanuel Vadot AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) | 725b5be541fSEmmanuel Vadot AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK)); 726b5be541fSEmmanuel Vadot 72735a18619SEmmanuel Vadot /* Unmask interrupts. */ 72835a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, 0); 72935a18619SEmmanuel Vadot 730b5be541fSEmmanuel Vadot /* Clear pending interrupts. */ 731b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 73235a18619SEmmanuel Vadot 73335a18619SEmmanuel Vadot /* Debug register, undocumented */ 73435a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DBGC, 0xdeb); 73535a18619SEmmanuel Vadot 73635a18619SEmmanuel Vadot /* Function select register */ 73735a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FUNS, 0xceaa0000); 73835a18619SEmmanuel Vadot 739b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff); 74035a18619SEmmanuel Vadot 741b091392eSEmmanuel Vadot /* Enable interrupts and disable AHB access. */ 742b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 743b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_INT_ENB; 744b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_FIFO_AC_MOD; 745b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_WAIT_MEM_ACCESS; 746b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 747b5be541fSEmmanuel Vadot 748b5be541fSEmmanuel Vadot return (0); 749b5be541fSEmmanuel Vadot } 750b5be541fSEmmanuel Vadot 751b5be541fSEmmanuel Vadot static void 752b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc) 753b5be541fSEmmanuel Vadot { 754b5be541fSEmmanuel Vadot struct mmc_command *cmd; 7555e03278fSIlya Bakulin #ifdef MMCCAM 7565e03278fSIlya Bakulin union ccb *ccb; 7575e03278fSIlya Bakulin #else 758b5be541fSEmmanuel Vadot struct mmc_request *req; 7595e03278fSIlya Bakulin #endif 760b5be541fSEmmanuel Vadot uint32_t val, mask; 761b5be541fSEmmanuel Vadot int retry; 762b5be541fSEmmanuel Vadot 7635e03278fSIlya Bakulin #ifdef MMCCAM 7645e03278fSIlya Bakulin ccb = sc->ccb; 7655e03278fSIlya Bakulin cmd = &ccb->mmcio.cmd; 7665e03278fSIlya Bakulin #else 767b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 7685e03278fSIlya Bakulin #endif 769020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) { 7705e03278fSIlya Bakulin device_printf(sc->aw_dev, "%s: cmd %d err %d\n", __func__, cmd->opcode, cmd->error); 7715e03278fSIlya Bakulin } 772b5be541fSEmmanuel Vadot if (cmd->error != MMC_ERR_NONE) { 773b5be541fSEmmanuel Vadot /* Reset the FIFO and DMA engines. */ 774b091392eSEmmanuel Vadot mask = AW_MMC_GCTL_FIFO_RST | AW_MMC_GCTL_DMA_RST; 775b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 776b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask); 777b5be541fSEmmanuel Vadot 778b5be541fSEmmanuel Vadot retry = AW_MMC_RESET_RETRY; 779b5be541fSEmmanuel Vadot while (--retry > 0) { 780c39ea909SEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & 781c39ea909SEmmanuel Vadot AW_MMC_GCTL_RESET) == 0) 782b5be541fSEmmanuel Vadot break; 783c39ea909SEmmanuel Vadot DELAY(100); 784b5be541fSEmmanuel Vadot } 785b5be541fSEmmanuel Vadot if (retry == 0) 786b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 787b5be541fSEmmanuel Vadot "timeout resetting DMA/FIFO\n"); 788b5be541fSEmmanuel Vadot aw_mmc_update_clock(sc, 1); 789b5be541fSEmmanuel Vadot } 790b5be541fSEmmanuel Vadot 791b5be541fSEmmanuel Vadot callout_stop(&sc->aw_timeoutc); 792b5be541fSEmmanuel Vadot sc->aw_intr = 0; 793b5be541fSEmmanuel Vadot sc->aw_resid = 0; 794b5be541fSEmmanuel Vadot sc->aw_dma_map_err = 0; 795b5be541fSEmmanuel Vadot sc->aw_intr_wait = 0; 7965e03278fSIlya Bakulin #ifdef MMCCAM 7975e03278fSIlya Bakulin sc->ccb = NULL; 7985e03278fSIlya Bakulin ccb->ccb_h.status = 7995e03278fSIlya Bakulin (ccb->mmcio.cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 8005e03278fSIlya Bakulin xpt_done(ccb); 8015e03278fSIlya Bakulin #else 8025e03278fSIlya Bakulin req = sc->aw_req; 8035e03278fSIlya Bakulin sc->aw_req = NULL; 804b5be541fSEmmanuel Vadot req->done(req); 8055e03278fSIlya Bakulin #endif 806b5be541fSEmmanuel Vadot } 807b5be541fSEmmanuel Vadot 808b5be541fSEmmanuel Vadot static void 809b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc) 810b5be541fSEmmanuel Vadot { 811b5be541fSEmmanuel Vadot int timeout; 812b5be541fSEmmanuel Vadot struct mmc_command *cmd; 813b5be541fSEmmanuel Vadot uint32_t status; 814b5be541fSEmmanuel Vadot 815b5be541fSEmmanuel Vadot timeout = 1000; 816b5be541fSEmmanuel Vadot while (--timeout > 0) { 817b5be541fSEmmanuel Vadot status = AW_MMC_READ_4(sc, AW_MMC_STAR); 818b5be541fSEmmanuel Vadot if ((status & AW_MMC_STAR_CARD_BUSY) == 0) 819b5be541fSEmmanuel Vadot break; 820b5be541fSEmmanuel Vadot DELAY(1000); 821b5be541fSEmmanuel Vadot } 8225e03278fSIlya Bakulin #ifdef MMCCAM 8235e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 8245e03278fSIlya Bakulin #else 825b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 8265e03278fSIlya Bakulin #endif 827b5be541fSEmmanuel Vadot if (timeout == 0) { 828b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 829b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 830b5be541fSEmmanuel Vadot return; 831b5be541fSEmmanuel Vadot } 832b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) { 833b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) { 834b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3); 835b5be541fSEmmanuel Vadot cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2); 836b5be541fSEmmanuel Vadot cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1); 837b5be541fSEmmanuel Vadot cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 838b5be541fSEmmanuel Vadot } else 839b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 840b5be541fSEmmanuel Vadot } 841b5be541fSEmmanuel Vadot /* All data has been transferred ? */ 842b5be541fSEmmanuel Vadot if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len) 843b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 844b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 845b5be541fSEmmanuel Vadot } 846b5be541fSEmmanuel Vadot 8475e03278fSIlya Bakulin static inline void 8485e03278fSIlya Bakulin set_mmc_error(struct aw_mmc_softc *sc, int error_code) 8495e03278fSIlya Bakulin { 8505e03278fSIlya Bakulin #ifdef MMCCAM 8515e03278fSIlya Bakulin sc->ccb->mmcio.cmd.error = error_code; 8525e03278fSIlya Bakulin #else 8535e03278fSIlya Bakulin sc->aw_req->cmd->error = error_code; 8545e03278fSIlya Bakulin #endif 8555e03278fSIlya Bakulin } 8565e03278fSIlya Bakulin 857b5be541fSEmmanuel Vadot static void 858b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg) 859b5be541fSEmmanuel Vadot { 860b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 861b5be541fSEmmanuel Vadot 862b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 8635e03278fSIlya Bakulin #ifdef MMCCAM 8645e03278fSIlya Bakulin if (sc->ccb != NULL) { 8655e03278fSIlya Bakulin #else 866b5be541fSEmmanuel Vadot if (sc->aw_req != NULL) { 8675e03278fSIlya Bakulin #endif 868b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "controller timeout\n"); 8695e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 870b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 871b5be541fSEmmanuel Vadot } else 872b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 873b5be541fSEmmanuel Vadot "Spurious timeout - no active request\n"); 874b5be541fSEmmanuel Vadot } 875b5be541fSEmmanuel Vadot 876b5be541fSEmmanuel Vadot static void 8775e03278fSIlya Bakulin aw_mmc_print_error(uint32_t err) 8785e03278fSIlya Bakulin { 8795e03278fSIlya Bakulin if(err & AW_MMC_INT_RESP_ERR) 8805e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_ERR "); 8815e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_CRC_ERR) 8825e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_CRC_ERR "); 8835e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_CRC_ERR) 8845e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_CRC_ERR "); 8855e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_TIMEOUT) 8865e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_TIMEOUT "); 8875e03278fSIlya Bakulin if (err & AW_MMC_INT_FIFO_RUN_ERR) 8885e03278fSIlya Bakulin printf("AW_MMC_INT_FIFO_RUN_ERR "); 8895e03278fSIlya Bakulin if (err & AW_MMC_INT_CMD_BUSY) 8905e03278fSIlya Bakulin printf("AW_MMC_INT_CMD_BUSY "); 8915e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_START_ERR) 8925e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_START_ERR "); 8935e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_END_BIT_ERR) 8945e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_END_BIT_ERR"); 8955e03278fSIlya Bakulin printf("\n"); 8965e03278fSIlya Bakulin } 8975e03278fSIlya Bakulin 8985e03278fSIlya Bakulin static void 899b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg) 900b5be541fSEmmanuel Vadot { 901b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 902b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 903b5be541fSEmmanuel Vadot struct mmc_data *data; 904b5be541fSEmmanuel Vadot uint32_t idst, imask, rint; 905b5be541fSEmmanuel Vadot 906b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 907b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 908b5be541fSEmmanuel Vadot rint = AW_MMC_READ_4(sc, AW_MMC_RISR); 909b5be541fSEmmanuel Vadot idst = AW_MMC_READ_4(sc, AW_MMC_IDST); 910b5be541fSEmmanuel Vadot imask = AW_MMC_READ_4(sc, AW_MMC_IMKR); 911b5be541fSEmmanuel Vadot if (idst == 0 && imask == 0 && rint == 0) { 912b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 913b5be541fSEmmanuel Vadot return; 914b5be541fSEmmanuel Vadot } 915020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) { 916b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n", 917b5be541fSEmmanuel Vadot idst, imask, rint); 918020df509SEmmanuel Vadot } 9195e03278fSIlya Bakulin #ifdef MMCCAM 9205e03278fSIlya Bakulin if (sc->ccb == NULL) { 9215e03278fSIlya Bakulin #else 922b5be541fSEmmanuel Vadot if (sc->aw_req == NULL) { 9235e03278fSIlya Bakulin #endif 924b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 925b5be541fSEmmanuel Vadot "Spurious interrupt - no active request, rint: 0x%08X\n", 926b5be541fSEmmanuel Vadot rint); 9275e03278fSIlya Bakulin aw_mmc_print_error(rint); 928b5be541fSEmmanuel Vadot goto end; 929b5be541fSEmmanuel Vadot } 930b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_ERR_BIT) { 931020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) { 932b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint); 9335e03278fSIlya Bakulin aw_mmc_print_error(rint); 934020df509SEmmanuel Vadot } 935b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_RESP_TIMEOUT) 9365e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 937b5be541fSEmmanuel Vadot else 9385e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 939b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 940b5be541fSEmmanuel Vadot goto end; 941b5be541fSEmmanuel Vadot } 942b5be541fSEmmanuel Vadot if (idst & AW_MMC_IDST_ERROR) { 943020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_INT)) 944b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst); 9455e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 946b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 947b5be541fSEmmanuel Vadot goto end; 948b5be541fSEmmanuel Vadot } 949b5be541fSEmmanuel Vadot 950b5be541fSEmmanuel Vadot sc->aw_intr |= rint; 9515e03278fSIlya Bakulin #ifdef MMCCAM 9525e03278fSIlya Bakulin data = sc->ccb->mmcio.cmd.data; 9535e03278fSIlya Bakulin #else 954b5be541fSEmmanuel Vadot data = sc->aw_req->cmd->data; 9555e03278fSIlya Bakulin #endif 956b5be541fSEmmanuel Vadot if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) { 957b5be541fSEmmanuel Vadot if (data->flags & MMC_DATA_WRITE) 958b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTWRITE; 959b5be541fSEmmanuel Vadot else 960b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTREAD; 961b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 962b5be541fSEmmanuel Vadot sync_op); 963b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, 964b5be541fSEmmanuel Vadot BUS_DMASYNC_POSTWRITE); 965b5be541fSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 966b5be541fSEmmanuel Vadot sc->aw_resid = data->len >> 2; 967b5be541fSEmmanuel Vadot } 968b5be541fSEmmanuel Vadot if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait) 969b5be541fSEmmanuel Vadot aw_mmc_req_ok(sc); 970b5be541fSEmmanuel Vadot 971b5be541fSEmmanuel Vadot end: 972b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst); 973b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint); 974b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 975b5be541fSEmmanuel Vadot } 976b5be541fSEmmanuel Vadot 977b5be541fSEmmanuel Vadot static int 978b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req) 979b5be541fSEmmanuel Vadot { 980b5be541fSEmmanuel Vadot int blksz; 981b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 982b5be541fSEmmanuel Vadot struct mmc_command *cmd; 98335a18619SEmmanuel Vadot uint32_t cmdreg, imask; 984b5be541fSEmmanuel Vadot int err; 985b5be541fSEmmanuel Vadot 986b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 987c39ea909SEmmanuel Vadot 988b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 9895e03278fSIlya Bakulin #ifdef MMCCAM 9905e03278fSIlya Bakulin KASSERT(req == NULL, ("req should be NULL in MMCCAM case!")); 9915e03278fSIlya Bakulin /* 9925e03278fSIlya Bakulin * For MMCCAM, sc->ccb has been NULL-checked and populated 9935e03278fSIlya Bakulin * by aw_mmc_cam_request() already. 9945e03278fSIlya Bakulin */ 9955e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 9965e03278fSIlya Bakulin #else 997b5be541fSEmmanuel Vadot if (sc->aw_req) { 998b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 999b5be541fSEmmanuel Vadot return (EBUSY); 1000b5be541fSEmmanuel Vadot } 1001b5be541fSEmmanuel Vadot sc->aw_req = req; 1002b5be541fSEmmanuel Vadot cmd = req->cmd; 10035e03278fSIlya Bakulin 1004020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CMD)) { 10055e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 10065e03278fSIlya Bakulin cmd->opcode, cmd->arg, cmd->flags, 10075e03278fSIlya Bakulin cmd->data != NULL ? (unsigned int)cmd->data->len : 0, 10085e03278fSIlya Bakulin cmd->data != NULL ? cmd->data->flags: 0); 1009020df509SEmmanuel Vadot } 10105e03278fSIlya Bakulin #endif 1011b5be541fSEmmanuel Vadot cmdreg = AW_MMC_CMDR_LOAD; 101235a18619SEmmanuel Vadot imask = AW_MMC_INT_ERR_BIT; 101335a18619SEmmanuel Vadot sc->aw_intr_wait = 0; 101435a18619SEmmanuel Vadot sc->aw_intr = 0; 101535a18619SEmmanuel Vadot sc->aw_resid = 0; 101635a18619SEmmanuel Vadot cmd->error = MMC_ERR_NONE; 101735a18619SEmmanuel Vadot 1018b5be541fSEmmanuel Vadot if (cmd->opcode == MMC_GO_IDLE_STATE) 1019b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ; 102035a18619SEmmanuel Vadot 1021b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) 1022b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_RESP_RCV; 1023b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) 1024b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_LONG_RESP; 1025b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_CRC) 1026b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC; 1027b5be541fSEmmanuel Vadot 102835a18619SEmmanuel Vadot if (cmd->data) { 1029b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER; 103035a18619SEmmanuel Vadot 1031b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_MULTI) { 1032b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG; 103335a18619SEmmanuel Vadot imask |= AW_MMC_INT_AUTO_STOP_DONE; 1034b5be541fSEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE; 103535a18619SEmmanuel Vadot } else { 103635a18619SEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER; 103735a18619SEmmanuel Vadot imask |= AW_MMC_INT_DATA_OVER; 1038b5be541fSEmmanuel Vadot } 1039b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 1040b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DIR_WRITE; 10415d5ae066SIlya Bakulin #ifdef MMCCAM 10425d5ae066SIlya Bakulin if (cmd->data->flags & MMC_DATA_BLOCK_SIZE) { 10435d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BKSR, cmd->data->block_size); 10445d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 10455d5ae066SIlya Bakulin } else 10465d5ae066SIlya Bakulin #endif 10475d5ae066SIlya Bakulin { 1048440565daSBjoern A. Zeeb blksz = min(cmd->data->len, MMC_SECTOR_SIZE); 1049b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz); 1050b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 10515d5ae066SIlya Bakulin } 105235a18619SEmmanuel Vadot } else { 105335a18619SEmmanuel Vadot imask |= AW_MMC_INT_CMD_DONE; 105435a18619SEmmanuel Vadot } 1055b5be541fSEmmanuel Vadot 105635a18619SEmmanuel Vadot /* Enable the interrupts we are interested in */ 105735a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, imask); 105835a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 105935a18619SEmmanuel Vadot 106035a18619SEmmanuel Vadot /* Enable auto stop if needed */ 106135a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_A12A, 106235a18619SEmmanuel Vadot cmdreg & AW_MMC_CMDR_STOP_CMD_FLAG ? 0 : 0xffff); 106335a18619SEmmanuel Vadot 106435a18619SEmmanuel Vadot /* Write the command argument */ 106535a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg); 106635a18619SEmmanuel Vadot 106735a18619SEmmanuel Vadot /* 106835a18619SEmmanuel Vadot * If we don't have data start the request 106935a18619SEmmanuel Vadot * if we do prepare the dma request and start the request 107035a18619SEmmanuel Vadot */ 107135a18619SEmmanuel Vadot if (cmd->data == NULL) { 107235a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 107335a18619SEmmanuel Vadot } else { 1074b5be541fSEmmanuel Vadot err = aw_mmc_prepare_dma(sc); 1075b5be541fSEmmanuel Vadot if (err != 0) 1076b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err); 107735a18619SEmmanuel Vadot 107835a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 1079b5be541fSEmmanuel Vadot } 1080b5be541fSEmmanuel Vadot 1081b5be541fSEmmanuel Vadot callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz, 1082b5be541fSEmmanuel Vadot aw_mmc_timeout, sc); 1083b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1084b5be541fSEmmanuel Vadot 1085b5be541fSEmmanuel Vadot return (0); 1086b5be541fSEmmanuel Vadot } 1087b5be541fSEmmanuel Vadot 1088b5be541fSEmmanuel Vadot static int 1089b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which, 1090b5be541fSEmmanuel Vadot uintptr_t *result) 1091b5be541fSEmmanuel Vadot { 1092b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1093b5be541fSEmmanuel Vadot 1094b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1095b5be541fSEmmanuel Vadot switch (which) { 1096b5be541fSEmmanuel Vadot default: 1097b5be541fSEmmanuel Vadot return (EINVAL); 1098b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1099b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_mode; 1100b5be541fSEmmanuel Vadot break; 1101b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1102b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_width; 1103b5be541fSEmmanuel Vadot break; 1104b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1105b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.chip_select; 1106b5be541fSEmmanuel Vadot break; 1107b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1108b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.clock; 1109b5be541fSEmmanuel Vadot break; 1110b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1111b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_min; 1112b5be541fSEmmanuel Vadot break; 1113b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1114b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_max; 1115b5be541fSEmmanuel Vadot break; 1116b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1117b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.host_ocr; 1118b5be541fSEmmanuel Vadot break; 1119b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1120b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.mode; 1121b5be541fSEmmanuel Vadot break; 1122b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1123b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ocr; 1124b5be541fSEmmanuel Vadot break; 1125b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1126b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.power_mode; 1127b5be541fSEmmanuel Vadot break; 1128b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1129b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.vdd; 1130b5be541fSEmmanuel Vadot break; 1131dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1132dfb8c122SEmmanuel Vadot *(int *)result = sc->aw_host.ios.vccq; 1133dfb8c122SEmmanuel Vadot break; 1134b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1135b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.caps; 1136b5be541fSEmmanuel Vadot break; 1137ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1138ce0618beSEmmanuel Vadot *(int *)result = sc->aw_host.ios.timing; 1139ce0618beSEmmanuel Vadot break; 1140b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1141c39ea909SEmmanuel Vadot *(int *)result = (sc->aw_mmc_conf->dma_xferlen * 1142c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 1143b5be541fSEmmanuel Vadot break; 114455f3f71cSEmmanuel Vadot case MMCBR_IVAR_RETUNE_REQ: 114555f3f71cSEmmanuel Vadot *(int *)result = retune_req_none; 114655f3f71cSEmmanuel Vadot break; 1147b5be541fSEmmanuel Vadot } 1148b5be541fSEmmanuel Vadot 1149b5be541fSEmmanuel Vadot return (0); 1150b5be541fSEmmanuel Vadot } 1151b5be541fSEmmanuel Vadot 1152b5be541fSEmmanuel Vadot static int 1153b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which, 1154b5be541fSEmmanuel Vadot uintptr_t value) 1155b5be541fSEmmanuel Vadot { 1156b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1157b5be541fSEmmanuel Vadot 1158b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1159b5be541fSEmmanuel Vadot switch (which) { 1160b5be541fSEmmanuel Vadot default: 1161b5be541fSEmmanuel Vadot return (EINVAL); 1162b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1163b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_mode = value; 1164b5be541fSEmmanuel Vadot break; 1165b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1166b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_width = value; 1167b5be541fSEmmanuel Vadot break; 1168b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1169b5be541fSEmmanuel Vadot sc->aw_host.ios.chip_select = value; 1170b5be541fSEmmanuel Vadot break; 1171b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1172b5be541fSEmmanuel Vadot sc->aw_host.ios.clock = value; 1173b5be541fSEmmanuel Vadot break; 1174b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1175b5be541fSEmmanuel Vadot sc->aw_host.mode = value; 1176b5be541fSEmmanuel Vadot break; 1177b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1178b5be541fSEmmanuel Vadot sc->aw_host.ocr = value; 1179b5be541fSEmmanuel Vadot break; 1180b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1181b5be541fSEmmanuel Vadot sc->aw_host.ios.power_mode = value; 1182b5be541fSEmmanuel Vadot break; 1183b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1184b5be541fSEmmanuel Vadot sc->aw_host.ios.vdd = value; 1185b5be541fSEmmanuel Vadot break; 1186dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1187dfb8c122SEmmanuel Vadot sc->aw_host.ios.vccq = value; 1188dfb8c122SEmmanuel Vadot break; 1189ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1190ce0618beSEmmanuel Vadot sc->aw_host.ios.timing = value; 1191ce0618beSEmmanuel Vadot break; 1192b5be541fSEmmanuel Vadot /* These are read-only */ 1193b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1194b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1195b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1196b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1197b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1198b5be541fSEmmanuel Vadot return (EINVAL); 1199b5be541fSEmmanuel Vadot } 1200b5be541fSEmmanuel Vadot 1201b5be541fSEmmanuel Vadot return (0); 1202b5be541fSEmmanuel Vadot } 1203b5be541fSEmmanuel Vadot 1204b5be541fSEmmanuel Vadot static int 1205b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon) 1206b5be541fSEmmanuel Vadot { 1207ce0618beSEmmanuel Vadot uint32_t reg; 1208b5be541fSEmmanuel Vadot int retry; 1209b5be541fSEmmanuel Vadot 1210ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1211ffdb1aa8SEmmanuel Vadot reg &= ~(AW_MMC_CKCR_ENB | AW_MMC_CKCR_LOW_POWER | 1212ffdb1aa8SEmmanuel Vadot AW_MMC_CKCR_MASK_DATA0); 1213b5be541fSEmmanuel Vadot 1214b5be541fSEmmanuel Vadot if (clkon) 1215ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_ENB; 1216ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) 1217ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_MASK_DATA0; 1218b5be541fSEmmanuel Vadot 1219ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1220b5be541fSEmmanuel Vadot 1221ce0618beSEmmanuel Vadot reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK | 1222b5be541fSEmmanuel Vadot AW_MMC_CMDR_WAIT_PRE_OVER; 1223ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg); 1224b5be541fSEmmanuel Vadot retry = 0xfffff; 1225ce0618beSEmmanuel Vadot 1226ce0618beSEmmanuel Vadot while (reg & AW_MMC_CMDR_LOAD && --retry > 0) { 1227ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CMDR); 1228b5be541fSEmmanuel Vadot DELAY(10); 1229b5be541fSEmmanuel Vadot } 1230b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 1231b5be541fSEmmanuel Vadot 1232ce0618beSEmmanuel Vadot if (reg & AW_MMC_CMDR_LOAD) { 1233ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "timeout updating clock\n"); 1234b5be541fSEmmanuel Vadot return (ETIMEDOUT); 1235b5be541fSEmmanuel Vadot } 1236b5be541fSEmmanuel Vadot 1237ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) { 1238ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1239ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_MASK_DATA0; 1240ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1241ce0618beSEmmanuel Vadot } 1242ce0618beSEmmanuel Vadot 1243ce0618beSEmmanuel Vadot return (0); 1244ce0618beSEmmanuel Vadot } 1245ce0618beSEmmanuel Vadot 12462671bdb5SEmmanuel Vadot #ifndef MMCCAM 1247623966e1SEmmanuel Vadot static int 1248623966e1SEmmanuel Vadot aw_mmc_switch_vccq(device_t bus, device_t child) 1249ce0618beSEmmanuel Vadot { 1250623966e1SEmmanuel Vadot struct aw_mmc_softc *sc; 1251623966e1SEmmanuel Vadot int uvolt, err; 1252623966e1SEmmanuel Vadot 1253623966e1SEmmanuel Vadot sc = device_get_softc(bus); 1254ce0618beSEmmanuel Vadot 12559ed83210SEmmanuel Vadot if (sc->mmc_helper.vqmmc_supply == NULL) 1256623966e1SEmmanuel Vadot return EOPNOTSUPP; 1257ce0618beSEmmanuel Vadot 1258623966e1SEmmanuel Vadot switch (sc->aw_host.ios.vccq) { 1259dfb8c122SEmmanuel Vadot case vccq_180: 1260dfb8c122SEmmanuel Vadot uvolt = 1800000; 1261ce0618beSEmmanuel Vadot break; 1262dfb8c122SEmmanuel Vadot case vccq_330: 1263dfb8c122SEmmanuel Vadot uvolt = 3300000; 1264ce0618beSEmmanuel Vadot break; 1265dfb8c122SEmmanuel Vadot default: 1266623966e1SEmmanuel Vadot return EINVAL; 1267ce0618beSEmmanuel Vadot } 1268ce0618beSEmmanuel Vadot 12699ed83210SEmmanuel Vadot err = regulator_set_voltage(sc->mmc_helper.vqmmc_supply, uvolt, uvolt); 1270623966e1SEmmanuel Vadot if (err != 0) { 1271ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, 1272ce0618beSEmmanuel Vadot "Cannot set vqmmc to %d<->%d\n", 1273dfb8c122SEmmanuel Vadot uvolt, 1274dfb8c122SEmmanuel Vadot uvolt); 1275623966e1SEmmanuel Vadot return (err); 1276623966e1SEmmanuel Vadot } 1277623966e1SEmmanuel Vadot 1278623966e1SEmmanuel Vadot return (0); 1279ce0618beSEmmanuel Vadot } 12802671bdb5SEmmanuel Vadot #endif 1281ce0618beSEmmanuel Vadot 1282b5be541fSEmmanuel Vadot static int 1283b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child) 1284b5be541fSEmmanuel Vadot { 1285b5be541fSEmmanuel Vadot int error; 1286b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1287b5be541fSEmmanuel Vadot struct mmc_ios *ios; 1288ce0618beSEmmanuel Vadot unsigned int clock; 1289ce0618beSEmmanuel Vadot uint32_t reg, div = 1; 1290115e71a4SEmmanuel Vadot int reg_status; 1291115e71a4SEmmanuel Vadot int rv; 1292b5be541fSEmmanuel Vadot 1293b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1294b5be541fSEmmanuel Vadot 1295b5be541fSEmmanuel Vadot ios = &sc->aw_host.ios; 1296b5be541fSEmmanuel Vadot 1297b5be541fSEmmanuel Vadot /* Set the bus width. */ 1298b5be541fSEmmanuel Vadot switch (ios->bus_width) { 1299b5be541fSEmmanuel Vadot case bus_width_1: 1300b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1); 1301b5be541fSEmmanuel Vadot break; 1302b5be541fSEmmanuel Vadot case bus_width_4: 1303b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4); 1304b5be541fSEmmanuel Vadot break; 1305b5be541fSEmmanuel Vadot case bus_width_8: 1306b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8); 1307b5be541fSEmmanuel Vadot break; 1308b5be541fSEmmanuel Vadot } 1309b5be541fSEmmanuel Vadot 131035a18619SEmmanuel Vadot switch (ios->power_mode) { 131135a18619SEmmanuel Vadot case power_on: 131235a18619SEmmanuel Vadot break; 131335a18619SEmmanuel Vadot case power_off: 1314020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 1315ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "Powering down sd/mmc\n"); 1316dfb8c122SEmmanuel Vadot 1317115e71a4SEmmanuel Vadot if (sc->mmc_helper.vmmc_supply) { 1318115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vmmc_supply, ®_status); 1319115e71a4SEmmanuel Vadot if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 13209ed83210SEmmanuel Vadot regulator_disable(sc->mmc_helper.vmmc_supply); 1321115e71a4SEmmanuel Vadot } 1322115e71a4SEmmanuel Vadot if (sc->mmc_helper.vqmmc_supply) { 1323115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vqmmc_supply, ®_status); 1324115e71a4SEmmanuel Vadot if (rv == 0 && reg_status == REGULATOR_STATUS_ENABLED) 13259ed83210SEmmanuel Vadot regulator_disable(sc->mmc_helper.vqmmc_supply); 1326115e71a4SEmmanuel Vadot } 1327dfb8c122SEmmanuel Vadot 1328*6bc8fe8aSEmmanuel Vadot if (sc->mmc_helper.mmc_pwrseq) 1329*6bc8fe8aSEmmanuel Vadot MMC_PWRSEQ_SET_POWER(sc->mmc_helper.mmc_pwrseq, false); 1330*6bc8fe8aSEmmanuel Vadot 133135a18619SEmmanuel Vadot aw_mmc_reset(sc); 133235a18619SEmmanuel Vadot break; 133335a18619SEmmanuel Vadot case power_up: 1334020df509SEmmanuel Vadot if (__predict_false(aw_mmc_debug & AW_MMC_DEBUG_CARD)) 133535a18619SEmmanuel Vadot device_printf(sc->aw_dev, "Powering up sd/mmc\n"); 1336dfb8c122SEmmanuel Vadot 1337115e71a4SEmmanuel Vadot if (sc->mmc_helper.vmmc_supply) { 1338115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vmmc_supply, ®_status); 1339115e71a4SEmmanuel Vadot if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 13409ed83210SEmmanuel Vadot regulator_enable(sc->mmc_helper.vmmc_supply); 1341115e71a4SEmmanuel Vadot } 1342115e71a4SEmmanuel Vadot if (sc->mmc_helper.vqmmc_supply) { 1343115e71a4SEmmanuel Vadot rv = regulator_status(sc->mmc_helper.vqmmc_supply, ®_status); 1344115e71a4SEmmanuel Vadot if (rv == 0 && reg_status != REGULATOR_STATUS_ENABLED) 13459ed83210SEmmanuel Vadot regulator_enable(sc->mmc_helper.vqmmc_supply); 1346115e71a4SEmmanuel Vadot } 1347*6bc8fe8aSEmmanuel Vadot 1348*6bc8fe8aSEmmanuel Vadot if (sc->mmc_helper.mmc_pwrseq) 1349*6bc8fe8aSEmmanuel Vadot MMC_PWRSEQ_SET_POWER(sc->mmc_helper.mmc_pwrseq, true); 135035a18619SEmmanuel Vadot aw_mmc_init(sc); 135135a18619SEmmanuel Vadot break; 135235a18619SEmmanuel Vadot }; 1353ce0618beSEmmanuel Vadot 1354ce0618beSEmmanuel Vadot /* Enable ddr mode if needed */ 1355ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 1356ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_uhs_ddr50 || 1357ce0618beSEmmanuel Vadot ios->timing == bus_timing_mmc_ddr52) 1358b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_DDR_MOD_SEL; 1359ce0618beSEmmanuel Vadot else 1360b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_DDR_MOD_SEL; 1361ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 1362ce0618beSEmmanuel Vadot 13630f7a6420SEmmanuel Vadot if (ios->clock && ios->clock != sc->aw_clock) { 13640f7a6420SEmmanuel Vadot sc->aw_clock = clock = ios->clock; 1365b5be541fSEmmanuel Vadot 1366b5be541fSEmmanuel Vadot /* Disable clock */ 1367b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 0); 1368b5be541fSEmmanuel Vadot if (error != 0) 1369b5be541fSEmmanuel Vadot return (error); 1370b5be541fSEmmanuel Vadot 1371ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_mmc_ddr52 && 1372ce0618beSEmmanuel Vadot (sc->aw_mmc_conf->new_timing || 1373ce0618beSEmmanuel Vadot ios->bus_width == bus_width_8)) { 1374ce0618beSEmmanuel Vadot div = 2; 1375ce0618beSEmmanuel Vadot clock <<= 1; 1376ce0618beSEmmanuel Vadot } 1377ce0618beSEmmanuel Vadot 1378b5be541fSEmmanuel Vadot /* Reset the divider. */ 1379ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1380ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_DIV; 1381ce0618beSEmmanuel Vadot reg |= div - 1; 1382ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1383ce0618beSEmmanuel Vadot 1384ce0618beSEmmanuel Vadot /* New timing mode if needed */ 1385ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->new_timing) { 1386ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_NTSR); 1387ce0618beSEmmanuel Vadot reg |= AW_MMC_NTSR_MODE_SELECT; 1388ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg); 1389ce0618beSEmmanuel Vadot } 1390b5be541fSEmmanuel Vadot 1391b5be541fSEmmanuel Vadot /* Set the MMC clock. */ 1392101260f3SEmmanuel Vadot error = clk_disable(sc->aw_clk_mmc); 1393101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1394101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1395101260f3SEmmanuel Vadot "failed to disable mmc clock: %d\n", error); 1396ce0618beSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, clock, 1397b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 1398b5be541fSEmmanuel Vadot if (error != 0) { 1399b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 1400b5be541fSEmmanuel Vadot "failed to set frequency to %u Hz: %d\n", 1401ce0618beSEmmanuel Vadot clock, error); 1402b5be541fSEmmanuel Vadot return (error); 1403b5be541fSEmmanuel Vadot } 1404101260f3SEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 1405101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1406101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1407101260f3SEmmanuel Vadot "failed to re-enable mmc clock: %d\n", error); 1408b5be541fSEmmanuel Vadot 1409ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->can_calibrate) 1410ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN); 1411ce0618beSEmmanuel Vadot 1412b5be541fSEmmanuel Vadot /* Enable clock. */ 1413b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 1); 1414b5be541fSEmmanuel Vadot if (error != 0) 1415b5be541fSEmmanuel Vadot return (error); 1416b5be541fSEmmanuel Vadot } 1417b5be541fSEmmanuel Vadot 1418b5be541fSEmmanuel Vadot return (0); 1419b5be541fSEmmanuel Vadot } 1420b5be541fSEmmanuel Vadot 14212671bdb5SEmmanuel Vadot #ifndef MMCCAM 1422b5be541fSEmmanuel Vadot static int 1423b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child) 1424b5be541fSEmmanuel Vadot { 14259ed83210SEmmanuel Vadot struct aw_mmc_softc *sc; 1426b5be541fSEmmanuel Vadot 14279ed83210SEmmanuel Vadot sc = device_get_softc(bus); 14289ed83210SEmmanuel Vadot 14299ed83210SEmmanuel Vadot return (mmc_fdt_gpio_get_readonly(&sc->mmc_helper)); 1430b5be541fSEmmanuel Vadot } 1431b5be541fSEmmanuel Vadot 1432b5be541fSEmmanuel Vadot static int 1433b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child) 1434b5be541fSEmmanuel Vadot { 1435b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1436b5be541fSEmmanuel Vadot int error; 1437b5be541fSEmmanuel Vadot 1438b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1439b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1440b5be541fSEmmanuel Vadot while (sc->aw_bus_busy) { 1441b5be541fSEmmanuel Vadot error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0); 1442b5be541fSEmmanuel Vadot if (error != 0) { 1443b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1444b5be541fSEmmanuel Vadot return (error); 1445b5be541fSEmmanuel Vadot } 1446b5be541fSEmmanuel Vadot } 1447b5be541fSEmmanuel Vadot sc->aw_bus_busy++; 1448b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1449b5be541fSEmmanuel Vadot 1450b5be541fSEmmanuel Vadot return (0); 1451b5be541fSEmmanuel Vadot } 1452b5be541fSEmmanuel Vadot 1453b5be541fSEmmanuel Vadot static int 1454b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child) 1455b5be541fSEmmanuel Vadot { 1456b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1457b5be541fSEmmanuel Vadot 1458b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1459b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1460b5be541fSEmmanuel Vadot sc->aw_bus_busy--; 1461b5be541fSEmmanuel Vadot wakeup(sc); 1462b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1463b5be541fSEmmanuel Vadot 1464b5be541fSEmmanuel Vadot return (0); 1465b5be541fSEmmanuel Vadot } 14662671bdb5SEmmanuel Vadot #endif 1467b5be541fSEmmanuel Vadot 1468b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = { 1469b5be541fSEmmanuel Vadot /* Device interface */ 1470b5be541fSEmmanuel Vadot DEVMETHOD(device_probe, aw_mmc_probe), 1471b5be541fSEmmanuel Vadot DEVMETHOD(device_attach, aw_mmc_attach), 1472b5be541fSEmmanuel Vadot DEVMETHOD(device_detach, aw_mmc_detach), 1473b5be541fSEmmanuel Vadot 1474b5be541fSEmmanuel Vadot /* Bus interface */ 1475b5be541fSEmmanuel Vadot DEVMETHOD(bus_read_ivar, aw_mmc_read_ivar), 1476b5be541fSEmmanuel Vadot DEVMETHOD(bus_write_ivar, aw_mmc_write_ivar), 1477ef546520SIlya Bakulin DEVMETHOD(bus_add_child, bus_generic_add_child), 1478b5be541fSEmmanuel Vadot 14792671bdb5SEmmanuel Vadot #ifndef MMCCAM 1480b5be541fSEmmanuel Vadot /* MMC bridge interface */ 1481b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_update_ios, aw_mmc_update_ios), 1482b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_request, aw_mmc_request), 1483b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_get_ro, aw_mmc_get_ro), 1484623966e1SEmmanuel Vadot DEVMETHOD(mmcbr_switch_vccq, aw_mmc_switch_vccq), 1485b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_acquire_host, aw_mmc_acquire_host), 1486b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_release_host, aw_mmc_release_host), 14872671bdb5SEmmanuel Vadot #endif 14882671bdb5SEmmanuel Vadot 14892671bdb5SEmmanuel Vadot #ifdef MMCCAM 14902671bdb5SEmmanuel Vadot /* MMCCAM interface */ 14912671bdb5SEmmanuel Vadot DEVMETHOD(mmc_sim_get_tran_settings, aw_mmc_get_tran_settings), 14922671bdb5SEmmanuel Vadot DEVMETHOD(mmc_sim_set_tran_settings, aw_mmc_set_tran_settings), 14932671bdb5SEmmanuel Vadot DEVMETHOD(mmc_sim_cam_request, aw_mmc_cam_request), 14942671bdb5SEmmanuel Vadot #endif 1495b5be541fSEmmanuel Vadot 1496b5be541fSEmmanuel Vadot DEVMETHOD_END 1497b5be541fSEmmanuel Vadot }; 1498b5be541fSEmmanuel Vadot 1499b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass; 1500b5be541fSEmmanuel Vadot 1501b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = { 1502b5be541fSEmmanuel Vadot "aw_mmc", 1503b5be541fSEmmanuel Vadot aw_mmc_methods, 1504b5be541fSEmmanuel Vadot sizeof(struct aw_mmc_softc), 1505b5be541fSEmmanuel Vadot }; 1506b5be541fSEmmanuel Vadot 1507b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL, 1508b5be541fSEmmanuel Vadot NULL); 15095e03278fSIlya Bakulin #ifndef MMCCAM 1510b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc); 15115e03278fSIlya Bakulin #endif 1512cbba9a7bSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data); 1513