hwregs.c 23.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9

/*******************************************************************************
 *
 * Module Name: hwregs - Read/write access functions for the various ACPI
 *                       control and status registers.
 *
 ******************************************************************************/

/*
10 11
 * Copyright (C) 2000 - 2003, R. Byron Moore
 * All rights reserved.
Linus Torvalds's avatar
Linus Torvalds committed
12
 *
13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 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,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
Linus Torvalds's avatar
Linus Torvalds committed
27
 *
28 29 30
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
Linus Torvalds's avatar
Linus Torvalds committed
31
 *
32 33 34 35 36 37 38 39 40 41 42 43
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES.
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46
 */


47 48
#include <acpi/acpi.h>
#include <acpi/acnamesp.h>
Linus Torvalds's avatar
Linus Torvalds committed
49

Linus Torvalds's avatar
Linus Torvalds committed
50
#define _COMPONENT          ACPI_HARDWARE
Andy Grover's avatar
Andy Grover committed
51
	 ACPI_MODULE_NAME    ("hwregs")
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54 55


/*******************************************************************************
 *
56
 * FUNCTION:    acpi_hw_clear_acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59 60 61 62 63 64 65
 *
 * PARAMETERS:  none
 *
 * RETURN:      none
 *
 * DESCRIPTION: Clears all fixed and general purpose status bits
 *
 ******************************************************************************/

66
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
67 68
acpi_hw_clear_acpi_status (void)
{
69 70
	acpi_native_uint                i;
	acpi_status                     status;
Andy Grover's avatar
Andy Grover committed
71
	struct acpi_gpe_block_info      *gpe_block;
Linus Torvalds's avatar
Linus Torvalds committed
72 73


74
	ACPI_FUNCTION_TRACE ("hw_clear_acpi_status");
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77


	ACPI_DEBUG_PRINT ((ACPI_DB_IO, "About to write %04X to %04X\n",
Andy Grover's avatar
Andy Grover committed
78
		ACPI_BITMASK_ALL_FIXED_STATUS,
79
		(u16) acpi_gbl_FADT->xpm1a_evt_blk.address));
Linus Torvalds's avatar
Linus Torvalds committed
80 81


Andy Grover's avatar
Andy Grover committed
82 83
	status = acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
	if (ACPI_FAILURE (status)) {
84
		return_ACPI_STATUS (status);
Andy Grover's avatar
Andy Grover committed
85
	}
Linus Torvalds's avatar
Linus Torvalds committed
86

87 88 89 90 91
	status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, ACPI_REGISTER_PM1_STATUS,
			  ACPI_BITMASK_ALL_FIXED_STATUS);
	if (ACPI_FAILURE (status)) {
		goto unlock_and_exit;
	}
Linus Torvalds's avatar
Linus Torvalds committed
92

Andy Grover's avatar
Andy Grover committed
93
	/* Clear the fixed events */
Linus Torvalds's avatar
Linus Torvalds committed
94

95
	if (acpi_gbl_FADT->xpm1b_evt_blk.address) {
96
		status = acpi_hw_low_level_write (16, ACPI_BITMASK_ALL_FIXED_STATUS,
97
				 &acpi_gbl_FADT->xpm1b_evt_blk, 0);
98 99 100
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}
Linus Torvalds's avatar
Linus Torvalds committed
101 102
	}

Andy Grover's avatar
Andy Grover committed
103
	/* Clear the GPE Bits in all GPE registers in all GPE blocks */
Linus Torvalds's avatar
Linus Torvalds committed
104

Andy Grover's avatar
Andy Grover committed
105 106 107
	gpe_block = acpi_gbl_gpe_block_list_head;
	while (gpe_block) {
		for (i = 0; i < gpe_block->register_count; i++) {
108
			status = acpi_hw_low_level_write (8, 0xFF,
Andy Grover's avatar
Andy Grover committed
109
					 &gpe_block->register_info[i].status_address, (u32) i);
110 111 112
			if (ACPI_FAILURE (status)) {
				goto unlock_and_exit;
			}
Linus Torvalds's avatar
Linus Torvalds committed
113
		}
Andy Grover's avatar
Andy Grover committed
114 115

		gpe_block = gpe_block->next;
Linus Torvalds's avatar
Linus Torvalds committed
116 117
	}

118
unlock_and_exit:
Andy Grover's avatar
Andy Grover committed
119
	(void) acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
120
	return_ACPI_STATUS (status);
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125
}


