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: 11500695 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 3457992C for ; Tue, 21 Apr 2020 08:01:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1773121D79 for ; Tue, 21 Apr 2020 08:01:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="ha1EhZTr" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728276AbgDUIB3 (ORCPT ); Tue, 21 Apr 2020 04:01:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728191AbgDUIBO (ORCPT ); Tue, 21 Apr 2020 04:01:14 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7581BC061A0F for ; Tue, 21 Apr 2020 01:01:14 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id t63so2462391wmt.3 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=IAhI2sWtHXsLyQV1mamJTf6Ade8s8IxNOM6FDQxSFCkqaCCB486HwO5rxbn4NGu7Y4 ejQTFtRGmHUt0R79NzHQxSPd1+rTdXFBqK3LNUYZ5JzQhAVNr2zm12twZEtS4p0oCQaC gBzFuX/9gnR6/PvWPkZ3S6nt9XUlXD+TLQNbPwVDKXeHTtE72iCiWylzzi2XZ+xHds/Z YgbdlZ+G+KfcnBA/vQb+qSmfTbzaoCwToVbcDweSeU8Kkk6MWjLElhE9g7/Uo7uZ71/q I0S0cIPvKUESzj7VRWpnYTwU1/TWRgtaJYPcJtjBp8q3gWO1l7y8iT72VULdDUGMyfXX bHKA== X-Gm-Message-State: AGi0PublpZ4eRhsgfl7VieDJkL47nwAfqhuTPF8XiydrLebVFsiwFklk HMf+MJLd548jQrpNxj0sVCiiVA== 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 Cc: nick@khadas.com, art@khadas.com, Neil Armstrong , linux-amlogic@lists.infradead.org, linux-hwmon@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.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 Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.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");