blob: b6ab06a08fb4a0b3bf45e4993d2037aa0a03b81d [file] [log] [blame]
Anup Patel7a167f22019-02-25 08:15:19 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
4 *
5 * Authors:
6 * Anup Patel <anup.patel@wdc.com>
7 */
8
Zong Liec348492021-09-01 15:01:42 +08009#include <cpu_func.h>
Anup Patel7a167f22019-02-25 08:15:19 +000010#include <dm.h>
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <env.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Pragnesh Patel2a449a32020-05-29 11:33:22 +053013#include <log.h>
Jagan Teki09a385d2020-07-15 15:38:58 +053014#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060015#include <linux/bug.h>
Anup Pateldb132102019-06-25 06:31:44 +000016#include <linux/delay.h>
Pragnesh Patel2a449a32020-05-29 11:33:22 +053017#include <misc.h>
Pragnesh Patele00653c2020-05-29 11:33:35 +053018#include <spl.h>
Zong Li5cc6af22021-07-27 17:06:59 +080019#include <asm/sections.h>
Anup Pateldb132102019-06-25 06:31:44 +000020
Pragnesh Patel2a449a32020-05-29 11:33:22 +053021/*
22 * This define is a value used for error/unknown serial.
23 * If we really care about distinguishing errors and 0 is
24 * valid, we'll need a different one.
25 */
26#define ERROR_READING_SERIAL_NUMBER 0
Anup Pateldb132102019-06-25 06:31:44 +000027
Pragnesh Patel2a449a32020-05-29 11:33:22 +053028#ifdef CONFIG_MISC_INIT_R
Anup Pateldb132102019-06-25 06:31:44 +000029
Simon Glass205a5042023-02-05 15:40:47 -070030#if IS_ENABLED(CONFIG_SIFIVE_OTP)
Pragnesh Patel2a449a32020-05-29 11:33:22 +053031static u32 otp_read_serialnum(struct udevice *dev)
Anup Pateldb132102019-06-25 06:31:44 +000032{
Pragnesh Patel2a449a32020-05-29 11:33:22 +053033 int ret;
34 u32 serial[2] = {0};
Anup Pateldb132102019-06-25 06:31:44 +000035
Pragnesh Patel2a449a32020-05-29 11:33:22 +053036 for (int i = 0xfe * 4; i > 0; i -= 8) {
37 ret = misc_read(dev, i, serial, sizeof(serial));
Anup Pateldb132102019-06-25 06:31:44 +000038
Pragnesh Patel2a449a32020-05-29 11:33:22 +053039 if (ret != sizeof(serial)) {
40 printf("%s: error reading serial from OTP\n", __func__);
41 break;
42 }
Anup Pateldb132102019-06-25 06:31:44 +000043
Pragnesh Patel2a449a32020-05-29 11:33:22 +053044 if (serial[0] == ~serial[1])
45 return serial[0];
Anup Pateldb132102019-06-25 06:31:44 +000046 }
47
Pragnesh Patel2a449a32020-05-29 11:33:22 +053048 return ERROR_READING_SERIAL_NUMBER;
Anup Pateldb132102019-06-25 06:31:44 +000049}
Pragnesh Patel2a449a32020-05-29 11:33:22 +053050#endif
Anup Pateldb132102019-06-25 06:31:44 +000051
52static u32 fu540_read_serialnum(void)
53{
Pragnesh Patel2a449a32020-05-29 11:33:22 +053054 u32 serial = ERROR_READING_SERIAL_NUMBER;
55
Simon Glass205a5042023-02-05 15:40:47 -070056#if IS_ENABLED(CONFIG_SIFIVE_OTP)
Pragnesh Patel2a449a32020-05-29 11:33:22 +053057 struct udevice *dev;
Anup Pateldb132102019-06-25 06:31:44 +000058 int ret;
Anup Pateldb132102019-06-25 06:31:44 +000059
Pragnesh Patel2a449a32020-05-29 11:33:22 +053060 /* init OTP */
61 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -070062 DM_DRIVER_GET(sifive_otp), &dev);
Pragnesh Patel2a449a32020-05-29 11:33:22 +053063
64 if (ret) {
65 debug("%s: could not find otp device\n", __func__);
66 return serial;
Anup Pateldb132102019-06-25 06:31:44 +000067 }
68
Pragnesh Patel2a449a32020-05-29 11:33:22 +053069 /* read serial from OTP and set env var */
70 serial = otp_read_serialnum(dev);
71#endif
72
73 return serial;
Anup Pateldb132102019-06-25 06:31:44 +000074}
75
76static void fu540_setup_macaddr(u32 serialnum)
77{
78 /* Default MAC address */
79 unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
80
81 /*
82 * We derive our board MAC address by ORing last three bytes
83 * of board serial number to above default MAC address.
84 *
85 * This logic of deriving board MAC address is taken from
86 * SiFive FSBL and is kept unchanged.
87 */
88 mac[5] |= (serialnum >> 0) & 0xff;
89 mac[4] |= (serialnum >> 8) & 0xff;
90 mac[3] |= (serialnum >> 16) & 0xff;
91
92 /* Update environment variable */
93 eth_env_set_enetaddr("ethaddr", mac);
94}
95
96int misc_init_r(void)
97{
Sagar Shrikant Kadam45309b22019-08-12 07:57:40 -070098 u32 serial_num;
99 char buf[9] = {0};
Anup Pateldb132102019-06-25 06:31:44 +0000100
Sagar Shrikant Kadam45309b22019-08-12 07:57:40 -0700101 /* Set ethaddr environment variable from board serial number */
102 if (!env_get("serial#")) {
103 serial_num = fu540_read_serialnum();
104 if (!serial_num) {
105 WARN(true, "Board serial number should not be 0 !!\n");
106 return 0;
107 }
108 snprintf(buf, sizeof(buf), "%08x", serial_num);
109 env_set("serial#", buf);
110 fu540_setup_macaddr(serial_num);
111 }
Anup Pateldb132102019-06-25 06:31:44 +0000112 return 0;
113}
114
115#endif
Anup Patel7a167f22019-02-25 08:15:19 +0000116
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300117void *board_fdt_blob_setup(int *err)
Zong Li5cc6af22021-07-27 17:06:59 +0800118{
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300119 *err = 0;
Bin Meng4c6a28c2022-01-05 09:07:57 +0800120 if (IS_ENABLED(CONFIG_OF_SEPARATE) || IS_ENABLED(CONFIG_OF_BOARD)) {
Zong Li5cc6af22021-07-27 17:06:59 +0800121 if (gd->arch.firmware_fdt_addr)
Bin Mengde1d0892021-09-12 11:15:16 +0800122 return (ulong *)(uintptr_t)gd->arch.firmware_fdt_addr;
Zong Li5cc6af22021-07-27 17:06:59 +0800123 }
Bin Meng50753b42021-09-11 22:31:23 +0800124
125 return (ulong *)&_end;
Zong Li5cc6af22021-07-27 17:06:59 +0800126}
127
Anup Patel7a167f22019-02-25 08:15:19 +0000128int board_init(void)
129{
Pragnesh Patel8a521282020-05-29 12:14:51 +0530130 /* enable all cache ways */
Zong Liec348492021-09-01 15:01:42 +0800131 enable_caches();
Anup Patel7a167f22019-02-25 08:15:19 +0000132
133 return 0;
134}