1 /*
2  * Copyright 2020 Google LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * This driver creates fake eSPI buses which can contain emulated devices
7  * (mainly host), implemented by a separate emulation driver.
8  * The API between this driver/controller and device emulators attached
9  * to its bus is defined by struct emul_espi_device_api.
10  */
11 
12 #define DT_DRV_COMPAT zephyr_espi_emul_controller
13 
14 #define LOG_LEVEL CONFIG_ESPI_LOG_LEVEL
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(espi_emul_ctlr);
17 
18 #include <zephyr/device.h>
19 #include <zephyr/drivers/emul.h>
20 #include <zephyr/drivers/espi.h>
21 #include <zephyr/drivers/espi_emul.h>
22 #include "espi_utils.h"
23 
24 /** Working data for the controller */
25 struct espi_emul_data {
26 	/* List of struct espi_emul associated with the device */
27 	sys_slist_t emuls;
28 	/* eSPI host configuration */
29 	struct espi_cfg cfg;
30 	/** List of eSPI callbacks */
31 	sys_slist_t callbacks;
32 };
33 
espi_emul_find(const struct device * dev,unsigned int chipsel)34 static struct espi_emul *espi_emul_find(const struct device *dev, unsigned int chipsel)
35 {
36 	struct espi_emul_data *data = dev->data;
37 	sys_snode_t *node;
38 
39 	SYS_SLIST_FOR_EACH_NODE(&data->emuls, node) {
40 		struct espi_emul *emul;
41 
42 		emul = CONTAINER_OF(node, struct espi_emul, node);
43 		if (emul->chipsel == chipsel) {
44 			return emul;
45 		}
46 	}
47 
48 	return NULL;
49 }
50 
espi_emul_config(const struct device * dev,struct espi_cfg * cfg)51 static int espi_emul_config(const struct device *dev, struct espi_cfg *cfg)
52 {
53 	struct espi_emul_data *data = dev->data;
54 
55 	__ASSERT_NO_MSG(cfg);
56 
57 	data->cfg = *cfg;
58 
59 	return 0;
60 }
61 
emul_espi_trigger_event(const struct device * dev,struct espi_event * evt)62 static int emul_espi_trigger_event(const struct device *dev, struct espi_event *evt)
63 {
64 	struct espi_emul_data *data = dev->data;
65 
66 	if (((evt->evt_type & ESPI_BUS_EVENT_VWIRE_RECEIVED) &&
67 	     !(data->cfg.channel_caps & ESPI_CHANNEL_VWIRE)) ||
68 	    ((evt->evt_type & ESPI_BUS_EVENT_OOB_RECEIVED) &&
69 	     !(data->cfg.channel_caps & ESPI_CHANNEL_OOB)) ||
70 	    ((evt->evt_type & ESPI_BUS_PERIPHERAL_NOTIFICATION) &&
71 	     !(data->cfg.channel_caps & ESPI_CHANNEL_PERIPHERAL))) {
72 		return -EIO;
73 	}
74 
75 	espi_send_callbacks(&data->callbacks, dev, *evt);
76 
77 	return 0;
78 }
79 
espi_emul_get_channel_status(const struct device * dev,enum espi_channel ch)80 static bool espi_emul_get_channel_status(const struct device *dev, enum espi_channel ch)
81 {
82 	struct espi_emul_data *data = dev->data;
83 
84 	return (data->cfg.channel_caps & ch);
85 }
86 
espi_emul_read_lpc_request(const struct device * dev,enum lpc_peripheral_opcode op,uint32_t * data)87 static int espi_emul_read_lpc_request(const struct device *dev, enum lpc_peripheral_opcode op,
88 				      uint32_t *data)
89 {
90 	const struct emul_espi_device_api *api;
91 	struct espi_emul *emul;
92 	struct espi_emul_data *emul_data = dev->data;
93 
94 	ARG_UNUSED(data);
95 
96 	if (!(emul_data->cfg.channel_caps & ESPI_CHANNEL_VWIRE)) {
97 		LOG_ERR("bad channel vwire");
98 		return -EINVAL;
99 	}
100 
101 	emul = espi_emul_find(dev, EMUL_ESPI_HOST_CHIPSEL);
102 	if (!emul) {
103 		LOG_ERR("espi_emul not found");
104 		return -ENOTSUP;
105 	}
106 
107 	__ASSERT_NO_MSG(emul->api);
108 	api = emul->api;
109 
110 	switch (op) {
111 #ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION
112 	case EACPI_GET_SHARED_MEMORY:
113 		__ASSERT_NO_MSG(api->get_acpi_shm);
114 		*data = (uint32_t)api->get_acpi_shm(emul->target);
115 		break;
116 #endif
117 	default:
118 		return -EINVAL;
119 	}
120 	return 0;
121 }
122 
espi_emul_write_lpc_request(const struct device * dev,enum lpc_peripheral_opcode op,uint32_t * data)123 static int espi_emul_write_lpc_request(const struct device *dev, enum lpc_peripheral_opcode op,
124 				       uint32_t *data)
125 {
126 	ARG_UNUSED(dev);
127 	ARG_UNUSED(op);
128 	ARG_UNUSED(data);
129 
130 	return -EINVAL;
131 }
132 
espi_emul_send_vwire(const struct device * dev,enum espi_vwire_signal vw,uint8_t level)133 static int espi_emul_send_vwire(const struct device *dev, enum espi_vwire_signal vw, uint8_t level)
134 {
135 	const struct emul_espi_device_api *api;
136 	struct espi_emul *emul;
137 	struct espi_emul_data *data = dev->data;
138 
139 	if (!(data->cfg.channel_caps & ESPI_CHANNEL_VWIRE)) {
140 		return -EIO;
141 	}
142 
143 	emul = espi_emul_find(dev, EMUL_ESPI_HOST_CHIPSEL);
144 	if (!emul) {
145 		LOG_DBG("espi_emul not found");
146 		return -EIO;
147 	}
148 
149 	__ASSERT_NO_MSG(emul->api);
150 	__ASSERT_NO_MSG(emul->api->set_vw);
151 	api = emul->api;
152 
153 	return api->set_vw(emul->target, vw, level);
154 }
155 
espi_emul_receive_vwire(const struct device * dev,enum espi_vwire_signal vw,uint8_t * level)156 static int espi_emul_receive_vwire(const struct device *dev, enum espi_vwire_signal vw,
157 				   uint8_t *level)
158 {
159 	const struct emul_espi_device_api *api;
160 	struct espi_emul *emul;
161 	struct espi_emul_data *data = dev->data;
162 
163 	if (!(data->cfg.channel_caps & ESPI_CHANNEL_VWIRE)) {
164 		return -EIO;
165 	}
166 
167 	emul = espi_emul_find(dev, EMUL_ESPI_HOST_CHIPSEL);
168 	if (!emul) {
169 		LOG_INF("espi_emul not found");
170 		return -EIO;
171 	}
172 
173 	__ASSERT_NO_MSG(emul->api);
174 	__ASSERT_NO_MSG(emul->api->get_vw);
175 	api = emul->api;
176 
177 	return api->get_vw(emul->target, vw, level);
178 }
179 
espi_emul_manage_callback(const struct device * dev,struct espi_callback * callback,bool set)180 static int espi_emul_manage_callback(const struct device *dev, struct espi_callback *callback,
181 				     bool set)
182 {
183 	struct espi_emul_data *data = dev->data;
184 
185 	return espi_manage_callback(&data->callbacks, callback, set);
186 }
187 
188 /**
189  * Set up a new emulator and add it to the list
190  *
191  * @param dev eSPI emulation controller device
192  */
espi_emul_init(const struct device * dev)193 static int espi_emul_init(const struct device *dev)
194 {
195 	struct espi_emul_data *data = dev->data;
196 
197 	sys_slist_init(&data->emuls);
198 
199 	return emul_init_for_bus(dev);
200 }
201 
espi_emul_register(const struct device * dev,struct espi_emul * emul)202 int espi_emul_register(const struct device *dev, struct espi_emul *emul)
203 {
204 	struct espi_emul_data *data = dev->data;
205 	const char *name = emul->target->dev->name;
206 
207 	sys_slist_append(&data->emuls, &emul->node);
208 
209 	LOG_INF("Register emulator '%s' at cs %u\n", name, emul->chipsel);
210 
211 	return 0;
212 }
213 
214 /* Device instantiation */
215 static struct emul_espi_driver_api emul_espi_driver_api = {
216 	.espi_api = {
217 		.config = espi_emul_config,
218 		.get_channel_status = espi_emul_get_channel_status,
219 		.read_lpc_request = espi_emul_read_lpc_request,
220 		.write_lpc_request = espi_emul_write_lpc_request,
221 		.send_vwire = espi_emul_send_vwire,
222 		.receive_vwire = espi_emul_receive_vwire,
223 		.manage_callback = espi_emul_manage_callback
224 	},
225 	.trigger_event = emul_espi_trigger_event,
226 	.find_emul = espi_emul_find,
227 };
228 
229 #define EMUL_LINK_AND_COMMA(node_id)                                                               \
230 	{                                                                                          \
231 		.dev = DEVICE_DT_GET(node_id),                                                     \
232 	},
233 
234 #define ESPI_EMUL_INIT(n)                                                                          \
235 	static const struct emul_link_for_bus emuls_##n[] = {                                      \
236 		DT_FOREACH_CHILD_STATUS_OKAY(DT_DRV_INST(n), EMUL_LINK_AND_COMMA)};                \
237 	static struct emul_list_for_bus espi_emul_cfg_##n = {                                      \
238 		.children = emuls_##n,                                                             \
239 		.num_children = ARRAY_SIZE(emuls_##n),                                             \
240 	};                                                                                         \
241 	static struct espi_emul_data espi_emul_data_##n;                                           \
242 	DEVICE_DT_INST_DEFINE(n, &espi_emul_init, NULL, &espi_emul_data_##n, &espi_emul_cfg_##n,   \
243 			      POST_KERNEL, CONFIG_ESPI_INIT_PRIORITY, &emul_espi_driver_api);
244 
245 DT_INST_FOREACH_STATUS_OKAY(ESPI_EMUL_INIT)
246