149ab747fSPaolo Bonzini /* 249ab747fSPaolo Bonzini * SD Association Host Standard Specification v2.0 controller emulation 349ab747fSPaolo Bonzini * 4598a40b3SPhilippe Mathieu-Daudé * Datasheet: PartA2_SD_Host_Controller_Simplified_Specification_Ver2.00.pdf 5598a40b3SPhilippe Mathieu-Daudé * 649ab747fSPaolo Bonzini * Copyright (c) 2011 Samsung Electronics Co., Ltd. 749ab747fSPaolo Bonzini * Mitsyanko Igor <i.mitsyanko@samsung.com> 849ab747fSPaolo Bonzini * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com> 949ab747fSPaolo Bonzini * 1049ab747fSPaolo Bonzini * Based on MMC controller for Samsung S5PC1xx-based board emulation 1149ab747fSPaolo Bonzini * by Alexey Merkulov and Vladimir Monakhov. 1249ab747fSPaolo Bonzini * 1349ab747fSPaolo Bonzini * This program is free software; you can redistribute it and/or modify it 1449ab747fSPaolo Bonzini * under the terms of the GNU General Public License as published by the 1549ab747fSPaolo Bonzini * Free Software Foundation; either version 2 of the License, or (at your 1649ab747fSPaolo Bonzini * option) any later version. 1749ab747fSPaolo Bonzini * 1849ab747fSPaolo Bonzini * This program is distributed in the hope that it will be useful, 1949ab747fSPaolo Bonzini * but WITHOUT ANY WARRANTY; without even the implied warranty of 2049ab747fSPaolo Bonzini * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 2149ab747fSPaolo Bonzini * See the GNU General Public License for more details. 2249ab747fSPaolo Bonzini * 2349ab747fSPaolo Bonzini * You should have received a copy of the GNU General Public License along 2449ab747fSPaolo Bonzini * with this program; if not, see <http://www.gnu.org/licenses/>. 2549ab747fSPaolo Bonzini */ 2649ab747fSPaolo Bonzini 270430891cSPeter Maydell #include "qemu/osdep.h" 284c8f9735SPhilippe Mathieu-Daudé #include "qemu/units.h" 296ff37c3dSPhilippe Mathieu-Daudé #include "qemu/error-report.h" 30b635d98cSPhilippe Mathieu-Daudé #include "qapi/error.h" 3164552b6bSMarkus Armbruster #include "hw/irq.h" 32a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 3349ab747fSPaolo Bonzini #include "sysemu/dma.h" 3449ab747fSPaolo Bonzini #include "qemu/timer.h" 3549ab747fSPaolo Bonzini #include "qemu/bitops.h" 36f82a0f44SPhilippe Mathieu-Daudé #include "hw/sd/sdhci.h" 37d6454270SMarkus Armbruster #include "migration/vmstate.h" 38637d23beSSai Pavan Boddu #include "sdhci-internal.h" 3903dd024fSPaolo Bonzini #include "qemu/log.h" 400b8fa32fSMarkus Armbruster #include "qemu/module.h" 418be487d8SPhilippe Mathieu-Daudé #include "trace.h" 42db1015e9SEduardo Habkost #include "qom/object.h" 4349ab747fSPaolo Bonzini 4440bbc194SPeter Maydell #define TYPE_SDHCI_BUS "sdhci-bus" 45fa34a3c5SEduardo Habkost /* This is reusing the SDBus typedef from SD_BUS */ 46fa34a3c5SEduardo Habkost DECLARE_INSTANCE_CHECKER(SDBus, SDHCI_BUS, 47fa34a3c5SEduardo Habkost TYPE_SDHCI_BUS) 4840bbc194SPeter Maydell 49aa164fbfSPhilippe Mathieu-Daudé #define MASKED_WRITE(reg, mask, val) (reg = (reg & (mask)) | (val)) 50aa164fbfSPhilippe Mathieu-Daudé 5109b738ffSPhilippe Mathieu-Daudé static inline unsigned int sdhci_get_fifolen(SDHCIState *s) 5209b738ffSPhilippe Mathieu-Daudé { 5309b738ffSPhilippe Mathieu-Daudé return 1 << (9 + FIELD_EX32(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH)); 5409b738ffSPhilippe Mathieu-Daudé } 5509b738ffSPhilippe Mathieu-Daudé 566ff37c3dSPhilippe Mathieu-Daudé /* return true on error */ 576ff37c3dSPhilippe Mathieu-Daudé static bool sdhci_check_capab_freq_range(SDHCIState *s, const char *desc, 586ff37c3dSPhilippe Mathieu-Daudé uint8_t freq, Error **errp) 596ff37c3dSPhilippe Mathieu-Daudé { 604d67852dSPhilippe Mathieu-Daudé if (s->sd_spec_version >= 3) { 614d67852dSPhilippe Mathieu-Daudé return false; 624d67852dSPhilippe Mathieu-Daudé } 636ff37c3dSPhilippe Mathieu-Daudé switch (freq) { 646ff37c3dSPhilippe Mathieu-Daudé case 0: 656ff37c3dSPhilippe Mathieu-Daudé case 10 ... 63: 666ff37c3dSPhilippe Mathieu-Daudé break; 676ff37c3dSPhilippe Mathieu-Daudé default: 686ff37c3dSPhilippe Mathieu-Daudé error_setg(errp, "SD %s clock frequency can have value" 696ff37c3dSPhilippe Mathieu-Daudé "in range 0-63 only", desc); 706ff37c3dSPhilippe Mathieu-Daudé return true; 716ff37c3dSPhilippe Mathieu-Daudé } 726ff37c3dSPhilippe Mathieu-Daudé return false; 736ff37c3dSPhilippe Mathieu-Daudé } 746ff37c3dSPhilippe Mathieu-Daudé 756ff37c3dSPhilippe Mathieu-Daudé static void sdhci_check_capareg(SDHCIState *s, Error **errp) 766ff37c3dSPhilippe Mathieu-Daudé { 776ff37c3dSPhilippe Mathieu-Daudé uint64_t msk = s->capareg; 786ff37c3dSPhilippe Mathieu-Daudé uint32_t val; 796ff37c3dSPhilippe Mathieu-Daudé bool y; 806ff37c3dSPhilippe Mathieu-Daudé 816ff37c3dSPhilippe Mathieu-Daudé switch (s->sd_spec_version) { 821e23b63fSPhilippe Mathieu-Daudé case 4: 831e23b63fSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT_V4); 841e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("64-bit system bus (v4)", val); 851e23b63fSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT_V4, 0); 861e23b63fSPhilippe Mathieu-Daudé 871e23b63fSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, UHS_II); 881e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("UHS-II", val); 891e23b63fSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, UHS_II, 0); 901e23b63fSPhilippe Mathieu-Daudé 911e23b63fSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA3); 921e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("ADMA3", val); 931e23b63fSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA3, 0); 941e23b63fSPhilippe Mathieu-Daudé 951e23b63fSPhilippe Mathieu-Daudé /* fallthrough */ 964d67852dSPhilippe Mathieu-Daudé case 3: 974d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ASYNC_INT); 984d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("async interrupt", val); 994d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ASYNC_INT, 0); 1004d67852dSPhilippe Mathieu-Daudé 1014d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SLOT_TYPE); 1024d67852dSPhilippe Mathieu-Daudé if (val) { 1034d67852dSPhilippe Mathieu-Daudé error_setg(errp, "slot-type not supported"); 1044d67852dSPhilippe Mathieu-Daudé return; 1054d67852dSPhilippe Mathieu-Daudé } 1064d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("slot type", val); 1074d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SLOT_TYPE, 0); 1084d67852dSPhilippe Mathieu-Daudé 1094d67852dSPhilippe Mathieu-Daudé if (val != 2) { 1104d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, EMBEDDED_8BIT); 1114d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("8-bit bus", val); 1124d67852dSPhilippe Mathieu-Daudé } 1134d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, EMBEDDED_8BIT, 0); 1144d67852dSPhilippe Mathieu-Daudé 1154d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS_SPEED); 1164d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("bus speed mask", val); 1174d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BUS_SPEED, 0); 1184d67852dSPhilippe Mathieu-Daudé 1194d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, DRIVER_STRENGTH); 1204d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("driver strength mask", val); 1214d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, DRIVER_STRENGTH, 0); 1224d67852dSPhilippe Mathieu-Daudé 1234d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, TIMER_RETUNING); 1244d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("timer re-tuning", val); 1254d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, TIMER_RETUNING, 0); 1264d67852dSPhilippe Mathieu-Daudé 1274d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDR50_TUNING); 1284d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("use SDR50 tuning", val); 1294d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SDR50_TUNING, 0); 1304d67852dSPhilippe Mathieu-Daudé 1314d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, RETUNING_MODE); 1324d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("re-tuning mode", val); 1334d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, RETUNING_MODE, 0); 1344d67852dSPhilippe Mathieu-Daudé 1354d67852dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, CLOCK_MULT); 1364d67852dSPhilippe Mathieu-Daudé trace_sdhci_capareg("clock multiplier", val); 1374d67852dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, CLOCK_MULT, 0); 1384d67852dSPhilippe Mathieu-Daudé 1394d67852dSPhilippe Mathieu-Daudé /* fallthrough */ 1406ff37c3dSPhilippe Mathieu-Daudé case 2: /* default version */ 1410540fba9SPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA2); 1420540fba9SPhilippe Mathieu-Daudé trace_sdhci_capareg("ADMA2", val); 1430540fba9SPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA2, 0); 1440540fba9SPhilippe Mathieu-Daudé 1450540fba9SPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA1); 1460540fba9SPhilippe Mathieu-Daudé trace_sdhci_capareg("ADMA1", val); 1470540fba9SPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA1, 0); 1480540fba9SPhilippe Mathieu-Daudé 1490540fba9SPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT); 1501e23b63fSPhilippe Mathieu-Daudé trace_sdhci_capareg("64-bit system bus (v3)", val); 1510540fba9SPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT, 0); 1526ff37c3dSPhilippe Mathieu-Daudé 1536ff37c3dSPhilippe Mathieu-Daudé /* fallthrough */ 1546ff37c3dSPhilippe Mathieu-Daudé case 1: 1556ff37c3dSPhilippe Mathieu-Daudé y = FIELD_EX64(s->capareg, SDHC_CAPAB, TOUNIT); 1566ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, TOUNIT, 0); 1576ff37c3dSPhilippe Mathieu-Daudé 1586ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, TOCLKFREQ); 1596ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg(y ? "timeout (MHz)" : "Timeout (KHz)", val); 1606ff37c3dSPhilippe Mathieu-Daudé if (sdhci_check_capab_freq_range(s, "timeout", val, errp)) { 1616ff37c3dSPhilippe Mathieu-Daudé return; 1626ff37c3dSPhilippe Mathieu-Daudé } 1636ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, TOCLKFREQ, 0); 1646ff37c3dSPhilippe Mathieu-Daudé 1656ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, BASECLKFREQ); 1666ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg(y ? "base (MHz)" : "Base (KHz)", val); 1676ff37c3dSPhilippe Mathieu-Daudé if (sdhci_check_capab_freq_range(s, "base", val, errp)) { 1686ff37c3dSPhilippe Mathieu-Daudé return; 1696ff37c3dSPhilippe Mathieu-Daudé } 1706ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, BASECLKFREQ, 0); 1716ff37c3dSPhilippe Mathieu-Daudé 1726ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH); 1736ff37c3dSPhilippe Mathieu-Daudé if (val >= 3) { 1746ff37c3dSPhilippe Mathieu-Daudé error_setg(errp, "block size can be 512, 1024 or 2048 only"); 1756ff37c3dSPhilippe Mathieu-Daudé return; 1766ff37c3dSPhilippe Mathieu-Daudé } 1776ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("max block length", sdhci_get_fifolen(s)); 1786ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, MAXBLOCKLENGTH, 0); 1796ff37c3dSPhilippe Mathieu-Daudé 1806ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, HIGHSPEED); 1816ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("high speed", val); 1826ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, HIGHSPEED, 0); 1836ff37c3dSPhilippe Mathieu-Daudé 1846ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDMA); 1856ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("SDMA", val); 1866ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SDMA, 0); 1876ff37c3dSPhilippe Mathieu-Daudé 1886ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, SUSPRESUME); 1896ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("suspend/resume", val); 1906ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, SUSPRESUME, 0); 1916ff37c3dSPhilippe Mathieu-Daudé 1926ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, V33); 1936ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("3.3v", val); 1946ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, V33, 0); 1956ff37c3dSPhilippe Mathieu-Daudé 1966ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, V30); 1976ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("3.0v", val); 1986ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, V30, 0); 1996ff37c3dSPhilippe Mathieu-Daudé 2006ff37c3dSPhilippe Mathieu-Daudé val = FIELD_EX64(s->capareg, SDHC_CAPAB, V18); 2016ff37c3dSPhilippe Mathieu-Daudé trace_sdhci_capareg("1.8v", val); 2026ff37c3dSPhilippe Mathieu-Daudé msk = FIELD_DP64(msk, SDHC_CAPAB, V18, 0); 2036ff37c3dSPhilippe Mathieu-Daudé break; 2046ff37c3dSPhilippe Mathieu-Daudé 2056ff37c3dSPhilippe Mathieu-Daudé default: 2066ff37c3dSPhilippe Mathieu-Daudé error_setg(errp, "Unsupported spec version: %u", s->sd_spec_version); 2076ff37c3dSPhilippe Mathieu-Daudé } 2086ff37c3dSPhilippe Mathieu-Daudé if (msk) { 2096ff37c3dSPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, 2106ff37c3dSPhilippe Mathieu-Daudé "SDHCI: unknown CAPAB mask: 0x%016" PRIx64 "\n", msk); 2116ff37c3dSPhilippe Mathieu-Daudé } 2126ff37c3dSPhilippe Mathieu-Daudé } 2136ff37c3dSPhilippe Mathieu-Daudé 21449ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s) 21549ab747fSPaolo Bonzini { 21649ab747fSPaolo Bonzini return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) || 21749ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) || 21849ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV)); 21949ab747fSPaolo Bonzini } 22049ab747fSPaolo Bonzini 2212bd9ae7eSPhilippe Mathieu-Daudé /* Return true if IRQ was pending and delivered */ 2222bd9ae7eSPhilippe Mathieu-Daudé static bool sdhci_update_irq(SDHCIState *s) 22349ab747fSPaolo Bonzini { 2242bd9ae7eSPhilippe Mathieu-Daudé bool pending = sdhci_slotint(s); 2252bd9ae7eSPhilippe Mathieu-Daudé 2262bd9ae7eSPhilippe Mathieu-Daudé qemu_set_irq(s->irq, pending); 2272bd9ae7eSPhilippe Mathieu-Daudé 2282bd9ae7eSPhilippe Mathieu-Daudé return pending; 22949ab747fSPaolo Bonzini } 23049ab747fSPaolo Bonzini 23149ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque) 23249ab747fSPaolo Bonzini { 23349ab747fSPaolo Bonzini SDHCIState *s = (SDHCIState *)opaque; 23449ab747fSPaolo Bonzini 23549ab747fSPaolo Bonzini if (s->norintsts & SDHC_NIS_REMOVE) { 236bc72ad67SAlex Bligh timer_mod(s->insert_timer, 237bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 23849ab747fSPaolo Bonzini } else { 23949ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 24049ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 24149ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 24249ab747fSPaolo Bonzini } 24349ab747fSPaolo Bonzini sdhci_update_irq(s); 24449ab747fSPaolo Bonzini } 24549ab747fSPaolo Bonzini } 24649ab747fSPaolo Bonzini 24740bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level) 24849ab747fSPaolo Bonzini { 24940bbc194SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 25049ab747fSPaolo Bonzini 2518be487d8SPhilippe Mathieu-Daudé trace_sdhci_set_inserted(level ? "insert" : "eject"); 25249ab747fSPaolo Bonzini if ((s->norintsts & SDHC_NIS_REMOVE) && level) { 25349ab747fSPaolo Bonzini /* Give target some time to notice card ejection */ 254bc72ad67SAlex Bligh timer_mod(s->insert_timer, 255bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 25649ab747fSPaolo Bonzini } else { 25749ab747fSPaolo Bonzini if (level) { 25849ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 25949ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 26049ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 26149ab747fSPaolo Bonzini } 26249ab747fSPaolo Bonzini } else { 26349ab747fSPaolo Bonzini s->prnsts = 0x1fa0000; 26449ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 26549ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_SDCLK_EN; 26649ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_REMOVE) { 26749ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_REMOVE; 26849ab747fSPaolo Bonzini } 26949ab747fSPaolo Bonzini } 27049ab747fSPaolo Bonzini sdhci_update_irq(s); 27149ab747fSPaolo Bonzini } 27249ab747fSPaolo Bonzini } 27349ab747fSPaolo Bonzini 27440bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level) 27549ab747fSPaolo Bonzini { 27640bbc194SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 27749ab747fSPaolo Bonzini 27849ab747fSPaolo Bonzini if (level) { 27949ab747fSPaolo Bonzini s->prnsts &= ~SDHC_WRITE_PROTECT; 28049ab747fSPaolo Bonzini } else { 28149ab747fSPaolo Bonzini /* Write enabled */ 28249ab747fSPaolo Bonzini s->prnsts |= SDHC_WRITE_PROTECT; 28349ab747fSPaolo Bonzini } 28449ab747fSPaolo Bonzini } 28549ab747fSPaolo Bonzini 28649ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s) 28749ab747fSPaolo Bonzini { 28840bbc194SPeter Maydell DeviceState *dev = DEVICE(s); 28940bbc194SPeter Maydell 290bc72ad67SAlex Bligh timer_del(s->insert_timer); 291bc72ad67SAlex Bligh timer_del(s->transfer_timer); 292aceb5b06SPhilippe Mathieu-Daudé 293aceb5b06SPhilippe Mathieu-Daudé /* Set all registers to 0. Capabilities/Version registers are not cleared 29449ab747fSPaolo Bonzini * and assumed to always preserve their value, given to them during 29549ab747fSPaolo Bonzini * initialization */ 29649ab747fSPaolo Bonzini memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad); 29749ab747fSPaolo Bonzini 29840bbc194SPeter Maydell /* Reset other state based on current card insertion/readonly status */ 29940bbc194SPeter Maydell sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus)); 30040bbc194SPeter Maydell sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus)); 30140bbc194SPeter Maydell 30249ab747fSPaolo Bonzini s->data_count = 0; 30349ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 3040a7ac9f9SAndrew Baumann s->pending_insert_state = false; 30549ab747fSPaolo Bonzini } 30649ab747fSPaolo Bonzini 3078b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev) 3088b41c305SPeter Maydell { 3098b41c305SPeter Maydell /* QOM (ie power-on) reset. This is identical to reset 3108b41c305SPeter Maydell * commanded via device register apart from handling of the 3118b41c305SPeter Maydell * 'pending insert on powerup' quirk. 3128b41c305SPeter Maydell */ 3138b41c305SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 3148b41c305SPeter Maydell 3158b41c305SPeter Maydell sdhci_reset(s); 3168b41c305SPeter Maydell 3178b41c305SPeter Maydell if (s->pending_insert_quirk) { 3188b41c305SPeter Maydell s->pending_insert_state = true; 3198b41c305SPeter Maydell } 3208b41c305SPeter Maydell } 3218b41c305SPeter Maydell 322d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque); 32349ab747fSPaolo Bonzini 32449ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s) 32549ab747fSPaolo Bonzini { 32649ab747fSPaolo Bonzini SDRequest request; 32749ab747fSPaolo Bonzini uint8_t response[16]; 32849ab747fSPaolo Bonzini int rlen; 329b263d8f9SBin Meng bool timeout = false; 33049ab747fSPaolo Bonzini 33149ab747fSPaolo Bonzini s->errintsts = 0; 33249ab747fSPaolo Bonzini s->acmd12errsts = 0; 33349ab747fSPaolo Bonzini request.cmd = s->cmdreg >> 8; 33449ab747fSPaolo Bonzini request.arg = s->argument; 3358be487d8SPhilippe Mathieu-Daudé 3368be487d8SPhilippe Mathieu-Daudé trace_sdhci_send_command(request.cmd, request.arg); 33740bbc194SPeter Maydell rlen = sdbus_do_command(&s->sdbus, &request, response); 33849ab747fSPaolo Bonzini 33949ab747fSPaolo Bonzini if (s->cmdreg & SDHC_CMD_RESPONSE) { 34049ab747fSPaolo Bonzini if (rlen == 4) { 341b3141c06SPhilippe Mathieu-Daudé s->rspreg[0] = ldl_be_p(response); 34249ab747fSPaolo Bonzini s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0; 3438be487d8SPhilippe Mathieu-Daudé trace_sdhci_response4(s->rspreg[0]); 34449ab747fSPaolo Bonzini } else if (rlen == 16) { 345b3141c06SPhilippe Mathieu-Daudé s->rspreg[0] = ldl_be_p(&response[11]); 346b3141c06SPhilippe Mathieu-Daudé s->rspreg[1] = ldl_be_p(&response[7]); 347b3141c06SPhilippe Mathieu-Daudé s->rspreg[2] = ldl_be_p(&response[3]); 34849ab747fSPaolo Bonzini s->rspreg[3] = (response[0] << 16) | (response[1] << 8) | 34949ab747fSPaolo Bonzini response[2]; 3508be487d8SPhilippe Mathieu-Daudé trace_sdhci_response16(s->rspreg[3], s->rspreg[2], 3518be487d8SPhilippe Mathieu-Daudé s->rspreg[1], s->rspreg[0]); 35249ab747fSPaolo Bonzini } else { 353b263d8f9SBin Meng timeout = true; 3548be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("timeout waiting for command response"); 35549ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) { 35649ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMDTIMEOUT; 35749ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 35849ab747fSPaolo Bonzini } 35949ab747fSPaolo Bonzini } 36049ab747fSPaolo Bonzini 361fd1e5c81SAndrey Smirnov if (!(s->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) && 362fd1e5c81SAndrey Smirnov (s->norintstsen & SDHC_NISEN_TRSCMP) && 36349ab747fSPaolo Bonzini (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) { 36449ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 36549ab747fSPaolo Bonzini } 36649ab747fSPaolo Bonzini } 36749ab747fSPaolo Bonzini 36849ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CMDCMP) { 36949ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_CMDCMP; 37049ab747fSPaolo Bonzini } 37149ab747fSPaolo Bonzini 37249ab747fSPaolo Bonzini sdhci_update_irq(s); 37349ab747fSPaolo Bonzini 374b263d8f9SBin Meng if (!timeout && s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) { 375656f416cSPeter Crosthwaite s->data_count = 0; 376d368ba43SKevin O'Connor sdhci_data_transfer(s); 37749ab747fSPaolo Bonzini } 37849ab747fSPaolo Bonzini } 37949ab747fSPaolo Bonzini 38049ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s) 38149ab747fSPaolo Bonzini { 38249ab747fSPaolo Bonzini /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */ 38349ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) { 38449ab747fSPaolo Bonzini SDRequest request; 38549ab747fSPaolo Bonzini uint8_t response[16]; 38649ab747fSPaolo Bonzini 38749ab747fSPaolo Bonzini request.cmd = 0x0C; 38849ab747fSPaolo Bonzini request.arg = 0; 3898be487d8SPhilippe Mathieu-Daudé trace_sdhci_end_transfer(request.cmd, request.arg); 39040bbc194SPeter Maydell sdbus_do_command(&s->sdbus, &request, response); 39149ab747fSPaolo Bonzini /* Auto CMD12 response goes to the upper Response register */ 392b3141c06SPhilippe Mathieu-Daudé s->rspreg[3] = ldl_be_p(response); 39349ab747fSPaolo Bonzini } 39449ab747fSPaolo Bonzini 39549ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE | 39649ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT | 39749ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE); 39849ab747fSPaolo Bonzini 39949ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_TRSCMP) { 40049ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 40149ab747fSPaolo Bonzini } 40249ab747fSPaolo Bonzini 40349ab747fSPaolo Bonzini sdhci_update_irq(s); 40449ab747fSPaolo Bonzini } 40549ab747fSPaolo Bonzini 40649ab747fSPaolo Bonzini /* 40749ab747fSPaolo Bonzini * Programmed i/o data transfer 40849ab747fSPaolo Bonzini */ 409d23b6caaSPhilippe Mathieu-Daudé #define BLOCK_SIZE_MASK (4 * KiB - 1) 41049ab747fSPaolo Bonzini 41149ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */ 41249ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s) 41349ab747fSPaolo Bonzini { 414ea55a221SPhilippe Mathieu-Daudé const uint16_t blk_size = s->blksize & BLOCK_SIZE_MASK; 41549ab747fSPaolo Bonzini 41649ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) && 41749ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) { 41849ab747fSPaolo Bonzini return; 41949ab747fSPaolo Bonzini } 42049ab747fSPaolo Bonzini 421ea55a221SPhilippe Mathieu-Daudé if (!FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) { 42208022a91SPhilippe Mathieu-Daudé /* Device is not in tuning */ 423618e0be1SPhilippe Mathieu-Daudé sdbus_read_data(&s->sdbus, s->fifo_buffer, blk_size); 424ea55a221SPhilippe Mathieu-Daudé } 425ea55a221SPhilippe Mathieu-Daudé 426ea55a221SPhilippe Mathieu-Daudé if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) { 42708022a91SPhilippe Mathieu-Daudé /* Device is in tuning */ 428ea55a221SPhilippe Mathieu-Daudé s->hostctl2 &= ~R_SDHC_HOSTCTL2_EXECUTE_TUNING_MASK; 429ea55a221SPhilippe Mathieu-Daudé s->hostctl2 |= R_SDHC_HOSTCTL2_SAMPLING_CLKSEL_MASK; 430ea55a221SPhilippe Mathieu-Daudé s->prnsts &= ~(SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ | 431ea55a221SPhilippe Mathieu-Daudé SDHC_DATA_INHIBIT); 432ea55a221SPhilippe Mathieu-Daudé goto read_done; 43349ab747fSPaolo Bonzini } 43449ab747fSPaolo Bonzini 43549ab747fSPaolo Bonzini /* New data now available for READ through Buffer Port Register */ 43649ab747fSPaolo Bonzini s->prnsts |= SDHC_DATA_AVAILABLE; 43749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_RBUFRDY) { 43849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_RBUFRDY; 43949ab747fSPaolo Bonzini } 44049ab747fSPaolo Bonzini 44149ab747fSPaolo Bonzini /* Clear DAT line active status if that was the last block */ 44249ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 44349ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) { 44449ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 44549ab747fSPaolo Bonzini } 44649ab747fSPaolo Bonzini 44749ab747fSPaolo Bonzini /* If stop at block gap request was set and it's not the last block of 44849ab747fSPaolo Bonzini * data - generate Block Event interrupt */ 44949ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) && 45049ab747fSPaolo Bonzini s->blkcnt != 1) { 45149ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 45249ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 45349ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 45449ab747fSPaolo Bonzini } 45549ab747fSPaolo Bonzini } 45649ab747fSPaolo Bonzini 457ea55a221SPhilippe Mathieu-Daudé read_done: 45849ab747fSPaolo Bonzini sdhci_update_irq(s); 45949ab747fSPaolo Bonzini } 46049ab747fSPaolo Bonzini 46149ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */ 46249ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size) 46349ab747fSPaolo Bonzini { 46449ab747fSPaolo Bonzini uint32_t value = 0; 46549ab747fSPaolo Bonzini int i; 46649ab747fSPaolo Bonzini 46749ab747fSPaolo Bonzini /* first check that a valid data exists in host controller input buffer */ 46849ab747fSPaolo Bonzini if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) { 4698be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("read from empty buffer"); 47049ab747fSPaolo Bonzini return 0; 47149ab747fSPaolo Bonzini } 47249ab747fSPaolo Bonzini 47349ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 47449ab747fSPaolo Bonzini value |= s->fifo_buffer[s->data_count] << i * 8; 47549ab747fSPaolo Bonzini s->data_count++; 47649ab747fSPaolo Bonzini /* check if we've read all valid data (blksize bytes) from buffer */ 477bf8ec38eSPhilippe Mathieu-Daudé if ((s->data_count) >= (s->blksize & BLOCK_SIZE_MASK)) { 4788be487d8SPhilippe Mathieu-Daudé trace_sdhci_read_dataport(s->data_count); 47949ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */ 48049ab747fSPaolo Bonzini s->data_count = 0; /* next buff read must start at position [0] */ 48149ab747fSPaolo Bonzini 48249ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 48349ab747fSPaolo Bonzini s->blkcnt--; 48449ab747fSPaolo Bonzini } 48549ab747fSPaolo Bonzini 48649ab747fSPaolo Bonzini /* if that was the last block of data */ 48749ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 48849ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) || 48949ab747fSPaolo Bonzini /* stop at gap request */ 49049ab747fSPaolo Bonzini (s->stopped_state == sdhc_gap_read && 49149ab747fSPaolo Bonzini !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) { 492d368ba43SKevin O'Connor sdhci_end_transfer(s); 49349ab747fSPaolo Bonzini } else { /* if there are more data, read next block from card */ 494d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 49549ab747fSPaolo Bonzini } 49649ab747fSPaolo Bonzini break; 49749ab747fSPaolo Bonzini } 49849ab747fSPaolo Bonzini } 49949ab747fSPaolo Bonzini 50049ab747fSPaolo Bonzini return value; 50149ab747fSPaolo Bonzini } 50249ab747fSPaolo Bonzini 50349ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */ 50449ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s) 50549ab747fSPaolo Bonzini { 50649ab747fSPaolo Bonzini if (s->prnsts & SDHC_SPACE_AVAILABLE) { 50749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 50849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_WBUFRDY; 50949ab747fSPaolo Bonzini } 51049ab747fSPaolo Bonzini sdhci_update_irq(s); 51149ab747fSPaolo Bonzini return; 51249ab747fSPaolo Bonzini } 51349ab747fSPaolo Bonzini 51449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 51549ab747fSPaolo Bonzini if (s->blkcnt == 0) { 51649ab747fSPaolo Bonzini return; 51749ab747fSPaolo Bonzini } else { 51849ab747fSPaolo Bonzini s->blkcnt--; 51949ab747fSPaolo Bonzini } 52049ab747fSPaolo Bonzini } 52149ab747fSPaolo Bonzini 52262a21be6SPhilippe Mathieu-Daudé sdbus_write_data(&s->sdbus, s->fifo_buffer, s->blksize & BLOCK_SIZE_MASK); 52349ab747fSPaolo Bonzini 52449ab747fSPaolo Bonzini /* Next data can be written through BUFFER DATORT register */ 52549ab747fSPaolo Bonzini s->prnsts |= SDHC_SPACE_AVAILABLE; 52649ab747fSPaolo Bonzini 52749ab747fSPaolo Bonzini /* Finish transfer if that was the last block of data */ 52849ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 52949ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && 53049ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) { 531d368ba43SKevin O'Connor sdhci_end_transfer(s); 532dcdb4cd8SPeter Crosthwaite } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 533dcdb4cd8SPeter Crosthwaite s->norintsts |= SDHC_NIS_WBUFRDY; 53449ab747fSPaolo Bonzini } 53549ab747fSPaolo Bonzini 53649ab747fSPaolo Bonzini /* Generate Block Gap Event if requested and if not the last block */ 53749ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) && 53849ab747fSPaolo Bonzini s->blkcnt > 0) { 53949ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DOING_WRITE; 54049ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 54149ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 54249ab747fSPaolo Bonzini } 543d368ba43SKevin O'Connor sdhci_end_transfer(s); 54449ab747fSPaolo Bonzini } 54549ab747fSPaolo Bonzini 54649ab747fSPaolo Bonzini sdhci_update_irq(s); 54749ab747fSPaolo Bonzini } 54849ab747fSPaolo Bonzini 54949ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port 55049ab747fSPaolo Bonzini * register */ 55149ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size) 55249ab747fSPaolo Bonzini { 55349ab747fSPaolo Bonzini unsigned i; 55449ab747fSPaolo Bonzini 55549ab747fSPaolo Bonzini /* Check that there is free space left in a buffer */ 55649ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) { 5578be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Can't write to data buffer: buffer full"); 55849ab747fSPaolo Bonzini return; 55949ab747fSPaolo Bonzini } 56049ab747fSPaolo Bonzini 56149ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 56249ab747fSPaolo Bonzini s->fifo_buffer[s->data_count] = value & 0xFF; 56349ab747fSPaolo Bonzini s->data_count++; 56449ab747fSPaolo Bonzini value >>= 8; 565bf8ec38eSPhilippe Mathieu-Daudé if (s->data_count >= (s->blksize & BLOCK_SIZE_MASK)) { 5668be487d8SPhilippe Mathieu-Daudé trace_sdhci_write_dataport(s->data_count); 56749ab747fSPaolo Bonzini s->data_count = 0; 56849ab747fSPaolo Bonzini s->prnsts &= ~SDHC_SPACE_AVAILABLE; 56949ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_WRITE) { 570d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 57149ab747fSPaolo Bonzini } 57249ab747fSPaolo Bonzini } 57349ab747fSPaolo Bonzini } 57449ab747fSPaolo Bonzini } 57549ab747fSPaolo Bonzini 57649ab747fSPaolo Bonzini /* 57749ab747fSPaolo Bonzini * Single DMA data transfer 57849ab747fSPaolo Bonzini */ 57949ab747fSPaolo Bonzini 58049ab747fSPaolo Bonzini /* Multi block SDMA transfer */ 58149ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s) 58249ab747fSPaolo Bonzini { 58349ab747fSPaolo Bonzini bool page_aligned = false; 584618e0be1SPhilippe Mathieu-Daudé unsigned int begin; 585bf8ec38eSPhilippe Mathieu-Daudé const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK; 586bf8ec38eSPhilippe Mathieu-Daudé uint32_t boundary_chk = 1 << (((s->blksize & ~BLOCK_SIZE_MASK) >> 12) + 12); 58749ab747fSPaolo Bonzini uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk); 58849ab747fSPaolo Bonzini 5896e86d903SPrasad J Pandit if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) { 5906e86d903SPrasad J Pandit qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n"); 5916e86d903SPrasad J Pandit return; 5926e86d903SPrasad J Pandit } 5936e86d903SPrasad J Pandit 59449ab747fSPaolo Bonzini /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for 59549ab747fSPaolo Bonzini * possible stop at page boundary if initial address is not page aligned, 59649ab747fSPaolo Bonzini * allow them to work properly */ 59749ab747fSPaolo Bonzini if ((s->sdmasysad % boundary_chk) == 0) { 59849ab747fSPaolo Bonzini page_aligned = true; 59949ab747fSPaolo Bonzini } 60049ab747fSPaolo Bonzini 6018bc1f1aaSBin Meng s->prnsts |= SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE; 60249ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 6038bc1f1aaSBin Meng s->prnsts |= SDHC_DOING_READ; 60449ab747fSPaolo Bonzini while (s->blkcnt) { 60549ab747fSPaolo Bonzini if (s->data_count == 0) { 606618e0be1SPhilippe Mathieu-Daudé sdbus_read_data(&s->sdbus, s->fifo_buffer, block_size); 60749ab747fSPaolo Bonzini } 60849ab747fSPaolo Bonzini begin = s->data_count; 60949ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 61049ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 61149ab747fSPaolo Bonzini boundary_count = 0; 61249ab747fSPaolo Bonzini } else { 61349ab747fSPaolo Bonzini s->data_count = block_size; 61449ab747fSPaolo Bonzini boundary_count -= block_size - begin; 61549ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 61649ab747fSPaolo Bonzini s->blkcnt--; 61749ab747fSPaolo Bonzini } 61849ab747fSPaolo Bonzini } 619ba06fe8aSPhilippe Mathieu-Daudé dma_memory_write(s->dma_as, s->sdmasysad, &s->fifo_buffer[begin], 620ba06fe8aSPhilippe Mathieu-Daudé s->data_count - begin, MEMTXATTRS_UNSPECIFIED); 62149ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 62249ab747fSPaolo Bonzini if (s->data_count == block_size) { 62349ab747fSPaolo Bonzini s->data_count = 0; 62449ab747fSPaolo Bonzini } 62549ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 62649ab747fSPaolo Bonzini break; 62749ab747fSPaolo Bonzini } 62849ab747fSPaolo Bonzini } 62949ab747fSPaolo Bonzini } else { 6308bc1f1aaSBin Meng s->prnsts |= SDHC_DOING_WRITE; 63149ab747fSPaolo Bonzini while (s->blkcnt) { 63249ab747fSPaolo Bonzini begin = s->data_count; 63349ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 63449ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 63549ab747fSPaolo Bonzini boundary_count = 0; 63649ab747fSPaolo Bonzini } else { 63749ab747fSPaolo Bonzini s->data_count = block_size; 63849ab747fSPaolo Bonzini boundary_count -= block_size - begin; 63949ab747fSPaolo Bonzini } 640ba06fe8aSPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, s->sdmasysad, &s->fifo_buffer[begin], 641ba06fe8aSPhilippe Mathieu-Daudé s->data_count - begin, MEMTXATTRS_UNSPECIFIED); 64249ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 64349ab747fSPaolo Bonzini if (s->data_count == block_size) { 64462a21be6SPhilippe Mathieu-Daudé sdbus_write_data(&s->sdbus, s->fifo_buffer, block_size); 64549ab747fSPaolo Bonzini s->data_count = 0; 64649ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 64749ab747fSPaolo Bonzini s->blkcnt--; 64849ab747fSPaolo Bonzini } 64949ab747fSPaolo Bonzini } 65049ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 65149ab747fSPaolo Bonzini break; 65249ab747fSPaolo Bonzini } 65349ab747fSPaolo Bonzini } 65449ab747fSPaolo Bonzini } 65549ab747fSPaolo Bonzini 65649ab747fSPaolo Bonzini if (s->blkcnt == 0) { 657d368ba43SKevin O'Connor sdhci_end_transfer(s); 65849ab747fSPaolo Bonzini } else { 65949ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_DMA) { 66049ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_DMA; 66149ab747fSPaolo Bonzini } 66249ab747fSPaolo Bonzini sdhci_update_irq(s); 66349ab747fSPaolo Bonzini } 66449ab747fSPaolo Bonzini } 66549ab747fSPaolo Bonzini 66649ab747fSPaolo Bonzini /* single block SDMA transfer */ 66749ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s) 66849ab747fSPaolo Bonzini { 669bf8ec38eSPhilippe Mathieu-Daudé uint32_t datacnt = s->blksize & BLOCK_SIZE_MASK; 67049ab747fSPaolo Bonzini 67149ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 672618e0be1SPhilippe Mathieu-Daudé sdbus_read_data(&s->sdbus, s->fifo_buffer, datacnt); 673ba06fe8aSPhilippe Mathieu-Daudé dma_memory_write(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt, 674ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 67549ab747fSPaolo Bonzini } else { 676ba06fe8aSPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt, 677ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 67862a21be6SPhilippe Mathieu-Daudé sdbus_write_data(&s->sdbus, s->fifo_buffer, datacnt); 67949ab747fSPaolo Bonzini } 68049ab747fSPaolo Bonzini s->blkcnt--; 68149ab747fSPaolo Bonzini 682d368ba43SKevin O'Connor sdhci_end_transfer(s); 68349ab747fSPaolo Bonzini } 68449ab747fSPaolo Bonzini 68549ab747fSPaolo Bonzini typedef struct ADMADescr { 68649ab747fSPaolo Bonzini hwaddr addr; 68749ab747fSPaolo Bonzini uint16_t length; 68849ab747fSPaolo Bonzini uint8_t attr; 68949ab747fSPaolo Bonzini uint8_t incr; 69049ab747fSPaolo Bonzini } ADMADescr; 69149ab747fSPaolo Bonzini 69249ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr) 69349ab747fSPaolo Bonzini { 69449ab747fSPaolo Bonzini uint32_t adma1 = 0; 69549ab747fSPaolo Bonzini uint64_t adma2 = 0; 69649ab747fSPaolo Bonzini hwaddr entry_addr = (hwaddr)s->admasysaddr; 69706c5120bSPhilippe Mathieu-Daudé switch (SDHC_DMA_TYPE(s->hostctl1)) { 69849ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 699ba06fe8aSPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), 700ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 70149ab747fSPaolo Bonzini adma2 = le64_to_cpu(adma2); 70249ab747fSPaolo Bonzini /* The spec does not specify endianness of descriptor table. 70349ab747fSPaolo Bonzini * We currently assume that it is LE. 70449ab747fSPaolo Bonzini */ 70549ab747fSPaolo Bonzini dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull; 70649ab747fSPaolo Bonzini dscr->length = (uint16_t)extract64(adma2, 16, 16); 70749ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract64(adma2, 0, 7); 70849ab747fSPaolo Bonzini dscr->incr = 8; 70949ab747fSPaolo Bonzini break; 71049ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 711ba06fe8aSPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr, &adma1, sizeof(adma1), 712ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 71349ab747fSPaolo Bonzini adma1 = le32_to_cpu(adma1); 71449ab747fSPaolo Bonzini dscr->addr = (hwaddr)(adma1 & 0xFFFFF000); 71549ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract32(adma1, 0, 7); 71649ab747fSPaolo Bonzini dscr->incr = 4; 71749ab747fSPaolo Bonzini if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) { 71849ab747fSPaolo Bonzini dscr->length = (uint16_t)extract32(adma1, 12, 16); 71949ab747fSPaolo Bonzini } else { 7204c8f9735SPhilippe Mathieu-Daudé dscr->length = 4 * KiB; 72149ab747fSPaolo Bonzini } 72249ab747fSPaolo Bonzini break; 72349ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 724ba06fe8aSPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr, &dscr->attr, 1, 725ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 726ba06fe8aSPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr + 2, &dscr->length, 2, 727ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 72849ab747fSPaolo Bonzini dscr->length = le16_to_cpu(dscr->length); 729ba06fe8aSPhilippe Mathieu-Daudé dma_memory_read(s->dma_as, entry_addr + 4, &dscr->addr, 8, 730ba06fe8aSPhilippe Mathieu-Daudé MEMTXATTRS_UNSPECIFIED); 73104654b5aSSai Pavan Boddu dscr->addr = le64_to_cpu(dscr->addr); 73204654b5aSSai Pavan Boddu dscr->attr &= (uint8_t) ~0xC0; 73349ab747fSPaolo Bonzini dscr->incr = 12; 73449ab747fSPaolo Bonzini break; 73549ab747fSPaolo Bonzini } 73649ab747fSPaolo Bonzini } 73749ab747fSPaolo Bonzini 73849ab747fSPaolo Bonzini /* Advanced DMA data transfer */ 73949ab747fSPaolo Bonzini 74049ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s) 74149ab747fSPaolo Bonzini { 742618e0be1SPhilippe Mathieu-Daudé unsigned int begin, length; 743bf8ec38eSPhilippe Mathieu-Daudé const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK; 744799f7f01SPhilippe Mathieu-Daudé const MemTxAttrs attrs = { .memory = true }; 7458be487d8SPhilippe Mathieu-Daudé ADMADescr dscr = {}; 74678e619cbSPhilippe Mathieu-Daudé MemTxResult res; 74749ab747fSPaolo Bonzini int i; 74849ab747fSPaolo Bonzini 7496a9e5cc6SPhilippe Mathieu-Daudé if (s->trnmod & SDHC_TRNS_BLK_CNT_EN && !s->blkcnt) { 7506a9e5cc6SPhilippe Mathieu-Daudé /* Stop Multiple Transfer */ 7516a9e5cc6SPhilippe Mathieu-Daudé sdhci_end_transfer(s); 7526a9e5cc6SPhilippe Mathieu-Daudé return; 7536a9e5cc6SPhilippe Mathieu-Daudé } 7546a9e5cc6SPhilippe Mathieu-Daudé 75549ab747fSPaolo Bonzini for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) { 75649ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH; 75749ab747fSPaolo Bonzini 75849ab747fSPaolo Bonzini get_adma_description(s, &dscr); 7598be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma_loop(dscr.addr, dscr.length, dscr.attr); 76049ab747fSPaolo Bonzini 76149ab747fSPaolo Bonzini if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) { 76249ab747fSPaolo Bonzini /* Indicate that error occurred in ST_FDS state */ 76349ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_STATE_MASK; 76449ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS; 76549ab747fSPaolo Bonzini 76649ab747fSPaolo Bonzini /* Generate ADMA error interrupt */ 76749ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 76849ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 76949ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 77049ab747fSPaolo Bonzini } 77149ab747fSPaolo Bonzini 77249ab747fSPaolo Bonzini sdhci_update_irq(s); 77349ab747fSPaolo Bonzini return; 77449ab747fSPaolo Bonzini } 77549ab747fSPaolo Bonzini 7764c8f9735SPhilippe Mathieu-Daudé length = dscr.length ? dscr.length : 64 * KiB; 77749ab747fSPaolo Bonzini 77849ab747fSPaolo Bonzini switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) { 77949ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_TRAN: /* data transfer */ 780bc6f2899SBin Meng s->prnsts |= SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE; 78149ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 782bc6f2899SBin Meng s->prnsts |= SDHC_DOING_READ; 78349ab747fSPaolo Bonzini while (length) { 78449ab747fSPaolo Bonzini if (s->data_count == 0) { 785618e0be1SPhilippe Mathieu-Daudé sdbus_read_data(&s->sdbus, s->fifo_buffer, block_size); 78649ab747fSPaolo Bonzini } 78749ab747fSPaolo Bonzini begin = s->data_count; 78849ab747fSPaolo Bonzini if ((length + begin) < block_size) { 78949ab747fSPaolo Bonzini s->data_count = length + begin; 79049ab747fSPaolo Bonzini length = 0; 79149ab747fSPaolo Bonzini } else { 79249ab747fSPaolo Bonzini s->data_count = block_size; 79349ab747fSPaolo Bonzini length -= block_size - begin; 79449ab747fSPaolo Bonzini } 79578e619cbSPhilippe Mathieu-Daudé res = dma_memory_write(s->dma_as, dscr.addr, 79649ab747fSPaolo Bonzini &s->fifo_buffer[begin], 797ba06fe8aSPhilippe Mathieu-Daudé s->data_count - begin, 798799f7f01SPhilippe Mathieu-Daudé attrs); 79978e619cbSPhilippe Mathieu-Daudé if (res != MEMTX_OK) { 80078e619cbSPhilippe Mathieu-Daudé break; 80178e619cbSPhilippe Mathieu-Daudé } 80249ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 80349ab747fSPaolo Bonzini if (s->data_count == block_size) { 80449ab747fSPaolo Bonzini s->data_count = 0; 80549ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 80649ab747fSPaolo Bonzini s->blkcnt--; 80749ab747fSPaolo Bonzini if (s->blkcnt == 0) { 80849ab747fSPaolo Bonzini break; 80949ab747fSPaolo Bonzini } 81049ab747fSPaolo Bonzini } 81149ab747fSPaolo Bonzini } 81249ab747fSPaolo Bonzini } 81349ab747fSPaolo Bonzini } else { 814bc6f2899SBin Meng s->prnsts |= SDHC_DOING_WRITE; 81549ab747fSPaolo Bonzini while (length) { 81649ab747fSPaolo Bonzini begin = s->data_count; 81749ab747fSPaolo Bonzini if ((length + begin) < block_size) { 81849ab747fSPaolo Bonzini s->data_count = length + begin; 81949ab747fSPaolo Bonzini length = 0; 82049ab747fSPaolo Bonzini } else { 82149ab747fSPaolo Bonzini s->data_count = block_size; 82249ab747fSPaolo Bonzini length -= block_size - begin; 82349ab747fSPaolo Bonzini } 82478e619cbSPhilippe Mathieu-Daudé res = dma_memory_read(s->dma_as, dscr.addr, 8259db11cefSPeter Crosthwaite &s->fifo_buffer[begin], 826ba06fe8aSPhilippe Mathieu-Daudé s->data_count - begin, 827799f7f01SPhilippe Mathieu-Daudé attrs); 82878e619cbSPhilippe Mathieu-Daudé if (res != MEMTX_OK) { 82978e619cbSPhilippe Mathieu-Daudé break; 83078e619cbSPhilippe Mathieu-Daudé } 83149ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 83249ab747fSPaolo Bonzini if (s->data_count == block_size) { 83362a21be6SPhilippe Mathieu-Daudé sdbus_write_data(&s->sdbus, s->fifo_buffer, block_size); 83449ab747fSPaolo Bonzini s->data_count = 0; 83549ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 83649ab747fSPaolo Bonzini s->blkcnt--; 83749ab747fSPaolo Bonzini if (s->blkcnt == 0) { 83849ab747fSPaolo Bonzini break; 83949ab747fSPaolo Bonzini } 84049ab747fSPaolo Bonzini } 84149ab747fSPaolo Bonzini } 84249ab747fSPaolo Bonzini } 84349ab747fSPaolo Bonzini } 84478e619cbSPhilippe Mathieu-Daudé if (res != MEMTX_OK) { 84578e619cbSPhilippe Mathieu-Daudé if (s->errintstsen & SDHC_EISEN_ADMAERR) { 84678e619cbSPhilippe Mathieu-Daudé trace_sdhci_error("Set ADMA error flag"); 84778e619cbSPhilippe Mathieu-Daudé s->errintsts |= SDHC_EIS_ADMAERR; 84878e619cbSPhilippe Mathieu-Daudé s->norintsts |= SDHC_NIS_ERR; 84978e619cbSPhilippe Mathieu-Daudé } 85078e619cbSPhilippe Mathieu-Daudé sdhci_update_irq(s); 85178e619cbSPhilippe Mathieu-Daudé } else { 85249ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 85378e619cbSPhilippe Mathieu-Daudé } 85449ab747fSPaolo Bonzini break; 85549ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_LINK: /* link to next descriptor table */ 85649ab747fSPaolo Bonzini s->admasysaddr = dscr.addr; 8578be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma("link", s->admasysaddr); 85849ab747fSPaolo Bonzini break; 85949ab747fSPaolo Bonzini default: 86049ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 86149ab747fSPaolo Bonzini break; 86249ab747fSPaolo Bonzini } 86349ab747fSPaolo Bonzini 8641d32c26fSPeter Crosthwaite if (dscr.attr & SDHC_ADMA_ATTR_INT) { 8658be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma("interrupt", s->admasysaddr); 8661d32c26fSPeter Crosthwaite if (s->norintstsen & SDHC_NISEN_DMA) { 8671d32c26fSPeter Crosthwaite s->norintsts |= SDHC_NIS_DMA; 8681d32c26fSPeter Crosthwaite } 8691d32c26fSPeter Crosthwaite 8709321c1f2SPhilippe Mathieu-Daudé if (sdhci_update_irq(s) && !(dscr.attr & SDHC_ADMA_ATTR_END)) { 8719321c1f2SPhilippe Mathieu-Daudé /* IRQ delivered, reschedule current transfer */ 8729321c1f2SPhilippe Mathieu-Daudé break; 8739321c1f2SPhilippe Mathieu-Daudé } 8741d32c26fSPeter Crosthwaite } 8751d32c26fSPeter Crosthwaite 87649ab747fSPaolo Bonzini /* ADMA transfer terminates if blkcnt == 0 or by END attribute */ 87749ab747fSPaolo Bonzini if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 87849ab747fSPaolo Bonzini (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) { 8798be487d8SPhilippe Mathieu-Daudé trace_sdhci_adma_transfer_completed(); 88049ab747fSPaolo Bonzini if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) && 88149ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 88249ab747fSPaolo Bonzini s->blkcnt != 0)) { 8838be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("SD/MMC host ADMA length mismatch"); 88449ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH | 88549ab747fSPaolo Bonzini SDHC_ADMAERR_STATE_ST_TFR; 88649ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 8878be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Set ADMA error flag"); 88849ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 88949ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 89049ab747fSPaolo Bonzini } 89149ab747fSPaolo Bonzini 89249ab747fSPaolo Bonzini sdhci_update_irq(s); 89349ab747fSPaolo Bonzini } 894d368ba43SKevin O'Connor sdhci_end_transfer(s); 89549ab747fSPaolo Bonzini return; 89649ab747fSPaolo Bonzini } 89749ab747fSPaolo Bonzini 89849ab747fSPaolo Bonzini } 89949ab747fSPaolo Bonzini 90049ab747fSPaolo Bonzini /* we have unfinished business - reschedule to continue ADMA */ 901bc72ad67SAlex Bligh timer_mod(s->transfer_timer, 902bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY); 90349ab747fSPaolo Bonzini } 90449ab747fSPaolo Bonzini 90549ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */ 90649ab747fSPaolo Bonzini 907d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque) 90849ab747fSPaolo Bonzini { 909d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 91049ab747fSPaolo Bonzini 91149ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_DMA) { 91206c5120bSPhilippe Mathieu-Daudé switch (SDHC_DMA_TYPE(s->hostctl1)) { 91349ab747fSPaolo Bonzini case SDHC_CTRL_SDMA: 91449ab747fSPaolo Bonzini if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) { 915d368ba43SKevin O'Connor sdhci_sdma_transfer_single_block(s); 91649ab747fSPaolo Bonzini } else { 917d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 91849ab747fSPaolo Bonzini } 91949ab747fSPaolo Bonzini 92049ab747fSPaolo Bonzini break; 92149ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 9220540fba9SPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_ADMA1_MASK)) { 9238be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("ADMA1 not supported"); 92449ab747fSPaolo Bonzini break; 92549ab747fSPaolo Bonzini } 92649ab747fSPaolo Bonzini 927d368ba43SKevin O'Connor sdhci_do_adma(s); 92849ab747fSPaolo Bonzini break; 92949ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 9300540fba9SPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK)) { 9318be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("ADMA2 not supported"); 93249ab747fSPaolo Bonzini break; 93349ab747fSPaolo Bonzini } 93449ab747fSPaolo Bonzini 935d368ba43SKevin O'Connor sdhci_do_adma(s); 93649ab747fSPaolo Bonzini break; 93749ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 9380540fba9SPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK) || 9390540fba9SPhilippe Mathieu-Daudé !(s->capareg & R_SDHC_CAPAB_BUS64BIT_MASK)) { 9408be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("64 bit ADMA not supported"); 94149ab747fSPaolo Bonzini break; 94249ab747fSPaolo Bonzini } 94349ab747fSPaolo Bonzini 944d368ba43SKevin O'Connor sdhci_do_adma(s); 94549ab747fSPaolo Bonzini break; 94649ab747fSPaolo Bonzini default: 9478be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Unsupported DMA type"); 94849ab747fSPaolo Bonzini break; 94949ab747fSPaolo Bonzini } 95049ab747fSPaolo Bonzini } else { 95140bbc194SPeter Maydell if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) { 95249ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT | 95349ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 954d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 95549ab747fSPaolo Bonzini } else { 95649ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE | 95749ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT; 958d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 95949ab747fSPaolo Bonzini } 96049ab747fSPaolo Bonzini } 96149ab747fSPaolo Bonzini } 96249ab747fSPaolo Bonzini 96349ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s) 96449ab747fSPaolo Bonzini { 9656890a695SPeter Crosthwaite if (!SDHC_CLOCK_IS_ON(s->clkcon) || 96649ab747fSPaolo Bonzini (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) && 96749ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_DATA_PRESENT) || 96849ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY && 96949ab747fSPaolo Bonzini !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) { 97049ab747fSPaolo Bonzini return false; 97149ab747fSPaolo Bonzini } 97249ab747fSPaolo Bonzini 97349ab747fSPaolo Bonzini return true; 97449ab747fSPaolo Bonzini } 97549ab747fSPaolo Bonzini 97649ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and 97749ab747fSPaolo Bonzini * continuous manner */ 97849ab747fSPaolo Bonzini static inline bool 97949ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num) 98049ab747fSPaolo Bonzini { 98149ab747fSPaolo Bonzini if ((s->data_count & 0x3) != byte_num) { 9828be487d8SPhilippe Mathieu-Daudé trace_sdhci_error("Non-sequential access to Buffer Data Port register" 98349ab747fSPaolo Bonzini "is prohibited\n"); 98449ab747fSPaolo Bonzini return false; 98549ab747fSPaolo Bonzini } 98649ab747fSPaolo Bonzini return true; 98749ab747fSPaolo Bonzini } 98849ab747fSPaolo Bonzini 98945e5dc43SPhilippe Mathieu-Daudé static void sdhci_resume_pending_transfer(SDHCIState *s) 99045e5dc43SPhilippe Mathieu-Daudé { 99145e5dc43SPhilippe Mathieu-Daudé timer_del(s->transfer_timer); 99245e5dc43SPhilippe Mathieu-Daudé sdhci_data_transfer(s); 99345e5dc43SPhilippe Mathieu-Daudé } 99445e5dc43SPhilippe Mathieu-Daudé 995d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size) 99649ab747fSPaolo Bonzini { 997d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 99849ab747fSPaolo Bonzini uint32_t ret = 0; 99949ab747fSPaolo Bonzini 100045e5dc43SPhilippe Mathieu-Daudé if (timer_pending(s->transfer_timer)) { 100145e5dc43SPhilippe Mathieu-Daudé sdhci_resume_pending_transfer(s); 100245e5dc43SPhilippe Mathieu-Daudé } 100345e5dc43SPhilippe Mathieu-Daudé 100449ab747fSPaolo Bonzini switch (offset & ~0x3) { 100549ab747fSPaolo Bonzini case SDHC_SYSAD: 100649ab747fSPaolo Bonzini ret = s->sdmasysad; 100749ab747fSPaolo Bonzini break; 100849ab747fSPaolo Bonzini case SDHC_BLKSIZE: 100949ab747fSPaolo Bonzini ret = s->blksize | (s->blkcnt << 16); 101049ab747fSPaolo Bonzini break; 101149ab747fSPaolo Bonzini case SDHC_ARGUMENT: 101249ab747fSPaolo Bonzini ret = s->argument; 101349ab747fSPaolo Bonzini break; 101449ab747fSPaolo Bonzini case SDHC_TRNMOD: 101549ab747fSPaolo Bonzini ret = s->trnmod | (s->cmdreg << 16); 101649ab747fSPaolo Bonzini break; 101749ab747fSPaolo Bonzini case SDHC_RSPREG0 ... SDHC_RSPREG3: 101849ab747fSPaolo Bonzini ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2]; 101949ab747fSPaolo Bonzini break; 102049ab747fSPaolo Bonzini case SDHC_BDATA: 102149ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 1022d368ba43SKevin O'Connor ret = sdhci_read_dataport(s, size); 10238be487d8SPhilippe Mathieu-Daudé trace_sdhci_access("rd", size << 3, offset, "->", ret, ret); 102449ab747fSPaolo Bonzini return ret; 102549ab747fSPaolo Bonzini } 102649ab747fSPaolo Bonzini break; 102749ab747fSPaolo Bonzini case SDHC_PRNSTS: 102849ab747fSPaolo Bonzini ret = s->prnsts; 1029da346922SPhilippe Mathieu-Daudé ret = FIELD_DP32(ret, SDHC_PRNSTS, DAT_LVL, 1030da346922SPhilippe Mathieu-Daudé sdbus_get_dat_lines(&s->sdbus)); 1031da346922SPhilippe Mathieu-Daudé ret = FIELD_DP32(ret, SDHC_PRNSTS, CMD_LVL, 1032da346922SPhilippe Mathieu-Daudé sdbus_get_cmd_line(&s->sdbus)); 103349ab747fSPaolo Bonzini break; 103449ab747fSPaolo Bonzini case SDHC_HOSTCTL: 103506c5120bSPhilippe Mathieu-Daudé ret = s->hostctl1 | (s->pwrcon << 8) | (s->blkgap << 16) | 103649ab747fSPaolo Bonzini (s->wakcon << 24); 103749ab747fSPaolo Bonzini break; 103849ab747fSPaolo Bonzini case SDHC_CLKCON: 103949ab747fSPaolo Bonzini ret = s->clkcon | (s->timeoutcon << 16); 104049ab747fSPaolo Bonzini break; 104149ab747fSPaolo Bonzini case SDHC_NORINTSTS: 104249ab747fSPaolo Bonzini ret = s->norintsts | (s->errintsts << 16); 104349ab747fSPaolo Bonzini break; 104449ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 104549ab747fSPaolo Bonzini ret = s->norintstsen | (s->errintstsen << 16); 104649ab747fSPaolo Bonzini break; 104749ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 104849ab747fSPaolo Bonzini ret = s->norintsigen | (s->errintsigen << 16); 104949ab747fSPaolo Bonzini break; 105049ab747fSPaolo Bonzini case SDHC_ACMD12ERRSTS: 1051ea55a221SPhilippe Mathieu-Daudé ret = s->acmd12errsts | (s->hostctl2 << 16); 105249ab747fSPaolo Bonzini break; 1053cd209421SPhilippe Mathieu-Daudé case SDHC_CAPAB: 10545efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)s->capareg; 10555efc9016SPhilippe Mathieu-Daudé break; 10565efc9016SPhilippe Mathieu-Daudé case SDHC_CAPAB + 4: 10575efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)(s->capareg >> 32); 105849ab747fSPaolo Bonzini break; 105949ab747fSPaolo Bonzini case SDHC_MAXCURR: 10605efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)s->maxcurr; 10615efc9016SPhilippe Mathieu-Daudé break; 10625efc9016SPhilippe Mathieu-Daudé case SDHC_MAXCURR + 4: 10635efc9016SPhilippe Mathieu-Daudé ret = (uint32_t)(s->maxcurr >> 32); 106449ab747fSPaolo Bonzini break; 106549ab747fSPaolo Bonzini case SDHC_ADMAERR: 106649ab747fSPaolo Bonzini ret = s->admaerr; 106749ab747fSPaolo Bonzini break; 106849ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 106949ab747fSPaolo Bonzini ret = (uint32_t)s->admasysaddr; 107049ab747fSPaolo Bonzini break; 107149ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 107249ab747fSPaolo Bonzini ret = (uint32_t)(s->admasysaddr >> 32); 107349ab747fSPaolo Bonzini break; 107449ab747fSPaolo Bonzini case SDHC_SLOT_INT_STATUS: 1075aceb5b06SPhilippe Mathieu-Daudé ret = (s->version << 16) | sdhci_slotint(s); 107649ab747fSPaolo Bonzini break; 107749ab747fSPaolo Bonzini default: 107800b004b3SPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "SDHC rd_%ub @0x%02" HWADDR_PRIx " " 107900b004b3SPhilippe Mathieu-Daudé "not implemented\n", size, offset); 108049ab747fSPaolo Bonzini break; 108149ab747fSPaolo Bonzini } 108249ab747fSPaolo Bonzini 108349ab747fSPaolo Bonzini ret >>= (offset & 0x3) * 8; 108449ab747fSPaolo Bonzini ret &= (1ULL << (size * 8)) - 1; 10858be487d8SPhilippe Mathieu-Daudé trace_sdhci_access("rd", size << 3, offset, "->", ret, ret); 108649ab747fSPaolo Bonzini return ret; 108749ab747fSPaolo Bonzini } 108849ab747fSPaolo Bonzini 108949ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value) 109049ab747fSPaolo Bonzini { 109149ab747fSPaolo Bonzini if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) { 109249ab747fSPaolo Bonzini return; 109349ab747fSPaolo Bonzini } 109449ab747fSPaolo Bonzini s->blkgap = value & SDHC_STOP_AT_GAP_REQ; 109549ab747fSPaolo Bonzini 109649ab747fSPaolo Bonzini if ((value & SDHC_CONTINUE_REQ) && s->stopped_state && 109749ab747fSPaolo Bonzini (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) { 109849ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read) { 109949ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ; 1100d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 110149ab747fSPaolo Bonzini } else { 110249ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE; 1103d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 110449ab747fSPaolo Bonzini } 110549ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 110649ab747fSPaolo Bonzini } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) { 110749ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_READ) { 110849ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_read; 110949ab747fSPaolo Bonzini } else if (s->prnsts & SDHC_DOING_WRITE) { 111049ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_write; 111149ab747fSPaolo Bonzini } 111249ab747fSPaolo Bonzini } 111349ab747fSPaolo Bonzini } 111449ab747fSPaolo Bonzini 111549ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value) 111649ab747fSPaolo Bonzini { 111749ab747fSPaolo Bonzini switch (value) { 111849ab747fSPaolo Bonzini case SDHC_RESET_ALL: 1119d368ba43SKevin O'Connor sdhci_reset(s); 112049ab747fSPaolo Bonzini break; 112149ab747fSPaolo Bonzini case SDHC_RESET_CMD: 112249ab747fSPaolo Bonzini s->prnsts &= ~SDHC_CMD_INHIBIT; 112349ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_CMDCMP; 112449ab747fSPaolo Bonzini break; 112549ab747fSPaolo Bonzini case SDHC_RESET_DATA: 112649ab747fSPaolo Bonzini s->data_count = 0; 112749ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE | 112849ab747fSPaolo Bonzini SDHC_DOING_READ | SDHC_DOING_WRITE | 112949ab747fSPaolo Bonzini SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE); 113049ab747fSPaolo Bonzini s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ); 113149ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 113249ab747fSPaolo Bonzini s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY | 113349ab747fSPaolo Bonzini SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP); 113449ab747fSPaolo Bonzini break; 113549ab747fSPaolo Bonzini } 113649ab747fSPaolo Bonzini } 113749ab747fSPaolo Bonzini 113849ab747fSPaolo Bonzini static void 1139d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) 114049ab747fSPaolo Bonzini { 1141d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 114249ab747fSPaolo Bonzini unsigned shift = 8 * (offset & 0x3); 114349ab747fSPaolo Bonzini uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift); 1144d368ba43SKevin O'Connor uint32_t value = val; 114549ab747fSPaolo Bonzini value <<= shift; 114649ab747fSPaolo Bonzini 114745e5dc43SPhilippe Mathieu-Daudé if (timer_pending(s->transfer_timer)) { 114845e5dc43SPhilippe Mathieu-Daudé sdhci_resume_pending_transfer(s); 114945e5dc43SPhilippe Mathieu-Daudé } 115045e5dc43SPhilippe Mathieu-Daudé 115149ab747fSPaolo Bonzini switch (offset & ~0x3) { 115249ab747fSPaolo Bonzini case SDHC_SYSAD: 11538be45cc9SBin Meng if (!TRANSFERRING_DATA(s->prnsts)) { 115449ab747fSPaolo Bonzini s->sdmasysad = (s->sdmasysad & mask) | value; 115549ab747fSPaolo Bonzini MASKED_WRITE(s->sdmasysad, mask, value); 115649ab747fSPaolo Bonzini /* Writing to last byte of sdmasysad might trigger transfer */ 11578be45cc9SBin Meng if (!(mask & 0xFF000000) && s->blkcnt && s->blksize && 11588be45cc9SBin Meng SDHC_DMA_TYPE(s->hostctl1) == SDHC_CTRL_SDMA) { 115945ba9f76SPrasad J Pandit if (s->trnmod & SDHC_TRNS_MULTI) { 1160d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 116145ba9f76SPrasad J Pandit } else { 116245ba9f76SPrasad J Pandit sdhci_sdma_transfer_single_block(s); 116345ba9f76SPrasad J Pandit } 116449ab747fSPaolo Bonzini } 11658be45cc9SBin Meng } 116649ab747fSPaolo Bonzini break; 116749ab747fSPaolo Bonzini case SDHC_BLKSIZE: 116849ab747fSPaolo Bonzini if (!TRANSFERRING_DATA(s->prnsts)) { 1169cffb446eSBin Meng uint16_t blksize = s->blksize; 1170cffb446eSBin Meng 1171dfba99f1SPhilippe Mathieu-Daudé MASKED_WRITE(s->blksize, mask, extract32(value, 0, 12)); 117249ab747fSPaolo Bonzini MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16); 11739201bb9aSAlistair Francis 11749201bb9aSAlistair Francis /* Limit block size to the maximum buffer size */ 11759201bb9aSAlistair Francis if (extract32(s->blksize, 0, 12) > s->buf_maxsz) { 117678ee6bd0SPhilippe Mathieu-Daudé qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than " 11779227cc52SPhilippe Mathieu-Daudé "the maximum buffer 0x%x\n", __func__, s->blksize, 11789201bb9aSAlistair Francis s->buf_maxsz); 11799201bb9aSAlistair Francis 11809201bb9aSAlistair Francis s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz); 11819201bb9aSAlistair Francis } 1182cffb446eSBin Meng 1183cffb446eSBin Meng /* 1184cffb446eSBin Meng * If the block size is programmed to a different value from 1185cffb446eSBin Meng * the previous one, reset the data pointer of s->fifo_buffer[] 1186cffb446eSBin Meng * so that s->fifo_buffer[] can be filled in using the new block 1187cffb446eSBin Meng * size in the next transfer. 1188cffb446eSBin Meng */ 1189cffb446eSBin Meng if (blksize != s->blksize) { 1190cffb446eSBin Meng s->data_count = 0; 1191cffb446eSBin Meng } 11925cd7aa34SBin Meng } 11939201bb9aSAlistair Francis 119449ab747fSPaolo Bonzini break; 119549ab747fSPaolo Bonzini case SDHC_ARGUMENT: 119649ab747fSPaolo Bonzini MASKED_WRITE(s->argument, mask, value); 119749ab747fSPaolo Bonzini break; 119849ab747fSPaolo Bonzini case SDHC_TRNMOD: 119949ab747fSPaolo Bonzini /* DMA can be enabled only if it is supported as indicated by 120049ab747fSPaolo Bonzini * capabilities register */ 12016ff37c3dSPhilippe Mathieu-Daudé if (!(s->capareg & R_SDHC_CAPAB_SDMA_MASK)) { 120249ab747fSPaolo Bonzini value &= ~SDHC_TRNS_DMA; 120349ab747fSPaolo Bonzini } 120424bddf9dSPhilippe Mathieu-Daudé MASKED_WRITE(s->trnmod, mask, value & SDHC_TRNMOD_MASK); 120549ab747fSPaolo Bonzini MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16); 120649ab747fSPaolo Bonzini 120749ab747fSPaolo Bonzini /* Writing to the upper byte of CMDREG triggers SD command generation */ 1208d368ba43SKevin O'Connor if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) { 120949ab747fSPaolo Bonzini break; 121049ab747fSPaolo Bonzini } 121149ab747fSPaolo Bonzini 1212d368ba43SKevin O'Connor sdhci_send_command(s); 121349ab747fSPaolo Bonzini break; 121449ab747fSPaolo Bonzini case SDHC_BDATA: 121549ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 1216d368ba43SKevin O'Connor sdhci_write_dataport(s, value >> shift, size); 121749ab747fSPaolo Bonzini } 121849ab747fSPaolo Bonzini break; 121949ab747fSPaolo Bonzini case SDHC_HOSTCTL: 122049ab747fSPaolo Bonzini if (!(mask & 0xFF0000)) { 122149ab747fSPaolo Bonzini sdhci_blkgap_write(s, value >> 16); 122249ab747fSPaolo Bonzini } 122306c5120bSPhilippe Mathieu-Daudé MASKED_WRITE(s->hostctl1, mask, value); 122449ab747fSPaolo Bonzini MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8); 122549ab747fSPaolo Bonzini MASKED_WRITE(s->wakcon, mask >> 24, value >> 24); 122649ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 || 122749ab747fSPaolo Bonzini !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) { 122849ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 122949ab747fSPaolo Bonzini } 123049ab747fSPaolo Bonzini break; 123149ab747fSPaolo Bonzini case SDHC_CLKCON: 123249ab747fSPaolo Bonzini if (!(mask & 0xFF000000)) { 123349ab747fSPaolo Bonzini sdhci_reset_write(s, value >> 24); 123449ab747fSPaolo Bonzini } 123549ab747fSPaolo Bonzini MASKED_WRITE(s->clkcon, mask, value); 123649ab747fSPaolo Bonzini MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16); 123749ab747fSPaolo Bonzini if (s->clkcon & SDHC_CLOCK_INT_EN) { 123849ab747fSPaolo Bonzini s->clkcon |= SDHC_CLOCK_INT_STABLE; 123949ab747fSPaolo Bonzini } else { 124049ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_INT_STABLE; 124149ab747fSPaolo Bonzini } 124249ab747fSPaolo Bonzini break; 124349ab747fSPaolo Bonzini case SDHC_NORINTSTS: 124449ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CARDINT) { 124549ab747fSPaolo Bonzini value &= ~SDHC_NIS_CARDINT; 124649ab747fSPaolo Bonzini } 124749ab747fSPaolo Bonzini s->norintsts &= mask | ~value; 124849ab747fSPaolo Bonzini s->errintsts &= (mask >> 16) | ~(value >> 16); 124949ab747fSPaolo Bonzini if (s->errintsts) { 125049ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 125149ab747fSPaolo Bonzini } else { 125249ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 125349ab747fSPaolo Bonzini } 125449ab747fSPaolo Bonzini sdhci_update_irq(s); 125549ab747fSPaolo Bonzini break; 125649ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 125749ab747fSPaolo Bonzini MASKED_WRITE(s->norintstsen, mask, value); 125849ab747fSPaolo Bonzini MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16); 125949ab747fSPaolo Bonzini s->norintsts &= s->norintstsen; 126049ab747fSPaolo Bonzini s->errintsts &= s->errintstsen; 126149ab747fSPaolo Bonzini if (s->errintsts) { 126249ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 126349ab747fSPaolo Bonzini } else { 126449ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 126549ab747fSPaolo Bonzini } 12660a7ac9f9SAndrew Baumann /* Quirk for Raspberry Pi: pending card insert interrupt 12670a7ac9f9SAndrew Baumann * appears when first enabled after power on */ 12680a7ac9f9SAndrew Baumann if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) { 12690a7ac9f9SAndrew Baumann assert(s->pending_insert_quirk); 12700a7ac9f9SAndrew Baumann s->norintsts |= SDHC_NIS_INSERT; 12710a7ac9f9SAndrew Baumann s->pending_insert_state = false; 12720a7ac9f9SAndrew Baumann } 127349ab747fSPaolo Bonzini sdhci_update_irq(s); 127449ab747fSPaolo Bonzini break; 127549ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 127649ab747fSPaolo Bonzini MASKED_WRITE(s->norintsigen, mask, value); 127749ab747fSPaolo Bonzini MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16); 127849ab747fSPaolo Bonzini sdhci_update_irq(s); 127949ab747fSPaolo Bonzini break; 128049ab747fSPaolo Bonzini case SDHC_ADMAERR: 128149ab747fSPaolo Bonzini MASKED_WRITE(s->admaerr, mask, value); 128249ab747fSPaolo Bonzini break; 128349ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 128449ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL | 128549ab747fSPaolo Bonzini (uint64_t)mask)) | (uint64_t)value; 128649ab747fSPaolo Bonzini break; 128749ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 128849ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL | 128949ab747fSPaolo Bonzini ((uint64_t)mask << 32))) | ((uint64_t)value << 32); 129049ab747fSPaolo Bonzini break; 129149ab747fSPaolo Bonzini case SDHC_FEAER: 129249ab747fSPaolo Bonzini s->acmd12errsts |= value; 129349ab747fSPaolo Bonzini s->errintsts |= (value >> 16) & s->errintstsen; 129449ab747fSPaolo Bonzini if (s->acmd12errsts) { 129549ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMD12ERR; 129649ab747fSPaolo Bonzini } 129749ab747fSPaolo Bonzini if (s->errintsts) { 129849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 129949ab747fSPaolo Bonzini } 130049ab747fSPaolo Bonzini sdhci_update_irq(s); 130149ab747fSPaolo Bonzini break; 13025d2c0464SAndrey Smirnov case SDHC_ACMD12ERRSTS: 13030034ebe6SPhilippe Mathieu-Daudé MASKED_WRITE(s->acmd12errsts, mask, value & UINT16_MAX); 13040034ebe6SPhilippe Mathieu-Daudé if (s->uhs_mode >= UHS_I) { 13050034ebe6SPhilippe Mathieu-Daudé MASKED_WRITE(s->hostctl2, mask >> 16, value >> 16); 13060034ebe6SPhilippe Mathieu-Daudé 13070034ebe6SPhilippe Mathieu-Daudé if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, V18_ENA)) { 13080034ebe6SPhilippe Mathieu-Daudé sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_1_8V); 13090034ebe6SPhilippe Mathieu-Daudé } else { 13100034ebe6SPhilippe Mathieu-Daudé sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_3_3V); 13110034ebe6SPhilippe Mathieu-Daudé } 13120034ebe6SPhilippe Mathieu-Daudé } 13135d2c0464SAndrey Smirnov break; 13145efc9016SPhilippe Mathieu-Daudé 13155efc9016SPhilippe Mathieu-Daudé case SDHC_CAPAB: 13165efc9016SPhilippe Mathieu-Daudé case SDHC_CAPAB + 4: 13175efc9016SPhilippe Mathieu-Daudé case SDHC_MAXCURR: 13185efc9016SPhilippe Mathieu-Daudé case SDHC_MAXCURR + 4: 13195efc9016SPhilippe Mathieu-Daudé qemu_log_mask(LOG_GUEST_ERROR, "SDHC wr_%ub @0x%02" HWADDR_PRIx 13205efc9016SPhilippe Mathieu-Daudé " <- 0x%08x read-only\n", size, offset, value >> shift); 13215efc9016SPhilippe Mathieu-Daudé break; 13225efc9016SPhilippe Mathieu-Daudé 132349ab747fSPaolo Bonzini default: 132400b004b3SPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "SDHC wr_%ub @0x%02" HWADDR_PRIx " <- 0x%08x " 132500b004b3SPhilippe Mathieu-Daudé "not implemented\n", size, offset, value >> shift); 132649ab747fSPaolo Bonzini break; 132749ab747fSPaolo Bonzini } 13288be487d8SPhilippe Mathieu-Daudé trace_sdhci_access("wr", size << 3, offset, "<-", 13298be487d8SPhilippe Mathieu-Daudé value >> shift, value >> shift); 133049ab747fSPaolo Bonzini } 133149ab747fSPaolo Bonzini 133249ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = { 1333d368ba43SKevin O'Connor .read = sdhci_read, 1334d368ba43SKevin O'Connor .write = sdhci_write, 133549ab747fSPaolo Bonzini .valid = { 133649ab747fSPaolo Bonzini .min_access_size = 1, 133749ab747fSPaolo Bonzini .max_access_size = 4, 133849ab747fSPaolo Bonzini .unaligned = false 133949ab747fSPaolo Bonzini }, 134049ab747fSPaolo Bonzini .endianness = DEVICE_LITTLE_ENDIAN, 134149ab747fSPaolo Bonzini }; 134249ab747fSPaolo Bonzini 1343aceb5b06SPhilippe Mathieu-Daudé static void sdhci_init_readonly_registers(SDHCIState *s, Error **errp) 1344aceb5b06SPhilippe Mathieu-Daudé { 1345de1b3800SVladimir Sementsov-Ogievskiy ERRP_GUARD(); 13466ff37c3dSPhilippe Mathieu-Daudé 13474d67852dSPhilippe Mathieu-Daudé switch (s->sd_spec_version) { 13484d67852dSPhilippe Mathieu-Daudé case 2 ... 3: 13494d67852dSPhilippe Mathieu-Daudé break; 13504d67852dSPhilippe Mathieu-Daudé default: 13514d67852dSPhilippe Mathieu-Daudé error_setg(errp, "Only Spec v2/v3 are supported"); 1352aceb5b06SPhilippe Mathieu-Daudé return; 1353aceb5b06SPhilippe Mathieu-Daudé } 1354aceb5b06SPhilippe Mathieu-Daudé s->version = (SDHC_HCVER_VENDOR << 8) | (s->sd_spec_version - 1); 13556ff37c3dSPhilippe Mathieu-Daudé 1356de1b3800SVladimir Sementsov-Ogievskiy sdhci_check_capareg(s, errp); 1357de1b3800SVladimir Sementsov-Ogievskiy if (*errp) { 13586ff37c3dSPhilippe Mathieu-Daudé return; 13596ff37c3dSPhilippe Mathieu-Daudé } 1360aceb5b06SPhilippe Mathieu-Daudé } 1361aceb5b06SPhilippe Mathieu-Daudé 1362b635d98cSPhilippe Mathieu-Daudé /* --- qdev common --- */ 1363b635d98cSPhilippe Mathieu-Daudé 1364ce864603SThomas Huth void sdhci_initfn(SDHCIState *s) 136549ab747fSPaolo Bonzini { 1366d637e1dcSPeter Maydell qbus_init(&s->sdbus, sizeof(s->sdbus), TYPE_SDHCI_BUS, DEVICE(s), "sd-bus"); 136749ab747fSPaolo Bonzini 1368bc72ad67SAlex Bligh s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s); 1369d368ba43SKevin O'Connor s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s); 1370fd1e5c81SAndrey Smirnov 1371fd1e5c81SAndrey Smirnov s->io_ops = &sdhci_mmio_ops; 137249ab747fSPaolo Bonzini } 137349ab747fSPaolo Bonzini 1374ce864603SThomas Huth void sdhci_uninitfn(SDHCIState *s) 137549ab747fSPaolo Bonzini { 1376bc72ad67SAlex Bligh timer_free(s->insert_timer); 1377bc72ad67SAlex Bligh timer_free(s->transfer_timer); 137849ab747fSPaolo Bonzini 137949ab747fSPaolo Bonzini g_free(s->fifo_buffer); 138049ab747fSPaolo Bonzini s->fifo_buffer = NULL; 138149ab747fSPaolo Bonzini } 138249ab747fSPaolo Bonzini 1383ce864603SThomas Huth void sdhci_common_realize(SDHCIState *s, Error **errp) 138425367498SPhilippe Mathieu-Daudé { 1385de1b3800SVladimir Sementsov-Ogievskiy ERRP_GUARD(); 1386aceb5b06SPhilippe Mathieu-Daudé 1387de1b3800SVladimir Sementsov-Ogievskiy sdhci_init_readonly_registers(s, errp); 1388de1b3800SVladimir Sementsov-Ogievskiy if (*errp) { 1389aceb5b06SPhilippe Mathieu-Daudé return; 1390aceb5b06SPhilippe Mathieu-Daudé } 139125367498SPhilippe Mathieu-Daudé s->buf_maxsz = sdhci_get_fifolen(s); 139225367498SPhilippe Mathieu-Daudé s->fifo_buffer = g_malloc0(s->buf_maxsz); 139325367498SPhilippe Mathieu-Daudé 1394c0983085SPeter Maydell memory_region_init_io(&s->iomem, OBJECT(s), s->io_ops, s, "sdhci", 139525367498SPhilippe Mathieu-Daudé SDHC_REGISTERS_MAP_SIZE); 139625367498SPhilippe Mathieu-Daudé } 139725367498SPhilippe Mathieu-Daudé 1398b69c3c21SMarkus Armbruster void sdhci_common_unrealize(SDHCIState *s) 13998b7455c7SPhilippe Mathieu-Daudé { 14008b7455c7SPhilippe Mathieu-Daudé /* This function is expected to be called only once for each class: 14018b7455c7SPhilippe Mathieu-Daudé * - SysBus: via DeviceClass->unrealize(), 14028b7455c7SPhilippe Mathieu-Daudé * - PCI: via PCIDeviceClass->exit(). 14038b7455c7SPhilippe Mathieu-Daudé * However to avoid double-free and/or use-after-free we still nullify 14048b7455c7SPhilippe Mathieu-Daudé * this variable (better safe than sorry!). */ 14058b7455c7SPhilippe Mathieu-Daudé g_free(s->fifo_buffer); 14068b7455c7SPhilippe Mathieu-Daudé s->fifo_buffer = NULL; 14078b7455c7SPhilippe Mathieu-Daudé } 14088b7455c7SPhilippe Mathieu-Daudé 14090a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque) 14100a7ac9f9SAndrew Baumann { 14110a7ac9f9SAndrew Baumann SDHCIState *s = opaque; 14120a7ac9f9SAndrew Baumann 14130a7ac9f9SAndrew Baumann return s->pending_insert_state; 14140a7ac9f9SAndrew Baumann } 14150a7ac9f9SAndrew Baumann 14160a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = { 14170a7ac9f9SAndrew Baumann .name = "sdhci/pending-insert", 14180a7ac9f9SAndrew Baumann .version_id = 1, 14190a7ac9f9SAndrew Baumann .minimum_version_id = 1, 14200a7ac9f9SAndrew Baumann .needed = sdhci_pending_insert_vmstate_needed, 14210a7ac9f9SAndrew Baumann .fields = (VMStateField[]) { 14220a7ac9f9SAndrew Baumann VMSTATE_BOOL(pending_insert_state, SDHCIState), 14230a7ac9f9SAndrew Baumann VMSTATE_END_OF_LIST() 14240a7ac9f9SAndrew Baumann }, 14250a7ac9f9SAndrew Baumann }; 14260a7ac9f9SAndrew Baumann 142749ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = { 142849ab747fSPaolo Bonzini .name = "sdhci", 142949ab747fSPaolo Bonzini .version_id = 1, 143049ab747fSPaolo Bonzini .minimum_version_id = 1, 143149ab747fSPaolo Bonzini .fields = (VMStateField[]) { 143249ab747fSPaolo Bonzini VMSTATE_UINT32(sdmasysad, SDHCIState), 143349ab747fSPaolo Bonzini VMSTATE_UINT16(blksize, SDHCIState), 143449ab747fSPaolo Bonzini VMSTATE_UINT16(blkcnt, SDHCIState), 143549ab747fSPaolo Bonzini VMSTATE_UINT32(argument, SDHCIState), 143649ab747fSPaolo Bonzini VMSTATE_UINT16(trnmod, SDHCIState), 143749ab747fSPaolo Bonzini VMSTATE_UINT16(cmdreg, SDHCIState), 143849ab747fSPaolo Bonzini VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4), 143949ab747fSPaolo Bonzini VMSTATE_UINT32(prnsts, SDHCIState), 144006c5120bSPhilippe Mathieu-Daudé VMSTATE_UINT8(hostctl1, SDHCIState), 144149ab747fSPaolo Bonzini VMSTATE_UINT8(pwrcon, SDHCIState), 144249ab747fSPaolo Bonzini VMSTATE_UINT8(blkgap, SDHCIState), 144349ab747fSPaolo Bonzini VMSTATE_UINT8(wakcon, SDHCIState), 144449ab747fSPaolo Bonzini VMSTATE_UINT16(clkcon, SDHCIState), 144549ab747fSPaolo Bonzini VMSTATE_UINT8(timeoutcon, SDHCIState), 144649ab747fSPaolo Bonzini VMSTATE_UINT8(admaerr, SDHCIState), 144749ab747fSPaolo Bonzini VMSTATE_UINT16(norintsts, SDHCIState), 144849ab747fSPaolo Bonzini VMSTATE_UINT16(errintsts, SDHCIState), 144949ab747fSPaolo Bonzini VMSTATE_UINT16(norintstsen, SDHCIState), 145049ab747fSPaolo Bonzini VMSTATE_UINT16(errintstsen, SDHCIState), 145149ab747fSPaolo Bonzini VMSTATE_UINT16(norintsigen, SDHCIState), 145249ab747fSPaolo Bonzini VMSTATE_UINT16(errintsigen, SDHCIState), 145349ab747fSPaolo Bonzini VMSTATE_UINT16(acmd12errsts, SDHCIState), 145449ab747fSPaolo Bonzini VMSTATE_UINT16(data_count, SDHCIState), 145549ab747fSPaolo Bonzini VMSTATE_UINT64(admasysaddr, SDHCIState), 145649ab747fSPaolo Bonzini VMSTATE_UINT8(stopped_state, SDHCIState), 145759046ec2SHalil Pasic VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz), 1458e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(insert_timer, SDHCIState), 1459e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(transfer_timer, SDHCIState), 146049ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 14610a7ac9f9SAndrew Baumann }, 14620a7ac9f9SAndrew Baumann .subsections = (const VMStateDescription*[]) { 14630a7ac9f9SAndrew Baumann &sdhci_pending_insert_vmstate, 14640a7ac9f9SAndrew Baumann NULL 14650a7ac9f9SAndrew Baumann }, 146649ab747fSPaolo Bonzini }; 146749ab747fSPaolo Bonzini 1468ce864603SThomas Huth void sdhci_common_class_init(ObjectClass *klass, void *data) 14691c92c505SPhilippe Mathieu-Daudé { 14701c92c505SPhilippe Mathieu-Daudé DeviceClass *dc = DEVICE_CLASS(klass); 14711c92c505SPhilippe Mathieu-Daudé 14721c92c505SPhilippe Mathieu-Daudé set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 14731c92c505SPhilippe Mathieu-Daudé dc->vmsd = &sdhci_vmstate; 14741c92c505SPhilippe Mathieu-Daudé dc->reset = sdhci_poweron_reset; 14751c92c505SPhilippe Mathieu-Daudé } 14761c92c505SPhilippe Mathieu-Daudé 1477b635d98cSPhilippe Mathieu-Daudé /* --- qdev SysBus --- */ 1478b635d98cSPhilippe Mathieu-Daudé 14795ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = { 1480b635d98cSPhilippe Mathieu-Daudé DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState), 14810a7ac9f9SAndrew Baumann DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk, 14820a7ac9f9SAndrew Baumann false), 148360765b6cSPhilippe Mathieu-Daudé DEFINE_PROP_LINK("dma", SDHCIState, 148460765b6cSPhilippe Mathieu-Daudé dma_mr, TYPE_MEMORY_REGION, MemoryRegion *), 14855ec911c3SKevin O'Connor DEFINE_PROP_END_OF_LIST(), 14865ec911c3SKevin O'Connor }; 14875ec911c3SKevin O'Connor 14887302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj) 148949ab747fSPaolo Bonzini { 14907302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 14915ec911c3SKevin O'Connor 149240bbc194SPeter Maydell sdhci_initfn(s); 14937302dcd6SKevin O'Connor } 14947302dcd6SKevin O'Connor 14957302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj) 14967302dcd6SKevin O'Connor { 14977302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 149860765b6cSPhilippe Mathieu-Daudé 149960765b6cSPhilippe Mathieu-Daudé if (s->dma_mr) { 150060765b6cSPhilippe Mathieu-Daudé object_unparent(OBJECT(s->dma_mr)); 150160765b6cSPhilippe Mathieu-Daudé } 150260765b6cSPhilippe Mathieu-Daudé 15037302dcd6SKevin O'Connor sdhci_uninitfn(s); 15047302dcd6SKevin O'Connor } 15057302dcd6SKevin O'Connor 15067302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error **errp) 15077302dcd6SKevin O'Connor { 1508de1b3800SVladimir Sementsov-Ogievskiy ERRP_GUARD(); 15097302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(dev); 151049ab747fSPaolo Bonzini SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 151149ab747fSPaolo Bonzini 1512de1b3800SVladimir Sementsov-Ogievskiy sdhci_common_realize(s, errp); 1513de1b3800SVladimir Sementsov-Ogievskiy if (*errp) { 151425367498SPhilippe Mathieu-Daudé return; 151525367498SPhilippe Mathieu-Daudé } 151625367498SPhilippe Mathieu-Daudé 151760765b6cSPhilippe Mathieu-Daudé if (s->dma_mr) { 151802e57e1cSPhilippe Mathieu-Daudé s->dma_as = &s->sysbus_dma_as; 151960765b6cSPhilippe Mathieu-Daudé address_space_init(s->dma_as, s->dma_mr, "sdhci-dma"); 152060765b6cSPhilippe Mathieu-Daudé } else { 152160765b6cSPhilippe Mathieu-Daudé /* use system_memory() if property "dma" not set */ 1522dd55c485SPhilippe Mathieu-Daudé s->dma_as = &address_space_memory; 152360765b6cSPhilippe Mathieu-Daudé } 1524dd55c485SPhilippe Mathieu-Daudé 152549ab747fSPaolo Bonzini sysbus_init_irq(sbd, &s->irq); 1526fd1e5c81SAndrey Smirnov 152749ab747fSPaolo Bonzini sysbus_init_mmio(sbd, &s->iomem); 152849ab747fSPaolo Bonzini } 152949ab747fSPaolo Bonzini 1530b69c3c21SMarkus Armbruster static void sdhci_sysbus_unrealize(DeviceState *dev) 15318b7455c7SPhilippe Mathieu-Daudé { 15328b7455c7SPhilippe Mathieu-Daudé SDHCIState *s = SYSBUS_SDHCI(dev); 15338b7455c7SPhilippe Mathieu-Daudé 1534b69c3c21SMarkus Armbruster sdhci_common_unrealize(s); 153560765b6cSPhilippe Mathieu-Daudé 153660765b6cSPhilippe Mathieu-Daudé if (s->dma_mr) { 153760765b6cSPhilippe Mathieu-Daudé address_space_destroy(s->dma_as); 153860765b6cSPhilippe Mathieu-Daudé } 15398b7455c7SPhilippe Mathieu-Daudé } 15408b7455c7SPhilippe Mathieu-Daudé 15417302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data) 154249ab747fSPaolo Bonzini { 154349ab747fSPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass); 154449ab747fSPaolo Bonzini 15454f67d30bSMarc-André Lureau device_class_set_props(dc, sdhci_sysbus_properties); 15467302dcd6SKevin O'Connor dc->realize = sdhci_sysbus_realize; 15478b7455c7SPhilippe Mathieu-Daudé dc->unrealize = sdhci_sysbus_unrealize; 15481c92c505SPhilippe Mathieu-Daudé 15491c92c505SPhilippe Mathieu-Daudé sdhci_common_class_init(klass, data); 155049ab747fSPaolo Bonzini } 155149ab747fSPaolo Bonzini 15527302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = { 15537302dcd6SKevin O'Connor .name = TYPE_SYSBUS_SDHCI, 155449ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 155549ab747fSPaolo Bonzini .instance_size = sizeof(SDHCIState), 15567302dcd6SKevin O'Connor .instance_init = sdhci_sysbus_init, 15577302dcd6SKevin O'Connor .instance_finalize = sdhci_sysbus_finalize, 15587302dcd6SKevin O'Connor .class_init = sdhci_sysbus_class_init, 155949ab747fSPaolo Bonzini }; 156049ab747fSPaolo Bonzini 1561b635d98cSPhilippe Mathieu-Daudé /* --- qdev bus master --- */ 1562b635d98cSPhilippe Mathieu-Daudé 156340bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data) 156440bbc194SPeter Maydell { 156540bbc194SPeter Maydell SDBusClass *sbc = SD_BUS_CLASS(klass); 156640bbc194SPeter Maydell 156740bbc194SPeter Maydell sbc->set_inserted = sdhci_set_inserted; 156840bbc194SPeter Maydell sbc->set_readonly = sdhci_set_readonly; 156940bbc194SPeter Maydell } 157040bbc194SPeter Maydell 157140bbc194SPeter Maydell static const TypeInfo sdhci_bus_info = { 157240bbc194SPeter Maydell .name = TYPE_SDHCI_BUS, 157340bbc194SPeter Maydell .parent = TYPE_SD_BUS, 157440bbc194SPeter Maydell .instance_size = sizeof(SDBus), 157540bbc194SPeter Maydell .class_init = sdhci_bus_class_init, 157640bbc194SPeter Maydell }; 157740bbc194SPeter Maydell 1578efadc818SPhilippe Mathieu-Daudé /* --- qdev i.MX eSDHC --- */ 1579efadc818SPhilippe Mathieu-Daudé 1580*1e76667fSBernhard Beschow #define USDHC_MIX_CTRL 0x48 1581c038e574SBernhard Beschow 1582*1e76667fSBernhard Beschow #define USDHC_VENDOR_SPEC 0xc0 1583*1e76667fSBernhard Beschow #define USDHC_IMX_FRC_SDCLK_ON (1 << 8) 1584c038e574SBernhard Beschow 1585*1e76667fSBernhard Beschow #define USDHC_DLL_CTRL 0x60 1586c038e574SBernhard Beschow 1587*1e76667fSBernhard Beschow #define USDHC_TUNING_CTRL 0xcc 1588*1e76667fSBernhard Beschow #define USDHC_TUNE_CTRL_STATUS 0x68 1589*1e76667fSBernhard Beschow #define USDHC_WTMK_LVL 0x44 1590c038e574SBernhard Beschow 1591c038e574SBernhard Beschow /* Undocumented register used by guests working around erratum ERR004536 */ 1592*1e76667fSBernhard Beschow #define USDHC_UNDOCUMENTED_REG27 0x6c 1593c038e574SBernhard Beschow 1594*1e76667fSBernhard Beschow #define USDHC_CTRL_4BITBUS (0x1 << 1) 1595*1e76667fSBernhard Beschow #define USDHC_CTRL_8BITBUS (0x2 << 1) 1596c038e574SBernhard Beschow 1597*1e76667fSBernhard Beschow #define USDHC_PRNSTS_SDSTB (1 << 3) 1598c038e574SBernhard Beschow 1599fd1e5c81SAndrey Smirnov static uint64_t usdhc_read(void *opaque, hwaddr offset, unsigned size) 1600fd1e5c81SAndrey Smirnov { 1601fd1e5c81SAndrey Smirnov SDHCIState *s = SYSBUS_SDHCI(opaque); 1602fd1e5c81SAndrey Smirnov uint32_t ret; 160306c5120bSPhilippe Mathieu-Daudé uint16_t hostctl1; 1604fd1e5c81SAndrey Smirnov 1605fd1e5c81SAndrey Smirnov switch (offset) { 1606fd1e5c81SAndrey Smirnov default: 1607fd1e5c81SAndrey Smirnov return sdhci_read(opaque, offset, size); 1608fd1e5c81SAndrey Smirnov 1609fd1e5c81SAndrey Smirnov case SDHC_HOSTCTL: 1610fd1e5c81SAndrey Smirnov /* 1611fd1e5c81SAndrey Smirnov * For a detailed explanation on the following bit 1612fd1e5c81SAndrey Smirnov * manipulation code see comments in a similar part of 1613fd1e5c81SAndrey Smirnov * usdhc_write() 1614fd1e5c81SAndrey Smirnov */ 161506c5120bSPhilippe Mathieu-Daudé hostctl1 = SDHC_DMA_TYPE(s->hostctl1) << (8 - 3); 1616fd1e5c81SAndrey Smirnov 161706c5120bSPhilippe Mathieu-Daudé if (s->hostctl1 & SDHC_CTRL_8BITBUS) { 1618*1e76667fSBernhard Beschow hostctl1 |= USDHC_CTRL_8BITBUS; 1619fd1e5c81SAndrey Smirnov } 1620fd1e5c81SAndrey Smirnov 162106c5120bSPhilippe Mathieu-Daudé if (s->hostctl1 & SDHC_CTRL_4BITBUS) { 1622*1e76667fSBernhard Beschow hostctl1 |= USDHC_CTRL_4BITBUS; 1623fd1e5c81SAndrey Smirnov } 1624fd1e5c81SAndrey Smirnov 162506c5120bSPhilippe Mathieu-Daudé ret = hostctl1; 1626fd1e5c81SAndrey Smirnov ret |= (uint32_t)s->blkgap << 16; 1627fd1e5c81SAndrey Smirnov ret |= (uint32_t)s->wakcon << 24; 1628fd1e5c81SAndrey Smirnov 1629fd1e5c81SAndrey Smirnov break; 1630fd1e5c81SAndrey Smirnov 16316bfd06daSHans-Erik Floryd case SDHC_PRNSTS: 16326bfd06daSHans-Erik Floryd /* Add SDSTB (SD Clock Stable) bit to PRNSTS */ 1633*1e76667fSBernhard Beschow ret = sdhci_read(opaque, offset, size) & ~USDHC_PRNSTS_SDSTB; 16346bfd06daSHans-Erik Floryd if (s->clkcon & SDHC_CLOCK_INT_STABLE) { 1635*1e76667fSBernhard Beschow ret |= USDHC_PRNSTS_SDSTB; 16366bfd06daSHans-Erik Floryd } 16376bfd06daSHans-Erik Floryd break; 16386bfd06daSHans-Erik Floryd 1639*1e76667fSBernhard Beschow case USDHC_VENDOR_SPEC: 16403b2d8176SGuenter Roeck ret = s->vendor_spec; 16413b2d8176SGuenter Roeck break; 1642*1e76667fSBernhard Beschow case USDHC_DLL_CTRL: 1643*1e76667fSBernhard Beschow case USDHC_TUNE_CTRL_STATUS: 1644*1e76667fSBernhard Beschow case USDHC_UNDOCUMENTED_REG27: 1645*1e76667fSBernhard Beschow case USDHC_TUNING_CTRL: 1646*1e76667fSBernhard Beschow case USDHC_MIX_CTRL: 1647*1e76667fSBernhard Beschow case USDHC_WTMK_LVL: 1648fd1e5c81SAndrey Smirnov ret = 0; 1649fd1e5c81SAndrey Smirnov break; 1650fd1e5c81SAndrey Smirnov } 1651fd1e5c81SAndrey Smirnov 1652fd1e5c81SAndrey Smirnov return ret; 1653fd1e5c81SAndrey Smirnov } 1654fd1e5c81SAndrey Smirnov 1655fd1e5c81SAndrey Smirnov static void 1656fd1e5c81SAndrey Smirnov usdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) 1657fd1e5c81SAndrey Smirnov { 1658fd1e5c81SAndrey Smirnov SDHCIState *s = SYSBUS_SDHCI(opaque); 165906c5120bSPhilippe Mathieu-Daudé uint8_t hostctl1; 1660fd1e5c81SAndrey Smirnov uint32_t value = (uint32_t)val; 1661fd1e5c81SAndrey Smirnov 1662fd1e5c81SAndrey Smirnov switch (offset) { 1663*1e76667fSBernhard Beschow case USDHC_DLL_CTRL: 1664*1e76667fSBernhard Beschow case USDHC_TUNE_CTRL_STATUS: 1665*1e76667fSBernhard Beschow case USDHC_UNDOCUMENTED_REG27: 1666*1e76667fSBernhard Beschow case USDHC_TUNING_CTRL: 1667*1e76667fSBernhard Beschow case USDHC_WTMK_LVL: 16683b2d8176SGuenter Roeck break; 16693b2d8176SGuenter Roeck 1670*1e76667fSBernhard Beschow case USDHC_VENDOR_SPEC: 16713b2d8176SGuenter Roeck s->vendor_spec = value; 16723b2d8176SGuenter Roeck switch (s->vendor) { 16733b2d8176SGuenter Roeck case SDHCI_VENDOR_IMX: 1674*1e76667fSBernhard Beschow if (value & USDHC_IMX_FRC_SDCLK_ON) { 16753b2d8176SGuenter Roeck s->prnsts &= ~SDHC_IMX_CLOCK_GATE_OFF; 16763b2d8176SGuenter Roeck } else { 16773b2d8176SGuenter Roeck s->prnsts |= SDHC_IMX_CLOCK_GATE_OFF; 16783b2d8176SGuenter Roeck } 16793b2d8176SGuenter Roeck break; 16803b2d8176SGuenter Roeck default: 16813b2d8176SGuenter Roeck break; 16823b2d8176SGuenter Roeck } 1683fd1e5c81SAndrey Smirnov break; 1684fd1e5c81SAndrey Smirnov 1685fd1e5c81SAndrey Smirnov case SDHC_HOSTCTL: 1686fd1e5c81SAndrey Smirnov /* 1687fd1e5c81SAndrey Smirnov * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL) 1688fd1e5c81SAndrey Smirnov * 1689fd1e5c81SAndrey Smirnov * 7 6 5 4 3 2 1 0 1690fd1e5c81SAndrey Smirnov * |-----------+--------+--------+-----------+----------+---------| 1691fd1e5c81SAndrey Smirnov * | Card | Card | Endian | DATA3 | Data | Led | 1692fd1e5c81SAndrey Smirnov * | Detect | Detect | Mode | as Card | Transfer | Control | 1693fd1e5c81SAndrey Smirnov * | Signal | Test | | Detection | Width | | 1694fd1e5c81SAndrey Smirnov * | Selection | Level | | Pin | | | 1695fd1e5c81SAndrey Smirnov * |-----------+--------+--------+-----------+----------+---------| 1696fd1e5c81SAndrey Smirnov * 1697fd1e5c81SAndrey Smirnov * and 0x29 1698fd1e5c81SAndrey Smirnov * 1699fd1e5c81SAndrey Smirnov * 15 10 9 8 1700fd1e5c81SAndrey Smirnov * |----------+------| 1701fd1e5c81SAndrey Smirnov * | Reserved | DMA | 1702fd1e5c81SAndrey Smirnov * | | Sel. | 1703fd1e5c81SAndrey Smirnov * | | | 1704fd1e5c81SAndrey Smirnov * |----------+------| 1705fd1e5c81SAndrey Smirnov * 1706fd1e5c81SAndrey Smirnov * and here's what SDCHI spec expects those offsets to be: 1707fd1e5c81SAndrey Smirnov * 1708fd1e5c81SAndrey Smirnov * 0x28 (Host Control Register) 1709fd1e5c81SAndrey Smirnov * 1710fd1e5c81SAndrey Smirnov * 7 6 5 4 3 2 1 0 1711fd1e5c81SAndrey Smirnov * |--------+--------+----------+------+--------+----------+---------| 1712fd1e5c81SAndrey Smirnov * | Card | Card | Extended | DMA | High | Data | LED | 1713fd1e5c81SAndrey Smirnov * | Detect | Detect | Data | Sel. | Speed | Transfer | Control | 1714fd1e5c81SAndrey Smirnov * | Signal | Test | Transfer | | Enable | Width | | 1715fd1e5c81SAndrey Smirnov * | Sel. | Level | Width | | | | | 1716fd1e5c81SAndrey Smirnov * |--------+--------+----------+------+--------+----------+---------| 1717fd1e5c81SAndrey Smirnov * 1718fd1e5c81SAndrey Smirnov * and 0x29 (Power Control Register) 1719fd1e5c81SAndrey Smirnov * 1720fd1e5c81SAndrey Smirnov * |----------------------------------| 1721fd1e5c81SAndrey Smirnov * | Power Control Register | 1722fd1e5c81SAndrey Smirnov * | | 1723fd1e5c81SAndrey Smirnov * | Description omitted, | 1724fd1e5c81SAndrey Smirnov * | since it has no analog in ESDHCI | 1725fd1e5c81SAndrey Smirnov * | | 1726fd1e5c81SAndrey Smirnov * |----------------------------------| 1727fd1e5c81SAndrey Smirnov * 1728fd1e5c81SAndrey Smirnov * Since offsets 0x2A and 0x2B should be compatible between 1729fd1e5c81SAndrey Smirnov * both IP specs we only need to reconcile least 16-bit of the 1730fd1e5c81SAndrey Smirnov * word we've been given. 1731fd1e5c81SAndrey Smirnov */ 1732fd1e5c81SAndrey Smirnov 1733fd1e5c81SAndrey Smirnov /* 1734fd1e5c81SAndrey Smirnov * First, save bits 7 6 and 0 since they are identical 1735fd1e5c81SAndrey Smirnov */ 173606c5120bSPhilippe Mathieu-Daudé hostctl1 = value & (SDHC_CTRL_LED | 1737fd1e5c81SAndrey Smirnov SDHC_CTRL_CDTEST_INS | 1738fd1e5c81SAndrey Smirnov SDHC_CTRL_CDTEST_EN); 1739fd1e5c81SAndrey Smirnov /* 1740fd1e5c81SAndrey Smirnov * Second, split "Data Transfer Width" from bits 2 and 1 in to 1741fd1e5c81SAndrey Smirnov * bits 5 and 1 1742fd1e5c81SAndrey Smirnov */ 1743*1e76667fSBernhard Beschow if (value & USDHC_CTRL_8BITBUS) { 174406c5120bSPhilippe Mathieu-Daudé hostctl1 |= SDHC_CTRL_8BITBUS; 1745fd1e5c81SAndrey Smirnov } 1746fd1e5c81SAndrey Smirnov 1747*1e76667fSBernhard Beschow if (value & USDHC_CTRL_4BITBUS) { 1748*1e76667fSBernhard Beschow hostctl1 |= USDHC_CTRL_4BITBUS; 1749fd1e5c81SAndrey Smirnov } 1750fd1e5c81SAndrey Smirnov 1751fd1e5c81SAndrey Smirnov /* 1752fd1e5c81SAndrey Smirnov * Third, move DMA select from bits 9 and 8 to bits 4 and 3 1753fd1e5c81SAndrey Smirnov */ 175406c5120bSPhilippe Mathieu-Daudé hostctl1 |= SDHC_DMA_TYPE(value >> (8 - 3)); 1755fd1e5c81SAndrey Smirnov 1756fd1e5c81SAndrey Smirnov /* 1757fd1e5c81SAndrey Smirnov * Now place the corrected value into low 16-bit of the value 1758fd1e5c81SAndrey Smirnov * we are going to give standard SDHCI write function 1759fd1e5c81SAndrey Smirnov * 1760fd1e5c81SAndrey Smirnov * NOTE: This transformation should be the inverse of what can 1761fd1e5c81SAndrey Smirnov * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux 1762fd1e5c81SAndrey Smirnov * kernel 1763fd1e5c81SAndrey Smirnov */ 1764fd1e5c81SAndrey Smirnov value &= ~UINT16_MAX; 176506c5120bSPhilippe Mathieu-Daudé value |= hostctl1; 1766fd1e5c81SAndrey Smirnov value |= (uint16_t)s->pwrcon << 8; 1767fd1e5c81SAndrey Smirnov 1768fd1e5c81SAndrey Smirnov sdhci_write(opaque, offset, value, size); 1769fd1e5c81SAndrey Smirnov break; 1770fd1e5c81SAndrey Smirnov 1771*1e76667fSBernhard Beschow case USDHC_MIX_CTRL: 1772fd1e5c81SAndrey Smirnov /* 1773fd1e5c81SAndrey Smirnov * So, when SD/MMC stack in Linux tries to write to "Transfer 1774fd1e5c81SAndrey Smirnov * Mode Register", ESDHC i.MX quirk code will translate it 1775fd1e5c81SAndrey Smirnov * into a write to ESDHC_MIX_CTRL, so we do the opposite in 1776fd1e5c81SAndrey Smirnov * order to get where we started 1777fd1e5c81SAndrey Smirnov * 1778fd1e5c81SAndrey Smirnov * Note that Auto CMD23 Enable bit is located in a wrong place 1779fd1e5c81SAndrey Smirnov * on i.MX, but since it is not used by QEMU we do not care. 1780fd1e5c81SAndrey Smirnov * 1781fd1e5c81SAndrey Smirnov * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...) 1782fd1e5c81SAndrey Smirnov * here becuase it will result in a call to 1783fd1e5c81SAndrey Smirnov * sdhci_send_command(s) which we don't want. 1784fd1e5c81SAndrey Smirnov * 1785fd1e5c81SAndrey Smirnov */ 1786fd1e5c81SAndrey Smirnov s->trnmod = value & UINT16_MAX; 1787fd1e5c81SAndrey Smirnov break; 1788fd1e5c81SAndrey Smirnov case SDHC_TRNMOD: 1789fd1e5c81SAndrey Smirnov /* 1790fd1e5c81SAndrey Smirnov * Similar to above, but this time a write to "Command 1791fd1e5c81SAndrey Smirnov * Register" will be translated into a 4-byte write to 1792fd1e5c81SAndrey Smirnov * "Transfer Mode register" where lower 16-bit of value would 1793fd1e5c81SAndrey Smirnov * be set to zero. So what we do is fill those bits with 1794fd1e5c81SAndrey Smirnov * cached value from s->trnmod and let the SDHCI 1795fd1e5c81SAndrey Smirnov * infrastructure handle the rest 1796fd1e5c81SAndrey Smirnov */ 1797fd1e5c81SAndrey Smirnov sdhci_write(opaque, offset, val | s->trnmod, size); 1798fd1e5c81SAndrey Smirnov break; 1799fd1e5c81SAndrey Smirnov case SDHC_BLKSIZE: 1800fd1e5c81SAndrey Smirnov /* 1801fd1e5c81SAndrey Smirnov * ESDHCI does not implement "Host SDMA Buffer Boundary", and 1802fd1e5c81SAndrey Smirnov * Linux driver will try to zero this field out which will 1803fd1e5c81SAndrey Smirnov * break the rest of SDHCI emulation. 1804fd1e5c81SAndrey Smirnov * 1805fd1e5c81SAndrey Smirnov * Linux defaults to maximum possible setting (512K boundary) 1806fd1e5c81SAndrey Smirnov * and it seems to be the only option that i.MX IP implements, 1807fd1e5c81SAndrey Smirnov * so we artificially set it to that value. 1808fd1e5c81SAndrey Smirnov */ 1809fd1e5c81SAndrey Smirnov val |= 0x7 << 12; 1810fd1e5c81SAndrey Smirnov /* FALLTHROUGH */ 1811fd1e5c81SAndrey Smirnov default: 1812fd1e5c81SAndrey Smirnov sdhci_write(opaque, offset, val, size); 1813fd1e5c81SAndrey Smirnov break; 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 1842c85fba50SPhilippe Mathieu-Daudé /* --- qdev Samsung s3c --- */ 1843c85fba50SPhilippe Mathieu-Daudé 1844c85fba50SPhilippe Mathieu-Daudé #define S3C_SDHCI_CONTROL2 0x80 1845c85fba50SPhilippe Mathieu-Daudé #define S3C_SDHCI_CONTROL3 0x84 1846c85fba50SPhilippe Mathieu-Daudé #define S3C_SDHCI_CONTROL4 0x8c 1847c85fba50SPhilippe Mathieu-Daudé 1848c85fba50SPhilippe Mathieu-Daudé static uint64_t sdhci_s3c_read(void *opaque, hwaddr offset, unsigned size) 1849c85fba50SPhilippe Mathieu-Daudé { 1850c85fba50SPhilippe Mathieu-Daudé uint64_t ret; 1851c85fba50SPhilippe Mathieu-Daudé 1852c85fba50SPhilippe Mathieu-Daudé switch (offset) { 1853c85fba50SPhilippe Mathieu-Daudé case S3C_SDHCI_CONTROL2: 1854c85fba50SPhilippe Mathieu-Daudé case S3C_SDHCI_CONTROL3: 1855c85fba50SPhilippe Mathieu-Daudé case S3C_SDHCI_CONTROL4: 1856c85fba50SPhilippe Mathieu-Daudé /* ignore */ 1857c85fba50SPhilippe Mathieu-Daudé ret = 0; 1858c85fba50SPhilippe Mathieu-Daudé break; 1859c85fba50SPhilippe Mathieu-Daudé default: 1860c85fba50SPhilippe Mathieu-Daudé ret = sdhci_read(opaque, offset, size); 1861c85fba50SPhilippe Mathieu-Daudé break; 1862c85fba50SPhilippe Mathieu-Daudé } 1863c85fba50SPhilippe Mathieu-Daudé 1864c85fba50SPhilippe Mathieu-Daudé return ret; 1865c85fba50SPhilippe Mathieu-Daudé } 1866c85fba50SPhilippe Mathieu-Daudé 1867c85fba50SPhilippe Mathieu-Daudé static void sdhci_s3c_write(void *opaque, hwaddr offset, uint64_t val, 1868c85fba50SPhilippe Mathieu-Daudé unsigned size) 1869c85fba50SPhilippe Mathieu-Daudé { 1870c85fba50SPhilippe Mathieu-Daudé switch (offset) { 1871c85fba50SPhilippe Mathieu-Daudé case S3C_SDHCI_CONTROL2: 1872c85fba50SPhilippe Mathieu-Daudé case S3C_SDHCI_CONTROL3: 1873c85fba50SPhilippe Mathieu-Daudé case S3C_SDHCI_CONTROL4: 1874c85fba50SPhilippe Mathieu-Daudé /* ignore */ 1875c85fba50SPhilippe Mathieu-Daudé break; 1876c85fba50SPhilippe Mathieu-Daudé default: 1877c85fba50SPhilippe Mathieu-Daudé sdhci_write(opaque, offset, val, size); 1878c85fba50SPhilippe Mathieu-Daudé break; 1879c85fba50SPhilippe Mathieu-Daudé } 1880c85fba50SPhilippe Mathieu-Daudé } 1881c85fba50SPhilippe Mathieu-Daudé 1882c85fba50SPhilippe Mathieu-Daudé static const MemoryRegionOps sdhci_s3c_mmio_ops = { 1883c85fba50SPhilippe Mathieu-Daudé .read = sdhci_s3c_read, 1884c85fba50SPhilippe Mathieu-Daudé .write = sdhci_s3c_write, 1885c85fba50SPhilippe Mathieu-Daudé .valid = { 1886c85fba50SPhilippe Mathieu-Daudé .min_access_size = 1, 1887c85fba50SPhilippe Mathieu-Daudé .max_access_size = 4, 1888c85fba50SPhilippe Mathieu-Daudé .unaligned = false 1889c85fba50SPhilippe Mathieu-Daudé }, 1890c85fba50SPhilippe Mathieu-Daudé .endianness = DEVICE_LITTLE_ENDIAN, 1891c85fba50SPhilippe Mathieu-Daudé }; 1892c85fba50SPhilippe Mathieu-Daudé 1893c85fba50SPhilippe Mathieu-Daudé static void sdhci_s3c_init(Object *obj) 1894c85fba50SPhilippe Mathieu-Daudé { 1895c85fba50SPhilippe Mathieu-Daudé SDHCIState *s = SYSBUS_SDHCI(obj); 1896c85fba50SPhilippe Mathieu-Daudé 1897c85fba50SPhilippe Mathieu-Daudé s->io_ops = &sdhci_s3c_mmio_ops; 1898c85fba50SPhilippe Mathieu-Daudé } 1899c85fba50SPhilippe Mathieu-Daudé 1900c85fba50SPhilippe Mathieu-Daudé static const TypeInfo sdhci_s3c_info = { 1901c85fba50SPhilippe Mathieu-Daudé .name = TYPE_S3C_SDHCI , 1902c85fba50SPhilippe Mathieu-Daudé .parent = TYPE_SYSBUS_SDHCI, 1903c85fba50SPhilippe Mathieu-Daudé .instance_init = sdhci_s3c_init, 1904c85fba50SPhilippe Mathieu-Daudé }; 1905c85fba50SPhilippe Mathieu-Daudé 190649ab747fSPaolo Bonzini static void sdhci_register_types(void) 190749ab747fSPaolo Bonzini { 19087302dcd6SKevin O'Connor type_register_static(&sdhci_sysbus_info); 190940bbc194SPeter Maydell type_register_static(&sdhci_bus_info); 1910fd1e5c81SAndrey Smirnov type_register_static(&imx_usdhc_info); 1911c85fba50SPhilippe Mathieu-Daudé type_register_static(&sdhci_s3c_info); 191249ab747fSPaolo Bonzini } 191349ab747fSPaolo Bonzini 191449ab747fSPaolo Bonzini type_init(sdhci_register_types) 1915