Please bear with me - another rename checkin. This qualifies as trivial, no
[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 FDC37B80X       0x42    /* Same ID: FDC37M70X (a.k.a. FDC37M707) */
48 #define FDC37B78X       0x44
49 #define FDC37B72X       0x4c
50 #define FDC37M81X       0x4d
51 #define FDC37M60X       0x47
52 #define LPC47B27X       0x51    /* a.k.a. LPC47B272 */
53 #define LPC47M10X       0x59    /* Same ID: LPC47M112, LPC47M13X */
54 #define LPC47M15X       0x60    /* Same ID: LPC47M192 */
55 #define LPC47S45X       0x62
56 #define LPC47B397       0x6f
57 #define A8000           0x77    /* ASUS A8000, a rebranded DME1737(?) */
58 #define DME1737         0x78
59 #define SCH5307         0x81    /* Rebranded LPC47B397(?) */
60
61 /* Register defines */
62 #define DEVICE_ID_REG   0x20    /* Device ID register */
63 #define DEVICE_REV_REG  0x21    /* Device revision register */
64
65 /* Static variables for the Super I/O device ID and revision. */
66 static int first_time = 1;
67 static uint8_t superio_id = 0;
68 static uint8_t superio_rev = 0;
69
70 /**
71  * A list of all possible logical devices which may be supported by at least
72  * one of the Super I/O chips. These values are used as index into the
73  * logical_device_table[i].devs array(s).
74  *
75  * If you change this enum, you must also adapt the logical_device_table[]
76  * array and MAX_LOGICAL_DEVICES!
77  */
78 enum {
79         LD_FDC,         /* Floppy disk controller */
80         LD_PP,          /* Parallel port */
81         LD_SP1,         /* Serial port 1 (COM1) */
82         LD_SP2,         /* Serial port 2 (COM2) */
83         LD_RTC,         /* Real-time clock */
84         LD_KBC,         /* Keyboard controller */
85         LD_AUX,         /* Auxiliary I/O */
86         LD_XBUS,        /* X-Bus */
87         LD_HWM,         /* Hardware monitor */
88         LD_GAME,        /* Game port */
89         LD_PME,         /* Power management events */
90         LD_MPU401,      /* MPU-401 MIDI UART */
91         LD_RT,          /* Runtime registers / security key registers */
92         LD_ACPI,        /* ACPI */
93         LD_SMB,         /* SMBus */
94 };
95
96 /* Note: This value must match the number of items in the enum above! */
97 #define MAX_LOGICAL_DEVICES 15
98
99 /**
100  * A table describing the logical devices which are present on the
101  * supported Super I/O chips.
102  *
103  * The first entry (superio_id) is the device ID of the Super I/O chip
104  * as stored in the (read-only) DEVICE_ID_REG register.
105  *
106  * The second entry (devs) is the list of logical device IDs which are
107  * present on that particular Super I/O chip. A value of -1 means the
108  * device is not present on that chip.
109  *
110  * Note: Do _not_ list chips with different name but same device ID twice!
111  *       The result would be that the init code would be executed twice!
112  */
113 static const struct logical_devices {
114         uint8_t superio_id;
115         int devs[MAX_LOGICAL_DEVICES];
116 } logical_device_table[] = {
117         // Chip   FDC PP SP1 SP2 RTC KBC AUX XBUS HWM GAME PME MPU RT ACPI SMB
118         {FDC37B80X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, -1, -1}},
119         {FDC37B78X,{0, 3, 4,  5,  6,  7,  8,  -1, -1,  -1, -1, -1, -1, 10, -1}},
120         {FDC37B72X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, 10, -1}},
121         {FDC37M81X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, -1, -1}},
122         {FDC37M60X,{0, 3, 4,  5, -1,  7,  8,  -1, -1,  -1, -1, -1, -1, -1, -1}},
123         {LPC47B27X,{0, 3, 4,  5, -1,  7, -1,  -1, -1,   9, -1, 11, 10, -1, -1}},
124         {LPC47M10X,{0, 3, 4,  5, -1,  7, -1,  -1, -1,   9, 10, 11, -1, -1, -1}},
125         {LPC47M15X,{0, 3, 4,  5, -1,  7, -1,  -1, -1,   9, 10, 11, -1, -1, -1}},
126         {LPC47S45X,{0, 3, 4,  5,  6,  7, -1,   8, -1,  -1, -1, -1, 10, -1, 11}},
127         {LPC47B397,{0, 3, 4,  5, -1,  7, -1,  -1,  8,  -1, -1, -1, 10, -1, -1}},
128         {A8000,    {0, 3, 4,  5, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, -1}},
129         {DME1737,  {0, 3, 4,  5, -1,  7, -1,  -1, -1,  -1, -1, -1, 10, -1, -1}},
130         {SCH5307,  {0, 3, 4,  5, -1,  7, -1,  -1,  8,  -1, -1, -1, 10, -1, -1}},
131 };
132
133 /**
134  * Enter the configuration state by writing 0x55 to the config port.
135  *
136  * The Super I/O configuration registers can only be modified when the chip
137  * is in the configuration state. Thus, to program the registers you have
138  * to a) enter config mode, b) program the registers, c) exit config mode.
139  *
140  * @param dev The device to use.
141  */
142 static inline void smsc_pnp_enter_conf_state(device_t dev)
143 {
144         outb(0x55, dev->path.u.pnp.port);
145 }
146
147 /**
148  * Exit the configuration state by writing 0xaa to the config port.
149  *
150  * This puts the chip into the 'run' state again.
151  *
152  * @param dev The device to use.
153  */
154 static inline void smsc_pnp_exit_conf_state(device_t dev)
155 {
156         outb(0xaa, dev->path.u.pnp.port);
157 }
158
159 /** Wrapper for pnp_set_resources(). */
160 static void smsc_pnp_set_resources(device_t dev)
161 {
162         smsc_pnp_enter_conf_state(dev);
163         pnp_set_resources(dev);
164         smsc_pnp_exit_conf_state(dev);
165 }
166
167 /** Wrapper for pnp_enable_resources(). */
168 static void smsc_pnp_enable_resources(device_t dev)
169 {
170         smsc_pnp_enter_conf_state(dev);
171         pnp_enable_resources(dev);
172         smsc_pnp_exit_conf_state(dev);
173 }
174
175 /**
176  * If so configured, enable the specified device, otherwise
177  * explicitly disable it.
178  *
179  * @param dev The device to use.
180  */
181 static void smsc_pnp_enable(device_t dev)
182 {
183         smsc_pnp_enter_conf_state(dev);
184         pnp_set_logical_device(dev);
185         (dev->enabled) ? pnp_set_enable(dev, 1) : pnp_set_enable(dev, 0);
186         smsc_pnp_exit_conf_state(dev);
187 }
188
189 /**
190  * Initialize those logical devices which need a special init.
191  *
192  * @param dev The device to use.
193  */
194 static void smsc_init(device_t dev)
195 {
196         struct superio_smsc_smscsuperio_config *conf = dev->chip_info;
197         struct resource *res0, *res1;
198         int i, ld;
199
200         /* Do not initialize disabled devices. */
201         if (!dev->enabled)
202                 return;
203
204         /* Find the correct Super I/O. */
205         for (i = 0; i < ARRAY_SIZE(logical_device_table); i++)
206                 if (logical_device_table[i].superio_id == superio_id)
207                         break;
208
209         /* If no Super I/O was found, return. */
210         if (i == ARRAY_SIZE(logical_device_table))
211                 return;
212
213         /* A Super I/O was found, so initialize the respective device. */
214         ld = dev->path.u.pnp.device;
215         if (ld == logical_device_table[i].devs[LD_SP1]) {
216                 res0 = find_resource(dev, PNP_IDX_IO0);
217                 init_uart8250(res0->base, &conf->com1);
218         } else if (ld == logical_device_table[i].devs[LD_SP2]) {
219                 res0 = find_resource(dev, PNP_IDX_IO0);
220                 init_uart8250(res0->base, &conf->com2);
221         } else if (ld == logical_device_table[i].devs[LD_KBC]) {
222                 res0 = find_resource(dev, PNP_IDX_IO0);
223                 res1 = find_resource(dev, PNP_IDX_IO1);
224                 init_pc_keyboard(res0->base, res1->base, &conf->keyboard);
225         }
226 }
227
228 /** Standard device operations. */
229 static struct device_operations ops = {
230         .read_resources         = pnp_read_resources,
231         .set_resources          = smsc_pnp_set_resources,
232         .enable_resources       = smsc_pnp_enable_resources,
233         .enable                 = smsc_pnp_enable,
234         .init                   = smsc_init,
235 };
236
237 /**
238  * TODO.
239  *
240  * This table should contain all possible entries for any of the supported
241  * Super I/O chips, even if some of them don't have the respective logical
242  * devices. That will be handled correctly by our code.
243  *
244  * The LD_FOO entries are device markers which tell you the type of the logical
245  * device (e.g. whether it's a floppy disk controller or a serial port etc.).
246  *
247  * Before using pnp_dev_info[] in pnp_enable_devices() these markers have
248  * to be replaced with the real logical device IDs of the respective
249  * Super I/O chip. This is done in enable_dev().
250  *
251  * TODO: FDC, PP, SP1, SP2, and KBC should work, the rest probably not (yet).
252  */
253 static struct pnp_info pnp_dev_info[] = {
254         { &ops, LD_FDC, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, { 0x07f8, 0 }, },
255         { &ops, LD_PP,  PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, { 0x07f8, 0 }, },
256         { &ops, LD_SP1, PNP_IO0 | PNP_IRQ0, { 0x7f8, 0 }, },
257         { &ops, LD_SP2, PNP_IO0 | PNP_IRQ0, { 0x7f8, 0 }, },
258         { &ops, LD_RTC, },
259         { &ops, LD_KBC, PNP_IO0 | PNP_IO1 | PNP_IRQ0 | PNP_IRQ1, { 0x7ff, 0 },
260                                                                  { 0x7ff, 4 },},
261         { &ops, LD_AUX, },
262         { &ops, LD_XBUS, },
263         { &ops, LD_HWM, PNP_IO0, { 0x7f0, 0 }, },
264         { &ops, LD_GAME, },
265         { &ops, LD_PME, },
266         { &ops, LD_MPU401, },
267         { &ops, LD_RT,  PNP_IO0, { 0x780, 0 }, },
268         { &ops, LD_ACPI, },
269         { &ops, LD_SMB, },
270 };
271
272 /**
273  * Enable the logical devices of the Super I/O chip.
274  *
275  * TODO: Think about how to handle the case when a mainboard has multiple
276  *       Super I/O chips soldered on.
277  * TODO: Can this code be simplified a bit?
278  *
279  * @param dev The device to use.
280  */
281 static void enable_dev(device_t dev)
282 {
283         int i, j, fn;
284         int tmp[MAX_LOGICAL_DEVICES];
285
286         if (first_time) {
287                 /* Read the device ID and revision of the Super I/O chip. */
288                 smsc_pnp_enter_conf_state(dev);
289                 superio_id = pnp_read_config(dev, DEVICE_ID_REG);
290                 superio_rev = pnp_read_config(dev, DEVICE_REV_REG);
291                 smsc_pnp_exit_conf_state(dev);
292
293                 /* TODO: Error handling? */
294
295                 printk_info("Found SMSC Super I/O (ID=0x%02x, rev=0x%02x)\n",
296                             superio_id, superio_rev);
297                 first_time = 0;
298         }
299
300         /* Find the correct Super I/O. */
301         for (i = 0; i < ARRAY_SIZE(logical_device_table); i++)
302                 if (logical_device_table[i].superio_id == superio_id)
303                         break;
304
305         /* If no Super I/O was found, return. */
306         if (i == ARRAY_SIZE(logical_device_table))
307                 return;
308
309         /* Temporarily save the LD_FOO values. */
310         for (j = 0; j < ARRAY_SIZE(pnp_dev_info); j++)
311                 tmp[j] = pnp_dev_info[j].function;
312
313         /* Replace the LD_FOO markers in pnp_dev_info[] with
314          * the real logical device IDs of this Super I/O chip.
315          */
316         for (j = 0; j < ARRAY_SIZE(pnp_dev_info); j++) {
317                 fn = pnp_dev_info[j].function;
318                 pnp_dev_info[j].function = logical_device_table[i].devs[fn];
319         }
320
321         /* Enable the specified devices (if present on the chip). */
322         pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info),
323                            &pnp_dev_info);
324
325         /* Restore LD_FOO values. */
326         for (j = 0; j < ARRAY_SIZE(pnp_dev_info); j++)
327                 pnp_dev_info[j].function = tmp[j];
328 }
329
330 struct chip_operations superio_smsc_smscsuperio_ops = {
331         CHIP_NAME("Various SMSC Super I/Os")
332         .enable_dev = enable_dev
333 };