From f8c5db89e793a4bc6c1e87bd7b3a5cec16b75bc3 Mon Sep 17 00:00:00 2001
From: John Crispin <blogic@openwrt.org>
Date: Wed, 10 Sep 2014 22:42:14 +0200
Subject: [PATCH 35/36] owrt: lantiq: wifi and ethernet eeprom handling

Signed-off-by: John Crispin <blogic@openwrt.org>
---
 .../mips/include/asm/mach-lantiq/xway/lantiq_soc.h |   3 +
 arch/mips/lantiq/xway/Makefile                     |   3 +
 arch/mips/lantiq/xway/ath5k_eep.c                  | 136 +++++++++++++++++++++
 arch/mips/lantiq/xway/eth_mac.c                    |  25 ++++
 drivers/net/ethernet/lantiq_etop.c                 |   6 +-
 5 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/lantiq/xway/ath5k_eep.c
 create mode 100644 arch/mips/lantiq/xway/eth_mac.c

--- a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
+++ b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
@@ -102,5 +102,8 @@ int xrx200_gphy_boot(struct device *dev,
 extern void ltq_pmu_enable(unsigned int module);
 extern void ltq_pmu_disable(unsigned int module);
 
+/* allow the ethernet driver to load a flash mapped mac addr */
+const u8* ltq_get_eth_mac(void);
+
 #endif /* CONFIG_SOC_TYPE_XWAY */
 #endif /* _LTQ_XWAY_H__ */
--- a/arch/mips/lantiq/xway/Makefile
+++ b/arch/mips/lantiq/xway/Makefile
@@ -8,3 +8,6 @@ obj-y += timer.o
 endif
 
 obj-y += vmmc.o
+
+obj-y += eth_mac.o
+obj-$(CONFIG_PCI) += ath5k_eep.o
--- /dev/null
+++ b/arch/mips/lantiq/xway/ath5k_eep.c
@@ -0,0 +1,136 @@
+/*
+ *  Copyright (C) 2011 Luca Olivetti <luca@ventoso.org>
+ *  Copyright (C) 2011 John Crispin <blogic@openwrt.org>
+ *  Copyright (C) 2011 Andrej Vlašić <andrej.vlasic0@gmail.com>
+ *  Copyright (C) 2013 Álvaro Fernández Rojas <noltari@gmail.com>
+ *  Copyright (C) 2013 Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
+ *  Copyright (C) 2015 Vittorio Gambaletta <openwrt@vittgam.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify it
+ *  under the terms of the GNU General Public License version 2 as published
+ *  by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/etherdevice.h>
+#include <linux/ath5k_platform.h>
+#include <linux/pci.h>
+#include <linux/err.h>
+#include <linux/mtd/mtd.h>
+#include <lantiq_soc.h>
+
+extern int (*ltq_pci_plat_dev_init)(struct pci_dev *dev);
+struct ath5k_platform_data ath5k_pdata;
+static u8 athxk_eeprom_mac[6];
+
+static int ath5k_pci_plat_dev_init(struct pci_dev *dev)
+{
+	dev->dev.platform_data = &ath5k_pdata;
+	return 0;
+}
+
+static int ath5k_eep_load;
+int __init of_ath5k_eeprom_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node, *mtd_np = NULL;
+	int mac_offset;
+	u32 mac_inc = 0;
+	int i;
+	struct mtd_info *the_mtd;
+	size_t flash_readlen;
+	const __be32 *list;
+	const char *part;
+	phandle phandle;
+
+	list = of_get_property(np, "ath,eep-flash", &i);
+	if (!list || (i != (2 * sizeof(*list))))
+		return -ENODEV;
+
+	phandle = be32_to_cpup(list++);
+	if (phandle)
+		mtd_np = of_find_node_by_phandle(phandle);
+
+	if (!mtd_np)
+		return -ENODEV;
+
+	part = of_get_property(mtd_np, "label", NULL);
+	if (!part)
+		part = mtd_np->name;
+
+	the_mtd = get_mtd_device_nm(part);
+	if (IS_ERR(the_mtd))
+		return -ENODEV;
+
+	ath5k_pdata.eeprom_data = kmalloc(ATH5K_PLAT_EEP_MAX_WORDS<<1, GFP_KERNEL);
+
+	i = mtd_read(the_mtd, be32_to_cpup(list), ATH5K_PLAT_EEP_MAX_WORDS << 1,
+		&flash_readlen, (void *) ath5k_pdata.eeprom_data);
+
+	if (!of_property_read_u32(np, "ath,mac-offset", &mac_offset)) {
+		size_t mac_readlen;
+		mtd_read(the_mtd, mac_offset, 6, &mac_readlen,
+			(void *) athxk_eeprom_mac);
+	}
+	put_mtd_device(the_mtd);
+
+	if (((ATH5K_PLAT_EEP_MAX_WORDS<<1) != flash_readlen) || i) {
+		dev_err(&pdev->dev, "failed to load eeprom from mtd\n");
+		return -ENODEV;
+	}
+
+	if (of_find_property(np, "ath,eep-swap", NULL))
+		for (i = 0; i < ATH5K_PLAT_EEP_MAX_WORDS; i++)
+			ath5k_pdata.eeprom_data[i] = swab16(ath5k_pdata.eeprom_data[i]);
+
+	if (!is_valid_ether_addr(athxk_eeprom_mac) && ltq_get_eth_mac())
+		ether_addr_copy(athxk_eeprom_mac, ltq_get_eth_mac());
+
+	if (!is_valid_ether_addr(athxk_eeprom_mac)) {
+		dev_warn(&pdev->dev, "using random mac\n");
+		eth_random_addr(athxk_eeprom_mac);
+	}
+
+	if (!of_property_read_u32(np, "ath,mac-increment", &mac_inc))
+		athxk_eeprom_mac[5] += mac_inc;
+
+	ath5k_pdata.macaddr = athxk_eeprom_mac;
+	ltq_pci_plat_dev_init = ath5k_pci_plat_dev_init;
+
+	dev_info(&pdev->dev, "loaded ath5k eeprom\n");
+
+	return 0;
+}
+
+static struct of_device_id ath5k_eeprom_ids[] = {
+	{ .compatible = "ath5k,eeprom" },
+	{ }
+};
+
+static struct platform_driver ath5k_eeprom_driver = {
+	.driver		= {
+		.name		= "ath5k,eeprom",
+		.owner	= THIS_MODULE,
+		.of_match_table	= of_match_ptr(ath5k_eeprom_ids),
+	},
+};
+
+static int __init of_ath5k_eeprom_init(void)
+{
+	int ret = platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
+
+	if (ret)
+		ath5k_eep_load = 1;
+
+	return ret;
+}
+
+static int __init of_ath5k_eeprom_init_late(void)
+{
+	if (!ath5k_eep_load)
+		return 0;
+
+	return platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
+}
+late_initcall(of_ath5k_eeprom_init_late);
+subsys_initcall(of_ath5k_eeprom_init);
--- /dev/null
+++ b/arch/mips/lantiq/xway/eth_mac.c
@@ -0,0 +1,25 @@
+/*
+ *  Copyright (C) 2012 John Crispin <blogic@openwrt.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify it
+ *  under the terms of the GNU General Public License version 2 as published
+ *  by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/if_ether.h>
+
+static u8 eth_mac[6];
+static int eth_mac_set;
+
+const u8* ltq_get_eth_mac(void)
+{
+	return eth_mac;
+}
+
+static int __init setup_ethaddr(char *str)
+{
+	eth_mac_set = mac_pton(str, eth_mac);
+	return !eth_mac_set;
+}
+early_param("ethaddr", setup_ethaddr);
--- a/drivers/net/ethernet/lantiq_etop.c
+++ b/drivers/net/ethernet/lantiq_etop.c
@@ -763,7 +763,11 @@ ltq_etop_init(struct net_device *dev)
 	if (err)
 		goto err_hw;
 
-	memcpy(&mac, &priv->pldata->mac, sizeof(struct sockaddr));
+	memcpy(&mac.sa_data, ltq_get_eth_mac(), ETH_ALEN);
+
+	if (!is_valid_ether_addr(mac.sa_data))
+		memcpy(&mac.sa_data, priv->mac, ETH_ALEN);
+
 	if (!is_valid_ether_addr(mac.sa_data)) {
 		pr_warn("etop: invalid MAC, using random\n");
 		eth_random_addr(mac.sa_data);
