root/drivers/net/wireless/ath/ath9k/ath9k_pci_owl_loader.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ath9k_pci_fixup
  2. owl_fw_cb
  3. owl_get_eeprom_name
  4. owl_probe
  5. owl_remove

   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/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>
  23 
  24 struct owl_ctx {
  25         struct completion eeprom_load;
  26 };
  27 
  28 #define EEPROM_FILENAME_LEN 100
  29 
  30 #define AR5416_EEPROM_MAGIC 0xa55a
  31 
  32 static int ath9k_pci_fixup(struct pci_dev *pdev, const u16 *cal_data,
  33                            size_t cal_len)
  34 {
  35         void __iomem *mem;
  36         const void *cal_end = (void *)cal_data + cal_len;
  37         const struct {
  38                 u16 reg;
  39                 u16 low_val;
  40                 u16 high_val;
  41         } __packed * data;
  42         u16 cmd;
  43         u32 bar0;
  44         bool swap_needed = false;
  45 
  46         if (*cal_data != AR5416_EEPROM_MAGIC) {
  47                 if (*cal_data != swab16(AR5416_EEPROM_MAGIC)) {
  48                         dev_err(&pdev->dev, "invalid calibration data\n");
  49                         return -EINVAL;
  50                 }
  51 
  52                 dev_dbg(&pdev->dev, "calibration data needs swapping\n");
  53                 swap_needed = true;
  54         }
  55 
  56         dev_info(&pdev->dev, "fixup device configuration\n");
  57 
  58         mem = pcim_iomap(pdev, 0, 0);
  59         if (!mem) {
  60                 dev_err(&pdev->dev, "ioremap error\n");
  61                 return -EINVAL;
  62         }
  63 
  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);
  70 
  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;
  74              data++) {
  75                 u32 val;
  76                 u16 reg;
  77 
  78                 reg = data->reg;
  79                 val = data->low_val;
  80                 val |= ((u32)data->high_val) << 16;
  81 
  82                 if (swap_needed) {
  83                         reg = swab16(reg);
  84                         val = swahb32(val);
  85                 }
  86 
  87                 iowrite32(val, mem + reg);
  88                 usleep_range(100, 120);
  89         }
  90 
  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);
  94 
  95         pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, bar0);
  96         pcim_iounmap(pdev, mem);
  97 
  98         pci_disable_device(pdev);
  99 
 100         return 0;
 101 }
 102 
 103 static void owl_fw_cb(const struct firmware *fw, void *context)
 104 {
 105         struct pci_dev *pdev = (struct pci_dev *)context;
 106         struct owl_ctx *ctx = (struct owl_ctx *)pci_get_drvdata(pdev);
 107         struct pci_bus *bus;
 108 
 109         complete(&ctx->eeprom_load);
 110 
 111         if (!fw) {
 112                 dev_err(&pdev->dev, "no eeprom data received.\n");
 113                 goto release;
 114         }
 115 
 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");
 119                 goto release;
 120         }
 121 
 122         if (ath9k_pci_fixup(pdev, (const u16 *)fw->data, fw->size))
 123                 goto release;
 124 
 125         pci_lock_rescan_remove();
 126         bus = pdev->bus;
 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.
 130          */
 131         pci_rescan_bus(bus);
 132         pci_unlock_rescan_remove();
 133 
 134 release:
 135         release_firmware(fw);
 136 }
 137 
 138 static const char *owl_get_eeprom_name(struct pci_dev *pdev)
 139 {
 140         struct device *dev = &pdev->dev;
 141         char *eeprom_name;
 142 
 143         dev_dbg(dev, "using auto-generated eeprom filename\n");
 144 
 145         eeprom_name = devm_kzalloc(dev, EEPROM_FILENAME_LEN, GFP_KERNEL);
 146         if (!eeprom_name)
 147                 return NULL;
 148 
 149         /* this should match the pattern used in ath9k/init.c */
 150         scnprintf(eeprom_name, EEPROM_FILENAME_LEN, "ath9k-eeprom-pci-%s.bin",
 151                   dev_name(dev));
 152 
 153         return eeprom_name;
 154 }
 155 
 156 static int owl_probe(struct pci_dev *pdev,
 157                      const struct pci_device_id *id)
 158 {
 159         struct owl_ctx *ctx;
 160         const char *eeprom_name;
 161         int err = 0;
 162 
 163         if (pcim_enable_device(pdev))
 164                 return -EIO;
 165 
 166         pcim_pin_device(pdev);
 167 
 168         eeprom_name = owl_get_eeprom_name(pdev);
 169         if (!eeprom_name) {
 170                 dev_err(&pdev->dev, "no eeprom filename found.\n");
 171                 return -ENODEV;
 172         }
 173 
 174         ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
 175         if (!ctx)
 176                 return -ENOMEM;
 177 
 178         init_completion(&ctx->eeprom_load);
 179 
 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);
 183         if (err)
 184                 dev_err(&pdev->dev, "failed to request caldata (%d).\n", err);
 185 
 186         return err;
 187 }
 188 
 189 static void owl_remove(struct pci_dev *pdev)
 190 {
 191         struct owl_ctx *ctx = pci_get_drvdata(pdev);
 192 
 193         if (ctx) {
 194                 wait_for_completion(&ctx->eeprom_load);
 195                 pci_set_drvdata(pdev, NULL);
 196         }
 197 }
 198 
 199 static const struct pci_device_id owl_pci_table[] = {
 200         { PCI_VDEVICE(ATHEROS, 0xff1c) }, /* PCIe */
 201         { PCI_VDEVICE(ATHEROS, 0xff1d) }, /* PCI */
 202         { },
 203 };
 204 MODULE_DEVICE_TABLE(pci, owl_pci_table);
 205 
 206 static struct pci_driver owl_driver = {
 207         .name           = KBUILD_MODNAME,
 208         .id_table       = owl_pci_table,
 209         .probe          = owl_probe,
 210         .remove         = owl_remove,
 211 };
 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");

/* [<][>][^][v][top][bottom][index][help] */