All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Peng Fan (OSS)" <peng.fan@oss.nxp.com>
To: sbabic@denx.de, festevam@gmail.com
Cc: uboot-imx@nxp.com, u-boot@lists.denx.de, Ye Li <ye.li@nxp.com>
Subject: [PATCH V2 36/46] driver: misc: imx8ulp: Add fuse driver for imx8ulp
Date: Tue, 29 Jun 2021 10:32:30 +0800	[thread overview]
Message-ID: <20210629023240.22394-37-peng.fan@oss.nxp.com> (raw)
In-Reply-To: <20210629023240.22394-1-peng.fan@oss.nxp.com>

From: Ye Li <ye.li@nxp.com>

This driver uses FSB to read some fuses, but not support program fuse.
It only works in SPL (secure mode), u-boot needs traps to ATF to
read them.

Some fuses can read from S400 API and others are from FSB.
Also support program some fuses via S400 API

Signed-off-by: Ye Li <ye.li@nxp.com>
---
 drivers/misc/imx8ulp/Makefile |   1 +
 drivers/misc/imx8ulp/fuse.c   | 198 ++++++++++++++++++++++++++++++++++
 2 files changed, 199 insertions(+)
 create mode 100644 drivers/misc/imx8ulp/fuse.c

diff --git a/drivers/misc/imx8ulp/Makefile b/drivers/misc/imx8ulp/Makefile
index 1d792415d2..927cc55216 100644
--- a/drivers/misc/imx8ulp/Makefile
+++ b/drivers/misc/imx8ulp/Makefile
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0+
 
 obj-y += s400_api.o imx8ulp_mu.o
