]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_i2c.c
i2c: common changes for multibus/multiadapter support
[karo-tx-uboot.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_MUX) || \
114         defined(CONFIG_I2C_MULTI_BUS)
115 static struct
116 {
117         uchar   bus;
118         uchar   addr;
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) */
131
132 #define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
133 #endif
134
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;
138 #endif
139
140 #define DISP_LINE_LEN   16
141
142 /**
143  * i2c_init_board() - Board-specific I2C bus init
144  *
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.
148  */
149 __weak
150 void i2c_init_board(void)
151 {
152 }
153
154 /* TODO: Implement architecture-specific get/set functions */
155
156 /**
157  * i2c_get_bus_speed() - Return I2C bus speed
158  *
159  * This function is the default implementation of function for retrieveing
160  * the current I2C bus speed in Hz.
161  *
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.
165  *
166  * Returns I2C bus speed in Hz.
167  */
168 #if !defined(CONFIG_SYS_I2C)
169 /*
170  * TODO: Implement architecture-specific get/set functions
171  * Should go away, if we switched completely to new multibus support
172  */
173 __weak
174 unsigned int i2c_get_bus_speed(void)
175 {
176         return CONFIG_SYS_I2C_SPEED;
177 }
178
179 /**
180  * i2c_set_bus_speed() - Configure I2C bus speed
181  * @speed:      Newly set speed of the I2C bus in Hz
182  *
183  * This function is the default implementation of function for setting
184  * the I2C bus speed in Hz.
185  *
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.
189  *
190  * Returns zero on success, negative value on error.
191  */
192 __weak
193 int i2c_set_bus_speed(unsigned int speed)
194 {
195         if (speed != CONFIG_SYS_I2C_SPEED)
196                 return -1;
197
198         return 0;
199 }
200 #endif
201
202 /**
203  * get_alen() - Small parser helper function to get address length
204  *
205  * Returns the address length.
206  */
207 static uint get_alen(char *arg)
208 {
209         int     j;
210         int     alen;
211
212         alen = 1;
213         for (j = 0; j < 8; j++) {
214                 if (arg[j] == '.') {
215                         alen = arg[j+1] - '0';
216                         break;
217                 } else if (arg[j] == '\0')
218                         break;
219         }
220         return alen;
221 }
222
223 /**
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
229  *
230  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
231  * on error.
232  *
233  * Syntax:
234  *      i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
235  */
236 static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
237 {
238         u_char  chip;
239         uint    devaddr, alen, length;
240         u_char  *memaddr;
241
242         if (argc != 5)
243                 return CMD_RET_USAGE;
244
245         /*
246          * I2C chip address
247          */
248         chip = simple_strtoul(argv[1], NULL, 16);
249
250         /*
251          * I2C data address within the chip.  This can be 1 or
252          * 2 bytes long.  Some day it might be 3 bytes long :-).
253          */
254         devaddr = simple_strtoul(argv[2], NULL, 16);
255         alen = get_alen(argv[2]);
256         if (alen > 3)
257                 return CMD_RET_USAGE;
258
259         /*
260          * Length is the number of objects, not number of bytes.
261          */
262         length = simple_strtoul(argv[3], NULL, 16);
263
264         /*
265          * memaddr is the address where to store things in memory
266          */
267         memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
268
269         if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
270                 puts ("Error reading the chip.\n");
271                 return 1;
272         }
273         return 0;
274 }
275
276 static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
277 {
278         u_char  chip;
279         uint    devaddr, alen, length;
280         u_char  *memaddr;
281
282         if (argc != 5)
283                 return cmd_usage(cmdtp);
284
285         /*
286          * memaddr is the address where to store things in memory
287          */
288         memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
289
290         /*
291          * I2C chip address
292          */
293         chip = simple_strtoul(argv[2], NULL, 16);
294
295         /*
296          * I2C data address within the chip.  This can be 1 or
297          * 2 bytes long.  Some day it might be 3 bytes long :-).
298          */
299         devaddr = simple_strtoul(argv[3], NULL, 16);
300         alen = get_alen(argv[3]);
301         if (alen > 3)
302                 return cmd_usage(cmdtp);
303
304         /*
305          * Length is the number of objects, not number of bytes.
306          */
307         length = simple_strtoul(argv[4], NULL, 16);
308
309         while (length-- > 0) {
310                 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
311                         puts("Error writing to the chip.\n");
312                         return 1;
313                 }
314 /*
315  * No write delay with FRAM devices.
316  */
317 #if !defined(CONFIG_SYS_I2C_FRAM)
318                 udelay(11000);
319 #endif
320         }
321         return 0;
322 }
323
324 /**
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
330  *
331  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
332  * on error.
333  *
334  * Syntax:
335  *      i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
336  */
337 static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
338 {
339         u_char  chip;
340         uint    addr, alen, length;
341         int     j, nbytes, linebytes;
342
343         /* We use the last specified parameters, unless new ones are
344          * entered.
345          */
346         chip   = i2c_dp_last_chip;
347         addr   = i2c_dp_last_addr;
348         alen   = i2c_dp_last_alen;
349         length = i2c_dp_last_length;
350
351         if (argc < 3)
352                 return CMD_RET_USAGE;
353
354         if ((flag & CMD_FLAG_REPEAT) == 0) {
355                 /*
356                  * New command specified.
357                  */
358
359                 /*
360                  * I2C chip address
361                  */
362                 chip = simple_strtoul(argv[1], NULL, 16);
363
364                 /*
365                  * I2C data address within the chip.  This can be 1 or
366                  * 2 bytes long.  Some day it might be 3 bytes long :-).
367                  */
368                 addr = simple_strtoul(argv[2], NULL, 16);
369                 alen = get_alen(argv[2]);
370                 if (alen > 3)
371                         return CMD_RET_USAGE;
372
373                 /*
374                  * If another parameter, it is the length to display.
375                  * Length is the number of objects, not number of bytes.
376                  */
377                 if (argc > 3)
378                         length = simple_strtoul(argv[3], NULL, 16);
379         }
380
381         /*
382          * Print the lines.
383          *
384          * We buffer all read data, so we can make sure data is read only
385          * once.
386          */
387         nbytes = length;
388         do {
389                 unsigned char   linebuf[DISP_LINE_LEN];
390                 unsigned char   *cp;
391
392                 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
393
394                 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
395                         puts ("Error reading the chip.\n");
396                 else {
397                         printf("%04x:", addr);
398                         cp = linebuf;
399                         for (j=0; j<linebytes; j++) {
400                                 printf(" %02x", *cp++);
401                                 addr++;
402                         }
403                         puts ("    ");
404                         cp = linebuf;
405                         for (j=0; j<linebytes; j++) {
406                                 if ((*cp < 0x20) || (*cp > 0x7e))
407                                         puts (".");
408                                 else
409                                         printf("%c", *cp);
410                                 cp++;
411                         }
412                         putc ('\n');
413                 }
414                 nbytes -= linebytes;
415         } while (nbytes > 0);
416
417         i2c_dp_last_chip   = chip;
418         i2c_dp_last_addr   = addr;
419         i2c_dp_last_alen   = alen;
420         i2c_dp_last_length = length;
421
422         return 0;
423 }
424
425 /**
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
431  *
432  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
433  * on error.
434  *
435  * Syntax:
436  *      i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
437  */
438 static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
439 {
440         uchar   chip;
441         ulong   addr;
442         uint    alen;
443         uchar   byte;
444         int     count;
445
446         if ((argc < 4) || (argc > 5))
447                 return CMD_RET_USAGE;
448
449         /*
450          * Chip is always specified.
451          */
452         chip = simple_strtoul(argv[1], NULL, 16);
453
454         /*
455          * Address is always specified.
456          */
457         addr = simple_strtoul(argv[2], NULL, 16);
458         alen = get_alen(argv[2]);
459         if (alen > 3)
460                 return CMD_RET_USAGE;
461
462         /*
463          * Value to write is always specified.
464          */
465         byte = simple_strtoul(argv[3], NULL, 16);
466
467         /*
468          * Optional count
469          */
470         if (argc == 5)
471                 count = simple_strtoul(argv[4], NULL, 16);
472         else
473                 count = 1;
474
475         while (count-- > 0) {
476                 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
477                         puts ("Error writing the chip.\n");
478                 /*
479                  * Wait for the write to complete.  The write can take
480                  * up to 10mSec (we allow a little more time).
481                  */
482 /*
483  * No write delay with FRAM devices.
484  */
485 #if !defined(CONFIG_SYS_I2C_FRAM)
486                 udelay(11000);
487 #endif
488         }
489
490         return 0;
491 }
492
493 /**
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
499  *
500  * Calculate a CRC on memory
501  *
502  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
503  * on error.
504  *
505  * Syntax:
506  *      i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
507  */
508 static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
509 {
510         uchar   chip;
511         ulong   addr;
512         uint    alen;
513         int     count;
514         uchar   byte;
515         ulong   crc;
516         ulong   err;
517
518         if (argc < 4)
519                 return CMD_RET_USAGE;
520
521         /*
522          * Chip is always specified.
523          */
524         chip = simple_strtoul(argv[1], NULL, 16);
525
526         /*
527          * Address is always specified.
528          */
529         addr = simple_strtoul(argv[2], NULL, 16);
530         alen = get_alen(argv[2]);
531         if (alen > 3)
532                 return CMD_RET_USAGE;
533
534         /*
535          * Count is always specified
536          */
537         count = simple_strtoul(argv[3], NULL, 16);
538
539         printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
540         /*
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.
543          */
544         crc = 0;
545         err = 0;
546         while (count-- > 0) {
547                 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
548                         err++;
549                 crc = crc32 (crc, &byte, 1);
550                 addr++;
551         }
552         if (err > 0)
553                 puts ("Error reading the chip,\n");
554         else
555                 printf ("%08lx\n", crc);
556
557         return 0;
558 }
559
560 /**
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
566  *
567  * Modify memory.
568  *
569  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
570  * on error.
571  *
572  * Syntax:
573  *      i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
574  *      i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
575  */
576 static int
577 mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
578 {
579         uchar   chip;
580         ulong   addr;
581         uint    alen;
582         ulong   data;
583         int     size = 1;
584         int     nbytes;
585
586         if (argc != 3)
587                 return CMD_RET_USAGE;
588
589 #ifdef CONFIG_BOOT_RETRY_TIME
590         reset_cmd_timeout();    /* got a good command to get here */
591 #endif
592         /*
593          * We use the last specified parameters, unless new ones are
594          * entered.
595          */
596         chip = i2c_mm_last_chip;
597         addr = i2c_mm_last_addr;
598         alen = i2c_mm_last_alen;
599
600         if ((flag & CMD_FLAG_REPEAT) == 0) {
601                 /*
602                  * New command specified.  Check for a size specification.
603                  * Defaults to byte if no or incorrect specification.
604                  */
605                 size = cmd_get_data_size(argv[0], 1);
606
607                 /*
608                  * Chip is always specified.
609                  */
610                 chip = simple_strtoul(argv[1], NULL, 16);
611
612                 /*
613                  * Address is always specified.
614                  */
615                 addr = simple_strtoul(argv[2], NULL, 16);
616                 alen = get_alen(argv[2]);
617                 if (alen > 3)
618                         return CMD_RET_USAGE;
619         }
620
621         /*
622          * Print the address, followed by value.  Then accept input for
623          * the next value.  A non-converted value exits.
624          */
625         do {
626                 printf("%08lx:", addr);
627                 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
628                         puts ("\nError reading the chip,\n");
629                 else {
630                         data = cpu_to_be32(data);
631                         if (size == 1)
632                                 printf(" %02lx", (data >> 24) & 0x000000FF);
633                         else if (size == 2)
634                                 printf(" %04lx", (data >> 16) & 0x0000FFFF);
635                         else
636                                 printf(" %08lx", data);
637                 }
638
639                 nbytes = readline (" ? ");
640                 if (nbytes == 0) {
641                         /*
642                          * <CR> pressed as only input, don't modify current
643                          * location and move to next.
644                          */
645                         if (incrflag)
646                                 addr += size;
647                         nbytes = size;
648 #ifdef CONFIG_BOOT_RETRY_TIME
649                         reset_cmd_timeout(); /* good enough to not time out */
650 #endif
651                 }
652 #ifdef CONFIG_BOOT_RETRY_TIME
653                 else if (nbytes == -2)
654                         break;  /* timed out, exit the command  */
655 #endif
656                 else {
657                         char *endp;
658
659                         data = simple_strtoul(console_buffer, &endp, 16);
660                         if (size == 1)
661                                 data = data << 24;
662                         else if (size == 2)
663                                 data = data << 16;
664                         data = be32_to_cpu(data);
665                         nbytes = endp - console_buffer;
666                         if (nbytes) {
667 #ifdef CONFIG_BOOT_RETRY_TIME
668                                 /*
669                                  * good enough to not time out
670                                  */
671                                 reset_cmd_timeout();
672 #endif
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);
677 #endif
678                                 if (incrflag)
679                                         addr += size;
680                         }
681                 }
682         } while (nbytes);
683
684         i2c_mm_last_chip = chip;
685         i2c_mm_last_addr = addr;
686         i2c_mm_last_alen = alen;
687
688         return 0;
689 }
690
691 /**
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
697  *
698  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
699  * on error.
700  *
701  * Syntax:
702  *      i2c probe {addr}
703  *
704  * Returns zero (success) if one or more I2C devices was found
705  */
706 static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
707 {
708         int j;
709         int addr = -1;
710         int found = 0;
711 #if defined(CONFIG_SYS_I2C_NOPROBES)
712         int k, skip;
713         unsigned int bus = GET_BUS_NUM;
714 #endif  /* NOPROBES */
715
716         if (argc == 2)
717                 addr = simple_strtol(argv[1], 0, 16);
718
719         puts ("Valid chip addresses:");
720         for (j = 0; j < 128; j++) {
721                 if ((0 <= addr) && (j != addr))
722                         continue;
723
724 #if defined(CONFIG_SYS_I2C_NOPROBES)
725                 skip = 0;
726                 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
727                         if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
728                                 skip = 1;
729                                 break;
730                         }
731                 }
732                 if (skip)
733                         continue;
734 #endif
735                 if (i2c_probe(j) == 0) {
736                         printf(" %02X", j);
737                         found++;
738                 }
739         }
740         putc ('\n');
741
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));
747         }
748         putc ('\n');
749 #endif
750
751         return (0 == found);
752 }
753
754 /**
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
760  *
761  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
762  * on error.
763  *
764  * Syntax:
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
768  */
769 static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
770 {
771         u_char  chip;
772         ulong   alen;
773         uint    addr;
774         uint    length;
775         u_char  bytes[16];
776         int     delay;
777
778         if (argc < 3)
779                 return CMD_RET_USAGE;
780
781         /*
782          * Chip is always specified.
783          */
784         chip = simple_strtoul(argv[1], NULL, 16);
785
786         /*
787          * Address is always specified.
788          */
789         addr = simple_strtoul(argv[2], NULL, 16);
790         alen = get_alen(argv[2]);
791         if (alen > 3)
792                 return CMD_RET_USAGE;
793
794         /*
795          * Length is the number of objects, not number of bytes.
796          */
797         length = 1;
798         length = simple_strtoul(argv[3], NULL, 16);
799         if (length > sizeof(bytes))
800                 length = sizeof(bytes);
801
802         /*
803          * The delay time (uSec) is optional.
804          */
805         delay = 1000;
806         if (argc > 3)
807                 delay = simple_strtoul(argv[4], NULL, 10);
808         /*
809          * Run the loop...
810          */
811         while (1) {
812                 if (i2c_read(chip, addr, alen, bytes, length) != 0)
813                         puts ("Error reading the chip.\n");
814                 udelay(delay);
815         }
816
817         /* NOTREACHED */
818         return 0;
819 }
820
821 /*
822  * The SDRAM command is separately configured because many
823  * (most?) embedded boards don't use SDRAM DIMMs.
824  *
825  * FIXME: Document and probably move elsewhere!
826  */
827 #if defined(CONFIG_CMD_SDRAM)
828 static void print_ddr2_tcyc (u_char const b)
829 {
830         printf ("%d.", (b >> 4) & 0x0F);
831         switch (b & 0x0F) {
832         case 0x0:
833         case 0x1:
834         case 0x2:
835         case 0x3:
836         case 0x4:
837         case 0x5:
838         case 0x6:
839         case 0x7:
840         case 0x8:
841         case 0x9:
842                 printf ("%d ns\n", b & 0x0F);
843                 break;
844         case 0xA:
845                 puts ("25 ns\n");
846                 break;
847         case 0xB:
848                 puts ("33 ns\n");
849                 break;
850         case 0xC:
851                 puts ("66 ns\n");
852                 break;
853         case 0xD:
854                 puts ("75 ns\n");
855                 break;
856         default:
857                 puts ("?? ns\n");
858                 break;
859         }
860 }
861
862 static void decode_bits (u_char const b, char const *str[], int const do_once)
863 {
864         u_char mask;
865
866         for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
867                 if (b & mask) {
868                         puts (*str);
869                         if (do_once)
870                                 return;
871                 }
872         }
873 }
874
875 /*
876  * Syntax:
877  *      i2c sdram {i2c_chip}
878  */
879 static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
880 {
881         enum { unknown, EDO, SDRAM, DDR2 } type;
882
883         u_char  chip;
884         u_char  data[128];
885         u_char  cksum;
886         int     j;
887
888         static const char *decode_CAS_DDR2[] = {
889                 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
890         };
891
892         static const char *decode_CAS_default[] = {
893                 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
894         };
895
896         static const char *decode_CS_WE_default[] = {
897                 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
898         };
899
900         static const char *decode_byte21_default[] = {
901                 "  TBD (bit 7)\n",
902                 "  Redundant row address\n",
903                 "  Differential clock input\n",
904                 "  Registerd DQMB inputs\n",
905                 "  Buffered DQMB inputs\n",
906                 "  On-card PLL\n",
907                 "  Registered address/control lines\n",
908                 "  Buffered address/control lines\n"
909         };
910
911         static const char *decode_byte22_DDR2[] = {
912                 "  TBD (bit 7)\n",
913                 "  TBD (bit 6)\n",
914                 "  TBD (bit 5)\n",
915                 "  TBD (bit 4)\n",
916                 "  TBD (bit 3)\n",
917                 "  Supports partial array self refresh\n",
918                 "  Supports 50 ohm ODT\n",
919                 "  Supports weak driver\n"
920         };
921
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"
925         };
926
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"
930         };
931
932         if (argc < 2)
933                 return CMD_RET_USAGE;
934
935         /*
936          * Chip is always specified.
937          */
938         chip = simple_strtoul (argv[1], NULL, 16);
939
940         if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
941                 puts ("No SDRAM Serial Presence Detect found.\n");
942                 return 1;
943         }
944
945         cksum = 0;
946         for (j = 0; j < 63; j++) {
947                 cksum += data[j];
948         }
949         if (cksum != data[63]) {
950                 printf ("WARNING: Configuration data checksum failure:\n"
951                         "  is 0x%02x, calculated 0x%02x\n", data[63], cksum);
952         }
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]);
957
958         puts ("Memory type                  ");
959         switch (data[2]) {
960         case 2:
961                 type = EDO;
962                 puts ("EDO\n");
963                 break;
964         case 4:
965                 type = SDRAM;
966                 puts ("SDRAM\n");
967                 break;
968         case 8:
969                 type = DDR2;
970                 puts ("DDR2\n");
971                 break;
972         default:
973                 type = unknown;
974                 puts ("unknown\n");
975                 break;
976         }
977
978         puts ("Row address bits             ");
979         if ((data[3] & 0x00F0) == 0)
980                 printf ("%d\n", data[3] & 0x0F);
981         else
982                 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
983
984         puts ("Column address bits          ");
985         if ((data[4] & 0x00F0) == 0)
986                 printf ("%d\n", data[4] & 0x0F);
987         else
988                 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
989
990         switch (type) {
991         case DDR2:
992                 printf ("Number of ranks              %d\n",
993                         (data[5] & 0x07) + 1);
994                 break;
995         default:
996                 printf ("Module rows                  %d\n", data[5]);
997                 break;
998         }
999
1000         switch (type) {
1001         case DDR2:
1002                 printf ("Module data width            %d bits\n", data[6]);
1003                 break;
1004         default:
1005                 printf ("Module data width            %d bits\n",
1006                         (data[7] << 8) | data[6]);
1007                 break;
1008         }
1009
1010         puts ("Interface signal levels      ");
1011         switch(data[8]) {
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;
1019         }
1020
1021         switch (type) {
1022         case DDR2:
1023                 printf ("SDRAM cycle time             ");
1024                 print_ddr2_tcyc (data[9]);
1025                 break;
1026         default:
1027                 printf ("SDRAM cycle time             %d.%d ns\n",
1028                         (data[9] >> 4) & 0x0F, data[9] & 0x0F);
1029                 break;
1030         }
1031
1032         switch (type) {
1033         case DDR2:
1034                 printf ("SDRAM access time            0.%d%d ns\n",
1035                         (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1036                 break;
1037         default:
1038                 printf ("SDRAM access time            %d.%d ns\n",
1039                         (data[10] >> 4) & 0x0F, data[10] & 0x0F);
1040                 break;
1041         }
1042
1043         puts ("EDC configuration            ");
1044         switch (data[11]) {
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;
1049         }
1050
1051         if ((data[12] & 0x80) == 0)
1052                 puts ("No self refresh, rate        ");
1053         else
1054                 puts ("Self refresh, rate           ");
1055
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;
1064         }
1065
1066         switch (type) {
1067         case DDR2:
1068                 printf ("SDRAM width (primary)        %d\n", data[13]);
1069                 break;
1070         default:
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));
1075                 }
1076                 break;
1077         }
1078
1079         switch (type) {
1080         case DDR2:
1081                 if (data[14] != 0)
1082                         printf ("EDC width                    %d\n", data[14]);
1083                 break;
1084         default:
1085                 if (data[14] != 0) {
1086                         printf ("EDC width                    %d\n",
1087                                 data[14] & 0x7F);
1088
1089                         if ((data[14] & 0x80) != 0) {
1090                                 printf ("  (second bank)              %d\n",
1091                                         2 * (data[14] & 0x7F));
1092                         }
1093                 }
1094                 break;
1095         }
1096
1097         if (DDR2 != type) {
1098                 printf ("Min clock delay, back-to-back random column addresses "
1099                         "%d\n", data[15]);
1100         }
1101
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");
1108         putc ('\n');
1109         printf ("Number of banks              %d\n", data[17]);
1110
1111         switch (type) {
1112         case DDR2:
1113                 puts ("CAS latency(s)              ");
1114                 decode_bits (data[18], decode_CAS_DDR2, 0);
1115                 putc ('\n');
1116                 break;
1117         default:
1118                 puts ("CAS latency(s)              ");
1119                 decode_bits (data[18], decode_CAS_default, 0);
1120                 putc ('\n');
1121                 break;
1122         }
1123
1124         if (DDR2 != type) {
1125                 puts ("CS latency(s)               ");
1126                 decode_bits (data[19], decode_CS_WE_default, 0);
1127                 putc ('\n');
1128         }
1129
1130         if (DDR2 != type) {
1131                 puts ("WE latency(s)               ");
1132                 decode_bits (data[20], decode_CS_WE_default, 0);
1133                 putc ('\n');
1134         }
1135
1136         switch (type) {
1137         case DDR2:
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);
1151                 }
1152                 break;
1153         default:
1154                 puts ("Module attributes:\n");
1155                 if (!data[21])
1156                         puts ("  (none)\n");
1157                 else
1158                         decode_bits (data[21], decode_byte21_default, 0);
1159                 break;
1160         }
1161
1162         switch (type) {
1163         case DDR2:
1164                 decode_bits (data[22], decode_byte22_DDR2, 0);
1165                 break;
1166         default:
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");
1178                 break;
1179         }
1180
1181         switch (type) {
1182         case DDR2:
1183                 printf ("SDRAM cycle time (2nd highest CAS latency)        ");
1184                 print_ddr2_tcyc (data[23]);
1185                 break;
1186         default:
1187                 printf ("SDRAM cycle time (2nd highest CAS latency)        %d."
1188                         "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
1189                 break;
1190         }
1191
1192         switch (type) {
1193         case DDR2:
1194                 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1195                         "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1196                 break;
1197         default:
1198                 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1199                         "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
1200                 break;
1201         }
1202
1203         switch (type) {
1204         case DDR2:
1205                 printf ("SDRAM cycle time (3rd highest CAS latency)        ");
1206                 print_ddr2_tcyc (data[25]);
1207                 break;
1208         default:
1209                 printf ("SDRAM cycle time (3rd highest CAS latency)        %d."
1210                         "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
1211                 break;
1212         }
1213
1214         switch (type) {
1215         case DDR2:
1216                 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1217                         "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1218                 break;
1219         default:
1220                 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1221                         "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
1222                 break;
1223         }
1224
1225         switch (type) {
1226         case DDR2:
1227                 printf ("Minimum row precharge        %d.%02d ns\n",
1228                         (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
1229                 break;
1230         default:
1231                 printf ("Minimum row precharge        %d ns\n", data[27]);
1232                 break;
1233         }
1234
1235         switch (type) {
1236         case DDR2:
1237                 printf ("Row active to row active min %d.%02d ns\n",
1238                         (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
1239                 break;
1240         default:
1241                 printf ("Row active to row active min %d ns\n", data[28]);
1242                 break;
1243         }
1244
1245         switch (type) {
1246         case DDR2:
1247                 printf ("RAS to CAS delay min         %d.%02d ns\n",
1248                         (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
1249                 break;
1250         default:
1251                 printf ("RAS to CAS delay min         %d ns\n", data[29]);
1252                 break;
1253         }
1254
1255         printf ("Minimum RAS pulse width      %d ns\n", data[30]);
1256
1257         switch (type) {
1258         case DDR2:
1259                 puts ("Density of each row          ");
1260                 decode_bits (data[31], decode_row_density_DDR2, 1);
1261                 putc ('\n');
1262                 break;
1263         default:
1264                 puts ("Density of each row          ");
1265                 decode_bits (data[31], decode_row_density_default, 1);
1266                 putc ('\n');
1267                 break;
1268         }
1269
1270         switch (type) {
1271         case DDR2:
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);
1276                 } else {
1277                         printf ("0.%d%d ns\n",
1278                                 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
1279                 }
1280                 break;
1281         default:
1282                 printf ("Command and Address setup    %c%d.%d ns\n",
1283                         (data[32] & 0x80) ? '-' : '+',
1284                         (data[32] >> 4) & 0x07, data[32] & 0x0F);
1285                 break;
1286         }
1287
1288         switch (type) {
1289         case DDR2:
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);
1294                 } else {
1295                         printf ("0.%d%d ns\n",
1296                                 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
1297                 }
1298                 break;
1299         default:
1300                 printf ("Command and Address hold     %c%d.%d ns\n",
1301                         (data[33] & 0x80) ? '-' : '+',
1302                         (data[33] >> 4) & 0x07, data[33] & 0x0F);
1303                 break;
1304         }
1305
1306         switch (type) {
1307         case DDR2:
1308                 printf ("Data signal input setup      0.%d%d ns\n",
1309                         (data[34] >> 4) & 0x0F, data[34] & 0x0F);
1310                 break;
1311         default:
1312                 printf ("Data signal input setup      %c%d.%d ns\n",
1313                         (data[34] & 0x80) ? '-' : '+',
1314                         (data[34] >> 4) & 0x07, data[34] & 0x0F);
1315                 break;
1316         }
1317
1318         switch (type) {
1319         case DDR2:
1320                 printf ("Data signal input hold       0.%d%d ns\n",
1321                         (data[35] >> 4) & 0x0F, data[35] & 0x0F);
1322                 break;
1323         default:
1324                 printf ("Data signal input hold       %c%d.%d ns\n",
1325                         (data[35] & 0x80) ? '-' : '+',
1326                         (data[35] >> 4) & 0x07, data[35] & 0x0F);
1327                 break;
1328         }
1329
1330         puts ("Manufacturer's JEDEC ID      ");
1331         for (j = 64; j <= 71; j++)
1332                 printf ("%02X ", data[j]);
1333         putc ('\n');
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]);
1338         putc ('\n');
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]);
1344         putc ('\n');
1345
1346         if (DDR2 != type) {
1347                 printf ("Speed rating                 PC%d\n",
1348                         data[126] == 0x66 ? 66 : data[126]);
1349         }
1350         return 0;
1351 }
1352 #endif
1353
1354 /*
1355  * Syntax:
1356  *      i2c edid {i2c_chip}
1357  */
1358 #if defined(CONFIG_I2C_EDID)
1359 int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1360 {
1361         u_char chip;
1362         struct edid1_info edid;
1363
1364         if (argc < 2) {
1365                 cmd_usage(cmdtp);
1366                 return 1;
1367         }
1368
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");
1372                 return 1;
1373         }
1374
1375         if (edid_check_info(&edid)) {
1376                 puts("Content isn't valid EDID.\n");
1377                 return 1;
1378         }
1379
1380         edid_print_info(&edid);
1381         return 0;
1382
1383 }
1384 #endif /* CONFIG_I2C_EDID */
1385
1386 /**
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
1392  *
1393  * Returns zero always.
1394  */
1395 #if defined(CONFIG_SYS_I2C)
1396 int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1397 {
1398         int     i;
1399 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
1400         int     j;
1401 #endif
1402
1403         if (argc == 1) {
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)
1410                                         break;
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);
1415                         }
1416 #endif
1417                         printf("\n");
1418                 }
1419         } else {
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);
1424                         return -1;
1425                 }
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)
1430                                         break;
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);
1435                         }
1436 #endif
1437                 printf("\n");
1438         }
1439
1440         return 0;
1441 }
1442 #endif
1443
1444 /**
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
1450  *
1451  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1452  * on error.
1453  */
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[])
1456 {
1457         int             ret = 0;
1458         unsigned int    bus_no;
1459
1460         if (argc == 1)
1461                 /* querying current setting */
1462                 printf("Current bus is %d\n", i2c_get_bus_num());
1463         else {
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);
1467                         return -1;
1468                 }
1469                 printf("Setting bus to %d\n", bus_no);
1470                 ret = i2c_set_bus_num(bus_no);
1471                 if (ret)
1472                         printf("Failure changing bus number (%d)\n", ret);
1473         }
1474         return ret;
1475 }
1476 #endif  /* defined(CONFIG_SYS_I2C) */
1477
1478 /**
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
1484  *
1485  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1486  * on error.
1487  */
1488 static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1489 {
1490         int speed, ret=0;
1491
1492         if (argc == 1)
1493                 /* querying current speed */
1494                 printf("Current bus speed=%d\n", i2c_get_bus_speed());
1495         else {
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);
1499                 if (ret)
1500                         printf("Failure changing bus speed (%d)\n", ret);
1501         }
1502         return ret;
1503 }
1504
1505 /**
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
1511  *
1512  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1513  * on error.
1514  */
1515 static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1516 {
1517         return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1518 }
1519
1520 /**
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
1526  *
1527  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1528  * on error.
1529  */
1530 static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1531 {
1532         return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1533 }
1534
1535 /**
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
1541  *
1542  * Returns zero always.
1543  */
1544 static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1545 {
1546 #if defined(CONFIG_SYS_I2C)
1547         i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1548 #else
1549         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
1550 #endif
1551         return 0;
1552 }
1553
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, "", ""),
1577 #endif
1578         U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1579 };
1580
1581 #ifdef CONFIG_NEEDS_MANUAL_RELOC
1582 void i2c_reloc(void) {
1583         fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1584 }
1585 #endif
1586
1587 /**
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
1593  *
1594  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1595  * on error.
1596  */
1597 static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1598 {
1599         cmd_tbl_t *c;
1600
1601         if (argc < 2)
1602                 return CMD_RET_USAGE;
1603
1604         /* Strip off leading 'i2c' command argument */
1605         argc--;
1606         argv++;
1607
1608         c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1609
1610         if (c)
1611                 return c->cmd(cmdtp, flag, argc, argv);
1612         else
1613                 return CMD_RET_USAGE;
1614 }
1615
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"
1641 #endif
1642         "i2c speed [speed] - show or set I2C bus speed";
1643 #endif
1644
1645 U_BOOT_CMD(
1646         i2c, 6, 1, do_i2c,
1647         "I2C sub-system",
1648         i2c_help_text
1649 );
1650
1651 #if defined(CONFIG_I2C_MUX)
1652 static int i2c_mux_add_device(I2C_MUX_DEVICE *dev)
1653 {
1654         I2C_MUX_DEVICE  *devtmp = i2c_mux_devices;
1655
1656         if (i2c_mux_devices == NULL) {
1657                 i2c_mux_devices = dev;
1658                 return 0;
1659         }
1660         while (devtmp->next != NULL)
1661                 devtmp = devtmp->next;
1662
1663         devtmp->next = dev;
1664         return 0;
1665 }
1666
1667 I2C_MUX_DEVICE  *i2c_mux_search_device(int id)
1668 {
1669         I2C_MUX_DEVICE  *device = i2c_mux_devices;
1670
1671         while (device != NULL) {
1672                 if (device->busid == id)
1673                         return device;
1674                 device = device->next;
1675         }
1676         return NULL;
1677 }
1678
1679 /* searches in the buf from *pos the next ':'.
1680  * returns:
1681  *     0 if found (with *pos = where)
1682  *   < 0 if an error occured
1683  *   > 0 if the end of buf is reached
1684  */
1685 static int i2c_mux_search_next (int *pos, uchar *buf, int len)
1686 {
1687         while ((buf[*pos] != ':') && (*pos < len)) {
1688                 *pos += 1;
1689         }
1690         if (*pos >= len)
1691                 return 1;
1692         if (buf[*pos] != ':')
1693                 return -1;
1694         return 0;
1695 }
1696
1697 static int i2c_mux_get_busid (void)
1698 {
1699         int     tmp = i2c_mux_busid;
1700
1701         i2c_mux_busid ++;
1702         return tmp;
1703 }
1704
1705 /* Analyses a Muxstring and immediately sends the
1706    commands to the muxes. Runs from flash.
1707  */
1708 int i2c_mux_ident_muxstring_f (uchar *buf)
1709 {
1710         int     pos = 0;
1711         int     oldpos;
1712         int     ret = 0;
1713         int     len = strlen((char *)buf);
1714         int     chip;
1715         uchar   channel;
1716         int     was = 0;
1717
1718         while (ret == 0) {
1719                 oldpos = pos;
1720                 /* search name */
1721                 ret = i2c_mux_search_next(&pos, buf, len);
1722                 if (ret != 0)
1723                         printf ("ERROR\n");
1724                 /* search address */
1725                 pos ++;
1726                 oldpos = pos;
1727                 ret = i2c_mux_search_next(&pos, buf, len);
1728                 if (ret != 0)
1729                         printf ("ERROR\n");
1730                 buf[pos] = 0;
1731                 chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1732                 buf[pos] = ':';
1733                 /* search channel */
1734                 pos ++;
1735                 oldpos = pos;
1736                 ret = i2c_mux_search_next(&pos, buf, len);
1737                 if (ret < 0)
1738                         printf ("ERROR\n");
1739                 was = 0;
1740                 if (buf[pos] != 0) {
1741                         buf[pos] = 0;
1742                         was = 1;
1743                 }
1744                 channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1745                 if (was)
1746                         buf[pos] = ':';
1747                 if (i2c_write(chip, 0, 0, &channel, 1) != 0) {
1748                         printf ("Error setting Mux: chip:%x channel: \
1749                                 %x\n", chip, channel);
1750                         return -1;
1751                 }
1752                 pos ++;
1753                 oldpos = pos;
1754
1755         }
1756         i2c_init_board();
1757
1758         return 0;
1759 }
1760
1761 /* Analyses a Muxstring and if this String is correct
1762  * adds a new I2C Bus.
1763  */
1764 I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf)
1765 {
1766         I2C_MUX_DEVICE  *device;
1767         I2C_MUX         *mux;
1768         int     pos = 0;
1769         int     oldpos;
1770         int     ret = 0;
1771         int     len = strlen((char *)buf);
1772         int     was = 0;
1773
1774         device = (I2C_MUX_DEVICE *)malloc (sizeof(I2C_MUX_DEVICE));
1775         device->mux = NULL;
1776         device->busid = i2c_mux_get_busid ();
1777         device->next = NULL;
1778         while (ret == 0) {
1779                 mux = (I2C_MUX *)malloc (sizeof(I2C_MUX));
1780                 mux->next = NULL;
1781                 /* search name of mux */
1782                 oldpos = pos;
1783                 ret = i2c_mux_search_next(&pos, buf, len);
1784                 if (ret != 0)
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 */
1790                 pos ++;
1791                 oldpos = pos;
1792                 ret = i2c_mux_search_next(&pos, buf, len);
1793                 if (ret != 0)
1794                         printf ("%s no mux address.\n", __FUNCTION__);
1795                 buf[pos] = 0;
1796                 mux->chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1797                 buf[pos] = ':';
1798                 /* search channel */
1799                 pos ++;
1800                 oldpos = pos;
1801                 ret = i2c_mux_search_next(&pos, buf, len);
1802                 if (ret < 0)
1803                         printf ("%s no mux channel.\n", __FUNCTION__);
1804                 was = 0;
1805                 if (buf[pos] != 0) {
1806                         buf[pos] = 0;
1807                         was = 1;
1808                 }
1809                 mux->channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1810                 if (was)
1811                         buf[pos] = ':';
1812                 if (device->mux == NULL)
1813                         device->mux = mux;
1814                 else {
1815                         I2C_MUX         *muxtmp = device->mux;
1816                         while (muxtmp->next != NULL) {
1817                                 muxtmp = muxtmp->next;
1818                         }
1819                         muxtmp->next = mux;
1820                 }
1821                 pos ++;
1822                 oldpos = pos;
1823         }
1824         if (ret > 0) {
1825                 /* Add Device */
1826                 i2c_mux_add_device (device);
1827                 return device;
1828         }
1829
1830         return NULL;
1831 }
1832
1833 int i2x_mux_select_mux(int bus)
1834 {
1835         I2C_MUX_DEVICE  *dev;
1836         I2C_MUX         *mux;
1837
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);
1842 #else
1843                 {
1844                 unsigned char *buf;
1845                 buf = (unsigned char *) getenv("EEprom_ivm");
1846                 if (buf != NULL)
1847                         i2c_mux_ident_muxstring_f (buf);
1848                 }
1849 #endif
1850                 return 0;
1851         }
1852         dev = i2c_mux_search_device(bus);
1853         if (dev == NULL)
1854                 return -1;
1855
1856         mux = dev->mux;
1857         while (mux != NULL) {
1858                 /* do deblocking on each level of mux, before mux config */
1859                 i2c_init_board();
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);
1863                         return -1;
1864                 }
1865                 mux = mux->next;
1866         }
1867         /* do deblocking on each level of mux and after mux config */
1868         i2c_init_board();
1869         return 0;
1870 }
1871 #endif /* CONFIG_I2C_MUX */