All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file
@ 2022-04-08 14:30 Pali Rohár
  2022-04-08 14:30 ` [PATCH 2/4] board: turris: Do not cache Atsha device in BSS Pali Rohár
                   ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Pali Rohár @ 2022-04-08 14:30 UTC (permalink / raw)
  To: Stefan Roese, Marek Behun; +Cc: u-boot

OTP code is not Atsha generic but also it is not Omnia specific. It is
common for all Turris routers which use Atsha cryptochip for storing OTP.
So move this common Turris specific Atsha OTP code from Turris Omnia into
separate file. It will be used also by other Turris routers.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 board/CZ.NIC/turris_atsha_otp.c          | 121 +++++++++++++++++++++++
 board/CZ.NIC/turris_atsha_otp.h          |   9 ++
 board/CZ.NIC/turris_omnia/Makefile       |   2 +-
 board/CZ.NIC/turris_omnia/turris_omnia.c | 108 +-------------------
 4 files changed, 135 insertions(+), 105 deletions(-)
 create mode 100644 board/CZ.NIC/turris_atsha_otp.c
 create mode 100644 board/CZ.NIC/turris_atsha_otp.h

diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
new file mode 100644
index 000000000000..a4a77c74fb19
--- /dev/null
+++ b/board/CZ.NIC/turris_atsha_otp.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2017 Marek Behun <marek.behun@nic.cz>
+ * Copyright (C) 2016 Tomas Hlavacek <tomas.hlavacek@nic.cz>
+ */
+
+#include <env.h>
+#include <net.h>
+#include <dm/uclass.h>
+#include <atsha204a-i2c.h>
+
+#include "turris_atsha_otp.h"
+
+#define TURRIS_ATSHA_OTP_VERSION	0
+#define TURRIS_ATSHA_OTP_SERIAL		1
+#define TURRIS_ATSHA_OTP_MAC0		3
+#define TURRIS_ATSHA_OTP_MAC1		4
+
+static struct udevice *get_atsha204a_dev(void)
+{
+	static struct udevice *dev;
+
+	if (dev)
+		return dev;
+
+	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
+		puts("Cannot find ATSHA204A on I2C bus!\n");
+		dev = NULL;
+	}
+
+	return dev;
+}
+
+static void increment_mac(u8 *mac)
+{
+	int i;
+
+	for (i = 5; i >= 3; i--) {
+		mac[i] += 1;
+		if (mac[i])
+			break;
+	}
+}
+
+static void set_mac_if_invalid(int i, u8 *mac)
+{
+	u8 oldmac[6];
+
+	if (is_valid_ethaddr(mac) &&
+	    !eth_env_get_enetaddr_by_index("eth", i, oldmac))
+		eth_env_set_enetaddr_by_index("eth", i, mac);
+}
+
+int turris_atsha_otp_init_mac_addresses(void)
+{
+	struct udevice *dev = get_atsha204a_dev();
+	u8 mac0[4], mac1[4], mac[6];
+	int ret;
+
+	if (!dev)
+		return -1;
+
+	ret = atsha204a_wakeup(dev);
+	if (ret)
+		return ret;
+
+	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
+			     TURRIS_ATSHA_OTP_MAC0, mac0);
+	if (ret)
+		return ret;
+
+	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
+			     TURRIS_ATSHA_OTP_MAC1, mac1);
+	if (ret)
+		return ret;
+
+	atsha204a_sleep(dev);
+
+	mac[0] = mac0[1];
+	mac[1] = mac0[2];
+	mac[2] = mac0[3];
+	mac[3] = mac1[1];
+	mac[4] = mac1[2];
+	mac[5] = mac1[3];
+
+	set_mac_if_invalid(1, mac);
+	increment_mac(mac);
+	set_mac_if_invalid(2, mac);
+	increment_mac(mac);
+	set_mac_if_invalid(0, mac);
+
+	return 0;
+}
+
+int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num)
+{
+	struct udevice *dev = get_atsha204a_dev();
+	int ret;
+
+	if (!dev)
+		return -1;
+
+	ret = atsha204a_wakeup(dev);
+	if (ret)
+		return ret;
+
+	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
+			     TURRIS_ATSHA_OTP_VERSION,
+			     (u8 *)version_num);
+	if (ret)
+		return ret;
+
+	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
+			     TURRIS_ATSHA_OTP_SERIAL,
+			     (u8 *)serial_num);
+	if (ret)
+		return ret;
+
+	atsha204a_sleep(dev);
+	return 0;
+}
diff --git a/board/CZ.NIC/turris_atsha_otp.h b/board/CZ.NIC/turris_atsha_otp.h
new file mode 100644
index 000000000000..667d01af7310
--- /dev/null
+++ b/board/CZ.NIC/turris_atsha_otp.h
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#ifndef TURRIS_ATSHA_OTP_H
+#define TURRIS_ATSHA_OTP_H
+
+int turris_atsha_otp_init_mac_addresses(void);
+int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num);
+
+#endif
diff --git a/board/CZ.NIC/turris_omnia/Makefile b/board/CZ.NIC/turris_omnia/Makefile
index ccdf6c352cad..b79555ab4673 100644
--- a/board/CZ.NIC/turris_omnia/Makefile
+++ b/board/CZ.NIC/turris_omnia/Makefile
@@ -2,4 +2,4 @@
 #
 # Copyright (C) 2017 Marek Behun <marek.behun@nic.cz>
 
-obj-y	:= turris_omnia.o
+obj-y	:= turris_omnia.o ../turris_atsha_otp.o
diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c b/board/CZ.NIC/turris_omnia/turris_omnia.c
index 33cec6587e19..719e8750e60a 100644
--- a/board/CZ.NIC/turris_omnia/turris_omnia.c
+++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
@@ -14,8 +14,6 @@
 #include <log.h>
 #include <miiphy.h>
 #include <mtd.h>
-#include <net.h>
-#include <netdev.h>
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <asm/arch/cpu.h>
@@ -25,10 +23,10 @@
 #include <time.h>
 #include <linux/bitops.h>
 #include <u-boot/crc.h>
-# include <atsha204a-i2c.h>
 
 #include "../drivers/ddr/marvell/a38x/ddr3_init.h"
 #include <../serdes/a38x/high_speed_env_spec.h>
+#include "../turris_atsha_otp.h"
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -71,11 +69,6 @@ enum status_word_bits {
 	MSATA_IND_STSBIT	= 0x0020,
 };
 
-#define OMNIA_ATSHA204_OTP_VERSION	0
-#define OMNIA_ATSHA204_OTP_SERIAL	1
-#define OMNIA_ATSHA204_OTP_MAC0		3
-#define OMNIA_ATSHA204_OTP_MAC1		4
-
 /*
  * Those values and defines are taken from the Marvell U-Boot version
  * "u-boot-2013.01-2014_T3.0"
@@ -594,49 +587,12 @@ int board_late_init(void)
 	return 0;
 }
 
-static struct udevice *get_atsha204a_dev(void)
-{
-	static struct udevice *dev;
-
-	if (dev)
-		return dev;
-
-	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
-		puts("Cannot find ATSHA204A on I2C bus!\n");
-		dev = NULL;
-	}
-
-	return dev;
-}
-
 int show_board_info(void)
 {
 	u32 version_num, serial_num;
-	int err = 1;
-
-	struct udevice *dev = get_atsha204a_dev();
-
-	if (dev) {
-		err = atsha204a_wakeup(dev);
-		if (err)
-			goto out;
-
-		err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
-				     OMNIA_ATSHA204_OTP_VERSION,
-				     (u8 *)&version_num);
-		if (err)
-			goto out;
-
-		err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
-				     OMNIA_ATSHA204_OTP_SERIAL,
-				     (u8 *)&serial_num);
-		if (err)
-			goto out;
-
-		atsha204a_sleep(dev);
-	}
+	int err;
 
-out:
+	err = turris_atsha_otp_get_serial_number(&version_num, &serial_num);
 	printf("Model: Turris Omnia\n");
 	printf("  RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
 	if (err)
@@ -648,65 +604,9 @@ out:
 	return 0;
 }
 
-static void increment_mac(u8 *mac)
-{
-	int i;
-
-	for (i = 5; i >= 3; i--) {
-		mac[i] += 1;
-		if (mac[i])
-			break;
-	}
-}
-
-static void set_mac_if_invalid(int i, u8 *mac)
-{
-	u8 oldmac[6];
-
-	if (is_valid_ethaddr(mac) &&
-	    !eth_env_get_enetaddr_by_index("eth", i, oldmac))
-		eth_env_set_enetaddr_by_index("eth", i, mac);
-}
-
 int misc_init_r(void)
 {
-	int err;
-	struct udevice *dev = get_atsha204a_dev();
-	u8 mac0[4], mac1[4], mac[6];
-
-	if (!dev)
-		goto out;
-
-	err = atsha204a_wakeup(dev);
-	if (err)
-		goto out;
-
-	err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
-			     OMNIA_ATSHA204_OTP_MAC0, mac0);
-	if (err)
-		goto out;
-
-	err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
-			     OMNIA_ATSHA204_OTP_MAC1, mac1);
-	if (err)
-		goto out;
-
-	atsha204a_sleep(dev);
-
-	mac[0] = mac0[1];
-	mac[1] = mac0[2];
-	mac[2] = mac0[3];
-	mac[3] = mac1[1];
-	mac[4] = mac1[2];
-	mac[5] = mac1[3];
-
-	set_mac_if_invalid(1, mac);
-	increment_mac(mac);
-	set_mac_if_invalid(2, mac);
-	increment_mac(mac);
-	set_mac_if_invalid(0, mac);
-
-out:
+	turris_atsha_otp_init_mac_addresses();
 	return 0;
 }
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/4] board: turris: Do not cache Atsha device in BSS
  2022-04-08 14:30 [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Pali Rohár
@ 2022-04-08 14:30 ` Pali Rohár
  2022-04-08 17:45   ` Marek Behún
  2022-04-21 14:14   ` Stefan Roese
  2022-04-08 14:30 ` [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address Pali Rohár
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 12+ messages in thread
From: Pali Rohár @ 2022-04-08 14:30 UTC (permalink / raw)
  To: Stefan Roese, Marek Behun; +Cc: u-boot

Atsha device is used prior relocation and at this early stage BSS does not
have to be ready yet. So do not cache Atsha device in BSS.

Fixes support for other Turris routers.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 board/CZ.NIC/turris_atsha_otp.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
index a4a77c74fb19..840721a9b737 100644
--- a/board/CZ.NIC/turris_atsha_otp.c
+++ b/board/CZ.NIC/turris_atsha_otp.c
@@ -18,10 +18,8 @@
 
 static struct udevice *get_atsha204a_dev(void)
 {
-	static struct udevice *dev;
-
-	if (dev)
-		return dev;
+	/* Cannot be static because BSS does not have to be ready at this early stage */
+	struct udevice *dev;
 
 	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
 		puts("Cannot find ATSHA204A on I2C bus!\n");
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address
  2022-04-08 14:30 [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Pali Rohár
  2022-04-08 14:30 ` [PATCH 2/4] board: turris: Do not cache Atsha device in BSS Pali Rohár
