sysmon.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_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 addr;
  94. };
  95. static sysmon_table_t sysmon_table[] =
  96. {
  97. {"Board temperature", " C", &sysmon_lm87_sgn, NULL, sysmon_ccfl_disable,
  98. 1, -128, 127, 0xFF, 0x58, 0xD5, 0, 0x27},
  99. {"Front temperature", " C", &sysmon_lm87, NULL, sysmon_ccfl_disable,
  100. 100, -27316, 8984, 0xFF, 0xA4, 0xFC, 0, 0x29},
  101. {"+3.3V CPU logic", "V", &sysmon_lm87, NULL, NULL,
  102. 1000, 0, 4386, 0xFF, 0xB6, 0xC9, 0, 0x22},
  103. {"+5V logic", "V", &sysmon_lm87, NULL, NULL,
  104. 1000, 0, 6630, 0xFF, 0xB6, 0xCA, 0, 0x23},
  105. {"+12V PCMCIA", "V", &sysmon_lm87, NULL, NULL,
  106. 1000, 0, 15460, 0xFF, 0xBC, 0xD0, 0, 0x21},
  107. {"+12V CCFL", "V", &sysmon_lm87, NULL, sysmon_ccfl_enable,
  108. 1000, 0, 15900, 0xFF, 0xB6, 0xCA, 0, 0x24},
  109. {"+5V standby", "V", &sysmon_pic, NULL, NULL,
  110. 1000, 0, 6040, 0xFF, 0xC8, 0xDE, 0, 0x7C},
  111. };
  112. static int sysmon_table_size = sizeof(sysmon_table) / sizeof(sysmon_table[0]);
  113. static int conversion_done = 0;
  114. int sysmon_init_f (void)
  115. {
  116. sysmon_t ** l;
  117. ulong reg;
  118. /* Power on CCFL, PCMCIA */
  119. reg = pic_read (0x60);
  120. reg |= 0x09;
  121. pic_write (0x60, reg);
  122. for (l = sysmon_list; *l; l++)
  123. {
  124. (*l)->init(*l);
  125. }
  126. return 0;
  127. }
  128. void sysmon_reloc (void)
  129. {
  130. DECLARE_GLOBAL_DATA_PTR;
  131. sysmon_t ** l;
  132. sysmon_table_t * t;
  133. for (l = sysmon_list; *l; l++)
  134. {
  135. RELOC(*l);
  136. RELOC((*l)->init);
  137. RELOC((*l)->read);
  138. }
  139. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
  140. {
  141. RELOC(t->exec_before);
  142. RELOC(t->exec_after);
  143. RELOC(t->sysmon);
  144. }
  145. }
  146. static char * sysmon_unit_value (sysmon_table_t * s, uint val)
  147. {
  148. static char buf[32];
  149. int unit_val =
  150. s->unit_min + (s->unit_max - s->unit_min) * val / s->val_mask;
  151. char * p;
  152. int dec, frac;
  153. sprintf(buf, "%+d", unit_val / s->unit_div);
  154. frac = (unit_val > 0 ? unit_val : -unit_val) % s->unit_div;
  155. p = buf + strlen(buf);
  156. dec = s->unit_div;
  157. if (dec != 1)
  158. {
  159. *p++ = '.';
  160. }
  161. for (dec /= 10; dec != 0; dec /= 10)
  162. {
  163. *p++ = '0' + frac / dec % 10;
  164. }
  165. strcpy(p, s->unit_name);
  166. return buf;
  167. }
  168. static void sysmon_lm87_init (sysmon_t * this)
  169. {
  170. uchar val;
  171. /* Detect LM87 chip */
  172. if (i2c_read(this->chip, 0x40, 1, &val, 1) || (val & 0x80) != 0 ||
  173. i2c_read(this->chip, 0x3E, 1, &val, 1) || val != 0x02)
  174. {
  175. printf("Error: LM87 not found at 0x%02X\n", this->chip);
  176. return;
  177. }
  178. /* Configure pins 5,6 as AIN */
  179. val = 0x03;
  180. if (i2c_write(this->chip, 0x16, 1, &val, 1))
  181. {
  182. printf("Error: can't write LM87 config register\n");
  183. return;
  184. }
  185. /* Start monitoring */
  186. val = 0x01;
  187. if (i2c_write(this->chip, 0x40, 1, &val, 1))
  188. {
  189. printf("Error: can't write LM87 config register\n");
  190. return;
  191. }
  192. }
  193. static void sysmon_pic_init (sysmon_t * this)
  194. {
  195. }
  196. static uint sysmon_i2c_read (sysmon_t * this, uint addr)
  197. {
  198. uchar val;
  199. uint res = i2c_read(this->chip, addr, 1, &val, 1);
  200. return res == 0 ? val : -1;
  201. }
  202. static uint sysmon_i2c_read_sgn (sysmon_t * this, uint addr)
  203. {
  204. uchar val;
  205. return i2c_read(this->chip, addr, 1, &val, 1) == 0 ?
  206. 128 + (signed char)val : -1;
  207. }
  208. static void sysmon_ccfl_disable (sysmon_table_t * this)
  209. {
  210. if (!this->val_valid)
  211. {
  212. sysmon_temp_invalid = 1;
  213. }
  214. }
  215. static void sysmon_ccfl_enable (sysmon_table_t * this)
  216. {
  217. ulong reg;
  218. if (!sysmon_temp_invalid)
  219. {
  220. reg = pic_read (0x60);
  221. reg |= 0x02;
  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. {
  236. while (post_time_ms(gd->post_init_f_time) < 300) WATCHDOG_RESET ();
  237. conversion_done = 1;
  238. }
  239. for (t = sysmon_table; t < sysmon_table + sysmon_table_size; t ++)
  240. {
  241. if (t->exec_before)
  242. {
  243. t->exec_before(t);
  244. }
  245. val = t->sysmon->read(t->sysmon, t->addr);
  246. t->val_valid = val >= t->val_min && val <= t->val_max;
  247. if (t->exec_after)
  248. {
  249. t->exec_after(t);
  250. }
  251. if ((!t->val_valid) || (flags & POST_MANUAL))
  252. {
  253. printf("%-17s = %-10s ", t->name, sysmon_unit_value(t, val));
  254. printf("allowed range");
  255. printf(" %-8s ..", sysmon_unit_value(t, t->val_min));
  256. printf(" %-8s", sysmon_unit_value(t, t->val_max));
  257. printf(" %s\n", t->val_valid ? "OK" : "FAIL");
  258. }
  259. if (!t->val_valid)
  260. {
  261. res = -1;
  262. }
  263. }
  264. return res;
  265. }
  266. #endif /* CONFIG_POST & CFG_POST_SYSMON */
  267. #endif /* CONFIG_POST */