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 basic of-platdata, so set it manually */
157 	if (CONFIG_IS_ENABLED(OF_PLATDATA) &&
158 	    !CONFIG_IS_ENABLED(OF_PLATDATA_INST))
159 		dev->driver_data = X86_IRQT_ITSS;
160 
161 	return 0;
162 }
163 
itss_of_to_plat(struct udevice * dev)164 static int itss_of_to_plat(struct udevice *dev)
165 {
166 	struct itss_priv *priv = dev_get_priv(dev);
167 	int ret;
168 
169 #if CONFIG_IS_ENABLED(OF_PLATDATA)
170 	struct itss_plat *plat = dev_get_plat(dev);
171 	struct dtd_intel_itss *dtplat = &plat->dtplat;
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 
207 static const struct irq_ops itss_ops = {
208 	.route_pmc_gpio_gpe	= route_pmc_gpio_gpe,
209 	.set_polarity	= set_polarity,
210 #ifndef CONFIG_TPL_BUILD
211 	.snapshot_polarities = snapshot_polarities,
212 	.restore_polarities = restore_polarities,
213 #endif
214 };
215 
216 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
217 static const struct udevice_id itss_ids[] = {
218 	{ .compatible = "intel,itss", .data = X86_IRQT_ITSS },
219 	{ }
220 };
221 #endif
222 
223 U_BOOT_DRIVER(intel_itss) = {
224 	.name		= "intel_itss",
225 	.id		= UCLASS_IRQ,
226 	.of_match	= of_match_ptr(itss_ids),
227 	.ops		= &itss_ops,
228 	.bind		= itss_bind,
229 	.of_to_plat = itss_of_to_plat,
230 	.plat_auto	= sizeof(struct itss_plat),
231 	.priv_auto	= sizeof(struct itss_priv),
232 };
233