common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * (C) Copyright 2008
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #if defined(CONFIG_MGCOGE) || defined(CONFIG_MGCOGE2NE)
  25. #include <mpc8260.h>
  26. #endif
  27. #include <ioports.h>
  28. #include <malloc.h>
  29. #include <hush.h>
  30. #include <net.h>
  31. #include <netdev.h>
  32. #include <asm/io.h>
  33. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
  34. #include <libfdt.h>
  35. #endif
  36. #include "../common/common.h"
  37. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  38. #include <i2c.h>
  39. static void i2c_write_start_seq(void);
  40. static int i2c_make_abort(void);
  41. int ivm_calc_crc(unsigned char *buf, int len)
  42. {
  43. const unsigned short crc_tab[16] = {
  44. 0x0000, 0xCC01, 0xD801, 0x1400,
  45. 0xF001, 0x3C00, 0x2800, 0xE401,
  46. 0xA001, 0x6C00, 0x7800, 0xB401,
  47. 0x5000, 0x9C01, 0x8801, 0x4400};
  48. unsigned short crc = 0; /* final result */
  49. unsigned short r1 = 0; /* temp */
  50. unsigned char byte = 0; /* input buffer */
  51. int i;
  52. /* calculate CRC from array data */
  53. for (i = 0; i < len; i++) {
  54. byte = buf[i];
  55. /* lower 4 bits */
  56. r1 = crc_tab[crc & 0xF];
  57. crc = ((crc) >> 4) & 0x0FFF;
  58. crc = crc ^ r1 ^ crc_tab[byte & 0xF];
  59. /* upper 4 bits */
  60. r1 = crc_tab[crc & 0xF];
  61. crc = (crc >> 4) & 0x0FFF;
  62. crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
  63. }
  64. return crc;
  65. }
  66. static int ivm_set_value(char *name, char *value)
  67. {
  68. char tempbuf[256];
  69. if (value != NULL) {
  70. sprintf(tempbuf, "%s=%s", name, value);
  71. return set_local_var(tempbuf, 0);
  72. } else {
  73. unset_local_var(name);
  74. }
  75. return 0;
  76. }
  77. static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
  78. int check)
  79. {
  80. unsigned short val;
  81. unsigned char valbuf[30];
  82. if ((buf[off + 0] != buf[off + 2]) &&
  83. (buf[off + 2] != buf[off + 4])) {
  84. printf("%s Error corrupted %s\n", __func__, name);
  85. val = -1;
  86. } else {
  87. val = buf[off + 0] + (buf[off + 1] << 8);
  88. if ((val == 0) && (check == 1))
  89. val = -1;
  90. }
  91. sprintf((char *)valbuf, "%x", val);
  92. ivm_set_value(name, (char *)valbuf);
  93. return val;
  94. }
  95. #define INV_BLOCKSIZE 0x100
  96. #define INV_DATAADDRESS 0x21
  97. #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
  98. #define IVM_POS_SHORT_TEXT 0
  99. #define IVM_POS_MANU_ID 1
  100. #define IVM_POS_MANU_SERIAL 2
  101. #define IVM_POS_PART_NUMBER 3
  102. #define IVM_POS_BUILD_STATE 4
  103. #define IVM_POS_SUPPLIER_PART_NUMBER 5
  104. #define IVM_POS_DELIVERY_DATE 6
  105. #define IVM_POS_SUPPLIER_BUILD_STATE 7
  106. #define IVM_POS_CUSTOMER_ID 8
  107. #define IVM_POS_CUSTOMER_PROD_ID 9
  108. #define IVM_POS_HISTORY 10
  109. #define IVM_POS_SYMBOL_ONLY 11
  110. static char convert_char(char c)
  111. {
  112. return (c < ' ' || c > '~') ? '.' : c;
  113. }
  114. static int ivm_findinventorystring(int type,
  115. unsigned char* const string,
  116. unsigned long maxlen,
  117. unsigned char *buf)
  118. {
  119. int xcode = 0;
  120. unsigned long cr = 0;
  121. unsigned long addr = INV_DATAADDRESS;
  122. unsigned long size = 0;
  123. unsigned long nr = type;
  124. int stop = 0; /* stop on semicolon */
  125. memset(string, '\0', maxlen);
  126. switch (type) {
  127. case IVM_POS_SYMBOL_ONLY:
  128. nr = 0;
  129. stop= 1;
  130. break;
  131. default:
  132. nr = type;
  133. stop = 0;
  134. }
  135. /* Look for the requested number of CR. */
  136. while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
  137. if ((buf[addr] == '\r')) {
  138. cr++;
  139. }
  140. addr++;
  141. }
  142. /*
  143. * the expected number of CR was found until the end of the IVM
  144. * content --> fill string
  145. */
  146. if (addr < INVENTORYDATASIZE) {
  147. /* Copy the IVM string in the corresponding string */
  148. for (; (buf[addr] != '\r') &&
  149. ((buf[addr] != ';') || (!stop)) &&
  150. (size < (maxlen - 1) &&
  151. (addr < INVENTORYDATASIZE)); addr++)
  152. {
  153. size += sprintf((char *)string + size, "%c",
  154. convert_char (buf[addr]));
  155. }
  156. /*
  157. * copy phase is done: check if everything is ok. If not,
  158. * the inventory data is most probably corrupted: tell
  159. * the world there is a problem!
  160. */
  161. if (addr == INVENTORYDATASIZE) {
  162. xcode = -1;
  163. printf("Error end of string not found\n");
  164. } else if ((size >= (maxlen - 1)) &&
  165. (buf[addr] != '\r')) {
  166. xcode = -1;
  167. printf("string too long till next CR\n");
  168. }
  169. } else {
  170. /*
  171. * some CR are missing...
  172. * the inventory data is most probably corrupted
  173. */
  174. xcode = -1;
  175. printf("not enough cr found\n");
  176. }
  177. return xcode;
  178. }
  179. #define GET_STRING(name, which, len) \
  180. if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
  181. ivm_set_value(name, (char *)valbuf); \
  182. }
  183. static int ivm_check_crc(unsigned char *buf, int block)
  184. {
  185. unsigned long crc;
  186. unsigned long crceeprom;
  187. crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
  188. crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
  189. buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
  190. if (crc != crceeprom) {
  191. if (block == 0)
  192. printf("Error CRC Block: %d EEprom: calculated: \
  193. %lx EEprom: %lx\n", block, crc, crceeprom);
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. static int ivm_analyze_block2(unsigned char *buf, int len)
  199. {
  200. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  201. unsigned long count;
  202. /* IVM_MacAddress */
  203. sprintf((char *)valbuf, "%pM", buf);
  204. ivm_set_value("IVM_MacAddress", (char *)valbuf);
  205. /* if an offset is defined, add it */
  206. #if defined(CONFIG_PIGGY_MAC_ADRESS_OFFSET)
  207. if (CONFIG_PIGGY_MAC_ADRESS_OFFSET > 0) {
  208. unsigned long val = (buf[4] << 16) + (buf[5] << 8) + buf[6];
  209. val += CONFIG_PIGGY_MAC_ADRESS_OFFSET;
  210. buf[4] = (val >> 16) & 0xff;
  211. buf[5] = (val >> 8) & 0xff;
  212. buf[6] = val & 0xff;
  213. sprintf((char *)valbuf, "%pM", buf);
  214. }
  215. #endif
  216. if (getenv("ethaddr") == NULL)
  217. setenv((char *)"ethaddr", (char *)valbuf);
  218. /* IVM_MacCount */
  219. count = (buf[10] << 24) +
  220. (buf[11] << 16) +
  221. (buf[12] << 8) +
  222. buf[13];
  223. if (count == 0xffffffff)
  224. count = 1;
  225. sprintf((char *)valbuf, "%lx", count);
  226. ivm_set_value("IVM_MacCount", (char *)valbuf);
  227. return 0;
  228. }
  229. int ivm_analyze_eeprom(unsigned char *buf, int len)
  230. {
  231. unsigned short val;
  232. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  233. unsigned char *tmp;
  234. if (ivm_check_crc(buf, 0) != 0)
  235. return -1;
  236. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  237. "IVM_BoardId", 0, 1);
  238. val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  239. "IVM_HWKey", 6, 1);
  240. if (val != 0xffff) {
  241. sprintf((char *)valbuf, "%x", ((val / 100) % 10));
  242. ivm_set_value("IVM_HWVariant", (char *)valbuf);
  243. sprintf((char *)valbuf, "%x", (val % 100));
  244. ivm_set_value("IVM_HWVersion", (char *)valbuf);
  245. }
  246. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  247. "IVM_Functions", 12, 0);
  248. GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
  249. GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
  250. tmp = (unsigned char *) getenv("IVM_DeviceName");
  251. if (tmp) {
  252. int len = strlen((char *)tmp);
  253. int i = 0;
  254. while (i < len) {
  255. if (tmp[i] == ';') {
  256. ivm_set_value("IVM_ShortText",
  257. (char *)&tmp[i + 1]);
  258. break;
  259. }
  260. i++;
  261. }
  262. if (i >= len)
  263. ivm_set_value("IVM_ShortText", NULL);
  264. } else {
  265. ivm_set_value("IVM_ShortText", NULL);
  266. }
  267. GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
  268. GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
  269. GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
  270. GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
  271. GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
  272. GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
  273. GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
  274. GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
  275. GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
  276. if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
  277. return 0;
  278. ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
  279. CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
  280. return 0;
  281. }
  282. int ivm_read_eeprom(void)
  283. {
  284. #if defined(CONFIG_I2C_MUX)
  285. I2C_MUX_DEVICE *dev = NULL;
  286. #endif
  287. uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
  288. uchar *buf;
  289. unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
  290. int ret;
  291. #if defined(CONFIG_I2C_MUX)
  292. /* First init the Bus, select the Bus */
  293. #if defined(CONFIG_SYS_I2C_IVM_BUS)
  294. dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
  295. #else
  296. buf = (unsigned char *) getenv("EEprom_ivm");
  297. if (buf != NULL)
  298. dev = i2c_mux_ident_muxstring(buf);
  299. #endif
  300. if (dev == NULL) {
  301. printf("Error couldnt add Bus for IVM\n");
  302. return -1;
  303. }
  304. i2c_set_bus_num(dev->busid);
  305. #endif
  306. buf = (unsigned char *) getenv("EEprom_ivm_addr");
  307. if (buf != NULL)
  308. dev_addr = simple_strtoul((char *)buf, NULL, 16);
  309. /* add deblocking here */
  310. i2c_make_abort();
  311. ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
  312. CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  313. if (ret != 0) {
  314. printf ("Error reading EEprom\n");
  315. return -2;
  316. }
  317. return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  318. }
  319. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  320. #define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
  321. #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
  322. #if defined(CONFIG_MGCOGE) || defined(CONFIG_MGCOGE2NE)
  323. #define SDA_MASK 0x00010000
  324. #define SCL_MASK 0x00020000
  325. static void set_pin(int state, unsigned long mask)
  326. {
  327. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  328. if (state)
  329. setbits_be32(&iop->pdat, mask);
  330. else
  331. clrbits_be32(&iop->pdat, mask);
  332. setbits_be32(&iop->pdir, mask);
  333. }
  334. static int get_pin(unsigned long mask)
  335. {
  336. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  337. clrbits_be32(&iop->pdir, mask);
  338. return 0 != (in_be32(&iop->pdat) & mask);
  339. }
  340. static void set_sda(int state)
  341. {
  342. set_pin(state, SDA_MASK);
  343. }
  344. static void set_scl(int state)
  345. {
  346. set_pin(state, SCL_MASK);
  347. }
  348. static int get_sda(void)
  349. {
  350. return get_pin(SDA_MASK);
  351. }
  352. static int get_scl(void)
  353. {
  354. return get_pin(SCL_MASK);
  355. }
  356. #if defined(CONFIG_HARD_I2C)
  357. static void setports(int gpio)
  358. {
  359. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  360. if (gpio) {
  361. clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  362. clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  363. } else {
  364. setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  365. clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
  366. setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  367. }
  368. }
  369. #endif
  370. #endif
  371. #if !defined(CONFIG_MPC83xx)
  372. static void i2c_write_start_seq(void)
  373. {
  374. set_sda(1);
  375. udelay(DELAY_HALF_PERIOD);
  376. set_scl(1);
  377. udelay(DELAY_HALF_PERIOD);
  378. set_sda(0);
  379. udelay(DELAY_HALF_PERIOD);
  380. set_scl(0);
  381. udelay(DELAY_HALF_PERIOD);
  382. }
  383. /*
  384. * I2C is a synchronous protocol and resets of the processor in the middle
  385. * of an access can block the I2C Bus until a powerdown of the full unit is
  386. * done. This function toggles the SCL until the SCL and SCA line are
  387. * released, but max. 16 times, after this a I2C start-sequence is sent.
  388. * This I2C Deblocking mechanism was developed by Keymile in association
  389. * with Anatech and Atmel in 1998.
  390. */
  391. static int i2c_make_abort(void)
  392. {
  393. #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
  394. immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
  395. i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  396. /*
  397. * disable I2C controller first, otherwhise it thinks we want to
  398. * talk to the slave port...
  399. */
  400. clrbits_8(&i2c->i2c_i2mod, 0x01);
  401. /* Set the PortPins to GPIO */
  402. setports(1);
  403. #endif
  404. int scl_state = 0;
  405. int sda_state = 0;
  406. int i = 0;
  407. int ret = 0;
  408. if (!get_sda()) {
  409. ret = -1;
  410. while (i < 16) {
  411. i++;
  412. set_scl(0);
  413. udelay(DELAY_ABORT_SEQ);
  414. set_scl(1);
  415. udelay(DELAY_ABORT_SEQ);
  416. scl_state = get_scl();
  417. sda_state = get_sda();
  418. if (scl_state && sda_state) {
  419. ret = 0;
  420. break;
  421. }
  422. }
  423. }
  424. if (ret == 0)
  425. for (i = 0; i < 5; i++)
  426. i2c_write_start_seq();
  427. /* respect stop setup time */
  428. udelay(DELAY_ABORT_SEQ);
  429. set_scl(1);
  430. udelay(DELAY_ABORT_SEQ);
  431. set_sda(1);
  432. get_sda();
  433. #if defined(CONFIG_HARD_I2C)
  434. /* Set the PortPins back to use for I2C */
  435. setports(0);
  436. #endif
  437. return ret;
  438. }
  439. #endif
  440. #if defined(CONFIG_MPC83xx)
  441. static void i2c_write_start_seq(void)
  442. {
  443. struct fsl_i2c *dev;
  444. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  445. udelay(DELAY_ABORT_SEQ);
  446. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  447. udelay(DELAY_ABORT_SEQ);
  448. out_8(&dev->cr, (I2C_CR_MEN));
  449. }
  450. static int i2c_make_abort(void)
  451. {
  452. struct fsl_i2c *dev;
  453. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  454. uchar dummy;
  455. uchar last;
  456. int nbr_read = 0;
  457. int i = 0;
  458. int ret = 0;
  459. /* wait after each operation to finsh with a delay */
  460. out_8(&dev->cr, (I2C_CR_MSTA));
  461. udelay(DELAY_ABORT_SEQ);
  462. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  463. udelay(DELAY_ABORT_SEQ);
  464. dummy = in_8(&dev->dr);
  465. udelay(DELAY_ABORT_SEQ);
  466. last = in_8(&dev->dr);
  467. nbr_read++;
  468. /*
  469. * do read until the last bit is 1, but stop if the full eeprom is
  470. * read.
  471. */
  472. while (((last & 0x01) != 0x01) &&
  473. (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
  474. udelay(DELAY_ABORT_SEQ);
  475. last = in_8(&dev->dr);
  476. nbr_read++;
  477. }
  478. if ((last & 0x01) != 0x01)
  479. ret = -2;
  480. if ((last != 0xff) || (nbr_read > 1))
  481. printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
  482. nbr_read, last);
  483. udelay(DELAY_ABORT_SEQ);
  484. out_8(&dev->cr, (I2C_CR_MEN));
  485. udelay(DELAY_ABORT_SEQ);
  486. /* clear status reg */
  487. out_8(&dev->sr, 0);
  488. for (i = 0; i < 5; i++)
  489. i2c_write_start_seq();
  490. if (ret != 0)
  491. printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
  492. nbr_read, last);
  493. return ret;
  494. }
  495. #endif
  496. /**
  497. * i2c_init_board - reset i2c bus. When the board is powercycled during a
  498. * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
  499. */
  500. void i2c_init_board(void)
  501. {
  502. /* Now run the AbortSequence() */
  503. i2c_make_abort();
  504. }
  505. #endif
  506. #endif
  507. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
  508. int fdt_set_node_and_value(void *blob,
  509. char *nodename,
  510. char *regname,
  511. void *var,
  512. int size)
  513. {
  514. int ret = 0;
  515. int nodeoffset = 0;
  516. nodeoffset = fdt_path_offset(blob, nodename);
  517. if (nodeoffset >= 0) {
  518. ret = fdt_setprop(blob, nodeoffset, regname, var,
  519. size);
  520. if (ret < 0)
  521. printf("ft_blob_update(): cannot set %s/%s "
  522. "property err:%s\n", nodename, regname,
  523. fdt_strerror(ret));
  524. } else {
  525. printf("ft_blob_update(): cannot find %s node "
  526. "err:%s\n", nodename, fdt_strerror(nodeoffset));
  527. }
  528. return ret;
  529. }
  530. int fdt_get_node_and_value(void *blob,
  531. char *nodename,
  532. char *propname,
  533. void **var)
  534. {
  535. int len;
  536. int nodeoffset = 0;
  537. nodeoffset = fdt_path_offset(blob, nodename);
  538. if (nodeoffset >= 0) {
  539. *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len);
  540. if (len == 0) {
  541. /* no value */
  542. printf("%s no value\n", __func__);
  543. return -1;
  544. } else if (len > 0) {
  545. return len;
  546. } else {
  547. printf("libfdt fdt_getprop(): %s\n",
  548. fdt_strerror(len));
  549. return -2;
  550. }
  551. } else {
  552. printf("%s: cannot find %s node err:%s\n", __func__,
  553. nodename, fdt_strerror(nodeoffset));
  554. return -3;
  555. }
  556. }
  557. #endif
  558. #if !defined(MACH_TYPE_KM_KIRKWOOD)
  559. int ethernet_present(void)
  560. {
  561. struct km_bec_fpga *base =
  562. (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
  563. return in_8(&base->bprth) & PIGGY_PRESENT;
  564. }
  565. #endif
  566. int board_eth_init(bd_t *bis)
  567. {
  568. #ifdef CONFIG_KEYMILE_HDLC_ENET
  569. (void)keymile_hdlc_enet_initialize(bis);
  570. #endif
  571. if (ethernet_present())
  572. return cpu_eth_init(bis);
  573. return -1;
  574. }