@ 2022-04-08 14:30 ` Pali Rohár
  2022-04-08 17:45   ` Marek Behún
  2022-04-21 14:15   ` Stefan Roese
  2022-04-08 14:30 ` [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64 Pali Rohár
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 12+ messages in thread
From: Pali Rohár @ 2022-04-08 14:30 UTC (permalink / raw)
  To: Stefan Roese, Marek Behun; +Cc: u-boot

Turris Omnia uses first MAC address from OTP for second ethernet interface.
Second MAC address for third interface and third MAC address for first
interface.

Other Turris routers do not have this rotate by one mapping. So add
function parameter for specifying id of the first ethernet interface.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 board/CZ.NIC/turris_atsha_otp.c          | 8 ++++----
 board/CZ.NIC/turris_atsha_otp.h          | 2 +-
 board/CZ.NIC/turris_omnia/turris_omnia.c | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
index 840721a9b737..7a39b7f61d5d 100644
--- a/board/CZ.NIC/turris_atsha_otp.c
+++ b/board/CZ.NIC/turris_atsha_otp.c
@@ -49,7 +49,7 @@ static void set_mac_if_invalid(int i, u8 *mac)
 		eth_env_set_enetaddr_by_index("eth", i, mac);
 }
 
-int turris_atsha_otp_init_mac_addresses(void)
+int turris_atsha_otp_init_mac_addresses(int first_idx)
 {
 	struct udevice *dev = get_atsha204a_dev();
 	u8 mac0[4], mac1[4], mac[6];
@@ -81,11 +81,11 @@ int turris_atsha_otp_init_mac_addresses(void)
 	mac[4] = mac1[2];
 	mac[5] = mac1[3];
 
-	set_mac_if_invalid(1, mac);
+	set_mac_if_invalid((first_idx + 0) % 3, mac);
 	increment_mac(mac);
-	set_mac_if_invalid(2, mac);
+	set_mac_if_invalid((first_idx + 1) % 3, mac);
 	increment_mac(mac);
-	set_mac_if_invalid(0, mac);
+	set_mac_if_invalid((first_idx + 2) % 3, mac);
 
 	return 0;
 }
