ocelot-reset.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/mfd/syscon.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.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 resource *res;
  61. struct device *dev = &pdev->dev;
  62. int err;
  63. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  64. if (!ctx)
  65. return -ENOMEM;
  66. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  67. ctx->base = devm_ioremap_resource(dev, res);
  68. if (IS_ERR(ctx->base))
  69. return PTR_ERR(ctx->base);
  70. ctx->props = device_get_match_data(dev);
  71. ctx->cpu_ctrl = syscon_regmap_lookup_by_compatible(ctx->props->syscon);
  72. if (IS_ERR(ctx->cpu_ctrl)) {
  73. dev_err(dev, "No syscon map: %s\n", ctx->props->syscon);
  74. return PTR_ERR(ctx->cpu_ctrl);
  75. }
  76. ctx->restart_handler.notifier_call = ocelot_restart_handle;
  77. ctx->restart_handler.priority = 192;
  78. err = register_restart_handler(&ctx->restart_handler);
  79. if (err)
  80. dev_err(dev, "can't register restart notifier (err=%d)\n", err);
  81. return err;
  82. }
  83. static const struct reset_props reset_props_jaguar2 = {
  84. .syscon = "mscc,ocelot-cpu-syscon",
  85. .protect_reg = 0x20,
  86. .vcore_protect = BIT(2),
  87. .if_si_owner_bit = 6,
  88. };
  89. static const struct reset_props reset_props_luton = {
  90. .syscon = "mscc,ocelot-cpu-syscon",
  91. .protect_reg = 0x20,
  92. .vcore_protect = BIT(2),
  93. .if_si_owner_bit = BIT_OFF_INVALID, /* n/a */
  94. };
  95. static const struct reset_props reset_props_ocelot = {
  96. .syscon = "mscc,ocelot-cpu-syscon",
  97. .protect_reg = 0x20,
  98. .vcore_protect = BIT(2),
  99. .if_si_owner_bit = 4,
  100. };
  101. static const struct reset_props reset_props_sparx5 = {
  102. .syscon = "microchip,sparx5-cpu-syscon",
  103. .protect_reg = 0x84,
  104. .vcore_protect = BIT(10),
  105. .if_si_owner_bit = 6,
  106. };
  107. static const struct of_device_id ocelot_reset_of_match[] = {
  108. {
  109. .compatible = "mscc,jaguar2-chip-reset",
  110. .data = &reset_props_jaguar2
  111. }, {
  112. .compatible = "mscc,luton-chip-reset",
  113. .data = &reset_props_luton
  114. }, {
  115. .compatible = "mscc,ocelot-chip-reset",
  116. .data = &reset_props_ocelot
  117. }, {
  118. .compatible = "microchip,sparx5-chip-reset",
  119. .data = &reset_props_sparx5
  120. },
  121. { /*sentinel*/ }
  122. };
  123. static struct platform_driver ocelot_reset_driver = {
  124. .probe = ocelot_reset_probe,
  125. .driver = {
  126. .name = "ocelot-chip-reset",
  127. .of_match_table = ocelot_reset_of_match,
  128. },
  129. };
  130. builtin_platform_driver(ocelot_reset_driver);