blob: ebce7d4c3c05162a675bb4a01fdec15dd287e172 [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>
69#include <command.h>
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +000070#include <edid.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020071#include <environment.h>
wdenk81a88242002-10-26 15:22:42 +000072#include <i2c.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020073#include <malloc.h>
wdenk81a88242002-10-26 15:22:42 +000074#include <asm/byteorder.h>
Marek Vasut2515d842012-11-12 14:34:25 +000075#include <linux/compiler.h>
wdenk81a88242002-10-26 15:22:42 +000076
Heiko Schocher3f4978c2012-01-16 21:12:24 +000077DECLARE_GLOBAL_DATA_PTR;
78
wdenk81a88242002-10-26 15:22:42 +000079/* Display values from last command.
80 * Memory modify remembered values are different from display memory.
81 */
82static uchar i2c_dp_last_chip;
83static uint i2c_dp_last_addr;
84static uint i2c_dp_last_alen;
85static uint i2c_dp_last_length = 0x10;
86
87static uchar i2c_mm_last_chip;
88static uint i2c_mm_last_addr;
89static uint i2c_mm_last_alen;
90
Ben Warrenbb99ad62006-09-07 16:50:54 -040091/* If only one I2C bus is present, the list of devices to ignore when
92 * the probe command is issued is represented by a 1D array of addresses.
93 * When multiple buses are present, the list is an array of bus-address
94 * pairs. The following macros take care of this */
95
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020096#if defined(CONFIG_SYS_I2C_NOPROBES)
Heiko Schocher9a2accb2012-10-25 10:32:14 +020097#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
Ben Warrenbb99ad62006-09-07 16:50:54 -040098static struct
99{
100 uchar bus;
101 uchar addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200102} i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400103#define GET_BUS_NUM i2c_get_bus_num()
104#define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
105#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
106#define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
107#else /* single bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200108static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400109#define GET_BUS_NUM 0
110#define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
111#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
112#define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000113#endif /* defined(CONFIG_SYS_I2C) */
Heiko Schocher67b23a32008-10-15 09:39:47 +0200114#endif
115
Frans Meulenbroeksa266fe92010-03-26 09:46:40 +0100116#define DISP_LINE_LEN 16
117
Marek Vasut06afa382012-11-12 14:34:27 +0000118/**
119 * i2c_init_board() - Board-specific I2C bus init
120 *
121 * This function is the default no-op implementation of I2C bus
122 * initialization. This function can be overriden by board-specific
123 * implementation if needed.
124 */
Marek Vasut2515d842012-11-12 14:34:25 +0000125__weak
126void i2c_init_board(void)
Stefan Biglerc649dda2011-04-08 16:24:08 +0200127{
Stefan Biglerc649dda2011-04-08 16:24:08 +0200128}
Stefan Biglerc649dda2011-04-08 16:24:08 +0200129
Peter Tyser655b34a2009-04-18 22:34:01 -0500130/* TODO: Implement architecture-specific get/set functions */
Marek Vasut06afa382012-11-12 14:34:27 +0000131
132/**
133 * i2c_get_bus_speed() - Return I2C bus speed
134 *
135 * This function is the default implementation of function for retrieveing
136 * the current I2C bus speed in Hz.
137 *
138 * A driver implementing runtime switching of I2C bus speed must override
139 * this function to report the speed correctly. Simple or legacy drivers
140 * can use this fallback.
141 *
142 * Returns I2C bus speed in Hz.
143 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000144#if !defined(CONFIG_SYS_I2C)
145/*
146 * TODO: Implement architecture-specific get/set functions
147 * Should go away, if we switched completely to new multibus support
148 */
Marek Vasut2515d842012-11-12 14:34:25 +0000149__weak
150unsigned int i2c_get_bus_speed(void)
Peter Tyser655b34a2009-04-18 22:34:01 -0500151{
152 return CONFIG_SYS_I2C_SPEED;
153}
Peter Tyser655b34a2009-04-18 22:34:01 -0500154
Marek Vasut06afa382012-11-12 14:34:27 +0000155/**
156 * i2c_set_bus_speed() - Configure I2C bus speed
157 * @speed: Newly set speed of the I2C bus in Hz
158 *
159 * This function is the default implementation of function for setting
160 * the I2C bus speed in Hz.
161 *
162 * A driver implementing runtime switching of I2C bus speed must override
163 * this function to report the speed correctly. Simple or legacy drivers
164 * can use this fallback.
165 *
166 * Returns zero on success, negative value on error.
167 */
Marek Vasut2515d842012-11-12 14:34:25 +0000168__weak
169int i2c_set_bus_speed(unsigned int speed)
Peter Tyser655b34a2009-04-18 22:34:01 -0500170{
171 if (speed != CONFIG_SYS_I2C_SPEED)
172 return -1;
173
174 return 0;
175}
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000176#endif
Peter Tyser655b34a2009-04-18 22:34:01 -0500177
Marek Vasut06afa382012-11-12 14:34:27 +0000178/**
179 * get_alen() - Small parser helper function to get address length
180 *
181 * Returns the address length.
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100182 */
183static uint get_alen(char *arg)
184{
185 int j;
186 int alen;
187
188 alen = 1;
189 for (j = 0; j < 8; j++) {
190 if (arg[j] == '.') {
191 alen = arg[j+1] - '0';
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100192 break;
193 } else if (arg[j] == '\0')
194 break;
195 }
196 return alen;
197}
198
Marek Vasut06afa382012-11-12 14:34:27 +0000199/**
200 * do_i2c_read() - Handle the "i2c read" command-line command
201 * @cmdtp: Command data struct pointer
202 * @flag: Command flag
203 * @argc: Command-line argument count
204 * @argv: Array of command-line arguments
205 *
206 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
207 * on error.
208 *
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100209 * Syntax:
210 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
211 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200212static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100213{
214 u_char chip;
215 uint devaddr, alen, length;
216 u_char *memaddr;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100217
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200218 if (argc != 5)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000219 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100220
221 /*
222 * I2C chip address
223 */
224 chip = simple_strtoul(argv[1], NULL, 16);
225
226 /*
227 * I2C data address within the chip. This can be 1 or
228 * 2 bytes long. Some day it might be 3 bytes long :-).
229 */
230 devaddr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100231 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200232 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000233 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100234
235 /*
236 * Length is the number of objects, not number of bytes.
237 */
238 length = simple_strtoul(argv[3], NULL, 16);
239
240 /*
241 * memaddr is the address where to store things in memory
242 */
243 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
244
245 if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
246 puts ("Error reading the chip.\n");
247 return 1;
248 }
249 return 0;
250}
251
York Sunff5d2dc2012-09-16 08:02:30 +0000252static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
253{
254 u_char chip;
255 uint devaddr, alen, length;
256 u_char *memaddr;
257
258 if (argc != 5)
259 return cmd_usage(cmdtp);
260
261 /*
262 * memaddr is the address where to store things in memory
263 */
264 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
265
266 /*
267 * I2C chip address
268 */
269 chip = simple_strtoul(argv[2], NULL, 16);
270
271 /*
272 * I2C data address within the chip. This can be 1 or
273 * 2 bytes long. Some day it might be 3 bytes long :-).
274 */
275 devaddr = simple_strtoul(argv[3], NULL, 16);
276 alen = get_alen(argv[3]);
277 if (alen > 3)
278 return cmd_usage(cmdtp);
279
280 /*
281 * Length is the number of objects, not number of bytes.
282 */
283 length = simple_strtoul(argv[4], NULL, 16);
284
285 while (length-- > 0) {
286 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
287 puts("Error writing to the chip.\n");
288 return 1;
289 }
290/*
291 * No write delay with FRAM devices.
292 */
293#if !defined(CONFIG_SYS_I2C_FRAM)
294 udelay(11000);
295#endif
296 }
297 return 0;
298}
299
Marek Vasut06afa382012-11-12 14:34:27 +0000300/**
301 * do_i2c_md() - Handle the "i2c md" command-line command
302 * @cmdtp: Command data struct pointer
303 * @flag: Command flag
304 * @argc: Command-line argument count
305 * @argv: Array of command-line arguments
306 *
307 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
308 * on error.
309 *
Frans Meulenbroeks4a8cf332010-03-26 09:46:39 +0100310 * Syntax:
311 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
312 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200313static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000314{
315 u_char chip;
316 uint addr, alen, length;
317 int j, nbytes, linebytes;
318
319 /* We use the last specified parameters, unless new ones are
320 * entered.
321 */
322 chip = i2c_dp_last_chip;
323 addr = i2c_dp_last_addr;
324 alen = i2c_dp_last_alen;
325 length = i2c_dp_last_length;
326
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200327 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000328 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000329
330 if ((flag & CMD_FLAG_REPEAT) == 0) {
331 /*
332 * New command specified.
333 */
wdenk81a88242002-10-26 15:22:42 +0000334
335 /*
336 * I2C chip address
337 */
338 chip = simple_strtoul(argv[1], NULL, 16);
339
340 /*
341 * I2C data address within the chip. This can be 1 or
342 * 2 bytes long. Some day it might be 3 bytes long :-).
343 */
344 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100345 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200346 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000347 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000348
349 /*
350 * If another parameter, it is the length to display.
351 * Length is the number of objects, not number of bytes.
352 */
353 if (argc > 3)
354 length = simple_strtoul(argv[3], NULL, 16);
355 }
356
357 /*
358 * Print the lines.
359 *
360 * We buffer all read data, so we can make sure data is read only
361 * once.
362 */
363 nbytes = length;
364 do {
365 unsigned char linebuf[DISP_LINE_LEN];
366 unsigned char *cp;
367
368 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
369
Timur Tabie857a5b2006-11-28 12:09:35 -0600370 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000371 puts ("Error reading the chip.\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600372 else {
wdenk81a88242002-10-26 15:22:42 +0000373 printf("%04x:", addr);
374 cp = linebuf;
375 for (j=0; j<linebytes; j++) {
376 printf(" %02x", *cp++);
377 addr++;
378 }
wdenk4b9206e2004-03-23 22:14:11 +0000379 puts (" ");
wdenk81a88242002-10-26 15:22:42 +0000380 cp = linebuf;
381 for (j=0; j<linebytes; j++) {
382 if ((*cp < 0x20) || (*cp > 0x7e))
wdenk4b9206e2004-03-23 22:14:11 +0000383 puts (".");
wdenk81a88242002-10-26 15:22:42 +0000384 else
385 printf("%c", *cp);
386 cp++;
387 }
wdenk4b9206e2004-03-23 22:14:11 +0000388 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000389 }
390 nbytes -= linebytes;
391 } while (nbytes > 0);
392
393 i2c_dp_last_chip = chip;
394 i2c_dp_last_addr = addr;
395 i2c_dp_last_alen = alen;
396 i2c_dp_last_length = length;
397
398 return 0;
399}
400
Marek Vasut06afa382012-11-12 14:34:27 +0000401/**
402 * do_i2c_mw() - Handle the "i2c mw" command-line command
403 * @cmdtp: Command data struct pointer
404 * @flag: Command flag
405 * @argc: Command-line argument count
406 * @argv: Array of command-line arguments
407 *
408 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
409 * on error.
wdenk81a88242002-10-26 15:22:42 +0000410 *
411 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500412 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
wdenk81a88242002-10-26 15:22:42 +0000413 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200414static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000415{
416 uchar chip;
417 ulong addr;
418 uint alen;
419 uchar byte;
420 int count;
wdenk81a88242002-10-26 15:22:42 +0000421
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200422 if ((argc < 4) || (argc > 5))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000423 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000424
425 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200426 * Chip is always specified.
427 */
wdenk81a88242002-10-26 15:22:42 +0000428 chip = simple_strtoul(argv[1], NULL, 16);
429
430 /*
431 * Address is always specified.
432 */
433 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100434 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200435 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000436 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000437
438 /*
439 * Value to write is always specified.
440 */
441 byte = simple_strtoul(argv[3], NULL, 16);
442
443 /*
444 * Optional count
445 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600446 if (argc == 5)
wdenk81a88242002-10-26 15:22:42 +0000447 count = simple_strtoul(argv[4], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600448 else
wdenk81a88242002-10-26 15:22:42 +0000449 count = 1;
wdenk81a88242002-10-26 15:22:42 +0000450
451 while (count-- > 0) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600452 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000453 puts ("Error writing the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000454 /*
455 * Wait for the write to complete. The write can take
456 * up to 10mSec (we allow a little more time).
wdenk81a88242002-10-26 15:22:42 +0000457 */
d4f5c722005-08-12 21:16:13 +0200458/*
459 * No write delay with FRAM devices.
460 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200461#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk81a88242002-10-26 15:22:42 +0000462 udelay(11000);
d4f5c722005-08-12 21:16:13 +0200463#endif
wdenk81a88242002-10-26 15:22:42 +0000464 }
465
Marek Vasut06afa382012-11-12 14:34:27 +0000466 return 0;
wdenk81a88242002-10-26 15:22:42 +0000467}
468
Marek Vasut06afa382012-11-12 14:34:27 +0000469/**
470 * do_i2c_crc() - Handle the "i2c crc32" command-line command
471 * @cmdtp: Command data struct pointer
472 * @flag: Command flag
473 * @argc: Command-line argument count
474 * @argv: Array of command-line arguments
475 *
476 * Calculate a CRC on memory
477 *
478 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
479 * on error.
wdenk81a88242002-10-26 15:22:42 +0000480 *
481 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500482 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
wdenk81a88242002-10-26 15:22:42 +0000483 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200484static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000485{
486 uchar chip;
487 ulong addr;
488 uint alen;
489 int count;
490 uchar byte;
491 ulong crc;
492 ulong err;
wdenk81a88242002-10-26 15:22:42 +0000493
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200494 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000495 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000496
497 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200498 * Chip is always specified.
499 */
wdenk81a88242002-10-26 15:22:42 +0000500 chip = simple_strtoul(argv[1], NULL, 16);
501
502 /*
503 * Address is always specified.
504 */
505 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100506 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200507 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000508 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000509
510 /*
511 * Count is always specified
512 */
513 count = simple_strtoul(argv[3], NULL, 16);
514
515 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
516 /*
517 * CRC a byte at a time. This is going to be slooow, but hey, the
518 * memories are small and slow too so hopefully nobody notices.
519 */
520 crc = 0;
521 err = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600522 while (count-- > 0) {
523 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
wdenk81a88242002-10-26 15:22:42 +0000524 err++;
wdenk81a88242002-10-26 15:22:42 +0000525 crc = crc32 (crc, &byte, 1);
526 addr++;
527 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600528 if (err > 0)
wdenk4b9206e2004-03-23 22:14:11 +0000529 puts ("Error reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600530 else
wdenk81a88242002-10-26 15:22:42 +0000531 printf ("%08lx\n", crc);
wdenk81a88242002-10-26 15:22:42 +0000532
533 return 0;
534}
535
Marek Vasut06afa382012-11-12 14:34:27 +0000536/**
537 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
538 * @cmdtp: Command data struct pointer
539 * @flag: Command flag
540 * @argc: Command-line argument count
541 * @argv: Array of command-line arguments
542 *
543 * Modify memory.
544 *
545 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
546 * on error.
wdenk81a88242002-10-26 15:22:42 +0000547 *
548 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500549 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
550 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
wdenk81a88242002-10-26 15:22:42 +0000551 */
wdenk81a88242002-10-26 15:22:42 +0000552static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200553mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000554{
555 uchar chip;
556 ulong addr;
557 uint alen;
558 ulong data;
559 int size = 1;
560 int nbytes;
wdenk81a88242002-10-26 15:22:42 +0000561
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200562 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000563 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000564
565#ifdef CONFIG_BOOT_RETRY_TIME
566 reset_cmd_timeout(); /* got a good command to get here */
567#endif
568 /*
569 * We use the last specified parameters, unless new ones are
570 * entered.
571 */
572 chip = i2c_mm_last_chip;
573 addr = i2c_mm_last_addr;
574 alen = i2c_mm_last_alen;
575
576 if ((flag & CMD_FLAG_REPEAT) == 0) {
577 /*
578 * New command specified. Check for a size specification.
579 * Defaults to byte if no or incorrect specification.
580 */
581 size = cmd_get_data_size(argv[0], 1);
582
583 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200584 * Chip is always specified.
585 */
wdenk81a88242002-10-26 15:22:42 +0000586 chip = simple_strtoul(argv[1], NULL, 16);
587
588 /*
589 * Address is always specified.
590 */
591 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100592 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200593 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000594 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000595 }
596
597 /*
598 * Print the address, followed by value. Then accept input for
599 * the next value. A non-converted value exits.
600 */
601 do {
602 printf("%08lx:", addr);
Timur Tabie857a5b2006-11-28 12:09:35 -0600603 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000604 puts ("\nError reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600605 else {
wdenk81a88242002-10-26 15:22:42 +0000606 data = cpu_to_be32(data);
Timur Tabie857a5b2006-11-28 12:09:35 -0600607 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000608 printf(" %02lx", (data >> 24) & 0x000000FF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600609 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000610 printf(" %04lx", (data >> 16) & 0x0000FFFF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600611 else
wdenk81a88242002-10-26 15:22:42 +0000612 printf(" %08lx", data);
wdenk81a88242002-10-26 15:22:42 +0000613 }
614
615 nbytes = readline (" ? ");
616 if (nbytes == 0) {
617 /*
618 * <CR> pressed as only input, don't modify current
619 * location and move to next.
620 */
621 if (incrflag)
622 addr += size;
623 nbytes = size;
624#ifdef CONFIG_BOOT_RETRY_TIME
625 reset_cmd_timeout(); /* good enough to not time out */
626#endif
627 }
628#ifdef CONFIG_BOOT_RETRY_TIME
Timur Tabie857a5b2006-11-28 12:09:35 -0600629 else if (nbytes == -2)
wdenk81a88242002-10-26 15:22:42 +0000630 break; /* timed out, exit the command */
wdenk81a88242002-10-26 15:22:42 +0000631#endif
632 else {
633 char *endp;
634
635 data = simple_strtoul(console_buffer, &endp, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600636 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000637 data = data << 24;
Timur Tabie857a5b2006-11-28 12:09:35 -0600638 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000639 data = data << 16;
wdenk81a88242002-10-26 15:22:42 +0000640 data = be32_to_cpu(data);
641 nbytes = endp - console_buffer;
642 if (nbytes) {
643#ifdef CONFIG_BOOT_RETRY_TIME
644 /*
645 * good enough to not time out
646 */
647 reset_cmd_timeout();
648#endif
Timur Tabie857a5b2006-11-28 12:09:35 -0600649 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000650 puts ("Error writing the chip.\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200651#ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
652 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk2535d602003-07-17 23:16:40 +0000653#endif
wdenk81a88242002-10-26 15:22:42 +0000654 if (incrflag)
655 addr += size;
656 }
657 }
658 } while (nbytes);
659
Peter Tyser08007072008-08-15 14:36:32 -0500660 i2c_mm_last_chip = chip;
661 i2c_mm_last_addr = addr;
662 i2c_mm_last_alen = alen;
wdenk81a88242002-10-26 15:22:42 +0000663
664 return 0;
665}
666
Marek Vasut06afa382012-11-12 14:34:27 +0000667/**
668 * do_i2c_probe() - Handle the "i2c probe" command-line command
669 * @cmdtp: Command data struct pointer
670 * @flag: Command flag
671 * @argc: Command-line argument count
672 * @argv: Array of command-line arguments
673 *
674 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
675 * on error.
676 *
wdenk81a88242002-10-26 15:22:42 +0000677 * Syntax:
Eric Nelson54b99e52012-09-23 10:12:56 +0000678 * i2c probe {addr}
679 *
680 * Returns zero (success) if one or more I2C devices was found
wdenk81a88242002-10-26 15:22:42 +0000681 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200682static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000683{
684 int j;
Eric Nelson54b99e52012-09-23 10:12:56 +0000685 int addr = -1;
686 int found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200687#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000688 int k, skip;
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000689 unsigned int bus = GET_BUS_NUM;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400690#endif /* NOPROBES */
wdenk81a88242002-10-26 15:22:42 +0000691
Eric Nelson54b99e52012-09-23 10:12:56 +0000692 if (argc == 2)
693 addr = simple_strtol(argv[1], 0, 16);
694
wdenk4b9206e2004-03-23 22:14:11 +0000695 puts ("Valid chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600696 for (j = 0; j < 128; j++) {
Eric Nelson54b99e52012-09-23 10:12:56 +0000697 if ((0 <= addr) && (j != addr))
698 continue;
699
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200700#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000701 skip = 0;
Axel Lincfb25cc2013-06-22 21:56:35 +0800702 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600703 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
wdenk81a88242002-10-26 15:22:42 +0000704 skip = 1;
705 break;
706 }
707 }
708 if (skip)
709 continue;
710#endif
Eric Nelson54b99e52012-09-23 10:12:56 +0000711 if (i2c_probe(j) == 0) {
wdenk81a88242002-10-26 15:22:42 +0000712 printf(" %02X", j);
Eric Nelson54b99e52012-09-23 10:12:56 +0000713 found++;
714 }
wdenk81a88242002-10-26 15:22:42 +0000715 }
wdenk4b9206e2004-03-23 22:14:11 +0000716 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000717
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200718#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000719 puts ("Excluded chip addresses:");
Axel Lincfb25cc2013-06-22 21:56:35 +0800720 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600721 if (COMPARE_BUS(bus,k))
Ben Warrenbb99ad62006-09-07 16:50:54 -0400722 printf(" %02X", NO_PROBE_ADDR(k));
723 }
wdenk4b9206e2004-03-23 22:14:11 +0000724 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000725#endif
726
Eric Nelson54b99e52012-09-23 10:12:56 +0000727 return (0 == found);
wdenk81a88242002-10-26 15:22:42 +0000728}
729
Marek Vasut06afa382012-11-12 14:34:27 +0000730/**
731 * do_i2c_loop() - Handle the "i2c loop" command-line command
732 * @cmdtp: Command data struct pointer
733 * @flag: Command flag
734 * @argc: Command-line argument count
735 * @argv: Array of command-line arguments
736 *
737 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
738 * on error.
739 *
wdenk81a88242002-10-26 15:22:42 +0000740 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500741 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
wdenk81a88242002-10-26 15:22:42 +0000742 * {length} - Number of bytes to read
743 * {delay} - A DECIMAL number and defaults to 1000 uSec
744 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200745static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000746{
747 u_char chip;
748 ulong alen;
749 uint addr;
750 uint length;
751 u_char bytes[16];
752 int delay;
wdenk81a88242002-10-26 15:22:42 +0000753
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200754 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000755 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000756
757 /*
758 * Chip is always specified.
759 */
760 chip = simple_strtoul(argv[1], NULL, 16);
761
762 /*
763 * Address is always specified.
764 */
765 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100766 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200767 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000768 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000769
770 /*
771 * Length is the number of objects, not number of bytes.
772 */
773 length = 1;
774 length = simple_strtoul(argv[3], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600775 if (length > sizeof(bytes))
wdenk81a88242002-10-26 15:22:42 +0000776 length = sizeof(bytes);
wdenk81a88242002-10-26 15:22:42 +0000777
778 /*
779 * The delay time (uSec) is optional.
780 */
781 delay = 1000;
Timur Tabie857a5b2006-11-28 12:09:35 -0600782 if (argc > 3)
wdenk81a88242002-10-26 15:22:42 +0000783 delay = simple_strtoul(argv[4], NULL, 10);
wdenk81a88242002-10-26 15:22:42 +0000784 /*
785 * Run the loop...
786 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600787 while (1) {
788 if (i2c_read(chip, addr, alen, bytes, length) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000789 puts ("Error reading the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000790 udelay(delay);
791 }
792
793 /* NOTREACHED */
794 return 0;
795}
796
wdenk81a88242002-10-26 15:22:42 +0000797/*
798 * The SDRAM command is separately configured because many
799 * (most?) embedded boards don't use SDRAM DIMMs.
Marek Vasut06afa382012-11-12 14:34:27 +0000800 *
801 * FIXME: Document and probably move elsewhere!
wdenk81a88242002-10-26 15:22:42 +0000802 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500803#if defined(CONFIG_CMD_SDRAM)
Larry Johnson632de062008-01-11 23:26:18 -0500804static void print_ddr2_tcyc (u_char const b)
805{
806 printf ("%d.", (b >> 4) & 0x0F);
807 switch (b & 0x0F) {
808 case 0x0:
809 case 0x1:
810 case 0x2:
811 case 0x3:
812 case 0x4:
813 case 0x5:
814 case 0x6:
815 case 0x7:
816 case 0x8:
817 case 0x9:
818 printf ("%d ns\n", b & 0x0F);
819 break;
820 case 0xA:
821 puts ("25 ns\n");
822 break;
823 case 0xB:
824 puts ("33 ns\n");
825 break;
826 case 0xC:
827 puts ("66 ns\n");
828 break;
829 case 0xD:
830 puts ("75 ns\n");
831 break;
832 default:
833 puts ("?? ns\n");
834 break;
835 }
836}
837
838static void decode_bits (u_char const b, char const *str[], int const do_once)
839{
840 u_char mask;
841
842 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
843 if (b & mask) {
844 puts (*str);
845 if (do_once)
846 return;
847 }
848 }
849}
wdenk81a88242002-10-26 15:22:42 +0000850
851/*
852 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500853 * i2c sdram {i2c_chip}
wdenk81a88242002-10-26 15:22:42 +0000854 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200855static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000856{
Larry Johnson632de062008-01-11 23:26:18 -0500857 enum { unknown, EDO, SDRAM, DDR2 } type;
858
wdenk81a88242002-10-26 15:22:42 +0000859 u_char chip;
860 u_char data[128];
861 u_char cksum;
862 int j;
863
Larry Johnson632de062008-01-11 23:26:18 -0500864 static const char *decode_CAS_DDR2[] = {
865 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
866 };
867
868 static const char *decode_CAS_default[] = {
869 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
870 };
871
872 static const char *decode_CS_WE_default[] = {
873 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
874 };
875
876 static const char *decode_byte21_default[] = {
877 " TBD (bit 7)\n",
878 " Redundant row address\n",
879 " Differential clock input\n",
880 " Registerd DQMB inputs\n",
881 " Buffered DQMB inputs\n",
882 " On-card PLL\n",
883 " Registered address/control lines\n",
884 " Buffered address/control lines\n"
885 };
886
887 static const char *decode_byte22_DDR2[] = {
888 " TBD (bit 7)\n",
889 " TBD (bit 6)\n",
890 " TBD (bit 5)\n",
891 " TBD (bit 4)\n",
892 " TBD (bit 3)\n",
893 " Supports partial array self refresh\n",
894 " Supports 50 ohm ODT\n",
895 " Supports weak driver\n"
896 };
897
898 static const char *decode_row_density_DDR2[] = {
899 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
900 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
901 };
902
903 static const char *decode_row_density_default[] = {
904 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
905 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
906 };
907
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200908 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000909 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200910
wdenk81a88242002-10-26 15:22:42 +0000911 /*
912 * Chip is always specified.
Larry Johnson632de062008-01-11 23:26:18 -0500913 */
914 chip = simple_strtoul (argv[1], NULL, 16);
wdenk81a88242002-10-26 15:22:42 +0000915
Larry Johnson632de062008-01-11 23:26:18 -0500916 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000917 puts ("No SDRAM Serial Presence Detect found.\n");
wdenk81a88242002-10-26 15:22:42 +0000918 return 1;
919 }
920
921 cksum = 0;
922 for (j = 0; j < 63; j++) {
923 cksum += data[j];
924 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600925 if (cksum != data[63]) {
wdenk81a88242002-10-26 15:22:42 +0000926 printf ("WARNING: Configuration data checksum failure:\n"
Larry Johnson632de062008-01-11 23:26:18 -0500927 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
wdenk81a88242002-10-26 15:22:42 +0000928 }
Larry Johnson632de062008-01-11 23:26:18 -0500929 printf ("SPD data revision %d.%d\n",
wdenk81a88242002-10-26 15:22:42 +0000930 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
Larry Johnson632de062008-01-11 23:26:18 -0500931 printf ("Bytes used 0x%02X\n", data[0]);
932 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
933
wdenk4b9206e2004-03-23 22:14:11 +0000934 puts ("Memory type ");
Larry Johnson632de062008-01-11 23:26:18 -0500935 switch (data[2]) {
Larry Johnson0df6b842008-01-10 22:23:39 -0500936 case 2:
937 type = EDO;
938 puts ("EDO\n");
939 break;
940 case 4:
941 type = SDRAM;
942 puts ("SDRAM\n");
943 break;
944 case 8:
945 type = DDR2;
946 puts ("DDR2\n");
947 break;
948 default:
949 type = unknown;
950 puts ("unknown\n");
951 break;
wdenk81a88242002-10-26 15:22:42 +0000952 }
Larry Johnson632de062008-01-11 23:26:18 -0500953
wdenk4b9206e2004-03-23 22:14:11 +0000954 puts ("Row address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600955 if ((data[3] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500956 printf ("%d\n", data[3] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600957 else
Larry Johnson632de062008-01-11 23:26:18 -0500958 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
959
wdenk4b9206e2004-03-23 22:14:11 +0000960 puts ("Column address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600961 if ((data[4] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500962 printf ("%d\n", data[4] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600963 else
Larry Johnson632de062008-01-11 23:26:18 -0500964 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -0500965
966 switch (type) {
967 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500968 printf ("Number of ranks %d\n",
969 (data[5] & 0x07) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -0500970 break;
971 default:
Larry Johnson632de062008-01-11 23:26:18 -0500972 printf ("Module rows %d\n", data[5]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500973 break;
974 }
975
976 switch (type) {
977 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500978 printf ("Module data width %d bits\n", data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500979 break;
980 default:
Larry Johnson632de062008-01-11 23:26:18 -0500981 printf ("Module data width %d bits\n",
982 (data[7] << 8) | data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500983 break;
984 }
985
wdenk4b9206e2004-03-23 22:14:11 +0000986 puts ("Interface signal levels ");
wdenk81a88242002-10-26 15:22:42 +0000987 switch(data[8]) {
Larry Johnson0df6b842008-01-10 22:23:39 -0500988 case 0: puts ("TTL 5.0 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +0000989 case 1: puts ("LVTTL\n"); break;
Larry Johnson0df6b842008-01-10 22:23:39 -0500990 case 2: puts ("HSTL 1.5 V\n"); break;
991 case 3: puts ("SSTL 3.3 V\n"); break;
992 case 4: puts ("SSTL 2.5 V\n"); break;
993 case 5: puts ("SSTL 1.8 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +0000994 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +0000995 }
Larry Johnson0df6b842008-01-10 22:23:39 -0500996
997 switch (type) {
998 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500999 printf ("SDRAM cycle time ");
1000 print_ddr2_tcyc (data[9]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001001 break;
1002 default:
Larry Johnson632de062008-01-11 23:26:18 -05001003 printf ("SDRAM cycle time %d.%d ns\n",
1004 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001005 break;
1006 }
1007
1008 switch (type) {
1009 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001010 printf ("SDRAM access time 0.%d%d ns\n",
1011 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001012 break;
1013 default:
Larry Johnson632de062008-01-11 23:26:18 -05001014 printf ("SDRAM access time %d.%d ns\n",
1015 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001016 break;
1017 }
1018
wdenk4b9206e2004-03-23 22:14:11 +00001019 puts ("EDC configuration ");
Larry Johnson632de062008-01-11 23:26:18 -05001020 switch (data[11]) {
wdenk4b9206e2004-03-23 22:14:11 +00001021 case 0: puts ("None\n"); break;
1022 case 1: puts ("Parity\n"); break;
1023 case 2: puts ("ECC\n"); break;
1024 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001025 }
Larry Johnson632de062008-01-11 23:26:18 -05001026
Timur Tabie857a5b2006-11-28 12:09:35 -06001027 if ((data[12] & 0x80) == 0)
wdenk4b9206e2004-03-23 22:14:11 +00001028 puts ("No self refresh, rate ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001029 else
wdenk4b9206e2004-03-23 22:14:11 +00001030 puts ("Self refresh, rate ");
Larry Johnson632de062008-01-11 23:26:18 -05001031
wdenk81a88242002-10-26 15:22:42 +00001032 switch(data[12] & 0x7F) {
Larry Johnson632de062008-01-11 23:26:18 -05001033 case 0: puts ("15.625 us\n"); break;
1034 case 1: puts ("3.9 us\n"); break;
1035 case 2: puts ("7.8 us\n"); break;
1036 case 3: puts ("31.3 us\n"); break;
1037 case 4: puts ("62.5 us\n"); break;
1038 case 5: puts ("125 us\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001039 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001040 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001041
1042 switch (type) {
1043 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001044 printf ("SDRAM width (primary) %d\n", data[13]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001045 break;
1046 default:
Larry Johnson632de062008-01-11 23:26:18 -05001047 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001048 if ((data[13] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001049 printf (" (second bank) %d\n",
1050 2 * (data[13] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001051 }
1052 break;
wdenk81a88242002-10-26 15:22:42 +00001053 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001054
1055 switch (type) {
1056 case DDR2:
1057 if (data[14] != 0)
Larry Johnson632de062008-01-11 23:26:18 -05001058 printf ("EDC width %d\n", data[14]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001059 break;
1060 default:
1061 if (data[14] != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001062 printf ("EDC width %d\n",
1063 data[14] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001064
1065 if ((data[14] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001066 printf (" (second bank) %d\n",
1067 2 * (data[14] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001068 }
1069 }
1070 break;
1071 }
1072
Larry Johnson632de062008-01-11 23:26:18 -05001073 if (DDR2 != type) {
1074 printf ("Min clock delay, back-to-back random column addresses "
1075 "%d\n", data[15]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001076 }
1077
wdenk4b9206e2004-03-23 22:14:11 +00001078 puts ("Burst length(s) ");
1079 if (data[16] & 0x80) puts (" Page");
1080 if (data[16] & 0x08) puts (" 8");
1081 if (data[16] & 0x04) puts (" 4");
1082 if (data[16] & 0x02) puts (" 2");
1083 if (data[16] & 0x01) puts (" 1");
1084 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001085 printf ("Number of banks %d\n", data[17]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001086
1087 switch (type) {
1088 case DDR2:
1089 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001090 decode_bits (data[18], decode_CAS_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001091 putc ('\n');
1092 break;
1093 default:
1094 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001095 decode_bits (data[18], decode_CAS_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001096 putc ('\n');
1097 break;
1098 }
1099
1100 if (DDR2 != type) {
1101 puts ("CS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001102 decode_bits (data[19], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001103 putc ('\n');
1104 }
1105
1106 if (DDR2 != type) {
1107 puts ("WE latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001108 decode_bits (data[20], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001109 putc ('\n');
1110 }
1111
1112 switch (type) {
1113 case DDR2:
1114 puts ("Module attributes:\n");
1115 if (data[21] & 0x80)
1116 puts (" TBD (bit 7)\n");
1117 if (data[21] & 0x40)
1118 puts (" Analysis probe installed\n");
1119 if (data[21] & 0x20)
1120 puts (" TBD (bit 5)\n");
1121 if (data[21] & 0x10)
1122 puts (" FET switch external enable\n");
Larry Johnson632de062008-01-11 23:26:18 -05001123 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
Larry Johnson0df6b842008-01-10 22:23:39 -05001124 if (data[20] & 0x11) {
Larry Johnson632de062008-01-11 23:26:18 -05001125 printf (" %d active registers on DIMM\n",
1126 (data[21] & 0x03) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001127 }
1128 break;
1129 default:
1130 puts ("Module attributes:\n");
1131 if (!data[21])
1132 puts (" (none)\n");
Larry Johnson632de062008-01-11 23:26:18 -05001133 else
1134 decode_bits (data[21], decode_byte21_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001135 break;
1136 }
1137
1138 switch (type) {
1139 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001140 decode_bits (data[22], decode_byte22_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001141 break;
1142 default:
1143 puts ("Device attributes:\n");
1144 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1145 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1146 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1147 else puts (" Upper Vcc tolerance 10%\n");
1148 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1149 else puts (" Lower Vcc tolerance 10%\n");
1150 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1151 if (data[22] & 0x04) puts (" Supports precharge all\n");
1152 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1153 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1154 break;
1155 }
1156
1157 switch (type) {
1158 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001159 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1160 print_ddr2_tcyc (data[23]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001161 break;
1162 default:
Larry Johnson632de062008-01-11 23:26:18 -05001163 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1164 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001165 break;
1166 }
1167
1168 switch (type) {
1169 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001170 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1171 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001172 break;
1173 default:
Larry Johnson632de062008-01-11 23:26:18 -05001174 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1175 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001176 break;
1177 }
1178
1179 switch (type) {
1180 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001181 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1182 print_ddr2_tcyc (data[25]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001183 break;
1184 default:
Larry Johnson632de062008-01-11 23:26:18 -05001185 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1186 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001187 break;
1188 }
1189
1190 switch (type) {
1191 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001192 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1193 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001194 break;
1195 default:
Larry Johnson632de062008-01-11 23:26:18 -05001196 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1197 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001198 break;
1199 }
1200
1201 switch (type) {
1202 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001203 printf ("Minimum row precharge %d.%02d ns\n",
1204 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001205 break;
1206 default:
Larry Johnson632de062008-01-11 23:26:18 -05001207 printf ("Minimum row precharge %d ns\n", data[27]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001208 break;
1209 }
1210
1211 switch (type) {
1212 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001213 printf ("Row active to row active min %d.%02d ns\n",
1214 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001215 break;
1216 default:
Larry Johnson632de062008-01-11 23:26:18 -05001217 printf ("Row active to row active min %d ns\n", data[28]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001218 break;
1219 }
1220
1221 switch (type) {
1222 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001223 printf ("RAS to CAS delay min %d.%02d ns\n",
1224 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001225 break;
1226 default:
Larry Johnson632de062008-01-11 23:26:18 -05001227 printf ("RAS to CAS delay min %d ns\n", data[29]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001228 break;
1229 }
1230
Larry Johnson632de062008-01-11 23:26:18 -05001231 printf ("Minimum RAS pulse width %d ns\n", data[30]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001232
1233 switch (type) {
1234 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001235 puts ("Density of each row ");
1236 decode_bits (data[31], decode_row_density_DDR2, 1);
1237 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001238 break;
1239 default:
Larry Johnson632de062008-01-11 23:26:18 -05001240 puts ("Density of each row ");
1241 decode_bits (data[31], decode_row_density_default, 1);
1242 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001243 break;
1244 }
1245
1246 switch (type) {
1247 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001248 puts ("Command and Address setup ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001249 if (data[32] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001250 printf ("1.%d%d ns\n",
1251 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001252 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001253 printf ("0.%d%d ns\n",
1254 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001255 }
1256 break;
1257 default:
Larry Johnson632de062008-01-11 23:26:18 -05001258 printf ("Command and Address setup %c%d.%d ns\n",
1259 (data[32] & 0x80) ? '-' : '+',
1260 (data[32] >> 4) & 0x07, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001261 break;
1262 }
1263
1264 switch (type) {
1265 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001266 puts ("Command and Address hold ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001267 if (data[33] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001268 printf ("1.%d%d ns\n",
1269 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001270 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001271 printf ("0.%d%d ns\n",
1272 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001273 }
1274 break;
1275 default:
Larry Johnson632de062008-01-11 23:26:18 -05001276 printf ("Command and Address hold %c%d.%d ns\n",
1277 (data[33] & 0x80) ? '-' : '+',
1278 (data[33] >> 4) & 0x07, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001279 break;
1280 }
1281
1282 switch (type) {
1283 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001284 printf ("Data signal input setup 0.%d%d ns\n",
1285 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001286 break;
1287 default:
Larry Johnson632de062008-01-11 23:26:18 -05001288 printf ("Data signal input setup %c%d.%d ns\n",
1289 (data[34] & 0x80) ? '-' : '+',
1290 (data[34] >> 4) & 0x07, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001291 break;
1292 }
1293
1294 switch (type) {
1295 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001296 printf ("Data signal input hold 0.%d%d ns\n",
1297 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001298 break;
1299 default:
Larry Johnson632de062008-01-11 23:26:18 -05001300 printf ("Data signal input hold %c%d.%d ns\n",
1301 (data[35] & 0x80) ? '-' : '+',
1302 (data[35] >> 4) & 0x07, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001303 break;
1304 }
1305
wdenk4b9206e2004-03-23 22:14:11 +00001306 puts ("Manufacturer's JEDEC ID ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001307 for (j = 64; j <= 71; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001308 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001309 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001310 printf ("Manufacturing Location %02X\n", data[72]);
wdenk4b9206e2004-03-23 22:14:11 +00001311 puts ("Manufacturer's Part Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001312 for (j = 73; j <= 90; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001313 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001314 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001315 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1316 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
wdenk4b9206e2004-03-23 22:14:11 +00001317 puts ("Assembly Serial Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001318 for (j = 95; j <= 98; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001319 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001320 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001321
Larry Johnson0df6b842008-01-10 22:23:39 -05001322 if (DDR2 != type) {
Larry Johnson632de062008-01-11 23:26:18 -05001323 printf ("Speed rating PC%d\n",
1324 data[126] == 0x66 ? 66 : data[126]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001325 }
wdenk81a88242002-10-26 15:22:42 +00001326 return 0;
1327}
Jon Loeliger90253172007-07-10 11:02:44 -05001328#endif
wdenk81a88242002-10-26 15:22:42 +00001329
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001330/*
1331 * Syntax:
1332 * i2c edid {i2c_chip}
1333 */
1334#if defined(CONFIG_I2C_EDID)
1335int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1336{
1337 u_char chip;
1338 struct edid1_info edid;
1339
1340 if (argc < 2) {
1341 cmd_usage(cmdtp);
1342 return 1;
1343 }
1344
1345 chip = simple_strtoul(argv[1], NULL, 16);
1346 if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1347 puts("Error reading EDID content.\n");
1348 return 1;
1349 }
1350
1351 if (edid_check_info(&edid)) {
1352 puts("Content isn't valid EDID.\n");
1353 return 1;
1354 }
1355
1356 edid_print_info(&edid);
1357 return 0;
1358
1359}
1360#endif /* CONFIG_I2C_EDID */
1361
Marek Vasut06afa382012-11-12 14:34:27 +00001362/**
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001363 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
Marek Vasut06afa382012-11-12 14:34:27 +00001364 * @cmdtp: Command data struct pointer
1365 * @flag: Command flag
1366 * @argc: Command-line argument count
1367 * @argv: Array of command-line arguments
1368 *
1369 * Returns zero always.
1370 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001371#if defined(CONFIG_SYS_I2C)
1372int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher67b23a32008-10-15 09:39:47 +02001373{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001374 int i;
1375#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1376 int j;
1377#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001378
1379 if (argc == 1) {
1380 /* show all busses */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001381 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1382 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1383#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1384 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1385 if (i2c_bus[i].next_hop[j].chip == 0)
1386 break;
1387 printf("->%s@0x%2x:%d",
1388 i2c_bus[i].next_hop[j].mux.name,
1389 i2c_bus[i].next_hop[j].chip,
1390 i2c_bus[i].next_hop[j].channel);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001391 }
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001392#endif
1393 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001394 }
1395 } else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001396 /* show specific bus */
1397 i = simple_strtoul(argv[1], NULL, 10);
1398 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1399 printf("Invalid bus %d\n", i);
1400 return -1;
1401 }
1402 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1403#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1404 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1405 if (i2c_bus[i].next_hop[j].chip == 0)
1406 break;
1407 printf("->%s@0x%2x:%d",
1408 i2c_bus[i].next_hop[j].mux.name,
1409 i2c_bus[i].next_hop[j].chip,
1410 i2c_bus[i].next_hop[j].channel);
1411 }
1412#endif
1413 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001414 }
Heiko Schocher67b23a32008-10-15 09:39:47 +02001415
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001416 return 0;
1417}
1418#endif
1419
Marek Vasut06afa382012-11-12 14:34:27 +00001420/**
1421 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1422 * @cmdtp: Command data struct pointer
1423 * @flag: Command flag
1424 * @argc: Command-line argument count
1425 * @argv: Array of command-line arguments
1426 *
1427 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1428 * on error.
1429 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001430#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
1431int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001432{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001433 int ret = 0;
1434 unsigned int bus_no;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001435
Timur Tabie857a5b2006-11-28 12:09:35 -06001436 if (argc == 1)
1437 /* querying current setting */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001438 printf("Current bus is %d\n", i2c_get_bus_num());
Timur Tabie857a5b2006-11-28 12:09:35 -06001439 else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001440 bus_no = simple_strtoul(argv[1], NULL, 10);
Heiko Schocher880a4122013-08-23 09:39:16 +02001441#if defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001442 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1443 printf("Invalid bus %d\n", bus_no);
1444 return -1;
1445 }
Heiko Schocher880a4122013-08-23 09:39:16 +02001446#endif
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001447 printf("Setting bus to %d\n", bus_no);
1448 ret = i2c_set_bus_num(bus_no);
Timur Tabie857a5b2006-11-28 12:09:35 -06001449 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001450 printf("Failure changing bus number (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001451 }
1452 return ret;
1453}
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001454#endif /* defined(CONFIG_SYS_I2C) */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001455
Marek Vasut06afa382012-11-12 14:34:27 +00001456/**
1457 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1458 * @cmdtp: Command data struct pointer
1459 * @flag: Command flag
1460 * @argc: Command-line argument count
1461 * @argv: Array of command-line arguments
1462 *
1463 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1464 * on error.
1465 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001466static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001467{
1468 int speed, ret=0;
1469
Timur Tabie857a5b2006-11-28 12:09:35 -06001470 if (argc == 1)
1471 /* querying current speed */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001472 printf("Current bus speed=%d\n", i2c_get_bus_speed());
Timur Tabie857a5b2006-11-28 12:09:35 -06001473 else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001474 speed = simple_strtoul(argv[1], NULL, 10);
1475 printf("Setting bus speed to %d Hz\n", speed);
1476 ret = i2c_set_bus_speed(speed);
Timur Tabie857a5b2006-11-28 12:09:35 -06001477 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001478 printf("Failure changing bus speed (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001479 }
1480 return ret;
1481}
1482
Marek Vasut06afa382012-11-12 14:34:27 +00001483/**
1484 * do_i2c_mm() - Handle the "i2c mm" command-line command
1485 * @cmdtp: Command data struct pointer
1486 * @flag: Command flag
1487 * @argc: Command-line argument count
1488 * @argv: Array of command-line arguments
1489 *
1490 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1491 * on error.
1492 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001493static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001494{
1495 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1496}
1497
Marek Vasut06afa382012-11-12 14:34:27 +00001498/**
1499 * do_i2c_nm() - Handle the "i2c nm" command-line command
1500 * @cmdtp: Command data struct pointer
1501 * @flag: Command flag
1502 * @argc: Command-line argument count
1503 * @argv: Array of command-line arguments
1504 *
1505 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1506 * on error.
1507 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001508static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001509{
1510 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1511}
1512
Marek Vasut06afa382012-11-12 14:34:27 +00001513/**
1514 * do_i2c_reset() - Handle the "i2c reset" command-line command
1515 * @cmdtp: Command data struct pointer
1516 * @flag: Command flag
1517 * @argc: Command-line argument count
1518 * @argv: Array of command-line arguments
1519 *
1520 * Returns zero always.
1521 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001522static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001523{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001524#if defined(CONFIG_SYS_I2C)
1525 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1526#else
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001527 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001528#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001529 return 0;
1530}
1531
1532static cmd_tbl_t cmd_i2c_sub[] = {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001533#if defined(CONFIG_SYS_I2C)
1534 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001535#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001536 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001537#if defined(CONFIG_SYS_I2C) || \
1538 defined(CONFIG_I2C_MULTI_BUS)
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001539 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1540#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001541#if defined(CONFIG_I2C_EDID)
1542 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1543#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001544 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1545 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1546 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1547 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1548 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1549 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001550 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
York Sunff5d2dc2012-09-16 08:02:30 +00001551 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001552 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1553#if defined(CONFIG_CMD_SDRAM)
1554 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1555#endif
1556 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1557};
1558
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001559#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001560void i2c_reloc(void) {
1561 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1562}
1563#endif
1564
Marek Vasut06afa382012-11-12 14:34:27 +00001565/**
1566 * do_i2c() - Handle the "i2c" command-line command
1567 * @cmdtp: Command data struct pointer
1568 * @flag: Command flag
1569 * @argc: Command-line argument count
1570 * @argv: Array of command-line arguments
1571 *
1572 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1573 * on error.
1574 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001575static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001576{
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001577 cmd_tbl_t *c;
1578
Heiko Schocher4444b222010-09-17 13:10:35 +02001579 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001580 return CMD_RET_USAGE;
Heiko Schocher4444b222010-09-17 13:10:35 +02001581
Peter Tysere96ad5d2009-04-18 22:34:04 -05001582 /* Strip off leading 'i2c' command argument */
1583 argc--;
1584 argv++;
1585
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001586 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1587
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001588 if (c)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001589 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001590 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00001591 return CMD_RET_USAGE;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001592}
wdenk8bde7f72003-06-27 21:31:46 +00001593
1594/***************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00001595#ifdef CONFIG_SYS_LONGHELP
1596static char i2c_help_text[] =
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001597#if defined(CONFIG_SYS_I2C)
1598 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001599#endif
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001600 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001601#if defined(CONFIG_SYS_I2C) || \
1602 defined(CONFIG_I2C_MULTI_BUS)
Peter Tyser9bc2e4e2008-10-01 12:25:04 -05001603 "i2c dev [dev] - show or set current I2C bus\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001604#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001605#if defined(CONFIG_I2C_EDID)
1606 "i2c edid chip - print EDID configuration information\n"
1607#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001608 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001609 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1610 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1611 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1612 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
Eric Nelson54b99e52012-09-23 10:12:56 +00001613 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001614 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
York Sunff5d2dc2012-09-16 08:02:30 +00001615 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
Heiko Schochere43a27c2008-10-15 09:33:30 +02001616 "i2c reset - re-init the I2C Controller\n"
Jon Loeligerc76fe472007-07-08 18:02:23 -05001617#if defined(CONFIG_CMD_SDRAM)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001618 "i2c sdram chip - print SDRAM configuration information\n"
Jon Loeliger90253172007-07-10 11:02:44 -05001619#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001620 "i2c speed [speed] - show or set I2C bus speed";
1621#endif
1622
1623U_BOOT_CMD(
1624 i2c, 6, 1, do_i2c,
1625 "I2C sub-system",
1626 i2c_help_text
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001627);