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