blob: b5391ebb736e1bac6b4175d7e897670dc38e7ea9 [file] [log] [blame]
Thomas Gleixner69f34c92005-11-07 11:15:40 +00001/* linux/drivers/mtd/maps/scx200_docflash.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 National Semiconductor SCx200 flash mapped with DOCCS
6*/
7
8#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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
22MODULE_AUTHOR("Christer Weinigel <wingel@hack.org>");
23MODULE_DESCRIPTION("NatSemi SCx200 DOCCS Flash Driver");
24MODULE_LICENSE("GPL");
25
26static int probe = 0; /* Don't autoprobe */
27static unsigned size = 0x1000000; /* 16 MiB the whole ISA address space */
28static unsigned width = 8; /* Default to 8 bits wide */
29static char *flashtype = "cfi_probe";
30
31module_param(probe, int, 0);
32MODULE_PARM_DESC(probe, "Probe for a BIOS mapping");
33module_param(size, int, 0);
34MODULE_PARM_DESC(size, "Size of the flash mapping");
35module_param(width, int, 0);
36MODULE_PARM_DESC(width, "Data width of the flash mapping (8/16)");
37module_param(flashtype, charp, 0);
38MODULE_PARM_DESC(flashtype, "Type of MTD probe to do");
39
40static struct resource docmem = {
41 .flags = IORESOURCE_MEM,
42 .name = "NatSemi SCx200 DOCCS Flash",
43};
44
45static struct mtd_info *mymtd;
46
47#ifdef CONFIG_MTD_PARTITIONS
48static struct mtd_partition partition_info[] = {
Thomas Gleixner69f34c92005-11-07 11:15:40 +000049 {
50 .name = "DOCCS Boot kernel",
51 .offset = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 .size = 0xc0000
53 },
Thomas Gleixner69f34c92005-11-07 11:15:40 +000054 {
55 .name = "DOCCS Low BIOS",
56 .offset = 0xc0000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .size = 0x40000
58 },
Thomas Gleixner69f34c92005-11-07 11:15:40 +000059 {
60 .name = "DOCCS File system",
61 .offset = 0x100000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .size = ~0 /* calculate from flash size */
63 },
Thomas Gleixner69f34c92005-11-07 11:15:40 +000064 {
65 .name = "DOCCS High BIOS",
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .offset = ~0, /* calculate from flash size */
67 .size = 0x80000
68 },
69};
Tobias Klauser87d10f32006-03-31 02:29:45 -080070#define NUM_PARTITIONS ARRAY_SIZE(partition_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
72
73
74static struct map_info scx200_docflash_map = {
75 .name = "NatSemi SCx200 DOCCS Flash",
76};
77
78static int __init init_scx200_docflash(void)
79{
80 unsigned u;
81 unsigned base;
82 unsigned ctrl;
83 unsigned pmr;
84 struct pci_dev *bridge;
85
86 printk(KERN_DEBUG NAME ": NatSemi SCx200 DOCCS Flash Driver\n");
87
Alan Coxdd8e9ed2006-09-22 10:19:20 +010088 if ((bridge = pci_get_device(PCI_VENDOR_ID_NS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 PCI_DEVICE_ID_NS_SCx200_BRIDGE,
90 NULL)) == NULL)
91 return -ENODEV;
92
93 /* check that we have found the configuration block */
Alan Coxdd8e9ed2006-09-22 10:19:20 +010094 if (!scx200_cb_present()) {
95 pci_dev_put(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -ENODEV;
Alan Coxdd8e9ed2006-09-22 10:19:20 +010097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if (probe) {
100 /* Try to use the present flash mapping if any */
101 pci_read_config_dword(bridge, SCx200_DOCCS_BASE, &base);
102 pci_read_config_dword(bridge, SCx200_DOCCS_CTRL, &ctrl);
Alan Coxdd8e9ed2006-09-22 10:19:20 +0100103 pci_dev_put(bridge);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 pmr = inl(scx200_cb_base + SCx200_PMR);
106
107 if (base == 0
108 || (ctrl & 0x07000000) != 0x07000000
109 || (ctrl & 0x0007ffff) == 0)
110 return -ENODEV;
111
112 size = ((ctrl&0x1fff)<<13) + (1<<13);
113
114 for (u = size; u > 1; u >>= 1)
115 ;
116 if (u != 1)
117 return -ENODEV;
118
119 if (pmr & (1<<6))
120 width = 16;
121 else
122 width = 8;
123
124 docmem.start = base;
125 docmem.end = base + size;
126
127 if (request_resource(&iomem_resource, &docmem)) {
128 printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
129 return -ENOMEM;
130 }
131 } else {
Alan Coxdd8e9ed2006-09-22 10:19:20 +0100132 pci_dev_put(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 for (u = size; u > 1; u >>= 1)
134 ;
135 if (u != 1) {
136 printk(KERN_ERR NAME ": invalid size for flash mapping\n");
137 return -EINVAL;
138 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (width != 8 && width != 16) {
141 printk(KERN_ERR NAME ": invalid bus width for flash mapping\n");
142 return -EINVAL;
143 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000144
145 if (allocate_resource(&iomem_resource, &docmem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 size,
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000147 0xc0000000, 0xffffffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 size, NULL, NULL)) {
149 printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
150 return -ENOMEM;
151 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 ctrl = 0x07000000 | ((size-1) >> 13);
154
155 printk(KERN_INFO "DOCCS BASE=0x%08lx, CTRL=0x%08lx\n", (long)docmem.start, (long)ctrl);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 pci_write_config_dword(bridge, SCx200_DOCCS_BASE, docmem.start);
158 pci_write_config_dword(bridge, SCx200_DOCCS_CTRL, ctrl);
159 pmr = inl(scx200_cb_base + SCx200_PMR);
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (width == 8) {
162 pmr &= ~(1<<6);
163 } else {
164 pmr |= (1<<6);
165 }
166 outl(pmr, scx200_cb_base + SCx200_PMR);
167 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000168
Greg Kroah-Hartman176dfc62006-06-12 15:15:17 -0700169 printk(KERN_INFO NAME ": DOCCS mapped at 0x%llx-0x%llx, width %d\n",
170 (unsigned long long)docmem.start,
171 (unsigned long long)docmem.end, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 scx200_docflash_map.size = size;
174 if (width == 8)
175 scx200_docflash_map.bankwidth = 1;
176 else
177 scx200_docflash_map.bankwidth = 2;
178
179 simple_map_init(&scx200_docflash_map);
180
181 scx200_docflash_map.phys = docmem.start;
182 scx200_docflash_map.virt = ioremap(docmem.start, scx200_docflash_map.size);
183 if (!scx200_docflash_map.virt) {
184 printk(KERN_ERR NAME ": failed to ioremap the flash\n");
185 release_resource(&docmem);
186 return -EIO;
187 }
188
189 mymtd = do_map_probe(flashtype, &scx200_docflash_map);
190 if (!mymtd) {
191 printk(KERN_ERR NAME ": unable to detect flash\n");
192 iounmap(scx200_docflash_map.virt);
193 release_resource(&docmem);
194 return -ENXIO;
195 }
196
197 if (size < mymtd->size)
198 printk(KERN_WARNING NAME ": warning, flash mapping is smaller than flash size\n");
199
200 mymtd->owner = THIS_MODULE;
201
202#ifdef CONFIG_MTD_PARTITIONS
203 partition_info[3].offset = mymtd->size-partition_info[3].size;
204 partition_info[2].size = partition_info[3].offset-partition_info[2].offset;
205 add_mtd_partitions(mymtd, partition_info, NUM_PARTITIONS);
206#else
207 add_mtd_device(mymtd);
208#endif
209 return 0;
210}
211
212static void __exit cleanup_scx200_docflash(void)
213{
214 if (mymtd) {
215#ifdef CONFIG_MTD_PARTITIONS
216 del_mtd_partitions(mymtd);
217#else
218 del_mtd_device(mymtd);
219#endif
220 map_destroy(mymtd);
221 }
222 if (scx200_docflash_map.virt) {
223 iounmap(scx200_docflash_map.virt);
224 release_resource(&docmem);
225 }
226}
227
228module_init(init_scx200_docflash);
229module_exit(cleanup_scx200_docflash);
230
231/*
232 Local variables:
233 compile-command: "make -k -C ../../.. SUBDIRS=drivers/mtd/maps modules"
234 c-basic-offset: 8
235 End:
236*/