3 * Sergey Kubushyn, himself, ksi@koi8.net
5 * Changes for unified multibus/multiadapter I2C support.
8 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
10 * SPDX-License-Identifier: GPL-2.0+
14 * I2C Functions similar to the standard memory functions.
16 * There are several parameters in many of the commands that bear further
19 * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
20 * Each I2C chip on the bus has a unique address. On the I2C data bus,
21 * the address is the upper seven bits and the LSB is the "read/write"
22 * bit. Note that the {i2c_chip} address specified on the command
23 * line is not shifted up: e.g. a typical EEPROM memory chip may have
24 * an I2C address of 0x50, but the data put on the bus will be 0xA0
25 * for write and 0xA1 for read. This "non shifted" address notation
26 * matches at least half of the data sheets :-/.
28 * {addr} is the address (or offset) within the chip. Small memory
29 * chips have 8 bit addresses. Large memory chips have 16 bit
30 * addresses. Other memory chips have 9, 10, or 11 bit addresses.
31 * Many non-memory chips have multiple registers and {addr} is used
32 * as the register index. Some non-memory chips have only one register
33 * and therefore don't need any {addr} parameter.
35 * The default {addr} parameter is one byte (.1) which works well for
36 * memories and registers with 8 bits of address space.
38 * You can specify the length of the {addr} field with the optional .0,
39 * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
40 * manipulating a single register device which doesn't use an address
41 * field, use "0.0" for the address and the ".0" length field will
42 * suppress the address in the I2C data stream. This also works for
43 * successive reads using the I2C auto-incrementing memory pointer.
45 * If you are manipulating a large memory with 2-byte addresses, use
46 * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
48 * Then there are the unfortunate memory chips that spill the most
49 * significant 1, 2, or 3 bits of address into the chip address byte.
50 * This effectively makes one chip (logically) look like 2, 4, or
51 * 8 chips. This is handled (awkwardly) by #defining
52 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
53 * {addr} field (since .1 is the default, it doesn't actually have to
54 * be specified). Examples: given a memory chip at I2C chip address
55 * 0x50, the following would happen...
56 * i2c md 50 0 10 display 16 bytes starting at 0x000
57 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
58 * i2c md 50 100 10 display 16 bytes starting at 0x100
59 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
60 * i2c md 50 210 10 display 16 bytes starting at 0x210
61 * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
62 * This is awfully ugly. It would be nice if someone would think up
63 * a better way of handling this.
65 * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
72 #include <environment.h>
75 #include <asm/byteorder.h>
76 #include <linux/compiler.h>
78 DECLARE_GLOBAL_DATA_PTR;
80 /* Display values from last command.
81 * Memory modify remembered values are different from display memory.
83 static uchar i2c_dp_last_chip;
84 static uint i2c_dp_last_addr;
85 static uint i2c_dp_last_alen;
86 static uint i2c_dp_last_length = 0x10;
88 static uchar i2c_mm_last_chip;
89 static uint i2c_mm_last_addr;
90 static uint i2c_mm_last_alen;
92 /* If only one I2C bus is present, the list of devices to ignore when
93 * the probe command is issued is represented by a 1D array of addresses.
94 * When multiple buses are present, the list is an array of bus-address
95 * pairs. The following macros take care of this */
97 #if defined(CONFIG_SYS_I2C_NOPROBES)
98 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
103 } i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
104 #define GET_BUS_NUM i2c_get_bus_num()
105 #define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
106 #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
107 #define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
108 #else /* single bus */
109 static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
110 #define GET_BUS_NUM 0
111 #define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
112 #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
113 #define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
114 #endif /* defined(CONFIG_SYS_I2C) */
117 #define DISP_LINE_LEN 16
120 * i2c_init_board() - Board-specific I2C bus init
122 * This function is the default no-op implementation of I2C bus
123 * initialization. This function can be overriden by board-specific
124 * implementation if needed.
127 void i2c_init_board(void)
131 /* TODO: Implement architecture-specific get/set functions */
134 * i2c_get_bus_speed() - Return I2C bus speed
136 * This function is the default implementation of function for retrieveing
137 * the current I2C bus speed in Hz.
139 * A driver implementing runtime switching of I2C bus speed must override
140 * this function to report the speed correctly. Simple or legacy drivers
141 * can use this fallback.
143 * Returns I2C bus speed in Hz.
145 #if !defined(CONFIG_SYS_I2C)
147 * TODO: Implement architecture-specific get/set functions
148 * Should go away, if we switched completely to new multibus support
151 unsigned int i2c_get_bus_speed(void)
153 return CONFIG_SYS_I2C_SPEED;
157 * i2c_set_bus_speed() - Configure I2C bus speed
158 * @speed: Newly set speed of the I2C bus in Hz
160 * This function is the default implementation of function for setting
161 * the I2C bus speed in Hz.
163 * A driver implementing runtime switching of I2C bus speed must override
164 * this function to report the speed correctly. Simple or legacy drivers
165 * can use this fallback.
167 * Returns zero on success, negative value on error.
170 int i2c_set_bus_speed(unsigned int speed)
172 if (speed != CONFIG_SYS_I2C_SPEED)
180 * get_alen() - Small parser helper function to get address length
182 * Returns the address length.
184 static uint get_alen(char *arg)
190 for (j = 0; j < 8; j++) {
192 alen = arg[j+1] - '0';
194 } else if (arg[j] == '\0')
201 * do_i2c_read() - Handle the "i2c read" command-line command
202 * @cmdtp: Command data struct pointer
203 * @flag: Command flag
204 * @argc: Command-line argument count
205 * @argv: Array of command-line arguments
207 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
211 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
213 static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
216 uint devaddr, alen, length;
220 return CMD_RET_USAGE;
225 chip = simple_strtoul(argv[1], NULL, 16);
228 * I2C data address within the chip. This can be 1 or
229 * 2 bytes long. Some day it might be 3 bytes long :-).
231 devaddr = simple_strtoul(argv[2], NULL, 16);
232 alen = get_alen(argv[2]);
234 return CMD_RET_USAGE;
237 * Length is the number of objects, not number of bytes.
239 length = simple_strtoul(argv[3], NULL, 16);
242 * memaddr is the address where to store things in memory
244 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
246 if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
247 puts ("Error reading the chip.\n");
253 static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
256 uint devaddr, alen, length;
260 return cmd_usage(cmdtp);
263 * memaddr is the address where to store things in memory
265 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
270 chip = simple_strtoul(argv[2], NULL, 16);
273 * I2C data address within the chip. This can be 1 or
274 * 2 bytes long. Some day it might be 3 bytes long :-).
276 devaddr = simple_strtoul(argv[3], NULL, 16);
277 alen = get_alen(argv[3]);
279 return cmd_usage(cmdtp);
282 * Length is the number of objects, not number of bytes.
284 length = simple_strtoul(argv[4], NULL, 16);
286 while (length-- > 0) {
287 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
288 puts("Error writing to the chip.\n");
292 * No write delay with FRAM devices.
294 #if !defined(CONFIG_SYS_I2C_FRAM)
302 * do_i2c_md() - Handle the "i2c md" command-line command
303 * @cmdtp: Command data struct pointer
304 * @flag: Command flag
305 * @argc: Command-line argument count
306 * @argv: Array of command-line arguments
308 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
312 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
314 static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
317 uint addr, alen, length;
318 int j, nbytes, linebytes;
320 /* We use the last specified parameters, unless new ones are
323 chip = i2c_dp_last_chip;
324 addr = i2c_dp_last_addr;
325 alen = i2c_dp_last_alen;
326 length = i2c_dp_last_length;
329 return CMD_RET_USAGE;
331 if ((flag & CMD_FLAG_REPEAT) == 0) {
333 * New command specified.
339 chip = simple_strtoul(argv[1], NULL, 16);
342 * I2C data address within the chip. This can be 1 or
343 * 2 bytes long. Some day it might be 3 bytes long :-).
345 addr = simple_strtoul(argv[2], NULL, 16);
346 alen = get_alen(argv[2]);
348 return CMD_RET_USAGE;
351 * If another parameter, it is the length to display.
352 * Length is the number of objects, not number of bytes.
355 length = simple_strtoul(argv[3], NULL, 16);
361 * We buffer all read data, so we can make sure data is read only
366 unsigned char linebuf[DISP_LINE_LEN];
369 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
371 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
372 puts ("Error reading the chip.\n");
374 printf("%04x:", addr);
376 for (j=0; j<linebytes; j++) {
377 printf(" %02x", *cp++);
382 for (j=0; j<linebytes; j++) {
383 if ((*cp < 0x20) || (*cp > 0x7e))
392 } while (nbytes > 0);
394 i2c_dp_last_chip = chip;
395 i2c_dp_last_addr = addr;
396 i2c_dp_last_alen = alen;
397 i2c_dp_last_length = length;
403 * do_i2c_mw() - Handle the "i2c mw" command-line command
404 * @cmdtp: Command data struct pointer
405 * @flag: Command flag
406 * @argc: Command-line argument count
407 * @argv: Array of command-line arguments
409 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
413 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
415 static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
423 if ((argc < 4) || (argc > 5))
424 return CMD_RET_USAGE;
427 * Chip is always specified.
429 chip = simple_strtoul(argv[1], NULL, 16);
432 * Address is always specified.
434 addr = simple_strtoul(argv[2], NULL, 16);
435 alen = get_alen(argv[2]);
437 return CMD_RET_USAGE;
440 * Value to write is always specified.
442 byte = simple_strtoul(argv[3], NULL, 16);
448 count = simple_strtoul(argv[4], NULL, 16);
452 while (count-- > 0) {
453 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
454 puts ("Error writing the chip.\n");
456 * Wait for the write to complete. The write can take
457 * up to 10mSec (we allow a little more time).
460 * No write delay with FRAM devices.
462 #if !defined(CONFIG_SYS_I2C_FRAM)
471 * do_i2c_crc() - Handle the "i2c crc32" command-line command
472 * @cmdtp: Command data struct pointer
473 * @flag: Command flag
474 * @argc: Command-line argument count
475 * @argv: Array of command-line arguments
477 * Calculate a CRC on memory
479 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
483 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
485 static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
496 return CMD_RET_USAGE;
499 * Chip is always specified.
501 chip = simple_strtoul(argv[1], NULL, 16);
504 * Address is always specified.
506 addr = simple_strtoul(argv[2], NULL, 16);
507 alen = get_alen(argv[2]);
509 return CMD_RET_USAGE;
512 * Count is always specified
514 count = simple_strtoul(argv[3], NULL, 16);
516 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
518 * CRC a byte at a time. This is going to be slooow, but hey, the
519 * memories are small and slow too so hopefully nobody notices.
523 while (count-- > 0) {
524 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
526 crc = crc32 (crc, &byte, 1);
530 puts ("Error reading the chip,\n");
532 printf ("%08lx\n", crc);
538 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
539 * @cmdtp: Command data struct pointer
540 * @flag: Command flag
541 * @argc: Command-line argument count
542 * @argv: Array of command-line arguments
546 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
550 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
551 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
554 mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
564 return CMD_RET_USAGE;
566 #ifdef CONFIG_BOOT_RETRY_TIME
567 reset_cmd_timeout(); /* got a good command to get here */
570 * We use the last specified parameters, unless new ones are
573 chip = i2c_mm_last_chip;
574 addr = i2c_mm_last_addr;
575 alen = i2c_mm_last_alen;
577 if ((flag & CMD_FLAG_REPEAT) == 0) {
579 * New command specified. Check for a size specification.
580 * Defaults to byte if no or incorrect specification.
582 size = cmd_get_data_size(argv[0], 1);
585 * Chip is always specified.
587 chip = simple_strtoul(argv[1], NULL, 16);
590 * Address is always specified.
592 addr = simple_strtoul(argv[2], NULL, 16);
593 alen = get_alen(argv[2]);
595 return CMD_RET_USAGE;
599 * Print the address, followed by value. Then accept input for
600 * the next value. A non-converted value exits.
603 printf("%08lx:", addr);
604 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
605 puts ("\nError reading the chip,\n");
607 data = cpu_to_be32(data);
609 printf(" %02lx", (data >> 24) & 0x000000FF);
611 printf(" %04lx", (data >> 16) & 0x0000FFFF);
613 printf(" %08lx", data);
616 nbytes = readline (" ? ");
619 * <CR> pressed as only input, don't modify current
620 * location and move to next.
625 #ifdef CONFIG_BOOT_RETRY_TIME
626 reset_cmd_timeout(); /* good enough to not time out */
629 #ifdef CONFIG_BOOT_RETRY_TIME
630 else if (nbytes == -2)
631 break; /* timed out, exit the command */
636 data = simple_strtoul(console_buffer, &endp, 16);
641 data = be32_to_cpu(data);
642 nbytes = endp - console_buffer;
644 #ifdef CONFIG_BOOT_RETRY_TIME
646 * good enough to not time out
650 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
651 puts ("Error writing the chip.\n");
652 #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
653 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
661 i2c_mm_last_chip = chip;
662 i2c_mm_last_addr = addr;
663 i2c_mm_last_alen = alen;
669 * do_i2c_probe() - Handle the "i2c probe" command-line command
670 * @cmdtp: Command data struct pointer
671 * @flag: Command flag
672 * @argc: Command-line argument count
673 * @argv: Array of command-line arguments
675 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
681 * Returns zero (success) if one or more I2C devices was found
683 static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
688 #if defined(CONFIG_SYS_I2C_NOPROBES)
690 unsigned int bus = GET_BUS_NUM;
691 #endif /* NOPROBES */
694 addr = simple_strtol(argv[1], 0, 16);
696 puts ("Valid chip addresses:");
697 for (j = 0; j < 128; j++) {
698 if ((0 <= addr) && (j != addr))
701 #if defined(CONFIG_SYS_I2C_NOPROBES)
703 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
704 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
712 if (i2c_probe(j) == 0) {
719 #if defined(CONFIG_SYS_I2C_NOPROBES)
720 puts ("Excluded chip addresses:");
721 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
722 if (COMPARE_BUS(bus,k))
723 printf(" %02X", NO_PROBE_ADDR(k));
732 * do_i2c_loop() - Handle the "i2c loop" command-line command
733 * @cmdtp: Command data struct pointer
734 * @flag: Command flag
735 * @argc: Command-line argument count
736 * @argv: Array of command-line arguments
738 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
742 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
743 * {length} - Number of bytes to read
744 * {delay} - A DECIMAL number and defaults to 1000 uSec
746 static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
756 return CMD_RET_USAGE;
759 * Chip is always specified.
761 chip = simple_strtoul(argv[1], NULL, 16);
764 * Address is always specified.
766 addr = simple_strtoul(argv[2], NULL, 16);
767 alen = get_alen(argv[2]);
769 return CMD_RET_USAGE;
772 * Length is the number of objects, not number of bytes.
775 length = simple_strtoul(argv[3], NULL, 16);
776 if (length > sizeof(bytes))
777 length = sizeof(bytes);
780 * The delay time (uSec) is optional.
784 delay = simple_strtoul(argv[4], NULL, 10);
789 if (i2c_read(chip, addr, alen, bytes, length) != 0)
790 puts ("Error reading the chip.\n");
799 * The SDRAM command is separately configured because many
800 * (most?) embedded boards don't use SDRAM DIMMs.
802 * FIXME: Document and probably move elsewhere!
804 #if defined(CONFIG_CMD_SDRAM)
805 static void print_ddr2_tcyc (u_char const b)
807 printf ("%d.", (b >> 4) & 0x0F);
819 printf ("%d ns\n", b & 0x0F);
839 static void decode_bits (u_char const b, char const *str[], int const do_once)
843 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
854 * i2c sdram {i2c_chip}
856 static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
858 enum { unknown, EDO, SDRAM, DDR2 } type;
865 static const char *decode_CAS_DDR2[] = {
866 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
869 static const char *decode_CAS_default[] = {
870 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
873 static const char *decode_CS_WE_default[] = {
874 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
877 static const char *decode_byte21_default[] = {
879 " Redundant row address\n",
880 " Differential clock input\n",
881 " Registerd DQMB inputs\n",
882 " Buffered DQMB inputs\n",
884 " Registered address/control lines\n",
885 " Buffered address/control lines\n"
888 static const char *decode_byte22_DDR2[] = {
894 " Supports partial array self refresh\n",
895 " Supports 50 ohm ODT\n",
896 " Supports weak driver\n"
899 static const char *decode_row_density_DDR2[] = {
900 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
901 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
904 static const char *decode_row_density_default[] = {
905 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
906 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
910 return CMD_RET_USAGE;
913 * Chip is always specified.
915 chip = simple_strtoul (argv[1], NULL, 16);
917 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
918 puts ("No SDRAM Serial Presence Detect found.\n");
923 for (j = 0; j < 63; j++) {
926 if (cksum != data[63]) {
927 printf ("WARNING: Configuration data checksum failure:\n"
928 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
930 printf ("SPD data revision %d.%d\n",
931 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
932 printf ("Bytes used 0x%02X\n", data[0]);
933 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
935 puts ("Memory type ");
955 puts ("Row address bits ");
956 if ((data[3] & 0x00F0) == 0)
957 printf ("%d\n", data[3] & 0x0F);
959 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
961 puts ("Column address bits ");
962 if ((data[4] & 0x00F0) == 0)
963 printf ("%d\n", data[4] & 0x0F);
965 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
969 printf ("Number of ranks %d\n",
970 (data[5] & 0x07) + 1);
973 printf ("Module rows %d\n", data[5]);
979 printf ("Module data width %d bits\n", data[6]);
982 printf ("Module data width %d bits\n",
983 (data[7] << 8) | data[6]);
987 puts ("Interface signal levels ");
989 case 0: puts ("TTL 5.0 V\n"); break;
990 case 1: puts ("LVTTL\n"); break;
991 case 2: puts ("HSTL 1.5 V\n"); break;
992 case 3: puts ("SSTL 3.3 V\n"); break;
993 case 4: puts ("SSTL 2.5 V\n"); break;
994 case 5: puts ("SSTL 1.8 V\n"); break;
995 default: puts ("unknown\n"); break;
1000 printf ("SDRAM cycle time ");
1001 print_ddr2_tcyc (data[9]);
1004 printf ("SDRAM cycle time %d.%d ns\n",
1005 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
1011 printf ("SDRAM access time 0.%d%d ns\n",
1012 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1015 printf ("SDRAM access time %d.%d ns\n",
1016 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1020 puts ("EDC configuration ");
1022 case 0: puts ("None\n"); break;
1023 case 1: puts ("Parity\n"); break;
1024 case 2: puts ("ECC\n"); break;
1025 default: puts ("unknown\n"); break;
1028 if ((data[12] & 0x80) == 0)
1029 puts ("No self refresh, rate ");
1031 puts ("Self refresh, rate ");
1033 switch(data[12] & 0x7F) {
1034 case 0: puts ("15.625 us\n"); break;
1035 case 1: puts ("3.9 us\n"); break;
1036 case 2: puts ("7.8 us\n"); break;
1037 case 3: puts ("31.3 us\n"); break;
1038 case 4: puts ("62.5 us\n"); break;
1039 case 5: puts ("125 us\n"); break;
1040 default: puts ("unknown\n"); break;
1045 printf ("SDRAM width (primary) %d\n", data[13]);
1048 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
1049 if ((data[13] & 0x80) != 0) {
1050 printf (" (second bank) %d\n",
1051 2 * (data[13] & 0x7F));
1059 printf ("EDC width %d\n", data[14]);
1062 if (data[14] != 0) {
1063 printf ("EDC width %d\n",
1066 if ((data[14] & 0x80) != 0) {
1067 printf (" (second bank) %d\n",
1068 2 * (data[14] & 0x7F));
1075 printf ("Min clock delay, back-to-back random column addresses "
1079 puts ("Burst length(s) ");
1080 if (data[16] & 0x80) puts (" Page");
1081 if (data[16] & 0x08) puts (" 8");
1082 if (data[16] & 0x04) puts (" 4");
1083 if (data[16] & 0x02) puts (" 2");
1084 if (data[16] & 0x01) puts (" 1");
1086 printf ("Number of banks %d\n", data[17]);
1090 puts ("CAS latency(s) ");
1091 decode_bits (data[18], decode_CAS_DDR2, 0);
1095 puts ("CAS latency(s) ");
1096 decode_bits (data[18], decode_CAS_default, 0);
1102 puts ("CS latency(s) ");
1103 decode_bits (data[19], decode_CS_WE_default, 0);
1108 puts ("WE latency(s) ");
1109 decode_bits (data[20], decode_CS_WE_default, 0);
1115 puts ("Module attributes:\n");
1116 if (data[21] & 0x80)
1117 puts (" TBD (bit 7)\n");
1118 if (data[21] & 0x40)
1119 puts (" Analysis probe installed\n");
1120 if (data[21] & 0x20)
1121 puts (" TBD (bit 5)\n");
1122 if (data[21] & 0x10)
1123 puts (" FET switch external enable\n");
1124 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
1125 if (data[20] & 0x11) {
1126 printf (" %d active registers on DIMM\n",
1127 (data[21] & 0x03) + 1);
1131 puts ("Module attributes:\n");
1135 decode_bits (data[21], decode_byte21_default, 0);
1141 decode_bits (data[22], decode_byte22_DDR2, 0);
1144 puts ("Device attributes:\n");
1145 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1146 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1147 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1148 else puts (" Upper Vcc tolerance 10%\n");
1149 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1150 else puts (" Lower Vcc tolerance 10%\n");
1151 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1152 if (data[22] & 0x04) puts (" Supports precharge all\n");
1153 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1154 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1160 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1161 print_ddr2_tcyc (data[23]);
1164 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1165 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
1171 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1172 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1175 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1176 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1182 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1183 print_ddr2_tcyc (data[25]);
1186 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1187 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
1193 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1194 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1197 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1198 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1204 printf ("Minimum row precharge %d.%02d ns\n",
1205 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
1208 printf ("Minimum row precharge %d ns\n", data[27]);
1214 printf ("Row active to row active min %d.%02d ns\n",
1215 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
1218 printf ("Row active to row active min %d ns\n", data[28]);
1224 printf ("RAS to CAS delay min %d.%02d ns\n",
1225 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
1228 printf ("RAS to CAS delay min %d ns\n", data[29]);
1232 printf ("Minimum RAS pulse width %d ns\n", data[30]);
1236 puts ("Density of each row ");
1237 decode_bits (data[31], decode_row_density_DDR2, 1);
1241 puts ("Density of each row ");
1242 decode_bits (data[31], decode_row_density_default, 1);
1249 puts ("Command and Address setup ");
1250 if (data[32] >= 0xA0) {
1251 printf ("1.%d%d ns\n",
1252 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
1254 printf ("0.%d%d ns\n",
1255 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
1259 printf ("Command and Address setup %c%d.%d ns\n",
1260 (data[32] & 0x80) ? '-' : '+',
1261 (data[32] >> 4) & 0x07, data[32] & 0x0F);
1267 puts ("Command and Address hold ");
1268 if (data[33] >= 0xA0) {
1269 printf ("1.%d%d ns\n",
1270 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
1272 printf ("0.%d%d ns\n",
1273 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
1277 printf ("Command and Address hold %c%d.%d ns\n",
1278 (data[33] & 0x80) ? '-' : '+',
1279 (data[33] >> 4) & 0x07, data[33] & 0x0F);
1285 printf ("Data signal input setup 0.%d%d ns\n",
1286 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
1289 printf ("Data signal input setup %c%d.%d ns\n",
1290 (data[34] & 0x80) ? '-' : '+',
1291 (data[34] >> 4) & 0x07, data[34] & 0x0F);
1297 printf ("Data signal input hold 0.%d%d ns\n",
1298 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
1301 printf ("Data signal input hold %c%d.%d ns\n",
1302 (data[35] & 0x80) ? '-' : '+',
1303 (data[35] >> 4) & 0x07, data[35] & 0x0F);
1307 puts ("Manufacturer's JEDEC ID ");
1308 for (j = 64; j <= 71; j++)
1309 printf ("%02X ", data[j]);
1311 printf ("Manufacturing Location %02X\n", data[72]);
1312 puts ("Manufacturer's Part Number ");
1313 for (j = 73; j <= 90; j++)
1314 printf ("%02X ", data[j]);
1316 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1317 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
1318 puts ("Assembly Serial Number ");
1319 for (j = 95; j <= 98; j++)
1320 printf ("%02X ", data[j]);
1324 printf ("Speed rating PC%d\n",
1325 data[126] == 0x66 ? 66 : data[126]);
1333 * i2c edid {i2c_chip}
1335 #if defined(CONFIG_I2C_EDID)
1336 int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1339 struct edid1_info edid;
1346 chip = simple_strtoul(argv[1], NULL, 16);
1347 if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1348 puts("Error reading EDID content.\n");
1352 if (edid_check_info(&edid)) {
1353 puts("Content isn't valid EDID.\n");
1357 edid_print_info(&edid);
1361 #endif /* CONFIG_I2C_EDID */
1364 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
1365 * @cmdtp: Command data struct pointer
1366 * @flag: Command flag
1367 * @argc: Command-line argument count
1368 * @argv: Array of command-line arguments
1370 * Returns zero always.
1372 #if defined(CONFIG_SYS_I2C)
1373 int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1376 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1381 /* show all busses */
1382 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1383 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1384 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1385 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1386 if (i2c_bus[i].next_hop[j].chip == 0)
1388 printf("->%s@0x%2x:%d",
1389 i2c_bus[i].next_hop[j].mux.name,
1390 i2c_bus[i].next_hop[j].chip,
1391 i2c_bus[i].next_hop[j].channel);
1397 /* show specific bus */
1398 i = simple_strtoul(argv[1], NULL, 10);
1399 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1400 printf("Invalid bus %d\n", i);
1403 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1404 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1405 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1406 if (i2c_bus[i].next_hop[j].chip == 0)
1408 printf("->%s@0x%2x:%d",
1409 i2c_bus[i].next_hop[j].mux.name,
1410 i2c_bus[i].next_hop[j].chip,
1411 i2c_bus[i].next_hop[j].channel);
1422 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1423 * @cmdtp: Command data struct pointer
1424 * @flag: Command flag
1425 * @argc: Command-line argument count
1426 * @argv: Array of command-line arguments
1428 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1431 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
1432 int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1435 unsigned int bus_no;
1438 /* querying current setting */
1439 printf("Current bus is %d\n", i2c_get_bus_num());
1441 bus_no = simple_strtoul(argv[1], NULL, 10);
1442 #if defined(CONFIG_SYS_I2C)
1443 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1444 printf("Invalid bus %d\n", bus_no);
1448 printf("Setting bus to %d\n", bus_no);
1449 ret = i2c_set_bus_num(bus_no);
1451 printf("Failure changing bus number (%d)\n", ret);
1455 #endif /* defined(CONFIG_SYS_I2C) */
1458 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1459 * @cmdtp: Command data struct pointer
1460 * @flag: Command flag
1461 * @argc: Command-line argument count
1462 * @argv: Array of command-line arguments
1464 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1467 static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1472 /* querying current speed */
1473 printf("Current bus speed=%d\n", i2c_get_bus_speed());
1475 speed = simple_strtoul(argv[1], NULL, 10);
1476 printf("Setting bus speed to %d Hz\n", speed);
1477 ret = i2c_set_bus_speed(speed);
1479 printf("Failure changing bus speed (%d)\n", ret);
1485 * do_i2c_mm() - Handle the "i2c mm" command-line command
1486 * @cmdtp: Command data struct pointer
1487 * @flag: Command flag
1488 * @argc: Command-line argument count
1489 * @argv: Array of command-line arguments
1491 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1494 static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1496 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1500 * do_i2c_nm() - Handle the "i2c nm" command-line command
1501 * @cmdtp: Command data struct pointer
1502 * @flag: Command flag
1503 * @argc: Command-line argument count
1504 * @argv: Array of command-line arguments
1506 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1509 static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1511 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1515 * do_i2c_reset() - Handle the "i2c reset" command-line command
1516 * @cmdtp: Command data struct pointer
1517 * @flag: Command flag
1518 * @argc: Command-line argument count
1519 * @argv: Array of command-line arguments
1521 * Returns zero always.
1523 static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1525 #if defined(CONFIG_SYS_I2C)
1526 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1528 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
1533 static cmd_tbl_t cmd_i2c_sub[] = {
1534 #if defined(CONFIG_SYS_I2C)
1535 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
1537 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
1538 #if defined(CONFIG_SYS_I2C) || \
1539 defined(CONFIG_I2C_MULTI_BUS)
1540 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1541 #endif /* CONFIG_I2C_MULTI_BUS */
1542 #if defined(CONFIG_I2C_EDID)
1543 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1544 #endif /* CONFIG_I2C_EDID */
1545 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1546 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1547 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1548 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1549 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1550 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
1551 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
1552 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
1553 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1554 #if defined(CONFIG_CMD_SDRAM)
1555 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1557 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1560 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1561 void i2c_reloc(void) {
1562 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1567 * do_i2c() - Handle the "i2c" command-line command
1568 * @cmdtp: Command data struct pointer
1569 * @flag: Command flag
1570 * @argc: Command-line argument count
1571 * @argv: Array of command-line arguments
1573 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1576 static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1581 return CMD_RET_USAGE;
1583 /* Strip off leading 'i2c' command argument */
1587 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1590 return c->cmd(cmdtp, flag, argc, argv);
1592 return CMD_RET_USAGE;
1595 /***************************************************/
1596 #ifdef CONFIG_SYS_LONGHELP
1597 static char i2c_help_text[] =
1598 #if defined(CONFIG_SYS_I2C)
1599 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
1601 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
1602 #if defined(CONFIG_SYS_I2C) || \
1603 defined(CONFIG_I2C_MULTI_BUS)
1604 "i2c dev [dev] - show or set current I2C bus\n"
1605 #endif /* CONFIG_I2C_MULTI_BUS */
1606 #if defined(CONFIG_I2C_EDID)
1607 "i2c edid chip - print EDID configuration information\n"
1608 #endif /* CONFIG_I2C_EDID */
1609 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
1610 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1611 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1612 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1613 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
1614 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
1615 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
1616 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
1617 "i2c reset - re-init the I2C Controller\n"
1618 #if defined(CONFIG_CMD_SDRAM)
1619 "i2c sdram chip - print SDRAM configuration information\n"
1621 "i2c speed [speed] - show or set I2C bus speed";