blob: e0f7796f2a873f91ef74bc6ce84966a4215c22f0 [file] [log] [blame]
Neil Armstrongdfadb942021-03-25 15:49:18 +01001/* 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
Ben Dooks4c56d752022-10-20 16:51:09 +010035#define PCIE_ATU_UNR_UPPER_LIMIT 0x20
Neil Armstrongdfadb942021-03-25 15:49:18 +010036
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
70/* Synopsys-specific PCIe configuration registers */
71#define PCIE_PORT_LINK_CONTROL 0x710
72#define PORT_LINK_DLL_LINK_EN BIT(5)
73#define PORT_LINK_FAST_LINK_MODE BIT(7)
74#define PORT_LINK_MODE_MASK GENMASK(21, 16)
75#define PORT_LINK_MODE(n) FIELD_PREP(PORT_LINK_MODE_MASK, n)
76#define PORT_LINK_MODE_1_LANES PORT_LINK_MODE(0x1)
77#define PORT_LINK_MODE_2_LANES PORT_LINK_MODE(0x3)
78#define PORT_LINK_MODE_4_LANES PORT_LINK_MODE(0x7)
79#define PORT_LINK_MODE_8_LANES PORT_LINK_MODE(0xf)
80
81#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
82#define PORT_LOGIC_N_FTS_MASK GENMASK(7, 0)
83#define PORT_LOGIC_SPEED_CHANGE BIT(17)
84#define PORT_LOGIC_LINK_WIDTH_MASK GENMASK(12, 8)
85#define PORT_LOGIC_LINK_WIDTH(n) FIELD_PREP(PORT_LOGIC_LINK_WIDTH_MASK, n)
86#define PORT_LOGIC_LINK_WIDTH_1_LANES PORT_LOGIC_LINK_WIDTH(0x1)
87#define PORT_LOGIC_LINK_WIDTH_2_LANES PORT_LOGIC_LINK_WIDTH(0x2)
88#define PORT_LOGIC_LINK_WIDTH_4_LANES PORT_LOGIC_LINK_WIDTH(0x4)
89#define PORT_LOGIC_LINK_WIDTH_8_LANES PORT_LOGIC_LINK_WIDTH(0x8)
90
91#define PCIE_MISC_CONTROL_1_OFF 0x8bc
92#define PCIE_DBI_RO_WR_EN BIT(0)
93
94/* Parameters for the waiting for iATU enabled routine */
95#define LINK_WAIT_MAX_IATU_RETRIES 5
96#define LINK_WAIT_IATU 10000
97
98/**
99 * struct pcie_dw - DW PCIe controller state
100 *
101 * @dbi_base: The base address of dbi register space
102 * @cfg_base: The base address of configuration space
103 * @atu_base: The base address of ATU space
104 * @cfg_size: The size of the configuration space which is needed
105 * as it gets written into the PCIE_ATU_LIMIT register
106 * @first_busno: This driver supports multiple PCIe controllers.
107 * first_busno stores the bus number of the PCIe root-port
108 * number which may vary depending on the PCIe setup
109 * (PEX switches etc).
110 * @io: The IO space for EP's BAR
111 * @mem: The memory space for EP's BAR
112 * @prefetch: The prefetch space for EP's BAR
113 */
114struct pcie_dw {
115 struct udevice *dev;
116 void __iomem *dbi_base;
117 void __iomem *cfg_base;
118 void __iomem *atu_base;
119 fdt_size_t cfg_size;
120
121 int first_busno;
122
123 /* IO, MEM & PREFETCH PCI regions */
124 struct pci_region io;
125 struct pci_region mem;
126 struct pci_region prefetch;
127};
128
129int pcie_dw_get_link_speed(struct pcie_dw *pci);
130
131int pcie_dw_get_link_width(struct pcie_dw *pci);
132
133int pcie_dw_prog_outbound_atu_unroll(struct pcie_dw *pci, int index, int type, u64 cpu_addr,
134 u64 pci_addr, u32 size);
135
136int pcie_dw_read_config(const struct udevice *bus, pci_dev_t bdf, uint offset, ulong *valuep,
137 enum pci_size_t size);
138
139int pcie_dw_write_config(struct udevice *bus, pci_dev_t bdf, uint offset, ulong value,
140 enum pci_size_t size);
141
142static inline void dw_pcie_dbi_write_enable(struct pcie_dw *pci, bool en)
143{
144 u32 val;
145
146 val = readl(pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
147 if (en)
148 val |= PCIE_DBI_RO_WR_EN;
149 else
150 val &= ~PCIE_DBI_RO_WR_EN;
151 writel(val, pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
152}
153
154void pcie_dw_setup_host(struct pcie_dw *pci);
155
156#endif