1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
4 #include <console/console.h>
5 #include <device/mmio.h>
6 #include <device/i2c_simple.h>
10 struct mtk_i2c mtk_i2c_bus_controller
[] = {
12 .i2c_regs
= (void *)(I2C_BASE
),
13 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
),
14 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
17 .i2c_regs
= (void *)(I2C_BASE
+ 0x1000),
18 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
+ 0x180),
19 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
22 .i2c_regs
= (void *)(I2C_BASE
+ 0x2000),
23 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
+ 0x300),
24 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
27 .i2c_regs
= (void *)(I2C_BASE
+ 0x3000),
28 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
+ 0x400),
29 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
32 .i2c_regs
= (void *)(I2C_BASE
+ 0x4000),
33 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
+ 0x480),
34 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
37 .i2c_regs
= (void *)(I2C_BASE
- 0x100000),
38 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
+ 0x500),
39 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
42 .i2c_regs
= (void *)(I2C_BASE
- 0xFF000),
43 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
+ 0x580),
44 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
47 .i2c_regs
= (void *)(I2C_BASE
- 0xFE000),
48 .i2c_dma_regs
= (void *)(I2C_DMA_BASE
+ 0x600),
49 .mt_i2c_flag
= I2C_APDMA_ASYNC
,
53 _Static_assert(ARRAY_SIZE(mtk_i2c_bus_controller
) == I2C_BUS_NUMBER
,
54 "Wrong size of mtk_i2c_bus_controller");
56 static const struct pad_func i2c_funcs
[I2C_BUS_NUMBER
][2] = {
58 PAD_FUNC_UP(SDA0
, SDA0
),
59 PAD_FUNC_UP(SCL0
, SCL0
),
62 PAD_FUNC_UP(SDA1
, SDA1
),
63 PAD_FUNC_UP(SCL1
, SCL1
),
66 PAD_FUNC_UP(SDA2
, SDA2
),
67 PAD_FUNC_UP(SCL2
, SCL2
),
70 PAD_FUNC_UP(SDA3
, SDA3
),
71 PAD_FUNC_UP(SCL3
, SCL3
),
74 PAD_FUNC_UP(SDA4
, SDA4
),
75 PAD_FUNC_UP(SCL4
, SCL4
),
78 PAD_FUNC_DOWN(HDMIRX_SCL
, SCL5
),
79 PAD_FUNC_DOWN(HDMIRX_SDA
, SDA5
),
82 PAD_FUNC_DOWN(HDMITX_SCL
, SCL6
),
83 PAD_FUNC_DOWN(HDMITX_SDA
, SDA6
),
86 PAD_FUNC_DOWN(HDMIRX_HTPLG
, SCL7
),
87 PAD_FUNC_DOWN(HDMIRX_PWR5V
, SDA7
),
92 static void mtk_i2c_set_gpio_pinmux(uint8_t bus
)
94 assert(bus
< I2C_BUS_NUMBER
);
96 const struct pad_func
*ptr
= i2c_funcs
[bus
];
97 for (size_t i
= 0; i
< 2; i
++) {
98 gpio_set_mode(ptr
[i
].gpio
, ptr
[i
].func
);
100 gpio_set_pull(ptr
[i
].gpio
, GPIO_PULL_ENABLE
, ptr
[i
].select
);
104 void mtk_i2c_bus_init(uint8_t bus
, uint32_t speed
)
106 mtk_i2c_speed_init(bus
, speed
);
107 mtk_i2c_set_gpio_pinmux(bus
);
110 void mtk_i2c_dump_more_info(struct mt_i2c_regs
*regs
)
112 printk(BIOS_DEBUG
, "LTIMING %x\nCLK_DIV %x\n",
113 read32(®s
->ltiming
),
114 read32(®s
->clock_div
));
117 void mtk_i2c_config_timing(struct mt_i2c_regs
*regs
, struct mtk_i2c
*bus_ctrl
)
119 write32(®s
->clock_div
, bus_ctrl
->ac_timing
.inter_clk_div
);
120 write32(®s
->timing
, bus_ctrl
->ac_timing
.htiming
);
121 write32(®s
->ltiming
, bus_ctrl
->ac_timing
.ltiming
);
122 write32(®s
->hs
, bus_ctrl
->ac_timing
.hs
);
123 write32(®s
->ext_conf
, bus_ctrl
->ac_timing
.ext
);