blob: 9cd20cf465aad802dbbc2e61fd965344c1c9af1a [file] [log] [blame]
Angel Ponse67ab182020-04-04 18:51:11 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Liguo Zhang5a899e92015-07-31 17:10:51 +08002
3#include <assert.h>
Elyes HAOUAS20eaef02019-03-29 17:45:28 +01004#include <console/console.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +02005#include <device/i2c_simple.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02006#include <device/mmio.h>
Liguo Zhang5a899e92015-07-31 17:10:51 +08007#include <soc/addressmap.h>
8#include <soc/i2c.h>
Liguo Zhang5a899e92015-07-31 17:10:51 +08009
Qii Wang30e9bc562019-01-18 09:53:01 +080010struct mtk_i2c mtk_i2c_bus_controller[7] = {
Liguo Zhang5a899e92015-07-31 17:10:51 +080011 /* i2c0 setting */
12 {
13 .i2c_regs = (void *)I2C_BASE,
14 .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x80),
15 },
16
17 /* i2c1 setting */
18 {
19 .i2c_regs = (void *)(I2C_BASE + 0x1000),
20 .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x100),
21 },
22
23 /* i2c2 setting */
24 {
25 .i2c_regs = (void *)(I2C_BASE + 0x2000),
26 .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x180),
27 },
28
29 /* i2c3 setting */
30 {
31 .i2c_regs = (void *)(I2C_BASE + 0x9000),
32 .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x200),
33 },
34
35 /* i2c4 setting */
36 {
37 .i2c_regs = (void *)(I2C_BASE + 0xa000),
38 .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x280),
39 },
40
41 /* i2c5 is reserved for internal use. */
42 {
43 },
44
45 /* i2c6 setting */
46 {
47 .i2c_regs = (void *)(I2C_BASE + 0xc000),
48 .i2c_dma_regs = (void *)I2C_DMA_BASE,
49 }
50};
51
Rex-BC Chen9d321582021-11-03 11:28:23 +080052_Static_assert(ARRAY_SIZE(mtk_i2c_bus_controller) == I2C_BUS_NUMBER,
53 "Wrong size of mtk_i2c_bus_controller");
54
Liguo Zhang5a899e92015-07-31 17:10:51 +080055#define I2CTAG "[I2C][PL] "
56
Julius Wernercd49cce2019-03-05 16:53:33 -080057#if CONFIG(DEBUG_I2C)
Liguo Zhang5a899e92015-07-31 17:10:51 +080058#define I2CLOG(fmt, arg...) printk(BIOS_INFO, I2CTAG fmt, ##arg)
59#else
60#define I2CLOG(fmt, arg...)
61#endif /* CONFIG_DEBUG_I2C */
62
jun.gaofa2ed272015-12-17 16:59:55 +080063void mtk_i2c_bus_init(uint8_t bus)
64{
jun.gaofa2ed272015-12-17 16:59:55 +080065 uint8_t step_div;
66 uint32_t i2c_freq;
Qii Wang30e9bc562019-01-18 09:53:01 +080067 const uint8_t sample_div = 1;
jun.gaofa2ed272015-12-17 16:59:55 +080068
Qii Wang30e9bc562019-01-18 09:53:01 +080069 assert(bus < ARRAY_SIZE(mtk_i2c_bus_controller));
jun.gaofa2ed272015-12-17 16:59:55 +080070
71 /* Calculate i2c frequency */
Elyes HAOUAS6df3b642018-11-26 22:53:49 +010072 step_div = DIV_ROUND_UP(I2C_CLK_HZ, (400 * KHz * sample_div * 2));
jun.gaofa2ed272015-12-17 16:59:55 +080073 i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2);
74 assert(sample_div < 8 && step_div < 64 && i2c_freq < 400 * KHz &&
75 i2c_freq >= 380 * KHz);
76
77 /* Init i2c bus Timing register */
Qii Wang30e9bc562019-01-18 09:53:01 +080078 write32(&mtk_i2c_bus_controller[bus].i2c_regs->timing,
79 (sample_div - 1) << 8 | (step_div - 1));
Liguo Zhang5a899e92015-07-31 17:10:51 +080080}