common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. if (getenv("ethaddr") == NULL)
  251. setenv((char *)"ethaddr", (char *)valbuf);
  252. /* IVM_MacCount */
  253. count = (buf[10] << 24) +
  254. (buf[11] << 16) +
  255. (buf[12] << 8) +
  256. buf[13];
  257. if (count == 0xffffffff)
  258. count = 1;
  259. sprintf((char *)valbuf, "%lx", count);
  260. ivm_set_value("IVM_MacCount", (char *)valbuf);
  261. return 0;
  262. }
  263. int ivm_analyze_eeprom(unsigned char *buf, int len)
  264. {
  265. unsigned short val;
  266. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  267. unsigned char *tmp;
  268. if (ivm_check_crc(buf, 0) != 0)
  269. return -1;
  270. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  271. "IVM_BoardId", 0, 1);
  272. val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  273. "IVM_HWKey", 6, 1);
  274. if (val != 0xffff) {
  275. sprintf((char *)valbuf, "%x", ((val / 100) % 10));
  276. ivm_set_value("IVM_HWVariant", (char *)valbuf);
  277. sprintf((char *)valbuf, "%x", (val % 100));
  278. ivm_set_value("IVM_HWVersion", (char *)valbuf);
  279. }
  280. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  281. "IVM_Functions", 12, 0);
  282. GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
  283. GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
  284. tmp = (unsigned char *) getenv("IVM_DeviceName");
  285. if (tmp) {
  286. int len = strlen((char *)tmp);
  287. int i = 0;
  288. while (i < len) {
  289. if (tmp[i] == ';') {
  290. ivm_set_value("IVM_ShortText",
  291. (char *)&tmp[i + 1]);
  292. break;
  293. }
  294. i++;
  295. }
  296. if (i >= len)
  297. ivm_set_value("IVM_ShortText", NULL);
  298. } else {
  299. ivm_set_value("IVM_ShortText", NULL);
  300. }
  301. GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
  302. GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
  303. GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
  304. GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
  305. GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
  306. GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
  307. GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
  308. GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
  309. GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
  310. if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
  311. return 0;
  312. ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
  313. CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
  314. return 0;
  315. }
  316. int ivm_read_eeprom(void)
  317. {
  318. #if defined(CONFIG_I2C_MUX)
  319. I2C_MUX_DEVICE *dev = NULL;
  320. #endif
  321. uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
  322. uchar *buf;
  323. unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
  324. int ret;
  325. #if defined(CONFIG_I2C_MUX)
  326. /* First init the Bus, select the Bus */
  327. #if defined(CONFIG_SYS_I2C_IVM_BUS)
  328. dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
  329. #else
  330. buf = (unsigned char *) getenv("EEprom_ivm");
  331. if (buf != NULL)
  332. dev = i2c_mux_ident_muxstring(buf);
  333. #endif
  334. if (dev == NULL) {
  335. printf("Error couldnt add Bus for IVM\n");
  336. return -1;
  337. }
  338. i2c_set_bus_num(dev->busid);
  339. #endif
  340. buf = (unsigned char *) getenv("EEprom_ivm_addr");
  341. if (buf != NULL)
  342. dev_addr = simple_strtoul((char *)buf, NULL, 16);
  343. /* add deblocking here */
  344. i2c_make_abort();
  345. ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
  346. CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  347. if (ret != 0) {
  348. printf ("Error reading EEprom\n");
  349. return -2;
  350. }
  351. return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  352. }
  353. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  354. #define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
  355. #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
  356. #if defined(CONFIG_KM_82XX)
  357. #define SDA_MASK 0x00010000
  358. #define SCL_MASK 0x00020000
  359. void set_pin(int state, unsigned long mask)
  360. {
  361. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  362. if (state)
  363. setbits_be32(&iop->pdat, mask);
  364. else
  365. clrbits_be32(&iop->pdat, mask);
  366. setbits_be32(&iop->pdir, mask);
  367. }
  368. static int get_pin(unsigned long mask)
  369. {
  370. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  371. clrbits_be32(&iop->pdir, mask);
  372. return 0 != (in_be32(&iop->pdat) & mask);
  373. }
  374. static void set_sda(int state)
  375. {
  376. set_pin(state, SDA_MASK);
  377. }
  378. static void set_scl(int state)
  379. {
  380. set_pin(state, SCL_MASK);
  381. }
  382. static int get_sda(void)
  383. {
  384. return get_pin(SDA_MASK);
  385. }
  386. static int get_scl(void)
  387. {
  388. return get_pin(SCL_MASK);
  389. }
  390. #if defined(CONFIG_HARD_I2C)
  391. static void setports(int gpio)
  392. {
  393. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  394. if (gpio) {
  395. clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  396. clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  397. } else {
  398. setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  399. clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
  400. setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  401. }
  402. }
  403. #endif
  404. #endif
  405. #if !defined(CONFIG_MPC83xx)
  406. static void i2c_write_start_seq(void)
  407. {
  408. set_sda(1);
  409. udelay(DELAY_HALF_PERIOD);
  410. set_scl(1);
  411. udelay(DELAY_HALF_PERIOD);
  412. set_sda(0);
  413. udelay(DELAY_HALF_PERIOD);
  414. set_scl(0);
  415. udelay(DELAY_HALF_PERIOD);
  416. }
  417. /*
  418. * I2C is a synchronous protocol and resets of the processor in the middle
  419. * of an access can block the I2C Bus until a powerdown of the full unit is
  420. * done. This function toggles the SCL until the SCL and SCA line are
  421. * released, but max. 16 times, after this a I2C start-sequence is sent.
  422. * This I2C Deblocking mechanism was developed by Keymile in association
  423. * with Anatech and Atmel in 1998.
  424. */
  425. static int i2c_make_abort(void)
  426. {
  427. #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
  428. immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
  429. i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  430. /*
  431. * disable I2C controller first, otherwhise it thinks we want to
  432. * talk to the slave port...
  433. */
  434. clrbits_8(&i2c->i2c_i2mod, 0x01);
  435. /* Set the PortPins to GPIO */
  436. setports(1);
  437. #endif
  438. int scl_state = 0;
  439. int sda_state = 0;
  440. int i = 0;
  441. int ret = 0;
  442. if (!get_sda()) {
  443. ret = -1;
  444. while (i < 16) {
  445. i++;
  446. set_scl(0);
  447. udelay(DELAY_ABORT_SEQ);
  448. set_scl(1);
  449. udelay(DELAY_ABORT_SEQ);
  450. scl_state = get_scl();
  451. sda_state = get_sda();
  452. if (scl_state && sda_state) {
  453. ret = 0;
  454. break;
  455. }
  456. }
  457. }
  458. if (ret == 0)
  459. for (i = 0; i < 5; i++)
  460. i2c_write_start_seq();
  461. /* respect stop setup time */
  462. udelay(DELAY_ABORT_SEQ);
  463. set_scl(1);
  464. udelay(DELAY_ABORT_SEQ);
  465. set_sda(1);
  466. get_sda();
  467. #if defined(CONFIG_HARD_I2C)
  468. /* Set the PortPins back to use for I2C */
  469. setports(0);
  470. #endif
  471. return ret;
  472. }
  473. #endif
  474. #if defined(CONFIG_MPC83xx)
  475. static void i2c_write_start_seq(void)
  476. {
  477. struct fsl_i2c *dev;
  478. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  479. udelay(DELAY_ABORT_SEQ);
  480. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  481. udelay(DELAY_ABORT_SEQ);
  482. out_8(&dev->cr, (I2C_CR_MEN));
  483. }
  484. static int i2c_make_abort(void)
  485. {
  486. struct fsl_i2c *dev;
  487. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  488. uchar dummy;
  489. uchar last;
  490. int nbr_read = 0;
  491. int i = 0;
  492. int ret = 0;
  493. /* wait after each operation to finsh with a delay */
  494. out_8(&dev->cr, (I2C_CR_MSTA));
  495. udelay(DELAY_ABORT_SEQ);
  496. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  497. udelay(DELAY_ABORT_SEQ);
  498. dummy = in_8(&dev->dr);
  499. udelay(DELAY_ABORT_SEQ);
  500. last = in_8(&dev->dr);
  501. nbr_read++;
  502. /*
  503. * do read until the last bit is 1, but stop if the full eeprom is
  504. * read.
  505. */
  506. while (((last & 0x01) != 0x01) &&
  507. (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
  508. udelay(DELAY_ABORT_SEQ);
  509. last = in_8(&dev->dr);
  510. nbr_read++;
  511. }
  512. if ((last & 0x01) != 0x01)
  513. ret = -2;
  514. if ((last != 0xff) || (nbr_read > 1))
  515. printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
  516. nbr_read, last);
  517. udelay(DELAY_ABORT_SEQ);
  518. out_8(&dev->cr, (I2C_CR_MEN));
  519. udelay(DELAY_ABORT_SEQ);
  520. /* clear status reg */
  521. out_8(&dev->sr, 0);
  522. for (i = 0; i < 5; i++)
  523. i2c_write_start_seq();
  524. if (ret != 0)
  525. printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
  526. nbr_read, last);
  527. return ret;
  528. }
  529. #endif
  530. /**
  531. * i2c_init_board - reset i2c bus. When the board is powercycled during a
  532. * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
  533. */
  534. void i2c_init_board(void)
  535. {
  536. /* Now run the AbortSequence() */
  537. i2c_make_abort();
  538. }
  539. #endif
  540. #endif
  541. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
  542. int fdt_set_node_and_value(void *blob,
  543. char *nodename,
  544. char *regname,
  545. void *var,
  546. int size)
  547. {
  548. int ret = 0;
  549. int nodeoffset = 0;
  550. nodeoffset = fdt_path_offset(blob, nodename);
  551. if (nodeoffset >= 0) {
  552. ret = fdt_setprop(blob, nodeoffset, regname, var,
  553. size);
  554. if (ret < 0)
  555. printf("ft_blob_update(): cannot set %s/%s "
  556. "property err:%s\n", nodename, regname,
  557. fdt_strerror(ret));
  558. } else {
  559. printf("ft_blob_update(): cannot find %s node "
  560. "err:%s\n", nodename, fdt_strerror(nodeoffset));
  561. }
  562. return ret;
  563. }
  564. int fdt_get_node_and_value(void *blob,
  565. char *nodename,
  566. char *propname,
  567. void **var)
  568. {
  569. int len;
  570. int nodeoffset = 0;
  571. nodeoffset = fdt_path_offset(blob, nodename);
  572. if (nodeoffset >= 0) {
  573. *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len);
  574. if (len == 0) {
  575. /* no value */
  576. printf("%s no value\n", __func__);
  577. return -1;
  578. } else if (len > 0) {
  579. return len;
  580. } else {
  581. printf("libfdt fdt_getprop(): %s\n",
  582. fdt_strerror(len));
  583. return -2;
  584. }
  585. } else {
  586. printf("%s: cannot find %s node err:%s\n", __func__,
  587. nodename, fdt_strerror(nodeoffset));
  588. return -3;
  589. }
  590. }
  591. #endif
  592. #if !defined(MACH_TYPE_KM_KIRKWOOD)
  593. int ethernet_present(void)
  594. {
  595. struct km_bec_fpga *base =
  596. (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
  597. return in_8(&base->bprth) & PIGGY_PRESENT;
  598. }
  599. #endif
  600. int board_eth_init(bd_t *bis)
  601. {
  602. if (ethernet_present())
  603. return cpu_eth_init(bis);
  604. return -1;
  605. }
  606. /*
  607. * do_setboardid command
  608. * read out the board id and the hw key from the intventory EEPROM and set
  609. * this values as environment variables.
  610. */
  611. static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
  612. char *const argv[])
  613. {
  614. unsigned char buf[32];
  615. char *p;
  616. p = get_local_var("IVM_BoardId");
  617. if (p == NULL) {
  618. printf("can't get the IVM_Boardid\n");
  619. return 1;
  620. }
  621. sprintf((char *)buf, "%s", p);
  622. setenv("boardid", (char *)buf);
  623. p = get_local_var("IVM_HWKey");
  624. if (p == NULL) {
  625. printf("can't get the IVM_HWKey\n");
  626. return 1;
  627. }
  628. sprintf((char *)buf, "%s", p);
  629. setenv("hwkey", (char *)buf);
  630. return 0;
  631. }
  632. U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
  633. "hwkey from IVM and set in environment");
  634. /*
  635. * command km_checkbidhwk
  636. * if "boardid" and "hwkey" are not already set in the environment, do:
  637. * if a "boardIdListHex" exists in the environment:
  638. * - read ivm data for boardid and hwkey
  639. * - compare each entry of the boardIdListHex with the
  640. * IVM data:
  641. * if match:
  642. * set environment variables boardid, boardId,
  643. * hwkey, hwKey to the found values
  644. * both (boardid and boardId) are set because
  645. * they might be used differently in the
  646. * application and in the init scripts (?)
  647. * return 0 in case of match, 1 if not match or error
  648. */
  649. int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
  650. char *const argv[])
  651. {
  652. unsigned long ivmbid = 0, ivmhwkey = 0;
  653. unsigned long envbid = 0, envhwkey = 0;
  654. char *p;
  655. int verbose = argc > 1 && *argv[1] == 'v';
  656. int rc = 0;
  657. /*
  658. * first read out the real inventory values, these values are
  659. * already stored in the local hush variables
  660. */
  661. p = get_local_var("IVM_BoardId");
  662. if (p == NULL) {
  663. printf("can't get the IVM_Boardid\n");
  664. return 1;
  665. }
  666. rc = strict_strtoul(p, 16, &ivmbid);
  667. p = get_local_var("IVM_HWKey");
  668. if (p == NULL) {
  669. printf("can't get the IVM_HWKey\n");
  670. return 1;
  671. }
  672. rc = strict_strtoul(p, 16, &ivmhwkey);
  673. if (!ivmbid || !ivmhwkey) {
  674. printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
  675. return rc;
  676. }
  677. /* now try to read values from environment if available */
  678. p = getenv("boardid");
  679. if (p != NULL)
  680. rc = strict_strtoul(p, 16, &envbid);
  681. p = getenv("hwkey");
  682. if (p != NULL)
  683. rc = strict_strtoul(p, 16, &envhwkey);
  684. if (rc != 0) {
  685. printf("strict_strtoul returns error: %d", rc);
  686. return rc;
  687. }
  688. if (!envbid || !envhwkey) {
  689. /*
  690. * BoardId/HWkey not available in the environment, so try the
  691. * environment variable for BoardId/HWkey list
  692. */
  693. char *bidhwklist = getenv("boardIdListHex");
  694. if (bidhwklist) {
  695. int found = 0;
  696. char *rest = bidhwklist;
  697. char *endp;
  698. if (verbose) {
  699. printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
  700. ivmbid, ivmhwkey);
  701. printf("boardIdHwKeyList: %s\n",
  702. bidhwklist);
  703. }
  704. while (!found) {
  705. /* loop over each bid/hwkey pair in the list */
  706. unsigned long bid = 0;
  707. unsigned long hwkey = 0;
  708. while (*rest && !isxdigit(*rest))
  709. rest++;
  710. /*
  711. * use simple_strtoul because we need &end and
  712. * we know we got non numeric char at the end
  713. */
  714. bid = simple_strtoul(rest, &endp, 16);
  715. /* BoardId and HWkey are separated with a "_" */
  716. if (*endp == '_') {
  717. rest = endp + 1;
  718. /*
  719. * use simple_strtoul because we need
  720. * &end
  721. */
  722. hwkey = simple_strtoul(rest, &endp, 16);
  723. rest = endp;
  724. while (*rest && !isxdigit(*rest))
  725. rest++;
  726. }
  727. if ((!bid) || (!hwkey)) {
  728. /* end of list */
  729. break;
  730. }
  731. if (verbose) {
  732. printf("trying bid=0x%lX, hwkey=%ld\n",
  733. bid, hwkey);
  734. }
  735. /*
  736. * Compare the values of the found entry in the
  737. * list with the valid values which are stored
  738. * in the inventory eeprom. If they are equal
  739. * store the values in environment variables
  740. * and save the environment.
  741. * This can only happen once for the lifetime
  742. * of a board, because once saved the function
  743. * will never reach the while loop.
  744. */
  745. if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
  746. char buf[10];
  747. found = 1;
  748. envbid = bid;
  749. envhwkey = hwkey;
  750. sprintf(buf, "%lx", bid);
  751. setenv("boardid", buf);
  752. sprintf(buf, "%lx", hwkey);
  753. setenv("hwkey", buf);
  754. saveenv();
  755. }
  756. } /* end while( ! found ) */
  757. }
  758. }
  759. /* compare now the values */
  760. if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
  761. printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
  762. rc = 0; /* match */
  763. } else {
  764. printf("Error: env bId=0x%3lX, hwKey=%ld\n", envbid, envhwkey);
  765. printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
  766. rc = 1; /* don't match */
  767. }
  768. return rc;
  769. }
  770. U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
  771. "check boardid and hwkey",
  772. "[v]\n - check environment parameter "\
  773. "\"boardIdListHex\" against stored boardid and hwkey "\
  774. "from the IVM\n v: verbose output"
  775. );