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