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: 2165e03278fSIlya Bakulin { 2175e03278fSIlya Bakulin struct ccb_pathinq *cpi; 2185e03278fSIlya Bakulin 2195e03278fSIlya Bakulin cpi = &ccb->cpi; 2205e03278fSIlya Bakulin cpi->version_num = 1; 2215e03278fSIlya Bakulin cpi->hba_inquiry = 0; 2225e03278fSIlya Bakulin cpi->target_sprt = 0; 2235e03278fSIlya Bakulin cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN; 2245e03278fSIlya Bakulin cpi->hba_eng_cnt = 0; 2255e03278fSIlya Bakulin cpi->max_target = 0; 2265e03278fSIlya Bakulin cpi->max_lun = 0; 2275e03278fSIlya Bakulin cpi->initiator_id = 1; 2285e03278fSIlya Bakulin cpi->maxio = (sc->aw_mmc_conf->dma_xferlen * 2295e03278fSIlya Bakulin AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 2305e03278fSIlya Bakulin strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2315e03278fSIlya Bakulin strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN); 2325e03278fSIlya Bakulin strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2335e03278fSIlya Bakulin cpi->unit_number = cam_sim_unit(sim); 2345e03278fSIlya Bakulin cpi->bus_id = cam_sim_bus(sim); 2355e03278fSIlya Bakulin cpi->protocol = PROTO_MMCSD; 2365e03278fSIlya Bakulin cpi->protocol_version = SCSI_REV_0; 2375e03278fSIlya Bakulin cpi->transport = XPORT_MMCSD; 2385e03278fSIlya Bakulin cpi->transport_version = 1; 2395e03278fSIlya Bakulin 2405e03278fSIlya Bakulin cpi->ccb_h.status = CAM_REQ_CMP; 2415e03278fSIlya Bakulin break; 2425e03278fSIlya Bakulin } 2435e03278fSIlya Bakulin case XPT_GET_TRAN_SETTINGS: 2445e03278fSIlya Bakulin { 2455e03278fSIlya Bakulin struct ccb_trans_settings *cts = &ccb->cts; 2465e03278fSIlya Bakulin 2475e03278fSIlya Bakulin if (bootverbose) 2485e03278fSIlya Bakulin device_printf(sc->aw_dev, "Got XPT_GET_TRAN_SETTINGS\n"); 2495e03278fSIlya Bakulin 2505e03278fSIlya Bakulin cts->protocol = PROTO_MMCSD; 2515e03278fSIlya Bakulin cts->protocol_version = 1; 2525e03278fSIlya Bakulin cts->transport = XPORT_MMCSD; 2535e03278fSIlya Bakulin cts->transport_version = 1; 2545e03278fSIlya Bakulin cts->xport_specific.valid = 0; 2555e03278fSIlya Bakulin cts->proto_specific.mmc.host_ocr = sc->aw_host.host_ocr; 2565e03278fSIlya Bakulin cts->proto_specific.mmc.host_f_min = sc->aw_host.f_min; 2575e03278fSIlya Bakulin cts->proto_specific.mmc.host_f_max = sc->aw_host.f_max; 2585e03278fSIlya Bakulin cts->proto_specific.mmc.host_caps = sc->aw_host.caps; 2595d20e651SIlya Bakulin cts->proto_specific.mmc.host_max_data = (sc->aw_mmc_conf->dma_xferlen * 2605d20e651SIlya Bakulin AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 2615e03278fSIlya Bakulin memcpy(&cts->proto_specific.mmc.ios, &sc->aw_host.ios, sizeof(struct mmc_ios)); 2625e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_CMP; 2635e03278fSIlya Bakulin break; 2645e03278fSIlya Bakulin } 2655e03278fSIlya Bakulin case XPT_SET_TRAN_SETTINGS: 2665e03278fSIlya Bakulin { 2675e03278fSIlya Bakulin if (bootverbose) 2685e03278fSIlya Bakulin device_printf(sc->aw_dev, "Got XPT_SET_TRAN_SETTINGS\n"); 2695e03278fSIlya Bakulin aw_mmc_cam_settran_settings(sc, ccb); 2705e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_CMP; 2715e03278fSIlya Bakulin break; 2725e03278fSIlya Bakulin } 2735e03278fSIlya Bakulin case XPT_RESET_BUS: 2745e03278fSIlya Bakulin if (bootverbose) 2755e03278fSIlya Bakulin device_printf(sc->aw_dev, "Got XPT_RESET_BUS, ACK it...\n"); 2765e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_CMP; 2775e03278fSIlya Bakulin break; 2785e03278fSIlya Bakulin case XPT_MMC_IO: 2795e03278fSIlya Bakulin /* 2805e03278fSIlya Bakulin * Here is the HW-dependent part of 2815e03278fSIlya Bakulin * sending the command to the underlying h/w 2825e03278fSIlya Bakulin * At some point in the future an interrupt comes. 2835e03278fSIlya Bakulin * Then the request will be marked as completed. 2845e03278fSIlya Bakulin */ 2855e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_INPROG; 2865e03278fSIlya Bakulin 2875e03278fSIlya Bakulin aw_mmc_cam_handle_mmcio(sim, ccb); 2885e03278fSIlya Bakulin return; 2895e03278fSIlya Bakulin /* NOTREACHED */ 2905e03278fSIlya Bakulin break; 2915e03278fSIlya Bakulin default: 2925e03278fSIlya Bakulin ccb->ccb_h.status = CAM_REQ_INVALID; 2935e03278fSIlya Bakulin break; 2945e03278fSIlya Bakulin } 2955e03278fSIlya Bakulin xpt_done(ccb); 2965e03278fSIlya Bakulin return; 2975e03278fSIlya Bakulin } 2985e03278fSIlya Bakulin 2995e03278fSIlya Bakulin static void 3005e03278fSIlya Bakulin aw_mmc_cam_poll(struct cam_sim *sim) 3015e03278fSIlya Bakulin { 3025e03278fSIlya Bakulin return; 3035e03278fSIlya Bakulin } 3045e03278fSIlya Bakulin 3055e03278fSIlya Bakulin static int 3065e03278fSIlya Bakulin aw_mmc_cam_settran_settings(struct aw_mmc_softc *sc, union ccb *ccb) 3075e03278fSIlya Bakulin { 3085e03278fSIlya Bakulin struct mmc_ios *ios; 3095e03278fSIlya Bakulin struct mmc_ios *new_ios; 3105e03278fSIlya Bakulin struct ccb_trans_settings_mmc *cts; 3115e03278fSIlya Bakulin 3125e03278fSIlya Bakulin ios = &sc->aw_host.ios; 3135e03278fSIlya Bakulin 3145e03278fSIlya Bakulin cts = &ccb->cts.proto_specific.mmc; 3155e03278fSIlya Bakulin new_ios = &cts->ios; 3165e03278fSIlya Bakulin 3175e03278fSIlya Bakulin /* Update only requested fields */ 3185e03278fSIlya Bakulin if (cts->ios_valid & MMC_CLK) { 3195e03278fSIlya Bakulin ios->clock = new_ios->clock; 3205e03278fSIlya Bakulin device_printf(sc->aw_dev, "Clock => %d\n", ios->clock); 3215e03278fSIlya Bakulin } 3225e03278fSIlya Bakulin if (cts->ios_valid & MMC_VDD) { 3235e03278fSIlya Bakulin ios->vdd = new_ios->vdd; 3245e03278fSIlya Bakulin device_printf(sc->aw_dev, "VDD => %d\n", ios->vdd); 3255e03278fSIlya Bakulin } 3265e03278fSIlya Bakulin if (cts->ios_valid & MMC_CS) { 3275e03278fSIlya Bakulin ios->chip_select = new_ios->chip_select; 3285e03278fSIlya Bakulin device_printf(sc->aw_dev, "CS => %d\n", ios->chip_select); 3295e03278fSIlya Bakulin } 3305e03278fSIlya Bakulin if (cts->ios_valid & MMC_BW) { 3315e03278fSIlya Bakulin ios->bus_width = new_ios->bus_width; 3325e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus width => %d\n", ios->bus_width); 3335e03278fSIlya Bakulin } 3345e03278fSIlya Bakulin if (cts->ios_valid & MMC_PM) { 3355e03278fSIlya Bakulin ios->power_mode = new_ios->power_mode; 3365e03278fSIlya Bakulin device_printf(sc->aw_dev, "Power mode => %d\n", ios->power_mode); 3375e03278fSIlya Bakulin } 3385e03278fSIlya Bakulin if (cts->ios_valid & MMC_BT) { 3395e03278fSIlya Bakulin ios->timing = new_ios->timing; 3405e03278fSIlya Bakulin device_printf(sc->aw_dev, "Timing => %d\n", ios->timing); 3415e03278fSIlya Bakulin } 3425e03278fSIlya Bakulin if (cts->ios_valid & MMC_BM) { 3435e03278fSIlya Bakulin ios->bus_mode = new_ios->bus_mode; 3445e03278fSIlya Bakulin device_printf(sc->aw_dev, "Bus mode => %d\n", ios->bus_mode); 3455e03278fSIlya Bakulin } 3465e03278fSIlya Bakulin 3475e03278fSIlya Bakulin return (aw_mmc_update_ios(sc->aw_dev, NULL)); 3485e03278fSIlya Bakulin } 3495e03278fSIlya Bakulin 3505e03278fSIlya Bakulin static int 3515e03278fSIlya Bakulin aw_mmc_cam_request(struct aw_mmc_softc *sc, union ccb *ccb) 3525e03278fSIlya Bakulin { 3535e03278fSIlya Bakulin struct ccb_mmcio *mmcio; 3545e03278fSIlya Bakulin 3555e03278fSIlya Bakulin mmcio = &ccb->mmcio; 3565e03278fSIlya Bakulin 3575e03278fSIlya Bakulin AW_MMC_LOCK(sc); 3585e03278fSIlya Bakulin 3595e03278fSIlya Bakulin #ifdef DEBUG 3605e03278fSIlya Bakulin if (__predict_false(bootverbose)) { 3615e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 3625e03278fSIlya Bakulin mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 3635e03278fSIlya Bakulin mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 3645e03278fSIlya Bakulin mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0); 3655e03278fSIlya Bakulin } 3665e03278fSIlya Bakulin #endif 3675e03278fSIlya Bakulin if (mmcio->cmd.data != NULL) { 3685e03278fSIlya Bakulin if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 3695e03278fSIlya Bakulin panic("data->len = %d, data->flags = %d -- something is b0rked", 3705e03278fSIlya Bakulin (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 3715e03278fSIlya Bakulin } 3725e03278fSIlya Bakulin if (sc->ccb != NULL) { 3735e03278fSIlya Bakulin device_printf(sc->aw_dev, "Controller still has an active command\n"); 3745e03278fSIlya Bakulin return (EBUSY); 3755e03278fSIlya Bakulin } 3765e03278fSIlya Bakulin sc->ccb = ccb; 3775e03278fSIlya Bakulin /* aw_mmc_request locks again */ 3785e03278fSIlya Bakulin AW_MMC_UNLOCK(sc); 3795e03278fSIlya Bakulin aw_mmc_request(sc->aw_dev, NULL, NULL); 3805e03278fSIlya Bakulin 3815e03278fSIlya Bakulin return (0); 3825e03278fSIlya Bakulin } 3835e03278fSIlya Bakulin #endif /* MMCCAM */ 3845e03278fSIlya Bakulin 385b5be541fSEmmanuel Vadot static int 386b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev) 387b5be541fSEmmanuel Vadot { 388b5be541fSEmmanuel Vadot 389b5be541fSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 390b5be541fSEmmanuel Vadot return (ENXIO); 391b5be541fSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 392b5be541fSEmmanuel Vadot return (ENXIO); 393b5be541fSEmmanuel Vadot 394b5be541fSEmmanuel Vadot device_set_desc(dev, "Allwinner Integrated MMC/SD controller"); 395b5be541fSEmmanuel Vadot 396b5be541fSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 397b5be541fSEmmanuel Vadot } 398b5be541fSEmmanuel Vadot 399b5be541fSEmmanuel Vadot static int 400b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev) 401b5be541fSEmmanuel Vadot { 402b5be541fSEmmanuel Vadot device_t child; 403b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 404b5be541fSEmmanuel Vadot struct sysctl_ctx_list *ctx; 405b5be541fSEmmanuel Vadot struct sysctl_oid_list *tree; 406bbf8c8faSEmmanuel Vadot uint32_t bus_width, max_freq; 407b5be541fSEmmanuel Vadot phandle_t node; 408b5be541fSEmmanuel Vadot int error; 409b5be541fSEmmanuel Vadot 410b5be541fSEmmanuel Vadot node = ofw_bus_get_node(dev); 411b5be541fSEmmanuel Vadot sc = device_get_softc(dev); 412b5be541fSEmmanuel Vadot sc->aw_dev = dev; 413ce0618beSEmmanuel Vadot 414ce0618beSEmmanuel Vadot sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 415ce0618beSEmmanuel Vadot 4165e03278fSIlya Bakulin #ifndef MMCCAM 417b5be541fSEmmanuel Vadot sc->aw_req = NULL; 4185e03278fSIlya Bakulin #endif 419b5be541fSEmmanuel Vadot if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) { 420b5be541fSEmmanuel Vadot device_printf(dev, "cannot allocate device resources\n"); 421b5be541fSEmmanuel Vadot return (ENXIO); 422b5be541fSEmmanuel Vadot } 423b5be541fSEmmanuel Vadot if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES], 424b5be541fSEmmanuel Vadot INTR_TYPE_MISC | INTR_MPSAFE, NULL, aw_mmc_intr, sc, 425b5be541fSEmmanuel Vadot &sc->aw_intrhand)) { 426b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 427b5be541fSEmmanuel Vadot device_printf(dev, "cannot setup interrupt handler\n"); 428b5be541fSEmmanuel Vadot return (ENXIO); 429b5be541fSEmmanuel Vadot } 430b5be541fSEmmanuel Vadot mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc", 431b5be541fSEmmanuel Vadot MTX_DEF); 432b5be541fSEmmanuel Vadot callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0); 433b5be541fSEmmanuel Vadot 434b5be541fSEmmanuel Vadot /* De-assert reset */ 435b5be541fSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) { 436b5be541fSEmmanuel Vadot error = hwreset_deassert(sc->aw_rst_ahb); 437b5be541fSEmmanuel Vadot if (error != 0) { 438b5be541fSEmmanuel Vadot device_printf(dev, "cannot de-assert reset\n"); 439b5be541fSEmmanuel Vadot goto fail; 440b5be541fSEmmanuel Vadot } 441b5be541fSEmmanuel Vadot } 442b5be541fSEmmanuel Vadot 443b5be541fSEmmanuel Vadot /* Activate the module clock. */ 444b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb); 445b5be541fSEmmanuel Vadot if (error != 0) { 446b5be541fSEmmanuel Vadot device_printf(dev, "cannot get ahb clock\n"); 447b5be541fSEmmanuel Vadot goto fail; 448b5be541fSEmmanuel Vadot } 449b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_ahb); 450b5be541fSEmmanuel Vadot if (error != 0) { 451b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable ahb clock\n"); 452b5be541fSEmmanuel Vadot goto fail; 453b5be541fSEmmanuel Vadot } 454b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc); 455b5be541fSEmmanuel Vadot if (error != 0) { 456b5be541fSEmmanuel Vadot device_printf(dev, "cannot get mmc clock\n"); 457b5be541fSEmmanuel Vadot goto fail; 458b5be541fSEmmanuel Vadot } 459b5be541fSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY, 460b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 461b5be541fSEmmanuel Vadot if (error != 0) { 462b5be541fSEmmanuel Vadot device_printf(dev, "cannot init mmc clock\n"); 463b5be541fSEmmanuel Vadot goto fail; 464b5be541fSEmmanuel Vadot } 465b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 466b5be541fSEmmanuel Vadot if (error != 0) { 467b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable mmc clock\n"); 468b5be541fSEmmanuel Vadot goto fail; 469b5be541fSEmmanuel Vadot } 470b5be541fSEmmanuel Vadot 471b5be541fSEmmanuel Vadot sc->aw_timeout = 10; 472b5be541fSEmmanuel Vadot ctx = device_get_sysctl_ctx(dev); 473b5be541fSEmmanuel Vadot tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 474b5be541fSEmmanuel Vadot SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW, 475b5be541fSEmmanuel Vadot &sc->aw_timeout, 0, "Request timeout in seconds"); 476b5be541fSEmmanuel Vadot 477b5be541fSEmmanuel Vadot /* Soft Reset controller. */ 478b5be541fSEmmanuel Vadot if (aw_mmc_reset(sc) != 0) { 479b5be541fSEmmanuel Vadot device_printf(dev, "cannot reset the controller\n"); 480b5be541fSEmmanuel Vadot goto fail; 481b5be541fSEmmanuel Vadot } 482b5be541fSEmmanuel Vadot 483b5be541fSEmmanuel Vadot if (aw_mmc_setup_dma(sc) != 0) { 484b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "Couldn't setup DMA!\n"); 485b5be541fSEmmanuel Vadot goto fail; 486b5be541fSEmmanuel Vadot } 487b5be541fSEmmanuel Vadot 488b5be541fSEmmanuel Vadot if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0) 489b5be541fSEmmanuel Vadot bus_width = 4; 490b5be541fSEmmanuel Vadot 491ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply", 4923177f7cdSEmmanuel Vadot &sc->aw_reg_vmmc) == 0) { 4933177f7cdSEmmanuel Vadot if (bootverbose) 494ce0618beSEmmanuel Vadot device_printf(dev, "vmmc-supply regulator found\n"); 4953177f7cdSEmmanuel Vadot } 496ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply", 4973177f7cdSEmmanuel Vadot &sc->aw_reg_vqmmc) == 0 && bootverbose) { 4983177f7cdSEmmanuel Vadot if (bootverbose) 499ce0618beSEmmanuel Vadot device_printf(dev, "vqmmc-supply regulator found\n"); 5003177f7cdSEmmanuel Vadot } 501ce0618beSEmmanuel Vadot 502b5be541fSEmmanuel Vadot sc->aw_host.f_min = 400000; 503bbf8c8faSEmmanuel Vadot 504bbf8c8faSEmmanuel Vadot if (OF_getencprop(node, "max-frequency", &max_freq, 505bbf8c8faSEmmanuel Vadot sizeof(uint32_t)) <= 0) 506bbf8c8faSEmmanuel Vadot max_freq = 52000000; 507bbf8c8faSEmmanuel Vadot sc->aw_host.f_max = max_freq; 508bbf8c8faSEmmanuel Vadot 509b5be541fSEmmanuel Vadot sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340; 510ce0618beSEmmanuel Vadot sc->aw_host.caps = MMC_CAP_HSPEED | MMC_CAP_UHS_SDR12 | 511ce0618beSEmmanuel Vadot MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 | 512ce0618beSEmmanuel Vadot MMC_CAP_UHS_DDR50 | MMC_CAP_MMC_DDR52; 513ce0618beSEmmanuel Vadot 514*94d3675eSEmmanuel Vadot if (sc->aw_reg_vqmmc != NULL) { 515*94d3675eSEmmanuel Vadot if (regulator_check_voltage(sc->aw_reg_vqmmc, 1800000) == 0) 516*94d3675eSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_180; 517*94d3675eSEmmanuel Vadot if (regulator_check_voltage(sc->aw_reg_vqmmc, 3300000) == 0) 518*94d3675eSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_330; 519*94d3675eSEmmanuel Vadot } else 520*94d3675eSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_330; 521ce0618beSEmmanuel Vadot 522b5be541fSEmmanuel Vadot if (bus_width >= 4) 523b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_4_BIT_DATA; 524b5be541fSEmmanuel Vadot if (bus_width >= 8) 525b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_8_BIT_DATA; 526b5be541fSEmmanuel Vadot 5275e03278fSIlya Bakulin #ifdef MMCCAM 5285e03278fSIlya Bakulin child = NULL; /* Not used by MMCCAM, need to silence compiler warnings */ 5295e03278fSIlya Bakulin sc->ccb = NULL; 5305e03278fSIlya Bakulin if ((sc->devq = cam_simq_alloc(1)) == NULL) { 5315e03278fSIlya Bakulin goto fail; 5325e03278fSIlya Bakulin } 5335e03278fSIlya Bakulin 5345e03278fSIlya Bakulin mtx_init(&sc->sim_mtx, "awmmcsim", NULL, MTX_DEF); 535ef546520SIlya Bakulin sc->sim = cam_sim_alloc_dev(aw_mmc_cam_action, aw_mmc_cam_poll, 536ef546520SIlya Bakulin "aw_mmc_sim", sc, dev, 5375e03278fSIlya Bakulin &sc->sim_mtx, 1, 1, sc->devq); 5385e03278fSIlya Bakulin 5395e03278fSIlya Bakulin if (sc->sim == NULL) { 5405e03278fSIlya Bakulin cam_simq_free(sc->devq); 5415e03278fSIlya Bakulin device_printf(dev, "cannot allocate CAM SIM\n"); 5425e03278fSIlya Bakulin goto fail; 5435e03278fSIlya Bakulin } 5445e03278fSIlya Bakulin 5455e03278fSIlya Bakulin mtx_lock(&sc->sim_mtx); 5465e03278fSIlya Bakulin if (xpt_bus_register(sc->sim, sc->aw_dev, 0) != 0) { 5475e03278fSIlya Bakulin device_printf(dev, "cannot register SCSI pass-through bus\n"); 5485e03278fSIlya Bakulin cam_sim_free(sc->sim, FALSE); 5495e03278fSIlya Bakulin cam_simq_free(sc->devq); 5505e03278fSIlya Bakulin mtx_unlock(&sc->sim_mtx); 5515e03278fSIlya Bakulin goto fail; 5525e03278fSIlya Bakulin } 5535e03278fSIlya Bakulin 5545e03278fSIlya Bakulin mtx_unlock(&sc->sim_mtx); 5555e03278fSIlya Bakulin #else /* !MMCCAM */ 556b5be541fSEmmanuel Vadot child = device_add_child(dev, "mmc", -1); 557b5be541fSEmmanuel Vadot if (child == NULL) { 558b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC bus failed!\n"); 559b5be541fSEmmanuel Vadot goto fail; 560b5be541fSEmmanuel Vadot } 561b5be541fSEmmanuel Vadot if (device_probe_and_attach(child) != 0) { 562b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC child failed!\n"); 563b5be541fSEmmanuel Vadot device_delete_child(dev, child); 564b5be541fSEmmanuel Vadot goto fail; 565b5be541fSEmmanuel Vadot } 5665e03278fSIlya Bakulin #endif /* MMCCAM */ 567b5be541fSEmmanuel Vadot return (0); 568b5be541fSEmmanuel Vadot 569b5be541fSEmmanuel Vadot fail: 570b5be541fSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 571b5be541fSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 572b5be541fSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 573b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 574b5be541fSEmmanuel Vadot 5755e03278fSIlya Bakulin #ifdef MMCCAM 5765e03278fSIlya Bakulin if (sc->sim != NULL) { 5775e03278fSIlya Bakulin mtx_lock(&sc->sim_mtx); 5785e03278fSIlya Bakulin xpt_bus_deregister(cam_sim_path(sc->sim)); 5795e03278fSIlya Bakulin cam_sim_free(sc->sim, FALSE); 5805e03278fSIlya Bakulin mtx_unlock(&sc->sim_mtx); 5815e03278fSIlya Bakulin } 5825e03278fSIlya Bakulin 5835e03278fSIlya Bakulin if (sc->devq != NULL) 5845e03278fSIlya Bakulin cam_simq_free(sc->devq); 5855e03278fSIlya Bakulin #endif 586b5be541fSEmmanuel Vadot return (ENXIO); 587b5be541fSEmmanuel Vadot } 588b5be541fSEmmanuel Vadot 589b5be541fSEmmanuel Vadot static int 590b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev) 591b5be541fSEmmanuel Vadot { 592b5be541fSEmmanuel Vadot 593b5be541fSEmmanuel Vadot return (EBUSY); 594b5be541fSEmmanuel Vadot } 595b5be541fSEmmanuel Vadot 596b5be541fSEmmanuel Vadot static void 597b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 598b5be541fSEmmanuel Vadot { 599b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 600b5be541fSEmmanuel Vadot 601b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 602b5be541fSEmmanuel Vadot if (err) { 603b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 604b5be541fSEmmanuel Vadot return; 605b5be541fSEmmanuel Vadot } 606b5be541fSEmmanuel Vadot sc->aw_dma_desc_phys = segs[0].ds_addr; 607b5be541fSEmmanuel Vadot } 608b5be541fSEmmanuel Vadot 609b5be541fSEmmanuel Vadot static int 610b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc) 611b5be541fSEmmanuel Vadot { 612c39ea909SEmmanuel Vadot int error; 613b5be541fSEmmanuel Vadot 614b5be541fSEmmanuel Vadot /* Allocate the DMA descriptor memory. */ 615c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 616c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 617c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 618c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 619c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 620c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 621c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, 1, /* maxsize, nsegment */ 622c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, /* maxsegsize */ 623c39ea909SEmmanuel Vadot 0, /* flags */ 624c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 625c39ea909SEmmanuel Vadot &sc->aw_dma_tag); 626b5be541fSEmmanuel Vadot if (error) 627b5be541fSEmmanuel Vadot return (error); 628b5be541fSEmmanuel Vadot 629c39ea909SEmmanuel Vadot error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc, 630c39ea909SEmmanuel Vadot BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, 631c39ea909SEmmanuel Vadot &sc->aw_dma_map); 632c39ea909SEmmanuel Vadot if (error) 633c39ea909SEmmanuel Vadot return (error); 634c39ea909SEmmanuel Vadot 635c39ea909SEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_tag, 636c39ea909SEmmanuel Vadot sc->aw_dma_map, 637c39ea909SEmmanuel Vadot sc->aw_dma_desc, AW_MMC_DMA_DESC_SIZE, 638c39ea909SEmmanuel Vadot aw_dma_desc_cb, sc, 0); 639b5be541fSEmmanuel Vadot if (error) 640b5be541fSEmmanuel Vadot return (error); 641b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 642b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 643b5be541fSEmmanuel Vadot 644b5be541fSEmmanuel Vadot /* Create the DMA map for data transfers. */ 645c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 646c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 647c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 648c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 649c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 650c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 651c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen * 652c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS, /* maxsize, nsegments */ 653c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen, /* maxsegsize */ 654c39ea909SEmmanuel Vadot BUS_DMA_ALLOCNOW, /* flags */ 655c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 656b5be541fSEmmanuel Vadot &sc->aw_dma_buf_tag); 657b5be541fSEmmanuel Vadot if (error) 658b5be541fSEmmanuel Vadot return (error); 659b5be541fSEmmanuel Vadot error = bus_dmamap_create(sc->aw_dma_buf_tag, 0, 660b5be541fSEmmanuel Vadot &sc->aw_dma_buf_map); 661b5be541fSEmmanuel Vadot if (error) 662b5be541fSEmmanuel Vadot return (error); 663b5be541fSEmmanuel Vadot 664b5be541fSEmmanuel Vadot return (0); 665b5be541fSEmmanuel Vadot } 666b5be541fSEmmanuel Vadot 667b5be541fSEmmanuel Vadot static void 668b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 669b5be541fSEmmanuel Vadot { 670b5be541fSEmmanuel Vadot int i; 671b5be541fSEmmanuel Vadot struct aw_mmc_dma_desc *dma_desc; 672b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 673b5be541fSEmmanuel Vadot 674b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 675b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 676b5be541fSEmmanuel Vadot 677b5be541fSEmmanuel Vadot if (err) 678b5be541fSEmmanuel Vadot return; 679b5be541fSEmmanuel Vadot 680b5be541fSEmmanuel Vadot dma_desc = sc->aw_dma_desc; 681b5be541fSEmmanuel Vadot for (i = 0; i < nsegs; i++) { 682c39ea909SEmmanuel Vadot if (segs[i].ds_len == sc->aw_mmc_conf->dma_xferlen) 683c39ea909SEmmanuel Vadot dma_desc[i].buf_size = 0; /* Size of 0 indicate max len */ 684c39ea909SEmmanuel Vadot else 685b5be541fSEmmanuel Vadot dma_desc[i].buf_size = segs[i].ds_len; 686b5be541fSEmmanuel Vadot dma_desc[i].buf_addr = segs[i].ds_addr; 687b5be541fSEmmanuel Vadot dma_desc[i].config = AW_MMC_DMA_CONFIG_CH | 688c39ea909SEmmanuel Vadot AW_MMC_DMA_CONFIG_OWN | AW_MMC_DMA_CONFIG_DIC; 689c39ea909SEmmanuel Vadot 690b5be541fSEmmanuel Vadot dma_desc[i].next = sc->aw_dma_desc_phys + 691b5be541fSEmmanuel Vadot ((i + 1) * sizeof(struct aw_mmc_dma_desc)); 692c39ea909SEmmanuel Vadot } 693c39ea909SEmmanuel Vadot 694c39ea909SEmmanuel Vadot dma_desc[0].config |= AW_MMC_DMA_CONFIG_FD; 695c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config |= AW_MMC_DMA_CONFIG_LD | 696b5be541fSEmmanuel Vadot AW_MMC_DMA_CONFIG_ER; 697c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config &= ~AW_MMC_DMA_CONFIG_DIC; 698c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].next = 0; 699b5be541fSEmmanuel Vadot } 700b5be541fSEmmanuel Vadot 701b5be541fSEmmanuel Vadot static int 702b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc) 703b5be541fSEmmanuel Vadot { 704b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 705b5be541fSEmmanuel Vadot int error; 706b5be541fSEmmanuel Vadot struct mmc_command *cmd; 707b5be541fSEmmanuel Vadot uint32_t val; 708b5be541fSEmmanuel Vadot 7095e03278fSIlya Bakulin #ifdef MMCCAM 7105e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 7115e03278fSIlya Bakulin #else 712b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 7135e03278fSIlya Bakulin #endif 714ce0618beSEmmanuel Vadot if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS)) 715b5be541fSEmmanuel Vadot return (EFBIG); 716b5be541fSEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 717b5be541fSEmmanuel Vadot cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0); 718b5be541fSEmmanuel Vadot if (error) 719b5be541fSEmmanuel Vadot return (error); 720b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 721b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 722b5be541fSEmmanuel Vadot 723b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 724b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREWRITE; 725b5be541fSEmmanuel Vadot else 726b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREREAD; 727b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op); 728b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE); 729b5be541fSEmmanuel Vadot 730b5be541fSEmmanuel Vadot /* Enable DMA */ 731b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 732b091392eSEmmanuel Vadot val &= ~AW_MMC_GCTL_FIFO_AC_MOD; 733b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_ENB; 734b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 735b5be541fSEmmanuel Vadot 736b5be541fSEmmanuel Vadot /* Reset DMA */ 737b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_RST; 738b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 739b5be541fSEmmanuel Vadot 740b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST); 741b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, 742b5be541fSEmmanuel Vadot AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST); 743b5be541fSEmmanuel Vadot 744b5be541fSEmmanuel Vadot /* Enable RX or TX DMA interrupt */ 745a37d59c1SEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_IDIE); 746b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 747b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_TX_INT; 748b5be541fSEmmanuel Vadot else 749b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_RX_INT; 750b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val); 751b5be541fSEmmanuel Vadot 752b5be541fSEmmanuel Vadot /* Set DMA descritptor list address */ 753b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys); 754b5be541fSEmmanuel Vadot 755b5be541fSEmmanuel Vadot /* FIFO trigger level */ 756b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL); 757b5be541fSEmmanuel Vadot 758b5be541fSEmmanuel Vadot return (0); 759b5be541fSEmmanuel Vadot } 760b5be541fSEmmanuel Vadot 761b5be541fSEmmanuel Vadot static int 762b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc) 763b5be541fSEmmanuel Vadot { 764b091392eSEmmanuel Vadot uint32_t reg; 765b5be541fSEmmanuel Vadot int timeout; 766b5be541fSEmmanuel Vadot 767b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 768b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_RESET; 769b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 770c39ea909SEmmanuel Vadot timeout = AW_MMC_RESET_RETRY; 771b5be541fSEmmanuel Vadot while (--timeout > 0) { 772b091392eSEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_GCTL_RESET) == 0) 773b5be541fSEmmanuel Vadot break; 774b5be541fSEmmanuel Vadot DELAY(100); 775b5be541fSEmmanuel Vadot } 776b5be541fSEmmanuel Vadot if (timeout == 0) 777b5be541fSEmmanuel Vadot return (ETIMEDOUT); 778b5be541fSEmmanuel Vadot 77935a18619SEmmanuel Vadot return (0); 78035a18619SEmmanuel Vadot } 78135a18619SEmmanuel Vadot 78235a18619SEmmanuel Vadot static int 78335a18619SEmmanuel Vadot aw_mmc_init(struct aw_mmc_softc *sc) 78435a18619SEmmanuel Vadot { 785b091392eSEmmanuel Vadot uint32_t reg; 78635a18619SEmmanuel Vadot int ret; 78735a18619SEmmanuel Vadot 78835a18619SEmmanuel Vadot ret = aw_mmc_reset(sc); 78935a18619SEmmanuel Vadot if (ret != 0) 79035a18619SEmmanuel Vadot return (ret); 79135a18619SEmmanuel Vadot 792b5be541fSEmmanuel Vadot /* Set the timeout. */ 793b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_TMOR, 794b5be541fSEmmanuel Vadot AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) | 795b5be541fSEmmanuel Vadot AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK)); 796b5be541fSEmmanuel Vadot 79735a18619SEmmanuel Vadot /* Unmask interrupts. */ 79835a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, 0); 79935a18619SEmmanuel Vadot 800b5be541fSEmmanuel Vadot /* Clear pending interrupts. */ 801b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 80235a18619SEmmanuel Vadot 80335a18619SEmmanuel Vadot /* Debug register, undocumented */ 80435a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DBGC, 0xdeb); 80535a18619SEmmanuel Vadot 80635a18619SEmmanuel Vadot /* Function select register */ 80735a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FUNS, 0xceaa0000); 80835a18619SEmmanuel Vadot 809b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff); 81035a18619SEmmanuel Vadot 811b091392eSEmmanuel Vadot /* Enable interrupts and disable AHB access. */ 812b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 813b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_INT_ENB; 814b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_FIFO_AC_MOD; 815b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_WAIT_MEM_ACCESS; 816b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 817b5be541fSEmmanuel Vadot 818b5be541fSEmmanuel Vadot return (0); 819b5be541fSEmmanuel Vadot } 820b5be541fSEmmanuel Vadot 821b5be541fSEmmanuel Vadot static void 822b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc) 823b5be541fSEmmanuel Vadot { 824b5be541fSEmmanuel Vadot struct mmc_command *cmd; 8255e03278fSIlya Bakulin #ifdef MMCCAM 8265e03278fSIlya Bakulin union ccb *ccb; 8275e03278fSIlya Bakulin #else 828b5be541fSEmmanuel Vadot struct mmc_request *req; 8295e03278fSIlya Bakulin #endif 830b5be541fSEmmanuel Vadot uint32_t val, mask; 831b5be541fSEmmanuel Vadot int retry; 832b5be541fSEmmanuel Vadot 8335e03278fSIlya Bakulin #ifdef MMCCAM 8345e03278fSIlya Bakulin ccb = sc->ccb; 8355e03278fSIlya Bakulin cmd = &ccb->mmcio.cmd; 8365e03278fSIlya Bakulin #else 837b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 8385e03278fSIlya Bakulin #endif 8395e03278fSIlya Bakulin #ifdef DEBUG 8405e03278fSIlya Bakulin if (bootverbose) { 8415e03278fSIlya Bakulin device_printf(sc->aw_dev, "%s: cmd %d err %d\n", __func__, cmd->opcode, cmd->error); 8425e03278fSIlya Bakulin } 8435e03278fSIlya Bakulin #endif 844b5be541fSEmmanuel Vadot if (cmd->error != MMC_ERR_NONE) { 845b5be541fSEmmanuel Vadot /* Reset the FIFO and DMA engines. */ 846b091392eSEmmanuel Vadot mask = AW_MMC_GCTL_FIFO_RST | AW_MMC_GCTL_DMA_RST; 847b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 848b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask); 849b5be541fSEmmanuel Vadot 850b5be541fSEmmanuel Vadot retry = AW_MMC_RESET_RETRY; 851b5be541fSEmmanuel Vadot while (--retry > 0) { 852c39ea909SEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & 853c39ea909SEmmanuel Vadot AW_MMC_GCTL_RESET) == 0) 854b5be541fSEmmanuel Vadot break; 855c39ea909SEmmanuel Vadot DELAY(100); 856b5be541fSEmmanuel Vadot } 857b5be541fSEmmanuel Vadot if (retry == 0) 858b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 859b5be541fSEmmanuel Vadot "timeout resetting DMA/FIFO\n"); 860b5be541fSEmmanuel Vadot aw_mmc_update_clock(sc, 1); 861b5be541fSEmmanuel Vadot } 862b5be541fSEmmanuel Vadot 863b5be541fSEmmanuel Vadot callout_stop(&sc->aw_timeoutc); 864b5be541fSEmmanuel Vadot sc->aw_intr = 0; 865b5be541fSEmmanuel Vadot sc->aw_resid = 0; 866b5be541fSEmmanuel Vadot sc->aw_dma_map_err = 0; 867b5be541fSEmmanuel Vadot sc->aw_intr_wait = 0; 8685e03278fSIlya Bakulin #ifdef MMCCAM 8695e03278fSIlya Bakulin sc->ccb = NULL; 8705e03278fSIlya Bakulin ccb->ccb_h.status = 8715e03278fSIlya Bakulin (ccb->mmcio.cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 8725e03278fSIlya Bakulin xpt_done(ccb); 8735e03278fSIlya Bakulin #else 8745e03278fSIlya Bakulin req = sc->aw_req; 8755e03278fSIlya Bakulin sc->aw_req = NULL; 876b5be541fSEmmanuel Vadot req->done(req); 8775e03278fSIlya Bakulin #endif 878b5be541fSEmmanuel Vadot } 879b5be541fSEmmanuel Vadot 880b5be541fSEmmanuel Vadot static void 881b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc) 882b5be541fSEmmanuel Vadot { 883b5be541fSEmmanuel Vadot int timeout; 884b5be541fSEmmanuel Vadot struct mmc_command *cmd; 885b5be541fSEmmanuel Vadot uint32_t status; 886b5be541fSEmmanuel Vadot 887b5be541fSEmmanuel Vadot timeout = 1000; 888b5be541fSEmmanuel Vadot while (--timeout > 0) { 889b5be541fSEmmanuel Vadot status = AW_MMC_READ_4(sc, AW_MMC_STAR); 890b5be541fSEmmanuel Vadot if ((status & AW_MMC_STAR_CARD_BUSY) == 0) 891b5be541fSEmmanuel Vadot break; 892b5be541fSEmmanuel Vadot DELAY(1000); 893b5be541fSEmmanuel Vadot } 8945e03278fSIlya Bakulin #ifdef MMCCAM 8955e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 8965e03278fSIlya Bakulin #else 897b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 8985e03278fSIlya Bakulin #endif 899b5be541fSEmmanuel Vadot if (timeout == 0) { 900b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 901b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 902b5be541fSEmmanuel Vadot return; 903b5be541fSEmmanuel Vadot } 904b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) { 905b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) { 906b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3); 907b5be541fSEmmanuel Vadot cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2); 908b5be541fSEmmanuel Vadot cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1); 909b5be541fSEmmanuel Vadot cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 910b5be541fSEmmanuel Vadot } else 911b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 912b5be541fSEmmanuel Vadot } 913b5be541fSEmmanuel Vadot /* All data has been transferred ? */ 914b5be541fSEmmanuel Vadot if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len) 915b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 916b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 917b5be541fSEmmanuel Vadot } 918b5be541fSEmmanuel Vadot 9195e03278fSIlya Bakulin 9205e03278fSIlya Bakulin static inline void 9215e03278fSIlya Bakulin set_mmc_error(struct aw_mmc_softc *sc, int error_code) 9225e03278fSIlya Bakulin { 9235e03278fSIlya Bakulin #ifdef MMCCAM 9245e03278fSIlya Bakulin sc->ccb->mmcio.cmd.error = error_code; 9255e03278fSIlya Bakulin #else 9265e03278fSIlya Bakulin sc->aw_req->cmd->error = error_code; 9275e03278fSIlya Bakulin #endif 9285e03278fSIlya Bakulin } 9295e03278fSIlya Bakulin 930b5be541fSEmmanuel Vadot static void 931b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg) 932b5be541fSEmmanuel Vadot { 933b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 934b5be541fSEmmanuel Vadot 935b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 9365e03278fSIlya Bakulin #ifdef MMCCAM 9375e03278fSIlya Bakulin if (sc->ccb != NULL) { 9385e03278fSIlya Bakulin #else 939b5be541fSEmmanuel Vadot if (sc->aw_req != NULL) { 9405e03278fSIlya Bakulin #endif 941b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "controller timeout\n"); 9425e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 943b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 944b5be541fSEmmanuel Vadot } else 945b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 946b5be541fSEmmanuel Vadot "Spurious timeout - no active request\n"); 947b5be541fSEmmanuel Vadot } 948b5be541fSEmmanuel Vadot 949b5be541fSEmmanuel Vadot static void 9505e03278fSIlya Bakulin aw_mmc_print_error(uint32_t err) 9515e03278fSIlya Bakulin { 9525e03278fSIlya Bakulin if(err & AW_MMC_INT_RESP_ERR) 9535e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_ERR "); 9545e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_CRC_ERR) 9555e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_CRC_ERR "); 9565e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_CRC_ERR) 9575e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_CRC_ERR "); 9585e03278fSIlya Bakulin if (err & AW_MMC_INT_RESP_TIMEOUT) 9595e03278fSIlya Bakulin printf("AW_MMC_INT_RESP_TIMEOUT "); 9605e03278fSIlya Bakulin if (err & AW_MMC_INT_FIFO_RUN_ERR) 9615e03278fSIlya Bakulin printf("AW_MMC_INT_FIFO_RUN_ERR "); 9625e03278fSIlya Bakulin if (err & AW_MMC_INT_CMD_BUSY) 9635e03278fSIlya Bakulin printf("AW_MMC_INT_CMD_BUSY "); 9645e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_START_ERR) 9655e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_START_ERR "); 9665e03278fSIlya Bakulin if (err & AW_MMC_INT_DATA_END_BIT_ERR) 9675e03278fSIlya Bakulin printf("AW_MMC_INT_DATA_END_BIT_ERR"); 9685e03278fSIlya Bakulin printf("\n"); 9695e03278fSIlya Bakulin } 9705e03278fSIlya Bakulin 9715e03278fSIlya Bakulin static void 972b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg) 973b5be541fSEmmanuel Vadot { 974b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 975b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 976b5be541fSEmmanuel Vadot struct mmc_data *data; 977b5be541fSEmmanuel Vadot uint32_t idst, imask, rint; 978b5be541fSEmmanuel Vadot 979b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 980b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 981b5be541fSEmmanuel Vadot rint = AW_MMC_READ_4(sc, AW_MMC_RISR); 982b5be541fSEmmanuel Vadot idst = AW_MMC_READ_4(sc, AW_MMC_IDST); 983b5be541fSEmmanuel Vadot imask = AW_MMC_READ_4(sc, AW_MMC_IMKR); 984b5be541fSEmmanuel Vadot if (idst == 0 && imask == 0 && rint == 0) { 985b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 986b5be541fSEmmanuel Vadot return; 987b5be541fSEmmanuel Vadot } 988b5be541fSEmmanuel Vadot #ifdef DEBUG 989b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n", 990b5be541fSEmmanuel Vadot idst, imask, rint); 991b5be541fSEmmanuel Vadot #endif 9925e03278fSIlya Bakulin #ifdef MMCCAM 9935e03278fSIlya Bakulin if (sc->ccb == NULL) { 9945e03278fSIlya Bakulin #else 995b5be541fSEmmanuel Vadot if (sc->aw_req == NULL) { 9965e03278fSIlya Bakulin #endif 997b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 998b5be541fSEmmanuel Vadot "Spurious interrupt - no active request, rint: 0x%08X\n", 999b5be541fSEmmanuel Vadot rint); 10005e03278fSIlya Bakulin aw_mmc_print_error(rint); 1001b5be541fSEmmanuel Vadot goto end; 1002b5be541fSEmmanuel Vadot } 1003b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_ERR_BIT) { 1004ce0618beSEmmanuel Vadot if (bootverbose) 1005b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint); 10065e03278fSIlya Bakulin aw_mmc_print_error(rint); 1007b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_RESP_TIMEOUT) 10085e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_TIMEOUT); 1009b5be541fSEmmanuel Vadot else 10105e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 1011b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 1012b5be541fSEmmanuel Vadot goto end; 1013b5be541fSEmmanuel Vadot } 1014b5be541fSEmmanuel Vadot if (idst & AW_MMC_IDST_ERROR) { 1015b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst); 10165e03278fSIlya Bakulin set_mmc_error(sc, MMC_ERR_FAILED); 1017b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 1018b5be541fSEmmanuel Vadot goto end; 1019b5be541fSEmmanuel Vadot } 1020b5be541fSEmmanuel Vadot 1021b5be541fSEmmanuel Vadot sc->aw_intr |= rint; 10225e03278fSIlya Bakulin #ifdef MMCCAM 10235e03278fSIlya Bakulin data = sc->ccb->mmcio.cmd.data; 10245e03278fSIlya Bakulin #else 1025b5be541fSEmmanuel Vadot data = sc->aw_req->cmd->data; 10265e03278fSIlya Bakulin #endif 1027b5be541fSEmmanuel Vadot if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) { 1028b5be541fSEmmanuel Vadot if (data->flags & MMC_DATA_WRITE) 1029b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTWRITE; 1030b5be541fSEmmanuel Vadot else 1031b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTREAD; 1032b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 1033b5be541fSEmmanuel Vadot sync_op); 1034b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, 1035b5be541fSEmmanuel Vadot BUS_DMASYNC_POSTWRITE); 1036b5be541fSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 1037b5be541fSEmmanuel Vadot sc->aw_resid = data->len >> 2; 1038b5be541fSEmmanuel Vadot } 1039b5be541fSEmmanuel Vadot if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait) 1040b5be541fSEmmanuel Vadot aw_mmc_req_ok(sc); 1041b5be541fSEmmanuel Vadot 1042b5be541fSEmmanuel Vadot end: 1043b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst); 1044b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint); 1045b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1046b5be541fSEmmanuel Vadot } 1047b5be541fSEmmanuel Vadot 1048b5be541fSEmmanuel Vadot static int 1049b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req) 1050b5be541fSEmmanuel Vadot { 1051b5be541fSEmmanuel Vadot int blksz; 1052b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1053b5be541fSEmmanuel Vadot struct mmc_command *cmd; 105435a18619SEmmanuel Vadot uint32_t cmdreg, imask; 1055b5be541fSEmmanuel Vadot int err; 1056b5be541fSEmmanuel Vadot 1057b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1058c39ea909SEmmanuel Vadot 1059b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 10605e03278fSIlya Bakulin #ifdef MMCCAM 10615e03278fSIlya Bakulin KASSERT(req == NULL, ("req should be NULL in MMCCAM case!")); 10625e03278fSIlya Bakulin /* 10635e03278fSIlya Bakulin * For MMCCAM, sc->ccb has been NULL-checked and populated 10645e03278fSIlya Bakulin * by aw_mmc_cam_request() already. 10655e03278fSIlya Bakulin */ 10665e03278fSIlya Bakulin cmd = &sc->ccb->mmcio.cmd; 10675e03278fSIlya Bakulin #else 1068b5be541fSEmmanuel Vadot if (sc->aw_req) { 1069b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1070b5be541fSEmmanuel Vadot return (EBUSY); 1071b5be541fSEmmanuel Vadot } 1072b5be541fSEmmanuel Vadot sc->aw_req = req; 1073b5be541fSEmmanuel Vadot cmd = req->cmd; 10745e03278fSIlya Bakulin 10755e03278fSIlya Bakulin #ifdef DEBUG 10765e03278fSIlya Bakulin if (bootverbose) 10775e03278fSIlya Bakulin device_printf(sc->aw_dev, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 10785e03278fSIlya Bakulin cmd->opcode, cmd->arg, cmd->flags, 10795e03278fSIlya Bakulin cmd->data != NULL ? (unsigned int)cmd->data->len : 0, 10805e03278fSIlya Bakulin cmd->data != NULL ? cmd->data->flags: 0); 10815e03278fSIlya Bakulin #endif 10825e03278fSIlya Bakulin #endif 1083b5be541fSEmmanuel Vadot cmdreg = AW_MMC_CMDR_LOAD; 108435a18619SEmmanuel Vadot imask = AW_MMC_INT_ERR_BIT; 108535a18619SEmmanuel Vadot sc->aw_intr_wait = 0; 108635a18619SEmmanuel Vadot sc->aw_intr = 0; 108735a18619SEmmanuel Vadot sc->aw_resid = 0; 108835a18619SEmmanuel Vadot cmd->error = MMC_ERR_NONE; 108935a18619SEmmanuel Vadot 1090b5be541fSEmmanuel Vadot if (cmd->opcode == MMC_GO_IDLE_STATE) 1091b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ; 109235a18619SEmmanuel Vadot 1093b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) 1094b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_RESP_RCV; 1095b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) 1096b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_LONG_RESP; 1097b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_CRC) 1098b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC; 1099b5be541fSEmmanuel Vadot 110035a18619SEmmanuel Vadot if (cmd->data) { 1101b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER; 110235a18619SEmmanuel Vadot 1103b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_MULTI) { 1104b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG; 110535a18619SEmmanuel Vadot imask |= AW_MMC_INT_AUTO_STOP_DONE; 1106b5be541fSEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE; 110735a18619SEmmanuel Vadot } else { 110835a18619SEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER; 110935a18619SEmmanuel Vadot imask |= AW_MMC_INT_DATA_OVER; 1110b5be541fSEmmanuel Vadot } 1111b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 1112b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DIR_WRITE; 11135d5ae066SIlya Bakulin #ifdef MMCCAM 11145d5ae066SIlya Bakulin if (cmd->data->flags & MMC_DATA_BLOCK_SIZE) { 11155d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BKSR, cmd->data->block_size); 11165d5ae066SIlya Bakulin AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 11175d5ae066SIlya Bakulin } else 11185d5ae066SIlya Bakulin #endif 11195d5ae066SIlya Bakulin { 1120440565daSBjoern A. Zeeb blksz = min(cmd->data->len, MMC_SECTOR_SIZE); 1121b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz); 1122b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 11235d5ae066SIlya Bakulin } 112435a18619SEmmanuel Vadot } else { 112535a18619SEmmanuel Vadot imask |= AW_MMC_INT_CMD_DONE; 112635a18619SEmmanuel Vadot } 1127b5be541fSEmmanuel Vadot 112835a18619SEmmanuel Vadot /* Enable the interrupts we are interested in */ 112935a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, imask); 113035a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 113135a18619SEmmanuel Vadot 113235a18619SEmmanuel Vadot /* Enable auto stop if needed */ 113335a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_A12A, 113435a18619SEmmanuel Vadot cmdreg & AW_MMC_CMDR_STOP_CMD_FLAG ? 0 : 0xffff); 113535a18619SEmmanuel Vadot 113635a18619SEmmanuel Vadot /* Write the command argument */ 113735a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg); 113835a18619SEmmanuel Vadot 113935a18619SEmmanuel Vadot /* 114035a18619SEmmanuel Vadot * If we don't have data start the request 114135a18619SEmmanuel Vadot * if we do prepare the dma request and start the request 114235a18619SEmmanuel Vadot */ 114335a18619SEmmanuel Vadot if (cmd->data == NULL) { 114435a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 114535a18619SEmmanuel Vadot } else { 1146b5be541fSEmmanuel Vadot err = aw_mmc_prepare_dma(sc); 1147b5be541fSEmmanuel Vadot if (err != 0) 1148b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err); 114935a18619SEmmanuel Vadot 115035a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 1151b5be541fSEmmanuel Vadot } 1152b5be541fSEmmanuel Vadot 1153b5be541fSEmmanuel Vadot callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz, 1154b5be541fSEmmanuel Vadot aw_mmc_timeout, sc); 1155b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1156b5be541fSEmmanuel Vadot 1157b5be541fSEmmanuel Vadot return (0); 1158b5be541fSEmmanuel Vadot } 1159b5be541fSEmmanuel Vadot 1160b5be541fSEmmanuel Vadot static int 1161b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which, 1162b5be541fSEmmanuel Vadot uintptr_t *result) 1163b5be541fSEmmanuel Vadot { 1164b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1165b5be541fSEmmanuel Vadot 1166b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1167b5be541fSEmmanuel Vadot switch (which) { 1168b5be541fSEmmanuel Vadot default: 1169b5be541fSEmmanuel Vadot return (EINVAL); 1170b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1171b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_mode; 1172b5be541fSEmmanuel Vadot break; 1173b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1174b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_width; 1175b5be541fSEmmanuel Vadot break; 1176b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1177b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.chip_select; 1178b5be541fSEmmanuel Vadot break; 1179b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1180b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.clock; 1181b5be541fSEmmanuel Vadot break; 1182b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1183b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_min; 1184b5be541fSEmmanuel Vadot break; 1185b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1186b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_max; 1187b5be541fSEmmanuel Vadot break; 1188b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1189b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.host_ocr; 1190b5be541fSEmmanuel Vadot break; 1191b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1192b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.mode; 1193b5be541fSEmmanuel Vadot break; 1194b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1195b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ocr; 1196b5be541fSEmmanuel Vadot break; 1197b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1198b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.power_mode; 1199b5be541fSEmmanuel Vadot break; 1200b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1201b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.vdd; 1202b5be541fSEmmanuel Vadot break; 1203dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1204dfb8c122SEmmanuel Vadot *(int *)result = sc->aw_host.ios.vccq; 1205dfb8c122SEmmanuel Vadot break; 1206b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1207b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.caps; 1208b5be541fSEmmanuel Vadot break; 1209ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1210ce0618beSEmmanuel Vadot *(int *)result = sc->aw_host.ios.timing; 1211ce0618beSEmmanuel Vadot break; 1212b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1213c39ea909SEmmanuel Vadot *(int *)result = (sc->aw_mmc_conf->dma_xferlen * 1214c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 1215b5be541fSEmmanuel Vadot break; 121655f3f71cSEmmanuel Vadot case MMCBR_IVAR_RETUNE_REQ: 121755f3f71cSEmmanuel Vadot *(int *)result = retune_req_none; 121855f3f71cSEmmanuel Vadot break; 1219b5be541fSEmmanuel Vadot } 1220b5be541fSEmmanuel Vadot 1221b5be541fSEmmanuel Vadot return (0); 1222b5be541fSEmmanuel Vadot } 1223b5be541fSEmmanuel Vadot 1224b5be541fSEmmanuel Vadot static int 1225b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which, 1226b5be541fSEmmanuel Vadot uintptr_t value) 1227b5be541fSEmmanuel Vadot { 1228b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1229b5be541fSEmmanuel Vadot 1230b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1231b5be541fSEmmanuel Vadot switch (which) { 1232b5be541fSEmmanuel Vadot default: 1233b5be541fSEmmanuel Vadot return (EINVAL); 1234b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 1235b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_mode = value; 1236b5be541fSEmmanuel Vadot break; 1237b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 1238b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_width = value; 1239b5be541fSEmmanuel Vadot break; 1240b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 1241b5be541fSEmmanuel Vadot sc->aw_host.ios.chip_select = value; 1242b5be541fSEmmanuel Vadot break; 1243b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 1244b5be541fSEmmanuel Vadot sc->aw_host.ios.clock = value; 1245b5be541fSEmmanuel Vadot break; 1246b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 1247b5be541fSEmmanuel Vadot sc->aw_host.mode = value; 1248b5be541fSEmmanuel Vadot break; 1249b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 1250b5be541fSEmmanuel Vadot sc->aw_host.ocr = value; 1251b5be541fSEmmanuel Vadot break; 1252b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 1253b5be541fSEmmanuel Vadot sc->aw_host.ios.power_mode = value; 1254b5be541fSEmmanuel Vadot break; 1255b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 1256b5be541fSEmmanuel Vadot sc->aw_host.ios.vdd = value; 1257b5be541fSEmmanuel Vadot break; 1258dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 1259dfb8c122SEmmanuel Vadot sc->aw_host.ios.vccq = value; 1260dfb8c122SEmmanuel Vadot break; 1261ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 1262ce0618beSEmmanuel Vadot sc->aw_host.ios.timing = value; 1263ce0618beSEmmanuel Vadot break; 1264b5be541fSEmmanuel Vadot /* These are read-only */ 1265b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 1266b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 1267b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 1268b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 1269b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 1270b5be541fSEmmanuel Vadot return (EINVAL); 1271b5be541fSEmmanuel Vadot } 1272b5be541fSEmmanuel Vadot 1273b5be541fSEmmanuel Vadot return (0); 1274b5be541fSEmmanuel Vadot } 1275b5be541fSEmmanuel Vadot 1276b5be541fSEmmanuel Vadot static int 1277b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon) 1278b5be541fSEmmanuel Vadot { 1279ce0618beSEmmanuel Vadot uint32_t reg; 1280b5be541fSEmmanuel Vadot int retry; 1281b5be541fSEmmanuel Vadot 1282ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1283ffdb1aa8SEmmanuel Vadot reg &= ~(AW_MMC_CKCR_ENB | AW_MMC_CKCR_LOW_POWER | 1284ffdb1aa8SEmmanuel Vadot AW_MMC_CKCR_MASK_DATA0); 1285b5be541fSEmmanuel Vadot 1286b5be541fSEmmanuel Vadot if (clkon) 1287ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_ENB; 1288ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) 1289ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_MASK_DATA0; 1290b5be541fSEmmanuel Vadot 1291ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1292b5be541fSEmmanuel Vadot 1293ce0618beSEmmanuel Vadot reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK | 1294b5be541fSEmmanuel Vadot AW_MMC_CMDR_WAIT_PRE_OVER; 1295ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg); 1296b5be541fSEmmanuel Vadot retry = 0xfffff; 1297ce0618beSEmmanuel Vadot 1298ce0618beSEmmanuel Vadot while (reg & AW_MMC_CMDR_LOAD && --retry > 0) { 1299ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CMDR); 1300b5be541fSEmmanuel Vadot DELAY(10); 1301b5be541fSEmmanuel Vadot } 1302b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 1303b5be541fSEmmanuel Vadot 1304ce0618beSEmmanuel Vadot if (reg & AW_MMC_CMDR_LOAD) { 1305ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "timeout updating clock\n"); 1306b5be541fSEmmanuel Vadot return (ETIMEDOUT); 1307b5be541fSEmmanuel Vadot } 1308b5be541fSEmmanuel Vadot 1309ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) { 1310ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1311ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_MASK_DATA0; 1312ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1313ce0618beSEmmanuel Vadot } 1314ce0618beSEmmanuel Vadot 1315ce0618beSEmmanuel Vadot return (0); 1316ce0618beSEmmanuel Vadot } 1317ce0618beSEmmanuel Vadot 1318623966e1SEmmanuel Vadot static int 1319623966e1SEmmanuel Vadot aw_mmc_switch_vccq(device_t bus, device_t child) 1320ce0618beSEmmanuel Vadot { 1321623966e1SEmmanuel Vadot struct aw_mmc_softc *sc; 1322623966e1SEmmanuel Vadot int uvolt, err; 1323623966e1SEmmanuel Vadot 1324623966e1SEmmanuel Vadot sc = device_get_softc(bus); 1325ce0618beSEmmanuel Vadot 13263177f7cdSEmmanuel Vadot if (sc->aw_reg_vqmmc == NULL) 1327623966e1SEmmanuel Vadot return EOPNOTSUPP; 1328ce0618beSEmmanuel Vadot 1329623966e1SEmmanuel Vadot switch (sc->aw_host.ios.vccq) { 1330dfb8c122SEmmanuel Vadot case vccq_180: 1331dfb8c122SEmmanuel Vadot uvolt = 1800000; 1332ce0618beSEmmanuel Vadot break; 1333dfb8c122SEmmanuel Vadot case vccq_330: 1334dfb8c122SEmmanuel Vadot uvolt = 3300000; 1335ce0618beSEmmanuel Vadot break; 1336dfb8c122SEmmanuel Vadot default: 1337623966e1SEmmanuel Vadot return EINVAL; 1338ce0618beSEmmanuel Vadot } 1339ce0618beSEmmanuel Vadot 1340623966e1SEmmanuel Vadot err = regulator_set_voltage(sc->aw_reg_vqmmc, uvolt, uvolt); 1341623966e1SEmmanuel Vadot if (err != 0) { 1342ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, 1343ce0618beSEmmanuel Vadot "Cannot set vqmmc to %d<->%d\n", 1344dfb8c122SEmmanuel Vadot uvolt, 1345dfb8c122SEmmanuel Vadot uvolt); 1346623966e1SEmmanuel Vadot return (err); 1347623966e1SEmmanuel Vadot } 1348623966e1SEmmanuel Vadot 1349623966e1SEmmanuel Vadot return (0); 1350ce0618beSEmmanuel Vadot } 1351ce0618beSEmmanuel Vadot 1352b5be541fSEmmanuel Vadot static int 1353b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child) 1354b5be541fSEmmanuel Vadot { 1355b5be541fSEmmanuel Vadot int error; 1356b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1357b5be541fSEmmanuel Vadot struct mmc_ios *ios; 1358ce0618beSEmmanuel Vadot unsigned int clock; 1359ce0618beSEmmanuel Vadot uint32_t reg, div = 1; 1360b5be541fSEmmanuel Vadot 1361b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1362b5be541fSEmmanuel Vadot 1363b5be541fSEmmanuel Vadot ios = &sc->aw_host.ios; 1364b5be541fSEmmanuel Vadot 1365b5be541fSEmmanuel Vadot /* Set the bus width. */ 1366b5be541fSEmmanuel Vadot switch (ios->bus_width) { 1367b5be541fSEmmanuel Vadot case bus_width_1: 1368b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1); 1369b5be541fSEmmanuel Vadot break; 1370b5be541fSEmmanuel Vadot case bus_width_4: 1371b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4); 1372b5be541fSEmmanuel Vadot break; 1373b5be541fSEmmanuel Vadot case bus_width_8: 1374b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8); 1375b5be541fSEmmanuel Vadot break; 1376b5be541fSEmmanuel Vadot } 1377b5be541fSEmmanuel Vadot 137835a18619SEmmanuel Vadot switch (ios->power_mode) { 137935a18619SEmmanuel Vadot case power_on: 138035a18619SEmmanuel Vadot break; 138135a18619SEmmanuel Vadot case power_off: 1382ce0618beSEmmanuel Vadot if (bootverbose) 1383ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "Powering down sd/mmc\n"); 1384dfb8c122SEmmanuel Vadot 1385dfb8c122SEmmanuel Vadot if (sc->aw_reg_vmmc) 1386dfb8c122SEmmanuel Vadot regulator_disable(sc->aw_reg_vmmc); 1387dfb8c122SEmmanuel Vadot if (sc->aw_reg_vqmmc) 1388dfb8c122SEmmanuel Vadot regulator_disable(sc->aw_reg_vqmmc); 1389dfb8c122SEmmanuel Vadot 139035a18619SEmmanuel Vadot aw_mmc_reset(sc); 139135a18619SEmmanuel Vadot break; 139235a18619SEmmanuel Vadot case power_up: 139335a18619SEmmanuel Vadot if (bootverbose) 139435a18619SEmmanuel Vadot device_printf(sc->aw_dev, "Powering up sd/mmc\n"); 1395dfb8c122SEmmanuel Vadot 1396dfb8c122SEmmanuel Vadot if (sc->aw_reg_vmmc) 1397dfb8c122SEmmanuel Vadot regulator_enable(sc->aw_reg_vmmc); 1398dfb8c122SEmmanuel Vadot if (sc->aw_reg_vqmmc) 1399dfb8c122SEmmanuel Vadot regulator_enable(sc->aw_reg_vqmmc); 140035a18619SEmmanuel Vadot aw_mmc_init(sc); 140135a18619SEmmanuel Vadot break; 140235a18619SEmmanuel Vadot }; 1403ce0618beSEmmanuel Vadot 1404ce0618beSEmmanuel Vadot /* Enable ddr mode if needed */ 1405ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 1406ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_uhs_ddr50 || 1407ce0618beSEmmanuel Vadot ios->timing == bus_timing_mmc_ddr52) 1408b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_DDR_MOD_SEL; 1409ce0618beSEmmanuel Vadot else 1410b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_DDR_MOD_SEL; 1411ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 1412ce0618beSEmmanuel Vadot 14130f7a6420SEmmanuel Vadot if (ios->clock && ios->clock != sc->aw_clock) { 14140f7a6420SEmmanuel Vadot sc->aw_clock = clock = ios->clock; 1415b5be541fSEmmanuel Vadot 1416b5be541fSEmmanuel Vadot /* Disable clock */ 1417b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 0); 1418b5be541fSEmmanuel Vadot if (error != 0) 1419b5be541fSEmmanuel Vadot return (error); 1420b5be541fSEmmanuel Vadot 1421ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_mmc_ddr52 && 1422ce0618beSEmmanuel Vadot (sc->aw_mmc_conf->new_timing || 1423ce0618beSEmmanuel Vadot ios->bus_width == bus_width_8)) { 1424ce0618beSEmmanuel Vadot div = 2; 1425ce0618beSEmmanuel Vadot clock <<= 1; 1426ce0618beSEmmanuel Vadot } 1427ce0618beSEmmanuel Vadot 1428b5be541fSEmmanuel Vadot /* Reset the divider. */ 1429ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1430ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_DIV; 1431ce0618beSEmmanuel Vadot reg |= div - 1; 1432ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1433ce0618beSEmmanuel Vadot 1434ce0618beSEmmanuel Vadot /* New timing mode if needed */ 1435ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->new_timing) { 1436ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_NTSR); 1437ce0618beSEmmanuel Vadot reg |= AW_MMC_NTSR_MODE_SELECT; 1438ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg); 1439ce0618beSEmmanuel Vadot } 1440b5be541fSEmmanuel Vadot 1441b5be541fSEmmanuel Vadot /* Set the MMC clock. */ 1442101260f3SEmmanuel Vadot error = clk_disable(sc->aw_clk_mmc); 1443101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1444101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1445101260f3SEmmanuel Vadot "failed to disable mmc clock: %d\n", error); 1446ce0618beSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, clock, 1447b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 1448b5be541fSEmmanuel Vadot if (error != 0) { 1449b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 1450b5be541fSEmmanuel Vadot "failed to set frequency to %u Hz: %d\n", 1451ce0618beSEmmanuel Vadot clock, error); 1452b5be541fSEmmanuel Vadot return (error); 1453b5be541fSEmmanuel Vadot } 1454101260f3SEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 1455101260f3SEmmanuel Vadot if (error != 0 && bootverbose) 1456101260f3SEmmanuel Vadot device_printf(sc->aw_dev, 1457101260f3SEmmanuel Vadot "failed to re-enable mmc clock: %d\n", error); 1458b5be541fSEmmanuel Vadot 1459ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->can_calibrate) 1460ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN); 1461ce0618beSEmmanuel Vadot 1462b5be541fSEmmanuel Vadot /* Enable clock. */ 1463b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 1); 1464b5be541fSEmmanuel Vadot if (error != 0) 1465b5be541fSEmmanuel Vadot return (error); 1466b5be541fSEmmanuel Vadot } 1467b5be541fSEmmanuel Vadot 1468b5be541fSEmmanuel Vadot 1469b5be541fSEmmanuel Vadot return (0); 1470b5be541fSEmmanuel Vadot } 1471b5be541fSEmmanuel Vadot 1472b5be541fSEmmanuel Vadot static int 1473b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child) 1474b5be541fSEmmanuel Vadot { 1475b5be541fSEmmanuel Vadot 1476b5be541fSEmmanuel Vadot return (0); 1477b5be541fSEmmanuel Vadot } 1478b5be541fSEmmanuel Vadot 1479b5be541fSEmmanuel Vadot static int 1480b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child) 1481b5be541fSEmmanuel Vadot { 1482b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1483b5be541fSEmmanuel Vadot int error; 1484b5be541fSEmmanuel Vadot 1485b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1486b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1487b5be541fSEmmanuel Vadot while (sc->aw_bus_busy) { 1488b5be541fSEmmanuel Vadot error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0); 1489b5be541fSEmmanuel Vadot if (error != 0) { 1490b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1491b5be541fSEmmanuel Vadot return (error); 1492b5be541fSEmmanuel Vadot } 1493b5be541fSEmmanuel Vadot } 1494b5be541fSEmmanuel Vadot sc->aw_bus_busy++; 1495b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1496b5be541fSEmmanuel Vadot 1497b5be541fSEmmanuel Vadot return (0); 1498b5be541fSEmmanuel Vadot } 1499b5be541fSEmmanuel Vadot 1500b5be541fSEmmanuel Vadot static int 1501b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child) 1502b5be541fSEmmanuel Vadot { 1503b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1504b5be541fSEmmanuel Vadot 1505b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1506b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1507b5be541fSEmmanuel Vadot sc->aw_bus_busy--; 1508b5be541fSEmmanuel Vadot wakeup(sc); 1509b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1510b5be541fSEmmanuel Vadot 1511b5be541fSEmmanuel Vadot return (0); 1512b5be541fSEmmanuel Vadot } 1513b5be541fSEmmanuel Vadot 1514b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = { 1515b5be541fSEmmanuel Vadot /* Device interface */ 1516b5be541fSEmmanuel Vadot DEVMETHOD(device_probe, aw_mmc_probe), 1517b5be541fSEmmanuel Vadot DEVMETHOD(device_attach, aw_mmc_attach), 1518b5be541fSEmmanuel Vadot DEVMETHOD(device_detach, aw_mmc_detach), 1519b5be541fSEmmanuel Vadot 1520b5be541fSEmmanuel Vadot /* Bus interface */ 1521b5be541fSEmmanuel Vadot DEVMETHOD(bus_read_ivar, aw_mmc_read_ivar), 1522b5be541fSEmmanuel Vadot DEVMETHOD(bus_write_ivar, aw_mmc_write_ivar), 1523ef546520SIlya Bakulin DEVMETHOD(bus_add_child, bus_generic_add_child), 1524b5be541fSEmmanuel Vadot 1525b5be541fSEmmanuel Vadot /* MMC bridge interface */ 1526b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_update_ios, aw_mmc_update_ios), 1527b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_request, aw_mmc_request), 1528b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_get_ro, aw_mmc_get_ro), 1529623966e1SEmmanuel Vadot DEVMETHOD(mmcbr_switch_vccq, aw_mmc_switch_vccq), 1530b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_acquire_host, aw_mmc_acquire_host), 1531b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_release_host, aw_mmc_release_host), 1532b5be541fSEmmanuel Vadot 1533b5be541fSEmmanuel Vadot DEVMETHOD_END 1534b5be541fSEmmanuel Vadot }; 1535b5be541fSEmmanuel Vadot 1536b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass; 1537b5be541fSEmmanuel Vadot 1538b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = { 1539b5be541fSEmmanuel Vadot "aw_mmc", 1540b5be541fSEmmanuel Vadot aw_mmc_methods, 1541b5be541fSEmmanuel Vadot sizeof(struct aw_mmc_softc), 1542b5be541fSEmmanuel Vadot }; 1543b5be541fSEmmanuel Vadot 1544b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL, 1545b5be541fSEmmanuel Vadot NULL); 15465e03278fSIlya Bakulin #ifndef MMCCAM 1547b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc); 15485e03278fSIlya Bakulin #endif 1549