blob: a92f00d0af5aa067da18558234f12b8580b356b7 [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
Pali Rohár238fbab2021-09-26 00:54:44 +020044#define PCIE_CORE_PCIEXP_CAP_OFF 0xc0
Wilson Dinga6bdc862018-03-26 15:57:29 +080045#define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
46#define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
47#define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
Pali Rohár9057a2c2021-02-05 15:32:28 +010048#define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE 0x2
49#define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT 5
50#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE 0x2
51#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
Wilson Dinga6bdc862018-03-26 15:57:29 +080052#define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
53#define PCIE_CORE_LINK_TRAINING BIT(5)
54#define PCIE_CORE_ERR_CAPCTL_REG 0x118
55#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
56#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
57#define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK BIT(7)
58#define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV BIT(8)
59
60/* PIO registers base address and register offsets */
61#define PIO_BASE_ADDR 0x4000
62#define PIO_CTRL (PIO_BASE_ADDR + 0x0)
63#define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
64#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
65#define PIO_STAT (PIO_BASE_ADDR + 0x4)
66#define PIO_COMPLETION_STATUS_SHIFT 7
67#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
68#define PIO_COMPLETION_STATUS_OK 0
69#define PIO_COMPLETION_STATUS_UR 1
70#define PIO_COMPLETION_STATUS_CRS 2
71#define PIO_COMPLETION_STATUS_CA 4
72#define PIO_NON_POSTED_REQ BIT(10)
73#define PIO_ERR_STATUS BIT(11)
74#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
75#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
76#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
77#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
78#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
79#define PIO_START (PIO_BASE_ADDR + 0x1c)
80#define PIO_ISR (PIO_BASE_ADDR + 0x20)
81
82/* Aardvark Control registers */
83#define CONTROL_BASE_ADDR 0x4800
84#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
85#define PCIE_GEN_SEL_MSK 0x3
86#define PCIE_GEN_SEL_SHIFT 0x0
87#define SPEED_GEN_1 0
88#define SPEED_GEN_2 1
89#define SPEED_GEN_3 2
90#define IS_RC_MSK 1
91#define IS_RC_SHIFT 2
92#define LANE_CNT_MSK 0x18
93#define LANE_CNT_SHIFT 0x3
94#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
95#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
96#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
97#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
98#define LINK_TRAINING_EN BIT(6)
99#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
100#define PCIE_CORE_CTRL2_RESERVED 0x7
101#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
102#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
103#define PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE BIT(6)
104
Pali Roháre78c8e02021-05-26 17:59:40 +0200105/* PCIe window configuration */
106#define OB_WIN_BASE_ADDR 0x4c00
107#define OB_WIN_BLOCK_SIZE 0x20
108#define OB_WIN_COUNT 8
109#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
110 OB_WIN_BLOCK_SIZE * (win) + \
111 (offset))
112#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
113#define OB_WIN_ENABLE BIT(0)
114#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
115#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
116#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
117#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
118#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
119#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
120#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
121#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
122#define OB_WIN_FUNC_NUM_SHIFT 24
123#define OB_WIN_FUNC_NUM_ENABLE BIT(23)
124#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
125#define OB_WIN_BUS_NUM_BITS_SHIFT 20
126#define OB_WIN_MSG_CODE_ENABLE BIT(22)
127#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
128#define OB_WIN_MSG_CODE_SHIFT 14
129#define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
130#define OB_WIN_ATTR_ENABLE BIT(11)
131#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
132#define OB_WIN_ATTR_TC_SHIFT 8
133#define OB_WIN_ATTR_RELAXED BIT(7)
134#define OB_WIN_ATTR_NOSNOOP BIT(6)
135#define OB_WIN_ATTR_POISON BIT(5)
136#define OB_WIN_ATTR_IDO BIT(4)
137#define OB_WIN_TYPE_MASK GENMASK(3, 0)
138#define OB_WIN_TYPE_SHIFT 0
139#define OB_WIN_TYPE_MEM 0x0
140#define OB_WIN_TYPE_IO 0x4
141#define OB_WIN_TYPE_CONFIG_TYPE0 0x8
142#define OB_WIN_TYPE_CONFIG_TYPE1 0x9
143#define OB_WIN_TYPE_MSG 0xc
144
Wilson Dinga6bdc862018-03-26 15:57:29 +0800145/* LMI registers base address and register offsets */
146#define LMI_BASE_ADDR 0x6000
147#define CFG_REG (LMI_BASE_ADDR + 0x0)
148#define LTSSM_SHIFT 24
149#define LTSSM_MASK 0x3f
150#define LTSSM_L0 0x10
Pali Rohár6d11d9e2021-09-26 00:54:41 +0200151#define LTSSM_DISABLED 0x20
Pali Rohárba40b6c2021-03-03 14:37:59 +0100152#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800153
154/* PCIe core controller registers */
155#define CTRL_CORE_BASE_ADDR 0x18000
156#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
157#define CTRL_MODE_SHIFT 0x0
158#define CTRL_MODE_MASK 0x1
159#define PCIE_CORE_MODE_DIRECT 0x0
160#define PCIE_CORE_MODE_COMMAND 0x1
161
162/* Transaction types */
163#define PCIE_CONFIG_RD_TYPE0 0x8
164#define PCIE_CONFIG_RD_TYPE1 0x9
165#define PCIE_CONFIG_WR_TYPE0 0xa
166#define PCIE_CONFIG_WR_TYPE1 0xb
167
168/* PCI_BDF shifts 8bit, so we need extra 4bit shift */
Pali Rohár525886e2021-09-26 00:54:42 +0200169#define PCIE_BDF(b, d, f) (PCI_BDF(b, d, f) << 4)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800170#define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
171#define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
172#define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
173#define PCIE_CONF_REG(reg) ((reg) & 0xffc)
174#define PCIE_CONF_ADDR(bus, devfn, where) \
175 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
176 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
177
178/* PCIe Retries & Timeout definitions */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200179#define PIO_MAX_RETRIES 1500
180#define PIO_WAIT_TIMEOUT 1000
181#define LINK_MAX_RETRIES 10
Wilson Dinga6bdc862018-03-26 15:57:29 +0800182#define LINK_WAIT_TIMEOUT 100000
183
Wilson Dinga6bdc862018-03-26 15:57:29 +0800184#define CFG_RD_CRS_VAL 0xFFFF0001
185
Wilson Dinga6bdc862018-03-26 15:57:29 +0800186/**
187 * struct pcie_advk - Advk PCIe controller state
188 *
Marek Behún7ec28982021-09-26 00:54:46 +0200189 * @base: The base address of the register space.
190 * @first_busno: Bus number of the PCIe root-port.
191 * This may vary depending on the PCIe setup.
192 * @sec_busno: Bus number for the device behind the PCIe root-port.
193 * @dev: The pointer to PCI uclass device.
194 * @reset_gpio: GPIO descriptor for PERST.
195 * @cfgcache: Buffer for emulation of PCIe Root Port's PCI Bridge registers
196 * that are not available on Aardvark.
197 * @cfgcrssve: For CRSSVE emulation.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800198 */
199struct pcie_advk {
Marek Behúncbc5fc82021-09-26 00:54:45 +0200200 void *base;
201 int first_busno;
202 int sec_busno;
203 struct udevice *dev;
204 struct gpio_desc reset_gpio;
Pali Rohár0f65c042021-11-11 16:35:48 +0100205 u32 cfgcache[(0x3c - 0x10) / 4];
Marek Behúncbc5fc82021-09-26 00:54:45 +0200206 bool cfgcrssve;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800207};
208
209static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg)
210{
211 writel(val, pcie->base + reg);
212}
213
214static inline uint advk_readl(struct pcie_advk *pcie, uint reg)
215{
216 return readl(pcie->base + reg);
217}
218
219/**
220 * pcie_advk_addr_valid() - Check for valid bus address
221 *
Pali Rohár525886e2021-09-26 00:54:42 +0200222 * @pcie: Pointer to the PCI bus
223 * @busno: Bus number of PCI device
224 * @dev: Device number of PCI device
225 * @func: Function number of PCI device
Wilson Dinga6bdc862018-03-26 15:57:29 +0800226 * @bdf: The PCI device to access
Wilson Dinga6bdc862018-03-26 15:57:29 +0800227 *
Pali Rohár525886e2021-09-26 00:54:42 +0200228 * Return: true on valid, false on invalid
Wilson Dinga6bdc862018-03-26 15:57:29 +0800229 */
Pali Rohár525886e2021-09-26 00:54:42 +0200230static bool pcie_advk_addr_valid(struct pcie_advk *pcie,
231 int busno, u8 dev, u8 func)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800232{
Pali Rohár525886e2021-09-26 00:54:42 +0200233 /* On the primary (local) bus there is only one PCI Bridge */
234 if (busno == pcie->first_busno && (dev != 0 || func != 0))
235 return false;
236
Wilson Dinga6bdc862018-03-26 15:57:29 +0800237 /*
Pali Rohár525886e2021-09-26 00:54:42 +0200238 * In PCI-E only a single device (0) can exist on the secondary bus.
239 * Beyond the secondary bus, there might be a Switch and anything is
240 * possible.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800241 */
Pali Rohár525886e2021-09-26 00:54:42 +0200242 if (busno == pcie->sec_busno && dev != 0)
243 return false;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800244
Pali Rohár525886e2021-09-26 00:54:42 +0200245 return true;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800246}
247
248/**
249 * pcie_advk_wait_pio() - Wait for PIO access to be accomplished
250 *
251 * @pcie: The PCI device to access
252 *
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200253 * Wait up to 1.5 seconds for PIO access to be accomplished.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800254 *
Pali Rohár907cd9a2021-08-27 14:14:44 +0200255 * Return positive - retry count if PIO access is accomplished.
256 * Return negative - error if PIO access is timed out.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800257 */
258static int pcie_advk_wait_pio(struct pcie_advk *pcie)
259{
260 uint start, isr;
261 uint count;
262
Pali Rohár907cd9a2021-08-27 14:14:44 +0200263 for (count = 1; count <= PIO_MAX_RETRIES; count++) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800264 start = advk_readl(pcie, PIO_START);
265 isr = advk_readl(pcie, PIO_ISR);
266 if (!start && isr)
Pali Rohár907cd9a2021-08-27 14:14:44 +0200267 return count;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800268 /*
269 * Do not check the PIO state too frequently,
270 * 100us delay is appropriate.
271 */
272 udelay(PIO_WAIT_TIMEOUT);
273 }
274
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200275 dev_err(pcie->dev, "PIO read/write transfer time out\n");
Pali Rohár907cd9a2021-08-27 14:14:44 +0200276 return -ETIMEDOUT;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800277}
278
279/**
280 * pcie_advk_check_pio_status() - Validate PIO status and get the read result
281 *
282 * @pcie: Pointer to the PCI bus
Pali Rohárb8344cf2021-08-09 09:53:13 +0200283 * @allow_crs: Only for read requests, if CRS response is allowed
284 * @read_val: Pointer to the read result
Wilson Dinga6bdc862018-03-26 15:57:29 +0800285 *
Pali Rohár907cd9a2021-08-27 14:14:44 +0200286 * Return: 0 on success
Wilson Dinga6bdc862018-03-26 15:57:29 +0800287 */
288static int pcie_advk_check_pio_status(struct pcie_advk *pcie,
Pali Rohárb8344cf2021-08-09 09:53:13 +0200289 bool allow_crs,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800290 uint *read_val)
291{
Pali Rohár907cd9a2021-08-27 14:14:44 +0200292 int ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800293 uint reg;
294 unsigned int status;
295 char *strcomp_status, *str_posted;
296
297 reg = advk_readl(pcie, PIO_STAT);
298 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
299 PIO_COMPLETION_STATUS_SHIFT;
300
301 switch (status) {
302 case PIO_COMPLETION_STATUS_OK:
303 if (reg & PIO_ERR_STATUS) {
304 strcomp_status = "COMP_ERR";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200305 ret = -EFAULT;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800306 break;
307 }
308 /* Get the read result */
Pali Rohárb8344cf2021-08-09 09:53:13 +0200309 if (read_val)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800310 *read_val = advk_readl(pcie, PIO_RD_DATA);
311 /* No error */
312 strcomp_status = NULL;
Pali Rohár907cd9a2021-08-27 14:14:44 +0200313 ret = 0;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800314 break;
315 case PIO_COMPLETION_STATUS_UR:
Pali Rohárb8344cf2021-08-09 09:53:13 +0200316 strcomp_status = "UR";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200317 ret = -EOPNOTSUPP;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800318 break;
319 case PIO_COMPLETION_STATUS_CRS:
Pali Rohárb8344cf2021-08-09 09:53:13 +0200320 if (allow_crs && read_val) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800321 /* For reading, CRS is not an error status. */
322 *read_val = CFG_RD_CRS_VAL;
323 strcomp_status = NULL;
Pali Rohár907cd9a2021-08-27 14:14:44 +0200324 ret = 0;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800325 } else {
326 strcomp_status = "CRS";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200327 ret = -EAGAIN;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800328 }
329 break;
330 case PIO_COMPLETION_STATUS_CA:
331 strcomp_status = "CA";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200332 ret = -ECANCELED;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800333 break;
334 default:
335 strcomp_status = "Unknown";
Pali Rohár907cd9a2021-08-27 14:14:44 +0200336 ret = -EINVAL;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800337 break;
338 }
339
340 if (!strcomp_status)
Pali Rohár907cd9a2021-08-27 14:14:44 +0200341 return ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800342
343 if (reg & PIO_NON_POSTED_REQ)
344 str_posted = "Non-posted";
345 else
346 str_posted = "Posted";
347
Marek Behún73d776a2021-09-07 17:27:08 +0200348 dev_dbg(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
Wilson Dinga6bdc862018-03-26 15:57:29 +0800349 str_posted, strcomp_status, reg,
350 advk_readl(pcie, PIO_ADDR_LS));
351
Pali Rohár907cd9a2021-08-27 14:14:44 +0200352 return ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800353}
354
355/**
356 * pcie_advk_read_config() - Read from configuration space
357 *
358 * @bus: Pointer to the PCI bus
359 * @bdf: Identifies the PCIe device to access
360 * @offset: The offset into the device's configuration space
361 * @valuep: A pointer at which to store the read value
362 * @size: Indicates the size of access to perform
363 *
364 * Read a value of size @size from offset @offset within the configuration
365 * space of the device identified by the bus, device & function numbers in @bdf
366 * on the PCI bus @bus.
367 *
368 * Return: 0 on success
369 */
Simon Glass2a311e82020-01-27 08:49:37 -0700370static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800371 uint offset, ulong *valuep,
372 enum pci_size_t size)
373{
374 struct pcie_advk *pcie = dev_get_priv(bus);
Pali Rohár525886e2021-09-26 00:54:42 +0200375 int busno = PCI_BUS(bdf) - dev_seq(bus);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200376 int retry_count;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200377 bool allow_crs;
Pali Rohár525886e2021-09-26 00:54:42 +0200378 ulong data;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800379 uint reg;
380 int ret;
381
382 dev_dbg(pcie->dev, "PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
383 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
384
Pali Rohár525886e2021-09-26 00:54:42 +0200385 if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800386 dev_dbg(pcie->dev, "- out of range\n");
387 *valuep = pci_get_ff(size);
388 return 0;
389 }
390
Pali Rohár67cd7212021-08-27 14:14:43 +0200391 /*
Pali Rohár0f65c042021-11-11 16:35:48 +0100392 * The configuration space of the PCI Bridge on primary (first) bus is
Pali Rohár525886e2021-09-26 00:54:42 +0200393 * not accessible via PIO transfers like all other PCIe devices. PCI
394 * Bridge config registers are available directly in Aardvark memory
Pali Rohár0f65c042021-11-11 16:35:48 +0100395 * space starting at offset zero. The PCI Bridge config space is of
396 * Type 0, but the BAR registers (including ROM BAR) don't have the same
397 * meaning as in the PCIe specification. Therefore do not access BAR
398 * registers and non-common registers (those which have different
399 * meaning for Type 0 and Type 1 config space) of the primary PCI Bridge
400 * and instead read their content from driver virtual cfgcache[].
Pali Rohár525886e2021-09-26 00:54:42 +0200401 */
402 if (busno == pcie->first_busno) {
Pali Rohár0f65c042021-11-11 16:35:48 +0100403 if ((offset >= 0x10 && offset < 0x34) || (offset >= 0x38 && offset < 0x3c))
Pali Rohár525886e2021-09-26 00:54:42 +0200404 data = pcie->cfgcache[(offset - 0x10) / 4];
Pali Rohár525886e2021-09-26 00:54:42 +0200405 else
406 data = advk_readl(pcie, offset & ~3);
407
408 if ((offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
409 /*
410 * Change Header Type of PCI Bridge device to Type 1
411 * (0x01, used by PCI Bridges) because hardwired value
412 * is Type 0 (0x00, used by Endpoint devices).
413 */
414 data &= ~0x007f0000;
415 data |= PCI_HEADER_TYPE_BRIDGE << 16;
416 }
417
Pali Rohár238fbab2021-09-26 00:54:44 +0200418 if ((offset & ~3) == PCIE_CORE_PCIEXP_CAP_OFF + PCI_EXP_RTCTL) {
419 /* CRSSVE bit is stored only in cache */
420 if (pcie->cfgcrssve)
421 data |= PCI_EXP_RTCTL_CRSSVE;
422 }
423
424 if ((offset & ~3) == PCIE_CORE_PCIEXP_CAP_OFF +
425 (PCI_EXP_RTCAP & ~3)) {
426 /* CRS is emulated below, so set CRSVIS capability */
427 data |= PCI_EXP_RTCAP_CRSVIS << 16;
428 }
429
Pali Rohár525886e2021-09-26 00:54:42 +0200430 *valuep = pci_conv_32_to_size(data, offset, size);
431
432 return 0;
433 }
434
435 /*
Pali Rohár67cd7212021-08-27 14:14:43 +0200436 * Returning fabricated CRS value (0xFFFF0001) by PCIe Root Complex to
437 * OS is allowed only for 4-byte PCI_VENDOR_ID config read request and
438 * only when CRSSVE bit in Root Port PCIe device is enabled. In all
439 * other error PCIe Root Complex must return all-ones.
Pali Rohár238fbab2021-09-26 00:54:44 +0200440 *
Pali Rohár67cd7212021-08-27 14:14:43 +0200441 * U-Boot currently does not support handling of CRS return value for
442 * PCI_VENDOR_ID config read request and also does not set CRSSVE bit.
Pali Rohár238fbab2021-09-26 00:54:44 +0200443 * So it means that pcie->cfgcrssve is false. But the code is prepared
444 * for returning CRS, so that if U-Boot does support CRS in the future,
445 * it will work for Aardvark.
Pali Rohár67cd7212021-08-27 14:14:43 +0200446 */
Pali Rohár4e02e702021-10-19 11:05:01 +0200447 allow_crs = (offset == PCI_VENDOR_ID) && (size == PCI_SIZE_32) && pcie->cfgcrssve;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200448
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200449 if (advk_readl(pcie, PIO_START)) {
450 dev_err(pcie->dev,
451 "Previous PIO read/write transfer is still running\n");
Pali Rohárb8344cf2021-08-09 09:53:13 +0200452 if (allow_crs) {
453 *valuep = CFG_RD_CRS_VAL;
454 return 0;
455 }
456 *valuep = pci_get_ff(size);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200457 return -EAGAIN;
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200458 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800459
460 /* Program the control register */
461 reg = advk_readl(pcie, PIO_CTRL);
462 reg &= ~PIO_CTRL_TYPE_MASK;
Pali Rohár525886e2021-09-26 00:54:42 +0200463 if (busno == pcie->sec_busno)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800464 reg |= PCIE_CONFIG_RD_TYPE0;
465 else
466 reg |= PCIE_CONFIG_RD_TYPE1;
467 advk_writel(pcie, reg, PIO_CTRL);
468
469 /* Program the address registers */
Pali Rohár525886e2021-09-26 00:54:42 +0200470 reg = PCIE_BDF(busno, PCI_DEV(bdf), PCI_FUNC(bdf)) | PCIE_CONF_REG(offset);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800471 advk_writel(pcie, reg, PIO_ADDR_LS);
472 advk_writel(pcie, 0, PIO_ADDR_MS);
473
Pali Rohár058f67b2021-11-01 10:12:51 +0100474 /* Program the data strobe */
475 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
476
Pali Rohár907cd9a2021-08-27 14:14:44 +0200477 retry_count = 0;
478
479retry:
Wilson Dinga6bdc862018-03-26 15:57:29 +0800480 /* Start the transfer */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200481 advk_writel(pcie, 1, PIO_ISR);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800482 advk_writel(pcie, 1, PIO_START);
483
Pali Rohár907cd9a2021-08-27 14:14:44 +0200484 ret = pcie_advk_wait_pio(pcie);
485 if (ret < 0) {
Pali Rohárb8344cf2021-08-09 09:53:13 +0200486 if (allow_crs) {
487 *valuep = CFG_RD_CRS_VAL;
488 return 0;
489 }
490 *valuep = pci_get_ff(size);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200491 return ret;
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200492 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800493
Pali Rohár907cd9a2021-08-27 14:14:44 +0200494 retry_count += ret;
495
Wilson Dinga6bdc862018-03-26 15:57:29 +0800496 /* Check PIO status and get the read result */
Pali Rohárb8344cf2021-08-09 09:53:13 +0200497 ret = pcie_advk_check_pio_status(pcie, allow_crs, &reg);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200498 if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
499 goto retry;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200500 if (ret) {
501 *valuep = pci_get_ff(size);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800502 return ret;
Pali Rohárb8344cf2021-08-09 09:53:13 +0200503 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800504
505 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
506 offset, size, reg);
507 *valuep = pci_conv_32_to_size(reg, offset, size);
508
509 return 0;
510}
511
512/**
513 * pcie_calc_datastrobe() - Calculate data strobe
514 *
515 * @offset: The offset into the device's configuration space
516 * @size: Indicates the size of access to perform
517 *
518 * Calculate data strobe according to offset and size
519 *
520 */
521static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size)
522{
523 uint bytes, data_strobe;
524
525 switch (size) {
526 case PCI_SIZE_8:
527 bytes = 1;
528 break;
529 case PCI_SIZE_16:
530 bytes = 2;
531 break;
532 default:
533 bytes = 4;
534 }
535
536 data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3);
537
538 return data_strobe;
539}
540
541/**
542 * pcie_advk_write_config() - Write to configuration space
543 *
544 * @bus: Pointer to the PCI bus
545 * @bdf: Identifies the PCIe device to access
546 * @offset: The offset into the device's configuration space
547 * @value: The value to write
548 * @size: Indicates the size of access to perform
549 *
550 * Write the value @value of size @size from offset @offset within the
551 * configuration space of the device identified by the bus, device & function
552 * numbers in @bdf on the PCI bus @bus.
553 *
554 * Return: 0 on success
555 */
556static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf,
557 uint offset, ulong value,
558 enum pci_size_t size)
559{
560 struct pcie_advk *pcie = dev_get_priv(bus);
Pali Rohár525886e2021-09-26 00:54:42 +0200561 int busno = PCI_BUS(bdf) - dev_seq(bus);
Pali Rohár907cd9a2021-08-27 14:14:44 +0200562 int retry_count;
Pali Rohár525886e2021-09-26 00:54:42 +0200563 ulong data;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800564 uint reg;
Pali Rohár907cd9a2021-08-27 14:14:44 +0200565 int ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800566
567 dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
568 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
569 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
570 offset, size, value);
571
Pali Rohár525886e2021-09-26 00:54:42 +0200572 if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800573 dev_dbg(pcie->dev, "- out of range\n");
574 return 0;
575 }
576
Pali Rohár525886e2021-09-26 00:54:42 +0200577 /*
Pali Rohár0f65c042021-11-11 16:35:48 +0100578 * As explained in pcie_advk_read_config(), PCI Bridge config registers
579 * are available directly in Aardvark memory space starting at offset
580 * zero. Type 1 specific registers are not available, so we write their
581 * content only into driver virtual cfgcache[].
Pali Rohár525886e2021-09-26 00:54:42 +0200582 */
583 if (busno == pcie->first_busno) {
Pali Rohár0f65c042021-11-11 16:35:48 +0100584 if ((offset >= 0x10 && offset < 0x34) ||
585 (offset >= 0x38 && offset < 0x3c)) {
Pali Rohár525886e2021-09-26 00:54:42 +0200586 data = pcie->cfgcache[(offset - 0x10) / 4];
587 data = pci_conv_size_to_32(data, value, offset, size);
Pali Rohárc5cc3ec2021-10-12 13:19:19 +0200588 /* This PCI bridge does not have configurable bars */
589 if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
Pali Rohár0f65c042021-11-11 16:35:48 +0100590 (offset & ~3) == PCI_BASE_ADDRESS_1 ||
591 (offset & ~3) == PCI_ROM_ADDRESS1)
Pali Rohárc5cc3ec2021-10-12 13:19:19 +0200592 data = 0x0;
Pali Rohár525886e2021-09-26 00:54:42 +0200593 pcie->cfgcache[(offset - 0x10) / 4] = data;
Pali Rohár525886e2021-09-26 00:54:42 +0200594 } else {
595 data = advk_readl(pcie, offset & ~3);
596 data = pci_conv_size_to_32(data, value, offset, size);
597 advk_writel(pcie, data, offset & ~3);
598 }
599
600 if (offset == PCI_PRIMARY_BUS)
601 pcie->first_busno = data & 0xff;
602
603 if (offset == PCI_SECONDARY_BUS ||
604 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8))
605 pcie->sec_busno = (data >> 8) & 0xff;
606
Pali Rohár238fbab2021-09-26 00:54:44 +0200607 if ((offset & ~3) == PCIE_CORE_PCIEXP_CAP_OFF + PCI_EXP_RTCTL)
608 pcie->cfgcrssve = data & PCI_EXP_RTCTL_CRSSVE;
609
Pali Rohár525886e2021-09-26 00:54:42 +0200610 return 0;
611 }
612
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200613 if (advk_readl(pcie, PIO_START)) {
614 dev_err(pcie->dev,
615 "Previous PIO read/write transfer is still running\n");
Pali Rohár907cd9a2021-08-27 14:14:44 +0200616 return -EAGAIN;
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200617 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800618
619 /* Program the control register */
620 reg = advk_readl(pcie, PIO_CTRL);
621 reg &= ~PIO_CTRL_TYPE_MASK;
Pali Rohár525886e2021-09-26 00:54:42 +0200622 if (busno == pcie->sec_busno)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800623 reg |= PCIE_CONFIG_WR_TYPE0;
624 else
625 reg |= PCIE_CONFIG_WR_TYPE1;
626 advk_writel(pcie, reg, PIO_CTRL);
627
628 /* Program the address registers */
Pali Rohár525886e2021-09-26 00:54:42 +0200629 reg = PCIE_BDF(busno, PCI_DEV(bdf), PCI_FUNC(bdf)) | PCIE_CONF_REG(offset);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800630 advk_writel(pcie, reg, PIO_ADDR_LS);
631 advk_writel(pcie, 0, PIO_ADDR_MS);
632 dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg);
633
634 /* Program the data register */
635 reg = pci_conv_size_to_32(0, value, offset, size);
636 advk_writel(pcie, reg, PIO_WR_DATA);
637 dev_dbg(pcie->dev, "\tPIO req. - val = 0x%08x\n", reg);
638
639 /* Program the data strobe */
640 reg = pcie_calc_datastrobe(offset, size);
641 advk_writel(pcie, reg, PIO_WR_DATA_STRB);
642 dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg);
643
Pali Rohár907cd9a2021-08-27 14:14:44 +0200644 retry_count = 0;
645
646retry:
Wilson Dinga6bdc862018-03-26 15:57:29 +0800647 /* Start the transfer */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200648 advk_writel(pcie, 1, PIO_ISR);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800649 advk_writel(pcie, 1, PIO_START);
650
Pali Rohár907cd9a2021-08-27 14:14:44 +0200651 ret = pcie_advk_wait_pio(pcie);
652 if (ret < 0)
653 return ret;
654
655 retry_count += ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800656
657 /* Check PIO status */
Pali Rohár907cd9a2021-08-27 14:14:44 +0200658 ret = pcie_advk_check_pio_status(pcie, false, NULL);
659 if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
660 goto retry;
661 return ret;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800662}
663
664/**
665 * pcie_advk_link_up() - Check if PCIe link is up or not
666 *
667 * @pcie: The PCI device to access
668 *
669 * Return 1 (true) on link up.
670 * Return 0 (false) on link down.
671 */
672static int pcie_advk_link_up(struct pcie_advk *pcie)
673{
674 u32 val, ltssm_state;
675
676 val = advk_readl(pcie, CFG_REG);
677 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
Pali Rohár6d11d9e2021-09-26 00:54:41 +0200678 return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800679}
680
681/**
682 * pcie_advk_wait_for_link() - Wait for link training to be accomplished
683 *
684 * @pcie: The PCI device to access
685 *
686 * Wait up to 1 second for link training to be accomplished.
687 *
688 * Return 1 (true) if link training ends up with link up success.
689 * Return 0 (false) if link training ends up with link up failure.
690 */
691static int pcie_advk_wait_for_link(struct pcie_advk *pcie)
692{
693 int retries;
694
695 /* check if the link is up or not */
Pali Rohárfc0eddf2021-04-22 16:23:04 +0200696 for (retries = 0; retries < LINK_MAX_RETRIES; retries++) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800697 if (pcie_advk_link_up(pcie)) {
Pali Rohár525886e2021-09-26 00:54:42 +0200698 printf("PCIe: Link up\n");
Wilson Dinga6bdc862018-03-26 15:57:29 +0800699 return 0;
700 }
701
702 udelay(LINK_WAIT_TIMEOUT);
703 }
704
Pali Rohár525886e2021-09-26 00:54:42 +0200705 printf("PCIe: Link down\n");
Wilson Dinga6bdc862018-03-26 15:57:29 +0800706
707 return -ETIMEDOUT;
708}
709
Pali Roháre78c8e02021-05-26 17:59:40 +0200710/*
711 * Set PCIe address window register which could be used for memory
712 * mapping.
713 */
714static void pcie_advk_set_ob_win(struct pcie_advk *pcie, u8 win_num,
715 phys_addr_t match, phys_addr_t remap,
716 phys_addr_t mask, u32 actions)
717{
718 advk_writel(pcie, OB_WIN_ENABLE |
719 lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
720 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
721 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
722 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
723 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
724 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
725 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
726}
727
728static void pcie_advk_disable_ob_win(struct pcie_advk *pcie, u8 win_num)
729{
730 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
731 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
732 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
733 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
734 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
735 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
736 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
737}
738
739static void pcie_advk_set_ob_region(struct pcie_advk *pcie, int *wins,
740 struct pci_region *region, u32 actions)
741{
742 phys_addr_t phys_start = region->phys_start;
743 pci_addr_t bus_start = region->bus_start;
744 pci_size_t size = region->size;
745 phys_addr_t win_mask;
746 u64 win_size;
747
748 if (*wins == -1)
749 return;
750
751 /*
752 * The n-th PCIe window is configured by tuple (match, remap, mask)
Pali Rohár2fef1db2021-07-08 20:19:00 +0200753 * and an access to address A uses this window if A matches the
Pali Roháre78c8e02021-05-26 17:59:40 +0200754 * match with given mask.
755 * So every PCIe window size must be a power of two and every start
756 * address must be aligned to window size. Minimal size is 64 KiB
Pali Rohár22b1e082021-07-08 20:18:58 +0200757 * because lower 16 bits of mask must be zero. Remapped address
758 * may have set only bits from the mask.
Pali Roháre78c8e02021-05-26 17:59:40 +0200759 */
760 while (*wins < OB_WIN_COUNT && size > 0) {
761 /* Calculate the largest aligned window size */
762 win_size = (1ULL << (fls64(size) - 1)) |
763 (phys_start ? (1ULL << __ffs64(phys_start)) : 0);
764 win_size = 1ULL << __ffs64(win_size);
Pali Rohár22b1e082021-07-08 20:18:58 +0200765 win_mask = ~(win_size - 1);
766 if (win_size < 0x10000 || (bus_start & ~win_mask))
Pali Roháre78c8e02021-05-26 17:59:40 +0200767 break;
768
769 dev_dbg(pcie->dev,
770 "Configuring PCIe window %d: [0x%llx-0x%llx] as 0x%x\n",
771 *wins, (u64)phys_start, (u64)phys_start + win_size,
772 actions);
Pali Roháre78c8e02021-05-26 17:59:40 +0200773 pcie_advk_set_ob_win(pcie, *wins, phys_start, bus_start,
774 win_mask, actions);
775
776 phys_start += win_size;
777 bus_start += win_size;
778 size -= win_size;
779 (*wins)++;
780 }
781
782 if (size > 0) {
783 *wins = -1;
784 dev_err(pcie->dev,
785 "Invalid PCIe region [0x%llx-0x%llx]\n",
786 (u64)region->phys_start,
787 (u64)region->phys_start + region->size);
788 }
789}
790
Wilson Dinga6bdc862018-03-26 15:57:29 +0800791/**
792 * pcie_advk_setup_hw() - PCIe initailzation
793 *
794 * @pcie: The PCI device to access
795 *
796 * Return: 0 on success
797 */
798static int pcie_advk_setup_hw(struct pcie_advk *pcie)
799{
Pali Roháre78c8e02021-05-26 17:59:40 +0200800 struct pci_region *io, *mem, *pref;
801 int i, wins;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800802 u32 reg;
803
804 /* Set to Direct mode */
805 reg = advk_readl(pcie, CTRL_CONFIG_REG);
806 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
807 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
808 advk_writel(pcie, reg, CTRL_CONFIG_REG);
809
810 /* Set PCI global control register to RC mode */
811 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
812 reg |= (IS_RC_MSK << IS_RC_SHIFT);
813 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
814
Pali Rohárba40b6c2021-03-03 14:37:59 +0100815 /*
816 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
817 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
818 * id in high 16 bits. Updating this register changes readback value of
819 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
820 * for erratum 4.1: "The value of device and vendor ID is incorrect".
821 */
822 advk_writel(pcie, 0x11ab11ab, VENDOR_ID_REG);
823
Pali Rohár525886e2021-09-26 00:54:42 +0200824 /*
825 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
826 * because default value is Mass Storage Controller (0x010400), causing
827 * U-Boot to fail to recognize it as P2P Bridge.
828 *
829 * Note that this Aardvark PCI Bridge does not have a compliant Type 1
830 * Configuration Space and it even cannot be accessed via Aardvark's
Pali Rohár0f65c042021-11-11 16:35:48 +0100831 * PCI config space access method. Aardvark PCI Bridge Config space is
Pali Rohár525886e2021-09-26 00:54:42 +0200832 * available in internal Aardvark registers starting at offset 0x0
Pali Rohár0f65c042021-11-11 16:35:48 +0100833 * and has format of Type 0 config space.
834 *
835 * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34)
836 * have the same format in Marvell's specification as in PCIe
837 * specification, but their meaning is totally different (and not even
838 * the same meaning as explained in the corresponding comment in the
839 * pci_mvebu driver; aardvark is still different).
840 *
841 * So our driver converts Type 0 config space to Type 1 and reports
842 * Header Type as Type 1. Access to BAR registers and to non-existent
843 * Type 1 registers is redirected to the virtual cfgcache[] buffer,
844 * which avoids changing unrelated registers.
Pali Rohár525886e2021-09-26 00:54:42 +0200845 */
846 reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
847 reg &= ~0xffffff00;
848 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
849 advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
850
Wilson Dinga6bdc862018-03-26 15:57:29 +0800851 /* Set Advanced Error Capabilities and Control PF0 register */
852 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
853 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
854 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK |
855 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV;
856 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
857
858 /* Set PCIe Device Control and Status 1 PF0 register */
859 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
Pali Rohár9057a2c2021-02-05 15:32:28 +0100860 (PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE <<
861 PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT) |
862 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE <<
863 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT) |
Wilson Dinga6bdc862018-03-26 15:57:29 +0800864 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE;
865 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
866
867 /* Program PCIe Control 2 to disable strict ordering */
868 reg = PCIE_CORE_CTRL2_RESERVED |
869 PCIE_CORE_CTRL2_TD_ENABLE;
870 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
871
872 /* Set GEN2 */
873 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
874 reg &= ~PCIE_GEN_SEL_MSK;
875 reg |= SPEED_GEN_2;
876 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
877
878 /* Set lane X1 */
879 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
880 reg &= ~LANE_CNT_MSK;
881 reg |= LANE_COUNT_1;
882 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
883
884 /* Enable link training */
885 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
886 reg |= LINK_TRAINING_EN;
887 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
888
889 /*
890 * Enable AXI address window location generation:
891 * When it is enabled, the default outbound window
892 * configurations (Default User Field: 0xD0074CFC)
893 * are used to transparent address translation for
894 * the outbound transactions. Thus, PCIe address
Pali Roháre78c8e02021-05-26 17:59:40 +0200895 * windows are not required for transparent memory
896 * access when default outbound window configuration
897 * is set for memory access.
Wilson Dinga6bdc862018-03-26 15:57:29 +0800898 */
899 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
900 reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE;
901 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
902
903 /*
904 * Bypass the address window mapping for PIO:
905 * Since PIO access already contains all required
906 * info over AXI interface by PIO registers, the
907 * address window is not required.
908 */
909 reg = advk_readl(pcie, PIO_CTRL);
910 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
911 advk_writel(pcie, reg, PIO_CTRL);
912
Pali Roháre78c8e02021-05-26 17:59:40 +0200913 /*
914 * Set memory access in Default User Field so it
915 * is not required to configure PCIe address for
916 * transparent memory access.
917 */
918 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
919
920 /*
921 * Configure PCIe address windows for non-memory or
922 * non-transparent access as by default PCIe uses
923 * transparent memory access.
924 */
925 wins = 0;
926 pci_get_regions(pcie->dev, &io, &mem, &pref);
927 if (io)
928 pcie_advk_set_ob_region(pcie, &wins, io, OB_WIN_TYPE_IO);
929 if (mem && mem->phys_start != mem->bus_start)
930 pcie_advk_set_ob_region(pcie, &wins, mem, OB_WIN_TYPE_MEM);
931 if (pref && pref->phys_start != pref->bus_start)
932 pcie_advk_set_ob_region(pcie, &wins, pref, OB_WIN_TYPE_MEM);
933
934 /* Disable remaining PCIe outbound windows */
935 for (i = ((wins >= 0) ? wins : 0); i < OB_WIN_COUNT; i++)
936 pcie_advk_disable_ob_win(pcie, i);
937
938 if (wins == -1)
939 return -EINVAL;
940
Wilson Dinga6bdc862018-03-26 15:57:29 +0800941 /* Wait for PCIe link up */
942 if (pcie_advk_wait_for_link(pcie))
943 return -ENXIO;
944
Wilson Dinga6bdc862018-03-26 15:57:29 +0800945 return 0;
946}
947
948/**
949 * pcie_advk_probe() - Probe the PCIe bus for active link
950 *
951 * @dev: A pointer to the device being operated on
952 *
953 * Probe for an active link on the PCIe bus and configure the controller
954 * to enable this port.
955 *
956 * Return: 0 on success, else -ENODEV
957 */
958static int pcie_advk_probe(struct udevice *dev)
959{
960 struct pcie_advk *pcie = dev_get_priv(dev);
961
Pali Rohár6a58b972020-08-19 15:57:07 +0200962 gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800963 GPIOD_IS_OUT);
964 /*
965 * Issue reset to add-in card through the dedicated GPIO.
966 * Some boards are connecting the card reset pin to common system
967 * reset wire and others are using separate GPIO port.
968 * In the last case we have to release a reset of the addon card
969 * using this GPIO.
970 *
971 * FIX-ME:
972 * The PCIe RESET signal is not supposed to be released along
973 * with the SOC RESET signal. It should be lowered as early as
974 * possible before PCIe PHY initialization. Moreover, the PCIe
975 * clock should be gated as well.
976 */
Pali Rohár6a58b972020-08-19 15:57:07 +0200977 if (dm_gpio_is_valid(&pcie->reset_gpio)) {
Pali Roháre3271bb2021-01-18 12:09:33 +0100978 dev_dbg(dev, "Toggle PCIE Reset GPIO ...\n");
Pali Rohár6a58b972020-08-19 15:57:07 +0200979 dm_gpio_set_value(&pcie->reset_gpio, 1);
Pali Rohár0130a612020-08-19 15:57:06 +0200980 mdelay(200);
Pali Rohár6a58b972020-08-19 15:57:07 +0200981 dm_gpio_set_value(&pcie->reset_gpio, 0);
Pali Rohár5c6edca2020-08-25 10:45:04 +0200982 } else {
Pali Roháre3271bb2021-01-18 12:09:33 +0100983 dev_warn(dev, "PCIE Reset on GPIO support is missing\n");
Wilson Dinga6bdc862018-03-26 15:57:29 +0800984 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800985
Wilson Dinga6bdc862018-03-26 15:57:29 +0800986 pcie->dev = pci_get_controller(dev);
987
Pali Rohár525886e2021-09-26 00:54:42 +0200988 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
989 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
990 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
991 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
992 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
993
Wilson Dinga6bdc862018-03-26 15:57:29 +0800994 return pcie_advk_setup_hw(pcie);
995}
996
Pali Rohár6a58b972020-08-19 15:57:07 +0200997static int pcie_advk_remove(struct udevice *dev)
998{
Pali Rohár6a58b972020-08-19 15:57:07 +0200999 struct pcie_advk *pcie = dev_get_priv(dev);
Pali Rohárff876992020-09-22 13:21:38 +02001000 u32 reg;
Pali Roháre78c8e02021-05-26 17:59:40 +02001001 int i;
1002
1003 for (i = 0; i < OB_WIN_COUNT; i++)
1004 pcie_advk_disable_ob_win(pcie, i);
Pali Rohár6a58b972020-08-19 15:57:07 +02001005
Pali Roháread96342021-05-26 17:59:35 +02001006 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
1007 reg &= ~(PCIE_CORE_CMD_MEM_ACCESS_EN |
1008 PCIE_CORE_CMD_IO_ACCESS_EN |
1009 PCIE_CORE_CMD_MEM_IO_REQ_EN);
1010 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
1011
Pali Rohárff876992020-09-22 13:21:38 +02001012 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
1013 reg &= ~LINK_TRAINING_EN;
1014 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
1015
Pali Rohár6a58b972020-08-19 15:57:07 +02001016 return 0;
1017}
1018
Wilson Dinga6bdc862018-03-26 15:57:29 +08001019/**
Simon Glassaad29ae2020-12-03 16:55:21 -07001020 * pcie_advk_of_to_plat() - Translate from DT to device state
Wilson Dinga6bdc862018-03-26 15:57:29 +08001021 *
1022 * @dev: A pointer to the device being operated on
1023 *
1024 * Translate relevant data from the device tree pertaining to device @dev into
1025 * state that the driver will later make use of. This state is stored in the
1026 * device's private data structure.
1027 *
1028 * Return: 0 on success, else -EINVAL
1029 */
Simon Glassaad29ae2020-12-03 16:55:21 -07001030static int pcie_advk_of_to_plat(struct udevice *dev)
Wilson Dinga6bdc862018-03-26 15:57:29 +08001031{
1032 struct pcie_advk *pcie = dev_get_priv(dev);
1033
1034 /* Get the register base address */
1035 pcie->base = (void *)dev_read_addr_index(dev, 0);
1036 if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE)
1037 return -EINVAL;
1038
1039 return 0;
1040}
1041
1042static const struct dm_pci_ops pcie_advk_ops = {
1043 .read_config = pcie_advk_read_config,
1044 .write_config = pcie_advk_write_config,
1045};
1046
1047static const struct udevice_id pcie_advk_ids[] = {
Pali Rohár678cf9d2021-05-26 17:59:36 +02001048 { .compatible = "marvell,armada-3700-pcie" },
Wilson Dinga6bdc862018-03-26 15:57:29 +08001049 { }
1050};
1051
1052U_BOOT_DRIVER(pcie_advk) = {
1053 .name = "pcie_advk",
1054 .id = UCLASS_PCI,
1055 .of_match = pcie_advk_ids,
1056 .ops = &pcie_advk_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -07001057 .of_to_plat = pcie_advk_of_to_plat,
Wilson Dinga6bdc862018-03-26 15:57:29 +08001058 .probe = pcie_advk_probe,
Pali Rohár6a58b972020-08-19 15:57:07 +02001059 .remove = pcie_advk_remove,
1060 .flags = DM_FLAG_OS_PREPARE,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001061 .priv_auto = sizeof(struct pcie_advk),
Wilson Dinga6bdc862018-03-26 15:57:29 +08001062};