blob: e0f8ece597c556d518697ff91d610632d1c3e2e0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk81a88242002-10-26 15:22:42 +00002/*
Heiko Schocher3f4978c2012-01-16 21:12:24 +00003 * (C) Copyright 2009
4 * Sergey Kubushyn, himself, ksi@koi8.net
5 *
6 * Changes for unified multibus/multiadapter I2C support.
7 *
wdenk81a88242002-10-26 15:22:42 +00008 * (C) Copyright 2001
9 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
wdenk81a88242002-10-26 15:22:42 +000010 */
11
12/*
13 * I2C Functions similar to the standard memory functions.
14 *
15 * There are several parameters in many of the commands that bear further
16 * explanations:
17 *
wdenk81a88242002-10-26 15:22:42 +000018 * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
19 * Each I2C chip on the bus has a unique address. On the I2C data bus,
20 * the address is the upper seven bits and the LSB is the "read/write"
21 * bit. Note that the {i2c_chip} address specified on the command
22 * line is not shifted up: e.g. a typical EEPROM memory chip may have
23 * an I2C address of 0x50, but the data put on the bus will be 0xA0
24 * for write and 0xA1 for read. This "non shifted" address notation
25 * matches at least half of the data sheets :-/.
26 *
27 * {addr} is the address (or offset) within the chip. Small memory
28 * chips have 8 bit addresses. Large memory chips have 16 bit
29 * addresses. Other memory chips have 9, 10, or 11 bit addresses.
30 * Many non-memory chips have multiple registers and {addr} is used
31 * as the register index. Some non-memory chips have only one register
32 * and therefore don't need any {addr} parameter.
33 *
34 * The default {addr} parameter is one byte (.1) which works well for
35 * memories and registers with 8 bits of address space.
36 *
37 * You can specify the length of the {addr} field with the optional .0,
38 * .1, or .2 modifier (similar to the .b, .w, .l modifier). If you are
39 * manipulating a single register device which doesn't use an address
40 * field, use "0.0" for the address and the ".0" length field will
41 * suppress the address in the I2C data stream. This also works for
42 * successive reads using the I2C auto-incrementing memory pointer.
43 *
44 * If you are manipulating a large memory with 2-byte addresses, use
45 * the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
46 *
47 * Then there are the unfortunate memory chips that spill the most
48 * significant 1, 2, or 3 bits of address into the chip address byte.
49 * This effectively makes one chip (logically) look like 2, 4, or
50 * 8 chips. This is handled (awkwardly) by #defining
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051 * CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
wdenk81a88242002-10-26 15:22:42 +000052 * {addr} field (since .1 is the default, it doesn't actually have to
53 * be specified). Examples: given a memory chip at I2C chip address
54 * 0x50, the following would happen...
Peter Tyser0f89c542009-04-18 22:34:03 -050055 * i2c md 50 0 10 display 16 bytes starting at 0x000
wdenk81a88242002-10-26 15:22:42 +000056 * On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050057 * i2c md 50 100 10 display 16 bytes starting at 0x100
wdenk81a88242002-10-26 15:22:42 +000058 * On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
Peter Tyser0f89c542009-04-18 22:34:03 -050059 * i2c md 50 210 10 display 16 bytes starting at 0x210
wdenk81a88242002-10-26 15:22:42 +000060 * On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
61 * This is awfully ugly. It would be nice if someone would think up
62 * a better way of handling this.
63 *
64 * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
65 */
66
67#include <common.h>
Simon Glass0098e172014-04-10 20:01:30 -060068#include <bootretry.h>
Simon Glass18d66532014-04-10 20:01:25 -060069#include <cli.h>
wdenk81a88242002-10-26 15:22:42 +000070#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070071#include <console.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>
Simon Glass63656b72014-12-10 08:55:48 -070074#include <errno.h>
wdenk81a88242002-10-26 15:22:42 +000075#include <i2c.h>
Heiko Schocher67b23a32008-10-15 09:39:47 +020076#include <malloc.h>
wdenk81a88242002-10-26 15:22:42 +000077#include <asm/byteorder.h>
Marek Vasut2515d842012-11-12 14:34:25 +000078#include <linux/compiler.h>
wdenk81a88242002-10-26 15:22:42 +000079
wdenk81a88242002-10-26 15:22:42 +000080/* Display values from last command.
81 * Memory modify remembered values are different from display memory.
82 */
Masahiro Yamada54684612014-12-20 03:34:23 +090083static uint i2c_dp_last_chip;
wdenk81a88242002-10-26 15:22:42 +000084static uint i2c_dp_last_addr;
85static uint i2c_dp_last_alen;
86static uint i2c_dp_last_length = 0x10;
87
Masahiro Yamada54684612014-12-20 03:34:23 +090088static uint i2c_mm_last_chip;
wdenk81a88242002-10-26 15:22:42 +000089static uint i2c_mm_last_addr;
90static uint i2c_mm_last_alen;
91
Ben Warrenbb99ad62006-09-07 16:50:54 -040092/* If only one I2C bus is present, the list of devices to ignore when
93 * the probe command is issued is represented by a 1D array of addresses.
94 * When multiple buses are present, the list is an array of bus-address
95 * pairs. The following macros take care of this */
96
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#if defined(CONFIG_SYS_I2C_NOPROBES)
Heiko Schocher9a2accb2012-10-25 10:32:14 +020098#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
Ben Warrenbb99ad62006-09-07 16:50:54 -040099static struct
100{
101 uchar bus;
102 uchar addr;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200103} i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400104#define GET_BUS_NUM i2c_get_bus_num()
105#define COMPARE_BUS(b,i) (i2c_no_probes[(i)].bus == (b))
106#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)].addr == (a))
107#define NO_PROBE_ADDR(i) i2c_no_probes[(i)].addr
108#else /* single bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200109static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400110#define GET_BUS_NUM 0
111#define COMPARE_BUS(b,i) ((b) == 0) /* Make compiler happy */
112#define COMPARE_ADDR(a,i) (i2c_no_probes[(i)] == (a))
113#define NO_PROBE_ADDR(i) i2c_no_probes[(i)]
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000114#endif /* defined(CONFIG_SYS_I2C) */
Heiko Schocher67b23a32008-10-15 09:39:47 +0200115#endif
116
Frans Meulenbroeksa266fe92010-03-26 09:46:40 +0100117#define DISP_LINE_LEN 16
118
Simon Glass63656b72014-12-10 08:55:48 -0700119/*
120 * Default for driver model is to use the chip's existing address length.
121 * For legacy code, this is not stored, so we need to use a suitable
122 * default.
123 */
124#ifdef CONFIG_DM_I2C
125#define DEFAULT_ADDR_LEN (-1)
126#else
127#define DEFAULT_ADDR_LEN 1
128#endif
129
130#ifdef CONFIG_DM_I2C
131static struct udevice *i2c_cur_bus;
132
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700133static int cmd_i2c_set_bus_num(unsigned int busnum)
Simon Glass63656b72014-12-10 08:55:48 -0700134{
135 struct udevice *bus;
136 int ret;
137
138 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
139 if (ret) {
140 debug("%s: No bus %d\n", __func__, busnum);
141 return ret;
142 }
143 i2c_cur_bus = bus;
144
145 return 0;
146}
147
148static int i2c_get_cur_bus(struct udevice **busp)
149{
Lukasz Majewskie46f8a32017-03-21 12:08:25 +0100150#ifdef CONFIG_I2C_SET_DEFAULT_BUS_NUM
151 if (!i2c_cur_bus) {
152 if (cmd_i2c_set_bus_num(CONFIG_I2C_DEFAULT_BUS_NUMBER)) {
153 printf("Default I2C bus %d not found\n",
154 CONFIG_I2C_DEFAULT_BUS_NUMBER);
155 return -ENODEV;
156 }
157 }
158#endif
159
Simon Glass63656b72014-12-10 08:55:48 -0700160 if (!i2c_cur_bus) {
161 puts("No I2C bus selected\n");
162 return -ENODEV;
163 }
164 *busp = i2c_cur_bus;
165
166 return 0;
167}
168
169static int i2c_get_cur_bus_chip(uint chip_addr, struct udevice **devp)
170{
171 struct udevice *bus;
172 int ret;
173
174 ret = i2c_get_cur_bus(&bus);
175 if (ret)
176 return ret;
177
Simon Glass25ab4b02015-01-25 08:26:55 -0700178 return i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glass63656b72014-12-10 08:55:48 -0700179}
180
181#endif
182
Marek Vasut06afa382012-11-12 14:34:27 +0000183/**
184 * i2c_init_board() - Board-specific I2C bus init
185 *
186 * This function is the default no-op implementation of I2C bus
Robert P. J. Day62a3b7d2016-07-15 13:44:45 -0400187 * initialization. This function can be overridden by board-specific
Marek Vasut06afa382012-11-12 14:34:27 +0000188 * implementation if needed.
189 */
Marek Vasut2515d842012-11-12 14:34:25 +0000190__weak
191void i2c_init_board(void)
Stefan Biglerc649dda2011-04-08 16:24:08 +0200192{
Stefan Biglerc649dda2011-04-08 16:24:08 +0200193}
Stefan Biglerc649dda2011-04-08 16:24:08 +0200194
Peter Tyser655b34a2009-04-18 22:34:01 -0500195/* TODO: Implement architecture-specific get/set functions */
Marek Vasut06afa382012-11-12 14:34:27 +0000196
197/**
198 * i2c_get_bus_speed() - Return I2C bus speed
199 *
200 * This function is the default implementation of function for retrieveing
201 * the current I2C bus speed in Hz.
202 *
203 * A driver implementing runtime switching of I2C bus speed must override
204 * this function to report the speed correctly. Simple or legacy drivers
205 * can use this fallback.
206 *
207 * Returns I2C bus speed in Hz.
208 */
Simon Glass63656b72014-12-10 08:55:48 -0700209#if !defined(CONFIG_SYS_I2C) && !defined(CONFIG_DM_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000210/*
211 * TODO: Implement architecture-specific get/set functions
212 * Should go away, if we switched completely to new multibus support
213 */
Marek Vasut2515d842012-11-12 14:34:25 +0000214__weak
215unsigned int i2c_get_bus_speed(void)
Peter Tyser655b34a2009-04-18 22:34:01 -0500216{
217 return CONFIG_SYS_I2C_SPEED;
218}
Peter Tyser655b34a2009-04-18 22:34:01 -0500219
Marek Vasut06afa382012-11-12 14:34:27 +0000220/**
221 * i2c_set_bus_speed() - Configure I2C bus speed
222 * @speed: Newly set speed of the I2C bus in Hz
223 *
224 * This function is the default implementation of function for setting
225 * the I2C bus speed in Hz.
226 *
227 * A driver implementing runtime switching of I2C bus speed must override
228 * this function to report the speed correctly. Simple or legacy drivers
229 * can use this fallback.
230 *
231 * Returns zero on success, negative value on error.
232 */
Marek Vasut2515d842012-11-12 14:34:25 +0000233__weak
234int i2c_set_bus_speed(unsigned int speed)
Peter Tyser655b34a2009-04-18 22:34:01 -0500235{
236 if (speed != CONFIG_SYS_I2C_SPEED)
237 return -1;
238
239 return 0;
240}
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000241#endif
Peter Tyser655b34a2009-04-18 22:34:01 -0500242
Marek Vasut06afa382012-11-12 14:34:27 +0000243/**
244 * get_alen() - Small parser helper function to get address length
245 *
246 * Returns the address length.
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100247 */
Simon Glass63656b72014-12-10 08:55:48 -0700248static uint get_alen(char *arg, int default_len)
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100249{
250 int j;
251 int alen;
252
Simon Glass63656b72014-12-10 08:55:48 -0700253 alen = default_len;
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100254 for (j = 0; j < 8; j++) {
255 if (arg[j] == '.') {
256 alen = arg[j+1] - '0';
Frans Meulenbroeks2c0dc992010-03-26 09:46:41 +0100257 break;
258 } else if (arg[j] == '\0')
259 break;
260 }
261 return alen;
262}
263
Simon Glassc1a6f372014-11-11 10:46:17 -0700264enum i2c_err_op {
265 I2C_ERR_READ,
266 I2C_ERR_WRITE,
267};
268
269static int i2c_report_err(int ret, enum i2c_err_op op)
270{
271 printf("Error %s the chip: %d\n",
272 op == I2C_ERR_READ ? "reading" : "writing", ret);
273
274 return CMD_RET_FAILURE;
275}
276
Marek Vasut06afa382012-11-12 14:34:27 +0000277/**
278 * do_i2c_read() - Handle the "i2c read" command-line command
279 * @cmdtp: Command data struct pointer
280 * @flag: Command flag
281 * @argc: Command-line argument count
282 * @argv: Array of command-line arguments
283 *
284 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
285 * on error.
286 *
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100287 * Syntax:
288 * i2c read {i2c_chip} {devaddr}{.0, .1, .2} {len} {memaddr}
289 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200290static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100291{
Masahiro Yamada54684612014-12-20 03:34:23 +0900292 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -0700293 uint devaddr, length;
294 int alen;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100295 u_char *memaddr;
Simon Glass63656b72014-12-10 08:55:48 -0700296 int ret;
297#ifdef CONFIG_DM_I2C
298 struct udevice *dev;
299#endif
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100300
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200301 if (argc != 5)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000302 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100303
304 /*
305 * I2C chip address
306 */
307 chip = simple_strtoul(argv[1], NULL, 16);
308
309 /*
310 * I2C data address within the chip. This can be 1 or
311 * 2 bytes long. Some day it might be 3 bytes long :-).
312 */
313 devaddr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700314 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200315 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000316 return CMD_RET_USAGE;
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100317
318 /*
319 * Length is the number of objects, not number of bytes.
320 */
321 length = simple_strtoul(argv[3], NULL, 16);
322
323 /*
324 * memaddr is the address where to store things in memory
325 */
326 memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
327
Simon Glass63656b72014-12-10 08:55:48 -0700328#ifdef CONFIG_DM_I2C
329 ret = i2c_get_cur_bus_chip(chip, &dev);
330 if (!ret && alen != -1)
331 ret = i2c_set_chip_offset_len(dev, alen);
332 if (!ret)
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700333 ret = dm_i2c_read(dev, devaddr, memaddr, length);
Simon Glass63656b72014-12-10 08:55:48 -0700334#else
335 ret = i2c_read(chip, devaddr, alen, memaddr, length);
336#endif
337 if (ret)
338 return i2c_report_err(ret, I2C_ERR_READ);
339
Frans Meulenbroeks652e5352010-02-25 10:12:16 +0100340 return 0;
341}
342
York Sunff5d2dc2012-09-16 08:02:30 +0000343static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
344{
Masahiro Yamada54684612014-12-20 03:34:23 +0900345 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -0700346 uint devaddr, length;
347 int alen;
York Sunff5d2dc2012-09-16 08:02:30 +0000348 u_char *memaddr;
Simon Glass63656b72014-12-10 08:55:48 -0700349 int ret;
350#ifdef CONFIG_DM_I2C
351 struct udevice *dev;
Lubomir Popoved16f142015-01-30 19:56:04 +0200352 struct dm_i2c_chip *i2c_chip;
Simon Glass63656b72014-12-10 08:55:48 -0700353#endif
York Sunff5d2dc2012-09-16 08:02:30 +0000354
Lubomir Popoved16f142015-01-30 19:56:04 +0200355 if ((argc < 5) || (argc > 6))
York Sunff5d2dc2012-09-16 08:02:30 +0000356 return cmd_usage(cmdtp);
357
358 /*
359 * memaddr is the address where to store things in memory
360 */
361 memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
362
363 /*
364 * I2C chip address
365 */
366 chip = simple_strtoul(argv[2], NULL, 16);
367
368 /*
369 * I2C data address within the chip. This can be 1 or
370 * 2 bytes long. Some day it might be 3 bytes long :-).
371 */
372 devaddr = simple_strtoul(argv[3], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700373 alen = get_alen(argv[3], DEFAULT_ADDR_LEN);
York Sunff5d2dc2012-09-16 08:02:30 +0000374 if (alen > 3)
375 return cmd_usage(cmdtp);
376
377 /*
Lubomir Popoved16f142015-01-30 19:56:04 +0200378 * Length is the number of bytes.
York Sunff5d2dc2012-09-16 08:02:30 +0000379 */
380 length = simple_strtoul(argv[4], NULL, 16);
381
Simon Glass63656b72014-12-10 08:55:48 -0700382#ifdef CONFIG_DM_I2C
383 ret = i2c_get_cur_bus_chip(chip, &dev);
384 if (!ret && alen != -1)
385 ret = i2c_set_chip_offset_len(dev, alen);
386 if (ret)
387 return i2c_report_err(ret, I2C_ERR_WRITE);
Lubomir Popoved16f142015-01-30 19:56:04 +0200388 i2c_chip = dev_get_parent_platdata(dev);
389 if (!i2c_chip)
390 return i2c_report_err(ret, I2C_ERR_WRITE);
Simon Glass63656b72014-12-10 08:55:48 -0700391#endif
392
Lubomir Popoved16f142015-01-30 19:56:04 +0200393 if (argc == 6 && !strcmp(argv[5], "-s")) {
394 /*
395 * Write all bytes in a single I2C transaction. If the target
396 * device is an EEPROM, it is your responsibility to not cross
397 * a page boundary. No write delay upon completion, take this
398 * into account if linking commands.
399 */
Simon Glass63656b72014-12-10 08:55:48 -0700400#ifdef CONFIG_DM_I2C
Lubomir Popoved16f142015-01-30 19:56:04 +0200401 i2c_chip->flags &= ~DM_I2C_CHIP_WR_ADDRESS;
402 ret = dm_i2c_write(dev, devaddr, memaddr, length);
Simon Glass63656b72014-12-10 08:55:48 -0700403#else
Lubomir Popoved16f142015-01-30 19:56:04 +0200404 ret = i2c_write(chip, devaddr, alen, memaddr, length);
Simon Glass63656b72014-12-10 08:55:48 -0700405#endif
406 if (ret)
407 return i2c_report_err(ret, I2C_ERR_WRITE);
Lubomir Popoved16f142015-01-30 19:56:04 +0200408 } else {
409 /*
410 * Repeated addressing - perform <length> separate
411 * write transactions of one byte each
412 */
413 while (length-- > 0) {
414#ifdef CONFIG_DM_I2C
415 i2c_chip->flags |= DM_I2C_CHIP_WR_ADDRESS;
416 ret = dm_i2c_write(dev, devaddr++, memaddr++, 1);
417#else
418 ret = i2c_write(chip, devaddr++, alen, memaddr++, 1);
419#endif
420 if (ret)
421 return i2c_report_err(ret, I2C_ERR_WRITE);
York Sunff5d2dc2012-09-16 08:02:30 +0000422/*
423 * No write delay with FRAM devices.
424 */
425#if !defined(CONFIG_SYS_I2C_FRAM)
Lubomir Popoved16f142015-01-30 19:56:04 +0200426 udelay(11000);
York Sunff5d2dc2012-09-16 08:02:30 +0000427#endif
Lubomir Popoved16f142015-01-30 19:56:04 +0200428 }
York Sunff5d2dc2012-09-16 08:02:30 +0000429 }
430 return 0;
431}
432
Simon Glass63656b72014-12-10 08:55:48 -0700433#ifdef CONFIG_DM_I2C
434static int do_i2c_flags(cmd_tbl_t *cmdtp, int flag, int argc,
435 char *const argv[])
436{
437 struct udevice *dev;
438 uint flags;
439 int chip;
440 int ret;
441
442 if (argc < 2)
443 return CMD_RET_USAGE;
444
445 chip = simple_strtoul(argv[1], NULL, 16);
446 ret = i2c_get_cur_bus_chip(chip, &dev);
447 if (ret)
448 return i2c_report_err(ret, I2C_ERR_READ);
449
450 if (argc > 2) {
451 flags = simple_strtoul(argv[2], NULL, 16);
452 ret = i2c_set_chip_flags(dev, flags);
453 } else {
454 ret = i2c_get_chip_flags(dev, &flags);
455 if (!ret)
456 printf("%x\n", flags);
457 }
458 if (ret)
459 return i2c_report_err(ret, I2C_ERR_READ);
460
461 return 0;
462}
Simon Glassc10c8e32015-08-22 18:31:33 -0600463
464static int do_i2c_olen(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
465{
466 struct udevice *dev;
467 uint olen;
468 int chip;
469 int ret;
470
471 if (argc < 2)
472 return CMD_RET_USAGE;
473
474 chip = simple_strtoul(argv[1], NULL, 16);
475 ret = i2c_get_cur_bus_chip(chip, &dev);
476 if (ret)
477 return i2c_report_err(ret, I2C_ERR_READ);
478
479 if (argc > 2) {
480 olen = simple_strtoul(argv[2], NULL, 16);
481 ret = i2c_set_chip_offset_len(dev, olen);
482 } else {
483 ret = i2c_get_chip_offset_len(dev);
484 if (ret >= 0) {
485 printf("%x\n", ret);
486 ret = 0;
487 }
488 }
489 if (ret)
490 return i2c_report_err(ret, I2C_ERR_READ);
491
492 return 0;
493}
Simon Glass63656b72014-12-10 08:55:48 -0700494#endif
495
Marek Vasut06afa382012-11-12 14:34:27 +0000496/**
497 * do_i2c_md() - Handle the "i2c md" command-line command
498 * @cmdtp: Command data struct pointer
499 * @flag: Command flag
500 * @argc: Command-line argument count
501 * @argv: Array of command-line arguments
502 *
503 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
504 * on error.
505 *
Frans Meulenbroeks4a8cf332010-03-26 09:46:39 +0100506 * Syntax:
507 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
508 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200509static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000510{
Masahiro Yamada54684612014-12-20 03:34:23 +0900511 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -0700512 uint addr, length;
513 int alen;
wdenk81a88242002-10-26 15:22:42 +0000514 int j, nbytes, linebytes;
Simon Glass63656b72014-12-10 08:55:48 -0700515 int ret;
516#ifdef CONFIG_DM_I2C
517 struct udevice *dev;
518#endif
wdenk81a88242002-10-26 15:22:42 +0000519
520 /* We use the last specified parameters, unless new ones are
521 * entered.
522 */
523 chip = i2c_dp_last_chip;
524 addr = i2c_dp_last_addr;
525 alen = i2c_dp_last_alen;
526 length = i2c_dp_last_length;
527
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200528 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000529 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000530
531 if ((flag & CMD_FLAG_REPEAT) == 0) {
532 /*
533 * New command specified.
534 */
wdenk81a88242002-10-26 15:22:42 +0000535
536 /*
537 * I2C chip address
538 */
539 chip = simple_strtoul(argv[1], NULL, 16);
540
541 /*
542 * I2C data address within the chip. This can be 1 or
543 * 2 bytes long. Some day it might be 3 bytes long :-).
544 */
545 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700546 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200547 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000548 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000549
550 /*
551 * If another parameter, it is the length to display.
552 * Length is the number of objects, not number of bytes.
553 */
554 if (argc > 3)
555 length = simple_strtoul(argv[3], NULL, 16);
556 }
557
Simon Glass63656b72014-12-10 08:55:48 -0700558#ifdef CONFIG_DM_I2C
559 ret = i2c_get_cur_bus_chip(chip, &dev);
560 if (!ret && alen != -1)
561 ret = i2c_set_chip_offset_len(dev, alen);
562 if (ret)
563 return i2c_report_err(ret, I2C_ERR_READ);
564#endif
565
wdenk81a88242002-10-26 15:22:42 +0000566 /*
567 * Print the lines.
568 *
569 * We buffer all read data, so we can make sure data is read only
570 * once.
571 */
572 nbytes = length;
573 do {
574 unsigned char linebuf[DISP_LINE_LEN];
575 unsigned char *cp;
576
577 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
578
Simon Glass63656b72014-12-10 08:55:48 -0700579#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700580 ret = dm_i2c_read(dev, addr, linebuf, linebytes);
Simon Glass63656b72014-12-10 08:55:48 -0700581#else
582 ret = i2c_read(chip, addr, alen, linebuf, linebytes);
583#endif
584 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900585 return i2c_report_err(ret, I2C_ERR_READ);
Timur Tabie857a5b2006-11-28 12:09:35 -0600586 else {
wdenk81a88242002-10-26 15:22:42 +0000587 printf("%04x:", addr);
588 cp = linebuf;
589 for (j=0; j<linebytes; j++) {
590 printf(" %02x", *cp++);
591 addr++;
592 }
wdenk4b9206e2004-03-23 22:14:11 +0000593 puts (" ");
wdenk81a88242002-10-26 15:22:42 +0000594 cp = linebuf;
595 for (j=0; j<linebytes; j++) {
596 if ((*cp < 0x20) || (*cp > 0x7e))
wdenk4b9206e2004-03-23 22:14:11 +0000597 puts (".");
wdenk81a88242002-10-26 15:22:42 +0000598 else
599 printf("%c", *cp);
600 cp++;
601 }
wdenk4b9206e2004-03-23 22:14:11 +0000602 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000603 }
604 nbytes -= linebytes;
605 } while (nbytes > 0);
606
607 i2c_dp_last_chip = chip;
608 i2c_dp_last_addr = addr;
609 i2c_dp_last_alen = alen;
610 i2c_dp_last_length = length;
611
612 return 0;
613}
614
Marek Vasut06afa382012-11-12 14:34:27 +0000615/**
616 * do_i2c_mw() - Handle the "i2c mw" command-line command
617 * @cmdtp: Command data struct pointer
618 * @flag: Command flag
619 * @argc: Command-line argument count
620 * @argv: Array of command-line arguments
621 *
622 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
623 * on error.
wdenk81a88242002-10-26 15:22:42 +0000624 *
625 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500626 * i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
wdenk81a88242002-10-26 15:22:42 +0000627 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200628static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000629{
Masahiro Yamada54684612014-12-20 03:34:23 +0900630 uint chip;
wdenk81a88242002-10-26 15:22:42 +0000631 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700632 int alen;
wdenk81a88242002-10-26 15:22:42 +0000633 uchar byte;
634 int count;
Simon Glass63656b72014-12-10 08:55:48 -0700635 int ret;
636#ifdef CONFIG_DM_I2C
637 struct udevice *dev;
638#endif
wdenk81a88242002-10-26 15:22:42 +0000639
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200640 if ((argc < 4) || (argc > 5))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000641 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000642
643 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200644 * Chip is always specified.
645 */
wdenk81a88242002-10-26 15:22:42 +0000646 chip = simple_strtoul(argv[1], NULL, 16);
647
648 /*
649 * Address is always specified.
650 */
651 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700652 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200653 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000654 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000655
Simon Glass63656b72014-12-10 08:55:48 -0700656#ifdef CONFIG_DM_I2C
657 ret = i2c_get_cur_bus_chip(chip, &dev);
658 if (!ret && alen != -1)
659 ret = i2c_set_chip_offset_len(dev, alen);
660 if (ret)
661 return i2c_report_err(ret, I2C_ERR_WRITE);
662#endif
wdenk81a88242002-10-26 15:22:42 +0000663 /*
664 * Value to write is always specified.
665 */
666 byte = simple_strtoul(argv[3], NULL, 16);
667
668 /*
669 * Optional count
670 */
Timur Tabie857a5b2006-11-28 12:09:35 -0600671 if (argc == 5)
wdenk81a88242002-10-26 15:22:42 +0000672 count = simple_strtoul(argv[4], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600673 else
wdenk81a88242002-10-26 15:22:42 +0000674 count = 1;
wdenk81a88242002-10-26 15:22:42 +0000675
676 while (count-- > 0) {
Simon Glass63656b72014-12-10 08:55:48 -0700677#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700678 ret = dm_i2c_write(dev, addr++, &byte, 1);
Simon Glass63656b72014-12-10 08:55:48 -0700679#else
680 ret = i2c_write(chip, addr++, alen, &byte, 1);
681#endif
682 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900683 return i2c_report_err(ret, I2C_ERR_WRITE);
wdenk81a88242002-10-26 15:22:42 +0000684 /*
685 * Wait for the write to complete. The write can take
686 * up to 10mSec (we allow a little more time).
wdenk81a88242002-10-26 15:22:42 +0000687 */
d4f5c722005-08-12 21:16:13 +0200688/*
689 * No write delay with FRAM devices.
690 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200691#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk81a88242002-10-26 15:22:42 +0000692 udelay(11000);
d4f5c722005-08-12 21:16:13 +0200693#endif
wdenk81a88242002-10-26 15:22:42 +0000694 }
695
Marek Vasut06afa382012-11-12 14:34:27 +0000696 return 0;
wdenk81a88242002-10-26 15:22:42 +0000697}
698
Marek Vasut06afa382012-11-12 14:34:27 +0000699/**
700 * do_i2c_crc() - Handle the "i2c crc32" command-line command
701 * @cmdtp: Command data struct pointer
702 * @flag: Command flag
703 * @argc: Command-line argument count
704 * @argv: Array of command-line arguments
705 *
706 * Calculate a CRC on memory
707 *
708 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
709 * on error.
wdenk81a88242002-10-26 15:22:42 +0000710 *
711 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500712 * i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
wdenk81a88242002-10-26 15:22:42 +0000713 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200714static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000715{
Masahiro Yamada54684612014-12-20 03:34:23 +0900716 uint chip;
wdenk81a88242002-10-26 15:22:42 +0000717 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700718 int alen;
wdenk81a88242002-10-26 15:22:42 +0000719 int count;
720 uchar byte;
721 ulong crc;
722 ulong err;
Simon Glass63656b72014-12-10 08:55:48 -0700723 int ret = 0;
724#ifdef CONFIG_DM_I2C
725 struct udevice *dev;
726#endif
wdenk81a88242002-10-26 15:22:42 +0000727
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200728 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000729 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000730
731 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200732 * Chip is always specified.
733 */
wdenk81a88242002-10-26 15:22:42 +0000734 chip = simple_strtoul(argv[1], NULL, 16);
735
736 /*
737 * Address is always specified.
738 */
739 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700740 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200741 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000742 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000743
Simon Glass63656b72014-12-10 08:55:48 -0700744#ifdef CONFIG_DM_I2C
745 ret = i2c_get_cur_bus_chip(chip, &dev);
746 if (!ret && alen != -1)
747 ret = i2c_set_chip_offset_len(dev, alen);
748 if (ret)
749 return i2c_report_err(ret, I2C_ERR_READ);
750#endif
wdenk81a88242002-10-26 15:22:42 +0000751 /*
752 * Count is always specified
753 */
754 count = simple_strtoul(argv[3], NULL, 16);
755
756 printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
757 /*
758 * CRC a byte at a time. This is going to be slooow, but hey, the
759 * memories are small and slow too so hopefully nobody notices.
760 */
761 crc = 0;
762 err = 0;
Timur Tabie857a5b2006-11-28 12:09:35 -0600763 while (count-- > 0) {
Simon Glass63656b72014-12-10 08:55:48 -0700764#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700765 ret = dm_i2c_read(dev, addr, &byte, 1);
Simon Glass63656b72014-12-10 08:55:48 -0700766#else
767 ret = i2c_read(chip, addr, alen, &byte, 1);
768#endif
769 if (ret)
wdenk81a88242002-10-26 15:22:42 +0000770 err++;
wdenk81a88242002-10-26 15:22:42 +0000771 crc = crc32 (crc, &byte, 1);
772 addr++;
773 }
Timur Tabie857a5b2006-11-28 12:09:35 -0600774 if (err > 0)
Simon Glass63656b72014-12-10 08:55:48 -0700775 i2c_report_err(ret, I2C_ERR_READ);
Timur Tabie857a5b2006-11-28 12:09:35 -0600776 else
wdenk81a88242002-10-26 15:22:42 +0000777 printf ("%08lx\n", crc);
wdenk81a88242002-10-26 15:22:42 +0000778
779 return 0;
780}
781
Marek Vasut06afa382012-11-12 14:34:27 +0000782/**
783 * mod_i2c_mem() - Handle the "i2c mm" and "i2c nm" command-line command
784 * @cmdtp: Command data struct pointer
785 * @flag: Command flag
786 * @argc: Command-line argument count
787 * @argv: Array of command-line arguments
788 *
789 * Modify memory.
790 *
791 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
792 * on error.
wdenk81a88242002-10-26 15:22:42 +0000793 *
794 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -0500795 * i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
796 * i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
wdenk81a88242002-10-26 15:22:42 +0000797 */
wdenk81a88242002-10-26 15:22:42 +0000798static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200799mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000800{
Masahiro Yamada54684612014-12-20 03:34:23 +0900801 uint chip;
wdenk81a88242002-10-26 15:22:42 +0000802 ulong addr;
Simon Glass63656b72014-12-10 08:55:48 -0700803 int alen;
wdenk81a88242002-10-26 15:22:42 +0000804 ulong data;
805 int size = 1;
806 int nbytes;
Simon Glass63656b72014-12-10 08:55:48 -0700807 int ret;
808#ifdef CONFIG_DM_I2C
809 struct udevice *dev;
810#endif
wdenk81a88242002-10-26 15:22:42 +0000811
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200812 if (argc != 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000813 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000814
Simon Glassb26440f2014-04-10 20:01:31 -0600815 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk81a88242002-10-26 15:22:42 +0000816 /*
817 * We use the last specified parameters, unless new ones are
818 * entered.
819 */
820 chip = i2c_mm_last_chip;
821 addr = i2c_mm_last_addr;
822 alen = i2c_mm_last_alen;
823
824 if ((flag & CMD_FLAG_REPEAT) == 0) {
825 /*
826 * New command specified. Check for a size specification.
827 * Defaults to byte if no or incorrect specification.
828 */
829 size = cmd_get_data_size(argv[0], 1);
830
831 /*
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200832 * Chip is always specified.
833 */
wdenk81a88242002-10-26 15:22:42 +0000834 chip = simple_strtoul(argv[1], NULL, 16);
835
836 /*
837 * Address is always specified.
838 */
839 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -0700840 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +0200841 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000842 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +0000843 }
844
Simon Glass63656b72014-12-10 08:55:48 -0700845#ifdef CONFIG_DM_I2C
846 ret = i2c_get_cur_bus_chip(chip, &dev);
847 if (!ret && alen != -1)
848 ret = i2c_set_chip_offset_len(dev, alen);
849 if (ret)
850 return i2c_report_err(ret, I2C_ERR_WRITE);
851#endif
852
wdenk81a88242002-10-26 15:22:42 +0000853 /*
854 * Print the address, followed by value. Then accept input for
855 * the next value. A non-converted value exits.
856 */
857 do {
858 printf("%08lx:", addr);
Simon Glass63656b72014-12-10 08:55:48 -0700859#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700860 ret = dm_i2c_read(dev, addr, (uchar *)&data, size);
Simon Glass63656b72014-12-10 08:55:48 -0700861#else
862 ret = i2c_read(chip, addr, alen, (uchar *)&data, size);
863#endif
864 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900865 return i2c_report_err(ret, I2C_ERR_READ);
866
867 data = cpu_to_be32(data);
868 if (size == 1)
869 printf(" %02lx", (data >> 24) & 0x000000FF);
870 else if (size == 2)
871 printf(" %04lx", (data >> 16) & 0x0000FFFF);
872 else
873 printf(" %08lx", data);
wdenk81a88242002-10-26 15:22:42 +0000874
Simon Glasse1bf8242014-04-10 20:01:27 -0600875 nbytes = cli_readline(" ? ");
wdenk81a88242002-10-26 15:22:42 +0000876 if (nbytes == 0) {
877 /*
878 * <CR> pressed as only input, don't modify current
879 * location and move to next.
880 */
881 if (incrflag)
882 addr += size;
883 nbytes = size;
Simon Glassb26440f2014-04-10 20:01:31 -0600884 /* good enough to not time out */
885 bootretry_reset_cmd_timeout();
wdenk81a88242002-10-26 15:22:42 +0000886 }
887#ifdef CONFIG_BOOT_RETRY_TIME
Timur Tabie857a5b2006-11-28 12:09:35 -0600888 else if (nbytes == -2)
wdenk81a88242002-10-26 15:22:42 +0000889 break; /* timed out, exit the command */
wdenk81a88242002-10-26 15:22:42 +0000890#endif
891 else {
892 char *endp;
893
894 data = simple_strtoul(console_buffer, &endp, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -0600895 if (size == 1)
wdenk81a88242002-10-26 15:22:42 +0000896 data = data << 24;
Timur Tabie857a5b2006-11-28 12:09:35 -0600897 else if (size == 2)
wdenk81a88242002-10-26 15:22:42 +0000898 data = data << 16;
wdenk81a88242002-10-26 15:22:42 +0000899 data = be32_to_cpu(data);
900 nbytes = endp - console_buffer;
901 if (nbytes) {
wdenk81a88242002-10-26 15:22:42 +0000902 /*
903 * good enough to not time out
904 */
Simon Glassb26440f2014-04-10 20:01:31 -0600905 bootretry_reset_cmd_timeout();
Simon Glass63656b72014-12-10 08:55:48 -0700906#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700907 ret = dm_i2c_write(dev, addr, (uchar *)&data,
908 size);
Simon Glass63656b72014-12-10 08:55:48 -0700909#else
910 ret = i2c_write(chip, addr, alen,
911 (uchar *)&data, size);
912#endif
913 if (ret)
Masahiro Yamada9e533cb2015-02-05 13:50:26 +0900914 return i2c_report_err(ret,
915 I2C_ERR_WRITE);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200916#ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
917 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk2535d602003-07-17 23:16:40 +0000918#endif
wdenk81a88242002-10-26 15:22:42 +0000919 if (incrflag)
920 addr += size;
921 }
922 }
923 } while (nbytes);
924
Peter Tyser08007072008-08-15 14:36:32 -0500925 i2c_mm_last_chip = chip;
926 i2c_mm_last_addr = addr;
927 i2c_mm_last_alen = alen;
wdenk81a88242002-10-26 15:22:42 +0000928
929 return 0;
930}
931
Marek Vasut06afa382012-11-12 14:34:27 +0000932/**
933 * do_i2c_probe() - Handle the "i2c probe" command-line command
934 * @cmdtp: Command data struct pointer
935 * @flag: Command flag
936 * @argc: Command-line argument count
937 * @argv: Array of command-line arguments
938 *
939 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
940 * on error.
941 *
wdenk81a88242002-10-26 15:22:42 +0000942 * Syntax:
Eric Nelson54b99e52012-09-23 10:12:56 +0000943 * i2c probe {addr}
944 *
945 * Returns zero (success) if one or more I2C devices was found
wdenk81a88242002-10-26 15:22:42 +0000946 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200947static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +0000948{
949 int j;
Eric Nelson54b99e52012-09-23 10:12:56 +0000950 int addr = -1;
951 int found = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200952#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000953 int k, skip;
Heiko Schocher3f4978c2012-01-16 21:12:24 +0000954 unsigned int bus = GET_BUS_NUM;
Ben Warrenbb99ad62006-09-07 16:50:54 -0400955#endif /* NOPROBES */
Simon Glass63656b72014-12-10 08:55:48 -0700956 int ret;
957#ifdef CONFIG_DM_I2C
958 struct udevice *bus, *dev;
959
960 if (i2c_get_cur_bus(&bus))
961 return CMD_RET_FAILURE;
962#endif
wdenk81a88242002-10-26 15:22:42 +0000963
Eric Nelson54b99e52012-09-23 10:12:56 +0000964 if (argc == 2)
965 addr = simple_strtol(argv[1], 0, 16);
966
wdenk4b9206e2004-03-23 22:14:11 +0000967 puts ("Valid chip addresses:");
Timur Tabie857a5b2006-11-28 12:09:35 -0600968 for (j = 0; j < 128; j++) {
Eric Nelson54b99e52012-09-23 10:12:56 +0000969 if ((0 <= addr) && (j != addr))
970 continue;
971
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200972#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000973 skip = 0;
Axel Lincfb25cc2013-06-22 21:56:35 +0800974 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600975 if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
wdenk81a88242002-10-26 15:22:42 +0000976 skip = 1;
977 break;
978 }
979 }
980 if (skip)
981 continue;
982#endif
Simon Glass63656b72014-12-10 08:55:48 -0700983#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700984 ret = dm_i2c_probe(bus, j, 0, &dev);
Simon Glass63656b72014-12-10 08:55:48 -0700985#else
986 ret = i2c_probe(j);
987#endif
988 if (ret == 0) {
wdenk81a88242002-10-26 15:22:42 +0000989 printf(" %02X", j);
Eric Nelson54b99e52012-09-23 10:12:56 +0000990 found++;
991 }
wdenk81a88242002-10-26 15:22:42 +0000992 }
wdenk4b9206e2004-03-23 22:14:11 +0000993 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +0000994
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200995#if defined(CONFIG_SYS_I2C_NOPROBES)
wdenk81a88242002-10-26 15:22:42 +0000996 puts ("Excluded chip addresses:");
Axel Lincfb25cc2013-06-22 21:56:35 +0800997 for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
Timur Tabie857a5b2006-11-28 12:09:35 -0600998 if (COMPARE_BUS(bus,k))
Ben Warrenbb99ad62006-09-07 16:50:54 -0400999 printf(" %02X", NO_PROBE_ADDR(k));
1000 }
wdenk4b9206e2004-03-23 22:14:11 +00001001 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001002#endif
1003
Eric Nelson54b99e52012-09-23 10:12:56 +00001004 return (0 == found);
wdenk81a88242002-10-26 15:22:42 +00001005}
1006
Marek Vasut06afa382012-11-12 14:34:27 +00001007/**
1008 * do_i2c_loop() - Handle the "i2c loop" command-line command
1009 * @cmdtp: Command data struct pointer
1010 * @flag: Command flag
1011 * @argc: Command-line argument count
1012 * @argv: Array of command-line arguments
1013 *
1014 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1015 * on error.
1016 *
wdenk81a88242002-10-26 15:22:42 +00001017 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -05001018 * i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
wdenk81a88242002-10-26 15:22:42 +00001019 * {length} - Number of bytes to read
1020 * {delay} - A DECIMAL number and defaults to 1000 uSec
1021 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001022static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +00001023{
Masahiro Yamada54684612014-12-20 03:34:23 +09001024 uint chip;
Simon Glass63656b72014-12-10 08:55:48 -07001025 int alen;
wdenk81a88242002-10-26 15:22:42 +00001026 uint addr;
1027 uint length;
1028 u_char bytes[16];
1029 int delay;
Simon Glass63656b72014-12-10 08:55:48 -07001030 int ret;
1031#ifdef CONFIG_DM_I2C
1032 struct udevice *dev;
1033#endif
wdenk81a88242002-10-26 15:22:42 +00001034
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001035 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001036 return CMD_RET_USAGE;
wdenk81a88242002-10-26 15:22:42 +00001037
1038 /*
1039 * Chip is always specified.
1040 */
1041 chip = simple_strtoul(argv[1], NULL, 16);
1042
1043 /*
1044 * Address is always specified.
1045 */
1046 addr = simple_strtoul(argv[2], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -07001047 alen = get_alen(argv[2], DEFAULT_ADDR_LEN);
Reinhard Meyer7a92e532010-08-25 14:41:16 +02001048 if (alen > 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001049 return CMD_RET_USAGE;
Simon Glass63656b72014-12-10 08:55:48 -07001050#ifdef CONFIG_DM_I2C
1051 ret = i2c_get_cur_bus_chip(chip, &dev);
1052 if (!ret && alen != -1)
1053 ret = i2c_set_chip_offset_len(dev, alen);
1054 if (ret)
1055 return i2c_report_err(ret, I2C_ERR_WRITE);
1056#endif
wdenk81a88242002-10-26 15:22:42 +00001057
1058 /*
1059 * Length is the number of objects, not number of bytes.
1060 */
1061 length = 1;
1062 length = simple_strtoul(argv[3], NULL, 16);
Timur Tabie857a5b2006-11-28 12:09:35 -06001063 if (length > sizeof(bytes))
wdenk81a88242002-10-26 15:22:42 +00001064 length = sizeof(bytes);
wdenk81a88242002-10-26 15:22:42 +00001065
1066 /*
1067 * The delay time (uSec) is optional.
1068 */
1069 delay = 1000;
Timur Tabie857a5b2006-11-28 12:09:35 -06001070 if (argc > 3)
wdenk81a88242002-10-26 15:22:42 +00001071 delay = simple_strtoul(argv[4], NULL, 10);
wdenk81a88242002-10-26 15:22:42 +00001072 /*
1073 * Run the loop...
1074 */
Timur Tabie857a5b2006-11-28 12:09:35 -06001075 while (1) {
Simon Glass63656b72014-12-10 08:55:48 -07001076#ifdef CONFIG_DM_I2C
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001077 ret = dm_i2c_read(dev, addr, bytes, length);
Simon Glass63656b72014-12-10 08:55:48 -07001078#else
1079 ret = i2c_read(chip, addr, alen, bytes, length);
1080#endif
1081 if (ret)
1082 i2c_report_err(ret, I2C_ERR_READ);
wdenk81a88242002-10-26 15:22:42 +00001083 udelay(delay);
1084 }
1085
1086 /* NOTREACHED */
1087 return 0;
1088}
1089
wdenk81a88242002-10-26 15:22:42 +00001090/*
1091 * The SDRAM command is separately configured because many
1092 * (most?) embedded boards don't use SDRAM DIMMs.
Marek Vasut06afa382012-11-12 14:34:27 +00001093 *
1094 * FIXME: Document and probably move elsewhere!
wdenk81a88242002-10-26 15:22:42 +00001095 */
Jon Loeligerc76fe472007-07-08 18:02:23 -05001096#if defined(CONFIG_CMD_SDRAM)
Larry Johnson632de062008-01-11 23:26:18 -05001097static void print_ddr2_tcyc (u_char const b)
1098{
1099 printf ("%d.", (b >> 4) & 0x0F);
1100 switch (b & 0x0F) {
1101 case 0x0:
1102 case 0x1:
1103 case 0x2:
1104 case 0x3:
1105 case 0x4:
1106 case 0x5:
1107 case 0x6:
1108 case 0x7:
1109 case 0x8:
1110 case 0x9:
1111 printf ("%d ns\n", b & 0x0F);
1112 break;
1113 case 0xA:
1114 puts ("25 ns\n");
1115 break;
1116 case 0xB:
1117 puts ("33 ns\n");
1118 break;
1119 case 0xC:
1120 puts ("66 ns\n");
1121 break;
1122 case 0xD:
1123 puts ("75 ns\n");
1124 break;
1125 default:
1126 puts ("?? ns\n");
1127 break;
1128 }
1129}
1130
1131static void decode_bits (u_char const b, char const *str[], int const do_once)
1132{
1133 u_char mask;
1134
1135 for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
1136 if (b & mask) {
1137 puts (*str);
1138 if (do_once)
1139 return;
1140 }
1141 }
1142}
wdenk81a88242002-10-26 15:22:42 +00001143
1144/*
1145 * Syntax:
Peter Tyser0f89c542009-04-18 22:34:03 -05001146 * i2c sdram {i2c_chip}
wdenk81a88242002-10-26 15:22:42 +00001147 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001148static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenk81a88242002-10-26 15:22:42 +00001149{
Michal Simek18c4e7f2016-02-15 11:58:37 +01001150 enum { unknown, EDO, SDRAM, DDR, DDR2, DDR3, DDR4 } type;
Larry Johnson632de062008-01-11 23:26:18 -05001151
Masahiro Yamada54684612014-12-20 03:34:23 +09001152 uint chip;
wdenk81a88242002-10-26 15:22:42 +00001153 u_char data[128];
1154 u_char cksum;
Nobuhiro Iwamatsu28df8ed2017-12-01 14:39:40 +09001155 int j, ret;
1156#ifdef CONFIG_DM_I2C
1157 struct udevice *dev;
1158#endif
wdenk81a88242002-10-26 15:22:42 +00001159
Larry Johnson632de062008-01-11 23:26:18 -05001160 static const char *decode_CAS_DDR2[] = {
1161 " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
1162 };
1163
1164 static const char *decode_CAS_default[] = {
1165 " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
1166 };
1167
1168 static const char *decode_CS_WE_default[] = {
1169 " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
1170 };
1171
1172 static const char *decode_byte21_default[] = {
1173 " TBD (bit 7)\n",
1174 " Redundant row address\n",
1175 " Differential clock input\n",
1176 " Registerd DQMB inputs\n",
1177 " Buffered DQMB inputs\n",
1178 " On-card PLL\n",
1179 " Registered address/control lines\n",
1180 " Buffered address/control lines\n"
1181 };
1182
1183 static const char *decode_byte22_DDR2[] = {
1184 " TBD (bit 7)\n",
1185 " TBD (bit 6)\n",
1186 " TBD (bit 5)\n",
1187 " TBD (bit 4)\n",
1188 " TBD (bit 3)\n",
1189 " Supports partial array self refresh\n",
1190 " Supports 50 ohm ODT\n",
1191 " Supports weak driver\n"
1192 };
1193
1194 static const char *decode_row_density_DDR2[] = {
1195 "512 MiB", "256 MiB", "128 MiB", "16 GiB",
1196 "8 GiB", "4 GiB", "2 GiB", "1 GiB"
1197 };
1198
1199 static const char *decode_row_density_default[] = {
1200 "512 MiB", "256 MiB", "128 MiB", "64 MiB",
1201 "32 MiB", "16 MiB", "8 MiB", "4 MiB"
1202 };
1203
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001204 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001205 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001206
wdenk81a88242002-10-26 15:22:42 +00001207 /*
1208 * Chip is always specified.
Larry Johnson632de062008-01-11 23:26:18 -05001209 */
1210 chip = simple_strtoul (argv[1], NULL, 16);
wdenk81a88242002-10-26 15:22:42 +00001211
Nobuhiro Iwamatsu28df8ed2017-12-01 14:39:40 +09001212#ifdef CONFIG_DM_I2C
1213 ret = i2c_get_cur_bus_chip(chip, &dev);
1214 if (!ret)
1215 ret = dm_i2c_read(dev, 0, data, sizeof(data));
1216#else
1217 ret = i2c_read(chip, 0, 1, data, sizeof(data));
1218#endif
1219 if (ret) {
wdenk4b9206e2004-03-23 22:14:11 +00001220 puts ("No SDRAM Serial Presence Detect found.\n");
wdenk81a88242002-10-26 15:22:42 +00001221 return 1;
1222 }
1223
1224 cksum = 0;
1225 for (j = 0; j < 63; j++) {
1226 cksum += data[j];
1227 }
Timur Tabie857a5b2006-11-28 12:09:35 -06001228 if (cksum != data[63]) {
wdenk81a88242002-10-26 15:22:42 +00001229 printf ("WARNING: Configuration data checksum failure:\n"
Larry Johnson632de062008-01-11 23:26:18 -05001230 " is 0x%02x, calculated 0x%02x\n", data[63], cksum);
wdenk81a88242002-10-26 15:22:42 +00001231 }
Larry Johnson632de062008-01-11 23:26:18 -05001232 printf ("SPD data revision %d.%d\n",
wdenk81a88242002-10-26 15:22:42 +00001233 (data[62] >> 4) & 0x0F, data[62] & 0x0F);
Larry Johnson632de062008-01-11 23:26:18 -05001234 printf ("Bytes used 0x%02X\n", data[0]);
1235 printf ("Serial memory size 0x%02X\n", 1 << data[1]);
1236
wdenk4b9206e2004-03-23 22:14:11 +00001237 puts ("Memory type ");
Larry Johnson632de062008-01-11 23:26:18 -05001238 switch (data[2]) {
Larry Johnson0df6b842008-01-10 22:23:39 -05001239 case 2:
1240 type = EDO;
1241 puts ("EDO\n");
1242 break;
1243 case 4:
1244 type = SDRAM;
1245 puts ("SDRAM\n");
1246 break;
Michal Simek18c4e7f2016-02-15 11:58:37 +01001247 case 7:
1248 type = DDR;
1249 puts("DDR\n");
1250 break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001251 case 8:
1252 type = DDR2;
1253 puts ("DDR2\n");
1254 break;
Michal Simek18c4e7f2016-02-15 11:58:37 +01001255 case 11:
1256 type = DDR3;
1257 puts("DDR3\n");
1258 break;
1259 case 12:
1260 type = DDR4;
1261 puts("DDR4\n");
1262 break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001263 default:
1264 type = unknown;
1265 puts ("unknown\n");
1266 break;
wdenk81a88242002-10-26 15:22:42 +00001267 }
Larry Johnson632de062008-01-11 23:26:18 -05001268
wdenk4b9206e2004-03-23 22:14:11 +00001269 puts ("Row address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001270 if ((data[3] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -05001271 printf ("%d\n", data[3] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -06001272 else
Larry Johnson632de062008-01-11 23:26:18 -05001273 printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
1274
wdenk4b9206e2004-03-23 22:14:11 +00001275 puts ("Column address bits ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001276 if ((data[4] & 0x00F0) == 0)
Larry Johnson632de062008-01-11 23:26:18 -05001277 printf ("%d\n", data[4] & 0x0F);
Timur Tabie857a5b2006-11-28 12:09:35 -06001278 else
Larry Johnson632de062008-01-11 23:26:18 -05001279 printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001280
1281 switch (type) {
1282 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001283 printf ("Number of ranks %d\n",
1284 (data[5] & 0x07) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001285 break;
1286 default:
Larry Johnson632de062008-01-11 23:26:18 -05001287 printf ("Module rows %d\n", data[5]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001288 break;
1289 }
1290
1291 switch (type) {
1292 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001293 printf ("Module data width %d bits\n", data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001294 break;
1295 default:
Larry Johnson632de062008-01-11 23:26:18 -05001296 printf ("Module data width %d bits\n",
1297 (data[7] << 8) | data[6]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001298 break;
1299 }
1300
wdenk4b9206e2004-03-23 22:14:11 +00001301 puts ("Interface signal levels ");
wdenk81a88242002-10-26 15:22:42 +00001302 switch(data[8]) {
Larry Johnson0df6b842008-01-10 22:23:39 -05001303 case 0: puts ("TTL 5.0 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001304 case 1: puts ("LVTTL\n"); break;
Larry Johnson0df6b842008-01-10 22:23:39 -05001305 case 2: puts ("HSTL 1.5 V\n"); break;
1306 case 3: puts ("SSTL 3.3 V\n"); break;
1307 case 4: puts ("SSTL 2.5 V\n"); break;
1308 case 5: puts ("SSTL 1.8 V\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001309 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001310 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001311
1312 switch (type) {
1313 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001314 printf ("SDRAM cycle time ");
1315 print_ddr2_tcyc (data[9]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001316 break;
1317 default:
Larry Johnson632de062008-01-11 23:26:18 -05001318 printf ("SDRAM cycle time %d.%d ns\n",
1319 (data[9] >> 4) & 0x0F, data[9] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001320 break;
1321 }
1322
1323 switch (type) {
1324 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001325 printf ("SDRAM access time 0.%d%d ns\n",
1326 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001327 break;
1328 default:
Larry Johnson632de062008-01-11 23:26:18 -05001329 printf ("SDRAM access time %d.%d ns\n",
1330 (data[10] >> 4) & 0x0F, data[10] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001331 break;
1332 }
1333
wdenk4b9206e2004-03-23 22:14:11 +00001334 puts ("EDC configuration ");
Larry Johnson632de062008-01-11 23:26:18 -05001335 switch (data[11]) {
wdenk4b9206e2004-03-23 22:14:11 +00001336 case 0: puts ("None\n"); break;
1337 case 1: puts ("Parity\n"); break;
1338 case 2: puts ("ECC\n"); break;
1339 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001340 }
Larry Johnson632de062008-01-11 23:26:18 -05001341
Timur Tabie857a5b2006-11-28 12:09:35 -06001342 if ((data[12] & 0x80) == 0)
wdenk4b9206e2004-03-23 22:14:11 +00001343 puts ("No self refresh, rate ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001344 else
wdenk4b9206e2004-03-23 22:14:11 +00001345 puts ("Self refresh, rate ");
Larry Johnson632de062008-01-11 23:26:18 -05001346
wdenk81a88242002-10-26 15:22:42 +00001347 switch(data[12] & 0x7F) {
Larry Johnson632de062008-01-11 23:26:18 -05001348 case 0: puts ("15.625 us\n"); break;
1349 case 1: puts ("3.9 us\n"); break;
1350 case 2: puts ("7.8 us\n"); break;
1351 case 3: puts ("31.3 us\n"); break;
1352 case 4: puts ("62.5 us\n"); break;
1353 case 5: puts ("125 us\n"); break;
wdenk4b9206e2004-03-23 22:14:11 +00001354 default: puts ("unknown\n"); break;
wdenk81a88242002-10-26 15:22:42 +00001355 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001356
1357 switch (type) {
1358 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001359 printf ("SDRAM width (primary) %d\n", data[13]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001360 break;
1361 default:
Larry Johnson632de062008-01-11 23:26:18 -05001362 printf ("SDRAM width (primary) %d\n", data[13] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001363 if ((data[13] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001364 printf (" (second bank) %d\n",
1365 2 * (data[13] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001366 }
1367 break;
wdenk81a88242002-10-26 15:22:42 +00001368 }
Larry Johnson0df6b842008-01-10 22:23:39 -05001369
1370 switch (type) {
1371 case DDR2:
1372 if (data[14] != 0)
Larry Johnson632de062008-01-11 23:26:18 -05001373 printf ("EDC width %d\n", data[14]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001374 break;
1375 default:
1376 if (data[14] != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001377 printf ("EDC width %d\n",
1378 data[14] & 0x7F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001379
1380 if ((data[14] & 0x80) != 0) {
Larry Johnson632de062008-01-11 23:26:18 -05001381 printf (" (second bank) %d\n",
1382 2 * (data[14] & 0x7F));
Larry Johnson0df6b842008-01-10 22:23:39 -05001383 }
1384 }
1385 break;
1386 }
1387
Larry Johnson632de062008-01-11 23:26:18 -05001388 if (DDR2 != type) {
1389 printf ("Min clock delay, back-to-back random column addresses "
1390 "%d\n", data[15]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001391 }
1392
wdenk4b9206e2004-03-23 22:14:11 +00001393 puts ("Burst length(s) ");
1394 if (data[16] & 0x80) puts (" Page");
1395 if (data[16] & 0x08) puts (" 8");
1396 if (data[16] & 0x04) puts (" 4");
1397 if (data[16] & 0x02) puts (" 2");
1398 if (data[16] & 0x01) puts (" 1");
1399 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001400 printf ("Number of banks %d\n", data[17]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001401
1402 switch (type) {
1403 case DDR2:
1404 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001405 decode_bits (data[18], decode_CAS_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001406 putc ('\n');
1407 break;
1408 default:
1409 puts ("CAS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001410 decode_bits (data[18], decode_CAS_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001411 putc ('\n');
1412 break;
1413 }
1414
1415 if (DDR2 != type) {
1416 puts ("CS latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001417 decode_bits (data[19], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001418 putc ('\n');
1419 }
1420
1421 if (DDR2 != type) {
1422 puts ("WE latency(s) ");
Larry Johnson632de062008-01-11 23:26:18 -05001423 decode_bits (data[20], decode_CS_WE_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001424 putc ('\n');
1425 }
1426
1427 switch (type) {
1428 case DDR2:
1429 puts ("Module attributes:\n");
1430 if (data[21] & 0x80)
1431 puts (" TBD (bit 7)\n");
1432 if (data[21] & 0x40)
1433 puts (" Analysis probe installed\n");
1434 if (data[21] & 0x20)
1435 puts (" TBD (bit 5)\n");
1436 if (data[21] & 0x10)
1437 puts (" FET switch external enable\n");
Larry Johnson632de062008-01-11 23:26:18 -05001438 printf (" %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
Larry Johnson0df6b842008-01-10 22:23:39 -05001439 if (data[20] & 0x11) {
Larry Johnson632de062008-01-11 23:26:18 -05001440 printf (" %d active registers on DIMM\n",
1441 (data[21] & 0x03) + 1);
Larry Johnson0df6b842008-01-10 22:23:39 -05001442 }
1443 break;
1444 default:
1445 puts ("Module attributes:\n");
1446 if (!data[21])
1447 puts (" (none)\n");
Larry Johnson632de062008-01-11 23:26:18 -05001448 else
1449 decode_bits (data[21], decode_byte21_default, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001450 break;
1451 }
1452
1453 switch (type) {
1454 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001455 decode_bits (data[22], decode_byte22_DDR2, 0);
Larry Johnson0df6b842008-01-10 22:23:39 -05001456 break;
1457 default:
1458 puts ("Device attributes:\n");
1459 if (data[22] & 0x80) puts (" TBD (bit 7)\n");
1460 if (data[22] & 0x40) puts (" TBD (bit 6)\n");
1461 if (data[22] & 0x20) puts (" Upper Vcc tolerance 5%\n");
1462 else puts (" Upper Vcc tolerance 10%\n");
1463 if (data[22] & 0x10) puts (" Lower Vcc tolerance 5%\n");
1464 else puts (" Lower Vcc tolerance 10%\n");
1465 if (data[22] & 0x08) puts (" Supports write1/read burst\n");
1466 if (data[22] & 0x04) puts (" Supports precharge all\n");
1467 if (data[22] & 0x02) puts (" Supports auto precharge\n");
1468 if (data[22] & 0x01) puts (" Supports early RAS# precharge\n");
1469 break;
1470 }
1471
1472 switch (type) {
1473 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001474 printf ("SDRAM cycle time (2nd highest CAS latency) ");
1475 print_ddr2_tcyc (data[23]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001476 break;
1477 default:
Larry Johnson632de062008-01-11 23:26:18 -05001478 printf ("SDRAM cycle time (2nd highest CAS latency) %d."
1479 "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001480 break;
1481 }
1482
1483 switch (type) {
1484 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001485 printf ("SDRAM access from clock (2nd highest CAS latency) 0."
1486 "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001487 break;
1488 default:
Larry Johnson632de062008-01-11 23:26:18 -05001489 printf ("SDRAM access from clock (2nd highest CAS latency) %d."
1490 "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001491 break;
1492 }
1493
1494 switch (type) {
1495 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001496 printf ("SDRAM cycle time (3rd highest CAS latency) ");
1497 print_ddr2_tcyc (data[25]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001498 break;
1499 default:
Larry Johnson632de062008-01-11 23:26:18 -05001500 printf ("SDRAM cycle time (3rd highest CAS latency) %d."
1501 "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001502 break;
1503 }
1504
1505 switch (type) {
1506 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001507 printf ("SDRAM access from clock (3rd highest CAS latency) 0."
1508 "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001509 break;
1510 default:
Larry Johnson632de062008-01-11 23:26:18 -05001511 printf ("SDRAM access from clock (3rd highest CAS latency) %d."
1512 "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001513 break;
1514 }
1515
1516 switch (type) {
1517 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001518 printf ("Minimum row precharge %d.%02d ns\n",
1519 (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001520 break;
1521 default:
Larry Johnson632de062008-01-11 23:26:18 -05001522 printf ("Minimum row precharge %d ns\n", data[27]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001523 break;
1524 }
1525
1526 switch (type) {
1527 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001528 printf ("Row active to row active min %d.%02d ns\n",
1529 (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001530 break;
1531 default:
Larry Johnson632de062008-01-11 23:26:18 -05001532 printf ("Row active to row active min %d ns\n", data[28]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001533 break;
1534 }
1535
1536 switch (type) {
1537 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001538 printf ("RAS to CAS delay min %d.%02d ns\n",
1539 (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
Larry Johnson0df6b842008-01-10 22:23:39 -05001540 break;
1541 default:
Larry Johnson632de062008-01-11 23:26:18 -05001542 printf ("RAS to CAS delay min %d ns\n", data[29]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001543 break;
1544 }
1545
Larry Johnson632de062008-01-11 23:26:18 -05001546 printf ("Minimum RAS pulse width %d ns\n", data[30]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001547
1548 switch (type) {
1549 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001550 puts ("Density of each row ");
1551 decode_bits (data[31], decode_row_density_DDR2, 1);
1552 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001553 break;
1554 default:
Larry Johnson632de062008-01-11 23:26:18 -05001555 puts ("Density of each row ");
1556 decode_bits (data[31], decode_row_density_default, 1);
1557 putc ('\n');
Larry Johnson0df6b842008-01-10 22:23:39 -05001558 break;
1559 }
1560
1561 switch (type) {
1562 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001563 puts ("Command and Address setup ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001564 if (data[32] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001565 printf ("1.%d%d ns\n",
1566 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001567 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001568 printf ("0.%d%d ns\n",
1569 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001570 }
1571 break;
1572 default:
Larry Johnson632de062008-01-11 23:26:18 -05001573 printf ("Command and Address setup %c%d.%d ns\n",
1574 (data[32] & 0x80) ? '-' : '+',
1575 (data[32] >> 4) & 0x07, data[32] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001576 break;
1577 }
1578
1579 switch (type) {
1580 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001581 puts ("Command and Address hold ");
Larry Johnson0df6b842008-01-10 22:23:39 -05001582 if (data[33] >= 0xA0) {
Larry Johnson632de062008-01-11 23:26:18 -05001583 printf ("1.%d%d ns\n",
1584 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001585 } else {
Larry Johnson632de062008-01-11 23:26:18 -05001586 printf ("0.%d%d ns\n",
1587 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001588 }
1589 break;
1590 default:
Larry Johnson632de062008-01-11 23:26:18 -05001591 printf ("Command and Address hold %c%d.%d ns\n",
1592 (data[33] & 0x80) ? '-' : '+',
1593 (data[33] >> 4) & 0x07, data[33] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001594 break;
1595 }
1596
1597 switch (type) {
1598 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001599 printf ("Data signal input setup 0.%d%d ns\n",
1600 (data[34] >> 4) & 0x0F, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001601 break;
1602 default:
Larry Johnson632de062008-01-11 23:26:18 -05001603 printf ("Data signal input setup %c%d.%d ns\n",
1604 (data[34] & 0x80) ? '-' : '+',
1605 (data[34] >> 4) & 0x07, data[34] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001606 break;
1607 }
1608
1609 switch (type) {
1610 case DDR2:
Larry Johnson632de062008-01-11 23:26:18 -05001611 printf ("Data signal input hold 0.%d%d ns\n",
1612 (data[35] >> 4) & 0x0F, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001613 break;
1614 default:
Larry Johnson632de062008-01-11 23:26:18 -05001615 printf ("Data signal input hold %c%d.%d ns\n",
1616 (data[35] & 0x80) ? '-' : '+',
1617 (data[35] >> 4) & 0x07, data[35] & 0x0F);
Larry Johnson0df6b842008-01-10 22:23:39 -05001618 break;
1619 }
1620
wdenk4b9206e2004-03-23 22:14:11 +00001621 puts ("Manufacturer's JEDEC ID ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001622 for (j = 64; j <= 71; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001623 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001624 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001625 printf ("Manufacturing Location %02X\n", data[72]);
wdenk4b9206e2004-03-23 22:14:11 +00001626 puts ("Manufacturer's Part Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001627 for (j = 73; j <= 90; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001628 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001629 putc ('\n');
Larry Johnson632de062008-01-11 23:26:18 -05001630 printf ("Revision Code %02X %02X\n", data[91], data[92]);
1631 printf ("Manufacturing Date %02X %02X\n", data[93], data[94]);
wdenk4b9206e2004-03-23 22:14:11 +00001632 puts ("Assembly Serial Number ");
Timur Tabie857a5b2006-11-28 12:09:35 -06001633 for (j = 95; j <= 98; j++)
Larry Johnson632de062008-01-11 23:26:18 -05001634 printf ("%02X ", data[j]);
wdenk4b9206e2004-03-23 22:14:11 +00001635 putc ('\n');
wdenk81a88242002-10-26 15:22:42 +00001636
Larry Johnson0df6b842008-01-10 22:23:39 -05001637 if (DDR2 != type) {
Larry Johnson632de062008-01-11 23:26:18 -05001638 printf ("Speed rating PC%d\n",
1639 data[126] == 0x66 ? 66 : data[126]);
Larry Johnson0df6b842008-01-10 22:23:39 -05001640 }
wdenk81a88242002-10-26 15:22:42 +00001641 return 0;
1642}
Jon Loeliger90253172007-07-10 11:02:44 -05001643#endif
wdenk81a88242002-10-26 15:22:42 +00001644
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001645/*
1646 * Syntax:
1647 * i2c edid {i2c_chip}
1648 */
1649#if defined(CONFIG_I2C_EDID)
1650int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
1651{
Masahiro Yamada54684612014-12-20 03:34:23 +09001652 uint chip;
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001653 struct edid1_info edid;
Simon Glass63656b72014-12-10 08:55:48 -07001654 int ret;
1655#ifdef CONFIG_DM_I2C
1656 struct udevice *dev;
1657#endif
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001658
1659 if (argc < 2) {
1660 cmd_usage(cmdtp);
1661 return 1;
1662 }
1663
1664 chip = simple_strtoul(argv[1], NULL, 16);
Simon Glass63656b72014-12-10 08:55:48 -07001665#ifdef CONFIG_DM_I2C
1666 ret = i2c_get_cur_bus_chip(chip, &dev);
1667 if (!ret)
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001668 ret = dm_i2c_read(dev, 0, (uchar *)&edid, sizeof(edid));
Simon Glass63656b72014-12-10 08:55:48 -07001669#else
1670 ret = i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid));
1671#endif
1672 if (ret)
1673 return i2c_report_err(ret, I2C_ERR_READ);
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001674
1675 if (edid_check_info(&edid)) {
1676 puts("Content isn't valid EDID.\n");
1677 return 1;
1678 }
1679
1680 edid_print_info(&edid);
1681 return 0;
1682
1683}
1684#endif /* CONFIG_I2C_EDID */
1685
Simon Glass59aa9df2015-05-04 11:30:57 -06001686#ifdef CONFIG_DM_I2C
1687static void show_bus(struct udevice *bus)
1688{
1689 struct udevice *dev;
1690
1691 printf("Bus %d:\t%s", bus->req_seq, bus->name);
1692 if (device_active(bus))
1693 printf(" (active %d)", bus->seq);
1694 printf("\n");
1695 for (device_find_first_child(bus, &dev);
1696 dev;
1697 device_find_next_child(&dev)) {
1698 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
1699
1700 printf(" %02x: %s, offset len %x, flags %x\n",
1701 chip->chip_addr, dev->name, chip->offset_len,
1702 chip->flags);
1703 }
1704}
1705#endif
1706
Marek Vasut06afa382012-11-12 14:34:27 +00001707/**
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001708 * do_i2c_show_bus() - Handle the "i2c bus" command-line command
Marek Vasut06afa382012-11-12 14:34:27 +00001709 * @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 always.
1715 */
Simon Glass59aa9df2015-05-04 11:30:57 -06001716#if defined(CONFIG_SYS_I2C) || defined(CONFIG_DM_I2C)
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001717static int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc,
1718 char * const argv[])
Heiko Schocher67b23a32008-10-15 09:39:47 +02001719{
Heiko Schocher67b23a32008-10-15 09:39:47 +02001720 if (argc == 1) {
1721 /* show all busses */
Simon Glass59aa9df2015-05-04 11:30:57 -06001722#ifdef CONFIG_DM_I2C
1723 struct udevice *bus;
1724 struct uclass *uc;
1725 int ret;
1726
1727 ret = uclass_get(UCLASS_I2C, &uc);
1728 if (ret)
1729 return CMD_RET_FAILURE;
1730 uclass_foreach_dev(bus, uc)
1731 show_bus(bus);
1732#else
1733 int i;
1734
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001735 for (i = 0; i < CONFIG_SYS_NUM_I2C_BUSES; i++) {
1736 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1737#ifndef CONFIG_SYS_I2C_DIRECT_BUS
Simon Glass59aa9df2015-05-04 11:30:57 -06001738 int j;
1739
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001740 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1741 if (i2c_bus[i].next_hop[j].chip == 0)
1742 break;
1743 printf("->%s@0x%2x:%d",
1744 i2c_bus[i].next_hop[j].mux.name,
1745 i2c_bus[i].next_hop[j].chip,
1746 i2c_bus[i].next_hop[j].channel);
Heiko Schocher67b23a32008-10-15 09:39:47 +02001747 }
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001748#endif
1749 printf("\n");
Heiko Schocher67b23a32008-10-15 09:39:47 +02001750 }
Simon Glass59aa9df2015-05-04 11:30:57 -06001751#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001752 } else {
Simon Glass59aa9df2015-05-04 11:30:57 -06001753 int i;
1754
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001755 /* show specific bus */
1756 i = simple_strtoul(argv[1], NULL, 10);
Simon Glass59aa9df2015-05-04 11:30:57 -06001757#ifdef CONFIG_DM_I2C
1758 struct udevice *bus;
1759 int ret;
1760
1761 ret = uclass_get_device_by_seq(UCLASS_I2C, i, &bus);
1762 if (ret) {
1763 printf("Invalid bus %d: err=%d\n", i, ret);
1764 return CMD_RET_FAILURE;
1765 }
1766 show_bus(bus);
1767#else
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001768 if (i >= CONFIG_SYS_NUM_I2C_BUSES) {
1769 printf("Invalid bus %d\n", i);
1770 return -1;
1771 }
1772 printf("Bus %d:\t%s", i, I2C_ADAP_NR(i)->name);
1773#ifndef CONFIG_SYS_I2C_DIRECT_BUS
Simon Glass59aa9df2015-05-04 11:30:57 -06001774 int j;
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001775 for (j = 0; j < CONFIG_SYS_I2C_MAX_HOPS; j++) {
1776 if (i2c_bus[i].next_hop[j].chip == 0)
1777 break;
1778 printf("->%s@0x%2x:%d",
1779 i2c_bus[i].next_hop[j].mux.name,
1780 i2c_bus[i].next_hop[j].chip,
1781 i2c_bus[i].next_hop[j].channel);
1782 }
1783#endif
1784 printf("\n");
Simon Glass59aa9df2015-05-04 11:30:57 -06001785#endif
Heiko Schocher67b23a32008-10-15 09:39:47 +02001786 }
Heiko Schocher67b23a32008-10-15 09:39:47 +02001787
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001788 return 0;
1789}
1790#endif
1791
Marek Vasut06afa382012-11-12 14:34:27 +00001792/**
1793 * do_i2c_bus_num() - Handle the "i2c dev" command-line command
1794 * @cmdtp: Command data struct pointer
1795 * @flag: Command flag
1796 * @argc: Command-line argument count
1797 * @argv: Array of command-line arguments
1798 *
1799 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1800 * on error.
1801 */
Simon Glass63656b72014-12-10 08:55:48 -07001802#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) || \
1803 defined(CONFIG_DM_I2C)
Jeroen Hofstee0e350f82014-06-23 00:22:08 +02001804static int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc,
1805 char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001806{
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001807 int ret = 0;
Simon Glass63656b72014-12-10 08:55:48 -07001808 int bus_no;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001809
Simon Glass63656b72014-12-10 08:55:48 -07001810 if (argc == 1) {
Timur Tabie857a5b2006-11-28 12:09:35 -06001811 /* querying current setting */
Simon Glass63656b72014-12-10 08:55:48 -07001812#ifdef CONFIG_DM_I2C
1813 struct udevice *bus;
1814
1815 if (!i2c_get_cur_bus(&bus))
1816 bus_no = bus->seq;
1817 else
1818 bus_no = -1;
1819#else
1820 bus_no = i2c_get_bus_num();
1821#endif
1822 printf("Current bus is %d\n", bus_no);
1823 } else {
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001824 bus_no = simple_strtoul(argv[1], NULL, 10);
Heiko Schocher880a4122013-08-23 09:39:16 +02001825#if defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001826 if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
1827 printf("Invalid bus %d\n", bus_no);
1828 return -1;
1829 }
Heiko Schocher880a4122013-08-23 09:39:16 +02001830#endif
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001831 printf("Setting bus to %d\n", bus_no);
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001832#ifdef CONFIG_DM_I2C
1833 ret = cmd_i2c_set_bus_num(bus_no);
1834#else
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001835 ret = i2c_set_bus_num(bus_no);
Simon Glassf9a4c2d2015-01-12 18:02:07 -07001836#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001837 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001838 printf("Failure changing bus number (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001839 }
Simon Glass4fbd2582015-12-29 05:22:50 -07001840
1841 return ret ? CMD_RET_FAILURE : 0;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001842}
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001843#endif /* defined(CONFIG_SYS_I2C) */
Ben Warrenbb99ad62006-09-07 16:50:54 -04001844
Marek Vasut06afa382012-11-12 14:34:27 +00001845/**
1846 * do_i2c_bus_speed() - Handle the "i2c speed" command-line command
1847 * @cmdtp: Command data struct pointer
1848 * @flag: Command flag
1849 * @argc: Command-line argument count
1850 * @argv: Array of command-line arguments
1851 *
1852 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1853 * on error.
1854 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001855static int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001856{
1857 int speed, ret=0;
1858
Simon Glass63656b72014-12-10 08:55:48 -07001859#ifdef CONFIG_DM_I2C
1860 struct udevice *bus;
1861
1862 if (i2c_get_cur_bus(&bus))
1863 return 1;
1864#endif
1865 if (argc == 1) {
1866#ifdef CONFIG_DM_I2C
Simon Glassca88b9b2015-02-05 21:41:32 -07001867 speed = dm_i2c_get_bus_speed(bus);
Simon Glass63656b72014-12-10 08:55:48 -07001868#else
1869 speed = i2c_get_bus_speed();
1870#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001871 /* querying current speed */
Simon Glass63656b72014-12-10 08:55:48 -07001872 printf("Current bus speed=%d\n", speed);
1873 } else {
Ben Warrenbb99ad62006-09-07 16:50:54 -04001874 speed = simple_strtoul(argv[1], NULL, 10);
1875 printf("Setting bus speed to %d Hz\n", speed);
Simon Glass63656b72014-12-10 08:55:48 -07001876#ifdef CONFIG_DM_I2C
Simon Glassca88b9b2015-02-05 21:41:32 -07001877 ret = dm_i2c_set_bus_speed(bus, speed);
Simon Glass63656b72014-12-10 08:55:48 -07001878#else
Ben Warrenbb99ad62006-09-07 16:50:54 -04001879 ret = i2c_set_bus_speed(speed);
Simon Glass63656b72014-12-10 08:55:48 -07001880#endif
Timur Tabie857a5b2006-11-28 12:09:35 -06001881 if (ret)
Ben Warrenbb99ad62006-09-07 16:50:54 -04001882 printf("Failure changing bus speed (%d)\n", ret);
Ben Warrenbb99ad62006-09-07 16:50:54 -04001883 }
Simon Glass4fbd2582015-12-29 05:22:50 -07001884
1885 return ret ? CMD_RET_FAILURE : 0;
Ben Warrenbb99ad62006-09-07 16:50:54 -04001886}
1887
Marek Vasut06afa382012-11-12 14:34:27 +00001888/**
1889 * do_i2c_mm() - Handle the "i2c mm" command-line command
1890 * @cmdtp: Command data struct pointer
1891 * @flag: Command flag
1892 * @argc: Command-line argument count
1893 * @argv: Array of command-line arguments
1894 *
1895 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1896 * on error.
1897 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001898static int do_i2c_mm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001899{
1900 return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
1901}
1902
Marek Vasut06afa382012-11-12 14:34:27 +00001903/**
1904 * do_i2c_nm() - Handle the "i2c nm" command-line command
1905 * @cmdtp: Command data struct pointer
1906 * @flag: Command flag
1907 * @argc: Command-line argument count
1908 * @argv: Array of command-line arguments
1909 *
1910 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1911 * on error.
1912 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001913static int do_i2c_nm(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001914{
1915 return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
1916}
1917
Marek Vasut06afa382012-11-12 14:34:27 +00001918/**
1919 * do_i2c_reset() - Handle the "i2c reset" command-line command
1920 * @cmdtp: Command data struct pointer
1921 * @flag: Command flag
1922 * @argc: Command-line argument count
1923 * @argv: Array of command-line arguments
1924 *
1925 * Returns zero always.
1926 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001927static int do_i2c_reset(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001928{
Simon Glass63656b72014-12-10 08:55:48 -07001929#if defined(CONFIG_DM_I2C)
1930 struct udevice *bus;
1931
1932 if (i2c_get_cur_bus(&bus))
1933 return CMD_RET_FAILURE;
1934 if (i2c_deblock(bus)) {
1935 printf("Error: Not supported by the driver\n");
1936 return CMD_RET_FAILURE;
1937 }
1938#elif defined(CONFIG_SYS_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001939 i2c_init(I2C_ADAP->speed, I2C_ADAP->slaveaddr);
1940#else
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001941 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001942#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001943 return 0;
1944}
1945
1946static cmd_tbl_t cmd_i2c_sub[] = {
Simon Glass59aa9df2015-05-04 11:30:57 -06001947#if defined(CONFIG_SYS_I2C) || defined(CONFIG_DM_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001948 U_BOOT_CMD_MKENT(bus, 1, 1, do_i2c_show_bus, "", ""),
Heiko Schocher9a2accb2012-10-25 10:32:14 +02001949#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001950 U_BOOT_CMD_MKENT(crc32, 3, 1, do_i2c_crc, "", ""),
Heiko Schocher3f4978c2012-01-16 21:12:24 +00001951#if defined(CONFIG_SYS_I2C) || \
Simon Glass63656b72014-12-10 08:55:48 -07001952 defined(CONFIG_I2C_MULTI_BUS) || defined(CONFIG_DM_I2C)
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001953 U_BOOT_CMD_MKENT(dev, 1, 1, do_i2c_bus_num, "", ""),
1954#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00001955#if defined(CONFIG_I2C_EDID)
1956 U_BOOT_CMD_MKENT(edid, 1, 1, do_edid, "", ""),
1957#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001958 U_BOOT_CMD_MKENT(loop, 3, 1, do_i2c_loop, "", ""),
1959 U_BOOT_CMD_MKENT(md, 3, 1, do_i2c_md, "", ""),
1960 U_BOOT_CMD_MKENT(mm, 2, 1, do_i2c_mm, "", ""),
1961 U_BOOT_CMD_MKENT(mw, 3, 1, do_i2c_mw, "", ""),
1962 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1963 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
Frans Meulenbroeks652e5352010-02-25 10:12:16 +01001964 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
Lubomir Popoved16f142015-01-30 19:56:04 +02001965 U_BOOT_CMD_MKENT(write, 6, 0, do_i2c_write, "", ""),
Simon Glass63656b72014-12-10 08:55:48 -07001966#ifdef CONFIG_DM_I2C
1967 U_BOOT_CMD_MKENT(flags, 2, 1, do_i2c_flags, "", ""),
Simon Glassc10c8e32015-08-22 18:31:33 -06001968 U_BOOT_CMD_MKENT(olen, 2, 1, do_i2c_olen, "", ""),
Simon Glass63656b72014-12-10 08:55:48 -07001969#endif
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001970 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1971#if defined(CONFIG_CMD_SDRAM)
1972 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
1973#endif
1974 U_BOOT_CMD_MKENT(speed, 1, 1, do_i2c_bus_speed, "", ""),
1975};
1976
Michal Simeke4099c82015-12-04 16:56:57 +01001977static __maybe_unused void i2c_reloc(void)
1978{
1979 static int relocated;
1980
1981 if (!relocated) {
1982 fixup_cmdtable(cmd_i2c_sub, ARRAY_SIZE(cmd_i2c_sub));
1983 relocated = 1;
1984 };
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001985}
Heiko Schocherf1d2b312010-09-17 13:10:39 +02001986
Marek Vasut06afa382012-11-12 14:34:27 +00001987/**
1988 * do_i2c() - Handle the "i2c" command-line command
1989 * @cmdtp: Command data struct pointer
1990 * @flag: Command flag
1991 * @argc: Command-line argument count
1992 * @argv: Array of command-line arguments
1993 *
1994 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
1995 * on error.
1996 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001997static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Ben Warrenbb99ad62006-09-07 16:50:54 -04001998{
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01001999 cmd_tbl_t *c;
2000
Michal Simeke4099c82015-12-04 16:56:57 +01002001#ifdef CONFIG_NEEDS_MANUAL_RELOC
2002 i2c_reloc();
2003#endif
2004
Heiko Schocher4444b222010-09-17 13:10:35 +02002005 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00002006 return CMD_RET_USAGE;
Heiko Schocher4444b222010-09-17 13:10:35 +02002007
Peter Tysere96ad5d2009-04-18 22:34:04 -05002008 /* Strip off leading 'i2c' command argument */
2009 argc--;
2010 argv++;
2011
Frans Meulenbroeksbfc3b772010-02-25 10:12:14 +01002012 c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
2013
Wolfgang Denk47e26b12010-07-17 01:06:04 +02002014 if (c)
Simon Glass4c12eeb2011-12-10 08:44:01 +00002015 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +02002016 else
Simon Glass4c12eeb2011-12-10 08:44:01 +00002017 return CMD_RET_USAGE;
Ben Warrenbb99ad62006-09-07 16:50:54 -04002018}
wdenk8bde7f72003-06-27 21:31:46 +00002019
2020/***************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00002021#ifdef CONFIG_SYS_LONGHELP
2022static char i2c_help_text[] =
Simon Glass59aa9df2015-05-04 11:30:57 -06002023#if defined(CONFIG_SYS_I2C) || defined(CONFIG_DM_I2C)
Heiko Schocher3f4978c2012-01-16 21:12:24 +00002024 "bus [muxtype:muxaddr:muxchannel] - show I2C bus info\n"
Christoph Muellner19f8c4d2018-12-04 11:27:18 +01002025 "i2c " /* That's the prefix for the crc32 command below. */
Heiko Schocher9a2accb2012-10-25 10:32:14 +02002026#endif
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01002027 "crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
Heiko Schocher3f4978c2012-01-16 21:12:24 +00002028#if defined(CONFIG_SYS_I2C) || \
Simon Glass63656b72014-12-10 08:55:48 -07002029 defined(CONFIG_I2C_MULTI_BUS) || defined(CONFIG_DM_I2C)
Peter Tyser9bc2e4e2008-10-01 12:25:04 -05002030 "i2c dev [dev] - show or set current I2C bus\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01002031#endif /* CONFIG_I2C_MULTI_BUS */
Tom Wai-Hong Tam735987c2012-12-05 14:46:40 +00002032#if defined(CONFIG_I2C_EDID)
2033 "i2c edid chip - print EDID configuration information\n"
2034#endif /* CONFIG_I2C_EDID */
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01002035 "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device\n"
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01002036 "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
2037 "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
2038 "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
2039 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
Eric Nelson54b99e52012-09-23 10:12:56 +00002040 "i2c probe [address] - test for and show device(s) on the I2C bus\n"
Simon Glass63656b72014-12-10 08:55:48 -07002041 "i2c read chip address[.0, .1, .2] length memaddress - read to memory\n"
Lubomir Popoved16f142015-01-30 19:56:04 +02002042 "i2c write memaddress chip address[.0, .1, .2] length [-s] - write memory\n"
2043 " to I2C; the -s option selects bulk write in a single transaction\n"
Simon Glass63656b72014-12-10 08:55:48 -07002044#ifdef CONFIG_DM_I2C
2045 "i2c flags chip [flags] - set or get chip flags\n"
Simon Glassc10c8e32015-08-22 18:31:33 -06002046 "i2c olen chip [offset_length] - set or get chip offset length\n"
Simon Glass63656b72014-12-10 08:55:48 -07002047#endif
Heiko Schochere43a27c2008-10-15 09:33:30 +02002048 "i2c reset - re-init the I2C Controller\n"
Jon Loeligerc76fe472007-07-08 18:02:23 -05002049#if defined(CONFIG_CMD_SDRAM)
Frans Meulenbroeksfb0070e2010-02-25 10:12:15 +01002050 "i2c sdram chip - print SDRAM configuration information\n"
Jon Loeliger90253172007-07-10 11:02:44 -05002051#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00002052 "i2c speed [speed] - show or set I2C bus speed";
2053#endif
2054
2055U_BOOT_CMD(
Lubomir Popoved16f142015-01-30 19:56:04 +02002056 i2c, 7, 1, do_i2c,
Kim Phillips088f1b12012-10-29 13:34:31 +00002057 "I2C sub-system",
2058 i2c_help_text
Matthias Fuchsd9fc7032007-03-08 16:25:47 +01002059);