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 <dm.h>
13 #include <dt-structs.h>
14 #include <irq.h>
15 #include <log.h>
16 #include <malloc.h>
17 #include <p2sb.h>
18 #include <spl.h>
19 #include <asm/global_data.h>
20 #include <asm/itss.h>
21
set_polarity(struct udevice * dev,uint irq,bool active_low)22 static int set_polarity(struct udevice *dev, uint irq, bool active_low)
23 {
24 u32 mask;
25 uint reg;
26
27 if (irq > ITSS_MAX_IRQ)
28 return -EINVAL;
29
30 reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * (irq / IRQS_PER_IPC);
31 mask = 1 << (irq % IRQS_PER_IPC);
32
33 pcr_clrsetbits32(dev, reg, mask, active_low ? mask : 0);
34
35 return 0;
36 }
37
38 #ifndef CONFIG_TPL_BUILD
snapshot_polarities(struct udevice * dev)39 static int snapshot_polarities(struct udevice *dev)
40 {
41 struct itss_priv *priv = dev_get_priv(dev);
42 const int start = GPIO_IRQ_START;
43 const int end = GPIO_IRQ_END;
44 int reg_start;
45 int reg_end;
46 int i;
47
48 reg_start = start / IRQS_PER_IPC;
49 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
50
51 log_debug("ITSS IRQ Polarities snapshot %p\n", priv->irq_snapshot);
52 for (i = reg_start; i < reg_end; i++) {
53 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
54
55 priv->irq_snapshot[i] = pcr_read32(dev, reg);
56 log_debug(" - %d, reg %x: irq_snapshot[i] %x\n", i, reg,
57 priv->irq_snapshot[i]);
58 }
59
60 /* Save the snapshot for use after relocation */
61 gd->start_addr_sp -= sizeof(*priv);
62 gd->start_addr_sp &= ~0xf;
63 gd->arch.itss_priv = (void *)gd->start_addr_sp;
64 memcpy(gd->arch.itss_priv, priv, sizeof(*priv));
65
66 return 0;
67 }
68
show_polarities(struct udevice * dev,const char * msg)69 static void show_polarities(struct udevice *dev, const char *msg)
70 {
71 int i;
72
73 log_debug("ITSS IRQ Polarities %s:\n", msg);
74 for (i = 0; i < NUM_IPC_REGS; i++) {
75 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
76
77 log_debug("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg));
78 }
79 }
80
restore_polarities(struct udevice * dev)81 static int restore_polarities(struct udevice *dev)
82 {
83 struct itss_priv *priv = dev_get_priv(dev);
84 struct itss_priv *old_priv;
85 const int start = GPIO_IRQ_START;
86 const int end = GPIO_IRQ_END;
87 int reg_start;
88 int reg_end;
89 int i;
90
91 /* Get the snapshot which was stored by the pre-reloc device */
92 old_priv = gd->arch.itss_priv;
93 if (!old_priv)
94 return log_msg_ret("priv", -EFAULT);
95 memcpy(priv->irq_snapshot, old_priv->irq_snapshot,
96 sizeof(priv->irq_snapshot));
97
98 show_polarities(dev, "Before");
99 log_debug("priv->irq_snapshot %p\n", priv->irq_snapshot);
100
101 reg_start = start / IRQS_PER_IPC;
102 reg_end = DIV_ROUND_UP(end, 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 log_debug(" - %d, reg %x: mask %x, irq_snapshot[i] %x\n",
128 i, reg, mask, priv->irq_snapshot[i]);
129 pcr_clrsetbits32(dev, reg, mask, mask & priv->irq_snapshot[i]);
130 }
131
132 show_polarities(dev, "After");
133
134 return 0;
135 }
136 #endif
137
route_pmc_gpio_gpe(struct udevice * dev,uint pmc_gpe_num)138 static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
139 {
140 struct itss_priv *priv = dev_get_priv(dev);
141 struct pmc_route *route;
142 int i;
143
144 for (i = 0, route = priv->route; i < priv->route_count; i++, route++) {
145 if (pmc_gpe_num == route->pmc)
146 return route->gpio;
147 }
148
149 return -ENOENT;
150 }
151
itss_bind(struct udevice * dev)152 static int itss_bind(struct udevice *dev)
153 {
154 /* This is not set with basic of-platdata, so set it manually */
155 if (CONFIG_IS_ENABLED(OF_PLATDATA) &&
156 !CONFIG_IS_ENABLED(OF_PLATDATA_INST))
157 dev->driver_data = X86_IRQT_ITSS;
158
159 return 0;
160 }
161
itss_of_to_plat(struct udevice * dev)162 static int itss_of_to_plat(struct udevice *dev)
163 {
164 struct itss_priv *priv = dev_get_priv(dev);
165 int ret;
166
167 #if CONFIG_IS_ENABLED(OF_PLATDATA)
168 struct itss_plat *plat = dev_get_plat(dev);
169 struct dtd_intel_itss *dtplat = &plat->dtplat;
170
171 /*
172 * It would be nice to do this in the bind() method, but with
173 * of-platdata binding happens in the order that DM finds things in the
174 * linker list (i.e. alphabetical order by driver name). So the GPIO
175 * device may well be bound before its parent (p2sb), and this call
176 * will fail if p2sb is not bound yet.
177 *
178 * TODO(sjg@chromium.org): Add a parent pointer to child devices in dtoc
179 */
180 ret = p2sb_set_port_id(dev, dtplat->intel_p2sb_port_id);
181 if (ret)
182 return log_msg_ret("Could not set port id", ret);
183 priv->route = (struct pmc_route *)dtplat->intel_pmc_routes;
184 priv->route_count = ARRAY_SIZE(dtplat->intel_pmc_routes) /
185 sizeof(struct pmc_route);
186 #else
187 int size;
188
189 size = dev_read_size(dev, "intel,pmc-routes");
190 if (size < 0)
191 return size;
192 priv->route = malloc(size);
193 if (!priv->route)
194 return -ENOMEM;
195 ret = dev_read_u32_array(dev, "intel,pmc-routes", (u32 *)priv->route,
196 size / sizeof(fdt32_t));
197 if (ret)
198 return log_msg_ret("Cannot read pmc-routes", ret);
199 priv->route_count = size / sizeof(struct pmc_route);
200 #endif
201
202 return 0;
203 }
204
205 static const struct irq_ops itss_ops = {
206 .route_pmc_gpio_gpe = route_pmc_gpio_gpe,
207 .set_polarity = set_polarity,
208 #ifndef CONFIG_TPL_BUILD
209 .snapshot_polarities = snapshot_polarities,
210 .restore_polarities = restore_polarities,
211 #endif
212 };
213
214 #if CONFIG_IS_ENABLED(OF_REAL)
215 static const struct udevice_id itss_ids[] = {
216 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
217 { }
218 };
219 #endif
220
221 U_BOOT_DRIVER(intel_itss) = {
222 .name = "intel_itss",
223 .id = UCLASS_IRQ,
224 .of_match = of_match_ptr(itss_ids),
225 .ops = &itss_ops,
226 .bind = itss_bind,
227 .of_to_plat = itss_of_to_plat,
228 .plat_auto = sizeof(struct itss_plat),
229 .priv_auto = sizeof(struct itss_priv),
230 };
231