summaryrefslogtreecommitdiff
path: root/zephyr/include/drivers/cros_flash.h
blob: d7078cfb83286f09b2ceafb6fb4a95c1e9d93f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/**
 * @file
 * @brief Chrome OS-specific API for flash memory access
 * This exists only support the interface expected by the Chrome OS EC. It seems
 * better to implement this so we can make use of most of the existing code in
 * its keyboard_scan.c file and thus make sure we operate the same way.
 *
 * It provides raw access to flash memory module.
 */

#ifndef ZEPHYR_INCLUDE_DRIVERS_CROS_FLASH_H_
#define ZEPHYR_INCLUDE_DRIVERS_CROS_FLASH_H_

#include <zephyr/device.h>
#include <zephyr/kernel.h>

/**
 * @brief CROS Flash Driver APIs
 * @defgroup cros_flash_interface CROS Flash Driver APIs
 * @ingroup io_interfaces
 * @{
 */

/**
 * @cond INTERNAL_HIDDEN
 *
 * cros keyboard raw driver API definition and system call entry points
 *
 * (Internal use only.)
 */
typedef int (*cros_flash_api_init)(const struct device *dev);

typedef int (*cros_flash_api_physical_write)(const struct device *dev,
					     int offset, int size,
					     const char *data);

typedef int (*cros_flash_api_physical_erase)(const struct device *dev,
					     int offset, int size);

typedef int (*cros_flash_api_physical_get_protect)(const struct device *dev,
						   int bank);

typedef uint32_t (*cros_flash_api_physical_get_protect_flags)(
	const struct device *dev);

typedef int (*cros_flash_api_physical_protect_at_boot)(const struct device *dev,
						       uint32_t new_flags);

typedef int (*cros_flash_api_physical_protect_now)(const struct device *dev,
						   int all);

typedef int (*cros_flash_api_physical_get_jedec_id)(const struct device *dev,
						    uint8_t *manufacturer,
						    uint16_t *device);

typedef int (*cros_flash_api_physical_get_status)(const struct device *dev,
						  uint8_t *sr1, uint8_t *sr2);

__subsystem struct cros_flash_driver_api {
	cros_flash_api_init init;
	cros_flash_api_physical_write physical_write;
	cros_flash_api_physical_erase physical_erase;
	cros_flash_api_physical_get_protect physical_get_protect;
	cros_flash_api_physical_get_protect_flags physical_get_protect_flags;
	cros_flash_api_physical_protect_at_boot physical_protect_at_boot;
	cros_flash_api_physical_protect_now physical_protect_now;
	cros_flash_api_physical_get_jedec_id physical_get_jedec_id;
	cros_flash_api_physical_get_status physical_get_status;
};

/**
 * @endcond
 */

/**
 * @brief Initialize physical flash.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 *
 * @return 0 If successful.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_init(const struct device *dev);

static inline int z_impl_cros_flash_init(const struct device *dev)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->init) {
		return -ENOTSUP;
	}

	return api->init(dev);
}

/**
 * @brief Write to physical flash.
 *
 * Offset and size must be a multiple of CONFIG_FLASH_WRITE_SIZE.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 * @param offset Flash offset to write.
 * @param size Number of bytes to write.
 * @param data Destination buffer for data.  Must be 32-bit aligned.
 *
 * @return 0 If successful.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_physical_write(const struct device *dev, int offset,
					int size, const char *data);

static inline int z_impl_cros_flash_physical_write(const struct device *dev,
						   int offset, int size,
						   const char *data)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_write) {
		return -ENOTSUP;
	}

	return api->physical_write(dev, offset, size, data);
}

/**
 * @brief Erase physical flash.
 *
 * Offset and size must be a multiple of CONFIG_FLASH_ERASE_SIZE.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 * @param offset	Flash offset to erase.
 * @param size	        Number of bytes to erase.
 *
 * @return 0 If successful.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_physical_erase(const struct device *dev, int offset,
					int size);

static inline int z_impl_cros_flash_physical_erase(const struct device *dev,
						   int offset, int size)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_erase) {
		return -ENOTSUP;
	}

	return api->physical_erase(dev, offset, size);
}

/**
 * @brief Read physical write protect setting for a flash bank.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 * @param bank	Bank index to check.
 *
 * @return non-zero if bank is protected until reboot.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_physical_get_protect(const struct device *dev,
					      int bank);

static inline int
z_impl_cros_flash_physical_get_protect(const struct device *dev, int bank)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_get_protect) {
		return -ENOTSUP;
	}

	return api->physical_get_protect(dev, bank);
}

/* clang-format off */
/**
 * @brief Return flash protect state flags from the physical layer.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 *
 * @retval -ENOTSUP Not supported api function.
 */
__syscall
uint32_t cros_flash_physical_get_protect_flags(const struct device *dev);
/* clang-format on */

static inline uint32_t
z_impl_cros_flash_physical_get_protect_flags(const struct device *dev)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_get_protect_flags) {
		return -ENOTSUP;
	}

	return api->physical_get_protect_flags(dev);
}

/**
 * @brief Enable/disable protecting firmware/pstate at boot.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 * @param new_flags	to protect (only EC_FLASH_PROTECT_*_AT_BOOT are
 * taken care of)
 *
 * @return 0 If successful.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_physical_protect_at_boot(const struct device *dev,
						  uint32_t new_flags);

static inline int
z_impl_cros_flash_physical_protect_at_boot(const struct device *dev,
					   uint32_t new_flags)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_protect_at_boot) {
		return -ENOTSUP;
	}

	return api->physical_protect_at_boot(dev, new_flags);
}

/**
 * @brief Protect now physical flash.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 * @param all	Protect all (=1) or just read-only and pstate (=0).
 *
 * @return 0 If successful.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_physical_protect_now(const struct device *dev,
					      int all);

static inline int
z_impl_cros_flash_physical_protect_now(const struct device *dev, int all)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_protect_now) {
		return -ENOTSUP;
	}

	return api->physical_protect_now(dev, all);
}

/**
 * @brief Get JEDEC manufacturer and device identifiers.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 * @param manufacturer Pointer to data where manufacturer id will be written.
 * @param device Pointer to data where device id will be written.
 *
 * @return 0 If successful.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_physical_get_jedec_id(const struct device *dev,
					       uint8_t *manufacturer,
					       uint16_t *device);

static inline int
z_impl_cros_flash_physical_get_jedec_id(const struct device *dev,
					uint8_t *manufacturer, uint16_t *device)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_get_jedec_id)
		return -ENOTSUP;

	return api->physical_get_jedec_id(dev, manufacturer, device);
}

/**
 * @brief Get status registers.
 *
 * @param dev Pointer to the device structure for the flash driver instance.
 * @param sr1 Pointer to data where status1 register will be written.
 * @param sr2 Pointer to data where status2 register will be written.
 *
 * @return 0 If successful.
 * @retval -ENOTSUP Not supported api function.
 */
__syscall int cros_flash_physical_get_status(const struct device *dev,
					     uint8_t *sr1, uint8_t *sr2);

static inline int
z_impl_cros_flash_physical_get_status(const struct device *dev, uint8_t *sr1,
				      uint8_t *sr2)
{
	const struct cros_flash_driver_api *api =
		(const struct cros_flash_driver_api *)dev->api;

	if (!api->physical_get_status)
		return -ENOTSUP;

	return api->physical_get_status(dev, sr1, sr2);
}

/**
 * @}
 */
#include <syscalls/cros_flash.h>
#endif /* ZEPHYR_INCLUDE_DRIVERS_CROS_FLASH_H_ */