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 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 495a3fa75aSNicolas Saenz Julienne return count; 505a3fa75aSNicolas Saenz Julienne } 515a3fa75aSNicolas Saenz Julienne 525a3fa75aSNicolas Saenz Julienne static int rmem_probe(struct platform_device *pdev) 535a3fa75aSNicolas Saenz Julienne { 545a3fa75aSNicolas Saenz Julienne struct nvmem_config config = { }; 555a3fa75aSNicolas Saenz Julienne struct device *dev = &pdev->dev; 565a3fa75aSNicolas Saenz Julienne struct reserved_mem *mem; 575a3fa75aSNicolas Saenz Julienne struct rmem *priv; 585a3fa75aSNicolas Saenz Julienne 595a3fa75aSNicolas Saenz Julienne priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 605a3fa75aSNicolas Saenz Julienne if (!priv) 615a3fa75aSNicolas Saenz Julienne return -ENOMEM; 625a3fa75aSNicolas Saenz Julienne priv->dev = dev; 635a3fa75aSNicolas Saenz Julienne 645a3fa75aSNicolas Saenz Julienne mem = of_reserved_mem_lookup(dev->of_node); 655a3fa75aSNicolas Saenz Julienne if (!mem) { 665a3fa75aSNicolas Saenz Julienne dev_err(dev, "Failed to lookup reserved memory\n"); 675a3fa75aSNicolas Saenz Julienne return -EINVAL; 685a3fa75aSNicolas Saenz Julienne } 695a3fa75aSNicolas Saenz Julienne priv->mem = mem; 705a3fa75aSNicolas Saenz Julienne 715a3fa75aSNicolas Saenz Julienne config.dev = dev; 725a3fa75aSNicolas Saenz Julienne config.priv = priv; 735a3fa75aSNicolas Saenz Julienne config.name = "rmem"; 74*09dd7b99SPhil Elwell config.id = NVMEM_DEVID_AUTO; 755a3fa75aSNicolas Saenz Julienne config.size = mem->size; 765a3fa75aSNicolas Saenz Julienne config.reg_read = rmem_read; 775a3fa75aSNicolas Saenz Julienne 785a3fa75aSNicolas Saenz Julienne return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); 795a3fa75aSNicolas Saenz Julienne } 805a3fa75aSNicolas Saenz Julienne 815a3fa75aSNicolas Saenz Julienne static const struct of_device_id rmem_match[] = { 825a3fa75aSNicolas Saenz Julienne { .compatible = "nvmem-rmem", }, 835a3fa75aSNicolas Saenz Julienne { /* sentinel */ }, 845a3fa75aSNicolas Saenz Julienne }; 855a3fa75aSNicolas Saenz Julienne MODULE_DEVICE_TABLE(of, rmem_match); 865a3fa75aSNicolas Saenz Julienne 875a3fa75aSNicolas Saenz Julienne static struct platform_driver rmem_driver = { 885a3fa75aSNicolas Saenz Julienne .probe = rmem_probe, 895a3fa75aSNicolas Saenz Julienne .driver = { 905a3fa75aSNicolas Saenz Julienne .name = "rmem", 915a3fa75aSNicolas Saenz Julienne .of_match_table = rmem_match, 925a3fa75aSNicolas Saenz Julienne }, 935a3fa75aSNicolas Saenz Julienne }; 945a3fa75aSNicolas Saenz Julienne module_platform_driver(rmem_driver); 955a3fa75aSNicolas Saenz Julienne 965a3fa75aSNicolas Saenz Julienne MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>"); 975a3fa75aSNicolas Saenz Julienne MODULE_DESCRIPTION("Reserved Memory Based nvmem Driver"); 985a3fa75aSNicolas Saenz Julienne MODULE_LICENSE("GPL"); 99