blob: ec3c9e37222a550666fbf8ed2f1ac4a8c274d261 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haiying Wang34c68782006-07-12 10:48:05 -04002/*
Timur Tabia2b41cb2011-02-09 02:00:09 +00003 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
Haiying Wang34c68782006-07-12 10:48:05 -04004 * York Sun (yorksun@freescale.com)
5 * Haiying Wang (haiying.wang@freescale.com)
Timur Tabibdea8372008-06-19 17:56:11 -05006 * Timur Tabi (timur@freescale.com)
Haiying Wang34c68782006-07-12 10:48:05 -04007 */
8
Haiying Wang34c68782006-07-12 10:48:05 -04009#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060010#include <env.h>
Haiying Wang34c68782006-07-12 10:48:05 -040011#include <i2c.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Haiying Wang34c68782006-07-12 10:48:05 -040013#include <linux/ctype.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070015#include <u-boot/crc.h>
Haiying Wang34c68782006-07-12 10:48:05 -040016
Timur Tabi11e98e72010-09-30 15:36:50 -050017#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050018#include "../common/eeprom.h"
Timur Tabi11e98e72010-09-30 15:36:50 -050019#define MAX_NUM_PORTS 8
20#endif
Haiying Wang34c68782006-07-12 10:48:05 -040021
Timur Tabi11e98e72010-09-30 15:36:50 -050022#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Shengzhou Liu2fa20052013-09-13 14:46:01 +080023/* some boards with non-256-bytes EEPROM have special define */
24/* for MAX_NUM_PORTS in board-specific file */
25#ifndef MAX_NUM_PORTS
Ebony Zhu575cc912014-04-25 18:38:44 -050026#define MAX_NUM_PORTS 16
Shengzhou Liu2fa20052013-09-13 14:46:01 +080027#endif
Timur Tabi11e98e72010-09-30 15:36:50 -050028#define NXID_VERSION 1
29#endif
Haiying Wange5607542009-06-04 16:12:40 -040030
Timur Tabibdea8372008-06-19 17:56:11 -050031/**
32 * static eeprom: EEPROM layout for CCID or NXID formats
33 *
34 * See application note AN3638 for details.
35 */
36static struct __attribute__ ((__packed__)) eeprom {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020037#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050038 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
39 u8 major; /* 0x04 Board revision, major */
40 u8 minor; /* 0x05 Board revision, minor */
41 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
42 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
43 u8 date[6]; /* 0x12 - 0x17 Build Date */
44 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
45 u8 mac_count; /* 0x40 Number of MAC addresses */
46 u8 mac_flag; /* 0x41 MAC table flags */
Haiying Wange5607542009-06-04 16:12:40 -040047 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
Timur Tabibdea8372008-06-19 17:56:11 -050048 u32 crc; /* 0x72 CRC32 checksum */
49#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020050#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050051 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
52 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
53 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
54 u8 date[6]; /* 0x15 - 0x1a Build Date */
55 u8 res_0; /* 0x1b Reserved */
56 u32 version; /* 0x1c - 0x1f NXID Version */
57 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
58 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
59 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
60 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
61 u8 mac_count; /* 0x40 Number of MAC addresses */
62 u8 mac_flag; /* 0x41 MAC table flags */
Ebony Zhu575cc912014-04-25 18:38:44 -050063 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0xa1 MAC addresses */
Wolfgang Denk9d328a62021-09-27 17:42:38 +020064 u8 res_2[90]; /* 0xa2 - 0xfb Reserved */
Ebony Zhu575cc912014-04-25 18:38:44 -050065 u32 crc; /* 0xfc - 0xff CRC32 checksum */
Timur Tabibdea8372008-06-19 17:56:11 -050066#endif
67} e;
68
69/* Set to 1 if we've read EEPROM into memory */
70static int has_been_read = 0;
71
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020072#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050073/* Is this a valid NXID EEPROM? */
Kumar Gala4dc62452009-07-03 12:45:44 -050074#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
75 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabibdea8372008-06-19 17:56:11 -050076#endif
77
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020078#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050079/* Is this a valid CCID EEPROM? */
Kumar Gala4dc62452009-07-03 12:45:44 -050080#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
81 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabibdea8372008-06-19 17:56:11 -050082#endif
83
84/**
85 * show_eeprom - display the contents of the EEPROM
86 */
87static void show_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -040088{
89 int i;
Timur Tabibdea8372008-06-19 17:56:11 -050090 unsigned int crc;
Haiying Wang34c68782006-07-12 10:48:05 -040091
Timur Tabibdea8372008-06-19 17:56:11 -050092 /* EEPROM tag ID, either CCID or NXID */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050094 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +053095 be32_to_cpu(e.version));
Timur Tabibdea8372008-06-19 17:56:11 -050096#else
97 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
98#endif
Haiying Wang34cf9592008-01-16 17:12:12 -050099
Timur Tabibdea8372008-06-19 17:56:11 -0500100 /* Serial number */
101 printf("SN: %s\n", e.sn);
Haiying Wang34cf9592008-01-16 17:12:12 -0500102
Timur Tabibdea8372008-06-19 17:56:11 -0500103 /* Errata level. */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200104#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500105 printf("Errata: %s\n", e.errata);
106#else
107 printf("Errata: %c%c\n",
108 e.errata[0] ? e.errata[0] : '.',
109 e.errata[1] ? e.errata[1] : '.');
110#endif
Haiying Wang34cf9592008-01-16 17:12:12 -0500111
Timur Tabibdea8372008-06-19 17:56:11 -0500112 /* Build date, BCD date values, as YYMMDDhhmmss */
113 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
114 e.date[0], e.date[1], e.date[2],
115 e.date[3] & 0x7F, e.date[4], e.date[5],
116 e.date[3] & 0x80 ? "PM" : "");
Haiying Wang34cf9592008-01-16 17:12:12 -0500117
Timur Tabibdea8372008-06-19 17:56:11 -0500118 /* Show MAC addresses */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900119 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
Haiying Wange5607542009-06-04 16:12:40 -0400120
Timur Tabibdea8372008-06-19 17:56:11 -0500121 u8 *p = e.mac[i];
Haiying Wang34cf9592008-01-16 17:12:12 -0500122
Timur Tabibdea8372008-06-19 17:56:11 -0500123 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
124 p[0], p[1], p[2], p[3], p[4], p[5]);
125 }
Haiying Wang34cf9592008-01-16 17:12:12 -0500126
Timur Tabibdea8372008-06-19 17:56:11 -0500127 crc = crc32(0, (void *)&e, sizeof(e) - 4);
Haiying Wang34c68782006-07-12 10:48:05 -0400128
Timur Tabibdea8372008-06-19 17:56:11 -0500129 if (crc == be32_to_cpu(e.crc))
130 printf("CRC: %08x\n", be32_to_cpu(e.crc));
131 else
132 printf("CRC: %08x (should be %08x)\n",
133 be32_to_cpu(e.crc), crc);
Haiying Wang34c68782006-07-12 10:48:05 -0400134
Timur Tabibdea8372008-06-19 17:56:11 -0500135#ifdef DEBUG
136 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
137 for (i = 0; i < sizeof(e); i++) {
138 if ((i % 16) == 0)
139 printf("%02X: ", i);
140 printf("%02X ", ((u8 *)&e)[i]);
141 if (((i % 16) == 15) || (i == sizeof(e) - 1))
142 printf("\n");
143 }
144#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400145}
146
Timur Tabibdea8372008-06-19 17:56:11 -0500147/**
148 * read_eeprom - read the EEPROM into memory
149 */
150static int read_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -0400151{
Timur Tabibdea8372008-06-19 17:56:11 -0500152 int ret;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200153#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniukf7c91762021-02-09 13:52:45 +0200154#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabibdea8372008-06-19 17:56:11 -0500155 unsigned int bus;
156#endif
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800157#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400158
Timur Tabibdea8372008-06-19 17:56:11 -0500159 if (has_been_read)
160 return 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400161
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200162#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniukf7c91762021-02-09 13:52:45 +0200163#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabibdea8372008-06-19 17:56:11 -0500164 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200165 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabibdea8372008-06-19 17:56:11 -0500166#endif
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800167#endif
Timur Tabibdea8372008-06-19 17:56:11 -0500168
Igor Opaniukf7c91762021-02-09 13:52:45 +0200169#if !CONFIG_IS_ENABLED(DM_I2C)
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800170 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
171 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
172 (void *)&e, sizeof(e));
173#else
174 struct udevice *dev;
175#ifdef CONFIG_SYS_EEPROM_BUS_NUM
176 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
Biwen Lia7534842020-10-26 15:14:05 +0800177 CONFIG_SYS_I2C_EEPROM_ADDR,
178 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800179#else
Biwen Lia7534842020-10-26 15:14:05 +0800180 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
181 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800182#endif
183 if (!ret)
184 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
185#endif
Timur Tabibdea8372008-06-19 17:56:11 -0500186
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200187#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniukf7c91762021-02-09 13:52:45 +0200188#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabibdea8372008-06-19 17:56:11 -0500189 i2c_set_bus_num(bus);
190#endif
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800191#endif
Timur Tabibdea8372008-06-19 17:56:11 -0500192
193#ifdef DEBUG
194 show_eeprom();
195#endif
196
197 has_been_read = (ret == 0) ? 1 : 0;
198
199 return ret;
Haiying Wang34c68782006-07-12 10:48:05 -0400200}
201
Timur Tabibdea8372008-06-19 17:56:11 -0500202/**
Timur Tabi13c312b2009-08-28 16:56:45 -0500203 * update_crc - update the CRC
204 *
205 * This function should be called after each update to the EEPROM structure,
206 * to make sure the CRC is always correct.
207 */
208static void update_crc(void)
209{
210 u32 crc;
211
212 crc = crc32(0, (void *)&e, sizeof(e) - 4);
213 e.crc = cpu_to_be32(crc);
214}
215
216/**
Timur Tabibdea8372008-06-19 17:56:11 -0500217 * prog_eeprom - write the EEPROM from memory
218 */
219static int prog_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -0400220{
Timur Tabia58e8802010-08-02 13:03:23 -0500221 int ret = 0;
Anton Vorontsovc33e3b82009-09-02 02:17:24 +0400222 int i;
Timur Tabibdea8372008-06-19 17:56:11 -0500223 void *p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200224#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Igor Opaniukf7c91762021-02-09 13:52:45 +0200225#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabibdea8372008-06-19 17:56:11 -0500226 unsigned int bus;
227#endif
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800228#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400229
Timur Tabibdea8372008-06-19 17:56:11 -0500230 /* Set the reserved values to 0xFF */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200231#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500232 e.res_0 = 0xFF;
233 memset(e.res_1, 0xFF, sizeof(e.res_1));
234#else
235 memset(e.res_0, 0xFF, sizeof(e.res_0));
236#endif
Timur Tabi13c312b2009-08-28 16:56:45 -0500237 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500238
Igor Opaniukf7c91762021-02-09 13:52:45 +0200239#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200240#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500241 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200242 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabibdea8372008-06-19 17:56:11 -0500243#endif
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800244#endif
Timur Tabibdea8372008-06-19 17:56:11 -0500245
Timur Tabia58e8802010-08-02 13:03:23 -0500246 /*
247 * The AT24C02 datasheet says that data can only be written in page
248 * mode, which means 8 bytes at a time, and it takes up to 5ms to
249 * complete a given write.
250 */
Timur Tabi13c312b2009-08-28 16:56:45 -0500251 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
Igor Opaniukf7c91762021-02-09 13:52:45 +0200252#if !CONFIG_IS_ENABLED(DM_I2C)
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800253 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
254 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Masahiro Yamadadb204642014-11-07 03:03:31 +0900255 p, min((int)(sizeof(e) - i), 8));
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800256#else
257 struct udevice *dev;
258#ifdef CONFIG_SYS_EEPROM_BUS_NUM
259 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
260 CONFIG_SYS_I2C_EEPROM_ADDR,
261 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
262 &dev);
263#else
264 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
265 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
266 &dev);
267#endif
268 if (!ret)
269 ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
270 8));
271#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400272 if (ret)
273 break;
Timur Tabibdea8372008-06-19 17:56:11 -0500274 udelay(5000); /* 5ms write cycle timing */
Haiying Wang34c68782006-07-12 10:48:05 -0400275 }
Timur Tabibdea8372008-06-19 17:56:11 -0500276
Timur Tabia58e8802010-08-02 13:03:23 -0500277 if (!ret) {
278 /* Verify the write by reading back the EEPROM and comparing */
279 struct eeprom e2;
280
Igor Opaniukf7c91762021-02-09 13:52:45 +0200281#if !CONFIG_IS_ENABLED(DM_I2C)
Timur Tabia58e8802010-08-02 13:03:23 -0500282 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800283 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
284 (void *)&e2, sizeof(e2));
285#else
286 struct udevice *dev;
287#ifdef CONFIG_SYS_EEPROM_BUS_NUM
288 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
289 CONFIG_SYS_I2C_EEPROM_ADDR,
290 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
291 &dev);
292#else
293 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
294 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
295 &dev);
296#endif
297 if (!ret)
298 ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
299#endif
Timur Tabia58e8802010-08-02 13:03:23 -0500300 if (!ret && memcmp(&e, &e2, sizeof(e)))
301 ret = -1;
302 }
303
Igor Opaniukf7c91762021-02-09 13:52:45 +0200304#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200305#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500306 i2c_set_bus_num(bus);
307#endif
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800308#endif
Timur Tabibdea8372008-06-19 17:56:11 -0500309
Haiying Wang34c68782006-07-12 10:48:05 -0400310 if (ret) {
311 printf("Programming failed.\n");
Timur Tabia58e8802010-08-02 13:03:23 -0500312 has_been_read = 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400313 return -1;
Haiying Wang34c68782006-07-12 10:48:05 -0400314 }
Timur Tabibdea8372008-06-19 17:56:11 -0500315
316 printf("Programming passed.\n");
Haiying Wang34c68782006-07-12 10:48:05 -0400317 return 0;
318}
319
Timur Tabibdea8372008-06-19 17:56:11 -0500320/**
321 * h2i - converts hex character into a number
322 *
323 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
324 * the integer equivalent.
325 */
326static inline u8 h2i(char p)
327{
328 if ((p >= '0') && (p <= '9'))
329 return p - '0';
330
331 if ((p >= 'A') && (p <= 'F'))
332 return (p - 'A') + 10;
333
334 if ((p >= 'a') && (p <= 'f'))
335 return (p - 'a') + 10;
336
337 return 0;
338}
339
340/**
341 * set_date - stores the build date into the EEPROM
342 *
343 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
344 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
345 * and stores it in the build date field of the EEPROM local copy.
346 */
347static void set_date(const char *string)
348{
349 unsigned int i;
350
351 if (strlen(string) != 12) {
352 printf("Usage: mac date YYMMDDhhmmss\n");
353 return;
354 }
355
356 for (i = 0; i < 6; i++)
357 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
Timur Tabi13c312b2009-08-28 16:56:45 -0500358
359 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500360}
361
362/**
363 * set_mac_address - stores a MAC address into the EEPROM
364 *
365 * This function takes a pointer to MAC address string
366 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
367 * stores it in one of the MAC address fields of the EEPROM local copy.
368 */
369static void set_mac_address(unsigned int index, const char *string)
370{
371 char *p = (char *) string;
372 unsigned int i;
373
Timur Tabi11e98e72010-09-30 15:36:50 -0500374 if ((index >= MAX_NUM_PORTS) || !string) {
Timur Tabibdea8372008-06-19 17:56:11 -0500375 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
376 return;
377 }
378
379 for (i = 0; *p && (i < 6); i++) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600380 e.mac[index][i] = hextoul(p, &p);
Timur Tabibdea8372008-06-19 17:56:11 -0500381 if (*p == ':')
382 p++;
383 }
Timur Tabi13c312b2009-08-28 16:56:45 -0500384
385 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500386}
387
Simon Glassed38aef2020-05-10 11:40:03 -0600388int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Haiying Wang34c68782006-07-12 10:48:05 -0400389{
Timur Tabibdea8372008-06-19 17:56:11 -0500390 char cmd;
Haiying Wang34c68782006-07-12 10:48:05 -0400391
Timur Tabibdea8372008-06-19 17:56:11 -0500392 if (argc == 1) {
393 show_eeprom();
394 return 0;
395 }
396
397 cmd = argv[1][0];
398
399 if (cmd == 'r') {
400 read_eeprom();
401 return 0;
402 }
Haiying Wang34c68782006-07-12 10:48:05 -0400403
Timur Tabi13c312b2009-08-28 16:56:45 -0500404 if (cmd == 'i') {
405#ifdef CONFIG_SYS_I2C_EEPROM_NXID
406 memcpy(e.id, "NXID", sizeof(e.id));
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +0530407 e.version = cpu_to_be32(NXID_VERSION);
Timur Tabi13c312b2009-08-28 16:56:45 -0500408#else
409 memcpy(e.id, "CCID", sizeof(e.id));
410#endif
Timur Tabi2f9f65f2011-02-09 13:40:51 -0600411 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500412 return 0;
413 }
414
415 if (!is_valid) {
416 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
417 return 0;
418 }
419
420 if (argc == 2) {
Haiying Wang34c68782006-07-12 10:48:05 -0400421 switch (cmd) {
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500422 case 's': /* save */
Timur Tabibdea8372008-06-19 17:56:11 -0500423 prog_eeprom();
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500424 break;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500425 default:
Heinrich Schuchardt19011492023-09-30 14:01:43 +0200426 return CMD_RET_USAGE;
Haiying Wang34c68782006-07-12 10:48:05 -0400427 }
Timur Tabibdea8372008-06-19 17:56:11 -0500428
429 return 0;
430 }
431
432 /* We know we have at least one parameter */
433
434 switch (cmd) {
435 case 'n': /* serial number */
436 memset(e.sn, 0, sizeof(e.sn));
437 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
Timur Tabi13c312b2009-08-28 16:56:45 -0500438 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500439 break;
440 case 'e': /* errata */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200441#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500442 memset(e.errata, 0, 5);
443 strncpy((char *)e.errata, argv[2], 4);
444#else
445 e.errata[0] = argv[2][0];
446 e.errata[1] = argv[2][1];
447#endif
Timur Tabi13c312b2009-08-28 16:56:45 -0500448 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500449 break;
450 case 'd': /* date BCD format YYMMDDhhmmss */
451 set_date(argv[2]);
452 break;
453 case 'p': /* MAC table size */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600454 e.mac_count = hextoul(argv[2], NULL);
Timur Tabi13c312b2009-08-28 16:56:45 -0500455 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500456 break;
Timur Tabi11e98e72010-09-30 15:36:50 -0500457 case '0' ... '9': /* "mac 0" through "mac 22" */
Simon Glassff9b9032021-07-24 09:03:30 -0600458 set_mac_address(dectoul(argv[1], NULL), argv[2]);
Timur Tabibdea8372008-06-19 17:56:11 -0500459 break;
460 case 'h': /* help */
461 default:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200462 return cmd_usage(cmdtp);
Haiying Wang34c68782006-07-12 10:48:05 -0400463 }
Timur Tabibdea8372008-06-19 17:56:11 -0500464
Haiying Wang34c68782006-07-12 10:48:05 -0400465 return 0;
466}
467
Timur Tabibdea8372008-06-19 17:56:11 -0500468/**
469 * mac_read_from_eeprom - read the MAC addresses from EEPROM
470 *
471 * This function reads the MAC addresses from EEPROM and sets the
472 * appropriate environment variables for each one read.
473 *
474 * The environment variables are only set if they haven't been set already.
475 * This ensures that any user-saved variables are never overwritten.
476 *
477 * This function must be called after relocation.
Timur Tabia2b41cb2011-02-09 02:00:09 +0000478 *
479 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
480 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
481 * located at a different offset.
Timur Tabibdea8372008-06-19 17:56:11 -0500482 */
Haiying Wang34c68782006-07-12 10:48:05 -0400483int mac_read_from_eeprom(void)
484{
Timur Tabibdea8372008-06-19 17:56:11 -0500485 unsigned int i;
Timur Tabia2b41cb2011-02-09 02:00:09 +0000486 u32 crc, crc_offset = offsetof(struct eeprom, crc);
487 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
Timur Tabi13c312b2009-08-28 16:56:45 -0500488
489 puts("EEPROM: ");
Haiying Wang34c68782006-07-12 10:48:05 -0400490
Timur Tabibdea8372008-06-19 17:56:11 -0500491 if (read_eeprom()) {
Haiying Wang34c68782006-07-12 10:48:05 -0400492 printf("Read failed.\n");
York Sun9215eff2014-04-30 14:43:46 -0700493 return 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400494 }
495
Timur Tabibdea8372008-06-19 17:56:11 -0500496 if (!is_valid) {
Timur Tabi13c312b2009-08-28 16:56:45 -0500497 printf("Invalid ID (%02x %02x %02x %02x)\n",
498 e.id[0], e.id[1], e.id[2], e.id[3]);
York Sun9215eff2014-04-30 14:43:46 -0700499 return 0;
Timur Tabibdea8372008-06-19 17:56:11 -0500500 }
501
Timur Tabia2b41cb2011-02-09 02:00:09 +0000502#ifdef CONFIG_SYS_I2C_EEPROM_NXID
503 /*
504 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
505 * to where it is in v0.
506 */
507 if (e.version == 0)
508 crc_offset = 0x72;
509#endif
510
511 crc = crc32(0, (void *)&e, crc_offset);
512 crcp = (void *)&e + crc_offset;
513 if (crc != be32_to_cpu(*crcp)) {
Timur Tabi13c312b2009-08-28 16:56:45 -0500514 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
York Sun9215eff2014-04-30 14:43:46 -0700515 return 0;
Haiying Wange5607542009-06-04 16:12:40 -0400516 }
517
Timur Tabia2b41cb2011-02-09 02:00:09 +0000518#ifdef CONFIG_SYS_I2C_EEPROM_NXID
519 /*
520 * MAC address #9 in v1 occupies the same position as the CRC in v0.
521 * Erase it so that it's not mistaken for a MAC address. We'll
522 * update the CRC later.
523 */
524 if (e.version == 0)
525 memset(e.mac[8], 0xff, 6);
526#endif
527
Masahiro Yamadadb204642014-11-07 03:03:31 +0900528 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
Timur Tabibdea8372008-06-19 17:56:11 -0500529 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
530 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
531 char ethaddr[18];
532 char enetvar[9];
533
534 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
535 e.mac[i][0],
536 e.mac[i][1],
537 e.mac[i][2],
538 e.mac[i][3],
539 e.mac[i][4],
540 e.mac[i][5]);
541 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
542 /* Only initialize environment variables that are blank
543 * (i.e. have not yet been set)
544 */
Simon Glass64b723f2017-08-03 12:22:12 -0600545 if (!env_get(enetvar))
Simon Glass6a38e412017-08-03 12:22:09 -0600546 env_set(enetvar, ethaddr);
Timur Tabibdea8372008-06-19 17:56:11 -0500547 }
548 }
549
Timur Tabi13c312b2009-08-28 16:56:45 -0500550#ifdef CONFIG_SYS_I2C_EEPROM_NXID
551 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +0530552 be32_to_cpu(e.version));
Timur Tabi13c312b2009-08-28 16:56:45 -0500553#else
554 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
555#endif
556
Timur Tabia2b41cb2011-02-09 02:00:09 +0000557#ifdef CONFIG_SYS_I2C_EEPROM_NXID
558 /*
559 * Now we need to upconvert the data into v1 format. We do this last so
560 * that at boot time, U-Boot will still say "NXID v0".
561 */
562 if (e.version == 0) {
Jaiprakash Singhe1029ef2015-05-28 14:54:03 +0530563 e.version = cpu_to_be32(NXID_VERSION);
Timur Tabia2b41cb2011-02-09 02:00:09 +0000564 update_crc();
565 }
566#endif
567
Haiying Wang34c68782006-07-12 10:48:05 -0400568 return 0;
569}
Timur Tabibdea8372008-06-19 17:56:11 -0500570
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200571#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -0500572
573/**
574 * get_cpu_board_revision - get the CPU board revision on 85xx boards
575 *
576 * Read the EEPROM to determine the board revision.
577 *
578 * This function is called before relocation, so we need to read a private
579 * copy of the EEPROM into a local variable on the stack.
580 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200581 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
582 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
Timur Tabibdea8372008-06-19 17:56:11 -0500583 * so that the SPD code will work. This means that all pre-relocation I2C
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200584 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
585 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
Timur Tabibdea8372008-06-19 17:56:11 -0500586 * this function is called. Oh well.
587 */
588unsigned int get_cpu_board_revision(void)
589{
590 struct board_eeprom {
591 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
592 u8 major; /* 0x04 Board revision, major */
593 u8 minor; /* 0x05 Board revision, minor */
594 } be;
595
Igor Opaniukf7c91762021-02-09 13:52:45 +0200596#if !CONFIG_IS_ENABLED(DM_I2C)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200597 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabibdea8372008-06-19 17:56:11 -0500598 (void *)&be, sizeof(be));
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800599#else
600 struct udevice *dev;
Biwen Li037fa1a2020-05-01 20:56:37 +0800601 int ret;
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800602#ifdef CONFIG_SYS_EEPROM_BUS_NUM
603 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
604 CONFIG_SYS_I2C_EEPROM_ADDR,
605 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
606 &dev);
607#else
608 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
609 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Biwen Li037fa1a2020-05-01 20:56:37 +0800610 &dev);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800611#endif
612 if (!ret)
613 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
614#endif
Timur Tabibdea8372008-06-19 17:56:11 -0500615
616 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
617 return MPC85XX_CPU_BOARD_REV(0, 0);
618
619 if ((be.major == 0xff) && (be.minor == 0xff))
620 return MPC85XX_CPU_BOARD_REV(0, 0);
621
Rafal Czubak5c50d142008-10-08 13:41:30 +0200622 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
Timur Tabibdea8372008-06-19 17:56:11 -0500623}
Timur Tabibdea8372008-06-19 17:56:11 -0500624#endif
Heinrich Schuchardtfa4f82d2023-09-30 14:01:42 +0200625
Tom Rini03f146c2023-10-07 15:13:08 -0400626U_BOOT_LONGHELP(mac,
Heinrich Schuchardtfa4f82d2023-09-30 14:01:42 +0200627 "[read|save|id|num|errata|date|ports|port_number]\n"
628 "mac read\n"
629 " - read EEPROM content into memory data structure\n"
630 "mac save\n"
631 " - save memory data structure to the EEPROM\n"
632 "mac id\n"
633 " - program system id per hard coded value\n"
634 "mac num string\n"
635 " - program system serial number to value string\n"
636 "mac errata string\n"
637 " - program errata data to value string\n"
638 "mac date YYMMDDhhmmss\n"
639 " - program date to string value YYMMDDhhmmss\n"
640 "mac ports N\n"
641 " - program the number of network ports to integer N\n"
642 "mac X string\n"
Tom Rini03f146c2023-10-07 15:13:08 -0400643 " - program MAC addr for port X [X=0,1..] to colon separated string");
Heinrich Schuchardt19011492023-09-30 14:01:43 +0200644
645U_BOOT_CMD(
646 mac, 3, 1, do_mac,
647 "display and program the system ID and MAC addresses in EEPROM",
Tom Rini03f146c2023-10-07 15:13:08 -0400648 mac_help_text);