// SPDX-License-Identifier: GPL-2.0+
/*
 * Normal mappings of chips in physical memory
 *
 * Copyright (C) 2003 MontaVista Software Inc.
 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
 *
 * 031022 - [jsun] add run-time configure and partition setup
 *
 * Device tree support:
 *    Copyright (C) 2006 MontaVista Software Inc.
 *    Author: Vitaly Wool <vwool@ru.mvista.com>
 *
 *    Revised to handle newer style flash binding by:
 *    Copyright (C) 2007 David Gibson, IBM Corporation.
 *
 * GPIO address extension:
 *    Handle the case where a flash device is mostly addressed using physical
 *    line and supplemented by GPIOs.  This way you can hook up say a 8MiB flash
 *    to a 2MiB memory range and use the GPIOs to select a particular range.
 *
 *    Copyright © 2000 Nicolas Pitre <nico@cam.org>
 *    Copyright © 2005-2009 Analog Devices Inc.
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/physmap.h>
#include <linux/mtd/concat.h>
#include <linux/mtd/cfi_endian.h>
#include <linux/io.h>
#include <linux/of_device.h>
#include <linux/pm_runtime.h>
#include <linux/gpio/consumer.h>

#include "physmap-bt1-rom.h"
#include "physmap-gemini.h"
#include "physmap-ixp4xx.h"
#include "physmap-versatile.h"

struct physmap_flash_info {
	unsigned int		nmaps;
	struct mtd_info		**mtds;
	struct mtd_info		*cmtd;
	struct map_info		*maps;
	spinlock_t		vpp_lock;
	int			vpp_refcnt;
	const char		*probe_type;
	const char * const	*part_types;
	unsigned int		nparts;
	const struct mtd_partition *parts;
	struct gpio_descs	*gpios;
	unsigned int		gpio_values;
	unsigned int		win_order;
};

static int physmap_flash_remove(struct platform_device *dev)
{
	struct physmap_flash_info *info;
	struct physmap_flash_data *physmap_data;
	int i;

	info = platform_get_drvdata(dev);

	if (info->cmtd) {
		WARN_ON(mtd_device_unregister(info->cmtd));

		if (info->cmtd != info->mtds[0])
			mtd_concat_destroy(info->cmtd);
	}

	for (i = 0; i < info->nmaps; i++) {
		if (info->mtds[i])
			map_destroy(info->mtds[i]);
	}

	physmap_data = dev_get_platdata(&dev->dev);
	if (physmap_data && physmap_data->exit)
		physmap_data->exit(dev);

	pm_runtime_put(&dev->dev);
	pm_runtime_disable(&dev->dev);
	return 0;
}

static void physmap_set_vpp(struct map_info *map, int state)
{
	struct platform_device *pdev;
	struct physmap_flash_data *physmap_data;
	struct physmap_flash_info *info;
	unsigned long flags;

	pdev = (struct platform_device *)map->map_priv_1;
	physmap_data = dev_get_platdata(&pdev->dev);

	if (!physmap_data->set_vpp)
		return;

	info = platform_get_drvdata(pdev);

	spin_lock_irqsave(&info->vpp_lock, flags);
	if (state) {
		if (++info->vpp_refcnt == 1)    /* first nested 'on' */
			physmap_data->set_vpp(pdev, 1);
	} else {
		if (--info->vpp_refcnt == 0)    /* last nested 'off' */
			physmap_data->set_vpp(pdev, 0);
	}
	spin_unlock_irqrestore(&info->vpp_lock, flags);
}

#if IS_ENABLED(CONFIG_MTD_PHYSMAP_GPIO_ADDR)
static void physmap_set_addr_gpios(struct physmap_flash_info *info,
				   unsigned long ofs)
{
	unsigned int i;

	ofs >>= info->win_order;
	if (info->gpio_values == ofs)
		return;

	for (i = 0; i < info->gpios->ndescs; i++) {
		if ((BIT(i) & ofs) == (BIT(i) & info->gpio_values))
			continue;

		gpiod_set_value(info->gpios->desc[i], !!(BIT(i) & ofs));
	}

	info->gpio_values = ofs;
}

