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