sysmon.c 7.3 KB

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