1 // SPDX-License-Identifier: ISC
2 /* Initialize Owl Emulation Devices
3 *
4 * Copyright (C) 2016 Christian Lamparter <chunkeey@gmail.com>
5 * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6 *
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.
13 */
14 #include <linux/module.h>
15 #include <linux/completion.h>
16 #include <linux/etherdevice.h>
17 #include <linux/firmware.h>
18 #include <linux/pci.h>
19 #include <linux/delay.h>
20 #include <linux/platform_device.h>
21 #include <linux/nvmem-consumer.h>
22 #include <linux/workqueue.h>
23
24 struct owl_ctx {
25 struct pci_dev *pdev;
26 struct completion eeprom_load;
27 struct work_struct work;
28 struct nvmem_cell *cell;
29 };
30
31 #define EEPROM_FILENAME_LEN 100
32
33 #define AR5416_EEPROM_MAGIC 0xa55a
34
ath9k_pci_fixup(struct pci_dev * pdev,const u16 * cal_data,size_t cal_len)35 static int ath9k_pci_fixup(struct pci_dev *pdev, const u16 *cal_data,
36 size_t cal_len)
37 {
38 void __iomem *mem;
39 const void *cal_end = (void *)cal_data + cal_len;
40 const struct {
41 u16 reg;
42 u16 low_val;
43 u16 high_val;
44 } __packed * data;
45 u16 cmd;
46 u32 bar0;
47 bool swap_needed = false;
48
49 /* also note that we are doing *u16 operations on the file */
50 if (cal_len > 4096 || cal_len < 0x200 || (cal_len & 1) == 1) {
51 dev_err(&pdev->dev, "eeprom has an invalid size.\n");
52 return -EINVAL;
53 }
54
55 if (*cal_data != AR5416_EEPROM_MAGIC) {
56 if (*cal_data != swab16(AR5416_EEPROM_MAGIC)) {
57 dev_err(&pdev->dev, "invalid calibration data\n");
58 return -EINVAL;
59 }
60
61 dev_dbg(&pdev->dev, "calibration data needs swapping\n");
62 swap_needed = true;
63 }
64
65 dev_info(&pdev->dev, "fixup device configuration\n");
66
67 mem = pci_iomap(pdev, 0, 0);
68 if (!mem) {
69 dev_err(&pdev->dev, "ioremap error\n");
70 return -EINVAL;
71 }
72
73 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &bar0);
74 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0,
75 pci_resource_start(pdev, 0));
76 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
77 cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
78 pci_write_config_word(pdev, PCI_COMMAND, cmd);
79
80 /* set pointer to first reg address */
81 for (data = (const void *)(cal_data + 3);
82 (const void *)data <= cal_end && data->reg != (u16)~0;
83 data++) {
84 u32 val;
85 u16 reg;
86
87 reg = data->reg;
88 val = data->low_val;
89 val |= ((u32)data->high_val) << 16;
90
91 if (swap_needed) {
92 reg = swab16(reg);
93 val = swahb32(val);
94 }
95
96 iowrite32(val, mem + reg);
97 usleep_range(100, 120);
98 }
99
100 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
101 cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
102 pci_write_config_word(pdev, PCI_COMMAND, cmd);
103
104 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, bar0);
105 pci_iounmap(pdev, mem);
106
107 pci_disable_device(pdev);
108
109 return 0;
110 }
111
owl_rescan(struct pci_dev * pdev)112 static void owl_rescan(struct pci_dev *pdev)
113 {
114 struct pci_bus *bus = pdev->bus;
115
116 pci_lock_rescan_remove();
117 pci_stop_and_remove_bus_device(pdev);
118 /* the device should come back with the proper
119 * ProductId. But we have to initiate a rescan.
120 */
121 pci_rescan_bus(bus);
122 pci_unlock_rescan_remove();
123 }
124
owl_fw_cb(const struct firmware * fw,void * context)125 static void owl_fw_cb(const struct firmware *fw, void *context)
126 {
127 struct owl_ctx *ctx = context;
128
129 complete(&ctx->eeprom_load);
130
131 if (fw) {
132 ath9k_pci_fixup(ctx->pdev, (const u16 *)fw->data, fw->size);
133 owl_rescan(ctx->pdev);
134 } else {
135 dev_err(&ctx->pdev->dev, "no eeprom data received.\n");
136 }
137 release_firmware(fw);
138 }
139
owl_get_eeprom_name(struct pci_dev * pdev)140 static const char *owl_get_eeprom_name(struct pci_dev *pdev)
141 {
142 struct device *dev = &pdev->dev;
143 char *eeprom_name;
144
145 dev_dbg(dev, "using auto-generated eeprom filename\n");
146
147 eeprom_name = devm_kzalloc(dev, EEPROM_FILENAME_LEN, GFP_KERNEL);
148 if (!eeprom_name)
149 return NULL;
150
151 /* this should match the pattern used in ath9k/init.c */
152 scnprintf(eeprom_name, EEPROM_FILENAME_LEN, "ath9k-eeprom-pci-%s.bin",
153 dev_name(dev));
154
155 return eeprom_name;
156 }
157
owl_nvmem_work(struct work_struct * work)158 static void owl_nvmem_work(struct work_struct *work)
159 {
160 struct owl_ctx *ctx = container_of(work, struct owl_ctx, work);
161 void *buf;
162 size_t len;
163
164 complete(&ctx->eeprom_load);
165
166 buf = nvmem_cell_read(ctx->cell, &len);
167 if (!IS_ERR(buf)) {
168 ath9k_pci_fixup(ctx->pdev, buf, len);
169 kfree(buf);
170 owl_rescan(ctx->pdev);
171 } else {
172 dev_err(&ctx->pdev->dev, "no nvmem data received.\n");
173 }
174 }
175
owl_nvmem_probe(struct owl_ctx * ctx)176 static int owl_nvmem_probe(struct owl_ctx *ctx)
177 {
178 int err;
179
180 ctx->cell = devm_nvmem_cell_get(&ctx->pdev->dev, "calibration");
181 if (IS_ERR(ctx->cell)) {
182 err = PTR_ERR(ctx->cell);
183 if (err == -ENOENT || err == -EOPNOTSUPP)
184 return 1; /* not present, try firmware_request */
185
186 return err;
187 }
188
189 INIT_WORK(&ctx->work, owl_nvmem_work);
190 schedule_work(&ctx->work);
191
192 return 0;
193 }
194
owl_probe(struct pci_dev * pdev,const struct pci_device_id * id)195 static int owl_probe(struct pci_dev *pdev,
196 const struct pci_device_id *id)
197 {
198 struct owl_ctx *ctx;
199 const char *eeprom_name;
200 int err = 0;
201
202 if (pci_enable_device(pdev))
203 return -EIO;
204
205 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
206 if (!ctx)
207 return -ENOMEM;
208
209 init_completion(&ctx->eeprom_load);
210 ctx->pdev = pdev;
211
212 pci_set_drvdata(pdev, ctx);
213
214 err = owl_nvmem_probe(ctx);
215 if (err <= 0)
216 return err;
217
218 eeprom_name = owl_get_eeprom_name(pdev);
219 if (!eeprom_name) {
220 dev_err(&pdev->dev, "no eeprom filename found.\n");
221 return -ENODEV;
222 }
223
224 err = request_firmware_nowait(THIS_MODULE, true, eeprom_name,
225 &pdev->dev, GFP_KERNEL, ctx, owl_fw_cb);
226 if (err)
227 dev_err(&pdev->dev, "failed to request caldata (%d).\n", err);
228
229 return err;
230 }
231
owl_remove(struct pci_dev * pdev)232 static void owl_remove(struct pci_dev *pdev)
233 {
234 struct owl_ctx *ctx = pci_get_drvdata(pdev);
235
236 if (ctx) {
237 wait_for_completion(&ctx->eeprom_load);
238 pci_set_drvdata(pdev, NULL);
239 }
240 }
241
242 static const struct pci_device_id owl_pci_table[] = {
243 { PCI_VDEVICE(ATHEROS, 0xff1c) }, /* PCIe */
244 { PCI_VDEVICE(ATHEROS, 0xff1d) }, /* PCI */
245 { },
246 };
247 MODULE_DEVICE_TABLE(pci, owl_pci_table);
248
249 static struct pci_driver owl_driver = {
250 .name = KBUILD_MODNAME,
251 .id_table = owl_pci_table,
252 .probe = owl_probe,
253 .remove = owl_remove,
254 };
255 module_pci_driver(owl_driver);
256 MODULE_AUTHOR("Christian Lamparter <chunkeey@gmail.com>");
257 MODULE_DESCRIPTION("External EEPROM data loader for Atheros AR500X to AR92XX");
258 MODULE_LICENSE("Dual BSD/GPL");
259