1 /* 2 * MSC (MMC/SD controller) peripheral support. 3 * 4 * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (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, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/devices/hw_mmio_register_block.h> 23 #include <l4/sys/irq.h> 24 #include <l4/util/util.h> 25 26 #include <stdio.h> 27 #include <string.h> 28 29 #include "msc-common.h" 30 31 32 33 // Register locations for each channel. 34 35 enum Regs : unsigned 36 { 37 Msc_control = 0x000, // MSC_CTRL 38 Msc_status = 0x004, // MSC_STAT 39 Msc_clock_rate = 0x008, // MSC_CLKRT 40 Msc_command_data_control = 0x00c, // MSC_CMDAT 41 Msc_response_timeout = 0x010, // MSC_RESTO 42 Msc_read_timeout = 0x014, // MSC_RDTO 43 Msc_block_length = 0x018, // MSC_BLKLEN 44 Msc_block_count = 0x01c, // MSC_NOB 45 Msc_block_success_count = 0x020, // MSC_SNOB 46 Msc_interrupt_mask = 0x024, // MSC_IMASK 47 Msc_interrupt_flag = 0x028, // MSC_IFLG/MSC_IREG 48 Msc_command_index = 0x02c, // MSC_CMD 49 Msc_command_argument = 0x030, // MSC_ARG 50 Msc_response_fifo = 0x034, // MSC_RES 51 Msc_recv_data_fifo = 0x038, // MSC_RXFIFO 52 Msc_trans_data_fifo = 0x03c, // MSC_TXFIFO 53 54 // JZ4780/X1600 only. 55 56 Msc_low_power_mode = 0x040, // MSC_LPM 57 Msc_dma_control = 0x044, // MSC_DMAC 58 Msc_dma_descriptor_address = 0x048, // MSC_DMANDA 59 Msc_dma_data_address = 0x04c, // MSC_DMADA 60 Msc_dma_data_length = 0x050, // MSC_DMALEN 61 Msc_dma_command = 0x054, // MSC_DMACMD 62 Msc_control2 = 0x058, // MSC_CTRL2 63 Msc_rtfifo_data_counter = 0x05c, // MSC_RTCNT 64 65 // Channel block size/offset. 66 67 Msc_channel_offset = 0x10000, 68 }; 69 70 // Field definitions. 71 72 enum Control_bits : unsigned 73 { 74 // JZ4780/X1600 only. 75 76 Control_send_ccsd = 0x8000, // SEND_CCSD 77 Control_send_ccsd_automatically = 0x4000, // SEND_CCSD 78 79 // Common. 80 81 Control_exit_multiple = 0x0080, // EXIT_MULTIPLE 82 Control_exit_transfer = 0x0040, // EXIT_TRANSFER 83 Control_start_read_wait = 0x0020, // START_READ_WAIT 84 Control_stop_read_wait = 0x0010, // STOP_READ_WAIT 85 Control_reset = 0x0008, // RESET 86 Control_start_operation = 0x0004, // START_OP 87 88 Control_clock_control_field_mask = 0x3, // CLOCK_CTRL 89 Control_clock_control_start = 2, 90 Control_clock_control_stop = 1, 91 Control_clock_control_field_shift = 0, 92 }; 93 94 enum Control2_bits : unsigned 95 { 96 // JZ4780/X1600 only. 97 98 Control2_pin_level_polarity_field_mask = 0x1f, // PIP 99 Control2_pin_level_polarity_field_shift = 24, 100 101 // JZ4780 only. 102 103 Control2_reset_enable = 0x00800000, // RST_EN 104 105 // JZ4780/X1600 only. 106 107 Control2_stop_read_operation_mode = 0x00000010, // STPRM 108 109 // JZ4780 only. 110 111 Control2_signal_voltage_change = 0x00000008, // SVC 112 113 // JZ4780/X1600 only. 114 115 Control2_speed_mode_field_mask = 0x7, // SMS 116 Control2_speed_mode_default = 0, // = 0 117 Control2_speed_mode_high = 1, // = 1 118 Control2_speed_mode_sdr12 = 2, // = 2 119 Control2_speed_mode_sdr25 = 3, // = 3 120 Control2_speed_mode_sdr50 = 4, // = 4 121 Control2_speed_mode_field_shift = 0, 122 }; 123 124 enum Status_bits : unsigned 125 { 126 // JZ4780/X1600 only. 127 128 Status_auto_cmd12_done = 0x80000000, // AUTO_CMD12_DONE 129 130 // JZ4780 only. 131 132 Status_auto_cmd23_done = 0x40000000, // AUTO_CMD23_DONE 133 Status_signal_voltage_change = 0x20000000, // SVS 134 135 // JZ4780/X1600 only. 136 137 Status_pin_level_field_mask = 0x1f, // PIN_LEVEL 138 Status_pin_level_field_shift = 24, 139 140 Status_boot_crc_error = 0x00100000, // BCE 141 Status_boot_data_end = 0x00080000, // BDE 142 Status_boot_ack_error = 0x00040000, // BAE 143 Status_boot_ack_received = 0x00020000, // BAR 144 Status_dma_end = 0x00010000, // DMAEND 145 146 // Common. 147 148 Status_resetting = 0x8000, // IS_RESETTING 149 Status_sdio_interrupt_active = 0x4000, // SDIO_INT_ACTIVE 150 Status_programming_done = 0x2000, // PRG_DONE 151 Status_data_transfer_done = 0x1000, // DATA_TRAN_DONE 152 Status_end_command_response = 0x0800, // END_CMD_RES 153 Status_data_fifo_almost_full = 0x0400, // DATA_FIFO_AFULL 154 Status_read_wait = 0x0200, // IS_READWAIT 155 Status_clock_enabled = 0x0100, // CLK_EN 156 Status_data_fifo_full = 0x0080, // DATA_FIFO_FULL 157 Status_data_fifo_empty = 0x0040, // DATA_FIFO_EMPTY 158 Status_response_crc_error = 0x0020, // CRC_RES_ERR 159 Status_read_crc_error = 0x0010, // CRC_READ_ERROR 160 Status_write_crc_error_no_status = 0x0008, // CRC_WRITE_ERROR (2) 161 Status_write_crc_error_data = 0x0004, // CRC_WRITE_ERROR (1) 162 Status_timeout_response = 0x0002, // TIME_OUT_RES 163 Status_timeout_read = 0x0001, // TIME_OUT_READ 164 }; 165 166 enum Clock_rate_bits : unsigned 167 { 168 Clock_rate_field_mask = 0x7, // CLK_RATE 169 Clock_rate_field_shift = 0, 170 }; 171 172 enum Command_data_control_bits : unsigned 173 { 174 // JZ4780/X1600 only. 175 176 Cdc_ccs_expected = 0x80000000, // CCS_EXPECTED 177 Cdc_read_ce_ata = 0x40000000, // READ_CEATA 178 Cdc_disable_boot = 0x08000000, // DIS_BOOT 179 Cdc_expect_boot_ack = 0x02000000, // EXP_BOOT_ACK 180 Cdc_alternative_boot_mode = 0x01000000, // BOOT_MODE 181 182 // JZ4780 only. 183 184 Cdc_auto_cmd23 = 0x00040000, // AUTO_CMD23 185 186 // JZ4780/X1600 only. 187 188 Cdc_sdio_interrupt_2cycle = 0x00020000, // SDIO_PRDT 189 Cdc_auto_cmd12 = 0x00010000, // AUTO_CMD12 190 191 Cdc_recv_fifo_level_field_mask = 0x3, // RTRG 192 Cdc_fifo_level_16 = 0, 193 Cdc_fifo_level_32 = 1, 194 Cdc_fifo_level_64 = 2, 195 Cdc_fifo_level_96 = 3, 196 Cdc_recv_fifo_level_field_shift = 14, 197 198 Cdc_trans_fifo_level_field_mask = 0x3, // TTRG 199 Cdc_trans_fifo_level_field_shift = 12, 200 201 // Common. 202 203 Cdc_io_abort = 0x0800, // IO_ABORT 204 205 Cdc_bus_width_field_mask = 0x3, // BUS_WIDTH 206 Cdc_bus_width_field_1bit = 0, // = 0 207 Cdc_bus_width_field_4bit = 2, // = 2 208 Cdc_bus_width_field_shift = 9, 209 210 // JZ4740 only. 211 212 Cdc_dma_enable = 0x0100, // DMA_EN 213 Cdc_dma_disable = 0x0000, 214 215 // Common. 216 217 Cdc_init_sequence = 0x0080, // INIT 218 219 Cdc_expect_busy = 0x0040, // BUSY 220 Cdc_do_not_expect_busy = 0x0000, 221 222 Cdc_stream_block = 0x0020, // STREAM_BLOCK 223 Cdc_not_stream_block = 0x0000, 224 225 Cdc_write_operation = 0x0010, // WRITE_READ 226 Cdc_read_operation = 0x0000, 227 228 Cdc_data_with_command = 0x0008, // DATA_EN 229 Cdc_no_data_with_command = 0x0000, 230 231 Cdc_response_format_field_mask = 0x7, // RESPONSE_FORMAT 232 Cdc_response_format_field_shift = 0, 233 }; 234 235 enum Response_timeout_bits : unsigned 236 { 237 // NOTE: 16-bit value in the JZ4780. 238 // NOTE: 32-bit value in the X1600. 239 240 Response_timeout_mask = 0x000000ff, // RES_TO 241 }; 242 243 enum Read_timeout_bits : unsigned 244 { 245 // NOTE: 16-bit value prior to the JZ4780/X1600. 246 247 Read_timeout_mask = 0xffffffff, // READ_TO 248 }; 249 250 enum Block_length_bits : unsigned 251 { 252 // NOTE: 16-bit value in the JZ4780/X1600. 253 254 Block_length_mask = 0x00000fff, // BLK_LEN 255 }; 256 257 enum Block_count_bits : unsigned 258 { 259 Block_count_mask = 0x0000ffff, // NOB/SNOB 260 }; 261 262 // Interrupt mask/flag bits. 263 264 enum Interrupt_bits : unsigned 265 { 266 // X1600 only. 267 268 Int_dma_data_done = 0x80000000, // DMA_DATA_DONE 269 270 // JZ4780 only. 271 272 Int_auto_cmd23_done = 0x40000000, // AUTO_CMD23_DONE 273 Int_signal_voltage_change = 0x20000000, // SVS 274 275 // JZ4780/X1600 only. 276 277 Int_pin_level_field_mask = 0x1f, // PIN_LEVEL 278 Int_pin_level_field_shift = 24, 279 280 // X1600 only. 281 282 Int_write_request_all_done = 0x00800000, // WR_ALL_DONE 283 284 // JZ4780/X1600 only. 285 286 Int_boot_crc_error = 0x00100000, // BCE 287 Int_boot_data_end = 0x00080000, // BDE 288 Int_boot_ack_error = 0x00040000, // BAE 289 Int_boot_ack_received = 0x00020000, // BAR 290 Int_dma_end = 0x00010000, // DMAEND 291 Int_auto_cmd12_done = 0x00008000, // AUTO_CMD12_DONE 292 Int_data_fifo_full = 0x00004000, // DATA_FIFO_FULL 293 Int_data_fifo_empty = 0x00002000, // DATA_FIFO_EMP 294 Int_crc_response_error = 0x00001000, // CRC_RES_ERR 295 Int_crc_read_error = 0x00000800, // CRC_READ_ERR 296 Int_crc_write_error = 0x00000400, // CRC_WRITE_ERR 297 Int_response_timeout = 0x00000200, // TIME_OUT_RES 298 Int_read_timeout = 0x00000100, // TIME_OUT_READ 299 300 // Common. 301 302 Int_sdio = 0x80, // SDIO 303 Int_trans_fifo_write_request = 0x40, // TXFIFO_WR_REQ 304 Int_recv_fifo_read_request = 0x20, // RXFIFO_RD_REQ 305 Int_end_command_response = 0x04, // END_CMD_RES 306 Int_programming_done = 0x02, // PRG_DONE 307 Int_data_transfer_done = 0x01, // DATA_TRAN_DONE 308 }; 309 310 enum Command_index_bits : unsigned 311 { 312 Command_index_mask = 0x0000003f, // CMD_INDEX 313 }; 314 315 enum Command_argument_bits : unsigned 316 { 317 Command_argument_mask = 0xffffffff, // ARG 318 }; 319 320 enum Response_fifo_bits : unsigned 321 { 322 Response_fifo_mask = 0x0000ffff, // DATA 323 }; 324 325 enum Recv_data_fifo_bits : unsigned 326 { 327 Recv_data_fifo_mask = 0xffffffff, // DATA 328 }; 329 330 enum Trans_data_fifo_bits : unsigned 331 { 332 Trans_data_fifo_mask = 0xffffffff, // DATA 333 }; 334 335 enum Low_power_mode_bits : unsigned 336 { 337 Low_power_mode_enable = 0x00000001, // LPM 338 }; 339 340 enum Dma_control_bits : unsigned 341 { 342 Dma_mode_specify_transfer_length = 0x80, // MODE_SEL 343 344 Dma_address_offset_field_mask = 0x3, // AOFST 345 Dma_address_offset_field_shift = 5, 346 347 Dma_align_enable = 0x10, // ALIGNEN 348 349 Dma_burst_type_field_mask = 0x3, // INCR 350 Dma_burst_type_incr16 = 0, 351 Dma_burst_type_incr32 = 1, 352 Dma_burst_type_incr64 = 2, 353 Dma_burst_type_field_shift = 2, 354 355 Dma_select_common_dma = 0x02, // DMASEL 356 Dma_select_special_dma = 0x00, 357 358 Dma_enable = 0x01, // DMAEN 359 Dma_disable = 0x00, 360 }; 361 362 363 364 // Command indexes. 365 366 enum Command_index : unsigned 367 { 368 Command_go_idle_state = 0, 369 Command_send_op_cond = 1, 370 Command_all_send_cid = 2, 371 Command_send_relative_addr = 3, // SD 372 Command_set_relative_addr = 3, // MMC 373 Command_set_dsr = 4, 374 Command_io_send_op_cond = 5, // SDIO 375 Command_select_deselect_card = 7, 376 Command_send_if_cond = 8, 377 Command_send_csd = 9, 378 Command_send_cid = 10, 379 Command_read_dat_until_stop = 11, 380 Command_stop_transmission = 12, 381 Command_send_status = 13, 382 Command_go_inactive_state = 15, 383 Command_set_blocklen = 16, 384 Command_read_single_block = 17, 385 Command_read_multiple_block = 18, 386 Command_write_dat_until_stop = 20, 387 Command_set_block_count = 23, 388 Command_write_block = 24, 389 Command_write_multiple_block = 25, 390 Command_program_cid = 26, 391 Command_program_csd = 27, 392 Command_set_write_prot = 28, 393 Command_clr_write_prot = 29, 394 Command_send_write_prot = 30, 395 Command_tag_sector_start = 32, 396 Command_tag_sector_end = 33, 397 Command_untag_sector = 34, 398 Command_tag_erase_group_start = 35, 399 Command_tag_erase_group_end = 36, 400 Command_untag_erase_group = 37, 401 Command_erase = 38, 402 Command_fast_io = 39, 403 Command_go_irq_state = 40, 404 Command_lock_unlock = 42, 405 Command_io_rw_direct = 52, // SDIO 406 Command_app_cmd = 55, 407 Command_gen_cmd = 56, 408 }; 409 410 // Application-specific command indexes, used by first issuing Command_app_cmd. 411 412 enum App_command_index : unsigned 413 { 414 App_command_set_bus_width = 6, 415 App_command_sd_status = 13, 416 App_command_send_num_wr_blocks = 22, 417 App_command_set_wr_block_erase_count = 23, 418 App_command_sd_send_op_cond = 41, 419 App_command_set_clr_card_detect = 42, 420 App_command_send_scr = 51, 421 App_command_read_ocr = 58, 422 }; 423 424 enum Bus_width_bits : unsigned 425 { 426 Bus_width_1bit = 0, 427 Bus_width_4bit = 2, 428 }; 429 430 // Command response sizes in 16-bit units. 431 432 enum Response_sizes : unsigned 433 { 434 Response_size_R1 = 3, 435 Response_size_R2 = 8, // omits the CRC and end bit 436 Response_size_R3 = 3, 437 Response_size_R4 = 3, 438 Response_size_R5 = 3, 439 Response_size_R6 = 3, 440 Response_size_R7 = 3, 441 }; 442 443 // SD_SEND_OP_COND argument flags. 444 445 enum Ocr_argument_flags : unsigned 446 { 447 Ocr_high_capacity_storage = 0x40000000, 448 }; 449 450 // SD_SEND_OP_COND response flags (R3). 451 452 enum Ocr_response_flags : unsigned 453 { 454 Ocr_card_powered_up = 0x80000000, 455 }; 456 457 // R1 status flags. 458 459 enum R1_status_flags : unsigned 460 { 461 R1_status_error_mask = 0xffff0000, 462 }; 463 464 465 466 // MMC response structures. 467 468 struct R1 469 { 470 uint8_t end_crc; 471 uint32_t status; 472 uint8_t index:6, trans_start:2; 473 } __attribute__((packed)); 474 475 struct R2 476 { 477 // uint8_t end_crc; (not retrieved) 478 479 union 480 { 481 uint8_t raw[15]; 482 struct CID cid; 483 struct CSD csd; 484 } payload; 485 486 uint8_t reserved_trans_start; 487 } __attribute__((packed)); 488 489 struct R3 490 { 491 uint8_t end_reserved; 492 uint32_t ocr; 493 uint8_t reserved_trans_start; 494 } __attribute__((packed)); 495 496 // SDIO response structures. 497 498 struct R4 499 { 500 uint8_t end_reserved; 501 uint32_t ocr:24, stuff:3, memory_present:1, number_io_functions:3, ready:1; 502 uint8_t reserved_trans_start; 503 } __attribute__((packed)); 504 505 struct R5 506 { 507 uint8_t end_crc; 508 uint8_t data; 509 uint8_t out_of_range:1, invalid_function_number:1, reserved:1, error:1, 510 io_current_state:2, illegal_command:1, crc_error:1; 511 uint16_t stuff; 512 uint8_t index:6, trans_start:2; 513 } __attribute__((packed)); 514 515 struct R6 516 { 517 uint8_t end_crc; 518 uint16_t status; 519 uint16_t rca; 520 uint8_t index:6, trans_start:2; 521 } __attribute__((packed)); 522 523 struct R7 524 { 525 uint8_t end_crc; 526 527 union 528 { 529 uint32_t check:8, voltage:4, reserved:20; 530 uint32_t raw; 531 } check_voltage; 532 533 uint8_t index:6, trans_start:2; 534 } __attribute__((packed)); 535 536 537 538 // Command frame: 539 // byte: start (1), direction (1), command (6) 540 // 4 bytes: argument 541 // byte: CRC (7), end (1) 542 543 // IO_RW_DIRECT argument: 544 // Argument MSB to LSB: R/W (1), function number (3), read after write flag (1), 545 // stuff (1), register address (17), stuff (1), 546 // write data or stuff (8) 547 // 0x88000c08: W, function = 0, read after write, register address = 6 (CCCR), 548 // data = 8 (reset) 549 550 const uint32_t Io_rw_direct_reset = 0x88000c08; 551 552 // (IO_)SEND_OP_COND argument and default voltage range expected in R3, R4: 553 // Argument MSB to LSB: stuff (8), voltage range (16), reserved (8) 554 // 0x00ff8000: voltage range 2.7 - 3.6V 555 556 const uint32_t Ocr_default_voltage_range = 0x00ff8000; 557 558 // SEND_IF_COND argument and default voltage range expected in R7: 559 // Argument MSB to LSB: stuff (20), voltage supplied (4), check (8) 560 // 0x000001aa: voltage range 2.7 - 3.6V, check = 0b10101010 561 562 const uint32_t If_cond_default_voltage_range = 0x000001aa; 563 564 565 566 // Utilities. 567 568 static enum Command_data_control_bits 569 encode_bus_width(uint8_t width) 570 { 571 switch (width) 572 { 573 case 4: return Cdc_bus_width_field_4bit; 574 case 1: return Cdc_bus_width_field_1bit; 575 default: return Cdc_bus_width_field_1bit; 576 } 577 } 578 579 580 581 // Channel abstraction. 582 583 Msc_channel::Msc_channel(l4_addr_t msc_start, l4_addr_t addr, l4_cap_idx_t irq) 584 : _msc_start(msc_start), _irq(irq) 585 { 586 _regs = new Hw::Mmio_register_block<32>(addr); 587 } 588 589 Msc_channel::~Msc_channel() 590 { 591 } 592 593 // Utility methods. 594 // NOTE: Also defined in the CPM abstraction, should be consolidated. 595 596 uint32_t 597 Msc_channel::get_field(uint32_t reg, uint32_t mask, uint8_t shift) 598 { 599 return (_regs[reg] & (mask << shift)) >> shift; 600 } 601 602 void 603 Msc_channel::set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value) 604 { 605 _regs[reg] = (_regs[reg] & (~(mask << shift))) | ((mask & value) << shift); 606 } 607 608 bool 609 Msc_channel::command_will_write(uint8_t index) 610 { 611 // NOTE: Probably incomplete coverage. 612 613 switch (index) 614 { 615 case Command_write_dat_until_stop: return true; 616 case Command_write_block: return true; 617 case Command_write_multiple_block: return true; 618 case Command_program_cid: return true; 619 case Command_program_csd: return true; 620 case Command_lock_unlock: return true; 621 default: return false; 622 } 623 } 624 625 bool 626 Msc_channel::app_command_will_write(uint8_t index) 627 { 628 // NOTE: Probably incomplete coverage. 629 630 (void) index; 631 632 return false; 633 } 634 635 bool 636 Msc_channel::command_with_data(uint8_t index) 637 { 638 // NOTE: Probably incomplete coverage. 639 640 switch (index) 641 { 642 case Command_read_dat_until_stop: return true; 643 case Command_read_single_block: return true; 644 case Command_read_multiple_block: return true; 645 case Command_write_dat_until_stop: return true; 646 case Command_write_block: return true; 647 case Command_write_multiple_block: return true; 648 case Command_program_cid: return true; 649 case Command_program_csd: return true; 650 case Command_lock_unlock: return true; 651 default: return false; 652 } 653 } 654 655 bool 656 Msc_channel::app_command_with_data(uint8_t index) 657 { 658 // NOTE: Probably incomplete coverage. 659 660 switch (index) 661 { 662 case App_command_sd_status: return true; 663 case App_command_send_num_wr_blocks: return true; 664 case App_command_send_scr: return true; 665 default: return false; 666 } 667 } 668 669 bool 670 Msc_channel::command_uses_busy(uint8_t index) 671 { 672 // NOTE: Probably incomplete coverage. 673 674 switch (index) 675 { 676 case Command_select_deselect_card: return true; 677 case Command_stop_transmission: return true; 678 default: return false; 679 } 680 } 681 682 bool 683 Msc_channel::app_command_uses_busy(uint8_t index) 684 { 685 // NOTE: Probably incomplete coverage. 686 687 (void) index; 688 689 return false; 690 } 691 692 uint8_t 693 Msc_channel::get_response_format(uint8_t index) 694 { 695 // NOTE: Probably incomplete coverage. 696 697 switch (index) 698 { 699 // Common commands without response. 700 701 case Command_go_idle_state: return 0; 702 case Command_set_dsr: return 0; 703 case Command_go_inactive_state: return 0; 704 705 // Common commands with response. 706 707 case Command_send_op_cond: return 3; 708 case Command_all_send_cid: return 2; 709 case Command_send_csd: return 2; 710 case Command_send_cid: return 2; 711 712 // SDIO only. 713 714 case Command_io_send_op_cond: return 4; 715 case Command_io_rw_direct: return 5; 716 717 // SDMEM only. 718 719 case Command_send_relative_addr: return 6; 720 case Command_send_if_cond: return 7; 721 722 // All other commands. 723 724 default: return 1; 725 } 726 } 727 728 uint8_t 729 Msc_channel::get_app_response_format(uint8_t index) 730 { 731 // NOTE: Probably incomplete coverage. 732 733 switch (index) 734 { 735 // SDMEM only. 736 737 case App_command_sd_send_op_cond: return 3; 738 739 // All other commands. 740 741 default: return 1; 742 } 743 } 744 745 // Read a response directly from the FIFO. 746 747 void 748 Msc_channel::read_response(uint16_t *buffer, uint8_t units) 749 { 750 uint8_t unit = units; 751 752 while (unit > 0) 753 { 754 uint32_t data = _regs[Msc_response_fifo]; 755 756 // Ignore the upper byte of the last unit in small transfers since it is the 757 // lower byte from the previous unit not shifted out of the register. 758 759 unit--; 760 761 if ((unit == 0) && (units == 3)) 762 buffer[unit] = (data & 0xff) << 8; 763 else 764 buffer[unit] = data; 765 } 766 } 767 768 void 769 Msc_channel::ack_irq(uint32_t flags) 770 { 771 // Clear the flags by setting them. 772 773 _regs[Msc_interrupt_flag] = _regs[Msc_interrupt_flag] | flags; 774 } 775 776 void 777 Msc_channel::unmask_irq(uint32_t flags) 778 { 779 ack_irq(flags); 780 781 if (_regs[Msc_interrupt_mask] & flags) 782 _regs[Msc_interrupt_mask] = _regs[Msc_interrupt_mask] & ~flags; 783 } 784 785 void 786 Msc_channel::reset() 787 { 788 _regs[Msc_control] = _regs[Msc_control] | Control_reset; 789 790 // NOTE: X1600 and other recent SoCs only. 791 792 _regs[Msc_control] = _regs[Msc_control] & ~Control_reset; 793 794 // Sufficient for other SoCs... 795 796 while (_regs[Msc_status] & Status_resetting); 797 } 798 799 void 800 Msc_channel::start_clock() 801 { 802 set_field(Msc_control, Control_clock_control_field_mask, 803 Control_clock_control_field_shift, Control_clock_control_start); 804 805 while (!(_regs[Msc_status] & Status_clock_enabled)); 806 } 807 808 void 809 Msc_channel::stop_clock() 810 { 811 set_field(Msc_control, Control_clock_control_field_mask, 812 Control_clock_control_field_shift, Control_clock_control_stop); 813 814 while (_regs[Msc_status] & Status_clock_enabled); 815 } 816 817 uint32_t 818 Msc_channel::get_status() 819 { 820 return _regs[Msc_status]; 821 } 822 823 // Send an application-specific command. 824 825 bool 826 Msc_channel::send_app_command(uint8_t index, uint32_t arg) 827 { 828 if (!send_command(Command_app_cmd, _current_rca << 16)) 829 return false; 830 831 return send_command(index, arg, get_app_response_format(index), 832 app_command_with_data(index), 833 app_command_will_write(index), 834 app_command_uses_busy(index)); 835 } 836 837 // Send a common MMC/SD command. 838 839 bool 840 Msc_channel::send_command(uint8_t index, uint32_t arg) 841 { 842 return send_command(index, arg, get_response_format(index), 843 command_with_data(index), 844 command_will_write(index), 845 command_uses_busy(index)); 846 } 847 848 // Initiate a command having the given index and using the given argument, 849 // employing the specified response format and involving a data transfer if 850 // indicated. 851 852 bool 853 Msc_channel::send_command(uint8_t index, uint32_t arg, uint8_t response_format, 854 bool data, bool write, bool busy) 855 { 856 stop_clock(); 857 858 // Enable DMA for data transfers. 859 // NOTE: Needed for JZ4780 and later SoCs. 860 861 _regs[Msc_dma_control] = (data ? Dma_select_common_dma | Dma_enable : Dma_disable); 862 863 // Set the command index and argument. 864 865 _regs[Msc_command_index] = index; 866 _regs[Msc_command_argument] = arg; 867 868 // Configure the response format and data bus width. 869 870 set_field(Msc_command_data_control, Cdc_response_format_field_mask, 871 Cdc_response_format_field_shift, response_format); 872 873 // NOTE: May need to set the SD bus width. 874 875 set_field(Msc_command_data_control, Cdc_bus_width_field_mask, 876 Cdc_bus_width_field_shift, encode_bus_width(_current_bus_width)); 877 878 set_field(Msc_command_data_control, Cdc_recv_fifo_level_field_mask, 879 Cdc_recv_fifo_level_field_shift, Cdc_fifo_level_16); 880 881 set_field(Msc_command_data_control, Cdc_trans_fifo_level_field_mask, 882 Cdc_trans_fifo_level_field_shift, Cdc_fifo_level_16); 883 884 // Set and clear control bits appropriate to the command. 885 // NOTE: Pre-JZ4780 SoCs enable DMA in this register. 886 887 _regs[Msc_command_data_control] = _regs[Msc_command_data_control] | 888 // (data ? Cdc_dma_enable : Cdc_dma_disable) | 889 (busy ? Cdc_expect_busy : Cdc_do_not_expect_busy) | 890 (data ? Cdc_data_with_command : Cdc_no_data_with_command) | 891 (write ? Cdc_write_operation : Cdc_read_operation); 892 893 _regs[Msc_command_data_control] = _regs[Msc_command_data_control] & 894 ~( 895 // (data ? Cdc_dma_disable : Cdc_dma_enable) | 896 (busy ? Cdc_do_not_expect_busy : Cdc_expect_busy) | 897 (data ? Cdc_no_data_with_command : Cdc_data_with_command) | 898 (write ? Cdc_read_operation : Cdc_write_operation) | 899 Cdc_stream_block | Cdc_init_sequence); 900 901 // Unmask interrupts, start the clock, then initiate the command. 902 903 uint32_t flags = Int_end_command_response | Int_response_timeout; 904 905 unmask_irq(flags); 906 start_clock(); 907 908 _regs[Msc_control] = _regs[Msc_control] | Control_start_operation; 909 910 // Wait for command completion. 911 912 if (!wait_for_irq(flags)) 913 return false; 914 915 // Determine whether a timeout occurred. 916 917 bool have_response = !((_regs[Msc_interrupt_flag] & Int_response_timeout) || 918 (_regs[Msc_status] & Status_response_crc_error) || 919 (_regs[Msc_status] & Status_timeout_response)); 920 921 // Acknowledge the response interrupts and return the status. 922 923 ack_irq(flags); 924 return have_response; 925 } 926 927 void 928 Msc_channel::enable() 929 { 930 // NOTE: X1600 and other recent SoCs only. 931 932 _regs[Msc_low_power_mode] = _regs[Msc_low_power_mode] & ~Low_power_mode_enable; 933 934 stop_clock(); 935 reset(); 936 937 // Slow the clock for initialisation. 938 // NOTE: Should use the CPM module to deduce the appropriate divider value. 939 940 set_field(Msc_clock_rate, Clock_rate_field_mask, Clock_rate_field_shift, 7); 941 942 send_command(Command_go_idle_state, 0); 943 944 if (check_sd()) 945 { 946 init_sdio(); 947 init_sdmem(); 948 } 949 950 init_mmc(); 951 identify_cards(); 952 query_cards(); 953 954 // Restore the clock. 955 // NOTE: Should use the CPM module to deduce the appropriate divider value. 956 957 set_field(Msc_clock_rate, Clock_rate_field_mask, Clock_rate_field_shift, 1); 958 959 _current_rca = 0; 960 } 961 962 // Check the voltage range of the SD card, potentially establishing that it is 963 // a high capacity card. Return false if the voltage range is incompatible. 964 965 bool 966 Msc_channel::check_sd() 967 { 968 uint16_t buffer[Response_size_R7]; 969 struct R7 *r = (struct R7 *) buffer; 970 971 // Send an interface condition command. 972 // A card may not respond to this command. 973 974 if (!send_command(Command_send_if_cond, If_cond_default_voltage_range)) 975 return true; 976 977 read_response(buffer, Response_size_R7); 978 979 // Reject any card not supporting the default voltage range. 980 981 if (r->check_voltage.raw != If_cond_default_voltage_range) 982 return false; 983 984 return true; 985 } 986 987 // Check the voltage range of the SDIO card, inactivating it if incompatible. 988 989 void 990 Msc_channel::init_sdio() 991 { 992 uint16_t buffer[Response_size_R4]; 993 struct R4 *r = (struct R4 *) buffer; 994 uint32_t ocr = 0; 995 996 // Reset any SDIO card or IO unit in a combined memory/IO card. 997 // A non-SDIO card may not respond to this command. 998 999 if (!send_command(Command_io_rw_direct, Io_rw_direct_reset)) 1000 return; 1001 1002 // Attempt to assert the operating conditions. 1003 1004 do 1005 { 1006 // Obtain OCR (operating conditions register) values for any IO card. 1007 // Without a response, the card may have inactivated itself due to voltage 1008 // range incompatibility reasons. 1009 1010 if (!send_command(Command_io_send_op_cond, ocr)) 1011 return; 1012 1013 read_response(buffer, Response_size_R4); 1014 1015 // Finish if no IO functions provided. 1016 // NOTE: Should only need to check this the first time. 1017 1018 if (r->number_io_functions == 0) 1019 return; 1020 1021 if (r->ocr != Ocr_default_voltage_range) 1022 { 1023 ocr = Ocr_default_voltage_range; 1024 continue; 1025 } 1026 } 1027 while (!r->ready); 1028 } 1029 1030 void 1031 Msc_channel::init_sdmem() 1032 { 1033 uint16_t buffer[Response_size_R3]; 1034 struct R3 *r = (struct R3 *) buffer; 1035 1036 // Incorporate the HCS bit into the OCR for SDMEM. 1037 1038 uint32_t ocr = Ocr_high_capacity_storage; 1039 1040 do 1041 { 1042 if (!send_app_command(App_command_sd_send_op_cond, ocr)) 1043 return; 1044 1045 read_response(buffer, Response_size_R3); 1046 1047 if (r->ocr != Ocr_default_voltage_range) 1048 { 1049 ocr = Ocr_default_voltage_range | Ocr_high_capacity_storage; 1050 continue; 1051 } 1052 } 1053 while (!(r->ocr & Ocr_card_powered_up)); 1054 } 1055 1056 void 1057 Msc_channel::init_mmc() 1058 { 1059 // Obtain OCR (operating conditions register) values for each card using 1060 // send_op_cond command variants without argument, or assert operating 1061 // conditions with argument to avoid handling card responses. Where responses 1062 // are solicited, the host must determine a suitable argument and reissue the 1063 // command. 1064 1065 uint16_t buffer[Response_size_R3]; 1066 struct R3 *r = (struct R3 *) buffer; 1067 uint32_t ocr = 0; 1068 1069 do 1070 { 1071 if (!send_command(Command_send_op_cond, ocr)) 1072 return; 1073 1074 read_response(buffer, Response_size_R3); 1075 1076 if (r->ocr != Ocr_default_voltage_range) 1077 { 1078 ocr = Ocr_default_voltage_range; 1079 continue; 1080 } 1081 } 1082 while (!(r->ocr & Ocr_card_powered_up)); 1083 } 1084 1085 void 1086 Msc_channel::identify_cards() 1087 { 1088 uint16_t buffer[Response_size_R2]; 1089 struct R2 *r = (struct R2 *) buffer; 1090 1091 _cards = 0; 1092 1093 while (send_command(Command_all_send_cid, 0)) 1094 { 1095 read_response(buffer, Response_size_R2); 1096 1097 memcpy(&_cid[_cards], r->payload.raw, sizeof(r->payload.raw)); 1098 1099 printf("card: %d\n", _cards); 1100 printf("date: %d %d\n", r->payload.cid.month, r->payload.cid.year); 1101 printf("serial: %d\n", r->payload.cid.serial); 1102 printf("revision: %d\n", r->payload.cid.revision); 1103 printf("name: %c%c%c%c%c\n", r->payload.cid.name[4], r->payload.cid.name[3], 1104 r->payload.cid.name[2], r->payload.cid.name[1], 1105 r->payload.cid.name[0]); 1106 printf("oem: %d\n", r->payload.cid.oem); 1107 printf("manufacturer: %d\n", r->payload.cid.manufacturer); 1108 1109 // Try and obtain a card-issued address. 1110 1111 if (send_command(Command_send_relative_addr, 0)) 1112 { 1113 uint16_t addr_buffer[Response_size_R6]; 1114 struct R6 *ar = (struct R6 *) addr_buffer; 1115 1116 read_response(addr_buffer, Response_size_R6); 1117 1118 memcpy(&_rca[_cards], &ar->rca, sizeof(ar->rca)); 1119 } 1120 1121 // Try and assign an address. 1122 // Employ 1-based relative addressing. 1123 1124 else if (send_command(Command_set_relative_addr, _cards + 1)) 1125 _rca[_cards] = _cards + 1; 1126 1127 // Otherwise, stop identification. 1128 1129 else 1130 return; 1131 1132 // Set the default bus width. 1133 1134 _bus_width[_cards] = 0; 1135 1136 _cards++; 1137 } 1138 } 1139 1140 void 1141 Msc_channel::query_cards() 1142 { 1143 uint16_t buffer[Response_size_R2]; 1144 struct R2 *r = (struct R2 *) buffer; 1145 uint8_t card; 1146 1147 for (card = 0; card < _cards; card++) 1148 { 1149 // Employ 1-based relative addressing. 1150 1151 if (!send_command(Command_send_csd, _rca[card] << 16)) 1152 return; 1153 1154 read_response(buffer, Response_size_R2); 1155 1156 memcpy(&_csd[card], r->payload.raw, sizeof(r->payload.raw)); 1157 1158 printf("card: %d\n", card); 1159 printf("csd: %d\n", r->payload.csd.csd); 1160 printf("copy: %s\n", r->payload.csd.copy ? "copied" : "original"); 1161 printf("card command classes: %03x\n", r->payload.csd.card_command_classes); 1162 printf("device (size multiplier): %d %d\n", r->payload.csd.device_size + 1, 1163 1 << (r->payload.csd.device_size_multiplier + 2)); 1164 printf("device size: %d\n", (1 << r->payload.csd.read_blocklen) * 1165 (r->payload.csd.device_size + 1) * 1166 (1 << (r->payload.csd.device_size_multiplier + 2))); 1167 printf("transfer speed: %d MHz\n", r->payload.csd.tran_speed == 0x32 ? 25 : 50); 1168 printf("format group: %d %d\n", r->payload.csd.format, r->payload.csd.format_group); 1169 printf("write time factor: %d\n", 1 << r->payload.csd.write_time_factor); 1170 printf("write protect (temp perm): %s %s\n", r->payload.csd.temp_write_prot ? "yes" : "no", 1171 r->payload.csd.perm_write_prot ? "yes" : "no"); 1172 printf("write protect group (enable size): %s %d\n", r->payload.csd.write_prot_group_enable ? "yes" : "no", 1173 r->payload.csd.write_prot_group_size + 1); 1174 printf("write block (partial length): %s %d\n", r->payload.csd.write_block_partial ? "yes" : "no", 1175 1 << r->payload.csd.write_blocklen); 1176 printf("read block (partial length): %s %d\n", r->payload.csd.read_block_partial ? "yes" : "no", 1177 1 << r->payload.csd.read_blocklen); 1178 printf("erase: sector single: %d %s\n", r->payload.csd.erase_sector_size + 1, 1179 r->payload.csd.erase_single_block_enable ? "yes" : "no"); 1180 printf("misalign: read write: %s %s\n", r->payload.csd.read_block_misalign ? "yes" : "no", 1181 r->payload.csd.write_block_misalign ? "yes" : "no"); 1182 printf("max read current (min max): %d %d\n", r->payload.csd.max_read_current_min, 1183 r->payload.csd.max_read_current_max); 1184 printf("max write current (min max): %d %d\n", r->payload.csd.max_write_current_min, 1185 r->payload.csd.max_write_current_max); 1186 printf("read access time (1 2): %d %d\n", r->payload.csd.data_read_access_time_1, 1187 r->payload.csd.data_read_access_time_2); 1188 printf("DSR: %s\n", r->payload.csd.dsr_implemented ? "yes" : "no"); 1189 } 1190 } 1191 1192 uint32_t 1193 Msc_channel::recv_data(l4re_dma_space_dma_addr_t paddr, uint32_t count) 1194 { 1195 uint32_t flags = Int_data_transfer_done; 1196 1197 unmask_irq(flags); 1198 1199 uint32_t to_transfer = transfer(_msc_start + Msc_recv_data_fifo, paddr, true, count); 1200 1201 wait_for_irq(flags); 1202 ack_irq(flags); 1203 1204 if (!to_transfer || 1205 (_regs[Msc_status] & Status_read_crc_error) || 1206 (_regs[Msc_status] & Status_timeout_read)) 1207 return 0; 1208 1209 return to_transfer; 1210 } 1211 1212 uint32_t 1213 Msc_channel::send_data(l4re_dma_space_dma_addr_t paddr, uint32_t count) 1214 { 1215 uint32_t flags = Int_data_transfer_done; 1216 1217 unmask_irq(flags); 1218 1219 uint32_t to_transfer = transfer(paddr, _msc_start + Msc_trans_data_fifo, false, count); 1220 1221 wait_for_irq(flags); 1222 ack_irq(flags); 1223 1224 if (!to_transfer || 1225 (_regs[Msc_status] & Status_write_crc_error_data) || 1226 (_regs[Msc_status] & Status_write_crc_error_no_status)) 1227 return 0; 1228 1229 return to_transfer; 1230 } 1231 1232 uint32_t 1233 Msc_channel::read_blocks(uint8_t card, l4re_dma_space_dma_addr_t paddr, 1234 uint32_t block_address, uint32_t block_count) 1235 { 1236 uint32_t block_size = 1 << _csd[card].read_blocklen; 1237 uint16_t buffer[Response_size_R1]; 1238 struct R1 *r = (struct R1 *) buffer; 1239 1240 // Select the requested card. 1241 1242 if (_current_rca != _rca[card]) 1243 { 1244 if (!send_command(Command_select_deselect_card, _rca[card] << 16)) 1245 return 0; 1246 1247 read_response(buffer, Response_size_R1); 1248 1249 if (r->status & R1_status_error_mask) 1250 return 0; 1251 1252 _current_rca = _rca[card]; 1253 } 1254 1255 // NOTE: SMEM cards should allow bus width setting with the SCR register 1256 // NOTE: describing the permitted values. 1257 // NOTE: SDIO cards have their bus width set in CCCR via CMD52. 1258 1259 if (!_bus_width[card]) 1260 { 1261 if (send_app_command(App_command_set_bus_width, Bus_width_4bit)) 1262 _bus_width[card] = 4; 1263 else 1264 _bus_width[card] = 1; 1265 } 1266 1267 _current_bus_width = _bus_width[card]; 1268 1269 if (!send_command(Command_set_blocklen, block_size)) 1270 return 0; 1271 1272 read_response(buffer, Response_size_R1); 1273 1274 if (r->status & R1_status_error_mask) 1275 return 0; 1276 1277 // NOTE: Consider issuing a predefined block count command to any multiple 1278 // NOTE: block operation, at least for cards that support it. 1279 1280 // Apply block count and size properties to the issued command. 1281 1282 _regs[Msc_block_count] = block_count; 1283 _regs[Msc_block_length] = block_size; 1284 1285 // NOTE: Where CCS = 0, byte addressing is used. Otherwise, block addressing is used. 1286 1287 if (!send_command(block_count == 1 ? Command_read_single_block 1288 : Command_read_multiple_block, block_address)) 1289 return 0; 1290 1291 read_response(buffer, Response_size_R1); 1292 1293 if (r->status & R1_status_error_mask) 1294 return 0; 1295 1296 // NOTE: Use Msc_block_success_count instead. 1297 1298 uint32_t transferred = recv_data(paddr, block_size * block_count); 1299 1300 if (block_count > 1) 1301 send_command(Command_stop_transmission, 0); 1302 1303 return transferred; 1304 } 1305 1306 // Wait indefinitely for an interrupt request, returning true if one was delivered. 1307 1308 bool 1309 Msc_channel::wait_for_irq(uint32_t flags) 1310 { 1311 return !l4_error(l4_irq_receive(_irq, L4_IPC_NEVER)) && 1312 (_regs[Msc_interrupt_flag] & flags); 1313 } 1314 1315 // Wait up to the given timeout (in microseconds) for an interrupt request, 1316 // returning true if one was delivered. 1317 1318 bool 1319 Msc_channel::wait_for_irq(uint32_t flags, unsigned int timeout) 1320 { 1321 return !l4_error(l4_irq_receive(_irq, l4_timeout(L4_IPC_TIMEOUT_NEVER, 1322 l4util_micros2l4to(timeout)))) && 1323 (_regs[Msc_interrupt_flag] & flags); 1324 } 1325 1326 1327 1328 // Peripheral abstraction. 1329 1330 Msc_chip::Msc_chip(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end) 1331 : _msc_start(msc_start), _start(start), _end(end) 1332 { 1333 }