/*******************************************************************************
 *
126
 * FUNCTION:    acpi_get_sleep_type_data
Linus Torvalds's avatar
Linus Torvalds committed
127
 *
128 129 130
 * PARAMETERS:  sleep_state         - Numeric sleep state
 *              *sleep_type_a        - Where SLP_TYPa is returned
 *              *sleep_type_b        - Where SLP_TYPb is returned
Linus Torvalds's avatar
Linus Torvalds committed
131 132 133
 *
 * RETURN:      Status - ACPI status
 *
Andy Grover's avatar
Andy Grover committed
134 135
 * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested sleep
 *              state.
Linus Torvalds's avatar
Linus Torvalds committed
136 137 138
 *
 ******************************************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
139
acpi_status
140
acpi_get_sleep_type_data (
141 142 143
	u8                              sleep_state,
	u8                              *sleep_type_a,
	u8                              *sleep_type_b)
Linus Torvalds's avatar
Linus Torvalds committed
144
{
145 146
	acpi_status                     status = AE_OK;
	union acpi_operand_object       *obj_desc;
Linus Torvalds's avatar
Linus Torvalds committed
147 148


149
	ACPI_FUNCTION_TRACE ("acpi_get_sleep_type_data");
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152


	/*
Andy Grover's avatar
Andy Grover committed
153
	 * Validate parameters
Linus Torvalds's avatar
Linus Torvalds committed
154 155
	 */
	if ((sleep_state > ACPI_S_STATES_MAX) ||
Andy Grover's avatar
Andy Grover committed
156
		!sleep_type_a || !sleep_type_b) {
Linus Torvalds's avatar
Linus Torvalds committed
157
		return_ACPI_STATUS (AE_BAD_PARAMETER);
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160
	}

	/*
Andy Grover's avatar
Andy Grover committed
161
	 * Evaluate the namespace object containing the values for this state
Linus Torvalds's avatar
Linus Torvalds committed
162
	 */
Andy Grover's avatar
Andy Grover committed
163
	status = acpi_ns_evaluate_by_name ((char *) acpi_gbl_db_sleep_states[sleep_state],
Linus Torvalds's avatar
Linus Torvalds committed
164
			  NULL, &obj_desc);
Linus Torvalds's avatar
Linus Torvalds committed
165
	if (ACPI_FAILURE (status)) {
166
		ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s while evaluating sleep_state [%s]\n",
167 168
			acpi_format_exception (status), acpi_gbl_db_sleep_states[sleep_state]));

Linus Torvalds's avatar
Linus Torvalds committed
169
		return_ACPI_STATUS (status);
Linus Torvalds's avatar
Linus Torvalds committed
170 171
	}

Andy Grover's avatar
Andy Grover committed
172 173
	/* Must have a return object */

Linus Torvalds's avatar
Linus Torvalds committed
174
	if (!obj_desc) {
Andy Grover's avatar
Andy Grover committed
175
		ACPI_REPORT_ERROR (("Missing Sleep State object\n"));
Andy Grover's avatar
Andy Grover committed
176
		status = AE_NOT_EXIST;
Linus Torvalds's avatar
Linus Torvalds committed
177 178
	}

Andy Grover's avatar
Andy Grover committed
179
	/* It must be of type Package */
Linus Torvalds's avatar
Linus Torvalds committed
180

Andy Grover's avatar
Andy Grover committed
181 182 183 184
	else if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_PACKAGE) {
		ACPI_REPORT_ERROR (("Sleep State object not a Package\n"));
		status = AE_AML_OPERAND_TYPE;
	}
Linus Torvalds's avatar
Linus Torvalds committed
185

Andy Grover's avatar
Andy Grover committed
186
	/* The package must have at least two elements */
Linus Torvalds's avatar
Linus Torvalds committed
187

Andy Grover's avatar
Andy Grover committed
188
	else if (obj_desc->package.count < 2) {
Andy Grover's avatar
Andy Grover committed
189 190
		ACPI_REPORT_ERROR (("Sleep State package does not have at least two elements\n"));
		status = AE_AML_NO_OPERAND;
Linus Torvalds's avatar
Linus Torvalds committed
191 192
	}

Andy Grover's avatar
Andy Grover committed
193 194 195 196 197 198 199
	/* The first two elements must both be of type Integer */

	else if ((ACPI_GET_OBJECT_TYPE (obj_desc->package.elements[0]) != ACPI_TYPE_INTEGER) ||
			 (ACPI_GET_OBJECT_TYPE (obj_desc->package.elements[1]) != ACPI_TYPE_INTEGER)) {
		ACPI_REPORT_ERROR (("Sleep State package elements are not both Integers (%s, %s)\n",
			acpi_ut_get_object_type_name (obj_desc->package.elements[0]),
			acpi_ut_get_object_type_name (obj_desc->package.elements[1])));
Andy Grover's avatar
Andy Grover committed
200
		status = AE_AML_OPERAND_TYPE;
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203
	}
	else {
		/*
Andy Grover's avatar
Andy Grover committed
204
		 * Valid _Sx_ package size, type, and value
Linus Torvalds's avatar
Linus Torvalds committed
205
		 */
Andy Grover's avatar
Andy Grover committed
206 207
		*sleep_type_a = (u8) (obj_desc->package.elements[0])->integer.value;
		*sleep_type_b = (u8) (obj_desc->package.elements[1])->integer.value;
Linus Torvalds's avatar
Linus Torvalds committed
208 209
	}

Linus Torvalds's avatar
Linus Torvalds committed
210
	if (ACPI_FAILURE (status)) {
211
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "While evaluating sleep_state [%s], bad Sleep object %p type %s\n",
212
			acpi_gbl_db_sleep_states[sleep_state], obj_desc, acpi_ut_get_object_type_name (obj_desc)));
Linus Torvalds's avatar
Linus Torvalds committed
213
	}
