common.c 14 KB

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