linux/drivers/nvmem/nintendo-otp.c
<<
>>
Prefs
   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
  29struct nintendo_otp_priv {
  30        void __iomem *regs;
  31};
  32
  33struct nintendo_otp_devtype_data {
  34        const char *name;
  35        unsigned int num_banks;
  36};
  37
  38static const struct nintendo_otp_devtype_data hollywood_otp_data = {
  39        .name = "wii-otp",
  40        .num_banks = 1,
  41};
  42
  43static const struct nintendo_otp_devtype_data latte_otp_data = {
  44        .name = "wiiu-otp",
  45        .num_banks = 8,
  46};
  47
  48static 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
  67static 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};
  72MODULE_DEVICE_TABLE(of, nintendo_otp_of_table);
  73
  74static 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
 114static 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};
 121module_platform_driver(nintendo_otp_driver);
 122MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>");
 123MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver");
 124MODULE_LICENSE("GPL v2");
 125