esb2rom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * esb2rom.c
  3. *
  4. * Normal mappings of flash chips in physical memory
  5. * through the Intel ESB2 Southbridge.
  6. *
  7. * This was derived from ichxrom.c in May 2006 by
  8. * Lew Glendenning <lglendenning@lnxi.com>
  9. *
  10. * Eric Biederman, of course, was a major help in this effort.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/version.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <asm/io.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/map.h>
  20. #include <linux/mtd/cfi.h>
  21. #include <linux/mtd/flashchip.h>
  22. #include <linux/pci.h>
  23. #include <linux/pci_ids.h>
  24. #include <linux/list.h>
  25. #define MOD_NAME KBUILD_BASENAME
  26. #define ADDRESS_NAME_LEN 18
  27. #define ROM_PROBE_STEP_SIZE (64*1024) /* 64KiB */
  28. #define BIOS_CNTL 0xDC
  29. #define BIOS_LOCK_ENABLE 0x02
  30. #define BIOS_WRITE_ENABLE 0x01
  31. /* This became a 16-bit register, and EN2 has disappeared */
  32. #define FWH_DEC_EN1 0xD8
  33. #define FWH_F8_EN 0x8000
  34. #define FWH_F0_EN 0x4000
  35. #define FWH_E8_EN 0x2000
  36. #define FWH_E0_EN 0x1000
  37. #define FWH_D8_EN 0x0800
  38. #define FWH_D0_EN 0x0400
  39. #define FWH_C8_EN 0x0200
  40. #define FWH_C0_EN 0x0100
  41. #define FWH_LEGACY_F_EN 0x0080
  42. #define FWH_LEGACY_E_EN 0x0040
  43. /* reserved 0x0020 and 0x0010 */
  44. #define FWH_70_EN 0x0008
  45. #define FWH_60_EN 0x0004
  46. #define FWH_50_EN 0x0002
  47. #define FWH_40_EN 0x0001
  48. /* these are 32-bit values */
  49. #define FWH_SEL1 0xD0
  50. #define FWH_SEL2 0xD4
  51. #define FWH_8MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  52. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  53. FWH_70_EN | FWH_60_EN | FWH_50_EN | FWH_40_EN)
  54. #define FWH_7MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  55. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  56. FWH_70_EN | FWH_60_EN | FWH_50_EN)
  57. #define FWH_6MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  58. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  59. FWH_70_EN | FWH_60_EN)
  60. #define FWH_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  61. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  62. FWH_70_EN)
  63. #define FWH_4MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  64. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN)
  65. #define FWH_3_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  66. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN)
  67. #define FWH_3MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  68. FWH_D8_EN | FWH_D0_EN)
  69. #define FWH_2_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  70. FWH_D8_EN)
  71. #define FWH_2MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN)
  72. #define FWH_1_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN)
  73. #define FWH_1MiB (FWH_F8_EN | FWH_F0_EN)
  74. #define FWH_0_5MiB (FWH_F8_EN)
  75. struct esb2rom_window {
  76. void __iomem* virt;
  77. unsigned long phys;
  78. unsigned long size;
  79. struct list_head maps;
  80. struct resource rsrc;
  81. struct pci_dev *pdev;
  82. };
  83. struct esb2rom_map_info {
  84. struct list_head list;
  85. struct map_info map;
  86. struct mtd_info *mtd;
  87. struct resource rsrc;
  88. char map_name[sizeof(MOD_NAME) + 2 + ADDRESS_NAME_LEN];
  89. };
  90. static struct esb2rom_window esb2rom_window = {
  91. .maps = LIST_HEAD_INIT(esb2rom_window.maps),
  92. };
  93. static void esb2rom_cleanup(struct esb2rom_window *window)
  94. {
  95. struct esb2rom_map_info *map, *scratch;
  96. u8 byte;
  97. /* Disable writes through the rom window */
  98. pci_read_config_byte(window->pdev, BIOS_CNTL, &byte);
  99. pci_write_config_byte(window->pdev, BIOS_CNTL,
  100. byte & ~BIOS_WRITE_ENABLE);
  101. /* Free all of the mtd devices */
  102. list_for_each_entry_safe(map, scratch, &window->maps, list) {
  103. if (map->rsrc.parent)
  104. release_resource(&map->rsrc);
  105. del_mtd_device(map->mtd);
  106. map_destroy(map->mtd);
  107. list_del(&map->list);
  108. kfree(map);
  109. }
  110. if (window->rsrc.parent)
  111. release_resource(&window->rsrc);
  112. if (window->virt) {
  113. iounmap(window->virt);
  114. window->virt = NULL;
  115. window->phys = 0;
  116. window->size = 0;
  117. }
  118. pci_dev_put(window->pdev);
  119. }
  120. static int __devinit esb2rom_init_one(struct pci_dev *pdev,
  121. const struct pci_device_id *ent)
  122. {
  123. static char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
  124. struct esb2rom_window *window = &esb2rom_window;
  125. struct esb2rom_map_info *map = NULL;
  126. unsigned long map_top;
  127. u8 byte;
  128. u16 word;
  129. /* For now I just handle the ecb2 and I assume there
  130. * are not a lot of resources up at the top of the address
  131. * space. It is possible to handle other devices in the
  132. * top 16MiB but it is very painful. Also since
  133. * you can only really attach a FWH to an ICHX there
  134. * a number of simplifications you can make.
  135. *
  136. * Also you can page firmware hubs if an 8MiB window isn't enough
  137. * but don't currently handle that case either.
  138. */
  139. window->pdev = pci_dev_get(pdev);
  140. /* RLG: experiment 2. Force the window registers to the widest values */
  141. /*
  142. pci_read_config_word(pdev, FWH_DEC_EN1, &word);
  143. printk(KERN_DEBUG "Original FWH_DEC_EN1 : %x\n", word);
  144. pci_write_config_byte(pdev, FWH_DEC_EN1, 0xff);
  145. pci_read_config_byte(pdev, FWH_DEC_EN1, &byte);
  146. printk(KERN_DEBUG "New FWH_DEC_EN1 : %x\n", byte);
  147. pci_read_config_byte(pdev, FWH_DEC_EN2, &byte);
  148. printk(KERN_DEBUG "Original FWH_DEC_EN2 : %x\n", byte);
  149. pci_write_config_byte(pdev, FWH_DEC_EN2, 0x0f);
  150. pci_read_config_byte(pdev, FWH_DEC_EN2, &byte);
  151. printk(KERN_DEBUG "New FWH_DEC_EN2 : %x\n", byte);
  152. */
  153. /* Find a region continuous to the end of the ROM window */
  154. window->phys = 0;
  155. pci_read_config_word(pdev, FWH_DEC_EN1, &word);
  156. printk(KERN_DEBUG "pci_read_config_byte : %x\n", word);
  157. if ((word & FWH_8MiB) == FWH_8MiB)
  158. window->phys = 0xff400000;
  159. else if ((word & FWH_7MiB) == FWH_7MiB)
  160. window->phys = 0xff500000;
  161. else if ((word & FWH_6MiB) == FWH_6MiB)
  162. window->phys = 0xff600000;
  163. else if ((word & FWH_5MiB) == FWH_5MiB)
  164. window->phys = 0xFF700000;
  165. else if ((word & FWH_4MiB) == FWH_4MiB)
  166. window->phys = 0xffc00000;
  167. else if ((word & FWH_3_5MiB) == FWH_3_5MiB)
  168. window->phys = 0xffc80000;
  169. else if ((word & FWH_3MiB) == FWH_3MiB)
  170. window->phys = 0xffd00000;
  171. else if ((word & FWH_2_5MiB) == FWH_2_5MiB)
  172. window->phys = 0xffd80000;
  173. else if ((word & FWH_2MiB) == FWH_2MiB)
  174. window->phys = 0xffe00000;
  175. else if ((word & FWH_1_5MiB) == FWH_1_5MiB)
  176. window->phys = 0xffe80000;
  177. else if ((word & FWH_1MiB) == FWH_1MiB)
  178. window->phys = 0xfff00000;
  179. else if ((word & FWH_0_5MiB) == FWH_0_5MiB)
  180. window->phys = 0xfff80000;
  181. /* reserved 0x0020 and 0x0010 */
  182. window->phys -= 0x400000UL;
  183. window->size = (0xffffffffUL - window->phys) + 1UL;
  184. /* Enable writes through the rom window */
  185. pci_read_config_byte(pdev, BIOS_CNTL, &byte);
  186. if (!(byte & BIOS_WRITE_ENABLE) && (byte & (BIOS_LOCK_ENABLE))) {
  187. /* The BIOS will generate an error if I enable
  188. * this device, so don't even try.
  189. */
  190. printk(KERN_ERR MOD_NAME ": firmware access control, I can't enable writes\n");
  191. goto out;
  192. }
  193. pci_write_config_byte(pdev, BIOS_CNTL, byte | BIOS_WRITE_ENABLE);
  194. /*
  195. * Try to reserve the window mem region. If this fails then
  196. * it is likely due to the window being "reseved" by the BIOS.
  197. */
  198. window->rsrc.name = MOD_NAME;
  199. window->rsrc.start = window->phys;
  200. window->rsrc.end = window->phys + window->size - 1;
  201. window->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  202. if (request_resource(&iomem_resource, &window->rsrc)) {
  203. window->rsrc.parent = NULL;
  204. printk(KERN_DEBUG MOD_NAME
  205. ": %s(): Unable to register resource"
  206. " 0x%.08llx-0x%.08llx - kernel bug?\n",
  207. __func__,
  208. (unsigned long long)window->rsrc.start,
  209. (unsigned long long)window->rsrc.end);
  210. }
  211. /* Map the firmware hub into my address space. */
  212. window->virt = ioremap_nocache(window->phys, window->size);
  213. if (!window->virt) {
  214. printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n",
  215. window->phys, window->size);
  216. goto out;
  217. }
  218. /* Get the first address to look for an rom chip at */
  219. map_top = window->phys;
  220. if ((window->phys & 0x3fffff) != 0) {
  221. /* if not aligned on 4MiB, look 4MiB lower in address space */
  222. map_top = window->phys + 0x400000;
  223. }
  224. #if 1
  225. /* The probe sequence run over the firmware hub lock
  226. * registers sets them to 0x7 (no access).
  227. * (Insane hardware design, but most copied Intel's.)
  228. * ==> Probe at most the last 4M of the address space.
  229. */
  230. if (map_top < 0xffc00000)
  231. map_top = 0xffc00000;
  232. #endif
  233. /* Loop through and look for rom chips */
  234. while ((map_top - 1) < 0xffffffffUL) {
  235. struct cfi_private *cfi;
  236. unsigned long offset;
  237. int i;
  238. if (!map)
  239. map = kmalloc(sizeof(*map), GFP_KERNEL);
  240. if (!map) {
  241. printk(KERN_ERR MOD_NAME ": kmalloc failed");
  242. goto out;
  243. }
  244. memset(map, 0, sizeof(*map));
  245. INIT_LIST_HEAD(&map->list);
  246. map->map.name = map->map_name;
  247. map->map.phys = map_top;
  248. offset = map_top - window->phys;
  249. map->map.virt = (void __iomem *)
  250. (((unsigned long)(window->virt)) + offset);
  251. map->map.size = 0xffffffffUL - map_top + 1UL;
  252. /* Set the name of the map to the address I am trying */
  253. sprintf(map->map_name, "%s @%08lx",
  254. MOD_NAME, map->map.phys);
  255. /* Firmware hubs only use vpp when being programmed
  256. * in a factory setting. So in-place programming
  257. * needs to use a different method.
  258. */
  259. for(map->map.bankwidth = 32; map->map.bankwidth;
  260. map->map.bankwidth >>= 1) {
  261. char **probe_type;
  262. /* Skip bankwidths that are not supported */
  263. if (!map_bankwidth_supported(map->map.bankwidth))
  264. continue;
  265. /* Setup the map methods */
  266. simple_map_init(&map->map);
  267. /* Try all of the probe methods */
  268. probe_type = rom_probe_types;
  269. for(; *probe_type; probe_type++) {
  270. map->mtd = do_map_probe(*probe_type, &map->map);
  271. if (map->mtd)
  272. goto found;
  273. }
  274. }
  275. map_top += ROM_PROBE_STEP_SIZE;
  276. continue;
  277. found:
  278. /* Trim the size if we are larger than the map */
  279. if (map->mtd->size > map->map.size) {
  280. printk(KERN_WARNING MOD_NAME
  281. " rom(%u) larger than window(%lu). fixing...\n",
  282. map->mtd->size, map->map.size);
  283. map->mtd->size = map->map.size;
  284. }
  285. if (window->rsrc.parent) {
  286. /*
  287. * Registering the MTD device in iomem may not be possible
  288. * if there is a BIOS "reserved" and BUSY range. If this
  289. * fails then continue anyway.
  290. */
  291. map->rsrc.name = map->map_name;
  292. map->rsrc.start = map->map.phys;
  293. map->rsrc.end = map->map.phys + map->mtd->size - 1;
  294. map->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  295. if (request_resource(&window->rsrc, &map->rsrc)) {
  296. printk(KERN_ERR MOD_NAME
  297. ": cannot reserve MTD resource\n");
  298. map->rsrc.parent = NULL;
  299. }
  300. }
  301. /* Make the whole region visible in the map */
  302. map->map.virt = window->virt;
  303. map->map.phys = window->phys;
  304. cfi = map->map.fldrv_priv;
  305. for(i = 0; i < cfi->numchips; i++)
  306. cfi->chips[i].start += offset;
  307. /* Now that the mtd devices is complete claim and export it */
  308. map->mtd->owner = THIS_MODULE;
  309. if (add_mtd_device(map->mtd)) {
  310. map_destroy(map->mtd);
  311. map->mtd = NULL;
  312. goto out;
  313. }
  314. /* Calculate the new value of map_top */
  315. map_top += map->mtd->size;
  316. /* File away the map structure */
  317. list_add(&map->list, &window->maps);
  318. map = NULL;
  319. }
  320. out:
  321. /* Free any left over map structures */
  322. kfree(map);
  323. /* See if I have any map structures */
  324. if (list_empty(&window->maps)) {
  325. esb2rom_cleanup(window);
  326. return -ENODEV;
  327. }
  328. return 0;
  329. }
  330. static void __devexit esb2rom_remove_one (struct pci_dev *pdev)
  331. {
  332. struct esb2rom_window *window = &esb2rom_window;
  333. esb2rom_cleanup(window);
  334. }
  335. static struct pci_device_id esb2rom_pci_tbl[] __devinitdata = {
  336. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0,
  337. PCI_ANY_ID, PCI_ANY_ID, },
  338. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0,
  339. PCI_ANY_ID, PCI_ANY_ID, },
  340. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0,
  341. PCI_ANY_ID, PCI_ANY_ID, },
  342. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0,
  343. PCI_ANY_ID, PCI_ANY_ID, },
  344. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1,
  345. PCI_ANY_ID, PCI_ANY_ID, },
  346. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_0,
  347. PCI_ANY_ID, PCI_ANY_ID, },
  348. { 0, },
  349. };
  350. #if 0
  351. MODULE_DEVICE_TABLE(pci, esb2rom_pci_tbl);
  352. static struct pci_driver esb2rom_driver = {
  353. .name = MOD_NAME,
  354. .id_table = esb2rom_pci_tbl,
  355. .probe = esb2rom_init_one,
  356. .remove = esb2rom_remove_one,
  357. };
  358. #endif
  359. static int __init init_esb2rom(void)
  360. {
  361. struct pci_dev *pdev;
  362. struct pci_device_id *id;
  363. int retVal;
  364. pdev = NULL;
  365. for (id = esb2rom_pci_tbl; id->vendor; id++) {
  366. printk(KERN_DEBUG "device id = %x\n", id->device);
  367. pdev = pci_get_device(id->vendor, id->device, NULL);
  368. if (pdev) {
  369. printk(KERN_DEBUG "matched device = %x\n", id->device);
  370. break;
  371. }
  372. }
  373. if (pdev) {
  374. printk(KERN_DEBUG "matched device id %x\n", id->device);
  375. retVal = esb2rom_init_one(pdev, &esb2rom_pci_tbl[0]);
  376. pci_dev_put(pdev);
  377. printk(KERN_DEBUG "retVal = %d\n", retVal);
  378. return retVal;
  379. }
  380. return -ENXIO;
  381. #if 0
  382. return pci_register_driver(&esb2rom_driver);
  383. #endif
  384. }
  385. static void __exit cleanup_esb2rom(void)
  386. {
  387. esb2rom_remove_one(esb2rom_window.pdev);
  388. }
  389. module_init(init_esb2rom);
  390. module_exit(cleanup_esb2rom);
  391. MODULE_LICENSE("GPL");
  392. MODULE_AUTHOR("Lew Glendenning <lglendenning@lnxi.com>");
  393. MODULE_DESCRIPTION("MTD map driver for BIOS chips on the ESB2 southbridge");