blob: 5c60d313d9ac4a04313d2a6ee170598c18d3cae0 [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 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29/*
30 * I2C Functions similar to the standard memory functions.
31 *
32 * There are several parameters in many of the commands that bear further
33 * explanations:
34 *
wdenk81a88242002-10-26 15:22:42 +000035 * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
36 * Each I2C chip on the bus has a unique address. On the I2C data bus,
37 * the address is the upper seven bits and the LSB is the "read/write"
38 * bit. Note that the {i2c_chip} address specified on the command
39 * line is not shifted up: e.g. a typical EEPROM memory chip may have
40 * an I2C address of 0x50, but the data put on the bus will be 0xA0
41 * for write and 0xA1 for read. This "non shifted" address notation
42 * matches at least half of the data sheets :-/.
43 *
44 * {addr} is the address (or offset) within the chip. Small memory
45 * chips have 8 bit addresses. Large memory chips have 16 bit
46 * addresses. Other memory chips have 9, 10, or 11 bit addresses.
47 * Many non-memory chips have multiple registers and {addr} is used
48 * as the register index. Some non-memory chips have only one register
49 * and therefore don't need any {addr} parameter.
50 *
51 * The default {addr} parameter is one byte (.1) which works well for
52 * memories and registers with 8 bits of address space.
53 *
54 * You can specify the length of the {addr} field with the optional .0,
55 * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
56 * manipulating a single register device which doesn't use an address
57 * field, use "0.0" for the address and the ".0" length field will
58 * suppress the address in the I2C data stream. This also works for
59 * successive reads using the I2C auto-incrementing memory pointer.
60 *
61 * If you are manipulating a large memory with 2-byte addresses, use
62 * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
63 *
64 * Then there are the unfortunate memory chips that spill the most
65 * significant 1, 2, or 3 bits of address into the chip address byte.
66 * This effectively makes one chip (logically) look like 2, 4, or
67 * 8 chips. This is handled (awkwardly) by #defining
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
wdenk81a88242002-10-26 15:22:42 +000069 * {addr} field (since .1 is the default, it doesn't actually have to
70 * be specified). Examples: given a memory chip at I2C chip address
71 * 0x50, the following would happen...
Peter Tyser0f89c542009-04-18 22:34:03 -050072 * i2c md 50 0 10 display 16 bytes starting at 0x000
wdenk81a88242002-10-26 15:22:42 +000073 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050074 * i2c md 50 100 10 display 16 bytes starting at 0x100
wdenk81a88242002-10-26 15:22:42 +000075 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050076 * i2c md 50 210 10 display 16 bytes starting at 0x210
wdenk81a88242002-10-26 15:22:42 +000077 * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
78 * This is awfully ugly. It would be nice if someone would think up
79 * a better way of handling this.
80 *
81 * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
82 */
83
84#include <common.h>
85#include <command.h>
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +000086#include <edid.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020087#include <environment.h>
wdenk81a88242002-10-26 15:22:42 +000088#include <i2c.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020089#include <malloc.h>
wdenk81a88242002-10-26 15:22:42 +000090#include <asm/byteorder.h>
Marek Vasut2515d842012-11-12 14:34:25 +000091#include <linux/compiler.h>
wdenk81a88242002-10-26 15:22:42 +000092
Heiko Schocher3f4978c2012-01-16 21:12:24 +000093DECLARE_GLOBAL_DATA_PTR;
94
wdenk81a88242002-10-26 15:22:42 +000095/* Display values from last command.
96 * Memory modify remembered values are different from display memory.
97 */
98static uchar i2c_dp_last_chip;
99static uint i2c_dp_last_addr;
100static uint i2c_dp_last_alen;
101static uint i2c_dp_last_length = 0x10;
102
103static uchar i2c_mm_last_chip;
104static uint i2c_mm_last_addr;
105static uint i2c_mm_last_alen;
106
Ben Warrenbb99ad62006-09-07 16:50:54 -0400107/* If only one I2C bus is present, the list of devices to ignore when
108 * the probe command is issued is represented by a 1D array of addresses.
109 * When multiple buses are present, the list is an array of bus-address
110 * pairs. The following macros take care of this */
111
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200112#if defined(CONFIG_SYS_I2C_NOPROBES)
Heiko Schocher9a2accb2012-10-25 10:32:14 +0200113#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
Ben Warrenbb99ad62006-09-07 16:50:54 -0400114static struct
115{
116 uchar bus;
117 uchar addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200118} i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400119#define GET_BUS_NUM i2c_get_bus_num()
120#define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
121#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
122#define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
123#else /* single bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200124static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400125#define GET_BUS_NUM 0
126#define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
127#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
128#define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000129#endif /* defined(CONFIG_SYS_I2C) */
Ben Warrenbb99ad62006-09-07 16:50:54 -0400130
131#define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
wdenk81a88242002-10-26 15:22:42 +0000132#endif
133
Frans Meulenbroeksa266fe92010-03-26 09:46:40 +0100134#define DISP_LINE_LEN 16
135
Marek Vasut06afa382012-11-12 14:34:27 +0000136/**
137 * i2c_init_board() - Board-specific I2C bus init
138 *
139 * This function is the default no-op implementation of I2C bus
140 * initialization. This function can be overriden by board-specific
141 * implementation if needed.
142 */
Marek Vasut2515d842012-11-12 14:34:25 +0000143__weak
144void i2c_init_board(void)
Stefan Biglerc649dda2011-04-08 16:24:08 +0200145{
Stefan Biglerc649dda2011-04-08 16:24:08 +0200146}
Stefan Biglerc649dda2011-04-08 16:24:08 +0200147
Peter Tyser655b34a2009-04-18 22:34:01 -0500148/* TODO: Implement architecture-specific get/set functions */
Marek Vasut06afa382012-11-12 14:34:27 +0000149
150/**
151 * i2c_get_bus_speed() - Return I2C bus speed
152 *
153 * This function is the default implementation of function for retrieveing
154 * the current I2C bus speed in Hz.
155 *
156 * A driver implementing runtime switching of I2C bus speed must override
157 * this function to report the speed correctly. Simple or legacy drivers
158 * can use this fallback.
159 *
160 * Returns I2C bus speed in Hz.
161 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000162#if !defined(CONFIG_SYS_I2C)
163/*
164 * TODO: Implement architecture-specific get/set functions
165 * Should go away, if we switched completely to new multibus support
166 */
Marek Vasut2515d842012-11-12 14:34:25 +0000167__weak
168unsigned int i2c_get_bus_speed(void)
Peter Tyser655b34a2009-04-18 22:34:01 -0500169{
170 return CONFIG_SYS_I2C_SPEED;
171}
Peter Tyser655b34a2009-04-18 22:34:01 -0500172
Marek Vasut06afa382012-11-12 14:34:27 +0000173/**
174 * i2c_set_bus_speed() - Configure I2C bus speed
175 * @speed: Newly set speed of the I2C bus in Hz
176 *
177 * This function is the default implementation of function for setting
178 * the I2C bus speed in Hz.
179 *
180 * A driver implementing runtime switching of I2C bus speed must override
181 * this function to report the speed correctly. Simple or legacy drivers
182 * can use this fallback.
183 *
184 * Returns zero on success, negative value on error.
185 */
Marek Vasut2515d842012-11-12 14:34:25 +0000186__weak
187int i2c_set_bus_speed(unsigned int speed)
Peter Tyser655b34a2009-04-18 22:34:01 -0500188{
189 if (speed != CONFIG_SYS_I2C_SPEED)
190 return -1;
191
192 return 0;
193}
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000194#endif
Peter Tyser655b34a2009-04-18 22:34:01 -0500195
Marek Vasut06afa382012-11-12 14:34:27 +0000196/**
197 * get_alen() - Small parser helper function to get address length
198 *
199 * Returns the address length.
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100200 */
201static uint get_alen(char *arg)
202{
203 int j;
204 int alen;
205
206 alen = 1;
207 for (j = 0; j < 8; j++) {
208 if (arg[j] == '.') {
209 alen = arg[j+1] - '0';
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100210 break;
211 } else if (arg[j] == '\0')
212 break;
213 }
214 return alen;
215}
216
Marek Vasut06afa382012-11-12 14:34:27 +0000217/**
218 * do_i2c_read() - Handle the "i2c read" command-line command
219 * @cmdtp: Command data struct pointer
220 * @flag: Command flag
221 * @argc: Command-line argument count
222 * @argv: Array of command-line arguments
223 *
224 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
225 * on error.
226 *
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100227 * Syntax:
228 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
229 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200230static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100231{
232 u_char chip;
233 uint devaddr, alen, length;
234 u_char *memaddr;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100235
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200236 if (argc != 5)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000237 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100238
239 /*
240 * I2C chip address
241 */
242 chip = simple_strtoul(argv[1], NULL, 16);
243
244 /*
245 * I2C data address within the chip. This can be 1 or
246 * 2 bytes long. Some day it might be 3 bytes long :-).
247 */
248 devaddr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100249 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200250 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000251 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100252
253 /*
254 * Length is the number of objects, not number of bytes.
255 */
256 length = simple_strtoul(argv[3], NULL, 16);
257
258 /*
259 * memaddr is the address where to store things in memory
260 */
261 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
262
263 if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
264 puts ("Error reading the chip.\n");
265 return 1;
266 }
267 return 0;
268}
269
York Sunff5d2dc2012-09-16 08:02:30 +0000270static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
271{
272 u_char chip;
273 uint devaddr, alen, length;
274 u_char *memaddr;
275
276 if (argc != 5)
277 return cmd_usage(cmdtp);
278
279 /*
280 * memaddr is the address where to store things in memory
281 */
282 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
283
284 /*
285 * I2C chip address
286 */
287 chip = simple_strtoul(argv[2], NULL, 16);
288
289 /*
290 * I2C data address within the chip. This can be 1 or
291 * 2 bytes long. Some day it might be 3 bytes long :-).
292 */
293 devaddr = simple_strtoul(argv[3], NULL, 16);
294 alen = get_alen(argv[3]);
295 if (alen > 3)
296 return cmd_usage(cmdtp);
297
298 /*
299 * Length is the number of objects, not number of bytes.
300 */
301 length = simple_strtoul(argv[4], NULL, 16);
302
303 while (length-- > 0) {
304 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
305 puts("Error writing to the chip.\n");
306 return 1;
307 }
308/*
309 * No write delay with FRAM devices.
310 */
311#if !defined(CONFIG_SYS_I2C_FRAM)
312 udelay(11000);
313#endif
314 }
315 return 0;
316}
317
Marek Vasut06afa382012-11-12 14:34:27 +0000318/**
319 * do_i2c_md() - Handle the "i2c md" command-line command
320 * @cmdtp: Command data struct pointer
321 * @flag: Command flag
322 * @argc: Command-line argument count
323 * @argv: Array of command-line arguments
324 *
325 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
326 * on error.
327 *
Frans Meulenbroeks4a8cf332010-03-26 09:46:39 +0100328 * Syntax:
329 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
330 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200331static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000332{
333 u_char chip;
334 uint addr, alen, length;
335 int j, nbytes, linebytes;
336
337 /* We use the last specified parameters, unless new ones are
338 * entered.
339 */
340 chip = i2c_dp_last_chip;
341 addr = i2c_dp_last_addr;
342 alen = i2c_dp_last_alen;
343 length = i2c_dp_last_length;
344
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200345 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000346 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000347
348 if ((flag & CMD_FLAG_REPEAT) == 0) {
349 /*
350 * New command specified.
351 */
wdenk81a88242002-10-26 15:22:42 +0000352
353 /*
354 * I2C chip address
355 */
356 chip = simple_strtoul(argv[1], NULL, 16);
357
358 /*
359 * I2C data address within the chip. This can be 1 or
360 * 2 bytes long. Some day it might be 3 bytes long :-).
361 */
362 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100363 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200364 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000365 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000366
367 /*
368 * If another parameter, it is the length to display.
369 * Length is the number of objects, not number of bytes.
370 */
371 if (argc > 3)
372 length = simple_strtoul(argv[3], NULL, 16);
373 }
374
375 /*
376 * Print the lines.
377 *
378 * We buffer all read data, so we can make sure data is read only
379 * once.
380 */
381 nbytes = length;
382 do {
383 unsigned char linebuf[DISP_LINE_LEN];
384 unsigned char *cp;
385
386 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
387
Timur Tabie857a5b2006-11-28 12:09:35 -0600388 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000389 puts ("Error reading the chip.\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600390 else {
wdenk81a88242002-10-26 15:22:42 +0000391 printf("%04x:", addr);
392 cp = linebuf;
393 for (j=0; j<linebytes; j++) {
394 printf(" %02x", *cp++);
395 addr++;
396 }
wdenk4b9206e2004-03-23 22:14:11 +0000397 puts (" ");
wdenk81a88242002-10-26 15:22:42 +0000398 cp = linebuf;
399 for (j=0; j<linebytes; j++) {
400 if ((*cp < 0x20) || (*cp > 0x7e))
wdenk4b9206e2004-03-23 22:14:11 +0000401 puts (".");
wdenk81a88242002-10-26 15:22:42 +0000402 else
403 printf("%c", *cp);
404 cp++;
405 }
wdenk4b9206e2004-03-23 22:14:11 +0000406 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000407 }
408 nbytes -= linebytes;
409 } while (nbytes > 0);
410
411 i2c_dp_last_chip = chip;
412 i2c_dp_last_addr = addr;
413 i2c_dp_last_alen = alen;
414 i2c_dp_last_length = length;
415
416 return 0;
417}
418
Marek Vasut06afa382012-11-12 14:34:27 +0000419/**
420 * do_i2c_mw() - Handle the "i2c mw" command-line command
421 * @cmdtp: Command data struct pointer
422 * @flag: Command flag
423 * @argc: Command-line argument count
424 * @argv: Array of command-line arguments
425 *
426 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
427 * on error.
wdenk81a88242002-10-26 15:22:42 +0000428 *
429 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500430 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
wdenk81a88242002-10-26 15:22:42 +0000431 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200432static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000433{
434 uchar chip;
435 ulong addr;
436 uint alen;
437 uchar byte;
438 int count;
wdenk81a88242002-10-26 15:22:42 +0000439
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200440 if ((argc < 4) || (argc > 5))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000441 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000442
443 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200444 * Chip is always specified.
445 */
wdenk81a88242002-10-26 15:22:42 +0000446 chip = simple_strtoul(argv[1], NULL, 16);
447
448 /*
449 * Address is always specified.
450 */
451 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100452 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200453 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000454 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000455
456 /*
457 * Value to write is always specified.
458 */
459 byte = simple_strtoul(argv[3], NULL, 16);
460
461 /*
462 * Optional count
463 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600464 if (argc == 5)
wdenk81a88242002-10-26 15:22:42 +0000465 count = simple_strtoul(argv[4], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600466 else
wdenk81a88242002-10-26 15:22:42 +0000467 count = 1;
wdenk81a88242002-10-26 15:22:42 +0000468
469 while (count-- > 0) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600470 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000471 puts ("Error writing the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000472 /*
473 * Wait for the write to complete. The write can take
474 * up to 10mSec (we allow a little more time).
wdenk81a88242002-10-26 15:22:42 +0000475 */
d4f5c722005-08-12 21:16:13 +0200476/*
477 * No write delay with FRAM devices.
478 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200479#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk81a88242002-10-26 15:22:42 +0000480 udelay(11000);
d4f5c722005-08-12 21:16:13 +0200481#endif
wdenk81a88242002-10-26 15:22:42 +0000482 }
483
Marek Vasut06afa382012-11-12 14:34:27 +0000484 return 0;
wdenk81a88242002-10-26 15:22:42 +0000485}
486
Marek Vasut06afa382012-11-12 14:34:27 +0000487/**
488 * do_i2c_crc() - Handle the "i2c crc32" command-line command
489 * @cmdtp: Command data struct pointer
490 * @flag: Command flag
491 * @argc: Command-line argument count
492 * @argv: Array of command-line arguments
493 *
494 * Calculate a CRC on memory
495 *
496 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
497 * on error.
wdenk81a88242002-10-26 15:22:42 +0000498 *
499 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500500 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
wdenk81a88242002-10-26 15:22:42 +0000501 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200502static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000503{
504 uchar chip;
505 ulong addr;
506 uint alen;
507 int count;
508 uchar byte;
509 ulong crc;
510 ulong err;
wdenk81a88242002-10-26 15:22:42 +0000511
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200512 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000513 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000514
515 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200516 * Chip is always specified.
517 */
wdenk81a88242002-10-26 15:22:42 +0000518 chip = simple_strtoul(argv[1], NULL, 16);
519
520 /*
521 * Address is always specified.
522 */
523 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100524 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200525 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000526 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000527
528 /*
529 * Count is always specified
530 */
531 count = simple_strtoul(argv[3], NULL, 16);
532
533 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
534 /*
535 * CRC a byte at a time. This is going to be slooow, but hey, the
536 * memories are small and slow too so hopefully nobody notices.
537 */
538 crc = 0;
539 err = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600540 while (count-- > 0) {
541 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
wdenk81a88242002-10-26 15:22:42 +0000542 err++;
wdenk81a88242002-10-26 15:22:42 +0000543 crc = crc32 (crc, &byte, 1);
544 addr++;
545 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600546 if (err > 0)
wdenk4b9206e2004-03-23 22:14:11 +0000547 puts ("Error reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600548 else
wdenk81a88242002-10-26 15:22:42 +0000549 printf ("%08lx\n", crc);
wdenk81a88242002-10-26 15:22:42 +0000550
551 return 0;
552}
553
Marek Vasut06afa382012-11-12 14:34:27 +0000554/**
555 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
556 * @cmdtp: Command data struct pointer
557 * @flag: Command flag
558 * @argc: Command-line argument count
559 * @argv: Array of command-line arguments
560 *
561 * Modify memory.
562 *
563 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
564 * on error.
wdenk81a88242002-10-26 15:22:42 +0000565 *
566 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500567 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
568 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
wdenk81a88242002-10-26 15:22:42 +0000569 */
wdenk81a88242002-10-26 15:22:42 +0000570static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200571mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000572{
573 uchar chip;
574 ulong addr;
575 uint alen;
576 ulong data;
577 int size = 1;
578 int nbytes;
wdenk81a88242002-10-26 15:22:42 +0000579
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200580 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000581 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000582
583#ifdef CONFIG_BOOT_RETRY_TIME
584 reset_cmd_timeout(); /* got a good command to get here */
585#endif
586 /*
587 * We use the last specified parameters, unless new ones are
588 * entered.
589 */
590 chip = i2c_mm_last_chip;
591 addr = i2c_mm_last_addr;
592 alen = i2c_mm_last_alen;
593
594 if ((flag & CMD_FLAG_REPEAT) == 0) {
595 /*
596 * New command specified. Check for a size specification.
597 * Defaults to byte if no or incorrect specification.
598 */
599 size = cmd_get_data_size(argv[0], 1);
600
601 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200602 * Chip is always specified.
603 */
wdenk81a88242002-10-26 15:22:42 +0000604 chip = simple_strtoul(argv[1], NULL, 16);
605
606 /*
607 * Address is always specified.
608 */
609 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100610 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200611 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000612 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000613 }
614
615 /*
616 * Print the address, followed by value. Then accept input for
617 * the next value. A non-converted value exits.
618 */
619 do {
620 printf("%08lx:", addr);
Timur Tabie857a5b2006-11-28 12:09:35 -0600621 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000622 puts ("\nError reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600623 else {
wdenk81a88242002-10-26 15:22:42 +0000624 data = cpu_to_be32(data);
Timur Tabie857a5b2006-11-28 12:09:35 -0600625 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000626 printf(" %02lx", (data >> 24) & 0x000000FF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600627 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000628 printf(" %04lx", (data >> 16) & 0x0000FFFF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600629 else
wdenk81a88242002-10-26 15:22:42 +0000630 printf(" %08lx", data);
wdenk81a88242002-10-26 15:22:42 +0000631 }
632
633 nbytes = readline (" ? ");
634 if (nbytes == 0) {
635 /*
636 * <CR> pressed as only input, don't modify current
637 * location and move to next.
638 */
639 if (incrflag)
640 addr += size;
641 nbytes = size;
642#ifdef CONFIG_BOOT_RETRY_TIME
643 reset_cmd_timeout(); /* good enough to not time out */
644#endif
645 }
646#ifdef CONFIG_BOOT_RETRY_TIME
Timur Tabie857a5b2006-11-28 12:09:35 -0600647 else if (nbytes == -2)
wdenk81a88242002-10-26 15:22:42 +0000648 break; /* timed out, exit the command */
wdenk81a88242002-10-26 15:22:42 +0000649#endif
650 else {
651 char *endp;
652
653 data = simple_strtoul(console_buffer, &endp, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600654 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000655 data = data << 24;
Timur Tabie857a5b2006-11-28 12:09:35 -0600656 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000657 data = data << 16;
wdenk81a88242002-10-26 15:22:42 +0000658 data = be32_to_cpu(data);
659 nbytes = endp - console_buffer;
660 if (nbytes) {
661#ifdef CONFIG_BOOT_RETRY_TIME
662 /*
663 * good enough to not time out
664 */
665 reset_cmd_timeout();
666#endif
Timur Tabie857a5b2006-11-28 12:09:35 -0600667 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000668 puts ("Error writing the chip.\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200669#ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
670 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk2535d602003-07-17 23:16:40 +0000671#endif
wdenk81a88242002-10-26 15:22:42 +0000672 if (incrflag)
673 addr += size;
674 }
675 }
676 } while (nbytes);
677
Peter Tyser08007072008-08-15 14:36:32 -0500678 i2c_mm_last_chip = chip;
679 i2c_mm_last_addr = addr;
680 i2c_mm_last_alen = alen;
wdenk81a88242002-10-26 15:22:42 +0000681
682 return 0;
683}
684
Marek Vasut06afa382012-11-12 14:34:27 +0000685/**
686 * do_i2c_probe() - Handle the "i2c probe" command-line command
687 * @cmdtp: Command data struct pointer
688 * @flag: Command flag
689 * @argc: Command-line argument count
690 * @argv: Array of command-line arguments
691 *
692 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
693 * on error.
694 *
wdenk81a88242002-10-26 15:22:42 +0000695 * Syntax:
Eric Nelson54b99e52012-09-23 10:12:56 +0000696 * i2c probe {addr}
697 *
698 * Returns zero (success) if one or more I2C devices was found
wdenk81a88242002-10-26 15:22:42 +0000699 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200700static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000701{
702 int j;
Eric Nelson54b99e52012-09-23 10:12:56 +0000703 int addr = -1;
704 int found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200705#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000706 int k, skip;
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000707 unsigned int bus = GET_BUS_NUM;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400708#endif /* NOPROBES */
wdenk81a88242002-10-26 15:22:42 +0000709
Eric Nelson54b99e52012-09-23 10:12:56 +0000710 if (argc == 2)
711 addr = simple_strtol(argv[1], 0, 16);
712
wdenk4b9206e2004-03-23 22:14:11 +0000713 puts ("Valid chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600714 for (j = 0; j < 128; j++) {
Eric Nelson54b99e52012-09-23 10:12:56 +0000715 if ((0 <= addr) && (j != addr))
716 continue;
717
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200718#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000719 skip = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600720 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
721 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
wdenk81a88242002-10-26 15:22:42 +0000722 skip = 1;
723 break;
724 }
725 }
726 if (skip)
727 continue;
728#endif
Eric Nelson54b99e52012-09-23 10:12:56 +0000729 if (i2c_probe(j) == 0) {
wdenk81a88242002-10-26 15:22:42 +0000730 printf(" %02X", j);
Eric Nelson54b99e52012-09-23 10:12:56 +0000731 found++;
732 }
wdenk81a88242002-10-26 15:22:42 +0000733 }
wdenk4b9206e2004-03-23 22:14:11 +0000734 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000735
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200736#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000737 puts ("Excluded chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600738 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
739 if (COMPARE_BUS(bus,k))
Ben Warrenbb99ad62006-09-07 16:50:54 -0400740 printf(" %02X", NO_PROBE_ADDR(k));
741 }
wdenk4b9206e2004-03-23 22:14:11 +0000742 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000743#endif
744
Eric Nelson54b99e52012-09-23 10:12:56 +0000745 return (0 == found);
wdenk81a88242002-10-26 15:22:42 +0000746}
747
Marek Vasut06afa382012-11-12 14:34:27 +0000748/**
749 * do_i2c_loop() - Handle the "i2c loop" command-line command
750 * @cmdtp: Command data struct pointer
751 * @flag: Command flag
752 * @argc: Command-line argument count
753 * @argv: Array of command-line arguments
754 *
755 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
756 * on error.
757 *
wdenk81a88242002-10-26 15:22:42 +0000758 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500759 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
wdenk81a88242002-10-26 15:22:42 +0000760 * {length} - Number of bytes to read
761 * {delay} - A DECIMAL number and defaults to 1000 uSec
762 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200763static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000764{
765 u_char chip;
766 ulong alen;
767 uint addr;
768 uint length;
769 u_char bytes[16];
770 int delay;
wdenk81a88242002-10-26 15:22:42 +0000771
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200772 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000773 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000774
775 /*
776 * Chip is always specified.
777 */
778 chip = simple_strtoul(argv[1], NULL, 16);
779
780 /*
781 * Address is always specified.
782 */
783 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100784 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200785 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000786 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000787
788 /*
789 * Length is the number of objects, not number of bytes.
790 */
791 length = 1;
792 length = simple_strtoul(argv[3], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600793 if (length > sizeof(bytes))
wdenk81a88242002-10-26 15:22:42 +0000794 length = sizeof(bytes);
wdenk81a88242002-10-26 15:22:42 +0000795
796 /*
797 * The delay time (uSec) is optional.
798 */
799 delay = 1000;
Timur Tabie857a5b2006-11-28 12:09:35 -0600800 if (argc > 3)
wdenk81a88242002-10-26 15:22:42 +0000801 delay = simple_strtoul(argv[4], NULL, 10);
wdenk81a88242002-10-26 15:22:42 +0000802 /*
803 * Run the loop...
804 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600805 while (1) {
806 if (i2c_read(chip, addr, alen, bytes, length) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000807 puts ("Error reading the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000808 udelay(delay);
809 }
810
811 /* NOTREACHED */
812 return 0;
813}
814
wdenk81a88242002-10-26 15:22:42 +0000815/*
816 * The SDRAM command is separately configured because many
817 * (most?) embedded boards don't use SDRAM DIMMs.
Marek Vasut06afa382012-11-12 14:34:27 +0000818 *
819 * FIXME: Document and probably move elsewhere!
wdenk81a88242002-10-26 15:22:42 +0000820 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500821#if defined(CONFIG_CMD_SDRAM)
Larry Johnson632de062008-01-11 23:26:18 -0500822static void print_ddr2_tcyc (u_char const b)
823{
824 printf ("%d.", (b >> 4) & 0x0F);
825 switch (b & 0x0F) {
826 case 0x0:
827 case 0x1:
828 case 0x2:
829 case 0x3:
830 case 0x4:
831 case 0x5:
832 case 0x6:
833 case 0x7:
834 case 0x8:
835 case 0x9:
836 printf ("%d ns\n", b & 0x0F);
837 break;
838 case 0xA:
839 puts ("25 ns\n");
840 break;
841 case 0xB:
842 puts ("33 ns\n");
843 break;
844 case 0xC:
845 puts ("66 ns\n");
846 break;
847 case 0xD:
848 puts ("75 ns\n");
849 break;
850 default:
851 puts ("?? ns\n");
852 break;
853 }
854}
855
856static void decode_bits (u_char const b, char const *str[], int const do_once)
857{
858 u_char mask;
859
860 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
861 if (b & mask) {
862 puts (*str);
863 if (do_once)
864 return;
865 }
866 }
867}
wdenk81a88242002-10-26 15:22:42 +0000868
869/*
870 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500871 * i2c sdram {i2c_chip}
wdenk81a88242002-10-26 15:22:42 +0000872 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200873static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000874{
Larry Johnson632de062008-01-11 23:26:18 -0500875 enum { unknown, EDO, SDRAM, DDR2 } type;
876
wdenk81a88242002-10-26 15:22:42 +0000877 u_char chip;
878 u_char data[128];
879 u_char cksum;
880 int j;
881
Larry Johnson632de062008-01-11 23:26:18 -0500882 static const char *decode_CAS_DDR2[] = {
883 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
884 };
885
886 static const char *decode_CAS_default[] = {
887 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
888 };
889
890 static const char *decode_CS_WE_default[] = {
891 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
892 };
893
894 static const char *decode_byte21_default[] = {
895 " TBD (bit 7)\n",
896 " Redundant row address\n",
897 " Differential clock input\n",
898 " Registerd DQMB inputs\n",
899 " Buffered DQMB inputs\n",
900 " On-card PLL\n",
901 " Registered address/control lines\n",
902 " Buffered address/control lines\n"
903 };
904
905 static const char *decode_byte22_DDR2[] = {
906 " TBD (bit 7)\n",
907 " TBD (bit 6)\n",
908 " TBD (bit 5)\n",
909 " TBD (bit 4)\n",
910 " TBD (bit 3)\n",
911 " Supports partial array self refresh\n",
912 " Supports 50 ohm ODT\n",
913 " Supports weak driver\n"
914 };
915
916 static const char *decode_row_density_DDR2[] = {
917 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
918 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
919 };
920
921 static const char *decode_row_density_default[] = {
922 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
923 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
924 };
925
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200926 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000927 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200928
wdenk81a88242002-10-26 15:22:42 +0000929 /*
930 * Chip is always specified.
Larry Johnson632de062008-01-11 23:26:18 -0500931 */
932 chip = simple_strtoul (argv[1], NULL, 16);
wdenk81a88242002-10-26 15:22:42 +0000933
Larry Johnson632de062008-01-11 23:26:18 -0500934 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000935 puts ("No SDRAM Serial Presence Detect found.\n");
wdenk81a88242002-10-26 15:22:42 +0000936 return 1;
937 }
938
939 cksum = 0;
940 for (j = 0; j < 63; j++) {
941 cksum += data[j];
942 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600943 if (cksum != data[63]) {
wdenk81a88242002-10-26 15:22:42 +0000944 printf ("WARNING: Configuration data checksum failure:\n"
Larry Johnson632de062008-01-11 23:26:18 -0500945 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
wdenk81a88242002-10-26 15:22:42 +0000946 }
Larry Johnson632de062008-01-11 23:26:18 -0500947 printf ("SPD data revision %d.%d\n",
wdenk81a88242002-10-26 15:22:42 +0000948 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
Larry Johnson632de062008-01-11 23:26:18 -0500949 printf ("Bytes used 0x%02X\n", data[0]);
950 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
951
wdenk4b9206e2004-03-23 22:14:11 +0000952 puts ("Memory type ");
Larry Johnson632de062008-01-11 23:26:18 -0500953 switch (data[2]) {
Larry Johnson0df6b842008-01-10 22:23:39 -0500954 case 2:
955 type = EDO;
956 puts ("EDO\n");
957 break;
958 case 4:
959 type = SDRAM;
960 puts ("SDRAM\n");
961 break;
962 case 8:
963 type = DDR2;
964 puts ("DDR2\n");
965 break;
966 default:
967 type = unknown;
968 puts ("unknown\n");
969 break;
wdenk81a88242002-10-26 15:22:42 +0000970 }
Larry Johnson632de062008-01-11 23:26:18 -0500971
wdenk4b9206e2004-03-23 22:14:11 +0000972 puts ("Row address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600973 if ((data[3] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500974 printf ("%d\n", data[3] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600975 else
Larry Johnson632de062008-01-11 23:26:18 -0500976 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
977
wdenk4b9206e2004-03-23 22:14:11 +0000978 puts ("Column address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600979 if ((data[4] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500980 printf ("%d\n", data[4] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600981 else
Larry Johnson632de062008-01-11 23:26:18 -0500982 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -0500983
984 switch (type) {
985 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500986 printf ("Number of ranks %d\n",
987 (data[5] & 0x07) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -0500988 break;
989 default:
Larry Johnson632de062008-01-11 23:26:18 -0500990 printf ("Module rows %d\n", data[5]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500991 break;
992 }
993
994 switch (type) {
995 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500996 printf ("Module data width %d bits\n", data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500997 break;
998 default:
Larry Johnson632de062008-01-11 23:26:18 -0500999 printf ("Module data width %d bits\n",
1000 (data[7] << 8) | data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001001 break;
1002 }
1003
wdenk4b9206e2004-03-23 22:14:11 +00001004 puts ("Interface signal levels ");
wdenk81a88242002-10-26 15:22:42 +00001005 switch(data[8]) {
Larry Johnson0df6b842008-01-10 22:23:39 -05001006 case 0: puts ("TTL 5.0 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001007 case 1: puts ("LVTTL\n"); break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001008 case 2: puts ("HSTL 1.5 V\n"); break;
1009 case 3: puts ("SSTL 3.3 V\n"); break;
1010 case 4: puts ("SSTL 2.5 V\n"); break;
1011 case 5: puts ("SSTL 1.8 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001012 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001013 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001014
1015 switch (type) {
1016 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001017 printf ("SDRAM cycle time ");
1018 print_ddr2_tcyc (data[9]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001019 break;
1020 default:
Larry Johnson632de062008-01-11 23:26:18 -05001021 printf ("SDRAM cycle time %d.%d ns\n",
1022 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001023 break;
1024 }
1025
1026 switch (type) {
1027 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001028 printf ("SDRAM access time 0.%d%d ns\n",
1029 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001030 break;
1031 default:
Larry Johnson632de062008-01-11 23:26:18 -05001032 printf ("SDRAM access time %d.%d ns\n",
1033 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001034 break;
1035 }
1036
wdenk4b9206e2004-03-23 22:14:11 +00001037 puts ("EDC configuration ");
Larry Johnson632de062008-01-11 23:26:18 -05001038 switch (data[11]) {
wdenk4b9206e2004-03-23 22:14:11 +00001039 case 0: puts ("None\n"); break;
1040 case 1: puts ("Parity\n"); break;
1041 case 2: puts ("ECC\n"); break;
1042 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001043 }
Larry Johnson632de062008-01-11 23:26:18 -05001044
Timur Tabie857a5b2006-11-28 12:09:35 -06001045 if ((data[12] & 0x80) == 0)
wdenk4b9206e2004-03-23 22:14:11 +00001046 puts ("No self refresh, rate ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001047 else
wdenk4b9206e2004-03-23 22:14:11 +00001048 puts ("Self refresh, rate ");
Larry Johnson632de062008-01-11 23:26:18 -05001049
wdenk81a88242002-10-26 15:22:42 +00001050 switch(data[12] & 0x7F) {
Larry Johnson632de062008-01-11 23:26:18 -05001051 case 0: puts ("15.625 us\n"); break;
1052 case 1: puts ("3.9 us\n"); break;
1053 case 2: puts ("7.8 us\n"); break;
1054 case 3: puts ("31.3 us\n"); break;
1055 case 4: puts ("62.5 us\n"); break;
1056 case 5: puts ("125 us\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001057 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001058 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001059
1060 switch (type) {
1061 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001062 printf ("SDRAM width (primary) %d\n", data[13]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001063 break;
1064 default:
Larry Johnson632de062008-01-11 23:26:18 -05001065 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001066 if ((data[13] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001067 printf (" (second bank) %d\n",
1068 2 * (data[13] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001069 }
1070 break;
wdenk81a88242002-10-26 15:22:42 +00001071 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001072
1073 switch (type) {
1074 case DDR2:
1075 if (data[14] != 0)
Larry Johnson632de062008-01-11 23:26:18 -05001076 printf ("EDC width %d\n", data[14]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001077 break;
1078 default:
1079 if (data[14] != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001080 printf ("EDC width %d\n",
1081 data[14] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001082
1083 if ((data[14] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001084 printf (" (second bank) %d\n",
1085 2 * (data[14] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001086 }
1087 }
1088 break;
1089 }
1090
Larry Johnson632de062008-01-11 23:26:18 -05001091 if (DDR2 != type) {
1092 printf ("Min clock delay, back-to-back random column addresses "
1093 "%d\n", data[15]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001094 }
1095
wdenk4b9206e2004-03-23 22:14:11 +00001096 puts ("Burst length(s) ");
1097 if (data[16] & 0x80) puts (" Page");
1098 if (data[16] & 0x08) puts (" 8");
1099 if (data[16] & 0x04) puts (" 4");
1100 if (data[16] & 0x02) puts (" 2");
1101 if (data[16] & 0x01) puts (" 1");
1102 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001103 printf ("Number of banks %d\n", data[17]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001104
1105 switch (type) {
1106 case DDR2:
1107 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001108 decode_bits (data[18], decode_CAS_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001109 putc ('\n');
1110 break;
1111 default:
1112 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001113 decode_bits (data[18], decode_CAS_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001114 putc ('\n');
1115 break;
1116 }
1117
1118 if (DDR2 != type) {
1119 puts ("CS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001120 decode_bits (data[19], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001121 putc ('\n');
1122 }
1123
1124 if (DDR2 != type) {
1125 puts ("WE latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001126 decode_bits (data[20], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001127 putc ('\n');
1128 }
1129
1130 switch (type) {
1131 case DDR2:
1132 puts ("Module attributes:\n");
1133 if (data[21] & 0x80)
1134 puts (" TBD (bit 7)\n");
1135 if (data[21] & 0x40)
1136 puts (" Analysis probe installed\n");
1137 if (data[21] & 0x20)
1138 puts (" TBD (bit 5)\n");
1139 if (data[21] & 0x10)
1140 puts (" FET switch external enable\n");
Larry Johnson632de062008-01-11 23:26:18 -05001141 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
Larry Johnson0df6b842008-01-10 22:23:39 -05001142 if (data[20] & 0x11) {
Larry Johnson632de062008-01-11 23:26:18 -05001143 printf (" %d active registers on DIMM\n",
1144 (data[21] & 0x03) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001145 }
1146 break;
1147 default:
1148 puts ("Module attributes:\n");
1149 if (!data[21])
1150 puts (" (none)\n");
Larry Johnson632de062008-01-11 23:26:18 -05001151 else
1152 decode_bits (data[21], decode_byte21_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001153 break;
1154 }
1155
1156 switch (type) {
1157 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001158 decode_bits (data[22], decode_byte22_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001159 break;
1160 default:
1161 puts ("Device attributes:\n");
1162 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1163 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1164 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1165 else puts (" Upper Vcc tolerance 10%\n");
1166 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1167 else puts (" Lower Vcc tolerance 10%\n");
1168 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1169 if (data[22] & 0x04) puts (" Supports precharge all\n");
1170 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1171 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1172 break;
1173 }
1174
1175 switch (type) {
1176 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001177 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1178 print_ddr2_tcyc (data[23]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001179 break;
1180 default:
Larry Johnson632de062008-01-11 23:26:18 -05001181 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1182 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001183 break;
1184 }
1185
1186 switch (type) {
1187 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001188 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1189 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001190 break;
1191 default:
Larry Johnson632de062008-01-11 23:26:18 -05001192 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1193 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001194 break;
1195 }
1196
1197 switch (type) {
1198 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001199 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1200 print_ddr2_tcyc (data[25]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001201 break;
1202 default:
Larry Johnson632de062008-01-11 23:26:18 -05001203 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1204 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001205 break;
1206 }
1207
1208 switch (type) {
1209 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001210 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1211 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001212 break;
1213 default:
Larry Johnson632de062008-01-11 23:26:18 -05001214 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1215 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001216 break;
1217 }
1218
1219 switch (type) {
1220 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001221 printf ("Minimum row precharge %d.%02d ns\n",
1222 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001223 break;
1224 default:
Larry Johnson632de062008-01-11 23:26:18 -05001225 printf ("Minimum row precharge %d ns\n", data[27]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001226 break;
1227 }
1228
1229 switch (type) {
1230 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001231 printf ("Row active to row active min %d.%02d ns\n",
1232 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001233 break;
1234 default:
Larry Johnson632de062008-01-11 23:26:18 -05001235 printf ("Row active to row active min %d ns\n", data[28]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001236 break;
1237 }
1238
1239 switch (type) {
1240 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001241 printf ("RAS to CAS delay min %d.%02d ns\n",
1242 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001243 break;
1244 default:
Larry Johnson632de062008-01-11 23:26:18 -05001245 printf ("RAS to CAS delay min %d ns\n", data[29]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001246 break;
1247 }
1248
Larry Johnson632de062008-01-11 23:26:18 -05001249 printf ("Minimum RAS pulse width %d ns\n", data[30]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001250
1251 switch (type) {
1252 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001253 puts ("Density of each row ");
1254 decode_bits (data[31], decode_row_density_DDR2, 1);
1255 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001256 break;
1257 default:
Larry Johnson632de062008-01-11 23:26:18 -05001258 puts ("Density of each row ");
1259 decode_bits (data[31], decode_row_density_default, 1);
1260 putc ('\n');
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 setup ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001267 if (data[32] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001268 printf ("1.%d%d ns\n",
1269 ((data[32] >> 4) & 0x0F) - 10, data[32] & 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[32] >> 4) & 0x0F), data[32] & 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 setup %c%d.%d ns\n",
1277 (data[32] & 0x80) ? '-' : '+',
1278 (data[32] >> 4) & 0x07, data[32] & 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 puts ("Command and Address hold ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001285 if (data[33] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001286 printf ("1.%d%d ns\n",
1287 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001288 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001289 printf ("0.%d%d ns\n",
1290 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001291 }
1292 break;
1293 default:
Larry Johnson632de062008-01-11 23:26:18 -05001294 printf ("Command and Address hold %c%d.%d ns\n",
1295 (data[33] & 0x80) ? '-' : '+',
1296 (data[33] >> 4) & 0x07, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001297 break;
1298 }
1299
1300 switch (type) {
1301 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001302 printf ("Data signal input setup 0.%d%d ns\n",
1303 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001304 break;
1305 default:
Larry Johnson632de062008-01-11 23:26:18 -05001306 printf ("Data signal input setup %c%d.%d ns\n",
1307 (data[34] & 0x80) ? '-' : '+',
1308 (data[34] >> 4) & 0x07, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001309 break;
1310 }
1311
1312 switch (type) {
1313 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001314 printf ("Data signal input hold 0.%d%d ns\n",
1315 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001316 break;
1317 default:
Larry Johnson632de062008-01-11 23:26:18 -05001318 printf ("Data signal input hold %c%d.%d ns\n",
1319 (data[35] & 0x80) ? '-' : '+',
1320 (data[35] >> 4) & 0x07, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001321 break;
1322 }
1323
wdenk4b9206e2004-03-23 22:14:11 +00001324 puts ("Manufacturer's JEDEC ID ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001325 for (j = 64; j <= 71; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001326 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001327 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001328 printf ("Manufacturing Location %02X\n", data[72]);
wdenk4b9206e2004-03-23 22:14:11 +00001329 puts ("Manufacturer's Part Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001330 for (j = 73; j <= 90; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001331 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001332 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001333 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1334 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
wdenk4b9206e2004-03-23 22:14:11 +00001335 puts ("Assembly Serial Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001336 for (j = 95; j <= 98; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001337 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001338 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001339
Larry Johnson0df6b842008-01-10 22:23:39 -05001340 if (DDR2 != type) {
Larry Johnson632de062008-01-11 23:26:18 -05001341 printf ("Speed rating PC%d\n",
1342 data[126] == 0x66 ? 66 : data[126]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001343 }
wdenk81a88242002-10-26 15:22:42 +00001344 return 0;
1345}
Jon Loeliger90253172007-07-10 11:02:44 -05001346#endif
wdenk81a88242002-10-26 15:22:42 +00001347
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001348/*
1349 * Syntax:
1350 * i2c edid {i2c_chip}
1351 */
1352#if defined(CONFIG_I2C_EDID)
1353int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1354{
1355 u_char chip;
1356 struct edid1_info edid;
1357
1358 if (argc < 2) {
1359 cmd_usage(cmdtp);
1360 return 1;
1361 }
1362
1363 chip = simple_strtoul(argv[1], NULL, 16);
1364 if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1365 puts("Error reading EDID content.\n");
1366 return 1;
1367 }
1368
1369 if (edid_check_info(&edid)) {
1370 puts("Content isn't valid EDID.\n");
1371 return 1;
1372 }
1373
1374 edid_print_info(&edid);
1375 return 0;
1376
1377}
1378#endif /* CONFIG_I2C_EDID */
1379
Marek Vasut06afa382012-11-12 14:34:27 +00001380/**
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001381 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
Marek Vasut06afa382012-11-12 14:34:27 +00001382 * @cmdtp: Command data struct pointer
1383 * @flag: Command flag
1384 * @argc: Command-line argument count
1385 * @argv: Array of command-line arguments
1386 *
1387 * Returns zero always.
1388 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001389#if defined(CONFIG_SYS_I2C)
1390int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher67b23a32008-10-15 09:39:47 +02001391{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001392 int i;
1393#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1394 int j;
1395#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001396
1397 if (argc == 1) {
1398 /* show all busses */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001399 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1400 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1401#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1402 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1403 if (i2c_bus[i].next_hop[j].chip == 0)
1404 break;
1405 printf("->%s@0x%2x:%d",
1406 i2c_bus[i].next_hop[j].mux.name,
1407 i2c_bus[i].next_hop[j].chip,
1408 i2c_bus[i].next_hop[j].channel);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001409 }
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001410#endif
1411 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001412 }
1413 } else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001414 /* show specific bus */
1415 i = simple_strtoul(argv[1], NULL, 10);
1416 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1417 printf("Invalid bus %d\n", i);
1418 return -1;
1419 }
1420 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1421#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1422 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1423 if (i2c_bus[i].next_hop[j].chip == 0)
1424 break;
1425 printf("->%s@0x%2x:%d",
1426 i2c_bus[i].next_hop[j].mux.name,
1427 i2c_bus[i].next_hop[j].chip,
1428 i2c_bus[i].next_hop[j].channel);
1429 }
1430#endif
1431 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001432 }
Heiko Schocher67b23a32008-10-15 09:39:47 +02001433
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001434 return 0;
1435}
1436#endif
1437
Marek Vasut06afa382012-11-12 14:34:27 +00001438/**
1439 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1440 * @cmdtp: Command data struct pointer
1441 * @flag: Command flag
1442 * @argc: Command-line argument count
1443 * @argv: Array of command-line arguments
1444 *
1445 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1446 * on error.
1447 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001448#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
1449int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001450{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001451 int ret = 0;
1452 unsigned int bus_no;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001453
Timur Tabie857a5b2006-11-28 12:09:35 -06001454 if (argc == 1)
1455 /* querying current setting */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001456 printf("Current bus is %d\n", i2c_get_bus_num());
Timur Tabie857a5b2006-11-28 12:09:35 -06001457 else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001458 bus_no = simple_strtoul(argv[1], NULL, 10);
1459 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1460 printf("Invalid bus %d\n", bus_no);
1461 return -1;
1462 }
1463 printf("Setting bus to %d\n", bus_no);
1464 ret = i2c_set_bus_num(bus_no);
Timur Tabie857a5b2006-11-28 12:09:35 -06001465 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001466 printf("Failure changing bus number (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001467 }
1468 return ret;
1469}
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001470#endif /* defined(CONFIG_SYS_I2C) */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001471
Marek Vasut06afa382012-11-12 14:34:27 +00001472/**
1473 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1474 * @cmdtp: Command data struct pointer
1475 * @flag: Command flag
1476 * @argc: Command-line argument count
1477 * @argv: Array of command-line arguments
1478 *
1479 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1480 * on error.
1481 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001482static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001483{
1484 int speed, ret=0;
1485
Timur Tabie857a5b2006-11-28 12:09:35 -06001486 if (argc == 1)
1487 /* querying current speed */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001488 printf("Current bus speed=%d\n", i2c_get_bus_speed());
Timur Tabie857a5b2006-11-28 12:09:35 -06001489 else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001490 speed = simple_strtoul(argv[1], NULL, 10);
1491 printf("Setting bus speed to %d Hz\n", speed);
1492 ret = i2c_set_bus_speed(speed);
Timur Tabie857a5b2006-11-28 12:09:35 -06001493 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001494 printf("Failure changing bus speed (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001495 }
1496 return ret;
1497}
1498
Marek Vasut06afa382012-11-12 14:34:27 +00001499/**
1500 * do_i2c_mm() - Handle the "i2c mm" 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_mm(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, 1, flag, argc, argv);
1512}
1513
Marek Vasut06afa382012-11-12 14:34:27 +00001514/**
1515 * do_i2c_nm() - Handle the "i2c nm" 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 on success, CMD_RET_USAGE in case of misuse and negative
1522 * on error.
1523 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001524static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001525{
1526 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1527}
1528
Marek Vasut06afa382012-11-12 14:34:27 +00001529/**
1530 * do_i2c_reset() - Handle the "i2c reset" command-line command
1531 * @cmdtp: Command data struct pointer
1532 * @flag: Command flag
1533 * @argc: Command-line argument count
1534 * @argv: Array of command-line arguments
1535 *
1536 * Returns zero always.
1537 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001538static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001539{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001540#if defined(CONFIG_SYS_I2C)
1541 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1542#else
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001543 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001544#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001545 return 0;
1546}
1547
1548static cmd_tbl_t cmd_i2c_sub[] = {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001549#if defined(CONFIG_SYS_I2C)
1550 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001551#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001552 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001553#if defined(CONFIG_SYS_I2C) || \
1554 defined(CONFIG_I2C_MULTI_BUS)
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001555 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1556#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001557#if defined(CONFIG_I2C_EDID)
1558 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1559#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001560 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1561 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1562 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1563 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1564 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1565 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001566 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
York Sunff5d2dc2012-09-16 08:02:30 +00001567 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001568 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1569#if defined(CONFIG_CMD_SDRAM)
1570 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1571#endif
1572 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1573};
1574
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001575#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001576void i2c_reloc(void) {
1577 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1578}
1579#endif
1580
Marek Vasut06afa382012-11-12 14:34:27 +00001581/**
1582 * do_i2c() - Handle the "i2c" command-line command
1583 * @cmdtp: Command data struct pointer
1584 * @flag: Command flag
1585 * @argc: Command-line argument count
1586 * @argv: Array of command-line arguments
1587 *
1588 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1589 * on error.
1590 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001591static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001592{
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001593 cmd_tbl_t *c;
1594
Heiko Schocher4444b222010-09-17 13:10:35 +02001595 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001596 return CMD_RET_USAGE;
Heiko Schocher4444b222010-09-17 13:10:35 +02001597
Peter Tysere96ad5d2009-04-18 22:34:04 -05001598 /* Strip off leading 'i2c' command argument */
1599 argc--;
1600 argv++;
1601
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001602 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1603
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001604 if (c)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001605 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001606 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00001607 return CMD_RET_USAGE;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001608}
wdenk8bde7f72003-06-27 21:31:46 +00001609
1610/***************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00001611#ifdef CONFIG_SYS_LONGHELP
1612static char i2c_help_text[] =
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001613#if defined(CONFIG_SYS_I2C)
1614 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001615#endif
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001616 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001617#if defined(CONFIG_SYS_I2C) || \
1618 defined(CONFIG_I2C_MULTI_BUS)
Peter Tyser9bc2e4e2008-10-01 12:25:04 -05001619 "i2c dev [dev] - show or set current I2C bus\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001620#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001621#if defined(CONFIG_I2C_EDID)
1622 "i2c edid chip - print EDID configuration information\n"
1623#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001624 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001625 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1626 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1627 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1628 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
Eric Nelson54b99e52012-09-23 10:12:56 +00001629 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001630 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
York Sunff5d2dc2012-09-16 08:02:30 +00001631 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
Heiko Schochere43a27c2008-10-15 09:33:30 +02001632 "i2c reset - re-init the I2C Controller\n"
Jon Loeligerc76fe472007-07-08 18:02:23 -05001633#if defined(CONFIG_CMD_SDRAM)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001634 "i2c sdram chip - print SDRAM configuration information\n"
Jon Loeliger90253172007-07-10 11:02:44 -05001635#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001636 "i2c speed [speed] - show or set I2C bus speed";
1637#endif
1638
1639U_BOOT_CMD(
1640 i2c, 6, 1, do_i2c,
1641 "I2C sub-system",
1642 i2c_help_text
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001643);