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 <common.h>
  40. #include <environment.h>
  41. #include <net.h>
  42. #include <configs/ml300.h>
  43. #include "xparameters.h"
  44. #ifdef CFG_ENV_IS_IN_EEPROM
  45. #include <i2c.h>
  46. #include "xiic_l.h"
  47. #define IIC_DELAY 5000
  48. static u8 envStep = 0; /* 0 means crc has not been read */
  49. const u8 hex[] = "0123456789ABCDEF"; /* lookup table for ML300 CRC */
  50. /************************************************************************
  51. * Use Xilinx provided driver to send data to EEPROM using iic bus.
  52. */
  53. static void
  54. send(u32 adr, u8 * data, u32 len)
  55. {
  56. u8 sendBuf[34]; /* first 2-bit is address and others are data */
  57. u32 pos, wlen;
  58. u32 ret;
  59. wlen = 32;
  60. for (pos = 0; pos < len; pos += 32) {
  61. if ((len - pos) < 32)
  62. wlen = len - pos;
  63. /* Put address and data bits together */
  64. sendBuf[0] = (u8) ((adr + pos) >> 8);
  65. sendBuf[1] = (u8) (adr + pos);
  66. memcpy(&sendBuf[2], &data[pos], wlen);
  67. /* Send to EEPROM through iic bus */
  68. ret = XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1,
  69. sendBuf, wlen + 2);
  70. udelay(IIC_DELAY);
  71. }
  72. }
  73. /************************************************************************
  74. * Use Xilinx provided driver to read data from EEPROM using the iic bus.
  75. */
  76. static void
  77. receive(u32 adr, u8 * data, u32 len)
  78. {
  79. u8 address[2];
  80. u32 ret;
  81. address[0] = (u8) (adr >> 8);
  82. address[1] = (u8) adr;
  83. /* Provide EEPROM address */
  84. ret =
  85. XIic_Send(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, address,
  86. 2);
  87. /* Receive data from EEPROM */
  88. ret =
  89. XIic_Recv(XPAR_IIC_0_BASEADDR, CFG_I2C_EEPROM_ADDR >> 1, data, len);
  90. }
  91. /************************************************************************
  92. * Convert a hexadecimal string to its equivalent integer value.
  93. */
  94. static u8
  95. axtoi(u8 * hexStg)
  96. {
  97. u8 n; /* position in string */
  98. u8 m; /* position in digit[] to shift */
  99. u8 count; /* loop index */
  100. u8 intValue; /* integer value of hex string */
  101. u8 digit[2]; /* hold values to convert */
  102. for (n = 0; n < 2; n++) {
  103. if (hexStg[n] == '\0')
  104. break;
  105. if (hexStg[n] > 0x29 && hexStg[n] < 0x40)
  106. digit[n] = hexStg[n] & 0x0f;
  107. else if (hexStg[n] >= 'a' && hexStg[n] <= 'f')
  108. digit[n] = (hexStg[n] & 0x0f) + 9;
  109. else if (hexStg[n] >= 'A' && hexStg[n] <= 'F')
  110. digit[n] = (hexStg[n] & 0x0f) + 9;
  111. else
  112. break;
  113. }
  114. intValue = 0;
  115. count = n;
  116. m = n - 1;
  117. n = 0;
  118. while (n < count) {
  119. intValue = intValue | (digit[n] << (m << 2));
  120. m--; /* adjust the position to set */
  121. n++; /* next digit to process */
  122. }
  123. return (intValue);
  124. }
  125. /************************************************************************
  126. * Convert an integer string to its equivalent value.
  127. */
  128. static u8
  129. atoi(uchar * string)
  130. {
  131. u8 res = 0;
  132. while (*string >= '0' && *string <= '9') {
  133. res *= 10;
  134. res += *string - '0';
  135. string++;
  136. }
  137. return res;
  138. }
  139. /************************************************************************
  140. * Key-value pairs are separated by "=" sign.
  141. */
  142. static void
  143. findKey(uchar * buffer, int *loc, u8 len)
  144. {
  145. u32 i;
  146. for (i = 0; i < len; i++)
  147. if (buffer[i] == '=') {
  148. *loc = i;
  149. return;
  150. }
  151. /* return -1 is no "=" sign found */
  152. *loc = -1;
  153. }
  154. /************************************************************************
  155. * Compute a new ML300 CRC when user calls the saveenv command.
  156. * Also update EEPROM with new CRC value.
  157. */
  158. static u8
  159. update_crc(u32 len, uchar * data)
  160. {
  161. uchar temp[6] = { 'C', '=', 0x00, 0x00, 0x00, 0x00 };
  162. u32 crc; /* new crc value */
  163. u32 i;
  164. crc = 0;
  165. /* calculate new CRC */
  166. for (i = 0; i < len; i++)
  167. crc += data[i];
  168. /* CRC includes key for check sum */
  169. crc += 'C' + '=';
  170. /* compose new CRC to be updated */
  171. temp[2] = hex[(crc >> 4) & 0xf];
  172. temp[3] = hex[crc & 0xf];
  173. /* check to see if env size exceeded */
  174. if (len + 6 > ENV_SIZE) {
  175. printf("ERROR: not enough space to store CRC on EEPROM");
  176. return 1;
  177. }
  178. memcpy(data + len, temp, 6);
  179. return 0;
  180. }
  181. /************************************************************************
  182. * Read out ML300 CRC and compare it with a runtime calculated ML300 CRC.
  183. * If equal, then pass back a u-boot CRC value, otherwise pass back
  184. * junk to indicate CRC error.
  185. */
  186. static void
  187. read_crc(uchar * buffer, int len)
  188. {
  189. u32 addr, n;
  190. u32 crc; /* runtime crc */
  191. u8 old[2] = { 0xff, 0xff }; /* current CRC in EEPROM */
  192. u8 stop; /* indication of end of env data */
  193. u8 pre; /* previous EEPROM data bit */
  194. int i, loc;
  195. addr = CFG_ENV_OFFSET; /* start from first env address */
  196. n = 0;
  197. pre = 1;
  198. stop = 1;
  199. crc = 0;
  200. /* calculate runtime CRC according to ML300 and read back
  201. old CRC stored in the EEPROM */
  202. while (n < CFG_ENV_SIZE) {
  203. receive(addr, buffer, len);
  204. /* found two null chars, end of env */
  205. if ((pre || buffer[0]) == 0)
  206. break;
  207. findKey(buffer, &loc, len);
  208. /* found old check sum, read and store old CRC */
  209. if ((loc == 0 && pre == 'C')
  210. || (loc > 0 && buffer[loc - 1] == 'C'))
  211. receive(addr + loc + 1, old, 2);
  212. pre = buffer[len - 1];
  213. /* calculate runtime ML300 CRC */
  214. crc += buffer[0];
  215. i = 1;
  216. do {
  217. crc += buffer[i];
  218. stop = buffer[i] || buffer[i - 1];
  219. i++;
  220. } while (stop && (i < len));
  221. if (stop == 0)
  222. break;
  223. n += len;
  224. addr += len;
  225. }
  226. /* exclude old CRC from runtime calculation */
  227. crc -= (old[0] + old[1]);
  228. /* match CRC values, send back u-boot CRC */
  229. if ((old[0] == hex[(crc >> 4) & 0xf])
  230. && (old[1] == hex[crc & 0xf])) {
  231. crc = 0;
  232. n = 0;
  233. addr =
  234. CFG_ENV_OFFSET - offsetof(env_t, crc) + offsetof(env_t,
  235. data);
  236. /* calculate u-boot crc */
  237. while (n < ENV_SIZE) {
  238. receive(addr, buffer, len);
  239. crc = crc32(crc, buffer, len);
  240. n += len;
  241. addr += len;
  242. }
  243. memcpy(buffer, &crc, 4);
  244. }
  245. }
  246. /************************************************************************
  247. * Convert IP address to hexadecimals.
  248. */
  249. static void
  250. ip_ml300(uchar * s, uchar * res)
  251. {
  252. uchar temp[2];
  253. u8 i;
  254. res[0] = 0x00;
  255. for (i = 0; i < 4; i++) {
  256. sprintf(temp, "%02x", atoi(s));
  257. s = strchr(s, '.') + 1;
  258. strcat(res, temp);
  259. }
  260. }
  261. /************************************************************************
  262. * Change 0xff (255), a dummy null char to 0x00.
  263. */
  264. static void
  265. change_null(uchar * s)
  266. {
  267. if (s != NULL) {
  268. change_null(strchr(s + 1, 255));
  269. *(strchr(s, 255)) = '\0';
  270. }
  271. }
  272. /************************************************************************
  273. * Update environment variable name and values to u-boot standard.
  274. */
  275. void
  276. convert_env(void)
  277. {
  278. uchar *s; /* pointer to env value */
  279. uchar temp[20]; /* temp storage for addresses */
  280. /* E -> ethaddr */
  281. s = getenv("E");
  282. if (s != NULL) {
  283. sprintf(temp, "%c%c.%c%c.%c%c.%c%c.%c%c.%c%c",
  284. *s++, *s++, *s++, *s++, *s++, *s++,
  285. *s++, *s++, *s++, *s++, *s++, *s);
  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(s), axtoi(s + 2),
  299. axtoi(s + 4), axtoi(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(s), axtoi(s + 2),
  307. axtoi(s + 4), axtoi(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. uchar eprom[ENV_SIZE]; /* buffer to be written back to EEPROM */
  339. uchar *s, temp[20];
  340. uchar 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(s, 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(s, 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(eprom); /* change 0xff to 0x00 */
  400. /* update EEPROM env values if there is enough space */
  401. if (update_crc(len, eprom) == 0)
  402. send(CFG_ENV_OFFSET, 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