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 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * I2C Functions similar to the standard memory functions.
32 * There are several parameters in many of the commands that bear further
35 * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
36 * Each I2C chip on the bus has a unique address. On the I2C data bus,
37 * the address is the upper seven bits and the LSB is the "read/write"
38 * bit. Note that the {i2c_chip} address specified on the command
39 * line is not shifted up: e.g. a typical EEPROM memory chip may have
40 * an I2C address of 0x50, but the data put on the bus will be 0xA0
41 * for write and 0xA1 for read. This "non shifted" address notation
42 * matches at least half of the data sheets :-/.
44 * {addr} is the address (or offset) within the chip. Small memory
45 * chips have 8 bit addresses. Large memory chips have 16 bit
46 * addresses. Other memory chips have 9, 10, or 11 bit addresses.
47 * Many non-memory chips have multiple registers and {addr} is used
48 * as the register index. Some non-memory chips have only one register
49 * and therefore don't need any {addr} parameter.
51 * The default {addr} parameter is one byte (.1) which works well for
52 * memories and registers with 8 bits of address space.
54 * You can specify the length of the {addr} field with the optional .0,
55 * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
56 * manipulating a single register device which doesn't use an address
57 * field, use "0.0" for the address and the ".0" length field will
58 * suppress the address in the I2C data stream. This also works for
59 * successive reads using the I2C auto-incrementing memory pointer.
61 * If you are manipulating a large memory with 2-byte addresses, use
62 * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
64 * Then there are the unfortunate memory chips that spill the most
65 * significant 1, 2, or 3 bits of address into the chip address byte.
66 * This effectively makes one chip (logically) look like 2, 4, or
67 * 8 chips. This is handled (awkwardly) by #defining
68 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
69 * {addr} field (since .1 is the default, it doesn't actually have to
70 * be specified). Examples: given a memory chip at I2C chip address
71 * 0x50, the following would happen...
72 * i2c md 50 0 10 display 16 bytes starting at 0x000
73 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
74 * i2c md 50 100 10 display 16 bytes starting at 0x100
75 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
76 * i2c md 50 210 10 display 16 bytes starting at 0x210
77 * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
78 * This is awfully ugly. It would be nice if someone would think up
79 * a better way of handling this.
81 * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
87 #include <environment.h>
90 #include <asm/byteorder.h>
91 #include <linux/compiler.h>
93 DECLARE_GLOBAL_DATA_PTR;
95 /* Display values from last command.
96 * Memory modify remembered values are different from display memory.
98 static uchar i2c_dp_last_chip;
99 static uint i2c_dp_last_addr;
100 static uint i2c_dp_last_alen;
101 static uint i2c_dp_last_length = 0x10;
103 static uchar i2c_mm_last_chip;
104 static uint i2c_mm_last_addr;
105 static uint i2c_mm_last_alen;
107 /* If only one I2C bus is present, the list of devices to ignore when
108 * the probe command is issued is represented by a 1D array of addresses.
109 * When multiple buses are present, the list is an array of bus-address
110 * pairs. The following macros take care of this */
112 #if defined(CONFIG_SYS_I2C_NOPROBES)
113 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MUX) || \
114 defined(CONFIG_I2C_MULTI_BUS)
119 } i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
120 #define GET_BUS_NUM i2c_get_bus_num()
121 #define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
122 #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
123 #define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
124 #else /* single bus */
125 static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
126 #define GET_BUS_NUM 0
127 #define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
128 #define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
129 #define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
130 #endif /* defined(CONFIG_SYS_I2C) */
132 #define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
135 #if defined(CONFIG_I2C_MUX)
136 static I2C_MUX_DEVICE *i2c_mux_devices = NULL;
137 static int i2c_mux_busid = CONFIG_SYS_MAX_I2C_BUS;
140 #define DISP_LINE_LEN 16
143 * i2c_init_board() - Board-specific I2C bus init
145 * This function is the default no-op implementation of I2C bus
146 * initialization. This function can be overriden by board-specific
147 * implementation if needed.
150 void i2c_init_board(void)
154 /* TODO: Implement architecture-specific get/set functions */
157 * i2c_get_bus_speed() - Return I2C bus speed
159 * This function is the default implementation of function for retrieveing
160 * the current I2C bus speed in Hz.
162 * A driver implementing runtime switching of I2C bus speed must override
163 * this function to report the speed correctly. Simple or legacy drivers
164 * can use this fallback.
166 * Returns I2C bus speed in Hz.
168 #if !defined(CONFIG_SYS_I2C)
170 * TODO: Implement architecture-specific get/set functions
171 * Should go away, if we switched completely to new multibus support
174 unsigned int i2c_get_bus_speed(void)
176 return CONFIG_SYS_I2C_SPEED;
180 * i2c_set_bus_speed() - Configure I2C bus speed
181 * @speed: Newly set speed of the I2C bus in Hz
183 * This function is the default implementation of function for setting
184 * the I2C bus speed in Hz.
186 * A driver implementing runtime switching of I2C bus speed must override
187 * this function to report the speed correctly. Simple or legacy drivers
188 * can use this fallback.
190 * Returns zero on success, negative value on error.
193 int i2c_set_bus_speed(unsigned int speed)
195 if (speed != CONFIG_SYS_I2C_SPEED)
203 * get_alen() - Small parser helper function to get address length
205 * Returns the address length.
207 static uint get_alen(char *arg)
213 for (j = 0; j < 8; j++) {
215 alen = arg[j+1] - '0';
217 } else if (arg[j] == '\0')
224 * do_i2c_read() - Handle the "i2c read" command-line command
225 * @cmdtp: Command data struct pointer
226 * @flag: Command flag
227 * @argc: Command-line argument count
228 * @argv: Array of command-line arguments
230 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
234 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
236 static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
239 uint devaddr, alen, length;
243 return CMD_RET_USAGE;
248 chip = simple_strtoul(argv[1], NULL, 16);
251 * I2C data address within the chip. This can be 1 or
252 * 2 bytes long. Some day it might be 3 bytes long :-).
254 devaddr = simple_strtoul(argv[2], NULL, 16);
255 alen = get_alen(argv[2]);
257 return CMD_RET_USAGE;
260 * Length is the number of objects, not number of bytes.
262 length = simple_strtoul(argv[3], NULL, 16);
265 * memaddr is the address where to store things in memory
267 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
269 if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
270 puts ("Error reading the chip.\n");
276 static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
279 uint devaddr, alen, length;
283 return cmd_usage(cmdtp);
286 * memaddr is the address where to store things in memory
288 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
293 chip = simple_strtoul(argv[2], NULL, 16);
296 * I2C data address within the chip. This can be 1 or
297 * 2 bytes long. Some day it might be 3 bytes long :-).
299 devaddr = simple_strtoul(argv[3], NULL, 16);
300 alen = get_alen(argv[3]);
302 return cmd_usage(cmdtp);
305 * Length is the number of objects, not number of bytes.
307 length = simple_strtoul(argv[4], NULL, 16);
309 while (length-- > 0) {
310 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
311 puts("Error writing to the chip.\n");
315 * No write delay with FRAM devices.
317 #if !defined(CONFIG_SYS_I2C_FRAM)
325 * do_i2c_md() - Handle the "i2c md" command-line command
326 * @cmdtp: Command data struct pointer
327 * @flag: Command flag
328 * @argc: Command-line argument count
329 * @argv: Array of command-line arguments
331 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
335 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
337 static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
340 uint addr, alen, length;
341 int j, nbytes, linebytes;
343 /* We use the last specified parameters, unless new ones are
346 chip = i2c_dp_last_chip;
347 addr = i2c_dp_last_addr;
348 alen = i2c_dp_last_alen;
349 length = i2c_dp_last_length;
352 return CMD_RET_USAGE;
354 if ((flag & CMD_FLAG_REPEAT) == 0) {
356 * New command specified.
362 chip = simple_strtoul(argv[1], NULL, 16);
365 * I2C data address within the chip. This can be 1 or
366 * 2 bytes long. Some day it might be 3 bytes long :-).
368 addr = simple_strtoul(argv[2], NULL, 16);
369 alen = get_alen(argv[2]);
371 return CMD_RET_USAGE;
374 * If another parameter, it is the length to display.
375 * Length is the number of objects, not number of bytes.
378 length = simple_strtoul(argv[3], NULL, 16);
384 * We buffer all read data, so we can make sure data is read only
389 unsigned char linebuf[DISP_LINE_LEN];
392 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
394 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
395 puts ("Error reading the chip.\n");
397 printf("%04x:", addr);
399 for (j=0; j<linebytes; j++) {
400 printf(" %02x", *cp++);
405 for (j=0; j<linebytes; j++) {
406 if ((*cp < 0x20) || (*cp > 0x7e))
415 } while (nbytes > 0);
417 i2c_dp_last_chip = chip;
418 i2c_dp_last_addr = addr;
419 i2c_dp_last_alen = alen;
420 i2c_dp_last_length = length;
426 * do_i2c_mw() - Handle the "i2c mw" command-line command
427 * @cmdtp: Command data struct pointer
428 * @flag: Command flag
429 * @argc: Command-line argument count
430 * @argv: Array of command-line arguments
432 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
436 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
438 static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
446 if ((argc < 4) || (argc > 5))
447 return CMD_RET_USAGE;
450 * Chip is always specified.
452 chip = simple_strtoul(argv[1], NULL, 16);
455 * Address is always specified.
457 addr = simple_strtoul(argv[2], NULL, 16);
458 alen = get_alen(argv[2]);
460 return CMD_RET_USAGE;
463 * Value to write is always specified.
465 byte = simple_strtoul(argv[3], NULL, 16);
471 count = simple_strtoul(argv[4], NULL, 16);
475 while (count-- > 0) {
476 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
477 puts ("Error writing the chip.\n");
479 * Wait for the write to complete. The write can take
480 * up to 10mSec (we allow a little more time).
483 * No write delay with FRAM devices.
485 #if !defined(CONFIG_SYS_I2C_FRAM)
494 * do_i2c_crc() - Handle the "i2c crc32" command-line command
495 * @cmdtp: Command data struct pointer
496 * @flag: Command flag
497 * @argc: Command-line argument count
498 * @argv: Array of command-line arguments
500 * Calculate a CRC on memory
502 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
506 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
508 static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
519 return CMD_RET_USAGE;
522 * Chip is always specified.
524 chip = simple_strtoul(argv[1], NULL, 16);
527 * Address is always specified.
529 addr = simple_strtoul(argv[2], NULL, 16);
530 alen = get_alen(argv[2]);
532 return CMD_RET_USAGE;
535 * Count is always specified
537 count = simple_strtoul(argv[3], NULL, 16);
539 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
541 * CRC a byte at a time. This is going to be slooow, but hey, the
542 * memories are small and slow too so hopefully nobody notices.
546 while (count-- > 0) {
547 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
549 crc = crc32 (crc, &byte, 1);
553 puts ("Error reading the chip,\n");
555 printf ("%08lx\n", crc);
561 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
562 * @cmdtp: Command data struct pointer
563 * @flag: Command flag
564 * @argc: Command-line argument count
565 * @argv: Array of command-line arguments
569 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
573 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
574 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
577 mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
587 return CMD_RET_USAGE;
589 #ifdef CONFIG_BOOT_RETRY_TIME
590 reset_cmd_timeout(); /* got a good command to get here */
593 * We use the last specified parameters, unless new ones are
596 chip = i2c_mm_last_chip;
597 addr = i2c_mm_last_addr;
598 alen = i2c_mm_last_alen;
600 if ((flag & CMD_FLAG_REPEAT) == 0) {
602 * New command specified. Check for a size specification.
603 * Defaults to byte if no or incorrect specification.
605 size = cmd_get_data_size(argv[0], 1);
608 * Chip is always specified.
610 chip = simple_strtoul(argv[1], NULL, 16);
613 * Address is always specified.
615 addr = simple_strtoul(argv[2], NULL, 16);
616 alen = get_alen(argv[2]);
618 return CMD_RET_USAGE;
622 * Print the address, followed by value. Then accept input for
623 * the next value. A non-converted value exits.
626 printf("%08lx:", addr);
627 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
628 puts ("\nError reading the chip,\n");
630 data = cpu_to_be32(data);
632 printf(" %02lx", (data >> 24) & 0x000000FF);
634 printf(" %04lx", (data >> 16) & 0x0000FFFF);
636 printf(" %08lx", data);
639 nbytes = readline (" ? ");
642 * <CR> pressed as only input, don't modify current
643 * location and move to next.
648 #ifdef CONFIG_BOOT_RETRY_TIME
649 reset_cmd_timeout(); /* good enough to not time out */
652 #ifdef CONFIG_BOOT_RETRY_TIME
653 else if (nbytes == -2)
654 break; /* timed out, exit the command */
659 data = simple_strtoul(console_buffer, &endp, 16);
664 data = be32_to_cpu(data);
665 nbytes = endp - console_buffer;
667 #ifdef CONFIG_BOOT_RETRY_TIME
669 * good enough to not time out
673 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
674 puts ("Error writing the chip.\n");
675 #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
676 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
684 i2c_mm_last_chip = chip;
685 i2c_mm_last_addr = addr;
686 i2c_mm_last_alen = alen;
692 * do_i2c_probe() - Handle the "i2c probe" command-line command
693 * @cmdtp: Command data struct pointer
694 * @flag: Command flag
695 * @argc: Command-line argument count
696 * @argv: Array of command-line arguments
698 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
704 * Returns zero (success) if one or more I2C devices was found
706 static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
711 #if defined(CONFIG_SYS_I2C_NOPROBES)
713 unsigned int bus = GET_BUS_NUM;
714 #endif /* NOPROBES */
717 addr = simple_strtol(argv[1], 0, 16);
719 puts ("Valid chip addresses:");
720 for (j = 0; j < 128; j++) {
721 if ((0 <= addr) && (j != addr))
724 #if defined(CONFIG_SYS_I2C_NOPROBES)
726 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
727 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
735 if (i2c_probe(j) == 0) {
742 #if defined(CONFIG_SYS_I2C_NOPROBES)
743 puts ("Excluded chip addresses:");
744 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
745 if (COMPARE_BUS(bus,k))
746 printf(" %02X", NO_PROBE_ADDR(k));
755 * do_i2c_loop() - Handle the "i2c loop" command-line command
756 * @cmdtp: Command data struct pointer
757 * @flag: Command flag
758 * @argc: Command-line argument count
759 * @argv: Array of command-line arguments
761 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
765 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
766 * {length} - Number of bytes to read
767 * {delay} - A DECIMAL number and defaults to 1000 uSec
769 static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
779 return CMD_RET_USAGE;
782 * Chip is always specified.
784 chip = simple_strtoul(argv[1], NULL, 16);
787 * Address is always specified.
789 addr = simple_strtoul(argv[2], NULL, 16);
790 alen = get_alen(argv[2]);
792 return CMD_RET_USAGE;
795 * Length is the number of objects, not number of bytes.
798 length = simple_strtoul(argv[3], NULL, 16);
799 if (length > sizeof(bytes))
800 length = sizeof(bytes);
803 * The delay time (uSec) is optional.
807 delay = simple_strtoul(argv[4], NULL, 10);
812 if (i2c_read(chip, addr, alen, bytes, length) != 0)
813 puts ("Error reading the chip.\n");
822 * The SDRAM command is separately configured because many
823 * (most?) embedded boards don't use SDRAM DIMMs.
825 * FIXME: Document and probably move elsewhere!
827 #if defined(CONFIG_CMD_SDRAM)
828 static void print_ddr2_tcyc (u_char const b)
830 printf ("%d.", (b >> 4) & 0x0F);
842 printf ("%d ns\n", b & 0x0F);
862 static void decode_bits (u_char const b, char const *str[], int const do_once)
866 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
877 * i2c sdram {i2c_chip}
879 static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
881 enum { unknown, EDO, SDRAM, DDR2 } type;
888 static const char *decode_CAS_DDR2[] = {
889 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
892 static const char *decode_CAS_default[] = {
893 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
896 static const char *decode_CS_WE_default[] = {
897 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
900 static const char *decode_byte21_default[] = {
902 " Redundant row address\n",
903 " Differential clock input\n",
904 " Registerd DQMB inputs\n",
905 " Buffered DQMB inputs\n",
907 " Registered address/control lines\n",
908 " Buffered address/control lines\n"
911 static const char *decode_byte22_DDR2[] = {
917 " Supports partial array self refresh\n",
918 " Supports 50 ohm ODT\n",
919 " Supports weak driver\n"
922 static const char *decode_row_density_DDR2[] = {
923 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
924 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
927 static const char *decode_row_density_default[] = {
928 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
929 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
933 return CMD_RET_USAGE;
936 * Chip is always specified.
938 chip = simple_strtoul (argv[1], NULL, 16);
940 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
941 puts ("No SDRAM Serial Presence Detect found.\n");
946 for (j = 0; j < 63; j++) {
949 if (cksum != data[63]) {
950 printf ("WARNING: Configuration data checksum failure:\n"
951 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
953 printf ("SPD data revision %d.%d\n",
954 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
955 printf ("Bytes used 0x%02X\n", data[0]);
956 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
958 puts ("Memory type ");
978 puts ("Row address bits ");
979 if ((data[3] & 0x00F0) == 0)
980 printf ("%d\n", data[3] & 0x0F);
982 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
984 puts ("Column address bits ");
985 if ((data[4] & 0x00F0) == 0)
986 printf ("%d\n", data[4] & 0x0F);
988 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
992 printf ("Number of ranks %d\n",
993 (data[5] & 0x07) + 1);
996 printf ("Module rows %d\n", data[5]);
1002 printf ("Module data width %d bits\n", data[6]);
1005 printf ("Module data width %d bits\n",
1006 (data[7] << 8) | data[6]);
1010 puts ("Interface signal levels ");
1012 case 0: puts ("TTL 5.0 V\n"); break;
1013 case 1: puts ("LVTTL\n"); break;
1014 case 2: puts ("HSTL 1.5 V\n"); break;
1015 case 3: puts ("SSTL 3.3 V\n"); break;
1016 case 4: puts ("SSTL 2.5 V\n"); break;
1017 case 5: puts ("SSTL 1.8 V\n"); break;
1018 default: puts ("unknown\n"); break;
1023 printf ("SDRAM cycle time ");
1024 print_ddr2_tcyc (data[9]);
1027 printf ("SDRAM cycle time %d.%d ns\n",
1028 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
1034 printf ("SDRAM access time 0.%d%d ns\n",
1035 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1038 printf ("SDRAM access time %d.%d ns\n",
1039 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1043 puts ("EDC configuration ");
1045 case 0: puts ("None\n"); break;
1046 case 1: puts ("Parity\n"); break;
1047 case 2: puts ("ECC\n"); break;
1048 default: puts ("unknown\n"); break;
1051 if ((data[12] & 0x80) == 0)
1052 puts ("No self refresh, rate ");
1054 puts ("Self refresh, rate ");
1056 switch(data[12] & 0x7F) {
1057 case 0: puts ("15.625 us\n"); break;
1058 case 1: puts ("3.9 us\n"); break;
1059 case 2: puts ("7.8 us\n"); break;
1060 case 3: puts ("31.3 us\n"); break;
1061 case 4: puts ("62.5 us\n"); break;
1062 case 5: puts ("125 us\n"); break;
1063 default: puts ("unknown\n"); break;
1068 printf ("SDRAM width (primary) %d\n", data[13]);
1071 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
1072 if ((data[13] & 0x80) != 0) {
1073 printf (" (second bank) %d\n",
1074 2 * (data[13] & 0x7F));
1082 printf ("EDC width %d\n", data[14]);
1085 if (data[14] != 0) {
1086 printf ("EDC width %d\n",
1089 if ((data[14] & 0x80) != 0) {
1090 printf (" (second bank) %d\n",
1091 2 * (data[14] & 0x7F));
1098 printf ("Min clock delay, back-to-back random column addresses "
1102 puts ("Burst length(s) ");
1103 if (data[16] & 0x80) puts (" Page");
1104 if (data[16] & 0x08) puts (" 8");
1105 if (data[16] & 0x04) puts (" 4");
1106 if (data[16] & 0x02) puts (" 2");
1107 if (data[16] & 0x01) puts (" 1");
1109 printf ("Number of banks %d\n", data[17]);
1113 puts ("CAS latency(s) ");
1114 decode_bits (data[18], decode_CAS_DDR2, 0);
1118 puts ("CAS latency(s) ");
1119 decode_bits (data[18], decode_CAS_default, 0);
1125 puts ("CS latency(s) ");
1126 decode_bits (data[19], decode_CS_WE_default, 0);
1131 puts ("WE latency(s) ");
1132 decode_bits (data[20], decode_CS_WE_default, 0);
1138 puts ("Module attributes:\n");
1139 if (data[21] & 0x80)
1140 puts (" TBD (bit 7)\n");
1141 if (data[21] & 0x40)
1142 puts (" Analysis probe installed\n");
1143 if (data[21] & 0x20)
1144 puts (" TBD (bit 5)\n");
1145 if (data[21] & 0x10)
1146 puts (" FET switch external enable\n");
1147 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
1148 if (data[20] & 0x11) {
1149 printf (" %d active registers on DIMM\n",
1150 (data[21] & 0x03) + 1);
1154 puts ("Module attributes:\n");
1158 decode_bits (data[21], decode_byte21_default, 0);
1164 decode_bits (data[22], decode_byte22_DDR2, 0);
1167 puts ("Device attributes:\n");
1168 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1169 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1170 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1171 else puts (" Upper Vcc tolerance 10%\n");
1172 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1173 else puts (" Lower Vcc tolerance 10%\n");
1174 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1175 if (data[22] & 0x04) puts (" Supports precharge all\n");
1176 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1177 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1183 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1184 print_ddr2_tcyc (data[23]);
1187 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1188 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
1194 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1195 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1198 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1199 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1205 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1206 print_ddr2_tcyc (data[25]);
1209 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1210 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
1216 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1217 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1220 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1221 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1227 printf ("Minimum row precharge %d.%02d ns\n",
1228 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
1231 printf ("Minimum row precharge %d ns\n", data[27]);
1237 printf ("Row active to row active min %d.%02d ns\n",
1238 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
1241 printf ("Row active to row active min %d ns\n", data[28]);
1247 printf ("RAS to CAS delay min %d.%02d ns\n",
1248 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
1251 printf ("RAS to CAS delay min %d ns\n", data[29]);
1255 printf ("Minimum RAS pulse width %d ns\n", data[30]);
1259 puts ("Density of each row ");
1260 decode_bits (data[31], decode_row_density_DDR2, 1);
1264 puts ("Density of each row ");
1265 decode_bits (data[31], decode_row_density_default, 1);
1272 puts ("Command and Address setup ");
1273 if (data[32] >= 0xA0) {
1274 printf ("1.%d%d ns\n",
1275 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
1277 printf ("0.%d%d ns\n",
1278 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
1282 printf ("Command and Address setup %c%d.%d ns\n",
1283 (data[32] & 0x80) ? '-' : '+',
1284 (data[32] >> 4) & 0x07, data[32] & 0x0F);
1290 puts ("Command and Address hold ");
1291 if (data[33] >= 0xA0) {
1292 printf ("1.%d%d ns\n",
1293 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
1295 printf ("0.%d%d ns\n",
1296 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
1300 printf ("Command and Address hold %c%d.%d ns\n",
1301 (data[33] & 0x80) ? '-' : '+',
1302 (data[33] >> 4) & 0x07, data[33] & 0x0F);
1308 printf ("Data signal input setup 0.%d%d ns\n",
1309 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
1312 printf ("Data signal input setup %c%d.%d ns\n",
1313 (data[34] & 0x80) ? '-' : '+',
1314 (data[34] >> 4) & 0x07, data[34] & 0x0F);
1320 printf ("Data signal input hold 0.%d%d ns\n",
1321 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
1324 printf ("Data signal input hold %c%d.%d ns\n",
1325 (data[35] & 0x80) ? '-' : '+',
1326 (data[35] >> 4) & 0x07, data[35] & 0x0F);
1330 puts ("Manufacturer's JEDEC ID ");
1331 for (j = 64; j <= 71; j++)
1332 printf ("%02X ", data[j]);
1334 printf ("Manufacturing Location %02X\n", data[72]);
1335 puts ("Manufacturer's Part Number ");
1336 for (j = 73; j <= 90; j++)
1337 printf ("%02X ", data[j]);
1339 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1340 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
1341 puts ("Assembly Serial Number ");
1342 for (j = 95; j <= 98; j++)
1343 printf ("%02X ", data[j]);
1347 printf ("Speed rating PC%d\n",
1348 data[126] == 0x66 ? 66 : data[126]);
1356 * i2c edid {i2c_chip}
1358 #if defined(CONFIG_I2C_EDID)
1359 int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1362 struct edid1_info edid;
1369 chip = simple_strtoul(argv[1], NULL, 16);
1370 if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1371 puts("Error reading EDID content.\n");
1375 if (edid_check_info(&edid)) {
1376 puts("Content isn't valid EDID.\n");
1380 edid_print_info(&edid);
1384 #endif /* CONFIG_I2C_EDID */
1387 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
1388 * @cmdtp: Command data struct pointer
1389 * @flag: Command flag
1390 * @argc: Command-line argument count
1391 * @argv: Array of command-line arguments
1393 * Returns zero always.
1395 #if defined(CONFIG_SYS_I2C)
1396 int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1399 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1404 /* show all busses */
1405 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1406 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1407 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1408 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1409 if (i2c_bus[i].next_hop[j].chip == 0)
1411 printf("->%s@0x%2x:%d",
1412 i2c_bus[i].next_hop[j].mux.name,
1413 i2c_bus[i].next_hop[j].chip,
1414 i2c_bus[i].next_hop[j].channel);
1420 /* show specific bus */
1421 i = simple_strtoul(argv[1], NULL, 10);
1422 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1423 printf("Invalid bus %d\n", i);
1426 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1427 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1428 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1429 if (i2c_bus[i].next_hop[j].chip == 0)
1431 printf("->%s@0x%2x:%d",
1432 i2c_bus[i].next_hop[j].mux.name,
1433 i2c_bus[i].next_hop[j].chip,
1434 i2c_bus[i].next_hop[j].channel);
1445 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1446 * @cmdtp: Command data struct pointer
1447 * @flag: Command flag
1448 * @argc: Command-line argument count
1449 * @argv: Array of command-line arguments
1451 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1454 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
1455 int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1458 unsigned int bus_no;
1461 /* querying current setting */
1462 printf("Current bus is %d\n", i2c_get_bus_num());
1464 bus_no = simple_strtoul(argv[1], NULL, 10);
1465 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1466 printf("Invalid bus %d\n", bus_no);
1469 printf("Setting bus to %d\n", bus_no);
1470 ret = i2c_set_bus_num(bus_no);
1472 printf("Failure changing bus number (%d)\n", ret);
1476 #endif /* defined(CONFIG_SYS_I2C) */
1479 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1480 * @cmdtp: Command data struct pointer
1481 * @flag: Command flag
1482 * @argc: Command-line argument count
1483 * @argv: Array of command-line arguments
1485 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1488 static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1493 /* querying current speed */
1494 printf("Current bus speed=%d\n", i2c_get_bus_speed());
1496 speed = simple_strtoul(argv[1], NULL, 10);
1497 printf("Setting bus speed to %d Hz\n", speed);
1498 ret = i2c_set_bus_speed(speed);
1500 printf("Failure changing bus speed (%d)\n", ret);
1506 * do_i2c_mm() - Handle the "i2c mm" command-line command
1507 * @cmdtp: Command data struct pointer
1508 * @flag: Command flag
1509 * @argc: Command-line argument count
1510 * @argv: Array of command-line arguments
1512 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1515 static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1517 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1521 * do_i2c_nm() - Handle the "i2c nm" command-line command
1522 * @cmdtp: Command data struct pointer
1523 * @flag: Command flag
1524 * @argc: Command-line argument count
1525 * @argv: Array of command-line arguments
1527 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1530 static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1532 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1536 * do_i2c_reset() - Handle the "i2c reset" command-line command
1537 * @cmdtp: Command data struct pointer
1538 * @flag: Command flag
1539 * @argc: Command-line argument count
1540 * @argv: Array of command-line arguments
1542 * Returns zero always.
1544 static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1546 #if defined(CONFIG_SYS_I2C)
1547 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1549 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
1554 static cmd_tbl_t cmd_i2c_sub[] = {
1555 #if defined(CONFIG_SYS_I2C)
1556 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
1557 #endif /* CONFIG_I2C_MUX */
1558 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
1559 #if defined(CONFIG_SYS_I2C) || \
1560 defined(CONFIG_I2C_MULTI_BUS)
1561 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1562 #endif /* CONFIG_I2C_MULTI_BUS */
1563 #if defined(CONFIG_I2C_EDID)
1564 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1565 #endif /* CONFIG_I2C_EDID */
1566 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1567 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1568 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1569 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1570 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1571 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
1572 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
1573 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
1574 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1575 #if defined(CONFIG_CMD_SDRAM)
1576 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1578 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1581 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1582 void i2c_reloc(void) {
1583 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1588 * do_i2c() - Handle the "i2c" command-line command
1589 * @cmdtp: Command data struct pointer
1590 * @flag: Command flag
1591 * @argc: Command-line argument count
1592 * @argv: Array of command-line arguments
1594 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1597 static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1602 return CMD_RET_USAGE;
1604 /* Strip off leading 'i2c' command argument */
1608 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1611 return c->cmd(cmdtp, flag, argc, argv);
1613 return CMD_RET_USAGE;
1616 /***************************************************/
1617 #ifdef CONFIG_SYS_LONGHELP
1618 static char i2c_help_text[] =
1619 #if defined(CONFIG_SYS_I2C)
1620 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
1621 #endif /* CONFIG_I2C_MUX */
1622 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
1623 #if defined(CONFIG_SYS_I2C) || \
1624 defined(CONFIG_I2C_MULTI_BUS)
1625 "i2c dev [dev] - show or set current I2C bus\n"
1626 #endif /* CONFIG_I2C_MULTI_BUS */
1627 #if defined(CONFIG_I2C_EDID)
1628 "i2c edid chip - print EDID configuration information\n"
1629 #endif /* CONFIG_I2C_EDID */
1630 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
1631 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1632 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1633 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1634 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
1635 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
1636 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
1637 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
1638 "i2c reset - re-init the I2C Controller\n"
1639 #if defined(CONFIG_CMD_SDRAM)
1640 "i2c sdram chip - print SDRAM configuration information\n"
1642 "i2c speed [speed] - show or set I2C bus speed";
1651 #if defined(CONFIG_I2C_MUX)
1652 static int i2c_mux_add_device(I2C_MUX_DEVICE *dev)
1654 I2C_MUX_DEVICE *devtmp = i2c_mux_devices;
1656 if (i2c_mux_devices == NULL) {
1657 i2c_mux_devices = dev;
1660 while (devtmp->next != NULL)
1661 devtmp = devtmp->next;
1667 I2C_MUX_DEVICE *i2c_mux_search_device(int id)
1669 I2C_MUX_DEVICE *device = i2c_mux_devices;
1671 while (device != NULL) {
1672 if (device->busid == id)
1674 device = device->next;
1679 /* searches in the buf from *pos the next ':'.
1681 * 0 if found (with *pos = where)
1682 * < 0 if an error occured
1683 * > 0 if the end of buf is reached
1685 static int i2c_mux_search_next (int *pos, uchar *buf, int len)
1687 while ((buf[*pos] != ':') && (*pos < len)) {
1692 if (buf[*pos] != ':')
1697 static int i2c_mux_get_busid (void)
1699 int tmp = i2c_mux_busid;
1705 /* Analyses a Muxstring and immediately sends the
1706 commands to the muxes. Runs from flash.
1708 int i2c_mux_ident_muxstring_f (uchar *buf)
1713 int len = strlen((char *)buf);
1721 ret = i2c_mux_search_next(&pos, buf, len);
1724 /* search address */
1727 ret = i2c_mux_search_next(&pos, buf, len);
1731 chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1733 /* search channel */
1736 ret = i2c_mux_search_next(&pos, buf, len);
1740 if (buf[pos] != 0) {
1744 channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1747 if (i2c_write(chip, 0, 0, &channel, 1) != 0) {
1748 printf ("Error setting Mux: chip:%x channel: \
1749 %x\n", chip, channel);
1761 /* Analyses a Muxstring and if this String is correct
1762 * adds a new I2C Bus.
1764 I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf)
1766 I2C_MUX_DEVICE *device;
1771 int len = strlen((char *)buf);
1774 device = (I2C_MUX_DEVICE *)malloc (sizeof(I2C_MUX_DEVICE));
1776 device->busid = i2c_mux_get_busid ();
1777 device->next = NULL;
1779 mux = (I2C_MUX *)malloc (sizeof(I2C_MUX));
1781 /* search name of mux */
1783 ret = i2c_mux_search_next(&pos, buf, len);
1785 printf ("%s no name.\n", __FUNCTION__);
1786 mux->name = (char *)malloc (pos - oldpos + 1);
1787 memcpy (mux->name, &buf[oldpos], pos - oldpos);
1788 mux->name[pos - oldpos] = 0;
1789 /* search address */
1792 ret = i2c_mux_search_next(&pos, buf, len);
1794 printf ("%s no mux address.\n", __FUNCTION__);
1796 mux->chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1798 /* search channel */
1801 ret = i2c_mux_search_next(&pos, buf, len);
1803 printf ("%s no mux channel.\n", __FUNCTION__);
1805 if (buf[pos] != 0) {
1809 mux->channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1812 if (device->mux == NULL)
1815 I2C_MUX *muxtmp = device->mux;
1816 while (muxtmp->next != NULL) {
1817 muxtmp = muxtmp->next;
1826 i2c_mux_add_device (device);
1833 int i2x_mux_select_mux(int bus)
1835 I2C_MUX_DEVICE *dev;
1838 if ((gd->flags & GD_FLG_RELOC) != GD_FLG_RELOC) {
1839 /* select Default Mux Bus */
1840 #if defined(CONFIG_SYS_I2C_IVM_BUS)
1841 i2c_mux_ident_muxstring_f ((uchar *)CONFIG_SYS_I2C_IVM_BUS);
1845 buf = (unsigned char *) getenv("EEprom_ivm");
1847 i2c_mux_ident_muxstring_f (buf);
1852 dev = i2c_mux_search_device(bus);
1857 while (mux != NULL) {
1858 /* do deblocking on each level of mux, before mux config */
1860 if (i2c_write(mux->chip, 0, 0, &mux->channel, 1) != 0) {
1861 printf ("Error setting Mux: chip:%x channel: \
1862 %x\n", mux->chip, mux->channel);
1867 /* do deblocking on each level of mux and after mux config */
1871 #endif /* CONFIG_I2C_MUX */