| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <device/mmio.h> |
| 6 |
#include <gpio.h> |
| 7 |
#include <soc/i2c.h> |
| 8 |
|
| 9 |
struct mtk_i2c mtk_i2c_bus_controller[] = { |
| 10 |
|
| 11 |
{ |
| 12 |
.i2c_regs = (void *)(I2C_BASE + 0x2000), |
| 13 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE), |
| 14 |
}, |
| 15 |
|
| 16 |
|
| 17 |
{ |
| 18 |
.i2c_regs = (void *)(I2C_BASE + 0xc000), |
| 19 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x400), |
| 20 |
}, |
| 21 |
|
| 22 |
|
| 23 |
{ |
| 24 |
.i2c_regs = (void *)(I2C_BASE + 0x4000), |
| 25 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x200), |
| 26 |
}, |
| 27 |
|
| 28 |
|
| 29 |
{ |
| 30 |
.i2c_regs = (void *)(I2C_BASE + 0xa000), |
| 31 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x380), |
| 32 |
}, |
| 33 |
|
| 34 |
|
| 35 |
{ |
| 36 |
.i2c_regs = (void *)(I2C_BASE + 0x3000), |
| 37 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x80), |
| 38 |
}, |
| 39 |
|
| 40 |
|
| 41 |
{ |
| 42 |
.i2c_regs = (void *)(I2C_BASE + 0x11000), |
| 43 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x480), |
| 44 |
}, |
| 45 |
|
| 46 |
|
| 47 |
{ |
| 48 |
.i2c_regs = (void *)(I2C_BASE), |
| 49 |
.i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x580), |
| 50 |
}, |
| 51 |
}; |
| 52 |
|
| 53 |
_Static_assert(ARRAY_SIZE(mtk_i2c_bus_controller) == I2C_BUS_NUMBER, |
| 54 |
"Wrong size of mtk_i2c_bus_controller"); |
| 55 |
|
| 56 |
static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2] = { |
| 57 |
{ |
| 58 |
PAD_FUNC_UP(SDA0, SDA0), |
| 59 |
PAD_FUNC_UP(SCL0, SCL0), |
| 60 |
}, |
| 61 |
{ |
| 62 |
PAD_FUNC_UP(SDA1, SDA1), |
| 63 |
PAD_FUNC_UP(SCL1, SCL1), |
| 64 |
}, |
| 65 |
{ |
| 66 |
PAD_FUNC_UP(SDA2, SDA2), |
| 67 |
PAD_FUNC_UP(SCL2, SCL2), |
| 68 |
}, |
| 69 |
{ |
| 70 |
PAD_FUNC_UP(SDA3, SDA3), |
| 71 |
PAD_FUNC_UP(SCL3, SCL3), |
| 72 |
}, |
| 73 |
{ |
| 74 |
PAD_FUNC_UP(SDA4, SDA4), |
| 75 |
PAD_FUNC_UP(SCL4, SCL4), |
| 76 |
}, |
| 77 |
{ |
| 78 |
PAD_FUNC_UP(SDA5, SDA5), |
| 79 |
PAD_FUNC_UP(SCL5, SCL5), |
| 80 |
}, |
| 81 |
{ |
| 82 |
PAD_FUNC_UP(SDA6, SDA6), |
| 83 |
PAD_FUNC_UP(SCL6, SCL6), |
| 84 |
}, |
| 85 |
}; |
| 86 |
|
| 87 |
void mtk_i2c_set_gpio_pinmux(uint8_t bus) |
| 88 |
{ |
| 89 |
assert(bus < I2C_BUS_NUMBER); |
| 90 |
|
| 91 |
const struct pad_func *ptr = i2c_funcs[bus]; |
| 92 |
for (size_t i = 0; i < 2; i++) { |
| 93 |
gpio_set_mode(ptr[i].gpio, ptr[i].func); |
| 94 |
gpio_set_pull(ptr[i].gpio, GPIO_PULL_ENABLE, ptr[i].select); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static void mtk_i2c_speed_init_soc(uint8_t bus) |
| 99 |
{ |
| 100 |
uint8_t step_div; |
| 101 |
const uint8_t clock_div = 5; |
| 102 |
const uint8_t sample_div = 1; |
| 103 |
uint32_t i2c_freq; |
| 104 |
|
| 105 |
assert(bus < ARRAY_SIZE(mtk_i2c_bus_controller)); |
| 106 |
|
| 107 |
|
| 108 |
step_div = DIV_ROUND_UP(I2C_CLK_HZ, |
| 109 |
(400 * KHz * sample_div * 2) * clock_div); |
| 110 |
i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div); |
| 111 |
assert(sample_div < 8 && step_div < 64 && i2c_freq <= 400 * KHz && |
| 112 |
i2c_freq >= 380 * KHz); |
| 113 |
|
| 114 |
|
| 115 |
write32(&mtk_i2c_bus_controller[bus].i2c_regs->timing, |
| 116 |
(sample_div - 1) << 8 | (step_div - 1)); |
| 117 |
write32(&mtk_i2c_bus_controller[bus].i2c_regs->ltiming, |
| 118 |
(sample_div - 1) << 6 | (step_div - 1)); |
| 119 |
|
| 120 |
|
| 121 |
write32(&mtk_i2c_bus_controller[bus].i2c_regs->clock_div, |
| 122 |
clock_div - 1); |
| 123 |
} |
| 124 |
|
| 125 |
void mtk_i2c_bus_init(uint8_t bus, uint32_t speed) |
| 126 |
{ |
| 127 |
assert(speed == I2C_SPEED_FAST); |
| 128 |
mtk_i2c_speed_init_soc(bus); |
| 129 |
mtk_i2c_set_gpio_pinmux(bus); |
| 130 |
} |
| 131 |
|
| 132 |
void mtk_i2c_dump_more_info(struct mt_i2c_regs *regs) |
| 133 |
{ |
| 134 |
printk(BIOS_DEBUG, "LTIMING %x\nCLK_DIV %x\n", |
| 135 |
read32(®s->ltiming), |
| 136 |
read32(®s->clock_div)); |
| 137 |
} |