blob: 77bcea44b41cf5a5c653f1ffa2621c37ed5ea1ec [file] [log] [blame]
Nikita Kiryanovb47cb9d2012-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 Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +00008 */
9
10#include <common.h>
11#include <i2c.h>
12
Igor Grinberg79564872014-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 Kiryanova5e77062014-09-17 15:59:25 +030018#ifndef CONFIG_SYS_I2C_EEPROM_BUS
19#define CONFIG_SYS_I2C_EEPROM_BUS 0
20#endif
21
Nikita Kiryanovb47cb9d2012-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 Kiryanov2eff8502012-01-02 04:01:34 +000025#define BOARD_REV_OFFSET 0
26#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov01b1e282012-05-24 04:01:22 +000027#define BOARD_REV_SIZE 2
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +000028#define MAC_ADDR_OFFSET 4
29#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000030
31#define LAYOUT_INVALID 0
32#define LAYOUT_LEGACY 0xff
33
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020034static int cl_eeprom_bus;
Igor Grinberg3394be82013-09-16 21:49:58 +030035static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000036
Igor Grinberg3394be82013-09-16 21:49:58 +030037static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000038{
Nikita Kiryanova8eeecb2014-08-20 15:08:52 +030039 int res;
40 unsigned int current_i2c_bus = i2c_get_bus_num();
41
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020042 res = i2c_set_bus_num(cl_eeprom_bus);
Nikita Kiryanova8eeecb2014-08-20 15:08:52 +030043 if (res < 0)
44 return res;
45
46 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000047 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
Nikita Kiryanova8eeecb2014-08-20 15:08:52 +030048
49 i2c_set_bus_num(current_i2c_bus);
50
51 return res;
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000052}
53
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020054static int cl_eeprom_setup(uint eeprom_bus)
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000055{
56 int res;
57
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020058 /*
59 * We know the setup was already done when the layout is set to a valid
60 * value and we're using the same bus as before.
61 */
62 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000063 return 0;
64
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020065 cl_eeprom_bus = eeprom_bus;
Igor Grinberg3394be82013-09-16 21:49:58 +030066 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
67 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000068 if (res) {
Igor Grinberg3394be82013-09-16 21:49:58 +030069 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000070 return res;
71 }
72
Igor Grinberg3394be82013-09-16 21:49:58 +030073 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
74 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000075
76 return 0;
77}
78
79void get_board_serial(struct tag_serialnr *serialnr)
80{
81 u32 serial[2];
82 uint offset;
83
84 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg3394be82013-09-16 21:49:58 +030085
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020086 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000087 return;
88
Igor Grinberg3394be82013-09-16 21:49:58 +030089 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
90 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
91
92 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000093 return;
94
95 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
96 serialnr->low = serial[0];
97 serialnr->high = serial[1];
98 }
99}
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000100
101/*
Igor Grinberg3394be82013-09-16 21:49:58 +0300102 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000103 * Description: read mac address and store it in buf.
104 */
Nikita Kiryanovb2c55472015-01-14 10:42:43 +0200105int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000106{
107 uint offset;
108
Nikita Kiryanovb2c55472015-01-14 10:42:43 +0200109 if (cl_eeprom_setup(eeprom_bus))
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000110 return 0;
111
Igor Grinberg3394be82013-09-16 21:49:58 +0300112 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000113 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg3394be82013-09-16 21:49:58 +0300114
115 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000116}
117
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200118static u32 board_rev;
119
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000120/*
Igor Grinberg3394be82013-09-16 21:49:58 +0300121 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanovcc935c62012-05-24 04:01:24 +0000122 * Description: read system revision from eeprom
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000123 */
Igor Grinberg3394be82013-09-16 21:49:58 +0300124u32 cl_eeprom_get_board_rev(void)
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000125{
Nikita Kiryanov15f26a72012-05-24 04:01:23 +0000126 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000127 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000128
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200129 if (board_rev)
130 return board_rev;
131
Nikita Kiryanovb2c55472015-01-14 10:42:43 +0200132 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000133 return 0;
134
Igor Grinberg3394be82013-09-16 21:49:58 +0300135 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000136 offset = BOARD_REV_OFFSET;
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000137
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200138 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000139 return 0;
140
Nikita Kiryanov15f26a72012-05-24 04:01:23 +0000141 /*
142 * Convert legacy syntactic representation to semantic
143 * representation. i.e. for rev 1.00: 0x100 --> 0x64
144 */
Igor Grinberg3394be82013-09-16 21:49:58 +0300145 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200146 sprintf(str, "%x", board_rev);
147 board_rev = simple_strtoul(str, NULL, 10);
Nikita Kiryanov15f26a72012-05-24 04:01:23 +0000148 }
149
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200150 return board_rev;
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000151};