Architecture
The firmware is a small freestanding C23 program, split into four layers; each layer calls only the layers below it.
blob/ entry table, ABI glue, logging sink, vsnprintf, mem* (image only)
start.S blob.c fmt.c blob.ld
------------------------------------------------------------------------
lib/k1_ddr_boot.c boot sequence: which step, in which order
lib/k1_ddr_accept.c PI-harvest range checks and UART dumps (informational)
lib/k1_ddr_eye.c independent WL/Vref/delay from a memory pattern
------------------------------------------------------------------------
lib/k1_ddr_init.c PLL, SW-FC, Device_Init, MC reset, map, DFC (hw drivers)
lib/k1_ddrt.c one training pass, MR read, wait primitives
------------------------------------------------------------------------
lib/k1_ddr_tables.c register values, command flows, IO configs (data)
lib/k1_ddrt_regs.h register offsets, fields, magic values
lib/k1_ddrt_io.h k1_r32 / k1_w32 / k1_rmw32 (real or mock)
lib/ and include/ are the portable part: they compile both into the
RISC-V image and natively against the test model. blob/ exists only in the
image.
Files and public API
File |
Role |
Public API |
|---|---|---|
|
Training-pass interface |
|
|
Full bring-up interface |
|
|
Building blocks used by the boot sequence and the tests |
|
|
Table types and declarations |
|
|
Range checks and printing |
|
|
Independent data-eye (production path with |
|
|
Shared helpers |
|
|
Register map as |
|
|
MMIO accessors |
|
Call flow of a cold boot
SPL --jump 0xc082d000--> _start (start.S)
k1_blob_ddr_init (blob.c) builds env + cfg
k1_ddr_boot_fallback
[prepare observer]
k1_ddr_boot (k1_ddr_boot.c)
k1_ddr_hw_init
k1_ddr_pll_init PLL1/PLL2, SW-FC fp0, MCK
k1_ddr_device_init MC, PHY analog, DFI, JEDEC, MR, ZQ
k1_boot_pass(0) -> k1_ddrt_train_pass
k1_ddrt_mr_read MR5, MR8
k1_ddr_geometry
[x8] k1_ddr_mc_reset + k1_ddr_device_init(x8)
k1_ddr_map_apply
k1_ddr_dfc_table_load
k1_boot_pass(0)
for fp in plan: k1_boot_dfc(fp) + k1_boot_pass(fp)
[16 GiB] k1_ddr_mr_write(MR21)
k1_boot_dfc(final fp)
[after: print MRs, independent eye at the live fp]
on retryable fail: MC reset, next rate, loop
After every pass the boot sequence calls the optional on_pass observer; a
non-zero return fails that pass. The image uses it to print the pass and,
depending on K1_INDEP_EYE, either the PI-harvest range checks or the
independent eye (Entry points and runtime glue). The boot order and hardware steps match
those exercised on silicon in logsRefs/020-eeprom-cs-num-16gib/readme.md and
logsRefs/021-eeprom-cs-num-8gib/readme.md.
Data types
Configuration and environment
Type |
Fields |
Used by |
|---|---|---|
|
|
one training pass, MR reads |
|
|
full bring-up |
|
|
everything that waits or logs |
|
|
|
The environment is the only way the library reaches the outside world:
now_usis a monotonic microsecond clock. Without it, waits are bounded only by a poll count and delays are no-ops.k1_ddr_boot()refuses to run without it (resets, ZQ and MR writes are timed).vlogis a printf-like sink;nullptrdiscards all output.poll_limitcaps the number of status reads per wait (default 100 000 000); it is the safety net when no clock is available.verboseadds one log line per Vref step and rank.
Results
Type |
Content |
|---|---|
|
everything a pass produced: rc, failing stage and rank, winning Rx index and MR14, PHY status, last PI status, per-step minima (all ranks and per rank), STATUS around SR, PI statistics per stage, final Vref words, WL/gate/RX/TX/DM taps per rank and byte, per-stage durations |
|
per stage: operations issued, “stale first read” count, max polls, max µs |
|
rc, failing step, final fp, MR5/MR8, byte mode, size, up to 5 pass summaries, STATUS/CTL_FREQ/CLKSEL on return, total µs |
|
per pass: rc, pp, Rx index, MR14, failing stage, PHY status, gate code of CS0 byte 0 |
Error model
All functions return an int from enum k1_ddrt_err:
Code |
Value |
Meaning in this code |
|---|---|---|
|
0 |
success |
|
−5 |
hardware answered but wrongly: gate timeout or untrained code,
|
|
−22 |
bad arguments, or an MR8 the address map does not support |
|
−38 |
reserved / unimplemented operation (never retried by rate fallback) |
|
−110 |
a wait expired (PLL lock, SW-FC, DFI init, JEDEC init, SR, PI op, MR read, DFC) |
Where it failed is recorded separately:
enum k1_ddrt_stage(inside a pass):args,sr-enter,write-leveling,read-gate,read-training,write-training,sr-exit,phy-status,mode-register.enum k1_ddr_step(in the boot):args,pll-lock,sw-fc,mc-program,dfi-init,dram-init,training,mr-read,address-map,dfc.
Both have *_name() functions used in the log.
Design principles
No writable static data. The loader copies a raw image and never initializes
.dataor.bss; the linker script asserts both are empty. All state lives in stack structures passed by pointer (struct k1_pass,struct k1_boot,struct k1_ddr_hw). Constant tables areconstand land in.rodata.Freestanding.
-ffreestanding -fno-builtin, no libc: the image provides its ownmemset/memcpy/memmoveand avsnprintfsubset (Entry points and runtime glue).Data separated from code. Every register value that comes from the specification lives in
k1_ddr_tables.cor as a named constant ink1_ddrt_regs.h; the code only sequences them. This is what letsdump_tablesprint the values for comparison with the specification (Data tables).Addresses relative to bases. MC and PHY addresses derive from the
ddrargument; only the PMU base, the DFC window andmtimeare fixed inblob.c. The tests run the whole library at the same addresses against a model, and one test checks that nothing depends on0xc0000000.One MMIO choke point. All register traffic goes through
k1_r32/k1_w32. In the image they are volatile accesses wrapped infence iorw, iorw; with-DK1_DDRT_MOCKthey call the test model instead (Testing).Every wait is bounded twice: by a time budget (when
now_usexists) and by a poll cap.Fail fast, report precisely. A failing step returns immediately with its stage/step recorded; cleanup that protects the DRAM still runs (self-refresh exit after a failed pass, SR exit after a stuck DFC).
C23.
constexpr,nullptr,[[nodiscard]], enums with fixed underlying types, digit separators,static_asserton table layouts.