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 | |
Anup Patel | 7a167f2 | 2019-02-25 08:15:19 +0000 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 11 | #include <init.h> |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 13 | #include <linux/bug.h> |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 14 | #include <linux/delay.h> |
| 15 | #include <linux/io.h> |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 16 | #include <misc.h> |
Pragnesh Patel | e00653c | 2020-05-29 11:33:35 +0530 | [diff] [blame] | 17 | #include <spl.h> |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 18 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 19 | /* |
| 20 | * This define is a value used for error/unknown serial. |
| 21 | * If we really care about distinguishing errors and 0 is |
| 22 | * valid, we'll need a different one. |
| 23 | */ |
| 24 | #define ERROR_READING_SERIAL_NUMBER 0 |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 25 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 26 | #ifdef CONFIG_MISC_INIT_R |
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 | #if CONFIG_IS_ENABLED(SIFIVE_OTP) |
| 29 | static u32 otp_read_serialnum(struct udevice *dev) |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 30 | { |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 31 | int ret; |
| 32 | u32 serial[2] = {0}; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 33 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 34 | for (int i = 0xfe * 4; i > 0; i -= 8) { |
| 35 | ret = misc_read(dev, i, serial, sizeof(serial)); |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 36 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 37 | if (ret != sizeof(serial)) { |
| 38 | printf("%s: error reading serial from OTP\n", __func__); |
| 39 | break; |
| 40 | } |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 41 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 42 | if (serial[0] == ~serial[1]) |
| 43 | return serial[0]; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 46 | return ERROR_READING_SERIAL_NUMBER; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 47 | } |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 48 | #endif |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 49 | |
| 50 | static u32 fu540_read_serialnum(void) |
| 51 | { |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 52 | u32 serial = ERROR_READING_SERIAL_NUMBER; |
| 53 | |
| 54 | #if CONFIG_IS_ENABLED(SIFIVE_OTP) |
| 55 | struct udevice *dev; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 56 | int ret; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 57 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 58 | /* init OTP */ |
| 59 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 60 | DM_GET_DRIVER(sifive_otp), &dev); |
| 61 | |
| 62 | if (ret) { |
| 63 | debug("%s: could not find otp device\n", __func__); |
| 64 | return serial; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Pragnesh Patel | 2a449a3 | 2020-05-29 11:33:22 +0530 | [diff] [blame] | 67 | /* read serial from OTP and set env var */ |
| 68 | serial = otp_read_serialnum(dev); |
| 69 | #endif |
| 70 | |
| 71 | return serial; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void fu540_setup_macaddr(u32 serialnum) |
| 75 | { |
| 76 | /* Default MAC address */ |
| 77 | unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 }; |
| 78 | |
| 79 | /* |
| 80 | * We derive our board MAC address by ORing last three bytes |
| 81 | * of board serial number to above default MAC address. |
| 82 | * |
| 83 | * This logic of deriving board MAC address is taken from |
| 84 | * SiFive FSBL and is kept unchanged. |
| 85 | */ |
| 86 | mac[5] |= (serialnum >> 0) & 0xff; |
| 87 | mac[4] |= (serialnum >> 8) & 0xff; |
| 88 | mac[3] |= (serialnum >> 16) & 0xff; |
| 89 | |
| 90 | /* Update environment variable */ |
| 91 | eth_env_set_enetaddr("ethaddr", mac); |
| 92 | } |
| 93 | |
| 94 | int misc_init_r(void) |
| 95 | { |
Sagar Shrikant Kadam | 45309b2 | 2019-08-12 07:57:40 -0700 | [diff] [blame] | 96 | u32 serial_num; |
| 97 | char buf[9] = {0}; |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 98 | |
Sagar Shrikant Kadam | 45309b2 | 2019-08-12 07:57:40 -0700 | [diff] [blame] | 99 | /* Set ethaddr environment variable from board serial number */ |
| 100 | if (!env_get("serial#")) { |
| 101 | serial_num = fu540_read_serialnum(); |
| 102 | if (!serial_num) { |
| 103 | WARN(true, "Board serial number should not be 0 !!\n"); |
| 104 | return 0; |
| 105 | } |
| 106 | snprintf(buf, sizeof(buf), "%08x", serial_num); |
| 107 | env_set("serial#", buf); |
| 108 | fu540_setup_macaddr(serial_num); |
| 109 | } |
Anup Patel | db13210 | 2019-06-25 06:31:44 +0000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | #endif |
Anup Patel | 7a167f2 | 2019-02-25 08:15:19 +0000 | [diff] [blame] | 114 | |
| 115 | int board_init(void) |
| 116 | { |
| 117 | /* For now nothing to do here. */ |
| 118 | |
| 119 | return 0; |
| 120 | } |
Pragnesh Patel | e00653c | 2020-05-29 11:33:35 +0530 | [diff] [blame] | 121 | |
| 122 | #ifdef CONFIG_SPL |
| 123 | u32 spl_boot_device(void) |
| 124 | { |
| 125 | #ifdef CONFIG_SPL_MMC_SUPPORT |
| 126 | return BOOT_DEVICE_MMC1; |
| 127 | #else |
| 128 | puts("Unknown boot device\n"); |
| 129 | hang(); |
| 130 | #endif |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #ifdef CONFIG_SPL_LOAD_FIT |
| 135 | int board_fit_config_name_match(const char *name) |
| 136 | { |
| 137 | /* boot using first FIT config */ |
| 138 | return 0; |
| 139 | } |
| 140 | #endif |