Debian Codegraph @

src/soc/mediatek/mt8192/i2c.c

raw symbols SCIP
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 <gpio.h>
7 #include <soc/i2c.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
13 struct mtk_i2c mtk_i2c_bus_controller[] = {
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
66 _Static_assert(ARRAY_SIZE(mtk_i2c_bus_controller) == I2C_BUS_NUMBER,
67 "Wrong size of mtk_i2c_bus_controller");
68
69 static const struct pad_func i2c_funcs[I2C_BUS_NUMBER][2] = {
70 [0] = {
71 PAD_FUNC_UP(SDA0, SDA0),
72 PAD_FUNC_UP(SCL0, SCL0),
73 },
74 [1] = {
75 PAD_FUNC_UP(SDA1, SDA1),
76 PAD_FUNC_UP(SCL1, SCL1),
77 },
78 [2] = {
79 PAD_FUNC_UP(SDA2, SDA2),
80 PAD_FUNC_UP(SCL2, SCL2),
81 },
82 [3] = {
83 PAD_FUNC_UP(SDA3, SDA3),
84 PAD_FUNC_UP(SCL3, SCL3),
85 },
86 [4] = {
87 PAD_FUNC_UP(SDA4, SDA4),
88 PAD_FUNC_UP(SCL4, SCL4),
89 },
90 [5] = {
91 PAD_FUNC_UP(SDA5, SDA5),
92 PAD_FUNC_UP(SCL5, SCL5),
93 },
94 [6] = {
95 PAD_FUNC_UP(SDA6, SDA6),
96 PAD_FUNC_UP(SCL6, SCL6),
97 },
98 [7] = {
99 PAD_FUNC_UP(SDA7, SDA7),
100 PAD_FUNC_UP(SCL7, SCL7),
101 },
102 [8] = {
103 PAD_FUNC_UP(SDA8, SDA8),
104 PAD_FUNC_UP(SCL8, SCL8),
105 },
106 [9] = {
107 PAD_FUNC_UP(SDA9, SDA9),
108 PAD_FUNC_UP(SCL9, SCL9),
109 },
110 };
111
112 void mtk_i2c_set_gpio_pinmux(uint8_t bus)
113 {
114 assert(bus < I2C_BUS_NUMBER);
115
116 const struct pad_func *ptr = i2c_funcs[bus];
117 for (size_t i = 0; i < 2; i++) {
118 gpio_set_mode(ptr[i].gpio, ptr[i].func);
119 gpio_set_pull(ptr[i].gpio, GPIO_PULL_ENABLE, ptr[i].select);
120 }
121 }
122
123 static void mtk_i2c_speed_init_soc(uint8_t bus)
124 {
125 uint8_t step_div;
126 const uint8_t clock_div = 5;
127 const uint8_t sample_div = 1;
128 uint32_t i2c_freq;
129 uint32_t tar_speed = 400;
130 uint32_t tar_speed_high;
131 uint32_t tar_speed_low;
132
133 assert(bus < I2C_BUS_NUMBER);
134
135 /* Adjust ratio of high/low level */
136 tar_speed_high = tar_speed * I2C_HALF_DUTY / I2C_ADJUSTED_DUTY;
137
138 /* Calculate i2c frequency */
139 step_div = DIV_ROUND_UP(I2C_CLK_HZ,
140 (tar_speed_high * KHz * sample_div * 2) * clock_div);
141 i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div);
142 assert(sample_div < 8 && step_div < 64 &&
143 i2c_freq <= tar_speed_high * KHz &&
144 i2c_freq >= (tar_speed_high - 20) * KHz);
145
146 /* Init i2c bus timing register */
147 write32(&mtk_i2c_bus_controller[bus].i2c_regs->timing,
148 (sample_div - 1) << 8 | (step_div - 1));
149
150 /* Adjust ratio of high/low level */
151 tar_speed_low = tar_speed * I2C_HALF_DUTY /
152 (I2C_FULL_DUTY - I2C_ADJUSTED_DUTY);
153
154 /* Calculate i2c frequency */
155 step_div = DIV_ROUND_UP(I2C_CLK_HZ,
156 (tar_speed_low * KHz * sample_div * 2) * clock_div);
157 i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2 * clock_div);
158 assert(sample_div < 8 && step_div < 64 &&
159 i2c_freq <= tar_speed_low * KHz &&
160 i2c_freq >= (tar_speed_low - 20) * KHz);
161 write32(&mtk_i2c_bus_controller[bus].i2c_regs->ltiming,
162 (sample_div - 1) << 6 | (step_div - 1));
163
164 /* Init i2c bus clock_div register */
165 write32(&mtk_i2c_bus_controller[bus].i2c_regs->clock_div,
166 clock_div - 1);
167
168 /* Adjust tSU,STA/tHD,STA/tSU,STO */
169 write32(&mtk_i2c_bus_controller[bus].i2c_regs->ext_conf, I2C_FS_START_CON);
170 }
171
172 void mtk_i2c_bus_init(uint8_t bus, uint32_t speed)
173 {
174 assert(speed == I2C_SPEED_FAST);
175 mtk_i2c_speed_init_soc(bus);
176 mtk_i2c_set_gpio_pinmux(bus);
177 }
178
179 void mtk_i2c_dump_more_info(struct mt_i2c_regs *regs)
180 {
181 printk(BIOS_DEBUG, "LTIMING %x\nCLK_DIV %x\n",
182 read32(&regs->ltiming),
183 read32(&regs->clock_div));
184 }