blob: aadf8ea3ea213f0bd5091a8a7b11f66b3854e9c5 [file] [log] [blame]
Stefan Reinauer6a5bc462007-01-17 10:57:42 +00001/*
Uwe Hermannafe83092007-09-28 15:45:43 +00002 * This file is part of the superiotool project.
Stefan Reinauer6a5bc462007-01-17 10:57:42 +00003 *
4 * Copyright (C) 2006 Ronald Minnich <rminnich@gmail.com>
Uwe Hermannbd263922007-09-01 19:42:42 +00005 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
Uwe Hermann519419b2007-09-16 20:59:01 +00006 * Copyright (C) 2007 Carl-Daniel Hailfinger
Robinson P. Tryon552cfb72008-01-15 22:30:55 +00007 * Copyright (C) 2008 Robinson P. Tryon <bishop.robinson@gmail.com>
Stefan Reinauer6a5bc462007-01-17 10:57:42 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Stefan Reinauer6a5bc462007-01-17 10:57:42 +000018 */
19
Uwe Hermann0120e1a2007-09-16 18:11:03 +000020#include "superiotool.h"
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000021
Andriy Gaponb64aa602008-10-28 22:13:38 +000022#if defined(__FreeBSD__)
23#include <fcntl.h>
24#include <unistd.h>
25#endif
26
Uwe Hermann3acf31e2007-09-19 01:55:35 +000027/* Command line options. */
Nico Huber3812a722016-10-12 12:12:51 +020028int dump = 0, verbose = 0, extra_dump = 0, alternate_dump = 0;
Uwe Hermann3acf31e2007-09-19 01:55:35 +000029
Uwe Hermanne9d46162007-10-07 20:01:23 +000030/* Global flag which indicates whether a chip was detected at all. */
31int chip_found = 0;
32
Guenter Roecka89da092012-06-29 12:23:50 -070033static void set_bank(uint16_t port, uint8_t bank)
34{
35 OUTB(0x4E, port);
36 OUTB(bank, port + 1);
37}
38
39static uint8_t datareg(uint16_t port, uint8_t reg)
40{
41 OUTB(reg, port);
42 return INB(port + 1);
43}
44
Uwe Hermannde24a0e2007-09-19 00:03:14 +000045uint8_t regval(uint16_t port, uint8_t reg)
Uwe Hermann2046ff92007-09-01 21:02:44 +000046{
Andriy Gaponb64aa602008-10-28 22:13:38 +000047 OUTB(reg, port);
48 return INB(port + ((port == 0x3bd) ? 2 : 1)); /* 0x3bd is special. */
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000049}
50
Uwe Hermannde24a0e2007-09-19 00:03:14 +000051void regwrite(uint16_t port, uint8_t reg, uint8_t val)
Uwe Hermann2046ff92007-09-01 21:02:44 +000052{
Andriy Gaponb64aa602008-10-28 22:13:38 +000053 OUTB(reg, port);
54 OUTB(val, port + 1);
Carl-Daniel Hailfinger7a7890a2007-08-27 07:28:28 +000055}
56
Uwe Hermannb4db2202007-09-20 23:37:56 +000057void enter_conf_mode_winbond_fintek_ite_8787(uint16_t port)
58{
Andriy Gaponb64aa602008-10-28 22:13:38 +000059 OUTB(0x87, port);
60 OUTB(0x87, port);
Uwe Hermannb4db2202007-09-20 23:37:56 +000061}
62
63void exit_conf_mode_winbond_fintek_ite_8787(uint16_t port)
64{
Andriy Gaponb64aa602008-10-28 22:13:38 +000065 OUTB(0xaa, port); /* Fintek, Winbond */
Uwe Hermannb4db2202007-09-20 23:37:56 +000066 regwrite(port, 0x02, 0x02); /* ITE */
67}
68
Stefan Reinauere7b7ae22010-08-17 08:24:01 +000069void enter_conf_mode_fintek_7777(uint16_t port)
70{
71 OUTB(0x77, port);
72 OUTB(0x77, port);
73}
74
75void exit_conf_mode_fintek_7777(uint16_t port)
76{
77 OUTB(0xaa, port); /* Fintek */
78}
79
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +000080int superio_unknown(const struct superio_registers reg_table[], uint16_t id)
81{
82 return !strncmp(get_superio_name(reg_table, id), "<unknown>", 9);
83}
84
Stefan Reinauere7b7ae22010-08-17 08:24:01 +000085
Uwe Hermann3acf31e2007-09-19 01:55:35 +000086const char *get_superio_name(const struct superio_registers reg_table[],
87 uint16_t id)
88{
89 int i;
90
91 for (i = 0; /* Nothing */; i++) {
92 if (reg_table[i].superio_id == EOT)
93 break;
94
95 if ((uint16_t)reg_table[i].superio_id != id)
96 continue;
97
98 return reg_table[i].name;
99 }
100
101 return "<unknown>";
102}
103
Uwe Hermann2c290e32007-09-20 00:00:49 +0000104static void dump_regs(const struct superio_registers reg_table[],
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000105 int i, int j, uint16_t port, uint8_t ldn_sel)
Uwe Hermann2c290e32007-09-20 00:00:49 +0000106{
Ward Vandewege2ee78d82007-09-24 22:02:31 +0000107 int k;
Nico Huber3812a722016-10-12 12:12:51 +0200108 const int16_t *idx, *def;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000109
110 if (reg_table[i].ldn[j].ldn != NOLDN) {
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000111 printf("LDN 0x%02x", reg_table[i].ldn[j].ldn);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000112 if (reg_table[i].ldn[j].name != NULL)
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000113 printf(" (%s)", reg_table[i].ldn[j].name);
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000114 regwrite(port, ldn_sel, reg_table[i].ldn[j].ldn);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000115 } else {
Anders Juel Jensen4fd7df92010-08-22 19:40:11 +0000116 if (reg_table[i].ldn[j].name == NULL)
117 printf("Register dump:");
118 else
119 printf("(%s)", reg_table[i].ldn[j].name);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000120 }
121
122 idx = reg_table[i].ldn[j].idx;
Nico Huber3812a722016-10-12 12:12:51 +0200123 def = reg_table[i].ldn[j].def;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000124
Nico Huber3812a722016-10-12 12:12:51 +0200125 if (alternate_dump) {
126 int skip_def = 0;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000127
Nico Huber3812a722016-10-12 12:12:51 +0200128 printf("\nidx val def\n");
Uwe Hermann2c290e32007-09-20 00:00:49 +0000129
Nico Huber3812a722016-10-12 12:12:51 +0200130 for (k = 0; idx[k] != EOT; k++) {
131 printf("0x%02x: 0x%02x", idx[k], regval(port, idx[k]));
132
133 if (skip_def || def[k] == EOT) {
134 skip_def = 1;
135 printf("\n");
136 continue;
137 }
138 if (def[k] == NANA)
139 printf(" (NA)\n");
140 else if (def[k] == RSVD)
141 printf(" (RR)\n");
142 else if (def[k] == MISC)
143 printf(" (MM)\n");
144 else
145 printf(" (0x%02x)\n", def[k]);
146 }
147 } else {
148 printf("\nidx");
149 for (k = 0; idx[k] != EOT; k++) {
150 if (k && !(k % 8))
151 putchar(' ');
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000152 printf(" %02x", idx[k]);
Nico Huber3812a722016-10-12 12:12:51 +0200153 }
154
155 printf("\nval");
156 for (k = 0; idx[k] != EOT; k++) {
157 if (k && !(k % 8))
158 putchar(' ');
159 printf(" %02x", regval(port, idx[k]));
160 }
161
162 printf("\ndef");
163 for (k = 0; def[k] != EOT; k++) {
164 if (k && !(k % 8))
165 putchar(' ');
166 if (def[k] == NANA)
167 printf(" NA");
168 else if (def[k] == RSVD)
169 printf(" RR");
170 else if (def[k] == MISC)
171 printf(" MM");
172 else
173 printf(" %02x", def[k]);
174 }
Uwe Hermann2c290e32007-09-20 00:00:49 +0000175 }
176 printf("\n");
177}
178
179void dump_superio(const char *vendor,
180 const struct superio_registers reg_table[],
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000181 uint16_t port, uint16_t id, uint8_t ldn_sel)
Uwe Hermann519419b2007-09-16 20:59:01 +0000182{
Uwe Hermann2c290e32007-09-20 00:00:49 +0000183 int i, j, no_dump_available = 1;
Uwe Hermann519419b2007-09-16 20:59:01 +0000184
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000185 if (!dump)
186 return;
187
Uwe Hermann519419b2007-09-16 20:59:01 +0000188 for (i = 0; /* Nothing */; i++) {
189 if (reg_table[i].superio_id == EOT)
190 break;
191
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000192 if ((uint16_t)reg_table[i].superio_id != id)
Uwe Hermann519419b2007-09-16 20:59:01 +0000193 continue;
194
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000195 for (j = 0; /* Nothing */; j++) {
Uwe Hermann519419b2007-09-16 20:59:01 +0000196 if (reg_table[i].ldn[j].ldn == EOT)
197 break;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000198 no_dump_available = 0;
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000199 dump_regs(reg_table, i, j, port, ldn_sel);
Uwe Hermann519419b2007-09-16 20:59:01 +0000200 }
Uwe Hermann25a6c0f2007-09-19 00:48:42 +0000201
Uwe Hermann2c290e32007-09-20 00:00:49 +0000202 if (no_dump_available)
203 printf("No dump available for this Super I/O\n");
Uwe Hermann519419b2007-09-16 20:59:01 +0000204 }
205}
206
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000207void dump_io(uint16_t iobase, uint16_t length)
208{
209 uint16_t i;
210
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000211 printf("Dumping %d I/O mapped registers at base 0x%04x:\n",
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000212 length, iobase);
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000213 for (i = 0; i < length; i++)
214 printf("%02x ", i);
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000215 printf("\n");
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000216 for (i = 0; i < length; i++)
217 printf("%02x ", INB(iobase + i));
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000218 printf("\n");
219}
220
Guenter Roecka89da092012-06-29 12:23:50 -0700221void dump_data(uint16_t iobase, int bank)
222{
223 uint16_t i;
224
225 printf("Bank %d:\n", bank);
226 printf(" ");
227 for (i = 0; i < 16; i++)
228 printf("%02x ", i);
229 set_bank(iobase, bank);
230 for (i = 0; i < 256; i++) {
231 if (i % 16 == 0)
232 printf("\n%02x: ", i / 16);
233 printf("%02x ", datareg(iobase, i));
234 }
235 printf("\n");
236}
237
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000238void probing_for(const char *vendor, const char *info, uint16_t port)
Uwe Hermann2c290e32007-09-20 00:00:49 +0000239{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000240 if (!verbose)
241 return;
242
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000243 /* Yes, there's no space between '%s' and 'at'! */
Uwe Hermanneec5ff42008-03-01 18:49:39 +0000244 printf("Probing for %s Super I/O %sat 0x%x...\n", vendor, info, port);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000245}
246
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000247/** Print a list of all supported chips from the given vendor. */
248void print_vendor_chips(const char *vendor,
249 const struct superio_registers reg_table[])
250{
251 int i;
252
253 for (i = 0; reg_table[i].superio_id != EOT; i++) {
254 printf("%s %s", vendor, reg_table[i].name);
255
256 /* Unless the ldn is empty, assume this chip has a dump. */
257 if (reg_table[i].ldn[0].ldn != EOT)
258 printf(" (dump available)");
259
260 printf("\n");
261 }
262
263 /* If we printed any chips for this vendor, put in a blank line. */
264 if (i != 0)
265 printf("\n");
266}
267
268/** Print a list of all chips supported by superiotool. */
269void print_list_of_supported_chips(void)
270{
271 int i;
272
273 printf("Supported Super I/O chips:\n\n");
274
275 for (i = 0; i < ARRAY_SIZE(vendor_print_functions); i++)
276 vendor_print_functions[i].print_list();
277
278 printf("See <http://coreboot.org/Superiotool#Supported_devices> "
279 "for more information.\n");
280}
281
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000282static void print_version(void)
283{
Ulf Jordan39a5bf72007-10-13 18:06:12 +0000284 printf("superiotool r%s\n", SUPERIOTOOL_VERSION);
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000285}
286
Uwe Hermann2046ff92007-09-01 21:02:44 +0000287int main(int argc, char *argv[])
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000288{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000289 int i, j, opt, option_index;
Andriy Gaponb64aa602008-10-28 22:13:38 +0000290#if defined(__FreeBSD__)
291 int io_fd;
292#endif
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000293
Uwe Hermann246be7d2007-10-31 22:22:11 +0000294 static const struct option long_options[] = {
Uwe Hermanneddc4732007-09-20 23:57:44 +0000295 {"dump", no_argument, NULL, 'd'},
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000296 {"extra-dump", no_argument, NULL, 'e'},
Nico Huber3812a722016-10-12 12:12:51 +0200297 {"alternate-dump", no_argument, NULL, 'a'},
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000298 {"list-supported", no_argument, NULL, 'l'},
Uwe Hermanneddc4732007-09-20 23:57:44 +0000299 {"verbose", no_argument, NULL, 'V'},
300 {"version", no_argument, NULL, 'v'},
301 {"help", no_argument, NULL, 'h'},
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000302 {0, 0, 0, 0}
303 };
304
Nico Huber3812a722016-10-12 12:12:51 +0200305 while ((opt = getopt_long(argc, argv, "dealVvh",
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000306 long_options, &option_index)) != EOF) {
307 switch (opt) {
308 case 'd':
309 dump = 1;
310 break;
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000311 case 'e':
312 extra_dump = 1;
313 break;
Nico Huber3812a722016-10-12 12:12:51 +0200314 case 'a':
315 alternate_dump = 1;
316 break;
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000317 case 'l':
318 print_list_of_supported_chips();
319 exit(0);
320 break;
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000321 case 'V':
322 verbose = 1;
323 break;
324 case 'v':
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000325 print_version();
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000326 exit(0);
327 break;
328 case 'h':
Uwe Hermanne4749562007-09-19 16:26:18 +0000329 printf(USAGE);
Uwe Hermann969a9f62008-03-17 13:43:48 +0000330 printf(USAGE_INFO);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000331 exit(0);
332 break;
333 default:
334 /* Unknown option. */
335 exit(1);
336 break;
337 }
338 }
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000339
Andriy Gaponb64aa602008-10-28 22:13:38 +0000340#if defined(__FreeBSD__)
341 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
342 perror("/dev/io");
343#else
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000344 if (iopl(3) < 0) {
345 perror("iopl");
Andriy Gaponb64aa602008-10-28 22:13:38 +0000346#endif
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000347 printf("Superiotool must be run as root.\n");
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000348 exit(1);
349 }
350
Uwe Hermannd937b522007-10-17 23:42:02 +0000351 print_version();
352
Carl-Daniel Hailfingerbb38f322010-01-24 01:40:46 +0000353#ifdef PCI_SUPPORT
354 /* Do some basic libpci init. */
355 pacc = pci_alloc();
356 pci_init(pacc);
357 pci_scan_bus(pacc);
358#endif
359
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000360 for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
361 for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
362 superio_ports_table[i].probe_idregs(
363 superio_ports_table[i].ports[j]);
364 }
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000365
Uwe Hermanne9d46162007-10-07 20:01:23 +0000366 if (!chip_found)
367 printf("No Super I/O found\n");
368
Andriy Gaponb64aa602008-10-28 22:13:38 +0000369#if defined(__FreeBSD__)
370 close(io_fd);
371#endif
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000372 return 0;
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000373}