xref: /openbmc/qemu/hw/sd/sdhci.c (revision 59046ec29ad4c24391bb9fe1fbdced33557aaa70)
149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini  * SD Association Host Standard Specification v2.0 controller emulation
349ab747fSPaolo Bonzini  *
449ab747fSPaolo Bonzini  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
549ab747fSPaolo Bonzini  * Mitsyanko Igor <i.mitsyanko@samsung.com>
649ab747fSPaolo Bonzini  * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com>
749ab747fSPaolo Bonzini  *
849ab747fSPaolo Bonzini  * Based on MMC controller for Samsung S5PC1xx-based board emulation
949ab747fSPaolo Bonzini  * by Alexey Merkulov and Vladimir Monakhov.
1049ab747fSPaolo Bonzini  *
1149ab747fSPaolo Bonzini  * This program is free software; you can redistribute it and/or modify it
1249ab747fSPaolo Bonzini  * under the terms of the GNU General Public License as published by the
1349ab747fSPaolo Bonzini  * Free Software Foundation; either version 2 of the License, or (at your
1449ab747fSPaolo Bonzini  * option) any later version.
1549ab747fSPaolo Bonzini  *
1649ab747fSPaolo Bonzini  * This program is distributed in the hope that it will be useful,
1749ab747fSPaolo Bonzini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1849ab747fSPaolo Bonzini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1949ab747fSPaolo Bonzini  * See the GNU General Public License for more details.
2049ab747fSPaolo Bonzini  *
2149ab747fSPaolo Bonzini  * You should have received a copy of the GNU General Public License along
2249ab747fSPaolo Bonzini  * with this program; if not, see <http://www.gnu.org/licenses/>.
2349ab747fSPaolo Bonzini  */
2449ab747fSPaolo Bonzini 
250430891cSPeter Maydell #include "qemu/osdep.h"
2649ab747fSPaolo Bonzini #include "hw/hw.h"
27fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
2849ab747fSPaolo Bonzini #include "sysemu/blockdev.h"
2949ab747fSPaolo Bonzini #include "sysemu/dma.h"
3049ab747fSPaolo Bonzini #include "qemu/timer.h"
3149ab747fSPaolo Bonzini #include "qemu/bitops.h"
32637d23beSSai Pavan Boddu #include "sdhci-internal.h"
3303dd024fSPaolo Bonzini #include "qemu/log.h"
3449ab747fSPaolo Bonzini 
3549ab747fSPaolo Bonzini /* host controller debug messages */
3649ab747fSPaolo Bonzini #ifndef SDHC_DEBUG
3749ab747fSPaolo Bonzini #define SDHC_DEBUG                        0
3849ab747fSPaolo Bonzini #endif
3949ab747fSPaolo Bonzini 
4049ab747fSPaolo Bonzini #define DPRINT_L1(fmt, args...) \
417af0fc99SSai Pavan Boddu     do { \
427af0fc99SSai Pavan Boddu         if (SDHC_DEBUG) { \
437af0fc99SSai Pavan Boddu             fprintf(stderr, "QEMU SDHC: " fmt, ## args); \
447af0fc99SSai Pavan Boddu         } \
457af0fc99SSai Pavan Boddu     } while (0)
4649ab747fSPaolo Bonzini #define DPRINT_L2(fmt, args...) \
477af0fc99SSai Pavan Boddu     do { \
487af0fc99SSai Pavan Boddu         if (SDHC_DEBUG > 1) { \
497af0fc99SSai Pavan Boddu             fprintf(stderr, "QEMU SDHC: " fmt, ## args); \
507af0fc99SSai Pavan Boddu         } \
517af0fc99SSai Pavan Boddu     } while (0)
5249ab747fSPaolo Bonzini #define ERRPRINT(fmt, args...) \
537af0fc99SSai Pavan Boddu     do { \
547af0fc99SSai Pavan Boddu         if (SDHC_DEBUG) { \
557af0fc99SSai Pavan Boddu             fprintf(stderr, "QEMU SDHC ERROR: " fmt, ## args); \
567af0fc99SSai Pavan Boddu         } \
577af0fc99SSai Pavan Boddu     } while (0)
5849ab747fSPaolo Bonzini 
5940bbc194SPeter Maydell #define TYPE_SDHCI_BUS "sdhci-bus"
6040bbc194SPeter Maydell #define SDHCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SDHCI_BUS)
6140bbc194SPeter Maydell 
6249ab747fSPaolo Bonzini /* Default SD/MMC host controller features information, which will be
6349ab747fSPaolo Bonzini  * presented in CAPABILITIES register of generic SD host controller at reset.
6449ab747fSPaolo Bonzini  * If not stated otherwise:
6549ab747fSPaolo Bonzini  * 0 - not supported, 1 - supported, other - prohibited.
6649ab747fSPaolo Bonzini  */
6749ab747fSPaolo Bonzini #define SDHC_CAPAB_64BITBUS       0ul        /* 64-bit System Bus Support */
6849ab747fSPaolo Bonzini #define SDHC_CAPAB_18V            1ul        /* Voltage support 1.8v */
6949ab747fSPaolo Bonzini #define SDHC_CAPAB_30V            0ul        /* Voltage support 3.0v */
7049ab747fSPaolo Bonzini #define SDHC_CAPAB_33V            1ul        /* Voltage support 3.3v */
7149ab747fSPaolo Bonzini #define SDHC_CAPAB_SUSPRESUME     0ul        /* Suspend/resume support */
7249ab747fSPaolo Bonzini #define SDHC_CAPAB_SDMA           1ul        /* SDMA support */
7349ab747fSPaolo Bonzini #define SDHC_CAPAB_HIGHSPEED      1ul        /* High speed support */
7449ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA1          1ul        /* ADMA1 support */
7549ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA2          1ul        /* ADMA2 support */
7649ab747fSPaolo Bonzini /* Maximum host controller R/W buffers size
7749ab747fSPaolo Bonzini  * Possible values: 512, 1024, 2048 bytes */
7849ab747fSPaolo Bonzini #define SDHC_CAPAB_MAXBLOCKLENGTH 512ul
7949ab747fSPaolo Bonzini /* Maximum clock frequency for SDclock in MHz
8049ab747fSPaolo Bonzini  * value in range 10-63 MHz, 0 - not defined */
81c7ff8daaSKevin O'Connor #define SDHC_CAPAB_BASECLKFREQ    52ul
8249ab747fSPaolo Bonzini #define SDHC_CAPAB_TOUNIT         1ul  /* Timeout clock unit 0 - kHz, 1 - MHz */
8349ab747fSPaolo Bonzini /* Timeout clock frequency 1-63, 0 - not defined */
84c7ff8daaSKevin O'Connor #define SDHC_CAPAB_TOCLKFREQ      52ul
8549ab747fSPaolo Bonzini 
8649ab747fSPaolo Bonzini /* Now check all parameters and calculate CAPABILITIES REGISTER value */
8749ab747fSPaolo Bonzini #if SDHC_CAPAB_64BITBUS > 1 || SDHC_CAPAB_18V > 1 || SDHC_CAPAB_30V > 1 ||     \
8849ab747fSPaolo Bonzini     SDHC_CAPAB_33V > 1 || SDHC_CAPAB_SUSPRESUME > 1 || SDHC_CAPAB_SDMA > 1 ||  \
8949ab747fSPaolo Bonzini     SDHC_CAPAB_HIGHSPEED > 1 || SDHC_CAPAB_ADMA2 > 1 || SDHC_CAPAB_ADMA1 > 1 ||\
9049ab747fSPaolo Bonzini     SDHC_CAPAB_TOUNIT > 1
9149ab747fSPaolo Bonzini #error Capabilities features can have value 0 or 1 only!
9249ab747fSPaolo Bonzini #endif
9349ab747fSPaolo Bonzini 
9449ab747fSPaolo Bonzini #if SDHC_CAPAB_MAXBLOCKLENGTH == 512
9549ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 0ul
9649ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 1024
9749ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 1ul
9849ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 2048
9949ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 2ul
10049ab747fSPaolo Bonzini #else
10149ab747fSPaolo Bonzini #error Max host controller block size can have value 512, 1024 or 2048 only!
10249ab747fSPaolo Bonzini #endif
10349ab747fSPaolo Bonzini 
10449ab747fSPaolo Bonzini #if (SDHC_CAPAB_BASECLKFREQ > 0 && SDHC_CAPAB_BASECLKFREQ < 10) || \
10549ab747fSPaolo Bonzini     SDHC_CAPAB_BASECLKFREQ > 63
10649ab747fSPaolo Bonzini #error SDclock frequency can have value in range 0, 10-63 only!
10749ab747fSPaolo Bonzini #endif
10849ab747fSPaolo Bonzini 
10949ab747fSPaolo Bonzini #if SDHC_CAPAB_TOCLKFREQ > 63
11049ab747fSPaolo Bonzini #error Timeout clock frequency can have value in range 0-63 only!
11149ab747fSPaolo Bonzini #endif
11249ab747fSPaolo Bonzini 
11349ab747fSPaolo Bonzini #define SDHC_CAPAB_REG_DEFAULT                                 \
11449ab747fSPaolo Bonzini    ((SDHC_CAPAB_64BITBUS << 28) | (SDHC_CAPAB_18V << 26) |     \
11549ab747fSPaolo Bonzini     (SDHC_CAPAB_30V << 25) | (SDHC_CAPAB_33V << 24) |          \
11649ab747fSPaolo Bonzini     (SDHC_CAPAB_SUSPRESUME << 23) | (SDHC_CAPAB_SDMA << 22) |  \
11749ab747fSPaolo Bonzini     (SDHC_CAPAB_HIGHSPEED << 21) | (SDHC_CAPAB_ADMA1 << 20) |  \
11849ab747fSPaolo Bonzini     (SDHC_CAPAB_ADMA2 << 19) | (MAX_BLOCK_LENGTH << 16) |      \
11949ab747fSPaolo Bonzini     (SDHC_CAPAB_BASECLKFREQ << 8) | (SDHC_CAPAB_TOUNIT << 7) | \
12049ab747fSPaolo Bonzini     (SDHC_CAPAB_TOCLKFREQ))
12149ab747fSPaolo Bonzini 
12249ab747fSPaolo Bonzini #define MASKED_WRITE(reg, mask, val)  (reg = (reg & (mask)) | (val))
12349ab747fSPaolo Bonzini 
12449ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s)
12549ab747fSPaolo Bonzini {
12649ab747fSPaolo Bonzini     return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) ||
12749ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) ||
12849ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV));
12949ab747fSPaolo Bonzini }
13049ab747fSPaolo Bonzini 
13149ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s)
13249ab747fSPaolo Bonzini {
13349ab747fSPaolo Bonzini     qemu_set_irq(s->irq, sdhci_slotint(s));
13449ab747fSPaolo Bonzini }
13549ab747fSPaolo Bonzini 
13649ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque)
13749ab747fSPaolo Bonzini {
13849ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
13949ab747fSPaolo Bonzini 
14049ab747fSPaolo Bonzini     if (s->norintsts & SDHC_NIS_REMOVE) {
141bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
142bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
14349ab747fSPaolo Bonzini     } else {
14449ab747fSPaolo Bonzini         s->prnsts = 0x1ff0000;
14549ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_INSERT) {
14649ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_INSERT;
14749ab747fSPaolo Bonzini         }
14849ab747fSPaolo Bonzini         sdhci_update_irq(s);
14949ab747fSPaolo Bonzini     }
15049ab747fSPaolo Bonzini }
15149ab747fSPaolo Bonzini 
15240bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level)
15349ab747fSPaolo Bonzini {
15440bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
15549ab747fSPaolo Bonzini     DPRINT_L1("Card state changed: %s!\n", level ? "insert" : "eject");
15649ab747fSPaolo Bonzini 
15749ab747fSPaolo Bonzini     if ((s->norintsts & SDHC_NIS_REMOVE) && level) {
15849ab747fSPaolo Bonzini         /* Give target some time to notice card ejection */
159bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
160bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
16149ab747fSPaolo Bonzini     } else {
16249ab747fSPaolo Bonzini         if (level) {
16349ab747fSPaolo Bonzini             s->prnsts = 0x1ff0000;
16449ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_INSERT) {
16549ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_INSERT;
16649ab747fSPaolo Bonzini             }
16749ab747fSPaolo Bonzini         } else {
16849ab747fSPaolo Bonzini             s->prnsts = 0x1fa0000;
16949ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
17049ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_SDCLK_EN;
17149ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_REMOVE) {
17249ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_REMOVE;
17349ab747fSPaolo Bonzini             }
17449ab747fSPaolo Bonzini         }
17549ab747fSPaolo Bonzini         sdhci_update_irq(s);
17649ab747fSPaolo Bonzini     }
17749ab747fSPaolo Bonzini }
17849ab747fSPaolo Bonzini 
17940bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level)
18049ab747fSPaolo Bonzini {
18140bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
18249ab747fSPaolo Bonzini 
18349ab747fSPaolo Bonzini     if (level) {
18449ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_WRITE_PROTECT;
18549ab747fSPaolo Bonzini     } else {
18649ab747fSPaolo Bonzini         /* Write enabled */
18749ab747fSPaolo Bonzini         s->prnsts |= SDHC_WRITE_PROTECT;
18849ab747fSPaolo Bonzini     }
18949ab747fSPaolo Bonzini }
19049ab747fSPaolo Bonzini 
19149ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s)
19249ab747fSPaolo Bonzini {
19340bbc194SPeter Maydell     DeviceState *dev = DEVICE(s);
19440bbc194SPeter Maydell 
195bc72ad67SAlex Bligh     timer_del(s->insert_timer);
196bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
19749ab747fSPaolo Bonzini     /* Set all registers to 0. Capabilities registers are not cleared
19849ab747fSPaolo Bonzini      * and assumed to always preserve their value, given to them during
19949ab747fSPaolo Bonzini      * initialization */
20049ab747fSPaolo Bonzini     memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad);
20149ab747fSPaolo Bonzini 
20240bbc194SPeter Maydell     /* Reset other state based on current card insertion/readonly status */
20340bbc194SPeter Maydell     sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus));
20440bbc194SPeter Maydell     sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus));
20540bbc194SPeter Maydell 
20649ab747fSPaolo Bonzini     s->data_count = 0;
20749ab747fSPaolo Bonzini     s->stopped_state = sdhc_not_stopped;
2080a7ac9f9SAndrew Baumann     s->pending_insert_state = false;
20949ab747fSPaolo Bonzini }
21049ab747fSPaolo Bonzini 
2118b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev)
2128b41c305SPeter Maydell {
2138b41c305SPeter Maydell     /* QOM (ie power-on) reset. This is identical to reset
2148b41c305SPeter Maydell      * commanded via device register apart from handling of the
2158b41c305SPeter Maydell      * 'pending insert on powerup' quirk.
2168b41c305SPeter Maydell      */
2178b41c305SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
2188b41c305SPeter Maydell 
2198b41c305SPeter Maydell     sdhci_reset(s);
2208b41c305SPeter Maydell 
2218b41c305SPeter Maydell     if (s->pending_insert_quirk) {
2228b41c305SPeter Maydell         s->pending_insert_state = true;
2238b41c305SPeter Maydell     }
2248b41c305SPeter Maydell }
2258b41c305SPeter Maydell 
226d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque);
22749ab747fSPaolo Bonzini 
22849ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s)
22949ab747fSPaolo Bonzini {
23049ab747fSPaolo Bonzini     SDRequest request;
23149ab747fSPaolo Bonzini     uint8_t response[16];
23249ab747fSPaolo Bonzini     int rlen;
23349ab747fSPaolo Bonzini 
23449ab747fSPaolo Bonzini     s->errintsts = 0;
23549ab747fSPaolo Bonzini     s->acmd12errsts = 0;
23649ab747fSPaolo Bonzini     request.cmd = s->cmdreg >> 8;
23749ab747fSPaolo Bonzini     request.arg = s->argument;
23849ab747fSPaolo Bonzini     DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request.cmd, request.arg);
23940bbc194SPeter Maydell     rlen = sdbus_do_command(&s->sdbus, &request, response);
24049ab747fSPaolo Bonzini 
24149ab747fSPaolo Bonzini     if (s->cmdreg & SDHC_CMD_RESPONSE) {
24249ab747fSPaolo Bonzini         if (rlen == 4) {
24349ab747fSPaolo Bonzini             s->rspreg[0] = (response[0] << 24) | (response[1] << 16) |
24449ab747fSPaolo Bonzini                            (response[2] << 8)  |  response[3];
24549ab747fSPaolo Bonzini             s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0;
24649ab747fSPaolo Bonzini             DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s->rspreg[0]);
24749ab747fSPaolo Bonzini         } else if (rlen == 16) {
24849ab747fSPaolo Bonzini             s->rspreg[0] = (response[11] << 24) | (response[12] << 16) |
24949ab747fSPaolo Bonzini                            (response[13] << 8) |  response[14];
25049ab747fSPaolo Bonzini             s->rspreg[1] = (response[7] << 24) | (response[8] << 16) |
25149ab747fSPaolo Bonzini                            (response[9] << 8)  |  response[10];
25249ab747fSPaolo Bonzini             s->rspreg[2] = (response[3] << 24) | (response[4] << 16) |
25349ab747fSPaolo Bonzini                            (response[5] << 8)  |  response[6];
25449ab747fSPaolo Bonzini             s->rspreg[3] = (response[0] << 16) | (response[1] << 8) |
25549ab747fSPaolo Bonzini                             response[2];
25649ab747fSPaolo Bonzini             DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.."
25749ab747fSPaolo Bonzini                   "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n",
25849ab747fSPaolo Bonzini                   s->rspreg[3], s->rspreg[2], s->rspreg[1], s->rspreg[0]);
25949ab747fSPaolo Bonzini         } else {
26049ab747fSPaolo Bonzini             ERRPRINT("Timeout waiting for command response\n");
26149ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) {
26249ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_CMDTIMEOUT;
26349ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
26449ab747fSPaolo Bonzini             }
26549ab747fSPaolo Bonzini         }
26649ab747fSPaolo Bonzini 
26749ab747fSPaolo Bonzini         if ((s->norintstsen & SDHC_NISEN_TRSCMP) &&
26849ab747fSPaolo Bonzini             (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) {
26949ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_TRSCMP;
27049ab747fSPaolo Bonzini         }
27149ab747fSPaolo Bonzini     }
27249ab747fSPaolo Bonzini 
27349ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_CMDCMP) {
27449ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_CMDCMP;
27549ab747fSPaolo Bonzini     }
27649ab747fSPaolo Bonzini 
27749ab747fSPaolo Bonzini     sdhci_update_irq(s);
27849ab747fSPaolo Bonzini 
27949ab747fSPaolo Bonzini     if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) {
280656f416cSPeter Crosthwaite         s->data_count = 0;
281d368ba43SKevin O'Connor         sdhci_data_transfer(s);
28249ab747fSPaolo Bonzini     }
28349ab747fSPaolo Bonzini }
28449ab747fSPaolo Bonzini 
28549ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s)
28649ab747fSPaolo Bonzini {
28749ab747fSPaolo Bonzini     /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
28849ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) {
28949ab747fSPaolo Bonzini         SDRequest request;
29049ab747fSPaolo Bonzini         uint8_t response[16];
29149ab747fSPaolo Bonzini 
29249ab747fSPaolo Bonzini         request.cmd = 0x0C;
29349ab747fSPaolo Bonzini         request.arg = 0;
29449ab747fSPaolo Bonzini         DPRINT_L1("Automatically issue CMD%d %08x\n", request.cmd, request.arg);
29540bbc194SPeter Maydell         sdbus_do_command(&s->sdbus, &request, response);
29649ab747fSPaolo Bonzini         /* Auto CMD12 response goes to the upper Response register */
29749ab747fSPaolo Bonzini         s->rspreg[3] = (response[0] << 24) | (response[1] << 16) |
29849ab747fSPaolo Bonzini                 (response[2] << 8) | response[3];
29949ab747fSPaolo Bonzini     }
30049ab747fSPaolo Bonzini 
30149ab747fSPaolo Bonzini     s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE |
30249ab747fSPaolo Bonzini             SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT |
30349ab747fSPaolo Bonzini             SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE);
30449ab747fSPaolo Bonzini 
30549ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_TRSCMP) {
30649ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_TRSCMP;
30749ab747fSPaolo Bonzini     }
30849ab747fSPaolo Bonzini 
30949ab747fSPaolo Bonzini     sdhci_update_irq(s);
31049ab747fSPaolo Bonzini }
31149ab747fSPaolo Bonzini 
31249ab747fSPaolo Bonzini /*
31349ab747fSPaolo Bonzini  * Programmed i/o data transfer
31449ab747fSPaolo Bonzini  */
31549ab747fSPaolo Bonzini 
31649ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
31749ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s)
31849ab747fSPaolo Bonzini {
31949ab747fSPaolo Bonzini     int index = 0;
32049ab747fSPaolo Bonzini 
32149ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) &&
32249ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) {
32349ab747fSPaolo Bonzini         return;
32449ab747fSPaolo Bonzini     }
32549ab747fSPaolo Bonzini 
32649ab747fSPaolo Bonzini     for (index = 0; index < (s->blksize & 0x0fff); index++) {
32740bbc194SPeter Maydell         s->fifo_buffer[index] = sdbus_read_data(&s->sdbus);
32849ab747fSPaolo Bonzini     }
32949ab747fSPaolo Bonzini 
33049ab747fSPaolo Bonzini     /* New data now available for READ through Buffer Port Register */
33149ab747fSPaolo Bonzini     s->prnsts |= SDHC_DATA_AVAILABLE;
33249ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_RBUFRDY) {
33349ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_RBUFRDY;
33449ab747fSPaolo Bonzini     }
33549ab747fSPaolo Bonzini 
33649ab747fSPaolo Bonzini     /* Clear DAT line active status if that was the last block */
33749ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
33849ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) {
33949ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
34049ab747fSPaolo Bonzini     }
34149ab747fSPaolo Bonzini 
34249ab747fSPaolo Bonzini     /* If stop at block gap request was set and it's not the last block of
34349ab747fSPaolo Bonzini      * data - generate Block Event interrupt */
34449ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) &&
34549ab747fSPaolo Bonzini             s->blkcnt != 1)    {
34649ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
34749ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
34849ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
34949ab747fSPaolo Bonzini         }
35049ab747fSPaolo Bonzini     }
35149ab747fSPaolo Bonzini 
35249ab747fSPaolo Bonzini     sdhci_update_irq(s);
35349ab747fSPaolo Bonzini }
35449ab747fSPaolo Bonzini 
35549ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
35649ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size)
35749ab747fSPaolo Bonzini {
35849ab747fSPaolo Bonzini     uint32_t value = 0;
35949ab747fSPaolo Bonzini     int i;
36049ab747fSPaolo Bonzini 
36149ab747fSPaolo Bonzini     /* first check that a valid data exists in host controller input buffer */
36249ab747fSPaolo Bonzini     if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) {
36349ab747fSPaolo Bonzini         ERRPRINT("Trying to read from empty buffer\n");
36449ab747fSPaolo Bonzini         return 0;
36549ab747fSPaolo Bonzini     }
36649ab747fSPaolo Bonzini 
36749ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
36849ab747fSPaolo Bonzini         value |= s->fifo_buffer[s->data_count] << i * 8;
36949ab747fSPaolo Bonzini         s->data_count++;
37049ab747fSPaolo Bonzini         /* check if we've read all valid data (blksize bytes) from buffer */
37149ab747fSPaolo Bonzini         if ((s->data_count) >= (s->blksize & 0x0fff)) {
37249ab747fSPaolo Bonzini             DPRINT_L2("All %u bytes of data have been read from input buffer\n",
37349ab747fSPaolo Bonzini                     s->data_count);
37449ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */
37549ab747fSPaolo Bonzini             s->data_count = 0;  /* next buff read must start at position [0] */
37649ab747fSPaolo Bonzini 
37749ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
37849ab747fSPaolo Bonzini                 s->blkcnt--;
37949ab747fSPaolo Bonzini             }
38049ab747fSPaolo Bonzini 
38149ab747fSPaolo Bonzini             /* if that was the last block of data */
38249ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
38349ab747fSPaolo Bonzini                 ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) ||
38449ab747fSPaolo Bonzini                  /* stop at gap request */
38549ab747fSPaolo Bonzini                 (s->stopped_state == sdhc_gap_read &&
38649ab747fSPaolo Bonzini                  !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) {
387d368ba43SKevin O'Connor                 sdhci_end_transfer(s);
38849ab747fSPaolo Bonzini             } else { /* if there are more data, read next block from card */
389d368ba43SKevin O'Connor                 sdhci_read_block_from_card(s);
39049ab747fSPaolo Bonzini             }
39149ab747fSPaolo Bonzini             break;
39249ab747fSPaolo Bonzini         }
39349ab747fSPaolo Bonzini     }
39449ab747fSPaolo Bonzini 
39549ab747fSPaolo Bonzini     return value;
39649ab747fSPaolo Bonzini }
39749ab747fSPaolo Bonzini 
39849ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */
39949ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s)
40049ab747fSPaolo Bonzini {
40149ab747fSPaolo Bonzini     int index = 0;
40249ab747fSPaolo Bonzini 
40349ab747fSPaolo Bonzini     if (s->prnsts & SDHC_SPACE_AVAILABLE) {
40449ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
40549ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_WBUFRDY;
40649ab747fSPaolo Bonzini         }
40749ab747fSPaolo Bonzini         sdhci_update_irq(s);
40849ab747fSPaolo Bonzini         return;
40949ab747fSPaolo Bonzini     }
41049ab747fSPaolo Bonzini 
41149ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
41249ab747fSPaolo Bonzini         if (s->blkcnt == 0) {
41349ab747fSPaolo Bonzini             return;
41449ab747fSPaolo Bonzini         } else {
41549ab747fSPaolo Bonzini             s->blkcnt--;
41649ab747fSPaolo Bonzini         }
41749ab747fSPaolo Bonzini     }
41849ab747fSPaolo Bonzini 
41949ab747fSPaolo Bonzini     for (index = 0; index < (s->blksize & 0x0fff); index++) {
42040bbc194SPeter Maydell         sdbus_write_data(&s->sdbus, s->fifo_buffer[index]);
42149ab747fSPaolo Bonzini     }
42249ab747fSPaolo Bonzini 
42349ab747fSPaolo Bonzini     /* Next data can be written through BUFFER DATORT register */
42449ab747fSPaolo Bonzini     s->prnsts |= SDHC_SPACE_AVAILABLE;
42549ab747fSPaolo Bonzini 
42649ab747fSPaolo Bonzini     /* Finish transfer if that was the last block of data */
42749ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
42849ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) &&
42949ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) {
430d368ba43SKevin O'Connor         sdhci_end_transfer(s);
431dcdb4cd8SPeter Crosthwaite     } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
432dcdb4cd8SPeter Crosthwaite         s->norintsts |= SDHC_NIS_WBUFRDY;
43349ab747fSPaolo Bonzini     }
43449ab747fSPaolo Bonzini 
43549ab747fSPaolo Bonzini     /* Generate Block Gap Event if requested and if not the last block */
43649ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) &&
43749ab747fSPaolo Bonzini             s->blkcnt > 0) {
43849ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DOING_WRITE;
43949ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
44049ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
44149ab747fSPaolo Bonzini         }
442d368ba43SKevin O'Connor         sdhci_end_transfer(s);
44349ab747fSPaolo Bonzini     }
44449ab747fSPaolo Bonzini 
44549ab747fSPaolo Bonzini     sdhci_update_irq(s);
44649ab747fSPaolo Bonzini }
44749ab747fSPaolo Bonzini 
44849ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port
44949ab747fSPaolo Bonzini  * register */
45049ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size)
45149ab747fSPaolo Bonzini {
45249ab747fSPaolo Bonzini     unsigned i;
45349ab747fSPaolo Bonzini 
45449ab747fSPaolo Bonzini     /* Check that there is free space left in a buffer */
45549ab747fSPaolo Bonzini     if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) {
45649ab747fSPaolo Bonzini         ERRPRINT("Can't write to data buffer: buffer full\n");
45749ab747fSPaolo Bonzini         return;
45849ab747fSPaolo Bonzini     }
45949ab747fSPaolo Bonzini 
46049ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
46149ab747fSPaolo Bonzini         s->fifo_buffer[s->data_count] = value & 0xFF;
46249ab747fSPaolo Bonzini         s->data_count++;
46349ab747fSPaolo Bonzini         value >>= 8;
46449ab747fSPaolo Bonzini         if (s->data_count >= (s->blksize & 0x0fff)) {
46549ab747fSPaolo Bonzini             DPRINT_L2("write buffer filled with %u bytes of data\n",
46649ab747fSPaolo Bonzini                     s->data_count);
46749ab747fSPaolo Bonzini             s->data_count = 0;
46849ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_SPACE_AVAILABLE;
46949ab747fSPaolo Bonzini             if (s->prnsts & SDHC_DOING_WRITE) {
470d368ba43SKevin O'Connor                 sdhci_write_block_to_card(s);
47149ab747fSPaolo Bonzini             }
47249ab747fSPaolo Bonzini         }
47349ab747fSPaolo Bonzini     }
47449ab747fSPaolo Bonzini }
47549ab747fSPaolo Bonzini 
47649ab747fSPaolo Bonzini /*
47749ab747fSPaolo Bonzini  * Single DMA data transfer
47849ab747fSPaolo Bonzini  */
47949ab747fSPaolo Bonzini 
48049ab747fSPaolo Bonzini /* Multi block SDMA transfer */
48149ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
48249ab747fSPaolo Bonzini {
48349ab747fSPaolo Bonzini     bool page_aligned = false;
48449ab747fSPaolo Bonzini     unsigned int n, begin;
48549ab747fSPaolo Bonzini     const uint16_t block_size = s->blksize & 0x0fff;
48649ab747fSPaolo Bonzini     uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12);
48749ab747fSPaolo Bonzini     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
48849ab747fSPaolo Bonzini 
48949ab747fSPaolo Bonzini     /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
49049ab747fSPaolo Bonzini      * possible stop at page boundary if initial address is not page aligned,
49149ab747fSPaolo Bonzini      * allow them to work properly */
49249ab747fSPaolo Bonzini     if ((s->sdmasysad % boundary_chk) == 0) {
49349ab747fSPaolo Bonzini         page_aligned = true;
49449ab747fSPaolo Bonzini     }
49549ab747fSPaolo Bonzini 
49649ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
49749ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
49849ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
49949ab747fSPaolo Bonzini         while (s->blkcnt) {
50049ab747fSPaolo Bonzini             if (s->data_count == 0) {
50149ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
50240bbc194SPeter Maydell                     s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
50349ab747fSPaolo Bonzini                 }
50449ab747fSPaolo Bonzini             }
50549ab747fSPaolo Bonzini             begin = s->data_count;
50649ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
50749ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
50849ab747fSPaolo Bonzini                 boundary_count = 0;
50949ab747fSPaolo Bonzini              } else {
51049ab747fSPaolo Bonzini                 s->data_count = block_size;
51149ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
51249ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
51349ab747fSPaolo Bonzini                     s->blkcnt--;
51449ab747fSPaolo Bonzini                 }
51549ab747fSPaolo Bonzini             }
516df32fd1cSPaolo Bonzini             dma_memory_write(&address_space_memory, s->sdmasysad,
51749ab747fSPaolo Bonzini                              &s->fifo_buffer[begin], s->data_count - begin);
51849ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
51949ab747fSPaolo Bonzini             if (s->data_count == block_size) {
52049ab747fSPaolo Bonzini                 s->data_count = 0;
52149ab747fSPaolo Bonzini             }
52249ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
52349ab747fSPaolo Bonzini                 break;
52449ab747fSPaolo Bonzini             }
52549ab747fSPaolo Bonzini         }
52649ab747fSPaolo Bonzini     } else {
52749ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT |
52849ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
52949ab747fSPaolo Bonzini         while (s->blkcnt) {
53049ab747fSPaolo Bonzini             begin = s->data_count;
53149ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
53249ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
53349ab747fSPaolo Bonzini                 boundary_count = 0;
53449ab747fSPaolo Bonzini              } else {
53549ab747fSPaolo Bonzini                 s->data_count = block_size;
53649ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
53749ab747fSPaolo Bonzini             }
538df32fd1cSPaolo Bonzini             dma_memory_read(&address_space_memory, s->sdmasysad,
53942922105SPrasad J Pandit                             &s->fifo_buffer[begin], s->data_count - begin);
54049ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
54149ab747fSPaolo Bonzini             if (s->data_count == block_size) {
54249ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
54340bbc194SPeter Maydell                     sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
54449ab747fSPaolo Bonzini                 }
54549ab747fSPaolo Bonzini                 s->data_count = 0;
54649ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
54749ab747fSPaolo Bonzini                     s->blkcnt--;
54849ab747fSPaolo Bonzini                 }
54949ab747fSPaolo Bonzini             }
55049ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
55149ab747fSPaolo Bonzini                 break;
55249ab747fSPaolo Bonzini             }
55349ab747fSPaolo Bonzini         }
55449ab747fSPaolo Bonzini     }
55549ab747fSPaolo Bonzini 
55649ab747fSPaolo Bonzini     if (s->blkcnt == 0) {
557d368ba43SKevin O'Connor         sdhci_end_transfer(s);
55849ab747fSPaolo Bonzini     } else {
55949ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_DMA) {
56049ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_DMA;
56149ab747fSPaolo Bonzini         }
56249ab747fSPaolo Bonzini         sdhci_update_irq(s);
56349ab747fSPaolo Bonzini     }
56449ab747fSPaolo Bonzini }
56549ab747fSPaolo Bonzini 
56649ab747fSPaolo Bonzini /* single block SDMA transfer */
56749ab747fSPaolo Bonzini 
56849ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s)
56949ab747fSPaolo Bonzini {
57049ab747fSPaolo Bonzini     int n;
57149ab747fSPaolo Bonzini     uint32_t datacnt = s->blksize & 0x0fff;
57249ab747fSPaolo Bonzini 
57349ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
57449ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
57540bbc194SPeter Maydell             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
57649ab747fSPaolo Bonzini         }
577df32fd1cSPaolo Bonzini         dma_memory_write(&address_space_memory, s->sdmasysad, s->fifo_buffer,
57849ab747fSPaolo Bonzini                          datacnt);
57949ab747fSPaolo Bonzini     } else {
580df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, s->sdmasysad, s->fifo_buffer,
58149ab747fSPaolo Bonzini                         datacnt);
58249ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
58340bbc194SPeter Maydell             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
58449ab747fSPaolo Bonzini         }
58549ab747fSPaolo Bonzini     }
58649ab747fSPaolo Bonzini 
58749ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
58849ab747fSPaolo Bonzini         s->blkcnt--;
58949ab747fSPaolo Bonzini     }
59049ab747fSPaolo Bonzini 
591d368ba43SKevin O'Connor     sdhci_end_transfer(s);
59249ab747fSPaolo Bonzini }
59349ab747fSPaolo Bonzini 
59449ab747fSPaolo Bonzini typedef struct ADMADescr {
59549ab747fSPaolo Bonzini     hwaddr addr;
59649ab747fSPaolo Bonzini     uint16_t length;
59749ab747fSPaolo Bonzini     uint8_t attr;
59849ab747fSPaolo Bonzini     uint8_t incr;
59949ab747fSPaolo Bonzini } ADMADescr;
60049ab747fSPaolo Bonzini 
60149ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
60249ab747fSPaolo Bonzini {
60349ab747fSPaolo Bonzini     uint32_t adma1 = 0;
60449ab747fSPaolo Bonzini     uint64_t adma2 = 0;
60549ab747fSPaolo Bonzini     hwaddr entry_addr = (hwaddr)s->admasysaddr;
60649ab747fSPaolo Bonzini     switch (SDHC_DMA_TYPE(s->hostctl)) {
60749ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_32:
608df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma2,
60949ab747fSPaolo Bonzini                         sizeof(adma2));
61049ab747fSPaolo Bonzini         adma2 = le64_to_cpu(adma2);
61149ab747fSPaolo Bonzini         /* The spec does not specify endianness of descriptor table.
61249ab747fSPaolo Bonzini          * We currently assume that it is LE.
61349ab747fSPaolo Bonzini          */
61449ab747fSPaolo Bonzini         dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull;
61549ab747fSPaolo Bonzini         dscr->length = (uint16_t)extract64(adma2, 16, 16);
61649ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract64(adma2, 0, 7);
61749ab747fSPaolo Bonzini         dscr->incr = 8;
61849ab747fSPaolo Bonzini         break;
61949ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA1_32:
620df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma1,
62149ab747fSPaolo Bonzini                         sizeof(adma1));
62249ab747fSPaolo Bonzini         adma1 = le32_to_cpu(adma1);
62349ab747fSPaolo Bonzini         dscr->addr = (hwaddr)(adma1 & 0xFFFFF000);
62449ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract32(adma1, 0, 7);
62549ab747fSPaolo Bonzini         dscr->incr = 4;
62649ab747fSPaolo Bonzini         if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
62749ab747fSPaolo Bonzini             dscr->length = (uint16_t)extract32(adma1, 12, 16);
62849ab747fSPaolo Bonzini         } else {
62949ab747fSPaolo Bonzini             dscr->length = 4096;
63049ab747fSPaolo Bonzini         }
63149ab747fSPaolo Bonzini         break;
63249ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_64:
633df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr,
63449ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->attr), 1);
635df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr + 2,
63649ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->length), 2);
63749ab747fSPaolo Bonzini         dscr->length = le16_to_cpu(dscr->length);
638df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr + 4,
63949ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->addr), 8);
64049ab747fSPaolo Bonzini         dscr->attr = le64_to_cpu(dscr->attr);
64149ab747fSPaolo Bonzini         dscr->attr &= 0xfffffff8;
64249ab747fSPaolo Bonzini         dscr->incr = 12;
64349ab747fSPaolo Bonzini         break;
64449ab747fSPaolo Bonzini     }
64549ab747fSPaolo Bonzini }
64649ab747fSPaolo Bonzini 
64749ab747fSPaolo Bonzini /* Advanced DMA data transfer */
64849ab747fSPaolo Bonzini 
64949ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s)
65049ab747fSPaolo Bonzini {
65149ab747fSPaolo Bonzini     unsigned int n, begin, length;
65249ab747fSPaolo Bonzini     const uint16_t block_size = s->blksize & 0x0fff;
65349ab747fSPaolo Bonzini     ADMADescr dscr;
65449ab747fSPaolo Bonzini     int i;
65549ab747fSPaolo Bonzini 
65649ab747fSPaolo Bonzini     for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
65749ab747fSPaolo Bonzini         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
65849ab747fSPaolo Bonzini 
65949ab747fSPaolo Bonzini         get_adma_description(s, &dscr);
66049ab747fSPaolo Bonzini         DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx ", len=%d, attr=%x\n",
66149ab747fSPaolo Bonzini                 dscr.addr, dscr.length, dscr.attr);
66249ab747fSPaolo Bonzini 
66349ab747fSPaolo Bonzini         if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
66449ab747fSPaolo Bonzini             /* Indicate that error occurred in ST_FDS state */
66549ab747fSPaolo Bonzini             s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
66649ab747fSPaolo Bonzini             s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
66749ab747fSPaolo Bonzini 
66849ab747fSPaolo Bonzini             /* Generate ADMA error interrupt */
66949ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_ADMAERR) {
67049ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_ADMAERR;
67149ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
67249ab747fSPaolo Bonzini             }
67349ab747fSPaolo Bonzini 
67449ab747fSPaolo Bonzini             sdhci_update_irq(s);
67549ab747fSPaolo Bonzini             return;
67649ab747fSPaolo Bonzini         }
67749ab747fSPaolo Bonzini 
67849ab747fSPaolo Bonzini         length = dscr.length ? dscr.length : 65536;
67949ab747fSPaolo Bonzini 
68049ab747fSPaolo Bonzini         switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) {
68149ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_TRAN:  /* data transfer */
68249ab747fSPaolo Bonzini 
68349ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_READ) {
68449ab747fSPaolo Bonzini                 while (length) {
68549ab747fSPaolo Bonzini                     if (s->data_count == 0) {
68649ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
68740bbc194SPeter Maydell                             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
68849ab747fSPaolo Bonzini                         }
68949ab747fSPaolo Bonzini                     }
69049ab747fSPaolo Bonzini                     begin = s->data_count;
69149ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
69249ab747fSPaolo Bonzini                         s->data_count = length + begin;
69349ab747fSPaolo Bonzini                         length = 0;
69449ab747fSPaolo Bonzini                      } else {
69549ab747fSPaolo Bonzini                         s->data_count = block_size;
69649ab747fSPaolo Bonzini                         length -= block_size - begin;
69749ab747fSPaolo Bonzini                     }
698df32fd1cSPaolo Bonzini                     dma_memory_write(&address_space_memory, dscr.addr,
69949ab747fSPaolo Bonzini                                      &s->fifo_buffer[begin],
70049ab747fSPaolo Bonzini                                      s->data_count - begin);
70149ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
70249ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
70349ab747fSPaolo Bonzini                         s->data_count = 0;
70449ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
70549ab747fSPaolo Bonzini                             s->blkcnt--;
70649ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
70749ab747fSPaolo Bonzini                                 break;
70849ab747fSPaolo Bonzini                             }
70949ab747fSPaolo Bonzini                         }
71049ab747fSPaolo Bonzini                     }
71149ab747fSPaolo Bonzini                 }
71249ab747fSPaolo Bonzini             } else {
71349ab747fSPaolo Bonzini                 while (length) {
71449ab747fSPaolo Bonzini                     begin = s->data_count;
71549ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
71649ab747fSPaolo Bonzini                         s->data_count = length + begin;
71749ab747fSPaolo Bonzini                         length = 0;
71849ab747fSPaolo Bonzini                      } else {
71949ab747fSPaolo Bonzini                         s->data_count = block_size;
72049ab747fSPaolo Bonzini                         length -= block_size - begin;
72149ab747fSPaolo Bonzini                     }
722df32fd1cSPaolo Bonzini                     dma_memory_read(&address_space_memory, dscr.addr,
7239db11cefSPeter Crosthwaite                                     &s->fifo_buffer[begin],
7249db11cefSPeter Crosthwaite                                     s->data_count - begin);
72549ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
72649ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
72749ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
72840bbc194SPeter Maydell                             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
72949ab747fSPaolo Bonzini                         }
73049ab747fSPaolo Bonzini                         s->data_count = 0;
73149ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
73249ab747fSPaolo Bonzini                             s->blkcnt--;
73349ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
73449ab747fSPaolo Bonzini                                 break;
73549ab747fSPaolo Bonzini                             }
73649ab747fSPaolo Bonzini                         }
73749ab747fSPaolo Bonzini                     }
73849ab747fSPaolo Bonzini                 }
73949ab747fSPaolo Bonzini             }
74049ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
74149ab747fSPaolo Bonzini             break;
74249ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
74349ab747fSPaolo Bonzini             s->admasysaddr = dscr.addr;
744be9c5ddeSSai Pavan Boddu             DPRINT_L1("ADMA link: admasysaddr=0x%" PRIx64 "\n",
745be9c5ddeSSai Pavan Boddu                       s->admasysaddr);
74649ab747fSPaolo Bonzini             break;
74749ab747fSPaolo Bonzini         default:
74849ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
74949ab747fSPaolo Bonzini             break;
75049ab747fSPaolo Bonzini         }
75149ab747fSPaolo Bonzini 
7521d32c26fSPeter Crosthwaite         if (dscr.attr & SDHC_ADMA_ATTR_INT) {
753be9c5ddeSSai Pavan Boddu             DPRINT_L1("ADMA interrupt: admasysaddr=0x%" PRIx64 "\n",
754be9c5ddeSSai Pavan Boddu                       s->admasysaddr);
7551d32c26fSPeter Crosthwaite             if (s->norintstsen & SDHC_NISEN_DMA) {
7561d32c26fSPeter Crosthwaite                 s->norintsts |= SDHC_NIS_DMA;
7571d32c26fSPeter Crosthwaite             }
7581d32c26fSPeter Crosthwaite 
7591d32c26fSPeter Crosthwaite             sdhci_update_irq(s);
7601d32c26fSPeter Crosthwaite         }
7611d32c26fSPeter Crosthwaite 
76249ab747fSPaolo Bonzini         /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
76349ab747fSPaolo Bonzini         if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
76449ab747fSPaolo Bonzini                     (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
76549ab747fSPaolo Bonzini             DPRINT_L2("ADMA transfer completed\n");
76649ab747fSPaolo Bonzini             if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
76749ab747fSPaolo Bonzini                 (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
76849ab747fSPaolo Bonzini                 s->blkcnt != 0)) {
76949ab747fSPaolo Bonzini                 ERRPRINT("SD/MMC host ADMA length mismatch\n");
77049ab747fSPaolo Bonzini                 s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
77149ab747fSPaolo Bonzini                         SDHC_ADMAERR_STATE_ST_TFR;
77249ab747fSPaolo Bonzini                 if (s->errintstsen & SDHC_EISEN_ADMAERR) {
77349ab747fSPaolo Bonzini                     ERRPRINT("Set ADMA error flag\n");
77449ab747fSPaolo Bonzini                     s->errintsts |= SDHC_EIS_ADMAERR;
77549ab747fSPaolo Bonzini                     s->norintsts |= SDHC_NIS_ERR;
77649ab747fSPaolo Bonzini                 }
77749ab747fSPaolo Bonzini 
77849ab747fSPaolo Bonzini                 sdhci_update_irq(s);
77949ab747fSPaolo Bonzini             }
780d368ba43SKevin O'Connor             sdhci_end_transfer(s);
78149ab747fSPaolo Bonzini             return;
78249ab747fSPaolo Bonzini         }
78349ab747fSPaolo Bonzini 
78449ab747fSPaolo Bonzini     }
78549ab747fSPaolo Bonzini 
78649ab747fSPaolo Bonzini     /* we have unfinished business - reschedule to continue ADMA */
787bc72ad67SAlex Bligh     timer_mod(s->transfer_timer,
788bc72ad67SAlex Bligh                    qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY);
78949ab747fSPaolo Bonzini }
79049ab747fSPaolo Bonzini 
79149ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */
79249ab747fSPaolo Bonzini 
793d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque)
79449ab747fSPaolo Bonzini {
795d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
79649ab747fSPaolo Bonzini 
79749ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_DMA) {
79849ab747fSPaolo Bonzini         switch (SDHC_DMA_TYPE(s->hostctl)) {
79949ab747fSPaolo Bonzini         case SDHC_CTRL_SDMA:
80049ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) &&
80149ab747fSPaolo Bonzini                     (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || s->blkcnt == 0)) {
80249ab747fSPaolo Bonzini                 break;
80349ab747fSPaolo Bonzini             }
80449ab747fSPaolo Bonzini 
80549ab747fSPaolo Bonzini             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
806d368ba43SKevin O'Connor                 sdhci_sdma_transfer_single_block(s);
80749ab747fSPaolo Bonzini             } else {
808d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
80949ab747fSPaolo Bonzini             }
81049ab747fSPaolo Bonzini 
81149ab747fSPaolo Bonzini             break;
81249ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA1_32:
81349ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA1)) {
81449ab747fSPaolo Bonzini                 ERRPRINT("ADMA1 not supported\n");
81549ab747fSPaolo Bonzini                 break;
81649ab747fSPaolo Bonzini             }
81749ab747fSPaolo Bonzini 
818d368ba43SKevin O'Connor             sdhci_do_adma(s);
81949ab747fSPaolo Bonzini             break;
82049ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_32:
82149ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA2)) {
82249ab747fSPaolo Bonzini                 ERRPRINT("ADMA2 not supported\n");
82349ab747fSPaolo Bonzini                 break;
82449ab747fSPaolo Bonzini             }
82549ab747fSPaolo Bonzini 
826d368ba43SKevin O'Connor             sdhci_do_adma(s);
82749ab747fSPaolo Bonzini             break;
82849ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_64:
82949ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA2) ||
83049ab747fSPaolo Bonzini                     !(s->capareg & SDHC_64_BIT_BUS_SUPPORT)) {
83149ab747fSPaolo Bonzini                 ERRPRINT("64 bit ADMA not supported\n");
83249ab747fSPaolo Bonzini                 break;
83349ab747fSPaolo Bonzini             }
83449ab747fSPaolo Bonzini 
835d368ba43SKevin O'Connor             sdhci_do_adma(s);
83649ab747fSPaolo Bonzini             break;
83749ab747fSPaolo Bonzini         default:
83849ab747fSPaolo Bonzini             ERRPRINT("Unsupported DMA type\n");
83949ab747fSPaolo Bonzini             break;
84049ab747fSPaolo Bonzini         }
84149ab747fSPaolo Bonzini     } else {
84240bbc194SPeter Maydell         if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) {
84349ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
84449ab747fSPaolo Bonzini                     SDHC_DAT_LINE_ACTIVE;
845d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
84649ab747fSPaolo Bonzini         } else {
84749ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
84849ab747fSPaolo Bonzini                     SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
849d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
85049ab747fSPaolo Bonzini         }
85149ab747fSPaolo Bonzini     }
85249ab747fSPaolo Bonzini }
85349ab747fSPaolo Bonzini 
85449ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s)
85549ab747fSPaolo Bonzini {
8566890a695SPeter Crosthwaite     if (!SDHC_CLOCK_IS_ON(s->clkcon) ||
85749ab747fSPaolo Bonzini         (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) &&
85849ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_DATA_PRESENT) ||
85949ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY &&
86049ab747fSPaolo Bonzini         !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) {
86149ab747fSPaolo Bonzini         return false;
86249ab747fSPaolo Bonzini     }
86349ab747fSPaolo Bonzini 
86449ab747fSPaolo Bonzini     return true;
86549ab747fSPaolo Bonzini }
86649ab747fSPaolo Bonzini 
86749ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and
86849ab747fSPaolo Bonzini  * continuous manner */
86949ab747fSPaolo Bonzini static inline bool
87049ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
87149ab747fSPaolo Bonzini {
87249ab747fSPaolo Bonzini     if ((s->data_count & 0x3) != byte_num) {
87349ab747fSPaolo Bonzini         ERRPRINT("Non-sequential access to Buffer Data Port register"
87449ab747fSPaolo Bonzini                 "is prohibited\n");
87549ab747fSPaolo Bonzini         return false;
87649ab747fSPaolo Bonzini     }
87749ab747fSPaolo Bonzini     return true;
87849ab747fSPaolo Bonzini }
87949ab747fSPaolo Bonzini 
880d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size)
88149ab747fSPaolo Bonzini {
882d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
88349ab747fSPaolo Bonzini     uint32_t ret = 0;
88449ab747fSPaolo Bonzini 
88549ab747fSPaolo Bonzini     switch (offset & ~0x3) {
88649ab747fSPaolo Bonzini     case SDHC_SYSAD:
88749ab747fSPaolo Bonzini         ret = s->sdmasysad;
88849ab747fSPaolo Bonzini         break;
88949ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
89049ab747fSPaolo Bonzini         ret = s->blksize | (s->blkcnt << 16);
89149ab747fSPaolo Bonzini         break;
89249ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
89349ab747fSPaolo Bonzini         ret = s->argument;
89449ab747fSPaolo Bonzini         break;
89549ab747fSPaolo Bonzini     case SDHC_TRNMOD:
89649ab747fSPaolo Bonzini         ret = s->trnmod | (s->cmdreg << 16);
89749ab747fSPaolo Bonzini         break;
89849ab747fSPaolo Bonzini     case SDHC_RSPREG0 ... SDHC_RSPREG3:
89949ab747fSPaolo Bonzini         ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2];
90049ab747fSPaolo Bonzini         break;
90149ab747fSPaolo Bonzini     case  SDHC_BDATA:
90249ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
903d368ba43SKevin O'Connor             ret = sdhci_read_dataport(s, size);
904d368ba43SKevin O'Connor             DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset,
905677ff2aeSPeter Crosthwaite                       ret, ret);
90649ab747fSPaolo Bonzini             return ret;
90749ab747fSPaolo Bonzini         }
90849ab747fSPaolo Bonzini         break;
90949ab747fSPaolo Bonzini     case SDHC_PRNSTS:
91049ab747fSPaolo Bonzini         ret = s->prnsts;
91149ab747fSPaolo Bonzini         break;
91249ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
91349ab747fSPaolo Bonzini         ret = s->hostctl | (s->pwrcon << 8) | (s->blkgap << 16) |
91449ab747fSPaolo Bonzini               (s->wakcon << 24);
91549ab747fSPaolo Bonzini         break;
91649ab747fSPaolo Bonzini     case SDHC_CLKCON:
91749ab747fSPaolo Bonzini         ret = s->clkcon | (s->timeoutcon << 16);
91849ab747fSPaolo Bonzini         break;
91949ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
92049ab747fSPaolo Bonzini         ret = s->norintsts | (s->errintsts << 16);
92149ab747fSPaolo Bonzini         break;
92249ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
92349ab747fSPaolo Bonzini         ret = s->norintstsen | (s->errintstsen << 16);
92449ab747fSPaolo Bonzini         break;
92549ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
92649ab747fSPaolo Bonzini         ret = s->norintsigen | (s->errintsigen << 16);
92749ab747fSPaolo Bonzini         break;
92849ab747fSPaolo Bonzini     case SDHC_ACMD12ERRSTS:
92949ab747fSPaolo Bonzini         ret = s->acmd12errsts;
93049ab747fSPaolo Bonzini         break;
93149ab747fSPaolo Bonzini     case SDHC_CAPAREG:
93249ab747fSPaolo Bonzini         ret = s->capareg;
93349ab747fSPaolo Bonzini         break;
93449ab747fSPaolo Bonzini     case SDHC_MAXCURR:
93549ab747fSPaolo Bonzini         ret = s->maxcurr;
93649ab747fSPaolo Bonzini         break;
93749ab747fSPaolo Bonzini     case SDHC_ADMAERR:
93849ab747fSPaolo Bonzini         ret =  s->admaerr;
93949ab747fSPaolo Bonzini         break;
94049ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
94149ab747fSPaolo Bonzini         ret = (uint32_t)s->admasysaddr;
94249ab747fSPaolo Bonzini         break;
94349ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
94449ab747fSPaolo Bonzini         ret = (uint32_t)(s->admasysaddr >> 32);
94549ab747fSPaolo Bonzini         break;
94649ab747fSPaolo Bonzini     case SDHC_SLOT_INT_STATUS:
94749ab747fSPaolo Bonzini         ret = (SD_HOST_SPECv2_VERS << 16) | sdhci_slotint(s);
94849ab747fSPaolo Bonzini         break;
94949ab747fSPaolo Bonzini     default:
950d368ba43SKevin O'Connor         ERRPRINT("bad %ub read: addr[0x%04x]\n", size, (int)offset);
95149ab747fSPaolo Bonzini         break;
95249ab747fSPaolo Bonzini     }
95349ab747fSPaolo Bonzini 
95449ab747fSPaolo Bonzini     ret >>= (offset & 0x3) * 8;
95549ab747fSPaolo Bonzini     ret &= (1ULL << (size * 8)) - 1;
956d368ba43SKevin O'Connor     DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset, ret, ret);
95749ab747fSPaolo Bonzini     return ret;
95849ab747fSPaolo Bonzini }
95949ab747fSPaolo Bonzini 
96049ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value)
96149ab747fSPaolo Bonzini {
96249ab747fSPaolo Bonzini     if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) {
96349ab747fSPaolo Bonzini         return;
96449ab747fSPaolo Bonzini     }
96549ab747fSPaolo Bonzini     s->blkgap = value & SDHC_STOP_AT_GAP_REQ;
96649ab747fSPaolo Bonzini 
96749ab747fSPaolo Bonzini     if ((value & SDHC_CONTINUE_REQ) && s->stopped_state &&
96849ab747fSPaolo Bonzini             (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) {
96949ab747fSPaolo Bonzini         if (s->stopped_state == sdhc_gap_read) {
97049ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ;
971d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
97249ab747fSPaolo Bonzini         } else {
97349ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE;
974d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
97549ab747fSPaolo Bonzini         }
97649ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
97749ab747fSPaolo Bonzini     } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) {
97849ab747fSPaolo Bonzini         if (s->prnsts & SDHC_DOING_READ) {
97949ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_read;
98049ab747fSPaolo Bonzini         } else if (s->prnsts & SDHC_DOING_WRITE) {
98149ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_write;
98249ab747fSPaolo Bonzini         }
98349ab747fSPaolo Bonzini     }
98449ab747fSPaolo Bonzini }
98549ab747fSPaolo Bonzini 
98649ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value)
98749ab747fSPaolo Bonzini {
98849ab747fSPaolo Bonzini     switch (value) {
98949ab747fSPaolo Bonzini     case SDHC_RESET_ALL:
990d368ba43SKevin O'Connor         sdhci_reset(s);
99149ab747fSPaolo Bonzini         break;
99249ab747fSPaolo Bonzini     case SDHC_RESET_CMD:
99349ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_CMD_INHIBIT;
99449ab747fSPaolo Bonzini         s->norintsts &= ~SDHC_NIS_CMDCMP;
99549ab747fSPaolo Bonzini         break;
99649ab747fSPaolo Bonzini     case SDHC_RESET_DATA:
99749ab747fSPaolo Bonzini         s->data_count = 0;
99849ab747fSPaolo Bonzini         s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE |
99949ab747fSPaolo Bonzini                 SDHC_DOING_READ | SDHC_DOING_WRITE |
100049ab747fSPaolo Bonzini                 SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE);
100149ab747fSPaolo Bonzini         s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ);
100249ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
100349ab747fSPaolo Bonzini         s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY |
100449ab747fSPaolo Bonzini                 SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP);
100549ab747fSPaolo Bonzini         break;
100649ab747fSPaolo Bonzini     }
100749ab747fSPaolo Bonzini }
100849ab747fSPaolo Bonzini 
100949ab747fSPaolo Bonzini static void
1010d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
101149ab747fSPaolo Bonzini {
1012d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
101349ab747fSPaolo Bonzini     unsigned shift =  8 * (offset & 0x3);
101449ab747fSPaolo Bonzini     uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift);
1015d368ba43SKevin O'Connor     uint32_t value = val;
101649ab747fSPaolo Bonzini     value <<= shift;
101749ab747fSPaolo Bonzini 
101849ab747fSPaolo Bonzini     switch (offset & ~0x3) {
101949ab747fSPaolo Bonzini     case SDHC_SYSAD:
102049ab747fSPaolo Bonzini         s->sdmasysad = (s->sdmasysad & mask) | value;
102149ab747fSPaolo Bonzini         MASKED_WRITE(s->sdmasysad, mask, value);
102249ab747fSPaolo Bonzini         /* Writing to last byte of sdmasysad might trigger transfer */
102349ab747fSPaolo Bonzini         if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt &&
102449ab747fSPaolo Bonzini                 s->blksize && SDHC_DMA_TYPE(s->hostctl) == SDHC_CTRL_SDMA) {
1025d368ba43SKevin O'Connor             sdhci_sdma_transfer_multi_blocks(s);
102649ab747fSPaolo Bonzini         }
102749ab747fSPaolo Bonzini         break;
102849ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
102949ab747fSPaolo Bonzini         if (!TRANSFERRING_DATA(s->prnsts)) {
103049ab747fSPaolo Bonzini             MASKED_WRITE(s->blksize, mask, value);
103149ab747fSPaolo Bonzini             MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16);
103249ab747fSPaolo Bonzini         }
10339201bb9aSAlistair Francis 
10349201bb9aSAlistair Francis         /* Limit block size to the maximum buffer size */
10359201bb9aSAlistair Francis         if (extract32(s->blksize, 0, 12) > s->buf_maxsz) {
10369201bb9aSAlistair Francis             qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than " \
10379201bb9aSAlistair Francis                           "the maximum buffer 0x%x", __func__, s->blksize,
10389201bb9aSAlistair Francis                           s->buf_maxsz);
10399201bb9aSAlistair Francis 
10409201bb9aSAlistair Francis             s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz);
10419201bb9aSAlistair Francis         }
10429201bb9aSAlistair Francis 
104349ab747fSPaolo Bonzini         break;
104449ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
104549ab747fSPaolo Bonzini         MASKED_WRITE(s->argument, mask, value);
104649ab747fSPaolo Bonzini         break;
104749ab747fSPaolo Bonzini     case SDHC_TRNMOD:
104849ab747fSPaolo Bonzini         /* DMA can be enabled only if it is supported as indicated by
104949ab747fSPaolo Bonzini          * capabilities register */
105049ab747fSPaolo Bonzini         if (!(s->capareg & SDHC_CAN_DO_DMA)) {
105149ab747fSPaolo Bonzini             value &= ~SDHC_TRNS_DMA;
105249ab747fSPaolo Bonzini         }
105349ab747fSPaolo Bonzini         MASKED_WRITE(s->trnmod, mask, value);
105449ab747fSPaolo Bonzini         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
105549ab747fSPaolo Bonzini 
105649ab747fSPaolo Bonzini         /* Writing to the upper byte of CMDREG triggers SD command generation */
1057d368ba43SKevin O'Connor         if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) {
105849ab747fSPaolo Bonzini             break;
105949ab747fSPaolo Bonzini         }
106049ab747fSPaolo Bonzini 
1061d368ba43SKevin O'Connor         sdhci_send_command(s);
106249ab747fSPaolo Bonzini         break;
106349ab747fSPaolo Bonzini     case  SDHC_BDATA:
106449ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
1065d368ba43SKevin O'Connor             sdhci_write_dataport(s, value >> shift, size);
106649ab747fSPaolo Bonzini         }
106749ab747fSPaolo Bonzini         break;
106849ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
106949ab747fSPaolo Bonzini         if (!(mask & 0xFF0000)) {
107049ab747fSPaolo Bonzini             sdhci_blkgap_write(s, value >> 16);
107149ab747fSPaolo Bonzini         }
107249ab747fSPaolo Bonzini         MASKED_WRITE(s->hostctl, mask, value);
107349ab747fSPaolo Bonzini         MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8);
107449ab747fSPaolo Bonzini         MASKED_WRITE(s->wakcon, mask >> 24, value >> 24);
107549ab747fSPaolo Bonzini         if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 ||
107649ab747fSPaolo Bonzini                 !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) {
107749ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
107849ab747fSPaolo Bonzini         }
107949ab747fSPaolo Bonzini         break;
108049ab747fSPaolo Bonzini     case SDHC_CLKCON:
108149ab747fSPaolo Bonzini         if (!(mask & 0xFF000000)) {
108249ab747fSPaolo Bonzini             sdhci_reset_write(s, value >> 24);
108349ab747fSPaolo Bonzini         }
108449ab747fSPaolo Bonzini         MASKED_WRITE(s->clkcon, mask, value);
108549ab747fSPaolo Bonzini         MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16);
108649ab747fSPaolo Bonzini         if (s->clkcon & SDHC_CLOCK_INT_EN) {
108749ab747fSPaolo Bonzini             s->clkcon |= SDHC_CLOCK_INT_STABLE;
108849ab747fSPaolo Bonzini         } else {
108949ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_INT_STABLE;
109049ab747fSPaolo Bonzini         }
109149ab747fSPaolo Bonzini         break;
109249ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
109349ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_CARDINT) {
109449ab747fSPaolo Bonzini             value &= ~SDHC_NIS_CARDINT;
109549ab747fSPaolo Bonzini         }
109649ab747fSPaolo Bonzini         s->norintsts &= mask | ~value;
109749ab747fSPaolo Bonzini         s->errintsts &= (mask >> 16) | ~(value >> 16);
109849ab747fSPaolo Bonzini         if (s->errintsts) {
109949ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
110049ab747fSPaolo Bonzini         } else {
110149ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
110249ab747fSPaolo Bonzini         }
110349ab747fSPaolo Bonzini         sdhci_update_irq(s);
110449ab747fSPaolo Bonzini         break;
110549ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
110649ab747fSPaolo Bonzini         MASKED_WRITE(s->norintstsen, mask, value);
110749ab747fSPaolo Bonzini         MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16);
110849ab747fSPaolo Bonzini         s->norintsts &= s->norintstsen;
110949ab747fSPaolo Bonzini         s->errintsts &= s->errintstsen;
111049ab747fSPaolo Bonzini         if (s->errintsts) {
111149ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
111249ab747fSPaolo Bonzini         } else {
111349ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
111449ab747fSPaolo Bonzini         }
11150a7ac9f9SAndrew Baumann         /* Quirk for Raspberry Pi: pending card insert interrupt
11160a7ac9f9SAndrew Baumann          * appears when first enabled after power on */
11170a7ac9f9SAndrew Baumann         if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) {
11180a7ac9f9SAndrew Baumann             assert(s->pending_insert_quirk);
11190a7ac9f9SAndrew Baumann             s->norintsts |= SDHC_NIS_INSERT;
11200a7ac9f9SAndrew Baumann             s->pending_insert_state = false;
11210a7ac9f9SAndrew Baumann         }
112249ab747fSPaolo Bonzini         sdhci_update_irq(s);
112349ab747fSPaolo Bonzini         break;
112449ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
112549ab747fSPaolo Bonzini         MASKED_WRITE(s->norintsigen, mask, value);
112649ab747fSPaolo Bonzini         MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16);
112749ab747fSPaolo Bonzini         sdhci_update_irq(s);
112849ab747fSPaolo Bonzini         break;
112949ab747fSPaolo Bonzini     case SDHC_ADMAERR:
113049ab747fSPaolo Bonzini         MASKED_WRITE(s->admaerr, mask, value);
113149ab747fSPaolo Bonzini         break;
113249ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
113349ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL |
113449ab747fSPaolo Bonzini                 (uint64_t)mask)) | (uint64_t)value;
113549ab747fSPaolo Bonzini         break;
113649ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
113749ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL |
113849ab747fSPaolo Bonzini                 ((uint64_t)mask << 32))) | ((uint64_t)value << 32);
113949ab747fSPaolo Bonzini         break;
114049ab747fSPaolo Bonzini     case SDHC_FEAER:
114149ab747fSPaolo Bonzini         s->acmd12errsts |= value;
114249ab747fSPaolo Bonzini         s->errintsts |= (value >> 16) & s->errintstsen;
114349ab747fSPaolo Bonzini         if (s->acmd12errsts) {
114449ab747fSPaolo Bonzini             s->errintsts |= SDHC_EIS_CMD12ERR;
114549ab747fSPaolo Bonzini         }
114649ab747fSPaolo Bonzini         if (s->errintsts) {
114749ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
114849ab747fSPaolo Bonzini         }
114949ab747fSPaolo Bonzini         sdhci_update_irq(s);
115049ab747fSPaolo Bonzini         break;
115149ab747fSPaolo Bonzini     default:
115249ab747fSPaolo Bonzini         ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n",
1153d368ba43SKevin O'Connor                  size, (int)offset, value >> shift, value >> shift);
115449ab747fSPaolo Bonzini         break;
115549ab747fSPaolo Bonzini     }
115649ab747fSPaolo Bonzini     DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n",
1157d368ba43SKevin O'Connor               size, (int)offset, value >> shift, value >> shift);
115849ab747fSPaolo Bonzini }
115949ab747fSPaolo Bonzini 
116049ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = {
1161d368ba43SKevin O'Connor     .read = sdhci_read,
1162d368ba43SKevin O'Connor     .write = sdhci_write,
116349ab747fSPaolo Bonzini     .valid = {
116449ab747fSPaolo Bonzini         .min_access_size = 1,
116549ab747fSPaolo Bonzini         .max_access_size = 4,
116649ab747fSPaolo Bonzini         .unaligned = false
116749ab747fSPaolo Bonzini     },
116849ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
116949ab747fSPaolo Bonzini };
117049ab747fSPaolo Bonzini 
117149ab747fSPaolo Bonzini static inline unsigned int sdhci_get_fifolen(SDHCIState *s)
117249ab747fSPaolo Bonzini {
117349ab747fSPaolo Bonzini     switch (SDHC_CAPAB_BLOCKSIZE(s->capareg)) {
117449ab747fSPaolo Bonzini     case 0:
117549ab747fSPaolo Bonzini         return 512;
117649ab747fSPaolo Bonzini     case 1:
117749ab747fSPaolo Bonzini         return 1024;
117849ab747fSPaolo Bonzini     case 2:
117949ab747fSPaolo Bonzini         return 2048;
118049ab747fSPaolo Bonzini     default:
118149ab747fSPaolo Bonzini         hw_error("SDHC: unsupported value for maximum block size\n");
118249ab747fSPaolo Bonzini         return 0;
118349ab747fSPaolo Bonzini     }
118449ab747fSPaolo Bonzini }
118549ab747fSPaolo Bonzini 
118640bbc194SPeter Maydell static void sdhci_initfn(SDHCIState *s)
118749ab747fSPaolo Bonzini {
118840bbc194SPeter Maydell     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus),
118940bbc194SPeter Maydell                         TYPE_SDHCI_BUS, DEVICE(s), "sd-bus");
119049ab747fSPaolo Bonzini 
1191bc72ad67SAlex Bligh     s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s);
1192d368ba43SKevin O'Connor     s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s);
119349ab747fSPaolo Bonzini }
119449ab747fSPaolo Bonzini 
11957302dcd6SKevin O'Connor static void sdhci_uninitfn(SDHCIState *s)
119649ab747fSPaolo Bonzini {
1197bc72ad67SAlex Bligh     timer_del(s->insert_timer);
1198bc72ad67SAlex Bligh     timer_free(s->insert_timer);
1199bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
1200bc72ad67SAlex Bligh     timer_free(s->transfer_timer);
1201127a4e1aSAndreas Färber     qemu_free_irq(s->eject_cb);
1202127a4e1aSAndreas Färber     qemu_free_irq(s->ro_cb);
120349ab747fSPaolo Bonzini 
120449ab747fSPaolo Bonzini     g_free(s->fifo_buffer);
120549ab747fSPaolo Bonzini     s->fifo_buffer = NULL;
120649ab747fSPaolo Bonzini }
120749ab747fSPaolo Bonzini 
12080a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque)
12090a7ac9f9SAndrew Baumann {
12100a7ac9f9SAndrew Baumann     SDHCIState *s = opaque;
12110a7ac9f9SAndrew Baumann 
12120a7ac9f9SAndrew Baumann     return s->pending_insert_state;
12130a7ac9f9SAndrew Baumann }
12140a7ac9f9SAndrew Baumann 
12150a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = {
12160a7ac9f9SAndrew Baumann     .name = "sdhci/pending-insert",
12170a7ac9f9SAndrew Baumann     .version_id = 1,
12180a7ac9f9SAndrew Baumann     .minimum_version_id = 1,
12190a7ac9f9SAndrew Baumann     .needed = sdhci_pending_insert_vmstate_needed,
12200a7ac9f9SAndrew Baumann     .fields = (VMStateField[]) {
12210a7ac9f9SAndrew Baumann         VMSTATE_BOOL(pending_insert_state, SDHCIState),
12220a7ac9f9SAndrew Baumann         VMSTATE_END_OF_LIST()
12230a7ac9f9SAndrew Baumann     },
12240a7ac9f9SAndrew Baumann };
12250a7ac9f9SAndrew Baumann 
122649ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = {
122749ab747fSPaolo Bonzini     .name = "sdhci",
122849ab747fSPaolo Bonzini     .version_id = 1,
122949ab747fSPaolo Bonzini     .minimum_version_id = 1,
123049ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
123149ab747fSPaolo Bonzini         VMSTATE_UINT32(sdmasysad, SDHCIState),
123249ab747fSPaolo Bonzini         VMSTATE_UINT16(blksize, SDHCIState),
123349ab747fSPaolo Bonzini         VMSTATE_UINT16(blkcnt, SDHCIState),
123449ab747fSPaolo Bonzini         VMSTATE_UINT32(argument, SDHCIState),
123549ab747fSPaolo Bonzini         VMSTATE_UINT16(trnmod, SDHCIState),
123649ab747fSPaolo Bonzini         VMSTATE_UINT16(cmdreg, SDHCIState),
123749ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4),
123849ab747fSPaolo Bonzini         VMSTATE_UINT32(prnsts, SDHCIState),
123949ab747fSPaolo Bonzini         VMSTATE_UINT8(hostctl, SDHCIState),
124049ab747fSPaolo Bonzini         VMSTATE_UINT8(pwrcon, SDHCIState),
124149ab747fSPaolo Bonzini         VMSTATE_UINT8(blkgap, SDHCIState),
124249ab747fSPaolo Bonzini         VMSTATE_UINT8(wakcon, SDHCIState),
124349ab747fSPaolo Bonzini         VMSTATE_UINT16(clkcon, SDHCIState),
124449ab747fSPaolo Bonzini         VMSTATE_UINT8(timeoutcon, SDHCIState),
124549ab747fSPaolo Bonzini         VMSTATE_UINT8(admaerr, SDHCIState),
124649ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsts, SDHCIState),
124749ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsts, SDHCIState),
124849ab747fSPaolo Bonzini         VMSTATE_UINT16(norintstsen, SDHCIState),
124949ab747fSPaolo Bonzini         VMSTATE_UINT16(errintstsen, SDHCIState),
125049ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsigen, SDHCIState),
125149ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsigen, SDHCIState),
125249ab747fSPaolo Bonzini         VMSTATE_UINT16(acmd12errsts, SDHCIState),
125349ab747fSPaolo Bonzini         VMSTATE_UINT16(data_count, SDHCIState),
125449ab747fSPaolo Bonzini         VMSTATE_UINT64(admasysaddr, SDHCIState),
125549ab747fSPaolo Bonzini         VMSTATE_UINT8(stopped_state, SDHCIState),
1256*59046ec2SHalil Pasic         VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz),
1257e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(insert_timer, SDHCIState),
1258e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(transfer_timer, SDHCIState),
125949ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
12600a7ac9f9SAndrew Baumann     },
12610a7ac9f9SAndrew Baumann     .subsections = (const VMStateDescription*[]) {
12620a7ac9f9SAndrew Baumann         &sdhci_pending_insert_vmstate,
12630a7ac9f9SAndrew Baumann         NULL
12640a7ac9f9SAndrew Baumann     },
126549ab747fSPaolo Bonzini };
126649ab747fSPaolo Bonzini 
126749ab747fSPaolo Bonzini /* Capabilities registers provide information on supported features of this
126849ab747fSPaolo Bonzini  * specific host controller implementation */
12695ec911c3SKevin O'Connor static Property sdhci_pci_properties[] = {
1270c7bcc85dSPaolo Bonzini     DEFINE_PROP_UINT32("capareg", SDHCIState, capareg,
127149ab747fSPaolo Bonzini             SDHC_CAPAB_REG_DEFAULT),
1272c7bcc85dSPaolo Bonzini     DEFINE_PROP_UINT32("maxcurr", SDHCIState, maxcurr, 0),
127349ab747fSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
127449ab747fSPaolo Bonzini };
127549ab747fSPaolo Bonzini 
12769af21dbeSMarkus Armbruster static void sdhci_pci_realize(PCIDevice *dev, Error **errp)
1277224d10ffSKevin O'Connor {
1278224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
1279224d10ffSKevin O'Connor     dev->config[PCI_CLASS_PROG] = 0x01; /* Standard Host supported DMA */
1280224d10ffSKevin O'Connor     dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
128140bbc194SPeter Maydell     sdhci_initfn(s);
1282224d10ffSKevin O'Connor     s->buf_maxsz = sdhci_get_fifolen(s);
1283224d10ffSKevin O'Connor     s->fifo_buffer = g_malloc0(s->buf_maxsz);
1284224d10ffSKevin O'Connor     s->irq = pci_allocate_irq(dev);
1285224d10ffSKevin O'Connor     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci",
1286224d10ffSKevin O'Connor             SDHC_REGISTERS_MAP_SIZE);
1287224d10ffSKevin O'Connor     pci_register_bar(dev, 0, 0, &s->iomem);
1288224d10ffSKevin O'Connor }
1289224d10ffSKevin O'Connor 
1290224d10ffSKevin O'Connor static void sdhci_pci_exit(PCIDevice *dev)
1291224d10ffSKevin O'Connor {
1292224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
1293224d10ffSKevin O'Connor     sdhci_uninitfn(s);
1294224d10ffSKevin O'Connor }
1295224d10ffSKevin O'Connor 
1296224d10ffSKevin O'Connor static void sdhci_pci_class_init(ObjectClass *klass, void *data)
1297224d10ffSKevin O'Connor {
1298224d10ffSKevin O'Connor     DeviceClass *dc = DEVICE_CLASS(klass);
1299224d10ffSKevin O'Connor     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1300224d10ffSKevin O'Connor 
13019af21dbeSMarkus Armbruster     k->realize = sdhci_pci_realize;
1302224d10ffSKevin O'Connor     k->exit = sdhci_pci_exit;
1303224d10ffSKevin O'Connor     k->vendor_id = PCI_VENDOR_ID_REDHAT;
1304224d10ffSKevin O'Connor     k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI;
1305224d10ffSKevin O'Connor     k->class_id = PCI_CLASS_SYSTEM_SDHCI;
1306224d10ffSKevin O'Connor     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1307224d10ffSKevin O'Connor     dc->vmsd = &sdhci_vmstate;
13085ec911c3SKevin O'Connor     dc->props = sdhci_pci_properties;
13098b41c305SPeter Maydell     dc->reset = sdhci_poweron_reset;
1310224d10ffSKevin O'Connor }
1311224d10ffSKevin O'Connor 
1312224d10ffSKevin O'Connor static const TypeInfo sdhci_pci_info = {
1313224d10ffSKevin O'Connor     .name = TYPE_PCI_SDHCI,
1314224d10ffSKevin O'Connor     .parent = TYPE_PCI_DEVICE,
1315224d10ffSKevin O'Connor     .instance_size = sizeof(SDHCIState),
1316224d10ffSKevin O'Connor     .class_init = sdhci_pci_class_init,
1317224d10ffSKevin O'Connor };
1318224d10ffSKevin O'Connor 
13195ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = {
13205ec911c3SKevin O'Connor     DEFINE_PROP_UINT32("capareg", SDHCIState, capareg,
13215ec911c3SKevin O'Connor             SDHC_CAPAB_REG_DEFAULT),
13225ec911c3SKevin O'Connor     DEFINE_PROP_UINT32("maxcurr", SDHCIState, maxcurr, 0),
13230a7ac9f9SAndrew Baumann     DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk,
13240a7ac9f9SAndrew Baumann                      false),
13255ec911c3SKevin O'Connor     DEFINE_PROP_END_OF_LIST(),
13265ec911c3SKevin O'Connor };
13275ec911c3SKevin O'Connor 
13287302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj)
132949ab747fSPaolo Bonzini {
13307302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
13315ec911c3SKevin O'Connor 
133240bbc194SPeter Maydell     sdhci_initfn(s);
13337302dcd6SKevin O'Connor }
13347302dcd6SKevin O'Connor 
13357302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj)
13367302dcd6SKevin O'Connor {
13377302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
13387302dcd6SKevin O'Connor     sdhci_uninitfn(s);
13397302dcd6SKevin O'Connor }
13407302dcd6SKevin O'Connor 
13417302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp)
13427302dcd6SKevin O'Connor {
13437302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(dev);
134449ab747fSPaolo Bonzini     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
134549ab747fSPaolo Bonzini 
134649ab747fSPaolo Bonzini     s->buf_maxsz = sdhci_get_fifolen(s);
134749ab747fSPaolo Bonzini     s->fifo_buffer = g_malloc0(s->buf_maxsz);
134849ab747fSPaolo Bonzini     sysbus_init_irq(sbd, &s->irq);
134929776739SPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci",
135049ab747fSPaolo Bonzini             SDHC_REGISTERS_MAP_SIZE);
135149ab747fSPaolo Bonzini     sysbus_init_mmio(sbd, &s->iomem);
135249ab747fSPaolo Bonzini }
135349ab747fSPaolo Bonzini 
13547302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)
135549ab747fSPaolo Bonzini {
135649ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
135749ab747fSPaolo Bonzini 
135849ab747fSPaolo Bonzini     dc->vmsd = &sdhci_vmstate;
13595ec911c3SKevin O'Connor     dc->props = sdhci_sysbus_properties;
13607302dcd6SKevin O'Connor     dc->realize = sdhci_sysbus_realize;
13618b41c305SPeter Maydell     dc->reset = sdhci_poweron_reset;
136249ab747fSPaolo Bonzini }
136349ab747fSPaolo Bonzini 
13647302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = {
13657302dcd6SKevin O'Connor     .name = TYPE_SYSBUS_SDHCI,
136649ab747fSPaolo Bonzini     .parent = TYPE_SYS_BUS_DEVICE,
136749ab747fSPaolo Bonzini     .instance_size = sizeof(SDHCIState),
13687302dcd6SKevin O'Connor     .instance_init = sdhci_sysbus_init,
13697302dcd6SKevin O'Connor     .instance_finalize = sdhci_sysbus_finalize,
13707302dcd6SKevin O'Connor     .class_init = sdhci_sysbus_class_init,
137149ab747fSPaolo Bonzini };
137249ab747fSPaolo Bonzini 
137340bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data)
137440bbc194SPeter Maydell {
137540bbc194SPeter Maydell     SDBusClass *sbc = SD_BUS_CLASS(klass);
137640bbc194SPeter Maydell 
137740bbc194SPeter Maydell     sbc->set_inserted = sdhci_set_inserted;
137840bbc194SPeter Maydell     sbc->set_readonly = sdhci_set_readonly;
137940bbc194SPeter Maydell }
138040bbc194SPeter Maydell 
138140bbc194SPeter Maydell static const TypeInfo sdhci_bus_info = {
138240bbc194SPeter Maydell     .name = TYPE_SDHCI_BUS,
138340bbc194SPeter Maydell     .parent = TYPE_SD_BUS,
138440bbc194SPeter Maydell     .instance_size = sizeof(SDBus),
138540bbc194SPeter Maydell     .class_init = sdhci_bus_class_init,
138640bbc194SPeter Maydell };
138740bbc194SPeter Maydell 
138849ab747fSPaolo Bonzini static void sdhci_register_types(void)
138949ab747fSPaolo Bonzini {
1390224d10ffSKevin O'Connor     type_register_static(&sdhci_pci_info);
13917302dcd6SKevin O'Connor     type_register_static(&sdhci_sysbus_info);
139240bbc194SPeter Maydell     type_register_static(&sdhci_bus_info);
139349ab747fSPaolo Bonzini }
139449ab747fSPaolo Bonzini 
139549ab747fSPaolo Bonzini type_init(sdhci_register_types)
1396