Debian Codegraph @

drivers/pci/controller/cadence/pcie-cadence.h

raw
1 /* SPDX-License-Identifier: GPL-2.0 */
2 // Copyright (c) 2017 Cadence
3 // Cadence PCIe controller driver.
4 // Author: Cyrille Pitchen <[email protected]>
5
6 #ifndef _PCIE_CADENCE_H
7 #define _PCIE_CADENCE_H
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/pci-epf.h>
13 #include <linux/phy/phy.h>
14 #include "pcie-cadence-lga-regs.h"
15 #include "pcie-cadence-hpa-regs.h"
16
17 enum cdns_pcie_rp_bar {
18 RP_BAR_UNDEFINED = -1,
19 RP_BAR0,
20 RP_BAR1,
21 RP_NO_BAR
22 };
23
24 struct cdns_pcie_rp_ib_bar {
25 u64 size;
26 bool free;
27 };
28
29 struct cdns_pcie;
30 struct cdns_pcie_rc;
31
32 enum cdns_pcie_reg_bank {
33 REG_BANK_RP,
34 REG_BANK_IP_REG,
35 REG_BANK_IP_CFG_CTRL_REG,
36 REG_BANK_AXI_MASTER_COMMON,
37 REG_BANK_AXI_MASTER,
38 REG_BANK_AXI_SLAVE,
39 REG_BANK_AXI_HLS,
40 REG_BANK_AXI_RAS,
41 REG_BANK_AXI_DTI,
42 REG_BANKS_MAX,
43 };
44
45 struct cdns_pcie_ops {
46 int (*start_link)(struct cdns_pcie *pcie);
47 void (*stop_link)(struct cdns_pcie *pcie);
48 bool (*link_up)(struct cdns_pcie *pcie);
49 u64 (*cpu_addr_fixup)(struct cdns_pcie *pcie, u64 cpu_addr);
50 };
51
52 /**
53 * struct cdns_plat_pcie_of_data - Register bank offset for a platform
54 * @is_rc: controller is a RC
55 * @ip_reg_bank_offset: ip register bank start offset
56 * @ip_cfg_ctrl_reg_offset: ip config control register start offset
57 * @axi_mstr_common_offset: AXI master common register start offset
58 * @axi_slave_offset: AXI slave start offset
59 * @axi_master_offset: AXI master start offset
60 * @axi_hls_offset: AXI HLS offset start
61 * @axi_ras_offset: AXI RAS offset
62 * @axi_dti_offset: AXI DTI offset
63 */
64 struct cdns_plat_pcie_of_data {
65 u32 is_rc:1;
66 u32 ip_reg_bank_offset;
67 u32 ip_cfg_ctrl_reg_offset;
68 u32 axi_mstr_common_offset;
69 u32 axi_slave_offset;
70 u32 axi_master_offset;
71 u32 axi_hls_offset;
72 u32 axi_ras_offset;
73 u32 axi_dti_offset;
74 };
75
76 /**
77 * struct cdns_pcie - private data for Cadence PCIe controller drivers
78 * @reg_base: IO mapped register base
79 * @mem_res: start/end offsets in the physical system memory to map PCI accesses
80 * @msg_res: Region for send message to map PCI accesses
81 * @dev: PCIe controller
82 * @is_rc: tell whether the PCIe controller mode is Root Complex or Endpoint.
83 * @phy_count: number of supported PHY devices
84 * @phy: list of pointers to specific PHY control blocks
85 * @link: list of pointers to corresponding device link representations
86 * @ops: Platform-specific ops to control various inputs from Cadence PCIe
87 * wrapper
88 * @cdns_pcie_reg_offsets: Register bank offsets for different SoC
89 */
90 struct cdns_pcie {
91 void __iomem *reg_base;
92 struct resource *mem_res;
93 struct resource *msg_res;
94 struct device *dev;
95 bool is_rc;
96 int phy_count;
97 struct phy **phy;
98 struct device_link **link;
99 const struct cdns_pcie_ops *ops;
100 const struct cdns_plat_pcie_of_data *cdns_pcie_reg_offsets;
101 };
102
103 /**
104 * struct cdns_pcie_rc - private data for this PCIe Root Complex driver
105 * @pcie: Cadence PCIe controller
106 * @cfg_res: start/end offsets in the physical system memory to map PCI
107 * configuration space accesses
108 * @cfg_base: IO mapped window to access the PCI configuration space of a
109 * single function at a time
110 * @vendor_id: PCI vendor ID
111 * @device_id: PCI device ID
112 * @avail_ib_bar: Status of RP_BAR0, RP_BAR1 and RP_NO_BAR if it's free or
113 * available
114 * @quirk_retrain_flag: Retrain link as quirk for PCIe Gen2
115 * @quirk_detect_quiet_flag: LTSSM Detect Quiet min delay set as quirk
116 * @ecam_supported: Whether the ECAM is supported
117 * @no_inbound_map: Whether inbound mapping is supported
118 * @quirk_broken_aspm_l0s: Disable ASPM L0s support as quirk
119 * @quirk_broken_aspm_l1: Disable ASPM L1 support as quirk
120 */
121 struct cdns_pcie_rc {
122 struct cdns_pcie pcie;
123 struct resource *cfg_res;
124 void __iomem *cfg_base;
125 u32 vendor_id;
126 u32 device_id;
127 bool avail_ib_bar[CDNS_PCIE_RP_MAX_IB];
128 unsigned int quirk_retrain_flag:1;
129 unsigned int quirk_detect_quiet_flag:1;
130 unsigned int ecam_supported:1;
131 unsigned int no_inbound_map:1;
132 unsigned int quirk_broken_aspm_l0s:1;
133 unsigned int quirk_broken_aspm_l1:1;
134 };
135
136 /**
137 * struct cdns_pcie_epf - Structure to hold info about endpoint function
138 * @epf: Info about virtual functions attached to the physical function
139 * @epf_bar: reference to the pci_epf_bar for the six Base Address Registers
140 */
141 struct cdns_pcie_epf {
142 struct cdns_pcie_epf *epf;
143 struct pci_epf_bar *epf_bar[PCI_STD_NUM_BARS];
144 };
145
146 /**
147 * struct cdns_pcie_ep - private data for this PCIe endpoint controller driver
148 * @pcie: Cadence PCIe controller
149 * @max_regions: maximum number of regions supported by hardware
150 * @ob_region_map: bitmask of mapped outbound regions
151 * @ob_addr: base addresses in the AXI bus where the outbound regions start
152 * @irq_phys_addr: base address on the AXI bus where the MSI/INTX IRQ
153 * dedicated outbound regions is mapped.
154 * @irq_cpu_addr: base address in the CPU space where a write access triggers
155 * the sending of a memory write (MSI) / normal message (INTX
156 * IRQ) TLP through the PCIe bus.
157 * @irq_pci_addr: used to save the current mapping of the MSI/INTX IRQ
158 * dedicated outbound region.
159 * @irq_pci_fn: the latest PCI function that has updated the mapping of
160 * the MSI/INTX IRQ dedicated outbound region.
161 * @irq_pending: bitmask of asserted INTX IRQs.
162 * @lock: spin lock to disable interrupts while modifying PCIe controller
163 * registers fields (RMW) accessible by both remote RC and EP to
164 * minimize time between read and write
165 * @epf: Structure to hold info about endpoint function
166 * @quirk_detect_quiet_flag: LTSSM Detect Quiet min delay set as quirk
167 * @quirk_disable_flr: Disable FLR (Function Level Reset) quirk flag
168 */
169 struct cdns_pcie_ep {
170 struct cdns_pcie pcie;
171 u32 max_regions;
172 unsigned long ob_region_map;
173 phys_addr_t *ob_addr;
174 phys_addr_t irq_phys_addr;
175 void __iomem *irq_cpu_addr;
176 u64 irq_pci_addr;
177 u8 irq_pci_fn;
178 u8 irq_pending;
179 /* protect writing to PCI_STATUS while raising INTX interrupts */
180 spinlock_t lock;
181 struct cdns_pcie_epf *epf;
182 unsigned int quirk_detect_quiet_flag:1;
183 unsigned int quirk_disable_flr:1;
184 };
185
186 static inline u32 cdns_reg_bank_to_off(struct cdns_pcie *pcie, enum cdns_pcie_reg_bank bank)
187 {
188 u32 offset = 0x0;
189
190 switch (bank) {
191 case REG_BANK_RP:
192 offset = 0;
193 break;
194 case REG_BANK_IP_REG:
195 offset = pcie->cdns_pcie_reg_offsets->ip_reg_bank_offset;
196 break;
197 case REG_BANK_IP_CFG_CTRL_REG:
198 offset = pcie->cdns_pcie_reg_offsets->ip_cfg_ctrl_reg_offset;
199 break;
200 case REG_BANK_AXI_MASTER_COMMON:
201 offset = pcie->cdns_pcie_reg_offsets->axi_mstr_common_offset;
202 break;
203 case REG_BANK_AXI_MASTER:
204 offset = pcie->cdns_pcie_reg_offsets->axi_master_offset;
205 break;
206 case REG_BANK_AXI_SLAVE:
207 offset = pcie->cdns_pcie_reg_offsets->axi_slave_offset;
208 break;
209 case REG_BANK_AXI_HLS:
210 offset = pcie->cdns_pcie_reg_offsets->axi_hls_offset;
211 break;
212 case REG_BANK_AXI_RAS:
213 offset = pcie->cdns_pcie_reg_offsets->axi_ras_offset;
214 break;
215 case REG_BANK_AXI_DTI:
216 offset = pcie->cdns_pcie_reg_offsets->axi_dti_offset;
217 break;
218 default:
219 break;
220 }
221 return offset;
222 }
223
224 /* Register access */
225 static inline void cdns_pcie_writel(struct cdns_pcie *pcie, u32 reg, u32 value)
226 {
227 writel(value, pcie->reg_base + reg);
228 }
229
230 static inline u32 cdns_pcie_readl(struct cdns_pcie *pcie, u32 reg)
231 {
232 return readl(pcie->reg_base + reg);
233 }
234
235 static inline void cdns_pcie_hpa_writel(struct cdns_pcie *pcie,
236 enum cdns_pcie_reg_bank bank,
237 u32 reg,
238 u32 value)
239 {
240 u32 offset = cdns_reg_bank_to_off(pcie, bank);
241
242 reg += offset;
243 writel(value, pcie->reg_base + reg);
244 }
245
246 static inline u32 cdns_pcie_hpa_readl(struct cdns_pcie *pcie,
247 enum cdns_pcie_reg_bank bank,
248 u32 reg)
249 {
250 u32 offset = cdns_reg_bank_to_off(pcie, bank);
251
252 reg += offset;
253 return readl(pcie->reg_base + reg);
254 }
255
256 static inline u32 cdns_pcie_read_sz(void __iomem *addr, int size)
257 {
258 void __iomem *aligned_addr = PTR_ALIGN_DOWN(addr, 0x4);
259 unsigned int offset = (unsigned long)addr & 0x3;
260 u32 val = readl(aligned_addr);
261
262 if (!IS_ALIGNED((uintptr_t)addr, size)) {
263 pr_warn("Address %p and size %d are not aligned\n", addr, size);
264 return 0;
265 }
266
267 if (size > 2)
268 return val;
269
270 return (val >> (8 * offset)) & ((1 << (size * 8)) - 1);
271 }
272
273 static inline void cdns_pcie_write_sz(void __iomem *addr, int size, u32 value)
274 {
275 void __iomem *aligned_addr = PTR_ALIGN_DOWN(addr, 0x4);
276 unsigned int offset = (unsigned long)addr & 0x3;
277 u32 mask;
278 u32 val;
279
280 if (!IS_ALIGNED((uintptr_t)addr, size)) {
281 pr_warn("Address %p and size %d are not aligned\n", addr, size);
282 return;
283 }
284
285 if (size > 2) {
286 writel(value, addr);
287 return;
288 }
289
290 mask = ~(((1 << (size * 8)) - 1) << (offset * 8));
291 val = readl(aligned_addr) & mask;
292 val |= value << (offset * 8);
293 writel(val, aligned_addr);
294 }
295
296 static inline int cdns_pcie_read_cfg_byte(struct cdns_pcie *pcie, int where,
297 u8 *val)
298 {
299 void __iomem *addr = pcie->reg_base + where;
300
301 *val = cdns_pcie_read_sz(addr, 0x1);
302 return PCIBIOS_SUCCESSFUL;
303 }
304
305 static inline int cdns_pcie_read_cfg_word(struct cdns_pcie *pcie, int where,
306 u16 *val)
307 {
308 void __iomem *addr = pcie->reg_base + where;
309
310 *val = cdns_pcie_read_sz(addr, 0x2);
311 return PCIBIOS_SUCCESSFUL;
312 }
313
314 static inline int cdns_pcie_read_cfg_dword(struct cdns_pcie *pcie, int where,
315 u32 *val)
316 {
317 *val = cdns_pcie_readl(pcie, where);
318 return PCIBIOS_SUCCESSFUL;
319 }
320
321 /* Root Port register access */
322 static inline void cdns_pcie_rp_writeb(struct cdns_pcie *pcie,
323 u32 reg, u8 value)
324 {
325 void __iomem *addr = pcie->reg_base + CDNS_PCIE_RP_BASE + reg;
326
327 cdns_pcie_write_sz(addr, 0x1, value);
328 }
329
330 static inline void cdns_pcie_rp_writew(struct cdns_pcie *pcie,
331 u32 reg, u16 value)
332 {
333 void __iomem *addr = pcie->reg_base + CDNS_PCIE_RP_BASE + reg;
334
335 cdns_pcie_write_sz(addr, 0x2, value);
336 }
337
338 static inline u16 cdns_pcie_rp_readw(struct cdns_pcie *pcie, u32 reg)
339 {
340 void __iomem *addr = pcie->reg_base + CDNS_PCIE_RP_BASE + reg;
341
342 return cdns_pcie_read_sz(addr, 0x2);
343 }
344
345 static inline void cdns_pcie_rp_writel(struct cdns_pcie *pcie,
346 u32 reg, u32 value)
347 {
348 void __iomem *addr = pcie->reg_base + CDNS_PCIE_RP_BASE + reg;
349
350 cdns_pcie_write_sz(addr, 0x4, value);
351 }
352
353 static inline u32 cdns_pcie_rp_readl(struct cdns_pcie *pcie, u32 reg)
354 {
355 void __iomem *addr = pcie->reg_base + CDNS_PCIE_RP_BASE + reg;
356
357 return cdns_pcie_read_sz(addr, 0x4);
358 }
359
360 static inline void cdns_pcie_hpa_rp_writeb(struct cdns_pcie *pcie,
361 u32 reg, u8 value)
362 {
363 void __iomem *addr = pcie->reg_base + CDNS_PCIE_HPA_RP_BASE + reg;
364
365 cdns_pcie_write_sz(addr, 0x1, value);
366 }
367
368 static inline void cdns_pcie_hpa_rp_writew(struct cdns_pcie *pcie,
369 u32 reg, u16 value)
370 {
371 void __iomem *addr = pcie->reg_base + CDNS_PCIE_HPA_RP_BASE + reg;
372
373 cdns_pcie_write_sz(addr, 0x2, value);
374 }
375
376 static inline u16 cdns_pcie_hpa_rp_readw(struct cdns_pcie *pcie, u32 reg)
377 {
378 void __iomem *addr = pcie->reg_base + CDNS_PCIE_HPA_RP_BASE + reg;
379
380 return cdns_pcie_read_sz(addr, 0x2);
381 }
382
383 /* Endpoint Function register access */
384 static inline void cdns_pcie_ep_fn_writeb(struct cdns_pcie *pcie, u8 fn,
385 u32 reg, u8 value)
386 {
387 void __iomem *addr = pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg;
388
389 cdns_pcie_write_sz(addr, 0x1, value);
390 }
391
392 static inline void cdns_pcie_ep_fn_writew(struct cdns_pcie *pcie, u8 fn,
393 u32 reg, u16 value)
394 {
395 void __iomem *addr = pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg;
396
397 cdns_pcie_write_sz(addr, 0x2, value);
398 }
399
400 static inline void cdns_pcie_ep_fn_writel(struct cdns_pcie *pcie, u8 fn,
401 u32 reg, u32 value)
402 {
403 writel(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
404 }
405
406 static inline u16 cdns_pcie_ep_fn_readw(struct cdns_pcie *pcie, u8 fn, u32 reg)
407 {
408 void __iomem *addr = pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg;
409
410 return cdns_pcie_read_sz(addr, 0x2);
411 }
412
413 static inline u32 cdns_pcie_ep_fn_readl(struct cdns_pcie *pcie, u8 fn, u32 reg)
414 {
415 return readl(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg);
416 }
417
418 static inline int cdns_pcie_start_link(struct cdns_pcie *pcie)
419 {
420 if (pcie->ops && pcie->ops->start_link)
421 return pcie->ops->start_link(pcie);
422
423 return 0;
424 }
425
426 static inline void cdns_pcie_stop_link(struct cdns_pcie *pcie)
427 {
428 if (pcie->ops && pcie->ops->stop_link)
429 pcie->ops->stop_link(pcie);
430 }
431
432 static inline bool cdns_pcie_link_up(struct cdns_pcie *pcie)
433 {
434 if (pcie->ops && pcie->ops->link_up)
435 return pcie->ops->link_up(pcie);
436
437 return true;
438 }
439
440 #if IS_ENABLED(CONFIG_PCIE_CADENCE_HOST)
441 int cdns_pcie_host_link_setup(struct cdns_pcie_rc *rc);
442 int cdns_pcie_host_init(struct cdns_pcie_rc *rc);
443 int cdns_pcie_host_setup(struct cdns_pcie_rc *rc);
444 void cdns_pcie_host_disable(struct cdns_pcie_rc *rc);
445 void __iomem *cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
446 int where);
447 int cdns_pcie_hpa_host_setup(struct cdns_pcie_rc *rc);
448 #else
449 static inline int cdns_pcie_host_link_setup(struct cdns_pcie_rc *rc)
450 {
451 return 0;
452 }
453
454 static inline int cdns_pcie_host_init(struct cdns_pcie_rc *rc)
455 {
456 return 0;
457 }
458
459 static inline int cdns_pcie_host_setup(struct cdns_pcie_rc *rc)
460 {
461 return 0;
462 }
463
464 static inline int cdns_pcie_hpa_host_setup(struct cdns_pcie_rc *rc)
465 {
466 return 0;
467 }
468
469 static inline void cdns_pcie_host_disable(struct cdns_pcie_rc *rc)
470 {
471 }
472
473 static inline void __iomem *cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
474 int where)
475 {
476 return NULL;
477 }
478 #endif
479
480 #if IS_ENABLED(CONFIG_PCIE_CADENCE_EP)
481 int cdns_pcie_ep_setup(struct cdns_pcie_ep *ep);
482 void cdns_pcie_ep_disable(struct cdns_pcie_ep *ep);
483 int cdns_pcie_hpa_ep_setup(struct cdns_pcie_ep *ep);
484 #else
485 static inline int cdns_pcie_ep_setup(struct cdns_pcie_ep *ep)
486 {
487 return 0;
488 }
489
490 static inline void cdns_pcie_ep_disable(struct cdns_pcie_ep *ep)
491 {
492 }
493
494 static inline int cdns_pcie_hpa_ep_setup(struct cdns_pcie_ep *ep)
495 {
496 return 0;
497 }
498
499 #endif
500
501 u8 cdns_pcie_find_capability(struct cdns_pcie *pcie, u8 cap);
502 u16 cdns_pcie_find_ext_capability(struct cdns_pcie *pcie, u8 cap);
503 bool cdns_pcie_linkup(struct cdns_pcie *pcie);
504
505 void cdns_pcie_detect_quiet_min_delay_set(struct cdns_pcie *pcie);
506
507 void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u8 busnr, u8 fn,
508 u32 r, bool is_io,
509 u64 cpu_addr, u64 pci_addr, size_t size);
510
511 void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie,
512 u8 busnr, u8 fn,
513 u32 r, u64 cpu_addr);
514
515 void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r);
516 void cdns_pcie_disable_phy(struct cdns_pcie *pcie);
517 int cdns_pcie_enable_phy(struct cdns_pcie *pcie);
518 int cdns_pcie_init_phy(struct device *dev, struct cdns_pcie *pcie);
519 void cdns_pcie_hpa_detect_quiet_min_delay_set(struct cdns_pcie *pcie);
520 void cdns_pcie_hpa_set_outbound_region(struct cdns_pcie *pcie, u8 busnr, u8 fn,
521 u32 r, bool is_io,
522 u64 cpu_addr, u64 pci_addr, size_t size);
523 void cdns_pcie_hpa_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie,
524 u8 busnr, u8 fn,
525 u32 r, u64 cpu_addr);
526 int cdns_pcie_hpa_host_link_setup(struct cdns_pcie_rc *rc);
527 void __iomem *cdns_pci_hpa_map_bus(struct pci_bus *bus, unsigned int devfn,
528 int where);
529 int cdns_pcie_hpa_host_start_link(struct cdns_pcie_rc *rc);
530 int cdns_pcie_hpa_start_link(struct cdns_pcie *pcie);
531 void cdns_pcie_hpa_stop_link(struct cdns_pcie *pcie);
532 bool cdns_pcie_hpa_link_up(struct cdns_pcie *pcie);
533
534 extern const struct dev_pm_ops cdns_pcie_pm_ops;
535
536 #endif /* _PCIE_CADENCE_H */