blob: b4e1b602405f34a26307a02c737ea9b4f9858b1a [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)
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ár9057a2c2021-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 Dinga6bdc862018-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
102/* LMI registers base address and register offsets */
103#define LMI_BASE_ADDR 0x6000
104#define CFG_REG (LMI_BASE_ADDR + 0x0)
105#define LTSSM_SHIFT 24
106#define LTSSM_MASK 0x3f
107#define LTSSM_L0 0x10
108
109/* PCIe core controller registers */
110#define CTRL_CORE_BASE_ADDR 0x18000
111#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
112#define CTRL_MODE_SHIFT 0x0
113#define CTRL_MODE_MASK 0x1
114#define PCIE_CORE_MODE_DIRECT 0x0
115#define PCIE_CORE_MODE_COMMAND 0x1
116
117/* Transaction types */
118#define PCIE_CONFIG_RD_TYPE0 0x8
119#define PCIE_CONFIG_RD_TYPE1 0x9
120#define PCIE_CONFIG_WR_TYPE0 0xa
121#define PCIE_CONFIG_WR_TYPE1 0xb
122
123/* PCI_BDF shifts 8bit, so we need extra 4bit shift */
124#define PCIE_BDF(dev) (dev << 4)
125#define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
126#define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
127#define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
128#define PCIE_CONF_REG(reg) ((reg) & 0xffc)
129#define PCIE_CONF_ADDR(bus, devfn, where) \
130 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
131 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
132
133/* PCIe Retries & Timeout definitions */
134#define MAX_RETRIES 10
135#define PIO_WAIT_TIMEOUT 100
136#define LINK_WAIT_TIMEOUT 100000
137
138#define CFG_RD_UR_VAL 0xFFFFFFFF
139#define CFG_RD_CRS_VAL 0xFFFF0001
140
Wilson Dinga6bdc862018-03-26 15:57:29 +0800141/**
142 * struct pcie_advk - Advk PCIe controller state
143 *
144 * @reg_base: The base address of the register space.
145 * @first_busno: This driver supports multiple PCIe controllers.
146 * first_busno stores the bus number of the PCIe root-port
147 * number which may vary depending on the PCIe setup
148 * (PEX switches etc).
149 * @device: The pointer to PCI uclass device.
150 */
151struct pcie_advk {
152 void *base;
153 int first_busno;
154 struct udevice *dev;
Pali Rohár6a58b972020-08-19 15:57:07 +0200155 struct gpio_desc reset_gpio;
Wilson Dinga6bdc862018-03-26 15:57:29 +0800156};
157
158static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg)
159{
160 writel(val, pcie->base + reg);
161}
162
163static inline uint advk_readl(struct pcie_advk *pcie, uint reg)
164{
165 return readl(pcie->base + reg);
166}
167
168/**
169 * pcie_advk_addr_valid() - Check for valid bus address
170 *
171 * @bdf: The PCI device to access
172 * @first_busno: Bus number of the PCIe controller root complex
173 *
174 * Return: 1 on valid, 0 on invalid
175 */
176static int pcie_advk_addr_valid(pci_dev_t bdf, int first_busno)
177{
178 /*
179 * In PCIE-E only a single device (0) can exist
180 * on the local bus. Beyound the local bus, there might be
181 * a Switch and everything is possible.
182 */
183 if ((PCI_BUS(bdf) == first_busno) && (PCI_DEV(bdf) > 0))
184 return 0;
185
186 return 1;
187}
188
189/**
190 * pcie_advk_wait_pio() - Wait for PIO access to be accomplished
191 *
192 * @pcie: The PCI device to access
193 *
194 * Wait up to 1 micro second for PIO access to be accomplished.
195 *
196 * Return 1 (true) if PIO access is accomplished.
197 * Return 0 (false) if PIO access is timed out.
198 */
199static int pcie_advk_wait_pio(struct pcie_advk *pcie)
200{
201 uint start, isr;
202 uint count;
203
204 for (count = 0; count < MAX_RETRIES; count++) {
205 start = advk_readl(pcie, PIO_START);
206 isr = advk_readl(pcie, PIO_ISR);
207 if (!start && isr)
208 return 1;
209 /*
210 * Do not check the PIO state too frequently,
211 * 100us delay is appropriate.
212 */
213 udelay(PIO_WAIT_TIMEOUT);
214 }
215
216 dev_err(pcie->dev, "config read/write timed out\n");
217 return 0;
218}
219
220/**
221 * pcie_advk_check_pio_status() - Validate PIO status and get the read result
222 *
223 * @pcie: Pointer to the PCI bus
224 * @read: Read from or write to configuration space - true(read) false(write)
225 * @read_val: Pointer to the read result, only valid when read is true
226 *
227 */
228static int pcie_advk_check_pio_status(struct pcie_advk *pcie,
229 bool read,
230 uint *read_val)
231{
232 uint reg;
233 unsigned int status;
234 char *strcomp_status, *str_posted;
235
236 reg = advk_readl(pcie, PIO_STAT);
237 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
238 PIO_COMPLETION_STATUS_SHIFT;
239
240 switch (status) {
241 case PIO_COMPLETION_STATUS_OK:
242 if (reg & PIO_ERR_STATUS) {
243 strcomp_status = "COMP_ERR";
244 break;
245 }
246 /* Get the read result */
247 if (read)
248 *read_val = advk_readl(pcie, PIO_RD_DATA);
249 /* No error */
250 strcomp_status = NULL;
251 break;
252 case PIO_COMPLETION_STATUS_UR:
253 if (read) {
254 /* For reading, UR is not an error status. */
255 *read_val = CFG_RD_UR_VAL;
256 strcomp_status = NULL;
257 } else {
258 strcomp_status = "UR";
259 }
260 break;
261 case PIO_COMPLETION_STATUS_CRS:
262 if (read) {
263 /* For reading, CRS is not an error status. */
264 *read_val = CFG_RD_CRS_VAL;
265 strcomp_status = NULL;
266 } else {
267 strcomp_status = "CRS";
268 }
269 break;
270 case PIO_COMPLETION_STATUS_CA:
271 strcomp_status = "CA";
272 break;
273 default:
274 strcomp_status = "Unknown";
275 break;
276 }
277
278 if (!strcomp_status)
279 return 0;
280
281 if (reg & PIO_NON_POSTED_REQ)
282 str_posted = "Non-posted";
283 else
284 str_posted = "Posted";
285
286 dev_err(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
287 str_posted, strcomp_status, reg,
288 advk_readl(pcie, PIO_ADDR_LS));
289
290 return -EFAULT;
291}
292
293/**
294 * pcie_advk_read_config() - Read from configuration space
295 *
296 * @bus: Pointer to the PCI bus
297 * @bdf: Identifies the PCIe device to access
298 * @offset: The offset into the device's configuration space
299 * @valuep: A pointer at which to store the read value
300 * @size: Indicates the size of access to perform
301 *
302 * Read a value of size @size from offset @offset within the configuration
303 * space of the device identified by the bus, device & function numbers in @bdf
304 * on the PCI bus @bus.
305 *
306 * Return: 0 on success
307 */
Simon Glass2a311e82020-01-27 08:49:37 -0700308static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800309 uint offset, ulong *valuep,
310 enum pci_size_t size)
311{
312 struct pcie_advk *pcie = dev_get_priv(bus);
313 uint reg;
314 int ret;
315
316 dev_dbg(pcie->dev, "PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
317 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
318
319 if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
320 dev_dbg(pcie->dev, "- out of range\n");
321 *valuep = pci_get_ff(size);
322 return 0;
323 }
324
325 /* Start PIO */
326 advk_writel(pcie, 0, PIO_START);
327 advk_writel(pcie, 1, PIO_ISR);
328
329 /* Program the control register */
330 reg = advk_readl(pcie, PIO_CTRL);
331 reg &= ~PIO_CTRL_TYPE_MASK;
332 if (PCI_BUS(bdf) == pcie->first_busno)
333 reg |= PCIE_CONFIG_RD_TYPE0;
334 else
335 reg |= PCIE_CONFIG_RD_TYPE1;
336 advk_writel(pcie, reg, PIO_CTRL);
337
338 /* Program the address registers */
339 reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
340 advk_writel(pcie, reg, PIO_ADDR_LS);
341 advk_writel(pcie, 0, PIO_ADDR_MS);
342
343 /* Start the transfer */
344 advk_writel(pcie, 1, PIO_START);
345
346 if (!pcie_advk_wait_pio(pcie))
347 return -EINVAL;
348
349 /* Check PIO status and get the read result */
350 ret = pcie_advk_check_pio_status(pcie, true, &reg);
351 if (ret)
352 return ret;
353
354 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
355 offset, size, reg);
356 *valuep = pci_conv_32_to_size(reg, offset, size);
357
358 return 0;
359}
360
361/**
362 * pcie_calc_datastrobe() - Calculate data strobe
363 *
364 * @offset: The offset into the device's configuration space
365 * @size: Indicates the size of access to perform
366 *
367 * Calculate data strobe according to offset and size
368 *
369 */
370static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size)
371{
372 uint bytes, data_strobe;
373
374 switch (size) {
375 case PCI_SIZE_8:
376 bytes = 1;
377 break;
378 case PCI_SIZE_16:
379 bytes = 2;
380 break;
381 default:
382 bytes = 4;
383 }
384
385 data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3);
386
387 return data_strobe;
388}
389
390/**
391 * pcie_advk_write_config() - Write to configuration space
392 *
393 * @bus: Pointer to the PCI bus
394 * @bdf: Identifies the PCIe device to access
395 * @offset: The offset into the device's configuration space
396 * @value: The value to write
397 * @size: Indicates the size of access to perform
398 *
399 * Write the value @value of size @size from offset @offset within the
400 * configuration space of the device identified by the bus, device & function
401 * numbers in @bdf on the PCI bus @bus.
402 *
403 * Return: 0 on success
404 */
405static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf,
406 uint offset, ulong value,
407 enum pci_size_t size)
408{
409 struct pcie_advk *pcie = dev_get_priv(bus);
410 uint reg;
411
412 dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
413 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
414 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
415 offset, size, value);
416
417 if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
418 dev_dbg(pcie->dev, "- out of range\n");
419 return 0;
420 }
421
422 /* Start PIO */
423 advk_writel(pcie, 0, PIO_START);
424 advk_writel(pcie, 1, PIO_ISR);
425
426 /* Program the control register */
427 reg = advk_readl(pcie, PIO_CTRL);
428 reg &= ~PIO_CTRL_TYPE_MASK;
429 if (PCI_BUS(bdf) == pcie->first_busno)
430 reg |= PCIE_CONFIG_WR_TYPE0;
431 else
432 reg |= PCIE_CONFIG_WR_TYPE1;
433 advk_writel(pcie, reg, PIO_CTRL);
434
435 /* Program the address registers */
436 reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
437 advk_writel(pcie, reg, PIO_ADDR_LS);
438 advk_writel(pcie, 0, PIO_ADDR_MS);
439 dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg);
440
441 /* Program the data register */
442 reg = pci_conv_size_to_32(0, value, offset, size);
443 advk_writel(pcie, reg, PIO_WR_DATA);
444 dev_dbg(pcie->dev, "\tPIO req. - val = 0x%08x\n", reg);
445
446 /* Program the data strobe */
447 reg = pcie_calc_datastrobe(offset, size);
448 advk_writel(pcie, reg, PIO_WR_DATA_STRB);
449 dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg);
450
451 /* Start the transfer */
452 advk_writel(pcie, 1, PIO_START);
453
454 if (!pcie_advk_wait_pio(pcie)) {
Wilson Dinga6bdc862018-03-26 15:57:29 +0800455 return -EINVAL;
456 }
457
458 /* Check PIO status */
459 pcie_advk_check_pio_status(pcie, false, &reg);
460
461 return 0;
462}
463
464/**
465 * pcie_advk_link_up() - Check if PCIe link is up or not
466 *
467 * @pcie: The PCI device to access
468 *
469 * Return 1 (true) on link up.
470 * Return 0 (false) on link down.
471 */
472static int pcie_advk_link_up(struct pcie_advk *pcie)
473{
474 u32 val, ltssm_state;
475
476 val = advk_readl(pcie, CFG_REG);
477 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
478 return ltssm_state >= LTSSM_L0;
479}
480
481/**
482 * pcie_advk_wait_for_link() - Wait for link training to be accomplished
483 *
484 * @pcie: The PCI device to access
485 *
486 * Wait up to 1 second for link training to be accomplished.
487 *
488 * Return 1 (true) if link training ends up with link up success.
489 * Return 0 (false) if link training ends up with link up failure.
490 */
491static int pcie_advk_wait_for_link(struct pcie_advk *pcie)
492{
493 int retries;
494
495 /* check if the link is up or not */
496 for (retries = 0; retries < MAX_RETRIES; retries++) {
497 if (pcie_advk_link_up(pcie)) {
498 printf("PCIE-%d: Link up\n", pcie->first_busno);
499 return 0;
500 }
501
502 udelay(LINK_WAIT_TIMEOUT);
503 }
504
505 printf("PCIE-%d: Link down\n", pcie->first_busno);
506
507 return -ETIMEDOUT;
508}
509
510/**
511 * pcie_advk_setup_hw() - PCIe initailzation
512 *
513 * @pcie: The PCI device to access
514 *
515 * Return: 0 on success
516 */
517static int pcie_advk_setup_hw(struct pcie_advk *pcie)
518{
519 u32 reg;
520
521 /* Set to Direct mode */
522 reg = advk_readl(pcie, CTRL_CONFIG_REG);
523 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
524 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
525 advk_writel(pcie, reg, CTRL_CONFIG_REG);
526
527 /* Set PCI global control register to RC mode */
528 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
529 reg |= (IS_RC_MSK << IS_RC_SHIFT);
530 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
531
532 /* Set Advanced Error Capabilities and Control PF0 register */
533 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
534 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
535 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK |
536 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV;
537 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
538
539 /* Set PCIe Device Control and Status 1 PF0 register */
540 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
Pali Rohár9057a2c2021-02-05 15:32:28 +0100541 (PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE <<
542 PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT) |
543 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE <<
544 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT) |
Wilson Dinga6bdc862018-03-26 15:57:29 +0800545 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE;
546 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
547
548 /* Program PCIe Control 2 to disable strict ordering */
549 reg = PCIE_CORE_CTRL2_RESERVED |
550 PCIE_CORE_CTRL2_TD_ENABLE;
551 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
552
553 /* Set GEN2 */
554 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
555 reg &= ~PCIE_GEN_SEL_MSK;
556 reg |= SPEED_GEN_2;
557 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
558
559 /* Set lane X1 */
560 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
561 reg &= ~LANE_CNT_MSK;
562 reg |= LANE_COUNT_1;
563 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
564
565 /* Enable link training */
566 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
567 reg |= LINK_TRAINING_EN;
568 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
569
570 /*
571 * Enable AXI address window location generation:
572 * When it is enabled, the default outbound window
573 * configurations (Default User Field: 0xD0074CFC)
574 * are used to transparent address translation for
575 * the outbound transactions. Thus, PCIe address
576 * windows are not required.
577 */
578 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
579 reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE;
580 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
581
582 /*
583 * Bypass the address window mapping for PIO:
584 * Since PIO access already contains all required
585 * info over AXI interface by PIO registers, the
586 * address window is not required.
587 */
588 reg = advk_readl(pcie, PIO_CTRL);
589 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
590 advk_writel(pcie, reg, PIO_CTRL);
591
592 /* Start link training */
593 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
594 reg |= PCIE_CORE_LINK_TRAINING;
595 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
596
597 /* Wait for PCIe link up */
598 if (pcie_advk_wait_for_link(pcie))
599 return -ENXIO;
600
601 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
602 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
603 PCIE_CORE_CMD_IO_ACCESS_EN |
604 PCIE_CORE_CMD_MEM_IO_REQ_EN;
605 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
606
607 return 0;
608}
609
610/**
611 * pcie_advk_probe() - Probe the PCIe bus for active link
612 *
613 * @dev: A pointer to the device being operated on
614 *
615 * Probe for an active link on the PCIe bus and configure the controller
616 * to enable this port.
617 *
618 * Return: 0 on success, else -ENODEV
619 */
620static int pcie_advk_probe(struct udevice *dev)
621{
622 struct pcie_advk *pcie = dev_get_priv(dev);
623
Pali Rohár6a58b972020-08-19 15:57:07 +0200624 gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800625 GPIOD_IS_OUT);
626 /*
627 * Issue reset to add-in card through the dedicated GPIO.
628 * Some boards are connecting the card reset pin to common system
629 * reset wire and others are using separate GPIO port.
630 * In the last case we have to release a reset of the addon card
631 * using this GPIO.
632 *
633 * FIX-ME:
634 * The PCIe RESET signal is not supposed to be released along
635 * with the SOC RESET signal. It should be lowered as early as
636 * possible before PCIe PHY initialization. Moreover, the PCIe
637 * clock should be gated as well.
638 */
Pali Rohár6a58b972020-08-19 15:57:07 +0200639 if (dm_gpio_is_valid(&pcie->reset_gpio)) {
Pali Roháre3271bb2021-01-18 12:09:33 +0100640 dev_dbg(dev, "Toggle PCIE Reset GPIO ...\n");
Pali Rohár6a58b972020-08-19 15:57:07 +0200641 dm_gpio_set_value(&pcie->reset_gpio, 1);
Pali Rohár0130a612020-08-19 15:57:06 +0200642 mdelay(200);
Pali Rohár6a58b972020-08-19 15:57:07 +0200643 dm_gpio_set_value(&pcie->reset_gpio, 0);
Pali Rohár5c6edca2020-08-25 10:45:04 +0200644 } else {
Pali Roháre3271bb2021-01-18 12:09:33 +0100645 dev_warn(dev, "PCIE Reset on GPIO support is missing\n");
Wilson Dinga6bdc862018-03-26 15:57:29 +0800646 }
Wilson Dinga6bdc862018-03-26 15:57:29 +0800647
Simon Glass75e534b2020-12-16 21:20:07 -0700648 pcie->first_busno = dev_seq(dev);
Wilson Dinga6bdc862018-03-26 15:57:29 +0800649 pcie->dev = pci_get_controller(dev);
650
651 return pcie_advk_setup_hw(pcie);
652}
653
Pali Rohár6a58b972020-08-19 15:57:07 +0200654static int pcie_advk_remove(struct udevice *dev)
655{
Pali Rohár6a58b972020-08-19 15:57:07 +0200656 struct pcie_advk *pcie = dev_get_priv(dev);
Pali Rohárff876992020-09-22 13:21:38 +0200657 u32 reg;
Pali Rohár6a58b972020-08-19 15:57:07 +0200658
Pali Rohárff876992020-09-22 13:21:38 +0200659 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
660 reg &= ~LINK_TRAINING_EN;
661 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
662
Pali Rohár6a58b972020-08-19 15:57:07 +0200663 return 0;
664}
665
Wilson Dinga6bdc862018-03-26 15:57:29 +0800666/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700667 * pcie_advk_of_to_plat() - Translate from DT to device state
Wilson Dinga6bdc862018-03-26 15:57:29 +0800668 *
669 * @dev: A pointer to the device being operated on
670 *
671 * Translate relevant data from the device tree pertaining to device @dev into
672 * state that the driver will later make use of. This state is stored in the
673 * device's private data structure.
674 *
675 * Return: 0 on success, else -EINVAL
676 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700677static int pcie_advk_of_to_plat(struct udevice *dev)
Wilson Dinga6bdc862018-03-26 15:57:29 +0800678{
679 struct pcie_advk *pcie = dev_get_priv(dev);
680
681 /* Get the register base address */
682 pcie->base = (void *)dev_read_addr_index(dev, 0);
683 if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE)
684 return -EINVAL;
685
686 return 0;
687}
688
689static const struct dm_pci_ops pcie_advk_ops = {
690 .read_config = pcie_advk_read_config,
691 .write_config = pcie_advk_write_config,
692};
693
694static const struct udevice_id pcie_advk_ids[] = {
695 { .compatible = "marvell,armada-37xx-pcie" },
696 { }
697};
698
699U_BOOT_DRIVER(pcie_advk) = {
700 .name = "pcie_advk",
701 .id = UCLASS_PCI,
702 .of_match = pcie_advk_ids,
703 .ops = &pcie_advk_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700704 .of_to_plat = pcie_advk_of_to_plat,
Wilson Dinga6bdc862018-03-26 15:57:29 +0800705 .probe = pcie_advk_probe,
Pali Rohár6a58b972020-08-19 15:57:07 +0200706 .remove = pcie_advk_remove,
707 .flags = DM_FLAG_OS_PREPARE,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700708 .priv_auto = sizeof(struct pcie_advk),
Wilson Dinga6bdc862018-03-26 15:57:29 +0800709};