Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/disk/disk_sdhci.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / disk / disk_sdhci.om` 926 lines ยท 19818 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/disk/disk_sdhci.om: Pure-Omega Freestanding Polled PIO SDHCI Driver + Software Controller Model. 3 note Converted from upstream aienux native/disk/disk_sdhci.{h,c} and native/disk/qemu/{payload.c,sdhci_payload.c,start.S}. 4 note Compose with: si/directives/om2.om aienux/disk/disk.om aienux/disk/disk_file.om aienux/disk/disk_sdhci.om 5 6 note SDHCI error codes matching disk_sdhci.h 7 const SDHCI_OK int 0 8 const SDHCI_EARG int -20 9 const SDHCI_EVER int -21 10 const SDHCI_ERESET int -22 11 const SDHCI_ENOCARD int -23 12 const SDHCI_EVOLT int -24 13 const SDHCI_ECLOCK int -25 14 const SDHCI_ECARD int -26 15 const SDHCI_ETIMEOUT int -27 16 const SDHCI_ECSD int -28 17 const SDHCI_ECMD int -29 18 const SDHCI_EDATA int -30 19 const SDHCI_ESTATUS int -31 20 const SDHCI_ESTATE int -32 21 22 note SDHCI register offsets 23 const SDHCI_R_BLOCK uint 0x04 24 const SDHCI_R_ARG uint 0x08 25 const SDHCI_R_CMD uint 0x0c 26 const SDHCI_R_RESP0 uint 0x10 27 const SDHCI_R_RESP1 uint 0x14 28 const SDHCI_R_RESP2 uint 0x18 29 const SDHCI_R_RESP3 uint 0x1c 30 const SDHCI_R_BUF uint 0x20 31 const SDHCI_R_PRESENT uint 0x24 32 const SDHCI_R_HOST uint 0x28 33 const SDHCI_R_CLOCK uint 0x2c 34 const SDHCI_R_INT uint 0x30 35 const SDHCI_R_INT_EN uint 0x34 36 const SDHCI_R_SIG_EN uint 0x38 37 const SDHCI_R_CAP uint 0x40 38 const SDHCI_R_VERSION uint 0xfc 39 40 note QEMU virt PCIe ECAM / MMIO constants (from qemu/sdhci_payload.c, payload.c) 41 const SDHCI_QEMU_UART ulong 0x09000000 42 const SDHCI_QEMU_ECAM ulong 0x3f000000 43 const SDHCI_QEMU_MMIO ulong 0x10000000 44 const SDHCI_PCI_CLASS_SDHCI uint 0x0805 45 const SDHCI_PCI_CLASS_NVME uint 0x010802 46 47 note PRESENT_STATE bits 48 const SD_PS_CMD_INHIBIT uint 0x00000001 49 const SD_PS_DATA_INHIBIT uint 0x00000002 50 const SD_PS_CARD_PRESENT uint 0x00010000 51 52 note INT_STATUS bits 53 const SD_INT_CMD_DONE uint 0x00000001 54 const SD_INT_XFER_DONE uint 0x00000002 55 const SD_INT_BUF_WR uint 0x00000010 56 const SD_INT_BUF_RD uint 0x00000020 57 const SD_INT_ERROR uint 0x00008000 58 const SD_INT_ERR_MASK uint 0xffff8000 59 60 note COMMAND / TRANSFER_MODE bits 61 const SD_CMD_RESP_NONE uint 0x00 62 const SD_CMD_RESP_LONG uint 0x01 63 const SD_CMD_RESP_SHORT uint 0x02 64 const SD_CMD_RESP_BUSY uint 0x03 65 const SD_CMD_CRC uint 0x08 66 const SD_CMD_INDEX uint 0x10 67 const SD_CMD_DATA uint 0x20 68 69 const SD_TM_BLK_CNT_EN uint 0x02 70 const SD_TM_AUTO_CMD12 uint 0x04 71 const SD_TM_READ uint 0x10 72 const SD_TM_MULTI uint 0x20 73 74 note CLOCK / HOST bits 75 const SD_CLK_INT_EN uint 0x0001 76 const SD_CLK_INT_STABLE uint 0x0002 77 const SD_CLK_CARD_EN uint 0x0004 78 const SD_RST_ALL uint 0x01000000 79 const SD_RST_CMD uint 0x02000000 80 const SD_RST_DATA uint 0x04000000 81 const SD_HOST_4BIT uint 0x02 82 const SD_PWR_330 uint 0x0e00 83 const SD_PWR_ON uint 0x0100 84 85 note Response kinds 86 const SD_R_NONE uint 0 87 const SD_R1 uint 1 88 const SD_R1B uint 2 89 const SD_R2 uint 3 90 const SD_R3 uint 4 91 const SD_R6 uint 5 92 const SD_R7 uint 6 93 94 const SD_R1_ERRORS uint 0xfdf98008 95 const SD_R1_READY_FOR_DATA uint 0x00000100 96 const SD_STATE_TRAN uint 4 97 98 note Driver state globals 99 note sd_u32: 0=ready, 1=spec, 2=base_hz, 3=card_hz, 4=rca, 5=high_cap, 6=max_blocks, 7=timeout_ms, 8=bus_4bit, 9=last_int 100 global sd_u32 uint 10 101 note sd_i32: 0=last_cmd, 1=last_err 102 global sd_i32 int 2 103 global sd_blocks ulong 1 104 global sd_cid uint 4 105 global sd_csd uint 4 106 global sd_resp uint 4 107 108 note Software SDHCI controller + 64-block (32 KiB) SDHC card model for host behavioral verification 109 const SD_SIM_BLOCKS ulong 64 110 global ssim_regs uint 64 111 global ssim_mem uchar 32768 112 note ssim_st: 0=active, 1=cmd55_next, 2=acmd41_polls, 3=card_present, 4=xfer_dir (1=rd,2=wr), 5=xfer_lba, 6=xfer_rem_words, 7=xfer_word_off, 8=inject_err, 9=inject_r1_err, 10=bad_cmd8, 11=spec_ver, 12=has_3v3, 13=base_mhz, 14=csd_csize 113 global ssim_st uint 15 114 115 fn sdhci_sim_init num_blocks ulong to void 116 for i ulong 64 117 put ssim_regs i 0 118 end 119 for i ulong 32768 120 put ssim_mem i 0 121 end 122 for i ulong 15 123 put ssim_st i 0 124 end 125 put ssim_st 0 1 126 put ssim_st 3 1 127 put ssim_st 11 2 128 put ssim_st 12 1 129 put ssim_st 13 50 130 csize = uint 0 131 if gt num_blocks 1024 132 csize = uint sub div num_blocks 1024 1 133 end 134 put ssim_st 14 csize 135 note Version register at 0xfc: spec=2 (3.00) in bits 23:16 136 put ssim_regs 63 shl uint 2 16 137 note Capabilities at 0x40: bit 24 = 3.3V, bits 15:8 = base_mhz (50) 138 put ssim_regs 16 or shl uint 1 24 shl uint 50 8 139 note Present state at 0x24: card present 140 put ssim_regs 9 SD_PS_CARD_PRESENT 141 end 142 143 fn sdhci_sim_set_spec spec_ver uint to void 144 put ssim_st 11 spec_ver 145 put ssim_regs 63 shl spec_ver 16 146 end 147 148 fn sdhci_sim_set_volt has_3v3 uint to void 149 put ssim_st 12 has_3v3 150 cap = shl get ssim_st 13 8 151 if ne has_3v3 0 152 cap = or cap shl uint 1 24 153 end 154 put ssim_regs 16 cap 155 end 156 157 fn sdhci_sim_set_card_present pres uint to void 158 put ssim_st 3 pres 159 if ne pres 0 160 put ssim_regs 9 SD_PS_CARD_PRESENT 161 else 162 put ssim_regs 9 0 163 end 164 end 165 166 fn sdhci_sim_inject_err err_bits uint to void 167 put ssim_st 8 err_bits 168 end 169 170 fn sdhci_sim_inject_r1 r1_bits uint to void 171 put ssim_st 9 r1_bits 172 end 173 174 fn sdhci_sim_set_bad_cmd8 v uint to void 175 put ssim_st 10 v 176 end 177 178 fn ssim_exec_cmd cmd_word uint to void 179 idx = and shr cmd_word 24 0x3f 180 arg = get ssim_regs 2 181 is_app = get ssim_st 1 182 put ssim_st 1 0 183 inj = get ssim_st 8 184 if ne inj 0 185 put ssim_st 8 0 186 put ssim_regs 12 or SD_INT_ERROR inj 187 ret 188 end 189 r1_base = or shl SD_STATE_TRAN 9 SD_R1_READY_FOR_DATA 190 r1_err = get ssim_st 9 191 if ne r1_err 0 192 put ssim_st 9 0 193 r1_base = or r1_base r1_err 194 end 195 put ssim_regs 4 0 196 put ssim_regs 5 0 197 put ssim_regs 6 0 198 put ssim_regs 7 0 199 if eq idx 0 200 put ssim_regs 12 SD_INT_CMD_DONE 201 ret 202 end 203 if eq idx 8 204 if ne get ssim_st 10 0 205 put ssim_regs 4 0x000 206 else 207 put ssim_regs 4 arg 208 end 209 put ssim_regs 12 SD_INT_CMD_DONE 210 ret 211 end 212 if eq idx 55 213 put ssim_st 1 1 214 put ssim_regs 4 r1_base 215 put ssim_regs 12 SD_INT_CMD_DONE 216 ret 217 end 218 if and ne is_app 0 eq idx 41 219 polls = add get ssim_st 2 1 220 put ssim_st 2 polls 221 if ge polls 2 222 note Ready + CCS (SDHC) 223 put ssim_regs 4 0xc0ff8000 224 else 225 put ssim_regs 4 0x00ff8000 226 end 227 put ssim_regs 12 SD_INT_CMD_DONE 228 ret 229 end 230 if eq idx 2 231 put ssim_regs 4 0x12345678 232 put ssim_regs 5 0x9abcdef0 233 put ssim_regs 6 0x11223344 234 put ssim_regs 7 0x55667788 235 put ssim_regs 12 SD_INT_CMD_DONE 236 ret 237 end 238 if eq idx 3 239 note RCA = 0x1234 240 put ssim_regs 4 shl uint 0x1234 16 241 put ssim_regs 12 SD_INT_CMD_DONE 242 ret 243 end 244 if eq idx 9 245 note CSD v2.0 (structure=1 at CSD bits 127:126 -> reg bits 119:118 -> resp[3] bits 23:22) 246 note C_SIZE at CSD bits 69:48 -> reg bits 61:40 -> resp[1] bits 29:8 247 put ssim_regs 7 shl uint 1 22 248 put ssim_regs 5 shl and get ssim_st 14 0x3fffff 8 249 put ssim_regs 12 SD_INT_CMD_DONE 250 ret 251 end 252 if eq idx 7 253 put ssim_regs 4 r1_base 254 put ssim_regs 12 or SD_INT_CMD_DONE SD_INT_XFER_DONE 255 ret 256 end 257 if and ne is_app 0 eq idx 6 258 put ssim_regs 4 r1_base 259 put ssim_regs 12 SD_INT_CMD_DONE 260 ret 261 end 262 if or eq idx 13 eq idx 16 263 put ssim_regs 4 r1_base 264 put ssim_regs 12 SD_INT_CMD_DONE 265 ret 266 end 267 if or eq idx 17 eq idx 18 268 nblks = shr get ssim_regs 1 16 269 if eq nblks 0 270 nblks = uint 1 271 end 272 put ssim_st 4 1 273 put ssim_st 5 arg 274 put ssim_st 6 mul nblks 128 275 put ssim_st 7 0 276 put ssim_regs 4 r1_base 277 put ssim_regs 12 or SD_INT_CMD_DONE SD_INT_BUF_RD 278 ret 279 end 280 if or eq idx 24 eq idx 25 281 nblks = shr get ssim_regs 1 16 282 if eq nblks 0 283 nblks = uint 1 284 end 285 put ssim_st 4 2 286 put ssim_st 5 arg 287 put ssim_st 6 mul nblks 128 288 put ssim_st 7 0 289 put ssim_regs 4 r1_base 290 put ssim_regs 12 or SD_INT_CMD_DONE SD_INT_BUF_WR 291 ret 292 end 293 put ssim_regs 4 r1_base 294 put ssim_regs 12 SD_INT_CMD_DONE 295 end 296 297 fn sdhci_r32 off uint to uint 298 if eq off SDHCI_R_BUF 299 if eq get ssim_st 4 1 300 lba = ulong get ssim_st 5 301 woff = ulong get ssim_st 7 302 boff = add mul rem lba SD_SIM_BLOCKS 512 mul woff 4 303 v = uint 0 304 if lt add boff 3 32768 305 b0 = uint get ssim_mem boff 306 b1 = shl uint get ssim_mem add boff 1 8 307 b2 = shl uint get ssim_mem add boff 2 16 308 b3 = shl uint get ssim_mem add boff 3 24 309 v = or or b0 b1 or b2 b3 310 end 311 woff = add woff 1 312 put ssim_st 7 uint woff 313 remw = get ssim_st 6 314 if gt remw 0 315 remw = sub remw 1 316 put ssim_st 6 remw 317 end 318 if eq remw 0 319 put ssim_st 4 0 320 put ssim_regs 12 or get ssim_regs 12 SD_INT_XFER_DONE 321 else 322 if eq rem woff 128 0 323 put ssim_regs 12 or get ssim_regs 12 SD_INT_BUF_RD 324 end 325 end 326 ret v 327 end 328 ret 0 329 end 330 ridx = ulong shr off 2 331 if lt ridx 64 332 ret get ssim_regs ridx 333 end 334 ret 0 335 end 336 337 fn sdhci_w32 off uint v uint to void 338 if eq off SDHCI_R_BUF 339 if eq get ssim_st 4 2 340 lba = ulong get ssim_st 5 341 woff = ulong get ssim_st 7 342 boff = add mul rem lba SD_SIM_BLOCKS 512 mul woff 4 343 if lt add boff 3 32768 344 put ssim_mem boff uchar and v 0xff 345 put ssim_mem add boff 1 uchar and shr v 8 0xff 346 put ssim_mem add boff 2 uchar and shr v 16 0xff 347 put ssim_mem add boff 3 uchar and shr v 24 0xff 348 end 349 woff = add woff 1 350 put ssim_st 7 uint woff 351 remw = get ssim_st 6 352 if gt remw 0 353 remw = sub remw 1 354 put ssim_st 6 remw 355 end 356 if eq remw 0 357 put ssim_st 4 0 358 put ssim_regs 12 or get ssim_regs 12 SD_INT_XFER_DONE 359 else 360 if eq rem woff 128 0 361 put ssim_regs 12 or get ssim_regs 12 SD_INT_BUF_WR 362 end 363 end 364 end 365 ret 366 end 367 if eq off SDHCI_R_INT 368 cur = get ssim_regs 12 369 put ssim_regs 12 and cur xor v 0xffffffff 370 ret 371 end 372 if eq off SDHCI_R_CLOCK 373 nv = and v xor or SD_RST_ALL or SD_RST_CMD SD_RST_DATA 0xffffffff 374 if ne and nv SD_CLK_INT_EN 0 375 nv = or nv SD_CLK_INT_STABLE 376 end 377 put ssim_regs 11 nv 378 ret 379 end 380 if eq off SDHCI_R_CMD 381 put ssim_regs 3 v 382 ssim_exec_cmd v 383 ret 384 end 385 ridx = ulong shr off 2 386 if lt ridx 64 387 put ssim_regs ridx v 388 end 389 end 390 391 note ---- SDHCI Core Driver Logic (disk_sdhci.c) ---- 392 393 fn sd_fail cmd int err int to int 394 put sd_i32 0 cmd 395 put sd_i32 1 err 396 ret err 397 end 398 399 fn sd_wait_reg off uint mask uint want uint ms uint to int 400 polls = add mul ms 100 1 401 i = uint 0 402 while lt i polls bound 200000 403 if eq and sdhci_r32 off mask want 404 ret 0 405 end 406 i = add i 1 407 end 408 ret -1 409 end 410 411 fn sd_line_reset bits uint to void 412 v = sdhci_r32 SDHCI_R_CLOCK 413 all_rst = or SD_RST_ALL or SD_RST_CMD SD_RST_DATA 414 if ne and v all_rst 0 415 w1 = sd_wait_reg SDHCI_R_CLOCK all_rst 0 100 416 end 417 v = and sdhci_r32 SDHCI_R_CLOCK 0x00ffffff 418 sdhci_w32 SDHCI_R_CLOCK or v bits 419 w2 = sd_wait_reg SDHCI_R_CLOCK bits 0 100 420 end 421 422 fn sd_wait_int mask uint ms uint to int 423 polls = add mul ms 100 1 424 i = uint 0 425 while lt i polls bound 200000 426 s = sdhci_r32 SDHCI_R_INT 427 put sd_u32 9 s 428 if ne and s or SD_INT_ERROR SD_INT_ERR_MASK 0 429 ret -1 430 end 431 if ne and s mask 0 432 sdhci_w32 SDHCI_R_INT and s mask 433 ret 0 434 end 435 i = add i 1 436 end 437 put sd_u32 9 sdhci_r32 SDHCI_R_INT 438 ret -2 439 end 440 441 fn sd_cmd_flags kind uint to uint 442 if or eq kind SD_R1 or eq kind SD_R6 eq kind SD_R7 443 ret or SD_CMD_RESP_SHORT or SD_CMD_CRC SD_CMD_INDEX 444 end 445 if eq kind SD_R1B 446 ret or SD_CMD_RESP_BUSY or SD_CMD_CRC SD_CMD_INDEX 447 end 448 if eq kind SD_R2 449 ret or SD_CMD_RESP_LONG SD_CMD_CRC 450 end 451 if eq kind SD_R3 452 ret SD_CMD_RESP_SHORT 453 end 454 ret SD_CMD_RESP_NONE 455 end 456 457 fn sd_command idx uint arg uint kind uint tmode uint nblocks uint to int 458 ms = get sd_u32 7 459 inhibit = SD_PS_CMD_INHIBIT 460 if or ne nblocks 0 eq kind SD_R1B 461 inhibit = or inhibit SD_PS_DATA_INHIBIT 462 end 463 if ne sd_wait_reg SDHCI_R_PRESENT inhibit 0 ms 0 464 ret sd_fail int idx SDHCI_ETIMEOUT 465 end 466 sdhci_w32 SDHCI_R_INT 0xffffffff 467 if ne nblocks 0 468 sdhci_w32 SDHCI_R_BLOCK or 512 shl nblocks 16 469 end 470 sdhci_w32 SDHCI_R_ARG arg 471 f = sd_cmd_flags kind 472 if ne nblocks 0 473 f = or f SD_CMD_DATA 474 end 475 sdhci_w32 SDHCI_R_CMD or shl or shl idx 8 f 16 tmode 476 w = sd_wait_int SD_INT_CMD_DONE ms 477 if ne w 0 478 sd_line_reset or SD_RST_CMD SD_RST_DATA 479 if eq w -2 480 ret sd_fail int idx SDHCI_ETIMEOUT 481 end 482 ret sd_fail int idx SDHCI_ECMD 483 end 484 put sd_resp 0 sdhci_r32 SDHCI_R_RESP0 485 if eq kind SD_R2 486 put sd_resp 1 sdhci_r32 SDHCI_R_RESP1 487 put sd_resp 2 sdhci_r32 SDHCI_R_RESP2 488 put sd_resp 3 sdhci_r32 SDHCI_R_RESP3 489 end 490 if eq kind SD_R1B 491 if ne sd_wait_int SD_INT_XFER_DONE ms 0 492 sd_line_reset or SD_RST_CMD SD_RST_DATA 493 ret sd_fail int idx SDHCI_ETIMEOUT 494 end 495 end 496 if or eq kind SD_R1 eq kind SD_R1B 497 if ne and get sd_resp 0 SD_R1_ERRORS 0 498 ret sd_fail int idx SDHCI_ESTATUS 499 end 500 end 501 ret SDHCI_OK 502 end 503 504 fn sd_app_cmd idx uint arg uint kind uint to int 505 rc = sd_command 55 shl get sd_u32 4 16 SD_R1 0 0 506 if ne rc SDHCI_OK 507 ret rc 508 end 509 ret sd_command idx arg kind 0 0 510 end 511 512 fn sd_set_clock hz uint to int 513 base = get sd_u32 2 514 spec = get sd_u32 1 515 divf = uint 0 516 actual = uint 0 517 if ge spec 2 518 n = uint 0 519 if gt base hz 520 denom = mul 2 hz 521 n = div sub add base denom 1 denom 522 end 523 if gt n 0x3ff 524 ret SDHCI_ECLOCK 525 end 526 if ne n 0 527 actual = div base mul 2 n 528 else 529 actual = base 530 end 531 divf = or shl and n 0xff 8 shl and shr n 8 0x3 6 532 else 533 dv = uint 1 534 while and lt dv 256 gt div base dv hz bound 16 535 dv = mul dv 2 536 end 537 if gt div base dv hz 538 ret SDHCI_ECLOCK 539 end 540 actual = div base dv 541 divf = shl div dv 2 8 542 end 543 v = and sdhci_r32 SDHCI_R_CLOCK 0xffff0000 544 sdhci_w32 SDHCI_R_CLOCK v 545 sdhci_w32 SDHCI_R_CLOCK or or v divf SD_CLK_INT_EN 546 if ne sd_wait_reg SDHCI_R_CLOCK SD_CLK_INT_STABLE SD_CLK_INT_STABLE 100 0 547 ret SDHCI_ECLOCK 548 end 549 sdhci_w32 SDHCI_R_CLOCK or or or v divf SD_CLK_INT_EN SD_CLK_CARD_EN 550 put sd_u32 3 actual 551 ret SDHCI_OK 552 end 553 554 fn sd_csd_bits hi uint lo uint to uint 555 rhi = sub hi 8 556 rlo = sub lo 8 557 v = uint 0 558 b = add rhi 1 559 while gt b rlo bound 128 560 b = sub b 1 561 word = get sd_csd ulong div b 32 562 bit = and shr word rem b 32 1 563 v = or shl v 1 bit 564 end 565 ret v 566 end 567 568 fn sd_card_status to int 569 rc = sd_command 13 shl get sd_u32 4 16 SD_R1 0 0 570 ret rc 571 end 572 573 fn sd_wait_tran to int 574 ms = get sd_u32 7 575 i = uint 0 576 while le i ms bound 10000 577 rc = sd_card_status 578 if ne rc SDHCI_OK 579 ret rc 580 end 581 st = get sd_resp 0 582 if and ne and st SD_R1_READY_FOR_DATA 0 eq and shr st 9 0xf SD_STATE_TRAN 583 ret SDHCI_OK 584 end 585 i = add i 1 586 end 587 ret sd_fail 13 SDHCI_ETIMEOUT 588 end 589 590 fn sdhci_init_card dev edit DiskDev base_hz uint timeout_ms uint bus_4bit uint max_blocks uint to int 591 for i ulong 10 592 put sd_u32 i 0 593 end 594 put sd_i32 0 -1 595 put sd_i32 1 0 596 put sd_blocks 0 0 597 tms = timeout_ms 598 if eq tms 0 599 tms = uint 1000 600 end 601 mb = max_blocks 602 if eq mb 0 603 mb = uint 128 604 end 605 if gt mb 65535 606 ret sd_fail -1 SDHCI_EARG 607 end 608 put sd_u32 7 tms 609 put sd_u32 6 mb 610 put sd_u32 8 bus_4bit 611 spec = and shr sdhci_r32 SDHCI_R_VERSION 16 0xff 612 put sd_u32 1 spec 613 if lt spec 1 614 ret sd_fail -1 SDHCI_EVER 615 end 616 sdhci_w32 SDHCI_R_CLOCK or and sdhci_r32 SDHCI_R_CLOCK 0x00ffffff SD_RST_ALL 617 if ne sd_wait_reg SDHCI_R_CLOCK SD_RST_ALL 0 100 0 618 ret sd_fail -1 SDHCI_ERESET 619 end 620 cap = sdhci_r32 SDHCI_R_CAP 621 if eq and cap shl uint 1 24 0 622 ret sd_fail -1 SDHCI_EVOLT 623 end 624 bhz = base_hz 625 if eq bhz 0 626 cmask = uint 0x3f 627 if ge spec 2 628 cmask = uint 0xff 629 end 630 bhz = mul and shr cap 8 cmask `1000000` 631 end 632 put sd_u32 2 bhz 633 if eq bhz 0 634 ret sd_fail -1 SDHCI_ECLOCK 635 end 636 ret sdhci_init_post dev 637 end 638 639 fn sdhci_setup_power_clock to int 640 host = and sdhci_r32 SDHCI_R_HOST 0xffff0000 641 sdhci_w32 SDHCI_R_HOST or host SD_PWR_330 642 sdhci_w32 SDHCI_R_HOST or or host SD_PWR_330 SD_PWR_ON 643 if ne sd_wait_reg SDHCI_R_PRESENT SD_PS_CARD_PRESENT SD_PS_CARD_PRESENT 100 0 644 ret sd_fail -1 SDHCI_ENOCARD 645 end 646 ien = or SD_INT_ERR_MASK SD_INT_BUF_RD 647 ien = or ien SD_INT_BUF_WR 648 ien = or ien or SD_INT_XFER_DONE SD_INT_CMD_DONE 649 sdhci_w32 SDHCI_R_INT_EN ien 650 sdhci_w32 SDHCI_R_SIG_EN 0 651 clk = and sdhci_r32 SDHCI_R_CLOCK 0x0000ffff 652 sdhci_w32 SDHCI_R_CLOCK or clk shl uint 0x0e 16 653 if ne sd_set_clock 400000 SDHCI_OK 654 ret sd_fail -1 SDHCI_ECLOCK 655 end 656 ret SDHCI_OK 657 end 658 659 fn sdhci_init_post dev edit DiskDev to int 660 rc = sdhci_setup_power_clock 661 if ne rc SDHCI_OK 662 ret rc 663 end 664 rc = sd_command 0 0 SD_R_NONE 0 0 665 if ne rc SDHCI_OK 666 ret rc 667 end 668 rc = sd_command 8 0x1aa SD_R7 0 0 669 if or ne rc SDHCI_OK ne and get sd_resp 0 0xfff 0x1aa 670 ret sd_fail 8 SDHCI_ECARD 671 end 672 ret sdhci_init_identify dev 673 end 674 675 fn sdhci_init_identify dev edit DiskDev to int 676 ready_ocr = false 677 i = uint 0 678 while le i 100 bound 128 679 rc = sd_app_cmd 41 0x40ff8000 SD_R3 680 if ne rc SDHCI_OK 681 ret rc 682 end 683 if ne and get sd_resp 0 0x80000000 0 684 ready_ocr = true 685 break 686 end 687 i = add i 1 688 end 689 if not ready_ocr 690 ret sd_fail 41 SDHCI_ETIMEOUT 691 end 692 put sd_u32 5 and shr get sd_resp 0 30 1 693 rc = sd_command 2 0 SD_R2 0 0 694 if ne rc SDHCI_OK 695 ret rc 696 end 697 for k ulong 4 698 put sd_cid k get sd_resp k 699 end 700 rc = sd_command 3 0 SD_R6 0 0 701 if ne rc SDHCI_OK 702 ret rc 703 end 704 put sd_u32 4 shr get sd_resp 0 16 705 rc = sd_command 9 shl get sd_u32 4 16 SD_R2 0 0 706 if ne rc SDHCI_OK 707 ret rc 708 end 709 for k ulong 4 710 put sd_csd k get sd_resp k 711 end 712 struct_v = sd_csd_bits 127 126 713 blks = ulong 0 714 if eq struct_v 1 715 csize = ulong sd_csd_bits 69 48 716 blks = mul add csize 1 1024 717 else 718 if and eq struct_v 0 eq get sd_u32 5 0 719 csize0 = ulong sd_csd_bits 73 62 720 cmult = sd_csd_bits 49 47 721 rblen = sd_csd_bits 83 80 722 nbytes = shl shl add csize0 1 add cmult 2 rblen 723 blks = div nbytes 512 724 else 725 ret sd_fail 9 SDHCI_ECSD 726 end 727 end 728 if eq blks 0 729 ret sd_fail 9 SDHCI_ECSD 730 end 731 put sd_blocks 0 blks 732 if ne sd_set_clock `25000000` SDHCI_OK 733 ret sd_fail -1 SDHCI_ECLOCK 734 end 735 rc = sd_command 7 shl get sd_u32 4 16 SD_R1B 0 0 736 if ne rc SDHCI_OK 737 ret rc 738 end 739 if ne get sd_u32 8 0 740 rc = sd_app_cmd 6 2 SD_R1 741 if ne rc SDHCI_OK 742 ret rc 743 end 744 sdhci_w32 SDHCI_R_HOST or sdhci_r32 SDHCI_R_HOST SD_HOST_4BIT 745 end 746 if eq get sd_u32 5 0 747 rc = sd_command 16 512 SD_R1 0 0 748 if ne rc SDHCI_OK 749 ret rc 750 end 751 end 752 put dev backend DISK_BACKEND_SDHCI 753 put dev block_size 512 754 put dev block_count blks 755 put dev max_blocks get sd_u32 6 756 put sd_u32 0 1 757 ret SDHCI_OK 758 end 759 760 fn sd_read_block buf edit uchar * base ulong to void 761 i = ulong 0 762 while lt i 512 bound 128 763 v = sdhci_r32 SDHCI_R_BUF 764 p = add base i 765 put buf p uchar and v 0xff 766 put buf add p 1 uchar and shr v 8 0xff 767 put buf add p 2 uchar and shr v 16 0xff 768 put buf add p 3 uchar and shr v 24 0xff 769 i = add i 4 770 end 771 end 772 773 fn sd_write_block buf view uchar * base ulong to void 774 i = ulong 0 775 while lt i 512 bound 128 776 p = add base i 777 b0 = uint get buf p 778 b1 = shl uint get buf add p 1 8 779 b2 = shl uint get buf add p 2 16 780 b3 = shl uint get buf add p 3 24 781 sdhci_w32 SDHCI_R_BUF or or b0 b1 or b2 b3 782 i = add i 4 783 end 784 end 785 786 fn sdhci_read lba ulong cnt uint buf edit uchar * off ulong to int 787 if or eq cnt 0 gt cnt get sd_u32 6 788 ret DISK_EARG 789 end 790 if eq get sd_u32 0 0 791 ret DISK_ESTATE 792 end 793 blks = get sd_blocks 0 794 if or ge lba blks gt ulong cnt sub blks lba 795 ret DISK_ERANGE 796 end 797 addr = lba 798 if eq get sd_u32 5 0 799 addr = mul lba 512 800 end 801 if gt addr 0xffffffff 802 ret DISK_ERANGE 803 end 804 idx = uint 17 805 tm = SD_TM_READ 806 if gt cnt 1 807 idx = uint 18 808 tm = or SD_TM_READ or SD_TM_BLK_CNT_EN or SD_TM_MULTI SD_TM_AUTO_CMD12 809 end 810 rc = sd_command idx uint addr SD_R1 tm cnt 811 b = uint 0 812 while and eq rc SDHCI_OK lt b cnt bound 65536 813 w = sd_wait_int SD_INT_BUF_RD get sd_u32 7 814 if ne w 0 815 if eq w -2 816 rc = sd_fail int idx SDHCI_ETIMEOUT 817 else 818 rc = sd_fail int idx SDHCI_EDATA 819 end 820 break 821 end 822 sd_read_block buf add off mul ulong b 512 823 b = add b 1 824 end 825 if eq rc SDHCI_OK 826 w = sd_wait_int SD_INT_XFER_DONE get sd_u32 7 827 if ne w 0 828 if eq w -2 829 rc = sd_fail int idx SDHCI_ETIMEOUT 830 else 831 rc = sd_fail int idx SDHCI_EDATA 832 end 833 end 834 end 835 if ne rc SDHCI_OK 836 sd_line_reset or SD_RST_CMD SD_RST_DATA 837 put sd_u32 0 0 838 ret DISK_EIO 839 end 840 ret DISK_OK 841 end 842 843 fn sdhci_write lba ulong cnt uint buf view uchar * off ulong to int 844 if or eq cnt 0 gt cnt get sd_u32 6 845 ret DISK_EARG 846 end 847 if eq get sd_u32 0 0 848 ret DISK_ESTATE 849 end 850 blks = get sd_blocks 0 851 if or ge lba blks gt ulong cnt sub blks lba 852 ret DISK_ERANGE 853 end 854 addr = lba 855 if eq get sd_u32 5 0 856 addr = mul lba 512 857 end 858 if gt addr 0xffffffff 859 ret DISK_ERANGE 860 end 861 idx = uint 24 862 tm = uint 0 863 if gt cnt 1 864 idx = uint 25 865 tm = or SD_TM_BLK_CNT_EN or SD_TM_MULTI SD_TM_AUTO_CMD12 866 end 867 rc = sd_command idx uint addr SD_R1 tm cnt 868 b = uint 0 869 while and eq rc SDHCI_OK lt b cnt bound 65536 870 w = sd_wait_int SD_INT_BUF_WR get sd_u32 7 871 if ne w 0 872 if eq w -2 873 rc = sd_fail int idx SDHCI_ETIMEOUT 874 else 875 rc = sd_fail int idx SDHCI_EDATA 876 end 877 break 878 end 879 sd_write_block buf add off mul ulong b 512 880 b = add b 1 881 end 882 if eq rc SDHCI_OK 883 w = sd_wait_int SD_INT_XFER_DONE get sd_u32 7 884 if ne w 0 885 if eq w -2 886 rc = sd_fail int idx SDHCI_ETIMEOUT 887 else 888 rc = sd_fail int idx SDHCI_EDATA 889 end 890 end 891 end 892 if eq rc SDHCI_OK 893 rc = sd_wait_tran 894 end 895 if ne rc SDHCI_OK 896 sd_line_reset or SD_RST_CMD SD_RST_DATA 897 put sd_u32 0 0 898 ret DISK_EIO 899 end 900 ret DISK_OK 901 end 902 903 fn sdhci_flush to int 904 if eq get sd_u32 0 0 905 ret DISK_ESTATE 906 end 907 rc = sd_card_status 908 st = get sd_resp 0 909 if or ne rc SDHCI_OK or eq and st SD_R1_READY_FOR_DATA 0 ne and shr st 9 0xf SD_STATE_TRAN 910 put sd_u32 0 0 911 ret DISK_EIO 912 end 913 ret DISK_OK 914 end 915 916 fn sdhci_is_ready to bool 917 ret ne get sd_u32 0 0 918 end 919 920 fn sdhci_last_err to int 921 ret get sd_i32 1 922 end 923 924 fn sdhci_card_hz to uint 925 ret get sd_u32 3 926 end