From patchwork Tue Apr 21 08:00:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 11500713 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 08A4692C for ; Tue, 21 Apr 2020 08:02:32 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D9B5F2071C for ; Tue, 21 Apr 2020 08:02:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="PeBmBCWS"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="ha1EhZTr" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D9B5F2071C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QGbxJXf7nWOqIbUKolIJm95B6yTdhJEFaizEUwdye8w=; b=PeBmBCWSnwkYuG O0SGKdWOCgaQ6yD6Ar3spzzEnU2POhgx5NlCFBVW3B+QQ0GrWBfUpW8YeSscl/KjLa84L5dS4vHNe ME0KKq9pnW3i9U3uR5mz09jb82ouhZO+gzNy/g/V7Y6bbyN8ixfkPh2cMytARrI2RhMBTQDGCZll+ jab/okE+Ll+obV6YRK1xgOeeh0pLAFADT1KwJGWdstxcJzsQUyHptZNY4lgqmNuudYsqfDefMUSpL MG6u0l8O5OlR1HGdE7/fzi7S+ou8iXm/u2XVl6h67uLfvCzIiebMpNUR5gQ8d25OI/tZd6nQ3aoUh 9UlzkcIRF64PkOopQ3tg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jQnrS-0002M6-DG; Tue, 21 Apr 2020 08:02:30 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jQnqE-0001BD-PE for linux-amlogic@lists.infradead.org; Tue, 21 Apr 2020 08:01:17 +0000 Received: by mail-wm1-x341.google.com with SMTP id 188so2471209wmc.2 for ; Tue, 21 Apr 2020 01:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Y7Fjt5IgWK4L8ZaTfnqiN9Z1czwHhj1bMsFl3W8CxoM=; b=ha1EhZTrXKUsj9NuoE1itCGNDmEaBBo2GjdzWF1OreI69GTM5lZk1hhpO3kvkHk8zo XjzrWBZsnN/Q8csthmQWV8hqRkw7PAFdh9WA8CFGKGcm7Qx3CctudF2FJK+9jEbKLCmo WZ7G0mjBe6gC3cCM93lviLhkWCB8ERrs8nuM92Mv1EQl4kzIVKRfbGoF5MkiAgzWa1Pn u3DNLUregN0KvcmooXOT+11ND8dzJwMBb0MyZUas5vapR17S8iul5NLQxhJc7+yJh8vH 2BUhqjgru0JXOrPbkLFaoHeHgwX28Z+BTa6l1iyHwSYywkm/ym8wUOm9uAbqvQsPbfGu 2rsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Y7Fjt5IgWK4L8ZaTfnqiN9Z1czwHhj1bMsFl3W8CxoM=; b=GTXA/AP+85Ta0P3cNQDGnpRTk3u7uu9U6CD0gLO+ruDYVHKF51q2ZcdApO7NFnu/KB NtbKRaYZ7HN/md0BAyx6ZPcPMvSwH9b70mqOdW0ezlhyKn7re/1GioA4kr5cX4ooO6oL ZIiGgCFbrm+9I9qbtgsnkaunAnDofGhYGAayQn50TBTmBQlb/Ti3MpenBRdRcOFAQbgc Al4/R6t3psmOdOUGgHr3jlxOncJrY19tk1wPFybPVisAaEzj9Rl8VocoyIznV1Rz7sDo X4wFmZFLHKPN4HfLLooubPrkVGeunL16XsyAH4RKn5tuwyUFFcbCoU4EUidrNo0E2vdQ SLcA== X-Gm-Message-State: AGi0Pubk07i5uCZF3u38gjMTD9qXn4bNrJe0KLZ9Oqr/2Vi+qKTt/VBG bLR4e1tQ/S1aZNhhgRgP2p6U4A== X-Google-Smtp-Source: APiQypKUJ/cjTU5KdVOCEhHx4v5y/Sf1RAOTdDgskIOOCXQ8YXmV5MGxkoDqUwhWM+8v+GBo+qgS1Q== X-Received: by 2002:a1c:ed18:: with SMTP id l24mr3481548wmh.122.1587456073043; Tue, 21 Apr 2020 01:01:13 -0700 (PDT) Received: from localhost.localdomain ([2a01:e35:2ec0:82b0:39cc:a07:8b48:cc56]) by smtp.gmail.com with ESMTPSA id x132sm2561552wmg.33.2020.04.21.01.01.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Apr 2020 01:01:12 -0700 (PDT) From: Neil Armstrong To: lee.jones@linaro.org, jdelvare@suse.com, linux@roeck-us.net, srinivas.kandagatla@linaro.org Subject: [RFC 4/8] nvmem: add support for the Khadas MCU Programmable User Memory Date: Tue, 21 Apr 2020 10:00:58 +0200 Message-Id: <20200421080102.22796-5-narmstrong@baylibre.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20200421080102.22796-1-narmstrong@baylibre.com> References: <20200421080102.22796-1-narmstrong@baylibre.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200421_010114_934222_2A2699FD X-CRM114-Status: GOOD ( 17.26 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.4 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:341 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-hwmon@vger.kernel.org, Neil Armstrong , linux-kernel@vger.kernel.org, nick@khadas.com, linux-amlogic@lists.infradead.org, art@khadas.com, linux-arm-kernel@lists.infradead.org Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org The new Khadas VIM2, VIM3 and Edge boards embeds an on-board microcontroller offering a 56bytes User Programmable NVMEM array. This array needs a password to be writable, thus a password sysfs file has been added on the device node to unlock the NVMEM. The default 6bytes password id: "Khadas" This implements the user NVMEM devices as cell of the Khadas MCU MFD driver. Signed-off-by: Neil Armstrong --- drivers/nvmem/Kconfig | 8 ++ drivers/nvmem/Makefile | 2 + drivers/nvmem/khadas-mcu-user-mem.c | 128 ++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 drivers/nvmem/khadas-mcu-user-mem.c diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index d7b7f6d688e7..92cd4f6aa931 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -67,6 +67,14 @@ config JZ4780_EFUSE To compile this driver as a module, choose M here: the module will be called nvmem_jz4780_efuse. +config NVMEM_KHADAS_MCU_USER_MEM + tristate "Khadas MCU User programmable memory support" + depends on MFD_KHADAS_MCU + depends on REGMAP + help + This is a driver for the MCU User programmable memory + available on the Khadas VIM and Edge boards. + config NVMEM_LPC18XX_EEPROM tristate "NXP LPC18XX EEPROM Memory Support" depends on ARCH_LPC18XX || COMPILE_TEST diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index a7c377218341..0516a309542d 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -17,6 +17,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU) += nvmem-imx-ocotp-scu.o nvmem-imx-ocotp-scu-y := imx-ocotp-scu.o obj-$(CONFIG_JZ4780_EFUSE) += nvmem_jz4780_efuse.o nvmem_jz4780_efuse-y := jz4780-efuse.o +obj-$(CONFIG_NVMEM_KHADAS_MCU_USER_MEM) += nvmem-khadas-mcu-user-mem.o +nvmem-khadas-mcu-user-mem-y := khadas-mcu-user-mem.o obj-$(CONFIG_NVMEM_LPC18XX_EEPROM) += nvmem_lpc18xx_eeprom.o nvmem_lpc18xx_eeprom-y := lpc18xx_eeprom.o obj-$(CONFIG_NVMEM_LPC18XX_OTP) += nvmem_lpc18xx_otp.o diff --git a/drivers/nvmem/khadas-mcu-user-mem.c b/drivers/nvmem/khadas-mcu-user-mem.c new file mode 100644 index 000000000000..a1d5ae9a030c --- /dev/null +++ b/drivers/nvmem/khadas-mcu-user-mem.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for Khadas MCU User programmable Memory + * + * Copyright (C) 2020 BayLibre SAS + * Author(s): Neil Armstrong + */ + +#include +#include +#include +#include +#include +#include +#include + +static int khadas_mcu_user_mem_read(void *context, unsigned int offset, + void *val, size_t bytes) +{ + struct khadas_mcu *khadas_mcu = context; + + return regmap_bulk_read(khadas_mcu->map, + KHADAS_MCU_USER_DATA_0_REG + offset, + val, bytes); +} + +static int khadas_mcu_user_mem_write(void *context, unsigned int offset, + void *val, size_t bytes) +{ + struct khadas_mcu *khadas_mcu = context; + + return regmap_bulk_write(khadas_mcu->map, + KHADAS_MCU_USER_DATA_0_REG + offset, + val, bytes); +} + +static ssize_t password_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct khadas_mcu *khadas_mcu = dev_get_drvdata(dev); + int i, ret; + + if (count < 6) + return -EINVAL; + + ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 1); + if (ret) + return ret; + + for (i = 0 ; i < 6 ; ++i) { + ret = regmap_write(khadas_mcu->map, + KHADAS_MCU_CHECK_USER_PASSWD_REG, + buf[i]); + if (ret) + goto out; + } + + ret = regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0); + if (ret) + return ret; + + return count; +out: + regmap_write(khadas_mcu->map, KHADAS_MCU_PASSWD_START_REG, 0); + + return ret; +} + +static DEVICE_ATTR_WO(password); + +static struct attribute *khadas_mcu_user_mem_sysfs_attributes[] = { + &dev_attr_password.attr, + NULL, +}; + +static const struct attribute_group khadas_mcu_user_mem_sysfs_attr_group = { + .attrs = khadas_mcu_user_mem_sysfs_attributes, +}; + +static int khadas_mcu_user_mem_probe(struct platform_device *pdev) +{ + struct khadas_mcu *khadas_mcu = dev_get_drvdata(pdev->dev.parent); + struct device *dev = &pdev->dev; + struct nvmem_device *nvmem; + struct nvmem_config *econfig; + + econfig = devm_kzalloc(dev, sizeof(*econfig), GFP_KERNEL); + if (!econfig) + return -ENOMEM; + + econfig->dev = pdev->dev.parent; + econfig->name = dev_name(pdev->dev.parent); + econfig->stride = 1; + econfig->word_size = 1; + econfig->reg_read = khadas_mcu_user_mem_read; + econfig->reg_write = khadas_mcu_user_mem_write; + econfig->size = 56; + econfig->priv = khadas_mcu; + + platform_set_drvdata(pdev, khadas_mcu); + + nvmem = devm_nvmem_register(&pdev->dev, econfig); + if (IS_ERR(nvmem)) + return PTR_ERR(nvmem); + + return sysfs_create_group(&pdev->dev.kobj, + &khadas_mcu_user_mem_sysfs_attr_group); +} + +static const struct platform_device_id khadas_mcu_user_mem_id_table[] = { + { .name = "khadas-mcu-user-mem", }, + {}, +}; +MODULE_DEVICE_TABLE(platform, khadas_mcu_user_mem_id_table); + +static struct platform_driver khadas_mcu_user_mem_driver = { + .probe = khadas_mcu_user_mem_probe, + .driver = { + .name = "khadas-mcu-user-mem", + }, + .id_table = khadas_mcu_user_mem_id_table, +}; + +module_platform_driver(khadas_mcu_user_mem_driver); + +MODULE_AUTHOR("Neil Armstrong "); +MODULE_DESCRIPTION("Khadas MCU User MEM driver"); +MODULE_LICENSE("GPL v2");