blob: 963afa8f5beba1c5eee5b7799819f1634da81d36 [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>
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 +010022struct itss_platdata {
Simon Glass8c0629b2019-12-08 17:40:08 -070023#if CONFIG_IS_ENABLED(OF_PLATDATA)
24 /* Put this first since driver model will copy the data here */
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010025 struct dtd_intel_itss dtplat;
Simon Glass8c0629b2019-12-08 17:40:08 -070026#endif
27};
28
29/* struct pmc_route - Routing for PMC to GPIO */
30struct pmc_route {
31 u32 pmc;
32 u32 gpio;
33};
34
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010035struct itss_priv {
Simon Glass8c0629b2019-12-08 17:40:08 -070036 struct pmc_route *route;
37 uint route_count;
38 u32 irq_snapshot[NUM_IPC_REGS];
39};
40
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010041static int set_polarity(struct udevice *dev, uint irq, bool active_low)
Simon Glass8c0629b2019-12-08 17:40:08 -070042{
43 u32 mask;
44 uint reg;
45
46 if (irq > ITSS_MAX_IRQ)
47 return -EINVAL;
48
49 reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * (irq / IRQS_PER_IPC);
50 mask = 1 << (irq % IRQS_PER_IPC);
51
52 pcr_clrsetbits32(dev, reg, mask, active_low ? mask : 0);
53
54 return 0;
55}
56
57#ifndef CONFIG_TPL_BUILD
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010058static int snapshot_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070059{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010060 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -070061 const int start = GPIO_IRQ_START;
62 const int end = GPIO_IRQ_END;
63 int reg_start;
64 int reg_end;
65 int i;
66
67 reg_start = start / IRQS_PER_IPC;
68 reg_end = (end + IRQS_PER_IPC - 1) / IRQS_PER_IPC;
69
70 for (i = reg_start; i < reg_end; i++) {
71 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
72
73 priv->irq_snapshot[i] = pcr_read32(dev, reg);
74 }
75
76 return 0;
77}
78
79static void show_polarities(struct udevice *dev, const char *msg)
80{
81 int i;
82
83 log_info("ITSS IRQ Polarities %s:\n", msg);
84 for (i = 0; i < NUM_IPC_REGS; i++) {
85 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
86
87 log_info("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg));
88 }
89}
90
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010091static int restore_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070092{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010093 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -070094 const int start = GPIO_IRQ_START;
95 const int end = GPIO_IRQ_END;
96 int reg_start;
97 int reg_end;
98 int i;
99
100 show_polarities(dev, "Before");
101
102 reg_start = start / IRQS_PER_IPC;
103 reg_end = (end + IRQS_PER_IPC - 1) / IRQS_PER_IPC;
104
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;
128 pcr_clrsetbits32(dev, reg, mask, mask & priv->irq_snapshot[i]);
129 }
130
131 show_polarities(dev, "After");
132
133 return 0;
134}
135#endif
136
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100137static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
Simon Glass8c0629b2019-12-08 17:40:08 -0700138{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100139 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700140 struct pmc_route *route;
141 int i;
142
143 for (i = 0, route = priv->route; i < priv->route_count; i++, route++) {
144 if (pmc_gpe_num == route->pmc)
145 return route->gpio;
146 }
147
148 return -ENOENT;
149}
150
Simon Glassc51a0212020-02-06 09:54:59 -0700151static int itss_bind(struct udevice *dev)
152{
153 /* This is not set with of-platdata, so set it manually */
154 if (CONFIG_IS_ENABLED(OF_PLATDATA))
155 dev->driver_data = X86_IRQT_ITSS;
156
157 return 0;
158}
159
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100160static int itss_ofdata_to_platdata(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -0700161{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100162 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700163 int ret;
164
165#if CONFIG_IS_ENABLED(OF_PLATDATA)
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100166 struct itss_platdata *plat = dev_get_platdata(dev);
167 struct dtd_intel_itss *dtplat = &plat->dtplat;
Simon Glass8c0629b2019-12-08 17:40:08 -0700168
169 /*
170 * It would be nice to do this in the bind() method, but with
171 * of-platdata binding happens in the order that DM finds things in the
172 * linker list (i.e. alphabetical order by driver name). So the GPIO
173 * device may well be bound before its parent (p2sb), and this call
174 * will fail if p2sb is not bound yet.
175 *
176 * TODO(sjg@chromium.org): Add a parent pointer to child devices in dtoc
177 */
178 ret = p2sb_set_port_id(dev, dtplat->intel_p2sb_port_id);
179 if (ret)
180 return log_msg_ret("Could not set port id", ret);
181 priv->route = (struct pmc_route *)dtplat->intel_pmc_routes;
182 priv->route_count = ARRAY_SIZE(dtplat->intel_pmc_routes) /
183 sizeof(struct pmc_route);
184#else
185 int size;
186
187 size = dev_read_size(dev, "intel,pmc-routes");
188 if (size < 0)
189 return size;
190 priv->route = malloc(size);
191 if (!priv->route)
192 return -ENOMEM;
193 ret = dev_read_u32_array(dev, "intel,pmc-routes", (u32 *)priv->route,
194 size / sizeof(fdt32_t));
195 if (ret)
196 return log_msg_ret("Cannot read pmc-routes", ret);
197 priv->route_count = size / sizeof(struct pmc_route);
198#endif
199
200 return 0;
201}
202
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100203static const struct irq_ops itss_ops = {
204 .route_pmc_gpio_gpe = route_pmc_gpio_gpe,
205 .set_polarity = set_polarity,
Simon Glass8c0629b2019-12-08 17:40:08 -0700206#ifndef CONFIG_TPL_BUILD
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100207 .snapshot_polarities = snapshot_polarities,
208 .restore_polarities = restore_polarities,
Simon Glass8c0629b2019-12-08 17:40:08 -0700209#endif
210};
211
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100212static const struct udevice_id itss_ids[] = {
Simon Glass21bb12a2020-02-06 09:54:58 -0700213 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
Simon Glass8c0629b2019-12-08 17:40:08 -0700214 { }
215};
216
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100217U_BOOT_DRIVER(itss_drv) = {
218 .name = "intel_itss",
Simon Glass8c0629b2019-12-08 17:40:08 -0700219 .id = UCLASS_IRQ,
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100220 .of_match = itss_ids,
221 .ops = &itss_ops,
Simon Glassc51a0212020-02-06 09:54:59 -0700222 .bind = itss_bind,
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100223 .ofdata_to_platdata = itss_ofdata_to_platdata,
224 .platdata_auto_alloc_size = sizeof(struct itss_platdata),
225 .priv_auto_alloc_size = sizeof(struct itss_priv),
Simon Glass8c0629b2019-12-08 17:40:08 -0700226};