blob: 5ba0498412fd5773d07feadd44ca442d88211b91 [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 Moorec8100dc2016-01-15 08:17:03 +08009 * Copyright (C) 2000 - 2016, 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"
Len Browne2f7a772009-01-09 00:30:03 -050047#include "acevents.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("hwregs")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Bob Moore33620c52012-02-14 18:14:27 +080052#if (!ACPI_REDUCED_HARDWARE)
Bob Moorec520aba2009-02-18 14:20:12 +080053/* Local Prototypes */
54static acpi_status
55acpi_hw_read_multiple(u32 *value,
56 struct acpi_generic_address *register_a,
57 struct acpi_generic_address *register_b);
58
59static acpi_status
60acpi_hw_write_multiple(u32 value,
61 struct acpi_generic_address *register_a,
62 struct acpi_generic_address *register_b);
63
Bob Moore33620c52012-02-14 18:14:27 +080064#endif /* !ACPI_REDUCED_HARDWARE */
65
Bob Moorec6b57742009-06-24 09:44:06 +080066/******************************************************************************
67 *
68 * FUNCTION: acpi_hw_validate_register
69 *
Bob Mooreba494be2012-07-12 09:40:10 +080070 * PARAMETERS: reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +080071 * max_bit_width - Max bit_width supported (32 or 64)
Bob Mooreba494be2012-07-12 09:40:10 +080072 * address - Pointer to where the gas->address
Bob Moorec6b57742009-06-24 09:44:06 +080073 * is returned
74 *
75 * RETURN: Status
76 *
77 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
78 * pointer, Address, space_id, bit_width, and bit_offset.
79 *
80 ******************************************************************************/
81
82acpi_status
83acpi_hw_validate_register(struct acpi_generic_address *reg,
84 u8 max_bit_width, u64 *address)
85{
86
87 /* Must have a valid pointer to a GAS structure */
88
89 if (!reg) {
90 return (AE_BAD_PARAMETER);
91 }
92
93 /*
94 * Copy the target address. This handles possible alignment issues.
95 * Address must not be null. A null address also indicates an optional
96 * ACPI register that is not supported, so no error message.
97 */
98 ACPI_MOVE_64_TO_64(address, &reg->address);
99 if (!(*address)) {
100 return (AE_BAD_ADDRESS);
101 }
102
Bob Mooreba494be2012-07-12 09:40:10 +0800103 /* Validate the space_ID */
Bob Moorec6b57742009-06-24 09:44:06 +0800104
105 if ((reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
106 (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
107 ACPI_ERROR((AE_INFO,
108 "Unsupported address space: 0x%X", reg->space_id));
109 return (AE_SUPPORT);
110 }
111
112 /* Validate the bit_width */
113
114 if ((reg->bit_width != 8) &&
115 (reg->bit_width != 16) &&
116 (reg->bit_width != 32) && (reg->bit_width != max_bit_width)) {
117 ACPI_ERROR((AE_INFO,
118 "Unsupported register bit width: 0x%X",
119 reg->bit_width));
120 return (AE_SUPPORT);
121 }
122
123 /* Validate the bit_offset. Just a warning for now. */
124
125 if (reg->bit_offset != 0) {
126 ACPI_WARNING((AE_INFO,
127 "Unsupported register bit offset: 0x%X",
128 reg->bit_offset));
129 }
130
131 return (AE_OK);
132}
133
134/******************************************************************************
135 *
136 * FUNCTION: acpi_hw_read
137 *
Bob Mooreba494be2012-07-12 09:40:10 +0800138 * PARAMETERS: value - Where the value is returned
139 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800140 *
141 * RETURN: Status
142 *
143 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
144 * version of acpi_read, used internally since the overhead of
145 * 64-bit values is not needed.
146 *
147 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
148 * bit_width must be exactly 8, 16, or 32.
Bob Mooreba494be2012-07-12 09:40:10 +0800149 * space_ID must be system_memory or system_IO.
Bob Moorec6b57742009-06-24 09:44:06 +0800150 * bit_offset and access_width are currently ignored, as there has
151 * not been a need to implement these.
152 *
153 ******************************************************************************/
154
155acpi_status acpi_hw_read(u32 *value, struct acpi_generic_address *reg)
156{
157 u64 address;
Bob Moore653f4b52012-02-14 18:29:55 +0800158 u64 value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800159 acpi_status status;
160
161 ACPI_FUNCTION_NAME(hw_read);
162
163 /* Validate contents of the GAS register */
164
165 status = acpi_hw_validate_register(reg, 32, &address);
166 if (ACPI_FAILURE(status)) {
167 return (status);
168 }
169
170 /* Initialize entire 32-bit return value to zero */
171
172 *value = 0;
173
174 /*
175 * Two address spaces supported: Memory or IO. PCI_Config is
176 * not supported here because the GAS structure is insufficient
177 */
178 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
179 status = acpi_os_read_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800180 address, &value64, reg->bit_width);
181
182 *value = (u32)value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800183 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
184
185 status = acpi_hw_read_port((acpi_io_address)
186 address, value, reg->bit_width);
187 }
188
189 ACPI_DEBUG_PRINT((ACPI_DB_IO,
190 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
191 *value, reg->bit_width, ACPI_FORMAT_UINT64(address),
192 acpi_ut_get_region_name(reg->space_id)));
193
194 return (status);
195}
196
197/******************************************************************************
198 *
199 * FUNCTION: acpi_hw_write
200 *
Bob Mooreba494be2012-07-12 09:40:10 +0800201 * PARAMETERS: value - Value to be written
202 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800203 *
204 * RETURN: Status
205 *
206 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
207 * version of acpi_write, used internally since the overhead of
208 * 64-bit values is not needed.
209 *
210 ******************************************************************************/
211
212acpi_status acpi_hw_write(u32 value, struct acpi_generic_address *reg)
213{
214 u64 address;
215 acpi_status status;
216
217 ACPI_FUNCTION_NAME(hw_write);
218
219 /* Validate contents of the GAS register */
220
221 status = acpi_hw_validate_register(reg, 32, &address);
222 if (ACPI_FAILURE(status)) {
223 return (status);
224 }
225
226 /*
227 * Two address spaces supported: Memory or IO. PCI_Config is
228 * not supported here because the GAS structure is insufficient
229 */
230 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
231 status = acpi_os_write_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800232 address, (u64)value,
233 reg->bit_width);
Bob Moorec6b57742009-06-24 09:44:06 +0800234 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
235
236 status = acpi_hw_write_port((acpi_io_address)
237 address, value, reg->bit_width);
238 }
239
240 ACPI_DEBUG_PRINT((ACPI_DB_IO,
241 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
242 value, reg->bit_width, ACPI_FORMAT_UINT64(address),
243 acpi_ut_get_region_name(reg->space_id)));
244
245 return (status);
246}
247
Bob Moore33620c52012-02-14 18:14:27 +0800248#if (!ACPI_REDUCED_HARDWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*******************************************************************************
250 *
251 * FUNCTION: acpi_hw_clear_acpi_status
252 *
Bob Moored8c71b62007-02-02 19:48:21 +0300253 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *
Bob Moore7db5d822008-12-30 11:04:48 +0800255 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
257 * DESCRIPTION: Clears all fixed and general purpose status bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
259 ******************************************************************************/
Bob Moorec520aba2009-02-18 14:20:12 +0800260
Bob Moored8c71b62007-02-02 19:48:21 +0300261acpi_status acpi_hw_clear_acpi_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Len Brown4be44fc2005-08-05 00:44:28 -0400263 acpi_status status;
Bob Moore4c90ece2006-06-08 16:29:00 -0400264 acpi_cpu_flags lock_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Bob Mooreb229cf92006-04-21 17:15:00 -0400266 ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Bob Moore8eb7b242009-04-22 10:28:22 +0800268 ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400269 ACPI_BITMASK_ALL_FIXED_STATUS,
Bob Moore8eb7b242009-04-22 10:28:22 +0800270 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Bob Moore4c90ece2006-06-08 16:29:00 -0400272 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Bob Moore227243a2009-02-18 14:24:50 +0800274 /* Clear the fixed events in PM1 A/B */
Bob Moore531c6332009-02-18 14:06:12 +0800275
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400276 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400277 ACPI_BITMASK_ALL_FIXED_STATUS);
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600278
279 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
280
Lv Zheng7d3e83b2014-07-08 10:08:19 +0800281 if (ACPI_FAILURE(status)) {
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600282 goto exit;
Lv Zheng7d3e83b2014-07-08 10:08:19 +0800283 }
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 ******************************************************************************/
Lv Zheng3e8214e2012-12-19 05:37:15 +0000367acpi_status acpi_hw_register_read(u32 register_id, u32 *return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Bob Moorec520aba2009-02-18 14:20:12 +0800369 u32 value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400370 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Bob Mooreb229cf92006-04-21 17:15:00 -0400372 ACPI_FUNCTION_TRACE(hw_register_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800375 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Bob Moorec520aba2009-02-18 14:20:12 +0800377 status = acpi_hw_read_multiple(&value,
378 &acpi_gbl_xpm1a_status,
379 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
381
Bob Moorec520aba2009-02-18 14:20:12 +0800382 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Bob Moorec520aba2009-02-18 14:20:12 +0800384 status = acpi_hw_read_multiple(&value,
385 &acpi_gbl_xpm1a_enable,
386 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
388
Bob Moorec520aba2009-02-18 14:20:12 +0800389 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Bob Moorec520aba2009-02-18 14:20:12 +0800391 status = acpi_hw_read_multiple(&value,
392 &acpi_gbl_FADT.
393 xpm1a_control_block,
394 &acpi_gbl_FADT.
395 xpm1b_control_block);
Lin Mingc3dd25f2009-03-19 09:51:01 +0800396
397 /*
398 * Zero the write-only bits. From the ACPI specification, "Hardware
399 * Write-Only Bits": "Upon reads to registers with write-only bits,
400 * software masks out all write-only bits."
401 */
402 value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
404
Len Brown4be44fc2005-08-05 00:44:28 -0400405 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Bob Moorec6b57742009-06-24 09:44:06 +0800407 status =
408 acpi_hw_read(&value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
410
Len Brown4be44fc2005-08-05 00:44:28 -0400411 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Bob Moorec6b57742009-06-24 09:44:06 +0800413 status = acpi_hw_read(&value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
415
Len Brown4be44fc2005-08-05 00:44:28 -0400416 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Bob Mooref3d2e782007-02-02 19:48:18 +0300418 status =
Bob Moore7f071902009-03-19 09:37:47 +0800419 acpi_hw_read_port(acpi_gbl_FADT.smi_command, &value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
421
422 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000423
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 */
Bob Moore967440e32006-06-23 17:04:00 -0400496 /*
497 * Perform a read first to preserve certain bits (per ACPI spec)
Bob Moorec520aba2009-02-18 14:20:12 +0800498 * Note: This includes SCI_EN, we never want to change this bit
Bob Moore967440e32006-06-23 17:04:00 -0400499 */
Bob Moorec520aba2009-02-18 14:20:12 +0800500 status = acpi_hw_read_multiple(&read_value,
501 &acpi_gbl_FADT.
502 xpm1a_control_block,
503 &acpi_gbl_FADT.
504 xpm1b_control_block);
Bob Moore967440e32006-06-23 17:04:00 -0400505 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400506 goto exit;
Bob Moore967440e32006-06-23 17:04:00 -0400507 }
508
509 /* Insert the bits to be preserved */
510
511 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
512 read_value);
513
514 /* Now we can write the data */
515
Bob Moorec520aba2009-02-18 14:20:12 +0800516 status = acpi_hw_write_multiple(value,
517 &acpi_gbl_FADT.
518 xpm1a_control_block,
519 &acpi_gbl_FADT.
520 xpm1b_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522
Len Brown4be44fc2005-08-05 00:44:28 -0400523 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Bob Moore20869dc2009-03-13 09:10:46 +0800524 /*
525 * For control registers, all reserved bits must be preserved,
526 * as per the ACPI spec.
527 */
528 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800529 acpi_hw_read(&read_value,
530 &acpi_gbl_FADT.xpm2_control_block);
Bob Moore20869dc2009-03-13 09:10:46 +0800531 if (ACPI_FAILURE(status)) {
532 goto exit;
533 }
534
535 /* Insert the bits to be preserved */
536
537 ACPI_INSERT_BITS(value, ACPI_PM2_CONTROL_PRESERVED_BITS,
538 read_value);
539
Bob Moorec6b57742009-06-24 09:44:06 +0800540 status =
541 acpi_hw_write(value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
Len Brown4be44fc2005-08-05 00:44:28 -0400544 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Bob Moorec6b57742009-06-24 09:44:06 +0800546 status = acpi_hw_write(value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548
Len Brown4be44fc2005-08-05 00:44:28 -0400549 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* SMI_CMD is currently always in IO space */
552
Bob Mooref3d2e782007-02-02 19:48:18 +0300553 status =
Bob Moore7f071902009-03-19 09:37:47 +0800554 acpi_hw_write_port(acpi_gbl_FADT.smi_command, value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000558
Bob Mooref6a22b02010-03-05 17:56:40 +0800559 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 status = AE_BAD_PARAMETER;
561 break;
562 }
563
Lv Zheng10622bf2013-10-29 09:30:02 +0800564exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400565 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Bob Moorec520aba2009-02-18 14:20:12 +0800567
568/******************************************************************************
569 *
570 * FUNCTION: acpi_hw_read_multiple
571 *
Bob Mooreba494be2012-07-12 09:40:10 +0800572 * PARAMETERS: value - Where the register value is returned
Bob Moorec520aba2009-02-18 14:20:12 +0800573 * register_a - First ACPI register (required)
574 * register_b - Second ACPI register (optional)
575 *
576 * RETURN: Status
577 *
578 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
579 *
580 ******************************************************************************/
581
582static acpi_status
583acpi_hw_read_multiple(u32 *value,
584 struct acpi_generic_address *register_a,
585 struct acpi_generic_address *register_b)
586{
587 u32 value_a = 0;
588 u32 value_b = 0;
589 acpi_status status;
590
591 /* The first register is always required */
592
Bob Moorec6b57742009-06-24 09:44:06 +0800593 status = acpi_hw_read(&value_a, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800594 if (ACPI_FAILURE(status)) {
595 return (status);
596 }
597
598 /* Second register is optional */
599
600 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800601 status = acpi_hw_read(&value_b, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800602 if (ACPI_FAILURE(status)) {
603 return (status);
604 }
605 }
606
Bob Mooreaefc7f92009-02-18 14:26:02 +0800607 /*
608 * OR the two return values together. No shifting or masking is necessary,
609 * because of how the PM1 registers are defined in the ACPI specification:
610 *
611 * "Although the bits can be split between the two register blocks (each
612 * register block has a unique pointer within the FADT), the bit positions
613 * are maintained. The register block with unimplemented bits (that is,
614 * those implemented in the other register block) always returns zeros,
615 * and writes have no side effects"
616 */
617 *value = (value_a | value_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800618 return (AE_OK);
619}
620
621/******************************************************************************
622 *
623 * FUNCTION: acpi_hw_write_multiple
624 *
Bob Mooreba494be2012-07-12 09:40:10 +0800625 * PARAMETERS: value - The value to write
Bob Moorec520aba2009-02-18 14:20:12 +0800626 * register_a - First ACPI register (required)
627 * register_b - Second ACPI register (optional)
628 *
629 * RETURN: Status
630 *
631 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
632 *
633 ******************************************************************************/
634
635static acpi_status
636acpi_hw_write_multiple(u32 value,
637 struct acpi_generic_address *register_a,
638 struct acpi_generic_address *register_b)
639{
640 acpi_status status;
641
642 /* The first register is always required */
643
Bob Moorec6b57742009-06-24 09:44:06 +0800644 status = acpi_hw_write(value, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800645 if (ACPI_FAILURE(status)) {
646 return (status);
647 }
648
Bob Mooreaefc7f92009-02-18 14:26:02 +0800649 /*
650 * Second register is optional
651 *
652 * No bit shifting or clearing is necessary, because of how the PM1
653 * registers are defined in the ACPI specification:
654 *
655 * "Although the bits can be split between the two register blocks (each
656 * register block has a unique pointer within the FADT), the bit positions
657 * are maintained. The register block with unimplemented bits (that is,
658 * those implemented in the other register block) always returns zeros,
659 * and writes have no side effects"
660 */
Bob Moorec520aba2009-02-18 14:20:12 +0800661 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800662 status = acpi_hw_write(value, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800663 }
664
665 return (status);
666}
Bob Moore33620c52012-02-14 18:14:27 +0800667
668#endif /* !ACPI_REDUCED_HARDWARE */