2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-12-10 15:55:51 +00:00
|
|
|
/*
|
|
|
|
* Simulate an I2C eeprom
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2015-05-04 17:31:08 +00:00
|
|
|
#include <errno.h>
|
2014-12-10 15:55:51 +00:00
|
|
|
#include <i2c.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2014-12-10 15:55:51 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <asm/test.h>
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define debug_buffer print_buffer
|
|
|
|
#else
|
|
|
|
#define debug_buffer(x, ...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct sandbox_i2c_flash_plat_data {
|
|
|
|
enum sandbox_i2c_eeprom_test_mode test_mode;
|
|
|
|
const char *filename;
|
|
|
|
int offset_len; /* Length of an offset in bytes */
|
|
|
|
int size; /* Size of data buffer */
|
2019-10-28 17:44:59 +00:00
|
|
|
uint chip_addr_offset_mask; /* mask of addr bits used for offset */
|
2014-12-10 15:55:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sandbox_i2c_flash {
|
|
|
|
uint8_t *data;
|
2019-10-28 17:44:58 +00:00
|
|
|
uint prev_addr; /* slave address of previous access */
|
|
|
|
uint prev_offset; /* offset of previous access */
|
2014-12-10 15:55:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
|
|
|
|
enum sandbox_i2c_eeprom_test_mode mode)
|
|
|
|
{
|
2020-12-03 23:55:20 +00:00
|
|
|
struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
|
2014-12-10 15:55:51 +00:00
|
|
|
|
|
|
|
plat->test_mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
|
|
|
|
{
|
2020-12-03 23:55:20 +00:00
|
|
|
struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
|
2014-12-10 15:55:51 +00:00
|
|
|
|
|
|
|
plat->offset_len = offset_len;
|
|
|
|
}
|
|
|
|
|
2019-10-28 17:44:59 +00:00
|
|
|
void sandbox_i2c_eeprom_set_chip_addr_offset_mask(struct udevice *dev,
|
|
|
|
uint mask)
|
|
|
|
{
|
2020-12-03 23:55:20 +00:00
|
|
|
struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
|
2019-10-28 17:44:59 +00:00
|
|
|
|
|
|
|
plat->chip_addr_offset_mask = mask;
|
|
|
|
}
|
|
|
|
|
2019-10-28 17:44:58 +00:00
|
|
|
uint sanbox_i2c_eeprom_get_prev_addr(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_i2c_flash *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->prev_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint sanbox_i2c_eeprom_get_prev_offset(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_i2c_flash *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->prev_offset;
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:55:51 +00:00
|
|
|
static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg,
|
|
|
|
int nmsgs)
|
|
|
|
{
|
|
|
|
struct sandbox_i2c_flash *priv = dev_get_priv(emul);
|
2020-12-03 23:55:20 +00:00
|
|
|
struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(emul);
|
2019-10-28 17:44:59 +00:00
|
|
|
uint offset = msg->addr & plat->chip_addr_offset_mask;
|
2014-12-10 15:55:51 +00:00
|
|
|
|
|
|
|
debug("\n%s\n", __func__);
|
|
|
|
debug_buffer(0, priv->data, 1, 16, 0);
|
2019-10-28 17:44:58 +00:00
|
|
|
|
|
|
|
/* store addr for testing visibity */
|
|
|
|
priv->prev_addr = msg->addr;
|
|
|
|
|
2014-12-10 15:55:51 +00:00
|
|
|
for (; nmsgs > 0; nmsgs--, msg++) {
|
|
|
|
int len;
|
|
|
|
u8 *ptr;
|
|
|
|
|
|
|
|
if (!plat->size)
|
|
|
|
return -ENODEV;
|
|
|
|
len = msg->len;
|
2019-10-28 17:44:59 +00:00
|
|
|
debug(" %s: msg->addr=%x msg->len=%d",
|
2014-12-10 15:55:51 +00:00
|
|
|
msg->flags & I2C_M_RD ? "read" : "write",
|
2019-10-28 17:44:59 +00:00
|
|
|
msg->addr, msg->len);
|
2014-12-10 15:55:51 +00:00
|
|
|
if (msg->flags & I2C_M_RD) {
|
|
|
|
if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
|
|
|
|
len = 1;
|
|
|
|
debug(", offset %x, len %x: ", offset, len);
|
2019-10-28 17:44:58 +00:00
|
|
|
if (offset + len > plat->size) {
|
|
|
|
int overflow = offset + len - plat->size;
|
|
|
|
int initial = len - overflow;
|
|
|
|
|
|
|
|
memcpy(msg->buf, priv->data + offset, initial);
|
|
|
|
memcpy(msg->buf + initial, priv->data,
|
|
|
|
overflow);
|
|
|
|
} else {
|
|
|
|
memcpy(msg->buf, priv->data + offset, len);
|
|
|
|
}
|
2014-12-10 15:55:51 +00:00
|
|
|
memset(msg->buf + len, '\xff', msg->len - len);
|
|
|
|
debug_buffer(0, msg->buf, 1, msg->len, 0);
|
|
|
|
} else if (len >= plat->offset_len) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ptr = msg->buf;
|
|
|
|
for (i = 0; i < plat->offset_len; i++, len--)
|
|
|
|
offset = (offset << 8) | *ptr++;
|
|
|
|
debug(", set offset %x: ", offset);
|
|
|
|
debug_buffer(0, msg->buf, 1, msg->len, 0);
|
|
|
|
if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
|
|
|
|
len = min(len, 1);
|
|
|
|
|
2019-10-28 17:44:58 +00:00
|
|
|
/* store offset for testing visibility */
|
|
|
|
priv->prev_offset = offset;
|
|
|
|
|
|
|
|
/* For testing, map offsets into our limited buffer.
|
|
|
|
* offset wraps every 256 bytes
|
|
|
|
*/
|
|
|
|
offset &= 0xff;
|
|
|
|
debug("mapped offset to %x\n", offset);
|
|
|
|
|
|
|
|
if (offset + len > plat->size) {
|
|
|
|
int overflow = offset + len - plat->size;
|
|
|
|
int initial = len - overflow;
|
|
|
|
|
|
|
|
memcpy(priv->data + offset, ptr, initial);
|
|
|
|
memcpy(priv->data, ptr + initial, overflow);
|
|
|
|
} else {
|
|
|
|
memcpy(priv->data + offset, ptr, len);
|
2014-12-10 15:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_buffer(0, priv->data, 1, 16, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dm_i2c_ops sandbox_i2c_emul_ops = {
|
|
|
|
.xfer = sandbox_i2c_eeprom_xfer,
|
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int sandbox_i2c_eeprom_of_to_plat(struct udevice *dev)
|
2014-12-10 15:55:51 +00:00
|
|
|
{
|
2020-12-03 23:55:20 +00:00
|
|
|
struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
|
2014-12-10 15:55:51 +00:00
|
|
|
|
2017-05-19 02:09:52 +00:00
|
|
|
plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
|
|
|
|
plat->filename = dev_read_string(dev, "sandbox,filename");
|
2014-12-10 15:55:51 +00:00
|
|
|
if (!plat->filename) {
|
|
|
|
debug("%s: No filename for device '%s'\n", __func__,
|
|
|
|
dev->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
plat->test_mode = SIE_TEST_MODE_NONE;
|
|
|
|
plat->offset_len = 1;
|
2019-10-28 17:44:59 +00:00
|
|
|
plat->chip_addr_offset_mask = 0;
|
2014-12-10 15:55:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_i2c_eeprom_probe(struct udevice *dev)
|
|
|
|
{
|
2020-12-03 23:55:20 +00:00
|
|
|
struct sandbox_i2c_flash_plat_data *plat = dev_get_plat(dev);
|
2014-12-10 15:55:51 +00:00
|
|
|
struct sandbox_i2c_flash *priv = dev_get_priv(dev);
|
2022-05-05 17:11:42 +00:00
|
|
|
/* For eth3 */
|
|
|
|
const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x45 };
|
2014-12-10 15:55:51 +00:00
|
|
|
|
|
|
|
priv->data = calloc(1, plat->size);
|
|
|
|
if (!priv->data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2022-05-05 17:11:42 +00:00
|
|
|
memcpy(&priv->data[24], mac, sizeof(mac));
|
|
|
|
|
2014-12-10 15:55:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_i2c_eeprom_remove(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_i2c_flash *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
free(priv->data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_i2c_ids[] = {
|
|
|
|
{ .compatible = "sandbox,i2c-eeprom" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(sandbox_i2c_emul) = {
|
|
|
|
.name = "sandbox_i2c_eeprom_emul",
|
|
|
|
.id = UCLASS_I2C_EMUL,
|
|
|
|
.of_match = sandbox_i2c_ids,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = sandbox_i2c_eeprom_of_to_plat,
|
2014-12-10 15:55:51 +00:00
|
|
|
.probe = sandbox_i2c_eeprom_probe,
|
|
|
|
.remove = sandbox_i2c_eeprom_remove,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct sandbox_i2c_flash),
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct sandbox_i2c_flash_plat_data),
|
2014-12-10 15:55:51 +00:00
|
|
|
.ops = &sandbox_i2c_emul_ops,
|
|
|
|
};
|