| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * ichxrom.c | 
 | 3 |  * | 
 | 4 |  * Normal mappings of chips in physical memory | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 5 |  * $Id: ichxrom.c,v 1.19 2005/11/07 11:14:27 gleixner Exp $ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 |  */ | 
 | 7 |  | 
 | 8 | #include <linux/module.h> | 
 | 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/cfi.h> | 
 | 16 | #include <linux/mtd/flashchip.h> | 
 | 17 | #include <linux/config.h> | 
 | 18 | #include <linux/pci.h> | 
 | 19 | #include <linux/pci_ids.h> | 
 | 20 | #include <linux/list.h> | 
 | 21 |  | 
 | 22 | #define xstr(s) str(s) | 
 | 23 | #define str(s) #s | 
 | 24 | #define MOD_NAME xstr(KBUILD_BASENAME) | 
 | 25 |  | 
 | 26 | #define ADDRESS_NAME_LEN 18 | 
 | 27 |  | 
 | 28 | #define ROM_PROBE_STEP_SIZE (64*1024) /* 64KiB */ | 
 | 29 |  | 
 | 30 | #define BIOS_CNTL	0x4e | 
 | 31 | #define FWH_DEC_EN1	0xE3 | 
 | 32 | #define FWH_DEC_EN2	0xF0 | 
 | 33 | #define FWH_SEL1	0xE8 | 
 | 34 | #define FWH_SEL2	0xEE | 
 | 35 |  | 
 | 36 | struct ichxrom_window { | 
 | 37 | 	void __iomem* virt; | 
 | 38 | 	unsigned long phys; | 
 | 39 | 	unsigned long size; | 
 | 40 | 	struct list_head maps; | 
 | 41 | 	struct resource rsrc; | 
 | 42 | 	struct pci_dev *pdev; | 
 | 43 | }; | 
 | 44 |  | 
 | 45 | struct ichxrom_map_info { | 
 | 46 | 	struct list_head list; | 
 | 47 | 	struct map_info map; | 
 | 48 | 	struct mtd_info *mtd; | 
 | 49 | 	struct resource rsrc; | 
 | 50 | 	char map_name[sizeof(MOD_NAME) + 2 + ADDRESS_NAME_LEN]; | 
 | 51 | }; | 
 | 52 |  | 
 | 53 | static struct ichxrom_window ichxrom_window = { | 
 | 54 | 	.maps = LIST_HEAD_INIT(ichxrom_window.maps), | 
 | 55 | }; | 
 | 56 |  | 
 | 57 | static void ichxrom_cleanup(struct ichxrom_window *window) | 
 | 58 | { | 
 | 59 | 	struct ichxrom_map_info *map, *scratch; | 
 | 60 | 	u16 word; | 
 | 61 |  | 
 | 62 | 	/* Disable writes through the rom window */ | 
 | 63 | 	pci_read_config_word(window->pdev, BIOS_CNTL, &word); | 
 | 64 | 	pci_write_config_word(window->pdev, BIOS_CNTL, word & ~1); | 
 | 65 |  | 
 | 66 | 	/* Free all of the mtd devices */ | 
 | 67 | 	list_for_each_entry_safe(map, scratch, &window->maps, list) { | 
 | 68 | 		if (map->rsrc.parent) | 
 | 69 | 			release_resource(&map->rsrc); | 
 | 70 | 		del_mtd_device(map->mtd); | 
 | 71 | 		map_destroy(map->mtd); | 
 | 72 | 		list_del(&map->list); | 
 | 73 | 		kfree(map); | 
 | 74 | 	} | 
 | 75 | 	if (window->rsrc.parent) | 
 | 76 | 		release_resource(&window->rsrc); | 
 | 77 | 	if (window->virt) { | 
 | 78 | 		iounmap(window->virt); | 
 | 79 | 		window->virt = NULL; | 
 | 80 | 		window->phys = 0; | 
 | 81 | 		window->size = 0; | 
 | 82 | 		window->pdev = NULL; | 
 | 83 | 	} | 
 | 84 | } | 
 | 85 |  | 
 | 86 |  | 
 | 87 | static int __devinit ichxrom_init_one (struct pci_dev *pdev, | 
 | 88 | 	const struct pci_device_id *ent) | 
 | 89 | { | 
 | 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; | 
 | 94 | 	u8 byte; | 
 | 95 | 	u16 word; | 
 | 96 |  | 
 | 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. | 
 | 103 | 	 * | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 104 | 	 * Also you can page firmware hubs if an 8MB window isn't enough | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | 	 * but don't currently handle that case either. | 
 | 106 | 	 */ | 
 | 107 | 	window->pdev = pdev; | 
 | 108 |  | 
 | 109 | 	/* Find a region continuous to the end of the ROM window  */ | 
 | 110 | 	window->phys = 0; | 
 | 111 | 	pci_read_config_byte(pdev, FWH_DEC_EN1, &byte); | 
 | 112 | 	if (byte == 0xff) { | 
 | 113 | 		window->phys = 0xffc00000; | 
 | 114 | 		pci_read_config_byte(pdev, FWH_DEC_EN2, &byte); | 
 | 115 | 		if ((byte & 0x0f) == 0x0f) { | 
 | 116 | 			window->phys = 0xff400000; | 
 | 117 | 		} | 
 | 118 | 		else if ((byte & 0x0e) == 0x0e) { | 
 | 119 | 			window->phys = 0xff500000; | 
 | 120 | 		} | 
 | 121 | 		else if ((byte & 0x0c) == 0x0c) { | 
 | 122 | 			window->phys = 0xff600000; | 
 | 123 | 		} | 
 | 124 | 		else if ((byte & 0x08) == 0x08) { | 
 | 125 | 			window->phys = 0xff700000; | 
 | 126 | 		} | 
 | 127 | 	} | 
 | 128 | 	else if ((byte & 0xfe) == 0xfe) { | 
 | 129 | 		window->phys = 0xffc80000; | 
 | 130 | 	} | 
 | 131 | 	else if ((byte & 0xfc) == 0xfc) { | 
 | 132 | 		window->phys = 0xffd00000; | 
 | 133 | 	} | 
 | 134 | 	else if ((byte & 0xf8) == 0xf8) { | 
 | 135 | 		window->phys = 0xffd80000; | 
 | 136 | 	} | 
 | 137 | 	else if ((byte & 0xf0) == 0xf0) { | 
 | 138 | 		window->phys = 0xffe00000; | 
 | 139 | 	} | 
 | 140 | 	else if ((byte & 0xe0) == 0xe0) { | 
 | 141 | 		window->phys = 0xffe80000; | 
 | 142 | 	} | 
 | 143 | 	else if ((byte & 0xc0) == 0xc0) { | 
 | 144 | 		window->phys = 0xfff00000; | 
 | 145 | 	} | 
 | 146 | 	else if ((byte & 0x80) == 0x80) { | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 147 | 		window->phys = 0xfff80000; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 	} | 
 | 149 |  | 
 | 150 | 	if (window->phys == 0) { | 
 | 151 | 		printk(KERN_ERR MOD_NAME ": Rom window is closed\n"); | 
 | 152 | 		goto out; | 
 | 153 | 	} | 
 | 154 | 	window->phys -= 0x400000UL; | 
 | 155 | 	window->size = (0xffffffffUL - window->phys) + 1UL; | 
 | 156 |  | 
 | 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. | 
 | 162 | 		 */ | 
 | 163 | 		printk(KERN_ERR MOD_NAME ": firmware access control, I can't enable writes\n"); | 
 | 164 | 		goto out; | 
 | 165 | 	} | 
 | 166 | 	pci_write_config_word(pdev, BIOS_CNTL, word | 1); | 
 | 167 |  | 
 | 168 | 	/* | 
 | 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. | 
 | 171 | 	 */ | 
 | 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" | 
 | 180 | 			" 0x%.08lx-0x%.08lx - kernel bug?\n", | 
 | 181 | 			__func__, | 
 | 182 | 			window->rsrc.start, window->rsrc.end); | 
 | 183 | 	} | 
 | 184 |  | 
 | 185 | 	/* Map the firmware hub into my address space. */ | 
 | 186 | 	window->virt = ioremap_nocache(window->phys, window->size); | 
 | 187 | 	if (!window->virt) { | 
 | 188 | 		printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n", | 
 | 189 | 			window->phys, window->size); | 
 | 190 | 		goto out; | 
 | 191 | 	} | 
 | 192 |  | 
 | 193 | 	/* Get the first address to look for an rom chip at */ | 
 | 194 | 	map_top = window->phys; | 
 | 195 | 	if ((window->phys & 0x3fffff) != 0) { | 
 | 196 | 		map_top = window->phys + 0x400000; | 
 | 197 | 	} | 
 | 198 | #if 1 | 
 | 199 | 	/* The probe sequence run over the firmware hub lock | 
 | 200 | 	 * registers sets them to 0x7 (no access). | 
 | 201 | 	 * Probe at most the last 4M of the address space. | 
 | 202 | 	 */ | 
 | 203 | 	if (map_top < 0xffc00000) { | 
 | 204 | 		map_top = 0xffc00000; | 
 | 205 | 	} | 
 | 206 | #endif | 
 | 207 | 	/* Loop through and look for rom chips */ | 
 | 208 | 	while((map_top - 1) < 0xffffffffUL) { | 
 | 209 | 		struct cfi_private *cfi; | 
 | 210 | 		unsigned long offset; | 
 | 211 | 		int i; | 
 | 212 |  | 
 | 213 | 		if (!map) { | 
 | 214 | 			map = kmalloc(sizeof(*map), GFP_KERNEL); | 
 | 215 | 		} | 
 | 216 | 		if (!map) { | 
 | 217 | 			printk(KERN_ERR MOD_NAME ": kmalloc failed"); | 
 | 218 | 			goto out; | 
 | 219 | 		} | 
 | 220 | 		memset(map, 0, sizeof(*map)); | 
 | 221 | 		INIT_LIST_HEAD(&map->list); | 
 | 222 | 		map->map.name = map->map_name; | 
 | 223 | 		map->map.phys = map_top; | 
 | 224 | 		offset = map_top - window->phys; | 
 | 225 | 		map->map.virt = (void __iomem *) | 
 | 226 | 			(((unsigned long)(window->virt)) + offset); | 
 | 227 | 		map->map.size = 0xffffffffUL - map_top + 1UL; | 
 | 228 | 		/* Set the name of the map to the address I am trying */ | 
 | 229 | 		sprintf(map->map_name, "%s @%08lx", | 
 | 230 | 			MOD_NAME, map->map.phys); | 
 | 231 |  | 
 | 232 | 		/* Firmware hubs only use vpp when being programmed | 
 | 233 | 		 * in a factory setting.  So in-place programming | 
 | 234 | 		 * needs to use a different method. | 
 | 235 | 		 */ | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 236 | 		for(map->map.bankwidth = 32; map->map.bankwidth; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | 			map->map.bankwidth >>= 1) | 
 | 238 | 		{ | 
 | 239 | 			char **probe_type; | 
 | 240 | 			/* Skip bankwidths that are not supported */ | 
 | 241 | 			if (!map_bankwidth_supported(map->map.bankwidth)) | 
 | 242 | 				continue; | 
 | 243 |  | 
 | 244 | 			/* Setup the map methods */ | 
 | 245 | 			simple_map_init(&map->map); | 
 | 246 |  | 
 | 247 | 			/* Try all of the probe methods */ | 
 | 248 | 			probe_type = rom_probe_types; | 
 | 249 | 			for(; *probe_type; probe_type++) { | 
 | 250 | 				map->mtd = do_map_probe(*probe_type, &map->map); | 
 | 251 | 				if (map->mtd) | 
 | 252 | 					goto found; | 
 | 253 | 			} | 
 | 254 | 		} | 
 | 255 | 		map_top += ROM_PROBE_STEP_SIZE; | 
 | 256 | 		continue; | 
 | 257 | 	found: | 
 | 258 | 		/* Trim the size if we are larger than the map */ | 
 | 259 | 		if (map->mtd->size > map->map.size) { | 
 | 260 | 			printk(KERN_WARNING MOD_NAME | 
 | 261 | 				" rom(%u) larger than window(%lu). fixing...\n", | 
 | 262 | 				map->mtd->size, map->map.size); | 
 | 263 | 			map->mtd->size = map->map.size; | 
 | 264 | 		} | 
 | 265 | 		if (window->rsrc.parent) { | 
 | 266 | 			/* | 
 | 267 | 			 * Registering the MTD device in iomem may not be possible | 
 | 268 | 			 * if there is a BIOS "reserved" and BUSY range.  If this | 
 | 269 | 			 * fails then continue anyway. | 
 | 270 | 			 */ | 
 | 271 | 			map->rsrc.name  = map->map_name; | 
 | 272 | 			map->rsrc.start = map->map.phys; | 
 | 273 | 			map->rsrc.end   = map->map.phys + map->mtd->size - 1; | 
 | 274 | 			map->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY; | 
 | 275 | 			if (request_resource(&window->rsrc, &map->rsrc)) { | 
 | 276 | 				printk(KERN_ERR MOD_NAME | 
 | 277 | 					": cannot reserve MTD resource\n"); | 
 | 278 | 				map->rsrc.parent = NULL; | 
 | 279 | 			} | 
 | 280 | 		} | 
 | 281 |  | 
 | 282 | 		/* Make the whole region visible in the map */ | 
 | 283 | 		map->map.virt = window->virt; | 
 | 284 | 		map->map.phys = window->phys; | 
 | 285 | 		cfi = map->map.fldrv_priv; | 
 | 286 | 		for(i = 0; i < cfi->numchips; i++) { | 
 | 287 | 			cfi->chips[i].start += offset; | 
 | 288 | 		} | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 289 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 		/* Now that the mtd devices is complete claim and export it */ | 
 | 291 | 		map->mtd->owner = THIS_MODULE; | 
 | 292 | 		if (add_mtd_device(map->mtd)) { | 
 | 293 | 			map_destroy(map->mtd); | 
 | 294 | 			map->mtd = NULL; | 
 | 295 | 			goto out; | 
 | 296 | 		} | 
 | 297 |  | 
 | 298 |  | 
 | 299 | 		/* Calculate the new value of map_top */ | 
 | 300 | 		map_top += map->mtd->size; | 
 | 301 |  | 
 | 302 | 		/* File away the map structure */ | 
 | 303 | 		list_add(&map->list, &window->maps); | 
 | 304 | 		map = NULL; | 
 | 305 | 	} | 
 | 306 |  | 
 | 307 |  out: | 
 | 308 | 	/* Free any left over map structures */ | 
| Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 309 | 	kfree(map); | 
 | 310 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | 	/* See if I have any map structures */ | 
 | 312 | 	if (list_empty(&window->maps)) { | 
 | 313 | 		ichxrom_cleanup(window); | 
 | 314 | 		return -ENODEV; | 
 | 315 | 	} | 
 | 316 | 	return 0; | 
 | 317 | } | 
 | 318 |  | 
 | 319 |  | 
 | 320 | static void __devexit ichxrom_remove_one (struct pci_dev *pdev) | 
 | 321 | { | 
 | 322 | 	struct ichxrom_window *window = &ichxrom_window; | 
 | 323 | 	ichxrom_cleanup(window); | 
 | 324 | } | 
 | 325 |  | 
 | 326 | static struct pci_device_id ichxrom_pci_tbl[] __devinitdata = { | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 327 | 	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | 	  PCI_ANY_ID, PCI_ANY_ID, }, | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 329 | 	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 	  PCI_ANY_ID, PCI_ANY_ID, }, | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 331 | 	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | 	  PCI_ANY_ID, PCI_ANY_ID, }, | 
 | 333 | 	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, | 
 | 334 | 	  PCI_ANY_ID, PCI_ANY_ID, }, | 
 | 335 | 	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1, | 
 | 336 | 	  PCI_ANY_ID, PCI_ANY_ID, }, | 
 | 337 | 	{ 0, }, | 
 | 338 | }; | 
 | 339 |  | 
