summaryrefslogtreecommitdiff
path: root/chip/mec1322/i2c.c
diff options
context:
space:
mode:
authorAlec Berg <alecaberg@chromium.org>2014-02-28 15:10:26 -0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-03-06 02:42:49 +0000
commit8a9817a5c7be54b6efc0e116ae607fb9955af247 (patch)
tree6afc3294ab5c27e46ba9211dcde516b4898e9f54 /chip/mec1322/i2c.c
parent362cf0864a46b8ae6a66093cceba6859c801b443 (diff)
downloadchrome-ec-8a9817a5c7be54b6efc0e116ae607fb9955af247.tar.gz
cleanup: Combined i2c unwedge code into one common function
Refactored the i2c unwedge code to place it in the common directory so that any EC chip can use it. Added to the STM32F and LM4 boards, code to automatically detect and unwedge the i2c bus at the start of an i2c transaction. Note that STM32L already had this ability. To enable unwedging of the i2c port though, the gpio pins for SDA and SCL must be defined in the i2c_ports[] array in the board.c file. This allows the i2c module to bit bang the unwedging for the given port. If SDA and SCL are not defined for the port, then the unwedge code will not run. BUG=chrome-os-partner:26315, chrome-os-partner:23802 BRANCH=none TEST=Manual testing on machines with different EC chips. Testing made extensive use of https://chromium-review.googlesource.com/66389 in order to force wedging of the i2c bus so that we can attempt to unwedge it. Note that you can easily test if the bus is wedged by running i2cscan. On pit and spring: On pit, after each of the following, I verified that the bus was automatically unwedged. On spring, the unwedge only runs at reboot, so, for the non-reboot wedge commands, I manually ran console command unwedge, and verified that the bus became unwedged. (1) Bit bang a transaction but only read part of the response. Command to wedge: i2cwedge 0x90 0 2 2 (2) Bit bang a transaction to do a "write" and stop while the other side is acking. Command to wedge: i2cwedge 0x90 0 1 (3) Same as (1) but do a reboot instead of returning and see that the unwedge works at init time w/ no cancelled transactions. Command to wedge: i2cwedge 0x90 0 6 2 (4) Same as (2) but do a reboot instead of returning and see that the unwedge works at init time w/ no cancelled transactions. Command to wedge: i2cwedge 0x90 0 5 On glimmer: Added code to call i2c_unwedge in accel_init(). Then tested unwedging the accelerometer with the following. One extra difficulty testing this with the accelerometer is that sometimes the bit you stop on is high, which means it won't be wedged at all, the next start transaction will reset the bus. So, sometimes running i2cwedge won't wedge the bus and sometimes it will depending on the acceleration data. (1) Big bang transaction to do a "read" of accelerometer and stop partway: i2cwedge 0x1c 0x0f 2 2 i2cscan to make sure bus is actually wedged i2cunwedge i2cscan to make sure bus is now unwedged. (2) Bit bang transaction to do a "read" and stop partway, then reboot: i2cwedge 0x1c 0x0f 6 2. i2cscan to verify that the bus is working after the reboot. Change-Id: Ie3328e843ffb40f5001c96626fea131c0f9ad9b1 Signed-off-by: Alec Berg <alecaberg@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/188422 Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'chip/mec1322/i2c.c')
-rw-r--r--chip/mec1322/i2c.c75
1 files changed, 73 insertions, 2 deletions
diff --git a/chip/mec1322/i2c.c b/chip/mec1322/i2c.c
index 98dd28e170..feaade3813 100644
--- a/chip/mec1322/i2c.c
+++ b/chip/mec1322/i2c.c
@@ -197,8 +197,15 @@ int i2c_xfer(int port, int slave_addr, const uint8_t *out, int out_size,
reg_sts = MEC1322_I2C_STATUS(port);
if (!started &&
- ((reg_sts & (STS_BER | STS_LAB)) || !(reg_sts & STS_NBB))) {
- CPRINTF("[%T I2C%d bad status 0x%02x]\n", port, reg_sts);
+ (((reg_sts & (STS_BER | STS_LAB)) || !(reg_sts & STS_NBB)) ||
+ (i2c_get_line_levels(port) != I2C_LINE_IDLE))) {
+ CPRINTF("[%T I2C%d bad status 0x%02x, SCL=%d, SDA=%d]\n", port,
+ reg_sts,
+ i2c_get_line_levels(port) & I2C_LINE_SCL_HIGH,
+ i2c_get_line_levels(port) & I2C_LINE_SDA_HIGH);
+
+ /* Attempt to unwedge the port. */
+ i2c_unwedge(port);
/* Bus error, bus busy, or arbitration lost. Reset port. */
reset_port(port);
@@ -296,6 +303,70 @@ err_i2c_xfer:
return EC_ERROR_UNKNOWN;
}
+int i2c_raw_get_scl(int port)
+{
+ enum gpio_signal g;
+ int ret;
+
+ /* If no SCL pin defined for this port, then return 1 to appear idle. */
+ if (get_scl_from_i2c_port(port, &g) != EC_SUCCESS)
+ return 1;
+
+ /*
+ * TODO(crosbug.com/p/26483): The following code assumes the worst case,
+ * that since the pin is an output, gpio_get_level() will return the
+ * state that we are trying to drive the output to, instead of the
+ * actual state of the pin. Need to determine if this is the case, and
+ * if not, we can optimize.
+ */
+
+ /* If we are driving the pin low, it must be low. */
+ if (gpio_get_level(g) == 0)
+ return 0;
+
+ /*
+ * Otherwise, we need to toggle it to an input to read the true pin
+ * state.
+ */
+ gpio_set_flags(g, GPIO_INPUT);
+ ret = gpio_get_level(g);
+ gpio_set_flags(g, GPIO_ODR_HIGH);
+
+ return ret;
+}
+
+int i2c_raw_get_sda(int port)
+{
+ enum gpio_signal g;
+ int ret;
+
+ /* If no SDA pin defined for this port, then return 1 to appear idle. */
+ if (get_sda_from_i2c_port(port, &g) != EC_SUCCESS)
+ return 1;
+
+ /*
+ * TODO(crosbug.com/p/26483): The following code assumes the worst case,
+ * that since the pin is an output, gpio_get_level() will return the
+ * state that we are trying to drive the output to, instead of the
+ * actual state of the pin. Need to determine if this is the case, and
+ * if not, we can optimize.
+ */
+
+ /* If we are driving the pin low, it must be low. */
+ if (gpio_get_level(g) == 0)
+ return 0;
+
+ /*
+ * Otherwise, we need to toggle it to an input to read the true pin
+ * state.
+ */
+ gpio_set_flags(g, GPIO_INPUT);
+ ret = gpio_get_level(g);
+ gpio_set_flags(g, GPIO_ODR_HIGH);
+
+ return ret;
+}
+
int i2c_get_line_levels(int port)
{
return (MEC1322_I2C_BB_CTRL(port) >> 5) & 0x3;