1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * LiteX SoC Controller Driver 4 * 5 * Copyright (C) 2020 Antmicro <www.antmicro.com> 6 * 7 */ 8 9 #include <linux/litex.h> 10 #include <linux/device.h> 11 #include <linux/errno.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/printk.h> 15 #include <linux/module.h> 16 #include <linux/errno.h> 17 #include <linux/io.h> 18 19 #define SCRATCH_REG_OFF 0x04 20 #define SCRATCH_REG_VALUE 0x12345678 21 #define SCRATCH_TEST_VALUE 0xdeadbeef 22 23 /* 24 * Check LiteX CSR read/write access 25 * 26 * This function reads and writes a scratch register in order to verify if CSR 27 * access works. 28 * 29 * In case any problems are detected, the driver should panic. 30 * 31 * Access to the LiteX CSR is, by design, done in CPU native endianness. 32 * The driver should not dynamically configure access functions when 33 * the endianness mismatch is detected. Such situation indicates problems in 34 * the soft SoC design and should be solved at the LiteX generator level, 35 * not in the software. 36 */ 37 static int litex_check_csr_access(void __iomem *reg_addr) 38 { 39 unsigned long reg; 40 41 reg = litex_read32(reg_addr + SCRATCH_REG_OFF); 42 43 if (reg != SCRATCH_REG_VALUE) { 44 panic("Scratch register read error - the system is probably broken! Expected: 0x%x but got: 0x%lx", 45 SCRATCH_REG_VALUE, reg); 46 return -EINVAL; 47 } 48 49 litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_TEST_VALUE); 50 reg = litex_read32(reg_addr + SCRATCH_REG_OFF); 51 52 if (reg != SCRATCH_TEST_VALUE) { 53 panic("Scratch register write error - the system is probably broken! Expected: 0x%x but got: 0x%lx", 54 SCRATCH_TEST_VALUE, reg); 55 return -EINVAL; 56 } 57 58 /* restore original value of the SCRATCH register */ 59 litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_VALUE); 60 61 pr_info("LiteX SoC Controller driver initialized"); 62 63 return 0; 64 } 65 66 struct litex_soc_ctrl_device { 67 void __iomem *base; 68 }; 69 70 static const struct of_device_id litex_soc_ctrl_of_match[] = { 71 {.compatible = "litex,soc-controller"}, 72 {}, 73 }; 74 75 MODULE_DEVICE_TABLE(of, litex_soc_ctrl_of_match); 76 77 static int litex_soc_ctrl_probe(struct platform_device *pdev) 78 { 79 struct litex_soc_ctrl_device *soc_ctrl_dev; 80 81 soc_ctrl_dev = devm_kzalloc(&pdev->dev, sizeof(*soc_ctrl_dev), GFP_KERNEL); 82 if (!soc_ctrl_dev) 83 return -ENOMEM; 84 85 soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0); 86 if (IS_ERR(soc_ctrl_dev->base)) 87 return PTR_ERR(soc_ctrl_dev->base); 88 89 return litex_check_csr_access(soc_ctrl_dev->base); 90 } 91 92 static struct platform_driver litex_soc_ctrl_driver = { 93 .driver = { 94 .name = "litex-soc-controller", 95 .of_match_table = of_match_ptr(litex_soc_ctrl_of_match) 96 }, 97 .probe = litex_soc_ctrl_probe, 98 }; 99 100 module_platform_driver(litex_soc_ctrl_driver); 101 MODULE_DESCRIPTION("LiteX SoC Controller driver"); 102 MODULE_AUTHOR("Antmicro <www.antmicro.com>"); 103 MODULE_LICENSE("GPL v2"); 104