esb2rom.c 13 KB

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