1 // SPDX-License-Identifier: GPL-2.0-only
3 * Nintendo Wii and Wii U OTP driver
5 * This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
7 * This memory contains common and per-console keys, signatures and
8 * related data required to access peripherals.
10 * Based on reversed documentation from https://wiiubrew.org/wiki/Hardware/OTP
12 * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
15 #include <linux/device.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>
25 #define OTP_READ 0x80000000
29 struct nintendo_otp_priv {
33 struct nintendo_otp_devtype_data {
35 unsigned int num_banks;
38 static const struct nintendo_otp_devtype_data hollywood_otp_data = {
43 static const struct nintendo_otp_devtype_data latte_otp_data = {
48 static int nintendo_otp_reg_read(void *context,
49 unsigned int reg, void *_val, size_t bytes)
51 struct nintendo_otp_priv *priv = context;
53 int words = bytes / WORD_SIZE;
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);
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 },
72 MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
74 static int nintendo_otp_probe(struct platform_device *pdev)
76 struct device *dev = &pdev->dev;
77 const struct of_device_id *of_id =
78 of_match_device(nintendo_otp_of_table, dev);
80 struct nvmem_device *nvmem;
81 struct nintendo_otp_priv *priv;
83 struct nvmem_config config = {
85 .word_size = WORD_SIZE,
86 .reg_read = nintendo_otp_reg_read,
91 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
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);
101 const struct nintendo_otp_devtype_data *data = of_id->data;
102 config.name = data->name;
103 config.size = data->num_banks * BANK_SIZE;
109 nvmem = devm_nvmem_register(dev, &config);
111 return PTR_ERR_OR_ZERO(nvmem);
114 static struct platform_driver nintendo_otp_driver = {
115 .probe = nintendo_otp_probe,
117 .name = "nintendo-otp",
118 .of_match_table = nintendo_otp_of_table,
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");