Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / x86 / cpu / acpi_gpe.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 Google, LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <irq.h>
10 #include <log.h>
11 #include <asm/io.h>
12
13 /**
14  * struct acpi_gpe_priv - private driver information
15  *
16  * @acpi_base: Base I/O address of ACPI registers
17  */
18 struct acpi_gpe_priv {
19         ulong acpi_base;
20 };
21
22 #define GPE0_STS(x)             (0x20 + ((x) * 4))
23
24 static int acpi_gpe_read_and_clear(struct irq *irq)
25 {
26         struct acpi_gpe_priv *priv = dev_get_priv(irq->dev);
27         u32 mask, sts;
28         ulong start;
29         int ret = 0;
30         int bank;
31
32         bank = irq->id / 32;
33         mask = 1 << (irq->id % 32);
34
35         /* Wait up to 1ms for GPE status to clear */
36         start = get_timer(0);
37         do {
38                 if (get_timer(start) > 1)
39                         return ret;
40
41                 sts = inl(priv->acpi_base + GPE0_STS(bank));
42                 if (sts & mask) {
43                         outl(mask, priv->acpi_base + GPE0_STS(bank));
44                         ret = 1;
45                 }
46         } while (sts & mask);
47
48         return ret;
49 }
50
51 static int acpi_gpe_ofdata_to_platdata(struct udevice *dev)
52 {
53         struct acpi_gpe_priv *priv = dev_get_priv(dev);
54
55         priv->acpi_base = dev_read_addr(dev);
56         if (!priv->acpi_base || priv->acpi_base == FDT_ADDR_T_NONE)
57                 return log_msg_ret("acpi_base", -EINVAL);
58
59         return 0;
60 }
61
62 static int acpi_gpe_of_xlate(struct irq *irq, struct ofnode_phandle_args *args)
63 {
64         irq->id = args->args[0];
65
66         return 0;
67 }
68
69 static const struct irq_ops acpi_gpe_ops = {
70         .read_and_clear         = acpi_gpe_read_and_clear,
71         .of_xlate               = acpi_gpe_of_xlate,
72 };
73
74 static const struct udevice_id acpi_gpe_ids[] = {
75         { .compatible = "intel,acpi-gpe", .data = X86_IRQT_ACPI_GPE },
76         { }
77 };
78
79 U_BOOT_DRIVER(acpi_gpe_drv) = {
80         .name           = "acpi_gpe",
81         .id             = UCLASS_IRQ,
82         .of_match       = acpi_gpe_ids,
83         .ops            = &acpi_gpe_ops,
84         .ofdata_to_platdata     = acpi_gpe_ofdata_to_platdata,
85         .priv_auto_alloc_size = sizeof(struct acpi_gpe_priv),
86 };