esb2rom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * esb2rom.c
  4. *
  5. * Normal mappings of flash chips in physical memory
  6. * through the Intel ESB2 Southbridge.
  7. *
  8. * This was derived from ichxrom.c in May 2006 by
  9. * Lew Glendenning <[email protected]>
  10. *
  11. * Eric Biederman, of course, was a major help in this effort.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <asm/io.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/map.h>
  21. #include <linux/mtd/cfi.h>
  22. #include <linux/mtd/flashchip.h>
  23. #include <linux/pci.h>
  24. #include <linux/pci_ids.h>
  25. #include <linux/list.h>
  26. #define MOD_NAME KBUILD_BASENAME
  27. #define ADDRESS_NAME_LEN 18
  28. #define ROM_PROBE_STEP_SIZE (64*1024) /* 64KiB */
  29. #define BIOS_CNTL 0xDC
  30. #define BIOS_LOCK_ENABLE 0x02
  31. #define BIOS_WRITE_ENABLE 0x01
  32. /* This became a 16-bit register, and EN2 has disappeared */
  33. #define FWH_DEC_EN1 0xD8
  34. #define FWH_F8_EN 0x8000
  35. #define FWH_F0_EN 0x4000
  36. #define FWH_E8_EN 0x2000
  37. #define FWH_E0_EN 0x1000
  38. #define FWH_D8_EN 0x0800
  39. #define FWH_D0_EN 0x0400
  40. #define FWH_C8_EN 0x0200
  41. #define FWH_C0_EN 0x0100
  42. #define FWH_LEGACY_F_EN 0x0080
  43. #define FWH_LEGACY_E_EN 0x0040
  44. /* reserved 0x0020 and 0x0010 */
  45. #define FWH_70_EN 0x0008
  46. #define FWH_60_EN 0x0004
  47. #define FWH_50_EN 0x0002
  48. #define FWH_40_EN 0x0001
  49. /* these are 32-bit values */
  50. #define FWH_SEL1 0xD0
  51. #define FWH_SEL2 0xD4
  52. #define FWH_8MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  53. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  54. FWH_70_EN | FWH_60_EN | FWH_50_EN | FWH_40_EN)
  55. #define FWH_7MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  56. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  57. FWH_70_EN | FWH_60_EN | FWH_50_EN)
  58. #define FWH_6MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  59. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  60. FWH_70_EN | FWH_60_EN)
  61. #define FWH_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  62. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
  63. FWH_70_EN)
  64. #define FWH_4MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  65. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN)
  66. #define FWH_3_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  67. FWH_D8_EN | FWH_D0_EN | FWH_C8_EN)
  68. #define FWH_3MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  69. FWH_D8_EN | FWH_D0_EN)
  70. #define FWH_2_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
  71. FWH_D8_EN)
  72. #define FWH_2MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN)
  73. #define FWH_1_5MiB (FWH_F8_EN | FWH_F0_EN | FWH_E8_EN)
  74. #define FWH_1MiB (FWH_F8_EN | FWH_F0_EN)
  75. #define FWH_0_5MiB (FWH_F8_EN)
  76. struct esb2rom_window {
  77. void __iomem* virt;
  78. unsigned long phys;
  79. unsigned long size;
  80. struct list_head maps;
  81. struct resource rsrc;
  82. struct pci_dev *pdev;
  83. };
  84. struct esb2rom_map_info {
  85. struct list_head list;
  86. struct map_info map;
  87. struct mtd_info *mtd;
  88. struct resource rsrc;
  89. char map_name[sizeof(MOD_NAME) + 2 + ADDRESS_NAME_LEN];
  90. };
  91. static struct esb2rom_window esb2rom_window = {
  92. .maps = LIST_HEAD_INIT(esb2rom_window.maps),
  93. };
  94. static void esb2rom_cleanup(struct esb2rom_window *window)
  95. {
  96. struct esb2rom_map_info *map, *scratch;
  97. u8 byte;
  98. /* Disable writes through the rom window */
  99. pci_read_config_byte(window->pdev, BIOS_CNTL, &byte);
  100. pci_write_config_byte(window->pdev, BIOS_CNTL,
  101. byte & ~BIOS_WRITE_ENABLE);
  102. /* Free all of the mtd devices */
  103. list_for_each_entry_safe(map, scratch, &window->maps, list) {
  104. if (map->rsrc.parent)
  105. release_resource(&map->rsrc);
  106. mtd_device_unregister(map->mtd);
  107. map_destroy(map->mtd);
  108. list_del(&map->list);
  109. kfree(map);
  110. }
  111. if (window->rsrc.parent)
  112. release_resource(&window->rsrc);
  113. if (window->virt) {
  114. iounmap(window->virt);
  115. window->virt = NULL;
  116. window->phys = 0;
  117. window->size = 0;
  118. }
  119. pci_dev_put(window->pdev);
  120. }
  121. static int __init esb2rom_init_one(struct pci_dev *pdev,
  122. const struct pci_device_id *ent)
  123. {
  124. static char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
  125. struct esb2rom_window *window = &esb2rom_window;
  126. struct esb2rom_map_info *map = NULL;
  127. unsigned long map_top;
  128. u8 byte;
  129. u16 word;
  130. /* For now I just handle the ecb2 and I assume there
  131. * are not a lot of resources up at the top of the address
  132. * space. It is possible to handle other devices in the
  133. * top 16MiB but it is very painful. Also since
  134. * you can only really attach a FWH to an ICHX there
  135. * a number of simplifications you can make.
  136. *
  137. * Also you can page firmware hubs if an 8MiB window isn't enough
  138. * but don't currently handle that case either.
  139. */
  140. window->pdev = pci_dev_get(pdev);
  141. /* RLG: experiment 2. Force the window registers to the widest values */
  142. /*
  143. pci_read_config_word(pdev, FWH_DEC_EN1, &word);
  144. printk(KERN_DEBUG "Original FWH_DEC_EN1 : %x\n", word);
  145. pci_write_config_byte(pdev, FWH_DEC_EN1, 0xff);
  146. pci_read_config_byte(pdev, FWH_DEC_EN1, &byte);
  147. printk(KERN_DEBUG "New FWH_DEC_EN1 : %x\n", byte);
  148. pci_read_config_byte(pdev, FWH_DEC_EN2, &byte);
  149. printk(KERN_DEBUG "Original FWH_DEC_EN2 : %x\n", byte);
  150. pci_write_config_byte(pdev, FWH_DEC_EN2, 0x0f);
  151. pci_read_config_byte(pdev, FWH_DEC_EN2, &byte);
  152. printk(KERN_DEBUG "New FWH_DEC_EN2 : %x\n", byte);
  153. */
  154. /* Find a region continuous to the end of the ROM window */
  155. window->phys = 0;
  156. pci_read_config_word(pdev, FWH_DEC_EN1, &word);
  157. printk(KERN_DEBUG "pci_read_config_word : %x\n", word);
  158. if ((word & FWH_8MiB) == FWH_8MiB)
  159. window->phys = 0xff400000;
  160. else if ((word & FWH_7MiB) == FWH_7MiB)
  161. window->phys = 0xff500000;
  162. else if ((word & FWH_6MiB) == FWH_6MiB)
  163. window->phys = 0xff600000;
  164. else if ((word & FWH_5MiB) == FWH_5MiB)
  165. window->phys = 0xFF700000;
  166. else if ((word & FWH_4MiB) == FWH_4MiB)
  167. window->phys = 0xffc00000;
  168. else if ((word & FWH_3_5MiB) == FWH_3_5MiB)
  169. window->phys = 0xffc80000;
  170. else if ((word & FWH_3MiB) == FWH_3MiB)
  171. window->phys = 0xffd00000;
  172. else if ((word & FWH_2_5MiB) == FWH_2_5MiB)
  173. window->phys = 0xffd80000;
  174. else if ((word & FWH_2MiB) == FWH_2MiB)
  175. window->phys = 0xffe00000;
  176. else if ((word & FWH_1_5MiB) == FWH_1_5MiB)
  177. window->phys = 0xffe80000;
  178. else if ((word & FWH_1MiB) == FWH_1MiB)
  179. window->phys = 0xfff00000;
  180. else if ((word & FWH_0_5MiB) == FWH_0_5MiB)
  181. window->phys = 0xfff80000;
  182. if (window->phys == 0) {
  183. printk(KERN_ERR MOD_NAME ": Rom window is closed\n");
  184. goto out;
  185. }
  186. /* reserved 0x0020 and 0x0010 */
  187. window->phys -= 0x400000UL;
  188. window->size = (0xffffffffUL - window->phys) + 1UL;
  189. /* Enable writes through the rom window */
  190. pci_read_config_byte(pdev, BIOS_CNTL, &byte);
  191. if (!(byte & BIOS_WRITE_ENABLE) && (byte & (BIOS_LOCK_ENABLE))) {
  192. /* The BIOS will generate an error if I enable
  193. * this device, so don't even try.
  194. */
  195. printk(KERN_ERR MOD_NAME ": firmware access control, I can't enable writes\n");
  196. goto out;
  197. }
  198. pci_write_config_byte(pdev, BIOS_CNTL, byte | BIOS_WRITE_ENABLE);
  199. /*
  200. * Try to reserve the window mem region. If this fails then
  201. * it is likely due to the window being "reserved" by the BIOS.
  202. */
  203. window->rsrc.name = MOD_NAME;
  204. window->rsrc.start = window->phys;
  205. window->rsrc.end = window->phys + window->size - 1;
  206. window->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  207. if (request_resource(&iomem_resource, &window->rsrc)) {
  208. window->rsrc.parent = NULL;
  209. printk(KERN_DEBUG MOD_NAME ": "
  210. "%s(): Unable to register resource %pR - kernel bug?\n",
  211. __func__, &window->rsrc);
  212. }
  213. /* Map the firmware hub into my address space. */
  214. window->virt = ioremap(window->phys, window->size);
  215. if (!window->virt) {
  216. printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n",
  217. window->phys, window->size);
  218. goto out;
  219. }
  220. /* Get the first address to look for an rom chip at */
  221. map_top = window->phys;
  222. if ((window->phys & 0x3fffff) != 0) {
  223. /* if not aligned on 4MiB, look 4MiB lower in address space */
  224. map_top = window->phys + 0x400000;
  225. }
  226. #if 1
  227. /* The probe sequence run over the firmware hub lock
  228. * registers sets them to 0x7 (no access).
  229. * (Insane hardware design, but most copied Intel's.)
  230. * ==> Probe at most the last 4M of the address space.
  231. */
  232. if (map_top < 0xffc00000)
  233. map_top = 0xffc00000;
  234. #endif
  235. /* Loop through and look for rom chips */
  236. while ((map_top - 1) < 0xffffffffUL) {
  237. struct cfi_private *cfi;
  238. unsigned long offset;
  239. int i;
  240. if (!map) {
  241. map = kmalloc(sizeof(*map), GFP_KERNEL);
  242. if (!map)
  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 <[email protected]>");
  394. MODULE_DESCRIPTION("MTD map driver for BIOS chips on the ESB2 southbridge");