xref: /openbmc/qemu/hw/sd/sdhci.c (revision da34692234a03b6ffabd0b5b7864b423ac8eca56)
149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini  * SD Association Host Standard Specification v2.0 controller emulation
349ab747fSPaolo Bonzini  *
449ab747fSPaolo Bonzini  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
549ab747fSPaolo Bonzini  * Mitsyanko Igor <i.mitsyanko@samsung.com>
649ab747fSPaolo Bonzini  * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com>
749ab747fSPaolo Bonzini  *
849ab747fSPaolo Bonzini  * Based on MMC controller for Samsung S5PC1xx-based board emulation
949ab747fSPaolo Bonzini  * by Alexey Merkulov and Vladimir Monakhov.
1049ab747fSPaolo Bonzini  *
1149ab747fSPaolo Bonzini  * This program is free software; you can redistribute it and/or modify it
1249ab747fSPaolo Bonzini  * under the terms of the GNU General Public License as published by the
1349ab747fSPaolo Bonzini  * Free Software Foundation; either version 2 of the License, or (at your
1449ab747fSPaolo Bonzini  * option) any later version.
1549ab747fSPaolo Bonzini  *
1649ab747fSPaolo Bonzini  * This program is distributed in the hope that it will be useful,
1749ab747fSPaolo Bonzini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1849ab747fSPaolo Bonzini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1949ab747fSPaolo Bonzini  * See the GNU General Public License for more details.
2049ab747fSPaolo Bonzini  *
2149ab747fSPaolo Bonzini  * You should have received a copy of the GNU General Public License along
2249ab747fSPaolo Bonzini  * with this program; if not, see <http://www.gnu.org/licenses/>.
2349ab747fSPaolo Bonzini  */
2449ab747fSPaolo Bonzini 
250430891cSPeter Maydell #include "qemu/osdep.h"
266ff37c3dSPhilippe Mathieu-Daudé #include "qemu/error-report.h"
27b635d98cSPhilippe Mathieu-Daudé #include "qapi/error.h"
2849ab747fSPaolo Bonzini #include "hw/hw.h"
29fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
3049ab747fSPaolo Bonzini #include "sysemu/blockdev.h"
3149ab747fSPaolo Bonzini #include "sysemu/dma.h"
3249ab747fSPaolo Bonzini #include "qemu/timer.h"
3349ab747fSPaolo Bonzini #include "qemu/bitops.h"
34f82a0f44SPhilippe Mathieu-Daudé #include "hw/sd/sdhci.h"
35637d23beSSai Pavan Boddu #include "sdhci-internal.h"
3603dd024fSPaolo Bonzini #include "qemu/log.h"
37bf8ec38eSPhilippe Mathieu-Daudé #include "qemu/cutils.h"
388be487d8SPhilippe Mathieu-Daudé #include "trace.h"
3949ab747fSPaolo Bonzini 
4040bbc194SPeter Maydell #define TYPE_SDHCI_BUS "sdhci-bus"
4140bbc194SPeter Maydell #define SDHCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SDHCI_BUS)
4240bbc194SPeter Maydell 
43aa164fbfSPhilippe Mathieu-Daudé #define MASKED_WRITE(reg, mask, val)  (reg = (reg & (mask)) | (val))
44aa164fbfSPhilippe Mathieu-Daudé 
4549ab747fSPaolo Bonzini /* Default SD/MMC host controller features information, which will be
4649ab747fSPaolo Bonzini  * presented in CAPABILITIES register of generic SD host controller at reset.
47aa164fbfSPhilippe Mathieu-Daudé  *
48aa164fbfSPhilippe Mathieu-Daudé  * support:
49aa164fbfSPhilippe Mathieu-Daudé  * - 3.3v and 1.8v voltages
50aa164fbfSPhilippe Mathieu-Daudé  * - SDMA/ADMA1/ADMA2
51aa164fbfSPhilippe Mathieu-Daudé  * - high-speed
52aa164fbfSPhilippe Mathieu-Daudé  * max host controller R/W buffers size: 512B
53aa164fbfSPhilippe Mathieu-Daudé  * max clock frequency for SDclock: 52 MHz
54aa164fbfSPhilippe Mathieu-Daudé  * timeout clock frequency: 52 MHz
55aa164fbfSPhilippe Mathieu-Daudé  *
56aa164fbfSPhilippe Mathieu-Daudé  * does not support:
57aa164fbfSPhilippe Mathieu-Daudé  * - 3.0v voltage
58aa164fbfSPhilippe Mathieu-Daudé  * - 64-bit system bus
59aa164fbfSPhilippe Mathieu-Daudé  * - suspend/resume
6049ab747fSPaolo Bonzini  */
61aa164fbfSPhilippe Mathieu-Daudé #define SDHC_CAPAB_REG_DEFAULT 0x057834b4
6249ab747fSPaolo Bonzini 
6309b738ffSPhilippe Mathieu-Daudé static inline unsigned int sdhci_get_fifolen(SDHCIState *s)
6409b738ffSPhilippe Mathieu-Daudé {
6509b738ffSPhilippe Mathieu-Daudé     return 1 << (9 + FIELD_EX32(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH));
6609b738ffSPhilippe Mathieu-Daudé }
6709b738ffSPhilippe Mathieu-Daudé 
686ff37c3dSPhilippe Mathieu-Daudé /* return true on error */
696ff37c3dSPhilippe Mathieu-Daudé static bool sdhci_check_capab_freq_range(SDHCIState *s, const char *desc,
706ff37c3dSPhilippe Mathieu-Daudé                                          uint8_t freq, Error **errp)
716ff37c3dSPhilippe Mathieu-Daudé {
724d67852dSPhilippe Mathieu-Daudé     if (s->sd_spec_version >= 3) {
734d67852dSPhilippe Mathieu-Daudé         return false;
744d67852dSPhilippe Mathieu-Daudé     }
756ff37c3dSPhilippe Mathieu-Daudé     switch (freq) {
766ff37c3dSPhilippe Mathieu-Daudé     case 0:
776ff37c3dSPhilippe Mathieu-Daudé     case 10 ... 63:
786ff37c3dSPhilippe Mathieu-Daudé         break;
796ff37c3dSPhilippe Mathieu-Daudé     default:
806ff37c3dSPhilippe Mathieu-Daudé         error_setg(errp, "SD %s clock frequency can have value"
816ff37c3dSPhilippe Mathieu-Daudé                    "in range 0-63 only", desc);
826ff37c3dSPhilippe Mathieu-Daudé         return true;
836ff37c3dSPhilippe Mathieu-Daudé     }
846ff37c3dSPhilippe Mathieu-Daudé     return false;
856ff37c3dSPhilippe Mathieu-Daudé }
866ff37c3dSPhilippe Mathieu-Daudé 
876ff37c3dSPhilippe Mathieu-Daudé static void sdhci_check_capareg(SDHCIState *s, Error **errp)
886ff37c3dSPhilippe Mathieu-Daudé {
896ff37c3dSPhilippe Mathieu-Daudé     uint64_t msk = s->capareg;
906ff37c3dSPhilippe Mathieu-Daudé     uint32_t val;
916ff37c3dSPhilippe Mathieu-Daudé     bool y;
926ff37c3dSPhilippe Mathieu-Daudé 
936ff37c3dSPhilippe Mathieu-Daudé     switch (s->sd_spec_version) {
944d67852dSPhilippe Mathieu-Daudé     case 3:
954d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ASYNC_INT);
964d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("async interrupt", val);
974d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ASYNC_INT, 0);
984d67852dSPhilippe Mathieu-Daudé 
994d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SLOT_TYPE);
1004d67852dSPhilippe Mathieu-Daudé         if (val) {
1014d67852dSPhilippe Mathieu-Daudé             error_setg(errp, "slot-type not supported");
1024d67852dSPhilippe Mathieu-Daudé             return;
1034d67852dSPhilippe Mathieu-Daudé         }
1044d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("slot type", val);
1054d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SLOT_TYPE, 0);
1064d67852dSPhilippe Mathieu-Daudé 
1074d67852dSPhilippe Mathieu-Daudé         if (val != 2) {
1084d67852dSPhilippe Mathieu-Daudé             val = FIELD_EX64(s->capareg, SDHC_CAPAB, EMBEDDED_8BIT);
1094d67852dSPhilippe Mathieu-Daudé             trace_sdhci_capareg("8-bit bus", val);
1104d67852dSPhilippe Mathieu-Daudé         }
1114d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, EMBEDDED_8BIT, 0);
1124d67852dSPhilippe Mathieu-Daudé 
1134d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS_SPEED);
1144d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("bus speed mask", val);
1154d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS_SPEED, 0);
1164d67852dSPhilippe Mathieu-Daudé 
1174d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, DRIVER_STRENGTH);
1184d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("driver strength mask", val);
1194d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, DRIVER_STRENGTH, 0);
1204d67852dSPhilippe Mathieu-Daudé 
1214d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, TIMER_RETUNING);
1224d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("timer re-tuning", val);
1234d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TIMER_RETUNING, 0);
1244d67852dSPhilippe Mathieu-Daudé 
1254d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDR50_TUNING);
1264d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("use SDR50 tuning", val);
1274d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SDR50_TUNING, 0);
1284d67852dSPhilippe Mathieu-Daudé 
1294d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, RETUNING_MODE);
1304d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("re-tuning mode", val);
1314d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, RETUNING_MODE, 0);
1324d67852dSPhilippe Mathieu-Daudé 
1334d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, CLOCK_MULT);
1344d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("clock multiplier", val);
1354d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, CLOCK_MULT, 0);
1364d67852dSPhilippe Mathieu-Daudé 
1374d67852dSPhilippe Mathieu-Daudé     /* fallthrough */
1386ff37c3dSPhilippe Mathieu-Daudé     case 2: /* default version */
1390540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA2);
1400540fba9SPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA2", val);
1410540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA2, 0);
1420540fba9SPhilippe Mathieu-Daudé 
1430540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA1);
1440540fba9SPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA1", val);
1450540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA1, 0);
1460540fba9SPhilippe Mathieu-Daudé 
1470540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT);
1480540fba9SPhilippe Mathieu-Daudé         trace_sdhci_capareg("64-bit system bus", val);
1490540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT, 0);
1506ff37c3dSPhilippe Mathieu-Daudé 
1516ff37c3dSPhilippe Mathieu-Daudé     /* fallthrough */
1526ff37c3dSPhilippe Mathieu-Daudé     case 1:
1536ff37c3dSPhilippe Mathieu-Daudé         y = FIELD_EX64(s->capareg, SDHC_CAPAB, TOUNIT);
1546ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TOUNIT, 0);
1556ff37c3dSPhilippe Mathieu-Daudé 
1566ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, TOCLKFREQ);
1576ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg(y ? "timeout (MHz)" : "Timeout (KHz)", val);
1586ff37c3dSPhilippe Mathieu-Daudé         if (sdhci_check_capab_freq_range(s, "timeout", val, errp)) {
1596ff37c3dSPhilippe Mathieu-Daudé             return;
1606ff37c3dSPhilippe Mathieu-Daudé         }
1616ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TOCLKFREQ, 0);
1626ff37c3dSPhilippe Mathieu-Daudé 
1636ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BASECLKFREQ);
1646ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg(y ? "base (MHz)" : "Base (KHz)", val);
1656ff37c3dSPhilippe Mathieu-Daudé         if (sdhci_check_capab_freq_range(s, "base", val, errp)) {
1666ff37c3dSPhilippe Mathieu-Daudé             return;
1676ff37c3dSPhilippe Mathieu-Daudé         }
1686ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BASECLKFREQ, 0);
1696ff37c3dSPhilippe Mathieu-Daudé 
1706ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH);
1716ff37c3dSPhilippe Mathieu-Daudé         if (val >= 3) {
1726ff37c3dSPhilippe Mathieu-Daudé             error_setg(errp, "block size can be 512, 1024 or 2048 only");
1736ff37c3dSPhilippe Mathieu-Daudé             return;
1746ff37c3dSPhilippe Mathieu-Daudé         }
1756ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("max block length", sdhci_get_fifolen(s));
1766ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, MAXBLOCKLENGTH, 0);
1776ff37c3dSPhilippe Mathieu-Daudé 
1786ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, HIGHSPEED);
1796ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("high speed", val);
1806ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, HIGHSPEED, 0);
1816ff37c3dSPhilippe Mathieu-Daudé 
1826ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDMA);
1836ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("SDMA", val);
1846ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SDMA, 0);
1856ff37c3dSPhilippe Mathieu-Daudé 
1866ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SUSPRESUME);
1876ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("suspend/resume", val);
1886ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SUSPRESUME, 0);
1896ff37c3dSPhilippe Mathieu-Daudé 
1906ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V33);
1916ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("3.3v", val);
1926ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V33, 0);
1936ff37c3dSPhilippe Mathieu-Daudé 
1946ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V30);
1956ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("3.0v", val);
1966ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V30, 0);
1976ff37c3dSPhilippe Mathieu-Daudé 
1986ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V18);
1996ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("1.8v", val);
2006ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V18, 0);
2016ff37c3dSPhilippe Mathieu-Daudé         break;
2026ff37c3dSPhilippe Mathieu-Daudé 
2036ff37c3dSPhilippe Mathieu-Daudé     default:
2046ff37c3dSPhilippe Mathieu-Daudé         error_setg(errp, "Unsupported spec version: %u", s->sd_spec_version);
2056ff37c3dSPhilippe Mathieu-Daudé     }
2066ff37c3dSPhilippe Mathieu-Daudé     if (msk) {
2076ff37c3dSPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP,
2086ff37c3dSPhilippe Mathieu-Daudé                       "SDHCI: unknown CAPAB mask: 0x%016" PRIx64 "\n", msk);
2096ff37c3dSPhilippe Mathieu-Daudé     }
2106ff37c3dSPhilippe Mathieu-Daudé }
2116ff37c3dSPhilippe Mathieu-Daudé 
21249ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s)
21349ab747fSPaolo Bonzini {
21449ab747fSPaolo Bonzini     return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) ||
21549ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) ||
21649ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV));
21749ab747fSPaolo Bonzini }
21849ab747fSPaolo Bonzini 
21949ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s)
22049ab747fSPaolo Bonzini {
22149ab747fSPaolo Bonzini     qemu_set_irq(s->irq, sdhci_slotint(s));
22249ab747fSPaolo Bonzini }
22349ab747fSPaolo Bonzini 
22449ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque)
22549ab747fSPaolo Bonzini {
22649ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
22749ab747fSPaolo Bonzini 
22849ab747fSPaolo Bonzini     if (s->norintsts & SDHC_NIS_REMOVE) {
229bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
230bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
23149ab747fSPaolo Bonzini     } else {
23249ab747fSPaolo Bonzini         s->prnsts = 0x1ff0000;
23349ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_INSERT) {
23449ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_INSERT;
23549ab747fSPaolo Bonzini         }
23649ab747fSPaolo Bonzini         sdhci_update_irq(s);
23749ab747fSPaolo Bonzini     }
23849ab747fSPaolo Bonzini }
23949ab747fSPaolo Bonzini 
24040bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level)
24149ab747fSPaolo Bonzini {
24240bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
24349ab747fSPaolo Bonzini 
2448be487d8SPhilippe Mathieu-Daudé     trace_sdhci_set_inserted(level ? "insert" : "eject");
24549ab747fSPaolo Bonzini     if ((s->norintsts & SDHC_NIS_REMOVE) && level) {
24649ab747fSPaolo Bonzini         /* Give target some time to notice card ejection */
247bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
248bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
24949ab747fSPaolo Bonzini     } else {
25049ab747fSPaolo Bonzini         if (level) {
25149ab747fSPaolo Bonzini             s->prnsts = 0x1ff0000;
25249ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_INSERT) {
25349ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_INSERT;
25449ab747fSPaolo Bonzini             }
25549ab747fSPaolo Bonzini         } else {
25649ab747fSPaolo Bonzini             s->prnsts = 0x1fa0000;
25749ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
25849ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_SDCLK_EN;
25949ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_REMOVE) {
26049ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_REMOVE;
26149ab747fSPaolo Bonzini             }
26249ab747fSPaolo Bonzini         }
26349ab747fSPaolo Bonzini         sdhci_update_irq(s);
26449ab747fSPaolo Bonzini     }
26549ab747fSPaolo Bonzini }
26649ab747fSPaolo Bonzini 
26740bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level)
26849ab747fSPaolo Bonzini {
26940bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
27049ab747fSPaolo Bonzini 
27149ab747fSPaolo Bonzini     if (level) {
27249ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_WRITE_PROTECT;
27349ab747fSPaolo Bonzini     } else {
27449ab747fSPaolo Bonzini         /* Write enabled */
27549ab747fSPaolo Bonzini         s->prnsts |= SDHC_WRITE_PROTECT;
27649ab747fSPaolo Bonzini     }
27749ab747fSPaolo Bonzini }
27849ab747fSPaolo Bonzini 
27949ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s)
28049ab747fSPaolo Bonzini {
28140bbc194SPeter Maydell     DeviceState *dev = DEVICE(s);
28240bbc194SPeter Maydell 
283bc72ad67SAlex Bligh     timer_del(s->insert_timer);
284bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
285aceb5b06SPhilippe Mathieu-Daudé 
286aceb5b06SPhilippe Mathieu-Daudé     /* Set all registers to 0. Capabilities/Version registers are not cleared
28749ab747fSPaolo Bonzini      * and assumed to always preserve their value, given to them during
28849ab747fSPaolo Bonzini      * initialization */
28949ab747fSPaolo Bonzini     memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad);
29049ab747fSPaolo Bonzini 
29140bbc194SPeter Maydell     /* Reset other state based on current card insertion/readonly status */
29240bbc194SPeter Maydell     sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus));
29340bbc194SPeter Maydell     sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus));
29440bbc194SPeter Maydell 
29549ab747fSPaolo Bonzini     s->data_count = 0;
29649ab747fSPaolo Bonzini     s->stopped_state = sdhc_not_stopped;
2970a7ac9f9SAndrew Baumann     s->pending_insert_state = false;
29849ab747fSPaolo Bonzini }
29949ab747fSPaolo Bonzini 
3008b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev)
3018b41c305SPeter Maydell {
3028b41c305SPeter Maydell     /* QOM (ie power-on) reset. This is identical to reset
3038b41c305SPeter Maydell      * commanded via device register apart from handling of the
3048b41c305SPeter Maydell      * 'pending insert on powerup' quirk.
3058b41c305SPeter Maydell      */
3068b41c305SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
3078b41c305SPeter Maydell 
3088b41c305SPeter Maydell     sdhci_reset(s);
3098b41c305SPeter Maydell 
3108b41c305SPeter Maydell     if (s->pending_insert_quirk) {
3118b41c305SPeter Maydell         s->pending_insert_state = true;
3128b41c305SPeter Maydell     }
3138b41c305SPeter Maydell }
3148b41c305SPeter Maydell 
315d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque);
31649ab747fSPaolo Bonzini 
31749ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s)
31849ab747fSPaolo Bonzini {
31949ab747fSPaolo Bonzini     SDRequest request;
32049ab747fSPaolo Bonzini     uint8_t response[16];
32149ab747fSPaolo Bonzini     int rlen;
32249ab747fSPaolo Bonzini 
32349ab747fSPaolo Bonzini     s->errintsts = 0;
32449ab747fSPaolo Bonzini     s->acmd12errsts = 0;
32549ab747fSPaolo Bonzini     request.cmd = s->cmdreg >> 8;
32649ab747fSPaolo Bonzini     request.arg = s->argument;
3278be487d8SPhilippe Mathieu-Daudé 
3288be487d8SPhilippe Mathieu-Daudé     trace_sdhci_send_command(request.cmd, request.arg);
32940bbc194SPeter Maydell     rlen = sdbus_do_command(&s->sdbus, &request, response);
33049ab747fSPaolo Bonzini 
33149ab747fSPaolo Bonzini     if (s->cmdreg & SDHC_CMD_RESPONSE) {
33249ab747fSPaolo Bonzini         if (rlen == 4) {
33349ab747fSPaolo Bonzini             s->rspreg[0] = (response[0] << 24) | (response[1] << 16) |
33449ab747fSPaolo Bonzini                            (response[2] << 8)  |  response[3];
33549ab747fSPaolo Bonzini             s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0;
3368be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response4(s->rspreg[0]);
33749ab747fSPaolo Bonzini         } else if (rlen == 16) {
33849ab747fSPaolo Bonzini             s->rspreg[0] = (response[11] << 24) | (response[12] << 16) |
33949ab747fSPaolo Bonzini                            (response[13] << 8) |  response[14];
34049ab747fSPaolo Bonzini             s->rspreg[1] = (response[7] << 24) | (response[8] << 16) |
34149ab747fSPaolo Bonzini                            (response[9] << 8)  |  response[10];
34249ab747fSPaolo Bonzini             s->rspreg[2] = (response[3] << 24) | (response[4] << 16) |
34349ab747fSPaolo Bonzini                            (response[5] << 8)  |  response[6];
34449ab747fSPaolo Bonzini             s->rspreg[3] = (response[0] << 16) | (response[1] << 8) |
34549ab747fSPaolo Bonzini                             response[2];
3468be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response16(s->rspreg[3], s->rspreg[2],
3478be487d8SPhilippe Mathieu-Daudé                                    s->rspreg[1], s->rspreg[0]);
34849ab747fSPaolo Bonzini         } else {
3498be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("timeout waiting for command response");
35049ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) {
35149ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_CMDTIMEOUT;
35249ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
35349ab747fSPaolo Bonzini             }
35449ab747fSPaolo Bonzini         }
35549ab747fSPaolo Bonzini 
356fd1e5c81SAndrey Smirnov         if (!(s->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
357fd1e5c81SAndrey Smirnov             (s->norintstsen & SDHC_NISEN_TRSCMP) &&
35849ab747fSPaolo Bonzini             (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) {
35949ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_TRSCMP;
36049ab747fSPaolo Bonzini         }
36149ab747fSPaolo Bonzini     }
36249ab747fSPaolo Bonzini 
36349ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_CMDCMP) {
36449ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_CMDCMP;
36549ab747fSPaolo Bonzini     }
36649ab747fSPaolo Bonzini 
36749ab747fSPaolo Bonzini     sdhci_update_irq(s);
36849ab747fSPaolo Bonzini 
36949ab747fSPaolo Bonzini     if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) {
370656f416cSPeter Crosthwaite         s->data_count = 0;
371d368ba43SKevin O'Connor         sdhci_data_transfer(s);
37249ab747fSPaolo Bonzini     }
37349ab747fSPaolo Bonzini }
37449ab747fSPaolo Bonzini 
37549ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s)
37649ab747fSPaolo Bonzini {
37749ab747fSPaolo Bonzini     /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
37849ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) {
37949ab747fSPaolo Bonzini         SDRequest request;
38049ab747fSPaolo Bonzini         uint8_t response[16];
38149ab747fSPaolo Bonzini 
38249ab747fSPaolo Bonzini         request.cmd = 0x0C;
38349ab747fSPaolo Bonzini         request.arg = 0;
3848be487d8SPhilippe Mathieu-Daudé         trace_sdhci_end_transfer(request.cmd, request.arg);
38540bbc194SPeter Maydell         sdbus_do_command(&s->sdbus, &request, response);
38649ab747fSPaolo Bonzini         /* Auto CMD12 response goes to the upper Response register */
38749ab747fSPaolo Bonzini         s->rspreg[3] = (response[0] << 24) | (response[1] << 16) |
38849ab747fSPaolo Bonzini                 (response[2] << 8) | response[3];
38949ab747fSPaolo Bonzini     }
39049ab747fSPaolo Bonzini 
39149ab747fSPaolo Bonzini     s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE |
39249ab747fSPaolo Bonzini             SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT |
39349ab747fSPaolo Bonzini             SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE);
39449ab747fSPaolo Bonzini 
39549ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_TRSCMP) {
39649ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_TRSCMP;
39749ab747fSPaolo Bonzini     }
39849ab747fSPaolo Bonzini 
39949ab747fSPaolo Bonzini     sdhci_update_irq(s);
40049ab747fSPaolo Bonzini }
40149ab747fSPaolo Bonzini 
40249ab747fSPaolo Bonzini /*
40349ab747fSPaolo Bonzini  * Programmed i/o data transfer
40449ab747fSPaolo Bonzini  */
405bf8ec38eSPhilippe Mathieu-Daudé #define BLOCK_SIZE_MASK (4 * K_BYTE - 1)
40649ab747fSPaolo Bonzini 
40749ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
40849ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s)
40949ab747fSPaolo Bonzini {
41049ab747fSPaolo Bonzini     int index = 0;
411ea55a221SPhilippe Mathieu-Daudé     uint8_t data;
412ea55a221SPhilippe Mathieu-Daudé     const uint16_t blk_size = s->blksize & BLOCK_SIZE_MASK;
41349ab747fSPaolo Bonzini 
41449ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) &&
41549ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) {
41649ab747fSPaolo Bonzini         return;
41749ab747fSPaolo Bonzini     }
41849ab747fSPaolo Bonzini 
419ea55a221SPhilippe Mathieu-Daudé     for (index = 0; index < blk_size; index++) {
420ea55a221SPhilippe Mathieu-Daudé         data = sdbus_read_data(&s->sdbus);
421ea55a221SPhilippe Mathieu-Daudé         if (!FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) {
422ea55a221SPhilippe Mathieu-Daudé             /* Device is not in tunning */
423ea55a221SPhilippe Mathieu-Daudé             s->fifo_buffer[index] = data;
424ea55a221SPhilippe Mathieu-Daudé         }
425ea55a221SPhilippe Mathieu-Daudé     }
426ea55a221SPhilippe Mathieu-Daudé 
427ea55a221SPhilippe Mathieu-Daudé     if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) {
428ea55a221SPhilippe Mathieu-Daudé         /* Device is in tunning */
429ea55a221SPhilippe Mathieu-Daudé         s->hostctl2 &= ~R_SDHC_HOSTCTL2_EXECUTE_TUNING_MASK;
430ea55a221SPhilippe Mathieu-Daudé         s->hostctl2 |= R_SDHC_HOSTCTL2_SAMPLING_CLKSEL_MASK;
431ea55a221SPhilippe Mathieu-Daudé         s->prnsts &= ~(SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ |
432ea55a221SPhilippe Mathieu-Daudé                        SDHC_DATA_INHIBIT);
433ea55a221SPhilippe Mathieu-Daudé         goto read_done;
43449ab747fSPaolo Bonzini     }
43549ab747fSPaolo Bonzini 
43649ab747fSPaolo Bonzini     /* New data now available for READ through Buffer Port Register */
43749ab747fSPaolo Bonzini     s->prnsts |= SDHC_DATA_AVAILABLE;
43849ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_RBUFRDY) {
43949ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_RBUFRDY;
44049ab747fSPaolo Bonzini     }
44149ab747fSPaolo Bonzini 
44249ab747fSPaolo Bonzini     /* Clear DAT line active status if that was the last block */
44349ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
44449ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) {
44549ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
44649ab747fSPaolo Bonzini     }
44749ab747fSPaolo Bonzini 
44849ab747fSPaolo Bonzini     /* If stop at block gap request was set and it's not the last block of
44949ab747fSPaolo Bonzini      * data - generate Block Event interrupt */
45049ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) &&
45149ab747fSPaolo Bonzini             s->blkcnt != 1)    {
45249ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
45349ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
45449ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
45549ab747fSPaolo Bonzini         }
45649ab747fSPaolo Bonzini     }
45749ab747fSPaolo Bonzini 
458ea55a221SPhilippe Mathieu-Daudé read_done:
45949ab747fSPaolo Bonzini     sdhci_update_irq(s);
46049ab747fSPaolo Bonzini }
46149ab747fSPaolo Bonzini 
46249ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
46349ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size)
46449ab747fSPaolo Bonzini {
46549ab747fSPaolo Bonzini     uint32_t value = 0;
46649ab747fSPaolo Bonzini     int i;
46749ab747fSPaolo Bonzini 
46849ab747fSPaolo Bonzini     /* first check that a valid data exists in host controller input buffer */
46949ab747fSPaolo Bonzini     if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) {
4708be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("read from empty buffer");
47149ab747fSPaolo Bonzini         return 0;
47249ab747fSPaolo Bonzini     }
47349ab747fSPaolo Bonzini 
47449ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
47549ab747fSPaolo Bonzini         value |= s->fifo_buffer[s->data_count] << i * 8;
47649ab747fSPaolo Bonzini         s->data_count++;
47749ab747fSPaolo Bonzini         /* check if we've read all valid data (blksize bytes) from buffer */
478bf8ec38eSPhilippe Mathieu-Daudé         if ((s->data_count) >= (s->blksize & BLOCK_SIZE_MASK)) {
4798be487d8SPhilippe Mathieu-Daudé             trace_sdhci_read_dataport(s->data_count);
48049ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */
48149ab747fSPaolo Bonzini             s->data_count = 0;  /* next buff read must start at position [0] */
48249ab747fSPaolo Bonzini 
48349ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
48449ab747fSPaolo Bonzini                 s->blkcnt--;
48549ab747fSPaolo Bonzini             }
48649ab747fSPaolo Bonzini 
48749ab747fSPaolo Bonzini             /* if that was the last block of data */
48849ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
48949ab747fSPaolo Bonzini                 ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) ||
49049ab747fSPaolo Bonzini                  /* stop at gap request */
49149ab747fSPaolo Bonzini                 (s->stopped_state == sdhc_gap_read &&
49249ab747fSPaolo Bonzini                  !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) {
493d368ba43SKevin O'Connor                 sdhci_end_transfer(s);
49449ab747fSPaolo Bonzini             } else { /* if there are more data, read next block from card */
495d368ba43SKevin O'Connor                 sdhci_read_block_from_card(s);
49649ab747fSPaolo Bonzini             }
49749ab747fSPaolo Bonzini             break;
49849ab747fSPaolo Bonzini         }
49949ab747fSPaolo Bonzini     }
50049ab747fSPaolo Bonzini 
50149ab747fSPaolo Bonzini     return value;
50249ab747fSPaolo Bonzini }
50349ab747fSPaolo Bonzini 
50449ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */
50549ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s)
50649ab747fSPaolo Bonzini {
50749ab747fSPaolo Bonzini     int index = 0;
50849ab747fSPaolo Bonzini 
50949ab747fSPaolo Bonzini     if (s->prnsts & SDHC_SPACE_AVAILABLE) {
51049ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
51149ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_WBUFRDY;
51249ab747fSPaolo Bonzini         }
51349ab747fSPaolo Bonzini         sdhci_update_irq(s);
51449ab747fSPaolo Bonzini         return;
51549ab747fSPaolo Bonzini     }
51649ab747fSPaolo Bonzini 
51749ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
51849ab747fSPaolo Bonzini         if (s->blkcnt == 0) {
51949ab747fSPaolo Bonzini             return;
52049ab747fSPaolo Bonzini         } else {
52149ab747fSPaolo Bonzini             s->blkcnt--;
52249ab747fSPaolo Bonzini         }
52349ab747fSPaolo Bonzini     }
52449ab747fSPaolo Bonzini 
525bf8ec38eSPhilippe Mathieu-Daudé     for (index = 0; index < (s->blksize & BLOCK_SIZE_MASK); index++) {
52640bbc194SPeter Maydell         sdbus_write_data(&s->sdbus, s->fifo_buffer[index]);
52749ab747fSPaolo Bonzini     }
52849ab747fSPaolo Bonzini 
52949ab747fSPaolo Bonzini     /* Next data can be written through BUFFER DATORT register */
53049ab747fSPaolo Bonzini     s->prnsts |= SDHC_SPACE_AVAILABLE;
53149ab747fSPaolo Bonzini 
53249ab747fSPaolo Bonzini     /* Finish transfer if that was the last block of data */
53349ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
53449ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) &&
53549ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) {
536d368ba43SKevin O'Connor         sdhci_end_transfer(s);
537dcdb4cd8SPeter Crosthwaite     } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
538dcdb4cd8SPeter Crosthwaite         s->norintsts |= SDHC_NIS_WBUFRDY;
53949ab747fSPaolo Bonzini     }
54049ab747fSPaolo Bonzini 
54149ab747fSPaolo Bonzini     /* Generate Block Gap Event if requested and if not the last block */
54249ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) &&
54349ab747fSPaolo Bonzini             s->blkcnt > 0) {
54449ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DOING_WRITE;
54549ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
54649ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
54749ab747fSPaolo Bonzini         }
548d368ba43SKevin O'Connor         sdhci_end_transfer(s);
54949ab747fSPaolo Bonzini     }
55049ab747fSPaolo Bonzini 
55149ab747fSPaolo Bonzini     sdhci_update_irq(s);
55249ab747fSPaolo Bonzini }
55349ab747fSPaolo Bonzini 
55449ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port
55549ab747fSPaolo Bonzini  * register */
55649ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size)
55749ab747fSPaolo Bonzini {
55849ab747fSPaolo Bonzini     unsigned i;
55949ab747fSPaolo Bonzini 
56049ab747fSPaolo Bonzini     /* Check that there is free space left in a buffer */
56149ab747fSPaolo Bonzini     if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) {
5628be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("Can't write to data buffer: buffer full");
56349ab747fSPaolo Bonzini         return;
56449ab747fSPaolo Bonzini     }
56549ab747fSPaolo Bonzini 
56649ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
56749ab747fSPaolo Bonzini         s->fifo_buffer[s->data_count] = value & 0xFF;
56849ab747fSPaolo Bonzini         s->data_count++;
56949ab747fSPaolo Bonzini         value >>= 8;
570bf8ec38eSPhilippe Mathieu-Daudé         if (s->data_count >= (s->blksize & BLOCK_SIZE_MASK)) {
5718be487d8SPhilippe Mathieu-Daudé             trace_sdhci_write_dataport(s->data_count);
57249ab747fSPaolo Bonzini             s->data_count = 0;
57349ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_SPACE_AVAILABLE;
57449ab747fSPaolo Bonzini             if (s->prnsts & SDHC_DOING_WRITE) {
575d368ba43SKevin O'Connor                 sdhci_write_block_to_card(s);
57649ab747fSPaolo Bonzini             }
57749ab747fSPaolo Bonzini         }
57849ab747fSPaolo Bonzini     }
57949ab747fSPaolo Bonzini }
58049ab747fSPaolo Bonzini 
58149ab747fSPaolo Bonzini /*
58249ab747fSPaolo Bonzini  * Single DMA data transfer
58349ab747fSPaolo Bonzini  */
58449ab747fSPaolo Bonzini 
58549ab747fSPaolo Bonzini /* Multi block SDMA transfer */
58649ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
58749ab747fSPaolo Bonzini {
58849ab747fSPaolo Bonzini     bool page_aligned = false;
58949ab747fSPaolo Bonzini     unsigned int n, begin;
590bf8ec38eSPhilippe Mathieu-Daudé     const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK;
591bf8ec38eSPhilippe Mathieu-Daudé     uint32_t boundary_chk = 1 << (((s->blksize & ~BLOCK_SIZE_MASK) >> 12) + 12);
59249ab747fSPaolo Bonzini     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
59349ab747fSPaolo Bonzini 
5946e86d903SPrasad J Pandit     if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) {
5956e86d903SPrasad J Pandit         qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n");
5966e86d903SPrasad J Pandit         return;
5976e86d903SPrasad J Pandit     }
5986e86d903SPrasad J Pandit 
59949ab747fSPaolo Bonzini     /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
60049ab747fSPaolo Bonzini      * possible stop at page boundary if initial address is not page aligned,
60149ab747fSPaolo Bonzini      * allow them to work properly */
60249ab747fSPaolo Bonzini     if ((s->sdmasysad % boundary_chk) == 0) {
60349ab747fSPaolo Bonzini         page_aligned = true;
60449ab747fSPaolo Bonzini     }
60549ab747fSPaolo Bonzini 
60649ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
60749ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
60849ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
60949ab747fSPaolo Bonzini         while (s->blkcnt) {
61049ab747fSPaolo Bonzini             if (s->data_count == 0) {
61149ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
61240bbc194SPeter Maydell                     s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
61349ab747fSPaolo Bonzini                 }
61449ab747fSPaolo Bonzini             }
61549ab747fSPaolo Bonzini             begin = s->data_count;
61649ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
61749ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
61849ab747fSPaolo Bonzini                 boundary_count = 0;
61949ab747fSPaolo Bonzini              } else {
62049ab747fSPaolo Bonzini                 s->data_count = block_size;
62149ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
62249ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
62349ab747fSPaolo Bonzini                     s->blkcnt--;
62449ab747fSPaolo Bonzini                 }
62549ab747fSPaolo Bonzini             }
626dd55c485SPhilippe Mathieu-Daudé             dma_memory_write(s->dma_as, s->sdmasysad,
62749ab747fSPaolo Bonzini                              &s->fifo_buffer[begin], s->data_count - begin);
62849ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
62949ab747fSPaolo Bonzini             if (s->data_count == block_size) {
63049ab747fSPaolo Bonzini                 s->data_count = 0;
63149ab747fSPaolo Bonzini             }
63249ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
63349ab747fSPaolo Bonzini                 break;
63449ab747fSPaolo Bonzini             }
63549ab747fSPaolo Bonzini         }
63649ab747fSPaolo Bonzini     } else {
63749ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT |
63849ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
63949ab747fSPaolo Bonzini         while (s->blkcnt) {
64049ab747fSPaolo Bonzini             begin = s->data_count;
64149ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
64249ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
64349ab747fSPaolo Bonzini                 boundary_count = 0;
64449ab747fSPaolo Bonzini              } else {
64549ab747fSPaolo Bonzini                 s->data_count = block_size;
64649ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
64749ab747fSPaolo Bonzini             }
648dd55c485SPhilippe Mathieu-Daudé             dma_memory_read(s->dma_as, s->sdmasysad,
64942922105SPrasad J Pandit                             &s->fifo_buffer[begin], s->data_count - begin);
65049ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
65149ab747fSPaolo Bonzini             if (s->data_count == block_size) {
65249ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
65340bbc194SPeter Maydell                     sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
65449ab747fSPaolo Bonzini                 }
65549ab747fSPaolo Bonzini                 s->data_count = 0;
65649ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
65749ab747fSPaolo Bonzini                     s->blkcnt--;
65849ab747fSPaolo Bonzini                 }
65949ab747fSPaolo Bonzini             }
66049ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
66149ab747fSPaolo Bonzini                 break;
66249ab747fSPaolo Bonzini             }
66349ab747fSPaolo Bonzini         }
66449ab747fSPaolo Bonzini     }
66549ab747fSPaolo Bonzini 
66649ab747fSPaolo Bonzini     if (s->blkcnt == 0) {
667d368ba43SKevin O'Connor         sdhci_end_transfer(s);
66849ab747fSPaolo Bonzini     } else {
66949ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_DMA) {
67049ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_DMA;
67149ab747fSPaolo Bonzini         }
67249ab747fSPaolo Bonzini         sdhci_update_irq(s);
67349ab747fSPaolo Bonzini     }
67449ab747fSPaolo Bonzini }
67549ab747fSPaolo Bonzini 
67649ab747fSPaolo Bonzini /* single block SDMA transfer */
67749ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s)
67849ab747fSPaolo Bonzini {
67949ab747fSPaolo Bonzini     int n;
680bf8ec38eSPhilippe Mathieu-Daudé     uint32_t datacnt = s->blksize & BLOCK_SIZE_MASK;
68149ab747fSPaolo Bonzini 
68249ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
68349ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
68440bbc194SPeter Maydell             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
68549ab747fSPaolo Bonzini         }
686dd55c485SPhilippe Mathieu-Daudé         dma_memory_write(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt);
68749ab747fSPaolo Bonzini     } else {
688dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt);
68949ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
69040bbc194SPeter Maydell             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
69149ab747fSPaolo Bonzini         }
69249ab747fSPaolo Bonzini     }
69349ab747fSPaolo Bonzini     s->blkcnt--;
69449ab747fSPaolo Bonzini 
695d368ba43SKevin O'Connor     sdhci_end_transfer(s);
69649ab747fSPaolo Bonzini }
69749ab747fSPaolo Bonzini 
69849ab747fSPaolo Bonzini typedef struct ADMADescr {
69949ab747fSPaolo Bonzini     hwaddr addr;
70049ab747fSPaolo Bonzini     uint16_t length;
70149ab747fSPaolo Bonzini     uint8_t attr;
70249ab747fSPaolo Bonzini     uint8_t incr;
70349ab747fSPaolo Bonzini } ADMADescr;
70449ab747fSPaolo Bonzini 
70549ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
70649ab747fSPaolo Bonzini {
70749ab747fSPaolo Bonzini     uint32_t adma1 = 0;
70849ab747fSPaolo Bonzini     uint64_t adma2 = 0;
70949ab747fSPaolo Bonzini     hwaddr entry_addr = (hwaddr)s->admasysaddr;
71006c5120bSPhilippe Mathieu-Daudé     switch (SDHC_DMA_TYPE(s->hostctl1)) {
71149ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_32:
712dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma2,
71349ab747fSPaolo Bonzini                         sizeof(adma2));
71449ab747fSPaolo Bonzini         adma2 = le64_to_cpu(adma2);
71549ab747fSPaolo Bonzini         /* The spec does not specify endianness of descriptor table.
71649ab747fSPaolo Bonzini          * We currently assume that it is LE.
71749ab747fSPaolo Bonzini          */
71849ab747fSPaolo Bonzini         dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull;
71949ab747fSPaolo Bonzini         dscr->length = (uint16_t)extract64(adma2, 16, 16);
72049ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract64(adma2, 0, 7);
72149ab747fSPaolo Bonzini         dscr->incr = 8;
72249ab747fSPaolo Bonzini         break;
72349ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA1_32:
724dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma1,
72549ab747fSPaolo Bonzini                         sizeof(adma1));
72649ab747fSPaolo Bonzini         adma1 = le32_to_cpu(adma1);
72749ab747fSPaolo Bonzini         dscr->addr = (hwaddr)(adma1 & 0xFFFFF000);
72849ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract32(adma1, 0, 7);
72949ab747fSPaolo Bonzini         dscr->incr = 4;
73049ab747fSPaolo Bonzini         if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
73149ab747fSPaolo Bonzini             dscr->length = (uint16_t)extract32(adma1, 12, 16);
73249ab747fSPaolo Bonzini         } else {
73349ab747fSPaolo Bonzini             dscr->length = 4096;
73449ab747fSPaolo Bonzini         }
73549ab747fSPaolo Bonzini         break;
73649ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_64:
737dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr,
73849ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->attr), 1);
739dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 2,
74049ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->length), 2);
74149ab747fSPaolo Bonzini         dscr->length = le16_to_cpu(dscr->length);
742dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 4,
74349ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->addr), 8);
74404654b5aSSai Pavan Boddu         dscr->addr = le64_to_cpu(dscr->addr);
74504654b5aSSai Pavan Boddu         dscr->attr &= (uint8_t) ~0xC0;
74649ab747fSPaolo Bonzini         dscr->incr = 12;
74749ab747fSPaolo Bonzini         break;
74849ab747fSPaolo Bonzini     }
74949ab747fSPaolo Bonzini }
75049ab747fSPaolo Bonzini 
75149ab747fSPaolo Bonzini /* Advanced DMA data transfer */
75249ab747fSPaolo Bonzini 
75349ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s)
75449ab747fSPaolo Bonzini {
75549ab747fSPaolo Bonzini     unsigned int n, begin, length;
756bf8ec38eSPhilippe Mathieu-Daudé     const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK;
7578be487d8SPhilippe Mathieu-Daudé     ADMADescr dscr = {};
75849ab747fSPaolo Bonzini     int i;
75949ab747fSPaolo Bonzini 
76049ab747fSPaolo Bonzini     for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
76149ab747fSPaolo Bonzini         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
76249ab747fSPaolo Bonzini 
76349ab747fSPaolo Bonzini         get_adma_description(s, &dscr);
7648be487d8SPhilippe Mathieu-Daudé         trace_sdhci_adma_loop(dscr.addr, dscr.length, dscr.attr);
76549ab747fSPaolo Bonzini 
76649ab747fSPaolo Bonzini         if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
76749ab747fSPaolo Bonzini             /* Indicate that error occurred in ST_FDS state */
76849ab747fSPaolo Bonzini             s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
76949ab747fSPaolo Bonzini             s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
77049ab747fSPaolo Bonzini 
77149ab747fSPaolo Bonzini             /* Generate ADMA error interrupt */
77249ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_ADMAERR) {
77349ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_ADMAERR;
77449ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
77549ab747fSPaolo Bonzini             }
77649ab747fSPaolo Bonzini 
77749ab747fSPaolo Bonzini             sdhci_update_irq(s);
77849ab747fSPaolo Bonzini             return;
77949ab747fSPaolo Bonzini         }
78049ab747fSPaolo Bonzini 
78149ab747fSPaolo Bonzini         length = dscr.length ? dscr.length : 65536;
78249ab747fSPaolo Bonzini 
78349ab747fSPaolo Bonzini         switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) {
78449ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_TRAN:  /* data transfer */
78549ab747fSPaolo Bonzini 
78649ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_READ) {
78749ab747fSPaolo Bonzini                 while (length) {
78849ab747fSPaolo Bonzini                     if (s->data_count == 0) {
78949ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
79040bbc194SPeter Maydell                             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
79149ab747fSPaolo Bonzini                         }
79249ab747fSPaolo Bonzini                     }
79349ab747fSPaolo Bonzini                     begin = s->data_count;
79449ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
79549ab747fSPaolo Bonzini                         s->data_count = length + begin;
79649ab747fSPaolo Bonzini                         length = 0;
79749ab747fSPaolo Bonzini                      } else {
79849ab747fSPaolo Bonzini                         s->data_count = block_size;
79949ab747fSPaolo Bonzini                         length -= block_size - begin;
80049ab747fSPaolo Bonzini                     }
801dd55c485SPhilippe Mathieu-Daudé                     dma_memory_write(s->dma_as, dscr.addr,
80249ab747fSPaolo Bonzini                                      &s->fifo_buffer[begin],
80349ab747fSPaolo Bonzini                                      s->data_count - begin);
80449ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
80549ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
80649ab747fSPaolo Bonzini                         s->data_count = 0;
80749ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
80849ab747fSPaolo Bonzini                             s->blkcnt--;
80949ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
81049ab747fSPaolo Bonzini                                 break;
81149ab747fSPaolo Bonzini                             }
81249ab747fSPaolo Bonzini                         }
81349ab747fSPaolo Bonzini                     }
81449ab747fSPaolo Bonzini                 }
81549ab747fSPaolo Bonzini             } else {
81649ab747fSPaolo Bonzini                 while (length) {
81749ab747fSPaolo Bonzini                     begin = s->data_count;
81849ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
81949ab747fSPaolo Bonzini                         s->data_count = length + begin;
82049ab747fSPaolo Bonzini                         length = 0;
82149ab747fSPaolo Bonzini                      } else {
82249ab747fSPaolo Bonzini                         s->data_count = block_size;
82349ab747fSPaolo Bonzini                         length -= block_size - begin;
82449ab747fSPaolo Bonzini                     }
825dd55c485SPhilippe Mathieu-Daudé                     dma_memory_read(s->dma_as, dscr.addr,
8269db11cefSPeter Crosthwaite                                     &s->fifo_buffer[begin],
8279db11cefSPeter Crosthwaite                                     s->data_count - begin);
82849ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
82949ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
83049ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
83140bbc194SPeter Maydell                             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
83249ab747fSPaolo Bonzini                         }
83349ab747fSPaolo Bonzini                         s->data_count = 0;
83449ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
83549ab747fSPaolo Bonzini                             s->blkcnt--;
83649ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
83749ab747fSPaolo Bonzini                                 break;
83849ab747fSPaolo Bonzini                             }
83949ab747fSPaolo Bonzini                         }
84049ab747fSPaolo Bonzini                     }
84149ab747fSPaolo Bonzini                 }
84249ab747fSPaolo Bonzini             }
84349ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
84449ab747fSPaolo Bonzini             break;
84549ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
84649ab747fSPaolo Bonzini             s->admasysaddr = dscr.addr;
8478be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("link", s->admasysaddr);
84849ab747fSPaolo Bonzini             break;
84949ab747fSPaolo Bonzini         default:
85049ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
85149ab747fSPaolo Bonzini             break;
85249ab747fSPaolo Bonzini         }
85349ab747fSPaolo Bonzini 
8541d32c26fSPeter Crosthwaite         if (dscr.attr & SDHC_ADMA_ATTR_INT) {
8558be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("interrupt", s->admasysaddr);
8561d32c26fSPeter Crosthwaite             if (s->norintstsen & SDHC_NISEN_DMA) {
8571d32c26fSPeter Crosthwaite                 s->norintsts |= SDHC_NIS_DMA;
8581d32c26fSPeter Crosthwaite             }
8591d32c26fSPeter Crosthwaite 
8601d32c26fSPeter Crosthwaite             sdhci_update_irq(s);
8611d32c26fSPeter Crosthwaite         }
8621d32c26fSPeter Crosthwaite 
86349ab747fSPaolo Bonzini         /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
86449ab747fSPaolo Bonzini         if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
86549ab747fSPaolo Bonzini                     (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
8668be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma_transfer_completed();
86749ab747fSPaolo Bonzini             if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
86849ab747fSPaolo Bonzini                 (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
86949ab747fSPaolo Bonzini                 s->blkcnt != 0)) {
8708be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("SD/MMC host ADMA length mismatch");
87149ab747fSPaolo Bonzini                 s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
87249ab747fSPaolo Bonzini                         SDHC_ADMAERR_STATE_ST_TFR;
87349ab747fSPaolo Bonzini                 if (s->errintstsen & SDHC_EISEN_ADMAERR) {
8748be487d8SPhilippe Mathieu-Daudé                     trace_sdhci_error("Set ADMA error flag");
87549ab747fSPaolo Bonzini                     s->errintsts |= SDHC_EIS_ADMAERR;
87649ab747fSPaolo Bonzini                     s->norintsts |= SDHC_NIS_ERR;
87749ab747fSPaolo Bonzini                 }
87849ab747fSPaolo Bonzini 
87949ab747fSPaolo Bonzini                 sdhci_update_irq(s);
88049ab747fSPaolo Bonzini             }
881d368ba43SKevin O'Connor             sdhci_end_transfer(s);
88249ab747fSPaolo Bonzini             return;
88349ab747fSPaolo Bonzini         }
88449ab747fSPaolo Bonzini 
88549ab747fSPaolo Bonzini     }
88649ab747fSPaolo Bonzini 
88749ab747fSPaolo Bonzini     /* we have unfinished business - reschedule to continue ADMA */
888bc72ad67SAlex Bligh     timer_mod(s->transfer_timer,
889bc72ad67SAlex Bligh                    qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY);
89049ab747fSPaolo Bonzini }
89149ab747fSPaolo Bonzini 
89249ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */
89349ab747fSPaolo Bonzini 
894d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque)
89549ab747fSPaolo Bonzini {
896d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
89749ab747fSPaolo Bonzini 
89849ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_DMA) {
89906c5120bSPhilippe Mathieu-Daudé         switch (SDHC_DMA_TYPE(s->hostctl1)) {
90049ab747fSPaolo Bonzini         case SDHC_CTRL_SDMA:
90149ab747fSPaolo Bonzini             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
902d368ba43SKevin O'Connor                 sdhci_sdma_transfer_single_block(s);
90349ab747fSPaolo Bonzini             } else {
904d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
90549ab747fSPaolo Bonzini             }
90649ab747fSPaolo Bonzini 
90749ab747fSPaolo Bonzini             break;
90849ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA1_32:
9090540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA1_MASK)) {
9108be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA1 not supported");
91149ab747fSPaolo Bonzini                 break;
91249ab747fSPaolo Bonzini             }
91349ab747fSPaolo Bonzini 
914d368ba43SKevin O'Connor             sdhci_do_adma(s);
91549ab747fSPaolo Bonzini             break;
91649ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_32:
9170540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK)) {
9188be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA2 not supported");
91949ab747fSPaolo Bonzini                 break;
92049ab747fSPaolo Bonzini             }
92149ab747fSPaolo Bonzini 
922d368ba43SKevin O'Connor             sdhci_do_adma(s);
92349ab747fSPaolo Bonzini             break;
92449ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_64:
9250540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK) ||
9260540fba9SPhilippe Mathieu-Daudé                     !(s->capareg & R_SDHC_CAPAB_BUS64BIT_MASK)) {
9278be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("64 bit ADMA not supported");
92849ab747fSPaolo Bonzini                 break;
92949ab747fSPaolo Bonzini             }
93049ab747fSPaolo Bonzini 
931d368ba43SKevin O'Connor             sdhci_do_adma(s);
93249ab747fSPaolo Bonzini             break;
93349ab747fSPaolo Bonzini         default:
9348be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("Unsupported DMA type");
93549ab747fSPaolo Bonzini             break;
93649ab747fSPaolo Bonzini         }
93749ab747fSPaolo Bonzini     } else {
93840bbc194SPeter Maydell         if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) {
93949ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
94049ab747fSPaolo Bonzini                     SDHC_DAT_LINE_ACTIVE;
941d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
94249ab747fSPaolo Bonzini         } else {
94349ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
94449ab747fSPaolo Bonzini                     SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
945d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
94649ab747fSPaolo Bonzini         }
94749ab747fSPaolo Bonzini     }
94849ab747fSPaolo Bonzini }
94949ab747fSPaolo Bonzini 
95049ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s)
95149ab747fSPaolo Bonzini {
9526890a695SPeter Crosthwaite     if (!SDHC_CLOCK_IS_ON(s->clkcon) ||
95349ab747fSPaolo Bonzini         (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) &&
95449ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_DATA_PRESENT) ||
95549ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY &&
95649ab747fSPaolo Bonzini         !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) {
95749ab747fSPaolo Bonzini         return false;
95849ab747fSPaolo Bonzini     }
95949ab747fSPaolo Bonzini 
96049ab747fSPaolo Bonzini     return true;
96149ab747fSPaolo Bonzini }
96249ab747fSPaolo Bonzini 
96349ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and
96449ab747fSPaolo Bonzini  * continuous manner */
96549ab747fSPaolo Bonzini static inline bool
96649ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
96749ab747fSPaolo Bonzini {
96849ab747fSPaolo Bonzini     if ((s->data_count & 0x3) != byte_num) {
9698be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("Non-sequential access to Buffer Data Port register"
97049ab747fSPaolo Bonzini                           "is prohibited\n");
97149ab747fSPaolo Bonzini         return false;
97249ab747fSPaolo Bonzini     }
97349ab747fSPaolo Bonzini     return true;
97449ab747fSPaolo Bonzini }
97549ab747fSPaolo Bonzini 
976d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size)
97749ab747fSPaolo Bonzini {
978d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
97949ab747fSPaolo Bonzini     uint32_t ret = 0;
98049ab747fSPaolo Bonzini 
98149ab747fSPaolo Bonzini     switch (offset & ~0x3) {
98249ab747fSPaolo Bonzini     case SDHC_SYSAD:
98349ab747fSPaolo Bonzini         ret = s->sdmasysad;
98449ab747fSPaolo Bonzini         break;
98549ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
98649ab747fSPaolo Bonzini         ret = s->blksize | (s->blkcnt << 16);
98749ab747fSPaolo Bonzini         break;
98849ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
98949ab747fSPaolo Bonzini         ret = s->argument;
99049ab747fSPaolo Bonzini         break;
99149ab747fSPaolo Bonzini     case SDHC_TRNMOD:
99249ab747fSPaolo Bonzini         ret = s->trnmod | (s->cmdreg << 16);
99349ab747fSPaolo Bonzini         break;
99449ab747fSPaolo Bonzini     case SDHC_RSPREG0 ... SDHC_RSPREG3:
99549ab747fSPaolo Bonzini         ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2];
99649ab747fSPaolo Bonzini         break;
99749ab747fSPaolo Bonzini     case  SDHC_BDATA:
99849ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
999d368ba43SKevin O'Connor             ret = sdhci_read_dataport(s, size);
10008be487d8SPhilippe Mathieu-Daudé             trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
100149ab747fSPaolo Bonzini             return ret;
100249ab747fSPaolo Bonzini         }
100349ab747fSPaolo Bonzini         break;
100449ab747fSPaolo Bonzini     case SDHC_PRNSTS:
100549ab747fSPaolo Bonzini         ret = s->prnsts;
1006*da346922SPhilippe Mathieu-Daudé         ret = FIELD_DP32(ret, SDHC_PRNSTS, DAT_LVL,
1007*da346922SPhilippe Mathieu-Daudé                          sdbus_get_dat_lines(&s->sdbus));
1008*da346922SPhilippe Mathieu-Daudé         ret = FIELD_DP32(ret, SDHC_PRNSTS, CMD_LVL,
1009*da346922SPhilippe Mathieu-Daudé                          sdbus_get_cmd_line(&s->sdbus));
101049ab747fSPaolo Bonzini         break;
101149ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
101206c5120bSPhilippe Mathieu-Daudé         ret = s->hostctl1 | (s->pwrcon << 8) | (s->blkgap << 16) |
101349ab747fSPaolo Bonzini               (s->wakcon << 24);
101449ab747fSPaolo Bonzini         break;
101549ab747fSPaolo Bonzini     case SDHC_CLKCON:
101649ab747fSPaolo Bonzini         ret = s->clkcon | (s->timeoutcon << 16);
101749ab747fSPaolo Bonzini         break;
101849ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
101949ab747fSPaolo Bonzini         ret = s->norintsts | (s->errintsts << 16);
102049ab747fSPaolo Bonzini         break;
102149ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
102249ab747fSPaolo Bonzini         ret = s->norintstsen | (s->errintstsen << 16);
102349ab747fSPaolo Bonzini         break;
102449ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
102549ab747fSPaolo Bonzini         ret = s->norintsigen | (s->errintsigen << 16);
102649ab747fSPaolo Bonzini         break;
102749ab747fSPaolo Bonzini     case SDHC_ACMD12ERRSTS:
1028ea55a221SPhilippe Mathieu-Daudé         ret = s->acmd12errsts | (s->hostctl2 << 16);
102949ab747fSPaolo Bonzini         break;
1030cd209421SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
10315efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->capareg;
10325efc9016SPhilippe Mathieu-Daudé         break;
10335efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
10345efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->capareg >> 32);
103549ab747fSPaolo Bonzini         break;
103649ab747fSPaolo Bonzini     case SDHC_MAXCURR:
10375efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->maxcurr;
10385efc9016SPhilippe Mathieu-Daudé         break;
10395efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
10405efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->maxcurr >> 32);
104149ab747fSPaolo Bonzini         break;
104249ab747fSPaolo Bonzini     case SDHC_ADMAERR:
104349ab747fSPaolo Bonzini         ret =  s->admaerr;
104449ab747fSPaolo Bonzini         break;
104549ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
104649ab747fSPaolo Bonzini         ret = (uint32_t)s->admasysaddr;
104749ab747fSPaolo Bonzini         break;
104849ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
104949ab747fSPaolo Bonzini         ret = (uint32_t)(s->admasysaddr >> 32);
105049ab747fSPaolo Bonzini         break;
105149ab747fSPaolo Bonzini     case SDHC_SLOT_INT_STATUS:
1052aceb5b06SPhilippe Mathieu-Daudé         ret = (s->version << 16) | sdhci_slotint(s);
105349ab747fSPaolo Bonzini         break;
105449ab747fSPaolo Bonzini     default:
105500b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC rd_%ub @0x%02" HWADDR_PRIx " "
105600b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset);
105749ab747fSPaolo Bonzini         break;
105849ab747fSPaolo Bonzini     }
105949ab747fSPaolo Bonzini 
106049ab747fSPaolo Bonzini     ret >>= (offset & 0x3) * 8;
106149ab747fSPaolo Bonzini     ret &= (1ULL << (size * 8)) - 1;
10628be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
106349ab747fSPaolo Bonzini     return ret;
106449ab747fSPaolo Bonzini }
106549ab747fSPaolo Bonzini 
106649ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value)
106749ab747fSPaolo Bonzini {
106849ab747fSPaolo Bonzini     if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) {
106949ab747fSPaolo Bonzini         return;
107049ab747fSPaolo Bonzini     }
107149ab747fSPaolo Bonzini     s->blkgap = value & SDHC_STOP_AT_GAP_REQ;
107249ab747fSPaolo Bonzini 
107349ab747fSPaolo Bonzini     if ((value & SDHC_CONTINUE_REQ) && s->stopped_state &&
107449ab747fSPaolo Bonzini             (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) {
107549ab747fSPaolo Bonzini         if (s->stopped_state == sdhc_gap_read) {
107649ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ;
1077d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
107849ab747fSPaolo Bonzini         } else {
107949ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE;
1080d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
108149ab747fSPaolo Bonzini         }
108249ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
108349ab747fSPaolo Bonzini     } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) {
108449ab747fSPaolo Bonzini         if (s->prnsts & SDHC_DOING_READ) {
108549ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_read;
108649ab747fSPaolo Bonzini         } else if (s->prnsts & SDHC_DOING_WRITE) {
108749ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_write;
108849ab747fSPaolo Bonzini         }
108949ab747fSPaolo Bonzini     }
109049ab747fSPaolo Bonzini }
109149ab747fSPaolo Bonzini 
109249ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value)
109349ab747fSPaolo Bonzini {
109449ab747fSPaolo Bonzini     switch (value) {
109549ab747fSPaolo Bonzini     case SDHC_RESET_ALL:
1096d368ba43SKevin O'Connor         sdhci_reset(s);
109749ab747fSPaolo Bonzini         break;
109849ab747fSPaolo Bonzini     case SDHC_RESET_CMD:
109949ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_CMD_INHIBIT;
110049ab747fSPaolo Bonzini         s->norintsts &= ~SDHC_NIS_CMDCMP;
110149ab747fSPaolo Bonzini         break;
110249ab747fSPaolo Bonzini     case SDHC_RESET_DATA:
110349ab747fSPaolo Bonzini         s->data_count = 0;
110449ab747fSPaolo Bonzini         s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE |
110549ab747fSPaolo Bonzini                 SDHC_DOING_READ | SDHC_DOING_WRITE |
110649ab747fSPaolo Bonzini                 SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE);
110749ab747fSPaolo Bonzini         s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ);
110849ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
110949ab747fSPaolo Bonzini         s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY |
111049ab747fSPaolo Bonzini                 SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP);
111149ab747fSPaolo Bonzini         break;
111249ab747fSPaolo Bonzini     }
111349ab747fSPaolo Bonzini }
111449ab747fSPaolo Bonzini 
111549ab747fSPaolo Bonzini static void
1116d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
111749ab747fSPaolo Bonzini {
1118d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
111949ab747fSPaolo Bonzini     unsigned shift =  8 * (offset & 0x3);
112049ab747fSPaolo Bonzini     uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift);
1121d368ba43SKevin O'Connor     uint32_t value = val;
112249ab747fSPaolo Bonzini     value <<= shift;
112349ab747fSPaolo Bonzini 
112449ab747fSPaolo Bonzini     switch (offset & ~0x3) {
112549ab747fSPaolo Bonzini     case SDHC_SYSAD:
112649ab747fSPaolo Bonzini         s->sdmasysad = (s->sdmasysad & mask) | value;
112749ab747fSPaolo Bonzini         MASKED_WRITE(s->sdmasysad, mask, value);
112849ab747fSPaolo Bonzini         /* Writing to last byte of sdmasysad might trigger transfer */
112949ab747fSPaolo Bonzini         if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt &&
113006c5120bSPhilippe Mathieu-Daudé                 s->blksize && SDHC_DMA_TYPE(s->hostctl1) == SDHC_CTRL_SDMA) {
113145ba9f76SPrasad J Pandit             if (s->trnmod & SDHC_TRNS_MULTI) {
1132d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
113345ba9f76SPrasad J Pandit             } else {
113445ba9f76SPrasad J Pandit                 sdhci_sdma_transfer_single_block(s);
113545ba9f76SPrasad J Pandit             }
113649ab747fSPaolo Bonzini         }
113749ab747fSPaolo Bonzini         break;
113849ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
113949ab747fSPaolo Bonzini         if (!TRANSFERRING_DATA(s->prnsts)) {
114049ab747fSPaolo Bonzini             MASKED_WRITE(s->blksize, mask, value);
114149ab747fSPaolo Bonzini             MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16);
114249ab747fSPaolo Bonzini         }
11439201bb9aSAlistair Francis 
11449201bb9aSAlistair Francis         /* Limit block size to the maximum buffer size */
11459201bb9aSAlistair Francis         if (extract32(s->blksize, 0, 12) > s->buf_maxsz) {
11469201bb9aSAlistair Francis             qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than " \
11479201bb9aSAlistair Francis                           "the maximum buffer 0x%x", __func__, s->blksize,
11489201bb9aSAlistair Francis                           s->buf_maxsz);
11499201bb9aSAlistair Francis 
11509201bb9aSAlistair Francis             s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz);
11519201bb9aSAlistair Francis         }
11529201bb9aSAlistair Francis 
115349ab747fSPaolo Bonzini         break;
115449ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
115549ab747fSPaolo Bonzini         MASKED_WRITE(s->argument, mask, value);
115649ab747fSPaolo Bonzini         break;
115749ab747fSPaolo Bonzini     case SDHC_TRNMOD:
115849ab747fSPaolo Bonzini         /* DMA can be enabled only if it is supported as indicated by
115949ab747fSPaolo Bonzini          * capabilities register */
11606ff37c3dSPhilippe Mathieu-Daudé         if (!(s->capareg & R_SDHC_CAPAB_SDMA_MASK)) {
116149ab747fSPaolo Bonzini             value &= ~SDHC_TRNS_DMA;
116249ab747fSPaolo Bonzini         }
116324bddf9dSPhilippe Mathieu-Daudé         MASKED_WRITE(s->trnmod, mask, value & SDHC_TRNMOD_MASK);
116449ab747fSPaolo Bonzini         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
116549ab747fSPaolo Bonzini 
116649ab747fSPaolo Bonzini         /* Writing to the upper byte of CMDREG triggers SD command generation */
1167d368ba43SKevin O'Connor         if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) {
116849ab747fSPaolo Bonzini             break;
116949ab747fSPaolo Bonzini         }
117049ab747fSPaolo Bonzini 
1171d368ba43SKevin O'Connor         sdhci_send_command(s);
117249ab747fSPaolo Bonzini         break;
117349ab747fSPaolo Bonzini     case  SDHC_BDATA:
117449ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
1175d368ba43SKevin O'Connor             sdhci_write_dataport(s, value >> shift, size);
117649ab747fSPaolo Bonzini         }
117749ab747fSPaolo Bonzini         break;
117849ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
117949ab747fSPaolo Bonzini         if (!(mask & 0xFF0000)) {
118049ab747fSPaolo Bonzini             sdhci_blkgap_write(s, value >> 16);
118149ab747fSPaolo Bonzini         }
118206c5120bSPhilippe Mathieu-Daudé         MASKED_WRITE(s->hostctl1, mask, value);
118349ab747fSPaolo Bonzini         MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8);
118449ab747fSPaolo Bonzini         MASKED_WRITE(s->wakcon, mask >> 24, value >> 24);
118549ab747fSPaolo Bonzini         if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 ||
118649ab747fSPaolo Bonzini                 !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) {
118749ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
118849ab747fSPaolo Bonzini         }
118949ab747fSPaolo Bonzini         break;
119049ab747fSPaolo Bonzini     case SDHC_CLKCON:
119149ab747fSPaolo Bonzini         if (!(mask & 0xFF000000)) {
119249ab747fSPaolo Bonzini             sdhci_reset_write(s, value >> 24);
119349ab747fSPaolo Bonzini         }
119449ab747fSPaolo Bonzini         MASKED_WRITE(s->clkcon, mask, value);
119549ab747fSPaolo Bonzini         MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16);
119649ab747fSPaolo Bonzini         if (s->clkcon & SDHC_CLOCK_INT_EN) {
119749ab747fSPaolo Bonzini             s->clkcon |= SDHC_CLOCK_INT_STABLE;
119849ab747fSPaolo Bonzini         } else {
119949ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_INT_STABLE;
120049ab747fSPaolo Bonzini         }
120149ab747fSPaolo Bonzini         break;
120249ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
120349ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_CARDINT) {
120449ab747fSPaolo Bonzini             value &= ~SDHC_NIS_CARDINT;
120549ab747fSPaolo Bonzini         }
120649ab747fSPaolo Bonzini         s->norintsts &= mask | ~value;
120749ab747fSPaolo Bonzini         s->errintsts &= (mask >> 16) | ~(value >> 16);
120849ab747fSPaolo Bonzini         if (s->errintsts) {
120949ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
121049ab747fSPaolo Bonzini         } else {
121149ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
121249ab747fSPaolo Bonzini         }
121349ab747fSPaolo Bonzini         sdhci_update_irq(s);
121449ab747fSPaolo Bonzini         break;
121549ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
121649ab747fSPaolo Bonzini         MASKED_WRITE(s->norintstsen, mask, value);
121749ab747fSPaolo Bonzini         MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16);
121849ab747fSPaolo Bonzini         s->norintsts &= s->norintstsen;
121949ab747fSPaolo Bonzini         s->errintsts &= s->errintstsen;
122049ab747fSPaolo Bonzini         if (s->errintsts) {
122149ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
122249ab747fSPaolo Bonzini         } else {
122349ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
122449ab747fSPaolo Bonzini         }
12250a7ac9f9SAndrew Baumann         /* Quirk for Raspberry Pi: pending card insert interrupt
12260a7ac9f9SAndrew Baumann          * appears when first enabled after power on */
12270a7ac9f9SAndrew Baumann         if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) {
12280a7ac9f9SAndrew Baumann             assert(s->pending_insert_quirk);
12290a7ac9f9SAndrew Baumann             s->norintsts |= SDHC_NIS_INSERT;
12300a7ac9f9SAndrew Baumann             s->pending_insert_state = false;
12310a7ac9f9SAndrew Baumann         }
123249ab747fSPaolo Bonzini         sdhci_update_irq(s);
123349ab747fSPaolo Bonzini         break;
123449ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
123549ab747fSPaolo Bonzini         MASKED_WRITE(s->norintsigen, mask, value);
123649ab747fSPaolo Bonzini         MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16);
123749ab747fSPaolo Bonzini         sdhci_update_irq(s);
123849ab747fSPaolo Bonzini         break;
123949ab747fSPaolo Bonzini     case SDHC_ADMAERR:
124049ab747fSPaolo Bonzini         MASKED_WRITE(s->admaerr, mask, value);
124149ab747fSPaolo Bonzini         break;
124249ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
124349ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL |
124449ab747fSPaolo Bonzini                 (uint64_t)mask)) | (uint64_t)value;
124549ab747fSPaolo Bonzini         break;
124649ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
124749ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL |
124849ab747fSPaolo Bonzini                 ((uint64_t)mask << 32))) | ((uint64_t)value << 32);
124949ab747fSPaolo Bonzini         break;
125049ab747fSPaolo Bonzini     case SDHC_FEAER:
125149ab747fSPaolo Bonzini         s->acmd12errsts |= value;
125249ab747fSPaolo Bonzini         s->errintsts |= (value >> 16) & s->errintstsen;
125349ab747fSPaolo Bonzini         if (s->acmd12errsts) {
125449ab747fSPaolo Bonzini             s->errintsts |= SDHC_EIS_CMD12ERR;
125549ab747fSPaolo Bonzini         }
125649ab747fSPaolo Bonzini         if (s->errintsts) {
125749ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
125849ab747fSPaolo Bonzini         }
125949ab747fSPaolo Bonzini         sdhci_update_irq(s);
126049ab747fSPaolo Bonzini         break;
12615d2c0464SAndrey Smirnov     case SDHC_ACMD12ERRSTS:
12620034ebe6SPhilippe Mathieu-Daudé         MASKED_WRITE(s->acmd12errsts, mask, value & UINT16_MAX);
12630034ebe6SPhilippe Mathieu-Daudé         if (s->uhs_mode >= UHS_I) {
12640034ebe6SPhilippe Mathieu-Daudé             MASKED_WRITE(s->hostctl2, mask >> 16, value >> 16);
12650034ebe6SPhilippe Mathieu-Daudé 
12660034ebe6SPhilippe Mathieu-Daudé             if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, V18_ENA)) {
12670034ebe6SPhilippe Mathieu-Daudé                 sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_1_8V);
12680034ebe6SPhilippe Mathieu-Daudé             } else {
12690034ebe6SPhilippe Mathieu-Daudé                 sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_3_3V);
12700034ebe6SPhilippe Mathieu-Daudé             }
12710034ebe6SPhilippe Mathieu-Daudé         }
12725d2c0464SAndrey Smirnov         break;
12735efc9016SPhilippe Mathieu-Daudé 
12745efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
12755efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
12765efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR:
12775efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
12785efc9016SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_GUEST_ERROR, "SDHC wr_%ub @0x%02" HWADDR_PRIx
12795efc9016SPhilippe Mathieu-Daudé                       " <- 0x%08x read-only\n", size, offset, value >> shift);
12805efc9016SPhilippe Mathieu-Daudé         break;
12815efc9016SPhilippe Mathieu-Daudé 
128249ab747fSPaolo Bonzini     default:
128300b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC wr_%ub @0x%02" HWADDR_PRIx " <- 0x%08x "
128400b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset, value >> shift);
128549ab747fSPaolo Bonzini         break;
128649ab747fSPaolo Bonzini     }
12878be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("wr", size << 3, offset, "<-",
12888be487d8SPhilippe Mathieu-Daudé                        value >> shift, value >> shift);
128949ab747fSPaolo Bonzini }
129049ab747fSPaolo Bonzini 
129149ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = {
1292d368ba43SKevin O'Connor     .read = sdhci_read,
1293d368ba43SKevin O'Connor     .write = sdhci_write,
129449ab747fSPaolo Bonzini     .valid = {
129549ab747fSPaolo Bonzini         .min_access_size = 1,
129649ab747fSPaolo Bonzini         .max_access_size = 4,
129749ab747fSPaolo Bonzini         .unaligned = false
129849ab747fSPaolo Bonzini     },
129949ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
130049ab747fSPaolo Bonzini };
130149ab747fSPaolo Bonzini 
1302aceb5b06SPhilippe Mathieu-Daudé static void sdhci_init_readonly_registers(SDHCIState *s, Error **errp)
1303aceb5b06SPhilippe Mathieu-Daudé {
13046ff37c3dSPhilippe Mathieu-Daudé     Error *local_err = NULL;
13056ff37c3dSPhilippe Mathieu-Daudé 
13064d67852dSPhilippe Mathieu-Daudé     switch (s->sd_spec_version) {
13074d67852dSPhilippe Mathieu-Daudé     case 2 ... 3:
13084d67852dSPhilippe Mathieu-Daudé         break;
13094d67852dSPhilippe Mathieu-Daudé     default:
13104d67852dSPhilippe Mathieu-Daudé         error_setg(errp, "Only Spec v2/v3 are supported");
1311aceb5b06SPhilippe Mathieu-Daudé         return;
1312aceb5b06SPhilippe Mathieu-Daudé     }
1313aceb5b06SPhilippe Mathieu-Daudé     s->version = (SDHC_HCVER_VENDOR << 8) | (s->sd_spec_version - 1);
13146ff37c3dSPhilippe Mathieu-Daudé 
13156ff37c3dSPhilippe Mathieu-Daudé     sdhci_check_capareg(s, &local_err);
13166ff37c3dSPhilippe Mathieu-Daudé     if (local_err) {
13176ff37c3dSPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
13186ff37c3dSPhilippe Mathieu-Daudé         return;
13196ff37c3dSPhilippe Mathieu-Daudé     }
1320aceb5b06SPhilippe Mathieu-Daudé }
1321aceb5b06SPhilippe Mathieu-Daudé 
1322b635d98cSPhilippe Mathieu-Daudé /* --- qdev common --- */
1323b635d98cSPhilippe Mathieu-Daudé 
1324b635d98cSPhilippe Mathieu-Daudé #define DEFINE_SDHCI_COMMON_PROPERTIES(_state) \
1325aceb5b06SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT8("sd-spec-version", _state, sd_spec_version, 2), \
13260034ebe6SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT8("uhs", _state, uhs_mode, UHS_NOT_SUPPORTED), \
1327aceb5b06SPhilippe Mathieu-Daudé     \
1328aceb5b06SPhilippe Mathieu-Daudé     /* Capabilities registers provide information on supported
1329aceb5b06SPhilippe Mathieu-Daudé      * features of this specific host controller implementation */ \
13305efc9016SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("capareg", _state, capareg, SDHC_CAPAB_REG_DEFAULT), \
13315efc9016SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("maxcurr", _state, maxcurr, 0)
1332b635d98cSPhilippe Mathieu-Daudé 
133340bbc194SPeter Maydell static void sdhci_initfn(SDHCIState *s)
133449ab747fSPaolo Bonzini {
133540bbc194SPeter Maydell     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus),
133640bbc194SPeter Maydell                         TYPE_SDHCI_BUS, DEVICE(s), "sd-bus");
133749ab747fSPaolo Bonzini 
1338bc72ad67SAlex Bligh     s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s);
1339d368ba43SKevin O'Connor     s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s);
1340fd1e5c81SAndrey Smirnov 
1341fd1e5c81SAndrey Smirnov     s->io_ops = &sdhci_mmio_ops;
134249ab747fSPaolo Bonzini }
134349ab747fSPaolo Bonzini 
13447302dcd6SKevin O'Connor static void sdhci_uninitfn(SDHCIState *s)
134549ab747fSPaolo Bonzini {
1346bc72ad67SAlex Bligh     timer_del(s->insert_timer);
1347bc72ad67SAlex Bligh     timer_free(s->insert_timer);
1348bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
1349bc72ad67SAlex Bligh     timer_free(s->transfer_timer);
135049ab747fSPaolo Bonzini 
135149ab747fSPaolo Bonzini     g_free(s->fifo_buffer);
135249ab747fSPaolo Bonzini     s->fifo_buffer = NULL;
135349ab747fSPaolo Bonzini }
135449ab747fSPaolo Bonzini 
135525367498SPhilippe Mathieu-Daudé static void sdhci_common_realize(SDHCIState *s, Error **errp)
135625367498SPhilippe Mathieu-Daudé {
1357aceb5b06SPhilippe Mathieu-Daudé     Error *local_err = NULL;
1358aceb5b06SPhilippe Mathieu-Daudé 
1359aceb5b06SPhilippe Mathieu-Daudé     sdhci_init_readonly_registers(s, &local_err);
1360aceb5b06SPhilippe Mathieu-Daudé     if (local_err) {
1361aceb5b06SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
1362aceb5b06SPhilippe Mathieu-Daudé         return;
1363aceb5b06SPhilippe Mathieu-Daudé     }
136425367498SPhilippe Mathieu-Daudé     s->buf_maxsz = sdhci_get_fifolen(s);
136525367498SPhilippe Mathieu-Daudé     s->fifo_buffer = g_malloc0(s->buf_maxsz);
136625367498SPhilippe Mathieu-Daudé 
136725367498SPhilippe Mathieu-Daudé     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci",
136825367498SPhilippe Mathieu-Daudé                           SDHC_REGISTERS_MAP_SIZE);
136925367498SPhilippe Mathieu-Daudé }
137025367498SPhilippe Mathieu-Daudé 
13718b7455c7SPhilippe Mathieu-Daudé static void sdhci_common_unrealize(SDHCIState *s, Error **errp)
13728b7455c7SPhilippe Mathieu-Daudé {
13738b7455c7SPhilippe Mathieu-Daudé     /* This function is expected to be called only once for each class:
13748b7455c7SPhilippe Mathieu-Daudé      * - SysBus:    via DeviceClass->unrealize(),
13758b7455c7SPhilippe Mathieu-Daudé      * - PCI:       via PCIDeviceClass->exit().
13768b7455c7SPhilippe Mathieu-Daudé      * However to avoid double-free and/or use-after-free we still nullify
13778b7455c7SPhilippe Mathieu-Daudé      * this variable (better safe than sorry!). */
13788b7455c7SPhilippe Mathieu-Daudé     g_free(s->fifo_buffer);
13798b7455c7SPhilippe Mathieu-Daudé     s->fifo_buffer = NULL;
13808b7455c7SPhilippe Mathieu-Daudé }
13818b7455c7SPhilippe Mathieu-Daudé 
13820a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque)
13830a7ac9f9SAndrew Baumann {
13840a7ac9f9SAndrew Baumann     SDHCIState *s = opaque;
13850a7ac9f9SAndrew Baumann 
13860a7ac9f9SAndrew Baumann     return s->pending_insert_state;
13870a7ac9f9SAndrew Baumann }
13880a7ac9f9SAndrew Baumann 
13890a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = {
13900a7ac9f9SAndrew Baumann     .name = "sdhci/pending-insert",
13910a7ac9f9SAndrew Baumann     .version_id = 1,
13920a7ac9f9SAndrew Baumann     .minimum_version_id = 1,
13930a7ac9f9SAndrew Baumann     .needed = sdhci_pending_insert_vmstate_needed,
13940a7ac9f9SAndrew Baumann     .fields = (VMStateField[]) {
13950a7ac9f9SAndrew Baumann         VMSTATE_BOOL(pending_insert_state, SDHCIState),
13960a7ac9f9SAndrew Baumann         VMSTATE_END_OF_LIST()
13970a7ac9f9SAndrew Baumann     },
13980a7ac9f9SAndrew Baumann };
13990a7ac9f9SAndrew Baumann 
140049ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = {
140149ab747fSPaolo Bonzini     .name = "sdhci",
140249ab747fSPaolo Bonzini     .version_id = 1,
140349ab747fSPaolo Bonzini     .minimum_version_id = 1,
140449ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
140549ab747fSPaolo Bonzini         VMSTATE_UINT32(sdmasysad, SDHCIState),
140649ab747fSPaolo Bonzini         VMSTATE_UINT16(blksize, SDHCIState),
140749ab747fSPaolo Bonzini         VMSTATE_UINT16(blkcnt, SDHCIState),
140849ab747fSPaolo Bonzini         VMSTATE_UINT32(argument, SDHCIState),
140949ab747fSPaolo Bonzini         VMSTATE_UINT16(trnmod, SDHCIState),
141049ab747fSPaolo Bonzini         VMSTATE_UINT16(cmdreg, SDHCIState),
141149ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4),
141249ab747fSPaolo Bonzini         VMSTATE_UINT32(prnsts, SDHCIState),
141306c5120bSPhilippe Mathieu-Daudé         VMSTATE_UINT8(hostctl1, SDHCIState),
141449ab747fSPaolo Bonzini         VMSTATE_UINT8(pwrcon, SDHCIState),
141549ab747fSPaolo Bonzini         VMSTATE_UINT8(blkgap, SDHCIState),
141649ab747fSPaolo Bonzini         VMSTATE_UINT8(wakcon, SDHCIState),
141749ab747fSPaolo Bonzini         VMSTATE_UINT16(clkcon, SDHCIState),
141849ab747fSPaolo Bonzini         VMSTATE_UINT8(timeoutcon, SDHCIState),
141949ab747fSPaolo Bonzini         VMSTATE_UINT8(admaerr, SDHCIState),
142049ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsts, SDHCIState),
142149ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsts, SDHCIState),
142249ab747fSPaolo Bonzini         VMSTATE_UINT16(norintstsen, SDHCIState),
142349ab747fSPaolo Bonzini         VMSTATE_UINT16(errintstsen, SDHCIState),
142449ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsigen, SDHCIState),
142549ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsigen, SDHCIState),
142649ab747fSPaolo Bonzini         VMSTATE_UINT16(acmd12errsts, SDHCIState),
142749ab747fSPaolo Bonzini         VMSTATE_UINT16(data_count, SDHCIState),
142849ab747fSPaolo Bonzini         VMSTATE_UINT64(admasysaddr, SDHCIState),
142949ab747fSPaolo Bonzini         VMSTATE_UINT8(stopped_state, SDHCIState),
143059046ec2SHalil Pasic         VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz),
1431e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(insert_timer, SDHCIState),
1432e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(transfer_timer, SDHCIState),
143349ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
14340a7ac9f9SAndrew Baumann     },
14350a7ac9f9SAndrew Baumann     .subsections = (const VMStateDescription*[]) {
14360a7ac9f9SAndrew Baumann         &sdhci_pending_insert_vmstate,
14370a7ac9f9SAndrew Baumann         NULL
14380a7ac9f9SAndrew Baumann     },
143949ab747fSPaolo Bonzini };
144049ab747fSPaolo Bonzini 
14411c92c505SPhilippe Mathieu-Daudé static void sdhci_common_class_init(ObjectClass *klass, void *data)
14421c92c505SPhilippe Mathieu-Daudé {
14431c92c505SPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(klass);
14441c92c505SPhilippe Mathieu-Daudé 
14451c92c505SPhilippe Mathieu-Daudé     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
14461c92c505SPhilippe Mathieu-Daudé     dc->vmsd = &sdhci_vmstate;
14471c92c505SPhilippe Mathieu-Daudé     dc->reset = sdhci_poweron_reset;
14481c92c505SPhilippe Mathieu-Daudé }
14491c92c505SPhilippe Mathieu-Daudé 
1450b635d98cSPhilippe Mathieu-Daudé /* --- qdev PCI --- */
1451b635d98cSPhilippe Mathieu-Daudé 
14525ec911c3SKevin O'Connor static Property sdhci_pci_properties[] = {
1453b635d98cSPhilippe Mathieu-Daudé     DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState),
145449ab747fSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
145549ab747fSPaolo Bonzini };
145649ab747fSPaolo Bonzini 
14579af21dbeSMarkus Armbruster static void sdhci_pci_realize(PCIDevice *dev, Error **errp)
1458224d10ffSKevin O'Connor {
1459224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
1460ab958e38SPhilippe Mathieu-Daudé     Error *local_err = NULL;
146125367498SPhilippe Mathieu-Daudé 
146225367498SPhilippe Mathieu-Daudé     sdhci_initfn(s);
146325367498SPhilippe Mathieu-Daudé     sdhci_common_realize(s, errp);
1464ab958e38SPhilippe Mathieu-Daudé     if (local_err) {
1465ab958e38SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
146625367498SPhilippe Mathieu-Daudé         return;
146725367498SPhilippe Mathieu-Daudé     }
146825367498SPhilippe Mathieu-Daudé 
1469224d10ffSKevin O'Connor     dev->config[PCI_CLASS_PROG] = 0x01; /* Standard Host supported DMA */
1470224d10ffSKevin O'Connor     dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
1471224d10ffSKevin O'Connor     s->irq = pci_allocate_irq(dev);
1472dd55c485SPhilippe Mathieu-Daudé     s->dma_as = pci_get_address_space(dev);
1473dd55c485SPhilippe Mathieu-Daudé     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->iomem);
1474224d10ffSKevin O'Connor }
1475224d10ffSKevin O'Connor 
1476224d10ffSKevin O'Connor static void sdhci_pci_exit(PCIDevice *dev)
1477224d10ffSKevin O'Connor {
1478224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
14798b7455c7SPhilippe Mathieu-Daudé 
14808b7455c7SPhilippe Mathieu-Daudé     sdhci_common_unrealize(s, &error_abort);
1481224d10ffSKevin O'Connor     sdhci_uninitfn(s);
1482224d10ffSKevin O'Connor }
1483224d10ffSKevin O'Connor 
1484224d10ffSKevin O'Connor static void sdhci_pci_class_init(ObjectClass *klass, void *data)
1485224d10ffSKevin O'Connor {
1486224d10ffSKevin O'Connor     DeviceClass *dc = DEVICE_CLASS(klass);
1487224d10ffSKevin O'Connor     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1488224d10ffSKevin O'Connor 
14899af21dbeSMarkus Armbruster     k->realize = sdhci_pci_realize;
1490224d10ffSKevin O'Connor     k->exit = sdhci_pci_exit;
1491224d10ffSKevin O'Connor     k->vendor_id = PCI_VENDOR_ID_REDHAT;
1492224d10ffSKevin O'Connor     k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI;
1493224d10ffSKevin O'Connor     k->class_id = PCI_CLASS_SYSTEM_SDHCI;
14945ec911c3SKevin O'Connor     dc->props = sdhci_pci_properties;
14951c92c505SPhilippe Mathieu-Daudé 
14961c92c505SPhilippe Mathieu-Daudé     sdhci_common_class_init(klass, data);
1497224d10ffSKevin O'Connor }
1498224d10ffSKevin O'Connor 
1499224d10ffSKevin O'Connor static const TypeInfo sdhci_pci_info = {
1500224d10ffSKevin O'Connor     .name = TYPE_PCI_SDHCI,
1501224d10ffSKevin O'Connor     .parent = TYPE_PCI_DEVICE,
1502224d10ffSKevin O'Connor     .instance_size = sizeof(SDHCIState),
1503224d10ffSKevin O'Connor     .class_init = sdhci_pci_class_init,
1504fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
1505fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1506fd3b02c8SEduardo Habkost         { },
1507fd3b02c8SEduardo Habkost     },
1508224d10ffSKevin O'Connor };
1509224d10ffSKevin O'Connor 
1510b635d98cSPhilippe Mathieu-Daudé /* --- qdev SysBus --- */
1511b635d98cSPhilippe Mathieu-Daudé 
15125ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = {
1513b635d98cSPhilippe Mathieu-Daudé     DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState),
15140a7ac9f9SAndrew Baumann     DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk,
15150a7ac9f9SAndrew Baumann                      false),
151660765b6cSPhilippe Mathieu-Daudé     DEFINE_PROP_LINK("dma", SDHCIState,
151760765b6cSPhilippe Mathieu-Daudé                      dma_mr, TYPE_MEMORY_REGION, MemoryRegion *),
15185ec911c3SKevin O'Connor     DEFINE_PROP_END_OF_LIST(),
15195ec911c3SKevin O'Connor };
15205ec911c3SKevin O'Connor 
15217302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj)
152249ab747fSPaolo Bonzini {
15237302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
15245ec911c3SKevin O'Connor 
152540bbc194SPeter Maydell     sdhci_initfn(s);
15267302dcd6SKevin O'Connor }
15277302dcd6SKevin O'Connor 
15287302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj)
15297302dcd6SKevin O'Connor {
15307302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
153160765b6cSPhilippe Mathieu-Daudé 
153260765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
153360765b6cSPhilippe Mathieu-Daudé         object_unparent(OBJECT(s->dma_mr));
153460765b6cSPhilippe Mathieu-Daudé     }
153560765b6cSPhilippe Mathieu-Daudé 
15367302dcd6SKevin O'Connor     sdhci_uninitfn(s);
15377302dcd6SKevin O'Connor }
15387302dcd6SKevin O'Connor 
15397302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp)
15407302dcd6SKevin O'Connor {
15417302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(dev);
154249ab747fSPaolo Bonzini     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1543ab958e38SPhilippe Mathieu-Daudé     Error *local_err = NULL;
154449ab747fSPaolo Bonzini 
154525367498SPhilippe Mathieu-Daudé     sdhci_common_realize(s, errp);
1546ab958e38SPhilippe Mathieu-Daudé     if (local_err) {
1547ab958e38SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
154825367498SPhilippe Mathieu-Daudé         return;
154925367498SPhilippe Mathieu-Daudé     }
155025367498SPhilippe Mathieu-Daudé 
155160765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
155202e57e1cSPhilippe Mathieu-Daudé         s->dma_as = &s->sysbus_dma_as;
155360765b6cSPhilippe Mathieu-Daudé         address_space_init(s->dma_as, s->dma_mr, "sdhci-dma");
155460765b6cSPhilippe Mathieu-Daudé     } else {
155560765b6cSPhilippe Mathieu-Daudé         /* use system_memory() if property "dma" not set */
1556dd55c485SPhilippe Mathieu-Daudé         s->dma_as = &address_space_memory;
155760765b6cSPhilippe Mathieu-Daudé     }
1558dd55c485SPhilippe Mathieu-Daudé 
155949ab747fSPaolo Bonzini     sysbus_init_irq(sbd, &s->irq);
1560fd1e5c81SAndrey Smirnov 
1561fd1e5c81SAndrey Smirnov     memory_region_init_io(&s->iomem, OBJECT(s), s->io_ops, s, "sdhci",
1562fd1e5c81SAndrey Smirnov             SDHC_REGISTERS_MAP_SIZE);
1563fd1e5c81SAndrey Smirnov 
156449ab747fSPaolo Bonzini     sysbus_init_mmio(sbd, &s->iomem);
156549ab747fSPaolo Bonzini }
156649ab747fSPaolo Bonzini 
15678b7455c7SPhilippe Mathieu-Daudé static void sdhci_sysbus_unrealize(DeviceState *dev, Error **errp)
15688b7455c7SPhilippe Mathieu-Daudé {
15698b7455c7SPhilippe Mathieu-Daudé     SDHCIState *s = SYSBUS_SDHCI(dev);
15708b7455c7SPhilippe Mathieu-Daudé 
15718b7455c7SPhilippe Mathieu-Daudé     sdhci_common_unrealize(s, &error_abort);
157260765b6cSPhilippe Mathieu-Daudé 
157360765b6cSPhilippe Mathieu-Daudé      if (s->dma_mr) {
157460765b6cSPhilippe Mathieu-Daudé         address_space_destroy(s->dma_as);
157560765b6cSPhilippe Mathieu-Daudé     }
15768b7455c7SPhilippe Mathieu-Daudé }
15778b7455c7SPhilippe Mathieu-Daudé 
15787302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)
157949ab747fSPaolo Bonzini {
158049ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
158149ab747fSPaolo Bonzini 
15825ec911c3SKevin O'Connor     dc->props = sdhci_sysbus_properties;
15837302dcd6SKevin O'Connor     dc->realize = sdhci_sysbus_realize;
15848b7455c7SPhilippe Mathieu-Daudé     dc->unrealize = sdhci_sysbus_unrealize;
15851c92c505SPhilippe Mathieu-Daudé 
15861c92c505SPhilippe Mathieu-Daudé     sdhci_common_class_init(klass, data);
158749ab747fSPaolo Bonzini }
158849ab747fSPaolo Bonzini 
15897302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = {
15907302dcd6SKevin O'Connor     .name = TYPE_SYSBUS_SDHCI,
159149ab747fSPaolo Bonzini     .parent = TYPE_SYS_BUS_DEVICE,
159249ab747fSPaolo Bonzini     .instance_size = sizeof(SDHCIState),
15937302dcd6SKevin O'Connor     .instance_init = sdhci_sysbus_init,
15947302dcd6SKevin O'Connor     .instance_finalize = sdhci_sysbus_finalize,
15957302dcd6SKevin O'Connor     .class_init = sdhci_sysbus_class_init,
159649ab747fSPaolo Bonzini };
159749ab747fSPaolo Bonzini 
1598b635d98cSPhilippe Mathieu-Daudé /* --- qdev bus master --- */
1599b635d98cSPhilippe Mathieu-Daudé 
160040bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data)
160140bbc194SPeter Maydell {
160240bbc194SPeter Maydell     SDBusClass *sbc = SD_BUS_CLASS(klass);
160340bbc194SPeter Maydell 
160440bbc194SPeter Maydell     sbc->set_inserted = sdhci_set_inserted;
160540bbc194SPeter Maydell     sbc->set_readonly = sdhci_set_readonly;
160640bbc194SPeter Maydell }
160740bbc194SPeter Maydell 
160840bbc194SPeter Maydell static const TypeInfo sdhci_bus_info = {
160940bbc194SPeter Maydell     .name = TYPE_SDHCI_BUS,
161040bbc194SPeter Maydell     .parent = TYPE_SD_BUS,
161140bbc194SPeter Maydell     .instance_size = sizeof(SDBus),
161240bbc194SPeter Maydell     .class_init = sdhci_bus_class_init,
161340bbc194SPeter Maydell };
161440bbc194SPeter Maydell 
1615fd1e5c81SAndrey Smirnov static uint64_t usdhc_read(void *opaque, hwaddr offset, unsigned size)
1616fd1e5c81SAndrey Smirnov {
1617fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
1618fd1e5c81SAndrey Smirnov     uint32_t ret;
161906c5120bSPhilippe Mathieu-Daudé     uint16_t hostctl1;
1620fd1e5c81SAndrey Smirnov 
1621fd1e5c81SAndrey Smirnov     switch (offset) {
1622fd1e5c81SAndrey Smirnov     default:
1623fd1e5c81SAndrey Smirnov         return sdhci_read(opaque, offset, size);
1624fd1e5c81SAndrey Smirnov 
1625fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1626fd1e5c81SAndrey Smirnov         /*
1627fd1e5c81SAndrey Smirnov          * For a detailed explanation on the following bit
1628fd1e5c81SAndrey Smirnov          * manipulation code see comments in a similar part of
1629fd1e5c81SAndrey Smirnov          * usdhc_write()
1630fd1e5c81SAndrey Smirnov          */
163106c5120bSPhilippe Mathieu-Daudé         hostctl1 = SDHC_DMA_TYPE(s->hostctl1) << (8 - 3);
1632fd1e5c81SAndrey Smirnov 
163306c5120bSPhilippe Mathieu-Daudé         if (s->hostctl1 & SDHC_CTRL_8BITBUS) {
163406c5120bSPhilippe Mathieu-Daudé             hostctl1 |= ESDHC_CTRL_8BITBUS;
1635fd1e5c81SAndrey Smirnov         }
1636fd1e5c81SAndrey Smirnov 
163706c5120bSPhilippe Mathieu-Daudé         if (s->hostctl1 & SDHC_CTRL_4BITBUS) {
163806c5120bSPhilippe Mathieu-Daudé             hostctl1 |= ESDHC_CTRL_4BITBUS;
1639fd1e5c81SAndrey Smirnov         }
1640fd1e5c81SAndrey Smirnov 
164106c5120bSPhilippe Mathieu-Daudé         ret  = hostctl1;
1642fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->blkgap << 16;
1643fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->wakcon << 24;
1644fd1e5c81SAndrey Smirnov 
1645fd1e5c81SAndrey Smirnov         break;
1646fd1e5c81SAndrey Smirnov 
1647fd1e5c81SAndrey Smirnov     case ESDHC_DLL_CTRL:
1648fd1e5c81SAndrey Smirnov     case ESDHC_TUNE_CTRL_STATUS:
1649fd1e5c81SAndrey Smirnov     case ESDHC_UNDOCUMENTED_REG27:
1650fd1e5c81SAndrey Smirnov     case ESDHC_TUNING_CTRL:
1651fd1e5c81SAndrey Smirnov     case ESDHC_VENDOR_SPEC:
1652fd1e5c81SAndrey Smirnov     case ESDHC_MIX_CTRL:
1653fd1e5c81SAndrey Smirnov     case ESDHC_WTMK_LVL:
1654fd1e5c81SAndrey Smirnov         ret = 0;
1655fd1e5c81SAndrey Smirnov         break;
1656fd1e5c81SAndrey Smirnov     }
1657fd1e5c81SAndrey Smirnov 
1658fd1e5c81SAndrey Smirnov     return ret;
1659fd1e5c81SAndrey Smirnov }
1660fd1e5c81SAndrey Smirnov 
1661fd1e5c81SAndrey Smirnov static void
1662fd1e5c81SAndrey Smirnov usdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
1663fd1e5c81SAndrey Smirnov {
1664fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
166506c5120bSPhilippe Mathieu-Daudé     uint8_t hostctl1;
1666fd1e5c81SAndrey Smirnov     uint32_t value = (uint32_t)val;
1667fd1e5c81SAndrey Smirnov 
1668fd1e5c81SAndrey Smirnov     switch (offset) {
1669fd1e5c81SAndrey Smirnov     case ESDHC_DLL_CTRL:
1670fd1e5c81SAndrey Smirnov     case ESDHC_TUNE_CTRL_STATUS:
1671fd1e5c81SAndrey Smirnov     case ESDHC_UNDOCUMENTED_REG27:
1672fd1e5c81SAndrey Smirnov     case ESDHC_TUNING_CTRL:
1673fd1e5c81SAndrey Smirnov     case ESDHC_WTMK_LVL:
1674fd1e5c81SAndrey Smirnov     case ESDHC_VENDOR_SPEC:
1675fd1e5c81SAndrey Smirnov         break;
1676fd1e5c81SAndrey Smirnov 
1677fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1678fd1e5c81SAndrey Smirnov         /*
1679fd1e5c81SAndrey Smirnov          * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL)
1680fd1e5c81SAndrey Smirnov          *
1681fd1e5c81SAndrey Smirnov          *       7         6     5      4      3      2        1      0
1682fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1683fd1e5c81SAndrey Smirnov          * | Card      | Card   | Endian | DATA3     | Data     | Led     |
1684fd1e5c81SAndrey Smirnov          * | Detect    | Detect | Mode   | as Card   | Transfer | Control |
1685fd1e5c81SAndrey Smirnov          * | Signal    | Test   |        | Detection | Width    |         |
1686fd1e5c81SAndrey Smirnov          * | Selection | Level  |        | Pin       |          |         |
1687fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1688fd1e5c81SAndrey Smirnov          *
1689fd1e5c81SAndrey Smirnov          * and 0x29
1690fd1e5c81SAndrey Smirnov          *
1691fd1e5c81SAndrey Smirnov          *  15      10 9    8
1692fd1e5c81SAndrey Smirnov          * |----------+------|
1693fd1e5c81SAndrey Smirnov          * | Reserved | DMA  |
1694fd1e5c81SAndrey Smirnov          * |          | Sel. |
1695fd1e5c81SAndrey Smirnov          * |          |      |
1696fd1e5c81SAndrey Smirnov          * |----------+------|
1697fd1e5c81SAndrey Smirnov          *
1698fd1e5c81SAndrey Smirnov          * and here's what SDCHI spec expects those offsets to be:
1699fd1e5c81SAndrey Smirnov          *
1700fd1e5c81SAndrey Smirnov          * 0x28 (Host Control Register)
1701fd1e5c81SAndrey Smirnov          *
1702fd1e5c81SAndrey Smirnov          *     7        6         5       4  3      2         1        0
1703fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1704fd1e5c81SAndrey Smirnov          * | Card   | Card   | Extended | DMA  | High   | Data     | LED     |
1705fd1e5c81SAndrey Smirnov          * | Detect | Detect | Data     | Sel. | Speed  | Transfer | Control |
1706fd1e5c81SAndrey Smirnov          * | Signal | Test   | Transfer |      | Enable | Width    |         |
1707fd1e5c81SAndrey Smirnov          * | Sel.   | Level  | Width    |      |        |          |         |
1708fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1709fd1e5c81SAndrey Smirnov          *
1710fd1e5c81SAndrey Smirnov          * and 0x29 (Power Control Register)
1711fd1e5c81SAndrey Smirnov          *
1712fd1e5c81SAndrey Smirnov          * |----------------------------------|
1713fd1e5c81SAndrey Smirnov          * | Power Control Register           |
1714fd1e5c81SAndrey Smirnov          * |                                  |
1715fd1e5c81SAndrey Smirnov          * | Description omitted,             |
1716fd1e5c81SAndrey Smirnov          * | since it has no analog in ESDHCI |
1717fd1e5c81SAndrey Smirnov          * |                                  |
1718fd1e5c81SAndrey Smirnov          * |----------------------------------|
1719fd1e5c81SAndrey Smirnov          *
1720fd1e5c81SAndrey Smirnov          * Since offsets 0x2A and 0x2B should be compatible between
1721fd1e5c81SAndrey Smirnov          * both IP specs we only need to reconcile least 16-bit of the
1722fd1e5c81SAndrey Smirnov          * word we've been given.
1723fd1e5c81SAndrey Smirnov          */
1724fd1e5c81SAndrey Smirnov 
1725fd1e5c81SAndrey Smirnov         /*
1726fd1e5c81SAndrey Smirnov          * First, save bits 7 6 and 0 since they are identical
1727fd1e5c81SAndrey Smirnov          */
172806c5120bSPhilippe Mathieu-Daudé         hostctl1 = value & (SDHC_CTRL_LED |
1729fd1e5c81SAndrey Smirnov                             SDHC_CTRL_CDTEST_INS |
1730fd1e5c81SAndrey Smirnov                             SDHC_CTRL_CDTEST_EN);
1731fd1e5c81SAndrey Smirnov         /*
1732fd1e5c81SAndrey Smirnov          * Second, split "Data Transfer Width" from bits 2 and 1 in to
1733fd1e5c81SAndrey Smirnov          * bits 5 and 1
1734fd1e5c81SAndrey Smirnov          */
1735fd1e5c81SAndrey Smirnov         if (value & ESDHC_CTRL_8BITBUS) {
173606c5120bSPhilippe Mathieu-Daudé             hostctl1 |= SDHC_CTRL_8BITBUS;
1737fd1e5c81SAndrey Smirnov         }
1738fd1e5c81SAndrey Smirnov 
1739fd1e5c81SAndrey Smirnov         if (value & ESDHC_CTRL_4BITBUS) {
174006c5120bSPhilippe Mathieu-Daudé             hostctl1 |= ESDHC_CTRL_4BITBUS;
1741fd1e5c81SAndrey Smirnov         }
1742fd1e5c81SAndrey Smirnov 
1743fd1e5c81SAndrey Smirnov         /*
1744fd1e5c81SAndrey Smirnov          * Third, move DMA select from bits 9 and 8 to bits 4 and 3
1745fd1e5c81SAndrey Smirnov          */
174606c5120bSPhilippe Mathieu-Daudé         hostctl1 |= SDHC_DMA_TYPE(value >> (8 - 3));
1747fd1e5c81SAndrey Smirnov 
1748fd1e5c81SAndrey Smirnov         /*
1749fd1e5c81SAndrey Smirnov          * Now place the corrected value into low 16-bit of the value
1750fd1e5c81SAndrey Smirnov          * we are going to give standard SDHCI write function
1751fd1e5c81SAndrey Smirnov          *
1752fd1e5c81SAndrey Smirnov          * NOTE: This transformation should be the inverse of what can
1753fd1e5c81SAndrey Smirnov          * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux
1754fd1e5c81SAndrey Smirnov          * kernel
1755fd1e5c81SAndrey Smirnov          */
1756fd1e5c81SAndrey Smirnov         value &= ~UINT16_MAX;
175706c5120bSPhilippe Mathieu-Daudé         value |= hostctl1;
1758fd1e5c81SAndrey Smirnov         value |= (uint16_t)s->pwrcon << 8;
1759fd1e5c81SAndrey Smirnov 
1760fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, value, size);
1761fd1e5c81SAndrey Smirnov         break;
1762fd1e5c81SAndrey Smirnov 
1763fd1e5c81SAndrey Smirnov     case ESDHC_MIX_CTRL:
1764fd1e5c81SAndrey Smirnov         /*
1765fd1e5c81SAndrey Smirnov          * So, when SD/MMC stack in Linux tries to write to "Transfer
1766fd1e5c81SAndrey Smirnov          * Mode Register", ESDHC i.MX quirk code will translate it
1767fd1e5c81SAndrey Smirnov          * into a write to ESDHC_MIX_CTRL, so we do the opposite in
1768fd1e5c81SAndrey Smirnov          * order to get where we started
1769fd1e5c81SAndrey Smirnov          *
1770fd1e5c81SAndrey Smirnov          * Note that Auto CMD23 Enable bit is located in a wrong place
1771fd1e5c81SAndrey Smirnov          * on i.MX, but since it is not used by QEMU we do not care.
1772fd1e5c81SAndrey Smirnov          *
1773fd1e5c81SAndrey Smirnov          * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
1774fd1e5c81SAndrey Smirnov          * here becuase it will result in a call to
1775fd1e5c81SAndrey Smirnov          * sdhci_send_command(s) which we don't want.
1776fd1e5c81SAndrey Smirnov          *
1777fd1e5c81SAndrey Smirnov          */
1778fd1e5c81SAndrey Smirnov         s->trnmod = value & UINT16_MAX;
1779fd1e5c81SAndrey Smirnov         break;
1780fd1e5c81SAndrey Smirnov     case SDHC_TRNMOD:
1781fd1e5c81SAndrey Smirnov         /*
1782fd1e5c81SAndrey Smirnov          * Similar to above, but this time a write to "Command
1783fd1e5c81SAndrey Smirnov          * Register" will be translated into a 4-byte write to
1784fd1e5c81SAndrey Smirnov          * "Transfer Mode register" where lower 16-bit of value would
1785fd1e5c81SAndrey Smirnov          * be set to zero. So what we do is fill those bits with
1786fd1e5c81SAndrey Smirnov          * cached value from s->trnmod and let the SDHCI
1787fd1e5c81SAndrey Smirnov          * infrastructure handle the rest
1788fd1e5c81SAndrey Smirnov          */
1789fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val | s->trnmod, size);
1790fd1e5c81SAndrey Smirnov         break;
1791fd1e5c81SAndrey Smirnov     case SDHC_BLKSIZE:
1792fd1e5c81SAndrey Smirnov         /*
1793fd1e5c81SAndrey Smirnov          * ESDHCI does not implement "Host SDMA Buffer Boundary", and
1794fd1e5c81SAndrey Smirnov          * Linux driver will try to zero this field out which will
1795fd1e5c81SAndrey Smirnov          * break the rest of SDHCI emulation.
1796fd1e5c81SAndrey Smirnov          *
1797fd1e5c81SAndrey Smirnov          * Linux defaults to maximum possible setting (512K boundary)
1798fd1e5c81SAndrey Smirnov          * and it seems to be the only option that i.MX IP implements,
1799fd1e5c81SAndrey Smirnov          * so we artificially set it to that value.
1800fd1e5c81SAndrey Smirnov          */
1801fd1e5c81SAndrey Smirnov         val |= 0x7 << 12;
1802fd1e5c81SAndrey Smirnov         /* FALLTHROUGH */
1803fd1e5c81SAndrey Smirnov     default:
1804fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val, size);
1805fd1e5c81SAndrey Smirnov         break;
1806fd1e5c81SAndrey Smirnov     }
1807fd1e5c81SAndrey Smirnov }
1808fd1e5c81SAndrey Smirnov 
1809fd1e5c81SAndrey Smirnov 
1810fd1e5c81SAndrey Smirnov static const MemoryRegionOps usdhc_mmio_ops = {
1811fd1e5c81SAndrey Smirnov     .read = usdhc_read,
1812fd1e5c81SAndrey Smirnov     .write = usdhc_write,
1813fd1e5c81SAndrey Smirnov     .valid = {
1814fd1e5c81SAndrey Smirnov         .min_access_size = 1,
1815fd1e5c81SAndrey Smirnov         .max_access_size = 4,
1816fd1e5c81SAndrey Smirnov         .unaligned = false
1817fd1e5c81SAndrey Smirnov     },
1818fd1e5c81SAndrey Smirnov     .endianness = DEVICE_LITTLE_ENDIAN,
1819fd1e5c81SAndrey Smirnov };
1820fd1e5c81SAndrey Smirnov 
1821fd1e5c81SAndrey Smirnov static void imx_usdhc_init(Object *obj)
1822fd1e5c81SAndrey Smirnov {
1823fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(obj);
1824fd1e5c81SAndrey Smirnov 
1825fd1e5c81SAndrey Smirnov     s->io_ops = &usdhc_mmio_ops;
1826fd1e5c81SAndrey Smirnov     s->quirks = SDHCI_QUIRK_NO_BUSY_IRQ;
1827fd1e5c81SAndrey Smirnov }
1828fd1e5c81SAndrey Smirnov 
1829fd1e5c81SAndrey Smirnov static const TypeInfo imx_usdhc_info = {
1830fd1e5c81SAndrey Smirnov     .name = TYPE_IMX_USDHC,
1831fd1e5c81SAndrey Smirnov     .parent = TYPE_SYSBUS_SDHCI,
1832fd1e5c81SAndrey Smirnov     .instance_init = imx_usdhc_init,
1833fd1e5c81SAndrey Smirnov };
1834fd1e5c81SAndrey Smirnov 
183549ab747fSPaolo Bonzini static void sdhci_register_types(void)
183649ab747fSPaolo Bonzini {
1837224d10ffSKevin O'Connor     type_register_static(&sdhci_pci_info);
18387302dcd6SKevin O'Connor     type_register_static(&sdhci_sysbus_info);
183940bbc194SPeter Maydell     type_register_static(&sdhci_bus_info);
1840fd1e5c81SAndrey Smirnov     type_register_static(&imx_usdhc_info);
184149ab747fSPaolo Bonzini }
184249ab747fSPaolo Bonzini 
184349ab747fSPaolo Bonzini type_init(sdhci_register_types)
1844