#define win_mask(order)		(BIT(order) - 1)

static map_word physmap_addr_gpios_read(struct map_info *map,
					unsigned long ofs)
{
	struct platform_device *pdev;
	struct physmap_flash_info *info;
	map_word mw;
	u16 word;

	pdev = (struct platform_device *)map->map_priv_1;
	info = platform_get_drvdata(pdev);
	physmap_set_addr_gpios(info, ofs);

	word = readw(map->virt + (ofs & win_mask(info->win_order)));
	mw.x[0] = word;
	return mw;
}

static void physmap_addr_gpios_copy_from(struct map_info *map, void *buf,
					 unsigned long ofs, ssize_t len)
{
	struct platform_device *pdev;
	struct physmap_flash_info *info;

	pdev = (struct platform_device *)map->map_priv_1;
	info = platform_get_drvdata(pdev);

	while (len) {
		unsigned int winofs = ofs & win_mask(info->win_order);
		unsigned int chunklen = min_t(unsigned int, len,
					      BIT(info->win_order) - winofs);

		physmap_set_addr_gpios(info, ofs);
		memcpy_fromio(buf, map->virt + winofs, chunklen);
		len -= chunklen;
		buf += chunklen;
		ofs += chunklen;
	}
}

static void physmap_addr_gpios_write(struct map_info *map, map_word mw,
				     unsigned long ofs)
{
	struct platform_device *pdev;
	struct physmap_flash_info *info;
	u16 word;

	pdev = (struct platform_device *)map->map_priv_1;
	info = platform_get_drvdata(pdev);
	physmap_set_addr_gpios(info, ofs);

	word = mw.x[0];
	writew(word, map->virt + (ofs & win_mask(info->win_order)));
}

static void physmap_addr_gpios_copy_to(struct map_info *map, unsigned long ofs,
				       const void *buf, ssize_t len)
{
	struct platform_device *pdev;
	struct physmap_flash_info *info;

	pdev = (struct platform_device *)map->map_priv_1;
	info = platform_get_drvdata(pdev);

	while (len) {
		unsigned int winofs = ofs & win_mask(info->win_order);
		unsigned int chunklen = min_t(unsigned int, len,
					      BIT(info->win_order) - winofs);

		physmap_set_addr_gpios(info, ofs);
		memcpy_toio(map->virt + winofs, buf, chunklen);
		len -= chunklen;
		buf += chunklen;
		ofs += chunklen;
	}
}

static int physmap_addr_gpios_map_init(struct map_info *map)
{
	map->phys = NO_XIP;
	map->read = physmap_addr_gpios_read;
	map->copy_from = physmap_addr_gpios_copy_from;
	map->write = physmap_addr_gpios_write;
	map->copy_to = physmap_addr_gpios_copy_to;

	return 0;
}
#else
static int physmap_addr_gpios_map_init(struct map_info *map)
{
	return -ENOTSUPP;
}
#endif

#if IS_ENABLED(CONFIG_MTD_PHYSMAP_OF)
static const struct of_device_id of_flash_match[] = {
	{
		.compatible = "cfi-flash",
		.data = "cfi_probe",
	},
	{
		/*
		 * FIXME: JEDEC chips can't be safely and reliably
		 * probed, although the mtd code gets it right in
		 * practice most of the time.  We should use the
		 * vendor and device ids specified by the binding to
		 * bypass the heuristic probe code, but the mtd layer
		 * provides, at present, no interface for doing so
		 * :(.
		 */
		.compatible = "jedec-flash",
		.data = "jedec_probe",
	},
	{
		.compatible = "mtd-ram",
		.data = "map_ram",
	},
	{
		.compatible = "mtd-rom",
		.data = "map_rom",
	},
	{
		.type = "rom",
		.compatible = "direct-mapped"
	},
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, of_flash_match);

static const char * const of_default_part_probes[] = {
	"cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL
};

