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