18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2013-2015, NVIDIA CORPORATION.  All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/device.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/bug.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <soc/tegra/fuse.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "fuse.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define CPU_PROCESS_CORNERS	2
158c2ecf20Sopenharmony_ci#define GPU_PROCESS_CORNERS	2
168c2ecf20Sopenharmony_ci#define SOC_PROCESS_CORNERS	3
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define FUSE_CPU_SPEEDO_0	0x014
198c2ecf20Sopenharmony_ci#define FUSE_CPU_SPEEDO_1	0x02c
208c2ecf20Sopenharmony_ci#define FUSE_CPU_SPEEDO_2	0x030
218c2ecf20Sopenharmony_ci#define FUSE_SOC_SPEEDO_0	0x034
228c2ecf20Sopenharmony_ci#define FUSE_SOC_SPEEDO_1	0x038
238c2ecf20Sopenharmony_ci#define FUSE_SOC_SPEEDO_2	0x03c
248c2ecf20Sopenharmony_ci#define FUSE_CPU_IDDQ		0x018
258c2ecf20Sopenharmony_ci#define FUSE_SOC_IDDQ		0x040
268c2ecf20Sopenharmony_ci#define FUSE_GPU_IDDQ		0x128
278c2ecf20Sopenharmony_ci#define FUSE_FT_REV		0x028
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cienum {
308c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_0,
318c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_1,
328c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_COUNT,
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const u32 __initconst cpu_process_speedos[][CPU_PROCESS_CORNERS] = {
368c2ecf20Sopenharmony_ci	{ 2119, UINT_MAX },
378c2ecf20Sopenharmony_ci	{ 2119, UINT_MAX },
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic const u32 __initconst gpu_process_speedos[][GPU_PROCESS_CORNERS] = {
418c2ecf20Sopenharmony_ci	{ UINT_MAX, UINT_MAX },
428c2ecf20Sopenharmony_ci	{ UINT_MAX, UINT_MAX },
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic const u32 __initconst soc_process_speedos[][SOC_PROCESS_CORNERS] = {
468c2ecf20Sopenharmony_ci	{ 1950, 2100, UINT_MAX },
478c2ecf20Sopenharmony_ci	{ 1950, 2100, UINT_MAX },
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic u8 __init get_speedo_revision(void)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	return tegra_fuse_read_spare(4) << 2 |
538c2ecf20Sopenharmony_ci	       tegra_fuse_read_spare(3) << 1 |
548c2ecf20Sopenharmony_ci	       tegra_fuse_read_spare(2) << 0;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void __init rev_sku_to_speedo_ids(struct tegra_sku_info *sku_info,
588c2ecf20Sopenharmony_ci					 u8 speedo_rev, int *threshold)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	int sku = sku_info->sku_id;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/* Assign to default */
638c2ecf20Sopenharmony_ci	sku_info->cpu_speedo_id = 0;
648c2ecf20Sopenharmony_ci	sku_info->soc_speedo_id = 0;
658c2ecf20Sopenharmony_ci	sku_info->gpu_speedo_id = 0;
668c2ecf20Sopenharmony_ci	*threshold = THRESHOLD_INDEX_0;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	switch (sku) {
698c2ecf20Sopenharmony_ci	case 0x00: /* Engineering SKU */
708c2ecf20Sopenharmony_ci	case 0x01: /* Engineering SKU */
718c2ecf20Sopenharmony_ci	case 0x07:
728c2ecf20Sopenharmony_ci	case 0x17:
738c2ecf20Sopenharmony_ci	case 0x27:
748c2ecf20Sopenharmony_ci		if (speedo_rev >= 2)
758c2ecf20Sopenharmony_ci			sku_info->gpu_speedo_id = 1;
768c2ecf20Sopenharmony_ci		break;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	case 0x13:
798c2ecf20Sopenharmony_ci		if (speedo_rev >= 2)
808c2ecf20Sopenharmony_ci			sku_info->gpu_speedo_id = 1;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci		sku_info->cpu_speedo_id = 1;
838c2ecf20Sopenharmony_ci		break;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	default:
868c2ecf20Sopenharmony_ci		pr_err("Tegra210: unknown SKU %#04x\n", sku);
878c2ecf20Sopenharmony_ci		/* Using the default for the error case */
888c2ecf20Sopenharmony_ci		break;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int get_process_id(int value, const u32 *speedos, unsigned int num)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	unsigned int i;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++)
978c2ecf20Sopenharmony_ci		if (value < speedos[i])
988c2ecf20Sopenharmony_ci			return i;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return -EINVAL;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_civoid __init tegra210_init_speedo_data(struct tegra_sku_info *sku_info)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	int cpu_speedo[3], soc_speedo[3], cpu_iddq, gpu_iddq, soc_iddq;
1068c2ecf20Sopenharmony_ci	unsigned int index;
1078c2ecf20Sopenharmony_ci	u8 speedo_revision;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(cpu_process_speedos) !=
1108c2ecf20Sopenharmony_ci			THRESHOLD_INDEX_COUNT);
1118c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(gpu_process_speedos) !=
1128c2ecf20Sopenharmony_ci			THRESHOLD_INDEX_COUNT);
1138c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(soc_process_speedos) !=
1148c2ecf20Sopenharmony_ci			THRESHOLD_INDEX_COUNT);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* Read speedo/IDDQ fuses */
1178c2ecf20Sopenharmony_ci	cpu_speedo[0] = tegra_fuse_read_early(FUSE_CPU_SPEEDO_0);
1188c2ecf20Sopenharmony_ci	cpu_speedo[1] = tegra_fuse_read_early(FUSE_CPU_SPEEDO_1);
1198c2ecf20Sopenharmony_ci	cpu_speedo[2] = tegra_fuse_read_early(FUSE_CPU_SPEEDO_2);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	soc_speedo[0] = tegra_fuse_read_early(FUSE_SOC_SPEEDO_0);
1228c2ecf20Sopenharmony_ci	soc_speedo[1] = tegra_fuse_read_early(FUSE_SOC_SPEEDO_1);
1238c2ecf20Sopenharmony_ci	soc_speedo[2] = tegra_fuse_read_early(FUSE_SOC_SPEEDO_2);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	cpu_iddq = tegra_fuse_read_early(FUSE_CPU_IDDQ) * 4;
1268c2ecf20Sopenharmony_ci	soc_iddq = tegra_fuse_read_early(FUSE_SOC_IDDQ) * 4;
1278c2ecf20Sopenharmony_ci	gpu_iddq = tegra_fuse_read_early(FUSE_GPU_IDDQ) * 5;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/*
1308c2ecf20Sopenharmony_ci	 * Determine CPU, GPU and SoC speedo values depending on speedo fusing
1318c2ecf20Sopenharmony_ci	 * revision. Note that GPU speedo value is fused in CPU_SPEEDO_2.
1328c2ecf20Sopenharmony_ci	 */
1338c2ecf20Sopenharmony_ci	speedo_revision = get_speedo_revision();
1348c2ecf20Sopenharmony_ci	pr_info("Speedo Revision %u\n", speedo_revision);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (speedo_revision >= 3) {
1378c2ecf20Sopenharmony_ci		sku_info->cpu_speedo_value = cpu_speedo[0];
1388c2ecf20Sopenharmony_ci		sku_info->gpu_speedo_value = cpu_speedo[2];
1398c2ecf20Sopenharmony_ci		sku_info->soc_speedo_value = soc_speedo[0];
1408c2ecf20Sopenharmony_ci	} else if (speedo_revision == 2) {
1418c2ecf20Sopenharmony_ci		sku_info->cpu_speedo_value = (-1938 + (1095 * cpu_speedo[0] / 100)) / 10;
1428c2ecf20Sopenharmony_ci		sku_info->gpu_speedo_value = (-1662 + (1082 * cpu_speedo[2] / 100)) / 10;
1438c2ecf20Sopenharmony_ci		sku_info->soc_speedo_value = ( -705 + (1037 * soc_speedo[0] / 100)) / 10;
1448c2ecf20Sopenharmony_ci	} else {
1458c2ecf20Sopenharmony_ci		sku_info->cpu_speedo_value = 2100;
1468c2ecf20Sopenharmony_ci		sku_info->gpu_speedo_value = cpu_speedo[2] - 75;
1478c2ecf20Sopenharmony_ci		sku_info->soc_speedo_value = 1900;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if ((sku_info->cpu_speedo_value <= 0) ||
1518c2ecf20Sopenharmony_ci	    (sku_info->gpu_speedo_value <= 0) ||
1528c2ecf20Sopenharmony_ci	    (sku_info->soc_speedo_value <= 0)) {
1538c2ecf20Sopenharmony_ci		WARN(1, "speedo value not fused\n");
1548c2ecf20Sopenharmony_ci		return;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	rev_sku_to_speedo_ids(sku_info, speedo_revision, &index);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	sku_info->gpu_process_id = get_process_id(sku_info->gpu_speedo_value,
1608c2ecf20Sopenharmony_ci						  gpu_process_speedos[index],
1618c2ecf20Sopenharmony_ci						  GPU_PROCESS_CORNERS);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	sku_info->cpu_process_id = get_process_id(sku_info->cpu_speedo_value,
1648c2ecf20Sopenharmony_ci						  cpu_process_speedos[index],
1658c2ecf20Sopenharmony_ci						  CPU_PROCESS_CORNERS);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	sku_info->soc_process_id = get_process_id(sku_info->soc_speedo_value,
1688c2ecf20Sopenharmony_ci						  soc_process_speedos[index],
1698c2ecf20Sopenharmony_ci						  SOC_PROCESS_CORNERS);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	pr_debug("Tegra GPU Speedo ID=%d, Speedo Value=%d\n",
1728c2ecf20Sopenharmony_ci		 sku_info->gpu_speedo_id, sku_info->gpu_speedo_value);
1738c2ecf20Sopenharmony_ci}
174