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" 26*b635d98cSPhilippe 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" 3649ab747fSPaolo Bonzini 3749ab747fSPaolo Bonzini /* host controller debug messages */ 3849ab747fSPaolo Bonzini #ifndef SDHC_DEBUG 3949ab747fSPaolo Bonzini #define SDHC_DEBUG 0 4049ab747fSPaolo Bonzini #endif 4149ab747fSPaolo Bonzini 4249ab747fSPaolo Bonzini #define DPRINT_L1(fmt, args...) \ 437af0fc99SSai Pavan Boddu do { \ 447af0fc99SSai Pavan Boddu if (SDHC_DEBUG) { \ 457af0fc99SSai Pavan Boddu fprintf(stderr, "QEMU SDHC: " fmt, ## args); \ 467af0fc99SSai Pavan Boddu } \ 477af0fc99SSai Pavan Boddu } while (0) 4849ab747fSPaolo Bonzini #define DPRINT_L2(fmt, args...) \ 497af0fc99SSai Pavan Boddu do { \ 507af0fc99SSai Pavan Boddu if (SDHC_DEBUG > 1) { \ 517af0fc99SSai Pavan Boddu fprintf(stderr, "QEMU SDHC: " fmt, ## args); \ 527af0fc99SSai Pavan Boddu } \ 537af0fc99SSai Pavan Boddu } while (0) 5449ab747fSPaolo Bonzini #define ERRPRINT(fmt, args...) \ 557af0fc99SSai Pavan Boddu do { \ 567af0fc99SSai Pavan Boddu if (SDHC_DEBUG) { \ 577af0fc99SSai Pavan Boddu fprintf(stderr, "QEMU SDHC ERROR: " fmt, ## args); \ 587af0fc99SSai Pavan Boddu } \ 597af0fc99SSai Pavan Boddu } while (0) 6049ab747fSPaolo Bonzini 6140bbc194SPeter Maydell #define TYPE_SDHCI_BUS "sdhci-bus" 6240bbc194SPeter Maydell #define SDHCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SDHCI_BUS) 6340bbc194SPeter Maydell 6449ab747fSPaolo Bonzini /* Default SD/MMC host controller features information, which will be 6549ab747fSPaolo Bonzini * presented in CAPABILITIES register of generic SD host controller at reset. 6649ab747fSPaolo Bonzini * If not stated otherwise: 6749ab747fSPaolo Bonzini * 0 - not supported, 1 - supported, other - prohibited. 6849ab747fSPaolo Bonzini */ 6949ab747fSPaolo Bonzini #define SDHC_CAPAB_64BITBUS 0ul /* 64-bit System Bus Support */ 7049ab747fSPaolo Bonzini #define SDHC_CAPAB_18V 1ul /* Voltage support 1.8v */ 7149ab747fSPaolo Bonzini #define SDHC_CAPAB_30V 0ul /* Voltage support 3.0v */ 7249ab747fSPaolo Bonzini #define SDHC_CAPAB_33V 1ul /* Voltage support 3.3v */ 7349ab747fSPaolo Bonzini #define SDHC_CAPAB_SUSPRESUME 0ul /* Suspend/resume support */ 7449ab747fSPaolo Bonzini #define SDHC_CAPAB_SDMA 1ul /* SDMA support */ 7549ab747fSPaolo Bonzini #define SDHC_CAPAB_HIGHSPEED 1ul /* High speed support */ 7649ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA1 1ul /* ADMA1 support */ 7749ab747fSPaolo Bonzini #define SDHC_CAPAB_ADMA2 1ul /* ADMA2 support */ 7849ab747fSPaolo Bonzini /* Maximum host controller R/W buffers size 7949ab747fSPaolo Bonzini * Possible values: 512, 1024, 2048 bytes */ 8049ab747fSPaolo Bonzini #define SDHC_CAPAB_MAXBLOCKLENGTH 512ul 8149ab747fSPaolo Bonzini /* Maximum clock frequency for SDclock in MHz 8249ab747fSPaolo Bonzini * value in range 10-63 MHz, 0 - not defined */ 83c7ff8daaSKevin O'Connor #define SDHC_CAPAB_BASECLKFREQ 52ul 8449ab747fSPaolo Bonzini #define SDHC_CAPAB_TOUNIT 1ul /* Timeout clock unit 0 - kHz, 1 - MHz */ 8549ab747fSPaolo Bonzini /* Timeout clock frequency 1-63, 0 - not defined */ 86c7ff8daaSKevin O'Connor #define SDHC_CAPAB_TOCLKFREQ 52ul 8749ab747fSPaolo Bonzini 8849ab747fSPaolo Bonzini /* Now check all parameters and calculate CAPABILITIES REGISTER value */ 8949ab747fSPaolo Bonzini #if SDHC_CAPAB_64BITBUS > 1 || SDHC_CAPAB_18V > 1 || SDHC_CAPAB_30V > 1 || \ 9049ab747fSPaolo Bonzini SDHC_CAPAB_33V > 1 || SDHC_CAPAB_SUSPRESUME > 1 || SDHC_CAPAB_SDMA > 1 || \ 9149ab747fSPaolo Bonzini SDHC_CAPAB_HIGHSPEED > 1 || SDHC_CAPAB_ADMA2 > 1 || SDHC_CAPAB_ADMA1 > 1 ||\ 9249ab747fSPaolo Bonzini SDHC_CAPAB_TOUNIT > 1 9349ab747fSPaolo Bonzini #error Capabilities features can have value 0 or 1 only! 9449ab747fSPaolo Bonzini #endif 9549ab747fSPaolo Bonzini 9649ab747fSPaolo Bonzini #if SDHC_CAPAB_MAXBLOCKLENGTH == 512 9749ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 0ul 9849ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 1024 9949ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 1ul 10049ab747fSPaolo Bonzini #elif SDHC_CAPAB_MAXBLOCKLENGTH == 2048 10149ab747fSPaolo Bonzini #define MAX_BLOCK_LENGTH 2ul 10249ab747fSPaolo Bonzini #else 10349ab747fSPaolo Bonzini #error Max host controller block size can have value 512, 1024 or 2048 only! 10449ab747fSPaolo Bonzini #endif 10549ab747fSPaolo Bonzini 10649ab747fSPaolo Bonzini #if (SDHC_CAPAB_BASECLKFREQ > 0 && SDHC_CAPAB_BASECLKFREQ < 10) || \ 10749ab747fSPaolo Bonzini SDHC_CAPAB_BASECLKFREQ > 63 10849ab747fSPaolo Bonzini #error SDclock frequency can have value in range 0, 10-63 only! 10949ab747fSPaolo Bonzini #endif 11049ab747fSPaolo Bonzini 11149ab747fSPaolo Bonzini #if SDHC_CAPAB_TOCLKFREQ > 63 11249ab747fSPaolo Bonzini #error Timeout clock frequency can have value in range 0-63 only! 11349ab747fSPaolo Bonzini #endif 11449ab747fSPaolo Bonzini 11549ab747fSPaolo Bonzini #define SDHC_CAPAB_REG_DEFAULT \ 11649ab747fSPaolo Bonzini ((SDHC_CAPAB_64BITBUS << 28) | (SDHC_CAPAB_18V << 26) | \ 11749ab747fSPaolo Bonzini (SDHC_CAPAB_30V << 25) | (SDHC_CAPAB_33V << 24) | \ 11849ab747fSPaolo Bonzini (SDHC_CAPAB_SUSPRESUME << 23) | (SDHC_CAPAB_SDMA << 22) | \ 11949ab747fSPaolo Bonzini (SDHC_CAPAB_HIGHSPEED << 21) | (SDHC_CAPAB_ADMA1 << 20) | \ 12049ab747fSPaolo Bonzini (SDHC_CAPAB_ADMA2 << 19) | (MAX_BLOCK_LENGTH << 16) | \ 12149ab747fSPaolo Bonzini (SDHC_CAPAB_BASECLKFREQ << 8) | (SDHC_CAPAB_TOUNIT << 7) | \ 12249ab747fSPaolo Bonzini (SDHC_CAPAB_TOCLKFREQ)) 12349ab747fSPaolo Bonzini 1248b20aefaSPrasad J Pandit #define MASK_TRNMOD 0x0037 12549ab747fSPaolo Bonzini #define MASKED_WRITE(reg, mask, val) (reg = (reg & (mask)) | (val)) 12649ab747fSPaolo Bonzini 12749ab747fSPaolo Bonzini static uint8_t sdhci_slotint(SDHCIState *s) 12849ab747fSPaolo Bonzini { 12949ab747fSPaolo Bonzini return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) || 13049ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_INSERT) && (s->wakcon & SDHC_WKUP_ON_INS)) || 13149ab747fSPaolo Bonzini ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV)); 13249ab747fSPaolo Bonzini } 13349ab747fSPaolo Bonzini 13449ab747fSPaolo Bonzini static inline void sdhci_update_irq(SDHCIState *s) 13549ab747fSPaolo Bonzini { 13649ab747fSPaolo Bonzini qemu_set_irq(s->irq, sdhci_slotint(s)); 13749ab747fSPaolo Bonzini } 13849ab747fSPaolo Bonzini 13949ab747fSPaolo Bonzini static void sdhci_raise_insertion_irq(void *opaque) 14049ab747fSPaolo Bonzini { 14149ab747fSPaolo Bonzini SDHCIState *s = (SDHCIState *)opaque; 14249ab747fSPaolo Bonzini 14349ab747fSPaolo Bonzini if (s->norintsts & SDHC_NIS_REMOVE) { 144bc72ad67SAlex Bligh timer_mod(s->insert_timer, 145bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 14649ab747fSPaolo Bonzini } else { 14749ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 14849ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 14949ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 15049ab747fSPaolo Bonzini } 15149ab747fSPaolo Bonzini sdhci_update_irq(s); 15249ab747fSPaolo Bonzini } 15349ab747fSPaolo Bonzini } 15449ab747fSPaolo Bonzini 15540bbc194SPeter Maydell static void sdhci_set_inserted(DeviceState *dev, bool level) 15649ab747fSPaolo Bonzini { 15740bbc194SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 15849ab747fSPaolo Bonzini DPRINT_L1("Card state changed: %s!\n", level ? "insert" : "eject"); 15949ab747fSPaolo Bonzini 16049ab747fSPaolo Bonzini if ((s->norintsts & SDHC_NIS_REMOVE) && level) { 16149ab747fSPaolo Bonzini /* Give target some time to notice card ejection */ 162bc72ad67SAlex Bligh timer_mod(s->insert_timer, 163bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY); 16449ab747fSPaolo Bonzini } else { 16549ab747fSPaolo Bonzini if (level) { 16649ab747fSPaolo Bonzini s->prnsts = 0x1ff0000; 16749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_INSERT) { 16849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_INSERT; 16949ab747fSPaolo Bonzini } 17049ab747fSPaolo Bonzini } else { 17149ab747fSPaolo Bonzini s->prnsts = 0x1fa0000; 17249ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 17349ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_SDCLK_EN; 17449ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_REMOVE) { 17549ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_REMOVE; 17649ab747fSPaolo Bonzini } 17749ab747fSPaolo Bonzini } 17849ab747fSPaolo Bonzini sdhci_update_irq(s); 17949ab747fSPaolo Bonzini } 18049ab747fSPaolo Bonzini } 18149ab747fSPaolo Bonzini 18240bbc194SPeter Maydell static void sdhci_set_readonly(DeviceState *dev, bool level) 18349ab747fSPaolo Bonzini { 18440bbc194SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 18549ab747fSPaolo Bonzini 18649ab747fSPaolo Bonzini if (level) { 18749ab747fSPaolo Bonzini s->prnsts &= ~SDHC_WRITE_PROTECT; 18849ab747fSPaolo Bonzini } else { 18949ab747fSPaolo Bonzini /* Write enabled */ 19049ab747fSPaolo Bonzini s->prnsts |= SDHC_WRITE_PROTECT; 19149ab747fSPaolo Bonzini } 19249ab747fSPaolo Bonzini } 19349ab747fSPaolo Bonzini 19449ab747fSPaolo Bonzini static void sdhci_reset(SDHCIState *s) 19549ab747fSPaolo Bonzini { 19640bbc194SPeter Maydell DeviceState *dev = DEVICE(s); 19740bbc194SPeter Maydell 198bc72ad67SAlex Bligh timer_del(s->insert_timer); 199bc72ad67SAlex Bligh timer_del(s->transfer_timer); 20049ab747fSPaolo Bonzini /* Set all registers to 0. Capabilities registers are not cleared 20149ab747fSPaolo Bonzini * and assumed to always preserve their value, given to them during 20249ab747fSPaolo Bonzini * initialization */ 20349ab747fSPaolo Bonzini memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad); 20449ab747fSPaolo Bonzini 20540bbc194SPeter Maydell /* Reset other state based on current card insertion/readonly status */ 20640bbc194SPeter Maydell sdhci_set_inserted(dev, sdbus_get_inserted(&s->sdbus)); 20740bbc194SPeter Maydell sdhci_set_readonly(dev, sdbus_get_readonly(&s->sdbus)); 20840bbc194SPeter Maydell 20949ab747fSPaolo Bonzini s->data_count = 0; 21049ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 2110a7ac9f9SAndrew Baumann s->pending_insert_state = false; 21249ab747fSPaolo Bonzini } 21349ab747fSPaolo Bonzini 2148b41c305SPeter Maydell static void sdhci_poweron_reset(DeviceState *dev) 2158b41c305SPeter Maydell { 2168b41c305SPeter Maydell /* QOM (ie power-on) reset. This is identical to reset 2178b41c305SPeter Maydell * commanded via device register apart from handling of the 2188b41c305SPeter Maydell * 'pending insert on powerup' quirk. 2198b41c305SPeter Maydell */ 2208b41c305SPeter Maydell SDHCIState *s = (SDHCIState *)dev; 2218b41c305SPeter Maydell 2228b41c305SPeter Maydell sdhci_reset(s); 2238b41c305SPeter Maydell 2248b41c305SPeter Maydell if (s->pending_insert_quirk) { 2258b41c305SPeter Maydell s->pending_insert_state = true; 2268b41c305SPeter Maydell } 2278b41c305SPeter Maydell } 2288b41c305SPeter Maydell 229d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque); 23049ab747fSPaolo Bonzini 23149ab747fSPaolo Bonzini static void sdhci_send_command(SDHCIState *s) 23249ab747fSPaolo Bonzini { 23349ab747fSPaolo Bonzini SDRequest request; 23449ab747fSPaolo Bonzini uint8_t response[16]; 23549ab747fSPaolo Bonzini int rlen; 23649ab747fSPaolo Bonzini 23749ab747fSPaolo Bonzini s->errintsts = 0; 23849ab747fSPaolo Bonzini s->acmd12errsts = 0; 23949ab747fSPaolo Bonzini request.cmd = s->cmdreg >> 8; 24049ab747fSPaolo Bonzini request.arg = s->argument; 24149ab747fSPaolo Bonzini DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request.cmd, request.arg); 24240bbc194SPeter Maydell rlen = sdbus_do_command(&s->sdbus, &request, response); 24349ab747fSPaolo Bonzini 24449ab747fSPaolo Bonzini if (s->cmdreg & SDHC_CMD_RESPONSE) { 24549ab747fSPaolo Bonzini if (rlen == 4) { 24649ab747fSPaolo Bonzini s->rspreg[0] = (response[0] << 24) | (response[1] << 16) | 24749ab747fSPaolo Bonzini (response[2] << 8) | response[3]; 24849ab747fSPaolo Bonzini s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0; 24949ab747fSPaolo Bonzini DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s->rspreg[0]); 25049ab747fSPaolo Bonzini } else if (rlen == 16) { 25149ab747fSPaolo Bonzini s->rspreg[0] = (response[11] << 24) | (response[12] << 16) | 25249ab747fSPaolo Bonzini (response[13] << 8) | response[14]; 25349ab747fSPaolo Bonzini s->rspreg[1] = (response[7] << 24) | (response[8] << 16) | 25449ab747fSPaolo Bonzini (response[9] << 8) | response[10]; 25549ab747fSPaolo Bonzini s->rspreg[2] = (response[3] << 24) | (response[4] << 16) | 25649ab747fSPaolo Bonzini (response[5] << 8) | response[6]; 25749ab747fSPaolo Bonzini s->rspreg[3] = (response[0] << 16) | (response[1] << 8) | 25849ab747fSPaolo Bonzini response[2]; 25949ab747fSPaolo Bonzini DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.." 26049ab747fSPaolo Bonzini "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n", 26149ab747fSPaolo Bonzini s->rspreg[3], s->rspreg[2], s->rspreg[1], s->rspreg[0]); 26249ab747fSPaolo Bonzini } else { 26349ab747fSPaolo Bonzini ERRPRINT("Timeout waiting for command response\n"); 26449ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT) { 26549ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMDTIMEOUT; 26649ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 26749ab747fSPaolo Bonzini } 26849ab747fSPaolo Bonzini } 26949ab747fSPaolo Bonzini 27049ab747fSPaolo Bonzini if ((s->norintstsen & SDHC_NISEN_TRSCMP) && 27149ab747fSPaolo Bonzini (s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY) { 27249ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 27349ab747fSPaolo Bonzini } 27449ab747fSPaolo Bonzini } 27549ab747fSPaolo Bonzini 27649ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CMDCMP) { 27749ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_CMDCMP; 27849ab747fSPaolo Bonzini } 27949ab747fSPaolo Bonzini 28049ab747fSPaolo Bonzini sdhci_update_irq(s); 28149ab747fSPaolo Bonzini 28249ab747fSPaolo Bonzini if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT)) { 283656f416cSPeter Crosthwaite s->data_count = 0; 284d368ba43SKevin O'Connor sdhci_data_transfer(s); 28549ab747fSPaolo Bonzini } 28649ab747fSPaolo Bonzini } 28749ab747fSPaolo Bonzini 28849ab747fSPaolo Bonzini static void sdhci_end_transfer(SDHCIState *s) 28949ab747fSPaolo Bonzini { 29049ab747fSPaolo Bonzini /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */ 29149ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_ACMD12) != 0) { 29249ab747fSPaolo Bonzini SDRequest request; 29349ab747fSPaolo Bonzini uint8_t response[16]; 29449ab747fSPaolo Bonzini 29549ab747fSPaolo Bonzini request.cmd = 0x0C; 29649ab747fSPaolo Bonzini request.arg = 0; 29749ab747fSPaolo Bonzini DPRINT_L1("Automatically issue CMD%d %08x\n", request.cmd, request.arg); 29840bbc194SPeter Maydell sdbus_do_command(&s->sdbus, &request, response); 29949ab747fSPaolo Bonzini /* Auto CMD12 response goes to the upper Response register */ 30049ab747fSPaolo Bonzini s->rspreg[3] = (response[0] << 24) | (response[1] << 16) | 30149ab747fSPaolo Bonzini (response[2] << 8) | response[3]; 30249ab747fSPaolo Bonzini } 30349ab747fSPaolo Bonzini 30449ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_DOING_READ | SDHC_DOING_WRITE | 30549ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE | SDHC_DATA_INHIBIT | 30649ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE); 30749ab747fSPaolo Bonzini 30849ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_TRSCMP) { 30949ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_TRSCMP; 31049ab747fSPaolo Bonzini } 31149ab747fSPaolo Bonzini 31249ab747fSPaolo Bonzini sdhci_update_irq(s); 31349ab747fSPaolo Bonzini } 31449ab747fSPaolo Bonzini 31549ab747fSPaolo Bonzini /* 31649ab747fSPaolo Bonzini * Programmed i/o data transfer 31749ab747fSPaolo Bonzini */ 31849ab747fSPaolo Bonzini 31949ab747fSPaolo Bonzini /* Fill host controller's read buffer with BLKSIZE bytes of data from card */ 32049ab747fSPaolo Bonzini static void sdhci_read_block_from_card(SDHCIState *s) 32149ab747fSPaolo Bonzini { 32249ab747fSPaolo Bonzini int index = 0; 32349ab747fSPaolo Bonzini 32449ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) && 32549ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) { 32649ab747fSPaolo Bonzini return; 32749ab747fSPaolo Bonzini } 32849ab747fSPaolo Bonzini 32949ab747fSPaolo Bonzini for (index = 0; index < (s->blksize & 0x0fff); index++) { 33040bbc194SPeter Maydell s->fifo_buffer[index] = sdbus_read_data(&s->sdbus); 33149ab747fSPaolo Bonzini } 33249ab747fSPaolo Bonzini 33349ab747fSPaolo Bonzini /* New data now available for READ through Buffer Port Register */ 33449ab747fSPaolo Bonzini s->prnsts |= SDHC_DATA_AVAILABLE; 33549ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_RBUFRDY) { 33649ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_RBUFRDY; 33749ab747fSPaolo Bonzini } 33849ab747fSPaolo Bonzini 33949ab747fSPaolo Bonzini /* Clear DAT line active status if that was the last block */ 34049ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 34149ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && s->blkcnt == 1)) { 34249ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 34349ab747fSPaolo Bonzini } 34449ab747fSPaolo Bonzini 34549ab747fSPaolo Bonzini /* If stop at block gap request was set and it's not the last block of 34649ab747fSPaolo Bonzini * data - generate Block Event interrupt */ 34749ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI) && 34849ab747fSPaolo Bonzini s->blkcnt != 1) { 34949ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DAT_LINE_ACTIVE; 35049ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 35149ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 35249ab747fSPaolo Bonzini } 35349ab747fSPaolo Bonzini } 35449ab747fSPaolo Bonzini 35549ab747fSPaolo Bonzini sdhci_update_irq(s); 35649ab747fSPaolo Bonzini } 35749ab747fSPaolo Bonzini 35849ab747fSPaolo Bonzini /* Read @size byte of data from host controller @s BUFFER DATA PORT register */ 35949ab747fSPaolo Bonzini static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size) 36049ab747fSPaolo Bonzini { 36149ab747fSPaolo Bonzini uint32_t value = 0; 36249ab747fSPaolo Bonzini int i; 36349ab747fSPaolo Bonzini 36449ab747fSPaolo Bonzini /* first check that a valid data exists in host controller input buffer */ 36549ab747fSPaolo Bonzini if ((s->prnsts & SDHC_DATA_AVAILABLE) == 0) { 36649ab747fSPaolo Bonzini ERRPRINT("Trying to read from empty buffer\n"); 36749ab747fSPaolo Bonzini return 0; 36849ab747fSPaolo Bonzini } 36949ab747fSPaolo Bonzini 37049ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 37149ab747fSPaolo Bonzini value |= s->fifo_buffer[s->data_count] << i * 8; 37249ab747fSPaolo Bonzini s->data_count++; 37349ab747fSPaolo Bonzini /* check if we've read all valid data (blksize bytes) from buffer */ 37449ab747fSPaolo Bonzini if ((s->data_count) >= (s->blksize & 0x0fff)) { 37549ab747fSPaolo Bonzini DPRINT_L2("All %u bytes of data have been read from input buffer\n", 37649ab747fSPaolo Bonzini s->data_count); 37749ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DATA_AVAILABLE; /* no more data in a buffer */ 37849ab747fSPaolo Bonzini s->data_count = 0; /* next buff read must start at position [0] */ 37949ab747fSPaolo Bonzini 38049ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 38149ab747fSPaolo Bonzini s->blkcnt--; 38249ab747fSPaolo Bonzini } 38349ab747fSPaolo Bonzini 38449ab747fSPaolo Bonzini /* if that was the last block of data */ 38549ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 38649ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0)) || 38749ab747fSPaolo Bonzini /* stop at gap request */ 38849ab747fSPaolo Bonzini (s->stopped_state == sdhc_gap_read && 38949ab747fSPaolo Bonzini !(s->prnsts & SDHC_DAT_LINE_ACTIVE))) { 390d368ba43SKevin O'Connor sdhci_end_transfer(s); 39149ab747fSPaolo Bonzini } else { /* if there are more data, read next block from card */ 392d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 39349ab747fSPaolo Bonzini } 39449ab747fSPaolo Bonzini break; 39549ab747fSPaolo Bonzini } 39649ab747fSPaolo Bonzini } 39749ab747fSPaolo Bonzini 39849ab747fSPaolo Bonzini return value; 39949ab747fSPaolo Bonzini } 40049ab747fSPaolo Bonzini 40149ab747fSPaolo Bonzini /* Write data from host controller FIFO to card */ 40249ab747fSPaolo Bonzini static void sdhci_write_block_to_card(SDHCIState *s) 40349ab747fSPaolo Bonzini { 40449ab747fSPaolo Bonzini int index = 0; 40549ab747fSPaolo Bonzini 40649ab747fSPaolo Bonzini if (s->prnsts & SDHC_SPACE_AVAILABLE) { 40749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 40849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_WBUFRDY; 40949ab747fSPaolo Bonzini } 41049ab747fSPaolo Bonzini sdhci_update_irq(s); 41149ab747fSPaolo Bonzini return; 41249ab747fSPaolo Bonzini } 41349ab747fSPaolo Bonzini 41449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 41549ab747fSPaolo Bonzini if (s->blkcnt == 0) { 41649ab747fSPaolo Bonzini return; 41749ab747fSPaolo Bonzini } else { 41849ab747fSPaolo Bonzini s->blkcnt--; 41949ab747fSPaolo Bonzini } 42049ab747fSPaolo Bonzini } 42149ab747fSPaolo Bonzini 42249ab747fSPaolo Bonzini for (index = 0; index < (s->blksize & 0x0fff); index++) { 42340bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[index]); 42449ab747fSPaolo Bonzini } 42549ab747fSPaolo Bonzini 42649ab747fSPaolo Bonzini /* Next data can be written through BUFFER DATORT register */ 42749ab747fSPaolo Bonzini s->prnsts |= SDHC_SPACE_AVAILABLE; 42849ab747fSPaolo Bonzini 42949ab747fSPaolo Bonzini /* Finish transfer if that was the last block of data */ 43049ab747fSPaolo Bonzini if ((s->trnmod & SDHC_TRNS_MULTI) == 0 || 43149ab747fSPaolo Bonzini ((s->trnmod & SDHC_TRNS_MULTI) && 43249ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && (s->blkcnt == 0))) { 433d368ba43SKevin O'Connor sdhci_end_transfer(s); 434dcdb4cd8SPeter Crosthwaite } else if (s->norintstsen & SDHC_NISEN_WBUFRDY) { 435dcdb4cd8SPeter Crosthwaite s->norintsts |= SDHC_NIS_WBUFRDY; 43649ab747fSPaolo Bonzini } 43749ab747fSPaolo Bonzini 43849ab747fSPaolo Bonzini /* Generate Block Gap Event if requested and if not the last block */ 43949ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI) && 44049ab747fSPaolo Bonzini s->blkcnt > 0) { 44149ab747fSPaolo Bonzini s->prnsts &= ~SDHC_DOING_WRITE; 44249ab747fSPaolo Bonzini if (s->norintstsen & SDHC_EISEN_BLKGAP) { 44349ab747fSPaolo Bonzini s->norintsts |= SDHC_EIS_BLKGAP; 44449ab747fSPaolo Bonzini } 445d368ba43SKevin O'Connor sdhci_end_transfer(s); 44649ab747fSPaolo Bonzini } 44749ab747fSPaolo Bonzini 44849ab747fSPaolo Bonzini sdhci_update_irq(s); 44949ab747fSPaolo Bonzini } 45049ab747fSPaolo Bonzini 45149ab747fSPaolo Bonzini /* Write @size bytes of @value data to host controller @s Buffer Data Port 45249ab747fSPaolo Bonzini * register */ 45349ab747fSPaolo Bonzini static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size) 45449ab747fSPaolo Bonzini { 45549ab747fSPaolo Bonzini unsigned i; 45649ab747fSPaolo Bonzini 45749ab747fSPaolo Bonzini /* Check that there is free space left in a buffer */ 45849ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_SPACE_AVAILABLE)) { 45949ab747fSPaolo Bonzini ERRPRINT("Can't write to data buffer: buffer full\n"); 46049ab747fSPaolo Bonzini return; 46149ab747fSPaolo Bonzini } 46249ab747fSPaolo Bonzini 46349ab747fSPaolo Bonzini for (i = 0; i < size; i++) { 46449ab747fSPaolo Bonzini s->fifo_buffer[s->data_count] = value & 0xFF; 46549ab747fSPaolo Bonzini s->data_count++; 46649ab747fSPaolo Bonzini value >>= 8; 46749ab747fSPaolo Bonzini if (s->data_count >= (s->blksize & 0x0fff)) { 46849ab747fSPaolo Bonzini DPRINT_L2("write buffer filled with %u bytes of data\n", 46949ab747fSPaolo Bonzini s->data_count); 47049ab747fSPaolo Bonzini s->data_count = 0; 47149ab747fSPaolo Bonzini s->prnsts &= ~SDHC_SPACE_AVAILABLE; 47249ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_WRITE) { 473d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 47449ab747fSPaolo Bonzini } 47549ab747fSPaolo Bonzini } 47649ab747fSPaolo Bonzini } 47749ab747fSPaolo Bonzini } 47849ab747fSPaolo Bonzini 47949ab747fSPaolo Bonzini /* 48049ab747fSPaolo Bonzini * Single DMA data transfer 48149ab747fSPaolo Bonzini */ 48249ab747fSPaolo Bonzini 48349ab747fSPaolo Bonzini /* Multi block SDMA transfer */ 48449ab747fSPaolo Bonzini static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s) 48549ab747fSPaolo Bonzini { 48649ab747fSPaolo Bonzini bool page_aligned = false; 48749ab747fSPaolo Bonzini unsigned int n, begin; 48849ab747fSPaolo Bonzini const uint16_t block_size = s->blksize & 0x0fff; 48949ab747fSPaolo Bonzini uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12); 49049ab747fSPaolo Bonzini uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk); 49149ab747fSPaolo Bonzini 4926e86d903SPrasad J Pandit if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) { 4936e86d903SPrasad J Pandit qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n"); 4946e86d903SPrasad J Pandit return; 4956e86d903SPrasad J Pandit } 4966e86d903SPrasad J Pandit 49749ab747fSPaolo Bonzini /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for 49849ab747fSPaolo Bonzini * possible stop at page boundary if initial address is not page aligned, 49949ab747fSPaolo Bonzini * allow them to work properly */ 50049ab747fSPaolo Bonzini if ((s->sdmasysad % boundary_chk) == 0) { 50149ab747fSPaolo Bonzini page_aligned = true; 50249ab747fSPaolo Bonzini } 50349ab747fSPaolo Bonzini 50449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 50549ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT | 50649ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 50749ab747fSPaolo Bonzini while (s->blkcnt) { 50849ab747fSPaolo Bonzini if (s->data_count == 0) { 50949ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 51040bbc194SPeter Maydell s->fifo_buffer[n] = sdbus_read_data(&s->sdbus); 51149ab747fSPaolo Bonzini } 51249ab747fSPaolo Bonzini } 51349ab747fSPaolo Bonzini begin = s->data_count; 51449ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 51549ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 51649ab747fSPaolo Bonzini boundary_count = 0; 51749ab747fSPaolo Bonzini } else { 51849ab747fSPaolo Bonzini s->data_count = block_size; 51949ab747fSPaolo Bonzini boundary_count -= block_size - begin; 52049ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 52149ab747fSPaolo Bonzini s->blkcnt--; 52249ab747fSPaolo Bonzini } 52349ab747fSPaolo Bonzini } 524df32fd1cSPaolo Bonzini dma_memory_write(&address_space_memory, s->sdmasysad, 52549ab747fSPaolo Bonzini &s->fifo_buffer[begin], s->data_count - begin); 52649ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 52749ab747fSPaolo Bonzini if (s->data_count == block_size) { 52849ab747fSPaolo Bonzini s->data_count = 0; 52949ab747fSPaolo Bonzini } 53049ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 53149ab747fSPaolo Bonzini break; 53249ab747fSPaolo Bonzini } 53349ab747fSPaolo Bonzini } 53449ab747fSPaolo Bonzini } else { 53549ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_WRITE | SDHC_DATA_INHIBIT | 53649ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 53749ab747fSPaolo Bonzini while (s->blkcnt) { 53849ab747fSPaolo Bonzini begin = s->data_count; 53949ab747fSPaolo Bonzini if (((boundary_count + begin) < block_size) && page_aligned) { 54049ab747fSPaolo Bonzini s->data_count = boundary_count + begin; 54149ab747fSPaolo Bonzini boundary_count = 0; 54249ab747fSPaolo Bonzini } else { 54349ab747fSPaolo Bonzini s->data_count = block_size; 54449ab747fSPaolo Bonzini boundary_count -= block_size - begin; 54549ab747fSPaolo Bonzini } 546df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, s->sdmasysad, 54742922105SPrasad J Pandit &s->fifo_buffer[begin], s->data_count - begin); 54849ab747fSPaolo Bonzini s->sdmasysad += s->data_count - begin; 54949ab747fSPaolo Bonzini if (s->data_count == block_size) { 55049ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 55140bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[n]); 55249ab747fSPaolo Bonzini } 55349ab747fSPaolo Bonzini s->data_count = 0; 55449ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 55549ab747fSPaolo Bonzini s->blkcnt--; 55649ab747fSPaolo Bonzini } 55749ab747fSPaolo Bonzini } 55849ab747fSPaolo Bonzini if (page_aligned && boundary_count == 0) { 55949ab747fSPaolo Bonzini break; 56049ab747fSPaolo Bonzini } 56149ab747fSPaolo Bonzini } 56249ab747fSPaolo Bonzini } 56349ab747fSPaolo Bonzini 56449ab747fSPaolo Bonzini if (s->blkcnt == 0) { 565d368ba43SKevin O'Connor sdhci_end_transfer(s); 56649ab747fSPaolo Bonzini } else { 56749ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_DMA) { 56849ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_DMA; 56949ab747fSPaolo Bonzini } 57049ab747fSPaolo Bonzini sdhci_update_irq(s); 57149ab747fSPaolo Bonzini } 57249ab747fSPaolo Bonzini } 57349ab747fSPaolo Bonzini 57449ab747fSPaolo Bonzini /* single block SDMA transfer */ 57549ab747fSPaolo Bonzini static void sdhci_sdma_transfer_single_block(SDHCIState *s) 57649ab747fSPaolo Bonzini { 57749ab747fSPaolo Bonzini int n; 57849ab747fSPaolo Bonzini uint32_t datacnt = s->blksize & 0x0fff; 57949ab747fSPaolo Bonzini 58049ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 58149ab747fSPaolo Bonzini for (n = 0; n < datacnt; n++) { 58240bbc194SPeter Maydell s->fifo_buffer[n] = sdbus_read_data(&s->sdbus); 58349ab747fSPaolo Bonzini } 584df32fd1cSPaolo Bonzini dma_memory_write(&address_space_memory, s->sdmasysad, s->fifo_buffer, 58549ab747fSPaolo Bonzini datacnt); 58649ab747fSPaolo Bonzini } else { 587df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, s->sdmasysad, s->fifo_buffer, 58849ab747fSPaolo Bonzini datacnt); 58949ab747fSPaolo Bonzini for (n = 0; n < datacnt; n++) { 59040bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[n]); 59149ab747fSPaolo Bonzini } 59249ab747fSPaolo Bonzini } 59349ab747fSPaolo Bonzini s->blkcnt--; 59449ab747fSPaolo Bonzini 595d368ba43SKevin O'Connor sdhci_end_transfer(s); 59649ab747fSPaolo Bonzini } 59749ab747fSPaolo Bonzini 59849ab747fSPaolo Bonzini typedef struct ADMADescr { 59949ab747fSPaolo Bonzini hwaddr addr; 60049ab747fSPaolo Bonzini uint16_t length; 60149ab747fSPaolo Bonzini uint8_t attr; 60249ab747fSPaolo Bonzini uint8_t incr; 60349ab747fSPaolo Bonzini } ADMADescr; 60449ab747fSPaolo Bonzini 60549ab747fSPaolo Bonzini static void get_adma_description(SDHCIState *s, ADMADescr *dscr) 60649ab747fSPaolo Bonzini { 60749ab747fSPaolo Bonzini uint32_t adma1 = 0; 60849ab747fSPaolo Bonzini uint64_t adma2 = 0; 60949ab747fSPaolo Bonzini hwaddr entry_addr = (hwaddr)s->admasysaddr; 61049ab747fSPaolo Bonzini switch (SDHC_DMA_TYPE(s->hostctl)) { 61149ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 612df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma2, 61349ab747fSPaolo Bonzini sizeof(adma2)); 61449ab747fSPaolo Bonzini adma2 = le64_to_cpu(adma2); 61549ab747fSPaolo Bonzini /* The spec does not specify endianness of descriptor table. 61649ab747fSPaolo Bonzini * We currently assume that it is LE. 61749ab747fSPaolo Bonzini */ 61849ab747fSPaolo Bonzini dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull; 61949ab747fSPaolo Bonzini dscr->length = (uint16_t)extract64(adma2, 16, 16); 62049ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract64(adma2, 0, 7); 62149ab747fSPaolo Bonzini dscr->incr = 8; 62249ab747fSPaolo Bonzini break; 62349ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 624df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma1, 62549ab747fSPaolo Bonzini sizeof(adma1)); 62649ab747fSPaolo Bonzini adma1 = le32_to_cpu(adma1); 62749ab747fSPaolo Bonzini dscr->addr = (hwaddr)(adma1 & 0xFFFFF000); 62849ab747fSPaolo Bonzini dscr->attr = (uint8_t)extract32(adma1, 0, 7); 62949ab747fSPaolo Bonzini dscr->incr = 4; 63049ab747fSPaolo Bonzini if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) { 63149ab747fSPaolo Bonzini dscr->length = (uint16_t)extract32(adma1, 12, 16); 63249ab747fSPaolo Bonzini } else { 63349ab747fSPaolo Bonzini dscr->length = 4096; 63449ab747fSPaolo Bonzini } 63549ab747fSPaolo Bonzini break; 63649ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 637df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr, 63849ab747fSPaolo Bonzini (uint8_t *)(&dscr->attr), 1); 639df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr + 2, 64049ab747fSPaolo Bonzini (uint8_t *)(&dscr->length), 2); 64149ab747fSPaolo Bonzini dscr->length = le16_to_cpu(dscr->length); 642df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, entry_addr + 4, 64349ab747fSPaolo Bonzini (uint8_t *)(&dscr->addr), 8); 64449ab747fSPaolo Bonzini dscr->attr = le64_to_cpu(dscr->attr); 64549ab747fSPaolo Bonzini dscr->attr &= 0xfffffff8; 64649ab747fSPaolo Bonzini dscr->incr = 12; 64749ab747fSPaolo Bonzini break; 64849ab747fSPaolo Bonzini } 64949ab747fSPaolo Bonzini } 65049ab747fSPaolo Bonzini 65149ab747fSPaolo Bonzini /* Advanced DMA data transfer */ 65249ab747fSPaolo Bonzini 65349ab747fSPaolo Bonzini static void sdhci_do_adma(SDHCIState *s) 65449ab747fSPaolo Bonzini { 65549ab747fSPaolo Bonzini unsigned int n, begin, length; 65649ab747fSPaolo Bonzini const uint16_t block_size = s->blksize & 0x0fff; 65749ab747fSPaolo Bonzini ADMADescr dscr; 65849ab747fSPaolo Bonzini int i; 65949ab747fSPaolo Bonzini 66049ab747fSPaolo Bonzini for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) { 66149ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH; 66249ab747fSPaolo Bonzini 66349ab747fSPaolo Bonzini get_adma_description(s, &dscr); 66449ab747fSPaolo Bonzini DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx ", len=%d, attr=%x\n", 66549ab747fSPaolo Bonzini dscr.addr, dscr.length, dscr.attr); 66649ab747fSPaolo Bonzini 66749ab747fSPaolo Bonzini if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) { 66849ab747fSPaolo Bonzini /* Indicate that error occurred in ST_FDS state */ 66949ab747fSPaolo Bonzini s->admaerr &= ~SDHC_ADMAERR_STATE_MASK; 67049ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS; 67149ab747fSPaolo Bonzini 67249ab747fSPaolo Bonzini /* Generate ADMA error interrupt */ 67349ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 67449ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 67549ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 67649ab747fSPaolo Bonzini } 67749ab747fSPaolo Bonzini 67849ab747fSPaolo Bonzini sdhci_update_irq(s); 67949ab747fSPaolo Bonzini return; 68049ab747fSPaolo Bonzini } 68149ab747fSPaolo Bonzini 68249ab747fSPaolo Bonzini length = dscr.length ? dscr.length : 65536; 68349ab747fSPaolo Bonzini 68449ab747fSPaolo Bonzini switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) { 68549ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_TRAN: /* data transfer */ 68649ab747fSPaolo Bonzini 68749ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_READ) { 68849ab747fSPaolo Bonzini while (length) { 68949ab747fSPaolo Bonzini if (s->data_count == 0) { 69049ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 69140bbc194SPeter Maydell s->fifo_buffer[n] = sdbus_read_data(&s->sdbus); 69249ab747fSPaolo Bonzini } 69349ab747fSPaolo Bonzini } 69449ab747fSPaolo Bonzini begin = s->data_count; 69549ab747fSPaolo Bonzini if ((length + begin) < block_size) { 69649ab747fSPaolo Bonzini s->data_count = length + begin; 69749ab747fSPaolo Bonzini length = 0; 69849ab747fSPaolo Bonzini } else { 69949ab747fSPaolo Bonzini s->data_count = block_size; 70049ab747fSPaolo Bonzini length -= block_size - begin; 70149ab747fSPaolo Bonzini } 702df32fd1cSPaolo Bonzini dma_memory_write(&address_space_memory, dscr.addr, 70349ab747fSPaolo Bonzini &s->fifo_buffer[begin], 70449ab747fSPaolo Bonzini s->data_count - begin); 70549ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 70649ab747fSPaolo Bonzini if (s->data_count == block_size) { 70749ab747fSPaolo Bonzini s->data_count = 0; 70849ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 70949ab747fSPaolo Bonzini s->blkcnt--; 71049ab747fSPaolo Bonzini if (s->blkcnt == 0) { 71149ab747fSPaolo Bonzini break; 71249ab747fSPaolo Bonzini } 71349ab747fSPaolo Bonzini } 71449ab747fSPaolo Bonzini } 71549ab747fSPaolo Bonzini } 71649ab747fSPaolo Bonzini } else { 71749ab747fSPaolo Bonzini while (length) { 71849ab747fSPaolo Bonzini begin = s->data_count; 71949ab747fSPaolo Bonzini if ((length + begin) < block_size) { 72049ab747fSPaolo Bonzini s->data_count = length + begin; 72149ab747fSPaolo Bonzini length = 0; 72249ab747fSPaolo Bonzini } else { 72349ab747fSPaolo Bonzini s->data_count = block_size; 72449ab747fSPaolo Bonzini length -= block_size - begin; 72549ab747fSPaolo Bonzini } 726df32fd1cSPaolo Bonzini dma_memory_read(&address_space_memory, dscr.addr, 7279db11cefSPeter Crosthwaite &s->fifo_buffer[begin], 7289db11cefSPeter Crosthwaite s->data_count - begin); 72949ab747fSPaolo Bonzini dscr.addr += s->data_count - begin; 73049ab747fSPaolo Bonzini if (s->data_count == block_size) { 73149ab747fSPaolo Bonzini for (n = 0; n < block_size; n++) { 73240bbc194SPeter Maydell sdbus_write_data(&s->sdbus, s->fifo_buffer[n]); 73349ab747fSPaolo Bonzini } 73449ab747fSPaolo Bonzini s->data_count = 0; 73549ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) { 73649ab747fSPaolo Bonzini s->blkcnt--; 73749ab747fSPaolo Bonzini if (s->blkcnt == 0) { 73849ab747fSPaolo Bonzini break; 73949ab747fSPaolo Bonzini } 74049ab747fSPaolo Bonzini } 74149ab747fSPaolo Bonzini } 74249ab747fSPaolo Bonzini } 74349ab747fSPaolo Bonzini } 74449ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 74549ab747fSPaolo Bonzini break; 74649ab747fSPaolo Bonzini case SDHC_ADMA_ATTR_ACT_LINK: /* link to next descriptor table */ 74749ab747fSPaolo Bonzini s->admasysaddr = dscr.addr; 748be9c5ddeSSai Pavan Boddu DPRINT_L1("ADMA link: admasysaddr=0x%" PRIx64 "\n", 749be9c5ddeSSai Pavan Boddu s->admasysaddr); 75049ab747fSPaolo Bonzini break; 75149ab747fSPaolo Bonzini default: 75249ab747fSPaolo Bonzini s->admasysaddr += dscr.incr; 75349ab747fSPaolo Bonzini break; 75449ab747fSPaolo Bonzini } 75549ab747fSPaolo Bonzini 7561d32c26fSPeter Crosthwaite if (dscr.attr & SDHC_ADMA_ATTR_INT) { 757be9c5ddeSSai Pavan Boddu DPRINT_L1("ADMA interrupt: admasysaddr=0x%" PRIx64 "\n", 758be9c5ddeSSai Pavan Boddu s->admasysaddr); 7591d32c26fSPeter Crosthwaite if (s->norintstsen & SDHC_NISEN_DMA) { 7601d32c26fSPeter Crosthwaite s->norintsts |= SDHC_NIS_DMA; 7611d32c26fSPeter Crosthwaite } 7621d32c26fSPeter Crosthwaite 7631d32c26fSPeter Crosthwaite sdhci_update_irq(s); 7641d32c26fSPeter Crosthwaite } 7651d32c26fSPeter Crosthwaite 76649ab747fSPaolo Bonzini /* ADMA transfer terminates if blkcnt == 0 or by END attribute */ 76749ab747fSPaolo Bonzini if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 76849ab747fSPaolo Bonzini (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) { 76949ab747fSPaolo Bonzini DPRINT_L2("ADMA transfer completed\n"); 77049ab747fSPaolo Bonzini if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) && 77149ab747fSPaolo Bonzini (s->trnmod & SDHC_TRNS_BLK_CNT_EN) && 77249ab747fSPaolo Bonzini s->blkcnt != 0)) { 77349ab747fSPaolo Bonzini ERRPRINT("SD/MMC host ADMA length mismatch\n"); 77449ab747fSPaolo Bonzini s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH | 77549ab747fSPaolo Bonzini SDHC_ADMAERR_STATE_ST_TFR; 77649ab747fSPaolo Bonzini if (s->errintstsen & SDHC_EISEN_ADMAERR) { 77749ab747fSPaolo Bonzini ERRPRINT("Set ADMA error flag\n"); 77849ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_ADMAERR; 77949ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 78049ab747fSPaolo Bonzini } 78149ab747fSPaolo Bonzini 78249ab747fSPaolo Bonzini sdhci_update_irq(s); 78349ab747fSPaolo Bonzini } 784d368ba43SKevin O'Connor sdhci_end_transfer(s); 78549ab747fSPaolo Bonzini return; 78649ab747fSPaolo Bonzini } 78749ab747fSPaolo Bonzini 78849ab747fSPaolo Bonzini } 78949ab747fSPaolo Bonzini 79049ab747fSPaolo Bonzini /* we have unfinished business - reschedule to continue ADMA */ 791bc72ad67SAlex Bligh timer_mod(s->transfer_timer, 792bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY); 79349ab747fSPaolo Bonzini } 79449ab747fSPaolo Bonzini 79549ab747fSPaolo Bonzini /* Perform data transfer according to controller configuration */ 79649ab747fSPaolo Bonzini 797d368ba43SKevin O'Connor static void sdhci_data_transfer(void *opaque) 79849ab747fSPaolo Bonzini { 799d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 80049ab747fSPaolo Bonzini 80149ab747fSPaolo Bonzini if (s->trnmod & SDHC_TRNS_DMA) { 80249ab747fSPaolo Bonzini switch (SDHC_DMA_TYPE(s->hostctl)) { 80349ab747fSPaolo Bonzini case SDHC_CTRL_SDMA: 80449ab747fSPaolo Bonzini if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) { 805d368ba43SKevin O'Connor sdhci_sdma_transfer_single_block(s); 80649ab747fSPaolo Bonzini } else { 807d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 80849ab747fSPaolo Bonzini } 80949ab747fSPaolo Bonzini 81049ab747fSPaolo Bonzini break; 81149ab747fSPaolo Bonzini case SDHC_CTRL_ADMA1_32: 81249ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_ADMA1)) { 81349ab747fSPaolo Bonzini ERRPRINT("ADMA1 not supported\n"); 81449ab747fSPaolo Bonzini break; 81549ab747fSPaolo Bonzini } 81649ab747fSPaolo Bonzini 817d368ba43SKevin O'Connor sdhci_do_adma(s); 81849ab747fSPaolo Bonzini break; 81949ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_32: 82049ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_ADMA2)) { 82149ab747fSPaolo Bonzini ERRPRINT("ADMA2 not supported\n"); 82249ab747fSPaolo Bonzini break; 82349ab747fSPaolo Bonzini } 82449ab747fSPaolo Bonzini 825d368ba43SKevin O'Connor sdhci_do_adma(s); 82649ab747fSPaolo Bonzini break; 82749ab747fSPaolo Bonzini case SDHC_CTRL_ADMA2_64: 82849ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_ADMA2) || 82949ab747fSPaolo Bonzini !(s->capareg & SDHC_64_BIT_BUS_SUPPORT)) { 83049ab747fSPaolo Bonzini ERRPRINT("64 bit ADMA not supported\n"); 83149ab747fSPaolo Bonzini break; 83249ab747fSPaolo Bonzini } 83349ab747fSPaolo Bonzini 834d368ba43SKevin O'Connor sdhci_do_adma(s); 83549ab747fSPaolo Bonzini break; 83649ab747fSPaolo Bonzini default: 83749ab747fSPaolo Bonzini ERRPRINT("Unsupported DMA type\n"); 83849ab747fSPaolo Bonzini break; 83949ab747fSPaolo Bonzini } 84049ab747fSPaolo Bonzini } else { 84140bbc194SPeter Maydell if ((s->trnmod & SDHC_TRNS_READ) && sdbus_data_ready(&s->sdbus)) { 84249ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_READ | SDHC_DATA_INHIBIT | 84349ab747fSPaolo Bonzini SDHC_DAT_LINE_ACTIVE; 844d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 84549ab747fSPaolo Bonzini } else { 84649ab747fSPaolo Bonzini s->prnsts |= SDHC_DOING_WRITE | SDHC_DAT_LINE_ACTIVE | 84749ab747fSPaolo Bonzini SDHC_SPACE_AVAILABLE | SDHC_DATA_INHIBIT; 848d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 84949ab747fSPaolo Bonzini } 85049ab747fSPaolo Bonzini } 85149ab747fSPaolo Bonzini } 85249ab747fSPaolo Bonzini 85349ab747fSPaolo Bonzini static bool sdhci_can_issue_command(SDHCIState *s) 85449ab747fSPaolo Bonzini { 8556890a695SPeter Crosthwaite if (!SDHC_CLOCK_IS_ON(s->clkcon) || 85649ab747fSPaolo Bonzini (((s->prnsts & SDHC_DATA_INHIBIT) || s->stopped_state) && 85749ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_DATA_PRESENT) || 85849ab747fSPaolo Bonzini ((s->cmdreg & SDHC_CMD_RESPONSE) == SDHC_CMD_RSP_WITH_BUSY && 85949ab747fSPaolo Bonzini !(SDHC_COMMAND_TYPE(s->cmdreg) == SDHC_CMD_ABORT))))) { 86049ab747fSPaolo Bonzini return false; 86149ab747fSPaolo Bonzini } 86249ab747fSPaolo Bonzini 86349ab747fSPaolo Bonzini return true; 86449ab747fSPaolo Bonzini } 86549ab747fSPaolo Bonzini 86649ab747fSPaolo Bonzini /* The Buffer Data Port register must be accessed in sequential and 86749ab747fSPaolo Bonzini * continuous manner */ 86849ab747fSPaolo Bonzini static inline bool 86949ab747fSPaolo Bonzini sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num) 87049ab747fSPaolo Bonzini { 87149ab747fSPaolo Bonzini if ((s->data_count & 0x3) != byte_num) { 87249ab747fSPaolo Bonzini ERRPRINT("Non-sequential access to Buffer Data Port register" 87349ab747fSPaolo Bonzini "is prohibited\n"); 87449ab747fSPaolo Bonzini return false; 87549ab747fSPaolo Bonzini } 87649ab747fSPaolo Bonzini return true; 87749ab747fSPaolo Bonzini } 87849ab747fSPaolo Bonzini 879d368ba43SKevin O'Connor static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size) 88049ab747fSPaolo Bonzini { 881d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 88249ab747fSPaolo Bonzini uint32_t ret = 0; 88349ab747fSPaolo Bonzini 88449ab747fSPaolo Bonzini switch (offset & ~0x3) { 88549ab747fSPaolo Bonzini case SDHC_SYSAD: 88649ab747fSPaolo Bonzini ret = s->sdmasysad; 88749ab747fSPaolo Bonzini break; 88849ab747fSPaolo Bonzini case SDHC_BLKSIZE: 88949ab747fSPaolo Bonzini ret = s->blksize | (s->blkcnt << 16); 89049ab747fSPaolo Bonzini break; 89149ab747fSPaolo Bonzini case SDHC_ARGUMENT: 89249ab747fSPaolo Bonzini ret = s->argument; 89349ab747fSPaolo Bonzini break; 89449ab747fSPaolo Bonzini case SDHC_TRNMOD: 89549ab747fSPaolo Bonzini ret = s->trnmod | (s->cmdreg << 16); 89649ab747fSPaolo Bonzini break; 89749ab747fSPaolo Bonzini case SDHC_RSPREG0 ... SDHC_RSPREG3: 89849ab747fSPaolo Bonzini ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG0) >> 2]; 89949ab747fSPaolo Bonzini break; 90049ab747fSPaolo Bonzini case SDHC_BDATA: 90149ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 902d368ba43SKevin O'Connor ret = sdhci_read_dataport(s, size); 903d368ba43SKevin O'Connor DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset, 904677ff2aeSPeter Crosthwaite ret, ret); 90549ab747fSPaolo Bonzini return ret; 90649ab747fSPaolo Bonzini } 90749ab747fSPaolo Bonzini break; 90849ab747fSPaolo Bonzini case SDHC_PRNSTS: 90949ab747fSPaolo Bonzini ret = s->prnsts; 91049ab747fSPaolo Bonzini break; 91149ab747fSPaolo Bonzini case SDHC_HOSTCTL: 91249ab747fSPaolo Bonzini ret = s->hostctl | (s->pwrcon << 8) | (s->blkgap << 16) | 91349ab747fSPaolo Bonzini (s->wakcon << 24); 91449ab747fSPaolo Bonzini break; 91549ab747fSPaolo Bonzini case SDHC_CLKCON: 91649ab747fSPaolo Bonzini ret = s->clkcon | (s->timeoutcon << 16); 91749ab747fSPaolo Bonzini break; 91849ab747fSPaolo Bonzini case SDHC_NORINTSTS: 91949ab747fSPaolo Bonzini ret = s->norintsts | (s->errintsts << 16); 92049ab747fSPaolo Bonzini break; 92149ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 92249ab747fSPaolo Bonzini ret = s->norintstsen | (s->errintstsen << 16); 92349ab747fSPaolo Bonzini break; 92449ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 92549ab747fSPaolo Bonzini ret = s->norintsigen | (s->errintsigen << 16); 92649ab747fSPaolo Bonzini break; 92749ab747fSPaolo Bonzini case SDHC_ACMD12ERRSTS: 92849ab747fSPaolo Bonzini ret = s->acmd12errsts; 92949ab747fSPaolo Bonzini break; 93049ab747fSPaolo Bonzini case SDHC_CAPAREG: 93149ab747fSPaolo Bonzini ret = s->capareg; 93249ab747fSPaolo Bonzini break; 93349ab747fSPaolo Bonzini case SDHC_MAXCURR: 93449ab747fSPaolo Bonzini ret = s->maxcurr; 93549ab747fSPaolo Bonzini break; 93649ab747fSPaolo Bonzini case SDHC_ADMAERR: 93749ab747fSPaolo Bonzini ret = s->admaerr; 93849ab747fSPaolo Bonzini break; 93949ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 94049ab747fSPaolo Bonzini ret = (uint32_t)s->admasysaddr; 94149ab747fSPaolo Bonzini break; 94249ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 94349ab747fSPaolo Bonzini ret = (uint32_t)(s->admasysaddr >> 32); 94449ab747fSPaolo Bonzini break; 94549ab747fSPaolo Bonzini case SDHC_SLOT_INT_STATUS: 94649ab747fSPaolo Bonzini ret = (SD_HOST_SPECv2_VERS << 16) | sdhci_slotint(s); 94749ab747fSPaolo Bonzini break; 94849ab747fSPaolo Bonzini default: 949d368ba43SKevin O'Connor ERRPRINT("bad %ub read: addr[0x%04x]\n", size, (int)offset); 95049ab747fSPaolo Bonzini break; 95149ab747fSPaolo Bonzini } 95249ab747fSPaolo Bonzini 95349ab747fSPaolo Bonzini ret >>= (offset & 0x3) * 8; 95449ab747fSPaolo Bonzini ret &= (1ULL << (size * 8)) - 1; 955d368ba43SKevin O'Connor DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, (int)offset, ret, ret); 95649ab747fSPaolo Bonzini return ret; 95749ab747fSPaolo Bonzini } 95849ab747fSPaolo Bonzini 95949ab747fSPaolo Bonzini static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value) 96049ab747fSPaolo Bonzini { 96149ab747fSPaolo Bonzini if ((value & SDHC_STOP_AT_GAP_REQ) && (s->blkgap & SDHC_STOP_AT_GAP_REQ)) { 96249ab747fSPaolo Bonzini return; 96349ab747fSPaolo Bonzini } 96449ab747fSPaolo Bonzini s->blkgap = value & SDHC_STOP_AT_GAP_REQ; 96549ab747fSPaolo Bonzini 96649ab747fSPaolo Bonzini if ((value & SDHC_CONTINUE_REQ) && s->stopped_state && 96749ab747fSPaolo Bonzini (s->blkgap & SDHC_STOP_AT_GAP_REQ) == 0) { 96849ab747fSPaolo Bonzini if (s->stopped_state == sdhc_gap_read) { 96949ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_READ; 970d368ba43SKevin O'Connor sdhci_read_block_from_card(s); 97149ab747fSPaolo Bonzini } else { 97249ab747fSPaolo Bonzini s->prnsts |= SDHC_DAT_LINE_ACTIVE | SDHC_DOING_WRITE; 973d368ba43SKevin O'Connor sdhci_write_block_to_card(s); 97449ab747fSPaolo Bonzini } 97549ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 97649ab747fSPaolo Bonzini } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ)) { 97749ab747fSPaolo Bonzini if (s->prnsts & SDHC_DOING_READ) { 97849ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_read; 97949ab747fSPaolo Bonzini } else if (s->prnsts & SDHC_DOING_WRITE) { 98049ab747fSPaolo Bonzini s->stopped_state = sdhc_gap_write; 98149ab747fSPaolo Bonzini } 98249ab747fSPaolo Bonzini } 98349ab747fSPaolo Bonzini } 98449ab747fSPaolo Bonzini 98549ab747fSPaolo Bonzini static inline void sdhci_reset_write(SDHCIState *s, uint8_t value) 98649ab747fSPaolo Bonzini { 98749ab747fSPaolo Bonzini switch (value) { 98849ab747fSPaolo Bonzini case SDHC_RESET_ALL: 989d368ba43SKevin O'Connor sdhci_reset(s); 99049ab747fSPaolo Bonzini break; 99149ab747fSPaolo Bonzini case SDHC_RESET_CMD: 99249ab747fSPaolo Bonzini s->prnsts &= ~SDHC_CMD_INHIBIT; 99349ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_CMDCMP; 99449ab747fSPaolo Bonzini break; 99549ab747fSPaolo Bonzini case SDHC_RESET_DATA: 99649ab747fSPaolo Bonzini s->data_count = 0; 99749ab747fSPaolo Bonzini s->prnsts &= ~(SDHC_SPACE_AVAILABLE | SDHC_DATA_AVAILABLE | 99849ab747fSPaolo Bonzini SDHC_DOING_READ | SDHC_DOING_WRITE | 99949ab747fSPaolo Bonzini SDHC_DATA_INHIBIT | SDHC_DAT_LINE_ACTIVE); 100049ab747fSPaolo Bonzini s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ | SDHC_CONTINUE_REQ); 100149ab747fSPaolo Bonzini s->stopped_state = sdhc_not_stopped; 100249ab747fSPaolo Bonzini s->norintsts &= ~(SDHC_NIS_WBUFRDY | SDHC_NIS_RBUFRDY | 100349ab747fSPaolo Bonzini SDHC_NIS_DMA | SDHC_NIS_TRSCMP | SDHC_NIS_BLKGAP); 100449ab747fSPaolo Bonzini break; 100549ab747fSPaolo Bonzini } 100649ab747fSPaolo Bonzini } 100749ab747fSPaolo Bonzini 100849ab747fSPaolo Bonzini static void 1009d368ba43SKevin O'Connor sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) 101049ab747fSPaolo Bonzini { 1011d368ba43SKevin O'Connor SDHCIState *s = (SDHCIState *)opaque; 101249ab747fSPaolo Bonzini unsigned shift = 8 * (offset & 0x3); 101349ab747fSPaolo Bonzini uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift); 1014d368ba43SKevin O'Connor uint32_t value = val; 101549ab747fSPaolo Bonzini value <<= shift; 101649ab747fSPaolo Bonzini 101749ab747fSPaolo Bonzini switch (offset & ~0x3) { 101849ab747fSPaolo Bonzini case SDHC_SYSAD: 101949ab747fSPaolo Bonzini s->sdmasysad = (s->sdmasysad & mask) | value; 102049ab747fSPaolo Bonzini MASKED_WRITE(s->sdmasysad, mask, value); 102149ab747fSPaolo Bonzini /* Writing to last byte of sdmasysad might trigger transfer */ 102249ab747fSPaolo Bonzini if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt && 102349ab747fSPaolo Bonzini s->blksize && SDHC_DMA_TYPE(s->hostctl) == SDHC_CTRL_SDMA) { 102445ba9f76SPrasad J Pandit if (s->trnmod & SDHC_TRNS_MULTI) { 1025d368ba43SKevin O'Connor sdhci_sdma_transfer_multi_blocks(s); 102645ba9f76SPrasad J Pandit } else { 102745ba9f76SPrasad J Pandit sdhci_sdma_transfer_single_block(s); 102845ba9f76SPrasad J Pandit } 102949ab747fSPaolo Bonzini } 103049ab747fSPaolo Bonzini break; 103149ab747fSPaolo Bonzini case SDHC_BLKSIZE: 103249ab747fSPaolo Bonzini if (!TRANSFERRING_DATA(s->prnsts)) { 103349ab747fSPaolo Bonzini MASKED_WRITE(s->blksize, mask, value); 103449ab747fSPaolo Bonzini MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16); 103549ab747fSPaolo Bonzini } 10369201bb9aSAlistair Francis 10379201bb9aSAlistair Francis /* Limit block size to the maximum buffer size */ 10389201bb9aSAlistair Francis if (extract32(s->blksize, 0, 12) > s->buf_maxsz) { 10399201bb9aSAlistair Francis qemu_log_mask(LOG_GUEST_ERROR, "%s: Size 0x%x is larger than " \ 10409201bb9aSAlistair Francis "the maximum buffer 0x%x", __func__, s->blksize, 10419201bb9aSAlistair Francis s->buf_maxsz); 10429201bb9aSAlistair Francis 10439201bb9aSAlistair Francis s->blksize = deposit32(s->blksize, 0, 12, s->buf_maxsz); 10449201bb9aSAlistair Francis } 10459201bb9aSAlistair Francis 104649ab747fSPaolo Bonzini break; 104749ab747fSPaolo Bonzini case SDHC_ARGUMENT: 104849ab747fSPaolo Bonzini MASKED_WRITE(s->argument, mask, value); 104949ab747fSPaolo Bonzini break; 105049ab747fSPaolo Bonzini case SDHC_TRNMOD: 105149ab747fSPaolo Bonzini /* DMA can be enabled only if it is supported as indicated by 105249ab747fSPaolo Bonzini * capabilities register */ 105349ab747fSPaolo Bonzini if (!(s->capareg & SDHC_CAN_DO_DMA)) { 105449ab747fSPaolo Bonzini value &= ~SDHC_TRNS_DMA; 105549ab747fSPaolo Bonzini } 10568b20aefaSPrasad J Pandit MASKED_WRITE(s->trnmod, mask, value & MASK_TRNMOD); 105749ab747fSPaolo Bonzini MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16); 105849ab747fSPaolo Bonzini 105949ab747fSPaolo Bonzini /* Writing to the upper byte of CMDREG triggers SD command generation */ 1060d368ba43SKevin O'Connor if ((mask & 0xFF000000) || !sdhci_can_issue_command(s)) { 106149ab747fSPaolo Bonzini break; 106249ab747fSPaolo Bonzini } 106349ab747fSPaolo Bonzini 1064d368ba43SKevin O'Connor sdhci_send_command(s); 106549ab747fSPaolo Bonzini break; 106649ab747fSPaolo Bonzini case SDHC_BDATA: 106749ab747fSPaolo Bonzini if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA)) { 1068d368ba43SKevin O'Connor sdhci_write_dataport(s, value >> shift, size); 106949ab747fSPaolo Bonzini } 107049ab747fSPaolo Bonzini break; 107149ab747fSPaolo Bonzini case SDHC_HOSTCTL: 107249ab747fSPaolo Bonzini if (!(mask & 0xFF0000)) { 107349ab747fSPaolo Bonzini sdhci_blkgap_write(s, value >> 16); 107449ab747fSPaolo Bonzini } 107549ab747fSPaolo Bonzini MASKED_WRITE(s->hostctl, mask, value); 107649ab747fSPaolo Bonzini MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8); 107749ab747fSPaolo Bonzini MASKED_WRITE(s->wakcon, mask >> 24, value >> 24); 107849ab747fSPaolo Bonzini if (!(s->prnsts & SDHC_CARD_PRESENT) || ((s->pwrcon >> 1) & 0x7) < 5 || 107949ab747fSPaolo Bonzini !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) { 108049ab747fSPaolo Bonzini s->pwrcon &= ~SDHC_POWER_ON; 108149ab747fSPaolo Bonzini } 108249ab747fSPaolo Bonzini break; 108349ab747fSPaolo Bonzini case SDHC_CLKCON: 108449ab747fSPaolo Bonzini if (!(mask & 0xFF000000)) { 108549ab747fSPaolo Bonzini sdhci_reset_write(s, value >> 24); 108649ab747fSPaolo Bonzini } 108749ab747fSPaolo Bonzini MASKED_WRITE(s->clkcon, mask, value); 108849ab747fSPaolo Bonzini MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16); 108949ab747fSPaolo Bonzini if (s->clkcon & SDHC_CLOCK_INT_EN) { 109049ab747fSPaolo Bonzini s->clkcon |= SDHC_CLOCK_INT_STABLE; 109149ab747fSPaolo Bonzini } else { 109249ab747fSPaolo Bonzini s->clkcon &= ~SDHC_CLOCK_INT_STABLE; 109349ab747fSPaolo Bonzini } 109449ab747fSPaolo Bonzini break; 109549ab747fSPaolo Bonzini case SDHC_NORINTSTS: 109649ab747fSPaolo Bonzini if (s->norintstsen & SDHC_NISEN_CARDINT) { 109749ab747fSPaolo Bonzini value &= ~SDHC_NIS_CARDINT; 109849ab747fSPaolo Bonzini } 109949ab747fSPaolo Bonzini s->norintsts &= mask | ~value; 110049ab747fSPaolo Bonzini s->errintsts &= (mask >> 16) | ~(value >> 16); 110149ab747fSPaolo Bonzini if (s->errintsts) { 110249ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 110349ab747fSPaolo Bonzini } else { 110449ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 110549ab747fSPaolo Bonzini } 110649ab747fSPaolo Bonzini sdhci_update_irq(s); 110749ab747fSPaolo Bonzini break; 110849ab747fSPaolo Bonzini case SDHC_NORINTSTSEN: 110949ab747fSPaolo Bonzini MASKED_WRITE(s->norintstsen, mask, value); 111049ab747fSPaolo Bonzini MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16); 111149ab747fSPaolo Bonzini s->norintsts &= s->norintstsen; 111249ab747fSPaolo Bonzini s->errintsts &= s->errintstsen; 111349ab747fSPaolo Bonzini if (s->errintsts) { 111449ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 111549ab747fSPaolo Bonzini } else { 111649ab747fSPaolo Bonzini s->norintsts &= ~SDHC_NIS_ERR; 111749ab747fSPaolo Bonzini } 11180a7ac9f9SAndrew Baumann /* Quirk for Raspberry Pi: pending card insert interrupt 11190a7ac9f9SAndrew Baumann * appears when first enabled after power on */ 11200a7ac9f9SAndrew Baumann if ((s->norintstsen & SDHC_NISEN_INSERT) && s->pending_insert_state) { 11210a7ac9f9SAndrew Baumann assert(s->pending_insert_quirk); 11220a7ac9f9SAndrew Baumann s->norintsts |= SDHC_NIS_INSERT; 11230a7ac9f9SAndrew Baumann s->pending_insert_state = false; 11240a7ac9f9SAndrew Baumann } 112549ab747fSPaolo Bonzini sdhci_update_irq(s); 112649ab747fSPaolo Bonzini break; 112749ab747fSPaolo Bonzini case SDHC_NORINTSIGEN: 112849ab747fSPaolo Bonzini MASKED_WRITE(s->norintsigen, mask, value); 112949ab747fSPaolo Bonzini MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16); 113049ab747fSPaolo Bonzini sdhci_update_irq(s); 113149ab747fSPaolo Bonzini break; 113249ab747fSPaolo Bonzini case SDHC_ADMAERR: 113349ab747fSPaolo Bonzini MASKED_WRITE(s->admaerr, mask, value); 113449ab747fSPaolo Bonzini break; 113549ab747fSPaolo Bonzini case SDHC_ADMASYSADDR: 113649ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL | 113749ab747fSPaolo Bonzini (uint64_t)mask)) | (uint64_t)value; 113849ab747fSPaolo Bonzini break; 113949ab747fSPaolo Bonzini case SDHC_ADMASYSADDR + 4: 114049ab747fSPaolo Bonzini s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL | 114149ab747fSPaolo Bonzini ((uint64_t)mask << 32))) | ((uint64_t)value << 32); 114249ab747fSPaolo Bonzini break; 114349ab747fSPaolo Bonzini case SDHC_FEAER: 114449ab747fSPaolo Bonzini s->acmd12errsts |= value; 114549ab747fSPaolo Bonzini s->errintsts |= (value >> 16) & s->errintstsen; 114649ab747fSPaolo Bonzini if (s->acmd12errsts) { 114749ab747fSPaolo Bonzini s->errintsts |= SDHC_EIS_CMD12ERR; 114849ab747fSPaolo Bonzini } 114949ab747fSPaolo Bonzini if (s->errintsts) { 115049ab747fSPaolo Bonzini s->norintsts |= SDHC_NIS_ERR; 115149ab747fSPaolo Bonzini } 115249ab747fSPaolo Bonzini sdhci_update_irq(s); 115349ab747fSPaolo Bonzini break; 115449ab747fSPaolo Bonzini default: 115549ab747fSPaolo Bonzini ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n", 1156d368ba43SKevin O'Connor size, (int)offset, value >> shift, value >> shift); 115749ab747fSPaolo Bonzini break; 115849ab747fSPaolo Bonzini } 115949ab747fSPaolo Bonzini DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n", 1160d368ba43SKevin O'Connor size, (int)offset, value >> shift, value >> shift); 116149ab747fSPaolo Bonzini } 116249ab747fSPaolo Bonzini 116349ab747fSPaolo Bonzini static const MemoryRegionOps sdhci_mmio_ops = { 1164d368ba43SKevin O'Connor .read = sdhci_read, 1165d368ba43SKevin O'Connor .write = sdhci_write, 116649ab747fSPaolo Bonzini .valid = { 116749ab747fSPaolo Bonzini .min_access_size = 1, 116849ab747fSPaolo Bonzini .max_access_size = 4, 116949ab747fSPaolo Bonzini .unaligned = false 117049ab747fSPaolo Bonzini }, 117149ab747fSPaolo Bonzini .endianness = DEVICE_LITTLE_ENDIAN, 117249ab747fSPaolo Bonzini }; 117349ab747fSPaolo Bonzini 117449ab747fSPaolo Bonzini static inline unsigned int sdhci_get_fifolen(SDHCIState *s) 117549ab747fSPaolo Bonzini { 117649ab747fSPaolo Bonzini switch (SDHC_CAPAB_BLOCKSIZE(s->capareg)) { 117749ab747fSPaolo Bonzini case 0: 117849ab747fSPaolo Bonzini return 512; 117949ab747fSPaolo Bonzini case 1: 118049ab747fSPaolo Bonzini return 1024; 118149ab747fSPaolo Bonzini case 2: 118249ab747fSPaolo Bonzini return 2048; 118349ab747fSPaolo Bonzini default: 118449ab747fSPaolo Bonzini hw_error("SDHC: unsupported value for maximum block size\n"); 118549ab747fSPaolo Bonzini return 0; 118649ab747fSPaolo Bonzini } 118749ab747fSPaolo Bonzini } 118849ab747fSPaolo Bonzini 1189*b635d98cSPhilippe Mathieu-Daudé /* --- qdev common --- */ 1190*b635d98cSPhilippe Mathieu-Daudé 1191*b635d98cSPhilippe Mathieu-Daudé #define DEFINE_SDHCI_COMMON_PROPERTIES(_state) \ 1192*b635d98cSPhilippe Mathieu-Daudé /* Capabilities registers provide information on supported features 1193*b635d98cSPhilippe Mathieu-Daudé * of this specific host controller implementation */ \ 1194*b635d98cSPhilippe Mathieu-Daudé DEFINE_PROP_UINT32("capareg", _state, capareg, SDHC_CAPAB_REG_DEFAULT), \ 1195*b635d98cSPhilippe Mathieu-Daudé DEFINE_PROP_UINT32("maxcurr", _state, maxcurr, 0) 1196*b635d98cSPhilippe Mathieu-Daudé 119740bbc194SPeter Maydell static void sdhci_initfn(SDHCIState *s) 119849ab747fSPaolo Bonzini { 119940bbc194SPeter Maydell qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), 120040bbc194SPeter Maydell TYPE_SDHCI_BUS, DEVICE(s), "sd-bus"); 120149ab747fSPaolo Bonzini 1202bc72ad67SAlex Bligh s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s); 1203d368ba43SKevin O'Connor s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_data_transfer, s); 120449ab747fSPaolo Bonzini } 120549ab747fSPaolo Bonzini 12067302dcd6SKevin O'Connor static void sdhci_uninitfn(SDHCIState *s) 120749ab747fSPaolo Bonzini { 1208bc72ad67SAlex Bligh timer_del(s->insert_timer); 1209bc72ad67SAlex Bligh timer_free(s->insert_timer); 1210bc72ad67SAlex Bligh timer_del(s->transfer_timer); 1211bc72ad67SAlex Bligh timer_free(s->transfer_timer); 121249ab747fSPaolo Bonzini 121349ab747fSPaolo Bonzini g_free(s->fifo_buffer); 121449ab747fSPaolo Bonzini s->fifo_buffer = NULL; 121549ab747fSPaolo Bonzini } 121649ab747fSPaolo Bonzini 12170a7ac9f9SAndrew Baumann static bool sdhci_pending_insert_vmstate_needed(void *opaque) 12180a7ac9f9SAndrew Baumann { 12190a7ac9f9SAndrew Baumann SDHCIState *s = opaque; 12200a7ac9f9SAndrew Baumann 12210a7ac9f9SAndrew Baumann return s->pending_insert_state; 12220a7ac9f9SAndrew Baumann } 12230a7ac9f9SAndrew Baumann 12240a7ac9f9SAndrew Baumann static const VMStateDescription sdhci_pending_insert_vmstate = { 12250a7ac9f9SAndrew Baumann .name = "sdhci/pending-insert", 12260a7ac9f9SAndrew Baumann .version_id = 1, 12270a7ac9f9SAndrew Baumann .minimum_version_id = 1, 12280a7ac9f9SAndrew Baumann .needed = sdhci_pending_insert_vmstate_needed, 12290a7ac9f9SAndrew Baumann .fields = (VMStateField[]) { 12300a7ac9f9SAndrew Baumann VMSTATE_BOOL(pending_insert_state, SDHCIState), 12310a7ac9f9SAndrew Baumann VMSTATE_END_OF_LIST() 12320a7ac9f9SAndrew Baumann }, 12330a7ac9f9SAndrew Baumann }; 12340a7ac9f9SAndrew Baumann 123549ab747fSPaolo Bonzini const VMStateDescription sdhci_vmstate = { 123649ab747fSPaolo Bonzini .name = "sdhci", 123749ab747fSPaolo Bonzini .version_id = 1, 123849ab747fSPaolo Bonzini .minimum_version_id = 1, 123949ab747fSPaolo Bonzini .fields = (VMStateField[]) { 124049ab747fSPaolo Bonzini VMSTATE_UINT32(sdmasysad, SDHCIState), 124149ab747fSPaolo Bonzini VMSTATE_UINT16(blksize, SDHCIState), 124249ab747fSPaolo Bonzini VMSTATE_UINT16(blkcnt, SDHCIState), 124349ab747fSPaolo Bonzini VMSTATE_UINT32(argument, SDHCIState), 124449ab747fSPaolo Bonzini VMSTATE_UINT16(trnmod, SDHCIState), 124549ab747fSPaolo Bonzini VMSTATE_UINT16(cmdreg, SDHCIState), 124649ab747fSPaolo Bonzini VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4), 124749ab747fSPaolo Bonzini VMSTATE_UINT32(prnsts, SDHCIState), 124849ab747fSPaolo Bonzini VMSTATE_UINT8(hostctl, SDHCIState), 124949ab747fSPaolo Bonzini VMSTATE_UINT8(pwrcon, SDHCIState), 125049ab747fSPaolo Bonzini VMSTATE_UINT8(blkgap, SDHCIState), 125149ab747fSPaolo Bonzini VMSTATE_UINT8(wakcon, SDHCIState), 125249ab747fSPaolo Bonzini VMSTATE_UINT16(clkcon, SDHCIState), 125349ab747fSPaolo Bonzini VMSTATE_UINT8(timeoutcon, SDHCIState), 125449ab747fSPaolo Bonzini VMSTATE_UINT8(admaerr, SDHCIState), 125549ab747fSPaolo Bonzini VMSTATE_UINT16(norintsts, SDHCIState), 125649ab747fSPaolo Bonzini VMSTATE_UINT16(errintsts, SDHCIState), 125749ab747fSPaolo Bonzini VMSTATE_UINT16(norintstsen, SDHCIState), 125849ab747fSPaolo Bonzini VMSTATE_UINT16(errintstsen, SDHCIState), 125949ab747fSPaolo Bonzini VMSTATE_UINT16(norintsigen, SDHCIState), 126049ab747fSPaolo Bonzini VMSTATE_UINT16(errintsigen, SDHCIState), 126149ab747fSPaolo Bonzini VMSTATE_UINT16(acmd12errsts, SDHCIState), 126249ab747fSPaolo Bonzini VMSTATE_UINT16(data_count, SDHCIState), 126349ab747fSPaolo Bonzini VMSTATE_UINT64(admasysaddr, SDHCIState), 126449ab747fSPaolo Bonzini VMSTATE_UINT8(stopped_state, SDHCIState), 126559046ec2SHalil Pasic VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, buf_maxsz), 1266e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(insert_timer, SDHCIState), 1267e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(transfer_timer, SDHCIState), 126849ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 12690a7ac9f9SAndrew Baumann }, 12700a7ac9f9SAndrew Baumann .subsections = (const VMStateDescription*[]) { 12710a7ac9f9SAndrew Baumann &sdhci_pending_insert_vmstate, 12720a7ac9f9SAndrew Baumann NULL 12730a7ac9f9SAndrew Baumann }, 127449ab747fSPaolo Bonzini }; 127549ab747fSPaolo Bonzini 1276*b635d98cSPhilippe Mathieu-Daudé /* --- qdev PCI --- */ 1277*b635d98cSPhilippe Mathieu-Daudé 12785ec911c3SKevin O'Connor static Property sdhci_pci_properties[] = { 1279*b635d98cSPhilippe Mathieu-Daudé DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState), 128049ab747fSPaolo Bonzini DEFINE_PROP_END_OF_LIST(), 128149ab747fSPaolo Bonzini }; 128249ab747fSPaolo Bonzini 12839af21dbeSMarkus Armbruster static void sdhci_pci_realize(PCIDevice *dev, Error **errp) 1284224d10ffSKevin O'Connor { 1285224d10ffSKevin O'Connor SDHCIState *s = PCI_SDHCI(dev); 1286224d10ffSKevin O'Connor dev->config[PCI_CLASS_PROG] = 0x01; /* Standard Host supported DMA */ 1287224d10ffSKevin O'Connor dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */ 128840bbc194SPeter Maydell sdhci_initfn(s); 1289224d10ffSKevin O'Connor s->buf_maxsz = sdhci_get_fifolen(s); 1290224d10ffSKevin O'Connor s->fifo_buffer = g_malloc0(s->buf_maxsz); 1291224d10ffSKevin O'Connor s->irq = pci_allocate_irq(dev); 1292224d10ffSKevin O'Connor memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci", 1293224d10ffSKevin O'Connor SDHC_REGISTERS_MAP_SIZE); 1294224d10ffSKevin O'Connor pci_register_bar(dev, 0, 0, &s->iomem); 1295224d10ffSKevin O'Connor } 1296224d10ffSKevin O'Connor 1297224d10ffSKevin O'Connor static void sdhci_pci_exit(PCIDevice *dev) 1298224d10ffSKevin O'Connor { 1299224d10ffSKevin O'Connor SDHCIState *s = PCI_SDHCI(dev); 1300224d10ffSKevin O'Connor sdhci_uninitfn(s); 1301224d10ffSKevin O'Connor } 1302224d10ffSKevin O'Connor 1303224d10ffSKevin O'Connor static void sdhci_pci_class_init(ObjectClass *klass, void *data) 1304224d10ffSKevin O'Connor { 1305224d10ffSKevin O'Connor DeviceClass *dc = DEVICE_CLASS(klass); 1306224d10ffSKevin O'Connor PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1307224d10ffSKevin O'Connor 13089af21dbeSMarkus Armbruster k->realize = sdhci_pci_realize; 1309224d10ffSKevin O'Connor k->exit = sdhci_pci_exit; 1310224d10ffSKevin O'Connor k->vendor_id = PCI_VENDOR_ID_REDHAT; 1311224d10ffSKevin O'Connor k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI; 1312224d10ffSKevin O'Connor k->class_id = PCI_CLASS_SYSTEM_SDHCI; 1313224d10ffSKevin O'Connor set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1314224d10ffSKevin O'Connor dc->vmsd = &sdhci_vmstate; 13155ec911c3SKevin O'Connor dc->props = sdhci_pci_properties; 13168b41c305SPeter Maydell dc->reset = sdhci_poweron_reset; 1317224d10ffSKevin O'Connor } 1318224d10ffSKevin O'Connor 1319224d10ffSKevin O'Connor static const TypeInfo sdhci_pci_info = { 1320224d10ffSKevin O'Connor .name = TYPE_PCI_SDHCI, 1321224d10ffSKevin O'Connor .parent = TYPE_PCI_DEVICE, 1322224d10ffSKevin O'Connor .instance_size = sizeof(SDHCIState), 1323224d10ffSKevin O'Connor .class_init = sdhci_pci_class_init, 1324fd3b02c8SEduardo Habkost .interfaces = (InterfaceInfo[]) { 1325fd3b02c8SEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 1326fd3b02c8SEduardo Habkost { }, 1327fd3b02c8SEduardo Habkost }, 1328224d10ffSKevin O'Connor }; 1329224d10ffSKevin O'Connor 1330*b635d98cSPhilippe Mathieu-Daudé /* --- qdev SysBus --- */ 1331*b635d98cSPhilippe Mathieu-Daudé 13325ec911c3SKevin O'Connor static Property sdhci_sysbus_properties[] = { 1333*b635d98cSPhilippe Mathieu-Daudé DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState), 13340a7ac9f9SAndrew Baumann DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState, pending_insert_quirk, 13350a7ac9f9SAndrew Baumann false), 13365ec911c3SKevin O'Connor DEFINE_PROP_END_OF_LIST(), 13375ec911c3SKevin O'Connor }; 13385ec911c3SKevin O'Connor 13397302dcd6SKevin O'Connor static void sdhci_sysbus_init(Object *obj) 134049ab747fSPaolo Bonzini { 13417302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 13425ec911c3SKevin O'Connor 134340bbc194SPeter Maydell sdhci_initfn(s); 13447302dcd6SKevin O'Connor } 13457302dcd6SKevin O'Connor 13467302dcd6SKevin O'Connor static void sdhci_sysbus_finalize(Object *obj) 13477302dcd6SKevin O'Connor { 13487302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(obj); 13497302dcd6SKevin O'Connor sdhci_uninitfn(s); 13507302dcd6SKevin O'Connor } 13517302dcd6SKevin O'Connor 13527302dcd6SKevin O'Connor static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp) 13537302dcd6SKevin O'Connor { 13547302dcd6SKevin O'Connor SDHCIState *s = SYSBUS_SDHCI(dev); 135549ab747fSPaolo Bonzini SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 135649ab747fSPaolo Bonzini 135749ab747fSPaolo Bonzini s->buf_maxsz = sdhci_get_fifolen(s); 135849ab747fSPaolo Bonzini s->fifo_buffer = g_malloc0(s->buf_maxsz); 135949ab747fSPaolo Bonzini sysbus_init_irq(sbd, &s->irq); 136029776739SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci", 136149ab747fSPaolo Bonzini SDHC_REGISTERS_MAP_SIZE); 136249ab747fSPaolo Bonzini sysbus_init_mmio(sbd, &s->iomem); 136349ab747fSPaolo Bonzini } 136449ab747fSPaolo Bonzini 13657302dcd6SKevin O'Connor static void sdhci_sysbus_class_init(ObjectClass *klass, void *data) 136649ab747fSPaolo Bonzini { 136749ab747fSPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass); 136849ab747fSPaolo Bonzini 136949ab747fSPaolo Bonzini dc->vmsd = &sdhci_vmstate; 13705ec911c3SKevin O'Connor dc->props = sdhci_sysbus_properties; 13717302dcd6SKevin O'Connor dc->realize = sdhci_sysbus_realize; 13728b41c305SPeter Maydell dc->reset = sdhci_poweron_reset; 137349ab747fSPaolo Bonzini } 137449ab747fSPaolo Bonzini 13757302dcd6SKevin O'Connor static const TypeInfo sdhci_sysbus_info = { 13767302dcd6SKevin O'Connor .name = TYPE_SYSBUS_SDHCI, 137749ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 137849ab747fSPaolo Bonzini .instance_size = sizeof(SDHCIState), 13797302dcd6SKevin O'Connor .instance_init = sdhci_sysbus_init, 13807302dcd6SKevin O'Connor .instance_finalize = sdhci_sysbus_finalize, 13817302dcd6SKevin O'Connor .class_init = sdhci_sysbus_class_init, 138249ab747fSPaolo Bonzini }; 138349ab747fSPaolo Bonzini 1384*b635d98cSPhilippe Mathieu-Daudé /* --- qdev bus master --- */ 1385*b635d98cSPhilippe Mathieu-Daudé 138640bbc194SPeter Maydell static void sdhci_bus_class_init(ObjectClass *klass, void *data) 138740bbc194SPeter Maydell { 138840bbc194SPeter Maydell SDBusClass *sbc = SD_BUS_CLASS(klass); 138940bbc194SPeter Maydell 139040bbc194SPeter Maydell sbc->set_inserted = sdhci_set_inserted; 139140bbc194SPeter Maydell sbc->set_readonly = sdhci_set_readonly; 139240bbc194SPeter Maydell } 139340bbc194SPeter Maydell 139440bbc194SPeter Maydell static const TypeInfo sdhci_bus_info = { 139540bbc194SPeter Maydell .name = TYPE_SDHCI_BUS, 139640bbc194SPeter Maydell .parent = TYPE_SD_BUS, 139740bbc194SPeter Maydell .instance_size = sizeof(SDBus), 139840bbc194SPeter Maydell .class_init = sdhci_bus_class_init, 139940bbc194SPeter Maydell }; 140040bbc194SPeter Maydell 140149ab747fSPaolo Bonzini static void sdhci_register_types(void) 140249ab747fSPaolo Bonzini { 1403224d10ffSKevin O'Connor type_register_static(&sdhci_pci_info); 14047302dcd6SKevin O'Connor type_register_static(&sdhci_sysbus_info); 140540bbc194SPeter Maydell type_register_static(&sdhci_bus_info); 140649ab747fSPaolo Bonzini } 140749ab747fSPaolo Bonzini 140849ab747fSPaolo Bonzini type_init(sdhci_register_types) 1409