blob: df91acd4a7c95cf2090536ed787fedb3782906da [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
25static int eeprom_layout; /* Implicitly LAYOUT_INVALID */
26
27static int cm_t3x_eeprom_read(uint offset, uchar *buf, int len)
28{
29 return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
30 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
31}
32
33static int eeprom_setup_layout(void)
34{
35 int res;
36
37 if (eeprom_layout != LAYOUT_INVALID)
38 return 0;
39
40 res = cm_t3x_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
41 (uchar *)&eeprom_layout, 1);
42 if (res) {
43 eeprom_layout = LAYOUT_INVALID;
44 return res;
45 }
46
47 if (eeprom_layout == 0 || eeprom_layout >= 0x20)
48 eeprom_layout = LAYOUT_LEGACY;
49
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));
59 if (eeprom_setup_layout())
60 return;
61
62 offset = (eeprom_layout != LAYOUT_LEGACY) ?
63 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
64 if (cm_t3x_eeprom_read(offset, (uchar *)serial, 8))
65 return;
66
67 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
68 serialnr->low = serial[0];
69 serialnr->high = serial[1];
70 }
71}
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000072
73/*
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000074 * Routine: cm_t3x_eeprom_read_mac_addr
75 * Description: read mac address and store it in buf.
76 */
77int cm_t3x_eeprom_read_mac_addr(uchar *buf)
78{
79 uint offset;
80
81 if (eeprom_setup_layout())
82 return 0;
83
84 offset = (eeprom_layout != LAYOUT_LEGACY) ?
85 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
86 return cm_t3x_eeprom_read(offset, buf, 6);
87}
88
89/*
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +000090 * Routine: cm_t3x_eeprom_get_board_rev
91 * Description: read system revision from eeprom
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000092 */
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +000093u32 cm_t3x_eeprom_get_board_rev(void)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000094{
95 u32 rev = 0;
Nikita Kiryanov2ef63022012-05-24 04:01:23 +000096 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000097 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000098
99 if (eeprom_setup_layout())
100 return 0;
101
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +0000102 if (eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000103 offset = BOARD_REV_OFFSET;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000104
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +0000105 if (cm_t3x_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000106 return 0;
107
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000108 /*
109 * Convert legacy syntactic representation to semantic
110 * representation. i.e. for rev 1.00: 0x100 --> 0x64
111 */
112 if (eeprom_layout == LAYOUT_LEGACY) {
113 sprintf(str, "%x", rev);
114 rev = simple_strtoul(str, NULL, 10);
115 }
116
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000117 return rev;
118};