hwregs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*******************************************************************************
  2. *
  3. * Module Name: hwregs - Read/write access functions for the various ACPI
  4. * control and status registers.
  5. *
  6. ******************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2008, Intel Corp.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #include "acevents.h"
  47. #define _COMPONENT ACPI_HARDWARE
  48. ACPI_MODULE_NAME("hwregs")
  49. /* Local Prototypes */
  50. static acpi_status
  51. acpi_hw_read_multiple(u32 *value,
  52. struct acpi_generic_address *register_a,
  53. struct acpi_generic_address *register_b);
  54. static acpi_status
  55. acpi_hw_write_multiple(u32 value,
  56. struct acpi_generic_address *register_a,
  57. struct acpi_generic_address *register_b);
  58. /*******************************************************************************
  59. *
  60. * FUNCTION: acpi_hw_clear_acpi_status
  61. *
  62. * PARAMETERS: None
  63. *
  64. * RETURN: Status
  65. *
  66. * DESCRIPTION: Clears all fixed and general purpose status bits
  67. *
  68. ******************************************************************************/
  69. acpi_status acpi_hw_clear_acpi_status(void)
  70. {
  71. acpi_status status;
  72. acpi_cpu_flags lock_flags = 0;
  73. ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
  74. ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %0llX\n",
  75. ACPI_BITMASK_ALL_FIXED_STATUS,
  76. acpi_gbl_xpm1a_status.address));
  77. lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  78. /* Clear the fixed events in PM1 A/B */
  79. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  80. ACPI_BITMASK_ALL_FIXED_STATUS);
  81. if (ACPI_FAILURE(status)) {
  82. goto unlock_and_exit;
  83. }
  84. /* Clear the GPE Bits in all GPE registers in all GPE blocks */
  85. status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
  86. unlock_and_exit:
  87. acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  88. return_ACPI_STATUS(status);
  89. }
  90. /*******************************************************************************
  91. *
  92. * FUNCTION: acpi_hw_get_register_bit_mask
  93. *
  94. * PARAMETERS: register_id - Index of ACPI Register to access
  95. *
  96. * RETURN: The bitmask to be used when accessing the register
  97. *
  98. * DESCRIPTION: Map register_id into a register bitmask.
  99. *
  100. ******************************************************************************/
  101. struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
  102. {
  103. ACPI_FUNCTION_ENTRY();
  104. if (register_id > ACPI_BITREG_MAX) {
  105. ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: %X",
  106. register_id));
  107. return (NULL);
  108. }
  109. return (&acpi_gbl_bit_register_info[register_id]);
  110. }
  111. /******************************************************************************
  112. *
  113. * FUNCTION: acpi_hw_write_pm1_control
  114. *
  115. * PARAMETERS: pm1a_control - Value to be written to PM1A control
  116. * pm1b_control - Value to be written to PM1B control
  117. *
  118. * RETURN: Status
  119. *
  120. * DESCRIPTION: Write the PM1 A/B control registers. These registers are
  121. * different than than the PM1 A/B status and enable registers
  122. * in that different values can be written to the A/B registers.
  123. * Most notably, the SLP_TYP bits can be different, as per the
  124. * values returned from the _Sx predefined methods.
  125. *
  126. ******************************************************************************/
  127. acpi_status acpi_hw_write_pm1_control(u32 pm1a_control, u32 pm1b_control)
  128. {
  129. acpi_status status;
  130. ACPI_FUNCTION_TRACE(hw_write_pm1_control);
  131. status = acpi_write(pm1a_control, &acpi_gbl_FADT.xpm1a_control_block);
  132. if (ACPI_FAILURE(status)) {
  133. return_ACPI_STATUS(status);
  134. }
  135. if (acpi_gbl_FADT.xpm1b_control_block.address) {
  136. status =
  137. acpi_write(pm1b_control,
  138. &acpi_gbl_FADT.xpm1b_control_block);
  139. }
  140. return_ACPI_STATUS(status);
  141. }
  142. /******************************************************************************
  143. *
  144. * FUNCTION: acpi_hw_register_read
  145. *
  146. * PARAMETERS: register_id - ACPI Register ID
  147. * return_value - Where the register value is returned
  148. *
  149. * RETURN: Status and the value read.
  150. *
  151. * DESCRIPTION: Read from the specified ACPI register
  152. *
  153. ******************************************************************************/
  154. acpi_status
  155. acpi_hw_register_read(u32 register_id, u32 * return_value)
  156. {
  157. u32 value = 0;
  158. acpi_status status;
  159. ACPI_FUNCTION_TRACE(hw_register_read);
  160. switch (register_id) {
  161. case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
  162. status = acpi_hw_read_multiple(&value,
  163. &acpi_gbl_xpm1a_status,
  164. &acpi_gbl_xpm1b_status);
  165. break;
  166. case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
  167. status = acpi_hw_read_multiple(&value,
  168. &acpi_gbl_xpm1a_enable,
  169. &acpi_gbl_xpm1b_enable);
  170. break;
  171. case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
  172. status = acpi_hw_read_multiple(&value,
  173. &acpi_gbl_FADT.
  174. xpm1a_control_block,
  175. &acpi_gbl_FADT.
  176. xpm1b_control_block);
  177. break;
  178. case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
  179. status = acpi_read(&value, &acpi_gbl_FADT.xpm2_control_block);
  180. break;
  181. case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
  182. status = acpi_read(&value, &acpi_gbl_FADT.xpm_timer_block);
  183. break;
  184. case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
  185. status =
  186. acpi_os_read_port(acpi_gbl_FADT.smi_command, &value, 8);
  187. break;
  188. default:
  189. ACPI_ERROR((AE_INFO, "Unknown Register ID: %X", register_id));
  190. status = AE_BAD_PARAMETER;
  191. break;
  192. }
  193. if (ACPI_SUCCESS(status)) {
  194. *return_value = value;
  195. }
  196. return_ACPI_STATUS(status);
  197. }
  198. /******************************************************************************
  199. *
  200. * FUNCTION: acpi_hw_register_write
  201. *
  202. * PARAMETERS: register_id - ACPI Register ID
  203. * Value - The value to write
  204. *
  205. * RETURN: Status
  206. *
  207. * DESCRIPTION: Write to the specified ACPI register
  208. *
  209. * NOTE: In accordance with the ACPI specification, this function automatically
  210. * preserves the value of the following bits, meaning that these bits cannot be
  211. * changed via this interface:
  212. *
  213. * PM1_CONTROL[0] = SCI_EN
  214. * PM1_CONTROL[9]
  215. * PM1_STATUS[11]
  216. *
  217. * ACPI References:
  218. * 1) Hardware Ignored Bits: When software writes to a register with ignored
  219. * bit fields, it preserves the ignored bit fields
  220. * 2) SCI_EN: OSPM always preserves this bit position
  221. *
  222. ******************************************************************************/
  223. acpi_status acpi_hw_register_write(u32 register_id, u32 value)
  224. {
  225. acpi_status status;
  226. u32 read_value;
  227. ACPI_FUNCTION_TRACE(hw_register_write);
  228. switch (register_id) {
  229. case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
  230. /*
  231. * Handle the "ignored" bit in PM1 Status. According to the ACPI
  232. * specification, ignored bits are to be preserved when writing.
  233. * Normally, this would mean a read/modify/write sequence. However,
  234. * preserving a bit in the status register is different. Writing a
  235. * one clears the status, and writing a zero preserves the status.
  236. * Therefore, we must always write zero to the ignored bit.
  237. *
  238. * This behavior is clarified in the ACPI 4.0 specification.
  239. */
  240. value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
  241. status = acpi_hw_write_multiple(value,
  242. &acpi_gbl_xpm1a_status,
  243. &acpi_gbl_xpm1b_status);
  244. break;
  245. case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access */
  246. status = acpi_hw_write_multiple(value,
  247. &acpi_gbl_xpm1a_enable,
  248. &acpi_gbl_xpm1b_enable);
  249. break;
  250. case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
  251. /*
  252. * Perform a read first to preserve certain bits (per ACPI spec)
  253. * Note: This includes SCI_EN, we never want to change this bit
  254. */
  255. status = acpi_hw_read_multiple(&read_value,
  256. &acpi_gbl_FADT.
  257. xpm1a_control_block,
  258. &acpi_gbl_FADT.
  259. xpm1b_control_block);
  260. if (ACPI_FAILURE(status)) {
  261. goto exit;
  262. }
  263. /* Insert the bits to be preserved */
  264. ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
  265. read_value);
  266. /* Now we can write the data */
  267. status = acpi_hw_write_multiple(value,
  268. &acpi_gbl_FADT.
  269. xpm1a_control_block,
  270. &acpi_gbl_FADT.
  271. xpm1b_control_block);
  272. break;
  273. case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
  274. status = acpi_write(value, &acpi_gbl_FADT.xpm2_control_block);
  275. break;
  276. case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
  277. status = acpi_write(value, &acpi_gbl_FADT.xpm_timer_block);
  278. break;
  279. case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
  280. /* SMI_CMD is currently always in IO space */
  281. status =
  282. acpi_os_write_port(acpi_gbl_FADT.smi_command, value, 8);
  283. break;
  284. default:
  285. ACPI_ERROR((AE_INFO, "Unknown Register ID: %X", register_id));
  286. status = AE_BAD_PARAMETER;
  287. break;
  288. }
  289. exit:
  290. return_ACPI_STATUS(status);
  291. }
  292. /******************************************************************************
  293. *
  294. * FUNCTION: acpi_hw_read_multiple
  295. *
  296. * PARAMETERS: Value - Where the register value is returned
  297. * register_a - First ACPI register (required)
  298. * register_b - Second ACPI register (optional)
  299. *
  300. * RETURN: Status
  301. *
  302. * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
  303. *
  304. ******************************************************************************/
  305. static acpi_status
  306. acpi_hw_read_multiple(u32 *value,
  307. struct acpi_generic_address *register_a,
  308. struct acpi_generic_address *register_b)
  309. {
  310. u32 value_a = 0;
  311. u32 value_b = 0;
  312. acpi_status status;
  313. /* The first register is always required */
  314. status = acpi_read(&value_a, register_a);
  315. if (ACPI_FAILURE(status)) {
  316. return (status);
  317. }
  318. /* Second register is optional */
  319. if (register_b->address) {
  320. status = acpi_read(&value_b, register_b);
  321. if (ACPI_FAILURE(status)) {
  322. return (status);
  323. }
  324. }
  325. /*
  326. * OR the two return values together. No shifting or masking is necessary,
  327. * because of how the PM1 registers are defined in the ACPI specification:
  328. *
  329. * "Although the bits can be split between the two register blocks (each
  330. * register block has a unique pointer within the FADT), the bit positions
  331. * are maintained. The register block with unimplemented bits (that is,
  332. * those implemented in the other register block) always returns zeros,
  333. * and writes have no side effects"
  334. */
  335. *value = (value_a | value_b);
  336. return (AE_OK);
  337. }
  338. /******************************************************************************
  339. *
  340. * FUNCTION: acpi_hw_write_multiple
  341. *
  342. * PARAMETERS: Value - The value to write
  343. * register_a - First ACPI register (required)
  344. * register_b - Second ACPI register (optional)
  345. *
  346. * RETURN: Status
  347. *
  348. * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
  349. *
  350. ******************************************************************************/
  351. static acpi_status
  352. acpi_hw_write_multiple(u32 value,
  353. struct acpi_generic_address *register_a,
  354. struct acpi_generic_address *register_b)
  355. {
  356. acpi_status status;
  357. /* The first register is always required */
  358. status = acpi_write(value, register_a);
  359. if (ACPI_FAILURE(status)) {
  360. return (status);
  361. }
  362. /*
  363. * Second register is optional
  364. *
  365. * No bit shifting or clearing is necessary, because of how the PM1
  366. * registers are defined in the ACPI specification:
  367. *
  368. * "Although the bits can be split between the two register blocks (each
  369. * register block has a unique pointer within the FADT), the bit positions
  370. * are maintained. The register block with unimplemented bits (that is,
  371. * those implemented in the other register block) always returns zeros,
  372. * and writes have no side effects"
  373. */
  374. if (register_b->address) {
  375. status = acpi_write(value, register_b);
  376. }
  377. return (status);
  378. }