keystone-reset.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI keystone reboot driver
  4. *
  5. * Copyright (C) 2014 Texas Instruments Incorporated. https://www.ti.com/
  6. *
  7. * Author: Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>
  8. */
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/notifier.h>
  12. #include <linux/reboot.h>
  13. #include <linux/regmap.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/of_platform.h>
  16. #define RSTYPE_RG 0x0
  17. #define RSCTRL_RG 0x4
  18. #define RSCFG_RG 0x8
  19. #define RSISO_RG 0xc
  20. #define RSCTRL_KEY_MASK 0x0000ffff
  21. #define RSCTRL_RESET_MASK BIT(16)
  22. #define RSCTRL_KEY 0x5a69
  23. #define RSMUX_OMODE_MASK 0xe
  24. #define RSMUX_OMODE_RESET_ON 0xa
  25. #define RSMUX_OMODE_RESET_OFF 0x0
  26. #define RSMUX_LOCK_MASK 0x1
  27. #define RSMUX_LOCK_SET 0x1
  28. #define RSCFG_RSTYPE_SOFT 0x300f
  29. #define RSCFG_RSTYPE_HARD 0x0
  30. #define WDT_MUX_NUMBER 0x4
  31. static int rspll_offset;
  32. static struct regmap *pllctrl_regs;
  33. /**
  34. * rsctrl_enable_rspll_write - enable access to RSCTRL, RSCFG
  35. * To be able to access to RSCTRL, RSCFG registers
  36. * we have to write a key before
  37. */
  38. static inline int rsctrl_enable_rspll_write(void)
  39. {
  40. return regmap_update_bits(pllctrl_regs, rspll_offset + RSCTRL_RG,
  41. RSCTRL_KEY_MASK, RSCTRL_KEY);
  42. }
  43. static int rsctrl_restart_handler(struct notifier_block *this,
  44. unsigned long mode, void *cmd)
  45. {
  46. /* enable write access to RSTCTRL */
  47. rsctrl_enable_rspll_write();
  48. /* reset the SOC */
  49. regmap_update_bits(pllctrl_regs, rspll_offset + RSCTRL_RG,
  50. RSCTRL_RESET_MASK, 0);
  51. return NOTIFY_DONE;
  52. }
  53. static struct notifier_block rsctrl_restart_nb = {
  54. .notifier_call = rsctrl_restart_handler,
  55. .priority = 128,
  56. };
  57. static const struct of_device_id rsctrl_of_match[] = {
  58. {.compatible = "ti,keystone-reset", },
  59. {},
  60. };
  61. static int rsctrl_probe(struct platform_device *pdev)
  62. {
  63. int i;
  64. int ret;
  65. u32 val;
  66. unsigned int rg;
  67. u32 rsmux_offset;
  68. struct regmap *devctrl_regs;
  69. struct device *dev = &pdev->dev;
  70. struct device_node *np = dev->of_node;
  71. if (!np)
  72. return -ENODEV;
  73. /* get regmaps */
  74. pllctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pll");
  75. if (IS_ERR(pllctrl_regs))
  76. return PTR_ERR(pllctrl_regs);
  77. devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-dev");
  78. if (IS_ERR(devctrl_regs))
  79. return PTR_ERR(devctrl_regs);
  80. ret = of_property_read_u32_index(np, "ti,syscon-pll", 1, &rspll_offset);
  81. if (ret) {
  82. dev_err(dev, "couldn't read the reset pll offset!\n");
  83. return -EINVAL;
  84. }
  85. ret = of_property_read_u32_index(np, "ti,syscon-dev", 1, &rsmux_offset);
  86. if (ret) {
  87. dev_err(dev, "couldn't read the rsmux offset!\n");
  88. return -EINVAL;
  89. }
  90. /* set soft/hard reset */
  91. val = of_property_read_bool(np, "ti,soft-reset");
  92. val = val ? RSCFG_RSTYPE_SOFT : RSCFG_RSTYPE_HARD;
  93. ret = rsctrl_enable_rspll_write();
  94. if (ret)
  95. return ret;
  96. ret = regmap_write(pllctrl_regs, rspll_offset + RSCFG_RG, val);
  97. if (ret)
  98. return ret;
  99. /* disable a reset isolation for all module clocks */
  100. ret = regmap_write(pllctrl_regs, rspll_offset + RSISO_RG, 0);
  101. if (ret)
  102. return ret;
  103. /* enable a reset for watchdogs from wdt-list */
  104. for (i = 0; i < WDT_MUX_NUMBER; i++) {
  105. ret = of_property_read_u32_index(np, "ti,wdt-list", i, &val);
  106. if (ret == -EOVERFLOW && !i) {
  107. dev_err(dev, "ti,wdt-list property has to contain at"
  108. "least one entry\n");
  109. return -EINVAL;
  110. } else if (ret) {
  111. break;
  112. }
  113. if (val >= WDT_MUX_NUMBER) {
  114. dev_err(dev, "ti,wdt-list property can contain "
  115. "only numbers < 4\n");
  116. return -EINVAL;
  117. }
  118. rg = rsmux_offset + val * 4;
  119. ret = regmap_update_bits(devctrl_regs, rg, RSMUX_OMODE_MASK,
  120. RSMUX_OMODE_RESET_ON |
  121. RSMUX_LOCK_SET);
  122. if (ret)
  123. return ret;
  124. }
  125. ret = register_restart_handler(&rsctrl_restart_nb);
  126. if (ret)
  127. dev_err(dev, "cannot register restart handler (err=%d)\n", ret);
  128. return ret;
  129. }
  130. static struct platform_driver rsctrl_driver = {
  131. .probe = rsctrl_probe,
  132. .driver = {
  133. .name = KBUILD_MODNAME,
  134. .of_match_table = rsctrl_of_match,
  135. },
  136. };
  137. module_platform_driver(rsctrl_driver);
  138. MODULE_AUTHOR("Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>");
  139. MODULE_DESCRIPTION("Texas Instruments keystone reset driver");
  140. MODULE_LICENSE("GPL v2");
  141. MODULE_ALIAS("platform:" KBUILD_MODNAME);