73b3c3a3c04a2b6543388654613da456c260ed42
[openwrt/staging/chunkeey.git] / target / linux / lantiq / patches-4.4 / 0035-owrt-lantiq-wifi-and-ethernet-eeprom-handling.patch
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
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
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
16
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);
22
23 +/* allow the ethernet driver to load a flash mapped mac addr */
24 +const u8* ltq_get_eth_mac(void);
25 +
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
31
32 obj-y += vmmc.o tffs.o
33
34 +obj-y += eth_mac.o
35 +obj-$(CONFIG_PCI) += ath5k_eep.o
36 +
37 obj-$(CONFIG_XRX200_PHY_FW) += xrx200_phy_fw.o
38 --- /dev/null
39 +++ b/arch/mips/lantiq/xway/ath5k_eep.c
40 @@ -0,0 +1,136 @@
41 +/*
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>
48 + *
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.
52 + */
53 +
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>
62 +
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];
66 +
67 +static int ath5k_pci_plat_dev_init(struct pci_dev *dev)
68 +{
69 + dev->dev.platform_data = &ath5k_pdata;
70 + return 0;
71 +}
72 +
73 +static int ath5k_eep_load;
74 +int __init of_ath5k_eeprom_probe(struct platform_device *pdev)
75 +{
76 + struct device_node *np = pdev->dev.of_node, *mtd_np = NULL;
77 + int mac_offset;
78 + u32 mac_inc = 0;
79 + int i;
80 + struct mtd_info *the_mtd;
81 + size_t flash_readlen;
82 + const __be32 *list;
83 + const char *part;
84 + phandle phandle;
85 +
86 + list = of_get_property(np, "ath,eep-flash", &i);
87 + if (!list || (i != (2 * sizeof(*list))))
88 + return -ENODEV;
89 +
90 + phandle = be32_to_cpup(list++);
91 + if (phandle)
92 + mtd_np = of_find_node_by_phandle(phandle);
93 +
94 + if (!mtd_np)
95 + return -ENODEV;
96 +
97 + part = of_get_property(mtd_np, "label", NULL);
98 + if (!part)
99 + part = mtd_np->name;
100 +
101 + the_mtd = get_mtd_device_nm(part);
102 + if (IS_ERR(the_mtd))
103 + return -ENODEV;
104 +
105 + ath5k_pdata.eeprom_data = kmalloc(ATH5K_PLAT_EEP_MAX_WORDS<<1, GFP_KERNEL);
106 +
107 + i = mtd_read(the_mtd, be32_to_cpup(list), ATH5K_PLAT_EEP_MAX_WORDS << 1,
108 + &flash_readlen, (void *) ath5k_pdata.eeprom_data);
109 +
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);
114 + }
115 + put_mtd_device(the_mtd);
116 +
117 + if (((ATH5K_PLAT_EEP_MAX_WORDS<<1) != flash_readlen) || i) {
118 + dev_err(&pdev->dev, "failed to load eeprom from mtd\n");
119 + return -ENODEV;
120 + }
121 +
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]);
125 +
126 + if (!is_valid_ether_addr(athxk_eeprom_mac) && ltq_get_eth_mac())
127 + ether_addr_copy(athxk_eeprom_mac, ltq_get_eth_mac());
128 +
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);
132 + }
133 +
134 + if (!of_property_read_u32(np, "ath,mac-increment", &mac_inc))
135 + athxk_eeprom_mac[5] += mac_inc;
136 +
137 + ath5k_pdata.macaddr = athxk_eeprom_mac;
138 + ltq_pci_plat_dev_init = ath5k_pci_plat_dev_init;
139 +
140 + dev_info(&pdev->dev, "loaded ath5k eeprom\n");
141 +
142 + return 0;
143 +}
144 +
145 +static struct of_device_id ath5k_eeprom_ids[] = {
146 + { .compatible = "ath5k,eeprom" },
147 + { }
148 +};
149 +
150 +static struct platform_driver ath5k_eeprom_driver = {
151 + .driver = {
152 + .name = "ath5k,eeprom",
153 + .owner = THIS_MODULE,
154 + .of_match_table = of_match_ptr(ath5k_eeprom_ids),
155 + },
156 +};
157 +
158 +static int __init of_ath5k_eeprom_init(void)
159 +{
160 + int ret = platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
161 +
162 + if (ret)
163 + ath5k_eep_load = 1;
164 +
165 + return ret;
166 +}
167 +
168 +static int __init of_ath5k_eeprom_init_late(void)
169 +{
170 + if (!ath5k_eep_load)
171 + return 0;
172 +
173 + return platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
174 +}
175 +late_initcall(of_ath5k_eeprom_init_late);
176 +subsys_initcall(of_ath5k_eeprom_init);
177 --- /dev/null
178 +++ b/arch/mips/lantiq/xway/eth_mac.c
179 @@ -0,0 +1,25 @@
180 +/*
181 + * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
182 + *
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.
186 + */
187 +
188 +#include <linux/init.h>
189 +#include <linux/if_ether.h>
190 +
191 +static u8 eth_mac[6];
192 +static int eth_mac_set;
193 +
194 +const u8* ltq_get_eth_mac(void)
195 +{
196 + return eth_mac;
197 +}
198 +
199 +static int __init setup_ethaddr(char *str)
200 +{
201 + eth_mac_set = mac_pton(str, eth_mac);
202 + return !eth_mac_set;
203 +}
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)
208 if (err)
209 goto err_hw;
210
211 - memcpy(&mac, &priv->pldata->mac, sizeof(struct sockaddr));
212 + memcpy(&mac.sa_data, ltq_get_eth_mac(), ETH_ALEN);
213 +
214 + if (priv->mac && !is_valid_ether_addr(mac.sa_data))
215 + memcpy(&mac.sa_data, priv->mac, ETH_ALEN);
216 +
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);