blob: 165395f1f7159b7fee8cb42d8e7b30cf1367f79f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: hwregs - Read/write access functions for the various ACPI
4 * control and status registers.
5 *
6 ******************************************************************************/
7
8/*
Bob Moore77848132012-01-12 13:27:23 +08009 * Copyright (C) 2000 - 2012, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acnamesp.h"
48#include "acevents.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("hwregs")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Bob Moore33620c52012-02-14 18:14:27 +080053#if (!ACPI_REDUCED_HARDWARE)
Bob Moorec520aba2009-02-18 14:20:12 +080054/* Local Prototypes */
55static acpi_status
56acpi_hw_read_multiple(u32 *value,
57 struct acpi_generic_address *register_a,
58 struct acpi_generic_address *register_b);
59
60static acpi_status
61acpi_hw_write_multiple(u32 value,
62 struct acpi_generic_address *register_a,
63 struct acpi_generic_address *register_b);
64
Bob Moore33620c52012-02-14 18:14:27 +080065#endif /* !ACPI_REDUCED_HARDWARE */
66
Bob Moorec6b57742009-06-24 09:44:06 +080067/******************************************************************************
68 *
69 * FUNCTION: acpi_hw_validate_register
70 *
Bob Mooreba494be2012-07-12 09:40:10 +080071 * PARAMETERS: reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +080072 * max_bit_width - Max bit_width supported (32 or 64)
Bob Mooreba494be2012-07-12 09:40:10 +080073 * address - Pointer to where the gas->address
Bob Moorec6b57742009-06-24 09:44:06 +080074 * is returned
75 *
76 * RETURN: Status
77 *
78 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
79 * pointer, Address, space_id, bit_width, and bit_offset.
80 *
81 ******************************************************************************/
82
83acpi_status
84acpi_hw_validate_register(struct acpi_generic_address *reg,
85 u8 max_bit_width, u64 *address)
86{
87
88 /* Must have a valid pointer to a GAS structure */
89
90 if (!reg) {
91 return (AE_BAD_PARAMETER);
92 }
93
94 /*
95 * Copy the target address. This handles possible alignment issues.
96 * Address must not be null. A null address also indicates an optional
97 * ACPI register that is not supported, so no error message.
98 */
99 ACPI_MOVE_64_TO_64(address, &reg->address);
100 if (!(*address)) {
101 return (AE_BAD_ADDRESS);
102 }
103
Bob Mooreba494be2012-07-12 09:40:10 +0800104 /* Validate the space_ID */
Bob Moorec6b57742009-06-24 09:44:06 +0800105
106 if ((reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
107 (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
108 ACPI_ERROR((AE_INFO,
109 "Unsupported address space: 0x%X", reg->space_id));
110 return (AE_SUPPORT);
111 }
112
113 /* Validate the bit_width */
114
115 if ((reg->bit_width != 8) &&
116 (reg->bit_width != 16) &&
117 (reg->bit_width != 32) && (reg->bit_width != max_bit_width)) {
118 ACPI_ERROR((AE_INFO,
119 "Unsupported register bit width: 0x%X",
120 reg->bit_width));
121 return (AE_SUPPORT);
122 }
123
124 /* Validate the bit_offset. Just a warning for now. */
125
126 if (reg->bit_offset != 0) {
127 ACPI_WARNING((AE_INFO,
128 "Unsupported register bit offset: 0x%X",
129 reg->bit_offset));
130 }
131
132 return (AE_OK);
133}
134
135/******************************************************************************
136 *
137 * FUNCTION: acpi_hw_read
138 *
Bob Mooreba494be2012-07-12 09:40:10 +0800139 * PARAMETERS: value - Where the value is returned
140 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800141 *
142 * RETURN: Status
143 *
144 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
145 * version of acpi_read, used internally since the overhead of
146 * 64-bit values is not needed.
147 *
148 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
149 * bit_width must be exactly 8, 16, or 32.
Bob Mooreba494be2012-07-12 09:40:10 +0800150 * space_ID must be system_memory or system_IO.
Bob Moorec6b57742009-06-24 09:44:06 +0800151 * bit_offset and access_width are currently ignored, as there has
152 * not been a need to implement these.
153 *
154 ******************************************************************************/
155
156acpi_status acpi_hw_read(u32 *value, struct acpi_generic_address *reg)
157{
158 u64 address;
Bob Moore653f4b52012-02-14 18:29:55 +0800159 u64 value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800160 acpi_status status;
161
162 ACPI_FUNCTION_NAME(hw_read);
163
164 /* Validate contents of the GAS register */
165
166 status = acpi_hw_validate_register(reg, 32, &address);
167 if (ACPI_FAILURE(status)) {
168 return (status);
169 }
170
171 /* Initialize entire 32-bit return value to zero */
172
173 *value = 0;
174
175 /*
176 * Two address spaces supported: Memory or IO. PCI_Config is
177 * not supported here because the GAS structure is insufficient
178 */
179 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
180 status = acpi_os_read_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800181 address, &value64, reg->bit_width);
182
183 *value = (u32)value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800184 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
185
186 status = acpi_hw_read_port((acpi_io_address)
187 address, value, reg->bit_width);
188 }
189
190 ACPI_DEBUG_PRINT((ACPI_DB_IO,
191 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
192 *value, reg->bit_width, ACPI_FORMAT_UINT64(address),
193 acpi_ut_get_region_name(reg->space_id)));
194
195 return (status);
196}
197
198/******************************************************************************
199 *
200 * FUNCTION: acpi_hw_write
201 *
Bob Mooreba494be2012-07-12 09:40:10 +0800202 * PARAMETERS: value - Value to be written
203 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800204 *
205 * RETURN: Status
206 *
207 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
208 * version of acpi_write, used internally since the overhead of
209 * 64-bit values is not needed.
210 *
211 ******************************************************************************/
212
213acpi_status acpi_hw_write(u32 value, struct acpi_generic_address *reg)
214{
215 u64 address;
216 acpi_status status;
217
218 ACPI_FUNCTION_NAME(hw_write);
219
220 /* Validate contents of the GAS register */
221
222 status = acpi_hw_validate_register(reg, 32, &address);
223 if (ACPI_FAILURE(status)) {
224 return (status);
225 }
226
227 /*
228 * Two address spaces supported: Memory or IO. PCI_Config is
229 * not supported here because the GAS structure is insufficient
230 */
231 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
232 status = acpi_os_write_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800233 address, (u64)value,
234 reg->bit_width);
Bob Moorec6b57742009-06-24 09:44:06 +0800235 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
236
237 status = acpi_hw_write_port((acpi_io_address)
238 address, value, reg->bit_width);
239 }
240
241 ACPI_DEBUG_PRINT((ACPI_DB_IO,
242 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
243 value, reg->bit_width, ACPI_FORMAT_UINT64(address),
244 acpi_ut_get_region_name(reg->space_id)));
245
246 return (status);
247}
248
Bob Moore33620c52012-02-14 18:14:27 +0800249#if (!ACPI_REDUCED_HARDWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*******************************************************************************
251 *
252 * FUNCTION: acpi_hw_clear_acpi_status
253 *
Bob Moored8c71b62007-02-02 19:48:21 +0300254 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Bob Moore7db5d822008-12-30 11:04:48 +0800256 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
258 * DESCRIPTION: Clears all fixed and general purpose status bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
260 ******************************************************************************/
Bob Moorec520aba2009-02-18 14:20:12 +0800261
Bob Moored8c71b62007-02-02 19:48:21 +0300262acpi_status acpi_hw_clear_acpi_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Len Brown4be44fc2005-08-05 00:44:28 -0400264 acpi_status status;
Bob Moore4c90ece2006-06-08 16:29:00 -0400265 acpi_cpu_flags lock_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Bob Mooreb229cf92006-04-21 17:15:00 -0400267 ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Bob Moore8eb7b242009-04-22 10:28:22 +0800269 ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400270 ACPI_BITMASK_ALL_FIXED_STATUS,
Bob Moore8eb7b242009-04-22 10:28:22 +0800271 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Bob Moore4c90ece2006-06-08 16:29:00 -0400273 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Bob Moore227243a2009-02-18 14:24:50 +0800275 /* Clear the fixed events in PM1 A/B */
Bob Moore531c6332009-02-18 14:06:12 +0800276
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400277 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400278 ACPI_BITMASK_ALL_FIXED_STATUS);
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600279
280 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
281
282 if (ACPI_FAILURE(status))
283 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
286
Bob Mooree97d6bf2008-12-30 09:45:17 +0800287 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600289exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400290 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*******************************************************************************
294 *
Bob Moore33620c52012-02-14 18:14:27 +0800295 * FUNCTION: acpi_hw_get_bit_register_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * PARAMETERS: register_id - Index of ACPI Register to access
298 *
Robert Moore44f6c012005-04-18 22:49:35 -0400299 * RETURN: The bitmask to be used when accessing the register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *
Robert Moore44f6c012005-04-18 22:49:35 -0400301 * DESCRIPTION: Map register_id into a register bitmask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 ******************************************************************************/
Bob Moore7db5d822008-12-30 11:04:48 +0800304
Len Brown4be44fc2005-08-05 00:44:28 -0400305struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Bob Moore4a90c7e2006-01-13 16:22:00 -0500307 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (register_id > ACPI_BITREG_MAX) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800310 ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: 0x%X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500311 register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return (NULL);
313 }
314
315 return (&acpi_gbl_bit_register_info[register_id]);
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/******************************************************************************
319 *
Bob Moore32c9ef92009-02-18 14:36:05 +0800320 * FUNCTION: acpi_hw_write_pm1_control
321 *
322 * PARAMETERS: pm1a_control - Value to be written to PM1A control
323 * pm1b_control - Value to be written to PM1B control
324 *
325 * RETURN: Status
326 *
327 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
328 * different than than the PM1 A/B status and enable registers
329 * in that different values can be written to the A/B registers.
330 * Most notably, the SLP_TYP bits can be different, as per the
331 * values returned from the _Sx predefined methods.
332 *
333 ******************************************************************************/
334
335acpi_status acpi_hw_write_pm1_control(u32 pm1a_control, u32 pm1b_control)
336{
337 acpi_status status;
338
339 ACPI_FUNCTION_TRACE(hw_write_pm1_control);
340
Bob Moorec6b57742009-06-24 09:44:06 +0800341 status =
342 acpi_hw_write(pm1a_control, &acpi_gbl_FADT.xpm1a_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800343 if (ACPI_FAILURE(status)) {
344 return_ACPI_STATUS(status);
345 }
346
347 if (acpi_gbl_FADT.xpm1b_control_block.address) {
348 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800349 acpi_hw_write(pm1b_control,
350 &acpi_gbl_FADT.xpm1b_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800351 }
352 return_ACPI_STATUS(status);
353}
354
355/******************************************************************************
356 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * FUNCTION: acpi_hw_register_read
358 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400359 * PARAMETERS: register_id - ACPI Register ID
Robert Moore44f6c012005-04-18 22:49:35 -0400360 * return_value - Where the register value is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
362 * RETURN: Status and the value read.
363 *
Bob Moore967440e32006-06-23 17:04:00 -0400364 * DESCRIPTION: Read from the specified ACPI register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367acpi_status
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400368acpi_hw_register_read(u32 register_id, u32 * return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Bob Moorec520aba2009-02-18 14:20:12 +0800370 u32 value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400371 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Bob Mooreb229cf92006-04-21 17:15:00 -0400373 ACPI_FUNCTION_TRACE(hw_register_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800376 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Bob Moorec520aba2009-02-18 14:20:12 +0800378 status = acpi_hw_read_multiple(&value,
379 &acpi_gbl_xpm1a_status,
380 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
382
Bob Moorec520aba2009-02-18 14:20:12 +0800383 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Bob Moorec520aba2009-02-18 14:20:12 +0800385 status = acpi_hw_read_multiple(&value,
386 &acpi_gbl_xpm1a_enable,
387 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
389
Bob Moorec520aba2009-02-18 14:20:12 +0800390 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Bob Moorec520aba2009-02-18 14:20:12 +0800392 status = acpi_hw_read_multiple(&value,
393 &acpi_gbl_FADT.
394 xpm1a_control_block,
395 &acpi_gbl_FADT.
396 xpm1b_control_block);
Lin Mingc3dd25f2009-03-19 09:51:01 +0800397
398 /*
399 * Zero the write-only bits. From the ACPI specification, "Hardware
400 * Write-Only Bits": "Upon reads to registers with write-only bits,
401 * software masks out all write-only bits."
402 */
403 value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405
Len Brown4be44fc2005-08-05 00:44:28 -0400406 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Bob Moorec6b57742009-06-24 09:44:06 +0800408 status =
409 acpi_hw_read(&value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
411
Len Brown4be44fc2005-08-05 00:44:28 -0400412 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Bob Moorec6b57742009-06-24 09:44:06 +0800414 status = acpi_hw_read(&value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416
Len Brown4be44fc2005-08-05 00:44:28 -0400417 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Bob Mooref3d2e782007-02-02 19:48:18 +0300419 status =
Bob Moore7f071902009-03-19 09:37:47 +0800420 acpi_hw_read_port(acpi_gbl_FADT.smi_command, &value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422
423 default:
Bob Mooref6a22b02010-03-05 17:56:40 +0800424 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 status = AE_BAD_PARAMETER;
426 break;
427 }
428
Len Brown4be44fc2005-08-05 00:44:28 -0400429 if (ACPI_SUCCESS(status)) {
Bob Moorec520aba2009-02-18 14:20:12 +0800430 *return_value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
Len Brown4be44fc2005-08-05 00:44:28 -0400433 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/******************************************************************************
437 *
438 * FUNCTION: acpi_hw_register_write
439 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400440 * PARAMETERS: register_id - ACPI Register ID
Bob Mooreba494be2012-07-12 09:40:10 +0800441 * value - The value to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *
443 * RETURN: Status
444 *
Bob Moore967440e32006-06-23 17:04:00 -0400445 * DESCRIPTION: Write to the specified ACPI register
446 *
447 * NOTE: In accordance with the ACPI specification, this function automatically
448 * preserves the value of the following bits, meaning that these bits cannot be
449 * changed via this interface:
450 *
451 * PM1_CONTROL[0] = SCI_EN
452 * PM1_CONTROL[9]
453 * PM1_STATUS[11]
454 *
455 * ACPI References:
456 * 1) Hardware Ignored Bits: When software writes to a register with ignored
457 * bit fields, it preserves the ignored bit fields
458 * 2) SCI_EN: OSPM always preserves this bit position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *
460 ******************************************************************************/
461
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400462acpi_status acpi_hw_register_write(u32 register_id, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Len Brown4be44fc2005-08-05 00:44:28 -0400464 acpi_status status;
Bob Moore967440e32006-06-23 17:04:00 -0400465 u32 read_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Bob Mooreb229cf92006-04-21 17:15:00 -0400467 ACPI_FUNCTION_TRACE(hw_register_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800470 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Bob Moore8636f8d2009-03-09 16:32:20 +0800471 /*
472 * Handle the "ignored" bit in PM1 Status. According to the ACPI
473 * specification, ignored bits are to be preserved when writing.
474 * Normally, this would mean a read/modify/write sequence. However,
475 * preserving a bit in the status register is different. Writing a
476 * one clears the status, and writing a zero preserves the status.
477 * Therefore, we must always write zero to the ignored bit.
478 *
479 * This behavior is clarified in the ACPI 4.0 specification.
480 */
481 value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
Bob Moore967440e32006-06-23 17:04:00 -0400482
Bob Moorec520aba2009-02-18 14:20:12 +0800483 status = acpi_hw_write_multiple(value,
484 &acpi_gbl_xpm1a_status,
485 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487
Lv Zheng75c80442012-12-19 05:36:49 +0000488 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Bob Moorec520aba2009-02-18 14:20:12 +0800490 status = acpi_hw_write_multiple(value,
491 &acpi_gbl_xpm1a_enable,
492 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494
Bob Moorec520aba2009-02-18 14:20:12 +0800495 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Bob Moore967440e32006-06-23 17:04:00 -0400497 /*
498 * Perform a read first to preserve certain bits (per ACPI spec)
Bob Moorec520aba2009-02-18 14:20:12 +0800499 * Note: This includes SCI_EN, we never want to change this bit
Bob Moore967440e32006-06-23 17:04:00 -0400500 */
Bob Moorec520aba2009-02-18 14:20:12 +0800501 status = acpi_hw_read_multiple(&read_value,
502 &acpi_gbl_FADT.
503 xpm1a_control_block,
504 &acpi_gbl_FADT.
505 xpm1b_control_block);
Bob Moore967440e32006-06-23 17:04:00 -0400506 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400507 goto exit;
Bob Moore967440e32006-06-23 17:04:00 -0400508 }
509
510 /* Insert the bits to be preserved */
511
512 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
513 read_value);
514
515 /* Now we can write the data */
516
Bob Moorec520aba2009-02-18 14:20:12 +0800517 status = acpi_hw_write_multiple(value,
518 &acpi_gbl_FADT.
519 xpm1a_control_block,
520 &acpi_gbl_FADT.
521 xpm1b_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523
Len Brown4be44fc2005-08-05 00:44:28 -0400524 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Bob Moore20869dc2009-03-13 09:10:46 +0800526 /*
527 * For control registers, all reserved bits must be preserved,
528 * as per the ACPI spec.
529 */
530 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800531 acpi_hw_read(&read_value,
532 &acpi_gbl_FADT.xpm2_control_block);
Bob Moore20869dc2009-03-13 09:10:46 +0800533 if (ACPI_FAILURE(status)) {
534 goto exit;
535 }
536
537 /* Insert the bits to be preserved */
538
539 ACPI_INSERT_BITS(value, ACPI_PM2_CONTROL_PRESERVED_BITS,
540 read_value);
541
Bob Moorec6b57742009-06-24 09:44:06 +0800542 status =
543 acpi_hw_write(value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545
Len Brown4be44fc2005-08-05 00:44:28 -0400546 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Bob Moorec6b57742009-06-24 09:44:06 +0800548 status = acpi_hw_write(value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550
Len Brown4be44fc2005-08-05 00:44:28 -0400551 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* SMI_CMD is currently always in IO space */
554
Bob Mooref3d2e782007-02-02 19:48:18 +0300555 status =
Bob Moore7f071902009-03-19 09:37:47 +0800556 acpi_hw_write_port(acpi_gbl_FADT.smi_command, value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 default:
Bob Mooref6a22b02010-03-05 17:56:40 +0800560 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 status = AE_BAD_PARAMETER;
562 break;
563 }
564
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400565 exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400566 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Bob Moorec520aba2009-02-18 14:20:12 +0800568
569/******************************************************************************
570 *
571 * FUNCTION: acpi_hw_read_multiple
572 *
Bob Mooreba494be2012-07-12 09:40:10 +0800573 * PARAMETERS: value - Where the register value is returned
Bob Moorec520aba2009-02-18 14:20:12 +0800574 * register_a - First ACPI register (required)
575 * register_b - Second ACPI register (optional)
576 *
577 * RETURN: Status
578 *
579 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
580 *
581 ******************************************************************************/
582
583static acpi_status
584acpi_hw_read_multiple(u32 *value,
585 struct acpi_generic_address *register_a,
586 struct acpi_generic_address *register_b)
587{
588 u32 value_a = 0;
589 u32 value_b = 0;
590 acpi_status status;
591
592 /* The first register is always required */
593
Bob Moorec6b57742009-06-24 09:44:06 +0800594 status = acpi_hw_read(&value_a, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800595 if (ACPI_FAILURE(status)) {
596 return (status);
597 }
598
599 /* Second register is optional */
600
601 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800602 status = acpi_hw_read(&value_b, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800603 if (ACPI_FAILURE(status)) {
604 return (status);
605 }
606 }
607
Bob Mooreaefc7f92009-02-18 14:26:02 +0800608 /*
609 * OR the two return values together. No shifting or masking is necessary,
610 * because of how the PM1 registers are defined in the ACPI specification:
611 *
612 * "Although the bits can be split between the two register blocks (each
613 * register block has a unique pointer within the FADT), the bit positions
614 * are maintained. The register block with unimplemented bits (that is,
615 * those implemented in the other register block) always returns zeros,
616 * and writes have no side effects"
617 */
618 *value = (value_a | value_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800619 return (AE_OK);
620}
621
622/******************************************************************************
623 *
624 * FUNCTION: acpi_hw_write_multiple
625 *
Bob Mooreba494be2012-07-12 09:40:10 +0800626 * PARAMETERS: value - The value to write
Bob Moorec520aba2009-02-18 14:20:12 +0800627 * register_a - First ACPI register (required)
628 * register_b - Second ACPI register (optional)
629 *
630 * RETURN: Status
631 *
632 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
633 *
634 ******************************************************************************/
635
636static acpi_status
637acpi_hw_write_multiple(u32 value,
638 struct acpi_generic_address *register_a,
639 struct acpi_generic_address *register_b)
640{
641 acpi_status status;
642
643 /* The first register is always required */
644
Bob Moorec6b57742009-06-24 09:44:06 +0800645 status = acpi_hw_write(value, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800646 if (ACPI_FAILURE(status)) {
647 return (status);
648 }
649
Bob Mooreaefc7f92009-02-18 14:26:02 +0800650 /*
651 * Second register is optional
652 *
653 * No bit shifting or clearing is necessary, because of how the PM1
654 * registers are defined in the ACPI specification:
655 *
656 * "Although the bits can be split between the two register blocks (each
657 * register block has a unique pointer within the FADT), the bit positions
658 * are maintained. The register block with unimplemented bits (that is,
659 * those implemented in the other register block) always returns zeros,
660 * and writes have no side effects"
661 */
Bob Moorec520aba2009-02-18 14:20:12 +0800662 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800663 status = acpi_hw_write(value, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800664 }
665
666 return (status);
667}
Bob Moore33620c52012-02-14 18:14:27 +0800668
669#endif /* !ACPI_REDUCED_HARDWARE */