Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 1 | /* linux/drivers/mtd/maps/scx200_docflash.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> |
| 4 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 5 | $Id: scx200_docflash.c,v 1.12 2005/11/07 11:14:28 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | National Semiconductor SCx200 flash mapped with DOCCS |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <linux/mtd/mtd.h> |
| 17 | #include <linux/mtd/map.h> |
| 18 | #include <linux/mtd/partitions.h> |
| 19 | |
| 20 | #include <linux/pci.h> |
| 21 | #include <linux/scx200.h> |
| 22 | |
| 23 | #define NAME "scx200_docflash" |
| 24 | |
| 25 | MODULE_AUTHOR("Christer Weinigel <wingel@hack.org>"); |
| 26 | MODULE_DESCRIPTION("NatSemi SCx200 DOCCS Flash Driver"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
| 29 | static int probe = 0; /* Don't autoprobe */ |
| 30 | static unsigned size = 0x1000000; /* 16 MiB the whole ISA address space */ |
| 31 | static unsigned width = 8; /* Default to 8 bits wide */ |
| 32 | static char *flashtype = "cfi_probe"; |
| 33 | |
| 34 | module_param(probe, int, 0); |
| 35 | MODULE_PARM_DESC(probe, "Probe for a BIOS mapping"); |
| 36 | module_param(size, int, 0); |
| 37 | MODULE_PARM_DESC(size, "Size of the flash mapping"); |
| 38 | module_param(width, int, 0); |
| 39 | MODULE_PARM_DESC(width, "Data width of the flash mapping (8/16)"); |
| 40 | module_param(flashtype, charp, 0); |
| 41 | MODULE_PARM_DESC(flashtype, "Type of MTD probe to do"); |
| 42 | |
| 43 | static struct resource docmem = { |
| 44 | .flags = IORESOURCE_MEM, |
| 45 | .name = "NatSemi SCx200 DOCCS Flash", |
| 46 | }; |
| 47 | |
| 48 | static struct mtd_info *mymtd; |
| 49 | |
| 50 | #ifdef CONFIG_MTD_PARTITIONS |
| 51 | static struct mtd_partition partition_info[] = { |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 52 | { |
| 53 | .name = "DOCCS Boot kernel", |
| 54 | .offset = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | .size = 0xc0000 |
| 56 | }, |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 57 | { |
| 58 | .name = "DOCCS Low BIOS", |
| 59 | .offset = 0xc0000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | .size = 0x40000 |
| 61 | }, |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 62 | { |
| 63 | .name = "DOCCS File system", |
| 64 | .offset = 0x100000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | .size = ~0 /* calculate from flash size */ |
| 66 | }, |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 67 | { |
| 68 | .name = "DOCCS High BIOS", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | .offset = ~0, /* calculate from flash size */ |
| 70 | .size = 0x80000 |
| 71 | }, |
| 72 | }; |
Tobias Klauser | 87d10f3 | 2006-03-31 02:29:45 -0800 | [diff] [blame] | 73 | #define NUM_PARTITIONS ARRAY_SIZE(partition_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | |
| 77 | static struct map_info scx200_docflash_map = { |
| 78 | .name = "NatSemi SCx200 DOCCS Flash", |
| 79 | }; |
| 80 | |
| 81 | static int __init init_scx200_docflash(void) |
| 82 | { |
| 83 | unsigned u; |
| 84 | unsigned base; |
| 85 | unsigned ctrl; |
| 86 | unsigned pmr; |
| 87 | struct pci_dev *bridge; |
| 88 | |
| 89 | printk(KERN_DEBUG NAME ": NatSemi SCx200 DOCCS Flash Driver\n"); |
| 90 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 91 | if ((bridge = pci_find_device(PCI_VENDOR_ID_NS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | PCI_DEVICE_ID_NS_SCx200_BRIDGE, |
| 93 | NULL)) == NULL) |
| 94 | return -ENODEV; |
| 95 | |
| 96 | /* check that we have found the configuration block */ |
| 97 | if (!scx200_cb_present()) |
| 98 | return -ENODEV; |
| 99 | |
| 100 | if (probe) { |
| 101 | /* Try to use the present flash mapping if any */ |
| 102 | pci_read_config_dword(bridge, SCx200_DOCCS_BASE, &base); |
| 103 | pci_read_config_dword(bridge, SCx200_DOCCS_CTRL, &ctrl); |
| 104 | pmr = inl(scx200_cb_base + SCx200_PMR); |
| 105 | |
| 106 | if (base == 0 |
| 107 | || (ctrl & 0x07000000) != 0x07000000 |
| 108 | || (ctrl & 0x0007ffff) == 0) |
| 109 | return -ENODEV; |
| 110 | |
| 111 | size = ((ctrl&0x1fff)<<13) + (1<<13); |
| 112 | |
| 113 | for (u = size; u > 1; u >>= 1) |
| 114 | ; |
| 115 | if (u != 1) |
| 116 | return -ENODEV; |
| 117 | |
| 118 | if (pmr & (1<<6)) |
| 119 | width = 16; |
| 120 | else |
| 121 | width = 8; |
| 122 | |
| 123 | docmem.start = base; |
| 124 | docmem.end = base + size; |
| 125 | |
| 126 | if (request_resource(&iomem_resource, &docmem)) { |
| 127 | printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n"); |
| 128 | return -ENOMEM; |
| 129 | } |
| 130 | } else { |
| 131 | for (u = size; u > 1; u >>= 1) |
| 132 | ; |
| 133 | if (u != 1) { |
| 134 | printk(KERN_ERR NAME ": invalid size for flash mapping\n"); |
| 135 | return -EINVAL; |
| 136 | } |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (width != 8 && width != 16) { |
| 139 | printk(KERN_ERR NAME ": invalid bus width for flash mapping\n"); |
| 140 | return -EINVAL; |
| 141 | } |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 142 | |
| 143 | if (allocate_resource(&iomem_resource, &docmem, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | size, |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 145 | 0xc0000000, 0xffffffff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | size, NULL, NULL)) { |
| 147 | printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n"); |
| 148 | return -ENOMEM; |
| 149 | } |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | ctrl = 0x07000000 | ((size-1) >> 13); |
| 152 | |
| 153 | printk(KERN_INFO "DOCCS BASE=0x%08lx, CTRL=0x%08lx\n", (long)docmem.start, (long)ctrl); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | pci_write_config_dword(bridge, SCx200_DOCCS_BASE, docmem.start); |
| 156 | pci_write_config_dword(bridge, SCx200_DOCCS_CTRL, ctrl); |
| 157 | pmr = inl(scx200_cb_base + SCx200_PMR); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (width == 8) { |
| 160 | pmr &= ~(1<<6); |
| 161 | } else { |
| 162 | pmr |= (1<<6); |
| 163 | } |
| 164 | outl(pmr, scx200_cb_base + SCx200_PMR); |
| 165 | } |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 166 | |
Greg Kroah-Hartman | 176dfc6 | 2006-06-12 15:15:17 -0700 | [diff] [blame^] | 167 | printk(KERN_INFO NAME ": DOCCS mapped at 0x%llx-0x%llx, width %d\n", |
| 168 | (unsigned long long)docmem.start, |
| 169 | (unsigned long long)docmem.end, width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | scx200_docflash_map.size = size; |
| 172 | if (width == 8) |
| 173 | scx200_docflash_map.bankwidth = 1; |
| 174 | else |
| 175 | scx200_docflash_map.bankwidth = 2; |
| 176 | |
| 177 | simple_map_init(&scx200_docflash_map); |
| 178 | |
| 179 | scx200_docflash_map.phys = docmem.start; |
| 180 | scx200_docflash_map.virt = ioremap(docmem.start, scx200_docflash_map.size); |
| 181 | if (!scx200_docflash_map.virt) { |
| 182 | printk(KERN_ERR NAME ": failed to ioremap the flash\n"); |
| 183 | release_resource(&docmem); |
| 184 | return -EIO; |
| 185 | } |
| 186 | |
| 187 | mymtd = do_map_probe(flashtype, &scx200_docflash_map); |
| 188 | if (!mymtd) { |
| 189 | printk(KERN_ERR NAME ": unable to detect flash\n"); |
| 190 | iounmap(scx200_docflash_map.virt); |
| 191 | release_resource(&docmem); |
| 192 | return -ENXIO; |
| 193 | } |
| 194 | |
| 195 | if (size < mymtd->size) |
| 196 | printk(KERN_WARNING NAME ": warning, flash mapping is smaller than flash size\n"); |
| 197 | |
| 198 | mymtd->owner = THIS_MODULE; |
| 199 | |
| 200 | #ifdef CONFIG_MTD_PARTITIONS |
| 201 | partition_info[3].offset = mymtd->size-partition_info[3].size; |
| 202 | partition_info[2].size = partition_info[3].offset-partition_info[2].offset; |
| 203 | add_mtd_partitions(mymtd, partition_info, NUM_PARTITIONS); |
| 204 | #else |
| 205 | add_mtd_device(mymtd); |
| 206 | #endif |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static void __exit cleanup_scx200_docflash(void) |
| 211 | { |
| 212 | if (mymtd) { |
| 213 | #ifdef CONFIG_MTD_PARTITIONS |
| 214 | del_mtd_partitions(mymtd); |
| 215 | #else |
| 216 | del_mtd_device(mymtd); |
| 217 | #endif |
| 218 | map_destroy(mymtd); |
| 219 | } |
| 220 | if (scx200_docflash_map.virt) { |
| 221 | iounmap(scx200_docflash_map.virt); |
| 222 | release_resource(&docmem); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | module_init(init_scx200_docflash); |
| 227 | module_exit(cleanup_scx200_docflash); |
| 228 | |
| 229 | /* |
| 230 | Local variables: |
| 231 | compile-command: "make -k -C ../../.. SUBDIRS=drivers/mtd/maps modules" |
| 232 | c-basic-offset: 8 |
| 233 | End: |
| 234 | */ |