xref: /openbmc/qemu/hw/sd/sdhci.c (revision 1e23b63f022ae79d7a5c535fe549127ad52d5ba6)
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) {
94*1e23b63fSPhilippe Mathieu-Daudé     case 4:
95*1e23b63fSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT_V4);
96*1e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("64-bit system bus (v4)", val);
97*1e23b63fSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT_V4, 0);
98*1e23b63fSPhilippe Mathieu-Daudé 
99*1e23b63fSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, UHS_II);
100*1e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("UHS-II", val);
101*1e23b63fSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, UHS_II, 0);
102*1e23b63fSPhilippe Mathieu-Daudé 
103*1e23b63fSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA3);
104*1e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA3", val);
105*1e23b63fSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA3, 0);
106*1e23b63fSPhilippe Mathieu-Daudé 
107*1e23b63fSPhilippe Mathieu-Daudé     /* fallthrough */
1084d67852dSPhilippe Mathieu-Daudé     case 3:
1094d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ASYNC_INT);
1104d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("async interrupt", val);
1114d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ASYNC_INT, 0);
1124d67852dSPhilippe Mathieu-Daudé 
1134d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SLOT_TYPE);
1144d67852dSPhilippe Mathieu-Daudé         if (val) {
1154d67852dSPhilippe Mathieu-Daudé             error_setg(errp, "slot-type not supported");
1164d67852dSPhilippe Mathieu-Daudé             return;
1174d67852dSPhilippe Mathieu-Daudé         }
1184d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("slot type", val);
1194d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SLOT_TYPE, 0);
1204d67852dSPhilippe Mathieu-Daudé 
1214d67852dSPhilippe Mathieu-Daudé         if (val != 2) {
1224d67852dSPhilippe Mathieu-Daudé             val = FIELD_EX64(s->capareg, SDHC_CAPAB, EMBEDDED_8BIT);
1234d67852dSPhilippe Mathieu-Daudé             trace_sdhci_capareg("8-bit bus", val);
1244d67852dSPhilippe Mathieu-Daudé         }
1254d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, EMBEDDED_8BIT, 0);
1264d67852dSPhilippe Mathieu-Daudé 
1274d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS_SPEED);
1284d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("bus speed mask", val);
1294d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS_SPEED, 0);
1304d67852dSPhilippe Mathieu-Daudé 
1314d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, DRIVER_STRENGTH);
1324d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("driver strength mask", val);
1334d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, DRIVER_STRENGTH, 0);
1344d67852dSPhilippe Mathieu-Daudé 
1354d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, TIMER_RETUNING);
1364d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("timer re-tuning", val);
1374d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TIMER_RETUNING, 0);
1384d67852dSPhilippe Mathieu-Daudé 
1394d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDR50_TUNING);
1404d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("use SDR50 tuning", val);
1414d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SDR50_TUNING, 0);
1424d67852dSPhilippe Mathieu-Daudé 
1434d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, RETUNING_MODE);
1444d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("re-tuning mode", val);
1454d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, RETUNING_MODE, 0);
1464d67852dSPhilippe Mathieu-Daudé 
1474d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, CLOCK_MULT);
1484d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("clock multiplier", val);
1494d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, CLOCK_MULT, 0);
1504d67852dSPhilippe Mathieu-Daudé 
1514d67852dSPhilippe Mathieu-Daudé     /* fallthrough */
1526ff37c3dSPhilippe Mathieu-Daudé     case 2: /* default version */
1530540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA2);
1540540fba9SPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA2", val);
1550540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA2, 0);
1560540fba9SPhilippe Mathieu-Daudé 
1570540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA1);
1580540fba9SPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA1", val);
1590540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA1, 0);
1600540fba9SPhilippe Mathieu-Daudé 
1610540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT);
162*1e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("64-bit system bus (v3)", val);
1630540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT, 0);
1646ff37c3dSPhilippe Mathieu-Daudé 
1656ff37c3dSPhilippe Mathieu-Daudé     /* fallthrough */
1666ff37c3dSPhilippe Mathieu-Daudé     case 1:
1676ff37c3dSPhilippe Mathieu-Daudé         y = FIELD_EX64(s->capareg, SDHC_CAPAB, TOUNIT);
1686ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TOUNIT, 0);
1696ff37c3dSPhilippe Mathieu-Daudé 
1706ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, TOCLKFREQ);
1716ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg(y ? "timeout (MHz)" : "Timeout (KHz)", val);
1726ff37c3dSPhilippe Mathieu-Daudé         if (sdhci_check_capab_freq_range(s, "timeout", val, errp)) {
1736ff37c3dSPhilippe Mathieu-Daudé             return;
1746ff37c3dSPhilippe Mathieu-Daudé         }
1756ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TOCLKFREQ, 0);
1766ff37c3dSPhilippe Mathieu-Daudé 
1776ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BASECLKFREQ);
1786ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg(y ? "base (MHz)" : "Base (KHz)", val);
1796ff37c3dSPhilippe Mathieu-Daudé         if (sdhci_check_capab_freq_range(s, "base", val, errp)) {
1806ff37c3dSPhilippe Mathieu-Daudé             return;
1816ff37c3dSPhilippe Mathieu-Daudé         }
1826ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BASECLKFREQ, 0);
1836ff37c3dSPhilippe Mathieu-Daudé 
1846ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH);
1856ff37c3dSPhilippe Mathieu-Daudé         if (val >= 3) {
1866ff37c3dSPhilippe Mathieu-Daudé             error_setg(errp, "block size can be 512, 1024 or 2048 only");
1876ff37c3dSPhilippe Mathieu-Daudé             return;
1886ff37c3dSPhilippe Mathieu-Daudé         }
1896ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("max block length", sdhci_get_fifolen(s));
1906ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, MAXBLOCKLENGTH, 0);
1916ff37c3dSPhilippe Mathieu-Daudé 
1926ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, HIGHSPEED);
1936ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("high speed", val);
1946ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, HIGHSPEED, 0);
1956ff37c3dSPhilippe Mathieu-Daudé 
1966ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDMA);
1976ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("SDMA", val);
1986ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SDMA, 0);
1996ff37c3dSPhilippe Mathieu-Daudé 
2006ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SUSPRESUME);
2016ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("suspend/resume", val);
2026ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SUSPRESUME, 0);
2036ff37c3dSPhilippe Mathieu-Daudé 
2046ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V33);
2056ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("3.3v", val);
2066ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V33, 0);
2076ff37c3dSPhilippe Mathieu-Daudé 
2086ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V30);
2096ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("3.0v", val);
2106ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V30, 0);
2116ff37c3dSPhilippe Mathieu-Daudé 
2126ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V18);
2136ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("1.8v", val);
2146ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V18, 0);
2156ff37c3dSPhilippe Mathieu-Daudé         break;
2166ff37c3dSPhilippe Mathieu-Daudé 
2176ff37c3dSPhilippe Mathieu-Daudé     default:
2186ff37c3dSPhilippe Mathieu-Daudé         error_setg(errp, "Unsupported spec version: %u", s->sd_spec_version);
2196ff37c3dSPhilippe Mathieu-Daudé     }
2206ff37c3dSPhilippe Mathieu-Daudé     if (msk) {
2216ff37c3dSPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP,
2226ff37c3dSPhilippe Mathieu-Daudé                       "SDHCI: unknown CAPAB mask: 0x%016" PRIx64 "\n", msk);
2236ff37c3dSPhilippe Mathieu-Daudé     }
2246ff37c3dSPhilippe Mathieu-Daudé }
2256ff37c3dSPhilippe Mathieu-Daudé 
22649ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s)
22749ab747fSPaolo Bonzini {
22849ab747fSPaolo Bonzini     return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) ||
22949ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) ||
23049ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV));
23149ab747fSPaolo Bonzini }
23249ab747fSPaolo Bonzini 
23349ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s)
23449ab747fSPaolo Bonzini {
23549ab747fSPaolo Bonzini     qemu_set_irq(s->irq, sdhci_slotint(s));
23649ab747fSPaolo Bonzini }
23749ab747fSPaolo Bonzini 
23849ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque)
23949ab747fSPaolo Bonzini {
24049ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
24149ab747fSPaolo Bonzini 
24249ab747fSPaolo Bonzini     if (s->norintsts & SDHC_NIS_REMOVE) {
243bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
244bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
24549ab747fSPaolo Bonzini     } else {
24649ab747fSPaolo Bonzini         s->prnsts = 0x1ff0000;
24749ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_INSERT) {
24849ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_INSERT;
24949ab747fSPaolo Bonzini         }
25049ab747fSPaolo Bonzini         sdhci_update_irq(s);
25149ab747fSPaolo Bonzini     }
25249ab747fSPaolo Bonzini }
25349ab747fSPaolo Bonzini 
25440bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level)
25549ab747fSPaolo Bonzini {
25640bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
25749ab747fSPaolo Bonzini 
2588be487d8SPhilippe Mathieu-Daudé     trace_sdhci_set_inserted(level ? "insert" : "eject");
25949ab747fSPaolo Bonzini     if ((s->norintsts & SDHC_NIS_REMOVE) && level) {
26049ab747fSPaolo Bonzini         /* Give target some time to notice card ejection */
261bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
262bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
26349ab747fSPaolo Bonzini     } else {
26449ab747fSPaolo Bonzini         if (level) {
26549ab747fSPaolo Bonzini             s->prnsts = 0x1ff0000;
26649ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_INSERT) {
26749ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_INSERT;
26849ab747fSPaolo Bonzini             }
26949ab747fSPaolo Bonzini         } else {
27049ab747fSPaolo Bonzini             s->prnsts = 0x1fa0000;
27149ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
27249ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_SDCLK_EN;
27349ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_REMOVE) {
27449ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_REMOVE;
27549ab747fSPaolo Bonzini             }
27649ab747fSPaolo Bonzini         }
27749ab747fSPaolo Bonzini         sdhci_update_irq(s);
27849ab747fSPaolo Bonzini     }
27949ab747fSPaolo Bonzini }
28049ab747fSPaolo Bonzini 
28140bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level)
28249ab747fSPaolo Bonzini {
28340bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
28449ab747fSPaolo Bonzini 
28549ab747fSPaolo Bonzini     if (level) {
28649ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_WRITE_PROTECT;
28749ab747fSPaolo Bonzini     } else {
28849ab747fSPaolo Bonzini         /* Write enabled */
28949ab747fSPaolo Bonzini         s->prnsts |= SDHC_WRITE_PROTECT;
29049ab747fSPaolo Bonzini     }
29149ab747fSPaolo Bonzini }
29249ab747fSPaolo Bonzini 
29349ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s)
29449ab747fSPaolo Bonzini {
29540bbc194SPeter Maydell     DeviceState *dev = DEVICE(s);
29640bbc194SPeter Maydell 
297bc72ad67SAlex Bligh     timer_del(s->insert_timer);
298bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
299aceb5b06SPhilippe Mathieu-Daudé 
300aceb5b06SPhilippe Mathieu-Daudé     /* Set all registers to 0. Capabilities/Version registers are not cleared
30149ab747fSPaolo Bonzini      * and assumed to always preserve their value, given to them during
30249ab747fSPaolo Bonzini      * initialization */
30349ab747fSPaolo Bonzini     memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad);
30449ab747fSPaolo Bonzini 
30540bbc194SPeter Maydell     /* Reset other state based on current card insertion/readonly status */
30640bbc194SPeter Maydell     sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus));
30740bbc194SPeter Maydell     sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus));
30840bbc194SPeter Maydell 
30949ab747fSPaolo Bonzini     s->data_count = 0;
31049ab747fSPaolo Bonzini     s->stopped_state = sdhc_not_stopped;
3110a7ac9f9SAndrew Baumann     s->pending_insert_state = false;
31249ab747fSPaolo Bonzini }
31349ab747fSPaolo Bonzini 
3148b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev)
3158b41c305SPeter Maydell {
3168b41c305SPeter Maydell     /* QOM (ie power-on) reset. This is identical to reset
3178b41c305SPeter Maydell      * commanded via device register apart from handling of the
3188b41c305SPeter Maydell      * 'pending insert on powerup' quirk.
3198b41c305SPeter Maydell      */
3208b41c305SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
3218b41c305SPeter Maydell 
3228b41c305SPeter Maydell     sdhci_reset(s);
3238b41c305SPeter Maydell 
3248b41c305SPeter Maydell     if (s->pending_insert_quirk) {
3258b41c305SPeter Maydell         s->pending_insert_state = true;
3268b41c305SPeter Maydell     }
3278b41c305SPeter Maydell }
3288b41c305SPeter Maydell 
329d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque);
33049ab747fSPaolo Bonzini 
33149ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s)
33249ab747fSPaolo Bonzini {
33349ab747fSPaolo Bonzini     SDRequest request;
33449ab747fSPaolo Bonzini     uint8_t response[16];
33549ab747fSPaolo Bonzini     int rlen;
33649ab747fSPaolo Bonzini 
33749ab747fSPaolo Bonzini     s->errintsts = 0;
33849ab747fSPaolo Bonzini     s->acmd12errsts = 0;
33949ab747fSPaolo Bonzini     request.cmd = s->cmdreg >> 8;
34049ab747fSPaolo Bonzini     request.arg = s->argument;
3418be487d8SPhilippe Mathieu-Daudé 
3428be487d8SPhilippe Mathieu-Daudé     trace_sdhci_send_command(request.cmd, request.arg);
34340bbc194SPeter Maydell     rlen = sdbus_do_command(&s->sdbus, &request, response);
34449ab747fSPaolo Bonzini 
34549ab747fSPaolo Bonzini     if (s->cmdreg & SDHC_CMD_RESPONSE) {
34649ab747fSPaolo Bonzini         if (rlen == 4) {
34749ab747fSPaolo Bonzini             s->rspreg[0] = (response[0] << 24) | (response[1] << 16) |
34849ab747fSPaolo Bonzini                            (response[2] << 8)  |  response[3];
34949ab747fSPaolo Bonzini             s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0;
3508be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response4(s->rspreg[0]);
35149ab747fSPaolo Bonzini         } else if (rlen == 16) {
35249ab747fSPaolo Bonzini             s->rspreg[0] = (response[11] << 24) | (response[12] << 16) |
35349ab747fSPaolo Bonzini                            (response[13] << 8) |  response[14];
35449ab747fSPaolo Bonzini             s->rspreg[1] = (response[7] << 24) | (response[8] << 16) |
35549ab747fSPaolo Bonzini                            (response[9] << 8)  |  response[10];
35649ab747fSPaolo Bonzini             s->rspreg[2] = (response[3] << 24) | (response[4] << 16) |
35749ab747fSPaolo Bonzini                            (response[5] << 8)  |  response[6];
35849ab747fSPaolo Bonzini             s->rspreg[3] = (response[0] << 16) | (response[1] << 8) |
35949ab747fSPaolo Bonzini                             response[2];
3608be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response16(s->rspreg[3], s->rspreg[2],
3618be487d8SPhilippe Mathieu-Daudé                                    s->rspreg[1], s->rspreg[0]);
36249ab747fSPaolo Bonzini         } else {
3638be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("timeout waiting for command response");
36449ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) {
36549ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_CMDTIMEOUT;
36649ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
36749ab747fSPaolo Bonzini             }
36849ab747fSPaolo Bonzini         }
36949ab747fSPaolo Bonzini 
370fd1e5c81SAndrey Smirnov         if (!(s->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
371fd1e5c81SAndrey Smirnov             (s->norintstsen & SDHC_NISEN_TRSCMP) &&
37249ab747fSPaolo Bonzini             (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) {
37349ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_TRSCMP;
37449ab747fSPaolo Bonzini         }
37549ab747fSPaolo Bonzini     }
37649ab747fSPaolo Bonzini 
37749ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_CMDCMP) {
37849ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_CMDCMP;
37949ab747fSPaolo Bonzini     }
38049ab747fSPaolo Bonzini 
38149ab747fSPaolo Bonzini     sdhci_update_irq(s);
38249ab747fSPaolo Bonzini 
38349ab747fSPaolo Bonzini     if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) {
384656f416cSPeter Crosthwaite         s->data_count = 0;
385d368ba43SKevin O'Connor         sdhci_data_transfer(s);
38649ab747fSPaolo Bonzini     }
38749ab747fSPaolo Bonzini }
38849ab747fSPaolo Bonzini 
38949ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s)
39049ab747fSPaolo Bonzini {
39149ab747fSPaolo Bonzini     /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
39249ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) {
39349ab747fSPaolo Bonzini         SDRequest request;
39449ab747fSPaolo Bonzini         uint8_t response[16];
39549ab747fSPaolo Bonzini 
39649ab747fSPaolo Bonzini         request.cmd = 0x0C;
39749ab747fSPaolo Bonzini         request.arg = 0;
3988be487d8SPhilippe Mathieu-Daudé         trace_sdhci_end_transfer(request.cmd, request.arg);
39940bbc194SPeter Maydell         sdbus_do_command(&s->sdbus, &request, response);
40049ab747fSPaolo Bonzini         /* Auto CMD12 response goes to the upper Response register */
40149ab747fSPaolo Bonzini         s->rspreg[3] = (response[0] << 24) | (response[1] << 16) |
40249ab747fSPaolo Bonzini                 (response[2] << 8) | response[3];
40349ab747fSPaolo Bonzini     }
40449ab747fSPaolo Bonzini 
40549ab747fSPaolo Bonzini     s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE |
40649ab747fSPaolo Bonzini             SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT |
40749ab747fSPaolo Bonzini             SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE);
40849ab747fSPaolo Bonzini 
40949ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_TRSCMP) {
41049ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_TRSCMP;
41149ab747fSPaolo Bonzini     }
41249ab747fSPaolo Bonzini 
41349ab747fSPaolo Bonzini     sdhci_update_irq(s);
41449ab747fSPaolo Bonzini }
41549ab747fSPaolo Bonzini 
41649ab747fSPaolo Bonzini /*
41749ab747fSPaolo Bonzini  * Programmed i/o data transfer
41849ab747fSPaolo Bonzini  */
419bf8ec38eSPhilippe Mathieu-Daudé #define BLOCK_SIZE_MASK (4 * K_BYTE - 1)
42049ab747fSPaolo Bonzini 
42149ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
42249ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s)
42349ab747fSPaolo Bonzini {
42449ab747fSPaolo Bonzini     int index = 0;
425ea55a221SPhilippe Mathieu-Daudé     uint8_t data;
426ea55a221SPhilippe Mathieu-Daudé     const uint16_t blk_size = s->blksize & BLOCK_SIZE_MASK;
42749ab747fSPaolo Bonzini 
42849ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) &&
42949ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) {
43049ab747fSPaolo Bonzini         return;
43149ab747fSPaolo Bonzini     }
43249ab747fSPaolo Bonzini 
433ea55a221SPhilippe Mathieu-Daudé     for (index = 0; index < blk_size; index++) {
434ea55a221SPhilippe Mathieu-Daudé         data = sdbus_read_data(&s->sdbus);
435ea55a221SPhilippe Mathieu-Daudé         if (!FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) {
436ea55a221SPhilippe Mathieu-Daudé             /* Device is not in tunning */
437ea55a221SPhilippe Mathieu-Daudé             s->fifo_buffer[index] = data;
438ea55a221SPhilippe Mathieu-Daudé         }
439ea55a221SPhilippe Mathieu-Daudé     }
440ea55a221SPhilippe Mathieu-Daudé 
441ea55a221SPhilippe Mathieu-Daudé     if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) {
442ea55a221SPhilippe Mathieu-Daudé         /* Device is in tunning */
443ea55a221SPhilippe Mathieu-Daudé         s->hostctl2 &= ~R_SDHC_HOSTCTL2_EXECUTE_TUNING_MASK;
444ea55a221SPhilippe Mathieu-Daudé         s->hostctl2 |= R_SDHC_HOSTCTL2_SAMPLING_CLKSEL_MASK;
445ea55a221SPhilippe Mathieu-Daudé         s->prnsts &= ~(SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ |
446ea55a221SPhilippe Mathieu-Daudé                        SDHC_DATA_INHIBIT);
447ea55a221SPhilippe Mathieu-Daudé         goto read_done;
44849ab747fSPaolo Bonzini     }
44949ab747fSPaolo Bonzini 
45049ab747fSPaolo Bonzini     /* New data now available for READ through Buffer Port Register */
45149ab747fSPaolo Bonzini     s->prnsts |= SDHC_DATA_AVAILABLE;
45249ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_RBUFRDY) {
45349ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_RBUFRDY;
45449ab747fSPaolo Bonzini     }
45549ab747fSPaolo Bonzini 
45649ab747fSPaolo Bonzini     /* Clear DAT line active status if that was the last block */
45749ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
45849ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) {
45949ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
46049ab747fSPaolo Bonzini     }
46149ab747fSPaolo Bonzini 
46249ab747fSPaolo Bonzini     /* If stop at block gap request was set and it's not the last block of
46349ab747fSPaolo Bonzini      * data - generate Block Event interrupt */
46449ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) &&
46549ab747fSPaolo Bonzini             s->blkcnt != 1)    {
46649ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
46749ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
46849ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
46949ab747fSPaolo Bonzini         }
47049ab747fSPaolo Bonzini     }
47149ab747fSPaolo Bonzini 
472ea55a221SPhilippe Mathieu-Daudé read_done:
47349ab747fSPaolo Bonzini     sdhci_update_irq(s);
47449ab747fSPaolo Bonzini }
47549ab747fSPaolo Bonzini 
47649ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
47749ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size)
47849ab747fSPaolo Bonzini {
47949ab747fSPaolo Bonzini     uint32_t value = 0;
48049ab747fSPaolo Bonzini     int i;
48149ab747fSPaolo Bonzini 
48249ab747fSPaolo Bonzini     /* first check that a valid data exists in host controller input buffer */
48349ab747fSPaolo Bonzini     if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) {
4848be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("read from empty buffer");
48549ab747fSPaolo Bonzini         return 0;
48649ab747fSPaolo Bonzini     }
48749ab747fSPaolo Bonzini 
48849ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
48949ab747fSPaolo Bonzini         value |= s->fifo_buffer[s->data_count] << i * 8;
49049ab747fSPaolo Bonzini         s->data_count++;
49149ab747fSPaolo Bonzini         /* check if we've read all valid data (blksize bytes) from buffer */
492bf8ec38eSPhilippe Mathieu-Daudé         if ((s->data_count) >= (s->blksize & BLOCK_SIZE_MASK)) {
4938be487d8SPhilippe Mathieu-Daudé             trace_sdhci_read_dataport(s->data_count);
49449ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */
49549ab747fSPaolo Bonzini             s->data_count = 0;  /* next buff read must start at position [0] */
49649ab747fSPaolo Bonzini 
49749ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
49849ab747fSPaolo Bonzini                 s->blkcnt--;
49949ab747fSPaolo Bonzini             }
50049ab747fSPaolo Bonzini 
50149ab747fSPaolo Bonzini             /* if that was the last block of data */
50249ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
50349ab747fSPaolo Bonzini                 ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) ||
50449ab747fSPaolo Bonzini                  /* stop at gap request */
50549ab747fSPaolo Bonzini                 (s->stopped_state == sdhc_gap_read &&
50649ab747fSPaolo Bonzini                  !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) {
507d368ba43SKevin O'Connor                 sdhci_end_transfer(s);
50849ab747fSPaolo Bonzini             } else { /* if there are more data, read next block from card */
509d368ba43SKevin O'Connor                 sdhci_read_block_from_card(s);
51049ab747fSPaolo Bonzini             }
51149ab747fSPaolo Bonzini             break;
51249ab747fSPaolo Bonzini         }
51349ab747fSPaolo Bonzini     }
51449ab747fSPaolo Bonzini 
51549ab747fSPaolo Bonzini     return value;
51649ab747fSPaolo Bonzini }
51749ab747fSPaolo Bonzini 
51849ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */
51949ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s)
52049ab747fSPaolo Bonzini {
52149ab747fSPaolo Bonzini     int index = 0;
52249ab747fSPaolo Bonzini 
52349ab747fSPaolo Bonzini     if (s->prnsts & SDHC_SPACE_AVAILABLE) {
52449ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
52549ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_WBUFRDY;
52649ab747fSPaolo Bonzini         }
52749ab747fSPaolo Bonzini         sdhci_update_irq(s);
52849ab747fSPaolo Bonzini         return;
52949ab747fSPaolo Bonzini     }
53049ab747fSPaolo Bonzini 
53149ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
53249ab747fSPaolo Bonzini         if (s->blkcnt == 0) {
53349ab747fSPaolo Bonzini             return;
53449ab747fSPaolo Bonzini         } else {
53549ab747fSPaolo Bonzini             s->blkcnt--;
53649ab747fSPaolo Bonzini         }
53749ab747fSPaolo Bonzini     }
53849ab747fSPaolo Bonzini 
539bf8ec38eSPhilippe Mathieu-Daudé     for (index = 0; index < (s->blksize & BLOCK_SIZE_MASK); index++) {
54040bbc194SPeter Maydell         sdbus_write_data(&s->sdbus, s->fifo_buffer[index]);
54149ab747fSPaolo Bonzini     }
54249ab747fSPaolo Bonzini 
54349ab747fSPaolo Bonzini     /* Next data can be written through BUFFER DATORT register */
54449ab747fSPaolo Bonzini     s->prnsts |= SDHC_SPACE_AVAILABLE;
54549ab747fSPaolo Bonzini 
54649ab747fSPaolo Bonzini     /* Finish transfer if that was the last block of data */
54749ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
54849ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) &&
54949ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) {
550d368ba43SKevin O'Connor         sdhci_end_transfer(s);
551dcdb4cd8SPeter Crosthwaite     } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
552dcdb4cd8SPeter Crosthwaite         s->norintsts |= SDHC_NIS_WBUFRDY;
55349ab747fSPaolo Bonzini     }
55449ab747fSPaolo Bonzini 
55549ab747fSPaolo Bonzini     /* Generate Block Gap Event if requested and if not the last block */
55649ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) &&
55749ab747fSPaolo Bonzini             s->blkcnt > 0) {
55849ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DOING_WRITE;
55949ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
56049ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
56149ab747fSPaolo Bonzini         }
562d368ba43SKevin O'Connor         sdhci_end_transfer(s);
56349ab747fSPaolo Bonzini     }
56449ab747fSPaolo Bonzini 
56549ab747fSPaolo Bonzini     sdhci_update_irq(s);
56649ab747fSPaolo Bonzini }
56749ab747fSPaolo Bonzini 
56849ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port
56949ab747fSPaolo Bonzini  * register */
57049ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size)
57149ab747fSPaolo Bonzini {
57249ab747fSPaolo Bonzini     unsigned i;
57349ab747fSPaolo Bonzini 
57449ab747fSPaolo Bonzini     /* Check that there is free space left in a buffer */
57549ab747fSPaolo Bonzini     if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) {
5768be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("Can't write to data buffer: buffer full");
57749ab747fSPaolo Bonzini         return;
57849ab747fSPaolo Bonzini     }
57949ab747fSPaolo Bonzini 
58049ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
58149ab747fSPaolo Bonzini         s->fifo_buffer[s->data_count] = value & 0xFF;
58249ab747fSPaolo Bonzini         s->data_count++;
58349ab747fSPaolo Bonzini         value >>= 8;
584bf8ec38eSPhilippe Mathieu-Daudé         if (s->data_count >= (s->blksize & BLOCK_SIZE_MASK)) {
5858be487d8SPhilippe Mathieu-Daudé             trace_sdhci_write_dataport(s->data_count);
58649ab747fSPaolo Bonzini             s->data_count = 0;
58749ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_SPACE_AVAILABLE;
58849ab747fSPaolo Bonzini             if (s->prnsts & SDHC_DOING_WRITE) {
589d368ba43SKevin O'Connor                 sdhci_write_block_to_card(s);
59049ab747fSPaolo Bonzini             }
59149ab747fSPaolo Bonzini         }
59249ab747fSPaolo Bonzini     }
59349ab747fSPaolo Bonzini }
59449ab747fSPaolo Bonzini 
59549ab747fSPaolo Bonzini /*
59649ab747fSPaolo Bonzini  * Single DMA data transfer
59749ab747fSPaolo Bonzini  */
59849ab747fSPaolo Bonzini 
59949ab747fSPaolo Bonzini /* Multi block SDMA transfer */
60049ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
60149ab747fSPaolo Bonzini {
60249ab747fSPaolo Bonzini     bool page_aligned = false;
60349ab747fSPaolo Bonzini     unsigned int n, begin;
604bf8ec38eSPhilippe Mathieu-Daudé     const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK;
605bf8ec38eSPhilippe Mathieu-Daudé     uint32_t boundary_chk = 1 << (((s->blksize & ~BLOCK_SIZE_MASK) >> 12) + 12);
60649ab747fSPaolo Bonzini     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
60749ab747fSPaolo Bonzini 
6086e86d903SPrasad J Pandit     if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) {
6096e86d903SPrasad J Pandit         qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n");
6106e86d903SPrasad J Pandit         return;
6116e86d903SPrasad J Pandit     }
6126e86d903SPrasad J Pandit 
61349ab747fSPaolo Bonzini     /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
61449ab747fSPaolo Bonzini      * possible stop at page boundary if initial address is not page aligned,
61549ab747fSPaolo Bonzini      * allow them to work properly */
61649ab747fSPaolo Bonzini     if ((s->sdmasysad % boundary_chk) == 0) {
61749ab747fSPaolo Bonzini         page_aligned = true;
61849ab747fSPaolo Bonzini     }
61949ab747fSPaolo Bonzini 
62049ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
62149ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
62249ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
62349ab747fSPaolo Bonzini         while (s->blkcnt) {
62449ab747fSPaolo Bonzini             if (s->data_count == 0) {
62549ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
62640bbc194SPeter Maydell                     s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
62749ab747fSPaolo Bonzini                 }
62849ab747fSPaolo Bonzini             }
62949ab747fSPaolo Bonzini             begin = s->data_count;
63049ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
63149ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
63249ab747fSPaolo Bonzini                 boundary_count = 0;
63349ab747fSPaolo Bonzini              } else {
63449ab747fSPaolo Bonzini                 s->data_count = block_size;
63549ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
63649ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
63749ab747fSPaolo Bonzini                     s->blkcnt--;
63849ab747fSPaolo Bonzini                 }
63949ab747fSPaolo Bonzini             }
640dd55c485SPhilippe Mathieu-Daudé             dma_memory_write(s->dma_as, s->sdmasysad,
64149ab747fSPaolo Bonzini                              &s->fifo_buffer[begin], s->data_count - begin);
64249ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
64349ab747fSPaolo Bonzini             if (s->data_count == block_size) {
64449ab747fSPaolo Bonzini                 s->data_count = 0;
64549ab747fSPaolo Bonzini             }
64649ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
64749ab747fSPaolo Bonzini                 break;
64849ab747fSPaolo Bonzini             }
64949ab747fSPaolo Bonzini         }
65049ab747fSPaolo Bonzini     } else {
65149ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT |
65249ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
65349ab747fSPaolo Bonzini         while (s->blkcnt) {
65449ab747fSPaolo Bonzini             begin = s->data_count;
65549ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
65649ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
65749ab747fSPaolo Bonzini                 boundary_count = 0;
65849ab747fSPaolo Bonzini              } else {
65949ab747fSPaolo Bonzini                 s->data_count = block_size;
66049ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
66149ab747fSPaolo Bonzini             }
662dd55c485SPhilippe Mathieu-Daudé             dma_memory_read(s->dma_as, s->sdmasysad,
66342922105SPrasad J Pandit                             &s->fifo_buffer[begin], s->data_count - begin);
66449ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
66549ab747fSPaolo Bonzini             if (s->data_count == block_size) {
66649ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
66740bbc194SPeter Maydell                     sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
66849ab747fSPaolo Bonzini                 }
66949ab747fSPaolo Bonzini                 s->data_count = 0;
67049ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
67149ab747fSPaolo Bonzini                     s->blkcnt--;
67249ab747fSPaolo Bonzini                 }
67349ab747fSPaolo Bonzini             }
67449ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
67549ab747fSPaolo Bonzini                 break;
67649ab747fSPaolo Bonzini             }
67749ab747fSPaolo Bonzini         }
67849ab747fSPaolo Bonzini     }
67949ab747fSPaolo Bonzini 
68049ab747fSPaolo Bonzini     if (s->blkcnt == 0) {
681d368ba43SKevin O'Connor         sdhci_end_transfer(s);
68249ab747fSPaolo Bonzini     } else {
68349ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_DMA) {
68449ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_DMA;
68549ab747fSPaolo Bonzini         }
68649ab747fSPaolo Bonzini         sdhci_update_irq(s);
68749ab747fSPaolo Bonzini     }
68849ab747fSPaolo Bonzini }
68949ab747fSPaolo Bonzini 
69049ab747fSPaolo Bonzini /* single block SDMA transfer */
69149ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s)
69249ab747fSPaolo Bonzini {
69349ab747fSPaolo Bonzini     int n;
694bf8ec38eSPhilippe Mathieu-Daudé     uint32_t datacnt = s->blksize & BLOCK_SIZE_MASK;
69549ab747fSPaolo Bonzini 
69649ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
69749ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
69840bbc194SPeter Maydell             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
69949ab747fSPaolo Bonzini         }
700dd55c485SPhilippe Mathieu-Daudé         dma_memory_write(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt);
70149ab747fSPaolo Bonzini     } else {
702dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt);
70349ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
70440bbc194SPeter Maydell             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
70549ab747fSPaolo Bonzini         }
70649ab747fSPaolo Bonzini     }
70749ab747fSPaolo Bonzini     s->blkcnt--;
70849ab747fSPaolo Bonzini 
709d368ba43SKevin O'Connor     sdhci_end_transfer(s);
71049ab747fSPaolo Bonzini }
71149ab747fSPaolo Bonzini 
71249ab747fSPaolo Bonzini typedef struct ADMADescr {
71349ab747fSPaolo Bonzini     hwaddr addr;
71449ab747fSPaolo Bonzini     uint16_t length;
71549ab747fSPaolo Bonzini     uint8_t attr;
71649ab747fSPaolo Bonzini     uint8_t incr;
71749ab747fSPaolo Bonzini } ADMADescr;
71849ab747fSPaolo Bonzini 
71949ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
72049ab747fSPaolo Bonzini {
72149ab747fSPaolo Bonzini     uint32_t adma1 = 0;
72249ab747fSPaolo Bonzini     uint64_t adma2 = 0;
72349ab747fSPaolo Bonzini     hwaddr entry_addr = (hwaddr)s->admasysaddr;
72406c5120bSPhilippe Mathieu-Daudé     switch (SDHC_DMA_TYPE(s->hostctl1)) {
72549ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_32:
726dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma2,
72749ab747fSPaolo Bonzini                         sizeof(adma2));
72849ab747fSPaolo Bonzini         adma2 = le64_to_cpu(adma2);
72949ab747fSPaolo Bonzini         /* The spec does not specify endianness of descriptor table.
73049ab747fSPaolo Bonzini          * We currently assume that it is LE.
73149ab747fSPaolo Bonzini          */
73249ab747fSPaolo Bonzini         dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull;
73349ab747fSPaolo Bonzini         dscr->length = (uint16_t)extract64(adma2, 16, 16);
73449ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract64(adma2, 0, 7);
73549ab747fSPaolo Bonzini         dscr->incr = 8;
73649ab747fSPaolo Bonzini         break;
73749ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA1_32:
738dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma1,
73949ab747fSPaolo Bonzini                         sizeof(adma1));
74049ab747fSPaolo Bonzini         adma1 = le32_to_cpu(adma1);
74149ab747fSPaolo Bonzini         dscr->addr = (hwaddr)(adma1 & 0xFFFFF000);
74249ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract32(adma1, 0, 7);
74349ab747fSPaolo Bonzini         dscr->incr = 4;
74449ab747fSPaolo Bonzini         if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
74549ab747fSPaolo Bonzini             dscr->length = (uint16_t)extract32(adma1, 12, 16);
74649ab747fSPaolo Bonzini         } else {
74749ab747fSPaolo Bonzini             dscr->length = 4096;
74849ab747fSPaolo Bonzini         }
74949ab747fSPaolo Bonzini         break;
75049ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_64:
751dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr,
75249ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->attr), 1);
753dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 2,
75449ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->length), 2);
75549ab747fSPaolo Bonzini         dscr->length = le16_to_cpu(dscr->length);
756dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 4,
75749ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->addr), 8);
75804654b5aSSai Pavan Boddu         dscr->addr = le64_to_cpu(dscr->addr);
75904654b5aSSai Pavan Boddu         dscr->attr &= (uint8_t) ~0xC0;
76049ab747fSPaolo Bonzini         dscr->incr = 12;
76149ab747fSPaolo Bonzini         break;
76249ab747fSPaolo Bonzini     }
76349ab747fSPaolo Bonzini }
76449ab747fSPaolo Bonzini 
76549ab747fSPaolo Bonzini /* Advanced DMA data transfer */
76649ab747fSPaolo Bonzini 
76749ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s)
76849ab747fSPaolo Bonzini {
76949ab747fSPaolo Bonzini     unsigned int n, begin, length;
770bf8ec38eSPhilippe Mathieu-Daudé     const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK;
7718be487d8SPhilippe Mathieu-Daudé     ADMADescr dscr = {};
77249ab747fSPaolo Bonzini     int i;
77349ab747fSPaolo Bonzini 
77449ab747fSPaolo Bonzini     for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
77549ab747fSPaolo Bonzini         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
77649ab747fSPaolo Bonzini 
77749ab747fSPaolo Bonzini         get_adma_description(s, &dscr);
7788be487d8SPhilippe Mathieu-Daudé         trace_sdhci_adma_loop(dscr.addr, dscr.length, dscr.attr);
77949ab747fSPaolo Bonzini 
78049ab747fSPaolo Bonzini         if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
78149ab747fSPaolo Bonzini             /* Indicate that error occurred in ST_FDS state */
78249ab747fSPaolo Bonzini             s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
78349ab747fSPaolo Bonzini             s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
78449ab747fSPaolo Bonzini 
78549ab747fSPaolo Bonzini             /* Generate ADMA error interrupt */
78649ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_ADMAERR) {
78749ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_ADMAERR;
78849ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
78949ab747fSPaolo Bonzini             }
79049ab747fSPaolo Bonzini 
79149ab747fSPaolo Bonzini             sdhci_update_irq(s);
79249ab747fSPaolo Bonzini             return;
79349ab747fSPaolo Bonzini         }
79449ab747fSPaolo Bonzini 
79549ab747fSPaolo Bonzini         length = dscr.length ? dscr.length : 65536;
79649ab747fSPaolo Bonzini 
79749ab747fSPaolo Bonzini         switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) {
79849ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_TRAN:  /* data transfer */
79949ab747fSPaolo Bonzini 
80049ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_READ) {
80149ab747fSPaolo Bonzini                 while (length) {
80249ab747fSPaolo Bonzini                     if (s->data_count == 0) {
80349ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
80440bbc194SPeter Maydell                             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
80549ab747fSPaolo Bonzini                         }
80649ab747fSPaolo Bonzini                     }
80749ab747fSPaolo Bonzini                     begin = s->data_count;
80849ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
80949ab747fSPaolo Bonzini                         s->data_count = length + begin;
81049ab747fSPaolo Bonzini                         length = 0;
81149ab747fSPaolo Bonzini                      } else {
81249ab747fSPaolo Bonzini                         s->data_count = block_size;
81349ab747fSPaolo Bonzini                         length -= block_size - begin;
81449ab747fSPaolo Bonzini                     }
815dd55c485SPhilippe Mathieu-Daudé                     dma_memory_write(s->dma_as, dscr.addr,
81649ab747fSPaolo Bonzini                                      &s->fifo_buffer[begin],
81749ab747fSPaolo Bonzini                                      s->data_count - begin);
81849ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
81949ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
82049ab747fSPaolo Bonzini                         s->data_count = 0;
82149ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
82249ab747fSPaolo Bonzini                             s->blkcnt--;
82349ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
82449ab747fSPaolo Bonzini                                 break;
82549ab747fSPaolo Bonzini                             }
82649ab747fSPaolo Bonzini                         }
82749ab747fSPaolo Bonzini                     }
82849ab747fSPaolo Bonzini                 }
82949ab747fSPaolo Bonzini             } else {
83049ab747fSPaolo Bonzini                 while (length) {
83149ab747fSPaolo Bonzini                     begin = s->data_count;
83249ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
83349ab747fSPaolo Bonzini                         s->data_count = length + begin;
83449ab747fSPaolo Bonzini                         length = 0;
83549ab747fSPaolo Bonzini                      } else {
83649ab747fSPaolo Bonzini                         s->data_count = block_size;
83749ab747fSPaolo Bonzini                         length -= block_size - begin;
83849ab747fSPaolo Bonzini                     }
839dd55c485SPhilippe Mathieu-Daudé                     dma_memory_read(s->dma_as, dscr.addr,
8409db11cefSPeter Crosthwaite                                     &s->fifo_buffer[begin],
8419db11cefSPeter Crosthwaite                                     s->data_count - begin);
84249ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
84349ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
84449ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
84540bbc194SPeter Maydell                             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
84649ab747fSPaolo Bonzini                         }
84749ab747fSPaolo Bonzini                         s->data_count = 0;
84849ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
84949ab747fSPaolo Bonzini                             s->blkcnt--;
85049ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
85149ab747fSPaolo Bonzini                                 break;
85249ab747fSPaolo Bonzini                             }
85349ab747fSPaolo Bonzini                         }
85449ab747fSPaolo Bonzini                     }
85549ab747fSPaolo Bonzini                 }
85649ab747fSPaolo Bonzini             }
85749ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
85849ab747fSPaolo Bonzini             break;
85949ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
86049ab747fSPaolo Bonzini             s->admasysaddr = dscr.addr;
8618be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("link", s->admasysaddr);
86249ab747fSPaolo Bonzini             break;
86349ab747fSPaolo Bonzini         default:
86449ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
86549ab747fSPaolo Bonzini             break;
86649ab747fSPaolo Bonzini         }
86749ab747fSPaolo Bonzini 
8681d32c26fSPeter Crosthwaite         if (dscr.attr & SDHC_ADMA_ATTR_INT) {
8698be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("interrupt", s->admasysaddr);
8701d32c26fSPeter Crosthwaite             if (s->norintstsen & SDHC_NISEN_DMA) {
8711d32c26fSPeter Crosthwaite                 s->norintsts |= SDHC_NIS_DMA;
8721d32c26fSPeter Crosthwaite             }
8731d32c26fSPeter Crosthwaite 
8741d32c26fSPeter Crosthwaite             sdhci_update_irq(s);
8751d32c26fSPeter Crosthwaite         }
8761d32c26fSPeter Crosthwaite 
87749ab747fSPaolo Bonzini         /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
87849ab747fSPaolo Bonzini         if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
87949ab747fSPaolo Bonzini                     (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
8808be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma_transfer_completed();
88149ab747fSPaolo Bonzini             if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
88249ab747fSPaolo Bonzini                 (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
88349ab747fSPaolo Bonzini                 s->blkcnt != 0)) {
8848be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("SD/MMC host ADMA length mismatch");
88549ab747fSPaolo Bonzini                 s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
88649ab747fSPaolo Bonzini                         SDHC_ADMAERR_STATE_ST_TFR;
88749ab747fSPaolo Bonzini                 if (s->errintstsen & SDHC_EISEN_ADMAERR) {
8888be487d8SPhilippe Mathieu-Daudé                     trace_sdhci_error("Set ADMA error flag");
88949ab747fSPaolo Bonzini                     s->errintsts |= SDHC_EIS_ADMAERR;
89049ab747fSPaolo Bonzini                     s->norintsts |= SDHC_NIS_ERR;
89149ab747fSPaolo Bonzini                 }
89249ab747fSPaolo Bonzini 
89349ab747fSPaolo Bonzini                 sdhci_update_irq(s);
89449ab747fSPaolo Bonzini             }
895d368ba43SKevin O'Connor             sdhci_end_transfer(s);
89649ab747fSPaolo Bonzini             return;
89749ab747fSPaolo Bonzini         }
89849ab747fSPaolo Bonzini 
89949ab747fSPaolo Bonzini     }
90049ab747fSPaolo Bonzini 
90149ab747fSPaolo Bonzini     /* we have unfinished business - reschedule to continue ADMA */
902bc72ad67SAlex Bligh     timer_mod(s->transfer_timer,
903bc72ad67SAlex Bligh                    qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY);
90449ab747fSPaolo Bonzini }
90549ab747fSPaolo Bonzini 
90649ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */
90749ab747fSPaolo Bonzini 
908d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque)
90949ab747fSPaolo Bonzini {
910d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
91149ab747fSPaolo Bonzini 
91249ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_DMA) {
91306c5120bSPhilippe Mathieu-Daudé         switch (SDHC_DMA_TYPE(s->hostctl1)) {
91449ab747fSPaolo Bonzini         case SDHC_CTRL_SDMA:
91549ab747fSPaolo Bonzini             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
916d368ba43SKevin O'Connor                 sdhci_sdma_transfer_single_block(s);
91749ab747fSPaolo Bonzini             } else {
918d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
91949ab747fSPaolo Bonzini             }
92049ab747fSPaolo Bonzini 
92149ab747fSPaolo Bonzini             break;
92249ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA1_32:
9230540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA1_MASK)) {
9248be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA1 not supported");
92549ab747fSPaolo Bonzini                 break;
92649ab747fSPaolo Bonzini             }
92749ab747fSPaolo Bonzini 
928d368ba43SKevin O'Connor             sdhci_do_adma(s);
92949ab747fSPaolo Bonzini             break;
93049ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_32:
9310540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK)) {
9328be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA2 not supported");
93349ab747fSPaolo Bonzini                 break;
93449ab747fSPaolo Bonzini             }
93549ab747fSPaolo Bonzini 
936d368ba43SKevin O'Connor             sdhci_do_adma(s);
93749ab747fSPaolo Bonzini             break;
93849ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_64:
9390540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK) ||
9400540fba9SPhilippe Mathieu-Daudé                     !(s->capareg & R_SDHC_CAPAB_BUS64BIT_MASK)) {
9418be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("64 bit ADMA not supported");
94249ab747fSPaolo Bonzini                 break;
94349ab747fSPaolo Bonzini             }
94449ab747fSPaolo Bonzini 
945d368ba43SKevin O'Connor             sdhci_do_adma(s);
94649ab747fSPaolo Bonzini             break;
94749ab747fSPaolo Bonzini         default:
9488be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("Unsupported DMA type");
94949ab747fSPaolo Bonzini             break;
95049ab747fSPaolo Bonzini         }
95149ab747fSPaolo Bonzini     } else {
95240bbc194SPeter Maydell         if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) {
95349ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
95449ab747fSPaolo Bonzini                     SDHC_DAT_LINE_ACTIVE;
955d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
95649ab747fSPaolo Bonzini         } else {
95749ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
95849ab747fSPaolo Bonzini                     SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
959d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
96049ab747fSPaolo Bonzini         }
96149ab747fSPaolo Bonzini     }
96249ab747fSPaolo Bonzini }
96349ab747fSPaolo Bonzini 
96449ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s)
96549ab747fSPaolo Bonzini {
9666890a695SPeter Crosthwaite     if (!SDHC_CLOCK_IS_ON(s->clkcon) ||
96749ab747fSPaolo Bonzini         (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) &&
96849ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_DATA_PRESENT) ||
96949ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY &&
97049ab747fSPaolo Bonzini         !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) {
97149ab747fSPaolo Bonzini         return false;
97249ab747fSPaolo Bonzini     }
97349ab747fSPaolo Bonzini 
97449ab747fSPaolo Bonzini     return true;
97549ab747fSPaolo Bonzini }
97649ab747fSPaolo Bonzini 
97749ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and
97849ab747fSPaolo Bonzini  * continuous manner */
97949ab747fSPaolo Bonzini static inline bool
98049ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
98149ab747fSPaolo Bonzini {
98249ab747fSPaolo Bonzini     if ((s->data_count & 0x3) != byte_num) {
9838be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("Non-sequential access to Buffer Data Port register"
98449ab747fSPaolo Bonzini                           "is prohibited\n");
98549ab747fSPaolo Bonzini         return false;
98649ab747fSPaolo Bonzini     }
98749ab747fSPaolo Bonzini     return true;
98849ab747fSPaolo Bonzini }
98949ab747fSPaolo Bonzini 
990d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size)
99149ab747fSPaolo Bonzini {
992d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
99349ab747fSPaolo Bonzini     uint32_t ret = 0;
99449ab747fSPaolo Bonzini 
99549ab747fSPaolo Bonzini     switch (offset & ~0x3) {
99649ab747fSPaolo Bonzini     case SDHC_SYSAD:
99749ab747fSPaolo Bonzini         ret = s->sdmasysad;
99849ab747fSPaolo Bonzini         break;
99949ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
100049ab747fSPaolo Bonzini         ret = s->blksize | (s->blkcnt << 16);
100149ab747fSPaolo Bonzini         break;
100249ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
100349ab747fSPaolo Bonzini         ret = s->argument;
100449ab747fSPaolo Bonzini         break;
100549ab747fSPaolo Bonzini     case SDHC_TRNMOD:
100649ab747fSPaolo Bonzini         ret = s->trnmod | (s->cmdreg << 16);
100749ab747fSPaolo Bonzini         break;
100849ab747fSPaolo Bonzini     case SDHC_RSPREG0 ... SDHC_RSPREG3:
100949ab747fSPaolo Bonzini         ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2];
101049ab747fSPaolo Bonzini         break;
101149ab747fSPaolo Bonzini     case  SDHC_BDATA:
101249ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
1013d368ba43SKevin O'Connor             ret = sdhci_read_dataport(s, size);
10148be487d8SPhilippe Mathieu-Daudé             trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
101549ab747fSPaolo Bonzini             return ret;
101649ab747fSPaolo Bonzini         }
101749ab747fSPaolo Bonzini         break;
101849ab747fSPaolo Bonzini     case SDHC_PRNSTS:
101949ab747fSPaolo Bonzini         ret = s->prnsts;
1020da346922SPhilippe Mathieu-Daudé         ret = FIELD_DP32(ret, SDHC_PRNSTS, DAT_LVL,
1021da346922SPhilippe Mathieu-Daudé                          sdbus_get_dat_lines(&s->sdbus));
1022da346922SPhilippe Mathieu-Daudé         ret = FIELD_DP32(ret, SDHC_PRNSTS, CMD_LVL,
1023da346922SPhilippe Mathieu-Daudé                          sdbus_get_cmd_line(&s->sdbus));
102449ab747fSPaolo Bonzini         break;
102549ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
102606c5120bSPhilippe Mathieu-Daudé         ret = s->hostctl1 | (s->pwrcon << 8) | (s->blkgap << 16) |
102749ab747fSPaolo Bonzini               (s->wakcon << 24);
102849ab747fSPaolo Bonzini         break;
102949ab747fSPaolo Bonzini     case SDHC_CLKCON:
103049ab747fSPaolo Bonzini         ret = s->clkcon | (s->timeoutcon << 16);
103149ab747fSPaolo Bonzini         break;
103249ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
103349ab747fSPaolo Bonzini         ret = s->norintsts | (s->errintsts << 16);
103449ab747fSPaolo Bonzini         break;
103549ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
103649ab747fSPaolo Bonzini         ret = s->norintstsen | (s->errintstsen << 16);
103749ab747fSPaolo Bonzini         break;
103849ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
103949ab747fSPaolo Bonzini         ret = s->norintsigen | (s->errintsigen << 16);
104049ab747fSPaolo Bonzini         break;
104149ab747fSPaolo Bonzini     case SDHC_ACMD12ERRSTS:
1042ea55a221SPhilippe Mathieu-Daudé         ret = s->acmd12errsts | (s->hostctl2 << 16);
104349ab747fSPaolo Bonzini         break;
1044cd209421SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
10455efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->capareg;
10465efc9016SPhilippe Mathieu-Daudé         break;
10475efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
10485efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->capareg >> 32);
104949ab747fSPaolo Bonzini         break;
105049ab747fSPaolo Bonzini     case SDHC_MAXCURR:
10515efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->maxcurr;
10525efc9016SPhilippe Mathieu-Daudé         break;
10535efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
10545efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->maxcurr >> 32);
105549ab747fSPaolo Bonzini         break;
105649ab747fSPaolo Bonzini     case SDHC_ADMAERR:
105749ab747fSPaolo Bonzini         ret =  s->admaerr;
105849ab747fSPaolo Bonzini         break;
105949ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
106049ab747fSPaolo Bonzini         ret = (uint32_t)s->admasysaddr;
106149ab747fSPaolo Bonzini         break;
106249ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
106349ab747fSPaolo Bonzini         ret = (uint32_t)(s->admasysaddr >> 32);
106449ab747fSPaolo Bonzini         break;
106549ab747fSPaolo Bonzini     case SDHC_SLOT_INT_STATUS:
1066aceb5b06SPhilippe Mathieu-Daudé         ret = (s->version << 16) | sdhci_slotint(s);
106749ab747fSPaolo Bonzini         break;
106849ab747fSPaolo Bonzini     default:
106900b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC rd_%ub @0x%02" HWADDR_PRIx " "
107000b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset);
107149ab747fSPaolo Bonzini         break;
107249ab747fSPaolo Bonzini     }
107349ab747fSPaolo Bonzini 
107449ab747fSPaolo Bonzini     ret >>= (offset & 0x3) * 8;
107549ab747fSPaolo Bonzini     ret &= (1ULL << (size * 8)) - 1;
10768be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
107749ab747fSPaolo Bonzini     return ret;
107849ab747fSPaolo Bonzini }
107949ab747fSPaolo Bonzini 
108049ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value)
108149ab747fSPaolo Bonzini {
108249ab747fSPaolo Bonzini     if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) {
108349ab747fSPaolo Bonzini         return;
108449ab747fSPaolo Bonzini     }
108549ab747fSPaolo Bonzini     s->blkgap = value & SDHC_STOP_AT_GAP_REQ;
108649ab747fSPaolo Bonzini 
108749ab747fSPaolo Bonzini     if ((value & SDHC_CONTINUE_REQ) && s->stopped_state &&
108849ab747fSPaolo Bonzini             (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) {
108949ab747fSPaolo Bonzini         if (s->stopped_state == sdhc_gap_read) {
109049ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ;
1091d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
109249ab747fSPaolo Bonzini         } else {
109349ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE;
1094d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
109549ab747fSPaolo Bonzini         }
109649ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
109749ab747fSPaolo Bonzini     } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) {
109849ab747fSPaolo Bonzini         if (s->prnsts & SDHC_DOING_READ) {
109949ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_read;
110049ab747fSPaolo Bonzini         } else if (s->prnsts & SDHC_DOING_WRITE) {
110149ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_write;
110249ab747fSPaolo Bonzini         }
110349ab747fSPaolo Bonzini     }
110449ab747fSPaolo Bonzini }
110549ab747fSPaolo Bonzini 
110649ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value)
110749ab747fSPaolo Bonzini {
110849ab747fSPaolo Bonzini     switch (value) {
110949ab747fSPaolo Bonzini     case SDHC_RESET_ALL:
1110d368ba43SKevin O'Connor         sdhci_reset(s);
111149ab747fSPaolo Bonzini         break;
111249ab747fSPaolo Bonzini     case SDHC_RESET_CMD:
111349ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_CMD_INHIBIT;
111449ab747fSPaolo Bonzini         s->norintsts &= ~SDHC_NIS_CMDCMP;
111549ab747fSPaolo Bonzini         break;
111649ab747fSPaolo Bonzini     case SDHC_RESET_DATA:
111749ab747fSPaolo Bonzini         s->data_count = 0;
111849ab747fSPaolo Bonzini         s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE |
111949ab747fSPaolo Bonzini                 SDHC_DOING_READ | SDHC_DOING_WRITE |
112049ab747fSPaolo Bonzini                 SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE);
112149ab747fSPaolo Bonzini         s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ);
112249ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
112349ab747fSPaolo Bonzini         s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY |
112449ab747fSPaolo Bonzini                 SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP);
112549ab747fSPaolo Bonzini         break;
112649ab747fSPaolo Bonzini     }
112749ab747fSPaolo Bonzini }
112849ab747fSPaolo Bonzini 
112949ab747fSPaolo Bonzini static void
1130d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
113149ab747fSPaolo Bonzini {
1132d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
113349ab747fSPaolo Bonzini     unsigned shift =  8 * (offset & 0x3);
113449ab747fSPaolo Bonzini     uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift);
1135d368ba43SKevin O'Connor     uint32_t value = val;
113649ab747fSPaolo Bonzini     value <<= shift;
113749ab747fSPaolo Bonzini 
113849ab747fSPaolo Bonzini     switch (offset & ~0x3) {
113949ab747fSPaolo Bonzini     case SDHC_SYSAD:
114049ab747fSPaolo Bonzini         s->sdmasysad = (s->sdmasysad & mask) | value;
114149ab747fSPaolo Bonzini         MASKED_WRITE(s->sdmasysad, mask, value);
114249ab747fSPaolo Bonzini         /* Writing to last byte of sdmasysad might trigger transfer */
114349ab747fSPaolo Bonzini         if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt &&
114406c5120bSPhilippe Mathieu-Daudé                 s->blksize && SDHC_DMA_TYPE(s->hostctl1) == SDHC_CTRL_SDMA) {
114545ba9f76SPrasad J Pandit             if (s->trnmod & SDHC_TRNS_MULTI) {
1146d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
114745ba9f76SPrasad J Pandit             } else {
114845ba9f76SPrasad J Pandit                 sdhci_sdma_transfer_single_block(s);
114945ba9f76SPrasad J Pandit             }
115049ab747fSPaolo Bonzini         }
115149ab747fSPaolo Bonzini         break;
115249ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
115349ab747fSPaolo Bonzini         if (!TRANSFERRING_DATA(s->prnsts)) {
115449ab747fSPaolo Bonzini             MASKED_WRITE(s->blksize, mask, value);
115549ab747fSPaolo Bonzini             MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16);
115649ab747fSPaolo Bonzini         }
11579201bb9aSAlistair Francis 
11589201bb9aSAlistair Francis         /* Limit block size to the maximum buffer size */
11599201bb9aSAlistair Francis         if (extract32(s->blksize, 0, 12) > s->buf_maxsz) {
11609201bb9aSAlistair Francis             qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than " \
11619201bb9aSAlistair Francis                           "the maximum buffer 0x%x", __func__, s->blksize,
11629201bb9aSAlistair Francis                           s->buf_maxsz);
11639201bb9aSAlistair Francis 
11649201bb9aSAlistair Francis             s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz);
11659201bb9aSAlistair Francis         }
11669201bb9aSAlistair Francis 
116749ab747fSPaolo Bonzini         break;
116849ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
116949ab747fSPaolo Bonzini         MASKED_WRITE(s->argument, mask, value);
117049ab747fSPaolo Bonzini         break;
117149ab747fSPaolo Bonzini     case SDHC_TRNMOD:
117249ab747fSPaolo Bonzini         /* DMA can be enabled only if it is supported as indicated by
117349ab747fSPaolo Bonzini          * capabilities register */
11746ff37c3dSPhilippe Mathieu-Daudé         if (!(s->capareg & R_SDHC_CAPAB_SDMA_MASK)) {
117549ab747fSPaolo Bonzini             value &= ~SDHC_TRNS_DMA;
117649ab747fSPaolo Bonzini         }
117724bddf9dSPhilippe Mathieu-Daudé         MASKED_WRITE(s->trnmod, mask, value & SDHC_TRNMOD_MASK);
117849ab747fSPaolo Bonzini         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
117949ab747fSPaolo Bonzini 
118049ab747fSPaolo Bonzini         /* Writing to the upper byte of CMDREG triggers SD command generation */
1181d368ba43SKevin O'Connor         if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) {
118249ab747fSPaolo Bonzini             break;
118349ab747fSPaolo Bonzini         }
118449ab747fSPaolo Bonzini 
1185d368ba43SKevin O'Connor         sdhci_send_command(s);
118649ab747fSPaolo Bonzini         break;
118749ab747fSPaolo Bonzini     case  SDHC_BDATA:
118849ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
1189d368ba43SKevin O'Connor             sdhci_write_dataport(s, value >> shift, size);
119049ab747fSPaolo Bonzini         }
119149ab747fSPaolo Bonzini         break;
119249ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
119349ab747fSPaolo Bonzini         if (!(mask & 0xFF0000)) {
119449ab747fSPaolo Bonzini             sdhci_blkgap_write(s, value >> 16);
119549ab747fSPaolo Bonzini         }
119606c5120bSPhilippe Mathieu-Daudé         MASKED_WRITE(s->hostctl1, mask, value);
119749ab747fSPaolo Bonzini         MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8);
119849ab747fSPaolo Bonzini         MASKED_WRITE(s->wakcon, mask >> 24, value >> 24);
119949ab747fSPaolo Bonzini         if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 ||
120049ab747fSPaolo Bonzini                 !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) {
120149ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
120249ab747fSPaolo Bonzini         }
120349ab747fSPaolo Bonzini         break;
120449ab747fSPaolo Bonzini     case SDHC_CLKCON:
120549ab747fSPaolo Bonzini         if (!(mask & 0xFF000000)) {
120649ab747fSPaolo Bonzini             sdhci_reset_write(s, value >> 24);
120749ab747fSPaolo Bonzini         }
120849ab747fSPaolo Bonzini         MASKED_WRITE(s->clkcon, mask, value);
120949ab747fSPaolo Bonzini         MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16);
121049ab747fSPaolo Bonzini         if (s->clkcon & SDHC_CLOCK_INT_EN) {
121149ab747fSPaolo Bonzini             s->clkcon |= SDHC_CLOCK_INT_STABLE;
121249ab747fSPaolo Bonzini         } else {
121349ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_INT_STABLE;
121449ab747fSPaolo Bonzini         }
121549ab747fSPaolo Bonzini         break;
121649ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
121749ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_CARDINT) {
121849ab747fSPaolo Bonzini             value &= ~SDHC_NIS_CARDINT;
121949ab747fSPaolo Bonzini         }
122049ab747fSPaolo Bonzini         s->norintsts &= mask | ~value;
122149ab747fSPaolo Bonzini         s->errintsts &= (mask >> 16) | ~(value >> 16);
122249ab747fSPaolo Bonzini         if (s->errintsts) {
122349ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
122449ab747fSPaolo Bonzini         } else {
122549ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
122649ab747fSPaolo Bonzini         }
122749ab747fSPaolo Bonzini         sdhci_update_irq(s);
122849ab747fSPaolo Bonzini         break;
122949ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
123049ab747fSPaolo Bonzini         MASKED_WRITE(s->norintstsen, mask, value);
123149ab747fSPaolo Bonzini         MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16);
123249ab747fSPaolo Bonzini         s->norintsts &= s->norintstsen;
123349ab747fSPaolo Bonzini         s->errintsts &= s->errintstsen;
123449ab747fSPaolo Bonzini         if (s->errintsts) {
123549ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
123649ab747fSPaolo Bonzini         } else {
123749ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
123849ab747fSPaolo Bonzini         }
12390a7ac9f9SAndrew Baumann         /* Quirk for Raspberry Pi: pending card insert interrupt
12400a7ac9f9SAndrew Baumann          * appears when first enabled after power on */
12410a7ac9f9SAndrew Baumann         if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) {
12420a7ac9f9SAndrew Baumann             assert(s->pending_insert_quirk);
12430a7ac9f9SAndrew Baumann             s->norintsts |= SDHC_NIS_INSERT;
12440a7ac9f9SAndrew Baumann             s->pending_insert_state = false;
12450a7ac9f9SAndrew Baumann         }
124649ab747fSPaolo Bonzini         sdhci_update_irq(s);
124749ab747fSPaolo Bonzini         break;
124849ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
124949ab747fSPaolo Bonzini         MASKED_WRITE(s->norintsigen, mask, value);
125049ab747fSPaolo Bonzini         MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16);
125149ab747fSPaolo Bonzini         sdhci_update_irq(s);
125249ab747fSPaolo Bonzini         break;
125349ab747fSPaolo Bonzini     case SDHC_ADMAERR:
125449ab747fSPaolo Bonzini         MASKED_WRITE(s->admaerr, mask, value);
125549ab747fSPaolo Bonzini         break;
125649ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
125749ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL |
125849ab747fSPaolo Bonzini                 (uint64_t)mask)) | (uint64_t)value;
125949ab747fSPaolo Bonzini         break;
126049ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
126149ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL |
126249ab747fSPaolo Bonzini                 ((uint64_t)mask << 32))) | ((uint64_t)value << 32);
126349ab747fSPaolo Bonzini         break;
126449ab747fSPaolo Bonzini     case SDHC_FEAER:
126549ab747fSPaolo Bonzini         s->acmd12errsts |= value;
126649ab747fSPaolo Bonzini         s->errintsts |= (value >> 16) & s->errintstsen;
126749ab747fSPaolo Bonzini         if (s->acmd12errsts) {
126849ab747fSPaolo Bonzini             s->errintsts |= SDHC_EIS_CMD12ERR;
126949ab747fSPaolo Bonzini         }
127049ab747fSPaolo Bonzini         if (s->errintsts) {
127149ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
127249ab747fSPaolo Bonzini         }
127349ab747fSPaolo Bonzini         sdhci_update_irq(s);
127449ab747fSPaolo Bonzini         break;
12755d2c0464SAndrey Smirnov     case SDHC_ACMD12ERRSTS:
12760034ebe6SPhilippe Mathieu-Daudé         MASKED_WRITE(s->acmd12errsts, mask, value & UINT16_MAX);
12770034ebe6SPhilippe Mathieu-Daudé         if (s->uhs_mode >= UHS_I) {
12780034ebe6SPhilippe Mathieu-Daudé             MASKED_WRITE(s->hostctl2, mask >> 16, value >> 16);
12790034ebe6SPhilippe Mathieu-Daudé 
12800034ebe6SPhilippe Mathieu-Daudé             if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, V18_ENA)) {
12810034ebe6SPhilippe Mathieu-Daudé                 sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_1_8V);
12820034ebe6SPhilippe Mathieu-Daudé             } else {
12830034ebe6SPhilippe Mathieu-Daudé                 sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_3_3V);
12840034ebe6SPhilippe Mathieu-Daudé             }
12850034ebe6SPhilippe Mathieu-Daudé         }
12865d2c0464SAndrey Smirnov         break;
12875efc9016SPhilippe Mathieu-Daudé 
12885efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
12895efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
12905efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR:
12915efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
12925efc9016SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_GUEST_ERROR, "SDHC wr_%ub @0x%02" HWADDR_PRIx
12935efc9016SPhilippe Mathieu-Daudé                       " <- 0x%08x read-only\n", size, offset, value >> shift);
12945efc9016SPhilippe Mathieu-Daudé         break;
12955efc9016SPhilippe Mathieu-Daudé 
129649ab747fSPaolo Bonzini     default:
129700b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC wr_%ub @0x%02" HWADDR_PRIx " <- 0x%08x "
129800b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset, value >> shift);
129949ab747fSPaolo Bonzini         break;
130049ab747fSPaolo Bonzini     }
13018be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("wr", size << 3, offset, "<-",
13028be487d8SPhilippe Mathieu-Daudé                        value >> shift, value >> shift);
130349ab747fSPaolo Bonzini }
130449ab747fSPaolo Bonzini 
130549ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = {
1306d368ba43SKevin O'Connor     .read = sdhci_read,
1307d368ba43SKevin O'Connor     .write = sdhci_write,
130849ab747fSPaolo Bonzini     .valid = {
130949ab747fSPaolo Bonzini         .min_access_size = 1,
131049ab747fSPaolo Bonzini         .max_access_size = 4,
131149ab747fSPaolo Bonzini         .unaligned = false
131249ab747fSPaolo Bonzini     },
131349ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
131449ab747fSPaolo Bonzini };
131549ab747fSPaolo Bonzini 
1316aceb5b06SPhilippe Mathieu-Daudé static void sdhci_init_readonly_registers(SDHCIState *s, Error **errp)
1317aceb5b06SPhilippe Mathieu-Daudé {
13186ff37c3dSPhilippe Mathieu-Daudé     Error *local_err = NULL;
13196ff37c3dSPhilippe Mathieu-Daudé 
13204d67852dSPhilippe Mathieu-Daudé     switch (s->sd_spec_version) {
13214d67852dSPhilippe Mathieu-Daudé     case 2 ... 3:
13224d67852dSPhilippe Mathieu-Daudé         break;
13234d67852dSPhilippe Mathieu-Daudé     default:
13244d67852dSPhilippe Mathieu-Daudé         error_setg(errp, "Only Spec v2/v3 are supported");
1325aceb5b06SPhilippe Mathieu-Daudé         return;
1326aceb5b06SPhilippe Mathieu-Daudé     }
1327aceb5b06SPhilippe Mathieu-Daudé     s->version = (SDHC_HCVER_VENDOR << 8) | (s->sd_spec_version - 1);
13286ff37c3dSPhilippe Mathieu-Daudé 
13296ff37c3dSPhilippe Mathieu-Daudé     sdhci_check_capareg(s, &local_err);
13306ff37c3dSPhilippe Mathieu-Daudé     if (local_err) {
13316ff37c3dSPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
13326ff37c3dSPhilippe Mathieu-Daudé         return;
13336ff37c3dSPhilippe Mathieu-Daudé     }
1334aceb5b06SPhilippe Mathieu-Daudé }
1335aceb5b06SPhilippe Mathieu-Daudé 
1336b635d98cSPhilippe Mathieu-Daudé /* --- qdev common --- */
1337b635d98cSPhilippe Mathieu-Daudé 
1338b635d98cSPhilippe Mathieu-Daudé #define DEFINE_SDHCI_COMMON_PROPERTIES(_state) \
1339aceb5b06SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT8("sd-spec-version", _state, sd_spec_version, 2), \
13400034ebe6SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT8("uhs", _state, uhs_mode, UHS_NOT_SUPPORTED), \
1341aceb5b06SPhilippe Mathieu-Daudé     \
1342aceb5b06SPhilippe Mathieu-Daudé     /* Capabilities registers provide information on supported
1343aceb5b06SPhilippe Mathieu-Daudé      * features of this specific host controller implementation */ \
13445efc9016SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("capareg", _state, capareg, SDHC_CAPAB_REG_DEFAULT), \
13455efc9016SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("maxcurr", _state, maxcurr, 0)
1346b635d98cSPhilippe Mathieu-Daudé 
134740bbc194SPeter Maydell static void sdhci_initfn(SDHCIState *s)
134849ab747fSPaolo Bonzini {
134940bbc194SPeter Maydell     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus),
135040bbc194SPeter Maydell                         TYPE_SDHCI_BUS, DEVICE(s), "sd-bus");
135149ab747fSPaolo Bonzini 
1352bc72ad67SAlex Bligh     s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s);
1353d368ba43SKevin O'Connor     s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s);
1354fd1e5c81SAndrey Smirnov 
1355fd1e5c81SAndrey Smirnov     s->io_ops = &sdhci_mmio_ops;
135649ab747fSPaolo Bonzini }
135749ab747fSPaolo Bonzini 
13587302dcd6SKevin O'Connor static void sdhci_uninitfn(SDHCIState *s)
135949ab747fSPaolo Bonzini {
1360bc72ad67SAlex Bligh     timer_del(s->insert_timer);
1361bc72ad67SAlex Bligh     timer_free(s->insert_timer);
1362bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
1363bc72ad67SAlex Bligh     timer_free(s->transfer_timer);
136449ab747fSPaolo Bonzini 
136549ab747fSPaolo Bonzini     g_free(s->fifo_buffer);
136649ab747fSPaolo Bonzini     s->fifo_buffer = NULL;
136749ab747fSPaolo Bonzini }
136849ab747fSPaolo Bonzini 
136925367498SPhilippe Mathieu-Daudé static void sdhci_common_realize(SDHCIState *s, Error **errp)
137025367498SPhilippe Mathieu-Daudé {
1371aceb5b06SPhilippe Mathieu-Daudé     Error *local_err = NULL;
1372aceb5b06SPhilippe Mathieu-Daudé 
1373aceb5b06SPhilippe Mathieu-Daudé     sdhci_init_readonly_registers(s, &local_err);
1374aceb5b06SPhilippe Mathieu-Daudé     if (local_err) {
1375aceb5b06SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
1376aceb5b06SPhilippe Mathieu-Daudé         return;
1377aceb5b06SPhilippe Mathieu-Daudé     }
137825367498SPhilippe Mathieu-Daudé     s->buf_maxsz = sdhci_get_fifolen(s);
137925367498SPhilippe Mathieu-Daudé     s->fifo_buffer = g_malloc0(s->buf_maxsz);
138025367498SPhilippe Mathieu-Daudé 
138125367498SPhilippe Mathieu-Daudé     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci",
138225367498SPhilippe Mathieu-Daudé                           SDHC_REGISTERS_MAP_SIZE);
138325367498SPhilippe Mathieu-Daudé }
138425367498SPhilippe Mathieu-Daudé 
13858b7455c7SPhilippe Mathieu-Daudé static void sdhci_common_unrealize(SDHCIState *s, Error **errp)
13868b7455c7SPhilippe Mathieu-Daudé {
13878b7455c7SPhilippe Mathieu-Daudé     /* This function is expected to be called only once for each class:
13888b7455c7SPhilippe Mathieu-Daudé      * - SysBus:    via DeviceClass->unrealize(),
13898b7455c7SPhilippe Mathieu-Daudé      * - PCI:       via PCIDeviceClass->exit().
13908b7455c7SPhilippe Mathieu-Daudé      * However to avoid double-free and/or use-after-free we still nullify
13918b7455c7SPhilippe Mathieu-Daudé      * this variable (better safe than sorry!). */
13928b7455c7SPhilippe Mathieu-Daudé     g_free(s->fifo_buffer);
13938b7455c7SPhilippe Mathieu-Daudé     s->fifo_buffer = NULL;
13948b7455c7SPhilippe Mathieu-Daudé }
13958b7455c7SPhilippe Mathieu-Daudé 
13960a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque)
13970a7ac9f9SAndrew Baumann {
13980a7ac9f9SAndrew Baumann     SDHCIState *s = opaque;
13990a7ac9f9SAndrew Baumann 
14000a7ac9f9SAndrew Baumann     return s->pending_insert_state;
14010a7ac9f9SAndrew Baumann }
14020a7ac9f9SAndrew Baumann 
14030a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = {
14040a7ac9f9SAndrew Baumann     .name = "sdhci/pending-insert",
14050a7ac9f9SAndrew Baumann     .version_id = 1,
14060a7ac9f9SAndrew Baumann     .minimum_version_id = 1,
14070a7ac9f9SAndrew Baumann     .needed = sdhci_pending_insert_vmstate_needed,
14080a7ac9f9SAndrew Baumann     .fields = (VMStateField[]) {
14090a7ac9f9SAndrew Baumann         VMSTATE_BOOL(pending_insert_state, SDHCIState),
14100a7ac9f9SAndrew Baumann         VMSTATE_END_OF_LIST()
14110a7ac9f9SAndrew Baumann     },
14120a7ac9f9SAndrew Baumann };
14130a7ac9f9SAndrew Baumann 
141449ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = {
141549ab747fSPaolo Bonzini     .name = "sdhci",
141649ab747fSPaolo Bonzini     .version_id = 1,
141749ab747fSPaolo Bonzini     .minimum_version_id = 1,
141849ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
141949ab747fSPaolo Bonzini         VMSTATE_UINT32(sdmasysad, SDHCIState),
142049ab747fSPaolo Bonzini         VMSTATE_UINT16(blksize, SDHCIState),
142149ab747fSPaolo Bonzini         VMSTATE_UINT16(blkcnt, SDHCIState),
142249ab747fSPaolo Bonzini         VMSTATE_UINT32(argument, SDHCIState),
142349ab747fSPaolo Bonzini         VMSTATE_UINT16(trnmod, SDHCIState),
142449ab747fSPaolo Bonzini         VMSTATE_UINT16(cmdreg, SDHCIState),
142549ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4),
142649ab747fSPaolo Bonzini         VMSTATE_UINT32(prnsts, SDHCIState),
142706c5120bSPhilippe Mathieu-Daudé         VMSTATE_UINT8(hostctl1, SDHCIState),
142849ab747fSPaolo Bonzini         VMSTATE_UINT8(pwrcon, SDHCIState),
142949ab747fSPaolo Bonzini         VMSTATE_UINT8(blkgap, SDHCIState),
143049ab747fSPaolo Bonzini         VMSTATE_UINT8(wakcon, SDHCIState),
143149ab747fSPaolo Bonzini         VMSTATE_UINT16(clkcon, SDHCIState),
143249ab747fSPaolo Bonzini         VMSTATE_UINT8(timeoutcon, SDHCIState),
143349ab747fSPaolo Bonzini         VMSTATE_UINT8(admaerr, SDHCIState),
143449ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsts, SDHCIState),
143549ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsts, SDHCIState),
143649ab747fSPaolo Bonzini         VMSTATE_UINT16(norintstsen, SDHCIState),
143749ab747fSPaolo Bonzini         VMSTATE_UINT16(errintstsen, SDHCIState),
143849ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsigen, SDHCIState),
143949ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsigen, SDHCIState),
144049ab747fSPaolo Bonzini         VMSTATE_UINT16(acmd12errsts, SDHCIState),
144149ab747fSPaolo Bonzini         VMSTATE_UINT16(data_count, SDHCIState),
144249ab747fSPaolo Bonzini         VMSTATE_UINT64(admasysaddr, SDHCIState),
144349ab747fSPaolo Bonzini         VMSTATE_UINT8(stopped_state, SDHCIState),
144459046ec2SHalil Pasic         VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz),
1445e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(insert_timer, SDHCIState),
1446e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(transfer_timer, SDHCIState),
144749ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
14480a7ac9f9SAndrew Baumann     },
14490a7ac9f9SAndrew Baumann     .subsections = (const VMStateDescription*[]) {
14500a7ac9f9SAndrew Baumann         &sdhci_pending_insert_vmstate,
14510a7ac9f9SAndrew Baumann         NULL
14520a7ac9f9SAndrew Baumann     },
145349ab747fSPaolo Bonzini };
145449ab747fSPaolo Bonzini 
14551c92c505SPhilippe Mathieu-Daudé static void sdhci_common_class_init(ObjectClass *klass, void *data)
14561c92c505SPhilippe Mathieu-Daudé {
14571c92c505SPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(klass);
14581c92c505SPhilippe Mathieu-Daudé 
14591c92c505SPhilippe Mathieu-Daudé     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
14601c92c505SPhilippe Mathieu-Daudé     dc->vmsd = &sdhci_vmstate;
14611c92c505SPhilippe Mathieu-Daudé     dc->reset = sdhci_poweron_reset;
14621c92c505SPhilippe Mathieu-Daudé }
14631c92c505SPhilippe Mathieu-Daudé 
1464b635d98cSPhilippe Mathieu-Daudé /* --- qdev PCI --- */
1465b635d98cSPhilippe Mathieu-Daudé 
14665ec911c3SKevin O'Connor static Property sdhci_pci_properties[] = {
1467b635d98cSPhilippe Mathieu-Daudé     DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState),
146849ab747fSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
146949ab747fSPaolo Bonzini };
147049ab747fSPaolo Bonzini 
14719af21dbeSMarkus Armbruster static void sdhci_pci_realize(PCIDevice *dev, Error **errp)
1472224d10ffSKevin O'Connor {
1473224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
1474ab958e38SPhilippe Mathieu-Daudé     Error *local_err = NULL;
147525367498SPhilippe Mathieu-Daudé 
147625367498SPhilippe Mathieu-Daudé     sdhci_initfn(s);
147725367498SPhilippe Mathieu-Daudé     sdhci_common_realize(s, errp);
1478ab958e38SPhilippe Mathieu-Daudé     if (local_err) {
1479ab958e38SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
148025367498SPhilippe Mathieu-Daudé         return;
148125367498SPhilippe Mathieu-Daudé     }
148225367498SPhilippe Mathieu-Daudé 
1483224d10ffSKevin O'Connor     dev->config[PCI_CLASS_PROG] = 0x01; /* Standard Host supported DMA */
1484224d10ffSKevin O'Connor     dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
1485224d10ffSKevin O'Connor     s->irq = pci_allocate_irq(dev);
1486dd55c485SPhilippe Mathieu-Daudé     s->dma_as = pci_get_address_space(dev);
1487dd55c485SPhilippe Mathieu-Daudé     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->iomem);
1488224d10ffSKevin O'Connor }
1489224d10ffSKevin O'Connor 
1490224d10ffSKevin O'Connor static void sdhci_pci_exit(PCIDevice *dev)
1491224d10ffSKevin O'Connor {
1492224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
14938b7455c7SPhilippe Mathieu-Daudé 
14948b7455c7SPhilippe Mathieu-Daudé     sdhci_common_unrealize(s, &error_abort);
1495224d10ffSKevin O'Connor     sdhci_uninitfn(s);
1496224d10ffSKevin O'Connor }
1497224d10ffSKevin O'Connor 
1498224d10ffSKevin O'Connor static void sdhci_pci_class_init(ObjectClass *klass, void *data)
1499224d10ffSKevin O'Connor {
1500224d10ffSKevin O'Connor     DeviceClass *dc = DEVICE_CLASS(klass);
1501224d10ffSKevin O'Connor     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1502224d10ffSKevin O'Connor 
15039af21dbeSMarkus Armbruster     k->realize = sdhci_pci_realize;
1504224d10ffSKevin O'Connor     k->exit = sdhci_pci_exit;
1505224d10ffSKevin O'Connor     k->vendor_id = PCI_VENDOR_ID_REDHAT;
1506224d10ffSKevin O'Connor     k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI;
1507224d10ffSKevin O'Connor     k->class_id = PCI_CLASS_SYSTEM_SDHCI;
15085ec911c3SKevin O'Connor     dc->props = sdhci_pci_properties;
15091c92c505SPhilippe Mathieu-Daudé 
15101c92c505SPhilippe Mathieu-Daudé     sdhci_common_class_init(klass, data);
1511224d10ffSKevin O'Connor }
1512224d10ffSKevin O'Connor 
1513224d10ffSKevin O'Connor static const TypeInfo sdhci_pci_info = {
1514224d10ffSKevin O'Connor     .name = TYPE_PCI_SDHCI,
1515224d10ffSKevin O'Connor     .parent = TYPE_PCI_DEVICE,
1516224d10ffSKevin O'Connor     .instance_size = sizeof(SDHCIState),
1517224d10ffSKevin O'Connor     .class_init = sdhci_pci_class_init,
1518fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
1519fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1520fd3b02c8SEduardo Habkost         { },
1521fd3b02c8SEduardo Habkost     },
1522224d10ffSKevin O'Connor };
1523224d10ffSKevin O'Connor 
1524b635d98cSPhilippe Mathieu-Daudé /* --- qdev SysBus --- */
1525b635d98cSPhilippe Mathieu-Daudé 
15265ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = {
1527b635d98cSPhilippe Mathieu-Daudé     DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState),
15280a7ac9f9SAndrew Baumann     DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk,
15290a7ac9f9SAndrew Baumann                      false),
153060765b6cSPhilippe Mathieu-Daudé     DEFINE_PROP_LINK("dma", SDHCIState,
153160765b6cSPhilippe Mathieu-Daudé                      dma_mr, TYPE_MEMORY_REGION, MemoryRegion *),
15325ec911c3SKevin O'Connor     DEFINE_PROP_END_OF_LIST(),
15335ec911c3SKevin O'Connor };
15345ec911c3SKevin O'Connor 
15357302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj)
153649ab747fSPaolo Bonzini {
15377302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
15385ec911c3SKevin O'Connor 
153940bbc194SPeter Maydell     sdhci_initfn(s);
15407302dcd6SKevin O'Connor }
15417302dcd6SKevin O'Connor 
15427302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj)
15437302dcd6SKevin O'Connor {
15447302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
154560765b6cSPhilippe Mathieu-Daudé 
154660765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
154760765b6cSPhilippe Mathieu-Daudé         object_unparent(OBJECT(s->dma_mr));
154860765b6cSPhilippe Mathieu-Daudé     }
154960765b6cSPhilippe Mathieu-Daudé 
15507302dcd6SKevin O'Connor     sdhci_uninitfn(s);
15517302dcd6SKevin O'Connor }
15527302dcd6SKevin O'Connor 
15537302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp)
15547302dcd6SKevin O'Connor {
15557302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(dev);
155649ab747fSPaolo Bonzini     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1557ab958e38SPhilippe Mathieu-Daudé     Error *local_err = NULL;
155849ab747fSPaolo Bonzini 
155925367498SPhilippe Mathieu-Daudé     sdhci_common_realize(s, errp);
1560ab958e38SPhilippe Mathieu-Daudé     if (local_err) {
1561ab958e38SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
156225367498SPhilippe Mathieu-Daudé         return;
156325367498SPhilippe Mathieu-Daudé     }
156425367498SPhilippe Mathieu-Daudé 
156560765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
156602e57e1cSPhilippe Mathieu-Daudé         s->dma_as = &s->sysbus_dma_as;
156760765b6cSPhilippe Mathieu-Daudé         address_space_init(s->dma_as, s->dma_mr, "sdhci-dma");
156860765b6cSPhilippe Mathieu-Daudé     } else {
156960765b6cSPhilippe Mathieu-Daudé         /* use system_memory() if property "dma" not set */
1570dd55c485SPhilippe Mathieu-Daudé         s->dma_as = &address_space_memory;
157160765b6cSPhilippe Mathieu-Daudé     }
1572dd55c485SPhilippe Mathieu-Daudé 
157349ab747fSPaolo Bonzini     sysbus_init_irq(sbd, &s->irq);
1574fd1e5c81SAndrey Smirnov 
1575fd1e5c81SAndrey Smirnov     memory_region_init_io(&s->iomem, OBJECT(s), s->io_ops, s, "sdhci",
1576fd1e5c81SAndrey Smirnov             SDHC_REGISTERS_MAP_SIZE);
1577fd1e5c81SAndrey Smirnov 
157849ab747fSPaolo Bonzini     sysbus_init_mmio(sbd, &s->iomem);
157949ab747fSPaolo Bonzini }
158049ab747fSPaolo Bonzini 
15818b7455c7SPhilippe Mathieu-Daudé static void sdhci_sysbus_unrealize(DeviceState *dev, Error **errp)
15828b7455c7SPhilippe Mathieu-Daudé {
15838b7455c7SPhilippe Mathieu-Daudé     SDHCIState *s = SYSBUS_SDHCI(dev);
15848b7455c7SPhilippe Mathieu-Daudé 
15858b7455c7SPhilippe Mathieu-Daudé     sdhci_common_unrealize(s, &error_abort);
158660765b6cSPhilippe Mathieu-Daudé 
158760765b6cSPhilippe Mathieu-Daudé      if (s->dma_mr) {
158860765b6cSPhilippe Mathieu-Daudé         address_space_destroy(s->dma_as);
158960765b6cSPhilippe Mathieu-Daudé     }
15908b7455c7SPhilippe Mathieu-Daudé }
15918b7455c7SPhilippe Mathieu-Daudé 
15927302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)
159349ab747fSPaolo Bonzini {
159449ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
159549ab747fSPaolo Bonzini 
15965ec911c3SKevin O'Connor     dc->props = sdhci_sysbus_properties;
15977302dcd6SKevin O'Connor     dc->realize = sdhci_sysbus_realize;
15988b7455c7SPhilippe Mathieu-Daudé     dc->unrealize = sdhci_sysbus_unrealize;
15991c92c505SPhilippe Mathieu-Daudé 
16001c92c505SPhilippe Mathieu-Daudé     sdhci_common_class_init(klass, data);
160149ab747fSPaolo Bonzini }
160249ab747fSPaolo Bonzini 
16037302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = {
16047302dcd6SKevin O'Connor     .name = TYPE_SYSBUS_SDHCI,
160549ab747fSPaolo Bonzini     .parent = TYPE_SYS_BUS_DEVICE,
160649ab747fSPaolo Bonzini     .instance_size = sizeof(SDHCIState),
16077302dcd6SKevin O'Connor     .instance_init = sdhci_sysbus_init,
16087302dcd6SKevin O'Connor     .instance_finalize = sdhci_sysbus_finalize,
16097302dcd6SKevin O'Connor     .class_init = sdhci_sysbus_class_init,
161049ab747fSPaolo Bonzini };
161149ab747fSPaolo Bonzini 
1612b635d98cSPhilippe Mathieu-Daudé /* --- qdev bus master --- */
1613b635d98cSPhilippe Mathieu-Daudé 
161440bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data)
161540bbc194SPeter Maydell {
161640bbc194SPeter Maydell     SDBusClass *sbc = SD_BUS_CLASS(klass);
161740bbc194SPeter Maydell 
161840bbc194SPeter Maydell     sbc->set_inserted = sdhci_set_inserted;
161940bbc194SPeter Maydell     sbc->set_readonly = sdhci_set_readonly;
162040bbc194SPeter Maydell }
162140bbc194SPeter Maydell 
162240bbc194SPeter Maydell static const TypeInfo sdhci_bus_info = {
162340bbc194SPeter Maydell     .name = TYPE_SDHCI_BUS,
162440bbc194SPeter Maydell     .parent = TYPE_SD_BUS,
162540bbc194SPeter Maydell     .instance_size = sizeof(SDBus),
162640bbc194SPeter Maydell     .class_init = sdhci_bus_class_init,
162740bbc194SPeter Maydell };
162840bbc194SPeter Maydell 
1629fd1e5c81SAndrey Smirnov static uint64_t usdhc_read(void *opaque, hwaddr offset, unsigned size)
1630fd1e5c81SAndrey Smirnov {
1631fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
1632fd1e5c81SAndrey Smirnov     uint32_t ret;
163306c5120bSPhilippe Mathieu-Daudé     uint16_t hostctl1;
1634fd1e5c81SAndrey Smirnov 
1635fd1e5c81SAndrey Smirnov     switch (offset) {
1636fd1e5c81SAndrey Smirnov     default:
1637fd1e5c81SAndrey Smirnov         return sdhci_read(opaque, offset, size);
1638fd1e5c81SAndrey Smirnov 
1639fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1640fd1e5c81SAndrey Smirnov         /*
1641fd1e5c81SAndrey Smirnov          * For a detailed explanation on the following bit
1642fd1e5c81SAndrey Smirnov          * manipulation code see comments in a similar part of
1643fd1e5c81SAndrey Smirnov          * usdhc_write()
1644fd1e5c81SAndrey Smirnov          */
164506c5120bSPhilippe Mathieu-Daudé         hostctl1 = SDHC_DMA_TYPE(s->hostctl1) << (8 - 3);
1646fd1e5c81SAndrey Smirnov 
164706c5120bSPhilippe Mathieu-Daudé         if (s->hostctl1 & SDHC_CTRL_8BITBUS) {
164806c5120bSPhilippe Mathieu-Daudé             hostctl1 |= ESDHC_CTRL_8BITBUS;
1649fd1e5c81SAndrey Smirnov         }
1650fd1e5c81SAndrey Smirnov 
165106c5120bSPhilippe Mathieu-Daudé         if (s->hostctl1 & SDHC_CTRL_4BITBUS) {
165206c5120bSPhilippe Mathieu-Daudé             hostctl1 |= ESDHC_CTRL_4BITBUS;
1653fd1e5c81SAndrey Smirnov         }
1654fd1e5c81SAndrey Smirnov 
165506c5120bSPhilippe Mathieu-Daudé         ret  = hostctl1;
1656fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->blkgap << 16;
1657fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->wakcon << 24;
1658fd1e5c81SAndrey Smirnov 
1659fd1e5c81SAndrey Smirnov         break;
1660fd1e5c81SAndrey Smirnov 
1661fd1e5c81SAndrey Smirnov     case ESDHC_DLL_CTRL:
1662fd1e5c81SAndrey Smirnov     case ESDHC_TUNE_CTRL_STATUS:
1663fd1e5c81SAndrey Smirnov     case ESDHC_UNDOCUMENTED_REG27:
1664fd1e5c81SAndrey Smirnov     case ESDHC_TUNING_CTRL:
1665fd1e5c81SAndrey Smirnov     case ESDHC_VENDOR_SPEC:
1666fd1e5c81SAndrey Smirnov     case ESDHC_MIX_CTRL:
1667fd1e5c81SAndrey Smirnov     case ESDHC_WTMK_LVL:
1668fd1e5c81SAndrey Smirnov         ret = 0;
1669fd1e5c81SAndrey Smirnov         break;
1670fd1e5c81SAndrey Smirnov     }
1671fd1e5c81SAndrey Smirnov 
1672fd1e5c81SAndrey Smirnov     return ret;
1673fd1e5c81SAndrey Smirnov }
1674fd1e5c81SAndrey Smirnov 
1675fd1e5c81SAndrey Smirnov static void
1676fd1e5c81SAndrey Smirnov usdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
1677fd1e5c81SAndrey Smirnov {
1678fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
167906c5120bSPhilippe Mathieu-Daudé     uint8_t hostctl1;
1680fd1e5c81SAndrey Smirnov     uint32_t value = (uint32_t)val;
1681fd1e5c81SAndrey Smirnov 
1682fd1e5c81SAndrey Smirnov     switch (offset) {
1683fd1e5c81SAndrey Smirnov     case ESDHC_DLL_CTRL:
1684fd1e5c81SAndrey Smirnov     case ESDHC_TUNE_CTRL_STATUS:
1685fd1e5c81SAndrey Smirnov     case ESDHC_UNDOCUMENTED_REG27:
1686fd1e5c81SAndrey Smirnov     case ESDHC_TUNING_CTRL:
1687fd1e5c81SAndrey Smirnov     case ESDHC_WTMK_LVL:
1688fd1e5c81SAndrey Smirnov     case ESDHC_VENDOR_SPEC:
1689fd1e5c81SAndrey Smirnov         break;
1690fd1e5c81SAndrey Smirnov 
1691fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1692fd1e5c81SAndrey Smirnov         /*
1693fd1e5c81SAndrey Smirnov          * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL)
1694fd1e5c81SAndrey Smirnov          *
1695fd1e5c81SAndrey Smirnov          *       7         6     5      4      3      2        1      0
1696fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1697fd1e5c81SAndrey Smirnov          * | Card      | Card   | Endian | DATA3     | Data     | Led     |
1698fd1e5c81SAndrey Smirnov          * | Detect    | Detect | Mode   | as Card   | Transfer | Control |
1699fd1e5c81SAndrey Smirnov          * | Signal    | Test   |        | Detection | Width    |         |
1700fd1e5c81SAndrey Smirnov          * | Selection | Level  |        | Pin       |          |         |
1701fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1702fd1e5c81SAndrey Smirnov          *
1703fd1e5c81SAndrey Smirnov          * and 0x29
1704fd1e5c81SAndrey Smirnov          *
1705fd1e5c81SAndrey Smirnov          *  15      10 9    8
1706fd1e5c81SAndrey Smirnov          * |----------+------|
1707fd1e5c81SAndrey Smirnov          * | Reserved | DMA  |
1708fd1e5c81SAndrey Smirnov          * |          | Sel. |
1709fd1e5c81SAndrey Smirnov          * |          |      |
1710fd1e5c81SAndrey Smirnov          * |----------+------|
1711fd1e5c81SAndrey Smirnov          *
1712fd1e5c81SAndrey Smirnov          * and here's what SDCHI spec expects those offsets to be:
1713fd1e5c81SAndrey Smirnov          *
1714fd1e5c81SAndrey Smirnov          * 0x28 (Host Control Register)
1715fd1e5c81SAndrey Smirnov          *
1716fd1e5c81SAndrey Smirnov          *     7        6         5       4  3      2         1        0
1717fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1718fd1e5c81SAndrey Smirnov          * | Card   | Card   | Extended | DMA  | High   | Data     | LED     |
1719fd1e5c81SAndrey Smirnov          * | Detect | Detect | Data     | Sel. | Speed  | Transfer | Control |
1720fd1e5c81SAndrey Smirnov          * | Signal | Test   | Transfer |      | Enable | Width    |         |
1721fd1e5c81SAndrey Smirnov          * | Sel.   | Level  | Width    |      |        |          |         |
1722fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1723fd1e5c81SAndrey Smirnov          *
1724fd1e5c81SAndrey Smirnov          * and 0x29 (Power Control Register)
1725fd1e5c81SAndrey Smirnov          *
1726fd1e5c81SAndrey Smirnov          * |----------------------------------|
1727fd1e5c81SAndrey Smirnov          * | Power Control Register           |
1728fd1e5c81SAndrey Smirnov          * |                                  |
1729fd1e5c81SAndrey Smirnov          * | Description omitted,             |
1730fd1e5c81SAndrey Smirnov          * | since it has no analog in ESDHCI |
1731fd1e5c81SAndrey Smirnov          * |                                  |
1732fd1e5c81SAndrey Smirnov          * |----------------------------------|
1733fd1e5c81SAndrey Smirnov          *
1734fd1e5c81SAndrey Smirnov          * Since offsets 0x2A and 0x2B should be compatible between
1735fd1e5c81SAndrey Smirnov          * both IP specs we only need to reconcile least 16-bit of the
1736fd1e5c81SAndrey Smirnov          * word we've been given.
1737fd1e5c81SAndrey Smirnov          */
1738fd1e5c81SAndrey Smirnov 
1739fd1e5c81SAndrey Smirnov         /*
1740fd1e5c81SAndrey Smirnov          * First, save bits 7 6 and 0 since they are identical
1741fd1e5c81SAndrey Smirnov          */
174206c5120bSPhilippe Mathieu-Daudé         hostctl1 = value & (SDHC_CTRL_LED |
1743fd1e5c81SAndrey Smirnov                             SDHC_CTRL_CDTEST_INS |
1744fd1e5c81SAndrey Smirnov                             SDHC_CTRL_CDTEST_EN);
1745fd1e5c81SAndrey Smirnov         /*
1746fd1e5c81SAndrey Smirnov          * Second, split "Data Transfer Width" from bits 2 and 1 in to
1747fd1e5c81SAndrey Smirnov          * bits 5 and 1
1748fd1e5c81SAndrey Smirnov          */
1749fd1e5c81SAndrey Smirnov         if (value & ESDHC_CTRL_8BITBUS) {
175006c5120bSPhilippe Mathieu-Daudé             hostctl1 |= SDHC_CTRL_8BITBUS;
1751fd1e5c81SAndrey Smirnov         }
1752fd1e5c81SAndrey Smirnov 
1753fd1e5c81SAndrey Smirnov         if (value & ESDHC_CTRL_4BITBUS) {
175406c5120bSPhilippe Mathieu-Daudé             hostctl1 |= ESDHC_CTRL_4BITBUS;
1755fd1e5c81SAndrey Smirnov         }
1756fd1e5c81SAndrey Smirnov 
1757fd1e5c81SAndrey Smirnov         /*
1758fd1e5c81SAndrey Smirnov          * Third, move DMA select from bits 9 and 8 to bits 4 and 3
1759fd1e5c81SAndrey Smirnov          */
176006c5120bSPhilippe Mathieu-Daudé         hostctl1 |= SDHC_DMA_TYPE(value >> (8 - 3));
1761fd1e5c81SAndrey Smirnov 
1762fd1e5c81SAndrey Smirnov         /*
1763fd1e5c81SAndrey Smirnov          * Now place the corrected value into low 16-bit of the value
1764fd1e5c81SAndrey Smirnov          * we are going to give standard SDHCI write function
1765fd1e5c81SAndrey Smirnov          *
1766fd1e5c81SAndrey Smirnov          * NOTE: This transformation should be the inverse of what can
1767fd1e5c81SAndrey Smirnov          * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux
1768fd1e5c81SAndrey Smirnov          * kernel
1769fd1e5c81SAndrey Smirnov          */
1770fd1e5c81SAndrey Smirnov         value &= ~UINT16_MAX;
177106c5120bSPhilippe Mathieu-Daudé         value |= hostctl1;
1772fd1e5c81SAndrey Smirnov         value |= (uint16_t)s->pwrcon << 8;
1773fd1e5c81SAndrey Smirnov 
1774fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, value, size);
1775fd1e5c81SAndrey Smirnov         break;
1776fd1e5c81SAndrey Smirnov 
1777fd1e5c81SAndrey Smirnov     case ESDHC_MIX_CTRL:
1778fd1e5c81SAndrey Smirnov         /*
1779fd1e5c81SAndrey Smirnov          * So, when SD/MMC stack in Linux tries to write to "Transfer
1780fd1e5c81SAndrey Smirnov          * Mode Register", ESDHC i.MX quirk code will translate it
1781fd1e5c81SAndrey Smirnov          * into a write to ESDHC_MIX_CTRL, so we do the opposite in
1782fd1e5c81SAndrey Smirnov          * order to get where we started
1783fd1e5c81SAndrey Smirnov          *
1784fd1e5c81SAndrey Smirnov          * Note that Auto CMD23 Enable bit is located in a wrong place
1785fd1e5c81SAndrey Smirnov          * on i.MX, but since it is not used by QEMU we do not care.
1786fd1e5c81SAndrey Smirnov          *
1787fd1e5c81SAndrey Smirnov          * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
1788fd1e5c81SAndrey Smirnov          * here becuase it will result in a call to
1789fd1e5c81SAndrey Smirnov          * sdhci_send_command(s) which we don't want.
1790fd1e5c81SAndrey Smirnov          *
1791fd1e5c81SAndrey Smirnov          */
1792fd1e5c81SAndrey Smirnov         s->trnmod = value & UINT16_MAX;
1793fd1e5c81SAndrey Smirnov         break;
1794fd1e5c81SAndrey Smirnov     case SDHC_TRNMOD:
1795fd1e5c81SAndrey Smirnov         /*
1796fd1e5c81SAndrey Smirnov          * Similar to above, but this time a write to "Command
1797fd1e5c81SAndrey Smirnov          * Register" will be translated into a 4-byte write to
1798fd1e5c81SAndrey Smirnov          * "Transfer Mode register" where lower 16-bit of value would
1799fd1e5c81SAndrey Smirnov          * be set to zero. So what we do is fill those bits with
1800fd1e5c81SAndrey Smirnov          * cached value from s->trnmod and let the SDHCI
1801fd1e5c81SAndrey Smirnov          * infrastructure handle the rest
1802fd1e5c81SAndrey Smirnov          */
1803fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val | s->trnmod, size);
1804fd1e5c81SAndrey Smirnov         break;
1805fd1e5c81SAndrey Smirnov     case SDHC_BLKSIZE:
1806fd1e5c81SAndrey Smirnov         /*
1807fd1e5c81SAndrey Smirnov          * ESDHCI does not implement "Host SDMA Buffer Boundary", and
1808fd1e5c81SAndrey Smirnov          * Linux driver will try to zero this field out which will
1809fd1e5c81SAndrey Smirnov          * break the rest of SDHCI emulation.
1810fd1e5c81SAndrey Smirnov          *
1811fd1e5c81SAndrey Smirnov          * Linux defaults to maximum possible setting (512K boundary)
1812fd1e5c81SAndrey Smirnov          * and it seems to be the only option that i.MX IP implements,
1813fd1e5c81SAndrey Smirnov          * so we artificially set it to that value.
1814fd1e5c81SAndrey Smirnov          */
1815fd1e5c81SAndrey Smirnov         val |= 0x7 << 12;
1816fd1e5c81SAndrey Smirnov         /* FALLTHROUGH */
1817fd1e5c81SAndrey Smirnov     default:
1818fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val, size);
1819fd1e5c81SAndrey Smirnov         break;
1820fd1e5c81SAndrey Smirnov     }
1821fd1e5c81SAndrey Smirnov }
1822fd1e5c81SAndrey Smirnov 
1823fd1e5c81SAndrey Smirnov 
1824fd1e5c81SAndrey Smirnov static const MemoryRegionOps usdhc_mmio_ops = {
1825fd1e5c81SAndrey Smirnov     .read = usdhc_read,
1826fd1e5c81SAndrey Smirnov     .write = usdhc_write,
1827fd1e5c81SAndrey Smirnov     .valid = {
1828fd1e5c81SAndrey Smirnov         .min_access_size = 1,
1829fd1e5c81SAndrey Smirnov         .max_access_size = 4,
1830fd1e5c81SAndrey Smirnov         .unaligned = false
1831fd1e5c81SAndrey Smirnov     },
1832fd1e5c81SAndrey Smirnov     .endianness = DEVICE_LITTLE_ENDIAN,
1833fd1e5c81SAndrey Smirnov };
1834fd1e5c81SAndrey Smirnov 
1835fd1e5c81SAndrey Smirnov static void imx_usdhc_init(Object *obj)
1836fd1e5c81SAndrey Smirnov {
1837fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(obj);
1838fd1e5c81SAndrey Smirnov 
1839fd1e5c81SAndrey Smirnov     s->io_ops = &usdhc_mmio_ops;
1840fd1e5c81SAndrey Smirnov     s->quirks = SDHCI_QUIRK_NO_BUSY_IRQ;
1841fd1e5c81SAndrey Smirnov }
1842fd1e5c81SAndrey Smirnov 
1843fd1e5c81SAndrey Smirnov static const TypeInfo imx_usdhc_info = {
1844fd1e5c81SAndrey Smirnov     .name = TYPE_IMX_USDHC,
1845fd1e5c81SAndrey Smirnov     .parent = TYPE_SYSBUS_SDHCI,
1846fd1e5c81SAndrey Smirnov     .instance_init = imx_usdhc_init,
1847fd1e5c81SAndrey Smirnov };
1848fd1e5c81SAndrey Smirnov 
184949ab747fSPaolo Bonzini static void sdhci_register_types(void)
185049ab747fSPaolo Bonzini {
1851224d10ffSKevin O'Connor     type_register_static(&sdhci_pci_info);
18527302dcd6SKevin O'Connor     type_register_static(&sdhci_sysbus_info);
185340bbc194SPeter Maydell     type_register_static(&sdhci_bus_info);
1854fd1e5c81SAndrey Smirnov     type_register_static(&imx_usdhc_info);
185549ab747fSPaolo Bonzini }
185649ab747fSPaolo Bonzini 
185749ab747fSPaolo Bonzini type_init(sdhci_register_types)
1858