blob: 1703ab41ac0cb7f98b189b4e596eb04406b0dc1e [file] [log] [blame]
wdenk81a88242002-10-26 15:22:42 +00001/*
2 * (C) Copyright 2001
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk81a88242002-10-26 15:22:42 +00006 */
7
8/*
9 * I2C Functions similar to the standard memory functions.
10 *
11 * There are several parameters in many of the commands that bear further
12 * explanations:
13 *
wdenk81a88242002-10-26 15:22:42 +000014 * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
15 * Each I2C chip on the bus has a unique address. On the I2C data bus,
16 * the address is the upper seven bits and the LSB is the "read/write"
17 * bit. Note that the {i2c_chip} address specified on the command
18 * line is not shifted up: e.g. a typical EEPROM memory chip may have
19 * an I2C address of 0x50, but the data put on the bus will be 0xA0
20 * for write and 0xA1 for read. This "non shifted" address notation
21 * matches at least half of the data sheets :-/.
22 *
23 * {addr} is the address (or offset) within the chip. Small memory
24 * chips have 8 bit addresses. Large memory chips have 16 bit
25 * addresses. Other memory chips have 9, 10, or 11 bit addresses.
26 * Many non-memory chips have multiple registers and {addr} is used
27 * as the register index. Some non-memory chips have only one register
28 * and therefore don't need any {addr} parameter.
29 *
30 * The default {addr} parameter is one byte (.1) which works well for
31 * memories and registers with 8 bits of address space.
32 *
33 * You can specify the length of the {addr} field with the optional .0,
34 * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
35 * manipulating a single register device which doesn't use an address
36 * field, use "0.0" for the address and the ".0" length field will
37 * suppress the address in the I2C data stream. This also works for
38 * successive reads using the I2C auto-incrementing memory pointer.
39 *
40 * If you are manipulating a large memory with 2-byte addresses, use
41 * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
42 *
43 * Then there are the unfortunate memory chips that spill the most
44 * significant 1, 2, or 3 bits of address into the chip address byte.
45 * This effectively makes one chip (logically) look like 2, 4, or
46 * 8 chips. This is handled (awkwardly) by #defining
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
wdenk81a88242002-10-26 15:22:42 +000048 * {addr} field (since .1 is the default, it doesn't actually have to
49 * be specified). Examples: given a memory chip at I2C chip address
50 * 0x50, the following would happen...
Peter Tyser0f89c542009-04-18 22:34:03 -050051 * i2c md 50 0 10 display 16 bytes starting at 0x000
wdenk81a88242002-10-26 15:22:42 +000052 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050053 * i2c md 50 100 10 display 16 bytes starting at 0x100
wdenk81a88242002-10-26 15:22:42 +000054 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050055 * i2c md 50 210 10 display 16 bytes starting at 0x210
wdenk81a88242002-10-26 15:22:42 +000056 * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
57 * This is awfully ugly. It would be nice if someone would think up
58 * a better way of handling this.
59 *
60 * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
61 */
62
63#include <common.h>
64#include <command.h>
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +000065#include <edid.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020066#include <environment.h>
wdenk81a88242002-10-26 15:22:42 +000067#include <i2c.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020068#include <malloc.h>
wdenk81a88242002-10-26 15:22:42 +000069#include <asm/byteorder.h>
Marek Vasut2515d842012-11-12 14:34:25 +000070#include <linux/compiler.h>
wdenk81a88242002-10-26 15:22:42 +000071
wdenk81a88242002-10-26 15:22:42 +000072/* Display values from last command.
73 * Memory modify remembered values are different from display memory.
74 */
75static uchar i2c_dp_last_chip;
76static uint i2c_dp_last_addr;
77static uint i2c_dp_last_alen;
78static uint i2c_dp_last_length = 0x10;
79
80static uchar i2c_mm_last_chip;
81static uint i2c_mm_last_addr;
82static uint i2c_mm_last_alen;
83
Ben Warrenbb99ad62006-09-07 16:50:54 -040084/* If only one I2C bus is present, the list of devices to ignore when
85 * the probe command is issued is represented by a 1D array of addresses.
86 * When multiple buses are present, the list is an array of bus-address
87 * pairs. The following macros take care of this */
88
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020089#if defined(CONFIG_SYS_I2C_NOPROBES)
Ben Warrenbb99ad62006-09-07 16:50:54 -040090#if defined(CONFIG_I2C_MULTI_BUS)
91static struct
92{
93 uchar bus;
94 uchar addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095} i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -040096#define GET_BUS_NUM i2c_get_bus_num()
97#define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
98#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
99#define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
100#else /* single bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400102#define GET_BUS_NUM 0
103#define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
104#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
105#define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
106#endif /* CONFIG_MULTI_BUS */
107
108#define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
wdenk81a88242002-10-26 15:22:42 +0000109#endif
110
Heiko Schocher67b23a32008-10-15 09:39:47 +0200111#if defined(CONFIG_I2C_MUX)
112static I2C_MUX_DEVICE *i2c_mux_devices = NULL;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200113static int i2c_mux_busid = CONFIG_SYS_MAX_I2C_BUS;
Heiko Schocher67b23a32008-10-15 09:39:47 +0200114
115DECLARE_GLOBAL_DATA_PTR;
116
117#endif
118
Frans Meulenbroeksa266fe92010-03-26 09:46:40 +0100119#define DISP_LINE_LEN 16
120
Marek Vasut06afa382012-11-12 14:34:27 +0000121/**
122 * i2c_init_board() - Board-specific I2C bus init
123 *
124 * This function is the default no-op implementation of I2C bus
125 * initialization. This function can be overriden by board-specific
126 * implementation if needed.
127 */
Marek Vasut2515d842012-11-12 14:34:25 +0000128__weak
129void i2c_init_board(void)
Stefan Biglerc649dda2011-04-08 16:24:08 +0200130{
131 return;
132}
Stefan Biglerc649dda2011-04-08 16:24:08 +0200133
Peter Tyser655b34a2009-04-18 22:34:01 -0500134/* TODO: Implement architecture-specific get/set functions */
Marek Vasut06afa382012-11-12 14:34:27 +0000135
136/**
137 * i2c_get_bus_speed() - Return I2C bus speed
138 *
139 * This function is the default implementation of function for retrieveing
140 * the current I2C bus speed in Hz.
141 *
142 * A driver implementing runtime switching of I2C bus speed must override
143 * this function to report the speed correctly. Simple or legacy drivers
144 * can use this fallback.
145 *
146 * Returns I2C bus speed in Hz.
147 */
Marek Vasut2515d842012-11-12 14:34:25 +0000148__weak
149unsigned int i2c_get_bus_speed(void)
Peter Tyser655b34a2009-04-18 22:34:01 -0500150{
151 return CONFIG_SYS_I2C_SPEED;
152}
Peter Tyser655b34a2009-04-18 22:34:01 -0500153
Marek Vasut06afa382012-11-12 14:34:27 +0000154/**
155 * i2c_set_bus_speed() - Configure I2C bus speed
156 * @speed: Newly set speed of the I2C bus in Hz
157 *
158 * This function is the default implementation of function for setting
159 * the I2C bus speed in Hz.
160 *
161 * A driver implementing runtime switching of I2C bus speed must override
162 * this function to report the speed correctly. Simple or legacy drivers
163 * can use this fallback.
164 *
165 * Returns zero on success, negative value on error.
166 */
Marek Vasut2515d842012-11-12 14:34:25 +0000167__weak
168int i2c_set_bus_speed(unsigned int speed)
Peter Tyser655b34a2009-04-18 22:34:01 -0500169{
170 if (speed != CONFIG_SYS_I2C_SPEED)
171 return -1;
172
173 return 0;
174}
Peter Tyser655b34a2009-04-18 22:34:01 -0500175
Marek Vasut06afa382012-11-12 14:34:27 +0000176/**
177 * get_alen() - Small parser helper function to get address length
178 *
179 * Returns the address length.
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100180 */
181static uint get_alen(char *arg)
182{
183 int j;
184 int alen;
185
186 alen = 1;
187 for (j = 0; j < 8; j++) {
188 if (arg[j] == '.') {
189 alen = arg[j+1] - '0';
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100190 break;
191 } else if (arg[j] == '\0')
192 break;
193 }
194 return alen;
195}
196
Marek Vasut06afa382012-11-12 14:34:27 +0000197/**
198 * do_i2c_read() - Handle the "i2c read" command-line command
199 * @cmdtp: Command data struct pointer
200 * @flag: Command flag
201 * @argc: Command-line argument count
202 * @argv: Array of command-line arguments
203 *
204 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
205 * on error.
206 *
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100207 * Syntax:
208 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
209 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200210static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100211{
212 u_char chip;
213 uint devaddr, alen, length;
214 u_char *memaddr;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100215
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200216 if (argc != 5)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000217 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100218
219 /*
220 * I2C chip address
221 */
222 chip = simple_strtoul(argv[1], NULL, 16);
223
224 /*
225 * I2C data address within the chip. This can be 1 or
226 * 2 bytes long. Some day it might be 3 bytes long :-).
227 */
228 devaddr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100229 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200230 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000231 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100232
233 /*
234 * Length is the number of objects, not number of bytes.
235 */
236 length = simple_strtoul(argv[3], NULL, 16);
237
238 /*
239 * memaddr is the address where to store things in memory
240 */
241 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
242
243 if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
244 puts ("Error reading the chip.\n");
245 return 1;
246 }
247 return 0;
248}
249
York Sunff5d2dc2012-09-16 08:02:30 +0000250static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
251{
252 u_char chip;
253 uint devaddr, alen, length;
254 u_char *memaddr;
255
256 if (argc != 5)
257 return cmd_usage(cmdtp);
258
259 /*
260 * memaddr is the address where to store things in memory
261 */
262 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
263
264 /*
265 * I2C chip address
266 */
267 chip = simple_strtoul(argv[2], NULL, 16);
268
269 /*
270 * I2C data address within the chip. This can be 1 or
271 * 2 bytes long. Some day it might be 3 bytes long :-).
272 */
273 devaddr = simple_strtoul(argv[3], NULL, 16);
274 alen = get_alen(argv[3]);
275 if (alen > 3)
276 return cmd_usage(cmdtp);
277
278 /*
279 * Length is the number of objects, not number of bytes.
280 */
281 length = simple_strtoul(argv[4], NULL, 16);
282
283 while (length-- > 0) {
284 if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
285 puts("Error writing to the chip.\n");
286 return 1;
287 }
288/*
289 * No write delay with FRAM devices.
290 */
291#if !defined(CONFIG_SYS_I2C_FRAM)
292 udelay(11000);
293#endif
294 }
295 return 0;
296}
297
Marek Vasut06afa382012-11-12 14:34:27 +0000298/**
299 * do_i2c_md() - Handle the "i2c md" command-line command
300 * @cmdtp: Command data struct pointer
301 * @flag: Command flag
302 * @argc: Command-line argument count
303 * @argv: Array of command-line arguments
304 *
305 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
306 * on error.
307 *
Frans Meulenbroeks4a8cf332010-03-26 09:46:39 +0100308 * Syntax:
309 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
310 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200311static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000312{
313 u_char chip;
314 uint addr, alen, length;
315 int j, nbytes, linebytes;
316
317 /* We use the last specified parameters, unless new ones are
318 * entered.
319 */
320 chip = i2c_dp_last_chip;
321 addr = i2c_dp_last_addr;
322 alen = i2c_dp_last_alen;
323 length = i2c_dp_last_length;
324
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200325 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000326 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000327
328 if ((flag & CMD_FLAG_REPEAT) == 0) {
329 /*
330 * New command specified.
331 */
wdenk81a88242002-10-26 15:22:42 +0000332
333 /*
334 * I2C chip address
335 */
336 chip = simple_strtoul(argv[1], NULL, 16);
337
338 /*
339 * I2C data address within the chip. This can be 1 or
340 * 2 bytes long. Some day it might be 3 bytes long :-).
341 */
342 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100343 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200344 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000345 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000346
347 /*
348 * If another parameter, it is the length to display.
349 * Length is the number of objects, not number of bytes.
350 */
351 if (argc > 3)
352 length = simple_strtoul(argv[3], NULL, 16);
353 }
354
355 /*
356 * Print the lines.
357 *
358 * We buffer all read data, so we can make sure data is read only
359 * once.
360 */
361 nbytes = length;
362 do {
363 unsigned char linebuf[DISP_LINE_LEN];
364 unsigned char *cp;
365
366 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
367
Timur Tabie857a5b2006-11-28 12:09:35 -0600368 if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000369 puts ("Error reading the chip.\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600370 else {
wdenk81a88242002-10-26 15:22:42 +0000371 printf("%04x:", addr);
372 cp = linebuf;
373 for (j=0; j<linebytes; j++) {
374 printf(" %02x", *cp++);
375 addr++;
376 }
wdenk4b9206e2004-03-23 22:14:11 +0000377 puts (" ");
wdenk81a88242002-10-26 15:22:42 +0000378 cp = linebuf;
379 for (j=0; j<linebytes; j++) {
380 if ((*cp < 0x20) || (*cp > 0x7e))
wdenk4b9206e2004-03-23 22:14:11 +0000381 puts (".");
wdenk81a88242002-10-26 15:22:42 +0000382 else
383 printf("%c", *cp);
384 cp++;
385 }
wdenk4b9206e2004-03-23 22:14:11 +0000386 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000387 }
388 nbytes -= linebytes;
389 } while (nbytes > 0);
390
391 i2c_dp_last_chip = chip;
392 i2c_dp_last_addr = addr;
393 i2c_dp_last_alen = alen;
394 i2c_dp_last_length = length;
395
396 return 0;
397}
398
Marek Vasut06afa382012-11-12 14:34:27 +0000399/**
400 * do_i2c_mw() - Handle the "i2c mw" command-line command
401 * @cmdtp: Command data struct pointer
402 * @flag: Command flag
403 * @argc: Command-line argument count
404 * @argv: Array of command-line arguments
405 *
406 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
407 * on error.
wdenk81a88242002-10-26 15:22:42 +0000408 *
409 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500410 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
wdenk81a88242002-10-26 15:22:42 +0000411 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200412static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000413{
414 uchar chip;
415 ulong addr;
416 uint alen;
417 uchar byte;
418 int count;
wdenk81a88242002-10-26 15:22:42 +0000419
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200420 if ((argc < 4) || (argc > 5))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000421 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000422
423 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200424 * Chip is always specified.
425 */
wdenk81a88242002-10-26 15:22:42 +0000426 chip = simple_strtoul(argv[1], NULL, 16);
427
428 /*
429 * Address is always specified.
430 */
431 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100432 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200433 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000434 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000435
436 /*
437 * Value to write is always specified.
438 */
439 byte = simple_strtoul(argv[3], NULL, 16);
440
441 /*
442 * Optional count
443 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600444 if (argc == 5)
wdenk81a88242002-10-26 15:22:42 +0000445 count = simple_strtoul(argv[4], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600446 else
wdenk81a88242002-10-26 15:22:42 +0000447 count = 1;
wdenk81a88242002-10-26 15:22:42 +0000448
449 while (count-- > 0) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600450 if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000451 puts ("Error writing the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000452 /*
453 * Wait for the write to complete. The write can take
454 * up to 10mSec (we allow a little more time).
wdenk81a88242002-10-26 15:22:42 +0000455 */
d4f5c722005-08-12 21:16:13 +0200456/*
457 * No write delay with FRAM devices.
458 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200459#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk81a88242002-10-26 15:22:42 +0000460 udelay(11000);
d4f5c722005-08-12 21:16:13 +0200461#endif
wdenk81a88242002-10-26 15:22:42 +0000462 }
463
Marek Vasut06afa382012-11-12 14:34:27 +0000464 return 0;
wdenk81a88242002-10-26 15:22:42 +0000465}
466
Marek Vasut06afa382012-11-12 14:34:27 +0000467/**
468 * do_i2c_crc() - Handle the "i2c crc32" command-line command
469 * @cmdtp: Command data struct pointer
470 * @flag: Command flag
471 * @argc: Command-line argument count
472 * @argv: Array of command-line arguments
473 *
474 * Calculate a CRC on memory
475 *
476 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
477 * on error.
wdenk81a88242002-10-26 15:22:42 +0000478 *
479 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500480 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
wdenk81a88242002-10-26 15:22:42 +0000481 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200482static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000483{
484 uchar chip;
485 ulong addr;
486 uint alen;
487 int count;
488 uchar byte;
489 ulong crc;
490 ulong err;
wdenk81a88242002-10-26 15:22:42 +0000491
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200492 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000493 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000494
495 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200496 * Chip is always specified.
497 */
wdenk81a88242002-10-26 15:22:42 +0000498 chip = simple_strtoul(argv[1], NULL, 16);
499
500 /*
501 * Address is always specified.
502 */
503 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100504 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200505 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000506 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000507
508 /*
509 * Count is always specified
510 */
511 count = simple_strtoul(argv[3], NULL, 16);
512
513 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
514 /*
515 * CRC a byte at a time. This is going to be slooow, but hey, the
516 * memories are small and slow too so hopefully nobody notices.
517 */
518 crc = 0;
519 err = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600520 while (count-- > 0) {
521 if (i2c_read(chip, addr, alen, &byte, 1) != 0)
wdenk81a88242002-10-26 15:22:42 +0000522 err++;
wdenk81a88242002-10-26 15:22:42 +0000523 crc = crc32 (crc, &byte, 1);
524 addr++;
525 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600526 if (err > 0)
wdenk4b9206e2004-03-23 22:14:11 +0000527 puts ("Error reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600528 else
wdenk81a88242002-10-26 15:22:42 +0000529 printf ("%08lx\n", crc);
wdenk81a88242002-10-26 15:22:42 +0000530
531 return 0;
532}
533
Marek Vasut06afa382012-11-12 14:34:27 +0000534/**
535 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
536 * @cmdtp: Command data struct pointer
537 * @flag: Command flag
538 * @argc: Command-line argument count
539 * @argv: Array of command-line arguments
540 *
541 * Modify memory.
542 *
543 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
544 * on error.
wdenk81a88242002-10-26 15:22:42 +0000545 *
546 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500547 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
548 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
wdenk81a88242002-10-26 15:22:42 +0000549 */
wdenk81a88242002-10-26 15:22:42 +0000550static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200551mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000552{
553 uchar chip;
554 ulong addr;
555 uint alen;
556 ulong data;
557 int size = 1;
558 int nbytes;
wdenk81a88242002-10-26 15:22:42 +0000559
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200560 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000561 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000562
563#ifdef CONFIG_BOOT_RETRY_TIME
564 reset_cmd_timeout(); /* got a good command to get here */
565#endif
566 /*
567 * We use the last specified parameters, unless new ones are
568 * entered.
569 */
570 chip = i2c_mm_last_chip;
571 addr = i2c_mm_last_addr;
572 alen = i2c_mm_last_alen;
573
574 if ((flag & CMD_FLAG_REPEAT) == 0) {
575 /*
576 * New command specified. Check for a size specification.
577 * Defaults to byte if no or incorrect specification.
578 */
579 size = cmd_get_data_size(argv[0], 1);
580
581 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200582 * Chip is always specified.
583 */
wdenk81a88242002-10-26 15:22:42 +0000584 chip = simple_strtoul(argv[1], NULL, 16);
585
586 /*
587 * Address is always specified.
588 */
589 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100590 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200591 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000592 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000593 }
594
595 /*
596 * Print the address, followed by value. Then accept input for
597 * the next value. A non-converted value exits.
598 */
599 do {
600 printf("%08lx:", addr);
Timur Tabie857a5b2006-11-28 12:09:35 -0600601 if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000602 puts ("\nError reading the chip,\n");
Timur Tabie857a5b2006-11-28 12:09:35 -0600603 else {
wdenk81a88242002-10-26 15:22:42 +0000604 data = cpu_to_be32(data);
Timur Tabie857a5b2006-11-28 12:09:35 -0600605 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000606 printf(" %02lx", (data >> 24) & 0x000000FF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600607 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000608 printf(" %04lx", (data >> 16) & 0x0000FFFF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600609 else
wdenk81a88242002-10-26 15:22:42 +0000610 printf(" %08lx", data);
wdenk81a88242002-10-26 15:22:42 +0000611 }
612
613 nbytes = readline (" ? ");
614 if (nbytes == 0) {
615 /*
616 * <CR> pressed as only input, don't modify current
617 * location and move to next.
618 */
619 if (incrflag)
620 addr += size;
621 nbytes = size;
622#ifdef CONFIG_BOOT_RETRY_TIME
623 reset_cmd_timeout(); /* good enough to not time out */
624#endif
625 }
626#ifdef CONFIG_BOOT_RETRY_TIME
Timur Tabie857a5b2006-11-28 12:09:35 -0600627 else if (nbytes == -2)
wdenk81a88242002-10-26 15:22:42 +0000628 break; /* timed out, exit the command */
wdenk81a88242002-10-26 15:22:42 +0000629#endif
630 else {
631 char *endp;
632
633 data = simple_strtoul(console_buffer, &endp, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600634 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000635 data = data << 24;
Timur Tabie857a5b2006-11-28 12:09:35 -0600636 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000637 data = data << 16;
wdenk81a88242002-10-26 15:22:42 +0000638 data = be32_to_cpu(data);
639 nbytes = endp - console_buffer;
640 if (nbytes) {
641#ifdef CONFIG_BOOT_RETRY_TIME
642 /*
643 * good enough to not time out
644 */
645 reset_cmd_timeout();
646#endif
Timur Tabie857a5b2006-11-28 12:09:35 -0600647 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000648 puts ("Error writing the chip.\n");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200649#ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
650 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk2535d602003-07-17 23:16:40 +0000651#endif
wdenk81a88242002-10-26 15:22:42 +0000652 if (incrflag)
653 addr += size;
654 }
655 }
656 } while (nbytes);
657
Peter Tyser08007072008-08-15 14:36:32 -0500658 i2c_mm_last_chip = chip;
659 i2c_mm_last_addr = addr;
660 i2c_mm_last_alen = alen;
wdenk81a88242002-10-26 15:22:42 +0000661
662 return 0;
663}
664
Marek Vasut06afa382012-11-12 14:34:27 +0000665/**
666 * do_i2c_probe() - Handle the "i2c probe" command-line command
667 * @cmdtp: Command data struct pointer
668 * @flag: Command flag
669 * @argc: Command-line argument count
670 * @argv: Array of command-line arguments
671 *
672 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
673 * on error.
674 *
wdenk81a88242002-10-26 15:22:42 +0000675 * Syntax:
Eric Nelson54b99e52012-09-23 10:12:56 +0000676 * i2c probe {addr}
677 *
678 * Returns zero (success) if one or more I2C devices was found
wdenk81a88242002-10-26 15:22:42 +0000679 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200680static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000681{
682 int j;
Eric Nelson54b99e52012-09-23 10:12:56 +0000683 int addr = -1;
684 int found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200685#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000686 int k, skip;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400687 uchar bus = GET_BUS_NUM;
688#endif /* NOPROBES */
wdenk81a88242002-10-26 15:22:42 +0000689
Eric Nelson54b99e52012-09-23 10:12:56 +0000690 if (argc == 2)
691 addr = simple_strtol(argv[1], 0, 16);
692
wdenk4b9206e2004-03-23 22:14:11 +0000693 puts ("Valid chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600694 for (j = 0; j < 128; j++) {
Eric Nelson54b99e52012-09-23 10:12:56 +0000695 if ((0 <= addr) && (j != addr))
696 continue;
697
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200698#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000699 skip = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600700 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
701 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
wdenk81a88242002-10-26 15:22:42 +0000702 skip = 1;
703 break;
704 }
705 }
706 if (skip)
707 continue;
708#endif
Eric Nelson54b99e52012-09-23 10:12:56 +0000709 if (i2c_probe(j) == 0) {
wdenk81a88242002-10-26 15:22:42 +0000710 printf(" %02X", j);
Eric Nelson54b99e52012-09-23 10:12:56 +0000711 found++;
712 }
wdenk81a88242002-10-26 15:22:42 +0000713 }
wdenk4b9206e2004-03-23 22:14:11 +0000714 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000715
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200716#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000717 puts ("Excluded chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600718 for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
719 if (COMPARE_BUS(bus,k))
Ben Warrenbb99ad62006-09-07 16:50:54 -0400720 printf(" %02X", NO_PROBE_ADDR(k));
721 }
wdenk4b9206e2004-03-23 22:14:11 +0000722 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000723#endif
724
Eric Nelson54b99e52012-09-23 10:12:56 +0000725 return (0 == found);
wdenk81a88242002-10-26 15:22:42 +0000726}
727
Marek Vasut06afa382012-11-12 14:34:27 +0000728/**
729 * do_i2c_loop() - Handle the "i2c loop" command-line command
730 * @cmdtp: Command data struct pointer
731 * @flag: Command flag
732 * @argc: Command-line argument count
733 * @argv: Array of command-line arguments
734 *
735 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
736 * on error.
737 *
wdenk81a88242002-10-26 15:22:42 +0000738 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500739 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
wdenk81a88242002-10-26 15:22:42 +0000740 * {length} - Number of bytes to read
741 * {delay} - A DECIMAL number and defaults to 1000 uSec
742 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200743static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000744{
745 u_char chip;
746 ulong alen;
747 uint addr;
748 uint length;
749 u_char bytes[16];
750 int delay;
wdenk81a88242002-10-26 15:22:42 +0000751
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200752 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000753 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000754
755 /*
756 * Chip is always specified.
757 */
758 chip = simple_strtoul(argv[1], NULL, 16);
759
760 /*
761 * Address is always specified.
762 */
763 addr = simple_strtoul(argv[2], NULL, 16);
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100764 alen = get_alen(argv[2]);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200765 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000766 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000767
768 /*
769 * Length is the number of objects, not number of bytes.
770 */
771 length = 1;
772 length = simple_strtoul(argv[3], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600773 if (length > sizeof(bytes))
wdenk81a88242002-10-26 15:22:42 +0000774 length = sizeof(bytes);
wdenk81a88242002-10-26 15:22:42 +0000775
776 /*
777 * The delay time (uSec) is optional.
778 */
779 delay = 1000;
Timur Tabie857a5b2006-11-28 12:09:35 -0600780 if (argc > 3)
wdenk81a88242002-10-26 15:22:42 +0000781 delay = simple_strtoul(argv[4], NULL, 10);
wdenk81a88242002-10-26 15:22:42 +0000782 /*
783 * Run the loop...
784 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600785 while (1) {
786 if (i2c_read(chip, addr, alen, bytes, length) != 0)
wdenk4b9206e2004-03-23 22:14:11 +0000787 puts ("Error reading the chip.\n");
wdenk81a88242002-10-26 15:22:42 +0000788 udelay(delay);
789 }
790
791 /* NOTREACHED */
792 return 0;
793}
794
wdenk81a88242002-10-26 15:22:42 +0000795/*
796 * The SDRAM command is separately configured because many
797 * (most?) embedded boards don't use SDRAM DIMMs.
Marek Vasut06afa382012-11-12 14:34:27 +0000798 *
799 * FIXME: Document and probably move elsewhere!
wdenk81a88242002-10-26 15:22:42 +0000800 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500801#if defined(CONFIG_CMD_SDRAM)
Larry Johnson632de062008-01-11 23:26:18 -0500802static void print_ddr2_tcyc (u_char const b)
803{
804 printf ("%d.", (b >> 4) & 0x0F);
805 switch (b & 0x0F) {
806 case 0x0:
807 case 0x1:
808 case 0x2:
809 case 0x3:
810 case 0x4:
811 case 0x5:
812 case 0x6:
813 case 0x7:
814 case 0x8:
815 case 0x9:
816 printf ("%d ns\n", b & 0x0F);
817 break;
818 case 0xA:
819 puts ("25 ns\n");
820 break;
821 case 0xB:
822 puts ("33 ns\n");
823 break;
824 case 0xC:
825 puts ("66 ns\n");
826 break;
827 case 0xD:
828 puts ("75 ns\n");
829 break;
830 default:
831 puts ("?? ns\n");
832 break;
833 }
834}
835
836static void decode_bits (u_char const b, char const *str[], int const do_once)
837{
838 u_char mask;
839
840 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
841 if (b & mask) {
842 puts (*str);
843 if (do_once)
844 return;
845 }
846 }
847}
wdenk81a88242002-10-26 15:22:42 +0000848
849/*
850 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500851 * i2c sdram {i2c_chip}
wdenk81a88242002-10-26 15:22:42 +0000852 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200853static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000854{
Larry Johnson632de062008-01-11 23:26:18 -0500855 enum { unknown, EDO, SDRAM, DDR2 } type;
856
wdenk81a88242002-10-26 15:22:42 +0000857 u_char chip;
858 u_char data[128];
859 u_char cksum;
860 int j;
861
Larry Johnson632de062008-01-11 23:26:18 -0500862 static const char *decode_CAS_DDR2[] = {
863 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
864 };
865
866 static const char *decode_CAS_default[] = {
867 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
868 };
869
870 static const char *decode_CS_WE_default[] = {
871 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
872 };
873
874 static const char *decode_byte21_default[] = {
875 " TBD (bit 7)\n",
876 " Redundant row address\n",
877 " Differential clock input\n",
878 " Registerd DQMB inputs\n",
879 " Buffered DQMB inputs\n",
880 " On-card PLL\n",
881 " Registered address/control lines\n",
882 " Buffered address/control lines\n"
883 };
884
885 static const char *decode_byte22_DDR2[] = {
886 " TBD (bit 7)\n",
887 " TBD (bit 6)\n",
888 " TBD (bit 5)\n",
889 " TBD (bit 4)\n",
890 " TBD (bit 3)\n",
891 " Supports partial array self refresh\n",
892 " Supports 50 ohm ODT\n",
893 " Supports weak driver\n"
894 };
895
896 static const char *decode_row_density_DDR2[] = {
897 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
898 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
899 };
900
901 static const char *decode_row_density_default[] = {
902 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
903 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
904 };
905
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200906 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000907 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200908
wdenk81a88242002-10-26 15:22:42 +0000909 /*
910 * Chip is always specified.
Larry Johnson632de062008-01-11 23:26:18 -0500911 */
912 chip = simple_strtoul (argv[1], NULL, 16);
wdenk81a88242002-10-26 15:22:42 +0000913
Larry Johnson632de062008-01-11 23:26:18 -0500914 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000915 puts ("No SDRAM Serial Presence Detect found.\n");
wdenk81a88242002-10-26 15:22:42 +0000916 return 1;
917 }
918
919 cksum = 0;
920 for (j = 0; j < 63; j++) {
921 cksum += data[j];
922 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600923 if (cksum != data[63]) {
wdenk81a88242002-10-26 15:22:42 +0000924 printf ("WARNING: Configuration data checksum failure:\n"
Larry Johnson632de062008-01-11 23:26:18 -0500925 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
wdenk81a88242002-10-26 15:22:42 +0000926 }
Larry Johnson632de062008-01-11 23:26:18 -0500927 printf ("SPD data revision %d.%d\n",
wdenk81a88242002-10-26 15:22:42 +0000928 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
Larry Johnson632de062008-01-11 23:26:18 -0500929 printf ("Bytes used 0x%02X\n", data[0]);
930 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
931
wdenk4b9206e2004-03-23 22:14:11 +0000932 puts ("Memory type ");
Larry Johnson632de062008-01-11 23:26:18 -0500933 switch (data[2]) {
Larry Johnson0df6b842008-01-10 22:23:39 -0500934 case 2:
935 type = EDO;
936 puts ("EDO\n");
937 break;
938 case 4:
939 type = SDRAM;
940 puts ("SDRAM\n");
941 break;
942 case 8:
943 type = DDR2;
944 puts ("DDR2\n");
945 break;
946 default:
947 type = unknown;
948 puts ("unknown\n");
949 break;
wdenk81a88242002-10-26 15:22:42 +0000950 }
Larry Johnson632de062008-01-11 23:26:18 -0500951
wdenk4b9206e2004-03-23 22:14:11 +0000952 puts ("Row address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600953 if ((data[3] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500954 printf ("%d\n", data[3] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600955 else
Larry Johnson632de062008-01-11 23:26:18 -0500956 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
957
wdenk4b9206e2004-03-23 22:14:11 +0000958 puts ("Column address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -0600959 if ((data[4] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -0500960 printf ("%d\n", data[4] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -0600961 else
Larry Johnson632de062008-01-11 23:26:18 -0500962 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -0500963
964 switch (type) {
965 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500966 printf ("Number of ranks %d\n",
967 (data[5] & 0x07) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -0500968 break;
969 default:
Larry Johnson632de062008-01-11 23:26:18 -0500970 printf ("Module rows %d\n", data[5]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500971 break;
972 }
973
974 switch (type) {
975 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500976 printf ("Module data width %d bits\n", data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500977 break;
978 default:
Larry Johnson632de062008-01-11 23:26:18 -0500979 printf ("Module data width %d bits\n",
980 (data[7] << 8) | data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500981 break;
982 }
983
wdenk4b9206e2004-03-23 22:14:11 +0000984 puts ("Interface signal levels ");
wdenk81a88242002-10-26 15:22:42 +0000985 switch(data[8]) {
Larry Johnson0df6b842008-01-10 22:23:39 -0500986 case 0: puts ("TTL 5.0 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +0000987 case 1: puts ("LVTTL\n"); break;
Larry Johnson0df6b842008-01-10 22:23:39 -0500988 case 2: puts ("HSTL 1.5 V\n"); break;
989 case 3: puts ("SSTL 3.3 V\n"); break;
990 case 4: puts ("SSTL 2.5 V\n"); break;
991 case 5: puts ("SSTL 1.8 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +0000992 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +0000993 }
Larry Johnson0df6b842008-01-10 22:23:39 -0500994
995 switch (type) {
996 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -0500997 printf ("SDRAM cycle time ");
998 print_ddr2_tcyc (data[9]);
Larry Johnson0df6b842008-01-10 22:23:39 -0500999 break;
1000 default:
Larry Johnson632de062008-01-11 23:26:18 -05001001 printf ("SDRAM cycle time %d.%d ns\n",
1002 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001003 break;
1004 }
1005
1006 switch (type) {
1007 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001008 printf ("SDRAM access time 0.%d%d ns\n",
1009 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001010 break;
1011 default:
Larry Johnson632de062008-01-11 23:26:18 -05001012 printf ("SDRAM access time %d.%d ns\n",
1013 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001014 break;
1015 }
1016
wdenk4b9206e2004-03-23 22:14:11 +00001017 puts ("EDC configuration ");
Larry Johnson632de062008-01-11 23:26:18 -05001018 switch (data[11]) {
wdenk4b9206e2004-03-23 22:14:11 +00001019 case 0: puts ("None\n"); break;
1020 case 1: puts ("Parity\n"); break;
1021 case 2: puts ("ECC\n"); break;
1022 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001023 }
Larry Johnson632de062008-01-11 23:26:18 -05001024
Timur Tabie857a5b2006-11-28 12:09:35 -06001025 if ((data[12] & 0x80) == 0)
wdenk4b9206e2004-03-23 22:14:11 +00001026 puts ("No self refresh, rate ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001027 else
wdenk4b9206e2004-03-23 22:14:11 +00001028 puts ("Self refresh, rate ");
Larry Johnson632de062008-01-11 23:26:18 -05001029
wdenk81a88242002-10-26 15:22:42 +00001030 switch(data[12] & 0x7F) {
Larry Johnson632de062008-01-11 23:26:18 -05001031 case 0: puts ("15.625 us\n"); break;
1032 case 1: puts ("3.9 us\n"); break;
1033 case 2: puts ("7.8 us\n"); break;
1034 case 3: puts ("31.3 us\n"); break;
1035 case 4: puts ("62.5 us\n"); break;
1036 case 5: puts ("125 us\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001037 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001038 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001039
1040 switch (type) {
1041 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001042 printf ("SDRAM width (primary) %d\n", data[13]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001043 break;
1044 default:
Larry Johnson632de062008-01-11 23:26:18 -05001045 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001046 if ((data[13] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001047 printf (" (second bank) %d\n",
1048 2 * (data[13] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001049 }
1050 break;
wdenk81a88242002-10-26 15:22:42 +00001051 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001052
1053 switch (type) {
1054 case DDR2:
1055 if (data[14] != 0)
Larry Johnson632de062008-01-11 23:26:18 -05001056 printf ("EDC width %d\n", data[14]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001057 break;
1058 default:
1059 if (data[14] != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001060 printf ("EDC width %d\n",
1061 data[14] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001062
1063 if ((data[14] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001064 printf (" (second bank) %d\n",
1065 2 * (data[14] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001066 }
1067 }
1068 break;
1069 }
1070
Larry Johnson632de062008-01-11 23:26:18 -05001071 if (DDR2 != type) {
1072 printf ("Min clock delay, back-to-back random column addresses "
1073 "%d\n", data[15]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001074 }
1075
wdenk4b9206e2004-03-23 22:14:11 +00001076 puts ("Burst length(s) ");
1077 if (data[16] & 0x80) puts (" Page");
1078 if (data[16] & 0x08) puts (" 8");
1079 if (data[16] & 0x04) puts (" 4");
1080 if (data[16] & 0x02) puts (" 2");
1081 if (data[16] & 0x01) puts (" 1");
1082 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001083 printf ("Number of banks %d\n", data[17]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001084
1085 switch (type) {
1086 case DDR2:
1087 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001088 decode_bits (data[18], decode_CAS_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001089 putc ('\n');
1090 break;
1091 default:
1092 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001093 decode_bits (data[18], decode_CAS_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001094 putc ('\n');
1095 break;
1096 }
1097
1098 if (DDR2 != type) {
1099 puts ("CS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001100 decode_bits (data[19], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001101 putc ('\n');
1102 }
1103
1104 if (DDR2 != type) {
1105 puts ("WE latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001106 decode_bits (data[20], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001107 putc ('\n');
1108 }
1109
1110 switch (type) {
1111 case DDR2:
1112 puts ("Module attributes:\n");
1113 if (data[21] & 0x80)
1114 puts (" TBD (bit 7)\n");
1115 if (data[21] & 0x40)
1116 puts (" Analysis probe installed\n");
1117 if (data[21] & 0x20)
1118 puts (" TBD (bit 5)\n");
1119 if (data[21] & 0x10)
1120 puts (" FET switch external enable\n");
Larry Johnson632de062008-01-11 23:26:18 -05001121 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
Larry Johnson0df6b842008-01-10 22:23:39 -05001122 if (data[20] & 0x11) {
Larry Johnson632de062008-01-11 23:26:18 -05001123 printf (" %d active registers on DIMM\n",
1124 (data[21] & 0x03) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001125 }
1126 break;
1127 default:
1128 puts ("Module attributes:\n");
1129 if (!data[21])
1130 puts (" (none)\n");
Larry Johnson632de062008-01-11 23:26:18 -05001131 else
1132 decode_bits (data[21], decode_byte21_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001133 break;
1134 }
1135
1136 switch (type) {
1137 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001138 decode_bits (data[22], decode_byte22_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001139 break;
1140 default:
1141 puts ("Device attributes:\n");
1142 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1143 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1144 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1145 else puts (" Upper Vcc tolerance 10%\n");
1146 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1147 else puts (" Lower Vcc tolerance 10%\n");
1148 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1149 if (data[22] & 0x04) puts (" Supports precharge all\n");
1150 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1151 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1152 break;
1153 }
1154
1155 switch (type) {
1156 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001157 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1158 print_ddr2_tcyc (data[23]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001159 break;
1160 default:
Larry Johnson632de062008-01-11 23:26:18 -05001161 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1162 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001163 break;
1164 }
1165
1166 switch (type) {
1167 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001168 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1169 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001170 break;
1171 default:
Larry Johnson632de062008-01-11 23:26:18 -05001172 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1173 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001174 break;
1175 }
1176
1177 switch (type) {
1178 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001179 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1180 print_ddr2_tcyc (data[25]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001181 break;
1182 default:
Larry Johnson632de062008-01-11 23:26:18 -05001183 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1184 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001185 break;
1186 }
1187
1188 switch (type) {
1189 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001190 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1191 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001192 break;
1193 default:
Larry Johnson632de062008-01-11 23:26:18 -05001194 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1195 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001196 break;
1197 }
1198
1199 switch (type) {
1200 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001201 printf ("Minimum row precharge %d.%02d ns\n",
1202 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001203 break;
1204 default:
Larry Johnson632de062008-01-11 23:26:18 -05001205 printf ("Minimum row precharge %d ns\n", data[27]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001206 break;
1207 }
1208
1209 switch (type) {
1210 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001211 printf ("Row active to row active min %d.%02d ns\n",
1212 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001213 break;
1214 default:
Larry Johnson632de062008-01-11 23:26:18 -05001215 printf ("Row active to row active min %d ns\n", data[28]);
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 ("RAS to CAS delay min %d.%02d ns\n",
1222 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001223 break;
1224 default:
Larry Johnson632de062008-01-11 23:26:18 -05001225 printf ("RAS to CAS delay min %d ns\n", data[29]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001226 break;
1227 }
1228
Larry Johnson632de062008-01-11 23:26:18 -05001229 printf ("Minimum RAS pulse width %d ns\n", data[30]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001230
1231 switch (type) {
1232 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001233 puts ("Density of each row ");
1234 decode_bits (data[31], decode_row_density_DDR2, 1);
1235 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001236 break;
1237 default:
Larry Johnson632de062008-01-11 23:26:18 -05001238 puts ("Density of each row ");
1239 decode_bits (data[31], decode_row_density_default, 1);
1240 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001241 break;
1242 }
1243
1244 switch (type) {
1245 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001246 puts ("Command and Address setup ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001247 if (data[32] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001248 printf ("1.%d%d ns\n",
1249 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001250 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001251 printf ("0.%d%d ns\n",
1252 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001253 }
1254 break;
1255 default:
Larry Johnson632de062008-01-11 23:26:18 -05001256 printf ("Command and Address setup %c%d.%d ns\n",
1257 (data[32] & 0x80) ? '-' : '+',
1258 (data[32] >> 4) & 0x07, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001259 break;
1260 }
1261
1262 switch (type) {
1263 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001264 puts ("Command and Address hold ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001265 if (data[33] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001266 printf ("1.%d%d ns\n",
1267 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001268 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001269 printf ("0.%d%d ns\n",
1270 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001271 }
1272 break;
1273 default:
Larry Johnson632de062008-01-11 23:26:18 -05001274 printf ("Command and Address hold %c%d.%d ns\n",
1275 (data[33] & 0x80) ? '-' : '+',
1276 (data[33] >> 4) & 0x07, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001277 break;
1278 }
1279
1280 switch (type) {
1281 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001282 printf ("Data signal input setup 0.%d%d ns\n",
1283 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001284 break;
1285 default:
Larry Johnson632de062008-01-11 23:26:18 -05001286 printf ("Data signal input setup %c%d.%d ns\n",
1287 (data[34] & 0x80) ? '-' : '+',
1288 (data[34] >> 4) & 0x07, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001289 break;
1290 }
1291
1292 switch (type) {
1293 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001294 printf ("Data signal input hold 0.%d%d ns\n",
1295 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001296 break;
1297 default:
Larry Johnson632de062008-01-11 23:26:18 -05001298 printf ("Data signal input hold %c%d.%d ns\n",
1299 (data[35] & 0x80) ? '-' : '+',
1300 (data[35] >> 4) & 0x07, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001301 break;
1302 }
1303
wdenk4b9206e2004-03-23 22:14:11 +00001304 puts ("Manufacturer's JEDEC ID ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001305 for (j = 64; j <= 71; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001306 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001307 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001308 printf ("Manufacturing Location %02X\n", data[72]);
wdenk4b9206e2004-03-23 22:14:11 +00001309 puts ("Manufacturer's Part Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001310 for (j = 73; j <= 90; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001311 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001312 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001313 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1314 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
wdenk4b9206e2004-03-23 22:14:11 +00001315 puts ("Assembly Serial Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001316 for (j = 95; j <= 98; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001317 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001318 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001319
Larry Johnson0df6b842008-01-10 22:23:39 -05001320 if (DDR2 != type) {
Larry Johnson632de062008-01-11 23:26:18 -05001321 printf ("Speed rating PC%d\n",
1322 data[126] == 0x66 ? 66 : data[126]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001323 }
wdenk81a88242002-10-26 15:22:42 +00001324 return 0;
1325}
Jon Loeliger90253172007-07-10 11:02:44 -05001326#endif
wdenk81a88242002-10-26 15:22:42 +00001327
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001328/*
1329 * Syntax:
1330 * i2c edid {i2c_chip}
1331 */
1332#if defined(CONFIG_I2C_EDID)
1333int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1334{
1335 u_char chip;
1336 struct edid1_info edid;
1337
1338 if (argc < 2) {
1339 cmd_usage(cmdtp);
1340 return 1;
1341 }
1342
1343 chip = simple_strtoul(argv[1], NULL, 16);
1344 if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
1345 puts("Error reading EDID content.\n");
1346 return 1;
1347 }
1348
1349 if (edid_check_info(&edid)) {
1350 puts("Content isn't valid EDID.\n");
1351 return 1;
1352 }
1353
1354 edid_print_info(&edid);
1355 return 0;
1356
1357}
1358#endif /* CONFIG_I2C_EDID */
1359
Heiko Schocher67b23a32008-10-15 09:39:47 +02001360#if defined(CONFIG_I2C_MUX)
Marek Vasut06afa382012-11-12 14:34:27 +00001361/**
1362 * do_i2c_add_bus() - Handle the "i2c bus" command-line command
1363 * @cmdtp: Command data struct pointer
1364 * @flag: Command flag
1365 * @argc: Command-line argument count
1366 * @argv: Array of command-line arguments
1367 *
1368 * Returns zero always.
1369 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001370static int do_i2c_add_bus(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher67b23a32008-10-15 09:39:47 +02001371{
1372 int ret=0;
1373
1374 if (argc == 1) {
1375 /* show all busses */
1376 I2C_MUX *mux;
1377 I2C_MUX_DEVICE *device = i2c_mux_devices;
1378
1379 printf ("Busses reached over muxes:\n");
1380 while (device != NULL) {
1381 printf ("Bus ID: %x\n", device->busid);
1382 printf (" reached over Mux(es):\n");
1383 mux = device->mux;
1384 while (mux != NULL) {
1385 printf (" %s@%x ch: %x\n", mux->name, mux->chip, mux->channel);
1386 mux = mux->next;
1387 }
1388 device = device->next;
1389 }
1390 } else {
Wolfgang Denke8260cb2011-11-04 15:55:54 +00001391 (void)i2c_mux_ident_muxstring ((uchar *)argv[1]);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001392 ret = 0;
1393 }
1394 return ret;
1395}
1396#endif /* CONFIG_I2C_MUX */
1397
Ben Warrenbb99ad62006-09-07 16:50:54 -04001398#if defined(CONFIG_I2C_MULTI_BUS)
Marek Vasut06afa382012-11-12 14:34:27 +00001399/**
1400 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1401 * @cmdtp: Command data struct pointer
1402 * @flag: Command flag
1403 * @argc: Command-line argument count
1404 * @argv: Array of command-line arguments
1405 *
1406 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1407 * on error.
1408 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001409static int do_i2c_bus_num(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001410{
1411 int bus_idx, ret=0;
1412
Timur Tabie857a5b2006-11-28 12:09:35 -06001413 if (argc == 1)
1414 /* querying current setting */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001415 printf("Current bus is %d\n", i2c_get_bus_num());
Timur Tabie857a5b2006-11-28 12:09:35 -06001416 else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001417 bus_idx = simple_strtoul(argv[1], NULL, 10);
1418 printf("Setting bus to %d\n", bus_idx);
1419 ret = i2c_set_bus_num(bus_idx);
Timur Tabie857a5b2006-11-28 12:09:35 -06001420 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001421 printf("Failure changing bus number (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001422 }
1423 return ret;
1424}
1425#endif /* CONFIG_I2C_MULTI_BUS */
1426
Marek Vasut06afa382012-11-12 14:34:27 +00001427/**
1428 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1429 * @cmdtp: Command data struct pointer
1430 * @flag: Command flag
1431 * @argc: Command-line argument count
1432 * @argv: Array of command-line arguments
1433 *
1434 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1435 * on error.
1436 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001437static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001438{
1439 int speed, ret=0;
1440
Timur Tabie857a5b2006-11-28 12:09:35 -06001441 if (argc == 1)
1442 /* querying current speed */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001443 printf("Current bus speed=%d\n", i2c_get_bus_speed());
Timur Tabie857a5b2006-11-28 12:09:35 -06001444 else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001445 speed = simple_strtoul(argv[1], NULL, 10);
1446 printf("Setting bus speed to %d Hz\n", speed);
1447 ret = i2c_set_bus_speed(speed);
Timur Tabie857a5b2006-11-28 12:09:35 -06001448 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001449 printf("Failure changing bus speed (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001450 }
1451 return ret;
1452}
1453
Marek Vasut06afa382012-11-12 14:34:27 +00001454/**
1455 * do_i2c_mm() - Handle the "i2c mm" command-line command
1456 * @cmdtp: Command data struct pointer
1457 * @flag: Command flag
1458 * @argc: Command-line argument count
1459 * @argv: Array of command-line arguments
1460 *
1461 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1462 * on error.
1463 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001464static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001465{
1466 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1467}
1468
Marek Vasut06afa382012-11-12 14:34:27 +00001469/**
1470 * do_i2c_nm() - Handle the "i2c nm" command-line command
1471 * @cmdtp: Command data struct pointer
1472 * @flag: Command flag
1473 * @argc: Command-line argument count
1474 * @argv: Array of command-line arguments
1475 *
1476 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1477 * on error.
1478 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001479static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001480{
1481 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1482}
1483
Marek Vasut06afa382012-11-12 14:34:27 +00001484/**
1485 * do_i2c_reset() - Handle the "i2c reset" command-line command
1486 * @cmdtp: Command data struct pointer
1487 * @flag: Command flag
1488 * @argc: Command-line argument count
1489 * @argv: Array of command-line arguments
1490 *
1491 * Returns zero always.
1492 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001493static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001494{
1495 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
1496 return 0;
1497}
1498
1499static cmd_tbl_t cmd_i2c_sub[] = {
1500#if defined(CONFIG_I2C_MUX)
1501 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_add_bus, "", ""),
1502#endif /* CONFIG_I2C_MUX */
1503 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
1504#if defined(CONFIG_I2C_MULTI_BUS)
1505 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1506#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001507#if defined(CONFIG_I2C_EDID)
1508 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1509#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001510 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1511 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1512 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1513 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1514 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1515 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001516 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
York Sunff5d2dc2012-09-16 08:02:30 +00001517 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001518 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1519#if defined(CONFIG_CMD_SDRAM)
1520 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1521#endif
1522 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1523};
1524
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001525#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001526void i2c_reloc(void) {
1527 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1528}
1529#endif
1530
Marek Vasut06afa382012-11-12 14:34:27 +00001531/**
1532 * do_i2c() - Handle the "i2c" command-line command
1533 * @cmdtp: Command data struct pointer
1534 * @flag: Command flag
1535 * @argc: Command-line argument count
1536 * @argv: Array of command-line arguments
1537 *
1538 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1539 * on error.
1540 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001541static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001542{
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001543 cmd_tbl_t *c;
1544
Heiko Schocher4444b222010-09-17 13:10:35 +02001545 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001546 return CMD_RET_USAGE;
Heiko Schocher4444b222010-09-17 13:10:35 +02001547
Peter Tysere96ad5d2009-04-18 22:34:04 -05001548 /* Strip off leading 'i2c' command argument */
1549 argc--;
1550 argv++;
1551
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001552 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1553
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001554 if (c)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001555 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001556 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00001557 return CMD_RET_USAGE;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001558}
wdenk8bde7f72003-06-27 21:31:46 +00001559
1560/***************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00001561#ifdef CONFIG_SYS_LONGHELP
1562static char i2c_help_text[] =
Peter Tyser9166b772009-04-18 22:34:06 -05001563#if defined(CONFIG_I2C_MUX)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001564 "bus [muxtype:muxaddr:muxchannel] - add a new bus reached over muxes\ni2c "
Peter Tyser9166b772009-04-18 22:34:06 -05001565#endif /* CONFIG_I2C_MUX */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001566 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001567#if defined(CONFIG_I2C_MULTI_BUS)
Peter Tyser9bc2e4e2008-10-01 12:25:04 -05001568 "i2c dev [dev] - show or set current I2C bus\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001569#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001570#if defined(CONFIG_I2C_EDID)
1571 "i2c edid chip - print EDID configuration information\n"
1572#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001573 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001574 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1575 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1576 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1577 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
Eric Nelson54b99e52012-09-23 10:12:56 +00001578 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001579 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
York Sunff5d2dc2012-09-16 08:02:30 +00001580 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
Heiko Schochere43a27c2008-10-15 09:33:30 +02001581 "i2c reset - re-init the I2C Controller\n"
Jon Loeligerc76fe472007-07-08 18:02:23 -05001582#if defined(CONFIG_CMD_SDRAM)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001583 "i2c sdram chip - print SDRAM configuration information\n"
Jon Loeliger90253172007-07-10 11:02:44 -05001584#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001585 "i2c speed [speed] - show or set I2C bus speed";
1586#endif
1587
1588U_BOOT_CMD(
1589 i2c, 6, 1, do_i2c,
1590 "I2C sub-system",
1591 i2c_help_text
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001592);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001593
1594#if defined(CONFIG_I2C_MUX)
Frans Meulenbroeksfd03ea82010-03-26 09:46:42 +01001595static int i2c_mux_add_device(I2C_MUX_DEVICE *dev)
Heiko Schocher67b23a32008-10-15 09:39:47 +02001596{
1597 I2C_MUX_DEVICE *devtmp = i2c_mux_devices;
1598
1599 if (i2c_mux_devices == NULL) {
1600 i2c_mux_devices = dev;
1601 return 0;
1602 }
1603 while (devtmp->next != NULL)
1604 devtmp = devtmp->next;
1605
1606 devtmp->next = dev;
1607 return 0;
1608}
1609
1610I2C_MUX_DEVICE *i2c_mux_search_device(int id)
1611{
1612 I2C_MUX_DEVICE *device = i2c_mux_devices;
1613
1614 while (device != NULL) {
1615 if (device->busid == id)
1616 return device;
1617 device = device->next;
1618 }
1619 return NULL;
1620}
1621
1622/* searches in the buf from *pos the next ':'.
1623 * returns:
1624 * 0 if found (with *pos = where)
1625 * < 0 if an error occured
1626 * > 0 if the end of buf is reached
1627 */
1628static int i2c_mux_search_next (int *pos, uchar *buf, int len)
1629{
1630 while ((buf[*pos] != ':') && (*pos < len)) {
1631 *pos += 1;
1632 }
1633 if (*pos >= len)
1634 return 1;
1635 if (buf[*pos] != ':')
1636 return -1;
1637 return 0;
1638}
1639
1640static int i2c_mux_get_busid (void)
1641{
1642 int tmp = i2c_mux_busid;
1643
1644 i2c_mux_busid ++;
1645 return tmp;
1646}
1647
Michael Jonesf9a78b82011-07-14 22:09:28 +00001648/* Analyses a Muxstring and immediately sends the
1649 commands to the muxes. Runs from flash.
Heiko Schocher67b23a32008-10-15 09:39:47 +02001650 */
1651int i2c_mux_ident_muxstring_f (uchar *buf)
1652{
1653 int pos = 0;
1654 int oldpos;
1655 int ret = 0;
1656 int len = strlen((char *)buf);
1657 int chip;
1658 uchar channel;
1659 int was = 0;
1660
1661 while (ret == 0) {
1662 oldpos = pos;
1663 /* search name */
1664 ret = i2c_mux_search_next(&pos, buf, len);
1665 if (ret != 0)
1666 printf ("ERROR\n");
1667 /* search address */
1668 pos ++;
1669 oldpos = pos;
1670 ret = i2c_mux_search_next(&pos, buf, len);
1671 if (ret != 0)
1672 printf ("ERROR\n");
1673 buf[pos] = 0;
1674 chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1675 buf[pos] = ':';
1676 /* search channel */
1677 pos ++;
1678 oldpos = pos;
1679 ret = i2c_mux_search_next(&pos, buf, len);
1680 if (ret < 0)
1681 printf ("ERROR\n");
1682 was = 0;
1683 if (buf[pos] != 0) {
1684 buf[pos] = 0;
1685 was = 1;
1686 }
1687 channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1688 if (was)
1689 buf[pos] = ':';
1690 if (i2c_write(chip, 0, 0, &channel, 1) != 0) {
1691 printf ("Error setting Mux: chip:%x channel: \
1692 %x\n", chip, channel);
1693 return -1;
1694 }
1695 pos ++;
1696 oldpos = pos;
1697
1698 }
Holger Brunck8ec038a2012-06-28 04:30:22 +00001699 i2c_init_board();
Heiko Schocher67b23a32008-10-15 09:39:47 +02001700
1701 return 0;
1702}
1703
1704/* Analyses a Muxstring and if this String is correct
1705 * adds a new I2C Bus.
1706 */
1707I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf)
1708{
1709 I2C_MUX_DEVICE *device;
1710 I2C_MUX *mux;
1711 int pos = 0;
1712 int oldpos;
1713 int ret = 0;
1714 int len = strlen((char *)buf);
1715 int was = 0;
1716
1717 device = (I2C_MUX_DEVICE *)malloc (sizeof(I2C_MUX_DEVICE));
1718 device->mux = NULL;
1719 device->busid = i2c_mux_get_busid ();
1720 device->next = NULL;
1721 while (ret == 0) {
1722 mux = (I2C_MUX *)malloc (sizeof(I2C_MUX));
1723 mux->next = NULL;
1724 /* search name of mux */
1725 oldpos = pos;
1726 ret = i2c_mux_search_next(&pos, buf, len);
1727 if (ret != 0)
1728 printf ("%s no name.\n", __FUNCTION__);
1729 mux->name = (char *)malloc (pos - oldpos + 1);
1730 memcpy (mux->name, &buf[oldpos], pos - oldpos);
1731 mux->name[pos - oldpos] = 0;
1732 /* search address */
1733 pos ++;
1734 oldpos = pos;
1735 ret = i2c_mux_search_next(&pos, buf, len);
1736 if (ret != 0)
1737 printf ("%s no mux address.\n", __FUNCTION__);
1738 buf[pos] = 0;
1739 mux->chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1740 buf[pos] = ':';
1741 /* search channel */
1742 pos ++;
1743 oldpos = pos;
1744 ret = i2c_mux_search_next(&pos, buf, len);
1745 if (ret < 0)
1746 printf ("%s no mux channel.\n", __FUNCTION__);
1747 was = 0;
1748 if (buf[pos] != 0) {
1749 buf[pos] = 0;
1750 was = 1;
1751 }
1752 mux->channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
1753 if (was)
1754 buf[pos] = ':';
1755 if (device->mux == NULL)
1756 device->mux = mux;
1757 else {
1758 I2C_MUX *muxtmp = device->mux;
1759 while (muxtmp->next != NULL) {
1760 muxtmp = muxtmp->next;
1761 }
1762 muxtmp->next = mux;
1763 }
1764 pos ++;
1765 oldpos = pos;
1766 }
1767 if (ret > 0) {
1768 /* Add Device */
1769 i2c_mux_add_device (device);
1770 return device;
1771 }
1772
1773 return NULL;
1774}
1775
1776int i2x_mux_select_mux(int bus)
1777{
1778 I2C_MUX_DEVICE *dev;
1779 I2C_MUX *mux;
1780
1781 if ((gd->flags & GD_FLG_RELOC) != GD_FLG_RELOC) {
1782 /* select Default Mux Bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001783#if defined(CONFIG_SYS_I2C_IVM_BUS)
1784 i2c_mux_ident_muxstring_f ((uchar *)CONFIG_SYS_I2C_IVM_BUS);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001785#else
1786 {
1787 unsigned char *buf;
1788 buf = (unsigned char *) getenv("EEprom_ivm");
1789 if (buf != NULL)
1790 i2c_mux_ident_muxstring_f (buf);
1791 }
1792#endif
1793 return 0;
1794 }
1795 dev = i2c_mux_search_device(bus);
1796 if (dev == NULL)
1797 return -1;
1798
1799 mux = dev->mux;
1800 while (mux != NULL) {
Stefan Biglerc649dda2011-04-08 16:24:08 +02001801 /* do deblocking on each level of mux, before mux config */
1802 i2c_init_board();
Heiko Schocher67b23a32008-10-15 09:39:47 +02001803 if (i2c_write(mux->chip, 0, 0, &mux->channel, 1) != 0) {
1804 printf ("Error setting Mux: chip:%x channel: \
1805 %x\n", mux->chip, mux->channel);
1806 return -1;
1807 }
1808 mux = mux->next;
1809 }
Stefan Biglerc649dda2011-04-08 16:24:08 +02001810 /* do deblocking on each level of mux and after mux config */
1811 i2c_init_board();
Heiko Schocher67b23a32008-10-15 09:39:47 +02001812 return 0;
1813}
1814#endif /* CONFIG_I2C_MUX */