scb2_flash.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * MTD map driver for BIOS Flash on Intel SCB2 boards
  3. * $Id: scb2_flash.c,v 1.12 2005/03/18 14:04:35 gleixner Exp $
  4. * Copyright (C) 2002 Sun Microsystems, Inc.
  5. * Tim Hockin <thockin@sun.com>
  6. *
  7. * A few notes on this MTD map:
  8. *
  9. * This was developed with a small number of SCB2 boards to test on.
  10. * Hopefully, Intel has not introducted too many unaccounted variables in the
  11. * making of this board.
  12. *
  13. * The BIOS marks its own memory region as 'reserved' in the e820 map. We
  14. * try to request it here, but if it fails, we carry on anyway.
  15. *
  16. * This is how the chip is attached, so said the schematic:
  17. * * a 4 MiB (32 Mib) 16 bit chip
  18. * * a 1 MiB memory region
  19. * * A20 and A21 pulled up
  20. * * D8-D15 ignored
  21. * What this means is that, while we are addressing bytes linearly, we are
  22. * really addressing words, and discarding the other byte. This means that
  23. * the chip MUST BE at least 2 MiB. This also means that every block is
  24. * actually half as big as the chip reports. It also means that accesses of
  25. * logical address 0 hit higher-address sections of the chip, not physical 0.
  26. * One can only hope that these 4MiB x16 chips were a lot cheaper than 1MiB x8
  27. * chips.
  28. *
  29. * This driver assumes the chip is not write-protected by an external signal.
  30. * As of the this writing, that is true, but may change, just to spite me.
  31. *
  32. * The actual BIOS layout has been mostly reverse engineered. Intel BIOS
  33. * updates for this board include 10 related (*.bio - &.bi9) binary files and
  34. * another separate (*.bbo) binary file. The 10 files are 64k of data + a
  35. * small header. If the headers are stripped off, the 10 64k files can be
  36. * concatenated into a 640k image. This is your BIOS image, proper. The
  37. * separate .bbo file also has a small header. It is the 'Boot Block'
  38. * recovery BIOS. Once the header is stripped, no further prep is needed.
  39. * As best I can tell, the BIOS is arranged as such:
  40. * offset 0x00000 to 0x4ffff (320k): unknown - SCSI BIOS, etc?
  41. * offset 0x50000 to 0xeffff (640k): BIOS proper
  42. * offset 0xf0000 ty 0xfffff (64k): Boot Block region
  43. *
  44. * Intel's BIOS update program flashes the BIOS and Boot Block in separate
  45. * steps. Probably a wise thing to do.
  46. */
  47. #include <linux/module.h>
  48. #include <linux/types.h>
  49. #include <linux/kernel.h>
  50. #include <linux/init.h>
  51. #include <asm/io.h>
  52. #include <linux/mtd/mtd.h>
  53. #include <linux/mtd/map.h>
  54. #include <linux/mtd/cfi.h>
  55. #include <linux/config.h>
  56. #include <linux/pci.h>
  57. #include <linux/pci_ids.h>
  58. #define MODNAME "scb2_flash"
  59. #define SCB2_ADDR 0xfff00000
  60. #define SCB2_WINDOW 0x00100000
  61. static void __iomem *scb2_ioaddr;
  62. static struct mtd_info *scb2_mtd;
  63. static struct map_info scb2_map = {
  64. .name = "SCB2 BIOS Flash",
  65. .size = 0,
  66. .bankwidth = 1,
  67. };
  68. static int region_fail;
  69. static int __devinit
  70. scb2_fixup_mtd(struct mtd_info *mtd)
  71. {
  72. int i;
  73. int done = 0;
  74. struct map_info *map = mtd->priv;
  75. struct cfi_private *cfi = map->fldrv_priv;
  76. /* barf if this doesn't look right */
  77. if (cfi->cfiq->InterfaceDesc != 1) {
  78. printk(KERN_ERR MODNAME ": unsupported InterfaceDesc: %#x\n",
  79. cfi->cfiq->InterfaceDesc);
  80. return -1;
  81. }
  82. /* I wasn't here. I didn't see. dwmw2. */
  83. /* the chip is sometimes bigger than the map - what a waste */
  84. mtd->size = map->size;
  85. /*
  86. * We only REALLY get half the chip, due to the way it is
  87. * wired up - D8-D15 are tossed away. We read linear bytes,
  88. * but in reality we are getting 1/2 of each 16-bit read,
  89. * which LOOKS linear to us. Because CFI code accounts for
  90. * things like lock/unlock/erase by eraseregions, we need to
  91. * fudge them to reflect this. Erases go like this:
  92. * * send an erase to an address
  93. * * the chip samples the address and erases the block
  94. * * add the block erasesize to the address and repeat
  95. * -- the problem is that addresses are 16-bit addressable
  96. * -- we end up erasing every-other block
  97. */
  98. mtd->erasesize /= 2;
  99. for (i = 0; i < mtd->numeraseregions; i++) {
  100. struct mtd_erase_region_info *region = &mtd->eraseregions[i];
  101. region->erasesize /= 2;
  102. }
  103. /*
  104. * If the chip is bigger than the map, it is wired with the high
  105. * address lines pulled up. This makes us access the top portion of
  106. * the chip, so all our erase-region info is wrong. Start cutting from
  107. * the bottom.
  108. */
  109. for (i = 0; !done && i < mtd->numeraseregions; i++) {
  110. struct mtd_erase_region_info *region = &mtd->eraseregions[i];
  111. if (region->numblocks * region->erasesize > mtd->size) {
  112. region->numblocks = (mtd->size / region->erasesize);
  113. done = 1;
  114. } else {
  115. region->numblocks = 0;
  116. }
  117. region->offset = 0;
  118. }
  119. return 0;
  120. }
  121. /* CSB5's 'Function Control Register' has bits for decoding @ >= 0xffc00000 */
  122. #define CSB5_FCR 0x41
  123. #define CSB5_FCR_DECODE_ALL 0x0e
  124. static int __devinit
  125. scb2_flash_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  126. {
  127. u8 reg;
  128. /* enable decoding of the flash region in the south bridge */
  129. pci_read_config_byte(dev, CSB5_FCR, &reg);
  130. pci_write_config_byte(dev, CSB5_FCR, reg | CSB5_FCR_DECODE_ALL);
  131. if (!request_mem_region(SCB2_ADDR, SCB2_WINDOW, scb2_map.name)) {
  132. /*
  133. * The BIOS seems to mark the flash region as 'reserved'
  134. * in the e820 map. Warn and go about our business.
  135. */
  136. printk(KERN_WARNING MODNAME
  137. ": warning - can't reserve rom window, continuing\n");
  138. region_fail = 1;
  139. }
  140. /* remap the IO window (w/o caching) */
  141. scb2_ioaddr = ioremap_nocache(SCB2_ADDR, SCB2_WINDOW);
  142. if (!scb2_ioaddr) {
  143. printk(KERN_ERR MODNAME ": Failed to ioremap window!\n");
  144. if (!region_fail)
  145. release_mem_region(SCB2_ADDR, SCB2_WINDOW);
  146. return -ENOMEM;
  147. }
  148. scb2_map.phys = SCB2_ADDR;
  149. scb2_map.virt = scb2_ioaddr;
  150. scb2_map.size = SCB2_WINDOW;
  151. simple_map_init(&scb2_map);
  152. /* try to find a chip */
  153. scb2_mtd = do_map_probe("cfi_probe", &scb2_map);
  154. if (!scb2_mtd) {
  155. printk(KERN_ERR MODNAME ": flash probe failed!\n");
  156. iounmap(scb2_ioaddr);
  157. if (!region_fail)
  158. release_mem_region(SCB2_ADDR, SCB2_WINDOW);
  159. return -ENODEV;
  160. }
  161. scb2_mtd->owner = THIS_MODULE;
  162. if (scb2_fixup_mtd(scb2_mtd) < 0) {
  163. del_mtd_device(scb2_mtd);
  164. map_destroy(scb2_mtd);
  165. iounmap(scb2_ioaddr);
  166. if (!region_fail)
  167. release_mem_region(SCB2_ADDR, SCB2_WINDOW);
  168. return -ENODEV;
  169. }
  170. printk(KERN_NOTICE MODNAME ": chip size 0x%x at offset 0x%x\n",
  171. scb2_mtd->size, SCB2_WINDOW - scb2_mtd->size);
  172. add_mtd_device(scb2_mtd);
  173. return 0;
  174. }
  175. static void __devexit
  176. scb2_flash_remove(struct pci_dev *dev)
  177. {
  178. if (!scb2_mtd)
  179. return;
  180. /* disable flash writes */
  181. if (scb2_mtd->lock)
  182. scb2_mtd->lock(scb2_mtd, 0, scb2_mtd->size);
  183. del_mtd_device(scb2_mtd);
  184. map_destroy(scb2_mtd);
  185. iounmap(scb2_ioaddr);
  186. scb2_ioaddr = NULL;
  187. if (!region_fail)
  188. release_mem_region(SCB2_ADDR, SCB2_WINDOW);
  189. pci_set_drvdata(dev, NULL);
  190. }
  191. static struct pci_device_id scb2_flash_pci_ids[] = {
  192. {
  193. .vendor = PCI_VENDOR_ID_SERVERWORKS,
  194. .device = PCI_DEVICE_ID_SERVERWORKS_CSB5,
  195. .subvendor = PCI_ANY_ID,
  196. .subdevice = PCI_ANY_ID
  197. },
  198. { 0, }
  199. };
  200. static struct pci_driver scb2_flash_driver = {
  201. .name = "Intel SCB2 BIOS Flash",
  202. .id_table = scb2_flash_pci_ids,
  203. .probe = scb2_flash_probe,
  204. .remove = __devexit_p(scb2_flash_remove),
  205. };
  206. static int __init
  207. scb2_flash_init(void)
  208. {
  209. return pci_register_driver(&scb2_flash_driver);
  210. }
  211. static void __exit
  212. scb2_flash_exit(void)
  213. {
  214. pci_unregister_driver(&scb2_flash_driver);
  215. }
  216. module_init(scb2_flash_init);
  217. module_exit(scb2_flash_exit);
  218. MODULE_LICENSE("GPL");
  219. MODULE_AUTHOR("Tim Hockin <thockin@sun.com>");
  220. MODULE_DESCRIPTION("MTD map driver for Intel SCB2 BIOS Flash");
  221. MODULE_DEVICE_TABLE(pci, scb2_flash_pci_ids);