Add constants for fast path resume copying
[coreboot.git] / src / superio / smsc / smscsuperio / superio.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 /*
22  * Generic driver for pretty much all known Standard Microsystems Corporation
23  * (SMSC) Super I/O chips.
24  *
25  * Datasheets are available from: http://www.smsc.com/main/datasheet.html
26  *
27  * Most of the SMSC Super I/O chips seem to be similar enough (for our
28  * purposes) so that we can handle them with a unified driver.
29  *
30  * So far only the ASUS A8000 has been tested on real hardware!
31  *
32  * The floppy disk controller, the parallel port, the serial ports, and the
33  * keyboard controller should work with all the chips. For the more advanced
34  * stuff (e.g. HWM, ACPI, SMBus) more work is probably required.
35  */
36
37 #include <arch/io.h>
38 #include <device/device.h>
39 #include <device/pnp.h>
40 #include <console/console.h>
41 #include <uart8250.h>
42 #include <pc80/keyboard.h>
43 #include <stdlib.h>
44 #include "chip.h"
45
46 /* The following Super I/O chips are currently supported by this driver: */
47 #define LPC47M172       0x14
48 #define FDC37B80X       0x42    /* Same ID: FDC37M70X (a.k.a. FDC37M707) */
49 #define FDC37B78X       0x44
50 #define FDC37B72X       0x4c
51 #define FDC37M81X       0x4d
52 #define FDC37M60X       0x47
53 #define LPC47B27X       0x51    /* a.k.a. LPC47B272 */
54 #define LPC47U33X       0x54
55 #define LPC47M10X       0x59    /* Same ID: LPC47M112, LPC47M13X */
56 #define LPC47M15X       0x60    /* Same ID: LPC47M192 */
57 #define LPC47S45X       0x62
58 #define LPC47B397       0x6f
59 #define A8000           0x77    /* ASUS A8000, a rebranded DME1737(?) */
60 #define DME1737         0x78
61 #define SCH3112         0x7c
62 #define SCH3114         0x7d
63 #define SCH5307         0x81    /* Rebranded LPC47B397(?) */
64 #define SCH5027D        0x89
65 #define SCH4304         0x90    /* SCH4304, SCH4307 */
66
67 /* Register defines */
68 #define DEVICE_ID_REG   0x20    /* Device ID register */
69 #define DEVICE_REV_REG  0x21    /* Device revision register */
70 #define DEVICE_TEST7_REG 0x29   /* Device test 7 register */
71
72 /* Static variables for the Super I/O device ID and revision. */
73 static int first_time = 1;
74 static u8 superio_id = 0;
75 static u8 superio_rev = 0;
76
77 /**
78  * A list of all possible logical devices which may be supported by at least
79  * one of the Super I/O chips. These values are used as index into the
80  * logical_device_table[i].devs array(s).
81  *
82  * If you change this enum, you must also adapt the logical_device_table[]
83  * array and MAX_LOGICAL_DEVICES!
84  */
85 enum {
86         LD_FDC,         /* Floppy disk controller */
87         LD_PP,          /* Parallel port */
88         LD_SP1,         /* Serial port 1 (COM1) */
89         LD_SP2,         /* Serial port 2 (COM2) */
90         LD_RTC,         /* Real-time clock */
91         LD_KBC,         /* Keyboard controller */
92         LD_AUX,         /* Auxiliary I/O */
93         LD_XBUS,        /* X-Bus */
94         LD_HWM,         /* Hardware monitor */
95         LD_GAME,        /* Game port */
96         LD_PME,         /* Power management events */
97         LD_MPU401,      /* MPU-401 MIDI UART */
98         LD_RT,          /* Runtime registers / security key registers */
99         LD_ACPI,        /* ACPI */
100         LD_SMB,         /* SMBus */
101 };
102
103 /* Note: This value must match the number of items in the enum above! */
104 #define MAX_LOGICAL_DEVICES 15
105
106 /**
107  * A table describing the logical devices which are present on the
108  * supported Super I/O chips.
109  *
110  * The first entry (superio_id) is the device ID of the Super I/O chip
111  * as stored in the (read-only) DEVICE_ID_REG register.
112  *
113  * The second entry (devs) is the list of logical device IDs which are
114  * present on that particular Super I/O chip. A value of -1 means the
115  * device is not present on that chip.
116  *
117  * Note: Do _not_ list chips with different name but same device ID twice!
118  *       The result would be that the init code would be executed twice!
119  */
120 static const struct logical_devices {
121         u8 superio_id;
122         int devs[MAX_LOGICAL_DEVICES];
123 } logical_device_table[] = {
124         /* Chip   FDC PP SP1 SP2 RTC KBC AUX XBUS HWM GAME PME MPU RT ACPI SMB */
125         {LPC47M172,{0, 3, 4,  2, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, -1}},
126         {FDC37B80X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, -1, -1}},
127         {FDC37B78X,{0, 3, 4,  5,  6,  7,  8,  -1, -1,  -1, -1, -1, -1, 10, -1}},
128         {FDC37B72X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, 10, -1}},
129         {FDC37M81X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, -1, -1}},
130         {FDC37M60X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, -1, -1}},
131         {LPC47B27X,{0, 3, 4,  5, -1,  7, -1,  -1, -1,   9, -1, 11, 10, -1, -1}},
132         {LPC47M10X,{0, 3, 4,  5, -1,  7, -1,  -1, -1,   9, 10, 11, -1, -1, -1}},
133         {LPC47M15X,{0, 3, 4,  5, -1,  7, -1,  -1, -1,   9, 10, 11, -1, -1, -1}},
134         {LPC47S45X,{0, 3, 4,  5,  6,  7, -1,   8, -1,  -1, -1, -1, 10, -1, 11}},
135         {LPC47B397,{0, 3, 4,  5, -1,  7, -1,  -1,  8,  -1, -1, -1, 10, -1, -1}},
136         {LPC47U33X,{0, 3, 4, -1, -1,  7, -1,  -1, -1,   9,  0,  5, 10,  0, 11}},
137         {A8000,    {0, 3, 4,  5, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, -1}},
138         {DME1737,  {0, 3, 4,  5, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, -1}},
139         {SCH3112,  {0, 3, 4,  5, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, -1}},
140         {SCH3114,  {0, 3, 4,  5, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, -1}},
141         {SCH5307,  {0, 3, 4,  5, -1,  7, -1,  -1,  8,  -1, -1, -1, 10, -1, -1}},
142         {SCH5027D, {0, 3, 4,  5, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, 11}},
143         {SCH4304,  {0, 3, 4,  5, -1,  7, -1,  11, -1,  -1, -1, -1, 10, -1, -1}},
144 };
145
146 /**
147  * Enter the configuration state by writing 0x55 to the config port.
148  *
149  * The Super I/O configuration registers can only be modified when the chip
150  * is in the configuration state. Thus, to program the registers you have
151  * to a) enter config mode, b) program the registers, c) exit config mode.
152  *
153  * @param dev The device to use.
154  */
155 static void smsc_pnp_enter_conf_state(device_t dev)
156 {
157         outb(0x55, dev->path.pnp.port);
158 }
159
160 /**
161  * Exit the configuration state by writing 0xaa to the config port.
162  *
163  * This puts the chip into the 'run' state again.
164  *
165  * @param dev The device to use.
166  */
167 static void smsc_pnp_exit_conf_state(device_t dev)
168 {
169         outb(0xaa, dev->path.pnp.port);
170 }
171
172 /** Wrapper for pnp_set_resources(). */
173 static void smsc_pnp_set_resources(device_t dev)
174 {
175         smsc_pnp_enter_conf_state(dev);
176         pnp_set_resources(dev);
177         smsc_pnp_exit_conf_state(dev);
178 }
179
180 /** Wrapper for pnp_enable_resources(). */
181 static void smsc_pnp_enable_resources(device_t dev)
182 {
183         smsc_pnp_enter_conf_state(dev);
184         pnp_enable_resources(dev);
185         smsc_pnp_exit_conf_state(dev);
186 }
187
188 /**
189  * If so configured, enable the specified device, otherwise
190  * explicitly disable it.
191  *
192  * @param dev The device to use.
193  */
194 static void smsc_pnp_enable(device_t dev)
195 {
196         smsc_pnp_enter_conf_state(dev);
197         pnp_set_logical_device(dev);
198         pnp_set_enable(dev, !!dev->enabled);
199         smsc_pnp_exit_conf_state(dev);
200 }
201
202 /**
203  * Initialize those logical devices which need a special init.
204  *
205  * @param dev The device to use.
206  */
207 static void smsc_init(device_t dev)
208 {
209         struct superio_smsc_smscsuperio_config *conf = dev->chip_info;
210         int i, ld;
211
212         /* Do not initialize disabled devices. */
213         if (!dev->enabled)
214                 return;
215
216         /* Find the correct Super I/O. */
217         for (i = 0; i < ARRAY_SIZE(logical_device_table); i++)
218                 if (logical_device_table[i].superio_id == superio_id)
219                         break;
220
221         /* If no Super I/O was found, return. */
222         if (i == ARRAY_SIZE(logical_device_table))
223                 return;
224
225         /* A Super I/O was found, so initialize the respective device. */
226         ld = dev->path.pnp.device;
227         if (ld == logical_device_table[i].devs[LD_KBC]) {
228                 pc_keyboard_init(&conf->keyboard);
229         }
230 }
231
232 /** Standard device operations. */
233 static struct device_operations ops = {
234         .read_resources         = pnp_read_resources,
235         .set_resources          = smsc_pnp_set_resources,
236         .enable_resources       = smsc_pnp_enable_resources,
237         .enable                 = smsc_pnp_enable,
238         .init                   = smsc_init,
239 };
240
241 /**
242  * TODO.
243  *
244  * This table should contain all possible entries for any of the supported
245  * Super I/O chips, even if some of them don't have the respective logical
246  * devices. That will be handled correctly by our code.
247  *
248  * The LD_FOO entries are device markers which tell you the type of the logical
249  * device (e.g. whether it's a floppy disk controller or a serial port etc.).
250  *
251  * Before using pnp_dev_info[] in pnp_enable_devices() these markers have
252  * to be replaced with the real logical device IDs of the respective
253  * Super I/O chip. This is done in enable_dev().
254  *
255  * TODO: FDC, PP, SP1, SP2, and KBC should work, the rest probably not (yet).
256  */
257 static struct pnp_info pnp_dev_info[] = {
258         { &ops, LD_FDC, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, {0x07f8, 0}, },
259         { &ops, LD_PP,  PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, {0x07f8, 0}, },
260         { &ops, LD_SP1, PNP_IO0 | PNP_IRQ0, {0x07f8, 0}, },
261         { &ops, LD_SP2, PNP_IO0 | PNP_IRQ0, {0x07f8, 0}, },
262         { &ops, LD_RTC, },
263         { &ops, LD_KBC, PNP_IO0 | PNP_IO1 | PNP_IRQ0 | PNP_IRQ1, {0x07ff, 0}, {0x07ff, 4}, },
264         { &ops, LD_AUX, },
265         { &ops, LD_XBUS, },
266         { &ops, LD_HWM, PNP_IO0, {0x07f0, 0}, },
267         { &ops, LD_GAME, },
268         { &ops, LD_PME, },
269         { &ops, LD_MPU401, },
270         { &ops, LD_RT,  PNP_IO0, {0x0780, 0}, },
271         { &ops, LD_ACPI, },
272         { &ops, LD_SMB, },
273 };
274
275 /**
276  * Enable the logical devices of the Super I/O chip.
277  *
278  * TODO: Think about how to handle the case when a mainboard has multiple
279  *       Super I/O chips soldered on.
280  * TODO: Can this code be simplified a bit?
281  *
282  * @param dev The device to use.
283  */
284 static void enable_dev(device_t dev)
285 {
286         int i, j, fn;
287         int tmp[MAX_LOGICAL_DEVICES];
288         u8 test7;
289
290         if (first_time) {
291                 /* Read the device ID and revision of the Super I/O chip. */
292                 smsc_pnp_enter_conf_state(dev);
293                 superio_id = pnp_read_config(dev, DEVICE_ID_REG);
294                 superio_rev = pnp_read_config(dev, DEVICE_REV_REG);
295                 smsc_pnp_exit_conf_state(dev);
296
297                 /* TODO: Error handling? */
298
299                 printk(BIOS_INFO, "Found SMSC Super I/O (ID=0x%02x, "
300                        "rev=0x%02x)\n", superio_id, superio_rev);
301                 first_time = 0;
302
303                 if (superio_id == LPC47M172) {
304                         /*
305                          * Do not use the default logical device number but
306                          * instead the standard SMSC registers set.
307                          */
308
309                         /*
310                          * TEST7 configuration register (0x29)
311                          * Bit 0: LD_NUM (0 = new, 1 = std SMSC)
312                          */
313                         test7 = pnp_read_config(dev, DEVICE_TEST7_REG);
314                         test7 |= (1 << 0);
315                         pnp_write_config(dev, DEVICE_TEST7_REG, test7);
316                 }
317         }
318
319         /* Find the correct Super I/O. */
320         for (i = 0; i < ARRAY_SIZE(logical_device_table); i++)
321                 if (logical_device_table[i].superio_id == superio_id)
322                         break;
323
324         /* If no Super I/O was found, return. */
325         if (i == ARRAY_SIZE(logical_device_table))
326                 return;
327
328         /* Temporarily save the LD_FOO values. */
329         for (j = 0; j < ARRAY_SIZE(pnp_dev_info); j++)
330                 tmp[j] = pnp_dev_info[j].function;
331
332         /*
333          * Replace the LD_FOO markers in pnp_dev_info[] with
334          * the real logical device IDs of this Super I/O chip.
335          */
336         for (j = 0; j < ARRAY_SIZE(pnp_dev_info); j++) {
337                 fn = pnp_dev_info[j].function;
338                 pnp_dev_info[j].function = logical_device_table[i].devs[fn];
339         }
340
341         /* Enable the specified devices (if present on the chip). */
342         pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info),
343                            &pnp_dev_info[0]);
344
345         /* Restore LD_FOO values. */
346         for (j = 0; j < ARRAY_SIZE(pnp_dev_info); j++)
347                 pnp_dev_info[j].function = tmp[j];
348 }
349
350 struct chip_operations superio_smsc_smscsuperio_ops = {
351         CHIP_NAME("Various SMSC Super I/Os")
352         .enable_dev = enable_dev
353 };