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