common.c 12 KB

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