• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 
mtk_i2c_set_gpio_pinmux(uint8_t bus)119 static void mtk_i2c_set_gpio_pinmux(uint8_t bus)
120 {
121 	assert(bus < I2C_BUS_NUMBER);
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);
126 		gpio_set_pull(ptr[i].gpio, GPIO_PULL_ENABLE, GPIO_PULL_UP);
127 	}
128 }
129 
mtk_i2c_speed_init_soc(uint8_t bus)130 static void mtk_i2c_speed_init_soc(uint8_t bus)
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 
140 	assert(bus < I2C_BUS_NUMBER);
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 */
154 	write32(&mtk_i2c_bus_controller[bus].i2c_regs->timing,
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 /
159 			(I2C_FULL_DUTY - I2C_ADJUSTED_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);
168 	write32(&mtk_i2c_bus_controller[bus].i2c_regs->ltiming,
169 		(sample_div - 1) << 6 | (step_div - 1));
170 
171 	/* Init i2c bus clock_div register */
172 	write32(&mtk_i2c_bus_controller[bus].i2c_regs->clock_div,
173 		clock_div - 1);
174 
175 	/* Adjust tSU,STA/tHD,STA/tSU,STO */
176 	write32(&mtk_i2c_bus_controller[bus].i2c_regs->ext_conf, I2C_FS_START_CON);
177 }
178 
mtk_i2c_bus_init(uint8_t bus)179 void mtk_i2c_bus_init(uint8_t bus)
180 {
181 	mtk_i2c_speed_init_soc(bus);
182 	mtk_i2c_set_gpio_pinmux(bus);
183 }
184 
mtk_i2c_dump_more_info(struct mt_i2c_regs * regs)185 void mtk_i2c_dump_more_info(struct mt_i2c_regs *regs)
186 {
187 	printk(BIOS_DEBUG, "LTIMING %x\nCLK_DIV %x\n",
188 	       read32(&regs->ltiming),
189 	       read32(&regs->clock_div));
190 }
191