sysmon.c 7.1 KB

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