2 /*******************************************************************************
4 * Module Name: hwregs - Read/write access functions for the various ACPI
5 * control and status registers.
8 ******************************************************************************/
11 * Copyright (C) 2000, 2001 R. Byron Moore
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #define _COMPONENT ACPI_HARDWARE
34 MODULE_NAME ("hwregs")
37 /*******************************************************************************
39 * FUNCTION: Acpi_hw_get_bit_shift
41 * PARAMETERS: Mask - Input mask to determine bit shift from.
42 * Must have at least 1 bit set.
44 * RETURN: Bit location of the lsb of the mask
46 * DESCRIPTION: Returns the bit number for the low order bit that's set.
48 ******************************************************************************/
51 acpi_hw_get_bit_shift (
57 FUNCTION_TRACE ("Hw_get_bit_shift");
60 for (shift = 0; ((mask >> shift) & 1) == 0; shift++) { ; }
66 /*******************************************************************************
68 * FUNCTION: Acpi_hw_clear_acpi_status
74 * DESCRIPTION: Clears all fixed and general purpose status bits
76 ******************************************************************************/
79 acpi_hw_clear_acpi_status (void)
85 FUNCTION_TRACE ("Hw_clear_acpi_status");
88 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "About to write %04X to %04X\n",
90 (u16) ACPI_GET_ADDRESS (acpi_gbl_FADT->Xpm1a_evt_blk.address)));
93 acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
95 acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, PM1_STS, ALL_FIXED_STS_BITS);
98 if (ACPI_VALID_ADDRESS (acpi_gbl_FADT->Xpm1b_evt_blk.address)) {
99 acpi_os_write_port ((ACPI_IO_ADDRESS)
100 ACPI_GET_ADDRESS (acpi_gbl_FADT->Xpm1b_evt_blk.address),
101 ALL_FIXED_STS_BITS, 16);
104 /* now clear the GPE Bits */
106 if (acpi_gbl_FADT->gpe0blk_len) {
107 gpe_length = (u16) DIV_2 (acpi_gbl_FADT->gpe0blk_len);
109 for (index = 0; index < gpe_length; index++) {
110 acpi_os_write_port ((ACPI_IO_ADDRESS) (
111 ACPI_GET_ADDRESS (acpi_gbl_FADT->Xgpe0blk.address) + index),
116 if (acpi_gbl_FADT->gpe1_blk_len) {
117 gpe_length = (u16) DIV_2 (acpi_gbl_FADT->gpe1_blk_len);
119 for (index = 0; index < gpe_length; index++) {
120 acpi_os_write_port ((ACPI_IO_ADDRESS) (
121 ACPI_GET_ADDRESS (acpi_gbl_FADT->Xgpe1_blk.address) + index),
126 acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
131 /*******************************************************************************
133 * FUNCTION: Acpi_hw_obtain_sleep_type_register_data
135 * PARAMETERS: Sleep_state - Numeric state requested
136 * *Slp_Typ_a - Pointer to byte to receive SLP_TYPa value
137 * *Slp_Typ_b - Pointer to byte to receive SLP_TYPb value
139 * RETURN: Status - ACPI status
141 * DESCRIPTION: Acpi_hw_obtain_sleep_type_register_data() obtains the SLP_TYP and
142 * SLP_TYPb values for the sleep state requested.
144 ******************************************************************************/
147 acpi_hw_obtain_sleep_type_register_data (
152 acpi_status status = AE_OK;
153 acpi_operand_object *obj_desc;
156 FUNCTION_TRACE ("Hw_obtain_sleep_type_register_data");
160 * Validate parameters
162 if ((sleep_state > ACPI_S_STATES_MAX) ||
163 !slp_typ_a || !slp_typ_b) {
164 return_ACPI_STATUS (AE_BAD_PARAMETER);
168 * Acpi_evaluate the namespace object containing the values for this state
170 status = acpi_ns_evaluate_by_name ((NATIVE_CHAR *) acpi_gbl_db_sleep_states[sleep_state],
172 if (ACPI_FAILURE (status)) {
173 return_ACPI_STATUS (status);
177 REPORT_ERROR (("Missing Sleep State object\n"));
178 return_ACPI_STATUS (AE_NOT_EXIST);
182 * We got something, now ensure it is correct. The object must
183 * be a package and must have at least 2 numeric values as the
187 /* Even though Acpi_evaluate_object resolves package references,
188 * Ns_evaluate dpesn't. So, we do it here.
190 status = acpi_ut_resolve_package_references(obj_desc);
192 if (obj_desc->package.count < 2) {
193 /* Must have at least two elements */
195 REPORT_ERROR (("Sleep State package does not have at least two elements\n"));
199 else if (((obj_desc->package.elements[0])->common.type !=
200 ACPI_TYPE_INTEGER) ||
201 ((obj_desc->package.elements[1])->common.type !=
202 ACPI_TYPE_INTEGER)) {
205 REPORT_ERROR (("Sleep State package elements are not both of type Number\n"));
211 * Valid _Sx_ package size, type, and value
213 *slp_typ_a = (u8) (obj_desc->package.elements[0])->integer.value;
215 *slp_typ_b = (u8) (obj_desc->package.elements[1])->integer.value;
219 if (ACPI_FAILURE (status)) {
220 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Bad Sleep object %p type %X\n",
221 obj_desc, obj_desc->common.type));
224 acpi_ut_remove_reference (obj_desc);
226 return_ACPI_STATUS (status);
230 /*******************************************************************************
232 * FUNCTION: Acpi_hw_register_bit_access
234 * PARAMETERS: Read_write - Either ACPI_READ or ACPI_WRITE.
235 * Use_lock - Lock the hardware
236 * Register_id - index of ACPI Register to access
237 * Value - (only used on write) value to write to the
238 * Register. Shifted all the way right.
240 * RETURN: Value written to or read from specified Register. This value
241 * is shifted all the way right.
243 * DESCRIPTION: Generic ACPI Register read/write function.
245 ******************************************************************************/
248 acpi_hw_register_bit_access (
249 NATIVE_UINT read_write,
252 ...) /* Value (only used on write) */
254 u32 register_value = 0;
260 FUNCTION_TRACE ("Hw_register_bit_access");
263 if (read_write == ACPI_WRITE) {
264 va_start (marker, register_id);
265 value = va_arg (marker, u32);
269 if (ACPI_MTX_LOCK == use_lock) {
270 acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
274 * Decode the Register ID
275 * Register id = Register block id | bit id
277 * Check bit id to fine locate Register offset.
278 * Check Mask to determine Register offset, and then read-write.
280 switch (REGISTER_BLOCK_ID (register_id)) {
283 switch (register_id) {
297 mask = PWRBTN_STS_MASK;
301 mask = SLPBTN_STS_MASK;
317 register_value = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, PM1_STS);
319 if (read_write == ACPI_WRITE) {
321 * Status Registers are different from the rest. Clear by
322 * writing 1, writing 0 has no effect. So, the only relevent
323 * information is the single bit we're interested in, all
324 * others should be written as 0 so they will be left
327 value <<= acpi_hw_get_bit_shift (mask);
331 acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, PM1_STS,
342 switch (register_id) {
352 mask = PWRBTN_EN_MASK;
356 mask = SLPBTN_EN_MASK;
368 register_value = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, PM1_EN);
370 if (read_write == ACPI_WRITE) {
371 register_value &= ~mask;
372 value <<= acpi_hw_get_bit_shift (mask);
374 register_value |= value;
376 acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, PM1_EN, (u16) register_value);
384 switch (register_id) {
399 mask = SLP_TYPE_X_MASK;
413 * Read the PM1 Control register.
414 * Note that at this level, the fact that there are actually TWO
415 * registers (A and B) and that B may not exist, are abstracted.
417 register_value = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, PM1_CONTROL);
419 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "PM1 control: Read %X\n", register_value));
421 if (read_write == ACPI_WRITE) {
422 register_value &= ~mask;
423 value <<= acpi_hw_get_bit_shift (mask);
425 register_value |= value;
428 * SLP_TYPE_x Registers are written differently
429 * than any other control Registers with
430 * respect to A and B Registers. The value
431 * for A may be different than the value for B
433 * Therefore, pass the Register_id, not just generic PM1_CONTROL,
434 * because we need to do different things. Yuck.
436 acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, register_id,
437 (u16) register_value);
444 switch (register_id) {
454 register_value = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, PM2_CONTROL);
456 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "PM2 control: Read %X from %8.8X%8.8X\n",
457 register_value, HIDWORD(acpi_gbl_FADT->Xpm2_cnt_blk.address),
458 LODWORD(acpi_gbl_FADT->Xpm2_cnt_blk.address)));
460 if (read_write == ACPI_WRITE) {
461 register_value &= ~mask;
462 value <<= acpi_hw_get_bit_shift (mask);
464 register_value |= value;
466 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
468 HIDWORD(acpi_gbl_FADT->Xpm2_cnt_blk.address),
469 LODWORD(acpi_gbl_FADT->Xpm2_cnt_blk.address)));
471 acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
472 PM2_CONTROL, (u8) (register_value));
480 register_value = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
482 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "PM_TIMER: Read %X from %8.8X%8.8X\n",
484 HIDWORD(acpi_gbl_FADT->Xpm_tmr_blk.address),
485 LODWORD(acpi_gbl_FADT->Xpm_tmr_blk.address)));
495 /* Determine the bit to be accessed
499 * +--------+--------+--------+--------+
500 * | gpe_block_id | gpe_bit_number |
501 * +--------+--------+--------+--------+
503 * gpe_block_id is one of GPE[01]_EN_BLOCK and GPE[01]_STS_BLOCK
504 * gpe_bit_number is relative from the gpe_block (0x00~0xFF)
506 mask = REGISTER_BIT_ID(register_id); /* gpe_bit_number */
507 register_id = REGISTER_BLOCK_ID(register_id) | (mask >> 3);
508 mask = acpi_gbl_decode_to8bit [mask % 8];
511 * The base address of the GPE 0 Register Block
512 * Plus 1/2 the length of the GPE 0 Register Block
513 * The enable Register is the Register following the Status Register
514 * and each Register is defined as 1/2 of the total Register Block
518 * This sets the bit within Enable_bit that needs to be written to
519 * the Register indicated in Mask to a 1, all others are 0
522 /* Now get the current Enable Bits in the selected Reg */
524 register_value = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, register_id);
525 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "GPE Enable bits: Read %X from %X\n",
526 register_value, register_id));
528 if (read_write == ACPI_WRITE) {
529 register_value &= ~mask;
530 value <<= acpi_hw_get_bit_shift (mask);
532 register_value |= value;
535 * This write will put the Action state into the General Purpose
536 * Enable Register indexed by the value in Mask
538 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "About to write %04X to %04X\n",
539 register_value, register_id));
540 acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK, register_id,
541 (u8) register_value);
542 register_value = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
549 case PROCESSOR_BLOCK:
551 /* Not used by any callers at this time - therefore, not implemented */
559 if (ACPI_MTX_LOCK == use_lock) {
560 acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
564 register_value &= mask;
565 register_value >>= acpi_hw_get_bit_shift (mask);
567 ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Register I/O: returning %X\n", register_value));
568 return_VALUE (register_value);
572 /******************************************************************************
574 * FUNCTION: Acpi_hw_register_read
576 * PARAMETERS: Use_lock - Mutex hw access.
577 * Register_id - Register_iD + Offset.
579 * RETURN: Value read or written.
581 * DESCRIPTION: Acpi register read function. Registers are read at the
584 ******************************************************************************/
587 acpi_hw_register_read (
595 FUNCTION_TRACE ("Hw_register_read");
598 if (ACPI_MTX_LOCK == use_lock) {
599 acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
603 switch (REGISTER_BLOCK_ID(register_id)) {
604 case PM1_STS: /* 16-bit access */
606 value = acpi_hw_low_level_read (16, &acpi_gbl_FADT->Xpm1a_evt_blk, 0);
607 value |= acpi_hw_low_level_read (16, &acpi_gbl_FADT->Xpm1b_evt_blk, 0);
611 case PM1_EN: /* 16-bit access*/
613 bank_offset = DIV_2 (acpi_gbl_FADT->pm1_evt_len);
614 value = acpi_hw_low_level_read (16, &acpi_gbl_FADT->Xpm1a_evt_blk, bank_offset);
615 value |= acpi_hw_low_level_read (16, &acpi_gbl_FADT->Xpm1b_evt_blk, bank_offset);
619 case PM1_CONTROL: /* 16-bit access */
621 value = acpi_hw_low_level_read (16, &acpi_gbl_FADT->Xpm1a_cnt_blk, 0);
622 value |= acpi_hw_low_level_read (16, &acpi_gbl_FADT->Xpm1b_cnt_blk, 0);
626 case PM2_CONTROL: /* 8-bit access */
628 value = acpi_hw_low_level_read (8, &acpi_gbl_FADT->Xpm2_cnt_blk, 0);
632 case PM_TIMER: /* 32-bit access */
634 value = acpi_hw_low_level_read (32, &acpi_gbl_FADT->Xpm_tmr_blk, 0);
639 * For the GPE? Blocks, the lower word of Register_id contains the
640 * byte offset for which to read, as each part of each block may be
641 * several bytes long.
643 case GPE0_STS_BLOCK: /* 8-bit access */
645 bank_offset = REGISTER_BIT_ID(register_id);
646 value = acpi_hw_low_level_read (8, &acpi_gbl_FADT->Xgpe0blk, bank_offset);
649 case GPE0_EN_BLOCK: /* 8-bit access */
651 bank_offset = DIV_2 (acpi_gbl_FADT->gpe0blk_len) + REGISTER_BIT_ID(register_id);
652 value = acpi_hw_low_level_read (8, &acpi_gbl_FADT->Xgpe0blk, bank_offset);
655 case GPE1_STS_BLOCK: /* 8-bit access */
657 bank_offset = REGISTER_BIT_ID(register_id);
658 value = acpi_hw_low_level_read (8, &acpi_gbl_FADT->Xgpe1_blk, bank_offset);
661 case GPE1_EN_BLOCK: /* 8-bit access */
663 bank_offset = DIV_2 (acpi_gbl_FADT->gpe1_blk_len) + REGISTER_BIT_ID(register_id);
664 value = acpi_hw_low_level_read (8, &acpi_gbl_FADT->Xgpe1_blk, bank_offset);
667 case SMI_CMD_BLOCK: /* 8bit */
669 acpi_os_read_port (acpi_gbl_FADT->smi_cmd, &value, 8);
673 /* Value will be returned as 0 */
678 if (ACPI_MTX_LOCK == use_lock) {
679 acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
682 return_VALUE (value);
686 /******************************************************************************
688 * FUNCTION: Acpi_hw_register_write
690 * PARAMETERS: Use_lock - Mutex hw access.
691 * Register_id - Register_iD + Offset.
693 * RETURN: Value read or written.
695 * DESCRIPTION: Acpi register Write function. Registers are written at the
698 ******************************************************************************/
701 acpi_hw_register_write (
709 FUNCTION_TRACE ("Hw_register_write");
712 if (ACPI_MTX_LOCK == use_lock) {
713 acpi_ut_acquire_mutex (ACPI_MTX_HARDWARE);
717 switch (REGISTER_BLOCK_ID (register_id)) {
718 case PM1_STS: /* 16-bit access */
720 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1a_evt_blk, 0);
721 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1b_evt_blk, 0);
725 case PM1_EN: /* 16-bit access*/
727 bank_offset = DIV_2 (acpi_gbl_FADT->pm1_evt_len);
728 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1a_evt_blk, bank_offset);
729 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1b_evt_blk, bank_offset);
733 case PM1_CONTROL: /* 16-bit access */
735 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1a_cnt_blk, 0);
736 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1b_cnt_blk, 0);
740 case PM1A_CONTROL: /* 16-bit access */
742 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1a_cnt_blk, 0);
746 case PM1B_CONTROL: /* 16-bit access */
748 acpi_hw_low_level_write (16, value, &acpi_gbl_FADT->Xpm1b_cnt_blk, 0);
752 case PM2_CONTROL: /* 8-bit access */
754 acpi_hw_low_level_write (8, value, &acpi_gbl_FADT->Xpm2_cnt_blk, 0);
758 case PM_TIMER: /* 32-bit access */
760 acpi_hw_low_level_write (32, value, &acpi_gbl_FADT->Xpm_tmr_blk, 0);
764 case GPE0_STS_BLOCK: /* 8-bit access */
766 bank_offset = REGISTER_BIT_ID(register_id);
767 acpi_hw_low_level_write (8, value, &acpi_gbl_FADT->Xgpe0blk, bank_offset);
771 case GPE0_EN_BLOCK: /* 8-bit access */
773 bank_offset = DIV_2 (acpi_gbl_FADT->gpe0blk_len) + REGISTER_BIT_ID(register_id);
774 acpi_hw_low_level_write (8, value, &acpi_gbl_FADT->Xgpe0blk, bank_offset);
778 case GPE1_STS_BLOCK: /* 8-bit access */
780 bank_offset = REGISTER_BIT_ID(register_id);
781 acpi_hw_low_level_write (8, value, &acpi_gbl_FADT->Xgpe1_blk, bank_offset);
785 case GPE1_EN_BLOCK: /* 8-bit access */
787 bank_offset = DIV_2 (acpi_gbl_FADT->gpe1_blk_len) + REGISTER_BIT_ID(register_id);
788 acpi_hw_low_level_write (8, value, &acpi_gbl_FADT->Xgpe1_blk, bank_offset);
792 case SMI_CMD_BLOCK: /* 8bit */
794 /* For 2.0, SMI_CMD is always in IO space */
795 /* TBD: what about 1.0? 0.71? */
797 acpi_os_write_port (acpi_gbl_FADT->smi_cmd, value, 8);
807 if (ACPI_MTX_LOCK == use_lock) {
808 acpi_ut_release_mutex (ACPI_MTX_HARDWARE);
815 /******************************************************************************
817 * FUNCTION: Acpi_hw_low_level_read
819 * PARAMETERS: Register - GAS register structure
820 * Offset - Offset from the base address in the GAS
821 * Width - 8, 16, or 32
825 * DESCRIPTION: Read from either memory, IO, or PCI config space.
827 ******************************************************************************/
830 acpi_hw_low_level_read (
832 acpi_generic_address *reg,
836 ACPI_PHYSICAL_ADDRESS mem_address;
837 ACPI_IO_ADDRESS io_address;
846 * Must have a valid pointer to a GAS structure, and
847 * a non-zero address within
850 (!ACPI_VALID_ADDRESS (reg->address))) {
856 * Three address spaces supported:
857 * Memory, Io, or PCI config.
859 switch (reg->address_space_id) {
860 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
862 mem_address = (ACPI_PHYSICAL_ADDRESS) (ACPI_GET_ADDRESS (reg->address) + offset);
864 acpi_os_read_memory (mem_address, &value, width);
868 case ACPI_ADR_SPACE_SYSTEM_IO:
870 io_address = (ACPI_IO_ADDRESS) (ACPI_GET_ADDRESS (reg->address) + offset);
872 acpi_os_read_port (io_address, &value, width);
876 case ACPI_ADR_SPACE_PCI_CONFIG:
880 pci_id.device = ACPI_PCI_DEVICE (ACPI_GET_ADDRESS (reg->address));
881 pci_id.function = ACPI_PCI_FUNCTION (ACPI_GET_ADDRESS (reg->address));
882 pci_register = (u16) (ACPI_PCI_REGISTER (ACPI_GET_ADDRESS (reg->address)) + offset);
884 acpi_os_read_pci_configuration (&pci_id, pci_register, &value, width);
892 /******************************************************************************
894 * FUNCTION: Acpi_hw_low_level_write
896 * PARAMETERS: Width - 8, 16, or 32
897 * Value - To be written
898 * Register - GAS register structure
899 * Offset - Offset from the base address in the GAS
904 * DESCRIPTION: Read from either memory, IO, or PCI config space.
906 ******************************************************************************/
909 acpi_hw_low_level_write (
912 acpi_generic_address *reg,
915 ACPI_PHYSICAL_ADDRESS mem_address;
916 ACPI_IO_ADDRESS io_address;
925 * Must have a valid pointer to a GAS structure, and
926 * a non-zero address within
929 (!ACPI_VALID_ADDRESS (reg->address))) {
935 * Three address spaces supported:
936 * Memory, Io, or PCI config.
938 switch (reg->address_space_id) {
939 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
941 mem_address = (ACPI_PHYSICAL_ADDRESS) (ACPI_GET_ADDRESS (reg->address) + offset);
943 acpi_os_write_memory (mem_address, value, width);
947 case ACPI_ADR_SPACE_SYSTEM_IO:
949 io_address = (ACPI_IO_ADDRESS) (ACPI_GET_ADDRESS (reg->address) + offset);
951 acpi_os_write_port (io_address, value, width);
955 case ACPI_ADR_SPACE_PCI_CONFIG:
959 pci_id.device = ACPI_PCI_DEVICE (ACPI_GET_ADDRESS (reg->address));
960 pci_id.function = ACPI_PCI_FUNCTION (ACPI_GET_ADDRESS (reg->address));
961 pci_register = (u16) (ACPI_PCI_REGISTER (ACPI_GET_ADDRESS (reg->address)) + offset);
963 acpi_os_write_pci_configuration (&pci_id, pci_register, value, width);