ocelot-reset.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Microsemi MIPS SoC reset driver
  4. *
  5. * License: Dual MIT/GPL
  6. * Copyright (c) 2017 Microsemi Corporation
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/notifier.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/reboot.h>
  16. #include <linux/regmap.h>
  17. struct reset_props {
  18. const char *syscon;
  19. u32 protect_reg;
  20. u32 vcore_protect;
  21. u32 if_si_owner_bit;
  22. };
  23. struct ocelot_reset_context {
  24. void __iomem *base;
  25. struct regmap *cpu_ctrl;
  26. const struct reset_props *props;
  27. struct notifier_block restart_handler;
  28. };
  29. #define BIT_OFF_INVALID 32
  30. #define SOFT_CHIP_RST BIT(0)
  31. #define ICPU_CFG_CPU_SYSTEM_CTRL_GENERAL_CTRL 0x24
  32. #define IF_SI_OWNER_MASK GENMASK(1, 0)
  33. #define IF_SI_OWNER_SISL 0
  34. #define IF_SI_OWNER_SIBM 1
  35. #define IF_SI_OWNER_SIMC 2
  36. static int ocelot_restart_handle(struct notifier_block *this,
  37. unsigned long mode, void *cmd)
  38. {
  39. struct ocelot_reset_context *ctx = container_of(this, struct
  40. ocelot_reset_context,
  41. restart_handler);
  42. u32 if_si_owner_bit = ctx->props->if_si_owner_bit;
  43. /* Make sure the core is not protected from reset */
  44. regmap_update_bits(ctx->cpu_ctrl, ctx->props->protect_reg,
  45. ctx->props->vcore_protect, 0);
  46. /* Make the SI back to boot mode */
  47. if (if_si_owner_bit != BIT_OFF_INVALID)
  48. regmap_update_bits(ctx->cpu_ctrl,
  49. ICPU_CFG_CPU_SYSTEM_CTRL_GENERAL_CTRL,
  50. IF_SI_OWNER_MASK << if_si_owner_bit,
  51. IF_SI_OWNER_SIBM << if_si_owner_bit);
  52. pr_emerg("Resetting SoC\n");
  53. writel(SOFT_CHIP_RST, ctx->base);
  54. pr_emerg("Unable to restart system\n");
  55. return NOTIFY_DONE;
  56. }
  57. static int ocelot_reset_probe(struct platform_device *pdev)
  58. {
  59. struct ocelot_reset_context *ctx;
  60. struct device *dev = &pdev->dev;
  61. int err;
  62. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  63. if (!ctx)
  64. return -ENOMEM;
  65. ctx->base = devm_platform_ioremap_resource(pdev, 0);
  66. if (IS_ERR(ctx->base))
  67. return PTR_ERR(ctx->base);
  68. ctx->props = device_get_match_data(dev);
  69. ctx->cpu_ctrl = syscon_regmap_lookup_by_compatible(ctx->props->syscon);
  70. if (IS_ERR(ctx->cpu_ctrl)) {
  71. dev_err(dev, "No syscon map: %s\n", ctx->props->syscon);
  72. return PTR_ERR(ctx->cpu_ctrl);
  73. }
  74. ctx->restart_handler.notifier_call = ocelot_restart_handle;
  75. ctx->restart_handler.priority = 192;
  76. err = register_restart_handler(&ctx->restart_handler);
  77. if (err)
  78. dev_err(dev, "can't register restart notifier (err=%d)\n", err);
  79. return err;
  80. }
  81. static const struct reset_props reset_props_jaguar2 = {
  82. .syscon = "mscc,ocelot-cpu-syscon",
  83. .protect_reg = 0x20,
  84. .vcore_protect = BIT(2),
  85. .if_si_owner_bit = 6,
  86. };
  87. static const struct reset_props reset_props_luton = {
  88. .syscon = "mscc,ocelot-cpu-syscon",
  89. .protect_reg = 0x20,
  90. .vcore_protect = BIT(2),
  91. .if_si_owner_bit = BIT_OFF_INVALID, /* n/a */
  92. };
  93. static const struct reset_props reset_props_ocelot = {
  94. .syscon = "mscc,ocelot-cpu-syscon",
  95. .protect_reg = 0x20,
  96. .vcore_protect = BIT(2),
  97. .if_si_owner_bit = 4,
  98. };
  99. static const struct reset_props reset_props_sparx5 = {
  100. .syscon = "microchip,sparx5-cpu-syscon",
  101. .protect_reg = 0x84,
  102. .vcore_protect = BIT(10),
  103. .if_si_owner_bit = 6,
  104. };
  105. static const struct of_device_id ocelot_reset_of_match[] = {
  106. {
  107. .compatible = "mscc,jaguar2-chip-reset",
  108. .data = &reset_props_jaguar2
  109. }, {
  110. .compatible = "mscc,luton-chip-reset",
  111. .data = &reset_props_luton
  112. }, {
  113. .compatible = "mscc,ocelot-chip-reset",
  114. .data = &reset_props_ocelot
  115. }, {
  116. .compatible = "microchip,sparx5-chip-reset",
  117. .data = &reset_props_sparx5
  118. },
  119. { /*sentinel*/ }
  120. };
  121. static struct platform_driver ocelot_reset_driver = {
  122. .probe = ocelot_reset_probe,
  123. .driver = {
  124. .name = "ocelot-chip-reset",
  125. .of_match_table = ocelot_reset_of_match,
  126. },
  127. };
  128. builtin_platform_driver(ocelot_reset_driver);