common.c 9.1 KB

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