Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Wolfgang Wallner | 12ea858 | 2020-01-22 16:01:44 +0100 | [diff] [blame] | 3 | * Interrupt Timer Subsystem |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2017 Intel Corporation. |
| 6 | * Copyright (C) 2017 Siemens AG |
| 7 | * Copyright 2019 Google LLC |
| 8 | * |
| 9 | * Taken from coreboot itss.c |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <dm.h> |
| 14 | #include <dt-structs.h> |
| 15 | #include <irq.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 18 | #include <p2sb.h> |
| 19 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 20 | #include <asm/global_data.h> |
Wolfgang Wallner | 9713216 | 2020-01-22 16:01:45 +0100 | [diff] [blame] | 21 | #include <asm/itss.h> |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 22 | |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 23 | static int set_polarity(struct udevice *dev, uint irq, bool active_low) |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 24 | { |
| 25 | u32 mask; |
| 26 | uint reg; |
| 27 | |
| 28 | if (irq > ITSS_MAX_IRQ) |
| 29 | return -EINVAL; |
| 30 | |
| 31 | reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * (irq / IRQS_PER_IPC); |
| 32 | mask = 1 << (irq % IRQS_PER_IPC); |
| 33 | |
| 34 | pcr_clrsetbits32(dev, reg, mask, active_low ? mask : 0); |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | #ifndef CONFIG_TPL_BUILD |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 40 | static int snapshot_polarities(struct udevice *dev) |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 41 | { |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 42 | struct itss_priv *priv = dev_get_priv(dev); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 43 | const int start = GPIO_IRQ_START; |
| 44 | const int end = GPIO_IRQ_END; |
| 45 | int reg_start; |
| 46 | int reg_end; |
| 47 | int i; |
| 48 | |
| 49 | reg_start = start / IRQS_PER_IPC; |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 50 | reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 51 | |
Simon Glass | dd5fa06 | 2020-11-04 09:57:39 -0700 | [diff] [blame] | 52 | log_debug("ITSS IRQ Polarities snapshot %p\n", priv->irq_snapshot); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 53 | for (i = reg_start; i < reg_end; i++) { |
| 54 | uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i; |
| 55 | |
| 56 | priv->irq_snapshot[i] = pcr_read32(dev, reg); |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 57 | log_debug(" - %d, reg %x: irq_snapshot[i] %x\n", i, reg, |
| 58 | priv->irq_snapshot[i]); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 61 | /* Save the snapshot for use after relocation */ |
| 62 | gd->start_addr_sp -= sizeof(*priv); |
| 63 | gd->start_addr_sp &= ~0xf; |
| 64 | gd->arch.itss_priv = (void *)gd->start_addr_sp; |
| 65 | memcpy(gd->arch.itss_priv, priv, sizeof(*priv)); |
| 66 | |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static void show_polarities(struct udevice *dev, const char *msg) |
| 71 | { |
| 72 | int i; |
| 73 | |
Simon Glass | dd5fa06 | 2020-11-04 09:57:39 -0700 | [diff] [blame] | 74 | log_debug("ITSS IRQ Polarities %s:\n", msg); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 75 | for (i = 0; i < NUM_IPC_REGS; i++) { |
| 76 | uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i; |
| 77 | |
Simon Glass | dd5fa06 | 2020-11-04 09:57:39 -0700 | [diff] [blame] | 78 | log_debug("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg)); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 82 | static int restore_polarities(struct udevice *dev) |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 83 | { |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 84 | struct itss_priv *priv = dev_get_priv(dev); |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 85 | struct itss_priv *old_priv; |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 86 | const int start = GPIO_IRQ_START; |
| 87 | const int end = GPIO_IRQ_END; |
| 88 | int reg_start; |
| 89 | int reg_end; |
| 90 | int i; |
| 91 | |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 92 | /* Get the snapshot which was stored by the pre-reloc device */ |
| 93 | old_priv = gd->arch.itss_priv; |
| 94 | if (!old_priv) |
| 95 | return log_msg_ret("priv", -EFAULT); |
| 96 | memcpy(priv->irq_snapshot, old_priv->irq_snapshot, |
| 97 | sizeof(priv->irq_snapshot)); |
| 98 | |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 99 | show_polarities(dev, "Before"); |
Simon Glass | dd5fa06 | 2020-11-04 09:57:39 -0700 | [diff] [blame] | 100 | log_debug("priv->irq_snapshot %p\n", priv->irq_snapshot); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 101 | |
| 102 | reg_start = start / IRQS_PER_IPC; |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 103 | reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC); |
| 104 | |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 105 | |
| 106 | for (i = reg_start; i < reg_end; i++) { |
| 107 | u32 mask; |
| 108 | u16 reg; |
| 109 | int irq_start; |
| 110 | int irq_end; |
| 111 | |
| 112 | irq_start = i * IRQS_PER_IPC; |
| 113 | irq_end = min(irq_start + IRQS_PER_IPC - 1, ITSS_MAX_IRQ); |
| 114 | |
| 115 | if (start > irq_end) |
| 116 | continue; |
| 117 | if (end < irq_start) |
| 118 | break; |
| 119 | |
| 120 | /* Track bits within the bounds of of the register */ |
| 121 | irq_start = max(start, irq_start) % IRQS_PER_IPC; |
| 122 | irq_end = min(end, irq_end) % IRQS_PER_IPC; |
| 123 | |
| 124 | /* Create bitmask of the inclusive range of start and end */ |
| 125 | mask = (((1U << irq_end) - 1) | (1U << irq_end)); |
| 126 | mask &= ~((1U << irq_start) - 1); |
| 127 | |
| 128 | reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i; |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 129 | log_debug(" - %d, reg %x: mask %x, irq_snapshot[i] %x\n", |
| 130 | i, reg, mask, priv->irq_snapshot[i]); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 131 | pcr_clrsetbits32(dev, reg, mask, mask & priv->irq_snapshot[i]); |
| 132 | } |
| 133 | |
| 134 | show_polarities(dev, "After"); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | #endif |
| 139 | |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 140 | static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num) |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 141 | { |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 142 | struct itss_priv *priv = dev_get_priv(dev); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 143 | struct pmc_route *route; |
| 144 | int i; |
| 145 | |
| 146 | for (i = 0, route = priv->route; i < priv->route_count; i++, route++) { |
| 147 | if (pmc_gpe_num == route->pmc) |
| 148 | return route->gpio; |
| 149 | } |
| 150 | |
| 151 | return -ENOENT; |
| 152 | } |
| 153 | |
Simon Glass | c51a021 | 2020-02-06 09:54:59 -0700 | [diff] [blame] | 154 | static int itss_bind(struct udevice *dev) |
| 155 | { |
Simon Glass | 8818202 | 2021-03-15 17:25:47 +1300 | [diff] [blame] | 156 | /* This is not set with basic of-platdata, so set it manually */ |
| 157 | if (CONFIG_IS_ENABLED(OF_PLATDATA) && |
| 158 | !CONFIG_IS_ENABLED(OF_PLATDATA_INST)) |
Simon Glass | c51a021 | 2020-02-06 09:54:59 -0700 | [diff] [blame] | 159 | dev->driver_data = X86_IRQT_ITSS; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 164 | static int itss_of_to_plat(struct udevice *dev) |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 165 | { |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 166 | struct itss_priv *priv = dev_get_priv(dev); |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 167 | int ret; |
| 168 | |
| 169 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 170 | struct itss_plat *plat = dev_get_plat(dev); |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 171 | struct dtd_intel_itss *dtplat = &plat->dtplat; |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * It would be nice to do this in the bind() method, but with |
| 175 | * of-platdata binding happens in the order that DM finds things in the |
| 176 | * linker list (i.e. alphabetical order by driver name). So the GPIO |
| 177 | * device may well be bound before its parent (p2sb), and this call |
| 178 | * will fail if p2sb is not bound yet. |
| 179 | * |
| 180 | * TODO(sjg@chromium.org): Add a parent pointer to child devices in dtoc |
| 181 | */ |
| 182 | ret = p2sb_set_port_id(dev, dtplat->intel_p2sb_port_id); |
| 183 | if (ret) |
| 184 | return log_msg_ret("Could not set port id", ret); |
| 185 | priv->route = (struct pmc_route *)dtplat->intel_pmc_routes; |
| 186 | priv->route_count = ARRAY_SIZE(dtplat->intel_pmc_routes) / |
| 187 | sizeof(struct pmc_route); |
| 188 | #else |
| 189 | int size; |
| 190 | |
| 191 | size = dev_read_size(dev, "intel,pmc-routes"); |
| 192 | if (size < 0) |
| 193 | return size; |
| 194 | priv->route = malloc(size); |
| 195 | if (!priv->route) |
| 196 | return -ENOMEM; |
| 197 | ret = dev_read_u32_array(dev, "intel,pmc-routes", (u32 *)priv->route, |
| 198 | size / sizeof(fdt32_t)); |
| 199 | if (ret) |
| 200 | return log_msg_ret("Cannot read pmc-routes", ret); |
| 201 | priv->route_count = size / sizeof(struct pmc_route); |
| 202 | #endif |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 207 | static const struct irq_ops itss_ops = { |
| 208 | .route_pmc_gpio_gpe = route_pmc_gpio_gpe, |
| 209 | .set_polarity = set_polarity, |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 210 | #ifndef CONFIG_TPL_BUILD |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 211 | .snapshot_polarities = snapshot_polarities, |
| 212 | .restore_polarities = restore_polarities, |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 213 | #endif |
| 214 | }; |
| 215 | |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 216 | #if CONFIG_IS_ENABLED(OF_REAL) |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 217 | static const struct udevice_id itss_ids[] = { |
Simon Glass | 21bb12a | 2020-02-06 09:54:58 -0700 | [diff] [blame] | 218 | { .compatible = "intel,itss", .data = X86_IRQT_ITSS }, |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 219 | { } |
| 220 | }; |
Simon Glass | ec8ae8a | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 221 | #endif |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 222 | |
Simon Glass | a055da8 | 2020-10-05 05:27:01 -0600 | [diff] [blame] | 223 | U_BOOT_DRIVER(intel_itss) = { |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 224 | .name = "intel_itss", |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 225 | .id = UCLASS_IRQ, |
Simon Glass | ec8ae8a | 2020-12-23 08:11:30 -0700 | [diff] [blame] | 226 | .of_match = of_match_ptr(itss_ids), |
Wolfgang Wallner | 3767e7f | 2020-01-22 16:01:47 +0100 | [diff] [blame] | 227 | .ops = &itss_ops, |
Simon Glass | c51a021 | 2020-02-06 09:54:59 -0700 | [diff] [blame] | 228 | .bind = itss_bind, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 229 | .of_to_plat = itss_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 230 | .plat_auto = sizeof(struct itss_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 231 | .priv_auto = sizeof(struct itss_priv), |
Simon Glass | 8c0629b | 2019-12-08 17:40:08 -0700 | [diff] [blame] | 232 | }; |