1 // SPDX-License-Identifier: GPL-2.0
3 * Interrupt Timer Subsystem
5 * Copyright (C) 2017 Intel Corporation.
6 * Copyright (C) 2017 Siemens AG
7 * Copyright 2019 Google LLC
9 * Taken from coreboot itss.c
14 #include <dt-structs.h>
20 #include <asm/global_data.h>
23 static int set_polarity(struct udevice *dev, uint irq, bool active_low)
28 if (irq > ITSS_MAX_IRQ)
31 reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * (irq / IRQS_PER_IPC);
32 mask = 1 << (irq % IRQS_PER_IPC);
34 pcr_clrsetbits32(dev, reg, mask, active_low ? mask : 0);
39 #ifndef CONFIG_TPL_BUILD
40 static int snapshot_polarities(struct udevice *dev)
42 struct itss_priv *priv = dev_get_priv(dev);
43 const int start = GPIO_IRQ_START;
44 const int end = GPIO_IRQ_END;
49 reg_start = start / IRQS_PER_IPC;
50 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
52 log_debug("ITSS IRQ Polarities snapshot %p\n", priv->irq_snapshot);
53 for (i = reg_start; i < reg_end; i++) {
54 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
56 priv->irq_snapshot[i] = pcr_read32(dev, reg);
57 log_debug(" - %d, reg %x: irq_snapshot[i] %x\n", i, reg,
58 priv->irq_snapshot[i]);
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));
70 static void show_polarities(struct udevice *dev, const char *msg)
74 log_debug("ITSS IRQ Polarities %s:\n", msg);
75 for (i = 0; i < NUM_IPC_REGS; i++) {
76 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
78 log_debug("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg));
82 static int restore_polarities(struct udevice *dev)
84 struct itss_priv *priv = dev_get_priv(dev);
85 struct itss_priv *old_priv;
86 const int start = GPIO_IRQ_START;
87 const int end = GPIO_IRQ_END;
92 /* Get the snapshot which was stored by the pre-reloc device */
93 old_priv = gd->arch.itss_priv;
95 return log_msg_ret("priv", -EFAULT);
96 memcpy(priv->irq_snapshot, old_priv->irq_snapshot,
97 sizeof(priv->irq_snapshot));
99 show_polarities(dev, "Before");
100 log_debug("priv->irq_snapshot %p\n", priv->irq_snapshot);
102 reg_start = start / IRQS_PER_IPC;
103 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
106 for (i = reg_start; i < reg_end; i++) {
112 irq_start = i * IRQS_PER_IPC;
113 irq_end = min(irq_start + IRQS_PER_IPC - 1, ITSS_MAX_IRQ);
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;
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);
128 reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
129 log_debug(" - %d, reg %x: mask %x, irq_snapshot[i] %x\n",
130 i, reg, mask, priv->irq_snapshot[i]);
131 pcr_clrsetbits32(dev, reg, mask, mask & priv->irq_snapshot[i]);
134 show_polarities(dev, "After");
140 static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
142 struct itss_priv *priv = dev_get_priv(dev);
143 struct pmc_route *route;
146 for (i = 0, route = priv->route; i < priv->route_count; i++, route++) {
147 if (pmc_gpe_num == route->pmc)
154 static int itss_bind(struct udevice *dev)
156 /* This is not set with of-platdata, so set it manually */
157 if (CONFIG_IS_ENABLED(OF_PLATDATA))
158 dev->driver_data = X86_IRQT_ITSS;
163 static int itss_of_to_plat(struct udevice *dev)
165 struct itss_priv *priv = dev_get_priv(dev);
168 #if CONFIG_IS_ENABLED(OF_PLATDATA)
169 struct itss_plat *plat = dev_get_plat(dev);
170 struct dtd_intel_itss *dtplat = &plat->dtplat;
173 * It would be nice to do this in the bind() method, but with
174 * of-platdata binding happens in the order that DM finds things in the
175 * linker list (i.e. alphabetical order by driver name). So the GPIO
176 * device may well be bound before its parent (p2sb), and this call
177 * will fail if p2sb is not bound yet.
181 ret = p2sb_set_port_id(dev, dtplat->intel_p2sb_port_id);
183 return log_msg_ret("Could not set port id", ret);
184 priv->route = (struct pmc_route *)dtplat->intel_pmc_routes;
185 priv->route_count = ARRAY_SIZE(dtplat->intel_pmc_routes) /
186 sizeof(struct pmc_route);
190 size = dev_read_size(dev, "intel,pmc-routes");
193 priv->route = malloc(size);
196 ret = dev_read_u32_array(dev, "intel,pmc-routes", (u32 *)priv->route,
197 size / sizeof(fdt32_t));
199 return log_msg_ret("Cannot read pmc-routes", ret);
200 priv->route_count = size / sizeof(struct pmc_route);
206 static const struct irq_ops itss_ops = {
207 .route_pmc_gpio_gpe = route_pmc_gpio_gpe,
208 .set_polarity = set_polarity,
209 #ifndef CONFIG_TPL_BUILD
210 .snapshot_polarities = snapshot_polarities,
211 .restore_polarities = restore_polarities,
215 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
216 static const struct udevice_id itss_ids[] = {
217 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
222 U_BOOT_DRIVER(intel_itss) = {
223 .name = "intel_itss",
225 .of_match = of_match_ptr(itss_ids),
228 .of_to_plat = itss_of_to_plat,
229 .plat_auto = sizeof(struct itss_plat),
230 .priv_auto = sizeof(struct itss_priv),