common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. printf("[INFO] i2c abort after %d clocks\n", i);
  138. break;
  139. }
  140. }
  141. }
  142. if (ret == 0)
  143. for (i = 0; i < 5; i++)
  144. i2c_write_start_seq();
  145. else
  146. printf("[ERROR] i2c abort failed\n");
  147. /* respect stop setup time */
  148. udelay(DELAY_ABORT_SEQ);
  149. set_scl(1);
  150. udelay(DELAY_ABORT_SEQ);
  151. set_sda(1);
  152. get_sda();
  153. #if defined(CONFIG_HARD_I2C)
  154. /* Set the PortPins back to use for I2C */
  155. setports(0);
  156. #endif
  157. return ret;
  158. }
  159. #endif
  160. /**
  161. * i2c_init_board - reset i2c bus. When the board is powercycled during a
  162. * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
  163. */
  164. void i2c_init_board(void)
  165. {
  166. /* Now run the AbortSequence() */
  167. i2c_make_abort();
  168. }
  169. #endif
  170. #if !defined(MACH_TYPE_KM_KIRKWOOD)
  171. int ethernet_present(void)
  172. {
  173. struct km_bec_fpga *base =
  174. (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
  175. return in_8(&base->bprth) & PIGGY_PRESENT;
  176. }
  177. #endif
  178. int board_eth_init(bd_t *bis)
  179. {
  180. if (ethernet_present())
  181. return cpu_eth_init(bis);
  182. return -1;
  183. }
  184. /*
  185. * do_setboardid command
  186. * read out the board id and the hw key from the intventory EEPROM and set
  187. * this values as environment variables.
  188. */
  189. static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
  190. char *const argv[])
  191. {
  192. unsigned char buf[32];
  193. char *p;
  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. sprintf((char *)buf, "%s", p);
  200. setenv("boardid", (char *)buf);
  201. printf("set boardid=%s\n", buf);
  202. p = get_local_var("IVM_HWKey");
  203. if (p == NULL) {
  204. printf("can't get the IVM_HWKey\n");
  205. return 1;
  206. }
  207. sprintf((char *)buf, "%s", p);
  208. setenv("hwkey", (char *)buf);
  209. printf("set hwkey=%s\n", buf);
  210. printf("Execute manually saveenv for persistent storage.\n");
  211. return 0;
  212. }
  213. U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
  214. "hwkey from IVM and set in environment");
  215. /*
  216. * command km_checkbidhwk
  217. * if "boardid" and "hwkey" are not already set in the environment, do:
  218. * if a "boardIdListHex" exists in the environment:
  219. * - read ivm data for boardid and hwkey
  220. * - compare each entry of the boardIdListHex with the
  221. * IVM data:
  222. * if match:
  223. * set environment variables boardid, boardId,
  224. * hwkey, hwKey to the found values
  225. * both (boardid and boardId) are set because
  226. * they might be used differently in the
  227. * application and in the init scripts (?)
  228. * return 0 in case of match, 1 if not match or error
  229. */
  230. int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
  231. char *const argv[])
  232. {
  233. unsigned long ivmbid = 0, ivmhwkey = 0;
  234. unsigned long envbid = 0, envhwkey = 0;
  235. char *p;
  236. int verbose = argc > 1 && *argv[1] == 'v';
  237. int rc = 0;
  238. /*
  239. * first read out the real inventory values, these values are
  240. * already stored in the local hush variables
  241. */
  242. p = get_local_var("IVM_BoardId");
  243. if (p == NULL) {
  244. printf("can't get the IVM_Boardid\n");
  245. return 1;
  246. }
  247. rc = strict_strtoul(p, 16, &ivmbid);
  248. p = get_local_var("IVM_HWKey");
  249. if (p == NULL) {
  250. printf("can't get the IVM_HWKey\n");
  251. return 1;
  252. }
  253. rc = strict_strtoul(p, 16, &ivmhwkey);
  254. if (!ivmbid || !ivmhwkey) {
  255. printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
  256. return rc;
  257. }
  258. /* now try to read values from environment if available */
  259. p = getenv("boardid");
  260. if (p != NULL)
  261. rc = strict_strtoul(p, 16, &envbid);
  262. p = getenv("hwkey");
  263. if (p != NULL)
  264. rc = strict_strtoul(p, 16, &envhwkey);
  265. if (rc != 0) {
  266. printf("strict_strtoul returns error: %d", rc);
  267. return rc;
  268. }
  269. if (!envbid || !envhwkey) {
  270. /*
  271. * BoardId/HWkey not available in the environment, so try the
  272. * environment variable for BoardId/HWkey list
  273. */
  274. char *bidhwklist = getenv("boardIdListHex");
  275. if (bidhwklist) {
  276. int found = 0;
  277. char *rest = bidhwklist;
  278. char *endp;
  279. if (verbose) {
  280. printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
  281. ivmbid, ivmhwkey);
  282. printf("boardIdHwKeyList: %s\n",
  283. bidhwklist);
  284. }
  285. while (!found) {
  286. /* loop over each bid/hwkey pair in the list */
  287. unsigned long bid = 0;
  288. unsigned long hwkey = 0;
  289. while (*rest && !isxdigit(*rest))
  290. rest++;
  291. /*
  292. * use simple_strtoul because we need &end and
  293. * we know we got non numeric char at the end
  294. */
  295. bid = simple_strtoul(rest, &endp, 16);
  296. /* BoardId and HWkey are separated with a "_" */
  297. if (*endp == '_') {
  298. rest = endp + 1;
  299. /*
  300. * use simple_strtoul because we need
  301. * &end
  302. */
  303. hwkey = simple_strtoul(rest, &endp, 16);
  304. rest = endp;
  305. while (*rest && !isxdigit(*rest))
  306. rest++;
  307. }
  308. if ((!bid) || (!hwkey)) {
  309. /* end of list */
  310. break;
  311. }
  312. if (verbose) {
  313. printf("trying bid=0x%lX, hwkey=%ld\n",
  314. bid, hwkey);
  315. }
  316. /*
  317. * Compare the values of the found entry in the
  318. * list with the valid values which are stored
  319. * in the inventory eeprom. If they are equal
  320. * set the values in environment variables.
  321. */
  322. if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
  323. char buf[10];
  324. found = 1;
  325. envbid = bid;
  326. envhwkey = hwkey;
  327. sprintf(buf, "%lx", bid);
  328. setenv("boardid", buf);
  329. sprintf(buf, "%lx", hwkey);
  330. setenv("hwkey", buf);
  331. }
  332. } /* end while( ! found ) */
  333. }
  334. }
  335. /* compare now the values */
  336. if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
  337. printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
  338. rc = 0; /* match */
  339. } else {
  340. printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
  341. envhwkey);
  342. printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
  343. rc = 1; /* don't match */
  344. }
  345. return rc;
  346. }
  347. U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
  348. "check boardid and hwkey",
  349. "[v]\n - check environment parameter "\
  350. "\"boardIdListHex\" against stored boardid and hwkey "\
  351. "from the IVM\n v: verbose output"
  352. );
  353. /*
  354. * command km_checktestboot
  355. * if the testpin of the board is asserted, return 1
  356. * * else return 0
  357. */
  358. int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
  359. char *const argv[])
  360. {
  361. int testpin = 0;
  362. char *s = NULL;
  363. int testboot = 0;
  364. int verbose = argc > 1 && *argv[1] == 'v';
  365. #if defined(CONFIG_POST)
  366. testpin = post_hotkeys_pressed();
  367. s = getenv("test_bank");
  368. #endif
  369. /* when test_bank is not set, act as if testpin is not asserted */
  370. testboot = (testpin != 0) && (s);
  371. if (verbose) {
  372. printf("testpin = %d\n", testpin);
  373. printf("test_bank = %s\n", s ? s : "not set");
  374. printf("boot test app : %s\n", (testboot) ? "yes" : "no");
  375. }
  376. /* return 0 means: testboot, therefore we need the inversion */
  377. return !testboot;
  378. }
  379. U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
  380. "check if testpin is asserted",
  381. "[v]\n v - verbose output"
  382. );