blob: 54bc47c8d8a02332f6c82e36e75ca9d7be8af269 [file] [log] [blame]
Simon Glass54028bc2019-12-06 21:41:59 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Sandbox driver for interrupts
4 *
5 * Copyright 2019 Google LLC
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <irq.h>
Simon Glass515dcff2020-02-06 09:55:00 -070011#include <asm/test.h>
12
13/**
14 * struct sandbox_irq_priv - private data for this driver
15 *
16 * @count: Counts the number calls to the read_and_clear() method
17 * @pending: true if an interrupt is pending, else false
18 */
19struct sandbox_irq_priv {
20 int count;
21 bool pending;
22};
Simon Glass54028bc2019-12-06 21:41:59 -070023
24static int sandbox_set_polarity(struct udevice *dev, uint irq, bool active_low)
25{
26 if (irq > 10)
27 return -EINVAL;
28
29 return 0;
30}
31
32static int sandbox_route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
33{
34 if (pmc_gpe_num > 10)
35 return -ENOENT;
36
37 return pmc_gpe_num + 1;
38}
39
40static int sandbox_snapshot_polarities(struct udevice *dev)
41{
42 return 0;
43}
44
45static int sandbox_restore_polarities(struct udevice *dev)
46{
Simon Glass515dcff2020-02-06 09:55:00 -070047 return 0;
48}
49
50static int sandbox_irq_read_and_clear(struct irq *irq)
51{
52 struct sandbox_irq_priv *priv = dev_get_priv(irq->dev);
53
54 if (irq->id != SANDBOX_IRQN_PEND)
55 return -EINVAL;
56 priv->count++;
57 if (priv->pending) {
58 priv->pending = false;
59 return 1;
60 }
61
62 if (!(priv->count % 3))
63 priv->pending = true;
64
65 return 0;
66}
67
68static int sandbox_irq_of_xlate(struct irq *irq,
69 struct ofnode_phandle_args *args)
70{
71 irq->id = args->args[0];
72
Simon Glass54028bc2019-12-06 21:41:59 -070073 return 0;
74}
75
76static const struct irq_ops sandbox_irq_ops = {
77 .route_pmc_gpio_gpe = sandbox_route_pmc_gpio_gpe,
78 .set_polarity = sandbox_set_polarity,
79 .snapshot_polarities = sandbox_snapshot_polarities,
80 .restore_polarities = sandbox_restore_polarities,
Simon Glass515dcff2020-02-06 09:55:00 -070081 .read_and_clear = sandbox_irq_read_and_clear,
82 .of_xlate = sandbox_irq_of_xlate,
Simon Glass54028bc2019-12-06 21:41:59 -070083};
84
85static const struct udevice_id sandbox_irq_ids[] = {
Simon Glassa847b272020-02-06 09:54:57 -070086 { .compatible = "sandbox,irq", SANDBOX_IRQT_BASE },
Simon Glass54028bc2019-12-06 21:41:59 -070087 { }
88};
89
90U_BOOT_DRIVER(sandbox_irq_drv) = {
91 .name = "sandbox_irq",
92 .id = UCLASS_IRQ,
93 .of_match = sandbox_irq_ids,
94 .ops = &sandbox_irq_ops,
Simon Glass515dcff2020-02-06 09:55:00 -070095 .priv_auto_alloc_size = sizeof(struct sandbox_irq_priv),
Simon Glass54028bc2019-12-06 21:41:59 -070096};