Linus Torvalds's avatar
Linus Torvalds committed
214

Linus Torvalds's avatar
Linus Torvalds committed
215
	acpi_ut_remove_reference (obj_desc);
Linus Torvalds's avatar
Linus Torvalds committed
216
	return_ACPI_STATUS (status);
Linus Torvalds's avatar
Linus Torvalds committed
217 218 219 220 221
}


/*******************************************************************************
 *
222
 * FUNCTION:    acpi_hw_get_register_bit_mask
Linus Torvalds's avatar
Linus Torvalds committed
223
 *
224
 * PARAMETERS:  register_id         - Index of ACPI Register to access
Linus Torvalds's avatar
Linus Torvalds committed
225
 *
Andy Grover's avatar
Andy Grover committed
226
 * RETURN:      The bit mask to be used when accessing the register
Linus Torvalds's avatar
Linus Torvalds committed
227
 *
228
 * DESCRIPTION: Map register_id into a register bit mask.
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231
 *
 ******************************************************************************/

232
struct acpi_bit_register_info *
Andy Grover's avatar
Andy Grover committed
233
acpi_hw_get_bit_register_info (
234
	u32                             register_id)
Linus Torvalds's avatar
Linus Torvalds committed
235
{
236
	ACPI_FUNCTION_NAME ("hw_get_bit_register_info");
Linus Torvalds's avatar
Linus Torvalds committed
237 238


Andy Grover's avatar
Andy Grover committed
239
	if (register_id > ACPI_BITREG_MAX) {
240
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid bit_register ID: %X\n", register_id));
Andy Grover's avatar
Andy Grover committed
241 242
		return (NULL);
	}
Linus Torvalds's avatar
Linus Torvalds committed
243

Andy Grover's avatar
Andy Grover committed
244 245
	return (&acpi_gbl_bit_register_info[register_id]);
}
Linus Torvalds's avatar
Linus Torvalds committed
246

Linus Torvalds's avatar
Linus Torvalds committed
247

Andy Grover's avatar
Andy Grover committed
248 249
/*******************************************************************************
 *
250
 * FUNCTION:    acpi_get_register
Andy Grover's avatar
Andy Grover committed
251
 *
252 253
 * PARAMETERS:  register_id         - Index of ACPI Register to access
 *              use_lock            - Lock the hardware
Andy Grover's avatar
Andy Grover committed
254 255 256 257
 *
 * RETURN:      Value is read from specified Register.  Value returned is
 *              normalized to bit0 (is shifted all the way right)
 *
258
 * DESCRIPTION: ACPI bit_register read function.
Andy Grover's avatar
Andy Grover committed
259 260
 *
 ******************************************************************************/
Linus Torvalds's avatar
Linus Torvalds committed
261

262 263
acpi_status
acpi_get_register (
264 265 266
	u32                             register_id,
	u32                             *return_value,
	u32                             flags)
Andy Grover's avatar
Andy Grover committed
267
{
268 269 270
	u32                             register_value = 0;
	struct acpi_bit_register_info   *bit_reg_info;
	acpi_status                     status;
Linus Torvalds's avatar
Linus Torvalds committed
271 272


273
	ACPI_FUNCTION_TRACE ("acpi_get_register");
Linus Torvalds's avatar
Linus Torvalds committed
274 275


Andy Grover's avatar
Andy Grover committed
276
	/* Get the info structure corresponding to the requested ACPI Register */
Linus Torvalds's avatar
Linus Torvalds committed
277

Andy Grover's avatar
Andy Grover committed
278 279 280 281
	bit_reg_info = acpi_hw_get_bit_register_info (register_id);
	if (!bit_reg_info) {
		return_ACPI_STATUS (AE_BAD_PARAMETER);
	}
Linus Torvalds's avatar
Linus Torvalds committed
282

283 284 285 286 287 288 289 290 291
	if (flags & ACPI_MTX_LOCK) {
		status = acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
		if (ACPI_FAILURE (status)) {
			return_ACPI_STATUS (status);
		}
	}

	status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
			  bit_reg_info->parent_register, &register_value);
Linus Torvalds's avatar
Linus Torvalds committed
292

Andy Grover's avatar
Andy Grover committed
293 294 295
	if (flags & ACPI_MTX_LOCK) {
		(void) acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
	}
Linus Torvalds's avatar
Linus Torvalds committed
296

297 298
	if (ACPI_SUCCESS (status)) {
		/* Normalize the value that was read */
Linus Torvalds's avatar
Linus Torvalds committed
299

300 301
		register_value = ((register_value & bit_reg_info->access_bit_mask)
				   >> bit_reg_info->bit_position);
Linus Torvalds's avatar
Linus Torvalds committed
302

303 304 305 306 307 308
		*return_value = register_value;

		ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Read value %X\n", register_value));
	}

	return_ACPI_STATUS (status);
Andy Grover's avatar
Andy Grover committed
309
}
Linus Torvalds's avatar
Linus Torvalds committed
310 311


