blob: 22db1bb47c137fd4fe08fc20e3e2f75fba72f16f [file] [log] [blame]
wdenk81a88242002-10-26 15:22:42 +00001/*
Heiko Schocher3f4978c2012-01-16 21:12:24 +00002 * (C) Copyright 2009
3 * Sergey Kubushyn, himself, ksi@koi8.net
4 *
5 * Changes for unified multibus/multiadapter I2C support.
6 *
wdenk81a88242002-10-26 15:22:42 +00007 * (C) Copyright 2001
8 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
9 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
wdenk81a88242002-10-26 15:22:42 +000011 */
12
13/*
14 * I2C Functions similar to the standard memory functions.
15 *
16 * There are several parameters in many of the commands that bear further
17 * explanations:
18 *
wdenk81a88242002-10-26 15:22:42 +000019 * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
20 * Each I2C chip on the bus has a unique address. On the I2C data bus,
21 * the address is the upper seven bits and the LSB is the "read/write"
22 * bit. Note that the {i2c_chip} address specified on the command
23 * line is not shifted up: e.g. a typical EEPROM memory chip may have
24 * an I2C address of 0x50, but the data put on the bus will be 0xA0
25 * for write and 0xA1 for read. This "non shifted" address notation
26 * matches at least half of the data sheets :-/.
27 *
28 * {addr} is the address (or offset) within the chip. Small memory
29 * chips have 8 bit addresses. Large memory chips have 16 bit
30 * addresses. Other memory chips have 9, 10, or 11 bit addresses.
31 * Many non-memory chips have multiple registers and {addr} is used
32 * as the register index. Some non-memory chips have only one register
33 * and therefore don't need any {addr} parameter.
34 *
35 * The default {addr} parameter is one byte (.1) which works well for
36 * memories and registers with 8 bits of address space.
37 *
38 * You can specify the length of the {addr} field with the optional .0,
39 * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
40 * manipulating a single register device which doesn't use an address
41 * field, use "0.0" for the address and the ".0" length field will
42 * suppress the address in the I2C data stream. This also works for
43 * successive reads using the I2C auto-incrementing memory pointer.
44 *
45 * If you are manipulating a large memory with 2-byte addresses, use
46 * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
47 *
48 * Then there are the unfortunate memory chips that spill the most
49 * significant 1, 2, or 3 bits of address into the chip address byte.
50 * This effectively makes one chip (logically) look like 2, 4, or
51 * 8 chips. This is handled (awkwardly) by #defining
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
wdenk81a88242002-10-26 15:22:42 +000053 * {addr} field (since .1 is the default, it doesn't actually have to
54 * be specified). Examples: given a memory chip at I2C chip address
55 * 0x50, the following would happen...
Peter Tyser0f89c542009-04-18 22:34:03 -050056 * i2c md 50 0 10 display 16 bytes starting at 0x000
wdenk81a88242002-10-26 15:22:42 +000057 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050058 * i2c md 50 100 10 display 16 bytes starting at 0x100
wdenk81a88242002-10-26 15:22:42 +000059 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050060 * i2c md 50 210 10 display 16 bytes starting at 0x210
wdenk81a88242002-10-26 15:22:42 +000061 * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
62 * This is awfully ugly. It would be nice if someone would think up
63 * a better way of handling this.
64 *
65 * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
66 */
67
68#include <common.h>
Simon Glass0098e172014-04-10 20:01:30 -060069#include <bootretry.h>
Simon Glass18d66532014-04-10 20:01:25 -060070#include <cli.h>
wdenk81a88242002-10-26 15:22:42 +000071#include <command.h>
Simon Glass63656b72014-12-10 08:55:48 -070072#include <dm.h>
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +000073#include <edid.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020074#include <environment.h>
Simon Glass63656b72014-12-10 08:55:48 -070075#include <errno.h>
wdenk81a88242002-10-26 15:22:42 +000076#include <i2c.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020077#include <malloc.h>
wdenk81a88242002-10-26 15:22:42 +000078#include <asm/byteorder.h>
Marek Vasut2515d842012-11-12 14:34:25 +000079#include <linux/compiler.h>
wdenk81a88242002-10-26 15:22:42 +000080
Heiko Schocher3f4978c2012-01-16 21:12:24 +000081DECLARE_GLOBAL_DATA_PTR;
82
wdenk81a88242002-10-26 15:22:42 +000083/* Display values from last command.
84 * Memory modify remembered values are different from display memory.
85 */
86static uchar i2c_dp_last_chip;
87static uint i2c_dp_last_addr;
88static uint i2c_dp_last_alen;
89static uint i2c_dp_last_length = 0x10;
90
91static uchar i2c_mm_last_chip;
92static uint i2c_mm_last_addr;
93static uint i2c_mm_last_alen;
94
Ben Warrenbb99ad62006-09-07 16:50:54 -040095/* If only one I2C bus is present, the list of devices to ignore when
96 * the probe command is issued is represented by a 1D array of addresses.
97 * When multiple buses are present, the list is an array of bus-address
98 * pairs. The following macros take care of this */
99
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200100#if defined(CONFIG_SYS_I2C_NOPROBES)
Heiko Schocher9a2accb2012-10-25 10:32:14 +0200101#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
Ben Warrenbb99ad62006-09-07 16:50:54 -0400102static struct
103{
104 uchar bus;
105 uchar addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200106} i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400107#define GET_BUS_NUM i2c_get_bus_num()
108#define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
109#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
110#define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
111#else /* single bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200112static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400113#define GET_BUS_NUM 0
114#define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
115#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
116#define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000117#endif /* defined(CONFIG_SYS_I2C) */
Heiko Schocher67b23a32008-10-15 09:39:47 +0200118#endif
119
Frans Meulenbroeksa266fe92010-03-26 09:46:40 +0100120#define DISP_LINE_LEN 16
121
Simon Glass63656b72014-12-10 08:55:48 -0700122/*
123 * Default for driver model is to use the chip's existing address length.
124 * For legacy code, this is not stored, so we need to use a suitable
125 * default.
126 */
127#ifdef CONFIG_DM_I2C
128#define DEFAULT_ADDR_LEN (-1)
129#else
130#define DEFAULT_ADDR_LEN 1
131#endif
132
133#ifdef CONFIG_DM_I2C
134static struct udevice *i2c_cur_bus;
135
136static int i2c_set_bus_num(unsigned int busnum)
137{
138 struct udevice *bus;
139 int ret;
140
141 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
142 if (ret) {
143 debug("%s: No bus %d\n", __func__, busnum);
144 return ret;
145 }
146 i2c_cur_bus = bus;
147
148 return 0;
149}
150
151static int i2c_get_cur_bus(struct udevice **busp)
152{
153 if (!i2c_cur_bus) {
154 puts("No I2C bus selected\n");
155 return -ENODEV;
156 }
157 *busp = i2c_cur_bus;
158
159 return 0;
160}
161
162static int i2c_get_cur_bus_chip(uint chip_addr, struct udevice **devp)
163{
164 struct udevice *bus;
165 int ret;
166
167 ret = i2c_get_cur_bus(&bus);
168 if (ret)
169 return ret;
170
171 return i2c_get_chip(bus, chip_addr, devp);
172}
173
174#endif
175
Marek Vasut06afa382012-11-12 14:34:27 +0000176/**
177 * i2c_init_board() - Board-specific I2C bus init
178 *
179 * This function is the default no-op implementation of I2C bus
180 * initialization. This function can be overriden by board-specific
181 * implementation if needed.
182 */
Marek Vasut2515d842012-11-12 14:34:25 +0000183__weak
184void i2c_init_board(void)
Stefan Biglerc649dda2011-04-08 16:24:08 +0200185{
Stefan Biglerc649dda2011-04-08 16:24:08 +0200186}
Stefan Biglerc649dda2011-04-08 16:24:08 +0200187
Peter Tyser655b34a2009-04-18 22:34:01 -0500188/* TODO: Implement architecture-specific get/set functions */
Marek Vasut06afa382012-11-12 14:34:27 +0000189
190/**
191 * i2c_get_bus_speed() - Return I2C bus speed
192 *
193 * This function is the default implementation of function for retrieveing
194 * the current I2C bus speed in Hz.
195 *
196 * A driver implementing runtime switching of I2C bus speed must override
197 * this function to report the speed correctly. Simple or legacy drivers
198 * can use this fallback.
199 *
200 * Returns I2C bus speed in Hz.
201 */
Simon Glass63656b72014-12-10 08:55:48 -0700202#if !defined(CONFIG_SYS_I2C) && !defined(CONFIG_DM_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000203/*
204 * TODO: Implement architecture-specific get/set functions
205 * Should go away, if we switched completely to new multibus support
206 */
Marek Vasut2515d842012-11-12 14:34:25 +0000207__weak
208unsigned int i2c_get_bus_speed(void)
Peter Tyser655b34a2009-04-18 22:34:01 -0500209{
210 return CONFIG_SYS_I2C_SPEED;
211}
Peter Tyser655b34a2009-04-18 22:34:01 -0500212
Marek Vasut06afa382012-11-12 14:34:27 +0000213/**
214 * i2c_set_bus_speed() - Configure I2C bus speed
215 * @speed: Newly set speed of the I2C bus in Hz
216 *
217 * This function is the default implementation of function for setting
218 * the I2C bus speed in Hz.
219 *
220 * A driver implementing runtime switching of I2C bus speed must override
221 * this function to report the speed correctly. Simple or legacy drivers
222 * can use this fallback.
223 *
224 * Returns zero on success, negative value on error.
225 */
Marek Vasut2515d842012-11-12 14:34:25 +0000226__weak
227int i2c_set_bus_speed(unsigned int speed)
Peter Tyser655b34a2009-04-18 22:34:01 -0500228{
229 if (speed != CONFIG_SYS_I2C_SPEED)
230 return -1;
231
232 return 0;
233}
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000234#endif
Peter Tyser655b34a2009-04-18 22:34:01 -0500235
Marek Vasut06afa382012-11-12 14:34:27 +0000236/**
237 * get_alen() - Small parser helper function to get address length
238 *
239 * Returns the address length.
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100240 */
Simon Glass63656b72014-12-10 08:55:48 -0700241static uint get_alen(char *arg, int default_len)
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100242{
243 int j;
244 int alen;
245
Simon Glass63656b72014-12-10 08:55:48 -0700246 alen = default_len;
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100247 for (j = 0; j < 8; j++) {
248 if (arg[j] == '.') {
249 alen = arg[j+1] - '0';
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100250 break;
251 } else if (arg[j] == '\0')
252 break;
253 }
254 return alen;
255}
256
Simon Glassc1a6f372014-11-11 10:46:17 -0700257enum i2c_err_op {
258 I2C_ERR_READ,
259 I2C_ERR_WRITE,
260};
261
262static int i2c_report_err(int ret, enum i2c_err_op op)
263{
264 printf("Error %s the chip: %d\n",
265 op == I2C_ERR_READ ? "reading" : "writing", ret);
266
267 return CMD_RET_FAILURE;
268}
269
Marek Vasut06afa382012-11-12 14:34:27 +0000270/**
271 * do_i2c_read() - Handle the "i2c read" command-line command
272 * @cmdtp: Command data struct pointer
273 * @flag: Command flag
274 * @argc: Command-line argument count
275 * @argv: Array of command-line arguments
276 *
277 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
278 * on error.
279 *
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100280 * Syntax:
281 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
282 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200283static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100284{
285 u_char chip;
Simon Glass63656b72014-12-10 08:55:48 -0700286 uint devaddr, length;
287 int alen;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100288 u_char *memaddr;
Simon Glass63656b72014-12-10 08:55:48 -0700289 int ret;
290#ifdef CONFIG_DM_I2C
291 struct udevice *dev;
292#endif
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100293
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200294 if (argc != 5)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000295 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100296
297 /*
298 * I2C chip address
299 */
300 chip = simple_strtoul(argv[1], NULL, 16);
301
302 /*
303 * I2C data address within the chip. This can be 1 or
304 * 2 bytes long. Some day it might be 3 bytes long :-).
305 */
306 devaddr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700307 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200308 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000309 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100310
311 /*
312 * Length is the number of objects, not number of bytes.
313 */
314 length = simple_strtoul(argv[3], NULL, 16);
315
316 /*
317 * memaddr is the address where to store things in memory
318 */
319 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
320
Simon Glass63656b72014-12-10 08:55:48 -0700321#ifdef CONFIG_DM_I2C
322 ret = i2c_get_cur_bus_chip(chip, &dev);
323 if (!ret && alen != -1)
324 ret = i2c_set_chip_offset_len(dev, alen);
325 if (!ret)
326 ret = i2c_read(dev, devaddr, memaddr, length);
327#else
328 ret = i2c_read(chip, devaddr, alen, memaddr, length);
329#endif
330 if (ret)
331 return i2c_report_err(ret, I2C_ERR_READ);
332
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100333 return 0;
334}
335
York Sunff5d2dc2012-09-16 08:02:30 +0000336static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
337{
338 u_char chip;
Simon Glass63656b72014-12-10 08:55:48 -0700339 uint devaddr, length;
340 int alen;
York Sunff5d2dc2012-09-16 08:02:30 +0000341 u_char *memaddr;
Simon Glass63656b72014-12-10 08:55:48 -0700342 int ret;
343#ifdef CONFIG_DM_I2C
344 struct udevice *dev;
345#endif
York Sunff5d2dc2012-09-16 08:02:30 +0000346
347 if (argc != 5)
348 return cmd_usage(cmdtp);
349
350 /*
351 * memaddr is the address where to store things in memory
352 */
353 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
354
355 /*
356 * I2C chip address
357 */
358 chip = simple_strtoul(argv[2], NULL, 16);
359
360 /*
361 * I2C data address within the chip. This can be 1 or
362 * 2 bytes long. Some day it might be 3 bytes long :-).
363 */
364 devaddr = simple_strtoul(argv[3], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700365 alen = get_alen(argv[3], DEFAULT_ADDR_LEN);
York Sunff5d2dc2012-09-16 08:02:30 +0000366 if (alen > 3)
367 return cmd_usage(cmdtp);
368
369 /*
370 * Length is the number of objects, not number of bytes.
371 */
372 length = simple_strtoul(argv[4], NULL, 16);
373
Simon Glass63656b72014-12-10 08:55:48 -0700374#ifdef CONFIG_DM_I2C
375 ret = i2c_get_cur_bus_chip(chip, &dev);
376 if (!ret && alen != -1)
377 ret = i2c_set_chip_offset_len(dev, alen);
378 if (ret)
379 return i2c_report_err(ret, I2C_ERR_WRITE);
380#endif
381
York Sunff5d2dc2012-09-16 08:02:30 +0000382 while (length-- > 0) {
Simon Glass63656b72014-12-10 08:55:48 -0700383#ifdef CONFIG_DM_I2C
384 ret = i2c_write(dev, devaddr++, memaddr++, 1);
385#else
386 ret = i2c_write(chip, devaddr++, alen, memaddr++, 1);
387#endif
388 if (ret)
389 return i2c_report_err(ret, I2C_ERR_WRITE);
York Sunff5d2dc2012-09-16 08:02:30 +0000390/*
391 * No write delay with FRAM devices.
392 */
393#if !defined(CONFIG_SYS_I2C_FRAM)
394 udelay(11000);
395#endif
396 }
397 return 0;
398}
399
Simon Glass63656b72014-12-10 08:55:48 -0700400#ifdef CONFIG_DM_I2C
401static int do_i2c_flags(cmd_tbl_t *cmdtp, int flag, int argc,
402 char *const argv[])
403{
404 struct udevice *dev;
405 uint flags;
406 int chip;
407 int ret;
408
409 if (argc < 2)
410 return CMD_RET_USAGE;
411
412 chip = simple_strtoul(argv[1], NULL, 16);
413 ret = i2c_get_cur_bus_chip(chip, &dev);
414 if (ret)
415 return i2c_report_err(ret, I2C_ERR_READ);
416
417 if (argc > 2) {
418 flags = simple_strtoul(argv[2], NULL, 16);
419 ret = i2c_set_chip_flags(dev, flags);
420 } else {
421 ret = i2c_get_chip_flags(dev, &flags);
422 if (!ret)
423 printf("%x\n", flags);
424 }
425 if (ret)
426 return i2c_report_err(ret, I2C_ERR_READ);
427
428 return 0;
429}
430#endif
431
Marek Vasut06afa382012-11-12 14:34:27 +0000432/**
433 * do_i2c_md() - Handle the "i2c md" command-line command
434 * @cmdtp: Command data struct pointer
435 * @flag: Command flag
436 * @argc: Command-line argument count
437 * @argv: Array of command-line arguments
438 *
439 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
440 * on error.
441 *
Frans Meulenbroeks4a8cf332010-03-26 09:46:39 +0100442 * Syntax:
443 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
444 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200445static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000446{
447 u_char chip;
Simon Glass63656b72014-12-10 08:55:48 -0700448 uint addr, length;
449 int alen;
wdenk81a88242002-10-26 15:22:42 +0000450 int j, nbytes, linebytes;
Simon Glass63656b72014-12-10 08:55:48 -0700451 int ret;
452#ifdef CONFIG_DM_I2C
453 struct udevice *dev;
454#endif
wdenk81a88242002-10-26 15:22:42 +0000455
456 /* We use the last specified parameters, unless new ones are
457 * entered.
458 */
459 chip = i2c_dp_last_chip;
460 addr = i2c_dp_last_addr;
461 alen = i2c_dp_last_alen;
462 length = i2c_dp_last_length;
463
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200464 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000465 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000466
467 if ((flag & CMD_FLAG_REPEAT) == 0) {
468 /*
469 * New command specified.
470 */
wdenk81a88242002-10-26 15:22:42 +0000471
472 /*
473 * I2C chip address
474 */
475 chip = simple_strtoul(argv[1], NULL, 16);
476
477 /*
478 * I2C data address within the chip. This can be 1 or
479 * 2 bytes long. Some day it might be 3 bytes long :-).
480 */
481 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700482 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200483 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000484 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000485
486 /*
487 * If another parameter, it is the length to display.
488 * Length is the number of objects, not number of bytes.
489 */
490 if (argc > 3)
491 length = simple_strtoul(argv[3], NULL, 16);
492 }
493
Simon Glass63656b72014-12-10 08:55:48 -0700494#ifdef CONFIG_DM_I2C
495 ret = i2c_get_cur_bus_chip(chip, &dev);
496 if (!ret && alen != -1)
497 ret = i2c_set_chip_offset_len(dev, alen);
498 if (ret)
499 return i2c_report_err(ret, I2C_ERR_READ);
500#endif
501
wdenk81a88242002-10-26 15:22:42 +0000502 /*
503 * Print the lines.
504 *
505 * We buffer all read data, so we can make sure data is read only
506 * once.
507 */
508 nbytes = length;
509 do {
510 unsigned char linebuf[DISP_LINE_LEN];
511 unsigned char *cp;
512
513 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
514
Simon Glass63656b72014-12-10 08:55:48 -0700515#ifdef CONFIG_DM_I2C
516 ret = i2c_read(dev, addr, linebuf, linebytes);
517#else
518 ret = i2c_read(chip, addr, alen, linebuf, linebytes);
519#endif
520 if (ret)
521 i2c_report_err(ret, I2C_ERR_READ);
Timur Tabie857a5b2006-11-28 12:09:35 -0600522 else {
wdenk81a88242002-10-26 15:22:42 +0000523 printf("%04x:", addr);
524 cp = linebuf;
525 for (j=0; j<linebytes; j++) {
526 printf(" %02x", *cp++);
527 addr++;
528 }
wdenk4b9206e2004-03-23 22:14:11 +0000529 puts (" ");
wdenk81a88242002-10-26 15:22:42 +0000530 cp = linebuf;
531 for (j=0; j<linebytes; j++) {
532 if ((*cp < 0x20) || (*cp > 0x7e))
wdenk4b9206e2004-03-23 22:14:11 +0000533 puts (".");
wdenk81a88242002-10-26 15:22:42 +0000534 else
535 printf("%c", *cp);
536 cp++;
537 }
wdenk4b9206e2004-03-23 22:14:11 +0000538 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000539 }
540 nbytes -= linebytes;
541 } while (nbytes > 0);
542
543 i2c_dp_last_chip = chip;
544 i2c_dp_last_addr = addr;
545 i2c_dp_last_alen = alen;
546 i2c_dp_last_length = length;
547
548 return 0;
549}
550
Marek Vasut06afa382012-11-12 14:34:27 +0000551/**
552 * do_i2c_mw() - Handle the "i2c mw" command-line command
553 * @cmdtp: Command data struct pointer
554 * @flag: Command flag
555 * @argc: Command-line argument count
556 * @argv: Array of command-line arguments
557 *
558 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
559 * on error.
wdenk81a88242002-10-26 15:22:42 +0000560 *
561 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500562 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
wdenk81a88242002-10-26 15:22:42 +0000563 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200564static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000565{
566 uchar chip;
567 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700568 int alen;
wdenk81a88242002-10-26 15:22:42 +0000569 uchar byte;
570 int count;
Simon Glass63656b72014-12-10 08:55:48 -0700571 int ret;
572#ifdef CONFIG_DM_I2C
573 struct udevice *dev;
574#endif
wdenk81a88242002-10-26 15:22:42 +0000575
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200576 if ((argc < 4) || (argc > 5))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000577 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000578
579 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200580 * Chip is always specified.
581 */
wdenk81a88242002-10-26 15:22:42 +0000582 chip = simple_strtoul(argv[1], NULL, 16);
583
584 /*
585 * Address is always specified.
586 */
587 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700588 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200589 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000590 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000591
Simon Glass63656b72014-12-10 08:55:48 -0700592#ifdef CONFIG_DM_I2C
593 ret = i2c_get_cur_bus_chip(chip, &dev);
594 if (!ret && alen != -1)
595 ret = i2c_set_chip_offset_len(dev, alen);
596 if (ret)
597 return i2c_report_err(ret, I2C_ERR_WRITE);
598#endif
wdenk81a88242002-10-26 15:22:42 +0000599 /*
600 * Value to write is always specified.
601 */
602 byte = simple_strtoul(argv[3], NULL, 16);
603
604 /*
605 * Optional count
606 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600607 if (argc == 5)
wdenk81a88242002-10-26 15:22:42 +0000608 count = simple_strtoul(argv[4], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600609 else
wdenk81a88242002-10-26 15:22:42 +0000610 count = 1;
wdenk81a88242002-10-26 15:22:42 +0000611
612 while (count-- > 0) {
Simon Glass63656b72014-12-10 08:55:48 -0700613#ifdef CONFIG_DM_I2C
614 ret = i2c_write(dev, addr++, &byte, 1);
615#else
616 ret = i2c_write(chip, addr++, alen, &byte, 1);
617#endif
618 if (ret)
619 i2c_report_err(ret, I2C_ERR_WRITE);
wdenk81a88242002-10-26 15:22:42 +0000620 /*
621 * Wait for the write to complete. The write can take
622 * up to 10mSec (we allow a little more time).
wdenk81a88242002-10-26 15:22:42 +0000623 */
d4f5c722005-08-12 21:16:13 +0200624/*
625 * No write delay with FRAM devices.
626 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200627#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk81a88242002-10-26 15:22:42 +0000628 udelay(11000);
d4f5c722005-08-12 21:16:13 +0200629#endif
wdenk81a88242002-10-26 15:22:42 +0000630 }
631
Marek Vasut06afa382012-11-12 14:34:27 +0000632 return 0;
wdenk81a88242002-10-26 15:22:42 +0000633}
634
Marek Vasut06afa382012-11-12 14:34:27 +0000635/**
636 * do_i2c_crc() - Handle the "i2c crc32" command-line command
637 * @cmdtp: Command data struct pointer
638 * @flag: Command flag
639 * @argc: Command-line argument count
640 * @argv: Array of command-line arguments
641 *
642 * Calculate a CRC on memory
643 *
644 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
645 * on error.
wdenk81a88242002-10-26 15:22:42 +0000646 *
647 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500648 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
wdenk81a88242002-10-26 15:22:42 +0000649 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200650static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000651{
652 uchar chip;
653 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700654 int alen;
wdenk81a88242002-10-26 15:22:42 +0000655 int count;
656 uchar byte;
657 ulong crc;
658 ulong err;
Simon Glass63656b72014-12-10 08:55:48 -0700659 int ret = 0;
660#ifdef CONFIG_DM_I2C
661 struct udevice *dev;
662#endif
wdenk81a88242002-10-26 15:22:42 +0000663
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200664 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000665 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000666
667 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200668 * Chip is always specified.
669 */
wdenk81a88242002-10-26 15:22:42 +0000670 chip = simple_strtoul(argv[1], NULL, 16);
671
672 /*
673 * Address is always specified.
674 */
675 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700676 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200677 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000678 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000679
Simon Glass63656b72014-12-10 08:55:48 -0700680#ifdef CONFIG_DM_I2C
681 ret = i2c_get_cur_bus_chip(chip, &dev);
682 if (!ret && alen != -1)
683 ret = i2c_set_chip_offset_len(dev, alen);
684 if (ret)
685 return i2c_report_err(ret, I2C_ERR_READ);
686#endif
wdenk81a88242002-10-26 15:22:42 +0000687 /*
688 * Count is always specified
689 */
690 count = simple_strtoul(argv[3], NULL, 16);
691
692 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
693 /*
694 * CRC a byte at a time. This is going to be slooow, but hey, the
695 * memories are small and slow too so hopefully nobody notices.
696 */
697 crc = 0;
698 err = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600699 while (count-- > 0) {
Simon Glass63656b72014-12-10 08:55:48 -0700700#ifdef CONFIG_DM_I2C
701 ret = i2c_read(dev, addr, &byte, 1);
702#else
703 ret = i2c_read(chip, addr, alen, &byte, 1);
704#endif
705 if (ret)
wdenk81a88242002-10-26 15:22:42 +0000706 err++;
wdenk81a88242002-10-26 15:22:42 +0000707 crc = crc32 (crc, &byte, 1);
708 addr++;
709 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600710 if (err > 0)
Simon Glass63656b72014-12-10 08:55:48 -0700711 i2c_report_err(ret, I2C_ERR_READ);
Timur Tabie857a5b2006-11-28 12:09:35 -0600712 else
wdenk81a88242002-10-26 15:22:42 +0000713 printf ("%08lx\n", crc);
wdenk81a88242002-10-26 15:22:42 +0000714
715 return 0;
716}
717
Marek Vasut06afa382012-11-12 14:34:27 +0000718/**
719 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
720 * @cmdtp: Command data struct pointer
721 * @flag: Command flag
722 * @argc: Command-line argument count
723 * @argv: Array of command-line arguments
724 *
725 * Modify memory.
726 *
727 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
728 * on error.
wdenk81a88242002-10-26 15:22:42 +0000729 *
730 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500731 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
732 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
wdenk81a88242002-10-26 15:22:42 +0000733 */
wdenk81a88242002-10-26 15:22:42 +0000734static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200735mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000736{
737 uchar chip;
738 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700739 int alen;
wdenk81a88242002-10-26 15:22:42 +0000740 ulong data;
741 int size = 1;
742 int nbytes;
Simon Glass63656b72014-12-10 08:55:48 -0700743 int ret;
744#ifdef CONFIG_DM_I2C
745 struct udevice *dev;
746#endif
wdenk81a88242002-10-26 15:22:42 +0000747
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200748 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000749 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000750
Simon Glassb26440f2014-04-10 20:01:31 -0600751 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk81a88242002-10-26 15:22:42 +0000752 /*
753 * We use the last specified parameters, unless new ones are
754 * entered.
755 */
756 chip = i2c_mm_last_chip;
757 addr = i2c_mm_last_addr;
758 alen = i2c_mm_last_alen;
759
760 if ((flag & CMD_FLAG_REPEAT) == 0) {
761 /*
762 * New command specified. Check for a size specification.
763 * Defaults to byte if no or incorrect specification.
764 */
765 size = cmd_get_data_size(argv[0], 1);
766
767 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200768 * Chip is always specified.
769 */
wdenk81a88242002-10-26 15:22:42 +0000770 chip = simple_strtoul(argv[1], NULL, 16);
771
772 /*
773 * Address is always specified.
774 */
775 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700776 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200777 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000778 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000779 }
780
Simon Glass63656b72014-12-10 08:55:48 -0700781#ifdef CONFIG_DM_I2C
782 ret = i2c_get_cur_bus_chip(chip, &dev);
783 if (!ret && alen != -1)
784 ret = i2c_set_chip_offset_len(dev, alen);
785 if (ret)
786 return i2c_report_err(ret, I2C_ERR_WRITE);
787#endif
788
wdenk81a88242002-10-26 15:22:42 +0000789 /*
790 * Print the address, followed by value. Then accept input for
791 * the next value. A non-converted value exits.
792 */
793 do {
794 printf("%08lx:", addr);
Simon Glass63656b72014-12-10 08:55:48 -0700795#ifdef CONFIG_DM_I2C
796 ret = i2c_read(dev, addr, (uchar *)&data, size);
797#else
798 ret = i2c_read(chip, addr, alen, (uchar *)&data, size);
799#endif
800 if (ret)
801 i2c_report_err(ret, I2C_ERR_READ);
Timur Tabie857a5b2006-11-28 12:09:35 -0600802 else {
wdenk81a88242002-10-26 15:22:42 +0000803 data = cpu_to_be32(data);
Timur Tabie857a5b2006-11-28 12:09:35 -0600804 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000805 printf(" %02lx", (data >> 24) & 0x000000FF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600806 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000807 printf(" %04lx", (data >> 16) & 0x0000FFFF);
Timur Tabie857a5b2006-11-28 12:09:35 -0600808 else
wdenk81a88242002-10-26 15:22:42 +0000809 printf(" %08lx", data);
wdenk81a88242002-10-26 15:22:42 +0000810 }
811
Simon Glasse1bf8242014-04-10 20:01:27 -0600812 nbytes = cli_readline(" ? ");
wdenk81a88242002-10-26 15:22:42 +0000813 if (nbytes == 0) {
814 /*
815 * <CR> pressed as only input, don't modify current
816 * location and move to next.
817 */
818 if (incrflag)
819 addr += size;
820 nbytes = size;
Simon Glassb26440f2014-04-10 20:01:31 -0600821 /* good enough to not time out */
822 bootretry_reset_cmd_timeout();
wdenk81a88242002-10-26 15:22:42 +0000823 }
824#ifdef CONFIG_BOOT_RETRY_TIME
Timur Tabie857a5b2006-11-28 12:09:35 -0600825 else if (nbytes == -2)
wdenk81a88242002-10-26 15:22:42 +0000826 break; /* timed out, exit the command */
wdenk81a88242002-10-26 15:22:42 +0000827#endif
828 else {
829 char *endp;
830
831 data = simple_strtoul(console_buffer, &endp, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600832 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000833 data = data << 24;
Timur Tabie857a5b2006-11-28 12:09:35 -0600834 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000835 data = data << 16;
wdenk81a88242002-10-26 15:22:42 +0000836 data = be32_to_cpu(data);
837 nbytes = endp - console_buffer;
838 if (nbytes) {
wdenk81a88242002-10-26 15:22:42 +0000839 /*
840 * good enough to not time out
841 */
Simon Glassb26440f2014-04-10 20:01:31 -0600842 bootretry_reset_cmd_timeout();
Simon Glass63656b72014-12-10 08:55:48 -0700843#ifdef CONFIG_DM_I2C
844 ret = i2c_write(dev, addr, (uchar *)&data,
845 size);
846#else
847 ret = i2c_write(chip, addr, alen,
848 (uchar *)&data, size);
849#endif
850 if (ret)
851 i2c_report_err(ret, I2C_ERR_WRITE);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200852#ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
853 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk2535d602003-07-17 23:16:40 +0000854#endif
wdenk81a88242002-10-26 15:22:42 +0000855 if (incrflag)
856 addr += size;
857 }
858 }
859 } while (nbytes);
860
Peter Tyser08007072008-08-15 14:36:32 -0500861 i2c_mm_last_chip = chip;
862 i2c_mm_last_addr = addr;
863 i2c_mm_last_alen = alen;
wdenk81a88242002-10-26 15:22:42 +0000864
865 return 0;
866}
867
Marek Vasut06afa382012-11-12 14:34:27 +0000868/**
869 * do_i2c_probe() - Handle the "i2c probe" command-line command
870 * @cmdtp: Command data struct pointer
871 * @flag: Command flag
872 * @argc: Command-line argument count
873 * @argv: Array of command-line arguments
874 *
875 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
876 * on error.
877 *
wdenk81a88242002-10-26 15:22:42 +0000878 * Syntax:
Eric Nelson54b99e52012-09-23 10:12:56 +0000879 * i2c probe {addr}
880 *
881 * Returns zero (success) if one or more I2C devices was found
wdenk81a88242002-10-26 15:22:42 +0000882 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200883static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000884{
885 int j;
Eric Nelson54b99e52012-09-23 10:12:56 +0000886 int addr = -1;
887 int found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200888#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000889 int k, skip;
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000890 unsigned int bus = GET_BUS_NUM;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400891#endif /* NOPROBES */
Simon Glass63656b72014-12-10 08:55:48 -0700892 int ret;
893#ifdef CONFIG_DM_I2C
894 struct udevice *bus, *dev;
895
896 if (i2c_get_cur_bus(&bus))
897 return CMD_RET_FAILURE;
898#endif
wdenk81a88242002-10-26 15:22:42 +0000899
Eric Nelson54b99e52012-09-23 10:12:56 +0000900 if (argc == 2)
901 addr = simple_strtol(argv[1], 0, 16);
902
wdenk4b9206e2004-03-23 22:14:11 +0000903 puts ("Valid chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600904 for (j = 0; j < 128; j++) {
Eric Nelson54b99e52012-09-23 10:12:56 +0000905 if ((0 <= addr) && (j != addr))
906 continue;
907
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200908#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000909 skip = 0;
Axel Lincfb25cc2013-06-22 21:56:35 +0800910 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600911 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
wdenk81a88242002-10-26 15:22:42 +0000912 skip = 1;
913 break;
914 }
915 }
916 if (skip)
917 continue;
918#endif
Simon Glass63656b72014-12-10 08:55:48 -0700919#ifdef CONFIG_DM_I2C
920 ret = i2c_probe(bus, j, 0, &dev);
921#else
922 ret = i2c_probe(j);
923#endif
924 if (ret == 0) {
wdenk81a88242002-10-26 15:22:42 +0000925 printf(" %02X", j);
Eric Nelson54b99e52012-09-23 10:12:56 +0000926 found++;
927 }
wdenk81a88242002-10-26 15:22:42 +0000928 }
wdenk4b9206e2004-03-23 22:14:11 +0000929 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000930
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200931#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000932 puts ("Excluded chip addresses:");
Axel Lincfb25cc2013-06-22 21:56:35 +0800933 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600934 if (COMPARE_BUS(bus,k))
Ben Warrenbb99ad62006-09-07 16:50:54 -0400935 printf(" %02X", NO_PROBE_ADDR(k));
936 }
wdenk4b9206e2004-03-23 22:14:11 +0000937 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000938#endif
939
Eric Nelson54b99e52012-09-23 10:12:56 +0000940 return (0 == found);
wdenk81a88242002-10-26 15:22:42 +0000941}
942
Marek Vasut06afa382012-11-12 14:34:27 +0000943/**
944 * do_i2c_loop() - Handle the "i2c loop" command-line command
945 * @cmdtp: Command data struct pointer
946 * @flag: Command flag
947 * @argc: Command-line argument count
948 * @argv: Array of command-line arguments
949 *
950 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
951 * on error.
952 *
wdenk81a88242002-10-26 15:22:42 +0000953 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500954 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
wdenk81a88242002-10-26 15:22:42 +0000955 * {length} - Number of bytes to read
956 * {delay} - A DECIMAL number and defaults to 1000 uSec
957 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200958static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000959{
960 u_char chip;
Simon Glass63656b72014-12-10 08:55:48 -0700961 int alen;
wdenk81a88242002-10-26 15:22:42 +0000962 uint addr;
963 uint length;
964 u_char bytes[16];
965 int delay;
Simon Glass63656b72014-12-10 08:55:48 -0700966 int ret;
967#ifdef CONFIG_DM_I2C
968 struct udevice *dev;
969#endif
wdenk81a88242002-10-26 15:22:42 +0000970
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200971 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000972 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000973
974 /*
975 * Chip is always specified.
976 */
977 chip = simple_strtoul(argv[1], NULL, 16);
978
979 /*
980 * Address is always specified.
981 */
982 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700983 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200984 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000985 return CMD_RET_USAGE;
Simon Glass63656b72014-12-10 08:55:48 -0700986#ifdef CONFIG_DM_I2C
987 ret = i2c_get_cur_bus_chip(chip, &dev);
988 if (!ret && alen != -1)
989 ret = i2c_set_chip_offset_len(dev, alen);
990 if (ret)
991 return i2c_report_err(ret, I2C_ERR_WRITE);
992#endif
wdenk81a88242002-10-26 15:22:42 +0000993
994 /*
995 * Length is the number of objects, not number of bytes.
996 */
997 length = 1;
998 length = simple_strtoul(argv[3], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600999 if (length > sizeof(bytes))
wdenk81a88242002-10-26 15:22:42 +00001000 length = sizeof(bytes);
wdenk81a88242002-10-26 15:22:42 +00001001
1002 /*
1003 * The delay time (uSec) is optional.
1004 */
1005 delay = 1000;
Timur Tabie857a5b2006-11-28 12:09:35 -06001006 if (argc > 3)
wdenk81a88242002-10-26 15:22:42 +00001007 delay = simple_strtoul(argv[4], NULL, 10);
wdenk81a88242002-10-26 15:22:42 +00001008 /*
1009 * Run the loop...
1010 */
Timur Tabie857a5b2006-11-28 12:09:35 -06001011 while (1) {
Simon Glass63656b72014-12-10 08:55:48 -07001012#ifdef CONFIG_DM_I2C
1013 ret = i2c_read(dev, addr, bytes, length);
1014#else
1015 ret = i2c_read(chip, addr, alen, bytes, length);
1016#endif
1017 if (ret)
1018 i2c_report_err(ret, I2C_ERR_READ);
wdenk81a88242002-10-26 15:22:42 +00001019 udelay(delay);
1020 }
1021
1022 /* NOTREACHED */
1023 return 0;
1024}
1025
wdenk81a88242002-10-26 15:22:42 +00001026/*
1027 * The SDRAM command is separately configured because many
1028 * (most?) embedded boards don't use SDRAM DIMMs.
Marek Vasut06afa382012-11-12 14:34:27 +00001029 *
1030 * FIXME: Document and probably move elsewhere!
wdenk81a88242002-10-26 15:22:42 +00001031 */
Jon Loeligerc76fe472007-07-08 18:02:23 -05001032#if defined(CONFIG_CMD_SDRAM)
Larry Johnson632de062008-01-11 23:26:18 -05001033static void print_ddr2_tcyc (u_char const b)
1034{
1035 printf ("%d.", (b >> 4) & 0x0F);
1036 switch (b & 0x0F) {
1037 case 0x0:
1038 case 0x1:
1039 case 0x2:
1040 case 0x3:
1041 case 0x4:
1042 case 0x5:
1043 case 0x6:
1044 case 0x7:
1045 case 0x8:
1046 case 0x9:
1047 printf ("%d ns\n", b & 0x0F);
1048 break;
1049 case 0xA:
1050 puts ("25 ns\n");
1051 break;
1052 case 0xB:
1053 puts ("33 ns\n");
1054 break;
1055 case 0xC:
1056 puts ("66 ns\n");
1057 break;
1058 case 0xD:
1059 puts ("75 ns\n");
1060 break;
1061 default:
1062 puts ("?? ns\n");
1063 break;
1064 }
1065}
1066
1067static void decode_bits (u_char const b, char const *str[], int const do_once)
1068{
1069 u_char mask;
1070
1071 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
1072 if (b & mask) {
1073 puts (*str);
1074 if (do_once)
1075 return;
1076 }
1077 }
1078}
wdenk81a88242002-10-26 15:22:42 +00001079
1080/*
1081 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -05001082 * i2c sdram {i2c_chip}
wdenk81a88242002-10-26 15:22:42 +00001083 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001084static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +00001085{
Larry Johnson632de062008-01-11 23:26:18 -05001086 enum { unknown, EDO, SDRAM, DDR2 } type;
1087
wdenk81a88242002-10-26 15:22:42 +00001088 u_char chip;
1089 u_char data[128];
1090 u_char cksum;
1091 int j;
1092
Larry Johnson632de062008-01-11 23:26:18 -05001093 static const char *decode_CAS_DDR2[] = {
1094 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
1095 };
1096
1097 static const char *decode_CAS_default[] = {
1098 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
1099 };
1100
1101 static const char *decode_CS_WE_default[] = {
1102 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
1103 };
1104
1105 static const char *decode_byte21_default[] = {
1106 " TBD (bit 7)\n",
1107 " Redundant row address\n",
1108 " Differential clock input\n",
1109 " Registerd DQMB inputs\n",
1110 " Buffered DQMB inputs\n",
1111 " On-card PLL\n",
1112 " Registered address/control lines\n",
1113 " Buffered address/control lines\n"
1114 };
1115
1116 static const char *decode_byte22_DDR2[] = {
1117 " TBD (bit 7)\n",
1118 " TBD (bit 6)\n",
1119 " TBD (bit 5)\n",
1120 " TBD (bit 4)\n",
1121 " TBD (bit 3)\n",
1122 " Supports partial array self refresh\n",
1123 " Supports 50 ohm ODT\n",
1124 " Supports weak driver\n"
1125 };
1126
1127 static const char *decode_row_density_DDR2[] = {
1128 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
1129 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
1130 };
1131
1132 static const char *decode_row_density_default[] = {
1133 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
1134 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
1135 };
1136
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001137 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001138 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001139
wdenk81a88242002-10-26 15:22:42 +00001140 /*
1141 * Chip is always specified.
Larry Johnson632de062008-01-11 23:26:18 -05001142 */
1143 chip = simple_strtoul (argv[1], NULL, 16);
wdenk81a88242002-10-26 15:22:42 +00001144
Larry Johnson632de062008-01-11 23:26:18 -05001145 if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +00001146 puts ("No SDRAM Serial Presence Detect found.\n");
wdenk81a88242002-10-26 15:22:42 +00001147 return 1;
1148 }
1149
1150 cksum = 0;
1151 for (j = 0; j < 63; j++) {
1152 cksum += data[j];
1153 }
Timur Tabie857a5b2006-11-28 12:09:35 -06001154 if (cksum != data[63]) {
wdenk81a88242002-10-26 15:22:42 +00001155 printf ("WARNING: Configuration data checksum failure:\n"
Larry Johnson632de062008-01-11 23:26:18 -05001156 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
wdenk81a88242002-10-26 15:22:42 +00001157 }
Larry Johnson632de062008-01-11 23:26:18 -05001158 printf ("SPD data revision %d.%d\n",
wdenk81a88242002-10-26 15:22:42 +00001159 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
Larry Johnson632de062008-01-11 23:26:18 -05001160 printf ("Bytes used 0x%02X\n", data[0]);
1161 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
1162
wdenk4b9206e2004-03-23 22:14:11 +00001163 puts ("Memory type ");
Larry Johnson632de062008-01-11 23:26:18 -05001164 switch (data[2]) {
Larry Johnson0df6b842008-01-10 22:23:39 -05001165 case 2:
1166 type = EDO;
1167 puts ("EDO\n");
1168 break;
1169 case 4:
1170 type = SDRAM;
1171 puts ("SDRAM\n");
1172 break;
1173 case 8:
1174 type = DDR2;
1175 puts ("DDR2\n");
1176 break;
1177 default:
1178 type = unknown;
1179 puts ("unknown\n");
1180 break;
wdenk81a88242002-10-26 15:22:42 +00001181 }
Larry Johnson632de062008-01-11 23:26:18 -05001182
wdenk4b9206e2004-03-23 22:14:11 +00001183 puts ("Row address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001184 if ((data[3] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -05001185 printf ("%d\n", data[3] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -06001186 else
Larry Johnson632de062008-01-11 23:26:18 -05001187 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
1188
wdenk4b9206e2004-03-23 22:14:11 +00001189 puts ("Column address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001190 if ((data[4] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -05001191 printf ("%d\n", data[4] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -06001192 else
Larry Johnson632de062008-01-11 23:26:18 -05001193 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001194
1195 switch (type) {
1196 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001197 printf ("Number of ranks %d\n",
1198 (data[5] & 0x07) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001199 break;
1200 default:
Larry Johnson632de062008-01-11 23:26:18 -05001201 printf ("Module rows %d\n", data[5]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001202 break;
1203 }
1204
1205 switch (type) {
1206 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001207 printf ("Module data width %d bits\n", data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001208 break;
1209 default:
Larry Johnson632de062008-01-11 23:26:18 -05001210 printf ("Module data width %d bits\n",
1211 (data[7] << 8) | data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001212 break;
1213 }
1214
wdenk4b9206e2004-03-23 22:14:11 +00001215 puts ("Interface signal levels ");
wdenk81a88242002-10-26 15:22:42 +00001216 switch(data[8]) {
Larry Johnson0df6b842008-01-10 22:23:39 -05001217 case 0: puts ("TTL 5.0 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001218 case 1: puts ("LVTTL\n"); break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001219 case 2: puts ("HSTL 1.5 V\n"); break;
1220 case 3: puts ("SSTL 3.3 V\n"); break;
1221 case 4: puts ("SSTL 2.5 V\n"); break;
1222 case 5: puts ("SSTL 1.8 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001223 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001224 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001225
1226 switch (type) {
1227 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001228 printf ("SDRAM cycle time ");
1229 print_ddr2_tcyc (data[9]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001230 break;
1231 default:
Larry Johnson632de062008-01-11 23:26:18 -05001232 printf ("SDRAM cycle time %d.%d ns\n",
1233 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001234 break;
1235 }
1236
1237 switch (type) {
1238 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001239 printf ("SDRAM access time 0.%d%d ns\n",
1240 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001241 break;
1242 default:
Larry Johnson632de062008-01-11 23:26:18 -05001243 printf ("SDRAM access time %d.%d ns\n",
1244 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001245 break;
1246 }
1247
wdenk4b9206e2004-03-23 22:14:11 +00001248 puts ("EDC configuration ");
Larry Johnson632de062008-01-11 23:26:18 -05001249 switch (data[11]) {
wdenk4b9206e2004-03-23 22:14:11 +00001250 case 0: puts ("None\n"); break;
1251 case 1: puts ("Parity\n"); break;
1252 case 2: puts ("ECC\n"); break;
1253 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001254 }
Larry Johnson632de062008-01-11 23:26:18 -05001255
Timur Tabie857a5b2006-11-28 12:09:35 -06001256 if ((data[12] & 0x80) == 0)
wdenk4b9206e2004-03-23 22:14:11 +00001257 puts ("No self refresh, rate ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001258 else
wdenk4b9206e2004-03-23 22:14:11 +00001259 puts ("Self refresh, rate ");
Larry Johnson632de062008-01-11 23:26:18 -05001260
wdenk81a88242002-10-26 15:22:42 +00001261 switch(data[12] & 0x7F) {
Larry Johnson632de062008-01-11 23:26:18 -05001262 case 0: puts ("15.625 us\n"); break;
1263 case 1: puts ("3.9 us\n"); break;
1264 case 2: puts ("7.8 us\n"); break;
1265 case 3: puts ("31.3 us\n"); break;
1266 case 4: puts ("62.5 us\n"); break;
1267 case 5: puts ("125 us\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001268 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001269 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001270
1271 switch (type) {
1272 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001273 printf ("SDRAM width (primary) %d\n", data[13]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001274 break;
1275 default:
Larry Johnson632de062008-01-11 23:26:18 -05001276 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001277 if ((data[13] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001278 printf (" (second bank) %d\n",
1279 2 * (data[13] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001280 }
1281 break;
wdenk81a88242002-10-26 15:22:42 +00001282 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001283
1284 switch (type) {
1285 case DDR2:
1286 if (data[14] != 0)
Larry Johnson632de062008-01-11 23:26:18 -05001287 printf ("EDC width %d\n", data[14]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001288 break;
1289 default:
1290 if (data[14] != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001291 printf ("EDC width %d\n",
1292 data[14] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001293
1294 if ((data[14] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001295 printf (" (second bank) %d\n",
1296 2 * (data[14] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001297 }
1298 }
1299 break;
1300 }
1301
Larry Johnson632de062008-01-11 23:26:18 -05001302 if (DDR2 != type) {
1303 printf ("Min clock delay, back-to-back random column addresses "
1304 "%d\n", data[15]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001305 }
1306
wdenk4b9206e2004-03-23 22:14:11 +00001307 puts ("Burst length(s) ");
1308 if (data[16] & 0x80) puts (" Page");
1309 if (data[16] & 0x08) puts (" 8");
1310 if (data[16] & 0x04) puts (" 4");
1311 if (data[16] & 0x02) puts (" 2");
1312 if (data[16] & 0x01) puts (" 1");
1313 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001314 printf ("Number of banks %d\n", data[17]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001315
1316 switch (type) {
1317 case DDR2:
1318 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001319 decode_bits (data[18], decode_CAS_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001320 putc ('\n');
1321 break;
1322 default:
1323 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001324 decode_bits (data[18], decode_CAS_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001325 putc ('\n');
1326 break;
1327 }
1328
1329 if (DDR2 != type) {
1330 puts ("CS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001331 decode_bits (data[19], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001332 putc ('\n');
1333 }
1334
1335 if (DDR2 != type) {
1336 puts ("WE latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001337 decode_bits (data[20], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001338 putc ('\n');
1339 }
1340
1341 switch (type) {
1342 case DDR2:
1343 puts ("Module attributes:\n");
1344 if (data[21] & 0x80)
1345 puts (" TBD (bit 7)\n");
1346 if (data[21] & 0x40)
1347 puts (" Analysis probe installed\n");
1348 if (data[21] & 0x20)
1349 puts (" TBD (bit 5)\n");
1350 if (data[21] & 0x10)
1351 puts (" FET switch external enable\n");
Larry Johnson632de062008-01-11 23:26:18 -05001352 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
Larry Johnson0df6b842008-01-10 22:23:39 -05001353 if (data[20] & 0x11) {
Larry Johnson632de062008-01-11 23:26:18 -05001354 printf (" %d active registers on DIMM\n",
1355 (data[21] & 0x03) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001356 }
1357 break;
1358 default:
1359 puts ("Module attributes:\n");
1360 if (!data[21])
1361 puts (" (none)\n");
Larry Johnson632de062008-01-11 23:26:18 -05001362 else
1363 decode_bits (data[21], decode_byte21_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001364 break;
1365 }
1366
1367 switch (type) {
1368 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001369 decode_bits (data[22], decode_byte22_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001370 break;
1371 default:
1372 puts ("Device attributes:\n");
1373 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1374 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1375 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1376 else puts (" Upper Vcc tolerance 10%\n");
1377 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1378 else puts (" Lower Vcc tolerance 10%\n");
1379 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1380 if (data[22] & 0x04) puts (" Supports precharge all\n");
1381 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1382 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1383 break;
1384 }
1385
1386 switch (type) {
1387 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001388 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1389 print_ddr2_tcyc (data[23]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001390 break;
1391 default:
Larry Johnson632de062008-01-11 23:26:18 -05001392 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1393 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001394 break;
1395 }
1396
1397 switch (type) {
1398 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001399 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1400 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001401 break;
1402 default:
Larry Johnson632de062008-01-11 23:26:18 -05001403 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1404 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001405 break;
1406 }
1407
1408 switch (type) {
1409 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001410 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1411 print_ddr2_tcyc (data[25]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001412 break;
1413 default:
Larry Johnson632de062008-01-11 23:26:18 -05001414 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1415 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001416 break;
1417 }
1418
1419 switch (type) {
1420 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001421 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1422 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001423 break;
1424 default:
Larry Johnson632de062008-01-11 23:26:18 -05001425 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1426 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001427 break;
1428 }
1429
1430 switch (type) {
1431 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001432 printf ("Minimum row precharge %d.%02d ns\n",
1433 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001434 break;
1435 default:
Larry Johnson632de062008-01-11 23:26:18 -05001436 printf ("Minimum row precharge %d ns\n", data[27]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001437 break;
1438 }
1439
1440 switch (type) {
1441 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001442 printf ("Row active to row active min %d.%02d ns\n",
1443 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001444 break;
1445 default:
Larry Johnson632de062008-01-11 23:26:18 -05001446 printf ("Row active to row active min %d ns\n", data[28]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001447 break;
1448 }
1449
1450 switch (type) {
1451 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001452 printf ("RAS to CAS delay min %d.%02d ns\n",
1453 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001454 break;
1455 default:
Larry Johnson632de062008-01-11 23:26:18 -05001456 printf ("RAS to CAS delay min %d ns\n", data[29]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001457 break;
1458 }
1459
Larry Johnson632de062008-01-11 23:26:18 -05001460 printf ("Minimum RAS pulse width %d ns\n", data[30]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001461
1462 switch (type) {
1463 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001464 puts ("Density of each row ");
1465 decode_bits (data[31], decode_row_density_DDR2, 1);
1466 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001467 break;
1468 default:
Larry Johnson632de062008-01-11 23:26:18 -05001469 puts ("Density of each row ");
1470 decode_bits (data[31], decode_row_density_default, 1);
1471 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001472 break;
1473 }
1474
1475 switch (type) {
1476 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001477 puts ("Command and Address setup ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001478 if (data[32] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001479 printf ("1.%d%d ns\n",
1480 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001481 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001482 printf ("0.%d%d ns\n",
1483 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001484 }
1485 break;
1486 default:
Larry Johnson632de062008-01-11 23:26:18 -05001487 printf ("Command and Address setup %c%d.%d ns\n",
1488 (data[32] & 0x80) ? '-' : '+',
1489 (data[32] >> 4) & 0x07, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001490 break;
1491 }
1492
1493 switch (type) {
1494 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001495 puts ("Command and Address hold ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001496 if (data[33] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001497 printf ("1.%d%d ns\n",
1498 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001499 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001500 printf ("0.%d%d ns\n",
1501 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001502 }
1503 break;
1504 default:
Larry Johnson632de062008-01-11 23:26:18 -05001505 printf ("Command and Address hold %c%d.%d ns\n",
1506 (data[33] & 0x80) ? '-' : '+',
1507 (data[33] >> 4) & 0x07, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001508 break;
1509 }
1510
1511 switch (type) {
1512 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001513 printf ("Data signal input setup 0.%d%d ns\n",
1514 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001515 break;
1516 default:
Larry Johnson632de062008-01-11 23:26:18 -05001517 printf ("Data signal input setup %c%d.%d ns\n",
1518 (data[34] & 0x80) ? '-' : '+',
1519 (data[34] >> 4) & 0x07, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001520 break;
1521 }
1522
1523 switch (type) {
1524 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001525 printf ("Data signal input hold 0.%d%d ns\n",
1526 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001527 break;
1528 default:
Larry Johnson632de062008-01-11 23:26:18 -05001529 printf ("Data signal input hold %c%d.%d ns\n",
1530 (data[35] & 0x80) ? '-' : '+',
1531 (data[35] >> 4) & 0x07, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001532 break;
1533 }
1534
wdenk4b9206e2004-03-23 22:14:11 +00001535 puts ("Manufacturer's JEDEC ID ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001536 for (j = 64; j <= 71; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001537 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001538 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001539 printf ("Manufacturing Location %02X\n", data[72]);
wdenk4b9206e2004-03-23 22:14:11 +00001540 puts ("Manufacturer's Part Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001541 for (j = 73; j <= 90; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001542 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001543 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001544 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1545 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
wdenk4b9206e2004-03-23 22:14:11 +00001546 puts ("Assembly Serial Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001547 for (j = 95; j <= 98; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001548 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001549 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001550
Larry Johnson0df6b842008-01-10 22:23:39 -05001551 if (DDR2 != type) {
Larry Johnson632de062008-01-11 23:26:18 -05001552 printf ("Speed rating PC%d\n",
1553 data[126] == 0x66 ? 66 : data[126]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001554 }
wdenk81a88242002-10-26 15:22:42 +00001555 return 0;
1556}
Jon Loeliger90253172007-07-10 11:02:44 -05001557#endif
wdenk81a88242002-10-26 15:22:42 +00001558
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001559/*
1560 * Syntax:
1561 * i2c edid {i2c_chip}
1562 */
1563#if defined(CONFIG_I2C_EDID)
1564int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1565{
1566 u_char chip;
1567 struct edid1_info edid;
Simon Glass63656b72014-12-10 08:55:48 -07001568 int ret;
1569#ifdef CONFIG_DM_I2C
1570 struct udevice *dev;
1571#endif
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001572
1573 if (argc < 2) {
1574 cmd_usage(cmdtp);
1575 return 1;
1576 }
1577
1578 chip = simple_strtoul(argv[1], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -07001579#ifdef CONFIG_DM_I2C
1580 ret = i2c_get_cur_bus_chip(chip, &dev);
1581 if (!ret)
1582 ret = i2c_read(dev, 0, (uchar *)&edid, sizeof(edid));
1583#else
1584 ret = i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid));
1585#endif
1586 if (ret)
1587 return i2c_report_err(ret, I2C_ERR_READ);
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001588
1589 if (edid_check_info(&edid)) {
1590 puts("Content isn't valid EDID.\n");
1591 return 1;
1592 }
1593
1594 edid_print_info(&edid);
1595 return 0;
1596
1597}
1598#endif /* CONFIG_I2C_EDID */
1599
Marek Vasut06afa382012-11-12 14:34:27 +00001600/**
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001601 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
Marek Vasut06afa382012-11-12 14:34:27 +00001602 * @cmdtp: Command data struct pointer
1603 * @flag: Command flag
1604 * @argc: Command-line argument count
1605 * @argv: Array of command-line arguments
1606 *
1607 * Returns zero always.
1608 */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001609#if defined(CONFIG_SYS_I2C)
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001610static int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc,
1611 char * const argv[])
Heiko Schocher67b23a32008-10-15 09:39:47 +02001612{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001613 int i;
1614#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1615 int j;
1616#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001617
1618 if (argc == 1) {
1619 /* show all busses */
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001620 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1621 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1622#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1623 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1624 if (i2c_bus[i].next_hop[j].chip == 0)
1625 break;
1626 printf("->%s@0x%2x:%d",
1627 i2c_bus[i].next_hop[j].mux.name,
1628 i2c_bus[i].next_hop[j].chip,
1629 i2c_bus[i].next_hop[j].channel);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001630 }
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001631#endif
1632 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001633 }
1634 } else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001635 /* show specific bus */
1636 i = simple_strtoul(argv[1], NULL, 10);
1637 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1638 printf("Invalid bus %d\n", i);
1639 return -1;
1640 }
1641 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1642#ifndef CONFIG_SYS_I2C_DIRECT_BUS
1643 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1644 if (i2c_bus[i].next_hop[j].chip == 0)
1645 break;
1646 printf("->%s@0x%2x:%d",
1647 i2c_bus[i].next_hop[j].mux.name,
1648 i2c_bus[i].next_hop[j].chip,
1649 i2c_bus[i].next_hop[j].channel);
1650 }
1651#endif
1652 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001653 }
Heiko Schocher67b23a32008-10-15 09:39:47 +02001654
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001655 return 0;
1656}
1657#endif
1658
Marek Vasut06afa382012-11-12 14:34:27 +00001659/**
1660 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1661 * @cmdtp: Command data struct pointer
1662 * @flag: Command flag
1663 * @argc: Command-line argument count
1664 * @argv: Array of command-line arguments
1665 *
1666 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1667 * on error.
1668 */
Simon Glass63656b72014-12-10 08:55:48 -07001669#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) || \
1670 defined(CONFIG_DM_I2C)
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001671static int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc,
1672 char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001673{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001674 int ret = 0;
Simon Glass63656b72014-12-10 08:55:48 -07001675 int bus_no;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001676
Simon Glass63656b72014-12-10 08:55:48 -07001677 if (argc == 1) {
Timur Tabie857a5b2006-11-28 12:09:35 -06001678 /* querying current setting */
Simon Glass63656b72014-12-10 08:55:48 -07001679#ifdef CONFIG_DM_I2C
1680 struct udevice *bus;
1681
1682 if (!i2c_get_cur_bus(&bus))
1683 bus_no = bus->seq;
1684 else
1685 bus_no = -1;
1686#else
1687 bus_no = i2c_get_bus_num();
1688#endif
1689 printf("Current bus is %d\n", bus_no);
1690 } else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001691 bus_no = simple_strtoul(argv[1], NULL, 10);
Heiko Schocher880a4122013-08-23 09:39:16 +02001692#if defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001693 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1694 printf("Invalid bus %d\n", bus_no);
1695 return -1;
1696 }
Heiko Schocher880a4122013-08-23 09:39:16 +02001697#endif
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001698 printf("Setting bus to %d\n", bus_no);
1699 ret = i2c_set_bus_num(bus_no);
Timur Tabie857a5b2006-11-28 12:09:35 -06001700 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001701 printf("Failure changing bus number (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001702 }
1703 return ret;
1704}
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001705#endif /* defined(CONFIG_SYS_I2C) */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001706
Marek Vasut06afa382012-11-12 14:34:27 +00001707/**
1708 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1709 * @cmdtp: Command data struct pointer
1710 * @flag: Command flag
1711 * @argc: Command-line argument count
1712 * @argv: Array of command-line arguments
1713 *
1714 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1715 * on error.
1716 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001717static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001718{
1719 int speed, ret=0;
1720
Simon Glass63656b72014-12-10 08:55:48 -07001721#ifdef CONFIG_DM_I2C
1722 struct udevice *bus;
1723
1724 if (i2c_get_cur_bus(&bus))
1725 return 1;
1726#endif
1727 if (argc == 1) {
1728#ifdef CONFIG_DM_I2C
1729 speed = i2c_get_bus_speed(bus);
1730#else
1731 speed = i2c_get_bus_speed();
1732#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001733 /* querying current speed */
Simon Glass63656b72014-12-10 08:55:48 -07001734 printf("Current bus speed=%d\n", speed);
1735 } else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001736 speed = simple_strtoul(argv[1], NULL, 10);
1737 printf("Setting bus speed to %d Hz\n", speed);
Simon Glass63656b72014-12-10 08:55:48 -07001738#ifdef CONFIG_DM_I2C
1739 ret = i2c_set_bus_speed(bus, speed);
1740#else
Ben Warrenbb99ad62006-09-07 16:50:54 -04001741 ret = i2c_set_bus_speed(speed);
Simon Glass63656b72014-12-10 08:55:48 -07001742#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001743 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001744 printf("Failure changing bus speed (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001745 }
1746 return ret;
1747}
1748
Marek Vasut06afa382012-11-12 14:34:27 +00001749/**
1750 * do_i2c_mm() - Handle the "i2c mm" command-line command
1751 * @cmdtp: Command data struct pointer
1752 * @flag: Command flag
1753 * @argc: Command-line argument count
1754 * @argv: Array of command-line arguments
1755 *
1756 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1757 * on error.
1758 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001759static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001760{
1761 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1762}
1763
Marek Vasut06afa382012-11-12 14:34:27 +00001764/**
1765 * do_i2c_nm() - Handle the "i2c nm" command-line command
1766 * @cmdtp: Command data struct pointer
1767 * @flag: Command flag
1768 * @argc: Command-line argument count
1769 * @argv: Array of command-line arguments
1770 *
1771 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1772 * on error.
1773 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001774static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001775{
1776 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1777}
1778
Marek Vasut06afa382012-11-12 14:34:27 +00001779/**
1780 * do_i2c_reset() - Handle the "i2c reset" command-line command
1781 * @cmdtp: Command data struct pointer
1782 * @flag: Command flag
1783 * @argc: Command-line argument count
1784 * @argv: Array of command-line arguments
1785 *
1786 * Returns zero always.
1787 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001788static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001789{
Simon Glass63656b72014-12-10 08:55:48 -07001790#if defined(CONFIG_DM_I2C)
1791 struct udevice *bus;
1792
1793 if (i2c_get_cur_bus(&bus))
1794 return CMD_RET_FAILURE;
1795 if (i2c_deblock(bus)) {
1796 printf("Error: Not supported by the driver\n");
1797 return CMD_RET_FAILURE;
1798 }
1799#elif defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001800 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1801#else
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001802 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001803#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001804 return 0;
1805}
1806
1807static cmd_tbl_t cmd_i2c_sub[] = {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001808#if defined(CONFIG_SYS_I2C)
1809 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001810#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001811 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001812#if defined(CONFIG_SYS_I2C) || \
Simon Glass63656b72014-12-10 08:55:48 -07001813 defined(CONFIG_I2C_MULTI_BUS) || defined(CONFIG_DM_I2C)
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001814 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1815#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001816#if defined(CONFIG_I2C_EDID)
1817 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1818#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001819 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1820 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1821 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1822 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1823 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1824 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001825 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
York Sunff5d2dc2012-09-16 08:02:30 +00001826 U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
Simon Glass63656b72014-12-10 08:55:48 -07001827#ifdef CONFIG_DM_I2C
1828 U_BOOT_CMD_MKENT(flags, 2, 1, do_i2c_flags, "", ""),
1829#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001830 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1831#if defined(CONFIG_CMD_SDRAM)
1832 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1833#endif
1834 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1835};
1836
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001837#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001838void i2c_reloc(void) {
1839 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1840}
1841#endif
1842
Marek Vasut06afa382012-11-12 14:34:27 +00001843/**
1844 * do_i2c() - Handle the "i2c" command-line command
1845 * @cmdtp: Command data struct pointer
1846 * @flag: Command flag
1847 * @argc: Command-line argument count
1848 * @argv: Array of command-line arguments
1849 *
1850 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1851 * on error.
1852 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001853static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001854{
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001855 cmd_tbl_t *c;
1856
Heiko Schocher4444b222010-09-17 13:10:35 +02001857 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001858 return CMD_RET_USAGE;
Heiko Schocher4444b222010-09-17 13:10:35 +02001859
Peter Tysere96ad5d2009-04-18 22:34:04 -05001860 /* Strip off leading 'i2c' command argument */
1861 argc--;
1862 argv++;
1863
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001864 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
1865
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001866 if (c)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001867 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001868 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00001869 return CMD_RET_USAGE;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001870}
wdenk8bde7f72003-06-27 21:31:46 +00001871
1872/***************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00001873#ifdef CONFIG_SYS_LONGHELP
1874static char i2c_help_text[] =
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001875#if defined(CONFIG_SYS_I2C)
1876 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001877#endif
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001878 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001879#if defined(CONFIG_SYS_I2C) || \
Simon Glass63656b72014-12-10 08:55:48 -07001880 defined(CONFIG_I2C_MULTI_BUS) || defined(CONFIG_DM_I2C)
Peter Tyser9bc2e4e2008-10-01 12:25:04 -05001881 "i2c dev [dev] - show or set current I2C bus\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001882#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001883#if defined(CONFIG_I2C_EDID)
1884 "i2c edid chip - print EDID configuration information\n"
1885#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001886 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001887 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
1888 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
1889 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
1890 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
Eric Nelson54b99e52012-09-23 10:12:56 +00001891 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
Simon Glass63656b72014-12-10 08:55:48 -07001892 "i2c read chip address[.0, .1, .2] length memaddress - read to memory\n"
York Sunff5d2dc2012-09-16 08:02:30 +00001893 "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
Simon Glass63656b72014-12-10 08:55:48 -07001894#ifdef CONFIG_DM_I2C
1895 "i2c flags chip [flags] - set or get chip flags\n"
1896#endif
Heiko Schochere43a27c2008-10-15 09:33:30 +02001897 "i2c reset - re-init the I2C Controller\n"
Jon Loeligerc76fe472007-07-08 18:02:23 -05001898#if defined(CONFIG_CMD_SDRAM)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01001899 "i2c sdram chip - print SDRAM configuration information\n"
Jon Loeliger90253172007-07-10 11:02:44 -05001900#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001901 "i2c speed [speed] - show or set I2C bus speed";
1902#endif
1903
1904U_BOOT_CMD(
1905 i2c, 6, 1, do_i2c,
1906 "I2C sub-system",
1907 i2c_help_text
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01001908);