Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* linux/drivers/mtd/maps/scx200_docflash.c
0003 
0004    Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
0005 
0006    National Semiconductor SCx200 flash mapped with DOCCS
0007 */
0008 
0009 #include <linux/module.h>
0010 #include <linux/types.h>
0011 #include <linux/kernel.h>
0012 #include <linux/init.h>
0013 #include <asm/io.h>
0014 #include <linux/mtd/mtd.h>
0015 #include <linux/mtd/map.h>
0016 #include <linux/mtd/partitions.h>
0017 
0018 #include <linux/pci.h>
0019 #include <linux/scx200.h>
0020 
0021 #define NAME "scx200_docflash"
0022 
0023 MODULE_AUTHOR("Christer Weinigel <wingel@hack.org>");
0024 MODULE_DESCRIPTION("NatSemi SCx200 DOCCS Flash Driver");
0025 MODULE_LICENSE("GPL");
0026 
0027 static int probe = 0;       /* Don't autoprobe */
0028 static unsigned size = 0x1000000; /* 16 MiB the whole ISA address space */
0029 static unsigned width = 8;  /* Default to 8 bits wide */
0030 static char *flashtype = "cfi_probe";
0031 
0032 module_param(probe, int, 0);
0033 MODULE_PARM_DESC(probe, "Probe for a BIOS mapping");
0034 module_param(size, int, 0);
0035 MODULE_PARM_DESC(size, "Size of the flash mapping");
0036 module_param(width, int, 0);
0037 MODULE_PARM_DESC(width, "Data width of the flash mapping (8/16)");
0038 module_param(flashtype, charp, 0);
0039 MODULE_PARM_DESC(flashtype, "Type of MTD probe to do");
0040 
0041 static struct resource docmem = {
0042     .flags = IORESOURCE_MEM,
0043     .name  = "NatSemi SCx200 DOCCS Flash",
0044 };
0045 
0046 static struct mtd_info *mymtd;
0047 
0048 static struct mtd_partition partition_info[] = {
0049     {
0050         .name   = "DOCCS Boot kernel",
0051         .offset = 0,
0052         .size   = 0xc0000
0053     },
0054     {
0055         .name   = "DOCCS Low BIOS",
0056         .offset = 0xc0000,
0057         .size   = 0x40000
0058     },
0059     {
0060         .name   = "DOCCS File system",
0061         .offset = 0x100000,
0062         .size   = ~0    /* calculate from flash size */
0063     },
0064     {
0065         .name   = "DOCCS High BIOS",
0066         .offset = ~0,   /* calculate from flash size */
0067         .size   = 0x80000
0068     },
0069 };
0070 #define NUM_PARTITIONS ARRAY_SIZE(partition_info)
0071 
0072 static struct map_info scx200_docflash_map = {
0073     .name      = "NatSemi SCx200 DOCCS Flash",
0074 };
0075 
0076 static int __init init_scx200_docflash(void)
0077 {
0078     unsigned u;
0079     unsigned base;
0080     unsigned ctrl;
0081     unsigned pmr;
0082     struct pci_dev *bridge;
0083 
0084     printk(KERN_DEBUG NAME ": NatSemi SCx200 DOCCS Flash Driver\n");
0085 
0086     if ((bridge = pci_get_device(PCI_VENDOR_ID_NS,
0087                       PCI_DEVICE_ID_NS_SCx200_BRIDGE,
0088                       NULL)) == NULL)
0089         return -ENODEV;
0090 
0091     /* check that we have found the configuration block */
0092     if (!scx200_cb_present()) {
0093         pci_dev_put(bridge);
0094         return -ENODEV;
0095     }
0096 
0097     if (probe) {
0098         /* Try to use the present flash mapping if any */
0099         pci_read_config_dword(bridge, SCx200_DOCCS_BASE, &base);
0100         pci_read_config_dword(bridge, SCx200_DOCCS_CTRL, &ctrl);
0101         pci_dev_put(bridge);
0102 
0103         pmr = inl(scx200_cb_base + SCx200_PMR);
0104 
0105         if (base == 0
0106             || (ctrl & 0x07000000) != 0x07000000
0107             || (ctrl & 0x0007ffff) == 0)
0108             return -ENODEV;
0109 
0110         size = ((ctrl&0x1fff)<<13) + (1<<13);
0111 
0112         for (u = size; u > 1; u >>= 1)
0113             ;
0114         if (u != 1)
0115             return -ENODEV;
0116 
0117         if (pmr & (1<<6))
0118             width = 16;
0119         else
0120             width = 8;
0121 
0122         docmem.start = base;
0123         docmem.end = base + size;
0124 
0125         if (request_resource(&iomem_resource, &docmem)) {
0126             printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
0127             return -ENOMEM;
0128         }
0129     } else {
0130         pci_dev_put(bridge);
0131         for (u = size; u > 1; u >>= 1)
0132             ;
0133         if (u != 1) {
0134             printk(KERN_ERR NAME ": invalid size for flash mapping\n");
0135             return -EINVAL;
0136         }
0137 
0138         if (width != 8 && width != 16) {
0139             printk(KERN_ERR NAME ": invalid bus width for flash mapping\n");
0140             return -EINVAL;
0141         }
0142 
0143         if (allocate_resource(&iomem_resource, &docmem,
0144                       size,
0145                       0xc0000000, 0xffffffff,
0146                       size, NULL, NULL)) {
0147             printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
0148             return -ENOMEM;
0149         }
0150 
0151         ctrl = 0x07000000 | ((size-1) >> 13);
0152 
0153         printk(KERN_INFO "DOCCS BASE=0x%08lx, CTRL=0x%08lx\n", (long)docmem.start, (long)ctrl);
0154 
0155         pci_write_config_dword(bridge, SCx200_DOCCS_BASE, docmem.start);
0156         pci_write_config_dword(bridge, SCx200_DOCCS_CTRL, ctrl);
0157         pmr = inl(scx200_cb_base + SCx200_PMR);
0158 
0159         if (width == 8) {
0160             pmr &= ~(1<<6);
0161         } else {
0162             pmr |= (1<<6);
0163         }
0164         outl(pmr, scx200_cb_base + SCx200_PMR);
0165     }
0166 
0167     printk(KERN_INFO NAME ": DOCCS mapped at %pR, width %d\n",
0168            &docmem, width);
0169 
0170     scx200_docflash_map.size = size;
0171     if (width == 8)
0172         scx200_docflash_map.bankwidth = 1;
0173     else
0174         scx200_docflash_map.bankwidth = 2;
0175 
0176     simple_map_init(&scx200_docflash_map);
0177 
0178     scx200_docflash_map.phys = docmem.start;
0179     scx200_docflash_map.virt = ioremap(docmem.start, scx200_docflash_map.size);
0180     if (!scx200_docflash_map.virt) {
0181         printk(KERN_ERR NAME ": failed to ioremap the flash\n");
0182         release_resource(&docmem);
0183         return -EIO;
0184     }
0185 
0186     mymtd = do_map_probe(flashtype, &scx200_docflash_map);
0187     if (!mymtd) {
0188         printk(KERN_ERR NAME ": unable to detect flash\n");
0189         iounmap(scx200_docflash_map.virt);
0190         release_resource(&docmem);
0191         return -ENXIO;
0192     }
0193 
0194     if (size < mymtd->size)
0195         printk(KERN_WARNING NAME ": warning, flash mapping is smaller than flash size\n");
0196 
0197     mymtd->owner = THIS_MODULE;
0198 
0199     partition_info[3].offset = mymtd->size-partition_info[3].size;
0200     partition_info[2].size = partition_info[3].offset-partition_info[2].offset;
0201     mtd_device_register(mymtd, partition_info, NUM_PARTITIONS);
0202 
0203     return 0;
0204 }
0205 
0206 static void __exit cleanup_scx200_docflash(void)
0207 {
0208     if (mymtd) {
0209         mtd_device_unregister(mymtd);
0210         map_destroy(mymtd);
0211     }
0212     if (scx200_docflash_map.virt) {
0213         iounmap(scx200_docflash_map.virt);
0214         release_resource(&docmem);
0215     }
0216 }
0217 
0218 module_init(init_scx200_docflash);
0219 module_exit(cleanup_scx200_docflash);