From 913bf39dba0db73530f80e2a2fc2af0ccdf1cc57 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:10:14 +0300 Subject: [PATCH 1/2] novatek: read NA51089 die temperature from the registers (#275) The only Novatek reader was the vendor nvt_adc driver's thermal zone, which OpenIPC's kernel does not build, so `ipctool --temp` and `ipcinfo --temp` had nothing to say on an NT98562/NT98566. The die thermometer is channel 3 of the SAR ADC, which averages it into 0xF0260040 [24:16]. The ADC is found gated and idle, so it is started the way the RTOS side's thermal_channel_init() does: ADC clock on (CG 0xF0020074 bit 13), channel 3 continuous and the converter on (0xF0260000 bits 3 and 23). adc_en alone converts nothing. Only missing bits are set and nothing is turned off afterwards, so a converter the vendor driver runs is read as it stands. The per-die calibration is eFuse row 6 (EFUSE_THERMAL_TRIM_DATA). Its read sequence exists only in the SDK's binary otp_protected.o_shipped; this is that blob's efuse_open(0, 0, 0) / efuse_read() / efuse_close(), read mode throughout. A row carries two copies, each with a programmed (bit 15) and an invalidated (bit 14) flag, and efuse_readParamOps() takes the first good one. With none, or with the controller already open (the kernel mid-read, behind a semaphore userspace cannot take), the trim is nvt_adc's default, 231. The formula is nvt_adc's for every die but NA51055: (code - trim) * 1.319 + 30. The sysfs reader stays for the other dies, and now reports no reading instead of -237 degC when the file cannot be read. novatek_test models the eFuse controller and the ADC, and fails on a write that leaves read mode or clears a bit someone else set. Lab NT98566 + SC501AI on OpenIPC's 2026-10-21 nightly: trim 0xE4 (228) from the eFuse, the same value `echo trim > /proc/nvt_otp_op/otp_trim` prints; code 262 for 74.85 degC, steady over ten reads; eFuse left closed. ipcinfo grows 928 bytes with IPCHW_VENDORS=all, none with none. --- .github/workflows/pr-build-check.yml | 3 +- CMakeLists.txt | 8 + src/hal/novatek.c | 141 ++++++++++++++- src/hal/novatek_test.c | 246 +++++++++++++++++++++++++++ 4 files changed, 392 insertions(+), 6 deletions(-) create mode 100644 src/hal/novatek_test.c diff --git a/.github/workflows/pr-build-check.yml b/.github/workflows/pr-build-check.yml index a5e14a8..ec22e34 100644 --- a/.github/workflows/pr-build-check.yml +++ b/.github/workflows/pr-build-check.yml @@ -148,7 +148,7 @@ jobs: cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release cmake --build build --target cYAML_test reginfo_test longse_test \ sensors_test anyka_test ambarella_test \ - hal_hisi_test \ + hal_hisi_test novatek_test \ report_test http_test mtd_alias_test \ nand_layout_test upgrade_env_test \ jffs2_test raw_image_test @@ -165,6 +165,7 @@ jobs: ./build/anyka_test ./build/ambarella_test ./build/hal_hisi_test + ./build/novatek_test # Run from the tree root: the fingerprint it compares against is # src/sensors_golden.txt, and a sensor that stopped being identified # has to fail here rather than be noticed on a camera in a ceiling. diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f59132..8605408 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -510,6 +510,14 @@ if(NOT ONLY_LIBRARY) IPCHW_HISI_V5 IPCHW_HISI_GOKE7206) target_link_libraries(hal_hisi_test m Threads::Threads) + # The same arrangement for NA51089's direct temperature read: novatek.c is + # included for its file-statics and the test models the eFuse controller and + # the ADC behind its own mem_reg(). + add_executable(novatek_test src/hal/novatek_test.c src/hal/common.c) + target_include_directories(novatek_test PRIVATE include src) + target_compile_definitions(novatek_test PRIVATE STANDALONE_LIBRARY) + target_link_libraries(novatek_test m Threads::Threads) + # Parsing only: longse.c itself pulls in cJSON and the HAL, and the part # worth testing off a camera is the version extraction, which is standalone. add_executable(longse_test src/boards/longse_test.c src/boards/longse.c diff --git a/src/hal/novatek.c b/src/hal/novatek.c index cdff1e3..f6211a4 100644 --- a/src/hal/novatek.c +++ b/src/hal/novatek.c @@ -1,5 +1,6 @@ #include "hal/novatek.h" +#include #include #include @@ -113,14 +114,142 @@ unsigned long novatek_totalmem(unsigned long *media_mem) { return *media_mem + kernel_mem(); } +/* The vendor nvt_adc driver's thermal zone. It reports whole degrees, not the + * millidegrees the sysfs ABI promises, so the value is taken as it stands. */ float novatek_get_temp() { - float ret = -237.0; char buf[16]; if (line_from_file("/sys/class/thermal/thermal_zone0/temp", "(.+)", buf, - sizeof(buf))) { - ret = strtof(buf, NULL); + sizeof(buf))) + return strtof(buf, NULL); + return NAN; +} + +/* NA51089's die thermometer is channel 3 of the SAR ADC, averaged by the ADC + * into its own register. OpenIPC's kernel builds no nvt_adc driver, so the ADC + * is found gated and idle, and is started here the way the RTOS side's + * thermal_channel_init() does: clock on, channel 3 continuous, converter on. + * Both are left running, as the vendor driver leaves them; turning a clock off + * is how a probe takes a peripheral away from its owner + * (OpenIPC/firmware#2439). */ +#define NA51089_CG_CLK_EN1 0xF0020074 +#define NA51089_CG_ADC (1u << 13) +#define NA51089_ADC_CTRL 0xF0260000 +#define NA51089_ADC_AIN3_MODE (1u << 3) +#define NA51089_ADC_EN (1u << 23) +#define NA51089_ADC_THERMAL 0xF0260040 /* bits [24:16], the average */ + +/* The per-die calibration is eFuse row 6 (EFUSE_THERMAL_TRIM_DATA). Its read + * sequence lives only in the SDK's binary otp_protected.o_shipped; this is + * that blob's efuse_open(0, 0, 0) / efuse_read() / efuse_close(), read mode + * throughout, so nothing here can program a fuse. */ +#define NA51089_EFUSE 0xF0660000 +#define EFUSE_MODE (NA51089_EFUSE + 0x00) +#define EFUSE_ROW (NA51089_EFUSE + 0x04) +#define EFUSE_DATA (NA51089_EFUSE + 0x08) +#define EFUSE_CTRL (NA51089_EFUSE + 0x20) /* bit 0 open, bit 4 read */ +#define EFUSE_STS (NA51089_EFUSE + 0x30) /* bit 0 opened, bit 1 read done */ +#define NA51089_THERMAL_TRIM_ROW 6 +/* nvt_adc's DEFAULT_THERMAL_528_LEVEL, for a die with no trim fused */ +#define NA51089_DEFAULT_TRIM 231 + +static bool nvt_wait(uint32_t addr, uint32_t mask, uint32_t want) { + uint32_t val; + for (int i = 0; i < 1000; i++) { + if (!mem_reg(addr, &val, OP_READ)) + return false; + if ((val & mask) == want) + return true; } - return ret; + return false; +} + +static bool nvt_efuse_row(uint32_t row, uint32_t *data) { + uint32_t val; + bool ok = false; + + /* The kernel serialises this block with a semaphore we cannot take. If + * it is open, someone is mid-sequence: leave it to them. */ + if (!mem_reg(EFUSE_CTRL, &val, OP_READ) || (val & 1)) + return false; + val |= 1; + if (!mem_reg(EFUSE_CTRL, &val, OP_WRITE)) + return false; + if (!nvt_wait(EFUSE_STS, 1, 1)) + goto close; + + val = 0; + mem_reg(EFUSE_MODE, &val, OP_WRITE); + mem_reg(EFUSE_ROW, &row, OP_WRITE); + if (!mem_reg(EFUSE_CTRL, &val, OP_READ)) + goto close; + val |= 0x10; + mem_reg(EFUSE_CTRL, &val, OP_WRITE); + if (!nvt_wait(EFUSE_CTRL, 0x10, 0) || !nvt_wait(EFUSE_STS, 2, 2)) + goto close; + if (mem_reg(EFUSE_STS, &val, OP_READ)) + mem_reg(EFUSE_STS, &val, OP_WRITE); /* write-one-to-clear */ + ok = mem_reg(EFUSE_DATA, data, OP_READ); + +close: + val = 0; + mem_reg(EFUSE_ROW, &val, OP_WRITE); + if (mem_reg(EFUSE_CTRL, &val, OP_READ)) { + val &= ~1u; + mem_reg(EFUSE_CTRL, &val, OP_WRITE); + nvt_wait(EFUSE_STS, 1, 0); + } + return ok; +} + +/* A row holds two copies, as efuse_readParamOps() reads them: bits [15:0] + * first, then [31:16]. In each, bit 15 says programmed and bit 14 says + * invalidated; the trim is the low 14 bits of the first good one. */ +static int na51089_thermal_trim() { + uint32_t row; + if (nvt_efuse_row(NA51089_THERMAL_TRIM_ROW, &row)) + for (int half = 0; half < 2; half++) { + const uint32_t w = (row >> (16 * half)) & 0xffff; + if ((w & 0xc000) == 0x8000) + return w & 0x3fff; + } + return NA51089_DEFAULT_TRIM; +} + +static float na51089_get_temp() { + uint32_t val; + + if (!mem_reg(NA51089_CG_CLK_EN1, &val, OP_READ)) + return NAN; + if (!(val & NA51089_CG_ADC)) { + val |= NA51089_CG_ADC; + if (!mem_reg(NA51089_CG_CLK_EN1, &val, OP_WRITE)) + return NAN; + } + + /* adc_en alone converts nothing: channel 3 must be in continuous mode. */ + const uint32_t on = NA51089_ADC_AIN3_MODE | NA51089_ADC_EN; + if (!mem_reg(NA51089_ADC_CTRL, &val, OP_READ)) + return NAN; + if ((val & on) != on) { + val |= on; + if (!mem_reg(NA51089_ADC_CTRL, &val, OP_WRITE)) + return NAN; + } + + /* The average reads 0 until the first conversion, well under 1 ms. */ + uint32_t code = 0; + for (int i = 0; i < 50 && !code; i++) { + if (!mem_reg(NA51089_ADC_THERMAL, &val, OP_READ)) + return NAN; + code = (val >> 16) & 0x1ff; + if (!code) + usleep(1000); + } + if (!code) + return NAN; + + /* nvt_adc's formula for every die but NA51055: 1.319 degC per code */ + return ((int)code - na51089_thermal_trim()) * 1.319f + 30; } static int novatek_open_i2c_fd(int i2c_adapter_nr) { @@ -142,7 +271,9 @@ void novatek_setup_hal() { possible_i2c_addrs = novatek_possible_i2c_addrs; i2c_change_addr = i2c_changenshift_addr; open_i2c_sensor_fd = novatek_open_i2c_fd; - if (!access("/sys/class/thermal/thermal_zone0/temp", R_OK)) + if (chip_generation == CHIP_NA51089) + hal_temperature = na51089_get_temp; + else if (!access("/sys/class/thermal/thermal_zone0/temp", R_OK)) hal_temperature = novatek_get_temp; #ifndef STANDALONE_LIBRARY hal_totalmem = novatek_totalmem; diff --git a/src/hal/novatek_test.c b/src/hal/novatek_test.c new file mode 100644 index 0000000..d6e772a --- /dev/null +++ b/src/hal/novatek_test.c @@ -0,0 +1,246 @@ +/* NA51089's direct temperature read, driven against a model of the eFuse + * controller and the SAR ADC. + * + * The model answers the way the lab NT98566 did: the ADC gated and idle from + * boot, its thermal average reading 0 until channel 3 is converting, and eFuse + * row 6 holding 0x000080E4. Writes are checked as they happen, so a write that + * could program a fuse, or one that takes away a clock or a converter someone + * else started, names itself where it is made. + * + * CHECK rather than assert(), because the release flags carry -DNDEBUG and + * would compile an assert-based test into one that passes unconditionally. + * + * novatek.c is included rather than linked, for the same reason + * hal_hisi_test.c includes hal_hisi.c: the readers are file-statics, and a + * seam on mem_reg() would cost an indirect call in every ipcinfo. */ +#include +#include +#include +#include +#include + +#include "hal/novatek.c" + +static int failures; + +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, " FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + failures++; \ + } \ + } while (0) + +/* ---- the model --------------------------------------------------------- */ + +static struct { + uint32_t cg_en1, adc_ctrl; + uint32_t efuse_mode, efuse_row, efuse_data, efuse_ctrl, efuse_sts; + uint32_t fuse[32]; + uint32_t code; /* what channel 3 averages to once it converts */ +} M; + +static int efuse_writes, adc_writes, cg_writes; +static const char *illegal; + +static void fresh(void) { + memset(&M, 0, sizeof M); + /* the lab camera, nightly 2026-10-21, as found */ + M.cg_en1 = 0xB0440FF1; + M.adc_ctrl = 0x5000F000; + M.efuse_sts = 0x0000000C; + M.fuse[6] = 0x000080E4; + M.code = 265; + efuse_writes = adc_writes = cg_writes = 0; + illegal = NULL; + chip_generation = CHIP_NA51089; +} + +static bool converting(void) { + const uint32_t on = NA51089_ADC_AIN3_MODE | NA51089_ADC_EN; + return (M.cg_en1 & NA51089_CG_ADC) && (M.adc_ctrl & on) == on; +} + +bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { + if (op == OP_READ) { + switch (addr) { + case NA51089_CG_CLK_EN1: + *data = M.cg_en1; + return true; + case NA51089_ADC_CTRL: + *data = M.adc_ctrl; + return true; + case NA51089_ADC_THERMAL: + *data = 0x30 | (converting() ? M.code << 16 : 0); + return true; + case EFUSE_MODE: + *data = M.efuse_mode; + return true; + case EFUSE_ROW: + *data = M.efuse_row; + return true; + case EFUSE_DATA: + *data = M.efuse_data; + return true; + case EFUSE_CTRL: + *data = M.efuse_ctrl; + return true; + case EFUSE_STS: + *data = M.efuse_sts; + return true; + } + return false; + } + + const uint32_t v = *data; + switch (addr) { + case NA51089_CG_CLK_EN1: + cg_writes++; + if (M.cg_en1 & ~v) + illegal = "gated a clock that was running"; + M.cg_en1 = v; + return true; + case NA51089_ADC_CTRL: + adc_writes++; + if (M.adc_ctrl & ~v) + illegal = "cleared an ADC control bit that was set"; + M.adc_ctrl = v; + return true; + } + + efuse_writes++; + switch (addr) { + case EFUSE_MODE: + /* efuse_open()'s first argument, bit 0, selects programming */ + if (v) + illegal = "eFuse taken out of read mode"; + M.efuse_mode = v; + return true; + case EFUSE_ROW: + M.efuse_row = v; + return true; + case EFUSE_CTRL: + if ((v & 0x10) && !(M.efuse_ctrl & 1)) + illegal = "eFuse read triggered while closed"; + if ((v & 0x10) && M.efuse_mode) + illegal = "eFuse triggered outside read mode"; + M.efuse_ctrl = v & ~0x10u; /* the trigger is self-clearing */ + M.efuse_sts = (M.efuse_sts & ~1u) | (v & 1); + if (v & 0x10) { + M.efuse_data = M.fuse[M.efuse_row & 0x1f]; + M.efuse_sts |= 2; + } + return true; + case EFUSE_STS: + M.efuse_sts &= ~(v & 2); /* write-one-to-clear */ + return true; + } + return false; +} + +/* The rest of novatek.c's externals. disable_printk() is not optional: the + * real one silences the host's kernel log if this ever runs as root. */ +bool line_from_file(const char *filename, const char *re, char *buf, size_t n) { + (void)filename; + (void)re; + (void)buf; + (void)n; + return false; +} +void disable_printk(void) {} +void restore_printk(void) {} +int chip_generation; +char chip_name[128]; + +static bool efuse_closed(void) { + return !(M.efuse_ctrl & 1) && !(M.efuse_sts & 1) && M.efuse_row == 0; +} + +/* (code - trim) * 1.319 + 30 */ +static bool near(float t, int code, int trim) { + return fabsf(t - ((code - trim) * 1.319f + 30)) < 0.01f; +} + +int main(void) { + /* novatek_setup_hal() is what installs the reader, so go through it. */ + fresh(); + novatek_setup_hal(); + CHECK(hal_temperature == na51089_get_temp); + + /* The lab camera: ADC gated and idle, trim 0xE4 fused. Started as the + * RTOS side starts it, read, eFuse left closed: 78.80 degC. */ + float t = hal_temperature(); + CHECK(near(t, 265, 228)); + CHECK(M.cg_en1 == (0xB0440FF1 | NA51089_CG_ADC)); + CHECK(M.adc_ctrl == 0x5080F008); + CHECK(efuse_closed()); + CHECK(illegal == NULL); + + /* A second read touches neither the clock nor the ADC. */ + cg_writes = adc_writes = 0; + t = hal_temperature(); + CHECK(near(t, 265, 228)); + CHECK(cg_writes == 0 && adc_writes == 0); + + /* An ADC someone else runs is read, not reconfigured: the vendor driver + * leaves clkdiv 1 and all four channels continuous. */ + fresh(); + M.cg_en1 |= NA51089_CG_ADC; + M.adc_ctrl = 0x5080100F; + t = na51089_get_temp(); + CHECK(near(t, 265, 228)); + CHECK(cg_writes == 0 && adc_writes == 0); + CHECK(illegal == NULL); + + /* Converting but with channel 3 one-shot: only ain3_mode is added. */ + fresh(); + M.cg_en1 |= NA51089_CG_ADC; + M.adc_ctrl = 0x50801007; + t = na51089_get_temp(); + CHECK(near(t, 265, 228)); + CHECK(M.adc_ctrl == 0x5080100F); + CHECK(illegal == NULL); + + /* An unfused die gets nvt_adc's default. */ + fresh(); + M.fuse[6] = 0; + CHECK(near(na51089_get_temp(), 265, NA51089_DEFAULT_TRIM)); + + /* The first copy invalidated (bit 14): the second one is used. */ + fresh(); + M.fuse[6] = (0x80E0u << 16) | 0xC0E4; + CHECK(near(na51089_get_temp(), 265, 0xE0)); + + /* Neither copy good: the default, not a stray 14-bit value. */ + fresh(); + M.fuse[6] = (0xC0E0u << 16) | 0x40E4; + CHECK(near(na51089_get_temp(), 265, NA51089_DEFAULT_TRIM)); + + /* The kernel mid-sequence: the controller is left alone entirely, and + * the reading falls back to the default trim rather than failing. */ + fresh(); + M.efuse_ctrl = 1; + M.efuse_sts |= 1; + t = na51089_get_temp(); + CHECK(near(t, 265, NA51089_DEFAULT_TRIM)); + CHECK(efuse_writes == 0); + + /* A converter that never produces a sample is no reading. */ + fresh(); + M.code = 0; + CHECK(isnan(na51089_get_temp())); + + /* Every other die keeps the sysfs reader, if the host even has a + * thermal zone; the registers above are NA51089's alone. */ + fresh(); + chip_generation = CHIP_NA51055; + hal_temperature = NULL; + novatek_setup_hal(); + CHECK(hal_temperature != na51089_get_temp); + + if (failures) + fprintf(stderr, "novatek_test: %d failure(s)\n", failures); + else + printf("novatek_test: ok\n"); + return failures ? 1 : 0; +} From 0b5eb872a8c4de252ef69bc2d1ba2423711efbcc Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:17:13 +0300 Subject: [PATCH 2/2] novatek: take the NA51089 trim only from a clean read, and fall back to sysfs From review on #276: - A kernel without nvt_pinmux names the die through its device-tree model ("Novatek NA51089") but left chip_generation unset, so no temperature reader was installed. The model now sets it. - With /dev/mem withheld, the direct reader returned no reading even where the vendor driver's thermal zone works. It now falls back to it. - A read-done bit left standing by an earlier reader passed the wait for ours while DATA still held that reader's row. It is cleared before the trigger. - Nothing here can take the kernel's eFuse semaphore or lock out a second ipctool, so the row is read twice and taken only if both agree; an interleaved sequence costs the calibration, not a wrong one. The register accesses go through two small helpers, which pays for all of it: ipcinfo now grows 880 bytes over master (IPCHW_VENDORS=all), not 928. novatek_test covers each case with a model whose eFuse reads take a few polls to land. Lab NT98566: code 259, trim 228, 70.89 degC. --- src/hal/novatek.c | 121 ++++++++++++++++++++++------------------- src/hal/novatek_test.c | 77 +++++++++++++++++++++++--- 2 files changed, 135 insertions(+), 63 deletions(-) diff --git a/src/hal/novatek.c b/src/hal/novatek.c index f6211a4..96ad218 100644 --- a/src/hal/novatek.c +++ b/src/hal/novatek.c @@ -94,6 +94,10 @@ bool novatek_detect_cpu(char *chip_name) { return false; strcpy(buf, "NA51055"); } + /* A kernel with no nvt_pinmux still names the die in its model, and + * NA51089 is the one whose temperature reader keys on the ID. */ + if (!strcmp(buf, "NA51089")) + chip_generation = CHIP_NA51089; strcpy(chip_name, buf); @@ -152,61 +156,68 @@ float novatek_get_temp() { /* nvt_adc's DEFAULT_THERMAL_528_LEVEL, for a die with no trim fused */ #define NA51089_DEFAULT_TRIM 231 +/* A failed read reads as 0, which no wait below accepts and no trim + * decodes, so the eFuse path needs no error handling of its own. */ +static uint32_t nvt_rd(uint32_t addr) { + uint32_t val = 0; + mem_reg(addr, &val, OP_READ); + return val; +} + +static void nvt_wr(uint32_t addr, uint32_t val) { + mem_reg(addr, &val, OP_WRITE); +} + static bool nvt_wait(uint32_t addr, uint32_t mask, uint32_t want) { - uint32_t val; - for (int i = 0; i < 1000; i++) { - if (!mem_reg(addr, &val, OP_READ)) - return false; - if ((val & mask) == want) + for (int i = 0; i < 1000; i++) + if ((nvt_rd(addr) & mask) == want) return true; - } return false; } static bool nvt_efuse_row(uint32_t row, uint32_t *data) { - uint32_t val; - bool ok = false; - /* The kernel serialises this block with a semaphore we cannot take. If * it is open, someone is mid-sequence: leave it to them. */ - if (!mem_reg(EFUSE_CTRL, &val, OP_READ) || (val & 1)) - return false; - val |= 1; - if (!mem_reg(EFUSE_CTRL, &val, OP_WRITE)) + const uint32_t ctrl = nvt_rd(EFUSE_CTRL); + if (ctrl & 1) return false; - if (!nvt_wait(EFUSE_STS, 1, 1)) - goto close; - - val = 0; - mem_reg(EFUSE_MODE, &val, OP_WRITE); - mem_reg(EFUSE_ROW, &row, OP_WRITE); - if (!mem_reg(EFUSE_CTRL, &val, OP_READ)) - goto close; - val |= 0x10; - mem_reg(EFUSE_CTRL, &val, OP_WRITE); - if (!nvt_wait(EFUSE_CTRL, 0x10, 0) || !nvt_wait(EFUSE_STS, 2, 2)) - goto close; - if (mem_reg(EFUSE_STS, &val, OP_READ)) - mem_reg(EFUSE_STS, &val, OP_WRITE); /* write-one-to-clear */ - ok = mem_reg(EFUSE_DATA, data, OP_READ); - -close: - val = 0; - mem_reg(EFUSE_ROW, &val, OP_WRITE); - if (mem_reg(EFUSE_CTRL, &val, OP_READ)) { - val &= ~1u; - mem_reg(EFUSE_CTRL, &val, OP_WRITE); - nvt_wait(EFUSE_STS, 1, 0); + nvt_wr(EFUSE_CTRL, ctrl | 1); + + bool ok = false; + if (nvt_wait(EFUSE_STS, 1, 1)) { + nvt_wr(EFUSE_MODE, 0); + nvt_wr(EFUSE_ROW, row); + /* A read-done left by the last reader would pass the wait below + * while DATA still holds that reader's row. Write one to clear. */ + nvt_wr(EFUSE_STS, 2); + if (nvt_wait(EFUSE_STS, 2, 0)) { + nvt_wr(EFUSE_CTRL, nvt_rd(EFUSE_CTRL) | 0x10); + if (nvt_wait(EFUSE_CTRL, 0x10, 0) && nvt_wait(EFUSE_STS, 2, 2)) { + nvt_wr(EFUSE_STS, 2); + *data = nvt_rd(EFUSE_DATA); + ok = true; + } + } } + + nvt_wr(EFUSE_ROW, 0); + nvt_wr(EFUSE_CTRL, nvt_rd(EFUSE_CTRL) & ~1u); + nvt_wait(EFUSE_STS, 1, 0); return ok; } /* A row holds two copies, as efuse_readParamOps() reads them: bits [15:0] * first, then [31:16]. In each, bit 15 says programmed and bit 14 says - * invalidated; the trim is the low 14 bits of the first good one. */ + * invalidated; the trim is the low 14 bits of the first good one. + * + * The row is read twice and taken only if both agree. Nothing here can lock + * out the kernel or a second ipctool, and a sequence one of them interleaved + * with comes back as a different word, which costs the calibration and + * nothing else. */ static int na51089_thermal_trim() { - uint32_t row; - if (nvt_efuse_row(NA51089_THERMAL_TRIM_ROW, &row)) + uint32_t row, again; + if (nvt_efuse_row(NA51089_THERMAL_TRIM_ROW, &row) && + nvt_efuse_row(NA51089_THERMAL_TRIM_ROW, &again) && row == again) for (int half = 0; half < 2; half++) { const uint32_t w = (row >> (16 * half)) & 0xffff; if ((w & 0xc000) == 0x8000) @@ -215,33 +226,27 @@ static int na51089_thermal_trim() { return NA51089_DEFAULT_TRIM; } -static float na51089_get_temp() { +static float na51089_read_temp() { uint32_t val; + /* The one checked read: it tells a kernel that withholds /dev/mem from + * an ADC that is merely idle. A write that fails after it leaves the + * average at 0, which the poll below turns into no reading. */ if (!mem_reg(NA51089_CG_CLK_EN1, &val, OP_READ)) return NAN; - if (!(val & NA51089_CG_ADC)) { - val |= NA51089_CG_ADC; - if (!mem_reg(NA51089_CG_CLK_EN1, &val, OP_WRITE)) - return NAN; - } + if (!(val & NA51089_CG_ADC)) + nvt_wr(NA51089_CG_CLK_EN1, val | NA51089_CG_ADC); /* adc_en alone converts nothing: channel 3 must be in continuous mode. */ const uint32_t on = NA51089_ADC_AIN3_MODE | NA51089_ADC_EN; - if (!mem_reg(NA51089_ADC_CTRL, &val, OP_READ)) - return NAN; - if ((val & on) != on) { - val |= on; - if (!mem_reg(NA51089_ADC_CTRL, &val, OP_WRITE)) - return NAN; - } + val = nvt_rd(NA51089_ADC_CTRL); + if ((val & on) != on) + nvt_wr(NA51089_ADC_CTRL, val | on); /* The average reads 0 until the first conversion, well under 1 ms. */ uint32_t code = 0; for (int i = 0; i < 50 && !code; i++) { - if (!mem_reg(NA51089_ADC_THERMAL, &val, OP_READ)) - return NAN; - code = (val >> 16) & 0x1ff; + code = (nvt_rd(NA51089_ADC_THERMAL) >> 16) & 0x1ff; if (!code) usleep(1000); } @@ -252,6 +257,12 @@ static float na51089_get_temp() { return ((int)code - na51089_thermal_trim()) * 1.319f + 30; } +/* A kernel that withholds /dev/mem may still carry the vendor driver. */ +static float na51089_get_temp() { + const float t = na51089_read_temp(); + return isnan(t) ? novatek_get_temp() : t; +} + static int novatek_open_i2c_fd(int i2c_adapter_nr) { if (i2c_adapter_nr<0 || i2c_adapter_nr>4) i2c_adapter_nr=0; diff --git a/src/hal/novatek_test.c b/src/hal/novatek_test.c index d6e772a..c2ce73a 100644 --- a/src/hal/novatek_test.c +++ b/src/hal/novatek_test.c @@ -38,10 +38,15 @@ static struct { uint32_t efuse_mode, efuse_row, efuse_data, efuse_ctrl, efuse_sts; uint32_t fuse[32]; uint32_t code; /* what channel 3 averages to once it converts */ + int pending; /* STS reads until a triggered read completes */ + int reads_of_row6; + uint32_t second_read; /* if set, what an interleaved second read sees */ } M; static int efuse_writes, adc_writes, cg_writes; static const char *illegal; +static bool mmio_fails; /* /dev/mem withheld */ +static const char *dt_model, *sysfs; /* line_from_file's answers */ static void fresh(void) { memset(&M, 0, sizeof M); @@ -53,6 +58,8 @@ static void fresh(void) { M.code = 265; efuse_writes = adc_writes = cg_writes = 0; illegal = NULL; + mmio_fails = false; + dt_model = sysfs = NULL; chip_generation = CHIP_NA51089; } @@ -61,8 +68,22 @@ static bool converting(void) { return (M.cg_en1 & NA51089_CG_ADC) && (M.adc_ctrl & on) == on; } +static void complete_read(void) { + const uint32_t row = M.efuse_row & 0x1f; + M.efuse_data = M.fuse[row]; + if (row == 6 && M.reads_of_row6++ && M.second_read) + M.efuse_data = M.second_read; + M.efuse_sts |= 2; +} + bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { + if (mmio_fails) + return false; if (op == OP_READ) { + /* The read takes a few polls to land, as a real one does, so a + * read-done bit already standing is not this read's. */ + if (addr == EFUSE_STS && M.pending && !--M.pending) + complete_read(); switch (addr) { case NA51089_CG_CLK_EN1: *data = M.cg_en1; @@ -126,10 +147,8 @@ bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { illegal = "eFuse triggered outside read mode"; M.efuse_ctrl = v & ~0x10u; /* the trigger is self-clearing */ M.efuse_sts = (M.efuse_sts & ~1u) | (v & 1); - if (v & 0x10) { - M.efuse_data = M.fuse[M.efuse_row & 0x1f]; - M.efuse_sts |= 2; - } + if (v & 0x10) + M.pending = 3; return true; case EFUSE_STS: M.efuse_sts &= ~(v & 2); /* write-one-to-clear */ @@ -141,11 +160,16 @@ bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { /* The rest of novatek.c's externals. disable_printk() is not optional: the * real one silences the host's kernel log if this ever runs as root. */ bool line_from_file(const char *filename, const char *re, char *buf, size_t n) { - (void)filename; (void)re; - (void)buf; - (void)n; - return false; + const char *ans = NULL; + if (!strcmp(filename, "/proc/device-tree/model")) + ans = dt_model; + else if (!strcmp(filename, "/sys/class/thermal/thermal_zone0/temp")) + ans = sysfs; + if (!ans) + return false; + snprintf(buf, n, "%s", ans); + return true; } void disable_printk(void) {} void restore_printk(void) {} @@ -225,6 +249,43 @@ int main(void) { CHECK(near(t, 265, NA51089_DEFAULT_TRIM)); CHECK(efuse_writes == 0); + /* A read-done bit someone left standing, with their row still in DATA: + * that word is not row 6, and must not be taken for it. */ + fresh(); + M.efuse_sts |= 2; + M.efuse_data = 0x00008007; /* row 7, IDDQ: a "valid" trim of 7 */ + CHECK(near(na51089_get_temp(), 265, 228)); + CHECK(illegal == NULL); + + /* Two reads that disagree are someone else's sequence interleaved with + * ours: no calibration rather than the wrong one. */ + fresh(); + M.second_read = 0x000080F0; + CHECK(near(na51089_get_temp(), 265, NA51089_DEFAULT_TRIM)); + CHECK(M.reads_of_row6 == 2); + + /* /dev/mem withheld: the vendor driver's thermal zone, if there is one, + * still answers -- and with none, there is no reading. */ + fresh(); + mmio_fails = true; + sysfs = "66"; + CHECK(na51089_get_temp() == 66.0f); + sysfs = NULL; + CHECK(isnan(na51089_get_temp())); + + /* A kernel without nvt_pinmux: the device tree names the die, and that + * is enough to install the direct reader. */ + fresh(); + chip_generation = 0; + dt_model = "NA51089"; + char name[128] = ""; + CHECK(novatek_detect_cpu(name)); + CHECK(!strcmp(name, "NA51089")); + CHECK(chip_generation == CHIP_NA51089); + hal_temperature = NULL; + novatek_setup_hal(); + CHECK(hal_temperature == na51089_get_temp); + /* A converter that never produces a sample is no reading. */ fresh(); M.code = 0;