Line data Source code
1 1 : /*
2 : * Copyright 2020 Google LLC
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : #ifndef ZEPHYR_INCLUDE_DRIVERS_MSPI_EMUL_H_
8 : #define ZEPHYR_INCLUDE_DRIVERS_MSPI_EMUL_H_
9 :
10 : #include <zephyr/device.h>
11 : #include <zephyr/drivers/emul.h>
12 : #include <zephyr/drivers/mspi.h>
13 : #include <zephyr/sys/slist.h>
14 : #include <zephyr/types.h>
15 :
16 : /**
17 : * @file
18 : *
19 : * @brief Public APIs for the MSPI emulation drivers.
20 : */
21 :
22 : /**
23 : * @brief MSPI Emulation Interface
24 : * @defgroup mspi_emul_interface MSPI Emulation Interface
25 : * @ingroup io_emulators
26 : * @ingroup mspi_interface
27 : * @{
28 : */
29 :
30 : #ifdef __cplusplus
31 : extern "C" {
32 : #endif
33 :
34 : struct mspi_emul;
35 :
36 : /**
37 : * Find an emulator present on a MSPI bus
38 : *
39 : * At present the function is used only to find an emulator of the host
40 : * device. It may be useful in systems with the SPI flash chips.
41 : *
42 : * @param dev MSPI emulation controller device
43 : * @param dev_idx Device index from device tree.
44 : * @return mspi_emul to use
45 : * @return NULL if not found
46 : */
47 : typedef struct mspi_emul *(*mspi_emul_find_emul)(const struct device *dev,
48 : uint16_t dev_idx);
49 :
50 : /**
51 : * Triggers an event on the emulator of MSPI controller side which causes
52 : * calling specific callbacks.
53 : *
54 : * @param dev MSPI emulation controller device
55 : * @param evt_type Event type to be triggered @see mspi_bus_event
56 : *
57 : * @retval 0 If successful.
58 : * @retval -EIO General input / output error.
59 : */
60 1 : typedef int (*mspi_emul_trigger_event)(const struct device *dev,
61 : enum mspi_bus_event evt_type);
62 :
63 : /**
64 : * Loopback MSPI transceive request to the device emulator
65 : * as no real hardware attached
66 : *
67 : * @param target The device Emulator instance
68 : * @param packets Pointer to the buffers of command, addr, data and etc.
69 : * @param num_packet The number of packets in packets.
70 : * @param async Indicate whether this is a asynchronous request.
71 : * @param timeout Maximum Time allowed for this request
72 : *
73 : * @retval 0 If successful.
74 : * @retval -EIO General input / output error.
75 : */
76 1 : typedef int (*emul_mspi_dev_api_transceive)(const struct emul *target,
77 : const struct mspi_xfer_packet *packets,
78 : uint32_t num_packet,
79 : bool async,
80 : uint32_t timeout);
81 :
82 : /** Definition of the MSPI device emulator API */
83 1 : struct emul_mspi_device_api {
84 0 : emul_mspi_dev_api_transceive transceive;
85 : };
86 :
87 : /** Node in a linked list of emulators for MSPI devices */
88 1 : struct mspi_emul {
89 0 : sys_snode_t node;
90 : /** Target emulator - REQUIRED for all emulated bus nodes of any type */
91 1 : const struct emul *target;
92 : /** API provided for this device */
93 1 : const struct emul_mspi_device_api *api;
94 : /** device index */
95 1 : uint16_t dev_idx;
96 : };
97 :
98 : /** Definition of the MSPI controller emulator API */
99 1 : struct emul_mspi_driver_api {
100 : /* The struct mspi_driver_api has to be first in
101 : * struct emul_mspi_driver_api to make pointer casting working
102 : */
103 0 : struct mspi_driver_api mspi_api;
104 : /* The rest, emulator specific functions */
105 0 : mspi_emul_trigger_event trigger_event;
106 0 : mspi_emul_find_emul find_emul;
107 : };
108 :
109 : /**
110 : * Register an emulated device on the controller
111 : *
112 : * @param dev MSPI emulation controller device
113 : * @param emul MSPI device emulator to be registered
114 : * @return 0 indicating success (always)
115 : */
116 1 : int mspi_emul_register(const struct device *dev, struct mspi_emul *emul);
117 :
118 : #ifdef __cplusplus
119 : }
120 : #endif
121 :
122 : /**
123 : * @}
124 : */
125 :
126 : #endif /* ZEPHYR_INCLUDE_DRIVERS_MSPI_EMUL_H_ */
|