interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / disk / disk_sdhci.h

141 lines · 6452 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/*
18 * disk_sdhci.h -- AIENOS freestanding SD card driver over an SD Host
19 * Controller (SDHCI) (C, no libc, no heap). Raspberry Pi 5 microSD slot
20 * (Broadcom brcm,bcm2712-sdhci, Linux bcm2712.dtsi) and QEMU sdhci-pci.
21 *
22 * Polled, programmed I/O only (Buffer Data Port 0x20): no DMA, so the device
23 * never writes memory and no IOMMU window is needed. One command at a time.
24 *
25 * Register map: SD Host Controller Simplified Specification, as Linux
26 * drivers/mmc/host/sdhci.h names it. Card protocol: SD Physical Layer
27 * Simplified Specification (CMD0, CMD8, ACMD41, CMD2, CMD3, CMD9, CMD7, ACMD6,
28 * CMD16, CMD17/18, CMD24/25, CMD12, CMD13).
29 *
30 * Every register access is a naturally aligned 32-bit read or write: some
31 * Broadcom SDHCI blocks refuse 8/16-bit writes, so TRANSFER_MODE+COMMAND
32 * (0x0C) and BLOCK_SIZE+BLOCK_COUNT (0x04) are written as one word each, and
33 * the byte registers at 0x28 and 0x2C are updated read-modify-write (never
34 * while a SOFTWARE_RESET bit is still set).
35 *
36 * The caller provides sdhci_ops: 32-bit MMIO read/write at a byte offset from
37 * the controller base, and a bounded delay in microseconds.
38 *
39 * Bring-up (sdhci_init), each refusal its own SDHCI_E* code:
40 * - HOST_VERSION spec 2.00 or later (1.00 controllers unsupported). EVER
41 * - SOFTWARE_RESET ALL clears within 100 ms. ERESET
42 * - a card is present (PRESENT_STATE bit 16) within 100 ms. ENOCARD
43 * - capabilities offer 3.3 V; bus power is 3.3 V only (no UHS 1.8 V
44 * signalling, no voltage switch). EVOLT
45 * - base clock from cfg.base_clock_hz, else CAPABILITIES (bits 13:8 on
46 * 2.00, 15:8 on 3.00+, MHz); zero from both refuses. ECLOCK
47 * - identification at <= 400 kHz, then default speed <= 25 MHz (no CMD6
48 * high speed), internal clock stable within 100 ms. ECLOCK
49 * - CMD8 echo 0x1AA (physical layer 2.00+ card; v1 cards refused). ECARD
50 * - ACMD41 ready within 1 s; CCS selects block addressing (SDHC/SDXC) or
51 * byte addressing (SDSC, CMD16 sets 512-byte blocks). ETIMEOUT
52 * - CSD structure 1 (SDHC/SDXC: (C_SIZE + 1) * 1024 blocks) or 0 (SDSC:
53 * (C_SIZE + 1) * 2^(C_SIZE_MULT + 2) * 2^READ_BL_LEN / 512). ECSD
54 * - 4-bit bus through ACMD6 when cfg.bus_4bit (else 1-bit).
55 * Every command: CMD_INHIBIT (and DATA_INHIBIT for data or busy) clear before
56 * issue; command complete, data ready and transfer complete each bounded
57 * (cfg.timeout_ms, default 1000 ms); any error interrupt bit fails the command
58 * (ECMD / EDATA) after CMD and DATA line resets. R1 card status error bits
59 * (31:19, 13) fail the command too (ESTATUS).
60 * Writes: transfer complete, then CMD13 until READY_FOR_DATA and state "tran"
61 * (programming finished). sdhci_flush therefore has nothing in flight; it
62 * still checks the card is in "tran" and ready. The SD cache (CMD48/49) is
63 * never enabled, so an acknowledged write is on the card.
64 */
65#ifndef AIENOS_DISK_SDHCI_H
66#define AIENOS_DISK_SDHCI_H
67#include <stdint.h>
68#include "disk.h"
69
70typedef struct {
71 void *ctx;
72 uint32_t (*r32)(void *ctx, uint32_t off);
73 void (*w32)(void *ctx, uint32_t off, uint32_t v);
74 void (*delay_us)(void *ctx, uint32_t us);
75} sdhci_ops;
76
77typedef struct {
78 uint32_t base_clock_hz; /* 0: from CAPABILITIES */
79 uint32_t timeout_ms; /* per command / data phase; 0: 1000 */
80 int bus_4bit; /* 1: switch to 4-bit after select */
81 uint32_t max_blocks; /* per multi-block transfer, 1..65535; 0: 128 */
82} sdhci_cfg;
83
84enum {
85 SDHCI_OK = 0,
86 SDHCI_EARG = -20,
87 SDHCI_EVER = -21,
88 SDHCI_ERESET = -22,
89 SDHCI_ENOCARD = -23,
90 SDHCI_EVOLT = -24,
91 SDHCI_ECLOCK = -25,
92 SDHCI_ECARD = -26,
93 SDHCI_ETIMEOUT = -27,
94 SDHCI_ECSD = -28,
95 SDHCI_ECMD = -29,
96 SDHCI_EDATA = -30,
97 SDHCI_ESTATUS = -31,
98 SDHCI_ESTATE = -32,
99};
100
101typedef struct {
102 sdhci_ops ops;
103 sdhci_cfg cfg;
104 int ready; /* init succeeded and no failure since */
105 uint32_t spec; /* HOST_VERSION spec field: 1 = 2.00, 2 = 3.00, ... */
106 uint32_t base_hz; /* base clock used */
107 uint32_t card_hz; /* SD clock after init */
108 uint16_t rca;
109 int high_capacity; /* CCS: block addressing */
110 uint64_t blocks; /* 512-byte blocks */
111 uint32_t cid[4], csd[4];
112 uint32_t last_int; /* INT_STATUS seen by the last failure (diagnostics) */
113 int last_cmd; /* command index of the last failure, -1 none */
114 int last_err; /* SDHCI_E* of the last failure, 0 none */
115 disk_dev dev;
116} sdhci_card;
117
118/* Reset the controller, power and clock it, identify and select the card.
119 * On success c->dev is a disk_dev (block_size 512) over the whole card. */
120int sdhci_init(sdhci_card *c, const sdhci_ops *ops, const sdhci_cfg *cfg);
121/* disk_dev backend entry points (c->dev uses them; ctx is the sdhci_card). */
122int sdhci_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf);
123int sdhci_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf);
124int sdhci_flush(void *ctx);
125const char *sdhci_strerror(int rc);
126
127/* Register offsets and bits used (exported for the software model). */
128#define SDHCI_R_BLOCK 0x04 /* BLOCK_SIZE (15:0) | BLOCK_COUNT (31:16) */
129#define SDHCI_R_ARG 0x08
130#define SDHCI_R_CMD 0x0C /* TRANSFER_MODE (15:0) | COMMAND (31:16) */
131#define SDHCI_R_RESP0 0x10
132#define SDHCI_R_BUF 0x20
133#define SDHCI_R_PRESENT 0x24
134#define SDHCI_R_HOST 0x28 /* HOST_CONTROL 7:0, POWER_CONTROL 15:8 */
135#define SDHCI_R_CLOCK 0x2C /* CLOCK_CONTROL 15:0, TIMEOUT 23:16, SOFTWARE_RESET 31:24 */
136#define SDHCI_R_INT 0x30
137#define SDHCI_R_INT_EN 0x34
138#define SDHCI_R_SIG_EN 0x38
139#define SDHCI_R_CAP 0x40
140#define SDHCI_R_VERSION 0xFC /* HOST_VERSION is bits 31:16 of the word at 0xFC */
141#endif