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 / kernel / dev / pi5_net.h
| 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 | /* pi5_net.h -- Raspberry Pi 5 network lane, cut 2: (A) hardware confinement of the |
| 18 | * BCM2712 PCIe2 inbound DMA window to the ADR 0020 bounce region, (B) RP1 Ethernet |
| 19 | * bring-up as far as the PHY: RP1 clocks the GEM needs, PHY reset on RP1 GPIO32, |
| 20 | * MDIO read of the PHY ID. NO frames, NO DMA descriptors, NO GEM transmit/receive. |
| 21 | * |
| 22 | * The decision and register-sequence logic here is hosted-testable over fake |
| 23 | * registers (tests/test_pi5_net.c, with mutants). The MMIO glue at the bottom of |
| 24 | * pi5_net.c is the CK_PI5_NET image only. A host test proves the decisions, NOT the |
| 25 | * hardware. Every number below cites its source; anything not read is UNVERIFIED. |
| 26 | * |
| 27 | * SOURCES (all read in this cut): |
| 28 | * [E1] edk2-platforms 8e1779b (aien-dev/rpi5-uefi v0.3 c1ca184 pins it), |
| 29 | * Silicon/Broadcom/Bcm27xx/Library/Bcm2712PciHostBridgeLib/Bcm2712PciHostBridge.c |
| 30 | * PcieEncodeInboundSize + PcieSetupInboundWindow (size code, LO/HI, REMAP_LO/HI), |
| 31 | * PcieSetupOutboundWindow (MEM_WIN_LO/HI, BASE_LIMIT, BASE_HI, LIMIT_HI). |
| 32 | * [E2] Silicon/Broadcom/Bcm27xx/Include/IndustryStandard/Bcm2712Pcie.h register offsets |
| 33 | * and Include/IndustryStandard/Bcm2712.h BCM2712_BRCMSTB_PCIE2_CPU_MEM_BASE 0x1f00000000; |
| 34 | * RpiPlatformDxe/ConfigTable.h PCI_RESERVED_MEM32_BASE 0xC0000000 (bus) size 1 GiB. |
| 35 | * [E3] Silicon/RaspberryPi/RpiSiliconPkg/Include/Rp1.h block offsets inside RP1 BAR1. |
| 36 | * [L1] raspberrypi/linux rpi-6.12.y drivers/pci/controller/pcie-brcmstb.c |
| 37 | * set_inbound_win_registers (LO write with size code, HI, remap with ACCESS_EN bit 0). |
| 38 | * [L2] drivers/clk/clk-rp1.c (register offsets, CLK_CTRL_ENABLE bit 11, AUXSRC bits 9:5, |
| 39 | * PLL fields, PLL_SEC divider table, RP1_CLK_ETH / RP1_CLK_ETH_TSU / RP1_CLK_SYS). |
| 40 | * [L3] arch/arm/boot/dts/broadcom (rp1.dtsi, bcm2712-rpi-5-b.dts): rp1_eth clocks pclk/hclk |
| 41 | * = RP1_CLK_SYS, tsu_clk = RP1_CLK_ETH_TSU 50 MHz, tx_clk = RP1_CLK_ETH 125 MHz, |
| 42 | * phy-reset-gpios = rp1_gpio 32 GPIO_ACTIVE_LOW, phy-reset-duration 5 ms, phy reg 1. |
| 43 | * [L4] drivers/pinctrl/pinctrl-rp1.c (GPIO CTRL/PAD/RIO layout, bank table, FSEL gpio = 5). |
| 44 | * [L5] drivers/net/ethernet/cadence/macb_main.c + macb.h (MAN frame, NSR.IDLE, NCR.MPE, |
| 45 | * GEM MDC divider table, MID register). |
| 46 | * [L6] drivers/net/phy/broadcom.c + include/linux/brcmphy.h PHY_ID_BCM54213PE 0x600d84a2, |
| 47 | * phy_id_mask 0xffffffff. |
| 48 | * [D1] RP1 peripherals datasheet RP-008370-DS-1: section 2.2 (PCIe outbound direct mapped |
| 49 | * space), chapter 7 (GEM_GXL, RGMII, ETH_CFG registers). */ |
| 50 | #ifndef AIENOS_CK_PI5_NET_H |
| 51 | #define AIENOS_CK_PI5_NET_H |
| 52 | #include <stddef.h> |
| 53 | #include <stdint.h> |
| 54 | #include "pi5_pcie.h" |
| 55 | |
| 56 | enum { |
| 57 | CK_PI5NET_OK = 0, |
| 58 | CK_PI5NET_E_ARG = -1, |
| 59 | CK_PI5NET_E_RANGE = -2, /* window cannot be expressed by the hardware */ |
| 60 | CK_PI5NET_E_QUIESCE = -3, /* RP1 bus mastering did not read back as off */ |
| 61 | CK_PI5NET_E_READBACK = -4, /* a register did not read back what was written */ |
| 62 | CK_PI5NET_E_TIMEOUT = -5, |
| 63 | CK_PI5NET_E_NOMAP = -6 /* BAR/window address could not be derived */ |
| 64 | }; |
| 65 | |
| 66 | /* ============ A. inbound window confinement ============ */ |
| 67 | |
| 68 | /* Registers (offset from the RC base) [E2] Bcm2712Pcie.h:90-185. Window 0 is BAR2. */ |
| 69 | #define CK_PI5NET_IB0_LO 0x4034u |
| 70 | #define CK_PI5NET_IB0_HI 0x4038u |
| 71 | #define CK_PI5NET_IB0_REMAP_LO 0x40b4u |
| 72 | #define CK_PI5NET_IB0_REMAP_HI 0x40b8u |
| 73 | #define CK_PI5NET_REMAP_ENABLE 0x1u |
| 74 | #define CK_PI5NET_REMAP_LO_MASK 0xfffff000u |
| 75 | #define CK_PI5NET_REMAP_HI_MASK 0xffu /* CPU address is 40 bits */ |
| 76 | #define CK_PI5NET_PCI_COMMAND 0x04u /* config dword 1: status[31:16] | command[15:0] */ |
| 77 | #define CK_PI5NET_CMD_BME 0x0004u /* bus master enable */ |
| 78 | |
| 79 | /* The smallest hardware-expressible window that contains [b_cpu, b_cpu+b_size). |
| 80 | * Bus address == CPU address (identity), as the firmware's own window 0 is |
| 81 | * (bus 0 -> cpu 0) and as ck_dma_alloc documents for the bounce region. */ |
| 82 | struct ck_pi5net_ibprog { |
| 83 | uint64_t bus_base, cpu_base, size; /* window; bus_base == cpu_base */ |
| 84 | uint32_t size_code; /* the 5-bit code [E1] PcieEncodeInboundSize */ |
| 85 | uint32_t lo, hi, remap_lo, remap_hi; /* register values to write */ |
| 86 | uint64_t extra_bytes; /* size - b_size: reach beyond the bounce region */ |
| 87 | }; |
| 88 | /* Pure. E_ARG for size 0 / overflow; E_RANGE if the window would exceed 64 GiB, |
| 89 | * the CPU address passes 40 bits, or the size has no code. */ |
| 90 | int ck_pi5net_ib_plan(uint64_t b_cpu, uint64_t b_size, struct ck_pi5net_ibprog *p); |
| 91 | /* bytes -> 5-bit size code ([E1]: 4K..32K = 0x1c.., 64K..64G = log2-15); 0 = not expressible */ |
| 92 | uint32_t ck_pi5net_ib_size_code(uint64_t size); |
| 93 | |
| 94 | /* Write a config dword of 01:00.0 through the RC window (INDEX then DATA+reg). */ |
| 95 | int ck_pi5net_cfg_write32(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn, uint32_t reg, |
| 96 | uint32_t v); |
| 97 | /* Clear RP1's PCI bus-master-enable so no new upstream DMA can start, and verify it |
| 98 | * read back off. *saved = the command register as found. Status bits (RW1C) are never |
| 99 | * written back. E_QUIESCE if the readback still shows BME. */ |
| 100 | int ck_pi5net_rp1_quiesce(const struct ck_pi5pcie_io *io, uint16_t *saved); |
| 101 | |
| 102 | /* Saved raw registers of window 0, to put the firmware's window back on failure. */ |
| 103 | struct ck_pi5net_ibsaved { |
| 104 | uint32_t lo, hi, remap_lo, remap_hi; |
| 105 | uint32_t other_lo[CK_PI5PCIE_IB_WINDOWS]; /* index 1.. used */ |
| 106 | }; |
| 107 | /* Reprogram window 0 to `p` and disable every other enabled inbound window. Order: |
| 108 | * window off (size code 0), HI, REMAP, LO (size code, enables last). Everything is read |
| 109 | * back; on any mismatch the saved values are written back and E_READBACK returned. |
| 110 | * `*n_disabled` = how many other windows were switched off. */ |
| 111 | int ck_pi5net_ib_apply(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibprog *p, |
| 112 | struct ck_pi5net_ibsaved *saved, uint32_t *n_disabled); |
| 113 | |
| 114 | /* Plain label for the boot line, from the verdict computed over what the registers read back as. CONFINED = |
| 115 | * reach is exactly the bounce region. PARTIAL = reach is the smallest window the hardware can express (it |
| 116 | * equals `plan`, so the extra bytes are unavoidable) and nothing else. NOT_CONFINED = anything wider. |
| 117 | * BROKEN = the window misses or does not fully cover the bounce region (or no window at all). */ |
| 118 | enum { CK_PI5NET_CONF_CONFINED = 0, CK_PI5NET_CONF_PARTIAL = 1, CK_PI5NET_CONF_NOT_CONFINED = 2, CK_PI5NET_CONF_BROKEN = 3, |
| 119 | CK_PI5NET_CONF_UNKNOWN = 4 }; |
| 120 | int ck_pi5net_confine_label(const struct ck_pi5pcie_dmav *dv, const struct ck_pi5net_ibprog *plan); |
| 121 | const char *ck_pi5net_confine_name(int label); |
| 122 | |
| 123 | /* ============ B0. where RP1 BAR1 is, as the CPU sees it ============ */ |
| 124 | |
| 125 | /* Outbound CPU->PCIe window 0 as programmed by firmware [E1] PcieSetupOutboundWindow. */ |
| 126 | #define CK_PI5NET_OB0_BUS_LO 0x400cu |
| 127 | #define CK_PI5NET_OB0_BUS_HI 0x4010u |
| 128 | #define CK_PI5NET_OB0_BASE_LIMIT 0x4070u |
| 129 | #define CK_PI5NET_OB0_BASE_HI 0x4080u |
| 130 | #define CK_PI5NET_OB0_LIMIT_HI 0x4084u |
| 131 | struct ck_pi5net_obwin { |
| 132 | uint64_t bus_base, cpu_base, size; /* size = limit - base + 1MB granule */ |
| 133 | uint8_t valid; |
| 134 | }; |
| 135 | int ck_pi5net_ob_read(const struct ck_pi5pcie_io *io, struct ck_pi5net_obwin *w); |
| 136 | /* CPU address of a PCI bus address inside the outbound window; 0 when outside. */ |
| 137 | uint64_t ck_pi5net_bus_to_cpu(const struct ck_pi5net_obwin *w, uint64_t bus, uint64_t len); |
| 138 | |
| 139 | /* ============ B1. RP1 blocks and the clocks the GEM needs ============ */ |
| 140 | |
| 141 | /* Offsets inside RP1 BAR1 [E3] Rp1.h. */ |
| 142 | #define CK_RP1_SYSINFO 0x00000u |
| 143 | #define CK_RP1_CLOCKS 0x18000u |
| 144 | #define CK_RP1_PLL_SYS 0x20000u |
| 145 | #define CK_RP1_IO_BANK1 0xd4000u |
| 146 | #define CK_RP1_RIO1 0xe4000u |
| 147 | #define CK_RP1_PADS_BANK1 0xf4000u |
| 148 | #define CK_RP1_ETH 0x100000u |
| 149 | #define CK_RP1_ETH_CFG 0x104000u |
| 150 | #define CK_RP1_BAR1_MAP_BYTES 0x108000u /* through ETH_CFG + 0x4000 */ |
| 151 | |
| 152 | /* Clock registers: offset from CK_RP1_CLOCKS (clk-rp1.c:32-67,119-121) and from CK_RP1_PLL_SYS |
| 153 | * for the PLL (clk-rp1.c:28-33; PLL_SYS_CS is 0x08000 from the clocks base = 0x20000 from BAR1). */ |
| 154 | #define CK_RP1_CLK_SYS_CTRL 0x014u |
| 155 | #define CK_RP1_CLK_SYS_DIV 0x018u |
| 156 | #define CK_RP1_CLK_SYS_SEL 0x020u |
| 157 | #define CK_RP1_CLK_ETH_CTRL 0x064u |
| 158 | #define CK_RP1_CLK_ETH_DIV 0x068u |
| 159 | #define CK_RP1_CLK_ETH_SEL 0x070u |
| 160 | #define CK_RP1_CLK_TSU_CTRL 0x134u |
| 161 | #define CK_RP1_CLK_TSU_DIV 0x138u |
| 162 | #define CK_RP1_CLK_TSU_SEL 0x140u |
| 163 | #define CK_RP1_PLL_CS 0x00u |
| 164 | #define CK_RP1_PLL_FBDIV_INT 0x08u |
| 165 | #define CK_RP1_PLL_FBDIV_FRAC 0x0cu |
| 166 | #define CK_RP1_PLL_PRIM 0x10u |
| 167 | #define CK_RP1_PLL_SEC 0x14u |
| 168 | #define CK_RP1_XOSC_HZ 50000000u /* clk-rp1 DT: XOSC 50 MHz ("Must match the XOSC frequency") */ |
| 169 | #define CK_RP1_CLK_ENABLE (1u << 11) |
| 170 | #define CK_RP1_CLK_AUXSRC_MASK 0x3e0u |
| 171 | #define CK_RP1_CLK_AUXSRC_SHIFT 5 |
| 172 | #define CK_RP1_ETH_HZ 125000000u /* assigned-clock-rates RP1_CLK_ETH */ |
| 173 | #define CK_RP1_TSU_HZ 50000000u /* assigned-clock-rates RP1_CLK_ETH_TSU */ |
| 174 | |
| 175 | /* Raw register snapshot the plan works from. */ |
| 176 | struct ck_rp1_clk_regs { |
| 177 | uint32_t pll_cs, pll_fbdiv_int, pll_fbdiv_frac, pll_prim, pll_sec; |
| 178 | uint32_t sys_ctrl, sys_div, sys_sel; |
| 179 | uint32_t eth_ctrl, eth_div, eth_sel; |
| 180 | uint32_t tsu_ctrl, tsu_div, tsu_sel; |
| 181 | }; |
| 182 | /* Rates in Hz from the snapshot; 0 = off / unlocked / not computable. */ |
| 183 | uint32_t ck_rp1_pll_core_hz(const struct ck_rp1_clk_regs *r); |
| 184 | uint32_t ck_rp1_pll_sec_hz(const struct ck_rp1_clk_regs *r); |
| 185 | uint32_t ck_rp1_pll_pri_hz(const struct ck_rp1_clk_regs *r); |
| 186 | uint32_t ck_rp1_clk_sys_hz(const struct ck_rp1_clk_regs *r); |
| 187 | uint32_t ck_rp1_clk_eth_hz(const struct ck_rp1_clk_regs *r); /* 0 if disabled */ |
| 188 | uint32_t ck_rp1_clk_tsu_hz(const struct ck_rp1_clk_regs *r); |
| 189 | |
| 190 | enum { CK_RP1_CLK_ALREADY_ON = 0, CK_RP1_CLK_TO_ENABLE = 1, CK_RP1_CLK_BLOCKED = 2 }; |
| 191 | struct ck_rp1_clk_plan { |
| 192 | int eth, tsu; /* the three states above */ |
| 193 | uint32_t eth_div, tsu_div; /* DIV_INT to write when TO_ENABLE */ |
| 194 | uint32_t eth_ctrl, tsu_ctrl; /* CTRL value to write (ENABLE set) when TO_ENABLE */ |
| 195 | const char *eth_why, *tsu_why; /* why BLOCKED / how decided */ |
| 196 | }; |
| 197 | /* A clock that is already enabled is left exactly as found (its rate is reported, never |
| 198 | * changed). A disabled clock is programmed to the DT assigned rate only if its source is |
| 199 | * running and gives an exact integer divide; the PLLs are never touched. */ |
| 200 | void ck_rp1_clk_decide(const struct ck_rp1_clk_regs *r, struct ck_rp1_clk_plan *p); |
| 201 | |
| 202 | /* Register access to RP1 BAR1 (offset from BAR1 base). */ |
| 203 | struct ck_rp1_io { |
| 204 | uint32_t (*rd)(void *ctx, uint32_t off); |
| 205 | void (*wr)(void *ctx, uint32_t off, uint32_t v); |
| 206 | void (*delay_us)(void *ctx, uint32_t us); |
| 207 | void *ctx; |
| 208 | }; |
| 209 | void ck_rp1_clk_read(const struct ck_rp1_io *io, struct ck_rp1_clk_regs *r); |
| 210 | /* Program the clocks the plan marks TO_ENABLE (DIV_INT, then CTRL with AUXSRC, then ENABLE) and |
| 211 | * read back. 0 = ok; E_READBACK if ENABLE does not stick. */ |
| 212 | int ck_rp1_clk_apply(const struct ck_rp1_io *io, const struct ck_rp1_clk_plan *p); |
| 213 | |
| 214 | /* ============ B2. PHY reset on RP1 GPIO32 ============ */ |
| 215 | |
| 216 | #define CK_RP1_PHY_RESET_GPIO 32u |
| 217 | #define CK_RP1_PHY_RESET_MS 5u /* [L3] phy-reset-duration */ |
| 218 | #define CK_RP1_PHY_SETTLE_MS 30u /* our own margin after release (PHY datasheet NOT read: UNVERIFIED) */ |
| 219 | /* GPIO32 = bank 1 pin 4 (clk: pinctrl-rp1.c:292-296 bank table { 28, 6, ...}). */ |
| 220 | #define CK_RP1_GPIO32_CTRL (CK_RP1_IO_BANK1 + 4u * 8u + 4u) /* gpio base + j*8 + RP1_GPIO_CTRL */ |
| 221 | #define CK_RP1_GPIO32_PAD (CK_RP1_PADS_BANK1 + 4u + 4u * 4u) /* pads bank offset 0x4004 + j*4 (from 0xf0000) */ |
| 222 | #define CK_RP1_RIO1_OUT (CK_RP1_RIO1 + 0x0u) |
| 223 | #define CK_RP1_RIO1_OE (CK_RP1_RIO1 + 0x4u) |
| 224 | #define CK_RP1_RIO1_IN (CK_RP1_RIO1 + 0x8u) |
| 225 | #define CK_RP1_ATOMIC_SET 0x2000u |
| 226 | #define CK_RP1_ATOMIC_CLR 0x3000u |
| 227 | #define CK_RP1_GPIO_PIN_BIT (1u << 4) /* bit j inside the bank's RIO word */ |
| 228 | #define CK_RP1_FSEL_GPIO 0x05u |
| 229 | #define CK_RP1_PAD_IN_ENABLE 0x40u |
| 230 | #define CK_RP1_PAD_OUT_DISABLE 0x80u |
| 231 | |
| 232 | struct ck_rp1_phyreset { |
| 233 | uint32_t ctrl_before, pad_before, oe_before, out_before, in_before; |
| 234 | uint32_t ctrl_after, pad_after, in_low, in_high; /* in_low: pin read while we drive low */ |
| 235 | }; |
| 236 | /* Drive GPIO32: line high first (no glitch low), pad+function, output enable, then low for |
| 237 | * CK_RP1_PHY_RESET_MS, then high and settle. Reads every register back. 0 = ok; |
| 238 | * E_READBACK if the pin does not read low while driven low or high after release. */ |
| 239 | int ck_rp1_phy_reset(const struct ck_rp1_io *io, struct ck_rp1_phyreset *out); |
| 240 | |
| 241 | /* ============ B3. MDIO read of the PHY ID ============ */ |
| 242 | |
| 243 | #define CK_GEM_NCR 0x000u |
| 244 | #define CK_GEM_NCFGR 0x004u |
| 245 | #define CK_GEM_NSR 0x008u |
| 246 | #define CK_GEM_MAN 0x034u |
| 247 | #define CK_GEM_MID 0x0fcu |
| 248 | #define CK_GEM_NCR_MPE (1u << 4) |
| 249 | #define CK_GEM_NSR_IDLE (1u << 2) |
| 250 | #define CK_GEM_NCFGR_CLK_SHIFT 18 |
| 251 | #define CK_GEM_NCFGR_CLK_MASK (7u << 18) |
| 252 | #define CK_GEM_MDIO_POLLS 2000u |
| 253 | #define CK_GEM_MDIO_POLL_US 10u |
| 254 | |
| 255 | /* GEM MDC divider code from the pclk rate (macb_main.c:2826-2849); 0 pclk -> the slowest. */ |
| 256 | uint32_t ck_gem_mdc_div_code(uint32_t pclk_hz); |
| 257 | /* The C22 read frame the MAN register wants (macb.h MACB_MAN_C22_*). phy, reg < 32. */ |
| 258 | uint32_t ck_gem_mdio_frame_read(uint32_t phy, uint32_t reg); |
| 259 | /* Set NCR.MPE and the MDC divider (read-modify-write, other bits kept). */ |
| 260 | int ck_gem_mdio_enable(const struct ck_rp1_io *io, uint32_t pclk_hz); |
| 261 | /* One clause 22 read. Waits for idle before and after (bounded). 0 = ok, E_TIMEOUT, E_ARG. */ |
| 262 | int ck_gem_mdio_read(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t *val); |
| 263 | |
| 264 | enum { CK_PHY_ABSENT = 0, CK_PHY_BCM54213PE = 1, CK_PHY_BROADCOM_OTHER = 2, CK_PHY_OTHER = 3 }; |
| 265 | #define CK_PHY_ID_BCM54213PE 0x600d84a2u |
| 266 | struct ck_phy_id { |
| 267 | uint32_t id; /* reg2 << 16 | reg3 */ |
| 268 | uint32_t oui; /* 24-bit OUI in IEEE octet order (e.g. 0x18c086), bits 1,2 taken as 0 */ |
| 269 | uint8_t model, rev; |
| 270 | int cls; /* CK_PHY_* */ |
| 271 | }; |
| 272 | void ck_phy_id_decode(uint16_t r2, uint16_t r3, struct ck_phy_id *out); |
| 273 | const char *ck_phy_class_name(int cls); |
| 274 | /* Find the PHY: poll address `addr` (dts: 1) up to `tries` times with `gap_ms` between; the |
| 275 | * first non-0xffff/0x0000 pair wins. If it never answers, scan 0..31 once for diagnostics |
| 276 | * (`*scan_hit` = lowest answering address, -1 if none). *attempts = polls used at `addr`. |
| 277 | * 0 = answered at addr, E_TIMEOUT otherwise. */ |
| 278 | int ck_phy_find(const struct ck_rp1_io *io, uint32_t addr, uint32_t tries, uint32_t gap_ms, struct ck_phy_id *id, |
| 279 | uint32_t *attempts, int *scan_hit); |
| 280 | |
| 281 | #ifdef CK_PI5_NET |
| 282 | /* Boot stage (CK_PI5_NET image only): see pi5_net.c. */ |
| 283 | void ck_pi5net_run(void); |
| 284 | #endif |
| 285 | |
| 286 | #endif |