Andy Grover's avatar
Andy Grover committed
312 313
/*******************************************************************************
 *
314
 * FUNCTION:    acpi_set_register
Andy Grover's avatar
Andy Grover committed
315
 *
316
 * PARAMETERS:  register_id     - ID of ACPI bit_register to access
Andy Grover's avatar
Andy Grover committed
317 318 319 320
 *              Value           - (only used on write) value to write to the
 *                                Register, NOT pre-normalized to the bit pos.
 *              Flags           - Lock the hardware or not
 *
321
 * RETURN:      None
Andy Grover's avatar
Andy Grover committed
322 323 324 325
 *
 * DESCRIPTION: ACPI Bit Register write function.
 *
 ******************************************************************************/
Linus Torvalds's avatar
Linus Torvalds committed
326

327 328
acpi_status
acpi_set_register (
329 330 331
	u32                             register_id,
	u32                             value,
	u32                             flags)
Andy Grover's avatar
Andy Grover committed
332
{
333 334 335
	u32                             register_value = 0;
	struct acpi_bit_register_info   *bit_reg_info;
	acpi_status                     status;
Linus Torvalds's avatar
Linus Torvalds committed
336 337


338
	ACPI_FUNCTION_TRACE_U32 ("acpi_set_register", register_id);
Linus Torvalds's avatar
Linus Torvalds committed
339 340


Andy Grover's avatar
Andy Grover committed
341
	/* Get the info structure corresponding to the requested ACPI Register */
Linus Torvalds's avatar
Linus Torvalds committed
342

Andy Grover's avatar
Andy Grover committed
343 344
	bit_reg_info = acpi_hw_get_bit_register_info (register_id);
	if (!bit_reg_info) {
345
		ACPI_REPORT_ERROR (("Bad ACPI HW register_id: %X\n", register_id));
Andy Grover's avatar
Andy Grover committed
346 347
		return_ACPI_STATUS (AE_BAD_PARAMETER);
	}
Linus Torvalds's avatar
Linus Torvalds committed
348

349 350 351 352 353 354 355
	if (flags & ACPI_MTX_LOCK) {
		status = acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
		if (ACPI_FAILURE (status)) {
			return_ACPI_STATUS (status);
		}
	}

Andy Grover's avatar
Andy Grover committed
356
	/* Always do a register read first so we can insert the new bits  */
Linus Torvalds's avatar
Linus Torvalds committed
357

358 359 360 361 362
	status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
			  bit_reg_info->parent_register, &register_value);
	if (ACPI_FAILURE (status)) {
		goto unlock_and_exit;
	}
Linus Torvalds's avatar
Linus Torvalds committed
363

Andy Grover's avatar
Andy Grover committed
364 365 366 367 368 369 370 371 372
	/*
	 * Decode the Register ID
	 * Register id = Register block id | bit id
	 *
	 * Check bit id to fine locate Register offset.
	 * Check Mask to determine Register offset, and then read-write.
	 */
	switch (bit_reg_info->parent_register) {
	case ACPI_REGISTER_PM1_STATUS:
Linus Torvalds's avatar
Linus Torvalds committed
373

Andy Grover's avatar
Andy Grover committed
374 375
		/*
		 * Status Registers are different from the rest.  Clear by
376
		 * writing 1, writing 0 has no effect.  So, the only relevant
Andy Grover's avatar
Andy Grover committed
377 378 379
		 * information is the single bit we're interested in, all others should
		 * be written as 0 so they will be left unchanged
		 */
380 381
		value = ACPI_REGISTER_PREPARE_BITS (value,
				 bit_reg_info->bit_position, bit_reg_info->access_bit_mask);
Andy Grover's avatar
Andy Grover committed
382
		if (value) {
383 384
			status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
					 ACPI_REGISTER_PM1_STATUS, (u16) value);
Andy Grover's avatar
Andy Grover committed
385 386 387
			register_value = 0;
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
388 389


Andy Grover's avatar
Andy Grover committed
390
	case ACPI_REGISTER_PM1_ENABLE:
Linus Torvalds's avatar
Linus Torvalds committed
391

392 393
		ACPI_REGISTER_INSERT_VALUE (register_value, bit_reg_info->bit_position,
				bit_reg_info->access_bit_mask, value);
Linus Torvalds's avatar
Linus Torvalds committed
394

395 396
		status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
				  ACPI_REGISTER_PM1_ENABLE, (u16) register_value);
Andy Grover's avatar
Andy Grover committed
397
		break;
Linus Torvalds's avatar
Linus Torvalds committed
398 399


Andy Grover's avatar
Andy Grover committed
400
	case ACPI_REGISTER_PM1_CONTROL:
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403 404

		/*
		 * Read the PM1 Control register.
		 * Note that at this level, the fact that there are actually TWO
Andy Grover's avatar
Andy Grover committed
405
		 * registers (A and B - and that B may not exist) is abstracted.
Linus Torvalds's avatar
Linus Torvalds committed
406
		 */
Linus Torvalds's avatar
Linus Torvalds committed
407 408
		ACPI_DEBUG_PRINT ((ACPI_DB_IO, "PM1 control: Read %X\n", register_value));

409 410
		ACPI_REGISTER_INSERT_VALUE (register_value, bit_reg_info->bit_position,
				bit_reg_info->access_bit_mask, value);
