1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 /*
8  * Cache support: switch on or off, get status
9  */
10 #include <command.h>
11 #include <cpu_func.h>
12 #include <linux/compiler.h>
13 #include <linux/string.h>
14 
15 static int parse_argv(const char *);
16 
do_icache(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])17 static int do_icache(struct cmd_tbl *cmdtp, int flag, int argc,
18 		     char *const argv[])
19 {
20 	switch (argc) {
21 	case 2:			/* on / off / flush */
22 		switch (parse_argv(argv[1])) {
23 		case 0:
24 			icache_disable();
25 			break;
26 		case 1:
27 			icache_enable();
28 			break;
29 		case 2:
30 			invalidate_icache_all();
31 			break;
32 		default:
33 			return CMD_RET_USAGE;
34 		}
35 		break;
36 	case 1:			/* get status */
37 		printf("Instruction Cache is %s\n",
38 			icache_status() ? "ON" : "OFF");
39 		return 0;
40 	default:
41 		return CMD_RET_USAGE;
42 	}
43 	return 0;
44 }
45 
do_dcache(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])46 static int do_dcache(struct cmd_tbl *cmdtp, int flag, int argc,
47 		     char *const argv[])
48 {
49 	switch (argc) {
50 	case 2:			/* on / off / flush */
51 		switch (parse_argv(argv[1])) {
52 		case 0:
53 			dcache_disable();
54 			break;
55 		case 1:
56 			dcache_enable();
57 #ifdef CONFIG_SYS_NONCACHED_MEMORY
58 			noncached_set_region();
59 #endif
60 			break;
61 		case 2:
62 			flush_dcache_all();
63 			break;
64 		default:
65 			return CMD_RET_USAGE;
66 		}
67 		break;
68 	case 1:			/* get status */
69 		printf("Data (writethrough) Cache is %s\n",
70 			dcache_status() ? "ON" : "OFF");
71 		return 0;
72 	default:
73 		return CMD_RET_USAGE;
74 	}
75 	return 0;
76 }
77 
parse_argv(const char * s)78 static int parse_argv(const char *s)
79 {
80 	if (strcmp(s, "flush") == 0)
81 		return 2;
82 	else if (strcmp(s, "on") == 0)
83 		return 1;
84 	else if (strcmp(s, "off") == 0)
85 		return 0;
86 
87 	return -1;
88 }
89 
90 U_BOOT_CMD(
91 	icache,   2,   1,     do_icache,
92 	"enable or disable instruction cache",
93 	"[on, off, flush]\n"
94 	"    - enable, disable, or flush instruction cache"
95 );
96 
97 U_BOOT_CMD(
98 	dcache,   2,   1,     do_dcache,
99 	"enable or disable data cache",
100 	"[on, off, flush]\n"
101 	"    - enable, disable, or flush data (writethrough) cache"
102 );
103