mirror of
https://github.com/torvalds/linux.git
synced 2024-11-26 14:12:06 +00:00
9722c3b66e
The of_property_for_each_u32() macro needs five parameters, two of which are primarily meant as internal variables for the macro itself (in the for() clause). Yet these two parameters are used by a few drivers, and this can be considered misuse or at least bad practice. Now that the kernel uses C11 to build, these two parameters can be avoided by declaring them internally, thus changing this pattern: struct property *prop; const __be32 *p; u32 val; of_property_for_each_u32(np, "xyz", prop, p, val) { ... } to this: u32 val; of_property_for_each_u32(np, "xyz", val) { ... } However two variables cannot be declared in the for clause even with C11, so declare one struct that contain the two variables we actually need. As the variables inside this struct are not meant to be used by users of this macro, give the struct instance the noticeable name "_it" so it is visible during code reviews, helping to avoid new code to use it directly. Most usages are trivially converted as they do not use those two parameters, as expected. The non-trivial cases are: - drivers/clk/clk.c, of_clk_get_parent_name(): easily doable anyway - drivers/clk/clk-si5351.c, si5351_dt_parse(): this is more complex as the checks had to be replicated in a different way, making code more verbose and somewhat uglier, but I refrained from a full rework to keep as much of the original code untouched having no hardware to test my changes All the changes have been build tested. The few for which I have the hardware have been runtime-tested too. Reviewed-by: Andre Przywara <andre.przywara@arm.com> # drivers/clk/sunxi/clk-simple-gates.c, drivers/clk/sunxi/clk-sun8i-bus-gates.c Acked-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> # drivers/gpio/gpio-brcmstb.c Acked-by: Nicolas Ferre <nicolas.ferre@microchip.com> # drivers/irqchip/irq-atmel-aic-common.c Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> # drivers/iio/adc/ti_am335x_adc.c Acked-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com> # drivers/pwm/pwm-samsung.c Acked-by: Richard Leitner <richard.leitner@linux.dev> # drivers/usb/misc/usb251xb.c Acked-by: Mark Brown <broonie@kernel.org> # sound/soc/codecs/arizona.c Reviewed-by: Richard Fitzgerald <rf@opensource.cirrus.com> # sound/soc/codecs/arizona.c Acked-by: Michael Ellerman <mpe@ellerman.id.au> # arch/powerpc/sysdev/xive/spapr.c Acked-by: Stephen Boyd <sboyd@kernel.org> # clk Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com> Acked-by: Lee Jones <lee@kernel.org> Link: https://lore.kernel.org/r/20240724-of_property_for_each_u32-v3-1-bea82ce429e2@bootlin.com Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
274 lines
6.0 KiB
C
274 lines
6.0 KiB
C
/*
|
|
* Atmel AT91 common AIC (Advanced Interrupt Controller) code shared by
|
|
* irq-atmel-aic and irq-atmel-aic5 drivers
|
|
*
|
|
* Copyright (C) 2004 SAN People
|
|
* Copyright (C) 2004 ATMEL
|
|
* Copyright (C) Rick Bronson
|
|
* Copyright (C) 2014 Free Electrons
|
|
*
|
|
* Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
|
|
*
|
|
* This file is licensed under the terms of the GNU General Public
|
|
* License version 2. This program is licensed "as is" without any
|
|
* warranty of any kind, whether express or implied.
|
|
*/
|
|
|
|
#include <linux/errno.h>
|
|
#include <linux/io.h>
|
|
#include <linux/irq.h>
|
|
#include <linux/irqdomain.h>
|
|
#include <linux/of.h>
|
|
#include <linux/of_address.h>
|
|
#include <linux/slab.h>
|
|
|
|
#include "irq-atmel-aic-common.h"
|
|
|
|
#define AT91_AIC_PRIOR GENMASK(2, 0)
|
|
#define AT91_AIC_IRQ_MIN_PRIORITY 0
|
|
#define AT91_AIC_IRQ_MAX_PRIORITY 7
|
|
|
|
#define AT91_AIC_SRCTYPE GENMASK(6, 5)
|
|
#define AT91_AIC_SRCTYPE_LOW (0 << 5)
|
|
#define AT91_AIC_SRCTYPE_FALLING (1 << 5)
|
|
#define AT91_AIC_SRCTYPE_HIGH (2 << 5)
|
|
#define AT91_AIC_SRCTYPE_RISING (3 << 5)
|
|
|
|
struct aic_chip_data {
|
|
u32 ext_irqs;
|
|
};
|
|
|
|
static void aic_common_shutdown(struct irq_data *d)
|
|
{
|
|
struct irq_chip_type *ct = irq_data_get_chip_type(d);
|
|
|
|
ct->chip.irq_mask(d);
|
|
}
|
|
|
|
int aic_common_set_type(struct irq_data *d, unsigned type, unsigned *val)
|
|
{
|
|
struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
|
|
struct aic_chip_data *aic = gc->private;
|
|
unsigned aic_type;
|
|
|
|
switch (type) {
|
|
case IRQ_TYPE_LEVEL_HIGH:
|
|
aic_type = AT91_AIC_SRCTYPE_HIGH;
|
|
break;
|
|
case IRQ_TYPE_EDGE_RISING:
|
|
aic_type = AT91_AIC_SRCTYPE_RISING;
|
|
break;
|
|
case IRQ_TYPE_LEVEL_LOW:
|
|
if (!(d->mask & aic->ext_irqs))
|
|
return -EINVAL;
|
|
|
|
aic_type = AT91_AIC_SRCTYPE_LOW;
|
|
break;
|
|
case IRQ_TYPE_EDGE_FALLING:
|
|
if (!(d->mask & aic->ext_irqs))
|
|
return -EINVAL;
|
|
|
|
aic_type = AT91_AIC_SRCTYPE_FALLING;
|
|
break;
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
|
|
*val &= ~AT91_AIC_SRCTYPE;
|
|
*val |= aic_type;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void aic_common_set_priority(int priority, unsigned *val)
|
|
{
|
|
*val &= ~AT91_AIC_PRIOR;
|
|
*val |= priority;
|
|
}
|
|
|
|
int aic_common_irq_domain_xlate(struct irq_domain *d,
|
|
struct device_node *ctrlr,
|
|
const u32 *intspec,
|
|
unsigned int intsize,
|
|
irq_hw_number_t *out_hwirq,
|
|
unsigned int *out_type)
|
|
{
|
|
if (WARN_ON(intsize < 3))
|
|
return -EINVAL;
|
|
|
|
if (WARN_ON((intspec[2] < AT91_AIC_IRQ_MIN_PRIORITY) ||
|
|
(intspec[2] > AT91_AIC_IRQ_MAX_PRIORITY)))
|
|
return -EINVAL;
|
|
|
|
*out_hwirq = intspec[0];
|
|
*out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void __init aic_common_ext_irq_of_init(struct irq_domain *domain)
|
|
{
|
|
struct device_node *node = irq_domain_get_of_node(domain);
|
|
struct irq_chip_generic *gc;
|
|
struct aic_chip_data *aic;
|
|
u32 hwirq;
|
|
|
|
gc = irq_get_domain_generic_chip(domain, 0);
|
|
|
|
aic = gc->private;
|
|
aic->ext_irqs |= 1;
|
|
|
|
of_property_for_each_u32(node, "atmel,external-irqs", hwirq) {
|
|
gc = irq_get_domain_generic_chip(domain, hwirq);
|
|
if (!gc) {
|
|
pr_warn("AIC: external irq %d >= %d skip it\n",
|
|
hwirq, domain->revmap_size);
|
|
continue;
|
|
}
|
|
|
|
aic = gc->private;
|
|
aic->ext_irqs |= (1 << (hwirq % 32));
|
|
}
|
|
}
|
|
|
|
#define AT91_RTC_IDR 0x24
|
|
#define AT91_RTC_IMR 0x28
|
|
#define AT91_RTC_IRQ_MASK 0x1f
|
|
|
|
void __init aic_common_rtc_irq_fixup(void)
|
|
{
|
|
struct device_node *np;
|
|
void __iomem *regs;
|
|
|
|
np = of_find_compatible_node(NULL, NULL, "atmel,at91rm9200-rtc");
|
|
if (!np)
|
|
np = of_find_compatible_node(NULL, NULL,
|
|
"atmel,at91sam9x5-rtc");
|
|
|
|
if (!np)
|
|
return;
|
|
|
|
regs = of_iomap(np, 0);
|
|
of_node_put(np);
|
|
|
|
if (!regs)
|
|
return;
|
|
|
|
writel(AT91_RTC_IRQ_MASK, regs + AT91_RTC_IDR);
|
|
|
|
iounmap(regs);
|
|
}
|
|
|
|
#define AT91_RTT_MR 0x00 /* Real-time Mode Register */
|
|
#define AT91_RTT_ALMIEN (1 << 16) /* Alarm Interrupt Enable */
|
|
#define AT91_RTT_RTTINCIEN (1 << 17) /* Real Time Timer Increment Interrupt Enable */
|
|
|
|
void __init aic_common_rtt_irq_fixup(void)
|
|
{
|
|
struct device_node *np;
|
|
void __iomem *regs;
|
|
|
|
/*
|
|
* The at91sam9263 SoC has 2 instances of the RTT block, hence we
|
|
* iterate over the DT to find each occurrence.
|
|
*/
|
|
for_each_compatible_node(np, NULL, "atmel,at91sam9260-rtt") {
|
|
regs = of_iomap(np, 0);
|
|
if (!regs)
|
|
continue;
|
|
|
|
writel(readl(regs + AT91_RTT_MR) &
|
|
~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN),
|
|
regs + AT91_RTT_MR);
|
|
|
|
iounmap(regs);
|
|
}
|
|
}
|
|
|
|
static void __init aic_common_irq_fixup(const struct of_device_id *matches)
|
|
{
|
|
struct device_node *root = of_find_node_by_path("/");
|
|
const struct of_device_id *match;
|
|
|
|
if (!root)
|
|
return;
|
|
|
|
match = of_match_node(matches, root);
|
|
|
|
if (match) {
|
|
void (*fixup)(void) = match->data;
|
|
fixup();
|
|
}
|
|
|
|
of_node_put(root);
|
|
}
|
|
|
|
struct irq_domain *__init aic_common_of_init(struct device_node *node,
|
|
const struct irq_domain_ops *ops,
|
|
const char *name, int nirqs,
|
|
const struct of_device_id *matches)
|
|
{
|
|
struct irq_chip_generic *gc;
|
|
struct irq_domain *domain;
|
|
struct aic_chip_data *aic;
|
|
void __iomem *reg_base;
|
|
int nchips;
|
|
int ret;
|
|
int i;
|
|
|
|
nchips = DIV_ROUND_UP(nirqs, 32);
|
|
|
|
reg_base = of_iomap(node, 0);
|
|
if (!reg_base)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
aic = kcalloc(nchips, sizeof(*aic), GFP_KERNEL);
|
|
if (!aic) {
|
|
ret = -ENOMEM;
|
|
goto err_iounmap;
|
|
}
|
|
|
|
domain = irq_domain_add_linear(node, nchips * 32, ops, aic);
|
|
if (!domain) {
|
|
ret = -ENOMEM;
|
|
goto err_free_aic;
|
|
}
|
|
|
|
ret = irq_alloc_domain_generic_chips(domain, 32, 1, name,
|
|
handle_fasteoi_irq,
|
|
IRQ_NOREQUEST | IRQ_NOPROBE |
|
|
IRQ_NOAUTOEN, 0, 0);
|
|
if (ret)
|
|
goto err_domain_remove;
|
|
|
|
for (i = 0; i < nchips; i++) {
|
|
gc = irq_get_domain_generic_chip(domain, i * 32);
|
|
|
|
gc->reg_base = reg_base;
|
|
|
|
gc->unused = 0;
|
|
gc->wake_enabled = ~0;
|
|
gc->chip_types[0].type = IRQ_TYPE_SENSE_MASK;
|
|
gc->chip_types[0].chip.irq_eoi = irq_gc_eoi;
|
|
gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
|
|
gc->chip_types[0].chip.irq_shutdown = aic_common_shutdown;
|
|
gc->private = &aic[i];
|
|
}
|
|
|
|
aic_common_ext_irq_of_init(domain);
|
|
aic_common_irq_fixup(matches);
|
|
|
|
return domain;
|
|
|
|
err_domain_remove:
|
|
irq_domain_remove(domain);
|
|
|
|
err_free_aic:
|
|
kfree(aic);
|
|
|
|
err_iounmap:
|
|
iounmap(reg_base);
|
|
|
|
return ERR_PTR(ret);
|
|
}
|