diff --git a/README.md b/README.md index ccf4a23..7339880 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ slow or destructive. - Boot a Linux kernel on CPU0 (switchroot's L4T 4.9), with a GIC-400, the generic timer, PSCI and the clock controller's reset values for it. See [Booting Linux on CPU0](#booting-linux-on-cpu0). +- Put a dock on the USB-C port (`--dock`): a Nintendo dock, a third-party + USB-C adapter or a Nintendo charger on the BM92T36, the dock's + DisplayPort-to-HDMI converter on DPAUX1, and a TV in a second window that + shows what display head B sends through SOR1. hwtest-rcm's Dock Test runs + its whole handshake and TV link against it. - Tweak emulated hardware live (battery, charger, thermal, USB-PD, PMIC, fuses, SD insertion, the USB host) from a side window. See [Live hardware tweaks](#live-hardware-tweaks). - Inspect each Tegra UART port (TX history) and inject keystrokes into the @@ -365,6 +370,7 @@ needed. | `--fault` | `NAME[,NAME…]` \| `list` | Break a chip or a rail; repeatable. A **chip** fault (`max77812`, `max77621_cpu`, `max77621_gpu`, `bq24193`, `max17050`, `tmp451`, `bm92t36`) makes the part NAK its I2C address, refuse writes and take every rail it supplies down with it. A **rail** fault (`vdd_cpu`) leaves the regulator answering and reading back as enabled while its output never comes up, so only what the rail feeds notices: the CPU RAM repair never completes and CPU0 cannot run. `list` prints the names and exits. Faults survive a payload reboot, like the hardware they model, and add to `[faults]` in the ini (`NAME=dead`). See [DESIGN.md](DESIGN.md#fault-injection). | | `--usb-host` | `nbd[:port]` (optional) | A PC on the USB-C port. It enumerates whatever USB gadget the payload brings up; a mass storage disk is read back (checked against the SD image), its last 8 sectors are written back unchanged and read again unless it is write-protected, and it is ejected; HID reports are polled. With `nbd` the disk is served over NBD on 127.0.0.1 (port 10809 by default) until the client disconnects, then ejected - see [Mounting a gadget's disk](#mounting-a-gadgets-disk). Logged as `[usb-host]`; `RCM_USB_TRACE=1` traces every transfer. Overrides `[usb] host` and `[usb] nbd_port` in the ini. | | `--load` | `FILE@ADDR` | Place a file in DRAM before the payload starts, the way a bootloader leaves a kernel and its device tree; repeatable. See [Booting Linux on CPU0](#booting-linux-on-cpu0). | +| `--dock` | `nintendo` \| `thirdparty` \| `charger` \| `none`, `:notv` | What is on the USB-C port, behind the BM92T36: a Nintendo dock (057E:2003, 15 V, Nintendo's alt mode, DisplayPort on pin D), a third-party adapter (Fresco Logic 1D5C:7102, four power profiles, the DisplayPort SVID) or the Nintendo charger (057E:2004, no video). A dock's DisplayPort-to-HDMI converter answers on DPAUX1 (DPCD 1.2, the TV's EDID over I2C-over-AUX), and SOR1 judges link training lane by lane against the board's wiring; once trained, head B's frames show in a TV window. `:notv` leaves the dock's HDMI empty. `RCM_EMU_TV_SNAP=file` saves the TV picture, `RCM_EMU_DPTRACE=file` traces DPAUX1 and SOR1. Overrides `[dock] kind` and `[dock] tv` in the ini. Default `none`. | | `--input-script` | file or spec | Scripted input keyed to emulated time: ` P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, ` TAP [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, ` SNAP ` saves the next displayed frame to `` (raw B, G, R, A like `last_fb.rgba`, size in the matching `.meta`), `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. | | `--auto-pin-recovery` | (none) | Drive the Lockpick PIN-recovery menu without user input. | | `--auto-te-script` | (none) | Drive `recover_pin.te` in TegraExplorer without input. | @@ -386,6 +392,7 @@ flowchart LR t210 --> se["se_engine.{h,cpp}
AES-128, SHA-256, RSA, RNG"] t210 --> i2c["i2c3.{h,cpp}
STMFTS / FTS4 touch"] t210 --> usb["usb.{h,cpp}
USB device controllers + host PC"] + t210 --> dock["dock.{h,cpp}
BM92T36, the dock, DPAUX1, SOR1"] display --> sdl["sdl_display.{h,cpp}
DC window scan-out
+ SDL2"] ``` diff --git a/display/config_window.cpp b/display/config_window.cpp index b2e4106..0cd6919 100644 --- a/display/config_window.cpp +++ b/display/config_window.cpp @@ -140,6 +140,8 @@ void reset_to_defaults(EmuState *s) { X(usb_pd_inserted, "usb_pd", "inserted", 0) \ X(usb_pd_voltage_mv, "usb_pd", "voltage_mv", 0) \ X(usb_pd_amperage_ma, "usb_pd", "amperage_ma", 0) \ + X(dock_kind, "dock", "kind", 0) \ + X(dock_tv, "dock", "tv", 0) \ X(usb_host, "usb", "host", 0) \ X(usb_nbd_port, "usb", "nbd_port", 0) \ /* SoC / PMIC */ \ diff --git a/display/sdl_display.cpp b/display/sdl_display.cpp index 41bed1e..554ae5b 100644 --- a/display/sdl_display.cpp +++ b/display/sdl_display.cpp @@ -1,4 +1,6 @@ #include "sdl_display.h" +#include "../t210/dock.h" +#include "../t210/mmio.h" #include #include "config_window.h" #include "console_window.h" @@ -291,8 +293,9 @@ uint32_t dst_factor(uint32_t sel, uint32_t k1, uint32_t k2, uint32_t sa) { } } -// Draw window w onto the panel picture. -void draw_window(const Win &w, std::vector &panel) { +// Draw window w onto the panel picture (pw x ph, row-major). +void draw_window(const Win &w, std::vector &panel, uint32_t pw = kPanelW, + uint32_t ph = kPanelH) { bool blend = !(w.blend_layer & (1u << 24)); uint32_t k1 = (w.blend_layer >> 8) & 0xFF, k2 = (w.blend_layer >> 16) & 0xFF; uint32_t ssel = w.blend_match & 7, dsel = (w.blend_match >> 4) & 7; @@ -305,12 +308,12 @@ void draw_window(const Win &w, std::vector &panel) { txs[ox] = (uint32_t)((uint64_t)ox * w.pre_w / w.out_w); for (uint32_t oy = 0; oy < w.out_h; oy++) { int32_t py = w.y + (int32_t)oy; - if (py < 0 || py >= (int32_t)kPanelH) + if (py < 0 || py >= (int32_t)ph) continue; uint32_t ty = (uint32_t)((uint64_t)oy * w.pre_h / w.out_h); for (uint32_t ox = 0; ox < w.out_w; ox++) { int32_t px = w.x + (int32_t)ox; - if (px < 0 || px >= (int32_t)kPanelW) + if (px < 0 || px >= (int32_t)pw) continue; uint32_t tx = txs[ox]; // (tx, ty) is post-turn; walk it back to the surface. @@ -323,7 +326,7 @@ void draw_window(const Win &w, std::vector &panel) { if (off + w.bpp > w.len) continue; uint32_t s = depth_argb(w.mem + off, w.depth); - uint32_t &d = panel[(size_t)py * kPanelW + px]; + uint32_t &d = panel[(size_t)py * pw + px]; if (!blend) { d = s | 0xFF000000u; continue; @@ -340,9 +343,98 @@ void draw_window(const Win &w, std::vector &panel) { } } +// ---- The TV on the dock ------------------------------------------------------ +// +// A second window, shown while a dock with video is configured: head B's +// windows composed at the head's active size, as SOR1 sends them, when the +// dock model says frames reach the TV; otherwise a dark screen, and the +// window title says why there is no picture. +SDL_Window *tv_window = nullptr; +SDL_Renderer *tv_renderer = nullptr; +SDL_Texture *tv_texture = nullptr; +uint32_t tv_tex_w = 0, tv_tex_h = 0; +std::string tv_title; + +void tv_update(EmuState *state, uc_engine *uc) { + uint8_t k = state->dock_kind.load(); + bool want = (k == 1 || k == 2) && state->dock_tv.load(); + if (!want) { + if (tv_window) + SDL_HideWindow(tv_window); + return; + } + if (!tv_window) { + tv_window = SDL_CreateWindow("TV (dock HDMI)", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, 960, 540, + SDL_WINDOW_RESIZABLE); + if (!tv_window) + return; + tv_renderer = SDL_CreateRenderer(tv_window, -1, SDL_RENDERER_ACCELERATED); + if (!tv_renderer) + tv_renderer = SDL_CreateRenderer(tv_window, -1, SDL_RENDERER_SOFTWARE); + } + SDL_ShowWindow(tv_window); + TvState t; + dock_tv_state(state, &t); + uint32_t act = mmio_bus_read(state, 0x54240000 + 0x409 * 4, 4); + uint32_t w = act & 0x1FFF, h = (act >> 16) & 0x1FFF; + char title[256]; + if (t.video && w && h) + snprintf(title, sizeof(title), "TV (dock HDMI) - %ux%u, %s", w, h, t.status); + else + snprintf(title, sizeof(title), "TV (dock HDMI) - no signal: %s", t.status); + if (tv_title != title) { + tv_title = title; + SDL_SetWindowTitle(tv_window, title); + printf("[tv] %s\n", title + 16); + fflush(stdout); + } + SDL_SetRenderDrawColor(tv_renderer, 8, 8, 24, 255); + SDL_RenderClear(tv_renderer); + if (t.video && w && h && w <= 4096 && h <= 4096) { + std::vector pic((size_t)w * h, 0xFF000000u); + std::vector wins; + for (int i = 0; i < 4; i++) { + Win wn; + if (load_window(state, uc, i, state->dcb_win_active[i], -1, wn)) + wins.push_back(std::move(wn)); + } + std::stable_sort(wins.begin(), wins.end(), + [](const Win &a, const Win &b) { return a.layer > b.layer; }); + for (const Win &wn : wins) + draw_window(wn, pic, w, h); + if (!tv_texture || tv_tex_w != w || tv_tex_h != h) { + if (tv_texture) + SDL_DestroyTexture(tv_texture); + tv_texture = SDL_CreateTexture(tv_renderer, SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, (int)w, (int)h); + tv_tex_w = w; + tv_tex_h = h; + } + if (tv_texture) { + SDL_UpdateTexture(tv_texture, nullptr, pic.data(), (int)(w * 4)); + SDL_RenderCopy(tv_renderer, tv_texture, nullptr, nullptr); + } + if (const char *snap = getenv("RCM_EMU_TV_SNAP")) { + static uint64_t last = 0; + if (state->emu_usec - last > 500000) { + last = state->emu_usec; + if (FILE *f = fopen(snap, "wb")) { + fwrite(&w, 4, 1, f); + fwrite(&h, 4, 1, f); + fwrite(pic.data(), 4, pic.size(), f); + fclose(f); + } + } + } + } + SDL_RenderPresent(tv_renderer); +} + } // namespace void sdl_display_update(EmuState *state, uc_engine *uc) { + tv_update(state, uc); if (!renderer) return; diff --git a/emu_state.h b/emu_state.h index 8773bfb..ec7e0b7 100644 --- a/emu_state.h +++ b/emu_state.h @@ -290,6 +290,12 @@ struct EmuState { uint32_t dc_win[4][dcwin::kRegs] = {}; uint32_t dc_win_active[4][dcwin::kRegs] = {}; uint32_t dc_window_header = 0; // DC_CMD_DISPLAY_WINDOW_HEADER + // Display head B (DC1, 0x54240000): the head that feeds SOR1 and the + // dock. Same window model as head A, scanned out to the TV window. + uint32_t dcb_win[4][dcwin::kRegs] = {}; + uint32_t dcb_win_active[4][dcwin::kRegs] = {}; + uint32_t dcb_window_header = 0; + bool dcb_programmed = false; // False from reset until the payload first activates a window. Until // then the scan-out shows the emulator's own framebuffer (FB_BASE). bool dc_programmed = false; @@ -499,6 +505,11 @@ struct EmuState { // USB-PD controller (BM92T36, I2C1 @ 0x18). One synthesized fixed PDO. std::atomic usb_pd_inserted{false}; + // The dock on the USB-C port (t210/dock.h DockKind) and whether a TV is + // on its HDMI output. [dock] kind=0 none, 1 Nintendo dock, 2 third-party + // adapter, 3 Nintendo charger; tv=0/1. --dock overrides. + std::atomic dock_kind{0}; + std::atomic dock_tv{true}; std::atomic usb_pd_voltage_mv{5000}; std::atomic usb_pd_amperage_ma{1500}; diff --git a/main.cpp b/main.cpp index 58049a7..03c6469 100644 --- a/main.cpp +++ b/main.cpp @@ -27,6 +27,7 @@ #include "trace.h" #include "t210/bpmp.h" #include "t210/ccplex.h" +#include "t210/dock.h" #include "t210/memory_map.h" #include "t210/mmio.h" #include "display/sdl_display.h" @@ -340,6 +341,23 @@ int main(int argc, char *argv[]) { rawnand_prefix = argv[++i]; } else if (strcmp(argv[i], "--prod-keys") == 0 && i + 1 < argc) { prod_keys_path = argv[++i]; + } else if (strcmp(argv[i], "--dock") == 0 && i + 1 < argc) { + // What is on the USB-C port. Same contract as --oem: overrides + // the ini. "nintendo", "thirdparty" (Ugreen-style), "charger", + // "none"; append ":notv" for a dock with nothing on its HDMI. + const char *d = argv[++i]; + uint8_t k = 0xFF; + if (!strncmp(d, "nintendo", 8)) k = 1; + else if (!strncmp(d, "thirdparty", 10) || !strncmp(d, "ugreen", 6)) k = 2; + else if (!strncmp(d, "charger", 7)) k = 3; + else if (!strncmp(d, "none", 4)) k = 0; + if (k == 0xFF) { + fprintf(stderr, "[emu] Unknown --dock '%s'; expected nintendo, thirdparty, charger or none\n", d); + return 1; + } + state.dock_kind = k; + state.dock_tv = strstr(d, ":notv") == nullptr; + printf("[emu] Dock: %s%s [overrides ini]\n", d, ""); } else if (strcmp(argv[i], "--oem") == 0 && i + 1 < argc) { const char *oem = argv[++i]; if (strcmp(oem, "mariko") == 0 || strcmp(oem, "t210b01") == 0) { @@ -802,6 +820,8 @@ int main(int argc, char *argv[]) { state.emu_usec += CPU0_QUANTUM_INSTRUCTIONS / 10; state.bpmp_slept_us += CPU0_QUANTUM_INSTRUCTIONS / 10; ccplex_run(&state, state.emu_usec); + dock_tick(&state); + display_tick(&state); if (state.reboot_requested.load()) break; continue; @@ -825,6 +845,8 @@ int main(int argc, char *argv[]) { } // CPU0 catches up to the BPMP's clock. ccplex_run(&state, state.emu_usec); + dock_tick(&state); + display_tick(&state); if (state.reboot_requested.load()) break; } diff --git a/t210/dock.cpp b/t210/dock.cpp new file mode 100644 index 0000000..92e48dd --- /dev/null +++ b/t210/dock.cpp @@ -0,0 +1,1218 @@ +#include "dock.h" + +#include +#include +#include +#include +#include + +#include "../emu_state.h" +#include "memory_map.h" +#include "mmio.h" + +/* + * See dock.h. Sources: switchroot's drivers/misc/bm92txx.c (register map, + * commands, VDM formats, the Nintendo dock's VDMs), HOS's usb sysmodule as + * read in IDA (boot-time reset), hwtest's own measurements on a Mariko with + * a Nintendo dock and a Ugreen adapter (status words, DPCD, branch ids), the + * DisplayPort 1.2 DPCD map, L4T's dpaux/sor/dp drivers for the SoC side. + */ + +namespace { + +EmuState *g_state = nullptr; + +// ========================================================================== +// Time and a tiny event queue +// ========================================================================== + +uint64_t now_us() { return g_state ? g_state->emu_usec : 0; } + +struct Event { + uint64_t at; + uint32_t gen; // dropped when the chip resets + std::function fn; +}; +std::vector events; +uint32_t chip_gen = 1; + +void after_us(uint64_t delay, std::function fn) { + events.push_back({now_us() + delay, chip_gen, std::move(fn)}); +} + +FILE *trace_file() { + static FILE *f = nullptr; + static bool tried = false; + if (!tried) { + tried = true; + if (const char *p = getenv("RCM_EMU_DPTRACE")) + f = fopen(p, "w"); + } + return f; +} + +#define DLOG(...) do { printf("[dock] " __VA_ARGS__); fflush(stdout); } while (0) + +void trace(const char *what, uint32_t off, uint32_t val) { + if (FILE *f = trace_file()) { + fprintf(f, "%10llu %s %03X %08X\n", (unsigned long long)now_us(), what, off, val); + fflush(f); + } +} + +// ========================================================================== +// BM92T36 +// ========================================================================== + +constexpr uint8_t REG_ALERT = 0x02, REG_STATUS1 = 0x03, REG_STATUS2 = 0x04, + REG_COMMAND = 0x05, REG_CONFIG1 = 0x06, REG_DEV_CAPS = 0x07, + REG_PDOS_SRC = 0x08, REG_CONFIG2 = 0x17, REG_DP_STATUS = 0x18, + REG_DP_ALERT_EN = 0x19, REG_VENDOR_CFG = 0x1A, REG_1C = 0x1C, + REG_1D = 0x1D, REG_AUTO_NGT_FIXED = 0x20, REG_AUTO_NGT_BATT = 0x23, + REG_SYS_CONFIG1 = 0x26, REG_SYS_CONFIG2 = 0x27, + REG_CURRENT_PDO = 0x28, REG_CURRENT_RDO = 0x2B, + REG_ALERT_ENABLE = 0x2E, REG_SYS_CONFIG3 = 0x2F, + REG_SET_RDO = 0x30, REG_PDOS_SNK = 0x33, REG_PDOS_PROV = 0x3C, + REG_FW_TYPE = 0x4B, REG_FW_REV = 0x4C, REG_MAN_ID = 0x4D, + REG_DEV_ID = 0x4E, REG_REV_ID = 0x4F, REG_INCOMING_VDM = 0x50, + REG_OUTGOING_VDM = 0x60; + +constexpr uint16_t AL_SNK_FAULT = 1u << 0, AL_CMD_DONE = 1u << 2, + AL_PLUGPULL = 1u << 3, AL_DP_EVENT = 1u << 6, + AL_DR_SWAP = 1u << 10, AL_VDM_RECEIVED = 1u << 11, + AL_CONTRACT = 1u << 12, AL_PDO = 1u << 14; + +constexpr uint16_t ST1_INSERT = 1u << 7, ST1_VSAFE = 1u << 10, + ST1_CMD_BUSY = 1u << 13, ST1_SPDSNK = 1u << 14; +constexpr int LASTCMD_COMPLETE = 0, LASTCMD_INVALID = 4, LASTCMD_REJECTED = 6; +constexpr int DR_NONE = 0, DR_UFP = 1, DR_DFP = 2; + +constexpr uint16_t CMD_PS_RDY = 0x0505, CMD_GET_SRC_CAP = 0x0606, + CMD_SEND_RDO = 0x0707, CMD_HARD_RST = 0x0808, + CMD_SYS_RESET = 0x0D0D, CMD_SEND_VDM = 0x1111, + CMD_ACCEPT_VDM = 0x1616, CMD_DR_SWAP = 0x1818, + CMD_SOFT_RST = 0x2C2C, CMD_DP_ENTER = 0x3131, + CMD_DP_STOP = 0x3232, CMD_START_HPD = 0x3434, + CMD_DP_CFG_HPD = 0x3636, CMD_STOP_HPD = 0x3939, + CMD_STOP_HPD_EXIT_DP = 0x3B3B; + +struct Pdo { uint32_t mv, ma; bool drd; }; + +struct Bm92 { + bool reset_held = false; // PV5 high + uint64_t answers_from = 0; // I2C ACKs from here on (boot after reset) + + uint16_t alert = 0, alert_enable = 0xFFFF; + int lastcmd = LASTCMD_COMPLETE; + bool busy = false; + uint16_t config1 = 0xC0E0, config2 = 0x0000; + uint16_t sys_config1 = 0x9749, sys_config2 = 0x0244, sys_config3 = 0; + uint16_t dp_alert_en = 0, vendor_cfg = 0; + uint16_t dev_caps = 0x02A2, fw_type = 0x0603, fw_rev = 0x0D7C; + uint16_t man_id = 0x04B5, dev_id = 0x03B0, rev_id = 0x0000; + uint8_t set_rdo[5] = {4, 0, 0, 0, 0}; + uint8_t outgoing[29] = {}; + uint8_t incoming[29] = {}; + uint8_t staged[29] = {}; // a reply waiting for ACCEPT_VDM + bool staged_valid = false; + uint8_t auto_fixed[5] = {4}, auto_batt[5] = {4}; + + // The cable and the partner as the chip sees them. + bool attached = false; + int dr = DR_NONE; + int pdo_no = 0; // the PDO of the contract, 1-based + uint32_t rdo = 0; + bool nin_mode = false, dp_entered = false, dp_configured = false; + uint8_t dp_pin = 0; + bool hpd = false; + bool hub_on = false; + uint8_t led[4] = {}; +}; + +Bm92 chip; + +DockKind kind() { + return g_state ? (DockKind)g_state->dock_kind.load() : DOCK_NONE; +} +bool tv_present() { return g_state && g_state->dock_tv.load(); } +bool has_dp() { return kind() == DOCK_NINTENDO || kind() == DOCK_THIRD_PARTY; } + +std::vector partner_pdos() { + switch (kind()) { + case DOCK_NINTENDO: + // The dock's identity profile, then its one real profile: what L4T + // looks for (DOCK_ID 5 V 0.5 A, then 15 V 2.6..3 A) and what hwtest saw. + return {{5000, 500, true}, {15000, 3000, false}}; + case DOCK_THIRD_PARTY: + return {{5000, 2000, true}, {9000, 2000, false}, {15000, 2600, false}, + {20000, 4700, false}}; + case DOCK_CHARGER: + return {{5000, 1500, false}, {15000, 2600, false}}; + default: + if (g_state && g_state->usb_pd_inserted.load()) + return {{g_state->usb_pd_voltage_mv.load(), g_state->usb_pd_amperage_ma.load(), false}}; + return {}; + } +} + +bool cable_in() { return kind() != DOCK_NONE || (g_state && g_state->usb_pd_inserted.load()); } + +uint32_t pdo_word(const Pdo &p) { + uint32_t info = p.drd ? ((1u << 5) | (1u << 6) | (1u << 7)) : 0; + return ((p.ma / 10) & 0x3FF) | (((p.mv / 50) & 0x3FF) << 10) | (info << 20); +} + +uint16_t status1() { + uint16_t s = 0; + if (chip.attached) + s |= ST1_INSERT | ST1_VSAFE | ST1_SPDSNK; + s |= (uint16_t)((chip.dr & 3) << 8); + s |= (uint16_t)((chip.lastcmd & 7) << 4); + if (chip.busy) + s |= ST1_CMD_BUSY; + return s; +} + +uint16_t dp_status() { + if (!chip.dp_entered) + return 0; + uint16_t s = (1u << 15) | (1u << 14); // DP_EN, INSERT + if (chip.dp_configured) + s |= (1u << 1) | (chip.dp_pin == 0x04 ? 0x0C : 0x18); // configured, pins + if (chip.hpd) + s |= 1u << 7; // SIGNAL_ON + return s; +} + +void raise(uint16_t bits) { + chip.alert |= bits; +} + +void put_le(uint8_t *p, uint32_t v, int n) { + for (int i = 0; i < n; i++) + p[i] = (uint8_t)(v >> (8 * i)); +} + +// A structured VDM reply as INCOMING_VDM holds it: length, header, VDOs. +void stage_reply(uint8_t cmd_ack, uint8_t hdr2, uint16_t svid, + const std::vector &vdos) { + memset(chip.staged, 0, sizeof(chip.staged)); + chip.staged[0] = (uint8_t)(4 + 4 * vdos.size()); + chip.staged[1] = cmd_ack; + chip.staged[2] = hdr2; + chip.staged[3] = (uint8_t)svid; + chip.staged[4] = (uint8_t)(svid >> 8); + for (size_t i = 0; i < vdos.size() && 5 + 4 * i + 4 <= sizeof(chip.staged); i++) + put_le(&chip.staged[5 + 4 * i], vdos[i], 4); + chip.staged_valid = true; +} + +void hpd_update(bool level) { + if (chip.hpd == level) + return; + chip.hpd = level; + DLOG("HPD %s\n", level ? "high (TV on the dock)" : "low"); + if (chip.dp_alert_en) + raise(AL_DP_EVENT); +} + +// Cable on: the chip comes up on 5 V and reports the partner's offer. +void attach() { + chip.attached = true; + chip.dr = DR_UFP; + chip.pdo_no = 1; + chip.lastcmd = LASTCMD_COMPLETE; + raise(AL_PLUGPULL | AL_CONTRACT | AL_PDO); + DLOG("cable in: %s, %zu PDO(s)\n", + kind() == DOCK_NINTENDO ? "Nintendo dock 057E:2003" + : kind() == DOCK_THIRD_PARTY ? "adapter 1D5C:7102" + : kind() == DOCK_CHARGER ? "Nintendo charger 057E:2004" : "power source", + partner_pdos().size()); +} + +void chip_reset(bool from_pin) { + chip_gen++; // pending events die with the old state + hpd_update(false); + bool held = chip.reset_held; + chip = Bm92(); + chip.reset_held = held; + uint64_t boot = from_pin ? 100000 : 30000; + chip.answers_from = now_us() + (from_pin ? 100000 : 0); + if (cable_in()) + after_us(boot, [] { attach(); }); +} + +// ---- the partner's answers to VDMs ---------------------------------------- + +void partner_vdm(const uint8_t *msg, uint32_t len) { + if (len < 4) + return; + uint8_t cmd = msg[0], type = msg[1]; + uint16_t svid = (uint16_t)(msg[2] | (msg[3] << 8)); + bool structured = type & 0x80; + DockKind k = kind(); + if (!structured) { + // Nintendo dock vendor messages: [REQ, 0, 7E 05, rw, src, cmd, 0, ...] + if (k != DOCK_NINTENDO || svid != 0x057E || len < 8 || !chip.nin_mode) + return; + uint8_t ncmd = msg[6]; + memset(chip.staged, 0, sizeof(chip.staged)); + uint8_t data[4] = {}; + int size = 8; + switch (ncmd) { + case 0x16: // device state: what hwtest read + data[0] = 0x00; data[1] = 0x00; data[2] = 0x1D; data[3] = 0x00; + size = 12; + break; + case 0x01: // LED control + memcpy(chip.led, &msg[8], len >= 12 ? 4 : 0); + DLOG("dock LED set %02X %02X %02X %02X\n", msg[8], msg[9], msg[10], msg[11]); + size = 12; + break; + case 0x20: // USB hub control + chip.hub_on = msg[4] == 1; + DLOG("dock USB hub %s\n", chip.hub_on ? "on" : "off"); + break; + case 0x1C: case 0x1E: // DP signal disable, hub reset + break; + default: + return; + } + chip.staged[0] = (uint8_t)size; + chip.staged[1] = 0x40; // REPLY + chip.staged[2] = 0x00; + chip.staged[3] = 0x7E; + chip.staged[4] = 0x05; + chip.staged[5] = 0x00; // not busy + chip.staged[6] = 0x02; // from the dock + chip.staged[7] = (uint8_t)(ncmd + 1); + chip.staged[8] = 0x00; + if (size == 12) + memcpy(&chip.staged[9], data, 4); + chip.staged_valid = true; + return; + } + uint8_t ack = (uint8_t)(0x40 | (cmd & 0x1F)); + switch (cmd & 0x1F) { + case 0x01: { // Discover Identity + uint16_t vid = k == DOCK_THIRD_PARTY ? 0x1D5C : 0x057E; + uint16_t pid = k == DOCK_THIRD_PARTY ? 0x7102 : k == DOCK_CHARGER ? 0x2004 : 0x2003; + uint32_t type_ama = k == DOCK_CHARGER ? 0 : 5; + uint32_t idh = vid | (1u << 26) | (type_ama << 27) | (1u << 30); + stage_reply(ack, 0x80, 0xFF00, {idh, 0x00000000, (uint32_t)pid << 16 | 0x0100, 0x00000000}); + break; + } + case 0x02: // Discover SVIDs + if (k == DOCK_THIRD_PARTY) + stage_reply(ack, 0x80, 0xFF00, {0x0000FF01}); + else if (k == DOCK_NINTENDO) + stage_reply(ack, 0x80, 0xFF00, {0xFF01057E}); + else + stage_reply(ack, 0x80, 0xFF00, {0x0000057E}); + break; + case 0x03: // Discover Modes + if (svid == 0xFF01 && has_dp()) + // The Nintendo dock reports a plug but puts its pins in the receptacle + // field (L4T: "dock reports Plug but uses Receptacle"); both are set. + stage_reply(ack, 0x80, 0xFF01, {k == DOCK_NINTENDO ? 0x000C0C05u : 0x000C0045u}); + else + stage_reply((uint8_t)(0x80 | (cmd & 0x1F)), 0x80, svid, {}); + break; + case 0x04: // Enter Mode + if (svid == 0x057E && k == DOCK_NINTENDO) { + chip.nin_mode = true; + DLOG("Nintendo alt mode entered\n"); + stage_reply(ack, 0x81, 0x057E, {}); + } else { + stage_reply((uint8_t)(0x80 | 0x04), (uint8_t)(0x80 | (type & 7)), svid, {}); + } + break; + case 0x05: // Exit Mode + if (svid == 0x057E) + chip.nin_mode = false; + stage_reply(ack, type, svid, {}); + break; + default: + stage_reply((uint8_t)(0x80 | (cmd & 0x1F)), type, svid, {}); + break; + } +} + +// ---- commands -------------------------------------------------------------- + +void done(int lastcmd, uint16_t extra = 0) { + chip.busy = false; + chip.lastcmd = lastcmd; + raise(AL_CMD_DONE | extra); +} + +void command(uint16_t cmd) { + if (!chip.attached && cmd != CMD_SYS_RESET) { + after_us(2000, [] { done(LASTCMD_INVALID); }); + return; + } + chip.busy = true; + switch (cmd) { + case CMD_SYS_RESET: + DLOG("SYS_RESET\n"); + chip_reset(false); + break; + case CMD_SEND_RDO: { + uint32_t rdo = chip.set_rdo[1] | chip.set_rdo[2] << 8 | chip.set_rdo[3] << 16 | + (uint32_t)chip.set_rdo[4] << 24; + int obj = (int)(rdo >> 28); + auto pdos = partner_pdos(); + if (obj < 1 || obj > (int)pdos.size()) { + after_us(20000, [] { done(LASTCMD_REJECTED); }); + break; + } + after_us(25000, [obj, rdo] { + chip.pdo_no = obj; + chip.rdo = rdo; + auto p = partner_pdos()[obj - 1]; + DLOG("contract: PDO %d, %u mV %u mA, drawing up to %u mA\n", obj, p.mv, p.ma, + ((rdo >> 10) & 0x3FF) * 10); + done(LASTCMD_COMPLETE, AL_CONTRACT); + }); + break; + } + case CMD_PS_RDY: + case CMD_GET_SRC_CAP: + case CMD_START_HPD: + case CMD_STOP_HPD: + after_us(3000, [] { done(LASTCMD_COMPLETE); }); + break; + case CMD_DR_SWAP: + after_us(10000, [] { + bool drd = !partner_pdos().empty() && partner_pdos()[0].drd; + if (!drd) { + done(LASTCMD_REJECTED); + return; + } + chip.dr = chip.dr == DR_UFP ? DR_DFP : DR_UFP; + DLOG("data role swap: console is now %s\n", chip.dr == DR_DFP ? "the host (DFP)" : "a device (UFP)"); + done(LASTCMD_COMPLETE, AL_DR_SWAP); + }); + break; + case CMD_SEND_VDM: { + uint8_t len = chip.outgoing[0]; + std::vector msg(chip.outgoing + 1, chip.outgoing + 1 + (len > 28 ? 28 : len)); + after_us(2000, [] { done(LASTCMD_COMPLETE); }); + after_us(8000, [msg] { + partner_vdm(msg.data(), (uint32_t)msg.size()); + if (chip.staged_valid) + raise(AL_VDM_RECEIVED); + }); + break; + } + case CMD_ACCEPT_VDM: + after_us(2000, [] { + if (chip.staged_valid) { + memcpy(chip.incoming, chip.staged, sizeof(chip.incoming)); + chip.staged_valid = false; + } + done(LASTCMD_COMPLETE); + }); + break; + case CMD_DP_ENTER: + // The chip runs DP discovery itself and leaves the partner's DP + // capabilities VDO in INCOMING_VDM, without a VDM header. + after_us(60000, [] { + if (!has_dp() || chip.dr != DR_DFP) { + done(LASTCMD_REJECTED); + return; + } + chip.dp_entered = true; + memset(chip.incoming, 0, sizeof(chip.incoming)); + chip.incoming[0] = 4; + uint32_t caps = kind() == DOCK_NINTENDO ? 0x000C0C05u : 0x000C0045u; + put_le(&chip.incoming[1], caps, 4); + DLOG("DisplayPort alt mode entered, caps %08X\n", caps); + done(LASTCMD_COMPLETE); + }); + break; + case CMD_DP_CFG_HPD: { + uint8_t b0 = chip.outgoing[1], b1 = chip.outgoing[2], b2 = chip.outgoing[3]; + after_us(40000, [b0, b1, b2] { + // The Nintendo dock refused a configure VDO without "UFP_U as UFP_D" + // (bit 1); it ignores where the pins are. A plain adapter wants them + // in the byte its capabilities named (receptacle here). + uint8_t pin = kind() == DOCK_NINTENDO ? (b1 | b2) : b1; + if (!chip.dp_entered || !(b0 & 0x02) || !(b0 & 0x04) || !(pin & 0x0C)) { + DLOG("DP configure refused (VDO %02X %02X %02X)\n", b0, b1, b2); + done(LASTCMD_REJECTED); + return; + } + chip.dp_configured = true; + chip.dp_pin = (pin & 0x08) ? 0x08 : 0x04; + DLOG("DisplayPort configured, pin %c\n", chip.dp_pin == 0x08 ? 'D' : 'C'); + done(LASTCMD_COMPLETE); + if (tv_present()) + after_us(150000, [] { if (chip.dp_configured) hpd_update(true); }); + }); + break; + } + case CMD_DP_STOP: + case CMD_STOP_HPD_EXIT_DP: + after_us(10000, [] { + chip.dp_configured = chip.dp_entered = false; + hpd_update(false); + done(LASTCMD_COMPLETE); + }); + break; + case CMD_HARD_RST: + case CMD_SOFT_RST: + after_us(30000, [] { + chip.dp_configured = chip.dp_entered = chip.nin_mode = false; + hpd_update(false); + chip.dr = DR_UFP; + chip.pdo_no = 1; + done(LASTCMD_COMPLETE, AL_CONTRACT | AL_PDO); + }); + break; + default: + after_us(2000, [] { done(LASTCMD_INVALID); }); + break; + } +} + +uint32_t pdo_bytes(uint8_t *out, uint32_t cap) { + auto pdos = partner_pdos(); + uint32_t n = 0; + out[n++] = (uint8_t)(4 * pdos.size()); + for (auto &p : pdos) + if (n + 4 <= cap) { + put_le(&out[n], pdo_word(p), 4); + n += 4; + } + return n; +} + +// ========================================================================== +// DisplayPort sink: the dock's converter and the TV behind it +// ========================================================================== + +uint8_t dpcd[0x800]; +uint8_t edid[256]; +uint32_t edid_ptr = 0; +uint32_t aux_i2c_addr = 0; +uint64_t crc_frames_base = 0; + +void edid_build() { + static const uint8_t base[128] = { + 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x15, 0xB5, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x1E, 0x01, 0x03, 0x80, 0x50, 0x2D, 0x78, + 0x0A, 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54, 0x21, + 0x08, 0x00, 0x81, 0xC0, 0xD1, 0xC0, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + // 1920x1080 at 60 Hz, 148.5 MHz + 0x02, 0x3A, 0x80, 0x18, 0x71, 0x38, 0x2D, 0x40, 0x58, 0x2C, 0x45, 0x00, + 0x20, 0xC2, 0x31, 0x00, 0x00, 0x1E, + // 1280x720 at 60 Hz, 74.25 MHz + 0x01, 0x1D, 0x00, 0x72, 0x51, 0xD0, 0x1E, 0x20, 0x6E, 0x28, 0x55, 0x00, + 0x20, 0xC2, 0x31, 0x00, 0x00, 0x1E, + // name "EMULATED TV" + 0x00, 0x00, 0x00, 0xFC, 0x00, 'E', 'M', 'U', 'L', 'A', 'T', 'E', 'D', + ' ', 'T', 'V', 0x0A, 0x20, + // range limits 24-75 Hz, 15-81 kHz, 170 MHz + 0x00, 0x00, 0x00, 0xFD, 0x00, 0x18, 0x4B, 0x0F, 0x51, 0x11, 0x00, 0x0A, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x01, 0x00}; + memcpy(edid, base, 128); + // CEA-861 extension: 1080p60 (native), 720p60, 480p, 640x480; HDMI VSDB. + static const uint8_t cea[] = {0x02, 0x03, 0x0F, 0x00, 0x44, 0x90, 0x04, 0x03, + 0x01, 0x65, 0x03, 0x0C, 0x00, 0x10, 0x00}; + memset(edid + 128, 0, 128); + memcpy(edid + 128, cea, sizeof(cea)); + for (int b = 0; b < 2; b++) { + uint8_t sum = 0; + for (int i = 0; i < 127; i++) + sum += edid[b * 128 + i]; + edid[b * 128 + 127] = (uint8_t)(0x100 - sum); + } +} + +void dpcd_build() { + memset(dpcd, 0, sizeof(dpcd)); + bool nin = kind() == DOCK_NINTENDO; + dpcd[0x000] = 0x12; // DPCD 1.2 + dpcd[0x001] = 0x14; // HBR2 + dpcd[0x002] = (uint8_t)(0xC0 | (nin ? 4 : 2)); // enhanced framing, TPS3, lanes + dpcd[0x003] = 0x01; // 0.5% down-spread + dpcd[0x004] = 0x01; + dpcd[0x005] = 0x05; // a downstream port, DVI/HDMI + dpcd[0x006] = 0x01; // 8b/10b + dpcd[0x007] = 0x01; // one downstream port + dpcd[0x008] = 0x02; + dpcd[0x00A] = 0x06; + dpcd[0x00E] = 0x00; // 100/400 us training waits + dpcd[0x080] = 0x0B; // DFP0: HDMI, HPD aware + dpcd[0x101] = 0x00; + dpcd[0x108] = 0x01; + dpcd[0x246] = 0x20; // TEST_CRC_SUPPORTED + dpcd[0x500] = 0x00; dpcd[0x501] = 0xE0; dpcd[0x502] = 0x4C; // Realtek + memcpy(&dpcd[0x503], nin ? "Dp1.2" : "Dp1.4", 5); + dpcd[0x600] = 0x01; +} + +bool sink_reachable() { + return has_dp() && chip.dp_configured && tv_present() && chip.hpd; +} + +// ========================================================================== +// SOR1 and the link +// ========================================================================== + +constexpr uint64_t SOR1_BASE_ADDR = 0x54580000; +uint32_t sor[0x400]; // word registers +bool lanes_powered = false; // the lane sequencer's last PU +uint32_t sor_super_state1 = 0; // committed by SUPER_STATE0.UPDATE +uint32_t sor_state1 = 0; // committed by STATE0.UPDATE +uint32_t lane_status[4] = {}; // CR(1) EQ(2) SYMLOCK(4) per lane + +enum : uint32_t { + S_SUPER_STATE0 = 0x01, S_SUPER_STATE1 = 0x02, S_STATE0 = 0x03, S_STATE1 = 0x04, + S_CRC_CNTRL = 0x11, S_CLK_CNTRL = 0x13, S_PWR = 0x15, S_TEST = 0x16, + S_PLL0 = 0x17, S_PLL1 = 0x18, S_PLL2 = 0x19, S_PLL3 = 0x1A, S_CRCA = 0x1D, + S_CRCB = 0x1E, S_SEQ_CTL = 0x20, S_LANE_SEQ_CTL = 0x21, S_XBAR_CTRL = 0x4A, + S_LINKCTL0 = 0x4C, S_DC0 = 0x4E, S_PR0 = 0x52, S_DP_CONFIG0 = 0x58, + S_PADCTL0 = 0x5C, S_SPARE0 = 0x60, S_TPG = 0x6D, +}; + +uint32_t car(uint32_t off) { return mmio_bus_read(g_state, CLK_RST_BASE + off, 4); } + +// The physical pad that carries logical lane `l` (see dock.h): through the +// crossbar unless it is in bypass. In bypass, and with XSEL as the identity, +// logical lane n leaves on the pad the TRM names "DP lane n" - pad 2 for 0. +int pad_of_lane(uint32_t l) { + static const int phys_of_index[4] = {2, 1, 0, 3}; + uint32_t x = sor[S_XBAR_CTRL]; + if (x & 1) + return phys_of_index[l & 3]; + for (int n = 0; n < 4; n++) + if (((x >> (2 + 3 * n)) & 7) == l) + return phys_of_index[n]; + return -1; +} + +int lanes_of(uint32_t lanecount_field) { + switch (lanecount_field) { + case 1: return 1; + case 3: return 2; + case 15: return 4; + default: return 0; + } +} + +// Why connector lane j would not carry a signal, or nullptr. The Switch +// wires DP lane j to physical pad j. +const char *lane_fault(uint32_t j) { + if (!(car(0x280) & (1u << 23))) return "SOR1 clock off"; + if (car(0x28C) & (1u << 23)) return "SOR1 held in reset"; + if (!(car(0x410) & (1u << 14))) return "SOR1 not on the brick (macro) clock"; + if (!(car(0x590) & (1u << 27))) return "PLLDP not locked"; + if (sor[S_PLL0] & 5) return "SOR macro PLL powered down"; + if (sor[S_PLL2] & (3u << 22)) return "SOR bandgap or port powered down"; + if (!lanes_powered) return "lane sequencer never powered the lanes up"; + if (!(sor[S_LINKCTL0] & 1)) return "DP link not enabled (LINKCTL)"; + if ((int)j >= lanes_of((sor[S_LINKCTL0] >> 16) & 0x1F)) return "lane beyond LINKCTL's lane count"; + int pad = pad_of_lane(j); + if (pad != (int)j) return "crossbar sends this lane to the wrong pad"; + if (!(sor[S_PADCTL0] & (1u << pad))) return "the lane's pad is powered down (PADCTL)"; + if (!((sor[S_DC0] >> (8 * pad)) & 0xFF)) return "no drive current on the lane's pad"; + uint32_t speed = (sor[S_CLK_CNTRL] >> 2) & 0x1F; + if (speed != dpcd[0x100]) return "SOR link speed differs from LINK_BW_SET"; + return nullptr; +} + +// The pixel clock display head B runs at: CLK_SOURCE_DISP2 (0x13C) picks +// the source, PLLD2 (BASE 0x4B8, SS_CFG 0x570, SS_CTRL2 0x578) makes it. +// Fvco = Fref / M * (N + 0.5 + SDM_DIN / 8192) with the sigma-delta on. +// Fref is the 38.4 MHz oscillator after OSC_CTRL's PLL_REF_DIV (27:26), +// /1 on the Switch: coreboot's 38.4 MHz table runs PLLDP at M = 2 to stay +// inside its 12..38 MHz comparison window, and SOR1 trained at 2.7 Gbps on +// a PLLDP set up that way. The post divider is the kernel's "qlin" map +// (clk-tegra210.c pll_qlin_pdiv_to_hw): code 8 is /10, code 9 is /12. +uint64_t head_b_pclk_khz() { + static const uint32_t qlin[17] = {1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 30, 32}; + uint32_t src = car(0x13C) >> 29; + if (src != 5) + return 0; // only PLLD2_OUT0 is modelled + uint32_t base = car(0x4B8); + if (!(base & (1u << 30))) + return 0; + uint32_t m = base & 0xFF, n = (base >> 8) & 0xFF, p = (base >> 19) & 0x1F; + if (!m || p > 16) + return 0; + uint64_t fref_hz = 38400000ULL >> ((car(0x050) >> 26) & 3); + uint64_t num = (uint64_t)n * 8192; + if (car(0x570) & (1u << 31)) // EN_SDM + num += 4096 + (int16_t)(car(0x578) & 0xFFFF); + uint64_t vco_hz = fref_hz * num / 8192 / m; + uint32_t div = car(0x13C) & 0xFF; // DISP2_CLK_DIVISOR: /(N/2+1) + uint64_t hz = vco_hz / qlin[p] * 2 / (div + 2); + return hz / 1000; +} + +uint32_t lane_pattern(uint32_t j) { return (sor[S_TPG] >> (8 * j)) & 0xF; } + +uint32_t dpcd_lanes() { return dpcd[0x101] & 0x1F; } + +// Re-evaluate the sink's per-lane status after anything the link depends on. +void link_eval() { + uint32_t n = dpcd_lanes(); + uint32_t want = dpcd[0x102] & 3; + for (uint32_t j = 0; j < 4; j++) { + if (j >= n || !sink_reachable() || lane_fault(j)) { + lane_status[j] = 0; + continue; + } + uint32_t sent = lane_pattern(j); + if (want == 1 && sent == 1) + lane_status[j] |= 1; + else if ((want == 2 || want == 3) && sent == want && (lane_status[j] & 1)) + lane_status[j] |= 6; + else if (want == 0 && sent == 0 && (sor[S_TPG] >> (8 * j) & 0x10)) { + // normal operation: the lane keeps what it trained to + } else if (want == 0 && sent != 0) { + lane_status[j] &= 1; + } + } +} + +bool link_trained(uint32_t *lanes_out = nullptr) { + uint32_t n = dpcd_lanes(); + if (lanes_out) + *lanes_out = n; + if (!n || !sink_reachable()) + return false; + link_eval(); + for (uint32_t j = 0; j < n; j++) + if ((lane_status[j] & 7) != 7) + return false; + return true; +} + +uint8_t dpcd_read_byte(uint32_t a) { + link_eval(); + switch (a) { + case 0x200: return tv_present() ? 0x41 : 0x00; // one sink, CP ready + case 0x202: return (uint8_t)(lane_status[0] | lane_status[1] << 4); + case 0x203: return (uint8_t)(lane_status[2] | lane_status[3] << 4); + case 0x204: { + uint32_t n = dpcd_lanes(); + bool all = n > 0; + for (uint32_t j = 0; j < n; j++) + all &= (lane_status[j] & 7) == 7; + return (uint8_t)((all ? 1 : 0) | 0x80); + } + case 0x205: return link_trained() && (dpcd[0x102] & 3) == 0 ? 1 : 0; + case 0x206: case 0x207: return 0; // keep the levels + case 0x246: { + TvState t; + dock_tv_state(g_state, &t); + uint8_t c = 0; + if (t.video && (dpcd[0x270] & 1)) + c = (uint8_t)(((now_us() - crc_frames_base) / 16667) & 0xF); + return (uint8_t)(0x20 | c); + } + default: + if (a >= 0x240 && a < 0x246) + return (dpcd[0x270] & 1) ? 0x5A : 0; + return a < sizeof(dpcd) ? dpcd[a] : 0; + } +} + +void dpcd_write_byte(uint32_t a, uint8_t v) { + if (a >= sizeof(dpcd)) + return; + if (a == 0x270 && (v & 1) && !(dpcd[0x270] & 1)) + crc_frames_base = now_us(); + if (a == 0x100 || a == 0x101) { + for (auto &s : lane_status) + s = 0; + } + dpcd[a] = v; + if (a == 0x102 && (v & 3) == 1) + for (auto &s : lane_status) + s = 0; +} + +// ========================================================================== +// DPAUX1 +// ========================================================================== + +constexpr uint64_t DPAUX1_BASE_ADDR = 0x54040000; +uint32_t aux[0x80]; + +enum : uint32_t { + A_INTR_EN = 0x01, A_INTR = 0x05, A_DATA_WR = 0x09, A_DATA_RD = 0x19, + A_ADDR = 0x29, A_CTL = 0x2D, A_STAT = 0x31, A_HPD_CONFIG = 0x3D, + A_HPD_IRQ_CONFIG = 0x41, A_PADCTL = 0x49, A_SPARE = 0x4D, +}; + +bool hpd_pad_sfio() { + // GPIO CC1: bank 7, port 0 in bank. GPIO_CNF bit 1 clear = SFIO. + return !(mmio_bus_read(g_state, GPIO_BASE + 0x700, 4) & (1u << 1)); +} + +bool dpaux_clocked() { + return (car(0x298) & (1u << 15)) && !(car(0x2A4) & (1u << 15)); +} + +uint32_t aux_hpd_bit() { + return (chip.hpd && hpd_pad_sfio() && dpaux_clocked()) ? (1u << 28) : 0; +} + +void aux_transact() { + uint32_t ctl = aux[A_CTL]; + uint32_t cmd = (ctl >> 12) & 0xF; + uint32_t len = (ctl & 0xFF) + 1; + uint32_t addr = aux[A_ADDR] & 0xFFFFF; + bool address_only = ctl & (1u << 8); + uint8_t wr[16], rd[16] = {}; + for (int i = 0; i < 16; i++) + wr[i] = (uint8_t)(aux[A_DATA_WR + 4 * (i / 4)] >> (8 * (i % 4))); + uint32_t stat = 0, got = 0; + bool pad_up = !(aux[A_SPARE] & 1); + if (!dpaux_clocked() || !pad_up || !sink_reachable()) { + stat = 1u << 8; // TIMEOUT_ERROR + } else if (cmd == 8) { // AUXWR + for (uint32_t i = 0; i < len && i < 16; i++) + dpcd_write_byte(addr + i, wr[i]); + stat = 0; + } else if (cmd == 9) { // AUXRD + for (uint32_t i = 0; i < len && i < 16; i++) + rd[i] = dpcd_read_byte(addr + i); + got = len > 16 ? 16 : len; + } else { // I2C over AUX + bool write = cmd == 0 || cmd == 4; + bool read = cmd == 1 || cmd == 5; + if (addr != 0x50 && addr != 0x30) { + stat = 4u << 16; // I2C NACK + } else if (address_only) { + aux_i2c_addr = addr; + } else if (write) { + if (addr == 0x50) + edid_ptr = wr[0]; + else + edid_ptr = (uint32_t)wr[0] * 256; // E-DDC segment + } else if (read && addr == 0x50) { + for (uint32_t i = 0; i < len && i < 16; i++) + rd[i] = edid[(edid_ptr + i) & 0xFF]; + edid_ptr += len; + got = len > 16 ? 16 : len; + } + } + for (int w = 0; w < 4; w++) { + uint32_t v = 0; + for (int b = 0; b < 4; b++) + v |= (uint32_t)rd[w * 4 + b] << (8 * b); + aux[A_DATA_RD + 4 * w] = v; + } + aux[A_STAT] = stat | got; + aux[A_CTL] &= ~(1u << 16); + aux[A_INTR] |= 1u << 3; // TX_DONE + trace("aux", cmd << 20 | (addr & 0xFFFF), len | stat); +} + +// ========================================================================== + +void tick_events() { + for (;;) { + size_t best = events.size(); + for (size_t i = 0; i < events.size(); i++) + if (events[i].at <= now_us() && (best == events.size() || events[i].at < events[best].at)) + best = i; + if (best == events.size()) + return; + Event e = std::move(events[best]); + events.erase(events.begin() + (long)best); + if (e.gen == chip_gen) + e.fn(); + } +} + +bool last_cable = false; +DockKind last_kind = DOCK_NONE; + +} // namespace + +// ========================================================================== + +void dock_reset(EmuState *state) { + g_state = state; + events.clear(); + chip = Bm92(); + memset(sor, 0, sizeof(sor)); + memset(aux, 0, sizeof(aux)); + sor[S_XBAR_CTRL] = 0x8D111A23; // TRM reset: bypass + link swap + sor[S_PLL0] = 0x5; // PLL and VCO powered down + sor[S_PLL2] = 3u << 22; + sor[S_SPARE0] = 0x2; + sor[S_STATE1] = 0x40; + aux[A_SPARE] = 0; // DPAUX pad powered at reset + lanes_powered = false; + sor_super_state1 = sor_state1 = 0; + memset(lane_status, 0, sizeof(lane_status)); + dpcd_build(); + edid_build(); + last_cable = cable_in(); + last_kind = kind(); + chip_reset(false); + chip.answers_from = 0; +} + +void dock_tick(EmuState *state) { + g_state = state; + if (kind() != last_kind || cable_in() != last_cable) { + // The config window (or ini) swapped what is on the cable. + bool was = last_cable; + last_kind = kind(); + last_cable = cable_in(); + dpcd_build(); + if (!last_cable && was) { + chip_gen++; + chip.attached = false; + chip.dr = DR_NONE; + chip.dp_entered = chip.dp_configured = chip.nin_mode = false; + hpd_update(false); + chip.alert |= AL_PLUGPULL; + DLOG("cable out\n"); + } else if (last_cable && !chip.reset_held) { + after_us(30000, [] { attach(); }); // debounce, then the chip reports it + } + } + if (!events.empty()) + tick_events(); +} + +bool bm92_present(EmuState *state) { + g_state = state; + dock_tick(state); + return !chip.reset_held && now_us() >= chip.answers_from; +} + +void bm92_reset_line(EmuState *state, bool high) { + g_state = state; + if (high == chip.reset_held) + return; + chip.reset_held = high; + if (high) { + chip_gen++; + events.clear(); + hpd_update(false); + chip = Bm92(); + chip.reset_held = true; + DLOG("PV5 high: PD controller held in reset\n"); + } else { + DLOG("PV5 low: PD controller boots\n"); + chip_reset(true); + } +} + +void bm92_write(EmuState *state, const uint8_t *buf, uint32_t n) { + g_state = state; + dock_tick(state); + if (!n || !bm92_present(state)) + return; + uint8_t reg = buf[0]; + const uint8_t *d = buf + 1; + uint32_t m = n - 1; + auto w16 = [&](uint16_t &r) { if (m >= 2) r = (uint16_t)(d[0] | d[1] << 8); }; + auto blk = [&](uint8_t *r, uint32_t cap) { + memset(r, 0, cap); + for (uint32_t i = 0; i < m && i < cap; i++) + r[i] = d[i]; + }; + switch (reg) { + case REG_COMMAND: + if (m >= 2) + command((uint16_t)(d[0] | d[1] << 8)); + break; + case REG_CONFIG1: w16(chip.config1); break; + case REG_CONFIG2: w16(chip.config2); break; + case REG_DP_ALERT_EN: w16(chip.dp_alert_en); break; + case REG_VENDOR_CFG: w16(chip.vendor_cfg); break; + case REG_SYS_CONFIG1: w16(chip.sys_config1); break; + case REG_SYS_CONFIG2: w16(chip.sys_config2); break; + case REG_SYS_CONFIG3: w16(chip.sys_config3); break; + case REG_ALERT_ENABLE: w16(chip.alert_enable); break; + case REG_SET_RDO: if (m) blk(chip.set_rdo, sizeof(chip.set_rdo)); break; + case REG_OUTGOING_VDM: if (m) blk(chip.outgoing, sizeof(chip.outgoing)); break; + case REG_AUTO_NGT_FIXED: if (m) blk(chip.auto_fixed, sizeof(chip.auto_fixed)); break; + case REG_AUTO_NGT_BATT: if (m) blk(chip.auto_batt, sizeof(chip.auto_batt)); break; + default: break; + } +} + +void bm92_read(EmuState *state, uint8_t reg, uint8_t *out, uint32_t n) { + g_state = state; + dock_tick(state); + memset(out, 0, n); + uint8_t tmp[32] = {}; + uint32_t len = 2; + auto v16 = [&](uint16_t v) { tmp[0] = (uint8_t)v; tmp[1] = (uint8_t)(v >> 8); }; + switch (reg) { + case REG_ALERT: + v16(chip.alert); + chip.alert = 0; // clear on read (L4T relies on it) + break; + case REG_STATUS1: v16(status1()); break; + case REG_STATUS2: v16(0); break; + case REG_CONFIG1: v16(chip.config1); break; + case REG_DEV_CAPS: v16(chip.dev_caps); break; + case REG_CONFIG2: v16(chip.config2); break; + case REG_DP_STATUS: v16(dp_status()); break; + case REG_DP_ALERT_EN: v16(chip.dp_alert_en); break; + case REG_VENDOR_CFG: v16(chip.vendor_cfg); break; + case REG_1C: v16(chip.dp_entered ? 0x0C05 : 0); break; + case REG_1D: v16(0); break; + case REG_SYS_CONFIG1: v16(chip.sys_config1); break; + case REG_SYS_CONFIG2: v16(chip.sys_config2); break; + case REG_SYS_CONFIG3: v16(chip.sys_config3); break; + case REG_ALERT_ENABLE: v16(chip.alert_enable); break; + case REG_FW_TYPE: v16(chip.fw_type); break; + case REG_FW_REV: v16(chip.fw_rev); break; + case REG_MAN_ID: v16(chip.man_id); break; + case REG_DEV_ID: v16(chip.dev_id); break; + case REG_REV_ID: v16(chip.rev_id); break; + case REG_PDOS_SRC: len = chip.attached ? pdo_bytes(tmp, sizeof(tmp)) : 1; break; + case REG_CURRENT_PDO: { + auto pdos = partner_pdos(); + tmp[0] = 4; + if (chip.attached && chip.pdo_no >= 1 && chip.pdo_no <= (int)pdos.size()) + put_le(&tmp[1], pdo_word(pdos[chip.pdo_no - 1]), 4); + len = 5; + break; + } + case REG_CURRENT_RDO: + tmp[0] = 4; + put_le(&tmp[1], chip.rdo, 4); + len = 5; + break; + case REG_SET_RDO: memcpy(tmp, chip.set_rdo, 5); len = 5; break; + case REG_INCOMING_VDM: memcpy(tmp, chip.incoming, 29); len = 29; break; + case REG_OUTGOING_VDM: memcpy(tmp, chip.outgoing, 29); len = 29; break; + case REG_AUTO_NGT_FIXED: memcpy(tmp, chip.auto_fixed, 5); len = 5; break; + case REG_AUTO_NGT_BATT: memcpy(tmp, chip.auto_batt, 5); len = 5; break; + default: len = 0; break; + } + for (uint32_t i = 0; i < n && i < len && i < sizeof(tmp); i++) + out[i] = tmp[i]; +} + +bool bm92_alert_line(EmuState *state) { + g_state = state; + dock_tick(state); + return !(chip.alert & chip.alert_enable) || chip.reset_held; +} + +bool dock_hpd(EmuState *state) { + g_state = state; + dock_tick(state); + return chip.hpd; +} + +uint32_t dpaux1_read(EmuState *state, uint32_t off) { + g_state = state; + dock_tick(state); + uint32_t r = (off >> 2) & 0x7F; + switch (r) { + case A_STAT: return (aux[A_STAT] & ~(1u << 28)) | aux_hpd_bit(); + case A_INTR: { + uint32_t v = aux[A_INTR]; + // Plug/unplug events latch on HPD edges. + static bool last = false; + bool h = aux_hpd_bit() != 0; + if (h != last) { + aux[A_INTR] |= h ? 1u : 2u; + last = h; + v = aux[A_INTR]; + } + return v; + } + default: return aux[r]; + } +} + +void dpaux1_write(EmuState *state, uint32_t off, uint32_t val) { + g_state = state; + dock_tick(state); + uint32_t r = (off >> 2) & 0x7F; + trace("aux-w", off, val); + switch (r) { + case A_INTR: + aux[A_INTR] &= ~val; // write 1 to clear + break; + case A_CTL: + aux[A_CTL] = val; + if (val & (1u << 31)) // RST + break; + if (val & (1u << 16)) + aux_transact(); + break; + case A_STAT: + break; + default: + aux[r] = val; + break; + } +} + +uint32_t sor1_read(EmuState *state, uint32_t off) { + g_state = state; + dock_tick(state); + uint32_t r = (off >> 2) & 0x3FF; + switch (r) { + case S_SUPER_STATE0: + case S_STATE0: + return 0; // UPDATE self-clears + case S_PWR: { + uint32_t v = sor[S_PWR] & ~(1u << 31); // SETTING_NEW done at once + if ((sor_super_state1 & 0xC) == 0xC) + v |= 1u << 28; // MODE NORMAL once attached normal + return v; + } + case S_TEST: { + uint32_t v = sor[S_TEST] & ~((1u << 10) | (3u << 8)); + if (sor_super_state1 & 0x8) + v |= 1u << 10; // ATTACHED + v |= (sor_super_state1 & 3) << 8; // ACT_HEAD_OPMODE + return v; + } + case S_LANE_SEQ_CTL: + return sor[r] & ~(1u << 31); + case S_PLL1: { + // The termination comparator: high while TERMADJ is above the value + // that matches the external resistor (the prod setting is 0x9). + uint32_t termadj = (sor[S_PLL1] >> 9) & 0xF; + return (sor[S_PLL1] & ~(1u << 15)) | (termadj > 9 ? (1u << 15) : 0); + } + case S_CRCA: { + TvState t; + dock_tv_state(state, &t); + return (sor[S_CRC_CNTRL] & 1) && t.video ? 1 : 0; + } + case S_CRCB: + return 0x1234ABCD; + case 0x12: { // DP_DEBUG_MVID + TvState t; + dock_tv_state(state, &t); + if (!t.video) + return 0; + uint64_t ls_khz = dpcd[0x100] == 0x14 ? 540000 : dpcd[0x100] == 0x0A ? 270000 : 162000; + uint32_t nval = sor[0x5A] & 0xFFFFFF; // DP_MN0 + if (!nval) + nval = 0x8000; + return (uint32_t)(head_b_pclk_khz() * nval / ls_khz) & 0xFFFFFF; + } + case 0x5A: // DP_MN0: N_VAL resets to 0x8000 + return sor[0x5A] ? sor[0x5A] : 0x8000; + default: + return sor[r]; + } +} + +void sor1_write(EmuState *state, uint32_t off, uint32_t val) { + g_state = state; + dock_tick(state); + uint32_t r = (off >> 2) & 0x3FF; + trace("sor1-w", off, val); + switch (r) { + case S_SUPER_STATE0: + if (val & 1) { + uint32_t prev = sor_super_state1; + sor_super_state1 = sor[S_SUPER_STATE1]; + if (prev != sor_super_state1) + DLOG("SOR1 %s, head %s, OR %s\n", (sor_super_state1 & 8) ? "attached" : "detached", + (sor_super_state1 & 3) == 2 ? "awake" : "asleep", + (sor_super_state1 & 4) ? "normal" : "safe"); + } + return; + case S_STATE0: + if (val & 1) + sor_state1 = sor[S_STATE1]; + return; + case S_LANE_SEQ_CTL: + sor[r] = val; + if (val & (1u << 31)) + lanes_powered = !(val & (1u << 16)); + break; + case S_PWR: + sor[r] = val; + return; + default: + sor[r] = val; + break; + } + link_eval(); +} + +void dock_tv_state(EmuState *state, TvState *t) { + g_state = state; + memset(t, 0, sizeof(*t)); + t->connected = has_dp() && tv_present() && chip.attached; + t->hpd = chip.hpd; + uint32_t lanes = 0; + t->link_trained = link_trained(&lanes); + t->lanes = lanes; + t->rate_code = dpcd[0x100]; + if (!has_dp()) { + snprintf(t->status, sizeof(t->status), "no dock with video on the USB-C port"); + return; + } + if (!tv_present()) { + snprintf(t->status, sizeof(t->status), "no TV on the dock"); + return; + } + if (!chip.attached) { + snprintf(t->status, sizeof(t->status), "console not on the dock"); + return; + } + if (!chip.dp_configured) { + snprintf(t->status, sizeof(t->status), "DisplayPort alt mode not configured by the PD controller"); + return; + } + if (!chip.hpd) { + snprintf(t->status, sizeof(t->status), "waiting for HPD"); + return; + } + if (!t->link_trained) { + const char *why = nullptr; + uint32_t bad = 0; + for (uint32_t j = 0; j < (lanes ? lanes : 1) && !why; j++) + if ((why = lane_fault(j))) + bad = j; + if (!lanes) + snprintf(t->status, sizeof(t->status), "link not configured (LANE_COUNT_SET 0)"); + else if (why) + snprintf(t->status, sizeof(t->status), "lane %u: %s", bad, why); + else + snprintf(t->status, sizeof(t->status), "link not trained"); + return; + } + // Frames: SOR1 attached and awake, driven by head 1 over DP-A, and head 1 + // (DC B) running with its pixel clock routed to SOR1. + uint32_t owner = sor_state1 & 0xF, proto = (sor_state1 >> 8) & 0xF; + uint64_t dcb = 0x54240000; + uint32_t cmd = mmio_bus_read(state, dcb + 0x032 * 4, 4); + uint32_t winopt = mmio_bus_read(state, dcb + 0x402 * 4, 4); + if ((sor_super_state1 & 0xF) != 0xE) + snprintf(t->status, sizeof(t->status), "SOR1 not attached, awake and normal (SUPER_STATE1 %X)", + sor_super_state1); + else if (owner != 2 || proto != 8) + snprintf(t->status, sizeof(t->status), "SOR1 STATE1 owner %u protocol %u: not head 1 over DP-A", + owner, proto); + else if (!(car(0x010) & (1u << 26)) || (car(0x004) & (1u << 26))) + snprintf(t->status, sizeof(t->status), "display head B not clocked or in reset"); + else if (((cmd >> 5) & 3) != 1) + snprintf(t->status, sizeof(t->status), "display head B stopped (DISPLAY_COMMAND %08X)", cmd); + else if (!(winopt & (1u << 26))) + snprintf(t->status, sizeof(t->status), "head B does not feed SOR1 (DISP_WIN_OPTIONS %08X)", winopt); + else { + t->video = true; + snprintf(t->status, sizeof(t->status), "%u lane%s at %s", lanes, lanes == 1 ? "" : "s", + dpcd[0x100] == 0x14 ? "5.4 Gbps" : dpcd[0x100] == 0x0A ? "2.7 Gbps" : "1.62 Gbps"); + } +} diff --git a/t210/dock.h b/t210/dock.h new file mode 100644 index 0000000..46cc0eb --- /dev/null +++ b/t210/dock.h @@ -0,0 +1,72 @@ +#ifndef T210_DOCK_H +#define T210_DOCK_H + +#include + +struct EmuState; + +/* + * The USB-C port, the dock on it and the TV behind the dock. + * + * BM92T36 (I2C1 0x18): the PD controller's registers and commands as + * switchroot's bm92txx.c and HOS's usb sysmodule drive them, with a partner + * on the cable that answers the way the measured docks did: a Nintendo dock + * (057E:2003, 5 V 0.5 A + 15 V 3 A, Nintendo alt mode, DP pin D), a third- + * party adapter (Fresco Logic 1D5C:7102, four PDOs, DisplayPort SVID) or + * the Nintendo charger (057E:2004, no video). Its interrupt is GPIO PK4, + * active low; its reset is PV5. + * + * DPAUX1 (0x54040000): the AUX channel to the dock's DisplayPort-to-HDMI + * converter (Realtek OUI 00-E0-4C, DPCD 1.2), its DPCD, and the TV's EDID + * over I2C-over-AUX. HPD reaches DPAUX1 only while the hdmi_int_dp_hpd pad + * is in SFIO mode, as measured. + * + * SOR1 (0x54580000): link training is judged lane by lane against what the + * board does - DP lane 0 leaves the SoC on pad 0 and lane 1 on pad 1, so the + * crossbar has to route them there (L4T's nvidia,xbar-ctrl <2 1 0 3 4>), and + * a lane only trains with its pad powered, the macro PLL up, PLLDP locked + * and SOR1 clocked from the brick at the rate the sink was told. + */ + +enum DockKind : uint8_t { + DOCK_NONE = 0, + DOCK_NINTENDO = 1, + DOCK_THIRD_PARTY = 2, // Ugreen-style adapter: Fresco Logic 1D5C:7102 + DOCK_CHARGER = 3, // Nintendo charger, no DisplayPort +}; + +void dock_reset(EmuState *state); +// Advance the dock's timers to state->emu_usec. Cheap; called often. +void dock_tick(EmuState *state); + +// BM92T36. A write carries its register first; a read returns n bytes from +// `reg` on (block registers lead with their length byte, as on the chip). +bool bm92_present(EmuState *state); +void bm92_write(EmuState *state, const uint8_t *buf, uint32_t n); +void bm92_read(EmuState *state, uint8_t reg, uint8_t *out, uint32_t n); +// PK4 as the SoC sees it: false while an enabled alert is pending. +bool bm92_alert_line(EmuState *state); +// PV5 driven by the SoC: high holds the chip in reset. +void bm92_reset_line(EmuState *state, bool high); + +// HPD from the dock's DisplayPort sink. +bool dock_hpd(EmuState *state); + +// DPAUX1 and SOR1 register windows (byte offsets). +uint32_t dpaux1_read(EmuState *state, uint32_t off); +void dpaux1_write(EmuState *state, uint32_t off, uint32_t val); +uint32_t sor1_read(EmuState *state, uint32_t off); +void sor1_write(EmuState *state, uint32_t off, uint32_t val); + +// What the TV shows, for its window. +struct TvState { + bool connected; // a dock with a TV behind it is on the cable + bool hpd; // the converter raised HPD to the SoC + bool link_trained; // every configured lane has CR, EQ, symbol lock + bool video; // SOR1 attached to a running head: frames flow + uint32_t lanes, rate_code; + char status[160]; // one line for the "no signal" screen +}; +void dock_tv_state(EmuState *state, TvState *out); + +#endif // T210_DOCK_H diff --git a/t210/memory_map.h b/t210/memory_map.h index 891aac9..f274789 100644 --- a/t210/memory_map.h +++ b/t210/memory_map.h @@ -108,6 +108,7 @@ constexpr uint64_t MC_SIZE = 0x1000; // Display Controller (DC/DSI) constexpr uint64_t DISPLAY_A_BASE = 0x54200000; +constexpr uint64_t DISPLAY_B_BASE = 0x54240000; // DC1: feeds SOR1 / the dock constexpr uint64_t DISPLAY_SIZE = 0x40000; // DSI Controller diff --git a/t210/mmio.cpp b/t210/mmio.cpp index 15522da..8cd2c10 100644 --- a/t210/mmio.cpp +++ b/t210/mmio.cpp @@ -9,6 +9,7 @@ #include "ccplex.h" #include "pcie.h" #include "gic.h" +#include "dock.h" #include "usb.h" #include "regcache.h" #include "se_engine.h" @@ -541,6 +542,19 @@ uint32_t gpio_read(EmuState *state, uint64_t addr) { // PH5/BT_HOST_WAKE and PH3/BT_DEV_WAKE are read while the Tegra is // deliberately not driving them, and mirroring OUT there answers 0 to every // question regardless of what is on the board. + // Port K (bank 2, port 2): PK4 is the PD controller's alert, active low. + // Port CC (bank 7, port 0): CC1 is HPD from the dock. Both read the pin + // when it is an input, as in GPIO mode they are. + if (offset == 0x238 || offset == 0x730) { + uint32_t v = mmio_regs.get(addr - 0x10); + uint32_t oe = mmio_regs.get(addr - 0x20); + uint32_t bit = offset == 0x238 ? (1u << 4) : (1u << 1); + bool level = offset == 0x238 ? bm92_alert_line(state) : dock_hpd(state); + if (!(oe & bit)) + v = level ? (v | bit) : (v & ~bit); + return v; + } + if (offset == 0x13C) { uint32_t v = gpio_h_in(state); TRACE("[gpio] R: Port H IN = 0x%02X\n", v); @@ -624,6 +638,13 @@ void gpio_write(EmuState *state, uint64_t addr, uint32_t val) { gpio_h_update(state); sd_card_gpio_update(); } + // Port V (bank 5, port 1): PV5 drives the PD controller's reset, high = + // held. Only once it is a GPIO output: as the SFIO it rests low. + if (offset >= 0x500 && offset < 0x600) { + uint32_t cnf = mmio_regs.get(GPIO_BASE + 0x504), oe = mmio_regs.get(GPIO_BASE + 0x514); + uint32_t out = mmio_regs.get(GPIO_BASE + 0x524); + bm92_reset_line(state, (cnf & oe & out & (1u << 5)) != 0); + } } // ==================== I2C ==================== @@ -647,6 +668,8 @@ void gpio_write(EmuState *state, uint64_t addr, uint32_t val) { static uint8_t i2c_slave_addr = 0; static uint8_t i2c_reg_addr = 0; +// What the last normal-mode read of the BM92T36 returned, for CMD_DATA1/2. +static uint8_t bm92_normal_rx[8]; // Last full CMD_DATA1 word, committed when I2C_CNFG starts the transfer. static uint32_t i2c_cmd_data1 = 0; @@ -799,7 +822,8 @@ static bool i2c_slave_fitted(EmuState *state, bool on_i2c5, uint8_t addr) { } } switch (addr) { - case 0x18: // BM92T36 USB-PD + case 0x18: // BM92T36 USB-PD: NAKs in reset and while it boots + return bm92_present(state); case 0x36: // MAX17050 fuel gauge case 0x4C: // TMP451 thermal case 0x6B: // BQ24193 charger @@ -987,49 +1011,22 @@ struct PacketState { uint8_t rx_buf[64] = {0}; uint32_t rx_size = 0; uint32_t rx_pos = 0; + uint8_t tx_buf[64] = {0}; // a write's payload, for chips that take + // whole messages (the BM92T36) }; static PacketState pkt_i2c1; static PacketState pkt_i2c5; +// I2C_INT_STATUS per controller (I2C1, I2C5): PACKET_XFER_COMPLETE (7) and +// ALL_PACKETS_XFER_COMPLETE (6) latch when a packet finishes, NO_ACK (3) +// when nobody answered; write 1 to clear. BUS_CLEAR_DONE (11) always reads +// set, as before. +static uint32_t i2c_int_latch[2]; #define I2C_PACKET_PROT_I2C (1u << 4) #define I2C_HEADER_READ (1u << 19) -static void bm92t36_fill_rx(EmuState *state, uint8_t reg, uint8_t *buf, uint32_t size) { - // All multi-byte values are little-endian on the wire (Hekate reassembles - // with `(buf[1] << 8) | buf[0]`). FW_TYPE is the exception — Hekate uses - // `(buf[0] << 4) | buf[1]` and expects 0x36, so buf[0]=3, buf[1]=6. - auto put = [&](uint32_t i, uint8_t v) { if (i < size) buf[i] = v; }; - switch (reg) { - case 0x03: // STATUS1: bit 7 = cable inserted - put(0, state->usb_pd_inserted.load() ? 0x80 : 0x00); - break; - case 0x4B: // FW_TYPE_REG -> VER_36 = 0x36 - put(0, 0x03); put(1, 0x06); - break; - case 0x4D: // MAN_ID_REG -> MAN_ROHM = 0x04B5 - put(0, 0xB5); put(1, 0x04); - break; - case 0x4E: // DEV_ID_REG -> DEV_BM92T = 0x03B0 - put(0, 0xB0); put(1, 0x03); - break; - case 0x08: // READ_PDOS_SRC: byte0 = PDO-bytes count, then 4-byte PDOs - case 0x28: { // CURRENT_PDO: same layout, 1 PDO - // Synthesize a single Fixed-type PDO from the EmuState values. - // pd_object_t bitfields (LSB->MSB): amp:10, volt:10, info:10, type:2. - uint32_t amp_lsb = (uint32_t)(state->usb_pd_amperage_ma.load() / 10) & 0x3FF; - uint32_t volt_lsb = (uint32_t)(state->usb_pd_voltage_mv.load() / 50) & 0x3FF; - uint32_t pdo = amp_lsb | (volt_lsb << 10); - put(0, 4); - put(1, (uint8_t)(pdo >> 0)); - put(2, (uint8_t)(pdo >> 8)); - put(3, (uint8_t)(pdo >> 16)); - put(4, (uint8_t)(pdo >> 24)); - break; - } - default: - break; - } -} +// The BM92T36 itself is t210/dock.cpp; packet and normal-mode accesses +// to I2C1 0x18 are routed there. // ==================== I2C_2: Rohm BH1730 ambient light sensor ============ // @@ -1097,7 +1094,7 @@ static void packet_populate_rx(EmuState *state, bool on_i2c5, PacketState &p) { p.rx_size = std::min((uint32_t)sizeof(p.rx_buf), p.payload_size); memset(p.rx_buf, 0, sizeof(p.rx_buf)); if (!on_i2c5 && p.dev_addr == 0x18) { - bm92t36_fill_rx(state, p.reg_addr, p.rx_buf, p.rx_size); + bm92_read(state, p.reg_addr, p.rx_buf, p.rx_size); return; } // Every other slave: serve packet-mode reads from the same register models @@ -1134,7 +1131,7 @@ uint32_t i2c_read(EmuState *state, uint64_t addr) { case 0x8C: // I2C_CONFIG_LOAD return 0; // MSTR_CONFIG_LOAD (bit 0) cleared = load complete case 0x68: // I2C_INT_STATUS - return (1 << 11); // BUS_CLEAR_DONE (bit 11) + return i2c_int_latch[on_i2c5 ? 1 : 0] | (1 << 11); case 0x54: { // I2C_RX_FIFO (packet-mode receive) uint32_t word = 0; uint32_t n = std::min((uint32_t)4, pkt.rx_size - pkt.rx_pos); @@ -1156,9 +1153,15 @@ uint32_t i2c_read(EmuState *state, uint64_t addr) { } return 0; } - case 0x10: // I2C_CMD_DATA2 (bytes 4-7) — no slaves currently need a >4 byte response + case 0x10: // I2C_CMD_DATA2 (bytes 4-7): only the PD controller fills it + if (!on_i2c5 && i2c_slave_addr == 0x18) + return bm92_normal_rx[4] | bm92_normal_rx[5] << 8 | bm92_normal_rx[6] << 16 | + (uint32_t)bm92_normal_rx[7] << 24; return 0; case 0x0C: // I2C_CMD_DATA1 + if (!on_i2c5 && i2c_slave_addr == 0x18) + return bm92_normal_rx[0] | bm92_normal_rx[1] << 8 | bm92_normal_rx[2] << 16 | + (uint32_t)bm92_normal_rx[3] << 24; // TMP451 SoC/PCB thermal sensor (slave 0x4C on I2C_1). // Hekate reads (per bdk/thermal/tmp451.c) the integer °C from // PCB: 0x00, SoC: 0x01 @@ -1503,6 +1506,20 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) { i2c_cnfg_reg[on_i2c5 ? 1 : 0] = val; bool is_write = (val & (1u << 6)) == 0; uint32_t size = ((val >> 1) & 7) + 1; + // BM92T36: whole messages, normal mode (up to 8 bytes in CMD_DATA1/2). + if ((val & (1u << 9)) && !on_i2c5 && i2c_slave_addr == 0x18 && + i2c_slave_present(state, false, 0x18)) { + uint32_t d2 = mmio_regs.get(I2C1_BASE + 0x10); + uint8_t b[8]; + for (int k = 0; k < 4; k++) { + b[k] = (uint8_t)(i2c_cmd_data1 >> (8 * k)); + b[4 + k] = (uint8_t)(d2 >> (8 * k)); + } + if (is_write && size >= 2) + bm92_write(state, b, size); + else if (!is_write) + bm92_read(state, i2c_reg_addr, bm92_normal_rx, size); + } if ((val & (1u << 9)) && is_write && size >= 2 && on_i2c5 && i2c_slave_addr == MAX77620_I2C_ADDR) { max77620_regs_init(state); @@ -1598,6 +1615,8 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) { if (pkt.is_read) { packet_populate_rx(state, on_i2c5, pkt); pkt.hdr_idx = 0; + i2c_int_latch[on_i2c5 ? 1 : 0] |= (1u << 7) | (1u << 6) | + (i2c_slave_present(state, on_i2c5, pkt.dev_addr) ? 0 : (1u << 3)); } else { pkt.hdr_idx = 3; } @@ -1605,6 +1624,8 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) { default: for (int k = 0; k < 4 && pkt.tx_seen < pkt.payload_size; k++) { uint8_t b = (uint8_t)(val >> (8 * k)); + if (pkt.tx_seen < sizeof(pkt.tx_buf)) + pkt.tx_buf[pkt.tx_seen] = b; if (pkt.tx_seen == 0) pkt.reg_addr = b; // first payload byte: the register address else @@ -1612,12 +1633,22 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) { (uint8_t)(pkt.reg_addr + pkt.tx_seen - 1), b); pkt.tx_seen++; } - if (pkt.tx_seen >= pkt.payload_size) + if (pkt.tx_seen >= pkt.payload_size) { pkt.hdr_idx = 0; + i2c_int_latch[on_i2c5 ? 1 : 0] |= (1u << 7) | (1u << 6) | + (i2c_slave_present(state, on_i2c5, pkt.dev_addr) ? 0 : (1u << 3)); + if (!on_i2c5 && pkt.dev_addr == 0x18 && pkt.payload_size >= 2 && + i2c_slave_present(state, false, 0x18)) + bm92_write(state, pkt.tx_buf, + std::min(pkt.payload_size, sizeof(pkt.tx_buf))); + } break; } break; } + case 0x68: // I2C_INT_STATUS: write 1 to clear + i2c_int_latch[on_i2c5 ? 1 : 0] &= ~val; + break; case 0x5C: // I2C_FIFO_CONTROL — TX/RX flush at the start of each xfer if (val & 0x3) { pkt.hdr_idx = 0; @@ -1653,15 +1684,15 @@ static constexpr uint32_t kDcIntFrameEndVBlank = (1u << 1) | (1u << 2); // Resolve a DC word index to the windows it addresses and the register-file // slot. False for anything that is not a window register. -static bool dc_window_reg(const EmuState *state, uint32_t index, +static bool dc_window_reg(uint32_t header, uint32_t index, uint32_t *mask, uint32_t *slot) { if (index >= 0x700 && index < 0x800) { - *mask = (state->dc_window_header >> 4) & 0xF; + *mask = (header >> 4) & 0xF; *slot = index - 0x700; return true; } if (index >= 0x800 && index < 0x840) { - *mask = (state->dc_window_header >> 4) & 0xF; + *mask = (header >> 4) & 0xF; *slot = 0x100 + (index - 0x800); return true; } @@ -1677,28 +1708,101 @@ static bool dc_window_reg(const EmuState *state, uint32_t index, return false; } +// The two heads share one model; this is a head's slice of EmuState. +struct DcHead { + uint64_t base; + uint32_t (*win)[dcwin::kRegs]; + uint32_t (*act)[dcwin::kRegs]; + uint32_t *header; + bool *programmed; + char name; +}; + +static DcHead dc_head(EmuState *s, uint64_t addr) { + if (addr >= DISPLAY_B_BASE) + return {DISPLAY_B_BASE, s->dcb_win, s->dcb_win_active, &s->dcb_window_header, + &s->dcb_programmed, 'B'}; + return {DISPLAY_A_BASE, s->dc_win, s->dc_win_active, &s->dc_window_header, + &s->dc_programmed, 'A'}; +} + +// Head B's interrupts are real: FRAME_END and V_BLANK latch once per frame +// while the head runs (DISPLAY_COMMAND C_DISPLAY), clear on a write of 1, +// and drive GIC SPI 74 when enabled - which a kernel's vblank waits need. +// Head A keeps reading "a frame has just ended" for the payloads that poll. +static uint32_t dcb_int_latch = 0; +static uint64_t dcb_last_frame = 0; +static bool dcb_running(EmuState *s) { + return ((mmio_regs.get(DISPLAY_B_BASE + 0x032 * 4) >> 5) & 3) == 1; +} +static void dcb_frames(EmuState *s) { + if (!dcb_running(s)) + return; + uint64_t f = s->emu_usec / 16667; + if (f != dcb_last_frame) { + dcb_last_frame = f; + dcb_int_latch |= (1u << 1) | (1u << 2); + } + uint32_t en = mmio_regs.get(DISPLAY_B_BASE + 0x039 * 4); + gic_set_spi(74, (dcb_int_latch & en) != 0); +} + +// DC_CMD_CONT_SYNCPT_VSYNC (0x028): SYNCPT_VSYNC_ENABLE (bit 8) makes the +// head increment host1x syncpoint INDX (7:0) once per frame. A frame counter +// that needs no interrupt logic: hekate's panel uses 9, hwtest's TV check 11. +static void dc_syncpt_tick(EmuState *s, uint64_t base, uint64_t *last) { + uint32_t cmd = mmio_regs.get(base + 0x032 * 4); + uint32_t sv = mmio_regs.get(base + 0x028 * 4); + uint64_t f = s->emu_usec / 16667; + if (((cmd >> 5) & 3) != 1 || !(sv & (1u << 8))) { + *last = f; + return; + } + if (f == *last) + return; + uint64_t reg = HOST1X_BASE + 0x2100 + 0xF80 + 4 * (sv & 0xFF); + mmio_regs[reg] = mmio_regs.get(reg) + (uint32_t)(f - *last); + *last = f; +} + +void display_tick(EmuState *s) { + static uint64_t last_a = 0, last_b = 0; + dcb_frames(s); + dc_syncpt_tick(s, DISPLAY_A_BASE, &last_a); + dc_syncpt_tick(s, DISPLAY_B_BASE, &last_b); +} + static void dc_reset(EmuState *state) { memset(state->dc_win, 0, sizeof(state->dc_win)); memset(state->dc_win_active, 0, sizeof(state->dc_win_active)); - for (int w = 0; w < 4; w++) + memset(state->dcb_win, 0, sizeof(state->dcb_win)); + memset(state->dcb_win_active, 0, sizeof(state->dcb_win_active)); + for (int w = 0; w < 4; w++) { state->dc_win[w][dcwin::BLEND_LAYER] = state->dc_win_active[w][dcwin::BLEND_LAYER] = dcwin::BLEND_LAYER_RESET; + state->dcb_win[w][dcwin::BLEND_LAYER] = + state->dcb_win_active[w][dcwin::BLEND_LAYER] = dcwin::BLEND_LAYER_RESET; + } state->dc_window_header = 0; state->dc_programmed = false; + state->dcb_window_header = 0; + state->dcb_programmed = false; + dcb_int_latch = 0; state->vic_out_addr = 0; state->vic_out_xform = 0; } // One line per window whose picture changed (not for moves: bdk slides its // log window in 16 pixels at a time). -static void dc_log_window(int w, const uint32_t *r) { +static void dc_log_window(char head, int w, const uint32_t *r) { + const char *hn = head == 'B' ? " (head B)" : ""; if (!(r[dcwin::OPTIONS] & dcwin::WIN_ENABLE)) { - printf("[display] window %c off\n", 'A' + w); + printf("[display] window %c%s off\n", 'A' + w, hn); return; } static const char *const kKind[4] = {"pitch", "tiled", "block", "?"}; uint32_t kind = r[dcwin::SURFACE_KIND]; - printf("[display] window %c: %ux%u @ 0x%08X, %s", 'A' + w, + printf("[display] window %c%s: %ux%u @ 0x%08X, %s", 'A' + w, hn, r[dcwin::SIZE] & 0x1FFF, (r[dcwin::SIZE] >> 16) & 0x1FFF, r[dcwin::START_ADDR], kKind[kind & 3]); if ((kind & 3) == 2) @@ -1709,52 +1813,71 @@ static void dc_log_window(int w, const uint32_t *r) { } uint32_t display_read(EmuState *state, uint64_t addr) { - uint32_t index = (uint32_t)(addr - DISPLAY_A_BASE) / 4; + DcHead h = dc_head(state, addr); + uint32_t index = (uint32_t)(addr - h.base) / 4; uint32_t mask, slot; - if (dc_window_reg(state, index, &mask, &slot)) { + if (dc_window_reg(*h.header, index, &mask, &slot)) { for (int w = 0; w < 4; w++) if (mask & (1u << w)) - return state->dc_win[w][slot]; + return h.win[w][slot]; return 0; } switch (index) { - case kDcCmdIntStatus: return kDcIntFrameEndVBlank; + case kDcCmdIntStatus: + if (h.name == 'B') { + dcb_frames(state); + return dcb_int_latch; + } + return kDcIntFrameEndVBlank; case kDcCmdStateControl: return 0; - case kDcCmdWindowHeader: return state->dc_window_header; + case kDcCmdWindowHeader: return *h.header; default: return mmio_regs.get(addr); } } void display_write(EmuState *state, uint64_t addr, uint32_t val) { - uint32_t index = (uint32_t)(addr - DISPLAY_A_BASE) / 4; + DcHead h = dc_head(state, addr); + uint32_t index = (uint32_t)(addr - h.base) / 4; uint32_t mask, slot; - if (dc_window_reg(state, index, &mask, &slot)) { + if (dc_window_reg(*h.header, index, &mask, &slot)) { for (int w = 0; w < 4; w++) if (mask & (1u << w)) - state->dc_win[w][slot] = val; + h.win[w][slot] = val; return; } + if (h.name == 'B' && index == kDcCmdIntStatus) { + dcb_int_latch &= ~val; // write 1 to clear + dcb_frames(state); + return; + } + if (h.name == 'B' && index == 0x032) { + static uint32_t last = 0; + if (((val >> 5) & 3) != ((last >> 5) & 3)) + printf("[display] head B %s\n", ((val >> 5) & 3) == 1 ? "running (C_DISPLAY)" : + ((val >> 5) & 3) == 0 ? "stopped" : "one-shot"); + last = val; + } if (index == kDcCmdWindowHeader) { - state->dc_window_header = val; + *h.header = val; } else if (index == kDcCmdStateControl) { uint32_t req = (val >> 1) & 0xF; // WIN_A..D_ACT_REQ for (int w = 0; w < 4; w++) { if (!(req & (1u << w))) continue; - uint32_t *act = state->dc_win_active[w]; - const uint32_t *asm_ = state->dc_win[w]; + uint32_t *act = h.act[w]; + const uint32_t *asm_ = h.win[w]; static const uint32_t kShown[] = { dcwin::OPTIONS, dcwin::COLOR_DEPTH, dcwin::SIZE, dcwin::LINE_STRIDE, dcwin::START_ADDR, dcwin::SURFACE_KIND}; - bool changed = !state->dc_programmed; + bool changed = !*h.programmed; for (uint32_t r : kShown) changed |= act[r] != asm_[r]; memcpy(act, asm_, sizeof(state->dc_win_active[w])); if (changed) - dc_log_window(w, act); + dc_log_window(h.name, w, act); } if (req) { - state->dc_programmed = true; + *h.programmed = true; state->display_dirty = true; } } @@ -4688,6 +4811,13 @@ static uint32_t i2c_empty_bus_read(uint64_t address) { uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) { (void)size; + // host1x syncpoints: the heads' per-frame increments are brought up to + // date before a read, so a payload timing frames inside one BPMP batch + // (a paced TIMERUS wait) sees them. + if (address >= HOST1X_BASE + 0x3080 && address < HOST1X_BASE + 0x3080 + 0x300) { + display_tick(state); + return mmio_regs.get(address); + } if (address >= TMR_BASE && address < TMR_BASE + TMR_SIZE) { // TIMERUS_CNTR_1US. The rest of the timer block reads 0. The BPMP's // reads go through the clock's poll pacing; CPU0 has its own clock. @@ -4723,6 +4853,10 @@ uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) { return bpmp_cache_read(state, address); if (address >= GICD_BASE && address < GICD_BASE + GICD_SIZE) return gic_dist_read((uint32_t)(address - GICD_BASE), size); + if (address >= 0x54040000 && address < 0x54041000) + return dpaux1_read(state, (uint32_t)(address - 0x54040000)); + if (address >= 0x54580000 && address < 0x54581000) + return sor1_read(state, (uint32_t)(address - 0x54580000)); if (address >= GICC_BASE && address < GICC_BASE + GICC_SIZE) return gic_cpu_read((uint32_t)(address - GICC_BASE), size); if (address >= SOC_THERM_BASE && address < SOC_THERM_BASE + SOC_THERM_SIZE) @@ -4751,7 +4885,7 @@ uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) { if ((address >= I2C4_BASE && address < I2C4_BASE + I2C_CTRL_SIZE) || (address >= I2C6_BASE && address < I2C6_BASE + I2C_CTRL_SIZE)) return i2c_empty_bus_read(address); - if (address >= DISPLAY_A_BASE && address < DISPLAY_A_BASE + DISPLAY_SIZE) + if (address >= DISPLAY_A_BASE && address < DISPLAY_B_BASE + DISPLAY_SIZE) return display_read(state, address); if (address >= PMC_BASE && address < PMC_BASE + PMC_SIZE) return pmc_read(state, address); @@ -4896,6 +5030,10 @@ void mmio_bus_write(EmuState *state, uint64_t address, unsigned size, } else if (address >= BPMP_CACHE_BASE && address < BPMP_CACHE_BASE + BPMP_CACHE_SIZE) { bpmp_cache_write(state, address, val); + } else if (address >= 0x54040000 && address < 0x54041000) { + dpaux1_write(state, (uint32_t)(address - 0x54040000), val); + } else if (address >= 0x54580000 && address < 0x54581000) { + sor1_write(state, (uint32_t)(address - 0x54580000), val); } else if (address >= GICD_BASE && address < GICD_BASE + GICD_SIZE) { gic_dist_write((uint32_t)(address - GICD_BASE), size, val); } else if (address >= GICC_BASE && address < GICC_BASE + GICC_SIZE) { @@ -4920,7 +5058,7 @@ void mmio_bus_write(EmuState *state, uint64_t address, unsigned size, } else if (address >= I2C5_BASE && address < I2C5_BASE + I2C_CTRL_SIZE) { i2c_write(state, address, val); } else if (address >= DISPLAY_A_BASE && - address < DISPLAY_A_BASE + DISPLAY_SIZE) { + address < DISPLAY_B_BASE + DISPLAY_SIZE) { display_write(state, address, val); } else if (address >= DSI_BASE && address < DSI_BASE + DSI_SIZE) { dsi_write(state, address, val); @@ -5035,6 +5173,7 @@ const struct { {EMC1_BASE, EMC_SIZE}, {MC_BASE, MC_SIZE}, {DISPLAY_A_BASE, DISPLAY_SIZE}, + {DISPLAY_B_BASE, DISPLAY_SIZE}, {DSI_BASE, DSI_SIZE}, {I2C2_BASE, I2C2_SIZE}, {SOC_THERM_BASE, SOC_THERM_SIZE}, @@ -5059,6 +5198,9 @@ const struct { {MSELECT_BASE, MSELECT_SIZE}, // GIC-400 distributor, CPU interface and the virtualisation pages. {GICD_BASE, 0x7000}, + // The DisplayPort path to the dock: DPAUX1, SOR1, display head B. + {0x54040000, 0x1000}, + {0x54580000, 0x1000}, // PCIe. T210 puts the root complex at the BOTTOM of the address map, // below every other peripheral. {PCIE_BLOCK_BASE, PCIE_BLOCK_SIZE}, @@ -5148,6 +5290,7 @@ void mmio_init(uc_engine *uc, EmuState *state) { pcie_reset(state); ccplex_reset(state); gic_reset(); + dock_reset(state); sdhci_reset_regs(sdhci_regs[0]); sdhci_reset_regs(sdhci_regs[1]); dc_reset(state); diff --git a/t210/mmio.h b/t210/mmio.h index b2cadc2..81dd629 100644 --- a/t210/mmio.h +++ b/t210/mmio.h @@ -39,6 +39,8 @@ void mmio_unmap_bus(uc_engine *uc); // Map one 4 KiB page of the peripheral band as a register window (CPU0's // first access to a block the emulator does not model). bool mmio_map_page(uc_engine *uc, EmuState *state, uint64_t page); +// Display head B's frame clock (interrupt latch, GIC line); called often. +void display_tick(EmuState *state); // The shared dispatch both engines' MMIO callbacks funnel into. `size` is the // access width in bytes (1, 2 or 4).