blob: 892e677f524f87f3c78fe494a4c0230e7137b496 [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 */
Lv Zhengb314a172016-05-05 12:58:39 +080054static u8
55acpi_hw_get_access_bit_width(struct acpi_generic_address *reg,
56 u8 max_bit_width);
57
Bob Moorec520aba2009-02-18 14:20:12 +080058static acpi_status
59acpi_hw_read_multiple(u32 *value,
60 struct acpi_generic_address *register_a,
61 struct acpi_generic_address *register_b);
62
63static acpi_status
64acpi_hw_write_multiple(u32 value,
65 struct acpi_generic_address *register_a,
66 struct acpi_generic_address *register_b);
67
Bob Moore33620c52012-02-14 18:14:27 +080068#endif /* !ACPI_REDUCED_HARDWARE */
69
Bob Moorec6b57742009-06-24 09:44:06 +080070/******************************************************************************
71 *
Lv Zhengb314a172016-05-05 12:58:39 +080072 * FUNCTION: acpi_hw_get_access_bit_width
73 *
74 * PARAMETERS: reg - GAS register structure
75 * max_bit_width - Max bit_width supported (32 or 64)
76 *
77 * RETURN: Status
78 *
79 * DESCRIPTION: Obtain optimal access bit width
80 *
81 ******************************************************************************/
82
83static u8
84acpi_hw_get_access_bit_width(struct acpi_generic_address *reg, u8 max_bit_width)
85{
86 u64 address;
87
88 if (!reg->access_width) {
89 /*
90 * Detect old register descriptors where only the bit_width field
91 * makes senses. The target address is copied to handle possible
92 * alignment issues.
93 */
94 ACPI_MOVE_64_TO_64(&address, &reg->address);
95 if (!reg->bit_offset && reg->bit_width &&
96 ACPI_IS_POWER_OF_TWO(reg->bit_width) &&
97 ACPI_IS_ALIGNED(reg->bit_width, 8) &&
98 ACPI_IS_ALIGNED(address, reg->bit_width)) {
99 return (reg->bit_width);
100 } else {
101 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
102 return (32);
103 } else {
104 return (max_bit_width);
105 }
106 }
107 } else {
108 return (1 << (reg->access_width + 2));
109 }
110}
111
112/******************************************************************************
113 *
Bob Moorec6b57742009-06-24 09:44:06 +0800114 * FUNCTION: acpi_hw_validate_register
115 *
Bob Mooreba494be2012-07-12 09:40:10 +0800116 * PARAMETERS: reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800117 * max_bit_width - Max bit_width supported (32 or 64)
Bob Mooreba494be2012-07-12 09:40:10 +0800118 * address - Pointer to where the gas->address
Bob Moorec6b57742009-06-24 09:44:06 +0800119 * is returned
120 *
121 * RETURN: Status
122 *
123 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
124 * pointer, Address, space_id, bit_width, and bit_offset.
125 *
126 ******************************************************************************/
127
128acpi_status
129acpi_hw_validate_register(struct acpi_generic_address *reg,
130 u8 max_bit_width, u64 *address)
131{
Lv Zheng920de6e2016-03-24 09:41:09 +0800132 u8 bit_width;
133 u8 access_width;
Bob Moorec6b57742009-06-24 09:44:06 +0800134
135 /* Must have a valid pointer to a GAS structure */
136
137 if (!reg) {
138 return (AE_BAD_PARAMETER);
139 }
140
141 /*
142 * Copy the target address. This handles possible alignment issues.
143 * Address must not be null. A null address also indicates an optional
144 * ACPI register that is not supported, so no error message.
145 */
146 ACPI_MOVE_64_TO_64(address, &reg->address);
147 if (!(*address)) {
148 return (AE_BAD_ADDRESS);
149 }
150
Bob Mooreba494be2012-07-12 09:40:10 +0800151 /* Validate the space_ID */
Bob Moorec6b57742009-06-24 09:44:06 +0800152
153 if ((reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
154 (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
155 ACPI_ERROR((AE_INFO,
156 "Unsupported address space: 0x%X", reg->space_id));
157 return (AE_SUPPORT);
158 }
159
Lv Zheng920de6e2016-03-24 09:41:09 +0800160 /* Validate the access_width */
Bob Moorec6b57742009-06-24 09:44:06 +0800161
Lv Zheng920de6e2016-03-24 09:41:09 +0800162 if (reg->access_width > 4) {
Bob Moorec6b57742009-06-24 09:44:06 +0800163 ACPI_ERROR((AE_INFO,
Lv Zheng920de6e2016-03-24 09:41:09 +0800164 "Unsupported register access width: 0x%X",
165 reg->access_width));
Bob Moorec6b57742009-06-24 09:44:06 +0800166 return (AE_SUPPORT);
167 }
168
Lv Zheng920de6e2016-03-24 09:41:09 +0800169 /* Validate the bit_width, convert access_width into number of bits */
Bob Moorec6b57742009-06-24 09:44:06 +0800170
Lv Zhengb314a172016-05-05 12:58:39 +0800171 access_width = acpi_hw_get_access_bit_width(reg, max_bit_width);
Lv Zheng920de6e2016-03-24 09:41:09 +0800172 bit_width =
173 ACPI_ROUND_UP(reg->bit_offset + reg->bit_width, access_width);
174 if (max_bit_width < bit_width) {
Bob Moorec6b57742009-06-24 09:44:06 +0800175 ACPI_WARNING((AE_INFO,
Lv Zheng920de6e2016-03-24 09:41:09 +0800176 "Requested bit width 0x%X is smaller than register bit width 0x%X",
177 max_bit_width, bit_width));
178 return (AE_SUPPORT);
Bob Moorec6b57742009-06-24 09:44:06 +0800179 }
180
181 return (AE_OK);
182}
183
184/******************************************************************************
185 *
186 * FUNCTION: acpi_hw_read
187 *
Bob Mooreba494be2012-07-12 09:40:10 +0800188 * PARAMETERS: value - Where the value is returned
189 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800190 *
191 * RETURN: Status
192 *
193 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
194 * version of acpi_read, used internally since the overhead of
195 * 64-bit values is not needed.
196 *
197 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
198 * bit_width must be exactly 8, 16, or 32.
Bob Mooreba494be2012-07-12 09:40:10 +0800199 * space_ID must be system_memory or system_IO.
Bob Moorec6b57742009-06-24 09:44:06 +0800200 * bit_offset and access_width are currently ignored, as there has
201 * not been a need to implement these.
202 *
203 ******************************************************************************/
204
205acpi_status acpi_hw_read(u32 *value, struct acpi_generic_address *reg)
206{
207 u64 address;
Bob Moore653f4b52012-02-14 18:29:55 +0800208 u64 value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800209 acpi_status status;
210
211 ACPI_FUNCTION_NAME(hw_read);
212
213 /* Validate contents of the GAS register */
214
215 status = acpi_hw_validate_register(reg, 32, &address);
216 if (ACPI_FAILURE(status)) {
217 return (status);
218 }
219
220 /* Initialize entire 32-bit return value to zero */
221
222 *value = 0;
223
224 /*
225 * Two address spaces supported: Memory or IO. PCI_Config is
226 * not supported here because the GAS structure is insufficient
227 */
228 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
229 status = acpi_os_read_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800230 address, &value64, reg->bit_width);
231
232 *value = (u32)value64;
Bob Moorec6b57742009-06-24 09:44:06 +0800233 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
234
235 status = acpi_hw_read_port((acpi_io_address)
236 address, value, reg->bit_width);
237 }
238
239 ACPI_DEBUG_PRINT((ACPI_DB_IO,
240 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
241 *value, reg->bit_width, ACPI_FORMAT_UINT64(address),
242 acpi_ut_get_region_name(reg->space_id)));
243
244 return (status);
245}
246
247/******************************************************************************
248 *
249 * FUNCTION: acpi_hw_write
250 *
Bob Mooreba494be2012-07-12 09:40:10 +0800251 * PARAMETERS: value - Value to be written
252 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800253 *
254 * RETURN: Status
255 *
256 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
257 * version of acpi_write, used internally since the overhead of
258 * 64-bit values is not needed.
259 *
260 ******************************************************************************/
261
262acpi_status acpi_hw_write(u32 value, struct acpi_generic_address *reg)
263{
264 u64 address;
265 acpi_status status;
266
267 ACPI_FUNCTION_NAME(hw_write);
268
269 /* Validate contents of the GAS register */
270
271 status = acpi_hw_validate_register(reg, 32, &address);
272 if (ACPI_FAILURE(status)) {
273 return (status);
274 }
275
276 /*
277 * Two address spaces supported: Memory or IO. PCI_Config is
278 * not supported here because the GAS structure is insufficient
279 */
280 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
281 status = acpi_os_write_memory((acpi_physical_address)
Bob Moore653f4b52012-02-14 18:29:55 +0800282 address, (u64)value,
283 reg->bit_width);
Bob Moorec6b57742009-06-24 09:44:06 +0800284 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
285
286 status = acpi_hw_write_port((acpi_io_address)
287 address, value, reg->bit_width);
288 }
289
290 ACPI_DEBUG_PRINT((ACPI_DB_IO,
291 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
292 value, reg->bit_width, ACPI_FORMAT_UINT64(address),
293 acpi_ut_get_region_name(reg->space_id)));
294
295 return (status);
296}
297
Bob Moore33620c52012-02-14 18:14:27 +0800298#if (!ACPI_REDUCED_HARDWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*******************************************************************************
300 *
301 * FUNCTION: acpi_hw_clear_acpi_status
302 *
Bob Moored8c71b62007-02-02 19:48:21 +0300303 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *
Bob Moore7db5d822008-12-30 11:04:48 +0800305 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
307 * DESCRIPTION: Clears all fixed and general purpose status bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 *
309 ******************************************************************************/
Bob Moorec520aba2009-02-18 14:20:12 +0800310
Bob Moored8c71b62007-02-02 19:48:21 +0300311acpi_status acpi_hw_clear_acpi_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Len Brown4be44fc2005-08-05 00:44:28 -0400313 acpi_status status;
Bob Moore4c90ece2006-06-08 16:29:00 -0400314 acpi_cpu_flags lock_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Bob Mooreb229cf92006-04-21 17:15:00 -0400316 ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Bob Moore8eb7b242009-04-22 10:28:22 +0800318 ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400319 ACPI_BITMASK_ALL_FIXED_STATUS,
Bob Moore8eb7b242009-04-22 10:28:22 +0800320 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Bob Moore4c90ece2006-06-08 16:29:00 -0400322 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Bob Moore227243a2009-02-18 14:24:50 +0800324 /* Clear the fixed events in PM1 A/B */
Bob Moore531c6332009-02-18 14:06:12 +0800325
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400326 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400327 ACPI_BITMASK_ALL_FIXED_STATUS);
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600328
329 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
330
Lv Zheng7d3e83b2014-07-08 10:08:19 +0800331 if (ACPI_FAILURE(status)) {
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600332 goto exit;
Lv Zheng7d3e83b2014-07-08 10:08:19 +0800333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
336
Bob Mooree97d6bf2008-12-30 09:45:17 +0800337 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600339exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400340 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/*******************************************************************************
344 *
Bob Moore33620c52012-02-14 18:14:27 +0800345 * FUNCTION: acpi_hw_get_bit_register_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
347 * PARAMETERS: register_id - Index of ACPI Register to access
348 *
Robert Moore44f6c012005-04-18 22:49:35 -0400349 * RETURN: The bitmask to be used when accessing the register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
Robert Moore44f6c012005-04-18 22:49:35 -0400351 * DESCRIPTION: Map register_id into a register bitmask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
353 ******************************************************************************/
Bob Moore7db5d822008-12-30 11:04:48 +0800354
Len Brown4be44fc2005-08-05 00:44:28 -0400355struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Bob Moore4a90c7e2006-01-13 16:22:00 -0500357 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (register_id > ACPI_BITREG_MAX) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800360 ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: 0x%X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500361 register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return (NULL);
363 }
364
365 return (&acpi_gbl_bit_register_info[register_id]);
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/******************************************************************************
369 *
Bob Moore32c9ef92009-02-18 14:36:05 +0800370 * FUNCTION: acpi_hw_write_pm1_control
371 *
372 * PARAMETERS: pm1a_control - Value to be written to PM1A control
373 * pm1b_control - Value to be written to PM1B control
374 *
375 * RETURN: Status
376 *
377 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
378 * different than than the PM1 A/B status and enable registers
379 * in that different values can be written to the A/B registers.
380 * Most notably, the SLP_TYP bits can be different, as per the
381 * values returned from the _Sx predefined methods.
382 *
383 ******************************************************************************/
384
385acpi_status acpi_hw_write_pm1_control(u32 pm1a_control, u32 pm1b_control)
386{
387 acpi_status status;
388
389 ACPI_FUNCTION_TRACE(hw_write_pm1_control);
390
Bob Moorec6b57742009-06-24 09:44:06 +0800391 status =
392 acpi_hw_write(pm1a_control, &acpi_gbl_FADT.xpm1a_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800393 if (ACPI_FAILURE(status)) {
394 return_ACPI_STATUS(status);
395 }
396
397 if (acpi_gbl_FADT.xpm1b_control_block.address) {
398 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800399 acpi_hw_write(pm1b_control,
400 &acpi_gbl_FADT.xpm1b_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800401 }
402 return_ACPI_STATUS(status);
403}
404
405/******************************************************************************
406 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * FUNCTION: acpi_hw_register_read
408 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400409 * PARAMETERS: register_id - ACPI Register ID
Robert Moore44f6c012005-04-18 22:49:35 -0400410 * return_value - Where the register value is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
412 * RETURN: Status and the value read.
413 *
Bob Moore967440e32006-06-23 17:04:00 -0400414 * DESCRIPTION: Read from the specified ACPI register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
416 ******************************************************************************/
Lv Zheng3e8214e2012-12-19 05:37:15 +0000417acpi_status acpi_hw_register_read(u32 register_id, u32 *return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Bob Moorec520aba2009-02-18 14:20:12 +0800419 u32 value = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400420 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Bob Mooreb229cf92006-04-21 17:15:00 -0400422 ACPI_FUNCTION_TRACE(hw_register_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800425 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Bob Moorec520aba2009-02-18 14:20:12 +0800427 status = acpi_hw_read_multiple(&value,
428 &acpi_gbl_xpm1a_status,
429 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431
Bob Moorec520aba2009-02-18 14:20:12 +0800432 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Bob Moorec520aba2009-02-18 14:20:12 +0800434 status = acpi_hw_read_multiple(&value,
435 &acpi_gbl_xpm1a_enable,
436 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438
Bob Moorec520aba2009-02-18 14:20:12 +0800439 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Bob Moorec520aba2009-02-18 14:20:12 +0800441 status = acpi_hw_read_multiple(&value,
442 &acpi_gbl_FADT.
443 xpm1a_control_block,
444 &acpi_gbl_FADT.
445 xpm1b_control_block);
Lin Mingc3dd25f2009-03-19 09:51:01 +0800446
447 /*
448 * Zero the write-only bits. From the ACPI specification, "Hardware
449 * Write-Only Bits": "Upon reads to registers with write-only bits,
450 * software masks out all write-only bits."
451 */
452 value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454
Len Brown4be44fc2005-08-05 00:44:28 -0400455 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Bob Moorec6b57742009-06-24 09:44:06 +0800457 status =
458 acpi_hw_read(&value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460
Len Brown4be44fc2005-08-05 00:44:28 -0400461 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Bob Moorec6b57742009-06-24 09:44:06 +0800463 status = acpi_hw_read(&value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465
Len Brown4be44fc2005-08-05 00:44:28 -0400466 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Bob Mooref3d2e782007-02-02 19:48:18 +0300468 status =
Bob Moore7f071902009-03-19 09:37:47 +0800469 acpi_hw_read_port(acpi_gbl_FADT.smi_command, &value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471
472 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000473
Bob Mooref6a22b02010-03-05 17:56:40 +0800474 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 status = AE_BAD_PARAMETER;
476 break;
477 }
478
Len Brown4be44fc2005-08-05 00:44:28 -0400479 if (ACPI_SUCCESS(status)) {
Bob Moorec520aba2009-02-18 14:20:12 +0800480 *return_value = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
Len Brown4be44fc2005-08-05 00:44:28 -0400483 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/******************************************************************************
487 *
488 * FUNCTION: acpi_hw_register_write
489 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400490 * PARAMETERS: register_id - ACPI Register ID
Bob Mooreba494be2012-07-12 09:40:10 +0800491 * value - The value to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 *
493 * RETURN: Status
494 *
Bob Moore967440e32006-06-23 17:04:00 -0400495 * DESCRIPTION: Write to the specified ACPI register
496 *
497 * NOTE: In accordance with the ACPI specification, this function automatically
498 * preserves the value of the following bits, meaning that these bits cannot be
499 * changed via this interface:
500 *
501 * PM1_CONTROL[0] = SCI_EN
502 * PM1_CONTROL[9]
503 * PM1_STATUS[11]
504 *
505 * ACPI References:
506 * 1) Hardware Ignored Bits: When software writes to a register with ignored
507 * bit fields, it preserves the ignored bit fields
508 * 2) SCI_EN: OSPM always preserves this bit position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
510 ******************************************************************************/
511
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400512acpi_status acpi_hw_register_write(u32 register_id, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Len Brown4be44fc2005-08-05 00:44:28 -0400514 acpi_status status;
Bob Moore967440e32006-06-23 17:04:00 -0400515 u32 read_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Bob Mooreb229cf92006-04-21 17:15:00 -0400517 ACPI_FUNCTION_TRACE(hw_register_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800520 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Bob Moore8636f8d2009-03-09 16:32:20 +0800521 /*
522 * Handle the "ignored" bit in PM1 Status. According to the ACPI
523 * specification, ignored bits are to be preserved when writing.
524 * Normally, this would mean a read/modify/write sequence. However,
525 * preserving a bit in the status register is different. Writing a
526 * one clears the status, and writing a zero preserves the status.
527 * Therefore, we must always write zero to the ignored bit.
528 *
529 * This behavior is clarified in the ACPI 4.0 specification.
530 */
531 value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
Bob Moore967440e32006-06-23 17:04:00 -0400532
Bob Moorec520aba2009-02-18 14:20:12 +0800533 status = acpi_hw_write_multiple(value,
534 &acpi_gbl_xpm1a_status,
535 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
537
Lv Zheng75c80442012-12-19 05:36:49 +0000538 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Bob Moorec520aba2009-02-18 14:20:12 +0800540 status = acpi_hw_write_multiple(value,
541 &acpi_gbl_xpm1a_enable,
542 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544
Bob Moorec520aba2009-02-18 14:20:12 +0800545 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Bob Moore967440e32006-06-23 17:04:00 -0400546 /*
547 * Perform a read first to preserve certain bits (per ACPI spec)
Bob Moorec520aba2009-02-18 14:20:12 +0800548 * Note: This includes SCI_EN, we never want to change this bit
Bob Moore967440e32006-06-23 17:04:00 -0400549 */
Bob Moorec520aba2009-02-18 14:20:12 +0800550 status = acpi_hw_read_multiple(&read_value,
551 &acpi_gbl_FADT.
552 xpm1a_control_block,
553 &acpi_gbl_FADT.
554 xpm1b_control_block);
Bob Moore967440e32006-06-23 17:04:00 -0400555 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400556 goto exit;
Bob Moore967440e32006-06-23 17:04:00 -0400557 }
558
559 /* Insert the bits to be preserved */
560
561 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
562 read_value);
563
564 /* Now we can write the data */
565
Bob Moorec520aba2009-02-18 14:20:12 +0800566 status = acpi_hw_write_multiple(value,
567 &acpi_gbl_FADT.
568 xpm1a_control_block,
569 &acpi_gbl_FADT.
570 xpm1b_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572
Len Brown4be44fc2005-08-05 00:44:28 -0400573 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Bob Moore20869dc2009-03-13 09:10:46 +0800574 /*
575 * For control registers, all reserved bits must be preserved,
576 * as per the ACPI spec.
577 */
578 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800579 acpi_hw_read(&read_value,
580 &acpi_gbl_FADT.xpm2_control_block);
Bob Moore20869dc2009-03-13 09:10:46 +0800581 if (ACPI_FAILURE(status)) {
582 goto exit;
583 }
584
585 /* Insert the bits to be preserved */
586
587 ACPI_INSERT_BITS(value, ACPI_PM2_CONTROL_PRESERVED_BITS,
588 read_value);
589
Bob Moorec6b57742009-06-24 09:44:06 +0800590 status =
591 acpi_hw_write(value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593
Len Brown4be44fc2005-08-05 00:44:28 -0400594 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Bob Moorec6b57742009-06-24 09:44:06 +0800596 status = acpi_hw_write(value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
598
Len Brown4be44fc2005-08-05 00:44:28 -0400599 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* SMI_CMD is currently always in IO space */
602
Bob Mooref3d2e782007-02-02 19:48:18 +0300603 status =
Bob Moore7f071902009-03-19 09:37:47 +0800604 acpi_hw_write_port(acpi_gbl_FADT.smi_command, value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000608
Bob Mooref6a22b02010-03-05 17:56:40 +0800609 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 status = AE_BAD_PARAMETER;
611 break;
612 }
613
Lv Zheng10622bf2013-10-29 09:30:02 +0800614exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400615 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Bob Moorec520aba2009-02-18 14:20:12 +0800617
618/******************************************************************************
619 *
620 * FUNCTION: acpi_hw_read_multiple
621 *
Bob Mooreba494be2012-07-12 09:40:10 +0800622 * PARAMETERS: value - Where the register value is returned
Bob Moorec520aba2009-02-18 14:20:12 +0800623 * register_a - First ACPI register (required)
624 * register_b - Second ACPI register (optional)
625 *
626 * RETURN: Status
627 *
628 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
629 *
630 ******************************************************************************/
631
632static acpi_status
633acpi_hw_read_multiple(u32 *value,
634 struct acpi_generic_address *register_a,
635 struct acpi_generic_address *register_b)
636{
637 u32 value_a = 0;
638 u32 value_b = 0;
639 acpi_status status;
640
641 /* The first register is always required */
642
Bob Moorec6b57742009-06-24 09:44:06 +0800643 status = acpi_hw_read(&value_a, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800644 if (ACPI_FAILURE(status)) {
645 return (status);
646 }
647
648 /* Second register is optional */
649
650 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800651 status = acpi_hw_read(&value_b, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800652 if (ACPI_FAILURE(status)) {
653 return (status);
654 }
655 }
656
Bob Mooreaefc7f92009-02-18 14:26:02 +0800657 /*
658 * OR the two return values together. No shifting or masking is necessary,
659 * because of how the PM1 registers are defined in the ACPI specification:
660 *
661 * "Although the bits can be split between the two register blocks (each
662 * register block has a unique pointer within the FADT), the bit positions
663 * are maintained. The register block with unimplemented bits (that is,
664 * those implemented in the other register block) always returns zeros,
665 * and writes have no side effects"
666 */
667 *value = (value_a | value_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800668 return (AE_OK);
669}
670
671/******************************************************************************
672 *
673 * FUNCTION: acpi_hw_write_multiple
674 *
Bob Mooreba494be2012-07-12 09:40:10 +0800675 * PARAMETERS: value - The value to write
Bob Moorec520aba2009-02-18 14:20:12 +0800676 * register_a - First ACPI register (required)
677 * register_b - Second ACPI register (optional)
678 *
679 * RETURN: Status
680 *
681 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
682 *
683 ******************************************************************************/
684
685static acpi_status
686acpi_hw_write_multiple(u32 value,
687 struct acpi_generic_address *register_a,
688 struct acpi_generic_address *register_b)
689{
690 acpi_status status;
691
692 /* The first register is always required */
693
Bob Moorec6b57742009-06-24 09:44:06 +0800694 status = acpi_hw_write(value, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800695 if (ACPI_FAILURE(status)) {
696 return (status);
697 }
698
Bob Mooreaefc7f92009-02-18 14:26:02 +0800699 /*
700 * Second register is optional
701 *
702 * No bit shifting or clearing is necessary, because of how the PM1
703 * registers are defined in the ACPI specification:
704 *
705 * "Although the bits can be split between the two register blocks (each
706 * register block has a unique pointer within the FADT), the bit positions
707 * are maintained. The register block with unimplemented bits (that is,
708 * those implemented in the other register block) always returns zeros,
709 * and writes have no side effects"
710 */
Bob Moorec520aba2009-02-18 14:20:12 +0800711 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800712 status = acpi_hw_write(value, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800713 }
714
715 return (status);
716}
Bob Moore33620c52012-02-14 18:14:27 +0800717
718#endif /* !ACPI_REDUCED_HARDWARE */