Commit | Line | Data |
---|---|---|
8cfab3cf | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
4b1ced84 | 2 | /* |
96291d56 | 3 | * Synopsys DesignWare PCIe host controller driver |
4b1ced84 JH |
4 | * |
5 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. | |
6 | * http://www.samsung.com | |
7 | * | |
8 | * Author: Jingoo Han <jg1.han@samsung.com> | |
4b1ced84 JH |
9 | */ |
10 | ||
18edf451 SJ |
11 | #ifndef _PCIE_DESIGNWARE_H |
12 | #define _PCIE_DESIGNWARE_H | |
13 | ||
111111a7 | 14 | #include <linux/dma-mapping.h> |
feb85d9b KVA |
15 | #include <linux/irq.h> |
16 | #include <linux/msi.h> | |
17 | #include <linux/pci.h> | |
18 | ||
f8aed6ec KVA |
19 | #include <linux/pci-epc.h> |
20 | #include <linux/pci-epf.h> | |
21 | ||
b90dc392 KVA |
22 | /* Parameters for the waiting for link up routine */ |
23 | #define LINK_WAIT_MAX_RETRIES 10 | |
24 | #define LINK_WAIT_USLEEP_MIN 90000 | |
25 | #define LINK_WAIT_USLEEP_MAX 100000 | |
26 | ||
27 | /* Parameters for the waiting for iATU enabled routine */ | |
28 | #define LINK_WAIT_MAX_IATU_RETRIES 5 | |
29 | #define LINK_WAIT_IATU_MIN 9000 | |
30 | #define LINK_WAIT_IATU_MAX 10000 | |
31 | ||
32 | /* Synopsys-specific PCIe configuration registers */ | |
33 | #define PCIE_PORT_LINK_CONTROL 0x710 | |
34 | #define PORT_LINK_MODE_MASK (0x3f << 16) | |
35 | #define PORT_LINK_MODE_1_LANES (0x1 << 16) | |
36 | #define PORT_LINK_MODE_2_LANES (0x3 << 16) | |
37 | #define PORT_LINK_MODE_4_LANES (0x7 << 16) | |
38 | #define PORT_LINK_MODE_8_LANES (0xf << 16) | |
39 | ||
40 | #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C | |
41 | #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17) | |
42 | #define PORT_LOGIC_LINK_WIDTH_MASK (0x1f << 8) | |
43 | #define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8) | |
44 | #define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8) | |
45 | #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8) | |
46 | #define PORT_LOGIC_LINK_WIDTH_8_LANES (0x8 << 8) | |
47 | ||
48 | #define PCIE_MSI_ADDR_LO 0x820 | |
49 | #define PCIE_MSI_ADDR_HI 0x824 | |
50 | #define PCIE_MSI_INTR0_ENABLE 0x828 | |
51 | #define PCIE_MSI_INTR0_MASK 0x82C | |
52 | #define PCIE_MSI_INTR0_STATUS 0x830 | |
53 | ||
54 | #define PCIE_ATU_VIEWPORT 0x900 | |
55 | #define PCIE_ATU_REGION_INBOUND (0x1 << 31) | |
56 | #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31) | |
57 | #define PCIE_ATU_REGION_INDEX2 (0x2 << 0) | |
58 | #define PCIE_ATU_REGION_INDEX1 (0x1 << 0) | |
59 | #define PCIE_ATU_REGION_INDEX0 (0x0 << 0) | |
60 | #define PCIE_ATU_CR1 0x904 | |
61 | #define PCIE_ATU_TYPE_MEM (0x0 << 0) | |
62 | #define PCIE_ATU_TYPE_IO (0x2 << 0) | |
63 | #define PCIE_ATU_TYPE_CFG0 (0x4 << 0) | |
64 | #define PCIE_ATU_TYPE_CFG1 (0x5 << 0) | |
65 | #define PCIE_ATU_CR2 0x908 | |
66 | #define PCIE_ATU_ENABLE (0x1 << 31) | |
67 | #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30) | |
68 | #define PCIE_ATU_LOWER_BASE 0x90C | |
69 | #define PCIE_ATU_UPPER_BASE 0x910 | |
70 | #define PCIE_ATU_LIMIT 0x914 | |
71 | #define PCIE_ATU_LOWER_TARGET 0x918 | |
72 | #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24) | |
73 | #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19) | |
74 | #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16) | |
75 | #define PCIE_ATU_UPPER_TARGET 0x91C | |
76 | ||
e44abfed HZ |
77 | #define PCIE_MISC_CONTROL_1_OFF 0x8BC |
78 | #define PCIE_DBI_RO_WR_EN (0x1 << 0) | |
79 | ||
b90dc392 KVA |
80 | /* |
81 | * iATU Unroll-specific register definitions | |
82 | * From 4.80 core version the address translation will be made by unroll | |
83 | */ | |
84 | #define PCIE_ATU_UNR_REGION_CTRL1 0x00 | |
85 | #define PCIE_ATU_UNR_REGION_CTRL2 0x04 | |
86 | #define PCIE_ATU_UNR_LOWER_BASE 0x08 | |
87 | #define PCIE_ATU_UNR_UPPER_BASE 0x0C | |
88 | #define PCIE_ATU_UNR_LIMIT 0x10 | |
89 | #define PCIE_ATU_UNR_LOWER_TARGET 0x14 | |
90 | #define PCIE_ATU_UNR_UPPER_TARGET 0x18 | |
91 | ||
92 | /* Register address builder */ | |
93 | #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) \ | |
94 | ((0x3 << 20) | ((region) << 9)) | |
95 | ||
f8aed6ec KVA |
96 | #define PCIE_GET_ATU_INB_UNR_REG_OFFSET(region) \ |
97 | ((0x3 << 20) | ((region) << 9) | (0x1 << 8)) | |
98 | ||
99 | #define MSI_MESSAGE_CONTROL 0x52 | |
100 | #define MSI_CAP_MMC_SHIFT 1 | |
099a95f3 | 101 | #define MSI_CAP_MMC_MASK (7 << MSI_CAP_MMC_SHIFT) |
f8aed6ec | 102 | #define MSI_CAP_MME_SHIFT 4 |
a134a457 | 103 | #define MSI_CAP_MSI_EN_MASK 0x1 |
f8aed6ec KVA |
104 | #define MSI_CAP_MME_MASK (7 << MSI_CAP_MME_SHIFT) |
105 | #define MSI_MESSAGE_ADDR_L32 0x54 | |
106 | #define MSI_MESSAGE_ADDR_U32 0x58 | |
6f6d7873 NC |
107 | #define MSI_MESSAGE_DATA_32 0x58 |
108 | #define MSI_MESSAGE_DATA_64 0x5C | |
f8aed6ec | 109 | |
f342d940 JH |
110 | /* |
111 | * Maximum number of MSI IRQs can be 256 per controller. But keep | |
112 | * it 32 as of now. Probably we will never need more than 32. If needed, | |
113 | * then increment it in multiple of 32. | |
114 | */ | |
115 | #define MAX_MSI_IRQS 32 | |
116 | #define MAX_MSI_CTRLS (MAX_MSI_IRQS / 32) | |
117 | ||
ad4a5bec NC |
118 | /* Maximum number of inbound/outbound iATUs */ |
119 | #define MAX_IATU_IN 256 | |
120 | #define MAX_IATU_OUT 256 | |
121 | ||
442ec4c0 KVA |
122 | struct pcie_port; |
123 | struct dw_pcie; | |
f8aed6ec KVA |
124 | struct dw_pcie_ep; |
125 | ||
126 | enum dw_pcie_region_type { | |
127 | DW_PCIE_REGION_UNKNOWN, | |
128 | DW_PCIE_REGION_INBOUND, | |
129 | DW_PCIE_REGION_OUTBOUND, | |
130 | }; | |
442ec4c0 | 131 | |
608793e2 KVA |
132 | enum dw_pcie_device_mode { |
133 | DW_PCIE_UNKNOWN_TYPE, | |
134 | DW_PCIE_EP_TYPE, | |
135 | DW_PCIE_LEG_EP_TYPE, | |
136 | DW_PCIE_RC_TYPE, | |
137 | }; | |
138 | ||
442ec4c0 KVA |
139 | struct dw_pcie_host_ops { |
140 | int (*rd_own_conf)(struct pcie_port *pp, int where, int size, u32 *val); | |
141 | int (*wr_own_conf)(struct pcie_port *pp, int where, int size, u32 val); | |
142 | int (*rd_other_conf)(struct pcie_port *pp, struct pci_bus *bus, | |
143 | unsigned int devfn, int where, int size, u32 *val); | |
144 | int (*wr_other_conf)(struct pcie_port *pp, struct pci_bus *bus, | |
145 | unsigned int devfn, int where, int size, u32 val); | |
4a301766 | 146 | int (*host_init)(struct pcie_port *pp); |
442ec4c0 KVA |
147 | void (*msi_set_irq)(struct pcie_port *pp, int irq); |
148 | void (*msi_clear_irq)(struct pcie_port *pp, int irq); | |
149 | phys_addr_t (*get_msi_addr)(struct pcie_port *pp); | |
150 | u32 (*get_msi_data)(struct pcie_port *pp, int pos); | |
151 | void (*scan_bus)(struct pcie_port *pp); | |
152 | int (*msi_host_init)(struct pcie_port *pp, struct msi_controller *chip); | |
153 | }; | |
154 | ||
4b1ced84 | 155 | struct pcie_port { |
4b1ced84 | 156 | u8 root_bus_nr; |
4b1ced84 JH |
157 | u64 cfg0_base; |
158 | void __iomem *va_cfg0_base; | |
adf70fc0 | 159 | u32 cfg0_size; |
4b1ced84 JH |
160 | u64 cfg1_base; |
161 | void __iomem *va_cfg1_base; | |
adf70fc0 | 162 | u32 cfg1_size; |
0021d22b | 163 | resource_size_t io_base; |
adf70fc0 PA |
164 | phys_addr_t io_bus_addr; |
165 | u32 io_size; | |
4b1ced84 | 166 | u64 mem_base; |
adf70fc0 PA |
167 | phys_addr_t mem_bus_addr; |
168 | u32 mem_size; | |
0021d22b ZW |
169 | struct resource *cfg; |
170 | struct resource *io; | |
171 | struct resource *mem; | |
172 | struct resource *busn; | |
4b1ced84 | 173 | int irq; |
4ab2e7c0 | 174 | const struct dw_pcie_host_ops *ops; |
f342d940 | 175 | int msi_irq; |
904d0e78 | 176 | struct irq_domain *irq_domain; |
111111a7 | 177 | dma_addr_t msi_data; |
f342d940 | 178 | DECLARE_BITMAP(msi_irq_in_use, MAX_MSI_IRQS); |
4b1ced84 JH |
179 | }; |
180 | ||
f8aed6ec KVA |
181 | enum dw_pcie_as_type { |
182 | DW_PCIE_AS_UNKNOWN, | |
183 | DW_PCIE_AS_MEM, | |
184 | DW_PCIE_AS_IO, | |
185 | }; | |
186 | ||
187 | struct dw_pcie_ep_ops { | |
188 | void (*ep_init)(struct dw_pcie_ep *ep); | |
16093362 BH |
189 | int (*raise_irq)(struct dw_pcie_ep *ep, u8 func_no, |
190 | enum pci_epc_irq_type type, u8 interrupt_num); | |
f8aed6ec KVA |
191 | }; |
192 | ||
193 | struct dw_pcie_ep { | |
194 | struct pci_epc *epc; | |
195 | struct dw_pcie_ep_ops *ops; | |
196 | phys_addr_t phys_base; | |
197 | size_t addr_size; | |
a937fe08 | 198 | size_t page_size; |
f8aed6ec KVA |
199 | u8 bar_to_atu[6]; |
200 | phys_addr_t *outbound_addr; | |
ad4a5bec NC |
201 | unsigned long *ib_window_map; |
202 | unsigned long *ob_window_map; | |
f8aed6ec KVA |
203 | u32 num_ib_windows; |
204 | u32 num_ob_windows; | |
2fd0c9d9 NC |
205 | void __iomem *msi_mem; |
206 | phys_addr_t msi_mem_phys; | |
f8aed6ec KVA |
207 | }; |
208 | ||
442ec4c0 | 209 | struct dw_pcie_ops { |
b6900aeb | 210 | u64 (*cpu_addr_fixup)(struct dw_pcie *pcie, u64 cpu_addr); |
a509d7d9 KVA |
211 | u32 (*read_dbi)(struct dw_pcie *pcie, void __iomem *base, u32 reg, |
212 | size_t size); | |
213 | void (*write_dbi)(struct dw_pcie *pcie, void __iomem *base, u32 reg, | |
214 | size_t size, u32 val); | |
442ec4c0 | 215 | int (*link_up)(struct dw_pcie *pcie); |
f8aed6ec KVA |
216 | int (*start_link)(struct dw_pcie *pcie); |
217 | void (*stop_link)(struct dw_pcie *pcie); | |
4b1ced84 JH |
218 | }; |
219 | ||
442ec4c0 KVA |
220 | struct dw_pcie { |
221 | struct device *dev; | |
222 | void __iomem *dbi_base; | |
f8aed6ec | 223 | void __iomem *dbi_base2; |
442ec4c0 KVA |
224 | u32 num_viewport; |
225 | u8 iatu_unroll_enabled; | |
226 | struct pcie_port pp; | |
f8aed6ec | 227 | struct dw_pcie_ep ep; |
442ec4c0 KVA |
228 | const struct dw_pcie_ops *ops; |
229 | }; | |
230 | ||
231 | #define to_dw_pcie_from_pp(port) container_of((port), struct dw_pcie, pp) | |
232 | ||
f8aed6ec KVA |
233 | #define to_dw_pcie_from_ep(endpoint) \ |
234 | container_of((endpoint), struct dw_pcie, ep) | |
235 | ||
19ce01cc KVA |
236 | int dw_pcie_read(void __iomem *addr, int size, u32 *val); |
237 | int dw_pcie_write(void __iomem *addr, int size, u32 val); | |
18edf451 | 238 | |
a509d7d9 KVA |
239 | u32 __dw_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg, |
240 | size_t size); | |
241 | void __dw_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg, | |
242 | size_t size, u32 val); | |
442ec4c0 KVA |
243 | int dw_pcie_link_up(struct dw_pcie *pci); |
244 | int dw_pcie_wait_for_link(struct dw_pcie *pci); | |
feb85d9b KVA |
245 | void dw_pcie_prog_outbound_atu(struct dw_pcie *pci, int index, |
246 | int type, u64 cpu_addr, u64 pci_addr, | |
247 | u32 size); | |
f8aed6ec KVA |
248 | int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, int index, int bar, |
249 | u64 cpu_addr, enum dw_pcie_as_type as_type); | |
250 | void dw_pcie_disable_atu(struct dw_pcie *pci, int index, | |
251 | enum dw_pcie_region_type type); | |
feb85d9b | 252 | void dw_pcie_setup(struct dw_pcie *pci); |
a0560209 | 253 | |
b50b2db2 KVA |
254 | static inline void dw_pcie_writel_dbi(struct dw_pcie *pci, u32 reg, u32 val) |
255 | { | |
a509d7d9 | 256 | __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x4, val); |
b50b2db2 KVA |
257 | } |
258 | ||
259 | static inline u32 dw_pcie_readl_dbi(struct dw_pcie *pci, u32 reg) | |
260 | { | |
a509d7d9 | 261 | return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x4); |
b50b2db2 KVA |
262 | } |
263 | ||
f8aed6ec KVA |
264 | static inline void dw_pcie_writew_dbi(struct dw_pcie *pci, u32 reg, u16 val) |
265 | { | |
266 | __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x2, val); | |
267 | } | |
268 | ||
269 | static inline u16 dw_pcie_readw_dbi(struct dw_pcie *pci, u32 reg) | |
270 | { | |
271 | return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x2); | |
272 | } | |
273 | ||
274 | static inline void dw_pcie_writeb_dbi(struct dw_pcie *pci, u32 reg, u8 val) | |
275 | { | |
276 | __dw_pcie_write_dbi(pci, pci->dbi_base, reg, 0x1, val); | |
277 | } | |
278 | ||
279 | static inline u8 dw_pcie_readb_dbi(struct dw_pcie *pci, u32 reg) | |
280 | { | |
281 | return __dw_pcie_read_dbi(pci, pci->dbi_base, reg, 0x1); | |
282 | } | |
283 | ||
284 | static inline void dw_pcie_writel_dbi2(struct dw_pcie *pci, u32 reg, u32 val) | |
285 | { | |
286 | __dw_pcie_write_dbi(pci, pci->dbi_base2, reg, 0x4, val); | |
287 | } | |
288 | ||
289 | static inline u32 dw_pcie_readl_dbi2(struct dw_pcie *pci, u32 reg) | |
290 | { | |
291 | return __dw_pcie_read_dbi(pci, pci->dbi_base2, reg, 0x4); | |
292 | } | |
293 | ||
e44abfed HZ |
294 | static inline void dw_pcie_dbi_ro_wr_en(struct dw_pcie *pci) |
295 | { | |
296 | u32 reg; | |
297 | u32 val; | |
298 | ||
299 | reg = PCIE_MISC_CONTROL_1_OFF; | |
300 | val = dw_pcie_readl_dbi(pci, reg); | |
301 | val |= PCIE_DBI_RO_WR_EN; | |
302 | dw_pcie_writel_dbi(pci, reg, val); | |
303 | } | |
304 | ||
305 | static inline void dw_pcie_dbi_ro_wr_dis(struct dw_pcie *pci) | |
306 | { | |
307 | u32 reg; | |
308 | u32 val; | |
309 | ||
310 | reg = PCIE_MISC_CONTROL_1_OFF; | |
311 | val = dw_pcie_readl_dbi(pci, reg); | |
312 | val &= ~PCIE_DBI_RO_WR_EN; | |
313 | dw_pcie_writel_dbi(pci, reg, val); | |
314 | } | |
315 | ||
a0560209 KVA |
316 | #ifdef CONFIG_PCIE_DW_HOST |
317 | irqreturn_t dw_handle_msi_irq(struct pcie_port *pp); | |
318 | void dw_pcie_msi_init(struct pcie_port *pp); | |
319 | void dw_pcie_setup_rc(struct pcie_port *pp); | |
320 | int dw_pcie_host_init(struct pcie_port *pp); | |
321 | #else | |
322 | static inline irqreturn_t dw_handle_msi_irq(struct pcie_port *pp) | |
323 | { | |
324 | return IRQ_NONE; | |
325 | } | |
326 | ||
327 | static inline void dw_pcie_msi_init(struct pcie_port *pp) | |
328 | { | |
329 | } | |
330 | ||
331 | static inline void dw_pcie_setup_rc(struct pcie_port *pp) | |
332 | { | |
333 | } | |
334 | ||
335 | static inline int dw_pcie_host_init(struct pcie_port *pp) | |
336 | { | |
337 | return 0; | |
338 | } | |
339 | #endif | |
f8aed6ec KVA |
340 | |
341 | #ifdef CONFIG_PCIE_DW_EP | |
342 | void dw_pcie_ep_linkup(struct dw_pcie_ep *ep); | |
343 | int dw_pcie_ep_init(struct dw_pcie_ep *ep); | |
344 | void dw_pcie_ep_exit(struct dw_pcie_ep *ep); | |
16093362 BH |
345 | int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no, |
346 | u8 interrupt_num); | |
9e718119 | 347 | void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar); |
f8aed6ec KVA |
348 | #else |
349 | static inline void dw_pcie_ep_linkup(struct dw_pcie_ep *ep) | |
350 | { | |
351 | } | |
352 | ||
353 | static inline int dw_pcie_ep_init(struct dw_pcie_ep *ep) | |
354 | { | |
355 | return 0; | |
356 | } | |
357 | ||
358 | static inline void dw_pcie_ep_exit(struct dw_pcie_ep *ep) | |
359 | { | |
360 | } | |
9e718119 | 361 | |
16093362 | 362 | static inline int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no, |
6f6d7873 NC |
363 | u8 interrupt_num) |
364 | { | |
365 | return 0; | |
366 | } | |
367 | ||
9e718119 NC |
368 | static inline void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar) |
369 | { | |
370 | } | |
f8aed6ec | 371 | #endif |
18edf451 | 372 | #endif /* _PCIE_DESIGNWARE_H */ |