123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
- *
- * Authors:
- * Serge Semin <[email protected]>
- *
- * Baikal-T1 AXI-bus driver
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/bitfield.h>
- #include <linux/device.h>
- #include <linux/atomic.h>
- #include <linux/regmap.h>
- #include <linux/platform_device.h>
- #include <linux/mfd/syscon.h>
- #include <linux/interrupt.h>
- #include <linux/io.h>
- #include <linux/nmi.h>
- #include <linux/of.h>
- #include <linux/clk.h>
- #include <linux/reset.h>
- #include <linux/sysfs.h>
- #define BT1_AXI_WERRL 0x110
- #define BT1_AXI_WERRH 0x114
- #define BT1_AXI_WERRH_TYPE BIT(23)
- #define BT1_AXI_WERRH_ADDR_FLD 24
- #define BT1_AXI_WERRH_ADDR_MASK GENMASK(31, BT1_AXI_WERRH_ADDR_FLD)
- /*
- * struct bt1_axi - Baikal-T1 AXI-bus private data
- * @dev: Pointer to the device structure.
- * @qos_regs: AXI Interconnect QoS tuning registers.
- * @sys_regs: Baikal-T1 System Controller registers map.
- * @irq: Errors IRQ number.
- * @aclk: AXI reference clock.
- * @arst: AXI Interconnect reset line.
- * @count: Number of errors detected.
- */
- struct bt1_axi {
- struct device *dev;
- void __iomem *qos_regs;
- struct regmap *sys_regs;
- int irq;
- struct clk *aclk;
- struct reset_control *arst;
- atomic_t count;
- };
- static irqreturn_t bt1_axi_isr(int irq, void *data)
- {
- struct bt1_axi *axi = data;
- u32 low = 0, high = 0;
- regmap_read(axi->sys_regs, BT1_AXI_WERRL, &low);
- regmap_read(axi->sys_regs, BT1_AXI_WERRH, &high);
- dev_crit_ratelimited(axi->dev,
- "AXI-bus fault %d: %s at 0x%x%08x\n",
- atomic_inc_return(&axi->count),
- high & BT1_AXI_WERRH_TYPE ? "no slave" : "slave protocol error",
- high, low);
- /*
- * Print backtrace on each CPU. This might be pointless if the fault
- * has happened on the same CPU as the IRQ handler is executed or
- * the other core proceeded further execution despite the error.
- * But if it's not, by looking at the trace we would get straight to
- * the cause of the problem.
- */
- trigger_all_cpu_backtrace();
- return IRQ_HANDLED;
- }
- static void bt1_axi_clear_data(void *data)
- {
- struct bt1_axi *axi = data;
- struct platform_device *pdev = to_platform_device(axi->dev);
- platform_set_drvdata(pdev, NULL);
- }
- static struct bt1_axi *bt1_axi_create_data(struct platform_device *pdev)
- {
- struct device *dev = &pdev->dev;
- struct bt1_axi *axi;
- int ret;
- axi = devm_kzalloc(dev, sizeof(*axi), GFP_KERNEL);
- if (!axi)
- return ERR_PTR(-ENOMEM);
- ret = devm_add_action(dev, bt1_axi_clear_data, axi);
- if (ret) {
- dev_err(dev, "Can't add AXI EHB data clear action\n");
- return ERR_PTR(ret);
- }
- axi->dev = dev;
- atomic_set(&axi->count, 0);
- platform_set_drvdata(pdev, axi);
- return axi;
- }
- static int bt1_axi_request_regs(struct bt1_axi *axi)
- {
- struct platform_device *pdev = to_platform_device(axi->dev);
- struct device *dev = axi->dev;
- axi->sys_regs = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
- if (IS_ERR(axi->sys_regs)) {
- dev_err(dev, "Couldn't find syscon registers\n");
- return PTR_ERR(axi->sys_regs);
- }
- axi->qos_regs = devm_platform_ioremap_resource_byname(pdev, "qos");
- if (IS_ERR(axi->qos_regs))
- dev_err(dev, "Couldn't map AXI-bus QoS registers\n");
- return PTR_ERR_OR_ZERO(axi->qos_regs);
- }
- static int bt1_axi_request_rst(struct bt1_axi *axi)
- {
- int ret;
- axi->arst = devm_reset_control_get_optional_exclusive(axi->dev, "arst");
- if (IS_ERR(axi->arst))
- return dev_err_probe(axi->dev, PTR_ERR(axi->arst),
- "Couldn't get reset control line\n");
- ret = reset_control_deassert(axi->arst);
- if (ret)
- dev_err(axi->dev, "Failed to deassert the reset line\n");
- return ret;
- }
- static void bt1_axi_disable_clk(void *data)
- {
- struct bt1_axi *axi = data;
- clk_disable_unprepare(axi->aclk);
- }
- static int bt1_axi_request_clk(struct bt1_axi *axi)
- {
- int ret;
- axi->aclk = devm_clk_get(axi->dev, "aclk");
- if (IS_ERR(axi->aclk))
- return dev_err_probe(axi->dev, PTR_ERR(axi->aclk),
- "Couldn't get AXI Interconnect clock\n");
- ret = clk_prepare_enable(axi->aclk);
- if (ret) {
- dev_err(axi->dev, "Couldn't enable the AXI clock\n");
- return ret;
- }
- ret = devm_add_action_or_reset(axi->dev, bt1_axi_disable_clk, axi);
- if (ret)
- dev_err(axi->dev, "Can't add AXI clock disable action\n");
- return ret;
- }
- static int bt1_axi_request_irq(struct bt1_axi *axi)
- {
- struct platform_device *pdev = to_platform_device(axi->dev);
- int ret;
- axi->irq = platform_get_irq(pdev, 0);
- if (axi->irq < 0)
- return axi->irq;
- ret = devm_request_irq(axi->dev, axi->irq, bt1_axi_isr, IRQF_SHARED,
- "bt1-axi", axi);
- if (ret)
- dev_err(axi->dev, "Couldn't request AXI EHB IRQ\n");
- return ret;
- }
- static ssize_t count_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct bt1_axi *axi = dev_get_drvdata(dev);
- return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&axi->count));
- }
- static DEVICE_ATTR_RO(count);
- static ssize_t inject_error_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- return scnprintf(buf, PAGE_SIZE, "Error injection: bus unaligned\n");
- }
- static ssize_t inject_error_store(struct device *dev,
- struct device_attribute *attr,
- const char *data, size_t count)
- {
- struct bt1_axi *axi = dev_get_drvdata(dev);
- /*
- * Performing unaligned read from the memory will cause the CM2 bus
- * error while unaligned writing - the AXI bus write error handled
- * by this driver.
- */
- if (sysfs_streq(data, "bus"))
- readb(axi->qos_regs);
- else if (sysfs_streq(data, "unaligned"))
- writeb(0, axi->qos_regs);
- else
- return -EINVAL;
- return count;
- }
- static DEVICE_ATTR_RW(inject_error);
- static struct attribute *bt1_axi_sysfs_attrs[] = {
- &dev_attr_count.attr,
- &dev_attr_inject_error.attr,
- NULL
- };
- ATTRIBUTE_GROUPS(bt1_axi_sysfs);
- static void bt1_axi_remove_sysfs(void *data)
- {
- struct bt1_axi *axi = data;
- device_remove_groups(axi->dev, bt1_axi_sysfs_groups);
- }
- static int bt1_axi_init_sysfs(struct bt1_axi *axi)
- {
- int ret;
- ret = device_add_groups(axi->dev, bt1_axi_sysfs_groups);
- if (ret) {
- dev_err(axi->dev, "Failed to add sysfs files group\n");
- return ret;
- }
- ret = devm_add_action_or_reset(axi->dev, bt1_axi_remove_sysfs, axi);
- if (ret)
- dev_err(axi->dev, "Can't add AXI EHB sysfs remove action\n");
- return ret;
- }
- static int bt1_axi_probe(struct platform_device *pdev)
- {
- struct bt1_axi *axi;
- int ret;
- axi = bt1_axi_create_data(pdev);
- if (IS_ERR(axi))
- return PTR_ERR(axi);
- ret = bt1_axi_request_regs(axi);
- if (ret)
- return ret;
- ret = bt1_axi_request_rst(axi);
- if (ret)
- return ret;
- ret = bt1_axi_request_clk(axi);
- if (ret)
- return ret;
- ret = bt1_axi_request_irq(axi);
- if (ret)
- return ret;
- ret = bt1_axi_init_sysfs(axi);
- if (ret)
- return ret;
- return 0;
- }
- static const struct of_device_id bt1_axi_of_match[] = {
- { .compatible = "baikal,bt1-axi" },
- { }
- };
- MODULE_DEVICE_TABLE(of, bt1_axi_of_match);
- static struct platform_driver bt1_axi_driver = {
- .probe = bt1_axi_probe,
- .driver = {
- .name = "bt1-axi",
- .of_match_table = bt1_axi_of_match
- }
- };
- module_platform_driver(bt1_axi_driver);
- MODULE_AUTHOR("Serge Semin <[email protected]>");
- MODULE_DESCRIPTION("Baikal-T1 AXI-bus driver");
- MODULE_LICENSE("GPL v2");
|