Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Lan Tianyu | 2fa97fe | 2013-06-05 02:27:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * ACPI support for CMOS RTC Address Space access |
| 4 | * |
| 5 | * Copyright (C) 2013, Intel Corporation |
| 6 | * Authors: Lan Tianyu <tianyu.lan@intel.com> |
Lan Tianyu | 2fa97fe | 2013-06-05 02:27:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/acpi.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
Arnd Bergmann | 463a863 | 2016-05-30 20:57:51 +0200 | [diff] [blame] | 14 | #include <linux/mc146818rtc.h> |
Lan Tianyu | 2fa97fe | 2013-06-05 02:27:50 +0000 | [diff] [blame] | 15 | |
| 16 | #include "internal.h" |
| 17 | |
Lan Tianyu | 2fa97fe | 2013-06-05 02:27:50 +0000 | [diff] [blame] | 18 | ACPI_MODULE_NAME("cmos rtc"); |
| 19 | |
| 20 | static const struct acpi_device_id acpi_cmos_rtc_ids[] = { |
| 21 | { "PNP0B00" }, |
| 22 | { "PNP0B01" }, |
| 23 | { "PNP0B02" }, |
| 24 | {} |
| 25 | }; |
| 26 | |
| 27 | static acpi_status |
| 28 | acpi_cmos_rtc_space_handler(u32 function, acpi_physical_address address, |
| 29 | u32 bits, u64 *value64, |
| 30 | void *handler_context, void *region_context) |
| 31 | { |
| 32 | int i; |
Lee, Chun-Yi | 9389f46 | 2014-09-04 15:13:39 +0800 | [diff] [blame] | 33 | u8 *value = (u8 *)value64; |
Lan Tianyu | 2fa97fe | 2013-06-05 02:27:50 +0000 | [diff] [blame] | 34 | |
| 35 | if (address > 0xff || !value64) |
| 36 | return AE_BAD_PARAMETER; |
| 37 | |
| 38 | if (function != ACPI_WRITE && function != ACPI_READ) |
| 39 | return AE_BAD_PARAMETER; |
| 40 | |
| 41 | spin_lock_irq(&rtc_lock); |
| 42 | |
| 43 | for (i = 0; i < DIV_ROUND_UP(bits, 8); ++i, ++address, ++value) |
| 44 | if (function == ACPI_READ) |
| 45 | *value = CMOS_READ(address); |
| 46 | else |
| 47 | CMOS_WRITE(*value, address); |
| 48 | |
| 49 | spin_unlock_irq(&rtc_lock); |
| 50 | |
| 51 | return AE_OK; |
| 52 | } |
| 53 | |
| 54 | static int acpi_install_cmos_rtc_space_handler(struct acpi_device *adev, |
| 55 | const struct acpi_device_id *id) |
| 56 | { |
| 57 | acpi_status status; |
| 58 | |
| 59 | status = acpi_install_address_space_handler(adev->handle, |
| 60 | ACPI_ADR_SPACE_CMOS, |
| 61 | &acpi_cmos_rtc_space_handler, |
| 62 | NULL, NULL); |
| 63 | if (ACPI_FAILURE(status)) { |
| 64 | pr_err(PREFIX "Error installing CMOS-RTC region handler\n"); |
| 65 | return -ENODEV; |
| 66 | } |
| 67 | |
Zhang Rui | eec15ed | 2014-05-30 04:23:01 +0200 | [diff] [blame] | 68 | return 1; |
Lan Tianyu | 2fa97fe | 2013-06-05 02:27:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void acpi_remove_cmos_rtc_space_handler(struct acpi_device *adev) |
| 72 | { |
| 73 | if (ACPI_FAILURE(acpi_remove_address_space_handler(adev->handle, |
| 74 | ACPI_ADR_SPACE_CMOS, &acpi_cmos_rtc_space_handler))) |
| 75 | pr_err(PREFIX "Error removing CMOS-RTC region handler\n"); |
| 76 | } |
| 77 | |
| 78 | static struct acpi_scan_handler cmos_rtc_handler = { |
| 79 | .ids = acpi_cmos_rtc_ids, |
| 80 | .attach = acpi_install_cmos_rtc_space_handler, |
| 81 | .detach = acpi_remove_cmos_rtc_space_handler, |
| 82 | }; |
| 83 | |
| 84 | void __init acpi_cmos_rtc_init(void) |
| 85 | { |
| 86 | acpi_scan_add_handler(&cmos_rtc_handler); |
| 87 | } |