1 /*
2 * Copyright (c) 2018 Savoir-Faire Linux.
3 * Copyright (c) 2020 Peter Bigot Consulting, LLC
4 * Copyright (c) 2023 Intercreate, Inc.
5 *
6 * This driver is heavily inspired from the spi_flash_w25qxxdv.c SPI NOR driver.
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 #define DT_DRV_COMPAT jedec_spi_nor
12
13 #include <errno.h>
14 #include <zephyr/drivers/flash.h>
15 #include <zephyr/drivers/gpio.h>
16 #include <zephyr/drivers/spi.h>
17 #include <zephyr/init.h>
18 #include <string.h>
19 #include <zephyr/logging/log.h>
20 #include <zephyr/sys_clock.h>
21 #include <zephyr/pm/device.h>
22 #include <zephyr/pm/device_runtime.h>
23
24 #include "spi_nor.h"
25 #include "jesd216.h"
26 #include "flash_priv.h"
27
28 LOG_MODULE_REGISTER(spi_nor, CONFIG_FLASH_LOG_LEVEL);
29
30 /* Device Power Management Notes
31 *
32 * These flash devices have several modes during operation:
33 * * When CSn is asserted (during a SPI operation) the device is
34 * active.
35 * * When CSn is deasserted the device enters a standby mode.
36 * * Some devices support a Deep Power-Down mode which reduces current
37 * to as little as 0.1% of standby.
38 *
39 * When mapped to the Zephyr Device Power Management states:
40 * * PM_DEVICE_STATE_ACTIVE covers both active and standby modes;
41 * * PM_DEVICE_STATE_SUSPENDED corresponds to deep-power-down mode;
42 * * PM_DEVICE_STATE_OFF covers the powered off state;
43 */
44
45 #define SPI_NOR_MAX_ADDR_WIDTH 4
46 #define SPI_NOR_3B_ADDR_MAX 0xFFFFFF
47
48 #define ANY_INST_HAS_MXICY_MX25R_POWER_MODE DT_ANY_INST_HAS_PROP_STATUS_OKAY(mxicy_mx25r_power_mode)
49 #define ANY_INST_HAS_DPD DT_ANY_INST_HAS_BOOL_STATUS_OKAY(has_dpd)
50 #define ANY_INST_HAS_T_EXIT_DPD DT_ANY_INST_HAS_PROP_STATUS_OKAY(t_exit_dpd)
51 #define ANY_INST_HAS_DPD_WAKEUP_SEQUENCE DT_ANY_INST_HAS_PROP_STATUS_OKAY(dpd_wakeup_sequence)
52 #define ANY_INST_HAS_RESET_GPIOS DT_ANY_INST_HAS_PROP_STATUS_OKAY(reset_gpios)
53 #define ANY_INST_HAS_WP_GPIOS DT_ANY_INST_HAS_PROP_STATUS_OKAY(wp_gpios)
54 #define ANY_INST_HAS_HOLD_GPIOS DT_ANY_INST_HAS_PROP_STATUS_OKAY(hold_gpios)
55 #define ANY_INST_USE_4B_ADDR_OPCODES DT_ANY_INST_HAS_BOOL_STATUS_OKAY(use_4b_addr_opcodes)
56
57 #ifdef CONFIG_SPI_NOR_ACTIVE_DWELL_MS
58 #define ACTIVE_DWELL_MS CONFIG_SPI_NOR_ACTIVE_DWELL_MS
59 #else
60 #define ACTIVE_DWELL_MS 0
61 #endif
62
63 #define DEV_CFG(_dev_) ((const struct spi_nor_config * const) (_dev_)->config)
64
65 /* MXICY Related defines*/
66 /* MXICY Low-power/high perf mode is second bit in configuration register 2 */
67 #define LH_SWITCH_BIT 9
68
69 #define JEDEC_MACRONIX_ID 0xc2
70 #define JEDEC_MX25R_TYPE_ID 0x28
71
72 /* Build-time data associated with the device. */
73 struct spi_nor_config {
74 /* Devicetree SPI configuration */
75 struct spi_dt_spec spi;
76
77 #if ANY_INST_HAS_RESET_GPIOS
78 const struct gpio_dt_spec reset;
79 #endif
80
81 /* Runtime SFDP stores no static configuration. */
82
83 #ifndef CONFIG_SPI_NOR_SFDP_RUNTIME
84 /* Size of device in bytes, from size property */
85 uint32_t flash_size;
86
87 #ifdef CONFIG_FLASH_PAGE_LAYOUT
88 /* Flash page layout can be determined from devicetree. */
89 struct flash_pages_layout layout;
90 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
91
92 /* Expected JEDEC ID, from jedec-id property */
93 uint8_t jedec_id[SPI_NOR_MAX_ID_LEN];
94
95 #if defined(CONFIG_SPI_NOR_SFDP_MINIMAL)
96 /* Optional support for entering 32-bit address mode. */
97 uint8_t enter_4byte_addr;
98 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
99
100 #if defined(CONFIG_SPI_NOR_SFDP_DEVICETREE)
101 /* Length of BFP structure, in 32-bit words. */
102 uint8_t bfp_len;
103
104 /* Pointer to the BFP table as read from the device
105 * (little-endian stored words), from sfdp-bfp property
106 */
107 const struct jesd216_bfp *bfp;
108 #endif /* CONFIG_SPI_NOR_SFDP_DEVICETREE */
109 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
110
111 /* Optional bits in SR to be cleared on startup.
112 *
113 * This information cannot be derived from SFDP.
114 */
115 uint8_t has_lock;
116
117 #if ANY_INST_HAS_WP_GPIOS
118 /* The write-protect GPIO (wp-gpios) */
119 const struct gpio_dt_spec wp;
120 #endif
121
122 #if ANY_INST_HAS_HOLD_GPIOS
123 /* The hold GPIO (hold-gpios) */
124 const struct gpio_dt_spec hold;
125 #endif
126
127 #if ANY_INST_HAS_DPD
128 uint16_t t_enter_dpd; /* in milliseconds */
129 uint16_t t_dpdd_ms; /* in milliseconds */
130 #if ANY_INST_HAS_T_EXIT_DPD
131 uint16_t t_exit_dpd; /* in milliseconds */
132 #endif
133 #endif
134
135 #if ANY_INST_HAS_DPD_WAKEUP_SEQUENCE
136 uint16_t t_crdp_ms; /* in milliseconds */
137 uint16_t t_rdp_ms; /* in milliseconds */
138 #endif
139
140 #if ANY_INST_HAS_MXICY_MX25R_POWER_MODE
141 bool mxicy_mx25r_power_mode;
142 #endif
143 bool use_4b_addr_opcodes:1;
144
145 /* exist flags for dts opt-ins */
146 bool dpd_exist:1;
147 bool dpd_wakeup_sequence_exist:1;
148 bool mxicy_mx25r_power_mode_exist:1;
149 bool reset_gpios_exist:1;
150 bool requires_ulbpr_exist:1;
151 bool wp_gpios_exist:1;
152 bool hold_gpios_exist:1;
153 };
154
155 /**
156 * struct spi_nor_data - Structure for defining the SPI NOR access
157 * @sem: The semaphore to access to the flash
158 */
159 struct spi_nor_data {
160 struct k_sem sem;
161 #if ANY_INST_HAS_DPD
162 /* Low 32-bits of uptime counter at which device last entered
163 * deep power-down.
164 */
165 uint32_t ts_enter_dpd;
166 #endif
167
168 /* Miscellaneous flags */
169
170 /* If set addressed operations should use 32-bit rather than
171 * 24-bit addresses.
172 *
173 * This is ignored if the access parameter to a command
174 * explicitly specifies 24-bit or 32-bit addressing.
175 */
176 bool flag_access_32bit: 1;
177
178 /* Minimal SFDP stores no dynamic configuration. Runtime and
179 * devicetree store page size and erase_types; runtime also
180 * stores flash size and layout.
181 */
182 #ifndef CONFIG_SPI_NOR_SFDP_MINIMAL
183
184 struct jesd216_erase_type erase_types[JESD216_NUM_ERASE_TYPES];
185
186 /* Number of bytes per page */
187 uint16_t page_size;
188
189 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
190 /* Size of flash, in bytes */
191 uint32_t flash_size;
192
193 #ifdef CONFIG_FLASH_PAGE_LAYOUT
194 struct flash_pages_layout layout;
195 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
196 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
197 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
198 };
199
200 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
201 /* The historically supported erase sizes. */
202 static const struct jesd216_erase_type minimal_erase_types[JESD216_NUM_ERASE_TYPES] = {
203 {
204 .cmd = SPI_NOR_CMD_BE,
205 .exp = 16,
206 },
207 {
208 .cmd = SPI_NOR_CMD_SE,
209 .exp = 12,
210 },
211 };
212 static const struct jesd216_erase_type minimal_erase_types_4b[JESD216_NUM_ERASE_TYPES] = {
213 {
214 .cmd = SPI_NOR_CMD_BE_4B,
215 .exp = 16,
216 },
217 {
218 .cmd = SPI_NOR_CMD_SE_4B,
219 .exp = 12,
220 },
221 };
222 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
223
224
225 /* Register writes should be ready extremely quickly */
226 #define WAIT_READY_REGISTER K_NO_WAIT
227 /* Page writes range from sub-ms to 10ms */
228 #define WAIT_READY_WRITE K_TICKS(1)
229
230 #ifdef CONFIG_SPI_NOR_SLEEP_WHILE_WAITING_UNTIL_READY
231 #define WAIT_READY_ERASE K_MSEC(CONFIG_SPI_NOR_SLEEP_ERASE_MS)
232 #else
233 #define WAIT_READY_ERASE K_NO_WAIT
234 #endif
235
236 static int spi_nor_write_protection_set(const struct device *dev,
237 bool write_protect);
238
239 /* Get pointer to array of supported erase types. Static const for
240 * minimal, data for runtime and devicetree.
241 */
242 static inline const struct jesd216_erase_type *
dev_erase_types(const struct device * dev)243 dev_erase_types(const struct device *dev)
244 {
245 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
246 if (IS_ENABLED(ANY_INST_USE_4B_ADDR_OPCODES) && DEV_CFG(dev)->use_4b_addr_opcodes) {
247 return minimal_erase_types_4b;
248 }
249 return minimal_erase_types;
250 #else /* CONFIG_SPI_NOR_SFDP_MINIMAL */
251 const struct spi_nor_data *data = dev->data;
252
253 return data->erase_types;
254 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
255 }
256
257 /* Get the size of the flash device. Data for runtime, constant for
258 * minimal and devicetree.
259 */
dev_flash_size(const struct device * dev)260 static inline uint32_t dev_flash_size(const struct device *dev)
261 {
262 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
263 const struct spi_nor_data *data = dev->data;
264
265 return data->flash_size;
266 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
267 const struct spi_nor_config *cfg = dev->config;
268
269 return cfg->flash_size;
270 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
271 }
272
273 /* Get the flash device page size. Constant for minimal, data for
274 * runtime and devicetree.
275 */
dev_page_size(const struct device * dev)276 static inline uint16_t dev_page_size(const struct device *dev)
277 {
278 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
279 return DT_INST_PROP_OR(0, page_size, 256);
280 #else /* CONFIG_SPI_NOR_SFDP_MINIMAL */
281 const struct spi_nor_data *data = dev->data;
282
283 return data->page_size;
284 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
285 }
286
287 static const struct flash_parameters flash_nor_parameters = {
288 .write_block_size = 1,
289 .erase_value = 0xff,
290 };
291
292 /* Capture the time at which the device entered deep power-down. */
record_entered_dpd(const struct device * const dev)293 static inline void record_entered_dpd(const struct device *const dev)
294 {
295 #if ANY_INST_HAS_DPD
296 const struct spi_nor_config *const driver_config = dev->config;
297
298 if (driver_config->dpd_exist) {
299 struct spi_nor_data *const driver_data = dev->data;
300
301 driver_data->ts_enter_dpd = k_uptime_get_32();
302 }
303 #else
304 ARG_UNUSED(dev);
305 #endif
306 }
307
308 #if ANY_INST_HAS_DPD
309 /* Check the current time against the time DPD was entered and delay
310 * until it's ok to initiate the DPD exit process.
311 */
delay_until_exit_dpd_ok(const struct device * const dev)312 static inline void delay_until_exit_dpd_ok(const struct device *const dev)
313 {
314 const struct spi_nor_config *const driver_config = dev->config;
315
316 if (driver_config->dpd_exist) {
317 struct spi_nor_data *const driver_data = dev->data;
318 int32_t since = (int32_t)(k_uptime_get_32() - driver_data->ts_enter_dpd);
319
320 /* If the time is negative the 32-bit counter has wrapped,
321 * which is certainly long enough no further delay is
322 * required. Otherwise we have to check whether it's been
323 * long enough taking into account necessary delays for
324 * entering and exiting DPD.
325 */
326 if (since >= 0) {
327 /* Subtract time required for DPD to be reached */
328 since -= driver_config->t_enter_dpd;
329
330 /* Subtract time required in DPD before exit */
331 since -= driver_config->t_dpdd_ms;
332
333 /* If the adjusted time is negative we have to wait
334 * until it reaches zero before we can proceed.
335 */
336 if (since < 0) {
337 k_sleep(K_MSEC((uint32_t)-since));
338 }
339 }
340 }
341 }
342 #endif /* ANY_INST_HAS_DPD */
343
344 /* Indicates that an access command includes bytes for the address.
345 * If not provided the opcode is not followed by address bytes.
346 */
347 #define NOR_ACCESS_ADDRESSED BIT(0)
348
349 /* Indicates that addressed access uses a 24-bit address regardless of
350 * spi_nor_data::flag_32bit_addr.
351 */
352 #define NOR_ACCESS_24BIT_ADDR BIT(1)
353
354 /* Indicates that addressed access uses a 32-bit address regardless of
355 * spi_nor_data::flag_32bit_addr.
356 */
357 #define NOR_ACCESS_32BIT_ADDR BIT(2)
358
359 /* Indicates that an access command is performing a write. If not
360 * provided access is a read.
361 */
362 #define NOR_ACCESS_WRITE BIT(7)
363
364 /*
365 * @brief Send an SPI command
366 *
367 * @param dev Device struct
368 * @param opcode The command to send
369 * @param access flags that determine how the command is constructed.
370 * See NOR_ACCESS_*.
371 * @param addr The address to send
372 * @param data The buffer to store or read the value
373 * @param length The size of the buffer
374 * @return 0 on success, negative errno code otherwise
375 */
spi_nor_access(const struct device * const dev,uint8_t opcode,unsigned int access,off_t addr,void * data,size_t length)376 static int spi_nor_access(const struct device *const dev,
377 uint8_t opcode, unsigned int access,
378 off_t addr, void *data, size_t length)
379 {
380 const struct spi_nor_config *const driver_cfg = dev->config;
381 struct spi_nor_data *const driver_data = dev->data;
382 bool is_addressed = (access & NOR_ACCESS_ADDRESSED) != 0U;
383 bool is_write = (access & NOR_ACCESS_WRITE) != 0U;
384 uint8_t buf[5] = { 0 };
385 struct spi_buf spi_buf[2] = {
386 {
387 .buf = buf,
388 .len = 1,
389 },
390 {
391 .buf = data,
392 .len = length
393 }
394 };
395
396 buf[0] = opcode;
397 if (is_addressed) {
398 bool access_24bit = (access & NOR_ACCESS_24BIT_ADDR) != 0;
399 bool access_32bit = (access & NOR_ACCESS_32BIT_ADDR) != 0;
400 bool use_32bit = (access_32bit
401 || (!access_24bit
402 && driver_data->flag_access_32bit));
403 union {
404 uint32_t u32;
405 uint8_t u8[4];
406 } addr32 = {
407 .u32 = sys_cpu_to_be32(addr),
408 };
409
410 if (use_32bit) {
411 memcpy(&buf[1], &addr32.u8[0], 4);
412 spi_buf[0].len += 4;
413 } else {
414 memcpy(&buf[1], &addr32.u8[1], 3);
415 spi_buf[0].len += 3;
416 }
417 };
418
419 const struct spi_buf_set tx_set = {
420 .buffers = spi_buf,
421 .count = (length != 0) ? 2 : 1,
422 };
423
424 const struct spi_buf_set rx_set = {
425 .buffers = spi_buf,
426 .count = 2,
427 };
428
429 if (is_write) {
430 return spi_write_dt(&driver_cfg->spi, &tx_set);
431 }
432
433 return spi_transceive_dt(&driver_cfg->spi, &tx_set, &rx_set);
434 }
435
436 #define spi_nor_cmd_read(dev, opcode, dest, length) \
437 spi_nor_access(dev, opcode, 0, 0, dest, length)
438 #define spi_nor_cmd_addr_read(dev, opcode, addr, dest, length) \
439 spi_nor_access(dev, opcode, NOR_ACCESS_ADDRESSED, addr, dest, length)
440 #define spi_nor_cmd_addr_read_3b(dev, opcode, addr, dest, length) \
441 spi_nor_access(dev, opcode, NOR_ACCESS_24BIT_ADDR | NOR_ACCESS_ADDRESSED, addr, dest, \
442 length)
443 #define spi_nor_cmd_addr_read_4b(dev, opcode, addr, dest, length) \
444 spi_nor_access(dev, opcode, NOR_ACCESS_32BIT_ADDR | NOR_ACCESS_ADDRESSED, addr, dest, \
445 length)
446 #define spi_nor_cmd_write(dev, opcode) \
447 spi_nor_access(dev, opcode, NOR_ACCESS_WRITE, 0, NULL, 0)
448 #define spi_nor_cmd_addr_write(dev, opcode, addr, src, length) \
449 spi_nor_access(dev, opcode, NOR_ACCESS_WRITE | NOR_ACCESS_ADDRESSED, \
450 addr, (void *)src, length)
451 #define spi_nor_cmd_addr_write_3b(dev, opcode, addr, src, length) \
452 spi_nor_access(dev, opcode, \
453 NOR_ACCESS_24BIT_ADDR | NOR_ACCESS_WRITE | NOR_ACCESS_ADDRESSED, addr, \
454 (void *)src, length)
455 #define spi_nor_cmd_addr_write_4b(dev, opcode, addr, src, length) \
456 spi_nor_access(dev, opcode, \
457 NOR_ACCESS_32BIT_ADDR | NOR_ACCESS_WRITE | NOR_ACCESS_ADDRESSED, addr, \
458 (void *)src, length)
459
460 /**
461 * @brief Wait until the flash is ready
462 *
463 * @note The device must be externally acquired before invoking this
464 * function.
465 *
466 * This function should be invoked after every ERASE, PROGRAM, or
467 * WRITE_STATUS operation before continuing. This allows us to assume
468 * that the device is ready to accept new commands at any other point
469 * in the code.
470 *
471 * @param dev The device structure
472 * @param poll_delay Duration between polls of status register
473 * @return 0 on success, negative errno code otherwise
474 */
spi_nor_wait_until_ready(const struct device * dev,k_timeout_t poll_delay)475 static int spi_nor_wait_until_ready(const struct device *dev, k_timeout_t poll_delay)
476 {
477 int ret;
478 uint8_t reg;
479
480 ARG_UNUSED(poll_delay);
481
482 while (true) {
483 ret = spi_nor_cmd_read(dev, SPI_NOR_CMD_RDSR, ®, sizeof(reg));
484 /* Exit on error or no longer WIP */
485 if (ret || !(reg & SPI_NOR_WIP_BIT)) {
486 break;
487 }
488 #ifdef CONFIG_SPI_NOR_SLEEP_WHILE_WAITING_UNTIL_READY
489 /* Don't monopolise the CPU while waiting for ready */
490 k_sleep(poll_delay);
491 #endif /* CONFIG_SPI_NOR_SLEEP_WHILE_WAITING_UNTIL_READY */
492 }
493 return ret;
494 }
495
496 #if defined(CONFIG_SPI_NOR_SFDP_RUNTIME) || defined(CONFIG_FLASH_JESD216_API)
497 /*
498 * @brief Read content from the SFDP hierarchy
499 *
500 * @note The device must be externally acquired before invoking this
501 * function.
502 *
503 * @param dev Device struct
504 * @param addr The address to send
505 * @param data The buffer to store or read the value
506 * @param length The size of the buffer
507 * @return 0 on success, negative errno code otherwise
508 */
read_sfdp(const struct device * const dev,off_t addr,void * data,size_t length)509 static int read_sfdp(const struct device *const dev,
510 off_t addr, void *data, size_t length)
511 {
512 /* READ_SFDP requires a 24-bit address followed by a single
513 * byte for a wait state. This is effected by using 32-bit
514 * address by shifting the 24-bit address up 8 bits.
515 */
516 return spi_nor_access(dev, JESD216_CMD_READ_SFDP,
517 NOR_ACCESS_32BIT_ADDR | NOR_ACCESS_ADDRESSED,
518 addr << 8, data, length);
519 }
520 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
521
enter_dpd(const struct device * const dev)522 static int enter_dpd(const struct device *const dev)
523 {
524 int ret = 0;
525 const struct spi_nor_config *cfg = dev->config;
526
527 if (cfg->dpd_exist) {
528 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_DPD);
529 if (ret == 0) {
530 record_entered_dpd(dev);
531 }
532 }
533 return ret;
534 }
535
exit_dpd(const struct device * const dev)536 static int exit_dpd(const struct device *const dev)
537 {
538 int ret = 0;
539 #if ANY_INST_HAS_DPD
540 const struct spi_nor_config *cfg = dev->config;
541
542 if (cfg->dpd_exist) {
543 delay_until_exit_dpd_ok(dev);
544
545 if (cfg->dpd_wakeup_sequence_exist) {
546 #if ANY_INST_HAS_DPD_WAKEUP_SEQUENCE
547 /* Assert CSn and wait for tCRDP.
548 *
549 * Unfortunately the SPI API doesn't allow us to
550 * control CSn so fake it by writing a known-supported
551 * single-byte command, hoping that'll hold the assert
552 * long enough. This is highly likely, since the
553 * duration is usually less than two SPI clock cycles.
554 */
555 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_RDID);
556
557 /* Deassert CSn and wait for tRDP */
558 k_sleep(K_MSEC(cfg->t_rdp_ms));
559 #endif /* ANY_INST_HAS_DPD_WAKEUP_SEQUENCE */
560 } else {
561 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_RDPD);
562
563 #if ANY_INST_HAS_T_EXIT_DPD
564 if (ret == 0) {
565 if (cfg->dpd_exist) {
566 k_sleep(K_MSEC(cfg->t_exit_dpd));
567 }
568 }
569 #endif /* T_EXIT_DPD */
570 }
571 }
572 #endif /* ANY_INST_HAS_DPD */
573 return ret;
574 }
575
576 /* Everything necessary to acquire owning access to the device. */
acquire_device(const struct device * dev)577 static void acquire_device(const struct device *dev)
578 {
579 const struct spi_nor_config *cfg = dev->config;
580
581 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
582 struct spi_nor_data *const driver_data = dev->data;
583
584 k_sem_take(&driver_data->sem, K_FOREVER);
585 }
586
587 (void)pm_device_runtime_get(cfg->spi.bus);
588 }
589
590 /* Everything necessary to release access to the device. */
release_device(const struct device * dev)591 static void release_device(const struct device *dev)
592 {
593 const struct spi_nor_config *cfg = dev->config;
594
595 (void)pm_device_runtime_put(cfg->spi.bus);
596
597 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
598 struct spi_nor_data *const driver_data = dev->data;
599
600 k_sem_give(&driver_data->sem);
601 }
602 }
603
604 /**
605 * @brief Read the status register.
606 *
607 * @note The device must be externally acquired before invoking this
608 * function.
609 *
610 * @param dev Device struct
611 *
612 * @return the non-negative value of the status register, or an error code.
613 */
spi_nor_rdsr(const struct device * dev)614 static int spi_nor_rdsr(const struct device *dev)
615 {
616 uint8_t reg;
617 int ret = spi_nor_cmd_read(dev, SPI_NOR_CMD_RDSR, ®, sizeof(reg));
618
619 if (ret == 0) {
620 ret = reg;
621 }
622
623 return ret;
624 }
625
626 /**
627 * @brief Write the status register.
628 *
629 * @note The device must be externally acquired before invoking this
630 * function.
631 *
632 * @param dev Device struct
633 * @param sr The new value of the status register
634 *
635 * @return 0 on success or a negative error code.
636 */
spi_nor_wrsr(const struct device * dev,uint8_t sr)637 static int spi_nor_wrsr(const struct device *dev,
638 uint8_t sr)
639 {
640 int ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
641
642 if (ret != 0) {
643 return ret;
644 }
645 ret = spi_nor_access(dev, SPI_NOR_CMD_WRSR, NOR_ACCESS_WRITE, 0, &sr,
646 sizeof(sr));
647 if (ret != 0) {
648 return ret;
649 }
650 return spi_nor_wait_until_ready(dev, WAIT_READY_REGISTER);
651 }
652
653 #if ANY_INST_HAS_MXICY_MX25R_POWER_MODE
654
655 /**
656 * @brief Read the configuration register.
657 *
658 * @note The device must be externally acquired before invoking this
659 * function.
660 *
661 * @param dev Device struct
662 *
663 * @return the non-negative value of the configuration register, or an error code.
664 */
mxicy_rdcr(const struct device * dev)665 static int mxicy_rdcr(const struct device *dev)
666 {
667 const struct spi_nor_config *cfg = dev->config;
668 uint16_t cr = -ENOSYS;
669
670 if (cfg->mxicy_mx25r_power_mode_exist) {
671 int ret = spi_nor_cmd_read(dev, CMD_RDCR, &cr, sizeof(cr));
672
673 if (ret < 0) {
674 return ret;
675 }
676 }
677
678 return cr;
679 }
680
681 /**
682 * @brief Write the configuration register.
683 *
684 * @note The device must be externally acquired before invoking this
685 * function.
686 *
687 * @param dev Device struct
688 * @param cr The new value of the configuration register
689 *
690 * @return 0 on success or a negative error code.
691 */
mxicy_wrcr(const struct device * dev,uint16_t cr)692 static int mxicy_wrcr(const struct device *dev,
693 uint16_t cr)
694 {
695 const struct spi_nor_config *cfg = dev->config;
696 int ret = -ENOSYS;
697 /* The configuration register bytes on the Macronix MX25R devices are
698 * written using the Write Status Register command where the configuration
699 * register bytes are written as two extra bytes after the status register.
700 * First read out the current status register to preserve the value.
701 */
702
703 if (cfg->mxicy_mx25r_power_mode_exist) {
704 int sr = spi_nor_rdsr(dev);
705
706 if (sr < 0) {
707 LOG_ERR("Read status register failed: %d", sr);
708 return sr;
709 }
710
711 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
712 if (ret != 0) {
713 return ret;
714 }
715
716 uint8_t data[] = {
717 sr,
718 cr & 0xFF, /* Configuration register 1 */
719 cr >> 8 /* Configuration register 2 */
720 };
721
722 ret = spi_nor_access(dev, SPI_NOR_CMD_WRSR, NOR_ACCESS_WRITE, 0,
723 data, sizeof(data));
724 if (ret != 0) {
725 return ret;
726 }
727
728 ret = spi_nor_wait_until_ready(dev, WAIT_READY_REGISTER);
729 }
730
731 return ret;
732 }
733
mxicy_configure(const struct device * dev,const uint8_t * jedec_id)734 static int mxicy_configure(const struct device *dev, const uint8_t *jedec_id)
735 {
736 const struct spi_nor_config *cfg = dev->config;
737 int ret = -ENOSYS;
738
739 if (cfg->mxicy_mx25r_power_mode_exist) {
740 /* Low-power/high perf mode is second bit in configuration register 2 */
741 int current_cr, new_cr;
742 /* lh_switch enum index:
743 * 0: Ultra low power
744 * 1: High performance mode
745 */
746 const bool use_high_perf = cfg->mxicy_mx25r_power_mode;
747
748 /* Only supported on Macronix MX25R Ultra Low Power series. */
749 if (jedec_id[0] != JEDEC_MACRONIX_ID || jedec_id[1] != JEDEC_MX25R_TYPE_ID) {
750 LOG_WRN("L/H switch not supported for device id: %02x %02x %02x",
751 jedec_id[0], jedec_id[1], jedec_id[2]);
752 /* Do not return an error here because the flash still functions */
753 return 0;
754 }
755
756 acquire_device(dev);
757
758 /* Read current configuration register */
759
760 ret = mxicy_rdcr(dev);
761 if (ret < 0) {
762 release_device(dev);
763 return ret;
764 }
765 current_cr = ret;
766
767 LOG_DBG("Use high performance mode? %d", use_high_perf);
768 new_cr = current_cr;
769 WRITE_BIT(new_cr, LH_SWITCH_BIT, use_high_perf);
770 if (new_cr != current_cr) {
771 ret = mxicy_wrcr(dev, new_cr);
772 } else {
773 ret = 0;
774 }
775
776 if (ret < 0) {
777 LOG_ERR("Enable high performace mode failed: %d", ret);
778 }
779
780 release_device(dev);
781 }
782
783 return ret;
784 }
785
786 #endif /* ANY_INST_HAS_MXICY_MX25R_POWER_MODE */
787
spi_nor_read(const struct device * dev,off_t addr,void * dest,size_t size)788 static int spi_nor_read(const struct device *dev, off_t addr, void *dest,
789 size_t size)
790 {
791 const size_t flash_size = dev_flash_size(dev);
792 int ret;
793
794 /* should be between 0 and flash size */
795 if ((addr < 0) || ((addr + size) > flash_size)) {
796 return -EINVAL;
797 }
798
799 /* Ensure flash is powered before read */
800 if (pm_device_runtime_get(dev) < 0) {
801 return -EIO;
802 }
803
804 acquire_device(dev);
805
806 if (IS_ENABLED(ANY_INST_USE_4B_ADDR_OPCODES) && DEV_CFG(dev)->use_4b_addr_opcodes) {
807 if (addr > SPI_NOR_3B_ADDR_MAX) {
808 ret = spi_nor_cmd_addr_read_4b(dev, SPI_NOR_CMD_READ_4B, addr, dest, size);
809 } else {
810 ret = spi_nor_cmd_addr_read_3b(dev, SPI_NOR_CMD_READ, addr, dest, size);
811 }
812 } else {
813 ret = spi_nor_cmd_addr_read(dev, SPI_NOR_CMD_READ, addr, dest, size);
814 }
815
816 release_device(dev);
817
818 /* Release flash power requirement */
819 (void)pm_device_runtime_put_async(dev, K_MSEC(ACTIVE_DWELL_MS));
820 return ret;
821 }
822
823 #if defined(CONFIG_FLASH_EX_OP_ENABLED)
flash_spi_nor_ex_op(const struct device * dev,uint16_t code,const uintptr_t in,void * out)824 static int flash_spi_nor_ex_op(const struct device *dev, uint16_t code,
825 const uintptr_t in, void *out)
826 {
827 int ret;
828
829 ARG_UNUSED(in);
830 ARG_UNUSED(out);
831
832 if (pm_device_runtime_get(dev) < 0) {
833 return -EIO;
834 }
835
836 acquire_device(dev);
837
838 switch (code) {
839 case FLASH_EX_OP_RESET:
840 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_RESET_EN);
841 if (ret == 0) {
842 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_RESET_MEM);
843 }
844 break;
845 default:
846 ret = -ENOTSUP;
847 break;
848 }
849
850 release_device(dev);
851 (void)pm_device_runtime_put_async(dev, K_MSEC(ACTIVE_DWELL_MS));
852 return ret;
853 }
854 #endif
855
spi_nor_write(const struct device * dev,off_t addr,const void * src,size_t size)856 static int spi_nor_write(const struct device *dev, off_t addr,
857 const void *src,
858 size_t size)
859 {
860 const size_t flash_size = dev_flash_size(dev);
861 const uint16_t page_size = dev_page_size(dev);
862 int ret;
863
864 /* should be between 0 and flash size */
865 if ((addr < 0) || ((size + addr) > flash_size)) {
866 return -EINVAL;
867 }
868
869 /* Ensure flash is powered before write */
870 if (pm_device_runtime_get(dev) < 0) {
871 return -EIO;
872 }
873
874 acquire_device(dev);
875 ret = spi_nor_write_protection_set(dev, false);
876 if (ret == 0) {
877 while (size > 0) {
878 size_t to_write = size;
879
880 /* Don't write more than a page. */
881 if (to_write >= page_size) {
882 to_write = page_size;
883 }
884
885 /* Don't write across a page boundary */
886 if (((addr + to_write - 1U) / page_size)
887 != (addr / page_size)) {
888 to_write = page_size - (addr % page_size);
889 }
890
891 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
892
893 if (ret != 0) {
894 break;
895 }
896
897 if (IS_ENABLED(ANY_INST_USE_4B_ADDR_OPCODES) &&
898 DEV_CFG(dev)->use_4b_addr_opcodes) {
899 if (addr > SPI_NOR_3B_ADDR_MAX) {
900 ret = spi_nor_cmd_addr_write_4b(dev, SPI_NOR_CMD_PP_4B,
901 addr, src, to_write);
902 } else {
903 ret = spi_nor_cmd_addr_write_3b(dev, SPI_NOR_CMD_PP, addr,
904 src, to_write);
905 }
906 } else {
907 ret = spi_nor_cmd_addr_write(dev, SPI_NOR_CMD_PP, addr, src,
908 to_write);
909 }
910
911 if (ret != 0) {
912 break;
913 }
914
915 size -= to_write;
916 src = (const uint8_t *)src + to_write;
917 addr += to_write;
918
919 ret = spi_nor_wait_until_ready(dev, WAIT_READY_WRITE);
920 if (ret != 0) {
921 break;
922 }
923 }
924 }
925
926 int ret2 = spi_nor_write_protection_set(dev, true);
927
928 if (!ret) {
929 ret = ret2;
930 }
931
932 release_device(dev);
933
934 /* Release flash power requirement */
935 (void)pm_device_runtime_put_async(dev, K_MSEC(ACTIVE_DWELL_MS));
936 return ret;
937 }
938
spi_nor_erase(const struct device * dev,off_t addr,size_t size)939 static int spi_nor_erase(const struct device *dev, off_t addr, size_t size)
940 {
941 const size_t flash_size = dev_flash_size(dev);
942 int ret;
943
944 /* erase area must be subregion of device */
945 if ((addr < 0) || ((size + addr) > flash_size)) {
946 return -EINVAL;
947 }
948
949 /* address must be sector-aligned */
950 if (!SPI_NOR_IS_SECTOR_ALIGNED(addr)) {
951 return -EINVAL;
952 }
953
954 /* size must be a multiple of sectors */
955 if ((size % SPI_NOR_SECTOR_SIZE) != 0) {
956 return -EINVAL;
957 }
958
959 /* Ensure flash is powered before erase */
960 if (pm_device_runtime_get(dev) < 0) {
961 return -EIO;
962 }
963
964 acquire_device(dev);
965 ret = spi_nor_write_protection_set(dev, false);
966
967 while ((size > 0) && (ret == 0)) {
968 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
969 if (ret) {
970 break;
971 }
972
973 if (size == flash_size) {
974 /* chip erase */
975 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_CE);
976 size -= flash_size;
977 } else {
978 const struct jesd216_erase_type *erase_types =
979 dev_erase_types(dev);
980 const struct jesd216_erase_type *bet = NULL;
981
982 for (uint8_t ei = 0; ei < JESD216_NUM_ERASE_TYPES; ++ei) {
983 const struct jesd216_erase_type *etp =
984 &erase_types[ei];
985
986 if ((etp->exp != 0)
987 && SPI_NOR_IS_ALIGNED(addr, etp->exp)
988 && (size >= BIT(etp->exp))
989 && ((bet == NULL)
990 || (etp->exp > bet->exp))) {
991 bet = etp;
992 }
993 }
994 if (bet != NULL) {
995 if (IS_ENABLED(ANY_INST_USE_4B_ADDR_OPCODES) &&
996 DEV_CFG(dev)->use_4b_addr_opcodes) {
997 ret = spi_nor_cmd_addr_write_4b(dev, bet->cmd, addr, NULL,
998 0);
999 } else {
1000 ret = spi_nor_cmd_addr_write(dev, bet->cmd, addr, NULL, 0);
1001 }
1002 addr += BIT(bet->exp);
1003 size -= BIT(bet->exp);
1004 } else {
1005 LOG_DBG("Can't erase %zu at 0x%lx",
1006 size, (long)addr);
1007 ret = -EINVAL;
1008 }
1009 }
1010 if (ret != 0) {
1011 break;
1012 }
1013
1014 ret = spi_nor_wait_until_ready(dev, WAIT_READY_ERASE);
1015 }
1016
1017 int ret2 = spi_nor_write_protection_set(dev, true);
1018
1019 if (!ret) {
1020 ret = ret2;
1021 }
1022
1023 release_device(dev);
1024
1025 /* Release flash power requirement */
1026 (void)pm_device_runtime_put_async(dev, K_MSEC(ACTIVE_DWELL_MS));
1027 return ret;
1028 }
1029
1030 /* @note The device must be externally acquired before invoking this
1031 * function.
1032 */
spi_nor_write_protection_set(const struct device * dev,bool write_protect)1033 static int spi_nor_write_protection_set(const struct device *dev,
1034 bool write_protect)
1035 {
1036 int ret;
1037 const struct spi_nor_config *cfg = dev->config;
1038
1039 #if ANY_INST_HAS_WP_GPIOS
1040 if (DEV_CFG(dev)->wp_gpios_exist && write_protect == false) {
1041 gpio_pin_set_dt(&(DEV_CFG(dev)->wp), 0);
1042 }
1043 #endif
1044
1045 ret = spi_nor_cmd_write(dev, (write_protect) ?
1046 SPI_NOR_CMD_WRDI : SPI_NOR_CMD_WREN);
1047
1048 if (cfg->requires_ulbpr_exist
1049 && (ret == 0)
1050 && !write_protect) {
1051 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_ULBPR);
1052 }
1053
1054 #if ANY_INST_HAS_WP_GPIOS
1055 if (DEV_CFG(dev)->wp_gpios_exist && write_protect == true) {
1056 gpio_pin_set_dt(&(DEV_CFG(dev)->wp), 1);
1057 }
1058 #endif
1059
1060 return ret;
1061 }
1062
1063 #if defined(CONFIG_FLASH_JESD216_API) || defined(CONFIG_SPI_NOR_SFDP_RUNTIME)
1064
spi_nor_sfdp_read(const struct device * dev,off_t addr,void * dest,size_t size)1065 static int spi_nor_sfdp_read(const struct device *dev, off_t addr,
1066 void *dest, size_t size)
1067 {
1068 if (pm_device_runtime_get(dev) < 0) {
1069 return -EIO;
1070 }
1071
1072 acquire_device(dev);
1073
1074 int ret = read_sfdp(dev, addr, dest, size);
1075
1076 release_device(dev);
1077
1078 (void)pm_device_runtime_put_async(dev, K_MSEC(ACTIVE_DWELL_MS));
1079
1080 return ret;
1081 }
1082
1083 #endif /* CONFIG_FLASH_JESD216_API || CONFIG_SPI_NOR_SFDP_RUNTIME */
1084
spi_nor_read_jedec_id(const struct device * dev,uint8_t * id)1085 static int spi_nor_read_jedec_id(const struct device *dev,
1086 uint8_t *id)
1087 {
1088 if (id == NULL) {
1089 return -EINVAL;
1090 }
1091
1092 if (pm_device_runtime_get(dev) < 0) {
1093 return -EIO;
1094 }
1095
1096 acquire_device(dev);
1097
1098 int ret = spi_nor_cmd_read(dev, SPI_NOR_CMD_RDID, id, SPI_NOR_MAX_ID_LEN);
1099
1100 release_device(dev);
1101
1102 (void)pm_device_runtime_put_async(dev, K_MSEC(ACTIVE_DWELL_MS));
1103
1104 return ret;
1105 }
1106
1107 /* Put the device into the appropriate address mode, if supported.
1108 *
1109 * On successful return spi_nor_data::flag_access_32bit has been set
1110 * (cleared) if the device is configured for 4-byte (3-byte) addresses
1111 * for read, write, and erase commands.
1112 *
1113 * @param dev the device
1114 *
1115 * @param enter_4byte_addr the Enter 4-Byte Addressing bit set from
1116 * DW16 of SFDP BFP. A value of all zeros or all ones is interpreted
1117 * as "not supported".
1118 *
1119 * @retval -ENOTSUP if 4-byte addressing is supported but not in a way
1120 * that the driver can handle.
1121 * @retval negative codes if the attempt was made and failed
1122 * @retval 0 if the device is successfully left in 24-bit mode or
1123 * reconfigured to 32-bit mode.
1124 */
spi_nor_set_address_mode(const struct device * dev,uint8_t enter_4byte_addr)1125 static int spi_nor_set_address_mode(const struct device *dev,
1126 uint8_t enter_4byte_addr)
1127 {
1128 int ret = 0;
1129
1130 LOG_DBG("Checking enter-4byte-addr %02x", enter_4byte_addr);
1131
1132 /* Do nothing if not provided (either no bits or all bits
1133 * set).
1134 */
1135 if ((enter_4byte_addr == 0)
1136 || (enter_4byte_addr == 0xff)) {
1137 return 0;
1138 }
1139
1140 /* This currently only supports command 0xB7 (Enter 4-Byte
1141 * Address Mode), with or without preceding WREN.
1142 * Or when BIT(3) is set where the 4-byte address mode can be entered
1143 * by setting BIT(7) in a register via a 0x17 write
1144 * instruction. See JEDEC 216F 16th DWORD.
1145 */
1146 if ((enter_4byte_addr & 0x0b) == 0) {
1147 return -ENOTSUP;
1148 }
1149
1150 acquire_device(dev);
1151
1152 if ((enter_4byte_addr & 0x08) != 0) {
1153 /* Enter 4-byte address mode by setting BIT(7) in a register
1154 * via a 0x17 write instruction.
1155 */
1156 uint8_t sr = BIT(7);
1157
1158 ret = spi_nor_access(dev, 0x17, NOR_ACCESS_WRITE, 0, &sr, sizeof(sr));
1159 goto done;
1160 }
1161
1162 if ((enter_4byte_addr & 0x02) != 0) {
1163 /* Enter after WREN. */
1164 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
1165 }
1166
1167 if (ret == 0) {
1168 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_4BA);
1169 }
1170
1171 done:
1172 if (ret == 0) {
1173 struct spi_nor_data *data = dev->data;
1174
1175 data->flag_access_32bit = true;
1176 }
1177
1178 release_device(dev);
1179
1180 return ret;
1181 }
1182
1183 #ifndef CONFIG_SPI_NOR_SFDP_MINIMAL
1184
spi_nor_process_bfp(const struct device * dev,const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)1185 static int spi_nor_process_bfp(const struct device *dev,
1186 const struct jesd216_param_header *php,
1187 const struct jesd216_bfp *bfp)
1188 {
1189 struct spi_nor_data *data = dev->data;
1190 struct jesd216_erase_type *etp = data->erase_types;
1191 const size_t flash_size = jesd216_bfp_density(bfp) / 8U;
1192
1193 LOG_INF("%s: %u %ciBy flash", dev->name,
1194 (flash_size < (1024U * 1024U)) ? (uint32_t)(flash_size >> 10)
1195 : (uint32_t)(flash_size >> 20),
1196 (flash_size < (1024U * 1024U)) ? 'k' : 'M');
1197
1198 /* Copy over the erase types, preserving their order. (The
1199 * Sector Map Parameter table references them by index.)
1200 */
1201 memset(data->erase_types, 0, sizeof(data->erase_types));
1202 for (uint8_t ti = 1; ti <= ARRAY_SIZE(data->erase_types); ++ti) {
1203 if (jesd216_bfp_erase(bfp, ti, etp) == 0) {
1204 LOG_DBG("Erase %u with %02x", (uint32_t)BIT(etp->exp), etp->cmd);
1205 }
1206 ++etp;
1207 }
1208
1209 data->page_size = jesd216_bfp_page_size(php, bfp);
1210 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
1211 data->flash_size = flash_size;
1212 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1213 if (flash_size != dev_flash_size(dev)) {
1214 LOG_ERR("BFP flash size mismatch with devicetree");
1215 return -EINVAL;
1216 }
1217 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1218
1219 LOG_DBG("Page size %u bytes", data->page_size);
1220
1221 /* If 4-byte addressing is supported, switch to it. */
1222 if (jesd216_bfp_addrbytes(bfp) != JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_3B) {
1223 struct jesd216_bfp_dw16 dw16;
1224 int rc = 0;
1225
1226 if (IS_ENABLED(ANY_INST_USE_4B_ADDR_OPCODES) && DEV_CFG(dev)->use_4b_addr_opcodes) {
1227 LOG_DBG("4-byte addressing supported, using it via specific opcodes");
1228 return 0;
1229 }
1230
1231 if (jesd216_bfp_decode_dw16(php, bfp, &dw16) == 0) {
1232 rc = spi_nor_set_address_mode(dev, dw16.enter_4ba);
1233 }
1234
1235 if (rc != 0) {
1236 LOG_ERR("Unable to enter 4-byte mode: %d\n", rc);
1237 return rc;
1238 }
1239 }
1240 return 0;
1241 }
1242
spi_nor_process_sfdp(const struct device * dev)1243 static int spi_nor_process_sfdp(const struct device *dev)
1244 {
1245 int rc;
1246
1247 #if defined(CONFIG_SPI_NOR_SFDP_RUNTIME)
1248 struct spi_nor_data *dev_data = dev->data;
1249 /* For runtime we need to read the SFDP table, identify the
1250 * BFP block, and process it.
1251 */
1252 const uint8_t decl_nph = 2;
1253 union {
1254 /* We only process BFP so use one parameter block */
1255 uint8_t raw[JESD216_SFDP_SIZE(decl_nph)];
1256 struct jesd216_sfdp_header sfdp;
1257 } u_header;
1258 const struct jesd216_sfdp_header *hp = &u_header.sfdp;
1259
1260 rc = spi_nor_sfdp_read(dev, 0, u_header.raw, sizeof(u_header.raw));
1261 if (rc != 0) {
1262 LOG_ERR("SFDP read failed: %d", rc);
1263 return rc;
1264 }
1265
1266 uint32_t magic = jesd216_sfdp_magic(hp);
1267
1268 if (magic != JESD216_SFDP_MAGIC) {
1269 LOG_ERR("SFDP magic %08x invalid", magic);
1270 return -EINVAL;
1271 }
1272
1273 LOG_INF("%s: SFDP v %u.%u AP %x with %u PH", dev->name,
1274 hp->rev_major, hp->rev_minor, hp->access, 1 + hp->nph);
1275
1276 const struct jesd216_param_header *php = hp->phdr;
1277 const struct jesd216_param_header *phpe = php + MIN(decl_nph, 1 + hp->nph);
1278
1279 while (php != phpe) {
1280 uint16_t id = jesd216_param_id(php);
1281
1282 LOG_INF("PH%u: %04x rev %u.%u: %u DW @ %x",
1283 (int)(php - hp->phdr), id, php->rev_major, php->rev_minor,
1284 php->len_dw, jesd216_param_addr(php));
1285
1286 if (id == JESD216_SFDP_PARAM_ID_BFP) {
1287 union {
1288 uint32_t dw[MIN(php->len_dw, 20)];
1289 struct jesd216_bfp bfp;
1290 } u_param;
1291 const struct jesd216_bfp *bfp = &u_param.bfp;
1292
1293 rc = spi_nor_sfdp_read(dev, jesd216_param_addr(php),
1294 u_param.dw, sizeof(u_param.dw));
1295 if (rc == 0) {
1296 rc = spi_nor_process_bfp(dev, php, bfp);
1297 }
1298
1299 if (rc != 0) {
1300 LOG_INF("SFDP BFP failed: %d", rc);
1301 break;
1302 }
1303 }
1304 if (id == JESD216_SFDP_PARAM_ID_4B_ADDR_INSTR) {
1305 if (IS_ENABLED(ANY_INST_USE_4B_ADDR_OPCODES) &&
1306 DEV_CFG(dev)->use_4b_addr_opcodes) {
1307 /*
1308 * Check table 4 byte address instruction table to get supported
1309 * erase opcodes when running in 4 byte address mode
1310 */
1311 union {
1312 uint32_t dw[2];
1313 struct {
1314 uint32_t dummy;
1315 uint8_t type[4];
1316 } types;
1317 } u2;
1318 rc = spi_nor_sfdp_read(
1319 dev, jesd216_param_addr(php), (uint8_t *)u2.dw,
1320 MIN(sizeof(uint32_t) * php->len_dw, sizeof(u2.dw)));
1321 if (rc != 0) {
1322 break;
1323 }
1324 for (uint8_t ei = 0; ei < JESD216_NUM_ERASE_TYPES; ++ei) {
1325 struct jesd216_erase_type *etp = &dev_data->erase_types[ei];
1326 const uint8_t cmd = u2.types.type[ei];
1327 /* 0xff means not supported */
1328 if (cmd == 0xff) {
1329 etp->exp = 0;
1330 etp->cmd = 0;
1331 } else {
1332 etp->cmd = cmd;
1333 };
1334 }
1335
1336 if (!((sys_le32_to_cpu(u2.dw[0]) &
1337 JESD216_SFDP_4B_ADDR_DW1_1S_1S_1S_READ_13_SUP) &&
1338 (sys_le32_to_cpu(u2.dw[0]) &
1339 JESD216_SFDP_4B_ADDR_DW1_1S_1S_1S_PP_12_SUP))) {
1340 LOG_ERR("4-byte addressing not supported");
1341 return -ENOTSUP;
1342 }
1343 }
1344 }
1345 ++php;
1346 }
1347 #elif defined(CONFIG_SPI_NOR_SFDP_DEVICETREE)
1348 /* For devicetree we need to synthesize a parameter header and
1349 * process the stored BFP data as if we had read it.
1350 */
1351 const struct spi_nor_config *cfg = dev->config;
1352 struct jesd216_param_header bfp_hdr = {
1353 .len_dw = cfg->bfp_len,
1354 };
1355
1356 rc = spi_nor_process_bfp(dev, &bfp_hdr, cfg->bfp);
1357 #else
1358 #error Unhandled SFDP choice
1359 #endif
1360
1361 return rc;
1362 }
1363
1364 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
setup_pages_layout(const struct device * dev)1365 static int setup_pages_layout(const struct device *dev)
1366 {
1367 #if defined(CONFIG_SPI_NOR_SFDP_RUNTIME)
1368 struct spi_nor_data *data = dev->data;
1369 const size_t flash_size = dev_flash_size(dev);
1370 const uint32_t layout_page_size = CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE;
1371 uint8_t exp = 0;
1372
1373 /* Find the smallest erase size. */
1374 for (size_t i = 0; i < ARRAY_SIZE(data->erase_types); ++i) {
1375 const struct jesd216_erase_type *etp = &data->erase_types[i];
1376
1377 if ((etp->cmd != 0)
1378 && ((exp == 0) || (etp->exp < exp))) {
1379 exp = etp->exp;
1380 }
1381 }
1382
1383 if (exp == 0) {
1384 return -ENOTSUP;
1385 }
1386
1387 uint32_t erase_size = BIT(exp);
1388
1389 /* Error if layout page size is not a multiple of smallest
1390 * erase size.
1391 */
1392 if ((layout_page_size % erase_size) != 0) {
1393 LOG_ERR("layout page %u not compatible with erase size %u",
1394 layout_page_size, erase_size);
1395 return -EINVAL;
1396 }
1397
1398 /* Warn but accept layout page sizes that leave inaccessible
1399 * space.
1400 */
1401 if ((flash_size % layout_page_size) != 0) {
1402 LOG_INF("layout page %u wastes space with device size %zu",
1403 layout_page_size, flash_size);
1404 }
1405
1406 data->layout.pages_size = layout_page_size;
1407 data->layout.pages_count = flash_size / layout_page_size;
1408 LOG_DBG("layout %zu x %zu By pages", data->layout.pages_count, data->layout.pages_size);
1409 #elif defined(CONFIG_SPI_NOR_SFDP_DEVICETREE)
1410 const struct spi_nor_config *cfg = dev->config;
1411 const struct flash_pages_layout *layout = &cfg->layout;
1412 const size_t flash_size = dev_flash_size(dev);
1413 size_t layout_size = layout->pages_size * layout->pages_count;
1414
1415 if (flash_size != layout_size) {
1416 LOG_ERR("device size %u mismatch %zu * %zu By pages",
1417 flash_size, layout->pages_count, layout->pages_size);
1418 return -EINVAL;
1419 }
1420 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1421 #error Unhandled SFDP choice
1422 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1423
1424 return 0;
1425 }
1426 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1427 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
1428
1429 /**
1430 * @brief Configure the flash
1431 *
1432 * @param dev The flash device structure
1433 * @param info The flash info structure
1434 * @return 0 on success, negative errno code otherwise
1435 */
spi_nor_configure(const struct device * dev)1436 static int spi_nor_configure(const struct device *dev)
1437 {
1438 const struct spi_nor_config *cfg = dev->config;
1439 uint8_t jedec_id[SPI_NOR_MAX_ID_LEN];
1440 int rc;
1441
1442 /* Validate bus and CS is ready */
1443 if (!spi_is_ready_dt(&cfg->spi)) {
1444 return -ENODEV;
1445 }
1446
1447 #if ANY_INST_HAS_RESET_GPIOS
1448
1449 if (cfg->reset_gpios_exist) {
1450 if (!gpio_is_ready_dt(&cfg->reset)) {
1451 LOG_ERR("Reset pin not ready");
1452 return -ENODEV;
1453 }
1454 if (gpio_pin_configure_dt(&cfg->reset, GPIO_OUTPUT_ACTIVE)) {
1455 LOG_ERR("Couldn't configure reset pin");
1456 return -ENODEV;
1457 }
1458 rc = gpio_pin_set_dt(&cfg->reset, 0);
1459 if (rc) {
1460 return rc;
1461 }
1462 }
1463 #endif
1464
1465 /* After a soft-reset the flash might be in DPD or busy writing/erasing.
1466 * Exit DPD and wait until flash is ready.
1467 */
1468 acquire_device(dev);
1469
1470 rc = exit_dpd(dev);
1471 if (rc < 0) {
1472 LOG_ERR("Failed to exit DPD (%d)", rc);
1473 release_device(dev);
1474 return -ENODEV;
1475 }
1476
1477 rc = spi_nor_rdsr(dev);
1478 if (rc > 0 && (rc & SPI_NOR_WIP_BIT)) {
1479 LOG_WRN("Waiting until flash is ready");
1480 rc = spi_nor_wait_until_ready(dev, WAIT_READY_REGISTER);
1481 }
1482 release_device(dev);
1483 if (rc < 0) {
1484 LOG_ERR("Failed to wait until flash is ready (%d)", rc);
1485 return -ENODEV;
1486 }
1487
1488 /* now the spi bus is configured, we can verify SPI
1489 * connectivity by reading the JEDEC ID.
1490 */
1491
1492 rc = spi_nor_read_jedec_id(dev, jedec_id);
1493 if (rc != 0) {
1494 LOG_ERR("JEDEC ID read failed: %d", rc);
1495 return -ENODEV;
1496 }
1497
1498 #ifndef CONFIG_SPI_NOR_SFDP_RUNTIME
1499 /* For minimal and devicetree we need to check the JEDEC ID
1500 * against the one from devicetree, to ensure we didn't find a
1501 * device that has different parameters.
1502 */
1503
1504 if (memcmp(jedec_id, cfg->jedec_id, sizeof(jedec_id)) != 0) {
1505 LOG_ERR("Device id %02x %02x %02x does not match config %02x %02x %02x",
1506 jedec_id[0], jedec_id[1], jedec_id[2],
1507 cfg->jedec_id[0], cfg->jedec_id[1], cfg->jedec_id[2]);
1508 return -EINVAL;
1509 }
1510 #endif
1511
1512 /* Check for block protect bits that need to be cleared. This
1513 * information cannot be determined from SFDP content, so the
1514 * devicetree node property must be set correctly for any device
1515 * that powers up with block protect enabled.
1516 */
1517 if (cfg->has_lock != 0) {
1518 acquire_device(dev);
1519
1520 rc = spi_nor_rdsr(dev);
1521
1522 /* Only clear if RDSR worked and something's set. */
1523 if (rc > 0) {
1524 rc = spi_nor_wrsr(dev, rc & ~cfg->has_lock);
1525 }
1526
1527 release_device(dev);
1528
1529 if (rc != 0) {
1530 LOG_ERR("BP clear failed: %d\n", rc);
1531 return -ENODEV;
1532 }
1533 }
1534
1535 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
1536 /* For minimal we support some overrides from specific
1537 * devicertee properties.
1538 */
1539 if (cfg->enter_4byte_addr != 0) {
1540 rc = spi_nor_set_address_mode(dev, cfg->enter_4byte_addr);
1541
1542 if (rc != 0) {
1543 LOG_ERR("Unable to enter 4-byte mode: %d\n", rc);
1544 return -ENODEV;
1545 }
1546 }
1547
1548 #else /* CONFIG_SPI_NOR_SFDP_MINIMAL */
1549 /* For devicetree and runtime we need to process BFP data and
1550 * set up or validate page layout.
1551 */
1552 rc = spi_nor_process_sfdp(dev);
1553 if (rc != 0) {
1554 LOG_ERR("SFDP read failed: %d", rc);
1555 return -ENODEV;
1556 }
1557
1558 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1559 rc = setup_pages_layout(dev);
1560 if (rc != 0) {
1561 LOG_ERR("layout setup failed: %d", rc);
1562 return -ENODEV;
1563 }
1564 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1565 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
1566
1567 #if ANY_INST_HAS_MXICY_MX25R_POWER_MODE
1568 if (cfg->mxicy_mx25r_power_mode_exist) {
1569 /* Do not fail init if setting configuration register fails */
1570 (void)mxicy_configure(dev, jedec_id);
1571 }
1572 #endif /* ANY_INST_HAS_MXICY_MX25R_POWER_MODE */
1573
1574 return 0;
1575 }
1576
spi_nor_pm_control(const struct device * dev,enum pm_device_action action)1577 static int spi_nor_pm_control(const struct device *dev, enum pm_device_action action)
1578 {
1579 int rc = 0;
1580
1581 switch (action) {
1582 case PM_DEVICE_ACTION_SUSPEND:
1583 acquire_device(dev);
1584 rc = enter_dpd(dev);
1585 release_device(dev);
1586 break;
1587 case PM_DEVICE_ACTION_RESUME:
1588 acquire_device(dev);
1589 rc = exit_dpd(dev);
1590 release_device(dev);
1591 break;
1592 case PM_DEVICE_ACTION_TURN_ON:
1593 /* Coming out of power off */
1594 rc = spi_nor_configure(dev);
1595 if (rc == 0) {
1596 /* Move to DPD, the correct device state
1597 * for PM_DEVICE_STATE_SUSPENDED
1598 */
1599 acquire_device(dev);
1600 rc = enter_dpd(dev);
1601 release_device(dev);
1602 }
1603 break;
1604 case PM_DEVICE_ACTION_TURN_OFF:
1605 break;
1606 default:
1607 rc = -ENOSYS;
1608 }
1609
1610 return rc;
1611 }
1612
1613 /**
1614 * @brief Initialize and configure the flash
1615 *
1616 * @param name The flash name
1617 * @return 0 on success, negative errno code otherwise
1618 */
spi_nor_init(const struct device * dev)1619 static int spi_nor_init(const struct device *dev)
1620 {
1621 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
1622 struct spi_nor_data *const driver_data = dev->data;
1623
1624 k_sem_init(&driver_data->sem, 1, K_SEM_MAX_LIMIT);
1625 }
1626
1627 #if ANY_INST_HAS_WP_GPIOS
1628 if (DEV_CFG(dev)->wp_gpios_exist) {
1629 if (!device_is_ready(DEV_CFG(dev)->wp.port)) {
1630 LOG_ERR("Write-protect pin not ready");
1631 return -ENODEV;
1632 }
1633 if (gpio_pin_configure_dt(&(DEV_CFG(dev)->wp), GPIO_OUTPUT_ACTIVE)) {
1634 LOG_ERR("Write-protect pin failed to set active");
1635 return -ENODEV;
1636 }
1637 }
1638 #endif /* ANY_INST_HAS_WP_GPIOS */
1639 #if ANY_INST_HAS_HOLD_GPIOS
1640 if (DEV_CFG(dev)->hold_gpios_exist) {
1641 if (!device_is_ready(DEV_CFG(dev)->hold.port)) {
1642 LOG_ERR("Hold pin not ready");
1643 return -ENODEV;
1644 }
1645 if (gpio_pin_configure_dt(&(DEV_CFG(dev)->hold), GPIO_OUTPUT_INACTIVE)) {
1646 LOG_ERR("Hold pin failed to set inactive");
1647 return -ENODEV;
1648 }
1649 }
1650 #endif /* ANY_INST_HAS_HOLD_GPIOS */
1651
1652 return pm_device_driver_init(dev, spi_nor_pm_control);
1653 }
1654
1655 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1656
spi_nor_pages_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)1657 static void spi_nor_pages_layout(const struct device *dev,
1658 const struct flash_pages_layout **layout,
1659 size_t *layout_size)
1660 {
1661 /* Data for runtime, const for devicetree and minimal. */
1662 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
1663 const struct spi_nor_data *data = dev->data;
1664
1665 *layout = &data->layout;
1666 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1667 const struct spi_nor_config *cfg = dev->config;
1668
1669 *layout = &cfg->layout;
1670 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1671
1672 *layout_size = 1;
1673 }
1674
1675 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1676
1677 static const struct flash_parameters *
flash_nor_get_parameters(const struct device * dev)1678 flash_nor_get_parameters(const struct device *dev)
1679 {
1680 ARG_UNUSED(dev);
1681
1682 return &flash_nor_parameters;
1683 }
1684
flash_nor_get_size(const struct device * dev,uint64_t * size)1685 static int flash_nor_get_size(const struct device *dev, uint64_t *size)
1686 {
1687 *size = (uint64_t)dev_flash_size(dev);
1688
1689 return 0;
1690 }
1691
1692 static DEVICE_API(flash, spi_nor_api) = {
1693 .read = spi_nor_read,
1694 .write = spi_nor_write,
1695 .erase = spi_nor_erase,
1696 .get_parameters = flash_nor_get_parameters,
1697 .get_size = flash_nor_get_size,
1698 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1699 .page_layout = spi_nor_pages_layout,
1700 #endif
1701 #if defined(CONFIG_FLASH_JESD216_API)
1702 .sfdp_read = spi_nor_sfdp_read,
1703 .read_jedec_id = spi_nor_read_jedec_id,
1704 #endif
1705 #if defined(CONFIG_FLASH_EX_OP_ENABLED)
1706 .ex_op = flash_spi_nor_ex_op,
1707 #endif
1708 };
1709
1710 #define PAGE_LAYOUT_GEN(idx) \
1711 BUILD_ASSERT(DT_INST_NODE_HAS_PROP(idx, size), \
1712 "jedec,spi-nor size required for non-runtime SFDP page layout"); \
1713 enum { \
1714 INST_##idx##_BYTES = (DT_INST_PROP(idx, size) / 8) \
1715 }; \
1716 BUILD_ASSERT(SPI_NOR_IS_SECTOR_ALIGNED(CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE), \
1717 "SPI_NOR_FLASH_LAYOUT_PAGE_SIZE must be multiple of 4096"); \
1718 enum { \
1719 LAYOUT_PAGES_##idx##_COUNT = \
1720 (INST_##idx##_BYTES / CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE) \
1721 }; \
1722 BUILD_ASSERT((CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE * LAYOUT_PAGES_##idx##_COUNT) == \
1723 INST_##idx##_BYTES, \
1724 "SPI_NOR_FLASH_LAYOUT_PAGE_SIZE incompatible with flash size");
1725
1726 #define SFDP_BFP_ATTR_GEN(idx) \
1727 BUILD_ASSERT(DT_INST_NODE_HAS_PROP(idx, sfdp_bfp), \
1728 "jedec,spi-nor sfdp-bfp required for devicetree SFDP"); \
1729 static const __aligned(4) uint8_t bfp_##idx##_data[] = DT_INST_PROP(idx, sfdp_bfp);
1730
1731 #define INST_ATTR_GEN(idx) \
1732 BUILD_ASSERT(DT_INST_NODE_HAS_PROP(idx, jedec_id), \
1733 "jedec,spi-nor jedec-id required for non-runtime SFDP"); \
1734 IF_ENABLED(CONFIG_FLASH_PAGE_LAYOUT, (PAGE_LAYOUT_GEN(idx))) \
1735 IF_ENABLED(CONFIG_SPI_NOR_SFDP_DEVICETREE, (SFDP_BFP_ATTR_GEN(idx)))
1736
1737 #define ATTRIBUTES_DEFINE(idx) COND_CODE_1(CONFIG_SPI_NOR_SFDP_RUNTIME, EMPTY(), \
1738 (INST_ATTR_GEN(idx)))
1739
1740 #define DEFINE_PAGE_LAYOUT(idx) \
1741 IF_ENABLED(CONFIG_FLASH_PAGE_LAYOUT, \
1742 (.layout = { \
1743 .pages_count = LAYOUT_PAGES_##idx##_COUNT, \
1744 .pages_size = CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE, \
1745 },))
1746
1747 #define INST_HAS_LOCK(idx) DT_INST_NODE_HAS_PROP(idx, has_lock)
1748
1749 #define LOCK_DEFINE(idx) \
1750 IF_ENABLED(INST_HAS_LOCK(idx), (BUILD_ASSERT(DT_INST_PROP(idx, has_lock) == \
1751 (DT_INST_PROP(idx, has_lock) & 0xFF), \
1752 "Need support for lock clear beyond SR1");))
1753
1754 #define CONFIGURE_4BYTE_ADDR(idx) .enter_4byte_addr = DT_INST_PROP_OR(idx, enter_4byte_addr, 0),
1755
1756 #define INIT_T_ENTER_DPD(idx) \
1757 COND_CODE_1(DT_INST_NODE_HAS_PROP(idx, t_enter_dpd), \
1758 (.t_enter_dpd = \
1759 DIV_ROUND_UP(DT_INST_PROP(idx, t_enter_dpd), NSEC_PER_MSEC)),\
1760 (.t_enter_dpd = 0))
1761
1762 #if ANY_INST_HAS_T_EXIT_DPD
1763 #define INIT_T_EXIT_DPD(idx) \
1764 COND_CODE_1( \
1765 DT_INST_NODE_HAS_PROP(idx, t_exit_dpd), \
1766 (.t_exit_dpd = DIV_ROUND_UP(DT_INST_PROP(idx, t_exit_dpd), NSEC_PER_MSEC)),\
1767 (.t_exit_dpd = 0))
1768 #endif
1769
1770 #define INIT_WP_GPIOS(idx) .wp = GPIO_DT_SPEC_INST_GET_OR(idx, wp_gpios, {0})
1771
1772 #define INIT_HOLD_GPIOS(idx) .hold = GPIO_DT_SPEC_INST_GET_OR(idx, hold_gpios, {0})
1773
1774 #define INIT_WAKEUP_SEQ_PARAMS(idx) \
1775 COND_CODE_1(DT_INST_NODE_HAS_PROP(idx, dpd_wakeup_sequence), \
1776 (.t_dpdd_ms = DIV_ROUND_UP( \
1777 DT_INST_PROP_BY_IDX(idx, dpd_wakeup_sequence, 0), NSEC_PER_MSEC),\
1778 .t_crdp_ms = DIV_ROUND_UP( \
1779 DT_INST_PROP_BY_IDX(idx, dpd_wakeup_sequence, 1), NSEC_PER_MSEC),\
1780 .t_rdp_ms = DIV_ROUND_UP( \
1781 DT_INST_PROP_BY_IDX(idx, dpd_wakeup_sequence, 2), NSEC_PER_MSEC)),\
1782 (.t_dpdd_ms = 0, .t_crdp_ms = 0, .t_rdp_ms = 0))
1783
1784 #define INIT_MXICY_MX25R_POWER_MODE(idx) \
1785 .mxicy_mx25r_power_mode = DT_INST_ENUM_IDX_OR(idx, mxicy_mx25r_power_mode, 0)
1786
1787 #define INIT_RESET_GPIOS(idx) .reset = GPIO_DT_SPEC_INST_GET_OR(idx, reset_gpios, {0})
1788
1789 #define INST_CONFIG_STRUCT_GEN(idx) \
1790 DEFINE_PAGE_LAYOUT(idx) \
1791 .flash_size = DT_INST_PROP(idx, size) / 8, \
1792 .jedec_id = DT_INST_PROP(idx, jedec_id), \
1793 IF_ENABLED(CONFIG_SPI_NOR_SFDP_MINIMAL, (CONFIGURE_4BYTE_ADDR(idx))) \
1794 IF_ENABLED(CONFIG_SPI_NOR_SFDP_DEVICETREE, \
1795 (.bfp_len = sizeof(bfp_##idx##_data) / 4, \
1796 .bfp = (const struct jesd216_bfp *)bfp_##idx##_data,))
1797
1798 #define GENERATE_CONFIG_STRUCT(idx) \
1799 static const struct spi_nor_config spi_nor_##idx##_config = { \
1800 .spi = SPI_DT_SPEC_INST_GET(idx, SPI_WORD_SET(8), CONFIG_SPI_NOR_CS_WAIT_DELAY),\
1801 .dpd_exist = DT_INST_PROP(idx, has_dpd), \
1802 .dpd_wakeup_sequence_exist = DT_INST_NODE_HAS_PROP(idx, dpd_wakeup_sequence), \
1803 .mxicy_mx25r_power_mode_exist = \
1804 DT_INST_NODE_HAS_PROP(idx, mxicy_mx25r_power_mode), \
1805 .reset_gpios_exist = DT_INST_NODE_HAS_PROP(idx, reset_gpios), \
1806 .requires_ulbpr_exist = DT_INST_PROP(idx, requires_ulbpr), \
1807 .wp_gpios_exist = DT_INST_NODE_HAS_PROP(idx, wp_gpios), \
1808 .hold_gpios_exist = DT_INST_NODE_HAS_PROP(idx, hold_gpios), \
1809 .use_4b_addr_opcodes = DT_INST_PROP(idx, use_4b_addr_opcodes), \
1810 IF_ENABLED(INST_HAS_LOCK(idx), (.has_lock = DT_INST_PROP(idx, has_lock),)) \
1811 IF_ENABLED(ANY_INST_HAS_DPD, (INIT_T_ENTER_DPD(idx),)) \
1812 IF_ENABLED(UTIL_AND(ANY_INST_HAS_DPD, ANY_INST_HAS_T_EXIT_DPD), \
1813 (INIT_T_EXIT_DPD(idx),)) \
1814 IF_ENABLED(ANY_INST_HAS_DPD_WAKEUP_SEQUENCE, (INIT_WAKEUP_SEQ_PARAMS(idx),)) \
1815 IF_ENABLED(ANY_INST_HAS_MXICY_MX25R_POWER_MODE, \
1816 (INIT_MXICY_MX25R_POWER_MODE(idx),)) \
1817 IF_ENABLED(ANY_INST_HAS_RESET_GPIOS, (INIT_RESET_GPIOS(idx),)) \
1818 IF_ENABLED(ANY_INST_HAS_WP_GPIOS, (INIT_WP_GPIOS(idx),)) \
1819 IF_ENABLED(ANY_INST_HAS_HOLD_GPIOS, (INIT_HOLD_GPIOS(idx),)) \
1820 IF_DISABLED(CONFIG_SPI_NOR_SFDP_RUNTIME, (INST_CONFIG_STRUCT_GEN(idx)))};
1821
1822 #define ASSIGN_PM(idx) \
1823 PM_DEVICE_DT_INST_DEFINE(idx, spi_nor_pm_control);
1824
1825 #define SPI_NOR_INST(idx) \
1826 ASSIGN_PM(idx) \
1827 ATTRIBUTES_DEFINE(idx) \
1828 LOCK_DEFINE(idx) \
1829 GENERATE_CONFIG_STRUCT(idx) \
1830 static struct spi_nor_data spi_nor_##idx##_data; \
1831 DEVICE_DT_INST_DEFINE(idx, &spi_nor_init, PM_DEVICE_DT_INST_GET(idx), \
1832 &spi_nor_##idx##_data, &spi_nor_##idx##_config, \
1833 POST_KERNEL, CONFIG_SPI_NOR_INIT_PRIORITY, &spi_nor_api);
1834
1835 DT_INST_FOREACH_STATUS_OKAY(SPI_NOR_INST)
1836