blob: e0fd9b4978cd6a22af964a3165d26cfd62ea032f [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 Moorefbb7a2d2014-02-08 09:42:25 +08009 * Copyright (C) 2000 - 2014, 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
281 if (ACPI_FAILURE(status))
282 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
285
Bob Mooree97d6bf2008-12-30 09:45:17 +0800286 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600288exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400289 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*******************************************************************************
293 *
Bob Moore33620c52012-02-14 18:14:27 +0800294 * FUNCTION: acpi_hw_get_bit_register_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *
296 * PARAMETERS: register_id - Index of ACPI Register to access
297 *
Robert Moore44f6c012005-04-18 22:49:35 -0400298 * RETURN: The bitmask to be used when accessing the register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *
Robert Moore44f6c012005-04-18 22:49:35 -0400300 * DESCRIPTION: Map register_id into a register bitmask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
302 ******************************************************************************/
Bob Moore7db5d822008-12-30 11:04:48 +0800303
Len Brown4be44fc2005-08-05 00:44:28 -0400304struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Bob Moore4a90c7e2006-01-13 16:22:00 -0500306 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (register_id > ACPI_BITREG_MAX) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800309 ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: 0x%X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500310 register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return (NULL);
312 }
313
314 return (&acpi_gbl_bit_register_info[register_id]);
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/******************************************************************************
318 *
Bob Moore32c9ef92009-02-18 14:36:05 +0800319 * FUNCTION: acpi_hw_write_pm1_control
320 *
321 * PARAMETERS: pm1a_control - Value to be written to PM1A control
322 * pm1b_control - Value to be written to PM1B control
323 *
324 * RETURN: Status
325 *
326 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
327 * different than than the PM1 A/B status and enable registers
328 * in that different values can be written to the A/B registers.
329 * Most notably, the SLP_TYP bits can be different, as per the
330 * values returned from the _Sx predefined methods.
331 *
332 ******************************************************************************/
333
334acpi_status acpi_hw_write_pm1_control(u32 pm1a_control, u32 pm1b_control)
335{
336 acpi_status status;
337
338 ACPI_FUNCTION_TRACE(hw_write_pm1_control);
339
Bob Moorec6b57742009-06-24 09:44:06 +0800340 status =
341 acpi_hw_write(pm1a_control, &acpi_gbl_FADT.xpm1a_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800342 if (ACPI_FAILURE(status)) {
343 return_ACPI_STATUS(status);
344 }
345
346 if (acpi_gbl_FADT.xpm1b_control_block.address) {
347 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800348 acpi_hw_write(pm1b_control,
349 &acpi_gbl_FADT.xpm1b_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800350 }
351 return_ACPI_STATUS(status);
352}
353
354/******************************************************************************
355 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * FUNCTION: acpi_hw_register_read
357 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400358 * PARAMETERS: register_id - ACPI Register ID
Robert Moore44f6c012005-04-18 22:49:35 -0400359 * return_value - Where the register value is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
361 * RETURN: Status and the value read.
362 *
Bob Moore967440e32006-06-23 17:04:00 -0400363 * DESCRIPTION: Read from the specified ACPI register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *
365 ******************************************************************************/
Lv Zheng3e8214e2012-12-19 05:37:15 +0000366acpi_status acpi_hw_register_read(u32 register_id, u32 *return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Bob Moorec520aba2009-02-18 14:20:12 +0800368 u32 value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400369 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Bob Mooreb229cf92006-04-21 17:15:00 -0400371 ACPI_FUNCTION_TRACE(hw_register_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800374 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Bob Moorec520aba2009-02-18 14:20:12 +0800376 status = acpi_hw_read_multiple(&value,
377 &acpi_gbl_xpm1a_status,
378 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380
Bob Moorec520aba2009-02-18 14:20:12 +0800381 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Bob Moorec520aba2009-02-18 14:20:12 +0800383 status = acpi_hw_read_multiple(&value,
384 &acpi_gbl_xpm1a_enable,
385 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387
Bob Moorec520aba2009-02-18 14:20:12 +0800388 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Bob Moorec520aba2009-02-18 14:20:12 +0800390 status = acpi_hw_read_multiple(&value,
391 &acpi_gbl_FADT.
392 xpm1a_control_block,
393 &acpi_gbl_FADT.
394 xpm1b_control_block);
Lin Mingc3dd25f2009-03-19 09:51:01 +0800395
396 /*
397 * Zero the write-only bits. From the ACPI specification, "Hardware
398 * Write-Only Bits": "Upon reads to registers with write-only bits,
399 * software masks out all write-only bits."
400 */
401 value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
403
Len Brown4be44fc2005-08-05 00:44:28 -0400404 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Bob Moorec6b57742009-06-24 09:44:06 +0800406 status =
407 acpi_hw_read(&value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
409
Len Brown4be44fc2005-08-05 00:44:28 -0400410 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Bob Moorec6b57742009-06-24 09:44:06 +0800412 status = acpi_hw_read(&value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 break;
414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Bob Mooref3d2e782007-02-02 19:48:18 +0300417 status =
Bob Moore7f071902009-03-19 09:37:47 +0800418 acpi_hw_read_port(acpi_gbl_FADT.smi_command, &value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
420
421 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000422
Bob Mooref6a22b02010-03-05 17:56:40 +0800423 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 status = AE_BAD_PARAMETER;
425 break;
426 }
427
Len Brown4be44fc2005-08-05 00:44:28 -0400428 if (ACPI_SUCCESS(status)) {
Bob Moorec520aba2009-02-18 14:20:12 +0800429 *return_value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
Len Brown4be44fc2005-08-05 00:44:28 -0400432 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/******************************************************************************
436 *
437 * FUNCTION: acpi_hw_register_write
438 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400439 * PARAMETERS: register_id - ACPI Register ID
Bob Mooreba494be2012-07-12 09:40:10 +0800440 * value - The value to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
442 * RETURN: Status
443 *
Bob Moore967440e32006-06-23 17:04:00 -0400444 * DESCRIPTION: Write to the specified ACPI register
445 *
446 * NOTE: In accordance with the ACPI specification, this function automatically
447 * preserves the value of the following bits, meaning that these bits cannot be
448 * changed via this interface:
449 *
450 * PM1_CONTROL[0] = SCI_EN
451 * PM1_CONTROL[9]
452 * PM1_STATUS[11]
453 *
454 * ACPI References:
455 * 1) Hardware Ignored Bits: When software writes to a register with ignored
456 * bit fields, it preserves the ignored bit fields
457 * 2) SCI_EN: OSPM always preserves this bit position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
459 ******************************************************************************/
460
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400461acpi_status acpi_hw_register_write(u32 register_id, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Len Brown4be44fc2005-08-05 00:44:28 -0400463 acpi_status status;
Bob Moore967440e32006-06-23 17:04:00 -0400464 u32 read_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Bob Mooreb229cf92006-04-21 17:15:00 -0400466 ACPI_FUNCTION_TRACE(hw_register_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800469 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Bob Moore8636f8d2009-03-09 16:32:20 +0800470 /*
471 * Handle the "ignored" bit in PM1 Status. According to the ACPI
472 * specification, ignored bits are to be preserved when writing.
473 * Normally, this would mean a read/modify/write sequence. However,
474 * preserving a bit in the status register is different. Writing a
475 * one clears the status, and writing a zero preserves the status.
476 * Therefore, we must always write zero to the ignored bit.
477 *
478 * This behavior is clarified in the ACPI 4.0 specification.
479 */
480 value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
Bob Moore967440e32006-06-23 17:04:00 -0400481
Bob Moorec520aba2009-02-18 14:20:12 +0800482 status = acpi_hw_write_multiple(value,
483 &acpi_gbl_xpm1a_status,
484 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486
Lv Zheng75c80442012-12-19 05:36:49 +0000487 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Bob Moorec520aba2009-02-18 14:20:12 +0800489 status = acpi_hw_write_multiple(value,
490 &acpi_gbl_xpm1a_enable,
491 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493
Bob Moorec520aba2009-02-18 14:20:12 +0800494 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Bob Moore967440e32006-06-23 17:04:00 -0400495 /*
496 * Perform a read first to preserve certain bits (per ACPI spec)
Bob Moorec520aba2009-02-18 14:20:12 +0800497 * Note: This includes SCI_EN, we never want to change this bit
Bob Moore967440e32006-06-23 17:04:00 -0400498 */
Bob Moorec520aba2009-02-18 14:20:12 +0800499 status = acpi_hw_read_multiple(&read_value,
500 &acpi_gbl_FADT.
501 xpm1a_control_block,
502 &acpi_gbl_FADT.
503 xpm1b_control_block);
Bob Moore967440e32006-06-23 17:04:00 -0400504 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400505 goto exit;
Bob Moore967440e32006-06-23 17:04:00 -0400506 }
507
508 /* Insert the bits to be preserved */
509
510 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
511 read_value);
512
513 /* Now we can write the data */
514
Bob Moorec520aba2009-02-18 14:20:12 +0800515 status = acpi_hw_write_multiple(value,
516 &acpi_gbl_FADT.
517 xpm1a_control_block,
518 &acpi_gbl_FADT.
519 xpm1b_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521
Len Brown4be44fc2005-08-05 00:44:28 -0400522 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Bob Moore20869dc2009-03-13 09:10:46 +0800523 /*
524 * For control registers, all reserved bits must be preserved,
525 * as per the ACPI spec.
526 */
527 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800528 acpi_hw_read(&read_value,
529 &acpi_gbl_FADT.xpm2_control_block);
Bob Moore20869dc2009-03-13 09:10:46 +0800530 if (ACPI_FAILURE(status)) {
531 goto exit;
532 }
533
534 /* Insert the bits to be preserved */
535
536 ACPI_INSERT_BITS(value, ACPI_PM2_CONTROL_PRESERVED_BITS,
537 read_value);
538
Bob Moorec6b57742009-06-24 09:44:06 +0800539 status =
540 acpi_hw_write(value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542
Len Brown4be44fc2005-08-05 00:44:28 -0400543 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Bob Moorec6b57742009-06-24 09:44:06 +0800545 status = acpi_hw_write(value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
547
Len Brown4be44fc2005-08-05 00:44:28 -0400548 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* SMI_CMD is currently always in IO space */
551
Bob Mooref3d2e782007-02-02 19:48:18 +0300552 status =
Bob Moore7f071902009-03-19 09:37:47 +0800553 acpi_hw_write_port(acpi_gbl_FADT.smi_command, value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000557
Bob Mooref6a22b02010-03-05 17:56:40 +0800558 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 status = AE_BAD_PARAMETER;
560 break;
561 }
562
Lv Zheng10622bf2013-10-29 09:30:02 +0800563exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400564 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
Bob Moorec520aba2009-02-18 14:20:12 +0800566
567/******************************************************************************
568 *
569 * FUNCTION: acpi_hw_read_multiple
570 *
Bob Mooreba494be2012-07-12 09:40:10 +0800571 * PARAMETERS: value - Where the register value is returned
Bob Moorec520aba2009-02-18 14:20:12 +0800572 * register_a - First ACPI register (required)
573 * register_b - Second ACPI register (optional)
574 *
575 * RETURN: Status
576 *
577 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
578 *
579 ******************************************************************************/
580
581static acpi_status
582acpi_hw_read_multiple(u32 *value,
583 struct acpi_generic_address *register_a,
584 struct acpi_generic_address *register_b)
585{
586 u32 value_a = 0;
587 u32 value_b = 0;
588 acpi_status status;
589
590 /* The first register is always required */
591
Bob Moorec6b57742009-06-24 09:44:06 +0800592 status = acpi_hw_read(&value_a, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800593 if (ACPI_FAILURE(status)) {
594 return (status);
595 }
596
597 /* Second register is optional */
598
599 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800600 status = acpi_hw_read(&value_b, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800601 if (ACPI_FAILURE(status)) {
602 return (status);
603 }
604 }
605
Bob Mooreaefc7f92009-02-18 14:26:02 +0800606 /*
607 * OR the two return values together. No shifting or masking is necessary,
608 * because of how the PM1 registers are defined in the ACPI specification:
609 *
610 * "Although the bits can be split between the two register blocks (each
611 * register block has a unique pointer within the FADT), the bit positions
612 * are maintained. The register block with unimplemented bits (that is,
613 * those implemented in the other register block) always returns zeros,
614 * and writes have no side effects"
615 */
616 *value = (value_a | value_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800617 return (AE_OK);
618}
619
620/******************************************************************************
621 *
622 * FUNCTION: acpi_hw_write_multiple
623 *
Bob Mooreba494be2012-07-12 09:40:10 +0800624 * PARAMETERS: value - The value to write
Bob Moorec520aba2009-02-18 14:20:12 +0800625 * register_a - First ACPI register (required)
626 * register_b - Second ACPI register (optional)
627 *
628 * RETURN: Status
629 *
630 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
631 *
632 ******************************************************************************/
633
634static acpi_status
635acpi_hw_write_multiple(u32 value,
636 struct acpi_generic_address *register_a,
637 struct acpi_generic_address *register_b)
638{
639 acpi_status status;
640
641 /* The first register is always required */
642
Bob Moorec6b57742009-06-24 09:44:06 +0800643 status = acpi_hw_write(value, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800644 if (ACPI_FAILURE(status)) {
645 return (status);
646 }
647
Bob Mooreaefc7f92009-02-18 14:26:02 +0800648 /*
649 * Second register is optional
650 *
651 * No bit shifting or clearing is necessary, because of how the PM1
652 * registers are defined in the ACPI specification:
653 *
654 * "Although the bits can be split between the two register blocks (each
655 * register block has a unique pointer within the FADT), the bit positions
656 * are maintained. The register block with unimplemented bits (that is,
657 * those implemented in the other register block) always returns zeros,
658 * and writes have no side effects"
659 */
Bob Moorec520aba2009-02-18 14:20:12 +0800660 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800661 status = acpi_hw_write(value, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800662 }
663
664 return (status);
665}
Bob Moore33620c52012-02-14 18:14:27 +0800666
667#endif /* !ACPI_REDUCED_HARDWARE */