ethernet:gmac: read MAC from NAND IDB first
authorroger <cz@rock-chips.com>
Thu, 4 Sep 2014 05:57:10 +0000 (13:57 +0800)
committerroger <cz@rock-chips.com>
Thu, 4 Sep 2014 05:57:10 +0000 (13:57 +0800)
drivers/net/ethernet/rockchip/Makefile
drivers/net/ethernet/rockchip/eth_mac.c [new file with mode: 0755]
drivers/net/ethernet/rockchip/eth_mac.h [new file with mode: 0755]
drivers/net/ethernet/rockchip/gmac/stmmac_main.c

index 8d8d89eff1addcaa3b8cd37d9854d78168249698..55fe6e177e642a8cb0535ade406fccdcb7e04f08 100755 (executable)
@@ -1,6 +1,6 @@
 #
 # Makefile for the rockchip device drivers.
 #
-
+obj-$(CONFIG_NET_VENDOR_ROCKCHIP) += eth_mac.o
 obj-$(CONFIG_RK_VMAC_ETH) += vmac/
 obj-$(CONFIG_RK_GMAC_ETH) += gmac/
diff --git a/drivers/net/ethernet/rockchip/eth_mac.c b/drivers/net/ethernet/rockchip/eth_mac.c
new file mode 100755 (executable)
index 0000000..296c828
--- /dev/null
@@ -0,0 +1,196 @@
+/*\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
+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
+       GetSNSectorInfo(tempBuf);\r
+#if 0\r
+       for (i = 0; i < 512; i++) {\r
+               printk("%02x, ", tempBuf[i]);\r
+               if(((i+1)%16) == 0) printk("\n");\r
+       }\r
+#endif\r
+       for (i = 506; i <= 511; i++) {\r
+               mac[i-506] = tempBuf[i];\r
+       }\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
+#if 0\r
+/**\r
+*大写转小写\r
+*\r
+*/\r
+static 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
+  *字符串格式转为mac 格式.\r
+  *\r
+  *\r
+  */\r
+static 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
+       int maci[6];\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
+       filp_close(file,NULL);\r
+       return 0;\r
+       \r
+}\r
+\r
+#endif\r
diff --git a/drivers/net/ethernet/rockchip/eth_mac.h b/drivers/net/ethernet/rockchip/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 d2f18b0cba8252e785ae89010bb661959a4abd38..6ce9fee67db29b255d98438c3467a69b6b5fd8bd 100755 (executable)
@@ -50,6 +50,7 @@
 #include <linux/net_tstamp.h>
 #include "stmmac_ptp.h"
 #include "stmmac.h"
+#include "../eth_mac.h"
 
 #undef STMMAC_DEBUG
 /*#define STMMAC_DEBUG*/
@@ -1573,6 +1574,8 @@ static void stmmac_check_ether_addr(struct stmmac_priv *priv)
                priv->hw->mac->get_umac_addr((void __iomem *)
                                             priv->dev->base_addr,
                                             priv->dev->dev_addr, 0);
+               if (!is_valid_ether_addr(priv->dev->dev_addr))
+                       eth_mac_idb(priv->dev->dev_addr);
                if (!is_valid_ether_addr(priv->dev->dev_addr))
                        eth_hw_addr_random(priv->dev);
        }