18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Mapping of a custom board with both AMD CFI and JEDEC flash in partitions.
38c2ecf20Sopenharmony_ci * Config with both CFI and JEDEC device support.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Basically physmap.c with the addition of partitions and
68c2ecf20Sopenharmony_ci * an array of mapping info to accommodate more than one flash type per board.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright 2005-2007 PMC-Sierra, Inc.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *  This program is free software; you can redistribute  it and/or modify it
118c2ecf20Sopenharmony_ci *  under  the terms of  the GNU General  Public License as published by the
128c2ecf20Sopenharmony_ci *  Free Software Foundation;  either version 2 of the  License, or (at your
138c2ecf20Sopenharmony_ci *  option) any later version.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
168c2ecf20Sopenharmony_ci *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
178c2ecf20Sopenharmony_ci *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
188c2ecf20Sopenharmony_ci *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
198c2ecf20Sopenharmony_ci *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
208c2ecf20Sopenharmony_ci *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
218c2ecf20Sopenharmony_ci *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
228c2ecf20Sopenharmony_ci *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
238c2ecf20Sopenharmony_ci *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248c2ecf20Sopenharmony_ci *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci *  You should have received a copy of the  GNU General Public License along
278c2ecf20Sopenharmony_ci *  with this program; if not, write  to the Free Software Foundation, Inc.,
288c2ecf20Sopenharmony_ci *  675 Mass Ave, Cambridge, MA 02139, USA.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/slab.h>
328c2ecf20Sopenharmony_ci#include <linux/module.h>
338c2ecf20Sopenharmony_ci#include <linux/types.h>
348c2ecf20Sopenharmony_ci#include <linux/kernel.h>
358c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h>
368c2ecf20Sopenharmony_ci#include <linux/mtd/map.h>
378c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include <asm/io.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include <msp_prom.h>
428c2ecf20Sopenharmony_ci#include <msp_regs.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic struct mtd_info **msp_flash;
468c2ecf20Sopenharmony_cistatic struct mtd_partition **msp_parts;
478c2ecf20Sopenharmony_cistatic struct map_info *msp_maps;
488c2ecf20Sopenharmony_cistatic int fcnt;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define DEBUG_MARKER printk(KERN_NOTICE "%s[%d]\n", __func__, __LINE__)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int __init init_msp_flash(void)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	int i, j, ret = -ENOMEM;
558c2ecf20Sopenharmony_ci	int offset, coff;
568c2ecf20Sopenharmony_ci	char *env;
578c2ecf20Sopenharmony_ci	int pcnt;
588c2ecf20Sopenharmony_ci	char flash_name[] = "flash0";
598c2ecf20Sopenharmony_ci	char part_name[] = "flash0_0";
608c2ecf20Sopenharmony_ci	unsigned addr, size;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/* If ELB is disabled by "ful-mux" mode, we can't get at flash */
638c2ecf20Sopenharmony_ci	if ((*DEV_ID_REG & DEV_ID_SINGLE_PC) &&
648c2ecf20Sopenharmony_ci	    (*ELB_1PC_EN_REG & SINGLE_PCCARD)) {
658c2ecf20Sopenharmony_ci		printk(KERN_NOTICE "Single PC Card mode: no flash access\n");
668c2ecf20Sopenharmony_ci		return -ENXIO;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* examine the prom environment for flash devices */
708c2ecf20Sopenharmony_ci	for (fcnt = 0; (env = prom_getenv(flash_name)); fcnt++)
718c2ecf20Sopenharmony_ci		flash_name[5] = '0' + fcnt + 1;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (fcnt < 1)
748c2ecf20Sopenharmony_ci		return -ENXIO;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	printk(KERN_NOTICE "Found %d PMC flash devices\n", fcnt);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	msp_flash = kcalloc(fcnt, sizeof(*msp_flash), GFP_KERNEL);
798c2ecf20Sopenharmony_ci	if (!msp_flash)
808c2ecf20Sopenharmony_ci		return -ENOMEM;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	msp_parts = kcalloc(fcnt, sizeof(*msp_parts), GFP_KERNEL);
838c2ecf20Sopenharmony_ci	if (!msp_parts)
848c2ecf20Sopenharmony_ci		goto free_msp_flash;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	msp_maps = kcalloc(fcnt, sizeof(*msp_maps), GFP_KERNEL);
878c2ecf20Sopenharmony_ci	if (!msp_maps)
888c2ecf20Sopenharmony_ci		goto free_msp_parts;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/* loop over the flash devices, initializing each */
918c2ecf20Sopenharmony_ci	for (i = 0; i < fcnt; i++) {
928c2ecf20Sopenharmony_ci		/* examine the prom environment for flash partititions */
938c2ecf20Sopenharmony_ci		part_name[5] = '0' + i;
948c2ecf20Sopenharmony_ci		part_name[7] = '0';
958c2ecf20Sopenharmony_ci		for (pcnt = 0; (env = prom_getenv(part_name)); pcnt++)
968c2ecf20Sopenharmony_ci			part_name[7] = '0' + pcnt + 1;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		if (pcnt == 0) {
998c2ecf20Sopenharmony_ci			printk(KERN_NOTICE "Skipping flash device %d "
1008c2ecf20Sopenharmony_ci				"(no partitions defined)\n", i);
1018c2ecf20Sopenharmony_ci			continue;
1028c2ecf20Sopenharmony_ci		}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		msp_parts[i] = kcalloc(pcnt, sizeof(struct mtd_partition),
1058c2ecf20Sopenharmony_ci				       GFP_KERNEL);
1068c2ecf20Sopenharmony_ci		if (!msp_parts[i])
1078c2ecf20Sopenharmony_ci			goto cleanup_loop;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		/* now initialize the devices proper */
1108c2ecf20Sopenharmony_ci		flash_name[5] = '0' + i;
1118c2ecf20Sopenharmony_ci		env = prom_getenv(flash_name);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci		if (sscanf(env, "%x:%x", &addr, &size) < 2) {
1148c2ecf20Sopenharmony_ci			ret = -ENXIO;
1158c2ecf20Sopenharmony_ci			kfree(msp_parts[i]);
1168c2ecf20Sopenharmony_ci			goto cleanup_loop;
1178c2ecf20Sopenharmony_ci		}
1188c2ecf20Sopenharmony_ci		addr = CPHYSADDR(addr);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci		printk(KERN_NOTICE
1218c2ecf20Sopenharmony_ci			"MSP flash device \"%s\": 0x%08x at 0x%08x\n",
1228c2ecf20Sopenharmony_ci			flash_name, size, addr);
1238c2ecf20Sopenharmony_ci		/* This must matchs the actual size of the flash chip */
1248c2ecf20Sopenharmony_ci		msp_maps[i].size = size;
1258c2ecf20Sopenharmony_ci		msp_maps[i].phys = addr;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci		/*
1288c2ecf20Sopenharmony_ci		 * Platforms have a specific limit of the size of memory
1298c2ecf20Sopenharmony_ci		 * which may be mapped for flash:
1308c2ecf20Sopenharmony_ci		 */
1318c2ecf20Sopenharmony_ci		if (size > CONFIG_MSP_FLASH_MAP_LIMIT)
1328c2ecf20Sopenharmony_ci			size = CONFIG_MSP_FLASH_MAP_LIMIT;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		msp_maps[i].virt = ioremap(addr, size);
1358c2ecf20Sopenharmony_ci		if (msp_maps[i].virt == NULL) {
1368c2ecf20Sopenharmony_ci			ret = -ENXIO;
1378c2ecf20Sopenharmony_ci			kfree(msp_parts[i]);
1388c2ecf20Sopenharmony_ci			goto cleanup_loop;
1398c2ecf20Sopenharmony_ci		}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		msp_maps[i].bankwidth = 1;
1428c2ecf20Sopenharmony_ci		msp_maps[i].name = kstrndup(flash_name, 7, GFP_KERNEL);
1438c2ecf20Sopenharmony_ci		if (!msp_maps[i].name) {
1448c2ecf20Sopenharmony_ci			iounmap(msp_maps[i].virt);
1458c2ecf20Sopenharmony_ci			kfree(msp_parts[i]);
1468c2ecf20Sopenharmony_ci			goto cleanup_loop;
1478c2ecf20Sopenharmony_ci		}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		for (j = 0; j < pcnt; j++) {
1508c2ecf20Sopenharmony_ci			part_name[5] = '0' + i;
1518c2ecf20Sopenharmony_ci			part_name[7] = '0' + j;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci			env = prom_getenv(part_name);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci			if (sscanf(env, "%x:%x:%n", &offset, &size,
1568c2ecf20Sopenharmony_ci						&coff) < 2) {
1578c2ecf20Sopenharmony_ci				ret = -ENXIO;
1588c2ecf20Sopenharmony_ci				kfree(msp_maps[i].name);
1598c2ecf20Sopenharmony_ci				iounmap(msp_maps[i].virt);
1608c2ecf20Sopenharmony_ci				kfree(msp_parts[i]);
1618c2ecf20Sopenharmony_ci				goto cleanup_loop;
1628c2ecf20Sopenharmony_ci			}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci			msp_parts[i][j].size = size;
1658c2ecf20Sopenharmony_ci			msp_parts[i][j].offset = offset;
1668c2ecf20Sopenharmony_ci			msp_parts[i][j].name = env + coff;
1678c2ecf20Sopenharmony_ci		}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		/* now probe and add the device */
1708c2ecf20Sopenharmony_ci		simple_map_init(&msp_maps[i]);
1718c2ecf20Sopenharmony_ci		msp_flash[i] = do_map_probe("cfi_probe", &msp_maps[i]);
1728c2ecf20Sopenharmony_ci		if (msp_flash[i]) {
1738c2ecf20Sopenharmony_ci			msp_flash[i]->owner = THIS_MODULE;
1748c2ecf20Sopenharmony_ci			mtd_device_register(msp_flash[i], msp_parts[i], pcnt);
1758c2ecf20Sopenharmony_ci		} else {
1768c2ecf20Sopenharmony_ci			printk(KERN_ERR "map probe failed for flash\n");
1778c2ecf20Sopenharmony_ci			ret = -ENXIO;
1788c2ecf20Sopenharmony_ci			kfree(msp_maps[i].name);
1798c2ecf20Sopenharmony_ci			iounmap(msp_maps[i].virt);
1808c2ecf20Sopenharmony_ci			kfree(msp_parts[i]);
1818c2ecf20Sopenharmony_ci			goto cleanup_loop;
1828c2ecf20Sopenharmony_ci		}
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	return 0;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cicleanup_loop:
1888c2ecf20Sopenharmony_ci	while (i--) {
1898c2ecf20Sopenharmony_ci		mtd_device_unregister(msp_flash[i]);
1908c2ecf20Sopenharmony_ci		map_destroy(msp_flash[i]);
1918c2ecf20Sopenharmony_ci		kfree(msp_maps[i].name);
1928c2ecf20Sopenharmony_ci		iounmap(msp_maps[i].virt);
1938c2ecf20Sopenharmony_ci		kfree(msp_parts[i]);
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci	kfree(msp_maps);
1968c2ecf20Sopenharmony_cifree_msp_parts:
1978c2ecf20Sopenharmony_ci	kfree(msp_parts);
1988c2ecf20Sopenharmony_cifree_msp_flash:
1998c2ecf20Sopenharmony_ci	kfree(msp_flash);
2008c2ecf20Sopenharmony_ci	return ret;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic void __exit cleanup_msp_flash(void)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	int i;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	for (i = 0; i < fcnt; i++) {
2088c2ecf20Sopenharmony_ci		mtd_device_unregister(msp_flash[i]);
2098c2ecf20Sopenharmony_ci		map_destroy(msp_flash[i]);
2108c2ecf20Sopenharmony_ci		iounmap((void *)msp_maps[i].virt);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		/* free the memory */
2138c2ecf20Sopenharmony_ci		kfree(msp_maps[i].name);
2148c2ecf20Sopenharmony_ci		kfree(msp_parts[i]);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	kfree(msp_flash);
2188c2ecf20Sopenharmony_ci	kfree(msp_parts);
2198c2ecf20Sopenharmony_ci	kfree(msp_maps);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ciMODULE_AUTHOR("PMC-Sierra, Inc");
2238c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MTD map driver for PMC-Sierra MSP boards");
2248c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cimodule_init(init_msp_flash);
2278c2ecf20Sopenharmony_cimodule_exit(cleanup_msp_flash);
228