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 58b5be541fSEmmanuel Vadot #define AW_MMC_MEMRES 0 59b5be541fSEmmanuel Vadot #define AW_MMC_IRQRES 1 60b5be541fSEmmanuel Vadot #define AW_MMC_RESSZ 2 61c39ea909SEmmanuel Vadot #define AW_MMC_DMA_SEGS (PAGE_SIZE / sizeof(struct aw_mmc_dma_desc)) 62c39ea909SEmmanuel Vadot #define AW_MMC_DMA_DESC_SIZE (sizeof(struct aw_mmc_dma_desc) * AW_MMC_DMA_SEGS) 63b5be541fSEmmanuel Vadot #define AW_MMC_DMA_FTRGLEVEL 0x20070008 64c39ea909SEmmanuel Vadot 65b5be541fSEmmanuel Vadot #define AW_MMC_RESET_RETRY 1000 66b5be541fSEmmanuel Vadot 67b5be541fSEmmanuel Vadot #define CARD_ID_FREQUENCY 400000 68b5be541fSEmmanuel Vadot 69ce0618beSEmmanuel Vadot struct aw_mmc_conf { 70ce0618beSEmmanuel Vadot uint32_t dma_xferlen; 71ce0618beSEmmanuel Vadot bool mask_data0; 72ce0618beSEmmanuel Vadot bool can_calibrate; 73ce0618beSEmmanuel Vadot bool new_timing; 74ce0618beSEmmanuel Vadot }; 75ce0618beSEmmanuel Vadot 76ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a10_mmc_conf = { 77ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 78ce0618beSEmmanuel Vadot }; 79ce0618beSEmmanuel Vadot 80ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a13_mmc_conf = { 81ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 82ce0618beSEmmanuel Vadot }; 83ce0618beSEmmanuel Vadot 84ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_mmc_conf = { 85ce0618beSEmmanuel Vadot .dma_xferlen = 0x10000, 86ce0618beSEmmanuel Vadot .mask_data0 = true, 87ce0618beSEmmanuel Vadot .can_calibrate = true, 88ce0618beSEmmanuel Vadot .new_timing = true, 89ce0618beSEmmanuel Vadot }; 90ce0618beSEmmanuel Vadot 91ce0618beSEmmanuel Vadot static const struct aw_mmc_conf a64_emmc_conf = { 92ce0618beSEmmanuel Vadot .dma_xferlen = 0x2000, 93ce0618beSEmmanuel Vadot .can_calibrate = true, 94ce0618beSEmmanuel Vadot }; 95ce0618beSEmmanuel Vadot 96b5be541fSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 97ce0618beSEmmanuel Vadot {"allwinner,sun4i-a10-mmc", (uintptr_t)&a10_mmc_conf}, 98ce0618beSEmmanuel Vadot {"allwinner,sun5i-a13-mmc", (uintptr_t)&a13_mmc_conf}, 99ce0618beSEmmanuel Vadot {"allwinner,sun7i-a20-mmc", (uintptr_t)&a13_mmc_conf}, 100ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-mmc", (uintptr_t)&a64_mmc_conf}, 101ce0618beSEmmanuel Vadot {"allwinner,sun50i-a64-emmc", (uintptr_t)&a64_emmc_conf}, 102b5be541fSEmmanuel Vadot {NULL, 0} 103b5be541fSEmmanuel Vadot }; 104b5be541fSEmmanuel Vadot 105b5be541fSEmmanuel Vadot struct aw_mmc_softc { 106b5be541fSEmmanuel Vadot device_t aw_dev; 107b5be541fSEmmanuel Vadot clk_t aw_clk_ahb; 108b5be541fSEmmanuel Vadot clk_t aw_clk_mmc; 109b5be541fSEmmanuel Vadot hwreset_t aw_rst_ahb; 110b5be541fSEmmanuel Vadot int aw_bus_busy; 111b5be541fSEmmanuel Vadot int aw_resid; 112b5be541fSEmmanuel Vadot int aw_timeout; 113b5be541fSEmmanuel Vadot struct callout aw_timeoutc; 114b5be541fSEmmanuel Vadot struct mmc_host aw_host; 115b5be541fSEmmanuel Vadot struct mmc_request * aw_req; 116b5be541fSEmmanuel Vadot struct mtx aw_mtx; 117b5be541fSEmmanuel Vadot struct resource * aw_res[AW_MMC_RESSZ]; 118ce0618beSEmmanuel Vadot struct aw_mmc_conf * aw_mmc_conf; 119b5be541fSEmmanuel Vadot uint32_t aw_intr; 120b5be541fSEmmanuel Vadot uint32_t aw_intr_wait; 121b5be541fSEmmanuel Vadot void * aw_intrhand; 122ce0618beSEmmanuel Vadot regulator_t aw_reg_vmmc; 123ce0618beSEmmanuel Vadot regulator_t aw_reg_vqmmc; 1240f7a6420SEmmanuel Vadot unsigned int aw_clock; 125b5be541fSEmmanuel Vadot 126b5be541fSEmmanuel Vadot /* Fields required for DMA access. */ 127b5be541fSEmmanuel Vadot bus_addr_t aw_dma_desc_phys; 128b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_map; 129b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_tag; 130b5be541fSEmmanuel Vadot void * aw_dma_desc; 131b5be541fSEmmanuel Vadot bus_dmamap_t aw_dma_buf_map; 132b5be541fSEmmanuel Vadot bus_dma_tag_t aw_dma_buf_tag; 133b5be541fSEmmanuel Vadot int aw_dma_map_err; 134b5be541fSEmmanuel Vadot }; 135b5be541fSEmmanuel Vadot 136b5be541fSEmmanuel Vadot static struct resource_spec aw_mmc_res_spec[] = { 137b5be541fSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE }, 138b5be541fSEmmanuel Vadot { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 139b5be541fSEmmanuel Vadot { -1, 0, 0 } 140b5be541fSEmmanuel Vadot }; 141b5be541fSEmmanuel Vadot 142b5be541fSEmmanuel Vadot static int aw_mmc_probe(device_t); 143b5be541fSEmmanuel Vadot static int aw_mmc_attach(device_t); 144b5be541fSEmmanuel Vadot static int aw_mmc_detach(device_t); 145b5be541fSEmmanuel Vadot static int aw_mmc_setup_dma(struct aw_mmc_softc *); 146b5be541fSEmmanuel Vadot static int aw_mmc_reset(struct aw_mmc_softc *); 14735a18619SEmmanuel Vadot static int aw_mmc_init(struct aw_mmc_softc *); 148b5be541fSEmmanuel Vadot static void aw_mmc_intr(void *); 149b5be541fSEmmanuel Vadot static int aw_mmc_update_clock(struct aw_mmc_softc *, uint32_t); 150b5be541fSEmmanuel Vadot 151b5be541fSEmmanuel Vadot static int aw_mmc_update_ios(device_t, device_t); 152b5be541fSEmmanuel Vadot static int aw_mmc_request(device_t, device_t, struct mmc_request *); 153b5be541fSEmmanuel Vadot static int aw_mmc_get_ro(device_t, device_t); 154b5be541fSEmmanuel Vadot static int aw_mmc_acquire_host(device_t, device_t); 155b5be541fSEmmanuel Vadot static int aw_mmc_release_host(device_t, device_t); 156b5be541fSEmmanuel Vadot 157b5be541fSEmmanuel Vadot #define AW_MMC_LOCK(_sc) mtx_lock(&(_sc)->aw_mtx) 158b5be541fSEmmanuel Vadot #define AW_MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->aw_mtx) 159b5be541fSEmmanuel Vadot #define AW_MMC_READ_4(_sc, _reg) \ 160b5be541fSEmmanuel Vadot bus_read_4((_sc)->aw_res[AW_MMC_MEMRES], _reg) 161b5be541fSEmmanuel Vadot #define AW_MMC_WRITE_4(_sc, _reg, _value) \ 162b5be541fSEmmanuel Vadot bus_write_4((_sc)->aw_res[AW_MMC_MEMRES], _reg, _value) 163b5be541fSEmmanuel Vadot 164b5be541fSEmmanuel Vadot static int 165b5be541fSEmmanuel Vadot aw_mmc_probe(device_t dev) 166b5be541fSEmmanuel Vadot { 167b5be541fSEmmanuel Vadot 168b5be541fSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 169b5be541fSEmmanuel Vadot return (ENXIO); 170b5be541fSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 171b5be541fSEmmanuel Vadot return (ENXIO); 172b5be541fSEmmanuel Vadot 173b5be541fSEmmanuel Vadot device_set_desc(dev, "Allwinner Integrated MMC/SD controller"); 174b5be541fSEmmanuel Vadot 175b5be541fSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 176b5be541fSEmmanuel Vadot } 177b5be541fSEmmanuel Vadot 178b5be541fSEmmanuel Vadot static int 179b5be541fSEmmanuel Vadot aw_mmc_attach(device_t dev) 180b5be541fSEmmanuel Vadot { 181b5be541fSEmmanuel Vadot device_t child; 182b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 183b5be541fSEmmanuel Vadot struct sysctl_ctx_list *ctx; 184b5be541fSEmmanuel Vadot struct sysctl_oid_list *tree; 185*bbf8c8faSEmmanuel Vadot uint32_t bus_width, max_freq; 186b5be541fSEmmanuel Vadot phandle_t node; 187b5be541fSEmmanuel Vadot int error; 188b5be541fSEmmanuel Vadot 189b5be541fSEmmanuel Vadot node = ofw_bus_get_node(dev); 190b5be541fSEmmanuel Vadot sc = device_get_softc(dev); 191b5be541fSEmmanuel Vadot sc->aw_dev = dev; 192ce0618beSEmmanuel Vadot 193ce0618beSEmmanuel Vadot sc->aw_mmc_conf = (struct aw_mmc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 194ce0618beSEmmanuel Vadot 195b5be541fSEmmanuel Vadot sc->aw_req = NULL; 196b5be541fSEmmanuel Vadot if (bus_alloc_resources(dev, aw_mmc_res_spec, sc->aw_res) != 0) { 197b5be541fSEmmanuel Vadot device_printf(dev, "cannot allocate device resources\n"); 198b5be541fSEmmanuel Vadot return (ENXIO); 199b5be541fSEmmanuel Vadot } 200b5be541fSEmmanuel Vadot if (bus_setup_intr(dev, sc->aw_res[AW_MMC_IRQRES], 201b5be541fSEmmanuel Vadot INTR_TYPE_MISC | INTR_MPSAFE, NULL, aw_mmc_intr, sc, 202b5be541fSEmmanuel Vadot &sc->aw_intrhand)) { 203b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 204b5be541fSEmmanuel Vadot device_printf(dev, "cannot setup interrupt handler\n"); 205b5be541fSEmmanuel Vadot return (ENXIO); 206b5be541fSEmmanuel Vadot } 207b5be541fSEmmanuel Vadot mtx_init(&sc->aw_mtx, device_get_nameunit(sc->aw_dev), "aw_mmc", 208b5be541fSEmmanuel Vadot MTX_DEF); 209b5be541fSEmmanuel Vadot callout_init_mtx(&sc->aw_timeoutc, &sc->aw_mtx, 0); 210b5be541fSEmmanuel Vadot 211b5be541fSEmmanuel Vadot /* De-assert reset */ 212b5be541fSEmmanuel Vadot if (hwreset_get_by_ofw_name(dev, 0, "ahb", &sc->aw_rst_ahb) == 0) { 213b5be541fSEmmanuel Vadot error = hwreset_deassert(sc->aw_rst_ahb); 214b5be541fSEmmanuel Vadot if (error != 0) { 215b5be541fSEmmanuel Vadot device_printf(dev, "cannot de-assert reset\n"); 216b5be541fSEmmanuel Vadot goto fail; 217b5be541fSEmmanuel Vadot } 218b5be541fSEmmanuel Vadot } 219b5be541fSEmmanuel Vadot 220b5be541fSEmmanuel Vadot /* Activate the module clock. */ 221b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->aw_clk_ahb); 222b5be541fSEmmanuel Vadot if (error != 0) { 223b5be541fSEmmanuel Vadot device_printf(dev, "cannot get ahb clock\n"); 224b5be541fSEmmanuel Vadot goto fail; 225b5be541fSEmmanuel Vadot } 226b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_ahb); 227b5be541fSEmmanuel Vadot if (error != 0) { 228b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable ahb clock\n"); 229b5be541fSEmmanuel Vadot goto fail; 230b5be541fSEmmanuel Vadot } 231b5be541fSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "mmc", &sc->aw_clk_mmc); 232b5be541fSEmmanuel Vadot if (error != 0) { 233b5be541fSEmmanuel Vadot device_printf(dev, "cannot get mmc clock\n"); 234b5be541fSEmmanuel Vadot goto fail; 235b5be541fSEmmanuel Vadot } 236b5be541fSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, CARD_ID_FREQUENCY, 237b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 238b5be541fSEmmanuel Vadot if (error != 0) { 239b5be541fSEmmanuel Vadot device_printf(dev, "cannot init mmc clock\n"); 240b5be541fSEmmanuel Vadot goto fail; 241b5be541fSEmmanuel Vadot } 242b5be541fSEmmanuel Vadot error = clk_enable(sc->aw_clk_mmc); 243b5be541fSEmmanuel Vadot if (error != 0) { 244b5be541fSEmmanuel Vadot device_printf(dev, "cannot enable mmc clock\n"); 245b5be541fSEmmanuel Vadot goto fail; 246b5be541fSEmmanuel Vadot } 247b5be541fSEmmanuel Vadot 248b5be541fSEmmanuel Vadot sc->aw_timeout = 10; 249b5be541fSEmmanuel Vadot ctx = device_get_sysctl_ctx(dev); 250b5be541fSEmmanuel Vadot tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 251b5be541fSEmmanuel Vadot SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "req_timeout", CTLFLAG_RW, 252b5be541fSEmmanuel Vadot &sc->aw_timeout, 0, "Request timeout in seconds"); 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 268ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vmmc-supply", 2693177f7cdSEmmanuel Vadot &sc->aw_reg_vmmc) == 0) { 2703177f7cdSEmmanuel Vadot if (bootverbose) 271ce0618beSEmmanuel Vadot device_printf(dev, "vmmc-supply regulator found\n"); 2723177f7cdSEmmanuel Vadot } 273ce0618beSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "vqmmc-supply", 2743177f7cdSEmmanuel Vadot &sc->aw_reg_vqmmc) == 0 && bootverbose) { 2753177f7cdSEmmanuel Vadot if (bootverbose) 276ce0618beSEmmanuel Vadot device_printf(dev, "vqmmc-supply regulator found\n"); 2773177f7cdSEmmanuel Vadot } 278ce0618beSEmmanuel Vadot 279b5be541fSEmmanuel Vadot sc->aw_host.f_min = 400000; 280*bbf8c8faSEmmanuel Vadot 281*bbf8c8faSEmmanuel Vadot if (OF_getencprop(node, "max-frequency", &max_freq, 282*bbf8c8faSEmmanuel Vadot sizeof(uint32_t)) <= 0) 283*bbf8c8faSEmmanuel Vadot max_freq = 52000000; 284*bbf8c8faSEmmanuel Vadot sc->aw_host.f_max = max_freq; 285*bbf8c8faSEmmanuel Vadot 286b5be541fSEmmanuel Vadot sc->aw_host.host_ocr = MMC_OCR_320_330 | MMC_OCR_330_340; 287ce0618beSEmmanuel Vadot sc->aw_host.caps = MMC_CAP_HSPEED | MMC_CAP_UHS_SDR12 | 288ce0618beSEmmanuel Vadot MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50 | 289ce0618beSEmmanuel Vadot MMC_CAP_UHS_DDR50 | MMC_CAP_MMC_DDR52; 290ce0618beSEmmanuel Vadot 291dfb8c122SEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_SIGNALING_330 | MMC_CAP_SIGNALING_180; 292ce0618beSEmmanuel Vadot 293b5be541fSEmmanuel Vadot if (bus_width >= 4) 294b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_4_BIT_DATA; 295b5be541fSEmmanuel Vadot if (bus_width >= 8) 296b5be541fSEmmanuel Vadot sc->aw_host.caps |= MMC_CAP_8_BIT_DATA; 297b5be541fSEmmanuel Vadot 298b5be541fSEmmanuel Vadot child = device_add_child(dev, "mmc", -1); 299b5be541fSEmmanuel Vadot if (child == NULL) { 300b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC bus failed!\n"); 301b5be541fSEmmanuel Vadot goto fail; 302b5be541fSEmmanuel Vadot } 303b5be541fSEmmanuel Vadot if (device_probe_and_attach(child) != 0) { 304b5be541fSEmmanuel Vadot device_printf(dev, "attaching MMC child failed!\n"); 305b5be541fSEmmanuel Vadot device_delete_child(dev, child); 306b5be541fSEmmanuel Vadot goto fail; 307b5be541fSEmmanuel Vadot } 308b5be541fSEmmanuel Vadot 309b5be541fSEmmanuel Vadot return (0); 310b5be541fSEmmanuel Vadot 311b5be541fSEmmanuel Vadot fail: 312b5be541fSEmmanuel Vadot callout_drain(&sc->aw_timeoutc); 313b5be541fSEmmanuel Vadot mtx_destroy(&sc->aw_mtx); 314b5be541fSEmmanuel Vadot bus_teardown_intr(dev, sc->aw_res[AW_MMC_IRQRES], sc->aw_intrhand); 315b5be541fSEmmanuel Vadot bus_release_resources(dev, aw_mmc_res_spec, sc->aw_res); 316b5be541fSEmmanuel Vadot 317b5be541fSEmmanuel Vadot return (ENXIO); 318b5be541fSEmmanuel Vadot } 319b5be541fSEmmanuel Vadot 320b5be541fSEmmanuel Vadot static int 321b5be541fSEmmanuel Vadot aw_mmc_detach(device_t dev) 322b5be541fSEmmanuel Vadot { 323b5be541fSEmmanuel Vadot 324b5be541fSEmmanuel Vadot return (EBUSY); 325b5be541fSEmmanuel Vadot } 326b5be541fSEmmanuel Vadot 327b5be541fSEmmanuel Vadot static void 328b5be541fSEmmanuel Vadot aw_dma_desc_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 329b5be541fSEmmanuel Vadot { 330b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 331b5be541fSEmmanuel Vadot 332b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 333b5be541fSEmmanuel Vadot if (err) { 334b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 335b5be541fSEmmanuel Vadot return; 336b5be541fSEmmanuel Vadot } 337b5be541fSEmmanuel Vadot sc->aw_dma_desc_phys = segs[0].ds_addr; 338b5be541fSEmmanuel Vadot } 339b5be541fSEmmanuel Vadot 340b5be541fSEmmanuel Vadot static int 341b5be541fSEmmanuel Vadot aw_mmc_setup_dma(struct aw_mmc_softc *sc) 342b5be541fSEmmanuel Vadot { 343c39ea909SEmmanuel Vadot int error; 344b5be541fSEmmanuel Vadot 345b5be541fSEmmanuel Vadot /* Allocate the DMA descriptor memory. */ 346c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 347c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 348c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 349c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 350c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 351c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 352c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, 1, /* maxsize, nsegment */ 353c39ea909SEmmanuel Vadot AW_MMC_DMA_DESC_SIZE, /* maxsegsize */ 354c39ea909SEmmanuel Vadot 0, /* flags */ 355c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 356c39ea909SEmmanuel Vadot &sc->aw_dma_tag); 357b5be541fSEmmanuel Vadot if (error) 358b5be541fSEmmanuel Vadot return (error); 359b5be541fSEmmanuel Vadot 360c39ea909SEmmanuel Vadot error = bus_dmamem_alloc(sc->aw_dma_tag, &sc->aw_dma_desc, 361c39ea909SEmmanuel Vadot BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, 362c39ea909SEmmanuel Vadot &sc->aw_dma_map); 363c39ea909SEmmanuel Vadot if (error) 364c39ea909SEmmanuel Vadot return (error); 365c39ea909SEmmanuel Vadot 366c39ea909SEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_tag, 367c39ea909SEmmanuel Vadot sc->aw_dma_map, 368c39ea909SEmmanuel Vadot sc->aw_dma_desc, AW_MMC_DMA_DESC_SIZE, 369c39ea909SEmmanuel Vadot aw_dma_desc_cb, sc, 0); 370b5be541fSEmmanuel Vadot if (error) 371b5be541fSEmmanuel Vadot return (error); 372b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 373b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 374b5be541fSEmmanuel Vadot 375b5be541fSEmmanuel Vadot /* Create the DMA map for data transfers. */ 376c39ea909SEmmanuel Vadot error = bus_dma_tag_create( 377c39ea909SEmmanuel Vadot bus_get_dma_tag(sc->aw_dev), /* parent */ 378c39ea909SEmmanuel Vadot AW_MMC_DMA_ALIGN, 0, /* align, boundary */ 379c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 380c39ea909SEmmanuel Vadot BUS_SPACE_MAXADDR, /* highaddr */ 381c39ea909SEmmanuel Vadot NULL, NULL, /* filter, filterarg*/ 382c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen * 383c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS, AW_MMC_DMA_SEGS, /* maxsize, nsegments */ 384c39ea909SEmmanuel Vadot sc->aw_mmc_conf->dma_xferlen, /* maxsegsize */ 385c39ea909SEmmanuel Vadot BUS_DMA_ALLOCNOW, /* flags */ 386c39ea909SEmmanuel Vadot NULL, NULL, /* lock, lockarg*/ 387b5be541fSEmmanuel Vadot &sc->aw_dma_buf_tag); 388b5be541fSEmmanuel Vadot if (error) 389b5be541fSEmmanuel Vadot return (error); 390b5be541fSEmmanuel Vadot error = bus_dmamap_create(sc->aw_dma_buf_tag, 0, 391b5be541fSEmmanuel Vadot &sc->aw_dma_buf_map); 392b5be541fSEmmanuel Vadot if (error) 393b5be541fSEmmanuel Vadot return (error); 394b5be541fSEmmanuel Vadot 395b5be541fSEmmanuel Vadot return (0); 396b5be541fSEmmanuel Vadot } 397b5be541fSEmmanuel Vadot 398b5be541fSEmmanuel Vadot static void 399b5be541fSEmmanuel Vadot aw_dma_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int err) 400b5be541fSEmmanuel Vadot { 401b5be541fSEmmanuel Vadot int i; 402b5be541fSEmmanuel Vadot struct aw_mmc_dma_desc *dma_desc; 403b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 404b5be541fSEmmanuel Vadot 405b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 406b5be541fSEmmanuel Vadot sc->aw_dma_map_err = err; 407b5be541fSEmmanuel Vadot 408b5be541fSEmmanuel Vadot if (err) 409b5be541fSEmmanuel Vadot return; 410b5be541fSEmmanuel Vadot 411b5be541fSEmmanuel Vadot dma_desc = sc->aw_dma_desc; 412b5be541fSEmmanuel Vadot for (i = 0; i < nsegs; i++) { 413c39ea909SEmmanuel Vadot if (segs[i].ds_len == sc->aw_mmc_conf->dma_xferlen) 414c39ea909SEmmanuel Vadot dma_desc[i].buf_size = 0; /* Size of 0 indicate max len */ 415c39ea909SEmmanuel Vadot else 416b5be541fSEmmanuel Vadot dma_desc[i].buf_size = segs[i].ds_len; 417b5be541fSEmmanuel Vadot dma_desc[i].buf_addr = segs[i].ds_addr; 418b5be541fSEmmanuel Vadot dma_desc[i].config = AW_MMC_DMA_CONFIG_CH | 419c39ea909SEmmanuel Vadot AW_MMC_DMA_CONFIG_OWN | AW_MMC_DMA_CONFIG_DIC; 420c39ea909SEmmanuel Vadot 421b5be541fSEmmanuel Vadot dma_desc[i].next = sc->aw_dma_desc_phys + 422b5be541fSEmmanuel Vadot ((i + 1) * sizeof(struct aw_mmc_dma_desc)); 423c39ea909SEmmanuel Vadot } 424c39ea909SEmmanuel Vadot 425c39ea909SEmmanuel Vadot dma_desc[0].config |= AW_MMC_DMA_CONFIG_FD; 426c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config |= AW_MMC_DMA_CONFIG_LD | 427b5be541fSEmmanuel Vadot AW_MMC_DMA_CONFIG_ER; 428c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].config &= ~AW_MMC_DMA_CONFIG_DIC; 429c39ea909SEmmanuel Vadot dma_desc[nsegs - 1].next = 0; 430b5be541fSEmmanuel Vadot } 431b5be541fSEmmanuel Vadot 432b5be541fSEmmanuel Vadot static int 433b5be541fSEmmanuel Vadot aw_mmc_prepare_dma(struct aw_mmc_softc *sc) 434b5be541fSEmmanuel Vadot { 435b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 436b5be541fSEmmanuel Vadot int error; 437b5be541fSEmmanuel Vadot struct mmc_command *cmd; 438b5be541fSEmmanuel Vadot uint32_t val; 439b5be541fSEmmanuel Vadot 440b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 441ce0618beSEmmanuel Vadot if (cmd->data->len > (sc->aw_mmc_conf->dma_xferlen * AW_MMC_DMA_SEGS)) 442b5be541fSEmmanuel Vadot return (EFBIG); 443b5be541fSEmmanuel Vadot error = bus_dmamap_load(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 444b5be541fSEmmanuel Vadot cmd->data->data, cmd->data->len, aw_dma_cb, sc, 0); 445b5be541fSEmmanuel Vadot if (error) 446b5be541fSEmmanuel Vadot return (error); 447b5be541fSEmmanuel Vadot if (sc->aw_dma_map_err) 448b5be541fSEmmanuel Vadot return (sc->aw_dma_map_err); 449b5be541fSEmmanuel Vadot 450b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 451b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREWRITE; 452b5be541fSEmmanuel Vadot else 453b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_PREREAD; 454b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, sync_op); 455b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, BUS_DMASYNC_PREWRITE); 456b5be541fSEmmanuel Vadot 457b5be541fSEmmanuel Vadot /* Enable DMA */ 458b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 459b091392eSEmmanuel Vadot val &= ~AW_MMC_GCTL_FIFO_AC_MOD; 460b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_ENB; 461b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 462b5be541fSEmmanuel Vadot 463b5be541fSEmmanuel Vadot /* Reset DMA */ 464b091392eSEmmanuel Vadot val |= AW_MMC_GCTL_DMA_RST; 465b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val); 466b5be541fSEmmanuel Vadot 467b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, AW_MMC_DMAC_IDMAC_SOFT_RST); 468b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DMAC, 469b5be541fSEmmanuel Vadot AW_MMC_DMAC_IDMAC_IDMA_ON | AW_MMC_DMAC_IDMAC_FIX_BURST); 470b5be541fSEmmanuel Vadot 471b5be541fSEmmanuel Vadot /* Enable RX or TX DMA interrupt */ 472a37d59c1SEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_IDIE); 473b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 474b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_TX_INT; 475b5be541fSEmmanuel Vadot else 476b5be541fSEmmanuel Vadot val |= AW_MMC_IDST_RX_INT; 477b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDIE, val); 478b5be541fSEmmanuel Vadot 479b5be541fSEmmanuel Vadot /* Set DMA descritptor list address */ 480b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DLBA, sc->aw_dma_desc_phys); 481b5be541fSEmmanuel Vadot 482b5be541fSEmmanuel Vadot /* FIFO trigger level */ 483b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FWLR, AW_MMC_DMA_FTRGLEVEL); 484b5be541fSEmmanuel Vadot 485b5be541fSEmmanuel Vadot return (0); 486b5be541fSEmmanuel Vadot } 487b5be541fSEmmanuel Vadot 488b5be541fSEmmanuel Vadot static int 489b5be541fSEmmanuel Vadot aw_mmc_reset(struct aw_mmc_softc *sc) 490b5be541fSEmmanuel Vadot { 491b091392eSEmmanuel Vadot uint32_t reg; 492b5be541fSEmmanuel Vadot int timeout; 493b5be541fSEmmanuel Vadot 494b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 495b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_RESET; 496b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 497c39ea909SEmmanuel Vadot timeout = AW_MMC_RESET_RETRY; 498b5be541fSEmmanuel Vadot while (--timeout > 0) { 499b091392eSEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & AW_MMC_GCTL_RESET) == 0) 500b5be541fSEmmanuel Vadot break; 501b5be541fSEmmanuel Vadot DELAY(100); 502b5be541fSEmmanuel Vadot } 503b5be541fSEmmanuel Vadot if (timeout == 0) 504b5be541fSEmmanuel Vadot return (ETIMEDOUT); 505b5be541fSEmmanuel Vadot 50635a18619SEmmanuel Vadot return (0); 50735a18619SEmmanuel Vadot } 50835a18619SEmmanuel Vadot 50935a18619SEmmanuel Vadot static int 51035a18619SEmmanuel Vadot aw_mmc_init(struct aw_mmc_softc *sc) 51135a18619SEmmanuel Vadot { 512b091392eSEmmanuel Vadot uint32_t reg; 51335a18619SEmmanuel Vadot int ret; 51435a18619SEmmanuel Vadot 51535a18619SEmmanuel Vadot ret = aw_mmc_reset(sc); 51635a18619SEmmanuel Vadot if (ret != 0) 51735a18619SEmmanuel Vadot return (ret); 51835a18619SEmmanuel Vadot 519b5be541fSEmmanuel Vadot /* Set the timeout. */ 520b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_TMOR, 521b5be541fSEmmanuel Vadot AW_MMC_TMOR_DTO_LMT_SHIFT(AW_MMC_TMOR_DTO_LMT_MASK) | 522b5be541fSEmmanuel Vadot AW_MMC_TMOR_RTO_LMT_SHIFT(AW_MMC_TMOR_RTO_LMT_MASK)); 523b5be541fSEmmanuel Vadot 52435a18619SEmmanuel Vadot /* Unmask interrupts. */ 52535a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, 0); 52635a18619SEmmanuel Vadot 527b5be541fSEmmanuel Vadot /* Clear pending interrupts. */ 528b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 52935a18619SEmmanuel Vadot 53035a18619SEmmanuel Vadot /* Debug register, undocumented */ 53135a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_DBGC, 0xdeb); 53235a18619SEmmanuel Vadot 53335a18619SEmmanuel Vadot /* Function select register */ 53435a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_FUNS, 0xceaa0000); 53535a18619SEmmanuel Vadot 536b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, 0xffffffff); 53735a18619SEmmanuel Vadot 538b091392eSEmmanuel Vadot /* Enable interrupts and disable AHB access. */ 539b091392eSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 540b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_INT_ENB; 541b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_FIFO_AC_MOD; 542b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_WAIT_MEM_ACCESS; 543b091392eSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 544b5be541fSEmmanuel Vadot 545b5be541fSEmmanuel Vadot return (0); 546b5be541fSEmmanuel Vadot } 547b5be541fSEmmanuel Vadot 548b5be541fSEmmanuel Vadot static void 549b5be541fSEmmanuel Vadot aw_mmc_req_done(struct aw_mmc_softc *sc) 550b5be541fSEmmanuel Vadot { 551b5be541fSEmmanuel Vadot struct mmc_command *cmd; 552b5be541fSEmmanuel Vadot struct mmc_request *req; 553b5be541fSEmmanuel Vadot uint32_t val, mask; 554b5be541fSEmmanuel Vadot int retry; 555b5be541fSEmmanuel Vadot 556b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 557b5be541fSEmmanuel Vadot if (cmd->error != MMC_ERR_NONE) { 558b5be541fSEmmanuel Vadot /* Reset the FIFO and DMA engines. */ 559b091392eSEmmanuel Vadot mask = AW_MMC_GCTL_FIFO_RST | AW_MMC_GCTL_DMA_RST; 560b5be541fSEmmanuel Vadot val = AW_MMC_READ_4(sc, AW_MMC_GCTL); 561b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, val | mask); 562b5be541fSEmmanuel Vadot 563b5be541fSEmmanuel Vadot retry = AW_MMC_RESET_RETRY; 564b5be541fSEmmanuel Vadot while (--retry > 0) { 565c39ea909SEmmanuel Vadot if ((AW_MMC_READ_4(sc, AW_MMC_GCTL) & 566c39ea909SEmmanuel Vadot AW_MMC_GCTL_RESET) == 0) 567b5be541fSEmmanuel Vadot break; 568c39ea909SEmmanuel Vadot DELAY(100); 569b5be541fSEmmanuel Vadot } 570b5be541fSEmmanuel Vadot if (retry == 0) 571b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 572b5be541fSEmmanuel Vadot "timeout resetting DMA/FIFO\n"); 573b5be541fSEmmanuel Vadot aw_mmc_update_clock(sc, 1); 574b5be541fSEmmanuel Vadot } 575b5be541fSEmmanuel Vadot 576b5be541fSEmmanuel Vadot req = sc->aw_req; 577b5be541fSEmmanuel Vadot callout_stop(&sc->aw_timeoutc); 578b5be541fSEmmanuel Vadot sc->aw_req = NULL; 579b5be541fSEmmanuel Vadot sc->aw_intr = 0; 580b5be541fSEmmanuel Vadot sc->aw_resid = 0; 581b5be541fSEmmanuel Vadot sc->aw_dma_map_err = 0; 582b5be541fSEmmanuel Vadot sc->aw_intr_wait = 0; 583b5be541fSEmmanuel Vadot req->done(req); 584b5be541fSEmmanuel Vadot } 585b5be541fSEmmanuel Vadot 586b5be541fSEmmanuel Vadot static void 587b5be541fSEmmanuel Vadot aw_mmc_req_ok(struct aw_mmc_softc *sc) 588b5be541fSEmmanuel Vadot { 589b5be541fSEmmanuel Vadot int timeout; 590b5be541fSEmmanuel Vadot struct mmc_command *cmd; 591b5be541fSEmmanuel Vadot uint32_t status; 592b5be541fSEmmanuel Vadot 593b5be541fSEmmanuel Vadot timeout = 1000; 594b5be541fSEmmanuel Vadot while (--timeout > 0) { 595b5be541fSEmmanuel Vadot status = AW_MMC_READ_4(sc, AW_MMC_STAR); 596b5be541fSEmmanuel Vadot if ((status & AW_MMC_STAR_CARD_BUSY) == 0) 597b5be541fSEmmanuel Vadot break; 598b5be541fSEmmanuel Vadot DELAY(1000); 599b5be541fSEmmanuel Vadot } 600b5be541fSEmmanuel Vadot cmd = sc->aw_req->cmd; 601b5be541fSEmmanuel Vadot if (timeout == 0) { 602b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 603b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 604b5be541fSEmmanuel Vadot return; 605b5be541fSEmmanuel Vadot } 606b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) { 607b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) { 608b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP3); 609b5be541fSEmmanuel Vadot cmd->resp[1] = AW_MMC_READ_4(sc, AW_MMC_RESP2); 610b5be541fSEmmanuel Vadot cmd->resp[2] = AW_MMC_READ_4(sc, AW_MMC_RESP1); 611b5be541fSEmmanuel Vadot cmd->resp[3] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 612b5be541fSEmmanuel Vadot } else 613b5be541fSEmmanuel Vadot cmd->resp[0] = AW_MMC_READ_4(sc, AW_MMC_RESP0); 614b5be541fSEmmanuel Vadot } 615b5be541fSEmmanuel Vadot /* All data has been transferred ? */ 616b5be541fSEmmanuel Vadot if (cmd->data != NULL && (sc->aw_resid << 2) < cmd->data->len) 617b5be541fSEmmanuel Vadot cmd->error = MMC_ERR_FAILED; 618b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 619b5be541fSEmmanuel Vadot } 620b5be541fSEmmanuel Vadot 621b5be541fSEmmanuel Vadot static void 622b5be541fSEmmanuel Vadot aw_mmc_timeout(void *arg) 623b5be541fSEmmanuel Vadot { 624b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 625b5be541fSEmmanuel Vadot 626b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 627b5be541fSEmmanuel Vadot if (sc->aw_req != NULL) { 628b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "controller timeout\n"); 629b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_TIMEOUT; 630b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 631b5be541fSEmmanuel Vadot } else 632b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 633b5be541fSEmmanuel Vadot "Spurious timeout - no active request\n"); 634b5be541fSEmmanuel Vadot } 635b5be541fSEmmanuel Vadot 636b5be541fSEmmanuel Vadot static void 637b5be541fSEmmanuel Vadot aw_mmc_intr(void *arg) 638b5be541fSEmmanuel Vadot { 639b5be541fSEmmanuel Vadot bus_dmasync_op_t sync_op; 640b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 641b5be541fSEmmanuel Vadot struct mmc_data *data; 642b5be541fSEmmanuel Vadot uint32_t idst, imask, rint; 643b5be541fSEmmanuel Vadot 644b5be541fSEmmanuel Vadot sc = (struct aw_mmc_softc *)arg; 645b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 646b5be541fSEmmanuel Vadot rint = AW_MMC_READ_4(sc, AW_MMC_RISR); 647b5be541fSEmmanuel Vadot idst = AW_MMC_READ_4(sc, AW_MMC_IDST); 648b5be541fSEmmanuel Vadot imask = AW_MMC_READ_4(sc, AW_MMC_IMKR); 649b5be541fSEmmanuel Vadot if (idst == 0 && imask == 0 && rint == 0) { 650b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 651b5be541fSEmmanuel Vadot return; 652b5be541fSEmmanuel Vadot } 653b5be541fSEmmanuel Vadot #ifdef DEBUG 654b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "idst: %#x, imask: %#x, rint: %#x\n", 655b5be541fSEmmanuel Vadot idst, imask, rint); 656b5be541fSEmmanuel Vadot #endif 657b5be541fSEmmanuel Vadot if (sc->aw_req == NULL) { 658b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 659b5be541fSEmmanuel Vadot "Spurious interrupt - no active request, rint: 0x%08X\n", 660b5be541fSEmmanuel Vadot rint); 661b5be541fSEmmanuel Vadot goto end; 662b5be541fSEmmanuel Vadot } 663b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_ERR_BIT) { 664ce0618beSEmmanuel Vadot if (bootverbose) 665b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error rint: 0x%08X\n", rint); 666b5be541fSEmmanuel Vadot if (rint & AW_MMC_INT_RESP_TIMEOUT) 667b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_TIMEOUT; 668b5be541fSEmmanuel Vadot else 669b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_FAILED; 670b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 671b5be541fSEmmanuel Vadot goto end; 672b5be541fSEmmanuel Vadot } 673b5be541fSEmmanuel Vadot if (idst & AW_MMC_IDST_ERROR) { 674b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "error idst: 0x%08x\n", idst); 675b5be541fSEmmanuel Vadot sc->aw_req->cmd->error = MMC_ERR_FAILED; 676b5be541fSEmmanuel Vadot aw_mmc_req_done(sc); 677b5be541fSEmmanuel Vadot goto end; 678b5be541fSEmmanuel Vadot } 679b5be541fSEmmanuel Vadot 680b5be541fSEmmanuel Vadot sc->aw_intr |= rint; 681b5be541fSEmmanuel Vadot data = sc->aw_req->cmd->data; 682b5be541fSEmmanuel Vadot if (data != NULL && (idst & AW_MMC_IDST_COMPLETE) != 0) { 683b5be541fSEmmanuel Vadot if (data->flags & MMC_DATA_WRITE) 684b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTWRITE; 685b5be541fSEmmanuel Vadot else 686b5be541fSEmmanuel Vadot sync_op = BUS_DMASYNC_POSTREAD; 687b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_buf_tag, sc->aw_dma_buf_map, 688b5be541fSEmmanuel Vadot sync_op); 689b5be541fSEmmanuel Vadot bus_dmamap_sync(sc->aw_dma_tag, sc->aw_dma_map, 690b5be541fSEmmanuel Vadot BUS_DMASYNC_POSTWRITE); 691b5be541fSEmmanuel Vadot bus_dmamap_unload(sc->aw_dma_buf_tag, sc->aw_dma_buf_map); 692b5be541fSEmmanuel Vadot sc->aw_resid = data->len >> 2; 693b5be541fSEmmanuel Vadot } 694b5be541fSEmmanuel Vadot if ((sc->aw_intr & sc->aw_intr_wait) == sc->aw_intr_wait) 695b5be541fSEmmanuel Vadot aw_mmc_req_ok(sc); 696b5be541fSEmmanuel Vadot 697b5be541fSEmmanuel Vadot end: 698b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IDST, idst); 699b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, rint); 700b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 701b5be541fSEmmanuel Vadot } 702b5be541fSEmmanuel Vadot 703b5be541fSEmmanuel Vadot static int 704b5be541fSEmmanuel Vadot aw_mmc_request(device_t bus, device_t child, struct mmc_request *req) 705b5be541fSEmmanuel Vadot { 706b5be541fSEmmanuel Vadot int blksz; 707b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 708b5be541fSEmmanuel Vadot struct mmc_command *cmd; 70935a18619SEmmanuel Vadot uint32_t cmdreg, imask; 710b5be541fSEmmanuel Vadot int err; 711b5be541fSEmmanuel Vadot 712b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 713c39ea909SEmmanuel Vadot 714b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 715b5be541fSEmmanuel Vadot if (sc->aw_req) { 716b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 717b5be541fSEmmanuel Vadot return (EBUSY); 718b5be541fSEmmanuel Vadot } 71935a18619SEmmanuel Vadot 720b5be541fSEmmanuel Vadot sc->aw_req = req; 721b5be541fSEmmanuel Vadot cmd = req->cmd; 722b5be541fSEmmanuel Vadot cmdreg = AW_MMC_CMDR_LOAD; 72335a18619SEmmanuel Vadot imask = AW_MMC_INT_ERR_BIT; 72435a18619SEmmanuel Vadot sc->aw_intr_wait = 0; 72535a18619SEmmanuel Vadot sc->aw_intr = 0; 72635a18619SEmmanuel Vadot sc->aw_resid = 0; 72735a18619SEmmanuel Vadot cmd->error = MMC_ERR_NONE; 72835a18619SEmmanuel Vadot 729b5be541fSEmmanuel Vadot if (cmd->opcode == MMC_GO_IDLE_STATE) 730b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_SEND_INIT_SEQ; 73135a18619SEmmanuel Vadot 732b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_PRESENT) 733b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_RESP_RCV; 734b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_136) 735b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_LONG_RESP; 736b5be541fSEmmanuel Vadot if (cmd->flags & MMC_RSP_CRC) 737b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_CHK_RESP_CRC; 738b5be541fSEmmanuel Vadot 73935a18619SEmmanuel Vadot if (cmd->data) { 740b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DATA_TRANS | AW_MMC_CMDR_WAIT_PRE_OVER; 74135a18619SEmmanuel Vadot 742b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_MULTI) { 743b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_STOP_CMD_FLAG; 74435a18619SEmmanuel Vadot imask |= AW_MMC_INT_AUTO_STOP_DONE; 745b5be541fSEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_AUTO_STOP_DONE; 74635a18619SEmmanuel Vadot } else { 74735a18619SEmmanuel Vadot sc->aw_intr_wait |= AW_MMC_INT_DATA_OVER; 74835a18619SEmmanuel Vadot imask |= AW_MMC_INT_DATA_OVER; 749b5be541fSEmmanuel Vadot } 750b5be541fSEmmanuel Vadot if (cmd->data->flags & MMC_DATA_WRITE) 751b5be541fSEmmanuel Vadot cmdreg |= AW_MMC_CMDR_DIR_WRITE; 75235a18619SEmmanuel Vadot 753b5be541fSEmmanuel Vadot blksz = min(cmd->data->len, MMC_SECTOR_SIZE); 754b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BKSR, blksz); 755b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BYCR, cmd->data->len); 75635a18619SEmmanuel Vadot } else { 75735a18619SEmmanuel Vadot imask |= AW_MMC_INT_CMD_DONE; 75835a18619SEmmanuel Vadot } 759b5be541fSEmmanuel Vadot 76035a18619SEmmanuel Vadot /* Enable the interrupts we are interested in */ 76135a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_IMKR, imask); 76235a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 76335a18619SEmmanuel Vadot 76435a18619SEmmanuel Vadot /* Enable auto stop if needed */ 76535a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_A12A, 76635a18619SEmmanuel Vadot cmdreg & AW_MMC_CMDR_STOP_CMD_FLAG ? 0 : 0xffff); 76735a18619SEmmanuel Vadot 76835a18619SEmmanuel Vadot /* Write the command argument */ 76935a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CAGR, cmd->arg); 77035a18619SEmmanuel Vadot 77135a18619SEmmanuel Vadot /* 77235a18619SEmmanuel Vadot * If we don't have data start the request 77335a18619SEmmanuel Vadot * if we do prepare the dma request and start the request 77435a18619SEmmanuel Vadot */ 77535a18619SEmmanuel Vadot if (cmd->data == NULL) { 77635a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 77735a18619SEmmanuel Vadot } else { 778b5be541fSEmmanuel Vadot err = aw_mmc_prepare_dma(sc); 779b5be541fSEmmanuel Vadot if (err != 0) 780b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, "prepare_dma failed: %d\n", err); 78135a18619SEmmanuel Vadot 78235a18619SEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, cmdreg | cmd->opcode); 783b5be541fSEmmanuel Vadot } 784b5be541fSEmmanuel Vadot 785b5be541fSEmmanuel Vadot callout_reset(&sc->aw_timeoutc, sc->aw_timeout * hz, 786b5be541fSEmmanuel Vadot aw_mmc_timeout, sc); 787b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 788b5be541fSEmmanuel Vadot 789b5be541fSEmmanuel Vadot return (0); 790b5be541fSEmmanuel Vadot } 791b5be541fSEmmanuel Vadot 792b5be541fSEmmanuel Vadot static int 793b5be541fSEmmanuel Vadot aw_mmc_read_ivar(device_t bus, device_t child, int which, 794b5be541fSEmmanuel Vadot uintptr_t *result) 795b5be541fSEmmanuel Vadot { 796b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 797b5be541fSEmmanuel Vadot 798b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 799b5be541fSEmmanuel Vadot switch (which) { 800b5be541fSEmmanuel Vadot default: 801b5be541fSEmmanuel Vadot return (EINVAL); 802b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 803b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_mode; 804b5be541fSEmmanuel Vadot break; 805b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 806b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.bus_width; 807b5be541fSEmmanuel Vadot break; 808b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 809b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.chip_select; 810b5be541fSEmmanuel Vadot break; 811b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 812b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.clock; 813b5be541fSEmmanuel Vadot break; 814b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 815b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_min; 816b5be541fSEmmanuel Vadot break; 817b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 818b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.f_max; 819b5be541fSEmmanuel Vadot break; 820b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 821b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.host_ocr; 822b5be541fSEmmanuel Vadot break; 823b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 824b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.mode; 825b5be541fSEmmanuel Vadot break; 826b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 827b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ocr; 828b5be541fSEmmanuel Vadot break; 829b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 830b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.power_mode; 831b5be541fSEmmanuel Vadot break; 832b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 833b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.ios.vdd; 834b5be541fSEmmanuel Vadot break; 835dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 836dfb8c122SEmmanuel Vadot *(int *)result = sc->aw_host.ios.vccq; 837dfb8c122SEmmanuel Vadot break; 838b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 839b5be541fSEmmanuel Vadot *(int *)result = sc->aw_host.caps; 840b5be541fSEmmanuel Vadot break; 841ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 842ce0618beSEmmanuel Vadot *(int *)result = sc->aw_host.ios.timing; 843ce0618beSEmmanuel Vadot break; 844b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 845c39ea909SEmmanuel Vadot *(int *)result = (sc->aw_mmc_conf->dma_xferlen * 846c39ea909SEmmanuel Vadot AW_MMC_DMA_SEGS) / MMC_SECTOR_SIZE; 847b5be541fSEmmanuel Vadot break; 848b5be541fSEmmanuel Vadot } 849b5be541fSEmmanuel Vadot 850b5be541fSEmmanuel Vadot return (0); 851b5be541fSEmmanuel Vadot } 852b5be541fSEmmanuel Vadot 853b5be541fSEmmanuel Vadot static int 854b5be541fSEmmanuel Vadot aw_mmc_write_ivar(device_t bus, device_t child, int which, 855b5be541fSEmmanuel Vadot uintptr_t value) 856b5be541fSEmmanuel Vadot { 857b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 858b5be541fSEmmanuel Vadot 859b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 860b5be541fSEmmanuel Vadot switch (which) { 861b5be541fSEmmanuel Vadot default: 862b5be541fSEmmanuel Vadot return (EINVAL); 863b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_MODE: 864b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_mode = value; 865b5be541fSEmmanuel Vadot break; 866b5be541fSEmmanuel Vadot case MMCBR_IVAR_BUS_WIDTH: 867b5be541fSEmmanuel Vadot sc->aw_host.ios.bus_width = value; 868b5be541fSEmmanuel Vadot break; 869b5be541fSEmmanuel Vadot case MMCBR_IVAR_CHIP_SELECT: 870b5be541fSEmmanuel Vadot sc->aw_host.ios.chip_select = value; 871b5be541fSEmmanuel Vadot break; 872b5be541fSEmmanuel Vadot case MMCBR_IVAR_CLOCK: 873b5be541fSEmmanuel Vadot sc->aw_host.ios.clock = value; 874b5be541fSEmmanuel Vadot break; 875b5be541fSEmmanuel Vadot case MMCBR_IVAR_MODE: 876b5be541fSEmmanuel Vadot sc->aw_host.mode = value; 877b5be541fSEmmanuel Vadot break; 878b5be541fSEmmanuel Vadot case MMCBR_IVAR_OCR: 879b5be541fSEmmanuel Vadot sc->aw_host.ocr = value; 880b5be541fSEmmanuel Vadot break; 881b5be541fSEmmanuel Vadot case MMCBR_IVAR_POWER_MODE: 882b5be541fSEmmanuel Vadot sc->aw_host.ios.power_mode = value; 883b5be541fSEmmanuel Vadot break; 884b5be541fSEmmanuel Vadot case MMCBR_IVAR_VDD: 885b5be541fSEmmanuel Vadot sc->aw_host.ios.vdd = value; 886b5be541fSEmmanuel Vadot break; 887dfb8c122SEmmanuel Vadot case MMCBR_IVAR_VCCQ: 888dfb8c122SEmmanuel Vadot sc->aw_host.ios.vccq = value; 889dfb8c122SEmmanuel Vadot break; 890ce0618beSEmmanuel Vadot case MMCBR_IVAR_TIMING: 891ce0618beSEmmanuel Vadot sc->aw_host.ios.timing = value; 892ce0618beSEmmanuel Vadot break; 893b5be541fSEmmanuel Vadot /* These are read-only */ 894b5be541fSEmmanuel Vadot case MMCBR_IVAR_CAPS: 895b5be541fSEmmanuel Vadot case MMCBR_IVAR_HOST_OCR: 896b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MIN: 897b5be541fSEmmanuel Vadot case MMCBR_IVAR_F_MAX: 898b5be541fSEmmanuel Vadot case MMCBR_IVAR_MAX_DATA: 899b5be541fSEmmanuel Vadot return (EINVAL); 900b5be541fSEmmanuel Vadot } 901b5be541fSEmmanuel Vadot 902b5be541fSEmmanuel Vadot return (0); 903b5be541fSEmmanuel Vadot } 904b5be541fSEmmanuel Vadot 905b5be541fSEmmanuel Vadot static int 906b5be541fSEmmanuel Vadot aw_mmc_update_clock(struct aw_mmc_softc *sc, uint32_t clkon) 907b5be541fSEmmanuel Vadot { 908ce0618beSEmmanuel Vadot uint32_t reg; 909b5be541fSEmmanuel Vadot int retry; 910b5be541fSEmmanuel Vadot 911ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 912ffdb1aa8SEmmanuel Vadot reg &= ~(AW_MMC_CKCR_ENB | AW_MMC_CKCR_LOW_POWER | 913ffdb1aa8SEmmanuel Vadot AW_MMC_CKCR_MASK_DATA0); 914b5be541fSEmmanuel Vadot 915b5be541fSEmmanuel Vadot if (clkon) 916ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_ENB; 917ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) 918ffdb1aa8SEmmanuel Vadot reg |= AW_MMC_CKCR_MASK_DATA0; 919b5be541fSEmmanuel Vadot 920ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 921b5be541fSEmmanuel Vadot 922ce0618beSEmmanuel Vadot reg = AW_MMC_CMDR_LOAD | AW_MMC_CMDR_PRG_CLK | 923b5be541fSEmmanuel Vadot AW_MMC_CMDR_WAIT_PRE_OVER; 924ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CMDR, reg); 925b5be541fSEmmanuel Vadot retry = 0xfffff; 926ce0618beSEmmanuel Vadot 927ce0618beSEmmanuel Vadot while (reg & AW_MMC_CMDR_LOAD && --retry > 0) { 928ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CMDR); 929b5be541fSEmmanuel Vadot DELAY(10); 930b5be541fSEmmanuel Vadot } 931b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_RISR, 0xffffffff); 932b5be541fSEmmanuel Vadot 933ce0618beSEmmanuel Vadot if (reg & AW_MMC_CMDR_LOAD) { 934ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "timeout updating clock\n"); 935b5be541fSEmmanuel Vadot return (ETIMEDOUT); 936b5be541fSEmmanuel Vadot } 937b5be541fSEmmanuel Vadot 938ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->mask_data0) { 939ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 940ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_MASK_DATA0; 941ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 942ce0618beSEmmanuel Vadot } 943ce0618beSEmmanuel Vadot 944ce0618beSEmmanuel Vadot return (0); 945ce0618beSEmmanuel Vadot } 946ce0618beSEmmanuel Vadot 947623966e1SEmmanuel Vadot static int 948623966e1SEmmanuel Vadot aw_mmc_switch_vccq(device_t bus, device_t child) 949ce0618beSEmmanuel Vadot { 950623966e1SEmmanuel Vadot struct aw_mmc_softc *sc; 951623966e1SEmmanuel Vadot int uvolt, err; 952623966e1SEmmanuel Vadot 953623966e1SEmmanuel Vadot sc = device_get_softc(bus); 954ce0618beSEmmanuel Vadot 9553177f7cdSEmmanuel Vadot if (sc->aw_reg_vqmmc == NULL) 956623966e1SEmmanuel Vadot return EOPNOTSUPP; 957ce0618beSEmmanuel Vadot 958623966e1SEmmanuel Vadot switch (sc->aw_host.ios.vccq) { 959dfb8c122SEmmanuel Vadot case vccq_180: 960dfb8c122SEmmanuel Vadot uvolt = 1800000; 961ce0618beSEmmanuel Vadot break; 962dfb8c122SEmmanuel Vadot case vccq_330: 963dfb8c122SEmmanuel Vadot uvolt = 3300000; 964ce0618beSEmmanuel Vadot break; 965dfb8c122SEmmanuel Vadot default: 966623966e1SEmmanuel Vadot return EINVAL; 967ce0618beSEmmanuel Vadot } 968ce0618beSEmmanuel Vadot 969623966e1SEmmanuel Vadot err = regulator_set_voltage(sc->aw_reg_vqmmc, uvolt, uvolt); 970623966e1SEmmanuel Vadot if (err != 0) { 971ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, 972ce0618beSEmmanuel Vadot "Cannot set vqmmc to %d<->%d\n", 973dfb8c122SEmmanuel Vadot uvolt, 974dfb8c122SEmmanuel Vadot uvolt); 975623966e1SEmmanuel Vadot return (err); 976623966e1SEmmanuel Vadot } 977623966e1SEmmanuel Vadot 978623966e1SEmmanuel Vadot return (0); 979ce0618beSEmmanuel Vadot } 980ce0618beSEmmanuel Vadot 981b5be541fSEmmanuel Vadot static int 982b5be541fSEmmanuel Vadot aw_mmc_update_ios(device_t bus, device_t child) 983b5be541fSEmmanuel Vadot { 984b5be541fSEmmanuel Vadot int error; 985b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 986b5be541fSEmmanuel Vadot struct mmc_ios *ios; 987ce0618beSEmmanuel Vadot unsigned int clock; 988ce0618beSEmmanuel Vadot uint32_t reg, div = 1; 989b5be541fSEmmanuel Vadot 990b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 991b5be541fSEmmanuel Vadot 992b5be541fSEmmanuel Vadot ios = &sc->aw_host.ios; 993b5be541fSEmmanuel Vadot 994b5be541fSEmmanuel Vadot /* Set the bus width. */ 995b5be541fSEmmanuel Vadot switch (ios->bus_width) { 996b5be541fSEmmanuel Vadot case bus_width_1: 997b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR1); 998b5be541fSEmmanuel Vadot break; 999b5be541fSEmmanuel Vadot case bus_width_4: 1000b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR4); 1001b5be541fSEmmanuel Vadot break; 1002b5be541fSEmmanuel Vadot case bus_width_8: 1003b5be541fSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_BWDR, AW_MMC_BWDR8); 1004b5be541fSEmmanuel Vadot break; 1005b5be541fSEmmanuel Vadot } 1006b5be541fSEmmanuel Vadot 100735a18619SEmmanuel Vadot switch (ios->power_mode) { 100835a18619SEmmanuel Vadot case power_on: 100935a18619SEmmanuel Vadot break; 101035a18619SEmmanuel Vadot case power_off: 1011ce0618beSEmmanuel Vadot if (bootverbose) 1012ce0618beSEmmanuel Vadot device_printf(sc->aw_dev, "Powering down sd/mmc\n"); 1013dfb8c122SEmmanuel Vadot 1014dfb8c122SEmmanuel Vadot if (sc->aw_reg_vmmc) 1015dfb8c122SEmmanuel Vadot regulator_disable(sc->aw_reg_vmmc); 1016dfb8c122SEmmanuel Vadot if (sc->aw_reg_vqmmc) 1017dfb8c122SEmmanuel Vadot regulator_disable(sc->aw_reg_vqmmc); 1018dfb8c122SEmmanuel Vadot 101935a18619SEmmanuel Vadot aw_mmc_reset(sc); 102035a18619SEmmanuel Vadot break; 102135a18619SEmmanuel Vadot case power_up: 102235a18619SEmmanuel Vadot if (bootverbose) 102335a18619SEmmanuel Vadot device_printf(sc->aw_dev, "Powering up sd/mmc\n"); 1024dfb8c122SEmmanuel Vadot 1025dfb8c122SEmmanuel Vadot if (sc->aw_reg_vmmc) 1026dfb8c122SEmmanuel Vadot regulator_enable(sc->aw_reg_vmmc); 1027dfb8c122SEmmanuel Vadot if (sc->aw_reg_vqmmc) 1028dfb8c122SEmmanuel Vadot regulator_enable(sc->aw_reg_vqmmc); 102935a18619SEmmanuel Vadot aw_mmc_init(sc); 103035a18619SEmmanuel Vadot break; 103135a18619SEmmanuel Vadot }; 1032ce0618beSEmmanuel Vadot 1033ce0618beSEmmanuel Vadot /* Enable ddr mode if needed */ 1034ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_GCTL); 1035ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_uhs_ddr50 || 1036ce0618beSEmmanuel Vadot ios->timing == bus_timing_mmc_ddr52) 1037b091392eSEmmanuel Vadot reg |= AW_MMC_GCTL_DDR_MOD_SEL; 1038ce0618beSEmmanuel Vadot else 1039b091392eSEmmanuel Vadot reg &= ~AW_MMC_GCTL_DDR_MOD_SEL; 1040ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_GCTL, reg); 1041ce0618beSEmmanuel Vadot 10420f7a6420SEmmanuel Vadot if (ios->clock && ios->clock != sc->aw_clock) { 10430f7a6420SEmmanuel Vadot sc->aw_clock = clock = ios->clock; 1044b5be541fSEmmanuel Vadot 1045b5be541fSEmmanuel Vadot /* Disable clock */ 1046b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 0); 1047b5be541fSEmmanuel Vadot if (error != 0) 1048b5be541fSEmmanuel Vadot return (error); 1049b5be541fSEmmanuel Vadot 1050ce0618beSEmmanuel Vadot if (ios->timing == bus_timing_mmc_ddr52 && 1051ce0618beSEmmanuel Vadot (sc->aw_mmc_conf->new_timing || 1052ce0618beSEmmanuel Vadot ios->bus_width == bus_width_8)) { 1053ce0618beSEmmanuel Vadot div = 2; 1054ce0618beSEmmanuel Vadot clock <<= 1; 1055ce0618beSEmmanuel Vadot } 1056ce0618beSEmmanuel Vadot 1057b5be541fSEmmanuel Vadot /* Reset the divider. */ 1058ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_CKCR); 1059ffdb1aa8SEmmanuel Vadot reg &= ~AW_MMC_CKCR_DIV; 1060ce0618beSEmmanuel Vadot reg |= div - 1; 1061ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_CKCR, reg); 1062ce0618beSEmmanuel Vadot 1063ce0618beSEmmanuel Vadot /* New timing mode if needed */ 1064ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->new_timing) { 1065ce0618beSEmmanuel Vadot reg = AW_MMC_READ_4(sc, AW_MMC_NTSR); 1066ce0618beSEmmanuel Vadot reg |= AW_MMC_NTSR_MODE_SELECT; 1067ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_NTSR, reg); 1068ce0618beSEmmanuel Vadot } 1069b5be541fSEmmanuel Vadot 1070b5be541fSEmmanuel Vadot /* Set the MMC clock. */ 1071ce0618beSEmmanuel Vadot error = clk_set_freq(sc->aw_clk_mmc, clock, 1072b5be541fSEmmanuel Vadot CLK_SET_ROUND_DOWN); 1073b5be541fSEmmanuel Vadot if (error != 0) { 1074b5be541fSEmmanuel Vadot device_printf(sc->aw_dev, 1075b5be541fSEmmanuel Vadot "failed to set frequency to %u Hz: %d\n", 1076ce0618beSEmmanuel Vadot clock, error); 1077b5be541fSEmmanuel Vadot return (error); 1078b5be541fSEmmanuel Vadot } 1079b5be541fSEmmanuel Vadot 1080ce0618beSEmmanuel Vadot if (sc->aw_mmc_conf->can_calibrate) 1081ce0618beSEmmanuel Vadot AW_MMC_WRITE_4(sc, AW_MMC_SAMP_DL, AW_MMC_SAMP_DL_SW_EN); 1082ce0618beSEmmanuel Vadot 1083b5be541fSEmmanuel Vadot /* Enable clock. */ 1084b5be541fSEmmanuel Vadot error = aw_mmc_update_clock(sc, 1); 1085b5be541fSEmmanuel Vadot if (error != 0) 1086b5be541fSEmmanuel Vadot return (error); 1087b5be541fSEmmanuel Vadot } 1088b5be541fSEmmanuel Vadot 1089b5be541fSEmmanuel Vadot 1090b5be541fSEmmanuel Vadot return (0); 1091b5be541fSEmmanuel Vadot } 1092b5be541fSEmmanuel Vadot 1093b5be541fSEmmanuel Vadot static int 1094b5be541fSEmmanuel Vadot aw_mmc_get_ro(device_t bus, device_t child) 1095b5be541fSEmmanuel Vadot { 1096b5be541fSEmmanuel Vadot 1097b5be541fSEmmanuel Vadot return (0); 1098b5be541fSEmmanuel Vadot } 1099b5be541fSEmmanuel Vadot 1100b5be541fSEmmanuel Vadot static int 1101b5be541fSEmmanuel Vadot aw_mmc_acquire_host(device_t bus, device_t child) 1102b5be541fSEmmanuel Vadot { 1103b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1104b5be541fSEmmanuel Vadot int error; 1105b5be541fSEmmanuel Vadot 1106b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1107b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1108b5be541fSEmmanuel Vadot while (sc->aw_bus_busy) { 1109b5be541fSEmmanuel Vadot error = msleep(sc, &sc->aw_mtx, PCATCH, "mmchw", 0); 1110b5be541fSEmmanuel Vadot if (error != 0) { 1111b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1112b5be541fSEmmanuel Vadot return (error); 1113b5be541fSEmmanuel Vadot } 1114b5be541fSEmmanuel Vadot } 1115b5be541fSEmmanuel Vadot sc->aw_bus_busy++; 1116b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1117b5be541fSEmmanuel Vadot 1118b5be541fSEmmanuel Vadot return (0); 1119b5be541fSEmmanuel Vadot } 1120b5be541fSEmmanuel Vadot 1121b5be541fSEmmanuel Vadot static int 1122b5be541fSEmmanuel Vadot aw_mmc_release_host(device_t bus, device_t child) 1123b5be541fSEmmanuel Vadot { 1124b5be541fSEmmanuel Vadot struct aw_mmc_softc *sc; 1125b5be541fSEmmanuel Vadot 1126b5be541fSEmmanuel Vadot sc = device_get_softc(bus); 1127b5be541fSEmmanuel Vadot AW_MMC_LOCK(sc); 1128b5be541fSEmmanuel Vadot sc->aw_bus_busy--; 1129b5be541fSEmmanuel Vadot wakeup(sc); 1130b5be541fSEmmanuel Vadot AW_MMC_UNLOCK(sc); 1131b5be541fSEmmanuel Vadot 1132b5be541fSEmmanuel Vadot return (0); 1133b5be541fSEmmanuel Vadot } 1134b5be541fSEmmanuel Vadot 1135b5be541fSEmmanuel Vadot static device_method_t aw_mmc_methods[] = { 1136b5be541fSEmmanuel Vadot /* Device interface */ 1137b5be541fSEmmanuel Vadot DEVMETHOD(device_probe, aw_mmc_probe), 1138b5be541fSEmmanuel Vadot DEVMETHOD(device_attach, aw_mmc_attach), 1139b5be541fSEmmanuel Vadot DEVMETHOD(device_detach, aw_mmc_detach), 1140b5be541fSEmmanuel Vadot 1141b5be541fSEmmanuel Vadot /* Bus interface */ 1142b5be541fSEmmanuel Vadot DEVMETHOD(bus_read_ivar, aw_mmc_read_ivar), 1143b5be541fSEmmanuel Vadot DEVMETHOD(bus_write_ivar, aw_mmc_write_ivar), 1144b5be541fSEmmanuel Vadot 1145b5be541fSEmmanuel Vadot /* MMC bridge interface */ 1146b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_update_ios, aw_mmc_update_ios), 1147b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_request, aw_mmc_request), 1148b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_get_ro, aw_mmc_get_ro), 1149623966e1SEmmanuel Vadot DEVMETHOD(mmcbr_switch_vccq, aw_mmc_switch_vccq), 1150b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_acquire_host, aw_mmc_acquire_host), 1151b5be541fSEmmanuel Vadot DEVMETHOD(mmcbr_release_host, aw_mmc_release_host), 1152b5be541fSEmmanuel Vadot 1153b5be541fSEmmanuel Vadot DEVMETHOD_END 1154b5be541fSEmmanuel Vadot }; 1155b5be541fSEmmanuel Vadot 1156b5be541fSEmmanuel Vadot static devclass_t aw_mmc_devclass; 1157b5be541fSEmmanuel Vadot 1158b5be541fSEmmanuel Vadot static driver_t aw_mmc_driver = { 1159b5be541fSEmmanuel Vadot "aw_mmc", 1160b5be541fSEmmanuel Vadot aw_mmc_methods, 1161b5be541fSEmmanuel Vadot sizeof(struct aw_mmc_softc), 1162b5be541fSEmmanuel Vadot }; 1163b5be541fSEmmanuel Vadot 1164b5be541fSEmmanuel Vadot DRIVER_MODULE(aw_mmc, simplebus, aw_mmc_driver, aw_mmc_devclass, NULL, 1165b5be541fSEmmanuel Vadot NULL); 1166b5be541fSEmmanuel Vadot MMC_DECLARE_BRIDGE(aw_mmc); 1167