common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * (C) Copyright 2008
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  4. *
  5. * (C) Copyright 2011
  6. * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <ioports.h>
  28. #include <command.h>
  29. #include <malloc.h>
  30. #include <hush.h>
  31. #include <net.h>
  32. #include <netdev.h>
  33. #include <asm/io.h>
  34. #include <linux/ctype.h>
  35. #if defined(CONFIG_POST)
  36. #include "post.h"
  37. #endif
  38. #include "common.h"
  39. #include <i2c.h>
  40. #if !defined(CONFIG_MPC83xx)
  41. static void i2c_write_start_seq(void);
  42. #endif
  43. DECLARE_GLOBAL_DATA_PTR;
  44. /*
  45. * Set Keymile specific environment variables
  46. * Currently only some memory layout variables are calculated here
  47. * ... ------------------------------------------------
  48. * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
  49. * ... |<------------------- pram ------------------->|
  50. * ... ------------------------------------------------
  51. * @END_OF_RAM: denotes the RAM size
  52. * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
  53. * @pram : preserved ram size in k
  54. * @varaddr : startadress for /var mounted into RAM
  55. */
  56. int set_km_env(void)
  57. {
  58. uchar buf[32];
  59. unsigned int pnvramaddr;
  60. unsigned int pram;
  61. unsigned int varaddr;
  62. unsigned int kernelmem;
  63. char *p;
  64. unsigned long rootfssize = 0;
  65. pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
  66. - CONFIG_KM_PNVRAM;
  67. sprintf((char *)buf, "0x%x", pnvramaddr);
  68. setenv("pnvramaddr", (char *)buf);
  69. /* try to read rootfssize (ram image) from envrionment */
  70. p = getenv("rootfssize");
  71. if (p != NULL)
  72. strict_strtoul(p, 16, &rootfssize);
  73. pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
  74. CONFIG_KM_PNVRAM) / 0x400;
  75. sprintf((char *)buf, "0x%x", pram);
  76. setenv("pram", (char *)buf);
  77. varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
  78. sprintf((char *)buf, "0x%x", varaddr);
  79. setenv("varaddr", (char *)buf);
  80. kernelmem = gd->ram_size - 0x400 * pram;
  81. sprintf((char *)buf, "0x%x", kernelmem);
  82. setenv("kernelmem", (char *)buf);
  83. return 0;
  84. }
  85. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  86. #if !defined(CONFIG_MPC83xx)
  87. static void i2c_write_start_seq(void)
  88. {
  89. set_sda(1);
  90. udelay(DELAY_HALF_PERIOD);
  91. set_scl(1);
  92. udelay(DELAY_HALF_PERIOD);
  93. set_sda(0);
  94. udelay(DELAY_HALF_PERIOD);
  95. set_scl(0);
  96. udelay(DELAY_HALF_PERIOD);
  97. }
  98. /*
  99. * I2C is a synchronous protocol and resets of the processor in the middle
  100. * of an access can block the I2C Bus until a powerdown of the full unit is
  101. * done. This function toggles the SCL until the SCL and SCA line are
  102. * released, but max. 16 times, after this a I2C start-sequence is sent.
  103. * This I2C Deblocking mechanism was developed by Keymile in association
  104. * with Anatech and Atmel in 1998.
  105. */
  106. int i2c_make_abort(void)
  107. {
  108. #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
  109. immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  110. i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  111. /*
  112. * disable I2C controller first, otherwhise it thinks we want to
  113. * talk to the slave port...
  114. */
  115. clrbits_8(&i2c->i2c_i2mod, 0x01);
  116. /* Set the PortPins to GPIO */
  117. setports(1);
  118. #endif
  119. int scl_state = 0;
  120. int sda_state = 0;
  121. int i = 0;
  122. int ret = 0;
  123. if (!get_sda()) {
  124. ret = -1;
  125. while (i < 16) {
  126. i++;
  127. set_scl(0);
  128. udelay(DELAY_ABORT_SEQ);
  129. set_scl(1);
  130. udelay(DELAY_ABORT_SEQ);
  131. scl_state = get_scl();
  132. sda_state = get_sda();
  133. if (scl_state && sda_state) {
  134. ret = 0;
  135. break;
  136. }
  137. }
  138. }
  139. if (ret == 0)
  140. for (i = 0; i < 5; i++)
  141. i2c_write_start_seq();
  142. /* respect stop setup time */
  143. udelay(DELAY_ABORT_SEQ);
  144. set_scl(1);
  145. udelay(DELAY_ABORT_SEQ);
  146. set_sda(1);
  147. get_sda();
  148. #if defined(CONFIG_HARD_I2C)
  149. /* Set the PortPins back to use for I2C */
  150. setports(0);
  151. #endif
  152. return ret;
  153. }
  154. #endif
  155. /**
  156. * i2c_init_board - reset i2c bus. When the board is powercycled during a
  157. * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
  158. */
  159. void i2c_init_board(void)
  160. {
  161. /* Now run the AbortSequence() */
  162. i2c_make_abort();
  163. }
  164. #endif
  165. int board_eth_init(bd_t *bis)
  166. {
  167. if (ethernet_present())
  168. return cpu_eth_init(bis);
  169. return -1;
  170. }
  171. /*
  172. * do_setboardid command
  173. * read out the board id and the hw key from the intventory EEPROM and set
  174. * this values as environment variables.
  175. */
  176. static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
  177. char *const argv[])
  178. {
  179. unsigned char buf[32];
  180. char *p;
  181. p = get_local_var("IVM_BoardId");
  182. if (p == NULL) {
  183. printf("can't get the IVM_Boardid\n");
  184. return 1;
  185. }
  186. sprintf((char *)buf, "%s", p);
  187. setenv("boardid", (char *)buf);
  188. printf("set boardid=%s\n", buf);
  189. p = get_local_var("IVM_HWKey");
  190. if (p == NULL) {
  191. printf("can't get the IVM_HWKey\n");
  192. return 1;
  193. }
  194. sprintf((char *)buf, "%s", p);
  195. setenv("hwkey", (char *)buf);
  196. printf("set hwkey=%s\n", buf);
  197. printf("Execute manually saveenv for persistent storage.\n");
  198. return 0;
  199. }
  200. U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
  201. "hwkey from IVM and set in environment");
  202. /*
  203. * command km_checkbidhwk
  204. * if "boardid" and "hwkey" are not already set in the environment, do:
  205. * if a "boardIdListHex" exists in the environment:
  206. * - read ivm data for boardid and hwkey
  207. * - compare each entry of the boardIdListHex with the
  208. * IVM data:
  209. * if match:
  210. * set environment variables boardid, boardId,
  211. * hwkey, hwKey to the found values
  212. * both (boardid and boardId) are set because
  213. * they might be used differently in the
  214. * application and in the init scripts (?)
  215. * return 0 in case of match, 1 if not match or error
  216. */
  217. int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
  218. char *const argv[])
  219. {
  220. unsigned long ivmbid = 0, ivmhwkey = 0;
  221. unsigned long envbid = 0, envhwkey = 0;
  222. char *p;
  223. int verbose = argc > 1 && *argv[1] == 'v';
  224. int rc = 0;
  225. /*
  226. * first read out the real inventory values, these values are
  227. * already stored in the local hush variables
  228. */
  229. p = get_local_var("IVM_BoardId");
  230. if (p == NULL) {
  231. printf("can't get the IVM_Boardid\n");
  232. return 1;
  233. }
  234. rc = strict_strtoul(p, 16, &ivmbid);
  235. p = get_local_var("IVM_HWKey");
  236. if (p == NULL) {
  237. printf("can't get the IVM_HWKey\n");
  238. return 1;
  239. }
  240. rc = strict_strtoul(p, 16, &ivmhwkey);
  241. if (!ivmbid || !ivmhwkey) {
  242. printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
  243. return rc;
  244. }
  245. /* now try to read values from environment if available */
  246. p = getenv("boardid");
  247. if (p != NULL)
  248. rc = strict_strtoul(p, 16, &envbid);
  249. p = getenv("hwkey");
  250. if (p != NULL)
  251. rc = strict_strtoul(p, 16, &envhwkey);
  252. if (rc != 0) {
  253. printf("strict_strtoul returns error: %d", rc);
  254. return rc;
  255. }
  256. if (!envbid || !envhwkey) {
  257. /*
  258. * BoardId/HWkey not available in the environment, so try the
  259. * environment variable for BoardId/HWkey list
  260. */
  261. char *bidhwklist = getenv("boardIdListHex");
  262. if (bidhwklist) {
  263. int found = 0;
  264. char *rest = bidhwklist;
  265. char *endp;
  266. if (verbose) {
  267. printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
  268. ivmbid, ivmhwkey);
  269. printf("boardIdHwKeyList: %s\n",
  270. bidhwklist);
  271. }
  272. while (!found) {
  273. /* loop over each bid/hwkey pair in the list */
  274. unsigned long bid = 0;
  275. unsigned long hwkey = 0;
  276. while (*rest && !isxdigit(*rest))
  277. rest++;
  278. /*
  279. * use simple_strtoul because we need &end and
  280. * we know we got non numeric char at the end
  281. */
  282. bid = simple_strtoul(rest, &endp, 16);
  283. /* BoardId and HWkey are separated with a "_" */
  284. if (*endp == '_') {
  285. rest = endp + 1;
  286. /*
  287. * use simple_strtoul because we need
  288. * &end
  289. */
  290. hwkey = simple_strtoul(rest, &endp, 16);
  291. rest = endp;
  292. while (*rest && !isxdigit(*rest))
  293. rest++;
  294. }
  295. if ((!bid) || (!hwkey)) {
  296. /* end of list */
  297. break;
  298. }
  299. if (verbose) {
  300. printf("trying bid=0x%lX, hwkey=%ld\n",
  301. bid, hwkey);
  302. }
  303. /*
  304. * Compare the values of the found entry in the
  305. * list with the valid values which are stored
  306. * in the inventory eeprom. If they are equal
  307. * set the values in environment variables.
  308. */
  309. if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
  310. char buf[10];
  311. found = 1;
  312. envbid = bid;
  313. envhwkey = hwkey;
  314. sprintf(buf, "%lx", bid);
  315. setenv("boardid", buf);
  316. sprintf(buf, "%lx", hwkey);
  317. setenv("hwkey", buf);
  318. }
  319. } /* end while( ! found ) */
  320. }
  321. }
  322. /* compare now the values */
  323. if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
  324. printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
  325. rc = 0; /* match */
  326. } else {
  327. printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
  328. envhwkey);
  329. printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
  330. rc = 1; /* don't match */
  331. }
  332. return rc;
  333. }
  334. U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
  335. "check boardid and hwkey",
  336. "[v]\n - check environment parameter "\
  337. "\"boardIdListHex\" against stored boardid and hwkey "\
  338. "from the IVM\n v: verbose output"
  339. );
  340. /*
  341. * command km_checktestboot
  342. * if the testpin of the board is asserted, return 1
  343. * * else return 0
  344. */
  345. int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
  346. char *const argv[])
  347. {
  348. int testpin = 0;
  349. char *s = NULL;
  350. int testboot = 0;
  351. int verbose = argc > 1 && *argv[1] == 'v';
  352. #if defined(CONFIG_POST)
  353. testpin = post_hotkeys_pressed();
  354. s = getenv("test_bank");
  355. #endif
  356. /* when test_bank is not set, act as if testpin is not asserted */
  357. testboot = (testpin != 0) && (s);
  358. if (verbose) {
  359. printf("testpin = %d\n", testpin);
  360. printf("test_bank = %s\n", s ? s : "not set");
  361. printf("boot test app : %s\n", (testboot) ? "yes" : "no");
  362. }
  363. /* return 0 means: testboot, therefore we need the inversion */
  364. return !testboot;
  365. }
  366. U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
  367. "check if testpin is asserted",
  368. "[v]\n v - verbose output"
  369. );