Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1727667pxj; Wed, 19 May 2021 12:27:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwg6Xt4d9CApsnW/enMmqBSAsY9xR8i73O/muBLSU4bbTZ6mZi/STsBs4Ekl3aUUIhjk9DM X-Received: by 2002:a05:6e02:118b:: with SMTP id y11mr713673ili.241.1621452437364; Wed, 19 May 2021 12:27:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621452437; cv=none; d=google.com; s=arc-20160816; b=YnUtCkvU1DkZBFpZ923gvxvusrIuw0e8qRrO5TElTHyw2FD0cMIpGBgcY79jwbxb+7 a4N9GPctBFSdHTDbQ9rODAhk3nFOE/ZUJqV6x3VA/HMjNTz536ZfAXuV3M4IHuePqwH+ 5tQpD67+rAikmK55SF4sTInF2itMhBq8N/+HmV+RWTuMbb0xBVIhYmrUKGW3wHZuJf2x 53x4tsBnuS2fpuT6+Z2QGOlpZy6q7ljwoCyfUgPcn+5uGlAOdd0IShB7b2E5aJp4qttg iFDfFKxGgZshILUGoEsozqIpMgg47gAMYnz9L+qB4+IxnuxYHCdIdiDYpQZhmxlBGyCT r2uA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=OuUSAfHGuSfhr9MvHqWWbFO+Z/byxlEl4XI56mM93pk=; b=axB+vFoW1uDvbCYjCkjiFHKvh3zUK/6r/G+TlRtSbihKoBBxo+GsEJKg3XgHFCfyT4 42cozwx3/C94kBvVHtwr0YED6NCFOiQX2XKK3JAmZIJ1GWoYM1MqN4GAaeDXidOL8w7L MAwsQJkR/944QBj7i2AWDRRXy3HkMxIzP3g+gs2uRm/hNA0RGtqSDEE2WyZDcQQM6w3a RoEBZo7fqDrjWAQLuCDelN5EqEjTDMGHhQ1gcYw7XuVpJ1cymM9PNk5qQsfT4uJbJRm/ eYfZYbsGwj4v4a3Z9IMnzWIrWChnICibjFDmOB9ApD7w/UB7RNsxFNv1OC+esD/ij90V J2kg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o21si227057jat.63.2021.05.19.12.27.04; Wed, 19 May 2021 12:27:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346080AbhESJwX (ORCPT + 99 others); Wed, 19 May 2021 05:52:23 -0400 Received: from 82-65-109-163.subs.proxad.net ([82.65.109.163]:41956 "EHLO luna.linkmauve.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230516AbhESJwQ (ORCPT ); Wed, 19 May 2021 05:52:16 -0400 Received: by luna.linkmauve.fr (Postfix, from userid 1000) id 92EE8F40629; Wed, 19 May 2021 11:50:47 +0200 (CEST) From: Emmanuel Gil Peyrot To: Srinivas Kandagatla , linuxppc-dev@lists.ozlabs.org, devicetree@vger.kernel.org Cc: Emmanuel Gil Peyrot , Ash Logan , =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= , Rob Herring , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/4] nvmem: nintendo-otp: Add new driver for the Wii and Wii U OTP Date: Wed, 19 May 2021 11:50:41 +0200 Message-Id: <20210519095044.4109-2-linkmauve@linkmauve.fr> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210519095044.4109-1-linkmauve@linkmauve.fr> References: <20210519095044.4109-1-linkmauve@linkmauve.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This OTP is read-only and contains various keys used by the console to decrypt, encrypt or verify various pieces of storage. Its size depends on the console, it is 128 bytes on the Wii and 1024 bytes on the Wii U (split into eight 128 bytes banks). It can be used directly by writing into one register and reading from the other one, without any additional synchronisation. Signed-off-by: Emmanuel Gil Peyrot --- drivers/nvmem/Kconfig | 11 ++++ drivers/nvmem/Makefile | 2 + drivers/nvmem/nintendo-otp.c | 115 +++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 drivers/nvmem/nintendo-otp.c diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index dd2019006838..dd6196e49b2d 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -107,6 +107,17 @@ config MTK_EFUSE This driver can also be built as a module. If so, the module will be called efuse-mtk. +config NVMEM_NINTENDO_OTP + tristate "Nintendo Wii and Wii U OTP Support" + help + This is a driver to expose the OTP on a Nintendo Wii or Wii U. + + This memory contains common and per-console keys, signatures and + related data required to access peripherals. + + This driver can also be built as a module. If so, the module + will be called nvmem-nintendo-otp. + config QCOM_QFPROM tristate "QCOM QFPROM Support" depends on ARCH_QCOM || COMPILE_TEST diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index bbea1410240a..dcbbde35b6a8 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -23,6 +23,8 @@ obj-$(CONFIG_NVMEM_LPC18XX_OTP) += nvmem_lpc18xx_otp.o nvmem_lpc18xx_otp-y := lpc18xx_otp.o obj-$(CONFIG_NVMEM_MXS_OCOTP) += nvmem-mxs-ocotp.o nvmem-mxs-ocotp-y := mxs-ocotp.o +obj-$(CONFIG_NVMEM_NINTENDO_OTP) += nvmem-nintendo-otp.o +nvmem-nintendo-otp-y := nintendo-otp.o obj-$(CONFIG_MTK_EFUSE) += nvmem_mtk-efuse.o nvmem_mtk-efuse-y := mtk-efuse.o obj-$(CONFIG_QCOM_QFPROM) += nvmem_qfprom.o diff --git a/drivers/nvmem/nintendo-otp.c b/drivers/nvmem/nintendo-otp.c new file mode 100644 index 000000000000..de6f5d7c10ef --- /dev/null +++ b/drivers/nvmem/nintendo-otp.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Nintendo Wii and Wii U OTP driver + * + * Copyright (C) 2021 Emmanuel Gil Peyrot + */ + +#include +#include +#include +#include +#include +#include +#include + +#define HW_OTPCMD 0 +#define HW_OTPDATA 4 +#define OTP_READ 0x80000000 + +struct nintendo_otp_priv { + void __iomem *regs; +}; + +struct nintendo_otp_devtype_data { + const char *name; + unsigned int num_banks; +}; + +static const struct nintendo_otp_devtype_data hollywood_otp_data = { + .name = "wii-otp", + .num_banks = 1, +}; + +static const struct nintendo_otp_devtype_data latte_otp_data = { + .name = "wiiu-otp", + .num_banks = 8, +}; + +static int nintendo_otp_reg_read(void *context, + unsigned int reg, void *_val, size_t bytes) +{ + struct nintendo_otp_priv *priv = context; + u32 *val = _val; + int words = bytes >> 2; + u32 bank, addr; + + while (words--) { + bank = (reg << 1) & ~0xff; + addr = (reg >> 2) & 0x1f; + iowrite32be(OTP_READ | bank | addr, priv->regs + HW_OTPCMD); + *val++ = ioread32be(priv->regs + HW_OTPDATA); + reg += 4; + } + + return 0; +} + +static const struct of_device_id nintendo_otp_of_table[] = { + { .compatible = "nintendo,hollywood-otp", .data = &hollywood_otp_data }, + { .compatible = "nintendo,latte-otp", .data = &latte_otp_data }, + {/* sentinel */}, +}; +MODULE_DEVICE_TABLE(of, nintendo_otp_of_table); + +static int nintendo_otp_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + const struct of_device_id *of_id = + of_match_device(nintendo_otp_of_table, dev); + struct resource *res; + struct nvmem_device *nvmem; + struct nintendo_otp_priv *priv; + + struct nvmem_config config = { + .stride = 4, + .word_size = 4, + .reg_read = nintendo_otp_reg_read, + .read_only = true, + .root_only = true, + }; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + priv->regs = devm_ioremap_resource(dev, res); + if (IS_ERR(priv->regs)) + return PTR_ERR(priv->regs); + + if (of_id->data) { + const struct nintendo_otp_devtype_data *data = of_id->data; + config.name = data->name; + config.size = data->num_banks * 128; + } + + config.dev = dev; + config.priv = priv; + + nvmem = devm_nvmem_register(dev, &config); + + return PTR_ERR_OR_ZERO(nvmem); +} + +static struct platform_driver nintendo_otp_driver = { + .probe = nintendo_otp_probe, + .driver = { + .name = "nintendo-otp", + .of_match_table = nintendo_otp_of_table, + }, +}; +module_platform_driver(nintendo_otp_driver); +MODULE_AUTHOR("Emmanuel Gil Peyrot "); +MODULE_DESCRIPTION("Nintendo Wii and Wii U OTP driver"); +MODULE_LICENSE("GPL v2"); -- 2.31.1