sysmon.c 7.4 KB

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