How to accessing I2C on dram_init

We would like to access the i2c from the ‘dram_init(void)’ function in ‘arch/arm/mach-mediatek/mt8188/init.c’ file to set the dram size based on value read with i2c rather than from the device tree.

I have tried the following I2C API functions:

int read_stm32_reg(void) {

u8 bus_num = 3;
u8 chip_addr = 0x40;
u8 reg_addr = 0xf0;

struct udevice *dev;
int ret;
char data[1];

ret = i2c_get_chip_for_busnum(bus_num, chip_addr, reg_addr, &dev);
if (ret) {
    printf("Error getting chip: %d\n", ret);
    return;
}

ret = dm_i2c_reg_read(dev, reg_addr);
if (ret) {
    printf("Error reading from I2C device: %d\n", ret);
    return;
}

printf("Read data from register: %02x\n", data[0]);

return 0;

}

however I am getting an error on u-boot log:

CPU: MediaTek MT8390
DRAM: Error getting chip: -19
4 GiB

Can this i2c driver be used for it? Is it even possible to make it work that early in the boot process?

Usually developer uses board detection function to apply dram size in advance.

If you want to use dynamic dram detection via I2C, you might be able to call i2c_init_all() “ref: common/board_f.c”
during board init before DM (driver model) driver bring up.
However, MediaTek didn’t tested such scenario, you could try it.