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> 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> 52b5be541fSEmmanuel Vadot 53b5be541fSEmmanuel Vadot #include <arm/allwinner/aw_mmc.h> 54b5be541fSEmmanuel Vadot #include <dev/extres/clk/clk.h> 55b5be541fSEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h> 56ce0618beSEmmanuel Vadot #include <dev/extres/regulator/regulator.h> 57b5be541fSEmmanuel Vadot 585e03278fSIlya Bakulin #include "opt_mmccam.h" 595e03278fSIlya Bakulin 605e03278fSIlya Bakulin #ifdef MMCCAM 615e03278fSIlya Bakulin #include <cam/cam.h> 625e03278fSIlya Bakulin #include <cam/cam_ccb.h> 635e03278fSIlya Bakulin #include <cam/cam_debug.h> 645e03278fSIlya Bakulin #include <cam/cam_sim.h> 655e03278fSIlya Bakulin #include <cam/cam_xpt_sim.h> 665e03278fSIlya Bakulin #endif 675e03278fSIlya Bakulin 68b5be541fSEmmanuel Vadot #define AW_MMC_MEMRES 0 69b5be541fSEmmanuel Vadot #define AW_MMC_IRQRES 1 70b5be541fSEmmanuel Vadot #define AW_MMC_RESSZ 2 71c39ea909SEmmanuel Vadot #define AW_MMC_DMA_SEGS (PAGE_SIZE / sizeof(struct aw_mmc_dma_desc)) 72c39ea909SEmmanuel Vadot #define AW_MMC_DMA_DESC_SIZE (sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS) 73b5be541fSEmmanuel Vadot #define AW_MMC_DMA_FTRGLEVEL 0x20070008 74c39ea909SEmmanuel Vadot 75b5be541fSEmmanuel Vadot #define AW_MMC_RESET_RETRY 1000 76b5be541fSEmmanuel Vadot 77b5be541fSEmmanuel Vadot #define CARD_ID_FREQUENCY 400000 78b5be541fSEmmanuel Vadot 79ce0618beSEmmanuel Vadot struct aw_mmc_conf { 80ce0618beSEmmanuel Vadot uint32_t dma_xferlen; 81ce0618beSEmmanuel Vadot bool mask_data0; 82ce0618beSEmmanuel Vadot bool can_calibrate; 83ce0618beSEmmanuel Vadot bool new_timing; 84ce0618beSEmmanuel Vadot }; 85ce0618beSEmmanuel Vadot 86ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a10_mmc_conf = { 87ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 88ce0618beSEmmanuel Vadot }; 89ce0618beSEmmanuel Vadot 90ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a13_mmc_conf = { 91ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 92ce0618beSEmmanuel Vadot }; 93ce0618beSEmmanuel Vadot 94ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_mmc_conf = { 95ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 96ce0618beSEmmanuel Vadot .mask_data0 = true, 97ce0618beSEmmanuel Vadot .can_calibrate = true, 98ce0618beSEmmanuel Vadot .new_timing = true, 99ce0618beSEmmanuel Vadot }; 100ce0618beSEmmanuel Vadot 101ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_emmc_conf = { 102ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 103ce0618beSEmmanuel Vadot .can_calibrate = true, 104ce0618beSEmmanuel Vadot }; 105ce0618beSEmmanuel Vadot 106b5be541fSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 107ce0618beSEmmanuel Vadot {"allwinner,sun4i-a10-mmc", (uintptr_t)&a10_mmc_conf}, 108ce0618beSEmmanuel Vadot {"allwinner,sun5i-a13-mmc", (uintptr_t)&a13_mmc_conf}, 109ce0618beSEmmanuel Vadot {"allwinner,sun7i-a20-mmc", (uintptr_t)&a13_mmc_conf}, 110ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-mmc", (uintptr_t)&a64_mmc_conf}, 111ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-emmc", (uintptr_t)&a64_emmc_conf}, 112b5be541fSEmmanuel Vadot {NULL, 0} 113b5be541fSEmmanuel Vadot }; 114b5be541fSEmmanuel Vadot 115b5be541fSEmmanuel Vadot struct aw_mmc_softc { 116b5be541fSEmmanuel Vadot device_t aw_dev; 117b5be541fSEmmanuel Vadot clk_t aw_clk_ahb; 118b5be541fSEmmanuel Vadot clk_t aw_clk_mmc; 119b5be541fSEmmanuel Vadot hwreset_t aw_rst_ahb; 120b5be541fSEmmanuel Vadot int aw_bus_busy; 121b5be541fSEmmanuel Vadot int aw_resid; 122b5be541fSEmmanuel Vadot int aw_timeout; 123b5be541fSEmmanuel Vadot struct callout aw_timeoutc; 124b5be541fSEmmanuel Vadot struct mmc_host aw_host; 1255e03278fSIlya Bakulin #ifdef MMCCAM 1265e03278fSIlya Bakulin union ccb * ccb; 1275e03278fSIlya Bakulin struct cam_devq * devq; 1285e03278fSIlya Bakulin struct cam_sim * sim; 1295e03278fSIlya Bakulin struct mtx sim_mtx; 1305e03278fSIlya Bakulin #else 131b5be541fSEmmanuel Vadot struct mmc_request * aw_req; 1325e03278fSIlya Bakulin #endif 133b5be541fSEmmanuel Vadot struct mtx aw_mtx; 134b5be541fSEmmanuel Vadot struct resource * aw_res[AW_MMC_RESSZ]; 135ce0618beSEmmanuel Vadot struct aw_mmc_conf * aw_mmc_conf; 136b5be541fSEmmanuel Vadot uint32_t aw_intr; 137b5be541fSEmmanuel Vadot uint32_t aw_intr_wait; 138b5be541fSEmmanuel Vadot void * aw_intrhand; 139ce0618beSEmmanuel Vadot regulator_t aw_reg_vmmc; 140ce0618beSEmmanuel Vadot regulator_t aw_reg_vqmmc; 1410f7a6420SEmmanuel Vadot unsigned int aw_clock; 142b5be541fSEmmanuel Vadot 143b5be541fSEmmanuel Vadot /* Fields required for DMA access. */ 144b5be541fSEmmanuel Vadot bus_addr_t aw_dma_desc_phys; 145b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_map; 146b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_tag; 147b5be541fSEmmanuel Vadot void * aw_dma_desc; 148b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_buf_map; 149b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_buf_tag; 150b5be541fSEmmanuel Vadot int aw_dma_map_err; 151b5be541fSEmmanuel Vadot }; 152b5be541fSEmmanuel Vadot 153b5be541fSEmmanuel Vadot static struct resource_spec aw_mmc_res_spec[] = { 154b5be541fSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE }, 155b5be541fSEmmanuel Vadot { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 156b5be541fSEmmanuel Vadot { -1, 0, 0 } 157b5be541fSEmmanuel Vadot }; 158b5be541fSEmmanuel Vadot 159b5be541fSEmmanuel Vadot static int aw_mmc_probe(device_t); 160b5be541fSEmmanuel Vadot static int aw_mmc_attach(device_t); 161b5be541fSEmmanuel Vadot static int aw_mmc_detach(device_t); 162b5be541fSEmmanuel Vadot static int aw_mmc_setup_dma(struct aw_mmc_softc *); 163b5be541fSEmmanuel Vadot static int aw_mmc_reset(struct aw_mmc_softc *); 16435a18619SEmmanuel Vadot static int aw_mmc_init(struct aw_mmc_softc *); 165b5be541fSEmmanuel Vadot static void aw_mmc_intr(void *); 166b5be541fSEmmanuel Vadot static int aw_mmc_update_clock(struct aw_mmc_softc *, uint32_t); 167b5be541fSEmmanuel Vadot 1685e03278fSIlya Bakulin static void aw_mmc_print_error(uint32_t); 169b5be541fSEmmanuel Vadot static int aw_mmc_update_ios(device_t, device_t); 170b5be541fSEmmanuel Vadot static int aw_mmc_request(device_t, device_t, struct mmc_request *); 171b5be541fSEmmanuel Vadot static int aw_mmc_get_ro(device_t, device_t); 172b5be541fSEmmanuel Vadot static int aw_mmc_acquire_host(device_t, device_t); 173b5be541fSEmmanuel Vadot static int aw_mmc_release_host(device_t, device_t); 1745e03278fSIlya Bakulin #ifdef MMCCAM 1755e03278fSIlya Bakulin static void aw_mmc_cam_action(struct cam_sim *, union ccb *); 1765e03278fSIlya Bakulin static void aw_mmc_cam_poll(struct cam_sim *); 1775e03278fSIlya Bakulin static int aw_mmc_cam_settran_settings(struct aw_mmc_softc *, union ccb *); 1785e03278fSIlya Bakulin static int aw_mmc_cam_request(struct aw_mmc_softc *, union ccb *); 1795e03278fSIlya Bakulin static void aw_mmc_cam_handle_mmcio(struct cam_sim *, union ccb *); 1805e03278fSIlya Bakulin #endif 181b5be541fSEmmanuel Vadot 182b5be541fSEmmanuel Vadot #define AW_MMC_LOCK(_sc) mtx_lock(&(_sc)->aw_mtx) 183b5be541fSEmmanuel Vadot #define AW_MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->aw_mtx) 184b5be541fSEmmanuel Vadot #define AW_MMC_READ_4(_sc, _reg) \ 185b5be541fSEmmanuel Vadot bus_read_4((_sc)->aw_res[AW_MMC_MEMRES], _reg) 186b5be541fSEmmanuel Vadot #define AW_MMC_WRITE_4(_sc, _reg, _value) \ 187b5be541fSEmmanuel Vadot bus_write_4((_sc)->aw_res[AW_MMC_MEMRES], _reg, _value) 188b5be541fSEmmanuel Vadot 1895e03278fSIlya Bakulin #ifdef MMCCAM 1905e03278fSIlya Bakulin static void 1915e03278fSIlya Bakulin aw_mmc_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb) 1925e03278fSIlya Bakulin { 1935e03278fSIlya Bakulin struct aw_mmc_softc *sc; 1945e03278fSIlya Bakulin 1955e03278fSIlya Bakulin sc = cam_sim_softc(sim); 1965e03278fSIlya Bakulin 1975e03278fSIlya Bakulin aw_mmc_cam_request(sc, ccb); 1985e03278fSIlya Bakulin } 1995e03278fSIlya Bakulin 2005e03278fSIlya Bakulin static void 2015e03278fSIlya Bakulin aw_mmc_cam_action(struct cam_sim *sim, union ccb *ccb) 2025e03278fSIlya Bakulin { 2035e03278fSIlya Bakulin struct aw_mmc_softc *sc; 2045e03278fSIlya Bakulin 2055e03278fSIlya Bakulin sc = cam_sim_softc(sim); 2065e03278fSIlya Bakulin if (sc == NULL) { 2075e03278fSIlya Bakulin ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2085e03278fSIlya Bakulin xpt_done(ccb); 2095e03278fSIlya Bakulin return; 2105e03278fSIlya Bakulin } 2115e03278fSIlya Bakulin 2125e03278fSIlya Bakulin mtx_assert(&sc->sim_mtx, MA_OWNED); 2135e03278fSIlya Bakulin 2145e03278fSIlya Bakulin switch (ccb->ccb_h.func_code) { 2155e03278fSIlya Bakulin case XPT_PATH_INQ: 216*8c7cd14aSWarner Losh mmc_path_inq(&ccb->cpi, "Deglitch Networks", sim, 217*8c7cd14aSWarner Losh (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS) / 218*8c7cd14aSWarner Losh MMC_SECTOR_SIZE); 2195e03278fSIlya Bakulin break; 220*8c7cd14aSWarner Losh 2215e03278fSIlya Bakulin case XPT_GET_TRAN_SETTINGS: 2225e03278fSIlya Bakulin { 2235e03278fSIlya Bakulin struct ccb_trans_settings *cts = &ccb->cts; 2245e03278fSIlya Bakulin 2255e03278fSIlya Bakulin if (bootverbose) 2265e03278fSIlya Bakulin device_printf(sc->aw_dev, "Got XPT_GET_TRAN_SETTINGS\n"); 2275e03278fSIlya Bakulin 2285e03278fSIlya Bakulin cts->protocol = PROTO_MMCSD; 2295e03278fSIlya Bakulin cts->protocol_version = 1; 2305e03278fSIlya Bakulin cts->transport = XPORT_MMCSD; 2315e03278fSIlya Bakulin cts->transport_version = 1; 2325e03278fSIlya Bakulin cts->xport_specific.valid = 0; 2335e03278fSIlya Bakulin cts->proto_specific.mmc.host_ocr = sc->aw_host.host_ocr; 2345e03278fSIlya Bakulin cts->proto_specific.mmc.host_f_min = sc->aw_host.f_min; 2355e03278fSIlya Bakulin cts->proto_specific.mmc.host_f_max = sc->aw_host.f_max; 2365e03278fSIlya Bakulin cts->proto_specific.mmc.host_caps = sc->aw_host.caps; 2375d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = (sc->aw_mmc_conf->dma_xferlen * 2385d20e651SIlya Bakulin AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 2395e03278fSIlya Bakulin memcpy(&cts->proto_specific.mmc.ios, &sc->aw_host.ios, sizeof(struct mmc_ios)); 2405e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_CMP; 2415e03278fSIlya Bakulin break; 2425e03278fSIlya Bakulin } 2435e03278fSIlya Bakulin case XPT_SET_TRAN_SETTINGS: 2445e03278fSIlya Bakulin { 2455e03278fSIlya Bakulin if (bootverbose) 2465e03278fSIlya Bakulin device_printf(sc->aw_dev, "Got XPT_SET_TRAN_SETTINGS\n"); 2475e03278fSIlya Bakulin aw_mmc_cam_settran_settings(sc, ccb); 2485e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_CMP; 2495e03278fSIlya Bakulin break; 2505e03278fSIlya Bakulin } 2515e03278fSIlya Bakulin case XPT_RESET_BUS: 2525e03278fSIlya Bakulin if (bootverbose) 2535e03278fSIlya Bakulin device_printf(sc->aw_dev, "Got XPT_RESET_BUS, ACK it...\n"); 2545e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_CMP; 2555e03278fSIlya Bakulin break; 2565e03278fSIlya Bakulin case XPT_MMC_IO: 2575e03278fSIlya Bakulin /* 2585e03278fSIlya Bakulin * Here is the HW-dependent part of 2595e03278fSIlya Bakulin * sending the command to the underlying h/w 2605e03278fSIlya Bakulin * At some point in the future an interrupt comes. 2615e03278fSIlya Bakulin * Then the request will be marked as completed. 2625e03278fSIlya Bakulin */ 2635e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_INPROG; 2645e03278fSIlya Bakulin 2655e03278fSIlya Bakulin aw_mmc_cam_handle_mmcio(sim, ccb); 2665e03278fSIlya Bakulin return; 2675e03278fSIlya Bakulin /* NOTREACHED */ 2685e03278fSIlya Bakulin break; 2695e03278fSIlya Bakulin default: 2705e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_INVALID; 2715e03278fSIlya Bakulin break; 2725e03278fSIlya Bakulin } 2735e03278fSIlya Bakulin xpt_done(ccb); 2745e03278fSIlya Bakulin return; 2755e03278fSIlya Bakulin } 2765e03278fSIlya Bakulin 2775e03278fSIlya Bakulin static void 2785e03278fSIlya Bakulin aw_mmc_cam_poll(struct cam_sim *sim) 2795e03278fSIlya Bakulin { 2805e03278fSIlya Bakulin return; 2815e03278fSIlya Bakulin } 2825e03278fSIlya Bakulin 2835e03278fSIlya Bakulin static int 2845e03278fSIlya Bakulin aw_mmc_cam_settran_settings(struct aw_mmc_softc *sc, union ccb *ccb) 2855e03278fSIlya Bakulin { 2865e03278fSIlya Bakulin struct mmc_ios *ios; 2875e03278fSIlya Bakulin struct mmc_ios *new_ios; 2885e03278fSIlya Bakulin struct ccb_trans_settings_mmc *cts; 2895e03278fSIlya Bakulin 2905e03278fSIlya Bakulin ios = &sc->aw_host.ios; 2915e03278fSIlya Bakulin 2925e03278fSIlya Bakulin cts = &ccb->cts.proto_specific.mmc; 2935e03278fSIlya Bakulin new_ios = &cts->ios; 2945e03278fSIlya Bakulin 2955e03278fSIlya Bakulin /* Update only requested fields */ 2965e03278fSIlya Bakulin if (cts->ios_valid & MMC_CLK) { 2975e03278fSIlya Bakulin ios->clock = new_ios->clock; 2985e03278fSIlya Bakulin device_printf(sc->aw_dev, "Clock => %d\n", ios->clock); 2995e03278fSIlya Bakulin } 3005e03278fSIlya Bakulin if (cts->ios_valid & MMC_VDD) { 3015e03278fSIlya Bakulin ios->vdd = new_ios->vdd; 3025e03278fSIlya Bakulin device_printf(sc->aw_dev, "VDD => %d\n", ios->vdd); 3035e03278fSIlya Bakulin } 3045e03278fSIlya Bakulin if (cts->ios_valid & MMC_CS) { 3055e03278fSIlya Bakulin ios->chip_select = new_ios->chip_select; 3065e03278fSIlya Bakulin device_printf(sc->aw_dev, "CS => %d\n", ios->chip_select); 3075e03278fSIlya Bakulin } 3085e03278fSIlya Bakulin if (cts->ios_valid & MMC_BW) { 3095e03278fSIlya Bakulin ios->bus_width = new_ios->bus_width; 3105e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus width => %d\n", ios->bus_width); 3115e03278fSIlya Bakulin } 3125e03278fSIlya Bakulin if (cts->ios_valid & MMC_PM) { 3135e03278fSIlya Bakulin ios->power_mode = new_ios->power_mode; 3145e03278fSIlya Bakulin device_printf(sc->aw_dev, "Power mode => %d\n", ios->power_mode); 3155e03278fSIlya Bakulin } 3165e03278fSIlya Bakulin if (cts->ios_valid & MMC_BT) { 3175e03278fSIlya Bakulin ios->timing = new_ios->timing; 3185e03278fSIlya Bakulin device_printf(sc->aw_dev, "Timing => %d\n", ios->timing); 3195e03278fSIlya Bakulin } 3205e03278fSIlya Bakulin if (cts->ios_valid & MMC_BM) { 3215e03278fSIlya Bakulin ios->bus_mode = new_ios->bus_mode; 3225e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus mode => %d\n", ios->bus_mode); 3235e03278fSIlya Bakulin } 3245e03278fSIlya Bakulin 3255e03278fSIlya Bakulin return (aw_mmc_update_ios(sc->aw_dev, NULL)); 3265e03278fSIlya Bakulin } 3275e03278fSIlya Bakulin 3285e03278fSIlya Bakulin static int 3295e03278fSIlya Bakulin aw_mmc_cam_request(struct aw_mmc_softc *sc, union ccb *ccb) 3305e03278fSIlya Bakulin { 3315e03278fSIlya Bakulin struct ccb_mmcio *mmcio; 3325e03278fSIlya Bakulin 3335e03278fSIlya Bakulin mmcio = &ccb->mmcio; 3345e03278fSIlya Bakulin 3355e03278fSIlya Bakulin AW_MMC_LOCK(sc); 3365e03278fSIlya Bakulin 3375e03278fSIlya Bakulin #ifdef DEBUG 3385e03278fSIlya Bakulin if (__predict_false(bootverbose)) { 3395e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 3405e03278fSIlya Bakulin mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 3415e03278fSIlya Bakulin mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 3425e03278fSIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0); 3435e03278fSIlya Bakulin } 3445e03278fSIlya Bakulin #endif 3455e03278fSIlya Bakulin if (mmcio->cmd.data != NULL) { 3465e03278fSIlya Bakulin if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 3475e03278fSIlya Bakulin panic("data->len = %d, data->flags = %d -- something is b0rked", 3485e03278fSIlya Bakulin (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 3495e03278fSIlya Bakulin } 3505e03278fSIlya Bakulin if (sc->ccb != NULL) { 3515e03278fSIlya Bakulin device_printf(sc->aw_dev, "Controller still has an active command\n"); 3525e03278fSIlya Bakulin return (EBUSY); 3535e03278fSIlya Bakulin } 3545e03278fSIlya Bakulin sc->ccb = ccb; 3555e03278fSIlya Bakulin /* aw_mmc_request locks again */ 3565e03278fSIlya Bakulin AW_MMC_UNLOCK(sc); 3575e03278fSIlya Bakulin aw_mmc_request(sc->aw_dev, NULL, NULL); 3585e03278fSIlya Bakulin 3595e03278fSIlya Bakulin return (0); 3605e03278fSIlya Bakulin } 3615e03278fSIlya Bakulin #endif /* MMCCAM */ 3625e03278fSIlya Bakulin 363b5be541fSEmmanuel Vadot static int 364b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev) 365b5be541fSEmmanuel Vadot { 366b5be541fSEmmanuel Vadot 367b5be541fSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 368b5be541fSEmmanuel Vadot return (ENXIO); 369b5be541fSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 370b5be541fSEmmanuel Vadot return (ENXIO); 371b5be541fSEmmanuel Vadot 372b5be541fSEmmanuel Vadot device_set_desc(dev, "Allwinner Integrated MMC/SD controller"); 373b5be541fSEmmanuel Vadot 374b5be541fSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 375b5be541fSEmmanuel Vadot } 376b5be541fSEmmanuel Vadot 377b5be541fSEmmanuel Vadot static int 378b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev) 379b5be541fSEmmanuel Vadot { 380b5be541fSEmmanuel Vadot device_t child; 381b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 382b5be541fSEmmanuel Vadot struct sysctl_ctx_list *ctx; 383b5be541fSEmmanuel Vadot struct sysctl_oid_list *tree; 384bbf8c8faSEmmanuel Vadot uint32_t bus_width, max_freq; 385b5be541fSEmmanuel Vadot phandle_t node; 386b5be541fSEmmanuel Vadot int error; 387b5be541fSEmmanuel Vadot 388b5be541fSEmmanuel Vadot node = ofw_bus_get_node(dev); 389b5be541fSEmmanuel Vadot sc = device_get_softc(dev); 390b5be541fSEmmanuel Vadot sc->aw_dev = dev; 391ce0618beSEmmanuel Vadot 392ce0618beSEmmanuel Vadot sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 393ce0618beSEmmanuel Vadot 3945e03278fSIlya Bakulin #ifndef MMCCAM 395b5be541fSEmmanuel Vadot sc->aw_req = NULL; 3965e03278fSIlya Bakulin #endif 397b5be541fSEmmanuel Vadot if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) { 398b5be541fSEmmanuel Vadot device_printf(dev, "cannot allocate device resources\n"); 399b5be541fSEmmanuel Vadot return (ENXIO); 400b5be541fSEmmanuel Vadot } 401b5be541fSEmmanuel Vadot if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES], 402b5be541fSEmmanuel Vadot INTR_TYPE_MISC | INTR_MPSAFE, NULL, aw_mmc_intr, sc, 403b5be541fSEmmanuel Vadot &sc->aw_intrhand)) { 404b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 405b5be541fSEmmanuel Vadot device_printf(dev, "cannot setup interrupt handler\n"); 406b5be541fSEmmanuel Vadot return (ENXIO); 407b5be541fSEmmanuel Vadot } 408b5be541fSEmmanuel Vadot mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc", 409b5be541fSEmmanuel Vadot MTX_DEF); 410b5be541fSEmmanuel Vadot callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0); 411b5be541fSEmmanuel Vadot 412b5be541fSEmmanuel Vadot /* De-assert reset */ 413b5be541fSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) { 414b5be541fSEmmanuel Vadot error = hwreset_deassert(sc->aw_rst_ahb); 415b5be541fSEmmanuel Vadot if (error != 0) { 416b5be541fSEmmanuel Vadot device_printf(dev, "cannot de-assert reset\n"); 417b5be541fSEmmanuel Vadot goto fail; 418b5be541fSEmmanuel Vadot } 419b5be541fSEmmanuel Vadot } 420b5be541fSEmmanuel Vadot 421b5be541fSEmmanuel Vadot /* Activate the module clock. */ 422b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb); 423b5be541fSEmmanuel Vadot if (error != 0) { 424b5be541fSEmmanuel Vadot device_printf(dev, "cannot get ahb clock\n"); 425b5be541fSEmmanuel Vadot goto fail; 426b5be541fSEmmanuel Vadot } 427b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_ahb); 428b5be541fSEmmanuel Vadot if (error != 0) { 429b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable ahb clock\n"); 430b5be541fSEmmanuel Vadot goto fail; 431b5be541fSEmmanuel Vadot } 432b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc); 433b5be541fSEmmanuel Vadot if (error != 0) { 434b5be541fSEmmanuel Vadot device_printf(dev, "cannot get mmc clock\n"); 435b5be541fSEmmanuel Vadot goto fail; 436b5be541fSEmmanuel Vadot } 437b5be541fSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY, 438b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 439b5be541fSEmmanuel Vadot if (error != 0) { 440b5be541fSEmmanuel Vadot device_printf(dev, "cannot init mmc clock\n"); 441b5be541fSEmmanuel Vadot goto fail; 442b5be541fSEmmanuel Vadot } 443b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 444b5be541fSEmmanuel Vadot if (error != 0) { 445b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable mmc clock\n"); 446b5be541fSEmmanuel Vadot goto fail; 447b5be541fSEmmanuel Vadot } 448b5be541fSEmmanuel Vadot 449b5be541fSEmmanuel Vadot sc->aw_timeout = 10; 450b5be541fSEmmanuel Vadot ctx = device_get_sysctl_ctx(dev); 451b5be541fSEmmanuel Vadot tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 452b5be541fSEmmanuel Vadot SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW, 453b5be541fSEmmanuel Vadot &sc->aw_timeout, 0, "Request timeout in seconds"); 454b5be541fSEmmanuel Vadot 455b5be541fSEmmanuel Vadot /* Soft Reset controller. */ 456b5be541fSEmmanuel Vadot if (aw_mmc_reset(sc) != 0) { 457b5be541fSEmmanuel Vadot device_printf(dev, "cannot reset the controller\n"); 458b5be541fSEmmanuel Vadot goto fail; 459b5be541fSEmmanuel Vadot } 460b5be541fSEmmanuel Vadot 461b5be541fSEmmanuel Vadot if (aw_mmc_setup_dma(sc) != 0) { 462b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "Couldn't setup DMA!\n"); 463b5be541fSEmmanuel Vadot goto fail; 464b5be541fSEmmanuel Vadot } 465b5be541fSEmmanuel Vadot 466b5be541fSEmmanuel Vadot if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0) 467b5be541fSEmmanuel Vadot bus_width = 4; 468b5be541fSEmmanuel Vadot 469ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply", 4703177f7cdSEmmanuel Vadot &sc->aw_reg_vmmc) == 0) { 4713177f7cdSEmmanuel Vadot if (bootverbose) 472ce0618beSEmmanuel Vadot device_printf(dev, "vmmc-supply regulator found\n"); 4733177f7cdSEmmanuel Vadot } 474ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply", 4753177f7cdSEmmanuel Vadot &sc->aw_reg_vqmmc) == 0 && bootverbose) { 4763177f7cdSEmmanuel Vadot if (bootverbose) 477ce0618beSEmmanuel Vadot device_printf(dev, "vqmmc-supply regulator found\n"); 4783177f7cdSEmmanuel Vadot } 479ce0618beSEmmanuel Vadot 480b5be541fSEmmanuel Vadot sc->aw_host.f_min = 400000; 481bbf8c8faSEmmanuel Vadot 482bbf8c8faSEmmanuel Vadot if (OF_getencprop(node, "max-frequency", &max_freq, 483bbf8c8faSEmmanuel Vadot sizeof(uint32_t)) <= 0) 484bbf8c8faSEmmanuel Vadot max_freq = 52000000; 485bbf8c8faSEmmanuel Vadot sc->aw_host.f_max = max_freq; 486bbf8c8faSEmmanuel Vadot 487b5be541fSEmmanuel Vadot sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340; 488ce0618beSEmmanuel Vadot sc->aw_host.caps = MMC_CAP_HSPEED | MMC_CAP_UHS_SDR12 | 489ce0618beSEmmanuel Vadot MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 | 490ce0618beSEmmanuel Vadot MMC_CAP_UHS_DDR50 | MMC_CAP_MMC_DDR52; 491ce0618beSEmmanuel Vadot 49294d3675eSEmmanuel Vadot if (sc->aw_reg_vqmmc != NULL) { 49394d3675eSEmmanuel Vadot if (regulator_check_voltage(sc->aw_reg_vqmmc, 1800000) == 0) 49494d3675eSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_180; 49594d3675eSEmmanuel Vadot if (regulator_check_voltage(sc->aw_reg_vqmmc, 3300000) == 0) 49694d3675eSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_330; 49794d3675eSEmmanuel Vadot } else 49894d3675eSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_330; 499ce0618beSEmmanuel Vadot 500b5be541fSEmmanuel Vadot if (bus_width >= 4) 501b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_4_BIT_DATA; 502b5be541fSEmmanuel Vadot if (bus_width >= 8) 503b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_8_BIT_DATA; 504b5be541fSEmmanuel Vadot 5055e03278fSIlya Bakulin #ifdef MMCCAM 5065e03278fSIlya Bakulin child = NULL; /* Not used by MMCCAM, need to silence compiler warnings */ 5075e03278fSIlya Bakulin sc->ccb = NULL; 5085e03278fSIlya Bakulin if ((sc->devq = cam_simq_alloc(1)) == NULL) { 5095e03278fSIlya Bakulin goto fail; 5105e03278fSIlya Bakulin } 5115e03278fSIlya Bakulin 5125e03278fSIlya Bakulin mtx_init(&sc->sim_mtx, "awmmcsim", NULL, MTX_DEF); 513ef546520SIlya Bakulin sc->sim = cam_sim_alloc_dev(aw_mmc_cam_action, aw_mmc_cam_poll, 514ef546520SIlya Bakulin "aw_mmc_sim", sc, dev, 5155e03278fSIlya Bakulin &sc->sim_mtx, 1, 1, sc->devq); 5165e03278fSIlya Bakulin 5175e03278fSIlya Bakulin if (sc->sim == NULL) { 5185e03278fSIlya Bakulin cam_simq_free(sc->devq); 5195e03278fSIlya Bakulin device_printf(dev, "cannot allocate CAM SIM\n"); 5205e03278fSIlya Bakulin goto fail; 5215e03278fSIlya Bakulin } 5225e03278fSIlya Bakulin 5235e03278fSIlya Bakulin mtx_lock(&sc->sim_mtx); 5245e03278fSIlya Bakulin if (xpt_bus_register(sc->sim, sc->aw_dev, 0) != 0) { 5255e03278fSIlya Bakulin device_printf(dev, "cannot register SCSI pass-through bus\n"); 5265e03278fSIlya Bakulin cam_sim_free(sc->sim, FALSE); 5275e03278fSIlya Bakulin cam_simq_free(sc->devq); 5285e03278fSIlya Bakulin mtx_unlock(&sc->sim_mtx); 5295e03278fSIlya Bakulin goto fail; 5305e03278fSIlya Bakulin } 5315e03278fSIlya Bakulin 5325e03278fSIlya Bakulin mtx_unlock(&sc->sim_mtx); 5335e03278fSIlya Bakulin #else /* !MMCCAM */ 534b5be541fSEmmanuel Vadot child = device_add_child(dev, "mmc", -1); 535b5be541fSEmmanuel Vadot if (child == NULL) { 536b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC bus failed!\n"); 537b5be541fSEmmanuel Vadot goto fail; 538b5be541fSEmmanuel Vadot } 539b5be541fSEmmanuel Vadot if (device_probe_and_attach(child) != 0) { 540b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC child failed!\n"); 541b5be541fSEmmanuel Vadot device_delete_child(dev, child); 542b5be541fSEmmanuel Vadot goto fail; 543b5be541fSEmmanuel Vadot } 5445e03278fSIlya Bakulin #endif /* MMCCAM */ 545b5be541fSEmmanuel Vadot return (0); 546b5be541fSEmmanuel Vadot 547b5be541fSEmmanuel Vadot fail: 548b5be541fSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 549b5be541fSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 550b5be541fSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 551b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 552b5be541fSEmmanuel Vadot 5535e03278fSIlya Bakulin #ifdef MMCCAM 5545e03278fSIlya Bakulin if (sc->sim != NULL) { 5555e03278fSIlya Bakulin mtx_lock(&sc->sim_mtx); 5565e03278fSIlya Bakulin xpt_bus_deregister(cam_sim_path(sc->sim)); 5575e03278fSIlya Bakulin cam_sim_free(sc->sim, FALSE); 5585e03278fSIlya Bakulin mtx_unlock(&sc->sim_mtx); 5595e03278fSIlya Bakulin } 5605e03278fSIlya Bakulin 5615e03278fSIlya Bakulin if (sc->devq != NULL) 5625e03278fSIlya Bakulin cam_simq_free(sc->devq); 5635e03278fSIlya Bakulin #endif 564b5be541fSEmmanuel Vadot return (ENXIO); 565b5be541fSEmmanuel Vadot } 566b5be541fSEmmanuel Vadot 567b5be541fSEmmanuel Vadot static int 568b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev) 569b5be541fSEmmanuel Vadot { 570b5be541fSEmmanuel Vadot 571b5be541fSEmmanuel Vadot return (EBUSY); 572b5be541fSEmmanuel Vadot } 573b5be541fSEmmanuel Vadot 574b5be541fSEmmanuel Vadot static void 575b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 576b5be541fSEmmanuel Vadot { 577b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 578b5be541fSEmmanuel Vadot 579b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 580b5be541fSEmmanuel Vadot if (err) { 581b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 582b5be541fSEmmanuel Vadot return; 583b5be541fSEmmanuel Vadot } 584b5be541fSEmmanuel Vadot sc->aw_dma_desc_phys = segs[0].ds_addr; 585b5be541fSEmmanuel Vadot } 586b5be541fSEmmanuel Vadot 587b5be541fSEmmanuel Vadot static int 588b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc) 589b5be541fSEmmanuel Vadot { 590c39ea909SEmmanuel Vadot int error; 591b5be541fSEmmanuel Vadot 592b5be541fSEmmanuel Vadot /* Allocate the DMA descriptor memory. */ 593c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 594c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 595c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 596c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 597c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 598c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 599c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, 1, /* maxsize, nsegment */ 600c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, /* maxsegsize */ 601c39ea909SEmmanuel Vadot 0, /* flags */ 602c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 603c39ea909SEmmanuel Vadot &sc->aw_dma_tag); 604b5be541fSEmmanuel Vadot if (error) 605b5be541fSEmmanuel Vadot return (error); 606b5be541fSEmmanuel Vadot 607c39ea909SEmmanuel Vadot error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc, 608c39ea909SEmmanuel Vadot BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, 609c39ea909SEmmanuel Vadot &sc->aw_dma_map); 610c39ea909SEmmanuel Vadot if (error) 611c39ea909SEmmanuel Vadot return (error); 612c39ea909SEmmanuel Vadot 613c39ea909SEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_tag, 614c39ea909SEmmanuel Vadot sc->aw_dma_map, 615c39ea909SEmmanuel Vadot sc->aw_dma_desc, AW_MMC_DMA_DESC_SIZE, 616c39ea909SEmmanuel Vadot aw_dma_desc_cb, sc, 0); 617b5be541fSEmmanuel Vadot if (error) 618b5be541fSEmmanuel Vadot return (error); 619b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 620b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 621b5be541fSEmmanuel Vadot 622b5be541fSEmmanuel Vadot /* Create the DMA map for data transfers. */ 623c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 624c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 625c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 626c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 627c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 628c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 629c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen * 630c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS, /* maxsize, nsegments */ 631c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen, /* maxsegsize */ 632c39ea909SEmmanuel Vadot BUS_DMA_ALLOCNOW, /* flags */ 633c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 634b5be541fSEmmanuel Vadot &sc->aw_dma_buf_tag); 635b5be541fSEmmanuel Vadot if (error) 636b5be541fSEmmanuel Vadot return (error); 637b5be541fSEmmanuel Vadot error = bus_dmamap_create(sc->aw_dma_buf_tag, 0, 638b5be541fSEmmanuel Vadot &sc->aw_dma_buf_map); 639b5be541fSEmmanuel Vadot if (error) 640b5be541fSEmmanuel Vadot return (error); 641b5be541fSEmmanuel Vadot 642b5be541fSEmmanuel Vadot return (0); 643b5be541fSEmmanuel Vadot } 644b5be541fSEmmanuel Vadot 645b5be541fSEmmanuel Vadot static void 646b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 647b5be541fSEmmanuel Vadot { 648b5be541fSEmmanuel Vadot int i; 649b5be541fSEmmanuel Vadot struct aw_mmc_dma_desc *dma_desc; 650b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 651b5be541fSEmmanuel Vadot 652b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 653b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 654b5be541fSEmmanuel Vadot 655b5be541fSEmmanuel Vadot if (err) 656b5be541fSEmmanuel Vadot return; 657b5be541fSEmmanuel Vadot 658b5be541fSEmmanuel Vadot dma_desc = sc->aw_dma_desc; 659b5be541fSEmmanuel Vadot for (i = 0; i < nsegs; i++) { 660c39ea909SEmmanuel Vadot if (segs[i].ds_len == sc->aw_mmc_conf->dma_xferlen) 661c39ea909SEmmanuel Vadot dma_desc[i].buf_size = 0; /* Size of 0 indicate max len */ 662c39ea909SEmmanuel Vadot else 663b5be541fSEmmanuel Vadot dma_desc[i].buf_size = segs[i].ds_len; 664b5be541fSEmmanuel Vadot dma_desc[i].buf_addr = segs[i].ds_addr; 665b5be541fSEmmanuel Vadot dma_desc[i].config = AW_MMC_DMA_CONFIG_CH | 666c39ea909SEmmanuel Vadot AW_MMC_DMA_CONFIG_OWN | AW_MMC_DMA_CONFIG_DIC; 667c39ea909SEmmanuel Vadot 668b5be541fSEmmanuel Vadot dma_desc[i].next = sc->aw_dma_desc_phys + 669b5be541fSEmmanuel Vadot ((i + 1) * sizeof(struct aw_mmc_dma_desc)); 670c39ea909SEmmanuel Vadot } 671c39ea909SEmmanuel Vadot 672c39ea909SEmmanuel Vadot dma_desc[0].config |= AW_MMC_DMA_CONFIG_FD; 673c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config |= AW_MMC_DMA_CONFIG_LD | 674b5be541fSEmmanuel Vadot AW_MMC_DMA_CONFIG_ER; 675c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config &= ~AW_MMC_DMA_CONFIG_DIC; 676c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].next = 0; 677b5be541fSEmmanuel Vadot } 678b5be541fSEmmanuel Vadot 679b5be541fSEmmanuel Vadot static int 680b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc) 681b5be541fSEmmanuel Vadot { 682b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 683b5be541fSEmmanuel Vadot int error; 684b5be541fSEmmanuel Vadot struct mmc_command *cmd; 685b5be541fSEmmanuel Vadot uint32_t val; 686b5be541fSEmmanuel Vadot 6875e03278fSIlya Bakulin #ifdef MMCCAM 6885e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 6895e03278fSIlya Bakulin #else 690b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 6915e03278fSIlya Bakulin #endif 692ce0618beSEmmanuel Vadot if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS)) 693b5be541fSEmmanuel Vadot return (EFBIG); 694b5be541fSEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 695b5be541fSEmmanuel Vadot cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0); 696b5be541fSEmmanuel Vadot if (error) 697b5be541fSEmmanuel Vadot return (error); 698b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 699b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 700b5be541fSEmmanuel Vadot 701b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 702b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREWRITE; 703b5be541fSEmmanuel Vadot else 704b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREREAD; 705b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op); 706b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE); 707b5be541fSEmmanuel Vadot 708b5be541fSEmmanuel Vadot /* Enable DMA */ 709b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 710b091392eSEmmanuel Vadot val &= ~AW_MMC_GCTL_FIFO_AC_MOD; 711b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_ENB; 712b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 713b5be541fSEmmanuel Vadot 714b5be541fSEmmanuel Vadot /* Reset DMA */ 715b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_RST; 716b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 717b5be541fSEmmanuel Vadot 718b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST); 719b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, 720b5be541fSEmmanuel Vadot AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST); 721b5be541fSEmmanuel Vadot 722b5be541fSEmmanuel Vadot /* Enable RX or TX DMA interrupt */ 723a37d59c1SEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_IDIE); 724b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 725b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_TX_INT; 726b5be541fSEmmanuel Vadot else 727b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_RX_INT; 728b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val); 729b5be541fSEmmanuel Vadot 730b5be541fSEmmanuel Vadot /* Set DMA descritptor list address */ 731b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys); 732b5be541fSEmmanuel Vadot 733b5be541fSEmmanuel Vadot /* FIFO trigger level */ 734b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL); 735b5be541fSEmmanuel Vadot 736b5be541fSEmmanuel Vadot return (0); 737b5be541fSEmmanuel Vadot } 738b5be541fSEmmanuel Vadot 739b5be541fSEmmanuel Vadot static int 740b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc) 741b5be541fSEmmanuel Vadot { 742b091392eSEmmanuel Vadot uint32_t reg; 743b5be541fSEmmanuel Vadot int timeout; 744b5be541fSEmmanuel Vadot 745b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 746b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_RESET; 747b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 748c39ea909SEmmanuel Vadot timeout = AW_MMC_RESET_RETRY; 749b5be541fSEmmanuel Vadot while (--timeout > 0) { 750b091392eSEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_GCTL_RESET) == 0) 751b5be541fSEmmanuel Vadot break; 752b5be541fSEmmanuel Vadot DELAY(100); 753b5be541fSEmmanuel Vadot } 754b5be541fSEmmanuel Vadot if (timeout == 0) 755b5be541fSEmmanuel Vadot return (ETIMEDOUT); 756b5be541fSEmmanuel Vadot 75735a18619SEmmanuel Vadot return (0); 75835a18619SEmmanuel Vadot } 75935a18619SEmmanuel Vadot 76035a18619SEmmanuel Vadot static int 76135a18619SEmmanuel Vadot aw_mmc_init(struct aw_mmc_softc *sc) 76235a18619SEmmanuel Vadot { 763b091392eSEmmanuel Vadot uint32_t reg; 76435a18619SEmmanuel Vadot int ret; 76535a18619SEmmanuel Vadot 76635a18619SEmmanuel Vadot ret = aw_mmc_reset(sc); 76735a18619SEmmanuel Vadot if (ret != 0) 76835a18619SEmmanuel Vadot return (ret); 76935a18619SEmmanuel Vadot 770b5be541fSEmmanuel Vadot /* Set the timeout. */ 771b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_TMOR, 772b5be541fSEmmanuel Vadot AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) | 773b5be541fSEmmanuel Vadot AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK)); 774b5be541fSEmmanuel Vadot 77535a18619SEmmanuel Vadot /* Unmask interrupts. */ 77635a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, 0); 77735a18619SEmmanuel Vadot 778b5be541fSEmmanuel Vadot /* Clear pending interrupts. */ 779b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 78035a18619SEmmanuel Vadot 78135a18619SEmmanuel Vadot /* Debug register, undocumented */ 78235a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DBGC, 0xdeb); 78335a18619SEmmanuel Vadot 78435a18619SEmmanuel Vadot /* Function select register */ 78535a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FUNS, 0xceaa0000); 78635a18619SEmmanuel Vadot 787b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff); 78835a18619SEmmanuel Vadot 789b091392eSEmmanuel Vadot /* Enable interrupts and disable AHB access. */ 790b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 791b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_INT_ENB; 792b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_FIFO_AC_MOD; 793b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_WAIT_MEM_ACCESS; 794b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 795b5be541fSEmmanuel Vadot 796b5be541fSEmmanuel Vadot return (0); 797b5be541fSEmmanuel Vadot } 798b5be541fSEmmanuel Vadot 799b5be541fSEmmanuel Vadot static void 800b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc) 801b5be541fSEmmanuel Vadot { 802b5be541fSEmmanuel Vadot struct mmc_command *cmd; 8035e03278fSIlya Bakulin #ifdef MMCCAM 8045e03278fSIlya Bakulin union ccb *ccb; 8055e03278fSIlya Bakulin #else 806b5be541fSEmmanuel Vadot struct mmc_request *req; 8075e03278fSIlya Bakulin #endif 808b5be541fSEmmanuel Vadot uint32_t val, mask; 809b5be541fSEmmanuel Vadot int retry; 810b5be541fSEmmanuel Vadot 8115e03278fSIlya Bakulin #ifdef MMCCAM 8125e03278fSIlya Bakulin ccb = sc->ccb; 8135e03278fSIlya Bakulin cmd = &ccb->mmcio.cmd; 8145e03278fSIlya Bakulin #else 815b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 8165e03278fSIlya Bakulin #endif 8175e03278fSIlya Bakulin #ifdef DEBUG 8185e03278fSIlya Bakulin if (bootverbose) { 8195e03278fSIlya Bakulin device_printf(sc->aw_dev, "%s: cmd %d err %d\n", __func__, cmd->opcode, cmd->error); 8205e03278fSIlya Bakulin } 8215e03278fSIlya Bakulin #endif 822b5be541fSEmmanuel Vadot if (cmd->error != MMC_ERR_NONE) { 823b5be541fSEmmanuel Vadot /* Reset the FIFO and DMA engines. */ 824b091392eSEmmanuel Vadot mask = AW_MMC_GCTL_FIFO_RST | AW_MMC_GCTL_DMA_RST; 825b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 826b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask); 827b5be541fSEmmanuel Vadot 828b5be541fSEmmanuel Vadot retry = AW_MMC_RESET_RETRY; 829b5be541fSEmmanuel Vadot while (--retry > 0) { 830c39ea909SEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & 831c39ea909SEmmanuel Vadot AW_MMC_GCTL_RESET) == 0) 832b5be541fSEmmanuel Vadot break; 833c39ea909SEmmanuel Vadot DELAY(100); 834b5be541fSEmmanuel Vadot } 835b5be541fSEmmanuel Vadot if (retry == 0) 836b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 837b5be541fSEmmanuel Vadot "timeout resetting DMA/FIFO\n"); 838b5be541fSEmmanuel Vadot aw_mmc_update_clock(sc, 1); 839b5be541fSEmmanuel Vadot } 840b5be541fSEmmanuel Vadot 841b5be541fSEmmanuel Vadot callout_stop(&sc->aw_timeoutc); 842b5be541fSEmmanuel Vadot sc->aw_intr = 0; 843b5be541fSEmmanuel Vadot sc->aw_resid = 0; 844b5be541fSEmmanuel Vadot sc->aw_dma_map_err = 0; 845b5be541fSEmmanuel Vadot sc->aw_intr_wait = 0; 8465e03278fSIlya Bakulin #ifdef MMCCAM 8475e03278fSIlya Bakulin sc->ccb = NULL; 8485e03278fSIlya Bakulin ccb->ccb_h.status = 8495e03278fSIlya Bakulin (ccb->mmcio.cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 8505e03278fSIlya Bakulin xpt_done(ccb); 8515e03278fSIlya Bakulin #else 8525e03278fSIlya Bakulin req = sc->aw_req; 8535e03278fSIlya Bakulin sc->aw_req = NULL; 854b5be541fSEmmanuel Vadot req->done(req); 8555e03278fSIlya Bakulin #endif 856b5be541fSEmmanuel Vadot } 857b5be541fSEmmanuel Vadot 858b5be541fSEmmanuel Vadot static void 859b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc) 860b5be541fSEmmanuel Vadot { 861b5be541fSEmmanuel Vadot int timeout; 862b5be541fSEmmanuel Vadot struct mmc_command *cmd; 863b5be541fSEmmanuel Vadot uint32_t status; 864b5be541fSEmmanuel Vadot 865b5be541fSEmmanuel Vadot timeout = 1000; 866b5be541fSEmmanuel Vadot while (--timeout > 0) { 867b5be541fSEmmanuel Vadot status = AW_MMC_READ_4(sc, AW_MMC_STAR); 868b5be541fSEmmanuel Vadot if ((status & AW_MMC_STAR_CARD_BUSY) == 0) 869b5be541fSEmmanuel Vadot break; 870b5be541fSEmmanuel Vadot DELAY(1000); 871b5be541fSEmmanuel Vadot } 8725e03278fSIlya Bakulin #ifdef MMCCAM 8735e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 8745e03278fSIlya Bakulin #else 875b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 8765e03278fSIlya Bakulin #endif 877b5be541fSEmmanuel Vadot if (timeout == 0) { 878b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 879b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 880b5be541fSEmmanuel Vadot return; 881b5be541fSEmmanuel Vadot } 882b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) { 883b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) { 884b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3); 885b5be541fSEmmanuel Vadot cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2); 886b5be541fSEmmanuel Vadot cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1); 887b5be541fSEmmanuel Vadot cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 888b5be541fSEmmanuel Vadot } else 889b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 890b5be541fSEmmanuel Vadot } 891b5be541fSEmmanuel Vadot /* All data has been transferred ? */ 892b5be541fSEmmanuel Vadot if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len) 893b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 894b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 895b5be541fSEmmanuel Vadot } 896b5be541fSEmmanuel Vadot 8975e03278fSIlya Bakulin 8985e03278fSIlya Bakulin static inline void 8995e03278fSIlya Bakulin set_mmc_error(struct aw_mmc_softc *sc, int error_code) 9005e03278fSIlya Bakulin { 9015e03278fSIlya Bakulin #ifdef MMCCAM 9025e03278fSIlya Bakulin sc->ccb->mmcio.cmd.error = error_code; 9035e03278fSIlya Bakulin #else 9045e03278fSIlya Bakulin sc->aw_req->cmd->error = error_code; 9055e03278fSIlya Bakulin #endif 9065e03278fSIlya Bakulin } 9075e03278fSIlya Bakulin 908b5be541fSEmmanuel Vadot static void 909b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg) 910b5be541fSEmmanuel Vadot { 911b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 912b5be541fSEmmanuel Vadot 913b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 9145e03278fSIlya Bakulin #ifdef MMCCAM 9155e03278fSIlya Bakulin if (sc->ccb != NULL) { 9165e03278fSIlya Bakulin #else 917b5be541fSEmmanuel Vadot if (sc->aw_req != NULL) { 9185e03278fSIlya Bakulin #endif 919b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "controller timeout\n"); 9205e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 921b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 922b5be541fSEmmanuel Vadot } else 923b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 924b5be541fSEmmanuel Vadot "Spurious timeout - no active request\n"); 925b5be541fSEmmanuel Vadot } 926b5be541fSEmmanuel Vadot 927b5be541fSEmmanuel Vadot static void 9285e03278fSIlya Bakulin aw_mmc_print_error(uint32_t err) 9295e03278fSIlya Bakulin { 9305e03278fSIlya Bakulin if(err & AW_MMC_INT_RESP_ERR) 9315e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_ERR "); 9325e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_CRC_ERR) 9335e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_CRC_ERR "); 9345e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_CRC_ERR) 9355e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_CRC_ERR "); 9365e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_TIMEOUT) 9375e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_TIMEOUT "); 9385e03278fSIlya Bakulin if (err & AW_MMC_INT_FIFO_RUN_ERR) 9395e03278fSIlya Bakulin printf("AW_MMC_INT_FIFO_RUN_ERR "); 9405e03278fSIlya Bakulin if (err & AW_MMC_INT_CMD_BUSY) 9415e03278fSIlya Bakulin printf("AW_MMC_INT_CMD_BUSY "); 9425e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_START_ERR) 9435e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_START_ERR "); 9445e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_END_BIT_ERR) 9455e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_END_BIT_ERR"); 9465e03278fSIlya Bakulin printf("\n"); 9475e03278fSIlya Bakulin } 9485e03278fSIlya Bakulin 9495e03278fSIlya Bakulin static void 950b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg) 951b5be541fSEmmanuel Vadot { 952b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 953b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 954b5be541fSEmmanuel Vadot struct mmc_data *data; 955b5be541fSEmmanuel Vadot uint32_t idst, imask, rint; 956b5be541fSEmmanuel Vadot 957b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 958b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 959b5be541fSEmmanuel Vadot rint = AW_MMC_READ_4(sc, AW_MMC_RISR); 960b5be541fSEmmanuel Vadot idst = AW_MMC_READ_4(sc, AW_MMC_IDST); 961b5be541fSEmmanuel Vadot imask = AW_MMC_READ_4(sc, AW_MMC_IMKR); 962b5be541fSEmmanuel Vadot if (idst == 0 && imask == 0 && rint == 0) { 963b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 964b5be541fSEmmanuel Vadot return; 965b5be541fSEmmanuel Vadot } 966b5be541fSEmmanuel Vadot #ifdef DEBUG 967b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n", 968b5be541fSEmmanuel Vadot idst, imask, rint); 969b5be541fSEmmanuel Vadot #endif 9705e03278fSIlya Bakulin #ifdef MMCCAM 9715e03278fSIlya Bakulin if (sc->ccb == NULL) { 9725e03278fSIlya Bakulin #else 973b5be541fSEmmanuel Vadot if (sc->aw_req == NULL) { 9745e03278fSIlya Bakulin #endif 975b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 976b5be541fSEmmanuel Vadot "Spurious interrupt - no active request, rint: 0x%08X\n", 977b5be541fSEmmanuel Vadot rint); 9785e03278fSIlya Bakulin aw_mmc_print_error(rint); 979b5be541fSEmmanuel Vadot goto end; 980b5be541fSEmmanuel Vadot } 981b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_ERR_BIT) { 982ce0618beSEmmanuel Vadot if (bootverbose) 983b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint); 9845e03278fSIlya Bakulin aw_mmc_print_error(rint); 985b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_RESP_TIMEOUT) 9865e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 987b5be541fSEmmanuel Vadot else 9885e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 989b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 990b5be541fSEmmanuel Vadot goto end; 991b5be541fSEmmanuel Vadot } 992b5be541fSEmmanuel Vadot if (idst & AW_MMC_IDST_ERROR) { 993b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst); 9945e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 995b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 996b5be541fSEmmanuel Vadot goto end; 997b5be541fSEmmanuel Vadot } 998b5be541fSEmmanuel Vadot 999b5be541fSEmmanuel Vadot sc->aw_intr |= rint; 10005e03278fSIlya Bakulin #ifdef MMCCAM 10015e03278fSIlya Bakulin data = sc->ccb->mmcio.cmd.data; 10025e03278fSIlya Bakulin #else 1003b5be541fSEmmanuel Vadot data = sc->aw_req->cmd->data; 10045e03278fSIlya Bakulin #endif 1005b5be541fSEmmanuel Vadot if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) { 1006b5be541fSEmmanuel Vadot if (data->flags & MMC_DATA_WRITE) 1007b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTWRITE; 1008b5be541fSEmmanuel Vadot else 1009b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTREAD; 1010b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 1011b5be541fSEmmanuel Vadot sync_op); 1012b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, 1013b5be541fSEmmanuel Vadot BUS_DMASYNC_POSTWRITE); 1014b5be541fSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 1015b5be541fSEmmanuel Vadot sc->aw_resid = data->len >> 2; 1016b5be541fSEmmanuel Vadot } 1017b5be541fSEmmanuel Vadot if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait) 1018b5be541fSEmmanuel Vadot aw_mmc_req_ok(sc); 1019b5be541fSEmmanuel Vadot 1020b5be541fSEmmanuel Vadot end: 1021b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst); 1022b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint); 1023b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1024b5be541fSEmmanuel Vadot } 1025b5be541fSEmmanuel Vadot 1026b5be541fSEmmanuel Vadot static int 1027b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req) 1028b5be541fSEmmanuel Vadot { 1029b5be541fSEmmanuel Vadot int blksz; 1030b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1031b5be541fSEmmanuel Vadot struct mmc_command *cmd; 103235a18619SEmmanuel Vadot uint32_t cmdreg, imask; 1033b5be541fSEmmanuel Vadot int err; 1034b5be541fSEmmanuel Vadot 1035b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1036c39ea909SEmmanuel Vadot 1037b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 10385e03278fSIlya Bakulin #ifdef MMCCAM 10395e03278fSIlya Bakulin KASSERT(req == NULL, ("req should be NULL in MMCCAM case!")); 10405e03278fSIlya Bakulin /* 10415e03278fSIlya Bakulin * For MMCCAM, sc->ccb has been NULL-checked and populated 10425e03278fSIlya Bakulin * by aw_mmc_cam_request() already. 10435e03278fSIlya Bakulin */ 10445e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 10455e03278fSIlya Bakulin #else 1046b5be541fSEmmanuel Vadot if (sc->aw_req) { 1047b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1048b5be541fSEmmanuel Vadot return (EBUSY); 1049b5be541fSEmmanuel Vadot } 1050b5be541fSEmmanuel Vadot sc->aw_req = req; 1051b5be541fSEmmanuel Vadot cmd = req->cmd; 10525e03278fSIlya Bakulin 10535e03278fSIlya Bakulin #ifdef DEBUG 10545e03278fSIlya Bakulin if (bootverbose) 10555e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 10565e03278fSIlya Bakulin cmd->opcode, cmd->arg, cmd->flags, 10575e03278fSIlya Bakulin cmd->data != NULL ? (unsigned int)cmd->data->len : 0, 10585e03278fSIlya Bakulin cmd->data != NULL ? cmd->data->flags: 0); 10595e03278fSIlya Bakulin #endif 10605e03278fSIlya Bakulin #endif 1061b5be541fSEmmanuel Vadot cmdreg = AW_MMC_CMDR_LOAD; 106235a18619SEmmanuel Vadot imask = AW_MMC_INT_ERR_BIT; 106335a18619SEmmanuel Vadot sc->aw_intr_wait = 0; 106435a18619SEmmanuel Vadot sc->aw_intr = 0; 106535a18619SEmmanuel Vadot sc->aw_resid = 0; 106635a18619SEmmanuel Vadot cmd->error = MMC_ERR_NONE; 106735a18619SEmmanuel Vadot 1068b5be541fSEmmanuel Vadot if (cmd->opcode == MMC_GO_IDLE_STATE) 1069b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ; 107035a18619SEmmanuel Vadot 1071b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) 1072b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_RESP_RCV; 1073b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) 1074b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_LONG_RESP; 1075b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_CRC) 1076b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC; 1077b5be541fSEmmanuel Vadot 107835a18619SEmmanuel Vadot if (cmd->data) { 1079b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER; 108035a18619SEmmanuel Vadot 1081b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_MULTI) { 1082b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG; 108335a18619SEmmanuel Vadot imask |= AW_MMC_INT_AUTO_STOP_DONE; 1084b5be541fSEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE; 108535a18619SEmmanuel Vadot } else { 108635a18619SEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER; 108735a18619SEmmanuel Vadot imask |= AW_MMC_INT_DATA_OVER; 1088b5be541fSEmmanuel Vadot } 1089b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 1090b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DIR_WRITE; 10915d5ae066SIlya Bakulin #ifdef MMCCAM 10925d5ae066SIlya Bakulin if (cmd->data->flags & MMC_DATA_BLOCK_SIZE) { 10935d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BKSR, cmd->data->block_size); 10945d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 10955d5ae066SIlya Bakulin } else 10965d5ae066SIlya Bakulin #endif 10975d5ae066SIlya Bakulin { 1098440565daSBjoern A. Zeeb blksz = min(cmd->data->len, MMC_SECTOR_SIZE); 1099b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz); 1100b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 11015d5ae066SIlya Bakulin } 110235a18619SEmmanuel Vadot } else { 110335a18619SEmmanuel Vadot imask |= AW_MMC_INT_CMD_DONE; 110435a18619SEmmanuel Vadot } 1105b5be541fSEmmanuel Vadot 110635a18619SEmmanuel Vadot /* Enable the interrupts we are interested in */ 110735a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, imask); 110835a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 110935a18619SEmmanuel Vadot 111035a18619SEmmanuel Vadot /* Enable auto stop if needed */ 111135a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_A12A, 111235a18619SEmmanuel Vadot cmdreg & AW_MMC_CMDR_STOP_CMD_FLAG ? 0 : 0xffff); 111335a18619SEmmanuel Vadot 111435a18619SEmmanuel Vadot /* Write the command argument */ 111535a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg); 111635a18619SEmmanuel Vadot 111735a18619SEmmanuel Vadot /* 111835a18619SEmmanuel Vadot * If we don't have data start the request 111935a18619SEmmanuel Vadot * if we do prepare the dma request and start the request 112035a18619SEmmanuel Vadot */ 112135a18619SEmmanuel Vadot if (cmd->data == NULL) { 112235a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 112335a18619SEmmanuel Vadot } else { 1124b5be541fSEmmanuel Vadot err = aw_mmc_prepare_dma(sc); 1125b5be541fSEmmanuel Vadot if (err != 0) 1126b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err); 112735a18619SEmmanuel Vadot 112835a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 1129b5be541fSEmmanuel Vadot } 1130b5be541fSEmmanuel Vadot 1131b5be541fSEmmanuel Vadot callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz, 1132b5be541fSEmmanuel Vadot aw_mmc_timeout, sc); 1133b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1134b5be541fSEmmanuel Vadot 1135b5be541fSEmmanuel Vadot return (0); 1136b5be541fSEmmanuel Vadot } 1137b5be541fSEmmanuel Vadot 1138b5be541fSEmmanuel Vadot static int 1139b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which, 1140b5be541fSEmmanuel Vadot uintptr_t *result) 1141b5be541fSEmmanuel Vadot { 1142b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1143b5be541fSEmmanuel Vadot 1144b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1145b5be541fSEmmanuel Vadot switch (which) { 1146b5be541fSEmmanuel Vadot default: 1147b5be541fSEmmanuel Vadot return (EINVAL); 1148b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1149b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_mode; 1150b5be541fSEmmanuel Vadot break; 1151b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1152b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_width; 1153b5be541fSEmmanuel Vadot break; 1154b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1155b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.chip_select; 1156b5be541fSEmmanuel Vadot break; 1157b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1158b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.clock; 1159b5be541fSEmmanuel Vadot break; 1160b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1161b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_min; 1162b5be541fSEmmanuel Vadot break; 1163b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1164b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_max; 1165b5be541fSEmmanuel Vadot break; 1166b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1167b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.host_ocr; 1168b5be541fSEmmanuel Vadot break; 1169b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1170b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.mode; 1171b5be541fSEmmanuel Vadot break; 1172b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1173b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ocr; 1174b5be541fSEmmanuel Vadot break; 1175b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1176b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.power_mode; 1177b5be541fSEmmanuel Vadot break; 1178b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1179b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.vdd; 1180b5be541fSEmmanuel Vadot break; 1181dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1182dfb8c122SEmmanuel Vadot *(int *)result = sc->aw_host.ios.vccq; 1183dfb8c122SEmmanuel Vadot break; 1184b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1185b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.caps; 1186b5be541fSEmmanuel Vadot break; 1187ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1188ce0618beSEmmanuel Vadot *(int *)result = sc->aw_host.ios.timing; 1189ce0618beSEmmanuel Vadot break; 1190b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1191c39ea909SEmmanuel Vadot *(int *)result = (sc->aw_mmc_conf->dma_xferlen * 1192c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 1193b5be541fSEmmanuel Vadot break; 119455f3f71cSEmmanuel Vadot case MMCBR_IVAR_RETUNE_REQ: 119555f3f71cSEmmanuel Vadot *(int *)result = retune_req_none; 119655f3f71cSEmmanuel Vadot break; 1197b5be541fSEmmanuel Vadot } 1198b5be541fSEmmanuel Vadot 1199b5be541fSEmmanuel Vadot return (0); 1200b5be541fSEmmanuel Vadot } 1201b5be541fSEmmanuel Vadot 1202b5be541fSEmmanuel Vadot static int 1203b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which, 1204b5be541fSEmmanuel Vadot uintptr_t value) 1205b5be541fSEmmanuel Vadot { 1206b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1207b5be541fSEmmanuel Vadot 1208b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1209b5be541fSEmmanuel Vadot switch (which) { 1210b5be541fSEmmanuel Vadot default: 1211b5be541fSEmmanuel Vadot return (EINVAL); 1212b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1213b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_mode = value; 1214b5be541fSEmmanuel Vadot break; 1215b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1216b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_width = value; 1217b5be541fSEmmanuel Vadot break; 1218b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1219b5be541fSEmmanuel Vadot sc->aw_host.ios.chip_select = value; 1220b5be541fSEmmanuel Vadot break; 1221b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1222b5be541fSEmmanuel Vadot sc->aw_host.ios.clock = value; 1223b5be541fSEmmanuel Vadot break; 1224b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1225b5be541fSEmmanuel Vadot sc->aw_host.mode = value; 1226b5be541fSEmmanuel Vadot break; 1227b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1228b5be541fSEmmanuel Vadot sc->aw_host.ocr = value; 1229b5be541fSEmmanuel Vadot break; 1230b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1231b5be541fSEmmanuel Vadot sc->aw_host.ios.power_mode = value; 1232b5be541fSEmmanuel Vadot break; 1233b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1234b5be541fSEmmanuel Vadot sc->aw_host.ios.vdd = value; 1235b5be541fSEmmanuel Vadot break; 1236dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1237dfb8c122SEmmanuel Vadot sc->aw_host.ios.vccq = value; 1238dfb8c122SEmmanuel Vadot break; 1239ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1240ce0618beSEmmanuel Vadot sc->aw_host.ios.timing = value; 1241ce0618beSEmmanuel Vadot break; 1242b5be541fSEmmanuel Vadot /* These are read-only */ 1243b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1244b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1245b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1246b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1247b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1248b5be541fSEmmanuel Vadot return (EINVAL); 1249b5be541fSEmmanuel Vadot } 1250b5be541fSEmmanuel Vadot 1251b5be541fSEmmanuel Vadot return (0); 1252b5be541fSEmmanuel Vadot } 1253b5be541fSEmmanuel Vadot 1254b5be541fSEmmanuel Vadot static int 1255b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon) 1256b5be541fSEmmanuel Vadot { 1257ce0618beSEmmanuel Vadot uint32_t reg; 1258b5be541fSEmmanuel Vadot int retry; 1259b5be541fSEmmanuel Vadot 1260ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1261ffdb1aa8SEmmanuel Vadot reg &= ~(AW_MMC_CKCR_ENB | AW_MMC_CKCR_LOW_POWER | 1262ffdb1aa8SEmmanuel Vadot AW_MMC_CKCR_MASK_DATA0); 1263b5be541fSEmmanuel Vadot 1264b5be541fSEmmanuel Vadot if (clkon) 1265ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_ENB; 1266ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) 1267ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_MASK_DATA0; 1268b5be541fSEmmanuel Vadot 1269ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1270b5be541fSEmmanuel Vadot 1271ce0618beSEmmanuel Vadot reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK | 1272b5be541fSEmmanuel Vadot AW_MMC_CMDR_WAIT_PRE_OVER; 1273ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg); 1274b5be541fSEmmanuel Vadot retry = 0xfffff; 1275ce0618beSEmmanuel Vadot 1276ce0618beSEmmanuel Vadot while (reg & AW_MMC_CMDR_LOAD && --retry > 0) { 1277ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CMDR); 1278b5be541fSEmmanuel Vadot DELAY(10); 1279b5be541fSEmmanuel Vadot } 1280b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 1281b5be541fSEmmanuel Vadot 1282ce0618beSEmmanuel Vadot if (reg & AW_MMC_CMDR_LOAD) { 1283ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "timeout updating clock\n"); 1284b5be541fSEmmanuel Vadot return (ETIMEDOUT); 1285b5be541fSEmmanuel Vadot } 1286b5be541fSEmmanuel Vadot 1287ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) { 1288ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1289ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_MASK_DATA0; 1290ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1291ce0618beSEmmanuel Vadot } 1292ce0618beSEmmanuel Vadot 1293ce0618beSEmmanuel Vadot return (0); 1294ce0618beSEmmanuel Vadot } 1295ce0618beSEmmanuel Vadot 1296623966e1SEmmanuel Vadot static int 1297623966e1SEmmanuel Vadot aw_mmc_switch_vccq(device_t bus, device_t child) 1298ce0618beSEmmanuel Vadot { 1299623966e1SEmmanuel Vadot struct aw_mmc_softc *sc; 1300623966e1SEmmanuel Vadot int uvolt, err; 1301623966e1SEmmanuel Vadot 1302623966e1SEmmanuel Vadot sc = device_get_softc(bus); 1303ce0618beSEmmanuel Vadot 13043177f7cdSEmmanuel Vadot if (sc->aw_reg_vqmmc == NULL) 1305623966e1SEmmanuel Vadot return EOPNOTSUPP; 1306ce0618beSEmmanuel Vadot 1307623966e1SEmmanuel Vadot switch (sc->aw_host.ios.vccq) { 1308dfb8c122SEmmanuel Vadot case vccq_180: 1309dfb8c122SEmmanuel Vadot uvolt = 1800000; 1310ce0618beSEmmanuel Vadot break; 1311dfb8c122SEmmanuel Vadot case vccq_330: 1312dfb8c122SEmmanuel Vadot uvolt = 3300000; 1313ce0618beSEmmanuel Vadot break; 1314dfb8c122SEmmanuel Vadot default: 1315623966e1SEmmanuel Vadot return EINVAL; 1316ce0618beSEmmanuel Vadot } 1317ce0618beSEmmanuel Vadot 1318623966e1SEmmanuel Vadot err = regulator_set_voltage(sc->aw_reg_vqmmc, uvolt, uvolt); 1319623966e1SEmmanuel Vadot if (err != 0) { 1320ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, 1321ce0618beSEmmanuel Vadot "Cannot set vqmmc to %d<->%d\n", 1322dfb8c122SEmmanuel Vadot uvolt, 1323dfb8c122SEmmanuel Vadot uvolt); 1324623966e1SEmmanuel Vadot return (err); 1325623966e1SEmmanuel Vadot } 1326623966e1SEmmanuel Vadot 1327623966e1SEmmanuel Vadot return (0); 1328ce0618beSEmmanuel Vadot } 1329ce0618beSEmmanuel Vadot 1330b5be541fSEmmanuel Vadot static int 1331b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child) 1332b5be541fSEmmanuel Vadot { 1333b5be541fSEmmanuel Vadot int error; 1334b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1335b5be541fSEmmanuel Vadot struct mmc_ios *ios; 1336ce0618beSEmmanuel Vadot unsigned int clock; 1337ce0618beSEmmanuel Vadot uint32_t reg, div = 1; 1338b5be541fSEmmanuel Vadot 1339b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1340b5be541fSEmmanuel Vadot 1341b5be541fSEmmanuel Vadot ios = &sc->aw_host.ios; 1342b5be541fSEmmanuel Vadot 1343b5be541fSEmmanuel Vadot /* Set the bus width. */ 1344b5be541fSEmmanuel Vadot switch (ios->bus_width) { 1345b5be541fSEmmanuel Vadot case bus_width_1: 1346b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1); 1347b5be541fSEmmanuel Vadot break; 1348b5be541fSEmmanuel Vadot case bus_width_4: 1349b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4); 1350b5be541fSEmmanuel Vadot break; 1351b5be541fSEmmanuel Vadot case bus_width_8: 1352b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8); 1353b5be541fSEmmanuel Vadot break; 1354b5be541fSEmmanuel Vadot } 1355b5be541fSEmmanuel Vadot 135635a18619SEmmanuel Vadot switch (ios->power_mode) { 135735a18619SEmmanuel Vadot case power_on: 135835a18619SEmmanuel Vadot break; 135935a18619SEmmanuel Vadot case power_off: 1360ce0618beSEmmanuel Vadot if (bootverbose) 1361ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "Powering down sd/mmc\n"); 1362dfb8c122SEmmanuel Vadot 1363dfb8c122SEmmanuel Vadot if (sc->aw_reg_vmmc) 1364dfb8c122SEmmanuel Vadot regulator_disable(sc->aw_reg_vmmc); 1365dfb8c122SEmmanuel Vadot if (sc->aw_reg_vqmmc) 1366dfb8c122SEmmanuel Vadot regulator_disable(sc->aw_reg_vqmmc); 1367dfb8c122SEmmanuel Vadot 136835a18619SEmmanuel Vadot aw_mmc_reset(sc); 136935a18619SEmmanuel Vadot break; 137035a18619SEmmanuel Vadot case power_up: 137135a18619SEmmanuel Vadot if (bootverbose) 137235a18619SEmmanuel Vadot device_printf(sc->aw_dev, "Powering up sd/mmc\n"); 1373dfb8c122SEmmanuel Vadot 1374dfb8c122SEmmanuel Vadot if (sc->aw_reg_vmmc) 1375dfb8c122SEmmanuel Vadot regulator_enable(sc->aw_reg_vmmc); 1376dfb8c122SEmmanuel Vadot if (sc->aw_reg_vqmmc) 1377dfb8c122SEmmanuel Vadot regulator_enable(sc->aw_reg_vqmmc); 137835a18619SEmmanuel Vadot aw_mmc_init(sc); 137935a18619SEmmanuel Vadot break; 138035a18619SEmmanuel Vadot }; 1381ce0618beSEmmanuel Vadot 1382ce0618beSEmmanuel Vadot /* Enable ddr mode if needed */ 1383ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 1384ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_uhs_ddr50 || 1385ce0618beSEmmanuel Vadot ios->timing == bus_timing_mmc_ddr52) 1386b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_DDR_MOD_SEL; 1387ce0618beSEmmanuel Vadot else 1388b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_DDR_MOD_SEL; 1389ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 1390ce0618beSEmmanuel Vadot 13910f7a6420SEmmanuel Vadot if (ios->clock && ios->clock != sc->aw_clock) { 13920f7a6420SEmmanuel Vadot sc->aw_clock = clock = ios->clock; 1393b5be541fSEmmanuel Vadot 1394b5be541fSEmmanuel Vadot /* Disable clock */ 1395b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 0); 1396b5be541fSEmmanuel Vadot if (error != 0) 1397b5be541fSEmmanuel Vadot return (error); 1398b5be541fSEmmanuel Vadot 1399ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_mmc_ddr52 && 1400ce0618beSEmmanuel Vadot (sc->aw_mmc_conf->new_timing || 1401ce0618beSEmmanuel Vadot ios->bus_width == bus_width_8)) { 1402ce0618beSEmmanuel Vadot div = 2; 1403ce0618beSEmmanuel Vadot clock <<= 1; 1404ce0618beSEmmanuel Vadot } 1405ce0618beSEmmanuel Vadot 1406b5be541fSEmmanuel Vadot /* Reset the divider. */ 1407ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1408ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_DIV; 1409ce0618beSEmmanuel Vadot reg |= div - 1; 1410ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1411ce0618beSEmmanuel Vadot 1412ce0618beSEmmanuel Vadot /* New timing mode if needed */ 1413ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->new_timing) { 1414ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_NTSR); 1415ce0618beSEmmanuel Vadot reg |= AW_MMC_NTSR_MODE_SELECT; 1416ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg); 1417ce0618beSEmmanuel Vadot } 1418b5be541fSEmmanuel Vadot 1419b5be541fSEmmanuel Vadot /* Set the MMC clock. */ 1420101260f3SEmmanuel Vadot error = clk_disable(sc->aw_clk_mmc); 1421101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1422101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1423101260f3SEmmanuel Vadot "failed to disable mmc clock: %d\n", error); 1424ce0618beSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, clock, 1425b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 1426b5be541fSEmmanuel Vadot if (error != 0) { 1427b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 1428b5be541fSEmmanuel Vadot "failed to set frequency to %u Hz: %d\n", 1429ce0618beSEmmanuel Vadot clock, error); 1430b5be541fSEmmanuel Vadot return (error); 1431b5be541fSEmmanuel Vadot } 1432101260f3SEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 1433101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1434101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1435101260f3SEmmanuel Vadot "failed to re-enable mmc clock: %d\n", error); 1436b5be541fSEmmanuel Vadot 1437ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->can_calibrate) 1438ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN); 1439ce0618beSEmmanuel Vadot 1440b5be541fSEmmanuel Vadot /* Enable clock. */ 1441b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 1); 1442b5be541fSEmmanuel Vadot if (error != 0) 1443b5be541fSEmmanuel Vadot return (error); 1444b5be541fSEmmanuel Vadot } 1445b5be541fSEmmanuel Vadot 1446b5be541fSEmmanuel Vadot 1447b5be541fSEmmanuel Vadot return (0); 1448b5be541fSEmmanuel Vadot } 1449b5be541fSEmmanuel Vadot 1450b5be541fSEmmanuel Vadot static int 1451b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child) 1452b5be541fSEmmanuel Vadot { 1453b5be541fSEmmanuel Vadot 1454b5be541fSEmmanuel Vadot return (0); 1455b5be541fSEmmanuel Vadot } 1456b5be541fSEmmanuel Vadot 1457b5be541fSEmmanuel Vadot static int 1458b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child) 1459b5be541fSEmmanuel Vadot { 1460b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1461b5be541fSEmmanuel Vadot int error; 1462b5be541fSEmmanuel Vadot 1463b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1464b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1465b5be541fSEmmanuel Vadot while (sc->aw_bus_busy) { 1466b5be541fSEmmanuel Vadot error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0); 1467b5be541fSEmmanuel Vadot if (error != 0) { 1468b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1469b5be541fSEmmanuel Vadot return (error); 1470b5be541fSEmmanuel Vadot } 1471b5be541fSEmmanuel Vadot } 1472b5be541fSEmmanuel Vadot sc->aw_bus_busy++; 1473b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1474b5be541fSEmmanuel Vadot 1475b5be541fSEmmanuel Vadot return (0); 1476b5be541fSEmmanuel Vadot } 1477b5be541fSEmmanuel Vadot 1478b5be541fSEmmanuel Vadot static int 1479b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child) 1480b5be541fSEmmanuel Vadot { 1481b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1482b5be541fSEmmanuel Vadot 1483b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1484b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1485b5be541fSEmmanuel Vadot sc->aw_bus_busy--; 1486b5be541fSEmmanuel Vadot wakeup(sc); 1487b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1488b5be541fSEmmanuel Vadot 1489b5be541fSEmmanuel Vadot return (0); 1490b5be541fSEmmanuel Vadot } 1491b5be541fSEmmanuel Vadot 1492b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = { 1493b5be541fSEmmanuel Vadot /* Device interface */ 1494b5be541fSEmmanuel Vadot DEVMETHOD(device_probe, aw_mmc_probe), 1495b5be541fSEmmanuel Vadot DEVMETHOD(device_attach, aw_mmc_attach), 1496b5be541fSEmmanuel Vadot DEVMETHOD(device_detach, aw_mmc_detach), 1497b5be541fSEmmanuel Vadot 1498b5be541fSEmmanuel Vadot /* Bus interface */ 1499b5be541fSEmmanuel Vadot DEVMETHOD(bus_read_ivar, aw_mmc_read_ivar), 1500b5be541fSEmmanuel Vadot DEVMETHOD(bus_write_ivar, aw_mmc_write_ivar), 1501ef546520SIlya Bakulin DEVMETHOD(bus_add_child, bus_generic_add_child), 1502b5be541fSEmmanuel Vadot 1503b5be541fSEmmanuel Vadot /* MMC bridge interface */ 1504b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_update_ios, aw_mmc_update_ios), 1505b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_request, aw_mmc_request), 1506b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_get_ro, aw_mmc_get_ro), 1507623966e1SEmmanuel Vadot DEVMETHOD(mmcbr_switch_vccq, aw_mmc_switch_vccq), 1508b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_acquire_host, aw_mmc_acquire_host), 1509b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_release_host, aw_mmc_release_host), 1510b5be541fSEmmanuel Vadot 1511b5be541fSEmmanuel Vadot DEVMETHOD_END 1512b5be541fSEmmanuel Vadot }; 1513b5be541fSEmmanuel Vadot 1514b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass; 1515b5be541fSEmmanuel Vadot 1516b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = { 1517b5be541fSEmmanuel Vadot "aw_mmc", 1518b5be541fSEmmanuel Vadot aw_mmc_methods, 1519b5be541fSEmmanuel Vadot sizeof(struct aw_mmc_softc), 1520b5be541fSEmmanuel Vadot }; 1521b5be541fSEmmanuel Vadot 1522b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL, 1523b5be541fSEmmanuel Vadot NULL); 15245e03278fSIlya Bakulin #ifndef MMCCAM 1525b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc); 15265e03278fSIlya Bakulin #endif 1527