1*09c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* linux/drivers/mtd/maps/scx200_docflash.c
31da177e4SLinus Torvalds
41da177e4SLinus Torvalds Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
51da177e4SLinus Torvalds
61da177e4SLinus Torvalds National Semiconductor SCx200 flash mapped with DOCCS
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <asm/io.h>
141da177e4SLinus Torvalds #include <linux/mtd/mtd.h>
151da177e4SLinus Torvalds #include <linux/mtd/map.h>
161da177e4SLinus Torvalds #include <linux/mtd/partitions.h>
171da177e4SLinus Torvalds
181da177e4SLinus Torvalds #include <linux/pci.h>
191da177e4SLinus Torvalds #include <linux/scx200.h>
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds #define NAME "scx200_docflash"
221da177e4SLinus Torvalds
231da177e4SLinus Torvalds MODULE_AUTHOR("Christer Weinigel <wingel@hack.org>");
241da177e4SLinus Torvalds MODULE_DESCRIPTION("NatSemi SCx200 DOCCS Flash Driver");
251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
261da177e4SLinus Torvalds
271da177e4SLinus Torvalds static int probe = 0; /* Don't autoprobe */
281da177e4SLinus Torvalds static unsigned size = 0x1000000; /* 16 MiB the whole ISA address space */
291da177e4SLinus Torvalds static unsigned width = 8; /* Default to 8 bits wide */
301da177e4SLinus Torvalds static char *flashtype = "cfi_probe";
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds module_param(probe, int, 0);
331da177e4SLinus Torvalds MODULE_PARM_DESC(probe, "Probe for a BIOS mapping");
341da177e4SLinus Torvalds module_param(size, int, 0);
351da177e4SLinus Torvalds MODULE_PARM_DESC(size, "Size of the flash mapping");
361da177e4SLinus Torvalds module_param(width, int, 0);
371da177e4SLinus Torvalds MODULE_PARM_DESC(width, "Data width of the flash mapping (8/16)");
381da177e4SLinus Torvalds module_param(flashtype, charp, 0);
391da177e4SLinus Torvalds MODULE_PARM_DESC(flashtype, "Type of MTD probe to do");
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds static struct resource docmem = {
421da177e4SLinus Torvalds .flags = IORESOURCE_MEM,
431da177e4SLinus Torvalds .name = "NatSemi SCx200 DOCCS Flash",
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds static struct mtd_info *mymtd;
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds static struct mtd_partition partition_info[] = {
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds .name = "DOCCS Boot kernel",
511da177e4SLinus Torvalds .offset = 0,
521da177e4SLinus Torvalds .size = 0xc0000
531da177e4SLinus Torvalds },
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds .name = "DOCCS Low BIOS",
561da177e4SLinus Torvalds .offset = 0xc0000,
571da177e4SLinus Torvalds .size = 0x40000
581da177e4SLinus Torvalds },
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds .name = "DOCCS File system",
611da177e4SLinus Torvalds .offset = 0x100000,
621da177e4SLinus Torvalds .size = ~0 /* calculate from flash size */
631da177e4SLinus Torvalds },
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds .name = "DOCCS High BIOS",
661da177e4SLinus Torvalds .offset = ~0, /* calculate from flash size */
671da177e4SLinus Torvalds .size = 0x80000
681da177e4SLinus Torvalds },
691da177e4SLinus Torvalds };
7087d10f3cSTobias Klauser #define NUM_PARTITIONS ARRAY_SIZE(partition_info)
711da177e4SLinus Torvalds
721da177e4SLinus Torvalds static struct map_info scx200_docflash_map = {
731da177e4SLinus Torvalds .name = "NatSemi SCx200 DOCCS Flash",
741da177e4SLinus Torvalds };
751da177e4SLinus Torvalds
init_scx200_docflash(void)761da177e4SLinus Torvalds static int __init init_scx200_docflash(void)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds unsigned u;
791da177e4SLinus Torvalds unsigned base;
801da177e4SLinus Torvalds unsigned ctrl;
811da177e4SLinus Torvalds unsigned pmr;
821da177e4SLinus Torvalds struct pci_dev *bridge;
831da177e4SLinus Torvalds
841da177e4SLinus Torvalds printk(KERN_DEBUG NAME ": NatSemi SCx200 DOCCS Flash Driver\n");
851da177e4SLinus Torvalds
86dd8e9ed6SAlan Cox if ((bridge = pci_get_device(PCI_VENDOR_ID_NS,
871da177e4SLinus Torvalds PCI_DEVICE_ID_NS_SCx200_BRIDGE,
881da177e4SLinus Torvalds NULL)) == NULL)
891da177e4SLinus Torvalds return -ENODEV;
901da177e4SLinus Torvalds
911da177e4SLinus Torvalds /* check that we have found the configuration block */
92dd8e9ed6SAlan Cox if (!scx200_cb_present()) {
93dd8e9ed6SAlan Cox pci_dev_put(bridge);
941da177e4SLinus Torvalds return -ENODEV;
95dd8e9ed6SAlan Cox }
961da177e4SLinus Torvalds
971da177e4SLinus Torvalds if (probe) {
981da177e4SLinus Torvalds /* Try to use the present flash mapping if any */
991da177e4SLinus Torvalds pci_read_config_dword(bridge, SCx200_DOCCS_BASE, &base);
1001da177e4SLinus Torvalds pci_read_config_dword(bridge, SCx200_DOCCS_CTRL, &ctrl);
101dd8e9ed6SAlan Cox pci_dev_put(bridge);
102dd8e9ed6SAlan Cox
1031da177e4SLinus Torvalds pmr = inl(scx200_cb_base + SCx200_PMR);
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds if (base == 0
1061da177e4SLinus Torvalds || (ctrl & 0x07000000) != 0x07000000
1071da177e4SLinus Torvalds || (ctrl & 0x0007ffff) == 0)
1081da177e4SLinus Torvalds return -ENODEV;
1091da177e4SLinus Torvalds
1101da177e4SLinus Torvalds size = ((ctrl&0x1fff)<<13) + (1<<13);
1111da177e4SLinus Torvalds
1121da177e4SLinus Torvalds for (u = size; u > 1; u >>= 1)
1131da177e4SLinus Torvalds ;
1141da177e4SLinus Torvalds if (u != 1)
1151da177e4SLinus Torvalds return -ENODEV;
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds if (pmr & (1<<6))
1181da177e4SLinus Torvalds width = 16;
1191da177e4SLinus Torvalds else
1201da177e4SLinus Torvalds width = 8;
1211da177e4SLinus Torvalds
1221da177e4SLinus Torvalds docmem.start = base;
1231da177e4SLinus Torvalds docmem.end = base + size;
1241da177e4SLinus Torvalds
1251da177e4SLinus Torvalds if (request_resource(&iomem_resource, &docmem)) {
1261da177e4SLinus Torvalds printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
1271da177e4SLinus Torvalds return -ENOMEM;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds } else {
130dd8e9ed6SAlan Cox pci_dev_put(bridge);
1311da177e4SLinus Torvalds for (u = size; u > 1; u >>= 1)
1321da177e4SLinus Torvalds ;
1331da177e4SLinus Torvalds if (u != 1) {
1341da177e4SLinus Torvalds printk(KERN_ERR NAME ": invalid size for flash mapping\n");
1351da177e4SLinus Torvalds return -EINVAL;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds if (width != 8 && width != 16) {
1391da177e4SLinus Torvalds printk(KERN_ERR NAME ": invalid bus width for flash mapping\n");
1401da177e4SLinus Torvalds return -EINVAL;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds
1431da177e4SLinus Torvalds if (allocate_resource(&iomem_resource, &docmem,
1441da177e4SLinus Torvalds size,
1451da177e4SLinus Torvalds 0xc0000000, 0xffffffff,
1461da177e4SLinus Torvalds size, NULL, NULL)) {
1471da177e4SLinus Torvalds printk(KERN_ERR NAME ": unable to allocate memory for flash mapping\n");
1481da177e4SLinus Torvalds return -ENOMEM;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds ctrl = 0x07000000 | ((size-1) >> 13);
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds printk(KERN_INFO "DOCCS BASE=0x%08lx, CTRL=0x%08lx\n", (long)docmem.start, (long)ctrl);
1541da177e4SLinus Torvalds
1551da177e4SLinus Torvalds pci_write_config_dword(bridge, SCx200_DOCCS_BASE, docmem.start);
1561da177e4SLinus Torvalds pci_write_config_dword(bridge, SCx200_DOCCS_CTRL, ctrl);
1571da177e4SLinus Torvalds pmr = inl(scx200_cb_base + SCx200_PMR);
1581da177e4SLinus Torvalds
1591da177e4SLinus Torvalds if (width == 8) {
1601da177e4SLinus Torvalds pmr &= ~(1<<6);
1611da177e4SLinus Torvalds } else {
1621da177e4SLinus Torvalds pmr |= (1<<6);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds outl(pmr, scx200_cb_base + SCx200_PMR);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds
167f9a5279cSJoe Perches printk(KERN_INFO NAME ": DOCCS mapped at %pR, width %d\n",
168f9a5279cSJoe Perches &docmem, width);
1691da177e4SLinus Torvalds
1701da177e4SLinus Torvalds scx200_docflash_map.size = size;
1711da177e4SLinus Torvalds if (width == 8)
1721da177e4SLinus Torvalds scx200_docflash_map.bankwidth = 1;
1731da177e4SLinus Torvalds else
1741da177e4SLinus Torvalds scx200_docflash_map.bankwidth = 2;
1751da177e4SLinus Torvalds
1761da177e4SLinus Torvalds simple_map_init(&scx200_docflash_map);
1771da177e4SLinus Torvalds
1781da177e4SLinus Torvalds scx200_docflash_map.phys = docmem.start;
1791da177e4SLinus Torvalds scx200_docflash_map.virt = ioremap(docmem.start, scx200_docflash_map.size);
1801da177e4SLinus Torvalds if (!scx200_docflash_map.virt) {
1811da177e4SLinus Torvalds printk(KERN_ERR NAME ": failed to ioremap the flash\n");
1821da177e4SLinus Torvalds release_resource(&docmem);
1831da177e4SLinus Torvalds return -EIO;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds
1861da177e4SLinus Torvalds mymtd = do_map_probe(flashtype, &scx200_docflash_map);
1871da177e4SLinus Torvalds if (!mymtd) {
1881da177e4SLinus Torvalds printk(KERN_ERR NAME ": unable to detect flash\n");
1891da177e4SLinus Torvalds iounmap(scx200_docflash_map.virt);
1901da177e4SLinus Torvalds release_resource(&docmem);
1911da177e4SLinus Torvalds return -ENXIO;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds
1941da177e4SLinus Torvalds if (size < mymtd->size)
1951da177e4SLinus Torvalds printk(KERN_WARNING NAME ": warning, flash mapping is smaller than flash size\n");
1961da177e4SLinus Torvalds
1971da177e4SLinus Torvalds mymtd->owner = THIS_MODULE;
1981da177e4SLinus Torvalds
1991da177e4SLinus Torvalds partition_info[3].offset = mymtd->size-partition_info[3].size;
2001da177e4SLinus Torvalds partition_info[2].size = partition_info[3].offset-partition_info[2].offset;
201f6c11c18SJamie Iles mtd_device_register(mymtd, partition_info, NUM_PARTITIONS);
202f6c11c18SJamie Iles
2031da177e4SLinus Torvalds return 0;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds
cleanup_scx200_docflash(void)2061da177e4SLinus Torvalds static void __exit cleanup_scx200_docflash(void)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds if (mymtd) {
209f6c11c18SJamie Iles mtd_device_unregister(mymtd);
2101da177e4SLinus Torvalds map_destroy(mymtd);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds if (scx200_docflash_map.virt) {
2131da177e4SLinus Torvalds iounmap(scx200_docflash_map.virt);
2141da177e4SLinus Torvalds release_resource(&docmem);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds module_init(init_scx200_docflash);
2191da177e4SLinus Torvalds module_exit(cleanup_scx200_docflash);
220