common.c 11 KB

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