blob: 168857feb762220fa11428c56961c975fe77e3e9 [file] [log] [blame]
Simon Glass8c0629b2019-12-08 17:40:08 -07001// SPDX-License-Identifier: GPL-2.0
2/*
Wolfgang Wallner12ea8582020-01-22 16:01:44 +01003 * Interrupt Timer Subsystem
Simon Glass8c0629b2019-12-08 17:40:08 -07004 *
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
Simon Glass8c0629b2019-12-08 17:40:08 -070012#include <dm.h>
13#include <dt-structs.h>
14#include <irq.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Simon Glass8c0629b2019-12-08 17:40:08 -070017#include <p2sb.h>
18#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Wolfgang Wallner97132162020-01-22 16:01:45 +010020#include <asm/itss.h>
Simon Glass8c0629b2019-12-08 17:40:08 -070021
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010022static int set_polarity(struct udevice *dev, uint irq, bool active_low)
Simon Glass8c0629b2019-12-08 17:40:08 -070023{
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
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010039static int snapshot_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070040{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010041 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -070042 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;
Simon Glassd89f1932020-07-16 21:22:30 -060049 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
Simon Glass8c0629b2019-12-08 17:40:08 -070050
Simon Glassdd5fa062020-11-04 09:57:39 -070051 log_debug("ITSS IRQ Polarities snapshot %p\n", priv->irq_snapshot);
Simon Glass8c0629b2019-12-08 17:40:08 -070052 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);
Simon Glassd89f1932020-07-16 21:22:30 -060056 log_debug(" - %d, reg %x: irq_snapshot[i] %x\n", i, reg,
57 priv->irq_snapshot[i]);
Simon Glass8c0629b2019-12-08 17:40:08 -070058 }
59
Simon Glassd89f1932020-07-16 21:22:30 -060060 /* 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
Simon Glass8c0629b2019-12-08 17:40:08 -070066 return 0;
67}
68
69static void show_polarities(struct udevice *dev, const char *msg)
70{
71 int i;
72
Simon Glassdd5fa062020-11-04 09:57:39 -070073 log_debug("ITSS IRQ Polarities %s:\n", msg);
Simon Glass8c0629b2019-12-08 17:40:08 -070074 for (i = 0; i < NUM_IPC_REGS; i++) {
75 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
76
Simon Glassdd5fa062020-11-04 09:57:39 -070077 log_debug("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg));
Simon Glass8c0629b2019-12-08 17:40:08 -070078 }
79}
80
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010081static int restore_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070082{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010083 struct itss_priv *priv = dev_get_priv(dev);
Simon Glassd89f1932020-07-16 21:22:30 -060084 struct itss_priv *old_priv;
Simon Glass8c0629b2019-12-08 17:40:08 -070085 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
Simon Glassd89f1932020-07-16 21:22:30 -060091 /* 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
Simon Glass8c0629b2019-12-08 17:40:08 -070098 show_polarities(dev, "Before");
Simon Glassdd5fa062020-11-04 09:57:39 -070099 log_debug("priv->irq_snapshot %p\n", priv->irq_snapshot);
Simon Glass8c0629b2019-12-08 17:40:08 -0700100
101 reg_start = start / IRQS_PER_IPC;
Simon Glassd89f1932020-07-16 21:22:30 -0600102 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
103
Simon Glass8c0629b2019-12-08 17:40:08 -0700104 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;
Simon Glassd89f1932020-07-16 21:22:30 -0600127 log_debug(" - %d, reg %x: mask %x, irq_snapshot[i] %x\n",
128 i, reg, mask, priv->irq_snapshot[i]);
Simon Glass8c0629b2019-12-08 17:40:08 -0700129 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
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100138static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
Simon Glass8c0629b2019-12-08 17:40:08 -0700139{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100140 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700141 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
Simon Glassc51a0212020-02-06 09:54:59 -0700152static int itss_bind(struct udevice *dev)
153{
Simon Glass88182022021-03-15 17:25:47 +1300154 /* 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))
Simon Glassc51a0212020-02-06 09:54:59 -0700157 dev->driver_data = X86_IRQT_ITSS;
158
159 return 0;
160}
161
Simon Glassaad29ae2020-12-03 16:55:21 -0700162static int itss_of_to_plat(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -0700163{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100164 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700165 int ret;
166
167#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700168 struct itss_plat *plat = dev_get_plat(dev);
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100169 struct dtd_intel_itss *dtplat = &plat->dtplat;
Simon Glass8c0629b2019-12-08 17:40:08 -0700170
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
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100205static const struct irq_ops itss_ops = {
206 .route_pmc_gpio_gpe = route_pmc_gpio_gpe,
207 .set_polarity = set_polarity,
Simon Glass8c0629b2019-12-08 17:40:08 -0700208#ifndef CONFIG_TPL_BUILD
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100209 .snapshot_polarities = snapshot_polarities,
210 .restore_polarities = restore_polarities,
Simon Glass8c0629b2019-12-08 17:40:08 -0700211#endif
212};
213
Simon Glass92882652021-08-07 07:24:04 -0600214#if CONFIG_IS_ENABLED(OF_REAL)
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100215static const struct udevice_id itss_ids[] = {
Simon Glass21bb12a2020-02-06 09:54:58 -0700216 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
Simon Glass8c0629b2019-12-08 17:40:08 -0700217 { }
218};
Simon Glassec8ae8a2020-12-23 08:11:30 -0700219#endif
Simon Glass8c0629b2019-12-08 17:40:08 -0700220
Simon Glassa055da82020-10-05 05:27:01 -0600221U_BOOT_DRIVER(intel_itss) = {
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100222 .name = "intel_itss",
Simon Glass8c0629b2019-12-08 17:40:08 -0700223 .id = UCLASS_IRQ,
Simon Glassec8ae8a2020-12-23 08:11:30 -0700224 .of_match = of_match_ptr(itss_ids),
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100225 .ops = &itss_ops,
Simon Glassc51a0212020-02-06 09:54:59 -0700226 .bind = itss_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700227 .of_to_plat = itss_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700228 .plat_auto = sizeof(struct itss_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700229 .priv_auto = sizeof(struct itss_priv),
Simon Glass8c0629b2019-12-08 17:40:08 -0700230};