blob: 6ccd1333f2e8fc71f437f71bffc4a3850829a2df [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001/* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
2/*
3 * Amlogic AXI PCIe controller driver
4 *
5 * Copyright (c) 2018 Amlogic, Inc.
6 *
7 */
8
9#ifndef _PCIE_AMLOGIC_V3_H
10#define _PCIE_AMLOGIC_V3_H
11#include <linux/pci.h>
12#include <linux/kernel.h>
13/*
14 * The upper 16 bits of PCIE_CLIENT_CONFIG are a write mask for the lower 16
15 * bits. This allows atomic updates of the register without locking.
16 */
17#define PCIE_BASIC_STATUS 0x018
18#define LINK_UP_MASK 0xff
19
20#define PCIE_CFGCTRL 0x084
21#define PCIE_PCI_IDS0 0x098
22#define PCIE_PCI_IDS1 0x09c
23#define PCIE_PCI_IDS2 0x0a0
24#define PCIE_PCI_IRQ 0x0a8
25#define PCIE_BAR_0 0x0e4
26#define PCIE_BAR_1 0x0e8
27#define PCIE_BAR_2 0x0ec
28#define PCIE_BAR_3 0x0f0
29#define PCIE_BAR_4 0x0f4
30#define PCIE_BAR_5 0x0f8
31#define PCIE_BAR_IO BIT(1)
32#define PCIE_BAR_MEM_TYPE_64 BIT(2)
33#define PCIE_BAR_MEM_TYPE_PREFETCH BIT(3)
34#define PCIE_BAR_MEM_MASK (~0x0fUL)
35#define PCIE_BAR_IO_MASK (~0x03UL)
36
37#define PCIE_CFGNUM 0x140
38#define IMASK_LOCAL 0x180
39#define ISTATUS_LOCAL 0x184
40#define IMASK_HOST 0x188
41#define ISTATUS_HOST 0x18c
42#define IMSI_ADDR 0x190
43#define ISTATUS_MSI 0x194
44#define ATR_PCIE_WIN0 0x600
45#define ATR_PCIE_WIN1 0x700
46#define ATR_AXI4_SLV0 0x800
47
48#define ATR_TABLE_SIZE 0x20
49#define ATR_SRC_ADDR_LOW 0x0
50#define ATR_SRC_ADDR_HIGH 0x4
51#define ATR_TRSL_ADDR_LOW 0x8
52#define ATR_TRSL_ADDR_HIGH 0xc
53#define ATR_TRSL_PARAM 0x10
54
55#define ATR_TRSLID_AXIDEVICE (0x420004)
56/* Write-through, read/write allocate */
57#define ATR_TRSLID_AXIMEMORY (0x4e0004)
58#define ATR_TRSLID_PCIE_CONF (0x000001)
59#define ATR_TRSLID_PCIE_IO (0x020000)
60/*#define ATR_TRSLID_PCIE_IO (0x020001)*/
61#define ATR_TRSLID_PCIE_MEMORY (0x000000)
62
63#define INT_AXI_POST_ERROR BIT(16)
64#define INT_AXI_FETCH_ERROR BIT(17)
65#define INT_AXI_DISCARD_ERROR BIT(18)
66#define INT_PCIE_POST_ERROR BIT(20)
67#define INT_PCIE_FETCH_ERROR BIT(21)
68#define INT_PCIE_DISCARD_ERROR BIT(22)
69#define INT_ERRORS (INT_AXI_POST_ERROR | INT_AXI_FETCH_ERROR | \
70 INT_AXI_DISCARD_ERROR | INT_PCIE_POST_ERROR | \
71 INT_PCIE_FETCH_ERROR | INT_PCIE_DISCARD_ERROR)
72
73#define INTA_OFFSET 24
74#define INTA BIT(24)
75#define INTB BIT(25)
76#define INTC BIT(26)
77#define INTD BIT(27)
78#define INT_MSI BIT(28)
79#define INT_INTX_MASK (INTA | INTB | INTC | INTD)
80#define INT_MASK (INT_INTX_MASK | INT_MSI | INT_ERRORS)
81
82#define INTX_NUM 4
83#define INT_PCI_MSI_NR 32
84
85#define DWORD_MASK 3
86
87#define PCI_CFG_SPACE 0x1000
88#define PCIE_HEADER_TYPE_OFFSET (PCI_CFG_SPACE + 0x00)
89#define PCIE_CAP_OFFSET (PCI_CFG_SPACE + 0x80)
90
91#define PCIE_ECAM_BUS(x) (((x) & 0xff) << 20)
92#define PCIE_ECAM_DEV(x) (((x) & 0x1f) << 15)
93#define PCIE_ECAM_FUNC(x) (((x) & 0x7) << 12)
94#define PCIE_ECAM_REG(x) (((x) & 0xfff) << 0)
95#define PCIE_ECAM_ADDR(bus, dev, func, reg) \
96 (PCIE_ECAM_BUS(bus) | PCIE_ECAM_DEV(dev) | \
97 PCIE_ECAM_FUNC(func) | PCIE_ECAM_REG(reg))
98
99#define PCIE_A_CTRL0 0x0
100#define PORT_TYPE BIT(0)
101#define PCIE_A_CTRL1 0x4
102#define PCIE_A_CTRL2 0x8
103#define PCIE_A_CTRL3 0xc
104#define PCIE_A_CTRL4 0x10
105#define PCIE_A_CTRL5 0x14
106#define PCIE_A_CTRL6 0x18
107
108#define EP_BASE_OFFSET 0
109#define AMLOGIC_PCIE_EP_FUNC_BASE(fn) (((fn) << 12) & GENMASK(19, 12))
110#define EP_FUNC_MSI_CAP_OFFSET 0x0e0
111
112#define RESETCTRL1_OFFSET 0x4
113#define RESETCTRL3_OFFSET 0xc
114
115#define WAIT_LINKUP_TIMEOUT 9000
116
117enum pcie_data_rate {
118 PCIE_GEN1,
119 PCIE_GEN2,
120 PCIE_GEN3,
121 PCIE_GEN4
122};
123
124struct amlogic_pcie {
125 void __iomem *apb_base; /* pcie_axi_lite for internal*/
126 void __iomem *axi_base; /* pcie_ctrl for data*/
127 void __iomem *pcictrl_base; /* pcie_A_ctrl for oneself*/
128 void __iomem *phy_base; /* pcie_gen3_phy*/
129 void __iomem *rst_base; /* reset_ctrl*/
130 void __iomem *ecam_base; /* ecam*/
131 phys_addr_t ecam_bus_base;
132 u32 ecam_size;
133
134 struct resource *io;
135 phys_addr_t io_bus_addr;
136 u32 io_size;
137 u32 mem_size;
138 phys_addr_t mem_bus_addr;
139
140 struct pcie_phy *phy;
141
142 int reset_gpio;
143 u32 gpio_type;
144
145 struct clk *pcie_400m_clk; /*CLKCTRL_USB_CLK_CTRL/1*/
146 struct clk *pcie_tl_clk;
147 struct clk *cts_pcie_clk;
148 struct clk *pcie_clk;
149 struct clk *phy_clk;
150 struct clk *refpll_clk;
151 struct clk *dev_clk;
152
153 struct reset_control *m31phy_rst;/*RESETCTRL_RESET1 bit 21*/
154 struct reset_control *gen3_l0_rst; /*RESETCTRL_RESET1 bit 18*/
155 struct reset_control *pcie_apb_rst; /*RESETCTRL_RESET1 bit 14*/
156 struct reset_control *pcie_phy_rst; /*RESETCTRL_RESET1 bit 13*/
157 struct reset_control *pcie_a_rst; /*RESETCTRL_RESET1 bit 12*/
158 struct reset_control *pcie_rst0; /*RESETCTRL_RESET3 bit 12*/
159 struct reset_control *pcie_rst1; /*RESETCTRL_RESET3 bit 13*/
160 struct reset_control *pcie_rst2; /*RESETCTRL_RESET3 bit 14*/
161 struct reset_control *pcie_rst3; /*RESETCTRL_RESET3 bit 15*/
162 struct reset_control *pcie_rst4; /*RESETCTRL_RESET3 bit 16*/
163 struct reset_control *pcie_rst5; /*RESETCTRL_RESET3 bit 17*/
164 struct reset_control *pcie_rst6; /*RESETCTRL_RESET3 bit 18*/
165 struct reset_control *pcie_rst7; /*RESETCTRL_RESET3 bit 19*/
166
167 struct device *dev;
168
169 struct pinctrl *p;
170
171 u32 m31phy_rst_bit;
172 u32 gen3_l0_rst_bit;
173 u32 apb_rst_bit;
174 u32 phy_rst_bit;
175 u32 pcie_a_rst_bit;
176 u32 pcie_rst_bit;
177 u32 pcie_rst_mask;
178
179 bool is_rc;
180
181 u32 lanes;
182
183 u8 lanes_map;
184 int link_gen;
185 int offset;
186 struct resource *mem_res;
187};
188
189#define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
190
191static inline u32 amlogic_pcie_read_sz(void __iomem *addr, int size)
192{
193 void __iomem *aligned_addr = PTR_ALIGN_DOWN(addr, 0x4);
194 unsigned int offset = (unsigned long)addr & 0x3;
195 u32 val = readl(aligned_addr);
196
197 if (!IS_ALIGNED((uintptr_t)addr, size)) {
198 pr_warn("Address %p and size %d are not aligned\n", addr, size);
199 return 0;
200 }
201
202 if (size > 2)
203 return val;
204
205 return (val >> (8 * offset)) & ((1 << (size * 8)) - 1);
206}
207
208static inline void amlogic_pcie_write_sz(void __iomem *addr, int size,
209 u32 value)
210{
211 void __iomem *aligned_addr = PTR_ALIGN_DOWN(addr, 0x4);
212 unsigned int offset = (unsigned long)addr & 0x3;
213 u32 mask;
214 u32 val;
215
216 if (!IS_ALIGNED((uintptr_t)addr, size)) {
217 pr_warn("Address %p and size %d are not aligned\n", addr, size);
218 return;
219 }
220
221 if (size > 2) {
222 writel(value, addr);
223 return;
224 }
225
226 mask = ~(((1 << (size * 8)) - 1) << (offset * 8));
227 val = readl(aligned_addr) & mask;
228 val |= value << (offset * 8);
229 writel(val, aligned_addr);
230}
231
232static inline void amlogic_pcie_ep_fn_writeb(struct amlogic_pcie *pcie, u8 fn,
233 u32 reg, u8 value)
234{
235 void __iomem *addr = pcie->ecam_base + EP_BASE_OFFSET +
236 AMLOGIC_PCIE_EP_FUNC_BASE(fn) + reg;
237
238 amlogic_pcie_write_sz(addr, 0x1, value);
239}
240
241static inline void amlogic_pcie_ep_fn_writew(struct amlogic_pcie *pcie, u8 fn,
242 u32 reg, u16 value)
243{
244 void __iomem *addr = pcie->ecam_base + EP_BASE_OFFSET +
245 AMLOGIC_PCIE_EP_FUNC_BASE(fn) + reg;
246
247 amlogic_pcie_write_sz(addr, 0x2, value);
248}
249
250static inline void amlogic_pcie_ep_fn_writel(struct amlogic_pcie *pcie, u8 fn,
251 u32 reg, u32 value)
252{
253 writel(value, pcie->ecam_base + EP_BASE_OFFSET +
254 AMLOGIC_PCIE_EP_FUNC_BASE(fn) + reg);
255}
256
257static inline u16 amlogic_pcie_ep_fn_readw(struct amlogic_pcie *pcie,
258 u8 fn, u32 reg)
259{
260 void __iomem *addr = pcie->ecam_base + EP_BASE_OFFSET +
261 AMLOGIC_PCIE_EP_FUNC_BASE(fn) + reg;
262
263 return amlogic_pcie_read_sz(addr, 0x2);
264}
265
266static inline u32 amlogic_pcie_ep_fn_readl(struct amlogic_pcie *pcie,
267 u8 fn, u32 reg)
268{
269 return readl(pcie->ecam_base + EP_BASE_OFFSET +
270 AMLOGIC_PCIE_EP_FUNC_BASE(fn) + reg);
271}
272
273static inline u32 amlogic_pcieinter_read(struct amlogic_pcie *pcie, u32 reg)
274{
275 if (reg < PCI_CFG_SPACE)
276 return readl(pcie->apb_base + reg);
277 else
278 return readl(pcie->ecam_base +
279 PCIE_ECAM_ADDR(0, 0, 0, reg - PCI_CFG_SPACE));
280}
281
282static inline void amlogic_pcieinter_write(struct amlogic_pcie *pcie, u32 val,
283 u32 reg)
284{
285 if (reg < PCI_CFG_SPACE)
286 writel(val, pcie->apb_base + reg);
287 else
288 writel(val, pcie->ecam_base +
289 PCIE_ECAM_ADDR(0, 0, 0, reg - PCI_CFG_SPACE));
290}
291
292static inline u32 amlogic_pciectrl_read(struct amlogic_pcie *pcie, u32 reg)
293{
294 return readl(pcie->pcictrl_base + reg);
295}
296
297static inline void amlogic_pciectrl_write(struct amlogic_pcie *pcie, u32 val,
298 u32 reg)
299{
300 writel(val, pcie->pcictrl_base + reg);
301}
302
303int amlogic_pcie_set_reset(struct amlogic_pcie *amlogic, bool set);
304int amlogic_pcie_parse_dt(struct amlogic_pcie *amlogic);
305int amlogic_pcie_init_port(struct amlogic_pcie *amlogic);
306int amlogic_pcie_get_phys(struct amlogic_pcie *amlogic);
307void amlogic_pcie_deinit_phys(struct amlogic_pcie *amlogic);
308int amlogic_pcie_enable_clocks(struct amlogic_pcie *amlogic);
309void amlogic_pcie_disable_clocks(struct amlogic_pcie *amlogic);
310bool amlogic_pcie_link_up(struct amlogic_pcie *amlogic);
311void amlogic_set_max_rd_req_size(struct amlogic_pcie *amlogic, int size);
312void amlogic_set_max_payload(struct amlogic_pcie *amlogic, int size);
313void amlogic_pcie_cfg_addr_map(struct amlogic_pcie *amlogic,
314 unsigned int atr_base,
315 u64 src_addr,
316 u64 trsl_addr,
317 int size,
318 int trsl_param);
319
320#endif /* _PCIE_AMLOGIC_V3_H */