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 
10  /* i2c0 setting */
11  {
12  .i2c_regs = (void *)(I2C_BASE + 0x2000),
13  .i2c_dma_regs = (void *)(I2C_DMA_BASE),
14  },
15 
16  /* i2c1 setting */
17  {
18  .i2c_regs = (void *)(I2C_BASE + 0xc000),
19  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x400),
20  },
21 
22  /* i2c2 setting */
23  {
24  .i2c_regs = (void *)(I2C_BASE + 0x4000),
25  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x200),
26  },
27 
28  /* i2c3 setting */
29  {
30  .i2c_regs = (void *)(I2C_BASE + 0xa000),
31  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x380),
32  },
33 
34  /* i2c4 setting */
35  {
36  .i2c_regs = (void *)(I2C_BASE + 0x3000),
37  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x80),
38  },
39 
40  /* i2c5 setting */
41  {
42  .i2c_regs = (void *)(I2C_BASE + 0x11000),
43  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x480),
44  },
45 
46  /* i2c6 setting */
47  {
48  .i2c_regs = (void *)(I2C_BASE),
49  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x580),
50  },
51 };
52 
54  "Wrong size of mtk_i2c_bus_controller");
55 
56 struct pad_func {
58  u8 func;
59 };
60 
61 #define PAD_FUNC(name, func) {GPIO(name), PAD_##name##_FUNC_##func}
62 
63 static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2] = {
64  {
65  PAD_FUNC(SDA0, SDA0),
66  PAD_FUNC(SCL0, SCL0),
67  },
68  {
69  PAD_FUNC(SDA1, SDA1),
70  PAD_FUNC(SCL1, SCL1),
71  },
72  {
73  PAD_FUNC(SDA2, SDA2),
74  PAD_FUNC(SCL2, SCL2),
75  },
76  {
77  PAD_FUNC(SDA3, SDA3),
78  PAD_FUNC(SCL3, SCL3),
79  },
80  {
81  PAD_FUNC(SDA4, SDA4),
82  PAD_FUNC(SCL4, SCL4),
83  },
84  {
85  PAD_FUNC(SDA5, SDA5),
86  PAD_FUNC(SCL5, SCL5),
87  },
88  {
89  PAD_FUNC(SDA6, SDA6),
90  PAD_FUNC(SCL6, SCL6),
91  },
92 };
93 
95 {
97 
98  const struct pad_func *ptr = i2c_funcs[bus];
99  for (size_t i = 0; i < 2; i++) {
100  gpio_set_mode(ptr[i].gpio, ptr[i].func);
102  }
103 }
104 
106 {
107  uint8_t step_div;
108  const uint8_t clock_div = 5;
109  const uint8_t sample_div = 1;
110  uint32_t i2c_freq;
111 
113 
114  /* Calculate i2c frequency */
115  step_div = DIV_ROUND_UP(I2C_CLK_HZ,
116  (400 * KHz * sample_div * 2) * clock_div);
117  i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div);
118  assert(sample_div < 8 && step_div < 64 && i2c_freq <= 400 * KHz &&
119  i2c_freq >= 380 * KHz);
120 
121  /* Init i2c bus Timing register */
123  (sample_div - 1) << 8 | (step_div - 1));
125  (sample_div - 1) << 6 | (step_div - 1));
126 
127  /* Init i2c bus clock_div register */
129  clock_div - 1);
130 }
131 
133 {
136 }
137 
139 {
140  printk(BIOS_DEBUG, "LTIMING %x\nCLK_DIV %x\n",
141  read32(&regs->ltiming),
142  read32(&regs->clock_div));
143 }
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
#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
static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2]
Definition: i2c.c:63
#define PAD_FUNC(name, func)
Definition: i2c.c:61
static void mtk_i2c_speed_init_soc(uint8_t bus)
Definition: i2c.c:105
static void mtk_i2c_set_gpio_pinmux(uint8_t bus)
Definition: i2c.c:94
#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