1 // SPDX-License-Identifier: ISC
2 /* Initialize Owl Emulation Devices
4 * Copyright (C) 2016 Christian Lamparter <chunkeey@gmail.com>
5 * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
7 * Some devices (like the Cisco Meraki Z1 Cloud Managed Teleworker Gateway)
8 * need to be able to initialize the PCIe wifi device. Normally, this is done
9 * during the early stages as a pci quirk.
10 * However, this isn't possible for devices which have the init code for the
11 * Atheros chip stored on UBI Volume on NAND. Hence, this module can be used to
12 * initialize the chip when the user-space is ready to extract the init code.
14 #include <linux/module.h>
15 #include <linux/version.h>
16 #include <linux/completion.h>
17 #include <linux/etherdevice.h>
18 #include <linux/firmware.h>
19 #include <linux/pci.h>
20 #include <linux/delay.h>
21 #include <linux/platform_device.h>
22 #include <linux/ath9k_platform.h>
25 struct completion eeprom_load;
28 #define EEPROM_FILENAME_LEN 100
30 #define AR5416_EEPROM_MAGIC 0xa55a
32 static int ath9k_pci_fixup(struct pci_dev *pdev, const u16 *cal_data,
36 const void *cal_end = (void *)cal_data + cal_len;
44 bool swap_needed = false;
46 if (*cal_data != AR5416_EEPROM_MAGIC) {
47 if (*cal_data != swab16(AR5416_EEPROM_MAGIC)) {
48 dev_err(&pdev->dev, "invalid calibration data\n");
52 dev_dbg(&pdev->dev, "calibration data needs swapping\n");
56 dev_info(&pdev->dev, "fixup device configuration\n");
58 mem = pcim_iomap(pdev, 0, 0);
60 dev_err(&pdev->dev, "ioremap error\n");
64 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &bar0);
65 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0,
66 pci_resource_start(pdev, 0));
67 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
68 cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
69 pci_write_config_word(pdev, PCI_COMMAND, cmd);
71 /* set pointer to first reg address */
72 for (data = (const void *)(cal_data + 3);
73 (const void *)data <= cal_end && data->reg != (u16)~0;
80 val |= ((u32)data->high_val) << 16;
87 iowrite32(val, mem + reg);
88 usleep_range(100, 120);
91 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
92 cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
93 pci_write_config_word(pdev, PCI_COMMAND, cmd);
95 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, bar0);
96 pcim_iounmap(pdev, mem);
98 pci_disable_device(pdev);
103 static void owl_fw_cb(const struct firmware *fw, void *context)
105 struct pci_dev *pdev = (struct pci_dev *)context;
106 struct owl_ctx *ctx = (struct owl_ctx *)pci_get_drvdata(pdev);
109 complete(&ctx->eeprom_load);
112 dev_err(&pdev->dev, "no eeprom data received.\n");
116 /* also note that we are doing *u16 operations on the file */
117 if (fw->size > 4096 || fw->size < 0x200 || (fw->size & 1) == 1) {
118 dev_err(&pdev->dev, "eeprom file has an invalid size.\n");
122 if (ath9k_pci_fixup(pdev, (const u16 *)fw->data, fw->size))
125 pci_lock_rescan_remove();
127 pci_stop_and_remove_bus_device(pdev);
128 /* the device should come back with the proper
129 * ProductId. But we have to initiate a rescan.
132 pci_unlock_rescan_remove();
135 release_firmware(fw);
138 static const char *owl_get_eeprom_name(struct pci_dev *pdev)
140 struct device *dev = &pdev->dev;
143 dev_dbg(dev, "using auto-generated eeprom filename\n");
145 eeprom_name = devm_kzalloc(dev, EEPROM_FILENAME_LEN, GFP_KERNEL);
149 /* this should match the pattern used in ath9k/init.c */
150 scnprintf(eeprom_name, EEPROM_FILENAME_LEN, "ath9k-eeprom-pci-%s.bin",
156 static int owl_probe(struct pci_dev *pdev,
157 const struct pci_device_id *id)
160 const char *eeprom_name;
163 if (pcim_enable_device(pdev))
166 pcim_pin_device(pdev);
168 eeprom_name = owl_get_eeprom_name(pdev);
170 dev_err(&pdev->dev, "no eeprom filename found.\n");
174 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
178 init_completion(&ctx->eeprom_load);
180 pci_set_drvdata(pdev, ctx);
181 err = request_firmware_nowait(THIS_MODULE, true, eeprom_name,
182 &pdev->dev, GFP_KERNEL, pdev, owl_fw_cb);
184 dev_err(&pdev->dev, "failed to request caldata (%d).\n", err);
189 static void owl_remove(struct pci_dev *pdev)
191 struct owl_ctx *ctx = pci_get_drvdata(pdev);
194 wait_for_completion(&ctx->eeprom_load);
195 pci_set_drvdata(pdev, NULL);
199 static const struct pci_device_id owl_pci_table[] = {
200 { PCI_VDEVICE(ATHEROS, 0xff1c) }, /* PCIe */
201 { PCI_VDEVICE(ATHEROS, 0xff1d) }, /* PCI */
204 MODULE_DEVICE_TABLE(pci, owl_pci_table);
206 static struct pci_driver owl_driver = {
207 .name = KBUILD_MODNAME,
208 .id_table = owl_pci_table,
210 .remove = owl_remove,
212 module_pci_driver(owl_driver);
213 MODULE_AUTHOR("Christian Lamparter <chunkeey@gmail.com>");
214 MODULE_DESCRIPTION("External EEPROM data loader for Atheros AR500X to AR92XX");
215 MODULE_LICENSE("Dual BSD/GPL");