blob: e4f785ce895dcb46f741708a99314478a09cd759 [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 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>
Wolfgang Wallner97132162020-01-22 16:01:45 +010019#include <asm/itss.h>
Simon Glass8c0629b2019-12-08 17:40:08 -070020
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010021struct itss_platdata {
Simon Glass8c0629b2019-12-08 17:40:08 -070022#if CONFIG_IS_ENABLED(OF_PLATDATA)
23 /* Put this first since driver model will copy the data here */
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010024 struct dtd_intel_itss dtplat;
Simon Glass8c0629b2019-12-08 17:40:08 -070025#endif
26};
27
28/* struct pmc_route - Routing for PMC to GPIO */
29struct pmc_route {
30 u32 pmc;
31 u32 gpio;
32};
33
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010034struct itss_priv {
Simon Glass8c0629b2019-12-08 17:40:08 -070035 struct pmc_route *route;
36 uint route_count;
37 u32 irq_snapshot[NUM_IPC_REGS];
38};
39
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010040static int set_polarity(struct udevice *dev, uint irq, bool active_low)
Simon Glass8c0629b2019-12-08 17:40:08 -070041{
42 u32 mask;
43 uint reg;
44
45 if (irq > ITSS_MAX_IRQ)
46 return -EINVAL;
47
48 reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * (irq / IRQS_PER_IPC);
49 mask = 1 << (irq % IRQS_PER_IPC);
50
51 pcr_clrsetbits32(dev, reg, mask, active_low ? mask : 0);
52
53 return 0;
54}
55
56#ifndef CONFIG_TPL_BUILD
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010057static int snapshot_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070058{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010059 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -070060 const int start = GPIO_IRQ_START;
61 const int end = GPIO_IRQ_END;
62 int reg_start;
63 int reg_end;
64 int i;
65
66 reg_start = start / IRQS_PER_IPC;
67 reg_end = (end + IRQS_PER_IPC - 1) / IRQS_PER_IPC;
68
69 for (i = reg_start; i < reg_end; i++) {
70 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
71
72 priv->irq_snapshot[i] = pcr_read32(dev, reg);
73 }
74
75 return 0;
76}
77
78static void show_polarities(struct udevice *dev, const char *msg)
79{
80 int i;
81
82 log_info("ITSS IRQ Polarities %s:\n", msg);
83 for (i = 0; i < NUM_IPC_REGS; i++) {
84 uint reg = PCR_ITSS_IPC0_CONF + sizeof(u32) * i;
85
86 log_info("IPC%d: 0x%08x\n", i, pcr_read32(dev, reg));
87 }
88}
89
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010090static int restore_polarities(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -070091{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +010092 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -070093 const int start = GPIO_IRQ_START;
94 const int end = GPIO_IRQ_END;
95 int reg_start;
96 int reg_end;
97 int i;
98
99 show_polarities(dev, "Before");
100
101 reg_start = start / IRQS_PER_IPC;
102 reg_end = (end + IRQS_PER_IPC - 1) / IRQS_PER_IPC;
103
104 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;
127 pcr_clrsetbits32(dev, reg, mask, mask & priv->irq_snapshot[i]);
128 }
129
130 show_polarities(dev, "After");
131
132 return 0;
133}
134#endif
135
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100136static int route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
Simon Glass8c0629b2019-12-08 17:40:08 -0700137{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100138 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700139 struct pmc_route *route;
140 int i;
141
142 for (i = 0, route = priv->route; i < priv->route_count; i++, route++) {
143 if (pmc_gpe_num == route->pmc)
144 return route->gpio;
145 }
146
147 return -ENOENT;
148}
149
Simon Glassc51a0212020-02-06 09:54:59 -0700150static int itss_bind(struct udevice *dev)
151{
152 /* This is not set with of-platdata, so set it manually */
153 if (CONFIG_IS_ENABLED(OF_PLATDATA))
154 dev->driver_data = X86_IRQT_ITSS;
155
156 return 0;
157}
158
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100159static int itss_ofdata_to_platdata(struct udevice *dev)
Simon Glass8c0629b2019-12-08 17:40:08 -0700160{
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100161 struct itss_priv *priv = dev_get_priv(dev);
Simon Glass8c0629b2019-12-08 17:40:08 -0700162 int ret;
163
164#if CONFIG_IS_ENABLED(OF_PLATDATA)
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100165 struct itss_platdata *plat = dev_get_platdata(dev);
166 struct dtd_intel_itss *dtplat = &plat->dtplat;
Simon Glass8c0629b2019-12-08 17:40:08 -0700167
168 /*
169 * It would be nice to do this in the bind() method, but with
170 * of-platdata binding happens in the order that DM finds things in the
171 * linker list (i.e. alphabetical order by driver name). So the GPIO
172 * device may well be bound before its parent (p2sb), and this call
173 * will fail if p2sb is not bound yet.
174 *
175 * TODO(sjg@chromium.org): Add a parent pointer to child devices in dtoc
176 */
177 ret = p2sb_set_port_id(dev, dtplat->intel_p2sb_port_id);
178 if (ret)
179 return log_msg_ret("Could not set port id", ret);
180 priv->route = (struct pmc_route *)dtplat->intel_pmc_routes;
181 priv->route_count = ARRAY_SIZE(dtplat->intel_pmc_routes) /
182 sizeof(struct pmc_route);
183#else
184 int size;
185
186 size = dev_read_size(dev, "intel,pmc-routes");
187 if (size < 0)
188 return size;
189 priv->route = malloc(size);
190 if (!priv->route)
191 return -ENOMEM;
192 ret = dev_read_u32_array(dev, "intel,pmc-routes", (u32 *)priv->route,
193 size / sizeof(fdt32_t));
194 if (ret)
195 return log_msg_ret("Cannot read pmc-routes", ret);
196 priv->route_count = size / sizeof(struct pmc_route);
197#endif
198
199 return 0;
200}
201
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100202static const struct irq_ops itss_ops = {
203 .route_pmc_gpio_gpe = route_pmc_gpio_gpe,
204 .set_polarity = set_polarity,
Simon Glass8c0629b2019-12-08 17:40:08 -0700205#ifndef CONFIG_TPL_BUILD
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100206 .snapshot_polarities = snapshot_polarities,
207 .restore_polarities = restore_polarities,
Simon Glass8c0629b2019-12-08 17:40:08 -0700208#endif
209};
210
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100211static const struct udevice_id itss_ids[] = {
Simon Glass21bb12a2020-02-06 09:54:58 -0700212 { .compatible = "intel,itss", .data = X86_IRQT_ITSS },
Simon Glass8c0629b2019-12-08 17:40:08 -0700213 { }
214};
215
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100216U_BOOT_DRIVER(itss_drv) = {
217 .name = "intel_itss",
Simon Glass8c0629b2019-12-08 17:40:08 -0700218 .id = UCLASS_IRQ,
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100219 .of_match = itss_ids,
220 .ops = &itss_ops,
Simon Glassc51a0212020-02-06 09:54:59 -0700221 .bind = itss_bind,
Wolfgang Wallner3767e7f2020-01-22 16:01:47 +0100222 .ofdata_to_platdata = itss_ofdata_to_platdata,
223 .platdata_auto_alloc_size = sizeof(struct itss_platdata),
224 .priv_auto_alloc_size = sizeof(struct itss_priv),
Simon Glass8c0629b2019-12-08 17:40:08 -0700225};