18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2012-2014, NVIDIA CORPORATION.  All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/bug.h>
78c2ecf20Sopenharmony_ci#include <linux/device.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <soc/tegra/fuse.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "fuse.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define SOC_PROCESS_CORNERS	1
158c2ecf20Sopenharmony_ci#define CPU_PROCESS_CORNERS	6
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define FUSE_SPEEDO_CALIB_0	0x14
188c2ecf20Sopenharmony_ci#define FUSE_PACKAGE_INFO	0XFC
198c2ecf20Sopenharmony_ci#define FUSE_TEST_PROG_VER	0X28
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define G_SPEEDO_BIT_MINUS1	58
228c2ecf20Sopenharmony_ci#define G_SPEEDO_BIT_MINUS1_R	59
238c2ecf20Sopenharmony_ci#define G_SPEEDO_BIT_MINUS2	60
248c2ecf20Sopenharmony_ci#define G_SPEEDO_BIT_MINUS2_R	61
258c2ecf20Sopenharmony_ci#define LP_SPEEDO_BIT_MINUS1	62
268c2ecf20Sopenharmony_ci#define LP_SPEEDO_BIT_MINUS1_R	63
278c2ecf20Sopenharmony_ci#define LP_SPEEDO_BIT_MINUS2	64
288c2ecf20Sopenharmony_ci#define LP_SPEEDO_BIT_MINUS2_R	65
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cienum {
318c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_0,
328c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_1,
338c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_2,
348c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_3,
358c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_4,
368c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_5,
378c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_6,
388c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_7,
398c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_8,
408c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_9,
418c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_10,
428c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_11,
438c2ecf20Sopenharmony_ci	THRESHOLD_INDEX_COUNT,
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const u32 __initconst soc_process_speedos[][SOC_PROCESS_CORNERS] = {
478c2ecf20Sopenharmony_ci	{180},
488c2ecf20Sopenharmony_ci	{170},
498c2ecf20Sopenharmony_ci	{195},
508c2ecf20Sopenharmony_ci	{180},
518c2ecf20Sopenharmony_ci	{168},
528c2ecf20Sopenharmony_ci	{192},
538c2ecf20Sopenharmony_ci	{180},
548c2ecf20Sopenharmony_ci	{170},
558c2ecf20Sopenharmony_ci	{195},
568c2ecf20Sopenharmony_ci	{180},
578c2ecf20Sopenharmony_ci	{180},
588c2ecf20Sopenharmony_ci	{180},
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const u32 __initconst cpu_process_speedos[][CPU_PROCESS_CORNERS] = {
628c2ecf20Sopenharmony_ci	{306, 338, 360, 376, UINT_MAX},
638c2ecf20Sopenharmony_ci	{295, 336, 358, 375, UINT_MAX},
648c2ecf20Sopenharmony_ci	{325, 325, 358, 375, UINT_MAX},
658c2ecf20Sopenharmony_ci	{325, 325, 358, 375, UINT_MAX},
668c2ecf20Sopenharmony_ci	{292, 324, 348, 364, UINT_MAX},
678c2ecf20Sopenharmony_ci	{324, 324, 348, 364, UINT_MAX},
688c2ecf20Sopenharmony_ci	{324, 324, 348, 364, UINT_MAX},
698c2ecf20Sopenharmony_ci	{295, 336, 358, 375, UINT_MAX},
708c2ecf20Sopenharmony_ci	{358, 358, 358, 358, 397, UINT_MAX},
718c2ecf20Sopenharmony_ci	{364, 364, 364, 364, 397, UINT_MAX},
728c2ecf20Sopenharmony_ci	{295, 336, 358, 375, 391, UINT_MAX},
738c2ecf20Sopenharmony_ci	{295, 336, 358, 375, 391, UINT_MAX},
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int threshold_index __initdata;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic void __init fuse_speedo_calib(u32 *speedo_g, u32 *speedo_lp)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	u32 reg;
818c2ecf20Sopenharmony_ci	int ate_ver;
828c2ecf20Sopenharmony_ci	int bit_minus1;
838c2ecf20Sopenharmony_ci	int bit_minus2;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	reg = tegra_fuse_read_early(FUSE_SPEEDO_CALIB_0);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	*speedo_lp = (reg & 0xFFFF) * 4;
888c2ecf20Sopenharmony_ci	*speedo_g = ((reg >> 16) & 0xFFFF) * 4;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	ate_ver = tegra_fuse_read_early(FUSE_TEST_PROG_VER);
918c2ecf20Sopenharmony_ci	pr_debug("Tegra ATE prog ver %d.%d\n", ate_ver/10, ate_ver%10);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (ate_ver >= 26) {
948c2ecf20Sopenharmony_ci		bit_minus1 = tegra_fuse_read_spare(LP_SPEEDO_BIT_MINUS1);
958c2ecf20Sopenharmony_ci		bit_minus1 |= tegra_fuse_read_spare(LP_SPEEDO_BIT_MINUS1_R);
968c2ecf20Sopenharmony_ci		bit_minus2 = tegra_fuse_read_spare(LP_SPEEDO_BIT_MINUS2);
978c2ecf20Sopenharmony_ci		bit_minus2 |= tegra_fuse_read_spare(LP_SPEEDO_BIT_MINUS2_R);
988c2ecf20Sopenharmony_ci		*speedo_lp |= (bit_minus1 << 1) | bit_minus2;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		bit_minus1 = tegra_fuse_read_spare(G_SPEEDO_BIT_MINUS1);
1018c2ecf20Sopenharmony_ci		bit_minus1 |= tegra_fuse_read_spare(G_SPEEDO_BIT_MINUS1_R);
1028c2ecf20Sopenharmony_ci		bit_minus2 = tegra_fuse_read_spare(G_SPEEDO_BIT_MINUS2);
1038c2ecf20Sopenharmony_ci		bit_minus2 |= tegra_fuse_read_spare(G_SPEEDO_BIT_MINUS2_R);
1048c2ecf20Sopenharmony_ci		*speedo_g |= (bit_minus1 << 1) | bit_minus2;
1058c2ecf20Sopenharmony_ci	} else {
1068c2ecf20Sopenharmony_ci		*speedo_lp |= 0x3;
1078c2ecf20Sopenharmony_ci		*speedo_g |= 0x3;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic void __init rev_sku_to_speedo_ids(struct tegra_sku_info *sku_info)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	int package_id = tegra_fuse_read_early(FUSE_PACKAGE_INFO) & 0x0F;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	switch (sku_info->revision) {
1168c2ecf20Sopenharmony_ci	case TEGRA_REVISION_A01:
1178c2ecf20Sopenharmony_ci		sku_info->cpu_speedo_id = 0;
1188c2ecf20Sopenharmony_ci		sku_info->soc_speedo_id = 0;
1198c2ecf20Sopenharmony_ci		threshold_index = THRESHOLD_INDEX_0;
1208c2ecf20Sopenharmony_ci		break;
1218c2ecf20Sopenharmony_ci	case TEGRA_REVISION_A02:
1228c2ecf20Sopenharmony_ci	case TEGRA_REVISION_A03:
1238c2ecf20Sopenharmony_ci		switch (sku_info->sku_id) {
1248c2ecf20Sopenharmony_ci		case 0x87:
1258c2ecf20Sopenharmony_ci		case 0x82:
1268c2ecf20Sopenharmony_ci			sku_info->cpu_speedo_id = 1;
1278c2ecf20Sopenharmony_ci			sku_info->soc_speedo_id = 1;
1288c2ecf20Sopenharmony_ci			threshold_index = THRESHOLD_INDEX_1;
1298c2ecf20Sopenharmony_ci			break;
1308c2ecf20Sopenharmony_ci		case 0x81:
1318c2ecf20Sopenharmony_ci			switch (package_id) {
1328c2ecf20Sopenharmony_ci			case 1:
1338c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 2;
1348c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 2;
1358c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_2;
1368c2ecf20Sopenharmony_ci				break;
1378c2ecf20Sopenharmony_ci			case 2:
1388c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 4;
1398c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 1;
1408c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_7;
1418c2ecf20Sopenharmony_ci				break;
1428c2ecf20Sopenharmony_ci			default:
1438c2ecf20Sopenharmony_ci				pr_err("Tegra Unknown pkg %d\n", package_id);
1448c2ecf20Sopenharmony_ci				break;
1458c2ecf20Sopenharmony_ci			}
1468c2ecf20Sopenharmony_ci			break;
1478c2ecf20Sopenharmony_ci		case 0x80:
1488c2ecf20Sopenharmony_ci			switch (package_id) {
1498c2ecf20Sopenharmony_ci			case 1:
1508c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 5;
1518c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 2;
1528c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_8;
1538c2ecf20Sopenharmony_ci				break;
1548c2ecf20Sopenharmony_ci			case 2:
1558c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 6;
1568c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 2;
1578c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_9;
1588c2ecf20Sopenharmony_ci				break;
1598c2ecf20Sopenharmony_ci			default:
1608c2ecf20Sopenharmony_ci				pr_err("Tegra Unknown pkg %d\n", package_id);
1618c2ecf20Sopenharmony_ci				break;
1628c2ecf20Sopenharmony_ci			}
1638c2ecf20Sopenharmony_ci			break;
1648c2ecf20Sopenharmony_ci		case 0x83:
1658c2ecf20Sopenharmony_ci			switch (package_id) {
1668c2ecf20Sopenharmony_ci			case 1:
1678c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 7;
1688c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 1;
1698c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_10;
1708c2ecf20Sopenharmony_ci				break;
1718c2ecf20Sopenharmony_ci			case 2:
1728c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 3;
1738c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 2;
1748c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_3;
1758c2ecf20Sopenharmony_ci				break;
1768c2ecf20Sopenharmony_ci			default:
1778c2ecf20Sopenharmony_ci				pr_err("Tegra Unknown pkg %d\n", package_id);
1788c2ecf20Sopenharmony_ci				break;
1798c2ecf20Sopenharmony_ci			}
1808c2ecf20Sopenharmony_ci			break;
1818c2ecf20Sopenharmony_ci		case 0x8F:
1828c2ecf20Sopenharmony_ci			sku_info->cpu_speedo_id = 8;
1838c2ecf20Sopenharmony_ci			sku_info->soc_speedo_id = 1;
1848c2ecf20Sopenharmony_ci			threshold_index = THRESHOLD_INDEX_11;
1858c2ecf20Sopenharmony_ci			break;
1868c2ecf20Sopenharmony_ci		case 0x08:
1878c2ecf20Sopenharmony_ci			sku_info->cpu_speedo_id = 1;
1888c2ecf20Sopenharmony_ci			sku_info->soc_speedo_id = 1;
1898c2ecf20Sopenharmony_ci			threshold_index = THRESHOLD_INDEX_4;
1908c2ecf20Sopenharmony_ci			break;
1918c2ecf20Sopenharmony_ci		case 0x02:
1928c2ecf20Sopenharmony_ci			sku_info->cpu_speedo_id = 2;
1938c2ecf20Sopenharmony_ci			sku_info->soc_speedo_id = 2;
1948c2ecf20Sopenharmony_ci			threshold_index = THRESHOLD_INDEX_5;
1958c2ecf20Sopenharmony_ci			break;
1968c2ecf20Sopenharmony_ci		case 0x04:
1978c2ecf20Sopenharmony_ci			sku_info->cpu_speedo_id = 3;
1988c2ecf20Sopenharmony_ci			sku_info->soc_speedo_id = 2;
1998c2ecf20Sopenharmony_ci			threshold_index = THRESHOLD_INDEX_6;
2008c2ecf20Sopenharmony_ci			break;
2018c2ecf20Sopenharmony_ci		case 0:
2028c2ecf20Sopenharmony_ci			switch (package_id) {
2038c2ecf20Sopenharmony_ci			case 1:
2048c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 2;
2058c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 2;
2068c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_2;
2078c2ecf20Sopenharmony_ci				break;
2088c2ecf20Sopenharmony_ci			case 2:
2098c2ecf20Sopenharmony_ci				sku_info->cpu_speedo_id = 3;
2108c2ecf20Sopenharmony_ci				sku_info->soc_speedo_id = 2;
2118c2ecf20Sopenharmony_ci				threshold_index = THRESHOLD_INDEX_3;
2128c2ecf20Sopenharmony_ci				break;
2138c2ecf20Sopenharmony_ci			default:
2148c2ecf20Sopenharmony_ci				pr_err("Tegra Unknown pkg %d\n", package_id);
2158c2ecf20Sopenharmony_ci				break;
2168c2ecf20Sopenharmony_ci			}
2178c2ecf20Sopenharmony_ci			break;
2188c2ecf20Sopenharmony_ci		default:
2198c2ecf20Sopenharmony_ci			pr_warn("Tegra Unknown SKU %d\n", sku_info->sku_id);
2208c2ecf20Sopenharmony_ci			sku_info->cpu_speedo_id = 0;
2218c2ecf20Sopenharmony_ci			sku_info->soc_speedo_id = 0;
2228c2ecf20Sopenharmony_ci			threshold_index = THRESHOLD_INDEX_0;
2238c2ecf20Sopenharmony_ci			break;
2248c2ecf20Sopenharmony_ci		}
2258c2ecf20Sopenharmony_ci		break;
2268c2ecf20Sopenharmony_ci	default:
2278c2ecf20Sopenharmony_ci		pr_warn("Tegra Unknown chip rev %d\n", sku_info->revision);
2288c2ecf20Sopenharmony_ci		sku_info->cpu_speedo_id = 0;
2298c2ecf20Sopenharmony_ci		sku_info->soc_speedo_id = 0;
2308c2ecf20Sopenharmony_ci		threshold_index = THRESHOLD_INDEX_0;
2318c2ecf20Sopenharmony_ci		break;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_civoid __init tegra30_init_speedo_data(struct tegra_sku_info *sku_info)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	u32 cpu_speedo_val;
2388c2ecf20Sopenharmony_ci	u32 soc_speedo_val;
2398c2ecf20Sopenharmony_ci	int i;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(cpu_process_speedos) !=
2428c2ecf20Sopenharmony_ci			THRESHOLD_INDEX_COUNT);
2438c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(soc_process_speedos) !=
2448c2ecf20Sopenharmony_ci			THRESHOLD_INDEX_COUNT);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	rev_sku_to_speedo_ids(sku_info);
2488c2ecf20Sopenharmony_ci	fuse_speedo_calib(&cpu_speedo_val, &soc_speedo_val);
2498c2ecf20Sopenharmony_ci	pr_debug("Tegra CPU speedo value %u\n", cpu_speedo_val);
2508c2ecf20Sopenharmony_ci	pr_debug("Tegra Core speedo value %u\n", soc_speedo_val);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	for (i = 0; i < CPU_PROCESS_CORNERS; i++) {
2538c2ecf20Sopenharmony_ci		if (cpu_speedo_val < cpu_process_speedos[threshold_index][i])
2548c2ecf20Sopenharmony_ci			break;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci	sku_info->cpu_process_id = i - 1;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	if (sku_info->cpu_process_id == -1) {
2598c2ecf20Sopenharmony_ci		pr_warn("Tegra CPU speedo value %3d out of range",
2608c2ecf20Sopenharmony_ci			 cpu_speedo_val);
2618c2ecf20Sopenharmony_ci		sku_info->cpu_process_id = 0;
2628c2ecf20Sopenharmony_ci		sku_info->cpu_speedo_id = 1;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	for (i = 0; i < SOC_PROCESS_CORNERS; i++) {
2668c2ecf20Sopenharmony_ci		if (soc_speedo_val < soc_process_speedos[threshold_index][i])
2678c2ecf20Sopenharmony_ci			break;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci	sku_info->soc_process_id = i - 1;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	if (sku_info->soc_process_id == -1) {
2728c2ecf20Sopenharmony_ci		pr_warn("Tegra SoC speedo value %3d out of range",
2738c2ecf20Sopenharmony_ci			soc_speedo_val);
2748c2ecf20Sopenharmony_ci		sku_info->soc_process_id = 0;
2758c2ecf20Sopenharmony_ci		sku_info->soc_speedo_id = 1;
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci}
278