common.c 12 KB

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