blob: aa4e29b1560e445a5bfa7ef50b69e141093573aa [file] [log] [blame]
Pali Rohár0387f7f2022-04-08 16:30:12 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
Marek Behúnd63726e2022-06-01 17:17:06 +02003 * Copyright (C) 2017 Marek Behún <kabel@kernel.org>
Pali Rohár0387f7f2022-04-08 16:30:12 +02004 * Copyright (C) 2016 Tomas Hlavacek <tomas.hlavacek@nic.cz>
5 */
6
7#include <env.h>
8#include <net.h>
Pali Rohár50f32032022-05-19 11:11:52 +02009#include <dm/device.h>
Pali Rohár0387f7f2022-04-08 16:30:12 +020010#include <dm/uclass.h>
11#include <atsha204a-i2c.h>
12
13#include "turris_atsha_otp.h"
14
15#define TURRIS_ATSHA_OTP_VERSION 0
16#define TURRIS_ATSHA_OTP_SERIAL 1
17#define TURRIS_ATSHA_OTP_MAC0 3
18#define TURRIS_ATSHA_OTP_MAC1 4
19
Pali Rohár50f32032022-05-19 11:11:52 +020020extern U_BOOT_DRIVER(atsha204);
21
Pali Rohár0387f7f2022-04-08 16:30:12 +020022static struct udevice *get_atsha204a_dev(void)
23{
Pali Rohár8a23da62022-04-08 16:30:13 +020024 /* Cannot be static because BSS does not have to be ready at this early stage */
25 struct udevice *dev;
Pali Rohár0387f7f2022-04-08 16:30:12 +020026
Pali Rohár50f32032022-05-19 11:11:52 +020027 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(atsha204), &dev)) {
Pali Rohár0387f7f2022-04-08 16:30:12 +020028 puts("Cannot find ATSHA204A on I2C bus!\n");
29 dev = NULL;
30 }
31
32 return dev;
33}
34
35static void increment_mac(u8 *mac)
36{
37 int i;
38
39 for (i = 5; i >= 3; i--) {
40 mac[i] += 1;
41 if (mac[i])
42 break;
43 }
44}
45
46static void set_mac_if_invalid(int i, u8 *mac)
47{
48 u8 oldmac[6];
49
50 if (is_valid_ethaddr(mac) &&
51 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
52 eth_env_set_enetaddr_by_index("eth", i, mac);
53}
54
Pali Rohár60f37e82022-04-08 16:30:14 +020055int turris_atsha_otp_init_mac_addresses(int first_idx)
Pali Rohár0387f7f2022-04-08 16:30:12 +020056{
57 struct udevice *dev = get_atsha204a_dev();
58 u8 mac0[4], mac1[4], mac[6];
59 int ret;
60
61 if (!dev)
62 return -1;
63
64 ret = atsha204a_wakeup(dev);
65 if (ret)
66 return ret;
67
68 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
69 TURRIS_ATSHA_OTP_MAC0, mac0);
70 if (ret)
71 return ret;
72
73 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
74 TURRIS_ATSHA_OTP_MAC1, mac1);
75 if (ret)
76 return ret;
77
78 atsha204a_sleep(dev);
79
80 mac[0] = mac0[1];
81 mac[1] = mac0[2];
82 mac[2] = mac0[3];
83 mac[3] = mac1[1];
84 mac[4] = mac1[2];
85 mac[5] = mac1[3];
86
Pali Rohár60f37e82022-04-08 16:30:14 +020087 set_mac_if_invalid((first_idx + 0) % 3, mac);
Pali Rohár0387f7f2022-04-08 16:30:12 +020088 increment_mac(mac);
Pali Rohár60f37e82022-04-08 16:30:14 +020089 set_mac_if_invalid((first_idx + 1) % 3, mac);
Pali Rohár0387f7f2022-04-08 16:30:12 +020090 increment_mac(mac);
Pali Rohár60f37e82022-04-08 16:30:14 +020091 set_mac_if_invalid((first_idx + 2) % 3, mac);
Pali Rohár0387f7f2022-04-08 16:30:12 +020092
93 return 0;
94}
95
96int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num)
97{
98 struct udevice *dev = get_atsha204a_dev();
99 int ret;
100
101 if (!dev)
102 return -1;
103
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,
110 (u8 *)version_num);
111 if (ret)
112 return ret;
113
114 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
115 TURRIS_ATSHA_OTP_SERIAL,
116 (u8 *)serial_num);
117 if (ret)
118 return ret;
119
120 atsha204a_sleep(dev);
121 return 0;
122}