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..96ad218 100644 --- a/src/hal/novatek.c +++ b/src/hal/novatek.c @@ -1,5 +1,6 @@ #include "hal/novatek.h" +#include #include #include @@ -93,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); @@ -113,14 +118,149 @@ 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 + +/* 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) { + 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) { + /* The kernel serialises this block with a semaphore we cannot take. If + * it is open, someone is mid-sequence: leave it to them. */ + const uint32_t ctrl = nvt_rd(EFUSE_CTRL); + if (ctrl & 1) + return false; + 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; + } + } } - return ret; + + 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. + * + * 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, 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) + return w & 0x3fff; + } + return NA51089_DEFAULT_TRIM; +} + +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)) + 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; + 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++) { + code = (nvt_rd(NA51089_ADC_THERMAL) >> 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; +} + +/* 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) { @@ -142,7 +282,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..c2ce73a --- /dev/null +++ b/src/hal/novatek_test.c @@ -0,0 +1,307 @@ +/* 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 */ + 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); + /* 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; + mmio_fails = false; + dt_model = sysfs = 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; +} + +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; + 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.pending = 3; + 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)re; + 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) {} +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 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; + 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; +}