Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * LiteX SoC Controller Driver
0004  *
0005  * Copyright (C) 2020 Antmicro <www.antmicro.com>
0006  *
0007  */
0008 
0009 #include <linux/litex.h>
0010 #include <linux/device.h>
0011 #include <linux/errno.h>
0012 #include <linux/of.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/printk.h>
0015 #include <linux/module.h>
0016 #include <linux/io.h>
0017 #include <linux/reboot.h>
0018 
0019 /* reset register located at the base address */
0020 #define RESET_REG_OFF           0x00
0021 #define RESET_REG_VALUE         0x00000001
0022 
0023 #define SCRATCH_REG_OFF         0x04
0024 #define SCRATCH_REG_VALUE       0x12345678
0025 #define SCRATCH_TEST_VALUE      0xdeadbeef
0026 
0027 /*
0028  * Check LiteX CSR read/write access
0029  *
0030  * This function reads and writes a scratch register in order to verify if CSR
0031  * access works.
0032  *
0033  * In case any problems are detected, the driver should panic.
0034  *
0035  * Access to the LiteX CSR is, by design, done in CPU native endianness.
0036  * The driver should not dynamically configure access functions when
0037  * the endianness mismatch is detected. Such situation indicates problems in
0038  * the soft SoC design and should be solved at the LiteX generator level,
0039  * not in the software.
0040  */
0041 static int litex_check_csr_access(void __iomem *reg_addr)
0042 {
0043     unsigned long reg;
0044 
0045     reg = litex_read32(reg_addr + SCRATCH_REG_OFF);
0046 
0047     if (reg != SCRATCH_REG_VALUE) {
0048         panic("Scratch register read error - the system is probably broken! Expected: 0x%x but got: 0x%lx",
0049             SCRATCH_REG_VALUE, reg);
0050         return -EINVAL;
0051     }
0052 
0053     litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_TEST_VALUE);
0054     reg = litex_read32(reg_addr + SCRATCH_REG_OFF);
0055 
0056     if (reg != SCRATCH_TEST_VALUE) {
0057         panic("Scratch register write error - the system is probably broken! Expected: 0x%x but got: 0x%lx",
0058             SCRATCH_TEST_VALUE, reg);
0059         return -EINVAL;
0060     }
0061 
0062     /* restore original value of the SCRATCH register */
0063     litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_VALUE);
0064 
0065     pr_info("LiteX SoC Controller driver initialized");
0066 
0067     return 0;
0068 }
0069 
0070 struct litex_soc_ctrl_device {
0071     void __iomem *base;
0072     struct notifier_block reset_nb;
0073 };
0074 
0075 static int litex_reset_handler(struct notifier_block *this, unsigned long mode,
0076                    void *cmd)
0077 {
0078     struct litex_soc_ctrl_device *soc_ctrl_dev =
0079         container_of(this, struct litex_soc_ctrl_device, reset_nb);
0080 
0081     litex_write32(soc_ctrl_dev->base + RESET_REG_OFF, RESET_REG_VALUE);
0082     return NOTIFY_DONE;
0083 }
0084 
0085 #ifdef CONFIG_OF
0086 static const struct of_device_id litex_soc_ctrl_of_match[] = {
0087     {.compatible = "litex,soc-controller"},
0088     {},
0089 };
0090 MODULE_DEVICE_TABLE(of, litex_soc_ctrl_of_match);
0091 #endif /* CONFIG_OF */
0092 
0093 static int litex_soc_ctrl_probe(struct platform_device *pdev)
0094 {
0095     struct litex_soc_ctrl_device *soc_ctrl_dev;
0096     int error;
0097 
0098     soc_ctrl_dev = devm_kzalloc(&pdev->dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
0099     if (!soc_ctrl_dev)
0100         return -ENOMEM;
0101 
0102     soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);
0103     if (IS_ERR(soc_ctrl_dev->base))
0104         return PTR_ERR(soc_ctrl_dev->base);
0105 
0106     error = litex_check_csr_access(soc_ctrl_dev->base);
0107     if (error)
0108         return error;
0109 
0110     platform_set_drvdata(pdev, soc_ctrl_dev);
0111 
0112     soc_ctrl_dev->reset_nb.notifier_call = litex_reset_handler;
0113     soc_ctrl_dev->reset_nb.priority = 128;
0114     error = register_restart_handler(&soc_ctrl_dev->reset_nb);
0115     if (error) {
0116         dev_warn(&pdev->dev, "cannot register restart handler: %d\n",
0117              error);
0118     }
0119 
0120     return 0;
0121 }
0122 
0123 static int litex_soc_ctrl_remove(struct platform_device *pdev)
0124 {
0125     struct litex_soc_ctrl_device *soc_ctrl_dev = platform_get_drvdata(pdev);
0126 
0127     unregister_restart_handler(&soc_ctrl_dev->reset_nb);
0128     return 0;
0129 }
0130 
0131 static struct platform_driver litex_soc_ctrl_driver = {
0132     .driver = {
0133         .name = "litex-soc-controller",
0134         .of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
0135     },
0136     .probe = litex_soc_ctrl_probe,
0137     .remove = litex_soc_ctrl_remove,
0138 };
0139 
0140 module_platform_driver(litex_soc_ctrl_driver);
0141 MODULE_DESCRIPTION("LiteX SoC Controller driver");
0142 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
0143 MODULE_LICENSE("GPL v2");