common.c 11 KB

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