1 /*
2  * Copyright 2020,2023 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <sys/types.h>
9 #include <fsl_flexspi.h>
10 
11 /* Size of a command in the LUT table */
12 #define MEMC_FLEXSPI_CMD_SIZE 4U
13 /* Number of commands in an instruction sequence */
14 #define MEMC_FLEXSPI_CMD_PER_SEQ 4U
15 
16 /**
17  * @brief Wait for the FlexSPI bus to be idle
18  *
19  * Waits for the FlexSPI bus to be idle. Can be used when reconfiguring
20  * the FlexSPI to make sure no flash access is occurring before changing
21  * settings.
22  *
23  * @param dev: FlexSPI device
24  */
25 void memc_flexspi_wait_bus_idle(const struct device *dev);
26 
27 /**
28  * @brief Check if FlexSPI is being used in XIP mode.
29  *
30  * Checks if the FlexSPI is being used for code execution in the current
31  * application.
32  *
33  * @param dev: FlexSPI device
34  * @retval true if FlexSPI being used for XIP
35  */
36 bool memc_flexspi_is_running_xip(const struct device *dev);
37 
38 /**
39  * @brief Update clock selection of the FlexSPI device
40  *
41  * Updates clock frequency of FlexSPI to new clock speed.
42  *
43  * @param dev: FlexSPI device
44  * @param device_config: External device configuration.
45  * @param port: FlexSPI port to use for this external device
46  * @param freq_hz: new clock frequency to apply
47  * @return 0 on success, negative value on failure
48  */
49 int memc_flexspi_update_clock(const struct device *dev,
50 		flexspi_device_config_t *device_config,
51 		flexspi_port_t port, uint32_t freq_hz);
52 
53 /**
54  * @brief configure new FlexSPI device
55  *
56  * Configures new device on the FlexSPI bus.
57  * @param dev: FlexSPI device
58  * @param device_config: External device configuration.
59  * @param lut_array: Lookup table of FlexSPI flash commands for device
60  * @param lut_count: number of LUT entries (4 bytes each) in lut array
61  * @param port: FlexSPI port to use for this external device
62  * @return 0 on success, negative value on failure
63  */
64 int memc_flexspi_set_device_config(const struct device *dev,
65 		const flexspi_device_config_t *device_config,
66 		const uint32_t *lut_array,
67 		uint8_t lut_count,
68 		flexspi_port_t port);
69 
70 
71 /**
72  * @brief Perform software reset of FlexSPI
73  *
74  * Software reset of FlexSPI. Does not clear configuration registers.
75  * @param dev: FlexSPI device
76  * @return 0 on success, negative value on failure
77  */
78 int memc_flexspi_reset(const struct device *dev);
79 
80 /**
81  * @brief Send blocking IP transfer
82  *
83  * Send blocking IP transfer using FlexSPI.
84  * @param dev: FlexSPI device
85  * @param transfer: FlexSPI transfer. seqIndex should be set  as though the
86  *                  LUT array was loaded at offset 0.
87  * @return 0 on success, negative value on failure
88  */
89 int memc_flexspi_transfer(const struct device *dev,
90 		flexspi_transfer_t *transfer);
91 
92 /**
93  * @brief Get AHB address for FlexSPI port
94  *
95  * Get AHB address for FlexSPI port. This address is memory mapped, and can be
96  * read from (and written to, for PSRAM) as though it were internal memory.
97  * @param dev: FlexSPI device
98  * @param port: FlexSPI port external device is on
99  * @param offset: byte offset from start of device to get AHB address for
100  * @return 0 on success, negative value on failure
101  */
102 void *memc_flexspi_get_ahb_address(const struct device *dev,
103 		flexspi_port_t port, off_t offset);
104