blob: be0fda06381e34233b146a882b5c681fdb12359b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haiying Wangbea3f282006-07-12 10:48:05 -04002/*
Timur Tabif098c9c2011-02-09 02:00:09 +00003 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
Haiying Wangbea3f282006-07-12 10:48:05 -04004 * York Sun (yorksun@freescale.com)
5 * Haiying Wang (haiying.wang@freescale.com)
Timur Tabie2d31fb2008-06-19 17:56:11 -05006 * Timur Tabi (timur@freescale.com)
Haiying Wangbea3f282006-07-12 10:48:05 -04007 */
8
9#include <common.h>
10#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060011#include <env.h>
Haiying Wangbea3f282006-07-12 10:48:05 -040012#include <i2c.h>
Simon Glass691d7192020-05-10 11:40:02 -060013#include <init.h>
Haiying Wangbea3f282006-07-12 10:48:05 -040014#include <linux/ctype.h>
Simon Glassc05ed002020-05-10 11:40:11 -060015#include <linux/delay.h>
Simon Glass3db71102019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Haiying Wangbea3f282006-07-12 10:48:05 -040017
Timur Tabibfb70712010-09-30 15:36:50 -050018#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -050019#include "../common/eeprom.h"
Timur Tabibfb70712010-09-30 15:36:50 -050020#define MAX_NUM_PORTS 8
Timur Tabie2d31fb2008-06-19 17:56:11 -050021#endif
Haiying Wangbea3f282006-07-12 10:48:05 -040022
Timur Tabibfb70712010-09-30 15:36:50 -050023#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Shengzhou Liu5536aeb2013-09-13 14:46:01 +080024/* some boards with non-256-bytes EEPROM have special define */
25/* for MAX_NUM_PORTS in board-specific file */
26#ifndef MAX_NUM_PORTS
Ebony Zhu477c8942014-04-25 18:38:44 -050027#define MAX_NUM_PORTS 16
Shengzhou Liu5536aeb2013-09-13 14:46:01 +080028#endif
Timur Tabibfb70712010-09-30 15:36:50 -050029#define NXID_VERSION 1
30#endif
Haiying Wang9a611082009-06-04 16:12:40 -040031
Timur Tabie2d31fb2008-06-19 17:56:11 -050032/**
33 * static eeprom: EEPROM layout for CCID or NXID formats
34 *
35 * See application note AN3638 for details.
36 */
37static struct __attribute__ ((__packed__)) eeprom {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -050039 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
40 u8 major; /* 0x04 Board revision, major */
41 u8 minor; /* 0x05 Board revision, minor */
42 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
43 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
44 u8 date[6]; /* 0x12 - 0x17 Build Date */
45 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
46 u8 mac_count; /* 0x40 Number of MAC addresses */
47 u8 mac_flag; /* 0x41 MAC table flags */
Haiying Wang9a611082009-06-04 16:12:40 -040048 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
Timur Tabie2d31fb2008-06-19 17:56:11 -050049 u32 crc; /* 0x72 CRC32 checksum */
50#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -050052 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
53 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
54 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
55 u8 date[6]; /* 0x15 - 0x1a Build Date */
56 u8 res_0; /* 0x1b Reserved */
57 u32 version; /* 0x1c - 0x1f NXID Version */
58 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
59 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
60 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
61 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
62 u8 mac_count; /* 0x40 Number of MAC addresses */
63 u8 mac_flag; /* 0x41 MAC table flags */
Ebony Zhu477c8942014-04-25 18:38:44 -050064 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0xa1 MAC addresses */
65 u8 res_2[90]; /* 0xa2 - 0xfb Reserved */
66 u32 crc; /* 0xfc - 0xff CRC32 checksum */
Timur Tabie2d31fb2008-06-19 17:56:11 -050067#endif
68} e;
69
70/* Set to 1 if we've read EEPROM into memory */
71static int has_been_read = 0;
72
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -050074/* Is this a valid NXID EEPROM? */
Kumar Galaafb0b1312009-07-03 12:45:44 -050075#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
76 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabie2d31fb2008-06-19 17:56:11 -050077#endif
78
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -050080/* Is this a valid CCID EEPROM? */
Kumar Galaafb0b1312009-07-03 12:45:44 -050081#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
82 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabie2d31fb2008-06-19 17:56:11 -050083#endif
84
85/**
86 * show_eeprom - display the contents of the EEPROM
87 */
88static void show_eeprom(void)
Haiying Wangbea3f282006-07-12 10:48:05 -040089{
90 int i;
Timur Tabie2d31fb2008-06-19 17:56:11 -050091 unsigned int crc;
Haiying Wangbea3f282006-07-12 10:48:05 -040092
Timur Tabie2d31fb2008-06-19 17:56:11 -050093 /* EEPROM tag ID, either CCID or NXID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020094#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -050095 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
Jaiprakash Singhb8baf462015-05-28 14:54:03 +053096 be32_to_cpu(e.version));
Timur Tabie2d31fb2008-06-19 17:56:11 -050097#else
98 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
99#endif
Haiying Wangd59feff2008-01-16 17:12:12 -0500100
Timur Tabie2d31fb2008-06-19 17:56:11 -0500101 /* Serial number */
102 printf("SN: %s\n", e.sn);
Haiying Wangd59feff2008-01-16 17:12:12 -0500103
Timur Tabie2d31fb2008-06-19 17:56:11 -0500104 /* Errata level. */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500106 printf("Errata: %s\n", e.errata);
107#else
108 printf("Errata: %c%c\n",
109 e.errata[0] ? e.errata[0] : '.',
110 e.errata[1] ? e.errata[1] : '.');
111#endif
Haiying Wangd59feff2008-01-16 17:12:12 -0500112
Timur Tabie2d31fb2008-06-19 17:56:11 -0500113 /* Build date, BCD date values, as YYMMDDhhmmss */
114 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
115 e.date[0], e.date[1], e.date[2],
116 e.date[3] & 0x7F, e.date[4], e.date[5],
117 e.date[3] & 0x80 ? "PM" : "");
Haiying Wangd59feff2008-01-16 17:12:12 -0500118
Timur Tabie2d31fb2008-06-19 17:56:11 -0500119 /* Show MAC addresses */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900120 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
Haiying Wang9a611082009-06-04 16:12:40 -0400121
Timur Tabie2d31fb2008-06-19 17:56:11 -0500122 u8 *p = e.mac[i];
123
124 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
125 p[0], p[1], p[2], p[3], p[4], p[5]);
126 }
127
128 crc = crc32(0, (void *)&e, sizeof(e) - 4);
129
130 if (crc == be32_to_cpu(e.crc))
131 printf("CRC: %08x\n", be32_to_cpu(e.crc));
Haiying Wangd59feff2008-01-16 17:12:12 -0500132 else
Timur Tabie2d31fb2008-06-19 17:56:11 -0500133 printf("CRC: %08x (should be %08x)\n",
134 be32_to_cpu(e.crc), crc);
Haiying Wangd59feff2008-01-16 17:12:12 -0500135
Timur Tabie2d31fb2008-06-19 17:56:11 -0500136#ifdef DEBUG
137 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
138 for (i = 0; i < sizeof(e); i++) {
139 if ((i % 16) == 0)
140 printf("%02X: ", i);
141 printf("%02X ", ((u8 *)&e)[i]);
142 if (((i % 16) == 15) || (i == sizeof(e) - 1))
143 printf("\n");
Haiying Wangd59feff2008-01-16 17:12:12 -0500144 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500145#endif
Haiying Wangbea3f282006-07-12 10:48:05 -0400146}
147
Timur Tabie2d31fb2008-06-19 17:56:11 -0500148/**
149 * read_eeprom - read the EEPROM into memory
150 */
151static int read_eeprom(void)
Haiying Wangbea3f282006-07-12 10:48:05 -0400152{
Timur Tabie2d31fb2008-06-19 17:56:11 -0500153 int ret;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniuk2147a162021-02-09 13:52:45 +0200155#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabie2d31fb2008-06-19 17:56:11 -0500156 unsigned int bus;
157#endif
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800158#endif
Haiying Wangbea3f282006-07-12 10:48:05 -0400159
Timur Tabie2d31fb2008-06-19 17:56:11 -0500160 if (has_been_read)
161 return 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400162
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200163#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniuk2147a162021-02-09 13:52:45 +0200164#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabie2d31fb2008-06-19 17:56:11 -0500165 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200166 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500167#endif
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800168#endif
Timur Tabie2d31fb2008-06-19 17:56:11 -0500169
Igor Opaniuk2147a162021-02-09 13:52:45 +0200170#if !CONFIG_IS_ENABLED(DM_I2C)
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800171 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
172 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
173 (void *)&e, sizeof(e));
174#else
175 struct udevice *dev;
176#ifdef CONFIG_SYS_EEPROM_BUS_NUM
177 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
Biwen Lie343ae72020-10-26 15:14:05 +0800178 CONFIG_SYS_I2C_EEPROM_ADDR,
179 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800180#else
Biwen Lie343ae72020-10-26 15:14:05 +0800181 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
182 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800183#endif
184 if (!ret)
185 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
186#endif
Timur Tabie2d31fb2008-06-19 17:56:11 -0500187
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200188#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniuk2147a162021-02-09 13:52:45 +0200189#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabie2d31fb2008-06-19 17:56:11 -0500190 i2c_set_bus_num(bus);
191#endif
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800192#endif
Timur Tabie2d31fb2008-06-19 17:56:11 -0500193
194#ifdef DEBUG
195 show_eeprom();
196#endif
197
198 has_been_read = (ret == 0) ? 1 : 0;
199
200 return ret;
Haiying Wangbea3f282006-07-12 10:48:05 -0400201}
202
Timur Tabie2d31fb2008-06-19 17:56:11 -0500203/**
Timur Tabi2d04db02009-08-28 16:56:45 -0500204 * update_crc - update the CRC
205 *
206 * This function should be called after each update to the EEPROM structure,
207 * to make sure the CRC is always correct.
208 */
209static void update_crc(void)
210{
211 u32 crc;
212
213 crc = crc32(0, (void *)&e, sizeof(e) - 4);
214 e.crc = cpu_to_be32(crc);
215}
216
217/**
Timur Tabie2d31fb2008-06-19 17:56:11 -0500218 * prog_eeprom - write the EEPROM from memory
219 */
220static int prog_eeprom(void)
Haiying Wangbea3f282006-07-12 10:48:05 -0400221{
Timur Tabi3addcb92010-08-02 13:03:23 -0500222 int ret = 0;
Anton Vorontsov9c671e72009-09-02 02:17:24 +0400223 int i;
Timur Tabie2d31fb2008-06-19 17:56:11 -0500224 void *p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200225#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniuk2147a162021-02-09 13:52:45 +0200226#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabie2d31fb2008-06-19 17:56:11 -0500227 unsigned int bus;
228#endif
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800229#endif
Haiying Wangbea3f282006-07-12 10:48:05 -0400230
Timur Tabie2d31fb2008-06-19 17:56:11 -0500231 /* Set the reserved values to 0xFF */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200232#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500233 e.res_0 = 0xFF;
234 memset(e.res_1, 0xFF, sizeof(e.res_1));
235#else
236 memset(e.res_0, 0xFF, sizeof(e.res_0));
237#endif
Timur Tabi2d04db02009-08-28 16:56:45 -0500238 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500239
Igor Opaniuk2147a162021-02-09 13:52:45 +0200240#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200241#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500242 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200243 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500244#endif
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800245#endif
Timur Tabie2d31fb2008-06-19 17:56:11 -0500246
Timur Tabi3addcb92010-08-02 13:03:23 -0500247 /*
248 * The AT24C02 datasheet says that data can only be written in page
249 * mode, which means 8 bytes at a time, and it takes up to 5ms to
250 * complete a given write.
251 */
Timur Tabi2d04db02009-08-28 16:56:45 -0500252 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
Igor Opaniuk2147a162021-02-09 13:52:45 +0200253#if !CONFIG_IS_ENABLED(DM_I2C)
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800254 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
255 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Masahiro Yamadab4141192014-11-07 03:03:31 +0900256 p, min((int)(sizeof(e) - i), 8));
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800257#else
258 struct udevice *dev;
259#ifdef CONFIG_SYS_EEPROM_BUS_NUM
260 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
261 CONFIG_SYS_I2C_EEPROM_ADDR,
262 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
263 &dev);
264#else
265 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
266 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
267 &dev);
268#endif
269 if (!ret)
270 ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
271 8));
272#endif
Haiying Wangbea3f282006-07-12 10:48:05 -0400273 if (ret)
274 break;
Timur Tabie2d31fb2008-06-19 17:56:11 -0500275 udelay(5000); /* 5ms write cycle timing */
Haiying Wangbea3f282006-07-12 10:48:05 -0400276 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500277
Timur Tabi3addcb92010-08-02 13:03:23 -0500278 if (!ret) {
279 /* Verify the write by reading back the EEPROM and comparing */
280 struct eeprom e2;
281
Igor Opaniuk2147a162021-02-09 13:52:45 +0200282#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabi3addcb92010-08-02 13:03:23 -0500283 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800284 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
285 (void *)&e2, sizeof(e2));
286#else
287 struct udevice *dev;
288#ifdef CONFIG_SYS_EEPROM_BUS_NUM
289 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
290 CONFIG_SYS_I2C_EEPROM_ADDR,
291 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
292 &dev);
293#else
294 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
295 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
296 &dev);
297#endif
298 if (!ret)
299 ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
300#endif
Timur Tabi3addcb92010-08-02 13:03:23 -0500301 if (!ret && memcmp(&e, &e2, sizeof(e)))
302 ret = -1;
303 }
304
Igor Opaniuk2147a162021-02-09 13:52:45 +0200305#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200306#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500307 i2c_set_bus_num(bus);
308#endif
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800309#endif
Timur Tabie2d31fb2008-06-19 17:56:11 -0500310
Haiying Wangbea3f282006-07-12 10:48:05 -0400311 if (ret) {
312 printf("Programming failed.\n");
Timur Tabi3addcb92010-08-02 13:03:23 -0500313 has_been_read = 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400314 return -1;
Haiying Wangbea3f282006-07-12 10:48:05 -0400315 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500316
317 printf("Programming passed.\n");
Haiying Wangbea3f282006-07-12 10:48:05 -0400318 return 0;
319}
320
Timur Tabie2d31fb2008-06-19 17:56:11 -0500321/**
322 * h2i - converts hex character into a number
323 *
324 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
325 * the integer equivalent.
326 */
327static inline u8 h2i(char p)
328{
329 if ((p >= '0') && (p <= '9'))
330 return p - '0';
331
332 if ((p >= 'A') && (p <= 'F'))
333 return (p - 'A') + 10;
334
335 if ((p >= 'a') && (p <= 'f'))
336 return (p - 'a') + 10;
337
338 return 0;
339}
340
341/**
342 * set_date - stores the build date into the EEPROM
343 *
344 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
345 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
346 * and stores it in the build date field of the EEPROM local copy.
347 */
348static void set_date(const char *string)
349{
350 unsigned int i;
351
352 if (strlen(string) != 12) {
353 printf("Usage: mac date YYMMDDhhmmss\n");
354 return;
355 }
356
357 for (i = 0; i < 6; i++)
358 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
Timur Tabi2d04db02009-08-28 16:56:45 -0500359
360 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500361}
362
363/**
364 * set_mac_address - stores a MAC address into the EEPROM
365 *
366 * This function takes a pointer to MAC address string
367 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
368 * stores it in one of the MAC address fields of the EEPROM local copy.
369 */
370static void set_mac_address(unsigned int index, const char *string)
371{
372 char *p = (char *) string;
373 unsigned int i;
374
Timur Tabibfb70712010-09-30 15:36:50 -0500375 if ((index >= MAX_NUM_PORTS) || !string) {
Timur Tabie2d31fb2008-06-19 17:56:11 -0500376 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
377 return;
378 }
379
380 for (i = 0; *p && (i < 6); i++) {
381 e.mac[index][i] = simple_strtoul(p, &p, 16);
382 if (*p == ':')
383 p++;
384 }
Timur Tabi2d04db02009-08-28 16:56:45 -0500385
386 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500387}
388
Simon Glass09140112020-05-10 11:40:03 -0600389int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Haiying Wangbea3f282006-07-12 10:48:05 -0400390{
Timur Tabie2d31fb2008-06-19 17:56:11 -0500391 char cmd;
Haiying Wangbea3f282006-07-12 10:48:05 -0400392
Timur Tabie2d31fb2008-06-19 17:56:11 -0500393 if (argc == 1) {
394 show_eeprom();
395 return 0;
396 }
Haiying Wangbea3f282006-07-12 10:48:05 -0400397
Timur Tabie2d31fb2008-06-19 17:56:11 -0500398 cmd = argv[1][0];
399
400 if (cmd == 'r') {
401 read_eeprom();
402 return 0;
403 }
404
Timur Tabi2d04db02009-08-28 16:56:45 -0500405 if (cmd == 'i') {
406#ifdef CONFIG_SYS_I2C_EEPROM_NXID
407 memcpy(e.id, "NXID", sizeof(e.id));
Jaiprakash Singhb8baf462015-05-28 14:54:03 +0530408 e.version = cpu_to_be32(NXID_VERSION);
Timur Tabi2d04db02009-08-28 16:56:45 -0500409#else
410 memcpy(e.id, "CCID", sizeof(e.id));
411#endif
Timur Tabi3fee3342011-02-09 13:40:51 -0600412 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500413 return 0;
414 }
415
416 if (!is_valid) {
417 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
418 return 0;
419 }
420
421 if (argc == 2) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400422 switch (cmd) {
Jon Loeliger80e955c2006-08-22 12:25:27 -0500423 case 's': /* save */
Timur Tabie2d31fb2008-06-19 17:56:11 -0500424 prog_eeprom();
Jon Loeliger80e955c2006-08-22 12:25:27 -0500425 break;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500426 default:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200427 return cmd_usage(cmdtp);
Haiying Wangbea3f282006-07-12 10:48:05 -0400428 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500429
430 return 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400431 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500432
433 /* We know we have at least one parameter */
434
435 switch (cmd) {
436 case 'n': /* serial number */
437 memset(e.sn, 0, sizeof(e.sn));
438 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
Timur Tabi2d04db02009-08-28 16:56:45 -0500439 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500440 break;
441 case 'e': /* errata */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200442#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500443 memset(e.errata, 0, 5);
444 strncpy((char *)e.errata, argv[2], 4);
445#else
446 e.errata[0] = argv[2][0];
447 e.errata[1] = argv[2][1];
448#endif
Timur Tabi2d04db02009-08-28 16:56:45 -0500449 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500450 break;
451 case 'd': /* date BCD format YYMMDDhhmmss */
452 set_date(argv[2]);
453 break;
454 case 'p': /* MAC table size */
455 e.mac_count = simple_strtoul(argv[2], NULL, 16);
Timur Tabi2d04db02009-08-28 16:56:45 -0500456 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500457 break;
Timur Tabibfb70712010-09-30 15:36:50 -0500458 case '0' ... '9': /* "mac 0" through "mac 22" */
459 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500460 break;
461 case 'h': /* help */
462 default:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200463 return cmd_usage(cmdtp);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500464 }
465
Haiying Wangbea3f282006-07-12 10:48:05 -0400466 return 0;
467}
468
Timur Tabie2d31fb2008-06-19 17:56:11 -0500469/**
470 * mac_read_from_eeprom - read the MAC addresses from EEPROM
471 *
472 * This function reads the MAC addresses from EEPROM and sets the
473 * appropriate environment variables for each one read.
474 *
475 * The environment variables are only set if they haven't been set already.
476 * This ensures that any user-saved variables are never overwritten.
477 *
478 * This function must be called after relocation.
Timur Tabif098c9c2011-02-09 02:00:09 +0000479 *
480 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
481 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
482 * located at a different offset.
Timur Tabie2d31fb2008-06-19 17:56:11 -0500483 */
Haiying Wangbea3f282006-07-12 10:48:05 -0400484int mac_read_from_eeprom(void)
485{
Timur Tabie2d31fb2008-06-19 17:56:11 -0500486 unsigned int i;
Timur Tabif098c9c2011-02-09 02:00:09 +0000487 u32 crc, crc_offset = offsetof(struct eeprom, crc);
488 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
Timur Tabi2d04db02009-08-28 16:56:45 -0500489
490 puts("EEPROM: ");
Haiying Wangbea3f282006-07-12 10:48:05 -0400491
Timur Tabie2d31fb2008-06-19 17:56:11 -0500492 if (read_eeprom()) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400493 printf("Read failed.\n");
York Sunbffac7a2014-04-30 14:43:46 -0700494 return 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400495 }
496
Timur Tabie2d31fb2008-06-19 17:56:11 -0500497 if (!is_valid) {
Timur Tabi2d04db02009-08-28 16:56:45 -0500498 printf("Invalid ID (%02x %02x %02x %02x)\n",
499 e.id[0], e.id[1], e.id[2], e.id[3]);
York Sunbffac7a2014-04-30 14:43:46 -0700500 return 0;
Timur Tabie2d31fb2008-06-19 17:56:11 -0500501 }
502
Timur Tabif098c9c2011-02-09 02:00:09 +0000503#ifdef CONFIG_SYS_I2C_EEPROM_NXID
504 /*
505 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
506 * to where it is in v0.
507 */
508 if (e.version == 0)
509 crc_offset = 0x72;
510#endif
511
512 crc = crc32(0, (void *)&e, crc_offset);
513 crcp = (void *)&e + crc_offset;
514 if (crc != be32_to_cpu(*crcp)) {
Timur Tabi2d04db02009-08-28 16:56:45 -0500515 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
York Sunbffac7a2014-04-30 14:43:46 -0700516 return 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400517 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500518
Timur Tabif098c9c2011-02-09 02:00:09 +0000519#ifdef CONFIG_SYS_I2C_EEPROM_NXID
520 /*
521 * MAC address #9 in v1 occupies the same position as the CRC in v0.
522 * Erase it so that it's not mistaken for a MAC address. We'll
523 * update the CRC later.
524 */
525 if (e.version == 0)
526 memset(e.mac[8], 0xff, 6);
527#endif
528
Masahiro Yamadab4141192014-11-07 03:03:31 +0900529 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
Timur Tabie2d31fb2008-06-19 17:56:11 -0500530 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
531 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
532 char ethaddr[18];
533 char enetvar[9];
534
535 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
536 e.mac[i][0],
537 e.mac[i][1],
538 e.mac[i][2],
539 e.mac[i][3],
540 e.mac[i][4],
541 e.mac[i][5]);
542 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
543 /* Only initialize environment variables that are blank
544 * (i.e. have not yet been set)
545 */
Simon Glass00caae62017-08-03 12:22:12 -0600546 if (!env_get(enetvar))
Simon Glass382bee52017-08-03 12:22:09 -0600547 env_set(enetvar, ethaddr);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500548 }
549 }
550
Timur Tabi2d04db02009-08-28 16:56:45 -0500551#ifdef CONFIG_SYS_I2C_EEPROM_NXID
552 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
Jaiprakash Singhb8baf462015-05-28 14:54:03 +0530553 be32_to_cpu(e.version));
Timur Tabi2d04db02009-08-28 16:56:45 -0500554#else
555 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
556#endif
557
Timur Tabif098c9c2011-02-09 02:00:09 +0000558#ifdef CONFIG_SYS_I2C_EEPROM_NXID
559 /*
560 * Now we need to upconvert the data into v1 format. We do this last so
561 * that at boot time, U-Boot will still say "NXID v0".
562 */
563 if (e.version == 0) {
Jaiprakash Singhb8baf462015-05-28 14:54:03 +0530564 e.version = cpu_to_be32(NXID_VERSION);
Timur Tabif098c9c2011-02-09 02:00:09 +0000565 update_crc();
566 }
567#endif
568
Haiying Wangbea3f282006-07-12 10:48:05 -0400569 return 0;
570}
Timur Tabie2d31fb2008-06-19 17:56:11 -0500571
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200572#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500573
574/**
575 * get_cpu_board_revision - get the CPU board revision on 85xx boards
576 *
577 * Read the EEPROM to determine the board revision.
578 *
579 * This function is called before relocation, so we need to read a private
580 * copy of the EEPROM into a local variable on the stack.
581 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200582 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
583 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
Timur Tabie2d31fb2008-06-19 17:56:11 -0500584 * so that the SPD code will work. This means that all pre-relocation I2C
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200585 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
586 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
Timur Tabie2d31fb2008-06-19 17:56:11 -0500587 * this function is called. Oh well.
588 */
589unsigned int get_cpu_board_revision(void)
590{
591 struct board_eeprom {
592 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
593 u8 major; /* 0x04 Board revision, major */
594 u8 minor; /* 0x05 Board revision, minor */
595 } be;
596
Igor Opaniuk2147a162021-02-09 13:52:45 +0200597#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200598 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabie2d31fb2008-06-19 17:56:11 -0500599 (void *)&be, sizeof(be));
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800600#else
601 struct udevice *dev;
Biwen Li01d97d52020-05-01 20:56:37 +0800602 int ret;
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800603#ifdef CONFIG_SYS_EEPROM_BUS_NUM
604 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
605 CONFIG_SYS_I2C_EEPROM_ADDR,
606 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
607 &dev);
608#else
609 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
610 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Biwen Li01d97d52020-05-01 20:56:37 +0800611 &dev);
Chuanhua Han0eba65d2019-07-10 21:00:20 +0800612#endif
613 if (!ret)
614 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
615#endif
Timur Tabie2d31fb2008-06-19 17:56:11 -0500616
617 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
618 return MPC85XX_CPU_BOARD_REV(0, 0);
619
620 if ((be.major == 0xff) && (be.minor == 0xff))
621 return MPC85XX_CPU_BOARD_REV(0, 0);
622
Rafal Czubake46c7bf2008-10-08 13:41:30 +0200623 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500624}
Timur Tabie2d31fb2008-06-19 17:56:11 -0500625#endif