1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Interrupt Timer Subsystem
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>
16 #include <log.h>
17 #include <malloc.h>
18 #include <p2sb.h>
19 #include <spl.h>
20 #include <asm/global_data.h>
21 #include <asm/itss.h>
22
set_polarity(struct udevice * dev,uint irq,bool active_low)23 static int set_polarity(struct udevice *dev, uint irq, bool active_low)
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
snapshot_polarities(struct udevice * dev)40 static int snapshot_polarities(struct udevice *dev)
41 {
42 struct itss_priv *priv = dev_get_priv(dev);
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;
50 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
51
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;
55
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]);
59 }
60
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
67 return 0;
68 }
69
show_polarities(struct udevice * dev,const char * msg)70 static void show_polarities(struct udevice *dev, const char *msg)
71 {
72 int i;
73
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;
77
78 log_debug("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg));
79 }
80 }
81
restore_polarities(struct udevice * dev)82 static int restore_polarities(struct udevice *dev)
83 {
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;
88 int reg_start;
89 int reg_end;
90 int i;
91
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
99 show_polarities(dev, "Before");
100 log_debug("priv->irq_snapshot %p\n", priv->irq_snapshot);
101
102 reg_start = start / IRQS_PER_IPC;
103 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
104
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;
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]);
132 }
133
134 show_polarities(dev, "After");
135
136 return 0;
137 }
138 #endif
139
route_pmc_gpio_gpe(struct udevice * dev,uint pmc_gpe_num)140 static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
141 {
142 struct itss_priv *priv = dev_get_priv(dev);
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
itss_bind(struct udevice * dev)154 static int itss_bind(struct udevice *dev)
155 {
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;
159
160 return 0;
161 }
162
itss_of_to_plat(struct udevice * dev)163 static int itss_of_to_plat(struct udevice *dev)
164 {
165 struct itss_priv *priv = dev_get_priv(dev);
166 int ret;
167
168 #if CONFIG_IS_ENABLED(OF_PLATDATA)
169 struct itss_plat *plat = dev_get_plat(dev);
170 struct dtd_intel_itss *dtplat = &plat->dtplat;
171
172 /*
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.
178 *
179 * TODO(sjg@chromium.org): Add a parent pointer to child devices in dtoc
180 */
181 ret = p2sb_set_port_id(dev, dtplat->intel_p2sb_port_id);
182 if (ret)
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);
187 #else
188 int size;
189
190 size = dev_read_size(dev, "intel,pmc-routes");
191 if (size < 0)
192 return size;
193 priv->route = malloc(size);
194 if (!priv->route)
195 return -ENOMEM;
196 ret = dev_read_u32_array(dev, "intel,pmc-routes", (u32 *)priv->route,
197 size / sizeof(fdt32_t));
198 if (ret)
199 return log_msg_ret("Cannot read pmc-routes", ret);
200 priv->route_count = size / sizeof(struct pmc_route);
201 #endif
202
203 return 0;
204 }
205
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,
212 #endif
213 };
214
215 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
216 static const struct udevice_id itss_ids[] = {
217 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
218 { }
219 };
220 #endif
221
222 U_BOOT_DRIVER(intel_itss) = {
223 .name = "intel_itss",
224 .id = UCLASS_IRQ,
225 .of_match = of_match_ptr(itss_ids),
226 .ops = &itss_ops,
227 .bind = itss_bind,
228 .of_to_plat = itss_of_to_plat,
229 .plat_auto = sizeof(struct itss_plat),
230 .priv_auto = sizeof(struct itss_priv),
231 };
232