Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Nintendo Wii and Wii U OTP driver
0004  *
0005  * This is a driver exposing the OTP of a Nintendo Wii or Wii U console.
0006  *
0007  * This memory contains common and per-console keys, signatures and
0008  * related data required to access peripherals.
0009  *
0010  * Based on reversed documentation from https://wiiubrew.org/wiki/Hardware/OTP
0011  *
0012  * Copyright (C) 2021 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
0013  */
0014 
0015 #include <linux/device.h>
0016 #include <linux/io.h>
0017 #include <linux/module.h>
0018 #include <linux/mod_devicetable.h>
0019 #include <linux/nvmem-provider.h>
0020 #include <linux/of_device.h>
0021 #include <linux/platform_device.h>
0022 
0023 #define HW_OTPCMD  0
0024 #define HW_OTPDATA 4
0025 #define OTP_READ   0x80000000
0026 #define BANK_SIZE  128
0027 #define WORD_SIZE  4
0028 
0029 struct nintendo_otp_priv {
0030     void __iomem *regs;
0031 };
0032 
0033 struct nintendo_otp_devtype_data {
0034     const char *name;
0035     unsigned int num_banks;
0036 };
0037 
0038 static const struct nintendo_otp_devtype_data hollywood_otp_data = {
0039     .name = "wii-otp",
0040     .num_banks = 1,
0041 };
0042 
0043 static const struct nintendo_otp_devtype_data latte_otp_data = {
0044     .name = "wiiu-otp",
0045     .num_banks = 8,
0046 };
0047 
0048 static int nintendo_otp_reg_read(void *context,
0049                  unsigned int reg, void *_val, size_t bytes)
0050 {
0051     struct nintendo_otp_priv *priv = context;
0052     u32 *val = _val;
0053     int words = bytes / WORD_SIZE;
0054     u32 bank, addr;
0055 
0056     while (words--) {
0057         bank = (reg / BANK_SIZE) << 8;
0058         addr = (reg / WORD_SIZE) % (BANK_SIZE / WORD_SIZE);
0059         iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD);
0060         *val++ = ioread32be(priv->regs + HW_OTPDATA);
0061         reg += WORD_SIZE;
0062     }
0063 
0064     return 0;
0065 }
0066 
0067 static const struct of_device_id nintendo_otp_of_table[] = {
0068     { .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data },
0069     { .compatible = "nintendo,latte-otp", .data = &latte_otp_data },
0070     {/* sentinel */},
0071 };
0072 MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
0073 
0074 static int nintendo_otp_probe(struct platform_device *pdev)
0075 {
0076     struct device *dev = &pdev->dev;
0077     const struct of_device_id *of_id =
0078         of_match_device(nintendo_otp_of_table, dev);
0079     struct resource *res;
0080     struct nvmem_device *nvmem;
0081     struct nintendo_otp_priv *priv;
0082 
0083     struct nvmem_config config = {
0084         .stride = WORD_SIZE,
0085         .word_size = WORD_SIZE,
0086         .reg_read = nintendo_otp_reg_read,
0087         .read_only = true,
0088         .root_only = true,
0089     };
0090 
0091     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0092     if (!priv)
0093         return -ENOMEM;
0094 
0095     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0096     priv->regs = devm_ioremap_resource(dev, res);
0097     if (IS_ERR(priv->regs))
0098         return PTR_ERR(priv->regs);
0099 
0100     if (of_id->data) {
0101         const struct nintendo_otp_devtype_data *data = of_id->data;
0102         config.name = data->name;
0103         config.size = data->num_banks * BANK_SIZE;
0104     }
0105 
0106     config.dev = dev;
0107     config.priv = priv;
0108 
0109     nvmem = devm_nvmem_register(dev, &config);
0110 
0111     return PTR_ERR_OR_ZERO(nvmem);
0112 }
0113 
0114 static struct platform_driver nintendo_otp_driver = {
0115     .probe = nintendo_otp_probe,
0116     .driver = {
0117         .name = "nintendo-otp",
0118         .of_match_table = nintendo_otp_of_table,
0119     },
0120 };
0121 module_platform_driver(nintendo_otp_driver);
0122 MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
0123 MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
0124 MODULE_LICENSE("GPL v2");