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