Skip to content

Commit efbf43c

Browse files
yangguangcai1xiaoxiang781216
authored andcommitted
driver/mem:add Mem Driver.
Signed-off-by: yangguangcai <[email protected]>
1 parent a2e7265 commit efbf43c

File tree

7 files changed

+316
-0
lines changed

7 files changed

+316
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
==============
2+
DEVMEM Drivers
3+
==============
4+
5+
The `devmem` driver provides an interface for accessing memory-mapped
6+
I/O in an embedded system. This driver allows for reading, writing, and
7+
memory mapping of specific memory regions or device registers.
8+
9+
``read()``: This function reads data from the memory-mapped I/O address
10+
space into the buffer provided by the caller. The first byte read
11+
corresponds to the address specified by the device's "current memory
12+
address". The addresses for subsequent bytes depend on the auto-increment
13+
behavior of the specific device.
14+
15+
``write()``: This function transfers data from the provided data buffer by
16+
the caller to the memory-mapped I/O address space. The first byte written
17+
corresponds to the address specified by the device's "current memory address".
18+
19+
``mmap()``: The `mmap()` function provides a mechanism to map a device's
20+
memory region into the user space, allowing direct access to device
21+
registers or memory regions. The mapped region can be accessed using
22+
normal memory operations.
23+
24+
The function requires a base address and size for the memory region
25+
to be mapped. If successful, it returns a pointer to the mapped region.
26+
If mapping fails, it returns `EINVAL` and `errno` is set appropriately.

Documentation/components/drivers/special/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ following section.
2525
audio.rst
2626
clk.rst
2727
devicetree.rst
28+
devmem.rst
2829
dma.rst
2930
framebuffer.rst
3031
i2c.rst

drivers/drivers_initialize.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ void drivers_initialize(void)
129129
devzero_register(); /* Standard /dev/zero */
130130
#endif
131131

132+
#ifdef CONFIG_DEV_MEM
133+
devmem_register();
134+
#endif
135+
132136
#if defined(CONFIG_DEV_LOOP)
133137
loop_register(); /* Standard /dev/loop */
134138
#endif

drivers/misc/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ config DEV_ZERO
1515
bool "Enable /dev/zero"
1616
default n
1717

18+
config DEV_MEM
19+
bool "Enable /dev/mem"
20+
default n
21+
---help---
22+
It is a full image of physical memory and can be used to
23+
access physical memory.
24+
1825
config DEV_ASCII
1926
bool "Enable /dev/ascii"
2027
default n

drivers/misc/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ ifeq ($(CONFIG_DEV_ZERO),y)
3030
CSRCS += dev_zero.c
3131
endif
3232

33+
ifeq ($(CONFIG_DEV_MEM),y)
34+
CSRCS += dev_mem.c
35+
endif
36+
3337
ifeq ($(CONFIG_DEV_ASCII),y)
3438
CSRCS += dev_ascii.c
3539
endif

