mt_boot_reason.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #define pr_fmt(fmt) "["KBUILD_MODNAME"] " fmt
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/mm.h>
  10. #include <linux/kfifo.h>
  11. #include <linux/firmware.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/of.h>
  18. #ifdef CONFIG_OF
  19. #include <linux/of_fdt.h>
  20. #endif
  21. #include <asm/setup.h>
  22. #include <linux/atomic.h>
  23. #include <mt-plat/mt_boot_reason.h>
  24. enum {
  25. BOOT_REASON_UNINIT = 0,
  26. BOOT_REASON_INITIALIZING = 1,
  27. BOOT_REASON_INITIALIZED = 2,
  28. } BOOT_REASON_STATE;
  29. enum boot_reason_t g_boot_reason __nosavedata = BR_UNKNOWN;
  30. static atomic_t g_br_state = ATOMIC_INIT(BOOT_REASON_UNINIT);
  31. static atomic_t g_br_errcnt = ATOMIC_INIT(0);
  32. static atomic_t g_br_status = ATOMIC_INIT(0);
  33. #ifdef CONFIG_OF
  34. static int __init dt_get_boot_reason(unsigned long node, const char *uname, int depth, void *data)
  35. {
  36. char *ptr = NULL, *br_ptr = NULL;
  37. if (depth != 1 || (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  38. return 0;
  39. ptr = (char *)of_get_flat_dt_prop(node, "bootargs", NULL);
  40. if (ptr) {
  41. br_ptr = strstr(ptr, "boot_reason=");
  42. if (br_ptr != 0) {
  43. g_boot_reason = br_ptr[12] - '0'; /* get boot reason */
  44. atomic_set(&g_br_status, 1);
  45. } else {
  46. pr_warn("'boot_reason=' is not found\n");
  47. }
  48. pr_debug("%s\n", ptr);
  49. } else
  50. pr_warn("'bootargs' is not found\n");
  51. /* break now */
  52. return 1;
  53. }
  54. #endif
  55. void init_boot_reason(unsigned int line)
  56. {
  57. #ifdef CONFIG_OF
  58. int rc;
  59. if (BOOT_REASON_INITIALIZING == atomic_read(&g_br_state)) {
  60. pr_warn("%s (%d) state(%d)\n", __func__, line, atomic_read(&g_br_state));
  61. atomic_inc(&g_br_errcnt);
  62. return;
  63. }
  64. if (BOOT_REASON_UNINIT == atomic_read(&g_br_state))
  65. atomic_set(&g_br_state, BOOT_REASON_INITIALIZING);
  66. else
  67. return;
  68. if (BR_UNKNOWN != g_boot_reason) {
  69. atomic_set(&g_br_state, BOOT_REASON_INITIALIZED);
  70. pr_warn("boot_reason = %d\n", g_boot_reason);
  71. return;
  72. }
  73. pr_debug("%s %d %d %d\n", __func__, line, g_boot_reason, atomic_read(&g_br_state));
  74. rc = of_scan_flat_dt(dt_get_boot_reason, NULL);
  75. if (0 != rc)
  76. atomic_set(&g_br_state, BOOT_REASON_INITIALIZED);
  77. else
  78. atomic_set(&g_br_state, BOOT_REASON_UNINIT);
  79. pr_debug("%s %d %d %d\n", __func__, line, g_boot_reason, atomic_read(&g_br_state));
  80. #endif
  81. }
  82. /* return boot reason */
  83. enum boot_reason_t get_boot_reason(void)
  84. {
  85. init_boot_reason(__LINE__);
  86. return g_boot_reason;
  87. }
  88. static int __init boot_reason_core(void)
  89. {
  90. init_boot_reason(__LINE__);
  91. return 0;
  92. }
  93. static int __init boot_reason_init(void)
  94. {
  95. pr_debug("boot_reason = %d, state(%d,%d,%d)", g_boot_reason,
  96. atomic_read(&g_br_state), atomic_read(&g_br_errcnt), atomic_read(&g_br_status));
  97. return 0;
  98. }
  99. early_initcall(boot_reason_core);
  100. module_init(boot_reason_init);
  101. MODULE_DESCRIPTION("Mediatek Boot Reason Driver");
  102. MODULE_LICENSE("GPL");