vserver 1.9.5.x5
[linux-2.6.git] / drivers / mtd / maps / amd76xrom.c
index b433127..51e97b0 100644 (file)
@@ -2,7 +2,7 @@
  * amd76xrom.c
  *
  * Normal mappings of chips in physical memory
- * $Id: amd76xrom.c,v 1.12 2004/07/14 14:44:31 thayne Exp $
+ * $Id: amd76xrom.c,v 1.19 2004/11/28 09:40:39 dwmw2 Exp $
  */
 
 #include <linux/module.h>
 #include <asm/io.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/map.h>
+#include <linux/mtd/cfi.h>
+#include <linux/mtd/flashchip.h>
 #include <linux/config.h>
 #include <linux/pci.h>
 #include <linux/pci_ids.h>
+#include <linux/list.h>
 
 
 #define xstr(s) str(s)
 #define str(s) #s
 #define MOD_NAME xstr(KBUILD_BASENAME)
 
-#define MTD_DEV_NAME_LENGTH 16
+#define ADDRESS_NAME_LEN 18
+
+#define ROM_PROBE_STEP_SIZE (64*1024) /* 64KiB */
+
+struct amd76xrom_window {
+       void __iomem *virt;
+       unsigned long phys;
+       unsigned long size;
+       struct list_head maps;
+       struct resource rsrc;
+       struct pci_dev *pdev;
+};
 
 struct amd76xrom_map_info {
+       struct list_head list;
        struct map_info map;
        struct mtd_info *mtd;
-       void __iomem * window_addr;
-       u32 window_start, window_size;
-       struct pci_dev *pdev;
-       struct resource window_rsrc;
-       struct resource rom_rsrc;
-       char mtd_name[MTD_DEV_NAME_LENGTH];
+       struct resource rsrc;
+       char map_name[sizeof(MOD_NAME) + 2 + ADDRESS_NAME_LEN];
 };
 
