ck804xrom.c 11 KB

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