Commit f92994f0f7403b84366ce04e554e461f624e6868

Authored by Vipin KUMAR
Committed by Tom Rix
1 parent 566c9c16fe

SPEAr : Support for HW mac id read/write from i2c mem

This patch adds the  support to read and write mac id from i2c
memory.
For reading:
	if (env contains ethaddr)
		pick env ethaddr
	else
		pick ethaddr from i2c memory
For writing:
	chip_config ethaddr XX:XX:XX:XX:XX:XX writes the mac id
	in i2c memory

Signed-off-by: Vipin <vipin.kumar@st.com>

Showing 2 changed files with 75 additions and 1 deletions Side-by-side Diff

board/spear/common/spr_misc.c
... ... @@ -67,6 +67,12 @@
67 67  
68 68 int misc_init_r(void)
69 69 {
  70 +#if defined(CONFIG_CMD_NET)
  71 + uchar mac_id[6];
  72 +
  73 + if (!eth_getenv_enetaddr("ethaddr", mac_id) && !i2c_read_mac(mac_id))
  74 + eth_setenv_enetaddr("ethaddr", mac_id);
  75 +#endif
70 76 setenv("verify", "n");
71 77  
72 78 #if defined(CONFIG_SPEAR_USBTTY)
73 79  
... ... @@ -101,12 +107,54 @@
101 107 return 0;
102 108 }
103 109  
  110 +static int i2c_read_mac(uchar *buffer)
  111 +{
  112 + u8 buf[2];
  113 +
  114 + i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  115 +
  116 + /* Check if mac in i2c memory is valid */
  117 + if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
  118 + /* Valid mac address is saved in i2c eeprom */
  119 + i2c_read(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, buffer, MAC_LEN);
  120 + return 0;
  121 + }
  122 +
  123 + return -1;
  124 +}
  125 +
  126 +static int write_mac(uchar *mac)
  127 +{
  128 + u8 buf[2];
  129 +
  130 + buf[0] = (u8)MAGIC_BYTE0;
  131 + buf[1] = (u8)MAGIC_BYTE1;
  132 + i2c_write(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  133 +
  134 + buf[0] = (u8)~MAGIC_BYTE0;
  135 + buf[1] = (u8)~MAGIC_BYTE1;
  136 +
  137 + i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
  138 +
  139 + /* check if valid MAC address is saved in I2C EEPROM or not? */
  140 + if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
  141 + i2c_write(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, mac, MAC_LEN);
  142 + puts("I2C EEPROM written with mac address \n");
  143 + return 0;
  144 + }
  145 +
  146 + puts("I2C EEPROM writing failed \n");
  147 + return -1;
  148 +}
  149 +
104 150 int do_chip_config(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
105 151 {
106 152 void (*sram_setfreq) (unsigned int, unsigned int);
107 153 struct chip_data *chip = &chip_data;
108 154 unsigned char mac[6];
109   - unsigned int frequency;
  155 + unsigned int reg, frequency;
  156 + char *s, *e;
  157 + char i2c_mac[20];
110 158  
111 159 if ((argc > 3) || (argc < 2)) {
112 160 cmd_usage(cmdtp);
... ... @@ -137,6 +185,17 @@
137 185 }
138 186  
139 187 return 0;
  188 + } else if (!strcmp(argv[1], "ethaddr")) {
  189 +
  190 + s = argv[2];
  191 + for (reg = 0; reg < 6; ++reg) {
  192 + mac[reg] = s ? simple_strtoul(s, &e, 16) : 0;
  193 + if (s)
  194 + s = (*e) ? e + 1 : e;
  195 + }
  196 + write_mac(mac);
  197 +
  198 + return 0;
140 199 } else if (!strcmp(argv[1], "print")) {
141 200  
142 201 if (chip->cpufreq == -1)
... ... @@ -155,6 +214,13 @@
155 214 printf("DDR Type = DDR2\n");
156 215 else
157 216 printf("DDR Type = Not Known\n");
  217 +
  218 + if (!i2c_read_mac(mac)) {
  219 + sprintf(i2c_mac, "%pM", mac);
  220 + printf("Ethaddr (from i2c mem) = %s\n", i2c_mac);
  221 + } else {
  222 + printf("Ethaddr (from i2c mem) = Not set\n");
  223 + }
158 224  
159 225 printf("Xloader Rev = %s\n", chip->version);
160 226  
include/asm-arm/arch-spear/spr_defs.h
... ... @@ -35,5 +35,13 @@
35 35 uchar version[32];
36 36 };
37 37  
  38 +/* HW mac id in i2c memory definitions */
  39 +#define MAGIC_OFF 0x0
  40 +#define MAGIC_LEN 0x2
  41 +#define MAGIC_BYTE0 0x55
  42 +#define MAGIC_BYTE1 0xAA
  43 +#define MAC_OFF 0x2
  44 +#define MAC_LEN 0x6
  45 +
38 46 #endif