ichxrom.c 9.7 KB

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