summaryrefslogtreecommitdiff
path: root/src/superio/smsc/sch5545/sch5545_emi.c
blob: 2bb150c69b54afcee6e2507624270c9128a981b9 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* SPDX-License-Identifier: GPL-2.0-only */

#include <assert.h>
#include <arch/io.h>
#include <device/pnp.h>
#include <device/pnp_def.h>
#include <device/pnp_ops.h>
#include <superio/conf_mode.h>

#include "sch5545.h"
#include "sch5545_emi.h"

static uint16_t emi_bar;

#ifdef __SIMPLE_DEVICE__
static void sch5545_enter_conf_state(pnp_devfn_t dev)
{
	unsigned int port = dev >> 8;
	outb(0x55, port);
}

static void sch5545_exit_conf_state(pnp_devfn_t dev)
{
	unsigned int port = dev >> 8;
	outb(0xaa, port);
}
#endif

uint16_t sch5545_read_emi_bar(uint8_t sio_port)
{
	uint16_t bar;

#ifdef __SIMPLE_DEVICE__
	pnp_devfn_t lpcif = PNP_DEV(sio_port, SCH5545_LDN_LPC);
	sch5545_enter_conf_state(lpcif);
#else
	struct device *lpcif = dev_find_slot_pnp(sio_port, SCH5545_LDN_LPC);
	if (!lpcif)
		return 0;
	pnp_enter_conf_mode_55(lpcif);
#endif
	pnp_set_logical_device(lpcif);

	bar = pnp_read_config(lpcif, SCH5545_BAR_EM_IF + 2);
	bar |= pnp_read_config(lpcif, SCH5545_BAR_EM_IF + 3) << 8;

#ifdef __SIMPLE_DEVICE__
	sch5545_exit_conf_state(lpcif);
#else
	pnp_exit_conf_mode_aa(lpcif);
#endif
	return bar;
}

void sch5545_emi_init(uint8_t sio_port)
{
	emi_bar = sch5545_read_emi_bar(sio_port);
	assert(emi_bar != 0);
}

void sch5545_emi_ec2h_mailbox_clear(void)
{
	sch5545_emi_ec2h_mbox_write(sch5545_emi_ec2h_mbox_read());
}

void sch5545_emi_disable_interrupts(void)
{
	sch5545_emi_set_int_mask(0);
}

void sch5545_emi_h2ec_mbox_write(uint8_t mbox_message)
{
	outb(mbox_message, emi_bar + SCH5545_EMI_HOST_TO_EC_MAILBOX);
}

uint8_t sch5545_emi_h2ec_mbox_read(void)
{
	return inb(emi_bar + SCH5545_EMI_HOST_TO_EC_MAILBOX);
}

void sch5545_emi_ec2h_mbox_write(uint8_t mbox_message)
{
	outb(mbox_message, emi_bar + SCH5545_EMI_EC_TO_HOST_MAILBOX);
}

uint8_t sch5545_emi_ec2h_mbox_read(void)
{
	return inb(emi_bar + SCH5545_EMI_EC_TO_HOST_MAILBOX);
}

void sch5545_emi_set_int_mask(uint16_t mask)
{
	outw(mask, emi_bar + SCH5545_EMI_INT_MASK);
}

void sch5545_emi_set_int_mask_low(uint8_t mask)
{
	outb(mask, emi_bar + SCH5545_EMI_INT_MASK);
}

void sch5545_emi_set_int_mask_high(uint8_t mask)
{
	outb(mask, emi_bar + SCH5545_EMI_INT_MASK + 1);
}

uint8_t sch5545_emi_get_int_mask_low(void)
{
	return inb(emi_bar + SCH5545_EMI_INT_MASK);
}

uint8_t sch5545_emi_get_int_mask_high(void)
{
	return inb(emi_bar + SCH5545_EMI_INT_MASK + 1);
}

uint16_t sch5545_emi_get_int_mask(void)
{
	return inw(emi_bar + SCH5545_EMI_INT_MASK);
}

void sch5545_emi_set_int_src_low(uint8_t int_src)
{
	outb(int_src, emi_bar + SCH5545_EMI_INT_SOURCE);
}

void sch5545_emi_set_int_src_high(uint8_t int_src)
{
	outb(int_src, emi_bar + SCH5545_EMI_INT_SOURCE + 1);
}

uint8_t sch5545_emi_get_int_src_low(void)
{
	return inb(emi_bar + SCH5545_EMI_INT_SOURCE);
}

uint8_t sch5545_emi_get_int_src_high(void)
{
	return inb(emi_bar + SCH5545_EMI_INT_SOURCE + 1);
}
uint16_t sch5545_emi_get_int_src(void)
{
	return inw(emi_bar + SCH5545_EMI_INT_SOURCE);
}

void sch5545_emi_set_int_src(uint16_t int_src)
{
	outw(int_src, emi_bar + SCH5545_EMI_INT_SOURCE);
}

void sch5545_emi_set_ec_addr(uint16_t addr)
{
	outw(addr, emi_bar + SCH5545_EMI_EC_ADDR);
}

uint16_t sch5545_emi_read_ec_addr(void)
{
	return inw(emi_bar + SCH5545_EMI_EC_ADDR);
}

void sch5545_emi_ec_write8(uint16_t addr, uint8_t data)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_8BIT_ACCESS);
	outb(data, emi_bar + SCH5545_EMI_EC_DATA + (addr & 3));
}

void sch5545_emi_ec_write16(uint16_t addr, uint16_t data)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_16BIT_ACCESS);
	outw(data, emi_bar + SCH5545_EMI_EC_DATA + (addr & 2));
}

void sch5545_emi_ec_write32(uint16_t addr, uint32_t data)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_32BIT_ACCESS);
	outl(data, emi_bar + SCH5545_EMI_EC_DATA);
}

void sch5545_emi_ec_write32_bulk(uint16_t addr, const uint32_t *buffer, size_t len)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_32BIT_AUTO_ACCESS);

	while (len > 0) {
		outl(*(buffer++), emi_bar + SCH5545_EMI_EC_DATA);
		len--;
	}
}

uint8_t sch5545_emi_ec_read8(uint16_t addr)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_8BIT_ACCESS);
	return inb(emi_bar + SCH5545_EMI_EC_DATA + (addr & 3));
}

uint16_t sch5545_emi_ec_read16(uint16_t addr)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_16BIT_ACCESS);
	return inw(emi_bar + SCH5545_EMI_EC_DATA + (addr & 2));
}

uint32_t sch5545_emi_ec_read32(uint16_t addr)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_32BIT_ACCESS);
	return inb(emi_bar + SCH5545_EMI_EC_DATA);
}

void sch5545_emi_ec_read32_bulk(uint16_t addr, uint32_t *buffer, size_t len)
{
	sch5545_emi_set_ec_addr((addr & 0xfffc) | EMI_EC_32BIT_AUTO_ACCESS);

	while (len > 0) {
		*(buffer++) = inl(emi_bar + SCH5545_EMI_EC_DATA);
		len--;
	}
}