Linus Torvalds's avatar
Linus Torvalds committed
411

412
		status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, register_id,
Andy Grover's avatar
Andy Grover committed
413 414
				(u16) register_value);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
415 416


Andy Grover's avatar
Andy Grover committed
417
	case ACPI_REGISTER_PM2_CONTROL:
Linus Torvalds's avatar
Linus Torvalds committed
418

419 420 421 422 423
		status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
				 ACPI_REGISTER_PM2_CONTROL, &register_value);
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}
Linus Torvalds's avatar
Linus Torvalds committed
424

Linus Torvalds's avatar
Linus Torvalds committed
425
		ACPI_DEBUG_PRINT ((ACPI_DB_IO, "PM2 control: Read %X from %8.8X%8.8X\n",
426
			register_value,
427 428
			ACPI_HIDWORD (acpi_gbl_FADT->xpm2_cnt_blk.address),
			ACPI_LODWORD (acpi_gbl_FADT->xpm2_cnt_blk.address)));
Linus Torvalds's avatar
Linus Torvalds committed
429

430 431
		ACPI_REGISTER_INSERT_VALUE (register_value, bit_reg_info->bit_position,
				bit_reg_info->access_bit_mask, value);
Linus Torvalds's avatar
Linus Torvalds committed
432

433
		ACPI_DEBUG_PRINT ((ACPI_DB_IO, "About to write %4.4X to %8.8X%8.8X\n",
Linus Torvalds's avatar
Linus Torvalds committed
434
			register_value,
435 436
			ACPI_HIDWORD (acpi_gbl_FADT->xpm2_cnt_blk.address),
			ACPI_LODWORD (acpi_gbl_FADT->xpm2_cnt_blk.address)));
Linus Torvalds's avatar
Linus Torvalds committed
437

438
		status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
Andy Grover's avatar
Andy Grover committed
439
				   ACPI_REGISTER_PM2_CONTROL, (u8) (register_value));
Linus Torvalds's avatar
Linus Torvalds committed
440 441 442 443 444 445 446
		break;


	default:
		break;
	}

447 448 449

unlock_and_exit:

Andy Grover's avatar
Andy Grover committed
450 451
	if (flags & ACPI_MTX_LOCK) {
		(void) acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
Linus Torvalds's avatar
Linus Torvalds committed
452 453
	}

Andy Grover's avatar
Andy Grover committed
454
	/* Normalize the value that was read */
Linus Torvalds's avatar
Linus Torvalds committed
455

456
	ACPI_DEBUG_EXEC (register_value = ((register_value & bit_reg_info->access_bit_mask) >> bit_reg_info->bit_position));
Linus Torvalds's avatar
Linus Torvalds committed
457

458 459
	ACPI_DEBUG_PRINT ((ACPI_DB_IO, "ACPI Register Write actual %X\n", register_value));
	return_ACPI_STATUS (status);
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464
}


/******************************************************************************
 *
465
 * FUNCTION:    acpi_hw_register_read
Linus Torvalds's avatar
Linus Torvalds committed
466
 *
467 468
 * PARAMETERS:  use_lock               - Mutex hw access.
 *              register_id            - register_iD + Offset.
Linus Torvalds's avatar
Linus Torvalds committed
469 470 471 472 473 474 475 476
 *
 * RETURN:      Value read or written.
 *
 * DESCRIPTION: Acpi register read function.  Registers are read at the
 *              given offset.
 *
 ******************************************************************************/

477
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
478
acpi_hw_register_read (
479 480 481
	u8                              use_lock,
	u32                             register_id,
	u32                             *return_value)
Linus Torvalds's avatar
Linus Torvalds committed
482
{
483 484 485 486
	u32                             value1 = 0;
	u32                             value2 = 0;
	u32                             bank_offset;
	acpi_status                     status;
Linus Torvalds's avatar
Linus Torvalds committed
487

Linus Torvalds's avatar
Linus Torvalds committed
488

489
	ACPI_FUNCTION_TRACE ("hw_register_read");
Linus Torvalds's avatar
Linus Torvalds committed
490 491


Linus Torvalds's avatar
Linus Torvalds committed
492
	if (ACPI_MTX_LOCK == use_lock) {
493 494 495
		status = acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
		if (ACPI_FAILURE (status)) {
			return_ACPI_STATUS (status);
Andy Grover's avatar
Andy Grover committed
496
		}
Linus Torvalds's avatar
Linus Torvalds committed
497 498
	}

Andy Grover's avatar
Andy Grover committed
499 500
	switch (register_id) {
	case ACPI_REGISTER_PM1_STATUS:           /* 16-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
501

502
		status = acpi_hw_low_level_read (16, &value1, &acpi_gbl_FADT->xpm1a_evt_blk, 0);
503 504 505 506
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}

507
		status = acpi_hw_low_level_read (16, &value2, &acpi_gbl_FADT->xpm1b_evt_blk, 0);
508
		value1 |= value2;
Linus Torvalds's avatar
Linus Torvalds committed
509 510 511
		break;


Andy Grover's avatar
Andy Grover committed
512
	case ACPI_REGISTER_PM1_ENABLE:           /* 16-bit access*/
Linus Torvalds's avatar
Linus Torvalds committed
513

Andy Grover's avatar
Andy Grover committed
514
		bank_offset = ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len);
515
		status = acpi_hw_low_level_read (16, &value1, &acpi_gbl_FADT->xpm1a_evt_blk, bank_offset);
516 517 518 519
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}

