1 /* Copyright 2014-2016 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * imitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <getopt.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <inttypes.h>
23 
24 #include "xscom.h"
25 
print_usage(int code)26 static void print_usage(int code)
27 {
28 	printf("usage: putscom [-c|--chip chip-id] [-b|--list-bits] addr value\n");
29 	printf("       putscom -v|--version\n");
30 	printf("\n");
31 	printf("       NB: --list-bits shows which PPC bits are set\n");
32 	exit(code);
33 	exit(code);
34 }
35 
36 extern const char version[];
37 
main(int argc,char * argv[])38 int main(int argc, char *argv[])
39 {
40 	uint64_t val = -1ull, addr = -1ull;
41 	uint32_t def_chip, chip_id = 0xffffffff;
42 	bool got_addr = false, got_val = false;
43 	bool list_bits = false;
44 	int rc;
45 
46 	while(1) {
47 		static struct option long_opts[] = {
48 			{"chip",	required_argument,	NULL,	'c'},
49 			{"help",	no_argument,		NULL,	'h'},
50 			{"version",	no_argument,		NULL,	'v'},
51 		};
52 		int c, oidx = 0;
53 
54 		c = getopt_long(argc, argv, "-c:bhv", long_opts, &oidx);
55 		if (c == EOF)
56 			break;
57 		switch(c) {
58 		case 1:
59 			if (!got_addr) {
60 				addr = strtoull(optarg, NULL, 16);
61 				got_addr = true;
62 				break;
63 			}
64 			val = strtoull(optarg, NULL, 16);
65 			got_val = true;
66 			break;
67 		case 'c':
68 			chip_id = strtoul(optarg, NULL, 16);
69 			break;
70 		case 'b':
71 			list_bits = true;
72 			break;
73 		case 'v':
74 			printf("xscom utils version %s\n", version);
75 			exit(0);
76 		case 'h':
77 			print_usage(0);
78 			break;
79 		default:
80 			exit(1);
81 		}
82 	}
83 
84 	if (!got_addr || !got_val) {
85 		fprintf(stderr, "Invalid or missing address/value\n");
86 		print_usage(1);
87 	}
88 
89 	def_chip = xscom_init();
90 	if (def_chip == 0xffffffff) {
91 		fprintf(stderr, "No valid XSCOM chip found\n");
92 		exit(1);
93 	}
94 	if (chip_id == 0xffffffff)
95 		chip_id = def_chip;
96 
97 	rc = xscom_write(chip_id, addr, val);
98 	if (rc) {
99 		fprintf(stderr,"Error %d writing XSCOM\n", rc);
100 		exit(1);
101 	}
102 	if (xscom_readable(addr)) {
103 		rc = xscom_read(chip_id, addr, &val);
104 		if (rc) {
105 			fprintf(stderr,"Error %d reading XSCOM\n", rc);
106 			exit(1);
107 		}
108 	}
109 
110 	printf("%016" PRIx64, val);
111 	if (list_bits) {
112 		int i;
113 
114 		printf(" - set: ");
115 
116 		for (i = 0; i < 64; i++)
117 			if (val & PPC_BIT(i))
118 				printf("%d ", i);
119 	}
120 
121 	putchar('\n');
122 	return 0;
123 }
124 
125