sysmon.c 6.8 KB

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