common.c 9.2 KB

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