1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * PLDA PCIe host controller driver
4  */
5 
6 #ifndef _PCIE_PLDA_H
7 #define _PCIE_PLDA_H
8 
9 /* Number of MSI IRQs */
10 #define PLDA_MAX_NUM_MSI_IRQS			32
11 
12 /* PCIe Bridge Phy Regs */
13 #define GEN_SETTINGS				0x80
14 #define  RP_ENABLE				1
15 #define PCIE_PCI_IDS_DW1			0x9c
16 #define  IDS_CLASS_CODE_SHIFT			16
17 #define  REVISION_ID_MASK			GENMASK(7, 0)
18 #define  CLASS_CODE_ID_MASK			GENMASK(31, 8)
19 #define PCIE_PCI_IRQ_DW0			0xa8
20 #define  MSIX_CAP_MASK				BIT(31)
21 #define  NUM_MSI_MSGS_MASK			GENMASK(6, 4)
22 #define  NUM_MSI_MSGS_SHIFT			4
23 #define PCI_MISC				0xb4
24 #define  PHY_FUNCTION_DIS			BIT(15)
25 #define PCIE_WINROM				0xfc
26 #define  PREF_MEM_WIN_64_SUPPORT		BIT(3)
27 
28 #define IMASK_LOCAL				0x180
29 #define  DMA_END_ENGINE_0_MASK			0x00000000u
30 #define  DMA_END_ENGINE_0_SHIFT			0
31 #define  DMA_END_ENGINE_1_MASK			0x00000000u
32 #define  DMA_END_ENGINE_1_SHIFT			1
33 #define  DMA_ERROR_ENGINE_0_MASK		0x00000100u
34 #define  DMA_ERROR_ENGINE_0_SHIFT		8
35 #define  DMA_ERROR_ENGINE_1_MASK		0x00000200u
36 #define  DMA_ERROR_ENGINE_1_SHIFT		9
37 #define  A_ATR_EVT_POST_ERR_MASK		0x00010000u
38 #define  A_ATR_EVT_POST_ERR_SHIFT		16
39 #define  A_ATR_EVT_FETCH_ERR_MASK		0x00020000u
40 #define  A_ATR_EVT_FETCH_ERR_SHIFT		17
41 #define  A_ATR_EVT_DISCARD_ERR_MASK		0x00040000u
42 #define  A_ATR_EVT_DISCARD_ERR_SHIFT		18
43 #define  A_ATR_EVT_DOORBELL_MASK		0x00000000u
44 #define  A_ATR_EVT_DOORBELL_SHIFT		19
45 #define  P_ATR_EVT_POST_ERR_MASK		0x00100000u
46 #define  P_ATR_EVT_POST_ERR_SHIFT		20
47 #define  P_ATR_EVT_FETCH_ERR_MASK		0x00200000u
48 #define  P_ATR_EVT_FETCH_ERR_SHIFT		21
49 #define  P_ATR_EVT_DISCARD_ERR_MASK		0x00400000u
50 #define  P_ATR_EVT_DISCARD_ERR_SHIFT		22
51 #define  P_ATR_EVT_DOORBELL_MASK		0x00000000u
52 #define  P_ATR_EVT_DOORBELL_SHIFT		23
53 #define  PM_MSI_INT_INTA_MASK			0x01000000u
54 #define  PM_MSI_INT_INTA_SHIFT			24
55 #define  PM_MSI_INT_INTB_MASK			0x02000000u
56 #define  PM_MSI_INT_INTB_SHIFT			25
57 #define  PM_MSI_INT_INTC_MASK			0x04000000u
58 #define  PM_MSI_INT_INTC_SHIFT			26
59 #define  PM_MSI_INT_INTD_MASK			0x08000000u
60 #define  PM_MSI_INT_INTD_SHIFT			27
61 #define  PM_MSI_INT_INTX_MASK			0x0f000000u
62 #define  PM_MSI_INT_INTX_SHIFT			24
63 #define  PM_MSI_INT_MSI_MASK			0x10000000u
64 #define  PM_MSI_INT_MSI_SHIFT			28
65 #define  PM_MSI_INT_AER_EVT_MASK		0x20000000u
66 #define  PM_MSI_INT_AER_EVT_SHIFT		29
67 #define  PM_MSI_INT_EVENTS_MASK			0x40000000u
68 #define  PM_MSI_INT_EVENTS_SHIFT		30
69 #define  PM_MSI_INT_SYS_ERR_MASK		0x80000000u
70 #define  PM_MSI_INT_SYS_ERR_SHIFT		31
71 #define  SYS_AND_MSI_MASK			GENMASK(31, 28)
72 #define  NUM_LOCAL_EVENTS			15
73 #define ISTATUS_LOCAL				0x184
74 #define IMASK_HOST				0x188
75 #define ISTATUS_HOST				0x18c
76 #define IMSI_ADDR				0x190
77 #define ISTATUS_MSI				0x194
78 #define PMSG_SUPPORT_RX				0x3f0
79 #define  PMSG_LTR_SUPPORT			BIT(2)
80 
81 /* PCIe Master table init defines */
82 #define ATR0_PCIE_WIN0_SRCADDR_PARAM		0x600u
83 #define  ATR0_PCIE_ATR_SIZE			0x25
84 #define  ATR0_PCIE_ATR_SIZE_SHIFT		1
85 #define ATR0_PCIE_WIN0_SRC_ADDR			0x604u
86 #define ATR0_PCIE_WIN0_TRSL_ADDR_LSB		0x608u
87 #define ATR0_PCIE_WIN0_TRSL_ADDR_UDW		0x60cu
88 #define ATR0_PCIE_WIN0_TRSL_PARAM		0x610u
89 
90 /* PCIe AXI slave table init defines */
91 #define ATR0_AXI4_SLV0_SRCADDR_PARAM		0x800u
92 #define  ATR_SIZE_MASK				GENMASK(6, 1)
93 #define  ATR_IMPL_ENABLE			BIT(0)
94 #define ATR0_AXI4_SLV0_SRC_ADDR			0x804u
95 #define ATR0_AXI4_SLV0_TRSL_ADDR_LSB		0x808u
96 #define ATR0_AXI4_SLV0_TRSL_ADDR_UDW		0x80cu
97 #define ATR0_AXI4_SLV0_TRSL_PARAM		0x810u
98 #define  PCIE_TX_RX_INTERFACE			0x00000000u
99 #define  PCIE_CONFIG_INTERFACE			0x00000001u
100 #define  TRSL_ID_AXI4_MASTER_0			0x00000004u
101 
102 #define CONFIG_SPACE_ADDR_OFFSET		0x1000u
103 
104 #define ATR_ENTRY_SIZE				32
105 
106 enum plda_int_event {
107 	PLDA_AXI_POST_ERR,
108 	PLDA_AXI_FETCH_ERR,
109 	PLDA_AXI_DISCARD_ERR,
110 	PLDA_AXI_DOORBELL,
111 	PLDA_PCIE_POST_ERR,
112 	PLDA_PCIE_FETCH_ERR,
113 	PLDA_PCIE_DISCARD_ERR,
114 	PLDA_PCIE_DOORBELL,
115 	PLDA_INTX,
116 	PLDA_MSI,
117 	PLDA_AER_EVENT,
118 	PLDA_MISC_EVENTS,
119 	PLDA_SYS_ERR,
120 	PLDA_INT_EVENT_NUM
121 };
122 
123 #define PLDA_NUM_DMA_EVENTS			16
124 
125 #define EVENT_PM_MSI_INT_INTX			(PLDA_NUM_DMA_EVENTS + PLDA_INTX)
126 #define EVENT_PM_MSI_INT_MSI			(PLDA_NUM_DMA_EVENTS + PLDA_MSI)
127 #define PLDA_MAX_EVENT_NUM			(PLDA_NUM_DMA_EVENTS + PLDA_INT_EVENT_NUM)
128 
129 /*
130  * PLDA interrupt register
131  *
132  * 31         27     23              15           7          0
133  * +--+--+--+-+------+-+-+-+-+-+-+-+-+-----------+-----------+
134  * |12|11|10|9| intx |7|6|5|4|3|2|1|0| DMA error | DMA end   |
135  * +--+--+--+-+------+-+-+-+-+-+-+-+-+-----------+-----------+
136  * event  bit
137  * 0-7   (0-7)   DMA interrupt end : reserved for vendor implement
138  * 8-15  (8-15)  DMA error : reserved for vendor implement
139  * 16    (16)    AXI post error (PLDA_AXI_POST_ERR)
140  * 17    (17)    AXI fetch error (PLDA_AXI_FETCH_ERR)
141  * 18    (18)    AXI discard error (PLDA_AXI_DISCARD_ERR)
142  * 19    (19)    AXI doorbell (PLDA_PCIE_DOORBELL)
143  * 20    (20)    PCIe post error (PLDA_PCIE_POST_ERR)
144  * 21    (21)    PCIe fetch error (PLDA_PCIE_FETCH_ERR)
145  * 22    (22)    PCIe discard error (PLDA_PCIE_DISCARD_ERR)
146  * 23    (23)    PCIe doorbell (PLDA_PCIE_DOORBELL)
147  * 24    (27-24) INTx interruts (PLDA_INTX)
148  * 25    (28):   MSI interrupt (PLDA_MSI)
149  * 26    (29):   AER event (PLDA_AER_EVENT)
150  * 27    (30):   PM/LTR/Hotplug (PLDA_MISC_EVENTS)
151  * 28    (31):   System error (PLDA_SYS_ERR)
152  */
153 
154 struct plda_pcie_rp;
155 
156 struct plda_event_ops {
157 	u32 (*get_events)(struct plda_pcie_rp *pcie);
158 };
159 
160 struct plda_pcie_host_ops {
161 	int (*host_init)(struct plda_pcie_rp *pcie);
162 	void (*host_deinit)(struct plda_pcie_rp *pcie);
163 };
164 
165 struct plda_msi {
166 	struct mutex lock;		/* Protect used bitmap */
167 	struct irq_domain *msi_domain;
168 	struct irq_domain *dev_domain;
169 	u32 num_vectors;
170 	u64 vector_phy;
171 	DECLARE_BITMAP(used, PLDA_MAX_NUM_MSI_IRQS);
172 };
173 
174 struct plda_pcie_rp {
175 	struct device *dev;
176 	struct pci_host_bridge *bridge;
177 	struct irq_domain *intx_domain;
178 	struct irq_domain *event_domain;
179 	raw_spinlock_t lock;
180 	struct plda_msi msi;
181 	const struct plda_event_ops *event_ops;
182 	const struct irq_chip *event_irq_chip;
183 	const struct plda_pcie_host_ops *host_ops;
184 	void __iomem *bridge_addr;
185 	void __iomem *config_base;
186 	unsigned long events_bitmap;
187 	int irq;
188 	int msi_irq;
189 	int intx_irq;
190 	int num_events;
191 };
192 
193 struct plda_event {
194 	int (*request_event_irq)(struct plda_pcie_rp *pcie,
195 				 int event_irq, int event);
196 	int intx_event;
197 	int msi_event;
198 };
199 
200 void __iomem *plda_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
201 				int where);
202 int plda_init_interrupts(struct platform_device *pdev,
203 			 struct plda_pcie_rp *port,
204 			 const struct plda_event *event);
205 void plda_pcie_setup_window(void __iomem *bridge_base_addr, u32 index,
206 			    phys_addr_t axi_addr, phys_addr_t pci_addr,
207 			    size_t size);
208 void plda_pcie_setup_inbound_address_translation(struct plda_pcie_rp *port);
209 int plda_pcie_setup_iomems(struct pci_host_bridge *bridge,
210 			   struct plda_pcie_rp *port);
211 int plda_pcie_host_init(struct plda_pcie_rp *port, struct pci_ops *ops,
212 			const struct plda_event *plda_event);
213 void plda_pcie_host_deinit(struct plda_pcie_rp *pcie);
214 
plda_set_default_msi(struct plda_msi * msi)215 static inline void plda_set_default_msi(struct plda_msi *msi)
216 {
217 	msi->vector_phy = IMSI_ADDR;
218 	msi->num_vectors = PLDA_MAX_NUM_MSI_IRQS;
219 }
220 
plda_pcie_enable_root_port(struct plda_pcie_rp * plda)221 static inline void plda_pcie_enable_root_port(struct plda_pcie_rp *plda)
222 {
223 	u32 value;
224 
225 	value = readl_relaxed(plda->bridge_addr + GEN_SETTINGS);
226 	value |= RP_ENABLE;
227 	writel_relaxed(value, plda->bridge_addr + GEN_SETTINGS);
228 }
229 
plda_pcie_set_standard_class(struct plda_pcie_rp * plda)230 static inline void plda_pcie_set_standard_class(struct plda_pcie_rp *plda)
231 {
232 	u32 value;
233 
234 	/* set class code and reserve revision id */
235 	value = readl_relaxed(plda->bridge_addr + PCIE_PCI_IDS_DW1);
236 	value &= REVISION_ID_MASK;
237 	value |= (PCI_CLASS_BRIDGE_PCI << IDS_CLASS_CODE_SHIFT);
238 	writel_relaxed(value, plda->bridge_addr + PCIE_PCI_IDS_DW1);
239 }
240 
plda_pcie_set_pref_win_64bit(struct plda_pcie_rp * plda)241 static inline void plda_pcie_set_pref_win_64bit(struct plda_pcie_rp *plda)
242 {
243 	u32 value;
244 
245 	value = readl_relaxed(plda->bridge_addr + PCIE_WINROM);
246 	value |= PREF_MEM_WIN_64_SUPPORT;
247 	writel_relaxed(value, plda->bridge_addr + PCIE_WINROM);
248 }
249 
plda_pcie_disable_ltr(struct plda_pcie_rp * plda)250 static inline void plda_pcie_disable_ltr(struct plda_pcie_rp *plda)
251 {
252 	u32 value;
253 
254 	value = readl_relaxed(plda->bridge_addr + PMSG_SUPPORT_RX);
255 	value &= ~PMSG_LTR_SUPPORT;
256 	writel_relaxed(value, plda->bridge_addr + PMSG_SUPPORT_RX);
257 }
258 
plda_pcie_disable_func(struct plda_pcie_rp * plda)259 static inline void plda_pcie_disable_func(struct plda_pcie_rp *plda)
260 {
261 	u32 value;
262 
263 	value = readl_relaxed(plda->bridge_addr + PCI_MISC);
264 	value |= PHY_FUNCTION_DIS;
265 	writel_relaxed(value, plda->bridge_addr + PCI_MISC);
266 }
267 
plda_pcie_write_rc_bar(struct plda_pcie_rp * plda,u64 val)268 static inline void plda_pcie_write_rc_bar(struct plda_pcie_rp *plda, u64 val)
269 {
270 	void __iomem *addr = plda->bridge_addr + CONFIG_SPACE_ADDR_OFFSET;
271 
272 	writel_relaxed(lower_32_bits(val), addr + PCI_BASE_ADDRESS_0);
273 	writel_relaxed(upper_32_bits(val), addr + PCI_BASE_ADDRESS_1);
274 }
275 #endif /* _PCIE_PLDA_H */
276