LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH v2 0/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
@ 2021-05-19  9:50 Emmanuel Gil Peyrot
  2021-05-19  9:50 ` [PATCH v2 1/4] " Emmanuel Gil Peyrot
                   ` (4 more replies)
  0 siblings, 5 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-05-19  9:50 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

The OTP is a read-only memory area which contains various keys and
signatures used to decrypt, encrypt or verify various pieces of storage.

Its size depends on the console, it is 128 bytes on the Wii and
1024 bytes on the Wii U (split into eight 128 bytes banks).

It can be used directly by writing into one register and reading from
the other one, without any additional synchronisation.

This series has only been tested on the Wii U so far, using the
downstream 4.19 branch from linux-wiiu[1], but it should also work on
the Wii on mainline.

[1] https://gitlab.com/linux-wiiu/linux-wiiu

Changes since v1:
- Fixed the commit messages so they can be accepted by other email
  servers, sorry about that.

Emmanuel Gil Peyrot (4):
  nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  powerpc: wii.dts: Expose the OTP on this platform
  powerpc: wii_defconfig: Enable OTP by default

 .../bindings/nvmem/nintendo-otp.txt           |  14 +++
 arch/powerpc/boot/dts/wii.dts                 |   5 +
 arch/powerpc/configs/wii_defconfig            |   1 +
 drivers/nvmem/Kconfig                         |  11 ++
 drivers/nvmem/Makefile                        |   2 +
 drivers/nvmem/nintendo-otp.c                  | 115 ++++++++++++++++++
 6 files changed, 148 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
 create mode 100644 drivers/nvmem/nintendo-otp.c

-- 
2.31.1


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

