cmd_i2c.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. * If another parameter, it is the length to display.
  173. * Length is the number of objects, not number of bytes.
  174. */
  175. if (argc > 3)
  176. length = simple_strtoul(argv[3], NULL, 16);
  177. }
  178. /*
  179. * Print the lines.
  180. *
  181. * We buffer all read data, so we can make sure data is read only
  182. * once.
  183. */
  184. nbytes = length;
  185. do {
  186. unsigned char linebuf[DISP_LINE_LEN];
  187. unsigned char *cp;
  188. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  189. if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
  190. puts ("Error reading the chip.\n");
  191. else {
  192. printf("%04x:", addr);
  193. cp = linebuf;
  194. for (j=0; j<linebytes; j++) {
  195. printf(" %02x", *cp++);
  196. addr++;
  197. }
  198. puts (" ");
  199. cp = linebuf;
  200. for (j=0; j<linebytes; j++) {
  201. if ((*cp < 0x20) || (*cp > 0x7e))
  202. puts (".");
  203. else
  204. printf("%c", *cp);
  205. cp++;
  206. }
  207. putc ('\n');
  208. }
  209. nbytes -= linebytes;
  210. } while (nbytes > 0);
  211. i2c_dp_last_chip = chip;
  212. i2c_dp_last_addr = addr;
  213. i2c_dp_last_alen = alen;
  214. i2c_dp_last_length = length;
  215. return 0;
  216. }
  217. int do_i2c_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  218. {
  219. return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
  220. }
  221. int do_i2c_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  222. {
  223. return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
  224. }
  225. /* Write (fill) memory
  226. *
  227. * Syntax:
  228. * imw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
  229. */
  230. int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  231. {
  232. uchar chip;
  233. ulong addr;
  234. uint alen;
  235. uchar byte;
  236. int count;
  237. int j;
  238. if ((argc < 4) || (argc > 5)) {
  239. printf ("Usage:\n%s\n", cmdtp->usage);
  240. return 1;
  241. }
  242. /*
  243. * Chip is always specified.
  244. */
  245. chip = simple_strtoul(argv[1], NULL, 16);
  246. /*
  247. * Address is always specified.
  248. */
  249. addr = simple_strtoul(argv[2], NULL, 16);
  250. alen = 1;
  251. for (j = 0; j < 8; j++) {
  252. if (argv[2][j] == '.') {
  253. alen = argv[2][j+1] - '0';
  254. if (alen > 4) {
  255. printf ("Usage:\n%s\n", cmdtp->usage);
  256. return 1;
  257. }
  258. break;
  259. } else if (argv[2][j] == '\0')
  260. break;
  261. }
  262. /*
  263. * Value to write is always specified.
  264. */
  265. byte = simple_strtoul(argv[3], NULL, 16);
  266. /*
  267. * Optional count
  268. */
  269. if (argc == 5)
  270. count = simple_strtoul(argv[4], NULL, 16);
  271. else
  272. count = 1;
  273. while (count-- > 0) {
  274. if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
  275. puts ("Error writing the chip.\n");
  276. /*
  277. * Wait for the write to complete. The write can take
  278. * up to 10mSec (we allow a little more time).
  279. *
  280. * On some chips, while the write is in progress, the
  281. * chip doesn't respond. This apparently isn't a
  282. * universal feature so we don't take advantage of it.
  283. */
  284. /*
  285. * No write delay with FRAM devices.
  286. */
  287. #if !defined(CFG_I2C_FRAM)
  288. udelay(11000);
  289. #endif
  290. #if 0
  291. for (timeout = 0; timeout < 10; timeout++) {
  292. udelay(2000);
  293. if (i2c_probe(chip) == 0)
  294. break;
  295. }
  296. #endif
  297. }
  298. return (0);
  299. }
  300. /* Calculate a CRC on memory
  301. *
  302. * Syntax:
  303. * icrc32 {i2c_chip} {addr}{.0, .1, .2} {count}
  304. */
  305. int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  306. {
  307. uchar chip;
  308. ulong addr;
  309. uint alen;
  310. int count;
  311. uchar byte;
  312. ulong crc;
  313. ulong err;
  314. int j;
  315. if (argc < 4) {
  316. printf ("Usage:\n%s\n", cmdtp->usage);
  317. return 1;
  318. }
  319. /*
  320. * Chip is always specified.
  321. */
  322. chip = simple_strtoul(argv[1], NULL, 16);
  323. /*
  324. * Address is always specified.
  325. */
  326. addr = simple_strtoul(argv[2], NULL, 16);
  327. alen = 1;
  328. for (j = 0; j < 8; j++) {
  329. if (argv[2][j] == '.') {
  330. alen = argv[2][j+1] - '0';
  331. if (alen > 4) {
  332. printf ("Usage:\n%s\n", cmdtp->usage);
  333. return 1;
  334. }
  335. break;
  336. } else if (argv[2][j] == '\0')
  337. break;
  338. }
  339. /*
  340. * Count is always specified
  341. */
  342. count = simple_strtoul(argv[3], NULL, 16);
  343. printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
  344. /*
  345. * CRC a byte at a time. This is going to be slooow, but hey, the
  346. * memories are small and slow too so hopefully nobody notices.
  347. */
  348. crc = 0;
  349. err = 0;
  350. while (count-- > 0) {
  351. if (i2c_read(chip, addr, alen, &byte, 1) != 0)
  352. err++;
  353. crc = crc32 (crc, &byte, 1);
  354. addr++;
  355. }
  356. if (err > 0)
  357. puts ("Error reading the chip,\n");
  358. else
  359. printf ("%08lx\n", crc);
  360. return 0;
  361. }
  362. /* Modify memory.
  363. *
  364. * Syntax:
  365. * imm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  366. * inm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  367. */
  368. static int
  369. mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  370. {
  371. uchar chip;
  372. ulong addr;
  373. uint alen;
  374. ulong data;
  375. int size = 1;
  376. int nbytes;
  377. int j;
  378. extern char console_buffer[];
  379. if (argc != 3) {
  380. printf ("Usage:\n%s\n", cmdtp->usage);
  381. return 1;
  382. }
  383. #ifdef CONFIG_BOOT_RETRY_TIME
  384. reset_cmd_timeout(); /* got a good command to get here */
  385. #endif
  386. /*
  387. * We use the last specified parameters, unless new ones are
  388. * entered.
  389. */
  390. chip = i2c_mm_last_chip;
  391. addr = i2c_mm_last_addr;
  392. alen = i2c_mm_last_alen;
  393. if ((flag & CMD_FLAG_REPEAT) == 0) {
  394. /*
  395. * New command specified. Check for a size specification.
  396. * Defaults to byte if no or incorrect specification.
  397. */
  398. size = cmd_get_data_size(argv[0], 1);
  399. /*
  400. * Chip is always specified.
  401. */
  402. chip = simple_strtoul(argv[1], NULL, 16);
  403. /*
  404. * Address is always specified.
  405. */
  406. addr = simple_strtoul(argv[2], NULL, 16);
  407. alen = 1;
  408. for (j = 0; j < 8; j++) {
  409. if (argv[2][j] == '.') {
  410. alen = argv[2][j+1] - '0';
  411. if (alen > 4) {
  412. printf ("Usage:\n%s\n", cmdtp->usage);
  413. return 1;
  414. }
  415. break;
  416. } else if (argv[2][j] == '\0')
  417. break;
  418. }
  419. }
  420. /*
  421. * Print the address, followed by value. Then accept input for
  422. * the next value. A non-converted value exits.
  423. */
  424. do {
  425. printf("%08lx:", addr);
  426. if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
  427. puts ("\nError reading the chip,\n");
  428. else {
  429. data = cpu_to_be32(data);
  430. if (size == 1)
  431. printf(" %02lx", (data >> 24) & 0x000000FF);
  432. else if (size == 2)
  433. printf(" %04lx", (data >> 16) & 0x0000FFFF);
  434. else
  435. printf(" %08lx", data);
  436. }
  437. nbytes = readline (" ? ");
  438. if (nbytes == 0) {
  439. /*
  440. * <CR> pressed as only input, don't modify current
  441. * location and move to next.
  442. */
  443. if (incrflag)
  444. addr += size;
  445. nbytes = size;
  446. #ifdef CONFIG_BOOT_RETRY_TIME
  447. reset_cmd_timeout(); /* good enough to not time out */
  448. #endif
  449. }
  450. #ifdef CONFIG_BOOT_RETRY_TIME
  451. else if (nbytes == -2)
  452. break; /* timed out, exit the command */
  453. #endif
  454. else {
  455. char *endp;
  456. data = simple_strtoul(console_buffer, &endp, 16);
  457. if (size == 1)
  458. data = data << 24;
  459. else if (size == 2)
  460. data = data << 16;
  461. data = be32_to_cpu(data);
  462. nbytes = endp - console_buffer;
  463. if (nbytes) {
  464. #ifdef CONFIG_BOOT_RETRY_TIME
  465. /*
  466. * good enough to not time out
  467. */
  468. reset_cmd_timeout();
  469. #endif
  470. if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
  471. puts ("Error writing the chip.\n");
  472. #ifdef CFG_EEPROM_PAGE_WRITE_DELAY_MS
  473. udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  474. #endif
  475. if (incrflag)
  476. addr += size;
  477. }
  478. }
  479. } while (nbytes);
  480. chip = i2c_mm_last_chip;
  481. addr = i2c_mm_last_addr;
  482. alen = i2c_mm_last_alen;
  483. return 0;
  484. }
  485. /*
  486. * Syntax:
  487. * iprobe {addr}{.0, .1, .2}
  488. */
  489. int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  490. {
  491. int j;
  492. #if defined(CFG_I2C_NOPROBES)
  493. int k, skip;
  494. uchar bus = GET_BUS_NUM;
  495. #endif /* NOPROBES */
  496. puts ("Valid chip addresses:");
  497. for (j = 0; j < 128; j++) {
  498. #if defined(CFG_I2C_NOPROBES)
  499. skip = 0;
  500. for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
  501. if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
  502. skip = 1;
  503. break;
  504. }
  505. }
  506. if (skip)
  507. continue;
  508. #endif
  509. if (i2c_probe(j) == 0)
  510. printf(" %02X", j);
  511. }
  512. putc ('\n');
  513. #if defined(CFG_I2C_NOPROBES)
  514. puts ("Excluded chip addresses:");
  515. for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
  516. if (COMPARE_BUS(bus,k))
  517. printf(" %02X", NO_PROBE_ADDR(k));
  518. }
  519. putc ('\n');
  520. #endif
  521. return 0;
  522. }
  523. /*
  524. * Syntax:
  525. * iloop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
  526. * {length} - Number of bytes to read
  527. * {delay} - A DECIMAL number and defaults to 1000 uSec
  528. */
  529. int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  530. {
  531. u_char chip;
  532. ulong alen;
  533. uint addr;
  534. uint length;
  535. u_char bytes[16];
  536. int delay;
  537. int j;
  538. if (argc < 3) {
  539. printf ("Usage:\n%s\n", cmdtp->usage);
  540. return 1;
  541. }
  542. /*
  543. * Chip is always specified.
  544. */
  545. chip = simple_strtoul(argv[1], NULL, 16);
  546. /*
  547. * Address is always specified.
  548. */
  549. addr = simple_strtoul(argv[2], NULL, 16);
  550. alen = 1;
  551. for (j = 0; j < 8; j++) {
  552. if (argv[2][j] == '.') {
  553. alen = argv[2][j+1] - '0';
  554. if (alen > 4) {
  555. printf ("Usage:\n%s\n", cmdtp->usage);
  556. return 1;
  557. }
  558. break;
  559. } else if (argv[2][j] == '\0')
  560. break;
  561. }
  562. /*
  563. * Length is the number of objects, not number of bytes.
  564. */
  565. length = 1;
  566. length = simple_strtoul(argv[3], NULL, 16);
  567. if (length > sizeof(bytes))
  568. length = sizeof(bytes);
  569. /*
  570. * The delay time (uSec) is optional.
  571. */
  572. delay = 1000;
  573. if (argc > 3)
  574. delay = simple_strtoul(argv[4], NULL, 10);
  575. /*
  576. * Run the loop...
  577. */
  578. while (1) {
  579. if (i2c_read(chip, addr, alen, bytes, length) != 0)
  580. puts ("Error reading the chip.\n");
  581. udelay(delay);
  582. }
  583. /* NOTREACHED */
  584. return 0;
  585. }
  586. /*
  587. * The SDRAM command is separately configured because many
  588. * (most?) embedded boards don't use SDRAM DIMMs.
  589. */
  590. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  591. /*
  592. * Syntax:
  593. * sdram {i2c_chip}
  594. */
  595. int do_sdram ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  596. {
  597. u_char chip;
  598. u_char data[128];
  599. u_char cksum;
  600. int j;
  601. if (argc < 2) {
  602. printf ("Usage:\n%s\n", cmdtp->usage);
  603. return 1;
  604. }
  605. /*
  606. * Chip is always specified.
  607. */
  608. chip = simple_strtoul(argv[1], NULL, 16);
  609. if (i2c_read(chip, 0, 1, data, sizeof(data)) != 0) {
  610. puts ("No SDRAM Serial Presence Detect found.\n");
  611. return 1;
  612. }
  613. cksum = 0;
  614. for (j = 0; j < 63; j++) {
  615. cksum += data[j];
  616. }
  617. if (cksum != data[63]) {
  618. printf ("WARNING: Configuration data checksum failure:\n"
  619. " is 0x%02x, calculated 0x%02x\n",
  620. data[63], cksum);
  621. }
  622. printf("SPD data revision %d.%d\n",
  623. (data[62] >> 4) & 0x0F, data[62] & 0x0F);
  624. printf("Bytes used 0x%02X\n", data[0]);
  625. printf("Serial memory size 0x%02X\n", 1 << data[1]);
  626. puts ("Memory type ");
  627. switch(data[2]) {
  628. case 2: puts ("EDO\n"); break;
  629. case 4: puts ("SDRAM\n"); break;
  630. default: puts ("unknown\n"); break;
  631. }
  632. puts ("Row address bits ");
  633. if ((data[3] & 0x00F0) == 0)
  634. printf("%d\n", data[3] & 0x0F);
  635. else
  636. printf("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
  637. puts ("Column address bits ");
  638. if ((data[4] & 0x00F0) == 0)
  639. printf("%d\n", data[4] & 0x0F);
  640. else
  641. printf("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
  642. printf("Module rows %d\n", data[5]);
  643. printf("Module data width %d bits\n", (data[7] << 8) | data[6]);
  644. puts ("Interface signal levels ");
  645. switch(data[8]) {
  646. case 0: puts ("5.0v/TTL\n"); break;
  647. case 1: puts ("LVTTL\n"); break;
  648. case 2: puts ("HSTL 1.5\n"); break;
  649. case 3: puts ("SSTL 3.3\n"); break;
  650. case 4: puts ("SSTL 2.5\n"); break;
  651. default: puts ("unknown\n"); break;
  652. }
  653. printf("SDRAM cycle time %d.%d nS\n",
  654. (data[9] >> 4) & 0x0F, data[9] & 0x0F);
  655. printf("SDRAM access time %d.%d nS\n",
  656. (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  657. puts ("EDC configuration ");
  658. switch(data[11]) {
  659. case 0: puts ("None\n"); break;
  660. case 1: puts ("Parity\n"); break;
  661. case 2: puts ("ECC\n"); break;
  662. default: puts ("unknown\n"); break;
  663. }
  664. if ((data[12] & 0x80) == 0)
  665. puts ("No self refresh, rate ");
  666. else
  667. puts ("Self refresh, rate ");
  668. switch(data[12] & 0x7F) {
  669. case 0: puts ("15.625uS\n"); break;
  670. case 1: puts ("3.9uS\n"); break;
  671. case 2: puts ("7.8uS\n"); break;
  672. case 3: puts ("31.3uS\n"); break;
  673. case 4: puts ("62.5uS\n"); break;
  674. case 5: puts ("125uS\n"); break;
  675. default: puts ("unknown\n"); break;
  676. }
  677. printf("SDRAM width (primary) %d\n", data[13] & 0x7F);
  678. if ((data[13] & 0x80) != 0) {
  679. printf(" (second bank) %d\n",
  680. 2 * (data[13] & 0x7F));
  681. }
  682. if (data[14] != 0) {
  683. printf("EDC width %d\n",
  684. data[14] & 0x7F);
  685. if ((data[14] & 0x80) != 0)
  686. printf(" (second bank) %d\n",
  687. 2 * (data[14] & 0x7F));
  688. }
  689. printf("Min clock delay, back-to-back random column addresses %d\n",
  690. data[15]);
  691. puts ("Burst length(s) ");
  692. if (data[16] & 0x80) puts (" Page");
  693. if (data[16] & 0x08) puts (" 8");
  694. if (data[16] & 0x04) puts (" 4");
  695. if (data[16] & 0x02) puts (" 2");
  696. if (data[16] & 0x01) puts (" 1");
  697. putc ('\n');
  698. printf("Number of banks %d\n", data[17]);
  699. puts ("CAS latency(s) ");
  700. if (data[18] & 0x80) puts (" TBD");
  701. if (data[18] & 0x40) puts (" 7");
  702. if (data[18] & 0x20) puts (" 6");
  703. if (data[18] & 0x10) puts (" 5");
  704. if (data[18] & 0x08) puts (" 4");
  705. if (data[18] & 0x04) puts (" 3");
  706. if (data[18] & 0x02) puts (" 2");
  707. if (data[18] & 0x01) puts (" 1");
  708. putc ('\n');
  709. puts ("CS latency(s) ");
  710. if (data[19] & 0x80) puts (" TBD");
  711. if (data[19] & 0x40) puts (" 6");
  712. if (data[19] & 0x20) puts (" 5");
  713. if (data[19] & 0x10) puts (" 4");
  714. if (data[19] & 0x08) puts (" 3");
  715. if (data[19] & 0x04) puts (" 2");
  716. if (data[19] & 0x02) puts (" 1");
  717. if (data[19] & 0x01) puts (" 0");
  718. putc ('\n');
  719. puts ("WE latency(s) ");
  720. if (data[20] & 0x80) puts (" TBD");
  721. if (data[20] & 0x40) puts (" 6");
  722. if (data[20] & 0x20) puts (" 5");
  723. if (data[20] & 0x10) puts (" 4");
  724. if (data[20] & 0x08) puts (" 3");
  725. if (data[20] & 0x04) puts (" 2");
  726. if (data[20] & 0x02) puts (" 1");
  727. if (data[20] & 0x01) puts (" 0");
  728. putc ('\n');
  729. puts ("Module attributes:\n");
  730. if (!data[21]) puts (" (none)\n");
  731. if (data[21] & 0x80) puts (" TBD (bit 7)\n");
  732. if (data[21] & 0x40) puts (" Redundant row address\n");
  733. if (data[21] & 0x20) puts (" Differential clock input\n");
  734. if (data[21] & 0x10) puts (" Registerd DQMB inputs\n");
  735. if (data[21] & 0x08) puts (" Buffered DQMB inputs\n");
  736. if (data[21] & 0x04) puts (" On-card PLL\n");
  737. if (data[21] & 0x02) puts (" Registered address/control lines\n");
  738. if (data[21] & 0x01) puts (" Buffered address/control lines\n");
  739. puts ("Device attributes:\n");
  740. if (data[22] & 0x80) puts (" TBD (bit 7)\n");
  741. if (data[22] & 0x40) puts (" TBD (bit 6)\n");
  742. if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
  743. else puts (" Upper Vcc tolerance 10%\n");
  744. if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
  745. else puts (" Lower Vcc tolerance 10%\n");
  746. if (data[22] & 0x08) puts (" Supports write1/read burst\n");
  747. if (data[22] & 0x04) puts (" Supports precharge all\n");
  748. if (data[22] & 0x02) puts (" Supports auto precharge\n");
  749. if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
  750. printf("SDRAM cycle time (2nd highest CAS latency) %d.%d nS\n",
  751. (data[23] >> 4) & 0x0F, data[23] & 0x0F);
  752. printf("SDRAM access from clock (2nd highest CAS latency) %d.%d nS\n",
  753. (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  754. printf("SDRAM cycle time (3rd highest CAS latency) %d.%d nS\n",
  755. (data[25] >> 4) & 0x0F, data[25] & 0x0F);
  756. printf("SDRAM access from clock (3rd highest CAS latency) %d.%d nS\n",
  757. (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  758. printf("Minimum row precharge %d nS\n", data[27]);
  759. printf("Row active to row active min %d nS\n", data[28]);
  760. printf("RAS to CAS delay min %d nS\n", data[29]);
  761. printf("Minimum RAS pulse width %d nS\n", data[30]);
  762. puts ("Density of each row ");
  763. if (data[31] & 0x80) puts (" 512");
  764. if (data[31] & 0x40) puts (" 256");
  765. if (data[31] & 0x20) puts (" 128");
  766. if (data[31] & 0x10) puts (" 64");
  767. if (data[31] & 0x08) puts (" 32");
  768. if (data[31] & 0x04) puts (" 16");
  769. if (data[31] & 0x02) puts (" 8");
  770. if (data[31] & 0x01) puts (" 4");
  771. puts ("MByte\n");
  772. printf("Command and Address setup %c%d.%d nS\n",
  773. (data[32] & 0x80) ? '-' : '+',
  774. (data[32] >> 4) & 0x07, data[32] & 0x0F);
  775. printf("Command and Address hold %c%d.%d nS\n",
  776. (data[33] & 0x80) ? '-' : '+',
  777. (data[33] >> 4) & 0x07, data[33] & 0x0F);
  778. printf("Data signal input setup %c%d.%d nS\n",
  779. (data[34] & 0x80) ? '-' : '+',
  780. (data[34] >> 4) & 0x07, data[34] & 0x0F);
  781. printf("Data signal input hold %c%d.%d nS\n",
  782. (data[35] & 0x80) ? '-' : '+',
  783. (data[35] >> 4) & 0x07, data[35] & 0x0F);
  784. puts ("Manufacturer's JEDEC ID ");
  785. for (j = 64; j <= 71; j++)
  786. printf("%02X ", data[j]);
  787. putc ('\n');
  788. printf("Manufacturing Location %02X\n", data[72]);
  789. puts ("Manufacturer's Part Number ");
  790. for (j = 73; j <= 90; j++)
  791. printf("%02X ", data[j]);
  792. putc ('\n');
  793. printf("Revision Code %02X %02X\n", data[91], data[92]);
  794. printf("Manufacturing Date %02X %02X\n", data[93], data[94]);
  795. puts ("Assembly Serial Number ");
  796. for (j = 95; j <= 98; j++)
  797. printf("%02X ", data[j]);
  798. putc ('\n');
  799. printf("Speed rating PC%d\n",
  800. data[126] == 0x66 ? 66 : data[126]);
  801. return 0;
  802. }
  803. #endif /* CFG_CMD_SDRAM */
  804. #if defined(CONFIG_I2C_CMD_TREE)
  805. #if defined(CONFIG_I2C_MULTI_BUS)
  806. int do_i2c_bus_num(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  807. {
  808. int bus_idx, ret=0;
  809. if (argc == 1)
  810. /* querying current setting */
  811. printf("Current bus is %d\n", i2c_get_bus_num());
  812. else {
  813. bus_idx = simple_strtoul(argv[1], NULL, 10);
  814. printf("Setting bus to %d\n", bus_idx);
  815. ret = i2c_set_bus_num(bus_idx);
  816. if (ret)
  817. printf("Failure changing bus number (%d)\n", ret);
  818. }
  819. return ret;
  820. }
  821. #endif /* CONFIG_I2C_MULTI_BUS */
  822. int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  823. {
  824. int speed, ret=0;
  825. if (argc == 1)
  826. /* querying current speed */
  827. printf("Current bus speed=%d\n", i2c_get_bus_speed());
  828. else {
  829. speed = simple_strtoul(argv[1], NULL, 10);
  830. printf("Setting bus speed to %d Hz\n", speed);
  831. ret = i2c_set_bus_speed(speed);
  832. if (ret)
  833. printf("Failure changing bus speed (%d)\n", ret);
  834. }
  835. return ret;
  836. }
  837. int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  838. {
  839. #if defined(CONFIG_I2C_MULTI_BUS)
  840. if (!strncmp(argv[1], "de", 2))
  841. return do_i2c_bus_num(cmdtp, flag, --argc, ++argv);
  842. #endif /* CONFIG_I2C_MULTI_BUS */
  843. if (!strncmp(argv[1], "sp", 2))
  844. return do_i2c_bus_speed(cmdtp, flag, --argc, ++argv);
  845. if (!strncmp(argv[1], "md", 2))
  846. return do_i2c_md(cmdtp, flag, --argc, ++argv);
  847. if (!strncmp(argv[1], "mm", 2))
  848. return do_i2c_mm(cmdtp, flag, --argc, ++argv);
  849. if (!strncmp(argv[1], "mw", 2))
  850. return do_i2c_mw(cmdtp, flag, --argc, ++argv);
  851. if (!strncmp(argv[1], "nm", 2))
  852. return do_i2c_nm(cmdtp, flag, --argc, ++argv);
  853. if (!strncmp(argv[1], "cr", 2))
  854. return do_i2c_crc(cmdtp, flag, --argc, ++argv);
  855. if (!strncmp(argv[1], "pr", 2))
  856. return do_i2c_probe(cmdtp, flag, --argc, ++argv);
  857. if (!strncmp(argv[1], "lo", 2))
  858. return do_i2c_loop(cmdtp, flag, --argc, ++argv);
  859. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  860. if (!strncmp(argv[1], "sd", 2))
  861. return do_sdram(cmdtp, flag, --argc, ++argv);
  862. #endif /* CFG_CMD_SDRAM */
  863. else
  864. printf ("Usage:\n%s\n", cmdtp->usage);
  865. return 0;
  866. }
  867. #endif /* CONFIG_I2C_CMD_TREE */
  868. /***************************************************/
  869. #if defined(CONFIG_I2C_CMD_TREE)
  870. U_BOOT_CMD(
  871. i2c, 6, 1, do_i2c,
  872. "i2c - I2C sub-system\n",
  873. #if defined(CONFIG_I2C_MULTI_BUS)
  874. "dev [dev] - show or set current I2C bus\n"
  875. #endif /* CONFIG_I2C_MULTI_BUS */
  876. "i2c speed [speed] - show or set I2C bus speed\n"
  877. "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
  878. "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
  879. "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
  880. "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
  881. "i2c crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
  882. "i2c probe - show devices on the I2C bus\n"
  883. "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
  884. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  885. "i2c sdram chip - print SDRAM configuration information\n"
  886. #endif /* CFG_CMD_SDRAM */
  887. );
  888. #else /* CONFIG_I2C_CMD_TREE */
  889. U_BOOT_CMD(
  890. imd, 4, 1, do_i2c_md, \
  891. "imd - i2c memory display\n", \
  892. "chip address[.0, .1, .2] [# of objects]\n - i2c memory display\n" \
  893. );
  894. U_BOOT_CMD(
  895. imm, 3, 1, do_i2c_mm,
  896. "imm - i2c memory modify (auto-incrementing)\n",
  897. "chip address[.0, .1, .2]\n"
  898. " - memory modify, auto increment address\n"
  899. );
  900. U_BOOT_CMD(
  901. inm, 3, 1, do_i2c_nm,
  902. "inm - memory modify (constant address)\n",
  903. "chip address[.0, .1, .2]\n - memory modify, read and keep address\n"
  904. );
  905. U_BOOT_CMD(
  906. imw, 5, 1, do_i2c_mw,
  907. "imw - memory write (fill)\n",
  908. "chip address[.0, .1, .2] value [count]\n - memory write (fill)\n"
  909. );
  910. U_BOOT_CMD(
  911. icrc32, 5, 1, do_i2c_crc,
  912. "icrc32 - checksum calculation\n",
  913. "chip address[.0, .1, .2] count\n - compute CRC32 checksum\n"
  914. );
  915. U_BOOT_CMD(
  916. iprobe, 1, 1, do_i2c_probe,
  917. "iprobe - probe to discover valid I2C chip addresses\n",
  918. "\n -discover valid I2C chip addresses\n"
  919. );
  920. /*
  921. * Require full name for "iloop" because it is an infinite loop!
  922. */
  923. U_BOOT_CMD(
  924. iloop, 5, 1, do_i2c_loop,
  925. "iloop - infinite loop on address range\n",
  926. "chip address[.0, .1, .2] [# of objects]\n"
  927. " - loop, reading a set of addresses\n"
  928. );
  929. #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
  930. U_BOOT_CMD(
  931. isdram, 2, 1, do_sdram,
  932. "isdram - print SDRAM configuration information\n",
  933. "chip\n - print SDRAM configuration information\n"
  934. " (valid chip values 50..57)\n"
  935. );
  936. #endif
  937. #endif /* CONFIG_I2C_CMD_TREE */
  938. #endif /* CFG_CMD_I2C */