xref: /openbmc/linux/drivers/nvmem/nintendo-otp.c (revision 234489ac)
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 nvmem_device *nvmem;
80 	struct nintendo_otp_priv *priv;
81 
82 	struct nvmem_config config = {
83 		.stride = WORD_SIZE,
84 		.word_size = WORD_SIZE,
85 		.reg_read = nintendo_otp_reg_read,
86 		.read_only = true,
87 		.root_only = true,
88 	};
89 
90 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
91 	if (!priv)
92 		return -ENOMEM;
93 
94 	priv->regs = devm_platform_ioremap_resource(pdev, 0);
95 	if (IS_ERR(priv->regs))
96 		return PTR_ERR(priv->regs);
97 
98 	if (of_id->data) {
99 		const struct nintendo_otp_devtype_data *data = of_id->data;
100 		config.name = data->name;
101 		config.size = data->num_banks * BANK_SIZE;
102 	}
103 
104 	config.dev = dev;
105 	config.priv = priv;
106 
107 	nvmem = devm_nvmem_register(dev, &config);
108 
109 	return PTR_ERR_OR_ZERO(nvmem);
110 }
111 
112 static struct platform_driver nintendo_otp_driver = {
113 	.probe = nintendo_otp_probe,
114 	.driver = {
115 		.name = "nintendo-otp",
116 		.of_match_table = nintendo_otp_of_table,
117 	},
118 };
119 module_platform_driver(nintendo_otp_driver);
120 MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
121 MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
122 MODULE_LICENSE("GPL v2");
123