blob: 5aa3dbd295585bdd33d2b8871a58558023e43222 [file] [log] [blame]
Nikita Kiryanov82309252012-01-12 03:26:30 +00001/*
2 * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
3 *
4 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
5 * Igor Grinberg <grinberg@compulab.co.il>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Nikita Kiryanov82309252012-01-12 03:26:30 +00008 */
9
10#include <common.h>
11#include <i2c.h>
12
13#define EEPROM_LAYOUT_VER_OFFSET 44
14#define BOARD_SERIAL_OFFSET 20
15#define BOARD_SERIAL_OFFSET_LEGACY 8
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000016#define BOARD_REV_OFFSET 0
17#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +000018#define BOARD_REV_SIZE 2
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000019#define MAC_ADDR_OFFSET 4
20#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanov82309252012-01-12 03:26:30 +000021
22#define LAYOUT_INVALID 0
23#define LAYOUT_LEGACY 0xff
24
Igor Grinberg689be5f2013-09-16 21:49:58 +030025static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanov82309252012-01-12 03:26:30 +000026
Igor Grinberg689be5f2013-09-16 21:49:58 +030027static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanov82309252012-01-12 03:26:30 +000028{
29 return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
30 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
31}
32
Igor Grinberg689be5f2013-09-16 21:49:58 +030033static int cl_eeprom_setup_layout(void)
Nikita Kiryanov82309252012-01-12 03:26:30 +000034{
35 int res;
36
Igor Grinberg689be5f2013-09-16 21:49:58 +030037 if (cl_eeprom_layout != LAYOUT_INVALID)
Nikita Kiryanov82309252012-01-12 03:26:30 +000038 return 0;
39
Igor Grinberg689be5f2013-09-16 21:49:58 +030040 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
41 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanov82309252012-01-12 03:26:30 +000042 if (res) {
Igor Grinberg689be5f2013-09-16 21:49:58 +030043 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanov82309252012-01-12 03:26:30 +000044 return res;
45 }
46
Igor Grinberg689be5f2013-09-16 21:49:58 +030047 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
48 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanov82309252012-01-12 03:26:30 +000049
50 return 0;
51}
52
53void get_board_serial(struct tag_serialnr *serialnr)
54{
55 u32 serial[2];
56 uint offset;
57
58 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg689be5f2013-09-16 21:49:58 +030059
60 if (cl_eeprom_setup_layout())
Nikita Kiryanov82309252012-01-12 03:26:30 +000061 return;
62
Igor Grinberg689be5f2013-09-16 21:49:58 +030063 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
64 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
65
66 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanov82309252012-01-12 03:26:30 +000067 return;
68
69 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
70 serialnr->low = serial[0];
71 serialnr->high = serial[1];
72 }
73}
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000074
75/*
Igor Grinberg689be5f2013-09-16 21:49:58 +030076 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000077 * Description: read mac address and store it in buf.
78 */
Igor Grinberg689be5f2013-09-16 21:49:58 +030079int cl_eeprom_read_mac_addr(uchar *buf)
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000080{
81 uint offset;
82
Igor Grinberg689be5f2013-09-16 21:49:58 +030083 if (cl_eeprom_setup_layout())
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000084 return 0;
85
Igor Grinberg689be5f2013-09-16 21:49:58 +030086 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000087 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg689be5f2013-09-16 21:49:58 +030088
89 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000090}
91
92/*
Igor Grinberg689be5f2013-09-16 21:49:58 +030093 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +000094 * Description: read system revision from eeprom
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000095 */
Igor Grinberg689be5f2013-09-16 21:49:58 +030096u32 cl_eeprom_get_board_rev(void)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000097{
98 u32 rev = 0;
Nikita Kiryanov2ef63022012-05-24 04:01:23 +000099 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000100 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000101
Igor Grinberg689be5f2013-09-16 21:49:58 +0300102 if (cl_eeprom_setup_layout())
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000103 return 0;
104
Igor Grinberg689be5f2013-09-16 21:49:58 +0300105 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000106 offset = BOARD_REV_OFFSET;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000107
Igor Grinberg689be5f2013-09-16 21:49:58 +0300108 if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000109 return 0;
110
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000111 /*
112 * Convert legacy syntactic representation to semantic
113 * representation. i.e. for rev 1.00: 0x100 --> 0x64
114 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300115 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000116 sprintf(str, "%x", rev);
117 rev = simple_strtoul(str, NULL, 10);
118 }
119
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000120 return rev;
121};