common.c 12 KB

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