amd76xrom.c 8.3 KB

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