common.c 11 KB

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