xref: /openbmc/qemu/hw/sd/sdhci.c (revision bc72ad67543f5c5d39c005ff0ca72da37642a1fb)
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 
2549ab747fSPaolo Bonzini #include "hw/hw.h"
2649ab747fSPaolo Bonzini #include "sysemu/blockdev.h"
2749ab747fSPaolo Bonzini #include "sysemu/dma.h"
2849ab747fSPaolo Bonzini #include "qemu/timer.h"
2949ab747fSPaolo Bonzini #include "block/block_int.h"
3049ab747fSPaolo Bonzini #include "qemu/bitops.h"
3149ab747fSPaolo Bonzini 
3247b43a1fSPaolo Bonzini #include "sdhci.h"
3349ab747fSPaolo Bonzini 
3449ab747fSPaolo Bonzini /* host controller debug messages */
3549ab747fSPaolo Bonzini #ifndef SDHC_DEBUG
3649ab747fSPaolo Bonzini #define SDHC_DEBUG                        0
3749ab747fSPaolo Bonzini #endif
3849ab747fSPaolo Bonzini 
3949ab747fSPaolo Bonzini #if SDHC_DEBUG == 0
4049ab747fSPaolo Bonzini     #define DPRINT_L1(fmt, args...)       do { } while (0)
4149ab747fSPaolo Bonzini     #define DPRINT_L2(fmt, args...)       do { } while (0)
4249ab747fSPaolo Bonzini     #define ERRPRINT(fmt, args...)        do { } while (0)
4349ab747fSPaolo Bonzini #elif SDHC_DEBUG == 1
4449ab747fSPaolo Bonzini     #define DPRINT_L1(fmt, args...)       \
4549ab747fSPaolo Bonzini         do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0)
4649ab747fSPaolo Bonzini     #define DPRINT_L2(fmt, args...)       do { } while (0)
4749ab747fSPaolo Bonzini     #define ERRPRINT(fmt, args...)        \
4849ab747fSPaolo Bonzini         do {fprintf(stderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0)
4949ab747fSPaolo Bonzini #else
5049ab747fSPaolo Bonzini     #define DPRINT_L1(fmt, args...)       \
5149ab747fSPaolo Bonzini         do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0)
5249ab747fSPaolo Bonzini     #define DPRINT_L2(fmt, args...)       \
5349ab747fSPaolo Bonzini         do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0)
5449ab747fSPaolo Bonzini     #define ERRPRINT(fmt, args...)        \
5549ab747fSPaolo Bonzini         do {fprintf(stderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0)
5649ab747fSPaolo Bonzini #endif
5749ab747fSPaolo Bonzini 
5849ab747fSPaolo Bonzini /* Default SD/MMC host controller features information, which will be
5949ab747fSPaolo Bonzini  * presented in CAPABILITIES register of generic SD host controller at reset.
6049ab747fSPaolo Bonzini  * If not stated otherwise:
6149ab747fSPaolo Bonzini  * 0 - not supported, 1 - supported, other - prohibited.
6249ab747fSPaolo Bonzini  */
6349ab747fSPaolo Bonzini #define SDHC_CAPAB_64BITBUS       0ul        /* 64-bit System Bus Support */
6449ab747fSPaolo Bonzini #define SDHC_CAPAB_18V            1ul        /* Voltage support 1.8v */
6549ab747fSPaolo Bonzini #define SDHC_CAPAB_30V            0ul        /* Voltage support 3.0v */
6649ab747fSPaolo Bonzini #define SDHC_CAPAB_33V            1ul        /* Voltage support 3.3v */
6749ab747fSPaolo Bonzini #define SDHC_CAPAB_SUSPRESUME     0ul        /* Suspend/resume support */
6849ab747fSPaolo Bonzini #define SDHC_CAPAB_SDMA           1ul        /* SDMA support */
6949ab747fSPaolo Bonzini #define SDHC_CAPAB_HIGHSPEED      1ul        /* High speed support */
7049ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA1          1ul        /* ADMA1 support */
7149ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA2          1ul        /* ADMA2 support */
7249ab747fSPaolo Bonzini /* Maximum host controller R/W buffers size
7349ab747fSPaolo Bonzini  * Possible values: 512, 1024, 2048 bytes */
7449ab747fSPaolo Bonzini #define SDHC_CAPAB_MAXBLOCKLENGTH 512ul
7549ab747fSPaolo Bonzini /* Maximum clock frequency for SDclock in MHz
7649ab747fSPaolo Bonzini  * value in range 10-63 MHz, 0 - not defined */
7749ab747fSPaolo Bonzini #define SDHC_CAPAB_BASECLKFREQ    0ul
7849ab747fSPaolo Bonzini #define SDHC_CAPAB_TOUNIT         1ul  /* Timeout clock unit 0 - kHz, 1 - MHz */
7949ab747fSPaolo Bonzini /* Timeout clock frequency 1-63, 0 - not defined */
8049ab747fSPaolo Bonzini #define SDHC_CAPAB_TOCLKFREQ      0ul
8149ab747fSPaolo Bonzini 
8249ab747fSPaolo Bonzini /* Now check all parameters and calculate CAPABILITIES REGISTER value */
8349ab747fSPaolo Bonzini #if SDHC_CAPAB_64BITBUS > 1 || SDHC_CAPAB_18V > 1 || SDHC_CAPAB_30V > 1 ||     \
8449ab747fSPaolo Bonzini     SDHC_CAPAB_33V > 1 || SDHC_CAPAB_SUSPRESUME > 1 || SDHC_CAPAB_SDMA > 1 ||  \
8549ab747fSPaolo Bonzini     SDHC_CAPAB_HIGHSPEED > 1 || SDHC_CAPAB_ADMA2 > 1 || SDHC_CAPAB_ADMA1 > 1 ||\
8649ab747fSPaolo Bonzini     SDHC_CAPAB_TOUNIT > 1
8749ab747fSPaolo Bonzini #error Capabilities features can have value 0 or 1 only!
8849ab747fSPaolo Bonzini #endif
8949ab747fSPaolo Bonzini 
9049ab747fSPaolo Bonzini #if SDHC_CAPAB_MAXBLOCKLENGTH == 512
9149ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 0ul
9249ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 1024
9349ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 1ul
9449ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 2048
9549ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 2ul
9649ab747fSPaolo Bonzini #else
9749ab747fSPaolo Bonzini #error Max host controller block size can have value 512, 1024 or 2048 only!
9849ab747fSPaolo Bonzini #endif
9949ab747fSPaolo Bonzini 
10049ab747fSPaolo Bonzini #if (SDHC_CAPAB_BASECLKFREQ > 0 && SDHC_CAPAB_BASECLKFREQ < 10) || \
10149ab747fSPaolo Bonzini     SDHC_CAPAB_BASECLKFREQ > 63
10249ab747fSPaolo Bonzini #error SDclock frequency can have value in range 0, 10-63 only!
10349ab747fSPaolo Bonzini #endif
10449ab747fSPaolo Bonzini 
10549ab747fSPaolo Bonzini #if SDHC_CAPAB_TOCLKFREQ > 63
10649ab747fSPaolo Bonzini #error Timeout clock frequency can have value in range 0-63 only!
10749ab747fSPaolo Bonzini #endif
10849ab747fSPaolo Bonzini 
10949ab747fSPaolo Bonzini #define SDHC_CAPAB_REG_DEFAULT                                 \
11049ab747fSPaolo Bonzini    ((SDHC_CAPAB_64BITBUS << 28) | (SDHC_CAPAB_18V << 26) |     \
11149ab747fSPaolo Bonzini     (SDHC_CAPAB_30V << 25) | (SDHC_CAPAB_33V << 24) |          \
11249ab747fSPaolo Bonzini     (SDHC_CAPAB_SUSPRESUME << 23) | (SDHC_CAPAB_SDMA << 22) |  \
11349ab747fSPaolo Bonzini     (SDHC_CAPAB_HIGHSPEED << 21) | (SDHC_CAPAB_ADMA1 << 20) |  \
11449ab747fSPaolo Bonzini     (SDHC_CAPAB_ADMA2 << 19) | (MAX_BLOCK_LENGTH << 16) |      \
11549ab747fSPaolo Bonzini     (SDHC_CAPAB_BASECLKFREQ << 8) | (SDHC_CAPAB_TOUNIT << 7) | \
11649ab747fSPaolo Bonzini     (SDHC_CAPAB_TOCLKFREQ))
11749ab747fSPaolo Bonzini 
11849ab747fSPaolo Bonzini #define MASKED_WRITE(reg, mask, val)  (reg = (reg & (mask)) | (val))
11949ab747fSPaolo Bonzini 
12049ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s)
12149ab747fSPaolo Bonzini {
12249ab747fSPaolo Bonzini     return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) ||
12349ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) ||
12449ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV));
12549ab747fSPaolo Bonzini }
12649ab747fSPaolo Bonzini 
12749ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s)
12849ab747fSPaolo Bonzini {
12949ab747fSPaolo Bonzini     qemu_set_irq(s->irq, sdhci_slotint(s));
13049ab747fSPaolo Bonzini }
13149ab747fSPaolo Bonzini 
13249ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque)
13349ab747fSPaolo Bonzini {
13449ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
13549ab747fSPaolo Bonzini 
13649ab747fSPaolo Bonzini     if (s->norintsts & SDHC_NIS_REMOVE) {
137*bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
138*bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
13949ab747fSPaolo Bonzini     } else {
14049ab747fSPaolo Bonzini         s->prnsts = 0x1ff0000;
14149ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_INSERT) {
14249ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_INSERT;
14349ab747fSPaolo Bonzini         }
14449ab747fSPaolo Bonzini         sdhci_update_irq(s);
14549ab747fSPaolo Bonzini     }
14649ab747fSPaolo Bonzini }
14749ab747fSPaolo Bonzini 
14849ab747fSPaolo Bonzini static void sdhci_insert_eject_cb(void *opaque, int irq, int level)
14949ab747fSPaolo Bonzini {
15049ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
15149ab747fSPaolo Bonzini     DPRINT_L1("Card state changed: %s!\n", level ? "insert" : "eject");
15249ab747fSPaolo Bonzini 
15349ab747fSPaolo Bonzini     if ((s->norintsts & SDHC_NIS_REMOVE) && level) {
15449ab747fSPaolo Bonzini         /* Give target some time to notice card ejection */
155*bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
156*bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
15749ab747fSPaolo Bonzini     } else {
15849ab747fSPaolo Bonzini         if (level) {
15949ab747fSPaolo Bonzini             s->prnsts = 0x1ff0000;
16049ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_INSERT) {
16149ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_INSERT;
16249ab747fSPaolo Bonzini             }
16349ab747fSPaolo Bonzini         } else {
16449ab747fSPaolo Bonzini             s->prnsts = 0x1fa0000;
16549ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
16649ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_SDCLK_EN;
16749ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_REMOVE) {
16849ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_REMOVE;
16949ab747fSPaolo Bonzini             }
17049ab747fSPaolo Bonzini         }
17149ab747fSPaolo Bonzini         sdhci_update_irq(s);
17249ab747fSPaolo Bonzini     }
17349ab747fSPaolo Bonzini }
17449ab747fSPaolo Bonzini 
17549ab747fSPaolo Bonzini static void sdhci_card_readonly_cb(void *opaque, int irq, int level)
17649ab747fSPaolo Bonzini {
17749ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
17849ab747fSPaolo Bonzini 
17949ab747fSPaolo Bonzini     if (level) {
18049ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_WRITE_PROTECT;
18149ab747fSPaolo Bonzini     } else {
18249ab747fSPaolo Bonzini         /* Write enabled */
18349ab747fSPaolo Bonzini         s->prnsts |= SDHC_WRITE_PROTECT;
18449ab747fSPaolo Bonzini     }
18549ab747fSPaolo Bonzini }
18649ab747fSPaolo Bonzini 
18749ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s)
18849ab747fSPaolo Bonzini {
189*bc72ad67SAlex Bligh     timer_del(s->insert_timer);
190*bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
19149ab747fSPaolo Bonzini     /* Set all registers to 0. Capabilities registers are not cleared
19249ab747fSPaolo Bonzini      * and assumed to always preserve their value, given to them during
19349ab747fSPaolo Bonzini      * initialization */
19449ab747fSPaolo Bonzini     memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad);
19549ab747fSPaolo Bonzini 
19649ab747fSPaolo Bonzini     sd_set_cb(s->card, s->ro_cb, s->eject_cb);
19749ab747fSPaolo Bonzini     s->data_count = 0;
19849ab747fSPaolo Bonzini     s->stopped_state = sdhc_not_stopped;
19949ab747fSPaolo Bonzini }
20049ab747fSPaolo Bonzini 
20149ab747fSPaolo Bonzini static void sdhci_do_data_transfer(void *opaque)
20249ab747fSPaolo Bonzini {
20349ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
20449ab747fSPaolo Bonzini 
20549ab747fSPaolo Bonzini     SDHCI_GET_CLASS(s)->data_transfer(s);
20649ab747fSPaolo Bonzini }
20749ab747fSPaolo Bonzini 
20849ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s)
20949ab747fSPaolo Bonzini {
21049ab747fSPaolo Bonzini     SDRequest request;
21149ab747fSPaolo Bonzini     uint8_t response[16];
21249ab747fSPaolo Bonzini     int rlen;
21349ab747fSPaolo Bonzini 
21449ab747fSPaolo Bonzini     s->errintsts = 0;
21549ab747fSPaolo Bonzini     s->acmd12errsts = 0;
21649ab747fSPaolo Bonzini     request.cmd = s->cmdreg >> 8;
21749ab747fSPaolo Bonzini     request.arg = s->argument;
21849ab747fSPaolo Bonzini     DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request.cmd, request.arg);
21949ab747fSPaolo Bonzini     rlen = sd_do_command(s->card, &request, response);
22049ab747fSPaolo Bonzini 
22149ab747fSPaolo Bonzini     if (s->cmdreg & SDHC_CMD_RESPONSE) {
22249ab747fSPaolo Bonzini         if (rlen == 4) {
22349ab747fSPaolo Bonzini             s->rspreg[0] = (response[0] << 24) | (response[1] << 16) |
22449ab747fSPaolo Bonzini                            (response[2] << 8)  |  response[3];
22549ab747fSPaolo Bonzini             s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0;
22649ab747fSPaolo Bonzini             DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s->rspreg[0]);
22749ab747fSPaolo Bonzini         } else if (rlen == 16) {
22849ab747fSPaolo Bonzini             s->rspreg[0] = (response[11] << 24) | (response[12] << 16) |
22949ab747fSPaolo Bonzini                            (response[13] << 8) |  response[14];
23049ab747fSPaolo Bonzini             s->rspreg[1] = (response[7] << 24) | (response[8] << 16) |
23149ab747fSPaolo Bonzini                            (response[9] << 8)  |  response[10];
23249ab747fSPaolo Bonzini             s->rspreg[2] = (response[3] << 24) | (response[4] << 16) |
23349ab747fSPaolo Bonzini                            (response[5] << 8)  |  response[6];
23449ab747fSPaolo Bonzini             s->rspreg[3] = (response[0] << 16) | (response[1] << 8) |
23549ab747fSPaolo Bonzini                             response[2];
23649ab747fSPaolo Bonzini             DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.."
23749ab747fSPaolo Bonzini                   "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n",
23849ab747fSPaolo Bonzini                   s->rspreg[3], s->rspreg[2], s->rspreg[1], s->rspreg[0]);
23949ab747fSPaolo Bonzini         } else {
24049ab747fSPaolo Bonzini             ERRPRINT("Timeout waiting for command response\n");
24149ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) {
24249ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_CMDTIMEOUT;
24349ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
24449ab747fSPaolo Bonzini             }
24549ab747fSPaolo Bonzini         }
24649ab747fSPaolo Bonzini 
24749ab747fSPaolo Bonzini         if ((s->norintstsen & SDHC_NISEN_TRSCMP) &&
24849ab747fSPaolo Bonzini             (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) {
24949ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_TRSCMP;
25049ab747fSPaolo Bonzini         }
25149ab747fSPaolo Bonzini     } else if (rlen != 0 && (s->errintstsen & SDHC_EISEN_CMDIDX)) {
25249ab747fSPaolo Bonzini         s->errintsts |= SDHC_EIS_CMDIDX;
25349ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_ERR;
25449ab747fSPaolo Bonzini     }
25549ab747fSPaolo Bonzini 
25649ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_CMDCMP) {
25749ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_CMDCMP;
25849ab747fSPaolo Bonzini     }
25949ab747fSPaolo Bonzini 
26049ab747fSPaolo Bonzini     sdhci_update_irq(s);
26149ab747fSPaolo Bonzini 
26249ab747fSPaolo Bonzini     if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) {
263656f416cSPeter Crosthwaite         s->data_count = 0;
26449ab747fSPaolo Bonzini         sdhci_do_data_transfer(s);
26549ab747fSPaolo Bonzini     }
26649ab747fSPaolo Bonzini }
26749ab747fSPaolo Bonzini 
26849ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s)
26949ab747fSPaolo Bonzini {
27049ab747fSPaolo Bonzini     /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
27149ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) {
27249ab747fSPaolo Bonzini         SDRequest request;
27349ab747fSPaolo Bonzini         uint8_t response[16];
27449ab747fSPaolo Bonzini 
27549ab747fSPaolo Bonzini         request.cmd = 0x0C;
27649ab747fSPaolo Bonzini         request.arg = 0;
27749ab747fSPaolo Bonzini         DPRINT_L1("Automatically issue CMD%d %08x\n", request.cmd, request.arg);
27849ab747fSPaolo Bonzini         sd_do_command(s->card, &request, response);
27949ab747fSPaolo Bonzini         /* Auto CMD12 response goes to the upper Response register */
28049ab747fSPaolo Bonzini         s->rspreg[3] = (response[0] << 24) | (response[1] << 16) |
28149ab747fSPaolo Bonzini                 (response[2] << 8) | response[3];
28249ab747fSPaolo Bonzini     }
28349ab747fSPaolo Bonzini 
28449ab747fSPaolo Bonzini     s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE |
28549ab747fSPaolo Bonzini             SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT |
28649ab747fSPaolo Bonzini             SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE);
28749ab747fSPaolo Bonzini 
28849ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_TRSCMP) {
28949ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_TRSCMP;
29049ab747fSPaolo Bonzini     }
29149ab747fSPaolo Bonzini 
29249ab747fSPaolo Bonzini     sdhci_update_irq(s);
29349ab747fSPaolo Bonzini }
29449ab747fSPaolo Bonzini 
29549ab747fSPaolo Bonzini /*
29649ab747fSPaolo Bonzini  * Programmed i/o data transfer
29749ab747fSPaolo Bonzini  */
29849ab747fSPaolo Bonzini 
29949ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
30049ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s)
30149ab747fSPaolo Bonzini {
30249ab747fSPaolo Bonzini     int index = 0;
30349ab747fSPaolo Bonzini 
30449ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) &&
30549ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) {
30649ab747fSPaolo Bonzini         return;
30749ab747fSPaolo Bonzini     }
30849ab747fSPaolo Bonzini 
30949ab747fSPaolo Bonzini     for (index = 0; index < (s->blksize & 0x0fff); index++) {
31049ab747fSPaolo Bonzini         s->fifo_buffer[index] = sd_read_data(s->card);
31149ab747fSPaolo Bonzini     }
31249ab747fSPaolo Bonzini 
31349ab747fSPaolo Bonzini     /* New data now available for READ through Buffer Port Register */
31449ab747fSPaolo Bonzini     s->prnsts |= SDHC_DATA_AVAILABLE;
31549ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_RBUFRDY) {
31649ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_RBUFRDY;
31749ab747fSPaolo Bonzini     }
31849ab747fSPaolo Bonzini 
31949ab747fSPaolo Bonzini     /* Clear DAT line active status if that was the last block */
32049ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
32149ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) {
32249ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
32349ab747fSPaolo Bonzini     }
32449ab747fSPaolo Bonzini 
32549ab747fSPaolo Bonzini     /* If stop at block gap request was set and it's not the last block of
32649ab747fSPaolo Bonzini      * data - generate Block Event interrupt */
32749ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) &&
32849ab747fSPaolo Bonzini             s->blkcnt != 1)    {
32949ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
33049ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
33149ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
33249ab747fSPaolo Bonzini         }
33349ab747fSPaolo Bonzini     }
33449ab747fSPaolo Bonzini 
33549ab747fSPaolo Bonzini     sdhci_update_irq(s);
33649ab747fSPaolo Bonzini }
33749ab747fSPaolo Bonzini 
33849ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
33949ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size)
34049ab747fSPaolo Bonzini {
34149ab747fSPaolo Bonzini     uint32_t value = 0;
34249ab747fSPaolo Bonzini     int i;
34349ab747fSPaolo Bonzini 
34449ab747fSPaolo Bonzini     /* first check that a valid data exists in host controller input buffer */
34549ab747fSPaolo Bonzini     if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) {
34649ab747fSPaolo Bonzini         ERRPRINT("Trying to read from empty buffer\n");
34749ab747fSPaolo Bonzini         return 0;
34849ab747fSPaolo Bonzini     }
34949ab747fSPaolo Bonzini 
35049ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
35149ab747fSPaolo Bonzini         value |= s->fifo_buffer[s->data_count] << i * 8;
35249ab747fSPaolo Bonzini         s->data_count++;
35349ab747fSPaolo Bonzini         /* check if we've read all valid data (blksize bytes) from buffer */
35449ab747fSPaolo Bonzini         if ((s->data_count) >= (s->blksize & 0x0fff)) {
35549ab747fSPaolo Bonzini             DPRINT_L2("All %u bytes of data have been read from input buffer\n",
35649ab747fSPaolo Bonzini                     s->data_count);
35749ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */
35849ab747fSPaolo Bonzini             s->data_count = 0;  /* next buff read must start at position [0] */
35949ab747fSPaolo Bonzini 
36049ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
36149ab747fSPaolo Bonzini                 s->blkcnt--;
36249ab747fSPaolo Bonzini             }
36349ab747fSPaolo Bonzini 
36449ab747fSPaolo Bonzini             /* if that was the last block of data */
36549ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
36649ab747fSPaolo Bonzini                 ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) ||
36749ab747fSPaolo Bonzini                  /* stop at gap request */
36849ab747fSPaolo Bonzini                 (s->stopped_state == sdhc_gap_read &&
36949ab747fSPaolo Bonzini                  !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) {
37049ab747fSPaolo Bonzini                 SDHCI_GET_CLASS(s)->end_data_transfer(s);
37149ab747fSPaolo Bonzini             } else { /* if there are more data, read next block from card */
37249ab747fSPaolo Bonzini                 SDHCI_GET_CLASS(s)->read_block_from_card(s);
37349ab747fSPaolo Bonzini             }
37449ab747fSPaolo Bonzini             break;
37549ab747fSPaolo Bonzini         }
37649ab747fSPaolo Bonzini     }
37749ab747fSPaolo Bonzini 
37849ab747fSPaolo Bonzini     return value;
37949ab747fSPaolo Bonzini }
38049ab747fSPaolo Bonzini 
38149ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */
38249ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s)
38349ab747fSPaolo Bonzini {
38449ab747fSPaolo Bonzini     int index = 0;
38549ab747fSPaolo Bonzini 
38649ab747fSPaolo Bonzini     if (s->prnsts & SDHC_SPACE_AVAILABLE) {
38749ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
38849ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_WBUFRDY;
38949ab747fSPaolo Bonzini         }
39049ab747fSPaolo Bonzini         sdhci_update_irq(s);
39149ab747fSPaolo Bonzini         return;
39249ab747fSPaolo Bonzini     }
39349ab747fSPaolo Bonzini 
39449ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
39549ab747fSPaolo Bonzini         if (s->blkcnt == 0) {
39649ab747fSPaolo Bonzini             return;
39749ab747fSPaolo Bonzini         } else {
39849ab747fSPaolo Bonzini             s->blkcnt--;
39949ab747fSPaolo Bonzini         }
40049ab747fSPaolo Bonzini     }
40149ab747fSPaolo Bonzini 
40249ab747fSPaolo Bonzini     for (index = 0; index < (s->blksize & 0x0fff); index++) {
40349ab747fSPaolo Bonzini         sd_write_data(s->card, s->fifo_buffer[index]);
40449ab747fSPaolo Bonzini     }
40549ab747fSPaolo Bonzini 
40649ab747fSPaolo Bonzini     /* Next data can be written through BUFFER DATORT register */
40749ab747fSPaolo Bonzini     s->prnsts |= SDHC_SPACE_AVAILABLE;
40849ab747fSPaolo Bonzini 
40949ab747fSPaolo Bonzini     /* Finish transfer if that was the last block of data */
41049ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
41149ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) &&
41249ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) {
41349ab747fSPaolo Bonzini         SDHCI_GET_CLASS(s)->end_data_transfer(s);
414dcdb4cd8SPeter Crosthwaite     } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
415dcdb4cd8SPeter Crosthwaite         s->norintsts |= SDHC_NIS_WBUFRDY;
41649ab747fSPaolo Bonzini     }
41749ab747fSPaolo Bonzini 
41849ab747fSPaolo Bonzini     /* Generate Block Gap Event if requested and if not the last block */
41949ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) &&
42049ab747fSPaolo Bonzini             s->blkcnt > 0) {
42149ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DOING_WRITE;
42249ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
42349ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
42449ab747fSPaolo Bonzini         }
42549ab747fSPaolo Bonzini         SDHCI_GET_CLASS(s)->end_data_transfer(s);
42649ab747fSPaolo Bonzini     }
42749ab747fSPaolo Bonzini 
42849ab747fSPaolo Bonzini     sdhci_update_irq(s);
42949ab747fSPaolo Bonzini }
43049ab747fSPaolo Bonzini 
43149ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port
43249ab747fSPaolo Bonzini  * register */
43349ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size)
43449ab747fSPaolo Bonzini {
43549ab747fSPaolo Bonzini     unsigned i;
43649ab747fSPaolo Bonzini 
43749ab747fSPaolo Bonzini     /* Check that there is free space left in a buffer */
43849ab747fSPaolo Bonzini     if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) {
43949ab747fSPaolo Bonzini         ERRPRINT("Can't write to data buffer: buffer full\n");
44049ab747fSPaolo Bonzini         return;
44149ab747fSPaolo Bonzini     }
44249ab747fSPaolo Bonzini 
44349ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
44449ab747fSPaolo Bonzini         s->fifo_buffer[s->data_count] = value & 0xFF;
44549ab747fSPaolo Bonzini         s->data_count++;
44649ab747fSPaolo Bonzini         value >>= 8;
44749ab747fSPaolo Bonzini         if (s->data_count >= (s->blksize & 0x0fff)) {
44849ab747fSPaolo Bonzini             DPRINT_L2("write buffer filled with %u bytes of data\n",
44949ab747fSPaolo Bonzini                     s->data_count);
45049ab747fSPaolo Bonzini             s->data_count = 0;
45149ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_SPACE_AVAILABLE;
45249ab747fSPaolo Bonzini             if (s->prnsts & SDHC_DOING_WRITE) {
45349ab747fSPaolo Bonzini                 SDHCI_GET_CLASS(s)->write_block_to_card(s);
45449ab747fSPaolo Bonzini             }
45549ab747fSPaolo Bonzini         }
45649ab747fSPaolo Bonzini     }
45749ab747fSPaolo Bonzini }
45849ab747fSPaolo Bonzini 
45949ab747fSPaolo Bonzini /*
46049ab747fSPaolo Bonzini  * Single DMA data transfer
46149ab747fSPaolo Bonzini  */
46249ab747fSPaolo Bonzini 
46349ab747fSPaolo Bonzini /* Multi block SDMA transfer */
46449ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
46549ab747fSPaolo Bonzini {
46649ab747fSPaolo Bonzini     bool page_aligned = false;
46749ab747fSPaolo Bonzini     unsigned int n, begin;
46849ab747fSPaolo Bonzini     const uint16_t block_size = s->blksize & 0x0fff;
46949ab747fSPaolo Bonzini     uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12);
47049ab747fSPaolo Bonzini     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
47149ab747fSPaolo Bonzini 
47249ab747fSPaolo Bonzini     /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
47349ab747fSPaolo Bonzini      * possible stop at page boundary if initial address is not page aligned,
47449ab747fSPaolo Bonzini      * allow them to work properly */
47549ab747fSPaolo Bonzini     if ((s->sdmasysad % boundary_chk) == 0) {
47649ab747fSPaolo Bonzini         page_aligned = true;
47749ab747fSPaolo Bonzini     }
47849ab747fSPaolo Bonzini 
47949ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
48049ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
48149ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
48249ab747fSPaolo Bonzini         while (s->blkcnt) {
48349ab747fSPaolo Bonzini             if (s->data_count == 0) {
48449ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
48549ab747fSPaolo Bonzini                     s->fifo_buffer[n] = sd_read_data(s->card);
48649ab747fSPaolo Bonzini                 }
48749ab747fSPaolo Bonzini             }
48849ab747fSPaolo Bonzini             begin = s->data_count;
48949ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
49049ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
49149ab747fSPaolo Bonzini                 boundary_count = 0;
49249ab747fSPaolo Bonzini              } else {
49349ab747fSPaolo Bonzini                 s->data_count = block_size;
49449ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
49549ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
49649ab747fSPaolo Bonzini                     s->blkcnt--;
49749ab747fSPaolo Bonzini                 }
49849ab747fSPaolo Bonzini             }
499df32fd1cSPaolo Bonzini             dma_memory_write(&address_space_memory, s->sdmasysad,
50049ab747fSPaolo Bonzini                              &s->fifo_buffer[begin], s->data_count - begin);
50149ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
50249ab747fSPaolo Bonzini             if (s->data_count == block_size) {
50349ab747fSPaolo Bonzini                 s->data_count = 0;
50449ab747fSPaolo Bonzini             }
50549ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
50649ab747fSPaolo Bonzini                 break;
50749ab747fSPaolo Bonzini             }
50849ab747fSPaolo Bonzini         }
50949ab747fSPaolo Bonzini     } else {
51049ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT |
51149ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
51249ab747fSPaolo Bonzini         while (s->blkcnt) {
51349ab747fSPaolo Bonzini             begin = s->data_count;
51449ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
51549ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
51649ab747fSPaolo Bonzini                 boundary_count = 0;
51749ab747fSPaolo Bonzini              } else {
51849ab747fSPaolo Bonzini                 s->data_count = block_size;
51949ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
52049ab747fSPaolo Bonzini             }
521df32fd1cSPaolo Bonzini             dma_memory_read(&address_space_memory, s->sdmasysad,
52249ab747fSPaolo Bonzini                             &s->fifo_buffer[begin], s->data_count);
52349ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
52449ab747fSPaolo Bonzini             if (s->data_count == block_size) {
52549ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
52649ab747fSPaolo Bonzini                     sd_write_data(s->card, s->fifo_buffer[n]);
52749ab747fSPaolo Bonzini                 }
52849ab747fSPaolo Bonzini                 s->data_count = 0;
52949ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
53049ab747fSPaolo Bonzini                     s->blkcnt--;
53149ab747fSPaolo Bonzini                 }
53249ab747fSPaolo Bonzini             }
53349ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
53449ab747fSPaolo Bonzini                 break;
53549ab747fSPaolo Bonzini             }
53649ab747fSPaolo Bonzini         }
53749ab747fSPaolo Bonzini     }
53849ab747fSPaolo Bonzini 
53949ab747fSPaolo Bonzini     if (s->blkcnt == 0) {
54049ab747fSPaolo Bonzini         SDHCI_GET_CLASS(s)->end_data_transfer(s);
54149ab747fSPaolo Bonzini     } else {
54249ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_DMA) {
54349ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_DMA;
54449ab747fSPaolo Bonzini         }
54549ab747fSPaolo Bonzini         sdhci_update_irq(s);
54649ab747fSPaolo Bonzini     }
54749ab747fSPaolo Bonzini }
54849ab747fSPaolo Bonzini 
54949ab747fSPaolo Bonzini /* single block SDMA transfer */
55049ab747fSPaolo Bonzini 
55149ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s)
55249ab747fSPaolo Bonzini {
55349ab747fSPaolo Bonzini     int n;
55449ab747fSPaolo Bonzini     uint32_t datacnt = s->blksize & 0x0fff;
55549ab747fSPaolo Bonzini 
55649ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
55749ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
55849ab747fSPaolo Bonzini             s->fifo_buffer[n] = sd_read_data(s->card);
55949ab747fSPaolo Bonzini         }
560df32fd1cSPaolo Bonzini         dma_memory_write(&address_space_memory, s->sdmasysad, s->fifo_buffer,
56149ab747fSPaolo Bonzini                          datacnt);
56249ab747fSPaolo Bonzini     } else {
563df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, s->sdmasysad, s->fifo_buffer,
56449ab747fSPaolo Bonzini                         datacnt);
56549ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
56649ab747fSPaolo Bonzini             sd_write_data(s->card, s->fifo_buffer[n]);
56749ab747fSPaolo Bonzini         }
56849ab747fSPaolo Bonzini     }
56949ab747fSPaolo Bonzini 
57049ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
57149ab747fSPaolo Bonzini         s->blkcnt--;
57249ab747fSPaolo Bonzini     }
57349ab747fSPaolo Bonzini 
57449ab747fSPaolo Bonzini     SDHCI_GET_CLASS(s)->end_data_transfer(s);
57549ab747fSPaolo Bonzini }
57649ab747fSPaolo Bonzini 
57749ab747fSPaolo Bonzini typedef struct ADMADescr {
57849ab747fSPaolo Bonzini     hwaddr addr;
57949ab747fSPaolo Bonzini     uint16_t length;
58049ab747fSPaolo Bonzini     uint8_t attr;
58149ab747fSPaolo Bonzini     uint8_t incr;
58249ab747fSPaolo Bonzini } ADMADescr;
58349ab747fSPaolo Bonzini 
58449ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
58549ab747fSPaolo Bonzini {
58649ab747fSPaolo Bonzini     uint32_t adma1 = 0;
58749ab747fSPaolo Bonzini     uint64_t adma2 = 0;
58849ab747fSPaolo Bonzini     hwaddr entry_addr = (hwaddr)s->admasysaddr;
58949ab747fSPaolo Bonzini     switch (SDHC_DMA_TYPE(s->hostctl)) {
59049ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_32:
591df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma2,
59249ab747fSPaolo Bonzini                         sizeof(adma2));
59349ab747fSPaolo Bonzini         adma2 = le64_to_cpu(adma2);
59449ab747fSPaolo Bonzini         /* The spec does not specify endianness of descriptor table.
59549ab747fSPaolo Bonzini          * We currently assume that it is LE.
59649ab747fSPaolo Bonzini          */
59749ab747fSPaolo Bonzini         dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull;
59849ab747fSPaolo Bonzini         dscr->length = (uint16_t)extract64(adma2, 16, 16);
59949ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract64(adma2, 0, 7);
60049ab747fSPaolo Bonzini         dscr->incr = 8;
60149ab747fSPaolo Bonzini         break;
60249ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA1_32:
603df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma1,
60449ab747fSPaolo Bonzini                         sizeof(adma1));
60549ab747fSPaolo Bonzini         adma1 = le32_to_cpu(adma1);
60649ab747fSPaolo Bonzini         dscr->addr = (hwaddr)(adma1 & 0xFFFFF000);
60749ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract32(adma1, 0, 7);
60849ab747fSPaolo Bonzini         dscr->incr = 4;
60949ab747fSPaolo Bonzini         if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
61049ab747fSPaolo Bonzini             dscr->length = (uint16_t)extract32(adma1, 12, 16);
61149ab747fSPaolo Bonzini         } else {
61249ab747fSPaolo Bonzini             dscr->length = 4096;
61349ab747fSPaolo Bonzini         }
61449ab747fSPaolo Bonzini         break;
61549ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_64:
616df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr,
61749ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->attr), 1);
618df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr + 2,
61949ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->length), 2);
62049ab747fSPaolo Bonzini         dscr->length = le16_to_cpu(dscr->length);
621df32fd1cSPaolo Bonzini         dma_memory_read(&address_space_memory, entry_addr + 4,
62249ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->addr), 8);
62349ab747fSPaolo Bonzini         dscr->attr = le64_to_cpu(dscr->attr);
62449ab747fSPaolo Bonzini         dscr->attr &= 0xfffffff8;
62549ab747fSPaolo Bonzini         dscr->incr = 12;
62649ab747fSPaolo Bonzini         break;
62749ab747fSPaolo Bonzini     }
62849ab747fSPaolo Bonzini }
62949ab747fSPaolo Bonzini 
63049ab747fSPaolo Bonzini /* Advanced DMA data transfer */
63149ab747fSPaolo Bonzini 
63249ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s)
63349ab747fSPaolo Bonzini {
63449ab747fSPaolo Bonzini     unsigned int n, begin, length;
63549ab747fSPaolo Bonzini     const uint16_t block_size = s->blksize & 0x0fff;
63649ab747fSPaolo Bonzini     ADMADescr dscr;
63749ab747fSPaolo Bonzini     int i;
63849ab747fSPaolo Bonzini 
63949ab747fSPaolo Bonzini     for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
64049ab747fSPaolo Bonzini         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
64149ab747fSPaolo Bonzini 
64249ab747fSPaolo Bonzini         get_adma_description(s, &dscr);
64349ab747fSPaolo Bonzini         DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx ", len=%d, attr=%x\n",
64449ab747fSPaolo Bonzini                 dscr.addr, dscr.length, dscr.attr);
64549ab747fSPaolo Bonzini 
64649ab747fSPaolo Bonzini         if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
64749ab747fSPaolo Bonzini             /* Indicate that error occurred in ST_FDS state */
64849ab747fSPaolo Bonzini             s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
64949ab747fSPaolo Bonzini             s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
65049ab747fSPaolo Bonzini 
65149ab747fSPaolo Bonzini             /* Generate ADMA error interrupt */
65249ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_ADMAERR) {
65349ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_ADMAERR;
65449ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
65549ab747fSPaolo Bonzini             }
65649ab747fSPaolo Bonzini 
65749ab747fSPaolo Bonzini             sdhci_update_irq(s);
65849ab747fSPaolo Bonzini             return;
65949ab747fSPaolo Bonzini         }
66049ab747fSPaolo Bonzini 
66149ab747fSPaolo Bonzini         length = dscr.length ? dscr.length : 65536;
66249ab747fSPaolo Bonzini 
66349ab747fSPaolo Bonzini         switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) {
66449ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_TRAN:  /* data transfer */
66549ab747fSPaolo Bonzini 
66649ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_READ) {
66749ab747fSPaolo Bonzini                 while (length) {
66849ab747fSPaolo Bonzini                     if (s->data_count == 0) {
66949ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
67049ab747fSPaolo Bonzini                             s->fifo_buffer[n] = sd_read_data(s->card);
67149ab747fSPaolo Bonzini                         }
67249ab747fSPaolo Bonzini                     }
67349ab747fSPaolo Bonzini                     begin = s->data_count;
67449ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
67549ab747fSPaolo Bonzini                         s->data_count = length + begin;
67649ab747fSPaolo Bonzini                         length = 0;
67749ab747fSPaolo Bonzini                      } else {
67849ab747fSPaolo Bonzini                         s->data_count = block_size;
67949ab747fSPaolo Bonzini                         length -= block_size - begin;
68049ab747fSPaolo Bonzini                     }
681df32fd1cSPaolo Bonzini                     dma_memory_write(&address_space_memory, dscr.addr,
68249ab747fSPaolo Bonzini                                      &s->fifo_buffer[begin],
68349ab747fSPaolo Bonzini                                      s->data_count - begin);
68449ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
68549ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
68649ab747fSPaolo Bonzini                         s->data_count = 0;
68749ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
68849ab747fSPaolo Bonzini                             s->blkcnt--;
68949ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
69049ab747fSPaolo Bonzini                                 break;
69149ab747fSPaolo Bonzini                             }
69249ab747fSPaolo Bonzini                         }
69349ab747fSPaolo Bonzini                     }
69449ab747fSPaolo Bonzini                 }
69549ab747fSPaolo Bonzini             } else {
69649ab747fSPaolo Bonzini                 while (length) {
69749ab747fSPaolo Bonzini                     begin = s->data_count;
69849ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
69949ab747fSPaolo Bonzini                         s->data_count = length + begin;
70049ab747fSPaolo Bonzini                         length = 0;
70149ab747fSPaolo Bonzini                      } else {
70249ab747fSPaolo Bonzini                         s->data_count = block_size;
70349ab747fSPaolo Bonzini                         length -= block_size - begin;
70449ab747fSPaolo Bonzini                     }
705df32fd1cSPaolo Bonzini                     dma_memory_read(&address_space_memory, dscr.addr,
70649ab747fSPaolo Bonzini                                     &s->fifo_buffer[begin], s->data_count);
70749ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
70849ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
70949ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
71049ab747fSPaolo Bonzini                             sd_write_data(s->card, s->fifo_buffer[n]);
71149ab747fSPaolo Bonzini                         }
71249ab747fSPaolo Bonzini                         s->data_count = 0;
71349ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
71449ab747fSPaolo Bonzini                             s->blkcnt--;
71549ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
71649ab747fSPaolo Bonzini                                 break;
71749ab747fSPaolo Bonzini                             }
71849ab747fSPaolo Bonzini                         }
71949ab747fSPaolo Bonzini                     }
72049ab747fSPaolo Bonzini                 }
72149ab747fSPaolo Bonzini             }
72249ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
72349ab747fSPaolo Bonzini             break;
72449ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
72549ab747fSPaolo Bonzini             s->admasysaddr = dscr.addr;
72649ab747fSPaolo Bonzini             DPRINT_L1("ADMA link: admasysaddr=0x%lx\n", s->admasysaddr);
72749ab747fSPaolo Bonzini             break;
72849ab747fSPaolo Bonzini         default:
72949ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
73049ab747fSPaolo Bonzini             break;
73149ab747fSPaolo Bonzini         }
73249ab747fSPaolo Bonzini 
7331d32c26fSPeter Crosthwaite         if (dscr.attr & SDHC_ADMA_ATTR_INT) {
7341d32c26fSPeter Crosthwaite             DPRINT_L1("ADMA interrupt: admasysaddr=0x%lx\n", s->admasysaddr);
7351d32c26fSPeter Crosthwaite             if (s->norintstsen & SDHC_NISEN_DMA) {
7361d32c26fSPeter Crosthwaite                 s->norintsts |= SDHC_NIS_DMA;
7371d32c26fSPeter Crosthwaite             }
7381d32c26fSPeter Crosthwaite 
7391d32c26fSPeter Crosthwaite             sdhci_update_irq(s);
7401d32c26fSPeter Crosthwaite         }
7411d32c26fSPeter Crosthwaite 
74249ab747fSPaolo Bonzini         /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
74349ab747fSPaolo Bonzini         if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
74449ab747fSPaolo Bonzini                     (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
74549ab747fSPaolo Bonzini             DPRINT_L2("ADMA transfer completed\n");
74649ab747fSPaolo Bonzini             if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
74749ab747fSPaolo Bonzini                 (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
74849ab747fSPaolo Bonzini                 s->blkcnt != 0)) {
74949ab747fSPaolo Bonzini                 ERRPRINT("SD/MMC host ADMA length mismatch\n");
75049ab747fSPaolo Bonzini                 s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
75149ab747fSPaolo Bonzini                         SDHC_ADMAERR_STATE_ST_TFR;
75249ab747fSPaolo Bonzini                 if (s->errintstsen & SDHC_EISEN_ADMAERR) {
75349ab747fSPaolo Bonzini                     ERRPRINT("Set ADMA error flag\n");
75449ab747fSPaolo Bonzini                     s->errintsts |= SDHC_EIS_ADMAERR;
75549ab747fSPaolo Bonzini                     s->norintsts |= SDHC_NIS_ERR;
75649ab747fSPaolo Bonzini                 }
75749ab747fSPaolo Bonzini 
75849ab747fSPaolo Bonzini                 sdhci_update_irq(s);
75949ab747fSPaolo Bonzini             }
76049ab747fSPaolo Bonzini             SDHCI_GET_CLASS(s)->end_data_transfer(s);
76149ab747fSPaolo Bonzini             return;
76249ab747fSPaolo Bonzini         }
76349ab747fSPaolo Bonzini 
76449ab747fSPaolo Bonzini     }
76549ab747fSPaolo Bonzini 
76649ab747fSPaolo Bonzini     /* we have unfinished business - reschedule to continue ADMA */
767*bc72ad67SAlex Bligh     timer_mod(s->transfer_timer,
768*bc72ad67SAlex Bligh                    qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY);
76949ab747fSPaolo Bonzini }
77049ab747fSPaolo Bonzini 
77149ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */
77249ab747fSPaolo Bonzini 
77349ab747fSPaolo Bonzini static void sdhci_data_transfer(SDHCIState *s)
77449ab747fSPaolo Bonzini {
77549ab747fSPaolo Bonzini     SDHCIClass *k = SDHCI_GET_CLASS(s);
77649ab747fSPaolo Bonzini 
77749ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_DMA) {
77849ab747fSPaolo Bonzini         switch (SDHC_DMA_TYPE(s->hostctl)) {
77949ab747fSPaolo Bonzini         case SDHC_CTRL_SDMA:
78049ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) &&
78149ab747fSPaolo Bonzini                     (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || s->blkcnt == 0)) {
78249ab747fSPaolo Bonzini                 break;
78349ab747fSPaolo Bonzini             }
78449ab747fSPaolo Bonzini 
78549ab747fSPaolo Bonzini             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
78649ab747fSPaolo Bonzini                 k->do_sdma_single(s);
78749ab747fSPaolo Bonzini             } else {
78849ab747fSPaolo Bonzini                 k->do_sdma_multi(s);
78949ab747fSPaolo Bonzini             }
79049ab747fSPaolo Bonzini 
79149ab747fSPaolo Bonzini             break;
79249ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA1_32:
79349ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA1)) {
79449ab747fSPaolo Bonzini                 ERRPRINT("ADMA1 not supported\n");
79549ab747fSPaolo Bonzini                 break;
79649ab747fSPaolo Bonzini             }
79749ab747fSPaolo Bonzini 
79849ab747fSPaolo Bonzini             k->do_adma(s);
79949ab747fSPaolo Bonzini             break;
80049ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_32:
80149ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA2)) {
80249ab747fSPaolo Bonzini                 ERRPRINT("ADMA2 not supported\n");
80349ab747fSPaolo Bonzini                 break;
80449ab747fSPaolo Bonzini             }
80549ab747fSPaolo Bonzini 
80649ab747fSPaolo Bonzini             k->do_adma(s);
80749ab747fSPaolo Bonzini             break;
80849ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_64:
80949ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA2) ||
81049ab747fSPaolo Bonzini                     !(s->capareg & SDHC_64_BIT_BUS_SUPPORT)) {
81149ab747fSPaolo Bonzini                 ERRPRINT("64 bit ADMA not supported\n");
81249ab747fSPaolo Bonzini                 break;
81349ab747fSPaolo Bonzini             }
81449ab747fSPaolo Bonzini 
81549ab747fSPaolo Bonzini             k->do_adma(s);
81649ab747fSPaolo Bonzini             break;
81749ab747fSPaolo Bonzini         default:
81849ab747fSPaolo Bonzini             ERRPRINT("Unsupported DMA type\n");
81949ab747fSPaolo Bonzini             break;
82049ab747fSPaolo Bonzini         }
82149ab747fSPaolo Bonzini     } else {
82249ab747fSPaolo Bonzini         if ((s->trnmod & SDHC_TRNS_READ) && sd_data_ready(s->card)) {
82349ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
82449ab747fSPaolo Bonzini                     SDHC_DAT_LINE_ACTIVE;
82549ab747fSPaolo Bonzini             SDHCI_GET_CLASS(s)->read_block_from_card(s);
82649ab747fSPaolo Bonzini         } else {
82749ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
82849ab747fSPaolo Bonzini                     SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
82949ab747fSPaolo Bonzini             SDHCI_GET_CLASS(s)->write_block_to_card(s);
83049ab747fSPaolo Bonzini         }
83149ab747fSPaolo Bonzini     }
83249ab747fSPaolo Bonzini }
83349ab747fSPaolo Bonzini 
83449ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s)
83549ab747fSPaolo Bonzini {
83649ab747fSPaolo Bonzini     if (!SDHC_CLOCK_IS_ON(s->clkcon) || !(s->pwrcon & SDHC_POWER_ON) ||
83749ab747fSPaolo Bonzini         (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) &&
83849ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_DATA_PRESENT) ||
83949ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY &&
84049ab747fSPaolo Bonzini         !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) {
84149ab747fSPaolo Bonzini         return false;
84249ab747fSPaolo Bonzini     }
84349ab747fSPaolo Bonzini 
84449ab747fSPaolo Bonzini     return true;
84549ab747fSPaolo Bonzini }
84649ab747fSPaolo Bonzini 
84749ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and
84849ab747fSPaolo Bonzini  * continuous manner */
84949ab747fSPaolo Bonzini static inline bool
85049ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
85149ab747fSPaolo Bonzini {
85249ab747fSPaolo Bonzini     if ((s->data_count & 0x3) != byte_num) {
85349ab747fSPaolo Bonzini         ERRPRINT("Non-sequential access to Buffer Data Port register"
85449ab747fSPaolo Bonzini                 "is prohibited\n");
85549ab747fSPaolo Bonzini         return false;
85649ab747fSPaolo Bonzini     }
85749ab747fSPaolo Bonzini     return true;
85849ab747fSPaolo Bonzini }
85949ab747fSPaolo Bonzini 
86049ab747fSPaolo Bonzini static uint32_t sdhci_read(SDHCIState *s, unsigned int offset, unsigned size)
86149ab747fSPaolo Bonzini {
86249ab747fSPaolo Bonzini     uint32_t ret = 0;
86349ab747fSPaolo Bonzini 
86449ab747fSPaolo Bonzini     switch (offset & ~0x3) {
86549ab747fSPaolo Bonzini     case SDHC_SYSAD:
86649ab747fSPaolo Bonzini         ret = s->sdmasysad;
86749ab747fSPaolo Bonzini         break;
86849ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
86949ab747fSPaolo Bonzini         ret = s->blksize | (s->blkcnt << 16);
87049ab747fSPaolo Bonzini         break;
87149ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
87249ab747fSPaolo Bonzini         ret = s->argument;
87349ab747fSPaolo Bonzini         break;
87449ab747fSPaolo Bonzini     case SDHC_TRNMOD:
87549ab747fSPaolo Bonzini         ret = s->trnmod | (s->cmdreg << 16);
87649ab747fSPaolo Bonzini         break;
87749ab747fSPaolo Bonzini     case SDHC_RSPREG0 ... SDHC_RSPREG3:
87849ab747fSPaolo Bonzini         ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2];
87949ab747fSPaolo Bonzini         break;
88049ab747fSPaolo Bonzini     case  SDHC_BDATA:
88149ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
88249ab747fSPaolo Bonzini             ret = SDHCI_GET_CLASS(s)->bdata_read(s, size);
883677ff2aeSPeter Crosthwaite             DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, offset,
884677ff2aeSPeter Crosthwaite                       ret, ret);
88549ab747fSPaolo Bonzini             return ret;
88649ab747fSPaolo Bonzini         }
88749ab747fSPaolo Bonzini         break;
88849ab747fSPaolo Bonzini     case SDHC_PRNSTS:
88949ab747fSPaolo Bonzini         ret = s->prnsts;
89049ab747fSPaolo Bonzini         break;
89149ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
89249ab747fSPaolo Bonzini         ret = s->hostctl | (s->pwrcon << 8) | (s->blkgap << 16) |
89349ab747fSPaolo Bonzini               (s->wakcon << 24);
89449ab747fSPaolo Bonzini         break;
89549ab747fSPaolo Bonzini     case SDHC_CLKCON:
89649ab747fSPaolo Bonzini         ret = s->clkcon | (s->timeoutcon << 16);
89749ab747fSPaolo Bonzini         break;
89849ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
89949ab747fSPaolo Bonzini         ret = s->norintsts | (s->errintsts << 16);
90049ab747fSPaolo Bonzini         break;
90149ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
90249ab747fSPaolo Bonzini         ret = s->norintstsen | (s->errintstsen << 16);
90349ab747fSPaolo Bonzini         break;
90449ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
90549ab747fSPaolo Bonzini         ret = s->norintsigen | (s->errintsigen << 16);
90649ab747fSPaolo Bonzini         break;
90749ab747fSPaolo Bonzini     case SDHC_ACMD12ERRSTS:
90849ab747fSPaolo Bonzini         ret = s->acmd12errsts;
90949ab747fSPaolo Bonzini         break;
91049ab747fSPaolo Bonzini     case SDHC_CAPAREG:
91149ab747fSPaolo Bonzini         ret = s->capareg;
91249ab747fSPaolo Bonzini         break;
91349ab747fSPaolo Bonzini     case SDHC_MAXCURR:
91449ab747fSPaolo Bonzini         ret = s->maxcurr;
91549ab747fSPaolo Bonzini         break;
91649ab747fSPaolo Bonzini     case SDHC_ADMAERR:
91749ab747fSPaolo Bonzini         ret =  s->admaerr;
91849ab747fSPaolo Bonzini         break;
91949ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
92049ab747fSPaolo Bonzini         ret = (uint32_t)s->admasysaddr;
92149ab747fSPaolo Bonzini         break;
92249ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
92349ab747fSPaolo Bonzini         ret = (uint32_t)(s->admasysaddr >> 32);
92449ab747fSPaolo Bonzini         break;
92549ab747fSPaolo Bonzini     case SDHC_SLOT_INT_STATUS:
92649ab747fSPaolo Bonzini         ret = (SD_HOST_SPECv2_VERS << 16) | sdhci_slotint(s);
92749ab747fSPaolo Bonzini         break;
92849ab747fSPaolo Bonzini     default:
92949ab747fSPaolo Bonzini         ERRPRINT("bad %ub read: addr[0x%04x]\n", size, offset);
93049ab747fSPaolo Bonzini         break;
93149ab747fSPaolo Bonzini     }
93249ab747fSPaolo Bonzini 
93349ab747fSPaolo Bonzini     ret >>= (offset & 0x3) * 8;
93449ab747fSPaolo Bonzini     ret &= (1ULL << (size * 8)) - 1;
93549ab747fSPaolo Bonzini     DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, offset, ret, ret);
93649ab747fSPaolo Bonzini     return ret;
93749ab747fSPaolo Bonzini }
93849ab747fSPaolo Bonzini 
93949ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value)
94049ab747fSPaolo Bonzini {
94149ab747fSPaolo Bonzini     if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) {
94249ab747fSPaolo Bonzini         return;
94349ab747fSPaolo Bonzini     }
94449ab747fSPaolo Bonzini     s->blkgap = value & SDHC_STOP_AT_GAP_REQ;
94549ab747fSPaolo Bonzini 
94649ab747fSPaolo Bonzini     if ((value & SDHC_CONTINUE_REQ) && s->stopped_state &&
94749ab747fSPaolo Bonzini             (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) {
94849ab747fSPaolo Bonzini         if (s->stopped_state == sdhc_gap_read) {
94949ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ;
95049ab747fSPaolo Bonzini             SDHCI_GET_CLASS(s)->read_block_from_card(s);
95149ab747fSPaolo Bonzini         } else {
95249ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE;
95349ab747fSPaolo Bonzini             SDHCI_GET_CLASS(s)->write_block_to_card(s);
95449ab747fSPaolo Bonzini         }
95549ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
95649ab747fSPaolo Bonzini     } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) {
95749ab747fSPaolo Bonzini         if (s->prnsts & SDHC_DOING_READ) {
95849ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_read;
95949ab747fSPaolo Bonzini         } else if (s->prnsts & SDHC_DOING_WRITE) {
96049ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_write;
96149ab747fSPaolo Bonzini         }
96249ab747fSPaolo Bonzini     }
96349ab747fSPaolo Bonzini }
96449ab747fSPaolo Bonzini 
96549ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value)
96649ab747fSPaolo Bonzini {
96749ab747fSPaolo Bonzini     switch (value) {
96849ab747fSPaolo Bonzini     case SDHC_RESET_ALL:
96949ab747fSPaolo Bonzini         DEVICE_GET_CLASS(s)->reset(DEVICE(s));
97049ab747fSPaolo Bonzini         break;
97149ab747fSPaolo Bonzini     case SDHC_RESET_CMD:
97249ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_CMD_INHIBIT;
97349ab747fSPaolo Bonzini         s->norintsts &= ~SDHC_NIS_CMDCMP;
97449ab747fSPaolo Bonzini         break;
97549ab747fSPaolo Bonzini     case SDHC_RESET_DATA:
97649ab747fSPaolo Bonzini         s->data_count = 0;
97749ab747fSPaolo Bonzini         s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE |
97849ab747fSPaolo Bonzini                 SDHC_DOING_READ | SDHC_DOING_WRITE |
97949ab747fSPaolo Bonzini                 SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE);
98049ab747fSPaolo Bonzini         s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ);
98149ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
98249ab747fSPaolo Bonzini         s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY |
98349ab747fSPaolo Bonzini                 SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP);
98449ab747fSPaolo Bonzini         break;
98549ab747fSPaolo Bonzini     }
98649ab747fSPaolo Bonzini }
98749ab747fSPaolo Bonzini 
98849ab747fSPaolo Bonzini static void
98949ab747fSPaolo Bonzini sdhci_write(SDHCIState *s, unsigned int offset, uint32_t value, unsigned size)
99049ab747fSPaolo Bonzini {
99149ab747fSPaolo Bonzini     unsigned shift =  8 * (offset & 0x3);
99249ab747fSPaolo Bonzini     uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift);
99349ab747fSPaolo Bonzini     value <<= shift;
99449ab747fSPaolo Bonzini 
99549ab747fSPaolo Bonzini     switch (offset & ~0x3) {
99649ab747fSPaolo Bonzini     case SDHC_SYSAD:
99749ab747fSPaolo Bonzini         s->sdmasysad = (s->sdmasysad & mask) | value;
99849ab747fSPaolo Bonzini         MASKED_WRITE(s->sdmasysad, mask, value);
99949ab747fSPaolo Bonzini         /* Writing to last byte of sdmasysad might trigger transfer */
100049ab747fSPaolo Bonzini         if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt &&
100149ab747fSPaolo Bonzini                 s->blksize && SDHC_DMA_TYPE(s->hostctl) == SDHC_CTRL_SDMA) {
100249ab747fSPaolo Bonzini             SDHCI_GET_CLASS(s)->do_sdma_multi(s);
100349ab747fSPaolo Bonzini         }
100449ab747fSPaolo Bonzini         break;
100549ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
100649ab747fSPaolo Bonzini         if (!TRANSFERRING_DATA(s->prnsts)) {
100749ab747fSPaolo Bonzini             MASKED_WRITE(s->blksize, mask, value);
100849ab747fSPaolo Bonzini             MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16);
100949ab747fSPaolo Bonzini         }
101049ab747fSPaolo Bonzini         break;
101149ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
101249ab747fSPaolo Bonzini         MASKED_WRITE(s->argument, mask, value);
101349ab747fSPaolo Bonzini         break;
101449ab747fSPaolo Bonzini     case SDHC_TRNMOD:
101549ab747fSPaolo Bonzini         /* DMA can be enabled only if it is supported as indicated by
101649ab747fSPaolo Bonzini          * capabilities register */
101749ab747fSPaolo Bonzini         if (!(s->capareg & SDHC_CAN_DO_DMA)) {
101849ab747fSPaolo Bonzini             value &= ~SDHC_TRNS_DMA;
101949ab747fSPaolo Bonzini         }
102049ab747fSPaolo Bonzini         MASKED_WRITE(s->trnmod, mask, value);
102149ab747fSPaolo Bonzini         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
102249ab747fSPaolo Bonzini 
102349ab747fSPaolo Bonzini         /* Writing to the upper byte of CMDREG triggers SD command generation */
102449ab747fSPaolo Bonzini         if ((mask & 0xFF000000) || !SDHCI_GET_CLASS(s)->can_issue_command(s)) {
102549ab747fSPaolo Bonzini             break;
102649ab747fSPaolo Bonzini         }
102749ab747fSPaolo Bonzini 
102849ab747fSPaolo Bonzini         SDHCI_GET_CLASS(s)->send_command(s);
102949ab747fSPaolo Bonzini         break;
103049ab747fSPaolo Bonzini     case  SDHC_BDATA:
103149ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
103249ab747fSPaolo Bonzini             SDHCI_GET_CLASS(s)->bdata_write(s, value >> shift, size);
103349ab747fSPaolo Bonzini         }
103449ab747fSPaolo Bonzini         break;
103549ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
103649ab747fSPaolo Bonzini         if (!(mask & 0xFF0000)) {
103749ab747fSPaolo Bonzini             sdhci_blkgap_write(s, value >> 16);
103849ab747fSPaolo Bonzini         }
103949ab747fSPaolo Bonzini         MASKED_WRITE(s->hostctl, mask, value);
104049ab747fSPaolo Bonzini         MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8);
104149ab747fSPaolo Bonzini         MASKED_WRITE(s->wakcon, mask >> 24, value >> 24);
104249ab747fSPaolo Bonzini         if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 ||
104349ab747fSPaolo Bonzini                 !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) {
104449ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
104549ab747fSPaolo Bonzini         }
104649ab747fSPaolo Bonzini         break;
104749ab747fSPaolo Bonzini     case SDHC_CLKCON:
104849ab747fSPaolo Bonzini         if (!(mask & 0xFF000000)) {
104949ab747fSPaolo Bonzini             sdhci_reset_write(s, value >> 24);
105049ab747fSPaolo Bonzini         }
105149ab747fSPaolo Bonzini         MASKED_WRITE(s->clkcon, mask, value);
105249ab747fSPaolo Bonzini         MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16);
105349ab747fSPaolo Bonzini         if (s->clkcon & SDHC_CLOCK_INT_EN) {
105449ab747fSPaolo Bonzini             s->clkcon |= SDHC_CLOCK_INT_STABLE;
105549ab747fSPaolo Bonzini         } else {
105649ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_INT_STABLE;
105749ab747fSPaolo Bonzini         }
105849ab747fSPaolo Bonzini         break;
105949ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
106049ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_CARDINT) {
106149ab747fSPaolo Bonzini             value &= ~SDHC_NIS_CARDINT;
106249ab747fSPaolo Bonzini         }
106349ab747fSPaolo Bonzini         s->norintsts &= mask | ~value;
106449ab747fSPaolo Bonzini         s->errintsts &= (mask >> 16) | ~(value >> 16);
106549ab747fSPaolo Bonzini         if (s->errintsts) {
106649ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
106749ab747fSPaolo Bonzini         } else {
106849ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
106949ab747fSPaolo Bonzini         }
107049ab747fSPaolo Bonzini         sdhci_update_irq(s);
107149ab747fSPaolo Bonzini         break;
107249ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
107349ab747fSPaolo Bonzini         MASKED_WRITE(s->norintstsen, mask, value);
107449ab747fSPaolo Bonzini         MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16);
107549ab747fSPaolo Bonzini         s->norintsts &= s->norintstsen;
107649ab747fSPaolo Bonzini         s->errintsts &= s->errintstsen;
107749ab747fSPaolo Bonzini         if (s->errintsts) {
107849ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
107949ab747fSPaolo Bonzini         } else {
108049ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
108149ab747fSPaolo Bonzini         }
108249ab747fSPaolo Bonzini         sdhci_update_irq(s);
108349ab747fSPaolo Bonzini         break;
108449ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
108549ab747fSPaolo Bonzini         MASKED_WRITE(s->norintsigen, mask, value);
108649ab747fSPaolo Bonzini         MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16);
108749ab747fSPaolo Bonzini         sdhci_update_irq(s);
108849ab747fSPaolo Bonzini         break;
108949ab747fSPaolo Bonzini     case SDHC_ADMAERR:
109049ab747fSPaolo Bonzini         MASKED_WRITE(s->admaerr, mask, value);
109149ab747fSPaolo Bonzini         break;
109249ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
109349ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL |
109449ab747fSPaolo Bonzini                 (uint64_t)mask)) | (uint64_t)value;
109549ab747fSPaolo Bonzini         break;
109649ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
109749ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL |
109849ab747fSPaolo Bonzini                 ((uint64_t)mask << 32))) | ((uint64_t)value << 32);
109949ab747fSPaolo Bonzini         break;
110049ab747fSPaolo Bonzini     case SDHC_FEAER:
110149ab747fSPaolo Bonzini         s->acmd12errsts |= value;
110249ab747fSPaolo Bonzini         s->errintsts |= (value >> 16) & s->errintstsen;
110349ab747fSPaolo Bonzini         if (s->acmd12errsts) {
110449ab747fSPaolo Bonzini             s->errintsts |= SDHC_EIS_CMD12ERR;
110549ab747fSPaolo Bonzini         }
110649ab747fSPaolo Bonzini         if (s->errintsts) {
110749ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
110849ab747fSPaolo Bonzini         }
110949ab747fSPaolo Bonzini         sdhci_update_irq(s);
111049ab747fSPaolo Bonzini         break;
111149ab747fSPaolo Bonzini     default:
111249ab747fSPaolo Bonzini         ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n",
111349ab747fSPaolo Bonzini                 size, offset, value >> shift, value >> shift);
111449ab747fSPaolo Bonzini         break;
111549ab747fSPaolo Bonzini     }
111649ab747fSPaolo Bonzini     DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n",
111749ab747fSPaolo Bonzini             size, offset, value >> shift, value >> shift);
111849ab747fSPaolo Bonzini }
111949ab747fSPaolo Bonzini 
112049ab747fSPaolo Bonzini static uint64_t
112149ab747fSPaolo Bonzini sdhci_readfn(void *opaque, hwaddr offset, unsigned size)
112249ab747fSPaolo Bonzini {
112349ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
112449ab747fSPaolo Bonzini 
112549ab747fSPaolo Bonzini     return SDHCI_GET_CLASS(s)->mem_read(s, offset, size);
112649ab747fSPaolo Bonzini }
112749ab747fSPaolo Bonzini 
112849ab747fSPaolo Bonzini static void
112949ab747fSPaolo Bonzini sdhci_writefn(void *opaque, hwaddr off, uint64_t val, unsigned sz)
113049ab747fSPaolo Bonzini {
113149ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
113249ab747fSPaolo Bonzini 
113349ab747fSPaolo Bonzini     SDHCI_GET_CLASS(s)->mem_write(s, off, val, sz);
113449ab747fSPaolo Bonzini }
113549ab747fSPaolo Bonzini 
113649ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = {
113749ab747fSPaolo Bonzini     .read = sdhci_readfn,
113849ab747fSPaolo Bonzini     .write = sdhci_writefn,
113949ab747fSPaolo Bonzini     .valid = {
114049ab747fSPaolo Bonzini         .min_access_size = 1,
114149ab747fSPaolo Bonzini         .max_access_size = 4,
114249ab747fSPaolo Bonzini         .unaligned = false
114349ab747fSPaolo Bonzini     },
114449ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
114549ab747fSPaolo Bonzini };
114649ab747fSPaolo Bonzini 
114749ab747fSPaolo Bonzini static inline unsigned int sdhci_get_fifolen(SDHCIState *s)
114849ab747fSPaolo Bonzini {
114949ab747fSPaolo Bonzini     switch (SDHC_CAPAB_BLOCKSIZE(s->capareg)) {
115049ab747fSPaolo Bonzini     case 0:
115149ab747fSPaolo Bonzini         return 512;
115249ab747fSPaolo Bonzini     case 1:
115349ab747fSPaolo Bonzini         return 1024;
115449ab747fSPaolo Bonzini     case 2:
115549ab747fSPaolo Bonzini         return 2048;
115649ab747fSPaolo Bonzini     default:
115749ab747fSPaolo Bonzini         hw_error("SDHC: unsupported value for maximum block size\n");
115849ab747fSPaolo Bonzini         return 0;
115949ab747fSPaolo Bonzini     }
116049ab747fSPaolo Bonzini }
116149ab747fSPaolo Bonzini 
116249ab747fSPaolo Bonzini static void sdhci_initfn(Object *obj)
116349ab747fSPaolo Bonzini {
116449ab747fSPaolo Bonzini     SDHCIState *s = SDHCI(obj);
116549ab747fSPaolo Bonzini     DriveInfo *di;
116649ab747fSPaolo Bonzini 
116749ab747fSPaolo Bonzini     di = drive_get_next(IF_SD);
11686790f59dSliguang     s->card = sd_init(di ? di->bdrv : NULL, false);
116949ab747fSPaolo Bonzini     s->eject_cb = qemu_allocate_irqs(sdhci_insert_eject_cb, s, 1)[0];
117049ab747fSPaolo Bonzini     s->ro_cb = qemu_allocate_irqs(sdhci_card_readonly_cb, s, 1)[0];
117149ab747fSPaolo Bonzini     sd_set_cb(s->card, s->ro_cb, s->eject_cb);
117249ab747fSPaolo Bonzini 
1173*bc72ad67SAlex Bligh     s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s);
1174*bc72ad67SAlex Bligh     s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_do_data_transfer, s);
117549ab747fSPaolo Bonzini }
117649ab747fSPaolo Bonzini 
117749ab747fSPaolo Bonzini static void sdhci_uninitfn(Object *obj)
117849ab747fSPaolo Bonzini {
117949ab747fSPaolo Bonzini     SDHCIState *s = SDHCI(obj);
118049ab747fSPaolo Bonzini 
1181*bc72ad67SAlex Bligh     timer_del(s->insert_timer);
1182*bc72ad67SAlex Bligh     timer_free(s->insert_timer);
1183*bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
1184*bc72ad67SAlex Bligh     timer_free(s->transfer_timer);
118549ab747fSPaolo Bonzini     qemu_free_irqs(&s->eject_cb);
118649ab747fSPaolo Bonzini     qemu_free_irqs(&s->ro_cb);
118749ab747fSPaolo Bonzini 
118849ab747fSPaolo Bonzini     if (s->fifo_buffer) {
118949ab747fSPaolo Bonzini         g_free(s->fifo_buffer);
119049ab747fSPaolo Bonzini         s->fifo_buffer = NULL;
119149ab747fSPaolo Bonzini     }
119249ab747fSPaolo Bonzini }
119349ab747fSPaolo Bonzini 
119449ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = {
119549ab747fSPaolo Bonzini     .name = "sdhci",
119649ab747fSPaolo Bonzini     .version_id = 1,
119749ab747fSPaolo Bonzini     .minimum_version_id = 1,
119849ab747fSPaolo Bonzini     .fields      = (VMStateField[]) {
119949ab747fSPaolo Bonzini         VMSTATE_UINT32(sdmasysad, SDHCIState),
120049ab747fSPaolo Bonzini         VMSTATE_UINT16(blksize, SDHCIState),
120149ab747fSPaolo Bonzini         VMSTATE_UINT16(blkcnt, SDHCIState),
120249ab747fSPaolo Bonzini         VMSTATE_UINT32(argument, SDHCIState),
120349ab747fSPaolo Bonzini         VMSTATE_UINT16(trnmod, SDHCIState),
120449ab747fSPaolo Bonzini         VMSTATE_UINT16(cmdreg, SDHCIState),
120549ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4),
120649ab747fSPaolo Bonzini         VMSTATE_UINT32(prnsts, SDHCIState),
120749ab747fSPaolo Bonzini         VMSTATE_UINT8(hostctl, SDHCIState),
120849ab747fSPaolo Bonzini         VMSTATE_UINT8(pwrcon, SDHCIState),
120949ab747fSPaolo Bonzini         VMSTATE_UINT8(blkgap, SDHCIState),
121049ab747fSPaolo Bonzini         VMSTATE_UINT8(wakcon, SDHCIState),
121149ab747fSPaolo Bonzini         VMSTATE_UINT16(clkcon, SDHCIState),
121249ab747fSPaolo Bonzini         VMSTATE_UINT8(timeoutcon, SDHCIState),
121349ab747fSPaolo Bonzini         VMSTATE_UINT8(admaerr, SDHCIState),
121449ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsts, SDHCIState),
121549ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsts, SDHCIState),
121649ab747fSPaolo Bonzini         VMSTATE_UINT16(norintstsen, SDHCIState),
121749ab747fSPaolo Bonzini         VMSTATE_UINT16(errintstsen, SDHCIState),
121849ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsigen, SDHCIState),
121949ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsigen, SDHCIState),
122049ab747fSPaolo Bonzini         VMSTATE_UINT16(acmd12errsts, SDHCIState),
122149ab747fSPaolo Bonzini         VMSTATE_UINT16(data_count, SDHCIState),
122249ab747fSPaolo Bonzini         VMSTATE_UINT64(admasysaddr, SDHCIState),
122349ab747fSPaolo Bonzini         VMSTATE_UINT8(stopped_state, SDHCIState),
122449ab747fSPaolo Bonzini         VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, 0, buf_maxsz),
122549ab747fSPaolo Bonzini         VMSTATE_TIMER(insert_timer, SDHCIState),
122649ab747fSPaolo Bonzini         VMSTATE_TIMER(transfer_timer, SDHCIState),
122749ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
122849ab747fSPaolo Bonzini     }
122949ab747fSPaolo Bonzini };
123049ab747fSPaolo Bonzini 
123149ab747fSPaolo Bonzini /* Capabilities registers provide information on supported features of this
123249ab747fSPaolo Bonzini  * specific host controller implementation */
123349ab747fSPaolo Bonzini static Property sdhci_properties[] = {
123449ab747fSPaolo Bonzini     DEFINE_PROP_HEX32("capareg", SDHCIState, capareg,
123549ab747fSPaolo Bonzini             SDHC_CAPAB_REG_DEFAULT),
123649ab747fSPaolo Bonzini     DEFINE_PROP_HEX32("maxcurr", SDHCIState, maxcurr, 0),
123749ab747fSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
123849ab747fSPaolo Bonzini };
123949ab747fSPaolo Bonzini 
124049ab747fSPaolo Bonzini static void sdhci_realize(DeviceState *dev, Error ** errp)
124149ab747fSPaolo Bonzini {
124249ab747fSPaolo Bonzini     SDHCIState *s = SDHCI(dev);
124349ab747fSPaolo Bonzini     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
124449ab747fSPaolo Bonzini 
124549ab747fSPaolo Bonzini     s->buf_maxsz = sdhci_get_fifolen(s);
124649ab747fSPaolo Bonzini     s->fifo_buffer = g_malloc0(s->buf_maxsz);
124749ab747fSPaolo Bonzini     sysbus_init_irq(sbd, &s->irq);
124829776739SPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci",
124949ab747fSPaolo Bonzini             SDHC_REGISTERS_MAP_SIZE);
125049ab747fSPaolo Bonzini     sysbus_init_mmio(sbd, &s->iomem);
125149ab747fSPaolo Bonzini }
125249ab747fSPaolo Bonzini 
125349ab747fSPaolo Bonzini static void sdhci_generic_reset(DeviceState *ds)
125449ab747fSPaolo Bonzini {
125549ab747fSPaolo Bonzini     SDHCIState *s = SDHCI(ds);
125649ab747fSPaolo Bonzini     SDHCI_GET_CLASS(s)->reset(s);
125749ab747fSPaolo Bonzini }
125849ab747fSPaolo Bonzini 
125949ab747fSPaolo Bonzini static void sdhci_class_init(ObjectClass *klass, void *data)
126049ab747fSPaolo Bonzini {
126149ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
126249ab747fSPaolo Bonzini     SDHCIClass *k = SDHCI_CLASS(klass);
126349ab747fSPaolo Bonzini 
126449ab747fSPaolo Bonzini     dc->vmsd = &sdhci_vmstate;
126549ab747fSPaolo Bonzini     dc->props = sdhci_properties;
126649ab747fSPaolo Bonzini     dc->reset = sdhci_generic_reset;
126749ab747fSPaolo Bonzini     dc->realize = sdhci_realize;
126849ab747fSPaolo Bonzini 
126949ab747fSPaolo Bonzini     k->reset = sdhci_reset;
127049ab747fSPaolo Bonzini     k->mem_read = sdhci_read;
127149ab747fSPaolo Bonzini     k->mem_write = sdhci_write;
127249ab747fSPaolo Bonzini     k->send_command = sdhci_send_command;
127349ab747fSPaolo Bonzini     k->can_issue_command = sdhci_can_issue_command;
127449ab747fSPaolo Bonzini     k->data_transfer = sdhci_data_transfer;
127549ab747fSPaolo Bonzini     k->end_data_transfer = sdhci_end_transfer;
127649ab747fSPaolo Bonzini     k->do_sdma_single = sdhci_sdma_transfer_single_block;
127749ab747fSPaolo Bonzini     k->do_sdma_multi = sdhci_sdma_transfer_multi_blocks;
127849ab747fSPaolo Bonzini     k->do_adma = sdhci_do_adma;
127949ab747fSPaolo Bonzini     k->read_block_from_card = sdhci_read_block_from_card;
128049ab747fSPaolo Bonzini     k->write_block_to_card = sdhci_write_block_to_card;
128149ab747fSPaolo Bonzini     k->bdata_read = sdhci_read_dataport;
128249ab747fSPaolo Bonzini     k->bdata_write = sdhci_write_dataport;
128349ab747fSPaolo Bonzini }
128449ab747fSPaolo Bonzini 
128549ab747fSPaolo Bonzini static const TypeInfo sdhci_type_info = {
128649ab747fSPaolo Bonzini     .name = TYPE_SDHCI,
128749ab747fSPaolo Bonzini     .parent = TYPE_SYS_BUS_DEVICE,
128849ab747fSPaolo Bonzini     .instance_size = sizeof(SDHCIState),
128949ab747fSPaolo Bonzini     .instance_init = sdhci_initfn,
129049ab747fSPaolo Bonzini     .instance_finalize = sdhci_uninitfn,
129149ab747fSPaolo Bonzini     .class_init = sdhci_class_init,
129249ab747fSPaolo Bonzini     .class_size = sizeof(SDHCIClass)
129349ab747fSPaolo Bonzini };
129449ab747fSPaolo Bonzini 
129549ab747fSPaolo Bonzini static void sdhci_register_types(void)
129649ab747fSPaolo Bonzini {
129749ab747fSPaolo Bonzini     type_register_static(&sdhci_type_info);
129849ab747fSPaolo Bonzini }
129949ab747fSPaolo Bonzini 
130049ab747fSPaolo Bonzini type_init(sdhci_register_types)
1301