Add support for multiple I2C buses
[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  * Two of the commands (imm and imw) take a byte/word/long modifier
31  * (e.g. imm.w specifies the word-length modifier).  This was done to
32  * allow manipulating word-length registers.  It was not done on any other
33  * commands because it was not deemed useful.
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  *   CFG_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  *     imd 50 0 10      display 16 bytes starting at 0x000
73  *                      On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
74  *     imd 50 100 10    display 16 bytes starting at 0x100
75  *                      On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
76  *     imd 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 <i2c.h>
87 #include <asm/byteorder.h>
88
89 #if (CONFIG_COMMANDS & CFG_CMD_I2C)
90
91
92 /* Display values from last command.
93  * Memory modify remembered values are different from display memory.
94  */
95 static uchar    i2c_dp_last_chip;
96 static uint     i2c_dp_last_addr;
97 static uint     i2c_dp_last_alen;
98 static uint     i2c_dp_last_length = 0x10;
99
100 static uchar    i2c_mm_last_chip;
101 static uint     i2c_mm_last_addr;
102 static uint     i2c_mm_last_alen;
103
104 /* If only one I2C bus is present, the list of devices to ignore when
105  * the probe command is issued is represented by a 1D array of addresses.
106  * When multiple buses are present, the list is an array of bus-address
107  * pairs.  The following macros take care of this */
108
109 #if defined(CFG_I2C_NOPROBES)
110 #if defined(CONFIG_I2C_MULTI_BUS)
111 static struct
112 {
113         uchar   bus;
114         uchar   addr;
115 } i2c_no_probes[] = CFG_I2C_NOPROBES;
116 #define GET_BUS_NUM     i2c_get_bus_num()
117 #define COMPARE_BUS(b,i)        (i2c_no_probes[(i)].bus == (b))
118 #define COMPARE_ADDR(a,i)       (i2c_no_probes[(i)].addr == (a))
119 #define NO_PROBE_ADDR(i)        i2c_no_probes[(i)].addr
120 #else           /* single bus */
121 static uchar i2c_no_probes[] = CFG_I2C_NOPROBES;
122 #define GET_BUS_NUM     0
123 #define COMPARE_BUS(b,i)        ((b) == 0)      /* Make compiler happy */
124 #define COMPARE_ADDR(a,i)       (i2c_no_probes[(i)] == (a))
125 #define NO_PROBE_ADDR(i)        i2c_no_probes[(i)]
126 #endif  /* CONFIG_MULTI_BUS */
127
128 #define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
129 #endif
130
131 static int
132 mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[]);
133 extern int cmd_get_data_size(char* arg, int default_size);
134
135 /*
136  * Syntax:
137  *      imd {i2c_chip} {addr}{.0, .1, .2} {len}
138  */
139 #define DISP_LINE_LEN   16
140
141 int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
142 {
143         u_char  chip;
144         uint    addr, alen, length;
145         int     j, nbytes, linebytes;
146
147         /* We use the last specified parameters, unless new ones are
148          * entered.
149          */
150         chip   = i2c_dp_last_chip;
151         addr   = i2c_dp_last_addr;
152         alen   = i2c_dp_last_alen;
153         length = i2c_dp_last_length;
154
155         if (argc < 3) {
156                 printf ("Usage:\n%s\n", cmdtp->usage);
157                 return 1;
158         }
159
160         if ((flag & CMD_FLAG_REPEAT) == 0) {
161                 /*
162                  * New command specified.
163                  */
164                 alen = 1;
165
166                 /*
167                  * I2C chip address
168                  */
169                 chip = simple_strtoul(argv[1], NULL, 16);
170
171                 /*
172                  * I2C data address within the chip.  This can be 1 or
173                  * 2 bytes long.  Some day it might be 3 bytes long :-).
174                  */
175                 addr = simple_strtoul(argv[2], NULL, 16);
176                 alen = 1;
177                 for(j = 0; j < 8; j++) {
178                         if (argv[2][j] == '.') {
179                                 alen = argv[2][j+1] - '0';
180                                 if (alen > 4) {
181                                         printf ("Usage:\n%s\n", cmdtp->usage);
182                                         return 1;
183                                 }
184                                 break;
185                         } else if (argv[2][j] == '\0') {
186                                 break;
187                         }
188                 }
189
190                 /*
191                  * If another parameter, it is the length to display.
192                  * Length is the number of objects, not number of bytes.
193                  */
194                 if (argc > 3)
195                         length = simple_strtoul(argv[3], NULL, 16);
196         }
197
198         /*
199          * Print the lines.
200          *
201          * We buffer all read data, so we can make sure data is read only
202          * once.
203          */
204         nbytes = length;
205         do {
206                 unsigned char   linebuf[DISP_LINE_LEN];
207                 unsigned char   *cp;
208
209                 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
210
211                 if(i2c_read(chip, addr, alen, linebuf, linebytes) != 0) {
212                         puts ("Error reading the chip.\n");
213                 } else {
214                         printf("%04x:", addr);
215                         cp = linebuf;
216                         for (j=0; j<linebytes; j++) {
217                                 printf(" %02x", *cp++);
218                                 addr++;
219                         }
220                         puts ("    ");
221                         cp = linebuf;
222                         for (j=0; j<linebytes; j++) {
223                                 if ((*cp < 0x20) || (*cp > 0x7e))
224                                         puts (".");
225                                 else
226                                         printf("%c", *cp);
227                                 cp++;
228                         }
229                         putc ('\n');
230                 }
231                 nbytes -= linebytes;
232         } while (nbytes > 0);
233
234         i2c_dp_last_chip   = chip;
235         i2c_dp_last_addr   = addr;
236         i2c_dp_last_alen   = alen;
237         i2c_dp_last_length = length;
238
239         return 0;
240 }
241
242 int do_i2c_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
243 {
244         return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
245 }
246
247
248 int do_i2c_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
249 {
250         return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
251 }
252
253 /* Write (fill) memory
254  *
255  * Syntax:
256  *      imw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
257  */
258 int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
259 {
260         uchar   chip;
261         ulong   addr;
262         uint    alen;
263         uchar   byte;
264         int     count;
265         int     j;
266
267         if ((argc < 4) || (argc > 5)) {
268                 printf ("Usage:\n%s\n", cmdtp->usage);
269                 return 1;
270         }
271
272         /*
273          * Chip is always specified.
274          */
275         chip = simple_strtoul(argv[1], NULL, 16);
276
277         /*
278          * Address is always specified.
279          */
280         addr = simple_strtoul(argv[2], NULL, 16);
281         alen = 1;
282         for(j = 0; j < 8; j++) {
283                 if (argv[2][j] == '.') {
284                         alen = argv[2][j+1] - '0';
285                         if(alen > 4) {
286                                 printf ("Usage:\n%s\n", cmdtp->usage);
287                                 return 1;
288                         }
289                         break;
290                 } else if (argv[2][j] == '\0') {
291                         break;
292                 }
293         }
294
295         /*
296          * Value to write is always specified.
297          */
298         byte = simple_strtoul(argv[3], NULL, 16);
299
300         /*
301          * Optional count
302          */
303         if(argc == 5) {
304                 count = simple_strtoul(argv[4], NULL, 16);
305         } else {
306                 count = 1;
307         }
308
309         while (count-- > 0) {
310                 if(i2c_write(chip, addr++, alen, &byte, 1) != 0) {
311                         puts ("Error writing the chip.\n");
312                 }
313                 /*
314                  * Wait for the write to complete.  The write can take
315                  * up to 10mSec (we allow a little more time).
316                  *
317                  * On some chips, while the write is in progress, the
318                  * chip doesn't respond.  This apparently isn't a
319                  * universal feature so we don't take advantage of it.
320                  */
321 /*
322  * No write delay with FRAM devices.
323  */
324 #if !defined(CFG_I2C_FRAM)
325                 udelay(11000);
326 #endif
327
328 #if 0
329                 for(timeout = 0; timeout < 10; timeout++) {
330                         udelay(2000);
331                         if(i2c_probe(chip) == 0)
332                                 break;
333                 }
334 #endif
335         }
336
337         return (0);
338 }
339
340
341 /* Calculate a CRC on memory
342  *
343  * Syntax:
344  *      icrc32 {i2c_chip} {addr}{.0, .1, .2} {count}
345  */
346 int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
347 {
348         uchar   chip;
349         ulong   addr;
350         uint    alen;
351         int     count;
352         uchar   byte;
353         ulong   crc;
354         ulong   err;
355         int     j;
356
357         if (argc < 4) {
358                 printf ("Usage:\n%s\n", cmdtp->usage);
359                 return 1;
360         }
361
362         /*
363          * Chip is always specified.
364          */
365         chip = simple_strtoul(argv[1], NULL, 16);
366
367         /*
368          * Address is always specified.
369          */
370         addr = simple_strtoul(argv[2], NULL, 16);
371         alen = 1;
372         for(j = 0; j < 8; j++) {
373                 if (argv[2][j] == '.') {
374                         alen = argv[2][j+1] - '0';
375                         if(alen > 4) {
376                                 printf ("Usage:\n%s\n", cmdtp->usage);
377                                 return 1;
378                         }
379                         break;
380                 } else if (argv[2][j] == '\0') {
381                         break;
382                 }
383         }
384
385         /*
386          * Count is always specified
387          */
388         count = simple_strtoul(argv[3], NULL, 16);
389
390         printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
391         /*
392          * CRC a byte at a time.  This is going to be slooow, but hey, the
393          * memories are small and slow too so hopefully nobody notices.
394          */
395         crc = 0;
396         err = 0;
397         while(count-- > 0) {
398                 if(i2c_read(chip, addr, alen, &byte, 1) != 0) {
399                         err++;
400                 }
401                 crc = crc32 (crc, &byte, 1);
402                 addr++;
403         }
404         if(err > 0)
405         {
406                 puts ("Error reading the chip,\n");
407         } else {
408                 printf ("%08lx\n", crc);
409         }
410
411         return 0;
412 }
413
414
415 /* Modify memory.
416  *
417  * Syntax:
418  *      imm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
419  *      inm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
420  */
421
422 static int
423 mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
424 {
425         uchar   chip;
426         ulong   addr;
427         uint    alen;
428         ulong   data;
429         int     size = 1;
430         int     nbytes;
431         int     j;
432         extern char console_buffer[];
433
434         if (argc != 3) {
435                 printf ("Usage:\n%s\n", cmdtp->usage);
436                 return 1;
437         }
438
439 #ifdef CONFIG_BOOT_RETRY_TIME
440         reset_cmd_timeout();    /* got a good command to get here */
441 #endif
442         /*
443          * We use the last specified parameters, unless new ones are
444          * entered.
445          */
446         chip = i2c_mm_last_chip;
447         addr = i2c_mm_last_addr;
448         alen = i2c_mm_last_alen;
449
450         if ((flag & CMD_FLAG_REPEAT) == 0) {
451                 /*
452                  * New command specified.  Check for a size specification.
453                  * Defaults to byte if no or incorrect specification.
454                  */
455                 size = cmd_get_data_size(argv[0], 1);
456
457                 /*
458                  * Chip is always specified.
459                  */
460                 chip = simple_strtoul(argv[1], NULL, 16);
461
462                 /*
463                  * Address is always specified.
464                  */
465                 addr = simple_strtoul(argv[2], NULL, 16);
466                 alen = 1;
467                 for(j = 0; j < 8; j++) {
468                         if (argv[2][j] == '.') {
469                                 alen = argv[2][j+1] - '0';
470                                 if(alen > 4) {
471                                         printf ("Usage:\n%s\n", cmdtp->usage);
472                                         return 1;
473                                 }
474                                 break;
475                         } else if (argv[2][j] == '\0') {
476                                 break;
477                         }
478                 }
479         }
480
481         /*
482          * Print the address, followed by value.  Then accept input for
483          * the next value.  A non-converted value exits.
484          */
485         do {
486                 printf("%08lx:", addr);
487                 if(i2c_read(chip, addr, alen, (uchar *)&data, size) != 0) {
488                         puts ("\nError reading the chip,\n");
489                 } else {
490                         data = cpu_to_be32(data);
491                         if(size == 1) {
492                                 printf(" %02lx", (data >> 24) & 0x000000FF);
493                         } else if(size == 2) {
494                                 printf(" %04lx", (data >> 16) & 0x0000FFFF);
495                         } else {
496                                 printf(" %08lx", data);
497                         }
498                 }
499
500                 nbytes = readline (" ? ");
501                 if (nbytes == 0) {
502                         /*
503                          * <CR> pressed as only input, don't modify current
504                          * location and move to next.
505                          */
506                         if (incrflag)
507                                 addr += size;
508                         nbytes = size;
509 #ifdef CONFIG_BOOT_RETRY_TIME
510                         reset_cmd_timeout(); /* good enough to not time out */
511 #endif
512                 }
513 #ifdef CONFIG_BOOT_RETRY_TIME
514                 else if (nbytes == -2) {
515                         break;  /* timed out, exit the command  */
516                 }
517 #endif
518                 else {
519                         char *endp;
520
521                         data = simple_strtoul(console_buffer, &endp, 16);
522                         if(size == 1) {
523                                 data = data << 24;
524                         } else if(size == 2) {
525                                 data = data << 16;
526                         }
527                         data = be32_to_cpu(data);
528                         nbytes = endp - console_buffer;
529                         if (nbytes) {
530 #ifdef CONFIG_BOOT_RETRY_TIME
531                                 /*
532                                  * good enough to not time out
533                                  */
534                                 reset_cmd_timeout();
535 #endif
536                                 if(i2c_write(chip, addr, alen, (uchar *)&data, size) != 0) {
537                                         puts ("Error writing the chip.\n");
538                                 }
539 #ifdef CFG_EEPROM_PAGE_WRITE_DELAY_MS
540                                 udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
541 #endif
542                                 if (incrflag)
543                                         addr += size;
544                         }
545                 }
546         } while (nbytes);
547
548         chip = i2c_mm_last_chip;
549         addr = i2c_mm_last_addr;
550         alen = i2c_mm_last_alen;
551
552         return 0;
553 }
554
555 /*
556  * Syntax:
557  *      iprobe {addr}{.0, .1, .2}
558  */
559 int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
560 {
561         int j;
562 #if defined(CFG_I2C_NOPROBES)
563         int k, skip;
564         uchar bus = GET_BUS_NUM;
565 #endif  /* NOPROBES */
566
567         puts ("Valid chip addresses:");
568         for(j = 0; j < 128; j++) {
569 #if defined(CFG_I2C_NOPROBES)
570                 skip = 0;
571                 for(k=0; k < NUM_ELEMENTS_NOPROBE; k++)
572                 {
573                         if(COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k))
574                         {
575                                 skip = 1;
576                                 break;
577                         }
578                 }
579                 if (skip)
580                         continue;
581 #endif
582                 if(i2c_probe(j) == 0) {
583                         printf(" %02X", j);
584                 }
585         }
586         putc ('\n');
587
588 #if defined(CFG_I2C_NOPROBES)
589         puts ("Excluded chip addresses:");
590         for(k=0; k < NUM_ELEMENTS_NOPROBE; k++)
591         {
592                 if(COMPARE_BUS(bus,k))
593                         printf(" %02X", NO_PROBE_ADDR(k));
594         }
595         putc ('\n');
596 #endif
597
598         return 0;
599 }
600
601
602 /*
603  * Syntax:
604  *      iloop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
605  *      {length} - Number of bytes to read
606  *      {delay}  - A DECIMAL number and defaults to 1000 uSec
607  */
608 int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
609 {
610         u_char  chip;
611         ulong   alen;
612         uint    addr;
613         uint    length;
614         u_char  bytes[16];
615         int     delay;
616         int     j;
617
618         if (argc < 3) {
619                 printf ("Usage:\n%s\n", cmdtp->usage);
620                 return 1;
621         }
622
623         /*
624          * Chip is always specified.
625          */
626         chip = simple_strtoul(argv[1], NULL, 16);
627
628         /*
629          * Address is always specified.
630          */
631         addr = simple_strtoul(argv[2], NULL, 16);
632         alen = 1;
633         for(j = 0; j < 8; j++) {
634                 if (argv[2][j] == '.') {
635                         alen = argv[2][j+1] - '0';
636                         if (alen > 4) {
637                                 printf ("Usage:\n%s\n", cmdtp->usage);
638                                 return 1;
639                         }
640                         break;
641                 } else if (argv[2][j] == '\0') {
642                         break;
643                 }
644         }
645
646         /*
647          * Length is the number of objects, not number of bytes.
648          */
649         length = 1;
650         length = simple_strtoul(argv[3], NULL, 16);
651         if(length > sizeof(bytes)) {
652                 length = sizeof(bytes);
653         }
654
655         /*
656          * The delay time (uSec) is optional.
657          */
658         delay = 1000;
659         if (argc > 3) {
660                 delay = simple_strtoul(argv[4], NULL, 10);
661         }
662         /*
663          * Run the loop...
664          */
665         while(1) {
666                 if(i2c_read(chip, addr, alen, bytes, length) != 0) {
667                         puts ("Error reading the chip.\n");
668                 }
669                 udelay(delay);
670         }
671
672         /* NOTREACHED */
673         return 0;
674 }
675
676
677 /*
678  * The SDRAM command is separately configured because many
679  * (most?) embedded boards don't use SDRAM DIMMs.
680  */
681 #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
682
683 /*
684  * Syntax:
685  *      sdram {i2c_chip}
686  */
687 int do_sdram  ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
688 {
689         u_char  chip;
690         u_char  data[128];
691         u_char  cksum;
692         int     j;
693
694         if (argc < 2) {
695                 printf ("Usage:\n%s\n", cmdtp->usage);
696                 return 1;
697         }
698         /*
699          * Chip is always specified.
700          */
701         chip = simple_strtoul(argv[1], NULL, 16);
702
703         if(i2c_read(chip, 0, 1, data, sizeof(data)) != 0) {
704                 puts ("No SDRAM Serial Presence Detect found.\n");
705                 return 1;
706         }
707
708         cksum = 0;
709         for (j = 0; j < 63; j++) {
710                 cksum += data[j];
711         }
712         if(cksum != data[63]) {
713                 printf ("WARNING: Configuration data checksum failure:\n"
714                         "  is 0x%02x, calculated 0x%02x\n",
715                         data[63], cksum);
716         }
717         printf("SPD data revision            %d.%d\n",
718                 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
719         printf("Bytes used                   0x%02X\n", data[0]);
720         printf("Serial memory size           0x%02X\n", 1 << data[1]);
721         puts ("Memory type                  ");
722         switch(data[2]) {
723                 case 2:  puts ("EDO\n");        break;
724                 case 4:  puts ("SDRAM\n");      break;
725                 default: puts ("unknown\n");    break;
726         }
727         puts ("Row address bits             ");
728         if((data[3] & 0x00F0) == 0) {
729                 printf("%d\n", data[3] & 0x0F);
730         } else {
731                 printf("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
732         }
733         puts ("Column address bits          ");
734         if((data[4] & 0x00F0) == 0) {
735                 printf("%d\n", data[4] & 0x0F);
736         } else {
737                 printf("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
738         }
739         printf("Module rows                  %d\n", data[5]);
740         printf("Module data width            %d bits\n", (data[7] << 8) | data[6]);
741         puts ("Interface signal levels      ");
742         switch(data[8]) {
743                 case 0:  puts ("5.0v/TTL\n");   break;
744                 case 1:  puts ("LVTTL\n");      break;
745                 case 2:  puts ("HSTL 1.5\n");   break;
746                 case 3:  puts ("SSTL 3.3\n");   break;
747                 case 4:  puts ("SSTL 2.5\n");   break;
748                 default: puts ("unknown\n");    break;
749         }
750         printf("SDRAM cycle time             %d.%d nS\n",
751                 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
752         printf("SDRAM access time            %d.%d nS\n",
753                 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
754         puts ("EDC configuration            ");
755         switch(data[11]) {
756                 case 0:  puts ("None\n");       break;
757                 case 1:  puts ("Parity\n");     break;
758                 case 2:  puts ("ECC\n");        break;
759                 default: puts ("unknown\n");    break;
760         }
761         if((data[12] & 0x80) == 0) {
762                 puts ("No self refresh, rate        ");
763         } else {
764                 puts ("Self refresh, rate           ");
765         }
766         switch(data[12] & 0x7F) {
767                 case 0:  puts ("15.625uS\n");   break;
768                 case 1:  puts ("3.9uS\n");      break;
769                 case 2:  puts ("7.8uS\n");      break;
770                 case 3:  puts ("31.3uS\n");     break;
771                 case 4:  puts ("62.5uS\n");     break;
772                 case 5:  puts ("125uS\n");      break;
773                 default: puts ("unknown\n");    break;
774         }
775         printf("SDRAM width (primary)        %d\n", data[13] & 0x7F);
776         if((data[13] & 0x80) != 0) {
777                 printf("  (second bank)              %d\n",
778                         2 * (data[13] & 0x7F));
779         }
780         if(data[14] != 0) {
781                 printf("EDC width                    %d\n",
782                         data[14] & 0x7F);
783                 if((data[14] & 0x80) != 0) {
784                         printf("  (second bank)              %d\n",
785                                 2 * (data[14] & 0x7F));
786                 }
787         }
788         printf("Min clock delay, back-to-back random column addresses %d\n",
789                 data[15]);
790         puts ("Burst length(s)             ");
791         if (data[16] & 0x80) puts (" Page");
792         if (data[16] & 0x08) puts (" 8");
793         if (data[16] & 0x04) puts (" 4");
794         if (data[16] & 0x02) puts (" 2");
795         if (data[16] & 0x01) puts (" 1");
796         putc ('\n');
797         printf("Number of banks              %d\n", data[17]);
798         puts ("CAS latency(s)              ");
799         if (data[18] & 0x80) puts (" TBD");
800         if (data[18] & 0x40) puts (" 7");
801         if (data[18] & 0x20) puts (" 6");
802         if (data[18] & 0x10) puts (" 5");
803         if (data[18] & 0x08) puts (" 4");
804         if (data[18] & 0x04) puts (" 3");
805         if (data[18] & 0x02) puts (" 2");
806         if (data[18] & 0x01) puts (" 1");
807         putc ('\n');
808         puts ("CS latency(s)               ");
809         if (data[19] & 0x80) puts (" TBD");
810         if (data[19] & 0x40) puts (" 6");
811         if (data[19] & 0x20) puts (" 5");
812         if (data[19] & 0x10) puts (" 4");
813         if (data[19] & 0x08) puts (" 3");
814         if (data[19] & 0x04) puts (" 2");
815         if (data[19] & 0x02) puts (" 1");
816         if (data[19] & 0x01) puts (" 0");
817         putc ('\n');
818         puts ("WE latency(s)               ");
819         if (data[20] & 0x80) puts (" TBD");
820         if (data[20] & 0x40) puts (" 6");
821         if (data[20] & 0x20) puts (" 5");
822         if (data[20] & 0x10) puts (" 4");
823         if (data[20] & 0x08) puts (" 3");
824         if (data[20] & 0x04) puts (" 2");
825         if (data[20] & 0x02) puts (" 1");
826         if (data[20] & 0x01) puts (" 0");
827         putc ('\n');
828         puts ("Module attributes:\n");
829         if (!data[21])       puts ("  (none)\n");
830         if (data[21] & 0x80) puts ("  TBD (bit 7)\n");
831         if (data[21] & 0x40) puts ("  Redundant row address\n");
832         if (data[21] & 0x20) puts ("  Differential clock input\n");
833         if (data[21] & 0x10) puts ("  Registerd DQMB inputs\n");
834         if (data[21] & 0x08) puts ("  Buffered DQMB inputs\n");
835         if (data[21] & 0x04) puts ("  On-card PLL\n");
836         if (data[21] & 0x02) puts ("  Registered address/control lines\n");
837         if (data[21] & 0x01) puts ("  Buffered address/control lines\n");
838         puts ("Device attributes:\n");
839         if (data[22] & 0x80) puts ("  TBD (bit 7)\n");
840         if (data[22] & 0x40) puts ("  TBD (bit 6)\n");
841         if (data[22] & 0x20) puts ("  Upper Vcc tolerance 5%\n");
842         else                 puts ("  Upper Vcc tolerance 10%\n");
843         if (data[22] & 0x10) puts ("  Lower Vcc tolerance 5%\n");
844         else                 puts ("  Lower Vcc tolerance 10%\n");
845         if (data[22] & 0x08) puts ("  Supports write1/read burst\n");
846         if (data[22] & 0x04) puts ("  Supports precharge all\n");
847         if (data[22] & 0x02) puts ("  Supports auto precharge\n");
848         if (data[22] & 0x01) puts ("  Supports early RAS# precharge\n");
849         printf("SDRAM cycle time (2nd highest CAS latency)        %d.%d nS\n",
850                 (data[23] >> 4) & 0x0F, data[23] & 0x0F);
851         printf("SDRAM access from clock (2nd highest CAS latency) %d.%d nS\n",
852                 (data[24] >> 4) & 0x0F, data[24] & 0x0F);
853         printf("SDRAM cycle time (3rd highest CAS latency)        %d.%d nS\n",
854                 (data[25] >> 4) & 0x0F, data[25] & 0x0F);
855         printf("SDRAM access from clock (3rd highest CAS latency) %d.%d nS\n",
856                 (data[26] >> 4) & 0x0F, data[26] & 0x0F);
857         printf("Minimum row precharge        %d nS\n", data[27]);
858         printf("Row active to row active min %d nS\n", data[28]);
859         printf("RAS to CAS delay min         %d nS\n", data[29]);
860         printf("Minimum RAS pulse width      %d nS\n", data[30]);
861         puts ("Density of each row         ");
862         if (data[31] & 0x80) puts (" 512");
863         if (data[31] & 0x40) puts (" 256");
864         if (data[31] & 0x20) puts (" 128");
865         if (data[31] & 0x10) puts (" 64");
866         if (data[31] & 0x08) puts (" 32");
867         if (data[31] & 0x04) puts (" 16");
868         if (data[31] & 0x02) puts (" 8");
869         if (data[31] & 0x01) puts (" 4");
870         puts ("MByte\n");
871         printf("Command and Address setup    %c%d.%d nS\n",
872                 (data[32] & 0x80) ? '-' : '+',
873                 (data[32] >> 4) & 0x07, data[32] & 0x0F);
874         printf("Command and Address hold     %c%d.%d nS\n",
875                 (data[33] & 0x80) ? '-' : '+',
876                 (data[33] >> 4) & 0x07, data[33] & 0x0F);
877         printf("Data signal input setup      %c%d.%d nS\n",
878                 (data[34] & 0x80) ? '-' : '+',
879                 (data[34] >> 4) & 0x07, data[34] & 0x0F);
880         printf("Data signal input hold       %c%d.%d nS\n",
881                 (data[35] & 0x80) ? '-' : '+',
882                 (data[35] >> 4) & 0x07, data[35] & 0x0F);
883         puts ("Manufacturer's JEDEC ID      ");
884         for(j = 64; j <= 71; j++)
885                 printf("%02X ", data[j]);
886         putc ('\n');
887         printf("Manufacturing Location       %02X\n", data[72]);
888         puts ("Manufacturer's Part Number   ");
889         for(j = 73; j <= 90; j++)
890                 printf("%02X ", data[j]);
891         putc ('\n');
892         printf("Revision Code                %02X %02X\n", data[91], data[92]);
893         printf("Manufacturing Date           %02X %02X\n", data[93], data[94]);
894         puts ("Assembly Serial Number       ");
895         for(j = 95; j <= 98; j++)
896                 printf("%02X ", data[j]);
897         putc ('\n');
898         printf("Speed rating                 PC%d\n",
899                 data[126] == 0x66 ? 66 : data[126]);
900
901         return 0;
902 }
903 #endif  /* CFG_CMD_SDRAM */
904
905 #if defined(CONFIG_I2C_CMD_TREE)
906 #if defined(CONFIG_I2C_MULTI_BUS)
907 int do_i2c_bus_num(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
908 {
909         int bus_idx, ret=0;
910
911         if (argc == 1)  /* querying current setting */
912         {
913                 printf("Current bus is %d\n", i2c_get_bus_num());
914         }
915         else
916         {
917                 bus_idx = simple_strtoul(argv[1], NULL, 10);
918                 printf("Setting bus to %d\n", bus_idx);
919                 ret = i2c_set_bus_num(bus_idx);
920                 if(ret)
921                 {
922                         printf("Failure changing bus number (%d)\n", ret);
923                 }
924         }
925         return ret;
926 }
927 #endif  /* CONFIG_I2C_MULTI_BUS */
928
929 int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
930 {
931         int speed, ret=0;
932
933         if (argc == 1)  /* querying current speed */
934         {
935                 printf("Current bus speed=%d\n", i2c_get_bus_speed());
936         }
937         else
938         {
939                 speed = simple_strtoul(argv[1], NULL, 10);
940                 printf("Setting bus speed to %d Hz\n", speed);
941                 ret = i2c_set_bus_speed(speed);
942                 if(ret)
943                 {
944                         printf("Failure changing bus speed (%d)\n", ret);
945                 }
946         }
947         return ret;
948 }
949
950 int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
951 {
952 #if defined(CONFIG_I2C_MULTI_BUS)
953         if(!strncmp(argv[1], "de", 2))
954         {
955                 return do_i2c_bus_num(cmdtp, flag, --argc, ++argv);
956         }
957 #endif  /* CONFIG_I2C_MULTI_BUS */
958         if(!strncmp(argv[1], "sp", 2))
959         {
960                 return do_i2c_bus_speed(cmdtp, flag, --argc, ++argv);
961         }
962         if(!strncmp(argv[1], "md", 2))
963         {
964                 return do_i2c_md(cmdtp, flag, --argc, ++argv);
965         }
966         if(!strncmp(argv[1], "mm", 2))
967         {
968                 return do_i2c_mm(cmdtp, flag, --argc, ++argv);
969         }
970         if(!strncmp(argv[1], "mw", 2))
971         {
972                 return do_i2c_mw(cmdtp, flag, --argc, ++argv);
973         }
974         if(!strncmp(argv[1], "nm", 2))
975         {
976                 return do_i2c_nm(cmdtp, flag, --argc, ++argv);
977         }
978         if(!strncmp(argv[1], "cr", 2))
979         {
980                 return do_i2c_crc(cmdtp, flag, --argc, ++argv);
981         }
982         if(!strncmp(argv[1], "pr", 2))
983         {
984                 return do_i2c_probe(cmdtp, flag, --argc, ++argv);
985         }
986         if(!strncmp(argv[1], "lo", 2))
987         {
988                 return do_i2c_loop(cmdtp, flag, --argc, ++argv);
989         }
990 #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
991         if(!strncmp(argv[1], "sd", 2))
992         {
993                 return do_sdram(cmdtp, flag, --argc, ++argv);
994         }
995 #endif  /* CFG_CMD_SDRAM */
996         else
997         {
998                 printf ("Usage:\n%s\n", cmdtp->usage);
999         }
1000         return 0;
1001 }
1002 #endif  /* CONFIG_I2C_CMD_TREE */
1003
1004 /***************************************************/
1005
1006 U_BOOT_CMD(
1007         imd,    4,      1,      do_i2c_md,              \
1008         "imd     - i2c memory display\n",                               \
1009         "chip address[.0, .1, .2] [# of objects]\n    - i2c memory display\n" \
1010 );
1011
1012 U_BOOT_CMD(
1013         imm,    3,      1,      do_i2c_mm,
1014         "imm     - i2c memory modify (auto-incrementing)\n",
1015         "chip address[.0, .1, .2]\n"
1016         "    - memory modify, auto increment address\n"
1017 );
1018 U_BOOT_CMD(
1019         inm,    3,      1,      do_i2c_nm,
1020         "inm     - memory modify (constant address)\n",
1021         "chip address[.0, .1, .2]\n    - memory modify, read and keep address\n"
1022 );
1023
1024 U_BOOT_CMD(
1025         imw,    5,      1,      do_i2c_mw,
1026         "imw     - memory write (fill)\n",
1027         "chip address[.0, .1, .2] value [count]\n    - memory write (fill)\n"
1028 );
1029
1030 U_BOOT_CMD(
1031         icrc32, 5,      1,      do_i2c_crc,
1032         "icrc32  - checksum calculation\n",
1033         "chip address[.0, .1, .2] count\n    - compute CRC32 checksum\n"
1034 );
1035
1036 U_BOOT_CMD(
1037         iprobe, 1,      1,      do_i2c_probe,
1038         "iprobe  - probe to discover valid I2C chip addresses\n",
1039         "\n    -discover valid I2C chip addresses\n"
1040 );
1041
1042 /*
1043  * Require full name for "iloop" because it is an infinite loop!
1044  */
1045 U_BOOT_CMD(
1046         iloop,  5,      1,      do_i2c_loop,
1047         "iloop   - infinite loop on address range\n",
1048         "chip address[.0, .1, .2] [# of objects]\n"
1049         "    - loop, reading a set of addresses\n"
1050 );
1051
1052 #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
1053 U_BOOT_CMD(
1054         isdram, 2,      1,      do_sdram,
1055         "isdram  - print SDRAM configuration information\n",
1056         "chip\n    - print SDRAM configuration information\n"
1057         "      (valid chip values 50..57)\n"
1058 );
1059 #endif
1060
1061 #if defined(CONFIG_I2C_CMD_TREE)
1062 U_BOOT_CMD(
1063         i2c, 6, 1, do_i2c,
1064         "i2c     - I2C sub-system\n",
1065 #if defined(CONFIG_I2C_MULTI_BUS)
1066         "dev [dev] - show or set current I2C bus\n"
1067 #endif  /* CONFIG_I2C_MULTI_BUS */
1068         "i2c speed [speed] - show or set I2C bus speed\n"
1069         "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1070         "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1071         "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1072         "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
1073         "i2c crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
1074         "i2c probe - show devices on the I2C bus\n"
1075         "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
1076 #if (CONFIG_COMMANDS & CFG_CMD_SDRAM)
1077         "i2c sdram chip - print SDRAM configuration information\n"
1078 #endif  /* CFG_CMD_SDRAM */
1079 );
1080 #endif  /* CONFIG_I2C_CMD_TREE */
1081
1082 #endif  /* CFG_CMD_I2C */