esb2rom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.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. mtd_device_unregister(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 __init 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_word : %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. if (window->phys == 0) {
  182. printk(KERN_ERR MOD_NAME ": Rom window is closed\n");
  183. goto out;
  184. }
  185. /* reserved 0x0020 and 0x0010 */
  186. window->phys -= 0x400000UL;
  187. window->size = (0xffffffffUL - window->phys) + 1UL;
  188. /* Enable writes through the rom window */
  189. pci_read_config_byte(pdev, BIOS_CNTL, &byte);
  190. if (!(byte & BIOS_WRITE_ENABLE) && (byte & (BIOS_LOCK_ENABLE))) {
  191. /* The BIOS will generate an error if I enable
  192. * this device, so don't even try.
  193. */
  194. printk(KERN_ERR MOD_NAME ": firmware access control, I can't enable writes\n");
  195. goto out;
  196. }
  197. pci_write_config_byte(pdev, BIOS_CNTL, byte | BIOS_WRITE_ENABLE);
  198. /*
  199. * Try to reserve the window mem region. If this fails then
  200. * it is likely due to the window being "reserved" by the BIOS.
  201. */
  202. window->rsrc.name = MOD_NAME;
  203. window->rsrc.start = window->phys;
  204. window->rsrc.end = window->phys + window->size - 1;
  205. window->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  206. if (request_resource(&iomem_resource, &window->rsrc)) {
  207. window->rsrc.parent = NULL;
  208. printk(KERN_DEBUG MOD_NAME ": "
  209. "%s(): Unable to register resource %pR - kernel bug?\n",
  210. __func__, &window->rsrc);
  211. }
  212. /* Map the firmware hub into my address space. */
  213. window->virt = ioremap_nocache(window->phys, window->size);
  214. if (!window->virt) {
  215. printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n",
  216. window->phys, window->size);
  217. goto out;
  218. }
  219. /* Get the first address to look for an rom chip at */
  220. map_top = window->phys;
  221. if ((window->phys & 0x3fffff) != 0) {
  222. /* if not aligned on 4MiB, look 4MiB lower in address space */
  223. map_top = window->phys + 0x400000;
  224. }
  225. #if 1
  226. /* The probe sequence run over the firmware hub lock
  227. * registers sets them to 0x7 (no access).
  228. * (Insane hardware design, but most copied Intel's.)
  229. * ==> Probe at most the last 4M of the address space.
  230. */
  231. if (map_top < 0xffc00000)
  232. map_top = 0xffc00000;
  233. #endif
  234. /* Loop through and look for rom chips */
  235. while ((map_top - 1) < 0xffffffffUL) {
  236. struct cfi_private *cfi;
  237. unsigned long offset;
  238. int i;
  239. if (!map)
  240. map = kmalloc(sizeof(*map), GFP_KERNEL);
  241. if (!map) {
  242. printk(KERN_ERR MOD_NAME ": kmalloc failed");
  243. goto out;
  244. }
  245. memset(map, 0, sizeof(*map));
  246. INIT_LIST_HEAD(&map->list);
  247. map->map.name = map->map_name;
  248. map->map.phys = map_top;
  249. offset = map_top - window->phys;
  250. map->map.virt = (void __iomem *)
  251. (((unsigned long)(window->virt)) + offset);
  252. map->map.size = 0xffffffffUL - map_top + 1UL;
  253. /* Set the name of the map to the address I am trying */
  254. sprintf(map->map_name, "%s @%08Lx",
  255. MOD_NAME, (unsigned long long)map->map.phys);
  256. /* Firmware hubs only use vpp when being programmed
  257. * in a factory setting. So in-place programming
  258. * needs to use a different method.
  259. */
  260. for(map->map.bankwidth = 32; map->map.bankwidth;
  261. map->map.bankwidth >>= 1) {
  262. char **probe_type;
  263. /* Skip bankwidths that are not supported */
  264. if (!map_bankwidth_supported(map->map.bankwidth))
  265. continue;
  266. /* Setup the map methods */
  267. simple_map_init(&map->map);
  268. /* Try all of the probe methods */
  269. probe_type = rom_probe_types;
  270. for(; *probe_type; probe_type++) {
  271. map->mtd = do_map_probe(*probe_type, &map->map);
  272. if (map->mtd)
  273. goto found;
  274. }
  275. }
  276. map_top += ROM_PROBE_STEP_SIZE;
  277. continue;
  278. found:
  279. /* Trim the size if we are larger than the map */
  280. if (map->mtd->size > map->map.size) {
  281. printk(KERN_WARNING MOD_NAME
  282. " rom(%llu) larger than window(%lu). fixing...\n",
  283. (unsigned long long)map->mtd->size, map->map.size);
  284. map->mtd->size = map->map.size;
  285. }
  286. if (window->rsrc.parent) {
  287. /*
  288. * Registering the MTD device in iomem may not be possible
  289. * if there is a BIOS "reserved" and BUSY range. If this
  290. * fails then continue anyway.
  291. */
  292. map->rsrc.name = map->map_name;
  293. map->rsrc.start = map->map.phys;
  294. map->rsrc.end = map->map.phys + map->mtd->size - 1;
  295. map->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  296. if (request_resource(&window->rsrc, &map->rsrc)) {
  297. printk(KERN_ERR MOD_NAME
  298. ": cannot reserve MTD resource\n");
  299. map->rsrc.parent = NULL;
  300. }
  301. }
  302. /* Make the whole region visible in the map */
  303. map->map.virt = window->virt;
  304. map->map.phys = window->phys;
  305. cfi = map->map.fldrv_priv;
  306. for(i = 0; i < cfi->numchips; i++)
  307. cfi->chips[i].start += offset;
  308. /* Now that the mtd devices is complete claim and export it */
  309. map->mtd->owner = THIS_MODULE;
  310. if (mtd_device_register(map->mtd, NULL, 0)) {
  311. map_destroy(map->mtd);
  312. map->mtd = NULL;
  313. goto out;
  314. }
  315. /* Calculate the new value of map_top */
  316. map_top += map->mtd->size;
  317. /* File away the map structure */
  318. list_add(&map->list, &window->maps);
  319. map = NULL;
  320. }
  321. out:
  322. /* Free any left over map structures */
  323. kfree(map);
  324. /* See if I have any map structures */
  325. if (list_empty(&window->maps)) {
  326. esb2rom_cleanup(window);
  327. return -ENODEV;
  328. }
  329. return 0;
  330. }
  331. static void esb2rom_remove_one(struct pci_dev *pdev)
  332. {
  333. struct esb2rom_window *window = &esb2rom_window;
  334. esb2rom_cleanup(window);
  335. }
  336. static const struct pci_device_id esb2rom_pci_tbl[] = {
  337. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0,
  338. PCI_ANY_ID, PCI_ANY_ID, },
  339. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0,
  340. PCI_ANY_ID, PCI_ANY_ID, },
  341. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0,
  342. PCI_ANY_ID, PCI_ANY_ID, },
  343. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0,
  344. PCI_ANY_ID, PCI_ANY_ID, },
  345. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1,
  346. PCI_ANY_ID, PCI_ANY_ID, },
  347. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_0,
  348. PCI_ANY_ID, PCI_ANY_ID, },
  349. { 0, },
  350. };
  351. #if 0
  352. MODULE_DEVICE_TABLE(pci, esb2rom_pci_tbl);
  353. static struct pci_driver esb2rom_driver = {
  354. .name = MOD_NAME,
  355. .id_table = esb2rom_pci_tbl,
  356. .probe = esb2rom_init_one,
  357. .remove = esb2rom_remove_one,
  358. };
  359. #endif
  360. static int __init init_esb2rom(void)
  361. {
  362. struct pci_dev *pdev;
  363. const struct pci_device_id *id;
  364. int retVal;
  365. pdev = NULL;
  366. for (id = esb2rom_pci_tbl; id->vendor; id++) {
  367. printk(KERN_DEBUG "device id = %x\n", id->device);
  368. pdev = pci_get_device(id->vendor, id->device, NULL);
  369. if (pdev) {
  370. printk(KERN_DEBUG "matched device = %x\n", id->device);
  371. break;
  372. }
  373. }
  374. if (pdev) {
  375. printk(KERN_DEBUG "matched device id %x\n", id->device);
  376. retVal = esb2rom_init_one(pdev, &esb2rom_pci_tbl[0]);
  377. pci_dev_put(pdev);
  378. printk(KERN_DEBUG "retVal = %d\n", retVal);
  379. return retVal;
  380. }
  381. return -ENXIO;
  382. #if 0
  383. return pci_register_driver(&esb2rom_driver);
  384. #endif
  385. }
  386. static void __exit cleanup_esb2rom(void)
  387. {
  388. esb2rom_remove_one(esb2rom_window.pdev);
  389. }
  390. module_init(init_esb2rom);
  391. module_exit(cleanup_esb2rom);
  392. MODULE_LICENSE("GPL");
  393. MODULE_AUTHOR("Lew Glendenning <lglendenning@lnxi.com>");
  394. MODULE_DESCRIPTION("MTD map driver for BIOS chips on the ESB2 southbridge");