From 9e8e00e1718dedf037196da7c375b0d68f2fe8b0 Mon Sep 17 00:00:00 2001 From: Yifeng Zhao Date: Tue, 28 Mar 2023 11:10:30 +0800 Subject: [PATCH] soc: rockchip: vendor_storage: add ram buffer for gki Uboot reads vendor storage data and stores it in memory. This patch provides a read api for other drivers to use. Signed-off-by: Yifeng Zhao Change-Id: Ie830195b447afa2d466786a0d177391130efba16 --- drivers/soc/rockchip/Kconfig | 6 + drivers/soc/rockchip/Makefile | 1 + drivers/soc/rockchip/ram_vendor_storage.c | 214 ++++++++++++++++++++++ drivers/soc/rockchip/rk_vendor_storage.c | 16 +- 4 files changed, 228 insertions(+), 9 deletions(-) create mode 100644 drivers/soc/rockchip/ram_vendor_storage.c diff --git a/drivers/soc/rockchip/Kconfig b/drivers/soc/rockchip/Kconfig index 0b0a73a9575d..33e33c7ac52a 100644 --- a/drivers/soc/rockchip/Kconfig +++ b/drivers/soc/rockchip/Kconfig @@ -190,6 +190,12 @@ config ROCKCHIP_MTD_VENDOR_STORAGE help Say y here to enable rockchip mtd vendor storage support. +config ROCKCHIP_RAM_VENDOR_STORAGE + tristate "Rockchip ram vendor storage support" + depends on ROCKCHIP_VENDOR_STORAGE + help + Say y here to enable rockchip ram vendor storage support. + config ROCKCHIP_VENDOR_STORAGE_UPDATE_LOADER bool "Rockchip vendor storage update loader support" default n diff --git a/drivers/soc/rockchip/Makefile b/drivers/soc/rockchip/Makefile index 8a1a8420b08d..fb32129eb169 100644 --- a/drivers/soc/rockchip/Makefile +++ b/drivers/soc/rockchip/Makefile @@ -15,6 +15,7 @@ obj-$(CONFIG_ROCKCHIP_VENDOR_STORAGE) += rk_vendor_storage.o obj-$(CONFIG_ROCKCHIP_MMC_VENDOR_STORAGE) += sdmmc_vendor_storage.o obj-$(CONFIG_ROCKCHIP_FLASH_VENDOR_STORAGE) += flash_vendor_storage.o obj-$(CONFIG_ROCKCHIP_MTD_VENDOR_STORAGE) += mtd_vendor_storage.o +obj-$(CONFIG_ROCKCHIP_RAM_VENDOR_STORAGE) += ram_vendor_storage.o obj-$(CONFIG_ROCKCHIP_IPA) += rockchip_ipa.o obj-$(CONFIG_ROCKCHIP_OPP) += rockchip_opp_select.o obj-$(CONFIG_ROCKCHIP_PERFORMANCE) += rockchip_performance.o diff --git a/drivers/soc/rockchip/ram_vendor_storage.c b/drivers/soc/rockchip/ram_vendor_storage.c new file mode 100644 index 000000000000..eb9768d88d3d --- /dev/null +++ b/drivers/soc/rockchip/ram_vendor_storage.c @@ -0,0 +1,214 @@ +// SPDX-License-Identifier: (GPL-2.0+ OR MIT) + +/* Copyright (c) 2023 Rockchip Electronics Co., Ltd */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct vendor_info *g_vendor; + +static int ram_vendor_read(u32 id, void *pbuf, u32 size) +{ + u32 i; + + if (!g_vendor) + return -ENOMEM; + + for (i = 0; i < g_vendor->item_num; i++) { + if (g_vendor->item[i].id == id) { + if (size > g_vendor->item[i].size) + size = g_vendor->item[i].size; + memcpy(pbuf, &g_vendor->data[g_vendor->item[i].offset], size); + return size; + } + } + + return (-1); +} + +static int ram_vendor_storage_open(struct inode *inode, struct file *file) +{ + return 0; +} + +static int ram_vendor_storage_release(struct inode *inode, struct file *file) +{ + return 0; +} + +static long ram_vendor_storage_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + long ret = -1; + int size; + struct RK_VENDOR_REQ *v_req; + u32 *page_buf; + + page_buf = kmalloc(4096, GFP_KERNEL); + if (!page_buf) + return -ENOMEM; + + v_req = (struct RK_VENDOR_REQ *)page_buf; + + switch (cmd) { + case VENDOR_READ_IO: + { + if (copy_from_user(page_buf, (void __user *)arg, 8)) { + ret = -EFAULT; + break; + } + if (v_req->tag == VENDOR_REQ_TAG && v_req->len <= 4096 - 8) { + size = ram_vendor_read(v_req->id, v_req->data, v_req->len); + if (size != -1) { + v_req->len = size; + ret = 0; + if (copy_to_user((void __user *)arg, + page_buf, + v_req->len + 8)) + ret = -EFAULT; + } + } + } break; + + case VENDOR_WRITE_IO: + default: + ret = -EINVAL; + goto exit; + } +exit: + kfree(page_buf); + return ret; +} + +static const struct file_operations vendor_storage_fops = { + .open = ram_vendor_storage_open, + .compat_ioctl = ram_vendor_storage_ioctl, + .unlocked_ioctl = ram_vendor_storage_ioctl, + .release = ram_vendor_storage_release, +}; + +static struct miscdevice vender_storage_dev = { + .minor = MISC_DYNAMIC_MINOR, + .name = "vendor_storage", + .fops = &vendor_storage_fops, +}; + +static void *ram_vendor_stroage_map(phys_addr_t start, size_t len) +{ + int i; + void *vaddr; + pgprot_t pgprot = PAGE_KERNEL; + phys_addr_t phys; + int npages = PAGE_ALIGN(len) / PAGE_SIZE; + struct page **p = vmalloc(sizeof(struct page *) * npages); + + if (!p) + return NULL; + + phys = start; + for (i = 0; i < npages; i++) { + p[i] = phys_to_page(phys); + phys += PAGE_SIZE; + } + + vaddr = vmap(p, npages, VM_MAP, pgprot); + vfree(p); + + return vaddr; +} + +static int ram_vendor_storage_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct device_node *node; + struct resource res; + int ret; + phys_addr_t size, start; + + if (g_vendor) + return -EINVAL; + + node = of_parse_phandle(np, "memory-region", 0); + if (!node) + return -ENOMEM; + + ret = of_address_to_resource(node, 0, &res); + if (ret) + return ret; + + ret = -EINVAL; + + size = resource_size(&res); + start = res.start; + if (size != VENDOR_PART_SIZE << 9 || (start & (PAGE_SIZE - 1))) + goto un_reserved; + + g_vendor = ram_vendor_stroage_map(start, size); + if (IS_ERR(g_vendor)) + goto un_reserved; + + if (g_vendor->tag != VENDOR_HEAD_TAG) + goto un_remap; + + misc_register(&vender_storage_dev); + rk_vendor_register(ram_vendor_read, NULL); + + return 0; + +un_remap: + vunmap(g_vendor); +un_reserved: +#ifndef MODULE + free_reserved_area(phys_to_virt(start), phys_to_virt(start) + size, -1, "memory-region"); +#endif + g_vendor = NULL; + + return ret; +} + +static int ram_vendor_storage_remove(struct platform_device *pdev) +{ + if (g_vendor) { + misc_deregister(&vender_storage_dev); + vunmap(g_vendor); + g_vendor = NULL; + } + + return 0; +} + +static const struct of_device_id dt_match[] = { + { .compatible = "rockchip,ram-vendor-storage" }, + {} +}; + +static struct platform_driver vendor_storage_driver = { + .probe = ram_vendor_storage_probe, + .remove = ram_vendor_storage_remove, + .driver = { + .name = "vendor-storage", + .of_match_table = dt_match, + }, +}; + +module_platform_driver(vendor_storage_driver); +MODULE_LICENSE("GPL"); diff --git a/drivers/soc/rockchip/rk_vendor_storage.c b/drivers/soc/rockchip/rk_vendor_storage.c index 5c49667e7e5c..a8a316e47ff9 100644 --- a/drivers/soc/rockchip/rk_vendor_storage.c +++ b/drivers/soc/rockchip/rk_vendor_storage.c @@ -11,8 +11,8 @@ #include #include -int (*_vendor_read)(u32 id, void *pbuf, u32 size) = NULL; -int (*_vendor_write)(u32 id, void *pbuf, u32 size) = NULL; +static int (*_vendor_read)(u32 id, void *pbuf, u32 size); +static int (*_vendor_write)(u32 id, void *pbuf, u32 size); int rk_vendor_read(u32 id, void *pbuf, u32 size) { @@ -32,18 +32,16 @@ EXPORT_SYMBOL(rk_vendor_write); int rk_vendor_register(void *read, void *write) { - if (!_vendor_read) { - _vendor_read = read; - _vendor_write = write; - return 0; - } - return -1; + _vendor_read = read; + _vendor_write = write; + + return 0; } EXPORT_SYMBOL(rk_vendor_register); bool is_rk_vendor_ready(void) { - if (_vendor_read && _vendor_write) + if (_vendor_read) return true; return false; }