keystone-reset.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <[email protected]>
  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. MODULE_DEVICE_TABLE(of, rsctrl_of_match);
  62. static int rsctrl_probe(struct platform_device *pdev)
  63. {
  64. int i;
  65. int ret;
  66. u32 val;
  67. unsigned int rg;
  68. u32 rsmux_offset;
  69. struct regmap *devctrl_regs;
  70. struct device *dev = &pdev->dev;
  71. struct device_node *np = dev->of_node;
  72. if (!np)
  73. return -ENODEV;
  74. /* get regmaps */
  75. pllctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pll");
  76. if (IS_ERR(pllctrl_regs))
  77. return PTR_ERR(pllctrl_regs);
  78. devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-dev");
  79. if (IS_ERR(devctrl_regs))
  80. return PTR_ERR(devctrl_regs);
  81. ret = of_property_read_u32_index(np, "ti,syscon-pll", 1, &rspll_offset);
  82. if (ret) {
  83. dev_err(dev, "couldn't read the reset pll offset!\n");
  84. return -EINVAL;
  85. }
  86. ret = of_property_read_u32_index(np, "ti,syscon-dev", 1, &rsmux_offset);
  87. if (ret) {
  88. dev_err(dev, "couldn't read the rsmux offset!\n");
  89. return -EINVAL;
  90. }
  91. /* set soft/hard reset */
  92. val = of_property_read_bool(np, "ti,soft-reset");
  93. val = val ? RSCFG_RSTYPE_SOFT : RSCFG_RSTYPE_HARD;
  94. ret = rsctrl_enable_rspll_write();
  95. if (ret)
  96. return ret;
  97. ret = regmap_write(pllctrl_regs, rspll_offset + RSCFG_RG, val);
  98. if (ret)
  99. return ret;
  100. /* disable a reset isolation for all module clocks */
  101. ret = regmap_write(pllctrl_regs, rspll_offset + RSISO_RG, 0);
  102. if (ret)
  103. return ret;
  104. /* enable a reset for watchdogs from wdt-list */
  105. for (i = 0; i < WDT_MUX_NUMBER; i++) {
  106. ret = of_property_read_u32_index(np, "ti,wdt-list", i, &val);
  107. if (ret == -EOVERFLOW && !i) {
  108. dev_err(dev, "ti,wdt-list property has to contain at"
  109. "least one entry\n");
  110. return -EINVAL;
  111. } else if (ret) {
  112. break;
  113. }
  114. if (val >= WDT_MUX_NUMBER) {
  115. dev_err(dev, "ti,wdt-list property can contain "
  116. "only numbers < 4\n");
  117. return -EINVAL;
  118. }
  119. rg = rsmux_offset + val * 4;
  120. ret = regmap_update_bits(devctrl_regs, rg, RSMUX_OMODE_MASK,
  121. RSMUX_OMODE_RESET_ON |
  122. RSMUX_LOCK_SET);
  123. if (ret)
  124. return ret;
  125. }
  126. ret = register_restart_handler(&rsctrl_restart_nb);
  127. if (ret)
  128. dev_err(dev, "cannot register restart handler (err=%d)\n", ret);
  129. return ret;
  130. }
  131. static struct platform_driver rsctrl_driver = {
  132. .probe = rsctrl_probe,
  133. .driver = {
  134. .name = KBUILD_MODNAME,
  135. .of_match_table = rsctrl_of_match,
  136. },
  137. };
  138. module_platform_driver(rsctrl_driver);
  139. MODULE_AUTHOR("Ivan Khoronzhuk <[email protected]>");
  140. MODULE_DESCRIPTION("Texas Instruments keystone reset driver");
  141. MODULE_LICENSE("GPL v2");
  142. MODULE_ALIAS("platform:" KBUILD_MODNAME);