blob: 8ccde68110e16c31f3eaa2be849ca3cc133e2446 [file] [log] [blame]
wdenk81a88242002-10-26 15:22:42 +00001/*
Heiko Schocher3f4978c2012-01-16 21:12:24 +00002 * (C) Copyright 2009
3 * Sergey Kubushyn, himself, ksi@koi8.net
4 *
5 * Changes for unified multibus/multiadapter I2C support.
6 *
wdenk81a88242002-10-26 15:22:42 +00007 * (C) Copyright 2001
8 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
9 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
wdenk81a88242002-10-26 15:22:42 +000011 */
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 *
wdenk81a88242002-10-26 15:22:42 +000019 * {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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
wdenk81a88242002-10-26 15:22:42 +000053 * {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...
Peter Tyser0f89c542009-04-18 22:34:03 -050056 * i2c md 50 0 10 display 16 bytes starting at 0x000
wdenk81a88242002-10-26 15:22:42 +000057 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050058 * i2c md 50 100 10 display 16 bytes starting at 0x100
wdenk81a88242002-10-26 15:22:42 +000059 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050060 * i2c md 50 210 10 display 16 bytes starting at 0x210
wdenk81a88242002-10-26 15:22:42 +000061 * 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>
Simon Glass18d66532014-04-10 20:01:25 -060069#include <cli.h>
wdenk81a88242002-10-26 15:22:42 +000070#include <command.h>
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +000071#include <edid.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020072#include <environment.h>
wdenk81a88242002-10-26 15:22:42 +000073#include <i2c.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020074#include <malloc.h>
wdenk81a88242002-10-26 15:22:42 +000075#include <asm/byteorder.h>
Marek Vasut2515d842012-11-12 14:34:25 +000076#include <linux/compiler.h>
wdenk81a88242002-10-26 15:22:42 +000077
Heiko Schocher3f4978c2012-01-16 21:12:24 +000078DECLARE_GLOBAL_DATA_PTR;
79
wdenk81a88242002-10-26 15:22:42 +000080/* Display values from last command.
81 * Memory modify remembered values are different from display memory.
82 */
83static uchar i2c_dp_last_chip;
84static uint i2c_dp_last_addr;
85static uint i2c_dp_last_alen;
86static uint i2c_dp_last_length = 0x10;
87
88static uchar i2c_mm_last_chip;
89static uint i2c_mm_last_addr;
90static uint i2c_mm_last_alen;
91
Ben Warrenbb99ad62006-09-07 16:50:54 -040092/* 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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#if defined(CONFIG_SYS_I2C_NOPROBES)
Heiko Schocher9a2accb2012-10-25 10:32:14 +020098#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
Ben Warrenbb99ad62006-09-07 16:50:54 -040099static struct
100{
101 uchar bus;
102 uchar addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200103} i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400104#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 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200109static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400110#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)]
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000114#endif /* defined(CONFIG_SYS_I2C) */
Heiko Schocher67b23a32008-10-15 09:39:47 +0200115#endif
116
Frans Meulenbroeksa266fe92010-03-26 09:46:40 +0100117#define DISP_LINE_LEN 16
118
Marek Vasut06afa382012-11-12 14:34:27 +0000119/**
120 * i2c_init_board() - Board-specific I2C bus init
121 *
122 * This function is the default no-op implementation of I2C bus
123 * initialization. This function can be overriden by board-specific
124 * implementation if needed.
125 */
Marek Vasut2515d842012-11-12 14:34:25 +0000126__weak
127void i2c_init_board(void)
Stefan Biglerc649dda2011-04-08 16:24:08 +0200128{
Stefan Biglerc649dda2011-04-08 16:24:08 +0200129}
Stefan Biglerc649dda2011-04-08 16:24:08 +0200130
Peter Tyser655b34a2009-04-18 22:34:01 -0500131/* TODO: Implement architecture-specific get/set functions */
Marek Vasut06afa382012-11-12 14:34:27 +0000132
133/**
134 * i2c_get_bus_speed() - Return I2C bus speed
135 *
136 * This function is the default implementation of function for retrieveing
137 * the current I2C bus speed in Hz.
138 *
139 * A driver implementing runtime switching of I2C bus speed must override
140 * this function to report the speed correctly. Simple or legacy drivers
141 * can use this fallback.
142 *
143 * Returns I2C bus speed in Hz.
144 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000145#if !defined(CONFIG_SYS_I2C)
146/*
147 * TODO: Implement architecture-specific get/set functions
148 * Should go away, if we switched completely to new multibus support
149 */
Marek Vasut2515d842012-11-12 14:34:25 +0000150__weak
151unsigned int i2c_get_bus_speed(void)
Peter Tyser655b34a2009-04-18 22:34:01 -0500152{
153 return CONFIG_SYS_I2C_SPEED;
154}
Peter Tyser655b34a2009-04-18 22:34:01 -0500155
Marek Vasut06afa382012-11-12 14:34:27 +0000156/**
157 * i2c_set_bus_speed() - Configure I2C bus speed
158 * @speed: Newly set speed of the I2C bus in Hz
159 *
160 * This function is the default implementation of function for setting
161 * the I2C bus speed in Hz.
162 *
163 * A driver implementing runtime switching of I2C bus speed must override
164 * this function to report the speed correctly. Simple or legacy drivers
165 * can use this fallback.
166 *
167 * Returns zero on success, negative value on error.
168 */
Marek Vasut2515d842012-11-12 14:34:25 +0000169__weak
170int i2c_set_bus_speed(unsigned int speed)
Peter Tyser655b34a2009-04-18 22:34:01 -0500171{
172 if (speed != CONFIG_SYS_I2C_SPEED)
173 return -1;
174
175 return 0;
176}
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000177#endif
Peter Tyser655b34a2009-04-18 22:34:01 -0500178
Marek Vasut06afa382012-11-12 14:34:27 +0000179/**
180 * get_alen() - Small parser helper function to get address length
181 *
182 * Returns the address length.
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100183 */
184static uint get_alen(char *arg)
185{
186 int j;
187 int alen;
188
189 alen = 1;
190 for (j = 0; j < 8; j++) {
191 if (arg[j] == '.') {
192 alen = arg[j+1] - '0';
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100193 break;
194 } else if (arg[j] == '\0')
195 break;
196 }
197 return alen;
198}
199
Marek Vasut06afa382012-11-12 14:34:27 +0000200/**
201 * do_i2c_read() - Handle the "i2c read" command-line command
202 * @cmdtp: Command data struct pointer
203 * @flag: Command flag
204 * @argc: Command-line argument count
205 * @argv: Array of command-line arguments
206 *
207 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
208 * on error.
209 *
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100210 * Syntax:
211 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
212 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200213static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100214{
215 u_char chip;
216 uint devaddr, alen, length;
217 u_char *memaddr;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100218
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200219 if (argc != 5)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000220 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100221
222 /*
223 * I2C chip address
224 */
225 chip = simple_strtoul(argv[1], NULL, 16);
226
227 /*
228 * I2C data address within the chip. This can be 1 or
229 * 2 bytes long. Some day it might be 3 bytes long :-).
230 */
231 devaddr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100232 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200233 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000234 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100235
236 /*
237 * Length is the number of objects, not number of bytes.
238 */
239 length = simple_strtoul(argv[3], NULL, 16);
240
241 /*
242 * memaddr is the address where to store things in memory
243 */
244 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
245
246 if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
247 puts ("Error reading the chip.\n");
248 return 1;
249 }
250 return 0;
251}
252
York Sunff5d2dc2012-09-16 08:02:30 +0000253static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
254{
255 u_char chip;
256 uint devaddr, alen, length;
257 u_char *memaddr;
258
259 if (argc != 5)
260 return cmd_usage(cmdtp);
261
262 /*
263 * memaddr is the address where to store things in memory
264 */
265 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
266
267 /*
268 * I2C chip address
269 */
270 chip = simple_strtoul(argv[2], NULL, 16);
271
272 /*
273 * I2C data address within the chip. This can be 1 or
274 * 2 bytes long. Some day it might be 3 bytes long :-).
275 */
276 devaddr = simple_strtoul(argv[3], NULL, 16);
277 alen = get_alen(argv[3]);
278 if (alen > 3)
279 return cmd_usage(cmdtp);
280
281 /*
282 * Length is the number of objects, not number of bytes.
283 */
284 length = simple_strtoul(argv[4], NULL, 16);
285
286 while (length-- > 0) {
287 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
288 puts("Error writing to the chip.\n");
289 return 1;
290 }
291/*
292 * No write delay with FRAM devices.
293 */
294#if !defined(CONFIG_SYS_I2C_FRAM)
295 udelay(11000);
296#endif
297 }
298 return 0;
299}
300
Marek Vasut06afa382012-11-12 14:34:27 +0000301/**
302 * do_i2c_md() - Handle the "i2c md" command-line command
303 * @cmdtp: Command data struct pointer
304 * @flag: Command flag
305 * @argc: Command-line argument count
306 * @argv: Array of command-line arguments
307 *
308 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
309 * on error.
310 *
Frans Meulenbroeks4a8cf332010-03-26 09:46:39 +0100311 * Syntax:
312 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
313 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200314static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000315{
316 u_char chip;
317 uint addr, alen, length;
318 int j, nbytes, linebytes;
319
320 /* We use the last specified parameters, unless new ones are
321 * entered.
322 */
323 chip = i2c_dp_last_chip;
324 addr = i2c_dp_last_addr;
325 alen = i2c_dp_last_alen;
326 length = i2c_dp_last_length;
327
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200328 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000329 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000330
331 if ((flag & CMD_FLAG_REPEAT) == 0) {
332 /*
333 * New command specified.
334 */
wdenk81a88242002-10-26 15:22:42 +0000335
336 /*
337 * I2C chip address
338 */
339 chip = simple_strtoul(argv[1], NULL, 16);
340
341 /*
342 * I2C data address within the chip. This can be 1 or
343 * 2 bytes long. Some day it might be 3 bytes long :-).
344 */
345 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100346 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200347 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000348 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000349
350 /*
351 * If another parameter, it is the length to display.
352 * Length is the number of objects, not number of bytes.
353 */
354 if (argc > 3)
355 length = simple_strtoul(argv[3], NULL, 16);
356 }
357
358 /*
359 * Print the lines.
360 *
361 * We buffer all read data, so we can make sure data is read only
362 * once.
363 */
364 nbytes = length;
365 do {
366 unsigned char linebuf[DISP_LINE_LEN];
367 unsigned char *cp;
368
369 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
370
Timur Tabie857a5b2006-11-28 12:09:35 -0600371 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000372 puts ("Error reading the chip.\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600373 else {
wdenk81a88242002-10-26 15:22:42 +0000374 printf("%04x:", addr);
375 cp = linebuf;
376 for (j=0; j<linebytes; j++) {
377 printf(" %02x", *cp++);
378 addr++;
379 }
wdenk4b9206e2004-03-23 22:14:11 +0000380 puts (" ");
wdenk81a88242002-10-26 15:22:42 +0000381 cp = linebuf;
382 for (j=0; j<linebytes; j++) {
383 if ((*cp < 0x20) || (*cp > 0x7e))
wdenk4b9206e2004-03-23 22:14:11 +0000384 puts (".");
wdenk81a88242002-10-26 15:22:42 +0000385 else
386 printf("%c", *cp);
387 cp++;
388 }
wdenk4b9206e2004-03-23 22:14:11 +0000389 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000390 }
391 nbytes -= linebytes;
392 } while (nbytes > 0);
393
394 i2c_dp_last_chip = chip;
395 i2c_dp_last_addr = addr;
396 i2c_dp_last_alen = alen;
397 i2c_dp_last_length = length;
398
399 return 0;
400}
401
Marek Vasut06afa382012-11-12 14:34:27 +0000402/**
403 * do_i2c_mw() - Handle the "i2c mw" command-line command
404 * @cmdtp: Command data struct pointer
405 * @flag: Command flag
406 * @argc: Command-line argument count
407 * @argv: Array of command-line arguments
408 *
409 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
410 * on error.
wdenk81a88242002-10-26 15:22:42 +0000411 *
412 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500413 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
wdenk81a88242002-10-26 15:22:42 +0000414 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200415static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000416{
417 uchar chip;
418 ulong addr;
419 uint alen;
420 uchar byte;
421 int count;
wdenk81a88242002-10-26 15:22:42 +0000422
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200423 if ((argc < 4) || (argc > 5))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000424 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000425
426 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200427 * Chip is always specified.
428 */
wdenk81a88242002-10-26 15:22:42 +0000429 chip = simple_strtoul(argv[1], NULL, 16);
430
431 /*
432 * Address is always specified.
433 */
434 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100435 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200436 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000437 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000438
439 /*
440 * Value to write is always specified.
441 */
442 byte = simple_strtoul(argv[3], NULL, 16);
443
444 /*
445 * Optional count
446 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600447 if (argc == 5)
wdenk81a88242002-10-26 15:22:42 +0000448 count = simple_strtoul(argv[4], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600449 else
wdenk81a88242002-10-26 15:22:42 +0000450 count = 1;
wdenk81a88242002-10-26 15:22:42 +0000451
452 while (count-- > 0) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600453 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000454 puts ("Error writing the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000455 /*
456 * Wait for the write to complete. The write can take
457 * up to 10mSec (we allow a little more time).
wdenk81a88242002-10-26 15:22:42 +0000458 */
d4f5c722005-08-12 21:16:13 +0200459/*
460 * No write delay with FRAM devices.
461 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200462#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk81a88242002-10-26 15:22:42 +0000463 udelay(11000);
d4f5c722005-08-12 21:16:13 +0200464#endif
wdenk81a88242002-10-26 15:22:42 +0000465 }
466
Marek Vasut06afa382012-11-12 14:34:27 +0000467 return 0;
wdenk81a88242002-10-26 15:22:42 +0000468}
469
Marek Vasut06afa382012-11-12 14:34:27 +0000470/**
471 * do_i2c_crc() - Handle the "i2c crc32" command-line command
472 * @cmdtp: Command data struct pointer
473 * @flag: Command flag
474 * @argc: Command-line argument count
475 * @argv: Array of command-line arguments
476 *
477 * Calculate a CRC on memory
478 *
479 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
480 * on error.
wdenk81a88242002-10-26 15:22:42 +0000481 *
482 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500483 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
wdenk81a88242002-10-26 15:22:42 +0000484 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200485static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000486{
487 uchar chip;
488 ulong addr;
489 uint alen;
490 int count;
491 uchar byte;
492 ulong crc;
493 ulong err;
wdenk81a88242002-10-26 15:22:42 +0000494
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200495 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000496 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000497
498 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200499 * Chip is always specified.
500 */
wdenk81a88242002-10-26 15:22:42 +0000501 chip = simple_strtoul(argv[1], NULL, 16);
502
503 /*
504 * Address is always specified.
505 */
506 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100507 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200508 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000509 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000510
511 /*
512 * Count is always specified
513 */
514 count = simple_strtoul(argv[3], NULL, 16);
515
516 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
517 /*
518 * CRC a byte at a time. This is going to be slooow, but hey, the
519 * memories are small and slow too so hopefully nobody notices.
520 */
521 crc = 0;
522 err = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600523 while (count-- > 0) {
524 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
wdenk81a88242002-10-26 15:22:42 +0000525 err++;
wdenk81a88242002-10-26 15:22:42 +0000526 crc = crc32 (crc, &byte, 1);
527 addr++;
528 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600529 if (err > 0)
wdenk4b9206e2004-03-23 22:14:11 +0000530 puts ("Error reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600531 else
wdenk81a88242002-10-26 15:22:42 +0000532 printf ("%08lx\n", crc);
wdenk81a88242002-10-26 15:22:42 +0000533
534 return 0;
535}
536
Marek Vasut06afa382012-11-12 14:34:27 +0000537/**
538 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
539 * @cmdtp: Command data struct pointer
540 * @flag: Command flag
541 * @argc: Command-line argument count
542 * @argv: Array of command-line arguments
543 *
544 * Modify memory.
545 *
546 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
547 * on error.
wdenk81a88242002-10-26 15:22:42 +0000548 *
549 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500550 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
551 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
wdenk81a88242002-10-26 15:22:42 +0000552 */
wdenk81a88242002-10-26 15:22:42 +0000553static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200554mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000555{
556 uchar chip;
557 ulong addr;
558 uint alen;
559 ulong data;
560 int size = 1;
561 int nbytes;
wdenk81a88242002-10-26 15:22:42 +0000562
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200563 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000564 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000565
566#ifdef CONFIG_BOOT_RETRY_TIME
567 reset_cmd_timeout(); /* got a good command to get here */
568#endif
569 /*
570 * We use the last specified parameters, unless new ones are
571 * entered.
572 */
573 chip = i2c_mm_last_chip;
574 addr = i2c_mm_last_addr;
575 alen = i2c_mm_last_alen;
576
577 if ((flag & CMD_FLAG_REPEAT) == 0) {
578 /*
579 * New command specified. Check for a size specification.
580 * Defaults to byte if no or incorrect specification.
581 */
582 size = cmd_get_data_size(argv[0], 1);
583
584 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200585 * Chip is always specified.
586 */
wdenk81a88242002-10-26 15:22:42 +0000587 chip = simple_strtoul(argv[1], NULL, 16);
588
589 /*
590 * Address is always specified.
591 */
592 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100593 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200594 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000595 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000596 }
597
598 /*
599 * Print the address, followed by value. Then accept input for
600 * the next value. A non-converted value exits.
601 */
602 do {
603 printf("%08lx:", addr);
Timur Tabie857a5b2006-11-28 12:09:35 -0600604 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000605 puts ("\nError reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600606 else {
wdenk81a88242002-10-26 15:22:42 +0000607 data = cpu_to_be32(data);
Timur Tabie857a5b2006-11-28 12:09:35 -0600608 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000609 printf(" %02lx", (data >> 24) & 0x000000FF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600610 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000611 printf(" %04lx", (data >> 16) & 0x0000FFFF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600612 else
wdenk81a88242002-10-26 15:22:42 +0000613 printf(" %08lx", data);
wdenk81a88242002-10-26 15:22:42 +0000614 }
615
616 nbytes = readline (" ? ");
617 if (nbytes == 0) {
618 /*
619 * <CR> pressed as only input, don't modify current
620 * location and move to next.
621 */
622 if (incrflag)
623 addr += size;
624 nbytes = size;
625#ifdef CONFIG_BOOT_RETRY_TIME
626 reset_cmd_timeout(); /* good enough to not time out */
627#endif
628 }
629#ifdef CONFIG_BOOT_RETRY_TIME
Timur Tabie857a5b2006-11-28 12:09:35 -0600630 else if (nbytes == -2)
wdenk81a88242002-10-26 15:22:42 +0000631 break; /* timed out, exit the command */
wdenk81a88242002-10-26 15:22:42 +0000632#endif
633 else {
634 char *endp;
635
636 data = simple_strtoul(console_buffer, &endp, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600637 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000638 data = data << 24;
Timur Tabie857a5b2006-11-28 12:09:35 -0600639 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000640 data = data << 16;
wdenk81a88242002-10-26 15:22:42 +0000641 data = be32_to_cpu(data);
642 nbytes = endp - console_buffer;
643 if (nbytes) {
644#ifdef CONFIG_BOOT_RETRY_TIME
645 /*
646 * good enough to not time out
647 */
648 reset_cmd_timeout();
649#endif
Timur Tabie857a5b2006-11-28 12:09:35 -0600650 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000651 puts ("Error writing the chip.\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200652#ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
653 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk2535d602003-07-17 23:16:40 +0000654#endif
wdenk81a88242002-10-26 15:22:42 +0000655 if (incrflag)
656 addr += size;
657 }
658 }
659 } while (nbytes);
660
Peter Tyser08007072008-08-15 14:36:32 -0500661 i2c_mm_last_chip = chip;
662 i2c_mm_last_addr = addr;
663 i2c_mm_last_alen = alen;
wdenk81a88242002-10-26 15:22:42 +0000664
665 return 0;
666}
667
Marek Vasut06afa382012-11-12 14:34:27 +0000668/**
669 * do_i2c_probe() - Handle the "i2c probe" command-line command
670 * @cmdtp: Command data struct pointer
671 * @flag: Command flag
672 * @argc: Command-line argument count
673 * @argv: Array of command-line arguments
674 *
675 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
676 * on error.
677 *
wdenk81a88242002-10-26 15:22:42 +0000678 * Syntax:
Eric Nelson54b99e52012-09-23 10:12:56 +0000679 * i2c probe {addr}
680 *
681 * Returns zero (success) if one or more I2C devices was found
wdenk81a88242002-10-26 15:22:42 +0000682 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200683static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000684{
685 int j;
Eric Nelson54b99e52012-09-23 10:12:56 +0000686 int addr = -1;
687 int found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200688#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000689 int k, skip;
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000690 unsigned int bus = GET_BUS_NUM;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400691#endif /* NOPROBES */
wdenk81a88242002-10-26 15:22:42 +0000692
Eric Nelson54b99e52012-09-23 10:12:56 +0000693 if (argc == 2)
694 addr = simple_strtol(argv[1], 0, 16);
695
wdenk4b9206e2004-03-23 22:14:11 +0000696 puts ("Valid chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600697 for (j = 0; j < 128; j++) {
Eric Nelson54b99e52012-09-23 10:12:56 +0000698 if ((0 <= addr) && (j != addr))
699 continue;
700
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200701#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000702 skip = 0;
Axel Lincfb25cc2013-06-22 21:56:35 +0800703 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600704 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
wdenk81a88242002-10-26 15:22:42 +0000705 skip = 1;
706 break;
707 }
708 }
709 if (skip)
710 continue;
711#endif
Eric Nelson54b99e52012-09-23 10:12:56 +0000712 if (i2c_probe(j) == 0) {
wdenk81a88242002-10-26 15:22:42 +0000713 printf(" %02X", j);
Eric Nelson54b99e52012-09-23 10:12:56 +0000714 found++;
715 }
wdenk81a88242002-10-26 15:22:42 +0000716 }
wdenk4b9206e2004-03-23 22:14:11 +0000717 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000718
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200719#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000720 puts ("Excluded chip addresses:");
Axel Lincfb25cc2013-06-22 21:56:35 +0800721 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600722 if (COMPARE_BUS(bus,k))
Ben Warrenbb99ad62006-09-07 16:50:54 -0400723 printf(" %02X", NO_PROBE_ADDR(k));
724 }
wdenk4b9206e2004-03-23 22:14:11 +0000725 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000726#endif
727
Eric Nelson54b99e52012-09-23 10:12:56 +0000728 return (0 == found);
wdenk81a88242002-10-26 15:22:42 +0000729}
730
Marek Vasut06afa382012-11-12 14:34:27 +0000731/**
732 * do_i2c_loop() - Handle the "i2c loop" command-line command
733 * @cmdtp: Command data struct pointer
734 * @flag: Command flag
735 * @argc: Command-line argument count
736 * @argv: Array of command-line arguments
737 *
738 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
739 * on error.
740 *
wdenk81a88242002-10-26 15:22:42 +0000741 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500742 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
wdenk81a88242002-10-26 15:22:42 +0000743 * {length} - Number of bytes to read
744 * {delay} - A DECIMAL number and defaults to 1000 uSec
745 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200746static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000747{
748 u_char chip;
749 ulong alen;
750 uint addr;
751 uint length;
752 u_char bytes[16];
753 int delay;
wdenk81a88242002-10-26 15:22:42 +0000754
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200755 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000756 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000757
758 /*
759 * Chip is always specified.
760 */
761 chip = simple_strtoul(argv[1], NULL, 16);
762
763 /*
764 * Address is always specified.
765 */
766 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100767 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200768 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000769 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000770
771 /*
772 * Length is the number of objects, not number of bytes.
773 */
774 length = 1;
775 length = simple_strtoul(argv[3], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600776 if (length > sizeof(bytes))
wdenk81a88242002-10-26 15:22:42 +0000777 length = sizeof(bytes);
wdenk81a88242002-10-26 15:22:42 +0000778
779 /*
780 * The delay time (uSec) is optional.
781 */
782 delay = 1000;
Timur Tabie857a5b2006-11-28 12:09:35 -0600783 if (argc > 3)
wdenk81a88242002-10-26 15:22:42 +0000784 delay = simple_strtoul(argv[4], NULL, 10);
wdenk81a88242002-10-26 15:22:42 +0000785 /*
786 * Run the loop...
787 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600788 while (1) {
789 if (i2c_read(chip, addr, alen, bytes, length) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000790 puts ("Error reading the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000791 udelay(delay);
792 }
793
794 /* NOTREACHED */
795 return 0;
796}
797
wdenk81a88242002-10-26 15:22:42 +0000798/*
799 * The SDRAM command is separately configured because many
800 * (most?) embedded boards don't use SDRAM DIMMs.
Marek Vasut06afa382012-11-12 14:34:27 +0000801 *
802 * FIXME: Document and probably move elsewhere!
wdenk81a88242002-10-26 15:22:42 +0000803 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500804#if defined(CONFIG_CMD_SDRAM)
Larry Johnson632de062008-01-11 23:26:18 -0500805static void print_ddr2_tcyc (u_char const b)
806{
807 printf ("%d.", (b >> 4) & 0x0F);
808 switch (b & 0x0F) {
809 case 0x0:
810 case 0x1:
811 case 0x2:
812 case 0x3:
813 case 0x4:
814 case 0x5:
815 case 0x6:
816 case 0x7:
817 case 0x8:
818 case 0x9:
819 printf ("%d ns\n", b & 0x0F);
820 break;
821 case 0xA:
822 puts ("25 ns\n");
823 break;
824 case 0xB:
825 puts ("33 ns\n");
826 break;
827 case 0xC:
828 puts ("66 ns\n");
829 break;
830 case 0xD:
831 puts ("75 ns\n");
832 break;
833 default:
834 puts ("?? ns\n");
835 break;
836 }
837}
838
839static void decode_bits (u_char const b, char const *str[], int const do_once)
840{
841 u_char mask;
842
843 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
844 if (b & mask) {
845 puts (*str);
846 if (do_once)
847 return;
848 }
849 }
850}
wdenk81a88242002-10-26 15:22:42 +0000851
852/*
853 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500854 * i2c sdram {i2c_chip}
wdenk81a88242002-10-26 15:22:42 +0000855 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200856static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000857{
Larry Johnson632de062008-01-11 23:26:18 -0500858 enum { unknown, EDO, SDRAM, DDR2 } type;
859
wdenk81a88242002-10-26 15:22:42 +0000860 u_char chip;
861 u_char data[128];
862 u_char cksum;
863 int j;
864
Larry Johnson632de062008-01-11 23:26:18 -0500865 static const char *decode_CAS_DDR2[] = {
866 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
867 };
868
869 static const char *decode_CAS_default[] = {
870 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
871 };
872
873 static const char *decode_CS_WE_default[] = {
874 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
875 };
876
877 static const char *decode_byte21_default[] = {
878 " TBD (bit 7)\n",
879 " Redundant row address\n",
880 " Differential clock input\n",
881 " Registerd DQMB inputs\n",
882 " Buffered DQMB inputs\n",
883 " On-card PLL\n",
884 " Registered address/control lines\n",
885 " Buffered address/control lines\n"
886 };
887
888 static const char *decode_byte22_DDR2[] = {
889 " TBD (bit 7)\n",
890 " TBD (bit 6)\n",
891 " TBD (bit 5)\n",
892 " TBD (bit 4)\n",
893 " TBD (bit 3)\n",
894 " Supports partial array self refresh\n",
895 " Supports 50 ohm ODT\n",
896 " Supports weak driver\n"
897 };
898
899 static const char *decode_row_density_DDR2[] = {
900 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
901 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
902 };
903
904 static const char *decode_row_density_default[] = {
905 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
906 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
907 };
908
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200909 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000910 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200911
wdenk81a88242002-10-26 15:22:42 +0000912 /*
913 * Chip is always specified.
Larry Johnson632de062008-01-11 23:26:18 -0500914 */
915 chip = simple_strtoul (argv[1], NULL, 16);
wdenk81a88242002-10-26 15:22:42 +0000916
Larry Johnson632de062008-01-11 23:26:18 -0500917 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000918 puts ("No SDRAM Serial Presence Detect found.\n");
wdenk81a88242002-10-26 15:22:42 +0000919 return 1;
920 }
921
922 cksum = 0;
923 for (j = 0; j < 63; j++) {
924 cksum += data[j];
925 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600926 if (cksum != data[63]) {
wdenk81a88242002-10-26 15:22:42 +0000927 printf ("WARNING: Configuration data checksum failure:\n"
Larry Johnson632de062008-01-11 23:26:18 -0500928 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
wdenk81a88242002-10-26 15:22:42 +0000929 }
Larry Johnson632de062008-01-11 23:26:18 -0500930 printf ("SPD data revision %d.%d\n",
wdenk81a88242002-10-26 15:22:42 +0000931 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
Larry Johnson632de062008-01-11 23:26:18 -0500932 printf ("Bytes used 0x%02X\n", data[0]);
933 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
934
wdenk4b9206e2004-03-23 22:14:11 +0000935 puts ("Memory type ");
Larry Johnson632de062008-01-11 23:26:18 -0500936 switch (data[2]) {
Larry Johnson0df6b842008-01-10 22:23:39 -0500937 case 2:
938 type = EDO;
939 puts ("EDO\n");
940 break;
941 case 4:
942 type = SDRAM;
943 puts ("SDRAM\n");
944 break;
945 case 8:
946 type = DDR2;
947 puts ("DDR2\n");
948 break;
949 default:
950 type = unknown;
951 puts ("unknown\n");
952 break;
wdenk81a88242002-10-26 15:22:42 +0000953 }
Larry Johnson632de062008-01-11 23:26:18 -0500954
wdenk4b9206e2004-03-23 22:14:11 +0000955 puts ("Row address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600956 if ((data[3] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500957 printf ("%d\n", data[3] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600958 else
Larry Johnson632de062008-01-11 23:26:18 -0500959 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
960
wdenk4b9206e2004-03-23 22:14:11 +0000961 puts ("Column address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600962 if ((data[4] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500963 printf ("%d\n", data[4] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600964 else
Larry Johnson632de062008-01-11 23:26:18 -0500965 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -0500966
967 switch (type) {
968 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500969 printf ("Number of ranks %d\n",
970 (data[5] & 0x07) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -0500971 break;
972 default:
Larry Johnson632de062008-01-11 23:26:18 -0500973 printf ("Module rows %d\n", data[5]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500974 break;
975 }
976
977 switch (type) {
978 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500979 printf ("Module data width %d bits\n", data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500980 break;
981 default:
Larry Johnson632de062008-01-11 23:26:18 -0500982 printf ("Module data width %d bits\n",
983 (data[7] << 8) | data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500984 break;
985 }
986
wdenk4b9206e2004-03-23 22:14:11 +0000987 puts ("Interface signal levels ");
wdenk81a88242002-10-26 15:22:42 +0000988 switch(data[8]) {
Larry Johnson0df6b842008-01-10 22:23:39 -0500989 case 0: puts ("TTL 5.0 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +0000990 case 1: puts ("LVTTL\n"); break;
Larry Johnson0df6b842008-01-10 22:23:39 -0500991 case 2: puts ("HSTL 1.5 V\n"); break;
992 case 3: puts ("SSTL 3.3 V\n"); break;
993 case 4: puts ("SSTL 2.5 V\n"); break;
994 case 5: puts ("SSTL 1.8 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +0000995 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +0000996 }
Larry Johnson0df6b842008-01-10 22:23:39 -0500997
998 switch (type) {
999 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001000 printf ("SDRAM cycle time ");
1001 print_ddr2_tcyc (data[9]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001002 break;
1003 default:
Larry Johnson632de062008-01-11 23:26:18 -05001004 printf ("SDRAM cycle time %d.%d ns\n",
1005 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001006 break;
1007 }
1008
1009 switch (type) {
1010 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001011 printf ("SDRAM access time 0.%d%d ns\n",
1012 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001013 break;
1014 default:
Larry Johnson632de062008-01-11 23:26:18 -05001015 printf ("SDRAM access time %d.%d ns\n",
1016 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001017 break;
1018 }
1019
wdenk4b9206e2004-03-23 22:14:11 +00001020 puts ("EDC configuration ");
Larry Johnson632de062008-01-11 23:26:18 -05001021 switch (data[11]) {
wdenk4b9206e2004-03-23 22:14:11 +00001022 case 0: puts ("None\n"); break;
1023 case 1: puts ("Parity\n"); break;
1024 case 2: puts ("ECC\n"); break;
1025 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001026 }
Larry Johnson632de062008-01-11 23:26:18 -05001027
Timur Tabie857a5b2006-11-28 12:09:35 -06001028 if ((data[12] & 0x80) == 0)
wdenk4b9206e2004-03-23 22:14:11 +00001029 puts ("No self refresh, rate ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001030 else
wdenk4b9206e2004-03-23 22:14:11 +00001031 puts ("Self refresh, rate ");
Larry Johnson632de062008-01-11 23:26:18 -05001032
wdenk81a88242002-10-26 15:22:42 +00001033 switch(data[12] & 0x7F) {
Larry Johnson632de062008-01-11 23:26:18 -05001034 case 0: puts ("15.625 us\n"); break;
1035 case 1: puts ("3.9 us\n"); break;
1036 case 2: puts ("7.8 us\n"); break;
1037 case 3: puts ("31.3 us\n"); break;
1038 case 4: puts ("62.5 us\n"); break;
1039 case 5: puts ("125 us\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001040 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001041 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001042
1043 switch (type) {
1044 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001045 printf ("SDRAM width (primary) %d\n", data[13]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001046 break;
1047 default:
Larry Johnson632de062008-01-11 23:26:18 -05001048 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001049 if ((data[13] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001050 printf (" (second bank) %d\n",
1051 2 * (data[13] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001052 }
1053 break;
wdenk81a88242002-10-26 15:22:42 +00001054 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001055
1056 switch (type) {
1057 case DDR2:
1058 if (data[14] != 0)
Larry Johnson632de062008-01-11 23:26:18 -05001059 printf ("EDC width %d\n", data[14]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001060 break;
1061 default:
1062 if (data[14] != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001063 printf ("EDC width %d\n",
1064 data[14] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001065
1066 if ((data[14] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001067 printf (" (second bank) %d\n",
1068 2 * (data[14] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001069 }
1070 }
1071 break;
1072 }
1073
Larry Johnson632de062008-01-11 23:26:18 -05001074 if (DDR2 != type) {
1075 printf ("Min clock delay, back-to-back random column addresses "
1076 "%d\n", data[15]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001077 }
1078
wdenk4b9206e2004-03-23 22:14:11 +00001079 puts ("Burst length(s) ");
1080 if (data[16] & 0x80) puts (" Page");
1081 if (data[16] & 0x08) puts (" 8");
1082 if (data[16] & 0x04) puts (" 4");
1083 if (data[16] & 0x02) puts (" 2");
1084 if (data[16] & 0x01) puts (" 1");
1085 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001086 printf ("Number of banks %d\n", data[17]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001087
1088 switch (type) {
1089 case DDR2:
1090 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001091 decode_bits (data[18], decode_CAS_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001092 putc ('\n');
1093 break;
1094 default:
1095 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001096 decode_bits (data[18], decode_CAS_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001097 putc ('\n');
1098 break;
1099 }
1100
1101 if (DDR2 != type) {
1102 puts ("CS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001103 decode_bits (data[19], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001104 putc ('\n');
1105 }
1106
1107 if (DDR2 != type) {
1108 puts ("WE latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001109 decode_bits (data[20], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001110 putc ('\n');
1111 }
1112
1113 switch (type) {
1114 case DDR2:
1115 puts ("Module attributes:\n");
1116 if (data[21] & 0x80)
1117 puts (" TBD (bit 7)\n");
1118 if (data[21] & 0x40)
1119 puts (" Analysis probe installed\n");
1120 if (data[21] & 0x20)
1121 puts (" TBD (bit 5)\n");
1122 if (data[21] & 0x10)
1123 puts (" FET switch external enable\n");
Larry Johnson632de062008-01-11 23:26:18 -05001124 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
Larry Johnson0df6b842008-01-10 22:23:39 -05001125 if (data[20] & 0x11) {
Larry Johnson632de062008-01-11 23:26:18 -05001126 printf (" %d active registers on DIMM\n",
1127 (data[21] & 0x03) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001128 }
1129 break;
1130 default:
1131 puts ("Module attributes:\n");
1132 if (!data[21])
1133 puts (" (none)\n");
Larry Johnson632de062008-01-11 23:26:18 -05001134 else
1135 decode_bits (data[21], decode_byte21_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001136 break;
1137 }
1138
1139 switch (type) {
1140 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001141 decode_bits (data[22], decode_byte22_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001142 break;
1143 default:
1144 puts ("Device attributes:\n");
1145 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1146 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1147 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1148 else puts (" Upper Vcc tolerance 10%\n");
1149 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1150 else puts (" Lower Vcc tolerance 10%\n");
1151 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1152 if (data[22] & 0x04) puts (" Supports precharge all\n");
1153 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1154 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1155 break;
1156 }
1157
1158 switch (type) {
1159 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001160 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1161 print_ddr2_tcyc (data[23]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001162 break;
1163 default:
Larry Johnson632de062008-01-11 23:26:18 -05001164 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1165 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001166 break;
1167 }
1168
1169 switch (type) {
1170 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001171 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1172 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001173 break;
1174 default:
Larry Johnson632de062008-01-11 23:26:18 -05001175 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1176 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001177 break;
1178 }
1179
1180 switch (type) {
1181 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001182 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1183 print_ddr2_tcyc (data[25]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001184 break;
1185 default:
Larry Johnson632de062008-01-11 23:26:18 -05001186 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1187 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001188 break;
1189 }
1190
1191 switch (type) {
1192 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001193 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1194 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001195 break;
1196 default:
Larry Johnson632de062008-01-11 23:26:18 -05001197 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1198 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001199 break;
1200 }
1201
1202 switch (type) {
1203 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001204 printf ("Minimum row precharge %d.%02d ns\n",
1205 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001206 break;
1207 default:
Larry Johnson632de062008-01-11 23:26:18 -05001208 printf ("Minimum row precharge %d ns\n", data[27]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001209 break;
1210 }
1211
1212 switch (type) {
1213 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001214 printf ("Row active to row active min %d.%02d ns\n",
1215 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001216 break;
1217 default:
Larry Johnson632de062008-01-11 23:26:18 -05001218 printf ("Row active to row active min %d ns\n", data[28]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001219 break;
1220 }
1221
1222 switch (type) {
1223 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001224 printf ("RAS to CAS delay min %d.%02d ns\n",
1225 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001226 break;
1227 default:
Larry Johnson632de062008-01-11 23:26:18 -05001228 printf ("RAS to CAS delay min %d ns\n", data[29]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001229 break;
1230 }
1231
Larry Johnson632de062008-01-11 23:26:18 -05001232 printf ("Minimum RAS pulse width %d ns\n", data[30]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001233
1234 switch (type) {
1235 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001236 puts ("Density of each row ");
1237 decode_bits (data[31], decode_row_density_DDR2, 1);
1238 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001239 break;
1240 default:
Larry Johnson632de062008-01-11 23:26:18 -05001241 puts ("Density of each row ");
1242 decode_bits (data[31], decode_row_density_default, 1);
1243 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001244 break;
1245 }
1246
1247 switch (type) {
1248 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001249 puts ("Command and Address setup ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001250 if (data[32] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001251 printf ("1.%d%d ns\n",
1252 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001253 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001254 printf ("0.%d%d ns\n",
1255 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001256 }
1257 break;
1258 default:
Larry Johnson632de062008-01-11 23:26:18 -05001259 printf ("Command and Address setup %c%d.%d ns\n",
1260 (data[32] & 0x80) ? '-' : '+',
1261 (data[32] >> 4) & 0x07, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001262 break;
1263 }
1264
1265 switch (type) {
1266 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001267 puts ("Command and Address hold ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001268 if (data[33] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001269 printf ("1.%d%d ns\n",
1270 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001271 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001272 printf ("0.%d%d ns\n",
1273 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001274 }
1275 break;
1276 default:
Larry Johnson632de062008-01-11 23:26:18 -05001277 printf ("Command and Address hold %c%d.%d ns\n",
1278 (data[33] & 0x80) ? '-' : '+',
1279 (data[33] >> 4) & 0x07, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001280 break;
1281 }
1282
1283 switch (type) {
1284 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001285 printf ("Data signal input setup 0.%d%d ns\n",
1286 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001287 break;
1288 default:
Larry Johnson632de062008-01-11 23:26:18 -05001289 printf ("Data signal input setup %c%d.%d ns\n",
1290 (data[34] & 0x80) ? '-' : '+',
1291 (data[34] >> 4) & 0x07, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001292 break;
1293 }
1294
1295 switch (type) {
1296 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001297 printf ("Data signal input hold 0.%d%d ns\n",
1298 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001299 break;
1300 default:
Larry Johnson632de062008-01-11 23:26:18 -05001301 printf ("Data signal input hold %c%d.%d ns\n",
1302 (data[35] & 0x80) ? '-' : '+',
1303 (data[35] >> 4) & 0x07, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001304 break;
1305 }
1306
wdenk4b9206e2004-03-23 22:14:11 +00001307 puts ("Manufacturer's JEDEC ID ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001308 for (j = 64; j <= 71; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001309 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001310 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001311 printf ("Manufacturing Location %02X\n", data[72]);
wdenk4b9206e2004-03-23 22:14:11 +00001312 puts ("Manufacturer's Part Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001313 for (j = 73; j <= 90; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001314 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001315 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001316 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1317 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
wdenk4b9206e2004-03-23 22:14:11 +00001318 puts ("Assembly Serial Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001319 for (j = 95; j <= 98; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001320 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001321 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001322
Larry Johnson0df6b842008-01-10 22:23:39 -05001323 if (DDR2 != type) {
Larry Johnson632de062008-01-11 23:26:18 -05001324 printf ("Speed rating PC%d\n",
1325 data[126] == 0x66 ? 66 : data[126]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001326 }
wdenk81a88242002-10-26 15:22:42 +00001327 return 0;
1328}
Jon Loeliger90253172007-07-10 11:02:44 -05001329#endif
wdenk81a88242002-10-26 15:22:42 +00001330
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001331/*
1332 * Syntax:
1333 * i2c edid {i2c_chip}
1334 */
1335#if defined(CONFIG_I2C_EDID)
1336int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1337{
1338 u_char chip;
1339 struct edid1_info edid;
1340
1341 if (argc < 2) {
1342 cmd_usage(cmdtp);
1343 return 1;
1344 }
1345
1346 chip = simple_strtoul(argv[1], NULL, 16);
1347 if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1348 puts("Error reading EDID content.\n");
1349 return 1;
1350 }
1351
1352 if (edid_check_info(&edid)) {
1353 puts("Content isn't valid EDID.\n");
1354 return 1;
1355 }
1356
1357 edid_print_info(&edid);
1358 return 0;
1359
1360}
1361#endif /* CONFIG_I2C_EDID */
1362
Marek Vasut06afa382012-11-12 14:34:27 +00001363/**
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001364 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
Marek Vasut06afa382012-11-12 14:34:27 +00001365 * @cmdtp: Command data struct pointer
1366 * @flag: Command flag
1367 * @argc: Command-line argument count
1368 * @argv: Array of command-line arguments
1369 *
1370 * Returns zero always.
1371 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001372#if defined(CONFIG_SYS_I2C)
1373int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher67b23a32008-10-15 09:39:47 +02001374{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001375 int i;
1376#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1377 int j;
1378#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001379
1380 if (argc == 1) {
1381 /* show all busses */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001382 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1383 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1384#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1385 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1386 if (i2c_bus[i].next_hop[j].chip == 0)
1387 break;
1388 printf("->%s@0x%2x:%d",
1389 i2c_bus[i].next_hop[j].mux.name,
1390 i2c_bus[i].next_hop[j].chip,
1391 i2c_bus[i].next_hop[j].channel);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001392 }
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001393#endif
1394 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001395 }
1396 } else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001397 /* show specific bus */
1398 i = simple_strtoul(argv[1], NULL, 10);
1399 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1400 printf("Invalid bus %d\n", i);
1401 return -1;
1402 }
1403 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1404#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1405 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1406 if (i2c_bus[i].next_hop[j].chip == 0)
1407 break;
1408 printf("->%s@0x%2x:%d",
1409 i2c_bus[i].next_hop[j].mux.name,
1410 i2c_bus[i].next_hop[j].chip,
1411 i2c_bus[i].next_hop[j].channel);
1412 }
1413#endif
1414 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001415 }
Heiko Schocher67b23a32008-10-15 09:39:47 +02001416
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001417 return 0;
1418}
1419#endif
1420
Marek Vasut06afa382012-11-12 14:34:27 +00001421/**
1422 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1423 * @cmdtp: Command data struct pointer
1424 * @flag: Command flag
1425 * @argc: Command-line argument count
1426 * @argv: Array of command-line arguments
1427 *
1428 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1429 * on error.
1430 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001431#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
1432int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001433{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001434 int ret = 0;
1435 unsigned int bus_no;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001436
Timur Tabie857a5b2006-11-28 12:09:35 -06001437 if (argc == 1)
1438 /* querying current setting */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001439 printf("Current bus is %d\n", i2c_get_bus_num());
Timur Tabie857a5b2006-11-28 12:09:35 -06001440 else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001441 bus_no = simple_strtoul(argv[1], NULL, 10);
Heiko Schocher880a4122013-08-23 09:39:16 +02001442#if defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001443 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1444 printf("Invalid bus %d\n", bus_no);
1445 return -1;
1446 }
Heiko Schocher880a4122013-08-23 09:39:16 +02001447#endif
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001448 printf("Setting bus to %d\n", bus_no);
1449 ret = i2c_set_bus_num(bus_no);
Timur Tabie857a5b2006-11-28 12:09:35 -06001450 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001451 printf("Failure changing bus number (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001452 }
1453 return ret;
1454}
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001455#endif /* defined(CONFIG_SYS_I2C) */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001456
Marek Vasut06afa382012-11-12 14:34:27 +00001457/**
1458 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1459 * @cmdtp: Command data struct pointer
1460 * @flag: Command flag
1461 * @argc: Command-line argument count
1462 * @argv: Array of command-line arguments
1463 *
1464 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1465 * on error.
1466 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001467static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001468{
1469 int speed, ret=0;
1470
Timur Tabie857a5b2006-11-28 12:09:35 -06001471 if (argc == 1)
1472 /* querying current speed */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001473 printf("Current bus speed=%d\n", i2c_get_bus_speed());
Timur Tabie857a5b2006-11-28 12:09:35 -06001474 else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001475 speed = simple_strtoul(argv[1], NULL, 10);
1476 printf("Setting bus speed to %d Hz\n", speed);
1477 ret = i2c_set_bus_speed(speed);
Timur Tabie857a5b2006-11-28 12:09:35 -06001478 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001479 printf("Failure changing bus speed (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001480 }
1481 return ret;
1482}
1483
Marek Vasut06afa382012-11-12 14:34:27 +00001484/**
1485 * do_i2c_mm() - Handle the "i2c mm" command-line command
1486 * @cmdtp: Command data struct pointer
1487 * @flag: Command flag
1488 * @argc: Command-line argument count
1489 * @argv: Array of command-line arguments
1490 *
1491 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1492 * on error.
1493 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001494static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001495{
1496 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1497}
1498
Marek Vasut06afa382012-11-12 14:34:27 +00001499/**
1500 * do_i2c_nm() - Handle the "i2c nm" command-line command
1501 * @cmdtp: Command data struct pointer
1502 * @flag: Command flag
1503 * @argc: Command-line argument count
1504 * @argv: Array of command-line arguments
1505 *
1506 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1507 * on error.
1508 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001509static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001510{
1511 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1512}
1513
Marek Vasut06afa382012-11-12 14:34:27 +00001514/**
1515 * do_i2c_reset() - Handle the "i2c reset" command-line command
1516 * @cmdtp: Command data struct pointer
1517 * @flag: Command flag
1518 * @argc: Command-line argument count
1519 * @argv: Array of command-line arguments
1520 *
1521 * Returns zero always.
1522 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001523static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001524{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001525#if defined(CONFIG_SYS_I2C)
1526 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1527#else
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001528 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001529#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001530 return 0;
1531}
1532
1533static cmd_tbl_t cmd_i2c_sub[] = {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001534#if defined(CONFIG_SYS_I2C)
1535 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001536#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001537 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001538#if defined(CONFIG_SYS_I2C) || \
1539 defined(CONFIG_I2C_MULTI_BUS)
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001540 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1541#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001542#if defined(CONFIG_I2C_EDID)
1543 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1544#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001545 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1546 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1547 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1548 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1549 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1550 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001551 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
York Sunff5d2dc2012-09-16 08:02:30 +00001552 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001553 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1554#if defined(CONFIG_CMD_SDRAM)
1555 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1556#endif
1557 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1558};
1559
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001560#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001561void i2c_reloc(void) {
1562 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1563}
1564#endif
1565
Marek Vasut06afa382012-11-12 14:34:27 +00001566/**
1567 * do_i2c() - Handle the "i2c" command-line command
1568 * @cmdtp: Command data struct pointer
1569 * @flag: Command flag
1570 * @argc: Command-line argument count
1571 * @argv: Array of command-line arguments
1572 *
1573 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1574 * on error.
1575 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001576static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001577{
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001578 cmd_tbl_t *c;
1579
Heiko Schocher4444b222010-09-17 13:10:35 +02001580 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001581 return CMD_RET_USAGE;
Heiko Schocher4444b222010-09-17 13:10:35 +02001582
Peter Tysere96ad5d2009-04-18 22:34:04 -05001583 /* Strip off leading 'i2c' command argument */
1584 argc--;
1585 argv++;
1586
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001587 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1588
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001589 if (c)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001590 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001591 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00001592 return CMD_RET_USAGE;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001593}
wdenk8bde7f72003-06-27 21:31:46 +00001594
1595/***************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00001596#ifdef CONFIG_SYS_LONGHELP
1597static char i2c_help_text[] =
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001598#if defined(CONFIG_SYS_I2C)
1599 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001600#endif
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001601 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001602#if defined(CONFIG_SYS_I2C) || \
1603 defined(CONFIG_I2C_MULTI_BUS)
Peter Tyser9bc2e4e2008-10-01 12:25:04 -05001604 "i2c dev [dev] - show or set current I2C bus\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001605#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001606#if defined(CONFIG_I2C_EDID)
1607 "i2c edid chip - print EDID configuration information\n"
1608#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001609 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001610 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1611 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1612 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1613 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
Eric Nelson54b99e52012-09-23 10:12:56 +00001614 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001615 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
York Sunff5d2dc2012-09-16 08:02:30 +00001616 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
Heiko Schochere43a27c2008-10-15 09:33:30 +02001617 "i2c reset - re-init the I2C Controller\n"
Jon Loeligerc76fe472007-07-08 18:02:23 -05001618#if defined(CONFIG_CMD_SDRAM)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001619 "i2c sdram chip - print SDRAM configuration information\n"
Jon Loeliger90253172007-07-10 11:02:44 -05001620#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001621 "i2c speed [speed] - show or set I2C bus speed";
1622#endif
1623
1624U_BOOT_CMD(
1625 i2c, 6, 1, do_i2c,
1626 "I2C sub-system",
1627 i2c_help_text
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001628);