xref: /openbmc/qemu/hw/sd/sdhci.c (revision 806ab537ac4705bcf0c577382f0e3f90c6edcd14)
149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini  * SD Association Host Standard Specification v2.0 controller emulation
349ab747fSPaolo Bonzini  *
4598a40b3SPhilippe Mathieu-Daudé  * Datasheet: PartA2_SD_Host_Controller_Simplified_Specification_Ver2.00.pdf
5598a40b3SPhilippe Mathieu-Daudé  *
649ab747fSPaolo Bonzini  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
749ab747fSPaolo Bonzini  * Mitsyanko Igor <i.mitsyanko@samsung.com>
849ab747fSPaolo Bonzini  * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com>
949ab747fSPaolo Bonzini  *
1049ab747fSPaolo Bonzini  * Based on MMC controller for Samsung S5PC1xx-based board emulation
1149ab747fSPaolo Bonzini  * by Alexey Merkulov and Vladimir Monakhov.
1249ab747fSPaolo Bonzini  *
1349ab747fSPaolo Bonzini  * This program is free software; you can redistribute it and/or modify it
1449ab747fSPaolo Bonzini  * under the terms of the GNU General Public License as published by the
1549ab747fSPaolo Bonzini  * Free Software Foundation; either version 2 of the License, or (at your
1649ab747fSPaolo Bonzini  * option) any later version.
1749ab747fSPaolo Bonzini  *
1849ab747fSPaolo Bonzini  * This program is distributed in the hope that it will be useful,
1949ab747fSPaolo Bonzini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2049ab747fSPaolo Bonzini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
2149ab747fSPaolo Bonzini  * See the GNU General Public License for more details.
2249ab747fSPaolo Bonzini  *
2349ab747fSPaolo Bonzini  * You should have received a copy of the GNU General Public License along
2449ab747fSPaolo Bonzini  * with this program; if not, see <http://www.gnu.org/licenses/>.
2549ab747fSPaolo Bonzini  */
2649ab747fSPaolo Bonzini 
270430891cSPeter Maydell #include "qemu/osdep.h"
284c8f9735SPhilippe Mathieu-Daudé #include "qemu/units.h"
296ff37c3dSPhilippe Mathieu-Daudé #include "qemu/error-report.h"
30b635d98cSPhilippe Mathieu-Daudé #include "qapi/error.h"
3164552b6bSMarkus Armbruster #include "hw/irq.h"
32a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
3349ab747fSPaolo Bonzini #include "sysemu/dma.h"
3449ab747fSPaolo Bonzini #include "qemu/timer.h"
3549ab747fSPaolo Bonzini #include "qemu/bitops.h"
36f82a0f44SPhilippe Mathieu-Daudé #include "hw/sd/sdhci.h"
37d6454270SMarkus Armbruster #include "migration/vmstate.h"
38637d23beSSai Pavan Boddu #include "sdhci-internal.h"
3903dd024fSPaolo Bonzini #include "qemu/log.h"
408be487d8SPhilippe Mathieu-Daudé #include "trace.h"
41db1015e9SEduardo Habkost #include "qom/object.h"
4249ab747fSPaolo Bonzini 
4340bbc194SPeter Maydell #define TYPE_SDHCI_BUS "sdhci-bus"
44fa34a3c5SEduardo Habkost /* This is reusing the SDBus typedef from SD_BUS */
DECLARE_INSTANCE_CHECKER(SDBus,SDHCI_BUS,TYPE_SDHCI_BUS)45fa34a3c5SEduardo Habkost DECLARE_INSTANCE_CHECKER(SDBus, SDHCI_BUS,
46fa34a3c5SEduardo Habkost                          TYPE_SDHCI_BUS)
4740bbc194SPeter Maydell 
48aa164fbfSPhilippe Mathieu-Daudé #define MASKED_WRITE(reg, mask, val)  (reg = (reg & (mask)) | (val))
49aa164fbfSPhilippe Mathieu-Daudé 
5009b738ffSPhilippe Mathieu-Daudé static inline unsigned int sdhci_get_fifolen(SDHCIState *s)
5109b738ffSPhilippe Mathieu-Daudé {
5209b738ffSPhilippe Mathieu-Daudé     return 1 << (9 + FIELD_EX32(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH));
5309b738ffSPhilippe Mathieu-Daudé }
5409b738ffSPhilippe Mathieu-Daudé 
556ff37c3dSPhilippe Mathieu-Daudé /* return true on error */
sdhci_check_capab_freq_range(SDHCIState * s,const char * desc,uint8_t freq,Error ** errp)566ff37c3dSPhilippe Mathieu-Daudé static bool sdhci_check_capab_freq_range(SDHCIState *s, const char *desc,
576ff37c3dSPhilippe Mathieu-Daudé                                          uint8_t freq, Error **errp)
586ff37c3dSPhilippe Mathieu-Daudé {
594d67852dSPhilippe Mathieu-Daudé     if (s->sd_spec_version >= 3) {
604d67852dSPhilippe Mathieu-Daudé         return false;
614d67852dSPhilippe Mathieu-Daudé     }
626ff37c3dSPhilippe Mathieu-Daudé     switch (freq) {
636ff37c3dSPhilippe Mathieu-Daudé     case 0:
646ff37c3dSPhilippe Mathieu-Daudé     case 10 ... 63:
656ff37c3dSPhilippe Mathieu-Daudé         break;
666ff37c3dSPhilippe Mathieu-Daudé     default:
676ff37c3dSPhilippe Mathieu-Daudé         error_setg(errp, "SD %s clock frequency can have value"
686ff37c3dSPhilippe Mathieu-Daudé                    "in range 0-63 only", desc);
696ff37c3dSPhilippe Mathieu-Daudé         return true;
706ff37c3dSPhilippe Mathieu-Daudé     }
716ff37c3dSPhilippe Mathieu-Daudé     return false;
726ff37c3dSPhilippe Mathieu-Daudé }
736ff37c3dSPhilippe Mathieu-Daudé 
sdhci_check_capareg(SDHCIState * s,Error ** errp)746ff37c3dSPhilippe Mathieu-Daudé static void sdhci_check_capareg(SDHCIState *s, Error **errp)
756ff37c3dSPhilippe Mathieu-Daudé {
766ff37c3dSPhilippe Mathieu-Daudé     uint64_t msk = s->capareg;
776ff37c3dSPhilippe Mathieu-Daudé     uint32_t val;
786ff37c3dSPhilippe Mathieu-Daudé     bool y;
796ff37c3dSPhilippe Mathieu-Daudé 
806ff37c3dSPhilippe Mathieu-Daudé     switch (s->sd_spec_version) {
811e23b63fSPhilippe Mathieu-Daudé     case 4:
821e23b63fSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT_V4);
831e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("64-bit system bus (v4)", val);
841e23b63fSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT_V4, 0);
851e23b63fSPhilippe Mathieu-Daudé 
861e23b63fSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, UHS_II);
871e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("UHS-II", val);
881e23b63fSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, UHS_II, 0);
891e23b63fSPhilippe Mathieu-Daudé 
901e23b63fSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA3);
911e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA3", val);
921e23b63fSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA3, 0);
931e23b63fSPhilippe Mathieu-Daudé 
941e23b63fSPhilippe Mathieu-Daudé     /* fallthrough */
954d67852dSPhilippe Mathieu-Daudé     case 3:
964d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ASYNC_INT);
974d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("async interrupt", val);
984d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ASYNC_INT, 0);
994d67852dSPhilippe Mathieu-Daudé 
1004d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SLOT_TYPE);
1014d67852dSPhilippe Mathieu-Daudé         if (val) {
1024d67852dSPhilippe Mathieu-Daudé             error_setg(errp, "slot-type not supported");
1034d67852dSPhilippe Mathieu-Daudé             return;
1044d67852dSPhilippe Mathieu-Daudé         }
1054d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("slot type", val);
1064d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SLOT_TYPE, 0);
1074d67852dSPhilippe Mathieu-Daudé 
1084d67852dSPhilippe Mathieu-Daudé         if (val != 2) {
1094d67852dSPhilippe Mathieu-Daudé             val = FIELD_EX64(s->capareg, SDHC_CAPAB, EMBEDDED_8BIT);
1104d67852dSPhilippe Mathieu-Daudé             trace_sdhci_capareg("8-bit bus", val);
1114d67852dSPhilippe Mathieu-Daudé         }
1124d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, EMBEDDED_8BIT, 0);
1134d67852dSPhilippe Mathieu-Daudé 
1144d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS_SPEED);
1154d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("bus speed mask", val);
1164d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS_SPEED, 0);
1174d67852dSPhilippe Mathieu-Daudé 
1184d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, DRIVER_STRENGTH);
1194d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("driver strength mask", val);
1204d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, DRIVER_STRENGTH, 0);
1214d67852dSPhilippe Mathieu-Daudé 
1224d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, TIMER_RETUNING);
1234d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("timer re-tuning", val);
1244d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TIMER_RETUNING, 0);
1254d67852dSPhilippe Mathieu-Daudé 
1264d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDR50_TUNING);
1274d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("use SDR50 tuning", val);
1284d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SDR50_TUNING, 0);
1294d67852dSPhilippe Mathieu-Daudé 
1304d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, RETUNING_MODE);
1314d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("re-tuning mode", val);
1324d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, RETUNING_MODE, 0);
1334d67852dSPhilippe Mathieu-Daudé 
1344d67852dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, CLOCK_MULT);
1354d67852dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("clock multiplier", val);
1364d67852dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, CLOCK_MULT, 0);
1374d67852dSPhilippe Mathieu-Daudé 
1384d67852dSPhilippe Mathieu-Daudé     /* fallthrough */
1396ff37c3dSPhilippe Mathieu-Daudé     case 2: /* default version */
1400540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA2);
1410540fba9SPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA2", val);
1420540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA2, 0);
1430540fba9SPhilippe Mathieu-Daudé 
1440540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, ADMA1);
1450540fba9SPhilippe Mathieu-Daudé         trace_sdhci_capareg("ADMA1", val);
1460540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, ADMA1, 0);
1470540fba9SPhilippe Mathieu-Daudé 
1480540fba9SPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BUS64BIT);
1491e23b63fSPhilippe Mathieu-Daudé         trace_sdhci_capareg("64-bit system bus (v3)", val);
1500540fba9SPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BUS64BIT, 0);
1516ff37c3dSPhilippe Mathieu-Daudé 
1526ff37c3dSPhilippe Mathieu-Daudé     /* fallthrough */
1536ff37c3dSPhilippe Mathieu-Daudé     case 1:
1546ff37c3dSPhilippe Mathieu-Daudé         y = FIELD_EX64(s->capareg, SDHC_CAPAB, TOUNIT);
1556ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TOUNIT, 0);
1566ff37c3dSPhilippe Mathieu-Daudé 
1576ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, TOCLKFREQ);
1586ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg(y ? "timeout (MHz)" : "Timeout (KHz)", val);
1596ff37c3dSPhilippe Mathieu-Daudé         if (sdhci_check_capab_freq_range(s, "timeout", val, errp)) {
1606ff37c3dSPhilippe Mathieu-Daudé             return;
1616ff37c3dSPhilippe Mathieu-Daudé         }
1626ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, TOCLKFREQ, 0);
1636ff37c3dSPhilippe Mathieu-Daudé 
1646ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, BASECLKFREQ);
1656ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg(y ? "base (MHz)" : "Base (KHz)", val);
1666ff37c3dSPhilippe Mathieu-Daudé         if (sdhci_check_capab_freq_range(s, "base", val, errp)) {
1676ff37c3dSPhilippe Mathieu-Daudé             return;
1686ff37c3dSPhilippe Mathieu-Daudé         }
1696ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, BASECLKFREQ, 0);
1706ff37c3dSPhilippe Mathieu-Daudé 
1716ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, MAXBLOCKLENGTH);
1726ff37c3dSPhilippe Mathieu-Daudé         if (val >= 3) {
1736ff37c3dSPhilippe Mathieu-Daudé             error_setg(errp, "block size can be 512, 1024 or 2048 only");
1746ff37c3dSPhilippe Mathieu-Daudé             return;
1756ff37c3dSPhilippe Mathieu-Daudé         }
1766ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("max block length", sdhci_get_fifolen(s));
1776ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, MAXBLOCKLENGTH, 0);
1786ff37c3dSPhilippe Mathieu-Daudé 
1796ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, HIGHSPEED);
1806ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("high speed", val);
1816ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, HIGHSPEED, 0);
1826ff37c3dSPhilippe Mathieu-Daudé 
1836ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SDMA);
1846ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("SDMA", val);
1856ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SDMA, 0);
1866ff37c3dSPhilippe Mathieu-Daudé 
1876ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, SUSPRESUME);
1886ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("suspend/resume", val);
1896ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, SUSPRESUME, 0);
1906ff37c3dSPhilippe Mathieu-Daudé 
1916ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V33);
1926ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("3.3v", val);
1936ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V33, 0);
1946ff37c3dSPhilippe Mathieu-Daudé 
1956ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V30);
1966ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("3.0v", val);
1976ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V30, 0);
1986ff37c3dSPhilippe Mathieu-Daudé 
1996ff37c3dSPhilippe Mathieu-Daudé         val = FIELD_EX64(s->capareg, SDHC_CAPAB, V18);
2006ff37c3dSPhilippe Mathieu-Daudé         trace_sdhci_capareg("1.8v", val);
2016ff37c3dSPhilippe Mathieu-Daudé         msk = FIELD_DP64(msk, SDHC_CAPAB, V18, 0);
2026ff37c3dSPhilippe Mathieu-Daudé         break;
2036ff37c3dSPhilippe Mathieu-Daudé 
2046ff37c3dSPhilippe Mathieu-Daudé     default:
2056ff37c3dSPhilippe Mathieu-Daudé         error_setg(errp, "Unsupported spec version: %u", s->sd_spec_version);
2066ff37c3dSPhilippe Mathieu-Daudé     }
2076ff37c3dSPhilippe Mathieu-Daudé     if (msk) {
2086ff37c3dSPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP,
2096ff37c3dSPhilippe Mathieu-Daudé                       "SDHCI: unknown CAPAB mask: 0x%016" PRIx64 "\n", msk);
2106ff37c3dSPhilippe Mathieu-Daudé     }
2116ff37c3dSPhilippe Mathieu-Daudé }
2126ff37c3dSPhilippe Mathieu-Daudé 
sdhci_slotint(SDHCIState * s)21349ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s)
21449ab747fSPaolo Bonzini {
21549ab747fSPaolo Bonzini     return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) ||
21649ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) ||
21749ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV));
21849ab747fSPaolo Bonzini }
21949ab747fSPaolo Bonzini 
2202bd9ae7eSPhilippe Mathieu-Daudé /* Return true if IRQ was pending and delivered */
sdhci_update_irq(SDHCIState * s)2212bd9ae7eSPhilippe Mathieu-Daudé static bool sdhci_update_irq(SDHCIState *s)
22249ab747fSPaolo Bonzini {
2232bd9ae7eSPhilippe Mathieu-Daudé     bool pending = sdhci_slotint(s);
2242bd9ae7eSPhilippe Mathieu-Daudé 
2252bd9ae7eSPhilippe Mathieu-Daudé     qemu_set_irq(s->irq, pending);
2262bd9ae7eSPhilippe Mathieu-Daudé 
2272bd9ae7eSPhilippe Mathieu-Daudé     return pending;
22849ab747fSPaolo Bonzini }
22949ab747fSPaolo Bonzini 
sdhci_raise_insertion_irq(void * opaque)23049ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque)
23149ab747fSPaolo Bonzini {
23249ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
23349ab747fSPaolo Bonzini 
23449ab747fSPaolo Bonzini     if (s->norintsts & SDHC_NIS_REMOVE) {
235bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
236bc72ad67SAlex Bligh                 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
23749ab747fSPaolo Bonzini     } else {
23849ab747fSPaolo Bonzini         s->prnsts = 0x1ff0000;
23949ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_INSERT) {
24049ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_INSERT;
24149ab747fSPaolo Bonzini         }
24249ab747fSPaolo Bonzini         sdhci_update_irq(s);
24349ab747fSPaolo Bonzini     }
24449ab747fSPaolo Bonzini }
24549ab747fSPaolo Bonzini 
sdhci_set_inserted(DeviceState * dev,bool level)24640bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level)
24749ab747fSPaolo Bonzini {
24840bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
24949ab747fSPaolo Bonzini 
2508be487d8SPhilippe Mathieu-Daudé     trace_sdhci_set_inserted(level ? "insert" : "eject");
25149ab747fSPaolo Bonzini     if ((s->norintsts & SDHC_NIS_REMOVE) && level) {
25249ab747fSPaolo Bonzini         /* Give target some time to notice card ejection */
253bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
254bc72ad67SAlex Bligh                 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
25549ab747fSPaolo Bonzini     } else {
25649ab747fSPaolo Bonzini         if (level) {
25749ab747fSPaolo Bonzini             s->prnsts = 0x1ff0000;
25849ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_INSERT) {
25949ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_INSERT;
26049ab747fSPaolo Bonzini             }
26149ab747fSPaolo Bonzini         } else {
26249ab747fSPaolo Bonzini             s->prnsts = 0x1fa0000;
26349ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
26449ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_SDCLK_EN;
26549ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_REMOVE) {
26649ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_REMOVE;
26749ab747fSPaolo Bonzini             }
26849ab747fSPaolo Bonzini         }
26949ab747fSPaolo Bonzini         sdhci_update_irq(s);
27049ab747fSPaolo Bonzini     }
27149ab747fSPaolo Bonzini }
27249ab747fSPaolo Bonzini 
sdhci_set_readonly(DeviceState * dev,bool level)27340bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level)
27449ab747fSPaolo Bonzini {
27540bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
27649ab747fSPaolo Bonzini 
277*806ab537SJamin Lin     if (s->wp_inverted) {
278*806ab537SJamin Lin         level = !level;
279*806ab537SJamin Lin     }
280*806ab537SJamin Lin 
28149ab747fSPaolo Bonzini     if (level) {
28249ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_WRITE_PROTECT;
28349ab747fSPaolo Bonzini     } else {
28449ab747fSPaolo Bonzini         /* Write enabled */
28549ab747fSPaolo Bonzini         s->prnsts |= SDHC_WRITE_PROTECT;
28649ab747fSPaolo Bonzini     }
28749ab747fSPaolo Bonzini }
28849ab747fSPaolo Bonzini 
sdhci_reset(SDHCIState * s)28949ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s)
29049ab747fSPaolo Bonzini {
29140bbc194SPeter Maydell     DeviceState *dev = DEVICE(s);
29240bbc194SPeter Maydell 
293bc72ad67SAlex Bligh     timer_del(s->insert_timer);
294bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
295aceb5b06SPhilippe Mathieu-Daudé 
2962df42919SJamin Lin     /*
2972df42919SJamin Lin      * Set all registers to 0. Capabilities/Version registers are not cleared
29849ab747fSPaolo Bonzini      * and assumed to always preserve their value, given to them during
2992df42919SJamin Lin      * initialization
3002df42919SJamin Lin      */
30149ab747fSPaolo Bonzini     memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad);
30249ab747fSPaolo Bonzini 
30340bbc194SPeter Maydell     /* Reset other state based on current card insertion/readonly status */
30440bbc194SPeter Maydell     sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus));
30540bbc194SPeter Maydell     sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus));
30640bbc194SPeter Maydell 
30749ab747fSPaolo Bonzini     s->data_count = 0;
30849ab747fSPaolo Bonzini     s->stopped_state = sdhc_not_stopped;
3090a7ac9f9SAndrew Baumann     s->pending_insert_state = false;
31049ab747fSPaolo Bonzini }
31149ab747fSPaolo Bonzini 
sdhci_poweron_reset(DeviceState * dev)3128b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev)
3138b41c305SPeter Maydell {
3142df42919SJamin Lin     /*
3152df42919SJamin Lin      * QOM (ie power-on) reset. This is identical to reset
3168b41c305SPeter Maydell      * commanded via device register apart from handling of the
3178b41c305SPeter Maydell      * 'pending insert on powerup' quirk.
3188b41c305SPeter Maydell      */
3198b41c305SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
3208b41c305SPeter Maydell 
3218b41c305SPeter Maydell     sdhci_reset(s);
3228b41c305SPeter Maydell 
3238b41c305SPeter Maydell     if (s->pending_insert_quirk) {
3248b41c305SPeter Maydell         s->pending_insert_state = true;
3258b41c305SPeter Maydell     }
3268b41c305SPeter Maydell }
3278b41c305SPeter Maydell 
328d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque);
32949ab747fSPaolo Bonzini 
330946df4d5SLu Gao #define BLOCK_SIZE_MASK (4 * KiB - 1)
331946df4d5SLu Gao 
sdhci_send_command(SDHCIState * s)33249ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s)
33349ab747fSPaolo Bonzini {
33449ab747fSPaolo Bonzini     SDRequest request;
33549ab747fSPaolo Bonzini     uint8_t response[16];
33649ab747fSPaolo Bonzini     int rlen;
337b263d8f9SBin Meng     bool timeout = false;
33849ab747fSPaolo Bonzini 
33949ab747fSPaolo Bonzini     s->errintsts = 0;
34049ab747fSPaolo Bonzini     s->acmd12errsts = 0;
34149ab747fSPaolo Bonzini     request.cmd = s->cmdreg >> 8;
34249ab747fSPaolo Bonzini     request.arg = s->argument;
3438be487d8SPhilippe Mathieu-Daudé 
3448be487d8SPhilippe Mathieu-Daudé     trace_sdhci_send_command(request.cmd, request.arg);
34540bbc194SPeter Maydell     rlen = sdbus_do_command(&s->sdbus, &request, response);
34649ab747fSPaolo Bonzini 
34749ab747fSPaolo Bonzini     if (s->cmdreg & SDHC_CMD_RESPONSE) {
34849ab747fSPaolo Bonzini         if (rlen == 4) {
349b3141c06SPhilippe Mathieu-Daudé             s->rspreg[0] = ldl_be_p(response);
35049ab747fSPaolo Bonzini             s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0;
3518be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response4(s->rspreg[0]);
35249ab747fSPaolo Bonzini         } else if (rlen == 16) {
353b3141c06SPhilippe Mathieu-Daudé             s->rspreg[0] = ldl_be_p(&response[11]);
354b3141c06SPhilippe Mathieu-Daudé             s->rspreg[1] = ldl_be_p(&response[7]);
355b3141c06SPhilippe Mathieu-Daudé             s->rspreg[2] = ldl_be_p(&response[3]);
35649ab747fSPaolo Bonzini             s->rspreg[3] = (response[0] << 16) | (response[1] << 8) |
35749ab747fSPaolo Bonzini                             response[2];
3588be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response16(s->rspreg[3], s->rspreg[2],
3598be487d8SPhilippe Mathieu-Daudé                                    s->rspreg[1], s->rspreg[0]);
36049ab747fSPaolo Bonzini         } else {
361b263d8f9SBin Meng             timeout = true;
3628be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("timeout waiting for command response");
36349ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) {
36449ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_CMDTIMEOUT;
36549ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
36649ab747fSPaolo Bonzini             }
36749ab747fSPaolo Bonzini         }
36849ab747fSPaolo Bonzini 
369fd1e5c81SAndrey Smirnov         if (!(s->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
370fd1e5c81SAndrey Smirnov             (s->norintstsen & SDHC_NISEN_TRSCMP) &&
37149ab747fSPaolo Bonzini             (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) {
37249ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_TRSCMP;
37349ab747fSPaolo Bonzini         }
37449ab747fSPaolo Bonzini     }
37549ab747fSPaolo Bonzini 
37649ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_CMDCMP) {
37749ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_CMDCMP;
37849ab747fSPaolo Bonzini     }
37949ab747fSPaolo Bonzini 
38049ab747fSPaolo Bonzini     sdhci_update_irq(s);
38149ab747fSPaolo Bonzini 
382946df4d5SLu Gao     if (!timeout && (s->blksize & BLOCK_SIZE_MASK) &&
383946df4d5SLu Gao         (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 
sdhci_end_transfer(SDHCIState * s)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 */
401b3141c06SPhilippe Mathieu-Daudé         s->rspreg[3] = ldl_be_p(response);
40249ab747fSPaolo Bonzini     }
40349ab747fSPaolo Bonzini 
40449ab747fSPaolo Bonzini     s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE |
40549ab747fSPaolo Bonzini             SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT |
40649ab747fSPaolo Bonzini             SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE);
40749ab747fSPaolo Bonzini 
40849ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_TRSCMP) {
40949ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_TRSCMP;
41049ab747fSPaolo Bonzini     }
41149ab747fSPaolo Bonzini 
41249ab747fSPaolo Bonzini     sdhci_update_irq(s);
41349ab747fSPaolo Bonzini }
41449ab747fSPaolo Bonzini 
41549ab747fSPaolo Bonzini /*
41649ab747fSPaolo Bonzini  * Programmed i/o data transfer
41749ab747fSPaolo Bonzini  */
41849ab747fSPaolo Bonzini 
41949ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
sdhci_read_block_from_card(SDHCIState * s)42049ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s)
42149ab747fSPaolo Bonzini {
422ea55a221SPhilippe Mathieu-Daudé     const uint16_t blk_size = s->blksize & BLOCK_SIZE_MASK;
42349ab747fSPaolo Bonzini 
42449ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) &&
42549ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) {
42649ab747fSPaolo Bonzini         return;
42749ab747fSPaolo Bonzini     }
42849ab747fSPaolo Bonzini 
429ea55a221SPhilippe Mathieu-Daudé     if (!FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) {
43008022a91SPhilippe Mathieu-Daudé         /* Device is not in tuning */
431618e0be1SPhilippe Mathieu-Daudé         sdbus_read_data(&s->sdbus, s->fifo_buffer, blk_size);
432ea55a221SPhilippe Mathieu-Daudé     }
433ea55a221SPhilippe Mathieu-Daudé 
434ea55a221SPhilippe Mathieu-Daudé     if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, EXECUTE_TUNING)) {
43508022a91SPhilippe Mathieu-Daudé         /* Device is in tuning */
436ea55a221SPhilippe Mathieu-Daudé         s->hostctl2 &= ~R_SDHC_HOSTCTL2_EXECUTE_TUNING_MASK;
437ea55a221SPhilippe Mathieu-Daudé         s->hostctl2 |= R_SDHC_HOSTCTL2_SAMPLING_CLKSEL_MASK;
438ea55a221SPhilippe Mathieu-Daudé         s->prnsts &= ~(SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ |
439ea55a221SPhilippe Mathieu-Daudé                        SDHC_DATA_INHIBIT);
440ea55a221SPhilippe Mathieu-Daudé         goto read_done;
44149ab747fSPaolo Bonzini     }
44249ab747fSPaolo Bonzini 
44349ab747fSPaolo Bonzini     /* New data now available for READ through Buffer Port Register */
44449ab747fSPaolo Bonzini     s->prnsts |= SDHC_DATA_AVAILABLE;
44549ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_RBUFRDY) {
44649ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_RBUFRDY;
44749ab747fSPaolo Bonzini     }
44849ab747fSPaolo Bonzini 
44949ab747fSPaolo Bonzini     /* Clear DAT line active status if that was the last block */
45049ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
45149ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) {
45249ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
45349ab747fSPaolo Bonzini     }
45449ab747fSPaolo Bonzini 
4552df42919SJamin Lin     /*
4562df42919SJamin Lin      * If stop at block gap request was set and it's not the last block of
4572df42919SJamin Lin      * data - generate Block Event interrupt
4582df42919SJamin Lin      */
45949ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) &&
46049ab747fSPaolo Bonzini             s->blkcnt != 1)    {
46149ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
46249ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
46349ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
46449ab747fSPaolo Bonzini         }
46549ab747fSPaolo Bonzini     }
46649ab747fSPaolo Bonzini 
467ea55a221SPhilippe Mathieu-Daudé read_done:
46849ab747fSPaolo Bonzini     sdhci_update_irq(s);
46949ab747fSPaolo Bonzini }
47049ab747fSPaolo Bonzini 
47149ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
sdhci_read_dataport(SDHCIState * s,unsigned size)47249ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size)
47349ab747fSPaolo Bonzini {
47449ab747fSPaolo Bonzini     uint32_t value = 0;
47549ab747fSPaolo Bonzini     int i;
47649ab747fSPaolo Bonzini 
47749ab747fSPaolo Bonzini     /* first check that a valid data exists in host controller input buffer */
47849ab747fSPaolo Bonzini     if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) {
4798be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("read from empty buffer");
48049ab747fSPaolo Bonzini         return 0;
48149ab747fSPaolo Bonzini     }
48249ab747fSPaolo Bonzini 
48349ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
4849e4b27caSPhilippe Mathieu-Daudé         assert(s->data_count < s->buf_maxsz);
48549ab747fSPaolo Bonzini         value |= s->fifo_buffer[s->data_count] << i * 8;
48649ab747fSPaolo Bonzini         s->data_count++;
48749ab747fSPaolo Bonzini         /* check if we've read all valid data (blksize bytes) from buffer */
488bf8ec38eSPhilippe Mathieu-Daudé         if ((s->data_count) >= (s->blksize & BLOCK_SIZE_MASK)) {
4898be487d8SPhilippe Mathieu-Daudé             trace_sdhci_read_dataport(s->data_count);
49049ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */
49149ab747fSPaolo Bonzini             s->data_count = 0;  /* next buff read must start at position [0] */
49249ab747fSPaolo Bonzini 
49349ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
49449ab747fSPaolo Bonzini                 s->blkcnt--;
49549ab747fSPaolo Bonzini             }
49649ab747fSPaolo Bonzini 
49749ab747fSPaolo Bonzini             /* if that was the last block of data */
49849ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
49949ab747fSPaolo Bonzini                 ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) ||
50049ab747fSPaolo Bonzini                  /* stop at gap request */
50149ab747fSPaolo Bonzini                 (s->stopped_state == sdhc_gap_read &&
50249ab747fSPaolo Bonzini                  !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) {
503d368ba43SKevin O'Connor                 sdhci_end_transfer(s);
50449ab747fSPaolo Bonzini             } else { /* if there are more data, read next block from card */
505d368ba43SKevin O'Connor                 sdhci_read_block_from_card(s);
50649ab747fSPaolo Bonzini             }
50749ab747fSPaolo Bonzini             break;
50849ab747fSPaolo Bonzini         }
50949ab747fSPaolo Bonzini     }
51049ab747fSPaolo Bonzini 
51149ab747fSPaolo Bonzini     return value;
51249ab747fSPaolo Bonzini }
51349ab747fSPaolo Bonzini 
51449ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */
sdhci_write_block_to_card(SDHCIState * s)51549ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s)
51649ab747fSPaolo Bonzini {
51749ab747fSPaolo Bonzini     if (s->prnsts & SDHC_SPACE_AVAILABLE) {
51849ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
51949ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_WBUFRDY;
52049ab747fSPaolo Bonzini         }
52149ab747fSPaolo Bonzini         sdhci_update_irq(s);
52249ab747fSPaolo Bonzini         return;
52349ab747fSPaolo Bonzini     }
52449ab747fSPaolo Bonzini 
52549ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
52649ab747fSPaolo Bonzini         if (s->blkcnt == 0) {
52749ab747fSPaolo Bonzini             return;
52849ab747fSPaolo Bonzini         } else {
52949ab747fSPaolo Bonzini             s->blkcnt--;
53049ab747fSPaolo Bonzini         }
53149ab747fSPaolo Bonzini     }
53249ab747fSPaolo Bonzini 
53362a21be6SPhilippe Mathieu-Daudé     sdbus_write_data(&s->sdbus, s->fifo_buffer, s->blksize & BLOCK_SIZE_MASK);
53449ab747fSPaolo Bonzini 
53549ab747fSPaolo Bonzini     /* Next data can be written through BUFFER DATORT register */
53649ab747fSPaolo Bonzini     s->prnsts |= SDHC_SPACE_AVAILABLE;
53749ab747fSPaolo Bonzini 
53849ab747fSPaolo Bonzini     /* Finish transfer if that was the last block of data */
53949ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
54049ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) &&
54149ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) {
542d368ba43SKevin O'Connor         sdhci_end_transfer(s);
543dcdb4cd8SPeter Crosthwaite     } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
544dcdb4cd8SPeter Crosthwaite         s->norintsts |= SDHC_NIS_WBUFRDY;
54549ab747fSPaolo Bonzini     }
54649ab747fSPaolo Bonzini 
54749ab747fSPaolo Bonzini     /* Generate Block Gap Event if requested and if not the last block */
54849ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) &&
54949ab747fSPaolo Bonzini             s->blkcnt > 0) {
55049ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DOING_WRITE;
55149ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
55249ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
55349ab747fSPaolo Bonzini         }
554d368ba43SKevin O'Connor         sdhci_end_transfer(s);
55549ab747fSPaolo Bonzini     }
55649ab747fSPaolo Bonzini 
55749ab747fSPaolo Bonzini     sdhci_update_irq(s);
55849ab747fSPaolo Bonzini }
55949ab747fSPaolo Bonzini 
5602df42919SJamin Lin /*
5612df42919SJamin Lin  * Write @size bytes of @value data to host controller @s Buffer Data Port
5622df42919SJamin Lin  * register
5632df42919SJamin Lin  */
sdhci_write_dataport(SDHCIState * s,uint32_t value,unsigned size)56449ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size)
56549ab747fSPaolo Bonzini {
56649ab747fSPaolo Bonzini     unsigned i;
56749ab747fSPaolo Bonzini 
56849ab747fSPaolo Bonzini     /* Check that there is free space left in a buffer */
56949ab747fSPaolo Bonzini     if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) {
5708be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("Can't write to data buffer: buffer full");
57149ab747fSPaolo Bonzini         return;
57249ab747fSPaolo Bonzini     }
57349ab747fSPaolo Bonzini 
57449ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
5759e4b27caSPhilippe Mathieu-Daudé         assert(s->data_count < s->buf_maxsz);
57649ab747fSPaolo Bonzini         s->fifo_buffer[s->data_count] = value & 0xFF;
57749ab747fSPaolo Bonzini         s->data_count++;
57849ab747fSPaolo Bonzini         value >>= 8;
579bf8ec38eSPhilippe Mathieu-Daudé         if (s->data_count >= (s->blksize & BLOCK_SIZE_MASK)) {
5808be487d8SPhilippe Mathieu-Daudé             trace_sdhci_write_dataport(s->data_count);
58149ab747fSPaolo Bonzini             s->data_count = 0;
58249ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_SPACE_AVAILABLE;
58349ab747fSPaolo Bonzini             if (s->prnsts & SDHC_DOING_WRITE) {
584d368ba43SKevin O'Connor                 sdhci_write_block_to_card(s);
58549ab747fSPaolo Bonzini             }
58649ab747fSPaolo Bonzini         }
58749ab747fSPaolo Bonzini     }
58849ab747fSPaolo Bonzini }
58949ab747fSPaolo Bonzini 
59049ab747fSPaolo Bonzini /*
59149ab747fSPaolo Bonzini  * Single DMA data transfer
59249ab747fSPaolo Bonzini  */
59349ab747fSPaolo Bonzini 
59449ab747fSPaolo Bonzini /* Multi block SDMA transfer */
sdhci_sdma_transfer_multi_blocks(SDHCIState * s)59549ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
59649ab747fSPaolo Bonzini {
59749ab747fSPaolo Bonzini     bool page_aligned = false;
598618e0be1SPhilippe Mathieu-Daudé     unsigned int begin;
599bf8ec38eSPhilippe Mathieu-Daudé     const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK;
600bf8ec38eSPhilippe Mathieu-Daudé     uint32_t boundary_chk = 1 << (((s->blksize & ~BLOCK_SIZE_MASK) >> 12) + 12);
60149ab747fSPaolo Bonzini     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
60249ab747fSPaolo Bonzini 
6036e86d903SPrasad J Pandit     if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) {
6046e86d903SPrasad J Pandit         qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n");
6056e86d903SPrasad J Pandit         return;
6066e86d903SPrasad J Pandit     }
6076e86d903SPrasad J Pandit 
6082df42919SJamin Lin     /*
6092df42919SJamin Lin      * XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
61049ab747fSPaolo Bonzini      * possible stop at page boundary if initial address is not page aligned,
6112df42919SJamin Lin      * allow them to work properly
6122df42919SJamin Lin      */
61349ab747fSPaolo Bonzini     if ((s->sdmasysad % boundary_chk) == 0) {
61449ab747fSPaolo Bonzini         page_aligned = true;
61549ab747fSPaolo Bonzini     }
61649ab747fSPaolo Bonzini 
6178bc1f1aaSBin Meng     s->prnsts |= SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE;
61849ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
6198bc1f1aaSBin Meng         s->prnsts |= SDHC_DOING_READ;
62049ab747fSPaolo Bonzini         while (s->blkcnt) {
62149ab747fSPaolo Bonzini             if (s->data_count == 0) {
622618e0be1SPhilippe Mathieu-Daudé                 sdbus_read_data(&s->sdbus, s->fifo_buffer, block_size);
62349ab747fSPaolo Bonzini             }
62449ab747fSPaolo Bonzini             begin = s->data_count;
62549ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
62649ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
62749ab747fSPaolo Bonzini                 boundary_count = 0;
62849ab747fSPaolo Bonzini              } else {
62949ab747fSPaolo Bonzini                 s->data_count = block_size;
63049ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
63149ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
63249ab747fSPaolo Bonzini                     s->blkcnt--;
63349ab747fSPaolo Bonzini                 }
63449ab747fSPaolo Bonzini             }
635ba06fe8aSPhilippe Mathieu-Daudé             dma_memory_write(s->dma_as, s->sdmasysad, &s->fifo_buffer[begin],
636ba06fe8aSPhilippe Mathieu-Daudé                              s->data_count - begin, MEMTXATTRS_UNSPECIFIED);
63749ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
63849ab747fSPaolo Bonzini             if (s->data_count == block_size) {
63949ab747fSPaolo Bonzini                 s->data_count = 0;
64049ab747fSPaolo Bonzini             }
64149ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
64249ab747fSPaolo Bonzini                 break;
64349ab747fSPaolo Bonzini             }
64449ab747fSPaolo Bonzini         }
64549ab747fSPaolo Bonzini     } else {
6468bc1f1aaSBin Meng         s->prnsts |= SDHC_DOING_WRITE;
64749ab747fSPaolo Bonzini         while (s->blkcnt) {
64849ab747fSPaolo Bonzini             begin = s->data_count;
64949ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
65049ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
65149ab747fSPaolo Bonzini                 boundary_count = 0;
65249ab747fSPaolo Bonzini              } else {
65349ab747fSPaolo Bonzini                 s->data_count = block_size;
65449ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
65549ab747fSPaolo Bonzini             }
656ba06fe8aSPhilippe Mathieu-Daudé             dma_memory_read(s->dma_as, s->sdmasysad, &s->fifo_buffer[begin],
657ba06fe8aSPhilippe Mathieu-Daudé                             s->data_count - begin, MEMTXATTRS_UNSPECIFIED);
65849ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
65949ab747fSPaolo Bonzini             if (s->data_count == block_size) {
66062a21be6SPhilippe Mathieu-Daudé                 sdbus_write_data(&s->sdbus, s->fifo_buffer, block_size);
66149ab747fSPaolo Bonzini                 s->data_count = 0;
66249ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
66349ab747fSPaolo Bonzini                     s->blkcnt--;
66449ab747fSPaolo Bonzini                 }
66549ab747fSPaolo Bonzini             }
66649ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
66749ab747fSPaolo Bonzini                 break;
66849ab747fSPaolo Bonzini             }
66949ab747fSPaolo Bonzini         }
67049ab747fSPaolo Bonzini     }
67149ab747fSPaolo Bonzini 
67249ab747fSPaolo Bonzini     if (s->blkcnt == 0) {
673d368ba43SKevin O'Connor         sdhci_end_transfer(s);
67449ab747fSPaolo Bonzini     } else {
67549ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_DMA) {
67649ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_DMA;
67749ab747fSPaolo Bonzini         }
67849ab747fSPaolo Bonzini         sdhci_update_irq(s);
67949ab747fSPaolo Bonzini     }
68049ab747fSPaolo Bonzini }
68149ab747fSPaolo Bonzini 
68249ab747fSPaolo Bonzini /* single block SDMA transfer */
sdhci_sdma_transfer_single_block(SDHCIState * s)68349ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s)
68449ab747fSPaolo Bonzini {
685bf8ec38eSPhilippe Mathieu-Daudé     uint32_t datacnt = s->blksize & BLOCK_SIZE_MASK;
68649ab747fSPaolo Bonzini 
68749ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
688618e0be1SPhilippe Mathieu-Daudé         sdbus_read_data(&s->sdbus, s->fifo_buffer, datacnt);
689ba06fe8aSPhilippe Mathieu-Daudé         dma_memory_write(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt,
690ba06fe8aSPhilippe Mathieu-Daudé                          MEMTXATTRS_UNSPECIFIED);
69149ab747fSPaolo Bonzini     } else {
692ba06fe8aSPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt,
693ba06fe8aSPhilippe Mathieu-Daudé                         MEMTXATTRS_UNSPECIFIED);
69462a21be6SPhilippe Mathieu-Daudé         sdbus_write_data(&s->sdbus, s->fifo_buffer, datacnt);
69549ab747fSPaolo Bonzini     }
69649ab747fSPaolo Bonzini     s->blkcnt--;
69749ab747fSPaolo Bonzini 
698d368ba43SKevin O'Connor     sdhci_end_transfer(s);
69949ab747fSPaolo Bonzini }
70049ab747fSPaolo Bonzini 
70149ab747fSPaolo Bonzini typedef struct ADMADescr {
70249ab747fSPaolo Bonzini     hwaddr addr;
70349ab747fSPaolo Bonzini     uint16_t length;
70449ab747fSPaolo Bonzini     uint8_t attr;
70549ab747fSPaolo Bonzini     uint8_t incr;
70649ab747fSPaolo Bonzini } ADMADescr;
70749ab747fSPaolo Bonzini 
get_adma_description(SDHCIState * s,ADMADescr * dscr)70849ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
70949ab747fSPaolo Bonzini {
71049ab747fSPaolo Bonzini     uint32_t adma1 = 0;
71149ab747fSPaolo Bonzini     uint64_t adma2 = 0;
71249ab747fSPaolo Bonzini     hwaddr entry_addr = (hwaddr)s->admasysaddr;
71306c5120bSPhilippe Mathieu-Daudé     switch (SDHC_DMA_TYPE(s->hostctl1)) {
71449ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_32:
715ba06fe8aSPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2),
716ba06fe8aSPhilippe Mathieu-Daudé                         MEMTXATTRS_UNSPECIFIED);
71749ab747fSPaolo Bonzini         adma2 = le64_to_cpu(adma2);
7182df42919SJamin Lin         /*
7192df42919SJamin Lin          * The spec does not specify endianness of descriptor table.
72049ab747fSPaolo Bonzini          * We currently assume that it is LE.
72149ab747fSPaolo Bonzini          */
72249ab747fSPaolo Bonzini         dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull;
72349ab747fSPaolo Bonzini         dscr->length = (uint16_t)extract64(adma2, 16, 16);
72449ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract64(adma2, 0, 7);
72549ab747fSPaolo Bonzini         dscr->incr = 8;
72649ab747fSPaolo Bonzini         break;
72749ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA1_32:
728ba06fe8aSPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, &adma1, sizeof(adma1),
729ba06fe8aSPhilippe Mathieu-Daudé                         MEMTXATTRS_UNSPECIFIED);
73049ab747fSPaolo Bonzini         adma1 = le32_to_cpu(adma1);
73149ab747fSPaolo Bonzini         dscr->addr = (hwaddr)(adma1 & 0xFFFFF000);
73249ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract32(adma1, 0, 7);
73349ab747fSPaolo Bonzini         dscr->incr = 4;
73449ab747fSPaolo Bonzini         if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
73549ab747fSPaolo Bonzini             dscr->length = (uint16_t)extract32(adma1, 12, 16);
73649ab747fSPaolo Bonzini         } else {
7374c8f9735SPhilippe Mathieu-Daudé             dscr->length = 4 * KiB;
73849ab747fSPaolo Bonzini         }
73949ab747fSPaolo Bonzini         break;
74049ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_64:
741ba06fe8aSPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, &dscr->attr, 1,
742ba06fe8aSPhilippe Mathieu-Daudé                         MEMTXATTRS_UNSPECIFIED);
743ba06fe8aSPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 2, &dscr->length, 2,
744ba06fe8aSPhilippe Mathieu-Daudé                         MEMTXATTRS_UNSPECIFIED);
74549ab747fSPaolo Bonzini         dscr->length = le16_to_cpu(dscr->length);
746ba06fe8aSPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 4, &dscr->addr, 8,
747ba06fe8aSPhilippe Mathieu-Daudé                         MEMTXATTRS_UNSPECIFIED);
74804654b5aSSai Pavan Boddu         dscr->addr = le64_to_cpu(dscr->addr);
74904654b5aSSai Pavan Boddu         dscr->attr &= (uint8_t) ~0xC0;
75049ab747fSPaolo Bonzini         dscr->incr = 12;
75149ab747fSPaolo Bonzini         break;
75249ab747fSPaolo Bonzini     }
75349ab747fSPaolo Bonzini }
75449ab747fSPaolo Bonzini 
75549ab747fSPaolo Bonzini /* Advanced DMA data transfer */
75649ab747fSPaolo Bonzini 
sdhci_do_adma(SDHCIState * s)75749ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s)
75849ab747fSPaolo Bonzini {
759618e0be1SPhilippe Mathieu-Daudé     unsigned int begin, length;
760bf8ec38eSPhilippe Mathieu-Daudé     const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK;
761799f7f01SPhilippe Mathieu-Daudé     const MemTxAttrs attrs = { .memory = true };
7628be487d8SPhilippe Mathieu-Daudé     ADMADescr dscr = {};
763ea34d1ddSMarc-André Lureau     MemTxResult res = MEMTX_ERROR;
76449ab747fSPaolo Bonzini     int i;
76549ab747fSPaolo Bonzini 
7666a9e5cc6SPhilippe Mathieu-Daudé     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN && !s->blkcnt) {
7676a9e5cc6SPhilippe Mathieu-Daudé         /* Stop Multiple Transfer */
7686a9e5cc6SPhilippe Mathieu-Daudé         sdhci_end_transfer(s);
7696a9e5cc6SPhilippe Mathieu-Daudé         return;
7706a9e5cc6SPhilippe Mathieu-Daudé     }
7716a9e5cc6SPhilippe Mathieu-Daudé 
77249ab747fSPaolo Bonzini     for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
77349ab747fSPaolo Bonzini         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
77449ab747fSPaolo Bonzini 
77549ab747fSPaolo Bonzini         get_adma_description(s, &dscr);
7768be487d8SPhilippe Mathieu-Daudé         trace_sdhci_adma_loop(dscr.addr, dscr.length, dscr.attr);
77749ab747fSPaolo Bonzini 
77849ab747fSPaolo Bonzini         if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
77949ab747fSPaolo Bonzini             /* Indicate that error occurred in ST_FDS state */
78049ab747fSPaolo Bonzini             s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
78149ab747fSPaolo Bonzini             s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
78249ab747fSPaolo Bonzini 
78349ab747fSPaolo Bonzini             /* Generate ADMA error interrupt */
78449ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_ADMAERR) {
78549ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_ADMAERR;
78649ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
78749ab747fSPaolo Bonzini             }
78849ab747fSPaolo Bonzini 
78949ab747fSPaolo Bonzini             sdhci_update_irq(s);
79049ab747fSPaolo Bonzini             return;
79149ab747fSPaolo Bonzini         }
79249ab747fSPaolo Bonzini 
7934c8f9735SPhilippe Mathieu-Daudé         length = dscr.length ? dscr.length : 64 * KiB;
79449ab747fSPaolo Bonzini 
79549ab747fSPaolo Bonzini         switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) {
79649ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_TRAN:  /* data transfer */
797bc6f2899SBin Meng             s->prnsts |= SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE;
79849ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_READ) {
799bc6f2899SBin Meng                 s->prnsts |= SDHC_DOING_READ;
80049ab747fSPaolo Bonzini                 while (length) {
80149ab747fSPaolo Bonzini                     if (s->data_count == 0) {
802618e0be1SPhilippe Mathieu-Daudé                         sdbus_read_data(&s->sdbus, s->fifo_buffer, block_size);
80349ab747fSPaolo Bonzini                     }
80449ab747fSPaolo Bonzini                     begin = s->data_count;
80549ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
80649ab747fSPaolo Bonzini                         s->data_count = length + begin;
80749ab747fSPaolo Bonzini                         length = 0;
80849ab747fSPaolo Bonzini                      } else {
80949ab747fSPaolo Bonzini                         s->data_count = block_size;
81049ab747fSPaolo Bonzini                         length -= block_size - begin;
81149ab747fSPaolo Bonzini                     }
81278e619cbSPhilippe Mathieu-Daudé                     res = dma_memory_write(s->dma_as, dscr.addr,
81349ab747fSPaolo Bonzini                                            &s->fifo_buffer[begin],
814ba06fe8aSPhilippe Mathieu-Daudé                                            s->data_count - begin,
815799f7f01SPhilippe Mathieu-Daudé                                            attrs);
81678e619cbSPhilippe Mathieu-Daudé                     if (res != MEMTX_OK) {
81778e619cbSPhilippe Mathieu-Daudé                         break;
81878e619cbSPhilippe Mathieu-Daudé                     }
81949ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
82049ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
82149ab747fSPaolo Bonzini                         s->data_count = 0;
82249ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
82349ab747fSPaolo Bonzini                             s->blkcnt--;
82449ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
82549ab747fSPaolo Bonzini                                 break;
82649ab747fSPaolo Bonzini                             }
82749ab747fSPaolo Bonzini                         }
82849ab747fSPaolo Bonzini                     }
82949ab747fSPaolo Bonzini                 }
83049ab747fSPaolo Bonzini             } else {
831bc6f2899SBin Meng                 s->prnsts |= SDHC_DOING_WRITE;
83249ab747fSPaolo Bonzini                 while (length) {
83349ab747fSPaolo Bonzini                     begin = s->data_count;
83449ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
83549ab747fSPaolo Bonzini                         s->data_count = length + begin;
83649ab747fSPaolo Bonzini                         length = 0;
83749ab747fSPaolo Bonzini                      } else {
83849ab747fSPaolo Bonzini                         s->data_count = block_size;
83949ab747fSPaolo Bonzini                         length -= block_size - begin;
84049ab747fSPaolo Bonzini                     }
84178e619cbSPhilippe Mathieu-Daudé                     res = dma_memory_read(s->dma_as, dscr.addr,
8429db11cefSPeter Crosthwaite                                           &s->fifo_buffer[begin],
843ba06fe8aSPhilippe Mathieu-Daudé                                           s->data_count - begin,
844799f7f01SPhilippe Mathieu-Daudé                                           attrs);
84578e619cbSPhilippe Mathieu-Daudé                     if (res != MEMTX_OK) {
84678e619cbSPhilippe Mathieu-Daudé                         break;
84778e619cbSPhilippe Mathieu-Daudé                     }
84849ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
84949ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
85062a21be6SPhilippe Mathieu-Daudé                         sdbus_write_data(&s->sdbus, s->fifo_buffer, block_size);
85149ab747fSPaolo Bonzini                         s->data_count = 0;
85249ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
85349ab747fSPaolo Bonzini                             s->blkcnt--;
85449ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
85549ab747fSPaolo Bonzini                                 break;
85649ab747fSPaolo Bonzini                             }
85749ab747fSPaolo Bonzini                         }
85849ab747fSPaolo Bonzini                     }
85949ab747fSPaolo Bonzini                 }
86049ab747fSPaolo Bonzini             }
86178e619cbSPhilippe Mathieu-Daudé             if (res != MEMTX_OK) {
862ed5a159cSPhilippe Mathieu-Daudé                 s->data_count = 0;
86378e619cbSPhilippe Mathieu-Daudé                 if (s->errintstsen & SDHC_EISEN_ADMAERR) {
86478e619cbSPhilippe Mathieu-Daudé                     trace_sdhci_error("Set ADMA error flag");
86578e619cbSPhilippe Mathieu-Daudé                     s->errintsts |= SDHC_EIS_ADMAERR;
86678e619cbSPhilippe Mathieu-Daudé                     s->norintsts |= SDHC_NIS_ERR;
86778e619cbSPhilippe Mathieu-Daudé                 }
86878e619cbSPhilippe Mathieu-Daudé                 sdhci_update_irq(s);
86978e619cbSPhilippe Mathieu-Daudé             } else {
87049ab747fSPaolo Bonzini                 s->admasysaddr += dscr.incr;
87178e619cbSPhilippe Mathieu-Daudé             }
87249ab747fSPaolo Bonzini             break;
87349ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
87449ab747fSPaolo Bonzini             s->admasysaddr = dscr.addr;
8758be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("link", s->admasysaddr);
87649ab747fSPaolo Bonzini             break;
87749ab747fSPaolo Bonzini         default:
87849ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
87949ab747fSPaolo Bonzini             break;
88049ab747fSPaolo Bonzini         }
88149ab747fSPaolo Bonzini 
8821d32c26fSPeter Crosthwaite         if (dscr.attr & SDHC_ADMA_ATTR_INT) {
8838be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("interrupt", s->admasysaddr);
8841d32c26fSPeter Crosthwaite             if (s->norintstsen & SDHC_NISEN_DMA) {
8851d32c26fSPeter Crosthwaite                 s->norintsts |= SDHC_NIS_DMA;
8861d32c26fSPeter Crosthwaite             }
8871d32c26fSPeter Crosthwaite 
8889321c1f2SPhilippe Mathieu-Daudé             if (sdhci_update_irq(s) && !(dscr.attr & SDHC_ADMA_ATTR_END)) {
8899321c1f2SPhilippe Mathieu-Daudé                 /* IRQ delivered, reschedule current transfer */
8909321c1f2SPhilippe Mathieu-Daudé                 break;
8919321c1f2SPhilippe Mathieu-Daudé             }
8921d32c26fSPeter Crosthwaite         }
8931d32c26fSPeter Crosthwaite 
89449ab747fSPaolo Bonzini         /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
89549ab747fSPaolo Bonzini         if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
89649ab747fSPaolo Bonzini                     (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
8978be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma_transfer_completed();
89849ab747fSPaolo Bonzini             if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
89949ab747fSPaolo Bonzini                 (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
90049ab747fSPaolo Bonzini                 s->blkcnt != 0)) {
9018be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("SD/MMC host ADMA length mismatch");
90249ab747fSPaolo Bonzini                 s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
90349ab747fSPaolo Bonzini                         SDHC_ADMAERR_STATE_ST_TFR;
90449ab747fSPaolo Bonzini                 if (s->errintstsen & SDHC_EISEN_ADMAERR) {
9058be487d8SPhilippe Mathieu-Daudé                     trace_sdhci_error("Set ADMA error flag");
90649ab747fSPaolo Bonzini                     s->errintsts |= SDHC_EIS_ADMAERR;
90749ab747fSPaolo Bonzini                     s->norintsts |= SDHC_NIS_ERR;
90849ab747fSPaolo Bonzini                 }
90949ab747fSPaolo Bonzini 
91049ab747fSPaolo Bonzini                 sdhci_update_irq(s);
91149ab747fSPaolo Bonzini             }
912d368ba43SKevin O'Connor             sdhci_end_transfer(s);
91349ab747fSPaolo Bonzini             return;
91449ab747fSPaolo Bonzini         }
91549ab747fSPaolo Bonzini 
91649ab747fSPaolo Bonzini     }
91749ab747fSPaolo Bonzini 
91849ab747fSPaolo Bonzini     /* we have unfinished business - reschedule to continue ADMA */
919bc72ad67SAlex Bligh     timer_mod(s->transfer_timer,
920bc72ad67SAlex Bligh                    qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY);
92149ab747fSPaolo Bonzini }
92249ab747fSPaolo Bonzini 
92349ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */
92449ab747fSPaolo Bonzini 
sdhci_data_transfer(void * opaque)925d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque)
92649ab747fSPaolo Bonzini {
927d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
92849ab747fSPaolo Bonzini 
92949ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_DMA) {
93006c5120bSPhilippe Mathieu-Daudé         switch (SDHC_DMA_TYPE(s->hostctl1)) {
93149ab747fSPaolo Bonzini         case SDHC_CTRL_SDMA:
93249ab747fSPaolo Bonzini             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
933d368ba43SKevin O'Connor                 sdhci_sdma_transfer_single_block(s);
93449ab747fSPaolo Bonzini             } else {
935d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
93649ab747fSPaolo Bonzini             }
93749ab747fSPaolo Bonzini 
93849ab747fSPaolo Bonzini             break;
93949ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA1_32:
9400540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA1_MASK)) {
9418be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA1 not supported");
94249ab747fSPaolo Bonzini                 break;
94349ab747fSPaolo Bonzini             }
94449ab747fSPaolo Bonzini 
945d368ba43SKevin O'Connor             sdhci_do_adma(s);
94649ab747fSPaolo Bonzini             break;
94749ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_32:
9480540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK)) {
9498be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA2 not supported");
95049ab747fSPaolo Bonzini                 break;
95149ab747fSPaolo Bonzini             }
95249ab747fSPaolo Bonzini 
953d368ba43SKevin O'Connor             sdhci_do_adma(s);
95449ab747fSPaolo Bonzini             break;
95549ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_64:
9560540fba9SPhilippe Mathieu-Daudé             if (!(s->capareg & R_SDHC_CAPAB_ADMA2_MASK) ||
9570540fba9SPhilippe Mathieu-Daudé                     !(s->capareg & R_SDHC_CAPAB_BUS64BIT_MASK)) {
9588be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("64 bit ADMA not supported");
95949ab747fSPaolo Bonzini                 break;
96049ab747fSPaolo Bonzini             }
96149ab747fSPaolo Bonzini 
962d368ba43SKevin O'Connor             sdhci_do_adma(s);
96349ab747fSPaolo Bonzini             break;
96449ab747fSPaolo Bonzini         default:
9658be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("Unsupported DMA type");
96649ab747fSPaolo Bonzini             break;
96749ab747fSPaolo Bonzini         }
96849ab747fSPaolo Bonzini     } else {
96940bbc194SPeter Maydell         if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) {
97049ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
97149ab747fSPaolo Bonzini                     SDHC_DAT_LINE_ACTIVE;
972d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
97349ab747fSPaolo Bonzini         } else {
97449ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
97549ab747fSPaolo Bonzini                     SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
976d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
97749ab747fSPaolo Bonzini         }
97849ab747fSPaolo Bonzini     }
97949ab747fSPaolo Bonzini }
98049ab747fSPaolo Bonzini 
sdhci_can_issue_command(SDHCIState * s)98149ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s)
98249ab747fSPaolo Bonzini {
9836890a695SPeter Crosthwaite     if (!SDHC_CLOCK_IS_ON(s->clkcon) ||
98449ab747fSPaolo Bonzini         (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) &&
98549ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_DATA_PRESENT) ||
98649ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY &&
98749ab747fSPaolo Bonzini         !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) {
98849ab747fSPaolo Bonzini         return false;
98949ab747fSPaolo Bonzini     }
99049ab747fSPaolo Bonzini 
99149ab747fSPaolo Bonzini     return true;
99249ab747fSPaolo Bonzini }
99349ab747fSPaolo Bonzini 
9942df42919SJamin Lin /*
9952df42919SJamin Lin  * The Buffer Data Port register must be accessed in sequential and
9962df42919SJamin Lin  * continuous manner
9972df42919SJamin Lin  */
99849ab747fSPaolo Bonzini static inline bool
sdhci_buff_access_is_sequential(SDHCIState * s,unsigned byte_num)99949ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
100049ab747fSPaolo Bonzini {
100149ab747fSPaolo Bonzini     if ((s->data_count & 0x3) != byte_num) {
1002bb8dacedSPhilippe Mathieu-Daudé         qemu_log_mask(LOG_GUEST_ERROR,
1003bb8dacedSPhilippe Mathieu-Daudé                       "SDHCI: Non-sequential access to Buffer Data Port"
1004bb8dacedSPhilippe Mathieu-Daudé                       " register is prohibited\n");
100549ab747fSPaolo Bonzini         return false;
100649ab747fSPaolo Bonzini     }
100749ab747fSPaolo Bonzini     return true;
100849ab747fSPaolo Bonzini }
100949ab747fSPaolo Bonzini 
sdhci_resume_pending_transfer(SDHCIState * s)101045e5dc43SPhilippe Mathieu-Daudé static void sdhci_resume_pending_transfer(SDHCIState *s)
101145e5dc43SPhilippe Mathieu-Daudé {
101245e5dc43SPhilippe Mathieu-Daudé     timer_del(s->transfer_timer);
101345e5dc43SPhilippe Mathieu-Daudé     sdhci_data_transfer(s);
101445e5dc43SPhilippe Mathieu-Daudé }
101545e5dc43SPhilippe Mathieu-Daudé 
sdhci_read(void * opaque,hwaddr offset,unsigned size)1016d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size)
101749ab747fSPaolo Bonzini {
1018d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
101949ab747fSPaolo Bonzini     uint32_t ret = 0;
102049ab747fSPaolo Bonzini 
102145e5dc43SPhilippe Mathieu-Daudé     if (timer_pending(s->transfer_timer)) {
102245e5dc43SPhilippe Mathieu-Daudé         sdhci_resume_pending_transfer(s);
102345e5dc43SPhilippe Mathieu-Daudé     }
102445e5dc43SPhilippe Mathieu-Daudé 
102549ab747fSPaolo Bonzini     switch (offset & ~0x3) {
102649ab747fSPaolo Bonzini     case SDHC_SYSAD:
102749ab747fSPaolo Bonzini         ret = s->sdmasysad;
102849ab747fSPaolo Bonzini         break;
102949ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
103049ab747fSPaolo Bonzini         ret = s->blksize | (s->blkcnt << 16);
103149ab747fSPaolo Bonzini         break;
103249ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
103349ab747fSPaolo Bonzini         ret = s->argument;
103449ab747fSPaolo Bonzini         break;
103549ab747fSPaolo Bonzini     case SDHC_TRNMOD:
103649ab747fSPaolo Bonzini         ret = s->trnmod | (s->cmdreg << 16);
103749ab747fSPaolo Bonzini         break;
103849ab747fSPaolo Bonzini     case SDHC_RSPREG0 ... SDHC_RSPREG3:
103949ab747fSPaolo Bonzini         ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2];
104049ab747fSPaolo Bonzini         break;
104149ab747fSPaolo Bonzini     case  SDHC_BDATA:
104249ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
1043d368ba43SKevin O'Connor             ret = sdhci_read_dataport(s, size);
10448be487d8SPhilippe Mathieu-Daudé             trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
104549ab747fSPaolo Bonzini             return ret;
104649ab747fSPaolo Bonzini         }
104749ab747fSPaolo Bonzini         break;
104849ab747fSPaolo Bonzini     case SDHC_PRNSTS:
104949ab747fSPaolo Bonzini         ret = s->prnsts;
1050da346922SPhilippe Mathieu-Daudé         ret = FIELD_DP32(ret, SDHC_PRNSTS, DAT_LVL,
1051da346922SPhilippe Mathieu-Daudé                          sdbus_get_dat_lines(&s->sdbus));
1052da346922SPhilippe Mathieu-Daudé         ret = FIELD_DP32(ret, SDHC_PRNSTS, CMD_LVL,
1053da346922SPhilippe Mathieu-Daudé                          sdbus_get_cmd_line(&s->sdbus));
105449ab747fSPaolo Bonzini         break;
105549ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
105606c5120bSPhilippe Mathieu-Daudé         ret = s->hostctl1 | (s->pwrcon << 8) | (s->blkgap << 16) |
105749ab747fSPaolo Bonzini               (s->wakcon << 24);
105849ab747fSPaolo Bonzini         break;
105949ab747fSPaolo Bonzini     case SDHC_CLKCON:
106049ab747fSPaolo Bonzini         ret = s->clkcon | (s->timeoutcon << 16);
106149ab747fSPaolo Bonzini         break;
106249ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
106349ab747fSPaolo Bonzini         ret = s->norintsts | (s->errintsts << 16);
106449ab747fSPaolo Bonzini         break;
106549ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
106649ab747fSPaolo Bonzini         ret = s->norintstsen | (s->errintstsen << 16);
106749ab747fSPaolo Bonzini         break;
106849ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
106949ab747fSPaolo Bonzini         ret = s->norintsigen | (s->errintsigen << 16);
107049ab747fSPaolo Bonzini         break;
107149ab747fSPaolo Bonzini     case SDHC_ACMD12ERRSTS:
1072ea55a221SPhilippe Mathieu-Daudé         ret = s->acmd12errsts | (s->hostctl2 << 16);
107349ab747fSPaolo Bonzini         break;
1074cd209421SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
10755efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->capareg;
10765efc9016SPhilippe Mathieu-Daudé         break;
10775efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
10785efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->capareg >> 32);
107949ab747fSPaolo Bonzini         break;
108049ab747fSPaolo Bonzini     case SDHC_MAXCURR:
10815efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->maxcurr;
10825efc9016SPhilippe Mathieu-Daudé         break;
10835efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
10845efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->maxcurr >> 32);
108549ab747fSPaolo Bonzini         break;
108649ab747fSPaolo Bonzini     case SDHC_ADMAERR:
108749ab747fSPaolo Bonzini         ret =  s->admaerr;
108849ab747fSPaolo Bonzini         break;
108949ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
109049ab747fSPaolo Bonzini         ret = (uint32_t)s->admasysaddr;
109149ab747fSPaolo Bonzini         break;
109249ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
109349ab747fSPaolo Bonzini         ret = (uint32_t)(s->admasysaddr >> 32);
109449ab747fSPaolo Bonzini         break;
109549ab747fSPaolo Bonzini     case SDHC_SLOT_INT_STATUS:
1096aceb5b06SPhilippe Mathieu-Daudé         ret = (s->version << 16) | sdhci_slotint(s);
109749ab747fSPaolo Bonzini         break;
109849ab747fSPaolo Bonzini     default:
109900b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC rd_%ub @0x%02" HWADDR_PRIx " "
110000b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset);
110149ab747fSPaolo Bonzini         break;
110249ab747fSPaolo Bonzini     }
110349ab747fSPaolo Bonzini 
110449ab747fSPaolo Bonzini     ret >>= (offset & 0x3) * 8;
110549ab747fSPaolo Bonzini     ret &= (1ULL << (size * 8)) - 1;
11068be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
110749ab747fSPaolo Bonzini     return ret;
110849ab747fSPaolo Bonzini }
110949ab747fSPaolo Bonzini 
sdhci_blkgap_write(SDHCIState * s,uint8_t value)111049ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value)
111149ab747fSPaolo Bonzini {
111249ab747fSPaolo Bonzini     if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) {
111349ab747fSPaolo Bonzini         return;
111449ab747fSPaolo Bonzini     }
111549ab747fSPaolo Bonzini     s->blkgap = value & SDHC_STOP_AT_GAP_REQ;
111649ab747fSPaolo Bonzini 
111749ab747fSPaolo Bonzini     if ((value & SDHC_CONTINUE_REQ) && s->stopped_state &&
111849ab747fSPaolo Bonzini             (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) {
111949ab747fSPaolo Bonzini         if (s->stopped_state == sdhc_gap_read) {
112049ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ;
1121d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
112249ab747fSPaolo Bonzini         } else {
112349ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE;
1124d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
112549ab747fSPaolo Bonzini         }
112649ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
112749ab747fSPaolo Bonzini     } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) {
112849ab747fSPaolo Bonzini         if (s->prnsts & SDHC_DOING_READ) {
112949ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_read;
113049ab747fSPaolo Bonzini         } else if (s->prnsts & SDHC_DOING_WRITE) {
113149ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_write;
113249ab747fSPaolo Bonzini         }
113349ab747fSPaolo Bonzini     }
113449ab747fSPaolo Bonzini }
113549ab747fSPaolo Bonzini 
sdhci_reset_write(SDHCIState * s,uint8_t value)113649ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value)
113749ab747fSPaolo Bonzini {
113849ab747fSPaolo Bonzini     switch (value) {
113949ab747fSPaolo Bonzini     case SDHC_RESET_ALL:
1140d368ba43SKevin O'Connor         sdhci_reset(s);
114149ab747fSPaolo Bonzini         break;
114249ab747fSPaolo Bonzini     case SDHC_RESET_CMD:
114349ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_CMD_INHIBIT;
114449ab747fSPaolo Bonzini         s->norintsts &= ~SDHC_NIS_CMDCMP;
114549ab747fSPaolo Bonzini         break;
114649ab747fSPaolo Bonzini     case SDHC_RESET_DATA:
114749ab747fSPaolo Bonzini         s->data_count = 0;
114849ab747fSPaolo Bonzini         s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE |
114949ab747fSPaolo Bonzini                 SDHC_DOING_READ | SDHC_DOING_WRITE |
115049ab747fSPaolo Bonzini                 SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE);
115149ab747fSPaolo Bonzini         s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ);
115249ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
115349ab747fSPaolo Bonzini         s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY |
115449ab747fSPaolo Bonzini                 SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP);
115549ab747fSPaolo Bonzini         break;
115649ab747fSPaolo Bonzini     }
115749ab747fSPaolo Bonzini }
115849ab747fSPaolo Bonzini 
115949ab747fSPaolo Bonzini static void
sdhci_write(void * opaque,hwaddr offset,uint64_t val,unsigned size)1160d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
116149ab747fSPaolo Bonzini {
1162d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
116349ab747fSPaolo Bonzini     unsigned shift =  8 * (offset & 0x3);
116449ab747fSPaolo Bonzini     uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift);
1165d368ba43SKevin O'Connor     uint32_t value = val;
116649ab747fSPaolo Bonzini     value <<= shift;
116749ab747fSPaolo Bonzini 
116845e5dc43SPhilippe Mathieu-Daudé     if (timer_pending(s->transfer_timer)) {
116945e5dc43SPhilippe Mathieu-Daudé         sdhci_resume_pending_transfer(s);
117045e5dc43SPhilippe Mathieu-Daudé     }
117145e5dc43SPhilippe Mathieu-Daudé 
117249ab747fSPaolo Bonzini     switch (offset & ~0x3) {
117349ab747fSPaolo Bonzini     case SDHC_SYSAD:
11748be45cc9SBin Meng         if (!TRANSFERRING_DATA(s->prnsts)) {
117549ab747fSPaolo Bonzini             s->sdmasysad = (s->sdmasysad & mask) | value;
117649ab747fSPaolo Bonzini             MASKED_WRITE(s->sdmasysad, mask, value);
117749ab747fSPaolo Bonzini             /* Writing to last byte of sdmasysad might trigger transfer */
1178946df4d5SLu Gao             if (!(mask & 0xFF000000) && s->blkcnt &&
1179946df4d5SLu Gao                 (s->blksize & BLOCK_SIZE_MASK) &&
11808be45cc9SBin Meng                 SDHC_DMA_TYPE(s->hostctl1) == SDHC_CTRL_SDMA) {
118145ba9f76SPrasad J Pandit                 if (s->trnmod & SDHC_TRNS_MULTI) {
1182d368ba43SKevin O'Connor                     sdhci_sdma_transfer_multi_blocks(s);
118345ba9f76SPrasad J Pandit                 } else {
118445ba9f76SPrasad J Pandit                     sdhci_sdma_transfer_single_block(s);
118545ba9f76SPrasad J Pandit                 }
118649ab747fSPaolo Bonzini             }
11878be45cc9SBin Meng         }
118849ab747fSPaolo Bonzini         break;
118949ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
119049ab747fSPaolo Bonzini         if (!TRANSFERRING_DATA(s->prnsts)) {
1191cffb446eSBin Meng             uint16_t blksize = s->blksize;
1192cffb446eSBin Meng 
1193946df4d5SLu Gao             /*
1194946df4d5SLu Gao              * [14:12] SDMA Buffer Boundary
1195946df4d5SLu Gao              * [11:00] Transfer Block Size
1196946df4d5SLu Gao              */
1197946df4d5SLu Gao             MASKED_WRITE(s->blksize, mask, extract32(value, 0, 15));
119849ab747fSPaolo Bonzini             MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16);
11999201bb9aSAlistair Francis 
12009201bb9aSAlistair Francis             /* Limit block size to the maximum buffer size */
12019201bb9aSAlistair Francis             if (extract32(s->blksize, 0, 12) > s->buf_maxsz) {
120278ee6bd0SPhilippe Mathieu-Daudé                 qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than "
12039227cc52SPhilippe Mathieu-Daudé                               "the maximum buffer 0x%x\n", __func__, s->blksize,
12049201bb9aSAlistair Francis                               s->buf_maxsz);
12059201bb9aSAlistair Francis 
12069201bb9aSAlistair Francis                 s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz);
12079201bb9aSAlistair Francis             }
1208cffb446eSBin Meng 
1209cffb446eSBin Meng             /*
1210cffb446eSBin Meng              * If the block size is programmed to a different value from
1211cffb446eSBin Meng              * the previous one, reset the data pointer of s->fifo_buffer[]
1212cffb446eSBin Meng              * so that s->fifo_buffer[] can be filled in using the new block
1213cffb446eSBin Meng              * size in the next transfer.
1214cffb446eSBin Meng              */
1215cffb446eSBin Meng             if (blksize != s->blksize) {
1216cffb446eSBin Meng                 s->data_count = 0;
1217cffb446eSBin Meng             }
12185cd7aa34SBin Meng         }
12199201bb9aSAlistair Francis 
122049ab747fSPaolo Bonzini         break;
122149ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
122249ab747fSPaolo Bonzini         MASKED_WRITE(s->argument, mask, value);
122349ab747fSPaolo Bonzini         break;
122449ab747fSPaolo Bonzini     case SDHC_TRNMOD:
12252df42919SJamin Lin         /*
12262df42919SJamin Lin          * DMA can be enabled only if it is supported as indicated by
12272df42919SJamin Lin          * capabilities register
12282df42919SJamin Lin          */
12296ff37c3dSPhilippe Mathieu-Daudé         if (!(s->capareg & R_SDHC_CAPAB_SDMA_MASK)) {
123049ab747fSPaolo Bonzini             value &= ~SDHC_TRNS_DMA;
123149ab747fSPaolo Bonzini         }
12329e4b27caSPhilippe Mathieu-Daudé 
12339e4b27caSPhilippe Mathieu-Daudé         /* TRNMOD writes are inhibited while Command Inhibit (DAT) is true */
12349e4b27caSPhilippe Mathieu-Daudé         if (s->prnsts & SDHC_DATA_INHIBIT) {
12359e4b27caSPhilippe Mathieu-Daudé             mask |= 0xffff;
12369e4b27caSPhilippe Mathieu-Daudé         }
12379e4b27caSPhilippe Mathieu-Daudé 
123824bddf9dSPhilippe Mathieu-Daudé         MASKED_WRITE(s->trnmod, mask, value & SDHC_TRNMOD_MASK);
123949ab747fSPaolo Bonzini         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
124049ab747fSPaolo Bonzini 
124149ab747fSPaolo Bonzini         /* Writing to the upper byte of CMDREG triggers SD command generation */
1242d368ba43SKevin O'Connor         if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) {
124349ab747fSPaolo Bonzini             break;
124449ab747fSPaolo Bonzini         }
124549ab747fSPaolo Bonzini 
1246d368ba43SKevin O'Connor         sdhci_send_command(s);
124749ab747fSPaolo Bonzini         break;
124849ab747fSPaolo Bonzini     case  SDHC_BDATA:
124949ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
1250d368ba43SKevin O'Connor             sdhci_write_dataport(s, value >> shift, size);
125149ab747fSPaolo Bonzini         }
125249ab747fSPaolo Bonzini         break;
125349ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
125449ab747fSPaolo Bonzini         if (!(mask & 0xFF0000)) {
125549ab747fSPaolo Bonzini             sdhci_blkgap_write(s, value >> 16);
125649ab747fSPaolo Bonzini         }
125706c5120bSPhilippe Mathieu-Daudé         MASKED_WRITE(s->hostctl1, mask, value);
125849ab747fSPaolo Bonzini         MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8);
125949ab747fSPaolo Bonzini         MASKED_WRITE(s->wakcon, mask >> 24, value >> 24);
126049ab747fSPaolo Bonzini         if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 ||
126149ab747fSPaolo Bonzini                 !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) {
126249ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
126349ab747fSPaolo Bonzini         }
126449ab747fSPaolo Bonzini         break;
126549ab747fSPaolo Bonzini     case SDHC_CLKCON:
126649ab747fSPaolo Bonzini         if (!(mask & 0xFF000000)) {
126749ab747fSPaolo Bonzini             sdhci_reset_write(s, value >> 24);
126849ab747fSPaolo Bonzini         }
126949ab747fSPaolo Bonzini         MASKED_WRITE(s->clkcon, mask, value);
127049ab747fSPaolo Bonzini         MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16);
127149ab747fSPaolo Bonzini         if (s->clkcon & SDHC_CLOCK_INT_EN) {
127249ab747fSPaolo Bonzini             s->clkcon |= SDHC_CLOCK_INT_STABLE;
127349ab747fSPaolo Bonzini         } else {
127449ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_INT_STABLE;
127549ab747fSPaolo Bonzini         }
127649ab747fSPaolo Bonzini         break;
127749ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
127849ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_CARDINT) {
127949ab747fSPaolo Bonzini             value &= ~SDHC_NIS_CARDINT;
128049ab747fSPaolo Bonzini         }
128149ab747fSPaolo Bonzini         s->norintsts &= mask | ~value;
128249ab747fSPaolo Bonzini         s->errintsts &= (mask >> 16) | ~(value >> 16);
128349ab747fSPaolo Bonzini         if (s->errintsts) {
128449ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
128549ab747fSPaolo Bonzini         } else {
128649ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
128749ab747fSPaolo Bonzini         }
128849ab747fSPaolo Bonzini         sdhci_update_irq(s);
128949ab747fSPaolo Bonzini         break;
129049ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
129149ab747fSPaolo Bonzini         MASKED_WRITE(s->norintstsen, mask, value);
129249ab747fSPaolo Bonzini         MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16);
129349ab747fSPaolo Bonzini         s->norintsts &= s->norintstsen;
129449ab747fSPaolo Bonzini         s->errintsts &= s->errintstsen;
129549ab747fSPaolo Bonzini         if (s->errintsts) {
129649ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
129749ab747fSPaolo Bonzini         } else {
129849ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
129949ab747fSPaolo Bonzini         }
13002df42919SJamin Lin         /*
13012df42919SJamin Lin          * Quirk for Raspberry Pi: pending card insert interrupt
13022df42919SJamin Lin          * appears when first enabled after power on
13032df42919SJamin Lin          */
13040a7ac9f9SAndrew Baumann         if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) {
13050a7ac9f9SAndrew Baumann             assert(s->pending_insert_quirk);
13060a7ac9f9SAndrew Baumann             s->norintsts |= SDHC_NIS_INSERT;
13070a7ac9f9SAndrew Baumann             s->pending_insert_state = false;
13080a7ac9f9SAndrew Baumann         }
130949ab747fSPaolo Bonzini         sdhci_update_irq(s);
131049ab747fSPaolo Bonzini         break;
131149ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
131249ab747fSPaolo Bonzini         MASKED_WRITE(s->norintsigen, mask, value);
131349ab747fSPaolo Bonzini         MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16);
131449ab747fSPaolo Bonzini         sdhci_update_irq(s);
131549ab747fSPaolo Bonzini         break;
131649ab747fSPaolo Bonzini     case SDHC_ADMAERR:
131749ab747fSPaolo Bonzini         MASKED_WRITE(s->admaerr, mask, value);
131849ab747fSPaolo Bonzini         break;
131949ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
132049ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL |
132149ab747fSPaolo Bonzini                 (uint64_t)mask)) | (uint64_t)value;
132249ab747fSPaolo Bonzini         break;
132349ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
132449ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL |
132549ab747fSPaolo Bonzini                 ((uint64_t)mask << 32))) | ((uint64_t)value << 32);
132649ab747fSPaolo Bonzini         break;
132749ab747fSPaolo Bonzini     case SDHC_FEAER:
132849ab747fSPaolo Bonzini         s->acmd12errsts |= value;
132949ab747fSPaolo Bonzini         s->errintsts |= (value >> 16) & s->errintstsen;
133049ab747fSPaolo Bonzini         if (s->acmd12errsts) {
133149ab747fSPaolo Bonzini             s->errintsts |= SDHC_EIS_CMD12ERR;
133249ab747fSPaolo Bonzini         }
133349ab747fSPaolo Bonzini         if (s->errintsts) {
133449ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
133549ab747fSPaolo Bonzini         }
133649ab747fSPaolo Bonzini         sdhci_update_irq(s);
133749ab747fSPaolo Bonzini         break;
13385d2c0464SAndrey Smirnov     case SDHC_ACMD12ERRSTS:
13390034ebe6SPhilippe Mathieu-Daudé         MASKED_WRITE(s->acmd12errsts, mask, value & UINT16_MAX);
13400034ebe6SPhilippe Mathieu-Daudé         if (s->uhs_mode >= UHS_I) {
13410034ebe6SPhilippe Mathieu-Daudé             MASKED_WRITE(s->hostctl2, mask >> 16, value >> 16);
13420034ebe6SPhilippe Mathieu-Daudé 
13430034ebe6SPhilippe Mathieu-Daudé             if (FIELD_EX32(s->hostctl2, SDHC_HOSTCTL2, V18_ENA)) {
13440034ebe6SPhilippe Mathieu-Daudé                 sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_1_8V);
13450034ebe6SPhilippe Mathieu-Daudé             } else {
13460034ebe6SPhilippe Mathieu-Daudé                 sdbus_set_voltage(&s->sdbus, SD_VOLTAGE_3_3V);
13470034ebe6SPhilippe Mathieu-Daudé             }
13480034ebe6SPhilippe Mathieu-Daudé         }
13495d2c0464SAndrey Smirnov         break;
13505efc9016SPhilippe Mathieu-Daudé 
13515efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
13525efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
13535efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR:
13545efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
13555efc9016SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_GUEST_ERROR, "SDHC wr_%ub @0x%02" HWADDR_PRIx
13565efc9016SPhilippe Mathieu-Daudé                       " <- 0x%08x read-only\n", size, offset, value >> shift);
13575efc9016SPhilippe Mathieu-Daudé         break;
13585efc9016SPhilippe Mathieu-Daudé 
135949ab747fSPaolo Bonzini     default:
136000b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC wr_%ub @0x%02" HWADDR_PRIx " <- 0x%08x "
136100b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset, value >> shift);
136249ab747fSPaolo Bonzini         break;
136349ab747fSPaolo Bonzini     }
13648be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("wr", size << 3, offset, "<-",
13658be487d8SPhilippe Mathieu-Daudé                        value >> shift, value >> shift);
136649ab747fSPaolo Bonzini }
136749ab747fSPaolo Bonzini 
1368c0a55a0cSPhilippe Mathieu-Daudé static const MemoryRegionOps sdhci_mmio_le_ops = {
1369d368ba43SKevin O'Connor     .read = sdhci_read,
1370d368ba43SKevin O'Connor     .write = sdhci_write,
137149ab747fSPaolo Bonzini     .valid = {
137249ab747fSPaolo Bonzini         .min_access_size = 1,
137349ab747fSPaolo Bonzini         .max_access_size = 4,
137449ab747fSPaolo Bonzini         .unaligned = false
137549ab747fSPaolo Bonzini     },
137649ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
137749ab747fSPaolo Bonzini };
137849ab747fSPaolo Bonzini 
1379c0a55a0cSPhilippe Mathieu-Daudé static const MemoryRegionOps sdhci_mmio_be_ops = {
1380c0a55a0cSPhilippe Mathieu-Daudé     .read = sdhci_read,
1381c0a55a0cSPhilippe Mathieu-Daudé     .write = sdhci_write,
1382c0a55a0cSPhilippe Mathieu-Daudé     .impl = {
1383c0a55a0cSPhilippe Mathieu-Daudé         .min_access_size = 4,
1384c0a55a0cSPhilippe Mathieu-Daudé         .max_access_size = 4,
1385c0a55a0cSPhilippe Mathieu-Daudé     },
1386c0a55a0cSPhilippe Mathieu-Daudé     .valid = {
1387c0a55a0cSPhilippe Mathieu-Daudé         .min_access_size = 1,
1388c0a55a0cSPhilippe Mathieu-Daudé         .max_access_size = 4,
1389c0a55a0cSPhilippe Mathieu-Daudé         .unaligned = false
1390c0a55a0cSPhilippe Mathieu-Daudé     },
1391c0a55a0cSPhilippe Mathieu-Daudé     .endianness = DEVICE_BIG_ENDIAN,
1392c0a55a0cSPhilippe Mathieu-Daudé };
1393c0a55a0cSPhilippe Mathieu-Daudé 
sdhci_init_readonly_registers(SDHCIState * s,Error ** errp)1394aceb5b06SPhilippe Mathieu-Daudé static void sdhci_init_readonly_registers(SDHCIState *s, Error **errp)
1395aceb5b06SPhilippe Mathieu-Daudé {
1396de1b3800SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
13976ff37c3dSPhilippe Mathieu-Daudé 
13984d67852dSPhilippe Mathieu-Daudé     switch (s->sd_spec_version) {
13994d67852dSPhilippe Mathieu-Daudé     case 2 ... 3:
14004d67852dSPhilippe Mathieu-Daudé         break;
14014d67852dSPhilippe Mathieu-Daudé     default:
14024d67852dSPhilippe Mathieu-Daudé         error_setg(errp, "Only Spec v2/v3 are supported");
1403aceb5b06SPhilippe Mathieu-Daudé         return;
1404aceb5b06SPhilippe Mathieu-Daudé     }
1405aceb5b06SPhilippe Mathieu-Daudé     s->version = (SDHC_HCVER_VENDOR << 8) | (s->sd_spec_version - 1);
14066ff37c3dSPhilippe Mathieu-Daudé 
1407de1b3800SVladimir Sementsov-Ogievskiy     sdhci_check_capareg(s, errp);
1408de1b3800SVladimir Sementsov-Ogievskiy     if (*errp) {
14096ff37c3dSPhilippe Mathieu-Daudé         return;
14106ff37c3dSPhilippe Mathieu-Daudé     }
1411aceb5b06SPhilippe Mathieu-Daudé }
1412aceb5b06SPhilippe Mathieu-Daudé 
1413b635d98cSPhilippe Mathieu-Daudé /* --- qdev common --- */
1414b635d98cSPhilippe Mathieu-Daudé 
sdhci_initfn(SDHCIState * s)1415ce864603SThomas Huth void sdhci_initfn(SDHCIState *s)
141649ab747fSPaolo Bonzini {
1417d637e1dcSPeter Maydell     qbus_init(&s->sdbus, sizeof(s->sdbus), TYPE_SDHCI_BUS, DEVICE(s), "sd-bus");
141849ab747fSPaolo Bonzini 
14192df42919SJamin Lin     s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
14202df42919SJamin Lin                                    sdhci_raise_insertion_irq, s);
14212df42919SJamin Lin     s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
14222df42919SJamin Lin                                      sdhci_data_transfer, s);
14233b830790SBernhard Beschow 
14243b830790SBernhard Beschow     s->io_ops = &sdhci_mmio_le_ops;
142549ab747fSPaolo Bonzini }
142649ab747fSPaolo Bonzini 
sdhci_uninitfn(SDHCIState * s)1427ce864603SThomas Huth void sdhci_uninitfn(SDHCIState *s)
142849ab747fSPaolo Bonzini {
1429bc72ad67SAlex Bligh     timer_free(s->insert_timer);
1430bc72ad67SAlex Bligh     timer_free(s->transfer_timer);
143149ab747fSPaolo Bonzini 
143249ab747fSPaolo Bonzini     g_free(s->fifo_buffer);
143349ab747fSPaolo Bonzini     s->fifo_buffer = NULL;
143449ab747fSPaolo Bonzini }
143549ab747fSPaolo Bonzini 
sdhci_common_realize(SDHCIState * s,Error ** errp)1436ce864603SThomas Huth void sdhci_common_realize(SDHCIState *s, Error **errp)
143725367498SPhilippe Mathieu-Daudé {
1438de1b3800SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
1439aceb5b06SPhilippe Mathieu-Daudé 
1440c0a55a0cSPhilippe Mathieu-Daudé     switch (s->endianness) {
1441c0a55a0cSPhilippe Mathieu-Daudé     case DEVICE_LITTLE_ENDIAN:
14423b830790SBernhard Beschow         /* s->io_ops is little endian by default */
1443c0a55a0cSPhilippe Mathieu-Daudé         break;
1444c0a55a0cSPhilippe Mathieu-Daudé     case DEVICE_BIG_ENDIAN:
14453b830790SBernhard Beschow         if (s->io_ops != &sdhci_mmio_le_ops) {
14463b830790SBernhard Beschow             error_setg(errp, "SD controller doesn't support big endianness");
14473b830790SBernhard Beschow             return;
14483b830790SBernhard Beschow         }
1449c0a55a0cSPhilippe Mathieu-Daudé         s->io_ops = &sdhci_mmio_be_ops;
1450c0a55a0cSPhilippe Mathieu-Daudé         break;
1451c0a55a0cSPhilippe Mathieu-Daudé     default:
1452c0a55a0cSPhilippe Mathieu-Daudé         error_setg(errp, "Incorrect endianness");
1453c0a55a0cSPhilippe Mathieu-Daudé         return;
1454c0a55a0cSPhilippe Mathieu-Daudé     }
1455c0a55a0cSPhilippe Mathieu-Daudé 
1456de1b3800SVladimir Sementsov-Ogievskiy     sdhci_init_readonly_registers(s, errp);
1457de1b3800SVladimir Sementsov-Ogievskiy     if (*errp) {
1458aceb5b06SPhilippe Mathieu-Daudé         return;
1459aceb5b06SPhilippe Mathieu-Daudé     }
1460c0a55a0cSPhilippe Mathieu-Daudé 
146125367498SPhilippe Mathieu-Daudé     s->buf_maxsz = sdhci_get_fifolen(s);
146225367498SPhilippe Mathieu-Daudé     s->fifo_buffer = g_malloc0(s->buf_maxsz);
146325367498SPhilippe Mathieu-Daudé 
1464c0983085SPeter Maydell     memory_region_init_io(&s->iomem, OBJECT(s), s->io_ops, s, "sdhci",
146525367498SPhilippe Mathieu-Daudé                           SDHC_REGISTERS_MAP_SIZE);
146625367498SPhilippe Mathieu-Daudé }
146725367498SPhilippe Mathieu-Daudé 
sdhci_common_unrealize(SDHCIState * s)1468b69c3c21SMarkus Armbruster void sdhci_common_unrealize(SDHCIState *s)
14698b7455c7SPhilippe Mathieu-Daudé {
14702df42919SJamin Lin     /*
14712df42919SJamin Lin      * This function is expected to be called only once for each class:
14728b7455c7SPhilippe Mathieu-Daudé      * - SysBus:    via DeviceClass->unrealize(),
14738b7455c7SPhilippe Mathieu-Daudé      * - PCI:       via PCIDeviceClass->exit().
14748b7455c7SPhilippe Mathieu-Daudé      * However to avoid double-free and/or use-after-free we still nullify
14752df42919SJamin Lin      * this variable (better safe than sorry!).
14762df42919SJamin Lin      */
14778b7455c7SPhilippe Mathieu-Daudé     g_free(s->fifo_buffer);
14788b7455c7SPhilippe Mathieu-Daudé     s->fifo_buffer = NULL;
14798b7455c7SPhilippe Mathieu-Daudé }
14808b7455c7SPhilippe Mathieu-Daudé 
sdhci_pending_insert_vmstate_needed(void * opaque)14810a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque)
14820a7ac9f9SAndrew Baumann {
14830a7ac9f9SAndrew Baumann     SDHCIState *s = opaque;
14840a7ac9f9SAndrew Baumann 
14850a7ac9f9SAndrew Baumann     return s->pending_insert_state;
14860a7ac9f9SAndrew Baumann }
14870a7ac9f9SAndrew Baumann 
14880a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = {
14890a7ac9f9SAndrew Baumann     .name = "sdhci/pending-insert",
14900a7ac9f9SAndrew Baumann     .version_id = 1,
14910a7ac9f9SAndrew Baumann     .minimum_version_id = 1,
14920a7ac9f9SAndrew Baumann     .needed = sdhci_pending_insert_vmstate_needed,
1493307119baSRichard Henderson     .fields = (const VMStateField[]) {
14940a7ac9f9SAndrew Baumann         VMSTATE_BOOL(pending_insert_state, SDHCIState),
14950a7ac9f9SAndrew Baumann         VMSTATE_END_OF_LIST()
14960a7ac9f9SAndrew Baumann     },
14970a7ac9f9SAndrew Baumann };
14980a7ac9f9SAndrew Baumann 
149949ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = {
150049ab747fSPaolo Bonzini     .name = "sdhci",
150149ab747fSPaolo Bonzini     .version_id = 1,
150249ab747fSPaolo Bonzini     .minimum_version_id = 1,
1503307119baSRichard Henderson     .fields = (const VMStateField[]) {
150449ab747fSPaolo Bonzini         VMSTATE_UINT32(sdmasysad, SDHCIState),
150549ab747fSPaolo Bonzini         VMSTATE_UINT16(blksize, SDHCIState),
150649ab747fSPaolo Bonzini         VMSTATE_UINT16(blkcnt, SDHCIState),
150749ab747fSPaolo Bonzini         VMSTATE_UINT32(argument, SDHCIState),
150849ab747fSPaolo Bonzini         VMSTATE_UINT16(trnmod, SDHCIState),
150949ab747fSPaolo Bonzini         VMSTATE_UINT16(cmdreg, SDHCIState),
151049ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4),
151149ab747fSPaolo Bonzini         VMSTATE_UINT32(prnsts, SDHCIState),
151206c5120bSPhilippe Mathieu-Daudé         VMSTATE_UINT8(hostctl1, SDHCIState),
151349ab747fSPaolo Bonzini         VMSTATE_UINT8(pwrcon, SDHCIState),
151449ab747fSPaolo Bonzini         VMSTATE_UINT8(blkgap, SDHCIState),
151549ab747fSPaolo Bonzini         VMSTATE_UINT8(wakcon, SDHCIState),
151649ab747fSPaolo Bonzini         VMSTATE_UINT16(clkcon, SDHCIState),
151749ab747fSPaolo Bonzini         VMSTATE_UINT8(timeoutcon, SDHCIState),
151849ab747fSPaolo Bonzini         VMSTATE_UINT8(admaerr, SDHCIState),
151949ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsts, SDHCIState),
152049ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsts, SDHCIState),
152149ab747fSPaolo Bonzini         VMSTATE_UINT16(norintstsen, SDHCIState),
152249ab747fSPaolo Bonzini         VMSTATE_UINT16(errintstsen, SDHCIState),
152349ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsigen, SDHCIState),
152449ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsigen, SDHCIState),
152549ab747fSPaolo Bonzini         VMSTATE_UINT16(acmd12errsts, SDHCIState),
152649ab747fSPaolo Bonzini         VMSTATE_UINT16(data_count, SDHCIState),
152749ab747fSPaolo Bonzini         VMSTATE_UINT64(admasysaddr, SDHCIState),
152849ab747fSPaolo Bonzini         VMSTATE_UINT8(stopped_state, SDHCIState),
152959046ec2SHalil Pasic         VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz),
1530e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(insert_timer, SDHCIState),
1531e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(transfer_timer, SDHCIState),
153249ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
15330a7ac9f9SAndrew Baumann     },
1534307119baSRichard Henderson     .subsections = (const VMStateDescription * const []) {
15350a7ac9f9SAndrew Baumann         &sdhci_pending_insert_vmstate,
15360a7ac9f9SAndrew Baumann         NULL
15370a7ac9f9SAndrew Baumann     },
153849ab747fSPaolo Bonzini };
153949ab747fSPaolo Bonzini 
sdhci_common_class_init(ObjectClass * klass,void * data)1540ce864603SThomas Huth void sdhci_common_class_init(ObjectClass *klass, void *data)
15411c92c505SPhilippe Mathieu-Daudé {
15421c92c505SPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(klass);
15431c92c505SPhilippe Mathieu-Daudé 
15441c92c505SPhilippe Mathieu-Daudé     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
15451c92c505SPhilippe Mathieu-Daudé     dc->vmsd = &sdhci_vmstate;
1546e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, sdhci_poweron_reset);
15471c92c505SPhilippe Mathieu-Daudé }
15481c92c505SPhilippe Mathieu-Daudé 
1549b635d98cSPhilippe Mathieu-Daudé /* --- qdev SysBus --- */
1550b635d98cSPhilippe Mathieu-Daudé 
15515ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = {
1552b635d98cSPhilippe Mathieu-Daudé     DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState),
15530a7ac9f9SAndrew Baumann     DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk,
15540a7ac9f9SAndrew Baumann                      false),
155560765b6cSPhilippe Mathieu-Daudé     DEFINE_PROP_LINK("dma", SDHCIState,
155660765b6cSPhilippe Mathieu-Daudé                      dma_mr, TYPE_MEMORY_REGION, MemoryRegion *),
1557*806ab537SJamin Lin     DEFINE_PROP_BOOL("wp-inverted", SDHCIState,
1558*806ab537SJamin Lin                      wp_inverted, false),
15595ec911c3SKevin O'Connor     DEFINE_PROP_END_OF_LIST(),
15605ec911c3SKevin O'Connor };
15615ec911c3SKevin O'Connor 
sdhci_sysbus_init(Object * obj)15627302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj)
156349ab747fSPaolo Bonzini {
15647302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
15655ec911c3SKevin O'Connor 
156640bbc194SPeter Maydell     sdhci_initfn(s);
15677302dcd6SKevin O'Connor }
15687302dcd6SKevin O'Connor 
sdhci_sysbus_finalize(Object * obj)15697302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj)
15707302dcd6SKevin O'Connor {
15717302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
157260765b6cSPhilippe Mathieu-Daudé 
157360765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
157460765b6cSPhilippe Mathieu-Daudé         object_unparent(OBJECT(s->dma_mr));
157560765b6cSPhilippe Mathieu-Daudé     }
157660765b6cSPhilippe Mathieu-Daudé 
15777302dcd6SKevin O'Connor     sdhci_uninitfn(s);
15787302dcd6SKevin O'Connor }
15797302dcd6SKevin O'Connor 
sdhci_sysbus_realize(DeviceState * dev,Error ** errp)15807302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error **errp)
15817302dcd6SKevin O'Connor {
1582de1b3800SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
15837302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(dev);
158449ab747fSPaolo Bonzini     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
158549ab747fSPaolo Bonzini 
1586de1b3800SVladimir Sementsov-Ogievskiy     sdhci_common_realize(s, errp);
1587de1b3800SVladimir Sementsov-Ogievskiy     if (*errp) {
158825367498SPhilippe Mathieu-Daudé         return;
158925367498SPhilippe Mathieu-Daudé     }
159025367498SPhilippe Mathieu-Daudé 
159160765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
159202e57e1cSPhilippe Mathieu-Daudé         s->dma_as = &s->sysbus_dma_as;
159360765b6cSPhilippe Mathieu-Daudé         address_space_init(s->dma_as, s->dma_mr, "sdhci-dma");
159460765b6cSPhilippe Mathieu-Daudé     } else {
159560765b6cSPhilippe Mathieu-Daudé         /* use system_memory() if property "dma" not set */
1596dd55c485SPhilippe Mathieu-Daudé         s->dma_as = &address_space_memory;
159760765b6cSPhilippe Mathieu-Daudé     }
1598dd55c485SPhilippe Mathieu-Daudé 
159949ab747fSPaolo Bonzini     sysbus_init_irq(sbd, &s->irq);
1600fd1e5c81SAndrey Smirnov 
160149ab747fSPaolo Bonzini     sysbus_init_mmio(sbd, &s->iomem);
160249ab747fSPaolo Bonzini }
160349ab747fSPaolo Bonzini 
sdhci_sysbus_unrealize(DeviceState * dev)1604b69c3c21SMarkus Armbruster static void sdhci_sysbus_unrealize(DeviceState *dev)
16058b7455c7SPhilippe Mathieu-Daudé {
16068b7455c7SPhilippe Mathieu-Daudé     SDHCIState *s = SYSBUS_SDHCI(dev);
16078b7455c7SPhilippe Mathieu-Daudé 
1608b69c3c21SMarkus Armbruster     sdhci_common_unrealize(s);
160960765b6cSPhilippe Mathieu-Daudé 
161060765b6cSPhilippe Mathieu-Daudé      if (s->dma_mr) {
161160765b6cSPhilippe Mathieu-Daudé         address_space_destroy(s->dma_as);
161260765b6cSPhilippe Mathieu-Daudé     }
16138b7455c7SPhilippe Mathieu-Daudé }
16148b7455c7SPhilippe Mathieu-Daudé 
sdhci_sysbus_class_init(ObjectClass * klass,void * data)16157302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)
161649ab747fSPaolo Bonzini {
161749ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
161849ab747fSPaolo Bonzini 
16194f67d30bSMarc-André Lureau     device_class_set_props(dc, sdhci_sysbus_properties);
16207302dcd6SKevin O'Connor     dc->realize = sdhci_sysbus_realize;
16218b7455c7SPhilippe Mathieu-Daudé     dc->unrealize = sdhci_sysbus_unrealize;
16221c92c505SPhilippe Mathieu-Daudé 
16231c92c505SPhilippe Mathieu-Daudé     sdhci_common_class_init(klass, data);
162449ab747fSPaolo Bonzini }
162549ab747fSPaolo Bonzini 
1626b635d98cSPhilippe Mathieu-Daudé /* --- qdev bus master --- */
1627b635d98cSPhilippe Mathieu-Daudé 
sdhci_bus_class_init(ObjectClass * klass,void * data)162840bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data)
162940bbc194SPeter Maydell {
163040bbc194SPeter Maydell     SDBusClass *sbc = SD_BUS_CLASS(klass);
163140bbc194SPeter Maydell 
163240bbc194SPeter Maydell     sbc->set_inserted = sdhci_set_inserted;
163340bbc194SPeter Maydell     sbc->set_readonly = sdhci_set_readonly;
163440bbc194SPeter Maydell }
163540bbc194SPeter Maydell 
1636efadc818SPhilippe Mathieu-Daudé /* --- qdev i.MX eSDHC --- */
1637efadc818SPhilippe Mathieu-Daudé 
16381e76667fSBernhard Beschow #define USDHC_MIX_CTRL                  0x48
1639c038e574SBernhard Beschow 
16401e76667fSBernhard Beschow #define USDHC_VENDOR_SPEC               0xc0
16411e76667fSBernhard Beschow #define USDHC_IMX_FRC_SDCLK_ON          (1 << 8)
1642c038e574SBernhard Beschow 
16431e76667fSBernhard Beschow #define USDHC_DLL_CTRL                  0x60
1644c038e574SBernhard Beschow 
16451e76667fSBernhard Beschow #define USDHC_TUNING_CTRL               0xcc
16461e76667fSBernhard Beschow #define USDHC_TUNE_CTRL_STATUS          0x68
16471e76667fSBernhard Beschow #define USDHC_WTMK_LVL                  0x44
1648c038e574SBernhard Beschow 
1649c038e574SBernhard Beschow /* Undocumented register used by guests working around erratum ERR004536 */
16501e76667fSBernhard Beschow #define USDHC_UNDOCUMENTED_REG27        0x6c
1651c038e574SBernhard Beschow 
16521e76667fSBernhard Beschow #define USDHC_CTRL_4BITBUS              (0x1 << 1)
16531e76667fSBernhard Beschow #define USDHC_CTRL_8BITBUS              (0x2 << 1)
1654c038e574SBernhard Beschow 
16551e76667fSBernhard Beschow #define USDHC_PRNSTS_SDSTB              (1 << 3)
1656c038e574SBernhard Beschow 
usdhc_read(void * opaque,hwaddr offset,unsigned size)1657fd1e5c81SAndrey Smirnov static uint64_t usdhc_read(void *opaque, hwaddr offset, unsigned size)
1658fd1e5c81SAndrey Smirnov {
1659fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
1660fd1e5c81SAndrey Smirnov     uint32_t ret;
166106c5120bSPhilippe Mathieu-Daudé     uint16_t hostctl1;
1662fd1e5c81SAndrey Smirnov 
1663fd1e5c81SAndrey Smirnov     switch (offset) {
1664fd1e5c81SAndrey Smirnov     default:
1665fd1e5c81SAndrey Smirnov         return sdhci_read(opaque, offset, size);
1666fd1e5c81SAndrey Smirnov 
1667fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1668fd1e5c81SAndrey Smirnov         /*
1669fd1e5c81SAndrey Smirnov          * For a detailed explanation on the following bit
1670fd1e5c81SAndrey Smirnov          * manipulation code see comments in a similar part of
1671fd1e5c81SAndrey Smirnov          * usdhc_write()
1672fd1e5c81SAndrey Smirnov          */
167306c5120bSPhilippe Mathieu-Daudé         hostctl1 = SDHC_DMA_TYPE(s->hostctl1) << (8 - 3);
1674fd1e5c81SAndrey Smirnov 
167506c5120bSPhilippe Mathieu-Daudé         if (s->hostctl1 & SDHC_CTRL_8BITBUS) {
16761e76667fSBernhard Beschow             hostctl1 |= USDHC_CTRL_8BITBUS;
1677fd1e5c81SAndrey Smirnov         }
1678fd1e5c81SAndrey Smirnov 
167906c5120bSPhilippe Mathieu-Daudé         if (s->hostctl1 & SDHC_CTRL_4BITBUS) {
16801e76667fSBernhard Beschow             hostctl1 |= USDHC_CTRL_4BITBUS;
1681fd1e5c81SAndrey Smirnov         }
1682fd1e5c81SAndrey Smirnov 
168306c5120bSPhilippe Mathieu-Daudé         ret  = hostctl1;
1684fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->blkgap << 16;
1685fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->wakcon << 24;
1686fd1e5c81SAndrey Smirnov 
1687fd1e5c81SAndrey Smirnov         break;
1688fd1e5c81SAndrey Smirnov 
16896bfd06daSHans-Erik Floryd     case SDHC_PRNSTS:
16906bfd06daSHans-Erik Floryd         /* Add SDSTB (SD Clock Stable) bit to PRNSTS */
16911e76667fSBernhard Beschow         ret = sdhci_read(opaque, offset, size) & ~USDHC_PRNSTS_SDSTB;
16926bfd06daSHans-Erik Floryd         if (s->clkcon & SDHC_CLOCK_INT_STABLE) {
16931e76667fSBernhard Beschow             ret |= USDHC_PRNSTS_SDSTB;
16946bfd06daSHans-Erik Floryd         }
16956bfd06daSHans-Erik Floryd         break;
16966bfd06daSHans-Erik Floryd 
16971e76667fSBernhard Beschow     case USDHC_VENDOR_SPEC:
16983b2d8176SGuenter Roeck         ret = s->vendor_spec;
16993b2d8176SGuenter Roeck         break;
17001e76667fSBernhard Beschow     case USDHC_DLL_CTRL:
17011e76667fSBernhard Beschow     case USDHC_TUNE_CTRL_STATUS:
17021e76667fSBernhard Beschow     case USDHC_UNDOCUMENTED_REG27:
17031e76667fSBernhard Beschow     case USDHC_TUNING_CTRL:
17041e76667fSBernhard Beschow     case USDHC_MIX_CTRL:
17051e76667fSBernhard Beschow     case USDHC_WTMK_LVL:
1706fd1e5c81SAndrey Smirnov         ret = 0;
1707fd1e5c81SAndrey Smirnov         break;
1708fd1e5c81SAndrey Smirnov     }
1709fd1e5c81SAndrey Smirnov 
1710fd1e5c81SAndrey Smirnov     return ret;
1711fd1e5c81SAndrey Smirnov }
1712fd1e5c81SAndrey Smirnov 
1713fd1e5c81SAndrey Smirnov static void
usdhc_write(void * opaque,hwaddr offset,uint64_t val,unsigned size)1714fd1e5c81SAndrey Smirnov usdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
1715fd1e5c81SAndrey Smirnov {
1716fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
171706c5120bSPhilippe Mathieu-Daudé     uint8_t hostctl1;
1718fd1e5c81SAndrey Smirnov     uint32_t value = (uint32_t)val;
1719fd1e5c81SAndrey Smirnov 
1720fd1e5c81SAndrey Smirnov     switch (offset) {
17211e76667fSBernhard Beschow     case USDHC_DLL_CTRL:
17221e76667fSBernhard Beschow     case USDHC_TUNE_CTRL_STATUS:
17231e76667fSBernhard Beschow     case USDHC_UNDOCUMENTED_REG27:
17241e76667fSBernhard Beschow     case USDHC_TUNING_CTRL:
17251e76667fSBernhard Beschow     case USDHC_WTMK_LVL:
17263b2d8176SGuenter Roeck         break;
17273b2d8176SGuenter Roeck 
17281e76667fSBernhard Beschow     case USDHC_VENDOR_SPEC:
17293b2d8176SGuenter Roeck         s->vendor_spec = value;
17303b2d8176SGuenter Roeck         switch (s->vendor) {
17313b2d8176SGuenter Roeck         case SDHCI_VENDOR_IMX:
17321e76667fSBernhard Beschow             if (value & USDHC_IMX_FRC_SDCLK_ON) {
17333b2d8176SGuenter Roeck                 s->prnsts &= ~SDHC_IMX_CLOCK_GATE_OFF;
17343b2d8176SGuenter Roeck             } else {
17353b2d8176SGuenter Roeck                 s->prnsts |= SDHC_IMX_CLOCK_GATE_OFF;
17363b2d8176SGuenter Roeck             }
17373b2d8176SGuenter Roeck             break;
17383b2d8176SGuenter Roeck         default:
17393b2d8176SGuenter Roeck             break;
17403b2d8176SGuenter Roeck         }
1741fd1e5c81SAndrey Smirnov         break;
1742fd1e5c81SAndrey Smirnov 
1743fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1744fd1e5c81SAndrey Smirnov         /*
1745fd1e5c81SAndrey Smirnov          * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL)
1746fd1e5c81SAndrey Smirnov          *
1747fd1e5c81SAndrey Smirnov          *       7         6     5      4      3      2        1      0
1748fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1749fd1e5c81SAndrey Smirnov          * | Card      | Card   | Endian | DATA3     | Data     | Led     |
1750fd1e5c81SAndrey Smirnov          * | Detect    | Detect | Mode   | as Card   | Transfer | Control |
1751fd1e5c81SAndrey Smirnov          * | Signal    | Test   |        | Detection | Width    |         |
1752fd1e5c81SAndrey Smirnov          * | Selection | Level  |        | Pin       |          |         |
1753fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1754fd1e5c81SAndrey Smirnov          *
1755fd1e5c81SAndrey Smirnov          * and 0x29
1756fd1e5c81SAndrey Smirnov          *
1757fd1e5c81SAndrey Smirnov          *  15      10 9    8
1758fd1e5c81SAndrey Smirnov          * |----------+------|
1759fd1e5c81SAndrey Smirnov          * | Reserved | DMA  |
1760fd1e5c81SAndrey Smirnov          * |          | Sel. |
1761fd1e5c81SAndrey Smirnov          * |          |      |
1762fd1e5c81SAndrey Smirnov          * |----------+------|
1763fd1e5c81SAndrey Smirnov          *
1764fd1e5c81SAndrey Smirnov          * and here's what SDCHI spec expects those offsets to be:
1765fd1e5c81SAndrey Smirnov          *
1766fd1e5c81SAndrey Smirnov          * 0x28 (Host Control Register)
1767fd1e5c81SAndrey Smirnov          *
1768fd1e5c81SAndrey Smirnov          *     7        6         5       4  3      2         1        0
1769fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1770fd1e5c81SAndrey Smirnov          * | Card   | Card   | Extended | DMA  | High   | Data     | LED     |
1771fd1e5c81SAndrey Smirnov          * | Detect | Detect | Data     | Sel. | Speed  | Transfer | Control |
1772fd1e5c81SAndrey Smirnov          * | Signal | Test   | Transfer |      | Enable | Width    |         |
1773fd1e5c81SAndrey Smirnov          * | Sel.   | Level  | Width    |      |        |          |         |
1774fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1775fd1e5c81SAndrey Smirnov          *
1776fd1e5c81SAndrey Smirnov          * and 0x29 (Power Control Register)
1777fd1e5c81SAndrey Smirnov          *
1778fd1e5c81SAndrey Smirnov          * |----------------------------------|
1779fd1e5c81SAndrey Smirnov          * | Power Control Register           |
1780fd1e5c81SAndrey Smirnov          * |                                  |
1781fd1e5c81SAndrey Smirnov          * | Description omitted,             |
1782fd1e5c81SAndrey Smirnov          * | since it has no analog in ESDHCI |
1783fd1e5c81SAndrey Smirnov          * |                                  |
1784fd1e5c81SAndrey Smirnov          * |----------------------------------|
1785fd1e5c81SAndrey Smirnov          *
1786fd1e5c81SAndrey Smirnov          * Since offsets 0x2A and 0x2B should be compatible between
1787fd1e5c81SAndrey Smirnov          * both IP specs we only need to reconcile least 16-bit of the
1788fd1e5c81SAndrey Smirnov          * word we've been given.
1789fd1e5c81SAndrey Smirnov          */
1790fd1e5c81SAndrey Smirnov 
1791fd1e5c81SAndrey Smirnov         /*
1792fd1e5c81SAndrey Smirnov          * First, save bits 7 6 and 0 since they are identical
1793fd1e5c81SAndrey Smirnov          */
179406c5120bSPhilippe Mathieu-Daudé         hostctl1 = value & (SDHC_CTRL_LED |
1795fd1e5c81SAndrey Smirnov                             SDHC_CTRL_CDTEST_INS |
1796fd1e5c81SAndrey Smirnov                             SDHC_CTRL_CDTEST_EN);
1797fd1e5c81SAndrey Smirnov         /*
1798fd1e5c81SAndrey Smirnov          * Second, split "Data Transfer Width" from bits 2 and 1 in to
1799fd1e5c81SAndrey Smirnov          * bits 5 and 1
1800fd1e5c81SAndrey Smirnov          */
18011e76667fSBernhard Beschow         if (value & USDHC_CTRL_8BITBUS) {
180206c5120bSPhilippe Mathieu-Daudé             hostctl1 |= SDHC_CTRL_8BITBUS;
1803fd1e5c81SAndrey Smirnov         }
1804fd1e5c81SAndrey Smirnov 
18051e76667fSBernhard Beschow         if (value & USDHC_CTRL_4BITBUS) {
18061e76667fSBernhard Beschow             hostctl1 |= USDHC_CTRL_4BITBUS;
1807fd1e5c81SAndrey Smirnov         }
1808fd1e5c81SAndrey Smirnov 
1809fd1e5c81SAndrey Smirnov         /*
1810fd1e5c81SAndrey Smirnov          * Third, move DMA select from bits 9 and 8 to bits 4 and 3
1811fd1e5c81SAndrey Smirnov          */
181206c5120bSPhilippe Mathieu-Daudé         hostctl1 |= SDHC_DMA_TYPE(value >> (8 - 3));
1813fd1e5c81SAndrey Smirnov 
1814fd1e5c81SAndrey Smirnov         /*
1815fd1e5c81SAndrey Smirnov          * Now place the corrected value into low 16-bit of the value
1816fd1e5c81SAndrey Smirnov          * we are going to give standard SDHCI write function
1817fd1e5c81SAndrey Smirnov          *
1818fd1e5c81SAndrey Smirnov          * NOTE: This transformation should be the inverse of what can
1819fd1e5c81SAndrey Smirnov          * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux
1820fd1e5c81SAndrey Smirnov          * kernel
1821fd1e5c81SAndrey Smirnov          */
1822fd1e5c81SAndrey Smirnov         value &= ~UINT16_MAX;
182306c5120bSPhilippe Mathieu-Daudé         value |= hostctl1;
1824fd1e5c81SAndrey Smirnov         value |= (uint16_t)s->pwrcon << 8;
1825fd1e5c81SAndrey Smirnov 
1826fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, value, size);
1827fd1e5c81SAndrey Smirnov         break;
1828fd1e5c81SAndrey Smirnov 
18291e76667fSBernhard Beschow     case USDHC_MIX_CTRL:
1830fd1e5c81SAndrey Smirnov         /*
1831fd1e5c81SAndrey Smirnov          * So, when SD/MMC stack in Linux tries to write to "Transfer
1832fd1e5c81SAndrey Smirnov          * Mode Register", ESDHC i.MX quirk code will translate it
1833fd1e5c81SAndrey Smirnov          * into a write to ESDHC_MIX_CTRL, so we do the opposite in
1834fd1e5c81SAndrey Smirnov          * order to get where we started
1835fd1e5c81SAndrey Smirnov          *
1836fd1e5c81SAndrey Smirnov          * Note that Auto CMD23 Enable bit is located in a wrong place
1837fd1e5c81SAndrey Smirnov          * on i.MX, but since it is not used by QEMU we do not care.
1838fd1e5c81SAndrey Smirnov          *
1839fd1e5c81SAndrey Smirnov          * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
1840b8d09982SMichael Tokarev          * here because it will result in a call to
1841fd1e5c81SAndrey Smirnov          * sdhci_send_command(s) which we don't want.
1842fd1e5c81SAndrey Smirnov          *
1843fd1e5c81SAndrey Smirnov          */
1844fd1e5c81SAndrey Smirnov         s->trnmod = value & UINT16_MAX;
1845fd1e5c81SAndrey Smirnov         break;
1846fd1e5c81SAndrey Smirnov     case SDHC_TRNMOD:
1847fd1e5c81SAndrey Smirnov         /*
1848fd1e5c81SAndrey Smirnov          * Similar to above, but this time a write to "Command
1849fd1e5c81SAndrey Smirnov          * Register" will be translated into a 4-byte write to
1850fd1e5c81SAndrey Smirnov          * "Transfer Mode register" where lower 16-bit of value would
1851fd1e5c81SAndrey Smirnov          * be set to zero. So what we do is fill those bits with
1852fd1e5c81SAndrey Smirnov          * cached value from s->trnmod and let the SDHCI
1853fd1e5c81SAndrey Smirnov          * infrastructure handle the rest
1854fd1e5c81SAndrey Smirnov          */
1855fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val | s->trnmod, size);
1856fd1e5c81SAndrey Smirnov         break;
1857fd1e5c81SAndrey Smirnov     case SDHC_BLKSIZE:
1858fd1e5c81SAndrey Smirnov         /*
1859fd1e5c81SAndrey Smirnov          * ESDHCI does not implement "Host SDMA Buffer Boundary", and
1860fd1e5c81SAndrey Smirnov          * Linux driver will try to zero this field out which will
1861fd1e5c81SAndrey Smirnov          * break the rest of SDHCI emulation.
1862fd1e5c81SAndrey Smirnov          *
1863fd1e5c81SAndrey Smirnov          * Linux defaults to maximum possible setting (512K boundary)
1864fd1e5c81SAndrey Smirnov          * and it seems to be the only option that i.MX IP implements,
1865fd1e5c81SAndrey Smirnov          * so we artificially set it to that value.
1866fd1e5c81SAndrey Smirnov          */
1867fd1e5c81SAndrey Smirnov         val |= 0x7 << 12;
1868fd1e5c81SAndrey Smirnov         /* FALLTHROUGH */
1869fd1e5c81SAndrey Smirnov     default:
1870fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val, size);
1871fd1e5c81SAndrey Smirnov         break;
1872fd1e5c81SAndrey Smirnov     }
1873fd1e5c81SAndrey Smirnov }
1874fd1e5c81SAndrey Smirnov 
1875fd1e5c81SAndrey Smirnov static const MemoryRegionOps usdhc_mmio_ops = {
1876fd1e5c81SAndrey Smirnov     .read = usdhc_read,
1877fd1e5c81SAndrey Smirnov     .write = usdhc_write,
1878fd1e5c81SAndrey Smirnov     .valid = {
1879fd1e5c81SAndrey Smirnov         .min_access_size = 1,
1880fd1e5c81SAndrey Smirnov         .max_access_size = 4,
1881fd1e5c81SAndrey Smirnov         .unaligned = false
1882fd1e5c81SAndrey Smirnov     },
1883fd1e5c81SAndrey Smirnov     .endianness = DEVICE_LITTLE_ENDIAN,
1884fd1e5c81SAndrey Smirnov };
1885fd1e5c81SAndrey Smirnov 
imx_usdhc_init(Object * obj)1886fd1e5c81SAndrey Smirnov static void imx_usdhc_init(Object *obj)
1887fd1e5c81SAndrey Smirnov {
1888fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(obj);
1889fd1e5c81SAndrey Smirnov 
1890fd1e5c81SAndrey Smirnov     s->io_ops = &usdhc_mmio_ops;
1891fd1e5c81SAndrey Smirnov     s->quirks = SDHCI_QUIRK_NO_BUSY_IRQ;
1892fd1e5c81SAndrey Smirnov }
1893fd1e5c81SAndrey Smirnov 
1894c85fba50SPhilippe Mathieu-Daudé /* --- qdev Samsung s3c --- */
1895c85fba50SPhilippe Mathieu-Daudé 
1896c85fba50SPhilippe Mathieu-Daudé #define S3C_SDHCI_CONTROL2      0x80
1897c85fba50SPhilippe Mathieu-Daudé #define S3C_SDHCI_CONTROL3      0x84
1898c85fba50SPhilippe Mathieu-Daudé #define S3C_SDHCI_CONTROL4      0x8c
1899c85fba50SPhilippe Mathieu-Daudé 
sdhci_s3c_read(void * opaque,hwaddr offset,unsigned size)1900c85fba50SPhilippe Mathieu-Daudé static uint64_t sdhci_s3c_read(void *opaque, hwaddr offset, unsigned size)
1901c85fba50SPhilippe Mathieu-Daudé {
1902c85fba50SPhilippe Mathieu-Daudé     uint64_t ret;
1903c85fba50SPhilippe Mathieu-Daudé 
1904c85fba50SPhilippe Mathieu-Daudé     switch (offset) {
1905c85fba50SPhilippe Mathieu-Daudé     case S3C_SDHCI_CONTROL2:
1906c85fba50SPhilippe Mathieu-Daudé     case S3C_SDHCI_CONTROL3:
1907c85fba50SPhilippe Mathieu-Daudé     case S3C_SDHCI_CONTROL4:
1908c85fba50SPhilippe Mathieu-Daudé         /* ignore */
1909c85fba50SPhilippe Mathieu-Daudé         ret = 0;
1910c85fba50SPhilippe Mathieu-Daudé         break;
1911c85fba50SPhilippe Mathieu-Daudé     default:
1912c85fba50SPhilippe Mathieu-Daudé         ret = sdhci_read(opaque, offset, size);
1913c85fba50SPhilippe Mathieu-Daudé         break;
1914c85fba50SPhilippe Mathieu-Daudé     }
1915c85fba50SPhilippe Mathieu-Daudé 
1916c85fba50SPhilippe Mathieu-Daudé     return ret;
1917c85fba50SPhilippe Mathieu-Daudé }
1918c85fba50SPhilippe Mathieu-Daudé 
sdhci_s3c_write(void * opaque,hwaddr offset,uint64_t val,unsigned size)1919c85fba50SPhilippe Mathieu-Daudé static void sdhci_s3c_write(void *opaque, hwaddr offset, uint64_t val,
1920c85fba50SPhilippe Mathieu-Daudé                             unsigned size)
1921c85fba50SPhilippe Mathieu-Daudé {
1922c85fba50SPhilippe Mathieu-Daudé     switch (offset) {
1923c85fba50SPhilippe Mathieu-Daudé     case S3C_SDHCI_CONTROL2:
1924c85fba50SPhilippe Mathieu-Daudé     case S3C_SDHCI_CONTROL3:
1925c85fba50SPhilippe Mathieu-Daudé     case S3C_SDHCI_CONTROL4:
1926c85fba50SPhilippe Mathieu-Daudé         /* ignore */
1927c85fba50SPhilippe Mathieu-Daudé         break;
1928c85fba50SPhilippe Mathieu-Daudé     default:
1929c85fba50SPhilippe Mathieu-Daudé         sdhci_write(opaque, offset, val, size);
1930c85fba50SPhilippe Mathieu-Daudé         break;
1931c85fba50SPhilippe Mathieu-Daudé     }
1932c85fba50SPhilippe Mathieu-Daudé }
1933c85fba50SPhilippe Mathieu-Daudé 
1934c85fba50SPhilippe Mathieu-Daudé static const MemoryRegionOps sdhci_s3c_mmio_ops = {
1935c85fba50SPhilippe Mathieu-Daudé     .read = sdhci_s3c_read,
1936c85fba50SPhilippe Mathieu-Daudé     .write = sdhci_s3c_write,
1937c85fba50SPhilippe Mathieu-Daudé     .valid = {
1938c85fba50SPhilippe Mathieu-Daudé         .min_access_size = 1,
1939c85fba50SPhilippe Mathieu-Daudé         .max_access_size = 4,
1940c85fba50SPhilippe Mathieu-Daudé         .unaligned = false
1941c85fba50SPhilippe Mathieu-Daudé     },
1942c85fba50SPhilippe Mathieu-Daudé     .endianness = DEVICE_LITTLE_ENDIAN,
1943c85fba50SPhilippe Mathieu-Daudé };
1944c85fba50SPhilippe Mathieu-Daudé 
sdhci_s3c_init(Object * obj)1945c85fba50SPhilippe Mathieu-Daudé static void sdhci_s3c_init(Object *obj)
1946c85fba50SPhilippe Mathieu-Daudé {
1947c85fba50SPhilippe Mathieu-Daudé     SDHCIState *s = SYSBUS_SDHCI(obj);
1948c85fba50SPhilippe Mathieu-Daudé 
1949c85fba50SPhilippe Mathieu-Daudé     s->io_ops = &sdhci_s3c_mmio_ops;
1950c85fba50SPhilippe Mathieu-Daudé }
1951c85fba50SPhilippe Mathieu-Daudé 
1952911f4dd8SBernhard Beschow static const TypeInfo sdhci_types[] = {
1953911f4dd8SBernhard Beschow     {
1954911f4dd8SBernhard Beschow         .name = TYPE_SDHCI_BUS,
1955911f4dd8SBernhard Beschow         .parent = TYPE_SD_BUS,
1956911f4dd8SBernhard Beschow         .instance_size = sizeof(SDBus),
1957911f4dd8SBernhard Beschow         .class_init = sdhci_bus_class_init,
1958911f4dd8SBernhard Beschow     },
1959911f4dd8SBernhard Beschow     {
1960911f4dd8SBernhard Beschow         .name = TYPE_SYSBUS_SDHCI,
1961911f4dd8SBernhard Beschow         .parent = TYPE_SYS_BUS_DEVICE,
1962911f4dd8SBernhard Beschow         .instance_size = sizeof(SDHCIState),
1963911f4dd8SBernhard Beschow         .instance_init = sdhci_sysbus_init,
1964911f4dd8SBernhard Beschow         .instance_finalize = sdhci_sysbus_finalize,
1965911f4dd8SBernhard Beschow         .class_init = sdhci_sysbus_class_init,
1966911f4dd8SBernhard Beschow     },
1967911f4dd8SBernhard Beschow     {
1968911f4dd8SBernhard Beschow         .name = TYPE_IMX_USDHC,
1969911f4dd8SBernhard Beschow         .parent = TYPE_SYSBUS_SDHCI,
1970911f4dd8SBernhard Beschow         .instance_init = imx_usdhc_init,
1971911f4dd8SBernhard Beschow     },
1972911f4dd8SBernhard Beschow     {
1973c85fba50SPhilippe Mathieu-Daudé         .name = TYPE_S3C_SDHCI,
1974c85fba50SPhilippe Mathieu-Daudé         .parent = TYPE_SYSBUS_SDHCI,
1975c85fba50SPhilippe Mathieu-Daudé         .instance_init = sdhci_s3c_init,
1976911f4dd8SBernhard Beschow     },
1977c85fba50SPhilippe Mathieu-Daudé };
1978c85fba50SPhilippe Mathieu-Daudé 
1979911f4dd8SBernhard Beschow DEFINE_TYPES(sdhci_types)
1980