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