2019-02-25 08:15:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Western Digital Corporation or its affiliates.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
|
|
*/
|
|
|
|
|
2021-09-01 07:01:42 +00:00
|
|
|
#include <cpu_func.h>
|
2019-02-25 08:15:19 +00:00
|
|
|
#include <dm.h>
|
2020-05-10 17:40:03 +00:00
|
|
|
#include <env.h>
|
2020-05-10 17:40:02 +00:00
|
|
|
#include <init.h>
|
2020-05-29 06:03:22 +00:00
|
|
|
#include <log.h>
|
2020-07-15 10:08:58 +00:00
|
|
|
#include <linux/bitops.h>
|
2020-05-10 17:40:08 +00:00
|
|
|
#include <linux/bug.h>
|
2019-06-25 06:31:44 +00:00
|
|
|
#include <linux/delay.h>
|
2020-05-29 06:03:22 +00:00
|
|
|
#include <misc.h>
|
2020-05-29 06:03:35 +00:00
|
|
|
#include <spl.h>
|
2021-07-27 09:06:59 +00:00
|
|
|
#include <asm/sections.h>
|
2019-06-25 06:31:44 +00:00
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
/*
|
|
|
|
* This define is a value used for error/unknown serial.
|
|
|
|
* If we really care about distinguishing errors and 0 is
|
|
|
|
* valid, we'll need a different one.
|
|
|
|
*/
|
|
|
|
#define ERROR_READING_SERIAL_NUMBER 0
|
2019-06-25 06:31:44 +00:00
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
#ifdef CONFIG_MISC_INIT_R
|
2019-06-25 06:31:44 +00:00
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SIFIVE_OTP)
|
|
|
|
static u32 otp_read_serialnum(struct udevice *dev)
|
2019-06-25 06:31:44 +00:00
|
|
|
{
|
2020-05-29 06:03:22 +00:00
|
|
|
int ret;
|
|
|
|
u32 serial[2] = {0};
|
2019-06-25 06:31:44 +00:00
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
for (int i = 0xfe * 4; i > 0; i -= 8) {
|
|
|
|
ret = misc_read(dev, i, serial, sizeof(serial));
|
2019-06-25 06:31:44 +00:00
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
if (ret != sizeof(serial)) {
|
|
|
|
printf("%s: error reading serial from OTP\n", __func__);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-25 06:31:44 +00:00
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
if (serial[0] == ~serial[1])
|
|
|
|
return serial[0];
|
2019-06-25 06:31:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
return ERROR_READING_SERIAL_NUMBER;
|
2019-06-25 06:31:44 +00:00
|
|
|
}
|
2020-05-29 06:03:22 +00:00
|
|
|
#endif
|
2019-06-25 06:31:44 +00:00
|
|
|
|
|
|
|
static u32 fu540_read_serialnum(void)
|
|
|
|
{
|
2020-05-29 06:03:22 +00:00
|
|
|
u32 serial = ERROR_READING_SERIAL_NUMBER;
|
|
|
|
|
|
|
|
#if CONFIG_IS_ENABLED(SIFIVE_OTP)
|
|
|
|
struct udevice *dev;
|
2019-06-25 06:31:44 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
/* init OTP */
|
|
|
|
ret = uclass_get_device_by_driver(UCLASS_MISC,
|
2020-12-29 03:34:56 +00:00
|
|
|
DM_DRIVER_GET(sifive_otp), &dev);
|
2020-05-29 06:03:22 +00:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: could not find otp device\n", __func__);
|
|
|
|
return serial;
|
2019-06-25 06:31:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 06:03:22 +00:00
|
|
|
/* read serial from OTP and set env var */
|
|
|
|
serial = otp_read_serialnum(dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return serial;
|
2019-06-25 06:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fu540_setup_macaddr(u32 serialnum)
|
|
|
|
{
|
|
|
|
/* Default MAC address */
|
|
|
|
unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We derive our board MAC address by ORing last three bytes
|
|
|
|
* of board serial number to above default MAC address.
|
|
|
|
*
|
|
|
|
* This logic of deriving board MAC address is taken from
|
|
|
|
* SiFive FSBL and is kept unchanged.
|
|
|
|
*/
|
|
|
|
mac[5] |= (serialnum >> 0) & 0xff;
|
|
|
|
mac[4] |= (serialnum >> 8) & 0xff;
|
|
|
|
mac[3] |= (serialnum >> 16) & 0xff;
|
|
|
|
|
|
|
|
/* Update environment variable */
|
|
|
|
eth_env_set_enetaddr("ethaddr", mac);
|
|
|
|
}
|
|
|
|
|
|
|
|
int misc_init_r(void)
|
|
|
|
{
|
2019-08-12 14:57:40 +00:00
|
|
|
u32 serial_num;
|
|
|
|
char buf[9] = {0};
|
|
|
|
|
|
|
|
/* Set ethaddr environment variable from board serial number */
|
|
|
|
if (!env_get("serial#")) {
|
|
|
|
serial_num = fu540_read_serialnum();
|
|
|
|
if (!serial_num) {
|
|
|
|
WARN(true, "Board serial number should not be 0 !!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
snprintf(buf, sizeof(buf), "%08x", serial_num);
|
|
|
|
env_set("serial#", buf);
|
|
|
|
fu540_setup_macaddr(serial_num);
|
|
|
|
}
|
2019-06-25 06:31:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2019-02-25 08:15:19 +00:00
|
|
|
|
2021-07-27 09:06:59 +00:00
|
|
|
void *board_fdt_blob_setup(void)
|
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_OF_SEPARATE)) {
|
|
|
|
if (gd->arch.firmware_fdt_addr)
|
2021-09-12 03:15:16 +00:00
|
|
|
return (ulong *)(uintptr_t)gd->arch.firmware_fdt_addr;
|
2021-07-27 09:06:59 +00:00
|
|
|
}
|
2021-09-11 14:31:23 +00:00
|
|
|
|
|
|
|
return (ulong *)&_end;
|
2021-07-27 09:06:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 08:15:19 +00:00
|
|
|
int board_init(void)
|
|
|
|
{
|
2020-05-29 06:44:51 +00:00
|
|
|
/* enable all cache ways */
|
2021-09-01 07:01:42 +00:00
|
|
|
enable_caches();
|
2019-02-25 08:15:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|