Angel Pons | e67ab18 | 2020-04-04 18:51:11 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Liguo Zhang | 5a899e9 | 2015-07-31 17:10:51 +0800 | [diff] [blame] | 2 | |
| 3 | #include <assert.h> |
Elyes HAOUAS | 20eaef0 | 2019-03-29 17:45:28 +0100 | [diff] [blame] | 4 | #include <console/console.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 5 | #include <device/i2c_simple.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 6 | #include <device/mmio.h> |
Liguo Zhang | 5a899e9 | 2015-07-31 17:10:51 +0800 | [diff] [blame] | 7 | #include <soc/addressmap.h> |
| 8 | #include <soc/i2c.h> |
Liguo Zhang | 5a899e9 | 2015-07-31 17:10:51 +0800 | [diff] [blame] | 9 | |
Qii Wang | 30e9bc56 | 2019-01-18 09:53:01 +0800 | [diff] [blame] | 10 | struct mtk_i2c mtk_i2c_bus_controller[7] = { |
Liguo Zhang | 5a899e9 | 2015-07-31 17:10:51 +0800 | [diff] [blame] | 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 | |
Rex-BC Chen | 9d32158 | 2021-11-03 11:28:23 +0800 | [diff] [blame] | 52 | _Static_assert(ARRAY_SIZE(mtk_i2c_bus_controller) == I2C_BUS_NUMBER, |
| 53 | "Wrong size of mtk_i2c_bus_controller"); |
| 54 | |
Liguo Zhang | 5a899e9 | 2015-07-31 17:10:51 +0800 | [diff] [blame] | 55 | #define I2CTAG "[I2C][PL] " |
| 56 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 57 | #if CONFIG(DEBUG_I2C) |
Liguo Zhang | 5a899e9 | 2015-07-31 17:10:51 +0800 | [diff] [blame] | 58 | #define I2CLOG(fmt, arg...) printk(BIOS_INFO, I2CTAG fmt, ##arg) |
| 59 | #else |
| 60 | #define I2CLOG(fmt, arg...) |
| 61 | #endif /* CONFIG_DEBUG_I2C */ |
| 62 | |
jun.gao | fa2ed27 | 2015-12-17 16:59:55 +0800 | [diff] [blame] | 63 | void mtk_i2c_bus_init(uint8_t bus) |
| 64 | { |
jun.gao | fa2ed27 | 2015-12-17 16:59:55 +0800 | [diff] [blame] | 65 | uint8_t step_div; |
| 66 | uint32_t i2c_freq; |
Qii Wang | 30e9bc56 | 2019-01-18 09:53:01 +0800 | [diff] [blame] | 67 | const uint8_t sample_div = 1; |
jun.gao | fa2ed27 | 2015-12-17 16:59:55 +0800 | [diff] [blame] | 68 | |
Qii Wang | 30e9bc56 | 2019-01-18 09:53:01 +0800 | [diff] [blame] | 69 | assert(bus < ARRAY_SIZE(mtk_i2c_bus_controller)); |
jun.gao | fa2ed27 | 2015-12-17 16:59:55 +0800 | [diff] [blame] | 70 | |
| 71 | /* Calculate i2c frequency */ |
Elyes HAOUAS | 6df3b64 | 2018-11-26 22:53:49 +0100 | [diff] [blame] | 72 | step_div = DIV_ROUND_UP(I2C_CLK_HZ, (400 * KHz * sample_div * 2)); |
jun.gao | fa2ed27 | 2015-12-17 16:59:55 +0800 | [diff] [blame] | 73 | i2c_freq = I2C_CLK_HZ / (step_div * sample_div * 2); |
| 74 | assert(sample_div < 8 && step_div < 64 && i2c_freq < 400 * KHz && |
| 75 | i2c_freq >= 380 * KHz); |
| 76 | |
| 77 | /* Init i2c bus Timing register */ |
Qii Wang | 30e9bc56 | 2019-01-18 09:53:01 +0800 | [diff] [blame] | 78 | write32(&mtk_i2c_bus_controller[bus].i2c_regs->timing, |
| 79 | (sample_div - 1) << 8 | (step_div - 1)); |
Liguo Zhang | 5a899e9 | 2015-07-31 17:10:51 +0800 | [diff] [blame] | 80 | } |