| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <device/i2c_simple.h> |
| 6 |
#include <device/mmio.h> |
| 7 |
#include <soc/addressmap.h> |
| 8 |
#include <soc/i2c.h> |
| 9 |
|
| 10 |
struct mtk_i2c mtk_i2c_bus_controller[7] = { |
| 11 |
|
| 12 |
{ |
| 13 |
.i2c_regs = (void *)I2C_BASE, |
| 14 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x80), |
| 15 |
}, |
| 16 |
|
| 17 |
|
| 18 |
{ |
| 19 |
.i2c_regs = (void *)(I2C_BASE + 0x1000), |
| 20 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x100), |
| 21 |
}, |
| 22 |
|
| 23 |
|
| 24 |
{ |
| 25 |
.i2c_regs = (void *)(I2C_BASE + 0x2000), |
| 26 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x180), |
| 27 |
}, |
| 28 |
|
| 29 |
|
| 30 |
{ |
| 31 |
.i2c_regs = (void *)(I2C_BASE + 0x9000), |
| 32 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x200), |
| 33 |
}, |
| 34 |
|
| 35 |
|
| 36 |
{ |
| 37 |
.i2c_regs = (void *)(I2C_BASE + 0xa000), |
| 38 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x280), |
| 39 |
}, |
| 40 |
|
| 41 |
|
| 42 |
{ |
| 43 |
}, |
| 44 |
|
| 45 |
|
| 46 |
{ |
| 47 |
.i2c_regs = (void *)(I2C_BASE + 0xc000), |
| 48 |
.i2c_dma_regs = (void *)I2C_DMA_BASE, |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
_Static_assert(ARRAY_SIZE(mtk_i2c_bus_controller) == I2C_BUS_NUMBER, |
| 53 |
"Wrong size of mtk_i2c_bus_controller"); |
| 54 |
|
| 55 |
#define I2CTAG "[I2C][PL] " |
| 56 |
|
| 57 |
#if CONFIG(DEBUG_I2C) |
| 58 |
#define I2CLOG(fmt, arg...) printk(BIOS_INFO, I2CTAG fmt, ##arg) |
| 59 |
#else |
| 60 |
#define I2CLOG(fmt, arg...) |
| 61 |
#endif |
| 62 |
|
| 63 |
void mtk_i2c_bus_init(uint8_t bus, uint32_t speed) |
| 64 |
{ |
| 65 |
uint8_t step_div; |
| 66 |
uint32_t i2c_freq; |
| 67 |
const uint8_t sample_div = 1; |
| 68 |
|
| 69 |
assert(bus < ARRAY_SIZE(mtk_i2c_bus_controller)); |
| 70 |
assert(speed == I2C_SPEED_FAST); |
| 71 |
|
| 72 |
|
| 73 |
step_div = DIV_ROUND_UP(I2C_CLK_HZ, (400 * KHz * sample_div * 2)); |
| 74 |
i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2); |
| 75 |
assert(sample_div < 8 && step_div < 64 && i2c_freq < 400 * KHz && |
| 76 |
i2c_freq >= 380 * KHz); |
| 77 |
|
| 78 |
|
| 79 |
write32(&mtk_i2c_bus_controller[bus].i2c_regs->timing, |
| 80 |
(sample_div - 1) << 8 | (step_div - 1)); |
| 81 |
} |