common.c 16 KB

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