1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
22 #define ATH9K_MAX_FIXUPS 2
24 #define ATH9K_DEF_LED_PIN -1
25 #define ATH9K_DEF_PCI_DEV 255
29 struct resource
*mem_res
;
32 struct ath9k_platform_data pdata
;
35 static int ath9k_num_fixups
;
36 static struct ath9k_fixup
*ath9k_fixups
[ATH9K_MAX_FIXUPS
];
38 static void ath9k_pci_fixup(struct pci_dev
*dev
)
41 struct ath9k_fixup
*priv
= NULL
;
42 struct ath9k_platform_data
*pdata
= NULL
;
43 struct pci_dev
*bridge
= pci_upstream_bridge(dev
);
50 for (i
= 0; i
< ath9k_num_fixups
; i
++) {
51 if (ath9k_fixups
[i
]->pci_dev
!= PCI_SLOT(dev
->devfn
))
54 priv
= ath9k_fixups
[i
];
55 cal_data
= priv
->pdata
.eeprom_data
;
63 if (*cal_data
!= 0xa55a) {
64 pr_err("pci %s: invalid calibration data\n", pci_name(dev
));
68 pr_info("pci %s: fixup device configuration\n", pci_name(dev
));
70 val
= priv
->mem_res
->start
;
71 mem
= ioremap(priv
->mem_res
->start
, resource_size(priv
->mem_res
));
73 pr_err("pci %s: ioremap error\n", pci_name(dev
));
78 pci_enable_device(bridge
);
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
);
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
);
88 /* set offset to first reg address */
90 while(*cal_data
!= 0xffff) {
95 val
|= (*cal_data
++) << 16;
97 writel(val
, mem
+ reg
);
101 pci_read_config_dword(dev
, PCI_VENDOR_ID
, &val
);
102 dev
->vendor
= val
& 0xffff;
103 dev
->device
= (val
>> 16) & 0xffff;
105 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &val
);
106 dev
->revision
= val
& 0xff;
107 dev
->class = val
>> 8; /* upper 3 bytes */
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
);
113 pci_write_config_dword(dev
, PCI_BASE_ADDRESS_0
, bar0
);
116 pci_disable_device(bridge
);
120 dev
->dev
.platform_data
= pdata
;
122 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATHEROS
, PCI_ANY_ID
, ath9k_pci_fixup
);
124 static int ath9k_mtd_eeprom(struct ath9k_fixup
*priv
)
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
;
131 size_t eeprom_readlen
;
136 list
= of_get_property(node
, "ath,eeprom", &i
);
137 if (!list
|| (i
!= (2 * sizeof(*list
))))
140 phandle
= be32_to_cpup(list
++);
142 mtd_np
= of_find_node_by_phandle(phandle
);
146 part
= of_get_property(mtd_np
, "label", NULL
);
150 the_mtd
= get_mtd_device_nm(part
);
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
);
164 static int ath9k_fixup_probe(struct platform_device
*pdev
)
166 struct device
*dev
= &pdev
->dev
;
167 struct device_node
*node
= dev
->of_node
;
168 struct ath9k_fixup
*priv
;
169 struct resource
*res
;
172 if (ath9k_num_fixups
>= ATH9K_MAX_FIXUPS
)
175 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
179 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
186 ret
= of_property_read_u32(node
, "pci-dev", &priv
->pci_dev
);
188 priv
->pci_dev
= ATH9K_DEF_PCI_DEV
;
190 ret
= ath9k_mtd_eeprom(priv
);
194 priv
->pdata
.endian_check
= of_property_read_bool(node
,
196 ret
= of_property_read_s32(node
, "ath,led-pin", &priv
->pdata
.led_pin
);
198 priv
->pdata
.led_pin
= ATH9K_DEF_LED_PIN
;
199 priv
->pdata
.led_active_high
= of_property_read_bool(node
,
200 "ath,led-active-high");
202 of_get_mac_address(node
, priv
->mac
);
203 if (is_valid_ether_addr(priv
->mac
)) {
204 dev_info(dev
, "mtd mac %pM\n", priv
->mac
);
206 random_ether_addr(priv
->mac
);
207 dev_info(dev
, "random mac %pM\n", priv
->mac
);
210 priv
->pdata
.macaddr
= priv
->mac
;
212 ath9k_fixups
[ath9k_num_fixups
] = priv
;
218 static const struct of_device_id ath9k_fixup_of_match
[] = {
219 { .compatible
= "brcm,ath9k-fixup" },
223 static struct platform_driver ath9k_fixup_driver
= {
224 .probe
= ath9k_fixup_probe
,
226 .name
= "ath9k-fixup",
227 .of_match_table
= ath9k_fixup_of_match
,
231 int __init
ath9k_fixup_init(void)
233 int ret
= platform_driver_register(&ath9k_fixup_driver
);
235 pr_err("ath9k_fixup: Error registering platform driver!\n");
238 late_initcall(ath9k_fixup_init
);