1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (c) 2021 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 *
6 * Copyright (c) 2021 Rockchip, Inc.
7 *
8 * Copyright (C) 2018 Texas Instruments, Inc
9 */
10
11 #ifndef PCIE_DW_COMMON_H
12 #define PCIE_DW_COMMON_H
13
14 #define DEFAULT_DBI_ATU_OFFSET (0x3 << 20)
15
16 /* PCI DBICS registers */
17 #define PCIE_LINK_STATUS_REG 0x80
18 #define PCIE_LINK_STATUS_SPEED_OFF 16
19 #define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
20 #define PCIE_LINK_STATUS_WIDTH_OFF 20
21 #define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
22
23 /*
24 * iATU Unroll-specific register definitions
25 * From 4.80 core version the address translation will be made by unroll.
26 * The registers are offset from atu_base
27 */
28 #define PCIE_ATU_UNR_REGION_CTRL1 0x00
29 #define PCIE_ATU_UNR_REGION_CTRL2 0x04
30 #define PCIE_ATU_UNR_LOWER_BASE 0x08
31 #define PCIE_ATU_UNR_UPPER_BASE 0x0c
32 #define PCIE_ATU_UNR_LIMIT 0x10
33 #define PCIE_ATU_UNR_LOWER_TARGET 0x14
34 #define PCIE_ATU_UNR_UPPER_TARGET 0x18
35 #define PCIE_ATU_UNR_UPPER_LIMIT 0x20
36
37 #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
38 #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
39 #define PCIE_ATU_TYPE_MEM (0x0 << 0)
40 #define PCIE_ATU_TYPE_IO (0x2 << 0)
41 #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
42 #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
43 #define PCIE_ATU_ENABLE (0x1 << 31)
44 #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
45 #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
46 #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
47 #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
48
49 /* Register address builder */
50 #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) ((region) << 9)
51
52 /* Parameters for the waiting for iATU enabled routine */
53 #define LINK_WAIT_MAX_IATU_RETRIES 5
54 #define LINK_WAIT_IATU_US 10000
55
56 /* PCI DBICS registers */
57 #define PCIE_LINK_STATUS_REG 0x80
58 #define PCIE_LINK_STATUS_SPEED_OFF 16
59 #define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
60 #define PCIE_LINK_STATUS_WIDTH_OFF 20
61 #define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
62
63 #define PCIE_LINK_CAPABILITY 0x7c
64 #define PCIE_LINK_CTL_2 0xa0
65 #define TARGET_LINK_SPEED_MASK 0xf
66 #define LINK_SPEED_GEN_1 0x1
67 #define LINK_SPEED_GEN_2 0x2
68 #define LINK_SPEED_GEN_3 0x3
69 #define LINK_SPEED_GEN_4 0x4
70
71 /* Synopsys-specific PCIe configuration registers */
72 #define PCIE_PORT_FORCE 0x708
73 #define PORT_FORCE_DO_DESKEW_FOR_SRIS BIT(23)
74
75 #define PCIE_PORT_LINK_CONTROL 0x710
76 #define PORT_LINK_DLL_LINK_EN BIT(5)
77 #define PORT_LINK_FAST_LINK_MODE BIT(7)
78 #define PORT_LINK_MODE_MASK GENMASK(21, 16)
79 #define PORT_LINK_MODE(n) FIELD_PREP(PORT_LINK_MODE_MASK, n)
80 #define PORT_LINK_MODE_1_LANES PORT_LINK_MODE(0x1)
81 #define PORT_LINK_MODE_2_LANES PORT_LINK_MODE(0x3)
82 #define PORT_LINK_MODE_4_LANES PORT_LINK_MODE(0x7)
83 #define PORT_LINK_MODE_8_LANES PORT_LINK_MODE(0xf)
84
85 #define PCIE_PORT_LANE_SKEW 0x714
86 #define PORT_LANE_SKEW_INSERT_MASK GENMASK(23, 0)
87
88 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
89 #define PORT_LOGIC_N_FTS_MASK GENMASK(7, 0)
90 #define PORT_LOGIC_SPEED_CHANGE BIT(17)
91 #define PORT_LOGIC_LINK_WIDTH_MASK GENMASK(12, 8)
92 #define PORT_LOGIC_LINK_WIDTH(n) FIELD_PREP(PORT_LOGIC_LINK_WIDTH_MASK, n)
93 #define PORT_LOGIC_LINK_WIDTH_1_LANES PORT_LOGIC_LINK_WIDTH(0x1)
94 #define PORT_LOGIC_LINK_WIDTH_2_LANES PORT_LOGIC_LINK_WIDTH(0x2)
95 #define PORT_LOGIC_LINK_WIDTH_4_LANES PORT_LOGIC_LINK_WIDTH(0x4)
96 #define PORT_LOGIC_LINK_WIDTH_8_LANES PORT_LOGIC_LINK_WIDTH(0x8)
97
98 #define PCIE_MISC_CONTROL_1_OFF 0x8bc
99 #define PCIE_DBI_RO_WR_EN BIT(0)
100
101 /* Parameters for the waiting for iATU enabled routine */
102 #define LINK_WAIT_MAX_IATU_RETRIES 5
103 #define LINK_WAIT_IATU 10000
104
105 /**
106 * struct pcie_dw - DW PCIe controller state
107 *
108 * @dbi_base: The base address of dbi register space
109 * @cfg_base: The base address of configuration space
110 * @atu_base: The base address of ATU space
111 * @cfg_size: The size of the configuration space which is needed
112 * as it gets written into the PCIE_ATU_LIMIT register
113 * @first_busno: This driver supports multiple PCIe controllers.
114 * first_busno stores the bus number of the PCIe root-port
115 * number which may vary depending on the PCIe setup
116 * (PEX switches etc).
117 * @io: The IO space for EP's BAR
118 * @mem: The memory space for EP's BAR
119 * @prefetch: The prefetch space for EP's BAR
120 */
121 struct pcie_dw {
122 struct udevice *dev;
123 void __iomem *dbi_base;
124 void __iomem *cfg_base;
125 void __iomem *atu_base;
126 fdt_size_t cfg_size;
127
128 int first_busno;
129
130 /* IO, MEM & PREFETCH PCI regions */
131 struct pci_region io;
132 struct pci_region mem;
133 struct pci_region prefetch;
134 };
135
136 int pcie_dw_get_link_speed(struct pcie_dw *pci);
137
138 int pcie_dw_get_link_width(struct pcie_dw *pci);
139
140 void dw_pcie_link_set_max_link_width(struct pcie_dw *pci, u32 num_lanes);
141
142 int pcie_dw_prog_outbound_atu_unroll(struct pcie_dw *pci, int index, int type, u64 cpu_addr,
143 u64 pci_addr, u32 size);
144
145 int pcie_dw_read_config(const struct udevice *bus, pci_dev_t bdf, uint offset, ulong *valuep,
146 enum pci_size_t size);
147
148 int pcie_dw_write_config(struct udevice *bus, pci_dev_t bdf, uint offset, ulong value,
149 enum pci_size_t size);
150
151 u8 pcie_dw_find_capability(struct pcie_dw *pci, u8 cap);
152
dw_pcie_dbi_write_enable(struct pcie_dw * pci,bool en)153 static inline void dw_pcie_dbi_write_enable(struct pcie_dw *pci, bool en)
154 {
155 u32 val;
156
157 val = readl(pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
158 if (en)
159 val |= PCIE_DBI_RO_WR_EN;
160 else
161 val &= ~PCIE_DBI_RO_WR_EN;
162 writel(val, pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
163 }
164
165 void pcie_dw_setup_host(struct pcie_dw *pci);
166
167 #endif
168