coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
i2c.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <assert.h>
4 #include <console/console.h>
5 #include <device/mmio.h>
6 #include <soc/i2c.h>
7 #include <soc/gpio.h>
8 
9 #define I2C_FULL_DUTY 100
10 #define I2C_HALF_DUTY 50
11 #define I2C_ADJUSTED_DUTY 45
12 #define I2C_FS_START_CON 0x601
14  [0] = {
15  .i2c_regs = (void *)(I2C_BASE + 0x250000),
16  .i2c_dma_regs = (void *)(I2C_DMA_BASE),
17  .mt_i2c_flag = I2C_APDMA_ASYNC,
18  },
19  [1] = {
20  .i2c_regs = (void *)(I2C_BASE + 0x70000),
21  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x80),
22  .mt_i2c_flag = I2C_APDMA_ASYNC,
23  },
24  [2] = {
25  .i2c_regs = (void *)(I2C_BASE + 0x71000),
26  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x100),
27  .mt_i2c_flag = I2C_APDMA_ASYNC,
28  },
29  [3] = {
30  .i2c_regs = (void *)(I2C_BASE),
31  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x280),
32  .mt_i2c_flag = I2C_APDMA_ASYNC,
33  },
34  [4] = {
35  .i2c_regs = (void *)(I2C_BASE + 0x72000),
36  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x300),
37  .mt_i2c_flag = I2C_APDMA_ASYNC,
38  },
39  [5] = {
40  .i2c_regs = (void *)(I2C_BASE + 0x150000),
41  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x480),
42  .mt_i2c_flag = I2C_APDMA_ASYNC,
43  },
44  [6] = {
45  .i2c_regs = (void *)(I2C_BASE + 0x251000),
46  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x500),
47  .mt_i2c_flag = I2C_APDMA_ASYNC,
48  },
49  [7] = {
50  .i2c_regs = (void *)(I2C_BASE + 0x50000),
51  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x580),
52  .mt_i2c_flag = I2C_APDMA_ASYNC,
53  },
54  [8] = {
55  .i2c_regs = (void *)(I2C_BASE + 0x51000),
56  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x700),
57  .mt_i2c_flag = I2C_APDMA_ASYNC,
58  },
59  [9] = {
60  .i2c_regs = (void *)(I2C_BASE + 0x52000),
61  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x880),
62  .mt_i2c_flag = I2C_APDMA_ASYNC,
63  },
64 };
65 
67  "Wrong size of mtk_i2c_bus_controller");
68 
69 struct pad_func {
70  gpio_t gpio;
71  u8 func;
72 };
73 
74 #define PAD_FUNC(name, func) {GPIO(name), PAD_##name##_FUNC_##func}
75 
76 static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2] = {
77  [0] = {
78  PAD_FUNC(SDA0, SDA0),
79  PAD_FUNC(SCL0, SCL0),
80  },
81  [1] = {
82  PAD_FUNC(SDA1, SDA1),
83  PAD_FUNC(SCL1, SCL1),
84  },
85  [2] = {
86  PAD_FUNC(SDA2, SDA2),
87  PAD_FUNC(SCL2, SCL2),
88  },
89  [3] = {
90  PAD_FUNC(SDA3, SDA3),
91  PAD_FUNC(SCL3, SCL3),
92  },
93  [4] = {
94  PAD_FUNC(SDA4, SDA4),
95  PAD_FUNC(SCL4, SCL4),
96  },
97  [5] = {
98  PAD_FUNC(SDA5, SDA5),
99  PAD_FUNC(SCL5, SCL5),
100  },
101  [6] = {
102  PAD_FUNC(SDA6, SDA6),
103  PAD_FUNC(SCL6, SCL6),
104  },
105  [7] = {
106  PAD_FUNC(SDA7, SDA7),
107  PAD_FUNC(SCL7, SCL7),
108  },
109  [8] = {
110  PAD_FUNC(SDA8, SDA8),
111  PAD_FUNC(SCL8, SCL8),
112  },
113  [9] = {
114  PAD_FUNC(SDA9, SDA9),
115  PAD_FUNC(SCL9, SCL9),
116  },
117 };
118 
120 {
122 
123  const struct pad_func *ptr = i2c_funcs[bus];
124  for (size_t i = 0; i < 2; i++) {
125  gpio_set_mode(ptr[i].gpio, ptr[i].func);
127  }
128 }
129 
131 {
132  uint8_t step_div;
133  const uint8_t clock_div = 5;
134  const uint8_t sample_div = 1;
135  uint32_t i2c_freq;
136  uint32_t tar_speed = 400;
137  uint32_t tar_speed_high;
138  uint32_t tar_speed_low;
139 
141 
142  /* Adjust ratio of high/low level */
143  tar_speed_high = tar_speed * I2C_HALF_DUTY / I2C_ADJUSTED_DUTY;
144 
145  /* Calculate i2c frequency */
146  step_div = DIV_ROUND_UP(I2C_CLK_HZ,
147  (tar_speed_high * KHz * sample_div * 2) * clock_div);
148  i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div);
149  assert(sample_div < 8 && step_div < 64 &&
150  i2c_freq <= tar_speed_high * KHz &&
151  i2c_freq >= (tar_speed_high - 20) * KHz);
152 
153  /* Init i2c bus timing register */
155  (sample_div - 1) << 8 | (step_div - 1));
156 
157  /* Adjust ratio of high/low level */
158  tar_speed_low = tar_speed * I2C_HALF_DUTY /
160 
161  /* Calculate i2c frequency */
162  step_div = DIV_ROUND_UP(I2C_CLK_HZ,
163  (tar_speed_low * KHz * sample_div * 2) * clock_div);
164  i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div);
165  assert(sample_div < 8 && step_div < 64 &&
166  i2c_freq <= tar_speed_low * KHz &&
167  i2c_freq >= (tar_speed_low - 20) * KHz);
169  (sample_div - 1) << 6 | (step_div - 1));
170 
171  /* Init i2c bus clock_div register */
173  clock_div - 1);
174 
175  /* Adjust tSU,STA/tHD,STA/tSU,STO */
177 }
178 
180 {
183 }
184 
186 {
187  printk(BIOS_DEBUG, "LTIMING %x\nCLK_DIV %x\n",
188  read32(&regs->ltiming),
189  read32(&regs->clock_div));
190 }
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
#define assert(statement)
Definition: assert.h:74
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define KHz
Definition: helpers.h:79
#define DIV_ROUND_UP(x, y)
Definition: helpers.h:60
#define printk(level,...)
Definition: stdlib.h:16
@ I2C_APDMA_ASYNC
Definition: i2c_common.h:59
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
@ GPIO_PULL_ENABLE
Definition: gpio_common.h:13
void gpio_set_pull(gpio_t gpio, enum pull_enable enable, enum pull_select select)
Definition: gpio.c:17
void gpio_set_mode(gpio_t gpio, int mode)
Definition: gpio.c:45
__weak void mtk_i2c_dump_more_info(struct mt_i2c_regs *regs)
Definition: i2c.c:35
_Static_assert(ARRAY_SIZE(mtk_i2c_bus_controller)==I2C_BUS_NUMBER, "Wrong size of mtk_i2c_bus_controller")
struct mtk_i2c mtk_i2c_bus_controller[7]
Definition: i2c.c:10
void mtk_i2c_bus_init(uint8_t bus)
Definition: i2c.c:65
@ I2C_DMA_BASE
Definition: addressmap.h:39
@ I2C_BASE
Definition: addressmap.h:38
#define I2C_CLK_HZ
Definition: i2c.h:39
#define I2C_BUS_NUMBER
Definition: i2c.h:40
#define I2C_FULL_DUTY
Definition: i2c.c:9
static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2]
Definition: i2c.c:76
#define I2C_ADJUSTED_DUTY
Definition: i2c.c:11
#define I2C_FS_START_CON
Definition: i2c.c:12
#define PAD_FUNC(name, func)
Definition: i2c.c:74
#define I2C_HALF_DUTY
Definition: i2c.c:10
static void mtk_i2c_speed_init_soc(uint8_t bus)
Definition: i2c.c:130
static void mtk_i2c_set_gpio_pinmux(uint8_t bus)
Definition: i2c.c:119
#define GPIO_PULL_UP
Definition: gpio.h:24
unsigned int uint32_t
Definition: stdint.h:14
uint8_t u8
Definition: stdint.h:45
unsigned char uint8_t
Definition: stdint.h:8
Definition: device.h:76
Definition: pinmux.c:36
Definition: i2c.c:16
struct mt_i2c_regs * i2c_regs
Definition: i2c_common.h:113
gpio_t gpio
Definition: i2c.c:57
u8 func
Definition: bootblock.c:15