bmips: add ATH9K PCI fixups
[openwrt/openwrt.git] / target / linux / bmips / files / arch / mips / bmips / ath9k-fixup.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ATH9K Fixup Driver
4 *
5 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
6 * Copyright (C) 2014 Jonas Gorski <jonas.gorski@gmail.com>
7 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
8 * Copyright (C) 2008 Florian Fainelli <f.fainelli@gmail.com>
9 */
10
11 #include <linux/delay.h>
12 #include <linux/etherdevice.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/of_net.h>
17 #include <linux/of_platform.h>
18 #include <linux/pci.h>
19 #include <linux/types.h>
20 #include <linux/ath9k_platform.h>
21
22 #define ATH9K_MAX_FIXUPS 2
23
24 #define ATH9K_DEF_LED_PIN -1
25 #define ATH9K_DEF_PCI_DEV 255
26
27 struct ath9k_fixup {
28 struct device *dev;
29 struct resource *mem_res;
30 u32 pci_dev;
31 u8 mac[ETH_ALEN];
32 struct ath9k_platform_data pdata;
33 };
34
35 static int ath9k_num_fixups;
36 static struct ath9k_fixup *ath9k_fixups[ATH9K_MAX_FIXUPS];
37
38 static void ath9k_pci_fixup(struct pci_dev *dev)
39 {
40 void __iomem *mem;
41 struct ath9k_fixup *priv = NULL;
42 struct ath9k_platform_data *pdata = NULL;
43 struct pci_dev *bridge = pci_upstream_bridge(dev);
44 u16 *cal_data = NULL;
45 u16 cmd;
46 u32 bar0;
47 u32 val;
48 unsigned i;
49
50 for (i = 0; i < ath9k_num_fixups; i++) {
51 if (ath9k_fixups[i]->pci_dev != PCI_SLOT(dev->devfn))
52 continue;
53
54 priv = ath9k_fixups[i];
55 cal_data = priv->pdata.eeprom_data;
56 pdata = &priv->pdata;
57 break;
58 }
59
60 if (cal_data == NULL)
61 return;
62
63 if (*cal_data != 0xa55a) {
64 pr_err("pci %s: invalid calibration data\n", pci_name(dev));
65 return;
66 }
67
68 pr_info("pci %s: fixup device configuration\n", pci_name(dev));
69
70 val = priv->mem_res->start;
71 mem = ioremap(priv->mem_res->start, resource_size(priv->mem_res));
72 if (!mem) {
73 pr_err("pci %s: ioremap error\n", pci_name(dev));
74 return;
75 }
76
77 if (bridge)
78 pci_enable_device(bridge);
79
80 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &bar0);
81 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &bar0);
82 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, val);
83
84 pci_read_config_word(dev, PCI_COMMAND, &cmd);
85 cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
86 pci_write_config_word(dev, PCI_COMMAND, cmd);
87
88 /* set offset to first reg address */
89 cal_data += 3;
90 while(*cal_data != 0xffff) {
91 u32 reg;
92
93 reg = *cal_data++;
94 val = *cal_data++;
95 val |= (*cal_data++) << 16;
96
97 writel(val, mem + reg);
98 udelay(100);
99 }
100
101 pci_read_config_dword(dev, PCI_VENDOR_ID, &val);
102 dev->vendor = val & 0xffff;
103 dev->device = (val >> 16) & 0xffff;
104
105 pci_read_config_dword(dev, PCI_CLASS_REVISION, &val);
106 dev->revision = val & 0xff;
107 dev->class = val >> 8; /* upper 3 bytes */
108
109 pci_read_config_word(dev, PCI_COMMAND, &cmd);
110 cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
111 pci_write_config_word(dev, PCI_COMMAND, cmd);
112
113 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
114
115 if (bridge)
116 pci_disable_device(bridge);
117
118 iounmap(mem);
119
120 dev->dev.platform_data = pdata;
121 }
122 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATHEROS, PCI_ANY_ID, ath9k_pci_fixup);
123
124 static int ath9k_mtd_eeprom(struct ath9k_fixup *priv)
125 {
126 struct device *dev = priv->dev;
127 struct device_node *node = dev->of_node;
128 struct device_node *mtd_np = NULL;
129 struct mtd_info *the_mtd;
130 phandle phandle;
131 size_t eeprom_readlen;
132 const char *part;
133 const __be32 *list;
134 int ret, i;
135
136 list = of_get_property(node, "ath,eeprom", &i);
137 if (!list || (i != (2 * sizeof(*list))))
138 return -ENODEV;
139
140 phandle = be32_to_cpup(list++);
141 if (phandle)
142 mtd_np = of_find_node_by_phandle(phandle);
143 if (!mtd_np)
144 return -ENODEV;
145
146 part = of_get_property(mtd_np, "label", NULL);
147 if (!part)
148 part = mtd_np->name;
149
150 the_mtd = get_mtd_device_nm(part);
151 if (IS_ERR(the_mtd))
152 return -ENODEV;
153
154 ret = mtd_read(the_mtd, be32_to_cpup(list),
155 ATH9K_PLAT_EEP_MAX_WORDS * sizeof(u16),
156 &eeprom_readlen, (void *) priv->pdata.eeprom_data);
157 put_mtd_device(the_mtd);
158 if (ret)
159 return ret;
160
161 return 0;
162 }
163
164 static int ath9k_fixup_probe(struct platform_device *pdev)
165 {
166 struct device *dev = &pdev->dev;
167 struct device_node *node = dev->of_node;
168 struct ath9k_fixup *priv;
169 struct resource *res;
170 const void *mac;
171 int ret;
172
173 if (ath9k_num_fixups >= ATH9K_MAX_FIXUPS)
174 return -ENOMEM;
175
176 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
177 if (!res)
178 return -EINVAL;
179
180 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
181 if (!priv)
182 return -ENOMEM;
183
184 priv->dev = dev;
185 priv->mem_res = res;
186
187 ret = of_property_read_u32(node, "pci-dev", &priv->pci_dev);
188 if (ret)
189 priv->pci_dev = ATH9K_DEF_PCI_DEV;
190
191 ret = ath9k_mtd_eeprom(priv);
192 if (ret)
193 return ret;
194
195 priv->pdata.endian_check = of_property_read_bool(node,
196 "ath,endian-check");
197 ret = of_property_read_s32(node, "ath,led-pin", &priv->pdata.led_pin);
198 if (ret)
199 priv->pdata.led_pin = ATH9K_DEF_LED_PIN;
200 priv->pdata.led_active_high = of_property_read_bool(node,
201 "ath,led-active-high");
202
203 mac = of_get_mac_address(node);
204 if (!IS_ERR_OR_NULL(mac)) {
205 memcpy(priv->mac, mac, ETH_ALEN);
206 dev_info(dev, "mtd mac %pM\n", priv->mac);
207 } else {
208 random_ether_addr(priv->mac);
209 dev_info(dev, "random mac %pM\n", priv->mac);
210 }
211
212 priv->pdata.macaddr = priv->mac;
213
214 ath9k_fixups[ath9k_num_fixups] = priv;
215 ath9k_num_fixups++;
216
217 return 0;
218 }
219
220 static const struct of_device_id ath9k_fixup_of_match[] = {
221 { .compatible = "brcm,ath9k-fixup" },
222 { /* sentinel */ }
223 };
224
225 static struct platform_driver ath9k_fixup_driver = {
226 .probe = ath9k_fixup_probe,
227 .driver = {
228 .name = "ath9k-fixup",
229 .of_match_table = ath9k_fixup_of_match,
230 },
231 };
232
233 int __init ath9k_fixup_init(void)
234 {
235 int ret = platform_driver_register(&ath9k_fixup_driver);
236 if (ret)
237 pr_err("ath9k_fixup: Error registering platform driver!\n");
238 return ret;
239 }
240 late_initcall(ath9k_fixup_init);