1 // SPDX-License-Identifier: GPL-2.0-only
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>
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>
14 #include <linux/mc146818rtc.h>
15
16 #include "internal.h"
17
18 static const struct acpi_device_id acpi_cmos_rtc_ids[] = {
19 { "PNP0B00" },
20 { "PNP0B01" },
21 { "PNP0B02" },
22 {}
23 };
24
25 static acpi_status
acpi_cmos_rtc_space_handler(u32 function,acpi_physical_address address,u32 bits,u64 * value64,void * handler_context,void * region_context)26 acpi_cmos_rtc_space_handler(u32 function, acpi_physical_address address,
27 u32 bits, u64 *value64,
28 void *handler_context, void *region_context)
29 {
30 int i;
31 u8 *value = (u8 *)value64;
32
33 if (address > 0xff || !value64)
34 return AE_BAD_PARAMETER;
35
36 if (function != ACPI_WRITE && function != ACPI_READ)
37 return AE_BAD_PARAMETER;
38
39 spin_lock_irq(&rtc_lock);
40
41 for (i = 0; i < DIV_ROUND_UP(bits, 8); ++i, ++address, ++value)
42 if (function == ACPI_READ)
43 *value = CMOS_READ(address);
44 else
45 CMOS_WRITE(*value, address);
46
47 spin_unlock_irq(&rtc_lock);
48
49 return AE_OK;
50 }
51
acpi_install_cmos_rtc_space_handler(struct acpi_device * adev,const struct acpi_device_id * id)52 static int acpi_install_cmos_rtc_space_handler(struct acpi_device *adev,
53 const struct acpi_device_id *id)
54 {
55 acpi_status status;
56
57 status = acpi_install_address_space_handler(adev->handle,
58 ACPI_ADR_SPACE_CMOS,
59 &acpi_cmos_rtc_space_handler,
60 NULL, NULL);
61 if (ACPI_FAILURE(status)) {
62 pr_err(PREFIX "Error installing CMOS-RTC region handler\n");
63 return -ENODEV;
64 }
65
66 return 1;
67 }
68
acpi_remove_cmos_rtc_space_handler(struct acpi_device * adev)69 static void acpi_remove_cmos_rtc_space_handler(struct acpi_device *adev)
70 {
71 if (ACPI_FAILURE(acpi_remove_address_space_handler(adev->handle,
72 ACPI_ADR_SPACE_CMOS, &acpi_cmos_rtc_space_handler)))
73 pr_err(PREFIX "Error removing CMOS-RTC region handler\n");
74 }
75
76 static struct acpi_scan_handler cmos_rtc_handler = {
77 .ids = acpi_cmos_rtc_ids,
78 .attach = acpi_install_cmos_rtc_space_handler,
79 .detach = acpi_remove_cmos_rtc_space_handler,
80 };
81
acpi_cmos_rtc_init(void)82 void __init acpi_cmos_rtc_init(void)
83 {
84 acpi_scan_add_handler(&cmos_rtc_handler);
85 }
86