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