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/nvmem-provider.h> 19 #include <linux/of.h> 20 #include <linux/platform_device.h> 21 22 #define HW_OTPCMD 0 23 #define HW_OTPDATA 4 24 #define OTP_READ 0x80000000 25 #define BANK_SIZE 128 26 #define WORD_SIZE 4 27 28 struct nintendo_otp_priv { 29 void __iomem *regs; 30 }; 31 32 struct nintendo_otp_devtype_data { 33 const char *name; 34 unsigned int num_banks; 35 }; 36 37 static const struct nintendo_otp_devtype_data hollywood_otp_data = { 38 .name = "wii-otp", 39 .num_banks = 1, 40 }; 41 42 static const struct nintendo_otp_devtype_data latte_otp_data = { 43 .name = "wiiu-otp", 44 .num_banks = 8, 45 }; 46 47 static int nintendo_otp_reg_read(void *context, 48 unsigned int reg, void *_val, size_t bytes) 49 { 50 struct nintendo_otp_priv *priv = context; 51 u32 *val = _val; 52 int words = bytes / WORD_SIZE; 53 u32 bank, addr; 54 55 while (words--) { 56 bank = (reg / BANK_SIZE) << 8; 57 addr = (reg / WORD_SIZE) % (BANK_SIZE / WORD_SIZE); 58 iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD); 59 *val++ = ioread32be(priv->regs + HW_OTPDATA); 60 reg += WORD_SIZE; 61 } 62 63 return 0; 64 } 65 66 static const struct of_device_id nintendo_otp_of_table[] = { 67 { .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data }, 68 { .compatible = "nintendo,latte-otp", .data = &latte_otp_data }, 69 {/* sentinel */}, 70 }; 71 MODULE_DEVICE_TABLE(of, nintendo_otp_of_table); 72 73 static int nintendo_otp_probe(struct platform_device *pdev) 74 { 75 struct device *dev = &pdev->dev; 76 const struct nintendo_otp_devtype_data *data; 77 struct nvmem_device *nvmem; 78 struct nintendo_otp_priv *priv; 79 80 struct nvmem_config config = { 81 .stride = WORD_SIZE, 82 .word_size = WORD_SIZE, 83 .reg_read = nintendo_otp_reg_read, 84 .read_only = true, 85 .root_only = true, 86 }; 87 88 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 89 if (!priv) 90 return -ENOMEM; 91 92 priv->regs = devm_platform_ioremap_resource(pdev, 0); 93 if (IS_ERR(priv->regs)) 94 return PTR_ERR(priv->regs); 95 96 data = of_device_get_match_data(dev); 97 if (data) { 98 config.name = data->name; 99 config.size = data->num_banks * BANK_SIZE; 100 } 101 102 config.dev = dev; 103 config.priv = priv; 104 105 nvmem = devm_nvmem_register(dev, &config); 106 107 return PTR_ERR_OR_ZERO(nvmem); 108 } 109 110 static struct platform_driver nintendo_otp_driver = { 111 .probe = nintendo_otp_probe, 112 .driver = { 113 .name = "nintendo-otp", 114 .of_match_table = nintendo_otp_of_table, 115 }, 116 }; 117 module_platform_driver(nintendo_otp_driver); 118 MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"); 119 MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver"); 120 MODULE_LICENSE("GPL v2"); 121