pcie-mobiveil.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * PCIe host controller driver for Mobiveil PCIe Host controller
  4. *
  5. * Copyright (c) 2018 Mobiveil Inc.
  6. * Copyright 2019 NXP
  7. *
  8. * Author: Subrahmanya Lingappa <[email protected]>
  9. * Hou Zhiqiang <[email protected]>
  10. */
  11. #ifndef _PCIE_MOBIVEIL_H
  12. #define _PCIE_MOBIVEIL_H
  13. #include <linux/pci.h>
  14. #include <linux/irq.h>
  15. #include <linux/msi.h>
  16. #include "../../pci.h"
  17. /* register offsets and bit positions */
  18. /*
  19. * translation tables are grouped into windows, each window registers are
  20. * grouped into blocks of 4 or 16 registers each
  21. */
  22. #define PAB_REG_BLOCK_SIZE 16
  23. #define PAB_EXT_REG_BLOCK_SIZE 4
  24. #define PAB_REG_ADDR(offset, win) \
  25. (offset + (win * PAB_REG_BLOCK_SIZE))
  26. #define PAB_EXT_REG_ADDR(offset, win) \
  27. (offset + (win * PAB_EXT_REG_BLOCK_SIZE))
  28. #define LTSSM_STATUS 0x0404
  29. #define LTSSM_STATUS_L0_MASK 0x3f
  30. #define LTSSM_STATUS_L0 0x2d
  31. #define PAB_CTRL 0x0808
  32. #define AMBA_PIO_ENABLE_SHIFT 0
  33. #define PEX_PIO_ENABLE_SHIFT 1
  34. #define PAGE_SEL_SHIFT 13
  35. #define PAGE_SEL_MASK 0x3f
  36. #define PAGE_LO_MASK 0x3ff
  37. #define PAGE_SEL_OFFSET_SHIFT 10
  38. #define PAB_ACTIVITY_STAT 0x81c
  39. #define PAB_AXI_PIO_CTRL 0x0840
  40. #define APIO_EN_MASK 0xf
  41. #define PAB_PEX_PIO_CTRL 0x08c0
  42. #define PIO_ENABLE_SHIFT 0
  43. #define PAB_INTP_AMBA_MISC_ENB 0x0b0c
  44. #define PAB_INTP_AMBA_MISC_STAT 0x0b1c
  45. #define PAB_INTP_RESET BIT(1)
  46. #define PAB_INTP_MSI BIT(3)
  47. #define PAB_INTP_INTA BIT(5)
  48. #define PAB_INTP_INTB BIT(6)
  49. #define PAB_INTP_INTC BIT(7)
  50. #define PAB_INTP_INTD BIT(8)
  51. #define PAB_INTP_PCIE_UE BIT(9)
  52. #define PAB_INTP_IE_PMREDI BIT(29)
  53. #define PAB_INTP_IE_EC BIT(30)
  54. #define PAB_INTP_MSI_MASK PAB_INTP_MSI
  55. #define PAB_INTP_INTX_MASK (PAB_INTP_INTA | PAB_INTP_INTB |\
  56. PAB_INTP_INTC | PAB_INTP_INTD)
  57. #define PAB_AXI_AMAP_CTRL(win) PAB_REG_ADDR(0x0ba0, win)
  58. #define WIN_ENABLE_SHIFT 0
  59. #define WIN_TYPE_SHIFT 1
  60. #define WIN_TYPE_MASK 0x3
  61. #define WIN_SIZE_MASK 0xfffffc00
  62. #define PAB_EXT_AXI_AMAP_SIZE(win) PAB_EXT_REG_ADDR(0xbaf0, win)
  63. #define PAB_EXT_AXI_AMAP_AXI_WIN(win) PAB_EXT_REG_ADDR(0x80a0, win)
  64. #define PAB_AXI_AMAP_AXI_WIN(win) PAB_REG_ADDR(0x0ba4, win)
  65. #define AXI_WINDOW_ALIGN_MASK 3
  66. #define PAB_AXI_AMAP_PEX_WIN_L(win) PAB_REG_ADDR(0x0ba8, win)
  67. #define PAB_BUS_SHIFT 24
  68. #define PAB_DEVICE_SHIFT 19
  69. #define PAB_FUNCTION_SHIFT 16
  70. #define PAB_AXI_AMAP_PEX_WIN_H(win) PAB_REG_ADDR(0x0bac, win)
  71. #define PAB_INTP_AXI_PIO_CLASS 0x474
  72. #define PAB_PEX_AMAP_CTRL(win) PAB_REG_ADDR(0x4ba0, win)
  73. #define AMAP_CTRL_EN_SHIFT 0
  74. #define AMAP_CTRL_TYPE_SHIFT 1
  75. #define AMAP_CTRL_TYPE_MASK 3
  76. #define PAB_EXT_PEX_AMAP_SIZEN(win) PAB_EXT_REG_ADDR(0xbef0, win)
  77. #define PAB_EXT_PEX_AMAP_AXI_WIN(win) PAB_EXT_REG_ADDR(0xb4a0, win)
  78. #define PAB_PEX_AMAP_AXI_WIN(win) PAB_REG_ADDR(0x4ba4, win)
  79. #define PAB_PEX_AMAP_PEX_WIN_L(win) PAB_REG_ADDR(0x4ba8, win)
  80. #define PAB_PEX_AMAP_PEX_WIN_H(win) PAB_REG_ADDR(0x4bac, win)
  81. /* starting offset of INTX bits in status register */
  82. #define PAB_INTX_START 5
  83. /* supported number of MSI interrupts */
  84. #define PCI_NUM_MSI 16
  85. /* MSI registers */
  86. #define MSI_BASE_LO_OFFSET 0x04
  87. #define MSI_BASE_HI_OFFSET 0x08
  88. #define MSI_SIZE_OFFSET 0x0c
  89. #define MSI_ENABLE_OFFSET 0x14
  90. #define MSI_STATUS_OFFSET 0x18
  91. #define MSI_DATA_OFFSET 0x20
  92. #define MSI_ADDR_L_OFFSET 0x24
  93. #define MSI_ADDR_H_OFFSET 0x28
  94. /* outbound and inbound window definitions */
  95. #define WIN_NUM_0 0
  96. #define WIN_NUM_1 1
  97. #define CFG_WINDOW_TYPE 0
  98. #define IO_WINDOW_TYPE 1
  99. #define MEM_WINDOW_TYPE 2
  100. #define IB_WIN_SIZE ((u64)256 * 1024 * 1024 * 1024)
  101. #define MAX_PIO_WINDOWS 8
  102. /* Parameters for the waiting for link up routine */
  103. #define LINK_WAIT_MAX_RETRIES 10
  104. #define LINK_WAIT_MIN 90000
  105. #define LINK_WAIT_MAX 100000
  106. #define PAGED_ADDR_BNDRY 0xc00
  107. #define OFFSET_TO_PAGE_ADDR(off) \
  108. ((off & PAGE_LO_MASK) | PAGED_ADDR_BNDRY)
  109. #define OFFSET_TO_PAGE_IDX(off) \
  110. ((off >> PAGE_SEL_OFFSET_SHIFT) & PAGE_SEL_MASK)
  111. struct mobiveil_msi { /* MSI information */
  112. struct mutex lock; /* protect bitmap variable */
  113. struct irq_domain *msi_domain;
  114. struct irq_domain *dev_domain;
  115. phys_addr_t msi_pages_phys;
  116. int num_of_vectors;
  117. DECLARE_BITMAP(msi_irq_in_use, PCI_NUM_MSI);
  118. };
  119. struct mobiveil_pcie;
  120. struct mobiveil_rp_ops {
  121. int (*interrupt_init)(struct mobiveil_pcie *pcie);
  122. };
  123. struct mobiveil_root_port {
  124. void __iomem *config_axi_slave_base; /* endpoint config base */
  125. struct resource *ob_io_res;
  126. struct mobiveil_rp_ops *ops;
  127. int irq;
  128. raw_spinlock_t intx_mask_lock;
  129. struct irq_domain *intx_domain;
  130. struct mobiveil_msi msi;
  131. struct pci_host_bridge *bridge;
  132. };
  133. struct mobiveil_pab_ops {
  134. int (*link_up)(struct mobiveil_pcie *pcie);
  135. };
  136. struct mobiveil_pcie {
  137. struct platform_device *pdev;
  138. void __iomem *csr_axi_slave_base; /* root port config base */
  139. void __iomem *apb_csr_base; /* MSI register base */
  140. phys_addr_t pcie_reg_base; /* Physical PCIe Controller Base */
  141. int apio_wins;
  142. int ppio_wins;
  143. int ob_wins_configured; /* configured outbound windows */
  144. int ib_wins_configured; /* configured inbound windows */
  145. const struct mobiveil_pab_ops *ops;
  146. struct mobiveil_root_port rp;
  147. };
  148. int mobiveil_pcie_host_probe(struct mobiveil_pcie *pcie);
  149. int mobiveil_host_init(struct mobiveil_pcie *pcie, bool reinit);
  150. bool mobiveil_pcie_link_up(struct mobiveil_pcie *pcie);
  151. int mobiveil_bringup_link(struct mobiveil_pcie *pcie);
  152. void program_ob_windows(struct mobiveil_pcie *pcie, int win_num, u64 cpu_addr,
  153. u64 pci_addr, u32 type, u64 size);
  154. void program_ib_windows(struct mobiveil_pcie *pcie, int win_num, u64 cpu_addr,
  155. u64 pci_addr, u32 type, u64 size);
  156. u32 mobiveil_csr_read(struct mobiveil_pcie *pcie, u32 off, size_t size);
  157. void mobiveil_csr_write(struct mobiveil_pcie *pcie, u32 val, u32 off,
  158. size_t size);
  159. static inline u32 mobiveil_csr_readl(struct mobiveil_pcie *pcie, u32 off)
  160. {
  161. return mobiveil_csr_read(pcie, off, 0x4);
  162. }
  163. static inline u16 mobiveil_csr_readw(struct mobiveil_pcie *pcie, u32 off)
  164. {
  165. return mobiveil_csr_read(pcie, off, 0x2);
  166. }
  167. static inline u8 mobiveil_csr_readb(struct mobiveil_pcie *pcie, u32 off)
  168. {
  169. return mobiveil_csr_read(pcie, off, 0x1);
  170. }
  171. static inline void mobiveil_csr_writel(struct mobiveil_pcie *pcie, u32 val,
  172. u32 off)
  173. {
  174. mobiveil_csr_write(pcie, val, off, 0x4);
  175. }
  176. static inline void mobiveil_csr_writew(struct mobiveil_pcie *pcie, u16 val,
  177. u32 off)
  178. {
  179. mobiveil_csr_write(pcie, val, off, 0x2);
  180. }
  181. static inline void mobiveil_csr_writeb(struct mobiveil_pcie *pcie, u8 val,
  182. u32 off)
  183. {
  184. mobiveil_csr_write(pcie, val, off, 0x1);
  185. }
  186. #endif /* _PCIE_MOBIVEIL_H */