common.c 13 KB

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