cmd_i2c.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*
  2. * (C) Copyright 2001
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  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. /*
  24. * I2C Functions similar to the standard memory functions.
  25. *
  26. * There are several parameters in many of the commands that bear further
  27. * explanations:
  28. *
  29. * Two of the commands (imm and imw) take a byte/word/long modifier
  30. * (e.g. imm.w specifies the word-length modifier). This was done to
  31. * allow manipulating word-length registers. It was not done on any other
  32. * commands because it was not deemed useful.
  33. *
  34. * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
  35. * Each I2C chip on the bus has a unique address. On the I2C data bus,
  36. * the address is the upper seven bits and the LSB is the "read/write"
  37. * bit. Note that the {i2c_chip} address specified on the command
  38. * line is not shifted up: e.g. a typical EEPROM memory chip may have
  39. * an I2C address of 0x50, but the data put on the bus will be 0xA0
  40. * for write and 0xA1 for read. This "non shifted" address notation
  41. * matches at least half of the data sheets :-/.
  42. *
  43. * {addr} is the address (or offset) within the chip. Small memory
  44. * chips have 8 bit addresses. Large memory chips have 16 bit
  45. * addresses. Other memory chips have 9, 10, or 11 bit addresses.
  46. * Many non-memory chips have multiple registers and {addr} is used
  47. * as the register index. Some non-memory chips have only one register
  48. * and therefore don't need any {addr} parameter.
  49. *
  50. * The default {addr} parameter is one byte (.1) which works well for
  51. * memories and registers with 8 bits of address space.
  52. *
  53. * You can specify the length of the {addr} field with the optional .0,
  54. * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
  55. * manipulating a single register device which doesn't use an address
  56. * field, use "0.0" for the address and the ".0" length field will
  57. * suppress the address in the I2C data stream. This also works for
  58. * successive reads using the I2C auto-incrementing memory pointer.
  59. *
  60. * If you are manipulating a large memory with 2-byte addresses, use
  61. * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
  62. *
  63. * Then there are the unfortunate memory chips that spill the most
  64. * significant 1, 2, or 3 bits of address into the chip address byte.
  65. * This effectively makes one chip (logically) look like 2, 4, or
  66. * 8 chips. This is handled (awkwardly) by #defining
  67. * CFG_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
  68. * {addr} field (since .1 is the default, it doesn't actually have to
  69. * be specified). Examples: given a memory chip at I2C chip address
  70. * 0x50, the following would happen...
  71. * imd 50 0 10 display 16 bytes starting at 0x000
  72. * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
  73. * imd 50 100 10 display 16 bytes starting at 0x100
  74. * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
  75. * imd 50 210 10 display 16 bytes starting at 0x210
  76. * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
  77. * This is awfully ugly. It would be nice if someone would think up
  78. * a better way of handling this.
  79. *
  80. * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
  81. */
  82. #include <common.h>
  83. #include <command.h>
  84. #include <i2c.h>
  85. #include <asm/byteorder.h>
  86. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  87. /* Display values from last command.
  88. * Memory modify remembered values are different from display memory.
  89. */
  90. static uchar i2c_dp_last_chip;
  91. static uint i2c_dp_last_addr;
  92. static uint i2c_dp_last_alen;
  93. static uint i2c_dp_last_length = 0x10;
  94. static uchar i2c_mm_last_chip;
  95. static uint i2c_mm_last_addr;
  96. static uint i2c_mm_last_alen;
  97. /* If only one I2C bus is present, the list of devices to ignore when
  98. * the probe command is issued is represented by a 1D array of addresses.
  99. * When multiple buses are present, the list is an array of bus-address
  100. * pairs. The following macros take care of this */
  101. #if defined(CFG_I2C_NOPROBES)
  102. #if defined(CONFIG_I2C_MULTI_BUS)
  103. static struct
  104. {
  105. uchar bus;
  106. uchar addr;
  107. } i2c_no_probes[] = CFG_I2C_NOPROBES;
  108. #define GET_BUS_NUM i2c_get_bus_num()
  109. #define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
  110. #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
  111. #define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
  112. #else /* single bus */
  113. static uchar i2c_no_probes[] = CFG_I2C_NOPROBES;
  114. #define GET_BUS_NUM 0
  115. #define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
  116. #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
  117. #define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
  118. #endif /* CONFIG_MULTI_BUS */
  119. #define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
  120. #endif
  121. static int
  122. mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[]);
  123. extern int cmd_get_data_size(char* arg, int default_size);
  124. /*
  125. * Syntax:
  126. * imd {i2c_chip} {addr}{.0, .1, .2} {len}
  127. */
  128. #define DISP_LINE_LEN 16
  129. int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  130. {
  131. u_char chip;
  132. uint addr, alen, length;
  133. int j, nbytes, linebytes;
  134. /* We use the last specified parameters, unless new ones are
  135. * entered.
  136. */
  137. chip = i2c_dp_last_chip;
  138. addr = i2c_dp_last_addr;
  139. alen = i2c_dp_last_alen;
  140. length = i2c_dp_last_length;
  141. if (argc < 3) {
  142. printf ("Usage:\n%s\n", cmdtp->usage);
  143. return 1;
  144. }
  145. if ((flag & CMD_FLAG_REPEAT) == 0) {
  146. /*
  147. * New command specified.
  148. */
  149. alen = 1;
  150. /*
  151. * I2C chip address
  152. */
  153. chip = simple_strtoul(argv[1], NULL, 16);
  154. /*
  155. * I2C data address within the chip. This can be 1 or
  156. * 2 bytes long. Some day it might be 3 bytes long :-).
  157. */
  158. addr = simple_strtoul(argv[2], NULL, 16);
  159. alen = 1;
  160. for(j = 0; j < 8; j++) {
  161. if (argv[2][j] == '.') {
  162. alen = argv[2][j+1] - '0';
  163. if (alen > 4) {
  164. printf ("Usage:\n%s\n", cmdtp->usage);
  165. return 1;
  166. }
  167. break;
  168. } else if (argv[2][j] == '\0') {
  169. break;
  170. }
  171. }
  172. /*
  173. * If another parameter, it is the length to display.
  174. * Length is the number of objects, not number of bytes.
  175. */
  176. if (argc > 3)
  177. length = simple_strtoul(argv[3], NULL, 16);
  178. }
  179. /*
  180. * Print the lines.
  181. *
  182. * We buffer all read data, so we can make sure data is read only
  183. * once.
  184. */
  185. nbytes = length;
  186. do {
  187. unsigned char linebuf[DISP_LINE_LEN];
  188. unsigned char *cp;
  189. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  190. if(i2c_read(chip, addr, alen, linebuf, linebytes) != 0) {
  191. puts ("Error reading the chip.\n");
  192. } else {
  193. printf("%04x:", addr);
  194. cp = linebuf;
  195. for (j=0; j<linebytes; j++) {
  196. printf(" %02x", *cp++);
  197. addr++;
  198. }
  199. puts (" ");
  200. cp = linebuf;
  201. for (j=0; j<linebytes; j++) {
  202. if ((*cp < 0x20) || (*cp > 0x7e))
  203. puts (".");
  204. else
  205. printf("%c", *cp);
  206. cp++;
  207. }
  208. putc ('\n');
  209. }
  210. nbytes -= linebytes;
  211. } while (nbytes > 0);
  212. i2c_dp_last_chip = chip;
  213. i2c_dp_last_addr = addr;
  214. i2c_dp_last_alen = alen;
  215. i2c_dp_last_length = length;
  216. return 0;
  217. }
  218. int do_i2c_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  219. {
  220. return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
  221. }
  222. int do_i2c_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  223. {
  224. return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
  225. }
  226. /* Write (fill) memory
  227. *
  228. * Syntax:
  229. * imw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
  230. */
  231. int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  232. {
  233. uchar chip;
  234. ulong addr;
  235. uint alen;
  236. uchar byte;
  237. int count;
  238. int j;
  239. if ((argc < 4) || (argc > 5)) {
  240. printf ("Usage:\n%s\n", cmdtp->usage);
  241. return 1;
  242. }
  243. /*
  244. * Chip is always specified.
  245. */
  246. chip = simple_strtoul(argv[1], NULL, 16);
  247. /*
  248. * Address is always specified.
  249. */
  250. addr = simple_strtoul(argv[2], NULL, 16);
  251. alen = 1;
  252. for(j = 0; j < 8; j++) {
  253. if (argv[2][j] == '.') {
  254. alen = argv[2][j+1] - '0';
  255. if(alen > 4) {
  256. printf ("Usage:\n%s\n", cmdtp->usage);
  257. return 1;
  258. }
  259. break;
  260. } else if (argv[2][j] == '\0') {
  261. break;
  262. }
  263. }
  264. /*
  265. * Value to write is always specified.
  266. */
  267. byte = simple_strtoul(argv[3], NULL, 16);
  268. /*
  269. * Optional count
  270. */
  271. if(argc == 5) {
  272. count = simple_strtoul(argv[4], NULL, 16);
  273. } else {
  274. count = 1;
  275. }
  276. while (count-- > 0) {
  277. if(i2c_write(chip, addr++, alen, &byte, 1) != 0) {
  278. puts ("Error writing the chip.\n");
  279. }
  280. /*
  281. * Wait for the write to complete. The write can take
  282. * up to 10mSec (we allow a little more time).
  283. *
  284. * On some chips, while the write is in progress, the
  285. * chip doesn't respond. This apparently isn't a
  286. * universal feature so we don't take advantage of it.
  287. */
  288. /*
  289. * No write delay with FRAM devices.
  290. */
  291. #if !defined(CFG_I2C_FRAM)
  292. udelay(11000);
  293. #endif
  294. #if 0
  295. for(timeout = 0; timeout < 10; timeout++) {
  296. udelay(2000);
  297. if(i2c_probe(chip) == 0)
  298. break;
  299. }
  300. #endif
  301. }
  302. return (0);
  303. }
  304. /* Calculate a CRC on memory
  305. *
  306. * Syntax:
  307. * icrc32 {i2c_chip} {addr}{.0, .1, .2} {count}
  308. */
  309. int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  310. {
  311. uchar chip;
  312. ulong addr;
  313. uint alen;
  314. int count;
  315. uchar byte;
  316. ulong crc;
  317. ulong err;
  318. int j;
  319. if (argc < 4) {
  320. printf ("Usage:\n%s\n", cmdtp->usage);
  321. return 1;
  322. }
  323. /*
  324. * Chip is always specified.
  325. */
  326. chip = simple_strtoul(argv[1], NULL, 16);
  327. /*
  328. * Address is always specified.
  329. */
  330. addr = simple_strtoul(argv[2], NULL, 16);
  331. alen = 1;
  332. for(j = 0; j < 8; j++) {
  333. if (argv[2][j] == '.') {
  334. alen = argv[2][j+1] - '0';
  335. if(alen > 4) {
  336. printf ("Usage:\n%s\n", cmdtp->usage);
  337. return 1;
  338. }
  339. break;
  340. } else if (argv[2][j] == '\0') {
  341. break;
  342. }
  343. }
  344. /*
  345. * Count is always specified
  346. */
  347. count = simple_strtoul(argv[3], NULL, 16);
  348. printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
  349. /*
  350. * CRC a byte at a time. This is going to be slooow, but hey, the
  351. * memories are small and slow too so hopefully nobody notices.
  352. */
  353. crc = 0;
  354. err = 0;
  355. while(count-- > 0) {
  356. if(i2c_read(chip, addr, alen, &byte, 1) != 0) {
  357. err++;
  358. }
  359. crc = crc32 (crc, &byte, 1);
  360. addr++;
  361. }
  362. if(err > 0)
  363. {
  364. puts ("Error reading the chip,\n");
  365. } else {
  366. printf ("%08lx\n", crc);
  367. }
  368. return 0;
  369. }
  370. /* Modify memory.
  371. *
  372. * Syntax:
  373. * imm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  374. * inm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  375. */
  376. static int
  377. mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  378. {
  379. uchar chip;
  380. ulong addr;
  381. uint alen;
  382. ulong data;
  383. int size = 1;
  384. int nbytes;
  385. int j;
  386. extern char console_buffer[];
  387. if (argc != 3) {
  388. printf ("Usage:\n%s\n", cmdtp->usage);
  389. return 1;
  390. }
  391. #ifdef CONFIG_BOOT_RETRY_TIME
  392. reset_cmd_timeout(); /* got a good command to get here */
  393. #endif
  394. /*
  395. * We use the last specified parameters, unless new ones are
  396. * entered.
  397. */
  398. chip = i2c_mm_last_chip;
  399. addr = i2c_mm_last_addr;
  400. alen = i2c_mm_last_alen;
  401. if ((flag & CMD_FLAG_REPEAT) == 0) {
  402. /*
  403. * New command specified. Check for a size specification.
  404. * Defaults to byte if no or incorrect specification.
  405. */
  406. size = cmd_get_data_size(argv[0], 1);
  407. /*
  408. * Chip is always specified.
  409. */
  410. chip = simple_strtoul(argv[1], NULL, 16);
  411. /*
  412. * Address is always specified.
  413. */
  414. addr = simple_strtoul(argv[2], NULL, 16);
  415. alen = 1;
  416. for(j = 0; j < 8; j++) {
  417. if (argv[2][j] == '.') {
  418. alen = argv[2][j+1] - '0';
  419. if(alen > 4) {
  420. printf ("Usage:\n%s\n", cmdtp->usage);
  421. return 1;
  422. }
  423. break;
  424. } else if (argv[2][j] == '\0') {
  425. break;
  426. }
  427. }
  428. }
  429. /*
  430. * Print the address, followed by value. Then accept input for
  431. * the next value. A non-converted value exits.
  432. */
  433. do {
  434. printf("%08lx:", addr);
  435. if(i2c_read(chip, addr, alen, (uchar *)&data, size) != 0) {
  436. puts ("\nError reading the chip,\n");
  437. } else {
  438. data = cpu_to_be32(data);
  439. if(size == 1) {
  440. printf(" %02lx", (data >> 24) & 0x000000FF);
  441. } else if(size == 2) {
  442. printf(" %04lx", (data >> 16) & 0x0000FFFF);
  443. } else {
  444. printf(" %08lx", data);
  445. }
  446. }
  447. nbytes = readline (" ? ");
  448. if (nbytes == 0) {
  449. /*
  450. * <CR> pressed as only input, don't modify current
  451. * location and move to next.
  452. */
  453. if (incrflag)
  454. addr += size;
  455. nbytes = size;
  456. #ifdef CONFIG_BOOT_RETRY_TIME
  457. reset_cmd_timeout(); /* good enough to not time out */
  458. #endif
  459. }
  460. #ifdef CONFIG_BOOT_RETRY_TIME
  461. else if (nbytes == -2) {
  462. break; /* timed out, exit the command */
  463. }
  464. #endif
  465. else {
  466. char *endp;
  467. data = simple_strtoul(console_buffer, &endp, 16);
  468. if(size == 1) {
  469. data = data << 24;
  470. } else if(size == 2) {
  471. data = data << 16;
  472. }
  473. data = be32_to_cpu(data);
  474. nbytes = endp - console_buffer;
  475. if (nbytes) {
  476. #ifdef CONFIG_BOOT_RETRY_TIME
  477. /*
  478. * good enough to not time out
  479. */
  480. reset_cmd_timeout();
  481. #endif
  482. if(i2c_write(chip, addr, alen, (uchar *)&data, size) != 0) {
  483. puts ("Error writing the chip.\n");
  484. }
  485. #ifdef CFG_EEPROM_PAGE_WRITE_DELAY_MS
  486. udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  487. #endif
  488. if (incrflag)
  489. addr += size;
  490. }
  491. }
  492. } while (nbytes);
  493. chip = i2c_mm_last_chip;
  494. addr = i2c_mm_last_addr;
  495. alen = i2c_mm_last_alen;
  496. return 0;
  497. }
  498. /*
  499. * Syntax:
  500. * iprobe {addr}{.0, .1, .2}
  501. */
  502. int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  503. {
  504. int j;
  505. #if defined(CFG_I2C_NOPROBES)
  506. int k, skip;
  507. uchar bus = GET_BUS_NUM;
  508. #endif /* NOPROBES */
  509. puts ("Valid chip addresses:");
  510. for(j = 0; j < 128; j++) {
  511. #if defined(CFG_I2C_NOPROBES)
  512. skip = 0;
  513. for(k=0; k < NUM_ELEMENTS_NOPROBE; k++)
  514. {
  515. if(COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k))
  516. {
  517. skip = 1;
  518. break;
  519. }
  520. }
  521. if (skip)
  522. continue;
  523. #endif
  524. if(i2c_probe(j) == 0) {
  525. printf(" %02X", j);
  526. }
  527. }
  528. putc ('\n');
  529. #if defined(CFG_I2C_NOPROBES)
  530. puts ("Excluded chip addresses:");
  531. for(k=0; k < NUM_ELEMENTS_NOPROBE; k++)
  532. {
  533. if(COMPARE_BUS(bus,k))
  534. printf(" %02X", NO_PROBE_ADDR(k));
  535. }
  536. putc ('\n');
  537. #endif
  538. return 0;
  539. }
  540. /*
  541. * Syntax:
  542. * iloop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
  543. * {length} - Number of bytes to read
  544. * {delay} - A DECIMAL number and defaults to 1000 uSec
  545. */
  546. int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  547. {
  548. u_char chip;
  549. ulong alen;
  550. uint addr;
  551. uint length;
  552. u_char bytes[16];
  553. int delay;
  554. int j;
  555. if (argc < 3) {
  556. printf ("Usage:\n%s\n", cmdtp->usage);
  557. return 1;
  558. }
  559. /*
  560. * Chip is always specified.
  561. */
  562. chip = simple_strtoul(argv[1], NULL, 16);
  563. /*
  564. * Address is always specified.
  565. */
  566. addr = simple_strtoul(argv[2], NULL, 16);
  567. alen = 1;
  568. for(j = 0; j < 8; j++) {
  569. if (argv[2][j] == '.') {
  570. alen = argv[2][j+1] - '0';
  571. if (alen > 4) {
  572. printf ("Usage:\n%s\n", cmdtp->usage);
  573. return 1;
  574. }
  575. break;
  576. } else if (argv[2][j] == '\0') {
  577. break;
  578. }
  579. }
  580. /*
  581. * Length is the number of objects, not number of bytes.
  582. */
  583. length = 1;
  584. length = simple_strtoul(argv[3], NULL, 16);
  585. if(length > sizeof(bytes)) {
  586. length = sizeof(bytes);
  587. }
  588. /*
  589. * The delay time (uSec) is optional.
  590. */
  591. delay = 1000;
  592. if (argc > 3) {
  593. delay = simple_strtoul(argv[4], NULL, 10);
  594. }
  595. /*
  596. * Run the loop...
  597. */
  598. while(1) {
  599. if(i2c_read(chip, addr, alen, bytes, length) != 0) {
  600. puts ("Error reading the chip.\n");
  601. }
  602. udelay(delay);
  603. }
  604. /* NOTREACHED */
  605. return 0;
  606. }
  607. /*
  608. * The SDRAM command is separately configured because many
  609. * (most?) embedded boards don't use SDRAM DIMMs.
  610. */
  611. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  612. /*
  613. * Syntax:
  614. * sdram {i2c_chip}
  615. */
  616. int do_sdram ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  617. {
  618. u_char chip;
  619. u_char data[128];
  620. u_char cksum;
  621. int j;
  622. if (argc < 2) {
  623. printf ("Usage:\n%s\n", cmdtp->usage);
  624. return 1;
  625. }
  626. /*
  627. * Chip is always specified.
  628. */
  629. chip = simple_strtoul(argv[1], NULL, 16);
  630. if(i2c_read(chip, 0, 1, data, sizeof(data)) != 0) {
  631. puts ("No SDRAM Serial Presence Detect found.\n");
  632. return 1;
  633. }
  634. cksum = 0;
  635. for (j = 0; j < 63; j++) {
  636. cksum += data[j];
  637. }
  638. if(cksum != data[63]) {
  639. printf ("WARNING: Configuration data checksum failure:\n"
  640. " is 0x%02x, calculated 0x%02x\n",
  641. data[63], cksum);
  642. }
  643. printf("SPD data revision %d.%d\n",
  644. (data[62] >> 4) & 0x0F, data[62] & 0x0F);
  645. printf("Bytes used 0x%02X\n", data[0]);
  646. printf("Serial memory size 0x%02X\n", 1 << data[1]);
  647. puts ("Memory type ");
  648. switch(data[2]) {
  649. case 2: puts ("EDO\n"); break;
  650. case 4: puts ("SDRAM\n"); break;
  651. default: puts ("unknown\n"); break;
  652. }
  653. puts ("Row address bits ");
  654. if((data[3] & 0x00F0) == 0) {
  655. printf("%d\n", data[3] & 0x0F);
  656. } else {
  657. printf("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
  658. }
  659. puts ("Column address bits ");
  660. if((data[4] & 0x00F0) == 0) {
  661. printf("%d\n", data[4] & 0x0F);
  662. } else {
  663. printf("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
  664. }
  665. printf("Module rows %d\n", data[5]);
  666. printf("Module data width %d bits\n", (data[7] << 8) | data[6]);
  667. puts ("Interface signal levels ");
  668. switch(data[8]) {
  669. case 0: puts ("5.0v/TTL\n"); break;
  670. case 1: puts ("LVTTL\n"); break;
  671. case 2: puts ("HSTL 1.5\n"); break;
  672. case 3: puts ("SSTL 3.3\n"); break;
  673. case 4: puts ("SSTL 2.5\n"); break;
  674. default: puts ("unknown\n"); break;
  675. }
  676. printf("SDRAM cycle time %d.%d nS\n",
  677. (data[9] >> 4) & 0x0F, data[9] & 0x0F);
  678. printf("SDRAM access time %d.%d nS\n",
  679. (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  680. puts ("EDC configuration ");
  681. switch(data[11]) {
  682. case 0: puts ("None\n"); break;
  683. case 1: puts ("Parity\n"); break;
  684. case 2: puts ("ECC\n"); break;
  685. default: puts ("unknown\n"); break;
  686. }
  687. if((data[12] & 0x80) == 0) {
  688. puts ("No self refresh, rate ");
  689. } else {
  690. puts ("Self refresh, rate ");
  691. }
  692. switch(data[12] & 0x7F) {
  693. case 0: puts ("15.625uS\n"); break;
  694. case 1: puts ("3.9uS\n"); break;
  695. case 2: puts ("7.8uS\n"); break;
  696. case 3: puts ("31.3uS\n"); break;
  697. case 4: puts ("62.5uS\n"); break;
  698. case 5: puts ("125uS\n"); break;
  699. default: puts ("unknown\n"); break;
  700. }
  701. printf("SDRAM width (primary) %d\n", data[13] & 0x7F);
  702. if((data[13] & 0x80) != 0) {
  703. printf(" (second bank) %d\n",
  704. 2 * (data[13] & 0x7F));
  705. }
  706. if(data[14] != 0) {
  707. printf("EDC width %d\n",
  708. data[14] & 0x7F);
  709. if((data[14] & 0x80) != 0) {
  710. printf(" (second bank) %d\n",
  711. 2 * (data[14] & 0x7F));
  712. }
  713. }
  714. printf("Min clock delay, back-to-back random column addresses %d\n",
  715. data[15]);
  716. puts ("Burst length(s) ");
  717. if (data[16] & 0x80) puts (" Page");
  718. if (data[16] & 0x08) puts (" 8");
  719. if (data[16] & 0x04) puts (" 4");
  720. if (data[16] & 0x02) puts (" 2");
  721. if (data[16] & 0x01) puts (" 1");
  722. putc ('\n');
  723. printf("Number of banks %d\n", data[17]);
  724. puts ("CAS latency(s) ");
  725. if (data[18] & 0x80) puts (" TBD");
  726. if (data[18] & 0x40) puts (" 7");
  727. if (data[18] & 0x20) puts (" 6");
  728. if (data[18] & 0x10) puts (" 5");
  729. if (data[18] & 0x08) puts (" 4");
  730. if (data[18] & 0x04) puts (" 3");
  731. if (data[18] & 0x02) puts (" 2");
  732. if (data[18] & 0x01) puts (" 1");
  733. putc ('\n');
  734. puts ("CS latency(s) ");
  735. if (data[19] & 0x80) puts (" TBD");
  736. if (data[19] & 0x40) puts (" 6");
  737. if (data[19] & 0x20) puts (" 5");
  738. if (data[19] & 0x10) puts (" 4");
  739. if (data[19] & 0x08) puts (" 3");
  740. if (data[19] & 0x04) puts (" 2");
  741. if (data[19] & 0x02) puts (" 1");
  742. if (data[19] & 0x01) puts (" 0");
  743. putc ('\n');
  744. puts ("WE latency(s) ");
  745. if (data[20] & 0x80) puts (" TBD");
  746. if (data[20] & 0x40) puts (" 6");
  747. if (data[20] & 0x20) puts (" 5");
  748. if (data[20] & 0x10) puts (" 4");
  749. if (data[20] & 0x08) puts (" 3");
  750. if (data[20] & 0x04) puts (" 2");
  751. if (data[20] & 0x02) puts (" 1");
  752. if (data[20] & 0x01) puts (" 0");
  753. putc ('\n');
  754. puts ("Module attributes:\n");
  755. if (!data[21]) puts (" (none)\n");
  756. if (data[21] & 0x80) puts (" TBD (bit 7)\n");
  757. if (data[21] & 0x40) puts (" Redundant row address\n");
  758. if (data[21] & 0x20) puts (" Differential clock input\n");
  759. if (data[21] & 0x10) puts (" Registerd DQMB inputs\n");
  760. if (data[21] & 0x08) puts (" Buffered DQMB inputs\n");
  761. if (data[21] & 0x04) puts (" On-card PLL\n");
  762. if (data[21] & 0x02) puts (" Registered address/control lines\n");
  763. if (data[21] & 0x01) puts (" Buffered address/control lines\n");
  764. puts ("Device attributes:\n");
  765. if (data[22] & 0x80) puts (" TBD (bit 7)\n");
  766. if (data[22] & 0x40) puts (" TBD (bit 6)\n");
  767. if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
  768. else puts (" Upper Vcc tolerance 10%\n");
  769. if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
  770. else puts (" Lower Vcc tolerance 10%\n");
  771. if (data[22] & 0x08) puts (" Supports write1/read burst\n");
  772. if (data[22] & 0x04) puts (" Supports precharge all\n");
  773. if (data[22] & 0x02) puts (" Supports auto precharge\n");
  774. if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
  775. printf("SDRAM cycle time (2nd highest CAS latency) %d.%d nS\n",
  776. (data[23] >> 4) & 0x0F, data[23] & 0x0F);
  777. printf("SDRAM access from clock (2nd highest CAS latency) %d.%d nS\n",
  778. (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  779. printf("SDRAM cycle time (3rd highest CAS latency) %d.%d nS\n",
  780. (data[25] >> 4) & 0x0F, data[25] & 0x0F);
  781. printf("SDRAM access from clock (3rd highest CAS latency) %d.%d nS\n",
  782. (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  783. printf("Minimum row precharge %d nS\n", data[27]);
  784. printf("Row active to row active min %d nS\n", data[28]);
  785. printf("RAS to CAS delay min %d nS\n", data[29]);
  786. printf("Minimum RAS pulse width %d nS\n", data[30]);
  787. puts ("Density of each row ");
  788. if (data[31] & 0x80) puts (" 512");
  789. if (data[31] & 0x40) puts (" 256");
  790. if (data[31] & 0x20) puts (" 128");
  791. if (data[31] & 0x10) puts (" 64");
  792. if (data[31] & 0x08) puts (" 32");
  793. if (data[31] & 0x04) puts (" 16");
  794. if (data[31] & 0x02) puts (" 8");
  795. if (data[31] & 0x01) puts (" 4");
  796. puts ("MByte\n");
  797. printf("Command and Address setup %c%d.%d nS\n",
  798. (data[32] & 0x80) ? '-' : '+',
  799. (data[32] >> 4) & 0x07, data[32] & 0x0F);
  800. printf("Command and Address hold %c%d.%d nS\n",
  801. (data[33] & 0x80) ? '-' : '+',
  802. (data[33] >> 4) & 0x07, data[33] & 0x0F);
  803. printf("Data signal input setup %c%d.%d nS\n",
  804. (data[34] & 0x80) ? '-' : '+',
  805. (data[34] >> 4) & 0x07, data[34] & 0x0F);
  806. printf("Data signal input hold %c%d.%d nS\n",
  807. (data[35] & 0x80) ? '-' : '+',
  808. (data[35] >> 4) & 0x07, data[35] & 0x0F);
  809. puts ("Manufacturer's JEDEC ID ");
  810. for(j = 64; j <= 71; j++)
  811. printf("%02X ", data[j]);
  812. putc ('\n');
  813. printf("Manufacturing Location %02X\n", data[72]);
  814. puts ("Manufacturer's Part Number ");
  815. for(j = 73; j <= 90; j++)
  816. printf("%02X ", data[j]);
  817. putc ('\n');
  818. printf("Revision Code %02X %02X\n", data[91], data[92]);
  819. printf("Manufacturing Date %02X %02X\n", data[93], data[94]);
  820. puts ("Assembly Serial Number ");
  821. for(j = 95; j <= 98; j++)
  822. printf("%02X ", data[j]);
  823. putc ('\n');
  824. printf("Speed rating PC%d\n",
  825. data[126] == 0x66 ? 66 : data[126]);
  826. return 0;
  827. }
  828. #endif /* CFG_CMD_SDRAM */
  829. #if defined(CONFIG_I2C_CMD_TREE)
  830. #if defined(CONFIG_I2C_MULTI_BUS)
  831. int do_i2c_bus_num(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  832. {
  833. int bus_idx, ret=0;
  834. if (argc == 1) /* querying current setting */
  835. {
  836. printf("Current bus is %d\n", i2c_get_bus_num());
  837. }
  838. else
  839. {
  840. bus_idx = simple_strtoul(argv[1], NULL, 10);
  841. printf("Setting bus to %d\n", bus_idx);
  842. ret = i2c_set_bus_num(bus_idx);
  843. if(ret)
  844. {
  845. printf("Failure changing bus number (%d)\n", ret);
  846. }
  847. }
  848. return ret;
  849. }
  850. #endif /* CONFIG_I2C_MULTI_BUS */
  851. int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  852. {
  853. int speed, ret=0;
  854. if (argc == 1) /* querying current speed */
  855. {
  856. printf("Current bus speed=%d\n", i2c_get_bus_speed());
  857. }
  858. else
  859. {
  860. speed = simple_strtoul(argv[1], NULL, 10);
  861. printf("Setting bus speed to %d Hz\n", speed);
  862. ret = i2c_set_bus_speed(speed);
  863. if(ret)
  864. {
  865. printf("Failure changing bus speed (%d)\n", ret);
  866. }
  867. }
  868. return ret;
  869. }
  870. int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  871. {
  872. #if defined(CONFIG_I2C_MULTI_BUS)
  873. if(!strncmp(argv[1], "de", 2))
  874. {
  875. return do_i2c_bus_num(cmdtp, flag, --argc, ++argv);
  876. }
  877. #endif /* CONFIG_I2C_MULTI_BUS */
  878. if(!strncmp(argv[1], "sp", 2))
  879. {
  880. return do_i2c_bus_speed(cmdtp, flag, --argc, ++argv);
  881. }
  882. if(!strncmp(argv[1], "md", 2))
  883. {
  884. return do_i2c_md(cmdtp, flag, --argc, ++argv);
  885. }
  886. if(!strncmp(argv[1], "mm", 2))
  887. {
  888. return do_i2c_mm(cmdtp, flag, --argc, ++argv);
  889. }
  890. if(!strncmp(argv[1], "mw", 2))
  891. {
  892. return do_i2c_mw(cmdtp, flag, --argc, ++argv);
  893. }
  894. if(!strncmp(argv[1], "nm", 2))
  895. {
  896. return do_i2c_nm(cmdtp, flag, --argc, ++argv);
  897. }
  898. if(!strncmp(argv[1], "cr", 2))
  899. {
  900. return do_i2c_crc(cmdtp, flag, --argc, ++argv);
  901. }
  902. if(!strncmp(argv[1], "pr", 2))
  903. {
  904. return do_i2c_probe(cmdtp, flag, --argc, ++argv);
  905. }
  906. if(!strncmp(argv[1], "lo", 2))
  907. {
  908. return do_i2c_loop(cmdtp, flag, --argc, ++argv);
  909. }
  910. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  911. if(!strncmp(argv[1], "sd", 2))
  912. {
  913. return do_sdram(cmdtp, flag, --argc, ++argv);
  914. }
  915. #endif /* CFG_CMD_SDRAM */
  916. else
  917. {
  918. printf ("Usage:\n%s\n", cmdtp->usage);
  919. }
  920. return 0;
  921. }
  922. #endif /* CONFIG_I2C_CMD_TREE */
  923. /***************************************************/
  924. U_BOOT_CMD(
  925. imd, 4, 1, do_i2c_md, \
  926. "imd - i2c memory display\n", \
  927. "chip address[.0, .1, .2] [# of objects]\n - i2c memory display\n" \
  928. );
  929. U_BOOT_CMD(
  930. imm, 3, 1, do_i2c_mm,
  931. "imm - i2c memory modify (auto-incrementing)\n",
  932. "chip address[.0, .1, .2]\n"
  933. " - memory modify, auto increment address\n"
  934. );
  935. U_BOOT_CMD(
  936. inm, 3, 1, do_i2c_nm,
  937. "inm - memory modify (constant address)\n",
  938. "chip address[.0, .1, .2]\n - memory modify, read and keep address\n"
  939. );
  940. U_BOOT_CMD(
  941. imw, 5, 1, do_i2c_mw,
  942. "imw - memory write (fill)\n",
  943. "chip address[.0, .1, .2] value [count]\n - memory write (fill)\n"
  944. );
  945. U_BOOT_CMD(
  946. icrc32, 5, 1, do_i2c_crc,
  947. "icrc32 - checksum calculation\n",
  948. "chip address[.0, .1, .2] count\n - compute CRC32 checksum\n"
  949. );
  950. U_BOOT_CMD(
  951. iprobe, 1, 1, do_i2c_probe,
  952. "iprobe - probe to discover valid I2C chip addresses\n",
  953. "\n -discover valid I2C chip addresses\n"
  954. );
  955. /*
  956. * Require full name for "iloop" because it is an infinite loop!
  957. */
  958. U_BOOT_CMD(
  959. iloop, 5, 1, do_i2c_loop,
  960. "iloop - infinite loop on address range\n",
  961. "chip address[.0, .1, .2] [# of objects]\n"
  962. " - loop, reading a set of addresses\n"
  963. );
  964. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  965. U_BOOT_CMD(
  966. isdram, 2, 1, do_sdram,
  967. "isdram - print SDRAM configuration information\n",
  968. "chip\n - print SDRAM configuration information\n"
  969. " (valid chip values 50..57)\n"
  970. );
  971. #endif
  972. #if defined(CONFIG_I2C_CMD_TREE)
  973. U_BOOT_CMD(
  974. i2c, 6, 1, do_i2c,
  975. "i2c - I2C sub-system\n",
  976. #if defined(CONFIG_I2C_MULTI_BUS)
  977. "dev [dev] - show or set current I2C bus\n"
  978. #endif /* CONFIG_I2C_MULTI_BUS */
  979. "i2c speed [speed] - show or set I2C bus speed\n"
  980. "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
  981. "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
  982. "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
  983. "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
  984. "i2c crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
  985. "i2c probe - show devices on the I2C bus\n"
  986. "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
  987. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  988. "i2c sdram chip - print SDRAM configuration information\n"
  989. #endif /* CFG_CMD_SDRAM */
  990. );
  991. #endif /* CONFIG_I2C_CMD_TREE */
  992. #endif /* CFG_CMD_I2C */