sysmon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <post.h>
  8. #include <common.h>
  9. /*
  10. * SYSMON test
  11. *
  12. * This test performs the system hardware monitoring.
  13. * The test passes when all the following voltages and temperatures
  14. * are within allowed ranges:
  15. *
  16. * Board temperature
  17. * Front temperature
  18. * +3.3V CPU logic
  19. * +5V logic
  20. * +12V PCMCIA
  21. * +12V CCFL
  22. * +5V standby
  23. *
  24. * CCFL is not enabled if temperature values are not within allowed ranges
  25. *
  26. * See the list off all parameters in the sysmon_table below
  27. */
  28. #include <post.h>
  29. #include <watchdog.h>
  30. #include <i2c.h>
  31. #if CONFIG_POST & CONFIG_SYS_POST_SYSMON
  32. DECLARE_GLOBAL_DATA_PTR;
  33. static int sysmon_temp_invalid = 0;
  34. /* #define DEBUG */
  35. typedef struct sysmon_s sysmon_t;
  36. typedef struct sysmon_table_s sysmon_table_t;
  37. static void sysmon_lm87_init (sysmon_t * this);
  38. static void sysmon_pic_init (sysmon_t * this);
  39. static uint sysmon_i2c_read (sysmon_t * this, uint addr);
  40. static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr);
  41. static void sysmon_ccfl_disable (sysmon_table_t * this);
  42. static void sysmon_ccfl_enable (sysmon_table_t * this);
  43. struct sysmon_s
  44. {
  45. uchar chip;
  46. void (*init)(sysmon_t *);
  47. uint (*read)(sysmon_t *, uint);
  48. };
  49. static sysmon_t sysmon_lm87 =
  50. {CONFIG_SYS_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read};
  51. static sysmon_t sysmon_lm87_sgn =
  52. {CONFIG_SYS_I2C_SYSMON_ADDR, sysmon_lm87_init, sysmon_i2c_read_sgn};
  53. static sysmon_t sysmon_pic =
  54. {CONFIG_SYS_I2C_PICIO_ADDR, sysmon_pic_init, sysmon_i2c_read};
  55. static sysmon_t * sysmon_list[] =
  56. {
  57. &sysmon_lm87,
  58. &sysmon_lm87_sgn,
  59. &sysmon_pic,
  60. NULL
  61. };
  62. struct sysmon_table_s
  63. {
  64. char * name;
  65. char * unit_name;
  66. sysmon_t * sysmon;
  67. void (*exec_before)(sysmon_table_t *);
  68. void (*exec_after)(sysmon_table_t *);
  69. int unit_precision;
  70. int unit_div;
  71. int unit_min;
  72. int unit_max;
  73. uint val_mask;
  74. uint val_min;
  75. uint val_max;
  76. int val_valid;
  77. uint val_min_alt;
  78. uint val_max_alt;
  79. int val_valid_alt;
  80. uint addr;
  81. };
  82. static sysmon_table_t sysmon_table[] =
  83. {
  84. {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable,
  85. 1, 1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x6C, 0xC6, 0, 0x27},
  86. {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable,
  87. 1, 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0xB2, 0xF1, 0, 0x29},
  88. {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL,
  89. 100, 1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0xB6, 0xC9, 0, 0x22},
  90. {"+ 5 V logic", "V", &sysmon_lm87, NULL, NULL,
  91. 100, 1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x23},
  92. {"+12 V PCMCIA", "V", &sysmon_lm87, NULL, NULL,
  93. 100, 1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0xBC, 0xD0, 0, 0x21},
  94. {"+12 V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable,
  95. 100, 1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0xB6, 0xCA, 0, 0x24},
  96. {"+ 5 V standby", "V", &sysmon_pic, NULL, NULL,
  97. 100, 1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0xC8, 0xDE, 0, 0x7C},
  98. };
  99. static int sysmon_table_size = ARRAY_SIZE(sysmon_table);
  100. static int conversion_done = 0;
  101. int sysmon_init_f (void)
  102. {
  103. sysmon_t ** l;
  104. ulong reg;
  105. /* Power on CCFL, PCMCIA */
  106. reg = pic_read (0x60);
  107. reg |= 0x09;
  108. pic_write (0x60, reg);
  109. for (l = sysmon_list; *l; l++) {
  110. (*l)->init(*l);
  111. }
  112. return 0;
  113. }
  114. void sysmon_reloc (void)
  115. {
  116. /* Do nothing for now, sysmon_reloc() is required by the sysmon post */
  117. }
  118. static char *sysmon_unit_value (sysmon_table_t *s, uint val)
  119. {
  120. static char buf[32];
  121. int unit_val =
  122. s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
  123. char *p, sign;
  124. int dec, frac;
  125. if (val == -1) {
  126. return "I/O ERROR";
  127. }
  128. if (unit_val < 0) {
  129. sign = '-';
  130. unit_val = -unit_val;
  131. } else {
  132. sign = '+';
  133. }
  134. p = buf + sprintf(buf, "%c%2d", sign, unit_val / s->unit_div);
  135. frac = unit_val % s->unit_div;
  136. frac /= (s->unit_div / s->unit_precision);
  137. dec = s->unit_precision;
  138. if (dec != 1) {
  139. *p++ = '.';
  140. }
  141. for (dec /= 10; dec != 0; dec /= 10) {
  142. *p++ = '0' + (frac / dec) % 10;
  143. }
  144. strcpy(p, s->unit_name);
  145. return buf;
  146. }
  147. static void sysmon_lm87_init (sysmon_t * this)
  148. {
  149. uchar val;
  150. /* Detect LM87 chip */
  151. if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 ||
  152. i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02) {
  153. printf("Error: LM87 not found at 0x%02X\n", this->chip);
  154. return;
  155. }
  156. /* Configure pins 5,6 as AIN */
  157. val = 0x03;
  158. if (i2c_write(this->chip, 0x16, 1, &val, 1)) {
  159. printf("Error: can't write LM87 config register\n");
  160. return;
  161. }
  162. /* Start monitoring */
  163. val = 0x01;
  164. if (i2c_write(this->chip, 0x40, 1, &val, 1)) {
  165. printf("Error: can't write LM87 config register\n");
  166. return;
  167. }
  168. }
  169. static void sysmon_pic_init (sysmon_t * this)
  170. {
  171. }
  172. static uint sysmon_i2c_read (sysmon_t * this, uint addr)
  173. {
  174. uchar val;
  175. uint res = i2c_read(this->chip, addr, 1, &val, 1);
  176. return res == 0 ? val : -1;
  177. }
  178. static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr)
  179. {
  180. uchar val;
  181. return i2c_read(this->chip, addr, 1, &val, 1) == 0 ?
  182. 128 + (signed char)val : -1;
  183. }
  184. static void sysmon_ccfl_disable (sysmon_table_t * this)
  185. {
  186. if (!this->val_valid_alt) {
  187. sysmon_temp_invalid = 1;
  188. }
  189. }
  190. static void sysmon_ccfl_enable (sysmon_table_t * this)
  191. {
  192. ulong reg;
  193. if (!sysmon_temp_invalid) {
  194. reg = pic_read (0x60);
  195. reg |= 0x06;
  196. pic_write (0x60, reg);
  197. }
  198. }
  199. int sysmon_post_test (int flags)
  200. {
  201. int res = 0;
  202. sysmon_table_t * t;
  203. uint val;
  204. /*
  205. * The A/D conversion on the LM87 sensor takes 300 ms.
  206. */
  207. if (! conversion_done) {
  208. while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET ();
  209. conversion_done = 1;
  210. }
  211. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++) {
  212. if (t->exec_before) {
  213. t->exec_before(t);
  214. }
  215. val = t->sysmon->read(t->sysmon, t->addr);
  216. if (val != -1) {
  217. t->val_valid = val >= t->val_min && val <= t->val_max;
  218. t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
  219. } else {
  220. t->val_valid = 0;
  221. t->val_valid_alt = 0;
  222. }
  223. if (t->exec_after) {
  224. t->exec_after(t);
  225. }
  226. if ((!t->val_valid) || (flags & POST_MANUAL)) {
  227. printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
  228. printf("allowed range");
  229. printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
  230. printf(" %-8s", sysmon_unit_value(t, t->val_max));
  231. printf(" %s\n", t->val_valid ? "OK" : "FAIL");
  232. }
  233. if (!t->val_valid) {
  234. res = -1;
  235. }
  236. }
  237. return res;
  238. }
  239. #endif /* CONFIG_POST & CONFIG_SYS_POST_SYSMON */