Anup Patel | 7a167f2 | 2019-02-25 08:15:19 +0000 | [diff] [blame] | 1 | // 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 Li | ec34849 | 2021-09-01 15:01:42 +0800 | [diff] [blame] | 9 | #include <cpu_func.h> |
Anup Patel | 7a167f2 | 2019-02-25 08:15:19 +0000 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 11 | #include <env.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 12 | #include <init.h> |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 13 | #include <log.h> |
Jagan Teki | 09a385d | 2020-07-15 15:38:58 +0530 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 15 | #include <linux/bug.h> |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 16 | #include <linux/delay.h> |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 17 | #include <misc.h> |
Pragnesh Patel | e00653c | 2020-05-29 11:33:35 +0530 | [diff] [blame] | 18 | #include <spl.h> |
Zong Li | 5cc6af2 | 2021-07-27 17:06:59 +0800 | [diff] [blame] | 19 | #include <asm/sections.h> |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 20 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 21 | /* |
| 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 Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 27 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 28 | #ifdef CONFIG_MISC_INIT_R |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 29 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 30 | #if CONFIG_IS_ENABLED(SIFIVE_OTP) |
| 31 | static u32 otp_read_serialnum(struct udevice *dev) |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 32 | { |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 33 | int ret; |
| 34 | u32 serial[2] = {0}; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 35 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 36 | for (int i = 0xfe * 4; i > 0; i -= 8) { |
| 37 | ret = misc_read(dev, i, serial, sizeof(serial)); |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 38 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 39 | if (ret != sizeof(serial)) { |
| 40 | printf("%s: error reading serial from OTP\n", __func__); |
| 41 | break; |
| 42 | } |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 43 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 44 | if (serial[0] == ~serial[1]) |
| 45 | return serial[0]; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 48 | return ERROR_READING_SERIAL_NUMBER; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 49 | } |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 50 | #endif |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 51 | |
| 52 | static u32 fu540_read_serialnum(void) |
| 53 | { |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 54 | u32 serial = ERROR_READING_SERIAL_NUMBER; |
| 55 | |
| 56 | #if CONFIG_IS_ENABLED(SIFIVE_OTP) |
| 57 | struct udevice *dev; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 58 | int ret; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 59 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 60 | /* init OTP */ |
| 61 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 62 | DM_DRIVER_GET(sifive_otp), &dev); |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 63 | |
| 64 | if (ret) { |
| 65 | debug("%s: could not find otp device\n", __func__); |
| 66 | return serial; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 69 | /* read serial from OTP and set env var */ |
| 70 | serial = otp_read_serialnum(dev); |
| 71 | #endif |
| 72 | |
| 73 | return serial; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static 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 | |
| 96 | int misc_init_r(void) |
| 97 | { |
Sagar Shrikant Kadam | 45309b2 | 2019-08-12 07:57:40 -0700 | [diff] [blame] | 98 | u32 serial_num; |
| 99 | char buf[9] = {0}; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 100 | |
Sagar Shrikant Kadam | 45309b2 | 2019-08-12 07:57:40 -0700 | [diff] [blame] | 101 | /* 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 Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | #endif |
Anup Patel | 7a167f2 | 2019-02-25 08:15:19 +0000 | [diff] [blame] | 116 | |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 117 | void *board_fdt_blob_setup(int *err) |
Zong Li | 5cc6af2 | 2021-07-27 17:06:59 +0800 | [diff] [blame] | 118 | { |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 119 | *err = 0; |
Bin Meng | 4c6a28c | 2022-01-05 09:07:57 +0800 | [diff] [blame] | 120 | if (IS_ENABLED(CONFIG_OF_SEPARATE) || IS_ENABLED(CONFIG_OF_BOARD)) { |
Zong Li | 5cc6af2 | 2021-07-27 17:06:59 +0800 | [diff] [blame] | 121 | if (gd->arch.firmware_fdt_addr) |
Bin Meng | de1d089 | 2021-09-12 11:15:16 +0800 | [diff] [blame] | 122 | return (ulong *)(uintptr_t)gd->arch.firmware_fdt_addr; |
Zong Li | 5cc6af2 | 2021-07-27 17:06:59 +0800 | [diff] [blame] | 123 | } |
Bin Meng | 50753b4 | 2021-09-11 22:31:23 +0800 | [diff] [blame] | 124 | |
| 125 | return (ulong *)&_end; |
Zong Li | 5cc6af2 | 2021-07-27 17:06:59 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Anup Patel | 7a167f2 | 2019-02-25 08:15:19 +0000 | [diff] [blame] | 128 | int board_init(void) |
| 129 | { |
Pragnesh Patel | 8a52128 | 2020-05-29 12:14:51 +0530 | [diff] [blame] | 130 | /* enable all cache ways */ |
Zong Li | ec34849 | 2021-09-01 15:01:42 +0800 | [diff] [blame] | 131 | enable_caches(); |
Anup Patel | 7a167f2 | 2019-02-25 08:15:19 +0000 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |