Commit ff5d2dce1e8b24e9f4d85db3906c5d2e25b0cedf

Authored by York Sun
Committed by Heiko Schocher
1 parent f539094f48

common/i2c: Add i2c write command

Add i2c write command to write data from memory to i2c devices.

Signed-off-by: York Sun <yorksun@freescale.com>

Showing 1 changed file with 50 additions and 0 deletions Side-by-side Diff

... ... @@ -223,7 +223,55 @@
223 223 return 0;
224 224 }
225 225  
  226 +static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  227 +{
  228 + u_char chip;
  229 + uint devaddr, alen, length;
  230 + u_char *memaddr;
  231 +
  232 + if (argc != 5)
  233 + return cmd_usage(cmdtp);
  234 +
  235 + /*
  236 + * memaddr is the address where to store things in memory
  237 + */
  238 + memaddr = (u_char *)simple_strtoul(argv[1], NULL, 16);
  239 +
  240 + /*
  241 + * I2C chip address
  242 + */
  243 + chip = simple_strtoul(argv[2], NULL, 16);
  244 +
  245 + /*
  246 + * I2C data address within the chip. This can be 1 or
  247 + * 2 bytes long. Some day it might be 3 bytes long :-).
  248 + */
  249 + devaddr = simple_strtoul(argv[3], NULL, 16);
  250 + alen = get_alen(argv[3]);
  251 + if (alen > 3)
  252 + return cmd_usage(cmdtp);
  253 +
  254 + /*
  255 + * Length is the number of objects, not number of bytes.
  256 + */
  257 + length = simple_strtoul(argv[4], NULL, 16);
  258 +
  259 + while (length-- > 0) {
  260 + if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
  261 + puts("Error writing to the chip.\n");
  262 + return 1;
  263 + }
226 264 /*
  265 + * No write delay with FRAM devices.
  266 + */
  267 +#if !defined(CONFIG_SYS_I2C_FRAM)
  268 + udelay(11000);
  269 +#endif
  270 + }
  271 + return 0;
  272 +}
  273 +
  274 +/*
227 275 * Syntax:
228 276 * i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
229 277 */
... ... @@ -1282,6 +1330,7 @@
1282 1330 U_BOOT_CMD_MKENT(nm, 2, 1, do_i2c_nm, "", ""),
1283 1331 U_BOOT_CMD_MKENT(probe, 0, 1, do_i2c_probe, "", ""),
1284 1332 U_BOOT_CMD_MKENT(read, 5, 1, do_i2c_read, "", ""),
  1333 + U_BOOT_CMD_MKENT(write, 5, 0, do_i2c_write, "", ""),
1285 1334 U_BOOT_CMD_MKENT(reset, 0, 1, do_i2c_reset, "", ""),
1286 1335 #if defined(CONFIG_CMD_SDRAM)
1287 1336 U_BOOT_CMD_MKENT(sdram, 1, 1, do_sdram, "", ""),
... ... @@ -1333,6 +1382,7 @@
1333 1382 "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
1334 1383 "i2c probe - show devices on the I2C bus\n"
1335 1384 "i2c read chip address[.0, .1, .2] length memaddress - read to memory \n"
  1385 + "i2c write memaddress chip address[.0, .1, .2] length - write memory to i2c\n"
1336 1386 "i2c reset - re-init the I2C Controller\n"
1337 1387 #if defined(CONFIG_CMD_SDRAM)
1338 1388 "i2c sdram chip - print SDRAM configuration information\n"