embox

Форк
0
99 строк · 1.9 Кб
1
/**
2
 * @brief Common interface for flash subsystem
3
 *
4
 * @date 21.08.2013
5
 * @author Andrey Gazukin
6
 * @author Denis Deryugin
7
 */
8

9
#include <errno.h>
10
#include <string.h>
11

12
#include <drivers/block_dev.h>
13
#include <drivers/flash/flash.h>
14

15
#include <mem/misc/pool.h>
16
#include <util/err.h>
17
#include <lib/libds/indexator.h>
18

19
#include <framework/mod/options.h>
20

21
#define MAX_FLASHDEV_QUANTITY OPTION_GET(NUMBER, dev_quantity)
22

23
POOL_DEF(flash_pool, struct flash_dev, MAX_FLASHDEV_QUANTITY);
24
INDEX_DEF(flash_idx, 0, MAX_FLASHDEV_QUANTITY);
25

26
static struct flash_dev *flashdev_tab[MAX_FLASHDEV_QUANTITY];
27

28
struct flash_dev *flash_alloc(void) {
29
	struct flash_dev *flash;
30
	int idx;
31

32
	if (NULL == (flash = pool_alloc(&flash_pool))) {
33
		return err2ptr(ENOMEM);
34
	}
35

36
	idx = index_alloc(&flash_idx, INDEX_MIN);
37
	if (idx == INDEX_NONE) {
38
		pool_free(&flash_pool, flash);
39
		return err2ptr(ENOMEM);
40
	}
41

42
	memset(flash, 0, sizeof(*flash));
43
	flash->idx = idx;
44

45
	flashdev_tab[idx] = flash;
46

47
	return flash;
48
}
49

50
int flash_free(struct flash_dev *dev) {
51
	assert(dev);
52

53
	index_free(&flash_idx, dev->idx);
54
	flashdev_tab[dev->idx] = 0;
55
	pool_free(&flash_pool, dev);
56

57
	return 0;
58
}
59

60
static int flash_initialized = 0;
61
struct flash_dev *flash_by_id(int idx) {
62
	if (idx < 0 || idx >= MAX_FLASHDEV_QUANTITY) {
63
		return NULL;
64
	}
65

66
	if (!flash_initialized) {
67
		flash_devs_init();
68
	}
69

70
	return flashdev_tab[idx];
71
}
72

73
int flash_max_id(void) {
74
	return MAX_FLASHDEV_QUANTITY;
75
}
76

77
ARRAY_SPREAD_DEF(const struct flash_dev_module, __flash_dev_registry);
78
int flash_devs_init(void) {
79
	int ret;
80
	const struct flash_dev_module *fdev_module;
81

82
	if (flash_initialized) {
83
		return 0;
84
	}
85

86
	flash_initialized = 1;
87

88
	/* TODO pass some device-specific data to init funcion? */
89
	array_spread_foreach_ptr(fdev_module, __flash_dev_registry) {
90
		if (fdev_module->dev_drv->flash_init != NULL) {
91
			ret = fdev_module->dev_drv->flash_init(NULL, NULL);
92
			if (ret != 0) {
93
				return ret;
94
			}
95
		}
96
	}
97

98
	return 0;
99
}
100

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.