+obj-$(CONFIG_CMD_FUSE) += fuse.o
diff --git a/drivers/misc/imx8ulp/fuse.c b/drivers/misc/imx8ulp/fuse.c
new file mode 100644
index 0000000000..d1feb62ab5
--- /dev/null
+++ b/drivers/misc/imx8ulp/fuse.c
@@ -0,0 +1,198 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 NXP
+ */
+
+#include <common.h>
+#include <console.h>
+#include <errno.h>
+#include <fuse.h>
+#include <asm/arch/sys_proto.h>
+#include <asm/arch/imx-regs.h>
+#include <env.h>
+#include <asm/arch/s400_api.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define FUSE_BANKS	64
+#define WORDS_PER_BANKS 8
+
+struct fsb_map_entry {
+	s32 fuse_bank;
+	u32 fuse_words;
+	bool redundancy;
+};
+
+struct s400_map_entry {
+	s32 fuse_bank;
+	u32 fuse_words;
+	u32 fuse_offset;
+	u32 s400_index;
+};
+
+struct fsb_map_entry fsb_mapping_table[] = {
+	{ 3, 8 },
+	{ 4, 8 },
+	{ 5, 8 },
+	{ 6, 8 },
+	{ -1, 48 }, /* Reserve 48 words */
+	{ 8,  4, true },
+	{ 24, 4, true },
+	{ 26, 4, true },
+	{ 27, 4, true },
+	{ 28, 8 },
+	{ 29, 8 },
+	{ 30, 8 },
+	{ 31, 8 },
+	{ 37, 8 },
+	{ 38, 8 },
+	{ 39, 8 },
+	{ 40, 8 },
+	{ 41, 8 },
+	{ 42, 8 },
+	{ 43, 8 },
+	{ 44, 8 },
+	{ 45, 8 },
+	{ 46, 8 },
+};
+
+struct s400_map_entry s400_api_mapping_table[] = {
+	{ 1, 8 },	/* LOCK */
+	{ 2, 8 },	/* ECID */
+	{ 7, 4, 0, 1 },	/* OTP_UNIQ_ID */
+	{ 23, 1, 4, 2 }, /* OTFAD */
+};
+
+static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
+{
+	s32 size = ARRAY_SIZE(fsb_mapping_table);
+	s32 i, word_pos = 0;
+
+	/* map the fuse from ocotp fuse map to FSB*/
+	for (i = 0; i < size; i++) {
+		if (fsb_mapping_table[i].fuse_bank != -1 &&
+		    fsb_mapping_table[i].fuse_bank == bank) {
+			break;
+		}
+
+		word_pos += fsb_mapping_table[i].fuse_words;
+	}
+
+	if (i == size)
+		return -1; /* Failed to find */
+
+	if (fsb_mapping_table[i].redundancy) {
+		*redundancy = true;
+		return (word >> 1) + word_pos;
+	}
+
+	*redundancy = false;
+	return word + word_pos;
+}
+
+static s32 map_s400_fuse_index(u32 bank, u32 word)
+{
+	s32 size = ARRAY_SIZE(s400_api_mapping_table);
+	s32 i;
+
+	/* map the fuse from ocotp fuse map to FSB*/
+	for (i = 0; i < size; i++) {
+		if (s400_api_mapping_table[i].fuse_bank != -1 &&
+		    s400_api_mapping_table[i].fuse_bank == bank) {
+			if (word >= s400_api_mapping_table[i].fuse_offset &&
+			    word < (s400_api_mapping_table[i].fuse_offset +
+			    s400_api_mapping_table[i].fuse_words))
+				break;
+		}
+	}
+
+	if (i == size)
+		return -1; /* Failed to find */
+
+	if (s400_api_mapping_table[i].s400_index != 0)
+		return s400_api_mapping_table[i].s400_index;
+
+	return s400_api_mapping_table[i].fuse_bank * 8 + word;
+}
+
+int fuse_sense(u32 bank, u32 word, u32 *val)
+{
+	s32 word_index;
+	bool redundancy;
+
+	if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
+		return -EINVAL;
+
+	word_index = map_fsb_fuse_index(bank, word, &redundancy);
+	if (word_index >= 0) {
+		*val = readl((ulong)FSB_BASE_ADDR + 0x800 + (word_index << 2));
+		if (redundancy)
+			*val = (*val >> ((word % 2) * 16)) & 0xFFFF;
+
+		return 0;
+	}
+
+	word_index = map_s400_fuse_index(bank, word);
+	if (word_index >= 0) {
+		u32 data[4];
+		u32 res, size = 4;
+		int ret;
+
+		/* Only UID return 4 words */
+		if (word_index != 1)
+			size = 1;
+
+		ret = ahab_read_common_fuse(word_index, data, size, &res);
+		if (ret) {
+			printf("ahab read fuse failed %d, 0x%x\n", ret, res);
+			return ret;
+		}
+
+		if (word_index == 1) {
+			*val = data[word]; /* UID */
+		} else if (word_index == 2) {
+			/*
+			 * OTFAD 3 bits as follow:
+			 * bit 0: OTFAD_ENABLE
+			 * bit 1: OTFAD_DISABLE_OVERRIDE
+			 * bit 2: KEY_BLOB_EN
+			 */
+			*val = data[0] << 3;
+		} else {
+			*val = data[0];
+		}
+
+		return 0;
+	}
+
+	return -ENOENT;
+}
+
+int fuse_read(u32 bank, u32 word, u32 *val)
+{
+	return fuse_sense(bank, word, val);
+}
+
+int fuse_prog(u32 bank, u32 word, u32 val)
+{
+	u32 res;
+	int ret;
+
+	if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
+		return -EINVAL;
+
+	ret = ahab_write_fuse((bank * 8 + word), val, false, &res);
+	if (ret) {
+		printf("ahab write fuse failed %d, 0x%x\n", ret, res);
+		return ret;
+	}
+
+	return 0;
+}
+
+int fuse_override(u32 bank, u32 word, u32 val)
+{
+	printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
+	return -EPERM;
+}
-- 
2.30.0


  parent reply	other threads:[~2021-06-29  2:06 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-29  2:31 [PATCH V2 00/46] imx: add i.MX8ULP support Peng Fan (OSS)
2021-06-29  2:31 ` [PATCH V2 01/46] arm: imx: add i.MX8ULP basic Kconfig option Peng Fan (OSS)
2021-06-29  2:31 ` [PATCH V2 02/46] arm: imx: add i.MX8ULP cpu type and helper Peng Fan (OSS)
2021-06-29  2:31 ` [PATCH V2 03/46] arm: imx: sys_proto: move boot mode define to common header Peng Fan (OSS)
2021-06-29  2:31 ` [PATCH V2 04/46] arm: imx8ulp: support print cpu info Peng Fan (OSS)
2021-06-29  2:31 ` [PATCH V2 05/46] imx: imx8ulp: add get reset cause Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 06/46] arm: imx: basic i.MX8ULP support Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 07/46] arm: imx8: Move container parser and image to mach-imx common folder Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 08/46] arm: imx8: Move container image header file to mach-imx Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 09/46] arm: imx: parse-container: guard included header files Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 10/46] arm: imx8ulp: add container support Peng Fan (OSS)
2021-07-17 12:49   ` Stefano Babic
2021-07-19  1:41     ` Peng Fan
2021-06-29  2:32 ` [PATCH V2 11/46] arm: imx: move container Kconfig under mach-imx Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 12/46] driver: misc: Add MU and S400 API to communicate with Sentinel Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 13/46] net: fec_mxc: support i.MX8ULP Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 14/46] pinctrl: Add pinctrl driver for imx8ulp Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 15/46] driver: serial: fsl_lpuart: support i.MX8ULP Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 16/46] arm: imx8ulp: add clock support Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 17/46] drivers: mmc: fsl_esdhc_imx: support i.MX8ULP Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 18/46] arm: imx8ulp: soc: Change to use CMC1 to get bootcfg Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 19/46] arm: imx8ulp: Enable full L2 cache in SPL Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 20/46] arm: imx8ulp: disable wdog3 Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 21/46] arm: imx8ulp: Update the reset vector in u-boot Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 22/46] drivers: misc: s400_api: Update S400_SUCCESS_IND to 0xd6 Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 23/46] drivers: misc: imx8ulp: Add S400 API for image authentication Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 24/46] drivers: misc: imx8ulp: Update S400 API for release RDC Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 25/46] drivers: misc: s400_api: Update API for fuse read and write Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 26/46] arm: imx8ulp: release and configure XRDC at early phase Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 27/46] arm: imx8ulp: add rdc support Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 28/46] arm: imx8ulp: add trdc release request Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 29/46] arm: imx8ulp: release trdc and assign lpav from RTD to APD Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 30/46] imx8ulp: unify rdc functions Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 31/46] arm: imx8ulp: Probe the S400 MU device in arch init Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 32/46] arm: iMX8ULP: Add boot device relevant functions Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 33/46] arm: imx8ulp: Allocate DCNANO and MIPI_DSI to AD domain Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 34/46] arm: imx8ulp: add dummy imx_get_mac_from_fuse Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 35/46] arm: imx8ulp: add iomuxc support Peng Fan (OSS)
2021-06-29  2:32 ` Peng Fan (OSS) [this message]
2021-06-29  2:32 ` [PATCH V2 37/46] imx8ulp: soc: correct reset cause Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 38/46] imx8ulp: Use DGO_GP5 to get boot config Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 39/46] imx8ulp: Add workaround for eMMC boot Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 40/46] mx7ulp: Update unlock and refresh sequences in sWDOG driver Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 41/46] mx7ulp: wdog: Wait for WDOG unlock and reconfiguration to complete Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 42/46] imx8ulp: move struct mu_type to common header Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 43/46] imx8ulp: add upower api support Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 44/46] ddr: Add DDR driver for iMX8ULP Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 45/46] arm: dts: add i.MX8ULP dtsi Peng Fan (OSS)
2021-06-29  2:32 ` [PATCH V2 46/46] arm: imx: add i.MX8ULP EVK support Peng Fan (OSS)
2021-07-15  3:29 ` [PATCH V2 00/46] imx: add i.MX8ULP support Peng Fan (OSS)
2021-07-15  8:11   ` Stefano Babic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210629023240.22394-37-peng.fan@oss.nxp.com \
    --to=peng.fan@oss.nxp.com \
    --cc=festevam@gmail.com \
    --cc=sbabic@denx.de \
    --cc=u-boot@lists.denx.de \
    --cc=uboot-imx@nxp.com \
    --cc=ye.li@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.