Advertisement
Guest User

Untitled

a guest
May 11th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 37.47 KB | None | 0 0
  1. /*
  2.  * U-Boot 2009.08 (Dec 21 2011 - 14:48:38)
  3.  * (C) Copyright 2001
  4.  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  5.  *
  6.  * See file CREDITS for list of people who contributed to this
  7.  * project.
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License as
  11.  * published by the Free Software Foundation; either version 2 of
  12.  * the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22.  * MA 02111-1307 USA
  23.  */
  24.  
  25. /*
  26.  * I2C Functions similar to the standard memory functions.
  27.  *
  28.  * There are several parameters in many of the commands that bear further
  29.  * explanations:
  30.  *
  31.  * {i2c_chip} is the I2C chip address (the first byte sent on the bus).
  32.  *   Each I2C chip on the bus has a unique address.  On the I2C data bus,
  33.  *   the address is the upper seven bits and the LSB is the "read/write"
  34.  *   bit.  Note that the {i2c_chip} address specified on the command
  35.  *   line is not shifted up: e.g. a typical EEPROM memory chip may have
  36.  *   an I2C address of 0x50, but the data put on the bus will be 0xA0
  37.  *   for write and 0xA1 for read.  This "non shifted" address notation
  38.  *   matches at least half of the data sheets :-/.
  39.  *
  40.  * {addr} is the address (or offset) within the chip.  Small memory
  41.  *   chips have 8 bit addresses.  Large memory chips have 16 bit
  42.  *   addresses.  Other memory chips have 9, 10, or 11 bit addresses.
  43.  *   Many non-memory chips have multiple registers and {addr} is used
  44.  *   as the register index.  Some non-memory chips have only one register
  45.  *   and therefore don't need any {addr} parameter.
  46.  *
  47.  *   The default {addr} parameter is one byte (.1) which works well for
  48.  *   memories and registers with 8 bits of address space.
  49.  *
  50.  *   You can specify the length of the {addr} field with the optional .0,
  51.  *   .1, or .2 modifier (similar to the .b, .w, .l modifier).  If you are
  52.  *   manipulating a single register device which doesn't use an address
  53.  *   field, use "0.0" for the address and the ".0" length field will
  54.  *   suppress the address in the I2C data stream.  This also works for
  55.  *   successive reads using the I2C auto-incrementing memory pointer.
  56.  *
  57.  *   If you are manipulating a large memory with 2-byte addresses, use
  58.  *   the .2 address modifier, e.g. 210.2 addresses location 528 (decimal).
  59.  *
  60.  *   Then there are the unfortunate memory chips that spill the most
  61.  *   significant 1, 2, or 3 bits of address into the chip address byte.
  62.  *   This effectively makes one chip (logically) look like 2, 4, or
  63.  *   8 chips.  This is handled (awkwardly) by #defining
  64.  *   CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW and using the .1 modifier on the
  65.  *   {addr} field (since .1 is the default, it doesn't actually have to
  66.  *   be specified).  Examples: given a memory chip at I2C chip address
  67.  *   0x50, the following would happen...
  68.  *     i2c md 50 0 10   display 16 bytes starting at 0x000
  69.  *                      On the bus: <S> A0 00 <E> <S> A1 <rd> ... <rd>
  70.  *     i2c md 50 100 10 display 16 bytes starting at 0x100
  71.  *                      On the bus: <S> A2 00 <E> <S> A3 <rd> ... <rd>
  72.  *     i2c md 50 210 10 display 16 bytes starting at 0x210
  73.  *                      On the bus: <S> A4 10 <E> <S> A5 <rd> ... <rd>
  74.  *   This is awfully ugly.  It would be nice if someone would think up
  75.  *   a better way of handling this.
  76.  *
  77.  * Adapted from cmd_mem.c which is copyright Wolfgang Denk (wd@denx.de).
  78.  */
  79.  
  80. #include <common.h>
  81. #include <command.h>
  82. #include <environment.h>
  83. #include <i2c.h>
  84. #include <malloc.h>
  85. #include <asm/byteorder.h>
  86.  
  87. /* Display values from last command.
  88.  * Memory modify remembered values are different from display memory.
  89.  */
  90. static uchar    i2c_dp_last_chip;
  91. static uint i2c_dp_last_addr;
  92. static uint i2c_dp_last_alen;
  93. static uint i2c_dp_last_length = 0x10;
  94.  
  95. static uchar    i2c_mm_last_chip;
  96. static uint i2c_mm_last_addr;
  97. static uint i2c_mm_last_alen;
  98.  
  99. /* If only one I2C bus is present, the list of devices to ignore when
  100.  * the probe command is issued is represented by a 1D array of addresses.
  101.  * When multiple buses are present, the list is an array of bus-address
  102.  * pairs.  The following macros take care of this */
  103.  
  104. #if defined(CONFIG_SYS_I2C_NOPROBES)
  105. #if defined(CONFIG_I2C_MULTI_BUS)
  106. static struct
  107. {
  108.     uchar   bus;
  109.     uchar   addr;
  110. } i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
  111. #define GET_BUS_NUM i2c_get_bus_num()
  112. #define COMPARE_BUS(b,i)    (i2c_no_probes[(i)].bus == (b))
  113. #define COMPARE_ADDR(a,i)   (i2c_no_probes[(i)].addr == (a))
  114. #define NO_PROBE_ADDR(i)    i2c_no_probes[(i)].addr
  115. #else       /* single bus */
  116. static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
  117. #define GET_BUS_NUM 0
  118. #define COMPARE_BUS(b,i)    ((b) == 0)  /* Make compiler happy */
  119. #define COMPARE_ADDR(a,i)   (i2c_no_probes[(i)] == (a))
  120. #define NO_PROBE_ADDR(i)    i2c_no_probes[(i)]
  121. #endif  /* CONFIG_MULTI_BUS */
  122.  
  123. #define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
  124. #endif
  125.  
  126. #if defined(CONFIG_I2C_MUX)
  127. static I2C_MUX_DEVICE   *i2c_mux_devices = NULL;
  128. static  int i2c_mux_busid = CONFIG_SYS_MAX_I2C_BUS;
  129.  
  130. DECLARE_GLOBAL_DATA_PTR;
  131.  
  132. #endif
  133.  
  134. /* TODO: Implement architecture-specific get/set functions */
  135. unsigned int __def_i2c_get_bus_speed(void)
  136. {
  137.     return CONFIG_SYS_I2C_SPEED;
  138. }
  139. unsigned int i2c_get_bus_speed(void)
  140.     __attribute__((weak, alias("__def_i2c_get_bus_speed")));
  141.  
  142. int __def_i2c_set_bus_speed(unsigned int speed)
  143. {
  144.     if (speed != CONFIG_SYS_I2C_SPEED)
  145.         return -1;
  146.  
  147.     return 0;
  148. }
  149. int i2c_set_bus_speed(unsigned int)
  150.     __attribute__((weak, alias("__def_i2c_set_bus_speed")));
  151.  
  152. /*
  153.  * Syntax:
  154.  *  i2c md {i2c_chip} {addr}{.0, .1, .2} {len}
  155.  */
  156. #define DISP_LINE_LEN   16
  157.  
  158. int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  159. {
  160.     u_char  chip;
  161.     uint    addr, alen, length;
  162.     int j, nbytes, linebytes;
  163.  
  164.     /* We use the last specified parameters, unless new ones are
  165.      * entered.
  166.      */
  167.     chip   = i2c_dp_last_chip;
  168.     addr   = i2c_dp_last_addr;
  169.     alen   = i2c_dp_last_alen;
  170.     length = i2c_dp_last_length;
  171.  
  172.     if (argc < 3) {
  173.         cmd_usage(cmdtp);
  174.         return 1;
  175.     }
  176.  
  177.     if ((flag & CMD_FLAG_REPEAT) == 0) {
  178.         /*
  179.          * New command specified.
  180.          */
  181.         alen = 1;
  182.  
  183.         /*
  184.          * I2C chip address
  185.          */
  186.         chip = simple_strtoul(argv[1], NULL, 16);
  187.  
  188.         /*
  189.          * I2C data address within the chip.  This can be 1 or
  190.          * 2 bytes long.  Some day it might be 3 bytes long :-).
  191.          */
  192.         addr = simple_strtoul(argv[2], NULL, 16);
  193.         alen = 1;
  194.         for (j = 0; j < 8; j++) {
  195.             if (argv[2][j] == '.') {
  196.                 alen = argv[2][j+1] - '0';
  197.                 if (alen > 4) {
  198.                     cmd_usage(cmdtp);
  199.                     return 1;
  200.                 }
  201.                 break;
  202.             } else if (argv[2][j] == '\0')
  203.                 break;
  204.         }
  205.  
  206.         /*
  207.          * If another parameter, it is the length to display.
  208.          * Length is the number of objects, not number of bytes.
  209.          */
  210.         if (argc > 3)
  211.             length = simple_strtoul(argv[3], NULL, 16);
  212.     }
  213.  
  214.     /*
  215.      * Print the lines.
  216.      *
  217.      * We buffer all read data, so we can make sure data is read only
  218.      * once.
  219.      */
  220.     nbytes = length;
  221.     do {
  222.         unsigned char   linebuf[DISP_LINE_LEN];
  223.         unsigned char   *cp;
  224.  
  225.         linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  226.  
  227.         if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
  228.             puts ("Error reading the chip.\n");
  229.         else {
  230.             printf("%04x:", addr);
  231.             cp = linebuf;
  232.             for (j=0; j<linebytes; j++) {
  233.                 printf(" %02x", *cp++);
  234.                 addr++;
  235.             }
  236.             puts ("    ");
  237.             cp = linebuf;
  238.             for (j=0; j<linebytes; j++) {
  239.                 if ((*cp < 0x20) || (*cp > 0x7e))
  240.                     puts (".");
  241.                 else
  242.                     printf("%c", *cp);
  243.                 cp++;
  244.             }
  245.             putc ('\n');
  246.         }
  247.         nbytes -= linebytes;
  248.     } while (nbytes > 0);
  249.  
  250.     i2c_dp_last_chip   = chip;
  251.     i2c_dp_last_addr   = addr;
  252.     i2c_dp_last_alen   = alen;
  253.     i2c_dp_last_length = length;
  254.  
  255.     return 0;
  256. }
  257.  
  258.  
  259. /* Write (fill) memory
  260.  *
  261.  * Syntax:
  262.  *  i2c mw {i2c_chip} {addr}{.0, .1, .2} {data} [{count}]
  263.  */
  264. int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  265. {
  266.     uchar   chip;
  267.     ulong   addr;
  268.     uint    alen;
  269.     uchar   byte;
  270.     int count;
  271.     int j;
  272.  
  273.     if ((argc < 4) || (argc > 5)) {
  274.         cmd_usage(cmdtp);
  275.         return 1;
  276.     }
  277.  
  278.     /*
  279.      * Chip is always specified.
  280.      */
  281.     chip = simple_strtoul(argv[1], NULL, 16);
  282.  
  283.     /*
  284.      * Address is always specified.
  285.      */
  286.     addr = simple_strtoul(argv[2], NULL, 16);
  287.     alen = 1;
  288.     for (j = 0; j < 8; j++) {
  289.         if (argv[2][j] == '.') {
  290.             alen = argv[2][j+1] - '0';
  291.             if (alen > 4) {
  292.                 cmd_usage(cmdtp);
  293.                 return 1;
  294.             }
  295.             break;
  296.         } else if (argv[2][j] == '\0')
  297.             break;
  298.     }
  299.  
  300.     /*
  301.      * Value to write is always specified.
  302.      */
  303.     byte = simple_strtoul(argv[3], NULL, 16);
  304.  
  305.     /*
  306.      * Optional count
  307.      */
  308.     if (argc == 5)
  309.         count = simple_strtoul(argv[4], NULL, 16);
  310.     else
  311.         count = 1;
  312.  
  313.     while (count-- > 0) {
  314.         if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
  315.             puts ("Error writing the chip.\n");
  316.         /*
  317.          * Wait for the write to complete.  The write can take
  318.          * up to 10mSec (we allow a little more time).
  319.          *
  320.          * On some chips, while the write is in progress, the
  321.          * chip doesn't respond.  This apparently isn't a
  322.          * universal feature so we don't take advantage of it.
  323.          */
  324. /*
  325.  * No write delay with FRAM devices.
  326.  */
  327. #if !defined(CONFIG_SYS_I2C_FRAM)
  328.         udelay(11000);
  329. #endif
  330.  
  331. #if 0
  332.         for (timeout = 0; timeout < 10; timeout++) {
  333.             udelay(2000);
  334.             if (i2c_probe(chip) == 0)
  335.                 break;
  336.         }
  337. #endif
  338.     }
  339.  
  340.     return (0);
  341. }
  342.  
  343. /* Calculate a CRC on memory
  344.  *
  345.  * Syntax:
  346.  *  i2c crc32 {i2c_chip} {addr}{.0, .1, .2} {count}
  347.  */
  348. int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  349. {
  350.     uchar   chip;
  351.     ulong   addr;
  352.     uint    alen;
  353.     int count;
  354.     uchar   byte;
  355.     ulong   crc;
  356.     ulong   err;
  357.     int j;
  358.  
  359.     if (argc < 4) {
  360.         cmd_usage(cmdtp);
  361.         return 1;
  362.     }
  363.  
  364.     /*
  365.      * Chip is always specified.
  366.      */
  367.     chip = simple_strtoul(argv[1], NULL, 16);
  368.  
  369.     /*
  370.      * Address is always specified.
  371.      */
  372.     addr = simple_strtoul(argv[2], NULL, 16);
  373.     alen = 1;
  374.     for (j = 0; j < 8; j++) {
  375.         if (argv[2][j] == '.') {
  376.             alen = argv[2][j+1] - '0';
  377.             if (alen > 4) {
  378.                 cmd_usage(cmdtp);
  379.                 return 1;
  380.             }
  381.             break;
  382.         } else if (argv[2][j] == '\0')
  383.             break;
  384.     }
  385.  
  386.     /*
  387.      * Count is always specified
  388.      */
  389.     count = simple_strtoul(argv[3], NULL, 16);
  390.  
  391.     printf ("CRC32 for %08lx ... %08lx ==> ", addr, addr + count - 1);
  392.     /*
  393.      * CRC a byte at a time.  This is going to be slooow, but hey, the
  394.      * memories are small and slow too so hopefully nobody notices.
  395.      */
  396.     crc = 0;
  397.     err = 0;
  398.     while (count-- > 0) {
  399.         if (i2c_read(chip, addr, alen, &byte, 1) != 0)
  400.             err++;
  401.         crc = crc32 (crc, &byte, 1);
  402.         addr++;
  403.     }
  404.     if (err > 0)
  405.         puts ("Error reading the chip,\n");
  406.     else
  407.         printf ("%08lx\n", crc);
  408.  
  409.     return 0;
  410. }
  411.  
  412. /* Modify memory.
  413.  *
  414.  * Syntax:
  415.  *  i2c mm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  416.  *  i2c nm{.b, .w, .l} {i2c_chip} {addr}{.0, .1, .2}
  417.  */
  418.  
  419. static int
  420. mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
  421. {
  422.     uchar   chip;
  423.     ulong   addr;
  424.     uint    alen;
  425.     ulong   data;
  426.     int size = 1;
  427.     int nbytes;
  428.     int j;
  429.     extern char console_buffer[];
  430.  
  431.     if (argc != 3) {
  432.         cmd_usage(cmdtp);
  433.         return 1;
  434.     }
  435.  
  436. #ifdef CONFIG_BOOT_RETRY_TIME
  437.     reset_cmd_timeout();    /* got a good command to get here */
  438. #endif
  439.     /*
  440.      * We use the last specified parameters, unless new ones are
  441.      * entered.
  442.      */
  443.     chip = i2c_mm_last_chip;
  444.     addr = i2c_mm_last_addr;
  445.     alen = i2c_mm_last_alen;
  446.  
  447.     if ((flag & CMD_FLAG_REPEAT) == 0) {
  448.         /*
  449.          * New command specified.  Check for a size specification.
  450.          * Defaults to byte if no or incorrect specification.
  451.          */
  452.         size = cmd_get_data_size(argv[0], 1);
  453.  
  454.         /*
  455.          * Chip is always specified.
  456.          */
  457.         chip = simple_strtoul(argv[1], NULL, 16);
  458.  
  459.         /*
  460.          * Address is always specified.
  461.          */
  462.         addr = simple_strtoul(argv[2], NULL, 16);
  463.         alen = 1;
  464.         for (j = 0; j < 8; j++) {
  465.             if (argv[2][j] == '.') {
  466.                 alen = argv[2][j+1] - '0';
  467.                 if (alen > 4) {
  468.                     cmd_usage(cmdtp);
  469.                     return 1;
  470.                 }
  471.                 break;
  472.             } else if (argv[2][j] == '\0')
  473.                 break;
  474.         }
  475.     }
  476.  
  477.     /*
  478.      * Print the address, followed by value.  Then accept input for
  479.      * the next value.  A non-converted value exits.
  480.      */
  481.     do {
  482.         printf("%08lx:", addr);
  483.         if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
  484.             puts ("\nError reading the chip,\n");
  485.         else {
  486.             data = cpu_to_be32(data);
  487.             if (size == 1)
  488.                 printf(" %02lx", (data >> 24) & 0x000000FF);
  489.             else if (size == 2)
  490.                 printf(" %04lx", (data >> 16) & 0x0000FFFF);
  491.             else
  492.                 printf(" %08lx", data);
  493.         }
  494.  
  495.         nbytes = readline (" ? ");
  496.         if (nbytes == 0) {
  497.             /*
  498.              * <CR> pressed as only input, don't modify current
  499.              * location and move to next.
  500.              */
  501.             if (incrflag)
  502.                 addr += size;
  503.             nbytes = size;
  504. #ifdef CONFIG_BOOT_RETRY_TIME
  505.             reset_cmd_timeout(); /* good enough to not time out */
  506. #endif
  507.         }
  508. #ifdef CONFIG_BOOT_RETRY_TIME
  509.         else if (nbytes == -2)
  510.             break;  /* timed out, exit the command  */
  511. #endif
  512.         else {
  513.             char *endp;
  514.  
  515.             data = simple_strtoul(console_buffer, &endp, 16);
  516.             if (size == 1)
  517.                 data = data << 24;
  518.             else if (size == 2)
  519.                 data = data << 16;
  520.             data = be32_to_cpu(data);
  521.             nbytes = endp - console_buffer;
  522.             if (nbytes) {
  523. #ifdef CONFIG_BOOT_RETRY_TIME
  524.                 /*
  525.                  * good enough to not time out
  526.                  */
  527.                 reset_cmd_timeout();
  528. #endif
  529.                 if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
  530.                     puts ("Error writing the chip.\n");
  531. #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  532.                 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  533. #endif
  534.                 if (incrflag)
  535.                     addr += size;
  536.             }
  537.         }
  538.     } while (nbytes);
  539.  
  540.     i2c_mm_last_chip = chip;
  541.     i2c_mm_last_addr = addr;
  542.     i2c_mm_last_alen = alen;
  543.  
  544.     return 0;
  545. }
  546.  
  547. /*
  548.  * Syntax:
  549.  *  i2c probe {addr}{.0, .1, .2}
  550.  */
  551. int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  552. {
  553.     int j;
  554. #if defined(CONFIG_SYS_I2C_NOPROBES)
  555.     int k, skip;
  556.     uchar bus = GET_BUS_NUM;
  557. #endif  /* NOPROBES */
  558.  
  559.     puts ("Valid chip addresses:");
  560.     for (j = 0; j < 128; j++) {
  561. #if defined(CONFIG_SYS_I2C_NOPROBES)
  562.         skip = 0;
  563.         for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
  564.             if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
  565.                 skip = 1;
  566.                 break;
  567.             }
  568.         }
  569.         if (skip)
  570.             continue;
  571. #endif
  572.         if (i2c_probe(j) == 0)
  573.             printf(" %02X", j);
  574.     }
  575.     putc ('\n');
  576.  
  577. #if defined(CONFIG_SYS_I2C_NOPROBES)
  578.     puts ("Excluded chip addresses:");
  579.     for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
  580.         if (COMPARE_BUS(bus,k))
  581.             printf(" %02X", NO_PROBE_ADDR(k));
  582.     }
  583.     putc ('\n');
  584. #endif
  585.  
  586.     return 0;
  587. }
  588.  
  589. /*
  590.  * Syntax:
  591.  *  i2c loop {i2c_chip} {addr}{.0, .1, .2} [{length}] [{delay}]
  592.  *  {length} - Number of bytes to read
  593.  *  {delay}  - A DECIMAL number and defaults to 1000 uSec
  594.  */
  595. int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  596. {
  597.     u_char  chip;
  598.     ulong   alen;
  599.     uint    addr;
  600.     uint    length;
  601.     u_char  bytes[16];
  602.     int delay;
  603.     int j;
  604.  
  605.     if (argc < 3) {
  606.         cmd_usage(cmdtp);
  607.         return 1;
  608.     }
  609.  
  610.     /*
  611.      * Chip is always specified.
  612.      */
  613.     chip = simple_strtoul(argv[1], NULL, 16);
  614.  
  615.     /*
  616.      * Address is always specified.
  617.      */
  618.     addr = simple_strtoul(argv[2], NULL, 16);
  619.     alen = 1;
  620.     for (j = 0; j < 8; j++) {
  621.         if (argv[2][j] == '.') {
  622.             alen = argv[2][j+1] - '0';
  623.             if (alen > 4) {
  624.                 cmd_usage(cmdtp);
  625.                 return 1;
  626.             }
  627.             break;
  628.         } else if (argv[2][j] == '\0')
  629.             break;
  630.     }
  631.  
  632.     /*
  633.      * Length is the number of objects, not number of bytes.
  634.      */
  635.     length = 1;
  636.     length = simple_strtoul(argv[3], NULL, 16);
  637.     if (length > sizeof(bytes))
  638.         length = sizeof(bytes);
  639.  
  640.     /*
  641.      * The delay time (uSec) is optional.
  642.      */
  643.     delay = 1000;
  644.     if (argc > 3)
  645.         delay = simple_strtoul(argv[4], NULL, 10);
  646.     /*
  647.      * Run the loop...
  648.      */
  649.     while (1) {
  650.         if (i2c_read(chip, addr, alen, bytes, length) != 0)
  651.             puts ("Error reading the chip.\n");
  652.         udelay(delay);
  653.     }
  654.  
  655.     /* NOTREACHED */
  656.     return 0;
  657. }
  658.  
  659. /*
  660.  * The SDRAM command is separately configured because many
  661.  * (most?) embedded boards don't use SDRAM DIMMs.
  662.  */
  663. #if defined(CONFIG_CMD_SDRAM)
  664. static void print_ddr2_tcyc (u_char const b)
  665. {
  666.     printf ("%d.", (b >> 4) & 0x0F);
  667.     switch (b & 0x0F) {
  668.     case 0x0:
  669.     case 0x1:
  670.     case 0x2:
  671.     case 0x3:
  672.     case 0x4:
  673.     case 0x5:
  674.     case 0x6:
  675.     case 0x7:
  676.     case 0x8:
  677.     case 0x9:
  678.         printf ("%d ns\n", b & 0x0F);
  679.         break;
  680.     case 0xA:
  681.         puts ("25 ns\n");
  682.         break;
  683.     case 0xB:
  684.         puts ("33 ns\n");
  685.         break;
  686.     case 0xC:
  687.         puts ("66 ns\n");
  688.         break;
  689.     case 0xD:
  690.         puts ("75 ns\n");
  691.         break;
  692.     default:
  693.         puts ("?? ns\n");
  694.         break;
  695.     }
  696. }
  697.  
  698. static void decode_bits (u_char const b, char const *str[], int const do_once)
  699. {
  700.     u_char mask;
  701.  
  702.     for (mask = 0x80; mask != 0x00; mask >>= 1, ++str) {
  703.         if (b & mask) {
  704.             puts (*str);
  705.             if (do_once)
  706.                 return;
  707.         }
  708.     }
  709. }
  710.  
  711. /*
  712.  * Syntax:
  713.  *  i2c sdram {i2c_chip}
  714.  */
  715. int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  716. {
  717.     enum { unknown, EDO, SDRAM, DDR2 } type;
  718.  
  719.     u_char  chip;
  720.     u_char  data[128];
  721.     u_char  cksum;
  722.     int j;
  723.  
  724.     static const char *decode_CAS_DDR2[] = {
  725.         " TBD", " 6", " 5", " 4", " 3", " 2", " TBD", " TBD"
  726.     };
  727.  
  728.     static const char *decode_CAS_default[] = {
  729.         " TBD", " 7", " 6", " 5", " 4", " 3", " 2", " 1"
  730.     };
  731.  
  732.     static const char *decode_CS_WE_default[] = {
  733.         " TBD", " 6", " 5", " 4", " 3", " 2", " 1", " 0"
  734.     };
  735.  
  736.     static const char *decode_byte21_default[] = {
  737.         "  TBD (bit 7)\n",
  738.         "  Redundant row address\n",
  739.         "  Differential clock input\n",
  740.         "  Registerd DQMB inputs\n",
  741.         "  Buffered DQMB inputs\n",
  742.         "  On-card PLL\n",
  743.         "  Registered address/control lines\n",
  744.         "  Buffered address/control lines\n"
  745.     };
  746.  
  747.     static const char *decode_byte22_DDR2[] = {
  748.         "  TBD (bit 7)\n",
  749.         "  TBD (bit 6)\n",
  750.         "  TBD (bit 5)\n",
  751.         "  TBD (bit 4)\n",
  752.         "  TBD (bit 3)\n",
  753.         "  Supports partial array self refresh\n",
  754.         "  Supports 50 ohm ODT\n",
  755.         "  Supports weak driver\n"
  756.     };
  757.  
  758.     static const char *decode_row_density_DDR2[] = {
  759.         "512 MiB", "256 MiB", "128 MiB", "16 GiB",
  760.         "8 GiB", "4 GiB", "2 GiB", "1 GiB"
  761.     };
  762.  
  763.     static const char *decode_row_density_default[] = {
  764.         "512 MiB", "256 MiB", "128 MiB", "64 MiB",
  765.         "32 MiB", "16 MiB", "8 MiB", "4 MiB"
  766.     };
  767.  
  768.     if (argc < 2) {
  769.         cmd_usage(cmdtp);
  770.         return 1;
  771.     }
  772.     /*
  773.      * Chip is always specified.
  774.      */
  775.     chip = simple_strtoul (argv[1], NULL, 16);
  776.  
  777.     if (i2c_read (chip, 0, 1, data, sizeof (data)) != 0) {
  778.         puts ("No SDRAM Serial Presence Detect found.\n");
  779.         return 1;
  780.     }
  781.  
  782.     cksum = 0;
  783.     for (j = 0; j < 63; j++) {
  784.         cksum += data[j];
  785.     }
  786.     if (cksum != data[63]) {
  787.         printf ("WARNING: Configuration data checksum failure:\n"
  788.             "  is 0x%02x, calculated 0x%02x\n", data[63], cksum);
  789.     }
  790.     printf ("SPD data revision            %d.%d\n",
  791.         (data[62] >> 4) & 0x0F, data[62] & 0x0F);
  792.     printf ("Bytes used                   0x%02X\n", data[0]);
  793.     printf ("Serial memory size           0x%02X\n", 1 << data[1]);
  794.  
  795.     puts ("Memory type                  ");
  796.     switch (data[2]) {
  797.     case 2:
  798.         type = EDO;
  799.         puts ("EDO\n");
  800.         break;
  801.     case 4:
  802.         type = SDRAM;
  803.         puts ("SDRAM\n");
  804.         break;
  805.     case 8:
  806.         type = DDR2;
  807.         puts ("DDR2\n");
  808.         break;
  809.     default:
  810.         type = unknown;
  811.         puts ("unknown\n");
  812.         break;
  813.     }
  814.  
  815.     puts ("Row address bits             ");
  816.     if ((data[3] & 0x00F0) == 0)
  817.         printf ("%d\n", data[3] & 0x0F);
  818.     else
  819.         printf ("%d/%d\n", data[3] & 0x0F, (data[3] >> 4) & 0x0F);
  820.  
  821.     puts ("Column address bits          ");
  822.     if ((data[4] & 0x00F0) == 0)
  823.         printf ("%d\n", data[4] & 0x0F);
  824.     else
  825.         printf ("%d/%d\n", data[4] & 0x0F, (data[4] >> 4) & 0x0F);
  826.  
  827.     switch (type) {
  828.     case DDR2:
  829.         printf ("Number of ranks              %d\n",
  830.             (data[5] & 0x07) + 1);
  831.         break;
  832.     default:
  833.         printf ("Module rows                  %d\n", data[5]);
  834.         break;
  835.     }
  836.  
  837.     switch (type) {
  838.     case DDR2:
  839.         printf ("Module data width            %d bits\n", data[6]);
  840.         break;
  841.     default:
  842.         printf ("Module data width            %d bits\n",
  843.             (data[7] << 8) | data[6]);
  844.         break;
  845.     }
  846.  
  847.     puts ("Interface signal levels      ");
  848.     switch(data[8]) {
  849.         case 0:  puts ("TTL 5.0 V\n");  break;
  850.         case 1:  puts ("LVTTL\n");  break;
  851.         case 2:  puts ("HSTL 1.5 V\n"); break;
  852.         case 3:  puts ("SSTL 3.3 V\n"); break;
  853.         case 4:  puts ("SSTL 2.5 V\n"); break;
  854.         case 5:  puts ("SSTL 1.8 V\n"); break;
  855.         default: puts ("unknown\n");    break;
  856.     }
  857.  
  858.     switch (type) {
  859.     case DDR2:
  860.         printf ("SDRAM cycle time             ");
  861.         print_ddr2_tcyc (data[9]);
  862.         break;
  863.     default:
  864.         printf ("SDRAM cycle time             %d.%d ns\n",
  865.             (data[9] >> 4) & 0x0F, data[9] & 0x0F);
  866.         break;
  867.     }
  868.  
  869.     switch (type) {
  870.     case DDR2:
  871.         printf ("SDRAM access time            0.%d%d ns\n",
  872.             (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  873.         break;
  874.     default:
  875.         printf ("SDRAM access time            %d.%d ns\n",
  876.             (data[10] >> 4) & 0x0F, data[10] & 0x0F);
  877.         break;
  878.     }
  879.  
  880.     puts ("EDC configuration            ");
  881.     switch (data[11]) {
  882.         case 0:  puts ("None\n");   break;
  883.         case 1:  puts ("Parity\n"); break;
  884.         case 2:  puts ("ECC\n");    break;
  885.         default: puts ("unknown\n");    break;
  886.     }
  887.  
  888.     if ((data[12] & 0x80) == 0)
  889.         puts ("No self refresh, rate        ");
  890.     else
  891.         puts ("Self refresh, rate           ");
  892.  
  893.     switch(data[12] & 0x7F) {
  894.         case 0:  puts ("15.625 us\n");  break;
  895.         case 1:  puts ("3.9 us\n"); break;
  896.         case 2:  puts ("7.8 us\n"); break;
  897.         case 3:  puts ("31.3 us\n");    break;
  898.         case 4:  puts ("62.5 us\n");    break;
  899.         case 5:  puts ("125 us\n"); break;
  900.         default: puts ("unknown\n");    break;
  901.     }
  902.  
  903.     switch (type) {
  904.     case DDR2:
  905.         printf ("SDRAM width (primary)        %d\n", data[13]);
  906.         break;
  907.     default:
  908.         printf ("SDRAM width (primary)        %d\n", data[13] & 0x7F);
  909.         if ((data[13] & 0x80) != 0) {
  910.             printf ("  (second bank)              %d\n",
  911.                 2 * (data[13] & 0x7F));
  912.         }
  913.         break;
  914.     }
  915.  
  916.     switch (type) {
  917.     case DDR2:
  918.         if (data[14] != 0)
  919.             printf ("EDC width                    %d\n", data[14]);
  920.         break;
  921.     default:
  922.         if (data[14] != 0) {
  923.             printf ("EDC width                    %d\n",
  924.                 data[14] & 0x7F);
  925.  
  926.             if ((data[14] & 0x80) != 0) {
  927.                 printf ("  (second bank)              %d\n",
  928.                     2 * (data[14] & 0x7F));
  929.             }
  930.         }
  931.         break;
  932.     }
  933.  
  934.     if (DDR2 != type) {
  935.         printf ("Min clock delay, back-to-back random column addresses "
  936.             "%d\n", data[15]);
  937.     }
  938.  
  939.     puts ("Burst length(s)             ");
  940.     if (data[16] & 0x80) puts (" Page");
  941.     if (data[16] & 0x08) puts (" 8");
  942.     if (data[16] & 0x04) puts (" 4");
  943.     if (data[16] & 0x02) puts (" 2");
  944.     if (data[16] & 0x01) puts (" 1");
  945.     putc ('\n');
  946.     printf ("Number of banks              %d\n", data[17]);
  947.  
  948.     switch (type) {
  949.     case DDR2:
  950.         puts ("CAS latency(s)              ");
  951.         decode_bits (data[18], decode_CAS_DDR2, 0);
  952.         putc ('\n');
  953.         break;
  954.     default:
  955.         puts ("CAS latency(s)              ");
  956.         decode_bits (data[18], decode_CAS_default, 0);
  957.         putc ('\n');
  958.         break;
  959.     }
  960.  
  961.     if (DDR2 != type) {
  962.         puts ("CS latency(s)               ");
  963.         decode_bits (data[19], decode_CS_WE_default, 0);
  964.         putc ('\n');
  965.     }
  966.  
  967.     if (DDR2 != type) {
  968.         puts ("WE latency(s)               ");
  969.         decode_bits (data[20], decode_CS_WE_default, 0);
  970.         putc ('\n');
  971.     }
  972.  
  973.     switch (type) {
  974.     case DDR2:
  975.         puts ("Module attributes:\n");
  976.         if (data[21] & 0x80)
  977.             puts ("  TBD (bit 7)\n");
  978.         if (data[21] & 0x40)
  979.             puts ("  Analysis probe installed\n");
  980.         if (data[21] & 0x20)
  981.             puts ("  TBD (bit 5)\n");
  982.         if (data[21] & 0x10)
  983.             puts ("  FET switch external enable\n");
  984.         printf ("  %d PLLs on DIMM\n", (data[21] >> 2) & 0x03);
  985.         if (data[20] & 0x11) {
  986.             printf ("  %d active registers on DIMM\n",
  987.                 (data[21] & 0x03) + 1);
  988.         }
  989.         break;
  990.     default:
  991.         puts ("Module attributes:\n");
  992.         if (!data[21])
  993.             puts ("  (none)\n");
  994.         else
  995.             decode_bits (data[21], decode_byte21_default, 0);
  996.         break;
  997.     }
  998.  
  999.     switch (type) {
  1000.     case DDR2:
  1001.         decode_bits (data[22], decode_byte22_DDR2, 0);
  1002.         break;
  1003.     default:
  1004.         puts ("Device attributes:\n");
  1005.         if (data[22] & 0x80) puts ("  TBD (bit 7)\n");
  1006.         if (data[22] & 0x40) puts ("  TBD (bit 6)\n");
  1007.         if (data[22] & 0x20) puts ("  Upper Vcc tolerance 5%\n");
  1008.         else                 puts ("  Upper Vcc tolerance 10%\n");
  1009.         if (data[22] & 0x10) puts ("  Lower Vcc tolerance 5%\n");
  1010.         else                 puts ("  Lower Vcc tolerance 10%\n");
  1011.         if (data[22] & 0x08) puts ("  Supports write1/read burst\n");
  1012.         if (data[22] & 0x04) puts ("  Supports precharge all\n");
  1013.         if (data[22] & 0x02) puts ("  Supports auto precharge\n");
  1014.         if (data[22] & 0x01) puts ("  Supports early RAS# precharge\n");
  1015.         break;
  1016.     }
  1017.  
  1018.     switch (type) {
  1019.     case DDR2:
  1020.         printf ("SDRAM cycle time (2nd highest CAS latency)        ");
  1021.         print_ddr2_tcyc (data[23]);
  1022.         break;
  1023.     default:
  1024.         printf ("SDRAM cycle time (2nd highest CAS latency)        %d."
  1025.             "%d ns\n", (data[23] >> 4) & 0x0F, data[23] & 0x0F);
  1026.         break;
  1027.     }
  1028.  
  1029.     switch (type) {
  1030.     case DDR2:
  1031.         printf ("SDRAM access from clock (2nd highest CAS latency) 0."
  1032.             "%d%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  1033.         break;
  1034.     default:
  1035.         printf ("SDRAM access from clock (2nd highest CAS latency) %d."
  1036.             "%d ns\n", (data[24] >> 4) & 0x0F, data[24] & 0x0F);
  1037.         break;
  1038.     }
  1039.  
  1040.     switch (type) {
  1041.     case DDR2:
  1042.         printf ("SDRAM cycle time (3rd highest CAS latency)        ");
  1043.         print_ddr2_tcyc (data[25]);
  1044.         break;
  1045.     default:
  1046.         printf ("SDRAM cycle time (3rd highest CAS latency)        %d."
  1047.             "%d ns\n", (data[25] >> 4) & 0x0F, data[25] & 0x0F);
  1048.         break;
  1049.     }
  1050.  
  1051.     switch (type) {
  1052.     case DDR2:
  1053.         printf ("SDRAM access from clock (3rd highest CAS latency) 0."
  1054.             "%d%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  1055.         break;
  1056.     default:
  1057.         printf ("SDRAM access from clock (3rd highest CAS latency) %d."
  1058.             "%d ns\n", (data[26] >> 4) & 0x0F, data[26] & 0x0F);
  1059.         break;
  1060.     }
  1061.  
  1062.     switch (type) {
  1063.     case DDR2:
  1064.         printf ("Minimum row precharge        %d.%02d ns\n",
  1065.             (data[27] >> 2) & 0x3F, 25 * (data[27] & 0x03));
  1066.         break;
  1067.     default:
  1068.         printf ("Minimum row precharge        %d ns\n", data[27]);
  1069.         break;
  1070.     }
  1071.  
  1072.     switch (type) {
  1073.     case DDR2:
  1074.         printf ("Row active to row active min %d.%02d ns\n",
  1075.             (data[28] >> 2) & 0x3F, 25 * (data[28] & 0x03));
  1076.         break;
  1077.     default:
  1078.         printf ("Row active to row active min %d ns\n", data[28]);
  1079.         break;
  1080.     }
  1081.  
  1082.     switch (type) {
  1083.     case DDR2:
  1084.         printf ("RAS to CAS delay min         %d.%02d ns\n",
  1085.             (data[29] >> 2) & 0x3F, 25 * (data[29] & 0x03));
  1086.         break;
  1087.     default:
  1088.         printf ("RAS to CAS delay min         %d ns\n", data[29]);
  1089.         break;
  1090.     }
  1091.  
  1092.     printf ("Minimum RAS pulse width      %d ns\n", data[30]);
  1093.  
  1094.     switch (type) {
  1095.     case DDR2:
  1096.         puts ("Density of each row          ");
  1097.         decode_bits (data[31], decode_row_density_DDR2, 1);
  1098.         putc ('\n');
  1099.         break;
  1100.     default:
  1101.         puts ("Density of each row          ");
  1102.         decode_bits (data[31], decode_row_density_default, 1);
  1103.         putc ('\n');
  1104.         break;
  1105.     }
  1106.  
  1107.     switch (type) {
  1108.     case DDR2:
  1109.         puts ("Command and Address setup    ");
  1110.         if (data[32] >= 0xA0) {
  1111.             printf ("1.%d%d ns\n",
  1112.                 ((data[32] >> 4) & 0x0F) - 10, data[32] & 0x0F);
  1113.         } else {
  1114.             printf ("0.%d%d ns\n",
  1115.                 ((data[32] >> 4) & 0x0F), data[32] & 0x0F);
  1116.         }
  1117.         break;
  1118.     default:
  1119.         printf ("Command and Address setup    %c%d.%d ns\n",
  1120.             (data[32] & 0x80) ? '-' : '+',
  1121.             (data[32] >> 4) & 0x07, data[32] & 0x0F);
  1122.         break;
  1123.     }
  1124.  
  1125.     switch (type) {
  1126.     case DDR2:
  1127.         puts ("Command and Address hold     ");
  1128.         if (data[33] >= 0xA0) {
  1129.             printf ("1.%d%d ns\n",
  1130.                 ((data[33] >> 4) & 0x0F) - 10, data[33] & 0x0F);
  1131.         } else {
  1132.             printf ("0.%d%d ns\n",
  1133.                 ((data[33] >> 4) & 0x0F), data[33] & 0x0F);
  1134.         }
  1135.         break;
  1136.     default:
  1137.         printf ("Command and Address hold     %c%d.%d ns\n",
  1138.             (data[33] & 0x80) ? '-' : '+',
  1139.             (data[33] >> 4) & 0x07, data[33] & 0x0F);
  1140.         break;
  1141.     }
  1142.  
  1143.     switch (type) {
  1144.     case DDR2:
  1145.         printf ("Data signal input setup      0.%d%d ns\n",
  1146.             (data[34] >> 4) & 0x0F, data[34] & 0x0F);
  1147.         break;
  1148.     default:
  1149.         printf ("Data signal input setup      %c%d.%d ns\n",
  1150.             (data[34] & 0x80) ? '-' : '+',
  1151.             (data[34] >> 4) & 0x07, data[34] & 0x0F);
  1152.         break;
  1153.     }
  1154.  
  1155.     switch (type) {
  1156.     case DDR2:
  1157.         printf ("Data signal input hold       0.%d%d ns\n",
  1158.             (data[35] >> 4) & 0x0F, data[35] & 0x0F);
  1159.         break;
  1160.     default:
  1161.         printf ("Data signal input hold       %c%d.%d ns\n",
  1162.             (data[35] & 0x80) ? '-' : '+',
  1163.             (data[35] >> 4) & 0x07, data[35] & 0x0F);
  1164.         break;
  1165.     }
  1166.  
  1167.     puts ("Manufacturer's JEDEC ID      ");
  1168.     for (j = 64; j <= 71; j++)
  1169.         printf ("%02X ", data[j]);
  1170.     putc ('\n');
  1171.     printf ("Manufacturing Location       %02X\n", data[72]);
  1172.     puts ("Manufacturer's Part Number   ");
  1173.     for (j = 73; j <= 90; j++)
  1174.         printf ("%02X ", data[j]);
  1175.     putc ('\n');
  1176.     printf ("Revision Code                %02X %02X\n", data[91], data[92]);
  1177.     printf ("Manufacturing Date           %02X %02X\n", data[93], data[94]);
  1178.     puts ("Assembly Serial Number       ");
  1179.     for (j = 95; j <= 98; j++)
  1180.         printf ("%02X ", data[j]);
  1181.     putc ('\n');
  1182.  
  1183.     if (DDR2 != type) {
  1184.         printf ("Speed rating                 PC%d\n",
  1185.             data[126] == 0x66 ? 66 : data[126]);
  1186.     }
  1187.     return 0;
  1188. }
  1189. #endif
  1190.  
  1191. #if defined(CONFIG_I2C_MUX)
  1192. int do_i2c_add_bus(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  1193. {
  1194.     int ret=0;
  1195.  
  1196.     if (argc == 1) {
  1197.         /* show all busses */
  1198.         I2C_MUX     *mux;
  1199.         I2C_MUX_DEVICE  *device = i2c_mux_devices;
  1200.  
  1201.         printf ("Busses reached over muxes:\n");
  1202.         while (device != NULL) {
  1203.             printf ("Bus ID: %x\n", device->busid);
  1204.             printf ("  reached over Mux(es):\n");
  1205.             mux = device->mux;
  1206.             while (mux != NULL) {
  1207.                 printf ("    %s@%x ch: %x\n", mux->name, mux->chip, mux->channel);
  1208.                 mux = mux->next;
  1209.             }
  1210.             device = device->next;
  1211.         }
  1212.     } else {
  1213.         I2C_MUX_DEVICE *dev;
  1214.  
  1215.         dev = i2c_mux_ident_muxstring ((uchar *)argv[1]);
  1216.         ret = 0;
  1217.     }
  1218.     return ret;
  1219. }
  1220. #endif  /* CONFIG_I2C_MUX */
  1221.  
  1222. #if defined(CONFIG_I2C_MULTI_BUS)
  1223. int do_i2c_bus_num(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  1224. {
  1225.     int bus_idx, ret=0;
  1226.  
  1227.     if (argc == 1)
  1228.         /* querying current setting */
  1229.         printf("Current bus is %d\n", i2c_get_bus_num());
  1230.     else {
  1231.         bus_idx = simple_strtoul(argv[1], NULL, 10);
  1232.         printf("Setting bus to %d\n", bus_idx);
  1233.         ret = i2c_set_bus_num(bus_idx);
  1234.         if (ret)
  1235.             printf("Failure changing bus number (%d)\n", ret);
  1236.     }
  1237.     return ret;
  1238. }
  1239. #endif  /* CONFIG_I2C_MULTI_BUS */
  1240.  
  1241. int do_i2c_bus_speed(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  1242. {
  1243.     int speed, ret=0;
  1244.  
  1245.     if (argc == 1)
  1246.         /* querying current speed */
  1247.         printf("Current bus speed=%d\n", i2c_get_bus_speed());
  1248.     else {
  1249.         speed = simple_strtoul(argv[1], NULL, 10);
  1250.         printf("Setting bus speed to %d Hz\n", speed);
  1251.         ret = i2c_set_bus_speed(speed);
  1252.         if (ret)
  1253.             printf("Failure changing bus speed (%d)\n", ret);
  1254.     }
  1255.     return ret;
  1256. }
  1257.  
  1258. int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  1259. {
  1260.     /* Strip off leading 'i2c' command argument */
  1261.     argc--;
  1262.     argv++;
  1263.  
  1264. #if defined(CONFIG_I2C_MUX)
  1265.     if (!strncmp(argv[0], "bu", 2))
  1266.         return do_i2c_add_bus(cmdtp, flag, argc, argv);
  1267. #endif  /* CONFIG_I2C_MUX */
  1268.     if (!strncmp(argv[0], "sp", 2))
  1269.         return do_i2c_bus_speed(cmdtp, flag, argc, argv);
  1270. #if defined(CONFIG_I2C_MULTI_BUS)
  1271.     if (!strncmp(argv[0], "de", 2))
  1272.         return do_i2c_bus_num(cmdtp, flag, argc, argv);
  1273. #endif  /* CONFIG_I2C_MULTI_BUS */
  1274.     if (!strncmp(argv[0], "md", 2))
  1275.         return do_i2c_md(cmdtp, flag, argc, argv);
  1276.     if (!strncmp(argv[0], "mm", 2))
  1277.         return mod_i2c_mem (cmdtp, 1, flag, argc, argv);
  1278.     if (!strncmp(argv[0], "mw", 2))
  1279.         return do_i2c_mw(cmdtp, flag, argc, argv);
  1280.     if (!strncmp(argv[0], "nm", 2))
  1281.         return mod_i2c_mem (cmdtp, 0, flag, argc, argv);
  1282.     if (!strncmp(argv[0], "cr", 2))
  1283.         return do_i2c_crc(cmdtp, flag, argc, argv);
  1284.     if (!strncmp(argv[0], "pr", 2))
  1285.         return do_i2c_probe(cmdtp, flag, argc, argv);
  1286.     if (!strncmp(argv[0], "re", 2)) {
  1287.         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  1288.         return 0;
  1289.     }
  1290.     if (!strncmp(argv[0], "lo", 2))
  1291.         return do_i2c_loop(cmdtp, flag, argc, argv);
  1292. #if defined(CONFIG_CMD_SDRAM)
  1293.     if (!strncmp(argv[0], "sd", 2))
  1294.         return do_sdram(cmdtp, flag, argc, argv);
  1295. #endif
  1296.     cmd_usage(cmdtp);
  1297.     return 0;
  1298. }
  1299.  
  1300. /***************************************************/
  1301.  
  1302. U_BOOT_CMD(
  1303.     i2c, 6, 1, do_i2c,
  1304.     "I2C sub-system",
  1305.     "speed [speed] - show or set I2C bus speed\n"
  1306. #if defined(CONFIG_I2C_MUX)
  1307.     "i2c bus [muxtype:muxaddr:muxchannel] - add a new bus reached over muxes\n"
  1308. #endif  /* CONFIG_I2C_MUX */
  1309. #if defined(CONFIG_I2C_MULTI_BUS)
  1310.     "i2c dev [dev] - show or set current I2C bus\n"
  1311. #endif  /* CONFIG_I2C_MULTI_BUS */
  1312.     "i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device\n"
  1313.     "i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing)\n"
  1314.     "i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill)\n"
  1315.     "i2c nm chip address[.0, .1, .2] - write to I2C device (constant address)\n"
  1316.     "i2c crc32 chip address[.0, .1, .2] count - compute CRC32 checksum\n"
  1317.     "i2c probe - show devices on the I2C bus\n"
  1318.     "i2c reset - re-init the I2C Controller\n"
  1319.     "i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device"
  1320. #if defined(CONFIG_CMD_SDRAM)
  1321.     "\n"
  1322.     "i2c sdram chip - print SDRAM configuration information"
  1323. #endif
  1324. );
  1325.  
  1326. #if defined(CONFIG_I2C_MUX)
  1327.  
  1328. int i2c_mux_add_device(I2C_MUX_DEVICE *dev)
  1329. {
  1330.     I2C_MUX_DEVICE  *devtmp = i2c_mux_devices;
  1331.  
  1332.     if (i2c_mux_devices == NULL) {
  1333.         i2c_mux_devices = dev;
  1334.         return 0;
  1335.     }
  1336.     while (devtmp->next != NULL)
  1337.         devtmp = devtmp->next;
  1338.  
  1339.     devtmp->next = dev;
  1340.     return 0;
  1341. }
  1342.  
  1343. I2C_MUX_DEVICE  *i2c_mux_search_device(int id)
  1344. {
  1345.     I2C_MUX_DEVICE  *device = i2c_mux_devices;
  1346.  
  1347.     while (device != NULL) {
  1348.         if (device->busid == id)
  1349.             return device;
  1350.         device = device->next;
  1351.     }
  1352.     return NULL;
  1353. }
  1354.  
  1355. /* searches in the buf from *pos the next ':'.
  1356.  * returns:
  1357.  *     0 if found (with *pos = where)
  1358.  *   < 0 if an error occured
  1359.  *   > 0 if the end of buf is reached
  1360.  */
  1361. static int i2c_mux_search_next (int *pos, uchar *buf, int len)
  1362. {
  1363.     while ((buf[*pos] != ':') && (*pos < len)) {
  1364.         *pos += 1;
  1365.     }
  1366.     if (*pos >= len)
  1367.         return 1;
  1368.     if (buf[*pos] != ':')
  1369.         return -1;
  1370.     return 0;
  1371. }
  1372.  
  1373. static int i2c_mux_get_busid (void)
  1374. {
  1375.     int tmp = i2c_mux_busid;
  1376.  
  1377.     i2c_mux_busid ++;
  1378.     return tmp;
  1379. }
  1380.  
  1381. /* Analyses a Muxstring and sends immediately the
  1382.    Commands to the Muxes. Runs from Flash.
  1383.  */
  1384. int i2c_mux_ident_muxstring_f (uchar *buf)
  1385. {
  1386.     int pos = 0;
  1387.     int oldpos;
  1388.     int ret = 0;
  1389.     int len = strlen((char *)buf);
  1390.     int chip;
  1391.     uchar   channel;
  1392.     int was = 0;
  1393.  
  1394.     while (ret == 0) {
  1395.         oldpos = pos;
  1396.         /* search name */
  1397.         ret = i2c_mux_search_next(&pos, buf, len);
  1398.         if (ret != 0)
  1399.             printf ("ERROR\n");
  1400.         /* search address */
  1401.         pos ++;
  1402.         oldpos = pos;
  1403.         ret = i2c_mux_search_next(&pos, buf, len);
  1404.         if (ret != 0)
  1405.             printf ("ERROR\n");
  1406.         buf[pos] = 0;
  1407.         chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
  1408.         buf[pos] = ':';
  1409.         /* search channel */
  1410.         pos ++;
  1411.         oldpos = pos;
  1412.         ret = i2c_mux_search_next(&pos, buf, len);
  1413.         if (ret < 0)
  1414.             printf ("ERROR\n");
  1415.         was = 0;
  1416.         if (buf[pos] != 0) {
  1417.             buf[pos] = 0;
  1418.             was = 1;
  1419.         }
  1420.         channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
  1421.         if (was)
  1422.             buf[pos] = ':';
  1423.         if (i2c_write(chip, 0, 0, &channel, 1) != 0) {
  1424.             printf ("Error setting Mux: chip:%x channel: \
  1425.                 %x\n", chip, channel);
  1426.             return -1;
  1427.         }
  1428.         pos ++;
  1429.         oldpos = pos;
  1430.  
  1431.     }
  1432.  
  1433.     return 0;
  1434. }
  1435.  
  1436. /* Analyses a Muxstring and if this String is correct
  1437.  * adds a new I2C Bus.
  1438.  */
  1439. I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf)
  1440. {
  1441.     I2C_MUX_DEVICE  *device;
  1442.     I2C_MUX     *mux;
  1443.     int pos = 0;
  1444.     int oldpos;
  1445.     int ret = 0;
  1446.     int len = strlen((char *)buf);
  1447.     int was = 0;
  1448.  
  1449.     device = (I2C_MUX_DEVICE *)malloc (sizeof(I2C_MUX_DEVICE));
  1450.     device->mux = NULL;
  1451.     device->busid = i2c_mux_get_busid ();
  1452.     device->next = NULL;
  1453.     while (ret == 0) {
  1454.         mux = (I2C_MUX *)malloc (sizeof(I2C_MUX));
  1455.         mux->next = NULL;
  1456.         /* search name of mux */
  1457.         oldpos = pos;
  1458.         ret = i2c_mux_search_next(&pos, buf, len);
  1459.         if (ret != 0)
  1460.             printf ("%s no name.\n", __FUNCTION__);
  1461.         mux->name = (char *)malloc (pos - oldpos + 1);
  1462.         memcpy (mux->name, &buf[oldpos], pos - oldpos);
  1463.         mux->name[pos - oldpos] = 0;
  1464.         /* search address */
  1465.         pos ++;
  1466.         oldpos = pos;
  1467.         ret = i2c_mux_search_next(&pos, buf, len);
  1468.         if (ret != 0)
  1469.             printf ("%s no mux address.\n", __FUNCTION__);
  1470.         buf[pos] = 0;
  1471.         mux->chip = simple_strtoul((char *)&buf[oldpos], NULL, 16);
  1472.         buf[pos] = ':';
  1473.         /* search channel */
  1474.         pos ++;
  1475.         oldpos = pos;
  1476.         ret = i2c_mux_search_next(&pos, buf, len);
  1477.         if (ret < 0)
  1478.             printf ("%s no mux channel.\n", __FUNCTION__);
  1479.         was = 0;
  1480.         if (buf[pos] != 0) {
  1481.             buf[pos] = 0;
  1482.             was = 1;
  1483.         }
  1484.         mux->channel = simple_strtoul((char *)&buf[oldpos], NULL, 16);
  1485.         if (was)
  1486.             buf[pos] = ':';
  1487.         if (device->mux == NULL)
  1488.             device->mux = mux;
  1489.         else {
  1490.             I2C_MUX     *muxtmp = device->mux;
  1491.             while (muxtmp->next != NULL) {
  1492.                 muxtmp = muxtmp->next;
  1493.             }
  1494.             muxtmp->next = mux;
  1495.         }
  1496.         pos ++;
  1497.         oldpos = pos;
  1498.     }
  1499.     if (ret > 0) {
  1500.         /* Add Device */
  1501.         i2c_mux_add_device (device);
  1502.         return device;
  1503.     }
  1504.  
  1505.     return NULL;
  1506. }
  1507.  
  1508. int i2x_mux_select_mux(int bus)
  1509. {
  1510.     I2C_MUX_DEVICE  *dev;
  1511.     I2C_MUX     *mux;
  1512.  
  1513.     if ((gd->flags & GD_FLG_RELOC) != GD_FLG_RELOC) {
  1514.         /* select Default Mux Bus */
  1515. #if defined(CONFIG_SYS_I2C_IVM_BUS)
  1516.         i2c_mux_ident_muxstring_f ((uchar *)CONFIG_SYS_I2C_IVM_BUS);
  1517. #else
  1518.         {
  1519.         unsigned char *buf;
  1520.         buf = (unsigned char *) getenv("EEprom_ivm");
  1521.         if (buf != NULL)
  1522.             i2c_mux_ident_muxstring_f (buf);
  1523.         }
  1524. #endif
  1525.         return 0;
  1526.     }
  1527.     dev = i2c_mux_search_device(bus);
  1528.     if (dev == NULL)
  1529.         return -1;
  1530.  
  1531.     mux = dev->mux;
  1532.     while (mux != NULL) {
  1533.         if (i2c_write(mux->chip, 0, 0, &mux->channel, 1) != 0) {
  1534.             printf ("Error setting Mux: chip:%x channel: \
  1535.                 %x\n", mux->chip, mux->channel);
  1536.             return -1;
  1537.         }
  1538.         mux = mux->next;
  1539.     }
  1540.     return 0;
  1541. }
  1542. #endif /* CONFIG_I2C_MUX */
  1543.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement