cmd_i2c: Use ARRAY_SIZE instead of reinventing it
[oweals/u-boot.git] / common / cmd_i2c.c
1 /*
2  * (C) Copyright 2009
3  * Sergey Kubushyn, himself, ksi@koi8.net
4  *
5  * Changes for unified multibus/multiadapter I2C support.
6  *
7  * (C) Copyright 2001
8  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
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.
17  *
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.
22  *
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,
26  * MA 02111-1307 USA
27  */
28
29 /*
30  * I2C Functions similar to the standard memory functions.
31  *
32  * There are several parameters in many of the commands that bear further
33  * explanations:
34  *
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 :-/.
43  *
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.
50  *
51  *   The default {addr} parameter is one byte (.1) which works well for
52  *   memories and registers with 8 bits of address space.
53  *
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.
60  *
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).
63  *
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.
80  *
81  * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
82  */
83
84 #include <common.h>
85 #include <command.h>
86 #include <edid.h>
87 #include <environment.h>
88 #include <i2c.h>
89 #include <malloc.h>
90 #include <asm/byteorder.h>
91 #include <linux/compiler.h>
92
93 DECLARE_GLOBAL_DATA_PTR;
94
95 /* Display values from last command.
96  * Memory modify remembered values are different from display memory.
97  */
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;
102
103 static uchar    i2c_mm_last_chip;
104 static uint     i2c_mm_last_addr;
105 static uint     i2c_mm_last_alen;
106
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 */
111
112 #if defined(CONFIG_SYS_I2C_NOPROBES)
113 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
114 static struct
115 {
116         uchar   bus;
117         uchar   addr;
118 } i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
119 #define GET_BUS_NUM     i2c_get_bus_num()
120 #define COMPARE_BUS(b,i)        (i2c_no_probes[(i)].bus == (b))
121 #define COMPARE_ADDR(a,i)       (i2c_no_probes[(i)].addr == (a))
122 #define NO_PROBE_ADDR(i)        i2c_no_probes[(i)].addr
123 #else           /* single bus */
124 static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
125 #define GET_BUS_NUM     0
126 #define COMPARE_BUS(b,i)        ((b) == 0)      /* Make compiler happy */
127 #define COMPARE_ADDR(a,i)       (i2c_no_probes[(i)] == (a))
128 #define NO_PROBE_ADDR(i)        i2c_no_probes[(i)]
129 #endif  /* defined(CONFIG_SYS_I2C) */
130 #endif
131
132 #define DISP_LINE_LEN   16
133
134 /**
135  * i2c_init_board() - Board-specific I2C bus init
136  *
137  * This function is the default no-op implementation of I2C bus
138  * initialization. This function can be overriden by board-specific
139  * implementation if needed.
140  */
141 __weak
142 void i2c_init_board(void)
143 {
144 }
145
146 /* TODO: Implement architecture-specific get/set functions */
147
148 /**
149  * i2c_get_bus_speed() - Return I2C bus speed
150  *
151  * This function is the default implementation of function for retrieveing
152  * the current I2C bus speed in Hz.
153  *
154  * A driver implementing runtime switching of I2C bus speed must override
155  * this function to report the speed correctly. Simple or legacy drivers
156  * can use this fallback.
157  *
158  * Returns I2C bus speed in Hz.
159  */
160 #if !defined(CONFIG_SYS_I2C)
161 /*
162  * TODO: Implement architecture-specific get/set functions
163  * Should go away, if we switched completely to new multibus support
164  */
165 __weak
166 unsigned int i2c_get_bus_speed(void)
167 {
168         return CONFIG_SYS_I2C_SPEED;
169 }
170
171 /**
172  * i2c_set_bus_speed() - Configure I2C bus speed
173  * @speed:      Newly set speed of the I2C bus in Hz
174  *
175  * This function is the default implementation of function for setting
176  * the I2C bus speed in Hz.
177  *
178  * A driver implementing runtime switching of I2C bus speed must override
179  * this function to report the speed correctly. Simple or legacy drivers
180  * can use this fallback.
181  *
182  * Returns zero on success, negative value on error.
183  */
184 __weak
185 int i2c_set_bus_speed(unsigned int speed)
186 {
187         if (speed != CONFIG_SYS_I2C_SPEED)
188                 return -1;
189
190         return 0;
191 }
192 #endif
193
194 /**
195  * get_alen() - Small parser helper function to get address length
196  *
197  * Returns the address length.
198  */
199 static uint get_alen(char *arg)
200 {
201         int     j;
202         int     alen;
203
204         alen = 1;
205         for (j = 0; j < 8; j++) {
206                 if (arg[j] == '.') {
207                         alen = arg[j+1] - '0';
208                         break;
209                 } else if (arg[j] == '\0')
210                         break;
211         }
212         return alen;
213 }
214
215 /**
216  * do_i2c_read() - Handle the "i2c read" command-line command
217  * @cmdtp:      Command data struct pointer
218  * @flag:       Command flag
219  * @argc:       Command-line argument count
220  * @argv:       Array of command-line arguments
221  *
222  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
223  * on error.
224  *
225  * Syntax:
226  *      i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
227  */
228 static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
229 {
230         u_char  chip;
231         uint    devaddr, alen, length;
232         u_char  *memaddr;
233
234         if (argc != 5)
235                 return CMD_RET_USAGE;
236
237         /*
238          * I2C chip address
239          */
240         chip = simple_strtoul(argv[1], NULL, 16);
241
242         /*
243          * I2C data address within the chip.  This can be 1 or
244          * 2 bytes long.  Some day it might be 3 bytes long :-).
245          */
246         devaddr = simple_strtoul(argv[2], NULL, 16);
247         alen = get_alen(argv[2]);
248         if (alen > 3)
249                 return CMD_RET_USAGE;
250
251         /*
252          * Length is the number of objects, not number of bytes.
253          */
254         length = simple_strtoul(argv[3], NULL, 16);
255
256         /*
257          * memaddr is the address where to store things in memory
258          */
259         memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
260
261         if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
262                 puts ("Error reading the chip.\n");
263                 return 1;
264         }
265         return 0;
266 }
267
268 static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
269 {
270         u_char  chip;
271         uint    devaddr, alen, length;
272         u_char  *memaddr;
273
274         if (argc != 5)
275                 return cmd_usage(cmdtp);
276
277         /*
278          * memaddr is the address where to store things in memory
279          */
280         memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
281
282         /*
283          * I2C chip address
284          */
285         chip = simple_strtoul(argv[2], NULL, 16);
286
287         /*
288          * I2C data address within the chip.  This can be 1 or
289          * 2 bytes long.  Some day it might be 3 bytes long :-).
290          */
291         devaddr = simple_strtoul(argv[3], NULL, 16);
292         alen = get_alen(argv[3]);
293         if (alen > 3)
294                 return cmd_usage(cmdtp);
295
296         /*
297          * Length is the number of objects, not number of bytes.
298          */
299         length = simple_strtoul(argv[4], NULL, 16);
300
301         while (length-- > 0) {
302                 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
303                         puts("Error writing to the chip.\n");
304                         return 1;
305                 }
306 /*
307  * No write delay with FRAM devices.
308  */
309 #if !defined(CONFIG_SYS_I2C_FRAM)
310                 udelay(11000);
311 #endif
312         }
313         return 0;
314 }
315
316 /**
317  * do_i2c_md() - Handle the "i2c md" command-line command
318  * @cmdtp:      Command data struct pointer
319  * @flag:       Command flag
320  * @argc:       Command-line argument count
321  * @argv:       Array of command-line arguments
322  *
323  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
324  * on error.
325  *
326  * Syntax:
327  *      i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
328  */
329 static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
330 {
331         u_char  chip;
332         uint    addr, alen, length;
333         int     j, nbytes, linebytes;
334
335         /* We use the last specified parameters, unless new ones are
336          * entered.
337          */
338         chip   = i2c_dp_last_chip;
339         addr   = i2c_dp_last_addr;
340         alen   = i2c_dp_last_alen;
341         length = i2c_dp_last_length;
342
343         if (argc < 3)
344                 return CMD_RET_USAGE;
345
346         if ((flag & CMD_FLAG_REPEAT) == 0) {
347                 /*
348                  * New command specified.
349                  */
350
351                 /*
352                  * I2C chip address
353                  */
354                 chip = simple_strtoul(argv[1], NULL, 16);
355
356                 /*
357                  * I2C data address within the chip.  This can be 1 or
358                  * 2 bytes long.  Some day it might be 3 bytes long :-).
359                  */
360                 addr = simple_strtoul(argv[2], NULL, 16);
361                 alen = get_alen(argv[2]);
362                 if (alen > 3)
363                         return CMD_RET_USAGE;
364
365                 /*
366                  * If another parameter, it is the length to display.
367                  * Length is the number of objects, not number of bytes.
368                  */
369                 if (argc > 3)
370                         length = simple_strtoul(argv[3], NULL, 16);
371         }
372
373         /*
374          * Print the lines.
375          *
376          * We buffer all read data, so we can make sure data is read only
377          * once.
378          */
379         nbytes = length;
380         do {
381                 unsigned char   linebuf[DISP_LINE_LEN];
382                 unsigned char   *cp;
383
384                 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
385
386                 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
387                         puts ("Error reading the chip.\n");
388                 else {
389                         printf("%04x:", addr);
390                         cp = linebuf;
391                         for (j=0; j<linebytes; j++) {
392                                 printf(" %02x", *cp++);
393                                 addr++;
394                         }
395                         puts ("    ");
396                         cp = linebuf;
397                         for (j=0; j<linebytes; j++) {
398                                 if ((*cp < 0x20) || (*cp > 0x7e))
399                                         puts (".");
400                                 else
401                                         printf("%c", *cp);
402                                 cp++;
403                         }
404                         putc ('\n');
405                 }
406                 nbytes -= linebytes;
407         } while (nbytes > 0);
408
409         i2c_dp_last_chip   = chip;
410         i2c_dp_last_addr   = addr;
411         i2c_dp_last_alen   = alen;
412         i2c_dp_last_length = length;
413
414         return 0;
415 }
416
417 /**
418  * do_i2c_mw() - Handle the "i2c mw" command-line command
419  * @cmdtp:      Command data struct pointer
420  * @flag:       Command flag
421  * @argc:       Command-line argument count
422  * @argv:       Array of command-line arguments
423  *
424  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
425  * on error.
426  *
427  * Syntax:
428  *      i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
429  */
430 static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
431 {
432         uchar   chip;
433         ulong   addr;
434         uint    alen;
435         uchar   byte;
436         int     count;
437
438         if ((argc < 4) || (argc > 5))
439                 return CMD_RET_USAGE;
440
441         /*
442          * Chip is always specified.
443          */
444         chip = simple_strtoul(argv[1], NULL, 16);
445
446         /*
447          * Address is always specified.
448          */
449         addr = simple_strtoul(argv[2], NULL, 16);
450         alen = get_alen(argv[2]);
451         if (alen > 3)
452                 return CMD_RET_USAGE;
453
454         /*
455          * Value to write is always specified.
456          */
457         byte = simple_strtoul(argv[3], NULL, 16);
458
459         /*
460          * Optional count
461          */
462         if (argc == 5)
463                 count = simple_strtoul(argv[4], NULL, 16);
464         else
465                 count = 1;
466
467         while (count-- > 0) {
468                 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
469                         puts ("Error writing the chip.\n");
470                 /*
471                  * Wait for the write to complete.  The write can take
472                  * up to 10mSec (we allow a little more time).
473                  */
474 /*
475  * No write delay with FRAM devices.
476  */
477 #if !defined(CONFIG_SYS_I2C_FRAM)
478                 udelay(11000);
479 #endif
480         }
481
482         return 0;
483 }
484
485 /**
486  * do_i2c_crc() - Handle the "i2c crc32" command-line command
487  * @cmdtp:      Command data struct pointer
488  * @flag:       Command flag
489  * @argc:       Command-line argument count
490  * @argv:       Array of command-line arguments
491  *
492  * Calculate a CRC on memory
493  *
494  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
495  * on error.
496  *
497  * Syntax:
498  *      i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
499  */
500 static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
501 {
502         uchar   chip;
503         ulong   addr;
504         uint    alen;
505         int     count;
506         uchar   byte;
507         ulong   crc;
508         ulong   err;
509
510         if (argc < 4)
511                 return CMD_RET_USAGE;
512
513         /*
514          * Chip is always specified.
515          */
516         chip = simple_strtoul(argv[1], NULL, 16);
517
518         /*
519          * Address is always specified.
520          */
521         addr = simple_strtoul(argv[2], NULL, 16);
522         alen = get_alen(argv[2]);
523         if (alen > 3)
524                 return CMD_RET_USAGE;
525
526         /*
527          * Count is always specified
528          */
529         count = simple_strtoul(argv[3], NULL, 16);
530
531         printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
532         /*
533          * CRC a byte at a time.  This is going to be slooow, but hey, the
534          * memories are small and slow too so hopefully nobody notices.
535          */
536         crc = 0;
537         err = 0;
538         while (count-- > 0) {
539                 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
540                         err++;
541                 crc = crc32 (crc, &byte, 1);
542                 addr++;
543         }
544         if (err > 0)
545                 puts ("Error reading the chip,\n");
546         else
547                 printf ("%08lx\n", crc);
548
549         return 0;
550 }
551
552 /**
553  * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
554  * @cmdtp:      Command data struct pointer
555  * @flag:       Command flag
556  * @argc:       Command-line argument count
557  * @argv:       Array of command-line arguments
558  *
559  * Modify memory.
560  *
561  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
562  * on error.
563  *
564  * Syntax:
565  *      i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
566  *      i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
567  */
568 static int
569 mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
570 {
571         uchar   chip;
572         ulong   addr;
573         uint    alen;
574         ulong   data;
575         int     size = 1;
576         int     nbytes;
577
578         if (argc != 3)
579                 return CMD_RET_USAGE;
580
581 #ifdef CONFIG_BOOT_RETRY_TIME
582         reset_cmd_timeout();    /* got a good command to get here */
583 #endif
584         /*
585          * We use the last specified parameters, unless new ones are
586          * entered.
587          */
588         chip = i2c_mm_last_chip;
589         addr = i2c_mm_last_addr;
590         alen = i2c_mm_last_alen;
591
592         if ((flag & CMD_FLAG_REPEAT) == 0) {
593                 /*
594                  * New command specified.  Check for a size specification.
595                  * Defaults to byte if no or incorrect specification.
596                  */
597                 size = cmd_get_data_size(argv[0], 1);
598
599                 /*
600                  * Chip is always specified.
601                  */
602                 chip = simple_strtoul(argv[1], NULL, 16);
603
604                 /*
605                  * Address is always specified.
606                  */
607                 addr = simple_strtoul(argv[2], NULL, 16);
608                 alen = get_alen(argv[2]);
609                 if (alen > 3)
610                         return CMD_RET_USAGE;
611         }
612
613         /*
614          * Print the address, followed by value.  Then accept input for
615          * the next value.  A non-converted value exits.
616          */
617         do {
618                 printf("%08lx:", addr);
619                 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
620                         puts ("\nError reading the chip,\n");
621                 else {
622                         data = cpu_to_be32(data);
623                         if (size == 1)
624                                 printf(" %02lx", (data >> 24) & 0x000000FF);
625                         else if (size == 2)
626                                 printf(" %04lx", (data >> 16) & 0x0000FFFF);
627                         else
628                                 printf(" %08lx", data);
629                 }
630
631                 nbytes = readline (" ? ");
632                 if (nbytes == 0) {
633                         /*
634                          * <CR> pressed as only input, don't modify current
635                          * location and move to next.
636                          */
637                         if (incrflag)
638                                 addr += size;
639                         nbytes = size;
640 #ifdef CONFIG_BOOT_RETRY_TIME
641                         reset_cmd_timeout(); /* good enough to not time out */
642 #endif
643                 }
644 #ifdef CONFIG_BOOT_RETRY_TIME
645                 else if (nbytes == -2)
646                         break;  /* timed out, exit the command  */
647 #endif
648                 else {
649                         char *endp;
650
651                         data = simple_strtoul(console_buffer, &endp, 16);
652                         if (size == 1)
653                                 data = data << 24;
654                         else if (size == 2)
655                                 data = data << 16;
656                         data = be32_to_cpu(data);
657                         nbytes = endp - console_buffer;
658                         if (nbytes) {
659 #ifdef CONFIG_BOOT_RETRY_TIME
660                                 /*
661                                  * good enough to not time out
662                                  */
663                                 reset_cmd_timeout();
664 #endif
665                                 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
666                                         puts ("Error writing the chip.\n");
667 #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
668                                 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
669 #endif
670                                 if (incrflag)
671                                         addr += size;
672                         }
673                 }
674         } while (nbytes);
675
676         i2c_mm_last_chip = chip;
677         i2c_mm_last_addr = addr;
678         i2c_mm_last_alen = alen;
679
680         return 0;
681 }
682
683 /**
684  * do_i2c_probe() - Handle the "i2c probe" command-line command
685  * @cmdtp:      Command data struct pointer
686  * @flag:       Command flag
687  * @argc:       Command-line argument count
688  * @argv:       Array of command-line arguments
689  *
690  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
691  * on error.
692  *
693  * Syntax:
694  *      i2c probe {addr}
695  *
696  * Returns zero (success) if one or more I2C devices was found
697  */
698 static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
699 {
700         int j;
701         int addr = -1;
702         int found = 0;
703 #if defined(CONFIG_SYS_I2C_NOPROBES)
704         int k, skip;
705         unsigned int bus = GET_BUS_NUM;
706 #endif  /* NOPROBES */
707
708         if (argc == 2)
709                 addr = simple_strtol(argv[1], 0, 16);
710
711         puts ("Valid chip addresses:");
712         for (j = 0; j < 128; j++) {
713                 if ((0 <= addr) && (j != addr))
714                         continue;
715
716 #if defined(CONFIG_SYS_I2C_NOPROBES)
717                 skip = 0;
718                 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
719                         if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
720                                 skip = 1;
721                                 break;
722                         }
723                 }
724                 if (skip)
725                         continue;
726 #endif
727                 if (i2c_probe(j) == 0) {
728                         printf(" %02X", j);
729                         found++;
730                 }
731         }
732         putc ('\n');
733
734 #if defined(CONFIG_SYS_I2C_NOPROBES)
735         puts ("Excluded chip addresses:");
736         for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
737                 if (COMPARE_BUS(bus,k))
738                         printf(" %02X", NO_PROBE_ADDR(k));
739         }
740         putc ('\n');
741 #endif
742
743         return (0 == found);
744 }
745
746 /**
747  * do_i2c_loop() - Handle the "i2c loop" command-line command
748  * @cmdtp:      Command data struct pointer
749  * @flag:       Command flag
750  * @argc:       Command-line argument count
751  * @argv:       Array of command-line arguments
752  *
753  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
754  * on error.
755  *
756  * Syntax:
757  *      i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
758  *      {length} - Number of bytes to read
759  *      {delay}  - A DECIMAL number and defaults to 1000 uSec
760  */
761 static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
762 {
763         u_char  chip;
764         ulong   alen;
765         uint    addr;
766         uint    length;
767         u_char  bytes[16];
768         int     delay;
769
770         if (argc < 3)
771                 return CMD_RET_USAGE;
772
773         /*
774          * Chip is always specified.
775          */
776         chip = simple_strtoul(argv[1], NULL, 16);
777
778         /*
779          * Address is always specified.
780          */
781         addr = simple_strtoul(argv[2], NULL, 16);
782         alen = get_alen(argv[2]);
783         if (alen > 3)
784                 return CMD_RET_USAGE;
785
786         /*
787          * Length is the number of objects, not number of bytes.
788          */
789         length = 1;
790         length = simple_strtoul(argv[3], NULL, 16);
791         if (length > sizeof(bytes))
792                 length = sizeof(bytes);
793
794         /*
795          * The delay time (uSec) is optional.
796          */
797         delay = 1000;
798         if (argc > 3)
799                 delay = simple_strtoul(argv[4], NULL, 10);
800         /*
801          * Run the loop...
802          */
803         while (1) {
804                 if (i2c_read(chip, addr, alen, bytes, length) != 0)
805                         puts ("Error reading the chip.\n");
806                 udelay(delay);
807         }
808
809         /* NOTREACHED */
810         return 0;
811 }
812
813 /*
814  * The SDRAM command is separately configured because many
815  * (most?) embedded boards don't use SDRAM DIMMs.
816  *
817  * FIXME: Document and probably move elsewhere!
818  */
819 #if defined(CONFIG_CMD_SDRAM)
820 static void print_ddr2_tcyc (u_char const b)
821 {
822         printf ("%d.", (b >> 4) & 0x0F);
823         switch (b & 0x0F) {
824         case 0x0:
825         case 0x1:
826         case 0x2:
827         case 0x3:
828         case 0x4:
829         case 0x5:
830         case 0x6:
831         case 0x7:
832         case 0x8:
833         case 0x9:
834                 printf ("%d ns\n", b & 0x0F);
835                 break;
836         case 0xA:
837                 puts ("25 ns\n");
838                 break;
839         case 0xB:
840                 puts ("33 ns\n");
841                 break;
842         case 0xC:
843                 puts ("66 ns\n");
844                 break;
845         case 0xD:
846                 puts ("75 ns\n");
847                 break;
848         default:
849                 puts ("?? ns\n");
850                 break;
851         }
852 }
853
854 static void decode_bits (u_char const b, char const *str[], int const do_once)
855 {
856         u_char mask;
857
858         for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
859                 if (b & mask) {
860                         puts (*str);
861                         if (do_once)
862                                 return;
863                 }
864         }
865 }
866
867 /*
868  * Syntax:
869  *      i2c sdram {i2c_chip}
870  */
871 static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
872 {
873         enum { unknown, EDO, SDRAM, DDR2 } type;
874
875         u_char  chip;
876         u_char  data[128];
877         u_char  cksum;
878         int     j;
879
880         static const char *decode_CAS_DDR2[] = {
881                 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
882         };
883
884         static const char *decode_CAS_default[] = {
885                 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
886         };
887
888         static const char *decode_CS_WE_default[] = {
889                 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
890         };
891
892         static const char *decode_byte21_default[] = {
893                 "  TBD (bit 7)\n",
894                 "  Redundant row address\n",
895                 "  Differential clock input\n",
896                 "  Registerd DQMB inputs\n",
897                 "  Buffered DQMB inputs\n",
898                 "  On-card PLL\n",
899                 "  Registered address/control lines\n",
900                 "  Buffered address/control lines\n"
901         };
902
903         static const char *decode_byte22_DDR2[] = {
904                 "  TBD (bit 7)\n",
905                 "  TBD (bit 6)\n",
906                 "  TBD (bit 5)\n",
907                 "  TBD (bit 4)\n",
908                 "  TBD (bit 3)\n",
909                 "  Supports partial array self refresh\n",
910                 "  Supports 50 ohm ODT\n",
911                 "  Supports weak driver\n"
912         };
913
914         static const char *decode_row_density_DDR2[] = {
915                 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
916                 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
917         };
918
919         static const char *decode_row_density_default[] = {
920                 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
921                 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
922         };
923
924         if (argc < 2)
925                 return CMD_RET_USAGE;
926
927         /*
928          * Chip is always specified.
929          */
930         chip = simple_strtoul (argv[1], NULL, 16);
931
932         if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
933                 puts ("No SDRAM Serial Presence Detect found.\n");
934                 return 1;
935         }
936
937         cksum = 0;
938         for (j = 0; j < 63; j++) {
939                 cksum += data[j];
940         }
941         if (cksum != data[63]) {
942                 printf ("WARNING: Configuration data checksum failure:\n"
943                         "  is 0x%02x, calculated 0x%02x\n", data[63], cksum);
944         }
945         printf ("SPD data revision            %d.%d\n",
946                 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
947         printf ("Bytes used                   0x%02X\n", data[0]);
948         printf ("Serial memory size           0x%02X\n", 1 << data[1]);
949
950         puts ("Memory type                  ");
951         switch (data[2]) {
952         case 2:
953                 type = EDO;
954                 puts ("EDO\n");
955                 break;
956         case 4:
957                 type = SDRAM;
958                 puts ("SDRAM\n");
959                 break;
960         case 8:
961                 type = DDR2;
962                 puts ("DDR2\n");
963                 break;
964         default:
965                 type = unknown;
966                 puts ("unknown\n");
967                 break;
968         }
969
970         puts ("Row address bits             ");
971         if ((data[3] & 0x00F0) == 0)
972                 printf ("%d\n", data[3] & 0x0F);
973         else
974                 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
975
976         puts ("Column address bits          ");
977         if ((data[4] & 0x00F0) == 0)
978                 printf ("%d\n", data[4] & 0x0F);
979         else
980                 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
981
982         switch (type) {
983         case DDR2:
984                 printf ("Number of ranks              %d\n",
985                         (data[5] & 0x07) + 1);
986                 break;
987         default:
988                 printf ("Module rows                  %d\n", data[5]);
989                 break;
990         }
991
992         switch (type) {
993         case DDR2:
994                 printf ("Module data width            %d bits\n", data[6]);
995                 break;
996         default:
997                 printf ("Module data width            %d bits\n",
998                         (data[7] << 8) | data[6]);
999                 break;
1000         }
1001
1002         puts ("Interface signal levels      ");
1003         switch(data[8]) {
1004                 case 0:  puts ("TTL 5.0 V\n");  break;
1005                 case 1:  puts ("LVTTL\n");      break;
1006                 case 2:  puts ("HSTL 1.5 V\n"); break;
1007                 case 3:  puts ("SSTL 3.3 V\n"); break;
1008                 case 4:  puts ("SSTL 2.5 V\n"); break;
1009                 case 5:  puts ("SSTL 1.8 V\n"); break;
1010                 default: puts ("unknown\n");    break;
1011         }
1012
1013         switch (type) {
1014         case DDR2:
1015                 printf ("SDRAM cycle time             ");
1016                 print_ddr2_tcyc (data[9]);
1017                 break;
1018         default:
1019                 printf ("SDRAM cycle time             %d.%d ns\n",
1020                         (data[9] >> 4) & 0x0F, data[9] & 0x0F);
1021                 break;
1022         }
1023
1024         switch (type) {
1025         case DDR2:
1026                 printf ("SDRAM access time            0.%d%d ns\n",
1027                         (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1028                 break;
1029         default:
1030                 printf ("SDRAM access time            %d.%d ns\n",
1031                         (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1032                 break;
1033         }
1034
1035         puts ("EDC configuration            ");
1036         switch (data[11]) {
1037                 case 0:  puts ("None\n");       break;
1038                 case 1:  puts ("Parity\n");     break;
1039                 case 2:  puts ("ECC\n");        break;
1040                 default: puts ("unknown\n");    break;
1041         }
1042
1043         if ((data[12] & 0x80) == 0)
1044                 puts ("No self refresh, rate        ");
1045         else
1046                 puts ("Self refresh, rate           ");
1047
1048         switch(data[12] & 0x7F) {
1049                 case 0:  puts ("15.625 us\n");  break;
1050                 case 1:  puts ("3.9 us\n");     break;
1051                 case 2:  puts ("7.8 us\n");     break;
1052                 case 3:  puts ("31.3 us\n");    break;
1053                 case 4:  puts ("62.5 us\n");    break;
1054                 case 5:  puts ("125 us\n");     break;
1055                 default: puts ("unknown\n");    break;
1056         }
1057
1058         switch (type) {
1059         case DDR2:
1060                 printf ("SDRAM width (primary)        %d\n", data[13]);
1061                 break;
1062         default:
1063                 printf ("SDRAM width (primary)        %d\n", data[13] & 0x7F);
1064                 if ((data[13] & 0x80) != 0) {
1065                         printf ("  (second bank)              %d\n",
1066                                 2 * (data[13] & 0x7F));
1067                 }
1068                 break;
1069         }
1070
1071         switch (type) {
1072         case DDR2:
1073                 if (data[14] != 0)
1074                         printf ("EDC width                    %d\n", data[14]);
1075                 break;
1076         default:
1077                 if (data[14] != 0) {
1078                         printf ("EDC width                    %d\n",
1079                                 data[14] & 0x7F);
1080
1081                         if ((data[14] & 0x80) != 0) {
1082                                 printf ("  (second bank)              %d\n",
1083                                         2 * (data[14] & 0x7F));
1084                         }
1085                 }
1086                 break;
1087         }
1088
1089         if (DDR2 != type) {
1090                 printf ("Min clock delay, back-to-back random column addresses "
1091                         "%d\n", data[15]);
1092         }
1093
1094         puts ("Burst length(s)             ");
1095         if (data[16] & 0x80) puts (" Page");
1096         if (data[16] & 0x08) puts (" 8");
1097         if (data[16] & 0x04) puts (" 4");
1098         if (data[16] & 0x02) puts (" 2");
1099         if (data[16] & 0x01) puts (" 1");
1100         putc ('\n');
1101         printf ("Number of banks              %d\n", data[17]);
1102
1103         switch (type) {
1104         case DDR2:
1105                 puts ("CAS latency(s)              ");
1106                 decode_bits (data[18], decode_CAS_DDR2, 0);
1107                 putc ('\n');
1108                 break;
1109         default:
1110                 puts ("CAS latency(s)              ");
1111                 decode_bits (data[18], decode_CAS_default, 0);
1112                 putc ('\n');
1113                 break;
1114         }
1115
1116         if (DDR2 != type) {
1117                 puts ("CS latency(s)               ");
1118                 decode_bits (data[19], decode_CS_WE_default, 0);
1119                 putc ('\n');
1120         }
1121
1122         if (DDR2 != type) {
1123                 puts ("WE latency(s)               ");
1124                 decode_bits (data[20], decode_CS_WE_default, 0);
1125                 putc ('\n');
1126         }
1127
1128         switch (type) {
1129         case DDR2:
1130                 puts ("Module attributes:\n");
1131                 if (data[21] & 0x80)
1132                         puts ("  TBD (bit 7)\n");
1133                 if (data[21] & 0x40)
1134                         puts ("  Analysis probe installed\n");
1135                 if (data[21] & 0x20)
1136                         puts ("  TBD (bit 5)\n");
1137                 if (data[21] & 0x10)
1138                         puts ("  FET switch external enable\n");
1139                 printf ("  %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
1140                 if (data[20] & 0x11) {
1141                         printf ("  %d active registers on DIMM\n",
1142                                 (data[21] & 0x03) + 1);
1143                 }
1144                 break;
1145         default:
1146                 puts ("Module attributes:\n");
1147                 if (!data[21])
1148                         puts ("  (none)\n");
1149                 else
1150                         decode_bits (data[21], decode_byte21_default, 0);
1151                 break;
1152         }
1153
1154         switch (type) {
1155         case DDR2:
1156                 decode_bits (data[22], decode_byte22_DDR2, 0);
1157                 break;
1158         default:
1159                 puts ("Device attributes:\n");
1160                 if (data[22] & 0x80) puts ("  TBD (bit 7)\n");
1161                 if (data[22] & 0x40) puts ("  TBD (bit 6)\n");
1162                 if (data[22] & 0x20) puts ("  Upper Vcc tolerance 5%\n");
1163                 else                 puts ("  Upper Vcc tolerance 10%\n");
1164                 if (data[22] & 0x10) puts ("  Lower Vcc tolerance 5%\n");
1165                 else                 puts ("  Lower Vcc tolerance 10%\n");
1166                 if (data[22] & 0x08) puts ("  Supports write1/read burst\n");
1167                 if (data[22] & 0x04) puts ("  Supports precharge all\n");
1168                 if (data[22] & 0x02) puts ("  Supports auto precharge\n");
1169                 if (data[22] & 0x01) puts ("  Supports early RAS# precharge\n");
1170                 break;
1171         }
1172
1173         switch (type) {
1174         case DDR2:
1175                 printf ("SDRAM cycle time (2nd highest CAS latency)        ");
1176                 print_ddr2_tcyc (data[23]);
1177                 break;
1178         default:
1179                 printf ("SDRAM cycle time (2nd highest CAS latency)        %d."
1180                         "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
1181                 break;
1182         }
1183
1184         switch (type) {
1185         case DDR2:
1186                 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1187                         "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1188                 break;
1189         default:
1190                 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1191                         "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1192                 break;
1193         }
1194
1195         switch (type) {
1196         case DDR2:
1197                 printf ("SDRAM cycle time (3rd highest CAS latency)        ");
1198                 print_ddr2_tcyc (data[25]);
1199                 break;
1200         default:
1201                 printf ("SDRAM cycle time (3rd highest CAS latency)        %d."
1202                         "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
1203                 break;
1204         }
1205
1206         switch (type) {
1207         case DDR2:
1208                 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1209                         "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1210                 break;
1211         default:
1212                 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1213                         "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1214                 break;
1215         }
1216
1217         switch (type) {
1218         case DDR2:
1219                 printf ("Minimum row precharge        %d.%02d ns\n",
1220                         (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
1221                 break;
1222         default:
1223                 printf ("Minimum row precharge        %d ns\n", data[27]);
1224                 break;
1225         }
1226
1227         switch (type) {
1228         case DDR2:
1229                 printf ("Row active to row active min %d.%02d ns\n",
1230                         (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
1231                 break;
1232         default:
1233                 printf ("Row active to row active min %d ns\n", data[28]);
1234                 break;
1235         }
1236
1237         switch (type) {
1238         case DDR2:
1239                 printf ("RAS to CAS delay min         %d.%02d ns\n",
1240                         (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
1241                 break;
1242         default:
1243                 printf ("RAS to CAS delay min         %d ns\n", data[29]);
1244                 break;
1245         }
1246
1247         printf ("Minimum RAS pulse width      %d ns\n", data[30]);
1248
1249         switch (type) {
1250         case DDR2:
1251                 puts ("Density of each row          ");
1252                 decode_bits (data[31], decode_row_density_DDR2, 1);
1253                 putc ('\n');
1254                 break;
1255         default:
1256                 puts ("Density of each row          ");
1257                 decode_bits (data[31], decode_row_density_default, 1);
1258                 putc ('\n');
1259                 break;
1260         }
1261
1262         switch (type) {
1263         case DDR2:
1264                 puts ("Command and Address setup    ");
1265                 if (data[32] >= 0xA0) {
1266                         printf ("1.%d%d ns\n",
1267                                 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
1268                 } else {
1269                         printf ("0.%d%d ns\n",
1270                                 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
1271                 }
1272                 break;
1273         default:
1274                 printf ("Command and Address setup    %c%d.%d ns\n",
1275                         (data[32] & 0x80) ? '-' : '+',
1276                         (data[32] >> 4) & 0x07, data[32] & 0x0F);
1277                 break;
1278         }
1279
1280         switch (type) {
1281         case DDR2:
1282                 puts ("Command and Address hold     ");
1283                 if (data[33] >= 0xA0) {
1284                         printf ("1.%d%d ns\n",
1285                                 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
1286                 } else {
1287                         printf ("0.%d%d ns\n",
1288                                 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
1289                 }
1290                 break;
1291         default:
1292                 printf ("Command and Address hold     %c%d.%d ns\n",
1293                         (data[33] & 0x80) ? '-' : '+',
1294                         (data[33] >> 4) & 0x07, data[33] & 0x0F);
1295                 break;
1296         }
1297
1298         switch (type) {
1299         case DDR2:
1300                 printf ("Data signal input setup      0.%d%d ns\n",
1301                         (data[34] >> 4) & 0x0F, data[34] & 0x0F);
1302                 break;
1303         default:
1304                 printf ("Data signal input setup      %c%d.%d ns\n",
1305                         (data[34] & 0x80) ? '-' : '+',
1306                         (data[34] >> 4) & 0x07, data[34] & 0x0F);
1307                 break;
1308         }
1309
1310         switch (type) {
1311         case DDR2:
1312                 printf ("Data signal input hold       0.%d%d ns\n",
1313                         (data[35] >> 4) & 0x0F, data[35] & 0x0F);
1314                 break;
1315         default:
1316                 printf ("Data signal input hold       %c%d.%d ns\n",
1317                         (data[35] & 0x80) ? '-' : '+',
1318                         (data[35] >> 4) & 0x07, data[35] & 0x0F);
1319                 break;
1320         }
1321
1322         puts ("Manufacturer's JEDEC ID      ");
1323         for (j = 64; j <= 71; j++)
1324                 printf ("%02X ", data[j]);
1325         putc ('\n');
1326         printf ("Manufacturing Location       %02X\n", data[72]);
1327         puts ("Manufacturer's Part Number   ");
1328         for (j = 73; j <= 90; j++)
1329                 printf ("%02X ", data[j]);
1330         putc ('\n');
1331         printf ("Revision Code                %02X %02X\n", data[91], data[92]);
1332         printf ("Manufacturing Date           %02X %02X\n", data[93], data[94]);
1333         puts ("Assembly Serial Number       ");
1334         for (j = 95; j <= 98; j++)
1335                 printf ("%02X ", data[j]);
1336         putc ('\n');
1337
1338         if (DDR2 != type) {
1339                 printf ("Speed rating                 PC%d\n",
1340                         data[126] == 0x66 ? 66 : data[126]);
1341         }
1342         return 0;
1343 }
1344 #endif
1345
1346 /*
1347  * Syntax:
1348  *      i2c edid {i2c_chip}
1349  */
1350 #if defined(CONFIG_I2C_EDID)
1351 int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1352 {
1353         u_char chip;
1354         struct edid1_info edid;
1355
1356         if (argc < 2) {
1357                 cmd_usage(cmdtp);
1358                 return 1;
1359         }
1360
1361         chip = simple_strtoul(argv[1], NULL, 16);
1362         if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1363                 puts("Error reading EDID content.\n");
1364                 return 1;
1365         }
1366
1367         if (edid_check_info(&edid)) {
1368                 puts("Content isn't valid EDID.\n");
1369                 return 1;
1370         }
1371
1372         edid_print_info(&edid);
1373         return 0;
1374
1375 }
1376 #endif /* CONFIG_I2C_EDID */
1377
1378 /**
1379  * do_i2c_show_bus() - Handle the "i2c bus" command-line command
1380  * @cmdtp:      Command data struct pointer
1381  * @flag:       Command flag
1382  * @argc:       Command-line argument count
1383  * @argv:       Array of command-line arguments
1384  *
1385  * Returns zero always.
1386  */
1387 #if defined(CONFIG_SYS_I2C)
1388 int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1389 {
1390         int     i;
1391 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1392         int     j;
1393 #endif
1394
1395         if (argc == 1) {
1396                 /* show all busses */
1397                 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1398                         printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1399 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1400                         for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1401                                 if (i2c_bus[i].next_hop[j].chip == 0)
1402                                         break;
1403                                 printf("->%s@0x%2x:%d",
1404                                        i2c_bus[i].next_hop[j].mux.name,
1405                                        i2c_bus[i].next_hop[j].chip,
1406                                        i2c_bus[i].next_hop[j].channel);
1407                         }
1408 #endif
1409                         printf("\n");
1410                 }
1411         } else {
1412                 /* show specific bus */
1413                 i = simple_strtoul(argv[1], NULL, 10);
1414                 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1415                         printf("Invalid bus %d\n", i);
1416                         return -1;
1417                 }
1418                 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1419 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1420                         for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1421                                 if (i2c_bus[i].next_hop[j].chip == 0)
1422                                         break;
1423                                 printf("->%s@0x%2x:%d",
1424                                        i2c_bus[i].next_hop[j].mux.name,
1425                                        i2c_bus[i].next_hop[j].chip,
1426                                        i2c_bus[i].next_hop[j].channel);
1427                         }
1428 #endif
1429                 printf("\n");
1430         }
1431
1432         return 0;
1433 }
1434 #endif
1435
1436 /**
1437  * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1438  * @cmdtp:      Command data struct pointer
1439  * @flag:       Command flag
1440  * @argc:       Command-line argument count
1441  * @argv:       Array of command-line arguments
1442  *
1443  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1444  * on error.
1445  */
1446 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
1447 int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1448 {
1449         int             ret = 0;
1450         unsigned int    bus_no;
1451
1452         if (argc == 1)
1453                 /* querying current setting */
1454                 printf("Current bus is %d\n", i2c_get_bus_num());
1455         else {
1456                 bus_no = simple_strtoul(argv[1], NULL, 10);
1457                 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1458                         printf("Invalid bus %d\n", bus_no);
1459                         return -1;
1460                 }
1461                 printf("Setting bus to %d\n", bus_no);
1462                 ret = i2c_set_bus_num(bus_no);
1463                 if (ret)
1464                         printf("Failure changing bus number (%d)\n", ret);
1465         }
1466         return ret;
1467 }
1468 #endif  /* defined(CONFIG_SYS_I2C) */
1469
1470 /**
1471  * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1472  * @cmdtp:      Command data struct pointer
1473  * @flag:       Command flag
1474  * @argc:       Command-line argument count
1475  * @argv:       Array of command-line arguments
1476  *
1477  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1478  * on error.
1479  */
1480 static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1481 {
1482         int speed, ret=0;
1483
1484         if (argc == 1)
1485                 /* querying current speed */
1486                 printf("Current bus speed=%d\n", i2c_get_bus_speed());
1487         else {
1488                 speed = simple_strtoul(argv[1], NULL, 10);
1489                 printf("Setting bus speed to %d Hz\n", speed);
1490                 ret = i2c_set_bus_speed(speed);
1491                 if (ret)
1492                         printf("Failure changing bus speed (%d)\n", ret);
1493         }
1494         return ret;
1495 }
1496
1497 /**
1498  * do_i2c_mm() - Handle the "i2c mm" command-line command
1499  * @cmdtp:      Command data struct pointer
1500  * @flag:       Command flag
1501  * @argc:       Command-line argument count
1502  * @argv:       Array of command-line arguments
1503  *
1504  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1505  * on error.
1506  */
1507 static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1508 {
1509         return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1510 }
1511
1512 /**
1513  * do_i2c_nm() - Handle the "i2c nm" command-line command
1514  * @cmdtp:      Command data struct pointer
1515  * @flag:       Command flag
1516  * @argc:       Command-line argument count
1517  * @argv:       Array of command-line arguments
1518  *
1519  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1520  * on error.
1521  */
1522 static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1523 {
1524         return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1525 }
1526
1527 /**
1528  * do_i2c_reset() - Handle the "i2c reset" command-line command
1529  * @cmdtp:      Command data struct pointer
1530  * @flag:       Command flag
1531  * @argc:       Command-line argument count
1532  * @argv:       Array of command-line arguments
1533  *
1534  * Returns zero always.
1535  */
1536 static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1537 {
1538 #if defined(CONFIG_SYS_I2C)
1539         i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1540 #else
1541         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
1542 #endif
1543         return 0;
1544 }
1545
1546 static cmd_tbl_t cmd_i2c_sub[] = {
1547 #if defined(CONFIG_SYS_I2C)
1548         U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
1549 #endif
1550         U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
1551 #if defined(CONFIG_SYS_I2C) || \
1552         defined(CONFIG_I2C_MULTI_BUS)
1553         U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1554 #endif  /* CONFIG_I2C_MULTI_BUS */
1555 #if defined(CONFIG_I2C_EDID)
1556         U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1557 #endif  /* CONFIG_I2C_EDID */
1558         U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1559         U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1560         U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1561         U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1562         U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1563         U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
1564         U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
1565         U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
1566         U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1567 #if defined(CONFIG_CMD_SDRAM)
1568         U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1569 #endif
1570         U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1571 };
1572
1573 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1574 void i2c_reloc(void) {
1575         fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1576 }
1577 #endif
1578
1579 /**
1580  * do_i2c() - Handle the "i2c" command-line command
1581  * @cmdtp:      Command data struct pointer
1582  * @flag:       Command flag
1583  * @argc:       Command-line argument count
1584  * @argv:       Array of command-line arguments
1585  *
1586  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1587  * on error.
1588  */
1589 static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1590 {
1591         cmd_tbl_t *c;
1592
1593         if (argc < 2)
1594                 return CMD_RET_USAGE;
1595
1596         /* Strip off leading 'i2c' command argument */
1597         argc--;
1598         argv++;
1599
1600         c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1601
1602         if (c)
1603                 return c->cmd(cmdtp, flag, argc, argv);
1604         else
1605                 return CMD_RET_USAGE;
1606 }
1607
1608 /***************************************************/
1609 #ifdef CONFIG_SYS_LONGHELP
1610 static char i2c_help_text[] =
1611 #if defined(CONFIG_SYS_I2C)
1612         "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
1613 #endif
1614         "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
1615 #if defined(CONFIG_SYS_I2C) || \
1616         defined(CONFIG_I2C_MULTI_BUS)
1617         "i2c dev [dev] - show or set current I2C bus\n"
1618 #endif  /* CONFIG_I2C_MULTI_BUS */
1619 #if defined(CONFIG_I2C_EDID)
1620         "i2c edid chip - print EDID configuration information\n"
1621 #endif  /* CONFIG_I2C_EDID */
1622         "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
1623         "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1624         "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1625         "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1626         "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
1627         "i2c probe [address] - test for and show device(s) on the I2C bus\n"
1628         "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
1629         "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
1630         "i2c reset - re-init the I2C Controller\n"
1631 #if defined(CONFIG_CMD_SDRAM)
1632         "i2c sdram chip - print SDRAM configuration information\n"
1633 #endif
1634         "i2c speed [speed] - show or set I2C bus speed";
1635 #endif
1636
1637 U_BOOT_CMD(
1638         i2c, 6, 1, do_i2c,
1639         "I2C sub-system",
1640         i2c_help_text
1641 );