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 #include <linux/reboot.h> 19 20 /* reset register located at the base address */ 21 #define RESET_REG_OFF 0x00 22 #define RESET_REG_VALUE 0x00000001 23 24 #define SCRATCH_REG_OFF 0x04 25 #define SCRATCH_REG_VALUE 0x12345678 26 #define SCRATCH_TEST_VALUE 0xdeadbeef 27 28 /* 29 * Check LiteX CSR read/write access 30 * 31 * This function reads and writes a scratch register in order to verify if CSR 32 * access works. 33 * 34 * In case any problems are detected, the driver should panic. 35 * 36 * Access to the LiteX CSR is, by design, done in CPU native endianness. 37 * The driver should not dynamically configure access functions when 38 * the endianness mismatch is detected. Such situation indicates problems in 39 * the soft SoC design and should be solved at the LiteX generator level, 40 * not in the software. 41 */ 42 static int litex_check_csr_access(void __iomem *reg_addr) 43 { 44 unsigned long reg; 45 46 reg = litex_read32(reg_addr + SCRATCH_REG_OFF); 47 48 if (reg != SCRATCH_REG_VALUE) { 49 panic("Scratch register read error - the system is probably broken! Expected: 0x%x but got: 0x%lx", 50 SCRATCH_REG_VALUE, reg); 51 return -EINVAL; 52 } 53 54 litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_TEST_VALUE); 55 reg = litex_read32(reg_addr + SCRATCH_REG_OFF); 56 57 if (reg != SCRATCH_TEST_VALUE) { 58 panic("Scratch register write error - the system is probably broken! Expected: 0x%x but got: 0x%lx", 59 SCRATCH_TEST_VALUE, reg); 60 return -EINVAL; 61 } 62 63 /* restore original value of the SCRATCH register */ 64 litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_VALUE); 65 66 pr_info("LiteX SoC Controller driver initialized: subreg:%d, align:%d", 67 LITEX_SUBREG_SIZE, LITEX_SUBREG_ALIGN); 68 69 return 0; 70 } 71 72 struct litex_soc_ctrl_device { 73 void __iomem *base; 74 struct notifier_block reset_nb; 75 }; 76 77 static int litex_reset_handler(struct notifier_block *this, unsigned long mode, 78 void *cmd) 79 { 80 struct litex_soc_ctrl_device *soc_ctrl_dev = 81 container_of(this, struct litex_soc_ctrl_device, reset_nb); 82 83 litex_write32(soc_ctrl_dev->base + RESET_REG_OFF, RESET_REG_VALUE); 84 return NOTIFY_DONE; 85 } 86 87 #ifdef CONFIG_OF 88 static const struct of_device_id litex_soc_ctrl_of_match[] = { 89 {.compatible = "litex,soc-controller"}, 90 {}, 91 }; 92 MODULE_DEVICE_TABLE(of, litex_soc_ctrl_of_match); 93 #endif /* CONFIG_OF */ 94 95 static int litex_soc_ctrl_probe(struct platform_device *pdev) 96 { 97 struct litex_soc_ctrl_device *soc_ctrl_dev; 98 int error; 99 100 soc_ctrl_dev = devm_kzalloc(&pdev->dev, sizeof(*soc_ctrl_dev), GFP_KERNEL); 101 if (!soc_ctrl_dev) 102 return -ENOMEM; 103 104 soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0); 105 if (IS_ERR(soc_ctrl_dev->base)) 106 return PTR_ERR(soc_ctrl_dev->base); 107 108 error = litex_check_csr_access(soc_ctrl_dev->base); 109 if (error) 110 return error; 111 112 platform_set_drvdata(pdev, soc_ctrl_dev); 113 114 soc_ctrl_dev->reset_nb.notifier_call = litex_reset_handler; 115 soc_ctrl_dev->reset_nb.priority = 128; 116 error = register_restart_handler(&soc_ctrl_dev->reset_nb); 117 if (error) { 118 dev_warn(&pdev->dev, "cannot register restart handler: %d\n", 119 error); 120 } 121 122 return 0; 123 } 124 125 static int litex_soc_ctrl_remove(struct platform_device *pdev) 126 { 127 struct litex_soc_ctrl_device *soc_ctrl_dev = platform_get_drvdata(pdev); 128 129 unregister_restart_handler(&soc_ctrl_dev->reset_nb); 130 return 0; 131 } 132 133 static struct platform_driver litex_soc_ctrl_driver = { 134 .driver = { 135 .name = "litex-soc-controller", 136 .of_match_table = of_match_ptr(litex_soc_ctrl_of_match) 137 }, 138 .probe = litex_soc_ctrl_probe, 139 .remove = litex_soc_ctrl_remove, 140 }; 141 142 module_platform_driver(litex_soc_ctrl_driver); 143 MODULE_DESCRIPTION("LiteX SoC Controller driver"); 144 MODULE_AUTHOR("Antmicro <www.antmicro.com>"); 145 MODULE_LICENSE("GPL v2"); 146