mb/google/poppy/variants/nocturne: Tune DPTF settings for CPU
[coreboot.git] / util / ectool / ectool.c
blobbc3d46cd529fc42377118856d7baa1ce8f5350df
1 /*
2 * This file is part of the ectool project.
4 * Copyright (C) 2008-2009 coresystems GmbH
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <getopt.h>
21 #if !(defined __NetBSD__ || defined __OpenBSD__)
22 #include <sys/io.h>
23 #endif
24 #include <ec.h>
25 #include <stdlib.h>
27 #if defined __NetBSD__ || defined __OpenBSD__
29 #include <machine/sysarch.h>
31 # if defined __i386__
32 # define iopl i386_iopl
33 # elif defined __NetBSD__
34 # define iopl x86_64_iopl
35 # else
36 # define iopl amd64_iopl
37 # endif
39 #endif
42 #define ECTOOL_VERSION "0.1"
44 void print_version(void)
46 printf("ectool v%s -- ", ECTOOL_VERSION);
47 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
48 printf(
49 "This program is free software: you can redistribute it and/or modify\n"
50 "it under the terms of the GNU General Public License as published by\n"
51 "the Free Software Foundation, version 2 of the License.\n\n"
52 "This program is distributed in the hope that it will be useful,\n"
53 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
54 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
55 "GNU General Public License for more details.\n\n");
58 void print_usage(const char *name)
60 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
61 printf("\n"
62 " -v | --version: print the version\n"
63 " -h | --help: print this help\n\n"
64 " -V | --verbose: print debug information\n"
65 " -p | --getports: get EC data & cmd ports from /proc/ioports\n"
66 " -d | --dump: print RAM\n"
67 " -i | --idx: print IDX RAM & RAM\n"
68 " -q | --query: print query byte\n"
69 " -w <addr in hex> write to addr\n"
70 " -z <data in hex> write to data\n"
71 "\n");
72 exit(1);
75 int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 0;
77 int main(int argc, char *argv[])
79 int i, opt, option_index = 0;
80 long write_data = -1;
81 long write_addr = -1;
83 static struct option long_options[] = {
84 {"version", 0, 0, 'v'},
85 {"help", 0, 0, 'h'},
86 {"verbose", 0, 0, 'V'},
87 {"idx", 0, 0, 'i'},
88 {"query", 0, 0, 'q'},
89 {"getports", 0, 0, 'p'},
90 {0, 0, 0, 0}
93 if (argv[1] == NULL) {
94 print_usage(argv[0]);
95 exit(1);
98 while ((opt = getopt_long(argc, argv, "vh?Vidqpw:z:",
99 long_options, &option_index)) != EOF) {
100 switch (opt) {
101 case 'v':
102 print_version();
103 exit(0);
104 break;
105 case 'V':
106 verbose = 1;
107 break;
108 case 'i':
109 dump_idx = 1;
110 dump_ram = 1;
111 break;
112 case 'w':
113 write_addr = strtol(optarg , NULL, 16);
114 break;
115 case 'z':
116 write_data = strtol(optarg , NULL, 16);
117 break;
118 case 'd':
119 dump_ram = 1;
120 break;
121 case 'q':
122 dump_query = 1;
123 break;
124 case 'p':
125 get_ports = 1;
126 break;
127 case 'h':
128 case '?':
129 default:
130 print_usage(argv[0]);
131 exit(0);
132 break;
136 if (optind < argc) {
137 fprintf(stderr, "Error: Extra parameter found.\n");
138 print_usage(argv[0]);
139 exit(1);
142 if (get_ports && get_ec_ports() != 0)
143 fprintf(stderr, "Cannot get EC ports from /proc/ioports, "
144 "fallback to default.");
146 if (iopl(3)) {
147 printf("You need to be root.\n");
148 exit(1);
150 if (write_addr >= 0 && write_data >= 0) {
151 write_addr &= 0xff;
152 write_data &= 0xff;
153 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
154 ec_write(write_addr & 0xff, write_data & 0xff);
157 /* preserve default - dump_ram if nothing selected */
158 if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1))
159 dump_ram = 1;
161 if (dump_ram) {
162 printf("EC RAM:\n");
163 for (i = 0; i < 0x100; i++) {
164 if ((i % 0x10) == 0)
165 printf("\n%02x: ", i);
166 printf("%02x ", ec_read(i));
168 printf("\n\n");
171 if (dump_query) {
172 printf("EC QUERY %02x\n", ec_query());
175 if (dump_idx) {
176 printf("EC IDX RAM:\n");
177 for (i = 0; i < 0x10000; i++) {
178 if ((i % 0x10) == 0)
179 printf("\n%04x: ", i);
180 printf("%02x ", ec_idx_read(i));
182 printf("\n\n");
185 return 0;