ethernet: support mac from idb
authorhwg <hwg@rock-chips.com>
Thu, 21 Mar 2013 08:31:51 +0000 (16:31 +0800)
committerhwg <hwg@rock-chips.com>
Thu, 21 Mar 2013 08:32:04 +0000 (16:32 +0800)
arch/arm/mach-rk30/board-rk30-sdk-vmac.c
drivers/net/Kconfig
drivers/net/Makefile [changed mode: 0644->0755]
drivers/net/eth_mac/Kconfig [new file with mode: 0755]
drivers/net/eth_mac/Makefile [new file with mode: 0755]
drivers/net/eth_mac/eth_mac.c [new file with mode: 0755]
drivers/net/eth_mac/eth_mac.h [new file with mode: 0755]
drivers/net/rk29_vmac.c

index 35d45119d3cc2cdcec1a387cb819db8a4761c62a..0a43c75aca87073f2e87f99461f1e388b2e44b96 100755 (executable)
@@ -75,7 +75,7 @@ static int rk30_rmii_power_control(int enable)
 #define BIT_EMAC_SPEED      (1 << 1)
 static int rk29_vmac_speed_switch(int speed)
 {
-       printk("%s--speed=%d\n", __FUNCTION__, speed);
+       //printk("%s--speed=%d\n", __FUNCTION__, speed);
        if (10 == speed) {
            writel_relaxed(readl_relaxed(RK30_GRF_BASE + GRF_SOC_CON1) & (~BIT_EMAC_SPEED), RK30_GRF_BASE + GRF_SOC_CON1);
        } else {
index 0a47cad725809569732c3633b5bf390d3e66dbb1..f17b3b0c1c28a83dac25bc31b5ab901555aa9b56 100755 (executable)
@@ -260,6 +260,7 @@ config RK29_VMAC
        help
          MAC device present on rockchip rk29xx 
 
+source "drivers/net/eth_mac/Kconfig"
 
 config MACE
        tristate "MACE (Power Mac ethernet) support"
old mode 100644 (file)
new mode 100755 (executable)
index 59693f5..61fd9d2
@@ -310,3 +310,5 @@ obj-$(CONFIG_CAIF) += caif/
 obj-$(CONFIG_OCTEON_MGMT_ETHERNET) += octeon/
 obj-$(CONFIG_PCH_GBE) += pch_gbe/
 obj-$(CONFIG_TILE_NET) += tile/
+
+obj-$(CONFIG_RK29_VMAC) += eth_mac/
diff --git a/drivers/net/eth_mac/Kconfig b/drivers/net/eth_mac/Kconfig
new file mode 100755 (executable)
index 0000000..0554164
--- /dev/null
@@ -0,0 +1,58 @@
+#
+#      set Ethernet mac source
+#
+choice 
+       prompt "Ethernet mac source"
+       depends on NETDEV_10000 ||NETDEV_1000 ||NET_ETHERNET
+       default ETH_MAC_FROM_RANDOM
+
+       config ETH_MAC_FROM_RANDOM
+               bool "Random Ethernet mac "
+               ---help---
+                  Say Y here if you want to set Random mac to Ethernet mac.
+        
+                  This option alone does not add any kernel code.
+
+                  If you say N, all options in this submenu will be skipped and disabled.      
+
+       config ETH_MAC_FROM_EEPROM
+               bool "Ethernet mac from EEPROM"
+               depends on EEPROM_AT24C16
+               ---help---
+                  Say Y here if you want to set EEPROM mac to Ethernet mac,
+                  when the EEPROM exsits and mac has been written in.
+        
+                  This option alone does not add any kernel code.
+
+                  If you say N, all options in this submenu will be skipped and disabled.      
+
+       config ETH_MAC_FROM_IDB
+               bool "Ethernet mac from IDB"
+               ---help---
+                 Say Y here if you want to set IDB mac to Ethernet mac,
+                 when the IDB mac has been written in.
+               
+                 This option alone does not add any kernel code.
+
+                 If you say N, all options in this submenu will be skipped and disabled.
+
+       config ETH_MAC_FROM_WIFI_MAC
+               bool "Ethernet mac from Wifi mac"
+               ---help---
+                 Say Y here if you want to set Wifi mac to Ethernet mac,
+                 when the Wifi device exsits.
+                 This option alone does not add any kernel code.
+
+                 If you say N, all options in this submenu will be skipped and disabled.
+
+       config ETH_MAC_FROM_SECURE_CHIP
+               bool "Ethernet mac from Secure chip"
+               ---help---
+                 Say Y here if you want to set Secure chip mac to Ethernet mac,
+                 when the Secure chip exsits and mac has been written in.
+                 This option alone does not add any kernel code.
+
+                 If you say N, all options in this submenu will be skipped and disabled.
+
+endchoice
+
diff --git a/drivers/net/eth_mac/Makefile b/drivers/net/eth_mac/Makefile
new file mode 100755 (executable)
index 0000000..c745b3d
--- /dev/null
@@ -0,0 +1 @@
+obj-y  += eth_mac.o
diff --git a/drivers/net/eth_mac/eth_mac.c b/drivers/net/eth_mac/eth_mac.c
new file mode 100755 (executable)
index 0000000..7f2c64f
--- /dev/null
@@ -0,0 +1,208 @@
+/*\r
+ * Copyright (C) 2010 ROCKCHIP, Inc.\r
+ * Author: roger_chen <cz@rock-chips.com>\r
+ *\r
+ * This program is the virtual flash device \r
+ * used to store bd_addr or MAC\r
+ *\r
+ */\r
+\r
+\r
+#include <linux/module.h>\r
+#include <linux/kernel.h>\r
+#include <linux/errno.h>\r
+#include <linux/miscdevice.h>\r
+#include <linux/fs.h>\r
+#include <linux/platform_device.h>\r
+#include <asm/uaccess.h>\r
+#include <asm/io.h>\r
+#include <linux/fs.h>\r
+#include <linux/slab.h>\r
+#include "eth_mac.h"\r
+\r
+#if 1\r
+#define DBG(x...)   printk("eth_mac:" x)\r
+#else\r
+#define DBG(x...)\r
+#endif\r
+\r
+#define VERSION "0.1"\r
+\r
+#define WLAN_MAC_FILE "/data/misc/wifi/wlan_mac"\r
+\r
+//extern char GetSNSectorInfo(char * pbuf);\r
+\r
+char GetSNSectorInfoBeforeNandInit(char * pbuf)\r
+{\r
+    char * sn_addr = ioremap(0x10501600,0x200);\r
+    memcpy(pbuf,sn_addr,0x200);\r
+    iounmap(sn_addr);\r
+       //print_hex_dump(KERN_WARNING, "sn:", DUMP_PREFIX_NONE, 16,1, sn_addr, 16, 0);\r
+    return 0;\r
+}\r
+\r
+int eth_mac_read_from_IDB(u8 *mac)\r
+{\r
+     int i;\r
+     char *tempBuf = kmalloc(512, GFP_KERNEL);\r
+    \r
+    if(mac == NULL)\r
+        return -EFAULT;\r
+\r
+    GetSNSectorInfoBeforeNandInit(tempBuf);\r
+    /*for (i = 0; i < 512; i++) {\r
+        printk("%02x, ", tempBuf[i]);\r
+               if(((i+1)%16) == 0) printk("\n");\r
+       }*/\r
+\r
+    for (i = 506; i <= 511; i++)\r
+               mac[i-506] = tempBuf[i];\r
+\r
+    kfree(tempBuf);\r
+    \r
+       return 0;\r
+}\r
+\r
+int eth_mac_idb(u8 *eth_mac)\r
+{\r
+       int i;\r
+       int err = 0;\r
+       memset(eth_mac, 0, 6);\r
+       err = eth_mac_read_from_IDB(eth_mac);\r
+       if (err)\r
+               return -1;\r
+       printk("Read the Ethernet MAC address from IDB:");\r
+       for (i = 0; i < 5; i++)\r
+               printk("%2.2x:", eth_mac[i]);\r
+       printk("%2.2x\n", eth_mac[i]);\r
+\r
+       return 0;\r
+}\r
+\r
+/**\r
+*大写转小写\r
+*\r
+*/\r
+void   to_lower(char   *str) \r
+{ \r
+       int   i=0; \r
+       while(str[i]!=0) \r
+       { \r
+               if((str[i] >= 'A')&&(str[i] <= 'Z')) \r
+                       str[i]+=32; \r
+               i++; \r
+       } \r
+} \r
+\r
+\r
+/**\r
+  *字符串格式转为mac 格式.\r
+  *\r
+  *\r
+  */\r
+void  trans( char *src ,int * k) \r
+{\r
+    char c;\r
+    int i;\r
+    int temp;\r
+    int temp2;\r
+\r
+    if( (src == NULL) ||(strlen(src) <16 ) ) // 参数检查\r
+    {\r
+      printk( "Arg Error\n" );\r
+      return ;\r
+    }\r
+\r
+    for( i = 0; i < 6; i++ )\r
+    {\r
+      temp = 0;\r
+      temp2 = 0;\r
+      c = *src;\r
+      if( c == ':' ){\r
+               src++;\r
+               c = *src;\r
+       }\r
+      if( c >= 'a' && c <= 'f' )          // 两个字符中的第一个 比如 "0f" ,则表示是字符 '0'\r
+         temp = ( c - 'a' ) + 10; \r
+      else\r
+         temp = ( c - '0' ) ;\r
+      src++;\r
+      \r
+      c = *src;\r
+      if( i == 5){                         //wifi mac 末尾加1\r
+       if(c =='f'){\r
+          c = '0';\r
+       } if (c == '9'){\r
+          c = 'a';\r
+       }else{\r
+          c = c + 1;\r
+       }\r
+      }\r
+      if( c >= 'a' && c <= 'f' )             // 两个字符中的第二个,如 "f8" ,那么表示字符 '8'\r
+         temp2 = ( c - 'a' ) + 10;\r
+      else\r
+         temp2 = ( c - '0' ) ;\r
+\r
+      temp = temp * 16;\r
+      temp += temp2;\r
+      src++;\r
+      *(k+i) = temp;\r
+\r
+    }\r
+\r
+}\r
+\r
+int eth_mac_wifi(u8 *eth_mac){\r
+       int i;\r
+       struct  file *file = NULL;\r
+       char wifi_mac[32];\r
+       mm_segment_t old_fs;\r
+       ssize_t ret;\r
+\r
+       int *maci=(int *)kmalloc(6, GFP_KERNEL);\r
+\r
+       memset(eth_mac, 0, 6);\r
+       \r
+       file = filp_open(WLAN_MAC_FILE, O_RDWR,0);\r
+       if (IS_ERR(file))\r
+       {\r
+               printk("open %s failed.", WLAN_MAC_FILE);\r
+               return -ENOENT;\r
+       } \r
+\r
+       old_fs = get_fs();\r
+       set_fs(get_ds());\r
+    \r
+       file->f_op->llseek(file,0,0);\r
+       ret = file->f_op->read(file, wifi_mac, 32, &file->f_pos);\r
+    \r
+       set_fs(old_fs);\r
+    \r
+        if(ret > 0){\r
+               //printk("mac read from %s: %s\n", WLAN_MAC_FILE,wifi_mac);\r
+\r
+               to_lower(wifi_mac);\r
+               trans(wifi_mac,maci);\r
+               for (i = 0;i< 6;i++){\r
+                       eth_mac[i] = maci[i];\r
+               }\r
+               \r
+        }\r
+        else if(ret == 0)\r
+               printk("read nothing from %s........\n",WLAN_MAC_FILE);\r
+        else \r
+        {\r
+               printk("read wifi mac error\n");\r
+                return -ENOENT;\r
+        }\r
+\r
+       kfree(maci);\r
+       filp_close(file,NULL);\r
+       return 0;\r
+       \r
+}\r
+\r
+\r
+\r
+\r
+\r
diff --git a/drivers/net/eth_mac/eth_mac.h b/drivers/net/eth_mac/eth_mac.h
new file mode 100755 (executable)
index 0000000..03db627
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef _ETH_MAC_H_
+#define _ETH_MAC_H_
+/*
+ *  eth_mac/eth_mac.h
+ *
+ *  Copyright (C) 2001 Russell King.
+ *
+ * This file is placed under the LGPL.
+ *
+ *
+ * 
+ */
+//int eth_mac_read_from_IDB(u8 *mac)
+
+int eth_mac_idb(u8 *eth_mac);
+int eth_mac_wifi(u8 *eth_mac);
+#endif /* _ETH_MAC_H_ */
index f64bb4e8a5f22ede7588a506d79667eb160fc792..42683cfb3673de8aa046bccc086d5b3c63362e5a 100755 (executable)
@@ -53,6 +53,7 @@
 #include <mach/board.h>\r
 \r
 #include "rk29_vmac.h"\r
+#include "eth_mac/eth_mac.h"\r
 \r
 static struct wake_lock idlelock; /* add by lyx @ 20110302 */\r
 \r
@@ -1548,8 +1549,56 @@ static int __devinit vmac_probe(struct platform_device *pdev)
        /* mac address intialize, set vmac_open  */\r
        read_mac_reg(dev, dev->dev_addr);\r
 \r
-       if (!is_valid_ether_addr(dev->dev_addr))\r
-               random_ether_addr(dev->dev_addr);\r
+       if (!is_valid_ether_addr(dev->dev_addr)) {\r
+       //add by cx@rock-chips.com\r
+       \r
+       #ifdef CONFIG_ETH_MAC_FROM_EEPROM\r
+               ret = eeprom_read_data(0,dev->dev_addr,6);\r
+               if (ret != 6){\r
+                       printk("read mac from Eeprom fail.\n");\r
+               }else {\r
+                       if (is_valid_ether_addr(dev->dev_addr)){\r
+                               printk("eth_mac_from_eeprom***********:%X:%X:%X:%X:%X:%X\n",dev->dev_addr[0],\r
+                                                       dev->dev_addr[1],dev->dev_addr[2],dev->dev_addr[3],\r
+                                                       dev->dev_addr[4],dev->dev_addr[5] );\r
+                       }\r
+               }\r
+       #endif\r
+       \r
+       #ifdef CONFIG_ETH_MAC_FROM_IDB\r
+               err = eth_mac_idb(dev->dev_addr);\r
+               if (err) {\r
+                       printk("read mac from IDB fail.\n");\r
+               } else {\r
+                       if (is_valid_ether_addr(dev->dev_addr)) {\r
+                               printk("eth_mac_from_idb***********:%X:%X:%X:%X:%X:%X\n",dev->dev_addr[0],\r
+                                                       dev->dev_addr[1],dev->dev_addr[2],dev->dev_addr[3],\r
+                                                       dev->dev_addr[4],dev->dev_addr[5] );\r
+                       }\r
+               }\r
+       #endif\r
+       \r
+       #ifdef CONFIG_ETH_MAC_FROM_WIFI_MAC\r
+               err = eth_mac_wifi(dev->dev_addr);\r
+               if (err) {\r
+                       printk("read mac from Wifi  fail.\n");\r
+               } else {\r
+                       if (is_valid_ether_addr(dev->dev_addr)) {\r
+                               printk("eth_mac_from_wifi_mac***********:%X:%X:%X:%X:%X:%X\n",dev->dev_addr[0],\r
+                                                       dev->dev_addr[1],dev->dev_addr[2],dev->dev_addr[3],\r
+                                                       dev->dev_addr[4],dev->dev_addr[5] );\r
+                       }\r
+               }\r
+       #endif\r
+       \r
+       #ifdef CONFIG_ETH_MAC_FROM_RANDOM\r
+           random_ether_addr(dev->dev_addr);\r
+        printk("random_ether_addr***********:%X:%X:%X:%X:%X:%X\n",dev->dev_addr[0],\r
+                                 dev->dev_addr[1],dev->dev_addr[2],dev->dev_addr[3],\r
+                                 dev->dev_addr[4],dev->dev_addr[5] );  \r
+       #endif\r
+       //add end       \r
+       }\r
 \r
        err = register_netdev(dev);\r
        if (err) {\r