Please bear with me - another rename checkin. This qualifies as trivial, no
[coreboot.git] / src / devices / pnp_device.c
index 5f4ede1ec4a1e5cdfa24b505b51042823f346d1a..b5d8f9716fb7a9785bf60638c5ed9624e78d78fb 100644 (file)
@@ -1,5 +1,25 @@
-/* Copyright 2004 Linux Networx  */
-/* This code is distrubted wihtout warrant under the GPL v2 (see COPYING) */
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2004 Linux Networx
+ * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
+ * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
+ * Copyright (C) 2005 Tyan
+ * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
 
 #include <console/console.h>
 #include <stdlib.h>
@@ -52,7 +72,7 @@ void pnp_set_irq(device_t dev, unsigned index, unsigned irq)
        pnp_write_config(dev, index, irq);
 }
 
-void pnp_set_drq(device_t dev, unsigned drq, unsigned index)
+void pnp_set_drq(device_t dev, unsigned index, unsigned drq)
 {
        /* Index == 0x74 */
        pnp_write_config(dev, index, drq & 0xff);
@@ -68,33 +88,31 @@ void pnp_read_resources(device_t dev)
 static void pnp_set_resource(device_t dev, struct resource *resource)
 {
        if (!(resource->flags & IORESOURCE_ASSIGNED)) {
-               printk_err("ERROR: %s %02x not allocated\n",
-                          dev_path(dev), resource->index);
+               printk_err("ERROR: %s %02x %s size: 0x%010Lx not assigned\n",
+                       dev_path(dev), resource->index,
+                       resource_type(resource),
+                       resource->size);
                return;
        }
 
        /* Now store the resource */
        if (resource->flags & IORESOURCE_IO) {
                pnp_set_iobase(dev, resource->index, resource->base);
-       } else if (resource->flags & IORESOURCE_DRQ) {
+       }
+       else if (resource->flags & IORESOURCE_DRQ) {
                pnp_set_drq(dev, resource->index, resource->base);
-       } else if (resource->flags  & IORESOURCE_IRQ) {
+       }
+       else if (resource->flags  & IORESOURCE_IRQ) {
                pnp_set_irq(dev, resource->index, resource->base);
-       } else {
+       }
+       else {
                printk_err("ERROR: %s %02x unknown resource type\n",
-                          dev_path(dev), resource->index);
+                       dev_path(dev), resource->index);
                return;
        }
        resource->flags |= IORESOURCE_STORED;
 
-       printk_debug("%s %02x <- [0x%08lx - 0x%08lx] %s\n", dev_path(dev),
-                    resource->index, resource->base,
-                    resource->base + resource->size - 1,
-                    (resource->flags & IORESOURCE_IO)? "io":
-                    (resource->flags & IORESOURCE_DRQ)? "drq":
-                    (resource->flags & IORESOURCE_IRQ)? "irq":
-                    (resource->flags & IORESOURCE_MEM)? "mem":
-                    "???");
+       report_resource_stored(dev, resource, "");
 }
 
 void pnp_set_resources(device_t dev)
@@ -105,7 +123,7 @@ void pnp_set_resources(device_t dev)
        pnp_set_logical_device(dev);
 
        /* Paranoia says I should disable the device here... */
-       for (i = 0; i < dev->resources; i++) {
+       for(i = 0; i < dev->resources; i++) {
                pnp_set_resource(dev, &dev->resource[i]);
        }
 }