drivers/misc/dev_mem.c

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
/****************************************************************************
2+
* drivers/misc/dev_mem.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/drivers/drivers.h>
26+
#include <nuttx/kmalloc.h>
27+
#include <nuttx/memoryregion.h>
28+
#include <sys/param.h>
29+
#include <sys/mman.h>
30+
31+
/****************************************************************************
32+
* Pre-processor Definitions
33+
****************************************************************************/
34+
35+
#define DEVMEM_REGION 8
36+
37+
/****************************************************************************
38+
* Public Data
39+
****************************************************************************/
40+
41+
extern uint8_t _stext[]; /* Start of .text */
42+
extern uint8_t _etext[]; /* End_1 of .text + .rodata */
43+
extern uint8_t _sdata[]; /* Start of .data */
44+
extern uint8_t _edata[]; /* End+1 of .data */
45+
extern uint8_t _sbss[]; /* Start of .bss */
46+
extern uint8_t _ebss[]; /* End+1 of .bss */
47+
48+
/****************************************************************************
49+
* Private Function Prototypes
50+
****************************************************************************/
51+
52+
/* Character driver methods */
53+
54+
static ssize_t devmem_read(FAR struct file *filep, FAR char *buffer,
55+
size_t buflen);
56+
static ssize_t devmem_write(FAR struct file *filep, FAR const char *buffer,
57+
size_t buflen);
58+
static int devmem_mmap(FAR struct file *filep,
59+
FAR struct mm_map_entry_s *map);
60+
61+
/****************************************************************************
62+
* Private Data
63+
****************************************************************************/
64+
65+
static const struct file_operations g_devmem_fops =
66+
{
67+
NULL, /* open */
68+
NULL, /* close */
69+
devmem_read, /* read */
70+
devmem_write, /* write */
71+
NULL, /* seek */
72+
NULL, /* ioctl */
73+
devmem_mmap, /* mmap */
74+
};
75+
76+
/****************************************************************************
77+
* Private Functions
78+
****************************************************************************/
79+
80+
/****************************************************************************
81+
* Name: devmem_read
82+
****************************************************************************/
83+
84+
static ssize_t devmem_read(FAR struct file *filep, FAR char *buffer,
85+
size_t buflen)
86+
{
87+
FAR struct memory_region_s *region = filep->f_inode->i_private;
88+
uintptr_t src = filep->f_pos;
89+
uintptr_t start;
90+
uintptr_t end;
91+
ssize_t len;
92+
int i;
93+
94+
DEBUGASSERT(region && src);
95+
96+
for (i = 0; i < DEVMEM_REGION; i++)
97+
{
98+
if (region[i].start == 0 && region[i].end == 0)
99+
{
100+
break;
101+
}
102+
103+
start = MAX(src, region[i].start);
104+
end = MIN(src + buflen, region[i].end);
105+
len = end - start;
106+
if (len > 0 && (region[i].flags & PROT_READ))
107+
{
108+
memcpy(buffer, (FAR const void *)start, len);
109+
return len;
110+
}
111+
}
112+
113+
return -EINVAL;
114+
}
115+
116+
/****************************************************************************
117+
* Name: devmem_write
118+
****************************************************************************/
119+
120+
static ssize_t devmem_write(FAR struct file *filep, FAR const char *buffer,
121+
size_t buflen)
122+
{
123+
FAR struct memory_region_s *region = filep->f_inode->i_private;
124+
uintptr_t dest = filep->f_pos;
125+
uintptr_t start;
126+
uintptr_t end;
127+
ssize_t len;
128+
int i;
129+
130+
DEBUGASSERT(region && dest);
131+
132+
for (i = 0; i < DEVMEM_REGION; i++)
133+
{
134+
if (region[i].start == 0 && region[i].end == 0)
135+
{
136+
break;
137+
}
138+
139+
start = MAX(dest, region[i].start);
140+
end = MIN(dest + buflen, region[i].end);
141+
len = end - start;
142+
if (len > 0 && (region[i].flags & PROT_WRITE))
143+
{
144+
memcpy((FAR void *)start, buffer, len);
145+
return len;
146+
}
147+
}
148+
149+
return -EINVAL;
150+
}
151+
152+
/****************************************************************************
153+
* Name: devmem_mmap
154+
****************************************************************************/
155+
156+
static int devmem_mmap(FAR struct file *filep,
157+
FAR struct mm_map_entry_s *map)
158+
{
159+
FAR struct memory_region_s *region = filep->f_inode->i_private;
160+
uintptr_t start;
161+
uintptr_t end;
162+
int i;
163+
164+
DEBUGASSERT(region);
165+
166+
if (map->offset < 0)
167+
{
168+
return -EINVAL;
169+
}
170+
171+
start = map->offset;
172+
end = start + map->length;
173+
174+
for (i = 0; i < DEVMEM_REGION; i++)
175+
{
176+
if (region[i].start == 0 && region[i].end == 0)
177+
{
178+
break;
179+
}
180+
181+
if (start >= region[i].start && end <= region[i].end)
182+
{
183+
map->vaddr = (FAR void *)start;
184+
return 0;
185+
}
186+
}
187+
188+
return -EINVAL;
189+
}
190+
191+
/****************************************************************************
192+
* Public Functions
193+
****************************************************************************/
194+
195+
/****************************************************************************
196+
* Name: devmem_register
197+
*
198+
* Description:
199+
* Create an MEM driver.
200+
*
201+
* Returned Value:
202+
* Zero (OK) on success; A negated errno value on failure.
203+
*
204+
****************************************************************************/
205+
206+
int devmem_register(void)
207+
{
208+
FAR struct memory_region_s *region;
209+
bool merge = (_edata == _sbss);
210+
ssize_t len = 0;
211+
int ret;
212+
213+
region = kmm_calloc(DEVMEM_REGION, sizeof(*region));
214+
if (region == NULL)
215+
{
216+
return -ENOMEM;
217+
}
218+
219+
#ifdef CONFIG_BOARD_MEMORY_RANGE
220+
len = parse_memory_region(CONFIG_BOARD_MEMORY_RANGE, region,
221+
DEVMEM_REGION - 1);
222+
if (len < 0)
223+
{
224+
kmm_free(region);
225+
return len;
226+
}
227+
#endif
228+
229+
if (len + (4 - merge) > DEVMEM_REGION)
230+
{
231+
len = DEVMEM_REGION - (4 - merge);
232+
}
233+
234+
region[len].flags = PROT_EXEC | PROT_READ;
235+
region[len].start = (uintptr_t)_stext;
236+
region[len++].end = (uintptr_t)_etext;
237+
region[len].flags = PROT_WRITE | PROT_READ;
238+
region[len].start = (uintptr_t)_sdata;
239+
region[len++].end = (uintptr_t)_edata;
240+
241+
if (merge)
242+
{
243+
region[len - 1].end = (uintptr_t)_ebss;
244+
}
245+
else
246+
{
247+
region[len].flags = PROT_WRITE | PROT_READ;
248+
region[len].start = (uintptr_t)_sbss;
249+
region[len++].end = (uintptr_t)_ebss;
250+
}
251+
252+
/* register the new MEM driver */
253+
254+
ret = register_driver("/dev/mem", &g_devmem_fops, 0666, region);
255+
if (ret < 0)
256+
{
257+
kmm_free(region);
258+
return -ENOMEM;
259+
}
260+
261+
return ret;
262+
}

include/nuttx/drivers/drivers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ void devurandom_register(void);
144144

145145
void devcrypto_register(void);
146146

147+
/****************************************************************************
148+
* Name: devmem_register
149+
*
150+
* Description:
151+
* Register devmem driver
152+
*
153+
****************************************************************************/
154+
155+
#ifdef CONFIG_DEV_MEM
156+
int devmem_register(void);
157+
#endif
158+
147159
/****************************************************************************
148160
* Name: devzero_register
149161
*

0 commit comments

Comments
 (0)