2 * This file is part of the coreboot project.
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)
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.
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.
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
24 #include <console/console.h>
30 #include <device/device.h>
31 #include <device/pnp.h>
33 /* PNP fundamental operations */
35 void pnp_write_config(device_t dev, u8 reg, u8 value)
37 outb(reg, dev->path.pnp.port);
38 outb(value, dev->path.pnp.port + 1);
41 u8 pnp_read_config(device_t dev, u8 reg)
43 outb(reg, dev->path.pnp.port);
44 return inb(dev->path.pnp.port + 1);
47 void pnp_set_logical_device(device_t dev)
49 pnp_write_config(dev, 0x07, dev->path.pnp.device & 0xff);
52 void pnp_set_enable(device_t dev, int enable)
56 tmp = pnp_read_config(dev, 0x30);
58 /* Handle virtual devices, which share the same LDN register. */
59 bitpos = (dev->path.pnp.device >> 8) & 0x7;
64 tmp &= ~(1 << bitpos);
66 pnp_write_config(dev, 0x30, tmp);
69 int pnp_read_enable(device_t dev)
73 tmp = pnp_read_config(dev, 0x30);
75 /* Handle virtual devices, which share the same LDN register. */
76 bitpos = (dev->path.pnp.device >> 8) & 0x7;
78 return !!(tmp & (1 << bitpos));
81 void pnp_set_iobase(device_t dev, u8 index, u16 iobase)
83 /* Index == 0x60 or 0x62. */
84 pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
85 pnp_write_config(dev, index + 1, iobase & 0xff);
88 void pnp_set_irq(device_t dev, u8 index, u8 irq)
90 /* Index == 0x70 or 0x72. */
91 pnp_write_config(dev, index, irq);
94 void pnp_set_drq(device_t dev, u8 index, u8 drq)
97 pnp_write_config(dev, index, drq & 0xff);
100 /* PNP device operations */
102 void pnp_read_resources(device_t dev)
107 static void pnp_set_resource(device_t dev, struct resource *resource)
109 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
110 printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010Lx "
111 "not assigned\n", dev_path(dev), resource->index,
112 resource_type(resource), resource->size);
116 /* Now store the resource. */
117 if (resource->flags & IORESOURCE_IO) {
118 pnp_set_iobase(dev, resource->index, resource->base);
119 } else if (resource->flags & IORESOURCE_DRQ) {
120 pnp_set_drq(dev, resource->index, resource->base);
121 } else if (resource->flags & IORESOURCE_IRQ) {
122 pnp_set_irq(dev, resource->index, resource->base);
124 printk(BIOS_ERR, "ERROR: %s %02lx unknown resource type\n",
125 dev_path(dev), resource->index);
128 resource->flags |= IORESOURCE_STORED;
130 report_resource_stored(dev, resource, "");
133 void pnp_set_resources(device_t dev)
135 struct resource *res;
137 /* Select the logical device (LDN). */
138 pnp_set_logical_device(dev);
140 /* Paranoia says I should disable the device here... */
141 for (res = dev->resource_list; res; res = res->next)
142 pnp_set_resource(dev, res);
145 void pnp_enable_resources(device_t dev)
147 pnp_set_logical_device(dev);
148 pnp_set_enable(dev, 1);
151 void pnp_enable(device_t dev)
154 pnp_set_logical_device(dev);
155 pnp_set_enable(dev, 0);
159 struct device_operations pnp_ops = {
160 .read_resources = pnp_read_resources,
161 .set_resources = pnp_set_resources,
162 .enable_resources = pnp_enable_resources,
163 .enable = pnp_enable,
166 /* PNP chip operations */
168 static void pnp_get_ioresource(device_t dev, u8 index, struct io_info *info)
170 struct resource *resource;
171 unsigned moving, gran, step;
173 resource = new_resource(dev, index);
175 /* Initilize the resource. */
176 resource->limit = 0xffff;
177 resource->flags |= IORESOURCE_IO;
179 /* Get the resource size... */
185 /* Find the first bit that moves. */
186 while ((moving & step) == 0) {
191 /* Now find the first bit that does not move. */
192 while ((moving & step) != 0) {
198 * Of the moving bits the last bit in the first group,
199 * tells us the size of this resource.
201 if ((moving & step) == 0) {
206 /* Set the resource size and alignment. */
207 resource->gran = gran;
208 resource->align = gran;
209 resource->limit = info->mask | (step - 1);
210 resource->size = 1 << gran;
213 static void get_resources(device_t dev, struct pnp_info *info)
215 struct resource *resource;
217 if (info->flags & PNP_IO0)
218 pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0);
219 if (info->flags & PNP_IO1)
220 pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1);
221 if (info->flags & PNP_IO2)
222 pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2);
223 if (info->flags & PNP_IO3)
224 pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3);
226 if (info->flags & PNP_IRQ0) {
227 resource = new_resource(dev, PNP_IDX_IRQ0);
229 resource->flags |= IORESOURCE_IRQ;
231 if (info->flags & PNP_IRQ1) {
232 resource = new_resource(dev, PNP_IDX_IRQ1);
234 resource->flags |= IORESOURCE_IRQ;
237 if (info->flags & PNP_DRQ0) {
238 resource = new_resource(dev, PNP_IDX_DRQ0);
240 resource->flags |= IORESOURCE_DRQ;
242 if (info->flags & PNP_DRQ1) {
243 resource = new_resource(dev, PNP_IDX_DRQ1);
245 resource->flags |= IORESOURCE_DRQ;
249 * These are not IRQs, but set the flag to have the
250 * resource allocator do the right thing.
252 if (info->flags & PNP_EN) {
253 resource = new_resource(dev, PNP_IDX_EN);
255 resource->flags |= IORESOURCE_IRQ;
257 if (info->flags & PNP_MSC0) {
258 resource = new_resource(dev, PNP_IDX_MSC0);
260 resource->flags |= IORESOURCE_IRQ;
262 if (info->flags & PNP_MSC1) {
263 resource = new_resource(dev, PNP_IDX_MSC1);
265 resource->flags |= IORESOURCE_IRQ;
269 void pnp_enable_devices(device_t base_dev, struct device_operations *ops,
270 unsigned int functions, struct pnp_info *info)
272 struct device_path path;
276 path.type = DEVICE_PATH_PNP;
277 path.pnp.port = base_dev->path.pnp.port;
279 /* Setup the ops and resources on the newly allocated devices. */
280 for (i = 0; i < functions; i++) {
281 /* Skip logical devices this Super I/O doesn't have. */
282 if (info[i].function == -1)
285 path.pnp.device = info[i].function;
286 dev = alloc_find_dev(base_dev->bus, &path);
288 /* Don't initialize a device multiple times. */
292 if (info[i].ops == 0)
295 dev->ops = info[i].ops;
297 get_resources(dev, &info[i]);