diff --git a/src/clocks.c b/src/clocks.c index 5ee2f56..02fe682 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -45,6 +45,46 @@ static uint32_t extract_field(uint32_t raw, uint8_t shift, uint8_t width) { return (raw >> shift) & mask; } +/* f = input * (FBDIV + FRACDIV/2^frac_width) / (REFDIV * POSTDIV1 * + * POSTDIV2), from the two raw control words. 0 when the fields cannot make a + * frequency (FBDIV or a divider of 0). */ +static double pll_freq_from_raw(const struct pll_info *pll, uint32_t raw1, + uint32_t raw2) { + uint32_t fracdiv = extract_field(raw1, pll->frac_shift, pll->frac_width); + uint32_t pdiv1 = + extract_field(raw1, pll->postdiv1_shift, pll->postdiv1_width); + uint32_t pdiv2 = + extract_field(raw1, pll->postdiv2_shift, pll->postdiv2_width); + uint32_t fbdiv = extract_field(raw2, pll->fbdiv_shift, pll->fbdiv_width); + uint32_t refdiv = extract_field(raw2, pll->refdiv_shift, pll->refdiv_width); + if (pll->postdiv1_width == 0) + pdiv1 = 1; + if (pll->postdiv2_width == 0) + pdiv2 = 1; + if (pll->refdiv_width == 0) + refdiv = 1; + + uint32_t denom = refdiv * pdiv1 * pdiv2; + if (fbdiv == 0 || denom == 0) + return 0.0; + uint64_t numer_khz = (uint64_t)pll->input_khz * fbdiv; + if (pll->frac_width) + numer_khz += ((uint64_t)pll->input_khz * fracdiv) >> pll->frac_width; + return (double)numer_khz / (double)denom / 1000.0; +} + +bool pll_read_freq_mhz(const struct pll_info *pll, double *mhz) { + uint32_t raw1 = 0, raw2; + if (!mem_reg(pll->ctrl_reg1, &raw1, OP_READ)) + return false; + raw2 = raw1; + if (pll->ctrl_reg2 && pll->ctrl_reg2 != pll->ctrl_reg1 && + !mem_reg(pll->ctrl_reg2, &raw2, OP_READ)) + return false; + *mhz = pll_freq_from_raw(pll, raw1, raw2); + return *mhz != 0.0; +} + static cJSON *decode_pll(const struct pll_info *pll, bool brief) { cJSON *j_inner = cJSON_CreateObject(); @@ -77,17 +117,7 @@ static cJSON *decode_pll(const struct pll_info *pll, bool brief) { if (pll->refdiv_width == 0) refdiv = 1; - /* Compute frequency. f = input * (FBDIV + FRACDIV/2^frac_width) / - * (REFDIV * POSTDIV1 * POSTDIV2). */ - double freq_mhz = 0.0; - uint32_t denom = refdiv * pdiv1 * pdiv2; - if (fbdiv != 0 && denom != 0) { - uint64_t numer_khz = (uint64_t)pll->input_khz * fbdiv; - if (pll->frac_width) - numer_khz += - ((uint64_t)pll->input_khz * fracdiv) >> pll->frac_width; - freq_mhz = (double)numer_khz / (double)denom / 1000.0; - } + double freq_mhz = pll_freq_from_raw(pll, raw1, raw2); if (brief) { ADD_PARAM_NUM("freq_mhz", freq_mhz); diff --git a/src/clocks.h b/src/clocks.h index 8b5bb28..701debc 100644 --- a/src/clocks.h +++ b/src/clocks.h @@ -92,6 +92,10 @@ struct clock_family { * HPM aux register, etc.) -- used by `ipctool clocks`. */ cJSON *clocks_build_json(bool brief); +/* One PLL's output frequency, read live. False if a register read fails or + * the fields make no frequency. */ +bool pll_read_freq_mhz(const struct pll_info *pll, double *mhz); + int clocks_cmd(int argc, char **argv); #endif /* CLOCKS_H */ diff --git a/src/hal/common.c b/src/hal/common.c index 7cefdc6..6404dad 100644 --- a/src/hal/common.c +++ b/src/hal/common.c @@ -12,6 +12,7 @@ #include "tools.h" int i2c_adapter_nr = 0; +const int *hal_sensor_buses; sensor_addr_t *possible_i2c_addrs; int (*open_i2c_sensor_fd)(); int (*open_spi_sensor_fd)(); diff --git a/src/hal/common.h b/src/hal/common.h index a6391d8..6acd5e5 100644 --- a/src/hal/common.h +++ b/src/hal/common.h @@ -76,6 +76,12 @@ typedef struct { } sensor_addr_t; extern int i2c_adapter_nr; +/* The i2c buses a board family carries sensors on, -1-terminated, for SoCs + * with more than one sensor interface. NULL (the default) means one sensor: + * the report stops at the first hit, as it always has. Only set it where + * open_i2c_sensor_fd() opens /dev/i2c- as asked, or the + * sweep would find one sensor on every number it tries. */ +extern const int *hal_sensor_buses; extern sensor_addr_t *possible_i2c_addrs; typedef int (*read_register_t)(int fd, unsigned char i2c_addr, diff --git a/src/hal/hisi/clocks_v5.c b/src/hal/hisi/clocks_v5.c index 0220e12..e4df7c9 100644 --- a/src/hal/hisi/clocks_v5.c +++ b/src/hal/hisi/clocks_v5.c @@ -46,6 +46,9 @@ * bits[9:0]) is surfaced through the standard hpm_info table. */ +#include + +#include "chipid.h" #include "cjson/cJSON.h" #include "clocks.h" #include "hal/hisi/hal_hisi.h" @@ -157,15 +160,59 @@ static const char *v5_dram_type_name(uint32_t t) { } } +/* Hi3519DV500/Hi3516DV500 A55 core clock: PERI_CRG2064 (CRG+0x2040) + * a55_cksel [10:8] picks one of seven sources, and only one of them is the + * APLL -- so "cpu_pll" above is not the CPU's frequency. Encoding from the + * DV500 data sheet; 6 (1000 MHz) needs cpu_cksel_limit_en=0xA. A Taobao + * DV500 kit reads 0x2600: a55_cksel 6, the CPU at 1 GHz with the APLL at + * 750 MHz beside it. The CV610's single A7 has a different register, so this + * is DV500-only. */ +static void v5_dv500_cpu(cJSON *root, bool brief) { + static const unsigned fixed_mhz[8] = {24, 675, 0, 792, 833, 900, 1000, 0}; + uint32_t crg2064; + + if (!strstr(chip_name, "DV500") || + !mem_reg(V5_CRG_BASE + 0x2040, &crg2064, OP_READ)) + return; + + const unsigned sel = (crg2064 >> 8) & 0x7; + double mhz = fixed_mhz[sel]; + const char *source = "fixed"; + if (sel == 2) { + if (!pll_read_freq_mhz(&v5_plls[0], &mhz)) /* cpu_pll: the APLL */ + return; + source = "cpu_pll"; + } + if (mhz == 0) + return; /* 7: reserved */ + + cJSON *cpu = cJSON_CreateObject(); + cJSON *j_inner = cpu; + ADD_PARAM_NUM("freq_mhz", mhz); + ADD_PARAM("source", source); + if (!brief) { + ADD_PARAM_FMT("crg2064", "0x%08x", crg2064); + ADD_PARAM_NUM("a55_cksel", sel); + } + cJSON_AddItemToObject(root, "cpu", cpu); +} + /* Decode the actual DDR data rate. The chain is: * ddr_cksel (CRG+0x2000 bits [18:16]) -- 0 = 24 MHz, 1 = clk_dpll_pst - * DPLL VCO (CRG+0x180 / 0x184, post-divided) -- already decoded as - * ddr_pll dficlk_ratio (DDR_PHY0+0x78 bits [1:0]) -- 0=1:1, 1=1:2, 2=1:4 DRAM - * data rate (MT/s) = phy_clk_mhz * (1 << dficlk_ratio). In the typical config - * (DDR3/DDR4 -> 1:2, LPDDR4 -> 1:4) the multiplier matches the DDR burst - * factor, so this is also "post-divided PLL frequency times the bus - * double-data-rate". */ + * DPLL VCO (CRG+0x180 / 0x184, post-divided) -- the DFI clock + * dficlk_ratio (DDR_PHY0+0x78 bits [1:0]) -- DFI:DRAM clock, 0=1:1, 1=1:2, + * 2=1:4, so the DRAM clock is DFI * (1 << dficlk_ratio) + * and the data rate is twice the DRAM clock, data being moved on both edges: + * data rate (MT/s) = phy_clk_mhz * (1 << dficlk_ratio) * 2. + * The Hi3519DV500 data sheet's clock tree runs the DPLL at 667 MHz for its + * "up to 2666 Mbps DDR4"; a Taobao DV500 kit whose boot table is + * ..._DDR4_2666M-4GB reads DPLL 664 MHz, ratio 1:2 -> 2656 MT/s. Without the + * final factor of two this reported the DRAM clock, 1328, as the rate. */ static void v5_extra(cJSON *root, bool brief) { + /* First, and on its own registers: a DDR read failing below must not + * take the CPU clock with it. */ + v5_dv500_cpu(root, brief); + uint32_t crg_cksel = 0; uint32_t phy_ctrl0 = 0; uint32_t phy_dramcfg = 0; @@ -199,7 +246,7 @@ static void v5_extra(cJSON *root, bool brief) { /* Source: at boot/training time ddr_cksel can momentarily switch to * 24 MHz; in normal operation it stays on the DPLL post-divider. */ double phy_clk_mhz = (ddr_cksel == 0) ? 24.0 : dpll_mhz; - double data_rate_mtps = phy_clk_mhz * (double)(1u << dficlk_ratio); + double data_rate_mtps = phy_clk_mhz * (double)(1u << dficlk_ratio) * 2.0; cJSON *ddr = cJSON_CreateObject(); cJSON *j_inner = ddr; diff --git a/src/hal/hisi/hal_hisi.c b/src/hal/hisi/hal_hisi.c index 40aeb09..94a5a27 100644 --- a/src/hal/hisi/hal_hisi.c +++ b/src/hal/hisi/hal_hisi.c @@ -473,8 +473,13 @@ typedef struct { bool owed; /* an undo is outstanding */ } sensor_crg_t; -static void sensor_crg_enable( - sensor_crg_t *st, uint32_t addr, uint32_t cken, uint32_t srst) { +/* sel_mask/sel_safe: a clock-select field and the value to put in it when + * we ungate a clock whose select still holds its reset value (0). Only a + * clock we find gated gets one: a running clock was set up by somebody, at + * whatever rate they chose. Pass 0/0 where the register has no such field. */ +static void sensor_crg_enable(sensor_crg_t *st, uint32_t addr, uint32_t cken, + uint32_t srst, uint32_t sel_mask, + uint32_t sel_safe) { uint32_t cur; /* A read that failed taught us nothing. Drop any entitlement rather than @@ -486,7 +491,9 @@ static void sensor_crg_enable( return; } - const uint32_t want = (cur | cken) & ~srst; + uint32_t want = (cur | cken) & ~srst; + if (sel_mask && !(cur & cken) && !(cur & sel_mask)) + want = (want & ~sel_mask) | sel_safe; if (want == cur) { /* Already fit for a probe, so this arm takes nothing -- and drops * anything an earlier one held. @@ -545,7 +552,8 @@ static sensor_crg_t gk7206_crg; static void gk7206_ensure_sensor_enabled() { sensor_crg_enable(&gk7206_crg, EV300_PERI_CRG60_ADDR, - EV300_PERI_CRG60_SENSOR0_CKEN, EV300_PERI_CRG60_SENSOR0_SRST); + EV300_PERI_CRG60_SENSOR0_CKEN, + EV300_PERI_CRG60_SENSOR0_SRST, 0, 0); } static void gk7206_ensure_sensor_restored() { @@ -558,7 +566,8 @@ static sensor_crg_t v4_crg; static void v4_ensure_sensor_enabled() { sensor_crg_enable(&v4_crg, EV300_PERI_CRG60_ADDR, - EV300_PERI_CRG60_SENSOR0_CKEN, EV300_PERI_CRG60_SENSOR0_SRST); + EV300_PERI_CRG60_SENSOR0_CKEN, + EV300_PERI_CRG60_SENSOR0_SRST, 0, 0); } static void v4_ensure_sensor_restored() { @@ -575,15 +584,91 @@ static const uint32_t ot_crg_addr[2] = { }; static sensor_crg_t ot_crg[2]; +/* Hi3519DV500/Hi3516DV500: the SENSOR1 connector of the reference design is + * controlled over I2C5, whose pads are SPI1_SCLK (iocfg_reg109) and SPI1_SDO + * (iocfg_reg110) -- function 0 GPIO1_0/GPIO1_1, 1 SPI1, 2 I2C5 (data sheet + * pin table). The vendor sys_config.ko only muxes them when built with + * I2C_EX_EN, which the stock SDK is not, so on a stock board /dev/i2c-5 + * exists, every transfer on it times out, and a sensor on SENSOR1 is + * invisible. Measured on a Taobao DV500 kit with an OS08A20 there. + * + * Same rules as the clock above. We switch a pad to I2C5 only while it is an + * unclaimed GPIO input -- never one in SPI1 mode, never a GPIO the board + * drives as an output -- and put back exactly what we found, and only if the + * pad still holds what we left in it. */ +#define DV500_IOCFG_SPI1_SCLK 0x0EFF0024u +#define DV500_IOCFG_SPI1_SDO 0x0EFF0028u +#define DV500_GPIO1_DIR 0x11091400u /* PL061 GPIODIR, bank 1 */ +#define DV500_IOCFG_FUNC_MASK 0xFu +#define DV500_IOCFG_FUNC_GPIO 0x0u +#define DV500_IOCFG_FUNC_I2C5 0x2u + +static const uint32_t dv500_i2c5_pad[2] = { + DV500_IOCFG_SPI1_SCLK, /* GPIO1_0 */ + DV500_IOCFG_SPI1_SDO, /* GPIO1_1 */ +}; +static sensor_crg_t dv500_i2c5[2]; + +static bool is_dv500(void) { return strstr(chip_name, "DV500") != NULL; } + +static void dv500_i2c5_enable(void) { + uint32_t cur[2], dir; + + if (!mem_reg(dv500_i2c5_pad[0], &cur[0], OP_READ) || + !mem_reg(dv500_i2c5_pad[1], &cur[1], OP_READ) || + !mem_reg(DV500_GPIO1_DIR, &dir, OP_READ)) { + for (size_t i = 0; i < ARRCNT(dv500_i2c5); i++) + dv500_i2c5[i].owed = false; /* we no longer know what is there */ + return; + } + + /* An undo outlives a re-arm only while the pad still holds our word. The + * clock above cannot do this -- its word is the SDK's word too -- but + * ours keeps the found drive/pull bits and changes only the function, + * while the vendor's I2C5 setup writes its own (0x1752), so "still ours" + * is readable here. Without it the arm that follows the SPI probe, which + * has no cleanup of its own, would find I2C5 already set, take nothing, + * and leave the pads switched after we exit. */ + for (size_t i = 0; i < 2; i++) + if (dv500_i2c5[i].owed && cur[i] != dv500_i2c5[i].wrote) + dv500_i2c5[i].owed = false; + + for (size_t i = 0; i < 2; i++) { + const uint32_t func = cur[i] & DV500_IOCFG_FUNC_MASK; + if (func == DV500_IOCFG_FUNC_I2C5) + continue; + if (func != DV500_IOCFG_FUNC_GPIO || (dir & (1u << i))) + return; /* somebody's SPI1 or GPIO output: not ours, either pad */ + } + + for (size_t i = 0; i < 2; i++) { + if ((cur[i] & DV500_IOCFG_FUNC_MASK) == DV500_IOCFG_FUNC_I2C5) + continue; + sensor_crg_t *st = &dv500_i2c5[i]; + st->found = cur[i]; + st->wrote = (cur[i] & ~DV500_IOCFG_FUNC_MASK) | DV500_IOCFG_FUNC_I2C5; + if (mem_reg(dv500_i2c5_pad[i], &st->wrote, OP_WRITE)) + st->owed = true; + } +} + static void ot_ensure_sensor_enabled(void) { for (size_t i = 0; i < ARRCNT(ot_crg_addr); i++) - sensor_crg_enable(&ot_crg[i], ot_crg_addr[i], - CV610_PERI_CRG_SENSOR0_CKEN, CV610_PERI_CRG_SENSOR0_SRST); + sensor_crg_enable( + &ot_crg[i], ot_crg_addr[i], CV610_PERI_CRG_SENSOR0_CKEN, + CV610_PERI_CRG_SENSOR0_SRST, CV610_PERI_CRG_SENSOR_CKSEL_MASK, + CV610_PERI_CRG_SENSOR_CKSEL_24M); } +/* The vendor SDK's sensor buses on DV500: SENSOR0..3 = I2C4, I2C5, I2C3, + * I2C6 (sample_comm_vi.c, sample_vio.c, sample_dis_two_pipe.c). */ +static const int dv500_sensor_buses[] = {4, 5, 3, 6, -1}; + static void ot_ensure_sensor_restored() { for (size_t i = 0; i < ARRCNT(ot_crg_addr); i++) sensor_crg_restore(&ot_crg[i], ot_crg_addr[i]); + for (size_t i = 0; i < ARRCNT(dv500_i2c5); i++) + sensor_crg_restore(&dv500_i2c5[i], dv500_i2c5_pad[i]); } /* Installed as hal_enable_sensor_clock so the bus sweep in getsensorid() can @@ -628,6 +713,18 @@ static void hisi_ensure_sensor_enabled() { } } +/* What a probe arms: the clocks, as setup_hal_hisi() also does, plus on DV500 + * the I2C5 pads. Those are switched only here, never at setup, because only a + * probe is followed by the hal_cleanup() that switches them back -- a + * chip-name or clocks query goes through setup and nothing else. */ +static void hisi_ensure_sensor_ready() { + hisi_ensure_sensor_enabled(); +#ifdef IPCHW_HISI_V5 + if (chip_generation == HISI_OT && is_dv500()) + dv500_i2c5_enable(); +#endif +} + static void hisi_hal_cleanup() { switch (chip_generation) { #ifdef IPCHW_HISI_V4 @@ -676,8 +773,12 @@ static void get_hisi_sdk(cJSON *j_inner) { #endif void setup_hal_hisi() { - hal_enable_sensor_clock = hisi_ensure_sensor_enabled; + hal_enable_sensor_clock = hisi_ensure_sensor_ready; hisi_ensure_sensor_enabled(); +#ifdef IPCHW_HISI_V5 + if (chip_generation == HISI_OT && is_dv500()) + hal_sensor_buses = dv500_sensor_buses; +#endif open_i2c_sensor_fd = hisi_open_i2c_fd; open_spi_sensor_fd = hisi_open_spi_fd; diff --git a/src/hal/hisi/hal_hisi_test.c b/src/hal/hisi/hal_hisi_test.c index ac85f90..e44149e 100644 --- a/src/hal/hisi/hal_hisi_test.c +++ b/src/hal/hisi/hal_hisi_test.c @@ -40,7 +40,7 @@ static int failures; /* ---- the fake register file -------------------------------------------- */ -#define SLOTS 4 +#define SLOTS 8 static struct { uint32_t addr; @@ -164,6 +164,7 @@ void restore_printk(void) {} /* chipid.c's, normally. Naming it here is what keeps chipid.c and every other * vendor HAL out of the link. */ int chip_generation; +char chip_name[128]; /* ---- helpers ----------------------------------------------------------- */ @@ -172,6 +173,9 @@ static void fresh(uint32_t addr, uint32_t val, int gen) { memset(&UNDO, 0, sizeof UNDO); memset(&v4_crg, 0, sizeof v4_crg); memset(ot_crg, 0, sizeof ot_crg); + memset(dv500_i2c5, 0, sizeof dv500_i2c5); + hal_sensor_buses = NULL; + strcpy(chip_name, gen == HISI_OT ? "3516CV610" : ""); chip_generation = gen; seed(addr, val); if (addr == CV610_PERI_CRG8464_ADDR) @@ -353,6 +357,127 @@ int main(void) { CHECK(peek(CV610_PERI_CRG8464_ADDR) == (CV610_PERI_CRG_SENSOR0_CKEN | CV610_PERI_CRG_SENSOR0_SRST)); + /* ---- OT: the clock select a gated clock is given -------------------- + * PERI_CRG8464/8472 reset to 0x3: cksel 0 = 74.25 MHz, above what an + * OS08A/OS04A takes. A clock we ungate from that state gets 24 MHz; the + * cleanup puts the reset value back. */ + fresh(OT, 0x3, HISI_OT); + setup_hal_hisi(); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0x4012); + CHECK(peek(CV610_PERI_CRG8472_ADDR) == 0x4012); + hal_cleanup(); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0x3); + CHECK(peek(CV610_PERI_CRG8472_ADDR) == 0x3); + CHECK(illegal == NULL); + + /* A gated clock whose select somebody already chose keeps it. */ + fresh(OT, 0xA003, HISI_OT); + setup_hal_hisi(); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0xA012); + + /* A running clock, even at 74.25 MHz, is somebody's: untouched. */ + fresh(OT, 0x0012, HISI_OT); + setup_hal_hisi(); + reset_counters(); + sweep(); + CHECK(writes == 0); + CHECK(peek(CV610_PERI_CRG8464_ADDR) == 0x0012); + + /* ---- DV500: the I2C5 pads of the SENSOR1 bus ------------------------ */ +#define SCLK DV500_IOCFG_SPI1_SCLK +#define SDO DV500_IOCFG_SPI1_SDO +#define DIR DV500_GPIO1_DIR + /* Setup alone -- a chip-name or clocks query, which never reaches a + * cleanup -- leaves the pads alone; only a probe's arm switches them. + * Stock sys_config leaves them GPIO inputs (0x1300): switched to I2C5, + * function bits only, and put back by the cleanup. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + CHECK(hal_sensor_buses && hal_sensor_buses[0] == 4 && + hal_sensor_buses[1] == 5); + hal_enable_sensor_clock(); + CHECK(peek(SCLK) == 0x1302); + CHECK(peek(SDO) == 0x1302); + hal_cleanup(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + + /* The whole sweep, SPI arm included (no cleanup of its own): the pads + * still end where they started. */ + hal_enable_sensor_clock(); + hal_cleanup(); + sweep(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + + /* In SPI1 mode they are the SPI controller's: neither pad is touched. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1301); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + hal_enable_sensor_clock(); + CHECK(peek(SCLK) == 0x1301); + CHECK(peek(SDO) == 0x1300); + + /* A GPIO the board drives (an IR-cut, a LED) is not ours either. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x2); /* GPIO1_1 is an output */ + setup_hal_hisi(); + hal_enable_sensor_clock(); + CHECK(peek(SCLK) == 0x1300); + CHECK(peek(SDO) == 0x1300); + + /* Already I2C5 (the board's rcS did it): no write at all, and the + * cleanup does not "restore" anything. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1752); + seed(SDO, 0x1752); + seed(DIR, 0x0); + reset_counters(); + setup_hal_hisi(); + sweep(); + CHECK(writes == 0); + CHECK(peek(SCLK) == 0x1752); + + /* Re-muxed by somebody else between our arm and our cleanup: theirs. */ + fresh(OT, 0x4012, HISI_OT); + strcpy(chip_name, "3519DV500"); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + hal_enable_sensor_clock(); + poke(SCLK, 0x1752); + hal_cleanup(); + CHECK(peek(SCLK) == 0x1752); + CHECK(peek(SDO) == 0x1300); + + /* Not a DV500: the pads are not even looked at, and the report stays + * single-sensor. */ + fresh(OT, 0x4012, HISI_OT); + seed(SCLK, 0x1300); + seed(SDO, 0x1300); + seed(DIR, 0x0); + setup_hal_hisi(); + sweep(); + CHECK(peek(SCLK) == 0x1300); + CHECK(hal_sensor_buses == NULL); +#undef SCLK +#undef SDO +#undef DIR + /* V3 ungates without ever restoring, which is exactly why it cannot * suffer this. A guard, so it does not grow a restore by symmetry. */ fresh(CV300_PERI_CRG11_ADDR, 0, HISI_V3); diff --git a/src/hal/hisi/ispreg.h b/src/hal/hisi/ispreg.h index a15d2e6..00e8c6b 100644 --- a/src/hal/hisi/ispreg.h +++ b/src/hal/hisi/ispreg.h @@ -43,6 +43,12 @@ struct CV610_PERI_CRG8464 { /* And the same for OT, where the two fields sit in different places. */ #define CV610_PERI_CRG_SENSOR0_SRST 0x1u #define CV610_PERI_CRG_SENSOR0_CKEN 0x10u +/* sensorN_cksel [15:12]. The reset value 0 is 74.25 MHz on both CV610 and + * DV500 (data sheets, PERI_CRG8464/8472), above the 27 MHz an OS08A10/A20 or + * the 36 MHz an OS04A10 accepts; 4 is 24 MHz, which every sensor we probe + * takes. */ +#define CV610_PERI_CRG_SENSOR_CKSEL_MASK 0xF000u +#define CV610_PERI_CRG_SENSOR_CKSEL_24M 0x4000u const char *hisi_detect_fmc(); void hisi_chip_properties(cJSON *j_inner); diff --git a/src/sensors.c b/src/sensors.c index 90db816..d955503 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -1483,11 +1483,19 @@ static void arm_sensor_clock(void) { hal_enable_sensor_clock(); } +/* What the last probe_sensor() already covered, so detect_sensors() does not + * probe an empty bus twice -- on a SoC with unmuxed adapters every probe of + * one is a run of controller timeouts. */ +static int probed_preferred_adapter = -1; +static bool found_by_sweep; + static bool probe_sensor(sensor_ctx_t *ctx) { if (!getchipname()) return false; const int preferred_adapter = i2c_adapter_nr; + probed_preferred_adapter = preferred_adapter; + found_by_sweep = false; // Use common settings as default ctx->data_width = 1; @@ -1529,6 +1537,7 @@ static bool probe_sensor(sensor_ctx_t *ctx) { arm_sensor_clock(); if (get_sensor_id_i2c(ctx)) { strcpy(ctx->control, "i2c"); + found_by_sweep = true; return true; } } @@ -1556,35 +1565,116 @@ bool getsensorid(sensor_ctx_t *ctx) { #ifndef STANDALONE_LIBRARY +/* Parts that answer with the same ID registers, so a probe cannot tell them + * apart. sensor_id keeps the name it always had -- libipchw consumers pick a + * driver by it -- and the report says what else it could be. */ +static const char *sensor_id_shared_with(const char *sensor_id) { + /* OS08A10 and OS08A20: 0x300A..C = 53 08 41 and 0x302A = 0xB0 in every + * revision of both data sheets; the A20 differs only by Nyxel NIR + * sensitivity, which no register shows. */ + if (!strcmp(sensor_id, "OS08A10")) + return "OS08A20"; + return NULL; +} + +static void add_sensor_entry(cJSON *j_sensors, sensor_ctx_t *ctx, int bus, + bool vi_info) { + cJSON *j_inner = ctx->j_sensor; + cJSON_AddItemToArray(j_sensors, j_inner); + + ADD_PARAM("vendor", ctx->vendor); + ADD_PARAM("model", ctx->sensor_id); + const char *twin = sensor_id_shared_with(ctx->sensor_id); + if (twin) + ADD_PARAM("id_shared_with", twin); + { + cJSON *j_inner = cJSON_CreateObject(); + cJSON_AddItemToObject(ctx->j_sensor, "control", j_inner); + ADD_PARAM_NUM("bus", bus); + ADD_PARAM("type", ctx->control); + if (ctx->addr) + ADD_PARAM_FMT("addr", "0x%x", ctx->addr); + if (ctx->j_params) + cJSON_AddItemToObject(ctx->j_sensor, "params", ctx->j_params); + + /* The VI/MIPI state in /proc/umap describes the pipeline, not a bus, + * so it is reported once, with the first sensor. */ + if (vi_info) + hisi_vi_information(ctx); + } +} + +/* Every sensor on the board, not just the first one, where the HAL says the + * board family has more than one (hal_sensor_buses: the Hi3519DV500 reference + * design's SENSOR0..3 sit on I2C4, I2C5, I2C3 and I2C6). getsensorid() finds + * the first, as it always has; then only the listed buses are probed. + * + * Opt-in, and only the listed buses, because a probe is not a read: the + * two-byte register address is a write to a one-byte-address part, and some + * families write page registers. Sweeping every adapter after every hit would + * do that to whatever codec, PMIC or EEPROM shares a sensor address on any + * camera. */ cJSON *detect_sensors() { sensor_ctx_t ctx; memset(&ctx, 0, sizeof(ctx)); - - cJSON *j_sensors = cJSON_CreateArray(); ctx.j_sensor = cJSON_CreateObject(); - cJSON *j_inner = ctx.j_sensor; - cJSON_AddItemToArray(j_sensors, j_inner); if (!getsensorid(&ctx)) { - cJSON_Delete(j_sensors); + cJSON_Delete(ctx.j_sensor); return NULL; } - ADD_PARAM("vendor", ctx.vendor); - ADD_PARAM("model", ctx.sensor_id); - { - cJSON *j_inner = cJSON_CreateObject(); - cJSON_AddItemToObject(ctx.j_sensor, "control", j_inner); - ADD_PARAM_NUM("bus", i2c_adapter_nr); - ADD_PARAM("type", ctx.control); - if (ctx.addr) - ADD_PARAM_FMT("addr", "0x%x", ctx.addr); - if (ctx.j_params) - cJSON_AddItemToObject(ctx.j_sensor, "params", ctx.j_params); - - hisi_vi_information(&ctx); + cJSON *j_sensors = cJSON_CreateArray(); + const int first_bus = i2c_adapter_nr; + add_sensor_entry(j_sensors, &ctx, first_bus, true); + + /* An SPI sensor leaves i2c_adapter_nr wherever the HAL put it, so there + * is no i2c bus to sweep around. */ + if (!hal_sensor_buses || strcmp(ctx.control, "i2c")) + return j_sensors; + + for (const int *b = hal_sensor_buses; *b >= 0; b++) { + const int bus = *b; + if (bus == first_bus) + continue; + /* Found by the fallback sweep: the HAL's own bus and every bus below + * this one were probed on the way and had nothing. */ + if (found_by_sweep && + (bus < first_bus || bus == probed_preferred_adapter)) + continue; + + /* Before the open: the HAL openers take no argument and open the + * adapter this global names. */ + i2c_adapter_nr = bus; + if (!i2c_bus_openable(bus)) + continue; + + sensor_ctx_t more; + memset(&more, 0, sizeof(more)); + more.data_width = 1; + more.reg_width = 2; + more.j_sensor = cJSON_CreateObject(); + + arm_sensor_clock(); + if (get_sensor_id_i2c(&more)) { + strcpy(more.control, "i2c"); + add_sensor_entry(j_sensors, &more, bus, false); + } else { + /* get_sensor_id_i2c() returns without a cleanup when its own + * open fails, and what the arm switched must go back. A second + * cleanup after one that ran is a no-op: each undo is spent. */ + if (hal_cleanup) + hal_cleanup(); + cJSON_Delete(more.j_sensor); + if (more.j_params) + cJSON_Delete(more.j_params); + } } + /* Process-global, read by `ipctool i2cget` and friends: leave it on the + * bus the first sensor was found on, as before. */ + i2c_adapter_nr = first_bus; + restore_printk(); return j_sensors; }