Skip to content

Commit 13fa233

Browse files
de-nordickartben
authored andcommitted
drivers/flash: Flash API: device size getter
The commit adds flash_get_size API functions. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 93541e2 commit 13fa233

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

drivers/flash/flash_handlers.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2017 Intel Corporation
3+
* Copyright (c) 2024 Nordic Semiconductor ASA
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -36,6 +37,14 @@ static inline int z_vrfy_flash_erase(const struct device *dev, off_t offset,
3637
}
3738
#include <zephyr/syscalls/flash_erase_mrsh.c>
3839

40+
static inline int z_vrfy_flash_get_size(const struct device *dev, uint64_t *size)
41+
{
42+
K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_FLASH));
43+
K_OOPS(K_SYSCALL_MEMORY_WRITE(size, sizeof(size)));
44+
return z_impl_flash_get_size((const struct device *)dev, size);
45+
}
46+
#include <zephyr/syscalls/flash_get_size_mrsh.c>
47+
3948
static inline size_t z_vrfy_flash_get_write_block_size(const struct device *dev)
4049
{
4150
K_OOPS(K_SYSCALL_OBJ(dev, K_OBJ_DRIVER_FLASH));

include/zephyr/drivers/flash.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ typedef int (*flash_api_write)(const struct device *dev, off_t offset,
155155
typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
156156
size_t size);
157157

158+
/**
159+
* @brief Get device size in bytes.
160+
*
161+
* Returns total logical device size in bytes.
162+
*
163+
* @param[in] dev flash device.
164+
* @param[out] size device size in bytes.
165+
*
166+
* @return 0 on success, negative errno code on error.
167+
*/
168+
typedef int (*flash_api_get_size)(const struct device *dev, uint64_t *size);
169+
158170
typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
159171

160172
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
@@ -195,6 +207,7 @@ __subsystem struct flash_driver_api {
195207
flash_api_write write;
196208
flash_api_erase erase;
197209
flash_api_get_parameters get_parameters;
210+
flash_api_get_size get_size;
198211
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
199212
flash_api_pages_layout page_layout;
200213
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
@@ -321,6 +334,33 @@ static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
321334
return rc;
322335
}
323336

337+
/**
338+
* @brief Get device size in bytes.
339+
*
340+
* Returns total logical device size in bytes. Not all devices may support
341+
* returning size, specifically those with non uniform page layouts or banked,
342+
* in which case the function will return -ENOTSUP, and user has to rely
343+
* on Flash page layout functions enabled by CONFIG_FLASH_PAGE_LAYOUT.
344+
*
345+
* @param[in] dev flash device.
346+
* @param[out] size device size in bytes.
347+
*
348+
* @return 0 on success, negative errno code on error.
349+
*/
350+
__syscall int flash_get_size(const struct device *dev, uint64_t *size);
351+
352+
static inline int z_impl_flash_get_size(const struct device *dev, uint64_t *size)
353+
{
354+
int rc = -ENOSYS;
355+
const struct flash_driver_api *api = (const struct flash_driver_api *)dev->api;
356+
357+
if (api->get_size != NULL) {
358+
rc = api->get_size(dev, size);
359+
}
360+
361+
return rc;
362+
}
363+
324364
/**
325365
* @brief Fill selected range of device with specified value
326366
*

0 commit comments

Comments
 (0)