Angel Pons | 1ddb894 | 2020-04-04 18:51:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 4 | #include <assert.h> |
Stefan Reinauer | 08dc357 | 2013-05-14 16:57:50 -0700 | [diff] [blame] | 5 | #include <console/console.h> |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 6 | #include <delay.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 7 | #include <device/i2c_simple.h> |
Julius Werner | 1ed0c8c | 2014-10-20 13:16:29 -0700 | [diff] [blame] | 8 | #include <soc/clk.h> |
| 9 | #include <soc/i2c.h> |
| 10 | #include <soc/periph.h> |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 11 | #include <timer.h> |
| 12 | |
| 13 | #define I2C_TIMEOUT_US (1000 * USECS_PER_MSEC) |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 14 | |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 15 | struct __packed i2c_regs |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 16 | { |
| 17 | uint8_t con; |
| 18 | uint8_t _1[3]; |
| 19 | uint8_t stat; |
| 20 | uint8_t _2[3]; |
| 21 | uint8_t add; |
| 22 | uint8_t _3[3]; |
| 23 | uint8_t ds; |
| 24 | uint8_t _4[3]; |
| 25 | uint8_t lc; |
| 26 | uint8_t _5[3]; |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 29 | struct s3c24x0_i2c_bus { |
| 30 | int bus_num; |
| 31 | struct i2c_regs *regs; |
| 32 | enum periph_id periph_id; |
| 33 | }; |
| 34 | |
| 35 | enum { |
| 36 | I2cConIntPending = 0x1 << 4, |
| 37 | I2cConIntEn = 0x1 << 5, |
| 38 | I2cConAckGen = 0x1 << 7 |
| 39 | }; |
| 40 | |
| 41 | enum { |
| 42 | I2cStatAck = 0x1 << 0, |
| 43 | I2cStatAddrZero = 0x1 << 1, |
| 44 | I2cStatAddrSlave = 0x1 << 2, |
| 45 | I2cStatArb = 0x1 << 3, |
| 46 | I2cStatEnable = 0x1 << 4, |
| 47 | I2cStatStartStop = 0x1 << 5, |
| 48 | I2cStatBusy = 0x1 << 5, |
| 49 | |
| 50 | I2cStatModeMask = 0x3 << 6, |
| 51 | I2cStatSlaveRecv = 0x0 << 6, |
| 52 | I2cStatSlaveXmit = 0x1 << 6, |
| 53 | I2cStatMasterRecv = 0x2 << 6, |
| 54 | I2cStatMasterXmit = 0x3 << 6 |
| 55 | }; |
| 56 | |
| 57 | static struct s3c24x0_i2c_bus i2c_busses[] = { |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 58 | { |
| 59 | .bus_num = 0, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 60 | .regs = (void *)0x12c60000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 61 | .periph_id = PERIPH_ID_I2C0, |
| 62 | }, |
| 63 | { |
| 64 | .bus_num = 1, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 65 | .regs = (void *)0x12c70000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 66 | .periph_id = PERIPH_ID_I2C1, |
| 67 | }, |
| 68 | { |
| 69 | .bus_num = 2, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 70 | .regs = (void *)0x12c80000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 71 | .periph_id = PERIPH_ID_I2C2, |
| 72 | }, |
| 73 | { |
| 74 | .bus_num = 3, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 75 | .regs = (void *)0x12c90000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 76 | .periph_id = PERIPH_ID_I2C3, |
| 77 | }, |
| 78 | { |
| 79 | .bus_num = 4, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 80 | .regs = (void *)0x12ca0000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 81 | .periph_id = PERIPH_ID_I2C4, |
| 82 | }, |
| 83 | { |
| 84 | .bus_num = 5, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 85 | .regs = (void *)0x12cb0000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 86 | .periph_id = PERIPH_ID_I2C5, |
| 87 | }, |
| 88 | { |
| 89 | .bus_num = 6, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 90 | .regs = (void *)0x12cc0000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 91 | .periph_id = PERIPH_ID_I2C6, |
| 92 | }, |
| 93 | { |
| 94 | .bus_num = 7, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 95 | .regs = (void *)0x12cd0000, |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 96 | .periph_id = PERIPH_ID_I2C7, |
| 97 | }, |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 98 | }; |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 99 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 100 | static int i2c_int_pending(struct i2c_regs *regs) |
| 101 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 102 | return read8(®s->con) & I2cConIntPending; |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void i2c_clear_int(struct i2c_regs *regs) |
| 106 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 107 | write8(®s->con, read8(®s->con) & ~I2cConIntPending); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void i2c_ack_enable(struct i2c_regs *regs) |
| 111 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 112 | write8(®s->con, read8(®s->con) | I2cConAckGen); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void i2c_ack_disable(struct i2c_regs *regs) |
| 116 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 117 | write8(®s->con, read8(®s->con) & ~I2cConAckGen); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int i2c_got_ack(struct i2c_regs *regs) |
| 121 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 122 | return !(read8(®s->stat) & I2cStatAck); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 125 | static int i2c_wait_for_idle(struct i2c_regs *regs, int timeout_us) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 126 | { |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 127 | int timeout = DIV_ROUND_UP(timeout_us, 10); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 128 | while (timeout--) { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 129 | if (!(read8(®s->stat) & I2cStatBusy)) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | udelay(10); |
| 132 | } |
| 133 | printk(BIOS_ERR, "I2C timeout waiting for idle.\n"); |
| 134 | return 1; |
| 135 | } |
| 136 | |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 137 | static int i2c_wait_for_int(struct i2c_regs *regs, int timeout_us) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 138 | { |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 139 | int timeout = DIV_ROUND_UP(timeout_us, 10); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 140 | while (timeout--) { |
| 141 | if (i2c_int_pending(regs)) |
| 142 | return 0; |
| 143 | udelay(10); |
| 144 | } |
| 145 | printk(BIOS_ERR, "I2C timeout waiting for I2C interrupt.\n"); |
| 146 | return 1; |
| 147 | } |
| 148 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 149 | static int i2c_send_stop(struct i2c_regs *regs) |
| 150 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 151 | uint8_t mode = read8(®s->stat) & (I2cStatModeMask); |
| 152 | write8(®s->stat, mode | I2cStatEnable); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 153 | i2c_clear_int(regs); |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 154 | return i2c_wait_for_idle(regs, I2C_TIMEOUT_US); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static int i2c_send_start(struct i2c_regs *regs, int read, int chip) |
| 158 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 159 | write8(®s->ds, chip << 1); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 160 | uint8_t mode = read ? I2cStatMasterRecv : I2cStatMasterXmit; |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 161 | write8(®s->stat, mode | I2cStatStartStop | I2cStatEnable); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 162 | i2c_clear_int(regs); |
| 163 | |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 164 | if (i2c_wait_for_int(regs, I2C_TIMEOUT_US)) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 165 | return 1; |
| 166 | |
| 167 | if (!i2c_got_ack(regs)) { |
| 168 | // Nobody home, but they may just be asleep. |
| 169 | return 1; |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 172 | return 0; |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 175 | static int i2c_xmit_buf(struct i2c_regs *regs, uint8_t *data, int len) |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 176 | { |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 177 | ASSERT(len); |
| 178 | |
| 179 | i2c_ack_enable(regs); |
| 180 | |
| 181 | int i; |
| 182 | for (i = 0; i < len; i++) { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 183 | write8(®s->ds, data[i]); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 184 | |
| 185 | i2c_clear_int(regs); |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 186 | if (i2c_wait_for_int(regs, CONFIG_I2C_TRANSFER_TIMEOUT_US)) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 187 | return 1; |
| 188 | |
| 189 | if (!i2c_got_ack(regs)) { |
| 190 | printk(BIOS_INFO, "I2c nacked.\n"); |
| 191 | return 1; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return 0; |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 198 | static int i2c_recv_buf(struct i2c_regs *regs, uint8_t *data, int len) |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 199 | { |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 200 | ASSERT(len); |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 201 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 202 | i2c_ack_enable(regs); |
| 203 | |
| 204 | int i; |
| 205 | for (i = 0; i < len; i++) { |
| 206 | if (i == len - 1) |
| 207 | i2c_ack_disable(regs); |
| 208 | |
| 209 | i2c_clear_int(regs); |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 210 | if (i2c_wait_for_int(regs, CONFIG_I2C_TRANSFER_TIMEOUT_US)) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 211 | return 1; |
| 212 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 213 | data[i] = read8(®s->ds); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return 0; |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 219 | int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments, |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 220 | int seg_count) |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 221 | { |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 222 | struct s3c24x0_i2c_bus *i2c = &i2c_busses[bus]; |
| 223 | struct i2c_regs *regs = i2c->regs; |
| 224 | int res = 0; |
| 225 | |
Jes Klinke | 19baa9d | 2022-02-22 16:00:09 -0800 | [diff] [blame] | 226 | if (!regs || i2c_wait_for_idle(regs, I2C_TIMEOUT_US)) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 227 | return 1; |
| 228 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 229 | write8(®s->stat, I2cStatMasterXmit | I2cStatEnable); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 230 | |
| 231 | int i; |
| 232 | for (i = 0; i < seg_count; i++) { |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 233 | struct i2c_msg *seg = &segments[i]; |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 234 | |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 235 | res = i2c_send_start(regs, seg->flags & I2C_M_RD, seg->slave); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 236 | if (res) |
| 237 | break; |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 238 | if (seg->flags & I2C_M_RD) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 239 | res = i2c_recv_buf(regs, seg->buf, seg->len); |
| 240 | else |
| 241 | res = i2c_xmit_buf(regs, seg->buf, seg->len); |
| 242 | if (res) |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | return i2c_send_stop(regs) || res; |
| 247 | } |
| 248 | |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 249 | void i2c_init(unsigned int bus, int speed, int slaveadd) |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 250 | { |
| 251 | struct s3c24x0_i2c_bus *i2c = &i2c_busses[bus]; |
| 252 | |
Stefan Reinauer | 1cf46a7 | 2013-02-14 17:06:43 -0800 | [diff] [blame] | 253 | unsigned long freq, pres = 16, div; |
David Hendricks | b959fbb | 2013-04-05 16:11:12 -0700 | [diff] [blame] | 254 | unsigned long val; |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 255 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 256 | freq = clock_get_periph_rate(i2c->periph_id); |
| 257 | // Calculate prescaler and divisor values. |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 258 | if ((freq / pres / (16 + 1)) > speed) |
| 259 | /* set prescaler to 512 */ |
| 260 | pres = 512; |
| 261 | |
| 262 | div = 0; |
| 263 | |
| 264 | while ((freq / pres / (div + 1)) > speed) |
| 265 | div++; |
| 266 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 267 | // Set prescaler, divisor according to freq, also set ACKGEN, IRQ. |
| 268 | val = (div & 0x0f) | 0xa0 | ((pres == 512) ? 0x40 : 0); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 269 | write32(&i2c->regs->con, val); |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 270 | |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 271 | // Init to SLAVE RECEIVE mode and clear I2CADDn. |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 272 | write32(&i2c->regs->stat, 0); |
| 273 | write32(&i2c->regs->add, slaveadd); |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 274 | // program Master Transmit (and implicit STOP). |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 275 | write32(&i2c->regs->stat, I2cStatMasterXmit | I2cStatEnable); |
David Hendricks | 45256b3 | 2012-12-27 14:06:05 -0800 | [diff] [blame] | 276 | } |