/src/barebox/drivers/nvmem/rmem.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de> |
4 | | */ |
5 | | |
6 | | #include <io.h> |
7 | | #include <driver.h> |
8 | | #include <linux/bitmap.h> |
9 | | #include <linux/nvmem-provider.h> |
10 | | #include <init.h> |
11 | | |
12 | | struct rmem { |
13 | | struct device *dev; |
14 | | const struct resource *mem; |
15 | | unsigned long *protection_bitmap; |
16 | | size_t size; |
17 | | }; |
18 | | |
19 | | static int rmem_read(void *context, unsigned int offset, |
20 | | void *val, size_t bytes) |
21 | 0 | { |
22 | 0 | struct rmem *rmem = context; |
23 | 0 | return mem_copy(rmem->dev, val, (void *)rmem->mem->start + offset, |
24 | 0 | bytes, offset, 0); |
25 | 0 | } |
26 | | |
27 | | /** |
28 | | * rmem_write() - Write data to the NVMEM device. |
29 | | * @context: Pointer to the rmem private data (struct rmem). |
30 | | * @offset: Offset within the NVMEM device to write to. |
31 | | * @val: Buffer containing the data to write. |
32 | | * @bytes: Number of bytes to write. |
33 | | * |
34 | | * This function is called by the NVMEM core to write data to the |
35 | | * reserved memory region. It first checks the protection bitmap to ensure |
36 | | * the target range is not write-protected. If writable, it uses the |
37 | | * custom 'mem_copy' function. |
38 | | * |
39 | | * Return: 0 on success, or a negative error code on failure. |
40 | | */ |
41 | | static int rmem_write(void *context, unsigned int offset, const void *val, |
42 | | size_t bytes) |
43 | 0 | { |
44 | 0 | unsigned long protected_offset; |
45 | 0 | struct rmem *rmem = context; |
46 | |
|
47 | 0 | protected_offset = find_next_bit(rmem->protection_bitmap, |
48 | 0 | offset + bytes, offset); |
49 | 0 | if (protected_offset < offset + bytes) |
50 | 0 | return -EROFS; |
51 | | |
52 | | /* |
53 | | * The last two arguments to mem_copy (0, 0) are specific to |
54 | | * the custom mem_copy implementation. |
55 | | */ |
56 | 0 | return mem_copy(rmem->dev, (void *)rmem->mem->start + offset, val, |
57 | 0 | bytes, 0, 0); |
58 | 0 | } |
59 | | |
60 | | /** |
61 | | * rmem_protect() - NVMEM callback to change protection status of a range. |
62 | | * @context: Pointer to the rmem private data (struct rmem). |
63 | | * @offset: Starting offset of the range. |
64 | | * @bytes: Length of the range in bytes. |
65 | | * @prot_mode: Protection mode to apply (PROTECT_DISABLE_WRITE or |
66 | | * PROTECT_ENABLE_WRITE). |
67 | | * |
68 | | * This function is called by the NVMEM core to enable or disable |
69 | | * write protection for a specified memory range. |
70 | | * |
71 | | * Return: 0 on success, or a negative error code on failure. |
72 | | */ |
73 | | static int rmem_protect(void *context, unsigned int offset, size_t bytes, |
74 | | int prot_mode) |
75 | 0 | { |
76 | 0 | struct rmem *rmem = context; |
77 | 0 | int ret = 0; |
78 | |
|
79 | 0 | if (offset + bytes > rmem->size) |
80 | 0 | return -EINVAL; |
81 | | |
82 | 0 | switch (prot_mode) { |
83 | 0 | case PROTECT_DISABLE_WRITE: /* Make read-only */ |
84 | 0 | bitmap_set(rmem->protection_bitmap, offset, bytes); |
85 | 0 | break; |
86 | 0 | case PROTECT_ENABLE_WRITE: /* Make writable */ |
87 | 0 | bitmap_clear(rmem->protection_bitmap, offset, bytes); |
88 | 0 | break; |
89 | 0 | default: |
90 | 0 | dev_warn(rmem->dev, "%s: Invalid protection mode %d\n", |
91 | 0 | __func__, prot_mode); |
92 | 0 | ret = -EINVAL; |
93 | 0 | break; |
94 | 0 | } |
95 | | |
96 | 0 | dev_dbg(rmem->dev, |
97 | 0 | "Protection op complete [0x%x, len %zu], mode %d\n", offset, |
98 | 0 | bytes, prot_mode); |
99 | |
|
100 | 0 | return ret; |
101 | 0 | } |
102 | | |
103 | | static int rmem_probe(struct device *dev) |
104 | 0 | { |
105 | 0 | struct nvmem_config config = { }; |
106 | 0 | struct resource *mem; |
107 | 0 | struct rmem *priv; |
108 | |
|
109 | 0 | mem = dev_get_resource(dev, IORESOURCE_MEM, 0); |
110 | 0 | if (IS_ERR(mem)) |
111 | 0 | return PTR_ERR(mem); |
112 | | |
113 | 0 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
114 | 0 | if (!priv) |
115 | 0 | return -ENOMEM; |
116 | | |
117 | 0 | priv->mem = mem; |
118 | |
|
119 | 0 | config.dev = priv->dev = dev; |
120 | 0 | config.priv = priv; |
121 | 0 | config.name = "rmem"; |
122 | 0 | config.id = NVMEM_DEVID_AUTO; |
123 | 0 | config.size = resource_size(mem); |
124 | 0 | priv->size = config.size; |
125 | |
|
126 | 0 | priv->protection_bitmap = bitmap_xzalloc(priv->size); |
127 | |
|
128 | 0 | config.reg_read = rmem_read; |
129 | 0 | config.reg_write = rmem_write; |
130 | 0 | config.reg_protect = rmem_protect; |
131 | |
|
132 | 0 | return PTR_ERR_OR_ZERO(nvmem_register(&config)); |
133 | 0 | } |
134 | | |
135 | | static const struct of_device_id rmem_match[] = { |
136 | | { .compatible = "nvmem-rmem", }, |
137 | | { /* sentinel */ }, |
138 | | }; |
139 | | MODULE_DEVICE_TABLE(of, rmem_match); |
140 | | |
141 | | static struct driver rmem_driver = { |
142 | | .name = "nvmem-rmem", |
143 | | .of_compatible = rmem_match, |
144 | | .probe = rmem_probe, |
145 | | }; |
146 | | device_platform_driver(rmem_driver); |
147 | | |
148 | | MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>"); |
149 | | MODULE_DESCRIPTION("Reserved Memory Based nvmem Driver"); |
150 | | MODULE_LICENSE("GPL"); |