static const char * const *of_get_part_probes(struct platform_device *dev)
{
	struct device_node *dp = dev->dev.of_node;
	const char **res;
	int count;

	count = of_property_count_strings(dp, "linux,part-probe");
	if (count < 0)
		return of_default_part_probes;

	res = devm_kcalloc(&dev->dev, count + 1, sizeof(*res), GFP_KERNEL);
	if (!res)
		return NULL;

	count = of_property_read_string_array(dp, "linux,part-probe", res,
					      count);
	if (count < 0)
		return NULL;

	return res;
}

static const char *of_select_probe_type(struct platform_device *dev)
{
	struct device_node *dp = dev->dev.of_node;
	const struct of_device_id *match;
	const char *probe_type;

	match = of_match_device(of_flash_match, &dev->dev);
	if (!match)
		return NULL;

	probe_type = match->data;
	if (probe_type)
		return probe_type;

	dev_warn(&dev->dev,
		 "Device tree uses obsolete \"direct-mapped\" flash binding\n");

	of_property_read_string(dp, "probe-type", &probe_type);
	if (!probe_type)
		return NULL;

	if (!strcmp(probe_type, "CFI")) {
		probe_type = "cfi_probe";
	} else if (!strcmp(probe_type, "JEDEC")) {
		probe_type = "jedec_probe";
	} else if (!strcmp(probe_type, "ROM")) {
		probe_type = "map_rom";
	} else {
		dev_warn(&dev->dev,
			 "obsolete_probe: don't know probe type '%s', mapping as rom\n",
			 probe_type);
		probe_type = "map_rom";
	}

	return probe_type;
}

static int physmap_flash_of_init(struct platform_device *dev)
{
	struct physmap_flash_info *info = platform_get_drvdata(dev);
	struct device_node *dp = dev->dev.of_node;
	const char *mtd_name = NULL;
	int err, swap = 0;
	bool map_indirect;
	unsigned int i;
	u32 bankwidth;

	if (!dp)
		return -EINVAL;

	info->probe_type = of_select_probe_type(dev);

	info->part_types = of_get_part_probes(dev);
	if (!info->part_types)
		return -ENOMEM;

	of_property_read_string(dp, "linux,mtd-name", &mtd_name);

	map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access");

	err = of_property_read_u32(dp, "bank-width", &bankwidth);
	if (err) {
		dev_err(&dev->dev, "Can't get bank width from device tree\n");
		return err;
	}

	if (of_property_read_bool(dp, "big-endian"))
		swap = CFI_BIG_ENDIAN;
	else if (of_property_read_bool(dp, "little-endian"))
		swap = CFI_LITTLE_ENDIAN;

	for (i = 0; i < info->nmaps; i++) {
		info->maps[i].name = mtd_name;
		info->maps[i].swap = swap;
		info->maps[i].bankwidth = bankwidth;
		info->maps[i].device_node = dp;

		err = of_flash_probe_bt1_rom(dev, dp, &info->maps[i]);
		if (err)
			return err;

		err = of_flash_probe_gemini(dev, dp, &info->maps[i]);
		if (err)
			return err;

		err = of_flash_probe_ixp4xx(dev, dp, &info->maps[i]);
		if (err)
			return err;

		err = of_flash_probe_versatile(dev, dp, &info->maps[i]);
		if (err)
			return err;

		/*
		 * On some platforms (e.g. MPC5200) a direct 1:1 mapping
		 * may cause problems with JFFS2 usage, as the local bus (LPB)
		 * doesn't support unaligned accesses as implemented in the
		 * JFFS2 code via memcpy(). By setting NO_XIP, the
		 * flash will not be exposed directly to the MTD users
		 * (e.g. JFFS2) any more.
		 */
		if (map_indirect)
			info->maps[i].phys = NO_XIP;
	}

	return 0;
}
#else /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */
#define of_flash_match NULL

static int physmap_flash_of_init(struct platform_device *dev)
{
	return -ENOTSUPP;
}
#endif /* IS_ENABLED(CONFIG_MTD_PHYSMAP_OF) */

static const char * const rom_probe_types[] = {
	"cfi_probe", "jedec_probe", "qinfo_probe", "map_rom",
};

