lantiq: Fixes to athxk EEPROM loading
[openwrt.git] / target / linux / lantiq / patches-3.8 / 0037-owrt-lantiq-wifi-and-ethernet-eeprom-handling.patch
1 From 0c9b05716ac0e597ae0f81a96ff68e54716decc9 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Wed, 13 Mar 2013 10:02:58 +0100
4 Subject: [PATCH 37/40] owrt: lantiq: wifi and ethernet eeprom handling
5
6 ---
7  arch/mips/include/asm/mach-lantiq/pci-ath-fixup.h  |    6 +
8  .../mips/include/asm/mach-lantiq/xway/lantiq_soc.h |    3 +
9  arch/mips/lantiq/xway/Makefile                     |    3 +
10  arch/mips/lantiq/xway/ath_eep.c                    |  206 ++++++++++++++++++++
11  arch/mips/lantiq/xway/eth_mac.c                    |   76 ++++++++
12  arch/mips/lantiq/xway/pci-ath-fixup.c              |  109 +++++++++++
13  arch/mips/lantiq/xway/rt_eep.c                     |   60 ++++++
14  drivers/net/ethernet/lantiq_etop.c                 |   10 +-
15  8 files changed, 469 insertions(+), 4 deletions(-)
16  create mode 100644 arch/mips/include/asm/mach-lantiq/pci-ath-fixup.h
17  create mode 100644 arch/mips/lantiq/xway/ath_eep.c
18  create mode 100644 arch/mips/lantiq/xway/eth_mac.c
19  create mode 100644 arch/mips/lantiq/xway/pci-ath-fixup.c
20  create mode 100644 arch/mips/lantiq/xway/rt_eep.c
21
22 --- /dev/null
23 +++ b/arch/mips/include/asm/mach-lantiq/pci-ath-fixup.h
24 @@ -0,0 +1,6 @@
25 +#ifndef _PCI_ATH_FIXUP
26 +#define _PCI_ATH_FIXUP
27 +
28 +void ltq_pci_ath_fixup(unsigned slot, u16 *cal_data) __init;
29 +
30 +#endif /* _PCI_ATH_FIXUP */
31 --- a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
32 +++ b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
33 @@ -90,5 +90,8 @@ int xrx200_gphy_boot(struct device *dev,
34  extern void ltq_pmu_enable(unsigned int module);
35  extern void ltq_pmu_disable(unsigned int module);
36  
37 +/* allow the ethernet driver to load a flash mapped mac addr */
38 +const u8* ltq_get_eth_mac(void);
39 +
40  #endif /* CONFIG_SOC_TYPE_XWAY */
41  #endif /* _LTQ_XWAY_H__ */
42 --- a/arch/mips/lantiq/xway/Makefile
43 +++ b/arch/mips/lantiq/xway/Makefile
44 @@ -1,3 +1,6 @@
45  obj-y := prom.o sysctrl.o clk.o reset.o dma.o gptu.o dcdc.o
46  
47 +obj-y += eth_mac.o
48 +obj-$(CONFIG_PCI) += ath_eep.o rt_eep.o pci-ath-fixup.o
49 +
50  obj-$(CONFIG_XRX200_PHY_FW) += xrx200_phy_fw.o
51 --- /dev/null
52 +++ b/arch/mips/lantiq/xway/ath_eep.c
53 @@ -0,0 +1,195 @@
54 +/*
55 + *  Copyright (C) 2011 Luca Olivetti <luca@ventoso.org>
56 + *  Copyright (C) 2011 John Crispin <blogic@openwrt.org>
57 + *  Copyright (C) 2011 Andrej Vlašić <andrej.vlasic0@gmail.com>
58 + *  Copyright (C) 2013 Álvaro Fernández Rojas <noltari@gmail.com>
59 + *
60 + *  This program is free software; you can redistribute it and/or modify it
61 + *  under the terms of the GNU General Public License version 2 as published
62 + *  by the Free Software Foundation.
63 + */
64 +
65 +#include <linux/init.h>
66 +#include <linux/module.h>
67 +#include <linux/platform_device.h>
68 +#include <linux/etherdevice.h>
69 +#include <linux/ath5k_platform.h>
70 +#include <linux/ath9k_platform.h>
71 +#include <linux/pci.h>
72 +#include <pci-ath-fixup.h>
73 +
74 +extern int (*ltq_pci_plat_dev_init)(struct pci_dev *dev);
75 +struct ath5k_platform_data ath5k_pdata;
76 +struct ath9k_platform_data ath9k_pdata = {
77 +       .led_pin = -1,
78 +};
79 +static u8 athxk_eeprom_mac[6];
80 +
81 +static int ath9k_pci_plat_dev_init(struct pci_dev *dev)
82 +{
83 +       dev->dev.platform_data = &ath9k_pdata;
84 +       return 0;
85 +}
86 +
87 +int __init of_ath9k_eeprom_probe(struct platform_device *pdev)
88 +{
89 +       struct device_node *np = pdev->dev.of_node;
90 +       struct resource *eep_res, *mac_res;
91 +       void __iomem *eep, *mac;
92 +       int mac_offset;
93 +       u32 mac_inc = 0, pci_slot = 0;
94 +       int i;
95 +
96 +       eep_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
97 +       mac_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
98 +
99 +       if (!eep_res) {
100 +               dev_err(&pdev->dev, "failed to load eeprom address\n");
101 +               return -ENODEV;
102 +       }
103 +       if (resource_size(eep_res) != ATH9K_PLAT_EEP_MAX_WORDS << 1) {
104 +               dev_err(&pdev->dev, "eeprom has an invalid size\n");
105 +               return -EINVAL;
106 +       }
107 +
108 +       eep = ioremap(eep_res->start, resource_size(eep_res));
109 +       memcpy_fromio(ath9k_pdata.eeprom_data, eep, ATH9K_PLAT_EEP_MAX_WORDS << 1);
110 +
111 +       if (of_find_property(np, "ath,eep-swap", NULL))
112 +               for (i = 0; i < ATH9K_PLAT_EEP_MAX_WORDS; i++)
113 +                       ath9k_pdata.eeprom_data[i] = swab16(ath9k_pdata.eeprom_data[i]);
114 +
115 +       if (of_find_property(np, "ath,eep-endian", NULL)) {
116 +               ath9k_pdata.endian_check = true;
117 +
118 +               dev_info(&pdev->dev, "endian check enabled.\n");
119 +       }
120 +
121 +       if (!of_property_read_u32(np, "ath,mac-offset", &mac_offset)) {
122 +               memcpy_fromio(athxk_eeprom_mac, (void*) ath9k_pdata.eeprom_data + mac_offset, 6);
123 +       } else if (mac_res) {
124 +               if (resource_size(mac_res) != 6) {
125 +                       dev_err(&pdev->dev, "mac has an invalid size\n");
126 +                       return -EINVAL;
127 +               }
128 +               mac = ioremap(mac_res->start, resource_size(mac_res));
129 +               memcpy_fromio(athxk_eeprom_mac, mac, 6);
130 +       } else {
131 +               dev_warn(&pdev->dev, "using random mac\n");
132 +               random_ether_addr(athxk_eeprom_mac);
133 +       }
134 +
135 +       if (!of_property_read_u32(np, "ath,mac-increment", &mac_inc))
136 +               athxk_eeprom_mac[5] += mac_inc;
137 +
138 +       ath9k_pdata.macaddr = athxk_eeprom_mac;
139 +       ltq_pci_plat_dev_init = ath9k_pci_plat_dev_init;
140 +
141 +       if (!of_property_read_u32(np, "ath,pci-slot", &pci_slot)) {
142 +               ltq_pci_ath_fixup(pci_slot, ath9k_pdata.eeprom_data);
143 +
144 +               dev_info(&pdev->dev, "pci slot: %u\n", pci_slot);
145 +       }
146 +
147 +       dev_info(&pdev->dev, "loaded ath9k eeprom\n");
148 +
149 +       return 0;
150 +}
151 +
152 +static struct of_device_id ath9k_eeprom_ids[] = {
153 +       { .compatible = "ath9k,eeprom" },
154 +       { }
155 +};
156 +
157 +static struct platform_driver ath9k_eeprom_driver = {
158 +       .driver         = {
159 +               .name           = "ath9k,eeprom",
160 +               .owner  = THIS_MODULE,
161 +               .of_match_table = of_match_ptr(ath9k_eeprom_ids),
162 +       },
163 +};
164 +
165 +static int __init of_ath9k_eeprom_init(void)
166 +{
167 +       return platform_driver_probe(&ath9k_eeprom_driver, of_ath9k_eeprom_probe);
168 +}
169 +late_initcall(of_ath9k_eeprom_init);
170 +
171 +
172 +static int ath5k_pci_plat_dev_init(struct pci_dev *dev)
173 +{
174 +       dev->dev.platform_data = &ath5k_pdata;
175 +       return 0;
176 +}
177 +
178 +int __init of_ath5k_eeprom_probe(struct platform_device *pdev)
179 +{
180 +       struct device_node *np = pdev->dev.of_node;
181 +       struct resource *eep_res, *mac_res;
182 +       void __iomem *eep, *mac;
183 +       int mac_offset;
184 +       u32 mac_inc = 0;
185 +       int i;
186 +
187 +       eep_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188 +       mac_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
189 +
190 +       if (!eep_res) {
191 +               dev_err(&pdev->dev, "failed to load eeprom address\n");
192 +               return -ENODEV;
193 +       }
194 +       if (resource_size(eep_res) != ATH5K_PLAT_EEP_MAX_WORDS << 1) {
195 +               dev_err(&pdev->dev, "eeprom has an invalid size\n");
196 +               return -EINVAL;
197 +       }
198 +
199 +       eep = ioremap(eep_res->start, resource_size(eep_res));
200 +       memcpy_fromio(ath5k_pdata.eeprom_data, eep, ATH5K_PLAT_EEP_MAX_WORDS << 1);
201 +
202 +       if (of_find_property(np, "ath,eep-swap", NULL))
203 +               for (i = 0; i < ATH5K_PLAT_EEP_MAX_WORDS; i++)
204 +                       ath5k_pdata.eeprom_data[i] = swab16(ath5k_pdata.eeprom_data[i]);
205 +
206 +       if (!of_property_read_u32(np, "ath,mac-offset", &mac_offset)) {
207 +               memcpy_fromio(athxk_eeprom_mac, (void*) ath5k_pdata.eeprom_data + mac_offset, 6);
208 +       } else if (mac_res) {
209 +               if (resource_size(mac_res) != 6) {
210 +                       dev_err(&pdev->dev, "mac has an invalid size\n");
211 +                       return -EINVAL;
212 +               }
213 +               mac = ioremap(mac_res->start, resource_size(mac_res));
214 +               memcpy_fromio(athxk_eeprom_mac, mac, 6);
215 +       } else {
216 +               dev_warn(&pdev->dev, "using random mac\n");
217 +               random_ether_addr(athxk_eeprom_mac);
218 +       }
219 +
220 +       if (!of_property_read_u32(np, "ath,mac-increment", &mac_inc))
221 +               athxk_eeprom_mac[5] += mac_inc;
222 +
223 +       ath5k_pdata.macaddr = athxk_eeprom_mac;
224 +       ltq_pci_plat_dev_init = ath5k_pci_plat_dev_init;
225 +
226 +       dev_info(&pdev->dev, "loaded ath5k eeprom\n");
227 +
228 +       return 0;
229 +}
230 +
231 +static struct of_device_id ath5k_eeprom_ids[] = {
232 +       { .compatible = "ath5k,eeprom" },
233 +       { }
234 +};
235 +
236 +static struct platform_driver ath5k_eeprom_driver = {
237 +       .driver         = {
238 +               .name           = "ath5k,eeprom",
239 +               .owner  = THIS_MODULE,
240 +               .of_match_table = of_match_ptr(ath5k_eeprom_ids),
241 +       },
242 +};
243 +
244 +static int __init of_ath5k_eeprom_init(void)
245 +{
246 +       return platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
247 +}
248 +late_initcall(of_ath5k_eeprom_init);
249 --- /dev/null
250 +++ b/arch/mips/lantiq/xway/eth_mac.c
251 @@ -0,0 +1,76 @@
252 +/*
253 + *  Copyright (C) 2012 John Crispin <blogic@openwrt.org>
254 + *
255 + *  This program is free software; you can redistribute it and/or modify it
256 + *  under the terms of the GNU General Public License version 2 as published
257 + *  by the Free Software Foundation.
258 + */
259 +
260 +#include <linux/init.h>
261 +#include <linux/module.h>
262 +#include <linux/of_platform.h>
263 +#include <linux/if_ether.h>
264 +
265 +static u8 eth_mac[6];
266 +static int eth_mac_set;
267 +
268 +const u8* ltq_get_eth_mac(void)
269 +{
270 +       return eth_mac;
271 +}
272 +
273 +static int __init setup_ethaddr(char *str)
274 +{
275 +       eth_mac_set = mac_pton(str, eth_mac);
276 +       return !eth_mac_set;
277 +}
278 +__setup("ethaddr=", setup_ethaddr);
279 +
280 +int __init of_eth_mac_probe(struct platform_device *pdev)
281 +{
282 +       struct device_node *np = pdev->dev.of_node;
283 +       struct resource *mac_res;
284 +       void __iomem *mac;
285 +       u32 mac_inc = 0;
286 +
287 +       if (eth_mac_set) {
288 +               dev_err(&pdev->dev, "mac was already set by bootloader\n");
289 +               return -EINVAL;
290 +       }
291 +       mac_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
292 +
293 +       if (!mac_res) {
294 +               dev_err(&pdev->dev, "failed to load mac\n");
295 +               return -EINVAL;
296 +       }
297 +       if (resource_size(mac_res) != 6) {
298 +               dev_err(&pdev->dev, "mac has an invalid size\n");
299 +               return -EINVAL;
300 +       }
301 +       mac = ioremap(mac_res->start, resource_size(mac_res));
302 +       memcpy_fromio(eth_mac, mac, 6);
303 +
304 +       if (!of_property_read_u32(np, "mac-increment", &mac_inc))
305 +               eth_mac[5] += mac_inc;
306 +
307 +       return 0;
308 +}
309 +
310 +static struct of_device_id eth_mac_ids[] = {
311 +       { .compatible = "lantiq,eth-mac" },
312 +       { /* sentinel */ }
313 +};
314 +
315 +static struct platform_driver eth_mac_driver = {
316 +       .driver         = {
317 +               .name           = "lantiq,eth-mac",
318 +               .owner  = THIS_MODULE,
319 +               .of_match_table = of_match_ptr(eth_mac_ids),
320 +       },
321 +};
322 +
323 +static int __init of_eth_mac_init(void)
324 +{
325 +       return platform_driver_probe(&eth_mac_driver, of_eth_mac_probe);
326 +}
327 +device_initcall(of_eth_mac_init);
328 --- /dev/null
329 +++ b/arch/mips/lantiq/xway/pci-ath-fixup.c
330 @@ -0,0 +1,109 @@
331 +/*
332 + *  Atheros AP94 reference board PCI initialization
333 + *
334 + *  Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
335 + *
336 + *  This program is free software; you can redistribute it and/or modify it
337 + *  under the terms of the GNU General Public License version 2 as published
338 + *  by the Free Software Foundation.
339 + */
340 +
341 +#include <linux/pci.h>
342 +#include <linux/init.h>
343 +#include <linux/delay.h>
344 +#include <lantiq_soc.h>
345 +
346 +#define LTQ_PCI_MEM_BASE               0x18000000
347 +
348 +struct ath_fixup {
349 +       u16             *cal_data;
350 +       unsigned        slot;
351 +};
352 +
353 +static int ath_num_fixups;
354 +static struct ath_fixup ath_fixups[2];
355 +
356 +static void ath_pci_fixup(struct pci_dev *dev)
357 +{
358 +       void __iomem *mem;
359 +       u16 *cal_data = NULL;
360 +       u16 cmd;
361 +       u32 bar0;
362 +       u32 val;
363 +       unsigned i;
364 +
365 +       for (i = 0; i < ath_num_fixups; i++) {
366 +               if (ath_fixups[i].cal_data == NULL)
367 +                       continue;
368 +
369 +               if (ath_fixups[i].slot != PCI_SLOT(dev->devfn))
370 +                       continue;
371 +
372 +               cal_data = ath_fixups[i].cal_data;
373 +               break;
374 +       }
375 +
376 +       if (cal_data == NULL)
377 +               return;
378 +
379 +       if (*cal_data != 0xa55a) {
380 +               pr_err("pci %s: invalid calibration data\n", pci_name(dev));
381 +               return;
382 +       }
383 +
384 +       pr_info("pci %s: fixup device configuration\n", pci_name(dev));
385 +
386 +       mem = ioremap(LTQ_PCI_MEM_BASE, 0x10000);
387 +       if (!mem) {
388 +               pr_err("pci %s: ioremap error\n", pci_name(dev));
389 +               return;
390 +       }
391 +
392 +       pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &bar0);
393 +       pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, LTQ_PCI_MEM_BASE);
394 +       pci_read_config_word(dev, PCI_COMMAND, &cmd);
395 +       cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
396 +       pci_write_config_word(dev, PCI_COMMAND, cmd);
397 +
398 +       /* set pointer to first reg address */
399 +       cal_data += 3;
400 +       while (*cal_data != 0xffff) {
401 +               u32 reg;
402 +               reg = *cal_data++;
403 +               val = *cal_data++;
404 +               val |= (*cal_data++) << 16;
405 +
406 +               ltq_w32(swab32(val), mem + reg);
407 +               udelay(100);
408 +       }
409 +
410 +       pci_read_config_dword(dev, PCI_VENDOR_ID, &val);
411 +       dev->vendor = val & 0xffff;
412 +       dev->device = (val >> 16) & 0xffff;
413 +
414 +       pci_read_config_dword(dev, PCI_CLASS_REVISION, &val);
415 +       dev->revision = val & 0xff;
416 +       dev->class = val >> 8; /* upper 3 bytes */
417 +
418 +       pr_info("pci %s: fixup info: [%04x:%04x] revision %02x class %#08x\n", 
419 +               pci_name(dev), dev->vendor, dev->device, dev->revision, dev->class);
420 +
421 +       pci_read_config_word(dev, PCI_COMMAND, &cmd);
422 +       cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
423 +       pci_write_config_word(dev, PCI_COMMAND, cmd);
424 +
425 +       pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
426 +
427 +       iounmap(mem);
428 +}
429 +DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_ATHEROS, PCI_ANY_ID, ath_pci_fixup);
430 +
431 +void __init ltq_pci_ath_fixup(unsigned slot, u16 *cal_data)
432 +{
433 +       if (ath_num_fixups >= ARRAY_SIZE(ath_fixups))
434 +               return;
435 +
436 +       ath_fixups[ath_num_fixups].slot = slot;
437 +       ath_fixups[ath_num_fixups].cal_data = cal_data;
438 +       ath_num_fixups++;
439 +}
440 --- /dev/null
441 +++ b/arch/mips/lantiq/xway/rt_eep.c
442 @@ -0,0 +1,60 @@
443 +/*
444 + *  Copyright (C) 2011 John Crispin <blogic@openwrt.org>
445 + *
446 + *  This program is free software; you can redistribute it and/or modify it
447 + *  under the terms of the GNU General Public License version 2 as published
448 + *  by the Free Software Foundation.
449 + */
450 +
451 +#include <linux/init.h>
452 +#include <linux/module.h>
453 +#include <linux/pci.h>
454 +#include <linux/platform_device.h>
455 +#include <linux/rt2x00_platform.h>
456 +
457 +extern int (*ltq_pci_plat_dev_init)(struct pci_dev *dev);
458 +static struct rt2x00_platform_data rt2x00_pdata;
459 +
460 +static int rt2x00_pci_plat_dev_init(struct pci_dev *dev)
461 +{
462 +       dev->dev.platform_data = &rt2x00_pdata;
463 +       return 0;
464 +}
465 +
466 +int __init of_ralink_eeprom_probe(struct platform_device *pdev)
467 +{
468 +       struct device_node *np = pdev->dev.of_node;
469 +       const char *eeprom;
470 +
471 +       if (of_property_read_string(np, "ralink,eeprom", &eeprom)) {
472 +               dev_err(&pdev->dev, "failed to load eeprom filename\n");
473 +               return 0;
474 +       }
475 +
476 +       rt2x00_pdata.eeprom_file_name = kstrdup(eeprom, GFP_KERNEL);
477 +//     rt2x00_pdata.mac_address = mac;
478 +       ltq_pci_plat_dev_init = rt2x00_pci_plat_dev_init;
479 +
480 +       dev_info(&pdev->dev, "using %s as eeprom\n", eeprom);
481 +
482 +       return 0;
483 +}
484 +
485 +static struct of_device_id ralink_eeprom_ids[] = {
486 +       { .compatible = "ralink,eeprom" },
487 +       { }
488 +};
489 +
490 +static struct platform_driver ralink_eeprom_driver = {
491 +       .driver         = {
492 +               .name           = "ralink,eeprom",
493 +               .owner  = THIS_MODULE,
494 +               .of_match_table = of_match_ptr(ralink_eeprom_ids),
495 +       },
496 +};
497 +
498 +static int __init of_ralink_eeprom_init(void)
499 +{
500 +       return platform_driver_probe(&ralink_eeprom_driver, of_ralink_eeprom_probe);
501 +}
502 +device_initcall(of_ralink_eeprom_init);
503 --- a/drivers/net/ethernet/lantiq_etop.c
504 +++ b/drivers/net/ethernet/lantiq_etop.c
505 @@ -826,7 +826,8 @@ ltq_etop_init(struct net_device *dev)
506  
507         ltq_etop_change_mtu(dev, 1500);
508  
509 -       memcpy(&mac.sa_data, priv->mac, ETH_ALEN);
510 +       if (priv->mac)
511 +               memcpy(&mac.sa_data, priv->mac, ETH_ALEN);
512         if (!is_valid_ether_addr(mac.sa_data)) {
513                 pr_warn("etop: invalid MAC, using random\n");
514                 random_ether_addr(mac.sa_data);
515 @@ -885,8 +886,7 @@ static const struct net_device_ops ltq_e
516         .ndo_tx_timeout = ltq_etop_tx_timeout,
517  };
518  
519 -static int __devinit
520 -ltq_etop_probe(struct platform_device *pdev)
521 +static int ltq_etop_probe(struct platform_device *pdev)
522  {
523         struct net_device *dev;
524         struct ltq_etop_priv *priv;
525 @@ -950,7 +950,9 @@ ltq_etop_probe(struct platform_device *p
526         priv->tx_irq = irqres[0].start;
527         priv->rx_irq = irqres[1].start;
528         priv->mii_mode = of_get_phy_mode(pdev->dev.of_node);
529 -       priv->mac = of_get_mac_address(pdev->dev.of_node);
530 +       priv->mac = ltq_get_eth_mac();
531 +       if (!priv->mac)
532 +               priv->mac = of_get_mac_address(pdev->dev.of_node);
533  
534         priv->clk_ppe = clk_get(&pdev->dev, NULL);
535         if (IS_ERR(priv->clk_ppe))
536 --- a/arch/mips/pci/pci-lantiq.c
537 +++ b/arch/mips/pci/pci-lantiq.c
538 @@ -260,4 +260,4 @@ int __init pcibios_init(void)
539         return ret;
540  }
541  
542 -arch_initcall(pcibios_init);
543 +late_initcall(pcibios_init);