2013-10-07 10:57:42 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <arch/io.h>
|
|
|
|
#include <console/console.h>
|
|
|
|
#include <device/i2c.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <soc/addressmap.h>
|
|
|
|
#include "i2c.h"
|
|
|
|
|
nyan*: I2C: Implement bus clear when 'ARB_LOST' error occurs
This is a fix for the 'Lost arb' we're seeing on Nyan* during
reboot stress testing. It occurs when we are slamming the
default PMIC registers with pmic_write_reg().
Currently, I've only captured this a few times, and the bus
clear seemed to work, as the PMIC writes continued (where
they'd hang the system before bus clear) for a couple of regs,
then it hangs hard, no messages, no 2nd lost arb, etc. So
I've added code to the PMIC write function that will reset the
SoC if any I2C error occurs. That seems to recover OK, i.e. on
the next reboot the PMIC writes all go thru, boot is OK, kernel
loads, etc.
BUG=chrome-os-partner:28323
BRANCH=nyan
TEST=Tested on nyan. Built for nyan and nyan_big.
Original-Change-Id: I1ac5e3023ae22c015105b7f0fb7849663b4aa982
Original-Signed-off-by: Tom Warren <twarren@nvidia.com>
Original-Reviewed-on: https://chromium-review.googlesource.com/197732
Original-Reviewed-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-by: Jimmy Zhang <jimmzhang@nvidia.com>
(cherry picked from commit f445127e2d9e223a5ef9117008a7ac7631a7980c)
Signed-off-by: Marc Jones <marc.jones@se-eng.com>
Change-Id: I584d55b99d65f1e278961db6bdde1845cb01f3bc
Reviewed-on: http://review.coreboot.org/7897
Tested-by: build bot (Jenkins)
Reviewed-by: David Hendricks <dhendrix@chromium.org>
2014-04-30 23:51:38 +02:00
|
|
|
static void do_bus_clear(int bus)
|
|
|
|
{
|
|
|
|
struct tegra_i2c_bus_info *info = &tegra_i2c_info[bus];
|
|
|
|
struct tegra_i2c_regs * const regs = info->base;
|
|
|
|
uint32_t bc;
|
|
|
|
|
|
|
|
// BUS CLEAR regs (from TRM):
|
|
|
|
// 1. Reset the I2C controller (already done)
|
|
|
|
// 2. Set the # of clock pulses required (using default of 9)
|
|
|
|
// 3. Select STOP condition (using default of 1 = STOP)
|
|
|
|
// 4. Set TERMINATE condition (1 = THRESHOLD)
|
|
|
|
bc = read32(®s->bus_clear_config);
|
|
|
|
bc |= I2C_BUS_CLEAR_CONFIG_BC_TERMINATE_THRESHOLD;
|
|
|
|
write32(bc, ®s->bus_clear_config);
|
|
|
|
// 4.1 Set MSTR_CONFIG_LOAD and wait for clear
|
|
|
|
write32(I2C_CONFIG_LOAD_MSTR_CONFIG_LOAD_ENABLE, ®s->config_load);
|
|
|
|
do {
|
|
|
|
printk(BIOS_DEBUG, "%s: wait for MSTR_CONFIG_LOAD to clear\n",
|
|
|
|
__func__);
|
|
|
|
} while (read32(®s->config_load) & I2C_CONFIG_LOAD_MSTR_CONFIG_LOAD_ENABLE);
|
|
|
|
// 5. Set ENABLE to start the bus clear op
|
|
|
|
write32(bc | I2C_BUS_CLEAR_CONFIG_BC_ENABLE, ®s->bus_clear_config);
|
|
|
|
do {
|
|
|
|
printk(BIOS_DEBUG, "%s: wait for bus clear completion\n",
|
|
|
|
__func__);
|
|
|
|
} while (read32(®s->bus_clear_config) & I2C_BUS_CLEAR_CONFIG_BC_ENABLE);
|
|
|
|
}
|
|
|
|
|
2014-03-27 05:43:53 +01:00
|
|
|
static int tegra_i2c_send_recv(int bus, int read,
|
2013-10-07 10:57:42 +02:00
|
|
|
uint32_t *headers, int header_words,
|
|
|
|
uint8_t *data, int data_len)
|
|
|
|
{
|
2014-03-27 05:43:53 +01:00
|
|
|
struct tegra_i2c_bus_info *info = &tegra_i2c_info[bus];
|
|
|
|
struct tegra_i2c_regs * const regs = info->base;
|
|
|
|
|
2013-10-07 10:57:42 +02:00
|
|
|
while (data_len) {
|
|
|
|
uint32_t status = read32(®s->fifo_status);
|
2013-10-10 08:45:07 +02:00
|
|
|
int tx_empty = status & I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT_MASK;
|
|
|
|
tx_empty >>= I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT_SHIFT;
|
|
|
|
int rx_full = status & I2C_FIFO_STATUS_RX_FIFO_FULL_CNT_MASK;
|
|
|
|
rx_full >>= I2C_FIFO_STATUS_RX_FIFO_FULL_CNT_SHIFT;
|
2013-10-07 10:57:42 +02:00
|
|
|
|
|
|
|
while (header_words && tx_empty) {
|
|
|
|
write32(*headers++, ®s->tx_packet_fifo);
|
|
|
|
header_words--;
|
|
|
|
tx_empty--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!header_words) {
|
|
|
|
if (read) {
|
|
|
|
while (data_len && rx_full) {
|
|
|
|
uint32_t word = read32(®s->rx_fifo);
|
|
|
|
int todo = MIN(data_len, sizeof(word));
|
|
|
|
|
|
|
|
memcpy(data, &word, todo);
|
|
|
|
data_len -= todo;
|
|
|
|
data += sizeof(word);
|
|
|
|
rx_full--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (data_len && tx_empty) {
|
|
|
|
uint32_t word;
|
|
|
|
int todo = MIN(data_len, sizeof(word));
|
|
|
|
|
|
|
|
memcpy(&word, data, todo);
|
|
|
|
write32(word, ®s->tx_packet_fifo);
|
|
|
|
data_len -= todo;
|
|
|
|
data += sizeof(word);
|
|
|
|
tx_empty--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t transfer_status =
|
|
|
|
read32(®s->packet_transfer_status);
|
|
|
|
|
2013-10-10 08:45:07 +02:00
|
|
|
if (transfer_status & I2C_PKT_STATUS_NOACK_ADDR) {
|
2013-10-07 10:57:42 +02:00
|
|
|
printk(BIOS_ERR,
|
|
|
|
"%s: The address was not acknowledged.\n",
|
|
|
|
__func__);
|
2014-03-27 05:43:53 +01:00
|
|
|
info->reset_func(info->reset_bit);
|
2013-10-07 10:57:42 +02:00
|
|
|
return -1;
|
2013-10-10 08:45:07 +02:00
|
|
|
} else if (transfer_status & I2C_PKT_STATUS_NOACK_DATA) {
|
2013-10-07 10:57:42 +02:00
|
|
|
printk(BIOS_ERR,
|
|
|
|
"%s: The data was not acknowledged.\n",
|
|
|
|
__func__);
|
2014-03-27 05:43:53 +01:00
|
|
|
info->reset_func(info->reset_bit);
|
2013-10-07 10:57:42 +02:00
|
|
|
return -1;
|
2013-10-10 08:45:07 +02:00
|
|
|
} else if (transfer_status & I2C_PKT_STATUS_ARB_LOST) {
|
2013-10-07 10:57:42 +02:00
|
|
|
printk(BIOS_ERR,
|
|
|
|
"%s: Lost arbitration.\n",
|
|
|
|
__func__);
|
2014-03-27 05:43:53 +01:00
|
|
|
info->reset_func(info->reset_bit);
|
nyan*: I2C: Implement bus clear when 'ARB_LOST' error occurs
This is a fix for the 'Lost arb' we're seeing on Nyan* during
reboot stress testing. It occurs when we are slamming the
default PMIC registers with pmic_write_reg().
Currently, I've only captured this a few times, and the bus
clear seemed to work, as the PMIC writes continued (where
they'd hang the system before bus clear) for a couple of regs,
then it hangs hard, no messages, no 2nd lost arb, etc. So
I've added code to the PMIC write function that will reset the
SoC if any I2C error occurs. That seems to recover OK, i.e. on
the next reboot the PMIC writes all go thru, boot is OK, kernel
loads, etc.
BUG=chrome-os-partner:28323
BRANCH=nyan
TEST=Tested on nyan. Built for nyan and nyan_big.
Original-Change-Id: I1ac5e3023ae22c015105b7f0fb7849663b4aa982
Original-Signed-off-by: Tom Warren <twarren@nvidia.com>
Original-Reviewed-on: https://chromium-review.googlesource.com/197732
Original-Reviewed-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-by: Jimmy Zhang <jimmzhang@nvidia.com>
(cherry picked from commit f445127e2d9e223a5ef9117008a7ac7631a7980c)
Signed-off-by: Marc Jones <marc.jones@se-eng.com>
Change-Id: I584d55b99d65f1e278961db6bdde1845cb01f3bc
Reviewed-on: http://review.coreboot.org/7897
Tested-by: build bot (Jenkins)
Reviewed-by: David Hendricks <dhendrix@chromium.org>
2014-04-30 23:51:38 +02:00
|
|
|
|
|
|
|
/* Use Tegra bus clear registers to unlock SDA */
|
|
|
|
do_bus_clear(bus);
|
|
|
|
|
|
|
|
/* Return w/error, let caller decide what to do */
|
2013-10-07 10:57:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tegra_i2c_request(int bus, unsigned chip, int cont, int restart,
|
|
|
|
int read, void *data, int data_len)
|
|
|
|
{
|
|
|
|
uint32_t headers[3];
|
|
|
|
|
|
|
|
if (restart && cont) {
|
|
|
|
printk(BIOS_ERR, "%s: Repeat start and continue xfer are "
|
|
|
|
"mutually exclusive.\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-10 08:45:07 +02:00
|
|
|
headers[0] = (0 << IOHEADER_PROTHDRSZ_SHIFT) |
|
|
|
|
(1 << IOHEADER_PKTID_SHIFT) |
|
|
|
|
(bus << IOHEADER_CONTROLLER_ID_SHIFT) |
|
|
|
|
IOHEADER_PROTOCOL_I2C | IOHEADER_PKTTYPE_REQUEST;
|
2013-10-07 10:57:42 +02:00
|
|
|
|
2013-10-10 08:45:07 +02:00
|
|
|
headers[1] = (data_len - 1) << IOHEADER_PAYLOADSIZE_SHIFT;
|
2013-10-07 10:57:42 +02:00
|
|
|
|
|
|
|
uint32_t slave_addr = (chip << 1) | (read ? 1 : 0);
|
2013-10-10 08:45:07 +02:00
|
|
|
headers[2] = IOHEADER_I2C_REQ_ADDR_MODE_7BIT |
|
2013-10-07 10:57:42 +02:00
|
|
|
(slave_addr << IOHEADER_I2C_REQ_SLAVE_ADDR_SHIFT);
|
|
|
|
if (read)
|
2013-10-10 08:45:07 +02:00
|
|
|
headers[2] |= IOHEADER_I2C_REQ_READ;
|
2013-10-07 10:57:42 +02:00
|
|
|
if (restart)
|
2013-10-10 08:45:07 +02:00
|
|
|
headers[2] |= IOHEADER_I2C_REQ_REPEAT_START;
|
2013-10-07 10:57:42 +02:00
|
|
|
if (cont)
|
2013-10-10 08:45:07 +02:00
|
|
|
headers[2] |= IOHEADER_I2C_REQ_CONTINUE_XFER;
|
2013-10-07 10:57:42 +02:00
|
|
|
|
2014-03-27 05:43:53 +01:00
|
|
|
return tegra_i2c_send_recv(bus, read, headers, ARRAY_SIZE(headers),
|
2013-10-07 10:57:42 +02:00
|
|
|
data, data_len);
|
|
|
|
}
|
|
|
|
|
2014-04-08 03:45:14 +02:00
|
|
|
static int i2c_transfer_segment(unsigned bus, unsigned chip, int restart,
|
|
|
|
int read, void *buf, int len)
|
2013-10-07 10:57:42 +02:00
|
|
|
{
|
|
|
|
const uint32_t max_payload =
|
2013-10-10 08:45:07 +02:00
|
|
|
(IOHEADER_PAYLOADSIZE_MASK + 1) >> IOHEADER_PAYLOADSIZE_SHIFT;
|
2013-10-07 10:57:42 +02:00
|
|
|
|
|
|
|
while (len) {
|
|
|
|
int todo = MIN(len, max_payload);
|
|
|
|
int cont = (todo < len);
|
2014-04-08 03:45:14 +02:00
|
|
|
if (tegra_i2c_request(bus, chip, cont, restart,
|
|
|
|
read, buf, todo))
|
2013-10-07 10:57:42 +02:00
|
|
|
return -1;
|
|
|
|
len -= todo;
|
|
|
|
buf += todo;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-06 03:03:46 +02:00
|
|
|
int platform_i2c_transfer(unsigned bus, struct i2c_seg *segments, int count)
|
2013-10-07 10:57:42 +02:00
|
|
|
{
|
2014-04-08 03:45:14 +02:00
|
|
|
struct i2c_seg *seg = segments;
|
2013-10-07 10:57:42 +02:00
|
|
|
|
2014-04-08 03:45:14 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < count; seg++, i++) {
|
|
|
|
if (i2c_transfer_segment(bus, seg->chip, i < count - 1,
|
|
|
|
seg->read, seg->buf, seg->len))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-10-07 10:57:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_init(unsigned bus)
|
|
|
|
{
|
2014-03-27 05:43:53 +01:00
|
|
|
struct tegra_i2c_regs * const regs = tegra_i2c_info[bus].base;
|
2013-10-07 10:57:42 +02:00
|
|
|
|
2013-10-10 08:45:07 +02:00
|
|
|
write32(I2C_CNFG_PACKET_MODE_EN, ®s->cnfg);
|
2013-10-07 10:57:42 +02:00
|
|
|
}
|