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