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 /*
4  * This file is created based on MT8186 Functional Specification
5  * Chapter number: 5.10
6  */
7 
8 #include <assert.h>
9 #include <console/console.h>
10 #include <device/mmio.h>
11 #include <device/i2c_simple.h>
12 #include <soc/i2c.h>
13 #include <soc/gpio.h>
14 
16  [0] = {
17  .i2c_regs = (void *)(I2C0_BASE),
18  .i2c_dma_regs = (void *)(I2C0_DMA_BASE),
19  },
20  [1] = {
21  .i2c_regs = (void *)(I2C1_BASE),
22  .i2c_dma_regs = (void *)(I2C1_DMA_BASE),
23  },
24  [2] = {
25  .i2c_regs = (void *)(I2C2_BASE),
26  .i2c_dma_regs = (void *)(I2C2_DMA_BASE),
27  },
28  [3] = {
29  .i2c_regs = (void *)(I2C3_BASE),
30  .i2c_dma_regs = (void *)(I2C3_DMA_BASE),
31  },
32  [4] = {
33  .i2c_regs = (void *)(I2C4_BASE),
34  .i2c_dma_regs = (void *)(I2C4_DMA_BASE),
35  },
36  [5] = {
37  .i2c_regs = (void *)(I2C5_BASE),
38  .i2c_dma_regs = (void *)(I2C5_DMA_BASE),
39  },
40  [6] = {
41  .i2c_regs = (void *)(I2C6_BASE),
42  .i2c_dma_regs = (void *)(I2C6_DMA_BASE),
43  },
44  [7] = {
45  .i2c_regs = (void *)(I2C7_BASE),
46  .i2c_dma_regs = (void *)(I2C7_DMA_BASE),
47  },
48  [8] = {
49  .i2c_regs = (void *)(I2C8_BASE),
50  .i2c_dma_regs = (void *)(I2C8_DMA_BASE),
51  },
52  [9] = {
53  .i2c_regs = (void *)(I2C9_BASE),
54  .i2c_dma_regs = (void *)(I2C9_DMA_BASE),
55  },
56 };
57 
59  "Wrong size of mtk_i2c_bus_controller");
60 
61 struct pad_func {
62  gpio_t gpio;
63  u8 func;
64 };
65 
66 #define PAD_FUNC(name, func) {GPIO(name), PAD_##name##_FUNC_##func}
67 
68 static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2] = {
69  [0] = {
70  PAD_FUNC(SDA0, SDA0),
71  PAD_FUNC(SCL0, SCL0),
72  },
73  [1] = {
74  PAD_FUNC(SDA1, SDA1),
75  PAD_FUNC(SCL1, SCL1),
76  },
77  [2] = {
78  PAD_FUNC(SDA2, SDA2),
79  PAD_FUNC(SCL2, SCL2),
80  },
81  [3] = {
82  PAD_FUNC(SDA3, SDA3),
83  PAD_FUNC(SCL3, SCL3),
84  },
85  [4] = {
86  PAD_FUNC(SDA4, SDA4),
87  PAD_FUNC(SCL4, SCL4),
88  },
89  [5] = {
90  PAD_FUNC(SDA5, SDA5),
91  PAD_FUNC(SCL5, SCL5),
92  },
93  [6] = {
94  PAD_FUNC(SDA6, SDA6),
95  PAD_FUNC(SCL6, SCL6),
96  },
97  [7] = {
98  PAD_FUNC(SDA7, SDA7),
99  PAD_FUNC(SCL7, SCL7),
100  },
101  [8] = {
102  PAD_FUNC(SDA8, SDA8),
103  PAD_FUNC(SCL8, SCL8),
104  },
105  [9] = {
106  PAD_FUNC(SDA9, SDA9),
107  PAD_FUNC(SCL9, SCL9),
108  },
109 };
110 
112 {
114 
115  const struct pad_func *ptr = i2c_funcs[bus];
116  for (size_t i = 0; i < 2; i++) {
117  gpio_set_mode(ptr[i].gpio, ptr[i].func);
119  }
120 }
121 
123 {
124  mtk_i2c_speed_init(bus, speed);
126 }
127 
129 {
130  printk(BIOS_DEBUG, "LTIMING %x\nCLK_DIV %x\n",
131  read32(&regs->ltiming),
132  read32(&regs->clock_div));
133 }
134 
135 void mtk_i2c_config_timing(struct mt_i2c_regs *regs, struct mtk_i2c *bus_ctrl)
136 {
137  write32(&regs->clock_div, bus_ctrl->ac_timing.inter_clk_div);
138  write32(&regs->timing, bus_ctrl->ac_timing.htiming);
139  write32(&regs->ltiming, bus_ctrl->ac_timing.ltiming);
140  write32(&regs->hs, bus_ctrl->ac_timing.hs);
141  write32(&regs->ext_conf, bus_ctrl->ac_timing.ext);
142 }
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 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
__weak void mtk_i2c_config_timing(struct mt_i2c_regs *regs, struct mtk_i2c *bus_ctrl)
Definition: i2c.c:40
void mtk_i2c_speed_init(uint8_t bus, uint32_t speed)
Definition: i2c.c:494
_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
#define I2C_BUS_NUMBER
Definition: i2c.h:40
static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2]
Definition: i2c.c:68
#define PAD_FUNC(name, func)
Definition: i2c.c:66
static void mtk_i2c_set_gpio_pinmux(uint8_t bus)
Definition: i2c.c:111
@ I2C6_BASE
Definition: addressmap.h:62
@ I2C7_DMA_BASE
Definition: addressmap.h:43
@ I2C3_BASE
Definition: addressmap.h:63
@ I2C3_DMA_BASE
Definition: addressmap.h:39
@ I2C0_DMA_BASE
Definition: addressmap.h:36
@ I2C4_BASE
Definition: addressmap.h:65
@ I2C8_BASE
Definition: addressmap.h:57
@ I2C2_DMA_BASE
Definition: addressmap.h:38
@ I2C5_DMA_BASE
Definition: addressmap.h:41
@ I2C8_DMA_BASE
Definition: addressmap.h:44
@ I2C1_BASE
Definition: addressmap.h:59
@ I2C1_DMA_BASE
Definition: addressmap.h:37
@ I2C9_DMA_BASE
Definition: addressmap.h:45
@ I2C0_BASE
Definition: addressmap.h:58
@ I2C7_BASE
Definition: addressmap.h:56
@ I2C2_BASE
Definition: addressmap.h:60
@ I2C9_BASE
Definition: addressmap.h:71
@ I2C6_DMA_BASE
Definition: addressmap.h:42
@ I2C4_DMA_BASE
Definition: addressmap.h:40
#define GPIO_PULL_UP
Definition: gpio.h:24
#define I2C5_BASE
Definition: addressmap.h:20
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
struct mt_i2c_regs * i2c_regs
Definition: i2c_common.h:113
struct mtk_i2c_ac_timing ac_timing
Definition: i2c_common.h:115
gpio_t gpio
Definition: i2c.c:57
u8 func
Definition: bootblock.c:15