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/i2c_simple.h>
6 #include <device/mmio.h>
7 #include <soc/addressmap.h>
8 #include <soc/i2c.h>
9 
10 struct mtk_i2c mtk_i2c_bus_controller[7] = {
11  /* i2c0 setting */
12  {
13  .i2c_regs = (void *)I2C_BASE,
14  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x80),
15  },
16 
17  /* i2c1 setting */
18  {
19  .i2c_regs = (void *)(I2C_BASE + 0x1000),
20  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x100),
21  },
22 
23  /* i2c2 setting */
24  {
25  .i2c_regs = (void *)(I2C_BASE + 0x2000),
26  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x180),
27  },
28 
29  /* i2c3 setting */
30  {
31  .i2c_regs = (void *)(I2C_BASE + 0x9000),
32  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x200),
33  },
34 
35  /* i2c4 setting */
36  {
37  .i2c_regs = (void *)(I2C_BASE + 0xa000),
38  .i2c_dma_regs = (void *)(I2C_DMA_BASE + 0x280),
39  },
40 
41  /* i2c5 is reserved for internal use. */
42  {
43  },
44 
45  /* i2c6 setting */
46  {
47  .i2c_regs = (void *)(I2C_BASE + 0xc000),
48  .i2c_dma_regs = (void *)I2C_DMA_BASE,
49  }
50 };
51 
53  "Wrong size of mtk_i2c_bus_controller");
54 
55 #define I2CTAG "[I2C][PL] "
56 
57 #if CONFIG(DEBUG_I2C)
58 #define I2CLOG(fmt, arg...) printk(BIOS_INFO, I2CTAG fmt, ##arg)
59 #else
60 #define I2CLOG(fmt, arg...)
61 #endif /* CONFIG_DEBUG_I2C */
62 
63 #define I2CERR(fmt, arg...) printk(BIOS_ERR, I2CTAG fmt, ##arg)
64 
66 {
67  uint8_t step_div;
68  uint32_t i2c_freq;
69  const uint8_t sample_div = 1;
70 
72 
73  /* Calculate i2c frequency */
74  step_div = DIV_ROUND_UP(I2C_CLK_HZ, (400 * KHz * sample_div * 2));
75  i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2);
76  assert(sample_div < 8 && step_div < 64 && i2c_freq < 400 * KHz &&
77  i2c_freq >= 380 * KHz);
78 
79  /* Init i2c bus Timing register */
81  (sample_div - 1) << 8 | (step_div - 1));
82 }
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
#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
_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
unsigned int uint32_t
Definition: stdint.h:14
unsigned char uint8_t
Definition: stdint.h:8
Definition: device.h:76
Definition: i2c.c:16
struct mt_i2c_dma_regs * i2c_dma_regs
Definition: i2c_common.h:114
struct mt_i2c_regs * i2c_regs
Definition: i2c_common.h:113