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