blob: 082fdc3b74da0db7755d7517e380e3b0db7548d4 [file] [log] [blame]
Wilson Dinga6bdc862018-03-26 15:57:29 +08001/*
2 * ***************************************************************************
3 * Copyright (C) 2015 Marvell International Ltd.
4 * ***************************************************************************
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation, either version 2 of the License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * ***************************************************************************
17 */
18/* pcie_advk.c
19 *
20 * Ported from Linux driver - driver/pci/host/pci-aardvark.c
21 *
22 * Author: Victor Gu <xigu@marvell.com>
23 * Hezi Shahmoon <hezi.shahmoon@marvell.com>
24 *
25 */
26
27#include <common.h>
28#include <dm.h>
29#include <pci.h>
30#include <asm/io.h>
31#include <asm-generic/gpio.h>
Simon Glass9bc15642020-02-03 07:36:16 -070032#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060033#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060034#include <linux/delay.h>
Wilson Dinga6bdc862018-03-26 15:57:29 +080035#include <linux/ioport.h>
36
37/* PCIe core registers */
38#define PCIE_CORE_CMD_STATUS_REG 0x4
39#define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
40#define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
41#define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
Pali Rohár525886e2021-09-26 00:54:42 +020042#define PCIE_CORE_DEV_REV_REG 0x8
43#define PCIE_CORE_EXP_ROM_BAR_REG 0x30
Wilson Dinga6bdc862018-03-26 15:57:29 +080044#define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
45#define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
46#define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
Pali Rohár9057a2c2021-02-05 15:32:28 +010047#define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE 0x2
48#define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT 5
49#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE 0x2
50#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
Wilson Dinga6bdc862018-03-26 15:57:29 +080051#define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
52#define PCIE_CORE_LINK_TRAINING BIT(5)
53#define PCIE_CORE_ERR_CAPCTL_REG 0x118
54#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
55#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
56#define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK BIT(7)
57#define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV BIT(8)
58
59/* PIO registers base address and register offsets */
60#define PIO_BASE_ADDR 0x4000
61#define PIO_CTRL (PIO_BASE_ADDR + 0x0)
62#define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
63#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
64#define PIO_STAT (PIO_BASE_ADDR + 0x4)
65#define PIO_COMPLETION_STATUS_SHIFT 7
66#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
67#define PIO_COMPLETION_STATUS_OK 0
68#define PIO_COMPLETION_STATUS_UR 1
69#define PIO_COMPLETION_STATUS_CRS 2
70#define PIO_COMPLETION_STATUS_CA 4
71#define PIO_NON_POSTED_REQ BIT(10)
72#define PIO_ERR_STATUS BIT(11)
73#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
74#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
75#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
76#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
77#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
78#define PIO_START (PIO_BASE_ADDR + 0x1c)
79#define PIO_ISR (PIO_BASE_ADDR + 0x20)
80
81/* Aardvark Control registers */
82#define CONTROL_BASE_ADDR 0x4800
83#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
84#define PCIE_GEN_SEL_MSK 0x3
85#define PCIE_GEN_SEL_SHIFT 0x0
86#define SPEED_GEN_1 0
87#define SPEED_GEN_2 1
88#define SPEED_GEN_3 2
89#define IS_RC_MSK 1
90#define IS_RC_SHIFT 2
91#define LANE_CNT_MSK 0x18
92#define LANE_CNT_SHIFT 0x3
93#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
94#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
95#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
96#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
97#define LINK_TRAINING_EN BIT(6)
98#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
99#define PCIE_CORE_CTRL2_RESERVED 0x7
100#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
101#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
102#define PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE BIT(6)
103
Pali Roháre78c8e02021-05-26 17:59:40 +0200104/* PCIe window configuration */
105#define OB_WIN_BASE_ADDR 0x4c00
106#define OB_WIN_BLOCK_SIZE 0x20
107#define OB_WIN_COUNT 8
108#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
109 OB_WIN_BLOCK_SIZE * (win) + \
110 (offset))
111#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
112#define OB_WIN_ENABLE BIT(0)
113#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
114#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
115#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
116#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
117#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
118#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
119#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
120#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
121#define OB_WIN_FUNC_NUM_SHIFT 24
122#define OB_WIN_FUNC_NUM_ENABLE BIT(23)
123#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
124#define OB_WIN_BUS_NUM_BITS_SHIFT 20
125#define OB_WIN_MSG_CODE_ENABLE BIT(22)
126#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
127#define OB_WIN_MSG_CODE_SHIFT 14
128#define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
129#define OB_WIN_ATTR_ENABLE BIT(11)
130#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
131#define OB_WIN_ATTR_TC_SHIFT 8
132#define OB_WIN_ATTR_RELAXED BIT(7)
133#define OB_WIN_ATTR_NOSNOOP BIT(6)
134#define OB_WIN_ATTR_POISON BIT(5)
135#define OB_WIN_ATTR_IDO BIT(4)
136#define OB_WIN_TYPE_MASK GENMASK(3, 0)
137#define OB_WIN_TYPE_SHIFT 0
138#define OB_WIN_TYPE_MEM 0x0
139#define OB_WIN_TYPE_IO 0x4
140#define OB_WIN_TYPE_CONFIG_TYPE0 0x8
141#define OB_WIN_TYPE_CONFIG_TYPE1 0x9
142#define OB_WIN_TYPE_MSG 0xc
143
Wilson Dinga6bdc862018-03-26 15:57:29 +0800144/* LMI registers base address and register offsets */
145#define LMI_BASE_ADDR 0x6000
146#define CFG_REG (LMI_BASE_ADDR + 0x0)
147#define LTSSM_SHIFT 24
148#define LTSSM_MASK 0x3f
149#define LTSSM_L0 0x10
Pali Rohár6d11d9e2021-09-26 00:54:41 +0200150#define LTSSM_DISABLED 0x20
Pali Rohárba40b6c2021-03-03 14:37:59 +0100151#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800152
153/* PCIe core controller registers */
154#define CTRL_CORE_BASE_ADDR 0x18000
155#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
156#define CTRL_MODE_SHIFT 0x0
157#define CTRL_MODE_MASK 0x1
158#define PCIE_CORE_MODE_DIRECT 0x0
159#define PCIE_CORE_MODE_COMMAND 0x1
160
161/* Transaction types */
162#define PCIE_CONFIG_RD_TYPE0 0x8
163#define PCIE_CONFIG_RD_TYPE1 0x9
164#define PCIE_CONFIG_WR_TYPE0 0xa
165#define PCIE_CONFIG_WR_TYPE1 0xb
166
167/* PCI_BDF shifts 8bit, so we need extra 4bit shift */
Pali Rohár525886e2021-09-26 00:54:42 +0200168#define PCIE_BDF(b, d, f) (PCI_BDF(b, d, f) << 4)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800169#define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
170#define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
171#define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
172#define PCIE_CONF_REG(reg) ((reg) & 0xffc)
173#define PCIE_CONF_ADDR(bus, devfn, where) \
174 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
175 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
176
177/* PCIe Retries & Timeout definitions */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200178#define PIO_MAX_RETRIES 1500
179#define PIO_WAIT_TIMEOUT 1000
180#define LINK_MAX_RETRIES 10
Wilson Dinga6bdc862018-03-26 15:57:29 +0800181#define LINK_WAIT_TIMEOUT 100000
182
Wilson Dinga6bdc862018-03-26 15:57:29 +0800183#define CFG_RD_CRS_VAL 0xFFFF0001
184
Wilson Dinga6bdc862018-03-26 15:57:29 +0800185/**
186 * struct pcie_advk - Advk PCIe controller state
187 *
188 * @reg_base: The base address of the register space.
189 * @first_busno: This driver supports multiple PCIe controllers.
190 * first_busno stores the bus number of the PCIe root-port
191 * number which may vary depending on the PCIe setup
192 * (PEX switches etc).
Pali Rohár525886e2021-09-26 00:54:42 +0200193 * @sec_busno: sec_busno stores the bus number for the device behind
194 * the PCIe root-port
Wilson Dinga6bdc862018-03-26 15:57:29 +0800195 * @device: The pointer to PCI uclass device.
196 */
197struct pcie_advk {
198 void *base;
199 int first_busno;
Pali Rohár525886e2021-09-26 00:54:42 +0200200 int sec_busno;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800201 struct udevice *dev;
Pali Rohár6a58b972020-08-19 15:57:07 +0200202 struct gpio_desc reset_gpio;
Pali Rohár525886e2021-09-26 00:54:42 +0200203 u32 cfgcache[0x34 - 0x10];
Wilson Dinga6bdc862018-03-26 15:57:29 +0800204};
205
206static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg)
207{
208 writel(val, pcie->base + reg);
209}
210
211static inline uint advk_readl(struct pcie_advk *pcie, uint reg)
212{
213 return readl(pcie->base + reg);
214}
215
216/**
217 * pcie_advk_addr_valid() - Check for valid bus address
218 *
Pali Rohár525886e2021-09-26 00:54:42 +0200219 * @pcie: Pointer to the PCI bus
220 * @busno: Bus number of PCI device
221 * @dev: Device number of PCI device
222 * @func: Function number of PCI device
Wilson Dinga6bdc862018-03-26 15:57:29 +0800223 * @bdf: The PCI device to access
Wilson Dinga6bdc862018-03-26 15:57:29 +0800224 *
Pali Rohár525886e2021-09-26 00:54:42 +0200225 * Return: true on valid, false on invalid
Wilson Dinga6bdc862018-03-26 15:57:29 +0800226 */
Pali Rohár525886e2021-09-26 00:54:42 +0200227static bool pcie_advk_addr_valid(struct pcie_advk *pcie,
228 int busno, u8 dev, u8 func)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800229{
Pali Rohár525886e2021-09-26 00:54:42 +0200230 /* On the primary (local) bus there is only one PCI Bridge */
231 if (busno == pcie->first_busno && (dev != 0 || func != 0))
232 return false;
233
Wilson Dinga6bdc862018-03-26 15:57:29 +0800234 /*
Pali Rohár525886e2021-09-26 00:54:42 +0200235 * In PCI-E only a single device (0) can exist on the secondary bus.
236 * Beyond the secondary bus, there might be a Switch and anything is
237 * possible.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800238 */
Pali Rohár525886e2021-09-26 00:54:42 +0200239 if (busno == pcie->sec_busno && dev != 0)
240 return false;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800241
Pali Rohár525886e2021-09-26 00:54:42 +0200242 return true;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800243}
244
245/**
246 * pcie_advk_wait_pio() - Wait for PIO access to be accomplished
247 *
248 * @pcie: The PCI device to access
249 *
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200250 * Wait up to 1.5 seconds for PIO access to be accomplished.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800251 *
Pali Rohár907cd9a2021-08-27 14:14:44 +0200252 * Return positive - retry count if PIO access is accomplished.
253 * Return negative - error if PIO access is timed out.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800254 */
255static int pcie_advk_wait_pio(struct pcie_advk *pcie)
256{
257 uint start, isr;
258 uint count;
259
Pali Rohár907cd9a2021-08-27 14:14:44 +0200260 for (count = 1; count <= PIO_MAX_RETRIES; count++) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800261 start = advk_readl(pcie, PIO_START);
262 isr = advk_readl(pcie, PIO_ISR);
263 if (!start && isr)
Pali Rohár907cd9a2021-08-27 14:14:44 +0200264 return count;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800265 /*
266 * Do not check the PIO state too frequently,
267 * 100us delay is appropriate.
268 */
269 udelay(PIO_WAIT_TIMEOUT);
270 }
271
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200272 dev_err(pcie->dev, "PIO read/write transfer time out\n");
Pali Rohár907cd9a2021-08-27 14:14:44 +0200273 return -ETIMEDOUT;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800274}
275
276/**
277 * pcie_advk_check_pio_status() - Validate PIO status and get the read result
278 *
279 * @pcie: Pointer to the PCI bus
Pali Rohárb8344cf2021-08-09 09:53:13 +0200280 * @allow_crs: Only for read requests, if CRS response is allowed
281 * @read_val: Pointer to the read result
Wilson Dinga6bdc862018-03-26 15:57:29 +0800282 *
Pali Rohár907cd9a2021-08-27 14:14:44 +0200283 * Return: 0 on success
Wilson Dinga6bdc862018-03-26 15:57:29 +0800284 */
285static int pcie_advk_check_pio_status(struct pcie_advk *pcie,
Pali Rohárb8344cf2021-08-09 09:53:13 +0200286 bool allow_crs,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800287 uint *read_val)
288{
Pali Rohár907cd9a2021-08-27 14:14:44 +0200289 int ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800290 uint reg;
291 unsigned int status;
292 char *strcomp_status, *str_posted;
293
294 reg = advk_readl(pcie, PIO_STAT);
295 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
296 PIO_COMPLETION_STATUS_SHIFT;
297
298 switch (status) {
299 case PIO_COMPLETION_STATUS_OK:
300 if (reg & PIO_ERR_STATUS) {
301 strcomp_status = "COMP_ERR";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200302 ret = -EFAULT;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800303 break;
304 }
305 /* Get the read result */
Pali Rohárb8344cf2021-08-09 09:53:13 +0200306 if (read_val)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800307 *read_val = advk_readl(pcie, PIO_RD_DATA);
308 /* No error */
309 strcomp_status = NULL;
Pali Rohár907cd9a2021-08-27 14:14:44 +0200310 ret = 0;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800311 break;
312 case PIO_COMPLETION_STATUS_UR:
Pali Rohárb8344cf2021-08-09 09:53:13 +0200313 strcomp_status = "UR";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200314 ret = -EOPNOTSUPP;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800315 break;
316 case PIO_COMPLETION_STATUS_CRS:
Pali Rohárb8344cf2021-08-09 09:53:13 +0200317 if (allow_crs && read_val) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800318 /* For reading, CRS is not an error status. */
319 *read_val = CFG_RD_CRS_VAL;
320 strcomp_status = NULL;
Pali Rohár907cd9a2021-08-27 14:14:44 +0200321 ret = 0;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800322 } else {
323 strcomp_status = "CRS";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200324 ret = -EAGAIN;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800325 }
326 break;
327 case PIO_COMPLETION_STATUS_CA:
328 strcomp_status = "CA";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200329 ret = -ECANCELED;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800330 break;
331 default:
332 strcomp_status = "Unknown";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200333 ret = -EINVAL;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800334 break;
335 }
336
337 if (!strcomp_status)
Pali Rohár907cd9a2021-08-27 14:14:44 +0200338 return ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800339
340 if (reg & PIO_NON_POSTED_REQ)
341 str_posted = "Non-posted";
342 else
343 str_posted = "Posted";
344
Marek Behún73d776a2021-09-07 17:27:08 +0200345 dev_dbg(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
Wilson Dinga6bdc862018-03-26 15:57:29 +0800346 str_posted, strcomp_status, reg,
347 advk_readl(pcie, PIO_ADDR_LS));
348
Pali Rohár907cd9a2021-08-27 14:14:44 +0200349 return ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800350}
351
352/**
353 * pcie_advk_read_config() - Read from configuration space
354 *
355 * @bus: Pointer to the PCI bus
356 * @bdf: Identifies the PCIe device to access
357 * @offset: The offset into the device's configuration space
358 * @valuep: A pointer at which to store the read value
359 * @size: Indicates the size of access to perform
360 *
361 * Read a value of size @size from offset @offset within the configuration
362 * space of the device identified by the bus, device & function numbers in @bdf
363 * on the PCI bus @bus.
364 *
365 * Return: 0 on success
366 */
Simon Glass2a311e82020-01-27 08:49:37 -0700367static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800368 uint offset, ulong *valuep,
369 enum pci_size_t size)
370{
371 struct pcie_advk *pcie = dev_get_priv(bus);
Pali Rohár525886e2021-09-26 00:54:42 +0200372 int busno = PCI_BUS(bdf) - dev_seq(bus);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200373 int retry_count;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200374 bool allow_crs;
Pali Rohár525886e2021-09-26 00:54:42 +0200375 ulong data;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800376 uint reg;
377 int ret;
378
379 dev_dbg(pcie->dev, "PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
380 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
381
Pali Rohár525886e2021-09-26 00:54:42 +0200382 if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800383 dev_dbg(pcie->dev, "- out of range\n");
384 *valuep = pci_get_ff(size);
385 return 0;
386 }
387
Pali Rohár67cd7212021-08-27 14:14:43 +0200388 /*
Pali Rohár525886e2021-09-26 00:54:42 +0200389 * The configuration space of the PCI Bridge on primary (local) bus is
390 * not accessible via PIO transfers like all other PCIe devices. PCI
391 * Bridge config registers are available directly in Aardvark memory
392 * space starting at offset zero. Moreover PCI Bridge registers in the
393 * range 0x10 - 0x34 are not available and register 0x38 (Expansion ROM
394 * Base Address) is at offset 0x30.
395 * We therefore read configuration space content of the primary PCI
396 * Bridge from our virtual cache.
397 */
398 if (busno == pcie->first_busno) {
399 if (offset >= 0x10 && offset < 0x34)
400 data = pcie->cfgcache[(offset - 0x10) / 4];
401 else if ((offset & ~3) == PCI_ROM_ADDRESS1)
402 data = advk_readl(pcie, PCIE_CORE_EXP_ROM_BAR_REG);
403 else
404 data = advk_readl(pcie, offset & ~3);
405
406 if ((offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
407 /*
408 * Change Header Type of PCI Bridge device to Type 1
409 * (0x01, used by PCI Bridges) because hardwired value
410 * is Type 0 (0x00, used by Endpoint devices).
411 */
412 data &= ~0x007f0000;
413 data |= PCI_HEADER_TYPE_BRIDGE << 16;
414 }
415
416 *valuep = pci_conv_32_to_size(data, offset, size);
417
418 return 0;
419 }
420
421 /*
Pali Rohár67cd7212021-08-27 14:14:43 +0200422 * Returning fabricated CRS value (0xFFFF0001) by PCIe Root Complex to
423 * OS is allowed only for 4-byte PCI_VENDOR_ID config read request and
424 * only when CRSSVE bit in Root Port PCIe device is enabled. In all
425 * other error PCIe Root Complex must return all-ones.
426 * Aardvark HW does not have Root Port PCIe device and U-Boot does not
427 * implement emulation of this device.
428 * U-Boot currently does not support handling of CRS return value for
429 * PCI_VENDOR_ID config read request and also does not set CRSSVE bit.
430 * Therefore disable returning CRS response for now.
431 */
432 allow_crs = false;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200433
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200434 if (advk_readl(pcie, PIO_START)) {
435 dev_err(pcie->dev,
436 "Previous PIO read/write transfer is still running\n");
Pali Rohárb8344cf2021-08-09 09:53:13 +0200437 if (allow_crs) {
438 *valuep = CFG_RD_CRS_VAL;
439 return 0;
440 }
441 *valuep = pci_get_ff(size);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200442 return -EAGAIN;
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200443 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800444
445 /* Program the control register */
446 reg = advk_readl(pcie, PIO_CTRL);
447 reg &= ~PIO_CTRL_TYPE_MASK;
Pali Rohár525886e2021-09-26 00:54:42 +0200448 if (busno == pcie->sec_busno)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800449 reg |= PCIE_CONFIG_RD_TYPE0;
450 else
451 reg |= PCIE_CONFIG_RD_TYPE1;
452 advk_writel(pcie, reg, PIO_CTRL);
453
454 /* Program the address registers */
Pali Rohár525886e2021-09-26 00:54:42 +0200455 reg = PCIE_BDF(busno, PCI_DEV(bdf), PCI_FUNC(bdf)) | PCIE_CONF_REG(offset);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800456 advk_writel(pcie, reg, PIO_ADDR_LS);
457 advk_writel(pcie, 0, PIO_ADDR_MS);
458
Pali Rohár907cd9a2021-08-27 14:14:44 +0200459 retry_count = 0;
460
461retry:
Wilson Dinga6bdc862018-03-26 15:57:29 +0800462 /* Start the transfer */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200463 advk_writel(pcie, 1, PIO_ISR);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800464 advk_writel(pcie, 1, PIO_START);
465
Pali Rohár907cd9a2021-08-27 14:14:44 +0200466 ret = pcie_advk_wait_pio(pcie);
467 if (ret < 0) {
Pali Rohárb8344cf2021-08-09 09:53:13 +0200468 if (allow_crs) {
469 *valuep = CFG_RD_CRS_VAL;
470 return 0;
471 }
472 *valuep = pci_get_ff(size);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200473 return ret;
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200474 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800475
Pali Rohár907cd9a2021-08-27 14:14:44 +0200476 retry_count += ret;
477
Wilson Dinga6bdc862018-03-26 15:57:29 +0800478 /* Check PIO status and get the read result */
Pali Rohárb8344cf2021-08-09 09:53:13 +0200479 ret = pcie_advk_check_pio_status(pcie, allow_crs, &reg);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200480 if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
481 goto retry;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200482 if (ret) {
483 *valuep = pci_get_ff(size);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800484 return ret;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200485 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800486
487 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
488 offset, size, reg);
489 *valuep = pci_conv_32_to_size(reg, offset, size);
490
491 return 0;
492}
493
494/**
495 * pcie_calc_datastrobe() - Calculate data strobe
496 *
497 * @offset: The offset into the device's configuration space
498 * @size: Indicates the size of access to perform
499 *
500 * Calculate data strobe according to offset and size
501 *
502 */
503static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size)
504{
505 uint bytes, data_strobe;
506
507 switch (size) {
508 case PCI_SIZE_8:
509 bytes = 1;
510 break;
511 case PCI_SIZE_16:
512 bytes = 2;
513 break;
514 default:
515 bytes = 4;
516 }
517
518 data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3);
519
520 return data_strobe;
521}
522
523/**
524 * pcie_advk_write_config() - Write to configuration space
525 *
526 * @bus: Pointer to the PCI bus
527 * @bdf: Identifies the PCIe device to access
528 * @offset: The offset into the device's configuration space
529 * @value: The value to write
530 * @size: Indicates the size of access to perform
531 *
532 * Write the value @value of size @size from offset @offset within the
533 * configuration space of the device identified by the bus, device & function
534 * numbers in @bdf on the PCI bus @bus.
535 *
536 * Return: 0 on success
537 */
538static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf,
539 uint offset, ulong value,
540 enum pci_size_t size)
541{
542 struct pcie_advk *pcie = dev_get_priv(bus);
Pali Rohár525886e2021-09-26 00:54:42 +0200543 int busno = PCI_BUS(bdf) - dev_seq(bus);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200544 int retry_count;
Pali Rohár525886e2021-09-26 00:54:42 +0200545 ulong data;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800546 uint reg;
Pali Rohár907cd9a2021-08-27 14:14:44 +0200547 int ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800548
549 dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
550 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
551 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
552 offset, size, value);
553
Pali Rohár525886e2021-09-26 00:54:42 +0200554 if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800555 dev_dbg(pcie->dev, "- out of range\n");
556 return 0;
557 }
558
Pali Rohár525886e2021-09-26 00:54:42 +0200559 /*
560 * As explained in pcie_advk_read_config(), for the configuration
561 * space of the primary PCI Bridge, we write the content into virtual
562 * cache.
563 */
564 if (busno == pcie->first_busno) {
565 if (offset >= 0x10 && offset < 0x34) {
566 data = pcie->cfgcache[(offset - 0x10) / 4];
567 data = pci_conv_size_to_32(data, value, offset, size);
568 pcie->cfgcache[(offset - 0x10) / 4] = data;
569 } else if ((offset & ~3) == PCI_ROM_ADDRESS1) {
570 data = advk_readl(pcie, PCIE_CORE_EXP_ROM_BAR_REG);
571 data = pci_conv_size_to_32(data, value, offset, size);
572 advk_writel(pcie, data, PCIE_CORE_EXP_ROM_BAR_REG);
573 } else {
574 data = advk_readl(pcie, offset & ~3);
575 data = pci_conv_size_to_32(data, value, offset, size);
576 advk_writel(pcie, data, offset & ~3);
577 }
578
579 if (offset == PCI_PRIMARY_BUS)
580 pcie->first_busno = data & 0xff;
581
582 if (offset == PCI_SECONDARY_BUS ||
583 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8))
584 pcie->sec_busno = (data >> 8) & 0xff;
585
586 return 0;
587 }
588
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200589 if (advk_readl(pcie, PIO_START)) {
590 dev_err(pcie->dev,
591 "Previous PIO read/write transfer is still running\n");
Pali Rohár907cd9a2021-08-27 14:14:44 +0200592 return -EAGAIN;
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200593 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800594
595 /* Program the control register */
596 reg = advk_readl(pcie, PIO_CTRL);
597 reg &= ~PIO_CTRL_TYPE_MASK;
Pali Rohár525886e2021-09-26 00:54:42 +0200598 if (busno == pcie->sec_busno)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800599 reg |= PCIE_CONFIG_WR_TYPE0;
600 else
601 reg |= PCIE_CONFIG_WR_TYPE1;
602 advk_writel(pcie, reg, PIO_CTRL);
603
604 /* Program the address registers */
Pali Rohár525886e2021-09-26 00:54:42 +0200605 reg = PCIE_BDF(busno, PCI_DEV(bdf), PCI_FUNC(bdf)) | PCIE_CONF_REG(offset);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800606 advk_writel(pcie, reg, PIO_ADDR_LS);
607 advk_writel(pcie, 0, PIO_ADDR_MS);
608 dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg);
609
610 /* Program the data register */
611 reg = pci_conv_size_to_32(0, value, offset, size);
612 advk_writel(pcie, reg, PIO_WR_DATA);
613 dev_dbg(pcie->dev, "\tPIO req. - val = 0x%08x\n", reg);
614
615 /* Program the data strobe */
616 reg = pcie_calc_datastrobe(offset, size);
617 advk_writel(pcie, reg, PIO_WR_DATA_STRB);
618 dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg);
619
Pali Rohár907cd9a2021-08-27 14:14:44 +0200620 retry_count = 0;
621
622retry:
Wilson Dinga6bdc862018-03-26 15:57:29 +0800623 /* Start the transfer */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200624 advk_writel(pcie, 1, PIO_ISR);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800625 advk_writel(pcie, 1, PIO_START);
626
Pali Rohár907cd9a2021-08-27 14:14:44 +0200627 ret = pcie_advk_wait_pio(pcie);
628 if (ret < 0)
629 return ret;
630
631 retry_count += ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800632
633 /* Check PIO status */
Pali Rohár907cd9a2021-08-27 14:14:44 +0200634 ret = pcie_advk_check_pio_status(pcie, false, NULL);
635 if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
636 goto retry;
637 return ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800638}
639
640/**
641 * pcie_advk_link_up() - Check if PCIe link is up or not
642 *
643 * @pcie: The PCI device to access
644 *
645 * Return 1 (true) on link up.
646 * Return 0 (false) on link down.
647 */
648static int pcie_advk_link_up(struct pcie_advk *pcie)
649{
650 u32 val, ltssm_state;
651
652 val = advk_readl(pcie, CFG_REG);
653 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
Pali Rohár6d11d9e2021-09-26 00:54:41 +0200654 return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800655}
656
657/**
658 * pcie_advk_wait_for_link() - Wait for link training to be accomplished
659 *
660 * @pcie: The PCI device to access
661 *
662 * Wait up to 1 second for link training to be accomplished.
663 *
664 * Return 1 (true) if link training ends up with link up success.
665 * Return 0 (false) if link training ends up with link up failure.
666 */
667static int pcie_advk_wait_for_link(struct pcie_advk *pcie)
668{
669 int retries;
670
671 /* check if the link is up or not */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200672 for (retries = 0; retries < LINK_MAX_RETRIES; retries++) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800673 if (pcie_advk_link_up(pcie)) {
Pali Rohár525886e2021-09-26 00:54:42 +0200674 printf("PCIe: Link up\n");
Wilson Dinga6bdc862018-03-26 15:57:29 +0800675 return 0;
676 }
677
678 udelay(LINK_WAIT_TIMEOUT);
679 }
680
Pali Rohár525886e2021-09-26 00:54:42 +0200681 printf("PCIe: Link down\n");
Wilson Dinga6bdc862018-03-26 15:57:29 +0800682
683 return -ETIMEDOUT;
684}
685
Pali Roháre78c8e02021-05-26 17:59:40 +0200686/*
687 * Set PCIe address window register which could be used for memory
688 * mapping.
689 */
690static void pcie_advk_set_ob_win(struct pcie_advk *pcie, u8 win_num,
691 phys_addr_t match, phys_addr_t remap,
692 phys_addr_t mask, u32 actions)
693{
694 advk_writel(pcie, OB_WIN_ENABLE |
695 lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
696 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
697 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
698 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
699 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
700 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
701 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
702}
703
704static void pcie_advk_disable_ob_win(struct pcie_advk *pcie, u8 win_num)
705{
706 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
707 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
708 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
709 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
710 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
711 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
712 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
713}
714
715static void pcie_advk_set_ob_region(struct pcie_advk *pcie, int *wins,
716 struct pci_region *region, u32 actions)
717{
718 phys_addr_t phys_start = region->phys_start;
719 pci_addr_t bus_start = region->bus_start;
720 pci_size_t size = region->size;
721 phys_addr_t win_mask;
722 u64 win_size;
723
724 if (*wins == -1)
725 return;
726
727 /*
728 * The n-th PCIe window is configured by tuple (match, remap, mask)
Pali Rohár2fef1db2021-07-08 20:19:00 +0200729 * and an access to address A uses this window if A matches the
Pali Roháre78c8e02021-05-26 17:59:40 +0200730 * match with given mask.
731 * So every PCIe window size must be a power of two and every start
732 * address must be aligned to window size. Minimal size is 64 KiB
Pali Rohár22b1e082021-07-08 20:18:58 +0200733 * because lower 16 bits of mask must be zero. Remapped address
734 * may have set only bits from the mask.
Pali Roháre78c8e02021-05-26 17:59:40 +0200735 */
736 while (*wins < OB_WIN_COUNT && size > 0) {
737 /* Calculate the largest aligned window size */
738 win_size = (1ULL << (fls64(size) - 1)) |
739 (phys_start ? (1ULL << __ffs64(phys_start)) : 0);
740 win_size = 1ULL << __ffs64(win_size);
Pali Rohár22b1e082021-07-08 20:18:58 +0200741 win_mask = ~(win_size - 1);
742 if (win_size < 0x10000 || (bus_start & ~win_mask))
Pali Roháre78c8e02021-05-26 17:59:40 +0200743 break;
744
745 dev_dbg(pcie->dev,
746 "Configuring PCIe window %d: [0x%llx-0x%llx] as 0x%x\n",
747 *wins, (u64)phys_start, (u64)phys_start + win_size,
748 actions);
Pali Roháre78c8e02021-05-26 17:59:40 +0200749 pcie_advk_set_ob_win(pcie, *wins, phys_start, bus_start,
750 win_mask, actions);
751
752 phys_start += win_size;
753 bus_start += win_size;
754 size -= win_size;
755 (*wins)++;
756 }
757
758 if (size > 0) {
759 *wins = -1;
760 dev_err(pcie->dev,
761 "Invalid PCIe region [0x%llx-0x%llx]\n",
762 (u64)region->phys_start,
763 (u64)region->phys_start + region->size);
764 }
765}
766
Wilson Dinga6bdc862018-03-26 15:57:29 +0800767/**
768 * pcie_advk_setup_hw() - PCIe initailzation
769 *
770 * @pcie: The PCI device to access
771 *
772 * Return: 0 on success
773 */
774static int pcie_advk_setup_hw(struct pcie_advk *pcie)
775{
Pali Roháre78c8e02021-05-26 17:59:40 +0200776 struct pci_region *io, *mem, *pref;
777 int i, wins;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800778 u32 reg;
779
780 /* Set to Direct mode */
781 reg = advk_readl(pcie, CTRL_CONFIG_REG);
782 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
783 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
784 advk_writel(pcie, reg, CTRL_CONFIG_REG);
785
786 /* Set PCI global control register to RC mode */
787 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
788 reg |= (IS_RC_MSK << IS_RC_SHIFT);
789 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
790
Pali Rohárba40b6c2021-03-03 14:37:59 +0100791 /*
792 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
793 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
794 * id in high 16 bits. Updating this register changes readback value of
795 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
796 * for erratum 4.1: "The value of device and vendor ID is incorrect".
797 */
798 advk_writel(pcie, 0x11ab11ab, VENDOR_ID_REG);
799
Pali Rohár525886e2021-09-26 00:54:42 +0200800 /*
801 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
802 * because default value is Mass Storage Controller (0x010400), causing
803 * U-Boot to fail to recognize it as P2P Bridge.
804 *
805 * Note that this Aardvark PCI Bridge does not have a compliant Type 1
806 * Configuration Space and it even cannot be accessed via Aardvark's
807 * PCI config space access method. Something like config space is
808 * available in internal Aardvark registers starting at offset 0x0
809 * and is reported as Type 0. In range 0x10 - 0x34 it has totally
810 * different registers. So our driver reports Header Type as Type 1 and
811 * for the above mentioned range redirects access to the virtual
812 * cfgcache[] buffer, which avoids changing internal Aardvark registers.
813 */
814 reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
815 reg &= ~0xffffff00;
816 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
817 advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
818
Wilson Dinga6bdc862018-03-26 15:57:29 +0800819 /* Set Advanced Error Capabilities and Control PF0 register */
820 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
821 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
822 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK |
823 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV;
824 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
825
826 /* Set PCIe Device Control and Status 1 PF0 register */
827 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
Pali Rohár9057a2c2021-02-05 15:32:28 +0100828 (PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE <<
829 PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT) |
830 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE <<
831 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT) |
Wilson Dinga6bdc862018-03-26 15:57:29 +0800832 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE;
833 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
834
835 /* Program PCIe Control 2 to disable strict ordering */
836 reg = PCIE_CORE_CTRL2_RESERVED |
837 PCIE_CORE_CTRL2_TD_ENABLE;
838 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
839
840 /* Set GEN2 */
841 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
842 reg &= ~PCIE_GEN_SEL_MSK;
843 reg |= SPEED_GEN_2;
844 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
845
846 /* Set lane X1 */
847 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
848 reg &= ~LANE_CNT_MSK;
849 reg |= LANE_COUNT_1;
850 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
851
852 /* Enable link training */
853 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
854 reg |= LINK_TRAINING_EN;
855 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
856
857 /*
858 * Enable AXI address window location generation:
859 * When it is enabled, the default outbound window
860 * configurations (Default User Field: 0xD0074CFC)
861 * are used to transparent address translation for
862 * the outbound transactions. Thus, PCIe address
Pali Roháre78c8e02021-05-26 17:59:40 +0200863 * windows are not required for transparent memory
864 * access when default outbound window configuration
865 * is set for memory access.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800866 */
867 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
868 reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE;
869 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
870
871 /*
872 * Bypass the address window mapping for PIO:
873 * Since PIO access already contains all required
874 * info over AXI interface by PIO registers, the
875 * address window is not required.
876 */
877 reg = advk_readl(pcie, PIO_CTRL);
878 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
879 advk_writel(pcie, reg, PIO_CTRL);
880
Pali Roháre78c8e02021-05-26 17:59:40 +0200881 /*
882 * Set memory access in Default User Field so it
883 * is not required to configure PCIe address for
884 * transparent memory access.
885 */
886 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
887
888 /*
889 * Configure PCIe address windows for non-memory or
890 * non-transparent access as by default PCIe uses
891 * transparent memory access.
892 */
893 wins = 0;
894 pci_get_regions(pcie->dev, &io, &mem, &pref);
895 if (io)
896 pcie_advk_set_ob_region(pcie, &wins, io, OB_WIN_TYPE_IO);
897 if (mem && mem->phys_start != mem->bus_start)
898 pcie_advk_set_ob_region(pcie, &wins, mem, OB_WIN_TYPE_MEM);
899 if (pref && pref->phys_start != pref->bus_start)
900 pcie_advk_set_ob_region(pcie, &wins, pref, OB_WIN_TYPE_MEM);
901
902 /* Disable remaining PCIe outbound windows */
903 for (i = ((wins >= 0) ? wins : 0); i < OB_WIN_COUNT; i++)
904 pcie_advk_disable_ob_win(pcie, i);
905
906 if (wins == -1)
907 return -EINVAL;
908
Wilson Dinga6bdc862018-03-26 15:57:29 +0800909 /* Wait for PCIe link up */
910 if (pcie_advk_wait_for_link(pcie))
911 return -ENXIO;
912
913 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
914 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
915 PCIE_CORE_CMD_IO_ACCESS_EN |
916 PCIE_CORE_CMD_MEM_IO_REQ_EN;
917 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
918
919 return 0;
920}
921
922/**
923 * pcie_advk_probe() - Probe the PCIe bus for active link
924 *
925 * @dev: A pointer to the device being operated on
926 *
927 * Probe for an active link on the PCIe bus and configure the controller
928 * to enable this port.
929 *
930 * Return: 0 on success, else -ENODEV
931 */
932static int pcie_advk_probe(struct udevice *dev)
933{
934 struct pcie_advk *pcie = dev_get_priv(dev);
935
Pali Rohár6a58b972020-08-19 15:57:07 +0200936 gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800937 GPIOD_IS_OUT);
938 /*
939 * Issue reset to add-in card through the dedicated GPIO.
940 * Some boards are connecting the card reset pin to common system
941 * reset wire and others are using separate GPIO port.
942 * In the last case we have to release a reset of the addon card
943 * using this GPIO.
944 *
945 * FIX-ME:
946 * The PCIe RESET signal is not supposed to be released along
947 * with the SOC RESET signal. It should be lowered as early as
948 * possible before PCIe PHY initialization. Moreover, the PCIe
949 * clock should be gated as well.
950 */
Pali Rohár6a58b972020-08-19 15:57:07 +0200951 if (dm_gpio_is_valid(&pcie->reset_gpio)) {
Pali Roháre3271bb2021-01-18 12:09:33 +0100952 dev_dbg(dev, "Toggle PCIE Reset GPIO ...\n");
Pali Rohár6a58b972020-08-19 15:57:07 +0200953 dm_gpio_set_value(&pcie->reset_gpio, 1);
Pali Rohár0130a612020-08-19 15:57:06 +0200954 mdelay(200);
Pali Rohár6a58b972020-08-19 15:57:07 +0200955 dm_gpio_set_value(&pcie->reset_gpio, 0);
Pali Rohár5c6edca2020-08-25 10:45:04 +0200956 } else {
Pali Roháre3271bb2021-01-18 12:09:33 +0100957 dev_warn(dev, "PCIE Reset on GPIO support is missing\n");
Wilson Dinga6bdc862018-03-26 15:57:29 +0800958 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800959
Wilson Dinga6bdc862018-03-26 15:57:29 +0800960 pcie->dev = pci_get_controller(dev);
961
Pali Rohár525886e2021-09-26 00:54:42 +0200962 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
963 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
964 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
965 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
966 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
967
Wilson Dinga6bdc862018-03-26 15:57:29 +0800968 return pcie_advk_setup_hw(pcie);
969}
970
Pali Rohár6a58b972020-08-19 15:57:07 +0200971static int pcie_advk_remove(struct udevice *dev)
972{
Pali Rohár6a58b972020-08-19 15:57:07 +0200973 struct pcie_advk *pcie = dev_get_priv(dev);
Pali Rohárff876992020-09-22 13:21:38 +0200974 u32 reg;
Pali Roháre78c8e02021-05-26 17:59:40 +0200975 int i;
976
977 for (i = 0; i < OB_WIN_COUNT; i++)
978 pcie_advk_disable_ob_win(pcie, i);
Pali Rohár6a58b972020-08-19 15:57:07 +0200979
Pali Roháread96342021-05-26 17:59:35 +0200980 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
981 reg &= ~(PCIE_CORE_CMD_MEM_ACCESS_EN |
982 PCIE_CORE_CMD_IO_ACCESS_EN |
983 PCIE_CORE_CMD_MEM_IO_REQ_EN);
984 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
985
Pali Rohárff876992020-09-22 13:21:38 +0200986 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
987 reg &= ~LINK_TRAINING_EN;
988 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
989
Pali Rohár6a58b972020-08-19 15:57:07 +0200990 return 0;
991}
992
Wilson Dinga6bdc862018-03-26 15:57:29 +0800993/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700994 * pcie_advk_of_to_plat() - Translate from DT to device state
Wilson Dinga6bdc862018-03-26 15:57:29 +0800995 *
996 * @dev: A pointer to the device being operated on
997 *
998 * Translate relevant data from the device tree pertaining to device @dev into
999 * state that the driver will later make use of. This state is stored in the
1000 * device's private data structure.
1001 *
1002 * Return: 0 on success, else -EINVAL
1003 */
Simon Glassaad29ae2020-12-03 16:55:21 -07001004static int pcie_advk_of_to_plat(struct udevice *dev)
Wilson Dinga6bdc862018-03-26 15:57:29 +08001005{
1006 struct pcie_advk *pcie = dev_get_priv(dev);
1007
1008 /* Get the register base address */
1009 pcie->base = (void *)dev_read_addr_index(dev, 0);
1010 if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE)
1011 return -EINVAL;
1012
1013 return 0;
1014}
1015
1016static const struct dm_pci_ops pcie_advk_ops = {
1017 .read_config = pcie_advk_read_config,
1018 .write_config = pcie_advk_write_config,
1019};
1020
1021static const struct udevice_id pcie_advk_ids[] = {
Pali Rohár678cf9d2021-05-26 17:59:36 +02001022 { .compatible = "marvell,armada-3700-pcie" },
Wilson Dinga6bdc862018-03-26 15:57:29 +08001023 { }
1024};
1025
1026U_BOOT_DRIVER(pcie_advk) = {
1027 .name = "pcie_advk",
1028 .id = UCLASS_PCI,
1029 .of_match = pcie_advk_ids,
1030 .ops = &pcie_advk_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -07001031 .of_to_plat = pcie_advk_of_to_plat,
Wilson Dinga6bdc862018-03-26 15:57:29 +08001032 .probe = pcie_advk_probe,
Pali Rohár6a58b972020-08-19 15:57:07 +02001033 .remove = pcie_advk_remove,
1034 .flags = DM_FLAG_OS_PREPARE,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001035 .priv_auto = sizeof(struct pcie_advk),
Wilson Dinga6bdc862018-03-26 15:57:29 +08001036};