| David Woodhouse | b9c86d5 | 2005-07-07 11:26:24 +0100 | [diff] [blame] | 340 | #if 0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | MODULE_DEVICE_TABLE(pci, ichxrom_pci_tbl); | 
 | 342 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | static struct pci_driver ichxrom_driver = { | 
 | 344 | 	.name =		MOD_NAME, | 
 | 345 | 	.id_table =	ichxrom_pci_tbl, | 
 | 346 | 	.probe =	ichxrom_init_one, | 
 | 347 | 	.remove =	ichxrom_remove_one, | 
 | 348 | }; | 
 | 349 | #endif | 
 | 350 |  | 
 | 351 | static int __init init_ichxrom(void) | 
 | 352 | { | 
 | 353 | 	struct pci_dev *pdev; | 
 | 354 | 	struct pci_device_id *id; | 
 | 355 |  | 
 | 356 | 	pdev = NULL; | 
 | 357 | 	for (id = ichxrom_pci_tbl; id->vendor; id++) { | 
 | 358 | 		pdev = pci_find_device(id->vendor, id->device, NULL); | 
 | 359 | 		if (pdev) { | 
 | 360 | 			break; | 
 | 361 | 		} | 
 | 362 | 	} | 
 | 363 | 	if (pdev) { | 
 | 364 | 		return ichxrom_init_one(pdev, &ichxrom_pci_tbl[0]); | 
 | 365 | 	} | 
 | 366 | 	return -ENXIO; | 
 | 367 | #if 0 | 
| Domen Puncer | ff3bc4e | 2005-03-18 14:04:38 +0000 | [diff] [blame] | 368 | 	return pci_register_driver(&ichxrom_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | #endif | 
 | 370 | } | 
 | 371 |  | 
 | 372 | static void __exit cleanup_ichxrom(void) | 
 | 373 | { | 
 | 374 | 	ichxrom_remove_one(ichxrom_window.pdev); | 
 | 375 | } | 
 | 376 |  | 
 | 377 | module_init(init_ichxrom); | 
 | 378 | module_exit(cleanup_ichxrom); | 
 | 379 |  | 
 | 380 | MODULE_LICENSE("GPL"); | 
 | 381 | MODULE_AUTHOR("Eric Biederman <ebiederman@lnxi.com>"); | 
 | 382 | MODULE_DESCRIPTION("MTD map driver for BIOS chips on the ICHX southbridge"); |