Rename probe_superio.c to superiotool.c.
[coreboot.git] / util / superiotool / superiotool.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2006 Ronald Minnich <rminnich@gmail.com>
5  * Copyright (C) 2006 coresystems GmbH <info@coresystems.de>
6  * Copyright (C) 2007 Carl-Daniel Hailfinger
7  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
8  *
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
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/io.h>
27
28 /* well, they really thought this through, eh? Family is 8 bits!!!! */
29 char *familyid[] = {
30         [0xf1] = "pc8374 (winbond, was NS)"
31 };
32
33 /* eventually, if you care, break this out into a file. For now, I don't know 
34   * if we need this. 
35   */
36
37 unsigned char regval(unsigned short port, unsigned char reg) {
38         outb(reg, port);
39         return inb(port + 1);
40 }
41
42 void regwrite(unsigned short port, unsigned char reg, unsigned char val) {
43         outb(reg, port);
44         outb(val, port + 1);
45 }
46
47 void
48 dump_ns8374(unsigned short port) {
49         printf("Enables: 21=%02x, 22=%02x, 23=%02x, 24=%02x, 26=%02x\n",
50                 regval(port, 0x21), regval(port, 0x22), regval(port, 0x23),
51                 regval(port, 0x24), regval(port, 0x26));
52         printf("SMBUS at %02x\n", regval(port, 0x2a));
53         /* check COM1. This is all we care about at present. */
54         printf("COM 1 is Globally %s\n", regval(port, 0x26) & 8 ? "disabled" : "enabled");
55         /* select com1 */
56         regwrite(port, 0x07, 0x03);
57         printf("COM 1 is locally %s\n", regval(port, 0x30) & 1 ? "enabled" : "disabled");
58         printf("COM1 60=%02x, 61=%02x, 70=%02x, 71=%02x, 74=%02x, 75=%02x, f0=%02x\n", 
59                 regval(port, 0x60), regval(port, 0x61), regval(port, 0x70), regval(port, 0x71),
60                 regval(port, 0x74), regval(port, 0x75), regval(port, 0xf0));
61         /* select gpio */
62         regwrite(port, 0x07, 0x07);
63         printf("GPIO is %s\n", regval(port, 0x30) & 1 ? "enabled" : "disabled");
64         printf("GPIO 60=%02x, 61=%02x, 70=%02x, 71=%02x, 74=%02x, 75=%02x, f0=%02x\n", 
65                 regval(port, 0x60), regval(port, 0x61), regval(port, 0x70), regval(port, 0x71),
66                 regval(port, 0x74), regval(port, 0x75), regval(port, 0xf0));
67
68 }
69
70 void
71 dump_fintek(unsigned short port, unsigned int did) 
72 {
73         switch(did) {
74         case 0x0604:
75                 printf ("Fintek F71805\n");
76                 break;
77         case 0x4103:
78                 printf ("Fintek F71872\n");
79                 break;
80         default:
81                 printf ("Unknown Fintek SuperI/O: did=0x%04x\n", did);
82                 return;
83         }
84
85         printf("Flash write is %s.\n", regval(port, 0x28) & 0x80 ? "enabled" : "disabled");
86         printf("Flash control is 0x%04x.\n", regval(port, 0x28));
87         printf("27=%02x\n", regval(port, 0x27));
88         printf("29=%02x\n", regval(port, 0x29));
89         printf("2a=%02x\n", regval(port, 0x2a));
90         printf("2b=%02x\n", regval(port, 0x2b));
91
92         /* select UART 1 */
93         regwrite(port, 0x07, 0x01);
94         printf("UART1 is %s\n", regval(port, 0x30) & 1 ? "enabled" : "disabled");
95         printf("UART1 base=%02x%02x, irq=%02x, mode=%s\n", 
96                 regval(port, 0x60), regval(port, 0x61), regval(port, 0x70) & 0x0f, 
97                 regval(port, 0xf0) & 0x10 ? "RS485":"RS232");
98
99         /* select UART 2 */
100         regwrite(port, 0x07, 0x02);
101         printf("UART2 is %s\n", regval(port, 0x30) & 1 ? "enabled" : "disabled");
102         printf("UART2 base=%02x%02x, irq=%02x, mode=%s\n", 
103                 regval(port, 0x60), regval(port, 0x61), regval(port, 0x70) & 0x0f, 
104                 regval(port, 0xf0) & 0x10 ? "RS485":"RS232");
105
106         /* select Parport */
107         regwrite(port, 0x07, 0x03);
108         printf("PARPORT is %s\n", regval(port, 0x30) & 1 ? "enabled" : "disabled");
109         printf("PARPORT base=%02x%02x, irq=%02x\n", 
110                 regval(port, 0x60), regval(port, 0x61), regval(port, 0x70) & 0x0f);
111
112         /* select hw monitor */
113         regwrite(port, 0x07, 0x04);
114         printf("HW monitor is %s\n", regval(port, 0x30) & 1 ? "enabled" : "disabled");
115         printf("HW monitor base=%02x%02x, irq=%02x\n", 
116                 regval(port, 0x60), regval(port, 0x61), regval(port, 0x70) & 0x0f);
117
118         /* select gpio */
119         regwrite(port, 0x07, 0x05);
120         printf("GPIO is %s\n", regval(port, 0x30) & 1 ? "enabled" : "disabled");
121         printf("GPIO 70=%02x, e0=%02x, e1=%02x, e2=%02x, e3=%02x, e4=%02x, e5=%02x\n", 
122                 regval(port, 0x70), regval(port, 0xe0), regval(port, 0xe1), regval(port, 0xe2),
123                 regval(port, 0xe3), regval(port, 0xe4), regval(port, 0xe5));
124         printf("GPIO e6=%02x, e7=%02x, e8=%02x, e9=%02x, f0=%02x, f1=%02x, f3=%02x, f4=%02x\n", 
125                 regval(port, 0xe6), regval(port, 0xe7), regval(port, 0xe8), regval(port, 0xe9),
126                 regval(port, 0xf0), regval(port, 0xf1), regval(port, 0xf3), regval(port, 0xf4));
127         printf("GPIO f5=%02x, f6=%02x, f7=%02x, f8=%02x\n", 
128                 regval(port, 0xf5), regval(port, 0xf6), regval(port, 0xf7), regval(port, 0xf8));
129
130
131 }
132
133 /* End Of Table */
134 #define EOT -1
135 /* NO LDN needed */
136 #define NOLDN -2
137 /* Not Available */
138 #define NANA -3
139 /* Maximum Name Length */
140 #define MAXNAMELEN 20
141 /* Biggest LDN */
142 #define MAXLDN 0xa
143 /* biggestLDN + 0 + NOLDN + EOT */
144 #define LDNSIZE MAXLDN + 3
145 /* MAXimum NUMber of Indexes */
146 #define MAXNUMIDX 70
147 #define IDXSIZE MAXNUMIDX + 1
148
149 const static struct ite_registers {
150         /* yes, superio_id should be unsigned, but EOT has to be negative */
151         signed short superio_id;
152         char name[MAXNAMELEN];
153         struct ite_ldnidx {
154                 signed short ldn;
155                 signed short idx[IDXSIZE];
156                 signed short def[IDXSIZE];
157         } ldn[LDNSIZE];
158 } ite_reg_table[] = {
159         {0x8702, "IT8702", {
160                 {EOT}}},
161         {0x8705, "IT8705 or IT8700", {
162                 {EOT}}},
163         {0x8708, "IT8708", {
164                 {NOLDN,
165                         {0x07,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
166                          0x29,0x2a,0x2e,0x2f,EOT},
167                         {NANA,0x87,0x08,0x00,0x00,NANA,0x3f,0x00,0xff,0xff,
168                          0xff,0xff,0x00,0x00,EOT}},
169                 {0x0,
170                         {0x30,0x60,0x61,0x70,0x74,0xf0,0xf1,EOT},
171                         {0x00,0x03,0xf0,0x06,0x02,0x00,0x00,EOT}},
172                 {0x1,
173                         {0x30,0x60,0x61,0x70,0xf0,EOT},
174                         {0x00,0x03,0xf8,0x04,0x00,EOT}},
175                 {0x2,
176                         {0x30,0x60,0x61,0x70,0xf0,0xf1,0xf2,0xf3,EOT},
177                         {0x00,0x02,0xf8,0x03,0x00,0x50,0x00,0x7f,EOT}},
178                 {0x3,
179                         {0x30,0x60,0x61,0x62,0x63,0x64,0x65,0x70,0x74,
180                          0xf0,EOT},
181                         {0x00,0x03,0x78,0x07,0x78,0x00,0x80,0x07,0x03,
182                          0x03,EOT}},
183                 {0x4,
184                         {0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,
185                          0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,EOT},
186                         {NANA,NANA,0x00,0x00,0x00,0x00,0x00,0x00,
187                          0x00,0x00,0x00,0x00,0x00,NANA,NANA,EOT}},
188                 {0x5,   /* Note: 0x30 can actually be 0x00 _or_ 0x01. */
189                         {0x30,0x60,0x61,0x62,0x63,0x70,0x71,0xf0,EOT},
190                         {0x01,0x00,0x60,0x00,0x64,0x01,0x02,0x00,EOT}},
191                 {0x6,
192                         {0x30,0x70,0x71,0xf0,EOT},
193                         {0x00,0x0c,0x02,0x00,EOT}},
194                 {0x7,
195                         {0x70,0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb8,0xb9,0xba,
196                          0xbb,0xbc,0xbd,0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc8,
197                          0xc9,0xca,0xcb,0xcc,0xcd,0xd0,0xd1,0xd2,0xd3,0xd4,
198                          0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xf0,0xf1,
199                          0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,
200                          0xfc,EOT},
201                         {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
202                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
203                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
204                          0x00,0x00,NANA,NANA,NANA,NANA,NANA,NANA,0x00,0x00,
205                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,NANA,
206                          0x00,EOT}},
207                 {0x8,
208                         {0x30,0x60,0x61,EOT},
209                         {0x00,0x02,0x01,EOT}},
210                 {0x9,
211                         {0x30,0x60,0x61,0x70,0xf0,EOT},
212                         {0x00,0x03,0x10,0x0b,0x00,EOT}},
213                 {0xa,
214                         {0x30,0x60,0x61,0x70,0xf0,EOT},
215                         {0x00,0x03,0x00,0x0a,0x00,EOT}},
216                 {EOT}}},
217         {0x8710, "IT8710", {
218                 {EOT}}},
219         {0x8712, "IT8712", {
220                 {NOLDN,
221                         {0x07,0x20,0x21,0x22,0x23,0x24,0x2b,EOT},
222                         {NANA,0x87,0x12,0x08,0x00,0x00,0x00,EOT}},
223                 {0x0,
224                         {0x30,0x60,0x61,0x70,0x74,0xf0,0xf1,EOT},
225                         {0x00,0x03,0xf0,0x06,0x02,0x00,0x00,EOT}},
226                 {0x1,
227                         {0x30,0x60,0x61,0x70,0xf0,0xf1,0xf2,0xf3,EOT},
228                         {0x00,0x03,0xf8,0x04,0x00,0x50,0x00,0x7f,EOT}},
229                 {0x2,
230                         {0x30,0x60,0x61,0x70,0xf0,0xf1,0xf2,0xf3,EOT},
231                         {0x00,0x02,0xf8,0x03,0x00,0x50,0x00,0x7f,EOT}},
232                 {0x3,
233                         {0x30,0x60,0x61,0x62,0x63,0x70,0x74,0xf0,EOT},
234                         {0x00,0x03,0x78,0x07,0x78,0x07,0x03,0x03,EOT}},
235                 {0x4,
236                         {0x30,0x60,0x61,0x62,0x63,0x70,0xf0,0xf1,0xf2,0xf3,
237                          0xf4,0xf5,0xf6,EOT},
238                         {0x00,0x02,0x90,0x02,0x30,0x09,0x00,0x00,0x00,0x00,
239                          0x00,NANA,NANA,EOT}},
240                 {0x5,
241                         {0x30,0x60,0x61,0x62,0x63,0x70,0x71,0xf0,EOT},
242                         {0x01,0x00,0x60,0x00,0x64,0x01,0x02,0x08,EOT}},
243                 {0x6,
244                         {0x30,0x70,0x71,0xf0,EOT},
245                         {0x00,0x0c,0x02,0x00,EOT}},
246                 {0x7,
247                         {0x25,0x26,0x27,0x28,0x29,0x2a,0x2c,0x60,0x61,0x62,
248                          0x63,0x64,0x65,0x70,0x71,0x72,0x73,0x74,0xb0,0xb1,
249                          0xb2,0xb3,0xb4,0xb5,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,
250                          0xc0,0xc1,0xc2,0xc3,0xc4,0xc8,0xc9,0xca,0xcb,0xcc,
251                          0xe0,0xe1,0xe2,0xe3,0xe4,0xf0,0xf1,0xf2,0xf3,0xf4,
252                          0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,EOT},
253                         {0x01,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
254                          0x00,0x00,0x00,0x00,0x00,0x30,0x38,0x00,0x00,0x00,
255                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
256                          0x01,0x00,0x00,0x40,0x00,0x01,0x00,0x00,0x40,0x00,
257                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
258                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,NANA,0x00,EOT}},
259                 {0x8,
260                         {0x30,0x60,0x61,0x70,0xf0,EOT},
261                         {0x00,0x03,0x00,0x0a,0x00,EOT}},
262                 {0x9,
263                         {0x30,0x60,0x61,EOT},
264                         {0x00,0x02,0x01,EOT}},
265                 {0xa,
266                         {0x30,0x60,0x61,0x70,0xf0,EOT},
267                         {0x00,0x03,0x10,0x0b,0x00,EOT}},
268                 {EOT}}},
269         {0x8716, "IT8716", {
270                 {EOT}}},
271         {0x8718, "IT8718", {
272                 {EOT}}},
273         {EOT}
274 };
275
276 void
277 dump_ite(unsigned short port, unsigned short id) 
278 {
279         int i, j, k;
280         signed short *idx;
281         printf ("ITE ");
282
283
284         /* ID Mapping Table
285            unknown -> IT8711 (no datasheet)
286            unknown -> IT8722 (no datasheet)
287            0x8702 -> IT8702
288            0x8705 -> IT8700 or IT8705
289            0x8708 -> IT8708
290            0x8710 -> IT8710
291            0x8712 -> IT8712
292            0x8716 -> IT8716
293            0x8718 -> IT8718
294            0x8726 -> IT8726 (datasheet wrongly says 0x8716)
295         */
296         switch(id) {
297         case 0x8702:
298         case 0x8705:
299         case 0x8708:
300         case 0x8710:
301         case 0x8712:
302         case 0x8716:
303         case 0x8718:
304                 for (i=0;; i++) {
305                         if (ite_reg_table[i].superio_id == EOT)
306                                 break;
307                         if ((unsigned short)ite_reg_table[i].superio_id != id)
308                                 continue;
309                         printf ("%s\n", ite_reg_table[i].name);
310                         for (j=0;; j++) {
311                                 if (ite_reg_table[i].ldn[j].ldn == EOT)
312                                         break;
313                                 if (ite_reg_table[i].ldn[j].ldn != NOLDN) {
314                                         printf("switching to LDN 0x%01x\n",
315                                                ite_reg_table[i].ldn[j].ldn);
316                                         regwrite(port, 0x07,
317                                                  ite_reg_table[i].ldn[j].ldn);
318                                 }
319                                 idx = ite_reg_table[i].ldn[j].idx;
320                                 printf("idx ");
321                                 for (k=0;; k++) {
322                                         if (idx[k] == EOT)
323                                                 break;
324                                         printf("%02x ", idx[k]);
325                                 }
326                                 printf("\nval ");
327                                 for (k=0;; k++) {
328                                         if (idx[k] == EOT)
329                                                 break;
330                                         printf("%02x ", regval(port, idx[k]));
331                                 }
332                                 printf("\ndef ");
333                                 idx = ite_reg_table[i].ldn[j].def;
334                                 for (k=0;; k++) {
335                                         if (idx[k] == EOT)
336                                                 break;
337                                         if (idx[k] == NANA)
338                                                 printf("NA ");
339                                         else
340                                                 printf("%02x ", idx[k]);
341                                 }
342                                 printf("\n");
343                         }
344                                         
345                 }
346                 break;
347         default:
348                 printf ("unknown ITE chip, id=%04x\n", id);
349                 for (i=0x20; i<=0x24; i++)
350                         printf("index %02x=%02x\n", i, regval(port, i));
351                 break;
352         }
353 }
354
355 void
356 probe_idregs_simple(unsigned short port){
357         unsigned char id;
358         outb(0x20, port);
359         if (inb(port) != 0x20)  {
360                 if (inb(port) == 0xff )
361                         printf ("No SuperI/O chip found at 0x%04x\n", port);
362                 else
363                         printf("probing 0x%04x, failed (0x%02x), data returns 0x%02x\n", 
364                                         port, inb(port), inb(port + 1));
365                 return;
366         }
367         id = inb(port + 1);
368
369         printf("SuperI/O found at 0x%02x: id = 0x%02x\n", port, id);
370         if (id == 0xff)
371                 return;
372
373         if (familyid[id]) 
374                 printf("%s\n", familyid[id]);
375         else
376                 printf("<unknown>\n");
377
378         switch(id) {
379                 case 0xf1:
380                         dump_ns8374(port);
381                         break;
382                 default:
383                         printf("no dump for 0x%02x\n", id);
384                         break;
385         }
386 }
387
388
389 void
390 probe_idregs_fintek(unsigned short port){
391         unsigned int vid, did, success = 0;
392
393         /* Enable configuration sequence (Fintek uses this for example)
394            Older ITE chips have the same enable sequence */
395         outb(0x87, port);
396         outb(0x87, port);
397
398         outb(0x20, port);
399         if (inb(port) != 0x20)  {
400                 if (inb(port) == 0xff )
401                         printf ("No SuperIO chip found at 0x%04x\n", port);
402                 else
403                         printf("probing 0x%04x, failed (0x%02x), data returns 0x%02x\n", 
404                                         port, inb(port), inb(port + 1));
405                 return;
406         }
407         did = inb(port + 1);
408
409         did |= (regval(port, 0x21)<<8);
410
411         vid = regval(port, 0x23);
412         vid |= (regval(port, 0x24)<<8);
413
414         printf("SuperIO found at 0x%02x: vid=0x%04x/did=0x%04x\n", port, vid, did);
415
416         if (vid == 0xff || vid == 0xffff)
417                 return;
418
419         /* printf("%s\n", familyid[id]); */
420         switch(did) {
421                 case 0x0887: /* pseudoreversed for ITE8708 */
422                 case 0x1087: /* pseudoreversed for ITE8710 */
423                         success = 1;
424                         dump_ite(port, ((did & 0xff) << 8) | ((did & 0xff00) >> 8));
425                         /* disable configuration */
426                         regwrite(port, 0x02, 0x02);
427                         break;
428                 default:
429                         break;
430         }
431         switch(vid) {
432                 case 0x3419:
433                         success = 1;
434                         dump_fintek(port, did);
435                         break;
436                 default:
437                         break;
438         }
439         if (!success)
440                 printf("no dump for vid 0x%04x, did 0x%04x\n", vid, did);
441
442         /* disable configuration (for Fintek, doesn't hurt ITE) */
443         outb(0xaa, port);
444 }
445
446 void
447 probe_idregs_ite(unsigned short port){
448         unsigned int id, chipver;
449
450         /* Enable configuration sequence (ITE uses this for newer IT87[012]x)
451            IT871[01] uses 0x87, 0x87 -> fintek detection should handle it
452            IT8708 uses 0x87, 0x87 -> fintek detection should handle it
453            IT8761 uses 0x87, 0x61, 0x55, 0x55/0xaa
454            IT86xx series uses different ports
455            IT8661 uses 0x86, 0x61, 0x55/0xaa, 0x55/0xaa and 32 more writes
456            IT8673 uses 0x86, 0x80, 0x55/0xaa, 0x55/0xaa and 32 more writes */
457         outb(0x87, port);
458         outb(0x01, port);
459         outb(0x55, port);
460         if (port == 0x2e)
461                 outb(0x55, port);
462         else
463                 outb(0xAA, port);
464
465         /* Read Chip ID Byte 1 */
466         id = regval(port, 0x20);
467         if (id != 0x87)  {
468                 if (inb(port) == 0xff )
469                         printf ("No SuperIO chip found at 0x%04x\n", port);
470                 else
471                         printf("probing 0x%04x, failed (0x%02x), data returns 0x%02x\n", 
472                                         port, inb(port), inb(port + 1));
473                 return;
474         }
475
476         id <<= 8;
477
478         /* Read Chip ID Byte 2 */
479         id |= regval(port, 0x21);
480
481         /* Read Chip Version, only bit 3..0 for all IT87xx */
482         chipver = regval(port, 0x22) & 0x0f;
483
484         /* ID Mapping Table
485            unknown -> IT8711 (no datasheet)
486            unknown -> IT8722 (no datasheet)
487            0x8702 -> IT8702
488            0x8705 -> IT8700 or IT8705
489            0x8708 -> IT8708
490            0x8710 -> IT8710
491            0x8712 -> IT8712
492            0x8716 -> IT8716
493            0x8718 -> IT8718
494            0x8726 -> IT8726 (datasheet wrongly says 0x8716)
495         */
496         printf("SuperI/O found at 0x%02x: id=0x%04x, chipver=0x%01x\n",
497                port, id, chipver);
498
499         switch(id) {
500                 case 0x8702:
501                 case 0x8705:
502                 case 0x8708:
503                 case 0x8712:
504                 case 0x8716:
505                 case 0x8718:
506                 case 0x8726:
507                         dump_ite(port, id);
508                         break;
509                 default:
510                         printf("no dump for id 0x%04x\n", id);
511                         break;
512         }
513         /* disable configuration */
514         regwrite(port, 0x02, 0x02);
515 }
516
517 void
518 probe_superio(unsigned short port) {
519         probe_idregs_simple(port);
520         probe_idregs_fintek(port);
521         probe_idregs_ite(port);
522 }
523
524 int
525 main(int argc, char *argv[])
526 {
527         if (iopl(3) < 0) {
528                 perror("iopl");
529                 exit(1);
530         }
531
532         /* try the 2e */
533         probe_superio(0x2e);
534         /* now try the 4e */
535         probe_superio(0x4e);
536
537         return 0;
538 }