blob: 8eca6fffaad44fa3812917926ee243d93a7d43d7 [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 Glass24b852a2015-11-08 23:47:45 -070072#include <console.h>
Simon Glass63656b72014-12-10 08:55:48 -070073#include <dm.h>
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +000074#include <edid.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020075#include <environment.h>
Simon Glass63656b72014-12-10 08:55:48 -070076#include <errno.h>
wdenk81a88242002-10-26 15:22:42 +000077#include <i2c.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020078#include <malloc.h>
wdenk81a88242002-10-26 15:22:42 +000079#include <asm/byteorder.h>
Marek Vasut2515d842012-11-12 14:34:25 +000080#include <linux/compiler.h>
wdenk81a88242002-10-26 15:22:42 +000081
wdenk81a88242002-10-26 15:22:42 +000082/* Display values from last command.
83 * Memory modify remembered values are different from display memory.
84 */
Masahiro Yamada54684612014-12-20 03:34:23 +090085static uint i2c_dp_last_chip;
wdenk81a88242002-10-26 15:22:42 +000086static uint i2c_dp_last_addr;
87static uint i2c_dp_last_alen;
88static uint i2c_dp_last_length = 0x10;
89
Masahiro Yamada54684612014-12-20 03:34:23 +090090static uint i2c_mm_last_chip;
wdenk81a88242002-10-26 15:22:42 +000091static uint i2c_mm_last_addr;
92static uint i2c_mm_last_alen;
93
Ben Warrenbb99ad62006-09-07 16:50:54 -040094/* If only one I2C bus is present, the list of devices to ignore when
95 * the probe command is issued is represented by a 1D array of addresses.
96 * When multiple buses are present, the list is an array of bus-address
97 * pairs. The following macros take care of this */
98
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099#if defined(CONFIG_SYS_I2C_NOPROBES)
Heiko Schocher9a2accb2012-10-25 10:32:14 +0200100#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
Ben Warrenbb99ad62006-09-07 16:50:54 -0400101static struct
102{
103 uchar bus;
104 uchar addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105} i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400106#define GET_BUS_NUM i2c_get_bus_num()
107#define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
108#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
109#define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
110#else /* single bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400112#define GET_BUS_NUM 0
113#define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
114#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
115#define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000116#endif /* defined(CONFIG_SYS_I2C) */
Heiko Schocher67b23a32008-10-15 09:39:47 +0200117#endif
118
Frans Meulenbroeksa266fe92010-03-26 09:46:40 +0100119#define DISP_LINE_LEN 16
120
Simon Glass63656b72014-12-10 08:55:48 -0700121/*
122 * Default for driver model is to use the chip's existing address length.
123 * For legacy code, this is not stored, so we need to use a suitable
124 * default.
125 */
126#ifdef CONFIG_DM_I2C
127#define DEFAULT_ADDR_LEN (-1)
128#else
129#define DEFAULT_ADDR_LEN 1
130#endif
131
132#ifdef CONFIG_DM_I2C
133static struct udevice *i2c_cur_bus;
134
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700135static int cmd_i2c_set_bus_num(unsigned int busnum)
Simon Glass63656b72014-12-10 08:55:48 -0700136{
137 struct udevice *bus;
138 int ret;
139
140 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
141 if (ret) {
142 debug("%s: No bus %d\n", __func__, busnum);
143 return ret;
144 }
145 i2c_cur_bus = bus;
146
147 return 0;
148}
149
150static int i2c_get_cur_bus(struct udevice **busp)
151{
Lukasz Majewskie46f8a32017-03-21 12:08:25 +0100152#ifdef CONFIG_I2C_SET_DEFAULT_BUS_NUM
153 if (!i2c_cur_bus) {
154 if (cmd_i2c_set_bus_num(CONFIG_I2C_DEFAULT_BUS_NUMBER)) {
155 printf("Default I2C bus %d not found\n",
156 CONFIG_I2C_DEFAULT_BUS_NUMBER);
157 return -ENODEV;
158 }
159 }
160#endif
161
Simon Glass63656b72014-12-10 08:55:48 -0700162 if (!i2c_cur_bus) {
163 puts("No I2C bus selected\n");
164 return -ENODEV;
165 }
166 *busp = i2c_cur_bus;
167
168 return 0;
169}
170
171static int i2c_get_cur_bus_chip(uint chip_addr, struct udevice **devp)
172{
173 struct udevice *bus;
174 int ret;
175
176 ret = i2c_get_cur_bus(&bus);
177 if (ret)
178 return ret;
179
Simon Glass25ab4b02015-01-25 08:26:55 -0700180 return i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glass63656b72014-12-10 08:55:48 -0700181}
182
183#endif
184
Marek Vasut06afa382012-11-12 14:34:27 +0000185/**
186 * i2c_init_board() - Board-specific I2C bus init
187 *
188 * This function is the default no-op implementation of I2C bus
Robert P. J. Day62a3b7d2016-07-15 13:44:45 -0400189 * initialization. This function can be overridden by board-specific
Marek Vasut06afa382012-11-12 14:34:27 +0000190 * implementation if needed.
191 */
Marek Vasut2515d842012-11-12 14:34:25 +0000192__weak
193void i2c_init_board(void)
Stefan Biglerc649dda2011-04-08 16:24:08 +0200194{
Stefan Biglerc649dda2011-04-08 16:24:08 +0200195}
Stefan Biglerc649dda2011-04-08 16:24:08 +0200196
Peter Tyser655b34a2009-04-18 22:34:01 -0500197/* TODO: Implement architecture-specific get/set functions */
Marek Vasut06afa382012-11-12 14:34:27 +0000198
199/**
200 * i2c_get_bus_speed() - Return I2C bus speed
201 *
202 * This function is the default implementation of function for retrieveing
203 * the current I2C bus speed in Hz.
204 *
205 * A driver implementing runtime switching of I2C bus speed must override
206 * this function to report the speed correctly. Simple or legacy drivers
207 * can use this fallback.
208 *
209 * Returns I2C bus speed in Hz.
210 */
Simon Glass63656b72014-12-10 08:55:48 -0700211#if !defined(CONFIG_SYS_I2C) && !defined(CONFIG_DM_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000212/*
213 * TODO: Implement architecture-specific get/set functions
214 * Should go away, if we switched completely to new multibus support
215 */
Marek Vasut2515d842012-11-12 14:34:25 +0000216__weak
217unsigned int i2c_get_bus_speed(void)
Peter Tyser655b34a2009-04-18 22:34:01 -0500218{
219 return CONFIG_SYS_I2C_SPEED;
220}
Peter Tyser655b34a2009-04-18 22:34:01 -0500221
Marek Vasut06afa382012-11-12 14:34:27 +0000222/**
223 * i2c_set_bus_speed() - Configure I2C bus speed
224 * @speed: Newly set speed of the I2C bus in Hz
225 *
226 * This function is the default implementation of function for setting
227 * the I2C bus speed in Hz.
228 *
229 * A driver implementing runtime switching of I2C bus speed must override
230 * this function to report the speed correctly. Simple or legacy drivers
231 * can use this fallback.
232 *
233 * Returns zero on success, negative value on error.
234 */
Marek Vasut2515d842012-11-12 14:34:25 +0000235__weak
236int i2c_set_bus_speed(unsigned int speed)
Peter Tyser655b34a2009-04-18 22:34:01 -0500237{
238 if (speed != CONFIG_SYS_I2C_SPEED)
239 return -1;
240
241 return 0;
242}
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000243#endif
Peter Tyser655b34a2009-04-18 22:34:01 -0500244
Marek Vasut06afa382012-11-12 14:34:27 +0000245/**
246 * get_alen() - Small parser helper function to get address length
247 *
248 * Returns the address length.
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100249 */
Simon Glass63656b72014-12-10 08:55:48 -0700250static uint get_alen(char *arg, int default_len)
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100251{
252 int j;
253 int alen;
254
Simon Glass63656b72014-12-10 08:55:48 -0700255 alen = default_len;
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100256 for (j = 0; j < 8; j++) {
257 if (arg[j] == '.') {
258 alen = arg[j+1] - '0';
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100259 break;
260 } else if (arg[j] == '\0')
261 break;
262 }
263 return alen;
264}
265
Simon Glassc1a6f372014-11-11 10:46:17 -0700266enum i2c_err_op {
267 I2C_ERR_READ,
268 I2C_ERR_WRITE,
269};
270
271static int i2c_report_err(int ret, enum i2c_err_op op)
272{
273 printf("Error %s the chip: %d\n",
274 op == I2C_ERR_READ ? "reading" : "writing", ret);
275
276 return CMD_RET_FAILURE;
277}
278
Marek Vasut06afa382012-11-12 14:34:27 +0000279/**
280 * do_i2c_read() - Handle the "i2c read" command-line command
281 * @cmdtp: Command data struct pointer
282 * @flag: Command flag
283 * @argc: Command-line argument count
284 * @argv: Array of command-line arguments
285 *
286 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
287 * on error.
288 *
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100289 * Syntax:
290 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
291 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200292static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100293{
Masahiro Yamada54684612014-12-20 03:34:23 +0900294 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -0700295 uint devaddr, length;
296 int alen;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100297 u_char *memaddr;
Simon Glass63656b72014-12-10 08:55:48 -0700298 int ret;
299#ifdef CONFIG_DM_I2C
300 struct udevice *dev;
301#endif
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100302
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200303 if (argc != 5)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000304 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100305
306 /*
307 * I2C chip address
308 */
309 chip = simple_strtoul(argv[1], NULL, 16);
310
311 /*
312 * I2C data address within the chip. This can be 1 or
313 * 2 bytes long. Some day it might be 3 bytes long :-).
314 */
315 devaddr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700316 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200317 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000318 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100319
320 /*
321 * Length is the number of objects, not number of bytes.
322 */
323 length = simple_strtoul(argv[3], NULL, 16);
324
325 /*
326 * memaddr is the address where to store things in memory
327 */
328 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
329
Simon Glass63656b72014-12-10 08:55:48 -0700330#ifdef CONFIG_DM_I2C
331 ret = i2c_get_cur_bus_chip(chip, &dev);
332 if (!ret && alen != -1)
333 ret = i2c_set_chip_offset_len(dev, alen);
334 if (!ret)
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700335 ret = dm_i2c_read(dev, devaddr, memaddr, length);
Simon Glass63656b72014-12-10 08:55:48 -0700336#else
337 ret = i2c_read(chip, devaddr, alen, memaddr, length);
338#endif
339 if (ret)
340 return i2c_report_err(ret, I2C_ERR_READ);
341
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100342 return 0;
343}
344
York Sunff5d2dc2012-09-16 08:02:30 +0000345static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
346{
Masahiro Yamada54684612014-12-20 03:34:23 +0900347 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -0700348 uint devaddr, length;
349 int alen;
York Sunff5d2dc2012-09-16 08:02:30 +0000350 u_char *memaddr;
Simon Glass63656b72014-12-10 08:55:48 -0700351 int ret;
352#ifdef CONFIG_DM_I2C
353 struct udevice *dev;
Lubomir Popoved16f142015-01-30 19:56:04 +0200354 struct dm_i2c_chip *i2c_chip;
Simon Glass63656b72014-12-10 08:55:48 -0700355#endif
York Sunff5d2dc2012-09-16 08:02:30 +0000356
Lubomir Popoved16f142015-01-30 19:56:04 +0200357 if ((argc < 5) || (argc > 6))
York Sunff5d2dc2012-09-16 08:02:30 +0000358 return cmd_usage(cmdtp);
359
360 /*
361 * memaddr is the address where to store things in memory
362 */
363 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
364
365 /*
366 * I2C chip address
367 */
368 chip = simple_strtoul(argv[2], NULL, 16);
369
370 /*
371 * I2C data address within the chip. This can be 1 or
372 * 2 bytes long. Some day it might be 3 bytes long :-).
373 */
374 devaddr = simple_strtoul(argv[3], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700375 alen = get_alen(argv[3], DEFAULT_ADDR_LEN);
York Sunff5d2dc2012-09-16 08:02:30 +0000376 if (alen > 3)
377 return cmd_usage(cmdtp);
378
379 /*
Lubomir Popoved16f142015-01-30 19:56:04 +0200380 * Length is the number of bytes.
York Sunff5d2dc2012-09-16 08:02:30 +0000381 */
382 length = simple_strtoul(argv[4], NULL, 16);
383
Simon Glass63656b72014-12-10 08:55:48 -0700384#ifdef CONFIG_DM_I2C
385 ret = i2c_get_cur_bus_chip(chip, &dev);
386 if (!ret && alen != -1)
387 ret = i2c_set_chip_offset_len(dev, alen);
388 if (ret)
389 return i2c_report_err(ret, I2C_ERR_WRITE);
Lubomir Popoved16f142015-01-30 19:56:04 +0200390 i2c_chip = dev_get_parent_platdata(dev);
391 if (!i2c_chip)
392 return i2c_report_err(ret, I2C_ERR_WRITE);
Simon Glass63656b72014-12-10 08:55:48 -0700393#endif
394
Lubomir Popoved16f142015-01-30 19:56:04 +0200395 if (argc == 6 && !strcmp(argv[5], "-s")) {
396 /*
397 * Write all bytes in a single I2C transaction. If the target
398 * device is an EEPROM, it is your responsibility to not cross
399 * a page boundary. No write delay upon completion, take this
400 * into account if linking commands.
401 */
Simon Glass63656b72014-12-10 08:55:48 -0700402#ifdef CONFIG_DM_I2C
Lubomir Popoved16f142015-01-30 19:56:04 +0200403 i2c_chip->flags &= ~DM_I2C_CHIP_WR_ADDRESS;
404 ret = dm_i2c_write(dev, devaddr, memaddr, length);
Simon Glass63656b72014-12-10 08:55:48 -0700405#else
Lubomir Popoved16f142015-01-30 19:56:04 +0200406 ret = i2c_write(chip, devaddr, alen, memaddr, length);
Simon Glass63656b72014-12-10 08:55:48 -0700407#endif
408 if (ret)
409 return i2c_report_err(ret, I2C_ERR_WRITE);
Lubomir Popoved16f142015-01-30 19:56:04 +0200410 } else {
411 /*
412 * Repeated addressing - perform <length> separate
413 * write transactions of one byte each
414 */
415 while (length-- > 0) {
416#ifdef CONFIG_DM_I2C
417 i2c_chip->flags |= DM_I2C_CHIP_WR_ADDRESS;
418 ret = dm_i2c_write(dev, devaddr++, memaddr++, 1);
419#else
420 ret = i2c_write(chip, devaddr++, alen, memaddr++, 1);
421#endif
422 if (ret)
423 return i2c_report_err(ret, I2C_ERR_WRITE);
York Sunff5d2dc2012-09-16 08:02:30 +0000424/*
425 * No write delay with FRAM devices.
426 */
427#if !defined(CONFIG_SYS_I2C_FRAM)
Lubomir Popoved16f142015-01-30 19:56:04 +0200428 udelay(11000);
York Sunff5d2dc2012-09-16 08:02:30 +0000429#endif
Lubomir Popoved16f142015-01-30 19:56:04 +0200430 }
York Sunff5d2dc2012-09-16 08:02:30 +0000431 }
432 return 0;
433}
434
Simon Glass63656b72014-12-10 08:55:48 -0700435#ifdef CONFIG_DM_I2C
436static int do_i2c_flags(cmd_tbl_t *cmdtp, int flag, int argc,
437 char *const argv[])
438{
439 struct udevice *dev;
440 uint flags;
441 int chip;
442 int ret;
443
444 if (argc < 2)
445 return CMD_RET_USAGE;
446
447 chip = simple_strtoul(argv[1], NULL, 16);
448 ret = i2c_get_cur_bus_chip(chip, &dev);
449 if (ret)
450 return i2c_report_err(ret, I2C_ERR_READ);
451
452 if (argc > 2) {
453 flags = simple_strtoul(argv[2], NULL, 16);
454 ret = i2c_set_chip_flags(dev, flags);
455 } else {
456 ret = i2c_get_chip_flags(dev, &flags);
457 if (!ret)
458 printf("%x\n", flags);
459 }
460 if (ret)
461 return i2c_report_err(ret, I2C_ERR_READ);
462
463 return 0;
464}
Simon Glassc10c8e32015-08-22 18:31:33 -0600465
466static int do_i2c_olen(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
467{
468 struct udevice *dev;
469 uint olen;
470 int chip;
471 int ret;
472
473 if (argc < 2)
474 return CMD_RET_USAGE;
475
476 chip = simple_strtoul(argv[1], NULL, 16);
477 ret = i2c_get_cur_bus_chip(chip, &dev);
478 if (ret)
479 return i2c_report_err(ret, I2C_ERR_READ);
480
481 if (argc > 2) {
482 olen = simple_strtoul(argv[2], NULL, 16);
483 ret = i2c_set_chip_offset_len(dev, olen);
484 } else {
485 ret = i2c_get_chip_offset_len(dev);
486 if (ret >= 0) {
487 printf("%x\n", ret);
488 ret = 0;
489 }
490 }
491 if (ret)
492 return i2c_report_err(ret, I2C_ERR_READ);
493
494 return 0;
495}
Simon Glass63656b72014-12-10 08:55:48 -0700496#endif
497
Marek Vasut06afa382012-11-12 14:34:27 +0000498/**
499 * do_i2c_md() - Handle the "i2c md" command-line command
500 * @cmdtp: Command data struct pointer
501 * @flag: Command flag
502 * @argc: Command-line argument count
503 * @argv: Array of command-line arguments
504 *
505 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
506 * on error.
507 *
Frans Meulenbroeks4a8cf332010-03-26 09:46:39 +0100508 * Syntax:
509 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
510 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200511static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000512{
Masahiro Yamada54684612014-12-20 03:34:23 +0900513 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -0700514 uint addr, length;
515 int alen;
wdenk81a88242002-10-26 15:22:42 +0000516 int j, nbytes, linebytes;
Simon Glass63656b72014-12-10 08:55:48 -0700517 int ret;
518#ifdef CONFIG_DM_I2C
519 struct udevice *dev;
520#endif
wdenk81a88242002-10-26 15:22:42 +0000521
522 /* We use the last specified parameters, unless new ones are
523 * entered.
524 */
525 chip = i2c_dp_last_chip;
526 addr = i2c_dp_last_addr;
527 alen = i2c_dp_last_alen;
528 length = i2c_dp_last_length;
529
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200530 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000531 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000532
533 if ((flag & CMD_FLAG_REPEAT) == 0) {
534 /*
535 * New command specified.
536 */
wdenk81a88242002-10-26 15:22:42 +0000537
538 /*
539 * I2C chip address
540 */
541 chip = simple_strtoul(argv[1], NULL, 16);
542
543 /*
544 * I2C data address within the chip. This can be 1 or
545 * 2 bytes long. Some day it might be 3 bytes long :-).
546 */
547 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700548 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200549 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000550 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000551
552 /*
553 * If another parameter, it is the length to display.
554 * Length is the number of objects, not number of bytes.
555 */
556 if (argc > 3)
557 length = simple_strtoul(argv[3], NULL, 16);
558 }
559
Simon Glass63656b72014-12-10 08:55:48 -0700560#ifdef CONFIG_DM_I2C
561 ret = i2c_get_cur_bus_chip(chip, &dev);
562 if (!ret && alen != -1)
563 ret = i2c_set_chip_offset_len(dev, alen);
564 if (ret)
565 return i2c_report_err(ret, I2C_ERR_READ);
566#endif
567
wdenk81a88242002-10-26 15:22:42 +0000568 /*
569 * Print the lines.
570 *
571 * We buffer all read data, so we can make sure data is read only
572 * once.
573 */
574 nbytes = length;
575 do {
576 unsigned char linebuf[DISP_LINE_LEN];
577 unsigned char *cp;
578
579 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
580
Simon Glass63656b72014-12-10 08:55:48 -0700581#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700582 ret = dm_i2c_read(dev, addr, linebuf, linebytes);
Simon Glass63656b72014-12-10 08:55:48 -0700583#else
584 ret = i2c_read(chip, addr, alen, linebuf, linebytes);
585#endif
586 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900587 return i2c_report_err(ret, I2C_ERR_READ);
Timur Tabie857a5b2006-11-28 12:09:35 -0600588 else {
wdenk81a88242002-10-26 15:22:42 +0000589 printf("%04x:", addr);
590 cp = linebuf;
591 for (j=0; j<linebytes; j++) {
592 printf(" %02x", *cp++);
593 addr++;
594 }
wdenk4b9206e2004-03-23 22:14:11 +0000595 puts (" ");
wdenk81a88242002-10-26 15:22:42 +0000596 cp = linebuf;
597 for (j=0; j<linebytes; j++) {
598 if ((*cp < 0x20) || (*cp > 0x7e))
wdenk4b9206e2004-03-23 22:14:11 +0000599 puts (".");
wdenk81a88242002-10-26 15:22:42 +0000600 else
601 printf("%c", *cp);
602 cp++;
603 }
wdenk4b9206e2004-03-23 22:14:11 +0000604 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000605 }
606 nbytes -= linebytes;
607 } while (nbytes > 0);
608
609 i2c_dp_last_chip = chip;
610 i2c_dp_last_addr = addr;
611 i2c_dp_last_alen = alen;
612 i2c_dp_last_length = length;
613
614 return 0;
615}
616
Marek Vasut06afa382012-11-12 14:34:27 +0000617/**
618 * do_i2c_mw() - Handle the "i2c mw" command-line command
619 * @cmdtp: Command data struct pointer
620 * @flag: Command flag
621 * @argc: Command-line argument count
622 * @argv: Array of command-line arguments
623 *
624 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
625 * on error.
wdenk81a88242002-10-26 15:22:42 +0000626 *
627 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500628 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
wdenk81a88242002-10-26 15:22:42 +0000629 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200630static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000631{
Masahiro Yamada54684612014-12-20 03:34:23 +0900632 uint chip;
wdenk81a88242002-10-26 15:22:42 +0000633 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700634 int alen;
wdenk81a88242002-10-26 15:22:42 +0000635 uchar byte;
636 int count;
Simon Glass63656b72014-12-10 08:55:48 -0700637 int ret;
638#ifdef CONFIG_DM_I2C
639 struct udevice *dev;
640#endif
wdenk81a88242002-10-26 15:22:42 +0000641
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200642 if ((argc < 4) || (argc > 5))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000643 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000644
645 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200646 * Chip is always specified.
647 */
wdenk81a88242002-10-26 15:22:42 +0000648 chip = simple_strtoul(argv[1], NULL, 16);
649
650 /*
651 * Address is always specified.
652 */
653 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700654 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200655 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000656 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000657
Simon Glass63656b72014-12-10 08:55:48 -0700658#ifdef CONFIG_DM_I2C
659 ret = i2c_get_cur_bus_chip(chip, &dev);
660 if (!ret && alen != -1)
661 ret = i2c_set_chip_offset_len(dev, alen);
662 if (ret)
663 return i2c_report_err(ret, I2C_ERR_WRITE);
664#endif
wdenk81a88242002-10-26 15:22:42 +0000665 /*
666 * Value to write is always specified.
667 */
668 byte = simple_strtoul(argv[3], NULL, 16);
669
670 /*
671 * Optional count
672 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600673 if (argc == 5)
wdenk81a88242002-10-26 15:22:42 +0000674 count = simple_strtoul(argv[4], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600675 else
wdenk81a88242002-10-26 15:22:42 +0000676 count = 1;
wdenk81a88242002-10-26 15:22:42 +0000677
678 while (count-- > 0) {
Simon Glass63656b72014-12-10 08:55:48 -0700679#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700680 ret = dm_i2c_write(dev, addr++, &byte, 1);
Simon Glass63656b72014-12-10 08:55:48 -0700681#else
682 ret = i2c_write(chip, addr++, alen, &byte, 1);
683#endif
684 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900685 return i2c_report_err(ret, I2C_ERR_WRITE);
wdenk81a88242002-10-26 15:22:42 +0000686 /*
687 * Wait for the write to complete. The write can take
688 * up to 10mSec (we allow a little more time).
wdenk81a88242002-10-26 15:22:42 +0000689 */
d4f5c722005-08-12 21:16:13 +0200690/*
691 * No write delay with FRAM devices.
692 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200693#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk81a88242002-10-26 15:22:42 +0000694 udelay(11000);
d4f5c722005-08-12 21:16:13 +0200695#endif
wdenk81a88242002-10-26 15:22:42 +0000696 }
697
Marek Vasut06afa382012-11-12 14:34:27 +0000698 return 0;
wdenk81a88242002-10-26 15:22:42 +0000699}
700
Marek Vasut06afa382012-11-12 14:34:27 +0000701/**
702 * do_i2c_crc() - Handle the "i2c crc32" command-line command
703 * @cmdtp: Command data struct pointer
704 * @flag: Command flag
705 * @argc: Command-line argument count
706 * @argv: Array of command-line arguments
707 *
708 * Calculate a CRC on memory
709 *
710 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
711 * on error.
wdenk81a88242002-10-26 15:22:42 +0000712 *
713 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500714 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
wdenk81a88242002-10-26 15:22:42 +0000715 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200716static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000717{
Masahiro Yamada54684612014-12-20 03:34:23 +0900718 uint chip;
wdenk81a88242002-10-26 15:22:42 +0000719 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700720 int alen;
wdenk81a88242002-10-26 15:22:42 +0000721 int count;
722 uchar byte;
723 ulong crc;
724 ulong err;
Simon Glass63656b72014-12-10 08:55:48 -0700725 int ret = 0;
726#ifdef CONFIG_DM_I2C
727 struct udevice *dev;
728#endif
wdenk81a88242002-10-26 15:22:42 +0000729
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200730 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000731 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000732
733 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200734 * Chip is always specified.
735 */
wdenk81a88242002-10-26 15:22:42 +0000736 chip = simple_strtoul(argv[1], NULL, 16);
737
738 /*
739 * Address is always specified.
740 */
741 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700742 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200743 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000744 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000745
Simon Glass63656b72014-12-10 08:55:48 -0700746#ifdef CONFIG_DM_I2C
747 ret = i2c_get_cur_bus_chip(chip, &dev);
748 if (!ret && alen != -1)
749 ret = i2c_set_chip_offset_len(dev, alen);
750 if (ret)
751 return i2c_report_err(ret, I2C_ERR_READ);
752#endif
wdenk81a88242002-10-26 15:22:42 +0000753 /*
754 * Count is always specified
755 */
756 count = simple_strtoul(argv[3], NULL, 16);
757
758 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
759 /*
760 * CRC a byte at a time. This is going to be slooow, but hey, the
761 * memories are small and slow too so hopefully nobody notices.
762 */
763 crc = 0;
764 err = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600765 while (count-- > 0) {
Simon Glass63656b72014-12-10 08:55:48 -0700766#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700767 ret = dm_i2c_read(dev, addr, &byte, 1);
Simon Glass63656b72014-12-10 08:55:48 -0700768#else
769 ret = i2c_read(chip, addr, alen, &byte, 1);
770#endif
771 if (ret)
wdenk81a88242002-10-26 15:22:42 +0000772 err++;
wdenk81a88242002-10-26 15:22:42 +0000773 crc = crc32 (crc, &byte, 1);
774 addr++;
775 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600776 if (err > 0)
Simon Glass63656b72014-12-10 08:55:48 -0700777 i2c_report_err(ret, I2C_ERR_READ);
Timur Tabie857a5b2006-11-28 12:09:35 -0600778 else
wdenk81a88242002-10-26 15:22:42 +0000779 printf ("%08lx\n", crc);
wdenk81a88242002-10-26 15:22:42 +0000780
781 return 0;
782}
783
Marek Vasut06afa382012-11-12 14:34:27 +0000784/**
785 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
786 * @cmdtp: Command data struct pointer
787 * @flag: Command flag
788 * @argc: Command-line argument count
789 * @argv: Array of command-line arguments
790 *
791 * Modify memory.
792 *
793 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
794 * on error.
wdenk81a88242002-10-26 15:22:42 +0000795 *
796 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500797 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
798 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
wdenk81a88242002-10-26 15:22:42 +0000799 */
wdenk81a88242002-10-26 15:22:42 +0000800static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200801mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000802{
Masahiro Yamada54684612014-12-20 03:34:23 +0900803 uint chip;
wdenk81a88242002-10-26 15:22:42 +0000804 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700805 int alen;
wdenk81a88242002-10-26 15:22:42 +0000806 ulong data;
807 int size = 1;
808 int nbytes;
Simon Glass63656b72014-12-10 08:55:48 -0700809 int ret;
810#ifdef CONFIG_DM_I2C
811 struct udevice *dev;
812#endif
wdenk81a88242002-10-26 15:22:42 +0000813
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200814 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000815 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000816
Simon Glassb26440f2014-04-10 20:01:31 -0600817 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk81a88242002-10-26 15:22:42 +0000818 /*
819 * We use the last specified parameters, unless new ones are
820 * entered.
821 */
822 chip = i2c_mm_last_chip;
823 addr = i2c_mm_last_addr;
824 alen = i2c_mm_last_alen;
825
826 if ((flag & CMD_FLAG_REPEAT) == 0) {
827 /*
828 * New command specified. Check for a size specification.
829 * Defaults to byte if no or incorrect specification.
830 */
831 size = cmd_get_data_size(argv[0], 1);
832
833 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200834 * Chip is always specified.
835 */
wdenk81a88242002-10-26 15:22:42 +0000836 chip = simple_strtoul(argv[1], NULL, 16);
837
838 /*
839 * Address is always specified.
840 */
841 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700842 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200843 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000844 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000845 }
846
Simon Glass63656b72014-12-10 08:55:48 -0700847#ifdef CONFIG_DM_I2C
848 ret = i2c_get_cur_bus_chip(chip, &dev);
849 if (!ret && alen != -1)
850 ret = i2c_set_chip_offset_len(dev, alen);
851 if (ret)
852 return i2c_report_err(ret, I2C_ERR_WRITE);
853#endif
854
wdenk81a88242002-10-26 15:22:42 +0000855 /*
856 * Print the address, followed by value. Then accept input for
857 * the next value. A non-converted value exits.
858 */
859 do {
860 printf("%08lx:", addr);
Simon Glass63656b72014-12-10 08:55:48 -0700861#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700862 ret = dm_i2c_read(dev, addr, (uchar *)&data, size);
Simon Glass63656b72014-12-10 08:55:48 -0700863#else
864 ret = i2c_read(chip, addr, alen, (uchar *)&data, size);
865#endif
866 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900867 return i2c_report_err(ret, I2C_ERR_READ);
868
869 data = cpu_to_be32(data);
870 if (size == 1)
871 printf(" %02lx", (data >> 24) & 0x000000FF);
872 else if (size == 2)
873 printf(" %04lx", (data >> 16) & 0x0000FFFF);
874 else
875 printf(" %08lx", data);
wdenk81a88242002-10-26 15:22:42 +0000876
Simon Glasse1bf8242014-04-10 20:01:27 -0600877 nbytes = cli_readline(" ? ");
wdenk81a88242002-10-26 15:22:42 +0000878 if (nbytes == 0) {
879 /*
880 * <CR> pressed as only input, don't modify current
881 * location and move to next.
882 */
883 if (incrflag)
884 addr += size;
885 nbytes = size;
Simon Glassb26440f2014-04-10 20:01:31 -0600886 /* good enough to not time out */
887 bootretry_reset_cmd_timeout();
wdenk81a88242002-10-26 15:22:42 +0000888 }
889#ifdef CONFIG_BOOT_RETRY_TIME
Timur Tabie857a5b2006-11-28 12:09:35 -0600890 else if (nbytes == -2)
wdenk81a88242002-10-26 15:22:42 +0000891 break; /* timed out, exit the command */
wdenk81a88242002-10-26 15:22:42 +0000892#endif
893 else {
894 char *endp;
895
896 data = simple_strtoul(console_buffer, &endp, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600897 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000898 data = data << 24;
Timur Tabie857a5b2006-11-28 12:09:35 -0600899 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000900 data = data << 16;
wdenk81a88242002-10-26 15:22:42 +0000901 data = be32_to_cpu(data);
902 nbytes = endp - console_buffer;
903 if (nbytes) {
wdenk81a88242002-10-26 15:22:42 +0000904 /*
905 * good enough to not time out
906 */
Simon Glassb26440f2014-04-10 20:01:31 -0600907 bootretry_reset_cmd_timeout();
Simon Glass63656b72014-12-10 08:55:48 -0700908#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700909 ret = dm_i2c_write(dev, addr, (uchar *)&data,
910 size);
Simon Glass63656b72014-12-10 08:55:48 -0700911#else
912 ret = i2c_write(chip, addr, alen,
913 (uchar *)&data, size);
914#endif
915 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900916 return i2c_report_err(ret,
917 I2C_ERR_WRITE);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200918#ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
919 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk2535d602003-07-17 23:16:40 +0000920#endif
wdenk81a88242002-10-26 15:22:42 +0000921 if (incrflag)
922 addr += size;
923 }
924 }
925 } while (nbytes);
926
Peter Tyser08007072008-08-15 14:36:32 -0500927 i2c_mm_last_chip = chip;
928 i2c_mm_last_addr = addr;
929 i2c_mm_last_alen = alen;
wdenk81a88242002-10-26 15:22:42 +0000930
931 return 0;
932}
933
Marek Vasut06afa382012-11-12 14:34:27 +0000934/**
935 * do_i2c_probe() - Handle the "i2c probe" command-line command
936 * @cmdtp: Command data struct pointer
937 * @flag: Command flag
938 * @argc: Command-line argument count
939 * @argv: Array of command-line arguments
940 *
941 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
942 * on error.
943 *
wdenk81a88242002-10-26 15:22:42 +0000944 * Syntax:
Eric Nelson54b99e52012-09-23 10:12:56 +0000945 * i2c probe {addr}
946 *
947 * Returns zero (success) if one or more I2C devices was found
wdenk81a88242002-10-26 15:22:42 +0000948 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200949static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000950{
951 int j;
Eric Nelson54b99e52012-09-23 10:12:56 +0000952 int addr = -1;
953 int found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200954#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000955 int k, skip;
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000956 unsigned int bus = GET_BUS_NUM;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400957#endif /* NOPROBES */
Simon Glass63656b72014-12-10 08:55:48 -0700958 int ret;
959#ifdef CONFIG_DM_I2C
960 struct udevice *bus, *dev;
961
962 if (i2c_get_cur_bus(&bus))
963 return CMD_RET_FAILURE;
964#endif
wdenk81a88242002-10-26 15:22:42 +0000965
Eric Nelson54b99e52012-09-23 10:12:56 +0000966 if (argc == 2)
967 addr = simple_strtol(argv[1], 0, 16);
968
wdenk4b9206e2004-03-23 22:14:11 +0000969 puts ("Valid chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600970 for (j = 0; j < 128; j++) {
Eric Nelson54b99e52012-09-23 10:12:56 +0000971 if ((0 <= addr) && (j != addr))
972 continue;
973
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200974#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000975 skip = 0;
Axel Lincfb25cc2013-06-22 21:56:35 +0800976 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600977 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
wdenk81a88242002-10-26 15:22:42 +0000978 skip = 1;
979 break;
980 }
981 }
982 if (skip)
983 continue;
984#endif
Simon Glass63656b72014-12-10 08:55:48 -0700985#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700986 ret = dm_i2c_probe(bus, j, 0, &dev);
Simon Glass63656b72014-12-10 08:55:48 -0700987#else
988 ret = i2c_probe(j);
989#endif
990 if (ret == 0) {
wdenk81a88242002-10-26 15:22:42 +0000991 printf(" %02X", j);
Eric Nelson54b99e52012-09-23 10:12:56 +0000992 found++;
993 }
wdenk81a88242002-10-26 15:22:42 +0000994 }
wdenk4b9206e2004-03-23 22:14:11 +0000995 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000996
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200997#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000998 puts ("Excluded chip addresses:");
Axel Lincfb25cc2013-06-22 21:56:35 +0800999 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -06001000 if (COMPARE_BUS(bus,k))
Ben Warrenbb99ad62006-09-07 16:50:54 -04001001 printf(" %02X", NO_PROBE_ADDR(k));
1002 }
wdenk4b9206e2004-03-23 22:14:11 +00001003 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001004#endif
1005
Eric Nelson54b99e52012-09-23 10:12:56 +00001006 return (0 == found);
wdenk81a88242002-10-26 15:22:42 +00001007}
1008
Marek Vasut06afa382012-11-12 14:34:27 +00001009/**
1010 * do_i2c_loop() - Handle the "i2c loop" command-line command
1011 * @cmdtp: Command data struct pointer
1012 * @flag: Command flag
1013 * @argc: Command-line argument count
1014 * @argv: Array of command-line arguments
1015 *
1016 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1017 * on error.
1018 *
wdenk81a88242002-10-26 15:22:42 +00001019 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -05001020 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
wdenk81a88242002-10-26 15:22:42 +00001021 * {length} - Number of bytes to read
1022 * {delay} - A DECIMAL number and defaults to 1000 uSec
1023 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001024static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +00001025{
Masahiro Yamada54684612014-12-20 03:34:23 +09001026 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -07001027 int alen;
wdenk81a88242002-10-26 15:22:42 +00001028 uint addr;
1029 uint length;
1030 u_char bytes[16];
1031 int delay;
Simon Glass63656b72014-12-10 08:55:48 -07001032 int ret;
1033#ifdef CONFIG_DM_I2C
1034 struct udevice *dev;
1035#endif
wdenk81a88242002-10-26 15:22:42 +00001036
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001037 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001038 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +00001039
1040 /*
1041 * Chip is always specified.
1042 */
1043 chip = simple_strtoul(argv[1], NULL, 16);
1044
1045 /*
1046 * Address is always specified.
1047 */
1048 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -07001049 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +02001050 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001051 return CMD_RET_USAGE;
Simon Glass63656b72014-12-10 08:55:48 -07001052#ifdef CONFIG_DM_I2C
1053 ret = i2c_get_cur_bus_chip(chip, &dev);
1054 if (!ret && alen != -1)
1055 ret = i2c_set_chip_offset_len(dev, alen);
1056 if (ret)
1057 return i2c_report_err(ret, I2C_ERR_WRITE);
1058#endif
wdenk81a88242002-10-26 15:22:42 +00001059
1060 /*
1061 * Length is the number of objects, not number of bytes.
1062 */
1063 length = 1;
1064 length = simple_strtoul(argv[3], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -06001065 if (length > sizeof(bytes))
wdenk81a88242002-10-26 15:22:42 +00001066 length = sizeof(bytes);
wdenk81a88242002-10-26 15:22:42 +00001067
1068 /*
1069 * The delay time (uSec) is optional.
1070 */
1071 delay = 1000;
Timur Tabie857a5b2006-11-28 12:09:35 -06001072 if (argc > 3)
wdenk81a88242002-10-26 15:22:42 +00001073 delay = simple_strtoul(argv[4], NULL, 10);
wdenk81a88242002-10-26 15:22:42 +00001074 /*
1075 * Run the loop...
1076 */
Timur Tabie857a5b2006-11-28 12:09:35 -06001077 while (1) {
Simon Glass63656b72014-12-10 08:55:48 -07001078#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001079 ret = dm_i2c_read(dev, addr, bytes, length);
Simon Glass63656b72014-12-10 08:55:48 -07001080#else
1081 ret = i2c_read(chip, addr, alen, bytes, length);
1082#endif
1083 if (ret)
1084 i2c_report_err(ret, I2C_ERR_READ);
wdenk81a88242002-10-26 15:22:42 +00001085 udelay(delay);
1086 }
1087
1088 /* NOTREACHED */
1089 return 0;
1090}
1091
wdenk81a88242002-10-26 15:22:42 +00001092/*
1093 * The SDRAM command is separately configured because many
1094 * (most?) embedded boards don't use SDRAM DIMMs.
Marek Vasut06afa382012-11-12 14:34:27 +00001095 *
1096 * FIXME: Document and probably move elsewhere!
wdenk81a88242002-10-26 15:22:42 +00001097 */
Jon Loeligerc76fe472007-07-08 18:02:23 -05001098#if defined(CONFIG_CMD_SDRAM)
Larry Johnson632de062008-01-11 23:26:18 -05001099static void print_ddr2_tcyc (u_char const b)
1100{
1101 printf ("%d.", (b >> 4) & 0x0F);
1102 switch (b & 0x0F) {
1103 case 0x0:
1104 case 0x1:
1105 case 0x2:
1106 case 0x3:
1107 case 0x4:
1108 case 0x5:
1109 case 0x6:
1110 case 0x7:
1111 case 0x8:
1112 case 0x9:
1113 printf ("%d ns\n", b & 0x0F);
1114 break;
1115 case 0xA:
1116 puts ("25 ns\n");
1117 break;
1118 case 0xB:
1119 puts ("33 ns\n");
1120 break;
1121 case 0xC:
1122 puts ("66 ns\n");
1123 break;
1124 case 0xD:
1125 puts ("75 ns\n");
1126 break;
1127 default:
1128 puts ("?? ns\n");
1129 break;
1130 }
1131}
1132
1133static void decode_bits (u_char const b, char const *str[], int const do_once)
1134{
1135 u_char mask;
1136
1137 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
1138 if (b & mask) {
1139 puts (*str);
1140 if (do_once)
1141 return;
1142 }
1143 }
1144}
wdenk81a88242002-10-26 15:22:42 +00001145
1146/*
1147 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -05001148 * i2c sdram {i2c_chip}
wdenk81a88242002-10-26 15:22:42 +00001149 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001150static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +00001151{
Michal Simek18c4e7f2016-02-15 11:58:37 +01001152 enum { unknown, EDO, SDRAM, DDR, DDR2, DDR3, DDR4 } type;
Larry Johnson632de062008-01-11 23:26:18 -05001153
Masahiro Yamada54684612014-12-20 03:34:23 +09001154 uint chip;
wdenk81a88242002-10-26 15:22:42 +00001155 u_char data[128];
1156 u_char cksum;
Nobuhiro Iwamatsu28df8ed2017-12-01 14:39:40 +09001157 int j, ret;
1158#ifdef CONFIG_DM_I2C
1159 struct udevice *dev;
1160#endif
wdenk81a88242002-10-26 15:22:42 +00001161
Larry Johnson632de062008-01-11 23:26:18 -05001162 static const char *decode_CAS_DDR2[] = {
1163 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
1164 };
1165
1166 static const char *decode_CAS_default[] = {
1167 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
1168 };
1169
1170 static const char *decode_CS_WE_default[] = {
1171 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
1172 };
1173
1174 static const char *decode_byte21_default[] = {
1175 " TBD (bit 7)\n",
1176 " Redundant row address\n",
1177 " Differential clock input\n",
1178 " Registerd DQMB inputs\n",
1179 " Buffered DQMB inputs\n",
1180 " On-card PLL\n",
1181 " Registered address/control lines\n",
1182 " Buffered address/control lines\n"
1183 };
1184
1185 static const char *decode_byte22_DDR2[] = {
1186 " TBD (bit 7)\n",
1187 " TBD (bit 6)\n",
1188 " TBD (bit 5)\n",
1189 " TBD (bit 4)\n",
1190 " TBD (bit 3)\n",
1191 " Supports partial array self refresh\n",
1192 " Supports 50 ohm ODT\n",
1193 " Supports weak driver\n"
1194 };
1195
1196 static const char *decode_row_density_DDR2[] = {
1197 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
1198 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
1199 };
1200
1201 static const char *decode_row_density_default[] = {
1202 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
1203 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
1204 };
1205
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001206 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001207 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001208
wdenk81a88242002-10-26 15:22:42 +00001209 /*
1210 * Chip is always specified.
Larry Johnson632de062008-01-11 23:26:18 -05001211 */
1212 chip = simple_strtoul (argv[1], NULL, 16);
wdenk81a88242002-10-26 15:22:42 +00001213
Nobuhiro Iwamatsu28df8ed2017-12-01 14:39:40 +09001214#ifdef CONFIG_DM_I2C
1215 ret = i2c_get_cur_bus_chip(chip, &dev);
1216 if (!ret)
1217 ret = dm_i2c_read(dev, 0, data, sizeof(data));
1218#else
1219 ret = i2c_read(chip, 0, 1, data, sizeof(data));
1220#endif
1221 if (ret) {
wdenk4b9206e2004-03-23 22:14:11 +00001222 puts ("No SDRAM Serial Presence Detect found.\n");
wdenk81a88242002-10-26 15:22:42 +00001223 return 1;
1224 }
1225
1226 cksum = 0;
1227 for (j = 0; j < 63; j++) {
1228 cksum += data[j];
1229 }
Timur Tabie857a5b2006-11-28 12:09:35 -06001230 if (cksum != data[63]) {
wdenk81a88242002-10-26 15:22:42 +00001231 printf ("WARNING: Configuration data checksum failure:\n"
Larry Johnson632de062008-01-11 23:26:18 -05001232 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
wdenk81a88242002-10-26 15:22:42 +00001233 }
Larry Johnson632de062008-01-11 23:26:18 -05001234 printf ("SPD data revision %d.%d\n",
wdenk81a88242002-10-26 15:22:42 +00001235 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
Larry Johnson632de062008-01-11 23:26:18 -05001236 printf ("Bytes used 0x%02X\n", data[0]);
1237 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
1238
wdenk4b9206e2004-03-23 22:14:11 +00001239 puts ("Memory type ");
Larry Johnson632de062008-01-11 23:26:18 -05001240 switch (data[2]) {
Larry Johnson0df6b842008-01-10 22:23:39 -05001241 case 2:
1242 type = EDO;
1243 puts ("EDO\n");
1244 break;
1245 case 4:
1246 type = SDRAM;
1247 puts ("SDRAM\n");
1248 break;
Michal Simek18c4e7f2016-02-15 11:58:37 +01001249 case 7:
1250 type = DDR;
1251 puts("DDR\n");
1252 break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001253 case 8:
1254 type = DDR2;
1255 puts ("DDR2\n");
1256 break;
Michal Simek18c4e7f2016-02-15 11:58:37 +01001257 case 11:
1258 type = DDR3;
1259 puts("DDR3\n");
1260 break;
1261 case 12:
1262 type = DDR4;
1263 puts("DDR4\n");
1264 break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001265 default:
1266 type = unknown;
1267 puts ("unknown\n");
1268 break;
wdenk81a88242002-10-26 15:22:42 +00001269 }
Larry Johnson632de062008-01-11 23:26:18 -05001270
wdenk4b9206e2004-03-23 22:14:11 +00001271 puts ("Row address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001272 if ((data[3] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -05001273 printf ("%d\n", data[3] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -06001274 else
Larry Johnson632de062008-01-11 23:26:18 -05001275 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
1276
wdenk4b9206e2004-03-23 22:14:11 +00001277 puts ("Column address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001278 if ((data[4] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -05001279 printf ("%d\n", data[4] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -06001280 else
Larry Johnson632de062008-01-11 23:26:18 -05001281 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001282
1283 switch (type) {
1284 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001285 printf ("Number of ranks %d\n",
1286 (data[5] & 0x07) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001287 break;
1288 default:
Larry Johnson632de062008-01-11 23:26:18 -05001289 printf ("Module rows %d\n", data[5]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001290 break;
1291 }
1292
1293 switch (type) {
1294 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001295 printf ("Module data width %d bits\n", data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001296 break;
1297 default:
Larry Johnson632de062008-01-11 23:26:18 -05001298 printf ("Module data width %d bits\n",
1299 (data[7] << 8) | data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001300 break;
1301 }
1302
wdenk4b9206e2004-03-23 22:14:11 +00001303 puts ("Interface signal levels ");
wdenk81a88242002-10-26 15:22:42 +00001304 switch(data[8]) {
Larry Johnson0df6b842008-01-10 22:23:39 -05001305 case 0: puts ("TTL 5.0 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001306 case 1: puts ("LVTTL\n"); break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001307 case 2: puts ("HSTL 1.5 V\n"); break;
1308 case 3: puts ("SSTL 3.3 V\n"); break;
1309 case 4: puts ("SSTL 2.5 V\n"); break;
1310 case 5: puts ("SSTL 1.8 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001311 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001312 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001313
1314 switch (type) {
1315 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001316 printf ("SDRAM cycle time ");
1317 print_ddr2_tcyc (data[9]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001318 break;
1319 default:
Larry Johnson632de062008-01-11 23:26:18 -05001320 printf ("SDRAM cycle time %d.%d ns\n",
1321 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001322 break;
1323 }
1324
1325 switch (type) {
1326 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001327 printf ("SDRAM access time 0.%d%d ns\n",
1328 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001329 break;
1330 default:
Larry Johnson632de062008-01-11 23:26:18 -05001331 printf ("SDRAM access time %d.%d ns\n",
1332 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001333 break;
1334 }
1335
wdenk4b9206e2004-03-23 22:14:11 +00001336 puts ("EDC configuration ");
Larry Johnson632de062008-01-11 23:26:18 -05001337 switch (data[11]) {
wdenk4b9206e2004-03-23 22:14:11 +00001338 case 0: puts ("None\n"); break;
1339 case 1: puts ("Parity\n"); break;
1340 case 2: puts ("ECC\n"); break;
1341 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001342 }
Larry Johnson632de062008-01-11 23:26:18 -05001343
Timur Tabie857a5b2006-11-28 12:09:35 -06001344 if ((data[12] & 0x80) == 0)
wdenk4b9206e2004-03-23 22:14:11 +00001345 puts ("No self refresh, rate ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001346 else
wdenk4b9206e2004-03-23 22:14:11 +00001347 puts ("Self refresh, rate ");
Larry Johnson632de062008-01-11 23:26:18 -05001348
wdenk81a88242002-10-26 15:22:42 +00001349 switch(data[12] & 0x7F) {
Larry Johnson632de062008-01-11 23:26:18 -05001350 case 0: puts ("15.625 us\n"); break;
1351 case 1: puts ("3.9 us\n"); break;
1352 case 2: puts ("7.8 us\n"); break;
1353 case 3: puts ("31.3 us\n"); break;
1354 case 4: puts ("62.5 us\n"); break;
1355 case 5: puts ("125 us\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001356 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001357 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001358
1359 switch (type) {
1360 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001361 printf ("SDRAM width (primary) %d\n", data[13]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001362 break;
1363 default:
Larry Johnson632de062008-01-11 23:26:18 -05001364 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001365 if ((data[13] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001366 printf (" (second bank) %d\n",
1367 2 * (data[13] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001368 }
1369 break;
wdenk81a88242002-10-26 15:22:42 +00001370 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001371
1372 switch (type) {
1373 case DDR2:
1374 if (data[14] != 0)
Larry Johnson632de062008-01-11 23:26:18 -05001375 printf ("EDC width %d\n", data[14]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001376 break;
1377 default:
1378 if (data[14] != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001379 printf ("EDC width %d\n",
1380 data[14] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001381
1382 if ((data[14] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001383 printf (" (second bank) %d\n",
1384 2 * (data[14] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001385 }
1386 }
1387 break;
1388 }
1389
Larry Johnson632de062008-01-11 23:26:18 -05001390 if (DDR2 != type) {
1391 printf ("Min clock delay, back-to-back random column addresses "
1392 "%d\n", data[15]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001393 }
1394
wdenk4b9206e2004-03-23 22:14:11 +00001395 puts ("Burst length(s) ");
1396 if (data[16] & 0x80) puts (" Page");
1397 if (data[16] & 0x08) puts (" 8");
1398 if (data[16] & 0x04) puts (" 4");
1399 if (data[16] & 0x02) puts (" 2");
1400 if (data[16] & 0x01) puts (" 1");
1401 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001402 printf ("Number of banks %d\n", data[17]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001403
1404 switch (type) {
1405 case DDR2:
1406 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001407 decode_bits (data[18], decode_CAS_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001408 putc ('\n');
1409 break;
1410 default:
1411 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001412 decode_bits (data[18], decode_CAS_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001413 putc ('\n');
1414 break;
1415 }
1416
1417 if (DDR2 != type) {
1418 puts ("CS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001419 decode_bits (data[19], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001420 putc ('\n');
1421 }
1422
1423 if (DDR2 != type) {
1424 puts ("WE latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001425 decode_bits (data[20], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001426 putc ('\n');
1427 }
1428
1429 switch (type) {
1430 case DDR2:
1431 puts ("Module attributes:\n");
1432 if (data[21] & 0x80)
1433 puts (" TBD (bit 7)\n");
1434 if (data[21] & 0x40)
1435 puts (" Analysis probe installed\n");
1436 if (data[21] & 0x20)
1437 puts (" TBD (bit 5)\n");
1438 if (data[21] & 0x10)
1439 puts (" FET switch external enable\n");
Larry Johnson632de062008-01-11 23:26:18 -05001440 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
Larry Johnson0df6b842008-01-10 22:23:39 -05001441 if (data[20] & 0x11) {
Larry Johnson632de062008-01-11 23:26:18 -05001442 printf (" %d active registers on DIMM\n",
1443 (data[21] & 0x03) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001444 }
1445 break;
1446 default:
1447 puts ("Module attributes:\n");
1448 if (!data[21])
1449 puts (" (none)\n");
Larry Johnson632de062008-01-11 23:26:18 -05001450 else
1451 decode_bits (data[21], decode_byte21_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001452 break;
1453 }
1454
1455 switch (type) {
1456 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001457 decode_bits (data[22], decode_byte22_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001458 break;
1459 default:
1460 puts ("Device attributes:\n");
1461 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1462 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1463 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1464 else puts (" Upper Vcc tolerance 10%\n");
1465 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1466 else puts (" Lower Vcc tolerance 10%\n");
1467 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1468 if (data[22] & 0x04) puts (" Supports precharge all\n");
1469 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1470 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1471 break;
1472 }
1473
1474 switch (type) {
1475 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001476 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1477 print_ddr2_tcyc (data[23]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001478 break;
1479 default:
Larry Johnson632de062008-01-11 23:26:18 -05001480 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1481 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001482 break;
1483 }
1484
1485 switch (type) {
1486 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001487 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1488 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001489 break;
1490 default:
Larry Johnson632de062008-01-11 23:26:18 -05001491 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1492 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001493 break;
1494 }
1495
1496 switch (type) {
1497 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001498 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1499 print_ddr2_tcyc (data[25]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001500 break;
1501 default:
Larry Johnson632de062008-01-11 23:26:18 -05001502 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1503 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001504 break;
1505 }
1506
1507 switch (type) {
1508 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001509 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1510 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001511 break;
1512 default:
Larry Johnson632de062008-01-11 23:26:18 -05001513 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1514 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001515 break;
1516 }
1517
1518 switch (type) {
1519 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001520 printf ("Minimum row precharge %d.%02d ns\n",
1521 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001522 break;
1523 default:
Larry Johnson632de062008-01-11 23:26:18 -05001524 printf ("Minimum row precharge %d ns\n", data[27]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001525 break;
1526 }
1527
1528 switch (type) {
1529 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001530 printf ("Row active to row active min %d.%02d ns\n",
1531 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001532 break;
1533 default:
Larry Johnson632de062008-01-11 23:26:18 -05001534 printf ("Row active to row active min %d ns\n", data[28]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001535 break;
1536 }
1537
1538 switch (type) {
1539 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001540 printf ("RAS to CAS delay min %d.%02d ns\n",
1541 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001542 break;
1543 default:
Larry Johnson632de062008-01-11 23:26:18 -05001544 printf ("RAS to CAS delay min %d ns\n", data[29]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001545 break;
1546 }
1547
Larry Johnson632de062008-01-11 23:26:18 -05001548 printf ("Minimum RAS pulse width %d ns\n", data[30]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001549
1550 switch (type) {
1551 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001552 puts ("Density of each row ");
1553 decode_bits (data[31], decode_row_density_DDR2, 1);
1554 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001555 break;
1556 default:
Larry Johnson632de062008-01-11 23:26:18 -05001557 puts ("Density of each row ");
1558 decode_bits (data[31], decode_row_density_default, 1);
1559 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001560 break;
1561 }
1562
1563 switch (type) {
1564 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001565 puts ("Command and Address setup ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001566 if (data[32] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001567 printf ("1.%d%d ns\n",
1568 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001569 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001570 printf ("0.%d%d ns\n",
1571 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001572 }
1573 break;
1574 default:
Larry Johnson632de062008-01-11 23:26:18 -05001575 printf ("Command and Address setup %c%d.%d ns\n",
1576 (data[32] & 0x80) ? '-' : '+',
1577 (data[32] >> 4) & 0x07, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001578 break;
1579 }
1580
1581 switch (type) {
1582 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001583 puts ("Command and Address hold ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001584 if (data[33] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001585 printf ("1.%d%d ns\n",
1586 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001587 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001588 printf ("0.%d%d ns\n",
1589 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001590 }
1591 break;
1592 default:
Larry Johnson632de062008-01-11 23:26:18 -05001593 printf ("Command and Address hold %c%d.%d ns\n",
1594 (data[33] & 0x80) ? '-' : '+',
1595 (data[33] >> 4) & 0x07, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001596 break;
1597 }
1598
1599 switch (type) {
1600 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001601 printf ("Data signal input setup 0.%d%d ns\n",
1602 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001603 break;
1604 default:
Larry Johnson632de062008-01-11 23:26:18 -05001605 printf ("Data signal input setup %c%d.%d ns\n",
1606 (data[34] & 0x80) ? '-' : '+',
1607 (data[34] >> 4) & 0x07, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001608 break;
1609 }
1610
1611 switch (type) {
1612 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001613 printf ("Data signal input hold 0.%d%d ns\n",
1614 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001615 break;
1616 default:
Larry Johnson632de062008-01-11 23:26:18 -05001617 printf ("Data signal input hold %c%d.%d ns\n",
1618 (data[35] & 0x80) ? '-' : '+',
1619 (data[35] >> 4) & 0x07, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001620 break;
1621 }
1622
wdenk4b9206e2004-03-23 22:14:11 +00001623 puts ("Manufacturer's JEDEC ID ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001624 for (j = 64; j <= 71; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001625 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001626 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001627 printf ("Manufacturing Location %02X\n", data[72]);
wdenk4b9206e2004-03-23 22:14:11 +00001628 puts ("Manufacturer's Part Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001629 for (j = 73; j <= 90; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001630 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001631 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001632 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1633 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
wdenk4b9206e2004-03-23 22:14:11 +00001634 puts ("Assembly Serial Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001635 for (j = 95; j <= 98; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001636 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001637 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001638
Larry Johnson0df6b842008-01-10 22:23:39 -05001639 if (DDR2 != type) {
Larry Johnson632de062008-01-11 23:26:18 -05001640 printf ("Speed rating PC%d\n",
1641 data[126] == 0x66 ? 66 : data[126]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001642 }
wdenk81a88242002-10-26 15:22:42 +00001643 return 0;
1644}
Jon Loeliger90253172007-07-10 11:02:44 -05001645#endif
wdenk81a88242002-10-26 15:22:42 +00001646
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001647/*
1648 * Syntax:
1649 * i2c edid {i2c_chip}
1650 */
1651#if defined(CONFIG_I2C_EDID)
1652int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1653{
Masahiro Yamada54684612014-12-20 03:34:23 +09001654 uint chip;
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001655 struct edid1_info edid;
Simon Glass63656b72014-12-10 08:55:48 -07001656 int ret;
1657#ifdef CONFIG_DM_I2C
1658 struct udevice *dev;
1659#endif
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001660
1661 if (argc < 2) {
1662 cmd_usage(cmdtp);
1663 return 1;
1664 }
1665
1666 chip = simple_strtoul(argv[1], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -07001667#ifdef CONFIG_DM_I2C
1668 ret = i2c_get_cur_bus_chip(chip, &dev);
1669 if (!ret)
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001670 ret = dm_i2c_read(dev, 0, (uchar *)&edid, sizeof(edid));
Simon Glass63656b72014-12-10 08:55:48 -07001671#else
1672 ret = i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid));
1673#endif
1674 if (ret)
1675 return i2c_report_err(ret, I2C_ERR_READ);
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001676
1677 if (edid_check_info(&edid)) {
1678 puts("Content isn't valid EDID.\n");
1679 return 1;
1680 }
1681
1682 edid_print_info(&edid);
1683 return 0;
1684
1685}
1686#endif /* CONFIG_I2C_EDID */
1687
Simon Glass59aa9df2015-05-04 11:30:57 -06001688#ifdef CONFIG_DM_I2C
1689static void show_bus(struct udevice *bus)
1690{
1691 struct udevice *dev;
1692
1693 printf("Bus %d:\t%s", bus->req_seq, bus->name);
1694 if (device_active(bus))
1695 printf(" (active %d)", bus->seq);
1696 printf("\n");
1697 for (device_find_first_child(bus, &dev);
1698 dev;
1699 device_find_next_child(&dev)) {
1700 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
1701
1702 printf(" %02x: %s, offset len %x, flags %x\n",
1703 chip->chip_addr, dev->name, chip->offset_len,
1704 chip->flags);
1705 }
1706}
1707#endif
1708
Marek Vasut06afa382012-11-12 14:34:27 +00001709/**
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001710 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
Marek Vasut06afa382012-11-12 14:34:27 +00001711 * @cmdtp: Command data struct pointer
1712 * @flag: Command flag
1713 * @argc: Command-line argument count
1714 * @argv: Array of command-line arguments
1715 *
1716 * Returns zero always.
1717 */
Simon Glass59aa9df2015-05-04 11:30:57 -06001718#if defined(CONFIG_SYS_I2C) || defined(CONFIG_DM_I2C)
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001719static int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc,
1720 char * const argv[])
Heiko Schocher67b23a32008-10-15 09:39:47 +02001721{
Heiko Schocher67b23a32008-10-15 09:39:47 +02001722 if (argc == 1) {
1723 /* show all busses */
Simon Glass59aa9df2015-05-04 11:30:57 -06001724#ifdef CONFIG_DM_I2C
1725 struct udevice *bus;
1726 struct uclass *uc;
1727 int ret;
1728
1729 ret = uclass_get(UCLASS_I2C, &uc);
1730 if (ret)
1731 return CMD_RET_FAILURE;
1732 uclass_foreach_dev(bus, uc)
1733 show_bus(bus);
1734#else
1735 int i;
1736
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001737 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1738 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1739#ifndef CONFIG_SYS_I2C_DIRECT_BUS
Simon Glass59aa9df2015-05-04 11:30:57 -06001740 int j;
1741
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001742 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1743 if (i2c_bus[i].next_hop[j].chip == 0)
1744 break;
1745 printf("->%s@0x%2x:%d",
1746 i2c_bus[i].next_hop[j].mux.name,
1747 i2c_bus[i].next_hop[j].chip,
1748 i2c_bus[i].next_hop[j].channel);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001749 }
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001750#endif
1751 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001752 }
Simon Glass59aa9df2015-05-04 11:30:57 -06001753#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001754 } else {
Simon Glass59aa9df2015-05-04 11:30:57 -06001755 int i;
1756
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001757 /* show specific bus */
1758 i = simple_strtoul(argv[1], NULL, 10);
Simon Glass59aa9df2015-05-04 11:30:57 -06001759#ifdef CONFIG_DM_I2C
1760 struct udevice *bus;
1761 int ret;
1762
1763 ret = uclass_get_device_by_seq(UCLASS_I2C, i, &bus);
1764 if (ret) {
1765 printf("Invalid bus %d: err=%d\n", i, ret);
1766 return CMD_RET_FAILURE;
1767 }
1768 show_bus(bus);
1769#else
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001770 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1771 printf("Invalid bus %d\n", i);
1772 return -1;
1773 }
1774 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1775#ifndef CONFIG_SYS_I2C_DIRECT_BUS
Simon Glass59aa9df2015-05-04 11:30:57 -06001776 int j;
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001777 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1778 if (i2c_bus[i].next_hop[j].chip == 0)
1779 break;
1780 printf("->%s@0x%2x:%d",
1781 i2c_bus[i].next_hop[j].mux.name,
1782 i2c_bus[i].next_hop[j].chip,
1783 i2c_bus[i].next_hop[j].channel);
1784 }
1785#endif
1786 printf("\n");
Simon Glass59aa9df2015-05-04 11:30:57 -06001787#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001788 }
Heiko Schocher67b23a32008-10-15 09:39:47 +02001789
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001790 return 0;
1791}
1792#endif
1793
Marek Vasut06afa382012-11-12 14:34:27 +00001794/**
1795 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1796 * @cmdtp: Command data struct pointer
1797 * @flag: Command flag
1798 * @argc: Command-line argument count
1799 * @argv: Array of command-line arguments
1800 *
1801 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1802 * on error.
1803 */
Simon Glass63656b72014-12-10 08:55:48 -07001804#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) || \
1805 defined(CONFIG_DM_I2C)
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001806static int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc,
1807 char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001808{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001809 int ret = 0;
Simon Glass63656b72014-12-10 08:55:48 -07001810 int bus_no;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001811
Simon Glass63656b72014-12-10 08:55:48 -07001812 if (argc == 1) {
Timur Tabie857a5b2006-11-28 12:09:35 -06001813 /* querying current setting */
Simon Glass63656b72014-12-10 08:55:48 -07001814#ifdef CONFIG_DM_I2C
1815 struct udevice *bus;
1816
1817 if (!i2c_get_cur_bus(&bus))
1818 bus_no = bus->seq;
1819 else
1820 bus_no = -1;
1821#else
1822 bus_no = i2c_get_bus_num();
1823#endif
1824 printf("Current bus is %d\n", bus_no);
1825 } else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001826 bus_no = simple_strtoul(argv[1], NULL, 10);
Heiko Schocher880a4122013-08-23 09:39:16 +02001827#if defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001828 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1829 printf("Invalid bus %d\n", bus_no);
1830 return -1;
1831 }
Heiko Schocher880a4122013-08-23 09:39:16 +02001832#endif
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001833 printf("Setting bus to %d\n", bus_no);
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001834#ifdef CONFIG_DM_I2C
1835 ret = cmd_i2c_set_bus_num(bus_no);
1836#else
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001837 ret = i2c_set_bus_num(bus_no);
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001838#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001839 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001840 printf("Failure changing bus number (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001841 }
Simon Glass4fbd2582015-12-29 05:22:50 -07001842
1843 return ret ? CMD_RET_FAILURE : 0;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001844}
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001845#endif /* defined(CONFIG_SYS_I2C) */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001846
Marek Vasut06afa382012-11-12 14:34:27 +00001847/**
1848 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1849 * @cmdtp: Command data struct pointer
1850 * @flag: Command flag
1851 * @argc: Command-line argument count
1852 * @argv: Array of command-line arguments
1853 *
1854 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1855 * on error.
1856 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001857static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001858{
1859 int speed, ret=0;
1860
Simon Glass63656b72014-12-10 08:55:48 -07001861#ifdef CONFIG_DM_I2C
1862 struct udevice *bus;
1863
1864 if (i2c_get_cur_bus(&bus))
1865 return 1;
1866#endif
1867 if (argc == 1) {
1868#ifdef CONFIG_DM_I2C
Simon Glassca88b9b2015-02-05 21:41:32 -07001869 speed = dm_i2c_get_bus_speed(bus);
Simon Glass63656b72014-12-10 08:55:48 -07001870#else
1871 speed = i2c_get_bus_speed();
1872#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001873 /* querying current speed */
Simon Glass63656b72014-12-10 08:55:48 -07001874 printf("Current bus speed=%d\n", speed);
1875 } else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001876 speed = simple_strtoul(argv[1], NULL, 10);
1877 printf("Setting bus speed to %d Hz\n", speed);
Simon Glass63656b72014-12-10 08:55:48 -07001878#ifdef CONFIG_DM_I2C
Simon Glassca88b9b2015-02-05 21:41:32 -07001879 ret = dm_i2c_set_bus_speed(bus, speed);
Simon Glass63656b72014-12-10 08:55:48 -07001880#else
Ben Warrenbb99ad62006-09-07 16:50:54 -04001881 ret = i2c_set_bus_speed(speed);
Simon Glass63656b72014-12-10 08:55:48 -07001882#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001883 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001884 printf("Failure changing bus speed (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001885 }
Simon Glass4fbd2582015-12-29 05:22:50 -07001886
1887 return ret ? CMD_RET_FAILURE : 0;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001888}
1889
Marek Vasut06afa382012-11-12 14:34:27 +00001890/**
1891 * do_i2c_mm() - Handle the "i2c mm" command-line command
1892 * @cmdtp: Command data struct pointer
1893 * @flag: Command flag
1894 * @argc: Command-line argument count
1895 * @argv: Array of command-line arguments
1896 *
1897 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1898 * on error.
1899 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001900static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001901{
1902 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1903}
1904
Marek Vasut06afa382012-11-12 14:34:27 +00001905/**
1906 * do_i2c_nm() - Handle the "i2c nm" command-line command
1907 * @cmdtp: Command data struct pointer
1908 * @flag: Command flag
1909 * @argc: Command-line argument count
1910 * @argv: Array of command-line arguments
1911 *
1912 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1913 * on error.
1914 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001915static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001916{
1917 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1918}
1919
Marek Vasut06afa382012-11-12 14:34:27 +00001920/**
1921 * do_i2c_reset() - Handle the "i2c reset" command-line command
1922 * @cmdtp: Command data struct pointer
1923 * @flag: Command flag
1924 * @argc: Command-line argument count
1925 * @argv: Array of command-line arguments
1926 *
1927 * Returns zero always.
1928 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001929static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001930{
Simon Glass63656b72014-12-10 08:55:48 -07001931#if defined(CONFIG_DM_I2C)
1932 struct udevice *bus;
1933
1934 if (i2c_get_cur_bus(&bus))
1935 return CMD_RET_FAILURE;
1936 if (i2c_deblock(bus)) {
1937 printf("Error: Not supported by the driver\n");
1938 return CMD_RET_FAILURE;
1939 }
1940#elif defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001941 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1942#else
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001943 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001944#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001945 return 0;
1946}
1947
1948static cmd_tbl_t cmd_i2c_sub[] = {
Simon Glass59aa9df2015-05-04 11:30:57 -06001949#if defined(CONFIG_SYS_I2C) || defined(CONFIG_DM_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001950 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001951#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001952 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001953#if defined(CONFIG_SYS_I2C) || \
Simon Glass63656b72014-12-10 08:55:48 -07001954 defined(CONFIG_I2C_MULTI_BUS) || defined(CONFIG_DM_I2C)
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001955 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1956#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001957#if defined(CONFIG_I2C_EDID)
1958 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1959#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001960 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1961 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1962 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1963 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1964 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1965 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001966 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
Lubomir Popoved16f142015-01-30 19:56:04 +02001967 U_BOOT_CMD_MKENT(write, 6, 0, do_i2c_write, "", ""),
Simon Glass63656b72014-12-10 08:55:48 -07001968#ifdef CONFIG_DM_I2C
1969 U_BOOT_CMD_MKENT(flags, 2, 1, do_i2c_flags, "", ""),
Simon Glassc10c8e32015-08-22 18:31:33 -06001970 U_BOOT_CMD_MKENT(olen, 2, 1, do_i2c_olen, "", ""),
Simon Glass63656b72014-12-10 08:55:48 -07001971#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001972 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1973#if defined(CONFIG_CMD_SDRAM)
1974 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1975#endif
1976 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1977};
1978
Michal Simeke4099c82015-12-04 16:56:57 +01001979static __maybe_unused void i2c_reloc(void)
1980{
1981 static int relocated;
1982
1983 if (!relocated) {
1984 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1985 relocated = 1;
1986 };
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001987}
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001988
Marek Vasut06afa382012-11-12 14:34:27 +00001989/**
1990 * do_i2c() - Handle the "i2c" command-line command
1991 * @cmdtp: Command data struct pointer
1992 * @flag: Command flag
1993 * @argc: Command-line argument count
1994 * @argv: Array of command-line arguments
1995 *
1996 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1997 * on error.
1998 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001999static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04002000{
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01002001 cmd_tbl_t *c;
2002
Michal Simeke4099c82015-12-04 16:56:57 +01002003#ifdef CONFIG_NEEDS_MANUAL_RELOC
2004 i2c_reloc();
2005#endif
2006
Heiko Schocher4444b222010-09-17 13:10:35 +02002007 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00002008 return CMD_RET_USAGE;
Heiko Schocher4444b222010-09-17 13:10:35 +02002009
Peter Tysere96ad5d2009-04-18 22:34:04 -05002010 /* Strip off leading 'i2c' command argument */
2011 argc--;
2012 argv++;
2013
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01002014 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
2015
Wolfgang Denk47e26b12010-07-17 01:06:04 +02002016 if (c)
Simon Glass4c12eeb2011-12-10 08:44:01 +00002017 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +02002018 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00002019 return CMD_RET_USAGE;
Ben Warrenbb99ad62006-09-07 16:50:54 -04002020}
wdenk8bde7f72003-06-27 21:31:46 +00002021
2022/***************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00002023#ifdef CONFIG_SYS_LONGHELP
2024static char i2c_help_text[] =
Simon Glass59aa9df2015-05-04 11:30:57 -06002025#if defined(CONFIG_SYS_I2C) || defined(CONFIG_DM_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00002026 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
Heiko Schocher9a2accb2012-10-25 10:32:14 +02002027#endif
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01002028 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
Heiko Schocher3f4978c2012-01-16 21:12:24 +00002029#if defined(CONFIG_SYS_I2C) || \
Simon Glass63656b72014-12-10 08:55:48 -07002030 defined(CONFIG_I2C_MULTI_BUS) || defined(CONFIG_DM_I2C)
Peter Tyser9bc2e4e2008-10-01 12:25:04 -05002031 "i2c dev [dev] - show or set current I2C bus\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01002032#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00002033#if defined(CONFIG_I2C_EDID)
2034 "i2c edid chip - print EDID configuration information\n"
2035#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01002036 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01002037 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
2038 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
2039 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
2040 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
Eric Nelson54b99e52012-09-23 10:12:56 +00002041 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
Simon Glass63656b72014-12-10 08:55:48 -07002042 "i2c read chip address[.0, .1, .2] length memaddress - read to memory\n"
Lubomir Popoved16f142015-01-30 19:56:04 +02002043 "i2c write memaddress chip address[.0, .1, .2] length [-s] - write memory\n"
2044 " to I2C; the -s option selects bulk write in a single transaction\n"
Simon Glass63656b72014-12-10 08:55:48 -07002045#ifdef CONFIG_DM_I2C
2046 "i2c flags chip [flags] - set or get chip flags\n"
Simon Glassc10c8e32015-08-22 18:31:33 -06002047 "i2c olen chip [offset_length] - set or get chip offset length\n"
Simon Glass63656b72014-12-10 08:55:48 -07002048#endif
Heiko Schochere43a27c2008-10-15 09:33:30 +02002049 "i2c reset - re-init the I2C Controller\n"
Jon Loeligerc76fe472007-07-08 18:02:23 -05002050#if defined(CONFIG_CMD_SDRAM)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01002051 "i2c sdram chip - print SDRAM configuration information\n"
Jon Loeliger90253172007-07-10 11:02:44 -05002052#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00002053 "i2c speed [speed] - show or set I2C bus speed";
2054#endif
2055
2056U_BOOT_CMD(
Lubomir Popoved16f142015-01-30 19:56:04 +02002057 i2c, 7, 1, do_i2c,
Kim Phillips088f1b12012-10-29 13:34:31 +00002058 "I2C sub-system",
2059 i2c_help_text
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01002060);