ck804xrom.c 11 KB

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