blob: 5869cbf0d3f692041691abc361bcd2cf6c1f42b7 [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.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer6a5bc462007-01-17 10:57:42 +000022 */
23
Uwe Hermann0120e1a2007-09-16 18:11:03 +000024#include "superiotool.h"
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000025
Andriy Gaponb64aa602008-10-28 22:13:38 +000026#if defined(__FreeBSD__)
27#include <fcntl.h>
28#include <unistd.h>
29#endif
30
Uwe Hermann3acf31e2007-09-19 01:55:35 +000031/* Command line options. */
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +000032int dump = 0, verbose = 0, extra_dump = 0;
Uwe Hermann3acf31e2007-09-19 01:55:35 +000033
Uwe Hermanne9d46162007-10-07 20:01:23 +000034/* Global flag which indicates whether a chip was detected at all. */
35int chip_found = 0;
36
Guenter Roecka89da092012-06-29 12:23:50 -070037static void set_bank(uint16_t port, uint8_t bank)
38{
39 OUTB(0x4E, port);
40 OUTB(bank, port + 1);
41}
42
43static uint8_t datareg(uint16_t port, uint8_t reg)
44{
45 OUTB(reg, port);
46 return INB(port + 1);
47}
48
Uwe Hermannde24a0e2007-09-19 00:03:14 +000049uint8_t regval(uint16_t port, uint8_t reg)
Uwe Hermann2046ff92007-09-01 21:02:44 +000050{
Andriy Gaponb64aa602008-10-28 22:13:38 +000051 OUTB(reg, port);
52 return INB(port + ((port == 0x3bd) ? 2 : 1)); /* 0x3bd is special. */
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000053}
54
Uwe Hermannde24a0e2007-09-19 00:03:14 +000055void regwrite(uint16_t port, uint8_t reg, uint8_t val)
Uwe Hermann2046ff92007-09-01 21:02:44 +000056{
Andriy Gaponb64aa602008-10-28 22:13:38 +000057 OUTB(reg, port);
58 OUTB(val, port + 1);
Carl-Daniel Hailfinger7a7890a2007-08-27 07:28:28 +000059}
60
Uwe Hermannb4db2202007-09-20 23:37:56 +000061void enter_conf_mode_winbond_fintek_ite_8787(uint16_t port)
62{
Andriy Gaponb64aa602008-10-28 22:13:38 +000063 OUTB(0x87, port);
64 OUTB(0x87, port);
Uwe Hermannb4db2202007-09-20 23:37:56 +000065}
66
67void exit_conf_mode_winbond_fintek_ite_8787(uint16_t port)
68{
Andriy Gaponb64aa602008-10-28 22:13:38 +000069 OUTB(0xaa, port); /* Fintek, Winbond */
Uwe Hermannb4db2202007-09-20 23:37:56 +000070 regwrite(port, 0x02, 0x02); /* ITE */
71}
72
Stefan Reinauere7b7ae22010-08-17 08:24:01 +000073void enter_conf_mode_fintek_7777(uint16_t port)
74{
75 OUTB(0x77, port);
76 OUTB(0x77, port);
77}
78
79void exit_conf_mode_fintek_7777(uint16_t port)
80{
81 OUTB(0xaa, port); /* Fintek */
82}
83
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +000084int superio_unknown(const struct superio_registers reg_table[], uint16_t id)
85{
86 return !strncmp(get_superio_name(reg_table, id), "<unknown>", 9);
87}
88
Stefan Reinauere7b7ae22010-08-17 08:24:01 +000089
Uwe Hermann3acf31e2007-09-19 01:55:35 +000090const char *get_superio_name(const struct superio_registers reg_table[],
91 uint16_t id)
92{
93 int i;
94
95 for (i = 0; /* Nothing */; i++) {
96 if (reg_table[i].superio_id == EOT)
97 break;
98
99 if ((uint16_t)reg_table[i].superio_id != id)
100 continue;
101
102 return reg_table[i].name;
103 }
104
105 return "<unknown>";
106}
107
Uwe Hermann2c290e32007-09-20 00:00:49 +0000108static void dump_regs(const struct superio_registers reg_table[],
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000109 int i, int j, uint16_t port, uint8_t ldn_sel)
Uwe Hermann2c290e32007-09-20 00:00:49 +0000110{
Ward Vandewege2ee78d82007-09-24 22:02:31 +0000111 int k;
Uwe Hermann42cccdf2008-03-29 01:35:21 +0000112 const int16_t *idx;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000113
114 if (reg_table[i].ldn[j].ldn != NOLDN) {
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000115 printf("LDN 0x%02x", reg_table[i].ldn[j].ldn);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000116 if (reg_table[i].ldn[j].name != NULL)
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000117 printf(" (%s)", reg_table[i].ldn[j].name);
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000118 regwrite(port, ldn_sel, reg_table[i].ldn[j].ldn);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000119 } else {
Anders Juel Jensen4fd7df92010-08-22 19:40:11 +0000120 if (reg_table[i].ldn[j].name == NULL)
121 printf("Register dump:");
122 else
123 printf("(%s)", reg_table[i].ldn[j].name);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000124 }
125
126 idx = reg_table[i].ldn[j].idx;
127
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000128 printf("\nidx");
129 for (k = 0; idx[k] != EOT; k++) {
130 if (k && !(k % 8))
131 putchar(' ');
132 printf(" %02x", idx[k]);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000133 }
134
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000135 printf("\nval");
136 for (k = 0; idx[k] != EOT; k++) {
137 if (k && !(k % 8))
138 putchar(' ');
139 printf(" %02x", regval(port, idx[k]));
Uwe Hermann2c290e32007-09-20 00:00:49 +0000140 }
141
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000142 printf("\ndef");
Uwe Hermann2c290e32007-09-20 00:00:49 +0000143 idx = reg_table[i].ldn[j].def;
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000144 for (k = 0; idx[k] != EOT; k++) {
145 if (k && !(k % 8))
146 putchar(' ');
147 if (idx[k] == NANA)
148 printf(" NA");
Uwe Hermann2c290e32007-09-20 00:00:49 +0000149 else if (idx[k] == RSVD)
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000150 printf(" RR");
Uwe Hermann59b99d92007-11-29 02:43:50 +0000151 else if (idx[k] == MISC)
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000152 printf(" MM");
Uwe Hermann2c290e32007-09-20 00:00:49 +0000153 else
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000154 printf(" %02x", idx[k]);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000155 }
156 printf("\n");
157}
158
159void dump_superio(const char *vendor,
160 const struct superio_registers reg_table[],
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000161 uint16_t port, uint16_t id, uint8_t ldn_sel)
Uwe Hermann519419b2007-09-16 20:59:01 +0000162{
Uwe Hermann2c290e32007-09-20 00:00:49 +0000163 int i, j, no_dump_available = 1;
Uwe Hermann519419b2007-09-16 20:59:01 +0000164
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000165 if (!dump)
166 return;
167
Uwe Hermann519419b2007-09-16 20:59:01 +0000168 for (i = 0; /* Nothing */; i++) {
169 if (reg_table[i].superio_id == EOT)
170 break;
171
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000172 if ((uint16_t)reg_table[i].superio_id != id)
Uwe Hermann519419b2007-09-16 20:59:01 +0000173 continue;
174
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000175 for (j = 0; /* Nothing */; j++) {
Uwe Hermann519419b2007-09-16 20:59:01 +0000176 if (reg_table[i].ldn[j].ldn == EOT)
177 break;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000178 no_dump_available = 0;
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000179 dump_regs(reg_table, i, j, port, ldn_sel);
Uwe Hermann519419b2007-09-16 20:59:01 +0000180 }
Uwe Hermann25a6c0f2007-09-19 00:48:42 +0000181
Uwe Hermann2c290e32007-09-20 00:00:49 +0000182 if (no_dump_available)
183 printf("No dump available for this Super I/O\n");
Uwe Hermann519419b2007-09-16 20:59:01 +0000184 }
185}
186
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000187void dump_io(uint16_t iobase, uint16_t length)
188{
189 uint16_t i;
190
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000191 printf("Dumping %d I/O mapped registers at base 0x%04x:\n",
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000192 length, iobase);
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000193 for (i = 0; i < length; i++)
194 printf("%02x ", i);
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000195 printf("\n");
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000196 for (i = 0; i < length; i++)
197 printf("%02x ", INB(iobase + i));
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000198 printf("\n");
199}
200
Guenter Roecka89da092012-06-29 12:23:50 -0700201void dump_data(uint16_t iobase, int bank)
202{
203 uint16_t i;
204
205 printf("Bank %d:\n", bank);
206 printf(" ");
207 for (i = 0; i < 16; i++)
208 printf("%02x ", i);
209 set_bank(iobase, bank);
210 for (i = 0; i < 256; i++) {
211 if (i % 16 == 0)
212 printf("\n%02x: ", i / 16);
213 printf("%02x ", datareg(iobase, i));
214 }
215 printf("\n");
216}
217
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000218void probing_for(const char *vendor, const char *info, uint16_t port)
Uwe Hermann2c290e32007-09-20 00:00:49 +0000219{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000220 if (!verbose)
221 return;
222
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000223 /* Yes, there's no space between '%s' and 'at'! */
Uwe Hermanneec5ff42008-03-01 18:49:39 +0000224 printf("Probing for %s Super I/O %sat 0x%x...\n", vendor, info, port);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000225}
226
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000227/** Print a list of all supported chips from the given vendor. */
228void print_vendor_chips(const char *vendor,
229 const struct superio_registers reg_table[])
230{
231 int i;
232
233 for (i = 0; reg_table[i].superio_id != EOT; i++) {
234 printf("%s %s", vendor, reg_table[i].name);
235
236 /* Unless the ldn is empty, assume this chip has a dump. */
237 if (reg_table[i].ldn[0].ldn != EOT)
238 printf(" (dump available)");
239
240 printf("\n");
241 }
242
243 /* If we printed any chips for this vendor, put in a blank line. */
244 if (i != 0)
245 printf("\n");
246}
247
248/** Print a list of all chips supported by superiotool. */
249void print_list_of_supported_chips(void)
250{
251 int i;
252
253 printf("Supported Super I/O chips:\n\n");
254
255 for (i = 0; i < ARRAY_SIZE(vendor_print_functions); i++)
256 vendor_print_functions[i].print_list();
257
258 printf("See <http://coreboot.org/Superiotool#Supported_devices> "
259 "for more information.\n");
260}
261
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000262static void print_version(void)
263{
Ulf Jordan39a5bf72007-10-13 18:06:12 +0000264 printf("superiotool r%s\n", SUPERIOTOOL_VERSION);
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000265}
266
Uwe Hermann2046ff92007-09-01 21:02:44 +0000267int main(int argc, char *argv[])
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000268{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000269 int i, j, opt, option_index;
Andriy Gaponb64aa602008-10-28 22:13:38 +0000270#if defined(__FreeBSD__)
271 int io_fd;
272#endif
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000273
Uwe Hermann246be7d2007-10-31 22:22:11 +0000274 static const struct option long_options[] = {
Uwe Hermanneddc4732007-09-20 23:57:44 +0000275 {"dump", no_argument, NULL, 'd'},
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000276 {"extra-dump", no_argument, NULL, 'e'},
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000277 {"list-supported", no_argument, NULL, 'l'},
Uwe Hermanneddc4732007-09-20 23:57:44 +0000278 {"verbose", no_argument, NULL, 'V'},
279 {"version", no_argument, NULL, 'v'},
280 {"help", no_argument, NULL, 'h'},
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000281 {0, 0, 0, 0}
282 };
283
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000284 while ((opt = getopt_long(argc, argv, "delVvh",
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000285 long_options, &option_index)) != EOF) {
286 switch (opt) {
287 case 'd':
288 dump = 1;
289 break;
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000290 case 'e':
291 extra_dump = 1;
292 break;
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000293 case 'l':
294 print_list_of_supported_chips();
295 exit(0);
296 break;
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000297 case 'V':
298 verbose = 1;
299 break;
300 case 'v':
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000301 print_version();
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000302 exit(0);
303 break;
304 case 'h':
Uwe Hermanne4749562007-09-19 16:26:18 +0000305 printf(USAGE);
Uwe Hermann969a9f62008-03-17 13:43:48 +0000306 printf(USAGE_INFO);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000307 exit(0);
308 break;
309 default:
310 /* Unknown option. */
311 exit(1);
312 break;
313 }
314 }
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000315
Andriy Gaponb64aa602008-10-28 22:13:38 +0000316#if defined(__FreeBSD__)
317 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
318 perror("/dev/io");
319#else
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000320 if (iopl(3) < 0) {
321 perror("iopl");
Andriy Gaponb64aa602008-10-28 22:13:38 +0000322#endif
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000323 printf("Superiotool must be run as root.\n");
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000324 exit(1);
325 }
326
Uwe Hermannd937b522007-10-17 23:42:02 +0000327 print_version();
328
Carl-Daniel Hailfingerbb38f322010-01-24 01:40:46 +0000329#ifdef PCI_SUPPORT
330 /* Do some basic libpci init. */
331 pacc = pci_alloc();
332 pci_init(pacc);
333 pci_scan_bus(pacc);
334#endif
335
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000336 for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
337 for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
338 superio_ports_table[i].probe_idregs(
339 superio_ports_table[i].ports[j]);
340 }
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000341
Uwe Hermanne9d46162007-10-07 20:01:23 +0000342 if (!chip_found)
343 printf("No Super I/O found\n");
344
Andriy Gaponb64aa602008-10-28 22:13:38 +0000345#if defined(__FreeBSD__)
346 close(io_fd);
347#endif
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000348 return 0;
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000349}