keystone-reset.c 4.2 KB

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