blob: 2df3adabf8b3dd097fcfcb6006b7d6baa10bbafe [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
Igor Grinbergd3f041c2014-05-15 11:25:09 +030013#ifndef CONFIG_SYS_I2C_EEPROM_ADDR
14# define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
15# define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
16#endif
17
Nikita Kiryanov7d2f6692014-09-17 15:59:25 +030018#ifndef CONFIG_SYS_I2C_EEPROM_BUS
19#define CONFIG_SYS_I2C_EEPROM_BUS 0
20#endif
21
Nikita Kiryanov82309252012-01-12 03:26:30 +000022#define EEPROM_LAYOUT_VER_OFFSET 44
23#define BOARD_SERIAL_OFFSET 20
24#define BOARD_SERIAL_OFFSET_LEGACY 8
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000025#define BOARD_REV_OFFSET 0
26#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +000027#define BOARD_REV_SIZE 2
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000028#define MAC_ADDR_OFFSET 4
29#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanov82309252012-01-12 03:26:30 +000030
31#define LAYOUT_INVALID 0
32#define LAYOUT_LEGACY 0xff
33
Igor Grinberg689be5f2013-09-16 21:49:58 +030034static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanov82309252012-01-12 03:26:30 +000035
Igor Grinberg689be5f2013-09-16 21:49:58 +030036static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanov82309252012-01-12 03:26:30 +000037{
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030038 int res;
39 unsigned int current_i2c_bus = i2c_get_bus_num();
40
41 res = i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
42 if (res < 0)
43 return res;
44
45 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
Nikita Kiryanov82309252012-01-12 03:26:30 +000046 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030047
48 i2c_set_bus_num(current_i2c_bus);
49
50 return res;
Nikita Kiryanov82309252012-01-12 03:26:30 +000051}
52
Igor Grinberg689be5f2013-09-16 21:49:58 +030053static int cl_eeprom_setup_layout(void)
Nikita Kiryanov82309252012-01-12 03:26:30 +000054{
55 int res;
56
Igor Grinberg689be5f2013-09-16 21:49:58 +030057 if (cl_eeprom_layout != LAYOUT_INVALID)
Nikita Kiryanov82309252012-01-12 03:26:30 +000058 return 0;
59
Igor Grinberg689be5f2013-09-16 21:49:58 +030060 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
61 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanov82309252012-01-12 03:26:30 +000062 if (res) {
Igor Grinberg689be5f2013-09-16 21:49:58 +030063 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanov82309252012-01-12 03:26:30 +000064 return res;
65 }
66
Igor Grinberg689be5f2013-09-16 21:49:58 +030067 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
68 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanov82309252012-01-12 03:26:30 +000069
70 return 0;
71}
72
73void get_board_serial(struct tag_serialnr *serialnr)
74{
75 u32 serial[2];
76 uint offset;
77
78 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg689be5f2013-09-16 21:49:58 +030079
80 if (cl_eeprom_setup_layout())
Nikita Kiryanov82309252012-01-12 03:26:30 +000081 return;
82
Igor Grinberg689be5f2013-09-16 21:49:58 +030083 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
84 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
85
86 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanov82309252012-01-12 03:26:30 +000087 return;
88
89 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
90 serialnr->low = serial[0];
91 serialnr->high = serial[1];
92 }
93}
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000094
95/*
Igor Grinberg689be5f2013-09-16 21:49:58 +030096 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000097 * Description: read mac address and store it in buf.
98 */
Igor Grinberg689be5f2013-09-16 21:49:58 +030099int cl_eeprom_read_mac_addr(uchar *buf)
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000100{
101 uint offset;
102
Igor Grinberg689be5f2013-09-16 21:49:58 +0300103 if (cl_eeprom_setup_layout())
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000104 return 0;
105
Igor Grinberg689be5f2013-09-16 21:49:58 +0300106 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000107 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg689be5f2013-09-16 21:49:58 +0300108
109 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000110}
111
112/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300113 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +0000114 * Description: read system revision from eeprom
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000115 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300116u32 cl_eeprom_get_board_rev(void)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000117{
118 u32 rev = 0;
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000119 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000120 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000121
Igor Grinberg689be5f2013-09-16 21:49:58 +0300122 if (cl_eeprom_setup_layout())
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000123 return 0;
124
Igor Grinberg689be5f2013-09-16 21:49:58 +0300125 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000126 offset = BOARD_REV_OFFSET;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000127
Igor Grinberg689be5f2013-09-16 21:49:58 +0300128 if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000129 return 0;
130
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000131 /*
132 * Convert legacy syntactic representation to semantic
133 * representation. i.e. for rev 1.00: 0x100 --> 0x64
134 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300135 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000136 sprintf(str, "%x", rev);
137 rev = simple_strtoul(str, NULL, 10);
138 }
139
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000140 return rev;
141};