Angel Pons | bbc99cf | 2020-04-04 18:51:23 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 4 | #include <assert.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <delay.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 7 | #include <device/i2c_simple.h> |
Julius Werner | 7a453eb | 2014-10-20 13:14:55 -0700 | [diff] [blame] | 8 | #include <soc/addressmap.h> |
| 9 | #include <soc/grf.h> |
| 10 | #include <soc/soc.h> |
| 11 | #include <soc/i2c.h> |
| 12 | #include <soc/clock.h> |
Elyes HAOUAS | c379d46 | 2020-07-27 08:16:24 +0200 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
huang lin | bbcffd9 | 2014-09-27 12:02:27 +0800 | [diff] [blame] | 15 | |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 16 | #define RETRY_COUNT 3 |
| 17 | /* 100000us = 100ms */ |
| 18 | #define I2C_TIMEOUT_US 100000 |
Daisuke Nojiri | 51de5a8 | 2014-09-18 12:35:09 -0700 | [diff] [blame] | 19 | #define I2C_BUS_MAX 6 |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 20 | #define I2C_NOACK 2 |
| 21 | #define I2C_TIMEOUT 3 |
| 22 | |
| 23 | #define i2c_info(x...) do {if (0) printk(BIOS_DEBUG, x); } while (0) |
| 24 | |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 25 | struct rk_i2c_regs { |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 26 | u32 i2c_con; |
| 27 | u32 i2c_clkdiv; |
| 28 | u32 i2c_mrxaddr; |
| 29 | u32 i2c_mrxraddr; |
| 30 | u32 i2c_mtxcnt; |
| 31 | u32 i2c_mrxcnt; |
| 32 | u32 i2c_ien; |
| 33 | u32 i2c_ipd; |
| 34 | u32 i2c_fcnt; |
| 35 | u32 reserved0[(0x100 - 0x24) / 4]; |
| 36 | u32 txdata[8]; |
| 37 | u32 reserved1[(0x200 - 0x120) / 4]; |
| 38 | u32 rxdata[8]; |
| 39 | }; |
| 40 | |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 41 | static const uintptr_t i2c_bus[] = IC_BASES; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 42 | |
| 43 | /* Con register bits. */ |
| 44 | #define I2C_ACT2NAK (1<<6) |
| 45 | #define I2C_NAK (1<<5) |
| 46 | #define I2C_STOP (1<<4) |
| 47 | #define I2C_START (1<<3) |
| 48 | #define I2C_MODE_TX (0<<1) |
| 49 | #define I2C_MODE_TRX (1<<1) |
| 50 | #define I2C_MODE_RX (2<<1) |
| 51 | #define I2C_EN (1<<0) |
| 52 | |
| 53 | #define I2C_8BIT (1<<24) |
| 54 | #define I2C_16BIT (3<<24) |
| 55 | #define I2C_24BIT (7<<24) |
| 56 | |
| 57 | /* Mtxcnt register bits. */ |
| 58 | #define I2C_CNT(cnt) ((cnt) & 0x3F) |
| 59 | |
| 60 | #define I2C_NAKRCVI (1<<6) |
| 61 | #define I2C_STOPI (1<<5) |
| 62 | #define I2C_STARTI (1<<4) |
| 63 | #define I2C_MBRFI (1<<3) |
| 64 | #define I2C_MBTFI (1<<2) |
| 65 | #define I2C_BRFI (1<<1) |
| 66 | #define I2C_BTFI (1<<0) |
| 67 | #define I2C_CLEANI 0x7F |
| 68 | |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 69 | static int i2c_send_start(struct rk_i2c_regs *reg_addr) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 70 | { |
| 71 | int res = 0; |
| 72 | int timeout = I2C_TIMEOUT_US; |
| 73 | |
| 74 | i2c_info("I2c Start::Send Start bit\n"); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 75 | write32(®_addr->i2c_ipd, I2C_CLEANI); |
| 76 | write32(®_addr->i2c_con, I2C_EN | I2C_START); |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 77 | while (timeout--) { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 78 | if (read32(®_addr->i2c_ipd) & I2C_STARTI) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 79 | break; |
| 80 | udelay(1); |
| 81 | } |
| 82 | |
| 83 | if (timeout <= 0) { |
| 84 | printk(BIOS_ERR, "I2C Start::Send Start Bit Timeout\n"); |
| 85 | res = I2C_TIMEOUT; |
| 86 | } |
| 87 | |
| 88 | return res; |
| 89 | } |
| 90 | |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 91 | static int i2c_send_stop(struct rk_i2c_regs *reg_addr) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 92 | { |
| 93 | int res = 0; |
| 94 | int timeout = I2C_TIMEOUT_US; |
| 95 | |
| 96 | i2c_info("I2c Stop::Send Stop bit\n"); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 97 | write32(®_addr->i2c_ipd, I2C_CLEANI); |
| 98 | write32(®_addr->i2c_con, I2C_EN | I2C_STOP); |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 99 | while (timeout--) { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 100 | if (read32(®_addr->i2c_ipd) & I2C_STOPI) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 101 | break; |
| 102 | udelay(1); |
| 103 | } |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 104 | write32(®_addr->i2c_con, 0); |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 105 | if (timeout <= 0) { |
| 106 | printk(BIOS_ERR, "I2C Stop::Send Stop Bit Timeout\n"); |
| 107 | res = I2C_TIMEOUT; |
| 108 | } |
| 109 | |
| 110 | return res; |
| 111 | } |
| 112 | |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 113 | static int i2c_read(struct rk_i2c_regs *reg_addr, struct i2c_msg segment) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 114 | { |
| 115 | int res = 0; |
| 116 | uint8_t *data = segment.buf; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 117 | unsigned int bytes_remaining = segment.len; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 118 | unsigned int con = 0; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 119 | |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 120 | write32(®_addr->i2c_mrxaddr, I2C_8BIT | segment.slave << 1 | 1); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 121 | write32(®_addr->i2c_mrxraddr, 0); |
Daisuke Nojiri | 51de5a8 | 2014-09-18 12:35:09 -0700 | [diff] [blame] | 122 | con = I2C_MODE_TRX | I2C_EN | I2C_ACT2NAK; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 123 | while (bytes_remaining) { |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 124 | int timeout = CONFIG_I2C_TRANSFER_TIMEOUT_US; |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 125 | size_t size = MIN(bytes_remaining, 32); |
| 126 | bytes_remaining -= size; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 127 | if (!bytes_remaining) |
| 128 | con |= I2C_EN | I2C_NAK; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 129 | |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 130 | i2c_info("I2C Read::%zu bytes\n", size); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 131 | write32(®_addr->i2c_ipd, I2C_CLEANI); |
| 132 | write32(®_addr->i2c_con, con); |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 133 | write32(®_addr->i2c_mrxcnt, size); |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 134 | |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 135 | while (timeout--) { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 136 | if (read32(®_addr->i2c_ipd) & I2C_NAKRCVI) { |
| 137 | write32(®_addr->i2c_mrxcnt, 0); |
| 138 | write32(®_addr->i2c_con, 0); |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 139 | return I2C_NOACK; |
| 140 | } |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 141 | if (read32(®_addr->i2c_ipd) & I2C_MBRFI) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 142 | break; |
| 143 | udelay(1); |
| 144 | } |
| 145 | if (timeout <= 0) { |
| 146 | printk(BIOS_ERR, "I2C Read::Recv Data Timeout\n"); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 147 | write32(®_addr->i2c_mrxcnt, 0); |
| 148 | write32(®_addr->i2c_con, 0); |
Daisuke Nojiri | 51de5a8 | 2014-09-18 12:35:09 -0700 | [diff] [blame] | 149 | return I2C_TIMEOUT; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 152 | buffer_from_fifo32(data, size, ®_addr->rxdata, 4, 4); |
| 153 | data += size; |
Daisuke Nojiri | 51de5a8 | 2014-09-18 12:35:09 -0700 | [diff] [blame] | 154 | con = I2C_MODE_RX | I2C_EN | I2C_ACT2NAK; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 155 | } |
| 156 | return res; |
| 157 | } |
| 158 | |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 159 | static int i2c_write(struct rk_i2c_regs *reg_addr, struct i2c_msg segment) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 160 | { |
| 161 | int res = 0; |
| 162 | uint8_t *data = segment.buf; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 163 | int bytes_remaining = segment.len + 1; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 164 | |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 165 | /* Prepend one byte for the slave address to the transfer. */ |
| 166 | u32 prefix = segment.slave << 1; |
| 167 | int prefsz = 1; |
| 168 | |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 169 | while (bytes_remaining) { |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 170 | int timeout = CONFIG_I2C_TRANSFER_TIMEOUT_US; |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 171 | size_t size = MIN(bytes_remaining, 32); |
| 172 | buffer_to_fifo32_prefix(data, prefix, prefsz, size, |
| 173 | ®_addr->txdata, 4, 4); |
| 174 | data += size - prefsz; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 175 | |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 176 | i2c_info("I2C Write::%zu bytes\n", size); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 177 | write32(®_addr->i2c_ipd, I2C_CLEANI); |
| 178 | write32(®_addr->i2c_con, |
| 179 | I2C_EN | I2C_MODE_TX | I2C_ACT2NAK); |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 180 | write32(®_addr->i2c_mtxcnt, size); |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 181 | |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 182 | while (timeout--) { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 183 | if (read32(®_addr->i2c_ipd) & I2C_NAKRCVI) { |
| 184 | write32(®_addr->i2c_mtxcnt, 0); |
| 185 | write32(®_addr->i2c_con, 0); |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 186 | return I2C_NOACK; |
| 187 | } |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 188 | if (read32(®_addr->i2c_ipd) & I2C_MBTFI) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 189 | break; |
| 190 | udelay(1); |
| 191 | } |
| 192 | |
| 193 | if (timeout <= 0) { |
| 194 | printk(BIOS_ERR, "I2C Write::Send Data Timeout\n"); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 195 | write32(®_addr->i2c_mtxcnt, 0); |
| 196 | write32(®_addr->i2c_con, 0); |
Daisuke Nojiri | 51de5a8 | 2014-09-18 12:35:09 -0700 | [diff] [blame] | 197 | return I2C_TIMEOUT; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Julius Werner | c788ae3 | 2019-08-09 16:57:20 -0700 | [diff] [blame] | 200 | bytes_remaining -= size; |
| 201 | prefsz = 0; |
| 202 | prefix = 0; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 203 | } |
| 204 | return res; |
| 205 | } |
| 206 | |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 207 | static int i2c_do_xfer(void *reg_addr, struct i2c_msg segment) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 208 | { |
| 209 | int res = 0; |
| 210 | |
| 211 | if (i2c_send_start(reg_addr)) |
| 212 | return I2C_TIMEOUT; |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 213 | if (segment.flags & I2C_M_RD) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 214 | res = i2c_read(reg_addr, segment); |
| 215 | else |
| 216 | res = i2c_write(reg_addr, segment); |
| 217 | return i2c_send_stop(reg_addr) || res; |
| 218 | } |
| 219 | |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 220 | int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments, |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 221 | int seg_count) |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 222 | { |
| 223 | int i; |
| 224 | int res = 0; |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 225 | struct rk_i2c_regs *regs = (struct rk_i2c_regs *)(i2c_bus[bus]); |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 226 | struct i2c_msg *seg = segments; |
huang lin | 441a578 | 2014-07-30 20:34:40 -0700 | [diff] [blame] | 227 | |
| 228 | for (i = 0; i < seg_count; i++, seg++) { |
| 229 | res = i2c_do_xfer(regs, *seg); |
| 230 | if (res) |
| 231 | break; |
| 232 | } |
| 233 | return res; |
| 234 | } |
huang lin | bbcffd9 | 2014-09-27 12:02:27 +0800 | [diff] [blame] | 235 | |
| 236 | void i2c_init(unsigned int bus, unsigned int hz) |
| 237 | { |
| 238 | unsigned int clk_div; |
| 239 | unsigned int divl; |
| 240 | unsigned int divh; |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 241 | unsigned int i2c_src_clk; |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 242 | unsigned int i2c_clk; |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 243 | struct rk_i2c_regs *regs = (struct rk_i2c_regs *)(i2c_bus[bus]); |
huang lin | bbcffd9 | 2014-09-27 12:02:27 +0800 | [diff] [blame] | 244 | |
huang lin | d4c175b | 2016-03-02 18:46:24 +0800 | [diff] [blame] | 245 | i2c_src_clk = rkclk_i2c_clock_for_bus(bus); |
huang lin | bbcffd9 | 2014-09-27 12:02:27 +0800 | [diff] [blame] | 246 | |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 247 | /* SCL Divisor = 8*(CLKDIVL + 1 + CLKDIVH + 1) |
| 248 | SCL = PCLK / SCLK Divisor */ |
Elyes HAOUAS | 6df3b64 | 2018-11-26 22:53:49 +0100 | [diff] [blame] | 249 | clk_div = DIV_ROUND_UP(i2c_src_clk, hz * 8); |
huang lin | 8affee5 | 2014-10-10 23:26:21 -0700 | [diff] [blame] | 250 | divh = clk_div * 3 / 7 - 1; |
| 251 | divl = clk_div - divh - 2; |
Julius Werner | 8e42bd1c | 2016-11-01 15:24:54 -0700 | [diff] [blame] | 252 | i2c_clk = i2c_src_clk / (8 * (divl + 1 + divh + 1)); |
| 253 | printk(BIOS_DEBUG, "I2C bus %u: %uHz (divh = %u, divl = %u)\n", |
| 254 | bus, i2c_clk, divh, divl); |
Julius Werner | 7e1f68c | 2016-12-16 16:03:57 -0800 | [diff] [blame] | 255 | assert((divh < 65536) && (divl < 65536) && hz - i2c_clk < 15*KHz); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 256 | write32(®s->i2c_clkdiv, (divh << 16) | (divl << 0)); |
huang lin | bbcffd9 | 2014-09-27 12:02:27 +0800 | [diff] [blame] | 257 | } |