common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. extern int i2c_soft_read_pin (void);
  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", __FUNCTION__, 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 INVENTORYBLOCKSIZE 0x100
  94. #define INVENTORYDATAADDRESS 0x21
  95. #define INVENTORYDATASIZE (INVENTORYBLOCKSIZE - INVENTORYDATAADDRESS - 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 = INVENTORYDATAADDRESS;
  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. /* the expected number of CR was found until the end of the IVM
  141. * content --> fill string */
  142. if (addr < INVENTORYDATASIZE) {
  143. /* Copy the IVM string in the corresponding string */
  144. for (; (buf[addr] != '\r') &&
  145. ((buf[addr] != ';') || (!stop)) &&
  146. (size < (maxlen - 1) &&
  147. (addr < INVENTORYDATASIZE)); addr++)
  148. {
  149. size += sprintf((char *)string + size, "%c",
  150. convert_char (buf[addr]));
  151. }
  152. /* copy phase is done: check if everything is ok. If not,
  153. * the inventory data is most probably corrupted: tell
  154. * the world there is a problem! */
  155. if (addr == INVENTORYDATASIZE) {
  156. xcode = -1;
  157. printf ("Error end of string not found\n");
  158. } else if ((size >= (maxlen - 1)) &&
  159. (buf[addr] != '\r')) {
  160. xcode = -1;
  161. printf ("string too long till next CR\n");
  162. }
  163. } else {
  164. /* some CR are missing...
  165. * the inventory data is most probably corrupted */
  166. xcode = -1;
  167. printf ("not enough cr found\n");
  168. }
  169. return xcode;
  170. }
  171. #define GET_STRING(name, which, len) \
  172. if (ivm_findinventorystring (which, valbuf, len, buf) == 0) { \
  173. ivm_set_value (name, (char *)valbuf); \
  174. }
  175. static int ivm_check_crc (unsigned char *buf, int block)
  176. {
  177. unsigned long crc;
  178. unsigned long crceeprom;
  179. crc = ivm_calc_crc (buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
  180. crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
  181. buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
  182. if (crc != crceeprom) {
  183. if (block == 0)
  184. printf ("Error CRC Block: %d EEprom: calculated: \
  185. %lx EEprom: %lx\n", block, crc, crceeprom);
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. static int ivm_analyze_block2 (unsigned char *buf, int len)
  191. {
  192. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  193. unsigned long count;
  194. /* IVM_MacAddress */
  195. sprintf ((char *)valbuf, "%02X:%02X:%02X:%02X:%02X:%02X",
  196. buf[1],
  197. buf[2],
  198. buf[3],
  199. buf[4],
  200. buf[5],
  201. buf[6]);
  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, "IVM_BoardId", 0, 1);
  224. val = ivm_get_value (buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, "IVM_HWKey", 6, 1);
  225. if (val != 0xffff) {
  226. sprintf ((char *)valbuf, "%x", ((val /100) % 10));
  227. ivm_set_value ("IVM_HWVariant", (char *)valbuf);
  228. sprintf ((char *)valbuf, "%x", (val % 100));
  229. ivm_set_value ("IVM_HWVersion", (char *)valbuf);
  230. }
  231. ivm_get_value (buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, "IVM_Functions", 12, 0);
  232. GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
  233. GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
  234. tmp = (unsigned char *) getenv("IVM_DeviceName");
  235. if (tmp) {
  236. int len = strlen ((char *)tmp);
  237. int i = 0;
  238. while (i < len) {
  239. if (tmp[i] == ';') {
  240. ivm_set_value ("IVM_ShortText", (char *)&tmp[i + 1]);
  241. break;
  242. }
  243. i++;
  244. }
  245. if (i >= len)
  246. ivm_set_value ("IVM_ShortText", NULL);
  247. } else {
  248. ivm_set_value ("IVM_ShortText", NULL);
  249. }
  250. GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
  251. GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
  252. GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
  253. GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
  254. GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
  255. GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
  256. GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
  257. GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
  258. GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
  259. if (ivm_check_crc (&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
  260. return 0;
  261. ivm_analyze_block2 (&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
  262. return 0;
  263. }
  264. int ivm_read_eeprom (void)
  265. {
  266. #if defined(CONFIG_I2C_MUX)
  267. I2C_MUX_DEVICE *dev = NULL;
  268. #endif
  269. uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
  270. uchar *buf;
  271. unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
  272. #if defined(CONFIG_I2C_MUX)
  273. /* First init the Bus, select the Bus */
  274. #if defined(CONFIG_SYS_I2C_IVM_BUS)
  275. dev = i2c_mux_ident_muxstring ((uchar *)CONFIG_SYS_I2C_IVM_BUS);
  276. #else
  277. buf = (unsigned char *) getenv ("EEprom_ivm");
  278. if (buf != NULL)
  279. dev = i2c_mux_ident_muxstring (buf);
  280. #endif
  281. if (dev == NULL) {
  282. printf ("Error couldnt add Bus for IVM\n");
  283. return -1;
  284. }
  285. i2c_set_bus_num (dev->busid);
  286. #endif
  287. buf = (unsigned char *) getenv ("EEprom_ivm_addr");
  288. if (buf != NULL)
  289. dev_addr = simple_strtoul ((char *)buf, NULL, 16);
  290. if (i2c_read(dev_addr, 0, 1, i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN) != 0) {
  291. printf ("Error reading EEprom\n");
  292. return -2;
  293. }
  294. return ivm_analyze_eeprom (i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  295. }
  296. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  297. #define DELAY_ABORT_SEQ 62
  298. #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
  299. #if defined(CONFIG_MGCOGE)
  300. #define SDA_MASK 0x00010000
  301. #define SCL_MASK 0x00020000
  302. static void set_pin (int state, unsigned long mask)
  303. {
  304. volatile ioport_t *iop = ioport_addr ((immap_t *)CONFIG_SYS_IMMR, 3);
  305. if (state)
  306. iop->pdat |= (mask);
  307. else
  308. iop->pdat &= ~(mask);
  309. iop->pdir |= (mask);
  310. }
  311. static int get_pin (unsigned long mask)
  312. {
  313. volatile ioport_t *iop = ioport_addr ((immap_t *)CONFIG_SYS_IMMR, 3);
  314. iop->pdir &= ~(mask);
  315. return (0 != (iop->pdat & (mask)));
  316. }
  317. static void set_sda (int state)
  318. {
  319. set_pin (state, SDA_MASK);
  320. }
  321. static void set_scl (int state)
  322. {
  323. set_pin (state, SCL_MASK);
  324. }
  325. static int get_sda (void)
  326. {
  327. return get_pin (SDA_MASK);
  328. }
  329. static int get_scl (void)
  330. {
  331. return get_pin (SCL_MASK);
  332. }
  333. #if defined(CONFIG_HARD_I2C)
  334. static void setports (int gpio)
  335. {
  336. volatile ioport_t *iop = ioport_addr ((immap_t *)CONFIG_SYS_IMMR, 3);
  337. if (gpio) {
  338. iop->ppar &= ~(SDA_MASK | SCL_MASK);
  339. iop->podr &= ~(SDA_MASK | SCL_MASK);
  340. } else {
  341. iop->ppar |= (SDA_MASK | SCL_MASK);
  342. iop->pdir &= ~(SDA_MASK | SCL_MASK);
  343. iop->podr |= (SDA_MASK | SCL_MASK);
  344. }
  345. }
  346. #endif
  347. #endif
  348. #if defined(CONFIG_KM8XX)
  349. static void set_sda (int state)
  350. {
  351. I2C_SDA(state);
  352. }
  353. static void set_scl (int state)
  354. {
  355. I2C_SCL(state);
  356. }
  357. static int get_sda (void)
  358. {
  359. return I2C_READ;
  360. }
  361. static int get_scl (void)
  362. {
  363. int val;
  364. *(unsigned short *)(I2C_BASE_DIR) &= ~SCL_CONF;
  365. udelay (1);
  366. val = *(unsigned char *)(I2C_BASE_PORT);
  367. return ((val & SCL_BIT) == SCL_BIT);
  368. }
  369. #endif
  370. #if !defined(CONFIG_KMETER1)
  371. static void writeStartSeq (void)
  372. {
  373. set_sda (1);
  374. udelay (DELAY_HALF_PERIOD);
  375. set_scl (1);
  376. udelay (DELAY_HALF_PERIOD);
  377. set_sda (0);
  378. udelay (DELAY_HALF_PERIOD);
  379. set_scl (0);
  380. udelay (DELAY_HALF_PERIOD);
  381. }
  382. /* I2C is a synchronous protocol and resets of the processor in the middle
  383. of an access can block the I2C Bus until a powerdown of the full unit is
  384. done. This function toggles the SCL until the SCL and SCA line are
  385. released, but max. 16 times, after this a I2C start-sequence is sent.
  386. This I2C Deblocking mechanism was developed by Keymile in association
  387. with Anatech and Atmel in 1998.
  388. */
  389. static int i2c_make_abort (void)
  390. {
  391. int scl_state = 0;
  392. int sda_state = 0;
  393. int i = 0;
  394. int ret = 0;
  395. if (!get_sda ()) {
  396. ret = -1;
  397. while (i < 16) {
  398. i++;
  399. set_scl (0);
  400. udelay (DELAY_ABORT_SEQ);
  401. set_scl (1);
  402. udelay (DELAY_ABORT_SEQ);
  403. scl_state = get_scl ();
  404. sda_state = get_sda ();
  405. if (scl_state && sda_state) {
  406. ret = 0;
  407. break;
  408. }
  409. }
  410. }
  411. if (ret == 0) {
  412. for (i =0; i < 5; i++) {
  413. writeStartSeq ();
  414. }
  415. }
  416. get_sda ();
  417. return ret;
  418. }
  419. #endif
  420. /**
  421. * i2c_init_board - reset i2c bus. When the board is powercycled during a
  422. * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
  423. */
  424. void i2c_init_board(void)
  425. {
  426. #if defined(CONFIG_KMETER1)
  427. struct fsl_i2c *dev;
  428. dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
  429. uchar dummy;
  430. out_8 (&dev->cr, (I2C_CR_MSTA));
  431. out_8 (&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
  432. dummy = in_8(&dev->dr);
  433. dummy = in_8(&dev->dr);
  434. if (dummy != 0xff) {
  435. dummy = in_8(&dev->dr);
  436. }
  437. out_8 (&dev->cr, (I2C_CR_MEN));
  438. out_8 (&dev->cr, 0x00);
  439. out_8 (&dev->cr, (I2C_CR_MEN));
  440. #else
  441. #if defined(CONFIG_HARD_I2C) && !defined(CONFIG_MACH_SUEN3)
  442. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
  443. volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  444. /* disable I2C controller first, otherwhise it thinks we want to */
  445. /* talk to the slave port... */
  446. i2c->i2c_i2mod &= ~0x01;
  447. /* Set the PortPins to GPIO */
  448. setports (1);
  449. #endif
  450. /* Now run the AbortSequence() */
  451. i2c_make_abort ();
  452. #if defined(CONFIG_HARD_I2C)
  453. /* Set the PortPins back to use for I2C */
  454. setports (0);
  455. #endif
  456. #endif
  457. }
  458. #endif
  459. #endif
  460. #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
  461. int fdt_set_node_and_value (void *blob,
  462. char *nodename,
  463. char *regname,
  464. void *var,
  465. int size)
  466. {
  467. int ret = 0;
  468. int nodeoffset = 0;
  469. nodeoffset = fdt_path_offset (blob, nodename);
  470. if (nodeoffset >= 0) {
  471. ret = fdt_setprop (blob, nodeoffset, regname, var,
  472. size);
  473. if (ret < 0)
  474. printf("ft_blob_update(): cannot set %s/%s "
  475. "property err:%s\n", nodename, regname,
  476. fdt_strerror (ret));
  477. } else {
  478. printf("ft_blob_update(): cannot find %s node "
  479. "err:%s\n", nodename, fdt_strerror (nodeoffset));
  480. }
  481. return ret;
  482. }
  483. int fdt_get_node_and_value (void *blob,
  484. char *nodename,
  485. char *propname,
  486. void **var)
  487. {
  488. int len;
  489. int nodeoffset = 0;
  490. nodeoffset = fdt_path_offset (blob, nodename);
  491. if (nodeoffset >= 0) {
  492. *var = (void *)fdt_getprop (blob, nodeoffset, propname, &len);
  493. if (len == 0) {
  494. /* no value */
  495. printf ("%s no value\n", __FUNCTION__);
  496. return -1;
  497. } else if (len > 0) {
  498. return len;
  499. } else {
  500. printf ("libfdt fdt_getprop(): %s\n",
  501. fdt_strerror(len));
  502. return -2;
  503. }
  504. } else {
  505. printf("%s: cannot find %s node err:%s\n", __FUNCTION__,
  506. nodename, fdt_strerror (nodeoffset));
  507. return -3;
  508. }
  509. }
  510. #endif
  511. #if !defined(CONFIG_MACH_SUEN3)
  512. int ethernet_present (void)
  513. {
  514. return (in_8((u8 *)CONFIG_SYS_PIGGY_BASE + CONFIG_SYS_SLOT_ID_OFF) & 0x80);
  515. }
  516. #endif
  517. int board_eth_init (bd_t *bis)
  518. {
  519. #ifdef CONFIG_KEYMILE_HDLC_ENET
  520. (void)keymile_hdlc_enet_initialize (bis);
  521. #endif
  522. if (ethernet_present ()) {
  523. return -1;
  524. }
  525. return 0;
  526. }