amd76xrom.c 9.2 KB

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