common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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)
  25. #include <mpc8260.h>
  26. #endif
  27. #include <ioports.h>
  28. #include <malloc.h>
  29. #include <hush.h>
  30. #include <net.h>
  31. #include <asm/io.h>
  32. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
  33. #include <libfdt.h>
  34. #endif
  35. #include "../common/common.h"
  36. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  37. #include <i2c.h>
  38. int ivm_calc_crc(unsigned char *buf, int len)
  39. {
  40. const unsigned short crc_tab[16] = {
  41. 0x0000, 0xCC01, 0xD801, 0x1400,
  42. 0xF001, 0x3C00, 0x2800, 0xE401,
  43. 0xA001, 0x6C00, 0x7800, 0xB401,
  44. 0x5000, 0x9C01, 0x8801, 0x4400};
  45. unsigned short crc = 0; /* final result */
  46. unsigned short r1 = 0; /* temp */
  47. unsigned char byte = 0; /* input buffer */
  48. int i;
  49. /* calculate CRC from array data */
  50. for (i = 0; i < len; i++) {
  51. byte = buf[i];
  52. /* lower 4 bits */
  53. r1 = crc_tab[crc & 0xF];
  54. crc = ((crc) >> 4) & 0x0FFF;
  55. crc = crc ^ r1 ^ crc_tab[byte & 0xF];
  56. /* upper 4 bits */
  57. r1 = crc_tab[crc & 0xF];
  58. crc = (crc >> 4) & 0x0FFF;
  59. crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
  60. }
  61. return crc;
  62. }
  63. static int ivm_set_value(char *name, char *value)
  64. {
  65. char tempbuf[256];
  66. if (value != NULL) {
  67. sprintf(tempbuf, "%s=%s", name, value);
  68. return set_local_var(tempbuf, 0);
  69. } else {
  70. unset_local_var(name);
  71. }
  72. return 0;
  73. }
  74. static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
  75. int check)
  76. {
  77. unsigned short val;
  78. unsigned char valbuf[30];
  79. if ((buf[off + 0] != buf[off + 2]) &&
  80. (buf[off + 2] != buf[off + 4])) {
  81. printf("%s Error corrupted %s\n", __func__, name);
  82. val = -1;
  83. } else {
  84. val = buf[off + 0] + (buf[off + 1] << 8);
  85. if ((val == 0) && (check == 1))
  86. val = -1;
  87. }
  88. sprintf((char *)valbuf, "%x", val);
  89. ivm_set_value(name, (char *)valbuf);
  90. return val;
  91. }
  92. #define INV_BLOCKSIZE 0x100
  93. #define INV_DATAADDRESS 0x21
  94. #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
  95. #define IVM_POS_SHORT_TEXT 0
  96. #define IVM_POS_MANU_ID 1
  97. #define IVM_POS_MANU_SERIAL 2
  98. #define IVM_POS_PART_NUMBER 3
  99. #define IVM_POS_BUILD_STATE 4
  100. #define IVM_POS_SUPPLIER_PART_NUMBER 5
  101. #define IVM_POS_DELIVERY_DATE 6
  102. #define IVM_POS_SUPPLIER_BUILD_STATE 7
  103. #define IVM_POS_CUSTOMER_ID 8
  104. #define IVM_POS_CUSTOMER_PROD_ID 9
  105. #define IVM_POS_HISTORY 10
  106. #define IVM_POS_SYMBOL_ONLY 11
  107. static char convert_char(char c)
  108. {
  109. return (c < ' ' || c > '~') ? '.' : c;
  110. }
  111. static int ivm_findinventorystring(int type,
  112. unsigned char* const string,
  113. unsigned long maxlen,
  114. unsigned char *buf)
  115. {
  116. int xcode = 0;
  117. unsigned long cr = 0;
  118. unsigned long addr = INV_DATAADDRESS;
  119. unsigned long size = 0;
  120. unsigned long nr = type;
  121. int stop = 0; /* stop on semicolon */
  122. memset(string, '\0', maxlen);
  123. switch (type) {
  124. case IVM_POS_SYMBOL_ONLY:
  125. nr = 0;
  126. stop= 1;
  127. break;
  128. default:
  129. nr = type;
  130. stop = 0;
  131. }
  132. /* Look for the requested number of CR. */
  133. while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
  134. if ((buf[addr] == '\r')) {
  135. cr++;
  136. }
  137. addr++;
  138. }
  139. /*
  140. * the expected number of CR was found until the end of the IVM
  141. * content --> fill string
  142. */
  143. if (addr < INVENTORYDATASIZE) {
  144. /* Copy the IVM string in the corresponding string */
  145. for (; (buf[addr] != '\r') &&
  146. ((buf[addr] != ';') || (!stop)) &&
  147. (size < (maxlen - 1) &&
  148. (addr < INVENTORYDATASIZE)); addr++)
  149. {
  150. size += sprintf((char *)string + size, "%c",
  151. convert_char (buf[addr]));
  152. }
  153. /*
  154. * copy phase is done: check if everything is ok. If not,
  155. * the inventory data is most probably corrupted: tell
  156. * the world there is a problem!
  157. */
  158. if (addr == INVENTORYDATASIZE) {
  159. xcode = -1;
  160. printf("Error end of string not found\n");
  161. } else if ((size >= (maxlen - 1)) &&
  162. (buf[addr] != '\r')) {
  163. xcode = -1;
  164. printf("string too long till next CR\n");
  165. }
  166. } else {
  167. /*
  168. * some CR are missing...
  169. * the inventory data is most probably corrupted
  170. */
  171. xcode = -1;
  172. printf("not enough cr found\n");
  173. }
  174. return xcode;
  175. }
  176. #define GET_STRING(name, which, len) \
  177. if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
  178. ivm_set_value(name, (char *)valbuf); \
  179. }
  180. static int ivm_check_crc(unsigned char *buf, int block)
  181. {
  182. unsigned long crc;
  183. unsigned long crceeprom;
  184. crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
  185. crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
  186. buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
  187. if (crc != crceeprom) {
  188. if (block == 0)
  189. printf("Error CRC Block: %d EEprom: calculated: \
  190. %lx EEprom: %lx\n", block, crc, crceeprom);
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. static int ivm_analyze_block2(unsigned char *buf, int len)
  196. {
  197. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  198. unsigned long count;
  199. /* IVM_MacAddress */
  200. sprintf((char *)valbuf, "%pM", buf);
  201. ivm_set_value("IVM_MacAddress", (char *)valbuf);
  202. if (getenv("ethaddr") == NULL)
  203. setenv((char *)"ethaddr", (char *)valbuf);
  204. /* IVM_MacCount */
  205. count = (buf[10] << 24) +
  206. (buf[11] << 16) +
  207. (buf[12] << 8) +
  208. buf[13];
  209. if (count == 0xffffffff)
  210. count = 1;
  211. sprintf((char *)valbuf, "%lx", count);
  212. ivm_set_value("IVM_MacCount", (char *)valbuf);
  213. return 0;
  214. }
  215. int ivm_analyze_eeprom(unsigned char *buf, int len)
  216. {
  217. unsigned short val;
  218. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  219. unsigned char *tmp;
  220. if (ivm_check_crc(buf, 0) != 0)
  221. return -1;
  222. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  223. "IVM_BoardId", 0, 1);
  224. val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  225. "IVM_HWKey", 6, 1);
  226. if (val != 0xffff) {
  227. sprintf((char *)valbuf, "%x", ((val / 100) % 10));
  228. ivm_set_value("IVM_HWVariant", (char *)valbuf);
  229. sprintf((char *)valbuf, "%x", (val % 100));
  230. ivm_set_value("IVM_HWVersion", (char *)valbuf);
  231. }
  232. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  233. "IVM_Functions", 12, 0);
  234. GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
  235. GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
  236. tmp = (unsigned char *) getenv("IVM_DeviceName");
  237. if (tmp) {
  238. int len = strlen((char *)tmp);
  239. int i = 0;
  240. while (i < len) {
  241. if (tmp[i] == ';') {
  242. ivm_set_value("IVM_ShortText",
  243. (char *)&tmp[i + 1]);
  244. break;
  245. }
  246. i++;
  247. }
  248. if (i >= len)
  249. ivm_set_value("IVM_ShortText", NULL);
  250. } else {
  251. ivm_set_value("IVM_ShortText", NULL);
  252. }
  253. GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
  254. GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
  255. GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
  256. GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
  257. GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
  258. GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
  259. GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
  260. GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
  261. GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
  262. if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
  263. return 0;
  264. ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
  265. CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
  266. return 0;
  267. }
  268. int ivm_read_eeprom(void)
  269. {
  270. #if defined(CONFIG_I2C_MUX)
  271. I2C_MUX_DEVICE *dev = NULL;
  272. #endif
  273. uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
  274. uchar *buf;
  275. unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
  276. int ret;
  277. #if defined(CONFIG_I2C_MUX)
  278. /* First init the Bus, select the Bus */
  279. #if defined(CONFIG_SYS_I2C_IVM_BUS)
  280. dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
  281. #else
  282. buf = (unsigned char *) getenv("EEprom_ivm");
  283. if (buf != NULL)
  284. dev = i2c_mux_ident_muxstring(buf);
  285. #endif
  286. if (dev == NULL) {
  287. printf("Error couldnt add Bus for IVM\n");
  288. return -1;
  289. }
  290. i2c_set_bus_num(dev->busid);
  291. #endif
  292. buf = (unsigned char *) getenv("EEprom_ivm_addr");
  293. if (buf != NULL)
  294. dev_addr = simple_strtoul((char *)buf, NULL, 16);
  295. ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
  296. CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  297. if (ret != 0) {
  298. printf ("Error reading EEprom\n");
  299. return -2;
  300. }
  301. return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  302. }
  303. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  304. #define DELAY_ABORT_SEQ 62
  305. #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
  306. #if defined(CONFIG_MGCOGE)
  307. #define SDA_MASK 0x00010000
  308. #define SCL_MASK 0x00020000
  309. static void set_pin(int state, unsigned long mask)
  310. {
  311. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  312. if (state)
  313. setbits_be32(&iop->pdat, mask);
  314. else
  315. clrbits_be32(&iop->pdat, mask);
  316. setbits_be32(&iop->pdir, mask);
  317. }
  318. static int get_pin(unsigned long mask)
  319. {
  320. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  321. clrbits_be32(&iop->pdir, mask);
  322. return 0 != (in_be32(&iop->pdat) & mask);
  323. }
  324. static void set_sda(int state)
  325. {
  326. set_pin(state, SDA_MASK);
  327. }
  328. static void set_scl(int state)
  329. {
  330. set_pin(state, SCL_MASK);
  331. }
  332. static int get_sda(void)
  333. {
  334. return get_pin(SDA_MASK);
  335. }
  336. static int get_scl(void)
  337. {
  338. return get_pin(SCL_MASK);
  339. }
  340. #if defined(CONFIG_HARD_I2C)
  341. static void setports(int gpio)
  342. {
  343. ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
  344. if (gpio) {
  345. clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  346. clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  347. } else {
  348. setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
  349. clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
  350. setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
  351. }
  352. }
  353. #endif
  354. #endif
  355. #if !defined(CONFIG_KMETER1)
  356. static void writeStartSeq(void)
  357. {
  358. set_sda(1);
  359. udelay(DELAY_HALF_PERIOD);
  360. set_scl(1);
  361. udelay(DELAY_HALF_PERIOD);
  362. set_sda(0);
  363. udelay(DELAY_HALF_PERIOD);
  364. set_scl(0);
  365. udelay(DELAY_HALF_PERIOD);
  366. }
  367. /*
  368. * I2C is a synchronous protocol and resets of the processor in the middle
  369. * of an access can block the I2C Bus until a powerdown of the full unit is
  370. * done. This function toggles the SCL until the SCL and SCA line are
  371. * released, but max. 16 times, after this a I2C start-sequence is sent.
  372. * This I2C Deblocking mechanism was developed by Keymile in association
  373. * with Anatech and Atmel in 1998.
  374. */
  375. static int i2c_make_abort(void)
  376. {
  377. int scl_state = 0;
  378. int sda_state = 0;
  379. int i = 0;
  380. int ret = 0;
  381. if (!get_sda()) {
  382. ret = -1;
  383. while (i < 16) {
  384. i++;
  385. set_scl(0);
  386. udelay(DELAY_ABORT_SEQ);
  387. set_scl(1);
  388. udelay(DELAY_ABORT_SEQ);
  389. scl_state = get_scl();
  390. sda_state = get_sda();
  391. if (scl_state && sda_state) {
  392. ret = 0;
  393. break;
  394. }
  395. }
  396. }
  397. if (ret == 0)
  398. for (i = 0; i < 5; i++)
  399. writeStartSeq();
  400. get_sda();
  401. return ret;
  402. }
  403. #endif
  404. /*
  405. * i2c_init_board - reset i2c bus. When the board is powercycled during a
  406. * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
  407. */
  408. void i2c_init_board(void)
  409. {
  410. #if defined(CONFIG_KMETER1)
  411. struct fsl_i2c *dev;
  412. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  413. uchar dummy;
  414. out_8(&dev->cr, (I2C_CR_MSTA));
  415. out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  416. dummy = in_8(&dev->dr);
  417. dummy = in_8(&dev->dr);
  418. if (dummy != 0xff) {
  419. dummy = in_8(&dev->dr);
  420. }
  421. out_8(&dev->cr, (I2C_CR_MEN));
  422. out_8(&dev->cr, 0x00);
  423. out_8(&dev->cr, (I2C_CR_MEN));
  424. #else
  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. /* Now run the AbortSequence() */
  437. i2c_make_abort();
  438. #if defined(CONFIG_HARD_I2C)
  439. /* Set the PortPins back to use for I2C */
  440. setports(0);
  441. #endif
  442. #endif
  443. }
  444. #endif
  445. #endif
  446. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
  447. int fdt_set_node_and_value(void *blob,
  448. char *nodename,
  449. char *regname,
  450. void *var,
  451. int size)
  452. {
  453. int ret = 0;
  454. int nodeoffset = 0;
  455. nodeoffset = fdt_path_offset(blob, nodename);
  456. if (nodeoffset >= 0) {
  457. ret = fdt_setprop(blob, nodeoffset, regname, var,
  458. size);
  459. if (ret < 0)
  460. printf("ft_blob_update(): cannot set %s/%s "
  461. "property err:%s\n", nodename, regname,
  462. fdt_strerror(ret));
  463. } else {
  464. printf("ft_blob_update(): cannot find %s node "
  465. "err:%s\n", nodename, fdt_strerror(nodeoffset));
  466. }
  467. return ret;
  468. }
  469. int fdt_get_node_and_value(void *blob,
  470. char *nodename,
  471. char *propname,
  472. void **var)
  473. {
  474. int len;
  475. int nodeoffset = 0;
  476. nodeoffset = fdt_path_offset(blob, nodename);
  477. if (nodeoffset >= 0) {
  478. *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len);
  479. if (len == 0) {
  480. /* no value */
  481. printf("%s no value\n", __func__);
  482. return -1;
  483. } else if (len > 0) {
  484. return len;
  485. } else {
  486. printf("libfdt fdt_getprop(): %s\n",
  487. fdt_strerror(len));
  488. return -2;
  489. }
  490. } else {
  491. printf("%s: cannot find %s node err:%s\n", __func__,
  492. nodename, fdt_strerror(nodeoffset));
  493. return -3;
  494. }
  495. }
  496. #endif
  497. #if !defined(MACH_TYPE_KM_KIRKWOOD)
  498. int ethernet_present(void)
  499. {
  500. struct km_bec_fpga *base = (struct km_bec_fpga *)CONFIG_SYS_PIGGY_BASE;
  501. return in_8(&base->bprth) & PIGGY_PRESENT;
  502. }
  503. #endif
  504. int board_eth_init(bd_t *bis)
  505. {
  506. #ifdef CONFIG_KEYMILE_HDLC_ENET
  507. (void)keymile_hdlc_enet_initialize(bis);
  508. #endif
  509. if (ethernet_present())
  510. return -1;
  511. return 0;
  512. }