Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/pr-build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
152 changes: 147 additions & 5 deletions src/hal/novatek.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "hal/novatek.h"

#include <math.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand All @@ -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;
Expand Down
Loading
Loading