iic_adapter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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[0], s[1], s[ 2], s[ 3],
  285. s[4], s[5], s[ 6], s[ 7],
  286. s[8], s[9], s[10], s[11] );
  287. setenv("ethaddr", temp);
  288. setenv("E", NULL);
  289. }
  290. /* L -> serial# */
  291. s = getenv("L");
  292. if (s != NULL) {
  293. setenv("serial#", s);
  294. setenv("L", NULL);
  295. }
  296. /* I -> ipaddr */
  297. s = getenv("I");
  298. if (s != NULL) {
  299. sprintf(temp, "%d.%d.%d.%d", axtoi(s), axtoi(s + 2),
  300. axtoi(s + 4), axtoi(s + 6));
  301. setenv("ipaddr", temp);
  302. setenv("I", NULL);
  303. }
  304. /* S -> serverip */
  305. s = getenv("S");
  306. if (s != NULL) {
  307. sprintf(temp, "%d.%d.%d.%d", axtoi(s), axtoi(s + 2),
  308. axtoi(s + 4), axtoi(s + 6));
  309. setenv("serverip", temp);
  310. setenv("S", NULL);
  311. }
  312. /* A -> bootargs */
  313. s = getenv("A");
  314. if (s != NULL) {
  315. setenv("bootargs", s);
  316. setenv("A", NULL);
  317. }
  318. /* F -> bootfile */
  319. s = getenv("F");
  320. if (s != NULL) {
  321. setenv("bootfile", s);
  322. setenv("F", NULL);
  323. }
  324. /* M -> bootcmd */
  325. s = getenv("M");
  326. if (s != NULL) {
  327. setenv("bootcmd", s);
  328. setenv("M", NULL);
  329. }
  330. /* Don't include C (CRC) */
  331. setenv("C", NULL);
  332. }
  333. /************************************************************************
  334. * Save user modified environment values back to EEPROM.
  335. */
  336. static void
  337. save_env(void)
  338. {
  339. uchar eprom[ENV_SIZE]; /* buffer to be written back to EEPROM */
  340. uchar *s, temp[20];
  341. uchar ff[] = { 0xff, 0x00 }; /* dummy null value */
  342. u32 len; /* length of env to be written to EEPROM */
  343. eprom[0] = 0x00;
  344. /* ethaddr -> E */
  345. s = getenv("ethaddr");
  346. if (s != NULL) {
  347. strcat(eprom, "E=");
  348. sprintf(temp, "%c%c%c%c%c%c%c%c%c%c%c%c",
  349. *s, *(s + 1), *(s + 3), *(s + 4), *(s + 6), *(s + 7),
  350. *(s + 9), *(s + 10), *(s + 12), *(s + 13), *(s + 15),
  351. *(s + 16));
  352. strcat(eprom, temp);
  353. strcat(eprom, ff);
  354. }
  355. /* serial# -> L */
  356. s = getenv("serial#");
  357. if (s != NULL) {
  358. strcat(eprom, "L=");
  359. strcat(eprom, s);
  360. strcat(eprom, ff);
  361. }
  362. /* ipaddr -> I */
  363. s = getenv("ipaddr");
  364. if (s != NULL) {
  365. strcat(eprom, "I=");
  366. ip_ml300(s, temp);
  367. strcat(eprom, temp);
  368. strcat(eprom, ff);
  369. }
  370. /* serverip -> S */
  371. s = getenv("serverip");
  372. if (s != NULL) {
  373. strcat(eprom, "S=");
  374. ip_ml300(s, temp);
  375. strcat(eprom, temp);
  376. strcat(eprom, ff);
  377. }
  378. /* bootargs -> A */
  379. s = getenv("bootargs");
  380. if (s != NULL) {
  381. strcat(eprom, "A=");
  382. strcat(eprom, s);
  383. strcat(eprom, ff);
  384. }
  385. /* bootfile -> F */
  386. s = getenv("bootfile");
  387. if (s != NULL) {
  388. strcat(eprom, "F=");
  389. strcat(eprom, s);
  390. strcat(eprom, ff);
  391. }
  392. /* bootcmd -> M */
  393. s = getenv("bootcmd");
  394. if (s != NULL) {
  395. strcat(eprom, "M=");
  396. strcat(eprom, s);
  397. strcat(eprom, ff);
  398. }
  399. len = strlen(eprom); /* find env length without crc */
  400. change_null(eprom); /* change 0xff to 0x00 */
  401. /* update EEPROM env values if there is enough space */
  402. if (update_crc(len, eprom) == 0)
  403. send(CFG_ENV_OFFSET, eprom, len + 6);
  404. }
  405. /************************************************************************
  406. * U-boot call for EEPROM read associated activities.
  407. */
  408. int
  409. i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
  410. {
  411. if (envStep == 0) {
  412. /* first read call is for crc */
  413. read_crc(buffer, len);
  414. ++envStep;
  415. return 0;
  416. } else if (envStep == 1) {
  417. /* then read out EEPROM content for runtime u-boot CRC calculation */
  418. receive(addr, buffer, len);
  419. if (addr + len - CFG_ENV_OFFSET == CFG_ENV_SIZE)
  420. /* end of runtime crc read */
  421. ++envStep;
  422. return 0;
  423. }
  424. if (len < 2) {
  425. /* when call getenv_r */
  426. receive(addr, buffer, len);
  427. } else if (addr + len < CFG_ENV_OFFSET + CFG_ENV_SIZE) {
  428. /* calling env_relocate(), but don't read out
  429. crc value from EEPROM */
  430. receive(addr, buffer + 4, len);
  431. } else {
  432. receive(addr, buffer + 4, len - 4);
  433. }
  434. return 0;
  435. }
  436. /************************************************************************
  437. * U-boot call for EEPROM write acativities.
  438. */
  439. int
  440. i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
  441. {
  442. /* save env on last page write called by u-boot */
  443. if (addr + len >= CFG_ENV_OFFSET + CFG_ENV_SIZE)
  444. save_env();
  445. return 0;
  446. }
  447. /************************************************************************
  448. * Dummy function.
  449. */
  450. int
  451. i2c_probe(uchar chip)
  452. {
  453. return 1;
  454. }
  455. #endif