-
-static struct amd76xrom_map_info amd76xrom_map = {
-       .map = {
-               .name = MOD_NAME,
-               .size = 0,
-               .bankwidth = 1,
-       }
-       /* remaining fields of structure are initialized to 0 */
+static struct amd76xrom_window amd76xrom_window = {
+       .maps = LIST_HEAD_INIT(amd76xrom_window.maps),
 };
 
-
-static void amd76xrom_cleanup(struct amd76xrom_map_info *info)
+static void amd76xrom_cleanup(struct amd76xrom_window *window)
 {
+       struct amd76xrom_map_info *map, *scratch;
        u8 byte;
 
-       /* Disable writes through the rom window */
-       pci_read_config_byte(info->pdev, 0x40, &byte);
-       pci_write_config_byte(info->pdev, 0x40, byte & ~1);
+       if (window->pdev) {
+               /* Disable writes through the rom window */
+               pci_read_config_byte(window->pdev, 0x40, &byte);
+               pci_write_config_byte(window->pdev, 0x40, byte & ~1);
+       }
 
-       if (info->mtd) {
-               del_mtd_device(info->mtd);
-               map_destroy(info->mtd);
-               info->mtd = NULL;
-               info->map.virt = NULL;
+       /* Free all of the mtd devices */
+       list_for_each_entry_safe(map, scratch, &window->maps, list) {
+               if (map->rsrc.parent) {
+                       release_resource(&map->rsrc);
+               }
+               del_mtd_device(map->mtd);
+               map_destroy(map->mtd);
+               list_del(&map->list);
+               kfree(map);
        }
-       if (info->rom_rsrc.parent)
-               release_resource(&info->rom_rsrc);
-       if (info->window_rsrc.parent)
-               release_resource(&info->window_rsrc);
-
-       if (info->window_addr) {
-               iounmap(info->window_addr);
-               info->window_addr = NULL;
+       if (window->rsrc.parent) 
+               release_resource(&window->rsrc);
+
+       if (window->virt) {
+               iounmap(window->virt);
+               window->virt = NULL;
+               window->phys = 0;
+               window->size = 0;
+               window->pdev = NULL;
        }
 }
 
@@ -74,167 +86,196 @@ static void amd76xrom_cleanup(struct amd76xrom_map_info *info)
 static int __devinit amd76xrom_init_one (struct pci_dev *pdev,
        const struct pci_device_id *ent)
 {
-       struct rom_window {
-               u32 start;
-               u32 size;
-               u8 segen_bits;
-       };
-       static struct rom_window rom_window[] = {
-               /*
-                * Need the 5MiB window for chips that have block lock/unlock
-                * registers located below 4MiB window.
-                */
-               { 0xffb00000, 5*1024*1024, (1<<7) | (1<<6), },
-               { 0xffc00000, 4*1024*1024, (1<<7), },
-               { 0xffff0000, 64*1024,     0 },
-               { 0         , 0,           0 },
-       };
-       static const u32 rom_probe_sizes[] = { 
-               5*1024*1024, 4*1024*1024, 2*1024*1024, 1024*1024, 512*1024, 
-               256*1024, 128*1024, 64*1024, 0};
        static char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
        u8 byte;
-       struct amd76xrom_map_info *info = &amd76xrom_map;
-       struct rom_window *window;
-       int i;
-       u32 rom_size;
-
-       info->pdev = pdev;
-       window = &rom_window[0];
-
-       while (window->size) {
-               /*
-                * Try to reserve the window mem region.  If this fails then
-                * it is likely due to a fragment of the window being
-                * "reseved" by the BIOS.  In the case that the
-                * request_mem_region() fails then once the rom size is
-                * discovered we will try to reserve the unreserved fragment.
-                */
-               info->window_rsrc.name = MOD_NAME;
-               info->window_rsrc.start = window->start;
-               info->window_rsrc.end = window->start + window->size - 1;
-               info->window_rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
-               if (request_resource(&iomem_resource, &info->window_rsrc)) {
-                       info->window_rsrc.parent = NULL;
-                       printk(KERN_ERR MOD_NAME
-                              " %s(): Unable to register resource"
-                              " 0x%.08lx-0x%.08lx - kernel bug?\n",
-                              __func__,
-                              info->window_rsrc.start, info->window_rsrc.end);
-               }
+       struct amd76xrom_window *window = &amd76xrom_window;
+       struct amd76xrom_map_info *map = NULL;
+       unsigned long map_top;
+
+       /* Remember the pci dev I find the window in */
+       window->pdev = pdev;
 
-               /* Enable the selected rom window */
-               pci_read_config_byte(pdev, 0x43, &byte);
-               pci_write_config_byte(pdev, 0x43, byte | window->segen_bits);
+       /* Assume the rom window is properly setup, and find it's size */
+       pci_read_config_byte(pdev, 0x43, &byte);
+       if ((byte & ((1<<7)|(1<<6))) == ((1<<7)|(1<<6))) {
+               window->phys = 0xffb00000; /* 5MiB */
+       }
+       else if ((byte & (1<<7)) == (1<<7)) {
+               window->phys = 0xffc00000; /* 4MiB */
+       }
+       else {
+               window->phys = 0xffff0000; /* 64KiB */
+       }
+       window->size = 0xffffffffUL - window->phys + 1UL;
+       
+       /*
+        * Try to reserve the window mem region.  If this fails then
+        * it is likely due to a fragment of the window being
+        * "reseved" by the BIOS.  In the case that the
+        * request_mem_region() fails then once the rom size is
+        * discovered we will try to reserve the unreserved fragment.
+        */
+       window->rsrc.name = MOD_NAME;
+       window->rsrc.start = window->phys;
+       window->rsrc.end   = window->phys + window->size - 1;
+       window->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
+       if (request_resource(&iomem_resource, &window->rsrc)) {
+               window->rsrc.parent = NULL;
+               printk(KERN_ERR MOD_NAME
+                       " %s(): Unable to register resource"
+                       " 0x%.08lx-0x%.08lx - kernel bug?\n",
+                       __func__,
+                       window->rsrc.start, window->rsrc.end);
+       }
 
-               /* Enable writes through the rom window */
-               pci_read_config_byte(pdev, 0x40, &byte);
-               pci_write_config_byte(pdev, 0x40, byte | 1);
+#if 0
 
-               /* FIXME handle registers 0x80 - 0x8C the bios region locks */
+       /* Enable the selected rom window */
+       pci_read_config_byte(pdev, 0x43, &byte);
+       pci_write_config_byte(pdev, 0x43, byte | rwindow->segen_bits);
+#endif
 
-               printk(KERN_NOTICE MOD_NAME " window : %x at %x\n", 
-                      window->size, window->start);
-               /* For write accesses caches are useless */
-               info->window_addr = ioremap_nocache(window->start,
-                                                      window->size);
+       /* Enable writes through the rom window */
+       pci_read_config_byte(pdev, 0x40, &byte);
+       pci_write_config_byte(pdev, 0x40, byte | 1);
+       
+       /* FIXME handle registers 0x80 - 0x8C the bios region locks */
+
+       /* For write accesses caches are useless */
+       window->virt = ioremap_nocache(window->phys, window->size);
+       if (!window->virt) {
+               printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n",
+                       window->phys, window->size);
+               goto out;
+       }
 
-               if (!info->window_addr) {
-                       printk(KERN_ERR "Failed to ioremap\n");
-                       continue;
+       /* Get the first address to look for an rom chip at */
+       map_top = window->phys;
+#if 1
+       /* The probe sequence run over the firmware hub lock
+        * registers sets them to 0x7 (no access).
+        * Probe at most the last 4M of the address space.
+        */
+       if (map_top < 0xffc00000) {
+               map_top = 0xffc00000;
+       }
+#endif
+       /* Loop  through and look for rom chips */
+       while((map_top - 1) < 0xffffffffUL) {
+               struct cfi_private *cfi;
+               unsigned long offset;
+               int i;
+
+               if (!map) {
+                       map = kmalloc(sizeof(*map), GFP_KERNEL);
+               }
+               if (!map) {
+                       printk(KERN_ERR MOD_NAME ": kmalloc failed");
+                       goto out;
                }
+               memset(map, 0, sizeof(*map));
+               INIT_LIST_HEAD(&map->list);
+               map->map.name = map->map_name;
+               map->map.phys = map_top;
+               offset = map_top - window->phys;
+               map->map.virt = (void __iomem *)
+                       (((unsigned long)(window->virt)) + offset);
+               map->map.size = 0xffffffffUL - map_top + 1UL;
+               /* Set the name of the map to the address I am trying */
+               sprintf(map->map_name, "%s @%08lx",
+                       MOD_NAME, map->map.phys);
+
+               /* There is no generic VPP support */
+               for(map->map.bankwidth = 32; map->map.bankwidth; 
+                       map->map.bankwidth >>= 1)
+               {
+                       char **probe_type;
+                       /* Skip bankwidths that are not supported */
+                       if (!map_bankwidth_supported(map->map.bankwidth))
+                               continue;
 
-               info->mtd = NULL;
+                       /* Setup the map methods */
+                       simple_map_init(&map->map);
 
-               for(i = 0; (rom_size = rom_probe_sizes[i]); i++) {
-                       char **chip_type;
-                       if (rom_size > window->size) {
-                               continue;
-                       }
-                       info->map.phys = window->start + window->size - rom_size;
-                       info->map.virt = 
-                               info->window_addr + window->size - rom_size;
-                       info->map.size = rom_size;
-                       simple_map_init(&info->map);
-                       chip_type = rom_probe_types;
-                       for(; !info->mtd && *chip_type; chip_type++) {
-                               info->mtd = do_map_probe(*chip_type, &amd76xrom_map.map);
+                       /* Try all of the probe methods */
+                       probe_type = rom_probe_types;
+                       for(; *probe_type; probe_type++) {
+                               map->mtd = do_map_probe(*probe_type, &map->map);
+                               if (map->mtd)
+                                       goto found;
                        }
-                       if (info->mtd) goto found_mtd;
                }
-               iounmap(info->window_addr);
-               info->window_addr = NULL;
-
-               /* Disable writes through the rom window */
-               pci_read_config_byte(pdev, 0x40, &byte);
-               pci_write_config_byte(pdev, 0x40, byte & ~1);
-
-               window++;
-       }
-       goto failed;
-
- found_mtd:
-       printk(KERN_NOTICE MOD_NAME " chip at offset: 0x%x\n",
-               window->size - rom_size);
-
-       info->mtd->owner = THIS_MODULE;
-
-       if (!info->window_rsrc.parent) {
-               /* failed to reserve entire window - try fragments */
-               info->window_rsrc.name = MOD_NAME;
-               info->window_rsrc.start = window->start;
-               info->window_rsrc.end = window->start + window->size - rom_size - 1;
-               info->window_rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
-               if (request_resource(&iomem_resource, &info->window_rsrc)) {
-                       printk(KERN_ERR MOD_NAME
-                              ": cannot reserve window resource fragment\n");
-#if 0
+               map_top += ROM_PROBE_STEP_SIZE;
+               continue;
+       found:
+               /* Trim the size if we are larger than the map */
+               if (map->mtd->size > map->map.size) {
+                       printk(KERN_WARNING MOD_NAME
+                               " rom(%u) larger than window(%lu). fixing...\n",
+                               map->mtd->size, map->map.size);
+                       map->mtd->size = map->map.size;
+               }
+               if (window->rsrc.parent) {
                        /*
-                        * The BIOS e820 usually reserves this so it isn't
-                        * usually an error.
+                        * Registering the MTD device in iomem may not be possible
+                        * if there is a BIOS "reserved" and BUSY range.  If this
+                        * fails then continue anyway.
                         */
-                       goto failed;
-#endif
+                       map->rsrc.name  = map->map_name;
+                       map->rsrc.start = map->map.phys;
+                       map->rsrc.end   = map->map.phys + map->mtd->size - 1;
+                       map->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
+                       if (request_resource(&window->rsrc, &map->rsrc)) {
+                               printk(KERN_ERR MOD_NAME
+                                       ": cannot reserve MTD resource\n");
+                               map->rsrc.parent = NULL;
+                       }
                }
-       }
 
-       add_mtd_device(info->mtd);
-       info->window_start = window->start;
-       info->window_size = window->size;
-
-       if (info->window_rsrc.parent) {
-               /*
-                * Registering the MTD device in iomem may not be possible
-                * if there is a BIOS "reserved" and BUSY range.  If this
-                * fails then continue anyway.
-                */
-               snprintf(info->mtd_name, MTD_DEV_NAME_LENGTH,
-                        "mtd%d", info->mtd->index);
-
-               info->rom_rsrc.name = info->mtd_name;
-               info->rom_rsrc.start = window->start + window->size - rom_size;
-               info->rom_rsrc.end = window->start + window->size - 1;
-               info->rom_rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
-               if (request_resource(&info->window_rsrc, &info->rom_rsrc)) {
-                       printk(KERN_ERR MOD_NAME
-                              ": cannot reserve MTD resource\n");
-                       info->rom_rsrc.parent = NULL;
+               /* Make the whole region visible in the map */
+               map->map.virt = window->virt;
+               map->map.phys = window->phys;
+               cfi = map->map.fldrv_priv;
+               for(i = 0; i < cfi->numchips; i++) {
+                       cfi->chips[i].start += offset;
                }
+               
+               /* Now that the mtd devices is complete claim and export it */
+               map->mtd->owner = THIS_MODULE;
+               if (add_mtd_device(map->mtd)) {
+                       map_destroy(map->mtd);
+                       map->mtd = NULL;
+                       goto out;
+               }
+
+
+               /* Calculate the new value of map_top */
+               map_top += map->mtd->size;
+
+               /* File away the map structure */
+               list_add(&map->list, &window->maps);
+               map = NULL;
        }
 
+ out:
+       /* Free any left over map structures */
+       if (map) {
+               kfree(map);
+       }
+       /* See if I have any map structures */
+       if (list_empty(&window->maps)) {
+               amd76xrom_cleanup(window);
+               return -ENODEV;
+       }
        return 0;
-
- failed:
-       amd76xrom_cleanup(info);
-       return -ENODEV;
 }
 
 
 static void __devexit amd76xrom_remove_one (struct pci_dev *pdev)
 {
-       struct amd76xrom_map_info *info = &amd76xrom_map;
+       struct amd76xrom_window *window = &amd76xrom_window;
 
-       amd76xrom_cleanup(info);
+       amd76xrom_cleanup(window);
 }
 
 static struct pci_device_id amd76xrom_pci_tbl[] = {
@@ -257,7 +298,7 @@ static struct pci_driver amd76xrom_driver = {
 };
 #endif
 
-int __init init_amd76xrom(void)
+static int __init init_amd76xrom(void)
 {
        struct pci_dev *pdev;
        struct pci_device_id *id;
@@ -269,7 +310,6 @@ int __init init_amd76xrom(void)
                }
        }
        if (pdev) {
-               amd76xrom_map.pdev = pdev;
                return amd76xrom_init_one(pdev, &amd76xrom_pci_tbl[0]);
        }
        return -ENXIO;
@@ -280,7 +320,7 @@ int __init init_amd76xrom(void)
 
 static void __exit cleanup_amd76xrom(void)
 {
-       amd76xrom_remove_one(amd76xrom_map.pdev);
+       amd76xrom_remove_one(amd76xrom_window.pdev);
 }
 
 module_init(init_amd76xrom);