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