remove trailing whitespace
[coreboot.git] / src / superio / smsc / lpc47n227 / superio.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005 Digital Design Corporation
5  * Copyright (C) 2008-2009 coresystems GmbH
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
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 /* RAM-based driver for SMSC LPC47N227 Super I/O chip. */
22
23 #include <arch/io.h>
24 #include <device/device.h>
25 #include <device/pnp.h>
26 #include <console/console.h>
27 #include <device/smbus.h>
28 #include <string.h>
29 #include <bitops.h>
30 #include <uart8250.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include "chip.h"
34 #include "lpc47n227.h"
35
36 /* Forward declarations. */
37 static void enable_dev(device_t dev);
38 void lpc47n227_pnp_set_resources(device_t dev);
39 void lpc47n227_pnp_enable_resources(device_t dev);
40 void lpc47n227_pnp_enable(device_t dev);
41 static void lpc47n227_init(device_t dev);
42 static void lpc47n227_pnp_set_resource(device_t dev, struct resource *resource);
43 void lpc47n227_pnp_set_iobase(device_t dev, u16 iobase);
44 void lpc47n227_pnp_set_drq(device_t dev, u8 drq);
45 void lpc47n227_pnp_set_irq(device_t dev, u8 irq);
46 void lpc47n227_pnp_set_enable(device_t dev, int enable);
47 static void pnp_enter_conf_state(device_t dev);
48 static void pnp_exit_conf_state(device_t dev);
49
50 struct chip_operations superio_smsc_lpc47n227_ops = {
51         CHIP_NAME("SMSC LPC47N227 Super I/O")
52         .enable_dev = enable_dev,
53 };
54
55 static struct device_operations ops = {
56         .read_resources   = pnp_read_resources,
57         .set_resources    = lpc47n227_pnp_set_resources,
58         .enable_resources = lpc47n227_pnp_enable_resources,
59         .enable           = lpc47n227_pnp_enable,
60         .init             = lpc47n227_init,
61 };
62
63 static struct pnp_info pnp_dev_info[] = {
64         { &ops, LPC47N227_PP, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, {0x07f8, 0}, },
65         { &ops, LPC47N227_SP1, PNP_IO0 | PNP_IRQ0, {0x07f8, 0}, },
66         { &ops, LPC47N227_SP2, PNP_IO0 | PNP_IRQ0, {0x07f8, 0}, },
67         { &ops, LPC47N227_KBDC, PNP_IO0 | PNP_IO1 | PNP_IRQ0, {0x07f8, 0}, {0x07f8, 4}, },
68 };
69
70 /**
71  * Create device structures and allocate resources to devices specified in the
72  * pnp_dev_info array (above).
73  *
74  * @param dev Pointer to structure describing a Super I/O device.
75  */
76 static void enable_dev(device_t dev)
77 {
78         pnp_enable_devices(dev, &pnp_ops,
79                            ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
80 }
81
82 /**
83  * Configure the specified Super I/O device with the resources (I/O space,
84  * etc.) that have been allocate for it.
85  *
86  * NOTE: Cannot use pnp_set_resources() here because it assumes chip
87  * support for logical devices, which the LPC47N227 doesn't have.
88  *
89  * @param dev Pointer to structure describing a Super I/O device.
90  */
91 void lpc47n227_pnp_set_resources(device_t dev)
92 {
93         struct resource *res;
94
95         pnp_enter_conf_state(dev);
96         for (res = dev->resource_list; res; res = res->next)
97                 lpc47n227_pnp_set_resource(dev, res);
98         pnp_exit_conf_state(dev);
99 }
100
101 /*
102  * NOTE: Cannot use pnp_enable_resources() here because it assumes chip
103  * support for logical devices, which the LPC47N227 doesn't have.
104  */
105 void lpc47n227_pnp_enable_resources(device_t dev)
106 {
107         pnp_enter_conf_state(dev);
108         lpc47n227_pnp_set_enable(dev, 1);
109         pnp_exit_conf_state(dev);
110 }
111
112 /*
113  * NOTE: Cannot use pnp_set_enable() here because it assumes chip
114  * support for logical devices, which the LPC47N227 doesn't have.
115  */
116 void lpc47n227_pnp_enable(device_t dev)
117 {
118         pnp_enter_conf_state(dev);
119         lpc47n227_pnp_set_enable(dev, !!dev->enabled);
120         pnp_exit_conf_state(dev);
121 }
122
123 /**
124  * Initialize the specified Super I/O device.
125  *
126  * Devices other than COM ports and keyboard controller are ignored.
127  * For COM ports, we configure the baud rate.
128  *
129  * @param dev Pointer to structure describing a Super I/O device.
130  */
131 static void lpc47n227_init(device_t dev)
132 {
133         struct superio_smsc_lpc47n227_config *conf = dev->chip_info;
134
135         if (!dev->enabled)
136                 return;
137
138         switch (dev->path.pnp.device) {
139         case LPC47N227_KBDC:
140                 printk(BIOS_DEBUG, "LPC47N227: Initializing keyboard.\n");
141                 pc_keyboard_init(&conf->keyboard);
142                 break;
143         }
144 }
145
146 static void lpc47n227_pnp_set_resource(device_t dev, struct resource *resource)
147 {
148         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
149                 printk(BIOS_ERR, "ERROR: %s %02lx not allocated\n",
150                        dev_path(dev), resource->index);
151                 return;
152         }
153
154         /* Now store the resource. */
155         /*
156          * NOTE: Cannot use pnp_set_XXX() here because they assume chip
157          * support for logical devices, which the LPC47N227 doesn't have.
158          */
159         if (resource->flags & IORESOURCE_IO) {
160                 lpc47n227_pnp_set_iobase(dev, resource->base);
161         } else if (resource->flags & IORESOURCE_DRQ) {
162                 lpc47n227_pnp_set_drq(dev, resource->base);
163         } else if (resource->flags & IORESOURCE_IRQ) {
164                 lpc47n227_pnp_set_irq(dev, resource->base);
165         } else {
166                 printk(BIOS_ERR, "ERROR: %s %02lx unknown resource type\n",
167                        dev_path(dev), resource->index);
168                 return;
169         }
170         resource->flags |= IORESOURCE_STORED;
171
172         report_resource_stored(dev, resource, "");
173 }
174
175 void lpc47n227_pnp_set_iobase(device_t dev, u16 iobase)
176 {
177         ASSERT(!(iobase & 0x3));
178
179         switch (dev->path.pnp.device) {
180         case LPC47N227_PP:
181                 pnp_write_config(dev, 0x23, (iobase >> 2) & 0xff);
182                 break;
183         case LPC47N227_SP1:
184                 pnp_write_config(dev, 0x24, (iobase >> 2) & 0xff);
185                 break;
186         case LPC47N227_SP2:
187                 pnp_write_config(dev, 0x25, (iobase >> 2) & 0xff);
188                 break;
189         case LPC47N227_KBDC:
190                 break;
191         default:
192                 BUG();
193                 break;
194         }
195 }
196
197 void lpc47n227_pnp_set_drq(device_t dev, u8 drq)
198 {
199         const u8 PP_DMA_MASK = 0x0F;
200         const u8 PP_DMA_SELECTION_REGISTER = 0x26;
201         u8 current_config, new_config;
202
203         if (dev->path.pnp.device == LPC47N227_PP) {
204                 current_config = pnp_read_config(dev,
205                                                  PP_DMA_SELECTION_REGISTER);
206                 ASSERT(!(drq & ~PP_DMA_MASK));  // DRQ out of range??
207                 new_config = (current_config & ~PP_DMA_MASK) | drq;
208                 pnp_write_config(dev, PP_DMA_SELECTION_REGISTER, new_config);
209         } else {
210                 BUG();
211         }
212 }
213
214 void lpc47n227_pnp_set_irq(device_t dev, u8 irq)
215 {
216         u8 irq_config_register = 0, irq_config_mask = 0;
217         u8 current_config, new_config;
218
219         switch (dev->path.pnp.device) {
220         case LPC47N227_PP:
221                 irq_config_register = 0x27;
222                 irq_config_mask = 0x0F;
223                 break;
224         case LPC47N227_SP1:
225                 irq_config_register = 0x28;
226                 irq_config_mask = 0xF0;
227                 irq <<= 4;
228                 break;
229         case LPC47N227_SP2:
230                 irq_config_register = 0x28;
231                 irq_config_mask = 0x0F;
232                 break;
233         case LPC47N227_KBDC:
234                 break;
235         default:
236                 BUG();
237                 return;
238         }
239
240         current_config = pnp_read_config(dev, irq_config_register);
241         new_config = (current_config & ~irq_config_mask) | irq;
242         pnp_write_config(dev, irq_config_register, new_config);
243 }
244
245 void lpc47n227_pnp_set_enable(device_t dev, int enable)
246 {
247         u8 power_register = 0, power_mask = 0, current_power, new_power;
248
249         switch (dev->path.pnp.device) {
250         case LPC47N227_PP:
251                 power_register = 0x01;
252                 power_mask = 0x04;
253                 break;
254         case LPC47N227_SP1:
255                 power_register = 0x02;
256                 power_mask = 0x08;
257                 break;
258         case LPC47N227_SP2:
259                 power_register = 0x02;
260                 power_mask = 0x80;
261                 break;
262         case LPC47N227_KBDC:
263                 break;
264         default:
265                 BUG();
266                 return;
267         }
268
269         current_power = pnp_read_config(dev, power_register);
270         new_power = current_power & ~power_mask; /* Disable by default. */
271         if (enable) {
272                 struct resource *ioport_resource;
273                 ioport_resource = find_resource(dev, PNP_IDX_IO0);
274                 lpc47n227_pnp_set_iobase(dev, ioport_resource->base);
275                 new_power |= power_mask; /* Enable. */
276         } else {
277                 lpc47n227_pnp_set_iobase(dev, 0);
278         }
279         pnp_write_config(dev, power_register, new_power);
280 }
281
282 static void pnp_enter_conf_state(device_t dev)
283 {
284         outb(0x55, dev->path.pnp.port);
285 }
286
287 static void pnp_exit_conf_state(device_t dev)
288 {
289         outb(0xaa, dev->path.pnp.port);
290 }