1 /*
2  * Copyright (c) 2021 ITE Corporation. All Rights Reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT ite_it8xxx2_espi
8 
9 #include <assert.h>
10 #include <zephyr/drivers/espi.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/drivers/interrupt_controller/wuc_ite_it8xxx2.h>
13 #include <zephyr/kernel.h>
14 #include <soc.h>
15 #include <soc_dt.h>
16 #include "soc_espi.h"
17 #include "espi_utils.h"
18 
19 #include <zephyr/logging/log.h>
20 #include <zephyr/irq.h>
21 LOG_MODULE_REGISTER(espi, CONFIG_ESPI_LOG_LEVEL);
22 
23 #define ESPI_IT8XXX2_GET_GCTRL_BASE \
24 	((struct gctrl_it8xxx2_regs *)DT_REG_ADDR(DT_NODELABEL(gctrl)))
25 
26 #define IT8XXX2_ESPI_IRQ     DT_INST_IRQ_BY_IDX(0, 0, irq)
27 #define IT8XXX2_ESPI_VW_IRQ  DT_INST_IRQ_BY_IDX(0, 1, irq)
28 #define IT8XXX2_KBC_IBF_IRQ  DT_INST_IRQ_BY_IDX(0, 2, irq)
29 #define IT8XXX2_KBC_OBE_IRQ  DT_INST_IRQ_BY_IDX(0, 3, irq)
30 #define IT8XXX2_PMC1_IBF_IRQ DT_INST_IRQ_BY_IDX(0, 4, irq)
31 #define IT8XXX2_PORT_80_IRQ  DT_INST_IRQ_BY_IDX(0, 5, irq)
32 #define IT8XXX2_PMC2_IBF_IRQ DT_INST_IRQ_BY_IDX(0, 6, irq)
33 #define IT8XXX2_TRANS_IRQ    DT_INST_IRQ_BY_IDX(0, 7, irq)
34 
35 /* General Capabilities and Configuration 1 */
36 #define IT8XXX2_ESPI_MAX_FREQ_MASK GENMASK(2, 0)
37 #define IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_20 0
38 #define IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_25 1
39 #define IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_33 2
40 #define IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_50 3
41 #define IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_66 4
42 
43 #define IT8XXX2_ESPI_PC_READY_MASK  BIT(1)
44 #define IT8XXX2_ESPI_VW_READY_MASK  BIT(1)
45 #define IT8XXX2_ESPI_OOB_READY_MASK BIT(1)
46 #define IT8XXX2_ESPI_FC_READY_MASK  BIT(1)
47 
48 #define IT8XXX2_ESPI_INTERRUPT_ENABLE          BIT(7)
49 #define IT8XXX2_ESPI_TO_WUC_ENABLE             BIT(4)
50 #define IT8XXX2_ESPI_VW_INTERRUPT_ENABLE       BIT(7)
51 #define IT8XXX2_ESPI_INTERRUPT_PUT_PC          BIT(7)
52 
53 /*
54  * VWCTRL2 register:
55  * bit4 = 1b: Refers to ESPI_RESET# for PLTRST#.
56  */
57 #define IT8XXX2_ESPI_VW_RESET_PLTRST           BIT(4)
58 
59 #define IT8XXX2_ESPI_UPSTREAM_ENABLE           BIT(7)
60 #define IT8XXX2_ESPI_UPSTREAM_GO               BIT(6)
61 #define IT8XXX2_ESPI_UPSTREAM_INTERRUPT_ENABLE BIT(5)
62 #define IT8XXX2_ESPI_UPSTREAM_CHANNEL_DISABLE  BIT(2)
63 #define IT8XXX2_ESPI_UPSTREAM_DONE             BIT(1)
64 #define IT8XXX2_ESPI_UPSTREAM_BUSY             BIT(0)
65 
66 #define IT8XXX2_ESPI_CYCLE_TYPE_OOB            0x07
67 
68 #define IT8XXX2_ESPI_PUT_OOB_STATUS            BIT(7)
69 #define IT8XXX2_ESPI_PUT_OOB_INTERRUPT_ENABLE  BIT(7)
70 #define IT8XXX2_ESPI_PUT_OOB_LEN_MASK          GENMASK(6, 0)
71 
72 #define IT8XXX2_ESPI_INPUT_PAD_GATING          BIT(6)
73 
74 #define IT8XXX2_ESPI_FLASH_MAX_PAYLOAD_SIZE    64
75 #define IT8XXX2_ESPI_PUT_FLASH_TAG_MASK        GENMASK(7, 4)
76 #define IT8XXX2_ESPI_PUT_FLASH_LEN_MASK        GENMASK(6, 0)
77 
78 struct espi_it8xxx2_wuc {
79 	/* WUC control device structure */
80 	const struct device *wucs;
81 	/* WUC pin mask */
82 	uint8_t mask;
83 };
84 
85 struct espi_it8xxx2_config {
86 	uintptr_t base_espi_slave;
87 	uintptr_t base_espi_vw;
88 	uintptr_t base_espi_queue1;
89 	uintptr_t base_espi_queue0;
90 	uintptr_t base_ec2i;
91 	uintptr_t base_kbc;
92 	uintptr_t base_pmc;
93 	uintptr_t base_smfi;
94 	const struct espi_it8xxx2_wuc wuc;
95 };
96 
97 struct espi_it8xxx2_data {
98 	sys_slist_t callbacks;
99 #ifdef CONFIG_ESPI_OOB_CHANNEL
100 	struct k_sem oob_upstream_go;
101 #endif
102 #ifdef CONFIG_ESPI_FLASH_CHANNEL
103 	struct k_sem flash_upstream_go;
104 	uint8_t put_flash_cycle_type;
105 	uint8_t put_flash_tag;
106 	uint8_t put_flash_len;
107 	uint8_t flash_buf[IT8XXX2_ESPI_FLASH_MAX_PAYLOAD_SIZE];
108 #endif
109 };
110 
111 struct vw_channel_t {
112 	uint8_t  vw_index;      /* VW index of signal */
113 	uint8_t  level_mask;    /* level bit of signal */
114 	uint8_t  valid_mask;    /* valid bit of signal */
115 };
116 
117 struct vwidx_isr_t {
118 	void (*vwidx_isr)(const struct device *dev, uint8_t update_flag);
119 	uint8_t vw_index;
120 };
121 
122 enum espi_ch_enable_isr_type {
123 	DEASSERTED_FLAG = 0,
124 	ASSERTED_FLAG = 1,
125 };
126 
127 struct espi_isr_t {
128 	void (*espi_isr)(const struct device *dev, bool enable);
129 	enum espi_ch_enable_isr_type isr_type;
130 };
131 
132 struct espi_vw_signal_t {
133 	enum espi_vwire_signal signal;
134 	void (*vw_signal_isr)(const struct device *dev);
135 };
136 
137 /* EC2I bridge and PNPCFG devices */
138 static const struct ec2i_t kbc_settings[] = {
139 	/* Select logical device 06h(keyboard) */
140 	{HOST_INDEX_LDN, LDN_KBC_KEYBOARD},
141 	/* Set IRQ=01h for logical device */
142 	{HOST_INDEX_IRQNUMX, 0x01},
143 	/* Configure IRQTP for KBC. */
144 	/*
145 	 * Interrupt request type select (IRQTP) for KBC.
146 	 * bit 1, 0: IRQ request is buffered and applied to SERIRQ
147 	 *        1: IRQ request is inverted before being applied to SERIRQ
148 	 * bit 0, 0: Edge triggered mode
149 	 *        1: Level triggered mode
150 	 *
151 	 * This interrupt configuration should the same on both host and EC side
152 	 */
153 	{HOST_INDEX_IRQTP, 0x02},
154 	/* Enable logical device */
155 	{HOST_INDEX_LDA, 0x01},
156 
157 #ifdef CONFIG_ESPI_IT8XXX2_PNPCFG_DEVICE_KBC_MOUSE
158 	/* Select logical device 05h(mouse) */
159 	{HOST_INDEX_LDN, LDN_KBC_MOUSE},
160 	/* Set IRQ=0Ch for logical device */
161 	{HOST_INDEX_IRQNUMX, 0x0C},
162 	/* Enable logical device */
163 	{HOST_INDEX_LDA, 0x01},
164 #endif
165 };
166 
167 static const struct ec2i_t pmc1_settings[] = {
168 	/* Select logical device 11h(PM1 ACPI) */
169 	{HOST_INDEX_LDN, LDN_PMC1},
170 	/* Set IRQ=00h for logical device */
171 	{HOST_INDEX_IRQNUMX, 0x00},
172 	/* Enable logical device */
173 	{HOST_INDEX_LDA, 0x01},
174 };
175 
176 #ifdef CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD
177 #define IT8XXX2_ESPI_HC_DATA_PORT_MSB \
178 	((CONFIG_ESPI_PERIPHERAL_HOST_CMD_DATA_PORT_NUM >> 8) & 0xff)
179 #define IT8XXX2_ESPI_HC_DATA_PORT_LSB \
180 	(CONFIG_ESPI_PERIPHERAL_HOST_CMD_DATA_PORT_NUM & 0xff)
181 #define IT8XXX2_ESPI_HC_CMD_PORT_MSB \
182 	(((CONFIG_ESPI_PERIPHERAL_HOST_CMD_DATA_PORT_NUM + 4) >> 8) & 0xff)
183 #define IT8XXX2_ESPI_HC_CMD_PORT_LSB \
184 	((CONFIG_ESPI_PERIPHERAL_HOST_CMD_DATA_PORT_NUM + 4) & 0xff)
185 static const struct ec2i_t pmc2_settings[] = {
186 	/* Select logical device 12h(PM2 host command) */
187 	{HOST_INDEX_LDN, LDN_PMC2},
188 	/* I/O Port Base Address (data/command ports) */
189 	{HOST_INDEX_IOBAD0_MSB, IT8XXX2_ESPI_HC_DATA_PORT_MSB},
190 	{HOST_INDEX_IOBAD0_LSB, IT8XXX2_ESPI_HC_DATA_PORT_LSB},
191 	{HOST_INDEX_IOBAD1_MSB, IT8XXX2_ESPI_HC_CMD_PORT_MSB},
192 	{HOST_INDEX_IOBAD1_LSB, IT8XXX2_ESPI_HC_CMD_PORT_LSB},
193 	/* Set IRQ=00h for logical device */
194 	{HOST_INDEX_IRQNUMX, 0x00},
195 	/* Enable logical device */
196 	{HOST_INDEX_LDA, 0x01},
197 };
198 #endif
199 
200 #if defined(CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD) || \
201 	defined(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION)
202 /*
203  * Host to RAM (H2RAM) memory mapping.
204  * This feature allows host access EC's memory directly by eSPI I/O cycles.
205  * Mapping range is 4K bytes and base address is adjustable.
206  * Eg. the I/O cycle 800h~8ffh from host can be mapped to x800h~x8ffh.
207  * Linker script will make the pool 4K aligned.
208  */
209 #define IT8XXX2_ESPI_H2RAM_POOL_SIZE_MAX 0x1000
210 #define IT8XXX2_ESPI_H2RAM_OFFSET_MASK   GENMASK(5, 0)
211 
212 #if defined(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION)
213 #define H2RAM_ACPI_SHM_MAX ((CONFIG_ESPI_IT8XXX2_ACPI_SHM_H2RAM_SIZE) + \
214 			(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION_PORT_NUM))
215 #if (H2RAM_ACPI_SHM_MAX > IT8XXX2_ESPI_H2RAM_POOL_SIZE_MAX)
216 #error "ACPI shared memory region out of h2ram"
217 #endif
218 #endif /* CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION */
219 
220 #if defined(CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD)
221 #define H2RAM_EC_HOST_CMD_MAX ((CONFIG_ESPI_IT8XXX2_HC_H2RAM_SIZE) + \
222 			(CONFIG_ESPI_PERIPHERAL_HOST_CMD_PARAM_PORT_NUM))
223 #if (H2RAM_EC_HOST_CMD_MAX > IT8XXX2_ESPI_H2RAM_POOL_SIZE_MAX)
224 #error "EC host command parameters out of h2ram"
225 #endif
226 #endif /* CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD */
227 
228 #if defined(CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD) && \
229 	defined(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION)
230 #if (MIN(H2RAM_ACPI_SHM_MAX, H2RAM_EC_HOST_CMD_MAX) > \
231 	MAX(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION_PORT_NUM, \
232 		CONFIG_ESPI_PERIPHERAL_HOST_CMD_PARAM_PORT_NUM))
233 #error "ACPI and HC sections of h2ram overlap"
234 #endif
235 #endif
236 
237 static uint8_t h2ram_pool[MAX(H2RAM_ACPI_SHM_MAX, H2RAM_EC_HOST_CMD_MAX)]
238 					__attribute__((section(".h2ram_pool")));
239 
240 #define H2RAM_WINDOW_SIZE(ram_size) ((find_msb_set((ram_size) / 16) - 1) & 0x7)
241 
242 static const struct ec2i_t smfi_settings[] = {
243 	/* Select logical device 0Fh(SMFI) */
244 	{HOST_INDEX_LDN, LDN_SMFI},
245 	/* Internal RAM base address on eSPI I/O space */
246 	{HOST_INDEX_DSLDC6, 0x00},
247 	/* Enable H2RAM eSPI I/O cycle */
248 	{HOST_INDEX_DSLDC7, 0x01},
249 	/* Enable logical device */
250 	{HOST_INDEX_LDA, 0x01},
251 };
252 
smfi_it8xxx2_init(const struct device * dev)253 static void smfi_it8xxx2_init(const struct device *dev)
254 {
255 	const struct espi_it8xxx2_config *const config = dev->config;
256 	struct smfi_it8xxx2_regs *const smfi_reg =
257 		(struct smfi_it8xxx2_regs *)config->base_smfi;
258 	struct gctrl_it8xxx2_regs *const gctrl = ESPI_IT8XXX2_GET_GCTRL_BASE;
259 	uint8_t h2ram_offset;
260 
261 	/* Set the host to RAM cycle address offset */
262 	h2ram_offset = ((uint32_t)h2ram_pool & 0xffff) /
263 				IT8XXX2_ESPI_H2RAM_POOL_SIZE_MAX;
264 	gctrl->GCTRL_H2ROFSR =
265 		(gctrl->GCTRL_H2ROFSR & ~IT8XXX2_ESPI_H2RAM_OFFSET_MASK) |
266 		h2ram_offset;
267 
268 #ifdef CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD
269 	memset(&h2ram_pool[CONFIG_ESPI_PERIPHERAL_HOST_CMD_PARAM_PORT_NUM], 0,
270 			CONFIG_ESPI_IT8XXX2_HC_H2RAM_SIZE);
271 	/* Set host RAM window 0 base address */
272 	smfi_reg->SMFI_HRAMW0BA =
273 		(CONFIG_ESPI_PERIPHERAL_HOST_CMD_PARAM_PORT_NUM >> 4) & 0xff;
274 	/* Set host RAM window 0 size. (allow R/W) */
275 	smfi_reg->SMFI_HRAMW0AAS =
276 		H2RAM_WINDOW_SIZE(CONFIG_ESPI_IT8XXX2_HC_H2RAM_SIZE);
277 	/* Enable window 0, H2RAM through IO cycle */
278 	smfi_reg->SMFI_HRAMWC |= (SMFI_H2RAMPS | SMFI_H2RAMW0E);
279 #endif
280 
281 #ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION
282 	memset(&h2ram_pool[CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION_PORT_NUM], 0,
283 			CONFIG_ESPI_IT8XXX2_ACPI_SHM_H2RAM_SIZE);
284 	/* Set host RAM window 1 base address */
285 	smfi_reg->SMFI_HRAMW1BA =
286 		(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION_PORT_NUM >> 4) & 0xff;
287 	/* Set host RAM window 1 size. (read-only) */
288 	smfi_reg->SMFI_HRAMW1AAS =
289 		H2RAM_WINDOW_SIZE(CONFIG_ESPI_IT8XXX2_ACPI_SHM_H2RAM_SIZE) |
290 		SMFI_HRAMWXWPE_ALL;
291 	/* Enable window 1, H2RAM through IO cycle */
292 	smfi_reg->SMFI_HRAMWC |= (SMFI_H2RAMPS | SMFI_H2RAMW1E);
293 #endif
294 }
295 #endif /* CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD ||
296 	* CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION
297 	*/
298 
ec2i_it8xxx2_wait_status_cleared(const struct device * dev,uint8_t mask)299 static void ec2i_it8xxx2_wait_status_cleared(const struct device *dev,
300 						uint8_t mask)
301 {
302 	const struct espi_it8xxx2_config *const config = dev->config;
303 	struct ec2i_regs *const ec2i = (struct ec2i_regs *)config->base_ec2i;
304 
305 	while (ec2i->IBCTL & mask) {
306 		;
307 	}
308 }
309 
ec2i_it8xxx2_write_pnpcfg(const struct device * dev,enum ec2i_access sel,uint8_t data)310 static void ec2i_it8xxx2_write_pnpcfg(const struct device *dev,
311 					enum ec2i_access sel, uint8_t data)
312 {
313 	const struct espi_it8xxx2_config *const config = dev->config;
314 	struct ec2i_regs *const ec2i = (struct ec2i_regs *)config->base_ec2i;
315 
316 	/* bit0: EC to I-Bus access enabled. */
317 	ec2i->IBCTL |= EC2I_IBCTL_CSAE;
318 	/*
319 	 * Wait that both CRIB and CWIB bits in IBCTL register
320 	 * are cleared.
321 	 */
322 	ec2i_it8xxx2_wait_status_cleared(dev, EC2I_IBCTL_CRWIB);
323 	/* Enable EC access to the PNPCFG registers */
324 	ec2i->IBMAE |= EC2I_IBMAE_CFGAE;
325 	/* Set indirect host I/O offset. */
326 	ec2i->IHIOA = sel;
327 	/* Write the data to IHD register */
328 	ec2i->IHD = data;
329 	/* Wait the CWIB bit in IBCTL cleared. */
330 	ec2i_it8xxx2_wait_status_cleared(dev, EC2I_IBCTL_CWIB);
331 	/* Disable EC access to the PNPCFG registers. */
332 	ec2i->IBMAE &= ~EC2I_IBMAE_CFGAE;
333 	/* Disable EC to I-Bus access. */
334 	ec2i->IBCTL &= ~EC2I_IBCTL_CSAE;
335 }
336 
ec2i_it8xxx2_write(const struct device * dev,enum host_pnpcfg_index index,uint8_t data)337 static void ec2i_it8xxx2_write(const struct device *dev,
338 				enum host_pnpcfg_index index, uint8_t data)
339 {
340 	/* Set index */
341 	ec2i_it8xxx2_write_pnpcfg(dev, EC2I_ACCESS_INDEX, index);
342 	/* Set data */
343 	ec2i_it8xxx2_write_pnpcfg(dev, EC2I_ACCESS_DATA, data);
344 }
345 
pnpcfg_it8xxx2_configure(const struct device * dev,const struct ec2i_t * settings,size_t entries)346 static void pnpcfg_it8xxx2_configure(const struct device *dev,
347 					const struct ec2i_t *settings,
348 					size_t entries)
349 {
350 	for (size_t i = 0; i < entries; i++) {
351 		ec2i_it8xxx2_write(dev, settings[i].index_port,
352 					settings[i].data_port);
353 	}
354 }
355 
356 #define PNPCFG(_s)						\
357 	pnpcfg_it8xxx2_configure(dev, _s##_settings, ARRAY_SIZE(_s##_settings))
358 
pnpcfg_it8xxx2_init(const struct device * dev)359 static void pnpcfg_it8xxx2_init(const struct device *dev)
360 {
361 	const struct espi_it8xxx2_config *const config = dev->config;
362 	struct ec2i_regs *const ec2i = (struct ec2i_regs *)config->base_ec2i;
363 	struct gctrl_it8xxx2_regs *const gctrl = ESPI_IT8XXX2_GET_GCTRL_BASE;
364 
365 	/* The register pair to access PNPCFG is 004Eh and 004Fh */
366 	gctrl->GCTRL_BADRSEL = 0x1;
367 	/* Host access is disabled */
368 	ec2i->LSIOHA |= 0x3;
369 	/* configure pnpcfg devices */
370 	if (IS_ENABLED(CONFIG_ESPI_PERIPHERAL_8042_KBC)) {
371 		PNPCFG(kbc);
372 	}
373 	if (IS_ENABLED(CONFIG_ESPI_PERIPHERAL_HOST_IO)) {
374 		PNPCFG(pmc1);
375 	}
376 #ifdef CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD
377 	PNPCFG(pmc2);
378 #endif
379 #if defined(CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD) || \
380 	defined(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION)
381 	PNPCFG(smfi);
382 #endif
383 }
384 
385 /* KBC (port 60h/64h) */
386 #ifdef CONFIG_ESPI_PERIPHERAL_8042_KBC
kbc_it8xxx2_ibf_isr(const struct device * dev)387 static void kbc_it8xxx2_ibf_isr(const struct device *dev)
388 {
389 	const struct espi_it8xxx2_config *const config = dev->config;
390 	struct espi_it8xxx2_data *const data = dev->data;
391 	struct kbc_regs *const kbc_reg = (struct kbc_regs *)config->base_kbc;
392 	struct espi_event evt = {
393 		ESPI_BUS_PERIPHERAL_NOTIFICATION,
394 		ESPI_PERIPHERAL_8042_KBC,
395 		ESPI_PERIPHERAL_NODATA
396 	};
397 	struct espi_evt_data_kbc *kbc_evt =
398 			(struct espi_evt_data_kbc *)&evt.evt_data;
399 
400 	/* KBC Input Buffer Full event */
401 	kbc_evt->evt = HOST_KBC_EVT_IBF;
402 	/*
403 	 * Indicates if the host sent a command or data.
404 	 * 0 = data
405 	 * 1 = Command.
406 	 */
407 	kbc_evt->type = !!(kbc_reg->KBHISR & KBC_KBHISR_A2_ADDR);
408 	/* The data in KBC Input Buffer */
409 	kbc_evt->data = kbc_reg->KBHIDIR;
410 
411 	espi_send_callbacks(&data->callbacks, dev, evt);
412 }
413 
kbc_it8xxx2_obe_isr(const struct device * dev)414 static void kbc_it8xxx2_obe_isr(const struct device *dev)
415 {
416 	const struct espi_it8xxx2_config *const config = dev->config;
417 	struct espi_it8xxx2_data *const data = dev->data;
418 	struct kbc_regs *const kbc_reg = (struct kbc_regs *)config->base_kbc;
419 	struct espi_event evt = {
420 		ESPI_BUS_PERIPHERAL_NOTIFICATION,
421 		ESPI_PERIPHERAL_8042_KBC,
422 		ESPI_PERIPHERAL_NODATA
423 	};
424 	struct espi_evt_data_kbc *kbc_evt =
425 				(struct espi_evt_data_kbc *)&evt.evt_data;
426 
427 	/* Disable KBC OBE interrupt first */
428 	kbc_reg->KBHICR &= ~KBC_KBHICR_OBECIE;
429 
430 	/* Notify application that host already read out data. */
431 	kbc_evt->evt = HOST_KBC_EVT_OBE;
432 	kbc_evt->data = 0;
433 	kbc_evt->type = 0;
434 	espi_send_callbacks(&data->callbacks, dev, evt);
435 }
436 
kbc_it8xxx2_init(const struct device * dev)437 static void kbc_it8xxx2_init(const struct device *dev)
438 {
439 	const struct espi_it8xxx2_config *const config = dev->config;
440 	struct kbc_regs *const kbc_reg = (struct kbc_regs *)config->base_kbc;
441 
442 	/* Disable KBC serirq IRQ */
443 	kbc_reg->KBIRQR = 0;
444 
445 	/*
446 	 * bit3: Input Buffer Full CPU Interrupt Enable.
447 	 * bit1: Enable the interrupt to mouse driver in the host processor via
448 	 *       SERIRQ when the output buffer is full.
449 	 * bit0: Enable the interrupt to keyboard driver in the host processor
450 	 *       via SERIRQ when the output buffer is full
451 	 */
452 	kbc_reg->KBHICR |=
453 		(KBC_KBHICR_IBFCIE | KBC_KBHICR_OBFKIE | KBC_KBHICR_OBFMIE);
454 
455 	/* Input Buffer Full CPU Interrupt Enable. */
456 	IRQ_CONNECT(IT8XXX2_KBC_IBF_IRQ, 0, kbc_it8xxx2_ibf_isr,
457 			DEVICE_DT_INST_GET(0), 0);
458 	irq_enable(IT8XXX2_KBC_IBF_IRQ);
459 
460 	/* Output Buffer Empty CPU Interrupt Enable */
461 	IRQ_CONNECT(IT8XXX2_KBC_OBE_IRQ, 0, kbc_it8xxx2_obe_isr,
462 			DEVICE_DT_INST_GET(0), 0);
463 	irq_enable(IT8XXX2_KBC_OBE_IRQ);
464 }
465 #endif
466 
467 /* PMC 1 (APCI port 62h/66h) */
468 #ifdef CONFIG_ESPI_PERIPHERAL_HOST_IO
pmc1_it8xxx2_ibf_isr(const struct device * dev)469 static void pmc1_it8xxx2_ibf_isr(const struct device *dev)
470 {
471 	const struct espi_it8xxx2_config *const config = dev->config;
472 	struct espi_it8xxx2_data *const data = dev->data;
473 	struct pmc_regs *const pmc_reg = (struct pmc_regs *)config->base_pmc;
474 	struct espi_event evt = {
475 		ESPI_BUS_PERIPHERAL_NOTIFICATION,
476 		ESPI_PERIPHERAL_HOST_IO,
477 		ESPI_PERIPHERAL_NODATA
478 	};
479 	struct espi_evt_data_acpi *acpi_evt =
480 				(struct espi_evt_data_acpi *)&evt.evt_data;
481 
482 	/*
483 	 * Indicates if the host sent a command or data.
484 	 * 0 = data
485 	 * 1 = Command.
486 	 */
487 	acpi_evt->type = !!(pmc_reg->PM1STS & PMC_PM1STS_A2_ADDR);
488 	/* Set processing flag before reading command byte */
489 	pmc_reg->PM1STS |= PMC_PM1STS_GPF;
490 	acpi_evt->data = pmc_reg->PM1DI;
491 
492 	espi_send_callbacks(&data->callbacks, dev, evt);
493 }
494 
pmc1_it8xxx2_init(const struct device * dev)495 static void pmc1_it8xxx2_init(const struct device *dev)
496 {
497 	const struct espi_it8xxx2_config *const config = dev->config;
498 	struct pmc_regs *const pmc_reg = (struct pmc_regs *)config->base_pmc;
499 
500 	/* Enable pmc1 input buffer full interrupt */
501 	pmc_reg->PM1CTL |= PMC_PM1CTL_IBFIE;
502 	IRQ_CONNECT(IT8XXX2_PMC1_IBF_IRQ, 0, pmc1_it8xxx2_ibf_isr,
503 			DEVICE_DT_INST_GET(0), 0);
504 	irq_enable(IT8XXX2_PMC1_IBF_IRQ);
505 }
506 #endif
507 
508 /* Port 80 */
509 #ifdef CONFIG_ESPI_PERIPHERAL_DEBUG_PORT_80
port80_it8xxx2_isr(const struct device * dev)510 static void port80_it8xxx2_isr(const struct device *dev)
511 {
512 	struct espi_it8xxx2_data *const data = dev->data;
513 	struct gctrl_it8xxx2_regs *const gctrl = ESPI_IT8XXX2_GET_GCTRL_BASE;
514 	struct espi_event evt = {
515 		ESPI_BUS_PERIPHERAL_NOTIFICATION,
516 		(ESPI_PERIPHERAL_INDEX_0 << 16) | ESPI_PERIPHERAL_DEBUG_PORT80,
517 		ESPI_PERIPHERAL_NODATA
518 	};
519 
520 	if (IS_ENABLED(CONFIG_ESPI_IT8XXX2_PORT_81_CYCLE)) {
521 		evt.evt_data = gctrl->GCTRL_P80HDR | (gctrl->GCTRL_P81HDR << 8);
522 	} else {
523 		evt.evt_data = gctrl->GCTRL_P80HDR;
524 	}
525 	/* Write 1 to clear this bit */
526 	gctrl->GCTRL_P80H81HSR |= BIT(0);
527 
528 	espi_send_callbacks(&data->callbacks, dev, evt);
529 }
530 
port80_it8xxx2_init(const struct device * dev)531 static void port80_it8xxx2_init(const struct device *dev)
532 {
533 	ARG_UNUSED(dev);
534 	struct gctrl_it8xxx2_regs *const gctrl = ESPI_IT8XXX2_GET_GCTRL_BASE;
535 
536 	/* Accept Port 80h (and 81h) Cycle */
537 	if (IS_ENABLED(CONFIG_ESPI_IT8XXX2_PORT_81_CYCLE)) {
538 		gctrl->GCTRL_SPCTRL1 |=
539 			(IT8XXX2_GCTRL_ACP80 | IT8XXX2_GCTRL_ACP81);
540 	} else {
541 		gctrl->GCTRL_SPCTRL1 |= IT8XXX2_GCTRL_ACP80;
542 	}
543 	IRQ_CONNECT(IT8XXX2_PORT_80_IRQ, 0, port80_it8xxx2_isr,
544 			DEVICE_DT_INST_GET(0), 0);
545 	irq_enable(IT8XXX2_PORT_80_IRQ);
546 }
547 #endif
548 
549 #ifdef CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD
550 /* PMC 2 (Host command port CONFIG_ESPI_PERIPHERAL_HOST_CMD_DATA_PORT_NUM) */
pmc2_it8xxx2_ibf_isr(const struct device * dev)551 static void pmc2_it8xxx2_ibf_isr(const struct device *dev)
552 {
553 	const struct espi_it8xxx2_config *const config = dev->config;
554 	struct espi_it8xxx2_data *const data = dev->data;
555 	struct pmc_regs *const pmc_reg = (struct pmc_regs *)config->base_pmc;
556 	struct espi_event evt = {
557 		ESPI_BUS_PERIPHERAL_NOTIFICATION,
558 		ESPI_PERIPHERAL_EC_HOST_CMD,
559 		ESPI_PERIPHERAL_NODATA
560 	};
561 
562 	/* Set processing flag before reading command byte */
563 	pmc_reg->PM2STS |= PMC_PM2STS_GPF;
564 	evt.evt_data = pmc_reg->PM2DI;
565 
566 	espi_send_callbacks(&data->callbacks, dev, evt);
567 }
568 
pmc2_it8xxx2_init(const struct device * dev)569 static void pmc2_it8xxx2_init(const struct device *dev)
570 {
571 	const struct espi_it8xxx2_config *const config = dev->config;
572 	struct pmc_regs *const pmc_reg = (struct pmc_regs *)config->base_pmc;
573 
574 	/* Dedicated interrupt for PMC2 */
575 	pmc_reg->MBXCTRL |= PMC_MBXCTRL_DINT;
576 	/* Enable pmc2 input buffer full interrupt */
577 	pmc_reg->PM2CTL |= PMC_PM2CTL_IBFIE;
578 	IRQ_CONNECT(IT8XXX2_PMC2_IBF_IRQ, 0, pmc2_it8xxx2_ibf_isr,
579 			DEVICE_DT_INST_GET(0), 0);
580 	irq_enable(IT8XXX2_PMC2_IBF_IRQ);
581 }
582 #endif
583 
584 /* eSPI api functions */
585 #define VW_CHAN(signal, index, level, valid) \
586 	[signal] = {.vw_index = index, .level_mask = level, .valid_mask = valid}
587 
588 /* VW signals used in eSPI */
589 static const struct vw_channel_t vw_channel_list[] = {
590 	VW_CHAN(ESPI_VWIRE_SIGNAL_SLP_S3,        0x02, BIT(0), BIT(4)),
591 	VW_CHAN(ESPI_VWIRE_SIGNAL_SLP_S4,        0x02, BIT(1), BIT(5)),
592 	VW_CHAN(ESPI_VWIRE_SIGNAL_SLP_S5,        0x02, BIT(2), BIT(6)),
593 	VW_CHAN(ESPI_VWIRE_SIGNAL_OOB_RST_WARN,  0x03, BIT(2), BIT(6)),
594 	VW_CHAN(ESPI_VWIRE_SIGNAL_PLTRST,        0x03, BIT(1), BIT(5)),
595 	VW_CHAN(ESPI_VWIRE_SIGNAL_SUS_STAT,      0x03, BIT(0), BIT(4)),
596 	VW_CHAN(ESPI_VWIRE_SIGNAL_NMIOUT,        0x07, BIT(2), BIT(6)),
597 	VW_CHAN(ESPI_VWIRE_SIGNAL_SMIOUT,        0x07, BIT(1), BIT(5)),
598 	VW_CHAN(ESPI_VWIRE_SIGNAL_HOST_RST_WARN, 0x07, BIT(0), BIT(4)),
599 	VW_CHAN(ESPI_VWIRE_SIGNAL_SLP_A,         0x41, BIT(3), BIT(7)),
600 	VW_CHAN(ESPI_VWIRE_SIGNAL_SUS_PWRDN_ACK, 0x41, BIT(1), BIT(5)),
601 	VW_CHAN(ESPI_VWIRE_SIGNAL_SUS_WARN,      0x41, BIT(0), BIT(4)),
602 	VW_CHAN(ESPI_VWIRE_SIGNAL_SLP_WLAN,      0x42, BIT(1), BIT(5)),
603 	VW_CHAN(ESPI_VWIRE_SIGNAL_SLP_LAN,       0x42, BIT(0), BIT(4)),
604 	VW_CHAN(ESPI_VWIRE_SIGNAL_HOST_C10,      0x47, BIT(0), BIT(4)),
605 	VW_CHAN(ESPI_VWIRE_SIGNAL_DNX_WARN,      0x4a, BIT(1), BIT(5)),
606 	VW_CHAN(ESPI_VWIRE_SIGNAL_PME,           0x04, BIT(3), BIT(7)),
607 	VW_CHAN(ESPI_VWIRE_SIGNAL_WAKE,          0x04, BIT(2), BIT(6)),
608 	VW_CHAN(ESPI_VWIRE_SIGNAL_OOB_RST_ACK,   0x04, BIT(0), BIT(4)),
609 	VW_CHAN(ESPI_VWIRE_SIGNAL_TARGET_BOOT_STS,  0x05, BIT(3), BIT(7)),
610 	VW_CHAN(ESPI_VWIRE_SIGNAL_ERR_NON_FATAL, 0x05, BIT(2), BIT(6)),
611 	VW_CHAN(ESPI_VWIRE_SIGNAL_ERR_FATAL,     0x05, BIT(1), BIT(5)),
612 	VW_CHAN(ESPI_VWIRE_SIGNAL_TARGET_BOOT_DONE, 0x05, BIT(0), BIT(4)),
613 	VW_CHAN(ESPI_VWIRE_SIGNAL_HOST_RST_ACK,  0x06, BIT(3), BIT(7)),
614 	VW_CHAN(ESPI_VWIRE_SIGNAL_RST_CPU_INIT,  0x06, BIT(2), BIT(6)),
615 	VW_CHAN(ESPI_VWIRE_SIGNAL_SMI,           0x06, BIT(1), BIT(5)),
616 	VW_CHAN(ESPI_VWIRE_SIGNAL_SCI,           0x06, BIT(0), BIT(4)),
617 	VW_CHAN(ESPI_VWIRE_SIGNAL_DNX_ACK,       0x40, BIT(1), BIT(5)),
618 	VW_CHAN(ESPI_VWIRE_SIGNAL_SUS_ACK,       0x40, BIT(0), BIT(4)),
619 };
620 
espi_it8xxx2_configure(const struct device * dev,struct espi_cfg * cfg)621 static int espi_it8xxx2_configure(const struct device *dev,
622 					struct espi_cfg *cfg)
623 {
624 	const struct espi_it8xxx2_config *const config = dev->config;
625 	struct espi_slave_regs *const slave_reg =
626 		(struct espi_slave_regs *)config->base_espi_slave;
627 	uint8_t capcfg1 = 0;
628 
629 	/* Set frequency */
630 	switch (cfg->max_freq) {
631 	case 20:
632 		capcfg1 = IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_20;
633 		break;
634 	case 25:
635 		capcfg1 = IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_25;
636 		break;
637 	case 33:
638 		capcfg1 = IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_33;
639 		break;
640 	case 50:
641 		capcfg1 = IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_50;
642 		break;
643 	case 66:
644 		capcfg1 = IT8XXX2_ESPI_CAPCFG1_MAX_FREQ_66;
645 		break;
646 	default:
647 		return -EINVAL;
648 	}
649 	slave_reg->GCAPCFG1 =
650 		(slave_reg->GCAPCFG1 & ~IT8XXX2_ESPI_MAX_FREQ_MASK) | capcfg1;
651 
652 	/*
653 	 * Configure eSPI I/O mode. (Register read only)
654 	 * Supported I/O mode : single, dual and quad.
655 	 */
656 
657 	/* Configure eSPI supported channels. (Register read only)
658 	 * Supported channels: peripheral, virtual wire, OOB, and flash access.
659 	 */
660 
661 	return 0;
662 }
663 
espi_it8xxx2_channel_ready(const struct device * dev,enum espi_channel ch)664 static bool espi_it8xxx2_channel_ready(const struct device *dev,
665 					enum espi_channel ch)
666 {
667 	const struct espi_it8xxx2_config *const config = dev->config;
668 	struct espi_slave_regs *const slave_reg =
669 		(struct espi_slave_regs *)config->base_espi_slave;
670 	bool sts = false;
671 
672 	switch (ch) {
673 	case ESPI_CHANNEL_PERIPHERAL:
674 		sts = slave_reg->CH_PC_CAPCFG3 & IT8XXX2_ESPI_PC_READY_MASK;
675 		break;
676 	case ESPI_CHANNEL_VWIRE:
677 		sts = slave_reg->CH_VW_CAPCFG3 & IT8XXX2_ESPI_VW_READY_MASK;
678 		break;
679 	case ESPI_CHANNEL_OOB:
680 		sts = slave_reg->CH_OOB_CAPCFG3 & IT8XXX2_ESPI_OOB_READY_MASK;
681 		break;
682 	case ESPI_CHANNEL_FLASH:
683 		sts = slave_reg->CH_FLASH_CAPCFG3 & IT8XXX2_ESPI_FC_READY_MASK;
684 		break;
685 	default:
686 		break;
687 	}
688 
689 	return sts;
690 }
691 
espi_it8xxx2_send_vwire(const struct device * dev,enum espi_vwire_signal signal,uint8_t level)692 static int espi_it8xxx2_send_vwire(const struct device *dev,
693 			enum espi_vwire_signal signal, uint8_t level)
694 {
695 	const struct espi_it8xxx2_config *const config = dev->config;
696 	struct espi_vw_regs *const vw_reg =
697 		(struct espi_vw_regs *)config->base_espi_vw;
698 	uint8_t vw_index = vw_channel_list[signal].vw_index;
699 	uint8_t level_mask = vw_channel_list[signal].level_mask;
700 	uint8_t valid_mask = vw_channel_list[signal].valid_mask;
701 
702 	if (signal > ARRAY_SIZE(vw_channel_list)) {
703 		return -EIO;
704 	}
705 
706 	if (level) {
707 		vw_reg->VW_INDEX[vw_index] |= level_mask;
708 	} else {
709 		vw_reg->VW_INDEX[vw_index] &= ~level_mask;
710 	}
711 
712 	vw_reg->VW_INDEX[vw_index] |= valid_mask;
713 
714 	return 0;
715 }
716 
espi_it8xxx2_receive_vwire(const struct device * dev,enum espi_vwire_signal signal,uint8_t * level)717 static int espi_it8xxx2_receive_vwire(const struct device *dev,
718 				  enum espi_vwire_signal signal, uint8_t *level)
719 {
720 	const struct espi_it8xxx2_config *const config = dev->config;
721 	struct espi_vw_regs *const vw_reg =
722 		(struct espi_vw_regs *)config->base_espi_vw;
723 	uint8_t vw_index = vw_channel_list[signal].vw_index;
724 	uint8_t level_mask = vw_channel_list[signal].level_mask;
725 	uint8_t valid_mask = vw_channel_list[signal].valid_mask;
726 
727 	if (signal > ARRAY_SIZE(vw_channel_list)) {
728 		return -EIO;
729 	}
730 
731 	if (vw_reg->VW_INDEX[vw_index] & valid_mask) {
732 		*level = !!(vw_reg->VW_INDEX[vw_index] & level_mask);
733 	} else {
734 		/* Not valid */
735 		*level = 0;
736 	}
737 
738 	return 0;
739 }
740 
espi_it8xxx2_manage_callback(const struct device * dev,struct espi_callback * callback,bool set)741 static int espi_it8xxx2_manage_callback(const struct device *dev,
742 				    struct espi_callback *callback, bool set)
743 {
744 	struct espi_it8xxx2_data *const data = dev->data;
745 
746 	return espi_manage_callback(&data->callbacks, callback, set);
747 }
748 
espi_it8xxx2_read_lpc_request(const struct device * dev,enum lpc_peripheral_opcode op,uint32_t * data)749 static int espi_it8xxx2_read_lpc_request(const struct device *dev,
750 				     enum lpc_peripheral_opcode op,
751 				     uint32_t *data)
752 {
753 	const struct espi_it8xxx2_config *const config = dev->config;
754 
755 	if (op >= E8042_START_OPCODE && op <= E8042_MAX_OPCODE) {
756 		struct kbc_regs *const kbc_reg =
757 			(struct kbc_regs *)config->base_kbc;
758 
759 		switch (op) {
760 		case E8042_OBF_HAS_CHAR:
761 			/* EC has written data back to host. OBF is
762 			 * automatically cleared after host reads
763 			 * the data
764 			 */
765 			*data = !!(kbc_reg->KBHISR & KBC_KBHISR_OBF);
766 			break;
767 		case E8042_IBF_HAS_CHAR:
768 			*data = !!(kbc_reg->KBHISR & KBC_KBHISR_IBF);
769 			break;
770 		case E8042_READ_KB_STS:
771 			*data = kbc_reg->KBHISR;
772 			break;
773 		default:
774 			return -EINVAL;
775 		}
776 	} else if (op >= EACPI_START_OPCODE && op <= EACPI_MAX_OPCODE) {
777 		struct pmc_regs *const pmc_reg =
778 			(struct pmc_regs *)config->base_pmc;
779 
780 		switch (op) {
781 		case EACPI_OBF_HAS_CHAR:
782 			/* EC has written data back to host. OBF is
783 			 * automatically cleared after host reads
784 			 * the data
785 			 */
786 			*data = !!(pmc_reg->PM1STS & PMC_PM1STS_OBF);
787 			break;
788 		case EACPI_IBF_HAS_CHAR:
789 			*data = !!(pmc_reg->PM1STS & PMC_PM1STS_IBF);
790 			break;
791 		case EACPI_READ_STS:
792 			*data = pmc_reg->PM1STS;
793 			break;
794 #ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION
795 		case EACPI_GET_SHARED_MEMORY:
796 			*data = (uint32_t)&h2ram_pool[
797 			CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION_PORT_NUM];
798 			break;
799 #endif /* CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION */
800 		default:
801 			return -EINVAL;
802 		}
803 	}
804 #ifdef CONFIG_ESPI_PERIPHERAL_CUSTOM_OPCODE
805 	else if (op >= ECUSTOM_START_OPCODE && op <= ECUSTOM_MAX_OPCODE) {
806 
807 		switch (op) {
808 		case ECUSTOM_HOST_CMD_GET_PARAM_MEMORY:
809 			*data = (uint32_t)&h2ram_pool[
810 				CONFIG_ESPI_PERIPHERAL_HOST_CMD_PARAM_PORT_NUM];
811 			break;
812 		case ECUSTOM_HOST_CMD_GET_PARAM_MEMORY_SIZE:
813 			*data = CONFIG_ESPI_IT8XXX2_HC_H2RAM_SIZE;
814 			break;
815 		default:
816 			return -EINVAL;
817 		}
818 	}
819 #endif /* CONFIG_ESPI_PERIPHERAL_CUSTOM_OPCODE */
820 	else {
821 		return -ENOTSUP;
822 	}
823 
824 	return 0;
825 }
826 
espi_it8xxx2_write_lpc_request(const struct device * dev,enum lpc_peripheral_opcode op,uint32_t * data)827 static int espi_it8xxx2_write_lpc_request(const struct device *dev,
828 				      enum lpc_peripheral_opcode op,
829 				      uint32_t *data)
830 {
831 	const struct espi_it8xxx2_config *const config = dev->config;
832 
833 	if (op >= E8042_START_OPCODE && op <= E8042_MAX_OPCODE) {
834 		struct kbc_regs *const kbc_reg =
835 			(struct kbc_regs *)config->base_kbc;
836 
837 		switch (op) {
838 		case E8042_WRITE_KB_CHAR:
839 			kbc_reg->KBHIKDOR = (*data & 0xff);
840 			/*
841 			 * Enable OBE interrupt after putting data in
842 			 * data register.
843 			 */
844 			kbc_reg->KBHICR |= KBC_KBHICR_OBECIE;
845 			break;
846 		case E8042_WRITE_MB_CHAR:
847 			kbc_reg->KBHIMDOR = (*data & 0xff);
848 			/*
849 			 * Enable OBE interrupt after putting data in
850 			 * data register.
851 			 */
852 			kbc_reg->KBHICR |= KBC_KBHICR_OBECIE;
853 			break;
854 		case E8042_RESUME_IRQ:
855 			/* Enable KBC IBF interrupt */
856 			kbc_reg->KBHICR |= KBC_KBHICR_IBFCIE;
857 			break;
858 		case E8042_PAUSE_IRQ:
859 			/* Disable KBC IBF interrupt */
860 			kbc_reg->KBHICR &= ~KBC_KBHICR_IBFCIE;
861 			break;
862 		case E8042_CLEAR_OBF:
863 			/*
864 			 * After enabling IBF/OBF clear mode, we have to make
865 			 * sure that IBF interrupt is not triggered before
866 			 * disabling the clear mode. Or the interrupt will keep
867 			 * triggering until the watchdog is reset.
868 			 */
869 			unsigned int key = irq_lock();
870 			/*
871 			 * When IBFOBFCME is enabled, write 1 to COBF bit to
872 			 * clear KBC OBF.
873 			 */
874 			kbc_reg->KBHICR |= KBC_KBHICR_IBFOBFCME;
875 			kbc_reg->KBHICR |= KBC_KBHICR_COBF;
876 			kbc_reg->KBHICR &= ~KBC_KBHICR_COBF;
877 			/* Disable clear mode */
878 			kbc_reg->KBHICR &= ~KBC_KBHICR_IBFOBFCME;
879 			irq_unlock(key);
880 			break;
881 		case E8042_SET_FLAG:
882 			kbc_reg->KBHISR |= (*data & 0xff);
883 			break;
884 		case E8042_CLEAR_FLAG:
885 			kbc_reg->KBHISR &= ~(*data & 0xff);
886 			break;
887 		default:
888 			return -EINVAL;
889 		}
890 	} else if (op >= EACPI_START_OPCODE && op <= EACPI_MAX_OPCODE) {
891 		struct pmc_regs *const pmc_reg =
892 			(struct pmc_regs *)config->base_pmc;
893 
894 		switch (op) {
895 		case EACPI_WRITE_CHAR:
896 			pmc_reg->PM1DO = (*data & 0xff);
897 			break;
898 		case EACPI_WRITE_STS:
899 			pmc_reg->PM1STS = (*data & 0xff);
900 			break;
901 		default:
902 			return -EINVAL;
903 		}
904 	}
905 #ifdef CONFIG_ESPI_PERIPHERAL_CUSTOM_OPCODE
906 	else if (op >= ECUSTOM_START_OPCODE && op <= ECUSTOM_MAX_OPCODE) {
907 		struct pmc_regs *const pmc_reg =
908 			(struct pmc_regs *)config->base_pmc;
909 
910 		switch (op) {
911 		/* Enable/Disable PMC1 (port 62h/66h) interrupt */
912 		case ECUSTOM_HOST_SUBS_INTERRUPT_EN:
913 			if (*data) {
914 				pmc_reg->PM1CTL |= PMC_PM1CTL_IBFIE;
915 			} else {
916 				pmc_reg->PM1CTL &= ~PMC_PM1CTL_IBFIE;
917 			}
918 			break;
919 		case ECUSTOM_HOST_CMD_SEND_RESULT:
920 			/* Write result to data output port (set OBF status) */
921 			pmc_reg->PM2DO = (*data & 0xff);
922 			/* Clear processing flag */
923 			pmc_reg->PM2STS &= ~PMC_PM2STS_GPF;
924 			break;
925 		default:
926 			return -EINVAL;
927 		}
928 	}
929 #endif /* CONFIG_ESPI_PERIPHERAL_CUSTOM_OPCODE */
930 	else {
931 		return -ENOTSUP;
932 	}
933 
934 	return 0;
935 }
936 
937 #ifdef CONFIG_ESPI_OOB_CHANNEL
938 /* eSPI cycle type field */
939 #define ESPI_OOB_CYCLE_TYPE          0x21
940 #define ESPI_OOB_TAG                 0x00
941 #define ESPI_OOB_TIMEOUT_MS          200
942 
943 /* eSPI tag + len[11:8] field */
944 #define ESPI_TAG_LEN_FIELD(tag, len) \
945 		   ((((tag) & 0xF) << 4) | (((len) >> 8) & 0xF))
946 
947 struct espi_oob_msg_packet {
948 	uint8_t data_byte[0];
949 };
950 
espi_it8xxx2_send_oob(const struct device * dev,struct espi_oob_packet * pckt)951 static int espi_it8xxx2_send_oob(const struct device *dev,
952 				struct espi_oob_packet *pckt)
953 {
954 	const struct espi_it8xxx2_config *const config = dev->config;
955 	struct espi_slave_regs *const slave_reg =
956 		(struct espi_slave_regs *)config->base_espi_slave;
957 	struct espi_queue1_regs *const queue1_reg =
958 		(struct espi_queue1_regs *)config->base_espi_queue1;
959 	struct espi_oob_msg_packet *oob_pckt =
960 		(struct espi_oob_msg_packet *)pckt->buf;
961 
962 	if (!(slave_reg->CH_OOB_CAPCFG3 & IT8XXX2_ESPI_OOB_READY_MASK)) {
963 		LOG_ERR("%s: OOB channel isn't ready", __func__);
964 		return -EIO;
965 	}
966 
967 	if (slave_reg->ESUCTRL0 & IT8XXX2_ESPI_UPSTREAM_BUSY) {
968 		LOG_ERR("%s: OOB upstream busy", __func__);
969 		return -EIO;
970 	}
971 
972 	if (pckt->len > ESPI_IT8XXX2_OOB_MAX_PAYLOAD_SIZE) {
973 		LOG_ERR("%s: Out of OOB queue space", __func__);
974 		return -EINVAL;
975 	}
976 
977 	/* Set cycle type */
978 	slave_reg->ESUCTRL1 = IT8XXX2_ESPI_CYCLE_TYPE_OOB;
979 	/* Set tag and length[11:8] */
980 	slave_reg->ESUCTRL2 = ESPI_TAG_LEN_FIELD(0, pckt->len);
981 	/* Set length [7:0] */
982 	slave_reg->ESUCTRL3 = pckt->len & 0xff;
983 
984 	/* Set data byte */
985 	for (int i = 0; i < pckt->len; i++) {
986 		queue1_reg->UPSTREAM_DATA[i] = oob_pckt->data_byte[i];
987 	}
988 
989 	/* Set upstream enable */
990 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_ENABLE;
991 	/* Set upstream go */
992 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_GO;
993 
994 	return 0;
995 }
996 
espi_it8xxx2_receive_oob(const struct device * dev,struct espi_oob_packet * pckt)997 static int espi_it8xxx2_receive_oob(const struct device *dev,
998 				struct espi_oob_packet *pckt)
999 {
1000 	const struct espi_it8xxx2_config *const config = dev->config;
1001 	struct espi_slave_regs *const slave_reg =
1002 		(struct espi_slave_regs *)config->base_espi_slave;
1003 	struct espi_queue0_regs *const queue0_reg =
1004 		(struct espi_queue0_regs *)config->base_espi_queue0;
1005 	struct espi_oob_msg_packet *oob_pckt =
1006 		(struct espi_oob_msg_packet *)pckt->buf;
1007 	uint8_t oob_len;
1008 
1009 	if (!(slave_reg->CH_OOB_CAPCFG3 & IT8XXX2_ESPI_OOB_READY_MASK)) {
1010 		LOG_ERR("%s: OOB channel isn't ready", __func__);
1011 		return -EIO;
1012 	}
1013 
1014 #ifndef CONFIG_ESPI_OOB_CHANNEL_RX_ASYNC
1015 	struct espi_it8xxx2_data *const data = dev->data;
1016 	int ret;
1017 
1018 	/* Wait until receive OOB message or timeout */
1019 	ret = k_sem_take(&data->oob_upstream_go, K_MSEC(ESPI_OOB_TIMEOUT_MS));
1020 	if (ret == -EAGAIN) {
1021 		LOG_ERR("%s: Timeout", __func__);
1022 		return -ETIMEDOUT;
1023 	}
1024 #endif
1025 
1026 	/* Get length */
1027 	oob_len = (slave_reg->ESOCTRL4 & IT8XXX2_ESPI_PUT_OOB_LEN_MASK);
1028 	/*
1029 	 * Buffer passed to driver isn't enough.
1030 	 * The first three bytes of buffer are cycle type, tag, and length.
1031 	 */
1032 	if (oob_len > pckt->len) {
1033 		LOG_ERR("%s: Out of rx buf %d vs %d", __func__,
1034 			oob_len, pckt->len);
1035 		return -EINVAL;
1036 	}
1037 
1038 	pckt->len = oob_len;
1039 	/* Get data byte */
1040 	for (int i = 0; i < oob_len; i++) {
1041 		oob_pckt->data_byte[i] = queue0_reg->PUT_OOB_DATA[i];
1042 	}
1043 
1044 	return 0;
1045 }
1046 
espi_it8xxx2_oob_init(const struct device * dev)1047 static void espi_it8xxx2_oob_init(const struct device *dev)
1048 {
1049 	const struct espi_it8xxx2_config *const config = dev->config;
1050 	struct espi_slave_regs *const slave_reg =
1051 		(struct espi_slave_regs *)config->base_espi_slave;
1052 
1053 #ifndef CONFIG_ESPI_OOB_CHANNEL_RX_ASYNC
1054 	struct espi_it8xxx2_data *const data = dev->data;
1055 
1056 	k_sem_init(&data->oob_upstream_go, 0, 1);
1057 #endif
1058 
1059 	/* Upstream interrupt enable */
1060 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_INTERRUPT_ENABLE;
1061 
1062 	/* PUT_OOB interrupt enable */
1063 	slave_reg->ESOCTRL1 |= IT8XXX2_ESPI_PUT_OOB_INTERRUPT_ENABLE;
1064 }
1065 #endif
1066 
1067 #ifdef CONFIG_ESPI_FLASH_CHANNEL
1068 #define ESPI_FLASH_TAG                      0x01
1069 #define ESPI_FLASH_READ_TIMEOUT_MS          200
1070 #define ESPI_FLASH_WRITE_TIMEOUT_MS         500
1071 #define ESPI_FLASH_ERASE_TIMEOUT_MS         1000
1072 
1073 /* Successful completion without data */
1074 #define ESPI_IT8XXX2_PUT_FLASH_C_SCWOD      0
1075 /* Successful completion with data */
1076 #define ESPI_IT8XXX2_PUT_FLASH_C_SCWD       4
1077 
1078 enum espi_flash_cycle_type {
1079 	IT8XXX2_ESPI_CYCLE_TYPE_FLASH_READ = 0x08,
1080 	IT8XXX2_ESPI_CYCLE_TYPE_FLASH_WRITE = 0x09,
1081 	IT8XXX2_ESPI_CYCLE_TYPE_FLASH_ERASE = 0x0A,
1082 };
1083 
espi_it8xxx2_flash_trans(const struct device * dev,struct espi_flash_packet * pckt,enum espi_flash_cycle_type tran)1084 static int espi_it8xxx2_flash_trans(const struct device *dev,
1085 				struct espi_flash_packet *pckt,
1086 				enum espi_flash_cycle_type tran)
1087 {
1088 	const struct espi_it8xxx2_config *const config = dev->config;
1089 	struct espi_slave_regs *const slave_reg =
1090 		(struct espi_slave_regs *)config->base_espi_slave;
1091 	struct espi_queue1_regs *const queue1_reg =
1092 		(struct espi_queue1_regs *)config->base_espi_queue1;
1093 
1094 	if (!(slave_reg->CH_FLASH_CAPCFG3 & IT8XXX2_ESPI_FC_READY_MASK)) {
1095 		LOG_ERR("%s: Flash channel isn't ready (tran:%d)",
1096 			__func__, tran);
1097 		return -EIO;
1098 	}
1099 
1100 	if (slave_reg->ESUCTRL0 & IT8XXX2_ESPI_UPSTREAM_BUSY) {
1101 		LOG_ERR("%s: Upstream busy (tran:%d)", __func__, tran);
1102 		return -EIO;
1103 	}
1104 
1105 	if (pckt->len > IT8XXX2_ESPI_FLASH_MAX_PAYLOAD_SIZE) {
1106 		LOG_ERR("%s: Invalid size request (tran:%d)", __func__, tran);
1107 		return -EINVAL;
1108 	}
1109 
1110 	/* Set cycle type */
1111 	slave_reg->ESUCTRL1 = tran;
1112 	/* Set tag and length[11:8] */
1113 	slave_reg->ESUCTRL2 = (ESPI_FLASH_TAG << 4);
1114 	/*
1115 	 * Set length [7:0]
1116 	 * Note: for erasing, the least significant 3 bit of the length field
1117 	 * specifies the size of the block to be erased:
1118 	 * 001b: 4 Kbytes
1119 	 * 010b: 64Kbytes
1120 	 * 100b: 128 Kbytes
1121 	 * 101b: 256 Kbytes
1122 	 */
1123 	slave_reg->ESUCTRL3 = pckt->len;
1124 	/* Set flash address */
1125 	queue1_reg->UPSTREAM_DATA[0] = (pckt->flash_addr >> 24) & 0xff;
1126 	queue1_reg->UPSTREAM_DATA[1] = (pckt->flash_addr >> 16) & 0xff;
1127 	queue1_reg->UPSTREAM_DATA[2] = (pckt->flash_addr >> 8) & 0xff;
1128 	queue1_reg->UPSTREAM_DATA[3] = pckt->flash_addr & 0xff;
1129 
1130 	return 0;
1131 }
1132 
espi_it8xxx2_flash_read(const struct device * dev,struct espi_flash_packet * pckt)1133 static int espi_it8xxx2_flash_read(const struct device *dev,
1134 					struct espi_flash_packet *pckt)
1135 {
1136 	const struct espi_it8xxx2_config *const config = dev->config;
1137 	struct espi_it8xxx2_data *const data = dev->data;
1138 	struct espi_slave_regs *const slave_reg =
1139 		(struct espi_slave_regs *)config->base_espi_slave;
1140 	int ret;
1141 
1142 	ret = espi_it8xxx2_flash_trans(dev, pckt,
1143 					IT8XXX2_ESPI_CYCLE_TYPE_FLASH_READ);
1144 	if (ret) {
1145 		return ret;
1146 	}
1147 
1148 	/* Set upstream enable */
1149 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_ENABLE;
1150 	/* Set upstream go */
1151 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_GO;
1152 
1153 	/* Wait until upstream done or timeout */
1154 	ret = k_sem_take(&data->flash_upstream_go,
1155 			K_MSEC(ESPI_FLASH_READ_TIMEOUT_MS));
1156 	if (ret == -EAGAIN) {
1157 		LOG_ERR("%s: Timeout", __func__);
1158 		return -ETIMEDOUT;
1159 	}
1160 
1161 	if (data->put_flash_cycle_type != ESPI_IT8XXX2_PUT_FLASH_C_SCWD) {
1162 		LOG_ERR("%s: Unsuccessful completion", __func__);
1163 		return -EIO;
1164 	}
1165 
1166 	memcpy(pckt->buf, data->flash_buf, pckt->len);
1167 
1168 	LOG_INF("%s: read (%d) bytes from flash over espi", __func__,
1169 		data->put_flash_len);
1170 
1171 	return 0;
1172 }
1173 
espi_it8xxx2_flash_write(const struct device * dev,struct espi_flash_packet * pckt)1174 static int espi_it8xxx2_flash_write(const struct device *dev,
1175 					struct espi_flash_packet *pckt)
1176 {
1177 	const struct espi_it8xxx2_config *const config = dev->config;
1178 	struct espi_it8xxx2_data *const data = dev->data;
1179 	struct espi_slave_regs *const slave_reg =
1180 		(struct espi_slave_regs *)config->base_espi_slave;
1181 	struct espi_queue1_regs *const queue1_reg =
1182 		(struct espi_queue1_regs *)config->base_espi_queue1;
1183 	int ret;
1184 
1185 	ret = espi_it8xxx2_flash_trans(dev, pckt,
1186 					IT8XXX2_ESPI_CYCLE_TYPE_FLASH_WRITE);
1187 	if (ret) {
1188 		return ret;
1189 	}
1190 
1191 	/* Set data byte */
1192 	for (int i = 0; i < pckt->len; i++) {
1193 		queue1_reg->UPSTREAM_DATA[4 + i] = pckt->buf[i];
1194 	}
1195 
1196 	/* Set upstream enable */
1197 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_ENABLE;
1198 	/* Set upstream go */
1199 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_GO;
1200 
1201 	/* Wait until upstream done or timeout */
1202 	ret = k_sem_take(&data->flash_upstream_go,
1203 			K_MSEC(ESPI_FLASH_WRITE_TIMEOUT_MS));
1204 	if (ret == -EAGAIN) {
1205 		LOG_ERR("%s: Timeout", __func__);
1206 		return -ETIMEDOUT;
1207 	}
1208 
1209 	if (data->put_flash_cycle_type != ESPI_IT8XXX2_PUT_FLASH_C_SCWOD) {
1210 		LOG_ERR("%s: Unsuccessful completion", __func__);
1211 		return -EIO;
1212 	}
1213 
1214 	return 0;
1215 }
1216 
espi_it8xxx2_flash_erase(const struct device * dev,struct espi_flash_packet * pckt)1217 static int espi_it8xxx2_flash_erase(const struct device *dev,
1218 					struct espi_flash_packet *pckt)
1219 {
1220 	const struct espi_it8xxx2_config *const config = dev->config;
1221 	struct espi_it8xxx2_data *const data = dev->data;
1222 	struct espi_slave_regs *const slave_reg =
1223 		(struct espi_slave_regs *)config->base_espi_slave;
1224 	int ret;
1225 
1226 	ret = espi_it8xxx2_flash_trans(dev, pckt,
1227 					IT8XXX2_ESPI_CYCLE_TYPE_FLASH_ERASE);
1228 	if (ret) {
1229 		return ret;
1230 	}
1231 
1232 	/* Set upstream enable */
1233 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_ENABLE;
1234 	/* Set upstream go */
1235 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_GO;
1236 
1237 	/* Wait until upstream done or timeout */
1238 	ret = k_sem_take(&data->flash_upstream_go,
1239 			K_MSEC(ESPI_FLASH_ERASE_TIMEOUT_MS));
1240 	if (ret == -EAGAIN) {
1241 		LOG_ERR("%s: Timeout", __func__);
1242 		return -ETIMEDOUT;
1243 	}
1244 
1245 	if (data->put_flash_cycle_type != ESPI_IT8XXX2_PUT_FLASH_C_SCWOD) {
1246 		LOG_ERR("%s: Unsuccessful completion", __func__);
1247 		return -EIO;
1248 	}
1249 
1250 	return 0;
1251 }
1252 
espi_it8xxx2_flash_upstream_done_isr(const struct device * dev)1253 static void espi_it8xxx2_flash_upstream_done_isr(const struct device *dev)
1254 {
1255 	const struct espi_it8xxx2_config *const config = dev->config;
1256 	struct espi_it8xxx2_data *const data = dev->data;
1257 	struct espi_slave_regs *const slave_reg =
1258 		(struct espi_slave_regs *)config->base_espi_slave;
1259 	struct espi_queue1_regs *const queue1_reg =
1260 		(struct espi_queue1_regs *)config->base_espi_queue1;
1261 
1262 	data->put_flash_cycle_type = slave_reg->ESUCTRL6;
1263 	data->put_flash_tag = slave_reg->ESUCTRL7 &
1264 				IT8XXX2_ESPI_PUT_FLASH_TAG_MASK;
1265 	data->put_flash_len = slave_reg->ESUCTRL8 &
1266 				IT8XXX2_ESPI_PUT_FLASH_LEN_MASK;
1267 
1268 	if (slave_reg->ESUCTRL1 == IT8XXX2_ESPI_CYCLE_TYPE_FLASH_READ) {
1269 		if (data->put_flash_len > IT8XXX2_ESPI_FLASH_MAX_PAYLOAD_SIZE) {
1270 			LOG_ERR("%s: Invalid size (%d)", __func__,
1271 							data->put_flash_len);
1272 		} else {
1273 			for (int i = 0; i < data->put_flash_len; i++) {
1274 				data->flash_buf[i] =
1275 					queue1_reg->UPSTREAM_DATA[i];
1276 			}
1277 		}
1278 	}
1279 
1280 	k_sem_give(&data->flash_upstream_go);
1281 }
1282 
espi_it8xxx2_flash_init(const struct device * dev)1283 static void espi_it8xxx2_flash_init(const struct device *dev)
1284 {
1285 	const struct espi_it8xxx2_config *const config = dev->config;
1286 	struct espi_it8xxx2_data *const data = dev->data;
1287 	struct espi_slave_regs *const slave_reg =
1288 		(struct espi_slave_regs *)config->base_espi_slave;
1289 
1290 	k_sem_init(&data->flash_upstream_go, 0, 1);
1291 
1292 	/* Upstream interrupt enable */
1293 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_INTERRUPT_ENABLE;
1294 }
1295 #endif /* CONFIG_ESPI_FLASH_CHANNEL */
1296 
1297 /* eSPI driver registration */
1298 static int espi_it8xxx2_init(const struct device *dev);
1299 
1300 static const struct espi_driver_api espi_it8xxx2_driver_api = {
1301 	.config = espi_it8xxx2_configure,
1302 	.get_channel_status = espi_it8xxx2_channel_ready,
1303 	.send_vwire = espi_it8xxx2_send_vwire,
1304 	.receive_vwire = espi_it8xxx2_receive_vwire,
1305 	.manage_callback = espi_it8xxx2_manage_callback,
1306 	.read_lpc_request = espi_it8xxx2_read_lpc_request,
1307 	.write_lpc_request = espi_it8xxx2_write_lpc_request,
1308 #ifdef CONFIG_ESPI_OOB_CHANNEL
1309 	.send_oob = espi_it8xxx2_send_oob,
1310 	.receive_oob = espi_it8xxx2_receive_oob,
1311 #endif
1312 #ifdef CONFIG_ESPI_FLASH_CHANNEL
1313 	.flash_read = espi_it8xxx2_flash_read,
1314 	.flash_write = espi_it8xxx2_flash_write,
1315 	.flash_erase = espi_it8xxx2_flash_erase,
1316 #endif
1317 };
1318 
espi_it8xxx2_vw_notify_system_state(const struct device * dev,enum espi_vwire_signal signal)1319 static void espi_it8xxx2_vw_notify_system_state(const struct device *dev,
1320 				enum espi_vwire_signal signal)
1321 {
1322 	struct espi_it8xxx2_data *const data = dev->data;
1323 	struct espi_event evt = {ESPI_BUS_EVENT_VWIRE_RECEIVED, 0, 0};
1324 	uint8_t level = 0;
1325 
1326 	espi_it8xxx2_receive_vwire(dev, signal, &level);
1327 
1328 	evt.evt_details = signal;
1329 	evt.evt_data = level;
1330 	espi_send_callbacks(&data->callbacks, dev, evt);
1331 }
1332 
espi_vw_signal_no_isr(const struct device * dev)1333 static void espi_vw_signal_no_isr(const struct device *dev)
1334 {
1335 	ARG_UNUSED(dev);
1336 }
1337 
1338 static const struct espi_vw_signal_t vwidx2_signals[] = {
1339 	{ESPI_VWIRE_SIGNAL_SLP_S3, NULL},
1340 	{ESPI_VWIRE_SIGNAL_SLP_S4, NULL},
1341 	{ESPI_VWIRE_SIGNAL_SLP_S5, NULL},
1342 };
1343 
espi_it8xxx2_vwidx2_isr(const struct device * dev,uint8_t updated_flag)1344 static void espi_it8xxx2_vwidx2_isr(const struct device *dev,
1345 					uint8_t updated_flag)
1346 {
1347 	for (int i = 0; i < ARRAY_SIZE(vwidx2_signals); i++) {
1348 		enum espi_vwire_signal vw_signal = vwidx2_signals[i].signal;
1349 
1350 		if (updated_flag & vw_channel_list[vw_signal].level_mask) {
1351 			espi_it8xxx2_vw_notify_system_state(dev, vw_signal);
1352 		}
1353 	}
1354 }
1355 
espi_vw_oob_rst_warn_isr(const struct device * dev)1356 static void espi_vw_oob_rst_warn_isr(const struct device *dev)
1357 {
1358 	uint8_t level = 0;
1359 
1360 	espi_it8xxx2_receive_vwire(dev, ESPI_VWIRE_SIGNAL_OOB_RST_WARN, &level);
1361 	espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_OOB_RST_ACK, level);
1362 }
1363 
espi_vw_pltrst_isr(const struct device * dev)1364 static void espi_vw_pltrst_isr(const struct device *dev)
1365 {
1366 	uint8_t pltrst = 0;
1367 
1368 	espi_it8xxx2_receive_vwire(dev, ESPI_VWIRE_SIGNAL_PLTRST, &pltrst);
1369 
1370 	if (pltrst) {
1371 		espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_SMI, 1);
1372 		espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_SCI, 1);
1373 		espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_HOST_RST_ACK, 1);
1374 		espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_RST_CPU_INIT, 1);
1375 	}
1376 
1377 	LOG_INF("VW PLTRST_L %sasserted", pltrst ? "de" : "");
1378 }
1379 
1380 static const struct espi_vw_signal_t vwidx3_signals[] = {
1381 	{ESPI_VWIRE_SIGNAL_OOB_RST_WARN, espi_vw_oob_rst_warn_isr},
1382 	{ESPI_VWIRE_SIGNAL_PLTRST,       espi_vw_pltrst_isr},
1383 };
1384 
espi_it8xxx2_vwidx3_isr(const struct device * dev,uint8_t updated_flag)1385 static void espi_it8xxx2_vwidx3_isr(const struct device *dev,
1386 					uint8_t updated_flag)
1387 {
1388 	for (int i = 0; i < ARRAY_SIZE(vwidx3_signals); i++) {
1389 		enum espi_vwire_signal vw_signal = vwidx3_signals[i].signal;
1390 
1391 		if (updated_flag & vw_channel_list[vw_signal].level_mask) {
1392 			vwidx3_signals[i].vw_signal_isr(dev);
1393 			espi_it8xxx2_vw_notify_system_state(dev, vw_signal);
1394 		}
1395 	}
1396 }
1397 
espi_vw_host_rst_warn_isr(const struct device * dev)1398 static void espi_vw_host_rst_warn_isr(const struct device *dev)
1399 {
1400 	uint8_t level = 0;
1401 
1402 	espi_it8xxx2_receive_vwire(dev,
1403 		ESPI_VWIRE_SIGNAL_HOST_RST_WARN, &level);
1404 	espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_HOST_RST_ACK, level);
1405 }
1406 
1407 static const struct espi_vw_signal_t vwidx7_signals[] = {
1408 	{ESPI_VWIRE_SIGNAL_HOST_RST_WARN, espi_vw_host_rst_warn_isr},
1409 };
1410 
espi_it8xxx2_vwidx7_isr(const struct device * dev,uint8_t updated_flag)1411 static void espi_it8xxx2_vwidx7_isr(const struct device *dev,
1412 					uint8_t updated_flag)
1413 {
1414 	for (int i = 0; i < ARRAY_SIZE(vwidx7_signals); i++) {
1415 		enum espi_vwire_signal vw_signal = vwidx7_signals[i].signal;
1416 
1417 		if (updated_flag & vw_channel_list[vw_signal].level_mask) {
1418 			vwidx7_signals[i].vw_signal_isr(dev);
1419 			espi_it8xxx2_vw_notify_system_state(dev, vw_signal);
1420 		}
1421 	}
1422 }
1423 
espi_vw_sus_warn_isr(const struct device * dev)1424 static void espi_vw_sus_warn_isr(const struct device *dev)
1425 {
1426 	uint8_t level = 0;
1427 
1428 	espi_it8xxx2_receive_vwire(dev, ESPI_VWIRE_SIGNAL_SUS_WARN, &level);
1429 	espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_SUS_ACK, level);
1430 }
1431 
1432 static const struct espi_vw_signal_t vwidx41_signals[] = {
1433 	{ESPI_VWIRE_SIGNAL_SUS_WARN,      espi_vw_sus_warn_isr},
1434 	{ESPI_VWIRE_SIGNAL_SUS_PWRDN_ACK, espi_vw_signal_no_isr},
1435 	{ESPI_VWIRE_SIGNAL_SLP_A,         espi_vw_signal_no_isr},
1436 };
1437 
espi_it8xxx2_vwidx41_isr(const struct device * dev,uint8_t updated_flag)1438 static void espi_it8xxx2_vwidx41_isr(const struct device *dev,
1439 					uint8_t updated_flag)
1440 {
1441 	for (int i = 0; i < ARRAY_SIZE(vwidx41_signals); i++) {
1442 		enum espi_vwire_signal vw_signal = vwidx41_signals[i].signal;
1443 
1444 		if (updated_flag & vw_channel_list[vw_signal].level_mask) {
1445 			vwidx41_signals[i].vw_signal_isr(dev);
1446 			espi_it8xxx2_vw_notify_system_state(dev, vw_signal);
1447 		}
1448 	}
1449 }
1450 
1451 static const struct espi_vw_signal_t vwidx42_signals[] = {
1452 	{ESPI_VWIRE_SIGNAL_SLP_LAN, NULL},
1453 	{ESPI_VWIRE_SIGNAL_SLP_WLAN, NULL},
1454 };
1455 
espi_it8xxx2_vwidx42_isr(const struct device * dev,uint8_t updated_flag)1456 static void espi_it8xxx2_vwidx42_isr(const struct device *dev,
1457 					uint8_t updated_flag)
1458 {
1459 	for (int i = 0; i < ARRAY_SIZE(vwidx42_signals); i++) {
1460 		enum espi_vwire_signal vw_signal = vwidx42_signals[i].signal;
1461 
1462 		if (updated_flag & vw_channel_list[vw_signal].level_mask) {
1463 			espi_it8xxx2_vw_notify_system_state(dev, vw_signal);
1464 		}
1465 	}
1466 }
1467 
espi_it8xxx2_vwidx43_isr(const struct device * dev,uint8_t updated_flag)1468 static void espi_it8xxx2_vwidx43_isr(const struct device *dev,
1469 					uint8_t updated_flag)
1470 {
1471 	ARG_UNUSED(dev);
1472 	/*
1473 	 * We haven't send callback to system because there is no index 43
1474 	 * virtual wire signal is listed in enum espi_vwire_signal.
1475 	 */
1476 	LOG_INF("vw isr %s is ignored!", __func__);
1477 }
1478 
espi_it8xxx2_vwidx44_isr(const struct device * dev,uint8_t updated_flag)1479 static void espi_it8xxx2_vwidx44_isr(const struct device *dev,
1480 					uint8_t updated_flag)
1481 {
1482 	ARG_UNUSED(dev);
1483 	/*
1484 	 * We haven't send callback to system because there is no index 44
1485 	 * virtual wire signal is listed in enum espi_vwire_signal.
1486 	 */
1487 	LOG_INF("vw isr %s is ignored!", __func__);
1488 }
1489 
1490 static const struct espi_vw_signal_t vwidx47_signals[] = {
1491 	{ESPI_VWIRE_SIGNAL_HOST_C10, NULL},
1492 };
espi_it8xxx2_vwidx47_isr(const struct device * dev,uint8_t updated_flag)1493 static void espi_it8xxx2_vwidx47_isr(const struct device *dev,
1494 					uint8_t updated_flag)
1495 {
1496 	for (int i = 0; i < ARRAY_SIZE(vwidx47_signals); i++) {
1497 		enum espi_vwire_signal vw_signal = vwidx47_signals[i].signal;
1498 
1499 		if (updated_flag & vw_channel_list[vw_signal].level_mask) {
1500 			espi_it8xxx2_vw_notify_system_state(dev, vw_signal);
1501 		}
1502 	}
1503 }
1504 
1505 /*
1506  * The ISR of espi VW interrupt in array needs to match bit order in
1507  * ESPI VW VWCTRL1 register.
1508  */
1509 static const struct vwidx_isr_t vwidx_isr_list[] = {
1510 	[0] = {espi_it8xxx2_vwidx2_isr,  0x02},
1511 	[1] = {espi_it8xxx2_vwidx3_isr,  0x03},
1512 	[2] = {espi_it8xxx2_vwidx7_isr,  0x07},
1513 	[3] = {espi_it8xxx2_vwidx41_isr, 0x41},
1514 	[4] = {espi_it8xxx2_vwidx42_isr, 0x42},
1515 	[5] = {espi_it8xxx2_vwidx43_isr, 0x43},
1516 	[6] = {espi_it8xxx2_vwidx44_isr, 0x44},
1517 	[7] = {espi_it8xxx2_vwidx47_isr, 0x47},
1518 };
1519 
1520 /*
1521  * This is used to record the previous VW valid/level field state to discover
1522  * changes. Then do following sequence only when state is changed.
1523  */
1524 static uint8_t vwidx_cached_flag[ARRAY_SIZE(vwidx_isr_list)];
1525 
espi_it8xxx2_reset_vwidx_cache(const struct device * dev)1526 static void espi_it8xxx2_reset_vwidx_cache(const struct device *dev)
1527 {
1528 	const struct espi_it8xxx2_config *const config = dev->config;
1529 	struct espi_vw_regs *const vw_reg =
1530 		(struct espi_vw_regs *)config->base_espi_vw;
1531 
1532 	/* reset vwidx_cached_flag */
1533 	for (int i = 0; i < ARRAY_SIZE(vwidx_isr_list); i++) {
1534 		vwidx_cached_flag[i] =
1535 			vw_reg->VW_INDEX[vwidx_isr_list[i].vw_index];
1536 	}
1537 }
1538 
espi_it8xxx2_vw_isr(const struct device * dev)1539 static void espi_it8xxx2_vw_isr(const struct device *dev)
1540 {
1541 	const struct espi_it8xxx2_config *const config = dev->config;
1542 	struct espi_vw_regs *const vw_reg =
1543 		(struct espi_vw_regs *)config->base_espi_vw;
1544 	uint8_t vwidx_updated = vw_reg->VWCTRL1;
1545 
1546 	/* write-1 to clear */
1547 	vw_reg->VWCTRL1 = vwidx_updated;
1548 
1549 	for (int i = 0; i < ARRAY_SIZE(vwidx_isr_list); i++) {
1550 		if (vwidx_updated & BIT(i)) {
1551 			uint8_t vw_flag;
1552 
1553 			vw_flag = vw_reg->VW_INDEX[vwidx_isr_list[i].vw_index];
1554 			vwidx_isr_list[i].vwidx_isr(dev,
1555 					vwidx_cached_flag[i] ^ vw_flag);
1556 			vwidx_cached_flag[i] = vw_flag;
1557 		}
1558 	}
1559 }
1560 
espi_it8xxx2_ch_notify_system_state(const struct device * dev,enum espi_channel ch,bool en)1561 static void espi_it8xxx2_ch_notify_system_state(const struct device *dev,
1562 						enum espi_channel ch, bool en)
1563 {
1564 	struct espi_it8xxx2_data *const data = dev->data;
1565 	struct espi_event evt = {
1566 		.evt_type = ESPI_BUS_EVENT_CHANNEL_READY,
1567 		.evt_details = ch,
1568 		.evt_data = en,
1569 	};
1570 
1571 	espi_send_callbacks(&data->callbacks, dev, evt);
1572 }
1573 
1574 /*
1575  * Peripheral channel enable asserted flag.
1576  * A 0-to-1 or 1-to-0 transition on "Peripheral Channel Enable" bit.
1577  */
espi_it8xxx2_peripheral_ch_en_isr(const struct device * dev,bool enable)1578 static void espi_it8xxx2_peripheral_ch_en_isr(const struct device *dev,
1579 						bool enable)
1580 {
1581 	espi_it8xxx2_ch_notify_system_state(dev,
1582 					ESPI_CHANNEL_PERIPHERAL, enable);
1583 }
1584 
1585 /*
1586  * VW channel enable asserted flag.
1587  * A 0-to-1 or 1-to-0 transition on "Virtual Wire Channel Enable" bit.
1588  */
espi_it8xxx2_vw_ch_en_isr(const struct device * dev,bool enable)1589 static void espi_it8xxx2_vw_ch_en_isr(const struct device *dev, bool enable)
1590 {
1591 	espi_it8xxx2_ch_notify_system_state(dev, ESPI_CHANNEL_VWIRE, enable);
1592 }
1593 
1594 /*
1595  * OOB message channel enable asserted flag.
1596  * A 0-to-1 or 1-to-0 transition on "OOB Message Channel Enable" bit.
1597  */
espi_it8xxx2_oob_ch_en_isr(const struct device * dev,bool enable)1598 static void espi_it8xxx2_oob_ch_en_isr(const struct device *dev, bool enable)
1599 {
1600 	espi_it8xxx2_ch_notify_system_state(dev, ESPI_CHANNEL_OOB, enable);
1601 }
1602 
1603 /*
1604  * Flash channel enable asserted flag.
1605  * A 0-to-1 or 1-to-0 transition on "Flash Access Channel Enable" bit.
1606  */
espi_it8xxx2_flash_ch_en_isr(const struct device * dev,bool enable)1607 static void espi_it8xxx2_flash_ch_en_isr(const struct device *dev, bool enable)
1608 {
1609 	if (enable) {
1610 		espi_it8xxx2_send_vwire(dev, ESPI_VWIRE_SIGNAL_TARGET_BOOT_STS, 1);
1611 		espi_it8xxx2_send_vwire(dev,
1612 					ESPI_VWIRE_SIGNAL_TARGET_BOOT_DONE, 1);
1613 	}
1614 
1615 	espi_it8xxx2_ch_notify_system_state(dev, ESPI_CHANNEL_FLASH, enable);
1616 }
1617 
espi_it8xxx2_put_pc_status_isr(const struct device * dev)1618 static void espi_it8xxx2_put_pc_status_isr(const struct device *dev)
1619 {
1620 	const struct espi_it8xxx2_config *const config = dev->config;
1621 	struct espi_slave_regs *const slave_reg =
1622 		(struct espi_slave_regs *)config->base_espi_slave;
1623 
1624 	/*
1625 	 * TODO: To check cycle type (bit[3-0] at ESPCTRL0) and make
1626 	 * corresponding modification if needed.
1627 	 */
1628 	LOG_INF("isr %s is ignored!", __func__);
1629 
1630 	/* write-1-clear to release PC_FREE */
1631 	slave_reg->ESPCTRL0 = IT8XXX2_ESPI_INTERRUPT_PUT_PC;
1632 }
1633 
1634 #ifdef CONFIG_ESPI_OOB_CHANNEL
espi_it8xxx2_upstream_channel_disable_isr(const struct device * dev)1635 static void espi_it8xxx2_upstream_channel_disable_isr(const struct device *dev)
1636 {
1637 	const struct espi_it8xxx2_config *const config = dev->config;
1638 	struct espi_slave_regs *const slave_reg =
1639 		(struct espi_slave_regs *)config->base_espi_slave;
1640 
1641 	LOG_INF("isr %s is ignored!", __func__);
1642 
1643 	/* write-1 to clear this bit */
1644 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_CHANNEL_DISABLE;
1645 }
1646 
espi_it8xxx2_put_oob_status_isr(const struct device * dev)1647 static void espi_it8xxx2_put_oob_status_isr(const struct device *dev)
1648 {
1649 	const struct espi_it8xxx2_config *const config = dev->config;
1650 	struct espi_it8xxx2_data *const data = dev->data;
1651 	struct espi_slave_regs *const slave_reg =
1652 		(struct espi_slave_regs *)config->base_espi_slave;
1653 #ifdef CONFIG_ESPI_OOB_CHANNEL_RX_ASYNC
1654 	struct espi_event evt = { .evt_type = ESPI_BUS_EVENT_OOB_RECEIVED,
1655 				  .evt_details = 0,
1656 				  .evt_data = 0 };
1657 #endif
1658 
1659 	/* Write-1 to clear this bit for the next coming posted transaction. */
1660 	slave_reg->ESOCTRL0 |= IT8XXX2_ESPI_PUT_OOB_STATUS;
1661 
1662 #ifndef CONFIG_ESPI_OOB_CHANNEL_RX_ASYNC
1663 	k_sem_give(&data->oob_upstream_go);
1664 #else
1665 	/* Additional detail is length field of PUT_OOB message packet. */
1666 	evt.evt_details = (slave_reg->ESOCTRL4 & IT8XXX2_ESPI_PUT_OOB_LEN_MASK);
1667 	espi_send_callbacks(&data->callbacks, dev, evt);
1668 #endif
1669 }
1670 #endif
1671 
1672 #if defined(CONFIG_ESPI_OOB_CHANNEL) || defined(CONFIG_ESPI_FLASH_CHANNEL)
espi_it8xxx2_upstream_done_isr(const struct device * dev)1673 static void espi_it8xxx2_upstream_done_isr(const struct device *dev)
1674 {
1675 	const struct espi_it8xxx2_config *const config = dev->config;
1676 	struct espi_slave_regs *const slave_reg =
1677 		(struct espi_slave_regs *)config->base_espi_slave;
1678 
1679 #ifdef CONFIG_ESPI_FLASH_CHANNEL
1680 	/* cycle type is flash read, write, or erase */
1681 	if (slave_reg->ESUCTRL1 != IT8XXX2_ESPI_CYCLE_TYPE_OOB) {
1682 		espi_it8xxx2_flash_upstream_done_isr(dev);
1683 	}
1684 #endif
1685 
1686 	/* write-1 to clear this bit */
1687 	slave_reg->ESUCTRL0 |= IT8XXX2_ESPI_UPSTREAM_DONE;
1688 	/* upstream disable */
1689 	slave_reg->ESUCTRL0 &= ~IT8XXX2_ESPI_UPSTREAM_ENABLE;
1690 }
1691 #endif
1692 
1693 /*
1694  * The ISR of espi interrupt event in array need to be matched bit order in
1695  * IT8XXX2 ESPI ESGCTRL0 register.
1696  */
1697 static const struct espi_isr_t espi_isr_list[] = {
1698 	[0] = {espi_it8xxx2_peripheral_ch_en_isr, ASSERTED_FLAG},
1699 	[1] = {espi_it8xxx2_vw_ch_en_isr,         ASSERTED_FLAG},
1700 	[2] = {espi_it8xxx2_oob_ch_en_isr,        ASSERTED_FLAG},
1701 	[3] = {espi_it8xxx2_flash_ch_en_isr,      ASSERTED_FLAG},
1702 	[4] = {espi_it8xxx2_peripheral_ch_en_isr, DEASSERTED_FLAG},
1703 	[5] = {espi_it8xxx2_vw_ch_en_isr,         DEASSERTED_FLAG},
1704 	[6] = {espi_it8xxx2_oob_ch_en_isr,        DEASSERTED_FLAG},
1705 	[7] = {espi_it8xxx2_flash_ch_en_isr,      DEASSERTED_FLAG},
1706 };
1707 
espi_it8xxx2_isr(const struct device * dev)1708 static void espi_it8xxx2_isr(const struct device *dev)
1709 {
1710 	const struct espi_it8xxx2_config *const config = dev->config;
1711 	struct espi_slave_regs *const slave_reg =
1712 		(struct espi_slave_regs *)config->base_espi_slave;
1713 	/* get espi interrupt events */
1714 	uint8_t espi_event = slave_reg->ESGCTRL0;
1715 #if defined(CONFIG_ESPI_OOB_CHANNEL) || defined(CONFIG_ESPI_FLASH_CHANNEL)
1716 	uint8_t espi_upstream = slave_reg->ESUCTRL0;
1717 #endif
1718 
1719 	/* write-1 to clear */
1720 	slave_reg->ESGCTRL0 = espi_event;
1721 
1722 	/* process espi interrupt events */
1723 	for (int i = 0; i < ARRAY_SIZE(espi_isr_list); i++) {
1724 		if (espi_event & BIT(i)) {
1725 			espi_isr_list[i].espi_isr(dev,
1726 				espi_isr_list[i].isr_type);
1727 		}
1728 	}
1729 
1730 	/*
1731 	 * bit7: the peripheral has received a peripheral posted/completion.
1732 	 * This bit indicates the peripheral has received a packet from eSPI
1733 	 * peripheral channel.
1734 	 */
1735 	if (slave_reg->ESPCTRL0 & IT8XXX2_ESPI_INTERRUPT_PUT_PC) {
1736 		espi_it8xxx2_put_pc_status_isr(dev);
1737 	}
1738 
1739 #ifdef CONFIG_ESPI_OOB_CHANNEL
1740 	/*
1741 	 * The corresponding channel of the eSPI upstream transaction is
1742 	 * disabled.
1743 	 */
1744 	if (espi_upstream & IT8XXX2_ESPI_UPSTREAM_CHANNEL_DISABLE) {
1745 		espi_it8xxx2_upstream_channel_disable_isr(dev);
1746 	}
1747 
1748 	/* The eSPI slave has received a PUT_OOB message. */
1749 	if (slave_reg->ESOCTRL0 & IT8XXX2_ESPI_PUT_OOB_STATUS) {
1750 		espi_it8xxx2_put_oob_status_isr(dev);
1751 	}
1752 #endif
1753 
1754 	/* eSPI oob and flash channels use the same interrupt of upstream. */
1755 #if defined(CONFIG_ESPI_OOB_CHANNEL) || defined(CONFIG_ESPI_FLASH_CHANNEL)
1756 	/* The eSPI upstream transaction is done. */
1757 	if (espi_upstream & IT8XXX2_ESPI_UPSTREAM_DONE) {
1758 		espi_it8xxx2_upstream_done_isr(dev);
1759 	}
1760 #endif
1761 }
1762 
espi_it8xxx2_enable_pad_ctrl(const struct device * dev,bool enable)1763 void espi_it8xxx2_enable_pad_ctrl(const struct device *dev, bool enable)
1764 {
1765 	const struct espi_it8xxx2_config *const config = dev->config;
1766 	struct espi_slave_regs *const slave_reg =
1767 		(struct espi_slave_regs *)config->base_espi_slave;
1768 
1769 	if (enable) {
1770 		/* Enable eSPI pad. */
1771 		slave_reg->ESGCTRL2 &= ~IT8XXX2_ESPI_INPUT_PAD_GATING;
1772 	} else {
1773 		/* Disable eSPI pad. */
1774 		slave_reg->ESGCTRL2 |= IT8XXX2_ESPI_INPUT_PAD_GATING;
1775 	}
1776 }
1777 
espi_it8xxx2_enable_trans_irq(const struct device * dev,bool enable)1778 void espi_it8xxx2_enable_trans_irq(const struct device *dev, bool enable)
1779 {
1780 	const struct espi_it8xxx2_config *const config = dev->config;
1781 
1782 	if (enable) {
1783 		irq_enable(IT8XXX2_TRANS_IRQ);
1784 	} else {
1785 		irq_disable(IT8XXX2_TRANS_IRQ);
1786 		/* Clear pending interrupt */
1787 		it8xxx2_wuc_clear_status(config->wuc.wucs, config->wuc.mask);
1788 	}
1789 }
1790 
espi_it8xxx2_trans_isr(const struct device * dev)1791 static void espi_it8xxx2_trans_isr(const struct device *dev)
1792 {
1793 	/*
1794 	 * This interrupt is only used to wake up CPU, there is no need to do
1795 	 * anything in the isr in addition to disable interrupt.
1796 	 */
1797 	espi_it8xxx2_enable_trans_irq(dev, false);
1798 }
1799 
espi_it8xxx2_espi_reset_isr(const struct device * port,struct gpio_callback * cb,uint32_t pins)1800 void espi_it8xxx2_espi_reset_isr(const struct device *port,
1801 				struct gpio_callback *cb, uint32_t pins)
1802 {
1803 	struct espi_it8xxx2_data *const data = ESPI_IT8XXX2_SOC_DEV->data;
1804 	struct espi_event evt = {ESPI_BUS_RESET, 0, 0};
1805 	bool espi_reset = gpio_pin_get(port, (find_msb_set(pins) - 1));
1806 
1807 	if (!(espi_reset)) {
1808 		/* Reset vwidx_cached_flag[] when espi_reset# asserted. */
1809 		espi_it8xxx2_reset_vwidx_cache(ESPI_IT8XXX2_SOC_DEV);
1810 	}
1811 
1812 	evt.evt_data = espi_reset;
1813 	espi_send_callbacks(&data->callbacks, ESPI_IT8XXX2_SOC_DEV, evt);
1814 
1815 	LOG_INF("eSPI reset %sasserted", espi_reset ? "de" : "");
1816 }
1817 
1818 /* eSPI reset# is enabled on GPD2 */
1819 #define ESPI_IT8XXX2_ESPI_RESET_PORT DEVICE_DT_GET(DT_NODELABEL(gpiod))
1820 #define ESPI_IT8XXX2_ESPI_RESET_PIN  2
espi_it8xxx2_enable_reset(void)1821 static void espi_it8xxx2_enable_reset(void)
1822 {
1823 	struct gpio_it8xxx2_regs *const gpio_regs = GPIO_IT8XXX2_REG_BASE;
1824 	static struct gpio_callback espi_reset_cb;
1825 
1826 	/* eSPI reset is enabled on GPD2 */
1827 	gpio_regs->GPIO_GCR =
1828 		(gpio_regs->GPIO_GCR & ~IT8XXX2_GPIO_GCR_ESPI_RST_EN_MASK) |
1829 		(IT8XXX2_GPIO_GCR_ESPI_RST_D2 << IT8XXX2_GPIO_GCR_ESPI_RST_POS);
1830 	/* enable eSPI reset isr */
1831 	gpio_init_callback(&espi_reset_cb, espi_it8xxx2_espi_reset_isr,
1832 				BIT(ESPI_IT8XXX2_ESPI_RESET_PIN));
1833 	gpio_add_callback(ESPI_IT8XXX2_ESPI_RESET_PORT, &espi_reset_cb);
1834 	gpio_pin_interrupt_configure(ESPI_IT8XXX2_ESPI_RESET_PORT,
1835 					ESPI_IT8XXX2_ESPI_RESET_PIN,
1836 					GPIO_INT_MODE_EDGE | GPIO_INT_TRIG_BOTH);
1837 }
1838 
1839 static struct espi_it8xxx2_data espi_it8xxx2_data_0;
1840 static const struct espi_it8xxx2_config espi_it8xxx2_config_0 = {
1841 	.base_espi_slave = DT_INST_REG_ADDR_BY_IDX(0, 0),
1842 	.base_espi_vw = DT_INST_REG_ADDR_BY_IDX(0, 1),
1843 	.base_espi_queue0 = DT_INST_REG_ADDR_BY_IDX(0, 2),
1844 	.base_espi_queue1 = DT_INST_REG_ADDR_BY_IDX(0, 3),
1845 	.base_ec2i = DT_INST_REG_ADDR_BY_IDX(0, 4),
1846 	.base_kbc = DT_INST_REG_ADDR_BY_IDX(0, 5),
1847 	.base_pmc = DT_INST_REG_ADDR_BY_IDX(0, 6),
1848 	.base_smfi = DT_INST_REG_ADDR_BY_IDX(0, 7),
1849 	.wuc = IT8XXX2_DT_WUC_ITEMS_FUNC(0, 0),
1850 };
1851 
1852 DEVICE_DT_INST_DEFINE(0, &espi_it8xxx2_init, NULL,
1853 		    &espi_it8xxx2_data_0, &espi_it8xxx2_config_0,
1854 		    PRE_KERNEL_2, CONFIG_ESPI_INIT_PRIORITY,
1855 		    &espi_it8xxx2_driver_api);
1856 
espi_it8xxx2_init(const struct device * dev)1857 static int espi_it8xxx2_init(const struct device *dev)
1858 {
1859 	const struct espi_it8xxx2_config *const config = dev->config;
1860 	struct espi_vw_regs *const vw_reg =
1861 		(struct espi_vw_regs *)config->base_espi_vw;
1862 	struct espi_slave_regs *const slave_reg =
1863 		(struct espi_slave_regs *)config->base_espi_slave;
1864 	struct gctrl_it8xxx2_regs *const gctrl = ESPI_IT8XXX2_GET_GCTRL_BASE;
1865 
1866 	/* configure VCC detector */
1867 	gctrl->GCTRL_RSTS = (gctrl->GCTRL_RSTS &
1868 			~(IT8XXX2_GCTRL_VCCDO_MASK | IT8XXX2_GCTRL_HGRST)) |
1869 			(IT8XXX2_GCTRL_VCCDO_VCC_ON | IT8XXX2_GCTRL_GRST);
1870 
1871 	/* enable PNPCFG devices */
1872 	pnpcfg_it8xxx2_init(dev);
1873 
1874 #ifdef CONFIG_ESPI_PERIPHERAL_8042_KBC
1875 	/* enable kbc port (60h/64h) */
1876 	kbc_it8xxx2_init(dev);
1877 #endif
1878 #ifdef CONFIG_ESPI_PERIPHERAL_HOST_IO
1879 	/* enable pmc1 for ACPI port (62h/66h) */
1880 	pmc1_it8xxx2_init(dev);
1881 #endif
1882 #ifdef CONFIG_ESPI_PERIPHERAL_DEBUG_PORT_80
1883 	/* Accept Port 80h Cycle */
1884 	port80_it8xxx2_init(dev);
1885 #endif
1886 #if defined(CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD) || \
1887 	defined(CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION)
1888 	smfi_it8xxx2_init(dev);
1889 #endif
1890 #ifdef CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD
1891 	/* enable pmc2 for host command port */
1892 	pmc2_it8xxx2_init(dev);
1893 #endif
1894 
1895 	/* Reset vwidx_cached_flag[] at initialization */
1896 	espi_it8xxx2_reset_vwidx_cache(dev);
1897 
1898 	/* Enable espi vw interrupt */
1899 	vw_reg->VWCTRL0 |= IT8XXX2_ESPI_VW_INTERRUPT_ENABLE;
1900 	IRQ_CONNECT(IT8XXX2_ESPI_VW_IRQ, 0, espi_it8xxx2_vw_isr,
1901 			DEVICE_DT_INST_GET(0), 0);
1902 	irq_enable(IT8XXX2_ESPI_VW_IRQ);
1903 
1904 	/* Reset PLTRST# virtual wire signal during eSPI reset */
1905 	vw_reg->VWCTRL2 |= IT8XXX2_ESPI_VW_RESET_PLTRST;
1906 
1907 #ifdef CONFIG_ESPI_OOB_CHANNEL
1908 	espi_it8xxx2_oob_init(dev);
1909 #endif
1910 
1911 #ifdef CONFIG_ESPI_FLASH_CHANNEL
1912 	espi_it8xxx2_flash_init(dev);
1913 #endif
1914 
1915 	/* Enable espi interrupt */
1916 	slave_reg->ESGCTRL1 |= IT8XXX2_ESPI_INTERRUPT_ENABLE;
1917 	IRQ_CONNECT(IT8XXX2_ESPI_IRQ, 0, espi_it8xxx2_isr,
1918 			DEVICE_DT_INST_GET(0), 0);
1919 	irq_enable(IT8XXX2_ESPI_IRQ);
1920 
1921 	/* enable interrupt and reset from eSPI_reset# */
1922 	espi_it8xxx2_enable_reset();
1923 
1924 	/*
1925 	 * Enable eSPI to WUC.
1926 	 * If an eSPI transaction is accepted, WU42 interrupt will be asserted.
1927 	 */
1928 	slave_reg->ESGCTRL2 |= IT8XXX2_ESPI_TO_WUC_ENABLE;
1929 
1930 	/* Enable WU42 of WUI */
1931 	it8xxx2_wuc_clear_status(config->wuc.wucs, config->wuc.mask);
1932 	it8xxx2_wuc_enable(config->wuc.wucs, config->wuc.mask);
1933 	/*
1934 	 * Only register isr here, the interrupt only need to be enabled
1935 	 * before CPU and RAM clocks gated in the idle function.
1936 	 */
1937 	IRQ_CONNECT(IT8XXX2_TRANS_IRQ, 0, espi_it8xxx2_trans_isr,
1938 			DEVICE_DT_INST_GET(0), 0);
1939 
1940 	return 0;
1941 }
1942