common.c 11 KB

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