static const char * const part_probe_types[] = {
	"cmdlinepart", "RedBoot", "afs", NULL
};

static int physmap_flash_pdata_init(struct platform_device *dev)
{
	struct physmap_flash_info *info = platform_get_drvdata(dev);
	struct physmap_flash_data *physmap_data;
	unsigned int i;
	int err;

	physmap_data = dev_get_platdata(&dev->dev);
	if (!physmap_data)
		return -EINVAL;

	info->probe_type = physmap_data->probe_type;
	info->part_types = physmap_data->part_probe_types ? : part_probe_types;
	info->parts = physmap_data->parts;
	info->nparts = physmap_data->nr_parts;

	if (physmap_data->init) {
		err = physmap_data->init(dev);
		if (err)
			return err;
	}

	for (i = 0; i < info->nmaps; i++) {
		info->maps[i].bankwidth = physmap_data->width;
		info->maps[i].pfow_base = physmap_data->pfow_base;
		info->maps[i].set_vpp = physmap_set_vpp;
	}

	return 0;
}

static int physmap_flash_probe(struct platform_device *dev)
{
	struct physmap_flash_info *info;
	int err = 0;
	int i;

	if (!dev->dev.of_node && !dev_get_platdata(&dev->dev))
		return -EINVAL;

	info = devm_kzalloc(&dev->dev, sizeof(*info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps))
		info->nmaps++;

	if (!info->nmaps)
		return -ENODEV;

	info->maps = devm_kzalloc(&dev->dev,
				  sizeof(*info->maps) * info->nmaps,
				  GFP_KERNEL);
	if (!info->maps)
		return -ENOMEM;

	info->mtds = devm_kzalloc(&dev->dev,
				  sizeof(*info->mtds) * info->nmaps,
				  GFP_KERNEL);
	if (!info->mtds)
		return -ENOMEM;

	platform_set_drvdata(dev, info);

	info->gpios = devm_gpiod_get_array_optional(&dev->dev, "addr",
						    GPIOD_OUT_LOW);
	if (IS_ERR(info->gpios))
		return PTR_ERR(info->gpios);

	if (info->gpios && info->nmaps > 1) {
		dev_err(&dev->dev, "addr-gpios only supported for nmaps == 1\n");
		return -EINVAL;
	}

	pm_runtime_enable(&dev->dev);
	pm_runtime_get_sync(&dev->dev);

	if (dev->dev.of_node)
		err = physmap_flash_of_init(dev);
	else
		err = physmap_flash_pdata_init(dev);

	if (err) {
		pm_runtime_put(&dev->dev);
		pm_runtime_disable(&dev->dev);
		return err;
	}

	for (i = 0; i < info->nmaps; i++) {
		struct resource *res;

		res = platform_get_resource(dev, IORESOURCE_MEM, i);
		info->maps[i].virt = devm_ioremap_resource(&dev->dev, res);
		if (IS_ERR(info->maps[i].virt)) {
			err = PTR_ERR(info->maps[i].virt);
			goto err_out;
		}

		dev_notice(&dev->dev, "physmap platform flash device: %pR\n",
			   res);

		if (!info->maps[i].name)
			info->maps[i].name = dev_name(&dev->dev);

		if (!info->maps[i].phys)
			info->maps[i].phys = res->start;

		info->win_order = fls64(resource_size(res)) - 1;
		info->maps[i].size = BIT(info->win_order +
					 (info->gpios ?
					  info->gpios->ndescs : 0));

		info->maps[i].map_priv_1 = (unsigned long)dev;

		if (info->gpios) {
			err = physmap_addr_gpios_map_init(&info->maps[i]);
			if (err)
				goto err_out;
		}

#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
		/*
		 * Only use the simple_map implementation if map hooks are not
		 * implemented. Since map->read() is mandatory checking for its
		 * presence is enough.
		 */
		if (!info->maps[i].read)
			simple_map_init(&info->maps[i]);
#else
		simple_map_init(&info->maps[i]);
#endif

		if (info->probe_type) {
			info->mtds[i] = do_map_probe(info->probe_type,
						     &info->maps[i]);

			/* Fall back to mapping region as ROM */
			if (!info->mtds[i] && IS_ENABLED(CONFIG_MTD_ROM) &&
			    strcmp(info->probe_type, "map_rom")) {
				dev_warn(&dev->dev,
					 "map_probe() failed for type %s\n",
					 info->probe_type);

				info->mtds[i] = do_map_probe("map_rom",
							     &info->maps[i]);
			}
		} else {
			int j;

			for (j = 0; j < ARRAY_SIZE(rom_probe_types); j++) {
				info->mtds[i] = do_map_probe(rom_probe_types[j],
							     &info->maps[i]);
				if (info->mtds[i])
					break;
			}
		}

		if (!info->mtds[i]) {
			dev_err(&dev->dev, "map_probe failed\n");
			err = -ENXIO;
			goto err_out;
		}
		info->mtds[i]->dev.parent = &dev->dev;
	}

	if (info->nmaps == 1) {
		info->cmtd = info->mtds[0];
	} else {
		/*
		 * We detected multiple devices. Concatenate them together.
		 */
		info->cmtd = mtd_concat_create(info->mtds, info->nmaps,
					       dev_name(&dev->dev));
		if (!info->cmtd)
			err = -ENXIO;
	}
	if (err)
		goto err_out;

	spin_lock_init(&info->vpp_lock);

	mtd_set_of_node(info->cmtd, dev->dev.of_node);
	err = mtd_device_parse_register(info->cmtd, info->part_types, NULL,
					info->parts, info->nparts);
	if (err)
		goto err_out;

	return 0;

err_out:
	physmap_flash_remove(dev);
	return err;
}

