blob: fa65fcade086fe64a21102f2a7744ce17613941b [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
Anup Patel7a167f22019-02-25 08:15:19 +00009#include <dm.h>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <env.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Pragnesh Patel2a449a32020-05-29 11:33:22 +053012#include <log.h>
Jagan Teki09a385d2020-07-15 15:38:58 +053013#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060014#include <linux/bug.h>
Anup Pateldb132102019-06-25 06:31:44 +000015#include <linux/delay.h>
Pragnesh Patel2a449a32020-05-29 11:33:22 +053016#include <misc.h>
Pragnesh Patele00653c2020-05-29 11:33:35 +053017#include <spl.h>
Pragnesh Patel8a521282020-05-29 12:14:51 +053018#include <asm/arch/cache.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
Pragnesh Patel2a449a32020-05-29 11:33:22 +053030#if CONFIG_IS_ENABLED(SIFIVE_OTP)
31static 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
56#if CONFIG_IS_ENABLED(SIFIVE_OTP)
57 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
Zong Li5cc6af22021-07-27 17:06:59 +0800117void *board_fdt_blob_setup(void)
118{
119 if (IS_ENABLED(CONFIG_OF_SEPARATE)) {
120 if (gd->arch.firmware_fdt_addr)
121 return (ulong *)gd->arch.firmware_fdt_addr;
122 else
123 return (ulong *)&_end;
124 }
125}
126
Anup Patel7a167f22019-02-25 08:15:19 +0000127int board_init(void)
128{
Pragnesh Patel8a521282020-05-29 12:14:51 +0530129 int ret;
130
131 /* enable all cache ways */
132 ret = cache_enable_ways();
133 if (ret) {
134 debug("%s: could not enable cache ways\n", __func__);
135 return ret;
136 }
Anup Patel7a167f22019-02-25 08:15:19 +0000137
138 return 0;
139}