318f0f35414467306b583a9417123aa88a8b9341
[coreboot.git] / src / superio / smsc / lpc47n227 / lpc47n227_early_serial.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 /* Pre-RAM driver for SMSC LPC47N227 Super I/O chip. */
22
23 #include <arch/romcc_io.h>
24 #include "lpc47n227.h"
25
26 /**
27  * Enable access to the LPC47N227's configuration registers.
28  *
29  * @param dev High 8 bits = Super I/O port.
30  */
31 static inline void pnp_enter_conf_state(device_t dev)
32 {
33         unsigned port = dev >> 8;
34         outb(0x55, port);
35 }
36
37 /**
38  * Disable access to the LPC47N227's configuration registers.
39  *
40  * @param dev High 8 bits = Super I/O port.
41  */
42 static void pnp_exit_conf_state(device_t dev)
43 {
44         unsigned port = dev >> 8;
45         outb(0xaa, port);
46 }
47
48 /**
49  * Program the base I/O port for the specified logical device.
50  *
51  * @param dev High 8 bits = Super I/O port, low 8 bits = logical device number.
52  * @param iobase Base I/O port for the logical device.
53  */
54 void lpc47n227_pnp_set_iobase(device_t dev, unsigned iobase)
55 {
56         // LPC47N227 requires base ports to be a multiple of 4
57         ASSERT(!(iobase & 0x3));
58
59         switch (dev & 0xFF) {
60         case LPC47N227_PP:
61                 pnp_write_config(dev, 0x23, (iobase >> 2) & 0xff);
62                 break;
63
64         case LPC47N227_SP1:
65                 pnp_write_config(dev, 0x24, (iobase >> 2) & 0xff);
66                 break;
67
68         case LPC47N227_SP2:
69                 pnp_write_config(dev, 0x25, (iobase >> 2) & 0xff);
70                 break;
71
72         default:
73                 break;
74         }
75 }
76
77 /**
78  * Enable or disable the specified logical device.
79  *
80  * Technically, a full disable requires setting the device's base I/O port
81  * below 0x100. We don't do that here, because we don't have access to a data
82  * structure that specifies what the 'real' base port is (when asked to enable
83  * the device). Also the function is used only to disable the device while its
84  * true base port is programmed (see lpc47n227_enable_serial() below).
85  *
86  * @param dev High 8 bits = Super I/O port, low 8 bits = logical device number.
87  * @param enable 0 to disable, anythig else to enable.
88  */
89 void lpc47n227_pnp_set_enable(device_t dev, int enable)
90 {
91         uint8_t power_register = 0;
92         uint8_t power_mask = 0;
93         uint8_t current_power;
94         uint8_t new_power;
95
96         switch (dev & 0xFF) {
97         case LPC47N227_PP:
98                 power_register = 0x01;
99                 power_mask = 0x04;
100                 break;
101
102         case LPC47N227_SP1:
103                 power_register = 0x02;
104                 power_mask = 0x08;
105                 break;
106
107         case LPC47N227_SP2:
108                 power_register = 0x02;
109                 power_mask = 0x80;
110                 break;
111
112         default:
113                 return;
114         }
115
116         current_power = pnp_read_config(dev, power_register);
117         new_power = current_power & ~power_mask;        // disable by default
118
119         if (enable)
120                 new_power |= power_mask;        // Enable
121
122         pnp_write_config(dev, power_register, new_power);
123 }
124
125 /**
126  * Configure the base I/O port of the specified serial device and enable the
127  * serial device.
128  *
129  * @param dev High 8 bits = Super I/O port, low 8 bits = logical device number.
130  * @param iobase Processor I/O port address to assign to this serial device.
131  */
132 static void lpc47n227_enable_serial(device_t dev, unsigned iobase)
133 {
134         /*
135          * NOTE: Cannot use pnp_set_XXX() here because they assume chip
136          * support for logical devices, which the LPC47N227 doesn't have.
137          */
138         pnp_enter_conf_state(dev);
139         lpc47n227_pnp_set_enable(dev, 0);
140         lpc47n227_pnp_set_iobase(dev, iobase);
141         lpc47n227_pnp_set_enable(dev, 1);
142         pnp_exit_conf_state(dev);
143 }