common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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_KM82XX)
  25. #include <mpc8260.h>
  26. #endif
  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/common.h"
  39. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  40. #include <i2c.h>
  41. static void i2c_write_start_seq(void);
  42. static int i2c_make_abort(void);
  43. DECLARE_GLOBAL_DATA_PTR;
  44. int ivm_calc_crc(unsigned char *buf, int len)
  45. {
  46. const unsigned short crc_tab[16] = {
  47. 0x0000, 0xCC01, 0xD801, 0x1400,
  48. 0xF001, 0x3C00, 0x2800, 0xE401,
  49. 0xA001, 0x6C00, 0x7800, 0xB401,
  50. 0x5000, 0x9C01, 0x8801, 0x4400};
  51. unsigned short crc = 0; /* final result */
  52. unsigned short r1 = 0; /* temp */
  53. unsigned char byte = 0; /* input buffer */
  54. int i;
  55. /* calculate CRC from array data */
  56. for (i = 0; i < len; i++) {
  57. byte = buf[i];
  58. /* lower 4 bits */
  59. r1 = crc_tab[crc & 0xF];
  60. crc = ((crc) >> 4) & 0x0FFF;
  61. crc = crc ^ r1 ^ crc_tab[byte & 0xF];
  62. /* upper 4 bits */
  63. r1 = crc_tab[crc & 0xF];
  64. crc = (crc >> 4) & 0x0FFF;
  65. crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
  66. }
  67. return crc;
  68. }
  69. /*
  70. * Set Keymile specific environment variables
  71. * Currently only some memory layout variables are calculated here
  72. * ... ------------------------------------------------
  73. * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
  74. * ... |<------------------- pram ------------------->|
  75. * ... ------------------------------------------------
  76. * @END_OF_RAM: denotes the RAM size
  77. * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
  78. * @pram : preserved ram size in k
  79. * @varaddr : startadress for /var mounted into RAM
  80. */
  81. int set_km_env(void)
  82. {
  83. uchar buf[32];
  84. unsigned int pnvramaddr;
  85. unsigned int pram;
  86. unsigned int varaddr;
  87. pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
  88. - CONFIG_KM_PNVRAM;
  89. sprintf((char *)buf, "0x%x", pnvramaddr);
  90. setenv("pnvramaddr", (char *)buf);
  91. pram = (CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM + CONFIG_KM_PNVRAM) /
  92. 0x400;
  93. sprintf((char *)buf, "0x%x", pram);
  94. setenv("pram", (char *)buf);
  95. varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
  96. sprintf((char *)buf, "0x%x", varaddr);
  97. setenv("varaddr", (char *)buf);
  98. return 0;
  99. }
  100. static int ivm_set_value(char *name, char *value)
  101. {
  102. char tempbuf[256];
  103. if (value != NULL) {
  104. sprintf(tempbuf, "%s=%s", name, value);
  105. return set_local_var(tempbuf, 0);
  106. } else {
  107. unset_local_var(name);
  108. }
  109. return 0;
  110. }
  111. static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
  112. int check)
  113. {
  114. unsigned short val;
  115. unsigned char valbuf[30];
  116. if ((buf[off + 0] != buf[off + 2]) &&
  117. (buf[off + 2] != buf[off + 4])) {
  118. printf("%s Error corrupted %s\n", __func__, name);
  119. val = -1;
  120. } else {
  121. val = buf[off + 0] + (buf[off + 1] << 8);
  122. if ((val == 0) && (check == 1))
  123. val = -1;
  124. }
  125. sprintf((char *)valbuf, "%x", val);
  126. ivm_set_value(name, (char *)valbuf);
  127. return val;
  128. }
  129. #define INV_BLOCKSIZE 0x100
  130. #define INV_DATAADDRESS 0x21
  131. #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
  132. #define IVM_POS_SHORT_TEXT 0
  133. #define IVM_POS_MANU_ID 1
  134. #define IVM_POS_MANU_SERIAL 2
  135. #define IVM_POS_PART_NUMBER 3
  136. #define IVM_POS_BUILD_STATE 4
  137. #define IVM_POS_SUPPLIER_PART_NUMBER 5
  138. #define IVM_POS_DELIVERY_DATE 6
  139. #define IVM_POS_SUPPLIER_BUILD_STATE 7
  140. #define IVM_POS_CUSTOMER_ID 8
  141. #define IVM_POS_CUSTOMER_PROD_ID 9
  142. #define IVM_POS_HISTORY 10
  143. #define IVM_POS_SYMBOL_ONLY 11
  144. static char convert_char(char c)
  145. {
  146. return (c < ' ' || c > '~') ? '.' : c;
  147. }
  148. static int ivm_findinventorystring(int type,
  149. unsigned char* const string,
  150. unsigned long maxlen,
  151. unsigned char *buf)
  152. {
  153. int xcode = 0;
  154. unsigned long cr = 0;
  155. unsigned long addr = INV_DATAADDRESS;
  156. unsigned long size = 0;
  157. unsigned long nr = type;
  158. int stop = 0; /* stop on semicolon */
  159. memset(string, '\0', maxlen);
  160. switch (type) {
  161. case IVM_POS_SYMBOL_ONLY:
  162. nr = 0;
  163. stop= 1;
  164. break;
  165. default:
  166. nr = type;
  167. stop = 0;
  168. }
  169. /* Look for the requested number of CR. */
  170. while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
  171. if ((buf[addr] == '\r')) {
  172. cr++;
  173. }
  174. addr++;
  175. }
  176. /*
  177. * the expected number of CR was found until the end of the IVM
  178. * content --> fill string
  179. */
  180. if (addr < INVENTORYDATASIZE) {
  181. /* Copy the IVM string in the corresponding string */
  182. for (; (buf[addr] != '\r') &&
  183. ((buf[addr] != ';') || (!stop)) &&
  184. (size < (maxlen - 1) &&
  185. (addr < INVENTORYDATASIZE)); addr++)
  186. {
  187. size += sprintf((char *)string + size, "%c",
  188. convert_char (buf[addr]));
  189. }
  190. /*
  191. * copy phase is done: check if everything is ok. If not,
  192. * the inventory data is most probably corrupted: tell
  193. * the world there is a problem!
  194. */
  195. if (addr == INVENTORYDATASIZE) {
  196. xcode = -1;
  197. printf("Error end of string not found\n");
  198. } else if ((size >= (maxlen - 1)) &&
  199. (buf[addr] != '\r')) {
  200. xcode = -1;
  201. printf("string too long till next CR\n");
  202. }
  203. } else {
  204. /*
  205. * some CR are missing...
  206. * the inventory data is most probably corrupted
  207. */
  208. xcode = -1;
  209. printf("not enough cr found\n");
  210. }
  211. return xcode;
  212. }
  213. #define GET_STRING(name, which, len) \
  214. if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
  215. ivm_set_value(name, (char *)valbuf); \
  216. }
  217. static int ivm_check_crc(unsigned char *buf, int block)
  218. {
  219. unsigned long crc;
  220. unsigned long crceeprom;
  221. crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
  222. crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
  223. buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
  224. if (crc != crceeprom) {
  225. if (block == 0)
  226. printf("Error CRC Block: %d EEprom: calculated: \
  227. %lx EEprom: %lx\n", block, crc, crceeprom);
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. static int ivm_analyze_block2(unsigned char *buf, int len)
  233. {
  234. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  235. unsigned long count;
  236. /* IVM_MacAddress */
  237. sprintf((char *)valbuf, "%pM", buf);
  238. ivm_set_value("IVM_MacAddress", (char *)valbuf);
  239. /* if an offset is defined, add it */
  240. #if defined(CONFIG_PIGGY_MAC_ADRESS_OFFSET)
  241. if (CONFIG_PIGGY_MAC_ADRESS_OFFSET > 0) {
  242. unsigned long val = (buf[4] << 16) + (buf[5] << 8) + buf[6];
  243. val += CONFIG_PIGGY_MAC_ADRESS_OFFSET;
  244. buf[4] = (val >> 16) & 0xff;
  245. buf[5] = (val >> 8) & 0xff;
  246. buf[6] = val & 0xff;
  247. sprintf((char *)valbuf, "%pM", buf);
  248. }
  249. #endif
  250. setenv((char *)"ethaddr", (char *)valbuf);
  251. /* IVM_MacCount */
  252. count = (buf[10] << 24) +
  253. (buf[11] << 16) +
  254. (buf[12] << 8) +
  255. buf[13];
  256. if (count == 0xffffffff)
  257. count = 1;
  258. sprintf((char *)valbuf, "%lx", count);
  259. ivm_set_value("IVM_MacCount", (char *)valbuf);
  260. return 0;
  261. }
  262. int ivm_analyze_eeprom(unsigned char *buf, int len)
  263. {
  264. unsigned short val;
  265. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  266. unsigned char *tmp;
  267. if (ivm_check_crc(buf, 0) != 0)
  268. return -1;
  269. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  270. "IVM_BoardId", 0, 1);
  271. val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  272. "IVM_HWKey", 6, 1);
  273. if (val != 0xffff) {
  274. sprintf((char *)valbuf, "%x", ((val / 100) % 10));
  275. ivm_set_value("IVM_HWVariant", (char *)valbuf);
  276. sprintf((char *)valbuf, "%x", (val % 100));
  277. ivm_set_value("IVM_HWVersion", (char *)valbuf);
  278. }
  279. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  280. "IVM_Functions", 12, 0);
  281. GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
  282. GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
  283. tmp = (unsigned char *) getenv("IVM_DeviceName");
  284. if (tmp) {
  285. int len = strlen((char *)tmp);
  286. int i = 0;
  287. while (i < len) {
  288. if (tmp[i] == ';') {
  289. ivm_set_value("IVM_ShortText",
  290. (char *)&tmp[i + 1]);
  291. break;
  292. }
  293. i++;
  294. }
  295. if (i >= len)
  296. ivm_set_value("IVM_ShortText", NULL);
  297. } else {
  298. ivm_set_value("IVM_ShortText", NULL);
  299. }
  300. GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
  301. GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
  302. GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
  303. GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
  304. GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
  305. GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
  306. GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
  307. GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
  308. GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
  309. if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
  310. return 0;
  311. ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
  312. CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
  313. return 0;
  314. }
  315. int ivm_read_eeprom(void)
  316. {
  317. #if defined(CONFIG_I2C_MUX)
  318. I2C_MUX_DEVICE *dev = NULL;
  319. #endif
  320. uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
  321. uchar *buf;
  322. unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
  323. int ret;
  324. #if defined(CONFIG_I2C_MUX)
  325. /* First init the Bus, select the Bus */
  326. #if defined(CONFIG_SYS_I2C_IVM_BUS)
  327. dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
  328. #else
  329. buf = (unsigned char *) getenv("EEprom_ivm");
  330. if (buf != NULL)
  331. dev = i2c_mux_ident_muxstring(buf);
  332. #endif
  333. if (dev == NULL) {
  334. printf("Error couldnt add Bus for IVM\n");
  335. return -1;
  336. }
  337. i2c_set_bus_num(dev->busid);
  338. #endif
  339. buf = (unsigned char *) getenv("EEprom_ivm_addr");
  340. if (buf != NULL)
  341. dev_addr = simple_strtoul((char *)buf, NULL, 16);
  342. /* add deblocking here */
  343. i2c_make_abort();
  344. ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
  345. CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  346. if (ret != 0) {
  347. printf ("Error reading EEprom\n");
  348. return -2;
  349. }
  350. return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  351. }
  352. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  353. #define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
  354. #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
  355. #if defined(CONFIG_KM_82XX)
  356. #define SDA_MASK 0x00010000
  357. #define SCL_MASK 0x00020000
  358. void set_pin(int state, unsigned long mask)
  359. {
  360. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  361. if (state)
  362. setbits_be32(&iop->pdat, mask);
  363. else
  364. clrbits_be32(&iop->pdat, mask);
  365. setbits_be32(&iop->pdir, mask);
  366. }
  367. static int get_pin(unsigned long mask)
  368. {
  369. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  370. clrbits_be32(&iop->pdir, mask);
  371. return 0 != (in_be32(&iop->pdat) & mask);
  372. }
  373. static void set_sda(int state)
  374. {
  375. set_pin(state, SDA_MASK);
  376. }
  377. static void set_scl(int state)
  378. {
  379. set_pin(state, SCL_MASK);
  380. }
  381. static int get_sda(void)
  382. {
  383. return get_pin(SDA_MASK);
  384. }
  385. static int get_scl(void)
  386. {
  387. return get_pin(SCL_MASK);
  388. }
  389. #if defined(CONFIG_HARD_I2C)
  390. static void setports(int gpio)
  391. {
  392. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  393. if (gpio) {
  394. clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  395. clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  396. } else {
  397. setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  398. clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
  399. setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  400. }
  401. }
  402. #endif
  403. #endif
  404. #if !defined(CONFIG_MPC83xx)
  405. static void i2c_write_start_seq(void)
  406. {
  407. set_sda(1);
  408. udelay(DELAY_HALF_PERIOD);
  409. set_scl(1);
  410. udelay(DELAY_HALF_PERIOD);
  411. set_sda(0);
  412. udelay(DELAY_HALF_PERIOD);
  413. set_scl(0);
  414. udelay(DELAY_HALF_PERIOD);
  415. }
  416. /*
  417. * I2C is a synchronous protocol and resets of the processor in the middle
  418. * of an access can block the I2C Bus until a powerdown of the full unit is
  419. * done. This function toggles the SCL until the SCL and SCA line are
  420. * released, but max. 16 times, after this a I2C start-sequence is sent.
  421. * This I2C Deblocking mechanism was developed by Keymile in association
  422. * with Anatech and Atmel in 1998.
  423. */
  424. static int i2c_make_abort(void)
  425. {
  426. #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
  427. immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
  428. i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  429. /*
  430. * disable I2C controller first, otherwhise it thinks we want to
  431. * talk to the slave port...
  432. */
  433. clrbits_8(&i2c->i2c_i2mod, 0x01);
  434. /* Set the PortPins to GPIO */
  435. setports(1);
  436. #endif
  437. int scl_state = 0;
  438. int sda_state = 0;
  439. int i = 0;
  440. int ret = 0;
  441. if (!get_sda()) {
  442. ret = -1;
  443. while (i < 16) {
  444. i++;
  445. set_scl(0);
  446. udelay(DELAY_ABORT_SEQ);
  447. set_scl(1);
  448. udelay(DELAY_ABORT_SEQ);
  449. scl_state = get_scl();
  450. sda_state = get_sda();
  451. if (scl_state && sda_state) {
  452. ret = 0;
  453. break;
  454. }
  455. }
  456. }
  457. if (ret == 0)
  458. for (i = 0; i < 5; i++)
  459. i2c_write_start_seq();
  460. /* respect stop setup time */
  461. udelay(DELAY_ABORT_SEQ);
  462. set_scl(1);
  463. udelay(DELAY_ABORT_SEQ);
  464. set_sda(1);
  465. get_sda();
  466. #if defined(CONFIG_HARD_I2C)
  467. /* Set the PortPins back to use for I2C */
  468. setports(0);
  469. #endif
  470. return ret;
  471. }
  472. #endif
  473. #if defined(CONFIG_MPC83xx)
  474. static void i2c_write_start_seq(void)
  475. {
  476. struct fsl_i2c *dev;
  477. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  478. udelay(DELAY_ABORT_SEQ);
  479. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  480. udelay(DELAY_ABORT_SEQ);
  481. out_8(&dev->cr, (I2C_CR_MEN));
  482. }
  483. static int i2c_make_abort(void)
  484. {
  485. struct fsl_i2c *dev;
  486. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  487. uchar dummy;
  488. uchar last;
  489. int nbr_read = 0;
  490. int i = 0;
  491. int ret = 0;
  492. /* wait after each operation to finsh with a delay */
  493. out_8(&dev->cr, (I2C_CR_MSTA));
  494. udelay(DELAY_ABORT_SEQ);
  495. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  496. udelay(DELAY_ABORT_SEQ);
  497. dummy = in_8(&dev->dr);
  498. udelay(DELAY_ABORT_SEQ);
  499. last = in_8(&dev->dr);
  500. nbr_read++;
  501. /*
  502. * do read until the last bit is 1, but stop if the full eeprom is
  503. * read.
  504. */
  505. while (((last & 0x01) != 0x01) &&
  506. (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
  507. udelay(DELAY_ABORT_SEQ);
  508. last = in_8(&dev->dr);
  509. nbr_read++;
  510. }
  511. if ((last & 0x01) != 0x01)
  512. ret = -2;
  513. if ((last != 0xff) || (nbr_read > 1))
  514. printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
  515. nbr_read, last);
  516. udelay(DELAY_ABORT_SEQ);
  517. out_8(&dev->cr, (I2C_CR_MEN));
  518. udelay(DELAY_ABORT_SEQ);
  519. /* clear status reg */
  520. out_8(&dev->sr, 0);
  521. for (i = 0; i < 5; i++)
  522. i2c_write_start_seq();
  523. if (ret != 0)
  524. printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
  525. nbr_read, last);
  526. return ret;
  527. }
  528. #endif
  529. /**
  530. * i2c_init_board - reset i2c bus. When the board is powercycled during a
  531. * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
  532. */
  533. void i2c_init_board(void)
  534. {
  535. /* Now run the AbortSequence() */
  536. i2c_make_abort();
  537. }
  538. #endif
  539. #endif
  540. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
  541. int fdt_set_node_and_value(void *blob,
  542. char *nodename,
  543. char *regname,
  544. void *var,
  545. int size)
  546. {
  547. int ret = 0;
  548. int nodeoffset = 0;
  549. nodeoffset = fdt_path_offset(blob, nodename);
  550. if (nodeoffset >= 0) {
  551. ret = fdt_setprop(blob, nodeoffset, regname, var,
  552. size);
  553. if (ret < 0)
  554. printf("ft_blob_update(): cannot set %s/%s "
  555. "property err:%s\n", nodename, regname,
  556. fdt_strerror(ret));
  557. } else {
  558. printf("ft_blob_update(): cannot find %s node "
  559. "err:%s\n", nodename, fdt_strerror(nodeoffset));
  560. }
  561. return ret;
  562. }
  563. int fdt_get_node_and_value(void *blob,
  564. char *nodename,
  565. char *propname,
  566. void **var)
  567. {
  568. int len;
  569. int nodeoffset = 0;
  570. nodeoffset = fdt_path_offset(blob, nodename);
  571. if (nodeoffset >= 0) {
  572. *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len);
  573. if (len == 0) {
  574. /* no value */
  575. printf("%s no value\n", __func__);
  576. return -1;
  577. } else if (len > 0) {
  578. return len;
  579. } else {
  580. printf("libfdt fdt_getprop(): %s\n",
  581. fdt_strerror(len));
  582. return -2;
  583. }
  584. } else {
  585. printf("%s: cannot find %s node err:%s\n", __func__,
  586. nodename, fdt_strerror(nodeoffset));
  587. return -3;
  588. }
  589. }
  590. #endif
  591. #if !defined(MACH_TYPE_KM_KIRKWOOD)
  592. int ethernet_present(void)
  593. {
  594. struct km_bec_fpga *base =
  595. (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
  596. return in_8(&base->bprth) & PIGGY_PRESENT;
  597. }
  598. #endif
  599. int board_eth_init(bd_t *bis)
  600. {
  601. if (ethernet_present())
  602. return cpu_eth_init(bis);
  603. return -1;
  604. }
  605. /*
  606. * do_setboardid command
  607. * read out the board id and the hw key from the intventory EEPROM and set
  608. * this values as environment variables.
  609. */
  610. static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
  611. char *const argv[])
  612. {
  613. unsigned char buf[32];
  614. char *p;
  615. p = get_local_var("IVM_BoardId");
  616. if (p == NULL) {
  617. printf("can't get the IVM_Boardid\n");
  618. return 1;
  619. }
  620. sprintf((char *)buf, "%s", p);
  621. setenv("boardid", (char *)buf);
  622. p = get_local_var("IVM_HWKey");
  623. if (p == NULL) {
  624. printf("can't get the IVM_HWKey\n");
  625. return 1;
  626. }
  627. sprintf((char *)buf, "%s", p);
  628. setenv("hwkey", (char *)buf);
  629. return 0;
  630. }
  631. U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
  632. "hwkey from IVM and set in environment");
  633. /*
  634. * command km_checkbidhwk
  635. * if "boardid" and "hwkey" are not already set in the environment, do:
  636. * if a "boardIdListHex" exists in the environment:
  637. * - read ivm data for boardid and hwkey
  638. * - compare each entry of the boardIdListHex with the
  639. * IVM data:
  640. * if match:
  641. * set environment variables boardid, boardId,
  642. * hwkey, hwKey to the found values
  643. * both (boardid and boardId) are set because
  644. * they might be used differently in the
  645. * application and in the init scripts (?)
  646. * return 0 in case of match, 1 if not match or error
  647. */
  648. int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
  649. char *const argv[])
  650. {
  651. unsigned long ivmbid = 0, ivmhwkey = 0;
  652. unsigned long envbid = 0, envhwkey = 0;
  653. char *p;
  654. int verbose = argc > 1 && *argv[1] == 'v';
  655. int rc = 0;
  656. /*
  657. * first read out the real inventory values, these values are
  658. * already stored in the local hush variables
  659. */
  660. p = get_local_var("IVM_BoardId");
  661. if (p == NULL) {
  662. printf("can't get the IVM_Boardid\n");
  663. return 1;
  664. }
  665. rc = strict_strtoul(p, 16, &ivmbid);
  666. p = get_local_var("IVM_HWKey");
  667. if (p == NULL) {
  668. printf("can't get the IVM_HWKey\n");
  669. return 1;
  670. }
  671. rc = strict_strtoul(p, 16, &ivmhwkey);
  672. if (!ivmbid || !ivmhwkey) {
  673. printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
  674. return rc;
  675. }
  676. /* now try to read values from environment if available */
  677. p = getenv("boardid");
  678. if (p != NULL)
  679. rc = strict_strtoul(p, 16, &envbid);
  680. p = getenv("hwkey");
  681. if (p != NULL)
  682. rc = strict_strtoul(p, 16, &envhwkey);
  683. if (rc != 0) {
  684. printf("strict_strtoul returns error: %d", rc);
  685. return rc;
  686. }
  687. if (!envbid || !envhwkey) {
  688. /*
  689. * BoardId/HWkey not available in the environment, so try the
  690. * environment variable for BoardId/HWkey list
  691. */
  692. char *bidhwklist = getenv("boardIdListHex");
  693. if (bidhwklist) {
  694. int found = 0;
  695. char *rest = bidhwklist;
  696. char *endp;
  697. if (verbose) {
  698. printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
  699. ivmbid, ivmhwkey);
  700. printf("boardIdHwKeyList: %s\n",
  701. bidhwklist);
  702. }
  703. while (!found) {
  704. /* loop over each bid/hwkey pair in the list */
  705. unsigned long bid = 0;
  706. unsigned long hwkey = 0;
  707. while (*rest && !isxdigit(*rest))
  708. rest++;
  709. /*
  710. * use simple_strtoul because we need &end and
  711. * we know we got non numeric char at the end
  712. */
  713. bid = simple_strtoul(rest, &endp, 16);
  714. /* BoardId and HWkey are separated with a "_" */
  715. if (*endp == '_') {
  716. rest = endp + 1;
  717. /*
  718. * use simple_strtoul because we need
  719. * &end
  720. */
  721. hwkey = simple_strtoul(rest, &endp, 16);
  722. rest = endp;
  723. while (*rest && !isxdigit(*rest))
  724. rest++;
  725. }
  726. if ((!bid) || (!hwkey)) {
  727. /* end of list */
  728. break;
  729. }
  730. if (verbose) {
  731. printf("trying bid=0x%lX, hwkey=%ld\n",
  732. bid, hwkey);
  733. }
  734. /*
  735. * Compare the values of the found entry in the
  736. * list with the valid values which are stored
  737. * in the inventory eeprom. If they are equal
  738. * set the values in environment variables.
  739. */
  740. if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
  741. char buf[10];
  742. found = 1;
  743. envbid = bid;
  744. envhwkey = hwkey;
  745. sprintf(buf, "%lx", bid);
  746. setenv("boardid", buf);
  747. sprintf(buf, "%lx", hwkey);
  748. setenv("hwkey", buf);
  749. }
  750. } /* end while( ! found ) */
  751. }
  752. }
  753. /* compare now the values */
  754. if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
  755. printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
  756. rc = 0; /* match */
  757. } else {
  758. printf("Error: env bId=0x%3lX, hwKey=%ld\n", envbid, envhwkey);
  759. printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
  760. rc = 1; /* don't match */
  761. }
  762. return rc;
  763. }
  764. U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
  765. "check boardid and hwkey",
  766. "[v]\n - check environment parameter "\
  767. "\"boardIdListHex\" against stored boardid and hwkey "\
  768. "from the IVM\n v: verbose output"
  769. );