4 * Normal mappings of chips in physical memory
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/map.h>
15 #include <linux/mtd/cfi.h>
16 #include <linux/mtd/flashchip.h>
17 #include <linux/pci.h>
18 #include <linux/pci_ids.h>
19 #include <linux/list.h>
21 #define xstr(s) str(s)
23 #define MOD_NAME xstr(KBUILD_BASENAME)
25 #define ADDRESS_NAME_LEN 18
27 #define ROM_PROBE_STEP_SIZE (64*1024) /* 64KiB */
29 #define BIOS_CNTL 0x4e
30 #define FWH_DEC_EN1 0xE3
31 #define FWH_DEC_EN2 0xF0
35 struct ichxrom_window
{
39 struct list_head maps
;
44 struct ichxrom_map_info
{
45 struct list_head list
;
49 char map_name
[sizeof(MOD_NAME
) + 2 + ADDRESS_NAME_LEN
];
52 static struct ichxrom_window ichxrom_window
= {
53 .maps
= LIST_HEAD_INIT(ichxrom_window
.maps
),
56 static void ichxrom_cleanup(struct ichxrom_window
*window
)
58 struct ichxrom_map_info
*map
, *scratch
;
61 /* Disable writes through the rom window */
62 pci_read_config_word(window
->pdev
, BIOS_CNTL
, &word
);
63 pci_write_config_word(window
->pdev
, BIOS_CNTL
, word
& ~1);
64 pci_dev_put(window
->pdev
);
66 /* Free all of the mtd devices */
67 list_for_each_entry_safe(map
, scratch
, &window
->maps
, list
) {
69 release_resource(&map
->rsrc
);
70 mtd_device_unregister(map
->mtd
);
71 map_destroy(map
->mtd
);
75 if (window
->rsrc
.parent
)
76 release_resource(&window
->rsrc
);
78 iounmap(window
->virt
);
87 static int ichxrom_init_one(struct pci_dev
*pdev
,
88 const struct pci_device_id
*ent
)
90 static char *rom_probe_types
[] = { "cfi_probe", "jedec_probe", NULL
};
91 struct ichxrom_window
*window
= &ichxrom_window
;
92 struct ichxrom_map_info
*map
= NULL
;
93 unsigned long map_top
;
97 /* For now I just handle the ichx and I assume there
98 * are not a lot of resources up at the top of the address
99 * space. It is possible to handle other devices in the
100 * top 16MB but it is very painful. Also since
101 * you can only really attach a FWH to an ICHX there
102 * a number of simplifications you can make.
104 * Also you can page firmware hubs if an 8MB window isn't enough
105 * but don't currently handle that case either.
109 /* Find a region continuous to the end of the ROM window */
111 pci_read_config_byte(pdev
, FWH_DEC_EN1
, &byte
);
113 window
->phys
= 0xffc00000;
114 pci_read_config_byte(pdev
, FWH_DEC_EN2
, &byte
);
115 if ((byte
& 0x0f) == 0x0f) {
116 window
->phys
= 0xff400000;
118 else if ((byte
& 0x0e) == 0x0e) {
119 window
->phys
= 0xff500000;
121 else if ((byte
& 0x0c) == 0x0c) {
122 window
->phys
= 0xff600000;
124 else if ((byte
& 0x08) == 0x08) {
125 window
->phys
= 0xff700000;
128 else if ((byte
& 0xfe) == 0xfe) {
129 window
->phys
= 0xffc80000;
131 else if ((byte
& 0xfc) == 0xfc) {
132 window
->phys
= 0xffd00000;
134 else if ((byte
& 0xf8) == 0xf8) {
135 window
->phys
= 0xffd80000;
137 else if ((byte
& 0xf0) == 0xf0) {
138 window
->phys
= 0xffe00000;
140 else if ((byte
& 0xe0) == 0xe0) {
141 window
->phys
= 0xffe80000;
143 else if ((byte
& 0xc0) == 0xc0) {
144 window
->phys
= 0xfff00000;
146 else if ((byte
& 0x80) == 0x80) {
147 window
->phys
= 0xfff80000;
150 if (window
->phys
== 0) {
151 printk(KERN_ERR MOD_NAME
": Rom window is closed\n");
154 window
->phys
-= 0x400000UL
;
155 window
->size
= (0xffffffffUL
- window
->phys
) + 1UL;
157 /* Enable writes through the rom window */
158 pci_read_config_word(pdev
, BIOS_CNTL
, &word
);
159 if (!(word
& 1) && (word
& (1<<1))) {
160 /* The BIOS will generate an error if I enable
161 * this device, so don't even try.
163 printk(KERN_ERR MOD_NAME
": firmware access control, I can't enable writes\n");
166 pci_write_config_word(pdev
, BIOS_CNTL
, word
| 1);
169 * Try to reserve the window mem region. If this fails then
170 * it is likely due to the window being "reseved" by the BIOS.
172 window
->rsrc
.name
= MOD_NAME
;
173 window
->rsrc
.start
= window
->phys
;
174 window
->rsrc
.end
= window
->phys
+ window
->size
- 1;
175 window
->rsrc
.flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
176 if (request_resource(&iomem_resource
, &window
->rsrc
)) {
177 window
->rsrc
.parent
= NULL
;
178 printk(KERN_DEBUG MOD_NAME
": "
179 "%s(): Unable to register resource %pR - kernel bug?\n",
180 __func__
, &window
->rsrc
);
183 /* Map the firmware hub into my address space. */
184 window
->virt
= ioremap_nocache(window
->phys
, window
->size
);
186 printk(KERN_ERR MOD_NAME
": ioremap(%08lx, %08lx) failed\n",
187 window
->phys
, window
->size
);
191 /* Get the first address to look for an rom chip at */
192 map_top
= window
->phys
;
193 if ((window
->phys
& 0x3fffff) != 0) {
194 map_top
= window
->phys
+ 0x400000;
197 /* The probe sequence run over the firmware hub lock
198 * registers sets them to 0x7 (no access).
199 * Probe at most the last 4M of the address space.
201 if (map_top
< 0xffc00000) {
202 map_top
= 0xffc00000;
205 /* Loop through and look for rom chips */
206 while((map_top
- 1) < 0xffffffffUL
) {
207 struct cfi_private
*cfi
;
208 unsigned long offset
;
212 map
= kmalloc(sizeof(*map
), GFP_KERNEL
);
215 printk(KERN_ERR MOD_NAME
": kmalloc failed");
218 memset(map
, 0, sizeof(*map
));
219 INIT_LIST_HEAD(&map
->list
);
220 map
->map
.name
= map
->map_name
;
221 map
->map
.phys
= map_top
;
222 offset
= map_top
- window
->phys
;
223 map
->map
.virt
= (void __iomem
*)
224 (((unsigned long)(window
->virt
)) + offset
);
225 map
->map
.size
= 0xffffffffUL
- map_top
+ 1UL;
226 /* Set the name of the map to the address I am trying */
227 sprintf(map
->map_name
, "%s @%08Lx",
228 MOD_NAME
, (unsigned long long)map
->map
.phys
);
230 /* Firmware hubs only use vpp when being programmed
231 * in a factory setting. So in-place programming
232 * needs to use a different method.
234 for(map
->map
.bankwidth
= 32; map
->map
.bankwidth
;
235 map
->map
.bankwidth
>>= 1)
238 /* Skip bankwidths that are not supported */
239 if (!map_bankwidth_supported(map
->map
.bankwidth
))
242 /* Setup the map methods */
243 simple_map_init(&map
->map
);
245 /* Try all of the probe methods */
246 probe_type
= rom_probe_types
;
247 for(; *probe_type
; probe_type
++) {
248 map
->mtd
= do_map_probe(*probe_type
, &map
->map
);
253 map_top
+= ROM_PROBE_STEP_SIZE
;
256 /* Trim the size if we are larger than the map */
257 if (map
->mtd
->size
> map
->map
.size
) {
258 printk(KERN_WARNING MOD_NAME
259 " rom(%llu) larger than window(%lu). fixing...\n",
260 (unsigned long long)map
->mtd
->size
, map
->map
.size
);
261 map
->mtd
->size
= map
->map
.size
;
263 if (window
->rsrc
.parent
) {
265 * Registering the MTD device in iomem may not be possible
266 * if there is a BIOS "reserved" and BUSY range. If this
267 * fails then continue anyway.
269 map
->rsrc
.name
= map
->map_name
;
270 map
->rsrc
.start
= map
->map
.phys
;
271 map
->rsrc
.end
= map
->map
.phys
+ map
->mtd
->size
- 1;
272 map
->rsrc
.flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
273 if (request_resource(&window
->rsrc
, &map
->rsrc
)) {
274 printk(KERN_ERR MOD_NAME
275 ": cannot reserve MTD resource\n");
276 map
->rsrc
.parent
= NULL
;
280 /* Make the whole region visible in the map */
281 map
->map
.virt
= window
->virt
;
282 map
->map
.phys
= window
->phys
;
283 cfi
= map
->map
.fldrv_priv
;
284 for(i
= 0; i
< cfi
->numchips
; i
++) {
285 cfi
->chips
[i
].start
+= offset
;
288 /* Now that the mtd devices is complete claim and export it */
289 map
->mtd
->owner
= THIS_MODULE
;
290 if (mtd_device_register(map
->mtd
, NULL
, 0)) {
291 map_destroy(map
->mtd
);
297 /* Calculate the new value of map_top */
298 map_top
+= map
->mtd
->size
;
300 /* File away the map structure */
301 list_add(&map
->list
, &window
->maps
);
306 /* Free any left over map structures */
309 /* See if I have any map structures */
310 if (list_empty(&window
->maps
)) {
311 ichxrom_cleanup(window
);
318 static void ichxrom_remove_one(struct pci_dev
*pdev
)
320 struct ichxrom_window
*window
= &ichxrom_window
;
321 ichxrom_cleanup(window
);
324 static struct pci_device_id ichxrom_pci_tbl
[] = {
325 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801BA_0
,
326 PCI_ANY_ID
, PCI_ANY_ID
, },
327 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801CA_0
,
328 PCI_ANY_ID
, PCI_ANY_ID
, },
329 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801DB_0
,
330 PCI_ANY_ID
, PCI_ANY_ID
, },
331 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801EB_0
,
332 PCI_ANY_ID
, PCI_ANY_ID
, },
333 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ESB_1
,
334 PCI_ANY_ID
, PCI_ANY_ID
, },
339 MODULE_DEVICE_TABLE(pci
, ichxrom_pci_tbl
);
341 static struct pci_driver ichxrom_driver
= {
343 .id_table
= ichxrom_pci_tbl
,
344 .probe
= ichxrom_init_one
,
345 .remove
= ichxrom_remove_one
,
349 static int __init
init_ichxrom(void)
351 struct pci_dev
*pdev
;
352 struct pci_device_id
*id
;
355 for (id
= ichxrom_pci_tbl
; id
->vendor
; id
++) {
356 pdev
= pci_get_device(id
->vendor
, id
->device
, NULL
);
362 return ichxrom_init_one(pdev
, &ichxrom_pci_tbl
[0]);
366 return pci_register_driver(&ichxrom_driver
);
370 static void __exit
cleanup_ichxrom(void)
372 ichxrom_remove_one(ichxrom_window
.pdev
);
375 module_init(init_ichxrom
);
376 module_exit(cleanup_ichxrom
);
378 MODULE_LICENSE("GPL");
379 MODULE_AUTHOR("Eric Biederman <ebiederman@lnxi.com>");
380 MODULE_DESCRIPTION("MTD map driver for BIOS chips on the ICHX southbridge");