149ab747fSPaolo Bonzini /* 249ab747fSPaolo Bonzini * SD Association Host Standard Specification v2.0 controller emulation 349ab747fSPaolo Bonzini * 449ab747fSPaolo Bonzini * Copyright (c) 2011 Samsung Electronics Co., Ltd. 549ab747fSPaolo Bonzini * Mitsyanko Igor <i.mitsyanko@samsung.com> 649ab747fSPaolo Bonzini * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com> 749ab747fSPaolo Bonzini * 849ab747fSPaolo Bonzini * Based on MMC controller for Samsung S5PC1xx-based board emulation 949ab747fSPaolo Bonzini * by Alexey Merkulov and Vladimir Monakhov. 1049ab747fSPaolo Bonzini * 1149ab747fSPaolo Bonzini * This program is free software; you can redistribute it and/or modify it 1249ab747fSPaolo Bonzini * under the terms of the GNU General Public License as published by the 1349ab747fSPaolo Bonzini * Free Software Foundation; either version 2 of the License, or (at your 1449ab747fSPaolo Bonzini * option) any later version. 1549ab747fSPaolo Bonzini * 1649ab747fSPaolo Bonzini * This program is distributed in the hope that it will be useful, 1749ab747fSPaolo Bonzini * but WITHOUT ANY WARRANTY; without even the implied warranty of 1849ab747fSPaolo Bonzini * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1949ab747fSPaolo Bonzini * See the GNU General Public License for more details. 2049ab747fSPaolo Bonzini * 2149ab747fSPaolo Bonzini * You should have received a copy of the GNU General Public License along 2249ab747fSPaolo Bonzini * with this program; if not, see <http://www.gnu.org/licenses/>. 2349ab747fSPaolo Bonzini */ 2449ab747fSPaolo Bonzini 2549ab747fSPaolo Bonzini #include "hw/hw.h" 26fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h" 2749ab747fSPaolo Bonzini #include "sysemu/blockdev.h" 2849ab747fSPaolo Bonzini #include "sysemu/dma.h" 2949ab747fSPaolo Bonzini #include "qemu/timer.h" 3049ab747fSPaolo Bonzini #include "qemu/bitops.h" 3149ab747fSPaolo Bonzini 3247b43a1fSPaolo Bonzini #include "sdhci.h" 3349ab747fSPaolo Bonzini 3449ab747fSPaolo Bonzini /* host controller debug messages */ 3549ab747fSPaolo Bonzini #ifndef SDHC_DEBUG 3649ab747fSPaolo Bonzini #define SDHC_DEBUG 0 3749ab747fSPaolo Bonzini #endif 3849ab747fSPaolo Bonzini 3949ab747fSPaolo Bonzini #if SDHC_DEBUG == 0 4049ab747fSPaolo Bonzini #define DPRINT_L1(fmt, args...) do { } while (0) 4149ab747fSPaolo Bonzini #define DPRINT_L2(fmt, args...) do { } while (0) 4249ab747fSPaolo Bonzini #define ERRPRINT(fmt, args...) do { } while (0) 4349ab747fSPaolo Bonzini #elif SDHC_DEBUG == 1 4449ab747fSPaolo Bonzini #define DPRINT_L1(fmt, args...) \ 4549ab747fSPaolo Bonzini do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0) 4649ab747fSPaolo Bonzini #define DPRINT_L2(fmt, args...) do { } while (0) 4749ab747fSPaolo Bonzini #define ERRPRINT(fmt, args...) \ 4849ab747fSPaolo Bonzini do {fprintf(stderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0) 4949ab747fSPaolo Bonzini #else 5049ab747fSPaolo Bonzini #define DPRINT_L1(fmt, args...) \ 5149ab747fSPaolo Bonzini do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0) 5249ab747fSPaolo Bonzini #define DPRINT_L2(fmt, args...) \ 5349ab747fSPaolo Bonzini do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0) 5449ab747fSPaolo Bonzini #define ERRPRINT(fmt, args...) \ 5549ab747fSPaolo Bonzini do {fprintf(stderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0) 5649ab747fSPaolo Bonzini #endif 5749ab747fSPaolo Bonzini 5849ab747fSPaolo Bonzini /* Default SD/MMC host controller features information, which will be 5949ab747fSPaolo Bonzini * presented in CAPABILITIES register of generic SD host controller at reset. 6049ab747fSPaolo Bonzini * If not stated otherwise: 6149ab747fSPaolo Bonzini * 0 - not supported, 1 - supported, other - prohibited. 6249ab747fSPaolo Bonzini */ 6349ab747fSPaolo Bonzini #define SDHC_CAPAB_64BITBUS 0ul /* 64-bit System Bus Support */ 6449ab747fSPaolo Bonzini #define SDHC_CAPAB_18V 1ul /* Voltage support 1.8v */ 6549ab747fSPaolo Bonzini #define SDHC_CAPAB_30V 0ul /* Voltage support 3.0v */ 6649ab747fSPaolo Bonzini #define SDHC_CAPAB_33V 1ul /* Voltage support 3.3v */ 6749ab747fSPaolo Bonzini #define SDHC_CAPAB_SUSPRESUME 0ul /* Suspend/resume support */ 6849ab747fSPaolo Bonzini #define SDHC_CAPAB_SDMA 1ul /* SDMA support */ 6949ab747fSPaolo Bonzini #define SDHC_CAPAB_HIGHSPEED 1ul /* High speed support */ 7049ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA1 1ul /* ADMA1 support */ 7149ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA2 1ul /* ADMA2 support */ 7249ab747fSPaolo Bonzini /* Maximum host controller R/W buffers size 7349ab747fSPaolo Bonzini * Possible values: 512, 1024, 2048 bytes */ 7449ab747fSPaolo Bonzini #define SDHC_CAPAB_MAXBLOCKLENGTH 512ul 7549ab747fSPaolo Bonzini /* Maximum clock frequency for SDclock in MHz 7649ab747fSPaolo Bonzini * value in range 10-63 MHz, 0 - not defined */ 77c7ff8daaSKevin O'Connor #define SDHC_CAPAB_BASECLKFREQ 52ul 7849ab747fSPaolo Bonzini #define SDHC_CAPAB_TOUNIT 1ul /* Timeout clock unit 0 - kHz, 1 - MHz */ 7949ab747fSPaolo Bonzini /* Timeout clock frequency 1-63, 0 - not defined */ 80c7ff8daaSKevin O'Connor #define SDHC_CAPAB_TOCLKFREQ 52ul 8149ab747fSPaolo Bonzini 8249ab747fSPaolo Bonzini /* Now check all parameters and calculate CAPABILITIES REGISTER value */ 8349ab747fSPaolo Bonzini #if SDHC_CAPAB_64BITBUS > 1 || SDHC_CAPAB_18V > 1 || SDHC_CAPAB_30V > 1 || \ 8449ab747fSPaolo Bonzini SDHC_CAPAB_33V > 1 || SDHC_CAPAB_SUSPRESUME > 1 || SDHC_CAPAB_SDMA > 1 || \ 8549ab747fSPaolo Bonzini SDHC_CAPAB_HIGHSPEED > 1 || SDHC_CAPAB_ADMA2 > 1 || SDHC_CAPAB_ADMA1 > 1 ||\ 8649ab747fSPaolo Bonzini SDHC_CAPAB_TOUNIT > 1 8749ab747fSPaolo Bonzini #error Capabilities features can have value 0 or 1 only! 8849ab747fSPaolo Bonzini #endif 8949ab747fSPaolo Bonzini 9049ab747fSPaolo Bonzini #if SDHC_CAPAB_MAXBLOCKLENGTH == 512 9149ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 0ul 9249ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 1024 9349ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 1ul 9449ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 2048 9549ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 2ul 9649ab747fSPaolo Bonzini #else 9749ab747fSPaolo Bonzini #error Max host controller block size can have value 512, 1024 or 2048 only! 9849ab747fSPaolo Bonzini #endif 9949ab747fSPaolo Bonzini 10049ab747fSPaolo Bonzini #if (SDHC_CAPAB_BASECLKFREQ > 0 && SDHC_CAPAB_BASECLKFREQ < 10) || \ 10149ab747fSPaolo Bonzini SDHC_CAPAB_BASECLKFREQ > 63 10249ab747fSPaolo Bonzini #error SDclock frequency can have value in range 0, 10-63 only! 10349ab747fSPaolo Bonzini #endif 10449ab747fSPaolo Bonzini 10549ab747fSPaolo Bonzini #if SDHC_CAPAB_TOCLKFREQ > 63 10649ab747fSPaolo Bonzini #error Timeout clock frequency can have value in range 0-63 only! 10749ab747fSPaolo Bonzini #endif 10849ab747fSPaolo Bonzini 10949ab747fSPaolo Bonzini #define SDHC_CAPAB_REG_DEFAULT \ 11049ab747fSPaolo Bonzini ((SDHC_CAPAB_64BITBUS << 28) | (SDHC_CAPAB_18V << 26) | \ 11149ab747fSPaolo Bonzini (SDHC_CAPAB_30V << 25) | (SDHC_CAPAB_33V << 24) | \ 11249ab747fSPaolo Bonzini (SDHC_CAPAB_SUSPRESUME << 23) | (SDHC_CAPAB_SDMA << 22) | \ 11349ab747fSPaolo Bonzini (SDHC_CAPAB_HIGHSPEED << 21) | (SDHC_CAPAB_ADMA1 << 20) | \ 11449ab747fSPaolo Bonzini (SDHC_CAPAB_ADMA2 << 19) | (MAX_BLOCK_LENGTH << 16) | \ 11549ab747fSPaolo Bonzini (SDHC_CAPAB_BASECLKFREQ << 8) | (SDHC_CAPAB_TOUNIT << 7) | \ 11649ab747fSPaolo Bonzini (SDHC_CAPAB_TOCLKFREQ)) 11749ab747fSPaolo Bonzini 11849ab747fSPaolo Bonzini #define MASKED_WRITE(reg, mask, val) (reg = (reg & (mask)) | (val)) 11949ab747fSPaolo Bonzini 12049ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s) 12149ab747fSPaolo Bonzini { 12249ab747fSPaolo Bonzini return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) || 12349ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) || 12449ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV)); 12549ab747fSPaolo Bonzini } 12649ab747fSPaolo Bonzini 12749ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s) 12849ab747fSPaolo Bonzini { 12949ab747fSPaolo Bonzini qemu_set_irq(s->irq, sdhci_slotint(s)); 13049ab747fSPaolo Bonzini } 13149ab747fSPaolo Bonzini 13249ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque) 13349ab747fSPaolo Bonzini { 13449ab747fSPaolo Bonzini SDHCIState *s = (SDHCIState *)opaque; 13549ab747fSPaolo Bonzini 13649ab747fSPaolo Bonzini if (s->norintsts & SDHC_NIS_REMOVE) { 137bc72ad67SAlex Bligh timer_mod(s->insert_timer, 138bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 13949ab747fSPaolo Bonzini } else { 14049ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 14149ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 14249ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 14349ab747fSPaolo Bonzini } 14449ab747fSPaolo Bonzini sdhci_update_irq(s); 14549ab747fSPaolo Bonzini } 14649ab747fSPaolo Bonzini } 14749ab747fSPaolo Bonzini 14849ab747fSPaolo Bonzini static void sdhci_insert_eject_cb(void *opaque, int irq, int level) 14949ab747fSPaolo Bonzini { 15049ab747fSPaolo Bonzini SDHCIState *s = (SDHCIState *)opaque; 15149ab747fSPaolo Bonzini DPRINT_L1("Card state changed: %s!\n", level ? "insert" : "eject"); 15249ab747fSPaolo Bonzini 15349ab747fSPaolo Bonzini if ((s->norintsts & SDHC_NIS_REMOVE) && level) { 15449ab747fSPaolo Bonzini /* Give target some time to notice card ejection */ 155bc72ad67SAlex Bligh timer_mod(s->insert_timer, 156bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 15749ab747fSPaolo Bonzini } else { 15849ab747fSPaolo Bonzini if (level) { 15949ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 16049ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 16149ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 16249ab747fSPaolo Bonzini } 16349ab747fSPaolo Bonzini } else { 16449ab747fSPaolo Bonzini s->prnsts = 0x1fa0000; 16549ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 16649ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_SDCLK_EN; 16749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_REMOVE) { 16849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_REMOVE; 16949ab747fSPaolo Bonzini } 17049ab747fSPaolo Bonzini } 17149ab747fSPaolo Bonzini sdhci_update_irq(s); 17249ab747fSPaolo Bonzini } 17349ab747fSPaolo Bonzini } 17449ab747fSPaolo Bonzini 17549ab747fSPaolo Bonzini static void sdhci_card_readonly_cb(void *opaque, int irq, int level) 17649ab747fSPaolo Bonzini { 17749ab747fSPaolo Bonzini SDHCIState *s = (SDHCIState *)opaque; 17849ab747fSPaolo Bonzini 17949ab747fSPaolo Bonzini if (level) { 18049ab747fSPaolo Bonzini s->prnsts &= ~SDHC_WRITE_PROTECT; 18149ab747fSPaolo Bonzini } else { 18249ab747fSPaolo Bonzini /* Write enabled */ 18349ab747fSPaolo Bonzini s->prnsts |= SDHC_WRITE_PROTECT; 18449ab747fSPaolo Bonzini } 18549ab747fSPaolo Bonzini } 18649ab747fSPaolo Bonzini 18749ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s) 18849ab747fSPaolo Bonzini { 189bc72ad67SAlex Bligh timer_del(s->insert_timer); 190bc72ad67SAlex Bligh timer_del(s->transfer_timer); 19149ab747fSPaolo Bonzini /* Set all registers to 0. Capabilities registers are not cleared 19249ab747fSPaolo Bonzini * and assumed to always preserve their value, given to them during 19349ab747fSPaolo Bonzini * initialization */ 19449ab747fSPaolo Bonzini memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad); 19549ab747fSPaolo Bonzini 19649ab747fSPaolo Bonzini sd_set_cb(s->card, s->ro_cb, s->eject_cb); 19749ab747fSPaolo Bonzini s->data_count = 0; 19849ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 19949ab747fSPaolo Bonzini } 20049ab747fSPaolo Bonzini 201d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque); 20249ab747fSPaolo Bonzini 20349ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s) 20449ab747fSPaolo Bonzini { 20549ab747fSPaolo Bonzini SDRequest request; 20649ab747fSPaolo Bonzini uint8_t response[16]; 20749ab747fSPaolo Bonzini int rlen; 20849ab747fSPaolo Bonzini 20949ab747fSPaolo Bonzini s->errintsts = 0; 21049ab747fSPaolo Bonzini s->acmd12errsts = 0; 21149ab747fSPaolo Bonzini request.cmd = s->cmdreg >> 8; 21249ab747fSPaolo Bonzini request.arg = s->argument; 21349ab747fSPaolo Bonzini DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request.cmd, request.arg); 21449ab747fSPaolo Bonzini rlen = sd_do_command(s->card, &request, response); 21549ab747fSPaolo Bonzini 21649ab747fSPaolo Bonzini if (s->cmdreg & SDHC_CMD_RESPONSE) { 21749ab747fSPaolo Bonzini if (rlen == 4) { 21849ab747fSPaolo Bonzini s->rspreg[0] = (response[0] << 24) | (response[1] << 16) | 21949ab747fSPaolo Bonzini (response[2] << 8) | response[3]; 22049ab747fSPaolo Bonzini s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0; 22149ab747fSPaolo Bonzini DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s->rspreg[0]); 22249ab747fSPaolo Bonzini } else if (rlen == 16) { 22349ab747fSPaolo Bonzini s->rspreg[0] = (response[11] << 24) | (response[12] << 16) | 22449ab747fSPaolo Bonzini (response[13] << 8) | response[14]; 22549ab747fSPaolo Bonzini s->rspreg[1] = (response[7] << 24) | (response[8] << 16) | 22649ab747fSPaolo Bonzini (response[9] << 8) | response[10]; 22749ab747fSPaolo Bonzini s->rspreg[2] = (response[3] << 24) | (response[4] << 16) | 22849ab747fSPaolo Bonzini (response[5] << 8) | response[6]; 22949ab747fSPaolo Bonzini s->rspreg[3] = (response[0] << 16) | (response[1] << 8) | 23049ab747fSPaolo Bonzini response[2]; 23149ab747fSPaolo Bonzini DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.." 23249ab747fSPaolo Bonzini "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n", 23349ab747fSPaolo Bonzini s->rspreg[3], s->rspreg[2], s->rspreg[1], s->rspreg[0]); 23449ab747fSPaolo Bonzini } else { 23549ab747fSPaolo Bonzini ERRPRINT("Timeout waiting for command response\n"); 23649ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) { 23749ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMDTIMEOUT; 23849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 23949ab747fSPaolo Bonzini } 24049ab747fSPaolo Bonzini } 24149ab747fSPaolo Bonzini 24249ab747fSPaolo Bonzini if ((s->norintstsen & SDHC_NISEN_TRSCMP) && 24349ab747fSPaolo Bonzini (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) { 24449ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 24549ab747fSPaolo Bonzini } 24649ab747fSPaolo Bonzini } else if (rlen != 0 && (s->errintstsen & SDHC_EISEN_CMDIDX)) { 24749ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMDIDX; 24849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 24949ab747fSPaolo Bonzini } 25049ab747fSPaolo Bonzini 25149ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CMDCMP) { 25249ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_CMDCMP; 25349ab747fSPaolo Bonzini } 25449ab747fSPaolo Bonzini 25549ab747fSPaolo Bonzini sdhci_update_irq(s); 25649ab747fSPaolo Bonzini 25749ab747fSPaolo Bonzini if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) { 258656f416cSPeter Crosthwaite s->data_count = 0; 259d368ba43SKevin O'Connor sdhci_data_transfer(s); 26049ab747fSPaolo Bonzini } 26149ab747fSPaolo Bonzini } 26249ab747fSPaolo Bonzini 26349ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s) 26449ab747fSPaolo Bonzini { 26549ab747fSPaolo Bonzini /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */ 26649ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) { 26749ab747fSPaolo Bonzini SDRequest request; 26849ab747fSPaolo Bonzini uint8_t response[16]; 26949ab747fSPaolo Bonzini 27049ab747fSPaolo Bonzini request.cmd = 0x0C; 27149ab747fSPaolo Bonzini request.arg = 0; 27249ab747fSPaolo Bonzini DPRINT_L1("Automatically issue CMD%d %08x\n", request.cmd, request.arg); 27349ab747fSPaolo Bonzini sd_do_command(s->card, &request, response); 27449ab747fSPaolo Bonzini /* Auto CMD12 response goes to the upper Response register */ 27549ab747fSPaolo Bonzini s->rspreg[3] = (response[0] << 24) | (response[1] << 16) | 27649ab747fSPaolo Bonzini (response[2] << 8) | response[3]; 27749ab747fSPaolo Bonzini } 27849ab747fSPaolo Bonzini 27949ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE | 28049ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT | 28149ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE); 28249ab747fSPaolo Bonzini 28349ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_TRSCMP) { 28449ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 28549ab747fSPaolo Bonzini } 28649ab747fSPaolo Bonzini 28749ab747fSPaolo Bonzini sdhci_update_irq(s); 28849ab747fSPaolo Bonzini } 28949ab747fSPaolo Bonzini 29049ab747fSPaolo Bonzini /* 29149ab747fSPaolo Bonzini * Programmed i/o data transfer 29249ab747fSPaolo Bonzini */ 29349ab747fSPaolo Bonzini 29449ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */ 29549ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s) 29649ab747fSPaolo Bonzini { 29749ab747fSPaolo Bonzini int index = 0; 29849ab747fSPaolo Bonzini 29949ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) && 30049ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) { 30149ab747fSPaolo Bonzini return; 30249ab747fSPaolo Bonzini } 30349ab747fSPaolo Bonzini 30449ab747fSPaolo Bonzini for (index = 0; index < (s->blksize & 0x0fff); index++) { 30549ab747fSPaolo Bonzini s->fifo_buffer[index] = sd_read_data(s->card); 30649ab747fSPaolo Bonzini } 30749ab747fSPaolo Bonzini 30849ab747fSPaolo Bonzini /* New data now available for READ through Buffer Port Register */ 30949ab747fSPaolo Bonzini s->prnsts |= SDHC_DATA_AVAILABLE; 31049ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_RBUFRDY) { 31149ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_RBUFRDY; 31249ab747fSPaolo Bonzini } 31349ab747fSPaolo Bonzini 31449ab747fSPaolo Bonzini /* Clear DAT line active status if that was the last block */ 31549ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 31649ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) { 31749ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 31849ab747fSPaolo Bonzini } 31949ab747fSPaolo Bonzini 32049ab747fSPaolo Bonzini /* If stop at block gap request was set and it's not the last block of 32149ab747fSPaolo Bonzini * data - generate Block Event interrupt */ 32249ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) && 32349ab747fSPaolo Bonzini s->blkcnt != 1) { 32449ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 32549ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 32649ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 32749ab747fSPaolo Bonzini } 32849ab747fSPaolo Bonzini } 32949ab747fSPaolo Bonzini 33049ab747fSPaolo Bonzini sdhci_update_irq(s); 33149ab747fSPaolo Bonzini } 33249ab747fSPaolo Bonzini 33349ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */ 33449ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size) 33549ab747fSPaolo Bonzini { 33649ab747fSPaolo Bonzini uint32_t value = 0; 33749ab747fSPaolo Bonzini int i; 33849ab747fSPaolo Bonzini 33949ab747fSPaolo Bonzini /* first check that a valid data exists in host controller input buffer */ 34049ab747fSPaolo Bonzini if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) { 34149ab747fSPaolo Bonzini ERRPRINT("Trying to read from empty buffer\n"); 34249ab747fSPaolo Bonzini return 0; 34349ab747fSPaolo Bonzini } 34449ab747fSPaolo Bonzini 34549ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 34649ab747fSPaolo Bonzini value |= s->fifo_buffer[s->data_count] << i * 8; 34749ab747fSPaolo Bonzini s->data_count++; 34849ab747fSPaolo Bonzini /* check if we've read all valid data (blksize bytes) from buffer */ 34949ab747fSPaolo Bonzini if ((s->data_count) >= (s->blksize & 0x0fff)) { 35049ab747fSPaolo Bonzini DPRINT_L2("All %u bytes of data have been read from input buffer\n", 35149ab747fSPaolo Bonzini s->data_count); 35249ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */ 35349ab747fSPaolo Bonzini s->data_count = 0; /* next buff read must start at position [0] */ 35449ab747fSPaolo Bonzini 35549ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 35649ab747fSPaolo Bonzini s->blkcnt--; 35749ab747fSPaolo Bonzini } 35849ab747fSPaolo Bonzini 35949ab747fSPaolo Bonzini /* if that was the last block of data */ 36049ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 36149ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) || 36249ab747fSPaolo Bonzini /* stop at gap request */ 36349ab747fSPaolo Bonzini (s->stopped_state == sdhc_gap_read && 36449ab747fSPaolo Bonzini !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) { 365d368ba43SKevin O'Connor sdhci_end_transfer(s); 36649ab747fSPaolo Bonzini } else { /* if there are more data, read next block from card */ 367d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 36849ab747fSPaolo Bonzini } 36949ab747fSPaolo Bonzini break; 37049ab747fSPaolo Bonzini } 37149ab747fSPaolo Bonzini } 37249ab747fSPaolo Bonzini 37349ab747fSPaolo Bonzini return value; 37449ab747fSPaolo Bonzini } 37549ab747fSPaolo Bonzini 37649ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */ 37749ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s) 37849ab747fSPaolo Bonzini { 37949ab747fSPaolo Bonzini int index = 0; 38049ab747fSPaolo Bonzini 38149ab747fSPaolo Bonzini if (s->prnsts & SDHC_SPACE_AVAILABLE) { 38249ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 38349ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_WBUFRDY; 38449ab747fSPaolo Bonzini } 38549ab747fSPaolo Bonzini sdhci_update_irq(s); 38649ab747fSPaolo Bonzini return; 38749ab747fSPaolo Bonzini } 38849ab747fSPaolo Bonzini 38949ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 39049ab747fSPaolo Bonzini if (s->blkcnt == 0) { 39149ab747fSPaolo Bonzini return; 39249ab747fSPaolo Bonzini } else { 39349ab747fSPaolo Bonzini s->blkcnt--; 39449ab747fSPaolo Bonzini } 39549ab747fSPaolo Bonzini } 39649ab747fSPaolo Bonzini 39749ab747fSPaolo Bonzini for (index = 0; index < (s->blksize & 0x0fff); index++) { 39849ab747fSPaolo Bonzini sd_write_data(s->card, s->fifo_buffer[index]); 39949ab747fSPaolo Bonzini } 40049ab747fSPaolo Bonzini 40149ab747fSPaolo Bonzini /* Next data can be written through BUFFER DATORT register */ 40249ab747fSPaolo Bonzini s->prnsts |= SDHC_SPACE_AVAILABLE; 40349ab747fSPaolo Bonzini 40449ab747fSPaolo Bonzini /* Finish transfer if that was the last block of data */ 40549ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 40649ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && 40749ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) { 408d368ba43SKevin O'Connor sdhci_end_transfer(s); 409dcdb4cd8SPeter Crosthwaite } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 410dcdb4cd8SPeter Crosthwaite s->norintsts |= SDHC_NIS_WBUFRDY; 41149ab747fSPaolo Bonzini } 41249ab747fSPaolo Bonzini 41349ab747fSPaolo Bonzini /* Generate Block Gap Event if requested and if not the last block */ 41449ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) && 41549ab747fSPaolo Bonzini s->blkcnt > 0) { 41649ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DOING_WRITE; 41749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 41849ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 41949ab747fSPaolo Bonzini } 420d368ba43SKevin O'Connor sdhci_end_transfer(s); 42149ab747fSPaolo Bonzini } 42249ab747fSPaolo Bonzini 42349ab747fSPaolo Bonzini sdhci_update_irq(s); 42449ab747fSPaolo Bonzini } 42549ab747fSPaolo Bonzini 42649ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port 42749ab747fSPaolo Bonzini * register */ 42849ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size) 42949ab747fSPaolo Bonzini { 43049ab747fSPaolo Bonzini unsigned i; 43149ab747fSPaolo Bonzini 43249ab747fSPaolo Bonzini /* Check that there is free space left in a buffer */ 43349ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) { 43449ab747fSPaolo Bonzini ERRPRINT("Can't write to data buffer: buffer full\n"); 43549ab747fSPaolo Bonzini return; 43649ab747fSPaolo Bonzini } 43749ab747fSPaolo Bonzini 43849ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 43949ab747fSPaolo Bonzini s->fifo_buffer[s->data_count] = value & 0xFF; 44049ab747fSPaolo Bonzini s->data_count++; 44149ab747fSPaolo Bonzini value >>= 8; 44249ab747fSPaolo Bonzini if (s->data_count >= (s->blksize & 0x0fff)) { 44349ab747fSPaolo Bonzini DPRINT_L2("write buffer filled with %u bytes of data\n", 44449ab747fSPaolo Bonzini s->data_count); 44549ab747fSPaolo Bonzini s->data_count = 0; 44649ab747fSPaolo Bonzini s->prnsts &= ~SDHC_SPACE_AVAILABLE; 44749ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_WRITE) { 448d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 44949ab747fSPaolo Bonzini } 45049ab747fSPaolo Bonzini } 45149ab747fSPaolo Bonzini } 45249ab747fSPaolo Bonzini } 45349ab747fSPaolo Bonzini 45449ab747fSPaolo Bonzini /* 45549ab747fSPaolo Bonzini * Single DMA data transfer 45649ab747fSPaolo Bonzini */ 45749ab747fSPaolo Bonzini 45849ab747fSPaolo Bonzini /* Multi block SDMA transfer */ 45949ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s) 46049ab747fSPaolo Bonzini { 46149ab747fSPaolo Bonzini bool page_aligned = false; 46249ab747fSPaolo Bonzini unsigned int n, begin; 46349ab747fSPaolo Bonzini const uint16_t block_size = s->blksize & 0x0fff; 46449ab747fSPaolo Bonzini uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12); 46549ab747fSPaolo Bonzini uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk); 46649ab747fSPaolo Bonzini 46749ab747fSPaolo Bonzini /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for 46849ab747fSPaolo Bonzini * possible stop at page boundary if initial address is not page aligned, 46949ab747fSPaolo Bonzini * allow them to work properly */ 47049ab747fSPaolo Bonzini if ((s->sdmasysad % boundary_chk) == 0) { 47149ab747fSPaolo Bonzini page_aligned = true; 47249ab747fSPaolo Bonzini } 47349ab747fSPaolo Bonzini 47449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 47549ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT | 47649ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 47749ab747fSPaolo Bonzini while (s->blkcnt) { 47849ab747fSPaolo Bonzini if (s->data_count == 0) { 47949ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 48049ab747fSPaolo Bonzini s->fifo_buffer[n] = sd_read_data(s->card); 48149ab747fSPaolo Bonzini } 48249ab747fSPaolo Bonzini } 48349ab747fSPaolo Bonzini begin = s->data_count; 48449ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 48549ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 48649ab747fSPaolo Bonzini boundary_count = 0; 48749ab747fSPaolo Bonzini } else { 48849ab747fSPaolo Bonzini s->data_count = block_size; 48949ab747fSPaolo Bonzini boundary_count -= block_size - begin; 49049ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 49149ab747fSPaolo Bonzini s->blkcnt--; 49249ab747fSPaolo Bonzini } 49349ab747fSPaolo Bonzini } 494df32fd1cSPaolo Bonzini dma_memory_write(&address_space_memory, s->sdmasysad, 49549ab747fSPaolo Bonzini &s->fifo_buffer[begin], s->data_count - begin); 49649ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 49749ab747fSPaolo Bonzini if (s->data_count == block_size) { 49849ab747fSPaolo Bonzini s->data_count = 0; 49949ab747fSPaolo Bonzini } 50049ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 50149ab747fSPaolo Bonzini break; 50249ab747fSPaolo Bonzini } 50349ab747fSPaolo Bonzini } 50449ab747fSPaolo Bonzini } else { 50549ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT | 50649ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 50749ab747fSPaolo Bonzini while (s->blkcnt) { 50849ab747fSPaolo Bonzini begin = s->data_count; 50949ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 51049ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 51149ab747fSPaolo Bonzini boundary_count = 0; 51249ab747fSPaolo Bonzini } else { 51349ab747fSPaolo Bonzini s->data_count = block_size; 51449ab747fSPaolo Bonzini boundary_count -= block_size - begin; 51549ab747fSPaolo Bonzini } 516df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, s->sdmasysad, 51749ab747fSPaolo Bonzini &s->fifo_buffer[begin], s->data_count); 51849ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 51949ab747fSPaolo Bonzini if (s->data_count == block_size) { 52049ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 52149ab747fSPaolo Bonzini sd_write_data(s->card, s->fifo_buffer[n]); 52249ab747fSPaolo Bonzini } 52349ab747fSPaolo Bonzini s->data_count = 0; 52449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 52549ab747fSPaolo Bonzini s->blkcnt--; 52649ab747fSPaolo Bonzini } 52749ab747fSPaolo Bonzini } 52849ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 52949ab747fSPaolo Bonzini break; 53049ab747fSPaolo Bonzini } 53149ab747fSPaolo Bonzini } 53249ab747fSPaolo Bonzini } 53349ab747fSPaolo Bonzini 53449ab747fSPaolo Bonzini if (s->blkcnt == 0) { 535d368ba43SKevin O'Connor sdhci_end_transfer(s); 53649ab747fSPaolo Bonzini } else { 53749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_DMA) { 53849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_DMA; 53949ab747fSPaolo Bonzini } 54049ab747fSPaolo Bonzini sdhci_update_irq(s); 54149ab747fSPaolo Bonzini } 54249ab747fSPaolo Bonzini } 54349ab747fSPaolo Bonzini 54449ab747fSPaolo Bonzini /* single block SDMA transfer */ 54549ab747fSPaolo Bonzini 54649ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s) 54749ab747fSPaolo Bonzini { 54849ab747fSPaolo Bonzini int n; 54949ab747fSPaolo Bonzini uint32_t datacnt = s->blksize & 0x0fff; 55049ab747fSPaolo Bonzini 55149ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 55249ab747fSPaolo Bonzini for (n = 0; n < datacnt; n++) { 55349ab747fSPaolo Bonzini s->fifo_buffer[n] = sd_read_data(s->card); 55449ab747fSPaolo Bonzini } 555df32fd1cSPaolo Bonzini dma_memory_write(&address_space_memory, s->sdmasysad, s->fifo_buffer, 55649ab747fSPaolo Bonzini datacnt); 55749ab747fSPaolo Bonzini } else { 558df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, s->sdmasysad, s->fifo_buffer, 55949ab747fSPaolo Bonzini datacnt); 56049ab747fSPaolo Bonzini for (n = 0; n < datacnt; n++) { 56149ab747fSPaolo Bonzini sd_write_data(s->card, s->fifo_buffer[n]); 56249ab747fSPaolo Bonzini } 56349ab747fSPaolo Bonzini } 56449ab747fSPaolo Bonzini 56549ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 56649ab747fSPaolo Bonzini s->blkcnt--; 56749ab747fSPaolo Bonzini } 56849ab747fSPaolo Bonzini 569d368ba43SKevin O'Connor sdhci_end_transfer(s); 57049ab747fSPaolo Bonzini } 57149ab747fSPaolo Bonzini 57249ab747fSPaolo Bonzini typedef struct ADMADescr { 57349ab747fSPaolo Bonzini hwaddr addr; 57449ab747fSPaolo Bonzini uint16_t length; 57549ab747fSPaolo Bonzini uint8_t attr; 57649ab747fSPaolo Bonzini uint8_t incr; 57749ab747fSPaolo Bonzini } ADMADescr; 57849ab747fSPaolo Bonzini 57949ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr) 58049ab747fSPaolo Bonzini { 58149ab747fSPaolo Bonzini uint32_t adma1 = 0; 58249ab747fSPaolo Bonzini uint64_t adma2 = 0; 58349ab747fSPaolo Bonzini hwaddr entry_addr = (hwaddr)s->admasysaddr; 58449ab747fSPaolo Bonzini switch (SDHC_DMA_TYPE(s->hostctl)) { 58549ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 586df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma2, 58749ab747fSPaolo Bonzini sizeof(adma2)); 58849ab747fSPaolo Bonzini adma2 = le64_to_cpu(adma2); 58949ab747fSPaolo Bonzini /* The spec does not specify endianness of descriptor table. 59049ab747fSPaolo Bonzini * We currently assume that it is LE. 59149ab747fSPaolo Bonzini */ 59249ab747fSPaolo Bonzini dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull; 59349ab747fSPaolo Bonzini dscr->length = (uint16_t)extract64(adma2, 16, 16); 59449ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract64(adma2, 0, 7); 59549ab747fSPaolo Bonzini dscr->incr = 8; 59649ab747fSPaolo Bonzini break; 59749ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 598df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma1, 59949ab747fSPaolo Bonzini sizeof(adma1)); 60049ab747fSPaolo Bonzini adma1 = le32_to_cpu(adma1); 60149ab747fSPaolo Bonzini dscr->addr = (hwaddr)(adma1 & 0xFFFFF000); 60249ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract32(adma1, 0, 7); 60349ab747fSPaolo Bonzini dscr->incr = 4; 60449ab747fSPaolo Bonzini if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) { 60549ab747fSPaolo Bonzini dscr->length = (uint16_t)extract32(adma1, 12, 16); 60649ab747fSPaolo Bonzini } else { 60749ab747fSPaolo Bonzini dscr->length = 4096; 60849ab747fSPaolo Bonzini } 60949ab747fSPaolo Bonzini break; 61049ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 611df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr, 61249ab747fSPaolo Bonzini (uint8_t *)(&dscr->attr), 1); 613df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr + 2, 61449ab747fSPaolo Bonzini (uint8_t *)(&dscr->length), 2); 61549ab747fSPaolo Bonzini dscr->length = le16_to_cpu(dscr->length); 616df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr + 4, 61749ab747fSPaolo Bonzini (uint8_t *)(&dscr->addr), 8); 61849ab747fSPaolo Bonzini dscr->attr = le64_to_cpu(dscr->attr); 61949ab747fSPaolo Bonzini dscr->attr &= 0xfffffff8; 62049ab747fSPaolo Bonzini dscr->incr = 12; 62149ab747fSPaolo Bonzini break; 62249ab747fSPaolo Bonzini } 62349ab747fSPaolo Bonzini } 62449ab747fSPaolo Bonzini 62549ab747fSPaolo Bonzini /* Advanced DMA data transfer */ 62649ab747fSPaolo Bonzini 62749ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s) 62849ab747fSPaolo Bonzini { 62949ab747fSPaolo Bonzini unsigned int n, begin, length; 63049ab747fSPaolo Bonzini const uint16_t block_size = s->blksize & 0x0fff; 63149ab747fSPaolo Bonzini ADMADescr dscr; 63249ab747fSPaolo Bonzini int i; 63349ab747fSPaolo Bonzini 63449ab747fSPaolo Bonzini for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) { 63549ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH; 63649ab747fSPaolo Bonzini 63749ab747fSPaolo Bonzini get_adma_description(s, &dscr); 63849ab747fSPaolo Bonzini DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx ", len=%d, attr=%x\n", 63949ab747fSPaolo Bonzini dscr.addr, dscr.length, dscr.attr); 64049ab747fSPaolo Bonzini 64149ab747fSPaolo Bonzini if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) { 64249ab747fSPaolo Bonzini /* Indicate that error occurred in ST_FDS state */ 64349ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_STATE_MASK; 64449ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS; 64549ab747fSPaolo Bonzini 64649ab747fSPaolo Bonzini /* Generate ADMA error interrupt */ 64749ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 64849ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 64949ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 65049ab747fSPaolo Bonzini } 65149ab747fSPaolo Bonzini 65249ab747fSPaolo Bonzini sdhci_update_irq(s); 65349ab747fSPaolo Bonzini return; 65449ab747fSPaolo Bonzini } 65549ab747fSPaolo Bonzini 65649ab747fSPaolo Bonzini length = dscr.length ? dscr.length : 65536; 65749ab747fSPaolo Bonzini 65849ab747fSPaolo Bonzini switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) { 65949ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_TRAN: /* data transfer */ 66049ab747fSPaolo Bonzini 66149ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 66249ab747fSPaolo Bonzini while (length) { 66349ab747fSPaolo Bonzini if (s->data_count == 0) { 66449ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 66549ab747fSPaolo Bonzini s->fifo_buffer[n] = sd_read_data(s->card); 66649ab747fSPaolo Bonzini } 66749ab747fSPaolo Bonzini } 66849ab747fSPaolo Bonzini begin = s->data_count; 66949ab747fSPaolo Bonzini if ((length + begin) < block_size) { 67049ab747fSPaolo Bonzini s->data_count = length + begin; 67149ab747fSPaolo Bonzini length = 0; 67249ab747fSPaolo Bonzini } else { 67349ab747fSPaolo Bonzini s->data_count = block_size; 67449ab747fSPaolo Bonzini length -= block_size - begin; 67549ab747fSPaolo Bonzini } 676df32fd1cSPaolo Bonzini dma_memory_write(&address_space_memory, dscr.addr, 67749ab747fSPaolo Bonzini &s->fifo_buffer[begin], 67849ab747fSPaolo Bonzini s->data_count - begin); 67949ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 68049ab747fSPaolo Bonzini if (s->data_count == block_size) { 68149ab747fSPaolo Bonzini s->data_count = 0; 68249ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 68349ab747fSPaolo Bonzini s->blkcnt--; 68449ab747fSPaolo Bonzini if (s->blkcnt == 0) { 68549ab747fSPaolo Bonzini break; 68649ab747fSPaolo Bonzini } 68749ab747fSPaolo Bonzini } 68849ab747fSPaolo Bonzini } 68949ab747fSPaolo Bonzini } 69049ab747fSPaolo Bonzini } else { 69149ab747fSPaolo Bonzini while (length) { 69249ab747fSPaolo Bonzini begin = s->data_count; 69349ab747fSPaolo Bonzini if ((length + begin) < block_size) { 69449ab747fSPaolo Bonzini s->data_count = length + begin; 69549ab747fSPaolo Bonzini length = 0; 69649ab747fSPaolo Bonzini } else { 69749ab747fSPaolo Bonzini s->data_count = block_size; 69849ab747fSPaolo Bonzini length -= block_size - begin; 69949ab747fSPaolo Bonzini } 700df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, dscr.addr, 7019db11cefSPeter Crosthwaite &s->fifo_buffer[begin], 7029db11cefSPeter Crosthwaite s->data_count - begin); 70349ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 70449ab747fSPaolo Bonzini if (s->data_count == block_size) { 70549ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 70649ab747fSPaolo Bonzini sd_write_data(s->card, s->fifo_buffer[n]); 70749ab747fSPaolo Bonzini } 70849ab747fSPaolo Bonzini s->data_count = 0; 70949ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 71049ab747fSPaolo Bonzini s->blkcnt--; 71149ab747fSPaolo Bonzini if (s->blkcnt == 0) { 71249ab747fSPaolo Bonzini break; 71349ab747fSPaolo Bonzini } 71449ab747fSPaolo Bonzini } 71549ab747fSPaolo Bonzini } 71649ab747fSPaolo Bonzini } 71749ab747fSPaolo Bonzini } 71849ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 71949ab747fSPaolo Bonzini break; 72049ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_LINK: /* link to next descriptor table */ 72149ab747fSPaolo Bonzini s->admasysaddr = dscr.addr; 72249ab747fSPaolo Bonzini DPRINT_L1("ADMA link: admasysaddr=0x%lx\n", s->admasysaddr); 72349ab747fSPaolo Bonzini break; 72449ab747fSPaolo Bonzini default: 72549ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 72649ab747fSPaolo Bonzini break; 72749ab747fSPaolo Bonzini } 72849ab747fSPaolo Bonzini 7291d32c26fSPeter Crosthwaite if (dscr.attr & SDHC_ADMA_ATTR_INT) { 7301d32c26fSPeter Crosthwaite DPRINT_L1("ADMA interrupt: admasysaddr=0x%lx\n", s->admasysaddr); 7311d32c26fSPeter Crosthwaite if (s->norintstsen & SDHC_NISEN_DMA) { 7321d32c26fSPeter Crosthwaite s->norintsts |= SDHC_NIS_DMA; 7331d32c26fSPeter Crosthwaite } 7341d32c26fSPeter Crosthwaite 7351d32c26fSPeter Crosthwaite sdhci_update_irq(s); 7361d32c26fSPeter Crosthwaite } 7371d32c26fSPeter Crosthwaite 73849ab747fSPaolo Bonzini /* ADMA transfer terminates if blkcnt == 0 or by END attribute */ 73949ab747fSPaolo Bonzini if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 74049ab747fSPaolo Bonzini (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) { 74149ab747fSPaolo Bonzini DPRINT_L2("ADMA transfer completed\n"); 74249ab747fSPaolo Bonzini if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) && 74349ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 74449ab747fSPaolo Bonzini s->blkcnt != 0)) { 74549ab747fSPaolo Bonzini ERRPRINT("SD/MMC host ADMA length mismatch\n"); 74649ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH | 74749ab747fSPaolo Bonzini SDHC_ADMAERR_STATE_ST_TFR; 74849ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 74949ab747fSPaolo Bonzini ERRPRINT("Set ADMA error flag\n"); 75049ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 75149ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 75249ab747fSPaolo Bonzini } 75349ab747fSPaolo Bonzini 75449ab747fSPaolo Bonzini sdhci_update_irq(s); 75549ab747fSPaolo Bonzini } 756d368ba43SKevin O'Connor sdhci_end_transfer(s); 75749ab747fSPaolo Bonzini return; 75849ab747fSPaolo Bonzini } 75949ab747fSPaolo Bonzini 76049ab747fSPaolo Bonzini } 76149ab747fSPaolo Bonzini 76249ab747fSPaolo Bonzini /* we have unfinished business - reschedule to continue ADMA */ 763bc72ad67SAlex Bligh timer_mod(s->transfer_timer, 764bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY); 76549ab747fSPaolo Bonzini } 76649ab747fSPaolo Bonzini 76749ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */ 76849ab747fSPaolo Bonzini 769d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque) 77049ab747fSPaolo Bonzini { 771d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 77249ab747fSPaolo Bonzini 77349ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_DMA) { 77449ab747fSPaolo Bonzini switch (SDHC_DMA_TYPE(s->hostctl)) { 77549ab747fSPaolo Bonzini case SDHC_CTRL_SDMA: 77649ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) && 77749ab747fSPaolo Bonzini (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || s->blkcnt == 0)) { 77849ab747fSPaolo Bonzini break; 77949ab747fSPaolo Bonzini } 78049ab747fSPaolo Bonzini 78149ab747fSPaolo Bonzini if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) { 782d368ba43SKevin O'Connor sdhci_sdma_transfer_single_block(s); 78349ab747fSPaolo Bonzini } else { 784d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 78549ab747fSPaolo Bonzini } 78649ab747fSPaolo Bonzini 78749ab747fSPaolo Bonzini break; 78849ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 78949ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_ADMA1)) { 79049ab747fSPaolo Bonzini ERRPRINT("ADMA1 not supported\n"); 79149ab747fSPaolo Bonzini break; 79249ab747fSPaolo Bonzini } 79349ab747fSPaolo Bonzini 794d368ba43SKevin O'Connor sdhci_do_adma(s); 79549ab747fSPaolo Bonzini break; 79649ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 79749ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_ADMA2)) { 79849ab747fSPaolo Bonzini ERRPRINT("ADMA2 not supported\n"); 79949ab747fSPaolo Bonzini break; 80049ab747fSPaolo Bonzini } 80149ab747fSPaolo Bonzini 802d368ba43SKevin O'Connor sdhci_do_adma(s); 80349ab747fSPaolo Bonzini break; 80449ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 80549ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_ADMA2) || 80649ab747fSPaolo Bonzini !(s->capareg & SDHC_64_BIT_BUS_SUPPORT)) { 80749ab747fSPaolo Bonzini ERRPRINT("64 bit ADMA not supported\n"); 80849ab747fSPaolo Bonzini break; 80949ab747fSPaolo Bonzini } 81049ab747fSPaolo Bonzini 811d368ba43SKevin O'Connor sdhci_do_adma(s); 81249ab747fSPaolo Bonzini break; 81349ab747fSPaolo Bonzini default: 81449ab747fSPaolo Bonzini ERRPRINT("Unsupported DMA type\n"); 81549ab747fSPaolo Bonzini break; 81649ab747fSPaolo Bonzini } 81749ab747fSPaolo Bonzini } else { 81849ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_READ) && sd_data_ready(s->card)) { 81949ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT | 82049ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 821d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 82249ab747fSPaolo Bonzini } else { 82349ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE | 82449ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT; 825d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 82649ab747fSPaolo Bonzini } 82749ab747fSPaolo Bonzini } 82849ab747fSPaolo Bonzini } 82949ab747fSPaolo Bonzini 83049ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s) 83149ab747fSPaolo Bonzini { 83249ab747fSPaolo Bonzini if (!SDHC_CLOCK_IS_ON(s->clkcon) || !(s->pwrcon & SDHC_POWER_ON) || 83349ab747fSPaolo Bonzini (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) && 83449ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_DATA_PRESENT) || 83549ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY && 83649ab747fSPaolo Bonzini !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) { 83749ab747fSPaolo Bonzini return false; 83849ab747fSPaolo Bonzini } 83949ab747fSPaolo Bonzini 84049ab747fSPaolo Bonzini return true; 84149ab747fSPaolo Bonzini } 84249ab747fSPaolo Bonzini 84349ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and 84449ab747fSPaolo Bonzini * continuous manner */ 84549ab747fSPaolo Bonzini static inline bool 84649ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num) 84749ab747fSPaolo Bonzini { 84849ab747fSPaolo Bonzini if ((s->data_count & 0x3) != byte_num) { 84949ab747fSPaolo Bonzini ERRPRINT("Non-sequential access to Buffer Data Port register" 85049ab747fSPaolo Bonzini "is prohibited\n"); 85149ab747fSPaolo Bonzini return false; 85249ab747fSPaolo Bonzini } 85349ab747fSPaolo Bonzini return true; 85449ab747fSPaolo Bonzini } 85549ab747fSPaolo Bonzini 856d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size) 85749ab747fSPaolo Bonzini { 858d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 85949ab747fSPaolo Bonzini uint32_t ret = 0; 86049ab747fSPaolo Bonzini 86149ab747fSPaolo Bonzini switch (offset & ~0x3) { 86249ab747fSPaolo Bonzini case SDHC_SYSAD: 86349ab747fSPaolo Bonzini ret = s->sdmasysad; 86449ab747fSPaolo Bonzini break; 86549ab747fSPaolo Bonzini case SDHC_BLKSIZE: 86649ab747fSPaolo Bonzini ret = s->blksize | (s->blkcnt << 16); 86749ab747fSPaolo Bonzini break; 86849ab747fSPaolo Bonzini case SDHC_ARGUMENT: 86949ab747fSPaolo Bonzini ret = s->argument; 87049ab747fSPaolo Bonzini break; 87149ab747fSPaolo Bonzini case SDHC_TRNMOD: 87249ab747fSPaolo Bonzini ret = s->trnmod | (s->cmdreg << 16); 87349ab747fSPaolo Bonzini break; 87449ab747fSPaolo Bonzini case SDHC_RSPREG0 ... SDHC_RSPREG3: 87549ab747fSPaolo Bonzini ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2]; 87649ab747fSPaolo Bonzini break; 87749ab747fSPaolo Bonzini case SDHC_BDATA: 87849ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 879d368ba43SKevin O'Connor ret = sdhci_read_dataport(s, size); 880d368ba43SKevin O'Connor DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset, 881677ff2aeSPeter Crosthwaite ret, ret); 88249ab747fSPaolo Bonzini return ret; 88349ab747fSPaolo Bonzini } 88449ab747fSPaolo Bonzini break; 88549ab747fSPaolo Bonzini case SDHC_PRNSTS: 88649ab747fSPaolo Bonzini ret = s->prnsts; 88749ab747fSPaolo Bonzini break; 88849ab747fSPaolo Bonzini case SDHC_HOSTCTL: 88949ab747fSPaolo Bonzini ret = s->hostctl | (s->pwrcon << 8) | (s->blkgap << 16) | 89049ab747fSPaolo Bonzini (s->wakcon << 24); 89149ab747fSPaolo Bonzini break; 89249ab747fSPaolo Bonzini case SDHC_CLKCON: 89349ab747fSPaolo Bonzini ret = s->clkcon | (s->timeoutcon << 16); 89449ab747fSPaolo Bonzini break; 89549ab747fSPaolo Bonzini case SDHC_NORINTSTS: 89649ab747fSPaolo Bonzini ret = s->norintsts | (s->errintsts << 16); 89749ab747fSPaolo Bonzini break; 89849ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 89949ab747fSPaolo Bonzini ret = s->norintstsen | (s->errintstsen << 16); 90049ab747fSPaolo Bonzini break; 90149ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 90249ab747fSPaolo Bonzini ret = s->norintsigen | (s->errintsigen << 16); 90349ab747fSPaolo Bonzini break; 90449ab747fSPaolo Bonzini case SDHC_ACMD12ERRSTS: 90549ab747fSPaolo Bonzini ret = s->acmd12errsts; 90649ab747fSPaolo Bonzini break; 90749ab747fSPaolo Bonzini case SDHC_CAPAREG: 90849ab747fSPaolo Bonzini ret = s->capareg; 90949ab747fSPaolo Bonzini break; 91049ab747fSPaolo Bonzini case SDHC_MAXCURR: 91149ab747fSPaolo Bonzini ret = s->maxcurr; 91249ab747fSPaolo Bonzini break; 91349ab747fSPaolo Bonzini case SDHC_ADMAERR: 91449ab747fSPaolo Bonzini ret = s->admaerr; 91549ab747fSPaolo Bonzini break; 91649ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 91749ab747fSPaolo Bonzini ret = (uint32_t)s->admasysaddr; 91849ab747fSPaolo Bonzini break; 91949ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 92049ab747fSPaolo Bonzini ret = (uint32_t)(s->admasysaddr >> 32); 92149ab747fSPaolo Bonzini break; 92249ab747fSPaolo Bonzini case SDHC_SLOT_INT_STATUS: 92349ab747fSPaolo Bonzini ret = (SD_HOST_SPECv2_VERS << 16) | sdhci_slotint(s); 92449ab747fSPaolo Bonzini break; 92549ab747fSPaolo Bonzini default: 926d368ba43SKevin O'Connor ERRPRINT("bad %ub read: addr[0x%04x]\n", size, (int)offset); 92749ab747fSPaolo Bonzini break; 92849ab747fSPaolo Bonzini } 92949ab747fSPaolo Bonzini 93049ab747fSPaolo Bonzini ret >>= (offset & 0x3) * 8; 93149ab747fSPaolo Bonzini ret &= (1ULL << (size * 8)) - 1; 932d368ba43SKevin O'Connor DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset, ret, ret); 93349ab747fSPaolo Bonzini return ret; 93449ab747fSPaolo Bonzini } 93549ab747fSPaolo Bonzini 93649ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value) 93749ab747fSPaolo Bonzini { 93849ab747fSPaolo Bonzini if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) { 93949ab747fSPaolo Bonzini return; 94049ab747fSPaolo Bonzini } 94149ab747fSPaolo Bonzini s->blkgap = value & SDHC_STOP_AT_GAP_REQ; 94249ab747fSPaolo Bonzini 94349ab747fSPaolo Bonzini if ((value & SDHC_CONTINUE_REQ) && s->stopped_state && 94449ab747fSPaolo Bonzini (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) { 94549ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read) { 94649ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ; 947d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 94849ab747fSPaolo Bonzini } else { 94949ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE; 950d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 95149ab747fSPaolo Bonzini } 95249ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 95349ab747fSPaolo Bonzini } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) { 95449ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_READ) { 95549ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_read; 95649ab747fSPaolo Bonzini } else if (s->prnsts & SDHC_DOING_WRITE) { 95749ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_write; 95849ab747fSPaolo Bonzini } 95949ab747fSPaolo Bonzini } 96049ab747fSPaolo Bonzini } 96149ab747fSPaolo Bonzini 96249ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value) 96349ab747fSPaolo Bonzini { 96449ab747fSPaolo Bonzini switch (value) { 96549ab747fSPaolo Bonzini case SDHC_RESET_ALL: 966d368ba43SKevin O'Connor sdhci_reset(s); 96749ab747fSPaolo Bonzini break; 96849ab747fSPaolo Bonzini case SDHC_RESET_CMD: 96949ab747fSPaolo Bonzini s->prnsts &= ~SDHC_CMD_INHIBIT; 97049ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_CMDCMP; 97149ab747fSPaolo Bonzini break; 97249ab747fSPaolo Bonzini case SDHC_RESET_DATA: 97349ab747fSPaolo Bonzini s->data_count = 0; 97449ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE | 97549ab747fSPaolo Bonzini SDHC_DOING_READ | SDHC_DOING_WRITE | 97649ab747fSPaolo Bonzini SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE); 97749ab747fSPaolo Bonzini s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ); 97849ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 97949ab747fSPaolo Bonzini s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY | 98049ab747fSPaolo Bonzini SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP); 98149ab747fSPaolo Bonzini break; 98249ab747fSPaolo Bonzini } 98349ab747fSPaolo Bonzini } 98449ab747fSPaolo Bonzini 98549ab747fSPaolo Bonzini static void 986d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) 98749ab747fSPaolo Bonzini { 988d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 98949ab747fSPaolo Bonzini unsigned shift = 8 * (offset & 0x3); 99049ab747fSPaolo Bonzini uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift); 991d368ba43SKevin O'Connor uint32_t value = val; 99249ab747fSPaolo Bonzini value <<= shift; 99349ab747fSPaolo Bonzini 99449ab747fSPaolo Bonzini switch (offset & ~0x3) { 99549ab747fSPaolo Bonzini case SDHC_SYSAD: 99649ab747fSPaolo Bonzini s->sdmasysad = (s->sdmasysad & mask) | value; 99749ab747fSPaolo Bonzini MASKED_WRITE(s->sdmasysad, mask, value); 99849ab747fSPaolo Bonzini /* Writing to last byte of sdmasysad might trigger transfer */ 99949ab747fSPaolo Bonzini if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt && 100049ab747fSPaolo Bonzini s->blksize && SDHC_DMA_TYPE(s->hostctl) == SDHC_CTRL_SDMA) { 1001d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 100249ab747fSPaolo Bonzini } 100349ab747fSPaolo Bonzini break; 100449ab747fSPaolo Bonzini case SDHC_BLKSIZE: 100549ab747fSPaolo Bonzini if (!TRANSFERRING_DATA(s->prnsts)) { 100649ab747fSPaolo Bonzini MASKED_WRITE(s->blksize, mask, value); 100749ab747fSPaolo Bonzini MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16); 100849ab747fSPaolo Bonzini } 100949ab747fSPaolo Bonzini break; 101049ab747fSPaolo Bonzini case SDHC_ARGUMENT: 101149ab747fSPaolo Bonzini MASKED_WRITE(s->argument, mask, value); 101249ab747fSPaolo Bonzini break; 101349ab747fSPaolo Bonzini case SDHC_TRNMOD: 101449ab747fSPaolo Bonzini /* DMA can be enabled only if it is supported as indicated by 101549ab747fSPaolo Bonzini * capabilities register */ 101649ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_DMA)) { 101749ab747fSPaolo Bonzini value &= ~SDHC_TRNS_DMA; 101849ab747fSPaolo Bonzini } 101949ab747fSPaolo Bonzini MASKED_WRITE(s->trnmod, mask, value); 102049ab747fSPaolo Bonzini MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16); 102149ab747fSPaolo Bonzini 102249ab747fSPaolo Bonzini /* Writing to the upper byte of CMDREG triggers SD command generation */ 1023d368ba43SKevin O'Connor if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) { 102449ab747fSPaolo Bonzini break; 102549ab747fSPaolo Bonzini } 102649ab747fSPaolo Bonzini 1027d368ba43SKevin O'Connor sdhci_send_command(s); 102849ab747fSPaolo Bonzini break; 102949ab747fSPaolo Bonzini case SDHC_BDATA: 103049ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 1031d368ba43SKevin O'Connor sdhci_write_dataport(s, value >> shift, size); 103249ab747fSPaolo Bonzini } 103349ab747fSPaolo Bonzini break; 103449ab747fSPaolo Bonzini case SDHC_HOSTCTL: 103549ab747fSPaolo Bonzini if (!(mask & 0xFF0000)) { 103649ab747fSPaolo Bonzini sdhci_blkgap_write(s, value >> 16); 103749ab747fSPaolo Bonzini } 103849ab747fSPaolo Bonzini MASKED_WRITE(s->hostctl, mask, value); 103949ab747fSPaolo Bonzini MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8); 104049ab747fSPaolo Bonzini MASKED_WRITE(s->wakcon, mask >> 24, value >> 24); 104149ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 || 104249ab747fSPaolo Bonzini !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) { 104349ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 104449ab747fSPaolo Bonzini } 104549ab747fSPaolo Bonzini break; 104649ab747fSPaolo Bonzini case SDHC_CLKCON: 104749ab747fSPaolo Bonzini if (!(mask & 0xFF000000)) { 104849ab747fSPaolo Bonzini sdhci_reset_write(s, value >> 24); 104949ab747fSPaolo Bonzini } 105049ab747fSPaolo Bonzini MASKED_WRITE(s->clkcon, mask, value); 105149ab747fSPaolo Bonzini MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16); 105249ab747fSPaolo Bonzini if (s->clkcon & SDHC_CLOCK_INT_EN) { 105349ab747fSPaolo Bonzini s->clkcon |= SDHC_CLOCK_INT_STABLE; 105449ab747fSPaolo Bonzini } else { 105549ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_INT_STABLE; 105649ab747fSPaolo Bonzini } 105749ab747fSPaolo Bonzini break; 105849ab747fSPaolo Bonzini case SDHC_NORINTSTS: 105949ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CARDINT) { 106049ab747fSPaolo Bonzini value &= ~SDHC_NIS_CARDINT; 106149ab747fSPaolo Bonzini } 106249ab747fSPaolo Bonzini s->norintsts &= mask | ~value; 106349ab747fSPaolo Bonzini s->errintsts &= (mask >> 16) | ~(value >> 16); 106449ab747fSPaolo Bonzini if (s->errintsts) { 106549ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 106649ab747fSPaolo Bonzini } else { 106749ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 106849ab747fSPaolo Bonzini } 106949ab747fSPaolo Bonzini sdhci_update_irq(s); 107049ab747fSPaolo Bonzini break; 107149ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 107249ab747fSPaolo Bonzini MASKED_WRITE(s->norintstsen, mask, value); 107349ab747fSPaolo Bonzini MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16); 107449ab747fSPaolo Bonzini s->norintsts &= s->norintstsen; 107549ab747fSPaolo Bonzini s->errintsts &= s->errintstsen; 107649ab747fSPaolo Bonzini if (s->errintsts) { 107749ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 107849ab747fSPaolo Bonzini } else { 107949ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 108049ab747fSPaolo Bonzini } 108149ab747fSPaolo Bonzini sdhci_update_irq(s); 108249ab747fSPaolo Bonzini break; 108349ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 108449ab747fSPaolo Bonzini MASKED_WRITE(s->norintsigen, mask, value); 108549ab747fSPaolo Bonzini MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16); 108649ab747fSPaolo Bonzini sdhci_update_irq(s); 108749ab747fSPaolo Bonzini break; 108849ab747fSPaolo Bonzini case SDHC_ADMAERR: 108949ab747fSPaolo Bonzini MASKED_WRITE(s->admaerr, mask, value); 109049ab747fSPaolo Bonzini break; 109149ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 109249ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL | 109349ab747fSPaolo Bonzini (uint64_t)mask)) | (uint64_t)value; 109449ab747fSPaolo Bonzini break; 109549ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 109649ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL | 109749ab747fSPaolo Bonzini ((uint64_t)mask << 32))) | ((uint64_t)value << 32); 109849ab747fSPaolo Bonzini break; 109949ab747fSPaolo Bonzini case SDHC_FEAER: 110049ab747fSPaolo Bonzini s->acmd12errsts |= value; 110149ab747fSPaolo Bonzini s->errintsts |= (value >> 16) & s->errintstsen; 110249ab747fSPaolo Bonzini if (s->acmd12errsts) { 110349ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMD12ERR; 110449ab747fSPaolo Bonzini } 110549ab747fSPaolo Bonzini if (s->errintsts) { 110649ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 110749ab747fSPaolo Bonzini } 110849ab747fSPaolo Bonzini sdhci_update_irq(s); 110949ab747fSPaolo Bonzini break; 111049ab747fSPaolo Bonzini default: 111149ab747fSPaolo Bonzini ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n", 1112d368ba43SKevin O'Connor size, (int)offset, value >> shift, value >> shift); 111349ab747fSPaolo Bonzini break; 111449ab747fSPaolo Bonzini } 111549ab747fSPaolo Bonzini DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n", 1116d368ba43SKevin O'Connor size, (int)offset, value >> shift, value >> shift); 111749ab747fSPaolo Bonzini } 111849ab747fSPaolo Bonzini 111949ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = { 1120d368ba43SKevin O'Connor .read = sdhci_read, 1121d368ba43SKevin O'Connor .write = sdhci_write, 112249ab747fSPaolo Bonzini .valid = { 112349ab747fSPaolo Bonzini .min_access_size = 1, 112449ab747fSPaolo Bonzini .max_access_size = 4, 112549ab747fSPaolo Bonzini .unaligned = false 112649ab747fSPaolo Bonzini }, 112749ab747fSPaolo Bonzini .endianness = DEVICE_LITTLE_ENDIAN, 112849ab747fSPaolo Bonzini }; 112949ab747fSPaolo Bonzini 113049ab747fSPaolo Bonzini static inline unsigned int sdhci_get_fifolen(SDHCIState *s) 113149ab747fSPaolo Bonzini { 113249ab747fSPaolo Bonzini switch (SDHC_CAPAB_BLOCKSIZE(s->capareg)) { 113349ab747fSPaolo Bonzini case 0: 113449ab747fSPaolo Bonzini return 512; 113549ab747fSPaolo Bonzini case 1: 113649ab747fSPaolo Bonzini return 1024; 113749ab747fSPaolo Bonzini case 2: 113849ab747fSPaolo Bonzini return 2048; 113949ab747fSPaolo Bonzini default: 114049ab747fSPaolo Bonzini hw_error("SDHC: unsupported value for maximum block size\n"); 114149ab747fSPaolo Bonzini return 0; 114249ab747fSPaolo Bonzini } 114349ab747fSPaolo Bonzini } 114449ab747fSPaolo Bonzini 11457302dcd6SKevin O'Connor static void sdhci_initfn(SDHCIState *s) 114649ab747fSPaolo Bonzini { 114749ab747fSPaolo Bonzini DriveInfo *di; 114849ab747fSPaolo Bonzini 114949ab747fSPaolo Bonzini di = drive_get_next(IF_SD); 11504be74634SMarkus Armbruster s->card = sd_init(di ? blk_by_legacy_dinfo(di) : NULL, false); 11514f8a066bSKevin Wolf if (s->card == NULL) { 11524f8a066bSKevin Wolf exit(1); 11534f8a066bSKevin Wolf } 1154f3c7d038SAndreas Färber s->eject_cb = qemu_allocate_irq(sdhci_insert_eject_cb, s, 0); 1155f3c7d038SAndreas Färber s->ro_cb = qemu_allocate_irq(sdhci_card_readonly_cb, s, 0); 115649ab747fSPaolo Bonzini sd_set_cb(s->card, s->ro_cb, s->eject_cb); 115749ab747fSPaolo Bonzini 1158bc72ad67SAlex Bligh s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s); 1159d368ba43SKevin O'Connor s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s); 116049ab747fSPaolo Bonzini } 116149ab747fSPaolo Bonzini 11627302dcd6SKevin O'Connor static void sdhci_uninitfn(SDHCIState *s) 116349ab747fSPaolo Bonzini { 1164bc72ad67SAlex Bligh timer_del(s->insert_timer); 1165bc72ad67SAlex Bligh timer_free(s->insert_timer); 1166bc72ad67SAlex Bligh timer_del(s->transfer_timer); 1167bc72ad67SAlex Bligh timer_free(s->transfer_timer); 1168127a4e1aSAndreas Färber qemu_free_irq(s->eject_cb); 1169127a4e1aSAndreas Färber qemu_free_irq(s->ro_cb); 117049ab747fSPaolo Bonzini 117149ab747fSPaolo Bonzini if (s->fifo_buffer) { 117249ab747fSPaolo Bonzini g_free(s->fifo_buffer); 117349ab747fSPaolo Bonzini s->fifo_buffer = NULL; 117449ab747fSPaolo Bonzini } 117549ab747fSPaolo Bonzini } 117649ab747fSPaolo Bonzini 117749ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = { 117849ab747fSPaolo Bonzini .name = "sdhci", 117949ab747fSPaolo Bonzini .version_id = 1, 118049ab747fSPaolo Bonzini .minimum_version_id = 1, 118149ab747fSPaolo Bonzini .fields = (VMStateField[]) { 118249ab747fSPaolo Bonzini VMSTATE_UINT32(sdmasysad, SDHCIState), 118349ab747fSPaolo Bonzini VMSTATE_UINT16(blksize, SDHCIState), 118449ab747fSPaolo Bonzini VMSTATE_UINT16(blkcnt, SDHCIState), 118549ab747fSPaolo Bonzini VMSTATE_UINT32(argument, SDHCIState), 118649ab747fSPaolo Bonzini VMSTATE_UINT16(trnmod, SDHCIState), 118749ab747fSPaolo Bonzini VMSTATE_UINT16(cmdreg, SDHCIState), 118849ab747fSPaolo Bonzini VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4), 118949ab747fSPaolo Bonzini VMSTATE_UINT32(prnsts, SDHCIState), 119049ab747fSPaolo Bonzini VMSTATE_UINT8(hostctl, SDHCIState), 119149ab747fSPaolo Bonzini VMSTATE_UINT8(pwrcon, SDHCIState), 119249ab747fSPaolo Bonzini VMSTATE_UINT8(blkgap, SDHCIState), 119349ab747fSPaolo Bonzini VMSTATE_UINT8(wakcon, SDHCIState), 119449ab747fSPaolo Bonzini VMSTATE_UINT16(clkcon, SDHCIState), 119549ab747fSPaolo Bonzini VMSTATE_UINT8(timeoutcon, SDHCIState), 119649ab747fSPaolo Bonzini VMSTATE_UINT8(admaerr, SDHCIState), 119749ab747fSPaolo Bonzini VMSTATE_UINT16(norintsts, SDHCIState), 119849ab747fSPaolo Bonzini VMSTATE_UINT16(errintsts, SDHCIState), 119949ab747fSPaolo Bonzini VMSTATE_UINT16(norintstsen, SDHCIState), 120049ab747fSPaolo Bonzini VMSTATE_UINT16(errintstsen, SDHCIState), 120149ab747fSPaolo Bonzini VMSTATE_UINT16(norintsigen, SDHCIState), 120249ab747fSPaolo Bonzini VMSTATE_UINT16(errintsigen, SDHCIState), 120349ab747fSPaolo Bonzini VMSTATE_UINT16(acmd12errsts, SDHCIState), 120449ab747fSPaolo Bonzini VMSTATE_UINT16(data_count, SDHCIState), 120549ab747fSPaolo Bonzini VMSTATE_UINT64(admasysaddr, SDHCIState), 120649ab747fSPaolo Bonzini VMSTATE_UINT8(stopped_state, SDHCIState), 120749ab747fSPaolo Bonzini VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, 0, buf_maxsz), 1208e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(insert_timer, SDHCIState), 1209e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(transfer_timer, SDHCIState), 121049ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 121149ab747fSPaolo Bonzini } 121249ab747fSPaolo Bonzini }; 121349ab747fSPaolo Bonzini 121449ab747fSPaolo Bonzini /* Capabilities registers provide information on supported features of this 121549ab747fSPaolo Bonzini * specific host controller implementation */ 121649ab747fSPaolo Bonzini static Property sdhci_properties[] = { 1217c7bcc85dSPaolo Bonzini DEFINE_PROP_UINT32("capareg", SDHCIState, capareg, 121849ab747fSPaolo Bonzini SDHC_CAPAB_REG_DEFAULT), 1219c7bcc85dSPaolo Bonzini DEFINE_PROP_UINT32("maxcurr", SDHCIState, maxcurr, 0), 122049ab747fSPaolo Bonzini DEFINE_PROP_END_OF_LIST(), 122149ab747fSPaolo Bonzini }; 122249ab747fSPaolo Bonzini 1223*9af21dbeSMarkus Armbruster static void sdhci_pci_realize(PCIDevice *dev, Error **errp) 1224224d10ffSKevin O'Connor { 1225224d10ffSKevin O'Connor SDHCIState *s = PCI_SDHCI(dev); 1226224d10ffSKevin O'Connor dev->config[PCI_CLASS_PROG] = 0x01; /* Standard Host supported DMA */ 1227224d10ffSKevin O'Connor dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */ 1228224d10ffSKevin O'Connor sdhci_initfn(s); 1229224d10ffSKevin O'Connor s->buf_maxsz = sdhci_get_fifolen(s); 1230224d10ffSKevin O'Connor s->fifo_buffer = g_malloc0(s->buf_maxsz); 1231224d10ffSKevin O'Connor s->irq = pci_allocate_irq(dev); 1232224d10ffSKevin O'Connor memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci", 1233224d10ffSKevin O'Connor SDHC_REGISTERS_MAP_SIZE); 1234224d10ffSKevin O'Connor pci_register_bar(dev, 0, 0, &s->iomem); 1235224d10ffSKevin O'Connor } 1236224d10ffSKevin O'Connor 1237224d10ffSKevin O'Connor static void sdhci_pci_exit(PCIDevice *dev) 1238224d10ffSKevin O'Connor { 1239224d10ffSKevin O'Connor SDHCIState *s = PCI_SDHCI(dev); 1240224d10ffSKevin O'Connor sdhci_uninitfn(s); 1241224d10ffSKevin O'Connor } 1242224d10ffSKevin O'Connor 1243224d10ffSKevin O'Connor static void sdhci_pci_class_init(ObjectClass *klass, void *data) 1244224d10ffSKevin O'Connor { 1245224d10ffSKevin O'Connor DeviceClass *dc = DEVICE_CLASS(klass); 1246224d10ffSKevin O'Connor PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1247224d10ffSKevin O'Connor 1248*9af21dbeSMarkus Armbruster k->realize = sdhci_pci_realize; 1249224d10ffSKevin O'Connor k->exit = sdhci_pci_exit; 1250224d10ffSKevin O'Connor k->vendor_id = PCI_VENDOR_ID_REDHAT; 1251224d10ffSKevin O'Connor k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI; 1252224d10ffSKevin O'Connor k->class_id = PCI_CLASS_SYSTEM_SDHCI; 1253224d10ffSKevin O'Connor set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1254224d10ffSKevin O'Connor dc->vmsd = &sdhci_vmstate; 1255224d10ffSKevin O'Connor dc->props = sdhci_properties; 1256224d10ffSKevin O'Connor } 1257224d10ffSKevin O'Connor 1258224d10ffSKevin O'Connor static const TypeInfo sdhci_pci_info = { 1259224d10ffSKevin O'Connor .name = TYPE_PCI_SDHCI, 1260224d10ffSKevin O'Connor .parent = TYPE_PCI_DEVICE, 1261224d10ffSKevin O'Connor .instance_size = sizeof(SDHCIState), 1262224d10ffSKevin O'Connor .class_init = sdhci_pci_class_init, 1263224d10ffSKevin O'Connor }; 1264224d10ffSKevin O'Connor 12657302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj) 126649ab747fSPaolo Bonzini { 12677302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 12687302dcd6SKevin O'Connor sdhci_initfn(s); 12697302dcd6SKevin O'Connor } 12707302dcd6SKevin O'Connor 12717302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj) 12727302dcd6SKevin O'Connor { 12737302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 12747302dcd6SKevin O'Connor sdhci_uninitfn(s); 12757302dcd6SKevin O'Connor } 12767302dcd6SKevin O'Connor 12777302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp) 12787302dcd6SKevin O'Connor { 12797302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(dev); 128049ab747fSPaolo Bonzini SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 128149ab747fSPaolo Bonzini 128249ab747fSPaolo Bonzini s->buf_maxsz = sdhci_get_fifolen(s); 128349ab747fSPaolo Bonzini s->fifo_buffer = g_malloc0(s->buf_maxsz); 128449ab747fSPaolo Bonzini sysbus_init_irq(sbd, &s->irq); 128529776739SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci", 128649ab747fSPaolo Bonzini SDHC_REGISTERS_MAP_SIZE); 128749ab747fSPaolo Bonzini sysbus_init_mmio(sbd, &s->iomem); 128849ab747fSPaolo Bonzini } 128949ab747fSPaolo Bonzini 12907302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data) 129149ab747fSPaolo Bonzini { 129249ab747fSPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass); 129349ab747fSPaolo Bonzini 129449ab747fSPaolo Bonzini dc->vmsd = &sdhci_vmstate; 129549ab747fSPaolo Bonzini dc->props = sdhci_properties; 12967302dcd6SKevin O'Connor dc->realize = sdhci_sysbus_realize; 129749ab747fSPaolo Bonzini } 129849ab747fSPaolo Bonzini 12997302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = { 13007302dcd6SKevin O'Connor .name = TYPE_SYSBUS_SDHCI, 130149ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 130249ab747fSPaolo Bonzini .instance_size = sizeof(SDHCIState), 13037302dcd6SKevin O'Connor .instance_init = sdhci_sysbus_init, 13047302dcd6SKevin O'Connor .instance_finalize = sdhci_sysbus_finalize, 13057302dcd6SKevin O'Connor .class_init = sdhci_sysbus_class_init, 130649ab747fSPaolo Bonzini }; 130749ab747fSPaolo Bonzini 130849ab747fSPaolo Bonzini static void sdhci_register_types(void) 130949ab747fSPaolo Bonzini { 1310224d10ffSKevin O'Connor type_register_static(&sdhci_pci_info); 13117302dcd6SKevin O'Connor type_register_static(&sdhci_sysbus_info); 131249ab747fSPaolo Bonzini } 131349ab747fSPaolo Bonzini 131449ab747fSPaolo Bonzini type_init(sdhci_register_types) 1315