blob: 85eebcdf18eddf57813353c72b6827036956f52b [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"
Marek Behúnc2d19d02024-04-04 09:50:54 +020014#include "turris_common.h"
Pali Rohár0387f7f2022-04-08 16:30:12 +020015
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ár50f32032022-05-19 11:11:52 +020021extern U_BOOT_DRIVER(atsha204);
22
Pali Rohár0387f7f2022-04-08 16:30:12 +020023static struct udevice *get_atsha204a_dev(void)
24{
Pali Rohár8a23da62022-04-08 16:30:13 +020025 /* Cannot be static because BSS does not have to be ready at this early stage */
26 struct udevice *dev;
Pali Rohár0387f7f2022-04-08 16:30:12 +020027
Pali Rohár50f32032022-05-19 11:11:52 +020028 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(atsha204), &dev)) {
Pali Rohár0387f7f2022-04-08 16:30:12 +020029 puts("Cannot find ATSHA204A on I2C bus!\n");
30 dev = NULL;
31 }
32
33 return dev;
34}
35
Pali Rohár60f37e82022-04-08 16:30:14 +020036int turris_atsha_otp_init_mac_addresses(int first_idx)
Pali Rohár0387f7f2022-04-08 16:30:12 +020037{
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únc2d19d02024-04-04 09:50:54 +020068 turris_init_mac_addresses(first_idx, mac);
Pali Rohár0387f7f2022-04-08 16:30:12 +020069
70 return 0;
71}
72
Pali Rohár38ecdab2022-08-27 20:06:30 +020073int 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
88int turris_atsha_otp_get_serial_number(char serial[17])
Pali Rohár0387f7f2022-04-08 16:30:12 +020089{
90 struct udevice *dev = get_atsha204a_dev();
Pali Rohár38ecdab2022-08-27 20:06:30 +020091 u32 version_num, serial_num;
92 const char *serial_env;
Pali Rohár0387f7f2022-04-08 16:30:12 +020093 int ret;
94
95 if (!dev)
96 return -1;
97
Pali Rohár38ecdab2022-08-27 20:06:30 +020098 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ár0387f7f2022-04-08 16:30:12 +0200104 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ár38ecdab2022-08-27 20:06:30 +0200110 (u8 *)&version_num);
Pali Rohár0387f7f2022-04-08 16:30:12 +0200111 if (ret)
112 return ret;
113
114 ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
115 TURRIS_ATSHA_OTP_SERIAL,
Pali Rohár38ecdab2022-08-27 20:06:30 +0200116 (u8 *)&serial_num);
Pali Rohár0387f7f2022-04-08 16:30:12 +0200117 if (ret)
118 return ret;
119
120 atsha204a_sleep(dev);
Pali Rohár38ecdab2022-08-27 20:06:30 +0200121
122 sprintf(serial, "%08X%08X", be32_to_cpu(version_num), be32_to_cpu(serial_num));
123 env_set("serial#", serial);
124
Pali Rohár0387f7f2022-04-08 16:30:12 +0200125 return 0;
126}