blob: 6b6c83b87b5215859e618f3bd85721438ed62bd8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*******************************************************************************
3 *
4 * Module Name: hwregs - Read/write access functions for the various ACPI
5 * control and status registers.
6 *
7 ******************************************************************************/
8
9/*
Bob Moore77848132012-01-12 13:27:23 +080010 * Copyright (C) 2000 - 2012, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions, and the following disclaimer,
18 * without modification.
19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20 * substantially similar to the "NO WARRANTY" disclaimer below
21 * ("Disclaimer") and any redistribution must be conditioned upon
22 * including a substantially similar Disclaimer requirement for further
23 * binary redistribution.
24 * 3. Neither the names of the above-listed copyright holders nor the names
25 * of any contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * Alternatively, this software may be distributed under the terms of the
29 * GNU General Public License ("GPL") version 2 as published by the Free
30 * Software Foundation.
31 *
32 * NO WARRANTY
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 * POSSIBILITY OF SUCH DAMAGES.
44 */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050047#include "accommon.h"
48#include "acnamesp.h"
49#include "acevents.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040052ACPI_MODULE_NAME("hwregs")
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Bob Moore33620c52012-02-14 18:14:27 +080054#if (!ACPI_REDUCED_HARDWARE)
Bob Moorec520aba2009-02-18 14:20:12 +080055/* Local Prototypes */
56static acpi_status
57acpi_hw_read_multiple(u32 *value,
58 struct acpi_generic_address *register_a,
59 struct acpi_generic_address *register_b);
60
61static acpi_status
62acpi_hw_write_multiple(u32 value,
63 struct acpi_generic_address *register_a,
64 struct acpi_generic_address *register_b);
65
Bob Moore33620c52012-02-14 18:14:27 +080066#endif /* !ACPI_REDUCED_HARDWARE */
67
Bob Moorec6b57742009-06-24 09:44:06 +080068/******************************************************************************
69 *
70 * FUNCTION: acpi_hw_validate_register
71 *
72 * PARAMETERS: Reg - GAS register structure
73 * max_bit_width - Max bit_width supported (32 or 64)
74 * Address - Pointer to where the gas->address
75 * is returned
76 *
77 * RETURN: Status
78 *
79 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
80 * pointer, Address, space_id, bit_width, and bit_offset.
81 *
82 ******************************************************************************/
83
84acpi_status
85acpi_hw_validate_register(struct acpi_generic_address *reg,
86 u8 max_bit_width, u64 *address)
87{
88
89 /* Must have a valid pointer to a GAS structure */
90
91 if (!reg) {
92 return (AE_BAD_PARAMETER);
93 }
94
95 /*
96 * Copy the target address. This handles possible alignment issues.
97 * Address must not be null. A null address also indicates an optional
98 * ACPI register that is not supported, so no error message.
99 */
100 ACPI_MOVE_64_TO_64(address, &reg->address);
101 if (!(*address)) {
102 return (AE_BAD_ADDRESS);
103 }
104
105 /* Validate the space_iD */
106
107 if ((reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
108 (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
109 ACPI_ERROR((AE_INFO,
110 "Unsupported address space: 0x%X", reg->space_id));
111 return (AE_SUPPORT);
112 }
113
114 /* Validate the bit_width */
115
116 if ((reg->bit_width != 8) &&
117 (reg->bit_width != 16) &&
118 (reg->bit_width != 32) && (reg->bit_width != max_bit_width)) {
119 ACPI_ERROR((AE_INFO,
120 "Unsupported register bit width: 0x%X",
121 reg->bit_width));
122 return (AE_SUPPORT);
123 }
124
125 /* Validate the bit_offset. Just a warning for now. */
126
127 if (reg->bit_offset != 0) {
128 ACPI_WARNING((AE_INFO,
129 "Unsupported register bit offset: 0x%X",
130 reg->bit_offset));
131 }
132
133 return (AE_OK);
134}
135
136/******************************************************************************
137 *
138 * FUNCTION: acpi_hw_read
139 *
140 * PARAMETERS: Value - Where the value is returned
141 * Reg - GAS register structure
142 *
143 * RETURN: Status
144 *
145 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
146 * version of acpi_read, used internally since the overhead of
147 * 64-bit values is not needed.
148 *
149 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
150 * bit_width must be exactly 8, 16, or 32.
151 * space_iD must be system_memory or system_iO.
152 * bit_offset and access_width are currently ignored, as there has
153 * not been a need to implement these.
154 *
155 ******************************************************************************/
156
157acpi_status acpi_hw_read(u32 *value, struct acpi_generic_address *reg)
158{
159 u64 address;
Bob Moore653f4b52012-02-14 18:29:55 +0800160 u64 value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800161 acpi_status status;
162
163 ACPI_FUNCTION_NAME(hw_read);
164
165 /* Validate contents of the GAS register */
166
167 status = acpi_hw_validate_register(reg, 32, &address);
168 if (ACPI_FAILURE(status)) {
169 return (status);
170 }
171
172 /* Initialize entire 32-bit return value to zero */
173
174 *value = 0;
175
176 /*
177 * Two address spaces supported: Memory or IO. PCI_Config is
178 * not supported here because the GAS structure is insufficient
179 */
180 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
181 status = acpi_os_read_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800182 address, &value64, reg->bit_width);
183
184 *value = (u32)value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800185 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
186
187 status = acpi_hw_read_port((acpi_io_address)
188 address, value, reg->bit_width);
189 }
190
191 ACPI_DEBUG_PRINT((ACPI_DB_IO,
192 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
193 *value, reg->bit_width, ACPI_FORMAT_UINT64(address),
194 acpi_ut_get_region_name(reg->space_id)));
195
196 return (status);
197}
198
199/******************************************************************************
200 *
201 * FUNCTION: acpi_hw_write
202 *
203 * PARAMETERS: Value - Value to be written
204 * Reg - GAS register structure
205 *
206 * RETURN: Status
207 *
208 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
209 * version of acpi_write, used internally since the overhead of
210 * 64-bit values is not needed.
211 *
212 ******************************************************************************/
213
214acpi_status acpi_hw_write(u32 value, struct acpi_generic_address *reg)
215{
216 u64 address;
217 acpi_status status;
218
219 ACPI_FUNCTION_NAME(hw_write);
220
221 /* Validate contents of the GAS register */
222
223 status = acpi_hw_validate_register(reg, 32, &address);
224 if (ACPI_FAILURE(status)) {
225 return (status);
226 }
227
228 /*
229 * Two address spaces supported: Memory or IO. PCI_Config is
230 * not supported here because the GAS structure is insufficient
231 */
232 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
233 status = acpi_os_write_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800234 address, (u64)value,
235 reg->bit_width);
Bob Moorec6b57742009-06-24 09:44:06 +0800236 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
237
238 status = acpi_hw_write_port((acpi_io_address)
239 address, value, reg->bit_width);
240 }
241
242 ACPI_DEBUG_PRINT((ACPI_DB_IO,
243 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
244 value, reg->bit_width, ACPI_FORMAT_UINT64(address),
245 acpi_ut_get_region_name(reg->space_id)));
246
247 return (status);
248}
249
Bob Moore33620c52012-02-14 18:14:27 +0800250#if (!ACPI_REDUCED_HARDWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*******************************************************************************
252 *
253 * FUNCTION: acpi_hw_clear_acpi_status
254 *
Bob Moored8c71b62007-02-02 19:48:21 +0300255 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Bob Moore7db5d822008-12-30 11:04:48 +0800257 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
259 * DESCRIPTION: Clears all fixed and general purpose status bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
261 ******************************************************************************/
Bob Moorec520aba2009-02-18 14:20:12 +0800262
Bob Moored8c71b62007-02-02 19:48:21 +0300263acpi_status acpi_hw_clear_acpi_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Len Brown4be44fc2005-08-05 00:44:28 -0400265 acpi_status status;
Bob Moore4c90ece2006-06-08 16:29:00 -0400266 acpi_cpu_flags lock_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Bob Mooreb229cf92006-04-21 17:15:00 -0400268 ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Bob Moore8eb7b242009-04-22 10:28:22 +0800270 ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400271 ACPI_BITMASK_ALL_FIXED_STATUS,
Bob Moore8eb7b242009-04-22 10:28:22 +0800272 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Bob Moore4c90ece2006-06-08 16:29:00 -0400274 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Bob Moore227243a2009-02-18 14:24:50 +0800276 /* Clear the fixed events in PM1 A/B */
Bob Moore531c6332009-02-18 14:06:12 +0800277
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400278 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400279 ACPI_BITMASK_ALL_FIXED_STATUS);
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600280
281 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
282
283 if (ACPI_FAILURE(status))
284 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
287
Bob Mooree97d6bf2008-12-30 09:45:17 +0800288 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600290exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400291 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*******************************************************************************
295 *
Bob Moore33620c52012-02-14 18:14:27 +0800296 * FUNCTION: acpi_hw_get_bit_register_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 *
298 * PARAMETERS: register_id - Index of ACPI Register to access
299 *
Robert Moore44f6c012005-04-18 22:49:35 -0400300 * RETURN: The bitmask to be used when accessing the register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
Robert Moore44f6c012005-04-18 22:49:35 -0400302 * DESCRIPTION: Map register_id into a register bitmask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 *
304 ******************************************************************************/
Bob Moore7db5d822008-12-30 11:04:48 +0800305
Len Brown4be44fc2005-08-05 00:44:28 -0400306struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Bob Moore4a90c7e2006-01-13 16:22:00 -0500308 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (register_id > ACPI_BITREG_MAX) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800311 ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: 0x%X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500312 register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return (NULL);
314 }
315
316 return (&acpi_gbl_bit_register_info[register_id]);
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/******************************************************************************
320 *
Bob Moore32c9ef92009-02-18 14:36:05 +0800321 * FUNCTION: acpi_hw_write_pm1_control
322 *
323 * PARAMETERS: pm1a_control - Value to be written to PM1A control
324 * pm1b_control - Value to be written to PM1B control
325 *
326 * RETURN: Status
327 *
328 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
329 * different than than the PM1 A/B status and enable registers
330 * in that different values can be written to the A/B registers.
331 * Most notably, the SLP_TYP bits can be different, as per the
332 * values returned from the _Sx predefined methods.
333 *
334 ******************************************************************************/
335
336acpi_status acpi_hw_write_pm1_control(u32 pm1a_control, u32 pm1b_control)
337{
338 acpi_status status;
339
340 ACPI_FUNCTION_TRACE(hw_write_pm1_control);
341
Bob Moorec6b57742009-06-24 09:44:06 +0800342 status =
343 acpi_hw_write(pm1a_control, &acpi_gbl_FADT.xpm1a_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800344 if (ACPI_FAILURE(status)) {
345 return_ACPI_STATUS(status);
346 }
347
348 if (acpi_gbl_FADT.xpm1b_control_block.address) {
349 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800350 acpi_hw_write(pm1b_control,
351 &acpi_gbl_FADT.xpm1b_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800352 }
353 return_ACPI_STATUS(status);
354}
355
356/******************************************************************************
357 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * FUNCTION: acpi_hw_register_read
359 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400360 * PARAMETERS: register_id - ACPI Register ID
Robert Moore44f6c012005-04-18 22:49:35 -0400361 * return_value - Where the register value is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 *
363 * RETURN: Status and the value read.
364 *
Bob Moore967440e2006-06-23 17:04:00 -0400365 * DESCRIPTION: Read from the specified ACPI register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *
367 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368acpi_status
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400369acpi_hw_register_read(u32 register_id, u32 * return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Bob Moorec520aba2009-02-18 14:20:12 +0800371 u32 value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400372 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Bob Mooreb229cf92006-04-21 17:15:00 -0400374 ACPI_FUNCTION_TRACE(hw_register_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800377 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Bob Moorec520aba2009-02-18 14:20:12 +0800379 status = acpi_hw_read_multiple(&value,
380 &acpi_gbl_xpm1a_status,
381 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383
Bob Moorec520aba2009-02-18 14:20:12 +0800384 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Bob Moorec520aba2009-02-18 14:20:12 +0800386 status = acpi_hw_read_multiple(&value,
387 &acpi_gbl_xpm1a_enable,
388 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 break;
390
Bob Moorec520aba2009-02-18 14:20:12 +0800391 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Bob Moorec520aba2009-02-18 14:20:12 +0800393 status = acpi_hw_read_multiple(&value,
394 &acpi_gbl_FADT.
395 xpm1a_control_block,
396 &acpi_gbl_FADT.
397 xpm1b_control_block);
Lin Mingc3dd25f2009-03-19 09:51:01 +0800398
399 /*
400 * Zero the write-only bits. From the ACPI specification, "Hardware
401 * Write-Only Bits": "Upon reads to registers with write-only bits,
402 * software masks out all write-only bits."
403 */
404 value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406
Len Brown4be44fc2005-08-05 00:44:28 -0400407 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Bob Moorec6b57742009-06-24 09:44:06 +0800409 status =
410 acpi_hw_read(&value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412
Len Brown4be44fc2005-08-05 00:44:28 -0400413 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Bob Moorec6b57742009-06-24 09:44:06 +0800415 status = acpi_hw_read(&value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 break;
417
Len Brown4be44fc2005-08-05 00:44:28 -0400418 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Bob Mooref3d2e782007-02-02 19:48:18 +0300420 status =
Bob Moore7f071902009-03-19 09:37:47 +0800421 acpi_hw_read_port(acpi_gbl_FADT.smi_command, &value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423
424 default:
Bob Mooref6a22b02010-03-05 17:56:40 +0800425 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 status = AE_BAD_PARAMETER;
427 break;
428 }
429
Len Brown4be44fc2005-08-05 00:44:28 -0400430 if (ACPI_SUCCESS(status)) {
Bob Moorec520aba2009-02-18 14:20:12 +0800431 *return_value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/******************************************************************************
438 *
439 * FUNCTION: acpi_hw_register_write
440 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400441 * PARAMETERS: register_id - ACPI Register ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * Value - The value to write
443 *
444 * RETURN: Status
445 *
Bob Moore967440e2006-06-23 17:04:00 -0400446 * DESCRIPTION: Write to the specified ACPI register
447 *
448 * NOTE: In accordance with the ACPI specification, this function automatically
449 * preserves the value of the following bits, meaning that these bits cannot be
450 * changed via this interface:
451 *
452 * PM1_CONTROL[0] = SCI_EN
453 * PM1_CONTROL[9]
454 * PM1_STATUS[11]
455 *
456 * ACPI References:
457 * 1) Hardware Ignored Bits: When software writes to a register with ignored
458 * bit fields, it preserves the ignored bit fields
459 * 2) SCI_EN: OSPM always preserves this bit position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *
461 ******************************************************************************/
462
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400463acpi_status acpi_hw_register_write(u32 register_id, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Len Brown4be44fc2005-08-05 00:44:28 -0400465 acpi_status status;
Bob Moore967440e2006-06-23 17:04:00 -0400466 u32 read_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Bob Mooreb229cf92006-04-21 17:15:00 -0400468 ACPI_FUNCTION_TRACE(hw_register_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800471 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Bob Moore8636f8d2009-03-09 16:32:20 +0800472 /*
473 * Handle the "ignored" bit in PM1 Status. According to the ACPI
474 * specification, ignored bits are to be preserved when writing.
475 * Normally, this would mean a read/modify/write sequence. However,
476 * preserving a bit in the status register is different. Writing a
477 * one clears the status, and writing a zero preserves the status.
478 * Therefore, we must always write zero to the ignored bit.
479 *
480 * This behavior is clarified in the ACPI 4.0 specification.
481 */
482 value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
Bob Moore967440e2006-06-23 17:04:00 -0400483
Bob Moorec520aba2009-02-18 14:20:12 +0800484 status = acpi_hw_write_multiple(value,
485 &acpi_gbl_xpm1a_status,
486 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 break;
488
Bob Moorec520aba2009-02-18 14:20:12 +0800489 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Bob Moorec520aba2009-02-18 14:20:12 +0800491 status = acpi_hw_write_multiple(value,
492 &acpi_gbl_xpm1a_enable,
493 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495
Bob Moorec520aba2009-02-18 14:20:12 +0800496 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Bob Moore967440e2006-06-23 17:04:00 -0400498 /*
499 * Perform a read first to preserve certain bits (per ACPI spec)
Bob Moorec520aba2009-02-18 14:20:12 +0800500 * Note: This includes SCI_EN, we never want to change this bit
Bob Moore967440e2006-06-23 17:04:00 -0400501 */
Bob Moorec520aba2009-02-18 14:20:12 +0800502 status = acpi_hw_read_multiple(&read_value,
503 &acpi_gbl_FADT.
504 xpm1a_control_block,
505 &acpi_gbl_FADT.
506 xpm1b_control_block);
Bob Moore967440e2006-06-23 17:04:00 -0400507 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400508 goto exit;
Bob Moore967440e2006-06-23 17:04:00 -0400509 }
510
511 /* Insert the bits to be preserved */
512
513 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
514 read_value);
515
516 /* Now we can write the data */
517
Bob Moorec520aba2009-02-18 14:20:12 +0800518 status = acpi_hw_write_multiple(value,
519 &acpi_gbl_FADT.
520 xpm1a_control_block,
521 &acpi_gbl_FADT.
522 xpm1b_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524
Len Brown4be44fc2005-08-05 00:44:28 -0400525 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Bob Moore20869dc2009-03-13 09:10:46 +0800527 /*
528 * For control registers, all reserved bits must be preserved,
529 * as per the ACPI spec.
530 */
531 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800532 acpi_hw_read(&read_value,
533 &acpi_gbl_FADT.xpm2_control_block);
Bob Moore20869dc2009-03-13 09:10:46 +0800534 if (ACPI_FAILURE(status)) {
535 goto exit;
536 }
537
538 /* Insert the bits to be preserved */
539
540 ACPI_INSERT_BITS(value, ACPI_PM2_CONTROL_PRESERVED_BITS,
541 read_value);
542
Bob Moorec6b57742009-06-24 09:44:06 +0800543 status =
544 acpi_hw_write(value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
546
Len Brown4be44fc2005-08-05 00:44:28 -0400547 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Bob Moorec6b57742009-06-24 09:44:06 +0800549 status = acpi_hw_write(value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551
Len Brown4be44fc2005-08-05 00:44:28 -0400552 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* SMI_CMD is currently always in IO space */
555
Bob Mooref3d2e782007-02-02 19:48:18 +0300556 status =
Bob Moore7f071902009-03-19 09:37:47 +0800557 acpi_hw_write_port(acpi_gbl_FADT.smi_command, value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 default:
Bob Mooref6a22b02010-03-05 17:56:40 +0800561 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 status = AE_BAD_PARAMETER;
563 break;
564 }
565
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400566 exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400567 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
Bob Moorec520aba2009-02-18 14:20:12 +0800569
570/******************************************************************************
571 *
572 * FUNCTION: acpi_hw_read_multiple
573 *
574 * PARAMETERS: Value - Where the register value is returned
575 * register_a - First ACPI register (required)
576 * register_b - Second ACPI register (optional)
577 *
578 * RETURN: Status
579 *
580 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
581 *
582 ******************************************************************************/
583
584static acpi_status
585acpi_hw_read_multiple(u32 *value,
586 struct acpi_generic_address *register_a,
587 struct acpi_generic_address *register_b)
588{
589 u32 value_a = 0;
590 u32 value_b = 0;
591 acpi_status status;
592
593 /* The first register is always required */
594
Bob Moorec6b57742009-06-24 09:44:06 +0800595 status = acpi_hw_read(&value_a, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800596 if (ACPI_FAILURE(status)) {
597 return (status);
598 }
599
600 /* Second register is optional */
601
602 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800603 status = acpi_hw_read(&value_b, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800604 if (ACPI_FAILURE(status)) {
605 return (status);
606 }
607 }
608
Bob Mooreaefc7f92009-02-18 14:26:02 +0800609 /*
610 * OR the two return values together. No shifting or masking is necessary,
611 * because of how the PM1 registers are defined in the ACPI specification:
612 *
613 * "Although the bits can be split between the two register blocks (each
614 * register block has a unique pointer within the FADT), the bit positions
615 * are maintained. The register block with unimplemented bits (that is,
616 * those implemented in the other register block) always returns zeros,
617 * and writes have no side effects"
618 */
619 *value = (value_a | value_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800620 return (AE_OK);
621}
622
623/******************************************************************************
624 *
625 * FUNCTION: acpi_hw_write_multiple
626 *
627 * PARAMETERS: Value - The value to write
628 * register_a - First ACPI register (required)
629 * register_b - Second ACPI register (optional)
630 *
631 * RETURN: Status
632 *
633 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
634 *
635 ******************************************************************************/
636
637static acpi_status
638acpi_hw_write_multiple(u32 value,
639 struct acpi_generic_address *register_a,
640 struct acpi_generic_address *register_b)
641{
642 acpi_status status;
643
644 /* The first register is always required */
645
Bob Moorec6b57742009-06-24 09:44:06 +0800646 status = acpi_hw_write(value, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800647 if (ACPI_FAILURE(status)) {
648 return (status);
649 }
650
Bob Mooreaefc7f92009-02-18 14:26:02 +0800651 /*
652 * Second register is optional
653 *
654 * No bit shifting or clearing is necessary, because of how the PM1
655 * registers are defined in the ACPI specification:
656 *
657 * "Although the bits can be split between the two register blocks (each
658 * register block has a unique pointer within the FADT), the bit positions
659 * are maintained. The register block with unimplemented bits (that is,
660 * those implemented in the other register block) always returns zeros,
661 * and writes have no side effects"
662 */
Bob Moorec520aba2009-02-18 14:20:12 +0800663 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800664 status = acpi_hw_write(value, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800665 }
666
667 return (status);
668}
Bob Moore33620c52012-02-14 18:14:27 +0800669
670#endif /* !ACPI_REDUCED_HARDWARE */