@@ -133,80 +151,109 @@ struct device_operations pnp_ops = {
 
 /* PNP chip opertations */
 
-static void pnp_get_ioresource(device_t dev, unsigned index,
-                              struct io_info *info)
+static void pnp_get_ioresource(device_t dev, unsigned index, struct io_info *info)
 {
        struct resource *resource;
-       uint32_t size;
+       unsigned moving, gran, step;
 
-       resource = get_resource(dev, index);
+       resource = new_resource(dev, index);
        
        /* Initilize the resource */
        resource->limit = 0xffff;
        resource->flags |= IORESOURCE_IO;
        
+       /* Get the resource size */
+       moving = info->mask;
+       gran = 15;
+       step = 1 << gran;
+       /* Find the first bit that moves */
+       while((moving & step) == 0) {
+               gran--;
+               step >>= 1;
+       }
+       /* Now find the first bit that does not move */
+       while((moving & step) != 0) {
+               gran--;
+               step >>= 1;
+       }
+       /* Of the moving bits the last bit in the first group,
+        * tells us the size of this resource.
+        */
+       if ((moving & step) == 0) {
+               gran++;
+               step <<= 1;
+       }
        /* Set the resource size and alignment */
-       size = (0xffff & info->mask);
-       resource->size  = (~(size | 0xfffff800) + 1);
-       resource->align = log2(resource->size);
-       resource->gran  = resource->align;
+       resource->gran  = gran;
+       resource->align = gran;
+       resource->limit = info->mask | (step - 1);
+       resource->size  = 1 << gran;
 }
 
 static void get_resources(device_t dev, struct pnp_info *info)
 {
        struct resource *resource;
 
-//     pnp_set_logical_device(dev);   // coment out by LYH
-
        if (info->flags & PNP_IO0) {
                pnp_get_ioresource(dev, PNP_IDX_IO0, &info->io0);
        }
        if (info->flags & PNP_IO1) {
                pnp_get_ioresource(dev, PNP_IDX_IO1, &info->io1);
        }
+       if (info->flags & PNP_IO2) {
+               pnp_get_ioresource(dev, PNP_IDX_IO2, &info->io2);
+       }
+       if (info->flags & PNP_IO3) {
+               pnp_get_ioresource(dev, PNP_IDX_IO3, &info->io3);
+       }
        if (info->flags & PNP_IRQ0) {
-               resource = get_resource(dev, PNP_IDX_IRQ0);
+               resource = new_resource(dev, PNP_IDX_IRQ0);
                resource->size = 1;
                resource->flags |= IORESOURCE_IRQ;
        }
        if (info->flags & PNP_IRQ1) {
-               resource = get_resource(dev, PNP_IDX_IRQ1);
+               resource = new_resource(dev, PNP_IDX_IRQ1);
                resource->size = 1;
                resource->flags |= IORESOURCE_IRQ;
        }
        if (info->flags & PNP_DRQ0) {
-               resource = get_resource(dev, PNP_IDX_DRQ0);
+               resource = new_resource(dev, PNP_IDX_DRQ0);
                resource->size = 1;
                resource->flags |= IORESOURCE_DRQ;
        }
        if (info->flags & PNP_DRQ1) {
-               resource = get_resource(dev, PNP_IDX_DRQ1);
+               resource = new_resource(dev, PNP_IDX_DRQ1);
                resource->size = 1;
                resource->flags |= IORESOURCE_DRQ;
        }       
 } 
 
-void pnp_enumerate(struct chip *chip, unsigned functions, 
-                  struct device_operations *ops, struct pnp_info *info)
+void pnp_enable_devices(device_t base_dev, struct device_operations *ops, 
+       unsigned functions, struct pnp_info *info)
 {
        struct device_path path;
        device_t dev;
        int i;
 
-       chip_enumerate(chip);
-
        path.type       = DEVICE_PATH_PNP;
-       path.u.pnp.port = chip->dev->path.u.pnp.port;
+       path.u.pnp.port = base_dev->path.u.pnp.port;
        
        /* Setup the ops and resources on the newly allocated devices */
-       for (i = 0; i < functions; i++) {
-               path.u.pnp.device = info[i].function;
+       for(i = 0; i < functions; i++) {
+               /* Skip logical devices this Super I/O doesn't have. */
+               if (info[i].function == -1)
+                       continue;
 
-               dev = alloc_find_dev(chip->bus, &path);
+               path.u.pnp.device = info[i].function;
+               dev = alloc_find_dev(base_dev->bus, &path);
+               
+               /* Don't initialize a device multiple times */
+               if (dev->ops) 
+                       continue;
 
                if (info[i].ops == 0) {
                        dev->ops = ops;
-               } else { 
+               } else {
                        dev->ops = info[i].ops;
                }
                get_resources(dev, &info[i]);