blob: 85442cd10314ae3be4bc10e0a33caf35106f32d5 [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 Kiryanov82309252012-01-12 03:26:30 +000018#define EEPROM_LAYOUT_VER_OFFSET 44
19#define BOARD_SERIAL_OFFSET 20
20#define BOARD_SERIAL_OFFSET_LEGACY 8
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000021#define BOARD_REV_OFFSET 0
22#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +000023#define BOARD_REV_SIZE 2
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000024#define MAC_ADDR_OFFSET 4
25#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanov82309252012-01-12 03:26:30 +000026
27#define LAYOUT_INVALID 0
28#define LAYOUT_LEGACY 0xff
29
Igor Grinberg689be5f2013-09-16 21:49:58 +030030static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanov82309252012-01-12 03:26:30 +000031
Igor Grinberg689be5f2013-09-16 21:49:58 +030032static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanov82309252012-01-12 03:26:30 +000033{
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030034 int res;
35 unsigned int current_i2c_bus = i2c_get_bus_num();
36
37 res = i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
38 if (res < 0)
39 return res;
40
41 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
Nikita Kiryanov82309252012-01-12 03:26:30 +000042 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030043
44 i2c_set_bus_num(current_i2c_bus);
45
46 return res;
Nikita Kiryanov82309252012-01-12 03:26:30 +000047}
48
Igor Grinberg689be5f2013-09-16 21:49:58 +030049static int cl_eeprom_setup_layout(void)
Nikita Kiryanov82309252012-01-12 03:26:30 +000050{
51 int res;
52
Igor Grinberg689be5f2013-09-16 21:49:58 +030053 if (cl_eeprom_layout != LAYOUT_INVALID)
Nikita Kiryanov82309252012-01-12 03:26:30 +000054 return 0;
55
Igor Grinberg689be5f2013-09-16 21:49:58 +030056 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
57 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanov82309252012-01-12 03:26:30 +000058 if (res) {
Igor Grinberg689be5f2013-09-16 21:49:58 +030059 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanov82309252012-01-12 03:26:30 +000060 return res;
61 }
62
Igor Grinberg689be5f2013-09-16 21:49:58 +030063 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
64 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanov82309252012-01-12 03:26:30 +000065
66 return 0;
67}
68
69void get_board_serial(struct tag_serialnr *serialnr)
70{
71 u32 serial[2];
72 uint offset;
73
74 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg689be5f2013-09-16 21:49:58 +030075
76 if (cl_eeprom_setup_layout())
Nikita Kiryanov82309252012-01-12 03:26:30 +000077 return;
78
Igor Grinberg689be5f2013-09-16 21:49:58 +030079 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
80 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
81
82 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanov82309252012-01-12 03:26:30 +000083 return;
84
85 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
86 serialnr->low = serial[0];
87 serialnr->high = serial[1];
88 }
89}
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000090
91/*
Igor Grinberg689be5f2013-09-16 21:49:58 +030092 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000093 * Description: read mac address and store it in buf.
94 */
Igor Grinberg689be5f2013-09-16 21:49:58 +030095int cl_eeprom_read_mac_addr(uchar *buf)
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000096{
97 uint offset;
98
Igor Grinberg689be5f2013-09-16 21:49:58 +030099 if (cl_eeprom_setup_layout())
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000100 return 0;
101
Igor Grinberg689be5f2013-09-16 21:49:58 +0300102 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000103 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg689be5f2013-09-16 21:49:58 +0300104
105 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000106}
107
108/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300109 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +0000110 * Description: read system revision from eeprom
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000111 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300112u32 cl_eeprom_get_board_rev(void)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000113{
114 u32 rev = 0;
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000115 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000116 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000117
Igor Grinberg689be5f2013-09-16 21:49:58 +0300118 if (cl_eeprom_setup_layout())
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000119 return 0;
120
Igor Grinberg689be5f2013-09-16 21:49:58 +0300121 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000122 offset = BOARD_REV_OFFSET;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000123
Igor Grinberg689be5f2013-09-16 21:49:58 +0300124 if (cl_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000125 return 0;
126
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000127 /*
128 * Convert legacy syntactic representation to semantic
129 * representation. i.e. for rev 1.00: 0x100 --> 0x64
130 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300131 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000132 sprintf(str, "%x", rev);
133 rev = simple_strtoul(str, NULL, 10);
134 }
135
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000136 return rev;
137};