Ramon Fried | c2ccc9e | 2019-04-27 11:15:22 +0300 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * Cadence PCIe controlloer definitions |
| 4 | * Adapted from linux kernel driver. |
| 5 | * Copyright (c) 2017 Cadence |
| 6 | * |
| 7 | * Copyright (c) 2019 |
| 8 | * Written by Ramon Fried <ramon.fried@gmail.com> |
| 9 | */ |
| 10 | |
| 11 | #ifndef PCIE_CADENCE_H |
| 12 | #define PCIE_CADENCE_H |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <pci_ep.h> |
| 16 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Ramon Fried | c2ccc9e | 2019-04-27 11:15:22 +0300 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Local Management Registers |
| 21 | */ |
| 22 | #define CDNS_PCIE_LM_BASE 0x00100000 |
| 23 | |
| 24 | /* Vendor ID Register */ |
| 25 | #define CDNS_PCIE_LM_ID (CDNS_PCIE_LM_BASE + 0x0044) |
| 26 | #define CDNS_PCIE_LM_ID_VENDOR_MASK GENMASK(15, 0) |
| 27 | #define CDNS_PCIE_LM_ID_VENDOR_SHIFT 0 |
| 28 | #define CDNS_PCIE_LM_ID_VENDOR(vid) \ |
| 29 | (((vid) << CDNS_PCIE_LM_ID_VENDOR_SHIFT) & CDNS_PCIE_LM_ID_VENDOR_MASK) |
| 30 | #define CDNS_PCIE_LM_ID_SUBSYS_MASK GENMASK(31, 16) |
| 31 | #define CDNS_PCIE_LM_ID_SUBSYS_SHIFT 16 |
| 32 | #define CDNS_PCIE_LM_ID_SUBSYS(sub) \ |
| 33 | (((sub) << CDNS_PCIE_LM_ID_SUBSYS_SHIFT) & CDNS_PCIE_LM_ID_SUBSYS_MASK) |
| 34 | |
| 35 | /* Root Port Requestor ID Register */ |
| 36 | #define CDNS_PCIE_LM_RP_RID (CDNS_PCIE_LM_BASE + 0x0228) |
| 37 | #define CDNS_PCIE_LM_RP_RID_MASK GENMASK(15, 0) |
| 38 | #define CDNS_PCIE_LM_RP_RID_SHIFT 0 |
| 39 | #define CDNS_PCIE_LM_RP_RID_(rid) \ |
| 40 | (((rid) << CDNS_PCIE_LM_RP_RID_SHIFT) & CDNS_PCIE_LM_RP_RID_MASK) |
| 41 | |
| 42 | /* Endpoint Bus and Device Number Register */ |
| 43 | #define CDNS_PCIE_LM_EP_ID (CDNS_PCIE_LM_BASE + 0x022c) |
| 44 | #define CDNS_PCIE_LM_EP_ID_DEV_MASK GENMASK(4, 0) |
| 45 | #define CDNS_PCIE_LM_EP_ID_DEV_SHIFT 0 |
| 46 | #define CDNS_PCIE_LM_EP_ID_BUS_MASK GENMASK(15, 8) |
| 47 | #define CDNS_PCIE_LM_EP_ID_BUS_SHIFT 8 |
| 48 | |
| 49 | /* Endpoint Function f BAR b Configuration Registers */ |
| 50 | #define CDNS_PCIE_LM_EP_FUNC_BAR_CFG0(fn) \ |
| 51 | (CDNS_PCIE_LM_BASE + 0x0240 + (fn) * 0x0008) |
| 52 | #define CDNS_PCIE_LM_EP_FUNC_BAR_CFG1(fn) \ |
| 53 | (CDNS_PCIE_LM_BASE + 0x0244 + (fn) * 0x0008) |
| 54 | #define CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b) \ |
| 55 | (GENMASK(4, 0) << ((b) * 8)) |
| 56 | #define CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE(b, a) \ |
| 57 | (((a) << ((b) * 8)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_APERTURE_MASK(b)) |
| 58 | #define CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b) \ |
| 59 | (GENMASK(7, 5) << ((b) * 8)) |
| 60 | #define CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL(b, c) \ |
| 61 | (((c) << ((b) * 8 + 5)) & CDNS_PCIE_LM_EP_FUNC_BAR_CFG_BAR_CTRL_MASK(b)) |
| 62 | |
| 63 | /* Endpoint Function Configuration Register */ |
| 64 | #define CDNS_PCIE_LM_EP_FUNC_CFG (CDNS_PCIE_LM_BASE + 0x02c0) |
| 65 | |
| 66 | /* Root Complex BAR Configuration Register */ |
| 67 | #define CDNS_PCIE_LM_RC_BAR_CFG (CDNS_PCIE_LM_BASE + 0x0300) |
| 68 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK GENMASK(5, 0) |
| 69 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE(a) \ |
| 70 | (((a) << 0) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_APERTURE_MASK) |
| 71 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK GENMASK(8, 6) |
| 72 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(c) \ |
| 73 | (((c) << 6) & CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL_MASK) |
| 74 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK GENMASK(13, 9) |
| 75 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE(a) \ |
| 76 | (((a) << 9) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_APERTURE_MASK) |
| 77 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK GENMASK(16, 14) |
| 78 | #define CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(c) \ |
| 79 | (((c) << 14) & CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL_MASK) |
| 80 | #define CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE BIT(17) |
| 81 | #define CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_32BITS 0 |
| 82 | #define CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS BIT(18) |
| 83 | #define CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE BIT(19) |
| 84 | #define CDNS_PCIE_LM_RC_BAR_CFG_IO_16BITS 0 |
| 85 | #define CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS BIT(20) |
| 86 | #define CDNS_PCIE_LM_RC_BAR_CFG_CHECK_ENABLE BIT(31) |
| 87 | |
| 88 | /* BAR control values applicable to both Endpoint Function and Root Complex */ |
| 89 | #define CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED 0x0 |
| 90 | #define CDNS_PCIE_LM_BAR_CFG_CTRL_IO_32BITS 0x1 |
| 91 | #define CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_32BITS 0x4 |
| 92 | #define CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_32BITS 0x5 |
| 93 | #define CDNS_PCIE_LM_BAR_CFG_CTRL_MEM_64BITS 0x6 |
| 94 | #define CDNS_PCIE_LM_BAR_CFG_CTRL_PREFETCH_MEM_64BITS 0x7 |
| 95 | |
| 96 | /* |
| 97 | * Endpoint Function Registers (PCI configuration space for endpoint functions) |
| 98 | */ |
| 99 | #define CDNS_PCIE_EP_FUNC_BASE(fn) (((fn) << 12) & GENMASK(19, 12)) |
| 100 | |
| 101 | #define CDNS_PCIE_EP_FUNC_MSI_CAP_OFFSET 0x90 |
| 102 | |
| 103 | /* |
| 104 | * Root Port Registers (PCI configuration space for the root port function) |
| 105 | */ |
| 106 | #define CDNS_PCIE_RP_BASE 0x00200000 |
| 107 | |
| 108 | /* |
| 109 | * Address Translation Registers |
| 110 | */ |
| 111 | #define CDNS_PCIE_AT_BASE 0x00400000 |
| 112 | |
| 113 | /* Region r Outbound AXI to PCIe Address Translation Register 0 */ |
| 114 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r) \ |
| 115 | (CDNS_PCIE_AT_BASE + 0x0000 + ((r) & 0x1f) * 0x0020) |
| 116 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK GENMASK(5, 0) |
| 117 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) \ |
| 118 | (((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS_MASK) |
| 119 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK GENMASK(19, 12) |
| 120 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) \ |
| 121 | (((devfn) << 12) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN_MASK) |
| 122 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK GENMASK(27, 20) |
| 123 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(bus) \ |
| 124 | (((bus) << 20) & CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS_MASK) |
| 125 | |
| 126 | /* Region r Outbound AXI to PCIe Address Translation Register 1 */ |
| 127 | #define CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r) \ |
| 128 | (CDNS_PCIE_AT_BASE + 0x0004 + ((r) & 0x1f) * 0x0020) |
| 129 | |
| 130 | /* Region r Outbound PCIe Descriptor Register 0 */ |
| 131 | #define CDNS_PCIE_AT_OB_REGION_DESC0(r) \ |
| 132 | (CDNS_PCIE_AT_BASE + 0x0008 + ((r) & 0x1f) * 0x0020) |
| 133 | #define CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MASK GENMASK(3, 0) |
| 134 | #define CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM 0x2 |
| 135 | #define CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO 0x6 |
| 136 | #define CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0 0xa |
| 137 | #define CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1 0xb |
| 138 | #define CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG 0xc |
| 139 | #define CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_VENDOR_MSG 0xd |
| 140 | /* Bit 23 MUST be set in RC mode. */ |
| 141 | #define CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID BIT(23) |
| 142 | #define CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK GENMASK(31, 24) |
| 143 | #define CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(devfn) \ |
| 144 | (((devfn) << 24) & CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN_MASK) |
| 145 | |
| 146 | /* Region r Outbound PCIe Descriptor Register 1 */ |
| 147 | #define CDNS_PCIE_AT_OB_REGION_DESC1(r) \ |
| 148 | (CDNS_PCIE_AT_BASE + 0x000c + ((r) & 0x1f) * 0x0020) |
| 149 | #define CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK GENMASK(7, 0) |
| 150 | #define CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus) \ |
| 151 | ((bus) & CDNS_PCIE_AT_OB_REGION_DESC1_BUS_MASK) |
| 152 | |
| 153 | /* Region r AXI Region Base Address Register 0 */ |
| 154 | #define CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r) \ |
| 155 | (CDNS_PCIE_AT_BASE + 0x0018 + ((r) & 0x1f) * 0x0020) |
| 156 | #define CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK GENMASK(5, 0) |
| 157 | #define CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) \ |
| 158 | (((nbits) - 1) & CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS_MASK) |
| 159 | |
| 160 | /* Region r AXI Region Base Address Register 1 */ |
| 161 | #define CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r) \ |
| 162 | (CDNS_PCIE_AT_BASE + 0x001c + ((r) & 0x1f) * 0x0020) |
| 163 | |
| 164 | /* Root Port BAR Inbound PCIe to AXI Address Translation Register */ |
| 165 | #define CDNS_PCIE_AT_IB_RP_BAR_ADDR0(bar) \ |
| 166 | (CDNS_PCIE_AT_BASE + 0x0800 + (bar) * 0x0008) |
| 167 | #define CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK GENMASK(5, 0) |
| 168 | #define CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(nbits) \ |
| 169 | (((nbits) - 1) & CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS_MASK) |
| 170 | #define CDNS_PCIE_AT_IB_RP_BAR_ADDR1(bar) \ |
| 171 | (CDNS_PCIE_AT_BASE + 0x0804 + (bar) * 0x0008) |
| 172 | |
| 173 | /* AXI link down register */ |
| 174 | #define CDNS_PCIE_AT_LINKDOWN (CDNS_PCIE_AT_BASE + 0x0824) |
| 175 | |
| 176 | enum cdns_pcie_rp_bar { |
| 177 | RP_BAR0, |
| 178 | RP_BAR1, |
| 179 | RP_NO_BAR |
| 180 | }; |
| 181 | |
| 182 | /* Endpoint Function BAR Inbound PCIe to AXI Address Translation Register */ |
| 183 | #define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR0(fn, bar) \ |
| 184 | (CDNS_PCIE_AT_BASE + 0x0840 + (fn) * 0x0040 + (bar) * 0x0008) |
| 185 | #define CDNS_PCIE_AT_IB_EP_FUNC_BAR_ADDR1(fn, bar) \ |
| 186 | (CDNS_PCIE_AT_BASE + 0x0844 + (fn) * 0x0040 + (bar) * 0x0008) |
| 187 | |
| 188 | /* Normal/Vendor specific message access: offset inside some outbound region */ |
| 189 | #define CDNS_PCIE_NORMAL_MSG_ROUTING_MASK GENMASK(7, 5) |
| 190 | #define CDNS_PCIE_NORMAL_MSG_ROUTING(route) \ |
| 191 | (((route) << 5) & CDNS_PCIE_NORMAL_MSG_ROUTING_MASK) |
| 192 | #define CDNS_PCIE_NORMAL_MSG_CODE_MASK GENMASK(15, 8) |
| 193 | #define CDNS_PCIE_NORMAL_MSG_CODE(code) \ |
| 194 | (((code) << 8) & CDNS_PCIE_NORMAL_MSG_CODE_MASK) |
| 195 | #define CDNS_PCIE_MSG_NO_DATA BIT(16) |
| 196 | |
| 197 | #define CDNS_PCIE_EP_MIN_APERTURE 128 /* 128 bytes */ |
| 198 | |
| 199 | enum cdns_pcie_msg_code { |
| 200 | MSG_CODE_ASSERT_INTA = 0x20, |
| 201 | MSG_CODE_ASSERT_INTB = 0x21, |
| 202 | MSG_CODE_ASSERT_INTC = 0x22, |
| 203 | MSG_CODE_ASSERT_INTD = 0x23, |
| 204 | MSG_CODE_DEASSERT_INTA = 0x24, |
| 205 | MSG_CODE_DEASSERT_INTB = 0x25, |
| 206 | MSG_CODE_DEASSERT_INTC = 0x26, |
| 207 | MSG_CODE_DEASSERT_INTD = 0x27, |
| 208 | }; |
| 209 | |
| 210 | enum cdns_pcie_msg_routing { |
| 211 | /* Route to Root Complex */ |
| 212 | MSG_ROUTING_TO_RC, |
| 213 | |
| 214 | /* Use Address Routing */ |
| 215 | MSG_ROUTING_BY_ADDR, |
| 216 | |
| 217 | /* Use ID Routing */ |
| 218 | MSG_ROUTING_BY_ID, |
| 219 | |
| 220 | /* Route as Broadcast Message from Root Complex */ |
| 221 | MSG_ROUTING_BCAST, |
| 222 | |
| 223 | /* Local message; terminate at receiver (INTx messages) */ |
| 224 | MSG_ROUTING_LOCAL, |
| 225 | |
| 226 | /* Gather & route to Root Complex (PME_TO_Ack message) */ |
| 227 | MSG_ROUTING_GATHER, |
| 228 | }; |
| 229 | |
| 230 | struct cdns_pcie { |
| 231 | void __iomem *reg_base; |
| 232 | u32 max_functions; |
| 233 | u32 max_regions; |
| 234 | }; |
| 235 | |
| 236 | /* Register access */ |
| 237 | static inline void cdns_pcie_writeb(struct cdns_pcie *pcie, u32 reg, u8 value) |
| 238 | { |
| 239 | writeb(value, pcie->reg_base + reg); |
| 240 | } |
| 241 | |
| 242 | static inline void cdns_pcie_writew(struct cdns_pcie *pcie, u32 reg, u16 value) |
| 243 | { |
| 244 | writew(value, pcie->reg_base + reg); |
| 245 | } |
| 246 | |
| 247 | static inline void cdns_pcie_writel(struct cdns_pcie *pcie, u32 reg, u32 value) |
| 248 | { |
| 249 | writel(value, pcie->reg_base + reg); |
| 250 | } |
| 251 | |
| 252 | static inline u32 cdns_pcie_readl(struct cdns_pcie *pcie, u32 reg) |
| 253 | { |
| 254 | return readl(pcie->reg_base + reg); |
| 255 | } |
| 256 | |
| 257 | /* Root Port register access */ |
| 258 | static inline void cdns_pcie_rp_writeb(struct cdns_pcie *pcie, |
| 259 | u32 reg, u8 value) |
| 260 | { |
| 261 | writeb(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg); |
| 262 | } |
| 263 | |
| 264 | static inline void cdns_pcie_rp_writew(struct cdns_pcie *pcie, |
| 265 | u32 reg, u16 value) |
| 266 | { |
| 267 | writew(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg); |
| 268 | } |
| 269 | |
| 270 | static inline void cdns_pcie_rp_writel(struct cdns_pcie *pcie, |
| 271 | u32 reg, u32 value) |
| 272 | { |
| 273 | writel(value, pcie->reg_base + CDNS_PCIE_RP_BASE + reg); |
| 274 | } |
| 275 | |
| 276 | /* Endpoint Function register access */ |
| 277 | static inline void cdns_pcie_ep_fn_writeb(struct cdns_pcie *pcie, u8 fn, |
| 278 | u32 reg, u8 value) |
| 279 | { |
| 280 | writeb(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg); |
| 281 | } |
| 282 | |
| 283 | static inline void cdns_pcie_ep_fn_writew(struct cdns_pcie *pcie, u8 fn, |
| 284 | u32 reg, u16 value) |
| 285 | { |
| 286 | writew(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg); |
| 287 | } |
| 288 | |
| 289 | static inline void cdns_pcie_ep_fn_writel(struct cdns_pcie *pcie, u8 fn, |
| 290 | u32 reg, u32 value) |
| 291 | { |
| 292 | writel(value, pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg); |
| 293 | } |
| 294 | |
| 295 | static inline u8 cdns_pcie_ep_fn_readb(struct cdns_pcie *pcie, u8 fn, u32 reg) |
| 296 | { |
| 297 | return readb(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg); |
| 298 | } |
| 299 | |
| 300 | static inline u16 cdns_pcie_ep_fn_readw(struct cdns_pcie *pcie, u8 fn, u32 reg) |
| 301 | { |
| 302 | return readw(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg); |
| 303 | } |
| 304 | |
| 305 | static inline u32 cdns_pcie_ep_fn_readl(struct cdns_pcie *pcie, u8 fn, u32 reg) |
| 306 | { |
| 307 | return readl(pcie->reg_base + CDNS_PCIE_EP_FUNC_BASE(fn) + reg); |
| 308 | } |
| 309 | |
| 310 | #endif /* end of include guard: PCIE_CADENCE_H */ |