520
		status = acpi_hw_low_level_read (16, &value2, &acpi_gbl_FADT->xpm1b_evt_blk, bank_offset);
521
		value1 |= value2;
Linus Torvalds's avatar
Linus Torvalds committed
522 523 524
		break;


Andy Grover's avatar
Andy Grover committed
525
	case ACPI_REGISTER_PM1_CONTROL:          /* 16-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
526

527
		status = acpi_hw_low_level_read (16, &value1, &acpi_gbl_FADT->xpm1a_cnt_blk, 0);
528 529 530 531
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}

532
		status = acpi_hw_low_level_read (16, &value2, &acpi_gbl_FADT->xpm1b_cnt_blk, 0);
533
		value1 |= value2;
Linus Torvalds's avatar
Linus Torvalds committed
534 535 536
		break;


Andy Grover's avatar
Andy Grover committed
537
	case ACPI_REGISTER_PM2_CONTROL:          /* 8-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
538

539
		status = acpi_hw_low_level_read (8, &value1, &acpi_gbl_FADT->xpm2_cnt_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542
		break;


Andy Grover's avatar
Andy Grover committed
543
	case ACPI_REGISTER_PM_TIMER:             /* 32-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
544

545
		status = acpi_hw_low_level_read (32, &value1, &acpi_gbl_FADT->xpm_tmr_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
546 547
		break;

Andy Grover's avatar
Andy Grover committed
548
	case ACPI_REGISTER_SMI_COMMAND_BLOCK:    /* 8-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
549

550
		status = acpi_os_read_port (acpi_gbl_FADT->smi_cmd, &value1, 8);
Linus Torvalds's avatar
Linus Torvalds committed
551 552 553
		break;

	default:
554 555
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown Register ID: %X\n", register_id));
		status = AE_BAD_PARAMETER;
Linus Torvalds's avatar
Linus Torvalds committed
556 557 558
		break;
	}

559
unlock_and_exit:
Linus Torvalds's avatar
Linus Torvalds committed
560
	if (ACPI_MTX_LOCK == use_lock) {
Andy Grover's avatar
Andy Grover committed
561
		(void) acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
Linus Torvalds's avatar
Linus Torvalds committed
562 563
	}

564 565 566 567 568
	if (ACPI_SUCCESS (status)) {
		*return_value = value1;
	}

	return_ACPI_STATUS (status);
Linus Torvalds's avatar
Linus Torvalds committed
569 570 571 572 573
}


/******************************************************************************
 *
574
 * FUNCTION:    acpi_hw_register_write
Linus Torvalds's avatar
Linus Torvalds committed
575
 *
576 577
 * PARAMETERS:  use_lock               - Mutex hw access.
 *              register_id            - register_iD + Offset.
Linus Torvalds's avatar
Linus Torvalds committed
578 579 580 581 582 583 584 585
 *
 * RETURN:      Value read or written.
 *
 * DESCRIPTION: Acpi register Write function.  Registers are written at the
 *              given offset.
 *
 ******************************************************************************/

586
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
587
acpi_hw_register_write (
588 589 590
	u8                              use_lock,
	u32                             register_id,
	u32                             value)
Linus Torvalds's avatar
Linus Torvalds committed
591
{
592 593
	u32                             bank_offset;
	acpi_status                     status;
Linus Torvalds's avatar
Linus Torvalds committed
594 595


596
	ACPI_FUNCTION_TRACE ("hw_register_write");
Linus Torvalds's avatar
Linus Torvalds committed
597 598


Linus Torvalds's avatar
Linus Torvalds committed
599
	if (ACPI_MTX_LOCK == use_lock) {
600 601 602
		status = acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
		if (ACPI_FAILURE (status)) {
			return_ACPI_STATUS (status);
Andy Grover's avatar
Andy Grover committed
603
		}
Linus Torvalds's avatar
Linus Torvalds committed
604 605
	}

Andy Grover's avatar
Andy Grover committed
606 607
	switch (register_id) {
	case ACPI_REGISTER_PM1_STATUS:           /* 16-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
608

609
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1a_evt_blk, 0);
610 611 612 613
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}

614
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1b_evt_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
615 616 617
		break;


Andy Grover's avatar
Andy Grover committed
618
	case ACPI_REGISTER_PM1_ENABLE:           /* 16-bit access*/
Linus Torvalds's avatar
Linus Torvalds committed
619

Andy Grover's avatar
Andy Grover committed
620
		bank_offset = ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len);
621
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1a_evt_blk, bank_offset);
622 623 624 625
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}

626
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1b_evt_blk, bank_offset);
Linus Torvalds's avatar
Linus Torvalds committed
627 628 629
		break;


