15a3fa75aSNicolas Saenz Julienne // SPDX-License-Identifier: GPL-2.0+
25a3fa75aSNicolas Saenz Julienne /*
35a3fa75aSNicolas Saenz Julienne * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
45a3fa75aSNicolas Saenz Julienne */
55a3fa75aSNicolas Saenz Julienne
65a3fa75aSNicolas Saenz Julienne #include <linux/io.h>
75a3fa75aSNicolas Saenz Julienne #include <linux/module.h>
85a3fa75aSNicolas Saenz Julienne #include <linux/nvmem-provider.h>
95a3fa75aSNicolas Saenz Julienne #include <linux/of_reserved_mem.h>
105a3fa75aSNicolas Saenz Julienne #include <linux/platform_device.h>
115a3fa75aSNicolas Saenz Julienne
125a3fa75aSNicolas Saenz Julienne struct rmem {
135a3fa75aSNicolas Saenz Julienne struct device *dev;
145a3fa75aSNicolas Saenz Julienne struct nvmem_device *nvmem;
155a3fa75aSNicolas Saenz Julienne struct reserved_mem *mem;
165a3fa75aSNicolas Saenz Julienne
175a3fa75aSNicolas Saenz Julienne phys_addr_t size;
185a3fa75aSNicolas Saenz Julienne };
195a3fa75aSNicolas Saenz Julienne
rmem_read(void * context,unsigned int offset,void * val,size_t bytes)205a3fa75aSNicolas Saenz Julienne static int rmem_read(void *context, unsigned int offset,
215a3fa75aSNicolas Saenz Julienne void *val, size_t bytes)
225a3fa75aSNicolas Saenz Julienne {
235a3fa75aSNicolas Saenz Julienne struct rmem *priv = context;
245a3fa75aSNicolas Saenz Julienne size_t available = priv->mem->size;
255a3fa75aSNicolas Saenz Julienne loff_t off = offset;
265a3fa75aSNicolas Saenz Julienne void *addr;
275a3fa75aSNicolas Saenz Julienne int count;
285a3fa75aSNicolas Saenz Julienne
295a3fa75aSNicolas Saenz Julienne /*
305a3fa75aSNicolas Saenz Julienne * Only map the reserved memory at this point to avoid potential rogue
315a3fa75aSNicolas Saenz Julienne * kernel threads inadvertently modifying it. Based on the current
325a3fa75aSNicolas Saenz Julienne * uses-cases for this driver, the performance hit isn't a concern.
335a3fa75aSNicolas Saenz Julienne * Nor is likely to be, given the nature of the subsystem. Most nvmem
345a3fa75aSNicolas Saenz Julienne * devices operate over slow buses to begin with.
355a3fa75aSNicolas Saenz Julienne *
365a3fa75aSNicolas Saenz Julienne * An alternative would be setting the memory as RO, set_memory_ro(),
375a3fa75aSNicolas Saenz Julienne * but as of Dec 2020 this isn't possible on arm64.
385a3fa75aSNicolas Saenz Julienne */
395a3fa75aSNicolas Saenz Julienne addr = memremap(priv->mem->base, available, MEMREMAP_WB);
4058e92c4aSWei Yongjun if (!addr) {
415a3fa75aSNicolas Saenz Julienne dev_err(priv->dev, "Failed to remap memory region\n");
4258e92c4aSWei Yongjun return -ENOMEM;
435a3fa75aSNicolas Saenz Julienne }
445a3fa75aSNicolas Saenz Julienne
455a3fa75aSNicolas Saenz Julienne count = memory_read_from_buffer(val, bytes, &off, addr, available);
465a3fa75aSNicolas Saenz Julienne
475a3fa75aSNicolas Saenz Julienne memunmap(addr);
485a3fa75aSNicolas Saenz Julienne
49*5f97b616SJoy Chakraborty if (count < 0)
505a3fa75aSNicolas Saenz Julienne return count;
51*5f97b616SJoy Chakraborty
52*5f97b616SJoy Chakraborty return count == bytes ? 0 : -EIO;
535a3fa75aSNicolas Saenz Julienne }
545a3fa75aSNicolas Saenz Julienne
rmem_probe(struct platform_device * pdev)555a3fa75aSNicolas Saenz Julienne static int rmem_probe(struct platform_device *pdev)
565a3fa75aSNicolas Saenz Julienne {
575a3fa75aSNicolas Saenz Julienne struct nvmem_config config = { };
585a3fa75aSNicolas Saenz Julienne struct device *dev = &pdev->dev;
595a3fa75aSNicolas Saenz Julienne struct reserved_mem *mem;
605a3fa75aSNicolas Saenz Julienne struct rmem *priv;
615a3fa75aSNicolas Saenz Julienne
625a3fa75aSNicolas Saenz Julienne priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
635a3fa75aSNicolas Saenz Julienne if (!priv)
645a3fa75aSNicolas Saenz Julienne return -ENOMEM;
655a3fa75aSNicolas Saenz Julienne priv->dev = dev;
665a3fa75aSNicolas Saenz Julienne
675a3fa75aSNicolas Saenz Julienne mem = of_reserved_mem_lookup(dev->of_node);
685a3fa75aSNicolas Saenz Julienne if (!mem) {
695a3fa75aSNicolas Saenz Julienne dev_err(dev, "Failed to lookup reserved memory\n");
705a3fa75aSNicolas Saenz Julienne return -EINVAL;
715a3fa75aSNicolas Saenz Julienne }
725a3fa75aSNicolas Saenz Julienne priv->mem = mem;
735a3fa75aSNicolas Saenz Julienne
745a3fa75aSNicolas Saenz Julienne config.dev = dev;
755a3fa75aSNicolas Saenz Julienne config.priv = priv;
765a3fa75aSNicolas Saenz Julienne config.name = "rmem";
7709dd7b99SPhil Elwell config.id = NVMEM_DEVID_AUTO;
785a3fa75aSNicolas Saenz Julienne config.size = mem->size;
795a3fa75aSNicolas Saenz Julienne config.reg_read = rmem_read;
805a3fa75aSNicolas Saenz Julienne
815a3fa75aSNicolas Saenz Julienne return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
825a3fa75aSNicolas Saenz Julienne }
835a3fa75aSNicolas Saenz Julienne
845a3fa75aSNicolas Saenz Julienne static const struct of_device_id rmem_match[] = {
855a3fa75aSNicolas Saenz Julienne { .compatible = "nvmem-rmem", },
865a3fa75aSNicolas Saenz Julienne { /* sentinel */ },
875a3fa75aSNicolas Saenz Julienne };
885a3fa75aSNicolas Saenz Julienne MODULE_DEVICE_TABLE(of, rmem_match);
895a3fa75aSNicolas Saenz Julienne
905a3fa75aSNicolas Saenz Julienne static struct platform_driver rmem_driver = {
915a3fa75aSNicolas Saenz Julienne .probe = rmem_probe,
925a3fa75aSNicolas Saenz Julienne .driver = {
935a3fa75aSNicolas Saenz Julienne .name = "rmem",
945a3fa75aSNicolas Saenz Julienne .of_match_table = rmem_match,
955a3fa75aSNicolas Saenz Julienne },
965a3fa75aSNicolas Saenz Julienne };
975a3fa75aSNicolas Saenz Julienne module_platform_driver(rmem_driver);
985a3fa75aSNicolas Saenz Julienne
995a3fa75aSNicolas Saenz Julienne MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
1005a3fa75aSNicolas Saenz Julienne MODULE_DESCRIPTION("Reserved Memory Based nvmem Driver");
1015a3fa75aSNicolas Saenz Julienne MODULE_LICENSE("GPL");
102