soc/mediatek: Fix I2C failures by adjusting AC timing and bus speed

1. The original algorithm for I2C speed cannot always make the
   timing meet I2C specification so a new algorithm is introduced
   to calculate the timing parameters more correctly.
2. Some I2C buses should be initialized in a different speed while
   the original implementation was fixed at fast mode (400Khz).
   So the mtk_i2c_bus_init is now also taking an extra speed
   parameter.

There is an equivalent change in kernel side:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/i2c/busses/i2c-mt65xx.c?h=v5.15-rc3&id=be5ce0e97cc7a5c0d2da45d617b7bc567c3d3fa1

BUG=b:189899864
TEST=Test on Tomato, boot pass and timing pass
     at 100/300/400/500/800/1000Khz.

Signed-off-by: Daolong Zhu <jg_daolongzhu@mediatek.corp-partner.google.com>
Change-Id: Id25b7bb3a76908a7943b940eb5bee799e80626a0
Reviewed-on: https://review.coreboot.org/c/coreboot/+/58053
Reviewed-by: Rex-BC Chen <rex-bc.chen@mediatek.corp-partner.google.com>
Reviewed-by: Yu-Ping Wu <yupingso@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Daolong Zhu 2021-09-15 13:01:55 +08:00 committed by Felix Held
parent 38abbdab71
commit f4b71734b2
7 changed files with 336 additions and 69 deletions

View File

@ -43,7 +43,7 @@ static void usb3_hub_reset(void)
void bootblock_mainboard_init(void) void bootblock_mainboard_init(void)
{ {
mtk_i2c_bus_init(CONFIG_DRIVER_TPM_I2C_BUS); mtk_i2c_bus_init(CONFIG_DRIVER_TPM_I2C_BUS, I2C_SPEED_FAST);
mtk_spi_init(CONFIG_EC_GOOGLE_CHROMEEC_SPI_BUS, SPI_PAD0_MASK, 3 * MHz, 0); mtk_spi_init(CONFIG_EC_GOOGLE_CHROMEEC_SPI_BUS, SPI_PAD0_MASK, 3 * MHz, 0);
nor_set_gpio_pinmux(); nor_set_gpio_pinmux();
setup_chromeos_gpios(); setup_chromeos_gpios();

View File

@ -131,7 +131,7 @@ static void configure_sdcard(void)
MSDC1_GPIO_MODE1_2, MSDC1_GPIO_MODE1_VALUE, MSDC1_GPIO_MODE1_2, MSDC1_GPIO_MODE1_VALUE,
MSDC1_GPIO_MODE1_3, MSDC1_GPIO_MODE1_VALUE); MSDC1_GPIO_MODE1_3, MSDC1_GPIO_MODE1_VALUE);
mtk_i2c_bus_init(I2C7); mtk_i2c_bus_init(I2C7, I2C_SPEED_FAST);
if (CONFIG(BOARD_GOOGLE_CHERRY)) if (CONFIG(BOARD_GOOGLE_CHERRY))
mt6360_init(I2C7); mt6360_init(I2C7);
@ -205,7 +205,7 @@ static void mainboard_init(struct device *dev)
/* for audio usage */ /* for audio usage */
if (CONFIG(CHERRY_USE_RT1011)) if (CONFIG(CHERRY_USE_RT1011))
mtk_i2c_bus_init(I2C2); mtk_i2c_bus_init(I2C2, I2C_SPEED_FAST);
if (dpm_init()) if (dpm_init())
printk(BIOS_ERR, "dpm init failed, DVFS may not work\n"); printk(BIOS_ERR, "dpm init failed, DVFS may not work\n");

View File

@ -28,7 +28,7 @@ void platform_romstage_main(void)
mt6359p_init(); mt6359p_init();
mt6315_init(); mt6315_init();
raise_little_cpu_freq(); raise_little_cpu_freq();
mtk_i2c_bus_init(I2C7); mtk_i2c_bus_init(I2C7, I2C_SPEED_FAST);
if (CONFIG(BOARD_GOOGLE_CHERRY)) if (CONFIG(BOARD_GOOGLE_CHERRY))
mt6360_init(I2C7); mt6360_init(I2C7);
clk_buf_init(); clk_buf_init();

