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