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 / kernel / dev / pi5_net.c

930 lines · 38214 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/* pi5_net.c -- see pi5_net.h. The decision and register-sequence logic is hosted-testable
18 * (tests/test_pi5_net.c); the MMIO glue at the bottom is the CK_PI5_NET image only. */
19#include "pi5_net.h"
20
21/* TEST-only mutants (make pi5-net-mutants): each removes one decision or one step and must
22 * make test_pi5_net FAIL. Host builds only; never part of an image. */
23#if defined(CK_NET_MUTANT_NO_STRADDLE) || defined(CK_NET_MUTANT_NO_REMAP_EN) || \
24 defined(CK_NET_MUTANT_NO_DISABLE_FIRST) || defined(CK_NET_MUTANT_NO_READBACK) || \
25 defined(CK_NET_MUTANT_NO_RESTORE) || defined(CK_NET_MUTANT_NO_OTHERS) || \
26 defined(CK_NET_MUTANT_NO_STATUS_MASK) || defined(CK_NET_MUTANT_NO_QUIESCE_CHECK) || \
27 defined(CK_NET_MUTANT_OB_NO_LIMIT) || defined(CK_NET_MUTANT_NO_ASSERT) || defined(CK_NET_MUTANT_NO_HOLD) || \
28 defined(CK_NET_MUTANT_NO_RELEASE) || defined(CK_NET_MUTANT_NO_HIGH_FIRST) || \
29 defined(CK_NET_MUTANT_NO_IDLE_AFTER) || defined(CK_NET_MUTANT_NO_MPE) || \
30 defined(CK_NET_MUTANT_TOUCH_ON_CLOCK) || defined(CK_NET_MUTANT_NO_EXACT_DIV) || \
31 defined(CK_NET_MUTANT_NO_CLK_VERIFY) || defined(CK_NET_MUTANT_OUI_ORDER) || \
32 defined(CK_NET_MUTANT_BAD_CLASS) || defined(CK_NET_MUTANT_NO_BOUNDS) || defined(CK_NET_MUTANT_NO_MDC_DIV) || \
33 defined(CK_NET_MUTANT_PARTIAL_ANY)
34#if !__STDC_HOSTED__
35#error "CK_NET_MUTANT_* are host test mutants only"
36#endif
37#endif
38
39#define IB_SIZE_MASK 0x1fu
40#define IB_LO_ADDR_MASK 0xfffff000u
41#define GiB64 (1ull << 36)
42
43/* ---------------- A. inbound window ---------------- */
44
45uint32_t ck_pi5net_ib_size_code(uint64_t size)
46{
47 uint32_t lg = 0;
48 if (size == 0 || (size & (size - 1)) != 0)
49 return 0;
50 while ((1ull << lg) < size)
51 lg++;
52 if (lg >= 12 && lg <= 15)
53 return (lg - 12) + 0x1cu; /* 4 KiB..32 KiB [E1] */
54 if (lg >= 16 && lg <= 36)
55 return lg - 15; /* 64 KiB..64 GiB [E1] */
56 return 0;
57}
58
59int ck_pi5net_ib_plan(uint64_t b_cpu, uint64_t b_size, struct ck_pi5net_ibprog *p)
60{
61 uint64_t blk = 4096, end, base;
62 if (!p)
63 return CK_PI5NET_E_ARG;
64 *p = (struct ck_pi5net_ibprog){0};
65 if (b_size == 0 || b_cpu > UINT64_MAX - b_size)
66 return CK_PI5NET_E_ARG;
67 end = b_cpu + b_size;
68 if (end > (1ull << 40)) /* REMAP_HI holds CPU address bits 39:32 */
69 return CK_PI5NET_E_RANGE;
70 while (blk < b_size && blk <= GiB64)
71 blk <<= 1;
72 for (;;) {
73 if (blk > GiB64)
74 return CK_PI5NET_E_RANGE;
75 base = b_cpu & ~(blk - 1);
76#ifndef CK_NET_MUTANT_NO_STRADDLE
77 if (base + blk < end) { /* aligned block does not reach the end: next size up */
78 blk <<= 1;
79 continue;
80 }
81#endif
82 break;
83 }
84 p->size_code = ck_pi5net_ib_size_code(blk);
85 if (p->size_code == 0)
86 return CK_PI5NET_E_RANGE;
87 p->bus_base = p->cpu_base = base;
88 p->size = blk;
89 p->extra_bytes = blk - b_size;
90 p->lo = ((uint32_t)base & ~IB_SIZE_MASK) | p->size_code;
91 p->hi = (uint32_t)(base >> 32);
92 p->remap_lo = (uint32_t)base & CK_PI5NET_REMAP_LO_MASK;
93#ifndef CK_NET_MUTANT_NO_REMAP_EN
94 p->remap_lo |= CK_PI5NET_REMAP_ENABLE;
95#endif
96 p->remap_hi = (uint32_t)(base >> 32) & CK_PI5NET_REMAP_HI_MASK;
97 return CK_PI5NET_OK;
98}
99
100int ck_pi5net_cfg_write32(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn, uint32_t reg,
101 uint32_t v)
102{
103 if (!io || !io->rd || !io->wr)
104 return CK_PI5NET_E_ARG;
105 if (reg >= 4096u || (reg & 3u) != 0 || fn > 7u)
106 return CK_PI5NET_E_ARG;
107 if (bus == 0) {
108 if (dev != 0 || fn != 0)
109 return CK_PI5NET_E_ARG;
110 io->wr(io->ctx, reg, v);
111 return CK_PI5NET_OK;
112 }
113 if (bus != 1 || dev != 0)
114 return CK_PI5NET_E_ARG;
115 io->wr(io->ctx, CK_PI5PCIE_EXT_CFG_INDEX, ((uint32_t)bus << 20) | ((uint32_t)dev << 15) | ((uint32_t)fn << 12));
116 io->wr(io->ctx, CK_PI5PCIE_EXT_CFG_DATA + reg, v);
117 return CK_PI5NET_OK;
118}
119
120int ck_pi5net_rp1_quiesce(const struct ck_pi5pcie_io *io, uint16_t *saved)
121{
122 uint32_t v, nv;
123 if (!saved)
124 return CK_PI5NET_E_ARG;
125 if (ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v))
126 return CK_PI5NET_E_ARG;
127 *saved = (uint16_t)(v & 0xffffu);
128#ifdef CK_NET_MUTANT_NO_STATUS_MASK
129 nv = v & ~(uint32_t)CK_PI5NET_CMD_BME; /* would write the RW1C status bits back as ones */
130#else
131 nv = (v & 0xffffu) & ~(uint32_t)CK_PI5NET_CMD_BME; /* status half written as zero: RW1C, nothing cleared */
132#endif
133 if (ck_pi5net_cfg_write32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, nv))
134 return CK_PI5NET_E_ARG;
135#ifndef CK_NET_MUTANT_NO_QUIESCE_CHECK
136 if (ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v))
137 return CK_PI5NET_E_ARG;
138 if (v & CK_PI5NET_CMD_BME)
139 return CK_PI5NET_E_QUIESCE;
140#endif
141 return CK_PI5NET_OK;
142}
143
144static uint32_t oth_lo(uint32_t i) { return 0x40d4u + (i - 1) * 8u; } /* windows 1..7 [E2] */
145
146static int ib_matches(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibprog *p)
147{
148#ifdef CK_NET_MUTANT_NO_READBACK
149 (void)io;
150 (void)p;
151 return 1;
152#else
153 uint32_t lo = io->rd(io->ctx, CK_PI5NET_IB0_LO), hi = io->rd(io->ctx, CK_PI5NET_IB0_HI);
154 uint32_t rlo = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_LO), rhi = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_HI);
155 /* bits 11:5 of LO are not part of the window (size code is 4:0, address from bit 12) */
156 return (lo & (IB_LO_ADDR_MASK | IB_SIZE_MASK)) == (p->lo & (IB_LO_ADDR_MASK | IB_SIZE_MASK)) && hi == p->hi &&
157 (rlo & (CK_PI5NET_REMAP_LO_MASK | CK_PI5NET_REMAP_ENABLE)) == p->remap_lo &&
158 (rhi & CK_PI5NET_REMAP_HI_MASK) == p->remap_hi;
159#endif
160}
161
162static void ib_restore(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibsaved *s)
163{
164#ifndef CK_NET_MUTANT_NO_RESTORE
165 uint32_t i;
166 io->wr(io->ctx, CK_PI5NET_IB0_LO, s->lo & ~IB_SIZE_MASK);
167 io->wr(io->ctx, CK_PI5NET_IB0_HI, s->hi);
168 io->wr(io->ctx, CK_PI5NET_IB0_REMAP_LO, s->remap_lo);
169 io->wr(io->ctx, CK_PI5NET_IB0_REMAP_HI, s->remap_hi);
170 io->wr(io->ctx, CK_PI5NET_IB0_LO, s->lo);
171 for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++)
172 io->wr(io->ctx, oth_lo(i), s->other_lo[i]);
173#else
174 (void)io;
175 (void)s;
176#endif
177}
178
179int ck_pi5net_ib_apply(const struct ck_pi5pcie_io *io, const struct ck_pi5net_ibprog *p,
180 struct ck_pi5net_ibsaved *saved, uint32_t *n_disabled)
181{
182 uint32_t i, n = 0;
183 if (!io || !io->rd || !io->wr || !p || !saved || p->size_code == 0)
184 return CK_PI5NET_E_ARG;
185 if (n_disabled)
186 *n_disabled = 0;
187 saved->lo = io->rd(io->ctx, CK_PI5NET_IB0_LO);
188 saved->hi = io->rd(io->ctx, CK_PI5NET_IB0_HI);
189 saved->remap_lo = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_LO);
190 saved->remap_hi = io->rd(io->ctx, CK_PI5NET_IB0_REMAP_HI);
191 saved->other_lo[0] = 0;
192 for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++)
193 saved->other_lo[i] = io->rd(io->ctx, oth_lo(i));
194#ifndef CK_NET_MUTANT_NO_DISABLE_FIRST
195 io->wr(io->ctx, CK_PI5NET_IB0_LO, saved->lo & ~IB_SIZE_MASK); /* window off while its other halves change */
196#endif
197 io->wr(io->ctx, CK_PI5NET_IB0_HI, p->hi);
198 io->wr(io->ctx, CK_PI5NET_IB0_REMAP_LO, p->remap_lo);
199 io->wr(io->ctx, CK_PI5NET_IB0_REMAP_HI, p->remap_hi);
200 io->wr(io->ctx, CK_PI5NET_IB0_LO, p->lo); /* size code last: the window turns on complete */
201#ifndef CK_NET_MUTANT_NO_OTHERS
202 for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++) {
203 if (saved->other_lo[i] & IB_SIZE_MASK) {
204 io->wr(io->ctx, oth_lo(i), saved->other_lo[i] & ~IB_SIZE_MASK);
205 n++;
206 }
207 }
208#endif
209 if (n_disabled)
210 *n_disabled = n;
211 if (!ib_matches(io, p)) {
212 ib_restore(io, saved);
213 return CK_PI5NET_E_READBACK;
214 }
215 for (i = 1; i < CK_PI5PCIE_IB_WINDOWS; i++) {
216 if (io->rd(io->ctx, oth_lo(i)) & IB_SIZE_MASK) {
217#ifndef CK_NET_MUTANT_NO_READBACK
218 ib_restore(io, saved);
219 return CK_PI5NET_E_READBACK;
220#endif
221 }
222 }
223 return CK_PI5NET_OK;
224}
225
226int ck_pi5net_confine_label(const struct ck_pi5pcie_dmav *dv, const struct ck_pi5net_ibprog *plan)
227{
228 if (!dv)
229 return CK_PI5NET_CONF_UNKNOWN;
230 switch (dv->verdict) {
231 case CK_PI5PCIE_DMA_CONFINED:
232 return CK_PI5NET_CONF_CONFINED;
233 case CK_PI5PCIE_DMA_WIDER:
234#ifdef CK_NET_MUTANT_PARTIAL_ANY
235 (void)plan;
236 return CK_PI5NET_CONF_PARTIAL; /* would call a 64 GiB window "partial" */
237#else
238 if (plan && plan->extra_bytes != 0 && dv->outside_bytes == plan->extra_bytes &&
239 dv->inside_bytes + dv->outside_bytes == plan->size)
240 return CK_PI5NET_CONF_PARTIAL;
241 return CK_PI5NET_CONF_NOT_CONFINED;
242#endif
243 case CK_PI5PCIE_DMA_NO_WINDOW:
244 case CK_PI5PCIE_DMA_MISS:
245 case CK_PI5PCIE_DMA_PARTIAL:
246 return CK_PI5NET_CONF_BROKEN;
247 default:
248 return CK_PI5NET_CONF_UNKNOWN;
249 }
250}
251
252const char *ck_pi5net_confine_name(int label)
253{
254 switch (label) {
255 case CK_PI5NET_CONF_CONFINED: return "CONFINED";
256 case CK_PI5NET_CONF_PARTIAL: return "PARTIAL";
257 case CK_PI5NET_CONF_NOT_CONFINED: return "NOT_CONFINED";
258 case CK_PI5NET_CONF_BROKEN: return "BROKEN";
259 default: return "UNKNOWN";
260 }
261}
262
263/* ---------------- B0. outbound window ---------------- */
264
265int ck_pi5net_ob_read(const struct ck_pi5pcie_io *io, struct ck_pi5net_obwin *w)
266{
267 uint32_t bl, bh, lh, bus_lo, bus_hi;
268 uint64_t base_mb, limit_mb;
269 if (!io || !io->rd || !w)
270 return CK_PI5NET_E_ARG;
271 *w = (struct ck_pi5net_obwin){0};
272 bl = io->rd(io->ctx, CK_PI5NET_OB0_BASE_LIMIT);
273 bh = io->rd(io->ctx, CK_PI5NET_OB0_BASE_HI) & 0xffu;
274 lh = io->rd(io->ctx, CK_PI5NET_OB0_LIMIT_HI) & 0xffu;
275 bus_lo = io->rd(io->ctx, CK_PI5NET_OB0_BUS_LO);
276 bus_hi = io->rd(io->ctx, CK_PI5NET_OB0_BUS_HI);
277 base_mb = ((uint64_t)bh << 12) | ((bl >> 4) & 0xfffu);
278 limit_mb = ((uint64_t)lh << 12) | ((bl >> 20) & 0xfffu);
279 w->bus_base = ((uint64_t)bus_hi << 32) | bus_lo;
280 w->cpu_base = base_mb << 20;
281#ifdef CK_NET_MUTANT_OB_NO_LIMIT
282 w->size = (limit_mb - base_mb) << 20; /* off by one granule */
283 w->valid = limit_mb >= base_mb;
284#else
285 w->valid = limit_mb >= base_mb;
286 w->size = w->valid ? (limit_mb - base_mb + 1) << 20 : 0;
287#endif
288 return CK_PI5NET_OK;
289}
290
291uint64_t ck_pi5net_bus_to_cpu(const struct ck_pi5net_obwin *w, uint64_t bus, uint64_t len)
292{
293 if (!w || !w->valid || len == 0)
294 return 0;
295#ifndef CK_NET_MUTANT_NO_BOUNDS
296 if (bus < w->bus_base || len > w->size || bus - w->bus_base > w->size - len)
297 return 0;
298#endif
299 return w->cpu_base + (bus - w->bus_base);
300}
301
302/* ---------------- B1. clocks ---------------- */
303
304static uint32_t div_round_closest(uint64_t a, uint64_t b) { return b ? (uint32_t)((a + (b >> 1)) / b) : 0; }
305
306uint32_t ck_rp1_pll_core_hz(const struct ck_rp1_clk_regs *r)
307{
308 uint64_t v;
309 if (!(r->pll_cs & 0x80000000u)) /* PLL_CS_LOCK [L2] */
310 return 0;
311 v = ((uint64_t)CK_RP1_XOSC_HZ * (((uint64_t)r->pll_fbdiv_int << 24) + r->pll_fbdiv_frac) + (1u << 23)) >> 24;
312 return v > 0xffffffffull ? 0 : (uint32_t)v;
313}
314
315static const uint8_t sec_div_tab[32] = {19, 19, 19, 19, 19, 19, 19, 19, 8, 9, 10, 11, 12, 13, 14, 15,
316 16, 17, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19};
317
318uint32_t ck_rp1_pll_sec_hz(const struct ck_rp1_clk_regs *r)
319{
320 uint32_t core = ck_rp1_pll_core_hz(r), d;
321 if (!core || (r->pll_sec & (1u << 16)) || !(r->pll_sec & 0x80000000u)) /* RST set, or divider not implemented */
322 return 0;
323 d = sec_div_tab[(r->pll_sec >> 8) & 0x1fu];
324 return (uint32_t)(((uint64_t)core + d - 1) / d); /* DIV_ROUND_UP like clk_divider_ops */
325}
326
327uint32_t ck_rp1_pll_pri_hz(const struct ck_rp1_clk_regs *r)
328{
329 uint32_t core = ck_rp1_pll_core_hz(r), d1 = (r->pll_prim >> 16) & 7u, d2 = (r->pll_prim >> 12) & 7u;
330 if (!core || !d1 || !d2)
331 return 0;
332 return div_round_closest(core, (uint64_t)d1 * d2);
333}
334
335static uint32_t clk_div(uint32_t div_reg) { return div_reg ? div_reg : 1u << 16; } /* clk-rp1.c recalc: 0 = 2^16 */
336
337uint32_t ck_rp1_clk_sys_hz(const struct ck_rp1_clk_regs *r)
338{
339 /* SEL is one-hot over the standard parents {xosc, -, pll_sys}; 0 = not switched yet: use CTRL.SRC (clk-rp1.c:1101-1114) */
340 uint32_t sel = r->sys_sel, src, parent;
341 if (sel)
342 src = (uint32_t)__builtin_ctz(sel);
343 else
344 src = r->sys_ctrl & 0x3u;
345 if (src == 0)
346 parent = CK_RP1_XOSC_HZ;
347 else if (src == 2)
348 parent = ck_rp1_pll_pri_hz(r);
349 else
350 return 0; /* aux source: not modelled */
351 return parent / clk_div(r->sys_div);
352}
353
354static uint32_t aux_parent_hz(const struct ck_rp1_clk_regs *r, uint32_t aux, int eth)
355{
356 /* RP1_CLK_ETH aux parents: 0 pll_sys_sec, 1 pll_sys; RP1_CLK_ETH_TSU aux parents: 0 xosc [L2] */
357 if (eth)
358 return aux == 0 ? ck_rp1_pll_sec_hz(r) : aux == 1 ? ck_rp1_pll_pri_hz(r) : 0;
359 return aux == 0 ? CK_RP1_XOSC_HZ : 0;
360}
361
362uint32_t ck_rp1_clk_eth_hz(const struct ck_rp1_clk_regs *r)
363{
364 uint32_t p;
365 if (!(r->eth_ctrl & CK_RP1_CLK_ENABLE))
366 return 0;
367 p = aux_parent_hz(r, (r->eth_ctrl & CK_RP1_CLK_AUXSRC_MASK) >> CK_RP1_CLK_AUXSRC_SHIFT, 1);
368 return p / clk_div(r->eth_div);
369}
370
371uint32_t ck_rp1_clk_tsu_hz(const struct ck_rp1_clk_regs *r)
372{
373 uint32_t p;
374 if (!(r->tsu_ctrl & CK_RP1_CLK_ENABLE))
375 return 0;
376 p = aux_parent_hz(r, (r->tsu_ctrl & CK_RP1_CLK_AUXSRC_MASK) >> CK_RP1_CLK_AUXSRC_SHIFT, 0);
377 return p / clk_div(r->tsu_div);
378}
379
380void ck_rp1_clk_decide(const struct ck_rp1_clk_regs *r, struct ck_rp1_clk_plan *p)
381{
382 uint32_t sec;
383 *p = (struct ck_rp1_clk_plan){0};
384 /* ETH (tx_clk, 125 MHz from pll_sys_sec) */
385#ifndef CK_NET_MUTANT_TOUCH_ON_CLOCK
386 if (r->eth_ctrl & CK_RP1_CLK_ENABLE) {
387 p->eth = CK_RP1_CLK_ALREADY_ON;
388 p->eth_why = "already enabled: left as found";
389 } else
390#endif
391 {
392 sec = ck_rp1_pll_sec_hz(r);
393 if (!sec) {
394 p->eth = CK_RP1_CLK_BLOCKED;
395 p->eth_why = "pll_sys_sec not running (core unlocked, divider in reset or not implemented)";
396#ifndef CK_NET_MUTANT_NO_EXACT_DIV
397 } else if (sec % CK_RP1_ETH_HZ != 0 || sec / CK_RP1_ETH_HZ == 0 || sec / CK_RP1_ETH_HZ > 255u) {
398#else
399 } else if (sec / CK_RP1_ETH_HZ == 0 || sec / CK_RP1_ETH_HZ > 255u) {
400#endif
401 p->eth = CK_RP1_CLK_BLOCKED;
402 p->eth_why = "pll_sys_sec is not an exact multiple (1..255) of 125 MHz";
403 } else {
404 p->eth = CK_RP1_CLK_TO_ENABLE;
405 p->eth_div = sec / CK_RP1_ETH_HZ;
406 p->eth_ctrl = (r->eth_ctrl & ~(uint32_t)CK_RP1_CLK_AUXSRC_MASK) | CK_RP1_CLK_ENABLE; /* aux 0 = pll_sys_sec */
407 p->eth_why = "disabled: enable from pll_sys_sec at 125 MHz";
408 }
409 }
410 /* ETH_TSU (tsu_clk, 50 MHz from xosc) */
411#ifndef CK_NET_MUTANT_TOUCH_ON_CLOCK
412 if (r->tsu_ctrl & CK_RP1_CLK_ENABLE) {
413 p->tsu = CK_RP1_CLK_ALREADY_ON;
414 p->tsu_why = "already enabled: left as found";
415 } else
416#endif
417 {
418 p->tsu = CK_RP1_CLK_TO_ENABLE;
419 p->tsu_div = CK_RP1_XOSC_HZ / CK_RP1_TSU_HZ;
420 p->tsu_ctrl = (r->tsu_ctrl & ~(uint32_t)CK_RP1_CLK_AUXSRC_MASK) | CK_RP1_CLK_ENABLE; /* aux 0 = xosc */
421 p->tsu_why = "disabled: enable from xosc at 50 MHz";
422 }
423}
424
425static uint32_t clk_rd(const struct ck_rp1_io *io, uint32_t off) { return io->rd(io->ctx, CK_RP1_CLOCKS + off); }
426static void clk_wr(const struct ck_rp1_io *io, uint32_t off, uint32_t v) { io->wr(io->ctx, CK_RP1_CLOCKS + off, v); }
427
428void ck_rp1_clk_read(const struct ck_rp1_io *io, struct ck_rp1_clk_regs *r)
429{
430 r->pll_cs = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_CS);
431 r->pll_fbdiv_int = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_FBDIV_INT);
432 r->pll_fbdiv_frac = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_FBDIV_FRAC);
433 r->pll_prim = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_PRIM);
434 r->pll_sec = io->rd(io->ctx, CK_RP1_PLL_SYS + CK_RP1_PLL_SEC);
435 r->sys_ctrl = clk_rd(io, CK_RP1_CLK_SYS_CTRL);
436 r->sys_div = clk_rd(io, CK_RP1_CLK_SYS_DIV);
437 r->sys_sel = clk_rd(io, CK_RP1_CLK_SYS_SEL);
438 r->eth_ctrl = clk_rd(io, CK_RP1_CLK_ETH_CTRL);
439 r->eth_div = clk_rd(io, CK_RP1_CLK_ETH_DIV);
440 r->eth_sel = clk_rd(io, CK_RP1_CLK_ETH_SEL);
441 r->tsu_ctrl = clk_rd(io, CK_RP1_CLK_TSU_CTRL);
442 r->tsu_div = clk_rd(io, CK_RP1_CLK_TSU_DIV);
443 r->tsu_sel = clk_rd(io, CK_RP1_CLK_TSU_SEL);
444}
445
446static int clk_enable_one(const struct ck_rp1_io *io, uint32_t ctrl_off, uint32_t div_off, uint32_t div, uint32_t ctrl)
447{
448 clk_wr(io, div_off, div); /* divider first (clk-rp1.c set_rate_and_parent) */
449 clk_wr(io, ctrl_off, ctrl & ~(uint32_t)CK_RP1_CLK_ENABLE); /* source (AUXSRC) while still off */
450 clk_wr(io, ctrl_off, ctrl); /* then clock on (rp1_clock_on) */
451#ifdef CK_NET_MUTANT_NO_CLK_VERIFY
452 return 0;
453#else
454 if (!(clk_rd(io, ctrl_off) & CK_RP1_CLK_ENABLE) || clk_rd(io, div_off) != div)
455 return CK_PI5NET_E_READBACK;
456 return 0;
457#endif
458}
459
460int ck_rp1_clk_apply(const struct ck_rp1_io *io, const struct ck_rp1_clk_plan *p)
461{
462 int rc = 0, e;
463 if (p->eth == CK_RP1_CLK_TO_ENABLE) {
464 e = clk_enable_one(io, CK_RP1_CLK_ETH_CTRL, CK_RP1_CLK_ETH_DIV, p->eth_div, p->eth_ctrl);
465 if (e)
466 rc = e;
467 }
468 if (p->tsu == CK_RP1_CLK_TO_ENABLE) {
469 e = clk_enable_one(io, CK_RP1_CLK_TSU_CTRL, CK_RP1_CLK_TSU_DIV, p->tsu_div, p->tsu_ctrl);
470 if (e)
471 rc = e;
472 }
473 return rc;
474}
475
476/* ---------------- B2. PHY reset ---------------- */
477
478static void delay_ms(const struct ck_rp1_io *io, uint32_t ms)
479{
480 if (io->delay_us && ms)
481 io->delay_us(io->ctx, ms * 1000u);
482}
483
484int ck_rp1_phy_reset(const struct ck_rp1_io *io, struct ck_rp1_phyreset *out)
485{
486 uint32_t ctrl, pad;
487 struct ck_rp1_phyreset o = {0};
488 int rc = 0;
489 o.ctrl_before = io->rd(io->ctx, CK_RP1_GPIO32_CTRL);
490 o.pad_before = io->rd(io->ctx, CK_RP1_GPIO32_PAD);
491 o.oe_before = io->rd(io->ctx, CK_RP1_RIO1_OE);
492 o.out_before = io->rd(io->ctx, CK_RP1_RIO1_OUT);
493 o.in_before = io->rd(io->ctx, CK_RP1_RIO1_IN);
494 /* 1. level high (reset released) before the pin is ours: no glitch low (pinctrl-rp1.c gpio_direction_output order) */
495#ifndef CK_NET_MUTANT_NO_HIGH_FIRST
496 io->wr(io->ctx, CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET, CK_RP1_GPIO_PIN_BIT);
497#endif
498 /* 2. pad: input enabled (so we can read the pin), output not disabled */
499 pad = io->rd(io->ctx, CK_RP1_GPIO32_PAD);
500 pad = (pad | CK_RP1_PAD_IN_ENABLE) & ~(uint32_t)CK_RP1_PAD_OUT_DISABLE;
501 io->wr(io->ctx, CK_RP1_GPIO32_PAD, pad);
502 /* 3. output enable */
503 io->wr(io->ctx, CK_RP1_RIO1_OE + CK_RP1_ATOMIC_SET, CK_RP1_GPIO_PIN_BIT);
504 /* 4. function = gpio (RIO), output/oe overrides = peripheral (0) */
505 ctrl = io->rd(io->ctx, CK_RP1_GPIO32_CTRL);
506 ctrl &= ~(0x1fu | 0x3000u | 0xc000u | 0x30000u);
507 ctrl |= CK_RP1_FSEL_GPIO;
508 io->wr(io->ctx, CK_RP1_GPIO32_CTRL, ctrl);
509 o.ctrl_after = io->rd(io->ctx, CK_RP1_GPIO32_CTRL);
510 o.pad_after = io->rd(io->ctx, CK_RP1_GPIO32_PAD);
511 if (!(io->rd(io->ctx, CK_RP1_RIO1_IN) & CK_RP1_GPIO_PIN_BIT))
512 rc = CK_PI5NET_E_READBACK; /* we drive it high and it does not read high */
513 /* 5. assert reset (line low, active low) */
514#ifndef CK_NET_MUTANT_NO_ASSERT
515 io->wr(io->ctx, CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_CLR, CK_RP1_GPIO_PIN_BIT);
516#endif
517#ifndef CK_NET_MUTANT_NO_HOLD
518 delay_ms(io, CK_RP1_PHY_RESET_MS);
519#endif
520 o.in_low = io->rd(io->ctx, CK_RP1_RIO1_IN);
521 if (o.in_low & CK_RP1_GPIO_PIN_BIT)
522 rc = CK_PI5NET_E_READBACK; /* driven low but reads high */
523 /* 6. release and let the PHY come up */
524#ifndef CK_NET_MUTANT_NO_RELEASE
525 io->wr(io->ctx, CK_RP1_RIO1_OUT + CK_RP1_ATOMIC_SET, CK_RP1_GPIO_PIN_BIT);
526#endif
527 delay_ms(io, CK_RP1_PHY_SETTLE_MS);
528 o.in_high = io->rd(io->ctx, CK_RP1_RIO1_IN);
529 if (!(o.in_high & CK_RP1_GPIO_PIN_BIT))
530 rc = CK_PI5NET_E_READBACK;
531 if (out)
532 *out = o;
533 return rc;
534}
535
536/* ---------------- B3. MDIO ---------------- */
537
538uint32_t ck_gem_mdc_div_code(uint32_t pclk_hz)
539{
540 if (pclk_hz == 0)
541 return 7; /* unknown rate: slowest MDC */
542#ifdef CK_NET_MUTANT_NO_MDC_DIV
543 return 7;
544#else
545 if (pclk_hz <= 20000000u) return 0;
546 if (pclk_hz <= 40000000u) return 1;
547 if (pclk_hz <= 80000000u) return 2;
548 if (pclk_hz <= 120000000u) return 3;
549 if (pclk_hz <= 160000000u) return 4;
550 if (pclk_hz <= 240000000u) return 5;
551 if (pclk_hz <= 320000000u) return 6;
552 return 7;
553#endif
554}
555
556uint32_t ck_gem_mdio_frame_read(uint32_t phy, uint32_t reg)
557{
558 return (1u << 30) | (2u << 28) | ((phy & 31u) << 23) | ((reg & 31u) << 18) | (2u << 16);
559}
560
561int ck_gem_mdio_enable(const struct ck_rp1_io *io, uint32_t pclk_hz)
562{
563 uint32_t ncr = io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCR), cfg = io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCFGR);
564 uint32_t code = ck_gem_mdc_div_code(pclk_hz);
565 cfg = (cfg & ~(uint32_t)CK_GEM_NCFGR_CLK_MASK) | (code << CK_GEM_NCFGR_CLK_SHIFT);
566 io->wr(io->ctx, CK_RP1_ETH + CK_GEM_NCFGR, cfg); /* divider before the port is enabled */
567#ifndef CK_NET_MUTANT_NO_MPE
568 io->wr(io->ctx, CK_RP1_ETH + CK_GEM_NCR, ncr | CK_GEM_NCR_MPE);
569#else
570 (void)ncr;
571#endif
572 if (!(io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCR) & CK_GEM_NCR_MPE) ||
573 ((io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NCFGR) & CK_GEM_NCFGR_CLK_MASK) >> CK_GEM_NCFGR_CLK_SHIFT) != code)
574 return CK_PI5NET_E_READBACK;
575 return 0;
576}
577
578static int mdio_wait_idle(const struct ck_rp1_io *io)
579{
580 uint32_t i;
581 for (i = 0; i < CK_GEM_MDIO_POLLS; i++) {
582 if (io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NSR) & CK_GEM_NSR_IDLE)
583 return 0;
584 if (io->delay_us)
585 io->delay_us(io->ctx, CK_GEM_MDIO_POLL_US);
586 }
587 return CK_PI5NET_E_TIMEOUT;
588}
589
590int ck_gem_mdio_read(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t *val)
591{
592 if (!io || !val || phy > 31u || reg > 31u)
593 return CK_PI5NET_E_ARG;
594 if (mdio_wait_idle(io))
595 return CK_PI5NET_E_TIMEOUT;
596 io->wr(io->ctx, CK_RP1_ETH + CK_GEM_MAN, ck_gem_mdio_frame_read(phy, reg));
597#ifndef CK_NET_MUTANT_NO_IDLE_AFTER
598 if (mdio_wait_idle(io))
599 return CK_PI5NET_E_TIMEOUT;
600#endif
601 *val = (uint16_t)(io->rd(io->ctx, CK_RP1_ETH + CK_GEM_MAN) & 0xffffu);
602 return 0;
603}
604
605void ck_phy_id_decode(uint16_t r2, uint16_t r3, struct ck_phy_id *o)
606{
607 uint8_t b[25] = {0}; /* b[1]..b[24] = OUI bits 1..24 (IEEE 802.3 22.2.4.3.1) */
608 uint32_t i, k, oui = 0;
609 *o = (struct ck_phy_id){0};
610 o->id = ((uint32_t)r2 << 16) | r3;
611 /* reg 2 bit 15..0 = OUI bit 3..18; reg 3 bit 15..10 = OUI bit 19..24 */
612 for (i = 0; i < 16; i++)
613 b[3 + i] = (uint8_t)((r2 >> (15 - i)) & 1u);
614 for (i = 0; i < 6; i++)
615 b[19 + i] = (uint8_t)((r3 >> (15 - i)) & 1u);
616 for (k = 0; k < 3; k++) {
617 uint32_t oct = 0;
618 for (i = 0; i < 8; i++)
619#ifdef CK_NET_MUTANT_OUI_ORDER
620 oct |= (uint32_t)b[8 * k + 1 + i] << (7 - i); /* wrong bit order within the octet */
621#else
622 oct |= (uint32_t)b[8 * k + 1 + i] << i; /* octet's bit 0 is the first transmitted */
623#endif
624 oui = (oui << 8) | oct;
625 }
626 o->oui = oui;
627 o->model = (uint8_t)((r3 >> 4) & 0x3fu);
628 o->rev = (uint8_t)(r3 & 0xfu);
629 if (o->id == 0xffffffffu || o->id == 0 || r2 == 0xffffu)
630 o->cls = CK_PHY_ABSENT;
631#ifndef CK_NET_MUTANT_BAD_CLASS
632 else if (o->id == CK_PHY_ID_BCM54213PE)
633 o->cls = CK_PHY_BCM54213PE;
634#else
635 else if ((o->id & 0xfffffff0u) == (CK_PHY_ID_BCM54213PE & 0xfffffff0u))
636 o->cls = CK_PHY_BCM54213PE; /* would also accept BCM54210E and any revision: Linux matches exactly */
637#endif
638 else if (o->oui == 0x18c086u || o->oui == 0x001018u) /* both are "Broadcom" in the IEEE registry */
639 o->cls = CK_PHY_BROADCOM_OTHER;
640 else
641 o->cls = CK_PHY_OTHER;
642}
643
644const char *ck_phy_class_name(int cls)
645{
646 switch (cls) {
647 case CK_PHY_ABSENT: return "ABSENT";
648 case CK_PHY_BCM54213PE: return "BCM54213PE";
649 case CK_PHY_BROADCOM_OTHER: return "BROADCOM_OTHER";
650 default: return "OTHER";
651 }
652}
653
654int ck_phy_find(const struct ck_rp1_io *io, uint32_t addr, uint32_t tries, uint32_t gap_ms, struct ck_phy_id *id,
655 uint32_t *attempts, int *scan_hit)
656{
657 uint32_t t, a;
658 uint16_t r2 = 0xffff, r3 = 0xffff;
659 if (attempts)
660 *attempts = 0;
661 if (scan_hit)
662 *scan_hit = -1;
663 if (!io || !id || addr > 31u || tries == 0)
664 return CK_PI5NET_E_ARG;
665 ck_phy_id_decode(0xffff, 0xffff, id);
666 for (t = 0; t < tries; t++) {
667 int e2, e3;
668 if (attempts)
669 *attempts = t + 1;
670 e2 = ck_gem_mdio_read(io, addr, 2, &r2);
671 e3 = ck_gem_mdio_read(io, addr, 3, &r3);
672 if (e2 || e3)
673 return e2 ? e2 : e3;
674 if (r2 != 0xffffu && !(r2 == 0 && r3 == 0)) {
675 ck_phy_id_decode(r2, r3, id);
676 return 0;
677 }
678 delay_ms(io, gap_ms);
679 }
680 if (scan_hit) {
681 for (a = 0; a < 32u; a++) {
682 if (ck_gem_mdio_read(io, a, 2, &r2) == 0 && r2 != 0xffffu && r2 != 0) {
683 *scan_hit = (int)a;
684 break;
685 }
686 }
687 }
688 return CK_PI5NET_E_TIMEOUT;
689}
690
691/* ================= image glue (CK_PI5_NET only) ================= */
692#ifdef CK_PI5_NET
693#include "ck.h"
694#include "dma_window.h"
695#include "pi5_gem.h"
696
697static volatile uint8_t *rc;
698static volatile uint8_t *rp1;
699
700static uint32_t rc_rd(void *c, uint32_t off) { (void)c; return *(volatile uint32_t *)(rc + off); }
701static void rc_wr(void *c, uint32_t off, uint32_t v) { (void)c; *(volatile uint32_t *)(rc + off) = v; ck_mb(); }
702static uint32_t rp_rd(void *c, uint32_t off) { (void)c; return *(volatile uint32_t *)(rp1 + off); }
703static void rp_wr(void *c, uint32_t off, uint32_t v) { (void)c; *(volatile uint32_t *)(rp1 + off) = v; ck_mb(); }
704static void rp_delay(void *c, uint32_t us)
705{
706 (void)c;
707 while (us > 1000u) {
708 ck_udelay(1000u);
709 us -= 1000u;
710 }
711 if (us)
712 ck_udelay(us);
713}
714
715static uint32_t cmd_dword(const struct ck_pi5pcie_io *io)
716{
717 uint32_t v = 0;
718 (void)ck_pi5pcie_cfg_read32(io, 1, 0, 0, 4, &v);
719 return v;
720}
721
722static void print_wins(const char *tag, const struct ck_pi5pcie_io *io, struct ck_pi5pcie_ibwin *win)
723{
724 uint32_t i;
725 for (i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++) {
726 if (ck_pi5pcie_ib_read(io, i, &win[i]))
727 continue;
728 if (win[i].enabled)
729 ck_printf("pi5_net: inbound %s win%u bus=0x%llx size=0x%llx cpu=0x%llx remap=%u valid=%u\n", tag, i,
730 (unsigned long long)win[i].bus_base, (unsigned long long)win[i].size,
731 (unsigned long long)win[i].cpu_base, win[i].remap_en, win[i].size_valid);
732 }
733}
734
735static uint32_t unmodelled_windows(void)
736{
737 uint32_t u = 0;
738 if (rc_rd(0, CK_PI5PCIE_RC_BAR1_LO) & CK_PI5PCIE_BAR_SIZE_MASK)
739 u++;
740 if (rc_rd(0, CK_PI5PCIE_RC_BAR3_LO) & CK_PI5PCIE_BAR_SIZE_MASK)
741 u++;
742 return u;
743}
744
745static int print_verdict(const char *tag, const struct ck_pi5pcie_ibwin *win, uint32_t unmod, uint64_t b_phys,
746 uint64_t b_size, const struct ck_pi5net_ibprog *plan)
747{
748 struct ck_pi5pcie_dmav dv;
749 int label;
750 ck_pi5pcie_ib_verdict(win, CK_PI5PCIE_IB_WINDOWS, unmod, b_phys, b_size, &dv);
751 label = ck_pi5net_confine_label(&dv, plan);
752 ck_printf("pi5_dma: hw_window %s=%s [%s] reach=0x%llx inside=0x%llx outside=0x%llx\n", tag,
753 ck_pi5net_confine_name(label), ck_pi5pcie_verdict_name(dv.verdict), (unsigned long long)dv.reach_bytes,
754 (unsigned long long)dv.inside_bytes, (unsigned long long)dv.outside_bytes);
755 return label;
756}
757
758static void print_clk_rates(const char *tag, const struct ck_rp1_clk_regs *r)
759{
760 ck_printf("pi5_net: rp1 clocks %s: pll_core=%u pll_sec=%u clk_sys=%u clk_eth=%u clk_eth_tsu=%u Hz (0 = off/unknown)\n",
761 tag, ck_rp1_pll_core_hz(r), ck_rp1_pll_sec_hz(r), ck_rp1_clk_sys_hz(r), ck_rp1_clk_eth_hz(r),
762 ck_rp1_clk_tsu_hz(r));
763}
764
765void ck_pi5net_run(void)
766{
767 static const struct ck_pi5pcie_io io = {rc_rd, rc_wr, 0};
768 static const struct ck_rp1_io rio = {rp_rd, rp_wr, rp_delay, 0};
769 struct ck_pi5pcie_func root, rp1f;
770 struct ck_pi5pcie_ibwin win[CK_PI5PCIE_IB_WINDOWS];
771 struct ck_pi5net_ibprog plan;
772 struct ck_pi5net_ibsaved saved;
773 struct ck_pi5net_obwin ob;
774 struct ck_pi5pcie_bar bar1;
775 struct ck_rp1_clk_regs cr;
776 struct ck_rp1_clk_plan cp;
777 struct ck_rp1_phyreset pr;
778 struct ck_phy_id pid;
779 struct ck_dmaw dw;
780 uint64_t b_phys = 0, bar1_cpu;
781 uint32_t unmod, ndis = 0, attempts = 0;
782 uint16_t cmd_saved = 0;
783 int rrc, rrp1, ver, rc2, scan_hit = -1, confined = 0;
784 void *bounce;
785
786 ck_printf("pi5_net: cut 3: hardware DMA confinement, RP1 Ethernet up to the PHY, then one frame through the GEM (descriptors inside the bounce window only)\n");
787 ck_printf("pi5_net: software-only DMA confinement (Pi 5, weaker than the Spark) plus the root-complex window below\n");
788 rc = (volatile uint8_t *)ck_mmio_try_map(CK_PI5PCIE_RC2_PHYS, CK_PI5PCIE_RC_BYTES);
789 if (!rc) {
790 ck_printf("pi5_net: FAIL map root complex 0x%llx (refused); nothing done\n",
791 (unsigned long long)CK_PI5PCIE_RC2_PHYS);
792 return;
793 }
794 rrc = ck_pi5pcie_read_func(&io, 0, 0, 0, &root);
795 rrp1 = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1f);
796 ver = ck_pi5pcie_rp1_check(rrp1, &rp1f);
797 if (rrc == 0)
798 ck_printf("pi5_net: root 00:00.0 %04x:%04x class=%06x sec=%u sub=%u\n", root.vendor, root.device,
799 root.class_code, root.secondary, root.subordinate);
800 ck_printf("pi5_net: rp1 01:00.0 %04x:%04x %s cmd=0x%04x\n", rp1f.vendor, rp1f.device,
801 ver == CK_PI5PCIE_RP1_OK ? "MATCH" : "NOT_RP1", rp1f.command);
802 if (ver != CK_PI5PCIE_RP1_OK) {
803 ck_printf("pi5_net: STOP rp1 not present at 01:00.0; nothing programmed\n");
804 return;
805 }
806
807 /* ---------------- A. confine the inbound window ---------------- */
808 bounce = ck_dma_alloc(CK_DMAW_MAX_BYTES, CK_DMAW_MAX_BYTES, &b_phys);
809 if (!bounce || ck_dmaw_init(&dw, bounce, b_phys, CK_DMAW_MAX_BYTES) != CK_DMAW_OK) {
810 ck_printf("pi5_dma: FAIL no bounce window (alloc/init); window left as firmware set it\n");
811 } else {
812 ck_printf("pi5_dma: bounce cpu=0x%llx size=0x%llx (ADR 0020 rule 1)\n", (unsigned long long)b_phys,
813 (unsigned long long)dw.size);
814 unmod = unmodelled_windows();
815 print_wins("before", &io, win);
816 print_verdict("before", win, unmod, b_phys, dw.size, 0);
817 rc2 = ck_pi5net_ib_plan(b_phys, dw.size, &plan);
818 if (rc2) {
819 ck_printf("pi5_dma: FAIL plan rc=%d; window left as firmware set it\n", rc2);
820 } else {
821 ck_printf("pi5_dma: plan bus=0x%llx cpu=0x%llx size=0x%llx code=%u extra_beyond_bounce=0x%llx\n",
822 (unsigned long long)plan.bus_base, (unsigned long long)plan.cpu_base,
823 (unsigned long long)plan.size, plan.size_code, (unsigned long long)plan.extra_bytes);
824 rc2 = ck_pi5net_rp1_quiesce(&io, &cmd_saved);
825 ck_printf("pi5_dma: rp1 bus-master off for the window change: rc=%d cmd_was=0x%04x cmd_now=0x%08x\n", rc2,
826 cmd_saved, cmd_dword(&io));
827 if (rc2 == 0) {
828 ck_udelay(2000); /* let anything already on the wire finish */
829 rc2 = ck_pi5net_ib_apply(&io, &plan, &saved, &ndis);
830 ck_printf("pi5_dma: apply rc=%d (0 = window now matches the plan); other_windows_disabled=%u\n", rc2,
831 ndis);
832 if (rc2)
833 ck_printf("pi5_dma: readback LO=0x%08x HI=0x%08x REMAP_LO=0x%08x REMAP_HI=0x%08x (firmware window restored)\n",
834 rc_rd(0, CK_PI5NET_IB0_LO), rc_rd(0, CK_PI5NET_IB0_HI),
835 rc_rd(0, CK_PI5NET_IB0_REMAP_LO), rc_rd(0, CK_PI5NET_IB0_REMAP_HI));
836 } else {
837 ck_printf("pi5_dma: STOP rp1 would not stop bus mastering; window left as firmware set it\n");
838 }
839 unmod = unmodelled_windows();
840 print_wins("after", &io, win);
841 confined = print_verdict("after", win, unmod, b_phys, dw.size, &plan) == CK_PI5NET_CONF_CONFINED && rc2 == 0;
842 ck_printf("pi5_dma: rp1 bus-master is LEFT OFF (cmd=0x%08x); the GEM driver turns it on when it needs it\n",
843 cmd_dword(&io));
844 }
845 }
846
847 /* ---------------- B. RP1 Ethernet up to the PHY ---------------- */
848 if (ck_pi5net_ob_read(&io, &ob) != 0 || !ob.valid) {
849 ck_printf("pi5_net: STOP outbound window 0 unreadable; RP1 not touched\n");
850 return;
851 }
852 ck_printf("pi5_net: outbound win0 bus=0x%llx cpu=0x%llx size=0x%llx\n", (unsigned long long)ob.bus_base,
853 (unsigned long long)ob.cpu_base, (unsigned long long)ob.size);
854 (void)ck_pi5pcie_bar_decode(rp1f.bar_raw, 1, rp1f.nbars, &bar1);
855 if (bar1.kind != CK_PI5PCIE_BAR_MEM32 && bar1.kind != CK_PI5PCIE_BAR_MEM64) {
856 ck_printf("pi5_net: STOP rp1 bar1 is not a memory BAR; RP1 not touched\n");
857 return;
858 }
859 bar1_cpu = ck_pi5net_bus_to_cpu(&ob, bar1.addr, CK_RP1_BAR1_MAP_BYTES);
860 if (!bar1_cpu) {
861 ck_printf("pi5_net: STOP rp1 bar1 bus 0x%llx is outside outbound win0; RP1 not touched\n",
862 (unsigned long long)bar1.addr);
863 return;
864 }
865 rp1 = (volatile uint8_t *)ck_mmio_try_map(bar1_cpu, CK_RP1_BAR1_MAP_BYTES);
866 if (!rp1) {
867 ck_printf("pi5_net: STOP map rp1 bar1 cpu 0x%llx refused; RP1 not touched\n", (unsigned long long)bar1_cpu);
868 return;
869 }
870 ck_printf("pi5_net: rp1 bar1 bus=0x%llx -> cpu=0x%llx chip_id=0x%08x platform=0x%08x\n",
871 (unsigned long long)bar1.addr, (unsigned long long)bar1_cpu, rp_rd(0, CK_RP1_SYSINFO),
872 rp_rd(0, CK_RP1_SYSINFO + 4));
873 if (rp_rd(0, CK_RP1_SYSINFO) == 0xffffffffu) {
874 ck_printf("pi5_net: STOP rp1 reads all ones (link or window problem); nothing programmed\n");
875 return;
876 }
877 ck_printf("pi5_net: gem before: MID=0x%08x NCR=0x%08x NCFGR=0x%08x NSR=0x%08x eth_cfg CONTROL=0x%08x CLKGEN=0x%08x\n",
878 rp_rd(0, CK_RP1_ETH + CK_GEM_MID), rp_rd(0, CK_RP1_ETH + CK_GEM_NCR),
879 rp_rd(0, CK_RP1_ETH + CK_GEM_NCFGR), rp_rd(0, CK_RP1_ETH + CK_GEM_NSR),
880 rp_rd(0, CK_RP1_ETH_CFG + 0x00), rp_rd(0, CK_RP1_ETH_CFG + 0x14));
881 ck_rp1_clk_read(&rio, &cr);
882 ck_printf("pi5_net: rp1 clock regs: PLL cs=0x%08x fbint=0x%08x frac=0x%08x prim=0x%08x sec=0x%08x | SYS ctrl=0x%08x div=%u sel=0x%x | ETH ctrl=0x%08x div=%u sel=0x%x | TSU ctrl=0x%08x div=%u sel=0x%x\n",
883 cr.pll_cs, cr.pll_fbdiv_int, cr.pll_fbdiv_frac, cr.pll_prim, cr.pll_sec, cr.sys_ctrl, cr.sys_div,
884 cr.sys_sel, cr.eth_ctrl, cr.eth_div, cr.eth_sel, cr.tsu_ctrl, cr.tsu_div, cr.tsu_sel);
885 print_clk_rates("before", &cr);
886 ck_rp1_clk_decide(&cr, &cp);
887 ck_printf("pi5_net: clk_eth: %s (%s) | clk_eth_tsu: %s (%s)\n",
888 cp.eth == CK_RP1_CLK_ALREADY_ON ? "ALREADY_ON" : cp.eth == CK_RP1_CLK_TO_ENABLE ? "ENABLE" : "BLOCKED",
889 cp.eth_why, cp.tsu == CK_RP1_CLK_ALREADY_ON ? "ALREADY_ON" : cp.tsu == CK_RP1_CLK_TO_ENABLE ? "ENABLE" : "BLOCKED",
890 cp.tsu_why);
891 rc2 = ck_rp1_clk_apply(&rio, &cp);
892 ck_rp1_clk_read(&rio, &cr);
893 print_clk_rates("after", &cr);
894 ck_printf("pi5_net: clock apply rc=%d\n", rc2);
895 if (cp.eth == CK_RP1_CLK_BLOCKED) {
896 ck_printf("pi5_net: STOP clk_eth blocked; PHY not touched\n");
897 return;
898 }
899
900 rc2 = ck_rp1_phy_reset(&rio, &pr);
901 ck_printf("pi5_net: phy reset gpio32 rc=%d ctrl %08x->%08x pad %08x->%08x oe_before=%08x out_before=%08x in: before=%08x low=%08x high=%08x\n",
902 rc2, pr.ctrl_before, pr.ctrl_after, pr.pad_before, pr.pad_after, pr.oe_before, pr.out_before,
903 pr.in_before, pr.in_low, pr.in_high);
904 if (rc2) {
905 ck_printf("pi5_net: STOP gpio32 did not follow the reset sequence; PHY not read\n");
906 return;
907 }
908 rc2 = ck_gem_mdio_enable(&rio, ck_rp1_clk_sys_hz(&cr));
909 ck_printf("pi5_net: mdio enable rc=%d NCR=0x%08x NCFGR=0x%08x (MDC divider code %u for pclk %u Hz)\n", rc2,
910 rp_rd(0, CK_RP1_ETH + CK_GEM_NCR), rp_rd(0, CK_RP1_ETH + CK_GEM_NCFGR),
911 ck_gem_mdc_div_code(ck_rp1_clk_sys_hz(&cr)), ck_rp1_clk_sys_hz(&cr));
912 if (rc2)
913 return;
914 rc2 = ck_phy_find(&rio, 1, 20, 25, &pid, &attempts, &scan_hit);
915 if (rc2 == 0)
916 ck_printf("pi5_net: phy addr=1 id=0x%08x oui=%02x-%02x-%02x model=0x%02x rev=%u class=%s attempts=%u\n", pid.id,
917 (pid.oui >> 16) & 0xff, (pid.oui >> 8) & 0xff, pid.oui & 0xff, pid.model, pid.rev,
918 ck_phy_class_name(pid.cls), attempts);
919 else
920 ck_printf("pi5_net: phy addr=1 NOT FOUND rc=%d attempts=%u scan_lowest_answering=%d\n", rc2, attempts, scan_hit);
921 if (rc2 == 0 && pid.cls == CK_PHY_BCM54213PE) {
922 struct ck_pi5gem_ctx gc = {&io, &rio, &dw, bounce, b_phys, confined, 1, ck_rp1_clk_sys_hz(&cr)};
923 ck_printf("pi5_net: phy found; handing over to the GEM frame test (hw window confined=%d)\n", confined);
924 ck_pi5gem_run(&gc);
925 } else {
926 ck_printf("pi5_net: no BCM54213PE at addr 1; GEM frame test not run\n");
927 }
928 ck_printf("pi5_net: done: rp1 bus-master off unless the line above says otherwise\n");
929}
930#endif