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);
79 struct nvmem_device *nvmem;
80 struct nintendo_otp_priv *priv;
82 struct nvmem_config config = {
84 .word_size = WORD_SIZE,
85 .reg_read = nintendo_otp_reg_read,
90 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
94 priv->regs = devm_platform_ioremap_resource(pdev, 0);
95 if (IS_ERR(priv->regs))
96 return PTR_ERR(priv->regs);
99 const struct nintendo_otp_devtype_data *data = of_id->data;
100 config.name = data->name;
101 config.size = data->num_banks * BANK_SIZE;
107 nvmem = devm_nvmem_register(dev, &config);
109 return PTR_ERR_OR_ZERO(nvmem);
112 static struct platform_driver nintendo_otp_driver = {
113 .probe = nintendo_otp_probe,
115 .name = "nintendo-otp",
116 .of_match_table = nintendo_otp_of_table,
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");