sysmon.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifdef CONFIG_POST
  27. /*
  28. * SYSMON test
  29. *
  30. * This test performs the system hardware monitoring.
  31. * The test passes when all the following voltages and temperatures
  32. * are within allowed ranges:
  33. *
  34. * Temperature -40 .. +85 C
  35. * +5V +4.75 .. +5.25 V
  36. * +5V standby +4.75 .. +5.25 V
  37. *
  38. * LCD backlight is not enabled if temperature values are not within
  39. * allowed ranges (-30 .. + 80). The brightness of backlite can be
  40. * controlled by setting "brightness" enviroment variable. Default value is 50%
  41. *
  42. * See the list of all parameters in the sysmon_table below
  43. */
  44. #include <post.h>
  45. #include <watchdog.h>
  46. #include <i2c.h>
  47. #if defined(CONFIG_VIDEO)
  48. #include <mb862xx.h>
  49. #endif
  50. #if CONFIG_POST & CFG_POST_SYSMON
  51. DECLARE_GLOBAL_DATA_PTR;
  52. /* from dspic.c */
  53. extern int dspic_read(ushort reg, ushort *data);
  54. #define RELOC(x) if (x != NULL) x = (void *) ((ulong) (x) + gd->reloc_off)
  55. typedef struct sysmon_s sysmon_t;
  56. typedef struct sysmon_table_s sysmon_table_t;
  57. static void sysmon_dspic_init (sysmon_t * this);
  58. static int sysmon_dspic_read (sysmon_t * this, uint addr);
  59. static int sysmon_dspic_read_sgn (sysmon_t * this, uint addr);
  60. static void sysmon_backlight_disable (sysmon_table_t * this);
  61. struct sysmon_s
  62. {
  63. uchar chip;
  64. void (*init)(sysmon_t *);
  65. int (*read)(sysmon_t *, uint);
  66. };
  67. static sysmon_t sysmon_dspic =
  68. {CFG_I2C_DSPIC_IO_ADDR, sysmon_dspic_init, sysmon_dspic_read};
  69. static sysmon_t sysmon_dspic_sgn =
  70. {CFG_I2C_DSPIC_IO_ADDR, sysmon_dspic_init, sysmon_dspic_read_sgn};
  71. static sysmon_t * sysmon_list[] =
  72. {
  73. &sysmon_dspic,
  74. &sysmon_dspic_sgn,
  75. NULL
  76. };
  77. struct sysmon_table_s
  78. {
  79. char * name;
  80. char * unit_name;
  81. sysmon_t * sysmon;
  82. void (*exec_before)(sysmon_table_t *);
  83. void (*exec_after)(sysmon_table_t *);
  84. int unit_precision;
  85. int unit_div;
  86. int unit_min;
  87. int unit_max;
  88. uint val_mask;
  89. uint val_min;
  90. uint val_max;
  91. int val_valid;
  92. uint val_min_alt;
  93. uint val_max_alt;
  94. int val_valid_alt;
  95. uint addr;
  96. };
  97. static sysmon_table_t sysmon_table[] =
  98. {
  99. {"Temperature", " C", &sysmon_dspic_sgn, NULL, sysmon_backlight_disable,
  100. 1, 1, -32768, 32767, 0xFFFF, 0x8000-40, 0x8000+85, 0,
  101. 0x8000-30, 0x8000+80, 0, 0x12BC},
  102. {"+ 5 V", "V", &sysmon_dspic, NULL, NULL,
  103. 100, 1000, 0, 0xFFFF, 0xFFFF, 4750, 5250, 0,
  104. 4750, 5250, 0, 0x12CA},
  105. {"+ 5 V standby", "V", &sysmon_dspic, NULL, NULL,
  106. 100, 1000, 0, 0xFFFF, 0xFFFF, 4750, 5250, 0,
  107. 4750, 5250, 0, 0x12C6},
  108. };
  109. static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
  110. int sysmon_init_f (void)
  111. {
  112. sysmon_t ** l;
  113. for (l = sysmon_list; *l; l++)
  114. (*l)->init(*l);
  115. return 0;
  116. }
  117. void sysmon_reloc (void)
  118. {
  119. sysmon_t ** l;
  120. sysmon_table_t * t;
  121. for (l = sysmon_list; *l; l++) {
  122. RELOC(*l);
  123. RELOC((*l)->init);
  124. RELOC((*l)->read);
  125. }
  126. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
  127. RELOC(t->exec_before);
  128. RELOC(t->exec_after);
  129. RELOC(t->sysmon);
  130. }
  131. }
  132. static char *sysmon_unit_value (sysmon_table_t *s, uint val)
  133. {
  134. static char buf[32];
  135. char *p, sign;
  136. int decimal, frac;
  137. int unit_val =
  138. s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
  139. if (val == -1)
  140. return "I/O ERROR";
  141. if (unit_val < 0) {
  142. sign = '-';
  143. unit_val = -unit_val;
  144. } else
  145. sign = '+';
  146. p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
  147. frac = unit_val % s->unit_div;
  148. frac /= (s->unit_div / s->unit_precision);
  149. decimal = s->unit_precision;
  150. if (decimal != 1)
  151. *p++ = '.';
  152. for (decimal /= 10; decimal != 0; decimal /= 10)
  153. *p++ = '0' + (frac / decimal) % 10;
  154. strcpy(p, s->unit_name);
  155. return buf;
  156. }
  157. static void sysmon_dspic_init (sysmon_t * this)
  158. {
  159. }
  160. static int sysmon_dspic_read (sysmon_t * this, uint addr)
  161. {
  162. ushort data;
  163. return (dspic_read(addr, &data)) ? -1 : data;
  164. }
  165. static int sysmon_dspic_read_sgn (sysmon_t * this, uint addr)
  166. {
  167. ushort data;
  168. /* To fit into the table range we should add 0x8000 */
  169. return (dspic_read(addr, &data)) ? -1 :
  170. (signed short)data + 0x8000;
  171. }
  172. static void sysmon_backlight_disable (sysmon_table_t * this)
  173. {
  174. #if defined(CONFIG_VIDEO)
  175. board_backlight_switch(this->val_valid_alt);
  176. #endif
  177. }
  178. int sysmon_post_test (int flags)
  179. {
  180. int res = 0;
  181. sysmon_table_t * t;
  182. int val;
  183. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
  184. if (t->exec_before)
  185. t->exec_before(t);
  186. val = t->sysmon->read(t->sysmon, t->addr);
  187. if (val != -1) {
  188. t->val_valid = val >= t->val_min && val <= t->val_max;
  189. t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
  190. } else {
  191. t->val_valid = 0;
  192. t->val_valid_alt = 0;
  193. }
  194. if (t->exec_after)
  195. t->exec_after(t);
  196. if ((!t->val_valid) || (flags & POST_MANUAL)) {
  197. printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
  198. printf("allowed range");
  199. printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
  200. printf(" %-8s", sysmon_unit_value(t, t->val_max));
  201. printf(" %s\n", t->val_valid ? "OK" : "FAIL");
  202. }
  203. if (!t->val_valid)
  204. res = 1;
  205. }
  206. return res;
  207. }
  208. #endif /* CONFIG_POST & CFG_POST_SYSMON */
  209. #endif /* CONFIG_POST */