2012-01-12 03:26:30 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
|
|
|
|
*
|
|
|
|
* Authors: Nikita Kiryanov <nikita@compulab.co.il>
|
|
|
|
* Igor Grinberg <grinberg@compulab.co.il>
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-01-12 03:26:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <i2c.h>
|
|
|
|
|
2014-05-15 08:25:09 +00:00
|
|
|
#ifndef CONFIG_SYS_I2C_EEPROM_ADDR
|
|
|
|
# define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
|
|
|
|
# define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
|
|
|
|
#endif
|
|
|
|
|
2014-09-17 12:59:25 +00:00
|
|
|
#ifndef CONFIG_SYS_I2C_EEPROM_BUS
|
|
|
|
#define CONFIG_SYS_I2C_EEPROM_BUS 0
|
|
|
|
#endif
|
|
|
|
|
2012-01-12 03:26:30 +00:00
|
|
|
#define EEPROM_LAYOUT_VER_OFFSET 44
|
|
|
|
#define BOARD_SERIAL_OFFSET 20
|
|
|
|
#define BOARD_SERIAL_OFFSET_LEGACY 8
|
2012-01-02 04:01:34 +00:00
|
|
|
#define BOARD_REV_OFFSET 0
|
|
|
|
#define BOARD_REV_OFFSET_LEGACY 6
|
2012-05-24 04:01:22 +00:00
|
|
|
#define BOARD_REV_SIZE 2
|
2012-01-12 03:28:09 +00:00
|
|
|
#define MAC_ADDR_OFFSET 4
|
|
|
|
#define MAC_ADDR_OFFSET_LEGACY 0
|
2012-01-12 03:26:30 +00:00
|
|
|
|
|
|
|
#define LAYOUT_INVALID 0
|
|
|
|
#define LAYOUT_LEGACY 0xff
|
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
static int cl_eeprom_bus;
|
2013-09-16 18:49:58 +00:00
|
|
|
static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
|
2012-01-12 03:26:30 +00:00
|
|
|
|
2013-09-16 18:49:58 +00:00
|
|
|
static int cl_eeprom_read(uint offset, uchar *buf, int len)
|
2012-01-12 03:26:30 +00:00
|
|
|
{
|
2014-08-20 12:08:52 +00:00
|
|
|
int res;
|
|
|
|
unsigned int current_i2c_bus = i2c_get_bus_num();
|
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
res = i2c_set_bus_num(cl_eeprom_bus);
|
2014-08-20 12:08:52 +00:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
|
2012-01-12 03:26:30 +00:00
|
|
|
CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
|
2014-08-20 12:08:52 +00:00
|
|
|
|
|
|
|
i2c_set_bus_num(current_i2c_bus);
|
|
|
|
|
|
|
|
return res;
|
2012-01-12 03:26:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
static int cl_eeprom_setup(uint eeprom_bus)
|
2012-01-12 03:26:30 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
/*
|
|
|
|
* We know the setup was already done when the layout is set to a valid
|
|
|
|
* value and we're using the same bus as before.
|
|
|
|
*/
|
|
|
|
if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
|
2012-01-12 03:26:30 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
cl_eeprom_bus = eeprom_bus;
|
2013-09-16 18:49:58 +00:00
|
|
|
res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
|
|
|
|
(uchar *)&cl_eeprom_layout, 1);
|
2012-01-12 03:26:30 +00:00
|
|
|
if (res) {
|
2013-09-16 18:49:58 +00:00
|
|
|
cl_eeprom_layout = LAYOUT_INVALID;
|
2012-01-12 03:26:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-09-16 18:49:58 +00:00
|
|
|
if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
|
|
|
|
cl_eeprom_layout = LAYOUT_LEGACY;
|
2012-01-12 03:26:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_board_serial(struct tag_serialnr *serialnr)
|
|
|
|
{
|
|
|
|
u32 serial[2];
|
|
|
|
uint offset;
|
|
|
|
|
|
|
|
memset(serialnr, 0, sizeof(*serialnr));
|
2013-09-16 18:49:58 +00:00
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
|
2012-01-12 03:26:30 +00:00
|
|
|
return;
|
|
|
|
|
2013-09-16 18:49:58 +00:00
|
|
|
offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
|
|
|
|
BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
|
|
|
|
|
|
|
|
if (cl_eeprom_read(offset, (uchar *)serial, 8))
|
2012-01-12 03:26:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
|
|
|
|
serialnr->low = serial[0];
|
|
|
|
serialnr->high = serial[1];
|
|
|
|
}
|
|
|
|
}
|
2012-01-02 04:01:34 +00:00
|
|
|
|
2012-01-12 03:28:09 +00:00
|
|
|
/*
|
2013-09-16 18:49:58 +00:00
|
|
|
* Routine: cl_eeprom_read_mac_addr
|
2012-01-12 03:28:09 +00:00
|
|
|
* Description: read mac address and store it in buf.
|
|
|
|
*/
|
2015-01-14 08:42:43 +00:00
|
|
|
int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
|
2012-01-12 03:28:09 +00:00
|
|
|
{
|
|
|
|
uint offset;
|
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
if (cl_eeprom_setup(eeprom_bus))
|
2012-01-12 03:28:09 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-09-16 18:49:58 +00:00
|
|
|
offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
|
2012-01-12 03:28:09 +00:00
|
|
|
MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
|
2013-09-16 18:49:58 +00:00
|
|
|
|
|
|
|
return cl_eeprom_read(offset, buf, 6);
|
2012-01-12 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 09:32:18 +00:00
|
|
|
static u32 board_rev;
|
|
|
|
|
2012-01-02 04:01:34 +00:00
|
|
|
/*
|
2013-09-16 18:49:58 +00:00
|
|
|
* Routine: cl_eeprom_get_board_rev
|
2012-05-24 04:01:24 +00:00
|
|
|
* Description: read system revision from eeprom
|
2012-01-02 04:01:34 +00:00
|
|
|
*/
|
2013-09-16 18:49:58 +00:00
|
|
|
u32 cl_eeprom_get_board_rev(void)
|
2012-01-02 04:01:34 +00:00
|
|
|
{
|
2012-05-24 04:01:23 +00:00
|
|
|
char str[5]; /* Legacy representation can contain at most 4 digits */
|
2012-01-02 04:01:34 +00:00
|
|
|
uint offset = BOARD_REV_OFFSET_LEGACY;
|
|
|
|
|
2014-11-03 09:32:18 +00:00
|
|
|
if (board_rev)
|
|
|
|
return board_rev;
|
|
|
|
|
2015-01-14 08:42:43 +00:00
|
|
|
if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
|
2012-01-02 04:01:34 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-09-16 18:49:58 +00:00
|
|
|
if (cl_eeprom_layout != LAYOUT_LEGACY)
|
2012-01-02 04:01:34 +00:00
|
|
|
offset = BOARD_REV_OFFSET;
|
|
|
|
|
2014-11-03 09:32:18 +00:00
|
|
|
if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
|
2012-01-02 04:01:34 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-05-24 04:01:23 +00:00
|
|
|
/*
|
|
|
|
* Convert legacy syntactic representation to semantic
|
|
|
|
* representation. i.e. for rev 1.00: 0x100 --> 0x64
|
|
|
|
*/
|
2013-09-16 18:49:58 +00:00
|
|
|
if (cl_eeprom_layout == LAYOUT_LEGACY) {
|
2014-11-03 09:32:18 +00:00
|
|
|
sprintf(str, "%x", board_rev);
|
|
|
|
board_rev = simple_strtoul(str, NULL, 10);
|
2012-05-24 04:01:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 09:32:18 +00:00
|
|
|
return board_rev;
|
2012-01-02 04:01:34 +00:00
|
|
|
};
|