blob: 630446820cc5fc3bbff02f1194f0c5eb671da5ec [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>
Nikita Kiryanov53af8772015-09-06 11:48:37 +030012#include "eeprom.h"
Nikita Kiryanov82309252012-01-12 03:26:30 +000013
Igor Grinbergd3f041c2014-05-15 11:25:09 +030014#ifndef CONFIG_SYS_I2C_EEPROM_ADDR
15# define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
16# define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
17#endif
18
Nikita Kiryanov7d2f6692014-09-17 15:59:25 +030019#ifndef CONFIG_SYS_I2C_EEPROM_BUS
20#define CONFIG_SYS_I2C_EEPROM_BUS 0
21#endif
22
Nikita Kiryanov82309252012-01-12 03:26:30 +000023#define EEPROM_LAYOUT_VER_OFFSET 44
24#define BOARD_SERIAL_OFFSET 20
25#define BOARD_SERIAL_OFFSET_LEGACY 8
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000026#define BOARD_REV_OFFSET 0
27#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +000028#define BOARD_REV_SIZE 2
Nikita Kiryanov53af8772015-09-06 11:48:37 +030029#define PRODUCT_NAME_OFFSET 128
30#define PRODUCT_NAME_SIZE 16
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000031#define MAC_ADDR_OFFSET 4
32#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanov82309252012-01-12 03:26:30 +000033
34#define LAYOUT_INVALID 0
35#define LAYOUT_LEGACY 0xff
36
Nikita Kiryanove7a24472015-01-14 10:42:43 +020037static int cl_eeprom_bus;
Igor Grinberg689be5f2013-09-16 21:49:58 +030038static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanov82309252012-01-12 03:26:30 +000039
Igor Grinberg689be5f2013-09-16 21:49:58 +030040static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanov82309252012-01-12 03:26:30 +000041{
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030042 int res;
43 unsigned int current_i2c_bus = i2c_get_bus_num();
44
Nikita Kiryanove7a24472015-01-14 10:42:43 +020045 res = i2c_set_bus_num(cl_eeprom_bus);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030046 if (res < 0)
47 return res;
48
49 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
Nikita Kiryanov82309252012-01-12 03:26:30 +000050 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030051
52 i2c_set_bus_num(current_i2c_bus);
53
54 return res;
Nikita Kiryanov82309252012-01-12 03:26:30 +000055}
56
Nikita Kiryanove7a24472015-01-14 10:42:43 +020057static int cl_eeprom_setup(uint eeprom_bus)
Nikita Kiryanov82309252012-01-12 03:26:30 +000058{
59 int res;
60
Nikita Kiryanove7a24472015-01-14 10:42:43 +020061 /*
62 * We know the setup was already done when the layout is set to a valid
63 * value and we're using the same bus as before.
64 */
65 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
Nikita Kiryanov82309252012-01-12 03:26:30 +000066 return 0;
67
Nikita Kiryanove7a24472015-01-14 10:42:43 +020068 cl_eeprom_bus = eeprom_bus;
Igor Grinberg689be5f2013-09-16 21:49:58 +030069 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
70 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanov82309252012-01-12 03:26:30 +000071 if (res) {
Igor Grinberg689be5f2013-09-16 21:49:58 +030072 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanov82309252012-01-12 03:26:30 +000073 return res;
74 }
75
Igor Grinberg689be5f2013-09-16 21:49:58 +030076 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
77 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanov82309252012-01-12 03:26:30 +000078
79 return 0;
80}
81
82void get_board_serial(struct tag_serialnr *serialnr)
83{
84 u32 serial[2];
85 uint offset;
86
87 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg689be5f2013-09-16 21:49:58 +030088
Nikita Kiryanove7a24472015-01-14 10:42:43 +020089 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
Nikita Kiryanov82309252012-01-12 03:26:30 +000090 return;
91
Igor Grinberg689be5f2013-09-16 21:49:58 +030092 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
93 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
94
95 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanov82309252012-01-12 03:26:30 +000096 return;
97
98 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
99 serialnr->low = serial[0];
100 serialnr->high = serial[1];
101 }
102}
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000103
104/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300105 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000106 * Description: read mac address and store it in buf.
107 */
Nikita Kiryanove7a24472015-01-14 10:42:43 +0200108int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000109{
110 uint offset;
Nikita Kiryanove93e8092015-09-06 11:48:36 +0300111 int err;
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000112
Nikita Kiryanove93e8092015-09-06 11:48:36 +0300113 err = cl_eeprom_setup(eeprom_bus);
114 if (err)
115 return err;
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000116
Igor Grinberg689be5f2013-09-16 21:49:58 +0300117 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000118 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg689be5f2013-09-16 21:49:58 +0300119
120 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000121}
122
Igor Grinberga937fd12014-11-03 11:32:18 +0200123static u32 board_rev;
124
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000125/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300126 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +0000127 * Description: read system revision from eeprom
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000128 */
Nikita Kiryanov72898ac2015-09-06 11:48:35 +0300129u32 cl_eeprom_get_board_rev(uint eeprom_bus)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000130{
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000131 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000132 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000133
Igor Grinberga937fd12014-11-03 11:32:18 +0200134 if (board_rev)
135 return board_rev;
136
Nikita Kiryanov72898ac2015-09-06 11:48:35 +0300137 if (cl_eeprom_setup(eeprom_bus))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000138 return 0;
139
Igor Grinberg689be5f2013-09-16 21:49:58 +0300140 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000141 offset = BOARD_REV_OFFSET;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000142
Igor Grinberga937fd12014-11-03 11:32:18 +0200143 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000144 return 0;
145
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000146 /*
147 * Convert legacy syntactic representation to semantic
148 * representation. i.e. for rev 1.00: 0x100 --> 0x64
149 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300150 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Igor Grinberga937fd12014-11-03 11:32:18 +0200151 sprintf(str, "%x", board_rev);
152 board_rev = simple_strtoul(str, NULL, 10);
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000153 }
154
Igor Grinberga937fd12014-11-03 11:32:18 +0200155 return board_rev;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000156};
Nikita Kiryanov53af8772015-09-06 11:48:37 +0300157
158/*
159 * Routine: cl_eeprom_get_board_rev
160 * Description: read system revision from eeprom
161 *
162 * @buf: buffer to store the product name
163 * @eeprom_bus: i2c bus num of the eeprom
164 *
165 * @return: 0 on success, < 0 on failure
166 */
167int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
168{
169 int err;
170
171 if (buf == NULL)
172 return -EINVAL;
173
174 err = cl_eeprom_setup(eeprom_bus);
175 if (err)
176 return err;
177
178 err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE);
179 if (!err) /* Protect ourselves from invalid data (unterminated str) */
180 buf[PRODUCT_NAME_SIZE - 1] = '\0';
181
182 return err;
183}