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, uint8_t reg, uint8_t value)
37 outb(reg, dev->path.u.pnp.port);
38 outb(value, dev->path.u.pnp.port + 1);
41 uint8_t pnp_read_config(device_t dev, uint8_t reg)
43 outb(reg, dev->path.u.pnp.port);
44 return inb(dev->path.u.pnp.port + 1);
47 void pnp_set_logical_device(device_t dev)
49 pnp_write_config(dev, 0x07, dev->path.u.pnp.device);
52 void pnp_set_enable(device_t dev, int enable)
54 pnp_write_config(dev, 0x30, enable?0x1:0x0);
57 int pnp_read_enable(device_t dev)
59 return !!pnp_read_config(dev, 0x30);
62 void pnp_set_iobase(device_t dev, unsigned index, unsigned iobase)
64 /* Index == 0x60 or 0x62 */
65 pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
66 pnp_write_config(dev, index + 1, iobase & 0xff);
69 void pnp_set_irq(device_t dev, unsigned index, unsigned irq)
71 /* Index == 0x70 or 0x72 */
72 pnp_write_config(dev, index, irq);
75 void pnp_set_drq(device_t dev, unsigned index, unsigned drq)
78 pnp_write_config(dev, index, drq & 0xff);
81 /* PNP device operations */
83 void pnp_read_resources(device_t dev)
88 static void pnp_set_resource(device_t dev, struct resource *resource)
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),
98 /* Now store the resource */
99 if (resource->flags & IORESOURCE_IO) {
100 pnp_set_iobase(dev, resource->index, resource->base);
102 else if (resource->flags & IORESOURCE_DRQ) {
103 pnp_set_drq(dev, resource->index, resource->base);
105 else if (resource->flags & IORESOURCE_IRQ) {
106 pnp_set_irq(dev, resource->index, resource->base);
109 printk_err("ERROR: %s %02x unknown resource type\n",
110 dev_path(dev), resource->index);
113 resource->flags |= IORESOURCE_STORED;
115 report_resource_stored(dev, resource, "");
118 void pnp_set_resources(device_t dev)
122 /* Select the device */
123 pnp_set_logical_device(dev);
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]);
131 void pnp_enable_resources(device_t dev)
133 pnp_set_logical_device(dev);
134 pnp_set_enable(dev, 1);
137 void pnp_enable(device_t dev)
140 pnp_set_logical_device(dev);
141 pnp_set_enable(dev, 0);
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,
152 /* PNP chip opertations */
154 static void pnp_get_ioresource(device_t dev, unsigned index, struct io_info *info)
156 struct resource *resource;
157 unsigned moving, gran, step;
159 resource = new_resource(dev, index);
161 /* Initilize the resource */
162 resource->limit = 0xffff;
163 resource->flags |= IORESOURCE_IO;
165 /* Get the resource size */
169 /* Find the first bit that moves */
170 while((moving & step) == 0) {
174 /* Now find the first bit that does not move */
175 while((moving & step) != 0) {
179 /* Of the moving bits the last bit in the first group,
180 * tells us the size of this resource.
182 if ((moving & step) == 0) {
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;
193 static void get_resources(device_t dev, struct pnp_info *info)
195 struct resource *resource;
197 if (info->flags & PNP_IO0) {
198 pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0);
200 if (info->flags & PNP_IO1) {
201 pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1);
203 if (info->flags & PNP_IO2) {
204 pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2);
206 if (info->flags & PNP_IO3) {
207 pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3);
209 if (info->flags & PNP_IRQ0) {
210 resource = new_resource(dev, PNP_IDX_IRQ0);
212 resource->flags |= IORESOURCE_IRQ;
214 if (info->flags & PNP_IRQ1) {
215 resource = new_resource(dev, PNP_IDX_IRQ1);
217 resource->flags |= IORESOURCE_IRQ;
219 if (info->flags & PNP_DRQ0) {
220 resource = new_resource(dev, PNP_IDX_DRQ0);
222 resource->flags |= IORESOURCE_DRQ;
224 if (info->flags & PNP_DRQ1) {
225 resource = new_resource(dev, PNP_IDX_DRQ1);
227 resource->flags |= IORESOURCE_DRQ;
231 void pnp_enable_devices(device_t base_dev, struct device_operations *ops,
232 unsigned functions, struct pnp_info *info)
234 struct device_path path;
238 path.type = DEVICE_PATH_PNP;
239 path.u.pnp.port = base_dev->path.u.pnp.port;
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)
247 path.u.pnp.device = info[i].function;
248 dev = alloc_find_dev(base_dev->bus, &path);
250 /* Don't initialize a device multiple times */
254 if (info[i].ops == 0) {
257 dev->ops = info[i].ops;
259 get_resources(dev, &info[i]);