blob: f5a921b3d1befe3721fe7af63919c111facca7b9 [file] [log] [blame]
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Broadcom.
4 */
5#include <common.h>
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +05306#include <dm.h>
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +05307#include <regmap.h>
8#include <syscon.h>
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -08009#include <asm/gic.h>
10#include <asm/gic-v3.h>
11#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Wasim Khan1fc54802020-02-14 11:00:52 +053013#include <linux/sizes.h>
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080014
15static u32 lpi_id_bits;
16
17#define LPI_NRBITS lpi_id_bits
18#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
19#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
20
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +053021/* Number of GIC re-distributors */
22#define MAX_GIC_REDISTRIBUTORS 8
23
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080024/*
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053025 * gic_v3_its_priv - gic details
26 *
27 * @gicd_base: gicd base address
28 * @gicr_base: gicr base address
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +053029 * @lpi_base: gic lpi base address
30 * @num_redist: number of gic re-distributors
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053031 */
32struct gic_v3_its_priv {
33 ulong gicd_base;
34 ulong gicr_base;
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +053035 ulong lpi_base;
36 u32 num_redist;
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053037};
38
39static int gic_v3_its_get_gic_addr(struct gic_v3_its_priv *priv)
40{
41 struct udevice *dev;
42 fdt_addr_t addr;
43 int ret;
44
45 ret = uclass_get_device_by_driver(UCLASS_IRQ,
Simon Glass65130cd2020-12-28 20:34:56 -070046 DM_DRIVER_GET(arm_gic_v3_its), &dev);
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053047 if (ret) {
48 pr_err("%s: failed to get %s irq device\n", __func__,
Simon Glass65130cd2020-12-28 20:34:56 -070049 DM_DRIVER_GET(arm_gic_v3_its)->name);
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053050 return ret;
51 }
52
53 addr = dev_read_addr_index(dev, 0);
54 if (addr == FDT_ADDR_T_NONE) {
55 pr_err("%s: failed to get GICD address\n", __func__);
56 return -EINVAL;
57 }
58 priv->gicd_base = addr;
59
60 addr = dev_read_addr_index(dev, 1);
61 if (addr == FDT_ADDR_T_NONE) {
62 pr_err("%s: failed to get GICR address\n", __func__);
63 return -EINVAL;
64 }
65 priv->gicr_base = addr;
66
67 return 0;
68}
69
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +053070static int gic_v3_its_get_gic_lpi_addr(struct gic_v3_its_priv *priv)
71{
72 struct regmap *regmap;
73 struct udevice *dev;
74 int ret;
75
76 ret = uclass_get_device_by_driver(UCLASS_SYSCON,
Simon Glass65130cd2020-12-28 20:34:56 -070077 DM_DRIVER_GET(gic_lpi_syscon), &dev);
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +053078 if (ret) {
79 pr_err("%s: failed to get %s syscon device\n", __func__,
Simon Glass65130cd2020-12-28 20:34:56 -070080 DM_DRIVER_GET(gic_lpi_syscon)->name);
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +053081 return ret;
82 }
83
84 regmap = syscon_get_regmap(dev);
85 if (!regmap) {
86 pr_err("%s: failed to regmap for %s syscon device\n", __func__,
Simon Glass65130cd2020-12-28 20:34:56 -070087 DM_DRIVER_GET(gic_lpi_syscon)->name);
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +053088 return -ENODEV;
89 }
90 priv->lpi_base = regmap->ranges[0].start;
91
92 priv->num_redist = dev_read_u32_default(dev, "max-gic-redistributors",
93 MAX_GIC_REDISTRIBUTORS);
94
95 return 0;
96}
97
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053098/*
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080099 * Program the GIC LPI configuration tables for all
100 * the re-distributors and enable the LPI table
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800101 */
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +0530102int gic_lpi_tables_init(void)
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800103{
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530104 struct gic_v3_its_priv priv;
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800105 u32 gicd_typer;
106 u64 val;
107 u64 tmp;
108 int i;
109 u64 redist_lpi_base;
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530110 u64 pend_base;
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800111
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530112 if (gic_v3_its_get_gic_addr(&priv))
113 return -EINVAL;
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800114
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +0530115 if (gic_v3_its_get_gic_lpi_addr(&priv))
116 return -EINVAL;
117
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530118 gicd_typer = readl((uintptr_t)(priv.gicd_base + GICD_TYPER));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800119 /* GIC support for Locality specific peripheral interrupts (LPI's) */
120 if (!(gicd_typer & GICD_TYPER_LPIS)) {
121 pr_err("GIC implementation does not support LPI's\n");
122 return -EINVAL;
123 }
124
125 /*
126 * Check for LPI is disabled for all the redistributors.
127 * Once the LPI table is enabled, can not program the
128 * LPI configuration tables again, unless the GIC is reset.
129 */
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +0530130 for (i = 0; i < priv.num_redist; i++) {
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800131 u32 offset = i * GIC_REDISTRIBUTOR_OFFSET;
132
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530133 if ((readl((uintptr_t)(priv.gicr_base + offset))) &
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800134 GICR_CTLR_ENABLE_LPIS) {
135 pr_err("Re-Distributor %d LPI is already enabled\n",
136 i);
137 return -EINVAL;
138 }
139 }
140
141 /* lpi_id_bits to get LPI_PENDBASE_SZ and LPi_PROPBASE_SZ */
142 lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gicd_typer),
143 ITS_MAX_LPI_NRBITS);
144
145 /* Set PropBase */
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +0530146 val = (priv.lpi_base |
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800147 GICR_PROPBASER_INNERSHAREABLE |
148 GICR_PROPBASER_RAWAWB |
149 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
150
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530151 writeq(val, (uintptr_t)(priv.gicr_base + GICR_PROPBASER));
152 tmp = readl((uintptr_t)(priv.gicr_base + GICR_PROPBASER));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800153 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
154 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
155 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
156 GICR_PROPBASER_CACHEABILITY_MASK);
157 val |= GICR_PROPBASER_NC;
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530158 writeq(val,
159 (uintptr_t)(priv.gicr_base + GICR_PROPBASER));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800160 }
161 }
162
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +0530163 redist_lpi_base = priv.lpi_base + LPI_PROPBASE_SZ;
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800164
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530165 pend_base = priv.gicr_base + GICR_PENDBASER;
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +0530166 for (i = 0; i < priv.num_redist; i++) {
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800167 u32 offset = i * GIC_REDISTRIBUTOR_OFFSET;
168
169 val = ((redist_lpi_base + (i * LPI_PENDBASE_SZ)) |
170 GICR_PENDBASER_INNERSHAREABLE |
171 GICR_PENDBASER_RAWAWB);
172
173 writeq(val, (uintptr_t)(pend_base + offset));
174 tmp = readq((uintptr_t)(pend_base + offset));
175 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
176 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
177 GICR_PENDBASER_CACHEABILITY_MASK);
178 val |= GICR_PENDBASER_NC;
179 writeq(val, (uintptr_t)(pend_base + offset));
180 }
181
182 /* Enable LPI for the redistributor */
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530183 writel(GICR_CTLR_ENABLE_LPIS,
184 (uintptr_t)(priv.gicr_base + offset));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800185 }
186
187 return 0;
188}
189
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530190static const struct udevice_id gic_v3_its_ids[] = {
191 { .compatible = "arm,gic-v3" },
192 {}
193};
194
195U_BOOT_DRIVER(arm_gic_v3_its) = {
196 .name = "gic-v3",
197 .id = UCLASS_IRQ,
198 .of_match = gic_v3_its_ids,
199};
Rayagonda Kokatanur158c16e2020-07-26 22:37:33 +0530200
201static const struct udevice_id gic_lpi_syscon_ids[] = {
202 { .compatible = "gic-lpi-base" },
203 {}
204};
205
206U_BOOT_DRIVER(gic_lpi_syscon) = {
207 .name = "gic-lpi-base",
208 .id = UCLASS_SYSCON,
209 .of_match = gic_lpi_syscon_ids,
210};