owl-loader.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Initialize Owl Emulation Devices
  3. *
  4. * Copyright (C) 2016 Christian Lamparter <chunkeey@googlemail.com>
  5. * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * Some devices (like the Cisco Meraki Z1 Cloud Managed Teleworker Gateway)
  12. * need to be able to initialize the PCIe wifi device. Normally, this is done
  13. * during the early stages of booting linux, because the necessary init code
  14. * is read from the memory mapped SPI and passed to pci_enable_ath9k_fixup.
  15. * However,this isn't possible for devices which have the init code for the
  16. * Atheros chip stored on NAND. Hence, this module can be used to initialze
  17. * the chip when the user-space is ready to extract the init code.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/version.h>
  21. #include <linux/completion.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/firmware.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/ath9k_platform.h>
  28. struct owl_ctx {
  29. struct completion eeprom_load;
  30. };
  31. #define EEPROM_FILENAME_LEN 100
  32. #define AR5416_EEPROM_MAGIC 0xa55a
  33. static int ath9k_pci_fixup(struct pci_dev *pdev, const u16 *cal_data,
  34. size_t cal_len)
  35. {
  36. void __iomem *mem;
  37. const void *cal_end = (void *)cal_data + cal_len;
  38. const struct {
  39. __be16 reg;
  40. __be16 low_val;
  41. __be16 high_val;
  42. } __packed *data;
  43. u16 cmd;
  44. u32 bar0;
  45. 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");
  49. return -EINVAL;
  50. }
  51. dev_dbg(&pdev->dev, "calibration data needs swapping\n");
  52. swap_needed = true;
  53. }
  54. dev_info(&pdev->dev, "fixup device configuration\n");
  55. mem = pcim_iomap(pdev, 0, 0);
  56. if (!mem) {
  57. dev_err(&pdev->dev, "ioremap error\n");
  58. return -EINVAL;
  59. }
  60. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &bar0);
  61. pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0,
  62. pci_resource_start(pdev, 0));
  63. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  64. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  65. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  66. /* set pointer to first reg address */
  67. for (data = (const void *) (cal_data + 3);
  68. (const void *) data <= cal_end && data->reg != cpu_to_be16(~0);
  69. data++) {
  70. u32 val;
  71. u16 reg;
  72. reg = data->reg;
  73. val = data->low_val;
  74. val |= data->high_val << 16;
  75. if (swap_needed) {
  76. reg = swab16(reg);
  77. val = swahb32(val);
  78. }
  79. #if CONFIG_LANTIQ
  80. val = swab32(val);
  81. #endif
  82. __raw_writel(val, mem + reg);
  83. udelay(100);
  84. }
  85. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  86. cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
  87. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  88. pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, bar0);
  89. pcim_iounmap(pdev, mem);
  90. pci_disable_device(pdev);
  91. return 0;
  92. }
  93. static void owl_fw_cb(const struct firmware *fw, void *context)
  94. {
  95. struct pci_dev *pdev = (struct pci_dev *) context;
  96. struct owl_ctx *ctx = (struct owl_ctx *) pci_get_drvdata(pdev);
  97. struct ath9k_platform_data *pdata = dev_get_platdata(&pdev->dev);
  98. struct pci_bus *bus;
  99. complete(&ctx->eeprom_load);
  100. if (!fw) {
  101. dev_err(&pdev->dev, "no eeprom data received.\n");
  102. goto release;
  103. }
  104. /* also note that we are doing *u16 operations on the file */
  105. if (fw->size > sizeof(pdata->eeprom_data) || fw->size < 0x200 ||
  106. (fw->size & 1) == 1) {
  107. dev_err(&pdev->dev, "eeprom file has an invalid size.\n");
  108. goto release;
  109. }
  110. if (pdata) {
  111. memcpy(pdata->eeprom_data, fw->data, fw->size);
  112. /*
  113. * eeprom has been successfully loaded - pass the data to ath9k
  114. * but remove the eeprom_name, so it doesn't try to load it too.
  115. */
  116. pdata->eeprom_name = NULL;
  117. }
  118. if (ath9k_pci_fixup(pdev, (const u16 *) fw->data, fw->size))
  119. goto release;
  120. pci_lock_rescan_remove();
  121. bus = pdev->bus;
  122. pci_stop_and_remove_bus_device(pdev);
  123. /*
  124. * the device should come back with the proper
  125. * ProductId. But we have to initiate a rescan.
  126. */
  127. pci_rescan_bus(bus);
  128. pci_unlock_rescan_remove();
  129. release:
  130. release_firmware(fw);
  131. }
  132. static const char *owl_get_eeprom_name(struct pci_dev *pdev)
  133. {
  134. struct device *dev = &pdev->dev;
  135. struct ath9k_platform_data *pdata;
  136. char *eeprom_name;
  137. /* try the existing platform data first */
  138. pdata = dev_get_platdata(dev);
  139. if (pdata && pdata->eeprom_name)
  140. return pdata->eeprom_name;
  141. dev_dbg(dev, "using auto-generated eeprom filename\n");
  142. eeprom_name = devm_kzalloc(dev, EEPROM_FILENAME_LEN, GFP_KERNEL);
  143. if (!eeprom_name)
  144. return NULL;
  145. /* this should match the pattern used in ath9k/init.c */
  146. scnprintf(eeprom_name, EEPROM_FILENAME_LEN, "ath9k-eeprom-pci-%s.bin",
  147. dev_name(dev));
  148. return eeprom_name;
  149. }
  150. static int owl_probe(struct pci_dev *pdev,
  151. const struct pci_device_id *id)
  152. {
  153. struct owl_ctx *ctx;
  154. const char *eeprom_name;
  155. int err = 0;
  156. if (pcim_enable_device(pdev))
  157. return -EIO;
  158. pcim_pin_device(pdev);
  159. eeprom_name = owl_get_eeprom_name(pdev);
  160. if (!eeprom_name) {
  161. dev_err(&pdev->dev, "no eeprom filename found.\n");
  162. return -ENODEV;
  163. }
  164. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  165. if (!ctx) {
  166. dev_err(&pdev->dev, "failed to alloc device context.\n");
  167. return -ENOMEM;
  168. }
  169. init_completion(&ctx->eeprom_load);
  170. pci_set_drvdata(pdev, ctx);
  171. err = request_firmware_nowait(THIS_MODULE, true, eeprom_name,
  172. &pdev->dev, GFP_KERNEL, pdev, owl_fw_cb);
  173. if (err) {
  174. dev_err(&pdev->dev, "failed to request caldata (%d).\n", err);
  175. kfree(ctx);
  176. }
  177. return err;
  178. }
  179. static void owl_remove(struct pci_dev *pdev)
  180. {
  181. struct owl_ctx *ctx = pci_get_drvdata(pdev);
  182. if (ctx) {
  183. wait_for_completion(&ctx->eeprom_load);
  184. pci_set_drvdata(pdev, NULL);
  185. kfree(ctx);
  186. }
  187. }
  188. static const struct pci_device_id owl_pci_table[] = {
  189. { PCI_VDEVICE(ATHEROS, 0xff1c) }, /* PCIe */
  190. { PCI_VDEVICE(ATHEROS, 0xff1d) }, /* PCI */
  191. { },
  192. };
  193. MODULE_DEVICE_TABLE(pci, owl_pci_table);
  194. static struct pci_driver owl_driver = {
  195. .name = "owl-loader",
  196. .id_table = owl_pci_table,
  197. .probe = owl_probe,
  198. .remove = owl_remove,
  199. };
  200. module_pci_driver(owl_driver);
  201. MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>");
  202. MODULE_DESCRIPTION("Initializes Atheros' Owl Emulation devices");
  203. MODULE_LICENSE("GPL v2");