0035-owrt-lantiq-wifi-and-ethernet-eeprom-handling.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. ---
  7. .../mips/include/asm/mach-lantiq/xway/lantiq_soc.h | 3 +
  8. arch/mips/lantiq/xway/Makefile | 3 +
  9. arch/mips/lantiq/xway/ath5k_eep.c | 136 +++++++++++++++++++++
  10. arch/mips/lantiq/xway/eth_mac.c | 25 ++++
  11. drivers/net/ethernet/lantiq_etop.c | 6 +-
  12. 5 files changed, 172 insertions(+), 1 deletion(-)
  13. create mode 100644 arch/mips/lantiq/xway/ath5k_eep.c
  14. create mode 100644 arch/mips/lantiq/xway/eth_mac.c
  15. --- a/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
  16. +++ b/arch/mips/include/asm/mach-lantiq/xway/lantiq_soc.h
  17. @@ -104,5 +104,8 @@ int xrx200_gphy_boot(struct device *dev,
  18. extern void ltq_pmu_enable(unsigned int module);
  19. extern void ltq_pmu_disable(unsigned int module);
  20. +/* allow the ethernet driver to load a flash mapped mac addr */
  21. +const u8* ltq_get_eth_mac(void);
  22. +
  23. #endif /* CONFIG_SOC_TYPE_XWAY */
  24. #endif /* _LTQ_XWAY_H__ */
  25. --- a/arch/mips/lantiq/xway/Makefile
  26. +++ b/arch/mips/lantiq/xway/Makefile
  27. @@ -2,4 +2,7 @@ obj-y := prom.o sysctrl.o clk.o reset.o
  28. obj-y += vmmc.o tffs.o
  29. +obj-y += eth_mac.o
  30. +obj-$(CONFIG_PCI) += ath5k_eep.o
  31. +
  32. obj-$(CONFIG_XRX200_PHY_FW) += xrx200_phy_fw.o
  33. --- /dev/null
  34. +++ b/arch/mips/lantiq/xway/ath5k_eep.c
  35. @@ -0,0 +1,136 @@
  36. +/*
  37. + * Copyright (C) 2011 Luca Olivetti <luca@ventoso.org>
  38. + * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
  39. + * Copyright (C) 2011 Andrej Vlašić <andrej.vlasic0@gmail.com>
  40. + * Copyright (C) 2013 Álvaro Fernández Rojas <noltari@gmail.com>
  41. + * Copyright (C) 2013 Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
  42. + * Copyright (C) 2015 Vittorio Gambaletta <openwrt@vittgam.net>
  43. + *
  44. + * This program is free software; you can redistribute it and/or modify it
  45. + * under the terms of the GNU General Public License version 2 as published
  46. + * by the Free Software Foundation.
  47. + */
  48. +
  49. +#include <linux/init.h>
  50. +#include <linux/platform_device.h>
  51. +#include <linux/etherdevice.h>
  52. +#include <linux/ath5k_platform.h>
  53. +#include <linux/pci.h>
  54. +#include <linux/err.h>
  55. +#include <linux/mtd/mtd.h>
  56. +#include <lantiq_soc.h>
  57. +
  58. +extern int (*ltq_pci_plat_dev_init)(struct pci_dev *dev);
  59. +struct ath5k_platform_data ath5k_pdata;
  60. +static u8 athxk_eeprom_mac[6];
  61. +
  62. +static int ath5k_pci_plat_dev_init(struct pci_dev *dev)
  63. +{
  64. + dev->dev.platform_data = &ath5k_pdata;
  65. + return 0;
  66. +}
  67. +
  68. +static int ath5k_eep_load;
  69. +int __init of_ath5k_eeprom_probe(struct platform_device *pdev)
  70. +{
  71. + struct device_node *np = pdev->dev.of_node, *mtd_np = NULL;
  72. + int mac_offset;
  73. + u32 mac_inc = 0;
  74. + int i;
  75. + struct mtd_info *the_mtd;
  76. + size_t flash_readlen;
  77. + const __be32 *list;
  78. + const char *part;
  79. + phandle phandle;
  80. +
  81. + list = of_get_property(np, "ath,eep-flash", &i);
  82. + if (!list || (i != (2 * sizeof(*list))))
  83. + return -ENODEV;
  84. +
  85. + phandle = be32_to_cpup(list++);
  86. + if (phandle)
  87. + mtd_np = of_find_node_by_phandle(phandle);
  88. +
  89. + if (!mtd_np)
  90. + return -ENODEV;
  91. +
  92. + part = of_get_property(mtd_np, "label", NULL);
  93. + if (!part)
  94. + part = mtd_np->name;
  95. +
  96. + the_mtd = get_mtd_device_nm(part);
  97. + if (IS_ERR(the_mtd))
  98. + return -ENODEV;
  99. +
  100. + ath5k_pdata.eeprom_data = kmalloc(ATH5K_PLAT_EEP_MAX_WORDS<<1, GFP_KERNEL);
  101. +
  102. + i = mtd_read(the_mtd, be32_to_cpup(list), ATH5K_PLAT_EEP_MAX_WORDS << 1,
  103. + &flash_readlen, (void *) ath5k_pdata.eeprom_data);
  104. +
  105. + if (!of_property_read_u32(np, "ath,mac-offset", &mac_offset)) {
  106. + size_t mac_readlen;
  107. + mtd_read(the_mtd, mac_offset, 6, &mac_readlen,
  108. + (void *) athxk_eeprom_mac);
  109. + }
  110. + put_mtd_device(the_mtd);
  111. +
  112. + if (((ATH5K_PLAT_EEP_MAX_WORDS<<1) != flash_readlen) || i) {
  113. + dev_err(&pdev->dev, "failed to load eeprom from mtd\n");
  114. + return -ENODEV;
  115. + }
  116. +
  117. + if (of_find_property(np, "ath,eep-swap", NULL))
  118. + for (i = 0; i < ATH5K_PLAT_EEP_MAX_WORDS; i++)
  119. + ath5k_pdata.eeprom_data[i] = swab16(ath5k_pdata.eeprom_data[i]);
  120. +
  121. + if (!is_valid_ether_addr(athxk_eeprom_mac) && ltq_get_eth_mac())
  122. + ether_addr_copy(athxk_eeprom_mac, ltq_get_eth_mac());
  123. +
  124. + if (!is_valid_ether_addr(athxk_eeprom_mac)) {
  125. + dev_warn(&pdev->dev, "using random mac\n");
  126. + random_ether_addr(athxk_eeprom_mac);
  127. + }
  128. +
  129. + if (!of_property_read_u32(np, "ath,mac-increment", &mac_inc))
  130. + athxk_eeprom_mac[5] += mac_inc;
  131. +
  132. + ath5k_pdata.macaddr = athxk_eeprom_mac;
  133. + ltq_pci_plat_dev_init = ath5k_pci_plat_dev_init;
  134. +
  135. + dev_info(&pdev->dev, "loaded ath5k eeprom\n");
  136. +
  137. + return 0;
  138. +}
  139. +
  140. +static struct of_device_id ath5k_eeprom_ids[] = {
  141. + { .compatible = "ath5k,eeprom" },
  142. + { }
  143. +};
  144. +
  145. +static struct platform_driver ath5k_eeprom_driver = {
  146. + .driver = {
  147. + .name = "ath5k,eeprom",
  148. + .owner = THIS_MODULE,
  149. + .of_match_table = of_match_ptr(ath5k_eeprom_ids),
  150. + },
  151. +};
  152. +
  153. +static int __init of_ath5k_eeprom_init(void)
  154. +{
  155. + int ret = platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
  156. +
  157. + if (ret)
  158. + ath5k_eep_load = 1;
  159. +
  160. + return ret;
  161. +}
  162. +
  163. +static int __init of_ath5k_eeprom_init_late(void)
  164. +{
  165. + if (!ath5k_eep_load)
  166. + return 0;
  167. +
  168. + return platform_driver_probe(&ath5k_eeprom_driver, of_ath5k_eeprom_probe);
  169. +}
  170. +late_initcall(of_ath5k_eeprom_init_late);
  171. +subsys_initcall(of_ath5k_eeprom_init);
  172. --- /dev/null
  173. +++ b/arch/mips/lantiq/xway/eth_mac.c
  174. @@ -0,0 +1,25 @@
  175. +/*
  176. + * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
  177. + *
  178. + * This program is free software; you can redistribute it and/or modify it
  179. + * under the terms of the GNU General Public License version 2 as published
  180. + * by the Free Software Foundation.
  181. + */
  182. +
  183. +#include <linux/init.h>
  184. +#include <linux/if_ether.h>
  185. +
  186. +static u8 eth_mac[6];
  187. +static int eth_mac_set;
  188. +
  189. +const u8* ltq_get_eth_mac(void)
  190. +{
  191. + return eth_mac;
  192. +}
  193. +
  194. +static int __init setup_ethaddr(char *str)
  195. +{
  196. + eth_mac_set = mac_pton(str, eth_mac);
  197. + return !eth_mac_set;
  198. +}
  199. +early_param("ethaddr", setup_ethaddr);
  200. --- a/drivers/net/ethernet/lantiq_etop.c
  201. +++ b/drivers/net/ethernet/lantiq_etop.c
  202. @@ -840,7 +840,11 @@ ltq_etop_init(struct net_device *dev)
  203. if (err)
  204. goto err_hw;
  205. - memcpy(&mac, &priv->pldata->mac, sizeof(struct sockaddr));
  206. + memcpy(&mac.sa_data, ltq_get_eth_mac(), ETH_ALEN);
  207. +
  208. + if (priv->mac && !is_valid_ether_addr(mac.sa_data))
  209. + memcpy(&mac.sa_data, priv->mac, ETH_ALEN);
  210. +
  211. if (!is_valid_ether_addr(mac.sa_data)) {
  212. pr_warn("etop: invalid MAC, using random\n");
  213. eth_random_addr(mac.sa_data);