* [PATCH v2 1/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  2021-05-19  9:50 [PATCH v2 0/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
@ 2021-05-19  9:50 ` Emmanuel Gil Peyrot
  2021-06-26 23:24   ` Jonathan Neuschäfer
  2021-05-19  9:50 ` [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-05-19  9:50 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This OTP is read-only and contains various keys used by the console to
decrypt, encrypt or verify various pieces of storage.

Its size depends on the console, it is 128 bytes on the Wii and
1024 bytes on the Wii U (split into eight 128 bytes banks).

It can be used directly by writing into one register and reading from
the other one, without any additional synchronisation.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/nvmem/Kconfig        |  11 ++++
 drivers/nvmem/Makefile       |   2 +
 drivers/nvmem/nintendo-otp.c | 115 +++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 drivers/nvmem/nintendo-otp.c

diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index dd2019006838..dd6196e49b2d 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -107,6 +107,17 @@ config MTK_EFUSE
 	  This driver can also be built as a module. If so, the module
 	  will be called efuse-mtk.
 
+config NVMEM_NINTENDO_OTP
+	tristate "Nintendo Wii and Wii U OTP Support"
+	help
+	  This is a driver to expose the OTP on a Nintendo Wii or Wii U.
+
+	  This memory contains common and per-console keys, signatures and
+	  related data required to access peripherals.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called nvmem-nintendo-otp.
+
 config QCOM_QFPROM
 	tristate "QCOM QFPROM Support"
 	depends on ARCH_QCOM || COMPILE_TEST
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index bbea1410240a..dcbbde35b6a8 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -23,6 +23,8 @@ obj-$(CONFIG_NVMEM_LPC18XX_OTP)	+= nvmem_lpc18xx_otp.o
 nvmem_lpc18xx_otp-y		:= lpc18xx_otp.o
 obj-$(CONFIG_NVMEM_MXS_OCOTP)	+= nvmem-mxs-ocotp.o
 nvmem-mxs-ocotp-y		:= mxs-ocotp.o
+obj-$(CONFIG_NVMEM_NINTENDO_OTP)	+= nvmem-nintendo-otp.o
+nvmem-nintendo-otp-y		:= nintendo-otp.o
 obj-$(CONFIG_MTK_EFUSE)		+= nvmem_mtk-efuse.o
 nvmem_mtk-efuse-y		:= mtk-efuse.o
 obj-$(CONFIG_QCOM_QFPROM)	+= nvmem_qfprom.o
diff --git a/drivers/nvmem/nintendo-otp.c b/drivers/nvmem/nintendo-otp.c
new file mode 100644
index 000000000000..de6f5d7c10ef
--- /dev/null
+++ b/drivers/nvmem/nintendo-otp.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Nintendo Wii and Wii U OTP driver
+ *
+ * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ */
+
+#include <linux/device.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/nvmem-provider.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+
+#define HW_OTPCMD  0
+#define HW_OTPDATA 4
+#define OTP_READ   0x80000000
+
+struct nintendo_otp_priv {
+	void __iomem *regs;
+};
+
+struct nintendo_otp_devtype_data {
+	const char *name;
+	unsigned int num_banks;
+};
+
+static const struct nintendo_otp_devtype_data hollywood_otp_data = {
+	.name = "wii-otp",
+	.num_banks = 1,
+};
+
+static const struct nintendo_otp_devtype_data latte_otp_data = {
+	.name = "wiiu-otp",
+	.num_banks = 8,
+};
+
+static int nintendo_otp_reg_read(void *context,
+				 unsigned int reg, void *_val, size_t bytes)
+{
+	struct nintendo_otp_priv *priv = context;
+	u32 *val = _val;
+	int words = bytes >> 2;
+	u32 bank, addr;
+
+	while (words--) {
+		bank = (reg << 1) & ~0xff;
+		addr = (reg >> 2) & 0x1f;
+		iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD);
+		*val++ = ioread32be(priv->regs + HW_OTPDATA);
+		reg += 4;
+	}
+
+	return 0;
+}
+
+static const struct of_device_id nintendo_otp_of_table[] = {
+	{ .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data },
+	{ .compatible = "nintendo,latte-otp", .data = &latte_otp_data },
+	{/* sentinel */},
+};
+MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
+
+static int nintendo_otp_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	const struct of_device_id *of_id =
+		of_match_device(nintendo_otp_of_table, dev);
+	struct resource *res;
+	struct nvmem_device *nvmem;
+	struct nintendo_otp_priv *priv;
+
+	struct nvmem_config config = {
+		.stride = 4,
+		.word_size = 4,
+		.reg_read = nintendo_otp_reg_read,
+		.read_only = true,
+		.root_only = true,
+	};
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->regs = devm_ioremap_resource(dev, res);
+	if (IS_ERR(priv->regs))
+		return PTR_ERR(priv->regs);
+
+	if (of_id->data) {
+		const struct nintendo_otp_devtype_data *data = of_id->data;
+		config.name = data->name;
+		config.size = data->num_banks * 128;
+	}
+
+	config.dev = dev;
+	config.priv = priv;
+
+	nvmem = devm_nvmem_register(dev, &config);
+
+	return PTR_ERR_OR_ZERO(nvmem);
+}
+
+static struct platform_driver nintendo_otp_driver = {
+	.probe = nintendo_otp_probe,
+	.driver = {
+		.name = "nintendo-otp",
+		.of_match_table = nintendo_otp_of_table,
+	},
+};
+module_platform_driver(nintendo_otp_driver);
+MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
+MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
+MODULE_LICENSE("GPL v2");
-- 
2.31.1


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

* [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  2021-05-19  9:50 [PATCH v2 0/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  2021-05-19  9:50 ` [PATCH v2 1/4] " Emmanuel Gil Peyrot
@ 2021-05-19  9:50 ` Emmanuel Gil Peyrot
  2021-05-21  1:37   ` Rob Herring
  2021-06-26 21:27   ` Jonathan Neuschäfer
  2021-05-19  9:50 ` [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-05-19  9:50 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

Both of these consoles use the exact same two registers, even at the
same address, but the Wii U has eight banks of 128 bytes memory while
the Wii only has one, hence the two compatible strings.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 .../devicetree/bindings/nvmem/nintendo-otp.txt     | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.txt

diff --git a/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt b/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
new file mode 100644
index 000000000000..b26d705ec52d
--- /dev/null
+++ b/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
@@ -0,0 +1,14 @@
+Nintendo Wii and Wii U OTP
+
+Required Properties:
+- compatible: depending on the console this should be one of:
+	- "nintendo,hollywood-otp" for the Wii
+	- "nintendo,latte-otp" for the Wii U
+- reg: base address and size of the OTP registers
+
+
+Example:
+	otp@d8001ec {
+		compatible = "nintendo,latte-otp";
+		reg = <0x0d8001ec 0x8>;
+	};
-- 
2.31.1


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

* [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform
  2021-05-19  9:50 [PATCH v2 0/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  2021-05-19  9:50 ` [PATCH v2 1/4] " Emmanuel Gil Peyrot
  2021-05-19  9:50 ` [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
@ 2021-05-19  9:50 ` Emmanuel Gil Peyrot
  2021-06-26 23:34   ` Jonathan Neuschäfer
  2021-05-19  9:50 ` [PATCH v2 4/4] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  4 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-05-19  9:50 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This can be used by the newly-added nintendo-otp nvmem module.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/boot/dts/wii.dts | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
index aaa381da1906..7837c4a3f09c 100644
--- a/arch/powerpc/boot/dts/wii.dts
+++ b/arch/powerpc/boot/dts/wii.dts
@@ -219,6 +219,11 @@ control@d800100 {
 			reg = <0x0d800100 0x300>;
 		};
 
+		otp@d8001ec {
+			compatible = "nintendo,hollywood-otp";
+			reg = <0x0d8001ec 0x8>;
+		};
+
 		disk@d806000 {
 			compatible = "nintendo,hollywood-di";
 			reg = <0x0d806000 0x40>;
-- 
2.31.1


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

* [PATCH v2 4/4] powerpc: wii_defconfig: Enable OTP by default
  2021-05-19  9:50 [PATCH v2 0/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                   ` (2 preceding siblings ...)
  2021-05-19  9:50 ` [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
@ 2021-05-19  9:50 ` Emmanuel Gil Peyrot
  2021-06-26 23:38   ` Jonathan Neuschäfer
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  4 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-05-19  9:50 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This selects the nintendo-otp module when building for this platform, if
CONFIG_NVMEM is also selected.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/configs/wii_defconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/powerpc/configs/wii_defconfig b/arch/powerpc/configs/wii_defconfig
index 379c171f3ddd..a0c45bf2bfb1 100644
--- a/arch/powerpc/configs/wii_defconfig
+++ b/arch/powerpc/configs/wii_defconfig
@@ -99,6 +99,7 @@ CONFIG_LEDS_TRIGGER_HEARTBEAT=y
 CONFIG_LEDS_TRIGGER_PANIC=y
 CONFIG_RTC_CLASS=y
 CONFIG_RTC_DRV_GENERIC=y
+CONFIG_NVMEM_NINTENDO_OTP=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_FUSE_FS=m
-- 
2.31.1


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

* Re: [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  2021-05-19  9:50 ` [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
@ 2021-05-21  1:37   ` Rob Herring
  2021-06-26 21:27   ` Jonathan Neuschäfer
  1 sibling, 0 replies; 28+ messages in thread
From: Rob Herring @ 2021-05-21  1:37 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Srinivas Kandagatla, linuxppc-dev, devicetree, Ash Logan,
	Jonathan Neuschäfer, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

On Wed, May 19, 2021 at 11:50:42AM +0200, Emmanuel Gil Peyrot wrote:
> Both of these consoles use the exact same two registers, even at the
> same address, but the Wii U has eight banks of 128 bytes memory while
> the Wii only has one, hence the two compatible strings.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> ---
>  .../devicetree/bindings/nvmem/nintendo-otp.txt     | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.txt

Bindings should be in DT schema format now.

> 
> diff --git a/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt b/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
> new file mode 100644
> index 000000000000..b26d705ec52d
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
> @@ -0,0 +1,14 @@
> +Nintendo Wii and Wii U OTP
> +
> +Required Properties:
> +- compatible: depending on the console this should be one of:
> +	- "nintendo,hollywood-otp" for the Wii
> +	- "nintendo,latte-otp" for the Wii U
> +- reg: base address and size of the OTP registers
> +
> +
> +Example:
> +	otp@d8001ec {
> +		compatible = "nintendo,latte-otp";
> +		reg = <0x0d8001ec 0x8>;
> +	};
> -- 
> 2.31.1
> 

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

* Re: [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  2021-05-19  9:50 ` [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
  2021-05-21  1:37   ` Rob Herring
@ 2021-06-26 21:27   ` Jonathan Neuschäfer
  1 sibling, 0 replies; 28+ messages in thread
From: Jonathan Neuschäfer @ 2021-06-26 21:27 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Srinivas Kandagatla, linuxppc-dev, devicetree, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1408 bytes --]

Hello and sorry for the delay,

On Wed, May 19, 2021 at 11:50:42AM +0200, Emmanuel Gil Peyrot wrote:
> Both of these consoles use the exact same two registers, even at the
> same address, but the Wii U has eight banks of 128 bytes memory while
> the Wii only has one, hence the two compatible strings.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

A link to the (third-party) documentation for the OTP device would be nice.


Best regards,
Jonathan Neuschäfer

> ---
>  .../devicetree/bindings/nvmem/nintendo-otp.txt     | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
> 
> diff --git a/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt b/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
> new file mode 100644
> index 000000000000..b26d705ec52d
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/nvmem/nintendo-otp.txt
> @@ -0,0 +1,14 @@
> +Nintendo Wii and Wii U OTP
> +
> +Required Properties:
> +- compatible: depending on the console this should be one of:
> +	- "nintendo,hollywood-otp" for the Wii
> +	- "nintendo,latte-otp" for the Wii U
> +- reg: base address and size of the OTP registers
> +
> +
> +Example:
> +	otp@d8001ec {
> +		compatible = "nintendo,latte-otp";
> +		reg = <0x0d8001ec 0x8>;
> +	};
> -- 
> 2.31.1
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v2 1/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  2021-05-19  9:50 ` [PATCH v2 1/4] " Emmanuel Gil Peyrot
@ 2021-06-26 23:24   ` Jonathan Neuschäfer
  0 siblings, 0 replies; 28+ messages in thread
From: Jonathan Neuschäfer @ 2021-06-26 23:24 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Srinivas Kandagatla, linuxppc-dev, devicetree, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2612 bytes --]

Hi,


On Wed, May 19, 2021 at 11:50:41AM +0200, Emmanuel Gil Peyrot wrote:
> This OTP is read-only and contains various keys used by the console to
> decrypt, encrypt or verify various pieces of storage.
> 
> Its size depends on the console, it is 128 bytes on the Wii and
> 1024 bytes on the Wii U (split into eight 128 bytes banks).
> 
> It can be used directly by writing into one register and reading from
> the other one, without any additional synchronisation.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> ---

A link to the (third-party) documentation would be nice, either in the
commit message or in the code itself.

(https://wiiubrew.org/wiki/Hardware/OTP i guess)

[...]
> +static int nintendo_otp_reg_read(void *context,
> +				 unsigned int reg, void *_val, size_t bytes)
> +{
> +	struct nintendo_otp_priv *priv = context;
> +	u32 *val = _val;
> +	int words = bytes >> 2;
> +	u32 bank, addr;
> +
> +	while (words--) {
> +		bank = (reg << 1) & ~0xff;

This is a bit non-obvious, IMHO. As far as I understand it, the expanded
formula is:

	bank = (reg / 128) << 8;

I.e. first divide by bank size, then shift the parameter into the right
place.

> +		addr = (reg >> 2) & 0x1f;

Here, I think it's about the word size (4 bytes); I think / 4 would be
clearer.

I *think* (but haven't checked) that gcc should generate efficent shifts
for the divisions above, so using the division operator shouldn't be
problem.

> +		iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD);
> +		*val++ = ioread32be(priv->regs + HW_OTPDATA);
> +		reg += 4;
> +	}
> +
> +	return 0;
> +}
> +
[...]
> +	if (of_id->data) {
> +		const struct nintendo_otp_devtype_data *data = of_id->data;
> +		config.name = data->name;
> +		config.size = data->num_banks * 128;

Given that 128 appears a few times, perhaps a #define would be good.

> +	}
> +
> +	config.dev = dev;
> +	config.priv = priv;
> +
> +	nvmem = devm_nvmem_register(dev, &config);
> +
> +	return PTR_ERR_OR_ZERO(nvmem);
> +}
> +
> +static struct platform_driver nintendo_otp_driver = {
> +	.probe = nintendo_otp_probe,
> +	.driver = {
> +		.name = "nintendo-otp",
> +		.of_match_table = nintendo_otp_of_table,
> +	},
> +};
> +module_platform_driver(nintendo_otp_driver);
> +MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
> +MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
> +MODULE_LICENSE("GPL v2");
> -- 
> 2.31.1
> 

Tested-by: Jonathan Neuschäfer <j.ne@posteo.net>  # on Wii



Thanks,
Jonathan Neuschäfer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform
  2021-05-19  9:50 ` [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
@ 2021-06-26 23:34   ` Jonathan Neuschäfer
  2021-07-01 19:56     ` Emmanuel Gil Peyrot
  0 siblings, 1 reply; 28+ messages in thread
From: Jonathan Neuschäfer @ 2021-06-26 23:34 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Srinivas Kandagatla, linuxppc-dev, devicetree, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1149 bytes --]

On Wed, May 19, 2021 at 11:50:43AM +0200, Emmanuel Gil Peyrot wrote:
> This can be used by the newly-added nintendo-otp nvmem module.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> ---
>  arch/powerpc/boot/dts/wii.dts | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
> index aaa381da1906..7837c4a3f09c 100644
> --- a/arch/powerpc/boot/dts/wii.dts
> +++ b/arch/powerpc/boot/dts/wii.dts
> @@ -219,6 +219,11 @@ control@d800100 {
>  			reg = <0x0d800100 0x300>;
>  		};
>  
> +		otp@d8001ec {
> +			compatible = "nintendo,hollywood-otp";
> +			reg = <0x0d8001ec 0x8>;

The OTP registers overlap with the previous node, control@d800100.
Not sure what's the best way to structure the devicetree in this case,
maybe something roughly like the following (untested, unverified):

	control@d800100 {
		compatible = "nintendo,hollywood-control", "simple-mfd";
		reg = <0x0d800100 0x300>;
		ranges;

		otp@d8001ec {
			compatible = "nintendo,hollywood-otp";
			reg = <0x0d8001ec 0x8>;
		};
	};



Thanks,
Jonathan Neuschäfer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v2 4/4] powerpc: wii_defconfig: Enable OTP by default
  2021-05-19  9:50 ` [PATCH v2 4/4] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
@ 2021-06-26 23:38   ` Jonathan Neuschäfer
  0 siblings, 0 replies; 28+ messages in thread
From: Jonathan Neuschäfer @ 2021-06-26 23:38 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Srinivas Kandagatla, linuxppc-dev, devicetree, Ash Logan,
	Jonathan Neuschäfer, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 308 bytes --]

On Wed, May 19, 2021 at 11:50:44AM +0200, Emmanuel Gil Peyrot wrote:
> This selects the nintendo-otp module when building for this platform, if
> CONFIG_NVMEM is also selected.

The 'if' is a bit confusing. CONFIG_NVRAM=y has indeed been in
wii_defconfig since 2009.


Thanks,
Jonathan Neuschäfer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform
  2021-06-26 23:34   ` Jonathan Neuschäfer
@ 2021-07-01 19:56     ` Emmanuel Gil Peyrot
  2021-07-02  8:56       ` Jonathan Neuschäfer
  0 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-07-01 19:56 UTC (permalink / raw)
  To: Jonathan Neuschäfer
  Cc: Emmanuel Gil Peyrot, Srinivas Kandagatla, linuxppc-dev,
	devicetree, Ash Logan, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1552 bytes --]

On Sat, Jun 26, 2021 at 11:34:01PM +0000, Jonathan Neuschäfer wrote:
> On Wed, May 19, 2021 at 11:50:43AM +0200, Emmanuel Gil Peyrot wrote:
> > This can be used by the newly-added nintendo-otp nvmem module.
> > 
> > Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> > ---
> >  arch/powerpc/boot/dts/wii.dts | 5 +++++
> >  1 file changed, 5 insertions(+)
> > 
> > diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
> > index aaa381da1906..7837c4a3f09c 100644
> > --- a/arch/powerpc/boot/dts/wii.dts
> > +++ b/arch/powerpc/boot/dts/wii.dts
> > @@ -219,6 +219,11 @@ control@d800100 {
> >  			reg = <0x0d800100 0x300>;
> >  		};
> >  
> > +		otp@d8001ec {
> > +			compatible = "nintendo,hollywood-otp";
> > +			reg = <0x0d8001ec 0x8>;
> 
> The OTP registers overlap with the previous node, control@d800100.
> Not sure what's the best way to structure the devicetree in this case,
> maybe something roughly like the following (untested, unverified):
[snip]

I couldn’t get this to work, but additionally it looks like it should
start 0x100 earlier and contain pic1@d800030 and gpio@d8000c0, given
https://wiibrew.org/wiki/Hardware/Hollywood_Registers

Would it make sense, for the time being, to reduce the size of this
control@d800100 device to the single register currently being used by
arch/powerpc/platforms/embedded6xx/wii.c (0xd800194, used to reboot the
system) and leave the refactor of restart + OTP + PIC + GPIO for a
future series?

Thanks,

-- 
Emmanuel Gil Peyrot

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  2021-05-19  9:50 [PATCH v2 0/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                   ` (3 preceding siblings ...)
  2021-05-19  9:50 ` [PATCH v2 4/4] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
@ 2021-07-01 22:57 ` Emmanuel Gil Peyrot
  2021-07-01 22:57   ` [PATCH v3 1/5] " Emmanuel Gil Peyrot
                     ` (5 more replies)
  4 siblings, 6 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-07-01 22:57 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

The OTP is a read-only memory area which contains various keys and
signatures used to decrypt, encrypt or verify various pieces of storage.

Its size depends on the console, it is 128 bytes on the Wii and
1024 bytes on the Wii U (split into eight 128 bytes banks).

It can be used directly by writing into one register and reading from
the other one, without any additional synchronisation.

This series has been tested on both the Wii U (using my downstream
master-wiiu branch[1]), as well as on the Wii on mainline.

[1] https://gitlab.com/linkmauve/linux-wiiu/-/commits/master-wiiu

Changes since v1:
- Fixed the commit messages so they can be accepted by other email
  servers, sorry about that.

Changes since v2:
- Switched the dt binding documentation to YAML.
- Used more obvious register arithmetic, and tested that gcc (at -O1 and
  above) outputs the exact same rlwinm instructions for them.
- Use more #defines to make the code easier to read.
- Include some links to the reversed documentation.
- Avoid overlapping dt regions by changing the existing control@d800100
  node to end before the OTP registers, with some bigger dt refactoring
  left for a future series.

Emmanuel Gil Peyrot (5):
  nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  powerpc: wii.dts: Reduce the size of the control area
  powerpc: wii.dts: Expose the OTP on this platform
  powerpc: wii_defconfig: Enable OTP by default

 .../bindings/nvmem/nintendo-otp.yaml          |  44 +++++++
 arch/powerpc/boot/dts/wii.dts                 |  13 +-
 arch/powerpc/configs/wii_defconfig            |   1 +
 drivers/nvmem/Kconfig                         |  11 ++
 drivers/nvmem/Makefile                        |   2 +
 drivers/nvmem/nintendo-otp.c                  | 124 ++++++++++++++++++
 6 files changed, 194 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
 create mode 100644 drivers/nvmem/nintendo-otp.c

-- 
2.32.0


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

* [PATCH v3 1/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
@ 2021-07-01 22:57   ` Emmanuel Gil Peyrot
  2021-07-01 22:57   ` [PATCH v3 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-07-01 22:57 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This OTP is read-only and contains various keys used by the console to
decrypt, encrypt or verify various pieces of storage.

Its size depends on the console, it is 128 bytes on the Wii and
1024 bytes on the Wii U (split into eight 128 bytes banks).

It can be used directly by writing into one register and reading from
the other one, without any additional synchronisation.

This driver was written based on reversed documentation, see:
https://wiiubrew.org/wiki/Hardware/OTP

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Tested-by: Jonathan Neuschäfer <j.ne@posteo.net>  # on Wii
Tested-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>  # on Wii U
---
 drivers/nvmem/Kconfig        |  11 ++++
 drivers/nvmem/Makefile       |   2 +
 drivers/nvmem/nintendo-otp.c | 124 +++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 drivers/nvmem/nintendo-otp.c

diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index dd2019006838..39854d43758b 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -107,6 +107,17 @@ config MTK_EFUSE
 	  This driver can also be built as a module. If so, the module
 	  will be called efuse-mtk.
 
+config NVMEM_NINTENDO_OTP
+	tristate "Nintendo Wii and Wii U OTP Support"
+	help
+	  This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
+
+	  This memory contains common and per-console keys, signatures and
+	  related data required to access peripherals.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called nvmem-nintendo-otp.
+
 config QCOM_QFPROM
 	tristate "QCOM QFPROM Support"
 	depends on ARCH_QCOM || COMPILE_TEST
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index bbea1410240a..dcbbde35b6a8 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -23,6 +23,8 @@ obj-$(CONFIG_NVMEM_LPC18XX_OTP)	+= nvmem_lpc18xx_otp.o
 nvmem_lpc18xx_otp-y		:= lpc18xx_otp.o
 obj-$(CONFIG_NVMEM_MXS_OCOTP)	+= nvmem-mxs-ocotp.o
 nvmem-mxs-ocotp-y		:= mxs-ocotp.o
+obj-$(CONFIG_NVMEM_NINTENDO_OTP)	+= nvmem-nintendo-otp.o
+nvmem-nintendo-otp-y		:= nintendo-otp.o
 obj-$(CONFIG_MTK_EFUSE)		+= nvmem_mtk-efuse.o
 nvmem_mtk-efuse-y		:= mtk-efuse.o
 obj-$(CONFIG_QCOM_QFPROM)	+= nvmem_qfprom.o
diff --git a/drivers/nvmem/nintendo-otp.c b/drivers/nvmem/nintendo-otp.c
new file mode 100644
index 000000000000..33961b17f9f1
--- /dev/null
+++ b/drivers/nvmem/nintendo-otp.c
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Nintendo Wii and Wii U OTP driver
+ *
+ * This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
+ *
+ * This memory contains common and per-console keys, signatures and
+ * related data required to access peripherals.
+ *
+ * Based on reversed documentation from https://wiiubrew.org/wiki/Hardware/OTP
+ *
+ * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ */
+
+#include <linux/device.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/nvmem-provider.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+
+#define HW_OTPCMD  0
+#define HW_OTPDATA 4
+#define OTP_READ   0x80000000
+#define BANK_SIZE  128
+#define WORD_SIZE  4
+
+struct nintendo_otp_priv {
+	void __iomem *regs;
+};
+
+struct nintendo_otp_devtype_data {
+	const char *name;
+	unsigned int num_banks;
+};
+
+static const struct nintendo_otp_devtype_data hollywood_otp_data = {
+	.name = "wii-otp",
+	.num_banks = 1,
+};
+
+static const struct nintendo_otp_devtype_data latte_otp_data = {
+	.name = "wiiu-otp",
+	.num_banks = 8,
+};
+
+static int nintendo_otp_reg_read(void *context,
+				 unsigned int reg, void *_val, size_t bytes)
+{
+	struct nintendo_otp_priv *priv = context;
+	u32 *val = _val;
+	int words = bytes / WORD_SIZE;
+	u32 bank, addr;
+
+	while (words--) {
+		bank = (reg / BANK_SIZE) << 8;
+		addr = (reg / WORD_SIZE) % (BANK_SIZE / WORD_SIZE);
+		iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD);
+		*val++ = ioread32be(priv->regs + HW_OTPDATA);
+		reg += WORD_SIZE;
+	}
+
+	return 0;
+}
+
+static const struct of_device_id nintendo_otp_of_table[] = {
+	{ .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data },
+	{ .compatible = "nintendo,latte-otp", .data = &latte_otp_data },
+	{/* sentinel */},
+};
+MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
+
+static int nintendo_otp_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	const struct of_device_id *of_id =
+		of_match_device(nintendo_otp_of_table, dev);
+	struct resource *res;
+	struct nvmem_device *nvmem;
+	struct nintendo_otp_priv *priv;
+
+	struct nvmem_config config = {
+		.stride = WORD_SIZE,
+		.word_size = WORD_SIZE,
+		.reg_read = nintendo_otp_reg_read,
+		.read_only = true,
+		.root_only = true,
+	};
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->regs = devm_ioremap_resource(dev, res);
+	if (IS_ERR(priv->regs))
+		return PTR_ERR(priv->regs);
+
+	if (of_id->data) {
+		const struct nintendo_otp_devtype_data *data = of_id->data;
+		config.name = data->name;
+		config.size = data->num_banks * BANK_SIZE;
+	}
+
+	config.dev = dev;
+	config.priv = priv;
+
+	nvmem = devm_nvmem_register(dev, &config);
+
+	return PTR_ERR_OR_ZERO(nvmem);
+}
+
+static struct platform_driver nintendo_otp_driver = {
+	.probe = nintendo_otp_probe,
+	.driver = {
+		.name = "nintendo-otp",
+		.of_match_table = nintendo_otp_of_table,
+	},
+};
+module_platform_driver(nintendo_otp_driver);
+MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
+MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
+MODULE_LICENSE("GPL v2");
-- 
2.32.0


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

* [PATCH v3 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  2021-07-01 22:57   ` [PATCH v3 1/5] " Emmanuel Gil Peyrot
@ 2021-07-01 22:57   ` Emmanuel Gil Peyrot
  2021-07-14 22:51     ` Rob Herring
  2021-07-01 22:57   ` [PATCH v3 3/5] powerpc: wii.dts: Reduce the size of the control area Emmanuel Gil Peyrot
                     ` (3 subsequent siblings)
  5 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-07-01 22:57 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

Both of these consoles use the exact same two registers, even at the
same address, but the Wii U has eight banks of 128 bytes memory while
the Wii only has one, hence the two compatible strings.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 .../bindings/nvmem/nintendo-otp.yaml          | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml

diff --git a/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml b/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
new file mode 100644
index 000000000000..c39bd64b03b9
--- /dev/null
+++ b/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/nvmem/nintendo-otp.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nintendo Wii and Wii U OTP Device Tree Bindings
+
+description: |
+  This binding represents the OTP memory as found on a Nintendo Wii or Wii U,
+  which contains common and per-console keys, signatures and related data
+  required to access peripherals.
+
+  See https://wiiubrew.org/wiki/Hardware/OTP
+
+maintainers:
+  - Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+
+allOf:
+  - $ref: "nvmem.yaml#"
+
+properties:
+  compatible:
+    enum:
+      - nintendo,hollywood-otp
+      - nintendo,latte-otp
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    otp@d8001ec {
+        compatible = "nintendo,latte-otp";
+        reg = <0x0d8001ec 0x8>;
+    };
+
+...
-- 
2.32.0


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

* [PATCH v3 3/5] powerpc: wii.dts: Reduce the size of the control area
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  2021-07-01 22:57   ` [PATCH v3 1/5] " Emmanuel Gil Peyrot
  2021-07-01 22:57   ` [PATCH v3 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
@ 2021-07-01 22:57   ` Emmanuel Gil Peyrot
  2021-07-01 22:57   ` [PATCH v3 4/5] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-07-01 22:57 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This is wrong, but needed in order to avoid overlapping ranges with the
OTP area added in the next commit.  A refactor of this part of the
device tree is needed: according to Wiibrew[1], this area starts at
0x0d800000 and spans 0x400 bytes (that is, 0x100 32-bit registers),
encompassing PIC and GPIO registers, amongst the ones already exposed in
this device tree, which should become children of the control@d800000
node.

[1] https://wiibrew.org/wiki/Hardware/Hollywood_Registers

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/boot/dts/wii.dts | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
index aaa381da1906..c5fb54f8cc02 100644
--- a/arch/powerpc/boot/dts/wii.dts
+++ b/arch/powerpc/boot/dts/wii.dts
@@ -216,7 +216,13 @@ AVE: audio-video-encoder@70 {
 
 		control@d800100 {
 			compatible = "nintendo,hollywood-control";
-			reg = <0x0d800100 0x300>;
+			/*
+			 * Both the address and length are wrong, according to
+			 * Wiibrew this should be <0x0d800000 0x400>, but it
+			 * requires refactoring the PIC1 and GPIO nodes before
+			 * changing that.
+			 */
+			reg = <0x0d800100 0xa0>;
 		};
 
 		disk@d806000 {
-- 
2.32.0


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

* [PATCH v3 4/5] powerpc: wii.dts: Expose the OTP on this platform
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                     ` (2 preceding siblings ...)
  2021-07-01 22:57   ` [PATCH v3 3/5] powerpc: wii.dts: Reduce the size of the control area Emmanuel Gil Peyrot
@ 2021-07-01 22:57   ` Emmanuel Gil Peyrot
  2021-07-01 22:57   ` [PATCH v3 5/5] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-07-01 22:57 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This can be used by the newly-added nintendo-otp nvmem module.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/boot/dts/wii.dts | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
index c5fb54f8cc02..e9c945b123c6 100644
--- a/arch/powerpc/boot/dts/wii.dts
+++ b/arch/powerpc/boot/dts/wii.dts
@@ -219,12 +219,17 @@ control@d800100 {
 			/*
 			 * Both the address and length are wrong, according to
 			 * Wiibrew this should be <0x0d800000 0x400>, but it
-			 * requires refactoring the PIC1 and GPIO nodes before
-			 * changing that.
+			 * requires refactoring the PIC1, GPIO and OTP nodes
+			 * before changing that.
 			 */
 			reg = <0x0d800100 0xa0>;
 		};
 
+		otp@d8001ec {
+			compatible = "nintendo,hollywood-otp";
+			reg = <0x0d8001ec 0x8>;
+		};
+
 		disk@d806000 {
 			compatible = "nintendo,hollywood-di";
 			reg = <0x0d806000 0x40>;
-- 
2.32.0


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

* [PATCH v3 5/5] powerpc: wii_defconfig: Enable OTP by default
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                     ` (3 preceding siblings ...)
  2021-07-01 22:57   ` [PATCH v3 4/5] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
@ 2021-07-01 22:57   ` Emmanuel Gil Peyrot
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-07-01 22:57 UTC (permalink / raw)
  To: Srinivas Kandagatla, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Rob Herring, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This selects the nintendo-otp module when building for this platform.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/configs/wii_defconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/powerpc/configs/wii_defconfig b/arch/powerpc/configs/wii_defconfig
index 379c171f3ddd..a0c45bf2bfb1 100644
--- a/arch/powerpc/configs/wii_defconfig
+++ b/arch/powerpc/configs/wii_defconfig
@@ -99,6 +99,7 @@ CONFIG_LEDS_TRIGGER_HEARTBEAT=y
 CONFIG_LEDS_TRIGGER_PANIC=y
 CONFIG_RTC_CLASS=y
 CONFIG_RTC_DRV_GENERIC=y
+CONFIG_NVMEM_NINTENDO_OTP=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_FUSE_FS=m
-- 
2.32.0


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

* Re: [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform
  2021-07-01 19:56     ` Emmanuel Gil Peyrot
@ 2021-07-02  8:56       ` Jonathan Neuschäfer
  2021-07-03 15:53         ` Segher Boessenkool
  0 siblings, 1 reply; 28+ messages in thread
From: Jonathan Neuschäfer @ 2021-07-02  8:56 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Jonathan Neuschäfer, Srinivas Kandagatla, linuxppc-dev,
	devicetree, Ash Logan, Rob Herring, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1122 bytes --]

On Thu, Jul 01, 2021 at 09:56:55PM +0200, Emmanuel Gil Peyrot wrote:
> On Sat, Jun 26, 2021 at 11:34:01PM +0000, Jonathan Neuschäfer wrote:
> > On Wed, May 19, 2021 at 11:50:43AM +0200, Emmanuel Gil Peyrot wrote:
[...]
> > > +		otp@d8001ec {
> > > +			compatible = "nintendo,hollywood-otp";
> > > +			reg = <0x0d8001ec 0x8>;
> > 
> > The OTP registers overlap with the previous node, control@d800100.
> > Not sure what's the best way to structure the devicetree in this case,
> > maybe something roughly like the following (untested, unverified):
> [snip]
> 
> I couldn’t get this to work, but additionally it looks like it should
> start 0x100 earlier and contain pic1@d800030 and gpio@d8000c0, given
> https://wiibrew.org/wiki/Hardware/Hollywood_Registers
> 
> Would it make sense, for the time being, to reduce the size of this
> control@d800100 device to the single register currently being used by
> arch/powerpc/platforms/embedded6xx/wii.c (0xd800194, used to reboot the
> system) and leave the refactor of restart + OTP + PIC + GPIO for a
> future series?

Makes sense to me!

Jonathan

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform
  2021-07-02  8:56       ` Jonathan Neuschäfer
@ 2021-07-03 15:53         ` Segher Boessenkool
  0 siblings, 0 replies; 28+ messages in thread
From: Segher Boessenkool @ 2021-07-03 15:53 UTC (permalink / raw)
  To: Jonathan Neuschäfer
  Cc: Emmanuel Gil Peyrot, devicetree, linux-kernel, Rob Herring,
	Srinivas Kandagatla, Ash Logan, Paul Mackerras, linuxppc-dev

On Fri, Jul 02, 2021 at 08:56:31AM +0000, Jonathan Neuschäfer wrote:
> On Thu, Jul 01, 2021 at 09:56:55PM +0200, Emmanuel Gil Peyrot wrote:
> > On Sat, Jun 26, 2021 at 11:34:01PM +0000, Jonathan Neuschäfer wrote:
> > > On Wed, May 19, 2021 at 11:50:43AM +0200, Emmanuel Gil Peyrot wrote:
> [...]
> > > > +		otp@d8001ec {
> > > > +			compatible = "nintendo,hollywood-otp";
> > > > +			reg = <0x0d8001ec 0x8>;
> > > 
> > > The OTP registers overlap with the previous node, control@d800100.
> > > Not sure what's the best way to structure the devicetree in this case,
> > > maybe something roughly like the following (untested, unverified):
> > [snip]
> > 
> > I couldn’t get this to work, but additionally it looks like it should
> > start 0x100 earlier and contain pic1@d800030 and gpio@d8000c0, given
> > https://wiibrew.org/wiki/Hardware/Hollywood_Registers
> > 
> > Would it make sense, for the time being, to reduce the size of this
> > control@d800100 device to the single register currently being used by
> > arch/powerpc/platforms/embedded6xx/wii.c (0xd800194, used to reboot the
> > system) and leave the refactor of restart + OTP + PIC + GPIO for a
> > future series?
> 
> Makes sense to me!

There is no benefit to pretending there is a "control" bus (there is no
such thing), it only gets in the way.


Segher

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

* Re: [PATCH v3 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  2021-07-01 22:57   ` [PATCH v3 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
@ 2021-07-14 22:51     ` Rob Herring
  0 siblings, 0 replies; 28+ messages in thread
From: Rob Herring @ 2021-07-14 22:51 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Srinivas Kandagatla, linuxppc-dev, devicetree, Ash Logan,
	Jonathan Neuschäfer, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel

On Fri, Jul 02, 2021 at 12:57:40AM +0200, Emmanuel Gil Peyrot wrote:
> Both of these consoles use the exact same two registers, even at the
> same address, but the Wii U has eight banks of 128 bytes memory while
> the Wii only has one, hence the two compatible strings.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> ---
>  .../bindings/nvmem/nintendo-otp.yaml          | 44 +++++++++++++++++++
>  1 file changed, 44 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
> 
> diff --git a/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml b/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
> new file mode 100644
> index 000000000000..c39bd64b03b9
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
> @@ -0,0 +1,44 @@
> +# SPDX-License-Identifier: GPL-2.0

GPL-2.0-only OR BSD-2-Clause

> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/nvmem/nintendo-otp.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Nintendo Wii and Wii U OTP Device Tree Bindings
> +
> +description: |
> +  This binding represents the OTP memory as found on a Nintendo Wii or Wii U,
> +  which contains common and per-console keys, signatures and related data
> +  required to access peripherals.
> +
> +  See https://wiiubrew.org/wiki/Hardware/OTP
> +
> +maintainers:
> +  - Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> +
> +allOf:
> +  - $ref: "nvmem.yaml#"
> +
> +properties:
> +  compatible:
> +    enum:
> +      - nintendo,hollywood-otp
> +      - nintendo,latte-otp
> +
> +  reg:
> +    maxItems: 1
> +
> +required:
> +  - compatible
> +  - reg
> +
> +unevaluatedProperties: false
> +
> +examples:
> +  - |
> +    otp@d8001ec {
> +        compatible = "nintendo,latte-otp";
> +        reg = <0x0d8001ec 0x8>;
> +    };
> +
> +...
> -- 
> 2.32.0
> 
> 

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

* [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                     ` (4 preceding siblings ...)
  2021-07-01 22:57   ` [PATCH v3 5/5] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
@ 2021-08-01  7:38   ` Emmanuel Gil Peyrot
  2021-08-01  7:38     ` [PATCH v4 1/5] " Emmanuel Gil Peyrot
                       ` (5 more replies)
  5 siblings, 6 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-08-01  7:38 UTC (permalink / raw)
  To: Rob Herring, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Srinivas Kandagatla, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

The OTP is a read-only memory area which contains various keys and
signatures used to decrypt, encrypt or verify various pieces of storage.

Its size depends on the console, it is 128 bytes on the Wii and
1024 bytes on the Wii U (split into eight 128 bytes banks).

It can be used directly by writing into one register and reading from
the other one, without any additional synchronisation.

This series has been tested on both the Wii U (using my downstream
master-wiiu branch[1]), as well as on the Wii on mainline.

[1] https://gitlab.com/linkmauve/linux-wiiu/-/commits/master-wiiu

Changes since v1:
- Fixed the commit messages so they can be accepted by other email
  servers, sorry about that.

Changes since v2:
- Switched the dt binding documentation to YAML.
- Used more obvious register arithmetic, and tested that gcc (at -O1 and
  above) outputs the exact same rlwinm instructions for them.
- Use more #defines to make the code easier to read.
- Include some links to the reversed documentation.
- Avoid overlapping dt regions by changing the existing control@d800100
  node to end before the OTP registers, with some bigger dt refactoring
  left for a future series.

Changes since v3:
- Relicense the dt-binding documentation under GPLv2-only or
  BSD-2-clauses.

Emmanuel Gil Peyrot (5):
  nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  powerpc: wii.dts: Reduce the size of the control area
  powerpc: wii.dts: Expose the OTP on this platform
  powerpc: wii_defconfig: Enable OTP by default

 .../bindings/nvmem/nintendo-otp.yaml          |  44 +++++++
 arch/powerpc/boot/dts/wii.dts                 |  13 +-
 arch/powerpc/configs/wii_defconfig            |   1 +
 drivers/nvmem/Kconfig                         |  11 ++
 drivers/nvmem/Makefile                        |   2 +
 drivers/nvmem/nintendo-otp.c                  | 124 ++++++++++++++++++
 6 files changed, 194 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
 create mode 100644 drivers/nvmem/nintendo-otp.c

-- 
2.32.0


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

* [PATCH v4 1/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
@ 2021-08-01  7:38     ` Emmanuel Gil Peyrot
  2021-08-01  7:38     ` [PATCH v4 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
                       ` (4 subsequent siblings)
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-08-01  7:38 UTC (permalink / raw)
  To: Rob Herring, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Srinivas Kandagatla, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This OTP is read-only and contains various keys used by the console to
decrypt, encrypt or verify various pieces of storage.

Its size depends on the console, it is 128 bytes on the Wii and
1024 bytes on the Wii U (split into eight 128 bytes banks).

It can be used directly by writing into one register and reading from
the other one, without any additional synchronisation.

This driver was written based on reversed documentation, see:
https://wiiubrew.org/wiki/Hardware/OTP

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Tested-by: Jonathan Neuschäfer <j.ne@posteo.net>  # on Wii
Tested-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>  # on Wii U
---
 drivers/nvmem/Kconfig        |  11 ++++
 drivers/nvmem/Makefile       |   2 +
 drivers/nvmem/nintendo-otp.c | 124 +++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 drivers/nvmem/nintendo-otp.c

diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index dd2019006838..39854d43758b 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -107,6 +107,17 @@ config MTK_EFUSE
 	  This driver can also be built as a module. If so, the module
 	  will be called efuse-mtk.
 
+config NVMEM_NINTENDO_OTP
+	tristate "Nintendo Wii and Wii U OTP Support"
+	help
+	  This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
+
+	  This memory contains common and per-console keys, signatures and
+	  related data required to access peripherals.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called nvmem-nintendo-otp.
+
 config QCOM_QFPROM
 	tristate "QCOM QFPROM Support"
 	depends on ARCH_QCOM || COMPILE_TEST
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index bbea1410240a..dcbbde35b6a8 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -23,6 +23,8 @@ obj-$(CONFIG_NVMEM_LPC18XX_OTP)	+= nvmem_lpc18xx_otp.o
 nvmem_lpc18xx_otp-y		:= lpc18xx_otp.o
 obj-$(CONFIG_NVMEM_MXS_OCOTP)	+= nvmem-mxs-ocotp.o
 nvmem-mxs-ocotp-y		:= mxs-ocotp.o
+obj-$(CONFIG_NVMEM_NINTENDO_OTP)	+= nvmem-nintendo-otp.o
+nvmem-nintendo-otp-y		:= nintendo-otp.o
 obj-$(CONFIG_MTK_EFUSE)		+= nvmem_mtk-efuse.o
 nvmem_mtk-efuse-y		:= mtk-efuse.o
 obj-$(CONFIG_QCOM_QFPROM)	+= nvmem_qfprom.o
diff --git a/drivers/nvmem/nintendo-otp.c b/drivers/nvmem/nintendo-otp.c
new file mode 100644
index 000000000000..33961b17f9f1
--- /dev/null
+++ b/drivers/nvmem/nintendo-otp.c
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Nintendo Wii and Wii U OTP driver
+ *
+ * This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
+ *
+ * This memory contains common and per-console keys, signatures and
+ * related data required to access peripherals.
+ *
+ * Based on reversed documentation from https://wiiubrew.org/wiki/Hardware/OTP
+ *
+ * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+ */
+
+#include <linux/device.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/nvmem-provider.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+
+#define HW_OTPCMD  0
+#define HW_OTPDATA 4
+#define OTP_READ   0x80000000
+#define BANK_SIZE  128
+#define WORD_SIZE  4
+
+struct nintendo_otp_priv {
+	void __iomem *regs;
+};
+
+struct nintendo_otp_devtype_data {
+	const char *name;
+	unsigned int num_banks;
+};
+
+static const struct nintendo_otp_devtype_data hollywood_otp_data = {
+	.name = "wii-otp",
+	.num_banks = 1,
+};
+
+static const struct nintendo_otp_devtype_data latte_otp_data = {
+	.name = "wiiu-otp",
+	.num_banks = 8,
+};
+
+static int nintendo_otp_reg_read(void *context,
+				 unsigned int reg, void *_val, size_t bytes)
+{
+	struct nintendo_otp_priv *priv = context;
+	u32 *val = _val;
+	int words = bytes / WORD_SIZE;
+	u32 bank, addr;
+
+	while (words--) {
+		bank = (reg / BANK_SIZE) << 8;
+		addr = (reg / WORD_SIZE) % (BANK_SIZE / WORD_SIZE);
+		iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD);
+		*val++ = ioread32be(priv->regs + HW_OTPDATA);
+		reg += WORD_SIZE;
+	}
+
+	return 0;
+}
+
+static const struct of_device_id nintendo_otp_of_table[] = {
+	{ .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data },
+	{ .compatible = "nintendo,latte-otp", .data = &latte_otp_data },
+	{/* sentinel */},
+};
+MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
+
+static int nintendo_otp_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	const struct of_device_id *of_id =
+		of_match_device(nintendo_otp_of_table, dev);
+	struct resource *res;
+	struct nvmem_device *nvmem;
+	struct nintendo_otp_priv *priv;
+
+	struct nvmem_config config = {
+		.stride = WORD_SIZE,
+		.word_size = WORD_SIZE,
+		.reg_read = nintendo_otp_reg_read,
+		.read_only = true,
+		.root_only = true,
+	};
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->regs = devm_ioremap_resource(dev, res);
+	if (IS_ERR(priv->regs))
+		return PTR_ERR(priv->regs);
+
+	if (of_id->data) {
+		const struct nintendo_otp_devtype_data *data = of_id->data;
+		config.name = data->name;
+		config.size = data->num_banks * BANK_SIZE;
+	}
+
+	config.dev = dev;
+	config.priv = priv;
+
+	nvmem = devm_nvmem_register(dev, &config);
+
+	return PTR_ERR_OR_ZERO(nvmem);
+}
+
+static struct platform_driver nintendo_otp_driver = {
+	.probe = nintendo_otp_probe,
+	.driver = {
+		.name = "nintendo-otp",
+		.of_match_table = nintendo_otp_of_table,
+	},
+};
+module_platform_driver(nintendo_otp_driver);
+MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
+MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
+MODULE_LICENSE("GPL v2");
-- 
2.32.0


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

* [PATCH v4 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  2021-08-01  7:38     ` [PATCH v4 1/5] " Emmanuel Gil Peyrot
@ 2021-08-01  7:38     ` Emmanuel Gil Peyrot
  2021-08-06 21:07       ` Rob Herring
  2021-08-01  7:38     ` [PATCH v4 3/5] powerpc: wii.dts: Reduce the size of the control area Emmanuel Gil Peyrot
                       ` (3 subsequent siblings)
  5 siblings, 1 reply; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-08-01  7:38 UTC (permalink / raw)
  To: Rob Herring, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Srinivas Kandagatla, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

Both of these consoles use the exact same two registers, even at the
same address, but the Wii U has eight banks of 128 bytes memory while
the Wii only has one, hence the two compatible strings.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 .../bindings/nvmem/nintendo-otp.yaml          | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml

diff --git a/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml b/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
new file mode 100644
index 000000000000..dbe4ffdd644c
--- /dev/null
+++ b/Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/nvmem/nintendo-otp.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nintendo Wii and Wii U OTP Device Tree Bindings
+
+description: |
+  This binding represents the OTP memory as found on a Nintendo Wii or Wii U,
+  which contains common and per-console keys, signatures and related data
+  required to access peripherals.
+
+  See https://wiiubrew.org/wiki/Hardware/OTP
+
+maintainers:
+  - Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
+
+allOf:
+  - $ref: "nvmem.yaml#"
+
+properties:
+  compatible:
+    enum:
+      - nintendo,hollywood-otp
+      - nintendo,latte-otp
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    otp@d8001ec {
+        compatible = "nintendo,latte-otp";
+        reg = <0x0d8001ec 0x8>;
+    };
+
+...
-- 
2.32.0


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

* [PATCH v4 3/5] powerpc: wii.dts: Reduce the size of the control area
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
  2021-08-01  7:38     ` [PATCH v4 1/5] " Emmanuel Gil Peyrot
  2021-08-01  7:38     ` [PATCH v4 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
@ 2021-08-01  7:38     ` Emmanuel Gil Peyrot
  2021-08-01  7:38     ` [PATCH v4 4/5] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
                       ` (2 subsequent siblings)
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-08-01  7:38 UTC (permalink / raw)
  To: Rob Herring, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Srinivas Kandagatla, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This is wrong, but needed in order to avoid overlapping ranges with the
OTP area added in the next commit.  A refactor of this part of the
device tree is needed: according to Wiibrew[1], this area starts at
0x0d800000 and spans 0x400 bytes (that is, 0x100 32-bit registers),
encompassing PIC and GPIO registers, amongst the ones already exposed in
this device tree, which should become children of the control@d800000
node.

[1] https://wiibrew.org/wiki/Hardware/Hollywood_Registers

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/boot/dts/wii.dts | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
index aaa381da1906..c5fb54f8cc02 100644
--- a/arch/powerpc/boot/dts/wii.dts
+++ b/arch/powerpc/boot/dts/wii.dts
@@ -216,7 +216,13 @@ AVE: audio-video-encoder@70 {
 
 		control@d800100 {
 			compatible = "nintendo,hollywood-control";
-			reg = <0x0d800100 0x300>;
+			/*
+			 * Both the address and length are wrong, according to
+			 * Wiibrew this should be <0x0d800000 0x400>, but it
+			 * requires refactoring the PIC1 and GPIO nodes before
+			 * changing that.
+			 */
+			reg = <0x0d800100 0xa0>;
 		};
 
 		disk@d806000 {
-- 
2.32.0


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

* [PATCH v4 4/5] powerpc: wii.dts: Expose the OTP on this platform
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                       ` (2 preceding siblings ...)
  2021-08-01  7:38     ` [PATCH v4 3/5] powerpc: wii.dts: Reduce the size of the control area Emmanuel Gil Peyrot
@ 2021-08-01  7:38     ` Emmanuel Gil Peyrot
  2021-08-01  7:38     ` [PATCH v4 5/5] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
  2021-08-10 10:57     ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Srinivas Kandagatla
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-08-01  7:38 UTC (permalink / raw)
  To: Rob Herring, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Srinivas Kandagatla, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This can be used by the newly-added nintendo-otp nvmem module.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/boot/dts/wii.dts | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/boot/dts/wii.dts b/arch/powerpc/boot/dts/wii.dts
index c5fb54f8cc02..e9c945b123c6 100644
--- a/arch/powerpc/boot/dts/wii.dts
+++ b/arch/powerpc/boot/dts/wii.dts
@@ -219,12 +219,17 @@ control@d800100 {
 			/*
 			 * Both the address and length are wrong, according to
 			 * Wiibrew this should be <0x0d800000 0x400>, but it
-			 * requires refactoring the PIC1 and GPIO nodes before
-			 * changing that.
+			 * requires refactoring the PIC1, GPIO and OTP nodes
+			 * before changing that.
 			 */
 			reg = <0x0d800100 0xa0>;
 		};
 
+		otp@d8001ec {
+			compatible = "nintendo,hollywood-otp";
+			reg = <0x0d8001ec 0x8>;
+		};
+
 		disk@d806000 {
 			compatible = "nintendo,hollywood-di";
 			reg = <0x0d806000 0x40>;
-- 
2.32.0


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

* [PATCH v4 5/5] powerpc: wii_defconfig: Enable OTP by default
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                       ` (3 preceding siblings ...)
  2021-08-01  7:38     ` [PATCH v4 4/5] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
@ 2021-08-01  7:38     ` Emmanuel Gil Peyrot
  2021-08-10 10:57     ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Srinivas Kandagatla
  5 siblings, 0 replies; 28+ messages in thread
From: Emmanuel Gil Peyrot @ 2021-08-01  7:38 UTC (permalink / raw)
  To: Rob Herring, linuxppc-dev, devicetree
  Cc: Emmanuel Gil Peyrot, Ash Logan, Jonathan Neuschäfer,
	Srinivas Kandagatla, Michael Ellerman, Benjamin Herrenschmidt,
	Paul Mackerras, linux-kernel

This selects the nintendo-otp module when building for this platform.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 arch/powerpc/configs/wii_defconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/powerpc/configs/wii_defconfig b/arch/powerpc/configs/wii_defconfig
index 379c171f3ddd..a0c45bf2bfb1 100644
--- a/arch/powerpc/configs/wii_defconfig
+++ b/arch/powerpc/configs/wii_defconfig
@@ -99,6 +99,7 @@ CONFIG_LEDS_TRIGGER_HEARTBEAT=y
 CONFIG_LEDS_TRIGGER_PANIC=y
 CONFIG_RTC_CLASS=y
 CONFIG_RTC_DRV_GENERIC=y
+CONFIG_NVMEM_NINTENDO_OTP=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_FUSE_FS=m
-- 
2.32.0


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

* Re: [PATCH v4 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support
  2021-08-01  7:38     ` [PATCH v4 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
@ 2021-08-06 21:07       ` Rob Herring
  0 siblings, 0 replies; 28+ messages in thread
From: Rob Herring @ 2021-08-06 21:07 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot
  Cc: Paul Mackerras, Ash Logan, linuxppc-dev, Michael Ellerman,
	devicetree, Rob Herring, Srinivas Kandagatla,
	Benjamin Herrenschmidt, linux-kernel, Jonathan Neuschäfer

On Sun, 01 Aug 2021 09:38:19 +0200, Emmanuel Gil Peyrot wrote:
> Both of these consoles use the exact same two registers, even at the
> same address, but the Wii U has eight banks of 128 bytes memory while
> the Wii only has one, hence the two compatible strings.
> 
> Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
> ---
>  .../bindings/nvmem/nintendo-otp.yaml          | 44 +++++++++++++++++++
>  1 file changed, 44 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
> 

Reviewed-by: Rob Herring <robh@kernel.org>

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

* Re: [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
  2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
                       ` (4 preceding siblings ...)
  2021-08-01  7:38     ` [PATCH v4 5/5] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
@ 2021-08-10 10:57     ` Srinivas Kandagatla
  5 siblings, 0 replies; 28+ messages in thread
From: Srinivas Kandagatla @ 2021-08-10 10:57 UTC (permalink / raw)
  To: Emmanuel Gil Peyrot, Rob Herring, linuxppc-dev, devicetree
  Cc: Ash Logan, Jonathan Neuschäfer, Michael Ellerman,
	Benjamin Herrenschmidt, Paul Mackerras, linux-kernel



On 01/08/2021 08:38, Emmanuel Gil Peyrot wrote:
> The OTP is a read-only memory area which contains various keys and
> signatures used to decrypt, encrypt or verify various pieces of storage.
> 
> Its size depends on the console, it is 128 bytes on the Wii and
> 1024 bytes on the Wii U (split into eight 128 bytes banks).
> 
> It can be used directly by writing into one register and reading from
> the other one, without any additional synchronisation.
> 
> This series has been tested on both the Wii U (using my downstream
> master-wiiu branch[1]), as well as on the Wii on mainline.
> 
> [1] https://gitlab.com/linkmauve/linux-wiiu/-/commits/master-wiiu
> 
> Changes since v1:
> - Fixed the commit messages so they can be accepted by other email
>    servers, sorry about that.
> 
> Changes since v2:
> - Switched the dt binding documentation to YAML.
> - Used more obvious register arithmetic, and tested that gcc (at -O1 and
>    above) outputs the exact same rlwinm instructions for them.
> - Use more #defines to make the code easier to read.
> - Include some links to the reversed documentation.
> - Avoid overlapping dt regions by changing the existing control@d800100
>    node to end before the OTP registers, with some bigger dt refactoring
>    left for a future series.
> 
> Changes since v3:
> - Relicense the dt-binding documentation under GPLv2-only or
>    BSD-2-clauses.
> 
> Emmanuel Gil Peyrot (5):
>    nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
>    dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support


Applied 1/5 and 2/5 to nvmem next,
rest of the patches should go via powerpc dts tree.

thanks,
--srini
>    powerpc: wii.dts: Reduce the size of the control area
>    powerpc: wii.dts: Expose the OTP on this platform
>    powerpc: wii_defconfig: Enable OTP by default
> 
>   .../bindings/nvmem/nintendo-otp.yaml          |  44 +++++++
>   arch/powerpc/boot/dts/wii.dts                 |  13 +-
>   arch/powerpc/configs/wii_defconfig            |   1 +
>   drivers/nvmem/Kconfig                         |  11 ++
>   drivers/nvmem/Makefile                        |   2 +
>   drivers/nvmem/nintendo-otp.c                  | 124 ++++++++++++++++++
>   6 files changed, 194 insertions(+), 1 deletion(-)
>   create mode 100644 Documentation/devicetree/bindings/nvmem/nintendo-otp.yaml
>   create mode 100644 drivers/nvmem/nintendo-otp.c
> 

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

end of thread, other threads:[~2021-08-10 10:57 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-19  9:50 [PATCH v2 0/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
2021-05-19  9:50 ` [PATCH v2 1/4] " Emmanuel Gil Peyrot
2021-06-26 23:24   ` Jonathan Neuschäfer
2021-05-19  9:50 ` [PATCH v2 2/4] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
2021-05-21  1:37   ` Rob Herring
2021-06-26 21:27   ` Jonathan Neuschäfer
2021-05-19  9:50 ` [PATCH v2 3/4] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
2021-06-26 23:34   ` Jonathan Neuschäfer
2021-07-01 19:56     ` Emmanuel Gil Peyrot
2021-07-02  8:56       ` Jonathan Neuschäfer
2021-07-03 15:53         ` Segher Boessenkool
2021-05-19  9:50 ` [PATCH v2 4/4] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
2021-06-26 23:38   ` Jonathan Neuschäfer
2021-07-01 22:57 ` [PATCH v3 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
2021-07-01 22:57   ` [PATCH v3 1/5] " Emmanuel Gil Peyrot
2021-07-01 22:57   ` [PATCH v3 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
2021-07-14 22:51     ` Rob Herring
2021-07-01 22:57   ` [PATCH v3 3/5] powerpc: wii.dts: Reduce the size of the control area Emmanuel Gil Peyrot
2021-07-01 22:57   ` [PATCH v3 4/5] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
2021-07-01 22:57   ` [PATCH v3 5/5] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
2021-08-01  7:38   ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Emmanuel Gil Peyrot
2021-08-01  7:38     ` [PATCH v4 1/5] " Emmanuel Gil Peyrot
2021-08-01  7:38     ` [PATCH v4 2/5] dt-bindings: nintendo-otp: Document the Wii and Wii U OTP support Emmanuel Gil Peyrot
2021-08-06 21:07       ` Rob Herring
2021-08-01  7:38     ` [PATCH v4 3/5] powerpc: wii.dts: Reduce the size of the control area Emmanuel Gil Peyrot
2021-08-01  7:38     ` [PATCH v4 4/5] powerpc: wii.dts: Expose the OTP on this platform Emmanuel Gil Peyrot
2021-08-01  7:38     ` [PATCH v4 5/5] powerpc: wii_defconfig: Enable OTP by default Emmanuel Gil Peyrot
2021-08-10 10:57     ` [PATCH v4 0/5] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Srinivas Kandagatla

This is a public inbox, see mirroring instructions
on how to clone and mirror all data and code used for this inbox