summaryrefslogtreecommitdiff
path: root/zephyr/subsys/ap_pwrseq/signal_vw.c
blob: b0abe4fe13190e8a785fe8826c23cdffff2c9402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright 2022 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <drivers/espi.h>
#include <x86_non_dsx_common_pwrseq_sm_handler.h>

#include "signal_vw.h"

#define MY_COMPAT	intel_ap_pwrseq_vw

#if HAS_VW_SIGNALS

LOG_MODULE_DECLARE(ap_pwrseq, CONFIG_AP_PWRSEQ_LOG_LEVEL);

#define INIT_ESPI_SIGNAL(id)					\
{								\
	.espi_signal = DT_STRING_UPPER_TOKEN(id, virtual_wire),	\
	.signal = PWR_SIGNAL_ENUM(id),				\
	.invert = DT_PROP(id, vw_invert),			\
},

/*
 * Struct containing the eSPI virtual wire config.
 */
struct vw_config {
	uint8_t espi_signal;	/* associated VW signal */
	uint8_t signal;		/* power signal */
	bool invert;		/* Invert the signal value */
};

const static struct vw_config vw_config[] = {
DT_FOREACH_STATUS_OKAY(MY_COMPAT, INIT_ESPI_SIGNAL)
};

static bool signal_data[ARRAY_SIZE(vw_config)];

#define espi_dev DEVICE_DT_GET(DT_CHOSEN(intel_ap_pwrseq_espi))

/*
 * Mask of updated signals. If the bus is reset, this is cleared,
 * and it is only when all the signals have been updated that
 * notification is sent that the signals are ready.
 */
static uint8_t espi_mask;
static bool espi_not_valid;
BUILD_ASSERT(ARRAY_SIZE(vw_config) <= 8);

static void espi_handler(const struct device *dev,
			 struct espi_callback *cb,
			 struct espi_event event)
{
	LOG_DBG("ESPI event type 0x%x %d:%d", event.evt_type,
		event.evt_details, event.evt_data);
	switch (event.evt_type) {
	default:
		__ASSERT(0, "ESPI unknown event type: %d",
			 event.evt_type);
		break;

	case ESPI_BUS_RESET:
		/*
		 * Notify that the bus isn't ready, and clear
		 * the signal mask.
		 */
		notify_espi_ready(false);
		espi_mask = 0;
		espi_not_valid = true;
		break;

	case ESPI_BUS_EVENT_VWIRE_RECEIVED:
		for (int i = 0; i < ARRAY_SIZE(vw_config); i++) {
			if (event.evt_details == vw_config[i].espi_signal) {
				int value = vw_config[i].invert
						? !event.evt_data
						: !!event.evt_data;

				signal_data[i] = value;
				if (espi_not_valid) {
					espi_mask |= BIT(i);
				}
				power_signal_interrupt(vw_config[i].signal,
						       value);
			}
		}
		/*
		 * When all the signals have been updated, notify that
		 * the ESPI signals are valid.
		 */
		if (espi_not_valid &&
		    espi_mask == BIT_MASK(ARRAY_SIZE(vw_config))) {
			espi_not_valid = false;
			LOG_DBG("ESPI signals valid");
			/*
			 * TODO(b/222946923): Convert to generalised
			 * callback pattern.
			 */
			notify_espi_ready(true);
		}
		break;
	}
}

int power_signal_vw_get(enum pwr_sig_vw vw)
{
	if (vw < 0 || vw >= ARRAY_SIZE(vw_config)) {
		return -EINVAL;
	}
	return signal_data[vw];
}

void power_signal_vw_init(void)
{
	static struct espi_callback espi_cb;

	struct espi_cfg cfg = {
		.io_caps = ESPI_IO_MODE_SINGLE_LINE,
		.channel_caps = ESPI_CHANNEL_VWIRE |
			ESPI_CHANNEL_PERIPHERAL |
			ESPI_CHANNEL_OOB,
		/* ESPI_FREQ_MHZ */
		.max_freq = DT_INST_PROP(0, pwrseq_espi_max_freq),
	};

	if (!device_is_ready(espi_dev))	{
		LOG_ERR("Espi device is not ready");
		return;
	}

	if (espi_config(espi_dev, &cfg)) {
		LOG_ERR("Failed to configure eSPI");
		return;
	}

	/* Configure handler for eSPI events */
	espi_init_callback(&espi_cb, espi_handler,
			   ESPI_BUS_RESET |
			   ESPI_BUS_EVENT_VWIRE_RECEIVED);
	espi_add_callback(espi_dev, &espi_cb);
}

#endif /* HAS_VW_SIGNALS */