summaryrefslogtreecommitdiffstats
path: root/sys/dev/acpi/acpi_io.c
blob: 1d1384f9542397d8252db95a46f0cf4497d913df (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*-
 * Copyright (c) 1999 Takanori Watanabe <takawata@shidahara1.planet.sci.kobe-u.ac.jp>
 * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>

#include <machine/bus.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>

/*
 * ACPI Register I/O
 */
static __inline void
acpi_register_input(acpi_softc_t *sc, int res, int offset, u_int32_t *value, u_int32_t size)
{
	bus_space_tag_t		bst;
	bus_space_handle_t	bsh;
	u_int32_t		val;

	if (sc->iores[res].rsc == NULL)
		return;

	bst = sc->iores[res].btag;
	bsh = sc->iores[res].bhandle;

	switch (size) {
	case 1:
		val = bus_space_read_1(bst, bsh, offset);
		break;
	case 2:
		val = bus_space_read_2(bst, bsh, offset);
		break;
	case 3:
		val = bus_space_read_4(bst, bsh, offset);
		val &= 0x00ffffff;
		break;
	case 4:
		val = bus_space_read_4(bst, bsh, offset);
		break;
	default:
		ACPI_DEVPRINTF("acpi_register_input(): invalid size (%d)\n", size);
		val = 0;
		break;
	}

	*value = val;
}

static __inline void
acpi_register_output(acpi_softc_t *sc, int res, int offset, u_int32_t *value, u_int32_t size)
{
	bus_space_tag_t		bst;
	bus_space_handle_t	bsh;
	u_int32_t		val;

	if (sc->iores[res].rsc == NULL)
		return;

	val = *value;
	bst = sc->iores[res].btag;
	bsh = sc->iores[res].bhandle;

	switch (size) {
	case 1:
		bus_space_write_1(bst, bsh, offset, val & 0xff);
		break;
	case 2:
		bus_space_write_2(bst, bsh, offset, val & 0xffff);
		break;
	case 3:
		bus_space_write_2(bst, bsh, offset, val & 0xffff);
		bus_space_write_1(bst, bsh, offset + 2, (val >> 16) & 0xff);
		break;
	case 4:
		bus_space_write_4(bst, bsh, offset, val);
		break;
	default:
		ACPI_DEVPRINTF("acpi_register_output(): invalid size\n");
		break;
	}
}

static __inline void
acpi_io_mirreg(acpi_softc_t *sc, boolean_t io, u_int32_t *data, 
	       int res, int altres, int offset, int size)
{
	u_int32_t	result;

	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, res, offset, &result, size);
		*data = result;
		acpi_register_input(sc, altres, offset, &result, size);
		*data |= result;
	} else {
		acpi_register_output(sc, res, offset, data, size);
		acpi_register_output(sc, altres, offset, data, size);
	}

	return;
}

void
acpi_enable_disable(acpi_softc_t *sc, boolean_t enable)
{
	u_int8_t	val;

	val = enable ? sc->facp_body->acpi_enable : sc->facp_body->acpi_disable;
	bus_space_write_1(sc->iores[ACPI_RES_SMI_CMD].btag,
			  sc->iores[ACPI_RES_SMI_CMD].bhandle,
			  0, val);
	sc->enabled = enable;

	ACPI_DEBUGPRINT("acpi_enable_disable(%d) = (%x)\n", enable, val);
}

void
acpi_io_pm1_status(acpi_softc_t *sc, boolean_t io, u_int32_t *status)
{
	int		size;
	struct FACPbody	*facp;

	facp = sc->facp_body;
	size = facp->pm1_evt_len / 2;
	acpi_io_mirreg(sc, io, status, ACPI_RES_PM1A_EVT, ACPI_RES_PM1B_EVT, 0, size);

	ACPI_DEBUGPRINT("acpi_io_pm1_status(%d) = (%x)\n", io, *status);
}

void
acpi_io_pm1_enable(acpi_softc_t *sc, boolean_t io, u_int32_t *enable)
{
	int		size;
	struct FACPbody	*facp;

	facp = sc->facp_body;
	size = facp->pm1_evt_len / 2;
	acpi_io_mirreg(sc, io, enable, ACPI_RES_PM1A_EVT, ACPI_RES_PM1B_EVT, size, size);

	ACPI_DEBUGPRINT("acpi_io_pm1_enable(%d) = (%x)\n", io, *enable);
}

/*
 * PM1 is awkward because the SLP_TYP bits are not common between the two registers.
 * A better interface than this might pass the SLP_TYP bits separately.
 */
void
acpi_io_pm1_control(acpi_softc_t *sc, boolean_t io, u_int32_t *value_a, u_int32_t *value_b)
{
	struct FACPbody	*facp;
	u_int32_t	result;

	facp = sc->facp_body;

	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, ACPI_RES_PM1A_CNT, 0, &result, facp->pm1_cnt_len);
		*value_a = result;
		acpi_register_input(sc, ACPI_RES_PM1B_CNT, 0, &result, facp->pm1_cnt_len);
		*value_a |= result;
		*value_a &= ~ACPI_CNT_SLP_TYPX;	/* mask the SLP_TYP bits */
	} else {
		acpi_register_output(sc, ACPI_RES_PM1A_CNT, 0, value_a, facp->pm1_cnt_len);
		acpi_register_output(sc, ACPI_RES_PM1B_CNT, 0, value_b, facp->pm1_cnt_len);
	}

	ACPI_DEBUGPRINT("acpi_io_pm1_control(%d) = (%x, %x)\n", io, *value_a, *value_b);
}