diff --git a/board/CZ.NIC/turris_atsha_otp.h b/board/CZ.NIC/turris_atsha_otp.h
index 667d01af7310..bd4308fdc3ef 100644
--- a/board/CZ.NIC/turris_atsha_otp.h
+++ b/board/CZ.NIC/turris_atsha_otp.h
@@ -3,7 +3,7 @@
 #ifndef TURRIS_ATSHA_OTP_H
 #define TURRIS_ATSHA_OTP_H
 
-int turris_atsha_otp_init_mac_addresses(void);
+int turris_atsha_otp_init_mac_addresses(int first_idx);
 int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num);
 
 #endif
diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c b/board/CZ.NIC/turris_omnia/turris_omnia.c
index 719e8750e60a..da2fee578c44 100644
--- a/board/CZ.NIC/turris_omnia/turris_omnia.c
+++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
@@ -606,7 +606,7 @@ int show_board_info(void)
 
 int misc_init_r(void)
 {
-	turris_atsha_otp_init_mac_addresses();
+	turris_atsha_otp_init_mac_addresses(1);
 	return 0;
 }
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64
  2022-04-08 14:30 [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Pali Rohár
  2022-04-08 14:30 ` [PATCH 2/4] board: turris: Do not cache Atsha device in BSS Pali Rohár
  2022-04-08 14:30 ` [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address Pali Rohár
@ 2022-04-08 14:30 ` Pali Rohár
  2022-04-08 17:46   ` Marek Behún
  2022-04-21 14:15   ` Stefan Roese
  2022-04-08 17:45 ` [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Marek Behún
  2022-04-21 14:14 ` Stefan Roese
  4 siblings, 2 replies; 12+ messages in thread
From: Pali Rohár @ 2022-04-08 14:30 UTC (permalink / raw)
  To: Stefan Roese, Marek Behun; +Cc: u-boot

DT node name should be generic, therefore rename atsha204a@64 to crypto@64.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi | 2 +-
 board/CZ.NIC/turris_atsha_otp.c                  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi b/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi
index 3ff76c946294..76a441524833 100644
--- a/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi
+++ b/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi
@@ -29,7 +29,7 @@
 			u-boot,dm-pre-reloc;
 
 			/* ATSHA204A at address 0x64 */
-			atsha204a@64 {
+			crypto@64 {
 				u-boot,dm-pre-reloc;
 				compatible = "atmel,atsha204a";
 				reg = <0x64>;
diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
index 7a39b7f61d5d..8c39f5e52414 100644
--- a/board/CZ.NIC/turris_atsha_otp.c
+++ b/board/CZ.NIC/turris_atsha_otp.c
@@ -21,7 +21,7 @@ static struct udevice *get_atsha204a_dev(void)
 	/* Cannot be static because BSS does not have to be ready at this early stage */
 	struct udevice *dev;
 
-	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
+	if (uclass_get_device_by_name(UCLASS_MISC, "crypto@64", &dev)) {
 		puts("Cannot find ATSHA204A on I2C bus!\n");
 		dev = NULL;
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file
  2022-04-08 14:30 [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Pali Rohár
                   ` (2 preceding siblings ...)
  2022-04-08 14:30 ` [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64 Pali Rohár
@ 2022-04-08 17:45 ` Marek Behún
  2022-04-21 14:14 ` Stefan Roese
  4 siblings, 0 replies; 12+ messages in thread
From: Marek Behún @ 2022-04-08 17:45 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Stefan Roese, u-boot

On Fri,  8 Apr 2022 16:30:12 +0200
Pali Rohár <pali@kernel.org> wrote:

> OTP code is not Atsha generic but also it is not Omnia specific. It is
> common for all Turris routers which use Atsha cryptochip for storing OTP.
> So move this common Turris specific Atsha OTP code from Turris Omnia into
> separate file. It will be used also by other Turris routers.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Reviewed-by: Marek Behún <marek.behun@nic.cz>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/4] board: turris: Do not cache Atsha device in BSS
  2022-04-08 14:30 ` [PATCH 2/4] board: turris: Do not cache Atsha device in BSS Pali Rohár
@ 2022-04-08 17:45   ` Marek Behún
  2022-04-21 14:14   ` Stefan Roese
  1 sibling, 0 replies; 12+ messages in thread
From: Marek Behún @ 2022-04-08 17:45 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Stefan Roese, u-boot

On Fri,  8 Apr 2022 16:30:13 +0200
Pali Rohár <pali@kernel.org> wrote:

> Atsha device is used prior relocation and at this early stage BSS does not
> have to be ready yet. So do not cache Atsha device in BSS.
> 
> Fixes support for other Turris routers.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Reviewed-by: Marek Behún <marek.behun@nic.cz>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address
  2022-04-08 14:30 ` [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address Pali Rohár
@ 2022-04-08 17:45   ` Marek Behún
  2022-04-21 14:15   ` Stefan Roese
  1 sibling, 0 replies; 12+ messages in thread
From: Marek Behún @ 2022-04-08 17:45 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Stefan Roese, u-boot

On Fri,  8 Apr 2022 16:30:14 +0200
Pali Rohár <pali@kernel.org> wrote:

> Turris Omnia uses first MAC address from OTP for second ethernet interface.
> Second MAC address for third interface and third MAC address for first
> interface.
> 
> Other Turris routers do not have this rotate by one mapping. So add
> function parameter for specifying id of the first ethernet interface.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Reviewed-by: Marek Behún <marek.behun@nic.cz>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64
  2022-04-08 14:30 ` [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64 Pali Rohár
@ 2022-04-08 17:46   ` Marek Behún
  2022-04-21 14:15   ` Stefan Roese
  1 sibling, 0 replies; 12+ messages in thread
From: Marek Behún @ 2022-04-08 17:46 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Stefan Roese, u-boot

On Fri,  8 Apr 2022 16:30:15 +0200
Pali Rohár <pali@kernel.org> wrote:

> DT node name should be generic, therefore rename atsha204a@64 to crypto@64.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Reviewed-by: Marek Behún <marek.behun@nic.cz>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file
  2022-04-08 14:30 [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Pali Rohár
                   ` (3 preceding siblings ...)
  2022-04-08 17:45 ` [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Marek Behún
@ 2022-04-21 14:14 ` Stefan Roese
  4 siblings, 0 replies; 12+ messages in thread
From: Stefan Roese @ 2022-04-21 14:14 UTC (permalink / raw)
  To: Pali Rohár, Marek Behun; +Cc: u-boot

On 4/8/22 16:30, Pali Rohár wrote:
> OTP code is not Atsha generic but also it is not Omnia specific. It is
> common for all Turris routers which use Atsha cryptochip for storing OTP.
> So move this common Turris specific Atsha OTP code from Turris Omnia into
> separate file. It will be used also by other Turris routers.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Applied to u-boot-marvell/master

Thanks,
Stefan

> ---
>   board/CZ.NIC/turris_atsha_otp.c          | 121 +++++++++++++++++++++++
>   board/CZ.NIC/turris_atsha_otp.h          |   9 ++
>   board/CZ.NIC/turris_omnia/Makefile       |   2 +-
>   board/CZ.NIC/turris_omnia/turris_omnia.c | 108 +-------------------
>   4 files changed, 135 insertions(+), 105 deletions(-)
>   create mode 100644 board/CZ.NIC/turris_atsha_otp.c
>   create mode 100644 board/CZ.NIC/turris_atsha_otp.h
> 
> diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
> new file mode 100644
> index 000000000000..a4a77c74fb19
> --- /dev/null
> +++ b/board/CZ.NIC/turris_atsha_otp.c
> @@ -0,0 +1,121 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) 2017 Marek Behun <marek.behun@nic.cz>
> + * Copyright (C) 2016 Tomas Hlavacek <tomas.hlavacek@nic.cz>
> + */
> +
> +#include <env.h>
> +#include <net.h>
> +#include <dm/uclass.h>
> +#include <atsha204a-i2c.h>
> +
> +#include "turris_atsha_otp.h"
> +
> +#define TURRIS_ATSHA_OTP_VERSION	0
> +#define TURRIS_ATSHA_OTP_SERIAL		1
> +#define TURRIS_ATSHA_OTP_MAC0		3
> +#define TURRIS_ATSHA_OTP_MAC1		4
> +
> +static struct udevice *get_atsha204a_dev(void)
> +{
> +	static struct udevice *dev;
> +
> +	if (dev)
> +		return dev;
> +
> +	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
> +		puts("Cannot find ATSHA204A on I2C bus!\n");
> +		dev = NULL;
> +	}
> +
> +	return dev;
> +}
> +
> +static void increment_mac(u8 *mac)
> +{
> +	int i;
> +
> +	for (i = 5; i >= 3; i--) {
> +		mac[i] += 1;
> +		if (mac[i])
> +			break;
> +	}
> +}
> +
> +static void set_mac_if_invalid(int i, u8 *mac)
> +{
> +	u8 oldmac[6];
> +
> +	if (is_valid_ethaddr(mac) &&
> +	    !eth_env_get_enetaddr_by_index("eth", i, oldmac))
> +		eth_env_set_enetaddr_by_index("eth", i, mac);
> +}
> +
> +int turris_atsha_otp_init_mac_addresses(void)
> +{
> +	struct udevice *dev = get_atsha204a_dev();
> +	u8 mac0[4], mac1[4], mac[6];
> +	int ret;
> +
> +	if (!dev)
> +		return -1;
> +
> +	ret = atsha204a_wakeup(dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> +			     TURRIS_ATSHA_OTP_MAC0, mac0);
> +	if (ret)
> +		return ret;
> +
> +	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> +			     TURRIS_ATSHA_OTP_MAC1, mac1);
> +	if (ret)
> +		return ret;
> +
> +	atsha204a_sleep(dev);
> +
> +	mac[0] = mac0[1];
> +	mac[1] = mac0[2];
> +	mac[2] = mac0[3];
> +	mac[3] = mac1[1];
> +	mac[4] = mac1[2];
> +	mac[5] = mac1[3];
> +
> +	set_mac_if_invalid(1, mac);
> +	increment_mac(mac);
> +	set_mac_if_invalid(2, mac);
> +	increment_mac(mac);
> +	set_mac_if_invalid(0, mac);
> +
> +	return 0;
> +}
> +
> +int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num)
> +{
> +	struct udevice *dev = get_atsha204a_dev();
> +	int ret;
> +
> +	if (!dev)
> +		return -1;
> +
> +	ret = atsha204a_wakeup(dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> +			     TURRIS_ATSHA_OTP_VERSION,
> +			     (u8 *)version_num);
> +	if (ret)
> +		return ret;
> +
> +	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> +			     TURRIS_ATSHA_OTP_SERIAL,
> +			     (u8 *)serial_num);
> +	if (ret)
> +		return ret;
> +
> +	atsha204a_sleep(dev);
> +	return 0;
> +}
> diff --git a/board/CZ.NIC/turris_atsha_otp.h b/board/CZ.NIC/turris_atsha_otp.h
> new file mode 100644
> index 000000000000..667d01af7310
> --- /dev/null
> +++ b/board/CZ.NIC/turris_atsha_otp.h
> @@ -0,0 +1,9 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +
> +#ifndef TURRIS_ATSHA_OTP_H
> +#define TURRIS_ATSHA_OTP_H
> +
> +int turris_atsha_otp_init_mac_addresses(void);
> +int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num);
> +
> +#endif
> diff --git a/board/CZ.NIC/turris_omnia/Makefile b/board/CZ.NIC/turris_omnia/Makefile
> index ccdf6c352cad..b79555ab4673 100644
> --- a/board/CZ.NIC/turris_omnia/Makefile
> +++ b/board/CZ.NIC/turris_omnia/Makefile
> @@ -2,4 +2,4 @@
>   #
>   # Copyright (C) 2017 Marek Behun <marek.behun@nic.cz>
>   
> -obj-y	:= turris_omnia.o
> +obj-y	:= turris_omnia.o ../turris_atsha_otp.o
> diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c b/board/CZ.NIC/turris_omnia/turris_omnia.c
> index 33cec6587e19..719e8750e60a 100644
> --- a/board/CZ.NIC/turris_omnia/turris_omnia.c
> +++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
> @@ -14,8 +14,6 @@
>   #include <log.h>
>   #include <miiphy.h>
>   #include <mtd.h>
> -#include <net.h>
> -#include <netdev.h>
>   #include <asm/global_data.h>
>   #include <asm/io.h>
>   #include <asm/arch/cpu.h>
> @@ -25,10 +23,10 @@
>   #include <time.h>
>   #include <linux/bitops.h>
>   #include <u-boot/crc.h>
> -# include <atsha204a-i2c.h>
>   
>   #include "../drivers/ddr/marvell/a38x/ddr3_init.h"
>   #include <../serdes/a38x/high_speed_env_spec.h>
> +#include "../turris_atsha_otp.h"
>   
>   DECLARE_GLOBAL_DATA_PTR;
>   
> @@ -71,11 +69,6 @@ enum status_word_bits {
>   	MSATA_IND_STSBIT	= 0x0020,
>   };
>   
> -#define OMNIA_ATSHA204_OTP_VERSION	0
> -#define OMNIA_ATSHA204_OTP_SERIAL	1
> -#define OMNIA_ATSHA204_OTP_MAC0		3
> -#define OMNIA_ATSHA204_OTP_MAC1		4
> -
>   /*
>    * Those values and defines are taken from the Marvell U-Boot version
>    * "u-boot-2013.01-2014_T3.0"
> @@ -594,49 +587,12 @@ int board_late_init(void)
>   	return 0;
>   }
>   
> -static struct udevice *get_atsha204a_dev(void)
> -{
> -	static struct udevice *dev;
> -
> -	if (dev)
> -		return dev;
> -
> -	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
> -		puts("Cannot find ATSHA204A on I2C bus!\n");
> -		dev = NULL;
> -	}
> -
> -	return dev;
> -}
> -
>   int show_board_info(void)
>   {
>   	u32 version_num, serial_num;
> -	int err = 1;
> -
> -	struct udevice *dev = get_atsha204a_dev();
> -
> -	if (dev) {
> -		err = atsha204a_wakeup(dev);
> -		if (err)
> -			goto out;
> -
> -		err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> -				     OMNIA_ATSHA204_OTP_VERSION,
> -				     (u8 *)&version_num);
> -		if (err)
> -			goto out;
> -
> -		err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> -				     OMNIA_ATSHA204_OTP_SERIAL,
> -				     (u8 *)&serial_num);
> -		if (err)
> -			goto out;
> -
> -		atsha204a_sleep(dev);
> -	}
> +	int err;
>   
> -out:
> +	err = turris_atsha_otp_get_serial_number(&version_num, &serial_num);
>   	printf("Model: Turris Omnia\n");
>   	printf("  RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
>   	if (err)
> @@ -648,65 +604,9 @@ out:
>   	return 0;
>   }
>   
> -static void increment_mac(u8 *mac)
> -{
> -	int i;
> -
> -	for (i = 5; i >= 3; i--) {
> -		mac[i] += 1;
> -		if (mac[i])
> -			break;
> -	}
> -}
> -
> -static void set_mac_if_invalid(int i, u8 *mac)
> -{
> -	u8 oldmac[6];
> -
> -	if (is_valid_ethaddr(mac) &&
> -	    !eth_env_get_enetaddr_by_index("eth", i, oldmac))
> -		eth_env_set_enetaddr_by_index("eth", i, mac);
> -}
> -
>   int misc_init_r(void)
>   {
> -	int err;
> -	struct udevice *dev = get_atsha204a_dev();
> -	u8 mac0[4], mac1[4], mac[6];
> -
> -	if (!dev)
> -		goto out;
> -
> -	err = atsha204a_wakeup(dev);
> -	if (err)
> -		goto out;
> -
> -	err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> -			     OMNIA_ATSHA204_OTP_MAC0, mac0);
> -	if (err)
> -		goto out;
> -
> -	err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
> -			     OMNIA_ATSHA204_OTP_MAC1, mac1);
> -	if (err)
> -		goto out;
> -
> -	atsha204a_sleep(dev);
> -
> -	mac[0] = mac0[1];
> -	mac[1] = mac0[2];
> -	mac[2] = mac0[3];
> -	mac[3] = mac1[1];
> -	mac[4] = mac1[2];
> -	mac[5] = mac1[3];
> -
> -	set_mac_if_invalid(1, mac);
> -	increment_mac(mac);
> -	set_mac_if_invalid(2, mac);
> -	increment_mac(mac);
> -	set_mac_if_invalid(0, mac);
> -
> -out:
> +	turris_atsha_otp_init_mac_addresses();
>   	return 0;
>   }
>   

Viele Grüße,
Stefan Roese

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/4] board: turris: Do not cache Atsha device in BSS
  2022-04-08 14:30 ` [PATCH 2/4] board: turris: Do not cache Atsha device in BSS Pali Rohár
  2022-04-08 17:45   ` Marek Behún
@ 2022-04-21 14:14   ` Stefan Roese
  1 sibling, 0 replies; 12+ messages in thread
From: Stefan Roese @ 2022-04-21 14:14 UTC (permalink / raw)
  To: Pali Rohár, Marek Behun; +Cc: u-boot

On 4/8/22 16:30, Pali Rohár wrote:
> Atsha device is used prior relocation and at this early stage BSS does not
> have to be ready yet. So do not cache Atsha device in BSS.
> 
> Fixes support for other Turris routers.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Applied to u-boot-marvell/master

Thanks,
Stefan

> ---
>   board/CZ.NIC/turris_atsha_otp.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
> index a4a77c74fb19..840721a9b737 100644
> --- a/board/CZ.NIC/turris_atsha_otp.c
> +++ b/board/CZ.NIC/turris_atsha_otp.c
> @@ -18,10 +18,8 @@
>   
>   static struct udevice *get_atsha204a_dev(void)
>   {
> -	static struct udevice *dev;
> -
> -	if (dev)
> -		return dev;
> +	/* Cannot be static because BSS does not have to be ready at this early stage */
> +	struct udevice *dev;
>   
>   	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
>   		puts("Cannot find ATSHA204A on I2C bus!\n");

Viele Grüße,
Stefan Roese

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address
  2022-04-08 14:30 ` [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address Pali Rohár
  2022-04-08 17:45   ` Marek Behún
@ 2022-04-21 14:15   ` Stefan Roese
  1 sibling, 0 replies; 12+ messages in thread
From: Stefan Roese @ 2022-04-21 14:15 UTC (permalink / raw)
  To: Pali Rohár, Marek Behun; +Cc: u-boot

On 4/8/22 16:30, Pali Rohár wrote:
> Turris Omnia uses first MAC address from OTP for second ethernet interface.
> Second MAC address for third interface and third MAC address for first
> interface.
> 
> Other Turris routers do not have this rotate by one mapping. So add
> function parameter for specifying id of the first ethernet interface.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Applied to u-boot-marvell/master

Thanks,
Stefan

> ---
>   board/CZ.NIC/turris_atsha_otp.c          | 8 ++++----
>   board/CZ.NIC/turris_atsha_otp.h          | 2 +-
>   board/CZ.NIC/turris_omnia/turris_omnia.c | 2 +-
>   3 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
> index 840721a9b737..7a39b7f61d5d 100644
> --- a/board/CZ.NIC/turris_atsha_otp.c
> +++ b/board/CZ.NIC/turris_atsha_otp.c
> @@ -49,7 +49,7 @@ static void set_mac_if_invalid(int i, u8 *mac)
>   		eth_env_set_enetaddr_by_index("eth", i, mac);
>   }
>   
> -int turris_atsha_otp_init_mac_addresses(void)
> +int turris_atsha_otp_init_mac_addresses(int first_idx)
>   {
>   	struct udevice *dev = get_atsha204a_dev();
>   	u8 mac0[4], mac1[4], mac[6];
> @@ -81,11 +81,11 @@ int turris_atsha_otp_init_mac_addresses(void)
>   	mac[4] = mac1[2];
>   	mac[5] = mac1[3];
>   
> -	set_mac_if_invalid(1, mac);
> +	set_mac_if_invalid((first_idx + 0) % 3, mac);
>   	increment_mac(mac);
> -	set_mac_if_invalid(2, mac);
> +	set_mac_if_invalid((first_idx + 1) % 3, mac);
>   	increment_mac(mac);
> -	set_mac_if_invalid(0, mac);
> +	set_mac_if_invalid((first_idx + 2) % 3, mac);
>   
>   	return 0;
>   }
> diff --git a/board/CZ.NIC/turris_atsha_otp.h b/board/CZ.NIC/turris_atsha_otp.h
> index 667d01af7310..bd4308fdc3ef 100644
> --- a/board/CZ.NIC/turris_atsha_otp.h
> +++ b/board/CZ.NIC/turris_atsha_otp.h
> @@ -3,7 +3,7 @@
>   #ifndef TURRIS_ATSHA_OTP_H
>   #define TURRIS_ATSHA_OTP_H
>   
> -int turris_atsha_otp_init_mac_addresses(void);
> +int turris_atsha_otp_init_mac_addresses(int first_idx);
>   int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num);
>   
>   #endif
> diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c b/board/CZ.NIC/turris_omnia/turris_omnia.c
> index 719e8750e60a..da2fee578c44 100644
> --- a/board/CZ.NIC/turris_omnia/turris_omnia.c
> +++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
> @@ -606,7 +606,7 @@ int show_board_info(void)
>   
>   int misc_init_r(void)
>   {
> -	turris_atsha_otp_init_mac_addresses();
> +	turris_atsha_otp_init_mac_addresses(1);
>   	return 0;
>   }
>   

Viele Grüße,
Stefan Roese

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64
  2022-04-08 14:30 ` [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64 Pali Rohár
  2022-04-08 17:46   ` Marek Behún
@ 2022-04-21 14:15   ` Stefan Roese
  1 sibling, 0 replies; 12+ messages in thread
From: Stefan Roese @ 2022-04-21 14:15 UTC (permalink / raw)
  To: Pali Rohár, Marek Behun; +Cc: u-boot

On 4/8/22 16:30, Pali Rohár wrote:
> DT node name should be generic, therefore rename atsha204a@64 to crypto@64.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Applied to u-boot-marvell/master

Thanks,
Stefan

> ---
>   arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi | 2 +-
>   board/CZ.NIC/turris_atsha_otp.c                  | 2 +-
>   2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi b/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi
> index 3ff76c946294..76a441524833 100644
> --- a/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi
> +++ b/arch/arm/dts/armada-385-turris-omnia-u-boot.dtsi
> @@ -29,7 +29,7 @@
>   			u-boot,dm-pre-reloc;
>   
>   			/* ATSHA204A at address 0x64 */
> -			atsha204a@64 {
> +			crypto@64 {
>   				u-boot,dm-pre-reloc;
>   				compatible = "atmel,atsha204a";
>   				reg = <0x64>;
> diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
> index 7a39b7f61d5d..8c39f5e52414 100644
> --- a/board/CZ.NIC/turris_atsha_otp.c
> +++ b/board/CZ.NIC/turris_atsha_otp.c
> @@ -21,7 +21,7 @@ static struct udevice *get_atsha204a_dev(void)
>   	/* Cannot be static because BSS does not have to be ready at this early stage */
>   	struct udevice *dev;
>   
> -	if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
> +	if (uclass_get_device_by_name(UCLASS_MISC, "crypto@64", &dev)) {
>   		puts("Cannot find ATSHA204A on I2C bus!\n");
>   		dev = NULL;
>   	}

Viele Grüße,
Stefan Roese

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-04-21 14:15 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-08 14:30 [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Pali Rohár
2022-04-08 14:30 ` [PATCH 2/4] board: turris: Do not cache Atsha device in BSS Pali Rohár
2022-04-08 17:45   ` Marek Behún
2022-04-21 14:14   ` Stefan Roese
2022-04-08 14:30 ` [PATCH 3/4] board: turris: Allow to specify first eth idx of first MAC address Pali Rohár
2022-04-08 17:45   ` Marek Behún
2022-04-21 14:15   ` Stefan Roese
2022-04-08 14:30 ` [PATCH 4/4] board: turris: Rename atsha204a@64 DT node to crypto@64 Pali Rohár
2022-04-08 17:46   ` Marek Behún
2022-04-21 14:15   ` Stefan Roese
2022-04-08 17:45 ` [PATCH 1/4] board: turris: Move Turris Atsha OTP code to separate file Marek Behún
2022-04-21 14:14 ` Stefan Roese

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.