blob: 6d3184f969f92b6b30716ce2bfb8245c81371d00 [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
105 for (i = reg_start; i < reg_end; i++) {
106 u32 mask;
107 u16 reg;
108 int irq_start;
109 int irq_end;
110
111 irq_start = i * IRQS_PER_IPC;
112 irq_end = min(irq_start + IRQS_PER_IPC - 1, ITSS_MAX_IRQ);
113
114 if (start > irq_end)
115 continue;
116 if (end < irq_start)
117 break;
118
119 /* Track bits within the bounds of of the register */
120 irq_start = max(start, irq_start) % IRQS_PER_IPC;
121 irq_end = min(end, irq_end) % IRQS_PER_IPC;
122
123 /* Create bitmask of the inclusive range of start and end */
124 mask = (((1U << irq_end) - 1) | (1U << irq_end));
125 mask &= ~((1U << irq_start) - 1);
126
127 reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
Simon Glassd89f1932020-07-16 21:22:30 -0600128 log_debug(" - %d, reg %x: mask %x, irq_snapshot[i] %x\n",
129 i, reg, mask, priv->irq_snapshot[i]);
Simon Glass8c0629b2019-12-08 17:40:08 -0700130 pcr_clrsetbits32(dev, reg, mask, mask & priv->irq_snapshot[i]);
131 }
132
133 show_polarities(dev, "After");
134
135 return 0;
136}
137#endif
138
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100139static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
Simon Glass8c0629b2019-12-08 17:40:08 -0700140{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100141 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700142 struct pmc_route *route;
143 int i;
144
145 for (i = 0, route = priv->route; i < priv->route_count; i++, route++) {
146 if (pmc_gpe_num == route->pmc)
147 return route->gpio;
148 }
149
150 return -ENOENT;
151}
152
Simon Glassc51a0212020-02-06 09:54:59 -0700153static int itss_bind(struct udevice *dev)
154{
Simon Glass88182022021-03-15 17:25:47 +1300155 /* This is not set with basic of-platdata, so set it manually */
156 if (CONFIG_IS_ENABLED(OF_PLATDATA) &&
157 !CONFIG_IS_ENABLED(OF_PLATDATA_INST))
Simon Glassc51a0212020-02-06 09:54:59 -0700158 dev->driver_data = X86_IRQT_ITSS;
159
160 return 0;
161}
162
Simon Glassaad29ae2020-12-03 16:55:21 -0700163static int itss_of_to_plat(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -0700164{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100165 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700166 int ret;
167
168#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700169 struct itss_plat *plat = dev_get_plat(dev);
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100170 struct dtd_intel_itss *dtplat = &plat->dtplat;
Simon Glass8c0629b2019-12-08 17:40:08 -0700171
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
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100206static const struct irq_ops itss_ops = {
207 .route_pmc_gpio_gpe = route_pmc_gpio_gpe,
208 .set_polarity = set_polarity,
Simon Glass8c0629b2019-12-08 17:40:08 -0700209#ifndef CONFIG_TPL_BUILD
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100210 .snapshot_polarities = snapshot_polarities,
211 .restore_polarities = restore_polarities,
Simon Glass8c0629b2019-12-08 17:40:08 -0700212#endif
213};
214
Simon Glass92882652021-08-07 07:24:04 -0600215#if CONFIG_IS_ENABLED(OF_REAL)
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100216static const struct udevice_id itss_ids[] = {
Simon Glass21bb12a2020-02-06 09:54:58 -0700217 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
Simon Glass8c0629b2019-12-08 17:40:08 -0700218 { }
219};
Simon Glassec8ae8a2020-12-23 08:11:30 -0700220#endif
Simon Glass8c0629b2019-12-08 17:40:08 -0700221
Simon Glassa055da82020-10-05 05:27:01 -0600222U_BOOT_DRIVER(intel_itss) = {
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100223 .name = "intel_itss",
Simon Glass8c0629b2019-12-08 17:40:08 -0700224 .id = UCLASS_IRQ,
Simon Glassec8ae8a2020-12-23 08:11:30 -0700225 .of_match = of_match_ptr(itss_ids),
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100226 .ops = &itss_ops,
Simon Glassc51a0212020-02-06 09:54:59 -0700227 .bind = itss_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700228 .of_to_plat = itss_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700229 .plat_auto = sizeof(struct itss_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700230 .priv_auto = sizeof(struct itss_priv),
Simon Glass8c0629b2019-12-08 17:40:08 -0700231};