iic_adapter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /******************************************************************************
  2. *
  3. * Author: Xilinx, Inc.
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. *
  12. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
  13. * COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
  14. * ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR STANDARD,
  15. * XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION IS FREE
  16. * FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE FOR OBTAINING
  17. * ANY THIRD PARTY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
  18. * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
  19. * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY
  20. * WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM
  21. * CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. * FITNESS FOR A PARTICULAR PURPOSE.
  23. *
  24. *
  25. * Xilinx hardware products are not intended for use in life support
  26. * appliances, devices, or systems. Use in such applications is
  27. * expressly prohibited.
  28. *
  29. *
  30. * (c) Copyright 2002-2004 Xilinx Inc.
  31. * All rights reserved.
  32. *
  33. *
  34. * You should have received a copy of the GNU General Public License along
  35. * with this program; if not, write to the Free Software Foundation, Inc.,
  36. * 675 Mass Ave, Cambridge, MA 02139, USA.
  37. *
  38. ******************************************************************************/
  39. #include <config.h>
  40. #include <common.h>
  41. #include <environment.h>
  42. #include <net.h>
  43. #ifdef CFG_ENV_IS_IN_EEPROM
  44. #include <i2c.h>
  45. #include "xiic_l.h"
  46. #define IIC_DELAY 5000
  47. static u8 envStep = 0; /* 0 means crc has not been read */
  48. const u8 hex[] = "0123456789ABCDEF"; /* lookup table for ML300 CRC */
  49. /************************************************************************
  50. * Use Xilinx provided driver to send data to EEPROM using iic bus.
  51. */
  52. static void
  53. send(u32 adr, u8 * data, u32 len)
  54. {
  55. u8 sendBuf[34]; /* first 2-bit is address and others are data */
  56. u32 pos, wlen;
  57. u32 ret;
  58. wlen = 32;
  59. for (pos = 0; pos < len; pos += 32) {
  60. if ((len - pos) < 32)
  61. wlen = len - pos;
  62. /* Put address and data bits together */
  63. sendBuf[0] = (u8) ((adr + pos) >> 8);
  64. sendBuf[1] = (u8) (adr + pos);
  65. memcpy(&sendBuf[2], &data[pos], wlen);
  66. /* Send to EEPROM through iic bus */
  67. ret = XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1,
  68. sendBuf, wlen + 2);
  69. udelay(IIC_DELAY);
  70. }
  71. }
  72. /************************************************************************
  73. * Use Xilinx provided driver to read data from EEPROM using the iic bus.
  74. */
  75. static void
  76. receive(u32 adr, u8 * data, u32 len)
  77. {
  78. u8 address[2];
  79. u32 ret;
  80. address[0] = (u8) (adr >> 8);
  81. address[1] = (u8) adr;
  82. /* Provide EEPROM address */
  83. ret =
  84. XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, address,
  85. 2);
  86. /* Receive data from EEPROM */
  87. ret =
  88. XIic_Recv(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, data, len);
  89. }
  90. /************************************************************************
  91. * Convert a hexadecimal string to its equivalent integer value.
  92. */
  93. static u8
  94. axtoi(u8 * hexStg)
  95. {
  96. u8 n; /* position in string */
  97. u8 m; /* position in digit[] to shift */
  98. u8 count; /* loop index */
  99. u8 intValue; /* integer value of hex string */
  100. u8 digit[2]; /* hold values to convert */
  101. for (n = 0; n < 2; n++) {
  102. if (hexStg[n] == '\0')
  103. break;
  104. if (hexStg[n] > 0x29 && hexStg[n] < 0x40)
  105. digit[n] = hexStg[n] & 0x0f;
  106. else if (hexStg[n] >= 'a' && hexStg[n] <= 'f')
  107. digit[n] = (hexStg[n] & 0x0f) + 9;
  108. else if (hexStg[n] >= 'A' && hexStg[n] <= 'F')
  109. digit[n] = (hexStg[n] & 0x0f) + 9;
  110. else
  111. break;
  112. }
  113. intValue = 0;
  114. count = n;
  115. m = n - 1;
  116. n = 0;
  117. while (n < count) {
  118. intValue = intValue | (digit[n] << (m << 2));
  119. m--; /* adjust the position to set */
  120. n++; /* next digit to process */
  121. }
  122. return (intValue);
  123. }
  124. /************************************************************************
  125. * Convert an integer string to its equivalent value.
  126. */
  127. static u8
  128. atoi(uchar * string)
  129. {
  130. u8 res = 0;
  131. while (*string >= '0' && *string <= '9') {
  132. res *= 10;
  133. res += *string - '0';
  134. string++;
  135. }
  136. return res;
  137. }
  138. /************************************************************************
  139. * Key-value pairs are separated by "=" sign.
  140. */
  141. static void
  142. findKey(uchar * buffer, int *loc, u8 len)
  143. {
  144. u32 i;
  145. for (i = 0; i < len; i++)
  146. if (buffer[i] == '=') {
  147. *loc = i;
  148. return;
  149. }
  150. /* return -1 is no "=" sign found */
  151. *loc = -1;
  152. }
  153. /************************************************************************
  154. * Compute a new ML300 CRC when user calls the saveenv command.
  155. * Also update EEPROM with new CRC value.
  156. */
  157. static u8
  158. update_crc(u32 len, uchar * data)
  159. {
  160. uchar temp[6] = { 'C', '=', 0x00, 0x00, 0x00, 0x00 };
  161. u32 crc; /* new crc value */
  162. u32 i;
  163. crc = 0;
  164. /* calculate new CRC */
  165. for (i = 0; i < len; i++)
  166. crc += data[i];
  167. /* CRC includes key for check sum */
  168. crc += 'C' + '=';
  169. /* compose new CRC to be updated */
  170. temp[2] = hex[(crc >> 4) & 0xf];
  171. temp[3] = hex[crc & 0xf];
  172. /* check to see if env size exceeded */
  173. if (len + 6 > ENV_SIZE) {
  174. printf("ERROR: not enough space to store CRC on EEPROM");
  175. return 1;
  176. }
  177. memcpy(data + len, temp, 6);
  178. return 0;
  179. }
  180. /************************************************************************
  181. * Read out ML300 CRC and compare it with a runtime calculated ML300 CRC.
  182. * If equal, then pass back a u-boot CRC value, otherwise pass back
  183. * junk to indicate CRC error.
  184. */
  185. static void
  186. read_crc(uchar * buffer, int len)
  187. {
  188. u32 addr, n;
  189. u32 crc; /* runtime crc */
  190. u8 old[2] = { 0xff, 0xff }; /* current CRC in EEPROM */
  191. u8 stop; /* indication of end of env data */
  192. u8 pre; /* previous EEPROM data bit */
  193. int i, loc;
  194. addr = CFG_ENV_OFFSET; /* start from first env address */
  195. n = 0;
  196. pre = 1;
  197. stop = 1;
  198. crc = 0;
  199. /* calculate runtime CRC according to ML300 and read back
  200. old CRC stored in the EEPROM */
  201. while (n < CFG_ENV_SIZE) {
  202. receive(addr, buffer, len);
  203. /* found two null chars, end of env */
  204. if ((pre || buffer[0]) == 0)
  205. break;
  206. findKey(buffer, &loc, len);
  207. /* found old check sum, read and store old CRC */
  208. if ((loc == 0 && pre == 'C')
  209. || (loc > 0 && buffer[loc - 1] == 'C'))
  210. receive(addr + loc + 1, old, 2);
  211. pre = buffer[len - 1];
  212. /* calculate runtime ML300 CRC */
  213. crc += buffer[0];
  214. i = 1;
  215. do {
  216. crc += buffer[i];
  217. stop = buffer[i] || buffer[i - 1];
  218. i++;
  219. } while (stop && (i < len));
  220. if (stop == 0)
  221. break;
  222. n += len;
  223. addr += len;
  224. }
  225. /* exclude old CRC from runtime calculation */
  226. crc -= (old[0] + old[1]);
  227. /* match CRC values, send back u-boot CRC */
  228. if ((old[0] == hex[(crc >> 4) & 0xf])
  229. && (old[1] == hex[crc & 0xf])) {
  230. crc = 0;
  231. n = 0;
  232. addr =
  233. CFG_ENV_OFFSET - offsetof(env_t, crc) + offsetof(env_t,
  234. data);
  235. /* calculate u-boot crc */
  236. while (n < ENV_SIZE) {
  237. receive(addr, buffer, len);
  238. crc = crc32(crc, buffer, len);
  239. n += len;
  240. addr += len;
  241. }
  242. memcpy(buffer, &crc, 4);
  243. }
  244. }
  245. /************************************************************************
  246. * Convert IP address to hexadecimals.
  247. */
  248. static void
  249. ip_ml300(uchar * s, uchar * res)
  250. {
  251. char temp[2];
  252. u8 i;
  253. res[0] = 0x00;
  254. for (i = 0; i < 4; i++) {
  255. sprintf(temp, "%02x", atoi(s));
  256. s = (uchar *)strchr((char *)s, '.') + 1;
  257. strcat((char *)res, temp);
  258. }
  259. }
  260. /************************************************************************
  261. * Change 0xff (255), a dummy null char to 0x00.
  262. */
  263. static void
  264. change_null(uchar * s)
  265. {
  266. if (s != NULL) {
  267. change_null((uchar *)strchr((char *)s + 1, 255));
  268. *(strchr((char *)s, 255)) = '\0';
  269. }
  270. }
  271. /************************************************************************
  272. * Update environment variable name and values to u-boot standard.
  273. */
  274. void
  275. convert_env(void)
  276. {
  277. char *s; /* pointer to env value */
  278. char temp[20]; /* temp storage for addresses */
  279. /* E -> ethaddr */
  280. s = getenv("E");
  281. if (s != NULL) {
  282. sprintf(temp, "%c%c.%c%c.%c%c.%c%c.%c%c.%c%c",
  283. s[0], s[1], s[ 2], s[ 3],
  284. s[4], s[5], s[ 6], s[ 7],
  285. s[8], s[9], s[10], s[11] );
  286. setenv("ethaddr", temp);
  287. setenv("E", NULL);
  288. }
  289. /* L -> serial# */
  290. s = getenv("L");
  291. if (s != NULL) {
  292. setenv("serial#", s);
  293. setenv("L", NULL);
  294. }
  295. /* I -> ipaddr */
  296. s = getenv("I");
  297. if (s != NULL) {
  298. sprintf(temp, "%d.%d.%d.%d", axtoi((u8 *)s), axtoi((u8 *)(s + 2)),
  299. axtoi((u8 *)(s + 4)), axtoi((u8 *)(s + 6)));
  300. setenv("ipaddr", temp);
  301. setenv("I", NULL);
  302. }
  303. /* S -> serverip */
  304. s = getenv("S");
  305. if (s != NULL) {
  306. sprintf(temp, "%d.%d.%d.%d", axtoi((u8 *)s), axtoi((u8 *)(s + 2)),
  307. axtoi((u8 *)(s + 4)), axtoi((u8 *)(s + 6)));
  308. setenv("serverip", temp);
  309. setenv("S", NULL);
  310. }
  311. /* A -> bootargs */
  312. s = getenv("A");
  313. if (s != NULL) {
  314. setenv("bootargs", s);
  315. setenv("A", NULL);
  316. }
  317. /* F -> bootfile */
  318. s = getenv("F");
  319. if (s != NULL) {
  320. setenv("bootfile", s);
  321. setenv("F", NULL);
  322. }
  323. /* M -> bootcmd */
  324. s = getenv("M");
  325. if (s != NULL) {
  326. setenv("bootcmd", s);
  327. setenv("M", NULL);
  328. }
  329. /* Don't include C (CRC) */
  330. setenv("C", NULL);
  331. }
  332. /************************************************************************
  333. * Save user modified environment values back to EEPROM.
  334. */
  335. static void
  336. save_env(void)
  337. {
  338. char eprom[ENV_SIZE]; /* buffer to be written back to EEPROM */
  339. char *s, temp[20];
  340. char ff[] = { 0xff, 0x00 }; /* dummy null value */
  341. u32 len; /* length of env to be written to EEPROM */
  342. eprom[0] = 0x00;
  343. /* ethaddr -> E */
  344. s = getenv("ethaddr");
  345. if (s != NULL) {
  346. strcat(eprom, "E=");
  347. sprintf(temp, "%c%c%c%c%c%c%c%c%c%c%c%c",
  348. *s, *(s + 1), *(s + 3), *(s + 4), *(s + 6), *(s + 7),
  349. *(s + 9), *(s + 10), *(s + 12), *(s + 13), *(s + 15),
  350. *(s + 16));
  351. strcat(eprom, temp);
  352. strcat(eprom, ff);
  353. }
  354. /* serial# -> L */
  355. s = getenv("serial#");
  356. if (s != NULL) {
  357. strcat(eprom, "L=");
  358. strcat(eprom, s);
  359. strcat(eprom, ff);
  360. }
  361. /* ipaddr -> I */
  362. s = getenv("ipaddr");
  363. if (s != NULL) {
  364. strcat(eprom, "I=");
  365. ip_ml300((uchar *)s, (uchar *)temp);
  366. strcat(eprom, temp);
  367. strcat(eprom, ff);
  368. }
  369. /* serverip -> S */
  370. s = getenv("serverip");
  371. if (s != NULL) {
  372. strcat(eprom, "S=");
  373. ip_ml300((uchar *)s, (uchar *)temp);
  374. strcat(eprom, temp);
  375. strcat(eprom, ff);
  376. }
  377. /* bootargs -> A */
  378. s = getenv("bootargs");
  379. if (s != NULL) {
  380. strcat(eprom, "A=");
  381. strcat(eprom, s);
  382. strcat(eprom, ff);
  383. }
  384. /* bootfile -> F */
  385. s = getenv("bootfile");
  386. if (s != NULL) {
  387. strcat(eprom, "F=");
  388. strcat(eprom, s);
  389. strcat(eprom, ff);
  390. }
  391. /* bootcmd -> M */
  392. s = getenv("bootcmd");
  393. if (s != NULL) {
  394. strcat(eprom, "M=");
  395. strcat(eprom, s);
  396. strcat(eprom, ff);
  397. }
  398. len = strlen(eprom); /* find env length without crc */
  399. change_null((uchar *)eprom); /* change 0xff to 0x00 */
  400. /* update EEPROM env values if there is enough space */
  401. if (update_crc(len, (uchar *)eprom) == 0)
  402. send(CFG_ENV_OFFSET, (uchar *)eprom, len + 6);
  403. }
  404. /************************************************************************
  405. * U-boot call for EEPROM read associated activities.
  406. */
  407. int
  408. i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
  409. {
  410. if (envStep == 0) {
  411. /* first read call is for crc */
  412. read_crc(buffer, len);
  413. ++envStep;
  414. return 0;
  415. } else if (envStep == 1) {
  416. /* then read out EEPROM content for runtime u-boot CRC calculation */
  417. receive(addr, buffer, len);
  418. if (addr + len - CFG_ENV_OFFSET == CFG_ENV_SIZE)
  419. /* end of runtime crc read */
  420. ++envStep;
  421. return 0;
  422. }
  423. if (len < 2) {
  424. /* when call getenv_r */
  425. receive(addr, buffer, len);
  426. } else if (addr + len < CFG_ENV_OFFSET + CFG_ENV_SIZE) {
  427. /* calling env_relocate(), but don't read out
  428. crc value from EEPROM */
  429. receive(addr, buffer + 4, len);
  430. } else {
  431. receive(addr, buffer + 4, len - 4);
  432. }
  433. return 0;
  434. }
  435. /************************************************************************
  436. * U-boot call for EEPROM write acativities.
  437. */
  438. int
  439. i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
  440. {
  441. /* save env on last page write called by u-boot */
  442. if (addr + len >= CFG_ENV_OFFSET + CFG_ENV_SIZE)
  443. save_env();
  444. return 0;
  445. }
  446. /************************************************************************
  447. * Dummy function.
  448. */
  449. int
  450. i2c_probe(uchar chip)
  451. {
  452. return 1;
  453. }
  454. #endif