1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
13 #include <linux/bitops.h>
14 #include <linux/bug.h>
15 #include <linux/delay.h>
18 #include <asm/arch/cache.h>
21 * This define is a value used for error/unknown serial.
22 * If we really care about distinguishing errors and 0 is
23 * valid, we'll need a different one.
25 #define ERROR_READING_SERIAL_NUMBER 0
27 #ifdef CONFIG_MISC_INIT_R
29 #if CONFIG_IS_ENABLED(SIFIVE_OTP)
30 static u32 otp_read_serialnum(struct udevice *dev)
35 for (int i = 0xfe * 4; i > 0; i -= 8) {
36 ret = misc_read(dev, i, serial, sizeof(serial));
38 if (ret != sizeof(serial)) {
39 printf("%s: error reading serial from OTP\n", __func__);
43 if (serial[0] == ~serial[1])
47 return ERROR_READING_SERIAL_NUMBER;
51 static u32 fu540_read_serialnum(void)
53 u32 serial = ERROR_READING_SERIAL_NUMBER;
55 #if CONFIG_IS_ENABLED(SIFIVE_OTP)
60 ret = uclass_get_device_by_driver(UCLASS_MISC,
61 DM_DRIVER_GET(sifive_otp), &dev);
64 debug("%s: could not find otp device\n", __func__);
68 /* read serial from OTP and set env var */
69 serial = otp_read_serialnum(dev);
75 static void fu540_setup_macaddr(u32 serialnum)
77 /* Default MAC address */
78 unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
81 * We derive our board MAC address by ORing last three bytes
82 * of board serial number to above default MAC address.
84 * This logic of deriving board MAC address is taken from
85 * SiFive FSBL and is kept unchanged.
87 mac[5] |= (serialnum >> 0) & 0xff;
88 mac[4] |= (serialnum >> 8) & 0xff;
89 mac[3] |= (serialnum >> 16) & 0xff;
91 /* Update environment variable */
92 eth_env_set_enetaddr("ethaddr", mac);
100 /* Set ethaddr environment variable from board serial number */
101 if (!env_get("serial#")) {
102 serial_num = fu540_read_serialnum();
104 WARN(true, "Board serial number should not be 0 !!\n");
107 snprintf(buf, sizeof(buf), "%08x", serial_num);
108 env_set("serial#", buf);
109 fu540_setup_macaddr(serial_num);
120 /* enable all cache ways */
121 ret = cache_enable_ways();
123 debug("%s: could not enable cache ways\n", __func__);