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