1 From f8c5db89e793a4bc6c1e87bd7b3a5cec16b75bc3 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Wed, 10 Sep 2014 22:42:14 +0200
4 Subject: [PATCH 35/36] owrt: lantiq: wifi and ethernet eeprom handling
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 .../mips/include/asm/mach-lantiq/xway/lantiq_soc.h | 3 +
9 arch/mips/lantiq/xway/Makefile | 3 +
10 arch/mips/lantiq/xway/ath5k_eep.c | 136 +++++++++++++++++++++
11 arch/mips/lantiq/xway/eth_mac.c | 25 ++++
12 drivers/net/ethernet/lantiq_etop.c | 6 +-
13 5 files changed, 172 insertions(+), 1 deletion(-)
14 create mode 100644 arch/mips/lantiq/xway/ath5k_eep.c
15 create mode 100644 arch/mips/lantiq/xway/eth_mac.c
17 --- a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
18 +++ b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
19 @@ -104,5 +104,8 @@ int xrx200_gphy_boot(struct device *dev,
20 extern void ltq_pmu_enable(unsigned int module);
21 extern void ltq_pmu_disable(unsigned int module);
23 +/* allow the ethernet driver to load a flash mapped mac addr */
24 +const u8* ltq_get_eth_mac(void);
26 #endif /* CONFIG_SOC_TYPE_XWAY */
27 #endif /* _LTQ_XWAY_H__ */
28 --- a/arch/mips/lantiq/xway/Makefile
29 +++ b/arch/mips/lantiq/xway/Makefile
30 @@ -2,4 +2,7 @@ obj-y := prom.o sysctrl.o clk.o reset.o
35 +obj-$(CONFIG_PCI) += ath5k_eep.o
37 obj-$(CONFIG_XRX200_PHY_FW) += xrx200_phy_fw.o
39 +++ b/arch/mips/lantiq/xway/ath5k_eep.c
42 + * Copyright (C) 2011 Luca Olivetti <luca@ventoso.org>
43 + * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
44 + * Copyright (C) 2011 Andrej Vlašić <andrej.vlasic0@gmail.com>
45 + * Copyright (C) 2013 Álvaro Fernández Rojas <noltari@gmail.com>
46 + * Copyright (C) 2013 Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
47 + * Copyright (C) 2015 Vittorio Gambaletta <openwrt@vittgam.net>
49 + * This program is free software; you can redistribute it and/or modify it
50 + * under the terms of the GNU General Public License version 2 as published
51 + * by the Free Software Foundation.
54 +#include <linux/init.h>
55 +#include <linux/platform_device.h>
56 +#include <linux/etherdevice.h>
57 +#include <linux/ath5k_platform.h>
58 +#include <linux/pci.h>
59 +#include <linux/err.h>
60 +#include <linux/mtd/mtd.h>
61 +#include <lantiq_soc.h>
63 +extern int (*ltq_pci_plat_dev_init)(struct pci_dev *dev);
64 +struct ath5k_platform_data ath5k_pdata;
65 +static u8 athxk_eeprom_mac[6];
67 +static int ath5k_pci_plat_dev_init(struct pci_dev *dev)
69 + dev->dev.platform_data = &ath5k_pdata;
73 +static int ath5k_eep_load;
74 +int __init of_ath5k_eeprom_probe(struct platform_device *pdev)
76 + struct device_node *np = pdev->dev.of_node, *mtd_np = NULL;
80 + struct mtd_info *the_mtd;
81 + size_t flash_readlen;
86 + list = of_get_property(np, "ath,eep-flash", &i);
87 + if (!list || (i != (2 * sizeof(*list))))
90 + phandle = be32_to_cpup(list++);
92 + mtd_np = of_find_node_by_phandle(phandle);
97 + part = of_get_property(mtd_np, "label", NULL);
99 + part = mtd_np->name;
101 + the_mtd = get_mtd_device_nm(part);
102 + if (IS_ERR(the_mtd))
105 + ath5k_pdata.eeprom_data = kmalloc(ATH5K_PLAT_EEP_MAX_WORDS<<1, GFP_KERNEL);
107 + i = mtd_read(the_mtd, be32_to_cpup(list), ATH5K_PLAT_EEP_MAX_WORDS << 1,
108 + &flash_readlen, (void *) ath5k_pdata.eeprom_data);
110 + if (!of_property_read_u32(np, "ath,mac-offset", &mac_offset)) {
111 + size_t mac_readlen;
112 + mtd_read(the_mtd, mac_offset, 6, &mac_readlen,
113 + (void *) athxk_eeprom_mac);
115 + put_mtd_device(the_mtd);
117 + if (((ATH5K_PLAT_EEP_MAX_WORDS<<1) != flash_readlen) || i) {
118 + dev_err(&pdev->dev, "failed to load eeprom from mtd\n");
122 + if (of_find_property(np, "ath,eep-swap", NULL))
123 + for (i = 0; i < ATH5K_PLAT_EEP_MAX_WORDS; i++)
124 + ath5k_pdata.eeprom_data[i] = swab16(ath5k_pdata.eeprom_data[i]);
126 + if (!is_valid_ether_addr(athxk_eeprom_mac) && ltq_get_eth_mac())
127 + ether_addr_copy(athxk_eeprom_mac, ltq_get_eth_mac());
129 + if (!is_valid_ether_addr(athxk_eeprom_mac)) {
130 + dev_warn(&pdev->dev, "using random mac\n");
131 + random_ether_addr(athxk_eeprom_mac);
134 + if (!of_property_read_u32(np, "ath,mac-increment", &mac_inc))
135 + athxk_eeprom_mac[5] += mac_inc;
137 + ath5k_pdata.macaddr = athxk_eeprom_mac;
138 + ltq_pci_plat_dev_init = ath5k_pci_plat_dev_init;
140 + dev_info(&pdev->dev, "loaded ath5k eeprom\n");
145 +static struct of_device_id ath5k_eeprom_ids[] = {
146 + { .compatible = "ath5k,eeprom" },
150 +static struct platform_driver ath5k_eeprom_driver = {
152 + .name = "ath5k,eeprom",
153 + .owner = THIS_MODULE,
154 + .of_match_table = of_match_ptr(ath5k_eeprom_ids),
158 +static int __init of_ath5k_eeprom_init(void)
160 + int ret = platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
163 + ath5k_eep_load = 1;
168 +static int __init of_ath5k_eeprom_init_late(void)
170 + if (!ath5k_eep_load)
173 + return platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
175 +late_initcall(of_ath5k_eeprom_init_late);
176 +subsys_initcall(of_ath5k_eeprom_init);
178 +++ b/arch/mips/lantiq/xway/eth_mac.c
181 + * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
183 + * This program is free software; you can redistribute it and/or modify it
184 + * under the terms of the GNU General Public License version 2 as published
185 + * by the Free Software Foundation.
188 +#include <linux/init.h>
189 +#include <linux/if_ether.h>
191 +static u8 eth_mac[6];
192 +static int eth_mac_set;
194 +const u8* ltq_get_eth_mac(void)
199 +static int __init setup_ethaddr(char *str)
201 + eth_mac_set = mac_pton(str, eth_mac);
202 + return !eth_mac_set;
204 +early_param("ethaddr", setup_ethaddr);
205 --- a/drivers/net/ethernet/lantiq_etop.c
206 +++ b/drivers/net/ethernet/lantiq_etop.c
207 @@ -840,7 +840,11 @@ ltq_etop_init(struct net_device *dev)
211 - memcpy(&mac, &priv->pldata->mac, sizeof(struct sockaddr));
212 + memcpy(&mac.sa_data, ltq_get_eth_mac(), ETH_ALEN);
214 + if (priv->mac && !is_valid_ether_addr(mac.sa_data))
215 + memcpy(&mac.sa_data, priv->mac, ETH_ALEN);
217 if (!is_valid_ether_addr(mac.sa_data)) {
218 pr_warn("etop: invalid MAC, using random\n");
219 eth_random_addr(mac.sa_data);