Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
0004  *
0005  * Authors:
0006  *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
0007  *
0008  * Baikal-T1 APB-bus driver
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/types.h>
0014 #include <linux/device.h>
0015 #include <linux/atomic.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/io.h>
0019 #include <linux/nmi.h>
0020 #include <linux/of.h>
0021 #include <linux/regmap.h>
0022 #include <linux/clk.h>
0023 #include <linux/reset.h>
0024 #include <linux/time64.h>
0025 #include <linux/clk.h>
0026 #include <linux/sysfs.h>
0027 
0028 #define APB_EHB_ISR         0x00
0029 #define APB_EHB_ISR_PENDING     BIT(0)
0030 #define APB_EHB_ISR_MASK        BIT(1)
0031 #define APB_EHB_ADDR            0x04
0032 #define APB_EHB_TIMEOUT         0x08
0033 
0034 #define APB_EHB_TIMEOUT_MIN     0x000003FFU
0035 #define APB_EHB_TIMEOUT_MAX     0xFFFFFFFFU
0036 
0037 /*
0038  * struct bt1_apb - Baikal-T1 APB EHB private data
0039  * @dev: Pointer to the device structure.
0040  * @regs: APB EHB registers map.
0041  * @res: No-device error injection memory region.
0042  * @irq: Errors IRQ number.
0043  * @rate: APB-bus reference clock rate.
0044  * @pclk: APB-reference clock.
0045  * @prst: APB domain reset line.
0046  * @count: Number of errors detected.
0047  */
0048 struct bt1_apb {
0049     struct device *dev;
0050 
0051     struct regmap *regs;
0052     void __iomem *res;
0053     int irq;
0054 
0055     unsigned long rate;
0056     struct clk *pclk;
0057 
0058     struct reset_control *prst;
0059 
0060     atomic_t count;
0061 };
0062 
0063 static const struct regmap_config bt1_apb_regmap_cfg = {
0064     .reg_bits = 32,
0065     .val_bits = 32,
0066     .reg_stride = 4,
0067     .max_register = APB_EHB_TIMEOUT,
0068     .fast_io = true
0069 };
0070 
0071 static inline unsigned long bt1_apb_n_to_timeout_us(struct bt1_apb *apb, u32 n)
0072 {
0073     u64 timeout = (u64)n * USEC_PER_SEC;
0074 
0075     do_div(timeout, apb->rate);
0076 
0077     return timeout;
0078 
0079 }
0080 
0081 static inline unsigned long bt1_apb_timeout_to_n_us(struct bt1_apb *apb,
0082                             unsigned long timeout)
0083 {
0084     u64 n = (u64)timeout * apb->rate;
0085 
0086     do_div(n, USEC_PER_SEC);
0087 
0088     return n;
0089 
0090 }
0091 
0092 static irqreturn_t bt1_apb_isr(int irq, void *data)
0093 {
0094     struct bt1_apb *apb = data;
0095     u32 addr = 0;
0096 
0097     regmap_read(apb->regs, APB_EHB_ADDR, &addr);
0098 
0099     dev_crit_ratelimited(apb->dev,
0100         "APB-bus fault %d: Slave access timeout at 0x%08x\n",
0101         atomic_inc_return(&apb->count),
0102         addr);
0103 
0104     /*
0105      * Print backtrace on each CPU. This might be pointless if the fault
0106      * has happened on the same CPU as the IRQ handler is executed or
0107      * the other core proceeded further execution despite the error.
0108      * But if it's not, by looking at the trace we would get straight to
0109      * the cause of the problem.
0110      */
0111     trigger_all_cpu_backtrace();
0112 
0113     regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_PENDING, 0);
0114 
0115     return IRQ_HANDLED;
0116 }
0117 
0118 static void bt1_apb_clear_data(void *data)
0119 {
0120     struct bt1_apb *apb = data;
0121     struct platform_device *pdev = to_platform_device(apb->dev);
0122 
0123     platform_set_drvdata(pdev, NULL);
0124 }
0125 
0126 static struct bt1_apb *bt1_apb_create_data(struct platform_device *pdev)
0127 {
0128     struct device *dev = &pdev->dev;
0129     struct bt1_apb *apb;
0130     int ret;
0131 
0132     apb = devm_kzalloc(dev, sizeof(*apb), GFP_KERNEL);
0133     if (!apb)
0134         return ERR_PTR(-ENOMEM);
0135 
0136     ret = devm_add_action(dev, bt1_apb_clear_data, apb);
0137     if (ret) {
0138         dev_err(dev, "Can't add APB EHB data clear action\n");
0139         return ERR_PTR(ret);
0140     }
0141 
0142     apb->dev = dev;
0143     atomic_set(&apb->count, 0);
0144     platform_set_drvdata(pdev, apb);
0145 
0146     return apb;
0147 }
0148 
0149 static int bt1_apb_request_regs(struct bt1_apb *apb)
0150 {
0151     struct platform_device *pdev = to_platform_device(apb->dev);
0152     void __iomem *regs;
0153 
0154     regs = devm_platform_ioremap_resource_byname(pdev, "ehb");
0155     if (IS_ERR(regs)) {
0156         dev_err(apb->dev, "Couldn't map APB EHB registers\n");
0157         return PTR_ERR(regs);
0158     }
0159 
0160     apb->regs = devm_regmap_init_mmio(apb->dev, regs, &bt1_apb_regmap_cfg);
0161     if (IS_ERR(apb->regs)) {
0162         dev_err(apb->dev, "Couldn't create APB EHB regmap\n");
0163         return PTR_ERR(apb->regs);
0164     }
0165 
0166     apb->res = devm_platform_ioremap_resource_byname(pdev, "nodev");
0167     if (IS_ERR(apb->res))
0168         dev_err(apb->dev, "Couldn't map reserved region\n");
0169 
0170     return PTR_ERR_OR_ZERO(apb->res);
0171 }
0172 
0173 static int bt1_apb_request_rst(struct bt1_apb *apb)
0174 {
0175     int ret;
0176 
0177     apb->prst = devm_reset_control_get_optional_exclusive(apb->dev, "prst");
0178     if (IS_ERR(apb->prst))
0179         return dev_err_probe(apb->dev, PTR_ERR(apb->prst),
0180                      "Couldn't get reset control line\n");
0181 
0182     ret = reset_control_deassert(apb->prst);
0183     if (ret)
0184         dev_err(apb->dev, "Failed to deassert the reset line\n");
0185 
0186     return ret;
0187 }
0188 
0189 static void bt1_apb_disable_clk(void *data)
0190 {
0191     struct bt1_apb *apb = data;
0192 
0193     clk_disable_unprepare(apb->pclk);
0194 }
0195 
0196 static int bt1_apb_request_clk(struct bt1_apb *apb)
0197 {
0198     int ret;
0199 
0200     apb->pclk = devm_clk_get(apb->dev, "pclk");
0201     if (IS_ERR(apb->pclk))
0202         return dev_err_probe(apb->dev, PTR_ERR(apb->pclk),
0203                      "Couldn't get APB clock descriptor\n");
0204 
0205     ret = clk_prepare_enable(apb->pclk);
0206     if (ret) {
0207         dev_err(apb->dev, "Couldn't enable the APB clock\n");
0208         return ret;
0209     }
0210 
0211     ret = devm_add_action_or_reset(apb->dev, bt1_apb_disable_clk, apb);
0212     if (ret) {
0213         dev_err(apb->dev, "Can't add APB EHB clocks disable action\n");
0214         return ret;
0215     }
0216 
0217     apb->rate = clk_get_rate(apb->pclk);
0218     if (!apb->rate) {
0219         dev_err(apb->dev, "Invalid clock rate\n");
0220         return -EINVAL;
0221     }
0222 
0223     return 0;
0224 }
0225 
0226 static void bt1_apb_clear_irq(void *data)
0227 {
0228     struct bt1_apb *apb = data;
0229 
0230     regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_MASK, 0);
0231 }
0232 
0233 static int bt1_apb_request_irq(struct bt1_apb *apb)
0234 {
0235     struct platform_device *pdev = to_platform_device(apb->dev);
0236     int ret;
0237 
0238     apb->irq = platform_get_irq(pdev, 0);
0239     if (apb->irq < 0)
0240         return apb->irq;
0241 
0242     ret = devm_request_irq(apb->dev, apb->irq, bt1_apb_isr, IRQF_SHARED,
0243                    "bt1-apb", apb);
0244     if (ret) {
0245         dev_err(apb->dev, "Couldn't request APB EHB IRQ\n");
0246         return ret;
0247     }
0248 
0249     ret = devm_add_action(apb->dev, bt1_apb_clear_irq, apb);
0250     if (ret) {
0251         dev_err(apb->dev, "Can't add APB EHB IRQs clear action\n");
0252         return ret;
0253     }
0254 
0255     /* Unmask IRQ and clear it' pending flag. */
0256     regmap_update_bits(apb->regs, APB_EHB_ISR,
0257                APB_EHB_ISR_PENDING | APB_EHB_ISR_MASK,
0258                APB_EHB_ISR_MASK);
0259 
0260     return 0;
0261 }
0262 
0263 static ssize_t count_show(struct device *dev, struct device_attribute *attr,
0264               char *buf)
0265 {
0266     struct bt1_apb *apb = dev_get_drvdata(dev);
0267 
0268     return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&apb->count));
0269 }
0270 static DEVICE_ATTR_RO(count);
0271 
0272 static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
0273                 char *buf)
0274 {
0275     struct bt1_apb *apb = dev_get_drvdata(dev);
0276     unsigned long timeout;
0277     int ret;
0278     u32 n;
0279 
0280     ret = regmap_read(apb->regs, APB_EHB_TIMEOUT, &n);
0281     if (ret)
0282         return ret;
0283 
0284     timeout = bt1_apb_n_to_timeout_us(apb, n);
0285 
0286     return scnprintf(buf, PAGE_SIZE, "%lu\n", timeout);
0287 }
0288 
0289 static ssize_t timeout_store(struct device *dev,
0290                  struct device_attribute *attr,
0291                  const char *buf, size_t count)
0292 {
0293     struct bt1_apb *apb = dev_get_drvdata(dev);
0294     unsigned long timeout;
0295     int ret;
0296     u32 n;
0297 
0298     if (kstrtoul(buf, 0, &timeout) < 0)
0299         return -EINVAL;
0300 
0301     n = bt1_apb_timeout_to_n_us(apb, timeout);
0302     n = clamp(n, APB_EHB_TIMEOUT_MIN, APB_EHB_TIMEOUT_MAX);
0303 
0304     ret = regmap_write(apb->regs, APB_EHB_TIMEOUT, n);
0305 
0306     return ret ?: count;
0307 }
0308 static DEVICE_ATTR_RW(timeout);
0309 
0310 static ssize_t inject_error_show(struct device *dev,
0311                  struct device_attribute *attr, char *buf)
0312 {
0313     return scnprintf(buf, PAGE_SIZE, "Error injection: nodev irq\n");
0314 }
0315 
0316 static ssize_t inject_error_store(struct device *dev,
0317                   struct device_attribute *attr,
0318                   const char *data, size_t count)
0319 {
0320     struct bt1_apb *apb = dev_get_drvdata(dev);
0321 
0322     /*
0323      * Either dummy read from the unmapped address in the APB IO area
0324      * or manually set the IRQ status.
0325      */
0326     if (sysfs_streq(data, "nodev"))
0327         readl(apb->res);
0328     else if (sysfs_streq(data, "irq"))
0329         regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_PENDING,
0330                    APB_EHB_ISR_PENDING);
0331     else
0332         return -EINVAL;
0333 
0334     return count;
0335 }
0336 static DEVICE_ATTR_RW(inject_error);
0337 
0338 static struct attribute *bt1_apb_sysfs_attrs[] = {
0339     &dev_attr_count.attr,
0340     &dev_attr_timeout.attr,
0341     &dev_attr_inject_error.attr,
0342     NULL
0343 };
0344 ATTRIBUTE_GROUPS(bt1_apb_sysfs);
0345 
0346 static void bt1_apb_remove_sysfs(void *data)
0347 {
0348     struct bt1_apb *apb = data;
0349 
0350     device_remove_groups(apb->dev, bt1_apb_sysfs_groups);
0351 }
0352 
0353 static int bt1_apb_init_sysfs(struct bt1_apb *apb)
0354 {
0355     int ret;
0356 
0357     ret = device_add_groups(apb->dev, bt1_apb_sysfs_groups);
0358     if (ret) {
0359         dev_err(apb->dev, "Failed to create EHB APB sysfs nodes\n");
0360         return ret;
0361     }
0362 
0363     ret = devm_add_action_or_reset(apb->dev, bt1_apb_remove_sysfs, apb);
0364     if (ret)
0365         dev_err(apb->dev, "Can't add APB EHB sysfs remove action\n");
0366 
0367     return ret;
0368 }
0369 
0370 static int bt1_apb_probe(struct platform_device *pdev)
0371 {
0372     struct bt1_apb *apb;
0373     int ret;
0374 
0375     apb = bt1_apb_create_data(pdev);
0376     if (IS_ERR(apb))
0377         return PTR_ERR(apb);
0378 
0379     ret = bt1_apb_request_regs(apb);
0380     if (ret)
0381         return ret;
0382 
0383     ret = bt1_apb_request_rst(apb);
0384     if (ret)
0385         return ret;
0386 
0387     ret = bt1_apb_request_clk(apb);
0388     if (ret)
0389         return ret;
0390 
0391     ret = bt1_apb_request_irq(apb);
0392     if (ret)
0393         return ret;
0394 
0395     ret = bt1_apb_init_sysfs(apb);
0396     if (ret)
0397         return ret;
0398 
0399     return 0;
0400 }
0401 
0402 static const struct of_device_id bt1_apb_of_match[] = {
0403     { .compatible = "baikal,bt1-apb" },
0404     { }
0405 };
0406 MODULE_DEVICE_TABLE(of, bt1_apb_of_match);
0407 
0408 static struct platform_driver bt1_apb_driver = {
0409     .probe = bt1_apb_probe,
0410     .driver = {
0411         .name = "bt1-apb",
0412         .of_match_table = bt1_apb_of_match
0413     }
0414 };
0415 module_platform_driver(bt1_apb_driver);
0416 
0417 MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
0418 MODULE_DESCRIPTION("Baikal-T1 APB-bus driver");
0419 MODULE_LICENSE("GPL v2");