amd76xrom.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * amd76xrom.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. struct amd76xrom_window {
  25. void __iomem *virt;
  26. unsigned long phys;
  27. unsigned long size;
  28. struct list_head maps;
  29. struct resource rsrc;
  30. struct pci_dev *pdev;
  31. };
  32. struct amd76xrom_map_info {
  33. struct list_head list;
  34. struct map_info map;
  35. struct mtd_info *mtd;
  36. struct resource rsrc;
  37. char map_name[sizeof(MOD_NAME) + 2 + ADDRESS_NAME_LEN];
  38. };
  39. /* The 2 bits controlling the window size are often set to allow reading
  40. * the BIOS, but too small to allow writing, since the lock registers are
  41. * 4MiB lower in the address space than the data.
  42. *
  43. * This is intended to prevent flashing the bios, perhaps accidentally.
  44. *
  45. * This parameter allows the normal driver to over-ride the BIOS settings.
  46. *
  47. * The bits are 6 and 7. If both bits are set, it is a 5MiB window.
  48. * If only the 7 Bit is set, it is a 4MiB window. Otherwise, a
  49. * 64KiB window.
  50. *
  51. */
  52. static uint win_size_bits;
  53. module_param(win_size_bits, uint, 0);
  54. MODULE_PARM_DESC(win_size_bits, "ROM window size bits override for 0x43 byte, normally set by BIOS.");
  55. static struct amd76xrom_window amd76xrom_window = {
  56. .maps = LIST_HEAD_INIT(amd76xrom_window.maps),
  57. };
  58. static void amd76xrom_cleanup(struct amd76xrom_window *window)
  59. {
  60. struct amd76xrom_map_info *map, *scratch;
  61. u8 byte;
  62. if (window->pdev) {
  63. /* Disable writes through the rom window */
  64. pci_read_config_byte(window->pdev, 0x40, &byte);
  65. pci_write_config_byte(window->pdev, 0x40, byte & ~1);
  66. pci_dev_put(window->pdev);
  67. }
  68. /* Free all of the mtd devices */
  69. list_for_each_entry_safe(map, scratch, &window->maps, list) {
  70. if (map->rsrc.parent) {
  71. release_resource(&map->rsrc);
  72. }
  73. mtd_device_unregister(map->mtd);
  74. map_destroy(map->mtd);
  75. list_del(&map->list);
  76. kfree(map);
  77. }
  78. if (window->rsrc.parent)
  79. release_resource(&window->rsrc);
  80. if (window->virt) {
  81. iounmap(window->virt);
  82. window->virt = NULL;
  83. window->phys = 0;
  84. window->size = 0;
  85. window->pdev = NULL;
  86. }
  87. }
  88. static int amd76xrom_init_one(struct pci_dev *pdev,
  89. const struct pci_device_id *ent)
  90. {
  91. static char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
  92. u8 byte;
  93. struct amd76xrom_window *window = &amd76xrom_window;
  94. struct amd76xrom_map_info *map = NULL;
  95. unsigned long map_top;
  96. /* Remember the pci dev I find the window in - already have a ref */
  97. window->pdev = pdev;
  98. /* Enable the selected rom window. This is often incorrectly
  99. * set up by the BIOS, and the 4MiB offset for the lock registers
  100. * requires the full 5MiB of window space.
  101. *
  102. * This 'write, then read' approach leaves the bits for
  103. * other uses of the hardware info.
  104. */
  105. pci_read_config_byte(pdev, 0x43, &byte);
  106. pci_write_config_byte(pdev, 0x43, byte | win_size_bits );
  107. /* Assume the rom window is properly setup, and find it's size */
  108. pci_read_config_byte(pdev, 0x43, &byte);
  109. if ((byte & ((1<<7)|(1<<6))) == ((1<<7)|(1<<6))) {
  110. window->phys = 0xffb00000; /* 5MiB */
  111. }
  112. else if ((byte & (1<<7)) == (1<<7)) {
  113. window->phys = 0xffc00000; /* 4MiB */
  114. }
  115. else {
  116. window->phys = 0xffff0000; /* 64KiB */
  117. }
  118. window->size = 0xffffffffUL - window->phys + 1UL;
  119. /*
  120. * Try to reserve the window mem region. If this fails then
  121. * it is likely due to a fragment of the window being
  122. * "reserved" by the BIOS. In the case that the
  123. * request_mem_region() fails then once the rom size is
  124. * discovered we will try to reserve the unreserved fragment.
  125. */
  126. window->rsrc.name = MOD_NAME;
  127. window->rsrc.start = window->phys;
  128. window->rsrc.end = window->phys + window->size - 1;
  129. window->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  130. if (request_resource(&iomem_resource, &window->rsrc)) {
  131. window->rsrc.parent = NULL;
  132. printk(KERN_ERR MOD_NAME
  133. " %s(): Unable to register resource %pR - kernel bug?\n",
  134. __func__, &window->rsrc);
  135. return -EBUSY;
  136. }
  137. /* Enable writes through the rom window */
  138. pci_read_config_byte(pdev, 0x40, &byte);
  139. pci_write_config_byte(pdev, 0x40, byte | 1);
  140. /* FIXME handle registers 0x80 - 0x8C the bios region locks */
  141. /* For write accesses caches are useless */
  142. window->virt = ioremap_nocache(window->phys, window->size);
  143. if (!window->virt) {
  144. printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n",
  145. window->phys, window->size);
  146. goto out;
  147. }
  148. /* Get the first address to look for an rom chip at */
  149. map_top = window->phys;
  150. #if 1
  151. /* The probe sequence run over the firmware hub lock
  152. * registers sets them to 0x7 (no access).
  153. * Probe at most the last 4M of the address space.
  154. */
  155. if (map_top < 0xffc00000) {
  156. map_top = 0xffc00000;
  157. }
  158. #endif
  159. /* Loop through and look for rom chips */
  160. while((map_top - 1) < 0xffffffffUL) {
  161. struct cfi_private *cfi;
  162. unsigned long offset;
  163. int i;
  164. if (!map) {
  165. map = kmalloc(sizeof(*map), GFP_KERNEL);
  166. }
  167. if (!map) {
  168. printk(KERN_ERR MOD_NAME ": kmalloc failed");
  169. goto out;
  170. }
  171. memset(map, 0, sizeof(*map));
  172. INIT_LIST_HEAD(&map->list);
  173. map->map.name = map->map_name;
  174. map->map.phys = map_top;
  175. offset = map_top - window->phys;
  176. map->map.virt = (void __iomem *)
  177. (((unsigned long)(window->virt)) + offset);
  178. map->map.size = 0xffffffffUL - map_top + 1UL;
  179. /* Set the name of the map to the address I am trying */
  180. sprintf(map->map_name, "%s @%08Lx",
  181. MOD_NAME, (unsigned long long)map->map.phys);
  182. /* There is no generic VPP support */
  183. for(map->map.bankwidth = 32; map->map.bankwidth;
  184. map->map.bankwidth >>= 1)
  185. {
  186. char **probe_type;
  187. /* Skip bankwidths that are not supported */
  188. if (!map_bankwidth_supported(map->map.bankwidth))
  189. continue;
  190. /* Setup the map methods */
  191. simple_map_init(&map->map);
  192. /* Try all of the probe methods */
  193. probe_type = rom_probe_types;
  194. for(; *probe_type; probe_type++) {
  195. map->mtd = do_map_probe(*probe_type, &map->map);
  196. if (map->mtd)
  197. goto found;
  198. }
  199. }
  200. map_top += ROM_PROBE_STEP_SIZE;
  201. continue;
  202. found:
  203. /* Trim the size if we are larger than the map */
  204. if (map->mtd->size > map->map.size) {
  205. printk(KERN_WARNING MOD_NAME
  206. " rom(%llu) larger than window(%lu). fixing...\n",
  207. (unsigned long long)map->mtd->size, map->map.size);
  208. map->mtd->size = map->map.size;
  209. }
  210. if (window->rsrc.parent) {
  211. /*
  212. * Registering the MTD device in iomem may not be possible
  213. * if there is a BIOS "reserved" and BUSY range. If this
  214. * fails then continue anyway.
  215. */
  216. map->rsrc.name = map->map_name;
  217. map->rsrc.start = map->map.phys;
  218. map->rsrc.end = map->map.phys + map->mtd->size - 1;
  219. map->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  220. if (request_resource(&window->rsrc, &map->rsrc)) {
  221. printk(KERN_ERR MOD_NAME
  222. ": cannot reserve MTD resource\n");
  223. map->rsrc.parent = NULL;
  224. }
  225. }
  226. /* Make the whole region visible in the map */
  227. map->map.virt = window->virt;
  228. map->map.phys = window->phys;
  229. cfi = map->map.fldrv_priv;
  230. for(i = 0; i < cfi->numchips; i++) {
  231. cfi->chips[i].start += offset;
  232. }
  233. /* Now that the mtd devices is complete claim and export it */
  234. map->mtd->owner = THIS_MODULE;
  235. if (mtd_device_register(map->mtd, NULL, 0)) {
  236. map_destroy(map->mtd);
  237. map->mtd = NULL;
  238. goto out;
  239. }
  240. /* Calculate the new value of map_top */
  241. map_top += map->mtd->size;
  242. /* File away the map structure */
  243. list_add(&map->list, &window->maps);
  244. map = NULL;
  245. }
  246. out:
  247. /* Free any left over map structures */
  248. kfree(map);
  249. /* See if I have any map structures */
  250. if (list_empty(&window->maps)) {
  251. amd76xrom_cleanup(window);
  252. return -ENODEV;
  253. }
  254. return 0;
  255. }
  256. static void amd76xrom_remove_one(struct pci_dev *pdev)
  257. {
  258. struct amd76xrom_window *window = &amd76xrom_window;
  259. amd76xrom_cleanup(window);
  260. }
  261. static struct pci_device_id amd76xrom_pci_tbl[] = {
  262. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7410,
  263. PCI_ANY_ID, PCI_ANY_ID, },
  264. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7440,
  265. PCI_ANY_ID, PCI_ANY_ID, },
  266. { PCI_VENDOR_ID_AMD, 0x7468 }, /* amd8111 support */
  267. { 0, }
  268. };
  269. MODULE_DEVICE_TABLE(pci, amd76xrom_pci_tbl);
  270. #if 0
  271. static struct pci_driver amd76xrom_driver = {
  272. .name = MOD_NAME,
  273. .id_table = amd76xrom_pci_tbl,
  274. .probe = amd76xrom_init_one,
  275. .remove = amd76xrom_remove_one,
  276. };
  277. #endif
  278. static int __init init_amd76xrom(void)
  279. {
  280. struct pci_dev *pdev;
  281. struct pci_device_id *id;
  282. pdev = NULL;
  283. for(id = amd76xrom_pci_tbl; id->vendor; id++) {
  284. pdev = pci_get_device(id->vendor, id->device, NULL);
  285. if (pdev) {
  286. break;
  287. }
  288. }
  289. if (pdev) {
  290. return amd76xrom_init_one(pdev, &amd76xrom_pci_tbl[0]);
  291. }
  292. return -ENXIO;
  293. #if 0
  294. return pci_register_driver(&amd76xrom_driver);
  295. #endif
  296. }
  297. static void __exit cleanup_amd76xrom(void)
  298. {
  299. amd76xrom_remove_one(amd76xrom_window.pdev);
  300. }
  301. module_init(init_amd76xrom);
  302. module_exit(cleanup_amd76xrom);
  303. MODULE_LICENSE("GPL");
  304. MODULE_AUTHOR("Eric Biederman <ebiederman@lnxi.com>");
  305. MODULE_DESCRIPTION("MTD map driver for BIOS chips on the AMD76X southbridge");