blob: ec73b3d89312adc5d1d4b08e28b1dc0d63841dc4 [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
12#include <common.h>
13#include <dm.h>
14#include <dt-structs.h>
15#include <irq.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glass8c0629b2019-12-08 17:40:08 -070018#include <p2sb.h>
19#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Wolfgang Wallner97132162020-01-22 16:01:45 +010021#include <asm/itss.h>
Simon Glass8c0629b2019-12-08 17:40:08 -070022
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010023static int set_polarity(struct udevice *dev, uint irq, bool active_low)
Simon Glass8c0629b2019-12-08 17:40:08 -070024{
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
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010040static int snapshot_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070041{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010042 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -070043 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;
Simon Glassd89f1932020-07-16 21:22:30 -060050 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
Simon Glass8c0629b2019-12-08 17:40:08 -070051
Simon Glassdd5fa062020-11-04 09:57:39 -070052 log_debug("ITSS IRQ Polarities snapshot %p\n", priv->irq_snapshot);
Simon Glass8c0629b2019-12-08 17:40:08 -070053 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);
Simon Glassd89f1932020-07-16 21:22:30 -060057 log_debug(" - %d, reg %x: irq_snapshot[i] %x\n", i, reg,
58 priv->irq_snapshot[i]);
Simon Glass8c0629b2019-12-08 17:40:08 -070059 }
60
Simon Glassd89f1932020-07-16 21:22:30 -060061 /* 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
Simon Glass8c0629b2019-12-08 17:40:08 -070067 return 0;
68}
69
70static void show_polarities(struct udevice *dev, const char *msg)
71{
72 int i;
73
Simon Glassdd5fa062020-11-04 09:57:39 -070074 log_debug("ITSS IRQ Polarities %s:\n", msg);
Simon Glass8c0629b2019-12-08 17:40:08 -070075 for (i = 0; i < NUM_IPC_REGS; i++) {
76 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
77
Simon Glassdd5fa062020-11-04 09:57:39 -070078 log_debug("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg));
Simon Glass8c0629b2019-12-08 17:40:08 -070079 }
80}
81
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010082static int restore_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070083{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010084 struct itss_priv *priv = dev_get_priv(dev);
Simon Glassd89f1932020-07-16 21:22:30 -060085 struct itss_priv *old_priv;
Simon Glass8c0629b2019-12-08 17:40:08 -070086 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
Simon Glassd89f1932020-07-16 21:22:30 -060092 /* 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
Simon Glass8c0629b2019-12-08 17:40:08 -070099 show_polarities(dev, "Before");
Simon Glassdd5fa062020-11-04 09:57:39 -0700100 log_debug("priv->irq_snapshot %p\n", priv->irq_snapshot);
Simon Glass8c0629b2019-12-08 17:40:08 -0700101
102 reg_start = start / IRQS_PER_IPC;
Simon Glassd89f1932020-07-16 21:22:30 -0600103 reg_end = DIV_ROUND_UP(end, IRQS_PER_IPC);
104
Simon Glass8c0629b2019-12-08 17:40:08 -0700105
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;
Simon Glassd89f1932020-07-16 21:22:30 -0600129 log_debug(" - %d, reg %x: mask %x, irq_snapshot[i] %x\n",
130 i, reg, mask, priv->irq_snapshot[i]);
Simon Glass8c0629b2019-12-08 17:40:08 -0700131 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
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100140static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
Simon Glass8c0629b2019-12-08 17:40:08 -0700141{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100142 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700143 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
Simon Glassc51a0212020-02-06 09:54:59 -0700154static int itss_bind(struct udevice *dev)
155{
Simon Glass88182022021-03-15 17:25:47 +1300156 /* 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))
Simon Glassc51a0212020-02-06 09:54:59 -0700159 dev->driver_data = X86_IRQT_ITSS;
160
161 return 0;
162}
163
Simon Glassaad29ae2020-12-03 16:55:21 -0700164static int itss_of_to_plat(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -0700165{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100166 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700167 int ret;
168
169#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700170 struct itss_plat *plat = dev_get_plat(dev);
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100171 struct dtd_intel_itss *dtplat = &plat->dtplat;
Simon Glass8c0629b2019-12-08 17:40:08 -0700172
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
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100207static const struct irq_ops itss_ops = {
208 .route_pmc_gpio_gpe = route_pmc_gpio_gpe,
209 .set_polarity = set_polarity,
Simon Glass8c0629b2019-12-08 17:40:08 -0700210#ifndef CONFIG_TPL_BUILD
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100211 .snapshot_polarities = snapshot_polarities,
212 .restore_polarities = restore_polarities,
Simon Glass8c0629b2019-12-08 17:40:08 -0700213#endif
214};
215
Simon Glass92882652021-08-07 07:24:04 -0600216#if CONFIG_IS_ENABLED(OF_REAL)
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100217static const struct udevice_id itss_ids[] = {
Simon Glass21bb12a2020-02-06 09:54:58 -0700218 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
Simon Glass8c0629b2019-12-08 17:40:08 -0700219 { }
220};
Simon Glassec8ae8a2020-12-23 08:11:30 -0700221#endif
Simon Glass8c0629b2019-12-08 17:40:08 -0700222
Simon Glassa055da82020-10-05 05:27:01 -0600223U_BOOT_DRIVER(intel_itss) = {
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100224 .name = "intel_itss",
Simon Glass8c0629b2019-12-08 17:40:08 -0700225 .id = UCLASS_IRQ,
Simon Glassec8ae8a2020-12-23 08:11:30 -0700226 .of_match = of_match_ptr(itss_ids),
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100227 .ops = &itss_ops,
Simon Glassc51a0212020-02-06 09:54:59 -0700228 .bind = itss_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700229 .of_to_plat = itss_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700230 .plat_auto = sizeof(struct itss_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700231 .priv_auto = sizeof(struct itss_priv),
Simon Glass8c0629b2019-12-08 17:40:08 -0700232};