1 /* 2 * Allwinner (sun4i and above) SD Host Controller emulation 3 * 4 * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "qemu/osdep.h" 21 #include "qemu/log.h" 22 #include "qemu/module.h" 23 #include "qemu/units.h" 24 #include "qapi/error.h" 25 #include "sysemu/blockdev.h" 26 #include "sysemu/dma.h" 27 #include "hw/qdev-properties.h" 28 #include "hw/irq.h" 29 #include "hw/sd/allwinner-sdhost.h" 30 #include "migration/vmstate.h" 31 #include "trace.h" 32 #include "qom/object.h" 33 34 #define TYPE_AW_SDHOST_BUS "allwinner-sdhost-bus" 35 /* This is reusing the SDBus typedef from SD_BUS */ 36 DECLARE_INSTANCE_CHECKER(SDBus, AW_SDHOST_BUS, 37 TYPE_AW_SDHOST_BUS) 38 39 /* SD Host register offsets */ 40 enum { 41 REG_SD_GCTL = 0x00, /* Global Control */ 42 REG_SD_CKCR = 0x04, /* Clock Control */ 43 REG_SD_TMOR = 0x08, /* Timeout */ 44 REG_SD_BWDR = 0x0C, /* Bus Width */ 45 REG_SD_BKSR = 0x10, /* Block Size */ 46 REG_SD_BYCR = 0x14, /* Byte Count */ 47 REG_SD_CMDR = 0x18, /* Command */ 48 REG_SD_CAGR = 0x1C, /* Command Argument */ 49 REG_SD_RESP0 = 0x20, /* Response Zero */ 50 REG_SD_RESP1 = 0x24, /* Response One */ 51 REG_SD_RESP2 = 0x28, /* Response Two */ 52 REG_SD_RESP3 = 0x2C, /* Response Three */ 53 REG_SD_IMKR = 0x30, /* Interrupt Mask */ 54 REG_SD_MISR = 0x34, /* Masked Interrupt Status */ 55 REG_SD_RISR = 0x38, /* Raw Interrupt Status */ 56 REG_SD_STAR = 0x3C, /* Status */ 57 REG_SD_FWLR = 0x40, /* FIFO Water Level */ 58 REG_SD_FUNS = 0x44, /* FIFO Function Select */ 59 REG_SD_DBGC = 0x50, /* Debug Enable */ 60 REG_SD_A12A = 0x58, /* Auto command 12 argument */ 61 REG_SD_NTSR = 0x5C, /* SD NewTiming Set */ 62 REG_SD_SDBG = 0x60, /* SD newTiming Set Debug */ 63 REG_SD_HWRST = 0x78, /* Hardware Reset Register */ 64 REG_SD_DMAC = 0x80, /* Internal DMA Controller Control */ 65 REG_SD_DLBA = 0x84, /* Descriptor List Base Address */ 66 REG_SD_IDST = 0x88, /* Internal DMA Controller Status */ 67 REG_SD_IDIE = 0x8C, /* Internal DMA Controller IRQ Enable */ 68 REG_SD_THLDC = 0x100, /* Card Threshold Control / FIFO (sun4i only)*/ 69 REG_SD_DSBD = 0x10C, /* eMMC DDR Start Bit Detection Control */ 70 REG_SD_RES_CRC = 0x110, /* Response CRC from card/eMMC */ 71 REG_SD_DATA7_CRC = 0x114, /* CRC Data 7 from card/eMMC */ 72 REG_SD_DATA6_CRC = 0x118, /* CRC Data 6 from card/eMMC */ 73 REG_SD_DATA5_CRC = 0x11C, /* CRC Data 5 from card/eMMC */ 74 REG_SD_DATA4_CRC = 0x120, /* CRC Data 4 from card/eMMC */ 75 REG_SD_DATA3_CRC = 0x124, /* CRC Data 3 from card/eMMC */ 76 REG_SD_DATA2_CRC = 0x128, /* CRC Data 2 from card/eMMC */ 77 REG_SD_DATA1_CRC = 0x12C, /* CRC Data 1 from card/eMMC */ 78 REG_SD_DATA0_CRC = 0x130, /* CRC Data 0 from card/eMMC */ 79 REG_SD_CRC_STA = 0x134, /* CRC status from card/eMMC during write */ 80 REG_SD_FIFO = 0x200, /* Read/Write FIFO */ 81 }; 82 83 /* SD Host register flags */ 84 enum { 85 SD_GCTL_FIFO_AC_MOD = (1 << 31), 86 SD_GCTL_DDR_MOD_SEL = (1 << 10), 87 SD_GCTL_CD_DBC_ENB = (1 << 8), 88 SD_GCTL_DMA_ENB = (1 << 5), 89 SD_GCTL_INT_ENB = (1 << 4), 90 SD_GCTL_DMA_RST = (1 << 2), 91 SD_GCTL_FIFO_RST = (1 << 1), 92 SD_GCTL_SOFT_RST = (1 << 0), 93 }; 94 95 enum { 96 SD_CMDR_LOAD = (1 << 31), 97 SD_CMDR_CLKCHANGE = (1 << 21), 98 SD_CMDR_WRITE = (1 << 10), 99 SD_CMDR_AUTOSTOP = (1 << 12), 100 SD_CMDR_DATA = (1 << 9), 101 SD_CMDR_RESPONSE_LONG = (1 << 7), 102 SD_CMDR_RESPONSE = (1 << 6), 103 SD_CMDR_CMDID_MASK = (0x3f), 104 }; 105 106 enum { 107 SD_RISR_CARD_REMOVE = (1 << 31), 108 SD_RISR_CARD_INSERT = (1 << 30), 109 SD_RISR_SDIO_INTR = (1 << 16), 110 SD_RISR_AUTOCMD_DONE = (1 << 14), 111 SD_RISR_DATA_COMPLETE = (1 << 3), 112 SD_RISR_CMD_COMPLETE = (1 << 2), 113 SD_RISR_NO_RESPONSE = (1 << 1), 114 }; 115 116 enum { 117 SD_STAR_FIFO_EMPTY = (1 << 2), 118 SD_STAR_CARD_PRESENT = (1 << 8), 119 SD_STAR_FIFO_LEVEL_1 = (1 << 17), 120 }; 121 122 enum { 123 SD_IDST_INT_SUMMARY = (1 << 8), 124 SD_IDST_RECEIVE_IRQ = (1 << 1), 125 SD_IDST_TRANSMIT_IRQ = (1 << 0), 126 SD_IDST_IRQ_MASK = (1 << 1) | (1 << 0) | (1 << 8), 127 SD_IDST_WR_MASK = (0x3ff), 128 }; 129 130 /* SD Host register reset values */ 131 enum { 132 REG_SD_GCTL_RST = 0x00000300, 133 REG_SD_CKCR_RST = 0x0, 134 REG_SD_TMOR_RST = 0xFFFFFF40, 135 REG_SD_BWDR_RST = 0x0, 136 REG_SD_BKSR_RST = 0x00000200, 137 REG_SD_BYCR_RST = 0x00000200, 138 REG_SD_CMDR_RST = 0x0, 139 REG_SD_CAGR_RST = 0x0, 140 REG_SD_RESP_RST = 0x0, 141 REG_SD_IMKR_RST = 0x0, 142 REG_SD_MISR_RST = 0x0, 143 REG_SD_RISR_RST = 0x0, 144 REG_SD_STAR_RST = 0x00000100, 145 REG_SD_FWLR_RST = 0x000F0000, 146 REG_SD_FUNS_RST = 0x0, 147 REG_SD_DBGC_RST = 0x0, 148 REG_SD_A12A_RST = 0x0000FFFF, 149 REG_SD_NTSR_RST = 0x00000001, 150 REG_SD_SDBG_RST = 0x0, 151 REG_SD_HWRST_RST = 0x00000001, 152 REG_SD_DMAC_RST = 0x0, 153 REG_SD_DLBA_RST = 0x0, 154 REG_SD_IDST_RST = 0x0, 155 REG_SD_IDIE_RST = 0x0, 156 REG_SD_THLDC_RST = 0x0, 157 REG_SD_DSBD_RST = 0x0, 158 REG_SD_RES_CRC_RST = 0x0, 159 REG_SD_DATA_CRC_RST = 0x0, 160 REG_SD_CRC_STA_RST = 0x0, 161 REG_SD_FIFO_RST = 0x0, 162 }; 163 164 /* Data transfer descriptor for DMA */ 165 typedef struct TransferDescriptor { 166 uint32_t status; /* Status flags */ 167 uint32_t size; /* Data buffer size */ 168 uint32_t addr; /* Data buffer address */ 169 uint32_t next; /* Physical address of next descriptor */ 170 } TransferDescriptor; 171 172 /* Data transfer descriptor flags */ 173 enum { 174 DESC_STATUS_HOLD = (1 << 31), /* Set when descriptor is in use by DMA */ 175 DESC_STATUS_ERROR = (1 << 30), /* Set when DMA transfer error occurred */ 176 DESC_STATUS_CHAIN = (1 << 4), /* Indicates chained descriptor. */ 177 DESC_STATUS_FIRST = (1 << 3), /* Set on the first descriptor */ 178 DESC_STATUS_LAST = (1 << 2), /* Set on the last descriptor */ 179 DESC_STATUS_NOIRQ = (1 << 1), /* Skip raising interrupt after transfer */ 180 DESC_SIZE_MASK = (0xfffffffc) 181 }; 182 183 static void allwinner_sdhost_update_irq(AwSdHostState *s) 184 { 185 uint32_t irq; 186 187 if (s->global_ctl & SD_GCTL_INT_ENB) { 188 irq = s->irq_status & s->irq_mask; 189 } else { 190 irq = 0; 191 } 192 193 trace_allwinner_sdhost_update_irq(irq); 194 qemu_set_irq(s->irq, irq); 195 } 196 197 static void allwinner_sdhost_update_transfer_cnt(AwSdHostState *s, 198 uint32_t bytes) 199 { 200 if (s->transfer_cnt > bytes) { 201 s->transfer_cnt -= bytes; 202 } else { 203 s->transfer_cnt = 0; 204 } 205 206 if (!s->transfer_cnt) { 207 s->irq_status |= SD_RISR_DATA_COMPLETE; 208 } 209 } 210 211 static void allwinner_sdhost_set_inserted(DeviceState *dev, bool inserted) 212 { 213 AwSdHostState *s = AW_SDHOST(dev); 214 215 trace_allwinner_sdhost_set_inserted(inserted); 216 217 if (inserted) { 218 s->irq_status |= SD_RISR_CARD_INSERT; 219 s->irq_status &= ~SD_RISR_CARD_REMOVE; 220 s->status |= SD_STAR_CARD_PRESENT; 221 } else { 222 s->irq_status &= ~SD_RISR_CARD_INSERT; 223 s->irq_status |= SD_RISR_CARD_REMOVE; 224 s->status &= ~SD_STAR_CARD_PRESENT; 225 } 226 227 allwinner_sdhost_update_irq(s); 228 } 229 230 static void allwinner_sdhost_send_command(AwSdHostState *s) 231 { 232 SDRequest request; 233 uint8_t resp[16]; 234 int rlen; 235 236 /* Auto clear load flag */ 237 s->command &= ~SD_CMDR_LOAD; 238 239 /* Clock change does not actually interact with the SD bus */ 240 if (!(s->command & SD_CMDR_CLKCHANGE)) { 241 242 /* Prepare request */ 243 request.cmd = s->command & SD_CMDR_CMDID_MASK; 244 request.arg = s->command_arg; 245 246 /* Send request to SD bus */ 247 rlen = sdbus_do_command(&s->sdbus, &request, resp); 248 if (rlen < 0) { 249 goto error; 250 } 251 252 /* If the command has a response, store it in the response registers */ 253 if ((s->command & SD_CMDR_RESPONSE)) { 254 if (rlen == 4 && !(s->command & SD_CMDR_RESPONSE_LONG)) { 255 s->response[0] = ldl_be_p(&resp[0]); 256 s->response[1] = s->response[2] = s->response[3] = 0; 257 258 } else if (rlen == 16 && (s->command & SD_CMDR_RESPONSE_LONG)) { 259 s->response[0] = ldl_be_p(&resp[12]); 260 s->response[1] = ldl_be_p(&resp[8]); 261 s->response[2] = ldl_be_p(&resp[4]); 262 s->response[3] = ldl_be_p(&resp[0]); 263 } else { 264 goto error; 265 } 266 } 267 } 268 269 /* Set interrupt status bits */ 270 s->irq_status |= SD_RISR_CMD_COMPLETE; 271 return; 272 273 error: 274 s->irq_status |= SD_RISR_NO_RESPONSE; 275 } 276 277 static void allwinner_sdhost_auto_stop(AwSdHostState *s) 278 { 279 /* 280 * The stop command (CMD12) ensures the SD bus 281 * returns to the transfer state. 282 */ 283 if ((s->command & SD_CMDR_AUTOSTOP) && (s->transfer_cnt == 0)) { 284 /* First save current command registers */ 285 uint32_t saved_cmd = s->command; 286 uint32_t saved_arg = s->command_arg; 287 288 /* Prepare stop command (CMD12) */ 289 s->command &= ~SD_CMDR_CMDID_MASK; 290 s->command |= 12; /* CMD12 */ 291 s->command_arg = 0; 292 293 /* Put the command on SD bus */ 294 allwinner_sdhost_send_command(s); 295 296 /* Restore command values */ 297 s->command = saved_cmd; 298 s->command_arg = saved_arg; 299 300 /* Set IRQ status bit for automatic stop done */ 301 s->irq_status |= SD_RISR_AUTOCMD_DONE; 302 } 303 } 304 305 static void read_descriptor(AwSdHostState *s, hwaddr desc_addr, 306 TransferDescriptor *desc) 307 { 308 uint32_t desc_words[4]; 309 dma_memory_read(&s->dma_as, desc_addr, &desc_words, sizeof(desc_words), 310 MEMTXATTRS_UNSPECIFIED); 311 desc->status = le32_to_cpu(desc_words[0]); 312 desc->size = le32_to_cpu(desc_words[1]); 313 desc->addr = le32_to_cpu(desc_words[2]); 314 desc->next = le32_to_cpu(desc_words[3]); 315 } 316 317 static void write_descriptor(AwSdHostState *s, hwaddr desc_addr, 318 const TransferDescriptor *desc) 319 { 320 uint32_t desc_words[4]; 321 desc_words[0] = cpu_to_le32(desc->status); 322 desc_words[1] = cpu_to_le32(desc->size); 323 desc_words[2] = cpu_to_le32(desc->addr); 324 desc_words[3] = cpu_to_le32(desc->next); 325 dma_memory_write(&s->dma_as, desc_addr, &desc_words, sizeof(desc_words), 326 MEMTXATTRS_UNSPECIFIED); 327 } 328 329 static uint32_t allwinner_sdhost_process_desc(AwSdHostState *s, 330 hwaddr desc_addr, 331 TransferDescriptor *desc, 332 bool is_write, uint32_t max_bytes) 333 { 334 AwSdHostClass *klass = AW_SDHOST_GET_CLASS(s); 335 uint32_t num_done = 0; 336 uint32_t num_bytes = max_bytes; 337 uint8_t buf[1024]; 338 339 read_descriptor(s, desc_addr, desc); 340 if (desc->size == 0) { 341 desc->size = klass->max_desc_size; 342 } else if (desc->size > klass->max_desc_size) { 343 qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA descriptor buffer size " 344 " is out-of-bounds: %" PRIu32 " > %zu", 345 __func__, desc->size, klass->max_desc_size); 346 desc->size = klass->max_desc_size; 347 } 348 if (desc->size < num_bytes) { 349 num_bytes = desc->size; 350 } 351 352 trace_allwinner_sdhost_process_desc(desc_addr, desc->size, 353 is_write, max_bytes); 354 355 while (num_done < num_bytes) { 356 /* Try to completely fill the local buffer */ 357 uint32_t buf_bytes = num_bytes - num_done; 358 if (buf_bytes > sizeof(buf)) { 359 buf_bytes = sizeof(buf); 360 } 361 362 /* Write to SD bus */ 363 if (is_write) { 364 dma_memory_read(&s->dma_as, 365 (desc->addr & DESC_SIZE_MASK) + num_done, buf, 366 buf_bytes, MEMTXATTRS_UNSPECIFIED); 367 sdbus_write_data(&s->sdbus, buf, buf_bytes); 368 369 /* Read from SD bus */ 370 } else { 371 sdbus_read_data(&s->sdbus, buf, buf_bytes); 372 dma_memory_write(&s->dma_as, 373 (desc->addr & DESC_SIZE_MASK) + num_done, buf, 374 buf_bytes, MEMTXATTRS_UNSPECIFIED); 375 } 376 num_done += buf_bytes; 377 } 378 379 /* Clear hold flag and flush descriptor */ 380 desc->status &= ~DESC_STATUS_HOLD; 381 write_descriptor(s, desc_addr, desc); 382 383 return num_done; 384 } 385 386 static void allwinner_sdhost_dma(AwSdHostState *s) 387 { 388 TransferDescriptor desc; 389 hwaddr desc_addr = s->desc_base; 390 bool is_write = (s->command & SD_CMDR_WRITE); 391 uint32_t bytes_done = 0; 392 393 /* Check if DMA can be performed */ 394 if (s->byte_count == 0 || s->block_size == 0 || 395 !(s->global_ctl & SD_GCTL_DMA_ENB)) { 396 return; 397 } 398 399 /* 400 * For read operations, data must be available on the SD bus 401 * If not, it is an error and we should not act at all 402 */ 403 if (!is_write && !sdbus_data_ready(&s->sdbus)) { 404 return; 405 } 406 407 /* Process the DMA descriptors until all data is copied */ 408 while (s->byte_count > 0) { 409 bytes_done = allwinner_sdhost_process_desc(s, desc_addr, &desc, 410 is_write, s->byte_count); 411 allwinner_sdhost_update_transfer_cnt(s, bytes_done); 412 413 if (bytes_done <= s->byte_count) { 414 s->byte_count -= bytes_done; 415 } else { 416 s->byte_count = 0; 417 } 418 419 if (desc.status & DESC_STATUS_LAST) { 420 break; 421 } else { 422 desc_addr = desc.next; 423 } 424 } 425 426 /* Raise IRQ to signal DMA is completed */ 427 s->irq_status |= SD_RISR_DATA_COMPLETE | SD_RISR_SDIO_INTR; 428 429 /* Update DMAC bits */ 430 s->dmac_status |= SD_IDST_INT_SUMMARY; 431 432 if (is_write) { 433 s->dmac_status |= SD_IDST_TRANSMIT_IRQ; 434 } else { 435 s->dmac_status |= SD_IDST_RECEIVE_IRQ; 436 } 437 } 438 439 static uint32_t allwinner_sdhost_fifo_read(AwSdHostState *s) 440 { 441 uint32_t res = 0; 442 443 if (sdbus_data_ready(&s->sdbus)) { 444 sdbus_read_data(&s->sdbus, &res, sizeof(uint32_t)); 445 le32_to_cpus(&res); 446 allwinner_sdhost_update_transfer_cnt(s, sizeof(uint32_t)); 447 allwinner_sdhost_auto_stop(s); 448 allwinner_sdhost_update_irq(s); 449 } else { 450 qemu_log_mask(LOG_GUEST_ERROR, "%s: no data ready on SD bus\n", 451 __func__); 452 } 453 454 return res; 455 } 456 457 static uint64_t allwinner_sdhost_read(void *opaque, hwaddr offset, 458 unsigned size) 459 { 460 AwSdHostState *s = AW_SDHOST(opaque); 461 AwSdHostClass *sc = AW_SDHOST_GET_CLASS(s); 462 uint32_t res = 0; 463 464 switch (offset) { 465 case REG_SD_GCTL: /* Global Control */ 466 res = s->global_ctl; 467 break; 468 case REG_SD_CKCR: /* Clock Control */ 469 res = s->clock_ctl; 470 break; 471 case REG_SD_TMOR: /* Timeout */ 472 res = s->timeout; 473 break; 474 case REG_SD_BWDR: /* Bus Width */ 475 res = s->bus_width; 476 break; 477 case REG_SD_BKSR: /* Block Size */ 478 res = s->block_size; 479 break; 480 case REG_SD_BYCR: /* Byte Count */ 481 res = s->byte_count; 482 break; 483 case REG_SD_CMDR: /* Command */ 484 res = s->command; 485 break; 486 case REG_SD_CAGR: /* Command Argument */ 487 res = s->command_arg; 488 break; 489 case REG_SD_RESP0: /* Response Zero */ 490 res = s->response[0]; 491 break; 492 case REG_SD_RESP1: /* Response One */ 493 res = s->response[1]; 494 break; 495 case REG_SD_RESP2: /* Response Two */ 496 res = s->response[2]; 497 break; 498 case REG_SD_RESP3: /* Response Three */ 499 res = s->response[3]; 500 break; 501 case REG_SD_IMKR: /* Interrupt Mask */ 502 res = s->irq_mask; 503 break; 504 case REG_SD_MISR: /* Masked Interrupt Status */ 505 res = s->irq_status & s->irq_mask; 506 break; 507 case REG_SD_RISR: /* Raw Interrupt Status */ 508 res = s->irq_status; 509 break; 510 case REG_SD_STAR: /* Status */ 511 res = s->status; 512 if (sdbus_data_ready(&s->sdbus)) { 513 res |= SD_STAR_FIFO_LEVEL_1; 514 } else { 515 res |= SD_STAR_FIFO_EMPTY; 516 } 517 break; 518 case REG_SD_FWLR: /* FIFO Water Level */ 519 res = s->fifo_wlevel; 520 break; 521 case REG_SD_FUNS: /* FIFO Function Select */ 522 res = s->fifo_func_sel; 523 break; 524 case REG_SD_DBGC: /* Debug Enable */ 525 res = s->debug_enable; 526 break; 527 case REG_SD_A12A: /* Auto command 12 argument */ 528 res = s->auto12_arg; 529 break; 530 case REG_SD_NTSR: /* SD NewTiming Set */ 531 res = s->newtiming_set; 532 break; 533 case REG_SD_SDBG: /* SD newTiming Set Debug */ 534 res = s->newtiming_debug; 535 break; 536 case REG_SD_HWRST: /* Hardware Reset Register */ 537 res = s->hardware_rst; 538 break; 539 case REG_SD_DMAC: /* Internal DMA Controller Control */ 540 res = s->dmac; 541 break; 542 case REG_SD_DLBA: /* Descriptor List Base Address */ 543 res = s->desc_base; 544 break; 545 case REG_SD_IDST: /* Internal DMA Controller Status */ 546 res = s->dmac_status; 547 break; 548 case REG_SD_IDIE: /* Internal DMA Controller Interrupt Enable */ 549 res = s->dmac_irq; 550 break; 551 case REG_SD_THLDC: /* Card Threshold Control or FIFO register (sun4i) */ 552 if (sc->is_sun4i) { 553 res = allwinner_sdhost_fifo_read(s); 554 } else { 555 res = s->card_threshold; 556 } 557 break; 558 case REG_SD_DSBD: /* eMMC DDR Start Bit Detection Control */ 559 res = s->startbit_detect; 560 break; 561 case REG_SD_RES_CRC: /* Response CRC from card/eMMC */ 562 res = s->response_crc; 563 break; 564 case REG_SD_DATA7_CRC: /* CRC Data 7 from card/eMMC */ 565 case REG_SD_DATA6_CRC: /* CRC Data 6 from card/eMMC */ 566 case REG_SD_DATA5_CRC: /* CRC Data 5 from card/eMMC */ 567 case REG_SD_DATA4_CRC: /* CRC Data 4 from card/eMMC */ 568 case REG_SD_DATA3_CRC: /* CRC Data 3 from card/eMMC */ 569 case REG_SD_DATA2_CRC: /* CRC Data 2 from card/eMMC */ 570 case REG_SD_DATA1_CRC: /* CRC Data 1 from card/eMMC */ 571 case REG_SD_DATA0_CRC: /* CRC Data 0 from card/eMMC */ 572 res = s->data_crc[((offset - REG_SD_DATA7_CRC) / sizeof(uint32_t))]; 573 break; 574 case REG_SD_CRC_STA: /* CRC status from card/eMMC in write operation */ 575 res = s->status_crc; 576 break; 577 case REG_SD_FIFO: /* Read/Write FIFO */ 578 res = allwinner_sdhost_fifo_read(s); 579 break; 580 default: 581 qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset %" 582 HWADDR_PRIx"\n", __func__, offset); 583 res = 0; 584 break; 585 } 586 587 trace_allwinner_sdhost_read(offset, res, size); 588 return res; 589 } 590 591 static void allwinner_sdhost_fifo_write(AwSdHostState *s, uint64_t value) 592 { 593 uint32_t u32 = cpu_to_le32(value); 594 sdbus_write_data(&s->sdbus, &u32, sizeof(u32)); 595 allwinner_sdhost_update_transfer_cnt(s, sizeof(u32)); 596 allwinner_sdhost_auto_stop(s); 597 allwinner_sdhost_update_irq(s); 598 } 599 600 static void allwinner_sdhost_write(void *opaque, hwaddr offset, 601 uint64_t value, unsigned size) 602 { 603 AwSdHostState *s = AW_SDHOST(opaque); 604 AwSdHostClass *sc = AW_SDHOST_GET_CLASS(s); 605 606 trace_allwinner_sdhost_write(offset, value, size); 607 608 switch (offset) { 609 case REG_SD_GCTL: /* Global Control */ 610 s->global_ctl = value; 611 s->global_ctl &= ~(SD_GCTL_DMA_RST | SD_GCTL_FIFO_RST | 612 SD_GCTL_SOFT_RST); 613 allwinner_sdhost_update_irq(s); 614 break; 615 case REG_SD_CKCR: /* Clock Control */ 616 s->clock_ctl = value; 617 break; 618 case REG_SD_TMOR: /* Timeout */ 619 s->timeout = value; 620 break; 621 case REG_SD_BWDR: /* Bus Width */ 622 s->bus_width = value; 623 break; 624 case REG_SD_BKSR: /* Block Size */ 625 s->block_size = value; 626 break; 627 case REG_SD_BYCR: /* Byte Count */ 628 s->byte_count = value; 629 s->transfer_cnt = value; 630 break; 631 case REG_SD_CMDR: /* Command */ 632 s->command = value; 633 if (value & SD_CMDR_LOAD) { 634 allwinner_sdhost_send_command(s); 635 allwinner_sdhost_dma(s); 636 allwinner_sdhost_auto_stop(s); 637 } 638 allwinner_sdhost_update_irq(s); 639 break; 640 case REG_SD_CAGR: /* Command Argument */ 641 s->command_arg = value; 642 break; 643 case REG_SD_RESP0: /* Response Zero */ 644 s->response[0] = value; 645 break; 646 case REG_SD_RESP1: /* Response One */ 647 s->response[1] = value; 648 break; 649 case REG_SD_RESP2: /* Response Two */ 650 s->response[2] = value; 651 break; 652 case REG_SD_RESP3: /* Response Three */ 653 s->response[3] = value; 654 break; 655 case REG_SD_IMKR: /* Interrupt Mask */ 656 s->irq_mask = value; 657 allwinner_sdhost_update_irq(s); 658 break; 659 case REG_SD_MISR: /* Masked Interrupt Status */ 660 case REG_SD_RISR: /* Raw Interrupt Status */ 661 s->irq_status &= ~value; 662 allwinner_sdhost_update_irq(s); 663 break; 664 case REG_SD_STAR: /* Status */ 665 s->status &= ~value; 666 allwinner_sdhost_update_irq(s); 667 break; 668 case REG_SD_FWLR: /* FIFO Water Level */ 669 s->fifo_wlevel = value; 670 break; 671 case REG_SD_FUNS: /* FIFO Function Select */ 672 s->fifo_func_sel = value; 673 break; 674 case REG_SD_DBGC: /* Debug Enable */ 675 s->debug_enable = value; 676 break; 677 case REG_SD_A12A: /* Auto command 12 argument */ 678 s->auto12_arg = value; 679 break; 680 case REG_SD_NTSR: /* SD NewTiming Set */ 681 s->newtiming_set = value; 682 break; 683 case REG_SD_SDBG: /* SD newTiming Set Debug */ 684 s->newtiming_debug = value; 685 break; 686 case REG_SD_HWRST: /* Hardware Reset Register */ 687 s->hardware_rst = value; 688 break; 689 case REG_SD_DMAC: /* Internal DMA Controller Control */ 690 s->dmac = value; 691 allwinner_sdhost_update_irq(s); 692 break; 693 case REG_SD_DLBA: /* Descriptor List Base Address */ 694 s->desc_base = value; 695 break; 696 case REG_SD_IDST: /* Internal DMA Controller Status */ 697 s->dmac_status &= (~SD_IDST_WR_MASK) | (~value & SD_IDST_WR_MASK); 698 allwinner_sdhost_update_irq(s); 699 break; 700 case REG_SD_IDIE: /* Internal DMA Controller Interrupt Enable */ 701 s->dmac_irq = value; 702 allwinner_sdhost_update_irq(s); 703 break; 704 case REG_SD_THLDC: /* Card Threshold Control or FIFO (sun4i) */ 705 if (sc->is_sun4i) { 706 allwinner_sdhost_fifo_write(s, value); 707 } else { 708 s->card_threshold = value; 709 } 710 break; 711 case REG_SD_DSBD: /* eMMC DDR Start Bit Detection Control */ 712 s->startbit_detect = value; 713 break; 714 case REG_SD_FIFO: /* Read/Write FIFO */ 715 allwinner_sdhost_fifo_write(s, value); 716 break; 717 case REG_SD_RES_CRC: /* Response CRC from card/eMMC */ 718 case REG_SD_DATA7_CRC: /* CRC Data 7 from card/eMMC */ 719 case REG_SD_DATA6_CRC: /* CRC Data 6 from card/eMMC */ 720 case REG_SD_DATA5_CRC: /* CRC Data 5 from card/eMMC */ 721 case REG_SD_DATA4_CRC: /* CRC Data 4 from card/eMMC */ 722 case REG_SD_DATA3_CRC: /* CRC Data 3 from card/eMMC */ 723 case REG_SD_DATA2_CRC: /* CRC Data 2 from card/eMMC */ 724 case REG_SD_DATA1_CRC: /* CRC Data 1 from card/eMMC */ 725 case REG_SD_DATA0_CRC: /* CRC Data 0 from card/eMMC */ 726 case REG_SD_CRC_STA: /* CRC status from card/eMMC in write operation */ 727 break; 728 default: 729 qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset %" 730 HWADDR_PRIx"\n", __func__, offset); 731 break; 732 } 733 } 734 735 static const MemoryRegionOps allwinner_sdhost_ops = { 736 .read = allwinner_sdhost_read, 737 .write = allwinner_sdhost_write, 738 .endianness = DEVICE_NATIVE_ENDIAN, 739 .valid = { 740 .min_access_size = 4, 741 .max_access_size = 4, 742 }, 743 .impl.min_access_size = 4, 744 }; 745 746 static const VMStateDescription vmstate_allwinner_sdhost = { 747 .name = "allwinner-sdhost", 748 .version_id = 1, 749 .minimum_version_id = 1, 750 .fields = (VMStateField[]) { 751 VMSTATE_UINT32(global_ctl, AwSdHostState), 752 VMSTATE_UINT32(clock_ctl, AwSdHostState), 753 VMSTATE_UINT32(timeout, AwSdHostState), 754 VMSTATE_UINT32(bus_width, AwSdHostState), 755 VMSTATE_UINT32(block_size, AwSdHostState), 756 VMSTATE_UINT32(byte_count, AwSdHostState), 757 VMSTATE_UINT32(transfer_cnt, AwSdHostState), 758 VMSTATE_UINT32(command, AwSdHostState), 759 VMSTATE_UINT32(command_arg, AwSdHostState), 760 VMSTATE_UINT32_ARRAY(response, AwSdHostState, 4), 761 VMSTATE_UINT32(irq_mask, AwSdHostState), 762 VMSTATE_UINT32(irq_status, AwSdHostState), 763 VMSTATE_UINT32(status, AwSdHostState), 764 VMSTATE_UINT32(fifo_wlevel, AwSdHostState), 765 VMSTATE_UINT32(fifo_func_sel, AwSdHostState), 766 VMSTATE_UINT32(debug_enable, AwSdHostState), 767 VMSTATE_UINT32(auto12_arg, AwSdHostState), 768 VMSTATE_UINT32(newtiming_set, AwSdHostState), 769 VMSTATE_UINT32(newtiming_debug, AwSdHostState), 770 VMSTATE_UINT32(hardware_rst, AwSdHostState), 771 VMSTATE_UINT32(dmac, AwSdHostState), 772 VMSTATE_UINT32(desc_base, AwSdHostState), 773 VMSTATE_UINT32(dmac_status, AwSdHostState), 774 VMSTATE_UINT32(dmac_irq, AwSdHostState), 775 VMSTATE_UINT32(card_threshold, AwSdHostState), 776 VMSTATE_UINT32(startbit_detect, AwSdHostState), 777 VMSTATE_UINT32(response_crc, AwSdHostState), 778 VMSTATE_UINT32_ARRAY(data_crc, AwSdHostState, 8), 779 VMSTATE_UINT32(status_crc, AwSdHostState), 780 VMSTATE_END_OF_LIST() 781 } 782 }; 783 784 static Property allwinner_sdhost_properties[] = { 785 DEFINE_PROP_LINK("dma-memory", AwSdHostState, dma_mr, 786 TYPE_MEMORY_REGION, MemoryRegion *), 787 DEFINE_PROP_END_OF_LIST(), 788 }; 789 790 static void allwinner_sdhost_init(Object *obj) 791 { 792 AwSdHostState *s = AW_SDHOST(obj); 793 794 qbus_init(&s->sdbus, sizeof(s->sdbus), 795 TYPE_AW_SDHOST_BUS, DEVICE(s), "sd-bus"); 796 797 memory_region_init_io(&s->iomem, obj, &allwinner_sdhost_ops, s, 798 TYPE_AW_SDHOST, 4 * KiB); 799 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem); 800 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->irq); 801 } 802 803 static void allwinner_sdhost_realize(DeviceState *dev, Error **errp) 804 { 805 AwSdHostState *s = AW_SDHOST(dev); 806 807 if (!s->dma_mr) { 808 error_setg(errp, TYPE_AW_SDHOST " 'dma-memory' link not set"); 809 return; 810 } 811 812 address_space_init(&s->dma_as, s->dma_mr, "sdhost-dma"); 813 } 814 815 static void allwinner_sdhost_reset(DeviceState *dev) 816 { 817 AwSdHostState *s = AW_SDHOST(dev); 818 819 s->global_ctl = REG_SD_GCTL_RST; 820 s->clock_ctl = REG_SD_CKCR_RST; 821 s->timeout = REG_SD_TMOR_RST; 822 s->bus_width = REG_SD_BWDR_RST; 823 s->block_size = REG_SD_BKSR_RST; 824 s->byte_count = REG_SD_BYCR_RST; 825 s->transfer_cnt = 0; 826 827 s->command = REG_SD_CMDR_RST; 828 s->command_arg = REG_SD_CAGR_RST; 829 830 for (int i = 0; i < ARRAY_SIZE(s->response); i++) { 831 s->response[i] = REG_SD_RESP_RST; 832 } 833 834 s->irq_mask = REG_SD_IMKR_RST; 835 s->irq_status = REG_SD_RISR_RST; 836 s->status = REG_SD_STAR_RST; 837 838 s->fifo_wlevel = REG_SD_FWLR_RST; 839 s->fifo_func_sel = REG_SD_FUNS_RST; 840 s->debug_enable = REG_SD_DBGC_RST; 841 s->auto12_arg = REG_SD_A12A_RST; 842 s->newtiming_set = REG_SD_NTSR_RST; 843 s->newtiming_debug = REG_SD_SDBG_RST; 844 s->hardware_rst = REG_SD_HWRST_RST; 845 s->dmac = REG_SD_DMAC_RST; 846 s->desc_base = REG_SD_DLBA_RST; 847 s->dmac_status = REG_SD_IDST_RST; 848 s->dmac_irq = REG_SD_IDIE_RST; 849 s->card_threshold = REG_SD_THLDC_RST; 850 s->startbit_detect = REG_SD_DSBD_RST; 851 s->response_crc = REG_SD_RES_CRC_RST; 852 853 for (int i = 0; i < ARRAY_SIZE(s->data_crc); i++) { 854 s->data_crc[i] = REG_SD_DATA_CRC_RST; 855 } 856 857 s->status_crc = REG_SD_CRC_STA_RST; 858 } 859 860 static void allwinner_sdhost_bus_class_init(ObjectClass *klass, void *data) 861 { 862 SDBusClass *sbc = SD_BUS_CLASS(klass); 863 864 sbc->set_inserted = allwinner_sdhost_set_inserted; 865 } 866 867 static void allwinner_sdhost_class_init(ObjectClass *klass, void *data) 868 { 869 DeviceClass *dc = DEVICE_CLASS(klass); 870 871 dc->reset = allwinner_sdhost_reset; 872 dc->vmsd = &vmstate_allwinner_sdhost; 873 dc->realize = allwinner_sdhost_realize; 874 device_class_set_props(dc, allwinner_sdhost_properties); 875 } 876 877 static void allwinner_sdhost_sun4i_class_init(ObjectClass *klass, void *data) 878 { 879 AwSdHostClass *sc = AW_SDHOST_CLASS(klass); 880 sc->max_desc_size = 8 * KiB; 881 sc->is_sun4i = true; 882 } 883 884 static void allwinner_sdhost_sun5i_class_init(ObjectClass *klass, void *data) 885 { 886 AwSdHostClass *sc = AW_SDHOST_CLASS(klass); 887 sc->max_desc_size = 64 * KiB; 888 sc->is_sun4i = false; 889 } 890 891 static const TypeInfo allwinner_sdhost_info = { 892 .name = TYPE_AW_SDHOST, 893 .parent = TYPE_SYS_BUS_DEVICE, 894 .instance_init = allwinner_sdhost_init, 895 .instance_size = sizeof(AwSdHostState), 896 .class_init = allwinner_sdhost_class_init, 897 .class_size = sizeof(AwSdHostClass), 898 .abstract = true, 899 }; 900 901 static const TypeInfo allwinner_sdhost_sun4i_info = { 902 .name = TYPE_AW_SDHOST_SUN4I, 903 .parent = TYPE_AW_SDHOST, 904 .class_init = allwinner_sdhost_sun4i_class_init, 905 }; 906 907 static const TypeInfo allwinner_sdhost_sun5i_info = { 908 .name = TYPE_AW_SDHOST_SUN5I, 909 .parent = TYPE_AW_SDHOST, 910 .class_init = allwinner_sdhost_sun5i_class_init, 911 }; 912 913 static const TypeInfo allwinner_sdhost_bus_info = { 914 .name = TYPE_AW_SDHOST_BUS, 915 .parent = TYPE_SD_BUS, 916 .instance_size = sizeof(SDBus), 917 .class_init = allwinner_sdhost_bus_class_init, 918 }; 919 920 static void allwinner_sdhost_register_types(void) 921 { 922 type_register_static(&allwinner_sdhost_info); 923 type_register_static(&allwinner_sdhost_sun4i_info); 924 type_register_static(&allwinner_sdhost_sun5i_info); 925 type_register_static(&allwinner_sdhost_bus_info); 926 } 927 928 type_init(allwinner_sdhost_register_types) 929