|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * SRAM mmap misc driver for ADI processor on-chip memory |
| 4 | + * |
| 5 | + * (C) Copyright 2022 - Analog Devices, Inc. |
| 6 | + * |
| 7 | + * Written and/or maintained by Timesys Corporation |
| 8 | + * |
| 9 | + * Contact: Nathan Barrett-Morrison <nathan.morrison@timesys.com> |
| 10 | + * Contact: Greg Malysa <greg.malysa@timesys.com> |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +#include <linux/fs.h> |
| 15 | +#include <linux/mm.h> |
| 16 | +#include <linux/module.h> |
| 17 | +#include <linux/uaccess.h> |
| 18 | +#include <linux/slab.h> |
| 19 | +#include <linux/of.h> |
| 20 | +#include <linux/of_device.h> |
| 21 | +#include <linux/genalloc.h> |
| 22 | +#include <linux/miscdevice.h> |
| 23 | +#include <linux/platform_device.h> |
| 24 | + |
| 25 | +#define SRAM_MMAP_DRV_NAME "sram_mmap" |
| 26 | + |
| 27 | +struct adi_sram_mmap { |
| 28 | + struct miscdevice miscdev; |
| 29 | + struct gen_pool *sram_pool; |
| 30 | +}; |
| 31 | + |
| 32 | +struct mmap_private_data { |
| 33 | + struct gen_pool *pool; |
| 34 | + unsigned long vaddr; |
| 35 | +}; |
| 36 | + |
| 37 | +static void mmap_open(struct vm_area_struct *vma) |
| 38 | +{ |
| 39 | + struct mmap_private_data *pdata = vma->vm_private_data; |
| 40 | + size_t sram_size = vma->vm_end - vma->vm_start; |
| 41 | + |
| 42 | + /* Alloc the virtual address from specific sram_pool */ |
| 43 | + pdata->vaddr = gen_pool_alloc(pdata->pool, sram_size); |
| 44 | + if (!pdata->vaddr) |
| 45 | + pr_err("Failed to alloc memory from sram pool!\n"); |
| 46 | +} |
| 47 | + |
| 48 | +static void mmap_close(struct vm_area_struct *vma) |
| 49 | +{ |
| 50 | + struct mmap_private_data *pdata = vma->vm_private_data; |
| 51 | + size_t sram_size = vma->vm_end - vma->vm_start; |
| 52 | + |
| 53 | + gen_pool_free(pdata->pool, pdata->vaddr, sram_size); |
| 54 | + kfree(pdata); |
| 55 | +} |
| 56 | + |
| 57 | +const struct vm_operations_struct sram_mmap_vm_ops = { |
| 58 | + .open = mmap_open, |
| 59 | + .close = mmap_close, |
| 60 | +}; |
| 61 | + |
| 62 | +static int sram_mmap(struct file *fp, struct vm_area_struct *vma) |
| 63 | +{ |
| 64 | + struct adi_sram_mmap *sram = container_of(fp->private_data, |
| 65 | + struct adi_sram_mmap, miscdev); |
| 66 | + struct mmap_private_data *pdata; |
| 67 | + size_t sram_size = vma->vm_end - vma->vm_start; |
| 68 | + unsigned long paddr; |
| 69 | + int ret = 0; |
| 70 | + |
| 71 | + /* Allocate private pdata */ |
| 72 | + pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 73 | + if (!pdata) |
| 74 | + return -ENOMEM; |
| 75 | + |
| 76 | + pdata->pool = sram->sram_pool; |
| 77 | + vma->vm_private_data = pdata; |
| 78 | + vma->vm_ops = &sram_mmap_vm_ops; |
| 79 | + vma->vm_ops->open(vma); |
| 80 | + |
| 81 | + if (!pdata->vaddr) { |
| 82 | + ret = -EAGAIN; |
| 83 | + goto out_free; |
| 84 | + } |
| 85 | + |
| 86 | + paddr = gen_pool_virt_to_phys(pdata->pool, pdata->vaddr); |
| 87 | + |
| 88 | + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 89 | + |
| 90 | + if (io_remap_pfn_range(vma, vma->vm_start, |
| 91 | + __phys_to_pfn(paddr), sram_size, |
| 92 | + vma->vm_page_prot)) { |
| 93 | + pr_err("Unable to mmap sram\n"); |
| 94 | + ret = -EAGAIN; |
| 95 | + goto out_free; |
| 96 | + } |
| 97 | + pr_info("sram mmaped 0x%lx : 0x%lx successfully!\n", |
| 98 | + paddr, paddr + sram_size); |
| 99 | + |
| 100 | + return 0; |
| 101 | + |
| 102 | +out_free: |
| 103 | + kfree(pdata); |
| 104 | + return ret; |
| 105 | +} |
| 106 | + |
| 107 | +static const struct file_operations sram_fops = { |
| 108 | + .mmap = sram_mmap, |
| 109 | +}; |
| 110 | + |
| 111 | +static const struct of_device_id adi_sram_mmap_of_match[] = { |
| 112 | + { .compatible = "adi,sram-mmap" }, |
| 113 | + { }, |
| 114 | +}; |
| 115 | +MODULE_DEVICE_TABLE(of, adi_sram_mmap_of_match); |
| 116 | + |
| 117 | +static int adi_sram_mmap_probe(struct platform_device *pdev) |
| 118 | +{ |
| 119 | + int ret = 0; |
| 120 | + struct adi_sram_mmap *sram; |
| 121 | + const struct of_device_id *match; |
| 122 | + struct device *dev; |
| 123 | + |
| 124 | + dev = &pdev->dev; |
| 125 | + |
| 126 | + /* Allocate sram device data */ |
| 127 | + sram = devm_kzalloc(dev, sizeof(*sram), GFP_KERNEL); |
| 128 | + if (!sram) |
| 129 | + return -ENOMEM; |
| 130 | + |
| 131 | + match = of_match_device(of_match_ptr(adi_sram_mmap_of_match), &pdev->dev); |
| 132 | + if (!match) { |
| 133 | + pr_err("No sram mmap device defined in dts file\n"); |
| 134 | + return -ENODEV; |
| 135 | + } |
| 136 | + sram->sram_pool = of_gen_pool_get(dev->of_node, "adi,sram", 0); |
| 137 | + if (!sram->sram_pool) { |
| 138 | + pr_err("Unable to get sram pool!\n"); |
| 139 | + return -ENODEV; |
| 140 | + } |
| 141 | + |
| 142 | + dev_set_drvdata(&pdev->dev, sram); |
| 143 | + |
| 144 | + sram->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 145 | + sram->miscdev.name = SRAM_MMAP_DRV_NAME; |
| 146 | + sram->miscdev.fops = &sram_fops; |
| 147 | + sram->miscdev.parent = dev; |
| 148 | + |
| 149 | + ret = misc_register(&sram->miscdev); |
| 150 | + if (ret < 0) |
| 151 | + pr_err("Failed to register sram mmap misc device\n"); |
| 152 | + |
| 153 | + return ret; |
| 154 | +} |
| 155 | + |
| 156 | +static int adi_sram_mmap_remove(struct platform_device *pdev) |
| 157 | +{ |
| 158 | + struct adi_sram_mmap *sram = dev_get_drvdata(&pdev->dev); |
| 159 | + |
| 160 | + misc_deregister(&sram->miscdev); |
| 161 | + |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +static struct platform_driver adi_sram_mmap_driver = { |
| 166 | + .probe = adi_sram_mmap_probe, |
| 167 | + .remove = adi_sram_mmap_remove, |
| 168 | + .driver = { |
| 169 | + .name = SRAM_MMAP_DRV_NAME, |
| 170 | + .of_match_table = of_match_ptr(adi_sram_mmap_of_match), |
| 171 | + }, |
| 172 | +}; |
| 173 | + |
| 174 | +module_platform_driver(adi_sram_mmap_driver); |
| 175 | +MODULE_DESCRIPTION("SRAM mmap misc driver for ADI processor on-chip memory"); |
| 176 | +MODULE_LICENSE("GPL"); |
0 commit comments