Please bear with me - another rename checkin. This qualifies as trivial, no
[coreboot.git] / src / devices / pnp_device.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2004 Linux Networx
5  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6  * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
7  * Copyright (C) 2005 Tyan
8  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <console/console.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <bitops.h>
28 #include <string.h>
29 #include <arch/io.h>
30 #include <device/device.h>
31 #include <device/pnp.h>
32
33 /* PNP fundamental operations */
34
35 void pnp_write_config(device_t dev, uint8_t reg, uint8_t value)
36 {
37         outb(reg, dev->path.u.pnp.port);
38         outb(value, dev->path.u.pnp.port + 1);
39 }
40
41 uint8_t pnp_read_config(device_t dev, uint8_t reg)
42 {
43         outb(reg, dev->path.u.pnp.port);
44         return inb(dev->path.u.pnp.port + 1);
45 }
46
47 void pnp_set_logical_device(device_t dev)
48 {
49         pnp_write_config(dev, 0x07, dev->path.u.pnp.device);
50 }
51
52 void pnp_set_enable(device_t dev, int enable)
53 {
54         pnp_write_config(dev, 0x30, enable?0x1:0x0);
55 }
56
57 int pnp_read_enable(device_t dev)
58 {
59         return !!pnp_read_config(dev, 0x30);
60 }
61
62 void pnp_set_iobase(device_t dev, unsigned index, unsigned iobase)
63 {
64         /* Index == 0x60 or 0x62 */
65         pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
66         pnp_write_config(dev, index + 1, iobase & 0xff);
67 }
68
69 void pnp_set_irq(device_t dev, unsigned index, unsigned irq)
70 {
71         /* Index == 0x70 or 0x72 */
72         pnp_write_config(dev, index, irq);
73 }
74
75 void pnp_set_drq(device_t dev, unsigned index, unsigned drq)
76 {
77         /* Index == 0x74 */
78         pnp_write_config(dev, index, drq & 0xff);
79 }
80
81 /* PNP device operations */
82
83 void pnp_read_resources(device_t dev)
84 {
85         return;
86 }
87
88 static void pnp_set_resource(device_t dev, struct resource *resource)
89 {
90         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
91                 printk_err("ERROR: %s %02x %s size: 0x%010Lx not assigned\n",
92                         dev_path(dev), resource->index,
93                         resource_type(resource),
94                         resource->size);
95                 return;
96         }
97
98         /* Now store the resource */
99         if (resource->flags & IORESOURCE_IO) {
100                 pnp_set_iobase(dev, resource->index, resource->base);
101         }
102         else if (resource->flags & IORESOURCE_DRQ) {
103                 pnp_set_drq(dev, resource->index, resource->base);
104         }
105         else if (resource->flags  & IORESOURCE_IRQ) {
106                 pnp_set_irq(dev, resource->index, resource->base);
107         }
108         else {
109                 printk_err("ERROR: %s %02x unknown resource type\n",
110                         dev_path(dev), resource->index);
111                 return;
112         }
113         resource->flags |= IORESOURCE_STORED;
114
115         report_resource_stored(dev, resource, "");
116 }
117
118 void pnp_set_resources(device_t dev)
119 {
120         int i;
121
122         /* Select the device */
123         pnp_set_logical_device(dev);
124
125         /* Paranoia says I should disable the device here... */
126         for(i = 0; i < dev->resources; i++) {
127                 pnp_set_resource(dev, &dev->resource[i]);
128         }
129 }
130
131 void pnp_enable_resources(device_t dev)
132 {
133         pnp_set_logical_device(dev);
134         pnp_set_enable(dev, 1);
135 }
136
137 void pnp_enable(device_t dev)
138 {
139         if (!dev->enabled) {
140                 pnp_set_logical_device(dev);
141                 pnp_set_enable(dev, 0);
142         }
143 }
144
145 struct device_operations pnp_ops = {
146         .read_resources   = pnp_read_resources,
147         .set_resources    = pnp_set_resources,
148         .enable_resources = pnp_enable_resources,
149         .enable           = pnp_enable,
150 };
151
152 /* PNP chip opertations */
153
154 static void pnp_get_ioresource(device_t dev, unsigned index, struct io_info *info)
155 {
156         struct resource *resource;
157         unsigned moving, gran, step;
158
159         resource = new_resource(dev, index);
160         
161         /* Initilize the resource */
162         resource->limit = 0xffff;
163         resource->flags |= IORESOURCE_IO;
164         
165         /* Get the resource size */
166         moving = info->mask;
167         gran = 15;
168         step = 1 << gran;
169         /* Find the first bit that moves */
170         while((moving & step) == 0) {
171                 gran--;
172                 step >>= 1;
173         }
174         /* Now find the first bit that does not move */
175         while((moving & step) != 0) {
176                 gran--;
177                 step >>= 1;
178         }
179         /* Of the moving bits the last bit in the first group,
180          * tells us the size of this resource.
181          */
182         if ((moving & step) == 0) {
183                 gran++;
184                 step <<= 1;
185         }
186         /* Set the resource size and alignment */
187         resource->gran  = gran;
188         resource->align = gran;
189         resource->limit = info->mask | (step - 1);
190         resource->size  = 1 << gran;
191 }
192
193 static void get_resources(device_t dev, struct pnp_info *info)
194 {
195         struct resource *resource;
196
197         if (info->flags & PNP_IO0) {
198                 pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0);
199         }
200         if (info->flags & PNP_IO1) {
201                 pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1);
202         }
203         if (info->flags & PNP_IO2) {
204                 pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2);
205         }
206         if (info->flags & PNP_IO3) {
207                 pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3);
208         }
209         if (info->flags & PNP_IRQ0) {
210                 resource = new_resource(dev, PNP_IDX_IRQ0);
211                 resource->size = 1;
212                 resource->flags |= IORESOURCE_IRQ;
213         }
214         if (info->flags & PNP_IRQ1) {
215                 resource = new_resource(dev, PNP_IDX_IRQ1);
216                 resource->size = 1;
217                 resource->flags |= IORESOURCE_IRQ;
218         }
219         if (info->flags & PNP_DRQ0) {
220                 resource = new_resource(dev, PNP_IDX_DRQ0);
221                 resource->size = 1;
222                 resource->flags |= IORESOURCE_DRQ;
223         }
224         if (info->flags & PNP_DRQ1) {
225                 resource = new_resource(dev, PNP_IDX_DRQ1);
226                 resource->size = 1;
227                 resource->flags |= IORESOURCE_DRQ;
228         }       
229
230
231 void pnp_enable_devices(device_t base_dev, struct device_operations *ops, 
232         unsigned functions, struct pnp_info *info)
233 {
234         struct device_path path;
235         device_t dev;
236         int i;
237
238         path.type       = DEVICE_PATH_PNP;
239         path.u.pnp.port = base_dev->path.u.pnp.port;
240         
241         /* Setup the ops and resources on the newly allocated devices */
242         for(i = 0; i < functions; i++) {
243                 /* Skip logical devices this Super I/O doesn't have. */
244                 if (info[i].function == -1)
245                         continue;
246
247                 path.u.pnp.device = info[i].function;
248                 dev = alloc_find_dev(base_dev->bus, &path);
249                 
250                 /* Don't initialize a device multiple times */
251                 if (dev->ops) 
252                         continue;
253
254                 if (info[i].ops == 0) {
255                         dev->ops = ops;
256                 } else {
257                         dev->ops = info[i].ops;
258                 }
259                 get_resources(dev, &info[i]);
260         }
261 }