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/aienux/blob/main/native/disk/disk_sdhci.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / disk / disk_sdhci.c` 434 lines ยท 15747 bytes Raw Blame History 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 /* disk_sdhci.c -- AIENOS freestanding SD card driver over SDHCI; see 18 * disk_sdhci.h for the contract, sources and refusals. No libc. */ 19 #include "disk_sdhci.h" 20 21 /* PRESENT_STATE */ 22 #define PS_CMD_INHIBIT 0x00000001u 23 #define PS_DATA_INHIBIT 0x00000002u 24 #define PS_CARD_PRESENT 0x00010000u 25 /* INT_STATUS */ 26 #define INT_CMD_DONE 0x00000001u 27 #define INT_XFER_DONE 0x00000002u 28 #define INT_BUF_WR 0x00000010u 29 #define INT_BUF_RD 0x00000020u 30 #define INT_ERROR 0x00008000u 31 #define INT_ERR_MASK 0xffff8000u 32 /* COMMAND (upper half of the 0x0C word) */ 33 #define CMD_RESP_NONE 0x00u 34 #define CMD_RESP_LONG 0x01u 35 #define CMD_RESP_SHORT 0x02u 36 #define CMD_RESP_BUSY 0x03u 37 #define CMD_CRC 0x08u 38 #define CMD_INDEX 0x10u 39 #define CMD_DATA 0x20u 40 /* TRANSFER_MODE (lower half of the 0x0C word) */ 41 #define TM_BLK_CNT_EN 0x02u 42 #define TM_AUTO_CMD12 0x04u 43 #define TM_READ 0x10u 44 #define TM_MULTI 0x20u 45 /* CLOCK word at 0x2C */ 46 #define CLK_INT_EN 0x0001u 47 #define CLK_INT_STABLE 0x0002u 48 #define CLK_CARD_EN 0x0004u 49 #define RST_ALL (1u << 24) 50 #define RST_CMD (1u << 25) 51 #define RST_DATA (1u << 26) 52 /* HOST word at 0x28 */ 53 #define HOST_4BIT 0x02u 54 #define PWR_330 (0x0eu << 8) 55 #define PWR_ON (0x01u << 8) 56 57 /* Response kinds (SD Physical Layer 4.9). */ 58 enum { R_NONE, R1, R1B, R2, R3, R6, R7 }; 59 60 /* R1 card status error bits: OUT_OF_RANGE..WP_VIOLATION (31:26), 61 * LOCK_UNLOCK_FAILED (24), COM_CRC_ERROR..ERROR (23:19), CSD_OVERWRITE (16), 62 * WP_ERASE_SKIP (15), AKE_SEQ_ERROR (3). */ 63 #define R1_ERRORS 0xfdf98008u 64 #define R1_READY_FOR_DATA 0x00000100u 65 #define R1_STATE(s) (((s) >> 9) & 0xfu) 66 #define STATE_TRAN 4u 67 68 #define BLOCK 512u 69 70 static uint32_t rd(sdhci_card *c, uint32_t off) { return c->ops.r32(c->ops.ctx, off); } 71 static void wr(sdhci_card *c, uint32_t off, uint32_t v) { c->ops.w32(c->ops.ctx, off, v); } 72 static void delay(sdhci_card *c, uint32_t us) { c->ops.delay_us(c->ops.ctx, us); } 73 74 static int fail(sdhci_card *c, int cmd, int err) 75 { 76 c->last_cmd = cmd; 77 c->last_err = err; 78 return err; 79 } 80 81 /* Poll until (reg & mask) == want, every 10 us, at most ms milliseconds. */ 82 static int wait_reg(sdhci_card *c, uint32_t off, uint32_t mask, uint32_t want, uint32_t ms) 83 { 84 for (uint32_t i = 0; i <= ms * 100u; i++) { 85 if ((rd(c, off) & mask) == want) 86 return 0; 87 delay(c, 10); 88 } 89 return -1; 90 } 91 92 /* Software reset of the CMD/DATA lines (bits 25/26 of the 0x2C word). */ 93 static void line_reset(sdhci_card *c, uint32_t bits) 94 { 95 uint32_t v = rd(c, SDHCI_R_CLOCK); 96 if (v & (RST_ALL | RST_CMD | RST_DATA)) /* a reset still running: do not re-trigger */ 97 (void)wait_reg(c, SDHCI_R_CLOCK, RST_ALL | RST_CMD | RST_DATA, 0, 100); 98 v = rd(c, SDHCI_R_CLOCK) & 0x00ffffffu; 99 wr(c, SDHCI_R_CLOCK, v | bits); 100 (void)wait_reg(c, SDHCI_R_CLOCK, bits, 0, 100); 101 } 102 103 /* Wait for any of `mask` or an error bit in INT_STATUS. 0 ok (the mask bits 104 * seen are cleared), -1 error bit, -2 timeout. */ 105 static int wait_int(sdhci_card *c, uint32_t mask, uint32_t ms) 106 { 107 for (uint32_t i = 0; i <= ms * 100u; i++) { 108 uint32_t s = rd(c, SDHCI_R_INT); 109 c->last_int = s; 110 if (s & (INT_ERROR | INT_ERR_MASK)) return -1; /* GUARD:sdhci-err-int */ 111 if (s & mask) { 112 wr(c, SDHCI_R_INT, s & mask); 113 return 0; 114 } 115 delay(c, 10); 116 } 117 c->last_int = rd(c, SDHCI_R_INT); 118 return -2; 119 } 120 121 static uint32_t cmd_flags(int kind) 122 { 123 switch (kind) { 124 case R1: 125 case R6: 126 case R7: 127 return CMD_RESP_SHORT | CMD_CRC | CMD_INDEX; 128 case R1B: 129 return CMD_RESP_BUSY | CMD_CRC | CMD_INDEX; 130 case R2: 131 return CMD_RESP_LONG | CMD_CRC; 132 case R3: 133 return CMD_RESP_SHORT; 134 default: 135 return CMD_RESP_NONE; 136 } 137 } 138 139 /* Issue one command; for data commands the caller moves the data and waits 140 * for transfer complete. resp gets RESPONSE0..3 (long) or RESPONSE0. */ 141 static int command(sdhci_card *c, uint32_t idx, uint32_t arg, int kind, uint32_t tmode, uint32_t nblocks, 142 uint32_t resp[4]) 143 { 144 uint32_t ms = c->cfg.timeout_ms; 145 int data = nblocks != 0; 146 uint32_t inhibit = PS_CMD_INHIBIT | ((data || kind == R1B) ? PS_DATA_INHIBIT : 0); 147 if (wait_reg(c, SDHCI_R_PRESENT, inhibit, 0, ms)) return fail(c, (int)idx, SDHCI_ETIMEOUT); /* GUARD:sdhci-inhibit */ 148 wr(c, SDHCI_R_INT, 0xffffffffu); 149 if (data) 150 wr(c, SDHCI_R_BLOCK, BLOCK | (nblocks << 16)); 151 wr(c, SDHCI_R_ARG, arg); 152 uint32_t f = cmd_flags(kind) | (data ? CMD_DATA : 0); 153 wr(c, SDHCI_R_CMD, ((idx << 8 | f) << 16) | tmode); 154 int w = wait_int(c, INT_CMD_DONE, ms); 155 if (w) { 156 line_reset(c, RST_CMD | RST_DATA); 157 return fail(c, (int)idx, w == -2 ? SDHCI_ETIMEOUT : SDHCI_ECMD); 158 } 159 if (resp) { 160 resp[0] = rd(c, SDHCI_R_RESP0); 161 if (kind == R2) 162 for (int i = 1; i < 4; i++) 163 resp[i] = rd(c, SDHCI_R_RESP0 + 4u * (uint32_t)i); 164 } 165 if (kind == R1B && wait_int(c, INT_XFER_DONE, ms)) { /* end of busy */ 166 line_reset(c, RST_CMD | RST_DATA); 167 return fail(c, (int)idx, SDHCI_ETIMEOUT); 168 } 169 if ((kind == R1 || kind == R1B) && resp && (resp[0] & R1_ERRORS)) return fail(c, (int)idx, SDHCI_ESTATUS); /* GUARD:sdhci-r1 */ 170 return 0; 171 } 172 173 static int app_command(sdhci_card *c, uint32_t idx, uint32_t arg, int kind, uint32_t resp[4]) 174 { 175 uint32_t r[4]; 176 int rc = command(c, 55, (uint32_t)c->rca << 16, R1, 0, 0, r); 177 if (rc) 178 return rc; 179 return command(c, idx, arg, kind, 0, 0, resp); 180 } 181 182 /* SD clock: <= hz from the base clock. Spec 2.00: power-of-two divisor 183 * 1..256 (field = divisor / 2); 3.00+: 10-bit N, divisor 2N (N = 0: base). */ 184 static int set_clock(sdhci_card *c, uint32_t hz) 185 { 186 uint32_t base = c->base_hz, field, actual; 187 if (c->spec >= 2) { 188 uint32_t n = base <= hz ? 0 : (base + 2 * hz - 1) / (2 * hz); 189 if (n > 0x3ff) 190 return SDHCI_ECLOCK; 191 actual = n ? base / (2 * n) : base; 192 field = ((n & 0xff) << 8) | ((n >> 8) & 0x3) << 6; 193 } else { 194 uint32_t div = 1; 195 while (div < 256 && base / div > hz) 196 div *= 2; 197 if (base / div > hz) 198 return SDHCI_ECLOCK; 199 actual = base / div; 200 field = (div / 2) << 8; 201 } 202 uint32_t v = rd(c, SDHCI_R_CLOCK) & 0xffff0000u; 203 wr(c, SDHCI_R_CLOCK, v); /* SD clock and internal clock off */ 204 wr(c, SDHCI_R_CLOCK, v | field | CLK_INT_EN); 205 if (wait_reg(c, SDHCI_R_CLOCK, CLK_INT_STABLE, CLK_INT_STABLE, 100)) 206 return SDHCI_ECLOCK; 207 wr(c, SDHCI_R_CLOCK, v | field | CLK_INT_EN | CLK_CARD_EN); 208 c->card_hz = actual; 209 return 0; 210 } 211 212 /* Bits [hi:lo] of a long response as the controller stores it (R2 bits 213 * 127:8 in RESPONSE bits 119:0), addressed by register bit position. */ 214 static uint32_t rbits(const uint32_t r[4], unsigned hi, unsigned lo) 215 { 216 uint32_t v = 0; 217 for (unsigned b = hi + 1; b-- > lo;) 218 v = (v << 1) | ((r[b / 32] >> (b % 32)) & 1u); 219 return v; 220 } 221 #define CSD(r, hi, lo) rbits((r), (hi) - 8, (lo) - 8) /* CSD bit numbers (Physical Layer 5.3) */ 222 223 static int card_status(sdhci_card *c, uint32_t *st) 224 { 225 uint32_t r[4]; 226 int rc = command(c, 13, (uint32_t)c->rca << 16, R1, 0, 0, r); 227 *st = r[0]; 228 return rc; 229 } 230 231 /* After a write: wait until the card left programming (READY_FOR_DATA, tran). */ 232 static int wait_tran(sdhci_card *c) 233 { 234 for (uint32_t i = 0; i <= c->cfg.timeout_ms; i++) { 235 uint32_t st = 0; 236 int rc = card_status(c, &st); 237 if (rc) 238 return rc; 239 if ((st & R1_READY_FOR_DATA) && R1_STATE(st) == STATE_TRAN) 240 return 0; 241 delay(c, 1000); 242 } 243 return fail(c, 13, SDHCI_ETIMEOUT); 244 } 245 246 int sdhci_init(sdhci_card *c, const sdhci_ops *ops, const sdhci_cfg *cfg) 247 { 248 if (!c || !ops || !ops->r32 || !ops->w32 || !ops->delay_us || !cfg) 249 return SDHCI_EARG; 250 for (uint8_t *p = (uint8_t *)c, *e = p + sizeof *c; p < e; p++) /* no memset: no libc */ 251 *p = 0; 252 c->ops = *ops; 253 c->cfg = *cfg; 254 c->last_cmd = -1; 255 if (!c->cfg.timeout_ms) 256 c->cfg.timeout_ms = 1000; 257 if (!c->cfg.max_blocks) 258 c->cfg.max_blocks = 128; 259 if (c->cfg.max_blocks > 65535) 260 return fail(c, -1, SDHCI_EARG); 261 c->spec = (rd(c, SDHCI_R_VERSION) >> 16) & 0xff; 262 if (c->spec < 1) return fail(c, -1, SDHCI_EVER); /* GUARD:sdhci-ver */ 263 wr(c, SDHCI_R_CLOCK, (rd(c, SDHCI_R_CLOCK) & 0x00ffffffu) | RST_ALL); 264 if (wait_reg(c, SDHCI_R_CLOCK, RST_ALL, 0, 100)) return fail(c, -1, SDHCI_ERESET); /* GUARD:sdhci-reset */ 265 uint32_t cap = rd(c, SDHCI_R_CAP); 266 if (!(cap & (1u << 24))) return fail(c, -1, SDHCI_EVOLT); /* GUARD:sdhci-volt */ 267 c->base_hz = c->cfg.base_clock_hz; 268 if (!c->base_hz) 269 c->base_hz = ((cap >> 8) & (c->spec >= 2 ? 0xffu : 0x3fu)) * 1000000u; 270 if (!c->base_hz) return fail(c, -1, SDHCI_ECLOCK); /* GUARD:sdhci-clock0 */ 271 /* 3.3 V bus power, 1-bit, normal speed. */ 272 uint32_t host = rd(c, SDHCI_R_HOST) & 0xffff0000u; 273 wr(c, SDHCI_R_HOST, host | PWR_330); 274 wr(c, SDHCI_R_HOST, host | PWR_330 | PWR_ON); 275 delay(c, 10000); 276 if (wait_reg(c, SDHCI_R_PRESENT, PS_CARD_PRESENT, PS_CARD_PRESENT, 100)) return fail(c, -1, SDHCI_ENOCARD); /* GUARD:sdhci-nocard */ 277 wr(c, SDHCI_R_INT_EN, INT_ERR_MASK | INT_BUF_RD | INT_BUF_WR | INT_XFER_DONE | INT_CMD_DONE); 278 wr(c, SDHCI_R_SIG_EN, 0); 279 wr(c, SDHCI_R_CLOCK, (rd(c, SDHCI_R_CLOCK) & 0x0000ffffu) | (0x0eu << 16)); /* data timeout: max */ 280 if (set_clock(c, 400000)) 281 return fail(c, -1, SDHCI_ECLOCK); 282 delay(c, 1000); /* >= 74 clocks before CMD0 */ 283 284 uint32_t r[4] = { 0 }; 285 int rc = command(c, 0, 0, R_NONE, 0, 0, 0); 286 if (rc) 287 return rc; 288 if (command(c, 8, 0x1aa, R7, 0, 0, r) || (r[0] & 0xfff) != 0x1aa) return fail(c, 8, SDHCI_ECARD); /* GUARD:sdhci-cmd8 */ 289 for (uint32_t i = 0;; i++) { 290 rc = app_command(c, 41, 0x40ff8000u, R3, r); /* HCS, 2.7-3.6 V */ 291 if (rc) 292 return rc; 293 if (r[0] & 0x80000000u) 294 break; 295 if (i >= 100) return fail(c, 41, SDHCI_ETIMEOUT); /* GUARD:sdhci-acmd41-bound */ 296 delay(c, 10000); 297 } 298 c->high_capacity = (r[0] >> 30) & 1; 299 if ((rc = command(c, 2, 0, R2, 0, 0, c->cid))) 300 return rc; 301 if ((rc = command(c, 3, 0, R6, 0, 0, r))) 302 return rc; 303 c->rca = (uint16_t)(r[0] >> 16); 304 if ((rc = command(c, 9, (uint32_t)c->rca << 16, R2, 0, 0, c->csd))) 305 return rc; 306 uint32_t structure = CSD(c->csd, 127, 126); 307 if (structure == 1) { 308 c->blocks = ((uint64_t)CSD(c->csd, 69, 48) + 1) * 1024u; 309 } else if (structure == 0 && !c->high_capacity) { 310 uint64_t bytes = ((uint64_t)CSD(c->csd, 73, 62) + 1) << (CSD(c->csd, 49, 47) + 2) << CSD(c->csd, 83, 80); 311 c->blocks = bytes / BLOCK; 312 } else { 313 return fail(c, 9, SDHCI_ECSD); 314 } 315 if (!c->blocks) return fail(c, 9, SDHCI_ECSD); /* GUARD:sdhci-csd-zero */ 316 if (set_clock(c, `25000000`)) 317 return fail(c, -1, SDHCI_ECLOCK); 318 if ((rc = command(c, 7, (uint32_t)c->rca << 16, R1B, 0, 0, r))) 319 return rc; 320 if (c->cfg.bus_4bit) { 321 if ((rc = app_command(c, 6, 2, R1, r))) 322 return rc; 323 wr(c, SDHCI_R_HOST, rd(c, SDHCI_R_HOST) | HOST_4BIT); 324 } 325 if (!c->high_capacity && (rc = command(c, 16, BLOCK, R1, 0, 0, r))) 326 return rc; 327 c->dev.ctx = c; 328 c->dev.block_size = BLOCK; 329 c->dev.block_count = c->blocks; 330 c->dev.read = sdhci_read; 331 c->dev.write = sdhci_write; 332 c->dev.flush = sdhci_flush; 333 c->dev.max_blocks_per_io = c->cfg.max_blocks; 334 c->ready = 1; 335 return 0; 336 } 337 338 static int transfer(sdhci_card *c, uint64_t lba, uint32_t count, uint8_t *buf, int is_read) 339 { 340 if (!c || !buf || !count || count > c->cfg.max_blocks) 341 return DISK_EARG; 342 if (!c->ready) return DISK_ESTATE; /* GUARD:sdhci-ready */ 343 if (lba >= c->blocks || count > c->blocks - lba) return DISK_ERANGE; /* GUARD:sdhci-range */ 344 uint64_t addr = c->high_capacity ? lba : lba * BLOCK; 345 if (addr > 0xffffffffu) 346 return DISK_ERANGE; 347 uint32_t idx, tm = is_read ? TM_READ : 0; 348 if (count == 1) { 349 idx = is_read ? 17 : 24; 350 } else { 351 idx = is_read ? 18 : 25; 352 tm |= TM_BLK_CNT_EN | TM_MULTI | TM_AUTO_CMD12; 353 } 354 uint32_t r[4]; 355 int rc = command(c, idx, (uint32_t)addr, R1, tm, count, r); 356 for (uint32_t b = 0; !rc && b < count; b++) { 357 int w = wait_int(c, is_read ? INT_BUF_RD : INT_BUF_WR, c->cfg.timeout_ms); 358 if (w) { 359 rc = fail(c, (int)idx, w == -2 ? SDHCI_ETIMEOUT : SDHCI_EDATA); 360 break; 361 } 362 uint8_t *p = buf + (uint64_t)b * BLOCK; 363 for (uint32_t i = 0; i < BLOCK; i += 4) { 364 if (is_read) { 365 uint32_t v = rd(c, SDHCI_R_BUF); 366 p[i] = (uint8_t)v; 367 p[i + 1] = (uint8_t)(v >> 8); 368 p[i + 2] = (uint8_t)(v >> 16); 369 p[i + 3] = (uint8_t)(v >> 24); 370 } else { 371 wr(c, SDHCI_R_BUF, (uint32_t)p[i] | (uint32_t)p[i + 1] << 8 | (uint32_t)p[i + 2] << 16 | 372 (uint32_t)p[i + 3] << 24); 373 } 374 } 375 } 376 if (!rc) { 377 int w = wait_int(c, INT_XFER_DONE, c->cfg.timeout_ms); 378 if (w) 379 rc = fail(c, (int)idx, w == -2 ? SDHCI_ETIMEOUT : SDHCI_EDATA); 380 } 381 if (!rc && !is_read) rc = wait_tran(c); /* GUARD:sdhci-tran */ 382 if (rc) { 383 line_reset(c, RST_CMD | RST_DATA); 384 c->ready = 0; /* fail closed: the Store sees DISK_ESTATE from now on */ /* GUARD:sdhci-failstop */ 385 return DISK_EIO; 386 } 387 return DISK_OK; 388 } 389 390 int sdhci_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf) 391 { 392 return transfer(ctx, lba, count, buf, 1); 393 } 394 395 int sdhci_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf) 396 { 397 return transfer(ctx, lba, count, (uint8_t *)buf, 0); 398 } 399 400 int sdhci_flush(void *ctx) 401 { 402 sdhci_card *c = ctx; 403 if (!c) 404 return DISK_EARG; 405 if (!c->ready) 406 return DISK_ESTATE; 407 uint32_t st = 0; 408 if (card_status(c, &st) || !(st & R1_READY_FOR_DATA) || R1_STATE(st) != STATE_TRAN) { 409 c->ready = 0; 410 return DISK_EIO; 411 } 412 return DISK_OK; 413 } 414 415 const char *sdhci_strerror(int rc) 416 { 417 switch (rc) { 418 case SDHCI_OK: return "ok"; 419 case SDHCI_EARG: return "bad argument"; 420 case SDHCI_EVER: return "controller spec 1.00 unsupported"; 421 case SDHCI_ERESET: return "software reset did not clear"; 422 case SDHCI_ENOCARD: return "no card present"; 423 case SDHCI_EVOLT: return "no 3.3 V support"; 424 case SDHCI_ECLOCK: return "clock: no base clock, divider out of range, or not stable"; 425 case SDHCI_ECARD: return "CMD8 failed: not a physical layer 2.00+ card"; 426 case SDHCI_ETIMEOUT: return "timeout"; 427 case SDHCI_ECSD: return "CSD unsupported"; 428 case SDHCI_ECMD: return "command error interrupt"; 429 case SDHCI_EDATA: return "data error interrupt"; 430 case SDHCI_ESTATUS: return "card status error bits"; 431 case SDHCI_ESTATE: return "card not ready (failed closed)"; 432 default: return "unknown"; 433 } 434 }