149ab747fSPaolo Bonzini /* 249ab747fSPaolo Bonzini * Arm PrimeCell PL181 MultiMedia Card Interface 349ab747fSPaolo Bonzini * 449ab747fSPaolo Bonzini * Copyright (c) 2007 CodeSourcery. 549ab747fSPaolo Bonzini * Written by Paul Brook 649ab747fSPaolo Bonzini * 749ab747fSPaolo Bonzini * This code is licensed under the GPL. 849ab747fSPaolo Bonzini */ 949ab747fSPaolo Bonzini 108ef94f0bSPeter Maydell #include "qemu/osdep.h" 1149ab747fSPaolo Bonzini #include "sysemu/blockdev.h" 1249ab747fSPaolo Bonzini #include "hw/sysbus.h" 13d6454270SMarkus Armbruster #include "migration/vmstate.h" 1464552b6bSMarkus Armbruster #include "hw/irq.h" 15e3382ef0SSai Pavan Boddu #include "hw/sd/sd.h" 1603dd024fSPaolo Bonzini #include "qemu/log.h" 170b8fa32fSMarkus Armbruster #include "qemu/module.h" 184858e256SAlistair Francis #include "qemu/error-report.h" 190d554cb0Sxiaoqiang zhao #include "qapi/error.h" 20583d09f0SPhilippe Mathieu-Daudé #include "trace.h" 2149ab747fSPaolo Bonzini 2249ab747fSPaolo Bonzini #define PL181_FIFO_LEN 16 2349ab747fSPaolo Bonzini 24630f4442SAndreas Färber #define TYPE_PL181 "pl181" 25630f4442SAndreas Färber #define PL181(obj) OBJECT_CHECK(PL181State, (obj), TYPE_PL181) 26630f4442SAndreas Färber 272762eed1SPhilippe Mathieu-Daudé #define TYPE_PL181_BUS "pl181-bus" 282762eed1SPhilippe Mathieu-Daudé 291d998d93SAndreas Färber typedef struct PL181State { 30630f4442SAndreas Färber SysBusDevice parent_obj; 31630f4442SAndreas Färber 3249ab747fSPaolo Bonzini MemoryRegion iomem; 332762eed1SPhilippe Mathieu-Daudé SDBus sdbus; 3449ab747fSPaolo Bonzini uint32_t clock; 3549ab747fSPaolo Bonzini uint32_t power; 3649ab747fSPaolo Bonzini uint32_t cmdarg; 3749ab747fSPaolo Bonzini uint32_t cmd; 3849ab747fSPaolo Bonzini uint32_t datatimer; 3949ab747fSPaolo Bonzini uint32_t datalength; 4049ab747fSPaolo Bonzini uint32_t respcmd; 4149ab747fSPaolo Bonzini uint32_t response[4]; 4249ab747fSPaolo Bonzini uint32_t datactrl; 4349ab747fSPaolo Bonzini uint32_t datacnt; 4449ab747fSPaolo Bonzini uint32_t status; 4549ab747fSPaolo Bonzini uint32_t mask[2]; 4649ab747fSPaolo Bonzini int32_t fifo_pos; 4749ab747fSPaolo Bonzini int32_t fifo_len; 4849ab747fSPaolo Bonzini /* The linux 2.6.21 driver is buggy, and misbehaves if new data arrives 4967cc32ebSVeres Lajos while it is reading the FIFO. We hack around this by deferring 5049ab747fSPaolo Bonzini subsequent transfers until after the driver polls the status word. 5149ab747fSPaolo Bonzini http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=4446/1 5249ab747fSPaolo Bonzini */ 5349ab747fSPaolo Bonzini int32_t linux_hack; 540e33730cSPhilippe Mathieu-Daudé uint32_t fifo[PL181_FIFO_LEN]; /* TODO use Fifo32 */ 5549ab747fSPaolo Bonzini qemu_irq irq[2]; 5649ab747fSPaolo Bonzini /* GPIO outputs for 'card is readonly' and 'card inserted' */ 5726c5b0f4SPhilippe Mathieu-Daudé qemu_irq card_readonly; 5826c5b0f4SPhilippe Mathieu-Daudé qemu_irq card_inserted; 591d998d93SAndreas Färber } PL181State; 6049ab747fSPaolo Bonzini 6149ab747fSPaolo Bonzini static const VMStateDescription vmstate_pl181 = { 6249ab747fSPaolo Bonzini .name = "pl181", 6349ab747fSPaolo Bonzini .version_id = 1, 6449ab747fSPaolo Bonzini .minimum_version_id = 1, 6549ab747fSPaolo Bonzini .fields = (VMStateField[]) { 661d998d93SAndreas Färber VMSTATE_UINT32(clock, PL181State), 671d998d93SAndreas Färber VMSTATE_UINT32(power, PL181State), 681d998d93SAndreas Färber VMSTATE_UINT32(cmdarg, PL181State), 691d998d93SAndreas Färber VMSTATE_UINT32(cmd, PL181State), 701d998d93SAndreas Färber VMSTATE_UINT32(datatimer, PL181State), 711d998d93SAndreas Färber VMSTATE_UINT32(datalength, PL181State), 721d998d93SAndreas Färber VMSTATE_UINT32(respcmd, PL181State), 731d998d93SAndreas Färber VMSTATE_UINT32_ARRAY(response, PL181State, 4), 741d998d93SAndreas Färber VMSTATE_UINT32(datactrl, PL181State), 751d998d93SAndreas Färber VMSTATE_UINT32(datacnt, PL181State), 761d998d93SAndreas Färber VMSTATE_UINT32(status, PL181State), 771d998d93SAndreas Färber VMSTATE_UINT32_ARRAY(mask, PL181State, 2), 781d998d93SAndreas Färber VMSTATE_INT32(fifo_pos, PL181State), 791d998d93SAndreas Färber VMSTATE_INT32(fifo_len, PL181State), 801d998d93SAndreas Färber VMSTATE_INT32(linux_hack, PL181State), 811d998d93SAndreas Färber VMSTATE_UINT32_ARRAY(fifo, PL181State, PL181_FIFO_LEN), 8249ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 8349ab747fSPaolo Bonzini } 8449ab747fSPaolo Bonzini }; 8549ab747fSPaolo Bonzini 8649ab747fSPaolo Bonzini #define PL181_CMD_INDEX 0x3f 8749ab747fSPaolo Bonzini #define PL181_CMD_RESPONSE (1 << 6) 8849ab747fSPaolo Bonzini #define PL181_CMD_LONGRESP (1 << 7) 8949ab747fSPaolo Bonzini #define PL181_CMD_INTERRUPT (1 << 8) 9049ab747fSPaolo Bonzini #define PL181_CMD_PENDING (1 << 9) 9149ab747fSPaolo Bonzini #define PL181_CMD_ENABLE (1 << 10) 9249ab747fSPaolo Bonzini 9349ab747fSPaolo Bonzini #define PL181_DATA_ENABLE (1 << 0) 9449ab747fSPaolo Bonzini #define PL181_DATA_DIRECTION (1 << 1) 9549ab747fSPaolo Bonzini #define PL181_DATA_MODE (1 << 2) 9649ab747fSPaolo Bonzini #define PL181_DATA_DMAENABLE (1 << 3) 9749ab747fSPaolo Bonzini 9849ab747fSPaolo Bonzini #define PL181_STATUS_CMDCRCFAIL (1 << 0) 9949ab747fSPaolo Bonzini #define PL181_STATUS_DATACRCFAIL (1 << 1) 10049ab747fSPaolo Bonzini #define PL181_STATUS_CMDTIMEOUT (1 << 2) 10149ab747fSPaolo Bonzini #define PL181_STATUS_DATATIMEOUT (1 << 3) 10249ab747fSPaolo Bonzini #define PL181_STATUS_TXUNDERRUN (1 << 4) 10349ab747fSPaolo Bonzini #define PL181_STATUS_RXOVERRUN (1 << 5) 10449ab747fSPaolo Bonzini #define PL181_STATUS_CMDRESPEND (1 << 6) 10549ab747fSPaolo Bonzini #define PL181_STATUS_CMDSENT (1 << 7) 10649ab747fSPaolo Bonzini #define PL181_STATUS_DATAEND (1 << 8) 10749ab747fSPaolo Bonzini #define PL181_STATUS_DATABLOCKEND (1 << 10) 10849ab747fSPaolo Bonzini #define PL181_STATUS_CMDACTIVE (1 << 11) 10949ab747fSPaolo Bonzini #define PL181_STATUS_TXACTIVE (1 << 12) 11049ab747fSPaolo Bonzini #define PL181_STATUS_RXACTIVE (1 << 13) 11149ab747fSPaolo Bonzini #define PL181_STATUS_TXFIFOHALFEMPTY (1 << 14) 11249ab747fSPaolo Bonzini #define PL181_STATUS_RXFIFOHALFFULL (1 << 15) 11349ab747fSPaolo Bonzini #define PL181_STATUS_TXFIFOFULL (1 << 16) 11449ab747fSPaolo Bonzini #define PL181_STATUS_RXFIFOFULL (1 << 17) 11549ab747fSPaolo Bonzini #define PL181_STATUS_TXFIFOEMPTY (1 << 18) 11649ab747fSPaolo Bonzini #define PL181_STATUS_RXFIFOEMPTY (1 << 19) 11749ab747fSPaolo Bonzini #define PL181_STATUS_TXDATAAVLBL (1 << 20) 11849ab747fSPaolo Bonzini #define PL181_STATUS_RXDATAAVLBL (1 << 21) 11949ab747fSPaolo Bonzini 12049ab747fSPaolo Bonzini #define PL181_STATUS_TX_FIFO (PL181_STATUS_TXACTIVE \ 12149ab747fSPaolo Bonzini |PL181_STATUS_TXFIFOHALFEMPTY \ 12249ab747fSPaolo Bonzini |PL181_STATUS_TXFIFOFULL \ 12349ab747fSPaolo Bonzini |PL181_STATUS_TXFIFOEMPTY \ 12449ab747fSPaolo Bonzini |PL181_STATUS_TXDATAAVLBL) 12549ab747fSPaolo Bonzini #define PL181_STATUS_RX_FIFO (PL181_STATUS_RXACTIVE \ 12649ab747fSPaolo Bonzini |PL181_STATUS_RXFIFOHALFFULL \ 12749ab747fSPaolo Bonzini |PL181_STATUS_RXFIFOFULL \ 12849ab747fSPaolo Bonzini |PL181_STATUS_RXFIFOEMPTY \ 12949ab747fSPaolo Bonzini |PL181_STATUS_RXDATAAVLBL) 13049ab747fSPaolo Bonzini 13149ab747fSPaolo Bonzini static const unsigned char pl181_id[] = 13249ab747fSPaolo Bonzini { 0x81, 0x11, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; 13349ab747fSPaolo Bonzini 1341d998d93SAndreas Färber static void pl181_update(PL181State *s) 13549ab747fSPaolo Bonzini { 13649ab747fSPaolo Bonzini int i; 13749ab747fSPaolo Bonzini for (i = 0; i < 2; i++) { 13849ab747fSPaolo Bonzini qemu_set_irq(s->irq[i], (s->status & s->mask[i]) != 0); 13949ab747fSPaolo Bonzini } 14049ab747fSPaolo Bonzini } 14149ab747fSPaolo Bonzini 1421d998d93SAndreas Färber static void pl181_fifo_push(PL181State *s, uint32_t value) 14349ab747fSPaolo Bonzini { 14449ab747fSPaolo Bonzini int n; 14549ab747fSPaolo Bonzini 14649ab747fSPaolo Bonzini if (s->fifo_len == PL181_FIFO_LEN) { 1474858e256SAlistair Francis error_report("%s: FIFO overflow", __func__); 14849ab747fSPaolo Bonzini return; 14949ab747fSPaolo Bonzini } 15049ab747fSPaolo Bonzini n = (s->fifo_pos + s->fifo_len) & (PL181_FIFO_LEN - 1); 15149ab747fSPaolo Bonzini s->fifo_len++; 15249ab747fSPaolo Bonzini s->fifo[n] = value; 153583d09f0SPhilippe Mathieu-Daudé trace_pl181_fifo_push(value); 15449ab747fSPaolo Bonzini } 15549ab747fSPaolo Bonzini 1561d998d93SAndreas Färber static uint32_t pl181_fifo_pop(PL181State *s) 15749ab747fSPaolo Bonzini { 15849ab747fSPaolo Bonzini uint32_t value; 15949ab747fSPaolo Bonzini 16049ab747fSPaolo Bonzini if (s->fifo_len == 0) { 1614858e256SAlistair Francis error_report("%s: FIFO underflow", __func__); 16249ab747fSPaolo Bonzini return 0; 16349ab747fSPaolo Bonzini } 16449ab747fSPaolo Bonzini value = s->fifo[s->fifo_pos]; 16549ab747fSPaolo Bonzini s->fifo_len--; 16649ab747fSPaolo Bonzini s->fifo_pos = (s->fifo_pos + 1) & (PL181_FIFO_LEN - 1); 167583d09f0SPhilippe Mathieu-Daudé trace_pl181_fifo_pop(value); 16849ab747fSPaolo Bonzini return value; 16949ab747fSPaolo Bonzini } 17049ab747fSPaolo Bonzini 171b67cd8f5SPhilippe Mathieu-Daudé static void pl181_do_command(PL181State *s) 17249ab747fSPaolo Bonzini { 17349ab747fSPaolo Bonzini SDRequest request; 17449ab747fSPaolo Bonzini uint8_t response[16]; 17549ab747fSPaolo Bonzini int rlen; 17649ab747fSPaolo Bonzini 17749ab747fSPaolo Bonzini request.cmd = s->cmd & PL181_CMD_INDEX; 17849ab747fSPaolo Bonzini request.arg = s->cmdarg; 179583d09f0SPhilippe Mathieu-Daudé trace_pl181_command_send(request.cmd, request.arg); 1802762eed1SPhilippe Mathieu-Daudé rlen = sdbus_do_command(&s->sdbus, &request, response); 18149ab747fSPaolo Bonzini if (rlen < 0) 18249ab747fSPaolo Bonzini goto error; 18349ab747fSPaolo Bonzini if (s->cmd & PL181_CMD_RESPONSE) { 18449ab747fSPaolo Bonzini if (rlen == 0 || (rlen == 4 && (s->cmd & PL181_CMD_LONGRESP))) 18549ab747fSPaolo Bonzini goto error; 18649ab747fSPaolo Bonzini if (rlen != 4 && rlen != 16) 18749ab747fSPaolo Bonzini goto error; 188b3141c06SPhilippe Mathieu-Daudé s->response[0] = ldl_be_p(&response[0]); 18949ab747fSPaolo Bonzini if (rlen == 4) { 19049ab747fSPaolo Bonzini s->response[1] = s->response[2] = s->response[3] = 0; 19149ab747fSPaolo Bonzini } else { 192b3141c06SPhilippe Mathieu-Daudé s->response[1] = ldl_be_p(&response[4]); 193b3141c06SPhilippe Mathieu-Daudé s->response[2] = ldl_be_p(&response[8]); 194b3141c06SPhilippe Mathieu-Daudé s->response[3] = ldl_be_p(&response[12]) & ~1; 19549ab747fSPaolo Bonzini } 196583d09f0SPhilippe Mathieu-Daudé trace_pl181_command_response_pending(); 19749ab747fSPaolo Bonzini s->status |= PL181_STATUS_CMDRESPEND; 19849ab747fSPaolo Bonzini } else { 199583d09f0SPhilippe Mathieu-Daudé trace_pl181_command_sent(); 20049ab747fSPaolo Bonzini s->status |= PL181_STATUS_CMDSENT; 20149ab747fSPaolo Bonzini } 20249ab747fSPaolo Bonzini return; 20349ab747fSPaolo Bonzini 20449ab747fSPaolo Bonzini error: 205583d09f0SPhilippe Mathieu-Daudé trace_pl181_command_timeout(); 20649ab747fSPaolo Bonzini s->status |= PL181_STATUS_CMDTIMEOUT; 20749ab747fSPaolo Bonzini } 20849ab747fSPaolo Bonzini 20949ab747fSPaolo Bonzini /* Transfer data between the card and the FIFO. This is complicated by 21049ab747fSPaolo Bonzini the FIFO holding 32-bit words and the card taking data in single byte 21149ab747fSPaolo Bonzini chunks. FIFO bytes are transferred in little-endian order. */ 21249ab747fSPaolo Bonzini 2131d998d93SAndreas Färber static void pl181_fifo_run(PL181State *s) 21449ab747fSPaolo Bonzini { 21549ab747fSPaolo Bonzini uint32_t bits; 21649ab747fSPaolo Bonzini uint32_t value = 0; 21749ab747fSPaolo Bonzini int n; 21849ab747fSPaolo Bonzini int is_read; 21949ab747fSPaolo Bonzini 22049ab747fSPaolo Bonzini is_read = (s->datactrl & PL181_DATA_DIRECTION) != 0; 2212762eed1SPhilippe Mathieu-Daudé if (s->datacnt != 0 && (!is_read || sdbus_data_ready(&s->sdbus)) 22249ab747fSPaolo Bonzini && !s->linux_hack) { 22349ab747fSPaolo Bonzini if (is_read) { 22449ab747fSPaolo Bonzini n = 0; 22549ab747fSPaolo Bonzini while (s->datacnt && s->fifo_len < PL181_FIFO_LEN) { 2262762eed1SPhilippe Mathieu-Daudé value |= (uint32_t)sdbus_read_data(&s->sdbus) << (n * 8); 22749ab747fSPaolo Bonzini s->datacnt--; 22849ab747fSPaolo Bonzini n++; 22949ab747fSPaolo Bonzini if (n == 4) { 23049ab747fSPaolo Bonzini pl181_fifo_push(s, value); 23149ab747fSPaolo Bonzini n = 0; 23249ab747fSPaolo Bonzini value = 0; 23349ab747fSPaolo Bonzini } 23449ab747fSPaolo Bonzini } 23549ab747fSPaolo Bonzini if (n != 0) { 23649ab747fSPaolo Bonzini pl181_fifo_push(s, value); 23749ab747fSPaolo Bonzini } 23849ab747fSPaolo Bonzini } else { /* write */ 23949ab747fSPaolo Bonzini n = 0; 24049ab747fSPaolo Bonzini while (s->datacnt > 0 && (s->fifo_len > 0 || n > 0)) { 24149ab747fSPaolo Bonzini if (n == 0) { 24249ab747fSPaolo Bonzini value = pl181_fifo_pop(s); 24349ab747fSPaolo Bonzini n = 4; 24449ab747fSPaolo Bonzini } 24549ab747fSPaolo Bonzini n--; 24649ab747fSPaolo Bonzini s->datacnt--; 247*39017143SPhilippe Mathieu-Daudé sdbus_write_byte(&s->sdbus, value & 0xff); 24849ab747fSPaolo Bonzini value >>= 8; 24949ab747fSPaolo Bonzini } 25049ab747fSPaolo Bonzini } 25149ab747fSPaolo Bonzini } 25249ab747fSPaolo Bonzini s->status &= ~(PL181_STATUS_RX_FIFO | PL181_STATUS_TX_FIFO); 25349ab747fSPaolo Bonzini if (s->datacnt == 0) { 25449ab747fSPaolo Bonzini s->status |= PL181_STATUS_DATAEND; 25549ab747fSPaolo Bonzini /* HACK: */ 25649ab747fSPaolo Bonzini s->status |= PL181_STATUS_DATABLOCKEND; 257583d09f0SPhilippe Mathieu-Daudé trace_pl181_fifo_transfer_complete(); 25849ab747fSPaolo Bonzini } 25949ab747fSPaolo Bonzini if (s->datacnt == 0 && s->fifo_len == 0) { 26049ab747fSPaolo Bonzini s->datactrl &= ~PL181_DATA_ENABLE; 261583d09f0SPhilippe Mathieu-Daudé trace_pl181_data_engine_idle(); 26249ab747fSPaolo Bonzini } else { 26349ab747fSPaolo Bonzini /* Update FIFO bits. */ 26449ab747fSPaolo Bonzini bits = PL181_STATUS_TXACTIVE | PL181_STATUS_RXACTIVE; 26549ab747fSPaolo Bonzini if (s->fifo_len == 0) { 26649ab747fSPaolo Bonzini bits |= PL181_STATUS_TXFIFOEMPTY; 26749ab747fSPaolo Bonzini bits |= PL181_STATUS_RXFIFOEMPTY; 26849ab747fSPaolo Bonzini } else { 26949ab747fSPaolo Bonzini bits |= PL181_STATUS_TXDATAAVLBL; 27049ab747fSPaolo Bonzini bits |= PL181_STATUS_RXDATAAVLBL; 27149ab747fSPaolo Bonzini } 27249ab747fSPaolo Bonzini if (s->fifo_len == 16) { 27349ab747fSPaolo Bonzini bits |= PL181_STATUS_TXFIFOFULL; 27449ab747fSPaolo Bonzini bits |= PL181_STATUS_RXFIFOFULL; 27549ab747fSPaolo Bonzini } 27649ab747fSPaolo Bonzini if (s->fifo_len <= 8) { 27749ab747fSPaolo Bonzini bits |= PL181_STATUS_TXFIFOHALFEMPTY; 27849ab747fSPaolo Bonzini } 27949ab747fSPaolo Bonzini if (s->fifo_len >= 8) { 28049ab747fSPaolo Bonzini bits |= PL181_STATUS_RXFIFOHALFFULL; 28149ab747fSPaolo Bonzini } 28249ab747fSPaolo Bonzini if (s->datactrl & PL181_DATA_DIRECTION) { 28349ab747fSPaolo Bonzini bits &= PL181_STATUS_RX_FIFO; 28449ab747fSPaolo Bonzini } else { 28549ab747fSPaolo Bonzini bits &= PL181_STATUS_TX_FIFO; 28649ab747fSPaolo Bonzini } 28749ab747fSPaolo Bonzini s->status |= bits; 28849ab747fSPaolo Bonzini } 28949ab747fSPaolo Bonzini } 29049ab747fSPaolo Bonzini 29149ab747fSPaolo Bonzini static uint64_t pl181_read(void *opaque, hwaddr offset, 29249ab747fSPaolo Bonzini unsigned size) 29349ab747fSPaolo Bonzini { 2941d998d93SAndreas Färber PL181State *s = (PL181State *)opaque; 29549ab747fSPaolo Bonzini uint32_t tmp; 29649ab747fSPaolo Bonzini 29749ab747fSPaolo Bonzini if (offset >= 0xfe0 && offset < 0x1000) { 29849ab747fSPaolo Bonzini return pl181_id[(offset - 0xfe0) >> 2]; 29949ab747fSPaolo Bonzini } 30049ab747fSPaolo Bonzini switch (offset) { 30149ab747fSPaolo Bonzini case 0x00: /* Power */ 30249ab747fSPaolo Bonzini return s->power; 30349ab747fSPaolo Bonzini case 0x04: /* Clock */ 30449ab747fSPaolo Bonzini return s->clock; 30549ab747fSPaolo Bonzini case 0x08: /* Argument */ 30649ab747fSPaolo Bonzini return s->cmdarg; 30749ab747fSPaolo Bonzini case 0x0c: /* Command */ 30849ab747fSPaolo Bonzini return s->cmd; 30949ab747fSPaolo Bonzini case 0x10: /* RespCmd */ 31049ab747fSPaolo Bonzini return s->respcmd; 31149ab747fSPaolo Bonzini case 0x14: /* Response0 */ 31249ab747fSPaolo Bonzini return s->response[0]; 31349ab747fSPaolo Bonzini case 0x18: /* Response1 */ 31449ab747fSPaolo Bonzini return s->response[1]; 31549ab747fSPaolo Bonzini case 0x1c: /* Response2 */ 31649ab747fSPaolo Bonzini return s->response[2]; 31749ab747fSPaolo Bonzini case 0x20: /* Response3 */ 31849ab747fSPaolo Bonzini return s->response[3]; 31949ab747fSPaolo Bonzini case 0x24: /* DataTimer */ 32049ab747fSPaolo Bonzini return s->datatimer; 32149ab747fSPaolo Bonzini case 0x28: /* DataLength */ 32249ab747fSPaolo Bonzini return s->datalength; 32349ab747fSPaolo Bonzini case 0x2c: /* DataCtrl */ 32449ab747fSPaolo Bonzini return s->datactrl; 32549ab747fSPaolo Bonzini case 0x30: /* DataCnt */ 32649ab747fSPaolo Bonzini return s->datacnt; 32749ab747fSPaolo Bonzini case 0x34: /* Status */ 32849ab747fSPaolo Bonzini tmp = s->status; 32949ab747fSPaolo Bonzini if (s->linux_hack) { 33049ab747fSPaolo Bonzini s->linux_hack = 0; 33149ab747fSPaolo Bonzini pl181_fifo_run(s); 33249ab747fSPaolo Bonzini pl181_update(s); 33349ab747fSPaolo Bonzini } 33449ab747fSPaolo Bonzini return tmp; 33549ab747fSPaolo Bonzini case 0x3c: /* Mask0 */ 33649ab747fSPaolo Bonzini return s->mask[0]; 33749ab747fSPaolo Bonzini case 0x40: /* Mask1 */ 33849ab747fSPaolo Bonzini return s->mask[1]; 33949ab747fSPaolo Bonzini case 0x48: /* FifoCnt */ 34049ab747fSPaolo Bonzini /* The documentation is somewhat vague about exactly what FifoCnt 34149ab747fSPaolo Bonzini does. On real hardware it appears to be when decrememnted 34249ab747fSPaolo Bonzini when a word is transferred between the FIFO and the serial 34349ab747fSPaolo Bonzini data engine. DataCnt is decremented after each byte is 34449ab747fSPaolo Bonzini transferred between the serial engine and the card. 34549ab747fSPaolo Bonzini We don't emulate this level of detail, so both can be the same. */ 34649ab747fSPaolo Bonzini tmp = (s->datacnt + 3) >> 2; 34749ab747fSPaolo Bonzini if (s->linux_hack) { 34849ab747fSPaolo Bonzini s->linux_hack = 0; 34949ab747fSPaolo Bonzini pl181_fifo_run(s); 35049ab747fSPaolo Bonzini pl181_update(s); 35149ab747fSPaolo Bonzini } 35249ab747fSPaolo Bonzini return tmp; 35349ab747fSPaolo Bonzini case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */ 35449ab747fSPaolo Bonzini case 0x90: case 0x94: case 0x98: case 0x9c: 35549ab747fSPaolo Bonzini case 0xa0: case 0xa4: case 0xa8: case 0xac: 35649ab747fSPaolo Bonzini case 0xb0: case 0xb4: case 0xb8: case 0xbc: 35749ab747fSPaolo Bonzini if (s->fifo_len == 0) { 35849ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, "pl181: Unexpected FIFO read\n"); 35949ab747fSPaolo Bonzini return 0; 36049ab747fSPaolo Bonzini } else { 36149ab747fSPaolo Bonzini uint32_t value; 36249ab747fSPaolo Bonzini value = pl181_fifo_pop(s); 36349ab747fSPaolo Bonzini s->linux_hack = 1; 36449ab747fSPaolo Bonzini pl181_fifo_run(s); 36549ab747fSPaolo Bonzini pl181_update(s); 36649ab747fSPaolo Bonzini return value; 36749ab747fSPaolo Bonzini } 36849ab747fSPaolo Bonzini default: 36949ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 37049ab747fSPaolo Bonzini "pl181_read: Bad offset %x\n", (int)offset); 37149ab747fSPaolo Bonzini return 0; 37249ab747fSPaolo Bonzini } 37349ab747fSPaolo Bonzini } 37449ab747fSPaolo Bonzini 37549ab747fSPaolo Bonzini static void pl181_write(void *opaque, hwaddr offset, 37649ab747fSPaolo Bonzini uint64_t value, unsigned size) 37749ab747fSPaolo Bonzini { 3781d998d93SAndreas Färber PL181State *s = (PL181State *)opaque; 37949ab747fSPaolo Bonzini 38049ab747fSPaolo Bonzini switch (offset) { 38149ab747fSPaolo Bonzini case 0x00: /* Power */ 38249ab747fSPaolo Bonzini s->power = value & 0xff; 38349ab747fSPaolo Bonzini break; 38449ab747fSPaolo Bonzini case 0x04: /* Clock */ 38549ab747fSPaolo Bonzini s->clock = value & 0xff; 38649ab747fSPaolo Bonzini break; 38749ab747fSPaolo Bonzini case 0x08: /* Argument */ 38849ab747fSPaolo Bonzini s->cmdarg = value; 38949ab747fSPaolo Bonzini break; 39049ab747fSPaolo Bonzini case 0x0c: /* Command */ 39149ab747fSPaolo Bonzini s->cmd = value; 39249ab747fSPaolo Bonzini if (s->cmd & PL181_CMD_ENABLE) { 39349ab747fSPaolo Bonzini if (s->cmd & PL181_CMD_INTERRUPT) { 39449ab747fSPaolo Bonzini qemu_log_mask(LOG_UNIMP, 39549ab747fSPaolo Bonzini "pl181: Interrupt mode not implemented\n"); 39649ab747fSPaolo Bonzini } if (s->cmd & PL181_CMD_PENDING) { 39749ab747fSPaolo Bonzini qemu_log_mask(LOG_UNIMP, 39849ab747fSPaolo Bonzini "pl181: Pending commands not implemented\n"); 39949ab747fSPaolo Bonzini } else { 400b67cd8f5SPhilippe Mathieu-Daudé pl181_do_command(s); 40149ab747fSPaolo Bonzini pl181_fifo_run(s); 40249ab747fSPaolo Bonzini } 40349ab747fSPaolo Bonzini /* The command has completed one way or the other. */ 40449ab747fSPaolo Bonzini s->cmd &= ~PL181_CMD_ENABLE; 40549ab747fSPaolo Bonzini } 40649ab747fSPaolo Bonzini break; 40749ab747fSPaolo Bonzini case 0x24: /* DataTimer */ 40849ab747fSPaolo Bonzini s->datatimer = value; 40949ab747fSPaolo Bonzini break; 41049ab747fSPaolo Bonzini case 0x28: /* DataLength */ 41149ab747fSPaolo Bonzini s->datalength = value & 0xffff; 41249ab747fSPaolo Bonzini break; 41349ab747fSPaolo Bonzini case 0x2c: /* DataCtrl */ 41449ab747fSPaolo Bonzini s->datactrl = value & 0xff; 41549ab747fSPaolo Bonzini if (value & PL181_DATA_ENABLE) { 41649ab747fSPaolo Bonzini s->datacnt = s->datalength; 41749ab747fSPaolo Bonzini pl181_fifo_run(s); 41849ab747fSPaolo Bonzini } 41949ab747fSPaolo Bonzini break; 42049ab747fSPaolo Bonzini case 0x38: /* Clear */ 42149ab747fSPaolo Bonzini s->status &= ~(value & 0x7ff); 42249ab747fSPaolo Bonzini break; 42349ab747fSPaolo Bonzini case 0x3c: /* Mask0 */ 42449ab747fSPaolo Bonzini s->mask[0] = value; 42549ab747fSPaolo Bonzini break; 42649ab747fSPaolo Bonzini case 0x40: /* Mask1 */ 42749ab747fSPaolo Bonzini s->mask[1] = value; 42849ab747fSPaolo Bonzini break; 42949ab747fSPaolo Bonzini case 0x80: case 0x84: case 0x88: case 0x8c: /* FifoData */ 43049ab747fSPaolo Bonzini case 0x90: case 0x94: case 0x98: case 0x9c: 43149ab747fSPaolo Bonzini case 0xa0: case 0xa4: case 0xa8: case 0xac: 43249ab747fSPaolo Bonzini case 0xb0: case 0xb4: case 0xb8: case 0xbc: 43349ab747fSPaolo Bonzini if (s->datacnt == 0) { 43449ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, "pl181: Unexpected FIFO write\n"); 43549ab747fSPaolo Bonzini } else { 43649ab747fSPaolo Bonzini pl181_fifo_push(s, value); 43749ab747fSPaolo Bonzini pl181_fifo_run(s); 43849ab747fSPaolo Bonzini } 43949ab747fSPaolo Bonzini break; 44049ab747fSPaolo Bonzini default: 44149ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 44249ab747fSPaolo Bonzini "pl181_write: Bad offset %x\n", (int)offset); 44349ab747fSPaolo Bonzini } 44449ab747fSPaolo Bonzini pl181_update(s); 44549ab747fSPaolo Bonzini } 44649ab747fSPaolo Bonzini 44749ab747fSPaolo Bonzini static const MemoryRegionOps pl181_ops = { 44849ab747fSPaolo Bonzini .read = pl181_read, 44949ab747fSPaolo Bonzini .write = pl181_write, 45049ab747fSPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN, 45149ab747fSPaolo Bonzini }; 45249ab747fSPaolo Bonzini 4532762eed1SPhilippe Mathieu-Daudé static void pl181_set_readonly(DeviceState *dev, bool level) 4542762eed1SPhilippe Mathieu-Daudé { 4552762eed1SPhilippe Mathieu-Daudé PL181State *s = (PL181State *)dev; 4562762eed1SPhilippe Mathieu-Daudé 4572762eed1SPhilippe Mathieu-Daudé qemu_set_irq(s->card_readonly, level); 4582762eed1SPhilippe Mathieu-Daudé } 4592762eed1SPhilippe Mathieu-Daudé 4602762eed1SPhilippe Mathieu-Daudé static void pl181_set_inserted(DeviceState *dev, bool level) 4612762eed1SPhilippe Mathieu-Daudé { 4622762eed1SPhilippe Mathieu-Daudé PL181State *s = (PL181State *)dev; 4632762eed1SPhilippe Mathieu-Daudé 4642762eed1SPhilippe Mathieu-Daudé qemu_set_irq(s->card_inserted, level); 4652762eed1SPhilippe Mathieu-Daudé } 4662762eed1SPhilippe Mathieu-Daudé 46749ab747fSPaolo Bonzini static void pl181_reset(DeviceState *d) 46849ab747fSPaolo Bonzini { 469630f4442SAndreas Färber PL181State *s = PL181(d); 47049ab747fSPaolo Bonzini 47149ab747fSPaolo Bonzini s->power = 0; 47249ab747fSPaolo Bonzini s->cmdarg = 0; 47349ab747fSPaolo Bonzini s->cmd = 0; 47449ab747fSPaolo Bonzini s->datatimer = 0; 47549ab747fSPaolo Bonzini s->datalength = 0; 47649ab747fSPaolo Bonzini s->respcmd = 0; 47749ab747fSPaolo Bonzini s->response[0] = 0; 47849ab747fSPaolo Bonzini s->response[1] = 0; 47949ab747fSPaolo Bonzini s->response[2] = 0; 48049ab747fSPaolo Bonzini s->response[3] = 0; 48149ab747fSPaolo Bonzini s->datatimer = 0; 48249ab747fSPaolo Bonzini s->datalength = 0; 48349ab747fSPaolo Bonzini s->datactrl = 0; 48449ab747fSPaolo Bonzini s->datacnt = 0; 48549ab747fSPaolo Bonzini s->status = 0; 48649ab747fSPaolo Bonzini s->linux_hack = 0; 48749ab747fSPaolo Bonzini s->mask[0] = 0; 48849ab747fSPaolo Bonzini s->mask[1] = 0; 48949ab747fSPaolo Bonzini 4902762eed1SPhilippe Mathieu-Daudé /* Reset other state based on current card insertion/readonly status */ 4912762eed1SPhilippe Mathieu-Daudé pl181_set_inserted(DEVICE(s), sdbus_get_inserted(&s->sdbus)); 4922762eed1SPhilippe Mathieu-Daudé pl181_set_readonly(DEVICE(s), sdbus_get_readonly(&s->sdbus)); 49349ab747fSPaolo Bonzini } 49449ab747fSPaolo Bonzini 4950d554cb0Sxiaoqiang zhao static void pl181_init(Object *obj) 49649ab747fSPaolo Bonzini { 4970d554cb0Sxiaoqiang zhao DeviceState *dev = DEVICE(obj); 4980d554cb0Sxiaoqiang zhao PL181State *s = PL181(obj); 4990d554cb0Sxiaoqiang zhao SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 50049ab747fSPaolo Bonzini 5010d554cb0Sxiaoqiang zhao memory_region_init_io(&s->iomem, obj, &pl181_ops, s, "pl181", 0x1000); 502630f4442SAndreas Färber sysbus_init_mmio(sbd, &s->iomem); 503630f4442SAndreas Färber sysbus_init_irq(sbd, &s->irq[0]); 504630f4442SAndreas Färber sysbus_init_irq(sbd, &s->irq[1]); 50526c5b0f4SPhilippe Mathieu-Daudé qdev_init_gpio_out_named(dev, &s->card_readonly, "card-read-only", 1); 50626c5b0f4SPhilippe Mathieu-Daudé qdev_init_gpio_out_named(dev, &s->card_inserted, "card-inserted", 1); 5072762eed1SPhilippe Mathieu-Daudé 5082762eed1SPhilippe Mathieu-Daudé qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), 5092762eed1SPhilippe Mathieu-Daudé TYPE_PL181_BUS, dev, "sd-bus"); 5100d554cb0Sxiaoqiang zhao } 5110d554cb0Sxiaoqiang zhao 51249ab747fSPaolo Bonzini static void pl181_class_init(ObjectClass *klass, void *data) 51349ab747fSPaolo Bonzini { 51449ab747fSPaolo Bonzini DeviceClass *k = DEVICE_CLASS(klass); 51549ab747fSPaolo Bonzini 51649ab747fSPaolo Bonzini k->vmsd = &vmstate_pl181; 51749ab747fSPaolo Bonzini k->reset = pl181_reset; 51826c607b8SPhilippe Mathieu-Daudé /* Reason: output IRQs should be wired up */ 519e90f2a8cSEduardo Habkost k->user_creatable = false; 52049ab747fSPaolo Bonzini } 52149ab747fSPaolo Bonzini 52249ab747fSPaolo Bonzini static const TypeInfo pl181_info = { 523630f4442SAndreas Färber .name = TYPE_PL181, 52449ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 5251d998d93SAndreas Färber .instance_size = sizeof(PL181State), 5260d554cb0Sxiaoqiang zhao .instance_init = pl181_init, 52749ab747fSPaolo Bonzini .class_init = pl181_class_init, 52849ab747fSPaolo Bonzini }; 52949ab747fSPaolo Bonzini 5302762eed1SPhilippe Mathieu-Daudé static void pl181_bus_class_init(ObjectClass *klass, void *data) 5312762eed1SPhilippe Mathieu-Daudé { 5322762eed1SPhilippe Mathieu-Daudé SDBusClass *sbc = SD_BUS_CLASS(klass); 5332762eed1SPhilippe Mathieu-Daudé 5342762eed1SPhilippe Mathieu-Daudé sbc->set_inserted = pl181_set_inserted; 5352762eed1SPhilippe Mathieu-Daudé sbc->set_readonly = pl181_set_readonly; 5362762eed1SPhilippe Mathieu-Daudé } 5372762eed1SPhilippe Mathieu-Daudé 5382762eed1SPhilippe Mathieu-Daudé static const TypeInfo pl181_bus_info = { 5392762eed1SPhilippe Mathieu-Daudé .name = TYPE_PL181_BUS, 5402762eed1SPhilippe Mathieu-Daudé .parent = TYPE_SD_BUS, 5412762eed1SPhilippe Mathieu-Daudé .instance_size = sizeof(SDBus), 5422762eed1SPhilippe Mathieu-Daudé .class_init = pl181_bus_class_init, 5432762eed1SPhilippe Mathieu-Daudé }; 5442762eed1SPhilippe Mathieu-Daudé 54549ab747fSPaolo Bonzini static void pl181_register_types(void) 54649ab747fSPaolo Bonzini { 54749ab747fSPaolo Bonzini type_register_static(&pl181_info); 5482762eed1SPhilippe Mathieu-Daudé type_register_static(&pl181_bus_info); 54949ab747fSPaolo Bonzini } 55049ab747fSPaolo Bonzini 55149ab747fSPaolo Bonzini type_init(pl181_register_types) 552