void
acpi_io_pm2_control(acpi_softc_t *sc, boolean_t io, u_int32_t *val)
{
	int		size;
	struct FACPbody	*facp;

	facp = sc->facp_body;
	size = facp->pm2_cnt_len;

	if (size == 0)			/* port is optional */
	    return;

	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, ACPI_RES_PM2_CNT, 0, val, size);
	} else {
		acpi_register_output(sc, ACPI_RES_PM2_CNT, 0, val, size);
	}

	ACPI_DEBUGPRINT("acpi_io_pm2_control(%d) = (%x)\n", io, *val);

	return;
}

void
acpi_io_pm_timer(acpi_softc_t *sc, boolean_t io, u_int32_t *val)
{
	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, ACPI_RES_PM_TMR, 0, val, sizeof(u_int32_t));

		ACPI_DEBUGPRINT("acpi_io_pm_timer(%d) = (%x)\n", io, *val);
	}
}

void
acpi_io_gpe0_status(acpi_softc_t *sc, boolean_t io, u_int32_t *val)
{
	int		size;
	struct	FACPbody *facp;

	facp = sc->facp_body;
	size = facp->gpe0_len / 2;

	if (size == 0)			/* port is optional */
	    return;

	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, ACPI_RES_GPE0, 0, val, size);
	} else {
		acpi_register_output(sc, ACPI_RES_GPE0, 0, val, size);
	}

	ACPI_DEBUGPRINT("acpi_io_gpe0_status(%d) = (%x)\n", io, *val);
}

void
acpi_io_gpe0_enable(acpi_softc_t *sc, boolean_t io, u_int32_t *val)
{
	int		size;
	struct	FACPbody *facp;

	facp = sc->facp_body;
	size = facp->gpe0_len / 2;

	if (size == 0)			/* port is optional */
	    return;

	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, ACPI_RES_GPE0, size, val, size);
	} else {
		acpi_register_output(sc, ACPI_RES_GPE0, size, val, size);
	}

	ACPI_DEBUGPRINT("acpi_io_gpe0_enable(%d) = (%x)\n", io, *val);
}

void
acpi_io_gpe1_status(acpi_softc_t *sc, boolean_t io, u_int32_t *val)
{
	int		size;
	struct	FACPbody *facp;

	facp = sc->facp_body;
	size = facp->gpe1_len / 2;

	if (size == 0)			/* port is optional */
	    return;

	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, ACPI_RES_GPE1, 0, val, size);
	} else {
		acpi_register_output(sc, ACPI_RES_GPE1, 0, val, size);
	}

	ACPI_DEBUGPRINT("acpi_io_gpe1_status(%d) = (%x)\n", io, *val);
}

void
acpi_io_gpe1_enable(acpi_softc_t *sc, boolean_t io, u_int32_t *val)
{
	int		size;
	struct	FACPbody *facp;

	facp = sc->facp_body;
	size = facp->gpe1_len / 2;

	if (size == 0)			/* port is optional */
	    return;

	if (io == ACPI_REGISTER_INPUT) {
		acpi_register_input(sc, ACPI_RES_GPE1, size, val, size);
	} else {
		acpi_register_output(sc, ACPI_RES_GPE1, size, val, size);
	}

	ACPI_DEBUGPRINT("acpi_io_gpe0_enable(%d) = (%x)\n", io, *val);
}

void
acpi_gpe_enable_bit(acpi_softc_t *sc, u_int32_t bit, boolean_t on_off)
{
	u_int32_t	value;
	int		res;

	/*
	 * Is the bit in the first GPE port?
	 */
	if (bit < ((sc->facp_body->gpe0_len / 2) * 8)) {
		res = ACPI_RES_GPE0;
	} else {
		/*
		 * Is the bit in the second GPE port?
		 */
		bit -= sc->facp_body->gpe1_base;
		if (bit < ((sc->facp_body->gpe1_len / 2) * 8)) {
			res = ACPI_RES_GPE1;
		} else {
			return;	/* do nothing */
		}
	}

	switch (res) {
	case ACPI_RES_GPE0:
		acpi_io_gpe0_enable(sc, ACPI_REGISTER_INPUT, &value);
		break;
	case ACPI_RES_GPE1:
		acpi_io_gpe1_enable(sc, ACPI_REGISTER_INPUT, &value);
		break;
	}
	value = (value & ~(1 << bit)) | (on_off ? (1 << bit) : 0);
	switch (res) {
	case ACPI_RES_GPE0:
		acpi_io_gpe0_enable(sc, ACPI_REGISTER_OUTPUT, &value);
		break;
	case ACPI_RES_GPE1:
		acpi_io_gpe1_enable(sc, ACPI_REGISTER_OUTPUT, &value);
		break;
	}
}

OpenPOWER on IntegriCloud