1b5be541fSEmmanuel Vadot /*- 2b5be541fSEmmanuel Vadot * Copyright (c) 2013 Alexander Fedorov 3b5be541fSEmmanuel Vadot * All rights reserved. 4b5be541fSEmmanuel Vadot * 5b5be541fSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 6b5be541fSEmmanuel Vadot * modification, are permitted provided that the following conditions 7b5be541fSEmmanuel Vadot * are met: 8b5be541fSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 9b5be541fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 10b5be541fSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 11b5be541fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 12b5be541fSEmmanuel Vadot * documentation and/or other materials provided with the distribution. 13b5be541fSEmmanuel Vadot * 14b5be541fSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15b5be541fSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16b5be541fSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17b5be541fSEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18b5be541fSEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19b5be541fSEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20b5be541fSEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21b5be541fSEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22b5be541fSEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23b5be541fSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24b5be541fSEmmanuel Vadot * SUCH DAMAGE. 25b5be541fSEmmanuel Vadot */ 26b5be541fSEmmanuel Vadot 27b5be541fSEmmanuel Vadot #include <sys/cdefs.h> 28b5be541fSEmmanuel Vadot __FBSDID("$FreeBSD$"); 29b5be541fSEmmanuel Vadot 30b5be541fSEmmanuel Vadot #include <sys/param.h> 31b5be541fSEmmanuel Vadot #include <sys/systm.h> 32b5be541fSEmmanuel Vadot #include <sys/bus.h> 33b5be541fSEmmanuel Vadot #include <sys/kernel.h> 34b5be541fSEmmanuel Vadot #include <sys/lock.h> 35b5be541fSEmmanuel Vadot #include <sys/malloc.h> 36b5be541fSEmmanuel Vadot #include <sys/module.h> 37b5be541fSEmmanuel Vadot #include <sys/mutex.h> 38b5be541fSEmmanuel Vadot #include <sys/resource.h> 39b5be541fSEmmanuel Vadot #include <sys/rman.h> 40b5be541fSEmmanuel Vadot #include <sys/sysctl.h> 41b5be541fSEmmanuel Vadot 42b5be541fSEmmanuel Vadot #include <machine/bus.h> 43b5be541fSEmmanuel Vadot 44b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 45b5be541fSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 46b5be541fSEmmanuel Vadot 47b5be541fSEmmanuel Vadot #include <dev/mmc/bridge.h> 48b5be541fSEmmanuel Vadot #include <dev/mmc/mmcbrvar.h> 49b5be541fSEmmanuel Vadot 50b5be541fSEmmanuel Vadot #include <arm/allwinner/aw_mmc.h> 51b5be541fSEmmanuel Vadot #include <dev/extres/clk/clk.h> 52b5be541fSEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h> 53*ce0618beSEmmanuel Vadot #include <dev/extres/regulator/regulator.h> 54b5be541fSEmmanuel Vadot 55b5be541fSEmmanuel Vadot #define AW_MMC_MEMRES 0 56b5be541fSEmmanuel Vadot #define AW_MMC_IRQRES 1 57b5be541fSEmmanuel Vadot #define AW_MMC_RESSZ 2 58b5be541fSEmmanuel Vadot #define AW_MMC_DMA_SEGS ((MAXPHYS / PAGE_SIZE) + 1) 59b5be541fSEmmanuel Vadot #define AW_MMC_DMA_FTRGLEVEL 0x20070008 60b5be541fSEmmanuel Vadot #define AW_MMC_RESET_RETRY 1000 61b5be541fSEmmanuel Vadot 62b5be541fSEmmanuel Vadot #define CARD_ID_FREQUENCY 400000 63b5be541fSEmmanuel Vadot 64*ce0618beSEmmanuel Vadot struct aw_mmc_conf { 65*ce0618beSEmmanuel Vadot uint32_t dma_xferlen; 66*ce0618beSEmmanuel Vadot bool mask_data0; 67*ce0618beSEmmanuel Vadot bool can_calibrate; 68*ce0618beSEmmanuel Vadot bool new_timing; 69*ce0618beSEmmanuel Vadot }; 70*ce0618beSEmmanuel Vadot 71*ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a10_mmc_conf = { 72*ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 73*ce0618beSEmmanuel Vadot }; 74*ce0618beSEmmanuel Vadot 75*ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a13_mmc_conf = { 76*ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 77*ce0618beSEmmanuel Vadot }; 78*ce0618beSEmmanuel Vadot 79*ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_mmc_conf = { 80*ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 81*ce0618beSEmmanuel Vadot .mask_data0 = true, 82*ce0618beSEmmanuel Vadot .can_calibrate = true, 83*ce0618beSEmmanuel Vadot .new_timing = true, 84*ce0618beSEmmanuel Vadot }; 85*ce0618beSEmmanuel Vadot 86*ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_emmc_conf = { 87*ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 88*ce0618beSEmmanuel Vadot .can_calibrate = true, 89*ce0618beSEmmanuel Vadot }; 90*ce0618beSEmmanuel Vadot 91b5be541fSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 92*ce0618beSEmmanuel Vadot {"allwinner,sun4i-a10-mmc", (uintptr_t)&a10_mmc_conf}, 93*ce0618beSEmmanuel Vadot {"allwinner,sun5i-a13-mmc", (uintptr_t)&a13_mmc_conf}, 94*ce0618beSEmmanuel Vadot {"allwinner,sun7i-a20-mmc", (uintptr_t)&a13_mmc_conf}, 95*ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-mmc", (uintptr_t)&a64_mmc_conf}, 96*ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-emmc", (uintptr_t)&a64_emmc_conf}, 97b5be541fSEmmanuel Vadot {NULL, 0} 98b5be541fSEmmanuel Vadot }; 99b5be541fSEmmanuel Vadot 100b5be541fSEmmanuel Vadot struct aw_mmc_softc { 101b5be541fSEmmanuel Vadot device_t aw_dev; 102b5be541fSEmmanuel Vadot clk_t aw_clk_ahb; 103b5be541fSEmmanuel Vadot clk_t aw_clk_mmc; 104b5be541fSEmmanuel Vadot hwreset_t aw_rst_ahb; 105b5be541fSEmmanuel Vadot int aw_bus_busy; 106b5be541fSEmmanuel Vadot int aw_resid; 107b5be541fSEmmanuel Vadot int aw_timeout; 108b5be541fSEmmanuel Vadot struct callout aw_timeoutc; 109b5be541fSEmmanuel Vadot struct mmc_host aw_host; 110b5be541fSEmmanuel Vadot struct mmc_request * aw_req; 111b5be541fSEmmanuel Vadot struct mtx aw_mtx; 112b5be541fSEmmanuel Vadot struct resource * aw_res[AW_MMC_RESSZ]; 113*ce0618beSEmmanuel Vadot struct aw_mmc_conf * aw_mmc_conf; 114b5be541fSEmmanuel Vadot uint32_t aw_intr; 115b5be541fSEmmanuel Vadot uint32_t aw_intr_wait; 116b5be541fSEmmanuel Vadot void * aw_intrhand; 117*ce0618beSEmmanuel Vadot int32_t aw_vdd; 118*ce0618beSEmmanuel Vadot regulator_t aw_reg_vmmc; 119*ce0618beSEmmanuel Vadot regulator_t aw_reg_vqmmc; 120b5be541fSEmmanuel Vadot 121b5be541fSEmmanuel Vadot /* Fields required for DMA access. */ 122b5be541fSEmmanuel Vadot bus_addr_t aw_dma_desc_phys; 123b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_map; 124b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_tag; 125b5be541fSEmmanuel Vadot void * aw_dma_desc; 126b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_buf_map; 127b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_buf_tag; 128b5be541fSEmmanuel Vadot int aw_dma_map_err; 129b5be541fSEmmanuel Vadot }; 130b5be541fSEmmanuel Vadot 131b5be541fSEmmanuel Vadot static struct resource_spec aw_mmc_res_spec[] = { 132b5be541fSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE }, 133b5be541fSEmmanuel Vadot { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 134b5be541fSEmmanuel Vadot { -1, 0, 0 } 135b5be541fSEmmanuel Vadot }; 136b5be541fSEmmanuel Vadot 137b5be541fSEmmanuel Vadot static int aw_mmc_probe(device_t); 138b5be541fSEmmanuel Vadot static int aw_mmc_attach(device_t); 139b5be541fSEmmanuel Vadot static int aw_mmc_detach(device_t); 140b5be541fSEmmanuel Vadot static int aw_mmc_setup_dma(struct aw_mmc_softc *); 141b5be541fSEmmanuel Vadot static int aw_mmc_reset(struct aw_mmc_softc *); 142b5be541fSEmmanuel Vadot static void aw_mmc_intr(void *); 143b5be541fSEmmanuel Vadot static int aw_mmc_update_clock(struct aw_mmc_softc *, uint32_t); 144b5be541fSEmmanuel Vadot 145b5be541fSEmmanuel Vadot static int aw_mmc_update_ios(device_t, device_t); 146b5be541fSEmmanuel Vadot static int aw_mmc_request(device_t, device_t, struct mmc_request *); 147b5be541fSEmmanuel Vadot static int aw_mmc_get_ro(device_t, device_t); 148b5be541fSEmmanuel Vadot static int aw_mmc_acquire_host(device_t, device_t); 149b5be541fSEmmanuel Vadot static int aw_mmc_release_host(device_t, device_t); 150b5be541fSEmmanuel Vadot 151b5be541fSEmmanuel Vadot #define AW_MMC_LOCK(_sc) mtx_lock(&(_sc)->aw_mtx) 152b5be541fSEmmanuel Vadot #define AW_MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->aw_mtx) 153b5be541fSEmmanuel Vadot #define AW_MMC_READ_4(_sc, _reg) \ 154b5be541fSEmmanuel Vadot bus_read_4((_sc)->aw_res[AW_MMC_MEMRES], _reg) 155b5be541fSEmmanuel Vadot #define AW_MMC_WRITE_4(_sc, _reg, _value) \ 156b5be541fSEmmanuel Vadot bus_write_4((_sc)->aw_res[AW_MMC_MEMRES], _reg, _value) 157b5be541fSEmmanuel Vadot 158b5be541fSEmmanuel Vadot static int 159b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev) 160b5be541fSEmmanuel Vadot { 161b5be541fSEmmanuel Vadot 162b5be541fSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 163b5be541fSEmmanuel Vadot return (ENXIO); 164b5be541fSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 165b5be541fSEmmanuel Vadot return (ENXIO); 166b5be541fSEmmanuel Vadot 167b5be541fSEmmanuel Vadot device_set_desc(dev, "Allwinner Integrated MMC/SD controller"); 168b5be541fSEmmanuel Vadot 169b5be541fSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 170b5be541fSEmmanuel Vadot } 171b5be541fSEmmanuel Vadot 172b5be541fSEmmanuel Vadot static int 173b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev) 174b5be541fSEmmanuel Vadot { 175b5be541fSEmmanuel Vadot device_t child; 176b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 177b5be541fSEmmanuel Vadot struct sysctl_ctx_list *ctx; 178b5be541fSEmmanuel Vadot struct sysctl_oid_list *tree; 179b5be541fSEmmanuel Vadot uint32_t bus_width; 180b5be541fSEmmanuel Vadot phandle_t node; 181b5be541fSEmmanuel Vadot int error; 182b5be541fSEmmanuel Vadot 183b5be541fSEmmanuel Vadot node = ofw_bus_get_node(dev); 184b5be541fSEmmanuel Vadot sc = device_get_softc(dev); 185b5be541fSEmmanuel Vadot sc->aw_dev = dev; 186*ce0618beSEmmanuel Vadot 187*ce0618beSEmmanuel Vadot sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 188*ce0618beSEmmanuel Vadot 189b5be541fSEmmanuel Vadot sc->aw_req = NULL; 190b5be541fSEmmanuel Vadot if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) { 191b5be541fSEmmanuel Vadot device_printf(dev, "cannot allocate device resources\n"); 192b5be541fSEmmanuel Vadot return (ENXIO); 193b5be541fSEmmanuel Vadot } 194b5be541fSEmmanuel Vadot if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES], 195b5be541fSEmmanuel Vadot INTR_TYPE_MISC | INTR_MPSAFE, NULL, aw_mmc_intr, sc, 196b5be541fSEmmanuel Vadot &sc->aw_intrhand)) { 197b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 198b5be541fSEmmanuel Vadot device_printf(dev, "cannot setup interrupt handler\n"); 199b5be541fSEmmanuel Vadot return (ENXIO); 200b5be541fSEmmanuel Vadot } 201b5be541fSEmmanuel Vadot mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc", 202b5be541fSEmmanuel Vadot MTX_DEF); 203b5be541fSEmmanuel Vadot callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0); 204b5be541fSEmmanuel Vadot 205b5be541fSEmmanuel Vadot /* De-assert reset */ 206b5be541fSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) { 207b5be541fSEmmanuel Vadot error = hwreset_deassert(sc->aw_rst_ahb); 208b5be541fSEmmanuel Vadot if (error != 0) { 209b5be541fSEmmanuel Vadot device_printf(dev, "cannot de-assert reset\n"); 210b5be541fSEmmanuel Vadot goto fail; 211b5be541fSEmmanuel Vadot } 212b5be541fSEmmanuel Vadot } 213b5be541fSEmmanuel Vadot 214b5be541fSEmmanuel Vadot /* Activate the module clock. */ 215b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb); 216b5be541fSEmmanuel Vadot if (error != 0) { 217b5be541fSEmmanuel Vadot device_printf(dev, "cannot get ahb clock\n"); 218b5be541fSEmmanuel Vadot goto fail; 219b5be541fSEmmanuel Vadot } 220b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_ahb); 221b5be541fSEmmanuel Vadot if (error != 0) { 222b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable ahb clock\n"); 223b5be541fSEmmanuel Vadot goto fail; 224b5be541fSEmmanuel Vadot } 225b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc); 226b5be541fSEmmanuel Vadot if (error != 0) { 227b5be541fSEmmanuel Vadot device_printf(dev, "cannot get mmc clock\n"); 228b5be541fSEmmanuel Vadot goto fail; 229b5be541fSEmmanuel Vadot } 230b5be541fSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY, 231b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 232b5be541fSEmmanuel Vadot if (error != 0) { 233b5be541fSEmmanuel Vadot device_printf(dev, "cannot init mmc clock\n"); 234b5be541fSEmmanuel Vadot goto fail; 235b5be541fSEmmanuel Vadot } 236b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 237b5be541fSEmmanuel Vadot if (error != 0) { 238b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable mmc clock\n"); 239b5be541fSEmmanuel Vadot goto fail; 240b5be541fSEmmanuel Vadot } 241b5be541fSEmmanuel Vadot 242b5be541fSEmmanuel Vadot sc->aw_timeout = 10; 243b5be541fSEmmanuel Vadot ctx = device_get_sysctl_ctx(dev); 244b5be541fSEmmanuel Vadot tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 245b5be541fSEmmanuel Vadot SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW, 246b5be541fSEmmanuel Vadot &sc->aw_timeout, 0, "Request timeout in seconds"); 247b5be541fSEmmanuel Vadot 248b5be541fSEmmanuel Vadot /* Hardware reset */ 249b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_HWRST, 1); 250b5be541fSEmmanuel Vadot DELAY(100); 251b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_HWRST, 0); 252b5be541fSEmmanuel Vadot DELAY(500); 253b5be541fSEmmanuel Vadot 254b5be541fSEmmanuel Vadot /* Soft Reset controller. */ 255b5be541fSEmmanuel Vadot if (aw_mmc_reset(sc) != 0) { 256b5be541fSEmmanuel Vadot device_printf(dev, "cannot reset the controller\n"); 257b5be541fSEmmanuel Vadot goto fail; 258b5be541fSEmmanuel Vadot } 259b5be541fSEmmanuel Vadot 260b5be541fSEmmanuel Vadot if (aw_mmc_setup_dma(sc) != 0) { 261b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "Couldn't setup DMA!\n"); 262b5be541fSEmmanuel Vadot goto fail; 263b5be541fSEmmanuel Vadot } 264b5be541fSEmmanuel Vadot 265b5be541fSEmmanuel Vadot if (OF_getencprop(node, "bus-width", &bus_width, sizeof(uint32_t)) <= 0) 266b5be541fSEmmanuel Vadot bus_width = 4; 267b5be541fSEmmanuel Vadot 268*ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply", 269*ce0618beSEmmanuel Vadot &sc->aw_reg_vmmc) == 0 && bootverbose) 270*ce0618beSEmmanuel Vadot device_printf(dev, "vmmc-supply regulator found\n"); 271*ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply", 272*ce0618beSEmmanuel Vadot &sc->aw_reg_vqmmc) == 0 && bootverbose) 273*ce0618beSEmmanuel Vadot device_printf(dev, "vqmmc-supply regulator found\n"); 274*ce0618beSEmmanuel Vadot 275b5be541fSEmmanuel Vadot sc->aw_host.f_min = 400000; 276b5be541fSEmmanuel Vadot sc->aw_host.f_max = 52000000; 277b5be541fSEmmanuel Vadot sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340; 278*ce0618beSEmmanuel Vadot sc->aw_host.caps = MMC_CAP_HSPEED | MMC_CAP_UHS_SDR12 | 279*ce0618beSEmmanuel Vadot MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 | 280*ce0618beSEmmanuel Vadot MMC_CAP_UHS_DDR50 | MMC_CAP_MMC_DDR52; 281*ce0618beSEmmanuel Vadot 282*ce0618beSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_330 /* | MMC_CAP_SIGNALING_180 */; 283*ce0618beSEmmanuel Vadot 284b5be541fSEmmanuel Vadot if (bus_width >= 4) 285b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_4_BIT_DATA; 286b5be541fSEmmanuel Vadot if (bus_width >= 8) 287b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_8_BIT_DATA; 288b5be541fSEmmanuel Vadot 289b5be541fSEmmanuel Vadot child = device_add_child(dev, "mmc", -1); 290b5be541fSEmmanuel Vadot if (child == NULL) { 291b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC bus failed!\n"); 292b5be541fSEmmanuel Vadot goto fail; 293b5be541fSEmmanuel Vadot } 294b5be541fSEmmanuel Vadot if (device_probe_and_attach(child) != 0) { 295b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC child failed!\n"); 296b5be541fSEmmanuel Vadot device_delete_child(dev, child); 297b5be541fSEmmanuel Vadot goto fail; 298b5be541fSEmmanuel Vadot } 299b5be541fSEmmanuel Vadot 300b5be541fSEmmanuel Vadot return (0); 301b5be541fSEmmanuel Vadot 302b5be541fSEmmanuel Vadot fail: 303b5be541fSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 304b5be541fSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 305b5be541fSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 306b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 307b5be541fSEmmanuel Vadot 308b5be541fSEmmanuel Vadot return (ENXIO); 309b5be541fSEmmanuel Vadot } 310b5be541fSEmmanuel Vadot 311b5be541fSEmmanuel Vadot static int 312b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev) 313b5be541fSEmmanuel Vadot { 314b5be541fSEmmanuel Vadot 315b5be541fSEmmanuel Vadot return (EBUSY); 316b5be541fSEmmanuel Vadot } 317b5be541fSEmmanuel Vadot 318b5be541fSEmmanuel Vadot static void 319b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 320b5be541fSEmmanuel Vadot { 321b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 322b5be541fSEmmanuel Vadot 323b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 324b5be541fSEmmanuel Vadot if (err) { 325b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 326b5be541fSEmmanuel Vadot return; 327b5be541fSEmmanuel Vadot } 328b5be541fSEmmanuel Vadot sc->aw_dma_desc_phys = segs[0].ds_addr; 329b5be541fSEmmanuel Vadot } 330b5be541fSEmmanuel Vadot 331b5be541fSEmmanuel Vadot static int 332b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc) 333b5be541fSEmmanuel Vadot { 334b5be541fSEmmanuel Vadot int dma_desc_size, error; 335b5be541fSEmmanuel Vadot 336b5be541fSEmmanuel Vadot /* Allocate the DMA descriptor memory. */ 337b5be541fSEmmanuel Vadot dma_desc_size = sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS; 338b5be541fSEmmanuel Vadot error = bus_dma_tag_create(bus_get_dma_tag(sc->aw_dev), 339b5be541fSEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, 340b5be541fSEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 341b5be541fSEmmanuel Vadot dma_desc_size, 1, dma_desc_size, 0, NULL, NULL, &sc->aw_dma_tag); 342b5be541fSEmmanuel Vadot if (error) 343b5be541fSEmmanuel Vadot return (error); 344b5be541fSEmmanuel Vadot error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc, 345b5be541fSEmmanuel Vadot BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->aw_dma_map); 346b5be541fSEmmanuel Vadot if (error) 347b5be541fSEmmanuel Vadot return (error); 348b5be541fSEmmanuel Vadot 349b5be541fSEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_tag, sc->aw_dma_map, 350b5be541fSEmmanuel Vadot sc->aw_dma_desc, dma_desc_size, aw_dma_desc_cb, sc, 0); 351b5be541fSEmmanuel Vadot if (error) 352b5be541fSEmmanuel Vadot return (error); 353b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 354b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 355b5be541fSEmmanuel Vadot 356b5be541fSEmmanuel Vadot /* Create the DMA map for data transfers. */ 357b5be541fSEmmanuel Vadot error = bus_dma_tag_create(bus_get_dma_tag(sc->aw_dev), 358b5be541fSEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, 359b5be541fSEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 360*ce0618beSEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS, 361*ce0618beSEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen, BUS_DMA_ALLOCNOW, NULL, NULL, 362b5be541fSEmmanuel Vadot &sc->aw_dma_buf_tag); 363b5be541fSEmmanuel Vadot if (error) 364b5be541fSEmmanuel Vadot return (error); 365b5be541fSEmmanuel Vadot error = bus_dmamap_create(sc->aw_dma_buf_tag, 0, 366b5be541fSEmmanuel Vadot &sc->aw_dma_buf_map); 367b5be541fSEmmanuel Vadot if (error) 368b5be541fSEmmanuel Vadot return (error); 369b5be541fSEmmanuel Vadot 370b5be541fSEmmanuel Vadot return (0); 371b5be541fSEmmanuel Vadot } 372b5be541fSEmmanuel Vadot 373b5be541fSEmmanuel Vadot static void 374b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 375b5be541fSEmmanuel Vadot { 376b5be541fSEmmanuel Vadot int i; 377b5be541fSEmmanuel Vadot struct aw_mmc_dma_desc *dma_desc; 378b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 379b5be541fSEmmanuel Vadot 380b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 381b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 382b5be541fSEmmanuel Vadot 383b5be541fSEmmanuel Vadot if (err) 384b5be541fSEmmanuel Vadot return; 385b5be541fSEmmanuel Vadot 386b5be541fSEmmanuel Vadot dma_desc = sc->aw_dma_desc; 387b5be541fSEmmanuel Vadot for (i = 0; i < nsegs; i++) { 388b5be541fSEmmanuel Vadot dma_desc[i].buf_size = segs[i].ds_len; 389b5be541fSEmmanuel Vadot dma_desc[i].buf_addr = segs[i].ds_addr; 390b5be541fSEmmanuel Vadot dma_desc[i].config = AW_MMC_DMA_CONFIG_CH | 391b5be541fSEmmanuel Vadot AW_MMC_DMA_CONFIG_OWN; 392b5be541fSEmmanuel Vadot if (i == 0) 393b5be541fSEmmanuel Vadot dma_desc[i].config |= AW_MMC_DMA_CONFIG_FD; 394b5be541fSEmmanuel Vadot if (i < (nsegs - 1)) { 395b5be541fSEmmanuel Vadot dma_desc[i].config |= AW_MMC_DMA_CONFIG_DIC; 396b5be541fSEmmanuel Vadot dma_desc[i].next = sc->aw_dma_desc_phys + 397b5be541fSEmmanuel Vadot ((i + 1) * sizeof(struct aw_mmc_dma_desc)); 398b5be541fSEmmanuel Vadot } else { 399b5be541fSEmmanuel Vadot dma_desc[i].config |= AW_MMC_DMA_CONFIG_LD | 400b5be541fSEmmanuel Vadot AW_MMC_DMA_CONFIG_ER; 401b5be541fSEmmanuel Vadot dma_desc[i].next = 0; 402b5be541fSEmmanuel Vadot } 403b5be541fSEmmanuel Vadot } 404b5be541fSEmmanuel Vadot } 405b5be541fSEmmanuel Vadot 406b5be541fSEmmanuel Vadot static int 407b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc) 408b5be541fSEmmanuel Vadot { 409b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 410b5be541fSEmmanuel Vadot int error; 411b5be541fSEmmanuel Vadot struct mmc_command *cmd; 412b5be541fSEmmanuel Vadot uint32_t val; 413b5be541fSEmmanuel Vadot 414b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 415*ce0618beSEmmanuel Vadot if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS)) 416b5be541fSEmmanuel Vadot return (EFBIG); 417b5be541fSEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 418b5be541fSEmmanuel Vadot cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0); 419b5be541fSEmmanuel Vadot if (error) 420b5be541fSEmmanuel Vadot return (error); 421b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 422b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 423b5be541fSEmmanuel Vadot 424b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 425b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREWRITE; 426b5be541fSEmmanuel Vadot else 427b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREREAD; 428b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op); 429b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE); 430b5be541fSEmmanuel Vadot 431b5be541fSEmmanuel Vadot /* Enable DMA */ 432b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 433b5be541fSEmmanuel Vadot val &= ~AW_MMC_CTRL_FIFO_AC_MOD; 434b5be541fSEmmanuel Vadot val |= AW_MMC_CTRL_DMA_ENB; 435b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 436b5be541fSEmmanuel Vadot 437b5be541fSEmmanuel Vadot /* Reset DMA */ 438b5be541fSEmmanuel Vadot val |= AW_MMC_CTRL_DMA_RST; 439b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 440b5be541fSEmmanuel Vadot 441b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST); 442b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, 443b5be541fSEmmanuel Vadot AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST); 444b5be541fSEmmanuel Vadot 445b5be541fSEmmanuel Vadot /* Enable RX or TX DMA interrupt */ 446b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 447b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_TX_INT; 448b5be541fSEmmanuel Vadot else 449b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_RX_INT; 450b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val); 451b5be541fSEmmanuel Vadot 452b5be541fSEmmanuel Vadot /* Set DMA descritptor list address */ 453b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys); 454b5be541fSEmmanuel Vadot 455b5be541fSEmmanuel Vadot /* FIFO trigger level */ 456b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL); 457b5be541fSEmmanuel Vadot 458b5be541fSEmmanuel Vadot return (0); 459b5be541fSEmmanuel Vadot } 460b5be541fSEmmanuel Vadot 461b5be541fSEmmanuel Vadot static int 462b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc) 463b5be541fSEmmanuel Vadot { 464b5be541fSEmmanuel Vadot int timeout; 465b5be541fSEmmanuel Vadot 466b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, AW_MMC_RESET); 467b5be541fSEmmanuel Vadot timeout = 1000; 468b5be541fSEmmanuel Vadot while (--timeout > 0) { 469b5be541fSEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_RESET) == 0) 470b5be541fSEmmanuel Vadot break; 471b5be541fSEmmanuel Vadot DELAY(100); 472b5be541fSEmmanuel Vadot } 473b5be541fSEmmanuel Vadot if (timeout == 0) 474b5be541fSEmmanuel Vadot return (ETIMEDOUT); 475b5be541fSEmmanuel Vadot 476b5be541fSEmmanuel Vadot /* Set the timeout. */ 477b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_TMOR, 478b5be541fSEmmanuel Vadot AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) | 479b5be541fSEmmanuel Vadot AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK)); 480b5be541fSEmmanuel Vadot 481b5be541fSEmmanuel Vadot /* Clear pending interrupts. */ 482b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 483b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff); 484b5be541fSEmmanuel Vadot /* Unmask interrupts. */ 485b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, 486b5be541fSEmmanuel Vadot AW_MMC_INT_CMD_DONE | AW_MMC_INT_ERR_BIT | 487b5be541fSEmmanuel Vadot AW_MMC_INT_DATA_OVER | AW_MMC_INT_AUTO_STOP_DONE); 488b5be541fSEmmanuel Vadot /* Enable interrupts and AHB access. */ 489b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, 490b5be541fSEmmanuel Vadot AW_MMC_READ_4(sc, AW_MMC_GCTL) | AW_MMC_CTRL_INT_ENB); 491b5be541fSEmmanuel Vadot 492b5be541fSEmmanuel Vadot return (0); 493b5be541fSEmmanuel Vadot } 494b5be541fSEmmanuel Vadot 495b5be541fSEmmanuel Vadot static void 496b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc) 497b5be541fSEmmanuel Vadot { 498b5be541fSEmmanuel Vadot struct mmc_command *cmd; 499b5be541fSEmmanuel Vadot struct mmc_request *req; 500b5be541fSEmmanuel Vadot uint32_t val, mask; 501b5be541fSEmmanuel Vadot int retry; 502b5be541fSEmmanuel Vadot 503b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 504b5be541fSEmmanuel Vadot if (cmd->error != MMC_ERR_NONE) { 505b5be541fSEmmanuel Vadot /* Reset the FIFO and DMA engines. */ 506b5be541fSEmmanuel Vadot mask = AW_MMC_CTRL_FIFO_RST | AW_MMC_CTRL_DMA_RST; 507b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 508b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask); 509b5be541fSEmmanuel Vadot 510b5be541fSEmmanuel Vadot retry = AW_MMC_RESET_RETRY; 511b5be541fSEmmanuel Vadot while (--retry > 0) { 512b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 513b5be541fSEmmanuel Vadot if ((val & mask) == 0) 514b5be541fSEmmanuel Vadot break; 515b5be541fSEmmanuel Vadot DELAY(10); 516b5be541fSEmmanuel Vadot } 517b5be541fSEmmanuel Vadot if (retry == 0) 518b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 519b5be541fSEmmanuel Vadot "timeout resetting DMA/FIFO\n"); 520b5be541fSEmmanuel Vadot aw_mmc_update_clock(sc, 1); 521b5be541fSEmmanuel Vadot } 522b5be541fSEmmanuel Vadot 523b5be541fSEmmanuel Vadot req = sc->aw_req; 524b5be541fSEmmanuel Vadot callout_stop(&sc->aw_timeoutc); 525b5be541fSEmmanuel Vadot sc->aw_req = NULL; 526b5be541fSEmmanuel Vadot sc->aw_intr = 0; 527b5be541fSEmmanuel Vadot sc->aw_resid = 0; 528b5be541fSEmmanuel Vadot sc->aw_dma_map_err = 0; 529b5be541fSEmmanuel Vadot sc->aw_intr_wait = 0; 530b5be541fSEmmanuel Vadot req->done(req); 531b5be541fSEmmanuel Vadot } 532b5be541fSEmmanuel Vadot 533b5be541fSEmmanuel Vadot static void 534b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc) 535b5be541fSEmmanuel Vadot { 536b5be541fSEmmanuel Vadot int timeout; 537b5be541fSEmmanuel Vadot struct mmc_command *cmd; 538b5be541fSEmmanuel Vadot uint32_t status; 539b5be541fSEmmanuel Vadot 540b5be541fSEmmanuel Vadot timeout = 1000; 541b5be541fSEmmanuel Vadot while (--timeout > 0) { 542b5be541fSEmmanuel Vadot status = AW_MMC_READ_4(sc, AW_MMC_STAR); 543b5be541fSEmmanuel Vadot if ((status & AW_MMC_STAR_CARD_BUSY) == 0) 544b5be541fSEmmanuel Vadot break; 545b5be541fSEmmanuel Vadot DELAY(1000); 546b5be541fSEmmanuel Vadot } 547b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 548b5be541fSEmmanuel Vadot if (timeout == 0) { 549b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 550b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 551b5be541fSEmmanuel Vadot return; 552b5be541fSEmmanuel Vadot } 553b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) { 554b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) { 555b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3); 556b5be541fSEmmanuel Vadot cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2); 557b5be541fSEmmanuel Vadot cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1); 558b5be541fSEmmanuel Vadot cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 559b5be541fSEmmanuel Vadot } else 560b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 561b5be541fSEmmanuel Vadot } 562b5be541fSEmmanuel Vadot /* All data has been transferred ? */ 563b5be541fSEmmanuel Vadot if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len) 564b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 565b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 566b5be541fSEmmanuel Vadot } 567b5be541fSEmmanuel Vadot 568b5be541fSEmmanuel Vadot static void 569b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg) 570b5be541fSEmmanuel Vadot { 571b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 572b5be541fSEmmanuel Vadot 573b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 574b5be541fSEmmanuel Vadot if (sc->aw_req != NULL) { 575b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "controller timeout\n"); 576b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_TIMEOUT; 577b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 578b5be541fSEmmanuel Vadot } else 579b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 580b5be541fSEmmanuel Vadot "Spurious timeout - no active request\n"); 581b5be541fSEmmanuel Vadot } 582b5be541fSEmmanuel Vadot 583b5be541fSEmmanuel Vadot static void 584b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg) 585b5be541fSEmmanuel Vadot { 586b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 587b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 588b5be541fSEmmanuel Vadot struct mmc_data *data; 589b5be541fSEmmanuel Vadot uint32_t idst, imask, rint; 590b5be541fSEmmanuel Vadot 591b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 592b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 593b5be541fSEmmanuel Vadot rint = AW_MMC_READ_4(sc, AW_MMC_RISR); 594b5be541fSEmmanuel Vadot idst = AW_MMC_READ_4(sc, AW_MMC_IDST); 595b5be541fSEmmanuel Vadot imask = AW_MMC_READ_4(sc, AW_MMC_IMKR); 596b5be541fSEmmanuel Vadot if (idst == 0 && imask == 0 && rint == 0) { 597b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 598b5be541fSEmmanuel Vadot return; 599b5be541fSEmmanuel Vadot } 600b5be541fSEmmanuel Vadot #ifdef DEBUG 601b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n", 602b5be541fSEmmanuel Vadot idst, imask, rint); 603b5be541fSEmmanuel Vadot #endif 604b5be541fSEmmanuel Vadot if (sc->aw_req == NULL) { 605b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 606b5be541fSEmmanuel Vadot "Spurious interrupt - no active request, rint: 0x%08X\n", 607b5be541fSEmmanuel Vadot rint); 608b5be541fSEmmanuel Vadot goto end; 609b5be541fSEmmanuel Vadot } 610b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_ERR_BIT) { 611*ce0618beSEmmanuel Vadot if (bootverbose) 612b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint); 613b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_RESP_TIMEOUT) 614b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_TIMEOUT; 615b5be541fSEmmanuel Vadot else 616b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_FAILED; 617b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 618b5be541fSEmmanuel Vadot goto end; 619b5be541fSEmmanuel Vadot } 620b5be541fSEmmanuel Vadot if (idst & AW_MMC_IDST_ERROR) { 621b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst); 622b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_FAILED; 623b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 624b5be541fSEmmanuel Vadot goto end; 625b5be541fSEmmanuel Vadot } 626b5be541fSEmmanuel Vadot 627b5be541fSEmmanuel Vadot sc->aw_intr |= rint; 628b5be541fSEmmanuel Vadot data = sc->aw_req->cmd->data; 629b5be541fSEmmanuel Vadot if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) { 630b5be541fSEmmanuel Vadot if (data->flags & MMC_DATA_WRITE) 631b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTWRITE; 632b5be541fSEmmanuel Vadot else 633b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTREAD; 634b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 635b5be541fSEmmanuel Vadot sync_op); 636b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, 637b5be541fSEmmanuel Vadot BUS_DMASYNC_POSTWRITE); 638b5be541fSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 639b5be541fSEmmanuel Vadot sc->aw_resid = data->len >> 2; 640b5be541fSEmmanuel Vadot } 641b5be541fSEmmanuel Vadot if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait) 642b5be541fSEmmanuel Vadot aw_mmc_req_ok(sc); 643b5be541fSEmmanuel Vadot 644b5be541fSEmmanuel Vadot end: 645b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst); 646b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint); 647b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 648b5be541fSEmmanuel Vadot } 649b5be541fSEmmanuel Vadot 650b5be541fSEmmanuel Vadot static int 651b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req) 652b5be541fSEmmanuel Vadot { 653b5be541fSEmmanuel Vadot int blksz; 654b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 655b5be541fSEmmanuel Vadot struct mmc_command *cmd; 656b5be541fSEmmanuel Vadot uint32_t cmdreg; 657b5be541fSEmmanuel Vadot int err; 658b5be541fSEmmanuel Vadot 659b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 660b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 661b5be541fSEmmanuel Vadot if (sc->aw_req) { 662b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 663b5be541fSEmmanuel Vadot return (EBUSY); 664b5be541fSEmmanuel Vadot } 665b5be541fSEmmanuel Vadot sc->aw_req = req; 666b5be541fSEmmanuel Vadot cmd = req->cmd; 667b5be541fSEmmanuel Vadot cmdreg = AW_MMC_CMDR_LOAD; 668b5be541fSEmmanuel Vadot if (cmd->opcode == MMC_GO_IDLE_STATE) 669b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ; 670b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) 671b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_RESP_RCV; 672b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) 673b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_LONG_RESP; 674b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_CRC) 675b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC; 676b5be541fSEmmanuel Vadot 677b5be541fSEmmanuel Vadot sc->aw_intr = 0; 678b5be541fSEmmanuel Vadot sc->aw_resid = 0; 679b5be541fSEmmanuel Vadot sc->aw_intr_wait = AW_MMC_INT_CMD_DONE; 680b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_NONE; 681b5be541fSEmmanuel Vadot if (cmd->data != NULL) { 682b5be541fSEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER; 683b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER; 684b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_MULTI) { 685b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG; 686b5be541fSEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE; 687b5be541fSEmmanuel Vadot } 688b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 689b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DIR_WRITE; 690b5be541fSEmmanuel Vadot blksz = min(cmd->data->len, MMC_SECTOR_SIZE); 691b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz); 692b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 693b5be541fSEmmanuel Vadot 694b5be541fSEmmanuel Vadot err = aw_mmc_prepare_dma(sc); 695b5be541fSEmmanuel Vadot if (err != 0) 696b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err); 697b5be541fSEmmanuel Vadot } 698b5be541fSEmmanuel Vadot 699b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg); 700b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 701b5be541fSEmmanuel Vadot callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz, 702b5be541fSEmmanuel Vadot aw_mmc_timeout, sc); 703b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 704b5be541fSEmmanuel Vadot 705b5be541fSEmmanuel Vadot return (0); 706b5be541fSEmmanuel Vadot } 707b5be541fSEmmanuel Vadot 708b5be541fSEmmanuel Vadot static int 709b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which, 710b5be541fSEmmanuel Vadot uintptr_t *result) 711b5be541fSEmmanuel Vadot { 712b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 713b5be541fSEmmanuel Vadot 714b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 715b5be541fSEmmanuel Vadot switch (which) { 716b5be541fSEmmanuel Vadot default: 717b5be541fSEmmanuel Vadot return (EINVAL); 718b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 719b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_mode; 720b5be541fSEmmanuel Vadot break; 721b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 722b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_width; 723b5be541fSEmmanuel Vadot break; 724b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 725b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.chip_select; 726b5be541fSEmmanuel Vadot break; 727b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 728b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.clock; 729b5be541fSEmmanuel Vadot break; 730b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 731b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_min; 732b5be541fSEmmanuel Vadot break; 733b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 734b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_max; 735b5be541fSEmmanuel Vadot break; 736b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 737b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.host_ocr; 738b5be541fSEmmanuel Vadot break; 739b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 740b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.mode; 741b5be541fSEmmanuel Vadot break; 742b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 743b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ocr; 744b5be541fSEmmanuel Vadot break; 745b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 746b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.power_mode; 747b5be541fSEmmanuel Vadot break; 748b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 749b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.vdd; 750b5be541fSEmmanuel Vadot break; 751b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 752b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.caps; 753b5be541fSEmmanuel Vadot break; 754*ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 755*ce0618beSEmmanuel Vadot *(int *)result = sc->aw_host.ios.timing; 756*ce0618beSEmmanuel Vadot break; 757b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 758b5be541fSEmmanuel Vadot *(int *)result = 65535; 759b5be541fSEmmanuel Vadot break; 760b5be541fSEmmanuel Vadot } 761b5be541fSEmmanuel Vadot 762b5be541fSEmmanuel Vadot return (0); 763b5be541fSEmmanuel Vadot } 764b5be541fSEmmanuel Vadot 765b5be541fSEmmanuel Vadot static int 766b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which, 767b5be541fSEmmanuel Vadot uintptr_t value) 768b5be541fSEmmanuel Vadot { 769b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 770b5be541fSEmmanuel Vadot 771b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 772b5be541fSEmmanuel Vadot switch (which) { 773b5be541fSEmmanuel Vadot default: 774b5be541fSEmmanuel Vadot return (EINVAL); 775b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 776b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_mode = value; 777b5be541fSEmmanuel Vadot break; 778b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 779b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_width = value; 780b5be541fSEmmanuel Vadot break; 781b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 782b5be541fSEmmanuel Vadot sc->aw_host.ios.chip_select = value; 783b5be541fSEmmanuel Vadot break; 784b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 785b5be541fSEmmanuel Vadot sc->aw_host.ios.clock = value; 786b5be541fSEmmanuel Vadot break; 787b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 788b5be541fSEmmanuel Vadot sc->aw_host.mode = value; 789b5be541fSEmmanuel Vadot break; 790b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 791b5be541fSEmmanuel Vadot sc->aw_host.ocr = value; 792b5be541fSEmmanuel Vadot break; 793b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 794b5be541fSEmmanuel Vadot sc->aw_host.ios.power_mode = value; 795b5be541fSEmmanuel Vadot break; 796b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 797b5be541fSEmmanuel Vadot sc->aw_host.ios.vdd = value; 798b5be541fSEmmanuel Vadot break; 799*ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 800*ce0618beSEmmanuel Vadot sc->aw_host.ios.timing = value; 801*ce0618beSEmmanuel Vadot break; 802b5be541fSEmmanuel Vadot /* These are read-only */ 803b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 804b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 805b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 806b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 807b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 808b5be541fSEmmanuel Vadot return (EINVAL); 809b5be541fSEmmanuel Vadot } 810b5be541fSEmmanuel Vadot 811b5be541fSEmmanuel Vadot return (0); 812b5be541fSEmmanuel Vadot } 813b5be541fSEmmanuel Vadot 814b5be541fSEmmanuel Vadot static int 815b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon) 816b5be541fSEmmanuel Vadot { 817*ce0618beSEmmanuel Vadot uint32_t reg; 818b5be541fSEmmanuel Vadot int retry; 819b5be541fSEmmanuel Vadot 820*ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 821*ce0618beSEmmanuel Vadot reg &= ~(AW_MMC_CKCR_CCLK_ENB | AW_MMC_CKCR_CCLK_CTRL | 822*ce0618beSEmmanuel Vadot AW_MMC_CKCR_CCLK_MASK_DATA0); 823b5be541fSEmmanuel Vadot 824b5be541fSEmmanuel Vadot if (clkon) 825*ce0618beSEmmanuel Vadot reg |= AW_MMC_CKCR_CCLK_ENB; 826*ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) 827*ce0618beSEmmanuel Vadot reg |= AW_MMC_CKCR_CCLK_MASK_DATA0; 828b5be541fSEmmanuel Vadot 829*ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 830b5be541fSEmmanuel Vadot 831*ce0618beSEmmanuel Vadot reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK | 832b5be541fSEmmanuel Vadot AW_MMC_CMDR_WAIT_PRE_OVER; 833*ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg); 834b5be541fSEmmanuel Vadot retry = 0xfffff; 835*ce0618beSEmmanuel Vadot 836*ce0618beSEmmanuel Vadot while (reg & AW_MMC_CMDR_LOAD && --retry > 0) { 837*ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CMDR); 838b5be541fSEmmanuel Vadot DELAY(10); 839b5be541fSEmmanuel Vadot } 840b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 841b5be541fSEmmanuel Vadot 842*ce0618beSEmmanuel Vadot if (reg & AW_MMC_CMDR_LOAD) { 843*ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "timeout updating clock\n"); 844b5be541fSEmmanuel Vadot return (ETIMEDOUT); 845b5be541fSEmmanuel Vadot } 846b5be541fSEmmanuel Vadot 847*ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) { 848*ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 849*ce0618beSEmmanuel Vadot reg &= ~AW_MMC_CKCR_CCLK_MASK_DATA0; 850*ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 851*ce0618beSEmmanuel Vadot } 852*ce0618beSEmmanuel Vadot 853*ce0618beSEmmanuel Vadot return (0); 854*ce0618beSEmmanuel Vadot } 855*ce0618beSEmmanuel Vadot 856*ce0618beSEmmanuel Vadot static void 857*ce0618beSEmmanuel Vadot aw_mmc_set_power(struct aw_mmc_softc *sc, int32_t vdd) 858*ce0618beSEmmanuel Vadot { 859*ce0618beSEmmanuel Vadot int min_uvolt, max_uvolt; 860*ce0618beSEmmanuel Vadot 861*ce0618beSEmmanuel Vadot sc->aw_vdd = vdd; 862*ce0618beSEmmanuel Vadot 863*ce0618beSEmmanuel Vadot if (sc->aw_reg_vmmc == NULL && sc->aw_reg_vqmmc == NULL) 864*ce0618beSEmmanuel Vadot return; 865*ce0618beSEmmanuel Vadot 866*ce0618beSEmmanuel Vadot switch (1 << vdd) { 867*ce0618beSEmmanuel Vadot case MMC_OCR_LOW_VOLTAGE: 868*ce0618beSEmmanuel Vadot min_uvolt = max_uvolt = 1800000; 869*ce0618beSEmmanuel Vadot break; 870*ce0618beSEmmanuel Vadot case MMC_OCR_320_330: 871*ce0618beSEmmanuel Vadot min_uvolt = 3200000; 872*ce0618beSEmmanuel Vadot max_uvolt = 3300000; 873*ce0618beSEmmanuel Vadot break; 874*ce0618beSEmmanuel Vadot case MMC_OCR_330_340: 875*ce0618beSEmmanuel Vadot min_uvolt = 3300000; 876*ce0618beSEmmanuel Vadot max_uvolt = 3400000; 877*ce0618beSEmmanuel Vadot break; 878*ce0618beSEmmanuel Vadot } 879*ce0618beSEmmanuel Vadot 880*ce0618beSEmmanuel Vadot if (sc->aw_reg_vmmc) 881*ce0618beSEmmanuel Vadot if (regulator_set_voltage(sc->aw_reg_vmmc, 882*ce0618beSEmmanuel Vadot min_uvolt, max_uvolt) != 0) 883*ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, 884*ce0618beSEmmanuel Vadot "Cannot set vmmc to %d<->%d\n", 885*ce0618beSEmmanuel Vadot min_uvolt, 886*ce0618beSEmmanuel Vadot max_uvolt); 887*ce0618beSEmmanuel Vadot if (sc->aw_reg_vqmmc) 888*ce0618beSEmmanuel Vadot if (regulator_set_voltage(sc->aw_reg_vqmmc, 889*ce0618beSEmmanuel Vadot min_uvolt, max_uvolt) != 0) 890*ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, 891*ce0618beSEmmanuel Vadot "Cannot set vqmmc to %d<->%d\n", 892*ce0618beSEmmanuel Vadot min_uvolt, 893*ce0618beSEmmanuel Vadot max_uvolt); 894*ce0618beSEmmanuel Vadot } 895*ce0618beSEmmanuel Vadot 896b5be541fSEmmanuel Vadot static int 897b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child) 898b5be541fSEmmanuel Vadot { 899b5be541fSEmmanuel Vadot int error; 900b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 901b5be541fSEmmanuel Vadot struct mmc_ios *ios; 902*ce0618beSEmmanuel Vadot unsigned int clock; 903*ce0618beSEmmanuel Vadot uint32_t reg, div = 1; 904b5be541fSEmmanuel Vadot 905b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 906b5be541fSEmmanuel Vadot 907b5be541fSEmmanuel Vadot ios = &sc->aw_host.ios; 908b5be541fSEmmanuel Vadot 909b5be541fSEmmanuel Vadot /* Set the bus width. */ 910b5be541fSEmmanuel Vadot switch (ios->bus_width) { 911b5be541fSEmmanuel Vadot case bus_width_1: 912b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1); 913b5be541fSEmmanuel Vadot break; 914b5be541fSEmmanuel Vadot case bus_width_4: 915b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4); 916b5be541fSEmmanuel Vadot break; 917b5be541fSEmmanuel Vadot case bus_width_8: 918b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8); 919b5be541fSEmmanuel Vadot break; 920b5be541fSEmmanuel Vadot } 921b5be541fSEmmanuel Vadot 922*ce0618beSEmmanuel Vadot /* Set the voltage */ 923*ce0618beSEmmanuel Vadot if (ios->power_mode == power_off) { 924*ce0618beSEmmanuel Vadot if (bootverbose) 925*ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "Powering down sd/mmc\n"); 926*ce0618beSEmmanuel Vadot if (sc->aw_reg_vmmc) 927*ce0618beSEmmanuel Vadot regulator_disable(sc->aw_reg_vmmc); 928*ce0618beSEmmanuel Vadot if (sc->aw_reg_vqmmc) 929*ce0618beSEmmanuel Vadot regulator_disable(sc->aw_reg_vqmmc); 930*ce0618beSEmmanuel Vadot } else if (sc->aw_vdd != ios->vdd) 931*ce0618beSEmmanuel Vadot aw_mmc_set_power(sc, ios->vdd); 932*ce0618beSEmmanuel Vadot 933*ce0618beSEmmanuel Vadot /* Enable ddr mode if needed */ 934*ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 935*ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_uhs_ddr50 || 936*ce0618beSEmmanuel Vadot ios->timing == bus_timing_mmc_ddr52) 937*ce0618beSEmmanuel Vadot reg |= AW_MMC_CTRL_DDR_MOD_SEL; 938*ce0618beSEmmanuel Vadot else 939*ce0618beSEmmanuel Vadot reg &= ~AW_MMC_CTRL_DDR_MOD_SEL; 940*ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 941*ce0618beSEmmanuel Vadot 942b5be541fSEmmanuel Vadot if (ios->clock) { 943*ce0618beSEmmanuel Vadot clock = ios->clock; 944b5be541fSEmmanuel Vadot 945b5be541fSEmmanuel Vadot /* Disable clock */ 946b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 0); 947b5be541fSEmmanuel Vadot if (error != 0) 948b5be541fSEmmanuel Vadot return (error); 949b5be541fSEmmanuel Vadot 950*ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_mmc_ddr52 && 951*ce0618beSEmmanuel Vadot (sc->aw_mmc_conf->new_timing || 952*ce0618beSEmmanuel Vadot ios->bus_width == bus_width_8)) { 953*ce0618beSEmmanuel Vadot div = 2; 954*ce0618beSEmmanuel Vadot clock <<= 1; 955*ce0618beSEmmanuel Vadot } 956*ce0618beSEmmanuel Vadot 957b5be541fSEmmanuel Vadot /* Reset the divider. */ 958*ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 959*ce0618beSEmmanuel Vadot reg &= ~AW_MMC_CKCR_CCLK_DIV; 960*ce0618beSEmmanuel Vadot reg |= div - 1; 961*ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 962*ce0618beSEmmanuel Vadot 963*ce0618beSEmmanuel Vadot /* New timing mode if needed */ 964*ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->new_timing) { 965*ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_NTSR); 966*ce0618beSEmmanuel Vadot reg |= AW_MMC_NTSR_MODE_SELECT; 967*ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg); 968*ce0618beSEmmanuel Vadot } 969b5be541fSEmmanuel Vadot 970b5be541fSEmmanuel Vadot /* Set the MMC clock. */ 971*ce0618beSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, clock, 972b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 973b5be541fSEmmanuel Vadot if (error != 0) { 974b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 975b5be541fSEmmanuel Vadot "failed to set frequency to %u Hz: %d\n", 976*ce0618beSEmmanuel Vadot clock, error); 977b5be541fSEmmanuel Vadot return (error); 978b5be541fSEmmanuel Vadot } 979b5be541fSEmmanuel Vadot 980*ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->can_calibrate) 981*ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN); 982*ce0618beSEmmanuel Vadot 983b5be541fSEmmanuel Vadot /* Enable clock. */ 984b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 1); 985b5be541fSEmmanuel Vadot if (error != 0) 986b5be541fSEmmanuel Vadot return (error); 987b5be541fSEmmanuel Vadot } 988b5be541fSEmmanuel Vadot 989b5be541fSEmmanuel Vadot 990b5be541fSEmmanuel Vadot return (0); 991b5be541fSEmmanuel Vadot } 992b5be541fSEmmanuel Vadot 993b5be541fSEmmanuel Vadot static int 994b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child) 995b5be541fSEmmanuel Vadot { 996b5be541fSEmmanuel Vadot 997b5be541fSEmmanuel Vadot return (0); 998b5be541fSEmmanuel Vadot } 999b5be541fSEmmanuel Vadot 1000b5be541fSEmmanuel Vadot static int 1001b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child) 1002b5be541fSEmmanuel Vadot { 1003b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1004b5be541fSEmmanuel Vadot int error; 1005b5be541fSEmmanuel Vadot 1006b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1007b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1008b5be541fSEmmanuel Vadot while (sc->aw_bus_busy) { 1009b5be541fSEmmanuel Vadot error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0); 1010b5be541fSEmmanuel Vadot if (error != 0) { 1011b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1012b5be541fSEmmanuel Vadot return (error); 1013b5be541fSEmmanuel Vadot } 1014b5be541fSEmmanuel Vadot } 1015b5be541fSEmmanuel Vadot sc->aw_bus_busy++; 1016b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1017b5be541fSEmmanuel Vadot 1018b5be541fSEmmanuel Vadot return (0); 1019b5be541fSEmmanuel Vadot } 1020b5be541fSEmmanuel Vadot 1021b5be541fSEmmanuel Vadot static int 1022b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child) 1023b5be541fSEmmanuel Vadot { 1024b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1025b5be541fSEmmanuel Vadot 1026b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1027b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1028b5be541fSEmmanuel Vadot sc->aw_bus_busy--; 1029b5be541fSEmmanuel Vadot wakeup(sc); 1030b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1031b5be541fSEmmanuel Vadot 1032b5be541fSEmmanuel Vadot return (0); 1033b5be541fSEmmanuel Vadot } 1034b5be541fSEmmanuel Vadot 1035b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = { 1036b5be541fSEmmanuel Vadot /* Device interface */ 1037b5be541fSEmmanuel Vadot DEVMETHOD(device_probe, aw_mmc_probe), 1038b5be541fSEmmanuel Vadot DEVMETHOD(device_attach, aw_mmc_attach), 1039b5be541fSEmmanuel Vadot DEVMETHOD(device_detach, aw_mmc_detach), 1040b5be541fSEmmanuel Vadot 1041b5be541fSEmmanuel Vadot /* Bus interface */ 1042b5be541fSEmmanuel Vadot DEVMETHOD(bus_read_ivar, aw_mmc_read_ivar), 1043b5be541fSEmmanuel Vadot DEVMETHOD(bus_write_ivar, aw_mmc_write_ivar), 1044b5be541fSEmmanuel Vadot 1045b5be541fSEmmanuel Vadot /* MMC bridge interface */ 1046b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_update_ios, aw_mmc_update_ios), 1047b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_request, aw_mmc_request), 1048b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_get_ro, aw_mmc_get_ro), 1049b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_acquire_host, aw_mmc_acquire_host), 1050b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_release_host, aw_mmc_release_host), 1051b5be541fSEmmanuel Vadot 1052b5be541fSEmmanuel Vadot DEVMETHOD_END 1053b5be541fSEmmanuel Vadot }; 1054b5be541fSEmmanuel Vadot 1055b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass; 1056b5be541fSEmmanuel Vadot 1057b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = { 1058b5be541fSEmmanuel Vadot "aw_mmc", 1059b5be541fSEmmanuel Vadot aw_mmc_methods, 1060b5be541fSEmmanuel Vadot sizeof(struct aw_mmc_softc), 1061b5be541fSEmmanuel Vadot }; 1062b5be541fSEmmanuel Vadot 1063b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL, 1064b5be541fSEmmanuel Vadot NULL); 1065b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc); 1066