Andy Grover's avatar
Andy Grover committed
630
	case ACPI_REGISTER_PM1_CONTROL:          /* 16-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
631

632
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1a_cnt_blk, 0);
633 634 635 636
		if (ACPI_FAILURE (status)) {
			goto unlock_and_exit;
		}

637
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1b_cnt_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
638
		break;
Linus Torvalds's avatar
Linus Torvalds committed
639 640


Andy Grover's avatar
Andy Grover committed
641
	case ACPI_REGISTER_PM1A_CONTROL:         /* 16-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
642

643
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1a_cnt_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
644 645 646
		break;


Andy Grover's avatar
Andy Grover committed
647
	case ACPI_REGISTER_PM1B_CONTROL:         /* 16-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
648

649
		status = acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->xpm1b_cnt_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
650 651 652
		break;


Andy Grover's avatar
Andy Grover committed
653
	case ACPI_REGISTER_PM2_CONTROL:          /* 8-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
654

655
		status = acpi_hw_low_level_write (8, value, &acpi_gbl_FADT->xpm2_cnt_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
656 657 658
		break;


Andy Grover's avatar
Andy Grover committed
659
	case ACPI_REGISTER_PM_TIMER:             /* 32-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
660

661
		status = acpi_hw_low_level_write (32, value, &acpi_gbl_FADT->xpm_tmr_blk, 0);
Linus Torvalds's avatar
Linus Torvalds committed
662 663 664
		break;


Andy Grover's avatar
Andy Grover committed
665
	case ACPI_REGISTER_SMI_COMMAND_BLOCK:    /* 8-bit access */
Linus Torvalds's avatar
Linus Torvalds committed
666

Andy Grover's avatar
Andy Grover committed
667
		/* SMI_CMD is currently always in IO space */
Linus Torvalds's avatar
Linus Torvalds committed
668

669
		status = acpi_os_write_port (acpi_gbl_FADT->smi_cmd, (acpi_integer) value, 8);
Linus Torvalds's avatar
Linus Torvalds committed
670 671 672 673
		break;


	default:
674
		status = AE_BAD_PARAMETER;
Linus Torvalds's avatar
Linus Torvalds committed
675 676 677
		break;
	}

678
unlock_and_exit:
Linus Torvalds's avatar
Linus Torvalds committed
679
	if (ACPI_MTX_LOCK == use_lock) {
Andy Grover's avatar
Andy Grover committed
680
		(void) acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
Linus Torvalds's avatar
Linus Torvalds committed
681 682
	}

683
	return_ACPI_STATUS (status);
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686 687 688
}


/******************************************************************************
 *
689
 * FUNCTION:    acpi_hw_low_level_read
Linus Torvalds's avatar
Linus Torvalds committed
690 691 692 693 694 695 696 697 698 699 700
 *
 * PARAMETERS:  Register            - GAS register structure
 *              Offset              - Offset from the base address in the GAS
 *              Width               - 8, 16, or 32
 *
 * RETURN:      Value read
 *
 * DESCRIPTION: Read from either memory, IO, or PCI config space.
 *
 ******************************************************************************/

701
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
702
acpi_hw_low_level_read (
703 704 705 706
	u32                             width,
	u32                             *value,
	struct acpi_generic_address     *reg,
	u32                             offset)
