almost there.
[coreboot.git] / src / superio / winbond / w83627thf / superio.c
1 /* This code is distributed without warranty under the GPL v2 (see COPYING) */
2
3 #include <arch/io.h>
4 #include <device/device.h>
5 #include <device/chip.h>
6 #include <console/console.h>
7 #include <string.h>
8 #include <bitops.h>
9 #include "chip.h"
10
11 void pnp_output(char address, char data)
12 {
13         outb(address, PNP_INDEX_REG);
14         outb(data, PNP_DATA_REG);
15 }
16
17 static void sio_enable(struct chip *chip, enum chip_pass pass)
18 {
19
20         struct superio_NSC_pc87360_config *conf = (struct superio_NSC_pc87360_config *)chip->chip_info;
21
22         switch (pass) {
23         case CONF_PASS_PRE_CONSOLE:
24                 /* Enable Super IO Chip */
25                 pnp_output(0x07, 6); /* LD 6 = UART1 */
26                 pnp_output(0x30, 0); /* Dectivate */
27                 pnp_output(0x60, conf->port >> 8); /* IO Base */
28                 pnp_output(0x61, conf->port & 0xFF); /* IO Base */
29                 pnp_output(0x30, 1); /* Activate */
30                 break;
31         default:
32                 /* nothing yet */
33                 break;
34         }
35 }
36
37 static void pnp_write_config(device_t dev, unsigned char value, unsigned char reg)
38 {
39         outb(reg, dev->path.u.pnp.port);
40         outb(value, dev->path.u.pnp.port + 1);
41 }
42
43 static unsigned char pnp_read_config(device_t dev, unsigned char reg)
44 {
45         outb(reg, dev->path.u.pnp.port);
46         return inb(dev->path.u.pnp.port + 1);
47 }
48
49 static void pnp_set_logical_device(device_t dev)
50 {
51         pnp_write_config(dev, dev->path.u.pnp.device, 0x07);
52 }
53
54 static void pnp_set_enable(device_t dev, int enable)
55 {
56         pnp_write_config(dev, enable?0x1:0x0, 0x30);
57 }
58
59 static int pnp_read_enable(device_t dev)
60 {
61         return !!pnp_read_config(dev, 0x30);
62 }
63
64 #define FLOPPY_DEVICE   0
65 #define PARALLEL_DEVICE 1
66 #define COM2_DEVICE     2
67 #define COM1_DEVICE     3
68 #define SWC_DEVICE      4
69 #define MOUSE_DEVICE    5
70 #define KBC_DEVICE      6
71 #define GPIO_DEVICE     7
72 #define ACB_DEVICE      8
73 #define FSCM_DEVICE     9
74 #define WDT_DEVICE     10
75
76 struct io_info {
77         unsigned mask, set;
78 };
79 struct pnp_info {
80         unsigned flags;
81 #define PNP_IO0  0x01
82 #define PNP_IO1  0x02
83 #define PNP_IRQ0 0x04
84 #define PNP_IRQ1 0x08
85 #define PNP_DRQ0 0x10
86 #define PNP_DRQ1 0x20
87         struct io_info io0, io1;
88 };
89
90 static struct pnp_info pnp_dev_info[] = {
91         [ 0] = { PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, { 0x07fa, 0}, },
92         [ 1] = { PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, { 0x04f8, 0}, },
93         [ 2] = { PNP_IO0 | PNP_IRQ0 | PNP_DRQ0 | PNP_DRQ1, { 0x7f8, 0 }, },
94         [ 3] = { PNP_IO0 | PNP_IRQ0, { 0x7f8, 0 }, },
95         [ 4] = { PNP_IO0 | PNP_IRQ0, { 0xfff0, 0 }, },
96         [ 5] = { PNP_IRQ0 },
97         [ 6] = { PNP_IO0 | PNP_IO1 | PNP_IRQ0, { 0x7f8, 0 }, { 0x7f8, 0x4}, },
98         [ 7] = { PNP_IO0 | PNP_IRQ0, { 0xfff8, 0 } },
99         [ 8] = { PNP_IO0 | PNP_IRQ0, { 0xfff8, 0 } },
100         [ 9] = { PNP_IO0 | PNP_IRQ0, { 0xfff8, 0 } },
101         [10] = { PNP_IO0 | PNP_IRQ0, { 0xfffc, 0 } },
102 };
103
104 static struct resource *get_resource(device_t dev, unsigned index)
105 {
106         struct resource *resource;
107         int i;
108         resource = 0;
109         for(i = 0; i < dev->resources; i++) {
110                 resource = &dev->resource[i];
111                 if (resource->index == index) {
112                         break;
113                 }
114         }
115         if (!resource || (resource->index != index)) {
116                 resource = &dev->resource[dev->resources];
117                 memset(resource, 0, sizeof(*resource));
118                 dev->resources++;
119         }
120         /* Initialize the resource values */
121         if (!(resource->flags & IORESOURCE_FIXED)) {
122                 resource->flags = 0;
123                 resource->base = 0;
124         }
125         resource->size  = 0;
126         resource->limit = 0;
127         resource->flags = 0;
128         resource->index = index;
129         resource->align = 0;
130         resource->gran  = 0;
131
132         return resource;
133 }
134
135 static void pnp_read_ioresource(device_t dev, unsigned index, struct io_info *info)
136 {
137         struct resource *resource;
138         uint32_t size;
139         resource = get_resource(dev, index);
140         
141         /* Initilize the resource */
142         resource->limit = 0xffff;
143         resource->flags |= IORESOURCE_IO;
144         
145         /* Set the resource size and alignment */
146         size = (0xffff & info->mask);
147         resource->size  = (~(size | 0xfffff800) + 1);
148         resource->align = log2(resource->size);
149         resource->gran  = resource->align;
150 }
151
152
153 static void pnp_read_resources(device_t dev)
154 {
155         struct pnp_info *info;
156         struct resource *resource;
157         pnp_set_logical_device(dev);
158
159         info = &pnp_dev_info[dev->path.u.pnp.device];
160
161         if (info->flags & PNP_IO0) {
162                 pnp_read_ioresource(dev, 0x60, &info->io0);
163         }
164         if (info->flags & PNP_IO1) {
165                 pnp_read_ioresource(dev, 0x62, &info->io1);
166         }
167         if (info->flags & PNP_IRQ0) {
168                 resource = get_resource(dev, 0x70);
169                 resource->size = 1;
170                 resource->flags |= IORESOURCE_IRQ;
171         }
172         if (info->flags & PNP_IRQ1) {
173                 resource = get_resource(dev, 0x72);
174                 resource->size = 1;
175                 resource->flags |= IORESOURCE_IRQ;
176         }
177         if (info->flags & PNP_DRQ0) {
178                 resource = get_resource(dev, 0x74);
179                 resource->size = 1;
180                 resource->flags |= IORESOURCE_DRQ;
181         }
182         if (info->flags & PNP_DRQ1) {
183                 resource = get_resource(dev, 0x75);
184                 resource->size = 1;
185                 resource->flags |= IORESOURCE_DRQ;
186         }
187 }
188
189 static void pnp_set_iobase(device_t dev, unsigned iobase, unsigned index)
190 {
191         /* Index == 0x60 or 0x62 */
192         pnp_write_config(dev, (iobase >> 8) & 0xff, index);
193         pnp_write_config(dev, iobase & 0xff, index + 1);
194 }
195
196 static void pnp_set_irq(device_t dev, unsigned irq, unsigned index)
197 {
198         /* Index == 0x70 or 0x72 */
199         pnp_write_config(dev, irq, index);
200 }
201
202 static void pnp_set_drq(device_t dev, unsigned drq, unsigned index)
203 {
204         /* Index == 0x74 */
205         pnp_write_config(dev, drq & 0xff, index);
206 }
207
208
209 static void pnp_set_resource(device_t dev, struct resource *resource)
210 {
211         if (!(resource->flags & IORESOURCE_SET)) {
212 #if 1
213                 printk_err("ERROR: %s %02x not allocated\n",
214                         dev_path(dev), resource->index);
215 #endif
216                 return;
217         }
218         if (resource->flags & IORESOURCE_IO) {
219                 pnp_set_iobase(dev, resource->base, resource->index);
220         }
221         else if (resource->flags & IORESOURCE_DRQ) {
222                 pnp_set_drq(dev, resource->base, resource->index);
223         }
224         else if (resource->flags  & IORESOURCE_IRQ) {
225                 pnp_set_irq(dev, resource->base, resource->index);
226         }
227         else {
228                 printk_err("ERROR: %s %02x unknown resource type\n",
229                         dev_path(dev), resource->index);
230                 return;
231         }
232         printk_debug(
233                 "%s %02x <- [0x%08lx - 0x%08lx %s\n",
234                 dev_path(dev),
235                 resource->index,
236                 resource->base,  resource->base + resource->size - 1,
237                 (resource->flags & IORESOURCE_IO)? "io":
238                 (resource->flags & IORESOURCE_DRQ)? "drq":
239                 (resource->flags & IORESOURCE_IRQ)? "irq":
240                 (resource->flags & IORESOURCE_MEM)? "mem":
241                 "???");
242 }
243
244 static void pnp_set_resources(device_t dev)
245 {
246         int i;
247         pnp_set_logical_device(dev);
248         for(i = 0; i < dev->resources; i++) {
249                 pnp_set_resource(dev, &dev->resource[i]);
250         }
251
252 }
253 static void pnp_enable_resources(device_t dev)
254 {
255         pnp_set_logical_device(dev);
256         pnp_set_enable(dev, 1);
257
258 }
259 static void pnp_enable(device_t dev)
260 {
261         pnp_set_logical_device(dev);
262         if (!dev->enable) {
263                 pnp_set_enable(dev, 0);
264         }
265 }
266
267 static struct device_operations pnp_ops = {
268         .read_resources   = pnp_read_resources,
269         .set_resources    = pnp_set_resources,
270         .enable_resources = pnp_enable_resources,
271         .enable           = pnp_enable,
272 };
273
274 #define MAX_FUNCTION 10
275 static void enumerate(struct chip *chip)
276 {
277         struct superio_NSC_pc87360_config *conf = (struct superio_NSC_pc87360_config *)chip->chip_info;
278         struct resource *resource;
279         struct device_path path;
280         device_t dev;
281         int i;
282
283         chip_enumerate(chip);
284         path.type       = DEVICE_PATH_PNP;
285         path.u.pnp.port = chip->dev->path.u.pnp.port;
286
287         /* Set the ops on the newly allocated devices */
288         for(i = 0; i <= WDT_DEVICE; i++) {
289                 path.u.pnp.device = i;
290                 dev = alloc_find_dev(chip->bus, &path);
291                 dev->ops = &pnp_ops;
292         }
293
294         /* Processes the hard codes for com1 */
295         path.u.pnp.device = COM1_DEVICE;
296         dev = alloc_find_dev(chip->bus, &path);
297         resource = get_resource(dev, 0x60);
298         if (conf->com1.base) {
299                 resource->base = conf->com1.base;
300                 resource->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_SET;
301         }
302         resource = get_resource(dev, 0x70);
303         if (conf->com1.irq) {
304                 resource->base = conf->com1.irq;
305                 resource->flags = IORESOURCE_IRQ | IORESOURCE_FIXED | IORESOURCE_SET;
306         }
307
308         /* Process the hard codes for the keyboard controller */
309         path.u.pnp.device = KBC_DEVICE;
310         dev = alloc_find_dev(dev, &path);
311         resource = get_resource(dev, 0x60);
312         resource->base = 0x60;
313         resource->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_SET;
314         resource = get_resource(dev, 0x62);
315         resource->base = 0x64;
316         resource->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_SET;
317 }
318
319 struct chip_control superio_NSC_pc87360_control = {
320         .enable    = sio_enable,
321         .enumerate = enumerate,
322         .name      = "winbond w83627thf";
323 };