#ifdef CONFIG_PM
static void physmap_flash_shutdown(struct platform_device *dev)
{
	struct physmap_flash_info *info = platform_get_drvdata(dev);
	int i;

	for (i = 0; i < info->nmaps && info->mtds[i]; i++)
		if (mtd_suspend(info->mtds[i]) == 0)
			mtd_resume(info->mtds[i]);
}
#else
#define physmap_flash_shutdown NULL
#endif

static struct platform_driver physmap_flash_driver = {
	.probe		= physmap_flash_probe,
	.remove		= physmap_flash_remove,
	.shutdown	= physmap_flash_shutdown,
	.driver		= {
		.name	= "physmap-flash",
		.of_match_table = of_flash_match,
	},
};

#ifdef CONFIG_MTD_PHYSMAP_COMPAT
static struct physmap_flash_data physmap_flash_data = {
	.width		= CONFIG_MTD_PHYSMAP_BANKWIDTH,
};

static struct resource physmap_flash_resource = {
	.start		= CONFIG_MTD_PHYSMAP_START,
	.end		= CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
	.flags		= IORESOURCE_MEM,
};

static struct platform_device physmap_flash = {
	.name		= "physmap-flash",
	.id		= 0,
	.dev		= {
		.platform_data	= &physmap_flash_data,
	},
	.num_resources	= 1,
	.resource	= &physmap_flash_resource,
};
#endif

static int __init physmap_init(void)
{
	int err;

	err = platform_driver_register(&physmap_flash_driver);
#ifdef CONFIG_MTD_PHYSMAP_COMPAT
	if (err == 0) {
		err = platform_device_register(&physmap_flash);
		if (err)
			platform_driver_unregister(&physmap_flash_driver);
	}
#endif

	return err;
}

static void __exit physmap_exit(void)
{
#ifdef CONFIG_MTD_PHYSMAP_COMPAT
	platform_device_unregister(&physmap_flash);
#endif
	platform_driver_unregister(&physmap_flash_driver);
}

module_init(physmap_init);
module_exit(physmap_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
MODULE_DESCRIPTION("Generic configurable MTD map driver");

/* legacy platform drivers can't hotplug or coldplg */
#ifndef CONFIG_MTD_PHYSMAP_COMPAT
/* work with hotplug and coldplug */
MODULE_ALIAS("platform:physmap-flash");
#endif