View File

@ -10,6 +10,39 @@
#include <soc/i2c.h> #include <soc/i2c.h>
#include <device/i2c_simple.h> #include <device/i2c_simple.h>
const struct i2c_spec_values standard_mode_spec = {
.min_low_ns = 4700 + I2C_STANDARD_MODE_BUFFER,
.min_su_sta_ns = 4700 + I2C_STANDARD_MODE_BUFFER,
.max_hd_dat_ns = 3450 - I2C_STANDARD_MODE_BUFFER,
.min_su_dat_ns = 250 + I2C_STANDARD_MODE_BUFFER,
};
const struct i2c_spec_values fast_mode_spec = {
.min_low_ns = 1300 + I2C_FAST_MODE_BUFFER,
.min_su_sta_ns = 600 + I2C_FAST_MODE_BUFFER,
.max_hd_dat_ns = 900 - I2C_FAST_MODE_BUFFER,
.min_su_dat_ns = 100 + I2C_FAST_MODE_BUFFER,
};
const struct i2c_spec_values fast_mode_plus_spec = {
.min_low_ns = 500 + I2C_FAST_MODE_PLUS_BUFFER,
.min_su_sta_ns = 260 + I2C_FAST_MODE_PLUS_BUFFER,
.max_hd_dat_ns = 400 - I2C_FAST_MODE_PLUS_BUFFER,
.min_su_dat_ns = 50 + I2C_FAST_MODE_PLUS_BUFFER,
};
__weak void mtk_i2c_dump_more_info(struct mt_i2c_regs *regs) { /* do nothing */ }
const struct i2c_spec_values *mtk_i2c_get_spec(uint32_t speed)
{
if (speed <= I2C_SPEED_STANDARD)
return &standard_mode_spec;
else if (speed <= I2C_SPEED_FAST)
return &fast_mode_spec;
else
return &fast_mode_plus_spec;
}
static inline void i2c_hw_reset(uint8_t bus) static inline void i2c_hw_reset(uint8_t bus)
{ {
struct mt_i2c_regs *regs; struct mt_i2c_regs *regs;
@ -42,7 +75,7 @@ static inline void i2c_hw_reset(uint8_t bus)
static inline void mtk_i2c_dump_info(struct mt_i2c_regs *regs) static inline void mtk_i2c_dump_info(struct mt_i2c_regs *regs)
{ {
printk(BIOS_ERR, "I2C register:\nSLAVE_ADDR %x\nINTR_MASK %x\n" printk(BIOS_DEBUG, "I2C register:\nSLAVE_ADDR %x\nINTR_MASK %x\n"
"INTR_STAT %x\nCONTROL %x\nTRANSFER_LEN %x\nTRANSAC_LEN %x\n" "INTR_STAT %x\nCONTROL %x\nTRANSFER_LEN %x\nTRANSAC_LEN %x\n"
"DELAY_LEN %x\nTIMING %x\nSTART %x\nFIFO_STAT %x\nIO_CONFIG %x\n" "DELAY_LEN %x\nTIMING %x\nSTART %x\nFIFO_STAT %x\nIO_CONFIG %x\n"
"HS %x\nDEBUGSTAT %x\nEXT_CONF %x\n", "HS %x\nDEBUGSTAT %x\nEXT_CONF %x\n",
@ -60,6 +93,8 @@ static inline void mtk_i2c_dump_info(struct mt_i2c_regs *regs)
read32(&regs->hs), read32(&regs->hs),
read32(&regs->debug_stat), read32(&regs->debug_stat),
read32(&regs->ext_conf)); read32(&regs->ext_conf));
mtk_i2c_dump_more_info(regs);
} }
static uint32_t mtk_i2c_transfer(uint8_t bus, struct i2c_msg *seg, static uint32_t mtk_i2c_transfer(uint8_t bus, struct i2c_msg *seg,

View File

@ -3,6 +3,8 @@
#ifndef MTK_COMMON_I2C_H #ifndef MTK_COMMON_I2C_H
#define MTK_COMMON_I2C_H #define MTK_COMMON_I2C_H
#include <device/i2c.h>
/* I2C DMA Registers */ /* I2C DMA Registers */
struct mt_i2c_dma_regs { struct mt_i2c_dma_regs {
uint32_t dma_int_flag; uint32_t dma_int_flag;
@ -84,7 +86,6 @@ enum {
}; };
/* I2C Status Code */ /* I2C Status Code */
enum { enum {
I2C_OK = 0x0000, I2C_OK = 0x0000,
I2C_SET_SPEED_FAIL_OVER_SPEED = 0xA001, I2C_SET_SPEED_FAIL_OVER_SPEED = 0xA001,
@ -95,11 +96,51 @@ enum {
I2C_TRANSFER_INVALID_ARGUMENT = 0xA006 I2C_TRANSFER_INVALID_ARGUMENT = 0xA006
}; };
struct mtk_i2c_ac_timing {
u16 htiming;
u16 ltiming;
u16 hs;
u16 ext;
u16 inter_clk_div;
u16 scl_hl_ratio;
u16 hs_scl_hl_ratio;
u16 sta_stop;
u16 hs_sta_stop;
u16 sda_timing;
};
struct mtk_i2c { struct mtk_i2c {
struct mt_i2c_regs *i2c_regs; struct mt_i2c_regs *i2c_regs;
struct mt_i2c_dma_regs *i2c_dma_regs; struct mt_i2c_dma_regs *i2c_dma_regs;
struct mtk_i2c_ac_timing ac_timing;
uint32_t mt_i2c_flag; uint32_t mt_i2c_flag;
}; };
#define I2C_TIME_CLR_VALUE 0x0000
#define MAX_SAMPLE_CNT_DIV 8
#define MAX_STEP_CNT_DIV 64
#define MAX_HS_STEP_CNT_DIV 8
#define I2C_TIME_DEFAULT_VALUE 0x0083
#define I2C_STANDARD_MODE_BUFFER (1000 / 3)
#define I2C_FAST_MODE_BUFFER (300 / 3)
#define I2C_FAST_MODE_PLUS_BUFFER (20 / 3)
/*
* struct i2c_spec_values:
* @min_low_ns: min LOW period of the SCL clock
* @min_su_sta_ns: min set-up time for a repeated START condition
* @max_hd_dat_ns: max data hold time
* @min_su_dat_ns: min data set-up time
*/
struct i2c_spec_values {
uint32_t min_low_ns;
uint32_t min_su_sta_ns;
uint32_t max_hd_dat_ns;
uint32_t min_su_dat_ns;
};
extern struct mtk_i2c mtk_i2c_bus_controller[]; extern struct mtk_i2c mtk_i2c_bus_controller[];
const struct i2c_spec_values *mtk_i2c_get_spec(uint32_t speed);
void mtk_i2c_dump_more_info(struct mt_i2c_regs *regs);
#endif #endif

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include <assert.h> #include <assert.h>
#include <console/console.h>
#include <device/mmio.h> #include <device/mmio.h>
#include <device/i2c_simple.h>
#include <soc/pll.h> #include <soc/pll.h>
#include <soc/i2c.h> #include <soc/i2c.h>
#include <soc/gpio.h> #include <soc/gpio.h>
#include <timer.h>
#define I2C_CLK_HZ (UNIVPLL_HZ / 20) #define I2C_CLK_HZ (UNIVPLL_HZ / 20)
#define I2C_FULL_DUTY 100
#define I2C_HALF_DUTY 50
#define I2C_ADJUSTED_DUTY 50
#define I2C_FS_START_CON 0x0
struct mtk_i2c mtk_i2c_bus_controller[] = { struct mtk_i2c mtk_i2c_bus_controller[] = {
[0] = { [0] = {
@ -112,57 +111,247 @@ static void mtk_i2c_set_gpio_pinmux(uint8_t bus)
} }
} }
static void mtk_i2c_speed_init(uint8_t bus) static int mtk_i2c_max_step_cnt(uint32_t target_speed)
{ {
uint8_t step_div; if (target_speed > I2C_SPEED_FAST_PLUS)
const uint8_t clock_div = 5; return MAX_HS_STEP_CNT_DIV;
const uint8_t sample_div = 1; else
uint32_t i2c_freq; return MAX_STEP_CNT_DIV;
uint32_t tar_speed = 400; }
uint32_t tar_speed_high;
uint32_t tar_speed_low;
assert(bus < I2C_BUS_NUMBER); /*
* Check and calculate i2c ac-timing.
*
* Hardware design:
* sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src
* xxx_cnt_div = spec->min_xxx_ns / sample_ns
*
* The calculation of sample_ns is rounded down;
* otherwise xxx_cnt_div would be greater than the smallest spec.
* The sda_timing is chosen as the middle value between
* the largest and smallest.
*/
static int mtk_i2c_check_ac_timing(uint8_t bus, uint32_t clk_src,
uint32_t check_speed,
uint32_t step_cnt,
uint32_t sample_cnt)
{
const struct i2c_spec_values *spec;
uint32_t su_sta_cnt, low_cnt, high_cnt, max_step_cnt;
uint32_t sda_max, sda_min, clk_ns, max_sta_cnt = 0x100;
uint32_t sample_ns = ((uint64_t)NSECS_PER_SEC * (sample_cnt + 1)) / clk_src;
struct mtk_i2c_ac_timing *ac_timing;
/* Adjust ratio of high/low level */ spec = mtk_i2c_get_spec(check_speed);
tar_speed_high = tar_speed * I2C_HALF_DUTY / I2C_ADJUSTED_DUTY;
/* Calculate i2c frequency */ clk_ns = NSECS_PER_SEC / clk_src;
step_div = DIV_ROUND_UP(I2C_CLK_HZ,
(tar_speed_high * KHz * sample_div * 2) * clock_div); su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns, clk_ns);
i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div); if (su_sta_cnt > max_sta_cnt)
assert(sample_div < 8 && step_div < 64 && return -1;
i2c_freq <= tar_speed_high * KHz &&
i2c_freq >= (tar_speed_high - 20) * KHz); low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns);
max_step_cnt = mtk_i2c_max_step_cnt(check_speed);
if (2 * step_cnt > low_cnt && low_cnt < max_step_cnt) {
if (low_cnt > step_cnt) {
high_cnt = 2 * step_cnt - low_cnt;
} else {
high_cnt = step_cnt;
low_cnt = step_cnt;
}
} else {
return -2;
}
sda_max = spec->max_hd_dat_ns / sample_ns;
if (sda_max > low_cnt)
sda_max = 0;
sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns);
if (sda_min < low_cnt)
sda_min = 0;
if (sda_min > sda_max)
return -3;
ac_timing = &mtk_i2c_bus_controller[bus].ac_timing;
if (check_speed > I2C_SPEED_FAST_PLUS) {
ac_timing->hs = I2C_TIME_DEFAULT_VALUE | (sample_cnt << 12) | (high_cnt << 8);
ac_timing->ltiming &= ~GENMASK(15, 9);
ac_timing->ltiming |= (sample_cnt << 12) | (low_cnt << 9);
ac_timing->ext &= ~GENMASK(7, 1);
ac_timing->ext |= (su_sta_cnt << 1) | (1 << 0);
} else {
ac_timing->htiming = (sample_cnt << 8) | (high_cnt);
ac_timing->ltiming = (sample_cnt << 6) | (low_cnt);
ac_timing->ext = (su_sta_cnt << 8) | (1 << 0);
}
return 0;
}
/*
* Calculate i2c port speed.
*
* Hardware design:
* i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt)
* clock_div: fixed in hardware, but may be various in different SoCs
*
* To calculate sample_cnt and step_cnt, we pick the highest bus frequency
* that is still no larger than i2c->speed_hz.
*/
static int mtk_i2c_calculate_speed(uint8_t bus, uint32_t clk_src,
uint32_t target_speed,
uint32_t *timing_step_cnt,
uint32_t *timing_sample_cnt)
{
uint32_t step_cnt;
uint32_t sample_cnt;
uint32_t max_step_cnt;
uint32_t base_sample_cnt = MAX_SAMPLE_CNT_DIV;
uint32_t base_step_cnt;
uint32_t opt_div;
uint32_t best_mul;
uint32_t cnt_mul;
uint32_t clk_div = mtk_i2c_bus_controller[bus].ac_timing.inter_clk_div;
int32_t clock_div_constraint = 0;
int success = 0;
if (target_speed > I2C_SPEED_HIGH)
target_speed = I2C_SPEED_HIGH;
max_step_cnt = mtk_i2c_max_step_cnt(target_speed);
base_step_cnt = max_step_cnt;
/* Find the best combination */
opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed);
best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt;
/* Search for the best pair (sample_cnt, step_cnt) with
* 0 < sample_cnt < MAX_SAMPLE_CNT_DIV
* 0 < step_cnt < max_step_cnt
* sample_cnt * step_cnt >= opt_div
* optimizing for sample_cnt * step_cnt being minimal
*/
for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) {
if (sample_cnt == 1) {
if (clk_div != 0)
clock_div_constraint = 1;
else
clock_div_constraint = 0;
} else {
if (clk_div > 1)
clock_div_constraint = 1;
else if (clk_div == 0)
clock_div_constraint = -1;
else
clock_div_constraint = 0;
}
step_cnt = DIV_ROUND_UP(opt_div + clock_div_constraint, sample_cnt);
if (step_cnt > max_step_cnt)
continue;
cnt_mul = step_cnt * sample_cnt;
if (cnt_mul >= best_mul)
continue;
if (mtk_i2c_check_ac_timing(bus, clk_src,
target_speed, step_cnt - 1,
sample_cnt - 1))
continue;
success = 1;
best_mul = cnt_mul;
base_sample_cnt = sample_cnt;
base_step_cnt = step_cnt;
if (best_mul == opt_div + clock_div_constraint)
break;
}
if (!success)
return -1;
sample_cnt = base_sample_cnt;
step_cnt = base_step_cnt;
if (clk_src / (2 * (sample_cnt * step_cnt - clock_div_constraint)) >
target_speed)
return -1;
*timing_step_cnt = step_cnt - 1;
*timing_sample_cnt = sample_cnt - 1;
return 0;
}
static void mtk_i2c_speed_init(uint8_t bus, uint32_t speed)
{
uint32_t max_clk_div = MAX_CLOCK_DIV;
uint32_t clk_src, clk_div, step_cnt, sample_cnt;
uint32_t l_step_cnt, l_sample_cnt;
uint32_t timing_reg_value, ltiming_reg_value;
struct mtk_i2c *bus_ctrl;
if (bus >= I2C_BUS_NUMBER) {
printk(BIOS_ERR, "%s, error bus num:%d\n", __func__, bus);
return;
}
bus_ctrl = &mtk_i2c_bus_controller[bus];
for (clk_div = 1; clk_div <= max_clk_div; clk_div++) {
clk_src = I2C_CLK_HZ / clk_div;
bus_ctrl->ac_timing.inter_clk_div = clk_div - 1;
if (speed > I2C_SPEED_FAST_PLUS) {
/* Set master code speed register */
if (mtk_i2c_calculate_speed(bus, clk_src, I2C_SPEED_FAST,
&l_step_cnt, &l_sample_cnt))
continue;
timing_reg_value = (l_sample_cnt << 8) | l_step_cnt;
/* Set the high speed mode register */
if (mtk_i2c_calculate_speed(bus, clk_src, speed,
&step_cnt, &sample_cnt))
continue;
ltiming_reg_value = (l_sample_cnt << 6) | l_step_cnt |
(sample_cnt << 12) | (step_cnt << 9);
bus_ctrl->ac_timing.inter_clk_div = (clk_div - 1) << 8 | (clk_div - 1);
} else {
if (mtk_i2c_calculate_speed(bus, clk_src, speed,
&l_step_cnt, &l_sample_cnt))
continue;
timing_reg_value = (l_sample_cnt << 8) | l_step_cnt;
/* Disable the high speed transaction */
bus_ctrl->ac_timing.hs = I2C_TIME_CLR_VALUE;
ltiming_reg_value = (l_sample_cnt << 6) | l_step_cnt;
}
break;
}
if (clk_div > max_clk_div) {
printk(BIOS_ERR, "%s, cannot support %d hz on i2c-%d\n", __func__, speed, bus);
return;
}
/* Init i2c bus timing register */ /* Init i2c bus timing register */
write32(&mtk_i2c_bus_controller[bus].i2c_regs->timing, write32(&bus_ctrl->i2c_regs->clock_div, bus_ctrl->ac_timing.inter_clk_div);
(sample_div - 1) << 8 | (step_div - 1)); write32(&bus_ctrl->i2c_regs->timing, bus_ctrl->ac_timing.htiming);
write32(&bus_ctrl->i2c_regs->ltiming, bus_ctrl->ac_timing.ltiming);
/* Adjust ratio of high/low level */ write32(&bus_ctrl->i2c_regs->hs, bus_ctrl->ac_timing.hs);
tar_speed_low = tar_speed * I2C_HALF_DUTY / write32(&bus_ctrl->i2c_regs->ext_conf, bus_ctrl->ac_timing.ext);
(I2C_FULL_DUTY - I2C_ADJUSTED_DUTY);
/* Calculate i2c frequency */
step_div = DIV_ROUND_UP(I2C_CLK_HZ,
(tar_speed_low * KHz * sample_div * 2) * clock_div);
i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div);
assert(sample_div < 8 && step_div < 64 &&
i2c_freq <= tar_speed_low * KHz &&
i2c_freq >= (tar_speed_low - 20) * KHz);
write32(&mtk_i2c_bus_controller[bus].i2c_regs->ltiming,
(sample_div - 1) << 6 | (step_div - 1));
/* Init i2c bus clock_div register */
write32(&mtk_i2c_bus_controller[bus].i2c_regs->clock_div,
clock_div - 1);
/* Adjust tSU,STA/tHD,STA/tSU,STO */
write32(&mtk_i2c_bus_controller[bus].i2c_regs->ext_conf, I2C_FS_START_CON);
} }
void mtk_i2c_bus_init(uint8_t bus) void mtk_i2c_bus_init(uint8_t bus, uint32_t speed)
{ {
mtk_i2c_speed_init(bus); mtk_i2c_speed_init(bus, speed);
mtk_i2c_set_gpio_pinmux(bus); mtk_i2c_set_gpio_pinmux(bus);
} }

View File

@ -22,7 +22,8 @@ struct mt_i2c_regs {
uint32_t hs; uint32_t hs;
uint32_t io_config; uint32_t io_config;
uint32_t fifo_addr_clr; uint32_t fifo_addr_clr;
uint32_t reserved0[2]; uint32_t data_timing;
uint32_t reserved0;
uint32_t transfer_aux_len; uint32_t transfer_aux_len;
uint32_t clock_div; uint32_t clock_div;
uint32_t time_out; uint32_t time_out;
@ -51,8 +52,9 @@ enum {
I2C7, I2C7,
}; };
#define MAX_CLOCK_DIV 32
check_member(mt_i2c_regs, multi_dma, 0xf8c); check_member(mt_i2c_regs, multi_dma, 0xf8c);
void mtk_i2c_bus_init(uint8_t bus); void mtk_i2c_bus_init(uint8_t bus, uint32_t speed);
#endif /* SOC_MEDIATEK_MT8195_I2C_H */ #endif /* SOC_MEDIATEK_MT8195_I2C_H */