Simon Glass | 54028bc | 2019-12-06 21:41:59 -0700 | [diff] [blame] | 1 | // 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 Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 11 | #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 | */ |
| 19 | struct sandbox_irq_priv { |
| 20 | int count; |
| 21 | bool pending; |
| 22 | }; |
Simon Glass | 54028bc | 2019-12-06 21:41:59 -0700 | [diff] [blame] | 23 | |
| 24 | static 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 | |
| 32 | static 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 | |
| 40 | static int sandbox_snapshot_polarities(struct udevice *dev) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int sandbox_restore_polarities(struct udevice *dev) |
| 46 | { |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static 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 | |
| 68 | static int sandbox_irq_of_xlate(struct irq *irq, |
| 69 | struct ofnode_phandle_args *args) |
| 70 | { |
| 71 | irq->id = args->args[0]; |
| 72 | |
Simon Glass | 54028bc | 2019-12-06 21:41:59 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static 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 Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 81 | .read_and_clear = sandbox_irq_read_and_clear, |
| 82 | .of_xlate = sandbox_irq_of_xlate, |
Simon Glass | 54028bc | 2019-12-06 21:41:59 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static const struct udevice_id sandbox_irq_ids[] = { |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 86 | { .compatible = "sandbox,irq", SANDBOX_IRQT_BASE }, |
Simon Glass | 54028bc | 2019-12-06 21:41:59 -0700 | [diff] [blame] | 87 | { } |
| 88 | }; |
| 89 | |
| 90 | U_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 Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 95 | .priv_auto_alloc_size = sizeof(struct sandbox_irq_priv), |
Simon Glass | 54028bc | 2019-12-06 21:41:59 -0700 | [diff] [blame] | 96 | }; |