Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Marek Behún | d63726e | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017 Marek Behún <kabel@kernel.org> |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 4 | * Copyright (C) 2016 Tomas Hlavacek <tomas.hlavacek@nic.cz> |
| 5 | */ |
| 6 | |
| 7 | #include <env.h> |
| 8 | #include <net.h> |
Pali Rohár | 50f3203 | 2022-05-19 11:11:52 +0200 | [diff] [blame] | 9 | #include <dm/device.h> |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 10 | #include <dm/uclass.h> |
| 11 | #include <atsha204a-i2c.h> |
| 12 | |
| 13 | #include "turris_atsha_otp.h" |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 14 | #include "turris_common.h" |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 15 | |
| 16 | #define TURRIS_ATSHA_OTP_VERSION 0 |
| 17 | #define TURRIS_ATSHA_OTP_SERIAL 1 |
| 18 | #define TURRIS_ATSHA_OTP_MAC0 3 |
| 19 | #define TURRIS_ATSHA_OTP_MAC1 4 |
| 20 | |
Pali Rohár | 50f3203 | 2022-05-19 11:11:52 +0200 | [diff] [blame] | 21 | extern U_BOOT_DRIVER(atsha204); |
| 22 | |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 23 | static struct udevice *get_atsha204a_dev(void) |
| 24 | { |
Pali Rohár | 8a23da6 | 2022-04-08 16:30:13 +0200 | [diff] [blame] | 25 | /* Cannot be static because BSS does not have to be ready at this early stage */ |
| 26 | struct udevice *dev; |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 27 | |
Pali Rohár | 50f3203 | 2022-05-19 11:11:52 +0200 | [diff] [blame] | 28 | if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(atsha204), &dev)) { |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 29 | puts("Cannot find ATSHA204A on I2C bus!\n"); |
| 30 | dev = NULL; |
| 31 | } |
| 32 | |
| 33 | return dev; |
| 34 | } |
| 35 | |
Pali Rohár | 60f37e8 | 2022-04-08 16:30:14 +0200 | [diff] [blame] | 36 | int turris_atsha_otp_init_mac_addresses(int first_idx) |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 37 | { |
| 38 | struct udevice *dev = get_atsha204a_dev(); |
| 39 | u8 mac0[4], mac1[4], mac[6]; |
| 40 | int ret; |
| 41 | |
| 42 | if (!dev) |
| 43 | return -1; |
| 44 | |
| 45 | ret = atsha204a_wakeup(dev); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false, |
| 50 | TURRIS_ATSHA_OTP_MAC0, mac0); |
| 51 | if (ret) |
| 52 | return ret; |
| 53 | |
| 54 | ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false, |
| 55 | TURRIS_ATSHA_OTP_MAC1, mac1); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | atsha204a_sleep(dev); |
| 60 | |
| 61 | mac[0] = mac0[1]; |
| 62 | mac[1] = mac0[2]; |
| 63 | mac[2] = mac0[3]; |
| 64 | mac[3] = mac1[1]; |
| 65 | mac[4] = mac1[2]; |
| 66 | mac[5] = mac1[3]; |
| 67 | |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 68 | turris_init_mac_addresses(first_idx, mac); |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Pali Rohár | 38ecdab | 2022-08-27 20:06:30 +0200 | [diff] [blame] | 73 | int turris_atsha_otp_init_serial_number(void) |
| 74 | { |
| 75 | char serial[17]; |
| 76 | int ret; |
| 77 | |
| 78 | ret = turris_atsha_otp_get_serial_number(serial); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | |
| 82 | if (!env_get("serial#")) |
| 83 | return -1; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int turris_atsha_otp_get_serial_number(char serial[17]) |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 89 | { |
| 90 | struct udevice *dev = get_atsha204a_dev(); |
Pali Rohár | 38ecdab | 2022-08-27 20:06:30 +0200 | [diff] [blame] | 91 | u32 version_num, serial_num; |
| 92 | const char *serial_env; |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 93 | int ret; |
| 94 | |
| 95 | if (!dev) |
| 96 | return -1; |
| 97 | |
Pali Rohár | 38ecdab | 2022-08-27 20:06:30 +0200 | [diff] [blame] | 98 | serial_env = env_get("serial#"); |
| 99 | if (serial_env && strlen(serial_env) == 16) { |
| 100 | memcpy(serial, serial_env, 17); |
| 101 | return 0; |
| 102 | } |
| 103 | |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 104 | ret = atsha204a_wakeup(dev); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false, |
| 109 | TURRIS_ATSHA_OTP_VERSION, |
Pali Rohár | 38ecdab | 2022-08-27 20:06:30 +0200 | [diff] [blame] | 110 | (u8 *)&version_num); |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 111 | if (ret) |
| 112 | return ret; |
| 113 | |
| 114 | ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false, |
| 115 | TURRIS_ATSHA_OTP_SERIAL, |
Pali Rohár | 38ecdab | 2022-08-27 20:06:30 +0200 | [diff] [blame] | 116 | (u8 *)&serial_num); |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 117 | if (ret) |
| 118 | return ret; |
| 119 | |
| 120 | atsha204a_sleep(dev); |
Pali Rohár | 38ecdab | 2022-08-27 20:06:30 +0200 | [diff] [blame] | 121 | |
| 122 | sprintf(serial, "%08X%08X", be32_to_cpu(version_num), be32_to_cpu(serial_num)); |
| 123 | env_set("serial#", serial); |
| 124 | |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } |