1 /* SPDX-License-Identifier: GPL-2.0-only */
5 #include <console/console.h>
6 #include <device/i2c_simple.h>
9 * The implementation is based on Wikipedia.
12 #define DEBUG 0 /* Set to 1 for per-byte output */
13 #define SPEW 0 /* Set to 1 for verbose bitwise/line-state output */
14 #define DELAY_US 5 /* Default setup delay: 4us (+1 for timer inaccuracy) */
15 #define TIMEOUT_US 50000 /* Maximum clock stretching time we want to allow */
17 #define spew(...) do { if (SPEW) printk(BIOS_SPEW, ##__VA_ARGS__); } while (0)
20 #define ERR_TIMEOUT -3
24 struct software_i2c_ops
*software_i2c
[SOFTWARE_I2C_MAX_BUS
];
27 * Waits until either timeout_us have passed or (iff for_scl is set) until SCL
28 * goes high. Will report random line changes during the wait and return SCL.
30 static int __wait(unsigned int bus
, int timeout_us
, int for_scl
)
33 int sda
= software_i2c
[bus
]->get_sda(bus
);
34 int scl
= software_i2c
[bus
]->get_scl(bus
);
37 stopwatch_init_usecs_expire(&sw
, timeout_us
);
43 us
= stopwatch_duration_usecs(&sw
);
45 if (old_sda
!= (sda
= software_i2c
[bus
]->get_sda(bus
)))
46 spew("[SDA transitioned to %d after %dus] ", sda
, us
);
47 if (old_scl
!= (scl
= software_i2c
[bus
]->get_scl(bus
)))
48 spew("[SCL transitioned to %d after %dus] ", scl
, us
);
49 } while (!stopwatch_expired(&sw
) && (!for_scl
|| !scl
));
54 /* Waits the default DELAY_US to allow line state to stabilize. */
55 static void wait(unsigned int bus
)
57 __wait(bus
, DELAY_US
, 0);
60 /* Waits until SCL goes high. Prints a contextual error message on timeout. */
61 static int wait_for_scl(unsigned int bus
, const char *error_context
)
63 if (!__wait(bus
, TIMEOUT_US
, 1)) {
64 printk(BIOS_ERR
, "software_i2c(%d): ERROR: Clock stretching "
65 "timeout %s!\n", bus
, error_context
);
72 static int start_cond(unsigned int bus
)
74 spew("software_i2c(%d): Sending start condition... ", bus
);
76 /* SDA might not yet be high if repeated start. */
77 software_i2c
[bus
]->set_sda(bus
, 1);
80 /* Might need to wait for clock stretching if repeated start. */
81 software_i2c
[bus
]->set_scl(bus
, 1);
82 if (wait_for_scl(bus
, "before start condition"))
84 wait(bus
); /* Repeated start setup time, minimum 4.7us */
86 if (!software_i2c
[bus
]->get_sda(bus
)) {
87 printk(BIOS_ERR
, "software_i2c(%d): Arbitration lost trying "
88 "to send start condition!\n", bus
);
92 /* SCL is high, transition SDA low as first part of start condition. */
93 software_i2c
[bus
]->set_sda(bus
, 0);
95 assert(software_i2c
[bus
]->get_scl(bus
));
97 /* Pull SCL low to finish start condition (next pulse will be data). */
98 software_i2c
[bus
]->set_scl(bus
, 0);
100 spew("Start condition transmitted!\n");
104 static int stop_cond(unsigned int bus
)
106 spew("software_i2c(%d): Sending stop condition... ", bus
);
108 /* SDA is unknown, set it to low. SCL must be low. */
109 software_i2c
[bus
]->set_sda(bus
, 0);
112 /* Clock stretching */
113 assert(!software_i2c
[bus
]->get_scl(bus
));
114 software_i2c
[bus
]->set_scl(bus
, 1);
115 if (wait_for_scl(bus
, "before stop condition"))
117 wait(bus
); /* Stop bit setup time, minimum 4us */
119 /* SCL is high, transition SDA high to signal stop condition. */
120 software_i2c
[bus
]->set_sda(bus
, 1);
122 if (!software_i2c
[bus
]->get_sda(bus
)) {
123 printk(BIOS_WARNING
, "software_i2c(%d): WARNING: SDA low after "
124 "stop condition... access by another master or line "
125 "stuck from faulty slave?\n", bus
);
126 /* Could theoretically happen with multi-master, so no -1. */
129 spew("Stop condition transmitted\n");
133 static int out_bit(unsigned int bus
, int bit
)
135 spew("software_i2c(%d): Sending a %d bit... ", bus
, bit
);
137 software_i2c
[bus
]->set_sda(bus
, bit
);
140 if (bit
&& !software_i2c
[bus
]->get_sda(bus
)) {
141 printk(BIOS_ERR
, "software_i2c(%d): ERROR: SDA wedged low "
142 "by slave before clock pulse on transmit!\n", bus
);
146 /* Clock stretching */
147 assert(!software_i2c
[bus
]->get_scl(bus
));
148 software_i2c
[bus
]->set_scl(bus
, 1);
149 if (wait_for_scl(bus
, "on transmit"))
153 if (bit
&& !software_i2c
[bus
]->get_sda(bus
)) {
154 printk(BIOS_ERR
, "software_i2c(%d): ERROR: SDA wedged low "
155 "by slave after clock pulse on transmit!\n", bus
);
159 assert(software_i2c
[bus
]->get_scl(bus
));
160 software_i2c
[bus
]->set_scl(bus
, 0);
162 spew("%d bit sent!\n", bit
);
166 static int in_bit(unsigned int bus
)
170 spew("software_i2c(%d): Receiving a bit... ", bus
);
172 /* Let the slave drive data */
173 software_i2c
[bus
]->set_sda(bus
, 1);
176 /* Clock stretching */
177 assert(!software_i2c
[bus
]->get_scl(bus
));
178 software_i2c
[bus
]->set_scl(bus
, 1);
179 if (wait_for_scl(bus
, "on receive"))
182 /* SCL is high, now data is valid */
183 bit
= software_i2c
[bus
]->get_sda(bus
);
185 assert(software_i2c
[bus
]->get_scl(bus
));
186 software_i2c
[bus
]->set_scl(bus
, 0);
188 spew("Received a %d!\n", bit
);
193 /* Write a byte to I2C bus. Return 0 if ack by the slave. */
194 static int out_byte(unsigned int bus
, u8 byte
)
199 for (bit
= 0; bit
< 8; bit
++)
200 if ((ret
= out_bit(bus
, (byte
>> (7 - bit
)) & 0x1)) < 0)
205 if (DEBUG
&& nack
>= 0)
206 printk(BIOS_DEBUG
, "software_i2c(%d): wrote byte 0x%02x, "
207 "received %s\n", bus
, byte
, nack
? "NAK" : "ACK");
209 return nack
> 0 ? ERR_NACK
: nack
;
212 static int in_byte(unsigned int bus
, int ack
)
216 for (i
= 0; i
< 8; ++i
) {
217 int bit
= in_bit(bus
);
220 byte
= (byte
<< 1) | bit
;
223 if ((ret
= out_bit(bus
, !ack
)) < 0)
227 printk(BIOS_DEBUG
, "software_i2c(%d): read byte 0x%02x, "
228 "sent %s\n", bus
, byte
, ack
? "ACK" : "NAK");
233 int software_i2c_transfer(unsigned int bus
, struct i2c_msg
*segments
, int count
)
238 for (seg
= segments
; seg
- segments
< count
; seg
++) {
239 if ((ret
= start_cond(bus
)) < 0)
241 const u8 addr_dir
= seg
->slave
<< 1 | !!(seg
->flags
& I2C_M_RD
);
242 if ((ret
= out_byte(bus
, addr_dir
)) < 0)
244 for (i
= 0; i
< seg
->len
; i
++) {
245 if (seg
->flags
& I2C_M_RD
) {
246 ret
= in_byte(bus
, i
< seg
->len
- 1);
247 seg
->buf
[i
] = (u8
)ret
;
249 ret
= out_byte(bus
, seg
->buf
[i
]);
255 if ((ret
= stop_cond(bus
)) < 0)
261 void software_i2c_wedge_ack(unsigned int bus
, u8 chip
)
265 /* Start a command to 'chip'... */
268 /* Send the address bits but don't yet read the ACK. */
270 for (i
= 0; i
< 8; ++i
)
271 out_bit(bus
, (chip
>> (7 - i
)) & 0x1);
273 /* Let the slave drive it's ACK but keep the clock high forever. */
274 software_i2c
[bus
]->set_sda(bus
, 1);
276 software_i2c
[bus
]->set_scl(bus
, 1);
277 wait_for_scl(bus
, "on wedge_ack()");
279 printk(BIOS_INFO
, "software_i2c(%d): wedged address write on slave "
280 "ACK. SDA %d, SCL %d\n", bus
, software_i2c
[bus
]->get_sda(bus
),
281 software_i2c
[bus
]->get_scl(bus
));
284 void software_i2c_wedge_read(unsigned int bus
, u8 chip
, u8 reg
, int bits
)
288 /* Start a command to 'chip'... */
290 out_byte(bus
, chip
<< 1);
291 /* ...for register 'reg'. */
294 /* Start a read command... */
296 out_byte(bus
, chip
<< 1 | 1);
298 /* Read bit_count bits and stop */
299 for (i
= 0; i
< bits
; ++i
)
302 /* Let the slave drive SDA but keep the clock high forever. */
303 software_i2c
[bus
]->set_sda(bus
, 1);
305 software_i2c
[bus
]->set_scl(bus
, 1);
306 wait_for_scl(bus
, "on wedge_read()");
308 printk(BIOS_INFO
, "software_i2c(%d): wedged data read after %d bits. "
309 "SDA %d, SCL %d\n", bus
, bits
, software_i2c
[bus
]->get_sda(bus
),
310 software_i2c
[bus
]->get_scl(bus
));
313 void software_i2c_wedge_write(unsigned int bus
, u8 chip
, u8 reg
, int bits
)
317 /* Start a command to 'chip'... */
319 out_byte(bus
, chip
<< 1);
321 /* Write bit_count register bits and stop */
322 for (i
= 0; i
< bits
; ++i
)
323 out_bit(bus
, (reg
>> (7 - i
)) & 0x1);
325 /* Pretend to write another 1 bit but keep the clock high forever. */
326 software_i2c
[bus
]->set_sda(bus
, 1);
328 software_i2c
[bus
]->set_scl(bus
, 1);
329 wait_for_scl(bus
, "on wedge_write()");
331 printk(BIOS_INFO
, "software_i2c(%d): wedged data write after %d bits. "
332 "SDA %d, SCL %d\n", bus
, bits
, software_i2c
[bus
]->get_sda(bus
),
333 software_i2c
[bus
]->get_scl(bus
));