3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 * SPDX-License-Identifier: GPL-2.0+
9 * I2C Functions similar to the standard memory functions.
11 * There are several parameters in many of the commands that bear further
14 * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
15 * Each I2C chip on the bus has a unique address. On the I2C data bus,
16 * the address is the upper seven bits and the LSB is the "read/write"
17 * bit. Note that the {i2c_chip} address specified on the command
18 * line is not shifted up: e.g. a typical EEPROM memory chip may have
19 * an I2C address of 0x50, but the data put on the bus will be 0xA0
20 * for write and 0xA1 for read. This "non shifted" address notation
21 * matches at least half of the data sheets :-/.
23 * {addr} is the address (or offset) within the chip. Small memory
24 * chips have 8 bit addresses. Large memory chips have 16 bit
25 * addresses. Other memory chips have 9, 10, or 11 bit addresses.
26 * Many non-memory chips have multiple registers and {addr} is used
27 * as the register index. Some non-memory chips have only one register
28 * and therefore don't need any {addr} parameter.
30 * The default {addr} parameter is one byte (.1) which works well for
31 * memories and registers with 8 bits of address space.
33 * You can specify the length of the {addr} field with the optional .0,
34 * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
35 * manipulating a single register device which doesn't use an address
36 * field, use "0.0" for the address and the ".0" length field will
37 * suppress the address in the I2C data stream. This also works for
38 * successive reads using the I2C auto-incrementing memory pointer.
40 * If you are manipulating a large memory with 2-byte addresses, use
41 * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
43 * Then there are the unfortunate memory chips that spill the most
44 * significant 1, 2, or 3 bits of address into the chip address byte.
45 * This effectively makes one chip (logically) look like 2, 4, or
46 * 8 chips. This is handled (awkwardly) by #defining
47 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
48 * {addr} field (since .1 is the default, it doesn't actually have to
49 * be specified). Examples: given a memory chip at I2C chip address
50 * 0x50, the following would happen...
51 * i2c md 50 0 10 display 16 bytes starting at 0x000
52 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
53 * i2c md 50 100 10 display 16 bytes starting at 0x100
54 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
55 * i2c md 50 210 10 display 16 bytes starting at 0x210
56 * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
57 * This is awfully ugly. It would be nice if someone would think up
58 * a better way of handling this.
60 * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
66 #include <environment.h>
69 #include <asm/byteorder.h>
70 #include <linux/compiler.h>
72 /* Display values from last command.
73 * Memory modify remembered values are different from display memory.
75 static uchar i2c_dp_last_chip;
76 static uint i2c_dp_last_addr;
77 static uint i2c_dp_last_alen;
78 static uint i2c_dp_last_length = 0x10;
80 static uchar i2c_mm_last_chip;
81 static uint i2c_mm_last_addr;
82 static uint i2c_mm_last_alen;
84 /* If only one I2C bus is present, the list of devices to ignore when
85 * the probe command is issued is represented by a 1D array of addresses.
86 * When multiple buses are present, the list is an array of bus-address
87 * pairs. The following macros take care of this */
89 #if defined(CONFIG_SYS_I2C_NOPROBES)
90 #if defined(CONFIG_I2C_MULTI_BUS)
95 } i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
96 #define GET_BUS_NUM i2c_get_bus_num()
97 #define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
98 #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
99 #define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
100 #else /* single bus */
101 static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
102 #define GET_BUS_NUM 0
103 #define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
104 #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
105 #define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
106 #endif /* CONFIG_MULTI_BUS */
108 #define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
111 #if defined(CONFIG_I2C_MUX)
112 static I2C_MUX_DEVICE *i2c_mux_devices = NULL;
113 static int i2c_mux_busid = CONFIG_SYS_MAX_I2C_BUS;
115 DECLARE_GLOBAL_DATA_PTR;
119 #define DISP_LINE_LEN 16
122 * i2c_init_board() - Board-specific I2C bus init
124 * This function is the default no-op implementation of I2C bus
125 * initialization. This function can be overriden by board-specific
126 * implementation if needed.
129 void i2c_init_board(void)
134 /* TODO: Implement architecture-specific get/set functions */
137 * i2c_get_bus_speed() - Return I2C bus speed
139 * This function is the default implementation of function for retrieveing
140 * the current I2C bus speed in Hz.
142 * A driver implementing runtime switching of I2C bus speed must override
143 * this function to report the speed correctly. Simple or legacy drivers
144 * can use this fallback.
146 * Returns I2C bus speed in Hz.
149 unsigned int i2c_get_bus_speed(void)
151 return CONFIG_SYS_I2C_SPEED;
155 * i2c_set_bus_speed() - Configure I2C bus speed
156 * @speed: Newly set speed of the I2C bus in Hz
158 * This function is the default implementation of function for setting
159 * the I2C bus speed in Hz.
161 * A driver implementing runtime switching of I2C bus speed must override
162 * this function to report the speed correctly. Simple or legacy drivers
163 * can use this fallback.
165 * Returns zero on success, negative value on error.
168 int i2c_set_bus_speed(unsigned int speed)
170 if (speed != CONFIG_SYS_I2C_SPEED)
177 * get_alen() - Small parser helper function to get address length
179 * Returns the address length.
181 static uint get_alen(char *arg)
187 for (j = 0; j < 8; j++) {
189 alen = arg[j+1] - '0';
191 } else if (arg[j] == '\0')
198 * do_i2c_read() - Handle the "i2c read" command-line command
199 * @cmdtp: Command data struct pointer
200 * @flag: Command flag
201 * @argc: Command-line argument count
202 * @argv: Array of command-line arguments
204 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
208 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
210 static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
213 uint devaddr, alen, length;
217 return CMD_RET_USAGE;
222 chip = simple_strtoul(argv[1], NULL, 16);
225 * I2C data address within the chip. This can be 1 or
226 * 2 bytes long. Some day it might be 3 bytes long :-).
228 devaddr = simple_strtoul(argv[2], NULL, 16);
229 alen = get_alen(argv[2]);
231 return CMD_RET_USAGE;
234 * Length is the number of objects, not number of bytes.
236 length = simple_strtoul(argv[3], NULL, 16);
239 * memaddr is the address where to store things in memory
241 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
243 if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
244 puts ("Error reading the chip.\n");
250 static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
253 uint devaddr, alen, length;
257 return cmd_usage(cmdtp);
260 * memaddr is the address where to store things in memory
262 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
267 chip = simple_strtoul(argv[2], NULL, 16);
270 * I2C data address within the chip. This can be 1 or
271 * 2 bytes long. Some day it might be 3 bytes long :-).
273 devaddr = simple_strtoul(argv[3], NULL, 16);
274 alen = get_alen(argv[3]);
276 return cmd_usage(cmdtp);
279 * Length is the number of objects, not number of bytes.
281 length = simple_strtoul(argv[4], NULL, 16);
283 while (length-- > 0) {
284 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
285 puts("Error writing to the chip.\n");
289 * No write delay with FRAM devices.
291 #if !defined(CONFIG_SYS_I2C_FRAM)
299 * do_i2c_md() - Handle the "i2c md" command-line command
300 * @cmdtp: Command data struct pointer
301 * @flag: Command flag
302 * @argc: Command-line argument count
303 * @argv: Array of command-line arguments
305 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
309 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
311 static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
314 uint addr, alen, length;
315 int j, nbytes, linebytes;
317 /* We use the last specified parameters, unless new ones are
320 chip = i2c_dp_last_chip;
321 addr = i2c_dp_last_addr;
322 alen = i2c_dp_last_alen;
323 length = i2c_dp_last_length;
326 return CMD_RET_USAGE;
328 if ((flag & CMD_FLAG_REPEAT) == 0) {
330 * New command specified.
336 chip = simple_strtoul(argv[1], NULL, 16);
339 * I2C data address within the chip. This can be 1 or
340 * 2 bytes long. Some day it might be 3 bytes long :-).
342 addr = simple_strtoul(argv[2], NULL, 16);
343 alen = get_alen(argv[2]);
345 return CMD_RET_USAGE;
348 * If another parameter, it is the length to display.
349 * Length is the number of objects, not number of bytes.
352 length = simple_strtoul(argv[3], NULL, 16);
358 * We buffer all read data, so we can make sure data is read only
363 unsigned char linebuf[DISP_LINE_LEN];
366 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
368 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
369 puts ("Error reading the chip.\n");
371 printf("%04x:", addr);
373 for (j=0; j<linebytes; j++) {
374 printf(" %02x", *cp++);
379 for (j=0; j<linebytes; j++) {
380 if ((*cp < 0x20) || (*cp > 0x7e))
389 } while (nbytes > 0);
391 i2c_dp_last_chip = chip;
392 i2c_dp_last_addr = addr;
393 i2c_dp_last_alen = alen;
394 i2c_dp_last_length = length;
400 * do_i2c_mw() - Handle the "i2c mw" command-line command
401 * @cmdtp: Command data struct pointer
402 * @flag: Command flag
403 * @argc: Command-line argument count
404 * @argv: Array of command-line arguments
406 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
410 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
412 static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
420 if ((argc < 4) || (argc > 5))
421 return CMD_RET_USAGE;
424 * Chip is always specified.
426 chip = simple_strtoul(argv[1], NULL, 16);
429 * Address is always specified.
431 addr = simple_strtoul(argv[2], NULL, 16);
432 alen = get_alen(argv[2]);
434 return CMD_RET_USAGE;
437 * Value to write is always specified.
439 byte = simple_strtoul(argv[3], NULL, 16);
445 count = simple_strtoul(argv[4], NULL, 16);
449 while (count-- > 0) {
450 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
451 puts ("Error writing the chip.\n");
453 * Wait for the write to complete. The write can take
454 * up to 10mSec (we allow a little more time).
457 * No write delay with FRAM devices.
459 #if !defined(CONFIG_SYS_I2C_FRAM)
468 * do_i2c_crc() - Handle the "i2c crc32" command-line command
469 * @cmdtp: Command data struct pointer
470 * @flag: Command flag
471 * @argc: Command-line argument count
472 * @argv: Array of command-line arguments
474 * Calculate a CRC on memory
476 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
480 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
482 static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
493 return CMD_RET_USAGE;
496 * Chip is always specified.
498 chip = simple_strtoul(argv[1], NULL, 16);
501 * Address is always specified.
503 addr = simple_strtoul(argv[2], NULL, 16);
504 alen = get_alen(argv[2]);
506 return CMD_RET_USAGE;
509 * Count is always specified
511 count = simple_strtoul(argv[3], NULL, 16);
513 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
515 * CRC a byte at a time. This is going to be slooow, but hey, the
516 * memories are small and slow too so hopefully nobody notices.
520 while (count-- > 0) {
521 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
523 crc = crc32 (crc, &byte, 1);
527 puts ("Error reading the chip,\n");
529 printf ("%08lx\n", crc);
535 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
536 * @cmdtp: Command data struct pointer
537 * @flag: Command flag
538 * @argc: Command-line argument count
539 * @argv: Array of command-line arguments
543 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
547 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
548 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
551 mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
561 return CMD_RET_USAGE;
563 #ifdef CONFIG_BOOT_RETRY_TIME
564 reset_cmd_timeout(); /* got a good command to get here */
567 * We use the last specified parameters, unless new ones are
570 chip = i2c_mm_last_chip;
571 addr = i2c_mm_last_addr;
572 alen = i2c_mm_last_alen;
574 if ((flag & CMD_FLAG_REPEAT) == 0) {
576 * New command specified. Check for a size specification.
577 * Defaults to byte if no or incorrect specification.
579 size = cmd_get_data_size(argv[0], 1);
582 * Chip is always specified.
584 chip = simple_strtoul(argv[1], NULL, 16);
587 * Address is always specified.
589 addr = simple_strtoul(argv[2], NULL, 16);
590 alen = get_alen(argv[2]);
592 return CMD_RET_USAGE;
596 * Print the address, followed by value. Then accept input for
597 * the next value. A non-converted value exits.
600 printf("%08lx:", addr);
601 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
602 puts ("\nError reading the chip,\n");
604 data = cpu_to_be32(data);
606 printf(" %02lx", (data >> 24) & 0x000000FF);
608 printf(" %04lx", (data >> 16) & 0x0000FFFF);
610 printf(" %08lx", data);
613 nbytes = readline (" ? ");
616 * <CR> pressed as only input, don't modify current
617 * location and move to next.
622 #ifdef CONFIG_BOOT_RETRY_TIME
623 reset_cmd_timeout(); /* good enough to not time out */
626 #ifdef CONFIG_BOOT_RETRY_TIME
627 else if (nbytes == -2)
628 break; /* timed out, exit the command */
633 data = simple_strtoul(console_buffer, &endp, 16);
638 data = be32_to_cpu(data);
639 nbytes = endp - console_buffer;
641 #ifdef CONFIG_BOOT_RETRY_TIME
643 * good enough to not time out
647 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
648 puts ("Error writing the chip.\n");
649 #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
650 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
658 i2c_mm_last_chip = chip;
659 i2c_mm_last_addr = addr;
660 i2c_mm_last_alen = alen;
666 * do_i2c_probe() - Handle the "i2c probe" command-line command
667 * @cmdtp: Command data struct pointer
668 * @flag: Command flag
669 * @argc: Command-line argument count
670 * @argv: Array of command-line arguments
672 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
678 * Returns zero (success) if one or more I2C devices was found
680 static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
685 #if defined(CONFIG_SYS_I2C_NOPROBES)
687 uchar bus = GET_BUS_NUM;
688 #endif /* NOPROBES */
691 addr = simple_strtol(argv[1], 0, 16);
693 puts ("Valid chip addresses:");
694 for (j = 0; j < 128; j++) {
695 if ((0 <= addr) && (j != addr))
698 #if defined(CONFIG_SYS_I2C_NOPROBES)
700 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
701 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
709 if (i2c_probe(j) == 0) {
716 #if defined(CONFIG_SYS_I2C_NOPROBES)
717 puts ("Excluded chip addresses:");
718 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
719 if (COMPARE_BUS(bus,k))
720 printf(" %02X", NO_PROBE_ADDR(k));
729 * do_i2c_loop() - Handle the "i2c loop" command-line command
730 * @cmdtp: Command data struct pointer
731 * @flag: Command flag
732 * @argc: Command-line argument count
733 * @argv: Array of command-line arguments
735 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
739 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
740 * {length} - Number of bytes to read
741 * {delay} - A DECIMAL number and defaults to 1000 uSec
743 static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
753 return CMD_RET_USAGE;
756 * Chip is always specified.
758 chip = simple_strtoul(argv[1], NULL, 16);
761 * Address is always specified.
763 addr = simple_strtoul(argv[2], NULL, 16);
764 alen = get_alen(argv[2]);
766 return CMD_RET_USAGE;
769 * Length is the number of objects, not number of bytes.
772 length = simple_strtoul(argv[3], NULL, 16);
773 if (length > sizeof(bytes))
774 length = sizeof(bytes);
777 * The delay time (uSec) is optional.
781 delay = simple_strtoul(argv[4], NULL, 10);
786 if (i2c_read(chip, addr, alen, bytes, length) != 0)
787 puts ("Error reading the chip.\n");
796 * The SDRAM command is separately configured because many
797 * (most?) embedded boards don't use SDRAM DIMMs.
799 * FIXME: Document and probably move elsewhere!
801 #if defined(CONFIG_CMD_SDRAM)
802 static void print_ddr2_tcyc (u_char const b)
804 printf ("%d.", (b >> 4) & 0x0F);
816 printf ("%d ns\n", b & 0x0F);
836 static void decode_bits (u_char const b, char const *str[], int const do_once)
840 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
851 * i2c sdram {i2c_chip}
853 static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
855 enum { unknown, EDO, SDRAM, DDR2 } type;
862 static const char *decode_CAS_DDR2[] = {
863 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
866 static const char *decode_CAS_default[] = {
867 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
870 static const char *decode_CS_WE_default[] = {
871 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
874 static const char *decode_byte21_default[] = {
876 " Redundant row address\n",
877 " Differential clock input\n",
878 " Registerd DQMB inputs\n",
879 " Buffered DQMB inputs\n",
881 " Registered address/control lines\n",
882 " Buffered address/control lines\n"
885 static const char *decode_byte22_DDR2[] = {
891 " Supports partial array self refresh\n",
892 " Supports 50 ohm ODT\n",
893 " Supports weak driver\n"
896 static const char *decode_row_density_DDR2[] = {
897 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
898 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
901 static const char *decode_row_density_default[] = {
902 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
903 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
907 return CMD_RET_USAGE;
910 * Chip is always specified.
912 chip = simple_strtoul (argv[1], NULL, 16);
914 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
915 puts ("No SDRAM Serial Presence Detect found.\n");
920 for (j = 0; j < 63; j++) {
923 if (cksum != data[63]) {
924 printf ("WARNING: Configuration data checksum failure:\n"
925 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
927 printf ("SPD data revision %d.%d\n",
928 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
929 printf ("Bytes used 0x%02X\n", data[0]);
930 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
932 puts ("Memory type ");
952 puts ("Row address bits ");
953 if ((data[3] & 0x00F0) == 0)
954 printf ("%d\n", data[3] & 0x0F);
956 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
958 puts ("Column address bits ");
959 if ((data[4] & 0x00F0) == 0)
960 printf ("%d\n", data[4] & 0x0F);
962 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
966 printf ("Number of ranks %d\n",
967 (data[5] & 0x07) + 1);
970 printf ("Module rows %d\n", data[5]);
976 printf ("Module data width %d bits\n", data[6]);
979 printf ("Module data width %d bits\n",
980 (data[7] << 8) | data[6]);
984 puts ("Interface signal levels ");
986 case 0: puts ("TTL 5.0 V\n"); break;
987 case 1: puts ("LVTTL\n"); break;
988 case 2: puts ("HSTL 1.5 V\n"); break;
989 case 3: puts ("SSTL 3.3 V\n"); break;
990 case 4: puts ("SSTL 2.5 V\n"); break;
991 case 5: puts ("SSTL 1.8 V\n"); break;
992 default: puts ("unknown\n"); break;
997 printf ("SDRAM cycle time ");
998 print_ddr2_tcyc (data[9]);
1001 printf ("SDRAM cycle time %d.%d ns\n",
1002 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
1008 printf ("SDRAM access time 0.%d%d ns\n",
1009 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1012 printf ("SDRAM access time %d.%d ns\n",
1013 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1017 puts ("EDC configuration ");
1019 case 0: puts ("None\n"); break;
1020 case 1: puts ("Parity\n"); break;
1021 case 2: puts ("ECC\n"); break;
1022 default: puts ("unknown\n"); break;
1025 if ((data[12] & 0x80) == 0)
1026 puts ("No self refresh, rate ");
1028 puts ("Self refresh, rate ");
1030 switch(data[12] & 0x7F) {
1031 case 0: puts ("15.625 us\n"); break;
1032 case 1: puts ("3.9 us\n"); break;
1033 case 2: puts ("7.8 us\n"); break;
1034 case 3: puts ("31.3 us\n"); break;
1035 case 4: puts ("62.5 us\n"); break;
1036 case 5: puts ("125 us\n"); break;
1037 default: puts ("unknown\n"); break;
1042 printf ("SDRAM width (primary) %d\n", data[13]);
1045 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
1046 if ((data[13] & 0x80) != 0) {
1047 printf (" (second bank) %d\n",
1048 2 * (data[13] & 0x7F));
1056 printf ("EDC width %d\n", data[14]);
1059 if (data[14] != 0) {
1060 printf ("EDC width %d\n",
1063 if ((data[14] & 0x80) != 0) {
1064 printf (" (second bank) %d\n",
1065 2 * (data[14] & 0x7F));
1072 printf ("Min clock delay, back-to-back random column addresses "
1076 puts ("Burst length(s) ");
1077 if (data[16] & 0x80) puts (" Page");
1078 if (data[16] & 0x08) puts (" 8");
1079 if (data[16] & 0x04) puts (" 4");
1080 if (data[16] & 0x02) puts (" 2");
1081 if (data[16] & 0x01) puts (" 1");
1083 printf ("Number of banks %d\n", data[17]);
1087 puts ("CAS latency(s) ");
1088 decode_bits (data[18], decode_CAS_DDR2, 0);
1092 puts ("CAS latency(s) ");
1093 decode_bits (data[18], decode_CAS_default, 0);
1099 puts ("CS latency(s) ");
1100 decode_bits (data[19], decode_CS_WE_default, 0);
1105 puts ("WE latency(s) ");
1106 decode_bits (data[20], decode_CS_WE_default, 0);
1112 puts ("Module attributes:\n");
1113 if (data[21] & 0x80)
1114 puts (" TBD (bit 7)\n");
1115 if (data[21] & 0x40)
1116 puts (" Analysis probe installed\n");
1117 if (data[21] & 0x20)
1118 puts (" TBD (bit 5)\n");
1119 if (data[21] & 0x10)
1120 puts (" FET switch external enable\n");
1121 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
1122 if (data[20] & 0x11) {
1123 printf (" %d active registers on DIMM\n",
1124 (data[21] & 0x03) + 1);
1128 puts ("Module attributes:\n");
1132 decode_bits (data[21], decode_byte21_default, 0);
1138 decode_bits (data[22], decode_byte22_DDR2, 0);
1141 puts ("Device attributes:\n");
1142 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1143 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1144 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1145 else puts (" Upper Vcc tolerance 10%\n");
1146 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1147 else puts (" Lower Vcc tolerance 10%\n");
1148 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1149 if (data[22] & 0x04) puts (" Supports precharge all\n");
1150 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1151 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1157 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1158 print_ddr2_tcyc (data[23]);
1161 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1162 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
1168 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1169 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1172 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1173 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1179 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1180 print_ddr2_tcyc (data[25]);
1183 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1184 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
1190 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1191 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1194 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1195 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1201 printf ("Minimum row precharge %d.%02d ns\n",
1202 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
1205 printf ("Minimum row precharge %d ns\n", data[27]);
1211 printf ("Row active to row active min %d.%02d ns\n",
1212 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
1215 printf ("Row active to row active min %d ns\n", data[28]);
1221 printf ("RAS to CAS delay min %d.%02d ns\n",
1222 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
1225 printf ("RAS to CAS delay min %d ns\n", data[29]);
1229 printf ("Minimum RAS pulse width %d ns\n", data[30]);
1233 puts ("Density of each row ");
1234 decode_bits (data[31], decode_row_density_DDR2, 1);
1238 puts ("Density of each row ");
1239 decode_bits (data[31], decode_row_density_default, 1);
1246 puts ("Command and Address setup ");
1247 if (data[32] >= 0xA0) {
1248 printf ("1.%d%d ns\n",
1249 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
1251 printf ("0.%d%d ns\n",
1252 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
1256 printf ("Command and Address setup %c%d.%d ns\n",
1257 (data[32] & 0x80) ? '-' : '+',
1258 (data[32] >> 4) & 0x07, data[32] & 0x0F);
1264 puts ("Command and Address hold ");
1265 if (data[33] >= 0xA0) {
1266 printf ("1.%d%d ns\n",
1267 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
1269 printf ("0.%d%d ns\n",
1270 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
1274 printf ("Command and Address hold %c%d.%d ns\n",
1275 (data[33] & 0x80) ? '-' : '+',
1276 (data[33] >> 4) & 0x07, data[33] & 0x0F);
1282 printf ("Data signal input setup 0.%d%d ns\n",
1283 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
1286 printf ("Data signal input setup %c%d.%d ns\n",
1287 (data[34] & 0x80) ? '-' : '+',
1288 (data[34] >> 4) & 0x07, data[34] & 0x0F);
1294 printf ("Data signal input hold 0.%d%d ns\n",
1295 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
1298 printf ("Data signal input hold %c%d.%d ns\n",
1299 (data[35] & 0x80) ? '-' : '+',
1300 (data[35] >> 4) & 0x07, data[35] & 0x0F);
1304 puts ("Manufacturer's JEDEC ID ");
1305 for (j = 64; j <= 71; j++)
1306 printf ("%02X ", data[j]);
1308 printf ("Manufacturing Location %02X\n", data[72]);
1309 puts ("Manufacturer's Part Number ");
1310 for (j = 73; j <= 90; j++)
1311 printf ("%02X ", data[j]);
1313 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1314 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
1315 puts ("Assembly Serial Number ");
1316 for (j = 95; j <= 98; j++)
1317 printf ("%02X ", data[j]);
1321 printf ("Speed rating PC%d\n",
1322 data[126] == 0x66 ? 66 : data[126]);
1330 * i2c edid {i2c_chip}
1332 #if defined(CONFIG_I2C_EDID)
1333 int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1336 struct edid1_info edid;
1343 chip = simple_strtoul(argv[1], NULL, 16);
1344 if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1345 puts("Error reading EDID content.\n");
1349 if (edid_check_info(&edid)) {
1350 puts("Content isn't valid EDID.\n");
1354 edid_print_info(&edid);
1358 #endif /* CONFIG_I2C_EDID */
1360 #if defined(CONFIG_I2C_MUX)
1362 * do_i2c_add_bus() - Handle the "i2c bus" command-line command
1363 * @cmdtp: Command data struct pointer
1364 * @flag: Command flag
1365 * @argc: Command-line argument count
1366 * @argv: Array of command-line arguments
1368 * Returns zero always.
1370 static int do_i2c_add_bus(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1375 /* show all busses */
1377 I2C_MUX_DEVICE *device = i2c_mux_devices;
1379 printf ("Busses reached over muxes:\n");
1380 while (device != NULL) {
1381 printf ("Bus ID: %x\n", device->busid);
1382 printf (" reached over Mux(es):\n");
1384 while (mux != NULL) {
1385 printf (" %s@%x ch: %x\n", mux->name, mux->chip, mux->channel);
1388 device = device->next;
1391 (void)i2c_mux_ident_muxstring ((uchar *)argv[1]);
1396 #endif /* CONFIG_I2C_MUX */
1398 #if defined(CONFIG_I2C_MULTI_BUS)
1400 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1401 * @cmdtp: Command data struct pointer
1402 * @flag: Command flag
1403 * @argc: Command-line argument count
1404 * @argv: Array of command-line arguments
1406 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1409 static int do_i2c_bus_num(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1414 /* querying current setting */
1415 printf("Current bus is %d\n", i2c_get_bus_num());
1417 bus_idx = simple_strtoul(argv[1], NULL, 10);
1418 printf("Setting bus to %d\n", bus_idx);
1419 ret = i2c_set_bus_num(bus_idx);
1421 printf("Failure changing bus number (%d)\n", ret);
1425 #endif /* CONFIG_I2C_MULTI_BUS */
1428 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1429 * @cmdtp: Command data struct pointer
1430 * @flag: Command flag
1431 * @argc: Command-line argument count
1432 * @argv: Array of command-line arguments
1434 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1437 static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1442 /* querying current speed */
1443 printf("Current bus speed=%d\n", i2c_get_bus_speed());
1445 speed = simple_strtoul(argv[1], NULL, 10);
1446 printf("Setting bus speed to %d Hz\n", speed);
1447 ret = i2c_set_bus_speed(speed);
1449 printf("Failure changing bus speed (%d)\n", ret);
1455 * do_i2c_mm() - Handle the "i2c mm" command-line command
1456 * @cmdtp: Command data struct pointer
1457 * @flag: Command flag
1458 * @argc: Command-line argument count
1459 * @argv: Array of command-line arguments
1461 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1464 static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1466 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1470 * do_i2c_nm() - Handle the "i2c nm" command-line command
1471 * @cmdtp: Command data struct pointer
1472 * @flag: Command flag
1473 * @argc: Command-line argument count
1474 * @argv: Array of command-line arguments
1476 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1479 static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1481 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1485 * do_i2c_reset() - Handle the "i2c reset" 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 always.
1493 static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1495 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
1499 static cmd_tbl_t cmd_i2c_sub[] = {
1500 #if defined(CONFIG_I2C_MUX)
1501 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_add_bus, "", ""),
1502 #endif /* CONFIG_I2C_MUX */
1503 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
1504 #if defined(CONFIG_I2C_MULTI_BUS)
1505 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1506 #endif /* CONFIG_I2C_MULTI_BUS */
1507 #if defined(CONFIG_I2C_EDID)
1508 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1509 #endif /* CONFIG_I2C_EDID */
1510 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1511 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1512 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1513 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1514 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1515 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
1516 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
1517 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
1518 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1519 #if defined(CONFIG_CMD_SDRAM)
1520 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1522 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1525 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1526 void i2c_reloc(void) {
1527 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1532 * do_i2c() - Handle the "i2c" command-line command
1533 * @cmdtp: Command data struct pointer
1534 * @flag: Command flag
1535 * @argc: Command-line argument count
1536 * @argv: Array of command-line arguments
1538 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1541 static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1546 return CMD_RET_USAGE;
1548 /* Strip off leading 'i2c' command argument */
1552 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1555 return c->cmd(cmdtp, flag, argc, argv);
1557 return CMD_RET_USAGE;
1560 /***************************************************/
1561 #ifdef CONFIG_SYS_LONGHELP
1562 static char i2c_help_text[] =
1563 #if defined(CONFIG_I2C_MUX)
1564 "bus [muxtype:muxaddr:muxchannel] - add a new bus reached over muxes\ni2c "
1565 #endif /* CONFIG_I2C_MUX */
1566 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
1567 #if defined(CONFIG_I2C_MULTI_BUS)
1568 "i2c dev [dev] - show or set current I2C bus\n"
1569 #endif /* CONFIG_I2C_MULTI_BUS */
1570 #if defined(CONFIG_I2C_EDID)
1571 "i2c edid chip - print EDID configuration information\n"
1572 #endif /* CONFIG_I2C_EDID */
1573 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
1574 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1575 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1576 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1577 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
1578 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
1579 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
1580 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
1581 "i2c reset - re-init the I2C Controller\n"
1582 #if defined(CONFIG_CMD_SDRAM)
1583 "i2c sdram chip - print SDRAM configuration information\n"
1585 "i2c speed [speed] - show or set I2C bus speed";
1594 #if defined(CONFIG_I2C_MUX)
1595 static int i2c_mux_add_device(I2C_MUX_DEVICE *dev)
1597 I2C_MUX_DEVICE *devtmp = i2c_mux_devices;
1599 if (i2c_mux_devices == NULL) {
1600 i2c_mux_devices = dev;
1603 while (devtmp->next != NULL)
1604 devtmp = devtmp->next;
1610 I2C_MUX_DEVICE *i2c_mux_search_device(int id)
1612 I2C_MUX_DEVICE *device = i2c_mux_devices;
1614 while (device != NULL) {
1615 if (device->busid == id)
1617 device = device->next;
1622 /* searches in the buf from *pos the next ':'.
1624 * 0 if found (with *pos = where)
1625 * < 0 if an error occured
1626 * > 0 if the end of buf is reached
1628 static int i2c_mux_search_next (int *pos, uchar *buf, int len)
1630 while ((buf[*pos] != ':') && (*pos < len)) {
1635 if (buf[*pos] != ':')
1640 static int i2c_mux_get_busid (void)
1642 int tmp = i2c_mux_busid;
1648 /* Analyses a Muxstring and immediately sends the
1649 commands to the muxes. Runs from flash.
1651 int i2c_mux_ident_muxstring_f (uchar *buf)
1656 int len = strlen((char *)buf);
1664 ret = i2c_mux_search_next(&pos, buf, len);
1667 /* search address */
1670 ret = i2c_mux_search_next(&pos, buf, len);
1674 chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1676 /* search channel */
1679 ret = i2c_mux_search_next(&pos, buf, len);
1683 if (buf[pos] != 0) {
1687 channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1690 if (i2c_write(chip, 0, 0, &channel, 1) != 0) {
1691 printf ("Error setting Mux: chip:%x channel: \
1692 %x\n", chip, channel);
1704 /* Analyses a Muxstring and if this String is correct
1705 * adds a new I2C Bus.
1707 I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf)
1709 I2C_MUX_DEVICE *device;
1714 int len = strlen((char *)buf);
1717 device = (I2C_MUX_DEVICE *)malloc (sizeof(I2C_MUX_DEVICE));
1719 device->busid = i2c_mux_get_busid ();
1720 device->next = NULL;
1722 mux = (I2C_MUX *)malloc (sizeof(I2C_MUX));
1724 /* search name of mux */
1726 ret = i2c_mux_search_next(&pos, buf, len);
1728 printf ("%s no name.\n", __FUNCTION__);
1729 mux->name = (char *)malloc (pos - oldpos + 1);
1730 memcpy (mux->name, &buf[oldpos], pos - oldpos);
1731 mux->name[pos - oldpos] = 0;
1732 /* search address */
1735 ret = i2c_mux_search_next(&pos, buf, len);
1737 printf ("%s no mux address.\n", __FUNCTION__);
1739 mux->chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1741 /* search channel */
1744 ret = i2c_mux_search_next(&pos, buf, len);
1746 printf ("%s no mux channel.\n", __FUNCTION__);
1748 if (buf[pos] != 0) {
1752 mux->channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1755 if (device->mux == NULL)
1758 I2C_MUX *muxtmp = device->mux;
1759 while (muxtmp->next != NULL) {
1760 muxtmp = muxtmp->next;
1769 i2c_mux_add_device (device);
1776 int i2x_mux_select_mux(int bus)
1778 I2C_MUX_DEVICE *dev;
1781 if ((gd->flags & GD_FLG_RELOC) != GD_FLG_RELOC) {
1782 /* select Default Mux Bus */
1783 #if defined(CONFIG_SYS_I2C_IVM_BUS)
1784 i2c_mux_ident_muxstring_f ((uchar *)CONFIG_SYS_I2C_IVM_BUS);
1788 buf = (unsigned char *) getenv("EEprom_ivm");
1790 i2c_mux_ident_muxstring_f (buf);
1795 dev = i2c_mux_search_device(bus);
1800 while (mux != NULL) {
1801 /* do deblocking on each level of mux, before mux config */
1803 if (i2c_write(mux->chip, 0, 0, &mux->channel, 1) != 0) {
1804 printf ("Error setting Mux: chip:%x channel: \
1805 %x\n", mux->chip, mux->channel);
1810 /* do deblocking on each level of mux and after mux config */
1814 #endif /* CONFIG_I2C_MUX */