1 /*
2 * Copyright (c) 2018 Savoir-Faire Linux.
3 * Copyright (c) 2020 Peter Bigot Consulting, LLC
4 *
5 * This driver is heavily inspired from the spi_flash_w25qxxdv.c SPI NOR driver.
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #define DT_DRV_COMPAT jedec_spi_nor
11
12 #include <errno.h>
13 #include <zephyr/drivers/flash.h>
14 #include <zephyr/drivers/spi.h>
15 #include <zephyr/init.h>
16 #include <string.h>
17 #include <zephyr/logging/log.h>
18 #include <zephyr/sys_clock.h>
19
20 #include "spi_nor.h"
21 #include "jesd216.h"
22 #include "flash_priv.h"
23
24 LOG_MODULE_REGISTER(spi_nor, CONFIG_FLASH_LOG_LEVEL);
25
26 /* Device Power Management Notes
27 *
28 * These flash devices have several modes during operation:
29 * * When CSn is asserted (during a SPI operation) the device is
30 * active.
31 * * When CSn is deasserted the device enters a standby mode.
32 * * Some devices support a Deep Power-Down mode which reduces current
33 * to as little as 0.1% of standby.
34 *
35 * The power reduction from DPD is sufficient to warrant allowing its
36 * use even in cases where Zephyr's device power management is not
37 * available. This is selected through the SPI_NOR_IDLE_IN_DPD
38 * Kconfig option.
39 *
40 * When mapped to the Zephyr Device Power Management states:
41 * * PM_DEVICE_STATE_ACTIVE covers both active and standby modes;
42 * * PM_DEVICE_STATE_SUSPENDED, and PM_DEVICE_STATE_OFF all correspond to
43 * deep-power-down mode.
44 */
45
46 #define SPI_NOR_MAX_ADDR_WIDTH 4
47
48 #if DT_INST_NODE_HAS_PROP(0, t_enter_dpd)
49 #define T_DP_MS DIV_ROUND_UP(DT_INST_PROP(0, t_enter_dpd), NSEC_PER_MSEC)
50 #else /* T_ENTER_DPD */
51 #define T_DP_MS 0
52 #endif /* T_ENTER_DPD */
53 #if DT_INST_NODE_HAS_PROP(0, t_exit_dpd)
54 #define T_RES1_MS DIV_ROUND_UP(DT_INST_PROP(0, t_exit_dpd), NSEC_PER_MSEC)
55 #endif /* T_EXIT_DPD */
56 #if DT_INST_NODE_HAS_PROP(0, dpd_wakeup_sequence)
57 #define T_DPDD_MS DIV_ROUND_UP(DT_INST_PROP_BY_IDX(0, dpd_wakeup_sequence, 0), NSEC_PER_MSEC)
58 #define T_CRDP_MS DIV_ROUND_UP(DT_INST_PROP_BY_IDX(0, dpd_wakeup_sequence, 1), NSEC_PER_MSEC)
59 #define T_RDP_MS DIV_ROUND_UP(DT_INST_PROP_BY_IDX(0, dpd_wakeup_sequence, 2), NSEC_PER_MSEC)
60 #else /* DPD_WAKEUP_SEQUENCE */
61 #define T_DPDD_MS 0
62 #endif /* DPD_WAKEUP_SEQUENCE */
63
64 /* Build-time data associated with the device. */
65 struct spi_nor_config {
66 /* Devicetree SPI configuration */
67 struct spi_dt_spec spi;
68
69 /* Runtime SFDP stores no static configuration. */
70
71 #ifndef CONFIG_SPI_NOR_SFDP_RUNTIME
72 /* Size of device in bytes, from size property */
73 uint32_t flash_size;
74
75 #ifdef CONFIG_FLASH_PAGE_LAYOUT
76 /* Flash page layout can be determined from devicetree. */
77 struct flash_pages_layout layout;
78 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
79
80 /* Expected JEDEC ID, from jedec-id property */
81 uint8_t jedec_id[SPI_NOR_MAX_ID_LEN];
82
83 #if defined(CONFIG_SPI_NOR_SFDP_MINIMAL)
84 /* Optional support for entering 32-bit address mode. */
85 uint8_t enter_4byte_addr;
86 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
87
88 #if defined(CONFIG_SPI_NOR_SFDP_DEVICETREE)
89 /* Length of BFP structure, in 32-bit words. */
90 uint8_t bfp_len;
91
92 /* Pointer to the BFP table as read from the device
93 * (little-endian stored words), from sfdp-bfp property
94 */
95 const struct jesd216_bfp *bfp;
96 #endif /* CONFIG_SPI_NOR_SFDP_DEVICETREE */
97 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
98
99 /* Optional bits in SR to be cleared on startup.
100 *
101 * This information cannot be derived from SFDP.
102 */
103 uint8_t has_lock;
104 };
105
106 /**
107 * struct spi_nor_data - Structure for defining the SPI NOR access
108 * @sem: The semaphore to access to the flash
109 */
110 struct spi_nor_data {
111 struct k_sem sem;
112 #if DT_INST_NODE_HAS_PROP(0, has_dpd)
113 /* Low 32-bits of uptime counter at which device last entered
114 * deep power-down.
115 */
116 uint32_t ts_enter_dpd;
117 #endif
118
119 /* Miscellaneous flags */
120
121 /* If set addressed operations should use 32-bit rather than
122 * 24-bit addresses.
123 *
124 * This is ignored if the access parameter to a command
125 * explicitly specifies 24-bit or 32-bit addressing.
126 */
127 bool flag_access_32bit: 1;
128
129 /* Minimal SFDP stores no dynamic configuration. Runtime and
130 * devicetree store page size and erase_types; runtime also
131 * stores flash size and layout.
132 */
133 #ifndef CONFIG_SPI_NOR_SFDP_MINIMAL
134
135 struct jesd216_erase_type erase_types[JESD216_NUM_ERASE_TYPES];
136
137 /* Number of bytes per page */
138 uint16_t page_size;
139
140 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
141 /* Size of flash, in bytes */
142 uint32_t flash_size;
143
144 #ifdef CONFIG_FLASH_PAGE_LAYOUT
145 struct flash_pages_layout layout;
146 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
147 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
148 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
149 };
150
151 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
152 /* The historically supported erase sizes. */
153 static const struct jesd216_erase_type minimal_erase_types[JESD216_NUM_ERASE_TYPES] = {
154 {
155 .cmd = SPI_NOR_CMD_BE,
156 .exp = 16,
157 },
158 {
159 .cmd = SPI_NOR_CMD_SE,
160 .exp = 12,
161 },
162 };
163 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
164
165 static int spi_nor_write_protection_set(const struct device *dev,
166 bool write_protect);
167
168 /* Get pointer to array of supported erase types. Static const for
169 * minimal, data for runtime and devicetree.
170 */
171 static inline const struct jesd216_erase_type *
dev_erase_types(const struct device * dev)172 dev_erase_types(const struct device *dev)
173 {
174 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
175 return minimal_erase_types;
176 #else /* CONFIG_SPI_NOR_SFDP_MINIMAL */
177 const struct spi_nor_data *data = dev->data;
178
179 return data->erase_types;
180 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
181 }
182
183 /* Get the size of the flash device. Data for runtime, constant for
184 * minimal and devicetree.
185 */
dev_flash_size(const struct device * dev)186 static inline uint32_t dev_flash_size(const struct device *dev)
187 {
188 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
189 const struct spi_nor_data *data = dev->data;
190
191 return data->flash_size;
192 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
193 const struct spi_nor_config *cfg = dev->config;
194
195 return cfg->flash_size;
196 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
197 }
198
199 /* Get the flash device page size. Constant for minimal, data for
200 * runtime and devicetree.
201 */
dev_page_size(const struct device * dev)202 static inline uint16_t dev_page_size(const struct device *dev)
203 {
204 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
205 return 256;
206 #else /* CONFIG_SPI_NOR_SFDP_MINIMAL */
207 const struct spi_nor_data *data = dev->data;
208
209 return data->page_size;
210 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
211 }
212
213 static const struct flash_parameters flash_nor_parameters = {
214 .write_block_size = 1,
215 .erase_value = 0xff,
216 };
217
218 /* Capture the time at which the device entered deep power-down. */
record_entered_dpd(const struct device * const dev)219 static inline void record_entered_dpd(const struct device *const dev)
220 {
221 #if DT_INST_NODE_HAS_PROP(0, has_dpd)
222 struct spi_nor_data *const driver_data = dev->data;
223
224 driver_data->ts_enter_dpd = k_uptime_get_32();
225 #endif
226 }
227
228 /* Check the current time against the time DPD was entered and delay
229 * until it's ok to initiate the DPD exit process.
230 */
delay_until_exit_dpd_ok(const struct device * const dev)231 static inline void delay_until_exit_dpd_ok(const struct device *const dev)
232 {
233 #if DT_INST_NODE_HAS_PROP(0, has_dpd)
234 struct spi_nor_data *const driver_data = dev->data;
235 int32_t since = (int32_t)(k_uptime_get_32() - driver_data->ts_enter_dpd);
236
237 /* If the time is negative the 32-bit counter has wrapped,
238 * which is certainly long enough no further delay is
239 * required. Otherwise we have to check whether it's been
240 * long enough taking into account necessary delays for
241 * entering and exiting DPD.
242 */
243 if (since >= 0) {
244 /* Subtract time required for DPD to be reached */
245 since -= T_DP_MS;
246
247 /* Subtract time required in DPD before exit */
248 since -= T_DPDD_MS;
249
250 /* If the adjusted time is negative we have to wait
251 * until it reaches zero before we can proceed.
252 */
253 if (since < 0) {
254 k_sleep(K_MSEC((uint32_t)-since));
255 }
256 }
257 #endif /* DT_INST_NODE_HAS_PROP(0, has_dpd) */
258 }
259
260 /* Indicates that an access command includes bytes for the address.
261 * If not provided the opcode is not followed by address bytes.
262 */
263 #define NOR_ACCESS_ADDRESSED BIT(0)
264
265 /* Indicates that addressed access uses a 24-bit address regardless of
266 * spi_nor_data::flag_32bit_addr.
267 */
268 #define NOR_ACCESS_24BIT_ADDR BIT(1)
269
270 /* Indicates that addressed access uses a 32-bit address regardless of
271 * spi_nor_data::flag_32bit_addr.
272 */
273 #define NOR_ACCESS_32BIT_ADDR BIT(2)
274
275 /* Indicates that an access command is performing a write. If not
276 * provided access is a read.
277 */
278 #define NOR_ACCESS_WRITE BIT(7)
279
280 /*
281 * @brief Send an SPI command
282 *
283 * @param dev Device struct
284 * @param opcode The command to send
285 * @param access flags that determine how the command is constructed.
286 * See NOR_ACCESS_*.
287 * @param addr The address to send
288 * @param data The buffer to store or read the value
289 * @param length The size of the buffer
290 * @return 0 on success, negative errno code otherwise
291 */
spi_nor_access(const struct device * const dev,uint8_t opcode,unsigned int access,off_t addr,void * data,size_t length)292 static int spi_nor_access(const struct device *const dev,
293 uint8_t opcode, unsigned int access,
294 off_t addr, void *data, size_t length)
295 {
296 const struct spi_nor_config *const driver_cfg = dev->config;
297 struct spi_nor_data *const driver_data = dev->data;
298 bool is_addressed = (access & NOR_ACCESS_ADDRESSED) != 0U;
299 bool is_write = (access & NOR_ACCESS_WRITE) != 0U;
300 uint8_t buf[5] = { 0 };
301 struct spi_buf spi_buf[2] = {
302 {
303 .buf = buf,
304 .len = 1,
305 },
306 {
307 .buf = data,
308 .len = length
309 }
310 };
311
312 buf[0] = opcode;
313 if (is_addressed) {
314 bool access_24bit = (access & NOR_ACCESS_24BIT_ADDR) != 0;
315 bool access_32bit = (access & NOR_ACCESS_32BIT_ADDR) != 0;
316 bool use_32bit = (access_32bit
317 || (!access_24bit
318 && driver_data->flag_access_32bit));
319 union {
320 uint32_t u32;
321 uint8_t u8[4];
322 } addr32 = {
323 .u32 = sys_cpu_to_be32(addr),
324 };
325
326 if (use_32bit) {
327 memcpy(&buf[1], &addr32.u8[0], 4);
328 spi_buf[0].len += 4;
329 } else {
330 memcpy(&buf[1], &addr32.u8[1], 3);
331 spi_buf[0].len += 3;
332 }
333 };
334
335 const struct spi_buf_set tx_set = {
336 .buffers = spi_buf,
337 .count = (length != 0) ? 2 : 1,
338 };
339
340 const struct spi_buf_set rx_set = {
341 .buffers = spi_buf,
342 .count = 2,
343 };
344
345 if (is_write) {
346 return spi_write_dt(&driver_cfg->spi, &tx_set);
347 }
348
349 return spi_transceive_dt(&driver_cfg->spi, &tx_set, &rx_set);
350 }
351
352 #define spi_nor_cmd_read(dev, opcode, dest, length) \
353 spi_nor_access(dev, opcode, 0, 0, dest, length)
354 #define spi_nor_cmd_addr_read(dev, opcode, addr, dest, length) \
355 spi_nor_access(dev, opcode, NOR_ACCESS_ADDRESSED, addr, dest, length)
356 #define spi_nor_cmd_write(dev, opcode) \
357 spi_nor_access(dev, opcode, NOR_ACCESS_WRITE, 0, NULL, 0)
358 #define spi_nor_cmd_addr_write(dev, opcode, addr, src, length) \
359 spi_nor_access(dev, opcode, NOR_ACCESS_WRITE | NOR_ACCESS_ADDRESSED, \
360 addr, (void *)src, length)
361
362 /**
363 * @brief Wait until the flash is ready
364 *
365 * @note The device must be externally acquired before invoking this
366 * function.
367 *
368 * This function should be invoked after every ERASE, PROGRAM, or
369 * WRITE_STATUS operation before continuing. This allows us to assume
370 * that the device is ready to accept new commands at any other point
371 * in the code.
372 *
373 * @param dev The device structure
374 * @return 0 on success, negative errno code otherwise
375 */
spi_nor_wait_until_ready(const struct device * dev)376 static int spi_nor_wait_until_ready(const struct device *dev)
377 {
378 int ret;
379 uint8_t reg;
380
381 do {
382 ret = spi_nor_cmd_read(dev, SPI_NOR_CMD_RDSR, ®, sizeof(reg));
383 } while (!ret && (reg & SPI_NOR_WIP_BIT));
384
385 return ret;
386 }
387
388 #if defined(CONFIG_SPI_NOR_SFDP_RUNTIME) || defined(CONFIG_FLASH_JESD216_API)
389 /*
390 * @brief Read content from the SFDP hierarchy
391 *
392 * @note The device must be externally acquired before invoking this
393 * function.
394 *
395 * @param dev Device struct
396 * @param addr The address to send
397 * @param data The buffer to store or read the value
398 * @param length The size of the buffer
399 * @return 0 on success, negative errno code otherwise
400 */
read_sfdp(const struct device * const dev,off_t addr,void * data,size_t length)401 static int read_sfdp(const struct device *const dev,
402 off_t addr, void *data, size_t length)
403 {
404 /* READ_SFDP requires a 24-bit address followed by a single
405 * byte for a wait state. This is effected by using 32-bit
406 * address by shifting the 24-bit address up 8 bits.
407 */
408 return spi_nor_access(dev, JESD216_CMD_READ_SFDP,
409 NOR_ACCESS_32BIT_ADDR | NOR_ACCESS_ADDRESSED,
410 addr << 8, data, length);
411 }
412 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
413
enter_dpd(const struct device * const dev)414 static int enter_dpd(const struct device *const dev)
415 {
416 int ret = 0;
417
418 if (IS_ENABLED(DT_INST_PROP(0, has_dpd))) {
419 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_DPD);
420 if (ret == 0) {
421 record_entered_dpd(dev);
422 }
423 }
424 return ret;
425 }
426
exit_dpd(const struct device * const dev)427 static int exit_dpd(const struct device *const dev)
428 {
429 int ret = 0;
430
431 if (IS_ENABLED(DT_INST_PROP(0, has_dpd))) {
432 delay_until_exit_dpd_ok(dev);
433
434 #if DT_INST_NODE_HAS_PROP(0, dpd_wakeup_sequence)
435 /* Assert CSn and wait for tCRDP.
436 *
437 * Unfortunately the SPI API doesn't allow us to
438 * control CSn so fake it by writing a known-supported
439 * single-byte command, hoping that'll hold the assert
440 * long enough. This is highly likely, since the
441 * duration is usually less than two SPI clock cycles.
442 */
443 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_RDID);
444
445 /* Deassert CSn and wait for tRDP */
446 k_sleep(K_MSEC(T_RDP_MS));
447 #else /* DPD_WAKEUP_SEQUENCE */
448 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_RDPD);
449
450 if (ret == 0) {
451 #if DT_INST_NODE_HAS_PROP(0, t_exit_dpd)
452 k_sleep(K_MSEC(T_RES1_MS));
453 #endif /* T_EXIT_DPD */
454 }
455 #endif /* DPD_WAKEUP_SEQUENCE */
456 }
457 return ret;
458 }
459
460 /* Everything necessary to acquire owning access to the device.
461 *
462 * This means taking the lock and, if necessary, waking the device
463 * from deep power-down mode.
464 */
acquire_device(const struct device * dev)465 static void acquire_device(const struct device *dev)
466 {
467 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
468 struct spi_nor_data *const driver_data = dev->data;
469
470 k_sem_take(&driver_data->sem, K_FOREVER);
471 }
472
473 if (IS_ENABLED(CONFIG_SPI_NOR_IDLE_IN_DPD)) {
474 exit_dpd(dev);
475 }
476 }
477
478 /* Everything necessary to release access to the device.
479 *
480 * This means (optionally) putting the device into deep power-down
481 * mode, and releasing the lock.
482 */
release_device(const struct device * dev)483 static void release_device(const struct device *dev)
484 {
485 if (IS_ENABLED(CONFIG_SPI_NOR_IDLE_IN_DPD)) {
486 enter_dpd(dev);
487 }
488
489 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
490 struct spi_nor_data *const driver_data = dev->data;
491
492 k_sem_give(&driver_data->sem);
493 }
494 }
495
496 /**
497 * @brief Read the status register.
498 *
499 * @note The device must be externally acquired before invoking this
500 * function.
501 *
502 * @param dev Device struct
503 *
504 * @return the non-negative value of the status register, or an error code.
505 */
spi_nor_rdsr(const struct device * dev)506 static int spi_nor_rdsr(const struct device *dev)
507 {
508 uint8_t reg;
509 int ret = spi_nor_cmd_read(dev, SPI_NOR_CMD_RDSR, ®, sizeof(reg));
510
511 if (ret == 0) {
512 ret = reg;
513 }
514
515 return ret;
516 }
517
518 /**
519 * @brief Write the status register.
520 *
521 * @note The device must be externally acquired before invoking this
522 * function.
523 *
524 * @param dev Device struct
525 * @param sr The new value of the status register
526 *
527 * @return 0 on success or a negative error code.
528 */
spi_nor_wrsr(const struct device * dev,uint8_t sr)529 static int spi_nor_wrsr(const struct device *dev,
530 uint8_t sr)
531 {
532 int ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
533
534 if (ret == 0) {
535 ret = spi_nor_access(dev, SPI_NOR_CMD_WRSR, NOR_ACCESS_WRITE, 0, &sr,
536 sizeof(sr));
537 spi_nor_wait_until_ready(dev);
538 }
539
540 return ret;
541 }
542
543 #if DT_INST_NODE_HAS_PROP(0, mxicy_mx25r_power_mode)
544
545 /**
546 * @brief Read the configuration register.
547 *
548 * @note The device must be externally acquired before invoking this
549 * function.
550 *
551 * @param dev Device struct
552 *
553 * @return the non-negative value of the configuration register, or an error code.
554 */
mxicy_rdcr(const struct device * dev)555 static int mxicy_rdcr(const struct device *dev)
556 {
557 uint16_t cr;
558 enum { CMD_RDCR = 0x15 };
559 int ret = spi_nor_cmd_read(dev, CMD_RDCR, &cr, sizeof(cr));
560
561 if (ret < 0) {
562 return ret;
563 }
564
565 return cr;
566 }
567
568 /**
569 * @brief Write the configuration register.
570 *
571 * @note The device must be externally acquired before invoking this
572 * function.
573 *
574 * @param dev Device struct
575 * @param cr The new value of the configuration register
576 *
577 * @return 0 on success or a negative error code.
578 */
mxicy_wrcr(const struct device * dev,uint16_t cr)579 static int mxicy_wrcr(const struct device *dev,
580 uint16_t cr)
581 {
582 /* The configuration register bytes on the Macronix MX25R devices are
583 * written using the Write Status Register command where the configuration
584 * register bytes are written as two extra bytes after the status register.
585 * First read out the current status register to preserve the value.
586 */
587 int sr = spi_nor_rdsr(dev);
588
589 if (sr < 0) {
590 LOG_ERR("Read status register failed: %d", sr);
591 return sr;
592 }
593
594 int ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
595
596 if (ret == 0) {
597 uint8_t data[] = {
598 sr,
599 cr & 0xFF, /* Configuration register 1 */
600 cr >> 8 /* Configuration register 2 */
601 };
602
603 ret = spi_nor_access(dev, SPI_NOR_CMD_WRSR, NOR_ACCESS_WRITE, 0, data,
604 sizeof(data));
605 spi_nor_wait_until_ready(dev);
606 }
607
608 return ret;
609 }
610
mxicy_configure(const struct device * dev,const uint8_t * jedec_id)611 static int mxicy_configure(const struct device *dev, const uint8_t *jedec_id)
612 {
613 /* Low-power/high perf mode is second bit in configuration register 2 */
614 enum { LH_SWITCH_BIT = 9 };
615 const uint8_t JEDEC_MACRONIX_ID = 0xc2;
616 const uint8_t JEDEC_MX25R_TYPE_ID = 0x28;
617 int current_cr, new_cr, ret;
618 /* lh_switch enum index:
619 * 0: Ultra low power
620 * 1: High performance mode
621 */
622 const bool use_high_perf = DT_INST_ENUM_IDX(0, mxicy_mx25r_power_mode);
623
624 /* Only supported on Macronix MX25R Ultra Low Power series. */
625 if (jedec_id[0] != JEDEC_MACRONIX_ID || jedec_id[1] != JEDEC_MX25R_TYPE_ID) {
626 LOG_WRN("L/H switch not supported for device id: %02x %02x %02x", jedec_id[0],
627 jedec_id[1], jedec_id[2]);
628 /* Do not return an error here because the flash still functions */
629 return 0;
630 }
631
632 acquire_device(dev);
633
634 /* Read current configuration register */
635
636 ret = mxicy_rdcr(dev);
637 if (ret < 0) {
638 return ret;
639 }
640 current_cr = ret;
641
642 LOG_DBG("Use high performance mode? %d", use_high_perf);
643 new_cr = current_cr;
644 WRITE_BIT(new_cr, LH_SWITCH_BIT, use_high_perf);
645 if (new_cr != current_cr) {
646 ret = mxicy_wrcr(dev, new_cr);
647 } else {
648 ret = 0;
649 }
650
651 if (ret < 0) {
652 LOG_ERR("Enable high performace mode failed: %d", ret);
653 }
654
655 release_device(dev);
656
657 return ret;
658 }
659
660 #endif /* DT_INST_NODE_HAS_PROP(0, mxicy_mx25r_power_mode) */
661
spi_nor_read(const struct device * dev,off_t addr,void * dest,size_t size)662 static int spi_nor_read(const struct device *dev, off_t addr, void *dest,
663 size_t size)
664 {
665 const size_t flash_size = dev_flash_size(dev);
666 int ret;
667
668 /* should be between 0 and flash size */
669 if ((addr < 0) || ((addr + size) > flash_size)) {
670 return -EINVAL;
671 }
672
673 acquire_device(dev);
674
675 ret = spi_nor_cmd_addr_read(dev, SPI_NOR_CMD_READ, addr, dest, size);
676
677 release_device(dev);
678 return ret;
679 }
680
spi_nor_write(const struct device * dev,off_t addr,const void * src,size_t size)681 static int spi_nor_write(const struct device *dev, off_t addr,
682 const void *src,
683 size_t size)
684 {
685 const size_t flash_size = dev_flash_size(dev);
686 const uint16_t page_size = dev_page_size(dev);
687 int ret = 0;
688
689 /* should be between 0 and flash size */
690 if ((addr < 0) || ((size + addr) > flash_size)) {
691 return -EINVAL;
692 }
693
694 acquire_device(dev);
695 ret = spi_nor_write_protection_set(dev, false);
696 if (ret == 0) {
697 while (size > 0) {
698 size_t to_write = size;
699
700 /* Don't write more than a page. */
701 if (to_write >= page_size) {
702 to_write = page_size;
703 }
704
705 /* Don't write across a page boundary */
706 if (((addr + to_write - 1U) / page_size)
707 != (addr / page_size)) {
708 to_write = page_size - (addr % page_size);
709 }
710
711 spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
712 ret = spi_nor_cmd_addr_write(dev, SPI_NOR_CMD_PP, addr,
713 src, to_write);
714 if (ret != 0) {
715 break;
716 }
717
718 size -= to_write;
719 src = (const uint8_t *)src + to_write;
720 addr += to_write;
721
722 spi_nor_wait_until_ready(dev);
723 }
724 }
725
726 int ret2 = spi_nor_write_protection_set(dev, true);
727
728 if (!ret) {
729 ret = ret2;
730 }
731
732 release_device(dev);
733 return ret;
734 }
735
spi_nor_erase(const struct device * dev,off_t addr,size_t size)736 static int spi_nor_erase(const struct device *dev, off_t addr, size_t size)
737 {
738 const size_t flash_size = dev_flash_size(dev);
739 int ret = 0;
740
741 /* erase area must be subregion of device */
742 if ((addr < 0) || ((size + addr) > flash_size)) {
743 return -EINVAL;
744 }
745
746 /* address must be sector-aligned */
747 if (!SPI_NOR_IS_SECTOR_ALIGNED(addr)) {
748 return -EINVAL;
749 }
750
751 /* size must be a multiple of sectors */
752 if ((size % SPI_NOR_SECTOR_SIZE) != 0) {
753 return -EINVAL;
754 }
755
756 acquire_device(dev);
757 ret = spi_nor_write_protection_set(dev, false);
758
759 while ((size > 0) && (ret == 0)) {
760 spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
761
762 if (size == flash_size) {
763 /* chip erase */
764 spi_nor_cmd_write(dev, SPI_NOR_CMD_CE);
765 size -= flash_size;
766 } else {
767 const struct jesd216_erase_type *erase_types =
768 dev_erase_types(dev);
769 const struct jesd216_erase_type *bet = NULL;
770
771 for (uint8_t ei = 0; ei < JESD216_NUM_ERASE_TYPES; ++ei) {
772 const struct jesd216_erase_type *etp =
773 &erase_types[ei];
774
775 if ((etp->exp != 0)
776 && SPI_NOR_IS_ALIGNED(addr, etp->exp)
777 && SPI_NOR_IS_ALIGNED(size, etp->exp)
778 && ((bet == NULL)
779 || (etp->exp > bet->exp))) {
780 bet = etp;
781 }
782 }
783 if (bet != NULL) {
784 spi_nor_cmd_addr_write(dev, bet->cmd, addr, NULL, 0);
785 addr += BIT(bet->exp);
786 size -= BIT(bet->exp);
787 } else {
788 LOG_DBG("Can't erase %zu at 0x%lx",
789 size, (long)addr);
790 ret = -EINVAL;
791 }
792 }
793
794 #ifdef __XCC__
795 /*
796 * FIXME: remove this hack once XCC is fixed.
797 *
798 * Without this volatile return value, XCC would segfault
799 * compiling this file complaining about failure in CGPREP
800 * phase.
801 */
802 volatile int xcc_ret =
803 #endif
804 spi_nor_wait_until_ready(dev);
805 }
806
807 int ret2 = spi_nor_write_protection_set(dev, true);
808
809 if (!ret) {
810 ret = ret2;
811 }
812
813 release_device(dev);
814
815 return ret;
816 }
817
818 /* @note The device must be externally acquired before invoking this
819 * function.
820 */
spi_nor_write_protection_set(const struct device * dev,bool write_protect)821 static int spi_nor_write_protection_set(const struct device *dev,
822 bool write_protect)
823 {
824 int ret;
825
826 ret = spi_nor_cmd_write(dev, (write_protect) ?
827 SPI_NOR_CMD_WRDI : SPI_NOR_CMD_WREN);
828
829 if (IS_ENABLED(DT_INST_PROP(0, requires_ulbpr))
830 && (ret == 0)
831 && !write_protect) {
832 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_ULBPR);
833 }
834
835 return ret;
836 }
837
838 #if defined(CONFIG_FLASH_JESD216_API) || defined(CONFIG_SPI_NOR_SFDP_RUNTIME)
839
spi_nor_sfdp_read(const struct device * dev,off_t addr,void * dest,size_t size)840 static int spi_nor_sfdp_read(const struct device *dev, off_t addr,
841 void *dest, size_t size)
842 {
843 acquire_device(dev);
844
845 int ret = read_sfdp(dev, addr, dest, size);
846
847 release_device(dev);
848
849 return ret;
850 }
851
852 #endif /* CONFIG_FLASH_JESD216_API || CONFIG_SPI_NOR_SFDP_RUNTIME */
853
spi_nor_read_jedec_id(const struct device * dev,uint8_t * id)854 static int spi_nor_read_jedec_id(const struct device *dev,
855 uint8_t *id)
856 {
857 if (id == NULL) {
858 return -EINVAL;
859 }
860
861 acquire_device(dev);
862
863 int ret = spi_nor_cmd_read(dev, SPI_NOR_CMD_RDID, id, SPI_NOR_MAX_ID_LEN);
864
865 release_device(dev);
866
867 return ret;
868 }
869
870 /* Put the device into the appropriate address mode, if supported.
871 *
872 * On successful return spi_nor_data::flag_access_32bit has been set
873 * (cleared) if the device is configured for 4-byte (3-byte) addresses
874 * for read, write, and erase commands.
875 *
876 * @param dev the device
877 *
878 * @param enter_4byte_addr the Enter 4-Byte Addressing bit set from
879 * DW16 of SFDP BFP. A value of all zeros or all ones is interpreted
880 * as "not supported".
881 *
882 * @retval -ENOTSUP if 4-byte addressing is supported but not in a way
883 * that the driver can handle.
884 * @retval negative codes if the attempt was made and failed
885 * @retval 0 if the device is successfully left in 24-bit mode or
886 * reconfigured to 32-bit mode.
887 */
spi_nor_set_address_mode(const struct device * dev,uint8_t enter_4byte_addr)888 static int spi_nor_set_address_mode(const struct device *dev,
889 uint8_t enter_4byte_addr)
890 {
891 int ret = 0;
892
893 /* Do nothing if not provided (either no bits or all bits
894 * set).
895 */
896 if ((enter_4byte_addr == 0)
897 || (enter_4byte_addr == 0xff)) {
898 return 0;
899 }
900
901 LOG_DBG("Checking enter-4byte-addr %02x", enter_4byte_addr);
902
903 /* This currently only supports command 0xB7 (Enter 4-Byte
904 * Address Mode), with or without preceding WREN.
905 */
906 if ((enter_4byte_addr & 0x03) == 0) {
907 return -ENOTSUP;
908 }
909
910 acquire_device(dev);
911
912 if ((enter_4byte_addr & 0x02) != 0) {
913 /* Enter after WREN. */
914 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);
915 }
916 if (ret == 0) {
917 ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_4BA);
918 }
919
920 if (ret == 0) {
921 struct spi_nor_data *data = dev->data;
922
923 data->flag_access_32bit = true;
924 }
925
926 release_device(dev);
927
928 return ret;
929 }
930
931 #ifndef CONFIG_SPI_NOR_SFDP_MINIMAL
932
spi_nor_process_bfp(const struct device * dev,const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)933 static int spi_nor_process_bfp(const struct device *dev,
934 const struct jesd216_param_header *php,
935 const struct jesd216_bfp *bfp)
936 {
937 struct spi_nor_data *data = dev->data;
938 struct jesd216_erase_type *etp = data->erase_types;
939 const size_t flash_size = jesd216_bfp_density(bfp) / 8U;
940
941 LOG_INF("%s: %u MiBy flash", dev->name, (uint32_t)(flash_size >> 20));
942
943 /* Copy over the erase types, preserving their order. (The
944 * Sector Map Parameter table references them by index.)
945 */
946 memset(data->erase_types, 0, sizeof(data->erase_types));
947 for (uint8_t ti = 1; ti <= ARRAY_SIZE(data->erase_types); ++ti) {
948 if (jesd216_bfp_erase(bfp, ti, etp) == 0) {
949 LOG_DBG("Erase %u with %02x", (uint32_t)BIT(etp->exp), etp->cmd);
950 }
951 ++etp;
952 }
953
954 data->page_size = jesd216_bfp_page_size(php, bfp);
955 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
956 data->flash_size = flash_size;
957 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
958 if (flash_size != dev_flash_size(dev)) {
959 LOG_ERR("BFP flash size mismatch with devicetree");
960 return -EINVAL;
961 }
962 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
963
964 LOG_DBG("Page size %u bytes", data->page_size);
965
966 /* If 4-byte addressing is supported, switch to it. */
967 if (jesd216_bfp_addrbytes(bfp) != JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_3B) {
968 struct jesd216_bfp_dw16 dw16;
969 int rc = 0;
970
971 if (jesd216_bfp_decode_dw16(php, bfp, &dw16) == 0) {
972 rc = spi_nor_set_address_mode(dev, dw16.enter_4ba);
973 }
974
975 if (rc != 0) {
976 LOG_ERR("Unable to enter 4-byte mode: %d\n", rc);
977 return rc;
978 }
979 }
980 return 0;
981 }
982
spi_nor_process_sfdp(const struct device * dev)983 static int spi_nor_process_sfdp(const struct device *dev)
984 {
985 int rc;
986
987 #if defined(CONFIG_SPI_NOR_SFDP_RUNTIME)
988 /* For runtime we need to read the SFDP table, identify the
989 * BFP block, and process it.
990 */
991 const uint8_t decl_nph = 2;
992 union {
993 /* We only process BFP so use one parameter block */
994 uint8_t raw[JESD216_SFDP_SIZE(decl_nph)];
995 struct jesd216_sfdp_header sfdp;
996 } u;
997 const struct jesd216_sfdp_header *hp = &u.sfdp;
998
999 rc = spi_nor_sfdp_read(dev, 0, u.raw, sizeof(u.raw));
1000 if (rc != 0) {
1001 LOG_ERR("SFDP read failed: %d", rc);
1002 return rc;
1003 }
1004
1005 uint32_t magic = jesd216_sfdp_magic(hp);
1006
1007 if (magic != JESD216_SFDP_MAGIC) {
1008 LOG_ERR("SFDP magic %08x invalid", magic);
1009 return -EINVAL;
1010 }
1011
1012 LOG_INF("%s: SFDP v %u.%u AP %x with %u PH", dev->name,
1013 hp->rev_major, hp->rev_minor, hp->access, 1 + hp->nph);
1014
1015 const struct jesd216_param_header *php = hp->phdr;
1016 const struct jesd216_param_header *phpe = php + MIN(decl_nph, 1 + hp->nph);
1017
1018 while (php != phpe) {
1019 uint16_t id = jesd216_param_id(php);
1020
1021 LOG_INF("PH%u: %04x rev %u.%u: %u DW @ %x",
1022 (php - hp->phdr), id, php->rev_major, php->rev_minor,
1023 php->len_dw, jesd216_param_addr(php));
1024
1025 if (id == JESD216_SFDP_PARAM_ID_BFP) {
1026 union {
1027 uint32_t dw[MIN(php->len_dw, 20)];
1028 struct jesd216_bfp bfp;
1029 } u;
1030 const struct jesd216_bfp *bfp = &u.bfp;
1031
1032 rc = spi_nor_sfdp_read(dev, jesd216_param_addr(php), u.dw, sizeof(u.dw));
1033 if (rc == 0) {
1034 rc = spi_nor_process_bfp(dev, php, bfp);
1035 }
1036
1037 if (rc != 0) {
1038 LOG_INF("SFDP BFP failed: %d", rc);
1039 break;
1040 }
1041 }
1042 ++php;
1043 }
1044 #elif defined(CONFIG_SPI_NOR_SFDP_DEVICETREE)
1045 /* For devicetree we need to synthesize a parameter header and
1046 * process the stored BFP data as if we had read it.
1047 */
1048 const struct spi_nor_config *cfg = dev->config;
1049 struct jesd216_param_header bfp_hdr = {
1050 .len_dw = cfg->bfp_len,
1051 };
1052
1053 rc = spi_nor_process_bfp(dev, &bfp_hdr, cfg->bfp);
1054 #else
1055 #error Unhandled SFDP choice
1056 #endif
1057
1058 return rc;
1059 }
1060
1061 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
setup_pages_layout(const struct device * dev)1062 static int setup_pages_layout(const struct device *dev)
1063 {
1064 int rv = 0;
1065
1066 #if defined(CONFIG_SPI_NOR_SFDP_RUNTIME)
1067 struct spi_nor_data *data = dev->data;
1068 const size_t flash_size = dev_flash_size(dev);
1069 const uint32_t layout_page_size = CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE;
1070 uint8_t exp = 0;
1071
1072 /* Find the smallest erase size. */
1073 for (size_t i = 0; i < ARRAY_SIZE(data->erase_types); ++i) {
1074 const struct jesd216_erase_type *etp = &data->erase_types[i];
1075
1076 if ((etp->cmd != 0)
1077 && ((exp == 0) || (etp->exp < exp))) {
1078 exp = etp->exp;
1079 }
1080 }
1081
1082 if (exp == 0) {
1083 return -ENOTSUP;
1084 }
1085
1086 uint32_t erase_size = BIT(exp);
1087
1088 /* Error if layout page size is not a multiple of smallest
1089 * erase size.
1090 */
1091 if ((layout_page_size % erase_size) != 0) {
1092 LOG_ERR("layout page %u not compatible with erase size %u",
1093 layout_page_size, erase_size);
1094 return -EINVAL;
1095 }
1096
1097 /* Warn but accept layout page sizes that leave inaccessible
1098 * space.
1099 */
1100 if ((flash_size % layout_page_size) != 0) {
1101 LOG_INF("layout page %u wastes space with device size %zu",
1102 layout_page_size, flash_size);
1103 }
1104
1105 data->layout.pages_size = layout_page_size;
1106 data->layout.pages_count = flash_size / layout_page_size;
1107 LOG_DBG("layout %u x %u By pages", data->layout.pages_count, data->layout.pages_size);
1108 #elif defined(CONFIG_SPI_NOR_SFDP_DEVICETREE)
1109 const struct spi_nor_config *cfg = dev->config;
1110 const struct flash_pages_layout *layout = &cfg->layout;
1111 const size_t flash_size = dev_flash_size(dev);
1112 size_t layout_size = layout->pages_size * layout->pages_count;
1113
1114 if (flash_size != layout_size) {
1115 LOG_ERR("device size %u mismatch %zu * %zu By pages",
1116 flash_size, layout->pages_count, layout->pages_size);
1117 return -EINVAL;
1118 }
1119 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1120 #error Unhandled SFDP choice
1121 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1122
1123 return rv;
1124 }
1125 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1126 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
1127
1128 /**
1129 * @brief Configure the flash
1130 *
1131 * @param dev The flash device structure
1132 * @param info The flash info structure
1133 * @return 0 on success, negative errno code otherwise
1134 */
spi_nor_configure(const struct device * dev)1135 static int spi_nor_configure(const struct device *dev)
1136 {
1137 const struct spi_nor_config *cfg = dev->config;
1138 uint8_t jedec_id[SPI_NOR_MAX_ID_LEN];
1139 int rc;
1140
1141 /* Validate bus and CS is ready */
1142 if (!spi_is_ready_dt(&cfg->spi)) {
1143 return -ENODEV;
1144 }
1145
1146 /* After a soft-reset the flash might be in DPD or busy writing/erasing.
1147 * Exit DPD and wait until flash is ready.
1148 */
1149 acquire_device(dev);
1150 rc = spi_nor_rdsr(dev);
1151 if (rc > 0 && (rc & SPI_NOR_WIP_BIT)) {
1152 LOG_WRN("Waiting until flash is ready");
1153 spi_nor_wait_until_ready(dev);
1154 }
1155 release_device(dev);
1156
1157 /* now the spi bus is configured, we can verify SPI
1158 * connectivity by reading the JEDEC ID.
1159 */
1160
1161 rc = spi_nor_read_jedec_id(dev, jedec_id);
1162 if (rc != 0) {
1163 LOG_ERR("JEDEC ID read failed: %d", rc);
1164 return -ENODEV;
1165 }
1166
1167 #ifndef CONFIG_SPI_NOR_SFDP_RUNTIME
1168 /* For minimal and devicetree we need to check the JEDEC ID
1169 * against the one from devicetree, to ensure we didn't find a
1170 * device that has different parameters.
1171 */
1172
1173 if (memcmp(jedec_id, cfg->jedec_id, sizeof(jedec_id)) != 0) {
1174 LOG_ERR("Device id %02x %02x %02x does not match config %02x %02x %02x",
1175 jedec_id[0], jedec_id[1], jedec_id[2],
1176 cfg->jedec_id[0], cfg->jedec_id[1], cfg->jedec_id[2]);
1177 return -EINVAL;
1178 }
1179 #endif
1180
1181 /* Check for block protect bits that need to be cleared. This
1182 * information cannot be determined from SFDP content, so the
1183 * devicetree node property must be set correctly for any device
1184 * that powers up with block protect enabled.
1185 */
1186 if (cfg->has_lock != 0) {
1187 acquire_device(dev);
1188
1189 rc = spi_nor_rdsr(dev);
1190
1191 /* Only clear if RDSR worked and something's set. */
1192 if (rc > 0) {
1193 rc = spi_nor_wrsr(dev, rc & ~cfg->has_lock);
1194 }
1195
1196 if (rc != 0) {
1197 LOG_ERR("BP clear failed: %d\n", rc);
1198 return -ENODEV;
1199 }
1200
1201 release_device(dev);
1202 }
1203
1204 #ifdef CONFIG_SPI_NOR_SFDP_MINIMAL
1205 /* For minimal we support some overrides from specific
1206 * devicertee properties.
1207 */
1208 if (cfg->enter_4byte_addr != 0) {
1209 rc = spi_nor_set_address_mode(dev, cfg->enter_4byte_addr);
1210
1211 if (rc != 0) {
1212 LOG_ERR("Unable to enter 4-byte mode: %d\n", rc);
1213 return -ENODEV;
1214 }
1215 }
1216
1217 #else /* CONFIG_SPI_NOR_SFDP_MINIMAL */
1218 /* For devicetree and runtime we need to process BFP data and
1219 * set up or validate page layout.
1220 */
1221 rc = spi_nor_process_sfdp(dev);
1222 if (rc != 0) {
1223 LOG_ERR("SFDP read failed: %d", rc);
1224 return -ENODEV;
1225 }
1226
1227 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1228 rc = setup_pages_layout(dev);
1229 if (rc != 0) {
1230 LOG_ERR("layout setup failed: %d", rc);
1231 return -ENODEV;
1232 }
1233 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1234 #endif /* CONFIG_SPI_NOR_SFDP_MINIMAL */
1235
1236 #if DT_INST_NODE_HAS_PROP(0, mxicy_mx25r_power_mode)
1237 /* Do not fail init if setting configuration register fails */
1238 (void) mxicy_configure(dev, jedec_id);
1239 #endif /* DT_INST_NODE_HAS_PROP(0, mxicy_mx25r_power_mode) */
1240
1241 if (IS_ENABLED(CONFIG_SPI_NOR_IDLE_IN_DPD)
1242 && (enter_dpd(dev) != 0)) {
1243 return -ENODEV;
1244 }
1245
1246 return 0;
1247 }
1248
1249 /**
1250 * @brief Initialize and configure the flash
1251 *
1252 * @param name The flash name
1253 * @return 0 on success, negative errno code otherwise
1254 */
spi_nor_init(const struct device * dev)1255 static int spi_nor_init(const struct device *dev)
1256 {
1257 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
1258 struct spi_nor_data *const driver_data = dev->data;
1259
1260 k_sem_init(&driver_data->sem, 1, K_SEM_MAX_LIMIT);
1261 }
1262
1263 return spi_nor_configure(dev);
1264 }
1265
1266 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1267
spi_nor_pages_layout(const struct device * dev,const struct flash_pages_layout ** layout,size_t * layout_size)1268 static void spi_nor_pages_layout(const struct device *dev,
1269 const struct flash_pages_layout **layout,
1270 size_t *layout_size)
1271 {
1272 /* Data for runtime, const for devicetree and minimal. */
1273 #ifdef CONFIG_SPI_NOR_SFDP_RUNTIME
1274 const struct spi_nor_data *data = dev->data;
1275
1276 *layout = &data->layout;
1277 #else /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1278 const struct spi_nor_config *cfg = dev->config;
1279
1280 *layout = &cfg->layout;
1281 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1282
1283 *layout_size = 1;
1284 }
1285
1286 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1287
1288 static const struct flash_parameters *
flash_nor_get_parameters(const struct device * dev)1289 flash_nor_get_parameters(const struct device *dev)
1290 {
1291 ARG_UNUSED(dev);
1292
1293 return &flash_nor_parameters;
1294 }
1295
1296 static const struct flash_driver_api spi_nor_api = {
1297 .read = spi_nor_read,
1298 .write = spi_nor_write,
1299 .erase = spi_nor_erase,
1300 .get_parameters = flash_nor_get_parameters,
1301 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1302 .page_layout = spi_nor_pages_layout,
1303 #endif
1304 #if defined(CONFIG_FLASH_JESD216_API)
1305 .sfdp_read = spi_nor_sfdp_read,
1306 .read_jedec_id = spi_nor_read_jedec_id,
1307 #endif
1308 };
1309
1310 #ifndef CONFIG_SPI_NOR_SFDP_RUNTIME
1311 /* We need to know the size and ID of the configuration data we're
1312 * using so we can disable the device we see at runtime if it isn't
1313 * compatible with what we're taking from devicetree or minimal.
1314 */
1315 BUILD_ASSERT(DT_INST_NODE_HAS_PROP(0, jedec_id),
1316 "jedec,spi-nor jedec-id required for non-runtime SFDP");
1317
1318 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1319
1320 /* For devicetree or minimal page layout we need to know the size of
1321 * the device. We can't extract it from the raw BFP data, so require
1322 * it to be present in devicetree.
1323 */
1324 BUILD_ASSERT(DT_INST_NODE_HAS_PROP(0, size),
1325 "jedec,spi-nor size required for non-runtime SFDP page layout");
1326
1327 /* instance 0 size in bytes */
1328 #define INST_0_BYTES (DT_INST_PROP(0, size) / 8)
1329
1330 BUILD_ASSERT(SPI_NOR_IS_SECTOR_ALIGNED(CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE),
1331 "SPI_NOR_FLASH_LAYOUT_PAGE_SIZE must be multiple of 4096");
1332
1333 /* instance 0 page count */
1334 #define LAYOUT_PAGES_COUNT (INST_0_BYTES / CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE)
1335
1336 BUILD_ASSERT((CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE * LAYOUT_PAGES_COUNT)
1337 == INST_0_BYTES,
1338 "SPI_NOR_FLASH_LAYOUT_PAGE_SIZE incompatible with flash size");
1339
1340 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1341
1342 #ifdef CONFIG_SPI_NOR_SFDP_DEVICETREE
1343 BUILD_ASSERT(DT_INST_NODE_HAS_PROP(0, sfdp_bfp),
1344 "jedec,spi-nor sfdp-bfp required for devicetree SFDP");
1345
1346 static const __aligned(4) uint8_t bfp_data_0[] = DT_INST_PROP(0, sfdp_bfp);
1347 #endif /* CONFIG_SPI_NOR_SFDP_DEVICETREE */
1348
1349 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1350
1351 #if DT_INST_NODE_HAS_PROP(0, has_lock)
1352 /* Currently we only know of devices where the BP bits are present in
1353 * the first byte of the status register. Complain if that changes.
1354 */
1355 BUILD_ASSERT(DT_INST_PROP(0, has_lock) == (DT_INST_PROP(0, has_lock) & 0xFF),
1356 "Need support for lock clear beyond SR1");
1357 #endif
1358
1359 static const struct spi_nor_config spi_nor_config_0 = {
1360 .spi = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8),
1361 CONFIG_SPI_NOR_CS_WAIT_DELAY),
1362 #if !defined(CONFIG_SPI_NOR_SFDP_RUNTIME)
1363
1364 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
1365 .layout = {
1366 .pages_count = LAYOUT_PAGES_COUNT,
1367 .pages_size = CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE,
1368 },
1369 #undef LAYOUT_PAGES_COUNT
1370 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
1371
1372 .flash_size = DT_INST_PROP(0, size) / 8,
1373 .jedec_id = DT_INST_PROP(0, jedec_id),
1374
1375 #if DT_INST_NODE_HAS_PROP(0, has_lock)
1376 .has_lock = DT_INST_PROP(0, has_lock),
1377 #endif
1378 #if defined(CONFIG_SPI_NOR_SFDP_MINIMAL) \
1379 && DT_INST_NODE_HAS_PROP(0, enter_4byte_addr)
1380 .enter_4byte_addr = DT_INST_PROP(0, enter_4byte_addr),
1381 #endif
1382 #ifdef CONFIG_SPI_NOR_SFDP_DEVICETREE
1383 .bfp_len = sizeof(bfp_data_0) / 4,
1384 .bfp = (const struct jesd216_bfp *)bfp_data_0,
1385 #endif /* CONFIG_SPI_NOR_SFDP_DEVICETREE */
1386
1387 #endif /* CONFIG_SPI_NOR_SFDP_RUNTIME */
1388 };
1389
1390 static struct spi_nor_data spi_nor_data_0;
1391
1392 DEVICE_DT_INST_DEFINE(0, &spi_nor_init, NULL,
1393 &spi_nor_data_0, &spi_nor_config_0,
1394 POST_KERNEL, CONFIG_SPI_NOR_INIT_PRIORITY,
1395 &spi_nor_api);
1396