litex_soc_ctrl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <linux/litex.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/printk.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/reboot.h>
  17. /* reset register located at the base address */
  18. #define RESET_REG_OFF 0x00
  19. #define RESET_REG_VALUE 0x00000001
  20. #define SCRATCH_REG_OFF 0x04
  21. #define SCRATCH_REG_VALUE 0x12345678
  22. #define SCRATCH_TEST_VALUE 0xdeadbeef
  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. reg = litex_read32(reg_addr + SCRATCH_REG_OFF);
  41. if (reg != SCRATCH_REG_VALUE) {
  42. panic("Scratch register read error - the system is probably broken! Expected: 0x%x but got: 0x%lx",
  43. SCRATCH_REG_VALUE, reg);
  44. return -EINVAL;
  45. }
  46. litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_TEST_VALUE);
  47. reg = litex_read32(reg_addr + SCRATCH_REG_OFF);
  48. if (reg != SCRATCH_TEST_VALUE) {
  49. panic("Scratch register write error - the system is probably broken! Expected: 0x%x but got: 0x%lx",
  50. SCRATCH_TEST_VALUE, reg);
  51. return -EINVAL;
  52. }
  53. /* restore original value of the SCRATCH register */
  54. litex_write32(reg_addr + SCRATCH_REG_OFF, SCRATCH_REG_VALUE);
  55. pr_info("LiteX SoC Controller driver initialized");
  56. return 0;
  57. }
  58. struct litex_soc_ctrl_device {
  59. void __iomem *base;
  60. struct notifier_block reset_nb;
  61. };
  62. static int litex_reset_handler(struct notifier_block *this, unsigned long mode,
  63. void *cmd)
  64. {
  65. struct litex_soc_ctrl_device *soc_ctrl_dev =
  66. container_of(this, struct litex_soc_ctrl_device, reset_nb);
  67. litex_write32(soc_ctrl_dev->base + RESET_REG_OFF, RESET_REG_VALUE);
  68. return NOTIFY_DONE;
  69. }
  70. #ifdef CONFIG_OF
  71. static const struct of_device_id litex_soc_ctrl_of_match[] = {
  72. {.compatible = "litex,soc-controller"},
  73. {},
  74. };
  75. MODULE_DEVICE_TABLE(of, litex_soc_ctrl_of_match);
  76. #endif /* CONFIG_OF */
  77. static int litex_soc_ctrl_probe(struct platform_device *pdev)
  78. {
  79. struct litex_soc_ctrl_device *soc_ctrl_dev;
  80. int error;
  81. soc_ctrl_dev = devm_kzalloc(&pdev->dev, sizeof(*soc_ctrl_dev), GFP_KERNEL);
  82. if (!soc_ctrl_dev)
  83. return -ENOMEM;
  84. soc_ctrl_dev->base = devm_platform_ioremap_resource(pdev, 0);
  85. if (IS_ERR(soc_ctrl_dev->base))
  86. return PTR_ERR(soc_ctrl_dev->base);
  87. error = litex_check_csr_access(soc_ctrl_dev->base);
  88. if (error)
  89. return error;
  90. platform_set_drvdata(pdev, soc_ctrl_dev);
  91. soc_ctrl_dev->reset_nb.notifier_call = litex_reset_handler;
  92. soc_ctrl_dev->reset_nb.priority = 128;
  93. error = register_restart_handler(&soc_ctrl_dev->reset_nb);
  94. if (error) {
  95. dev_warn(&pdev->dev, "cannot register restart handler: %d\n",
  96. error);
  97. }
  98. return 0;
  99. }
  100. static int litex_soc_ctrl_remove(struct platform_device *pdev)
  101. {
  102. struct litex_soc_ctrl_device *soc_ctrl_dev = platform_get_drvdata(pdev);
  103. unregister_restart_handler(&soc_ctrl_dev->reset_nb);
  104. return 0;
  105. }
  106. static struct platform_driver litex_soc_ctrl_driver = {
  107. .driver = {
  108. .name = "litex-soc-controller",
  109. .of_match_table = of_match_ptr(litex_soc_ctrl_of_match)
  110. },
  111. .probe = litex_soc_ctrl_probe,
  112. .remove = litex_soc_ctrl_remove,
  113. };
  114. module_platform_driver(litex_soc_ctrl_driver);
  115. MODULE_DESCRIPTION("LiteX SoC Controller driver");
  116. MODULE_AUTHOR("Antmicro <www.antmicro.com>");
  117. MODULE_LICENSE("GPL v2");