49e11ce039dfb63cda6f18f042d327bcd340f38a
[coreboot.git] / src / superio / via / vt1211 / vt1211.c
1 /*
2  * (C) Copyright 2004 Nick Barker <nick.barker9@btinternet.com>
3  *
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18  * MA 02110-1301 USA
19  */
20
21  /* vt1211 routines and defines*/
22
23
24 #include <arch/io.h>
25 #include <console/console.h>
26 #include <device/device.h>
27 #include <device/pnp.h>
28 #include <uart8250.h>
29
30 #include "vt1211.h"
31 #include "chip.h"
32
33
34 static unsigned char vt1211hwmonitorinits[]={
35  0x10,0x3, 0x11,0x10, 0x12,0xd, 0x13,0x7f,
36  0x14,0x21, 0x15,0x81, 0x16,0xbd, 0x17,0x8a,
37  0x18,0x0, 0x19,0x0, 0x1a,0x0, 0x1b,0x0,
38  0x1d,0xff, 0x1e,0x0, 0x1f,0x73, 0x20,0x67,
39  0x21,0xc1, 0x22,0xca, 0x23,0x74, 0x24,0xc2,
40  0x25,0xc7, 0x26,0xc9, 0x27,0x7f, 0x29,0x0,
41  0x2a,0x0, 0x2b,0xff, 0x2c,0x0, 0x2d,0xff,
42  0x2e,0x0, 0x2f,0xff, 0x30,0x0, 0x31,0xff,
43  0x32,0x0, 0x33,0xff, 0x34,0x0, 0x39,0xff,
44  0x3a,0x0, 0x3b,0xff, 0x3c,0xff, 0x3d,0xff,
45  0x3e,0x0, 0x3f,0xb0, 0x43,0xff, 0x44,0xff,
46  0x46,0xff, 0x47,0x50, 0x4a,0x3, 0x4b,0xc0,
47  0x4c,0x0, 0x4d,0x0, 0x4e,0xf, 0x5d,0x77,
48  0x5c,0x0, 0x5f,0x33, 0x40,0x1};
49
50 static void pnp_enter_ext_func_mode(device_t dev) 
51 {
52         outb(0x87, dev->path.u.pnp.port);
53         outb(0x87, dev->path.u.pnp.port);
54 }
55
56 static void pnp_exit_ext_func_mode(device_t dev) 
57 {
58         outb(0xaa, dev->path.u.pnp.port);
59 }
60
61 static void vt1211_set_iobase(device_t dev, unsigned index, unsigned iobase)
62 {
63
64         switch (dev->path.u.pnp.device) {
65                 case VT1211_FDC:
66                 case VT1211_PP:
67                 case VT1211_SP1:
68                 case VT1211_SP2:
69                         pnp_write_config(dev, index + 0, (iobase >> 2) & 0xff);
70                         break;
71                 case VT1211_HWM:
72                 default:
73                         pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
74                         pnp_write_config(dev, index + 1, iobase & 0xff);
75                         break;
76         }
77         
78 }
79
80 static void init_hwm(unsigned long base)
81 {
82         int i;
83
84         // initialize vt1211 hardware monitor registers, which are at 0xECXX
85         for(i = 0; i < sizeof(vt1211hwmonitorinits); i += 2) {
86                         outb(vt1211hwmonitorinits[i + 1],
87                                         base + vt1211hwmonitorinits[i]);
88         }
89 }
90
91 static void vt1211_init(struct device *dev)
92 {
93         struct superio_via_vt1211_config *conf = dev->chip_info;
94         struct resource *res0;
95
96         if (!dev->enabled) {
97                 return;
98         }
99
100         switch (dev->path.u.pnp.device) {
101         case VT1211_FDC:
102         case VT1211_PP:
103                 break;
104         case VT1211_SP1:
105                 res0 = find_resource(dev, PNP_IDX_IO0);
106                 init_uart8250(res0->base, &conf->com1);
107                 break;
108         case VT1211_SP2:
109                 res0 = find_resource(dev, PNP_IDX_IO0);
110                 init_uart8250(res0->base, &conf->com2);
111                 break;
112         case VT1211_HWM:
113                 res0 = find_resource(dev, PNP_IDX_IO0);
114                 init_hwm(res0->base);
115                 break;
116         default:
117                 printk_info("vt1211 asked to initialise unknown device!\n");
118         }
119         
120
121 }
122
123 void vt1211_pnp_enable_resources(device_t dev)
124 {
125         printk_debug("%s - enabling\n",dev_path(dev));
126         pnp_enter_ext_func_mode(dev);
127         pnp_enable_resources(dev);
128         pnp_exit_ext_func_mode(dev);
129 }
130
131 void vt1211_pnp_set_resources(struct device *dev)
132 {
133         int i;
134         struct resource *resource;
135
136 #if CONFIG_CONSOLE_SERIAL8250 == 1
137         if( dev->path.u.pnp.device == 2 ){
138                 for( i = 0 ; i < dev->resources; i++){
139                         resource = &dev->resource[i];
140                         resource->flags |= IORESOURCE_STORED;
141                         report_resource_stored(dev, resource, "");      
142                 }
143                 return;
144         }
145 #endif
146         pnp_enter_ext_func_mode(dev);
147         /* Select the device */
148         pnp_set_logical_device(dev);
149
150         /* Paranoia says I should disable the device here... */
151         for(i = 0; i < dev->resources; i++) {
152                 resource = &dev->resource[i];
153                 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
154                         printk_err("ERROR: %s %02x %s size: 0x%010Lx not assigned\n",
155                                 dev_path(dev), dev->resource->index,
156                                 resource_type(resource),
157                                 resource->size);
158                         continue;
159                 }
160
161                 /* Now store the resource */
162                 if (resource->flags & IORESOURCE_IO) {
163                         vt1211_set_iobase(dev, resource->index, resource->base);
164                 }
165                 else if (resource->flags & IORESOURCE_DRQ) {
166                         pnp_set_drq(dev, resource->index, resource->base);
167                 }
168                 else if (resource->flags  & IORESOURCE_IRQ) {
169                         pnp_set_irq(dev, resource->index, resource->base);
170                 }
171                 else {
172                         printk_err("ERROR: %s %02x unknown resource type\n",
173                                 dev_path(dev), resource->index);
174                         return;
175                 }
176                 resource->flags |= IORESOURCE_STORED;
177
178                 report_resource_stored(dev, resource, "");      
179         }
180
181         pnp_exit_ext_func_mode(dev);
182 }
183
184 void vt1211_pnp_enable(device_t dev)
185 {
186         if (!dev->enabled) {
187                 pnp_enter_ext_func_mode(dev);
188                 pnp_set_logical_device(dev);
189                 pnp_set_enable(dev, 0);
190                 pnp_exit_ext_func_mode(dev);
191         }
192 }
193
194 struct device_operations ops = {
195         .read_resources   = pnp_read_resources,
196         .set_resources    = vt1211_pnp_set_resources,
197         .enable_resources = vt1211_pnp_enable_resources,
198         .enable           = vt1211_pnp_enable,
199         .init             = vt1211_init,
200 };
201
202 static struct pnp_info pnp_dev_info[] = {
203         { &ops, VT1211_FDC, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, { 0x07f8, 0}, },
204         { &ops, VT1211_PP,  PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, { 0x07f8, 0}, },
205         { &ops, VT1211_SP1, PNP_IO0 | PNP_IRQ0,            { 0x07f8, 0}, },
206         { &ops, VT1211_SP2, PNP_IO0 | PNP_IRQ0,            { 0x07f8, 0}, },
207         { &ops, VT1211_HWM, PNP_IO0 , { 0xff00, 0 }, },
208 };
209
210 static void enable_dev(struct device *dev)
211 {
212         printk_debug("vt1211 enabling PNP devices.\n");
213         pnp_enable_devices(dev,
214                         &ops,
215                         sizeof(pnp_dev_info) / sizeof(pnp_dev_info[0]),
216                         pnp_dev_info);
217 }
218
219 struct chip_operations superio_via_vt1211_ops = {
220         CHIP_NAME("VIA VT1211 Super I/O")
221         .enable_dev = enable_dev,
222 };