62d9e2aa0f00e34b4f09b79852309b159d01a546
[openwrt/staging/neocturne.git] /
1 From 3683b761fe3a10ad18515acd5368dd601268cfe5 Mon Sep 17 00:00:00 2001
2 From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
3 Date: Tue, 10 Aug 2021 16:30:36 +0100
4 Subject: [PATCH] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This OTP is read-only and contains various keys used by the console to
10 decrypt, encrypt or verify various pieces of storage.
11
12 Its size depends on the console, it is 128 bytes on the Wii and
13 1024 bytes on the Wii U (split into eight 128 bytes banks).
14
15 It can be used directly by writing into one register and reading from
16 the other one, without any additional synchronisation.
17
18 This driver was written based on reversed documentation, see:
19 https://wiiubrew.org/wiki/Hardware/OTP
20
21 Tested-by: Jonathan Neuschäfer <j.ne@posteo.net> # on Wii
22 Tested-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> # on Wii U
23 Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
24 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
25 Link: https://lore.kernel.org/r/20210810153036.1494-3-srinivas.kandagatla@linaro.org
26 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
27 ---
28 drivers/nvmem/Kconfig | 11 ++++
29 drivers/nvmem/Makefile | 2 +
30 drivers/nvmem/nintendo-otp.c | 124 +++++++++++++++++++++++++++++++++++
31 3 files changed, 137 insertions(+)
32 create mode 100644 drivers/nvmem/nintendo-otp.c
33
34 --- a/drivers/nvmem/Kconfig
35 +++ b/drivers/nvmem/Kconfig
36 @@ -107,6 +107,17 @@ config MTK_EFUSE
37 This driver can also be built as a module. If so, the module
38 will be called efuse-mtk.
39
40 +config NVMEM_NINTENDO_OTP
41 + tristate "Nintendo Wii and Wii U OTP Support"
42 + help
43 + This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
44 +
45 + This memory contains common and per-console keys, signatures and
46 + related data required to access peripherals.
47 +
48 + This driver can also be built as a module. If so, the module
49 + will be called nvmem-nintendo-otp.
50 +
51 config QCOM_QFPROM
52 tristate "QCOM QFPROM Support"
53 depends on ARCH_QCOM || COMPILE_TEST
54 --- a/drivers/nvmem/Makefile
55 +++ b/drivers/nvmem/Makefile
56 @@ -23,6 +23,8 @@ obj-$(CONFIG_NVMEM_LPC18XX_OTP) += nvmem
57 nvmem_lpc18xx_otp-y := lpc18xx_otp.o
58 obj-$(CONFIG_NVMEM_MXS_OCOTP) += nvmem-mxs-ocotp.o
59 nvmem-mxs-ocotp-y := mxs-ocotp.o
60 +obj-$(CONFIG_NVMEM_NINTENDO_OTP) += nvmem-nintendo-otp.o
61 +nvmem-nintendo-otp-y := nintendo-otp.o
62 obj-$(CONFIG_MTK_EFUSE) += nvmem_mtk-efuse.o
63 nvmem_mtk-efuse-y := mtk-efuse.o
64 obj-$(CONFIG_QCOM_QFPROM) += nvmem_qfprom.o
65 --- /dev/null
66 +++ b/drivers/nvmem/nintendo-otp.c
67 @@ -0,0 +1,124 @@
68 +// SPDX-License-Identifier: GPL-2.0-only
69 +/*
70 + * Nintendo Wii and Wii U OTP driver
71 + *
72 + * This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
73 + *
74 + * This memory contains common and per-console keys, signatures and
75 + * related data required to access peripherals.
76 + *
77 + * Based on reversed documentation from https://wiiubrew.org/wiki/Hardware/OTP
78 + *
79 + * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
80 + */
81 +
82 +#include <linux/device.h>
83 +#include <linux/io.h>
84 +#include <linux/module.h>
85 +#include <linux/mod_devicetable.h>
86 +#include <linux/nvmem-provider.h>
87 +#include <linux/of_device.h>
88 +#include <linux/platform_device.h>
89 +
90 +#define HW_OTPCMD 0
91 +#define HW_OTPDATA 4
92 +#define OTP_READ 0x80000000
93 +#define BANK_SIZE 128
94 +#define WORD_SIZE 4
95 +
96 +struct nintendo_otp_priv {
97 + void __iomem *regs;
98 +};
99 +
100 +struct nintendo_otp_devtype_data {
101 + const char *name;
102 + unsigned int num_banks;
103 +};
104 +
105 +static const struct nintendo_otp_devtype_data hollywood_otp_data = {
106 + .name = "wii-otp",
107 + .num_banks = 1,
108 +};
109 +
110 +static const struct nintendo_otp_devtype_data latte_otp_data = {
111 + .name = "wiiu-otp",
112 + .num_banks = 8,
113 +};
114 +
115 +static int nintendo_otp_reg_read(void *context,
116 + unsigned int reg, void *_val, size_t bytes)
117 +{
118 + struct nintendo_otp_priv *priv = context;
119 + u32 *val = _val;
120 + int words = bytes / WORD_SIZE;
121 + u32 bank, addr;
122 +
123 + while (words--) {
124 + bank = (reg / BANK_SIZE) << 8;
125 + addr = (reg / WORD_SIZE) % (BANK_SIZE / WORD_SIZE);
126 + iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD);
127 + *val++ = ioread32be(priv->regs + HW_OTPDATA);
128 + reg += WORD_SIZE;
129 + }
130 +
131 + return 0;
132 +}
133 +
134 +static const struct of_device_id nintendo_otp_of_table[] = {
135 + { .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data },
136 + { .compatible = "nintendo,latte-otp", .data = &latte_otp_data },
137 + {/* sentinel */},
138 +};
139 +MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
140 +
141 +static int nintendo_otp_probe(struct platform_device *pdev)
142 +{
143 + struct device *dev = &pdev->dev;
144 + const struct of_device_id *of_id =
145 + of_match_device(nintendo_otp_of_table, dev);
146 + struct resource *res;
147 + struct nvmem_device *nvmem;
148 + struct nintendo_otp_priv *priv;
149 +
150 + struct nvmem_config config = {
151 + .stride = WORD_SIZE,
152 + .word_size = WORD_SIZE,
153 + .reg_read = nintendo_otp_reg_read,
154 + .read_only = true,
155 + .root_only = true,
156 + };
157 +
158 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
159 + if (!priv)
160 + return -ENOMEM;
161 +
162 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
163 + priv->regs = devm_ioremap_resource(dev, res);
164 + if (IS_ERR(priv->regs))
165 + return PTR_ERR(priv->regs);
166 +
167 + if (of_id->data) {
168 + const struct nintendo_otp_devtype_data *data = of_id->data;
169 + config.name = data->name;
170 + config.size = data->num_banks * BANK_SIZE;
171 + }
172 +
173 + config.dev = dev;
174 + config.priv = priv;
175 +
176 + nvmem = devm_nvmem_register(dev, &config);
177 +
178 + return PTR_ERR_OR_ZERO(nvmem);
179 +}
180 +
181 +static struct platform_driver nintendo_otp_driver = {
182 + .probe = nintendo_otp_probe,
183 + .driver = {
184 + .name = "nintendo-otp",
185 + .of_match_table = nintendo_otp_of_table,
186 + },
187 +};
188 +module_platform_driver(nintendo_otp_driver);
189 +MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
190 +MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
191 +MODULE_LICENSE("GPL v2");