C and other Super I/O cosmetic fixes.
[coreboot.git] / src / superio / smsc / lpc47n217 / superio.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2000 AG Electronics Ltd.
5  * Copyright (C) 2003-2004 Linux Networx
6  * Copyright (C) 2004 Tyan
7  * Copyright (C) 2005 Digital Design Corporation
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 /* RAM-based driver for SMSC LPC47N217 Super I/O chip. */
25
26 #include <arch/io.h>
27 #include <device/device.h>
28 #include <device/pnp.h>
29 #include <console/console.h>
30 #include <device/smbus.h>
31 #include <string.h>
32 #include <bitops.h>
33 #include <uart8250.h>
34 #include <assert.h>
35 #include <stdlib.h>
36 #include "chip.h"
37 #include "lpc47n217.h"
38
39 /* Forward declarations */
40 static void enable_dev(device_t dev);
41 static void lpc47n217_pnp_set_resources(device_t dev);
42 static void lpc47n217_pnp_enable_resources(device_t dev);
43 static void lpc47n217_pnp_enable(device_t dev);
44 static void lpc47n217_init(device_t dev);
45 static void lpc47n217_pnp_set_resource(device_t dev, struct resource *resource);
46 static void lpc47n217_pnp_set_iobase(device_t dev, u16 iobase);
47 static void lpc47n217_pnp_set_drq(device_t dev, u8 drq);
48 static void lpc47n217_pnp_set_irq(device_t dev, u8 irq);
49 static void lpc47n217_pnp_set_enable(device_t dev, int enable);
50 static void pnp_enter_conf_state(device_t dev);
51 static void pnp_exit_conf_state(device_t dev);
52
53 struct chip_operations superio_smsc_lpc47n217_ops = {
54         CHIP_NAME("SMSC LPC47N217 Super I/O")
55         .enable_dev = enable_dev,
56 };
57
58 static struct device_operations ops = {
59         .read_resources   = pnp_read_resources,
60         .set_resources    = lpc47n217_pnp_set_resources,
61         .enable_resources = lpc47n217_pnp_enable_resources,
62         .enable           = lpc47n217_pnp_enable,
63         .init             = lpc47n217_init,
64 };
65
66 static struct pnp_info pnp_dev_info[] = {
67         { &ops, LPC47N217_PP,   PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, {0x07f8, 0}, },
68         { &ops, LPC47N217_SP1,  PNP_IO0 | PNP_IRQ0, {0x07f8, 0}, },
69         { &ops, LPC47N217_SP2,  PNP_IO0 | PNP_IRQ0, {0x07f8, 0}, }
70 };
71
72 /**
73  * Create device structures and allocate resources to devices specified in the
74  * pnp_dev_info array (above).
75  *
76  * @param dev Pointer to structure describing a Super I/O device.
77  */
78 static void enable_dev(device_t dev)
79 {
80         pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info),
81                            pnp_dev_info);
82 }
83
84 /**
85  * Configure the specified Super I/O device with the resources (I/O space,
86  * etc.) that have been allocate for it.
87  *
88  * NOTE: Cannot use pnp_set_resources() here because it assumes chip
89  * support for logical devices, which the LPC47N217 doesn't have.
90  *
91  * @param dev Pointer to structure describing a Super I/O device.
92  */
93 static void lpc47n217_pnp_set_resources(device_t dev)
94 {
95         struct resource *res;
96
97         pnp_enter_conf_state(dev);
98         for (res = dev->resource_list; res; res = res->next)
99                 lpc47n217_pnp_set_resource(dev, res);
100         /* dump_pnp_device(dev); */
101         pnp_exit_conf_state(dev);
102 }
103
104 /*
105  * NOTE: Cannot use pnp_enable_resources() here because it assumes chip
106  * support for logical devices, which the LPC47N217 doesn't have.
107  */
108 static void lpc47n217_pnp_enable_resources(device_t dev)
109 {
110         pnp_enter_conf_state(dev);
111         lpc47n217_pnp_set_enable(dev, 1);
112         pnp_exit_conf_state(dev);
113 }
114
115 /*
116  * NOTE: Cannot use pnp_set_enable() here because it assumes chip
117  * support for logical devices, which the LPC47N217 doesn't have.
118  */
119 static void lpc47n217_pnp_enable(device_t dev)
120 {
121         pnp_enter_conf_state(dev);
122         lpc47n217_pnp_set_enable(dev, (dev->enabled) ? 1 : 0);
123         pnp_exit_conf_state(dev);
124 }
125
126 /**
127  * Initialize the specified Super I/O device.
128  *
129  * Devices other than COM ports are ignored. For COM ports, we configure the
130  * baud rate.
131  *
132  * @param dev Pointer to structure describing a Super I/O device.
133  */
134 static void lpc47n217_init(device_t dev)
135 {
136         struct superio_smsc_lpc47n217_config* conf = dev->chip_info;
137         struct resource *res0;
138
139         if (!dev->enabled)
140                 return;
141
142         switch(dev->path.pnp.device) {
143         case LPC47N217_SP1:
144                 res0 = find_resource(dev, PNP_IDX_IO0);
145                 init_uart8250(res0->base, &conf->com1);
146                 break;
147         case LPC47N217_SP2:
148                 res0 = find_resource(dev, PNP_IDX_IO0);
149                 init_uart8250(res0->base, &conf->com2);
150                 break;
151         }
152 }
153
154 static void lpc47n217_pnp_set_resource(device_t dev, struct resource *resource)
155 {
156         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
157                 printk(BIOS_ERR, "ERROR: %s %02x not allocated\n",
158                        dev_path(dev), resource->index);
159                 return;
160         }
161
162         /* Now store the resource. */
163
164         /*
165          * NOTE: Cannot use pnp_set_XXX() here because they assume chip
166          * support for logical devices, which the LPC47N217 doesn't have.
167          */
168         if (resource->flags & IORESOURCE_IO) {
169                 lpc47n217_pnp_set_iobase(dev, resource->base);
170         } else if (resource->flags & IORESOURCE_DRQ) {
171                 lpc47n217_pnp_set_drq(dev, resource->base);
172         } else if (resource->flags & IORESOURCE_IRQ) {
173                 lpc47n217_pnp_set_irq(dev, resource->base);
174         } else {
175                 printk(BIOS_ERR, "ERROR: %s %02x unknown resource type\n",
176                        dev_path(dev), resource->index);
177                 return;
178         }
179         resource->flags |= IORESOURCE_STORED;
180
181         report_resource_stored(dev, resource, "");
182 }
183
184 static void lpc47n217_pnp_set_iobase(device_t dev, u16 iobase)
185 {
186         ASSERT(!(iobase & 0x3));
187
188         switch(dev->path.pnp.device) {
189         case LPC47N217_PP:
190                 pnp_write_config(dev, 0x23, (iobase >> 2) & 0xff);
191                 break;
192         case LPC47N217_SP1:
193                 pnp_write_config(dev, 0x24, (iobase >> 2) & 0xff);
194                 break;
195         case LPC47N217_SP2:
196                 pnp_write_config(dev, 0x25, (iobase >> 2) & 0xff);
197                 break;
198         default:
199                 BUG();
200                 break;
201         }
202 }
203
204 static void lpc47n217_pnp_set_drq(device_t dev, u8 drq)
205 {
206         const u8 PP_DMA_MASK = 0x0F;
207         const u8 PP_DMA_SELECTION_REGISTER = 0x26;
208         u8 current_config, new_config;
209
210         if (dev->path.pnp.device == LPC47N217_PP) {
211                 current_config = pnp_read_config(dev,
212                                                  PP_DMA_SELECTION_REGISTER);
213                 ASSERT(!(drq & ~PP_DMA_MASK)); /* DRQ out of range? */
214                 new_config = (current_config & ~PP_DMA_MASK) | drq;
215                 pnp_write_config(dev, PP_DMA_SELECTION_REGISTER, new_config);
216         } else {
217                 BUG();
218         }
219 }
220
221 static void lpc47n217_pnp_set_irq(device_t dev, u8 irq)
222 {
223         u8 irq_config_register = 0, irq_config_mask = 0;
224         u8 current_config, new_config;
225
226         switch(dev->path.pnp.device) {
227         case LPC47N217_PP:
228                 irq_config_register = 0x27;
229                 irq_config_mask = 0x0F;
230                 break;
231         case LPC47N217_SP1:
232                 irq_config_register = 0x28;
233                 irq_config_mask = 0xF0;
234                 irq <<= 4;
235                 break;
236         case LPC47N217_SP2:
237                 irq_config_register = 0x28;
238                 irq_config_mask = 0x0F;
239                 break;
240         default:
241                 BUG();
242                 return;
243         }
244
245         ASSERT(!(irq & ~irq_config_mask)); /* IRQ out of range? */
246
247         current_config = pnp_read_config(dev, irq_config_register);
248         new_config = (current_config & ~irq_config_mask) | irq;
249         pnp_write_config(dev, irq_config_register, new_config);
250 }
251
252 static void lpc47n217_pnp_set_enable(device_t dev, int enable)
253 {
254         u8 power_register = 0, power_mask = 0, current_power, new_power;
255
256         switch(dev->path.pnp.device) {
257         case LPC47N217_PP:
258                 power_register = 0x01;
259                 power_mask = 0x04;
260                 break;
261         case LPC47N217_SP1:
262                 power_register = 0x02;
263                 power_mask = 0x08;
264                 break;
265         case LPC47N217_SP2:
266                 power_register = 0x02;
267                 power_mask = 0x80;
268                 break;
269         default:
270                 BUG();
271                 return;
272         }
273
274         current_power = pnp_read_config(dev, power_register);
275         new_power = current_power & ~power_mask; /* Disable by default. */
276         if (enable) {
277                 struct resource* ioport_resource;
278                 ioport_resource = find_resource(dev, PNP_IDX_IO0);
279                 lpc47n217_pnp_set_iobase(dev, ioport_resource->base);
280                 new_power |= power_mask; /* Enable. */
281         } else {
282                 lpc47n217_pnp_set_iobase(dev, 0);
283         }
284         pnp_write_config(dev, power_register, new_power);
285 }
286
287 static void pnp_enter_conf_state(device_t dev)
288 {
289         outb(0x55, dev->path.pnp.port);
290 }
291
292 static void pnp_exit_conf_state(device_t dev)
293 {
294         outb(0xaa, dev->path.pnp.port);
295 }
296
297 #if 0
298 /**
299  * Print the values of all of the LPC47N217's configuration registers.
300  *
301  * NOTE: The LPC47N217 must be in config mode when this function is called.
302  *
303  * @param dev Pointer to structure describing a Super I/O device.
304  */
305 static void dump_pnp_device(device_t dev)
306 {
307         int i;
308         print_debug("\n");
309
310         for (i = 0; i <= LPC47N217_MAX_CONFIG_REGISTER; i++) {
311                 u8 register_value;
312
313                 if ((i & 0x0f) == 0) {
314                         print_debug_hex8(i);
315                         print_debug_char(':');
316                 }
317
318                 /*
319                  * Skip over 'register' that would cause exit from
320                  * configuration mode.
321                  */
322                 if (i == 0xaa)
323                         register_value = 0xaa;
324                 else
325                         register_value = pnp_read_config(dev, i);
326
327                 print_debug_char(' ');
328                 print_debug_hex8(register_value);
329                 if ((i & 0x0f) == 0x0f)
330                         print_debug("\n");
331         }
332
333         print_debug("\n");
334 }
335 #endif