^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Serge Semin <Sergey.Semin@baikalelectronics.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Baikal-T1 APB-bus driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/reset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/time64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define APB_EHB_ISR 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define APB_EHB_ISR_PENDING BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define APB_EHB_ISR_MASK BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define APB_EHB_ADDR 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define APB_EHB_TIMEOUT 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define APB_EHB_TIMEOUT_MIN 0x000003FFU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define APB_EHB_TIMEOUT_MAX 0xFFFFFFFFU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * struct bt1_apb - Baikal-T1 APB EHB private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @dev: Pointer to the device structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @regs: APB EHB registers map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @res: No-device error injection memory region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @irq: Errors IRQ number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @rate: APB-bus reference clock rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @pclk: APB-reference clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @prst: APB domain reset line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @count: Number of errors detected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct bt1_apb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct regmap *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void __iomem *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned long rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct clk *pclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct reset_control *prst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) atomic_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static const struct regmap_config bt1_apb_regmap_cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .reg_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .val_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .reg_stride = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .max_register = APB_EHB_TIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .fast_io = true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static inline unsigned long bt1_apb_n_to_timeout_us(struct bt1_apb *apb, u32 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u64 timeout = (u64)n * USEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) do_div(timeout, apb->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static inline unsigned long bt1_apb_timeout_to_n_us(struct bt1_apb *apb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) u64 n = (u64)timeout * apb->rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) do_div(n, USEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static irqreturn_t bt1_apb_isr(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct bt1_apb *apb = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) u32 addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) regmap_read(apb->regs, APB_EHB_ADDR, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) dev_crit_ratelimited(apb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) "APB-bus fault %d: Slave access timeout at 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) atomic_inc_return(&apb->count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * Print backtrace on each CPU. This might be pointless if the fault
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * has happened on the same CPU as the IRQ handler is executed or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * the other core proceeded further execution despite the error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * But if it's not, by looking at the trace we would get straight to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * the cause of the problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) trigger_all_cpu_backtrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_PENDING, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void bt1_apb_clear_data(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct bt1_apb *apb = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct platform_device *pdev = to_platform_device(apb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) platform_set_drvdata(pdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static struct bt1_apb *bt1_apb_create_data(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct bt1_apb *apb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) apb = devm_kzalloc(dev, sizeof(*apb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!apb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = devm_add_action(dev, bt1_apb_clear_data, apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) dev_err(dev, "Can't add APB EHB data clear action\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) apb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) atomic_set(&apb->count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) platform_set_drvdata(pdev, apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return apb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int bt1_apb_request_regs(struct bt1_apb *apb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct platform_device *pdev = to_platform_device(apb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void __iomem *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) regs = devm_platform_ioremap_resource_byname(pdev, "ehb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (IS_ERR(regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) dev_err(apb->dev, "Couldn't map APB EHB registers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return PTR_ERR(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) apb->regs = devm_regmap_init_mmio(apb->dev, regs, &bt1_apb_regmap_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (IS_ERR(apb->regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) dev_err(apb->dev, "Couldn't create APB EHB regmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return PTR_ERR(apb->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) apb->res = devm_platform_ioremap_resource_byname(pdev, "nodev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (IS_ERR(apb->res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) dev_err(apb->dev, "Couldn't map reserved region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return PTR_ERR_OR_ZERO(apb->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int bt1_apb_request_rst(struct bt1_apb *apb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) apb->prst = devm_reset_control_get_optional_exclusive(apb->dev, "prst");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (IS_ERR(apb->prst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) dev_warn(apb->dev, "Couldn't get reset control line\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return PTR_ERR(apb->prst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = reset_control_deassert(apb->prst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) dev_err(apb->dev, "Failed to deassert the reset line\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static void bt1_apb_disable_clk(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct bt1_apb *apb = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) clk_disable_unprepare(apb->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int bt1_apb_request_clk(struct bt1_apb *apb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) apb->pclk = devm_clk_get(apb->dev, "pclk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (IS_ERR(apb->pclk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) dev_err(apb->dev, "Couldn't get APB clock descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return PTR_ERR(apb->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ret = clk_prepare_enable(apb->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dev_err(apb->dev, "Couldn't enable the APB clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ret = devm_add_action_or_reset(apb->dev, bt1_apb_disable_clk, apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) dev_err(apb->dev, "Can't add APB EHB clocks disable action\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) apb->rate = clk_get_rate(apb->pclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!apb->rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dev_err(apb->dev, "Invalid clock rate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static void bt1_apb_clear_irq(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct bt1_apb *apb = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_MASK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int bt1_apb_request_irq(struct bt1_apb *apb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct platform_device *pdev = to_platform_device(apb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) apb->irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (apb->irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return apb->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ret = devm_request_irq(apb->dev, apb->irq, bt1_apb_isr, IRQF_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) "bt1-apb", apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) dev_err(apb->dev, "Couldn't request APB EHB IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = devm_add_action(apb->dev, bt1_apb_clear_irq, apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) dev_err(apb->dev, "Can't add APB EHB IRQs clear action\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Unmask IRQ and clear it' pending flag. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) regmap_update_bits(apb->regs, APB_EHB_ISR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) APB_EHB_ISR_PENDING | APB_EHB_ISR_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) APB_EHB_ISR_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static ssize_t count_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct bt1_apb *apb = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&apb->count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static DEVICE_ATTR_RO(count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct bt1_apb *apb = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) u32 n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) ret = regmap_read(apb->regs, APB_EHB_TIMEOUT, &n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) timeout = bt1_apb_n_to_timeout_us(apb, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return scnprintf(buf, PAGE_SIZE, "%lu\n", timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static ssize_t timeout_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct bt1_apb *apb = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) u32 n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (kstrtoul(buf, 0, &timeout) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) n = bt1_apb_timeout_to_n_us(apb, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) n = clamp(n, APB_EHB_TIMEOUT_MIN, APB_EHB_TIMEOUT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ret = regmap_write(apb->regs, APB_EHB_TIMEOUT, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return ret ?: count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static DEVICE_ATTR_RW(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static ssize_t inject_error_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return scnprintf(buf, PAGE_SIZE, "Error injection: nodev irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static ssize_t inject_error_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) const char *data, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct bt1_apb *apb = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Either dummy read from the unmapped address in the APB IO area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * or manually set the IRQ status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (sysfs_streq(data, "nodev"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) readl(apb->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) else if (sysfs_streq(data, "irq"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_PENDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) APB_EHB_ISR_PENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static DEVICE_ATTR_RW(inject_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static struct attribute *bt1_apb_sysfs_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) &dev_attr_count.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) &dev_attr_timeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) &dev_attr_inject_error.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ATTRIBUTE_GROUPS(bt1_apb_sysfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static void bt1_apb_remove_sysfs(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct bt1_apb *apb = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) device_remove_groups(apb->dev, bt1_apb_sysfs_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int bt1_apb_init_sysfs(struct bt1_apb *apb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ret = device_add_groups(apb->dev, bt1_apb_sysfs_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) dev_err(apb->dev, "Failed to create EHB APB sysfs nodes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ret = devm_add_action_or_reset(apb->dev, bt1_apb_remove_sysfs, apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) dev_err(apb->dev, "Can't add APB EHB sysfs remove action\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int bt1_apb_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct bt1_apb *apb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) apb = bt1_apb_create_data(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (IS_ERR(apb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return PTR_ERR(apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ret = bt1_apb_request_regs(apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) ret = bt1_apb_request_rst(apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ret = bt1_apb_request_clk(apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) ret = bt1_apb_request_irq(apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) ret = bt1_apb_init_sysfs(apb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static const struct of_device_id bt1_apb_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) { .compatible = "baikal,bt1-apb" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) MODULE_DEVICE_TABLE(of, bt1_apb_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static struct platform_driver bt1_apb_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .probe = bt1_apb_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) .name = "bt1-apb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) .of_match_table = bt1_apb_of_match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) module_platform_driver(bt1_apb_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) MODULE_DESCRIPTION("Baikal-T1 APB-bus driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) MODULE_LICENSE("GPL v2");