scx200_docflash.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* linux/drivers/mtd/maps/scx200_docflash.c
  3. Copyright (c) 2001,2002 Christer Weinigel <[email protected]>
  4. National Semiconductor SCx200 flash mapped with DOCCS
  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/partitions.h>
  14. #include <linux/pci.h>
  15. #include <linux/scx200.h>
  16. #define NAME "scx200_docflash"
  17. MODULE_AUTHOR("Christer Weinigel <[email protected]>");
  18. MODULE_DESCRIPTION("NatSemi SCx200 DOCCS Flash Driver");
  19. MODULE_LICENSE("GPL");
  20. static int probe = 0; /* Don't autoprobe */
  21. static unsigned size = 0x1000000; /* 16 MiB the whole ISA address space */
  22. static unsigned width = 8; /* Default to 8 bits wide */
  23. static char *flashtype = "cfi_probe";
  24. module_param(probe, int, 0);
  25. MODULE_PARM_DESC(probe, "Probe for a BIOS mapping");
  26. module_param(size, int, 0);
  27. MODULE_PARM_DESC(size, "Size of the flash mapping");
  28. module_param(width, int, 0);
  29. MODULE_PARM_DESC(width, "Data width of the flash mapping (8/16)");
  30. module_param(flashtype, charp, 0);
  31. MODULE_PARM_DESC(flashtype, "Type of MTD probe to do");
  32. static struct resource docmem = {
  33. .flags = IORESOURCE_MEM,
  34. .name = "NatSemi SCx200 DOCCS Flash",
  35. };
  36. static struct mtd_info *mymtd;
  37. static struct mtd_partition partition_info[] = {
  38. {
  39. .name = "DOCCS Boot kernel",
  40. .offset = 0,
  41. .size = 0xc0000
  42. },
  43. {
  44. .name = "DOCCS Low BIOS",
  45. .offset = 0xc0000,
  46. .size = 0x40000
  47. },
  48. {
  49. .name = "DOCCS File system",
  50. .offset = 0x100000,
  51. .size = ~0 /* calculate from flash size */
  52. },
  53. {
  54. .name = "DOCCS High BIOS",
  55. .offset = ~0, /* calculate from flash size */
  56. .size = 0x80000
  57. },
  58. };
  59. #define NUM_PARTITIONS ARRAY_SIZE(partition_info)
  60. static struct map_info scx200_docflash_map = {
  61. .name = "NatSemi SCx200 DOCCS Flash",
  62. };
  63. static int __init init_scx200_docflash(void)
  64. {
  65. unsigned u;
  66. unsigned base;
  67. unsigned ctrl;
  68. unsigned pmr;
  69. struct pci_dev *bridge;
  70. printk(KERN_DEBUG NAME ": NatSemi SCx200 DOCCS Flash Driver\n");
  71. if ((bridge = pci_get_device(PCI_VENDOR_ID_NS,
  72. PCI_DEVICE_ID_NS_SCx200_BRIDGE,
  73. NULL)) == NULL)
  74. return -ENODEV;
  75. /* check that we have found the configuration block */
  76. if (!scx200_cb_present()) {
  77. pci_dev_put(bridge);
  78. return -ENODEV;
  79. }
  80. if (probe) {
  81. /* Try to use the present flash mapping if any */
  82. pci_read_config_dword(bridge, SCx200_DOCCS_BASE, &base);
  83. pci_read_config_dword(bridge, SCx200_DOCCS_CTRL, &ctrl);
  84. pci_dev_put(bridge);
  85. pmr = inl(scx200_cb_base + SCx200_PMR);
  86. if (base == 0
  87. || (ctrl & 0x07000000) != 0x07000000
  88. || (ctrl & 0x0007ffff) == 0)
  89. return -ENODEV;
  90. size = ((ctrl&0x1fff)<<13) + (1<<13);
  91. for (u = size; u > 1; u >>= 1)
  92. ;
  93. if (u != 1)
  94. return -ENODEV;
  95. if (pmr & (1<<6))
  96. width = 16;
  97. else
  98. width = 8;
  99. docmem.start = base;
  100. docmem.end = base + size;
  101. if (request_resource(&iomem_resource, &docmem)) {
  102. printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
  103. return -ENOMEM;
  104. }
  105. } else {
  106. pci_dev_put(bridge);
  107. for (u = size; u > 1; u >>= 1)
  108. ;
  109. if (u != 1) {
  110. printk(KERN_ERR NAME ": invalid size for flash mapping\n");
  111. return -EINVAL;
  112. }
  113. if (width != 8 && width != 16) {
  114. printk(KERN_ERR NAME ": invalid bus width for flash mapping\n");
  115. return -EINVAL;
  116. }
  117. if (allocate_resource(&iomem_resource, &docmem,
  118. size,
  119. 0xc0000000, 0xffffffff,
  120. size, NULL, NULL)) {
  121. printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
  122. return -ENOMEM;
  123. }
  124. ctrl = 0x07000000 | ((size-1) >> 13);
  125. printk(KERN_INFO "DOCCS BASE=0x%08lx, CTRL=0x%08lx\n", (long)docmem.start, (long)ctrl);
  126. pci_write_config_dword(bridge, SCx200_DOCCS_BASE, docmem.start);
  127. pci_write_config_dword(bridge, SCx200_DOCCS_CTRL, ctrl);
  128. pmr = inl(scx200_cb_base + SCx200_PMR);
  129. if (width == 8) {
  130. pmr &= ~(1<<6);
  131. } else {
  132. pmr |= (1<<6);
  133. }
  134. outl(pmr, scx200_cb_base + SCx200_PMR);
  135. }
  136. printk(KERN_INFO NAME ": DOCCS mapped at %pR, width %d\n",
  137. &docmem, width);
  138. scx200_docflash_map.size = size;
  139. if (width == 8)
  140. scx200_docflash_map.bankwidth = 1;
  141. else
  142. scx200_docflash_map.bankwidth = 2;
  143. simple_map_init(&scx200_docflash_map);
  144. scx200_docflash_map.phys = docmem.start;
  145. scx200_docflash_map.virt = ioremap(docmem.start, scx200_docflash_map.size);
  146. if (!scx200_docflash_map.virt) {
  147. printk(KERN_ERR NAME ": failed to ioremap the flash\n");
  148. release_resource(&docmem);
  149. return -EIO;
  150. }
  151. mymtd = do_map_probe(flashtype, &scx200_docflash_map);
  152. if (!mymtd) {
  153. printk(KERN_ERR NAME ": unable to detect flash\n");
  154. iounmap(scx200_docflash_map.virt);
  155. release_resource(&docmem);
  156. return -ENXIO;
  157. }
  158. if (size < mymtd->size)
  159. printk(KERN_WARNING NAME ": warning, flash mapping is smaller than flash size\n");
  160. mymtd->owner = THIS_MODULE;
  161. partition_info[3].offset = mymtd->size-partition_info[3].size;
  162. partition_info[2].size = partition_info[3].offset-partition_info[2].offset;
  163. mtd_device_register(mymtd, partition_info, NUM_PARTITIONS);
  164. return 0;
  165. }
  166. static void __exit cleanup_scx200_docflash(void)
  167. {
  168. if (mymtd) {
  169. mtd_device_unregister(mymtd);
  170. map_destroy(mymtd);
  171. }
  172. if (scx200_docflash_map.virt) {
  173. iounmap(scx200_docflash_map.virt);
  174. release_resource(&docmem);
  175. }
  176. }
  177. module_init(init_scx200_docflash);
  178. module_exit(cleanup_scx200_docflash);