blob: d3ef8f203d97a7928baf9433225e57fb533c92d8 [file] [log] [blame]
Wilson Dinge51f2b12018-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 Glass336d4612020-02-03 07:36:16 -070032#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060033#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060034#include <linux/delay.h>
Wilson Dinge51f2b12018-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)
42#define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
43#define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
44#define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
Pali Rohárcba6edd2021-02-05 15:32:28 +010045#define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE 0x2
46#define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT 5
47#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE 0x2
48#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
Wilson Dinge51f2b12018-03-26 15:57:29 +080049#define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
50#define PCIE_CORE_LINK_TRAINING BIT(5)
51#define PCIE_CORE_ERR_CAPCTL_REG 0x118
52#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
53#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
54#define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK BIT(7)
55#define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV BIT(8)
56
57/* PIO registers base address and register offsets */
58#define PIO_BASE_ADDR 0x4000
59#define PIO_CTRL (PIO_BASE_ADDR + 0x0)
60#define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
61#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
62#define PIO_STAT (PIO_BASE_ADDR + 0x4)
63#define PIO_COMPLETION_STATUS_SHIFT 7
64#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
65#define PIO_COMPLETION_STATUS_OK 0
66#define PIO_COMPLETION_STATUS_UR 1
67#define PIO_COMPLETION_STATUS_CRS 2
68#define PIO_COMPLETION_STATUS_CA 4
69#define PIO_NON_POSTED_REQ BIT(10)
70#define PIO_ERR_STATUS BIT(11)
71#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
72#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
73#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
74#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
75#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
76#define PIO_START (PIO_BASE_ADDR + 0x1c)
77#define PIO_ISR (PIO_BASE_ADDR + 0x20)
78
79/* Aardvark Control registers */
80#define CONTROL_BASE_ADDR 0x4800
81#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
82#define PCIE_GEN_SEL_MSK 0x3
83#define PCIE_GEN_SEL_SHIFT 0x0
84#define SPEED_GEN_1 0
85#define SPEED_GEN_2 1
86#define SPEED_GEN_3 2
87#define IS_RC_MSK 1
88#define IS_RC_SHIFT 2
89#define LANE_CNT_MSK 0x18
90#define LANE_CNT_SHIFT 0x3
91#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
92#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
93#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
94#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
95#define LINK_TRAINING_EN BIT(6)
96#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
97#define PCIE_CORE_CTRL2_RESERVED 0x7
98#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
99#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
100#define PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE BIT(6)
101
Pali Rohárb3217222021-05-26 17:59:40 +0200102/* PCIe window configuration */
103#define OB_WIN_BASE_ADDR 0x4c00
104#define OB_WIN_BLOCK_SIZE 0x20
105#define OB_WIN_COUNT 8
106#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
107 OB_WIN_BLOCK_SIZE * (win) + \
108 (offset))
109#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
110#define OB_WIN_ENABLE BIT(0)
111#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
112#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
113#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
114#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
115#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
116#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
117#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
118#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
119#define OB_WIN_FUNC_NUM_SHIFT 24
120#define OB_WIN_FUNC_NUM_ENABLE BIT(23)
121#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
122#define OB_WIN_BUS_NUM_BITS_SHIFT 20
123#define OB_WIN_MSG_CODE_ENABLE BIT(22)
124#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
125#define OB_WIN_MSG_CODE_SHIFT 14
126#define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
127#define OB_WIN_ATTR_ENABLE BIT(11)
128#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
129#define OB_WIN_ATTR_TC_SHIFT 8
130#define OB_WIN_ATTR_RELAXED BIT(7)
131#define OB_WIN_ATTR_NOSNOOP BIT(6)
132#define OB_WIN_ATTR_POISON BIT(5)
133#define OB_WIN_ATTR_IDO BIT(4)
134#define OB_WIN_TYPE_MASK GENMASK(3, 0)
135#define OB_WIN_TYPE_SHIFT 0
136#define OB_WIN_TYPE_MEM 0x0
137#define OB_WIN_TYPE_IO 0x4
138#define OB_WIN_TYPE_CONFIG_TYPE0 0x8
139#define OB_WIN_TYPE_CONFIG_TYPE1 0x9
140#define OB_WIN_TYPE_MSG 0xc
141
Wilson Dinge51f2b12018-03-26 15:57:29 +0800142/* LMI registers base address and register offsets */
143#define LMI_BASE_ADDR 0x6000
144#define CFG_REG (LMI_BASE_ADDR + 0x0)
145#define LTSSM_SHIFT 24
146#define LTSSM_MASK 0x3f
147#define LTSSM_L0 0x10
Pali Rohár2fa30d02021-03-03 14:37:59 +0100148#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
Wilson Dinge51f2b12018-03-26 15:57:29 +0800149
150/* PCIe core controller registers */
151#define CTRL_CORE_BASE_ADDR 0x18000
152#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
153#define CTRL_MODE_SHIFT 0x0
154#define CTRL_MODE_MASK 0x1
155#define PCIE_CORE_MODE_DIRECT 0x0
156#define PCIE_CORE_MODE_COMMAND 0x1
157
158/* Transaction types */
159#define PCIE_CONFIG_RD_TYPE0 0x8
160#define PCIE_CONFIG_RD_TYPE1 0x9
161#define PCIE_CONFIG_WR_TYPE0 0xa
162#define PCIE_CONFIG_WR_TYPE1 0xb
163
164/* PCI_BDF shifts 8bit, so we need extra 4bit shift */
165#define PCIE_BDF(dev) (dev << 4)
166#define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
167#define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
168#define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
169#define PCIE_CONF_REG(reg) ((reg) & 0xffc)
170#define PCIE_CONF_ADDR(bus, devfn, where) \
171 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
172 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
173
174/* PCIe Retries & Timeout definitions */
Pali Roháreccbd4a2021-04-22 16:23:04 +0200175#define PIO_MAX_RETRIES 1500
176#define PIO_WAIT_TIMEOUT 1000
177#define LINK_MAX_RETRIES 10
Wilson Dinge51f2b12018-03-26 15:57:29 +0800178#define LINK_WAIT_TIMEOUT 100000
179
Wilson Dinge51f2b12018-03-26 15:57:29 +0800180#define CFG_RD_CRS_VAL 0xFFFF0001
181
Wilson Dinge51f2b12018-03-26 15:57:29 +0800182/**
183 * struct pcie_advk - Advk PCIe controller state
184 *
185 * @reg_base: The base address of the register space.
186 * @first_busno: This driver supports multiple PCIe controllers.
187 * first_busno stores the bus number of the PCIe root-port
188 * number which may vary depending on the PCIe setup
189 * (PEX switches etc).
190 * @device: The pointer to PCI uclass device.
191 */
192struct pcie_advk {
193 void *base;
194 int first_busno;
195 struct udevice *dev;
Pali Rohár828d3262020-08-19 15:57:07 +0200196 struct gpio_desc reset_gpio;
Wilson Dinge51f2b12018-03-26 15:57:29 +0800197};
198
199static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg)
200{
201 writel(val, pcie->base + reg);
202}
203
204static inline uint advk_readl(struct pcie_advk *pcie, uint reg)
205{
206 return readl(pcie->base + reg);
207}
208
209/**
210 * pcie_advk_addr_valid() - Check for valid bus address
211 *
212 * @bdf: The PCI device to access
213 * @first_busno: Bus number of the PCIe controller root complex
214 *
215 * Return: 1 on valid, 0 on invalid
216 */
217static int pcie_advk_addr_valid(pci_dev_t bdf, int first_busno)
218{
219 /*
220 * In PCIE-E only a single device (0) can exist
221 * on the local bus. Beyound the local bus, there might be
222 * a Switch and everything is possible.
223 */
224 if ((PCI_BUS(bdf) == first_busno) && (PCI_DEV(bdf) > 0))
225 return 0;
226
227 return 1;
228}
229
230/**
231 * pcie_advk_wait_pio() - Wait for PIO access to be accomplished
232 *
233 * @pcie: The PCI device to access
234 *
Pali Roháreccbd4a2021-04-22 16:23:04 +0200235 * Wait up to 1.5 seconds for PIO access to be accomplished.
Wilson Dinge51f2b12018-03-26 15:57:29 +0800236 *
237 * Return 1 (true) if PIO access is accomplished.
238 * Return 0 (false) if PIO access is timed out.
239 */
240static int pcie_advk_wait_pio(struct pcie_advk *pcie)
241{
242 uint start, isr;
243 uint count;
244
Pali Roháreccbd4a2021-04-22 16:23:04 +0200245 for (count = 0; count < PIO_MAX_RETRIES; count++) {
Wilson Dinge51f2b12018-03-26 15:57:29 +0800246 start = advk_readl(pcie, PIO_START);
247 isr = advk_readl(pcie, PIO_ISR);
248 if (!start && isr)
249 return 1;
250 /*
251 * Do not check the PIO state too frequently,
252 * 100us delay is appropriate.
253 */
254 udelay(PIO_WAIT_TIMEOUT);
255 }
256
Pali Roháreccbd4a2021-04-22 16:23:04 +0200257 dev_err(pcie->dev, "PIO read/write transfer time out\n");
Wilson Dinge51f2b12018-03-26 15:57:29 +0800258 return 0;
259}
260
261/**
262 * pcie_advk_check_pio_status() - Validate PIO status and get the read result
263 *
264 * @pcie: Pointer to the PCI bus
Pali Rohár4cd61c42021-08-09 09:53:13 +0200265 * @allow_crs: Only for read requests, if CRS response is allowed
266 * @read_val: Pointer to the read result
Wilson Dinge51f2b12018-03-26 15:57:29 +0800267 *
268 */
269static int pcie_advk_check_pio_status(struct pcie_advk *pcie,
Pali Rohár4cd61c42021-08-09 09:53:13 +0200270 bool allow_crs,
Wilson Dinge51f2b12018-03-26 15:57:29 +0800271 uint *read_val)
272{
273 uint reg;
274 unsigned int status;
275 char *strcomp_status, *str_posted;
276
277 reg = advk_readl(pcie, PIO_STAT);
278 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
279 PIO_COMPLETION_STATUS_SHIFT;
280
281 switch (status) {
282 case PIO_COMPLETION_STATUS_OK:
283 if (reg & PIO_ERR_STATUS) {
284 strcomp_status = "COMP_ERR";
285 break;
286 }
287 /* Get the read result */
Pali Rohár4cd61c42021-08-09 09:53:13 +0200288 if (read_val)
Wilson Dinge51f2b12018-03-26 15:57:29 +0800289 *read_val = advk_readl(pcie, PIO_RD_DATA);
290 /* No error */
291 strcomp_status = NULL;
292 break;
293 case PIO_COMPLETION_STATUS_UR:
Pali Rohár4cd61c42021-08-09 09:53:13 +0200294 strcomp_status = "UR";
Wilson Dinge51f2b12018-03-26 15:57:29 +0800295 break;
296 case PIO_COMPLETION_STATUS_CRS:
Pali Rohár4cd61c42021-08-09 09:53:13 +0200297 if (allow_crs && read_val) {
Wilson Dinge51f2b12018-03-26 15:57:29 +0800298 /* For reading, CRS is not an error status. */
299 *read_val = CFG_RD_CRS_VAL;
300 strcomp_status = NULL;
301 } else {
302 strcomp_status = "CRS";
303 }
304 break;
305 case PIO_COMPLETION_STATUS_CA:
306 strcomp_status = "CA";
307 break;
308 default:
309 strcomp_status = "Unknown";
310 break;
311 }
312
313 if (!strcomp_status)
314 return 0;
315
316 if (reg & PIO_NON_POSTED_REQ)
317 str_posted = "Non-posted";
318 else
319 str_posted = "Posted";
320
321 dev_err(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
322 str_posted, strcomp_status, reg,
323 advk_readl(pcie, PIO_ADDR_LS));
324
325 return -EFAULT;
326}
327
328/**
329 * pcie_advk_read_config() - Read from configuration space
330 *
331 * @bus: Pointer to the PCI bus
332 * @bdf: Identifies the PCIe device to access
333 * @offset: The offset into the device's configuration space
334 * @valuep: A pointer at which to store the read value
335 * @size: Indicates the size of access to perform
336 *
337 * Read a value of size @size from offset @offset within the configuration
338 * space of the device identified by the bus, device & function numbers in @bdf
339 * on the PCI bus @bus.
340 *
341 * Return: 0 on success
342 */
Simon Glassc4e72c42020-01-27 08:49:37 -0700343static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf,
Wilson Dinge51f2b12018-03-26 15:57:29 +0800344 uint offset, ulong *valuep,
345 enum pci_size_t size)
346{
347 struct pcie_advk *pcie = dev_get_priv(bus);
Pali Rohár4cd61c42021-08-09 09:53:13 +0200348 bool allow_crs;
Wilson Dinge51f2b12018-03-26 15:57:29 +0800349 uint reg;
350 int ret;
351
352 dev_dbg(pcie->dev, "PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
353 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
354
355 if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
356 dev_dbg(pcie->dev, "- out of range\n");
357 *valuep = pci_get_ff(size);
358 return 0;
359 }
360
Pali Rohár758262b2021-08-27 14:14:43 +0200361 /*
362 * Returning fabricated CRS value (0xFFFF0001) by PCIe Root Complex to
363 * OS is allowed only for 4-byte PCI_VENDOR_ID config read request and
364 * only when CRSSVE bit in Root Port PCIe device is enabled. In all
365 * other error PCIe Root Complex must return all-ones.
366 * Aardvark HW does not have Root Port PCIe device and U-Boot does not
367 * implement emulation of this device.
368 * U-Boot currently does not support handling of CRS return value for
369 * PCI_VENDOR_ID config read request and also does not set CRSSVE bit.
370 * Therefore disable returning CRS response for now.
371 */
372 allow_crs = false;
Pali Rohár4cd61c42021-08-09 09:53:13 +0200373
Pali Roháreccbd4a2021-04-22 16:23:04 +0200374 if (advk_readl(pcie, PIO_START)) {
375 dev_err(pcie->dev,
376 "Previous PIO read/write transfer is still running\n");
Pali Rohár4cd61c42021-08-09 09:53:13 +0200377 if (allow_crs) {
378 *valuep = CFG_RD_CRS_VAL;
379 return 0;
380 }
381 *valuep = pci_get_ff(size);
382 return -EINVAL;
Pali Roháreccbd4a2021-04-22 16:23:04 +0200383 }
Wilson Dinge51f2b12018-03-26 15:57:29 +0800384
385 /* Program the control register */
386 reg = advk_readl(pcie, PIO_CTRL);
387 reg &= ~PIO_CTRL_TYPE_MASK;
388 if (PCI_BUS(bdf) == pcie->first_busno)
389 reg |= PCIE_CONFIG_RD_TYPE0;
390 else
391 reg |= PCIE_CONFIG_RD_TYPE1;
392 advk_writel(pcie, reg, PIO_CTRL);
393
394 /* Program the address registers */
395 reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
396 advk_writel(pcie, reg, PIO_ADDR_LS);
397 advk_writel(pcie, 0, PIO_ADDR_MS);
398
399 /* Start the transfer */
Pali Roháreccbd4a2021-04-22 16:23:04 +0200400 advk_writel(pcie, 1, PIO_ISR);
Wilson Dinge51f2b12018-03-26 15:57:29 +0800401 advk_writel(pcie, 1, PIO_START);
402
Pali Roháreccbd4a2021-04-22 16:23:04 +0200403 if (!pcie_advk_wait_pio(pcie)) {
Pali Rohár4cd61c42021-08-09 09:53:13 +0200404 if (allow_crs) {
405 *valuep = CFG_RD_CRS_VAL;
406 return 0;
407 }
408 *valuep = pci_get_ff(size);
409 return -EINVAL;
Pali Roháreccbd4a2021-04-22 16:23:04 +0200410 }
Wilson Dinge51f2b12018-03-26 15:57:29 +0800411
412 /* Check PIO status and get the read result */
Pali Rohár4cd61c42021-08-09 09:53:13 +0200413 ret = pcie_advk_check_pio_status(pcie, allow_crs, &reg);
414 if (ret) {
415 *valuep = pci_get_ff(size);
Wilson Dinge51f2b12018-03-26 15:57:29 +0800416 return ret;
Pali Rohár4cd61c42021-08-09 09:53:13 +0200417 }
Wilson Dinge51f2b12018-03-26 15:57:29 +0800418
419 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
420 offset, size, reg);
421 *valuep = pci_conv_32_to_size(reg, offset, size);
422
423 return 0;
424}
425
426/**
427 * pcie_calc_datastrobe() - Calculate data strobe
428 *
429 * @offset: The offset into the device's configuration space
430 * @size: Indicates the size of access to perform
431 *
432 * Calculate data strobe according to offset and size
433 *
434 */
435static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size)
436{
437 uint bytes, data_strobe;
438
439 switch (size) {
440 case PCI_SIZE_8:
441 bytes = 1;
442 break;
443 case PCI_SIZE_16:
444 bytes = 2;
445 break;
446 default:
447 bytes = 4;
448 }
449
450 data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3);
451
452 return data_strobe;
453}
454
455/**
456 * pcie_advk_write_config() - Write to configuration space
457 *
458 * @bus: Pointer to the PCI bus
459 * @bdf: Identifies the PCIe device to access
460 * @offset: The offset into the device's configuration space
461 * @value: The value to write
462 * @size: Indicates the size of access to perform
463 *
464 * Write the value @value of size @size from offset @offset within the
465 * configuration space of the device identified by the bus, device & function
466 * numbers in @bdf on the PCI bus @bus.
467 *
468 * Return: 0 on success
469 */
470static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf,
471 uint offset, ulong value,
472 enum pci_size_t size)
473{
474 struct pcie_advk *pcie = dev_get_priv(bus);
475 uint reg;
476
477 dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
478 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
479 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
480 offset, size, value);
481
482 if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
483 dev_dbg(pcie->dev, "- out of range\n");
484 return 0;
485 }
486
Pali Roháreccbd4a2021-04-22 16:23:04 +0200487 if (advk_readl(pcie, PIO_START)) {
488 dev_err(pcie->dev,
489 "Previous PIO read/write transfer is still running\n");
490 return -EINVAL;
491 }
Wilson Dinge51f2b12018-03-26 15:57:29 +0800492
493 /* Program the control register */
494 reg = advk_readl(pcie, PIO_CTRL);
495 reg &= ~PIO_CTRL_TYPE_MASK;
496 if (PCI_BUS(bdf) == pcie->first_busno)
497 reg |= PCIE_CONFIG_WR_TYPE0;
498 else
499 reg |= PCIE_CONFIG_WR_TYPE1;
500 advk_writel(pcie, reg, PIO_CTRL);
501
502 /* Program the address registers */
503 reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
504 advk_writel(pcie, reg, PIO_ADDR_LS);
505 advk_writel(pcie, 0, PIO_ADDR_MS);
506 dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg);
507
508 /* Program the data register */
509 reg = pci_conv_size_to_32(0, value, offset, size);
510 advk_writel(pcie, reg, PIO_WR_DATA);
511 dev_dbg(pcie->dev, "\tPIO req. - val = 0x%08x\n", reg);
512
513 /* Program the data strobe */
514 reg = pcie_calc_datastrobe(offset, size);
515 advk_writel(pcie, reg, PIO_WR_DATA_STRB);
516 dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg);
517
518 /* Start the transfer */
Pali Roháreccbd4a2021-04-22 16:23:04 +0200519 advk_writel(pcie, 1, PIO_ISR);
Wilson Dinge51f2b12018-03-26 15:57:29 +0800520 advk_writel(pcie, 1, PIO_START);
521
522 if (!pcie_advk_wait_pio(pcie)) {
Wilson Dinge51f2b12018-03-26 15:57:29 +0800523 return -EINVAL;
524 }
525
526 /* Check PIO status */
Pali Rohár4cd61c42021-08-09 09:53:13 +0200527 return pcie_advk_check_pio_status(pcie, false, NULL);
Wilson Dinge51f2b12018-03-26 15:57:29 +0800528}
529
530/**
531 * pcie_advk_link_up() - Check if PCIe link is up or not
532 *
533 * @pcie: The PCI device to access
534 *
535 * Return 1 (true) on link up.
536 * Return 0 (false) on link down.
537 */
538static int pcie_advk_link_up(struct pcie_advk *pcie)
539{
540 u32 val, ltssm_state;
541
542 val = advk_readl(pcie, CFG_REG);
543 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
544 return ltssm_state >= LTSSM_L0;
545}
546
547/**
548 * pcie_advk_wait_for_link() - Wait for link training to be accomplished
549 *
550 * @pcie: The PCI device to access
551 *
552 * Wait up to 1 second for link training to be accomplished.
553 *
554 * Return 1 (true) if link training ends up with link up success.
555 * Return 0 (false) if link training ends up with link up failure.
556 */
557static int pcie_advk_wait_for_link(struct pcie_advk *pcie)
558{
559 int retries;
560
561 /* check if the link is up or not */
Pali Roháreccbd4a2021-04-22 16:23:04 +0200562 for (retries = 0; retries < LINK_MAX_RETRIES; retries++) {
Wilson Dinge51f2b12018-03-26 15:57:29 +0800563 if (pcie_advk_link_up(pcie)) {
564 printf("PCIE-%d: Link up\n", pcie->first_busno);
565 return 0;
566 }
567
568 udelay(LINK_WAIT_TIMEOUT);
569 }
570
571 printf("PCIE-%d: Link down\n", pcie->first_busno);
572
573 return -ETIMEDOUT;
574}
575
Pali Rohárb3217222021-05-26 17:59:40 +0200576/*
577 * Set PCIe address window register which could be used for memory
578 * mapping.
579 */
580static void pcie_advk_set_ob_win(struct pcie_advk *pcie, u8 win_num,
581 phys_addr_t match, phys_addr_t remap,
582 phys_addr_t mask, u32 actions)
583{
584 advk_writel(pcie, OB_WIN_ENABLE |
585 lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
586 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
587 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
588 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
589 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
590 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
591 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
592}
593
594static void pcie_advk_disable_ob_win(struct pcie_advk *pcie, u8 win_num)
595{
596 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
597 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
598 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
599 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
600 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
601 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
602 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
603}
604
605static void pcie_advk_set_ob_region(struct pcie_advk *pcie, int *wins,
606 struct pci_region *region, u32 actions)
607{
608 phys_addr_t phys_start = region->phys_start;
609 pci_addr_t bus_start = region->bus_start;
610 pci_size_t size = region->size;
611 phys_addr_t win_mask;
612 u64 win_size;
613
614 if (*wins == -1)
615 return;
616
617 /*
618 * The n-th PCIe window is configured by tuple (match, remap, mask)
Pali Rohár960d4592021-07-08 20:19:00 +0200619 * and an access to address A uses this window if A matches the
Pali Rohárb3217222021-05-26 17:59:40 +0200620 * match with given mask.
621 * So every PCIe window size must be a power of two and every start
622 * address must be aligned to window size. Minimal size is 64 KiB
Pali Rohára8314952021-07-08 20:18:58 +0200623 * because lower 16 bits of mask must be zero. Remapped address
624 * may have set only bits from the mask.
Pali Rohárb3217222021-05-26 17:59:40 +0200625 */
626 while (*wins < OB_WIN_COUNT && size > 0) {
627 /* Calculate the largest aligned window size */
628 win_size = (1ULL << (fls64(size) - 1)) |
629 (phys_start ? (1ULL << __ffs64(phys_start)) : 0);
630 win_size = 1ULL << __ffs64(win_size);
Pali Rohára8314952021-07-08 20:18:58 +0200631 win_mask = ~(win_size - 1);
632 if (win_size < 0x10000 || (bus_start & ~win_mask))
Pali Rohárb3217222021-05-26 17:59:40 +0200633 break;
634
635 dev_dbg(pcie->dev,
636 "Configuring PCIe window %d: [0x%llx-0x%llx] as 0x%x\n",
637 *wins, (u64)phys_start, (u64)phys_start + win_size,
638 actions);
Pali Rohárb3217222021-05-26 17:59:40 +0200639 pcie_advk_set_ob_win(pcie, *wins, phys_start, bus_start,
640 win_mask, actions);
641
642 phys_start += win_size;
643 bus_start += win_size;
644 size -= win_size;
645 (*wins)++;
646 }
647
648 if (size > 0) {
649 *wins = -1;
650 dev_err(pcie->dev,
651 "Invalid PCIe region [0x%llx-0x%llx]\n",
652 (u64)region->phys_start,
653 (u64)region->phys_start + region->size);
654 }
655}
656
Wilson Dinge51f2b12018-03-26 15:57:29 +0800657/**
658 * pcie_advk_setup_hw() - PCIe initailzation
659 *
660 * @pcie: The PCI device to access
661 *
662 * Return: 0 on success
663 */
664static int pcie_advk_setup_hw(struct pcie_advk *pcie)
665{
Pali Rohárb3217222021-05-26 17:59:40 +0200666 struct pci_region *io, *mem, *pref;
667 int i, wins;
Wilson Dinge51f2b12018-03-26 15:57:29 +0800668 u32 reg;
669
670 /* Set to Direct mode */
671 reg = advk_readl(pcie, CTRL_CONFIG_REG);
672 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
673 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
674 advk_writel(pcie, reg, CTRL_CONFIG_REG);
675
676 /* Set PCI global control register to RC mode */
677 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
678 reg |= (IS_RC_MSK << IS_RC_SHIFT);
679 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
680
Pali Rohár2fa30d02021-03-03 14:37:59 +0100681 /*
682 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
683 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
684 * id in high 16 bits. Updating this register changes readback value of
685 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
686 * for erratum 4.1: "The value of device and vendor ID is incorrect".
687 */
688 advk_writel(pcie, 0x11ab11ab, VENDOR_ID_REG);
689
Wilson Dinge51f2b12018-03-26 15:57:29 +0800690 /* Set Advanced Error Capabilities and Control PF0 register */
691 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
692 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
693 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK |
694 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV;
695 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
696
697 /* Set PCIe Device Control and Status 1 PF0 register */
698 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
Pali Rohárcba6edd2021-02-05 15:32:28 +0100699 (PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE <<
700 PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT) |
701 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE <<
702 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT) |
Wilson Dinge51f2b12018-03-26 15:57:29 +0800703 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE;
704 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
705
706 /* Program PCIe Control 2 to disable strict ordering */
707 reg = PCIE_CORE_CTRL2_RESERVED |
708 PCIE_CORE_CTRL2_TD_ENABLE;
709 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
710
711 /* Set GEN2 */
712 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
713 reg &= ~PCIE_GEN_SEL_MSK;
714 reg |= SPEED_GEN_2;
715 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
716
717 /* Set lane X1 */
718 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
719 reg &= ~LANE_CNT_MSK;
720 reg |= LANE_COUNT_1;
721 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
722
723 /* Enable link training */
724 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
725 reg |= LINK_TRAINING_EN;
726 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
727
728 /*
729 * Enable AXI address window location generation:
730 * When it is enabled, the default outbound window
731 * configurations (Default User Field: 0xD0074CFC)
732 * are used to transparent address translation for
733 * the outbound transactions. Thus, PCIe address
Pali Rohárb3217222021-05-26 17:59:40 +0200734 * windows are not required for transparent memory
735 * access when default outbound window configuration
736 * is set for memory access.
Wilson Dinge51f2b12018-03-26 15:57:29 +0800737 */
738 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
739 reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE;
740 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
741
742 /*
743 * Bypass the address window mapping for PIO:
744 * Since PIO access already contains all required
745 * info over AXI interface by PIO registers, the
746 * address window is not required.
747 */
748 reg = advk_readl(pcie, PIO_CTRL);
749 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
750 advk_writel(pcie, reg, PIO_CTRL);
751
Pali Rohárb3217222021-05-26 17:59:40 +0200752 /*
753 * Set memory access in Default User Field so it
754 * is not required to configure PCIe address for
755 * transparent memory access.
756 */
757 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
758
759 /*
760 * Configure PCIe address windows for non-memory or
761 * non-transparent access as by default PCIe uses
762 * transparent memory access.
763 */
764 wins = 0;
765 pci_get_regions(pcie->dev, &io, &mem, &pref);
766 if (io)
767 pcie_advk_set_ob_region(pcie, &wins, io, OB_WIN_TYPE_IO);
768 if (mem && mem->phys_start != mem->bus_start)
769 pcie_advk_set_ob_region(pcie, &wins, mem, OB_WIN_TYPE_MEM);
770 if (pref && pref->phys_start != pref->bus_start)
771 pcie_advk_set_ob_region(pcie, &wins, pref, OB_WIN_TYPE_MEM);
772
773 /* Disable remaining PCIe outbound windows */
774 for (i = ((wins >= 0) ? wins : 0); i < OB_WIN_COUNT; i++)
775 pcie_advk_disable_ob_win(pcie, i);
776
777 if (wins == -1)
778 return -EINVAL;
779
Wilson Dinge51f2b12018-03-26 15:57:29 +0800780 /* Wait for PCIe link up */
781 if (pcie_advk_wait_for_link(pcie))
782 return -ENXIO;
783
784 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
785 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
786 PCIE_CORE_CMD_IO_ACCESS_EN |
787 PCIE_CORE_CMD_MEM_IO_REQ_EN;
788 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
789
790 return 0;
791}
792
793/**
794 * pcie_advk_probe() - Probe the PCIe bus for active link
795 *
796 * @dev: A pointer to the device being operated on
797 *
798 * Probe for an active link on the PCIe bus and configure the controller
799 * to enable this port.
800 *
801 * Return: 0 on success, else -ENODEV
802 */
803static int pcie_advk_probe(struct udevice *dev)
804{
805 struct pcie_advk *pcie = dev_get_priv(dev);
806
Pali Rohár828d3262020-08-19 15:57:07 +0200807 gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio,
Wilson Dinge51f2b12018-03-26 15:57:29 +0800808 GPIOD_IS_OUT);
809 /*
810 * Issue reset to add-in card through the dedicated GPIO.
811 * Some boards are connecting the card reset pin to common system
812 * reset wire and others are using separate GPIO port.
813 * In the last case we have to release a reset of the addon card
814 * using this GPIO.
815 *
816 * FIX-ME:
817 * The PCIe RESET signal is not supposed to be released along
818 * with the SOC RESET signal. It should be lowered as early as
819 * possible before PCIe PHY initialization. Moreover, the PCIe
820 * clock should be gated as well.
821 */
Pali Rohár828d3262020-08-19 15:57:07 +0200822 if (dm_gpio_is_valid(&pcie->reset_gpio)) {
Pali Rohár279b5732021-01-18 12:09:33 +0100823 dev_dbg(dev, "Toggle PCIE Reset GPIO ...\n");
Pali Rohár828d3262020-08-19 15:57:07 +0200824 dm_gpio_set_value(&pcie->reset_gpio, 1);
Pali Rohár563b85b2020-08-19 15:57:06 +0200825 mdelay(200);
Pali Rohár828d3262020-08-19 15:57:07 +0200826 dm_gpio_set_value(&pcie->reset_gpio, 0);
Pali Rohár835d9692020-08-25 10:45:04 +0200827 } else {
Pali Rohár279b5732021-01-18 12:09:33 +0100828 dev_warn(dev, "PCIE Reset on GPIO support is missing\n");
Wilson Dinge51f2b12018-03-26 15:57:29 +0800829 }
Wilson Dinge51f2b12018-03-26 15:57:29 +0800830
Simon Glass8b85dfc2020-12-16 21:20:07 -0700831 pcie->first_busno = dev_seq(dev);
Wilson Dinge51f2b12018-03-26 15:57:29 +0800832 pcie->dev = pci_get_controller(dev);
833
834 return pcie_advk_setup_hw(pcie);
835}
836
Pali Rohár828d3262020-08-19 15:57:07 +0200837static int pcie_advk_remove(struct udevice *dev)
838{
Pali Rohár828d3262020-08-19 15:57:07 +0200839 struct pcie_advk *pcie = dev_get_priv(dev);
Pali Rohár5f50b882020-09-22 13:21:38 +0200840 u32 reg;
Pali Rohárb3217222021-05-26 17:59:40 +0200841 int i;
842
843 for (i = 0; i < OB_WIN_COUNT; i++)
844 pcie_advk_disable_ob_win(pcie, i);
Pali Rohár828d3262020-08-19 15:57:07 +0200845
Pali Rohár7b85aef2021-05-26 17:59:35 +0200846 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
847 reg &= ~(PCIE_CORE_CMD_MEM_ACCESS_EN |
848 PCIE_CORE_CMD_IO_ACCESS_EN |
849 PCIE_CORE_CMD_MEM_IO_REQ_EN);
850 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
851
Pali Rohár5f50b882020-09-22 13:21:38 +0200852 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
853 reg &= ~LINK_TRAINING_EN;
854 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
855
Pali Rohár828d3262020-08-19 15:57:07 +0200856 return 0;
857}
858
Wilson Dinge51f2b12018-03-26 15:57:29 +0800859/**
Simon Glassd1998a92020-12-03 16:55:21 -0700860 * pcie_advk_of_to_plat() - Translate from DT to device state
Wilson Dinge51f2b12018-03-26 15:57:29 +0800861 *
862 * @dev: A pointer to the device being operated on
863 *
864 * Translate relevant data from the device tree pertaining to device @dev into
865 * state that the driver will later make use of. This state is stored in the
866 * device's private data structure.
867 *
868 * Return: 0 on success, else -EINVAL
869 */
Simon Glassd1998a92020-12-03 16:55:21 -0700870static int pcie_advk_of_to_plat(struct udevice *dev)
Wilson Dinge51f2b12018-03-26 15:57:29 +0800871{
872 struct pcie_advk *pcie = dev_get_priv(dev);
873
874 /* Get the register base address */
875 pcie->base = (void *)dev_read_addr_index(dev, 0);
876 if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE)
877 return -EINVAL;
878
879 return 0;
880}
881
882static const struct dm_pci_ops pcie_advk_ops = {
883 .read_config = pcie_advk_read_config,
884 .write_config = pcie_advk_write_config,
885};
886
887static const struct udevice_id pcie_advk_ids[] = {
Pali Rohára544d652021-05-26 17:59:36 +0200888 { .compatible = "marvell,armada-3700-pcie" },
Wilson Dinge51f2b12018-03-26 15:57:29 +0800889 { }
890};
891
892U_BOOT_DRIVER(pcie_advk) = {
893 .name = "pcie_advk",
894 .id = UCLASS_PCI,
895 .of_match = pcie_advk_ids,
896 .ops = &pcie_advk_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700897 .of_to_plat = pcie_advk_of_to_plat,
Wilson Dinge51f2b12018-03-26 15:57:29 +0800898 .probe = pcie_advk_probe,
Pali Rohár828d3262020-08-19 15:57:07 +0200899 .remove = pcie_advk_remove,
900 .flags = DM_FLAG_OS_PREPARE,
Simon Glass41575d82020-12-03 16:55:17 -0700901 .priv_auto = sizeof(struct pcie_advk),
Wilson Dinge51f2b12018-03-26 15:57:29 +0800902};