pci.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef _PCI_H_
  18. #define _PCI_H_
  19. #include <linux/interrupt.h>
  20. #include "hw.h"
  21. #include "ce.h"
  22. /* FW dump area */
  23. #define REG_DUMP_COUNT_QCA988X 60
  24. /*
  25. * maximum number of bytes that can be handled atomically by DiagRead/DiagWrite
  26. */
  27. #define DIAG_TRANSFER_LIMIT 2048
  28. /*
  29. * maximum number of bytes that can be
  30. * handled atomically by DiagRead/DiagWrite
  31. */
  32. #define DIAG_TRANSFER_LIMIT 2048
  33. struct bmi_xfer {
  34. struct completion done;
  35. bool wait_for_resp;
  36. u32 resp_len;
  37. };
  38. struct ath10k_pci_compl {
  39. struct list_head list;
  40. int send_or_recv;
  41. struct ce_state *ce_state;
  42. struct hif_ce_pipe_info *pipe_info;
  43. void *transfer_context;
  44. unsigned int nbytes;
  45. unsigned int transfer_id;
  46. unsigned int flags;
  47. };
  48. /* compl_state.send_or_recv */
  49. #define HIF_CE_COMPLETE_FREE 0
  50. #define HIF_CE_COMPLETE_SEND 1
  51. #define HIF_CE_COMPLETE_RECV 2
  52. /*
  53. * PCI-specific Target state
  54. *
  55. * NOTE: Structure is shared between Host software and Target firmware!
  56. *
  57. * Much of this may be of interest to the Host so
  58. * HOST_INTEREST->hi_interconnect_state points here
  59. * (and all members are 32-bit quantities in order to
  60. * facilitate Host access). In particular, Host software is
  61. * required to initialize pipe_cfg_addr and svc_to_pipe_map.
  62. */
  63. struct pcie_state {
  64. /* Pipe configuration Target address */
  65. /* NB: ce_pipe_config[CE_COUNT] */
  66. u32 pipe_cfg_addr;
  67. /* Service to pipe map Target address */
  68. /* NB: service_to_pipe[PIPE_TO_CE_MAP_CN] */
  69. u32 svc_to_pipe_map;
  70. /* number of MSI interrupts requested */
  71. u32 msi_requested;
  72. /* number of MSI interrupts granted */
  73. u32 msi_granted;
  74. /* Message Signalled Interrupt address */
  75. u32 msi_addr;
  76. /* Base data */
  77. u32 msi_data;
  78. /*
  79. * Data for firmware interrupt;
  80. * MSI data for other interrupts are
  81. * in various SoC registers
  82. */
  83. u32 msi_fw_intr_data;
  84. /* PCIE_PWR_METHOD_* */
  85. u32 power_mgmt_method;
  86. /* PCIE_CONFIG_FLAG_* */
  87. u32 config_flags;
  88. };
  89. /* PCIE_CONFIG_FLAG definitions */
  90. #define PCIE_CONFIG_FLAG_ENABLE_L1 0x0000001
  91. /* Host software's Copy Engine configuration. */
  92. #define CE_ATTR_FLAGS 0
  93. /*
  94. * Configuration information for a Copy Engine pipe.
  95. * Passed from Host to Target during startup (one per CE).
  96. *
  97. * NOTE: Structure is shared between Host software and Target firmware!
  98. */
  99. struct ce_pipe_config {
  100. u32 pipenum;
  101. u32 pipedir;
  102. u32 nentries;
  103. u32 nbytes_max;
  104. u32 flags;
  105. u32 reserved;
  106. };
  107. /*
  108. * Directions for interconnect pipe configuration.
  109. * These definitions may be used during configuration and are shared
  110. * between Host and Target.
  111. *
  112. * Pipe Directions are relative to the Host, so PIPEDIR_IN means
  113. * "coming IN over air through Target to Host" as with a WiFi Rx operation.
  114. * Conversely, PIPEDIR_OUT means "going OUT from Host through Target over air"
  115. * as with a WiFi Tx operation. This is somewhat awkward for the "middle-man"
  116. * Target since things that are "PIPEDIR_OUT" are coming IN to the Target
  117. * over the interconnect.
  118. */
  119. #define PIPEDIR_NONE 0
  120. #define PIPEDIR_IN 1 /* Target-->Host, WiFi Rx direction */
  121. #define PIPEDIR_OUT 2 /* Host->Target, WiFi Tx direction */
  122. #define PIPEDIR_INOUT 3 /* bidirectional */
  123. /* Establish a mapping between a service/direction and a pipe. */
  124. struct service_to_pipe {
  125. u32 service_id;
  126. u32 pipedir;
  127. u32 pipenum;
  128. };
  129. enum ath10k_pci_features {
  130. ATH10K_PCI_FEATURE_MSI_X = 0,
  131. ATH10K_PCI_FEATURE_HW_1_0_WORKAROUND = 1,
  132. ATH10K_PCI_FEATURE_SOC_POWER_SAVE = 2,
  133. /* keep last */
  134. ATH10K_PCI_FEATURE_COUNT
  135. };
  136. /* Per-pipe state. */
  137. struct hif_ce_pipe_info {
  138. /* Handle of underlying Copy Engine */
  139. struct ce_state *ce_hdl;
  140. /* Our pipe number; facilitiates use of pipe_info ptrs. */
  141. u8 pipe_num;
  142. /* Convenience back pointer to hif_ce_state. */
  143. struct ath10k *hif_ce_state;
  144. size_t buf_sz;
  145. /* protects compl_free and num_send_allowed */
  146. spinlock_t pipe_lock;
  147. /* List of free CE completion slots */
  148. struct list_head compl_free;
  149. /* Limit the number of outstanding send requests. */
  150. int num_sends_allowed;
  151. struct ath10k_pci *ar_pci;
  152. struct tasklet_struct intr;
  153. };
  154. struct ath10k_pci {
  155. struct pci_dev *pdev;
  156. struct device *dev;
  157. struct ath10k *ar;
  158. void __iomem *mem;
  159. int cacheline_sz;
  160. DECLARE_BITMAP(features, ATH10K_PCI_FEATURE_COUNT);
  161. /*
  162. * Number of MSI interrupts granted, 0 --> using legacy PCI line
  163. * interrupts.
  164. */
  165. int num_msi_intrs;
  166. struct tasklet_struct intr_tq;
  167. struct tasklet_struct msi_fw_err;
  168. /* Number of Copy Engines supported */
  169. unsigned int ce_count;
  170. int started;
  171. atomic_t keep_awake_count;
  172. bool verified_awake;
  173. /* List of CE completions to be processed */
  174. struct list_head compl_process;
  175. /* protects compl_processing and compl_process */
  176. spinlock_t compl_lock;
  177. bool compl_processing;
  178. struct hif_ce_pipe_info pipe_info[CE_COUNT_MAX];
  179. struct ath10k_hif_cb msg_callbacks_current;
  180. /* Target address used to signal a pending firmware event */
  181. u32 fw_indicator_address;
  182. /* Copy Engine used for Diagnostic Accesses */
  183. struct ce_state *ce_diag;
  184. /* FIXME: document what this really protects */
  185. spinlock_t ce_lock;
  186. /* Map CE id to ce_state */
  187. struct ce_state *ce_id_to_state[CE_COUNT_MAX];
  188. /* makes sure that dummy reads are atomic */
  189. spinlock_t hw_v1_workaround_lock;
  190. };
  191. static inline struct ath10k_pci *ath10k_pci_priv(struct ath10k *ar)
  192. {
  193. return ar->hif.priv;
  194. }
  195. static inline u32 ath10k_pci_reg_read32(void __iomem *mem, u32 addr)
  196. {
  197. return ioread32(mem + PCIE_LOCAL_BASE_ADDRESS + addr);
  198. }
  199. static inline void ath10k_pci_reg_write32(void __iomem *mem, u32 addr, u32 val)
  200. {
  201. iowrite32(val, mem + PCIE_LOCAL_BASE_ADDRESS + addr);
  202. }
  203. #define ATH_PCI_RESET_WAIT_MAX 10 /* ms */
  204. #define PCIE_WAKE_TIMEOUT 5000 /* 5ms */
  205. #define BAR_NUM 0
  206. #define CDC_WAR_MAGIC_STR 0xceef0000
  207. #define CDC_WAR_DATA_CE 4
  208. /*
  209. * TODO: Should be a function call specific to each Target-type.
  210. * This convoluted macro converts from Target CPU Virtual Address Space to CE
  211. * Address Space. As part of this process, we conservatively fetch the current
  212. * PCIE_BAR. MOST of the time, this should match the upper bits of PCI space
  213. * for this device; but that's not guaranteed.
  214. */
  215. #define TARG_CPU_SPACE_TO_CE_SPACE(ar, pci_addr, addr) \
  216. (((ioread32((pci_addr)+(SOC_CORE_BASE_ADDRESS| \
  217. CORE_CTRL_ADDRESS)) & 0x7ff) << 21) | \
  218. 0x100000 | ((addr) & 0xfffff))
  219. /* Wait up to this many Ms for a Diagnostic Access CE operation to complete */
  220. #define DIAG_ACCESS_CE_TIMEOUT_MS 10
  221. /*
  222. * This API allows the Host to access Target registers directly
  223. * and relatively efficiently over PCIe.
  224. * This allows the Host to avoid extra overhead associated with
  225. * sending a message to firmware and waiting for a response message
  226. * from firmware, as is done on other interconnects.
  227. *
  228. * Yet there is some complexity with direct accesses because the
  229. * Target's power state is not known a priori. The Host must issue
  230. * special PCIe reads/writes in order to explicitly wake the Target
  231. * and to verify that it is awake and will remain awake.
  232. *
  233. * Usage:
  234. *
  235. * Use ath10k_pci_read32 and ath10k_pci_write32 to access Target space.
  236. * These calls must be bracketed by ath10k_pci_wake and
  237. * ath10k_pci_sleep. A single BEGIN/END pair is adequate for
  238. * multiple READ/WRITE operations.
  239. *
  240. * Use ath10k_pci_wake to put the Target in a state in
  241. * which it is legal for the Host to directly access it. This
  242. * may involve waking the Target from a low power state, which
  243. * may take up to 2Ms!
  244. *
  245. * Use ath10k_pci_sleep to tell the Target that as far as
  246. * this code path is concerned, it no longer needs to remain
  247. * directly accessible. BEGIN/END is under a reference counter;
  248. * multiple code paths may issue BEGIN/END on a single targid.
  249. */
  250. static inline void ath10k_pci_write32(struct ath10k *ar, u32 offset,
  251. u32 value)
  252. {
  253. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  254. void __iomem *addr = ar_pci->mem;
  255. if (test_bit(ATH10K_PCI_FEATURE_HW_1_0_WORKAROUND, ar_pci->features)) {
  256. unsigned long irq_flags;
  257. spin_lock_irqsave(&ar_pci->hw_v1_workaround_lock, irq_flags);
  258. ioread32(addr+offset+4); /* 3rd read prior to write */
  259. ioread32(addr+offset+4); /* 2nd read prior to write */
  260. ioread32(addr+offset+4); /* 1st read prior to write */
  261. iowrite32(value, addr+offset);
  262. spin_unlock_irqrestore(&ar_pci->hw_v1_workaround_lock,
  263. irq_flags);
  264. } else {
  265. iowrite32(value, addr+offset);
  266. }
  267. }
  268. static inline u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
  269. {
  270. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  271. return ioread32(ar_pci->mem + offset);
  272. }
  273. void ath10k_do_pci_wake(struct ath10k *ar);
  274. void ath10k_do_pci_sleep(struct ath10k *ar);
  275. static inline void ath10k_pci_wake(struct ath10k *ar)
  276. {
  277. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  278. if (test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
  279. ath10k_do_pci_wake(ar);
  280. }
  281. static inline void ath10k_pci_sleep(struct ath10k *ar)
  282. {
  283. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  284. if (test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
  285. ath10k_do_pci_sleep(ar);
  286. }
  287. #endif /* _PCI_H_ */