Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 20099C77B61 for ; Thu, 13 Apr 2023 06:01:40 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 01DFA859FF; Thu, 13 Apr 2023 08:01:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Wg11bOz/"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8EA5785A82; Thu, 13 Apr 2023 08:00:53 +0200 (CEST) Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7619585A1A for ; Thu, 13 Apr 2023 08:00:48 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=clamor95@gmail.com Received: by mail-lf1-x12b.google.com with SMTP id d7so28739437lfj.3 for ; Wed, 12 Apr 2023 23:00:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681365647; x=1683957647; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=J3UIkoFoA+lpf0jkKRxL38iD4jK4qJHvXxp20ja4MY4=; b=Wg11bOz/g/e0BhF+JG0opp81/LX+U3y/cIbyvtuHanHwvhLy7HWPt2m1Pk9JwmZ2Jy jnRpWED8GKHCheTH6lRQqcdvn2hqt8L+tV6G8XzW4D8dJoFnMMfzv3lvdmclGmD6m1bO z75ihQ2ZyhZdOfvoAZl7SzOK+/KrG3byFX2LLs4sX5UgxqtziL7RPa5JXriV2bn34UIt 5CcLjZ+HvQGPm90So2LhvK/j+io9tmjpvOwRbkGZ3Pfvvqm2nmFt+JnCmWUkGYZIHm0j ix25NP5TA+1r6t6WX4YZgC4QP1remIQCayKmZYns90P+w/RWo72h95v/500QXZ/IY0Sz /YIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681365647; x=1683957647; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J3UIkoFoA+lpf0jkKRxL38iD4jK4qJHvXxp20ja4MY4=; b=cD1XHRYr0LZa+Y8TiSSp9CDZv4PXk7jZ0kikoh6RrDvF/likZZvWPv4xS512FMRDBP TsV84DYYQgnRkfy02QbbLUIkQNq4nwWIq7wBeLiJMJ+y+gfLcacOW1IXAI8tf01Jo37+ j3YOuK64lIFlJAI7fVsC0oxbZ3+ompvo9PRphlQiBjKXeCSFx9XpL5u486+d7V6H0cUq Ny/AMevqo4HHD9ebJJqZaIeBMPZzmffeLlfHjLc2PhPS+OAQk8vrqiWHnNnNM5pmjxLC 371keLTBlpZDQhbKF1tcp8njVBC5mMBvvinRs9HVbL4gFoEW1GDV+MlnZ4+4SXX780Lc 2Z2A== X-Gm-Message-State: AAQBX9fVURDp3OjVULOI2vpZvgHNFZQ/XWnvfrgEoF7nSqAwrEH7SRsW HmSaiph5dRonSD0YFySY+Bg= X-Google-Smtp-Source: AKy350bRUldcqg7cJdu9hfP7YHnyT4NHCRZqFqXJAmmj+4SKLC/Vzknb1WTZC6wy0itY243JUtbg7Q== X-Received: by 2002:ac2:48b8:0:b0:4eb:7fa:38b9 with SMTP id u24-20020ac248b8000000b004eb07fa38b9mr345532lfg.8.1681365647465; Wed, 12 Apr 2023 23:00:47 -0700 (PDT) Received: from xeon.. ([188.163.112.59]) by smtp.gmail.com with ESMTPSA id d1-20020ac25441000000b004e1b880ba20sm150829lfn.292.2023.04.12.23.00.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 23:00:47 -0700 (PDT) From: Svyatoslav Ryhel To: Tom Warren , Svyatoslav Ryhel , Marek Vasut , Philippe Reynes , Marcel Ziswiler , Fabio Estevam , Simon Glass , =?UTF-8?q?Jonas=20Schw=C3=B6bel?= Cc: u-boot@lists.denx.de Subject: [PATCH v2 3/4] ARM: tegra: add SoC UID calculation function Date: Thu, 13 Apr 2023 09:00:25 +0300 Message-Id: <20230413060026.30823-4-clamor95@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230413060026.30823-1-clamor95@gmail.com> References: <20230413060026.30823-1-clamor95@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This is a small tool for calculation of SoC UID based on the same Linux function. It can be further used for generation of device unique data like mac address or exposing it as serial number. Tested-by: Andreas Westman Dorcsak # ASUS Grouper E1565 Tested-by: Svyatoslav Ryhel # LG P895 T30 Signed-off-by: Svyatoslav Ryhel --- arch/arm/include/asm/arch-tegra/fuse.h | 7 ++ arch/arm/mach-tegra/Makefile | 2 +- arch/arm/mach-tegra/fuse.c | 151 +++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 arch/arm/mach-tegra/fuse.c diff --git a/arch/arm/include/asm/arch-tegra/fuse.h b/arch/arm/include/asm/arch-tegra/fuse.h index 5b8e0bd8d1..f3f2ad8e3f 100644 --- a/arch/arm/include/asm/arch-tegra/fuse.h +++ b/arch/arm/include/asm/arch-tegra/fuse.h @@ -19,4 +19,11 @@ struct fuse_regs { u32 security_mode; /* 0x1A0: FUSE_SECURITY_MODE */ }; +/** + * Calculate SoC UID + * + * Return: uid if ok, 0 on error + */ +unsigned long long tegra_chip_uid(void); + #endif /* ifndef _FUSE_H_ */ diff --git a/arch/arm/mach-tegra/Makefile b/arch/arm/mach-tegra/Makefile index 9147050b32..04e60ac853 100644 --- a/arch/arm/mach-tegra/Makefile +++ b/arch/arm/mach-tegra/Makefile @@ -14,7 +14,7 @@ endif obj-$(CONFIG_TEGRA_GP_PADCTRL) += ap.o obj-y += board.o board2.o -obj-y += cache.o +obj-y += cache.o fuse.o obj-$(CONFIG_TEGRA_CLKRST) += clock.o obj-$(CONFIG_$(SPL_)TEGRA_CRYPTO) += crypto.o obj-$(CONFIG_TEGRA_PINCTRL) += pinmux-common.o diff --git a/arch/arm/mach-tegra/fuse.c b/arch/arm/mach-tegra/fuse.c new file mode 100644 index 0000000000..2e99ae8ef7 --- /dev/null +++ b/arch/arm/mach-tegra/fuse.c @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2012-2013 + * NVIDIA Corporation + * + * (C) Copyright 2022 + * Svyatoslav Ryhel + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include "cpu.h" + +#define FUSE_UID_LOW 0x108 +#define FUSE_UID_HIGH 0x10c + +#define FUSE_VENDOR_CODE 0x200 +#define FUSE_FAB_CODE 0x204 +#define FUSE_LOT_CODE_0 0x208 +#define FUSE_LOT_CODE_1 0x20c +#define FUSE_WAFER_ID 0x210 +#define FUSE_X_COORDINATE 0x214 +#define FUSE_Y_COORDINATE 0x218 + +#define FUSE_VENDOR_CODE_MASK 0xf +#define FUSE_FAB_CODE_MASK 0x3f +#define FUSE_WAFER_ID_MASK 0x3f +#define FUSE_X_COORDINATE_MASK 0x1ff +#define FUSE_Y_COORDINATE_MASK 0x1ff + +static u32 tegra_fuse_readl(unsigned long offset) +{ + return readl(NV_PA_FUSE_BASE + offset); +} + +static void tegra_set_fuse(void) +{ + u32 reg; + + /* + * Performed by downstream and is not + * documented by TRM. Whithout setting + * this bit fuse region will not work. + */ + reg = readl_relaxed(NV_PA_CLK_RST_BASE + 0x48); + reg |= BIT(28); + writel(reg, NV_PA_CLK_RST_BASE + 0x48); + + clock_enable(PERIPH_ID_FUSE); + udelay(2); + reset_set_enable(PERIPH_ID_FUSE, 0); +} + +unsigned long long tegra_chip_uid(void) +{ + u64 uid = 0ull; + u32 reg; + u32 cid; + u32 vendor; + u32 fab; + u32 lot; + u32 wafer; + u32 x; + u32 y; + u32 i; + + tegra_set_fuse(); + + /* This used to be so much easier in prior chips. Unfortunately, there + is no one-stop shopping for the unique id anymore. It must be + constructed from various bits of information burned into the fuses + during the manufacturing process. The 64-bit unique id is formed + by concatenating several bit fields. The notation used for the + various fields is with the UID composed + thusly: + + Where: + Field Bits Position Data + ------- ---- -------- ---------------------------------------- + CID 4 60 Chip id + VENDOR 4 56 Vendor code + FAB 6 50 FAB code + LOT 26 24 Lot code (5-digit base-36-coded-decimal, + re-encoded to 26 bits binary) + WAFER 6 18 Wafer id + X 9 9 Wafer X-coordinate + Y 9 0 Wafer Y-coordinate + ------- ---- + Total 64 + */ + + switch (tegra_get_chip()) { + case CHIPID_TEGRA20: + /* T20 has simple calculation */ + return ((unsigned long long)tegra_fuse_readl(FUSE_UID_HIGH) << 32ull) | + (unsigned long long)tegra_fuse_readl(FUSE_UID_LOW); + case CHIPID_TEGRA30: + /* T30 chip id is 0 */ + cid = 0; + break; + case CHIPID_TEGRA114: + /* T11x chip id is 1 */ + cid = 1; + break; + case CHIPID_TEGRA124: + /* T12x chip id is 3 */ + cid = 3; + break; + case CHIPID_TEGRA210: + /* T210 chip id is 5 */ + cid = 5; + default: + return 0; + } + + vendor = tegra_fuse_readl(FUSE_VENDOR_CODE) & FUSE_VENDOR_CODE_MASK; + fab = tegra_fuse_readl(FUSE_FAB_CODE) & FUSE_FAB_CODE_MASK; + + /* Lot code must be re-encoded from a 5 digit base-36 'BCD' number + to a binary number. */ + lot = 0; + reg = tegra_fuse_readl(FUSE_LOT_CODE_0) << 2; + + for (i = 0; i < 5; ++i) { + u32 digit = (reg & 0xFC000000) >> 26; + lot *= 36; + lot += digit; + reg <<= 6; + } + + wafer = tegra_fuse_readl(FUSE_WAFER_ID) & FUSE_WAFER_ID_MASK; + x = tegra_fuse_readl(FUSE_X_COORDINATE) & FUSE_X_COORDINATE_MASK; + y = tegra_fuse_readl(FUSE_Y_COORDINATE) & FUSE_Y_COORDINATE_MASK; + + uid = ((unsigned long long)cid << 60ull) + | ((unsigned long long)vendor << 56ull) + | ((unsigned long long)fab << 50ull) + | ((unsigned long long)lot << 24ull) + | ((unsigned long long)wafer << 18ull) + | ((unsigned long long)x << 9ull) + | ((unsigned long long)y << 0ull); + + return uid; +} -- 2.37.2