ichxrom.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ichxrom.c
  4. *
  5. * Normal mappings of chips in physical memory
  6. */
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.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/pci.h>
  18. #include <linux/pci_ids.h>
  19. #include <linux/list.h>
  20. #define xstr(s) str(s)
  21. #define str(s) #s
  22. #define MOD_NAME xstr(KBUILD_BASENAME)
  23. #define ADDRESS_NAME_LEN 18
  24. #define ROM_PROBE_STEP_SIZE (64*1024) /* 64KiB */
  25. #define BIOS_CNTL 0x4e
  26. #define FWH_DEC_EN1 0xE3
  27. #define FWH_DEC_EN2 0xF0
  28. #define FWH_SEL1 0xE8
  29. #define FWH_SEL2 0xEE
  30. struct ichxrom_window {
  31. void __iomem* virt;
  32. unsigned long phys;
  33. unsigned long size;
  34. struct list_head maps;
  35. struct resource rsrc;
  36. struct pci_dev *pdev;
  37. };
  38. struct ichxrom_map_info {
  39. struct list_head list;
  40. struct map_info map;
  41. struct mtd_info *mtd;
  42. struct resource rsrc;
  43. char map_name[sizeof(MOD_NAME) + 2 + ADDRESS_NAME_LEN];
  44. };
  45. static struct ichxrom_window ichxrom_window = {
  46. .maps = LIST_HEAD_INIT(ichxrom_window.maps),
  47. };
  48. static void ichxrom_cleanup(struct ichxrom_window *window)
  49. {
  50. struct ichxrom_map_info *map, *scratch;
  51. u16 word;
  52. int ret;
  53. /* Disable writes through the rom window */
  54. ret = pci_read_config_word(window->pdev, BIOS_CNTL, &word);
  55. if (!ret)
  56. pci_write_config_word(window->pdev, BIOS_CNTL, word & ~1);
  57. pci_dev_put(window->pdev);
  58. /* Free all of the mtd devices */
  59. list_for_each_entry_safe(map, scratch, &window->maps, list) {
  60. if (map->rsrc.parent)
  61. release_resource(&map->rsrc);
  62. mtd_device_unregister(map->mtd);
  63. map_destroy(map->mtd);
  64. list_del(&map->list);
  65. kfree(map);
  66. }
  67. if (window->rsrc.parent)
  68. release_resource(&window->rsrc);
  69. if (window->virt) {
  70. iounmap(window->virt);
  71. window->virt = NULL;
  72. window->phys = 0;
  73. window->size = 0;
  74. window->pdev = NULL;
  75. }
  76. }
  77. static int __init ichxrom_init_one(struct pci_dev *pdev,
  78. const struct pci_device_id *ent)
  79. {
  80. static char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
  81. struct ichxrom_window *window = &ichxrom_window;
  82. struct ichxrom_map_info *map = NULL;
  83. unsigned long map_top;
  84. u8 byte;
  85. u16 word;
  86. /* For now I just handle the ichx and I assume there
  87. * are not a lot of resources up at the top of the address
  88. * space. It is possible to handle other devices in the
  89. * top 16MB but it is very painful. Also since
  90. * you can only really attach a FWH to an ICHX there
  91. * a number of simplifications you can make.
  92. *
  93. * Also you can page firmware hubs if an 8MB window isn't enough
  94. * but don't currently handle that case either.
  95. */
  96. window->pdev = pdev;
  97. /* Find a region continuous to the end of the ROM window */
  98. window->phys = 0;
  99. pci_read_config_byte(pdev, FWH_DEC_EN1, &byte);
  100. if (byte == 0xff) {
  101. window->phys = 0xffc00000;
  102. pci_read_config_byte(pdev, FWH_DEC_EN2, &byte);
  103. if ((byte & 0x0f) == 0x0f) {
  104. window->phys = 0xff400000;
  105. }
  106. else if ((byte & 0x0e) == 0x0e) {
  107. window->phys = 0xff500000;
  108. }
  109. else if ((byte & 0x0c) == 0x0c) {
  110. window->phys = 0xff600000;
  111. }
  112. else if ((byte & 0x08) == 0x08) {
  113. window->phys = 0xff700000;
  114. }
  115. }
  116. else if ((byte & 0xfe) == 0xfe) {
  117. window->phys = 0xffc80000;
  118. }
  119. else if ((byte & 0xfc) == 0xfc) {
  120. window->phys = 0xffd00000;
  121. }
  122. else if ((byte & 0xf8) == 0xf8) {
  123. window->phys = 0xffd80000;
  124. }
  125. else if ((byte & 0xf0) == 0xf0) {
  126. window->phys = 0xffe00000;
  127. }
  128. else if ((byte & 0xe0) == 0xe0) {
  129. window->phys = 0xffe80000;
  130. }
  131. else if ((byte & 0xc0) == 0xc0) {
  132. window->phys = 0xfff00000;
  133. }
  134. else if ((byte & 0x80) == 0x80) {
  135. window->phys = 0xfff80000;
  136. }
  137. if (window->phys == 0) {
  138. printk(KERN_ERR MOD_NAME ": Rom window is closed\n");
  139. goto out;
  140. }
  141. window->phys -= 0x400000UL;
  142. window->size = (0xffffffffUL - window->phys) + 1UL;
  143. /* Enable writes through the rom window */
  144. pci_read_config_word(pdev, BIOS_CNTL, &word);
  145. if (!(word & 1) && (word & (1<<1))) {
  146. /* The BIOS will generate an error if I enable
  147. * this device, so don't even try.
  148. */
  149. printk(KERN_ERR MOD_NAME ": firmware access control, I can't enable writes\n");
  150. goto out;
  151. }
  152. pci_write_config_word(pdev, BIOS_CNTL, word | 1);
  153. /*
  154. * Try to reserve the window mem region. If this fails then
  155. * it is likely due to the window being "reserved" by the BIOS.
  156. */
  157. window->rsrc.name = MOD_NAME;
  158. window->rsrc.start = window->phys;
  159. window->rsrc.end = window->phys + window->size - 1;
  160. window->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  161. if (request_resource(&iomem_resource, &window->rsrc)) {
  162. window->rsrc.parent = NULL;
  163. printk(KERN_DEBUG MOD_NAME ": "
  164. "%s(): Unable to register resource %pR - kernel bug?\n",
  165. __func__, &window->rsrc);
  166. }
  167. /* Map the firmware hub into my address space. */
  168. window->virt = ioremap(window->phys, window->size);
  169. if (!window->virt) {
  170. printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n",
  171. window->phys, window->size);
  172. goto out;
  173. }
  174. /* Get the first address to look for an rom chip at */
  175. map_top = window->phys;
  176. if ((window->phys & 0x3fffff) != 0) {
  177. map_top = window->phys + 0x400000;
  178. }
  179. #if 1
  180. /* The probe sequence run over the firmware hub lock
  181. * registers sets them to 0x7 (no access).
  182. * Probe at most the last 4M of the address space.
  183. */
  184. if (map_top < 0xffc00000) {
  185. map_top = 0xffc00000;
  186. }
  187. #endif
  188. /* Loop through and look for rom chips */
  189. while((map_top - 1) < 0xffffffffUL) {
  190. struct cfi_private *cfi;
  191. unsigned long offset;
  192. int i;
  193. if (!map) {
  194. map = kmalloc(sizeof(*map), GFP_KERNEL);
  195. if (!map)
  196. goto out;
  197. }
  198. memset(map, 0, sizeof(*map));
  199. INIT_LIST_HEAD(&map->list);
  200. map->map.name = map->map_name;
  201. map->map.phys = map_top;
  202. offset = map_top - window->phys;
  203. map->map.virt = (void __iomem *)
  204. (((unsigned long)(window->virt)) + offset);
  205. map->map.size = 0xffffffffUL - map_top + 1UL;
  206. /* Set the name of the map to the address I am trying */
  207. sprintf(map->map_name, "%s @%08Lx",
  208. MOD_NAME, (unsigned long long)map->map.phys);
  209. /* Firmware hubs only use vpp when being programmed
  210. * in a factory setting. So in-place programming
  211. * needs to use a different method.
  212. */
  213. for(map->map.bankwidth = 32; map->map.bankwidth;
  214. map->map.bankwidth >>= 1)
  215. {
  216. char **probe_type;
  217. /* Skip bankwidths that are not supported */
  218. if (!map_bankwidth_supported(map->map.bankwidth))
  219. continue;
  220. /* Setup the map methods */
  221. simple_map_init(&map->map);
  222. /* Try all of the probe methods */
  223. probe_type = rom_probe_types;
  224. for(; *probe_type; probe_type++) {
  225. map->mtd = do_map_probe(*probe_type, &map->map);
  226. if (map->mtd)
  227. goto found;
  228. }
  229. }
  230. map_top += ROM_PROBE_STEP_SIZE;
  231. continue;
  232. found:
  233. /* Trim the size if we are larger than the map */
  234. if (map->mtd->size > map->map.size) {
  235. printk(KERN_WARNING MOD_NAME
  236. " rom(%llu) larger than window(%lu). fixing...\n",
  237. (unsigned long long)map->mtd->size, map->map.size);
  238. map->mtd->size = map->map.size;
  239. }
  240. if (window->rsrc.parent) {
  241. /*
  242. * Registering the MTD device in iomem may not be possible
  243. * if there is a BIOS "reserved" and BUSY range. If this
  244. * fails then continue anyway.
  245. */
  246. map->rsrc.name = map->map_name;
  247. map->rsrc.start = map->map.phys;
  248. map->rsrc.end = map->map.phys + map->mtd->size - 1;
  249. map->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  250. if (request_resource(&window->rsrc, &map->rsrc)) {
  251. printk(KERN_ERR MOD_NAME
  252. ": cannot reserve MTD resource\n");
  253. map->rsrc.parent = NULL;
  254. }
  255. }
  256. /* Make the whole region visible in the map */
  257. map->map.virt = window->virt;
  258. map->map.phys = window->phys;
  259. cfi = map->map.fldrv_priv;
  260. for(i = 0; i < cfi->numchips; i++) {
  261. cfi->chips[i].start += offset;
  262. }
  263. /* Now that the mtd devices is complete claim and export it */
  264. map->mtd->owner = THIS_MODULE;
  265. if (mtd_device_register(map->mtd, NULL, 0)) {
  266. map_destroy(map->mtd);
  267. map->mtd = NULL;
  268. goto out;
  269. }
  270. /* Calculate the new value of map_top */
  271. map_top += map->mtd->size;
  272. /* File away the map structure */
  273. list_add(&map->list, &window->maps);
  274. map = NULL;
  275. }
  276. out:
  277. /* Free any left over map structures */
  278. kfree(map);
  279. /* See if I have any map structures */
  280. if (list_empty(&window->maps)) {
  281. ichxrom_cleanup(window);
  282. return -ENODEV;
  283. }
  284. return 0;
  285. }
  286. static void ichxrom_remove_one(struct pci_dev *pdev)
  287. {
  288. struct ichxrom_window *window = &ichxrom_window;
  289. ichxrom_cleanup(window);
  290. }
  291. static const struct pci_device_id ichxrom_pci_tbl[] = {
  292. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0,
  293. PCI_ANY_ID, PCI_ANY_ID, },
  294. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0,
  295. PCI_ANY_ID, PCI_ANY_ID, },
  296. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0,
  297. PCI_ANY_ID, PCI_ANY_ID, },
  298. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0,
  299. PCI_ANY_ID, PCI_ANY_ID, },
  300. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1,
  301. PCI_ANY_ID, PCI_ANY_ID, },
  302. { 0, },
  303. };
  304. #if 0
  305. MODULE_DEVICE_TABLE(pci, ichxrom_pci_tbl);
  306. static struct pci_driver ichxrom_driver = {
  307. .name = MOD_NAME,
  308. .id_table = ichxrom_pci_tbl,
  309. .probe = ichxrom_init_one,
  310. .remove = ichxrom_remove_one,
  311. };
  312. #endif
  313. static int __init init_ichxrom(void)
  314. {
  315. struct pci_dev *pdev;
  316. const struct pci_device_id *id;
  317. pdev = NULL;
  318. for (id = ichxrom_pci_tbl; id->vendor; id++) {
  319. pdev = pci_get_device(id->vendor, id->device, NULL);
  320. if (pdev) {
  321. break;
  322. }
  323. }
  324. if (pdev) {
  325. return ichxrom_init_one(pdev, &ichxrom_pci_tbl[0]);
  326. }
  327. return -ENXIO;
  328. #if 0
  329. return pci_register_driver(&ichxrom_driver);
  330. #endif
  331. }
  332. static void __exit cleanup_ichxrom(void)
  333. {
  334. ichxrom_remove_one(ichxrom_window.pdev);
  335. }
  336. module_init(init_ichxrom);
  337. module_exit(cleanup_ichxrom);
  338. MODULE_LICENSE("GPL");
  339. MODULE_AUTHOR("Eric Biederman <[email protected]>");
  340. MODULE_DESCRIPTION("MTD map driver for BIOS chips on the ICHX southbridge");