sysmon.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, 0x67, 0xC6, 0, 0x27},
  103. {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable,
  104. 1, 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0xAE, 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. {
  128. (*l)->init(*l);
  129. }
  130. return 0;
  131. }
  132. void sysmon_reloc (void)
  133. {
  134. DECLARE_GLOBAL_DATA_PTR;
  135. sysmon_t ** l;
  136. sysmon_table_t * t;
  137. for (l = sysmon_list; *l; l++)
  138. {
  139. RELOC(*l);
  140. RELOC((*l)->init);
  141. RELOC((*l)->read);
  142. }
  143. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
  144. {
  145. RELOC(t->exec_before);
  146. RELOC(t->exec_after);
  147. RELOC(t->sysmon);
  148. }
  149. }
  150. static char *sysmon_unit_value (sysmon_table_t *s, uint val)
  151. {
  152. static char buf[32];
  153. int unit_val =
  154. s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
  155. char *p, sign;
  156. int dec, frac;
  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. {
  169. *p++ = '.';
  170. }
  171. for (dec /= 10; dec != 0; dec /= 10)
  172. {
  173. *p++ = '0' + (frac / dec) % 10;
  174. }
  175. strcpy(p, s->unit_name);
  176. return buf;
  177. }
  178. static void sysmon_lm87_init (sysmon_t * this)
  179. {
  180. uchar val;
  181. /* Detect LM87 chip */
  182. if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 ||
  183. i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02)
  184. {
  185. printf("Error: LM87 not found at 0x%02X\n", this->chip);
  186. return;
  187. }
  188. /* Configure pins 5,6 as AIN */
  189. val = 0x03;
  190. if (i2c_write(this->chip, 0x16, 1, &val, 1))
  191. {
  192. printf("Error: can't write LM87 config register\n");
  193. return;
  194. }
  195. /* Start monitoring */
  196. val = 0x01;
  197. if (i2c_write(this->chip, 0x40, 1, &val, 1))
  198. {
  199. printf("Error: can't write LM87 config register\n");
  200. return;
  201. }
  202. }
  203. static void sysmon_pic_init (sysmon_t * this)
  204. {
  205. }
  206. static uint sysmon_i2c_read (sysmon_t * this, uint addr)
  207. {
  208. uchar val;
  209. uint res = i2c_read(this->chip, addr, 1, &val, 1);
  210. return res == 0 ? val : -1;
  211. }
  212. static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr)
  213. {
  214. uchar val;
  215. return i2c_read(this->chip, addr, 1, &val, 1) == 0 ?
  216. 128 + (signed char)val : -1;
  217. }
  218. static void sysmon_ccfl_disable (sysmon_table_t * this)
  219. {
  220. if (!this->val_valid_alt)
  221. {
  222. sysmon_temp_invalid = 1;
  223. }
  224. }
  225. static void sysmon_ccfl_enable (sysmon_table_t * this)
  226. {
  227. ulong reg;
  228. if (!sysmon_temp_invalid)
  229. {
  230. reg = pic_read (0x60);
  231. reg |= 0x02;
  232. pic_write (0x60, reg);
  233. }
  234. }
  235. int sysmon_post_test (int flags)
  236. {
  237. DECLARE_GLOBAL_DATA_PTR;
  238. int res = 0;
  239. sysmon_table_t * t;
  240. uint val;
  241. /*
  242. * The A/D conversion on the LM87 sensor takes 300 ms.
  243. */
  244. if (! conversion_done)
  245. {
  246. while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET ();
  247. conversion_done = 1;
  248. }
  249. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
  250. {
  251. if (t->exec_before)
  252. {
  253. t->exec_before(t);
  254. }
  255. val = t->sysmon->read(t->sysmon, t->addr);
  256. t->val_valid = val >= t->val_min && val <= t->val_max;
  257. t->val_valid_alt = val >= t->val_min_alt && val <= t->val_max_alt;
  258. if (t->exec_after)
  259. {
  260. t->exec_after(t);
  261. }
  262. if ((!t->val_valid) || (flags & POST_MANUAL))
  263. {
  264. printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
  265. printf("allowed range");
  266. printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
  267. printf(" %-8s", sysmon_unit_value(t, t->val_max));
  268. printf(" %s\n", t->val_valid ? "OK" : "FAIL");
  269. }
  270. if (!t->val_valid)
  271. {
  272. res = -1;
  273. }
  274. }
  275. return res;
  276. }
  277. #endif /* CONFIG_POST & CFG_POST_SYSMON */
  278. #endif /* CONFIG_POST */