blob: 8c39f5e52414b14d6d68782b24347a3990c4dd90 [file] [log] [blame]
Pali Rohár0387f7f2022-04-08 16:30:12 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2017 Marek Behun <marek.behun@nic.cz>
4 * Copyright (C) 2016 Tomas Hlavacek <tomas.hlavacek@nic.cz>
5 */
6
7#include <env.h>
8#include <net.h>
9#include <dm/uclass.h>
10#include <atsha204a-i2c.h>
11
12#include "turris_atsha_otp.h"
13
14#define TURRIS_ATSHA_OTP_VERSION 0
15#define TURRIS_ATSHA_OTP_SERIAL 1
16#define TURRIS_ATSHA_OTP_MAC0 3
17#define TURRIS_ATSHA_OTP_MAC1 4
18
19static struct udevice *get_atsha204a_dev(void)
20{
Pali Rohár8a23da62022-04-08 16:30:13 +020021 /* Cannot be static because BSS does not have to be ready at this early stage */
22 struct udevice *dev;
Pali Rohár0387f7f2022-04-08 16:30:12 +020023
Pali Rohár9300f6c2022-04-08 16:30:15 +020024 if (uclass_get_device_by_name(UCLASS_MISC, "crypto@64", &dev)) {
Pali Rohár0387f7f2022-04-08 16:30:12 +020025 puts("Cannot find ATSHA204A on I2C bus!\n");
26 dev = NULL;
27 }
28
29 return dev;
30}
31
32static void increment_mac(u8 *mac)
33{
34 int i;
35
36 for (i = 5; i >= 3; i--) {
37 mac[i] += 1;
38 if (mac[i])
39 break;
40 }
41}
42
43static void set_mac_if_invalid(int i, u8 *mac)
44{
45 u8 oldmac[6];
46
47 if (is_valid_ethaddr(mac) &&
48 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
49 eth_env_set_enetaddr_by_index("eth", i, mac);
50}
51
Pali Rohár60f37e82022-04-08 16:30:14 +020052int turris_atsha_otp_init_mac_addresses(int first_idx)
Pali Rohár0387f7f2022-04-08 16:30:12 +020053{
54 struct udevice *dev = get_atsha204a_dev();
55 u8 mac0[4], mac1[4], mac[6];
56 int ret;
57
58 if (!dev)
59 return -1;
60
61 ret = atsha204a_wakeup(dev);
62 if (ret)
63 return ret;
64
65 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
66 TURRIS_ATSHA_OTP_MAC0, mac0);
67 if (ret)
68 return ret;
69
70 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
71 TURRIS_ATSHA_OTP_MAC1, mac1);
72 if (ret)
73 return ret;
74
75 atsha204a_sleep(dev);
76
77 mac[0] = mac0[1];
78 mac[1] = mac0[2];
79 mac[2] = mac0[3];
80 mac[3] = mac1[1];
81 mac[4] = mac1[2];
82 mac[5] = mac1[3];
83
Pali Rohár60f37e82022-04-08 16:30:14 +020084 set_mac_if_invalid((first_idx + 0) % 3, mac);
Pali Rohár0387f7f2022-04-08 16:30:12 +020085 increment_mac(mac);
Pali Rohár60f37e82022-04-08 16:30:14 +020086 set_mac_if_invalid((first_idx + 1) % 3, mac);
Pali Rohár0387f7f2022-04-08 16:30:12 +020087 increment_mac(mac);
Pali Rohár60f37e82022-04-08 16:30:14 +020088 set_mac_if_invalid((first_idx + 2) % 3, mac);
Pali Rohár0387f7f2022-04-08 16:30:12 +020089
90 return 0;
91}
92
93int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num)
94{
95 struct udevice *dev = get_atsha204a_dev();
96 int ret;
97
98 if (!dev)
99 return -1;
100
101 ret = atsha204a_wakeup(dev);
102 if (ret)
103 return ret;
104
105 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
106 TURRIS_ATSHA_OTP_VERSION,
107 (u8 *)version_num);
108 if (ret)
109 return ret;
110
111 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
112 TURRIS_ATSHA_OTP_SERIAL,
113 (u8 *)serial_num);
114 if (ret)
115 return ret;
116
117 atsha204a_sleep(dev);
118 return 0;
119}