blob: f32cd00b18b8a3d6c4b8afea98f929fd514afd05 [file] [log] [blame]
Liming Xuefe754dc2024-11-20 10:57:19 +08001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#include <common.h>
7#include <command.h>
8#include <asm/io.h>
9#include <asm/amlogic/arch/secure_apb.h>
10#include <linux/libfdt_env.h>
11#include <dt-bindings/amlogic/thermal/s6-thermal.h>
12#include "cmd_pdvfs.h"
13
14#define GET_DVFS_TABLE_INDEX 0x82000088
15
16static int get_table_size(const char *path)
17{
18 int ret = 0;
19 char cmdbuf[256] = {0};
20 char *phandle_str = NULL;
21 unsigned int phandle_val = 0;
22
23 memset(cmdbuf, 0, sizeof(cmdbuf));
24 sprintf(cmdbuf, "fdt get size table_size %s", path);
25 ret = run_command(cmdbuf, 0);
26 if (ret != 0) {
27 printf("Error: Failed to get phandle for %s\n", path);
28 return -EBADMSG;
29 }
30
31 phandle_str = env_get("table_size");
32 if (phandle_str == NULL) {
33 printf("Error: Failed to retrieve table_size from environment\n");
34 return -EBADMSG;
35 }
36
37 phandle_val = strtoul(phandle_str, NULL, 10);
38 return phandle_val;
39}
40
41static int set_cooling_state(const char *path, unsigned int cpus_phandle,
42 unsigned int lower, unsigned int upper)
43{
44 int ret = 0;
45 char cmdbuf[256] = {0};
46
47 memset(cmdbuf, 0, sizeof(cmdbuf));
48 sprintf(cmdbuf, "fdt set %s cooling-device <%d %d %d>", path, cpus_phandle, lower, upper);
49 ret = run_command(cmdbuf, 0);
50 if (ret != 0) {
51 printf("Error: Failed to update cooling-device state.\n");
52 return -EBADMSG;
53 }
54
55 return 0;
56}
57
58static int do_update_cooling_state(cmd_tbl_t *cmdtp, int flag1, int argc, char * const argv[])
59{
60 unsigned int cpus_phandle = 0;
61 unsigned int board_id = 0;
62 unsigned int state = 0;
63 unsigned int table_size = 0;
64 unsigned int pdvfs_index = 0;
65
66 cpus_phandle = get_phandle("/cpus/cpu@0");
67 board_id = get_board_id();
68 pdvfs_index = get_cpufreq_table_index(GET_DVFS_TABLE_INDEX, 0, 0, 0);
69
70 if (board_id == 2 && (pdvfs_index == 2 || pdvfs_index == 3)) {
71 table_size = get_table_size(CONFIG_CPU_OPP_TABLE);
72 state = table_size - CONFIG_OPP_TABLE_667M_SIZE;
73 set_cooling_state("/thermal-zones/soc_thermal/cooling-maps/cpufreq_cooling_map",
74 cpus_phandle, 0, state);
75 }
76
77 return 0;
78}
79
80U_BOOT_CMD(
81 update_cooling_state, 1, 0, do_update_cooling_state,
82 "update cpu cooling_state",
83 "update_cooling_state pos"
84);
85