sysmon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 & CFG_POST_SYSMON
  50. DECLARE_GLOBAL_DATA_PTR;
  51. /* from dspic.c */
  52. extern int dspic_read(ushort reg);
  53. #define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off)
  54. #define REG_TEMPERATURE 0x12BC
  55. #define REG_VOLTAGE_5V 0x12CA
  56. #define REG_VOLTAGE_5V_STANDBY 0x12C6
  57. #define TEMPERATURE_MIN (-40) /* degr. C */
  58. #define TEMPERATURE_MAX (+90) /* degr. C */
  59. #define TEMPERATURE_DISPLAY_MIN (-35) /* degr. C */
  60. #define TEMPERATURE_DISPLAY_MAX (+85) /* degr. C */
  61. #define VOLTAGE_5V_MIN (+4500) /* mV */
  62. #define VOLTAGE_5V_MAX (+5500) /* mV */
  63. #define VOLTAGE_5V_STANDBY_MIN (+3500) /* mV */
  64. #define VOLTAGE_5V_STANDBY_MAX (+5500) /* mV */
  65. typedef struct sysmon_s sysmon_t;
  66. typedef struct sysmon_table_s sysmon_table_t;
  67. static void sysmon_dspic_init (sysmon_t * this);
  68. static int sysmon_dspic_read (sysmon_t * this, uint addr);
  69. static void sysmon_backlight_disable (sysmon_table_t * this);
  70. struct sysmon_s
  71. {
  72. uchar chip;
  73. void (*init)(sysmon_t *);
  74. int (*read)(sysmon_t *, uint);
  75. };
  76. static sysmon_t sysmon_dspic =
  77. {CFG_I2C_DSPIC_IO_ADDR, sysmon_dspic_init, sysmon_dspic_read};
  78. static sysmon_t * sysmon_list[] =
  79. {
  80. &sysmon_dspic,
  81. NULL
  82. };
  83. struct sysmon_table_s
  84. {
  85. char * name;
  86. char * unit_name;
  87. sysmon_t * sysmon;
  88. void (*exec_before)(sysmon_table_t *);
  89. void (*exec_after)(sysmon_table_t *);
  90. int unit_precision;
  91. int unit_div;
  92. int unit_min;
  93. int unit_max;
  94. uint val_mask;
  95. uint val_min;
  96. uint val_max;
  97. int val_valid;
  98. uint val_min_alt;
  99. uint val_max_alt;
  100. int val_valid_alt;
  101. uint addr;
  102. };
  103. static sysmon_table_t sysmon_table[] =
  104. {
  105. {
  106. "Temperature", " C", &sysmon_dspic, NULL, sysmon_backlight_disable,
  107. 1, 1, -32768, 32767, 0xFFFF,
  108. 0x8000 + TEMPERATURE_MIN, 0x8000 + TEMPERATURE_MAX, 0,
  109. 0x8000 + TEMPERATURE_DISPLAY_MIN, 0x8000 + TEMPERATURE_DISPLAY_MAX, 0,
  110. REG_TEMPERATURE
  111. },
  112. {
  113. "+ 5 V", "V", &sysmon_dspic, NULL, NULL,
  114. 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
  115. 100, 1000, 0, 0xFFFF, 0xFFFF,
  116. VOLTAGE_5V_MIN, VOLTAGE_5V_MAX, 0,
  117. VOLTAGE_5V_MIN, VOLTAGE_5V_MAX, 0,
  118. REG_VOLTAGE_5V
  119. },
  120. {
  121. "+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
  122. 100, 1000, -0x8000, 0x7FFF, 0xFFFF,
  123. 100, 1000, 0, 0xFFFF, 0xFFFF,
  124. VOLTAGE_5V_STANDBY_MIN, VOLTAGE_5V_STANDBY_MAX, 0,
  125. VOLTAGE_5V_STANDBY_MIN, VOLTAGE_5V_STANDBY_MAX, 0,
  126. REG_VOLTAGE_5V_STANDBY
  127. },
  128. };
  129. static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
  130. int sysmon_init_f (void)
  131. {
  132. sysmon_t ** l;
  133. for (l = sysmon_list; *l; l++)
  134. (*l)->init(*l);
  135. return 0;
  136. }
  137. void sysmon_reloc (void)
  138. {
  139. sysmon_t ** l;
  140. sysmon_table_t * t;
  141. for (l = sysmon_list; *l; l++) {
  142. RELOC(*l);
  143. RELOC((*l)->init);
  144. RELOC((*l)->read);
  145. }
  146. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
  147. RELOC(t->exec_before);
  148. RELOC(t->exec_after);
  149. RELOC(t->sysmon);
  150. }
  151. }
  152. static char *sysmon_unit_value (sysmon_table_t *s, uint val)
  153. {
  154. static char buf[32];
  155. char *p, sign;
  156. int decimal, frac;
  157. int unit_val;
  158. unit_val = s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
  159. if (val == -1)
  160. return "I/O ERROR";
  161. if (unit_val < 0) {
  162. sign = '-';
  163. unit_val = -unit_val;
  164. } else
  165. sign = '+';
  166. p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
  167. frac = unit_val % s->unit_div;
  168. frac /= (s->unit_div / s->unit_precision);
  169. decimal = s->unit_precision;
  170. if (decimal != 1)
  171. *p++ = '.';
  172. for (decimal /= 10; decimal != 0; decimal /= 10)
  173. *p++ = '0' + (frac / decimal) % 10;
  174. strcpy(p, s->unit_name);
  175. return buf;
  176. }
  177. static void sysmon_dspic_init (sysmon_t * this)
  178. {
  179. }
  180. static int sysmon_dspic_read (sysmon_t * this, uint addr)
  181. {
  182. int res = dspic_read(addr);
  183. /* To fit into the table range we should add 0x8000 */
  184. return (res == -1) ? -1 : (res + 0x8000);
  185. }
  186. static void sysmon_backlight_disable (sysmon_table_t * this)
  187. {
  188. #if defined(CONFIG_VIDEO)
  189. board_backlight_switch(this->val_valid_alt);
  190. #endif
  191. }
  192. int sysmon_post_test (int flags)
  193. {
  194. int res = 0;
  195. sysmon_table_t * t;
  196. int val;
  197. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
  198. if (t->exec_before)
  199. t->exec_before(t);
  200. val = t->sysmon->read(t->sysmon, t->addr);
  201. if (val != -1) {
  202. t->val_valid = val >= t->val_min && val <= t->val_max;
  203. t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
  204. } else {
  205. t->val_valid = 0;
  206. t->val_valid_alt = 0;
  207. }
  208. if (t->exec_after)
  209. t->exec_after(t);
  210. if ((!t->val_valid) || (flags & POST_MANUAL)) {
  211. printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
  212. printf("allowed range");
  213. printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
  214. printf(" %-8s", sysmon_unit_value(t, t->val_max));
  215. printf(" %s\n", t->val_valid ? "OK" : "FAIL");
  216. }
  217. if (!t->val_valid)
  218. res = 1;
  219. }
  220. return res;
  221. }
  222. #endif /* CONFIG_POST & CFG_POST_SYSMON */