sysmon.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * (C) Copyright 2008 Dmitry Rakhchev, EmCraft Systems, rda@emcraft.com
  3. *
  4. * Developed for DENX Software Engineering GmbH
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <post.h>
  9. #include <common.h>
  10. /*
  11. * SYSMON test
  12. *
  13. * This test performs the system hardware monitoring.
  14. * The test passes when all the following voltages and temperatures
  15. * are within allowed ranges:
  16. *
  17. * Temperature -40 .. +90 C
  18. * +5V +4.50 .. +5.50 V
  19. * +5V standby +3.50 .. +5.50 V
  20. *
  21. * LCD backlight is not enabled if temperature values are not within
  22. * allowed ranges (-30 .. + 80). The brightness of backlite can be
  23. * controlled by setting "brightness" environment variable. Default value is 50%
  24. *
  25. * See the list of all parameters in the sysmon_table below
  26. */
  27. #include <post.h>
  28. #include <watchdog.h>
  29. #include <i2c.h>
  30. #if defined(CONFIG_VIDEO)
  31. #include <mb862xx.h>
  32. #endif
  33. #if CONFIG_POST & CONFIG_SYS_POST_SYSMON
  34. DECLARE_GLOBAL_DATA_PTR;
  35. /* from dspic.c */
  36. extern int dspic_read(ushort reg, ushort *data);
  37. #define REG_TEMPERATURE 0x12BC
  38. #define REG_VOLTAGE_5V 0x12CA
  39. #define REG_VOLTAGE_5V_STANDBY 0x12C6
  40. #define TEMPERATURE_MIN (-40) /* degr. C */
  41. #define TEMPERATURE_MAX (+90) /* degr. C */
  42. #define TEMPERATURE_DISPLAY_MIN (-35) /* degr. C */
  43. #define TEMPERATURE_DISPLAY_MAX (+85) /* degr. C */
  44. #define VOLTAGE_5V_MIN (+4500) /* mV */
  45. #define VOLTAGE_5V_MAX (+5500) /* mV */
  46. #define VOLTAGE_5V_STANDBY_MIN (+3500) /* mV */
  47. #define VOLTAGE_5V_STANDBY_MAX (+5500) /* mV */
  48. typedef struct sysmon_s sysmon_t;
  49. typedef struct sysmon_table_s sysmon_table_t;
  50. static void sysmon_dspic_init(sysmon_t *this);
  51. static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val);
  52. static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val);
  53. static void sysmon_backlight_disable(sysmon_table_t *this);
  54. struct sysmon_s {
  55. uchar chip;
  56. void (*init)(sysmon_t *);
  57. int (*read)(sysmon_t *, uint, int *);
  58. };
  59. static sysmon_t sysmon_dspic = {
  60. CONFIG_SYS_I2C_DSPIC_IO_ADDR,
  61. sysmon_dspic_init,
  62. sysmon_dspic_read
  63. };
  64. static sysmon_t sysmon_dspic_sgn = {
  65. CONFIG_SYS_I2C_DSPIC_IO_ADDR,
  66. sysmon_dspic_init,
  67. sysmon_dspic_read_sgn
  68. };
  69. static sysmon_t *sysmon_list[] = {
  70. &sysmon_dspic,
  71. NULL
  72. };
  73. struct sysmon_table_s {
  74. char *name;
  75. char *unit_name;
  76. sysmon_t *sysmon;
  77. void (*exec_before)(sysmon_table_t *);
  78. void (*exec_after)(sysmon_table_t *);
  79. int unit_precision;
  80. int unit_div;
  81. int unit_min;
  82. int unit_max;
  83. uint val_mask;
  84. uint val_min;
  85. uint val_max;
  86. int val_valid;
  87. uint val_min_alt;
  88. uint val_max_alt;
  89. int val_valid_alt;
  90. uint addr;
  91. };
  92. static sysmon_table_t sysmon_table[] = {
  93. {
  94. "Temperature", " C", &sysmon_dspic, NULL, sysmon_backlight_disable,
  95. 1, 1, -32768, 32767, 0xFFFF,
  96. 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
  97. 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
  98. REG_TEMPERATURE,
  99. },
  100. {
  101. "+ 5 V", "V", &sysmon_dspic, NULL, NULL,
  102. 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
  103. 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
  104. 0x8000 + VOLTAGE_5V_MIN, 0x8000 + VOLTAGE_5V_MAX, 0,
  105. REG_VOLTAGE_5V,
  106. },
  107. {
  108. "+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
  109. 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
  110. 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
  111. 0x8000 + VOLTAGE_5V_STANDBY_MIN, 0x8000 + VOLTAGE_5V_STANDBY_MAX, 0,
  112. REG_VOLTAGE_5V_STANDBY,
  113. },
  114. {
  115. "Temperature", "°C", &sysmon_dspic_sgn, NULL, sysmon_backlight_disable,
  116. 1, 1, -32768, 32767, 0xFFFF,
  117. 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
  118. 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
  119. REG_TEMPERATURE,
  120. },
  121. };
  122. int sysmon_init_f(void)
  123. {
  124. sysmon_t **l;
  125. for (l = sysmon_list; *l; l++)
  126. (*l)->init(*l);
  127. return 0;
  128. }
  129. void sysmon_reloc(void)
  130. {
  131. /* Do nothing for now, sysmon_reloc() is required by the sysmon post */
  132. }
  133. static char *sysmon_unit_value(sysmon_table_t *s, uint val)
  134. {
  135. static char buf[32];
  136. char *p, sign;
  137. int decimal, frac;
  138. int unit_val;
  139. unit_val = s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
  140. if (val == -1)
  141. return "I/O ERROR";
  142. if (unit_val < 0) {
  143. sign = '-';
  144. unit_val = -unit_val;
  145. } else {
  146. sign = '+';
  147. }
  148. p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
  149. frac = unit_val % s->unit_div;
  150. frac /= (s->unit_div / s->unit_precision);
  151. decimal = s->unit_precision;
  152. if (decimal != 1)
  153. *p++ = '.';
  154. for (decimal /= 10; decimal != 0; decimal /= 10)
  155. *p++ = '0' + (frac / decimal) % 10;
  156. strcpy(p, s->unit_name);
  157. return buf;
  158. }
  159. static void sysmon_dspic_init(sysmon_t *this)
  160. {
  161. }
  162. static int sysmon_dspic_read(sysmon_t *this, uint addr, int *val)
  163. {
  164. ushort data;
  165. if (dspic_read(addr, &data) == 0){
  166. /* To fit into the table range we should add 0x8000 */
  167. *val = data + 0x8000;
  168. return 0;
  169. }
  170. return -1;
  171. }
  172. static int sysmon_dspic_read_sgn(sysmon_t *this, uint addr, int *val)
  173. {
  174. ushort data;
  175. if (dspic_read(addr, &data) == 0){
  176. /* To fit into the table range we should add 0x8000 */
  177. *val = (signed short)data + 0x8000;
  178. return 0;
  179. }
  180. return -1;
  181. }
  182. static void sysmon_backlight_disable(sysmon_table_t *this)
  183. {
  184. #if defined(CONFIG_VIDEO)
  185. board_backlight_switch(this->val_valid_alt);
  186. #endif
  187. }
  188. int sysmon_post_test(int flags)
  189. {
  190. int res = 0;
  191. sysmon_table_t * t;
  192. int val;
  193. for (t = sysmon_table; t < sysmon_table + ARRAY_SIZE(sysmon_table); t++) {
  194. t->val_valid = 1;
  195. if (t->exec_before)
  196. t->exec_before(t);
  197. if (t->sysmon->read(t->sysmon, t->addr, &val) != 0) {
  198. t->val_valid = 0;
  199. t->val_valid_alt = 0;
  200. post_log(": read failed\n");
  201. res = 1;
  202. break;
  203. }
  204. if (t->val_valid != 0) {
  205. t->val_valid = val >= t->val_min && val <= t->val_max;
  206. t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
  207. }
  208. if (t->exec_after)
  209. t->exec_after(t);
  210. if ((!t->val_valid) || (flags)) {
  211. post_log("\n\t%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
  212. post_log("allowed range");
  213. post_log(" %-8s ..", sysmon_unit_value(t, t->val_min));
  214. post_log(" %-8s", sysmon_unit_value(t, t->val_max));
  215. post_log(" %s", t->val_valid ? "OK" : "FAIL");
  216. }
  217. if (!t->val_valid) {
  218. res = 1;
  219. break;
  220. }
  221. }
  222. post_log("\n");
  223. return res;
  224. }
  225. #endif /* CONFIG_POST & CONFIG_SYS_POST_SYSMON */