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_device.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 of_device_id *of_id = 77 of_match_device(nintendo_otp_of_table, dev); 78 struct nvmem_device *nvmem; 79 struct nintendo_otp_priv *priv; 80 81 struct nvmem_config config = { 82 .stride = WORD_SIZE, 83 .word_size = WORD_SIZE, 84 .reg_read = nintendo_otp_reg_read, 85 .read_only = true, 86 .root_only = true, 87 }; 88 89 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 90 if (!priv) 91 return -ENOMEM; 92 93 priv->regs = devm_platform_ioremap_resource(pdev, 0); 94 if (IS_ERR(priv->regs)) 95 return PTR_ERR(priv->regs); 96 97 if (of_id->data) { 98 const struct nintendo_otp_devtype_data *data = of_id->data; 99 config.name = data->name; 100 config.size = data->num_banks * BANK_SIZE; 101 } 102 103 config.dev = dev; 104 config.priv = priv; 105 106 nvmem = devm_nvmem_register(dev, &config); 107 108 return PTR_ERR_OR_ZERO(nvmem); 109 } 110 111 static struct platform_driver nintendo_otp_driver = { 112 .probe = nintendo_otp_probe, 113 .driver = { 114 .name = "nintendo-otp", 115 .of_match_table = nintendo_otp_of_table, 116 }, 117 }; 118 module_platform_driver(nintendo_otp_driver); 119 MODULE_AUTHOR("Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"); 120 MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver"); 121 MODULE_LICENSE("GPL v2"); 122