xref: /openbmc/qemu/hw/sd/sdhci.c (revision aa164fbff3fef9e2815d5782dd063a3d32153af6)
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"
26b635d98cSPhilippe Mathieu-Daudé #include "qapi/error.h"
2749ab747fSPaolo Bonzini #include "hw/hw.h"
28fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
2949ab747fSPaolo Bonzini #include "sysemu/blockdev.h"
3049ab747fSPaolo Bonzini #include "sysemu/dma.h"
3149ab747fSPaolo Bonzini #include "qemu/timer.h"
3249ab747fSPaolo Bonzini #include "qemu/bitops.h"
33f82a0f44SPhilippe Mathieu-Daudé #include "hw/sd/sdhci.h"
34637d23beSSai Pavan Boddu #include "sdhci-internal.h"
3503dd024fSPaolo Bonzini #include "qemu/log.h"
368be487d8SPhilippe Mathieu-Daudé #include "trace.h"
3749ab747fSPaolo Bonzini 
3840bbc194SPeter Maydell #define TYPE_SDHCI_BUS "sdhci-bus"
3940bbc194SPeter Maydell #define SDHCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SDHCI_BUS)
4040bbc194SPeter Maydell 
41*aa164fbfSPhilippe Mathieu-Daudé #define MASKED_WRITE(reg, mask, val)  (reg = (reg & (mask)) | (val))
42*aa164fbfSPhilippe Mathieu-Daudé 
4349ab747fSPaolo Bonzini /* Default SD/MMC host controller features information, which will be
4449ab747fSPaolo Bonzini  * presented in CAPABILITIES register of generic SD host controller at reset.
45*aa164fbfSPhilippe Mathieu-Daudé  *
46*aa164fbfSPhilippe Mathieu-Daudé  * support:
47*aa164fbfSPhilippe Mathieu-Daudé  * - 3.3v and 1.8v voltages
48*aa164fbfSPhilippe Mathieu-Daudé  * - SDMA/ADMA1/ADMA2
49*aa164fbfSPhilippe Mathieu-Daudé  * - high-speed
50*aa164fbfSPhilippe Mathieu-Daudé  * max host controller R/W buffers size: 512B
51*aa164fbfSPhilippe Mathieu-Daudé  * max clock frequency for SDclock: 52 MHz
52*aa164fbfSPhilippe Mathieu-Daudé  * timeout clock frequency: 52 MHz
53*aa164fbfSPhilippe Mathieu-Daudé  *
54*aa164fbfSPhilippe Mathieu-Daudé  * does not support:
55*aa164fbfSPhilippe Mathieu-Daudé  * - 3.0v voltage
56*aa164fbfSPhilippe Mathieu-Daudé  * - 64-bit system bus
57*aa164fbfSPhilippe Mathieu-Daudé  * - suspend/resume
5849ab747fSPaolo Bonzini  */
59*aa164fbfSPhilippe Mathieu-Daudé #define SDHC_CAPAB_REG_DEFAULT 0x057834b4
6049ab747fSPaolo Bonzini 
6149ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s)
6249ab747fSPaolo Bonzini {
6349ab747fSPaolo Bonzini     return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) ||
6449ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) ||
6549ab747fSPaolo Bonzini          ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV));
6649ab747fSPaolo Bonzini }
6749ab747fSPaolo Bonzini 
6849ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s)
6949ab747fSPaolo Bonzini {
7049ab747fSPaolo Bonzini     qemu_set_irq(s->irq, sdhci_slotint(s));
7149ab747fSPaolo Bonzini }
7249ab747fSPaolo Bonzini 
7349ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque)
7449ab747fSPaolo Bonzini {
7549ab747fSPaolo Bonzini     SDHCIState *s = (SDHCIState *)opaque;
7649ab747fSPaolo Bonzini 
7749ab747fSPaolo Bonzini     if (s->norintsts & SDHC_NIS_REMOVE) {
78bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
79bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
8049ab747fSPaolo Bonzini     } else {
8149ab747fSPaolo Bonzini         s->prnsts = 0x1ff0000;
8249ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_INSERT) {
8349ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_INSERT;
8449ab747fSPaolo Bonzini         }
8549ab747fSPaolo Bonzini         sdhci_update_irq(s);
8649ab747fSPaolo Bonzini     }
8749ab747fSPaolo Bonzini }
8849ab747fSPaolo Bonzini 
8940bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level)
9049ab747fSPaolo Bonzini {
9140bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
9249ab747fSPaolo Bonzini 
938be487d8SPhilippe Mathieu-Daudé     trace_sdhci_set_inserted(level ? "insert" : "eject");
9449ab747fSPaolo Bonzini     if ((s->norintsts & SDHC_NIS_REMOVE) && level) {
9549ab747fSPaolo Bonzini         /* Give target some time to notice card ejection */
96bc72ad67SAlex Bligh         timer_mod(s->insert_timer,
97bc72ad67SAlex Bligh                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY);
9849ab747fSPaolo Bonzini     } else {
9949ab747fSPaolo Bonzini         if (level) {
10049ab747fSPaolo Bonzini             s->prnsts = 0x1ff0000;
10149ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_INSERT) {
10249ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_INSERT;
10349ab747fSPaolo Bonzini             }
10449ab747fSPaolo Bonzini         } else {
10549ab747fSPaolo Bonzini             s->prnsts = 0x1fa0000;
10649ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
10749ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_SDCLK_EN;
10849ab747fSPaolo Bonzini             if (s->norintstsen & SDHC_NISEN_REMOVE) {
10949ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_REMOVE;
11049ab747fSPaolo Bonzini             }
11149ab747fSPaolo Bonzini         }
11249ab747fSPaolo Bonzini         sdhci_update_irq(s);
11349ab747fSPaolo Bonzini     }
11449ab747fSPaolo Bonzini }
11549ab747fSPaolo Bonzini 
11640bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level)
11749ab747fSPaolo Bonzini {
11840bbc194SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
11949ab747fSPaolo Bonzini 
12049ab747fSPaolo Bonzini     if (level) {
12149ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_WRITE_PROTECT;
12249ab747fSPaolo Bonzini     } else {
12349ab747fSPaolo Bonzini         /* Write enabled */
12449ab747fSPaolo Bonzini         s->prnsts |= SDHC_WRITE_PROTECT;
12549ab747fSPaolo Bonzini     }
12649ab747fSPaolo Bonzini }
12749ab747fSPaolo Bonzini 
12849ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s)
12949ab747fSPaolo Bonzini {
13040bbc194SPeter Maydell     DeviceState *dev = DEVICE(s);
13140bbc194SPeter Maydell 
132bc72ad67SAlex Bligh     timer_del(s->insert_timer);
133bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
134aceb5b06SPhilippe Mathieu-Daudé 
135aceb5b06SPhilippe Mathieu-Daudé     /* Set all registers to 0. Capabilities/Version registers are not cleared
13649ab747fSPaolo Bonzini      * and assumed to always preserve their value, given to them during
13749ab747fSPaolo Bonzini      * initialization */
13849ab747fSPaolo Bonzini     memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad);
13949ab747fSPaolo Bonzini 
14040bbc194SPeter Maydell     /* Reset other state based on current card insertion/readonly status */
14140bbc194SPeter Maydell     sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus));
14240bbc194SPeter Maydell     sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus));
14340bbc194SPeter Maydell 
14449ab747fSPaolo Bonzini     s->data_count = 0;
14549ab747fSPaolo Bonzini     s->stopped_state = sdhc_not_stopped;
1460a7ac9f9SAndrew Baumann     s->pending_insert_state = false;
14749ab747fSPaolo Bonzini }
14849ab747fSPaolo Bonzini 
1498b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev)
1508b41c305SPeter Maydell {
1518b41c305SPeter Maydell     /* QOM (ie power-on) reset. This is identical to reset
1528b41c305SPeter Maydell      * commanded via device register apart from handling of the
1538b41c305SPeter Maydell      * 'pending insert on powerup' quirk.
1548b41c305SPeter Maydell      */
1558b41c305SPeter Maydell     SDHCIState *s = (SDHCIState *)dev;
1568b41c305SPeter Maydell 
1578b41c305SPeter Maydell     sdhci_reset(s);
1588b41c305SPeter Maydell 
1598b41c305SPeter Maydell     if (s->pending_insert_quirk) {
1608b41c305SPeter Maydell         s->pending_insert_state = true;
1618b41c305SPeter Maydell     }
1628b41c305SPeter Maydell }
1638b41c305SPeter Maydell 
164d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque);
16549ab747fSPaolo Bonzini 
16649ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s)
16749ab747fSPaolo Bonzini {
16849ab747fSPaolo Bonzini     SDRequest request;
16949ab747fSPaolo Bonzini     uint8_t response[16];
17049ab747fSPaolo Bonzini     int rlen;
17149ab747fSPaolo Bonzini 
17249ab747fSPaolo Bonzini     s->errintsts = 0;
17349ab747fSPaolo Bonzini     s->acmd12errsts = 0;
17449ab747fSPaolo Bonzini     request.cmd = s->cmdreg >> 8;
17549ab747fSPaolo Bonzini     request.arg = s->argument;
1768be487d8SPhilippe Mathieu-Daudé 
1778be487d8SPhilippe Mathieu-Daudé     trace_sdhci_send_command(request.cmd, request.arg);
17840bbc194SPeter Maydell     rlen = sdbus_do_command(&s->sdbus, &request, response);
17949ab747fSPaolo Bonzini 
18049ab747fSPaolo Bonzini     if (s->cmdreg & SDHC_CMD_RESPONSE) {
18149ab747fSPaolo Bonzini         if (rlen == 4) {
18249ab747fSPaolo Bonzini             s->rspreg[0] = (response[0] << 24) | (response[1] << 16) |
18349ab747fSPaolo Bonzini                            (response[2] << 8)  |  response[3];
18449ab747fSPaolo Bonzini             s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0;
1858be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response4(s->rspreg[0]);
18649ab747fSPaolo Bonzini         } else if (rlen == 16) {
18749ab747fSPaolo Bonzini             s->rspreg[0] = (response[11] << 24) | (response[12] << 16) |
18849ab747fSPaolo Bonzini                            (response[13] << 8) |  response[14];
18949ab747fSPaolo Bonzini             s->rspreg[1] = (response[7] << 24) | (response[8] << 16) |
19049ab747fSPaolo Bonzini                            (response[9] << 8)  |  response[10];
19149ab747fSPaolo Bonzini             s->rspreg[2] = (response[3] << 24) | (response[4] << 16) |
19249ab747fSPaolo Bonzini                            (response[5] << 8)  |  response[6];
19349ab747fSPaolo Bonzini             s->rspreg[3] = (response[0] << 16) | (response[1] << 8) |
19449ab747fSPaolo Bonzini                             response[2];
1958be487d8SPhilippe Mathieu-Daudé             trace_sdhci_response16(s->rspreg[3], s->rspreg[2],
1968be487d8SPhilippe Mathieu-Daudé                                    s->rspreg[1], s->rspreg[0]);
19749ab747fSPaolo Bonzini         } else {
1988be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("timeout waiting for command response");
19949ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) {
20049ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_CMDTIMEOUT;
20149ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
20249ab747fSPaolo Bonzini             }
20349ab747fSPaolo Bonzini         }
20449ab747fSPaolo Bonzini 
205fd1e5c81SAndrey Smirnov         if (!(s->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
206fd1e5c81SAndrey Smirnov             (s->norintstsen & SDHC_NISEN_TRSCMP) &&
20749ab747fSPaolo Bonzini             (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) {
20849ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_TRSCMP;
20949ab747fSPaolo Bonzini         }
21049ab747fSPaolo Bonzini     }
21149ab747fSPaolo Bonzini 
21249ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_CMDCMP) {
21349ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_CMDCMP;
21449ab747fSPaolo Bonzini     }
21549ab747fSPaolo Bonzini 
21649ab747fSPaolo Bonzini     sdhci_update_irq(s);
21749ab747fSPaolo Bonzini 
21849ab747fSPaolo Bonzini     if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) {
219656f416cSPeter Crosthwaite         s->data_count = 0;
220d368ba43SKevin O'Connor         sdhci_data_transfer(s);
22149ab747fSPaolo Bonzini     }
22249ab747fSPaolo Bonzini }
22349ab747fSPaolo Bonzini 
22449ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s)
22549ab747fSPaolo Bonzini {
22649ab747fSPaolo Bonzini     /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
22749ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) {
22849ab747fSPaolo Bonzini         SDRequest request;
22949ab747fSPaolo Bonzini         uint8_t response[16];
23049ab747fSPaolo Bonzini 
23149ab747fSPaolo Bonzini         request.cmd = 0x0C;
23249ab747fSPaolo Bonzini         request.arg = 0;
2338be487d8SPhilippe Mathieu-Daudé         trace_sdhci_end_transfer(request.cmd, request.arg);
23440bbc194SPeter Maydell         sdbus_do_command(&s->sdbus, &request, response);
23549ab747fSPaolo Bonzini         /* Auto CMD12 response goes to the upper Response register */
23649ab747fSPaolo Bonzini         s->rspreg[3] = (response[0] << 24) | (response[1] << 16) |
23749ab747fSPaolo Bonzini                 (response[2] << 8) | response[3];
23849ab747fSPaolo Bonzini     }
23949ab747fSPaolo Bonzini 
24049ab747fSPaolo Bonzini     s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE |
24149ab747fSPaolo Bonzini             SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT |
24249ab747fSPaolo Bonzini             SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE);
24349ab747fSPaolo Bonzini 
24449ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_TRSCMP) {
24549ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_TRSCMP;
24649ab747fSPaolo Bonzini     }
24749ab747fSPaolo Bonzini 
24849ab747fSPaolo Bonzini     sdhci_update_irq(s);
24949ab747fSPaolo Bonzini }
25049ab747fSPaolo Bonzini 
25149ab747fSPaolo Bonzini /*
25249ab747fSPaolo Bonzini  * Programmed i/o data transfer
25349ab747fSPaolo Bonzini  */
25449ab747fSPaolo Bonzini 
25549ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
25649ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s)
25749ab747fSPaolo Bonzini {
25849ab747fSPaolo Bonzini     int index = 0;
25949ab747fSPaolo Bonzini 
26049ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) &&
26149ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) {
26249ab747fSPaolo Bonzini         return;
26349ab747fSPaolo Bonzini     }
26449ab747fSPaolo Bonzini 
26549ab747fSPaolo Bonzini     for (index = 0; index < (s->blksize & 0x0fff); index++) {
26640bbc194SPeter Maydell         s->fifo_buffer[index] = sdbus_read_data(&s->sdbus);
26749ab747fSPaolo Bonzini     }
26849ab747fSPaolo Bonzini 
26949ab747fSPaolo Bonzini     /* New data now available for READ through Buffer Port Register */
27049ab747fSPaolo Bonzini     s->prnsts |= SDHC_DATA_AVAILABLE;
27149ab747fSPaolo Bonzini     if (s->norintstsen & SDHC_NISEN_RBUFRDY) {
27249ab747fSPaolo Bonzini         s->norintsts |= SDHC_NIS_RBUFRDY;
27349ab747fSPaolo Bonzini     }
27449ab747fSPaolo Bonzini 
27549ab747fSPaolo Bonzini     /* Clear DAT line active status if that was the last block */
27649ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
27749ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) {
27849ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
27949ab747fSPaolo Bonzini     }
28049ab747fSPaolo Bonzini 
28149ab747fSPaolo Bonzini     /* If stop at block gap request was set and it's not the last block of
28249ab747fSPaolo Bonzini      * data - generate Block Event interrupt */
28349ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) &&
28449ab747fSPaolo Bonzini             s->blkcnt != 1)    {
28549ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DAT_LINE_ACTIVE;
28649ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
28749ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
28849ab747fSPaolo Bonzini         }
28949ab747fSPaolo Bonzini     }
29049ab747fSPaolo Bonzini 
29149ab747fSPaolo Bonzini     sdhci_update_irq(s);
29249ab747fSPaolo Bonzini }
29349ab747fSPaolo Bonzini 
29449ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
29549ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size)
29649ab747fSPaolo Bonzini {
29749ab747fSPaolo Bonzini     uint32_t value = 0;
29849ab747fSPaolo Bonzini     int i;
29949ab747fSPaolo Bonzini 
30049ab747fSPaolo Bonzini     /* first check that a valid data exists in host controller input buffer */
30149ab747fSPaolo Bonzini     if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) {
3028be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("read from empty buffer");
30349ab747fSPaolo Bonzini         return 0;
30449ab747fSPaolo Bonzini     }
30549ab747fSPaolo Bonzini 
30649ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
30749ab747fSPaolo Bonzini         value |= s->fifo_buffer[s->data_count] << i * 8;
30849ab747fSPaolo Bonzini         s->data_count++;
30949ab747fSPaolo Bonzini         /* check if we've read all valid data (blksize bytes) from buffer */
31049ab747fSPaolo Bonzini         if ((s->data_count) >= (s->blksize & 0x0fff)) {
3118be487d8SPhilippe Mathieu-Daudé             trace_sdhci_read_dataport(s->data_count);
31249ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */
31349ab747fSPaolo Bonzini             s->data_count = 0;  /* next buff read must start at position [0] */
31449ab747fSPaolo Bonzini 
31549ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
31649ab747fSPaolo Bonzini                 s->blkcnt--;
31749ab747fSPaolo Bonzini             }
31849ab747fSPaolo Bonzini 
31949ab747fSPaolo Bonzini             /* if that was the last block of data */
32049ab747fSPaolo Bonzini             if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
32149ab747fSPaolo Bonzini                 ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) ||
32249ab747fSPaolo Bonzini                  /* stop at gap request */
32349ab747fSPaolo Bonzini                 (s->stopped_state == sdhc_gap_read &&
32449ab747fSPaolo Bonzini                  !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) {
325d368ba43SKevin O'Connor                 sdhci_end_transfer(s);
32649ab747fSPaolo Bonzini             } else { /* if there are more data, read next block from card */
327d368ba43SKevin O'Connor                 sdhci_read_block_from_card(s);
32849ab747fSPaolo Bonzini             }
32949ab747fSPaolo Bonzini             break;
33049ab747fSPaolo Bonzini         }
33149ab747fSPaolo Bonzini     }
33249ab747fSPaolo Bonzini 
33349ab747fSPaolo Bonzini     return value;
33449ab747fSPaolo Bonzini }
33549ab747fSPaolo Bonzini 
33649ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */
33749ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s)
33849ab747fSPaolo Bonzini {
33949ab747fSPaolo Bonzini     int index = 0;
34049ab747fSPaolo Bonzini 
34149ab747fSPaolo Bonzini     if (s->prnsts & SDHC_SPACE_AVAILABLE) {
34249ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
34349ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_WBUFRDY;
34449ab747fSPaolo Bonzini         }
34549ab747fSPaolo Bonzini         sdhci_update_irq(s);
34649ab747fSPaolo Bonzini         return;
34749ab747fSPaolo Bonzini     }
34849ab747fSPaolo Bonzini 
34949ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
35049ab747fSPaolo Bonzini         if (s->blkcnt == 0) {
35149ab747fSPaolo Bonzini             return;
35249ab747fSPaolo Bonzini         } else {
35349ab747fSPaolo Bonzini             s->blkcnt--;
35449ab747fSPaolo Bonzini         }
35549ab747fSPaolo Bonzini     }
35649ab747fSPaolo Bonzini 
35749ab747fSPaolo Bonzini     for (index = 0; index < (s->blksize & 0x0fff); index++) {
35840bbc194SPeter Maydell         sdbus_write_data(&s->sdbus, s->fifo_buffer[index]);
35949ab747fSPaolo Bonzini     }
36049ab747fSPaolo Bonzini 
36149ab747fSPaolo Bonzini     /* Next data can be written through BUFFER DATORT register */
36249ab747fSPaolo Bonzini     s->prnsts |= SDHC_SPACE_AVAILABLE;
36349ab747fSPaolo Bonzini 
36449ab747fSPaolo Bonzini     /* Finish transfer if that was the last block of data */
36549ab747fSPaolo Bonzini     if ((s->trnmod & SDHC_TRNS_MULTI) == 0 ||
36649ab747fSPaolo Bonzini             ((s->trnmod & SDHC_TRNS_MULTI) &&
36749ab747fSPaolo Bonzini             (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) {
368d368ba43SKevin O'Connor         sdhci_end_transfer(s);
369dcdb4cd8SPeter Crosthwaite     } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) {
370dcdb4cd8SPeter Crosthwaite         s->norintsts |= SDHC_NIS_WBUFRDY;
37149ab747fSPaolo Bonzini     }
37249ab747fSPaolo Bonzini 
37349ab747fSPaolo Bonzini     /* Generate Block Gap Event if requested and if not the last block */
37449ab747fSPaolo Bonzini     if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) &&
37549ab747fSPaolo Bonzini             s->blkcnt > 0) {
37649ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_DOING_WRITE;
37749ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_EISEN_BLKGAP) {
37849ab747fSPaolo Bonzini             s->norintsts |= SDHC_EIS_BLKGAP;
37949ab747fSPaolo Bonzini         }
380d368ba43SKevin O'Connor         sdhci_end_transfer(s);
38149ab747fSPaolo Bonzini     }
38249ab747fSPaolo Bonzini 
38349ab747fSPaolo Bonzini     sdhci_update_irq(s);
38449ab747fSPaolo Bonzini }
38549ab747fSPaolo Bonzini 
38649ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port
38749ab747fSPaolo Bonzini  * register */
38849ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size)
38949ab747fSPaolo Bonzini {
39049ab747fSPaolo Bonzini     unsigned i;
39149ab747fSPaolo Bonzini 
39249ab747fSPaolo Bonzini     /* Check that there is free space left in a buffer */
39349ab747fSPaolo Bonzini     if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) {
3948be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("Can't write to data buffer: buffer full");
39549ab747fSPaolo Bonzini         return;
39649ab747fSPaolo Bonzini     }
39749ab747fSPaolo Bonzini 
39849ab747fSPaolo Bonzini     for (i = 0; i < size; i++) {
39949ab747fSPaolo Bonzini         s->fifo_buffer[s->data_count] = value & 0xFF;
40049ab747fSPaolo Bonzini         s->data_count++;
40149ab747fSPaolo Bonzini         value >>= 8;
40249ab747fSPaolo Bonzini         if (s->data_count >= (s->blksize & 0x0fff)) {
4038be487d8SPhilippe Mathieu-Daudé             trace_sdhci_write_dataport(s->data_count);
40449ab747fSPaolo Bonzini             s->data_count = 0;
40549ab747fSPaolo Bonzini             s->prnsts &= ~SDHC_SPACE_AVAILABLE;
40649ab747fSPaolo Bonzini             if (s->prnsts & SDHC_DOING_WRITE) {
407d368ba43SKevin O'Connor                 sdhci_write_block_to_card(s);
40849ab747fSPaolo Bonzini             }
40949ab747fSPaolo Bonzini         }
41049ab747fSPaolo Bonzini     }
41149ab747fSPaolo Bonzini }
41249ab747fSPaolo Bonzini 
41349ab747fSPaolo Bonzini /*
41449ab747fSPaolo Bonzini  * Single DMA data transfer
41549ab747fSPaolo Bonzini  */
41649ab747fSPaolo Bonzini 
41749ab747fSPaolo Bonzini /* Multi block SDMA transfer */
41849ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
41949ab747fSPaolo Bonzini {
42049ab747fSPaolo Bonzini     bool page_aligned = false;
42149ab747fSPaolo Bonzini     unsigned int n, begin;
42249ab747fSPaolo Bonzini     const uint16_t block_size = s->blksize & 0x0fff;
42349ab747fSPaolo Bonzini     uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12);
42449ab747fSPaolo Bonzini     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
42549ab747fSPaolo Bonzini 
4266e86d903SPrasad J Pandit     if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) {
4276e86d903SPrasad J Pandit         qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n");
4286e86d903SPrasad J Pandit         return;
4296e86d903SPrasad J Pandit     }
4306e86d903SPrasad J Pandit 
43149ab747fSPaolo Bonzini     /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
43249ab747fSPaolo Bonzini      * possible stop at page boundary if initial address is not page aligned,
43349ab747fSPaolo Bonzini      * allow them to work properly */
43449ab747fSPaolo Bonzini     if ((s->sdmasysad % boundary_chk) == 0) {
43549ab747fSPaolo Bonzini         page_aligned = true;
43649ab747fSPaolo Bonzini     }
43749ab747fSPaolo Bonzini 
43849ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
43949ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
44049ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
44149ab747fSPaolo Bonzini         while (s->blkcnt) {
44249ab747fSPaolo Bonzini             if (s->data_count == 0) {
44349ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
44440bbc194SPeter Maydell                     s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
44549ab747fSPaolo Bonzini                 }
44649ab747fSPaolo Bonzini             }
44749ab747fSPaolo Bonzini             begin = s->data_count;
44849ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
44949ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
45049ab747fSPaolo Bonzini                 boundary_count = 0;
45149ab747fSPaolo Bonzini              } else {
45249ab747fSPaolo Bonzini                 s->data_count = block_size;
45349ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
45449ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
45549ab747fSPaolo Bonzini                     s->blkcnt--;
45649ab747fSPaolo Bonzini                 }
45749ab747fSPaolo Bonzini             }
458dd55c485SPhilippe Mathieu-Daudé             dma_memory_write(s->dma_as, s->sdmasysad,
45949ab747fSPaolo Bonzini                              &s->fifo_buffer[begin], s->data_count - begin);
46049ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
46149ab747fSPaolo Bonzini             if (s->data_count == block_size) {
46249ab747fSPaolo Bonzini                 s->data_count = 0;
46349ab747fSPaolo Bonzini             }
46449ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
46549ab747fSPaolo Bonzini                 break;
46649ab747fSPaolo Bonzini             }
46749ab747fSPaolo Bonzini         }
46849ab747fSPaolo Bonzini     } else {
46949ab747fSPaolo Bonzini         s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT |
47049ab747fSPaolo Bonzini                 SDHC_DAT_LINE_ACTIVE;
47149ab747fSPaolo Bonzini         while (s->blkcnt) {
47249ab747fSPaolo Bonzini             begin = s->data_count;
47349ab747fSPaolo Bonzini             if (((boundary_count + begin) < block_size) && page_aligned) {
47449ab747fSPaolo Bonzini                 s->data_count = boundary_count + begin;
47549ab747fSPaolo Bonzini                 boundary_count = 0;
47649ab747fSPaolo Bonzini              } else {
47749ab747fSPaolo Bonzini                 s->data_count = block_size;
47849ab747fSPaolo Bonzini                 boundary_count -= block_size - begin;
47949ab747fSPaolo Bonzini             }
480dd55c485SPhilippe Mathieu-Daudé             dma_memory_read(s->dma_as, s->sdmasysad,
48142922105SPrasad J Pandit                             &s->fifo_buffer[begin], s->data_count - begin);
48249ab747fSPaolo Bonzini             s->sdmasysad += s->data_count - begin;
48349ab747fSPaolo Bonzini             if (s->data_count == block_size) {
48449ab747fSPaolo Bonzini                 for (n = 0; n < block_size; n++) {
48540bbc194SPeter Maydell                     sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
48649ab747fSPaolo Bonzini                 }
48749ab747fSPaolo Bonzini                 s->data_count = 0;
48849ab747fSPaolo Bonzini                 if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
48949ab747fSPaolo Bonzini                     s->blkcnt--;
49049ab747fSPaolo Bonzini                 }
49149ab747fSPaolo Bonzini             }
49249ab747fSPaolo Bonzini             if (page_aligned && boundary_count == 0) {
49349ab747fSPaolo Bonzini                 break;
49449ab747fSPaolo Bonzini             }
49549ab747fSPaolo Bonzini         }
49649ab747fSPaolo Bonzini     }
49749ab747fSPaolo Bonzini 
49849ab747fSPaolo Bonzini     if (s->blkcnt == 0) {
499d368ba43SKevin O'Connor         sdhci_end_transfer(s);
50049ab747fSPaolo Bonzini     } else {
50149ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_DMA) {
50249ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_DMA;
50349ab747fSPaolo Bonzini         }
50449ab747fSPaolo Bonzini         sdhci_update_irq(s);
50549ab747fSPaolo Bonzini     }
50649ab747fSPaolo Bonzini }
50749ab747fSPaolo Bonzini 
50849ab747fSPaolo Bonzini /* single block SDMA transfer */
50949ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s)
51049ab747fSPaolo Bonzini {
51149ab747fSPaolo Bonzini     int n;
51249ab747fSPaolo Bonzini     uint32_t datacnt = s->blksize & 0x0fff;
51349ab747fSPaolo Bonzini 
51449ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_READ) {
51549ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
51640bbc194SPeter Maydell             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
51749ab747fSPaolo Bonzini         }
518dd55c485SPhilippe Mathieu-Daudé         dma_memory_write(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt);
51949ab747fSPaolo Bonzini     } else {
520dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, s->sdmasysad, s->fifo_buffer, datacnt);
52149ab747fSPaolo Bonzini         for (n = 0; n < datacnt; n++) {
52240bbc194SPeter Maydell             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
52349ab747fSPaolo Bonzini         }
52449ab747fSPaolo Bonzini     }
52549ab747fSPaolo Bonzini     s->blkcnt--;
52649ab747fSPaolo Bonzini 
527d368ba43SKevin O'Connor     sdhci_end_transfer(s);
52849ab747fSPaolo Bonzini }
52949ab747fSPaolo Bonzini 
53049ab747fSPaolo Bonzini typedef struct ADMADescr {
53149ab747fSPaolo Bonzini     hwaddr addr;
53249ab747fSPaolo Bonzini     uint16_t length;
53349ab747fSPaolo Bonzini     uint8_t attr;
53449ab747fSPaolo Bonzini     uint8_t incr;
53549ab747fSPaolo Bonzini } ADMADescr;
53649ab747fSPaolo Bonzini 
53749ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
53849ab747fSPaolo Bonzini {
53949ab747fSPaolo Bonzini     uint32_t adma1 = 0;
54049ab747fSPaolo Bonzini     uint64_t adma2 = 0;
54149ab747fSPaolo Bonzini     hwaddr entry_addr = (hwaddr)s->admasysaddr;
54249ab747fSPaolo Bonzini     switch (SDHC_DMA_TYPE(s->hostctl)) {
54349ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_32:
544dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma2,
54549ab747fSPaolo Bonzini                         sizeof(adma2));
54649ab747fSPaolo Bonzini         adma2 = le64_to_cpu(adma2);
54749ab747fSPaolo Bonzini         /* The spec does not specify endianness of descriptor table.
54849ab747fSPaolo Bonzini          * We currently assume that it is LE.
54949ab747fSPaolo Bonzini          */
55049ab747fSPaolo Bonzini         dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull;
55149ab747fSPaolo Bonzini         dscr->length = (uint16_t)extract64(adma2, 16, 16);
55249ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract64(adma2, 0, 7);
55349ab747fSPaolo Bonzini         dscr->incr = 8;
55449ab747fSPaolo Bonzini         break;
55549ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA1_32:
556dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr, (uint8_t *)&adma1,
55749ab747fSPaolo Bonzini                         sizeof(adma1));
55849ab747fSPaolo Bonzini         adma1 = le32_to_cpu(adma1);
55949ab747fSPaolo Bonzini         dscr->addr = (hwaddr)(adma1 & 0xFFFFF000);
56049ab747fSPaolo Bonzini         dscr->attr = (uint8_t)extract32(adma1, 0, 7);
56149ab747fSPaolo Bonzini         dscr->incr = 4;
56249ab747fSPaolo Bonzini         if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
56349ab747fSPaolo Bonzini             dscr->length = (uint16_t)extract32(adma1, 12, 16);
56449ab747fSPaolo Bonzini         } else {
56549ab747fSPaolo Bonzini             dscr->length = 4096;
56649ab747fSPaolo Bonzini         }
56749ab747fSPaolo Bonzini         break;
56849ab747fSPaolo Bonzini     case SDHC_CTRL_ADMA2_64:
569dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr,
57049ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->attr), 1);
571dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 2,
57249ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->length), 2);
57349ab747fSPaolo Bonzini         dscr->length = le16_to_cpu(dscr->length);
574dd55c485SPhilippe Mathieu-Daudé         dma_memory_read(s->dma_as, entry_addr + 4,
57549ab747fSPaolo Bonzini                         (uint8_t *)(&dscr->addr), 8);
57649ab747fSPaolo Bonzini         dscr->attr = le64_to_cpu(dscr->attr);
57749ab747fSPaolo Bonzini         dscr->attr &= 0xfffffff8;
57849ab747fSPaolo Bonzini         dscr->incr = 12;
57949ab747fSPaolo Bonzini         break;
58049ab747fSPaolo Bonzini     }
58149ab747fSPaolo Bonzini }
58249ab747fSPaolo Bonzini 
58349ab747fSPaolo Bonzini /* Advanced DMA data transfer */
58449ab747fSPaolo Bonzini 
58549ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s)
58649ab747fSPaolo Bonzini {
58749ab747fSPaolo Bonzini     unsigned int n, begin, length;
58849ab747fSPaolo Bonzini     const uint16_t block_size = s->blksize & 0x0fff;
5898be487d8SPhilippe Mathieu-Daudé     ADMADescr dscr = {};
59049ab747fSPaolo Bonzini     int i;
59149ab747fSPaolo Bonzini 
59249ab747fSPaolo Bonzini     for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
59349ab747fSPaolo Bonzini         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
59449ab747fSPaolo Bonzini 
59549ab747fSPaolo Bonzini         get_adma_description(s, &dscr);
5968be487d8SPhilippe Mathieu-Daudé         trace_sdhci_adma_loop(dscr.addr, dscr.length, dscr.attr);
59749ab747fSPaolo Bonzini 
59849ab747fSPaolo Bonzini         if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
59949ab747fSPaolo Bonzini             /* Indicate that error occurred in ST_FDS state */
60049ab747fSPaolo Bonzini             s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
60149ab747fSPaolo Bonzini             s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
60249ab747fSPaolo Bonzini 
60349ab747fSPaolo Bonzini             /* Generate ADMA error interrupt */
60449ab747fSPaolo Bonzini             if (s->errintstsen & SDHC_EISEN_ADMAERR) {
60549ab747fSPaolo Bonzini                 s->errintsts |= SDHC_EIS_ADMAERR;
60649ab747fSPaolo Bonzini                 s->norintsts |= SDHC_NIS_ERR;
60749ab747fSPaolo Bonzini             }
60849ab747fSPaolo Bonzini 
60949ab747fSPaolo Bonzini             sdhci_update_irq(s);
61049ab747fSPaolo Bonzini             return;
61149ab747fSPaolo Bonzini         }
61249ab747fSPaolo Bonzini 
61349ab747fSPaolo Bonzini         length = dscr.length ? dscr.length : 65536;
61449ab747fSPaolo Bonzini 
61549ab747fSPaolo Bonzini         switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) {
61649ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_TRAN:  /* data transfer */
61749ab747fSPaolo Bonzini 
61849ab747fSPaolo Bonzini             if (s->trnmod & SDHC_TRNS_READ) {
61949ab747fSPaolo Bonzini                 while (length) {
62049ab747fSPaolo Bonzini                     if (s->data_count == 0) {
62149ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
62240bbc194SPeter Maydell                             s->fifo_buffer[n] = sdbus_read_data(&s->sdbus);
62349ab747fSPaolo Bonzini                         }
62449ab747fSPaolo Bonzini                     }
62549ab747fSPaolo Bonzini                     begin = s->data_count;
62649ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
62749ab747fSPaolo Bonzini                         s->data_count = length + begin;
62849ab747fSPaolo Bonzini                         length = 0;
62949ab747fSPaolo Bonzini                      } else {
63049ab747fSPaolo Bonzini                         s->data_count = block_size;
63149ab747fSPaolo Bonzini                         length -= block_size - begin;
63249ab747fSPaolo Bonzini                     }
633dd55c485SPhilippe Mathieu-Daudé                     dma_memory_write(s->dma_as, dscr.addr,
63449ab747fSPaolo Bonzini                                      &s->fifo_buffer[begin],
63549ab747fSPaolo Bonzini                                      s->data_count - begin);
63649ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
63749ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
63849ab747fSPaolo Bonzini                         s->data_count = 0;
63949ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
64049ab747fSPaolo Bonzini                             s->blkcnt--;
64149ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
64249ab747fSPaolo Bonzini                                 break;
64349ab747fSPaolo Bonzini                             }
64449ab747fSPaolo Bonzini                         }
64549ab747fSPaolo Bonzini                     }
64649ab747fSPaolo Bonzini                 }
64749ab747fSPaolo Bonzini             } else {
64849ab747fSPaolo Bonzini                 while (length) {
64949ab747fSPaolo Bonzini                     begin = s->data_count;
65049ab747fSPaolo Bonzini                     if ((length + begin) < block_size) {
65149ab747fSPaolo Bonzini                         s->data_count = length + begin;
65249ab747fSPaolo Bonzini                         length = 0;
65349ab747fSPaolo Bonzini                      } else {
65449ab747fSPaolo Bonzini                         s->data_count = block_size;
65549ab747fSPaolo Bonzini                         length -= block_size - begin;
65649ab747fSPaolo Bonzini                     }
657dd55c485SPhilippe Mathieu-Daudé                     dma_memory_read(s->dma_as, dscr.addr,
6589db11cefSPeter Crosthwaite                                     &s->fifo_buffer[begin],
6599db11cefSPeter Crosthwaite                                     s->data_count - begin);
66049ab747fSPaolo Bonzini                     dscr.addr += s->data_count - begin;
66149ab747fSPaolo Bonzini                     if (s->data_count == block_size) {
66249ab747fSPaolo Bonzini                         for (n = 0; n < block_size; n++) {
66340bbc194SPeter Maydell                             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
66449ab747fSPaolo Bonzini                         }
66549ab747fSPaolo Bonzini                         s->data_count = 0;
66649ab747fSPaolo Bonzini                         if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
66749ab747fSPaolo Bonzini                             s->blkcnt--;
66849ab747fSPaolo Bonzini                             if (s->blkcnt == 0) {
66949ab747fSPaolo Bonzini                                 break;
67049ab747fSPaolo Bonzini                             }
67149ab747fSPaolo Bonzini                         }
67249ab747fSPaolo Bonzini                     }
67349ab747fSPaolo Bonzini                 }
67449ab747fSPaolo Bonzini             }
67549ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
67649ab747fSPaolo Bonzini             break;
67749ab747fSPaolo Bonzini         case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
67849ab747fSPaolo Bonzini             s->admasysaddr = dscr.addr;
6798be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("link", s->admasysaddr);
68049ab747fSPaolo Bonzini             break;
68149ab747fSPaolo Bonzini         default:
68249ab747fSPaolo Bonzini             s->admasysaddr += dscr.incr;
68349ab747fSPaolo Bonzini             break;
68449ab747fSPaolo Bonzini         }
68549ab747fSPaolo Bonzini 
6861d32c26fSPeter Crosthwaite         if (dscr.attr & SDHC_ADMA_ATTR_INT) {
6878be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma("interrupt", s->admasysaddr);
6881d32c26fSPeter Crosthwaite             if (s->norintstsen & SDHC_NISEN_DMA) {
6891d32c26fSPeter Crosthwaite                 s->norintsts |= SDHC_NIS_DMA;
6901d32c26fSPeter Crosthwaite             }
6911d32c26fSPeter Crosthwaite 
6921d32c26fSPeter Crosthwaite             sdhci_update_irq(s);
6931d32c26fSPeter Crosthwaite         }
6941d32c26fSPeter Crosthwaite 
69549ab747fSPaolo Bonzini         /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
69649ab747fSPaolo Bonzini         if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
69749ab747fSPaolo Bonzini                     (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
6988be487d8SPhilippe Mathieu-Daudé             trace_sdhci_adma_transfer_completed();
69949ab747fSPaolo Bonzini             if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
70049ab747fSPaolo Bonzini                 (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
70149ab747fSPaolo Bonzini                 s->blkcnt != 0)) {
7028be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("SD/MMC host ADMA length mismatch");
70349ab747fSPaolo Bonzini                 s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
70449ab747fSPaolo Bonzini                         SDHC_ADMAERR_STATE_ST_TFR;
70549ab747fSPaolo Bonzini                 if (s->errintstsen & SDHC_EISEN_ADMAERR) {
7068be487d8SPhilippe Mathieu-Daudé                     trace_sdhci_error("Set ADMA error flag");
70749ab747fSPaolo Bonzini                     s->errintsts |= SDHC_EIS_ADMAERR;
70849ab747fSPaolo Bonzini                     s->norintsts |= SDHC_NIS_ERR;
70949ab747fSPaolo Bonzini                 }
71049ab747fSPaolo Bonzini 
71149ab747fSPaolo Bonzini                 sdhci_update_irq(s);
71249ab747fSPaolo Bonzini             }
713d368ba43SKevin O'Connor             sdhci_end_transfer(s);
71449ab747fSPaolo Bonzini             return;
71549ab747fSPaolo Bonzini         }
71649ab747fSPaolo Bonzini 
71749ab747fSPaolo Bonzini     }
71849ab747fSPaolo Bonzini 
71949ab747fSPaolo Bonzini     /* we have unfinished business - reschedule to continue ADMA */
720bc72ad67SAlex Bligh     timer_mod(s->transfer_timer,
721bc72ad67SAlex Bligh                    qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY);
72249ab747fSPaolo Bonzini }
72349ab747fSPaolo Bonzini 
72449ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */
72549ab747fSPaolo Bonzini 
726d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque)
72749ab747fSPaolo Bonzini {
728d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
72949ab747fSPaolo Bonzini 
73049ab747fSPaolo Bonzini     if (s->trnmod & SDHC_TRNS_DMA) {
73149ab747fSPaolo Bonzini         switch (SDHC_DMA_TYPE(s->hostctl)) {
73249ab747fSPaolo Bonzini         case SDHC_CTRL_SDMA:
73349ab747fSPaolo Bonzini             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
734d368ba43SKevin O'Connor                 sdhci_sdma_transfer_single_block(s);
73549ab747fSPaolo Bonzini             } else {
736d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
73749ab747fSPaolo Bonzini             }
73849ab747fSPaolo Bonzini 
73949ab747fSPaolo Bonzini             break;
74049ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA1_32:
74149ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA1)) {
7428be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA1 not supported");
74349ab747fSPaolo Bonzini                 break;
74449ab747fSPaolo Bonzini             }
74549ab747fSPaolo Bonzini 
746d368ba43SKevin O'Connor             sdhci_do_adma(s);
74749ab747fSPaolo Bonzini             break;
74849ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_32:
74949ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA2)) {
7508be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("ADMA2 not supported");
75149ab747fSPaolo Bonzini                 break;
75249ab747fSPaolo Bonzini             }
75349ab747fSPaolo Bonzini 
754d368ba43SKevin O'Connor             sdhci_do_adma(s);
75549ab747fSPaolo Bonzini             break;
75649ab747fSPaolo Bonzini         case SDHC_CTRL_ADMA2_64:
75749ab747fSPaolo Bonzini             if (!(s->capareg & SDHC_CAN_DO_ADMA2) ||
75849ab747fSPaolo Bonzini                     !(s->capareg & SDHC_64_BIT_BUS_SUPPORT)) {
7598be487d8SPhilippe Mathieu-Daudé                 trace_sdhci_error("64 bit ADMA not supported");
76049ab747fSPaolo Bonzini                 break;
76149ab747fSPaolo Bonzini             }
76249ab747fSPaolo Bonzini 
763d368ba43SKevin O'Connor             sdhci_do_adma(s);
76449ab747fSPaolo Bonzini             break;
76549ab747fSPaolo Bonzini         default:
7668be487d8SPhilippe Mathieu-Daudé             trace_sdhci_error("Unsupported DMA type");
76749ab747fSPaolo Bonzini             break;
76849ab747fSPaolo Bonzini         }
76949ab747fSPaolo Bonzini     } else {
77040bbc194SPeter Maydell         if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) {
77149ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT |
77249ab747fSPaolo Bonzini                     SDHC_DAT_LINE_ACTIVE;
773d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
77449ab747fSPaolo Bonzini         } else {
77549ab747fSPaolo Bonzini             s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE |
77649ab747fSPaolo Bonzini                     SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT;
777d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
77849ab747fSPaolo Bonzini         }
77949ab747fSPaolo Bonzini     }
78049ab747fSPaolo Bonzini }
78149ab747fSPaolo Bonzini 
78249ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s)
78349ab747fSPaolo Bonzini {
7846890a695SPeter Crosthwaite     if (!SDHC_CLOCK_IS_ON(s->clkcon) ||
78549ab747fSPaolo Bonzini         (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) &&
78649ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_DATA_PRESENT) ||
78749ab747fSPaolo Bonzini         ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY &&
78849ab747fSPaolo Bonzini         !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) {
78949ab747fSPaolo Bonzini         return false;
79049ab747fSPaolo Bonzini     }
79149ab747fSPaolo Bonzini 
79249ab747fSPaolo Bonzini     return true;
79349ab747fSPaolo Bonzini }
79449ab747fSPaolo Bonzini 
79549ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and
79649ab747fSPaolo Bonzini  * continuous manner */
79749ab747fSPaolo Bonzini static inline bool
79849ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
79949ab747fSPaolo Bonzini {
80049ab747fSPaolo Bonzini     if ((s->data_count & 0x3) != byte_num) {
8018be487d8SPhilippe Mathieu-Daudé         trace_sdhci_error("Non-sequential access to Buffer Data Port register"
80249ab747fSPaolo Bonzini                           "is prohibited\n");
80349ab747fSPaolo Bonzini         return false;
80449ab747fSPaolo Bonzini     }
80549ab747fSPaolo Bonzini     return true;
80649ab747fSPaolo Bonzini }
80749ab747fSPaolo Bonzini 
808d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size)
80949ab747fSPaolo Bonzini {
810d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
81149ab747fSPaolo Bonzini     uint32_t ret = 0;
81249ab747fSPaolo Bonzini 
81349ab747fSPaolo Bonzini     switch (offset & ~0x3) {
81449ab747fSPaolo Bonzini     case SDHC_SYSAD:
81549ab747fSPaolo Bonzini         ret = s->sdmasysad;
81649ab747fSPaolo Bonzini         break;
81749ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
81849ab747fSPaolo Bonzini         ret = s->blksize | (s->blkcnt << 16);
81949ab747fSPaolo Bonzini         break;
82049ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
82149ab747fSPaolo Bonzini         ret = s->argument;
82249ab747fSPaolo Bonzini         break;
82349ab747fSPaolo Bonzini     case SDHC_TRNMOD:
82449ab747fSPaolo Bonzini         ret = s->trnmod | (s->cmdreg << 16);
82549ab747fSPaolo Bonzini         break;
82649ab747fSPaolo Bonzini     case SDHC_RSPREG0 ... SDHC_RSPREG3:
82749ab747fSPaolo Bonzini         ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2];
82849ab747fSPaolo Bonzini         break;
82949ab747fSPaolo Bonzini     case  SDHC_BDATA:
83049ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
831d368ba43SKevin O'Connor             ret = sdhci_read_dataport(s, size);
8328be487d8SPhilippe Mathieu-Daudé             trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
83349ab747fSPaolo Bonzini             return ret;
83449ab747fSPaolo Bonzini         }
83549ab747fSPaolo Bonzini         break;
83649ab747fSPaolo Bonzini     case SDHC_PRNSTS:
83749ab747fSPaolo Bonzini         ret = s->prnsts;
83849ab747fSPaolo Bonzini         break;
83949ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
84049ab747fSPaolo Bonzini         ret = s->hostctl | (s->pwrcon << 8) | (s->blkgap << 16) |
84149ab747fSPaolo Bonzini               (s->wakcon << 24);
84249ab747fSPaolo Bonzini         break;
84349ab747fSPaolo Bonzini     case SDHC_CLKCON:
84449ab747fSPaolo Bonzini         ret = s->clkcon | (s->timeoutcon << 16);
84549ab747fSPaolo Bonzini         break;
84649ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
84749ab747fSPaolo Bonzini         ret = s->norintsts | (s->errintsts << 16);
84849ab747fSPaolo Bonzini         break;
84949ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
85049ab747fSPaolo Bonzini         ret = s->norintstsen | (s->errintstsen << 16);
85149ab747fSPaolo Bonzini         break;
85249ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
85349ab747fSPaolo Bonzini         ret = s->norintsigen | (s->errintsigen << 16);
85449ab747fSPaolo Bonzini         break;
85549ab747fSPaolo Bonzini     case SDHC_ACMD12ERRSTS:
85649ab747fSPaolo Bonzini         ret = s->acmd12errsts;
85749ab747fSPaolo Bonzini         break;
858cd209421SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
8595efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->capareg;
8605efc9016SPhilippe Mathieu-Daudé         break;
8615efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
8625efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->capareg >> 32);
86349ab747fSPaolo Bonzini         break;
86449ab747fSPaolo Bonzini     case SDHC_MAXCURR:
8655efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)s->maxcurr;
8665efc9016SPhilippe Mathieu-Daudé         break;
8675efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
8685efc9016SPhilippe Mathieu-Daudé         ret = (uint32_t)(s->maxcurr >> 32);
86949ab747fSPaolo Bonzini         break;
87049ab747fSPaolo Bonzini     case SDHC_ADMAERR:
87149ab747fSPaolo Bonzini         ret =  s->admaerr;
87249ab747fSPaolo Bonzini         break;
87349ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
87449ab747fSPaolo Bonzini         ret = (uint32_t)s->admasysaddr;
87549ab747fSPaolo Bonzini         break;
87649ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
87749ab747fSPaolo Bonzini         ret = (uint32_t)(s->admasysaddr >> 32);
87849ab747fSPaolo Bonzini         break;
87949ab747fSPaolo Bonzini     case SDHC_SLOT_INT_STATUS:
880aceb5b06SPhilippe Mathieu-Daudé         ret = (s->version << 16) | sdhci_slotint(s);
88149ab747fSPaolo Bonzini         break;
88249ab747fSPaolo Bonzini     default:
88300b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC rd_%ub @0x%02" HWADDR_PRIx " "
88400b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset);
88549ab747fSPaolo Bonzini         break;
88649ab747fSPaolo Bonzini     }
88749ab747fSPaolo Bonzini 
88849ab747fSPaolo Bonzini     ret >>= (offset & 0x3) * 8;
88949ab747fSPaolo Bonzini     ret &= (1ULL << (size * 8)) - 1;
8908be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("rd", size << 3, offset, "->", ret, ret);
89149ab747fSPaolo Bonzini     return ret;
89249ab747fSPaolo Bonzini }
89349ab747fSPaolo Bonzini 
89449ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value)
89549ab747fSPaolo Bonzini {
89649ab747fSPaolo Bonzini     if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) {
89749ab747fSPaolo Bonzini         return;
89849ab747fSPaolo Bonzini     }
89949ab747fSPaolo Bonzini     s->blkgap = value & SDHC_STOP_AT_GAP_REQ;
90049ab747fSPaolo Bonzini 
90149ab747fSPaolo Bonzini     if ((value & SDHC_CONTINUE_REQ) && s->stopped_state &&
90249ab747fSPaolo Bonzini             (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) {
90349ab747fSPaolo Bonzini         if (s->stopped_state == sdhc_gap_read) {
90449ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ;
905d368ba43SKevin O'Connor             sdhci_read_block_from_card(s);
90649ab747fSPaolo Bonzini         } else {
90749ab747fSPaolo Bonzini             s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE;
908d368ba43SKevin O'Connor             sdhci_write_block_to_card(s);
90949ab747fSPaolo Bonzini         }
91049ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
91149ab747fSPaolo Bonzini     } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) {
91249ab747fSPaolo Bonzini         if (s->prnsts & SDHC_DOING_READ) {
91349ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_read;
91449ab747fSPaolo Bonzini         } else if (s->prnsts & SDHC_DOING_WRITE) {
91549ab747fSPaolo Bonzini             s->stopped_state = sdhc_gap_write;
91649ab747fSPaolo Bonzini         }
91749ab747fSPaolo Bonzini     }
91849ab747fSPaolo Bonzini }
91949ab747fSPaolo Bonzini 
92049ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value)
92149ab747fSPaolo Bonzini {
92249ab747fSPaolo Bonzini     switch (value) {
92349ab747fSPaolo Bonzini     case SDHC_RESET_ALL:
924d368ba43SKevin O'Connor         sdhci_reset(s);
92549ab747fSPaolo Bonzini         break;
92649ab747fSPaolo Bonzini     case SDHC_RESET_CMD:
92749ab747fSPaolo Bonzini         s->prnsts &= ~SDHC_CMD_INHIBIT;
92849ab747fSPaolo Bonzini         s->norintsts &= ~SDHC_NIS_CMDCMP;
92949ab747fSPaolo Bonzini         break;
93049ab747fSPaolo Bonzini     case SDHC_RESET_DATA:
93149ab747fSPaolo Bonzini         s->data_count = 0;
93249ab747fSPaolo Bonzini         s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE |
93349ab747fSPaolo Bonzini                 SDHC_DOING_READ | SDHC_DOING_WRITE |
93449ab747fSPaolo Bonzini                 SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE);
93549ab747fSPaolo Bonzini         s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ);
93649ab747fSPaolo Bonzini         s->stopped_state = sdhc_not_stopped;
93749ab747fSPaolo Bonzini         s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY |
93849ab747fSPaolo Bonzini                 SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP);
93949ab747fSPaolo Bonzini         break;
94049ab747fSPaolo Bonzini     }
94149ab747fSPaolo Bonzini }
94249ab747fSPaolo Bonzini 
94349ab747fSPaolo Bonzini static void
944d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
94549ab747fSPaolo Bonzini {
946d368ba43SKevin O'Connor     SDHCIState *s = (SDHCIState *)opaque;
94749ab747fSPaolo Bonzini     unsigned shift =  8 * (offset & 0x3);
94849ab747fSPaolo Bonzini     uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift);
949d368ba43SKevin O'Connor     uint32_t value = val;
95049ab747fSPaolo Bonzini     value <<= shift;
95149ab747fSPaolo Bonzini 
95249ab747fSPaolo Bonzini     switch (offset & ~0x3) {
95349ab747fSPaolo Bonzini     case SDHC_SYSAD:
95449ab747fSPaolo Bonzini         s->sdmasysad = (s->sdmasysad & mask) | value;
95549ab747fSPaolo Bonzini         MASKED_WRITE(s->sdmasysad, mask, value);
95649ab747fSPaolo Bonzini         /* Writing to last byte of sdmasysad might trigger transfer */
95749ab747fSPaolo Bonzini         if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt &&
95849ab747fSPaolo Bonzini                 s->blksize && SDHC_DMA_TYPE(s->hostctl) == SDHC_CTRL_SDMA) {
95945ba9f76SPrasad J Pandit             if (s->trnmod & SDHC_TRNS_MULTI) {
960d368ba43SKevin O'Connor                 sdhci_sdma_transfer_multi_blocks(s);
96145ba9f76SPrasad J Pandit             } else {
96245ba9f76SPrasad J Pandit                 sdhci_sdma_transfer_single_block(s);
96345ba9f76SPrasad J Pandit             }
96449ab747fSPaolo Bonzini         }
96549ab747fSPaolo Bonzini         break;
96649ab747fSPaolo Bonzini     case SDHC_BLKSIZE:
96749ab747fSPaolo Bonzini         if (!TRANSFERRING_DATA(s->prnsts)) {
96849ab747fSPaolo Bonzini             MASKED_WRITE(s->blksize, mask, value);
96949ab747fSPaolo Bonzini             MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16);
97049ab747fSPaolo Bonzini         }
9719201bb9aSAlistair Francis 
9729201bb9aSAlistair Francis         /* Limit block size to the maximum buffer size */
9739201bb9aSAlistair Francis         if (extract32(s->blksize, 0, 12) > s->buf_maxsz) {
9749201bb9aSAlistair Francis             qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than " \
9759201bb9aSAlistair Francis                           "the maximum buffer 0x%x", __func__, s->blksize,
9769201bb9aSAlistair Francis                           s->buf_maxsz);
9779201bb9aSAlistair Francis 
9789201bb9aSAlistair Francis             s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz);
9799201bb9aSAlistair Francis         }
9809201bb9aSAlistair Francis 
98149ab747fSPaolo Bonzini         break;
98249ab747fSPaolo Bonzini     case SDHC_ARGUMENT:
98349ab747fSPaolo Bonzini         MASKED_WRITE(s->argument, mask, value);
98449ab747fSPaolo Bonzini         break;
98549ab747fSPaolo Bonzini     case SDHC_TRNMOD:
98649ab747fSPaolo Bonzini         /* DMA can be enabled only if it is supported as indicated by
98749ab747fSPaolo Bonzini          * capabilities register */
98849ab747fSPaolo Bonzini         if (!(s->capareg & SDHC_CAN_DO_DMA)) {
98949ab747fSPaolo Bonzini             value &= ~SDHC_TRNS_DMA;
99049ab747fSPaolo Bonzini         }
99124bddf9dSPhilippe Mathieu-Daudé         MASKED_WRITE(s->trnmod, mask, value & SDHC_TRNMOD_MASK);
99249ab747fSPaolo Bonzini         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
99349ab747fSPaolo Bonzini 
99449ab747fSPaolo Bonzini         /* Writing to the upper byte of CMDREG triggers SD command generation */
995d368ba43SKevin O'Connor         if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) {
99649ab747fSPaolo Bonzini             break;
99749ab747fSPaolo Bonzini         }
99849ab747fSPaolo Bonzini 
999d368ba43SKevin O'Connor         sdhci_send_command(s);
100049ab747fSPaolo Bonzini         break;
100149ab747fSPaolo Bonzini     case  SDHC_BDATA:
100249ab747fSPaolo Bonzini         if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) {
1003d368ba43SKevin O'Connor             sdhci_write_dataport(s, value >> shift, size);
100449ab747fSPaolo Bonzini         }
100549ab747fSPaolo Bonzini         break;
100649ab747fSPaolo Bonzini     case SDHC_HOSTCTL:
100749ab747fSPaolo Bonzini         if (!(mask & 0xFF0000)) {
100849ab747fSPaolo Bonzini             sdhci_blkgap_write(s, value >> 16);
100949ab747fSPaolo Bonzini         }
101049ab747fSPaolo Bonzini         MASKED_WRITE(s->hostctl, mask, value);
101149ab747fSPaolo Bonzini         MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8);
101249ab747fSPaolo Bonzini         MASKED_WRITE(s->wakcon, mask >> 24, value >> 24);
101349ab747fSPaolo Bonzini         if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 ||
101449ab747fSPaolo Bonzini                 !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) {
101549ab747fSPaolo Bonzini             s->pwrcon &= ~SDHC_POWER_ON;
101649ab747fSPaolo Bonzini         }
101749ab747fSPaolo Bonzini         break;
101849ab747fSPaolo Bonzini     case SDHC_CLKCON:
101949ab747fSPaolo Bonzini         if (!(mask & 0xFF000000)) {
102049ab747fSPaolo Bonzini             sdhci_reset_write(s, value >> 24);
102149ab747fSPaolo Bonzini         }
102249ab747fSPaolo Bonzini         MASKED_WRITE(s->clkcon, mask, value);
102349ab747fSPaolo Bonzini         MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16);
102449ab747fSPaolo Bonzini         if (s->clkcon & SDHC_CLOCK_INT_EN) {
102549ab747fSPaolo Bonzini             s->clkcon |= SDHC_CLOCK_INT_STABLE;
102649ab747fSPaolo Bonzini         } else {
102749ab747fSPaolo Bonzini             s->clkcon &= ~SDHC_CLOCK_INT_STABLE;
102849ab747fSPaolo Bonzini         }
102949ab747fSPaolo Bonzini         break;
103049ab747fSPaolo Bonzini     case SDHC_NORINTSTS:
103149ab747fSPaolo Bonzini         if (s->norintstsen & SDHC_NISEN_CARDINT) {
103249ab747fSPaolo Bonzini             value &= ~SDHC_NIS_CARDINT;
103349ab747fSPaolo Bonzini         }
103449ab747fSPaolo Bonzini         s->norintsts &= mask | ~value;
103549ab747fSPaolo Bonzini         s->errintsts &= (mask >> 16) | ~(value >> 16);
103649ab747fSPaolo Bonzini         if (s->errintsts) {
103749ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
103849ab747fSPaolo Bonzini         } else {
103949ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
104049ab747fSPaolo Bonzini         }
104149ab747fSPaolo Bonzini         sdhci_update_irq(s);
104249ab747fSPaolo Bonzini         break;
104349ab747fSPaolo Bonzini     case SDHC_NORINTSTSEN:
104449ab747fSPaolo Bonzini         MASKED_WRITE(s->norintstsen, mask, value);
104549ab747fSPaolo Bonzini         MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16);
104649ab747fSPaolo Bonzini         s->norintsts &= s->norintstsen;
104749ab747fSPaolo Bonzini         s->errintsts &= s->errintstsen;
104849ab747fSPaolo Bonzini         if (s->errintsts) {
104949ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
105049ab747fSPaolo Bonzini         } else {
105149ab747fSPaolo Bonzini             s->norintsts &= ~SDHC_NIS_ERR;
105249ab747fSPaolo Bonzini         }
10530a7ac9f9SAndrew Baumann         /* Quirk for Raspberry Pi: pending card insert interrupt
10540a7ac9f9SAndrew Baumann          * appears when first enabled after power on */
10550a7ac9f9SAndrew Baumann         if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) {
10560a7ac9f9SAndrew Baumann             assert(s->pending_insert_quirk);
10570a7ac9f9SAndrew Baumann             s->norintsts |= SDHC_NIS_INSERT;
10580a7ac9f9SAndrew Baumann             s->pending_insert_state = false;
10590a7ac9f9SAndrew Baumann         }
106049ab747fSPaolo Bonzini         sdhci_update_irq(s);
106149ab747fSPaolo Bonzini         break;
106249ab747fSPaolo Bonzini     case SDHC_NORINTSIGEN:
106349ab747fSPaolo Bonzini         MASKED_WRITE(s->norintsigen, mask, value);
106449ab747fSPaolo Bonzini         MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16);
106549ab747fSPaolo Bonzini         sdhci_update_irq(s);
106649ab747fSPaolo Bonzini         break;
106749ab747fSPaolo Bonzini     case SDHC_ADMAERR:
106849ab747fSPaolo Bonzini         MASKED_WRITE(s->admaerr, mask, value);
106949ab747fSPaolo Bonzini         break;
107049ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR:
107149ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL |
107249ab747fSPaolo Bonzini                 (uint64_t)mask)) | (uint64_t)value;
107349ab747fSPaolo Bonzini         break;
107449ab747fSPaolo Bonzini     case SDHC_ADMASYSADDR + 4:
107549ab747fSPaolo Bonzini         s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL |
107649ab747fSPaolo Bonzini                 ((uint64_t)mask << 32))) | ((uint64_t)value << 32);
107749ab747fSPaolo Bonzini         break;
107849ab747fSPaolo Bonzini     case SDHC_FEAER:
107949ab747fSPaolo Bonzini         s->acmd12errsts |= value;
108049ab747fSPaolo Bonzini         s->errintsts |= (value >> 16) & s->errintstsen;
108149ab747fSPaolo Bonzini         if (s->acmd12errsts) {
108249ab747fSPaolo Bonzini             s->errintsts |= SDHC_EIS_CMD12ERR;
108349ab747fSPaolo Bonzini         }
108449ab747fSPaolo Bonzini         if (s->errintsts) {
108549ab747fSPaolo Bonzini             s->norintsts |= SDHC_NIS_ERR;
108649ab747fSPaolo Bonzini         }
108749ab747fSPaolo Bonzini         sdhci_update_irq(s);
108849ab747fSPaolo Bonzini         break;
10895d2c0464SAndrey Smirnov     case SDHC_ACMD12ERRSTS:
10905d2c0464SAndrey Smirnov         MASKED_WRITE(s->acmd12errsts, mask, value);
10915d2c0464SAndrey Smirnov         break;
10925efc9016SPhilippe Mathieu-Daudé 
10935efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB:
10945efc9016SPhilippe Mathieu-Daudé     case SDHC_CAPAB + 4:
10955efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR:
10965efc9016SPhilippe Mathieu-Daudé     case SDHC_MAXCURR + 4:
10975efc9016SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_GUEST_ERROR, "SDHC wr_%ub @0x%02" HWADDR_PRIx
10985efc9016SPhilippe Mathieu-Daudé                       " <- 0x%08x read-only\n", size, offset, value >> shift);
10995efc9016SPhilippe Mathieu-Daudé         break;
11005efc9016SPhilippe Mathieu-Daudé 
110149ab747fSPaolo Bonzini     default:
110200b004b3SPhilippe Mathieu-Daudé         qemu_log_mask(LOG_UNIMP, "SDHC wr_%ub @0x%02" HWADDR_PRIx " <- 0x%08x "
110300b004b3SPhilippe Mathieu-Daudé                       "not implemented\n", size, offset, value >> shift);
110449ab747fSPaolo Bonzini         break;
110549ab747fSPaolo Bonzini     }
11068be487d8SPhilippe Mathieu-Daudé     trace_sdhci_access("wr", size << 3, offset, "<-",
11078be487d8SPhilippe Mathieu-Daudé                        value >> shift, value >> shift);
110849ab747fSPaolo Bonzini }
110949ab747fSPaolo Bonzini 
111049ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = {
1111d368ba43SKevin O'Connor     .read = sdhci_read,
1112d368ba43SKevin O'Connor     .write = sdhci_write,
111349ab747fSPaolo Bonzini     .valid = {
111449ab747fSPaolo Bonzini         .min_access_size = 1,
111549ab747fSPaolo Bonzini         .max_access_size = 4,
111649ab747fSPaolo Bonzini         .unaligned = false
111749ab747fSPaolo Bonzini     },
111849ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
111949ab747fSPaolo Bonzini };
112049ab747fSPaolo Bonzini 
112149ab747fSPaolo Bonzini static inline unsigned int sdhci_get_fifolen(SDHCIState *s)
112249ab747fSPaolo Bonzini {
112349ab747fSPaolo Bonzini     switch (SDHC_CAPAB_BLOCKSIZE(s->capareg)) {
112449ab747fSPaolo Bonzini     case 0:
112549ab747fSPaolo Bonzini         return 512;
112649ab747fSPaolo Bonzini     case 1:
112749ab747fSPaolo Bonzini         return 1024;
112849ab747fSPaolo Bonzini     case 2:
112949ab747fSPaolo Bonzini         return 2048;
113049ab747fSPaolo Bonzini     default:
113149ab747fSPaolo Bonzini         hw_error("SDHC: unsupported value for maximum block size\n");
113249ab747fSPaolo Bonzini         return 0;
113349ab747fSPaolo Bonzini     }
113449ab747fSPaolo Bonzini }
113549ab747fSPaolo Bonzini 
1136aceb5b06SPhilippe Mathieu-Daudé static void sdhci_init_readonly_registers(SDHCIState *s, Error **errp)
1137aceb5b06SPhilippe Mathieu-Daudé {
1138aceb5b06SPhilippe Mathieu-Daudé     if (s->sd_spec_version != 2) {
1139aceb5b06SPhilippe Mathieu-Daudé         error_setg(errp, "Only Spec v2 is supported");
1140aceb5b06SPhilippe Mathieu-Daudé         return;
1141aceb5b06SPhilippe Mathieu-Daudé     }
1142aceb5b06SPhilippe Mathieu-Daudé     s->version = (SDHC_HCVER_VENDOR << 8) | (s->sd_spec_version - 1);
1143aceb5b06SPhilippe Mathieu-Daudé }
1144aceb5b06SPhilippe Mathieu-Daudé 
1145b635d98cSPhilippe Mathieu-Daudé /* --- qdev common --- */
1146b635d98cSPhilippe Mathieu-Daudé 
1147b635d98cSPhilippe Mathieu-Daudé #define DEFINE_SDHCI_COMMON_PROPERTIES(_state) \
1148aceb5b06SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT8("sd-spec-version", _state, sd_spec_version, 2), \
1149aceb5b06SPhilippe Mathieu-Daudé     \
1150aceb5b06SPhilippe Mathieu-Daudé     /* Capabilities registers provide information on supported
1151aceb5b06SPhilippe Mathieu-Daudé      * features of this specific host controller implementation */ \
11525efc9016SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("capareg", _state, capareg, SDHC_CAPAB_REG_DEFAULT), \
11535efc9016SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT64("maxcurr", _state, maxcurr, 0)
1154b635d98cSPhilippe Mathieu-Daudé 
115540bbc194SPeter Maydell static void sdhci_initfn(SDHCIState *s)
115649ab747fSPaolo Bonzini {
115740bbc194SPeter Maydell     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus),
115840bbc194SPeter Maydell                         TYPE_SDHCI_BUS, DEVICE(s), "sd-bus");
115949ab747fSPaolo Bonzini 
1160bc72ad67SAlex Bligh     s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s);
1161d368ba43SKevin O'Connor     s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s);
1162fd1e5c81SAndrey Smirnov 
1163fd1e5c81SAndrey Smirnov     s->io_ops = &sdhci_mmio_ops;
116449ab747fSPaolo Bonzini }
116549ab747fSPaolo Bonzini 
11667302dcd6SKevin O'Connor static void sdhci_uninitfn(SDHCIState *s)
116749ab747fSPaolo Bonzini {
1168bc72ad67SAlex Bligh     timer_del(s->insert_timer);
1169bc72ad67SAlex Bligh     timer_free(s->insert_timer);
1170bc72ad67SAlex Bligh     timer_del(s->transfer_timer);
1171bc72ad67SAlex Bligh     timer_free(s->transfer_timer);
117249ab747fSPaolo Bonzini 
117349ab747fSPaolo Bonzini     g_free(s->fifo_buffer);
117449ab747fSPaolo Bonzini     s->fifo_buffer = NULL;
117549ab747fSPaolo Bonzini }
117649ab747fSPaolo Bonzini 
117725367498SPhilippe Mathieu-Daudé static void sdhci_common_realize(SDHCIState *s, Error **errp)
117825367498SPhilippe Mathieu-Daudé {
1179aceb5b06SPhilippe Mathieu-Daudé     Error *local_err = NULL;
1180aceb5b06SPhilippe Mathieu-Daudé 
1181aceb5b06SPhilippe Mathieu-Daudé     sdhci_init_readonly_registers(s, &local_err);
1182aceb5b06SPhilippe Mathieu-Daudé     if (local_err) {
1183aceb5b06SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
1184aceb5b06SPhilippe Mathieu-Daudé         return;
1185aceb5b06SPhilippe Mathieu-Daudé     }
118625367498SPhilippe Mathieu-Daudé     s->buf_maxsz = sdhci_get_fifolen(s);
118725367498SPhilippe Mathieu-Daudé     s->fifo_buffer = g_malloc0(s->buf_maxsz);
118825367498SPhilippe Mathieu-Daudé 
118925367498SPhilippe Mathieu-Daudé     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci",
119025367498SPhilippe Mathieu-Daudé                           SDHC_REGISTERS_MAP_SIZE);
119125367498SPhilippe Mathieu-Daudé }
119225367498SPhilippe Mathieu-Daudé 
11938b7455c7SPhilippe Mathieu-Daudé static void sdhci_common_unrealize(SDHCIState *s, Error **errp)
11948b7455c7SPhilippe Mathieu-Daudé {
11958b7455c7SPhilippe Mathieu-Daudé     /* This function is expected to be called only once for each class:
11968b7455c7SPhilippe Mathieu-Daudé      * - SysBus:    via DeviceClass->unrealize(),
11978b7455c7SPhilippe Mathieu-Daudé      * - PCI:       via PCIDeviceClass->exit().
11988b7455c7SPhilippe Mathieu-Daudé      * However to avoid double-free and/or use-after-free we still nullify
11998b7455c7SPhilippe Mathieu-Daudé      * this variable (better safe than sorry!). */
12008b7455c7SPhilippe Mathieu-Daudé     g_free(s->fifo_buffer);
12018b7455c7SPhilippe Mathieu-Daudé     s->fifo_buffer = NULL;
12028b7455c7SPhilippe Mathieu-Daudé }
12038b7455c7SPhilippe Mathieu-Daudé 
12040a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque)
12050a7ac9f9SAndrew Baumann {
12060a7ac9f9SAndrew Baumann     SDHCIState *s = opaque;
12070a7ac9f9SAndrew Baumann 
12080a7ac9f9SAndrew Baumann     return s->pending_insert_state;
12090a7ac9f9SAndrew Baumann }
12100a7ac9f9SAndrew Baumann 
12110a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = {
12120a7ac9f9SAndrew Baumann     .name = "sdhci/pending-insert",
12130a7ac9f9SAndrew Baumann     .version_id = 1,
12140a7ac9f9SAndrew Baumann     .minimum_version_id = 1,
12150a7ac9f9SAndrew Baumann     .needed = sdhci_pending_insert_vmstate_needed,
12160a7ac9f9SAndrew Baumann     .fields = (VMStateField[]) {
12170a7ac9f9SAndrew Baumann         VMSTATE_BOOL(pending_insert_state, SDHCIState),
12180a7ac9f9SAndrew Baumann         VMSTATE_END_OF_LIST()
12190a7ac9f9SAndrew Baumann     },
12200a7ac9f9SAndrew Baumann };
12210a7ac9f9SAndrew Baumann 
122249ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = {
122349ab747fSPaolo Bonzini     .name = "sdhci",
122449ab747fSPaolo Bonzini     .version_id = 1,
122549ab747fSPaolo Bonzini     .minimum_version_id = 1,
122649ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
122749ab747fSPaolo Bonzini         VMSTATE_UINT32(sdmasysad, SDHCIState),
122849ab747fSPaolo Bonzini         VMSTATE_UINT16(blksize, SDHCIState),
122949ab747fSPaolo Bonzini         VMSTATE_UINT16(blkcnt, SDHCIState),
123049ab747fSPaolo Bonzini         VMSTATE_UINT32(argument, SDHCIState),
123149ab747fSPaolo Bonzini         VMSTATE_UINT16(trnmod, SDHCIState),
123249ab747fSPaolo Bonzini         VMSTATE_UINT16(cmdreg, SDHCIState),
123349ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4),
123449ab747fSPaolo Bonzini         VMSTATE_UINT32(prnsts, SDHCIState),
123549ab747fSPaolo Bonzini         VMSTATE_UINT8(hostctl, SDHCIState),
123649ab747fSPaolo Bonzini         VMSTATE_UINT8(pwrcon, SDHCIState),
123749ab747fSPaolo Bonzini         VMSTATE_UINT8(blkgap, SDHCIState),
123849ab747fSPaolo Bonzini         VMSTATE_UINT8(wakcon, SDHCIState),
123949ab747fSPaolo Bonzini         VMSTATE_UINT16(clkcon, SDHCIState),
124049ab747fSPaolo Bonzini         VMSTATE_UINT8(timeoutcon, SDHCIState),
124149ab747fSPaolo Bonzini         VMSTATE_UINT8(admaerr, SDHCIState),
124249ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsts, SDHCIState),
124349ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsts, SDHCIState),
124449ab747fSPaolo Bonzini         VMSTATE_UINT16(norintstsen, SDHCIState),
124549ab747fSPaolo Bonzini         VMSTATE_UINT16(errintstsen, SDHCIState),
124649ab747fSPaolo Bonzini         VMSTATE_UINT16(norintsigen, SDHCIState),
124749ab747fSPaolo Bonzini         VMSTATE_UINT16(errintsigen, SDHCIState),
124849ab747fSPaolo Bonzini         VMSTATE_UINT16(acmd12errsts, SDHCIState),
124949ab747fSPaolo Bonzini         VMSTATE_UINT16(data_count, SDHCIState),
125049ab747fSPaolo Bonzini         VMSTATE_UINT64(admasysaddr, SDHCIState),
125149ab747fSPaolo Bonzini         VMSTATE_UINT8(stopped_state, SDHCIState),
125259046ec2SHalil Pasic         VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz),
1253e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(insert_timer, SDHCIState),
1254e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(transfer_timer, SDHCIState),
125549ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
12560a7ac9f9SAndrew Baumann     },
12570a7ac9f9SAndrew Baumann     .subsections = (const VMStateDescription*[]) {
12580a7ac9f9SAndrew Baumann         &sdhci_pending_insert_vmstate,
12590a7ac9f9SAndrew Baumann         NULL
12600a7ac9f9SAndrew Baumann     },
126149ab747fSPaolo Bonzini };
126249ab747fSPaolo Bonzini 
12631c92c505SPhilippe Mathieu-Daudé static void sdhci_common_class_init(ObjectClass *klass, void *data)
12641c92c505SPhilippe Mathieu-Daudé {
12651c92c505SPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(klass);
12661c92c505SPhilippe Mathieu-Daudé 
12671c92c505SPhilippe Mathieu-Daudé     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
12681c92c505SPhilippe Mathieu-Daudé     dc->vmsd = &sdhci_vmstate;
12691c92c505SPhilippe Mathieu-Daudé     dc->reset = sdhci_poweron_reset;
12701c92c505SPhilippe Mathieu-Daudé }
12711c92c505SPhilippe Mathieu-Daudé 
1272b635d98cSPhilippe Mathieu-Daudé /* --- qdev PCI --- */
1273b635d98cSPhilippe Mathieu-Daudé 
12745ec911c3SKevin O'Connor static Property sdhci_pci_properties[] = {
1275b635d98cSPhilippe Mathieu-Daudé     DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState),
127649ab747fSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
127749ab747fSPaolo Bonzini };
127849ab747fSPaolo Bonzini 
12799af21dbeSMarkus Armbruster static void sdhci_pci_realize(PCIDevice *dev, Error **errp)
1280224d10ffSKevin O'Connor {
1281224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
1282ab958e38SPhilippe Mathieu-Daudé     Error *local_err = NULL;
128325367498SPhilippe Mathieu-Daudé 
128425367498SPhilippe Mathieu-Daudé     sdhci_initfn(s);
128525367498SPhilippe Mathieu-Daudé     sdhci_common_realize(s, errp);
1286ab958e38SPhilippe Mathieu-Daudé     if (local_err) {
1287ab958e38SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
128825367498SPhilippe Mathieu-Daudé         return;
128925367498SPhilippe Mathieu-Daudé     }
129025367498SPhilippe Mathieu-Daudé 
1291224d10ffSKevin O'Connor     dev->config[PCI_CLASS_PROG] = 0x01; /* Standard Host supported DMA */
1292224d10ffSKevin O'Connor     dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
1293224d10ffSKevin O'Connor     s->irq = pci_allocate_irq(dev);
1294dd55c485SPhilippe Mathieu-Daudé     s->dma_as = pci_get_address_space(dev);
1295dd55c485SPhilippe Mathieu-Daudé     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->iomem);
1296224d10ffSKevin O'Connor }
1297224d10ffSKevin O'Connor 
1298224d10ffSKevin O'Connor static void sdhci_pci_exit(PCIDevice *dev)
1299224d10ffSKevin O'Connor {
1300224d10ffSKevin O'Connor     SDHCIState *s = PCI_SDHCI(dev);
13018b7455c7SPhilippe Mathieu-Daudé 
13028b7455c7SPhilippe Mathieu-Daudé     sdhci_common_unrealize(s, &error_abort);
1303224d10ffSKevin O'Connor     sdhci_uninitfn(s);
1304224d10ffSKevin O'Connor }
1305224d10ffSKevin O'Connor 
1306224d10ffSKevin O'Connor static void sdhci_pci_class_init(ObjectClass *klass, void *data)
1307224d10ffSKevin O'Connor {
1308224d10ffSKevin O'Connor     DeviceClass *dc = DEVICE_CLASS(klass);
1309224d10ffSKevin O'Connor     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1310224d10ffSKevin O'Connor 
13119af21dbeSMarkus Armbruster     k->realize = sdhci_pci_realize;
1312224d10ffSKevin O'Connor     k->exit = sdhci_pci_exit;
1313224d10ffSKevin O'Connor     k->vendor_id = PCI_VENDOR_ID_REDHAT;
1314224d10ffSKevin O'Connor     k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI;
1315224d10ffSKevin O'Connor     k->class_id = PCI_CLASS_SYSTEM_SDHCI;
13165ec911c3SKevin O'Connor     dc->props = sdhci_pci_properties;
13171c92c505SPhilippe Mathieu-Daudé 
13181c92c505SPhilippe Mathieu-Daudé     sdhci_common_class_init(klass, data);
1319224d10ffSKevin O'Connor }
1320224d10ffSKevin O'Connor 
1321224d10ffSKevin O'Connor static const TypeInfo sdhci_pci_info = {
1322224d10ffSKevin O'Connor     .name = TYPE_PCI_SDHCI,
1323224d10ffSKevin O'Connor     .parent = TYPE_PCI_DEVICE,
1324224d10ffSKevin O'Connor     .instance_size = sizeof(SDHCIState),
1325224d10ffSKevin O'Connor     .class_init = sdhci_pci_class_init,
1326fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
1327fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1328fd3b02c8SEduardo Habkost         { },
1329fd3b02c8SEduardo Habkost     },
1330224d10ffSKevin O'Connor };
1331224d10ffSKevin O'Connor 
1332b635d98cSPhilippe Mathieu-Daudé /* --- qdev SysBus --- */
1333b635d98cSPhilippe Mathieu-Daudé 
13345ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = {
1335b635d98cSPhilippe Mathieu-Daudé     DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState),
13360a7ac9f9SAndrew Baumann     DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk,
13370a7ac9f9SAndrew Baumann                      false),
133860765b6cSPhilippe Mathieu-Daudé     DEFINE_PROP_LINK("dma", SDHCIState,
133960765b6cSPhilippe Mathieu-Daudé                      dma_mr, TYPE_MEMORY_REGION, MemoryRegion *),
13405ec911c3SKevin O'Connor     DEFINE_PROP_END_OF_LIST(),
13415ec911c3SKevin O'Connor };
13425ec911c3SKevin O'Connor 
13437302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj)
134449ab747fSPaolo Bonzini {
13457302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
13465ec911c3SKevin O'Connor 
134740bbc194SPeter Maydell     sdhci_initfn(s);
13487302dcd6SKevin O'Connor }
13497302dcd6SKevin O'Connor 
13507302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj)
13517302dcd6SKevin O'Connor {
13527302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(obj);
135360765b6cSPhilippe Mathieu-Daudé 
135460765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
135560765b6cSPhilippe Mathieu-Daudé         object_unparent(OBJECT(s->dma_mr));
135660765b6cSPhilippe Mathieu-Daudé     }
135760765b6cSPhilippe Mathieu-Daudé 
13587302dcd6SKevin O'Connor     sdhci_uninitfn(s);
13597302dcd6SKevin O'Connor }
13607302dcd6SKevin O'Connor 
13617302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp)
13627302dcd6SKevin O'Connor {
13637302dcd6SKevin O'Connor     SDHCIState *s = SYSBUS_SDHCI(dev);
136449ab747fSPaolo Bonzini     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1365ab958e38SPhilippe Mathieu-Daudé     Error *local_err = NULL;
136649ab747fSPaolo Bonzini 
136725367498SPhilippe Mathieu-Daudé     sdhci_common_realize(s, errp);
1368ab958e38SPhilippe Mathieu-Daudé     if (local_err) {
1369ab958e38SPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
137025367498SPhilippe Mathieu-Daudé         return;
137125367498SPhilippe Mathieu-Daudé     }
137225367498SPhilippe Mathieu-Daudé 
137360765b6cSPhilippe Mathieu-Daudé     if (s->dma_mr) {
137402e57e1cSPhilippe Mathieu-Daudé         s->dma_as = &s->sysbus_dma_as;
137560765b6cSPhilippe Mathieu-Daudé         address_space_init(s->dma_as, s->dma_mr, "sdhci-dma");
137660765b6cSPhilippe Mathieu-Daudé     } else {
137760765b6cSPhilippe Mathieu-Daudé         /* use system_memory() if property "dma" not set */
1378dd55c485SPhilippe Mathieu-Daudé         s->dma_as = &address_space_memory;
137960765b6cSPhilippe Mathieu-Daudé     }
1380dd55c485SPhilippe Mathieu-Daudé 
138149ab747fSPaolo Bonzini     sysbus_init_irq(sbd, &s->irq);
1382fd1e5c81SAndrey Smirnov 
1383fd1e5c81SAndrey Smirnov     memory_region_init_io(&s->iomem, OBJECT(s), s->io_ops, s, "sdhci",
1384fd1e5c81SAndrey Smirnov             SDHC_REGISTERS_MAP_SIZE);
1385fd1e5c81SAndrey Smirnov 
138649ab747fSPaolo Bonzini     sysbus_init_mmio(sbd, &s->iomem);
138749ab747fSPaolo Bonzini }
138849ab747fSPaolo Bonzini 
13898b7455c7SPhilippe Mathieu-Daudé static void sdhci_sysbus_unrealize(DeviceState *dev, Error **errp)
13908b7455c7SPhilippe Mathieu-Daudé {
13918b7455c7SPhilippe Mathieu-Daudé     SDHCIState *s = SYSBUS_SDHCI(dev);
13928b7455c7SPhilippe Mathieu-Daudé 
13938b7455c7SPhilippe Mathieu-Daudé     sdhci_common_unrealize(s, &error_abort);
139460765b6cSPhilippe Mathieu-Daudé 
139560765b6cSPhilippe Mathieu-Daudé      if (s->dma_mr) {
139660765b6cSPhilippe Mathieu-Daudé         address_space_destroy(s->dma_as);
139760765b6cSPhilippe Mathieu-Daudé     }
13988b7455c7SPhilippe Mathieu-Daudé }
13998b7455c7SPhilippe Mathieu-Daudé 
14007302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)
140149ab747fSPaolo Bonzini {
140249ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
140349ab747fSPaolo Bonzini 
14045ec911c3SKevin O'Connor     dc->props = sdhci_sysbus_properties;
14057302dcd6SKevin O'Connor     dc->realize = sdhci_sysbus_realize;
14068b7455c7SPhilippe Mathieu-Daudé     dc->unrealize = sdhci_sysbus_unrealize;
14071c92c505SPhilippe Mathieu-Daudé 
14081c92c505SPhilippe Mathieu-Daudé     sdhci_common_class_init(klass, data);
140949ab747fSPaolo Bonzini }
141049ab747fSPaolo Bonzini 
14117302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = {
14127302dcd6SKevin O'Connor     .name = TYPE_SYSBUS_SDHCI,
141349ab747fSPaolo Bonzini     .parent = TYPE_SYS_BUS_DEVICE,
141449ab747fSPaolo Bonzini     .instance_size = sizeof(SDHCIState),
14157302dcd6SKevin O'Connor     .instance_init = sdhci_sysbus_init,
14167302dcd6SKevin O'Connor     .instance_finalize = sdhci_sysbus_finalize,
14177302dcd6SKevin O'Connor     .class_init = sdhci_sysbus_class_init,
141849ab747fSPaolo Bonzini };
141949ab747fSPaolo Bonzini 
1420b635d98cSPhilippe Mathieu-Daudé /* --- qdev bus master --- */
1421b635d98cSPhilippe Mathieu-Daudé 
142240bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data)
142340bbc194SPeter Maydell {
142440bbc194SPeter Maydell     SDBusClass *sbc = SD_BUS_CLASS(klass);
142540bbc194SPeter Maydell 
142640bbc194SPeter Maydell     sbc->set_inserted = sdhci_set_inserted;
142740bbc194SPeter Maydell     sbc->set_readonly = sdhci_set_readonly;
142840bbc194SPeter Maydell }
142940bbc194SPeter Maydell 
143040bbc194SPeter Maydell static const TypeInfo sdhci_bus_info = {
143140bbc194SPeter Maydell     .name = TYPE_SDHCI_BUS,
143240bbc194SPeter Maydell     .parent = TYPE_SD_BUS,
143340bbc194SPeter Maydell     .instance_size = sizeof(SDBus),
143440bbc194SPeter Maydell     .class_init = sdhci_bus_class_init,
143540bbc194SPeter Maydell };
143640bbc194SPeter Maydell 
1437fd1e5c81SAndrey Smirnov static uint64_t usdhc_read(void *opaque, hwaddr offset, unsigned size)
1438fd1e5c81SAndrey Smirnov {
1439fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
1440fd1e5c81SAndrey Smirnov     uint32_t ret;
1441fd1e5c81SAndrey Smirnov     uint16_t hostctl;
1442fd1e5c81SAndrey Smirnov 
1443fd1e5c81SAndrey Smirnov     switch (offset) {
1444fd1e5c81SAndrey Smirnov     default:
1445fd1e5c81SAndrey Smirnov         return sdhci_read(opaque, offset, size);
1446fd1e5c81SAndrey Smirnov 
1447fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1448fd1e5c81SAndrey Smirnov         /*
1449fd1e5c81SAndrey Smirnov          * For a detailed explanation on the following bit
1450fd1e5c81SAndrey Smirnov          * manipulation code see comments in a similar part of
1451fd1e5c81SAndrey Smirnov          * usdhc_write()
1452fd1e5c81SAndrey Smirnov          */
1453fd1e5c81SAndrey Smirnov         hostctl = SDHC_DMA_TYPE(s->hostctl) << (8 - 3);
1454fd1e5c81SAndrey Smirnov 
1455fd1e5c81SAndrey Smirnov         if (s->hostctl & SDHC_CTRL_8BITBUS) {
1456fd1e5c81SAndrey Smirnov             hostctl |= ESDHC_CTRL_8BITBUS;
1457fd1e5c81SAndrey Smirnov         }
1458fd1e5c81SAndrey Smirnov 
1459fd1e5c81SAndrey Smirnov         if (s->hostctl & SDHC_CTRL_4BITBUS) {
1460fd1e5c81SAndrey Smirnov             hostctl |= ESDHC_CTRL_4BITBUS;
1461fd1e5c81SAndrey Smirnov         }
1462fd1e5c81SAndrey Smirnov 
1463fd1e5c81SAndrey Smirnov         ret  = hostctl;
1464fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->blkgap << 16;
1465fd1e5c81SAndrey Smirnov         ret |= (uint32_t)s->wakcon << 24;
1466fd1e5c81SAndrey Smirnov 
1467fd1e5c81SAndrey Smirnov         break;
1468fd1e5c81SAndrey Smirnov 
1469fd1e5c81SAndrey Smirnov     case ESDHC_DLL_CTRL:
1470fd1e5c81SAndrey Smirnov     case ESDHC_TUNE_CTRL_STATUS:
1471fd1e5c81SAndrey Smirnov     case ESDHC_UNDOCUMENTED_REG27:
1472fd1e5c81SAndrey Smirnov     case ESDHC_TUNING_CTRL:
1473fd1e5c81SAndrey Smirnov     case ESDHC_VENDOR_SPEC:
1474fd1e5c81SAndrey Smirnov     case ESDHC_MIX_CTRL:
1475fd1e5c81SAndrey Smirnov     case ESDHC_WTMK_LVL:
1476fd1e5c81SAndrey Smirnov         ret = 0;
1477fd1e5c81SAndrey Smirnov         break;
1478fd1e5c81SAndrey Smirnov     }
1479fd1e5c81SAndrey Smirnov 
1480fd1e5c81SAndrey Smirnov     return ret;
1481fd1e5c81SAndrey Smirnov }
1482fd1e5c81SAndrey Smirnov 
1483fd1e5c81SAndrey Smirnov static void
1484fd1e5c81SAndrey Smirnov usdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
1485fd1e5c81SAndrey Smirnov {
1486fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(opaque);
1487fd1e5c81SAndrey Smirnov     uint8_t hostctl;
1488fd1e5c81SAndrey Smirnov     uint32_t value = (uint32_t)val;
1489fd1e5c81SAndrey Smirnov 
1490fd1e5c81SAndrey Smirnov     switch (offset) {
1491fd1e5c81SAndrey Smirnov     case ESDHC_DLL_CTRL:
1492fd1e5c81SAndrey Smirnov     case ESDHC_TUNE_CTRL_STATUS:
1493fd1e5c81SAndrey Smirnov     case ESDHC_UNDOCUMENTED_REG27:
1494fd1e5c81SAndrey Smirnov     case ESDHC_TUNING_CTRL:
1495fd1e5c81SAndrey Smirnov     case ESDHC_WTMK_LVL:
1496fd1e5c81SAndrey Smirnov     case ESDHC_VENDOR_SPEC:
1497fd1e5c81SAndrey Smirnov         break;
1498fd1e5c81SAndrey Smirnov 
1499fd1e5c81SAndrey Smirnov     case SDHC_HOSTCTL:
1500fd1e5c81SAndrey Smirnov         /*
1501fd1e5c81SAndrey Smirnov          * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL)
1502fd1e5c81SAndrey Smirnov          *
1503fd1e5c81SAndrey Smirnov          *       7         6     5      4      3      2        1      0
1504fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1505fd1e5c81SAndrey Smirnov          * | Card      | Card   | Endian | DATA3     | Data     | Led     |
1506fd1e5c81SAndrey Smirnov          * | Detect    | Detect | Mode   | as Card   | Transfer | Control |
1507fd1e5c81SAndrey Smirnov          * | Signal    | Test   |        | Detection | Width    |         |
1508fd1e5c81SAndrey Smirnov          * | Selection | Level  |        | Pin       |          |         |
1509fd1e5c81SAndrey Smirnov          * |-----------+--------+--------+-----------+----------+---------|
1510fd1e5c81SAndrey Smirnov          *
1511fd1e5c81SAndrey Smirnov          * and 0x29
1512fd1e5c81SAndrey Smirnov          *
1513fd1e5c81SAndrey Smirnov          *  15      10 9    8
1514fd1e5c81SAndrey Smirnov          * |----------+------|
1515fd1e5c81SAndrey Smirnov          * | Reserved | DMA  |
1516fd1e5c81SAndrey Smirnov          * |          | Sel. |
1517fd1e5c81SAndrey Smirnov          * |          |      |
1518fd1e5c81SAndrey Smirnov          * |----------+------|
1519fd1e5c81SAndrey Smirnov          *
1520fd1e5c81SAndrey Smirnov          * and here's what SDCHI spec expects those offsets to be:
1521fd1e5c81SAndrey Smirnov          *
1522fd1e5c81SAndrey Smirnov          * 0x28 (Host Control Register)
1523fd1e5c81SAndrey Smirnov          *
1524fd1e5c81SAndrey Smirnov          *     7        6         5       4  3      2         1        0
1525fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1526fd1e5c81SAndrey Smirnov          * | Card   | Card   | Extended | DMA  | High   | Data     | LED     |
1527fd1e5c81SAndrey Smirnov          * | Detect | Detect | Data     | Sel. | Speed  | Transfer | Control |
1528fd1e5c81SAndrey Smirnov          * | Signal | Test   | Transfer |      | Enable | Width    |         |
1529fd1e5c81SAndrey Smirnov          * | Sel.   | Level  | Width    |      |        |          |         |
1530fd1e5c81SAndrey Smirnov          * |--------+--------+----------+------+--------+----------+---------|
1531fd1e5c81SAndrey Smirnov          *
1532fd1e5c81SAndrey Smirnov          * and 0x29 (Power Control Register)
1533fd1e5c81SAndrey Smirnov          *
1534fd1e5c81SAndrey Smirnov          * |----------------------------------|
1535fd1e5c81SAndrey Smirnov          * | Power Control Register           |
1536fd1e5c81SAndrey Smirnov          * |                                  |
1537fd1e5c81SAndrey Smirnov          * | Description omitted,             |
1538fd1e5c81SAndrey Smirnov          * | since it has no analog in ESDHCI |
1539fd1e5c81SAndrey Smirnov          * |                                  |
1540fd1e5c81SAndrey Smirnov          * |----------------------------------|
1541fd1e5c81SAndrey Smirnov          *
1542fd1e5c81SAndrey Smirnov          * Since offsets 0x2A and 0x2B should be compatible between
1543fd1e5c81SAndrey Smirnov          * both IP specs we only need to reconcile least 16-bit of the
1544fd1e5c81SAndrey Smirnov          * word we've been given.
1545fd1e5c81SAndrey Smirnov          */
1546fd1e5c81SAndrey Smirnov 
1547fd1e5c81SAndrey Smirnov         /*
1548fd1e5c81SAndrey Smirnov          * First, save bits 7 6 and 0 since they are identical
1549fd1e5c81SAndrey Smirnov          */
1550fd1e5c81SAndrey Smirnov         hostctl = value & (SDHC_CTRL_LED |
1551fd1e5c81SAndrey Smirnov                            SDHC_CTRL_CDTEST_INS |
1552fd1e5c81SAndrey Smirnov                            SDHC_CTRL_CDTEST_EN);
1553fd1e5c81SAndrey Smirnov         /*
1554fd1e5c81SAndrey Smirnov          * Second, split "Data Transfer Width" from bits 2 and 1 in to
1555fd1e5c81SAndrey Smirnov          * bits 5 and 1
1556fd1e5c81SAndrey Smirnov          */
1557fd1e5c81SAndrey Smirnov         if (value & ESDHC_CTRL_8BITBUS) {
1558fd1e5c81SAndrey Smirnov             hostctl |= SDHC_CTRL_8BITBUS;
1559fd1e5c81SAndrey Smirnov         }
1560fd1e5c81SAndrey Smirnov 
1561fd1e5c81SAndrey Smirnov         if (value & ESDHC_CTRL_4BITBUS) {
1562fd1e5c81SAndrey Smirnov             hostctl |= ESDHC_CTRL_4BITBUS;
1563fd1e5c81SAndrey Smirnov         }
1564fd1e5c81SAndrey Smirnov 
1565fd1e5c81SAndrey Smirnov         /*
1566fd1e5c81SAndrey Smirnov          * Third, move DMA select from bits 9 and 8 to bits 4 and 3
1567fd1e5c81SAndrey Smirnov          */
1568fd1e5c81SAndrey Smirnov         hostctl |= SDHC_DMA_TYPE(value >> (8 - 3));
1569fd1e5c81SAndrey Smirnov 
1570fd1e5c81SAndrey Smirnov         /*
1571fd1e5c81SAndrey Smirnov          * Now place the corrected value into low 16-bit of the value
1572fd1e5c81SAndrey Smirnov          * we are going to give standard SDHCI write function
1573fd1e5c81SAndrey Smirnov          *
1574fd1e5c81SAndrey Smirnov          * NOTE: This transformation should be the inverse of what can
1575fd1e5c81SAndrey Smirnov          * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux
1576fd1e5c81SAndrey Smirnov          * kernel
1577fd1e5c81SAndrey Smirnov          */
1578fd1e5c81SAndrey Smirnov         value &= ~UINT16_MAX;
1579fd1e5c81SAndrey Smirnov         value |= hostctl;
1580fd1e5c81SAndrey Smirnov         value |= (uint16_t)s->pwrcon << 8;
1581fd1e5c81SAndrey Smirnov 
1582fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, value, size);
1583fd1e5c81SAndrey Smirnov         break;
1584fd1e5c81SAndrey Smirnov 
1585fd1e5c81SAndrey Smirnov     case ESDHC_MIX_CTRL:
1586fd1e5c81SAndrey Smirnov         /*
1587fd1e5c81SAndrey Smirnov          * So, when SD/MMC stack in Linux tries to write to "Transfer
1588fd1e5c81SAndrey Smirnov          * Mode Register", ESDHC i.MX quirk code will translate it
1589fd1e5c81SAndrey Smirnov          * into a write to ESDHC_MIX_CTRL, so we do the opposite in
1590fd1e5c81SAndrey Smirnov          * order to get where we started
1591fd1e5c81SAndrey Smirnov          *
1592fd1e5c81SAndrey Smirnov          * Note that Auto CMD23 Enable bit is located in a wrong place
1593fd1e5c81SAndrey Smirnov          * on i.MX, but since it is not used by QEMU we do not care.
1594fd1e5c81SAndrey Smirnov          *
1595fd1e5c81SAndrey Smirnov          * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
1596fd1e5c81SAndrey Smirnov          * here becuase it will result in a call to
1597fd1e5c81SAndrey Smirnov          * sdhci_send_command(s) which we don't want.
1598fd1e5c81SAndrey Smirnov          *
1599fd1e5c81SAndrey Smirnov          */
1600fd1e5c81SAndrey Smirnov         s->trnmod = value & UINT16_MAX;
1601fd1e5c81SAndrey Smirnov         break;
1602fd1e5c81SAndrey Smirnov     case SDHC_TRNMOD:
1603fd1e5c81SAndrey Smirnov         /*
1604fd1e5c81SAndrey Smirnov          * Similar to above, but this time a write to "Command
1605fd1e5c81SAndrey Smirnov          * Register" will be translated into a 4-byte write to
1606fd1e5c81SAndrey Smirnov          * "Transfer Mode register" where lower 16-bit of value would
1607fd1e5c81SAndrey Smirnov          * be set to zero. So what we do is fill those bits with
1608fd1e5c81SAndrey Smirnov          * cached value from s->trnmod and let the SDHCI
1609fd1e5c81SAndrey Smirnov          * infrastructure handle the rest
1610fd1e5c81SAndrey Smirnov          */
1611fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val | s->trnmod, size);
1612fd1e5c81SAndrey Smirnov         break;
1613fd1e5c81SAndrey Smirnov     case SDHC_BLKSIZE:
1614fd1e5c81SAndrey Smirnov         /*
1615fd1e5c81SAndrey Smirnov          * ESDHCI does not implement "Host SDMA Buffer Boundary", and
1616fd1e5c81SAndrey Smirnov          * Linux driver will try to zero this field out which will
1617fd1e5c81SAndrey Smirnov          * break the rest of SDHCI emulation.
1618fd1e5c81SAndrey Smirnov          *
1619fd1e5c81SAndrey Smirnov          * Linux defaults to maximum possible setting (512K boundary)
1620fd1e5c81SAndrey Smirnov          * and it seems to be the only option that i.MX IP implements,
1621fd1e5c81SAndrey Smirnov          * so we artificially set it to that value.
1622fd1e5c81SAndrey Smirnov          */
1623fd1e5c81SAndrey Smirnov         val |= 0x7 << 12;
1624fd1e5c81SAndrey Smirnov         /* FALLTHROUGH */
1625fd1e5c81SAndrey Smirnov     default:
1626fd1e5c81SAndrey Smirnov         sdhci_write(opaque, offset, val, size);
1627fd1e5c81SAndrey Smirnov         break;
1628fd1e5c81SAndrey Smirnov     }
1629fd1e5c81SAndrey Smirnov }
1630fd1e5c81SAndrey Smirnov 
1631fd1e5c81SAndrey Smirnov 
1632fd1e5c81SAndrey Smirnov static const MemoryRegionOps usdhc_mmio_ops = {
1633fd1e5c81SAndrey Smirnov     .read = usdhc_read,
1634fd1e5c81SAndrey Smirnov     .write = usdhc_write,
1635fd1e5c81SAndrey Smirnov     .valid = {
1636fd1e5c81SAndrey Smirnov         .min_access_size = 1,
1637fd1e5c81SAndrey Smirnov         .max_access_size = 4,
1638fd1e5c81SAndrey Smirnov         .unaligned = false
1639fd1e5c81SAndrey Smirnov     },
1640fd1e5c81SAndrey Smirnov     .endianness = DEVICE_LITTLE_ENDIAN,
1641fd1e5c81SAndrey Smirnov };
1642fd1e5c81SAndrey Smirnov 
1643fd1e5c81SAndrey Smirnov static void imx_usdhc_init(Object *obj)
1644fd1e5c81SAndrey Smirnov {
1645fd1e5c81SAndrey Smirnov     SDHCIState *s = SYSBUS_SDHCI(obj);
1646fd1e5c81SAndrey Smirnov 
1647fd1e5c81SAndrey Smirnov     s->io_ops = &usdhc_mmio_ops;
1648fd1e5c81SAndrey Smirnov     s->quirks = SDHCI_QUIRK_NO_BUSY_IRQ;
1649fd1e5c81SAndrey Smirnov }
1650fd1e5c81SAndrey Smirnov 
1651fd1e5c81SAndrey Smirnov static const TypeInfo imx_usdhc_info = {
1652fd1e5c81SAndrey Smirnov     .name = TYPE_IMX_USDHC,
1653fd1e5c81SAndrey Smirnov     .parent = TYPE_SYSBUS_SDHCI,
1654fd1e5c81SAndrey Smirnov     .instance_init = imx_usdhc_init,
1655fd1e5c81SAndrey Smirnov };
1656fd1e5c81SAndrey Smirnov 
165749ab747fSPaolo Bonzini static void sdhci_register_types(void)
165849ab747fSPaolo Bonzini {
1659224d10ffSKevin O'Connor     type_register_static(&sdhci_pci_info);
16607302dcd6SKevin O'Connor     type_register_static(&sdhci_sysbus_info);
166140bbc194SPeter Maydell     type_register_static(&sdhci_bus_info);
1662fd1e5c81SAndrey Smirnov     type_register_static(&imx_usdhc_info);
166349ab747fSPaolo Bonzini }
166449ab747fSPaolo Bonzini 
166549ab747fSPaolo Bonzini type_init(sdhci_register_types)
1666