aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / disk / tests / sdhci_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* sdhci_test.c -- disk_sdhci.c against a software SDHCI + SD card model. |
| 18 | * Hosted test; not QEMU and not hardware. The model offers only aligned |
| 19 | * 32-bit access, flags every protocol violation it can see (command issued |
| 20 | * while inhibited, data command while the card is programming, wrong |
| 21 | * response type, wrong address mode, block size not 512, unaligned or |
| 22 | * out-of-window access), and injects faults on request. */ |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include "../disk_sdhci.h" |
| 28 | |
| 29 | static int failures; |
| 30 | #define CHECK(x) \ |
| 31 | do { \ |
| 32 | if (!(x)) { \ |
| 33 | printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); \ |
| 34 | failures++; \ |
| 35 | } \ |
| 36 | } while (0) |
| 37 | |
| 38 | enum { ST_IDLE = 0, ST_READY = 1, ST_IDENT = 2, ST_STBY = 3, ST_TRAN = 4, ST_DATA = 5, ST_RCV = 6, ST_PRG = 7 }; |
| 39 | |
| 40 | typedef struct { |
| 41 | uint32_t reg[64]; |
| 42 | /* card */ |
| 43 | uint8_t *mem; |
| 44 | uint64_t blocks; |
| 45 | int sdsc, v1_card, present, state, app, acmd41_polls, acmd41_needed, bus4; |
| 46 | uint16_t rca; |
| 47 | uint32_t blocklen; |
| 48 | /* transfer */ |
| 49 | int xfer_dir; /* 0 none, 1 read, 2 write */ |
| 50 | uint64_t xfer_block; |
| 51 | uint32_t xfer_left, xfer_word; |
| 52 | int prg_polls, prg_after_write; |
| 53 | /* timing */ |
| 54 | int cmd_inhibit_reads, data_inhibit_reads; |
| 55 | uint64_t now_us; |
| 56 | /* faults */ |
| 57 | int reset_stuck, data_err_at_block, data_err_armed, r1_err_cmd, csd_override; |
| 58 | uint32_t csd_raw[4]; |
| 59 | /* observations */ |
| 60 | int violations, commands, accesses; |
| 61 | uint32_t last_arg[64]; |
| 62 | } model; |
| 63 | |
| 64 | static void violation(model *m, const char *why) |
| 65 | { |
| 66 | m->violations++; |
| 67 | printf(" model violation: %s\n", why); |
| 68 | } |
| 69 | |
| 70 | static uint32_t r1(model *m) { return (uint32_t)m->state << 9 | (m->state == ST_TRAN ? 0x100u : 0) | (m->app ? 0x20u : 0); } |
| 71 | |
| 72 | /* Put an R2 value given as CSD/CID bits 127:8 into RESPONSE 119:0. */ |
| 73 | static void set_r2(model *m, const uint32_t v[4]) |
| 74 | { |
| 75 | for (int i = 0; i < 4; i++) |
| 76 | m->reg[0x10 / 4 + i] = v[i]; |
| 77 | } |
| 78 | |
| 79 | static void put_bits(uint32_t r[4], unsigned hi, unsigned lo, uint32_t val) /* CSD bit numbers */ |
| 80 | { |
| 81 | for (unsigned b = lo; b <= hi; b++, val >>= 1) { |
| 82 | unsigned rb = b - 8; |
| 83 | if (val & 1) |
| 84 | r[rb / 32] |= 1u << (rb % 32); |
| 85 | else |
| 86 | r[rb / 32] &= ~(1u << (rb % 32)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void raise_int(model *m, uint32_t bits) { m->reg[0x30 / 4] |= bits; } |
| 91 | static void next_block_ready(model *m); |
| 92 | |
| 93 | static void do_command(model *m, uint32_t word) |
| 94 | { |
| 95 | uint32_t tmode = word & 0xffff, cmdreg = word >> 16, idx = (cmdreg >> 8) & 0x3f, flags = cmdreg & 0xff; |
| 96 | uint32_t arg = m->reg[0x08 / 4]; |
| 97 | int app = m->app; |
| 98 | m->app = 0; |
| 99 | m->commands++; |
| 100 | m->last_arg[idx] = arg; |
| 101 | int data = (flags & 0x20) != 0; |
| 102 | if (m->cmd_inhibit_reads > 0 || (data && m->data_inhibit_reads > 0)) |
| 103 | violation(m, "command issued while CMD/DATA inhibit set"); |
| 104 | m->cmd_inhibit_reads = 1; |
| 105 | if (data) |
| 106 | m->data_inhibit_reads = 1; |
| 107 | uint32_t want = 0; |
| 108 | uint32_t resp = 0; |
| 109 | int error = 0; |
| 110 | if (app) { |
| 111 | switch (idx) { |
| 112 | case 41: /* R3 */ |
| 113 | want = 0x02; |
| 114 | if (++m->acmd41_polls >= m->acmd41_needed && m->acmd41_needed >= 0) { |
| 115 | m->state = ST_READY; |
| 116 | resp = 0x80000000u | (m->sdsc ? 0 : 0x40000000u) | 0x00ff8000u; |
| 117 | } else { |
| 118 | resp = 0x00ff8000u; |
| 119 | } |
| 120 | break; |
| 121 | case 6: |
| 122 | want = 0x1a; |
| 123 | if (m->state != ST_TRAN || (arg != 0 && arg != 2)) |
| 124 | violation(m, "ACMD6 outside tran or bad width"); |
| 125 | m->bus4 = arg == 2; |
| 126 | resp = r1(m); |
| 127 | break; |
| 128 | default: |
| 129 | violation(m, "unexpected app command"); |
| 130 | } |
| 131 | } else { |
| 132 | switch (idx) { |
| 133 | case 0: |
| 134 | want = 0x00; |
| 135 | m->state = ST_IDLE; |
| 136 | break; |
| 137 | case 8: |
| 138 | want = 0x1a; |
| 139 | if (m->v1_card) |
| 140 | error = 0x00018000; /* command timeout */ |
| 141 | else |
| 142 | resp = arg & 0xfff; |
| 143 | break; |
| 144 | case 55: |
| 145 | want = 0x1a; |
| 146 | m->app = 1; |
| 147 | resp = r1(m) | 0x20u; |
| 148 | break; |
| 149 | case 2: { |
| 150 | want = 0x09; |
| 151 | uint32_t cid[4] = { 0x11223344, 0x55667788, 0x99aabbcc, 0x00ddeeff }; |
| 152 | set_r2(m, cid); |
| 153 | m->state = ST_IDENT; |
| 154 | break; |
| 155 | } |
| 156 | case 3: |
| 157 | want = 0x1a; |
| 158 | m->rca = 0x1234; |
| 159 | m->state = ST_STBY; |
| 160 | resp = (uint32_t)m->rca << 16 | 0x0500; |
| 161 | break; |
| 162 | case 9: { |
| 163 | want = 0x09; |
| 164 | if (arg != (uint32_t)m->rca << 16) |
| 165 | violation(m, "CMD9 wrong RCA"); |
| 166 | uint32_t csd[4] = { 0 }; |
| 167 | if (m->csd_override) { |
| 168 | memcpy(csd, m->csd_raw, sizeof csd); |
| 169 | } else if (m->sdsc) { /* CSD 1.0: C_SIZE, C_SIZE_MULT, READ_BL_LEN = 9 */ |
| 170 | put_bits(csd, 127, 126, 0); |
| 171 | put_bits(csd, 83, 80, 9); |
| 172 | uint64_t mult = 7; /* 2^(7+2) = 512 */ |
| 173 | uint64_t csize = m->blocks / 512 - 1; |
| 174 | put_bits(csd, 73, 62, (uint32_t)csize); |
| 175 | put_bits(csd, 49, 47, (uint32_t)mult); |
| 176 | } else { |
| 177 | put_bits(csd, 127, 126, 1); |
| 178 | put_bits(csd, 69, 48, (uint32_t)(m->blocks / 1024 - 1)); |
| 179 | } |
| 180 | set_r2(m, csd); |
| 181 | break; |
| 182 | } |
| 183 | case 7: |
| 184 | want = 0x1b; |
| 185 | if (arg != (uint32_t)m->rca << 16) |
| 186 | violation(m, "CMD7 wrong RCA"); |
| 187 | m->state = ST_TRAN; |
| 188 | resp = r1(m); |
| 189 | raise_int(m, 0x2); /* busy end */ |
| 190 | break; |
| 191 | case 13: |
| 192 | want = 0x1a; |
| 193 | if (m->prg_polls > 0 && --m->prg_polls == 0) |
| 194 | m->state = ST_TRAN; |
| 195 | resp = r1(m); |
| 196 | break; |
| 197 | case 16: |
| 198 | want = 0x1a; |
| 199 | m->blocklen = arg; |
| 200 | resp = r1(m); |
| 201 | break; |
| 202 | case 17: |
| 203 | case 18: |
| 204 | case 24: |
| 205 | case 25: { |
| 206 | want = 0x3a; |
| 207 | if (m->state != ST_TRAN) |
| 208 | violation(m, "data command while the card is not in tran (programming?)"); |
| 209 | uint32_t bs = m->reg[0x04 / 4] & 0xfff, bc = m->reg[0x04 / 4] >> 16; |
| 210 | if (bs != 512) |
| 211 | violation(m, "block size not 512"); |
| 212 | int multi = idx == 18 || idx == 25; |
| 213 | if (multi && (tmode & 0x26) != 0x26) |
| 214 | violation(m, "multi-block without BLK_CNT_EN|MULTI|AUTO_CMD12"); |
| 215 | if (!multi && (tmode & 0x20)) |
| 216 | violation(m, "single-block with MULTI"); |
| 217 | int rd = idx == 17 || idx == 18; |
| 218 | if (!!(tmode & 0x10) != rd) |
| 219 | violation(m, "TRANSFER_MODE read bit does not match the command"); |
| 220 | uint64_t blk; |
| 221 | if (m->sdsc) { |
| 222 | if (arg % 512) |
| 223 | violation(m, "SDSC address not a byte address of a block"); |
| 224 | blk = arg / 512; |
| 225 | } else { |
| 226 | blk = arg; |
| 227 | } |
| 228 | uint32_t n = multi ? bc : 1; |
| 229 | resp = r1(m); |
| 230 | if (blk >= m->blocks || n > m->blocks - blk) { |
| 231 | resp |= 0x80000000u; /* OUT_OF_RANGE */ |
| 232 | break; |
| 233 | } |
| 234 | if ((int)idx == m->r1_err_cmd) { |
| 235 | resp |= 0x40000000u; /* ADDRESS_ERROR, no data follows */ |
| 236 | break; |
| 237 | } |
| 238 | m->xfer_dir = rd ? 1 : 2; |
| 239 | m->xfer_block = blk; |
| 240 | m->xfer_left = n; |
| 241 | m->xfer_word = 0; |
| 242 | m->state = rd ? ST_DATA : ST_RCV; |
| 243 | next_block_ready(m); |
| 244 | break; |
| 245 | } |
| 246 | default: |
| 247 | violation(m, "unexpected command"); |
| 248 | } |
| 249 | } |
| 250 | if ((flags & 0x3f) != want) { |
| 251 | char b[96]; |
| 252 | snprintf(b, sizeof b, "CMD%u flags 0x%02x, want 0x%02x", idx, flags & 0x3f, want); |
| 253 | violation(m, b); |
| 254 | } |
| 255 | if (error) { |
| 256 | raise_int(m, (uint32_t)error); |
| 257 | return; |
| 258 | } |
| 259 | m->reg[0x10 / 4] = (flags & 3) == 1 ? m->reg[0x10 / 4] : resp; |
| 260 | raise_int(m, 0x1); |
| 261 | } |
| 262 | |
| 263 | /* Make the next block ready, or report the injected data CRC error in its place |
| 264 | * (real controllers raise the error instead of BUF_RD_READY). */ |
| 265 | static void next_block_ready(model *m) |
| 266 | { |
| 267 | if (m->xfer_dir == 1 && m->data_err_armed && (int64_t)m->xfer_block == m->data_err_at_block) { |
| 268 | m->data_err_armed = 0; |
| 269 | m->xfer_dir = 0; |
| 270 | m->state = ST_TRAN; |
| 271 | m->data_inhibit_reads = 2; |
| 272 | raise_int(m, 0x00208000); |
| 273 | return; |
| 274 | } |
| 275 | raise_int(m, m->xfer_dir == 1 ? 0x20 : 0x10); |
| 276 | } |
| 277 | |
| 278 | static void block_done(model *m) |
| 279 | { |
| 280 | m->xfer_word = 0; |
| 281 | m->xfer_block++; |
| 282 | if (--m->xfer_left == 0) { |
| 283 | if (m->xfer_dir == 2) { |
| 284 | m->state = ST_PRG; |
| 285 | m->prg_polls = m->prg_after_write ? m->prg_after_write : 1; |
| 286 | } else { |
| 287 | m->state = ST_TRAN; |
| 288 | } |
| 289 | m->xfer_dir = 0; |
| 290 | m->data_inhibit_reads = 2; |
| 291 | raise_int(m, 0x2); |
| 292 | return; |
| 293 | } |
| 294 | next_block_ready(m); |
| 295 | } |
| 296 | |
| 297 | static uint32_t m_r32(void *ctx, uint32_t off) |
| 298 | { |
| 299 | model *m = ctx; |
| 300 | m->accesses++; |
| 301 | if (off % 4 || off >= 0x100) { |
| 302 | violation(m, "unaligned or out-of-window read"); |
| 303 | return 0; |
| 304 | } |
| 305 | if (off == 0x24) { |
| 306 | uint32_t v = (m->present ? 0x00010000u : 0) | (m->cmd_inhibit_reads > 0 ? 1u : 0) | |
| 307 | (m->data_inhibit_reads > 0 ? 2u : 0); |
| 308 | if (m->cmd_inhibit_reads > 0) |
| 309 | m->cmd_inhibit_reads--; |
| 310 | if (m->data_inhibit_reads > 0) |
| 311 | m->data_inhibit_reads--; |
| 312 | return v; |
| 313 | } |
| 314 | if (off == 0x20) { |
| 315 | if (m->xfer_dir != 1) { |
| 316 | violation(m, "buffer read with no read transfer"); |
| 317 | return 0; |
| 318 | } |
| 319 | const uint8_t *p = m->mem + m->xfer_block * 512 + m->xfer_word * 4; |
| 320 | uint32_t v = (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; |
| 321 | if (++m->xfer_word == 128) |
| 322 | block_done(m); |
| 323 | return v; |
| 324 | } |
| 325 | return m->reg[off / 4]; |
| 326 | } |
| 327 | |
| 328 | static void m_w32(void *ctx, uint32_t off, uint32_t v) |
| 329 | { |
| 330 | model *m = ctx; |
| 331 | m->accesses++; |
| 332 | if (off % 4 || off >= 0x100) { |
| 333 | violation(m, "unaligned or out-of-window write"); |
| 334 | return; |
| 335 | } |
| 336 | switch (off) { |
| 337 | case 0x0c: |
| 338 | do_command(m, v); |
| 339 | return; |
| 340 | case 0x20: { |
| 341 | if (m->xfer_dir != 2) { |
| 342 | violation(m, "buffer write with no write transfer"); |
| 343 | return; |
| 344 | } |
| 345 | uint8_t *p = m->mem + m->xfer_block * 512 + m->xfer_word * 4; |
| 346 | p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); |
| 347 | if (++m->xfer_word == 128) |
| 348 | block_done(m); |
| 349 | return; |
| 350 | } |
| 351 | case 0x2c: { |
| 352 | uint32_t old = m->reg[0x2c / 4]; |
| 353 | if ((old >> 24) && (v >> 24)) |
| 354 | violation(m, "SOFTWARE_RESET written while a reset bit is still set"); |
| 355 | if (v & (1u << 24)) { /* reset all */ |
| 356 | uint32_t cap = m->reg[0x40 / 4], ver = m->reg[0xfc / 4]; |
| 357 | memset(m->reg, 0, sizeof m->reg); |
| 358 | m->reg[0x40 / 4] = cap; |
| 359 | m->reg[0xfc / 4] = ver; |
| 360 | m->reg[0x2c / 4] = m->reset_stuck ? (1u << 24) : 0; |
| 361 | m->xfer_dir = 0; |
| 362 | return; |
| 363 | } |
| 364 | if (v & (3u << 25)) { /* line resets clear at once */ |
| 365 | m->xfer_dir = 0; |
| 366 | v &= ~(3u << 25); |
| 367 | } |
| 368 | if (v & 1) |
| 369 | v |= 2; /* internal clock stable at once */ |
| 370 | m->reg[0x2c / 4] = v | (m->reset_stuck ? (1u << 24) : 0); |
| 371 | return; |
| 372 | } |
| 373 | case 0x30: |
| 374 | m->reg[0x30 / 4] &= ~v; |
| 375 | if (!(m->reg[0x30 / 4] & 0xffff0000u)) |
| 376 | m->reg[0x30 / 4] &= ~0x8000u; |
| 377 | return; |
| 378 | case 0x40: |
| 379 | case 0xfc: |
| 380 | violation(m, "write to a read-only register"); |
| 381 | return; |
| 382 | default: |
| 383 | m->reg[off / 4] = v; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void m_delay(void *ctx, uint32_t us) { ((model *)ctx)->now_us += us; } |
| 388 | |
| 389 | static void model_init(model *m, uint64_t blocks, uint32_t version, uint32_t base_mhz) |
| 390 | { |
| 391 | memset(m, 0, sizeof *m); |
| 392 | m->mem = calloc(blocks, 512); |
| 393 | m->blocks = blocks; |
| 394 | m->present = 1; |
| 395 | m->acmd41_needed = 3; |
| 396 | m->data_err_at_block = -1; |
| 397 | m->r1_err_cmd = -1; |
| 398 | m->reg[0xfc / 4] = version << 16; |
| 399 | m->reg[0x40 / 4] = (1u << 24) | ((base_mhz & 0xff) << 8); |
| 400 | } |
| 401 | |
| 402 | static sdhci_ops ops_for(model *m) |
| 403 | { |
| 404 | sdhci_ops o = { m, m_r32, m_w32, m_delay }; |
| 405 | return o; |
| 406 | } |
| 407 | |
| 408 | static const sdhci_cfg cfg4 = { 0, 0, 1, 4 }; |
| 409 | |
| 410 | static void test_sdhc_round_trip(void) |
| 411 | { |
| 412 | model m; |
| 413 | sdhci_card c; |
| 414 | model_init(&m, 8192, 2, 200); /* spec 3.00, 200 MHz base clock (Pi 5 class) */ |
| 415 | sdhci_ops o = ops_for(&m); |
| 416 | CHECK(sdhci_init(&c, &o, &cfg4) == 0); |
| 417 | CHECK(c.ready && c.high_capacity == 1 && c.blocks == 8192 && c.rca == 0x1234 && m.bus4 == 1); |
| 418 | CHECK(c.spec == 2 && c.base_hz == 200000000u && c.card_hz == 25000000u); |
| 419 | CHECK(c.dev.block_size == 512 && c.dev.block_count == 8192 && c.dev.max_blocks_per_io == 4); |
| 420 | uint8_t w[10 * 512], r[10 * 512]; |
| 421 | for (size_t i = 0; i < sizeof w; i++) |
| 422 | w[i] = (uint8_t)(i * 7 + 3); |
| 423 | CHECK(disk_write(&c.dev, 100, 10, w) == DISK_OK); /* split into 4 + 4 + 2 */ |
| 424 | CHECK(memcmp(m.mem + 100 * 512, w, sizeof w) == 0); |
| 425 | CHECK(disk_write(&c.dev, 8191, 1, w) == DISK_OK); /* last block, single */ |
| 426 | CHECK(disk_flush(&c.dev) == DISK_OK); |
| 427 | memset(r, 0, sizeof r); |
| 428 | CHECK(disk_read(&c.dev, 100, 10, r) == DISK_OK && memcmp(r, w, sizeof w) == 0); |
| 429 | CHECK(disk_read(&c.dev, 8191, 1, r) == DISK_OK && memcmp(r, w, 512) == 0); |
| 430 | CHECK(m.last_arg[18] == 108 && m.last_arg[25] == 108); /* block addressing */ |
| 431 | /* Range refused before the card sees it. */ |
| 432 | int before = m.commands; |
| 433 | CHECK(sdhci_read(&c, 8190, 3, r) == DISK_ERANGE && m.commands == before); |
| 434 | CHECK(c.ready); |
| 435 | CHECK(m.violations == 0); |
| 436 | free(m.mem); |
| 437 | } |
| 438 | |
| 439 | static void test_sdsc_and_clock_v2(void) |
| 440 | { |
| 441 | model m; |
| 442 | sdhci_card c; |
| 443 | model_init(&m, 4096, 1, 52); /* spec 2.00, 52 MHz */ |
| 444 | m.sdsc = 1; |
| 445 | sdhci_ops o = ops_for(&m); |
| 446 | sdhci_cfg cfg = { 0, 0, 0, 0 }; |
| 447 | CHECK(sdhci_init(&c, &o, &cfg) == 0); |
| 448 | CHECK(c.high_capacity == 0 && c.blocks == 4096 && m.blocklen == 512 && m.bus4 == 0); |
| 449 | CHECK(c.card_hz == 13000000u); /* 52 MHz / 4: the largest power-of-two step <= 25 MHz */ |
| 450 | CHECK(c.dev.max_blocks_per_io == 128); |
| 451 | uint8_t w[3 * 512], r[3 * 512]; |
| 452 | for (size_t i = 0; i < sizeof w; i++) |
| 453 | w[i] = (uint8_t)(i ^ 0x5a); |
| 454 | CHECK(disk_write(&c.dev, 7, 3, w) == DISK_OK && m.last_arg[25] == 7 * 512); |
| 455 | CHECK(disk_read(&c.dev, 7, 3, r) == DISK_OK && memcmp(r, w, sizeof w) == 0 && m.last_arg[18] == 7 * 512); |
| 456 | CHECK(m.violations == 0); |
| 457 | free(m.mem); |
| 458 | } |
| 459 | |
| 460 | static void test_busy_after_write(void) |
| 461 | { |
| 462 | model m; |
| 463 | sdhci_card c; |
| 464 | model_init(&m, 2048, 2, 100); |
| 465 | m.prg_after_write = 5; /* card programs for five CMD13 polls */ |
| 466 | sdhci_ops o = ops_for(&m); |
| 467 | CHECK(sdhci_init(&c, &o, &cfg4) == 0); |
| 468 | uint8_t w[512] = { 1, 2, 3 }, r[512]; |
| 469 | CHECK(disk_write(&c.dev, 1, 1, w) == DISK_OK); |
| 470 | CHECK(disk_read(&c.dev, 1, 1, r) == DISK_OK && memcmp(r, w, 512) == 0); |
| 471 | CHECK(m.violations == 0); /* no data command while programming */ |
| 472 | free(m.mem); |
| 473 | } |
| 474 | |
| 475 | static void test_failstop_on_data_error(void) |
| 476 | { |
| 477 | model m; |
| 478 | sdhci_card c; |
| 479 | model_init(&m, 2048, 2, 100); |
| 480 | sdhci_ops o = ops_for(&m); |
| 481 | CHECK(sdhci_init(&c, &o, &cfg4) == 0); |
| 482 | uint8_t r[4 * 512]; |
| 483 | m.data_err_at_block = 11; |
| 484 | m.data_err_armed = 1; |
| 485 | CHECK(disk_read(&c.dev, 10, 4, r) == DISK_EIO); |
| 486 | CHECK(c.last_err == SDHCI_EDATA && c.last_cmd == 18 && (c.last_int & 0x00200000u)); |
| 487 | CHECK(disk_read(&c.dev, 10, 1, r) == DISK_ESTATE); /* failed closed */ |
| 488 | CHECK(disk_flush(&c.dev) == DISK_ESTATE); |
| 489 | CHECK(m.violations == 0); |
| 490 | free(m.mem); |
| 491 | } |
| 492 | |
| 493 | static void test_r1_error_bits(void) |
| 494 | { |
| 495 | model m; |
| 496 | sdhci_card c; |
| 497 | model_init(&m, 2048, 2, 100); |
| 498 | m.r1_err_cmd = 17; |
| 499 | sdhci_ops o = ops_for(&m); |
| 500 | CHECK(sdhci_init(&c, &o, &cfg4) == 0); |
| 501 | uint8_t r[512]; |
| 502 | CHECK(disk_read(&c.dev, 3, 1, r) == DISK_EIO && c.last_err == SDHCI_ESTATUS && c.last_cmd == 17); |
| 503 | CHECK(m.violations == 0); |
| 504 | free(m.mem); |
| 505 | } |
| 506 | |
| 507 | static int init_rc(void (*setup)(model *), uint32_t version, uint32_t base_mhz, uint32_t cfg_hz) |
| 508 | { |
| 509 | model m; |
| 510 | sdhci_card c; |
| 511 | model_init(&m, 2048, version, base_mhz); |
| 512 | if (setup) |
| 513 | setup(&m); |
| 514 | sdhci_ops o = ops_for(&m); |
| 515 | sdhci_cfg cfg = { cfg_hz, 0, 1, 0 }; |
| 516 | int rc = sdhci_init(&c, &o, &cfg); |
| 517 | free(m.mem); |
| 518 | return rc; |
| 519 | } |
| 520 | static void no_volt(model *m) { m->reg[0x40 / 4] &= ~(1u << 24); } |
| 521 | static void no_card(model *m) { m->present = 0; } |
| 522 | static void v1_card(model *m) { m->v1_card = 1; } |
| 523 | static void never_ready(model *m) { m->acmd41_needed = -1; } |
| 524 | static void stuck_reset(model *m) { m->reset_stuck = 1; } |
| 525 | static void zero_csd(model *m) |
| 526 | { |
| 527 | m->sdsc = 1; |
| 528 | m->csd_override = 1; /* CSD 1.0 with C_SIZE 0, C_SIZE_MULT 0, READ_BL_LEN 0: 4 bytes */ |
| 529 | } |
| 530 | |
| 531 | static void test_refusals(void) |
| 532 | { |
| 533 | CHECK(init_rc(0, 0, 100, 0) == SDHCI_EVER); |
| 534 | CHECK(init_rc(stuck_reset, 2, 100, 0) == SDHCI_ERESET); |
| 535 | CHECK(init_rc(no_volt, 2, 100, 0) == SDHCI_EVOLT); |
| 536 | CHECK(init_rc(0, 2, 0, 0) == SDHCI_ECLOCK); /* no base clock anywhere */ |
| 537 | CHECK(init_rc(0, 2, 0, 100000000u) == 0); /* base clock from cfg */ |
| 538 | CHECK(init_rc(no_card, 2, 100, 0) == SDHCI_ENOCARD); |
| 539 | CHECK(init_rc(v1_card, 2, 100, 0) == SDHCI_ECARD); |
| 540 | CHECK(init_rc(never_ready, 2, 100, 0) == SDHCI_ETIMEOUT); |
| 541 | CHECK(init_rc(zero_csd, 2, 100, 0) == SDHCI_ECSD); |
| 542 | sdhci_card c; |
| 543 | model m; |
| 544 | model_init(&m, 16, 2, 100); |
| 545 | sdhci_ops o = ops_for(&m); |
| 546 | sdhci_cfg big = { 0, 0, 1, 70000 }; |
| 547 | CHECK(sdhci_init(&c, &o, &big) == SDHCI_EARG); |
| 548 | CHECK(sdhci_init(&c, 0, &cfg4) == SDHCI_EARG); |
| 549 | free(m.mem); |
| 550 | } |
| 551 | |
| 552 | static void test_clock_math(void) |
| 553 | { |
| 554 | model m; |
| 555 | sdhci_card c; |
| 556 | model_init(&m, 2048, 2, 100); /* 3.00, 100 MHz: 25 MHz is N = 2 */ |
| 557 | sdhci_ops o = ops_for(&m); |
| 558 | CHECK(sdhci_init(&c, &o, &cfg4) == 0 && c.card_hz == 25000000u); |
| 559 | CHECK(((m.reg[0x2c / 4] >> 8) & 0xff) == 2 && (m.reg[0x2c / 4] & 0x7) == 0x7); |
| 560 | free(m.mem); |
| 561 | model_init(&m, 2048, 1, 48); /* 2.00, 48 MHz: divisor 2 gives 24 MHz */ |
| 562 | CHECK(sdhci_init(&c, &o, &cfg4) == 0 && c.card_hz == 24000000u && ((m.reg[0x2c / 4] >> 8) & 0xff) == 1); |
| 563 | free(m.mem); |
| 564 | } |
| 565 | |
| 566 | int main(void) |
| 567 | { |
| 568 | setvbuf(stdout, 0, _IONBF, 0); |
| 569 | printf("- %s\n", "test_sdhc_round_trip"); |
| 570 | test_sdhc_round_trip(); |
| 571 | printf("- %s\n", "test_sdsc_and_clock_v2"); |
| 572 | test_sdsc_and_clock_v2(); |
| 573 | printf("- %s\n", "test_busy_after_write"); |
| 574 | test_busy_after_write(); |
| 575 | printf("- %s\n", "test_failstop_on_data_error"); |
| 576 | test_failstop_on_data_error(); |
| 577 | printf("- %s\n", "test_r1_error_bits"); |
| 578 | test_r1_error_bits(); |
| 579 | printf("- %s\n", "test_refusals"); |
| 580 | test_refusals(); |
| 581 | printf("- %s\n", "test_clock_math"); |
| 582 | test_clock_math(); |
| 583 | if (failures) { |
| 584 | printf("AIENOS_DISK_SDHCI_MODEL: FAIL (%d)\n", failures); |
| 585 | return 1; |
| 586 | } |
| 587 | printf("AIENOS_DISK_SDHCI_MODEL: PASS\n"); |
| 588 | return 0; |
| 589 | } |