mt_boot_common.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <linux/atomic.h>
  22. #include <mt-plat/mt_boot_common.h>
  23. enum {
  24. BM_UNINIT = 0,
  25. BM_INITIALIZING = 1,
  26. BM_INITIALIZED = 2,
  27. } BM_INIT_STATE;
  28. enum boot_mode_t g_boot_mode __nosavedata = UNKNOWN_BOOT;
  29. static atomic_t g_boot_init = ATOMIC_INIT(BM_UNINIT);
  30. static atomic_t g_boot_errcnt = ATOMIC_INIT(0);
  31. static atomic_t g_boot_status = ATOMIC_INIT(0);
  32. #ifdef CONFIG_OF
  33. struct tag_bootmode {
  34. u32 size;
  35. u32 tag;
  36. u32 bootmode;
  37. };
  38. static int __init dt_get_boot_common(unsigned long node, const char *uname, int depth, void *data)
  39. {
  40. struct tag_bootmode *tags = NULL;
  41. if (depth != 1 || (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  42. return 0;
  43. tags = (struct tag_bootmode *)of_get_flat_dt_prop(node, "atag,boot", NULL);
  44. if (tags) {
  45. g_boot_mode = tags->bootmode;
  46. atomic_set(&g_boot_status, 1);
  47. } else {
  48. pr_warn("'atag,boot' is not found\n");
  49. }
  50. /* break now */
  51. return 1;
  52. }
  53. #endif
  54. void init_boot_common(unsigned int line)
  55. {
  56. #ifdef CONFIG_OF
  57. int rc;
  58. if (BM_INITIALIZING == atomic_read(&g_boot_init)) {
  59. pr_warn("%s (%d) state(%d,%d)\n", __func__, line, atomic_read(&g_boot_init),
  60. g_boot_mode);
  61. atomic_inc(&g_boot_errcnt);
  62. return;
  63. }
  64. if (BM_UNINIT == atomic_read(&g_boot_init))
  65. atomic_set(&g_boot_init, BM_INITIALIZING);
  66. else
  67. return;
  68. if (UNKNOWN_BOOT != g_boot_mode) {
  69. atomic_set(&g_boot_init, BM_INITIALIZED);
  70. pr_warn("%s (%d) boot_mode = %d\n", __func__, line, g_boot_mode);
  71. return;
  72. }
  73. pr_debug("%s %d %d %d\n", __func__, line, g_boot_mode, atomic_read(&g_boot_init));
  74. rc = of_scan_flat_dt(dt_get_boot_common, NULL);
  75. if (0 != rc)
  76. atomic_set(&g_boot_init, BM_INITIALIZED);
  77. else
  78. pr_warn("of_scan_flat_dt() = %d", rc);
  79. pr_debug("%s %d %d %d\n", __func__, line, g_boot_mode, atomic_read(&g_boot_init));
  80. #endif
  81. }
  82. /* return boot mode */
  83. unsigned int get_boot_mode(void)
  84. {
  85. init_boot_common(__LINE__);
  86. return g_boot_mode;
  87. }
  88. EXPORT_SYMBOL(get_boot_mode);
  89. /* for convenience, simply check is meta mode or not */
  90. bool is_meta_mode(void)
  91. {
  92. init_boot_common(__LINE__);
  93. if (g_boot_mode == META_BOOT)
  94. return true;
  95. else
  96. return false;
  97. }
  98. EXPORT_SYMBOL(is_meta_mode);
  99. bool is_advanced_meta_mode(void)
  100. {
  101. init_boot_common(__LINE__);
  102. if (g_boot_mode == ADVMETA_BOOT)
  103. return true;
  104. else
  105. return false;
  106. }
  107. EXPORT_SYMBOL(is_advanced_meta_mode);
  108. static ssize_t boot_show(struct kobject *kobj, struct attribute *a, char *buf)
  109. {
  110. return sprintf(buf, "%d\n", get_boot_mode());
  111. }
  112. static ssize_t boot_store(struct kobject *kobj, struct attribute *a, const char *buf, size_t count)
  113. {
  114. return count;
  115. }
  116. /* boot object */
  117. static struct kobject boot_kobj;
  118. static const struct sysfs_ops boot_sysfs_ops = {
  119. .show = boot_show,
  120. .store = boot_store,
  121. };
  122. /* boot attribute */
  123. struct attribute boot_attr = { BOOT_SYSFS_ATTR, 0644 };
  124. static struct attribute *boot_attrs[] = {
  125. &boot_attr,
  126. NULL
  127. };
  128. /* boot type */
  129. static struct kobj_type boot_ktype = {
  130. .sysfs_ops = &boot_sysfs_ops,
  131. .default_attrs = boot_attrs
  132. };
  133. /* boot device node */
  134. static dev_t boot_dev_num;
  135. static struct cdev boot_cdev;
  136. static const struct file_operations boot_fops = {
  137. .owner = THIS_MODULE,
  138. .open = NULL,
  139. .release = NULL,
  140. .write = NULL,
  141. .read = NULL,
  142. .unlocked_ioctl = NULL
  143. };
  144. /* boot device class */
  145. static struct class *boot_class;
  146. static struct device *boot_device;
  147. static int __init create_sysfs(void)
  148. {
  149. int ret;
  150. /* allocate device major number */
  151. if (alloc_chrdev_region(&boot_dev_num, 0, 1, BOOT_DEV_NAME) < 0) {
  152. pr_warn("fail to register chrdev\n");
  153. return -1;
  154. }
  155. /* add character driver */
  156. cdev_init(&boot_cdev, &boot_fops);
  157. ret = cdev_add(&boot_cdev, boot_dev_num, 1);
  158. if (ret < 0) {
  159. pr_warn("fail to add cdev\n");
  160. return ret;
  161. }
  162. /* create class (device model) */
  163. boot_class = class_create(THIS_MODULE, BOOT_DEV_NAME);
  164. if (IS_ERR(boot_class)) {
  165. pr_warn("fail to create class\n");
  166. return -1;
  167. }
  168. boot_device = device_create(boot_class, NULL, boot_dev_num, NULL, BOOT_DEV_NAME);
  169. if (IS_ERR(boot_device)) {
  170. pr_warn("fail to create device\n");
  171. return -1;
  172. }
  173. /* add kobject */
  174. ret = kobject_init_and_add(&boot_kobj, &boot_ktype, &(boot_device->kobj), BOOT_SYSFS);
  175. if (ret < 0) {
  176. pr_warn("fail to add kobject\n");
  177. return ret;
  178. }
  179. return 0;
  180. }
  181. static void __exit destroy_sysfs(void)
  182. {
  183. cdev_del(&boot_cdev);
  184. }
  185. static int boot_mode_proc_show(struct seq_file *p, void *v)
  186. {
  187. seq_puts(p, "\n\rMTK BOOT MODE : ");
  188. switch (g_boot_mode) {
  189. case NORMAL_BOOT:
  190. seq_puts(p, "NORMAL BOOT\n");
  191. break;
  192. case META_BOOT:
  193. seq_puts(p, "META BOOT\n");
  194. break;
  195. case ADVMETA_BOOT:
  196. seq_puts(p, "Advanced META BOOT\n");
  197. break;
  198. case ATE_FACTORY_BOOT:
  199. seq_puts(p, "ATE_FACTORY BOOT\n");
  200. break;
  201. case ALARM_BOOT:
  202. seq_puts(p, "ALARM BOOT\n");
  203. break;
  204. default:
  205. seq_puts(p, "UNKNOWN BOOT\n");
  206. break;
  207. }
  208. return 0;
  209. }
  210. static int boot_mode_proc_open(struct inode *inode, struct file *file)
  211. {
  212. return single_open(file, boot_mode_proc_show, NULL);
  213. }
  214. static const struct file_operations boot_mode_proc_fops = {
  215. .open = boot_mode_proc_open,
  216. .read = seq_read,
  217. .llseek = seq_lseek,
  218. .release = single_release,
  219. };
  220. static int __init boot_common_core(void)
  221. {
  222. init_boot_common(__LINE__);
  223. /* create proc entry at /proc/boot_mode */
  224. if (NULL == proc_create_data("boot_mode", S_IRUGO, NULL, &boot_mode_proc_fops, NULL))
  225. pr_warn("create procfs fail");
  226. /* create sysfs entry at /sys/class/BOOT/BOOT/boot */
  227. create_sysfs();
  228. return 0;
  229. }
  230. static int __init boot_common_init(void)
  231. {
  232. pr_debug("boot_mode = %d, state(%d,%d,%d)", g_boot_mode,
  233. atomic_read(&g_boot_init), atomic_read(&g_boot_errcnt),
  234. atomic_read(&g_boot_status));
  235. return 0;
  236. }
  237. core_initcall(boot_common_core);
  238. module_init(boot_common_init);
  239. MODULE_DESCRIPTION("MTK Boot Information Common Driver");
  240. MODULE_LICENSE("GPL");