Linus Torvalds's avatar
Linus Torvalds committed
707
{
708 709 710 711 712
	acpi_physical_address           mem_address;
	acpi_io_address                 io_address;
	struct acpi_pci_id              pci_id;
	u16                             pci_register;
	acpi_status                     status;
Linus Torvalds's avatar
Linus Torvalds committed
713 714


715
	ACPI_FUNCTION_NAME ("hw_low_level_read");
Linus Torvalds's avatar
Linus Torvalds committed
716 717 718 719


	/*
	 * Must have a valid pointer to a GAS structure, and
720 721
	 * a non-zero address within. However, don't return an error
	 * because the PM1A/B code must not fail if B isn't present.
Linus Torvalds's avatar
Linus Torvalds committed
722 723
	 */
	if ((!reg) ||
Andy Grover's avatar
Andy Grover committed
724
		(!reg->address)) {
725
		return (AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
726
	}
727
	*value = 0;
Linus Torvalds's avatar
Linus Torvalds committed
728 729 730 731 732

	/*
	 * Three address spaces supported:
	 * Memory, Io, or PCI config.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
733 734
	switch (reg->address_space_id) {
	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Linus Torvalds's avatar
Linus Torvalds committed
735

Andy Grover's avatar
Andy Grover committed
736
		mem_address = (reg->address
737
				  + (acpi_physical_address) offset);
Linus Torvalds's avatar
Linus Torvalds committed
738

739
		status = acpi_os_read_memory (mem_address, value, width);
Linus Torvalds's avatar
Linus Torvalds committed
740 741 742
		break;


Linus Torvalds's avatar
Linus Torvalds committed
743
	case ACPI_ADR_SPACE_SYSTEM_IO:
Linus Torvalds's avatar
Linus Torvalds committed
744

745 746
		io_address = (acpi_io_address) (reg->address
				   + (acpi_physical_address) offset);
Linus Torvalds's avatar
Linus Torvalds committed
747

748
		status = acpi_os_read_port (io_address, value, width);
Linus Torvalds's avatar
Linus Torvalds committed
749 750 751
		break;


Linus Torvalds's avatar
Linus Torvalds committed
752
	case ACPI_ADR_SPACE_PCI_CONFIG:
Linus Torvalds's avatar
Linus Torvalds committed
753

Linus Torvalds's avatar
Linus Torvalds committed
754 755
		pci_id.segment = 0;
		pci_id.bus     = 0;
Andy Grover's avatar
Andy Grover committed
756 757 758
		pci_id.device  = ACPI_PCI_DEVICE (reg->address);
		pci_id.function = ACPI_PCI_FUNCTION (reg->address);
		pci_register   = (u16) (ACPI_PCI_REGISTER (reg->address)
759 760 761 762 763
				  + offset);

		status = acpi_os_read_pci_configuration (&pci_id, pci_register, value, width);
		break;

Linus Torvalds's avatar
Linus Torvalds committed
764

765 766 767
	default:
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unsupported address space: %X\n", reg->address_space_id));
		status = AE_BAD_PARAMETER;
Linus Torvalds's avatar
Linus Torvalds committed
768 769 770
		break;
	}

771
	return (status);
Linus Torvalds's avatar
Linus Torvalds committed
772 773 774 775 776
}


/******************************************************************************
 *
777
 * FUNCTION:    acpi_hw_low_level_write
Linus Torvalds's avatar
Linus Torvalds committed
778 779 780 781 782 783 784 785 786 787 788 789 790
 *
 * PARAMETERS:  Width               - 8, 16, or 32
 *              Value               - To be written
 *              Register            - GAS register structure
 *              Offset              - Offset from the base address in the GAS
 *
 *
 * RETURN:      Value read
 *
 * DESCRIPTION: Read from either memory, IO, or PCI config space.
 *
 ******************************************************************************/

791
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
792
acpi_hw_low_level_write (
793 794 795 796
	u32                             width,
	u32                             value,
	struct acpi_generic_address     *reg,
	u32                             offset)
Linus Torvalds's avatar
Linus Torvalds committed
797
{
798 799 800 801 802
	acpi_physical_address           mem_address;
	acpi_io_address                 io_address;
	struct acpi_pci_id              pci_id;
	u16                             pci_register;
	acpi_status                     status;
Linus Torvalds's avatar
Linus Torvalds committed
803 804


805
	ACPI_FUNCTION_NAME ("hw_low_level_write");
Linus Torvalds's avatar
Linus Torvalds committed
806 807 808 809


	/*
	 * Must have a valid pointer to a GAS structure, and
810 811
	 * a non-zero address within. However, don't return an error
	 * because the PM1A/B code must not fail if B isn't present.
Linus Torvalds's avatar
Linus Torvalds committed
812 813
	 */
	if ((!reg) ||
Andy Grover's avatar
Andy Grover committed
814
		(!reg->address)) {
815
		return (AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
816 817 818 819 820
	}
	/*
	 * Three address spaces supported:
	 * Memory, Io, or PCI config.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
821 822
	switch (reg->address_space_id) {
	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
Linus Torvalds's avatar
Linus Torvalds committed
823

Andy Grover's avatar
Andy Grover committed
824
		mem_address = (reg->address
825
				  + (acpi_physical_address) offset);
Linus Torvalds's avatar
Linus Torvalds committed
826

827
		status = acpi_os_write_memory (mem_address, (acpi_integer) value, width);
Linus Torvalds's avatar
Linus Torvalds committed
828 829 830
		break;


Linus Torvalds's avatar
Linus Torvalds committed
831
	case ACPI_ADR_SPACE_SYSTEM_IO:
Linus Torvalds's avatar
Linus Torvalds committed
832

833 834
		io_address = (acpi_io_address) (reg->address
				   + (acpi_physical_address) offset);
Linus Torvalds's avatar
Linus Torvalds committed
835

836
		status = acpi_os_write_port (io_address, (acpi_integer) value, width);
Linus Torvalds's avatar
Linus Torvalds committed
837 838 839
		break;


Linus Torvalds's avatar
Linus Torvalds committed
840
	case ACPI_ADR_SPACE_PCI_CONFIG:
Linus Torvalds's avatar
Linus Torvalds committed
841

Linus Torvalds's avatar
Linus Torvalds committed
842 843
		pci_id.segment = 0;
		pci_id.bus     = 0;
Andy Grover's avatar
Andy Grover committed
844 845 846
		pci_id.device  = ACPI_PCI_DEVICE (reg->address);
		pci_id.function = ACPI_PCI_FUNCTION (reg->address);
		pci_register   = (u16) (ACPI_PCI_REGISTER (reg->address)
847
				  + offset);
Linus Torvalds's avatar
Linus Torvalds committed
848

849 850 851 852 853 854 855
		status = acpi_os_write_pci_configuration (&pci_id, pci_register, (acpi_integer) value, width);
		break;


	default:
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unsupported address space: %X\n", reg->address_space_id));
		status = AE_BAD_PARAMETER;
Linus Torvalds's avatar
Linus Torvalds committed
856 857
		break;
	}
858 859

	return (status);
Linus Torvalds's avatar
Linus Torvalds committed
860
}