Skip to content

Commit 3683b76

Browse files
linkmauvegregkh
authored andcommitted
nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP
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 Tested-by: Jonathan Neuschäfer <j.ne@posteo.net> # on Wii Tested-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> # on Wii U Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> Link: https://lore.kernel.org/r/20210810153036.1494-3-srinivas.kandagatla@linaro.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9aaf4d2 commit 3683b76

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

drivers/nvmem/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ config MTK_EFUSE
107107
This driver can also be built as a module. If so, the module
108108
will be called efuse-mtk.
109109

110+
config NVMEM_NINTENDO_OTP
111+
tristate "Nintendo Wii and Wii U OTP Support"
112+
help
113+
This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
114+
115+
This memory contains common and per-console keys, signatures and
116+
related data required to access peripherals.
117+
118+
This driver can also be built as a module. If so, the module
119+
will be called nvmem-nintendo-otp.
120+
110121
config QCOM_QFPROM
111122
tristate "QCOM QFPROM Support"
112123
depends on ARCH_QCOM || COMPILE_TEST

drivers/nvmem/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ obj-$(CONFIG_NVMEM_LPC18XX_OTP) += nvmem_lpc18xx_otp.o
2323
nvmem_lpc18xx_otp-y := lpc18xx_otp.o
2424
obj-$(CONFIG_NVMEM_MXS_OCOTP) += nvmem-mxs-ocotp.o
2525
nvmem-mxs-ocotp-y := mxs-ocotp.o
26+
obj-$(CONFIG_NVMEM_NINTENDO_OTP) += nvmem-nintendo-otp.o
27+
nvmem-nintendo-otp-y := nintendo-otp.o
2628
obj-$(CONFIG_MTK_EFUSE) += nvmem_mtk-efuse.o
2729
nvmem_mtk-efuse-y := mtk-efuse.o
2830
obj-$(CONFIG_QCOM_QFPROM) += nvmem_qfprom.o

drivers/nvmem/nintendo-otp.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Nintendo Wii and Wii U OTP driver
4+
*
5+
* This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
6+
*
7+
* This memory contains common and per-console keys, signatures and
8+
* related data required to access peripherals.
9+
*
10+
* Based on reversed documentation from https://wiiubrew.org/wiki/Hardware/OTP
11+
*
12+
* Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
13+
*/
14+
15+
#include <linux/device.h>
16+
#include <linux/io.h>
17+
#include <linux/module.h>
18+
#include <linux/mod_devicetable.h>
19+
#include <linux/nvmem-provider.h>
20+
#include <linux/of_device.h>
21+
#include <linux/platform_device.h>
22+
23+
#define HW_OTPCMD 0
24+
#define HW_OTPDATA 4
25+
#define OTP_READ 0x80000000
26+
#define BANK_SIZE 128
27+
#define WORD_SIZE 4
28+
29+
struct nintendo_otp_priv {
30+
void __iomem *regs;
31+
};
32+
33+
struct nintendo_otp_devtype_data {
34+
const char *name;
35+
unsigned int num_banks;
36+
};
37+
38+
static const struct nintendo_otp_devtype_data hollywood_otp_data = {
39+
.name = "wii-otp",
40+
.num_banks = 1,
41+
};
42+
43+
static const struct nintendo_otp_devtype_data latte_otp_data = {
44+
.name = "wiiu-otp",
45+
.num_banks = 8,
46+
};
47+
48+
static int nintendo_otp_reg_read(void *context,
49+
unsigned int reg, void *_val, size_t bytes)
50+
{
51+
struct nintendo_otp_priv *priv = context;
52+
u32 *val = _val;
53+
int words = bytes / WORD_SIZE;
54+
u32 bank, addr;
55+
56+
while (words--) {
57+
bank = (reg / BANK_SIZE) << 8;
58+
addr = (reg / WORD_SIZE) % (BANK_SIZE / WORD_SIZE);
59+
iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD);
60+
*val++ = ioread32be(priv->regs + HW_OTPDATA);
61+
reg += WORD_SIZE;
62+
}
63+
64+
return 0;
65+
}
66+
67+
static const struct of_device_id nintendo_otp_of_table[] = {
68+
{ .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data },
69+
{ .compatible = "nintendo,latte-otp", .data = &latte_otp_data },
70+
{/* sentinel */},
71+
};
72+
MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
73+
74+
static int nintendo_otp_probe(struct platform_device *pdev)
75+
{
76+
struct device *dev = &pdev->dev;
77+
const struct of_device_id *of_id =
78+
of_match_device(nintendo_otp_of_table, dev);
79+
struct resource *res;
80+
struct nvmem_device *nvmem;
81+
struct nintendo_otp_priv *priv;
82+
83+
struct nvmem_config config = {
84+
.stride = WORD_SIZE,
85+
.word_size = WORD_SIZE,
86+
.reg_read = nintendo_otp_reg_read,
87+
.read_only = true,
88+
.root_only = true,
89+
};
90+
91+
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
92+
if (!priv)
93+
return -ENOMEM;
94+
95+
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
96+
priv->regs = devm_ioremap_resource(dev, res);
97+
if (IS_ERR(priv->regs))
98+
return PTR_ERR(priv->regs);
99+
100+
if (of_id->data) {
101+
const struct nintendo_otp_devtype_data *data = of_id->data;
102+
config.name = data->name;
103+
config.size = data->num_banks * BANK_SIZE;
104+
}
105+
106+
config.dev = dev;
107+
config.priv = priv;
108+
109+
nvmem = devm_nvmem_register(dev, &config);
110+
111+
return PTR_ERR_OR_ZERO(nvmem);
112+
}
113+
114+
static struct platform_driver nintendo_otp_driver = {
115+
.probe = nintendo_otp_probe,
116+
.driver = {
117+
.name = "nintendo-otp",
118+
.of_match_table = nintendo_otp_of_table,
119+
},
120+
};
121+
module_platform_driver(nintendo_otp_driver);
122+
MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
123+
MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
124+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)