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
52 changes: 41 additions & 11 deletions src/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/clocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
1 change: 1 addition & 0 deletions src/hal/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
Expand Down
6 changes: 6 additions & 0 deletions src/hal/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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-<i2c_adapter_nr> 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,
Expand Down
61 changes: 54 additions & 7 deletions src/hal/hisi/clocks_v5.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
* bits[9:0]) is surfaced through the standard hpm_info table.
*/

#include <string.h>

#include "chipid.h"
#include "cjson/cJSON.h"
#include "clocks.h"
#include "hal/hisi/hal_hisi.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
117 changes: 109 additions & 8 deletions src/hal/hisi/hal_hisi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading