149ab747fSPaolo Bonzini /* 249ab747fSPaolo Bonzini * SD Association Host Standard Specification v2.0 controller emulation 349ab747fSPaolo Bonzini * 449ab747fSPaolo Bonzini * Copyright (c) 2011 Samsung Electronics Co., Ltd. 549ab747fSPaolo Bonzini * Mitsyanko Igor <i.mitsyanko@samsung.com> 649ab747fSPaolo Bonzini * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com> 749ab747fSPaolo Bonzini * 849ab747fSPaolo Bonzini * Based on MMC controller for Samsung S5PC1xx-based board emulation 949ab747fSPaolo Bonzini * by Alexey Merkulov and Vladimir Monakhov. 1049ab747fSPaolo Bonzini * 1149ab747fSPaolo Bonzini * This program is free software; you can redistribute it and/or modify it 1249ab747fSPaolo Bonzini * under the terms of the GNU General Public License as published by the 1349ab747fSPaolo Bonzini * Free Software Foundation; either version 2 of the License, or (at your 1449ab747fSPaolo Bonzini * option) any later version. 1549ab747fSPaolo Bonzini * 1649ab747fSPaolo Bonzini * This program is distributed in the hope that it will be useful, 1749ab747fSPaolo Bonzini * but WITHOUT ANY WARRANTY; without even the implied warranty of 1849ab747fSPaolo Bonzini * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1949ab747fSPaolo Bonzini * See the GNU General Public License for more details. 2049ab747fSPaolo Bonzini * 2149ab747fSPaolo Bonzini * You should have received a copy of the GNU General Public License along 2249ab747fSPaolo Bonzini * with this program; if not, see <http://www.gnu.org/licenses/>. 2349ab747fSPaolo Bonzini */ 2449ab747fSPaolo Bonzini 250430891cSPeter Maydell #include "qemu/osdep.h" 266ff37c3dSPhilippe Mathieu-Daudé #include "qemu/error-report.h" 27b635d98cSPhilippe Mathieu-Daudé #include "qapi/error.h" 2849ab747fSPaolo Bonzini #include "hw/hw.h" 2949ab747fSPaolo Bonzini #include "sysemu/dma.h" 3049ab747fSPaolo Bonzini #include "qemu/timer.h" 3149ab747fSPaolo Bonzini #include "qemu/bitops.h" 32f82a0f44SPhilippe Mathieu-Daudé #include "hw/sd/sdhci.h" 33637d23beSSai Pavan Boddu #include "sdhci-internal.h" 3403dd024fSPaolo Bonzini #include "qemu/log.h" 35bf8ec38eSPhilippe Mathieu-Daudé #include "qemu/cutils.h" 368be487d8SPhilippe Mathieu-Daudé #include "trace.h" 3749ab747fSPaolo Bonzini 3840bbc194SPeter Maydell #define TYPE_SDHCI_BUS "sdhci-bus" 3940bbc194SPeter Maydell #define SDHCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SDHCI_BUS) 4040bbc194SPeter Maydell 41aa164fbfSPhilippe Mathieu-Daudé #define MASKED_WRITE(reg, mask, val) (reg = (reg & (mask)) | (val)) 42aa164fbfSPhilippe Mathieu-Daudé 4349ab747fSPaolo Bonzini /* Default SD/MMC host controller features information, which will be 4449ab747fSPaolo Bonzini * presented in CAPABILITIES register of generic SD host controller at reset. 45aa164fbfSPhilippe Mathieu-Daudé * 46aa164fbfSPhilippe Mathieu-Daudé * support: 47aa164fbfSPhilippe Mathieu-Daudé * - 3.3v and 1.8v voltages 48aa164fbfSPhilippe Mathieu-Daudé * - SDMA/ADMA1/ADMA2 49aa164fbfSPhilippe Mathieu-Daudé * - high-speed 50aa164fbfSPhilippe Mathieu-Daudé * max host controller R/W buffers size: 512B 51aa164fbfSPhilippe Mathieu-Daudé * max clock frequency for SDclock: 52 MHz 52aa164fbfSPhilippe Mathieu-Daudé * timeout clock frequency: 52 MHz 53aa164fbfSPhilippe Mathieu-Daudé * 54aa164fbfSPhilippe Mathieu-Daudé * does not support: 55aa164fbfSPhilippe Mathieu-Daudé * - 3.0v voltage 56aa164fbfSPhilippe Mathieu-Daudé * - 64-bit system bus 57aa164fbfSPhilippe Mathieu-Daudé * - suspend/resume 5849ab747fSPaolo Bonzini */ 59aa164fbfSPhilippe Mathieu-Daudé #define SDHC_CAPAB_REG_DEFAULT 0x057834b4 6049ab747fSPaolo Bonzini 6109b738ffSPhilippe Mathieu-Daudé static inline unsigned int sdhci_get_fifolen(SDHCIState *s) 6209b738ffSPhilippe Mathieu-Daudé { 6309b738ffSPhilippe Mathieu-Daudé return 1 << (9 + FIELD_EX32(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH)); 6409b738ffSPhilippe Mathieu-Daudé } 6509b738ffSPhilippe Mathieu-Daudé 666ff37c3dSPhilippe Mathieu-Daudé /* return true on error */ 676ff37c3dSPhilippe Mathieu-Daudé static bool sdhci_check_capab_freq_range(SDHCIState *s, const char *desc, 686ff37c3dSPhilippe Mathieu-Daudé uint8_t freq, Error **errp) 696ff37c3dSPhilippe Mathieu-Daudé { 704d67852dSPhilippe Mathieu-Daudé if (s->sd_spec_version >= 3) { 714d67852dSPhilippe Mathieu-Daudé return false; 724d67852dSPhilippe Mathieu-Daudé } 736ff37c3dSPhilippe Mathieu-Daudé switch (freq) { 746ff37c3dSPhilippe Mathieu-Daudé case 0: 756ff37c3dSPhilippe Mathieu-Daudé case 10 ... 63: 766ff37c3dSPhilippe Mathieu-Daudé break; 776ff37c3dSPhilippe Mathieu-Daudé default: 786ff37c3dSPhilippe Mathieu-Daudé error_setg(errp, "SD %s clock frequency can have value" 796ff37c3dSPhilippe Mathieu-Daudé "in range 0-63 only", desc); 806ff37c3dSPhilippe Mathieu-Daudé return true; 816ff37c3dSPhilippe Mathieu-Daudé } 826ff37c3dSPhilippe Mathieu-Daudé return false; 836ff37c3dSPhilippe Mathieu-Daudé } 846ff37c3dSPhilippe Mathieu-Daudé 856ff37c3dSPhilippe Mathieu-Daudé static void sdhci_check_capareg(SDHCIState *s, Error **errp) 866ff37c3dSPhilippe Mathieu-Daudé { 876ff37c3dSPhilippe Mathieu-Daudé uint64_t msk = s->capareg; 886ff37c3dSPhilippe Mathieu-Daudé uint32_t val; 896ff37c3dSPhilippe Mathieu-Daudé bool y; 906ff37c3dSPhilippe Mathieu-Daudé 916ff37c3dSPhilippe Mathieu-Daudé switch (s->sd_spec_version) { 921e23b63fSPhilippe Mathieu-Daudé case 4: 931e23b63fSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT_V4); 941e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("64-bit system bus (v4)", val); 951e23b63fSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT_V4, 0); 961e23b63fSPhilippe Mathieu-Daudé 971e23b63fSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, UHS_II); 981e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("UHS-II", val); 991e23b63fSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, UHS_II, 0); 1001e23b63fSPhilippe Mathieu-Daudé 1011e23b63fSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA3); 1021e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("ADMA3", val); 1031e23b63fSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA3, 0); 1041e23b63fSPhilippe Mathieu-Daudé 1051e23b63fSPhilippe Mathieu-Daudé /* fallthrough */ 1064d67852dSPhilippe Mathieu-Daudé case 3: 1074d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ASYNC_INT); 1084d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("async interrupt", val); 1094d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ASYNC_INT, 0); 1104d67852dSPhilippe Mathieu-Daudé 1114d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SLOT_TYPE); 1124d67852dSPhilippe Mathieu-Daudé if (val) { 1134d67852dSPhilippe Mathieu-Daudé error_setg(errp, "slot-type not supported"); 1144d67852dSPhilippe Mathieu-Daudé return; 1154d67852dSPhilippe Mathieu-Daudé } 1164d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("slot type", val); 1174d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SLOT_TYPE, 0); 1184d67852dSPhilippe Mathieu-Daudé 1194d67852dSPhilippe Mathieu-Daudé if (val != 2) { 1204d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, EMBEDDED_8BIT); 1214d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("8-bit bus", val); 1224d67852dSPhilippe Mathieu-Daudé } 1234d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, EMBEDDED_8BIT, 0); 1244d67852dSPhilippe Mathieu-Daudé 1254d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS_SPEED); 1264d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("bus speed mask", val); 1274d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BUS_SPEED, 0); 1284d67852dSPhilippe Mathieu-Daudé 1294d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, DRIVER_STRENGTH); 1304d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("driver strength mask", val); 1314d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, DRIVER_STRENGTH, 0); 1324d67852dSPhilippe Mathieu-Daudé 1334d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, TIMER_RETUNING); 1344d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("timer re-tuning", val); 1354d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, TIMER_RETUNING, 0); 1364d67852dSPhilippe Mathieu-Daudé 1374d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDR50_TUNING); 1384d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("use SDR50 tuning", val); 1394d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SDR50_TUNING, 0); 1404d67852dSPhilippe Mathieu-Daudé 1414d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, RETUNING_MODE); 1424d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("re-tuning mode", val); 1434d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, RETUNING_MODE, 0); 1444d67852dSPhilippe Mathieu-Daudé 1454d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, CLOCK_MULT); 1464d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("clock multiplier", val); 1474d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, CLOCK_MULT, 0); 1484d67852dSPhilippe Mathieu-Daudé 1494d67852dSPhilippe Mathieu-Daudé /* fallthrough */ 1506ff37c3dSPhilippe Mathieu-Daudé case 2: /* default version */ 1510540fba9SPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA2); 1520540fba9SPhilippe Mathieu-Daudé trace_sdhci_capareg("ADMA2", val); 1530540fba9SPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA2, 0); 1540540fba9SPhilippe Mathieu-Daudé 1550540fba9SPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA1); 1560540fba9SPhilippe Mathieu-Daudé trace_sdhci_capareg("ADMA1", val); 1570540fba9SPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA1, 0); 1580540fba9SPhilippe Mathieu-Daudé 1590540fba9SPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT); 1601e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("64-bit system bus (v3)", val); 1610540fba9SPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT, 0); 1626ff37c3dSPhilippe Mathieu-Daudé 1636ff37c3dSPhilippe Mathieu-Daudé /* fallthrough */ 1646ff37c3dSPhilippe Mathieu-Daudé case 1: 1656ff37c3dSPhilippe Mathieu-Daudé y = FIELD_EX64(s->capareg, SDHC_CAPAB, TOUNIT); 1666ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, TOUNIT, 0); 1676ff37c3dSPhilippe Mathieu-Daudé 1686ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, TOCLKFREQ); 1696ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg(y ? "timeout (MHz)" : "Timeout (KHz)", val); 1706ff37c3dSPhilippe Mathieu-Daudé if (sdhci_check_capab_freq_range(s, "timeout", val, errp)) { 1716ff37c3dSPhilippe Mathieu-Daudé return; 1726ff37c3dSPhilippe Mathieu-Daudé } 1736ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, TOCLKFREQ, 0); 1746ff37c3dSPhilippe Mathieu-Daudé 1756ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BASECLKFREQ); 1766ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg(y ? "base (MHz)" : "Base (KHz)", val); 1776ff37c3dSPhilippe Mathieu-Daudé if (sdhci_check_capab_freq_range(s, "base", val, errp)) { 1786ff37c3dSPhilippe Mathieu-Daudé return; 1796ff37c3dSPhilippe Mathieu-Daudé } 1806ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BASECLKFREQ, 0); 1816ff37c3dSPhilippe Mathieu-Daudé 1826ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH); 1836ff37c3dSPhilippe Mathieu-Daudé if (val >= 3) { 1846ff37c3dSPhilippe Mathieu-Daudé error_setg(errp, "block size can be 512, 1024 or 2048 only"); 1856ff37c3dSPhilippe Mathieu-Daudé return; 1866ff37c3dSPhilippe Mathieu-Daudé } 1876ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("max block length", sdhci_get_fifolen(s)); 1886ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, MAXBLOCKLENGTH, 0); 1896ff37c3dSPhilippe Mathieu-Daudé 1906ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, HIGHSPEED); 1916ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("high speed", val); 1926ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, HIGHSPEED, 0); 1936ff37c3dSPhilippe Mathieu-Daudé 1946ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDMA); 1956ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("SDMA", val); 1966ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SDMA, 0); 1976ff37c3dSPhilippe Mathieu-Daudé 1986ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SUSPRESUME); 1996ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("suspend/resume", val); 2006ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SUSPRESUME, 0); 2016ff37c3dSPhilippe Mathieu-Daudé 2026ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, V33); 2036ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("3.3v", val); 2046ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, V33, 0); 2056ff37c3dSPhilippe Mathieu-Daudé 2066ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, V30); 2076ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("3.0v", val); 2086ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, V30, 0); 2096ff37c3dSPhilippe Mathieu-Daudé 2106ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, V18); 2116ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("1.8v", val); 2126ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, V18, 0); 2136ff37c3dSPhilippe Mathieu-Daudé break; 2146ff37c3dSPhilippe Mathieu-Daudé 2156ff37c3dSPhilippe Mathieu-Daudé default: 2166ff37c3dSPhilippe Mathieu-Daudé error_setg(errp, "Unsupported spec version: %u", s->sd_spec_version); 2176ff37c3dSPhilippe Mathieu-Daudé } 2186ff37c3dSPhilippe Mathieu-Daudé if (msk) { 2196ff37c3dSPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, 2206ff37c3dSPhilippe Mathieu-Daudé "SDHCI: unknown CAPAB mask: 0x%016" PRIx64 "\n", msk); 2216ff37c3dSPhilippe Mathieu-Daudé } 2226ff37c3dSPhilippe Mathieu-Daudé } 2236ff37c3dSPhilippe Mathieu-Daudé 22449ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s) 22549ab747fSPaolo Bonzini { 22649ab747fSPaolo Bonzini return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) || 22749ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) || 22849ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV)); 22949ab747fSPaolo Bonzini } 23049ab747fSPaolo Bonzini 23149ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s) 23249ab747fSPaolo Bonzini { 23349ab747fSPaolo Bonzini qemu_set_irq(s->irq, sdhci_slotint(s)); 23449ab747fSPaolo Bonzini } 23549ab747fSPaolo Bonzini 23649ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque) 23749ab747fSPaolo Bonzini { 23849ab747fSPaolo Bonzini SDHCIState *s = (SDHCIState *)opaque; 23949ab747fSPaolo Bonzini 24049ab747fSPaolo Bonzini if (s->norintsts & SDHC_NIS_REMOVE) { 241bc72ad67SAlex Bligh timer_mod(s->insert_timer, 242bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 24349ab747fSPaolo Bonzini } else { 24449ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 24549ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 24649ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 24749ab747fSPaolo Bonzini } 24849ab747fSPaolo Bonzini sdhci_update_irq(s); 24949ab747fSPaolo Bonzini } 25049ab747fSPaolo Bonzini } 25149ab747fSPaolo Bonzini 25240bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level) 25349ab747fSPaolo Bonzini { 25440bbc194SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 25549ab747fSPaolo Bonzini 2568be487d8SPhilippe Mathieu-Daudé trace_sdhci_set_inserted(level ? "insert" : "eject"); 25749ab747fSPaolo Bonzini if ((s->norintsts & SDHC_NIS_REMOVE) && level) { 25849ab747fSPaolo Bonzini /* Give target some time to notice card ejection */ 259bc72ad67SAlex Bligh timer_mod(s->insert_timer, 260bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 26149ab747fSPaolo Bonzini } else { 26249ab747fSPaolo Bonzini if (level) { 26349ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 26449ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 26549ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 26649ab747fSPaolo Bonzini } 26749ab747fSPaolo Bonzini } else { 26849ab747fSPaolo Bonzini s->prnsts = 0x1fa0000; 26949ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 27049ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_SDCLK_EN; 27149ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_REMOVE) { 27249ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_REMOVE; 27349ab747fSPaolo Bonzini } 27449ab747fSPaolo Bonzini } 27549ab747fSPaolo Bonzini sdhci_update_irq(s); 27649ab747fSPaolo Bonzini } 27749ab747fSPaolo Bonzini } 27849ab747fSPaolo Bonzini 27940bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level) 28049ab747fSPaolo Bonzini { 28140bbc194SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 28249ab747fSPaolo Bonzini 28349ab747fSPaolo Bonzini if (level) { 28449ab747fSPaolo Bonzini s->prnsts &= ~SDHC_WRITE_PROTECT; 28549ab747fSPaolo Bonzini } else { 28649ab747fSPaolo Bonzini /* Write enabled */ 28749ab747fSPaolo Bonzini s->prnsts |= SDHC_WRITE_PROTECT; 28849ab747fSPaolo Bonzini } 28949ab747fSPaolo Bonzini } 29049ab747fSPaolo Bonzini 29149ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s) 29249ab747fSPaolo Bonzini { 29340bbc194SPeter Maydell DeviceState *dev = DEVICE(s); 29440bbc194SPeter Maydell 295bc72ad67SAlex Bligh timer_del(s->insert_timer); 296bc72ad67SAlex Bligh timer_del(s->transfer_timer); 297aceb5b06SPhilippe Mathieu-Daudé 298aceb5b06SPhilippe Mathieu-Daudé /* Set all registers to 0. Capabilities/Version registers are not cleared 29949ab747fSPaolo Bonzini * and assumed to always preserve their value, given to them during 30049ab747fSPaolo Bonzini * initialization */ 30149ab747fSPaolo Bonzini memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad); 30249ab747fSPaolo Bonzini 30340bbc194SPeter Maydell /* Reset other state based on current card insertion/readonly status */ 30440bbc194SPeter Maydell sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus)); 30540bbc194SPeter Maydell sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus)); 30640bbc194SPeter Maydell 30749ab747fSPaolo Bonzini s->data_count = 0; 30849ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 3090a7ac9f9SAndrew Baumann s->pending_insert_state = false; 31049ab747fSPaolo Bonzini } 31149ab747fSPaolo Bonzini 3128b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev) 3138b41c305SPeter Maydell { 3148b41c305SPeter Maydell /* QOM (ie power-on) reset. This is identical to reset 3158b41c305SPeter Maydell * commanded via device register apart from handling of the 3168b41c305SPeter Maydell * 'pending insert on powerup' quirk. 3178b41c305SPeter Maydell */ 3188b41c305SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 3198b41c305SPeter Maydell 3208b41c305SPeter Maydell sdhci_reset(s); 3218b41c305SPeter Maydell 3228b41c305SPeter Maydell if (s->pending_insert_quirk) { 3238b41c305SPeter Maydell s->pending_insert_state = true; 3248b41c305SPeter Maydell } 3258b41c305SPeter Maydell } 3268b41c305SPeter Maydell 327d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque); 32849ab747fSPaolo Bonzini 32949ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s) 33049ab747fSPaolo Bonzini { 33149ab747fSPaolo Bonzini SDRequest request; 33249ab747fSPaolo Bonzini uint8_t response[16]; 33349ab747fSPaolo Bonzini int rlen; 33449ab747fSPaolo Bonzini 33549ab747fSPaolo Bonzini s->errintsts = 0; 33649ab747fSPaolo Bonzini s->acmd12errsts = 0; 33749ab747fSPaolo Bonzini request.cmd = s->cmdreg >> 8; 33849ab747fSPaolo Bonzini request.arg = s->argument; 3398be487d8SPhilippe Mathieu-Daudé 3408be487d8SPhilippe Mathieu-Daudé trace_sdhci_send_command(request.cmd, request.arg); 34140bbc194SPeter Maydell rlen = sdbus_do_command(&s->sdbus, &request, response); 34249ab747fSPaolo Bonzini 34349ab747fSPaolo Bonzini if (s->cmdreg & SDHC_CMD_RESPONSE) { 34449ab747fSPaolo Bonzini if (rlen == 4) { 345*b3141c06SPhilippe Mathieu-Daudé s->rspreg[0] = ldl_be_p(response); 34649ab747fSPaolo Bonzini s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0; 3478be487d8SPhilippe Mathieu-Daudé trace_sdhci_response4(s->rspreg[0]); 34849ab747fSPaolo Bonzini } else if (rlen == 16) { 349*b3141c06SPhilippe Mathieu-Daudé s->rspreg[0] = ldl_be_p(&response[11]); 350*b3141c06SPhilippe Mathieu-Daudé s->rspreg[1] = ldl_be_p(&response[7]); 351*b3141c06SPhilippe Mathieu-Daudé s->rspreg[2] = ldl_be_p(&response[3]); 35249ab747fSPaolo Bonzini s->rspreg[3] = (response[0] << 16) | (response[1] << 8) | 35349ab747fSPaolo Bonzini response[2]; 3548be487d8SPhilippe Mathieu-Daudé trace_sdhci_response16(s->rspreg[3], s->rspreg[2], 3558be487d8SPhilippe Mathieu-Daudé s->rspreg[1], s->rspreg[0]); 35649ab747fSPaolo Bonzini } else { 3578be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("timeout waiting for command response"); 35849ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) { 35949ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMDTIMEOUT; 36049ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 36149ab747fSPaolo Bonzini } 36249ab747fSPaolo Bonzini } 36349ab747fSPaolo Bonzini 364fd1e5c81SAndrey Smirnov if (!(s->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) && 365fd1e5c81SAndrey Smirnov (s->norintstsen & SDHC_NISEN_TRSCMP) && 36649ab747fSPaolo Bonzini (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) { 36749ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 36849ab747fSPaolo Bonzini } 36949ab747fSPaolo Bonzini } 37049ab747fSPaolo Bonzini 37149ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CMDCMP) { 37249ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_CMDCMP; 37349ab747fSPaolo Bonzini } 37449ab747fSPaolo Bonzini 37549ab747fSPaolo Bonzini sdhci_update_irq(s); 37649ab747fSPaolo Bonzini 37749ab747fSPaolo Bonzini if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) { 378656f416cSPeter Crosthwaite s->data_count = 0; 379d368ba43SKevin O'Connor sdhci_data_transfer(s); 38049ab747fSPaolo Bonzini } 38149ab747fSPaolo Bonzini } 38249ab747fSPaolo Bonzini 38349ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s) 38449ab747fSPaolo Bonzini { 38549ab747fSPaolo Bonzini /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */ 38649ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) { 38749ab747fSPaolo Bonzini SDRequest request; 38849ab747fSPaolo Bonzini uint8_t response[16]; 38949ab747fSPaolo Bonzini 39049ab747fSPaolo Bonzini request.cmd = 0x0C; 39149ab747fSPaolo Bonzini request.arg = 0; 3928be487d8SPhilippe Mathieu-Daudé trace_sdhci_end_transfer(request.cmd, request.arg); 39340bbc194SPeter Maydell sdbus_do_command(&s->sdbus, &request, response); 39449ab747fSPaolo Bonzini /* Auto CMD12 response goes to the upper Response register */ 395*b3141c06SPhilippe Mathieu-Daudé s->rspreg[3] = ldl_be_p(response); 39649ab747fSPaolo Bonzini } 39749ab747fSPaolo Bonzini 39849ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE | 39949ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT | 40049ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE); 40149ab747fSPaolo Bonzini 40249ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_TRSCMP) { 40349ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 40449ab747fSPaolo Bonzini } 40549ab747fSPaolo Bonzini 40649ab747fSPaolo Bonzini sdhci_update_irq(s); 40749ab747fSPaolo Bonzini } 40849ab747fSPaolo Bonzini 40949ab747fSPaolo Bonzini /* 41049ab747fSPaolo Bonzini * Programmed i/o data transfer 41149ab747fSPaolo Bonzini */ 412bf8ec38eSPhilippe Mathieu-Daudé #define BLOCK_SIZE_MASK (4 * K_BYTE - 1) 41349ab747fSPaolo Bonzini 41449ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */ 41549ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s) 41649ab747fSPaolo Bonzini { 41749ab747fSPaolo Bonzini int index = 0; 418ea55a221SPhilippe Mathieu-Daudé uint8_t data; 419ea55a221SPhilippe Mathieu-Daudé const uint16_t blk_size = s->blksize & BLOCK_SIZE_MASK; 42049ab747fSPaolo Bonzini 42149ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) && 42249ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) { 42349ab747fSPaolo Bonzini return; 42449ab747fSPaolo Bonzini } 42549ab747fSPaolo Bonzini 426ea55a221SPhilippe Mathieu-Daudé for (index = 0; index < blk_size; index++) { 427ea55a221SPhilippe Mathieu-Daudé data = sdbus_read_data(&s->sdbus); 428ea55a221SPhilippe Mathieu-Daudé if (!FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) { 42908022a91SPhilippe Mathieu-Daudé /* Device is not in tuning */ 430ea55a221SPhilippe Mathieu-Daudé s->fifo_buffer[index] = data; 431ea55a221SPhilippe Mathieu-Daudé } 432ea55a221SPhilippe Mathieu-Daudé } 433ea55a221SPhilippe Mathieu-Daudé 434ea55a221SPhilippe Mathieu-Daudé if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) { 43508022a91SPhilippe Mathieu-Daudé /* Device is in tuning */ 436ea55a221SPhilippe Mathieu-Daudé s->hostctl2 &= ~R_SDHC_HOSTCTL2_EXECUTE_TUNING_MASK; 437ea55a221SPhilippe Mathieu-Daudé s->hostctl2 |= R_SDHC_HOSTCTL2_SAMPLING_CLKSEL_MASK; 438ea55a221SPhilippe Mathieu-Daudé s->prnsts &= ~(SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ | 439ea55a221SPhilippe Mathieu-Daudé SDHC_DATA_INHIBIT); 440ea55a221SPhilippe Mathieu-Daudé goto read_done; 44149ab747fSPaolo Bonzini } 44249ab747fSPaolo Bonzini 44349ab747fSPaolo Bonzini /* New data now available for READ through Buffer Port Register */ 44449ab747fSPaolo Bonzini s->prnsts |= SDHC_DATA_AVAILABLE; 44549ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_RBUFRDY) { 44649ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_RBUFRDY; 44749ab747fSPaolo Bonzini } 44849ab747fSPaolo Bonzini 44949ab747fSPaolo Bonzini /* Clear DAT line active status if that was the last block */ 45049ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 45149ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) { 45249ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 45349ab747fSPaolo Bonzini } 45449ab747fSPaolo Bonzini 45549ab747fSPaolo Bonzini /* If stop at block gap request was set and it's not the last block of 45649ab747fSPaolo Bonzini * data - generate Block Event interrupt */ 45749ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) && 45849ab747fSPaolo Bonzini s->blkcnt != 1) { 45949ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 46049ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 46149ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 46249ab747fSPaolo Bonzini } 46349ab747fSPaolo Bonzini } 46449ab747fSPaolo Bonzini 465ea55a221SPhilippe Mathieu-Daudé read_done: 46649ab747fSPaolo Bonzini sdhci_update_irq(s); 46749ab747fSPaolo Bonzini } 46849ab747fSPaolo Bonzini 46949ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */ 47049ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size) 47149ab747fSPaolo Bonzini { 47249ab747fSPaolo Bonzini uint32_t value = 0; 47349ab747fSPaolo Bonzini int i; 47449ab747fSPaolo Bonzini 47549ab747fSPaolo Bonzini /* first check that a valid data exists in host controller input buffer */ 47649ab747fSPaolo Bonzini if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) { 4778be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("read from empty buffer"); 47849ab747fSPaolo Bonzini return 0; 47949ab747fSPaolo Bonzini } 48049ab747fSPaolo Bonzini 48149ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 48249ab747fSPaolo Bonzini value |= s->fifo_buffer[s->data_count] << i * 8; 48349ab747fSPaolo Bonzini s->data_count++; 48449ab747fSPaolo Bonzini /* check if we've read all valid data (blksize bytes) from buffer */ 485bf8ec38eSPhilippe Mathieu-Daudé if ((s->data_count) >= (s->blksize & BLOCK_SIZE_MASK)) { 4868be487d8SPhilippe Mathieu-Daudé trace_sdhci_read_dataport(s->data_count); 48749ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */ 48849ab747fSPaolo Bonzini s->data_count = 0; /* next buff read must start at position [0] */ 48949ab747fSPaolo Bonzini 49049ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 49149ab747fSPaolo Bonzini s->blkcnt--; 49249ab747fSPaolo Bonzini } 49349ab747fSPaolo Bonzini 49449ab747fSPaolo Bonzini /* if that was the last block of data */ 49549ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 49649ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) || 49749ab747fSPaolo Bonzini /* stop at gap request */ 49849ab747fSPaolo Bonzini (s->stopped_state == sdhc_gap_read && 49949ab747fSPaolo Bonzini !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) { 500d368ba43SKevin O'Connor sdhci_end_transfer(s); 50149ab747fSPaolo Bonzini } else { /* if there are more data, read next block from card */ 502d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 50349ab747fSPaolo Bonzini } 50449ab747fSPaolo Bonzini break; 50549ab747fSPaolo Bonzini } 50649ab747fSPaolo Bonzini } 50749ab747fSPaolo Bonzini 50849ab747fSPaolo Bonzini return value; 50949ab747fSPaolo Bonzini } 51049ab747fSPaolo Bonzini 51149ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */ 51249ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s) 51349ab747fSPaolo Bonzini { 51449ab747fSPaolo Bonzini int index = 0; 51549ab747fSPaolo Bonzini 51649ab747fSPaolo Bonzini if (s->prnsts & SDHC_SPACE_AVAILABLE) { 51749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 51849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_WBUFRDY; 51949ab747fSPaolo Bonzini } 52049ab747fSPaolo Bonzini sdhci_update_irq(s); 52149ab747fSPaolo Bonzini return; 52249ab747fSPaolo Bonzini } 52349ab747fSPaolo Bonzini 52449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 52549ab747fSPaolo Bonzini if (s->blkcnt == 0) { 52649ab747fSPaolo Bonzini return; 52749ab747fSPaolo Bonzini } else { 52849ab747fSPaolo Bonzini s->blkcnt--; 52949ab747fSPaolo Bonzini } 53049ab747fSPaolo Bonzini } 53149ab747fSPaolo Bonzini 532bf8ec38eSPhilippe Mathieu-Daudé for (index = 0; index < (s->blksize & BLOCK_SIZE_MASK); index++) { 53340bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[index]); 53449ab747fSPaolo Bonzini } 53549ab747fSPaolo Bonzini 53649ab747fSPaolo Bonzini /* Next data can be written through BUFFER DATORT register */ 53749ab747fSPaolo Bonzini s->prnsts |= SDHC_SPACE_AVAILABLE; 53849ab747fSPaolo Bonzini 53949ab747fSPaolo Bonzini /* Finish transfer if that was the last block of data */ 54049ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 54149ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && 54249ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) { 543d368ba43SKevin O'Connor sdhci_end_transfer(s); 544dcdb4cd8SPeter Crosthwaite } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 545dcdb4cd8SPeter Crosthwaite s->norintsts |= SDHC_NIS_WBUFRDY; 54649ab747fSPaolo Bonzini } 54749ab747fSPaolo Bonzini 54849ab747fSPaolo Bonzini /* Generate Block Gap Event if requested and if not the last block */ 54949ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) && 55049ab747fSPaolo Bonzini s->blkcnt > 0) { 55149ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DOING_WRITE; 55249ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 55349ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 55449ab747fSPaolo Bonzini } 555d368ba43SKevin O'Connor sdhci_end_transfer(s); 55649ab747fSPaolo Bonzini } 55749ab747fSPaolo Bonzini 55849ab747fSPaolo Bonzini sdhci_update_irq(s); 55949ab747fSPaolo Bonzini } 56049ab747fSPaolo Bonzini 56149ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port 56249ab747fSPaolo Bonzini * register */ 56349ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size) 56449ab747fSPaolo Bonzini { 56549ab747fSPaolo Bonzini unsigned i; 56649ab747fSPaolo Bonzini 56749ab747fSPaolo Bonzini /* Check that there is free space left in a buffer */ 56849ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) { 5698be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Can't write to data buffer: buffer full"); 57049ab747fSPaolo Bonzini return; 57149ab747fSPaolo Bonzini } 57249ab747fSPaolo Bonzini 57349ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 57449ab747fSPaolo Bonzini s->fifo_buffer[s->data_count] = value & 0xFF; 57549ab747fSPaolo Bonzini s->data_count++; 57649ab747fSPaolo Bonzini value >>= 8; 577bf8ec38eSPhilippe Mathieu-Daudé if (s->data_count >= (s->blksize & BLOCK_SIZE_MASK)) { 5788be487d8SPhilippe Mathieu-Daudé trace_sdhci_write_dataport(s->data_count); 57949ab747fSPaolo Bonzini s->data_count = 0; 58049ab747fSPaolo Bonzini s->prnsts &= ~SDHC_SPACE_AVAILABLE; 58149ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_WRITE) { 582d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 58349ab747fSPaolo Bonzini } 58449ab747fSPaolo Bonzini } 58549ab747fSPaolo Bonzini } 58649ab747fSPaolo Bonzini } 58749ab747fSPaolo Bonzini 58849ab747fSPaolo Bonzini /* 58949ab747fSPaolo Bonzini * Single DMA data transfer 59049ab747fSPaolo Bonzini */ 59149ab747fSPaolo Bonzini 59249ab747fSPaolo Bonzini /* Multi block SDMA transfer */ 59349ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s) 59449ab747fSPaolo Bonzini { 59549ab747fSPaolo Bonzini bool page_aligned = false; 59649ab747fSPaolo Bonzini unsigned int n, begin; 597bf8ec38eSPhilippe Mathieu-Daudé const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK; 598bf8ec38eSPhilippe Mathieu-Daudé uint32_t boundary_chk = 1 << (((s->blksize & ~BLOCK_SIZE_MASK) >> 12) + 12); 59949ab747fSPaolo Bonzini uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk); 60049ab747fSPaolo Bonzini 6016e86d903SPrasad J Pandit if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) { 6026e86d903SPrasad J Pandit qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n"); 6036e86d903SPrasad J Pandit return; 6046e86d903SPrasad J Pandit } 6056e86d903SPrasad J Pandit 60649ab747fSPaolo Bonzini /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for 60749ab747fSPaolo Bonzini * possible stop at page boundary if initial address is not page aligned, 60849ab747fSPaolo Bonzini * allow them to work properly */ 60949ab747fSPaolo Bonzini if ((s->sdmasysad % boundary_chk) == 0) { 61049ab747fSPaolo Bonzini page_aligned = true; 61149ab747fSPaolo Bonzini } 61249ab747fSPaolo Bonzini 61349ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 61449ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT | 61549ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 61649ab747fSPaolo Bonzini while (s->blkcnt) { 61749ab747fSPaolo Bonzini if (s->data_count == 0) { 61849ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 61940bbc194SPeter Maydell s->fifo_buffer[n] = sdbus_read_data(&s->sdbus); 62049ab747fSPaolo Bonzini } 62149ab747fSPaolo Bonzini } 62249ab747fSPaolo Bonzini begin = s->data_count; 62349ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 62449ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 62549ab747fSPaolo Bonzini boundary_count = 0; 62649ab747fSPaolo Bonzini } else { 62749ab747fSPaolo Bonzini s->data_count = block_size; 62849ab747fSPaolo Bonzini boundary_count -= block_size - begin; 62949ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 63049ab747fSPaolo Bonzini s->blkcnt--; 63149ab747fSPaolo Bonzini } 63249ab747fSPaolo Bonzini } 633dd55c485SPhilippe Mathieu-Daudé dma_memory_write(s->dma_as, s->sdmasysad, 63449ab747fSPaolo Bonzini &s->fifo_buffer[begin], s->data_count - begin); 63549ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 63649ab747fSPaolo Bonzini if (s->data_count == block_size) { 63749ab747fSPaolo Bonzini s->data_count = 0; 63849ab747fSPaolo Bonzini } 63949ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 64049ab747fSPaolo Bonzini break; 64149ab747fSPaolo Bonzini } 64249ab747fSPaolo Bonzini } 64349ab747fSPaolo Bonzini } else { 64449ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT | 64549ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 64649ab747fSPaolo Bonzini while (s->blkcnt) { 64749ab747fSPaolo Bonzini begin = s->data_count; 64849ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 64949ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 65049ab747fSPaolo Bonzini boundary_count = 0; 65149ab747fSPaolo Bonzini } else { 65249ab747fSPaolo Bonzini s->data_count = block_size; 65349ab747fSPaolo Bonzini boundary_count -= block_size - begin; 65449ab747fSPaolo Bonzini } 655dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, s->sdmasysad, 65642922105SPrasad J Pandit &s->fifo_buffer[begin], s->data_count - begin); 65749ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 65849ab747fSPaolo Bonzini if (s->data_count == block_size) { 65949ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 66040bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[n]); 66149ab747fSPaolo Bonzini } 66249ab747fSPaolo Bonzini s->data_count = 0; 66349ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 66449ab747fSPaolo Bonzini s->blkcnt--; 66549ab747fSPaolo Bonzini } 66649ab747fSPaolo Bonzini } 66749ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 66849ab747fSPaolo Bonzini break; 66949ab747fSPaolo Bonzini } 67049ab747fSPaolo Bonzini } 67149ab747fSPaolo Bonzini } 67249ab747fSPaolo Bonzini 67349ab747fSPaolo Bonzini if (s->blkcnt == 0) { 674d368ba43SKevin O'Connor sdhci_end_transfer(s); 67549ab747fSPaolo Bonzini } else { 67649ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_DMA) { 67749ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_DMA; 67849ab747fSPaolo Bonzini } 67949ab747fSPaolo Bonzini sdhci_update_irq(s); 68049ab747fSPaolo Bonzini } 68149ab747fSPaolo Bonzini } 68249ab747fSPaolo Bonzini 68349ab747fSPaolo Bonzini /* single block SDMA transfer */ 68449ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s) 68549ab747fSPaolo Bonzini { 68649ab747fSPaolo Bonzini int n; 687bf8ec38eSPhilippe Mathieu-Daudé uint32_t datacnt = s->blksize & BLOCK_SIZE_MASK; 68849ab747fSPaolo Bonzini 68949ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 69049ab747fSPaolo Bonzini for (n = 0; n < datacnt; n++) { 69140bbc194SPeter Maydell s->fifo_buffer[n] = sdbus_read_data(&s->sdbus); 69249ab747fSPaolo Bonzini } 693dd55c485SPhilippe Mathieu-Daudé dma_memory_write(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt); 69449ab747fSPaolo Bonzini } else { 695dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt); 69649ab747fSPaolo Bonzini for (n = 0; n < datacnt; n++) { 69740bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[n]); 69849ab747fSPaolo Bonzini } 69949ab747fSPaolo Bonzini } 70049ab747fSPaolo Bonzini s->blkcnt--; 70149ab747fSPaolo Bonzini 702d368ba43SKevin O'Connor sdhci_end_transfer(s); 70349ab747fSPaolo Bonzini } 70449ab747fSPaolo Bonzini 70549ab747fSPaolo Bonzini typedef struct ADMADescr { 70649ab747fSPaolo Bonzini hwaddr addr; 70749ab747fSPaolo Bonzini uint16_t length; 70849ab747fSPaolo Bonzini uint8_t attr; 70949ab747fSPaolo Bonzini uint8_t incr; 71049ab747fSPaolo Bonzini } ADMADescr; 71149ab747fSPaolo Bonzini 71249ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr) 71349ab747fSPaolo Bonzini { 71449ab747fSPaolo Bonzini uint32_t adma1 = 0; 71549ab747fSPaolo Bonzini uint64_t adma2 = 0; 71649ab747fSPaolo Bonzini hwaddr entry_addr = (hwaddr)s->admasysaddr; 71706c5120bSPhilippe Mathieu-Daudé switch (SDHC_DMA_TYPE(s->hostctl1)) { 71849ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 719dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma2, 72049ab747fSPaolo Bonzini sizeof(adma2)); 72149ab747fSPaolo Bonzini adma2 = le64_to_cpu(adma2); 72249ab747fSPaolo Bonzini /* The spec does not specify endianness of descriptor table. 72349ab747fSPaolo Bonzini * We currently assume that it is LE. 72449ab747fSPaolo Bonzini */ 72549ab747fSPaolo Bonzini dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull; 72649ab747fSPaolo Bonzini dscr->length = (uint16_t)extract64(adma2, 16, 16); 72749ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract64(adma2, 0, 7); 72849ab747fSPaolo Bonzini dscr->incr = 8; 72949ab747fSPaolo Bonzini break; 73049ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 731dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma1, 73249ab747fSPaolo Bonzini sizeof(adma1)); 73349ab747fSPaolo Bonzini adma1 = le32_to_cpu(adma1); 73449ab747fSPaolo Bonzini dscr->addr = (hwaddr)(adma1 & 0xFFFFF000); 73549ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract32(adma1, 0, 7); 73649ab747fSPaolo Bonzini dscr->incr = 4; 73749ab747fSPaolo Bonzini if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) { 73849ab747fSPaolo Bonzini dscr->length = (uint16_t)extract32(adma1, 12, 16); 73949ab747fSPaolo Bonzini } else { 74049ab747fSPaolo Bonzini dscr->length = 4096; 74149ab747fSPaolo Bonzini } 74249ab747fSPaolo Bonzini break; 74349ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 744dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr, 74549ab747fSPaolo Bonzini (uint8_t *)(&dscr->attr), 1); 746dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr + 2, 74749ab747fSPaolo Bonzini (uint8_t *)(&dscr->length), 2); 74849ab747fSPaolo Bonzini dscr->length = le16_to_cpu(dscr->length); 749dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr + 4, 75049ab747fSPaolo Bonzini (uint8_t *)(&dscr->addr), 8); 75104654b5aSSai Pavan Boddu dscr->addr = le64_to_cpu(dscr->addr); 75204654b5aSSai Pavan Boddu dscr->attr &= (uint8_t) ~0xC0; 75349ab747fSPaolo Bonzini dscr->incr = 12; 75449ab747fSPaolo Bonzini break; 75549ab747fSPaolo Bonzini } 75649ab747fSPaolo Bonzini } 75749ab747fSPaolo Bonzini 75849ab747fSPaolo Bonzini /* Advanced DMA data transfer */ 75949ab747fSPaolo Bonzini 76049ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s) 76149ab747fSPaolo Bonzini { 76249ab747fSPaolo Bonzini unsigned int n, begin, length; 763bf8ec38eSPhilippe Mathieu-Daudé const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK; 7648be487d8SPhilippe Mathieu-Daudé ADMADescr dscr = {}; 76549ab747fSPaolo Bonzini int i; 76649ab747fSPaolo Bonzini 76749ab747fSPaolo Bonzini for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) { 76849ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH; 76949ab747fSPaolo Bonzini 77049ab747fSPaolo Bonzini get_adma_description(s, &dscr); 7718be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma_loop(dscr.addr, dscr.length, dscr.attr); 77249ab747fSPaolo Bonzini 77349ab747fSPaolo Bonzini if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) { 77449ab747fSPaolo Bonzini /* Indicate that error occurred in ST_FDS state */ 77549ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_STATE_MASK; 77649ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS; 77749ab747fSPaolo Bonzini 77849ab747fSPaolo Bonzini /* Generate ADMA error interrupt */ 77949ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 78049ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 78149ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 78249ab747fSPaolo Bonzini } 78349ab747fSPaolo Bonzini 78449ab747fSPaolo Bonzini sdhci_update_irq(s); 78549ab747fSPaolo Bonzini return; 78649ab747fSPaolo Bonzini } 78749ab747fSPaolo Bonzini 78849ab747fSPaolo Bonzini length = dscr.length ? dscr.length : 65536; 78949ab747fSPaolo Bonzini 79049ab747fSPaolo Bonzini switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) { 79149ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_TRAN: /* data transfer */ 79249ab747fSPaolo Bonzini 79349ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 79449ab747fSPaolo Bonzini while (length) { 79549ab747fSPaolo Bonzini if (s->data_count == 0) { 79649ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 79740bbc194SPeter Maydell s->fifo_buffer[n] = sdbus_read_data(&s->sdbus); 79849ab747fSPaolo Bonzini } 79949ab747fSPaolo Bonzini } 80049ab747fSPaolo Bonzini begin = s->data_count; 80149ab747fSPaolo Bonzini if ((length + begin) < block_size) { 80249ab747fSPaolo Bonzini s->data_count = length + begin; 80349ab747fSPaolo Bonzini length = 0; 80449ab747fSPaolo Bonzini } else { 80549ab747fSPaolo Bonzini s->data_count = block_size; 80649ab747fSPaolo Bonzini length -= block_size - begin; 80749ab747fSPaolo Bonzini } 808dd55c485SPhilippe Mathieu-Daudé dma_memory_write(s->dma_as, dscr.addr, 80949ab747fSPaolo Bonzini &s->fifo_buffer[begin], 81049ab747fSPaolo Bonzini s->data_count - begin); 81149ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 81249ab747fSPaolo Bonzini if (s->data_count == block_size) { 81349ab747fSPaolo Bonzini s->data_count = 0; 81449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 81549ab747fSPaolo Bonzini s->blkcnt--; 81649ab747fSPaolo Bonzini if (s->blkcnt == 0) { 81749ab747fSPaolo Bonzini break; 81849ab747fSPaolo Bonzini } 81949ab747fSPaolo Bonzini } 82049ab747fSPaolo Bonzini } 82149ab747fSPaolo Bonzini } 82249ab747fSPaolo Bonzini } else { 82349ab747fSPaolo Bonzini while (length) { 82449ab747fSPaolo Bonzini begin = s->data_count; 82549ab747fSPaolo Bonzini if ((length + begin) < block_size) { 82649ab747fSPaolo Bonzini s->data_count = length + begin; 82749ab747fSPaolo Bonzini length = 0; 82849ab747fSPaolo Bonzini } else { 82949ab747fSPaolo Bonzini s->data_count = block_size; 83049ab747fSPaolo Bonzini length -= block_size - begin; 83149ab747fSPaolo Bonzini } 832dd55c485SPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, dscr.addr, 8339db11cefSPeter Crosthwaite &s->fifo_buffer[begin], 8349db11cefSPeter Crosthwaite s->data_count - begin); 83549ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 83649ab747fSPaolo Bonzini if (s->data_count == block_size) { 83749ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 83840bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[n]); 83949ab747fSPaolo Bonzini } 84049ab747fSPaolo Bonzini s->data_count = 0; 84149ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 84249ab747fSPaolo Bonzini s->blkcnt--; 84349ab747fSPaolo Bonzini if (s->blkcnt == 0) { 84449ab747fSPaolo Bonzini break; 84549ab747fSPaolo Bonzini } 84649ab747fSPaolo Bonzini } 84749ab747fSPaolo Bonzini } 84849ab747fSPaolo Bonzini } 84949ab747fSPaolo Bonzini } 85049ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 85149ab747fSPaolo Bonzini break; 85249ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_LINK: /* link to next descriptor table */ 85349ab747fSPaolo Bonzini s->admasysaddr = dscr.addr; 8548be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma("link", s->admasysaddr); 85549ab747fSPaolo Bonzini break; 85649ab747fSPaolo Bonzini default: 85749ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 85849ab747fSPaolo Bonzini break; 85949ab747fSPaolo Bonzini } 86049ab747fSPaolo Bonzini 8611d32c26fSPeter Crosthwaite if (dscr.attr & SDHC_ADMA_ATTR_INT) { 8628be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma("interrupt", s->admasysaddr); 8631d32c26fSPeter Crosthwaite if (s->norintstsen & SDHC_NISEN_DMA) { 8641d32c26fSPeter Crosthwaite s->norintsts |= SDHC_NIS_DMA; 8651d32c26fSPeter Crosthwaite } 8661d32c26fSPeter Crosthwaite 8671d32c26fSPeter Crosthwaite sdhci_update_irq(s); 8681d32c26fSPeter Crosthwaite } 8691d32c26fSPeter Crosthwaite 87049ab747fSPaolo Bonzini /* ADMA transfer terminates if blkcnt == 0 or by END attribute */ 87149ab747fSPaolo Bonzini if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 87249ab747fSPaolo Bonzini (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) { 8738be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma_transfer_completed(); 87449ab747fSPaolo Bonzini if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) && 87549ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 87649ab747fSPaolo Bonzini s->blkcnt != 0)) { 8778be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("SD/MMC host ADMA length mismatch"); 87849ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH | 87949ab747fSPaolo Bonzini SDHC_ADMAERR_STATE_ST_TFR; 88049ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 8818be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Set ADMA error flag"); 88249ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 88349ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 88449ab747fSPaolo Bonzini } 88549ab747fSPaolo Bonzini 88649ab747fSPaolo Bonzini sdhci_update_irq(s); 88749ab747fSPaolo Bonzini } 888d368ba43SKevin O'Connor sdhci_end_transfer(s); 88949ab747fSPaolo Bonzini return; 89049ab747fSPaolo Bonzini } 89149ab747fSPaolo Bonzini 89249ab747fSPaolo Bonzini } 89349ab747fSPaolo Bonzini 89449ab747fSPaolo Bonzini /* we have unfinished business - reschedule to continue ADMA */ 895bc72ad67SAlex Bligh timer_mod(s->transfer_timer, 896bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY); 89749ab747fSPaolo Bonzini } 89849ab747fSPaolo Bonzini 89949ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */ 90049ab747fSPaolo Bonzini 901d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque) 90249ab747fSPaolo Bonzini { 903d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 90449ab747fSPaolo Bonzini 90549ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_DMA) { 90606c5120bSPhilippe Mathieu-Daudé switch (SDHC_DMA_TYPE(s->hostctl1)) { 90749ab747fSPaolo Bonzini case SDHC_CTRL_SDMA: 90849ab747fSPaolo Bonzini if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) { 909d368ba43SKevin O'Connor sdhci_sdma_transfer_single_block(s); 91049ab747fSPaolo Bonzini } else { 911d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 91249ab747fSPaolo Bonzini } 91349ab747fSPaolo Bonzini 91449ab747fSPaolo Bonzini break; 91549ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 9160540fba9SPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_ADMA1_MASK)) { 9178be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("ADMA1 not supported"); 91849ab747fSPaolo Bonzini break; 91949ab747fSPaolo Bonzini } 92049ab747fSPaolo Bonzini 921d368ba43SKevin O'Connor sdhci_do_adma(s); 92249ab747fSPaolo Bonzini break; 92349ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 9240540fba9SPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK)) { 9258be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("ADMA2 not supported"); 92649ab747fSPaolo Bonzini break; 92749ab747fSPaolo Bonzini } 92849ab747fSPaolo Bonzini 929d368ba43SKevin O'Connor sdhci_do_adma(s); 93049ab747fSPaolo Bonzini break; 93149ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 9320540fba9SPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK) || 9330540fba9SPhilippe Mathieu-Daudé !(s->capareg & R_SDHC_CAPAB_BUS64BIT_MASK)) { 9348be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("64 bit ADMA not supported"); 93549ab747fSPaolo Bonzini break; 93649ab747fSPaolo Bonzini } 93749ab747fSPaolo Bonzini 938d368ba43SKevin O'Connor sdhci_do_adma(s); 93949ab747fSPaolo Bonzini break; 94049ab747fSPaolo Bonzini default: 9418be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Unsupported DMA type"); 94249ab747fSPaolo Bonzini break; 94349ab747fSPaolo Bonzini } 94449ab747fSPaolo Bonzini } else { 94540bbc194SPeter Maydell if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) { 94649ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT | 94749ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 948d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 94949ab747fSPaolo Bonzini } else { 95049ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE | 95149ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT; 952d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 95349ab747fSPaolo Bonzini } 95449ab747fSPaolo Bonzini } 95549ab747fSPaolo Bonzini } 95649ab747fSPaolo Bonzini 95749ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s) 95849ab747fSPaolo Bonzini { 9596890a695SPeter Crosthwaite if (!SDHC_CLOCK_IS_ON(s->clkcon) || 96049ab747fSPaolo Bonzini (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) && 96149ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_DATA_PRESENT) || 96249ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY && 96349ab747fSPaolo Bonzini !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) { 96449ab747fSPaolo Bonzini return false; 96549ab747fSPaolo Bonzini } 96649ab747fSPaolo Bonzini 96749ab747fSPaolo Bonzini return true; 96849ab747fSPaolo Bonzini } 96949ab747fSPaolo Bonzini 97049ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and 97149ab747fSPaolo Bonzini * continuous manner */ 97249ab747fSPaolo Bonzini static inline bool 97349ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num) 97449ab747fSPaolo Bonzini { 97549ab747fSPaolo Bonzini if ((s->data_count & 0x3) != byte_num) { 9768be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Non-sequential access to Buffer Data Port register" 97749ab747fSPaolo Bonzini "is prohibited\n"); 97849ab747fSPaolo Bonzini return false; 97949ab747fSPaolo Bonzini } 98049ab747fSPaolo Bonzini return true; 98149ab747fSPaolo Bonzini } 98249ab747fSPaolo Bonzini 983d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size) 98449ab747fSPaolo Bonzini { 985d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 98649ab747fSPaolo Bonzini uint32_t ret = 0; 98749ab747fSPaolo Bonzini 98849ab747fSPaolo Bonzini switch (offset & ~0x3) { 98949ab747fSPaolo Bonzini case SDHC_SYSAD: 99049ab747fSPaolo Bonzini ret = s->sdmasysad; 99149ab747fSPaolo Bonzini break; 99249ab747fSPaolo Bonzini case SDHC_BLKSIZE: 99349ab747fSPaolo Bonzini ret = s->blksize | (s->blkcnt << 16); 99449ab747fSPaolo Bonzini break; 99549ab747fSPaolo Bonzini case SDHC_ARGUMENT: 99649ab747fSPaolo Bonzini ret = s->argument; 99749ab747fSPaolo Bonzini break; 99849ab747fSPaolo Bonzini case SDHC_TRNMOD: 99949ab747fSPaolo Bonzini ret = s->trnmod | (s->cmdreg << 16); 100049ab747fSPaolo Bonzini break; 100149ab747fSPaolo Bonzini case SDHC_RSPREG0 ... SDHC_RSPREG3: 100249ab747fSPaolo Bonzini ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2]; 100349ab747fSPaolo Bonzini break; 100449ab747fSPaolo Bonzini case SDHC_BDATA: 100549ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 1006d368ba43SKevin O'Connor ret = sdhci_read_dataport(s, size); 10078be487d8SPhilippe Mathieu-Daudé trace_sdhci_access("rd", size << 3, offset, "->", ret, ret); 100849ab747fSPaolo Bonzini return ret; 100949ab747fSPaolo Bonzini } 101049ab747fSPaolo Bonzini break; 101149ab747fSPaolo Bonzini case SDHC_PRNSTS: 101249ab747fSPaolo Bonzini ret = s->prnsts; 1013da346922SPhilippe Mathieu-Daudé ret = FIELD_DP32(ret, SDHC_PRNSTS, DAT_LVL, 1014da346922SPhilippe Mathieu-Daudé sdbus_get_dat_lines(&s->sdbus)); 1015da346922SPhilippe Mathieu-Daudé ret = FIELD_DP32(ret, SDHC_PRNSTS, CMD_LVL, 1016da346922SPhilippe Mathieu-Daudé sdbus_get_cmd_line(&s->sdbus)); 101749ab747fSPaolo Bonzini break; 101849ab747fSPaolo Bonzini case SDHC_HOSTCTL: 101906c5120bSPhilippe Mathieu-Daudé ret = s->hostctl1 | (s->pwrcon << 8) | (s->blkgap << 16) | 102049ab747fSPaolo Bonzini (s->wakcon << 24); 102149ab747fSPaolo Bonzini break; 102249ab747fSPaolo Bonzini case SDHC_CLKCON: 102349ab747fSPaolo Bonzini ret = s->clkcon | (s->timeoutcon << 16); 102449ab747fSPaolo Bonzini break; 102549ab747fSPaolo Bonzini case SDHC_NORINTSTS: 102649ab747fSPaolo Bonzini ret = s->norintsts | (s->errintsts << 16); 102749ab747fSPaolo Bonzini break; 102849ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 102949ab747fSPaolo Bonzini ret = s->norintstsen | (s->errintstsen << 16); 103049ab747fSPaolo Bonzini break; 103149ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 103249ab747fSPaolo Bonzini ret = s->norintsigen | (s->errintsigen << 16); 103349ab747fSPaolo Bonzini break; 103449ab747fSPaolo Bonzini case SDHC_ACMD12ERRSTS: 1035ea55a221SPhilippe Mathieu-Daudé ret = s->acmd12errsts | (s->hostctl2 << 16); 103649ab747fSPaolo Bonzini break; 1037cd209421SPhilippe Mathieu-Daudé case SDHC_CAPAB: 10385efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)s->capareg; 10395efc9016SPhilippe Mathieu-Daudé break; 10405efc9016SPhilippe Mathieu-Daudé case SDHC_CAPAB + 4: 10415efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)(s->capareg >> 32); 104249ab747fSPaolo Bonzini break; 104349ab747fSPaolo Bonzini case SDHC_MAXCURR: 10445efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)s->maxcurr; 10455efc9016SPhilippe Mathieu-Daudé break; 10465efc9016SPhilippe Mathieu-Daudé case SDHC_MAXCURR + 4: 10475efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)(s->maxcurr >> 32); 104849ab747fSPaolo Bonzini break; 104949ab747fSPaolo Bonzini case SDHC_ADMAERR: 105049ab747fSPaolo Bonzini ret = s->admaerr; 105149ab747fSPaolo Bonzini break; 105249ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 105349ab747fSPaolo Bonzini ret = (uint32_t)s->admasysaddr; 105449ab747fSPaolo Bonzini break; 105549ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 105649ab747fSPaolo Bonzini ret = (uint32_t)(s->admasysaddr >> 32); 105749ab747fSPaolo Bonzini break; 105849ab747fSPaolo Bonzini case SDHC_SLOT_INT_STATUS: 1059aceb5b06SPhilippe Mathieu-Daudé ret = (s->version << 16) | sdhci_slotint(s); 106049ab747fSPaolo Bonzini break; 106149ab747fSPaolo Bonzini default: 106200b004b3SPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "SDHC rd_%ub @0x%02" HWADDR_PRIx " " 106300b004b3SPhilippe Mathieu-Daudé "not implemented\n", size, offset); 106449ab747fSPaolo Bonzini break; 106549ab747fSPaolo Bonzini } 106649ab747fSPaolo Bonzini 106749ab747fSPaolo Bonzini ret >>= (offset & 0x3) * 8; 106849ab747fSPaolo Bonzini ret &= (1ULL << (size * 8)) - 1; 10698be487d8SPhilippe Mathieu-Daudé trace_sdhci_access("rd", size << 3, offset, "->", ret, ret); 107049ab747fSPaolo Bonzini return ret; 107149ab747fSPaolo Bonzini } 107249ab747fSPaolo Bonzini 107349ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value) 107449ab747fSPaolo Bonzini { 107549ab747fSPaolo Bonzini if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) { 107649ab747fSPaolo Bonzini return; 107749ab747fSPaolo Bonzini } 107849ab747fSPaolo Bonzini s->blkgap = value & SDHC_STOP_AT_GAP_REQ; 107949ab747fSPaolo Bonzini 108049ab747fSPaolo Bonzini if ((value & SDHC_CONTINUE_REQ) && s->stopped_state && 108149ab747fSPaolo Bonzini (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) { 108249ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read) { 108349ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ; 1084d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 108549ab747fSPaolo Bonzini } else { 108649ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE; 1087d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 108849ab747fSPaolo Bonzini } 108949ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 109049ab747fSPaolo Bonzini } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) { 109149ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_READ) { 109249ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_read; 109349ab747fSPaolo Bonzini } else if (s->prnsts & SDHC_DOING_WRITE) { 109449ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_write; 109549ab747fSPaolo Bonzini } 109649ab747fSPaolo Bonzini } 109749ab747fSPaolo Bonzini } 109849ab747fSPaolo Bonzini 109949ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value) 110049ab747fSPaolo Bonzini { 110149ab747fSPaolo Bonzini switch (value) { 110249ab747fSPaolo Bonzini case SDHC_RESET_ALL: 1103d368ba43SKevin O'Connor sdhci_reset(s); 110449ab747fSPaolo Bonzini break; 110549ab747fSPaolo Bonzini case SDHC_RESET_CMD: 110649ab747fSPaolo Bonzini s->prnsts &= ~SDHC_CMD_INHIBIT; 110749ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_CMDCMP; 110849ab747fSPaolo Bonzini break; 110949ab747fSPaolo Bonzini case SDHC_RESET_DATA: 111049ab747fSPaolo Bonzini s->data_count = 0; 111149ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE | 111249ab747fSPaolo Bonzini SDHC_DOING_READ | SDHC_DOING_WRITE | 111349ab747fSPaolo Bonzini SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE); 111449ab747fSPaolo Bonzini s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ); 111549ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 111649ab747fSPaolo Bonzini s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY | 111749ab747fSPaolo Bonzini SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP); 111849ab747fSPaolo Bonzini break; 111949ab747fSPaolo Bonzini } 112049ab747fSPaolo Bonzini } 112149ab747fSPaolo Bonzini 112249ab747fSPaolo Bonzini static void 1123d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) 112449ab747fSPaolo Bonzini { 1125d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 112649ab747fSPaolo Bonzini unsigned shift = 8 * (offset & 0x3); 112749ab747fSPaolo Bonzini uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift); 1128d368ba43SKevin O'Connor uint32_t value = val; 112949ab747fSPaolo Bonzini value <<= shift; 113049ab747fSPaolo Bonzini 113149ab747fSPaolo Bonzini switch (offset & ~0x3) { 113249ab747fSPaolo Bonzini case SDHC_SYSAD: 113349ab747fSPaolo Bonzini s->sdmasysad = (s->sdmasysad & mask) | value; 113449ab747fSPaolo Bonzini MASKED_WRITE(s->sdmasysad, mask, value); 113549ab747fSPaolo Bonzini /* Writing to last byte of sdmasysad might trigger transfer */ 113649ab747fSPaolo Bonzini if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt && 113706c5120bSPhilippe Mathieu-Daudé s->blksize && SDHC_DMA_TYPE(s->hostctl1) == SDHC_CTRL_SDMA) { 113845ba9f76SPrasad J Pandit if (s->trnmod & SDHC_TRNS_MULTI) { 1139d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 114045ba9f76SPrasad J Pandit } else { 114145ba9f76SPrasad J Pandit sdhci_sdma_transfer_single_block(s); 114245ba9f76SPrasad J Pandit } 114349ab747fSPaolo Bonzini } 114449ab747fSPaolo Bonzini break; 114549ab747fSPaolo Bonzini case SDHC_BLKSIZE: 114649ab747fSPaolo Bonzini if (!TRANSFERRING_DATA(s->prnsts)) { 114749ab747fSPaolo Bonzini MASKED_WRITE(s->blksize, mask, value); 114849ab747fSPaolo Bonzini MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16); 114949ab747fSPaolo Bonzini } 11509201bb9aSAlistair Francis 11519201bb9aSAlistair Francis /* Limit block size to the maximum buffer size */ 11529201bb9aSAlistair Francis if (extract32(s->blksize, 0, 12) > s->buf_maxsz) { 11539201bb9aSAlistair Francis qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than " \ 11549201bb9aSAlistair Francis "the maximum buffer 0x%x", __func__, s->blksize, 11559201bb9aSAlistair Francis s->buf_maxsz); 11569201bb9aSAlistair Francis 11579201bb9aSAlistair Francis s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz); 11589201bb9aSAlistair Francis } 11599201bb9aSAlistair Francis 116049ab747fSPaolo Bonzini break; 116149ab747fSPaolo Bonzini case SDHC_ARGUMENT: 116249ab747fSPaolo Bonzini MASKED_WRITE(s->argument, mask, value); 116349ab747fSPaolo Bonzini break; 116449ab747fSPaolo Bonzini case SDHC_TRNMOD: 116549ab747fSPaolo Bonzini /* DMA can be enabled only if it is supported as indicated by 116649ab747fSPaolo Bonzini * capabilities register */ 11676ff37c3dSPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_SDMA_MASK)) { 116849ab747fSPaolo Bonzini value &= ~SDHC_TRNS_DMA; 116949ab747fSPaolo Bonzini } 117024bddf9dSPhilippe Mathieu-Daudé MASKED_WRITE(s->trnmod, mask, value & SDHC_TRNMOD_MASK); 117149ab747fSPaolo Bonzini MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16); 117249ab747fSPaolo Bonzini 117349ab747fSPaolo Bonzini /* Writing to the upper byte of CMDREG triggers SD command generation */ 1174d368ba43SKevin O'Connor if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) { 117549ab747fSPaolo Bonzini break; 117649ab747fSPaolo Bonzini } 117749ab747fSPaolo Bonzini 1178d368ba43SKevin O'Connor sdhci_send_command(s); 117949ab747fSPaolo Bonzini break; 118049ab747fSPaolo Bonzini case SDHC_BDATA: 118149ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 1182d368ba43SKevin O'Connor sdhci_write_dataport(s, value >> shift, size); 118349ab747fSPaolo Bonzini } 118449ab747fSPaolo Bonzini break; 118549ab747fSPaolo Bonzini case SDHC_HOSTCTL: 118649ab747fSPaolo Bonzini if (!(mask & 0xFF0000)) { 118749ab747fSPaolo Bonzini sdhci_blkgap_write(s, value >> 16); 118849ab747fSPaolo Bonzini } 118906c5120bSPhilippe Mathieu-Daudé MASKED_WRITE(s->hostctl1, mask, value); 119049ab747fSPaolo Bonzini MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8); 119149ab747fSPaolo Bonzini MASKED_WRITE(s->wakcon, mask >> 24, value >> 24); 119249ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 || 119349ab747fSPaolo Bonzini !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) { 119449ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 119549ab747fSPaolo Bonzini } 119649ab747fSPaolo Bonzini break; 119749ab747fSPaolo Bonzini case SDHC_CLKCON: 119849ab747fSPaolo Bonzini if (!(mask & 0xFF000000)) { 119949ab747fSPaolo Bonzini sdhci_reset_write(s, value >> 24); 120049ab747fSPaolo Bonzini } 120149ab747fSPaolo Bonzini MASKED_WRITE(s->clkcon, mask, value); 120249ab747fSPaolo Bonzini MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16); 120349ab747fSPaolo Bonzini if (s->clkcon & SDHC_CLOCK_INT_EN) { 120449ab747fSPaolo Bonzini s->clkcon |= SDHC_CLOCK_INT_STABLE; 120549ab747fSPaolo Bonzini } else { 120649ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_INT_STABLE; 120749ab747fSPaolo Bonzini } 120849ab747fSPaolo Bonzini break; 120949ab747fSPaolo Bonzini case SDHC_NORINTSTS: 121049ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CARDINT) { 121149ab747fSPaolo Bonzini value &= ~SDHC_NIS_CARDINT; 121249ab747fSPaolo Bonzini } 121349ab747fSPaolo Bonzini s->norintsts &= mask | ~value; 121449ab747fSPaolo Bonzini s->errintsts &= (mask >> 16) | ~(value >> 16); 121549ab747fSPaolo Bonzini if (s->errintsts) { 121649ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 121749ab747fSPaolo Bonzini } else { 121849ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 121949ab747fSPaolo Bonzini } 122049ab747fSPaolo Bonzini sdhci_update_irq(s); 122149ab747fSPaolo Bonzini break; 122249ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 122349ab747fSPaolo Bonzini MASKED_WRITE(s->norintstsen, mask, value); 122449ab747fSPaolo Bonzini MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16); 122549ab747fSPaolo Bonzini s->norintsts &= s->norintstsen; 122649ab747fSPaolo Bonzini s->errintsts &= s->errintstsen; 122749ab747fSPaolo Bonzini if (s->errintsts) { 122849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 122949ab747fSPaolo Bonzini } else { 123049ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 123149ab747fSPaolo Bonzini } 12320a7ac9f9SAndrew Baumann /* Quirk for Raspberry Pi: pending card insert interrupt 12330a7ac9f9SAndrew Baumann * appears when first enabled after power on */ 12340a7ac9f9SAndrew Baumann if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) { 12350a7ac9f9SAndrew Baumann assert(s->pending_insert_quirk); 12360a7ac9f9SAndrew Baumann s->norintsts |= SDHC_NIS_INSERT; 12370a7ac9f9SAndrew Baumann s->pending_insert_state = false; 12380a7ac9f9SAndrew Baumann } 123949ab747fSPaolo Bonzini sdhci_update_irq(s); 124049ab747fSPaolo Bonzini break; 124149ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 124249ab747fSPaolo Bonzini MASKED_WRITE(s->norintsigen, mask, value); 124349ab747fSPaolo Bonzini MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16); 124449ab747fSPaolo Bonzini sdhci_update_irq(s); 124549ab747fSPaolo Bonzini break; 124649ab747fSPaolo Bonzini case SDHC_ADMAERR: 124749ab747fSPaolo Bonzini MASKED_WRITE(s->admaerr, mask, value); 124849ab747fSPaolo Bonzini break; 124949ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 125049ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL | 125149ab747fSPaolo Bonzini (uint64_t)mask)) | (uint64_t)value; 125249ab747fSPaolo Bonzini break; 125349ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 125449ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL | 125549ab747fSPaolo Bonzini ((uint64_t)mask << 32))) | ((uint64_t)value << 32); 125649ab747fSPaolo Bonzini break; 125749ab747fSPaolo Bonzini case SDHC_FEAER: 125849ab747fSPaolo Bonzini s->acmd12errsts |= value; 125949ab747fSPaolo Bonzini s->errintsts |= (value >> 16) & s->errintstsen; 126049ab747fSPaolo Bonzini if (s->acmd12errsts) { 126149ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMD12ERR; 126249ab747fSPaolo Bonzini } 126349ab747fSPaolo Bonzini if (s->errintsts) { 126449ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 126549ab747fSPaolo Bonzini } 126649ab747fSPaolo Bonzini sdhci_update_irq(s); 126749ab747fSPaolo Bonzini break; 12685d2c0464SAndrey Smirnov case SDHC_ACMD12ERRSTS: 12690034ebe6SPhilippe Mathieu-Daudé MASKED_WRITE(s->acmd12errsts, mask, value & UINT16_MAX); 12700034ebe6SPhilippe Mathieu-Daudé if (s->uhs_mode >= UHS_I) { 12710034ebe6SPhilippe Mathieu-Daudé MASKED_WRITE(s->hostctl2, mask >> 16, value >> 16); 12720034ebe6SPhilippe Mathieu-Daudé 12730034ebe6SPhilippe Mathieu-Daudé if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, V18_ENA)) { 12740034ebe6SPhilippe Mathieu-Daudé sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_1_8V); 12750034ebe6SPhilippe Mathieu-Daudé } else { 12760034ebe6SPhilippe Mathieu-Daudé sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_3_3V); 12770034ebe6SPhilippe Mathieu-Daudé } 12780034ebe6SPhilippe Mathieu-Daudé } 12795d2c0464SAndrey Smirnov break; 12805efc9016SPhilippe Mathieu-Daudé 12815efc9016SPhilippe Mathieu-Daudé case SDHC_CAPAB: 12825efc9016SPhilippe Mathieu-Daudé case SDHC_CAPAB + 4: 12835efc9016SPhilippe Mathieu-Daudé case SDHC_MAXCURR: 12845efc9016SPhilippe Mathieu-Daudé case SDHC_MAXCURR + 4: 12855efc9016SPhilippe Mathieu-Daudé qemu_log_mask(LOG_GUEST_ERROR, "SDHC wr_%ub @0x%02" HWADDR_PRIx 12865efc9016SPhilippe Mathieu-Daudé " <- 0x%08x read-only\n", size, offset, value >> shift); 12875efc9016SPhilippe Mathieu-Daudé break; 12885efc9016SPhilippe Mathieu-Daudé 128949ab747fSPaolo Bonzini default: 129000b004b3SPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "SDHC wr_%ub @0x%02" HWADDR_PRIx " <- 0x%08x " 129100b004b3SPhilippe Mathieu-Daudé "not implemented\n", size, offset, value >> shift); 129249ab747fSPaolo Bonzini break; 129349ab747fSPaolo Bonzini } 12948be487d8SPhilippe Mathieu-Daudé trace_sdhci_access("wr", size << 3, offset, "<-", 12958be487d8SPhilippe Mathieu-Daudé value >> shift, value >> shift); 129649ab747fSPaolo Bonzini } 129749ab747fSPaolo Bonzini 129849ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = { 1299d368ba43SKevin O'Connor .read = sdhci_read, 1300d368ba43SKevin O'Connor .write = sdhci_write, 130149ab747fSPaolo Bonzini .valid = { 130249ab747fSPaolo Bonzini .min_access_size = 1, 130349ab747fSPaolo Bonzini .max_access_size = 4, 130449ab747fSPaolo Bonzini .unaligned = false 130549ab747fSPaolo Bonzini }, 130649ab747fSPaolo Bonzini .endianness = DEVICE_LITTLE_ENDIAN, 130749ab747fSPaolo Bonzini }; 130849ab747fSPaolo Bonzini 1309aceb5b06SPhilippe Mathieu-Daudé static void sdhci_init_readonly_registers(SDHCIState *s, Error **errp) 1310aceb5b06SPhilippe Mathieu-Daudé { 13116ff37c3dSPhilippe Mathieu-Daudé Error *local_err = NULL; 13126ff37c3dSPhilippe Mathieu-Daudé 13134d67852dSPhilippe Mathieu-Daudé switch (s->sd_spec_version) { 13144d67852dSPhilippe Mathieu-Daudé case 2 ... 3: 13154d67852dSPhilippe Mathieu-Daudé break; 13164d67852dSPhilippe Mathieu-Daudé default: 13174d67852dSPhilippe Mathieu-Daudé error_setg(errp, "Only Spec v2/v3 are supported"); 1318aceb5b06SPhilippe Mathieu-Daudé return; 1319aceb5b06SPhilippe Mathieu-Daudé } 1320aceb5b06SPhilippe Mathieu-Daudé s->version = (SDHC_HCVER_VENDOR << 8) | (s->sd_spec_version - 1); 13216ff37c3dSPhilippe Mathieu-Daudé 13226ff37c3dSPhilippe Mathieu-Daudé sdhci_check_capareg(s, &local_err); 13236ff37c3dSPhilippe Mathieu-Daudé if (local_err) { 13246ff37c3dSPhilippe Mathieu-Daudé error_propagate(errp, local_err); 13256ff37c3dSPhilippe Mathieu-Daudé return; 13266ff37c3dSPhilippe Mathieu-Daudé } 1327aceb5b06SPhilippe Mathieu-Daudé } 1328aceb5b06SPhilippe Mathieu-Daudé 1329b635d98cSPhilippe Mathieu-Daudé /* --- qdev common --- */ 1330b635d98cSPhilippe Mathieu-Daudé 1331b635d98cSPhilippe Mathieu-Daudé #define DEFINE_SDHCI_COMMON_PROPERTIES(_state) \ 1332aceb5b06SPhilippe Mathieu-Daudé DEFINE_PROP_UINT8("sd-spec-version", _state, sd_spec_version, 2), \ 13330034ebe6SPhilippe Mathieu-Daudé DEFINE_PROP_UINT8("uhs", _state, uhs_mode, UHS_NOT_SUPPORTED), \ 1334aceb5b06SPhilippe Mathieu-Daudé \ 1335aceb5b06SPhilippe Mathieu-Daudé /* Capabilities registers provide information on supported 1336aceb5b06SPhilippe Mathieu-Daudé * features of this specific host controller implementation */ \ 13375efc9016SPhilippe Mathieu-Daudé DEFINE_PROP_UINT64("capareg", _state, capareg, SDHC_CAPAB_REG_DEFAULT), \ 13385efc9016SPhilippe Mathieu-Daudé DEFINE_PROP_UINT64("maxcurr", _state, maxcurr, 0) 1339b635d98cSPhilippe Mathieu-Daudé 134040bbc194SPeter Maydell static void sdhci_initfn(SDHCIState *s) 134149ab747fSPaolo Bonzini { 134240bbc194SPeter Maydell qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), 134340bbc194SPeter Maydell TYPE_SDHCI_BUS, DEVICE(s), "sd-bus"); 134449ab747fSPaolo Bonzini 1345bc72ad67SAlex Bligh s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s); 1346d368ba43SKevin O'Connor s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s); 1347fd1e5c81SAndrey Smirnov 1348fd1e5c81SAndrey Smirnov s->io_ops = &sdhci_mmio_ops; 134949ab747fSPaolo Bonzini } 135049ab747fSPaolo Bonzini 13517302dcd6SKevin O'Connor static void sdhci_uninitfn(SDHCIState *s) 135249ab747fSPaolo Bonzini { 1353bc72ad67SAlex Bligh timer_del(s->insert_timer); 1354bc72ad67SAlex Bligh timer_free(s->insert_timer); 1355bc72ad67SAlex Bligh timer_del(s->transfer_timer); 1356bc72ad67SAlex Bligh timer_free(s->transfer_timer); 135749ab747fSPaolo Bonzini 135849ab747fSPaolo Bonzini g_free(s->fifo_buffer); 135949ab747fSPaolo Bonzini s->fifo_buffer = NULL; 136049ab747fSPaolo Bonzini } 136149ab747fSPaolo Bonzini 136225367498SPhilippe Mathieu-Daudé static void sdhci_common_realize(SDHCIState *s, Error **errp) 136325367498SPhilippe Mathieu-Daudé { 1364aceb5b06SPhilippe Mathieu-Daudé Error *local_err = NULL; 1365aceb5b06SPhilippe Mathieu-Daudé 1366aceb5b06SPhilippe Mathieu-Daudé sdhci_init_readonly_registers(s, &local_err); 1367aceb5b06SPhilippe Mathieu-Daudé if (local_err) { 1368aceb5b06SPhilippe Mathieu-Daudé error_propagate(errp, local_err); 1369aceb5b06SPhilippe Mathieu-Daudé return; 1370aceb5b06SPhilippe Mathieu-Daudé } 137125367498SPhilippe Mathieu-Daudé s->buf_maxsz = sdhci_get_fifolen(s); 137225367498SPhilippe Mathieu-Daudé s->fifo_buffer = g_malloc0(s->buf_maxsz); 137325367498SPhilippe Mathieu-Daudé 137425367498SPhilippe Mathieu-Daudé memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci", 137525367498SPhilippe Mathieu-Daudé SDHC_REGISTERS_MAP_SIZE); 137625367498SPhilippe Mathieu-Daudé } 137725367498SPhilippe Mathieu-Daudé 13788b7455c7SPhilippe Mathieu-Daudé static void sdhci_common_unrealize(SDHCIState *s, Error **errp) 13798b7455c7SPhilippe Mathieu-Daudé { 13808b7455c7SPhilippe Mathieu-Daudé /* This function is expected to be called only once for each class: 13818b7455c7SPhilippe Mathieu-Daudé * - SysBus: via DeviceClass->unrealize(), 13828b7455c7SPhilippe Mathieu-Daudé * - PCI: via PCIDeviceClass->exit(). 13838b7455c7SPhilippe Mathieu-Daudé * However to avoid double-free and/or use-after-free we still nullify 13848b7455c7SPhilippe Mathieu-Daudé * this variable (better safe than sorry!). */ 13858b7455c7SPhilippe Mathieu-Daudé g_free(s->fifo_buffer); 13868b7455c7SPhilippe Mathieu-Daudé s->fifo_buffer = NULL; 13878b7455c7SPhilippe Mathieu-Daudé } 13888b7455c7SPhilippe Mathieu-Daudé 13890a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque) 13900a7ac9f9SAndrew Baumann { 13910a7ac9f9SAndrew Baumann SDHCIState *s = opaque; 13920a7ac9f9SAndrew Baumann 13930a7ac9f9SAndrew Baumann return s->pending_insert_state; 13940a7ac9f9SAndrew Baumann } 13950a7ac9f9SAndrew Baumann 13960a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = { 13970a7ac9f9SAndrew Baumann .name = "sdhci/pending-insert", 13980a7ac9f9SAndrew Baumann .version_id = 1, 13990a7ac9f9SAndrew Baumann .minimum_version_id = 1, 14000a7ac9f9SAndrew Baumann .needed = sdhci_pending_insert_vmstate_needed, 14010a7ac9f9SAndrew Baumann .fields = (VMStateField[]) { 14020a7ac9f9SAndrew Baumann VMSTATE_BOOL(pending_insert_state, SDHCIState), 14030a7ac9f9SAndrew Baumann VMSTATE_END_OF_LIST() 14040a7ac9f9SAndrew Baumann }, 14050a7ac9f9SAndrew Baumann }; 14060a7ac9f9SAndrew Baumann 140749ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = { 140849ab747fSPaolo Bonzini .name = "sdhci", 140949ab747fSPaolo Bonzini .version_id = 1, 141049ab747fSPaolo Bonzini .minimum_version_id = 1, 141149ab747fSPaolo Bonzini .fields = (VMStateField[]) { 141249ab747fSPaolo Bonzini VMSTATE_UINT32(sdmasysad, SDHCIState), 141349ab747fSPaolo Bonzini VMSTATE_UINT16(blksize, SDHCIState), 141449ab747fSPaolo Bonzini VMSTATE_UINT16(blkcnt, SDHCIState), 141549ab747fSPaolo Bonzini VMSTATE_UINT32(argument, SDHCIState), 141649ab747fSPaolo Bonzini VMSTATE_UINT16(trnmod, SDHCIState), 141749ab747fSPaolo Bonzini VMSTATE_UINT16(cmdreg, SDHCIState), 141849ab747fSPaolo Bonzini VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4), 141949ab747fSPaolo Bonzini VMSTATE_UINT32(prnsts, SDHCIState), 142006c5120bSPhilippe Mathieu-Daudé VMSTATE_UINT8(hostctl1, SDHCIState), 142149ab747fSPaolo Bonzini VMSTATE_UINT8(pwrcon, SDHCIState), 142249ab747fSPaolo Bonzini VMSTATE_UINT8(blkgap, SDHCIState), 142349ab747fSPaolo Bonzini VMSTATE_UINT8(wakcon, SDHCIState), 142449ab747fSPaolo Bonzini VMSTATE_UINT16(clkcon, SDHCIState), 142549ab747fSPaolo Bonzini VMSTATE_UINT8(timeoutcon, SDHCIState), 142649ab747fSPaolo Bonzini VMSTATE_UINT8(admaerr, SDHCIState), 142749ab747fSPaolo Bonzini VMSTATE_UINT16(norintsts, SDHCIState), 142849ab747fSPaolo Bonzini VMSTATE_UINT16(errintsts, SDHCIState), 142949ab747fSPaolo Bonzini VMSTATE_UINT16(norintstsen, SDHCIState), 143049ab747fSPaolo Bonzini VMSTATE_UINT16(errintstsen, SDHCIState), 143149ab747fSPaolo Bonzini VMSTATE_UINT16(norintsigen, SDHCIState), 143249ab747fSPaolo Bonzini VMSTATE_UINT16(errintsigen, SDHCIState), 143349ab747fSPaolo Bonzini VMSTATE_UINT16(acmd12errsts, SDHCIState), 143449ab747fSPaolo Bonzini VMSTATE_UINT16(data_count, SDHCIState), 143549ab747fSPaolo Bonzini VMSTATE_UINT64(admasysaddr, SDHCIState), 143649ab747fSPaolo Bonzini VMSTATE_UINT8(stopped_state, SDHCIState), 143759046ec2SHalil Pasic VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz), 1438e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(insert_timer, SDHCIState), 1439e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(transfer_timer, SDHCIState), 144049ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 14410a7ac9f9SAndrew Baumann }, 14420a7ac9f9SAndrew Baumann .subsections = (const VMStateDescription*[]) { 14430a7ac9f9SAndrew Baumann &sdhci_pending_insert_vmstate, 14440a7ac9f9SAndrew Baumann NULL 14450a7ac9f9SAndrew Baumann }, 144649ab747fSPaolo Bonzini }; 144749ab747fSPaolo Bonzini 14481c92c505SPhilippe Mathieu-Daudé static void sdhci_common_class_init(ObjectClass *klass, void *data) 14491c92c505SPhilippe Mathieu-Daudé { 14501c92c505SPhilippe Mathieu-Daudé DeviceClass *dc = DEVICE_CLASS(klass); 14511c92c505SPhilippe Mathieu-Daudé 14521c92c505SPhilippe Mathieu-Daudé set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 14531c92c505SPhilippe Mathieu-Daudé dc->vmsd = &sdhci_vmstate; 14541c92c505SPhilippe Mathieu-Daudé dc->reset = sdhci_poweron_reset; 14551c92c505SPhilippe Mathieu-Daudé } 14561c92c505SPhilippe Mathieu-Daudé 1457b635d98cSPhilippe Mathieu-Daudé /* --- qdev PCI --- */ 1458b635d98cSPhilippe Mathieu-Daudé 14595ec911c3SKevin O'Connor static Property sdhci_pci_properties[] = { 1460b635d98cSPhilippe Mathieu-Daudé DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState), 146149ab747fSPaolo Bonzini DEFINE_PROP_END_OF_LIST(), 146249ab747fSPaolo Bonzini }; 146349ab747fSPaolo Bonzini 14649af21dbeSMarkus Armbruster static void sdhci_pci_realize(PCIDevice *dev, Error **errp) 1465224d10ffSKevin O'Connor { 1466224d10ffSKevin O'Connor SDHCIState *s = PCI_SDHCI(dev); 1467ab958e38SPhilippe Mathieu-Daudé Error *local_err = NULL; 146825367498SPhilippe Mathieu-Daudé 146925367498SPhilippe Mathieu-Daudé sdhci_initfn(s); 1470544156efSPaolo Bonzini sdhci_common_realize(s, &local_err); 1471ab958e38SPhilippe Mathieu-Daudé if (local_err) { 1472ab958e38SPhilippe Mathieu-Daudé error_propagate(errp, local_err); 147325367498SPhilippe Mathieu-Daudé return; 147425367498SPhilippe Mathieu-Daudé } 147525367498SPhilippe Mathieu-Daudé 1476224d10ffSKevin O'Connor dev->config[PCI_CLASS_PROG] = 0x01; /* Standard Host supported DMA */ 1477224d10ffSKevin O'Connor dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */ 1478224d10ffSKevin O'Connor s->irq = pci_allocate_irq(dev); 1479dd55c485SPhilippe Mathieu-Daudé s->dma_as = pci_get_address_space(dev); 1480dd55c485SPhilippe Mathieu-Daudé pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->iomem); 1481224d10ffSKevin O'Connor } 1482224d10ffSKevin O'Connor 1483224d10ffSKevin O'Connor static void sdhci_pci_exit(PCIDevice *dev) 1484224d10ffSKevin O'Connor { 1485224d10ffSKevin O'Connor SDHCIState *s = PCI_SDHCI(dev); 14868b7455c7SPhilippe Mathieu-Daudé 14878b7455c7SPhilippe Mathieu-Daudé sdhci_common_unrealize(s, &error_abort); 1488224d10ffSKevin O'Connor sdhci_uninitfn(s); 1489224d10ffSKevin O'Connor } 1490224d10ffSKevin O'Connor 1491224d10ffSKevin O'Connor static void sdhci_pci_class_init(ObjectClass *klass, void *data) 1492224d10ffSKevin O'Connor { 1493224d10ffSKevin O'Connor DeviceClass *dc = DEVICE_CLASS(klass); 1494224d10ffSKevin O'Connor PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1495224d10ffSKevin O'Connor 14969af21dbeSMarkus Armbruster k->realize = sdhci_pci_realize; 1497224d10ffSKevin O'Connor k->exit = sdhci_pci_exit; 1498224d10ffSKevin O'Connor k->vendor_id = PCI_VENDOR_ID_REDHAT; 1499224d10ffSKevin O'Connor k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI; 1500224d10ffSKevin O'Connor k->class_id = PCI_CLASS_SYSTEM_SDHCI; 15015ec911c3SKevin O'Connor dc->props = sdhci_pci_properties; 15021c92c505SPhilippe Mathieu-Daudé 15031c92c505SPhilippe Mathieu-Daudé sdhci_common_class_init(klass, data); 1504224d10ffSKevin O'Connor } 1505224d10ffSKevin O'Connor 1506224d10ffSKevin O'Connor static const TypeInfo sdhci_pci_info = { 1507224d10ffSKevin O'Connor .name = TYPE_PCI_SDHCI, 1508224d10ffSKevin O'Connor .parent = TYPE_PCI_DEVICE, 1509224d10ffSKevin O'Connor .instance_size = sizeof(SDHCIState), 1510224d10ffSKevin O'Connor .class_init = sdhci_pci_class_init, 1511fd3b02c8SEduardo Habkost .interfaces = (InterfaceInfo[]) { 1512fd3b02c8SEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 1513fd3b02c8SEduardo Habkost { }, 1514fd3b02c8SEduardo Habkost }, 1515224d10ffSKevin O'Connor }; 1516224d10ffSKevin O'Connor 1517b635d98cSPhilippe Mathieu-Daudé /* --- qdev SysBus --- */ 1518b635d98cSPhilippe Mathieu-Daudé 15195ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = { 1520b635d98cSPhilippe Mathieu-Daudé DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState), 15210a7ac9f9SAndrew Baumann DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk, 15220a7ac9f9SAndrew Baumann false), 152360765b6cSPhilippe Mathieu-Daudé DEFINE_PROP_LINK("dma", SDHCIState, 152460765b6cSPhilippe Mathieu-Daudé dma_mr, TYPE_MEMORY_REGION, MemoryRegion *), 15255ec911c3SKevin O'Connor DEFINE_PROP_END_OF_LIST(), 15265ec911c3SKevin O'Connor }; 15275ec911c3SKevin O'Connor 15287302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj) 152949ab747fSPaolo Bonzini { 15307302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 15315ec911c3SKevin O'Connor 153240bbc194SPeter Maydell sdhci_initfn(s); 15337302dcd6SKevin O'Connor } 15347302dcd6SKevin O'Connor 15357302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj) 15367302dcd6SKevin O'Connor { 15377302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 153860765b6cSPhilippe Mathieu-Daudé 153960765b6cSPhilippe Mathieu-Daudé if (s->dma_mr) { 154060765b6cSPhilippe Mathieu-Daudé object_unparent(OBJECT(s->dma_mr)); 154160765b6cSPhilippe Mathieu-Daudé } 154260765b6cSPhilippe Mathieu-Daudé 15437302dcd6SKevin O'Connor sdhci_uninitfn(s); 15447302dcd6SKevin O'Connor } 15457302dcd6SKevin O'Connor 15467302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp) 15477302dcd6SKevin O'Connor { 15487302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(dev); 154949ab747fSPaolo Bonzini SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 1550ab958e38SPhilippe Mathieu-Daudé Error *local_err = NULL; 155149ab747fSPaolo Bonzini 1552544156efSPaolo Bonzini sdhci_common_realize(s, &local_err); 1553ab958e38SPhilippe Mathieu-Daudé if (local_err) { 1554ab958e38SPhilippe Mathieu-Daudé error_propagate(errp, local_err); 155525367498SPhilippe Mathieu-Daudé return; 155625367498SPhilippe Mathieu-Daudé } 155725367498SPhilippe Mathieu-Daudé 155860765b6cSPhilippe Mathieu-Daudé if (s->dma_mr) { 155902e57e1cSPhilippe Mathieu-Daudé s->dma_as = &s->sysbus_dma_as; 156060765b6cSPhilippe Mathieu-Daudé address_space_init(s->dma_as, s->dma_mr, "sdhci-dma"); 156160765b6cSPhilippe Mathieu-Daudé } else { 156260765b6cSPhilippe Mathieu-Daudé /* use system_memory() if property "dma" not set */ 1563dd55c485SPhilippe Mathieu-Daudé s->dma_as = &address_space_memory; 156460765b6cSPhilippe Mathieu-Daudé } 1565dd55c485SPhilippe Mathieu-Daudé 156649ab747fSPaolo Bonzini sysbus_init_irq(sbd, &s->irq); 1567fd1e5c81SAndrey Smirnov 1568fd1e5c81SAndrey Smirnov memory_region_init_io(&s->iomem, OBJECT(s), s->io_ops, s, "sdhci", 1569fd1e5c81SAndrey Smirnov SDHC_REGISTERS_MAP_SIZE); 1570fd1e5c81SAndrey Smirnov 157149ab747fSPaolo Bonzini sysbus_init_mmio(sbd, &s->iomem); 157249ab747fSPaolo Bonzini } 157349ab747fSPaolo Bonzini 15748b7455c7SPhilippe Mathieu-Daudé static void sdhci_sysbus_unrealize(DeviceState *dev, Error **errp) 15758b7455c7SPhilippe Mathieu-Daudé { 15768b7455c7SPhilippe Mathieu-Daudé SDHCIState *s = SYSBUS_SDHCI(dev); 15778b7455c7SPhilippe Mathieu-Daudé 15788b7455c7SPhilippe Mathieu-Daudé sdhci_common_unrealize(s, &error_abort); 157960765b6cSPhilippe Mathieu-Daudé 158060765b6cSPhilippe Mathieu-Daudé if (s->dma_mr) { 158160765b6cSPhilippe Mathieu-Daudé address_space_destroy(s->dma_as); 158260765b6cSPhilippe Mathieu-Daudé } 15838b7455c7SPhilippe Mathieu-Daudé } 15848b7455c7SPhilippe Mathieu-Daudé 15857302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data) 158649ab747fSPaolo Bonzini { 158749ab747fSPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass); 158849ab747fSPaolo Bonzini 15895ec911c3SKevin O'Connor dc->props = sdhci_sysbus_properties; 15907302dcd6SKevin O'Connor dc->realize = sdhci_sysbus_realize; 15918b7455c7SPhilippe Mathieu-Daudé dc->unrealize = sdhci_sysbus_unrealize; 15921c92c505SPhilippe Mathieu-Daudé 15931c92c505SPhilippe Mathieu-Daudé sdhci_common_class_init(klass, data); 159449ab747fSPaolo Bonzini } 159549ab747fSPaolo Bonzini 15967302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = { 15977302dcd6SKevin O'Connor .name = TYPE_SYSBUS_SDHCI, 159849ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 159949ab747fSPaolo Bonzini .instance_size = sizeof(SDHCIState), 16007302dcd6SKevin O'Connor .instance_init = sdhci_sysbus_init, 16017302dcd6SKevin O'Connor .instance_finalize = sdhci_sysbus_finalize, 16027302dcd6SKevin O'Connor .class_init = sdhci_sysbus_class_init, 160349ab747fSPaolo Bonzini }; 160449ab747fSPaolo Bonzini 1605b635d98cSPhilippe Mathieu-Daudé /* --- qdev bus master --- */ 1606b635d98cSPhilippe Mathieu-Daudé 160740bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data) 160840bbc194SPeter Maydell { 160940bbc194SPeter Maydell SDBusClass *sbc = SD_BUS_CLASS(klass); 161040bbc194SPeter Maydell 161140bbc194SPeter Maydell sbc->set_inserted = sdhci_set_inserted; 161240bbc194SPeter Maydell sbc->set_readonly = sdhci_set_readonly; 161340bbc194SPeter Maydell } 161440bbc194SPeter Maydell 161540bbc194SPeter Maydell static const TypeInfo sdhci_bus_info = { 161640bbc194SPeter Maydell .name = TYPE_SDHCI_BUS, 161740bbc194SPeter Maydell .parent = TYPE_SD_BUS, 161840bbc194SPeter Maydell .instance_size = sizeof(SDBus), 161940bbc194SPeter Maydell .class_init = sdhci_bus_class_init, 162040bbc194SPeter Maydell }; 162140bbc194SPeter Maydell 1622fd1e5c81SAndrey Smirnov static uint64_t usdhc_read(void *opaque, hwaddr offset, unsigned size) 1623fd1e5c81SAndrey Smirnov { 1624fd1e5c81SAndrey Smirnov SDHCIState *s = SYSBUS_SDHCI(opaque); 1625fd1e5c81SAndrey Smirnov uint32_t ret; 162606c5120bSPhilippe Mathieu-Daudé uint16_t hostctl1; 1627fd1e5c81SAndrey Smirnov 1628fd1e5c81SAndrey Smirnov switch (offset) { 1629fd1e5c81SAndrey Smirnov default: 1630fd1e5c81SAndrey Smirnov return sdhci_read(opaque, offset, size); 1631fd1e5c81SAndrey Smirnov 1632fd1e5c81SAndrey Smirnov case SDHC_HOSTCTL: 1633fd1e5c81SAndrey Smirnov /* 1634fd1e5c81SAndrey Smirnov * For a detailed explanation on the following bit 1635fd1e5c81SAndrey Smirnov * manipulation code see comments in a similar part of 1636fd1e5c81SAndrey Smirnov * usdhc_write() 1637fd1e5c81SAndrey Smirnov */ 163806c5120bSPhilippe Mathieu-Daudé hostctl1 = SDHC_DMA_TYPE(s->hostctl1) << (8 - 3); 1639fd1e5c81SAndrey Smirnov 164006c5120bSPhilippe Mathieu-Daudé if (s->hostctl1 & SDHC_CTRL_8BITBUS) { 164106c5120bSPhilippe Mathieu-Daudé hostctl1 |= ESDHC_CTRL_8BITBUS; 1642fd1e5c81SAndrey Smirnov } 1643fd1e5c81SAndrey Smirnov 164406c5120bSPhilippe Mathieu-Daudé if (s->hostctl1 & SDHC_CTRL_4BITBUS) { 164506c5120bSPhilippe Mathieu-Daudé hostctl1 |= ESDHC_CTRL_4BITBUS; 1646fd1e5c81SAndrey Smirnov } 1647fd1e5c81SAndrey Smirnov 164806c5120bSPhilippe Mathieu-Daudé ret = hostctl1; 1649fd1e5c81SAndrey Smirnov ret |= (uint32_t)s->blkgap << 16; 1650fd1e5c81SAndrey Smirnov ret |= (uint32_t)s->wakcon << 24; 1651fd1e5c81SAndrey Smirnov 1652fd1e5c81SAndrey Smirnov break; 1653fd1e5c81SAndrey Smirnov 1654fd1e5c81SAndrey Smirnov case ESDHC_DLL_CTRL: 1655fd1e5c81SAndrey Smirnov case ESDHC_TUNE_CTRL_STATUS: 1656fd1e5c81SAndrey Smirnov case ESDHC_UNDOCUMENTED_REG27: 1657fd1e5c81SAndrey Smirnov case ESDHC_TUNING_CTRL: 1658fd1e5c81SAndrey Smirnov case ESDHC_VENDOR_SPEC: 1659fd1e5c81SAndrey Smirnov case ESDHC_MIX_CTRL: 1660fd1e5c81SAndrey Smirnov case ESDHC_WTMK_LVL: 1661fd1e5c81SAndrey Smirnov ret = 0; 1662fd1e5c81SAndrey Smirnov break; 1663fd1e5c81SAndrey Smirnov } 1664fd1e5c81SAndrey Smirnov 1665fd1e5c81SAndrey Smirnov return ret; 1666fd1e5c81SAndrey Smirnov } 1667fd1e5c81SAndrey Smirnov 1668fd1e5c81SAndrey Smirnov static void 1669fd1e5c81SAndrey Smirnov usdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) 1670fd1e5c81SAndrey Smirnov { 1671fd1e5c81SAndrey Smirnov SDHCIState *s = SYSBUS_SDHCI(opaque); 167206c5120bSPhilippe Mathieu-Daudé uint8_t hostctl1; 1673fd1e5c81SAndrey Smirnov uint32_t value = (uint32_t)val; 1674fd1e5c81SAndrey Smirnov 1675fd1e5c81SAndrey Smirnov switch (offset) { 1676fd1e5c81SAndrey Smirnov case ESDHC_DLL_CTRL: 1677fd1e5c81SAndrey Smirnov case ESDHC_TUNE_CTRL_STATUS: 1678fd1e5c81SAndrey Smirnov case ESDHC_UNDOCUMENTED_REG27: 1679fd1e5c81SAndrey Smirnov case ESDHC_TUNING_CTRL: 1680fd1e5c81SAndrey Smirnov case ESDHC_WTMK_LVL: 1681fd1e5c81SAndrey Smirnov case ESDHC_VENDOR_SPEC: 1682fd1e5c81SAndrey Smirnov break; 1683fd1e5c81SAndrey Smirnov 1684fd1e5c81SAndrey Smirnov case SDHC_HOSTCTL: 1685fd1e5c81SAndrey Smirnov /* 1686fd1e5c81SAndrey Smirnov * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL) 1687fd1e5c81SAndrey Smirnov * 1688fd1e5c81SAndrey Smirnov * 7 6 5 4 3 2 1 0 1689fd1e5c81SAndrey Smirnov * |-----------+--------+--------+-----------+----------+---------| 1690fd1e5c81SAndrey Smirnov * | Card | Card | Endian | DATA3 | Data | Led | 1691fd1e5c81SAndrey Smirnov * | Detect | Detect | Mode | as Card | Transfer | Control | 1692fd1e5c81SAndrey Smirnov * | Signal | Test | | Detection | Width | | 1693fd1e5c81SAndrey Smirnov * | Selection | Level | | Pin | | | 1694fd1e5c81SAndrey Smirnov * |-----------+--------+--------+-----------+----------+---------| 1695fd1e5c81SAndrey Smirnov * 1696fd1e5c81SAndrey Smirnov * and 0x29 1697fd1e5c81SAndrey Smirnov * 1698fd1e5c81SAndrey Smirnov * 15 10 9 8 1699fd1e5c81SAndrey Smirnov * |----------+------| 1700fd1e5c81SAndrey Smirnov * | Reserved | DMA | 1701fd1e5c81SAndrey Smirnov * | | Sel. | 1702fd1e5c81SAndrey Smirnov * | | | 1703fd1e5c81SAndrey Smirnov * |----------+------| 1704fd1e5c81SAndrey Smirnov * 1705fd1e5c81SAndrey Smirnov * and here's what SDCHI spec expects those offsets to be: 1706fd1e5c81SAndrey Smirnov * 1707fd1e5c81SAndrey Smirnov * 0x28 (Host Control Register) 1708fd1e5c81SAndrey Smirnov * 1709fd1e5c81SAndrey Smirnov * 7 6 5 4 3 2 1 0 1710fd1e5c81SAndrey Smirnov * |--------+--------+----------+------+--------+----------+---------| 1711fd1e5c81SAndrey Smirnov * | Card | Card | Extended | DMA | High | Data | LED | 1712fd1e5c81SAndrey Smirnov * | Detect | Detect | Data | Sel. | Speed | Transfer | Control | 1713fd1e5c81SAndrey Smirnov * | Signal | Test | Transfer | | Enable | Width | | 1714fd1e5c81SAndrey Smirnov * | Sel. | Level | Width | | | | | 1715fd1e5c81SAndrey Smirnov * |--------+--------+----------+------+--------+----------+---------| 1716fd1e5c81SAndrey Smirnov * 1717fd1e5c81SAndrey Smirnov * and 0x29 (Power Control Register) 1718fd1e5c81SAndrey Smirnov * 1719fd1e5c81SAndrey Smirnov * |----------------------------------| 1720fd1e5c81SAndrey Smirnov * | Power Control Register | 1721fd1e5c81SAndrey Smirnov * | | 1722fd1e5c81SAndrey Smirnov * | Description omitted, | 1723fd1e5c81SAndrey Smirnov * | since it has no analog in ESDHCI | 1724fd1e5c81SAndrey Smirnov * | | 1725fd1e5c81SAndrey Smirnov * |----------------------------------| 1726fd1e5c81SAndrey Smirnov * 1727fd1e5c81SAndrey Smirnov * Since offsets 0x2A and 0x2B should be compatible between 1728fd1e5c81SAndrey Smirnov * both IP specs we only need to reconcile least 16-bit of the 1729fd1e5c81SAndrey Smirnov * word we've been given. 1730fd1e5c81SAndrey Smirnov */ 1731fd1e5c81SAndrey Smirnov 1732fd1e5c81SAndrey Smirnov /* 1733fd1e5c81SAndrey Smirnov * First, save bits 7 6 and 0 since they are identical 1734fd1e5c81SAndrey Smirnov */ 173506c5120bSPhilippe Mathieu-Daudé hostctl1 = value & (SDHC_CTRL_LED | 1736fd1e5c81SAndrey Smirnov SDHC_CTRL_CDTEST_INS | 1737fd1e5c81SAndrey Smirnov SDHC_CTRL_CDTEST_EN); 1738fd1e5c81SAndrey Smirnov /* 1739fd1e5c81SAndrey Smirnov * Second, split "Data Transfer Width" from bits 2 and 1 in to 1740fd1e5c81SAndrey Smirnov * bits 5 and 1 1741fd1e5c81SAndrey Smirnov */ 1742fd1e5c81SAndrey Smirnov if (value & ESDHC_CTRL_8BITBUS) { 174306c5120bSPhilippe Mathieu-Daudé hostctl1 |= SDHC_CTRL_8BITBUS; 1744fd1e5c81SAndrey Smirnov } 1745fd1e5c81SAndrey Smirnov 1746fd1e5c81SAndrey Smirnov if (value & ESDHC_CTRL_4BITBUS) { 174706c5120bSPhilippe Mathieu-Daudé hostctl1 |= ESDHC_CTRL_4BITBUS; 1748fd1e5c81SAndrey Smirnov } 1749fd1e5c81SAndrey Smirnov 1750fd1e5c81SAndrey Smirnov /* 1751fd1e5c81SAndrey Smirnov * Third, move DMA select from bits 9 and 8 to bits 4 and 3 1752fd1e5c81SAndrey Smirnov */ 175306c5120bSPhilippe Mathieu-Daudé hostctl1 |= SDHC_DMA_TYPE(value >> (8 - 3)); 1754fd1e5c81SAndrey Smirnov 1755fd1e5c81SAndrey Smirnov /* 1756fd1e5c81SAndrey Smirnov * Now place the corrected value into low 16-bit of the value 1757fd1e5c81SAndrey Smirnov * we are going to give standard SDHCI write function 1758fd1e5c81SAndrey Smirnov * 1759fd1e5c81SAndrey Smirnov * NOTE: This transformation should be the inverse of what can 1760fd1e5c81SAndrey Smirnov * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux 1761fd1e5c81SAndrey Smirnov * kernel 1762fd1e5c81SAndrey Smirnov */ 1763fd1e5c81SAndrey Smirnov value &= ~UINT16_MAX; 176406c5120bSPhilippe Mathieu-Daudé value |= hostctl1; 1765fd1e5c81SAndrey Smirnov value |= (uint16_t)s->pwrcon << 8; 1766fd1e5c81SAndrey Smirnov 1767fd1e5c81SAndrey Smirnov sdhci_write(opaque, offset, value, size); 1768fd1e5c81SAndrey Smirnov break; 1769fd1e5c81SAndrey Smirnov 1770fd1e5c81SAndrey Smirnov case ESDHC_MIX_CTRL: 1771fd1e5c81SAndrey Smirnov /* 1772fd1e5c81SAndrey Smirnov * So, when SD/MMC stack in Linux tries to write to "Transfer 1773fd1e5c81SAndrey Smirnov * Mode Register", ESDHC i.MX quirk code will translate it 1774fd1e5c81SAndrey Smirnov * into a write to ESDHC_MIX_CTRL, so we do the opposite in 1775fd1e5c81SAndrey Smirnov * order to get where we started 1776fd1e5c81SAndrey Smirnov * 1777fd1e5c81SAndrey Smirnov * Note that Auto CMD23 Enable bit is located in a wrong place 1778fd1e5c81SAndrey Smirnov * on i.MX, but since it is not used by QEMU we do not care. 1779fd1e5c81SAndrey Smirnov * 1780fd1e5c81SAndrey Smirnov * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) 1781fd1e5c81SAndrey Smirnov * here becuase it will result in a call to 1782fd1e5c81SAndrey Smirnov * sdhci_send_command(s) which we don't want. 1783fd1e5c81SAndrey Smirnov * 1784fd1e5c81SAndrey Smirnov */ 1785fd1e5c81SAndrey Smirnov s->trnmod = value & UINT16_MAX; 1786fd1e5c81SAndrey Smirnov break; 1787fd1e5c81SAndrey Smirnov case SDHC_TRNMOD: 1788fd1e5c81SAndrey Smirnov /* 1789fd1e5c81SAndrey Smirnov * Similar to above, but this time a write to "Command 1790fd1e5c81SAndrey Smirnov * Register" will be translated into a 4-byte write to 1791fd1e5c81SAndrey Smirnov * "Transfer Mode register" where lower 16-bit of value would 1792fd1e5c81SAndrey Smirnov * be set to zero. So what we do is fill those bits with 1793fd1e5c81SAndrey Smirnov * cached value from s->trnmod and let the SDHCI 1794fd1e5c81SAndrey Smirnov * infrastructure handle the rest 1795fd1e5c81SAndrey Smirnov */ 1796fd1e5c81SAndrey Smirnov sdhci_write(opaque, offset, val | s->trnmod, size); 1797fd1e5c81SAndrey Smirnov break; 1798fd1e5c81SAndrey Smirnov case SDHC_BLKSIZE: 1799fd1e5c81SAndrey Smirnov /* 1800fd1e5c81SAndrey Smirnov * ESDHCI does not implement "Host SDMA Buffer Boundary", and 1801fd1e5c81SAndrey Smirnov * Linux driver will try to zero this field out which will 1802fd1e5c81SAndrey Smirnov * break the rest of SDHCI emulation. 1803fd1e5c81SAndrey Smirnov * 1804fd1e5c81SAndrey Smirnov * Linux defaults to maximum possible setting (512K boundary) 1805fd1e5c81SAndrey Smirnov * and it seems to be the only option that i.MX IP implements, 1806fd1e5c81SAndrey Smirnov * so we artificially set it to that value. 1807fd1e5c81SAndrey Smirnov */ 1808fd1e5c81SAndrey Smirnov val |= 0x7 << 12; 1809fd1e5c81SAndrey Smirnov /* FALLTHROUGH */ 1810fd1e5c81SAndrey Smirnov default: 1811fd1e5c81SAndrey Smirnov sdhci_write(opaque, offset, val, size); 1812fd1e5c81SAndrey Smirnov break; 1813fd1e5c81SAndrey Smirnov } 1814fd1e5c81SAndrey Smirnov } 1815fd1e5c81SAndrey Smirnov 1816fd1e5c81SAndrey Smirnov 1817fd1e5c81SAndrey Smirnov static const MemoryRegionOps usdhc_mmio_ops = { 1818fd1e5c81SAndrey Smirnov .read = usdhc_read, 1819fd1e5c81SAndrey Smirnov .write = usdhc_write, 1820fd1e5c81SAndrey Smirnov .valid = { 1821fd1e5c81SAndrey Smirnov .min_access_size = 1, 1822fd1e5c81SAndrey Smirnov .max_access_size = 4, 1823fd1e5c81SAndrey Smirnov .unaligned = false 1824fd1e5c81SAndrey Smirnov }, 1825fd1e5c81SAndrey Smirnov .endianness = DEVICE_LITTLE_ENDIAN, 1826fd1e5c81SAndrey Smirnov }; 1827fd1e5c81SAndrey Smirnov 1828fd1e5c81SAndrey Smirnov static void imx_usdhc_init(Object *obj) 1829fd1e5c81SAndrey Smirnov { 1830fd1e5c81SAndrey Smirnov SDHCIState *s = SYSBUS_SDHCI(obj); 1831fd1e5c81SAndrey Smirnov 1832fd1e5c81SAndrey Smirnov s->io_ops = &usdhc_mmio_ops; 1833fd1e5c81SAndrey Smirnov s->quirks = SDHCI_QUIRK_NO_BUSY_IRQ; 1834fd1e5c81SAndrey Smirnov } 1835fd1e5c81SAndrey Smirnov 1836fd1e5c81SAndrey Smirnov static const TypeInfo imx_usdhc_info = { 1837fd1e5c81SAndrey Smirnov .name = TYPE_IMX_USDHC, 1838fd1e5c81SAndrey Smirnov .parent = TYPE_SYSBUS_SDHCI, 1839fd1e5c81SAndrey Smirnov .instance_init = imx_usdhc_init, 1840fd1e5c81SAndrey Smirnov }; 1841fd1e5c81SAndrey Smirnov 184249ab747fSPaolo Bonzini static void sdhci_register_types(void) 184349ab747fSPaolo Bonzini { 1844224d10ffSKevin O'Connor type_register_static(&sdhci_pci_info); 18457302dcd6SKevin O'Connor type_register_static(&sdhci_sysbus_info); 184640bbc194SPeter Maydell type_register_static(&sdhci_bus_info); 1847fd1e5c81SAndrey Smirnov type_register_static(&imx_usdhc_info); 184849ab747fSPaolo Bonzini } 184949ab747fSPaolo Bonzini 185049ab747fSPaolo Bonzini type_init(sdhci_register_types) 1851