Controller bring-up
lib/k1_ddr_init.c holds the building blocks that k1_ddr_boot()
sequences: clocks, Device_Init, the MC reset, the address map and
mode-register writes. The DFC part of the same file is described in DFC table and hardware freq. change.
The register offsets and constants live in src/lib/k1_ddrt_regs.h and
src/lib/k1_ddr_tables.c; hardware verification across both reference
boards is in logsRefs/020-eeprom-cs-num-16gib/readme.md,
logsRefs/021-eeprom-cs-num-8gib/readme.md,
logsRefs/026-indep-1333-vco-8gib/readme.md,
logsRefs/027-indep-1333-vco-16gib/readme.md and
logsRefs/029-fallback-2666-to-2400-16gib/readme.md.
Hardware context
struct k1_ddr_hw k1_ddr_hw_init(const struct k1_ddr_boot_cfg *cfg,
const struct k1_ddrt_env *env);
Returns { ddr, phy = ddr + 0x40000, pmu, win = dfc_win, cs_num,
cs_sel = (cs_num == 1 ? 1 : 3), type, io, env }, where io points to
k1_io_lpddr4x or k1_io_lpddr4 (Data tables). Every other function in
the file takes this structure.
Timing constants
constant |
value |
used for |
|---|---|---|
|
1 000 µs |
PLL lock wait |
|
10 000 µs |
SW-FC and HW-DFC busy waits |
|
10 000 µs |
SR exit after a stuck DFC |
|
10 000 µs |
DFI |
|
100 µs |
each half of the MC reset pulse |
|
2 µs |
after ZQCAL START |
|
1 µs |
after ZQCAL LATCH |
|
1 µs |
after every MR write |
k1_ddr_pll_init() — PLLs, SW-FC to fp0, MC clock
Logs the PMU words found at entry (
cc_ap,hw_slp,clksel, both PLL1 words,mck), for the record.CLKSEL: clears the PLL1/PLL2 enable bits (8, 9). A previous attempt may have left the PLLs locked; the VCO words cannot be rewritten until then.
PLL1:
data_rate == 2666or1333: writesPLL1_CTL1 = 0x203c5555,PLL1_CTL2 = 0x50dd6538(1333 parks at fp0, so this VCO is ≈1333 MT/s);anything else: read-modify-write of
PLL1_CTL1[15:8] = 0x3B. IfPLL1_CTL2is not the BootROM analog0x50dd6432(a previous 2666 attempt left0x50dd6538), both BootROM PLL1 words are restored first (CTL1 = 0x2a200000,CTL2 = 0x50dd6432) and then the RMW runs. RMW of the 2666 leftover would leaveCTL1 = 0x203c3b55.
PLL2: writes
0x20238000/0x50dd632c.CLKSEL: sets enable bits 8, 9 and 11.
Waits for both lock bits (
CLKSEL & 0x30000), 1 ms →-ETIMEDOUT, steppll-lock.Writes CLKSEL =
k1_clksel[0](0x3b50),HW_SLP = 0x40600400, sets the SW-FC kick (bit 26 ofCC_AP, read-modify-write) and waits for it to clear, 10 ms →-ETIMEDOUT, stepsw-fc.Sets
MCK_CTRL[1:0] = 3(MC clock on).Logs the resulting CLKSEL / HW_SLP / MCK and PLL1 words.
No MC register is read or written before the MC clock enable (the test model flags any access to an unclocked MC; a test asserts zero MC writes when the PLL never locks).
Error injection: with -DK1_EXP_PLL_NO_LOCK the enable bits are left clear,
so the lock wait times out.
k1_ddr_device_init() — Device_Init
int k1_ddr_device_init(const struct k1_ddr_hw *hw, bool x8,
enum k1_ddr_step *step);
Three parts, then a verification.
k1_mc_program()
Writes the 20 global words of
k1_mc_globalsin table order.Reads
CTL0back. If it does not hold the value just written, the MC has no clock or is still in reset: returns-EIO(stepmc-program) before doing anything else.Read-modify-write of
ddr+0x2c8bit 17 withio->rx_pu_cal.Fills the four timing banks, from fp3 down to fp0. For each bank it writes
CTL_FREQ = k1_freq_bank(n)(both fp fields set ton), then every row ofk1_mc_timing(byte roles: MC timing: accepted ranges and optima):normal rows: the value of column
n;IO rows (
+0x30c,+0x310,+0x314): computed byk1_write_io()from the IO configuration (below);LPDDR4 only:
ddr+0x400 = k1_mc_wdqs[n].
If
x8: a second loop over fp3..fp0 writes the two overlay words ofk1_mc_x8_overlayinto each bank (after all timings, before fp0 is selected).Read-modify-write of
ddr+0x308[27:20] = 0x10.Selects fp0: clears
CTL_FREQ[31:28];PI_CFG |= 0.Writes the three refresh words.
k1_write_io():
register |
seed |
read-modify-write |
|---|---|---|
|
|
keep |
|
|
keep |
k1_phy_analog()
PMU:
PHY_BIAS[15:8] = 0x0F; LDO: LPDDR4X sets[11:10] = 3; LPDDR4 sets[11:10] = 2then[13:12] = 3.For each of the four slices: COMMON A and B
+0written 0, 0, then 1, 1 (both zeroed before either is armed); then for each COMMON:+0x4: keep0xffff0fff, set bits 11, 13, 15;+0xc[15:8]:(tx_pdrv << 5) | (tx_ndrv << 2);+0xc[21:16]:(rx_odt << 3) | rx_odt;+0x4[31:16]:rx_vrefin both bytes;+0x14: keep0xff9fffef, set3 << 21;+0x10: set bit 28.
phy+0x3030 = 0x1077(slice 0, COMMON A only).slice+0x64 = 0x4349in each slice (byte 0 of CS0 only).phy+0x10024 = 0,phy+0x10000 |= 1.
k1_dram_start()
PI_CMD = 0x10000001 | cs_sel << 24(DFIinit_start); waits forPI_STATUSbit 31, 10 ms →-ETIMEDOUT, stepdfi-init.PI_CMD = 0x10000100 | cs_sel << 24(clear).USER_0 = 0x10000001 | cs_sel << 24(JEDEC init); waits for STATUS0x01(1 rank) or0x11(2 ranks), 10 ms →-ETIMEDOUT, stepdram-init.MR writes of
k1_mr_init1(MR1, MR2, MR13, MR3, MR22).ZQCAL per rank, through USER_0 with a single-rank selector (
1 << cs0in the CS byte): START, 2 µs, LATCH, 1 µs.MR writes of
k1_mr_init2(MR12, MR14, MR11, MR23).
Verification
After the three parts, k1_ddr_device_init() reads CTL_FREQ, PI_CFG,
PHY_INIT, STATUS, PI_STATUS and COMMON+0xc, logs them, and:
returns
-EIO(stepmc-program) ifCTL_FREQ != k1_freq_bank(0)orPI_CFG[3:2] != 0;only warns if
PHY_INIT != 0x100.
k1_ddr_mc_reset()
Clears bit 21 of HW_SLP, waits 100 µs, sets it, waits 100 µs. The caller
must run a full Device_Init afterwards; the PLL configuration is kept.
k1_ddr_mr_write()
void k1_ddr_mr_write(const struct k1_ddr_hw *hw, uint32_t mr);
CA_CMD = 0x10020000 | cs_sel << 24 | (mr & 0xff), then a 1 µs delay. There
is no completion bit to wait for; the data comes from the active timing bank.
Used for the Device_Init batches and for MR21 (mr = 0x95) on 16 GiB parts.
Address map
k1_ddr_size_code()
Maps the size of one rank in MB to the CS-map size code: 512 → 0x0d,
768 → 0x01, 1024 → 0x0e, 1536 → 0x02, 2048 → 0x0f,
3072 → 0x03, 4096 → 0x10, 8192 → 0x11, 16384 → 0x12; anything
else → 0 (no code).
k1_ddr_geometry()
int k1_ddr_geometry(uint8_t mr8, struct k1_ddr_geom *g);
io = MR8[7:6]: only 0 (x16) and 1 (x8) are accepted.dens = MR8[5:2]is looked up in an internal table of x16 per-channel sizes and row counts: 0 → 256 MB / ROW_14, 1 → 384 / 15, 2 → 512 / 15, 3 → 768 / 16, 4 → 1024 / 16, 5 → 1536 / 17, 6 → 2048 / 17.x8 doubles the size and adds one row.
area_mb = 2 × (channel MB << io)(two JEDEC channels per rank).Returns
-EINVALforio > 1, a density not in the table (including code 12, 1 Gb) or an area without a size code.
Examples (from the tests): 0x52 → x8, 4096 MB/rank, code 0x10, ROW_17;
0x18 → x16, 4096 MB, ROW_17; 0x58 → x8, 8192 MB, code 0x11, ROW_18;
0x54 (x8 12 Gb, 6144 MB) → -EINVAL.
k1_ddr_map_apply()
start = area_mb >> 3 (CS1 start, 8 MB units)
CS0_MAP: keep 0x0060ffff, set code << 16 | present
CS1_MAP: keep 0x0060fffe, set (start & 0x1ff) << 23 | code << 16
| present only when cs_num == 2
CS1_HI = start >> 9
CS0_GEOM, CS1_GEOM: keep 0xfffff00c, set (row - 10) << 8 | COL_10 | BANK_8
With one rank the CS1 start and size are still written but the present bit is cleared.
Error-injection hooks in this file
flag |
effect |
|---|---|
|
does not re-enable PLL1/PLL2; the lock wait times out |