74797e984cb8633315964555652ff2e79ba9b958
[platform/kernel/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 /* 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
142 /* 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
148 #define VENDOR_ID_REG                           (LMI_BASE_ADDR + 0x44)
149
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 */
175 #define PIO_MAX_RETRIES                         1500
176 #define PIO_WAIT_TIMEOUT                        1000
177 #define LINK_MAX_RETRIES                        10
178 #define LINK_WAIT_TIMEOUT                       100000
179
180 #define CFG_RD_CRS_VAL                  0xFFFF0001
181
182 /**
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  */
192 struct pcie_advk {
193         void           *base;
194         int            first_busno;
195         struct udevice *dev;
196         struct gpio_desc reset_gpio;
197 };
198
199 static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg)
200 {
201         writel(val, pcie->base + reg);
202 }
203
204 static 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  */
217 static 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  *
235  * Wait up to 1.5 seconds for PIO access to be accomplished.
236  *
237  * Return positive - retry count if PIO access is accomplished.
238  * Return negative - error if PIO access is timed out.
239  */
240 static int pcie_advk_wait_pio(struct pcie_advk *pcie)
241 {
242         uint start, isr;
243         uint count;
244
245         for (count = 1; count <= PIO_MAX_RETRIES; count++) {
246                 start = advk_readl(pcie, PIO_START);
247                 isr = advk_readl(pcie, PIO_ISR);
248                 if (!start && isr)
249                         return count;
250                 /*
251                  * Do not check the PIO state too frequently,
252                  * 100us delay is appropriate.
253                  */
254                 udelay(PIO_WAIT_TIMEOUT);
255         }
256
257         dev_err(pcie->dev, "PIO read/write transfer time out\n");
258         return -ETIMEDOUT;
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
265  * @allow_crs: Only for read requests, if CRS response is allowed
266  * @read_val: Pointer to the read result
267  *
268  * Return: 0 on success
269  */
270 static int pcie_advk_check_pio_status(struct pcie_advk *pcie,
271                                       bool allow_crs,
272                                       uint *read_val)
273 {
274         int ret;
275         uint reg;
276         unsigned int status;
277         char *strcomp_status, *str_posted;
278
279         reg = advk_readl(pcie, PIO_STAT);
280         status = (reg & PIO_COMPLETION_STATUS_MASK) >>
281                 PIO_COMPLETION_STATUS_SHIFT;
282
283         switch (status) {
284         case PIO_COMPLETION_STATUS_OK:
285                 if (reg & PIO_ERR_STATUS) {
286                         strcomp_status = "COMP_ERR";
287                         ret = -EFAULT;
288                         break;
289                 }
290                 /* Get the read result */
291                 if (read_val)
292                         *read_val = advk_readl(pcie, PIO_RD_DATA);
293                 /* No error */
294                 strcomp_status = NULL;
295                 ret = 0;
296                 break;
297         case PIO_COMPLETION_STATUS_UR:
298                 strcomp_status = "UR";
299                 ret = -EOPNOTSUPP;
300                 break;
301         case PIO_COMPLETION_STATUS_CRS:
302                 if (allow_crs && read_val) {
303                         /* For reading, CRS is not an error status. */
304                         *read_val = CFG_RD_CRS_VAL;
305                         strcomp_status = NULL;
306                         ret = 0;
307                 } else {
308                         strcomp_status = "CRS";
309                         ret = -EAGAIN;
310                 }
311                 break;
312         case PIO_COMPLETION_STATUS_CA:
313                 strcomp_status = "CA";
314                 ret = -ECANCELED;
315                 break;
316         default:
317                 strcomp_status = "Unknown";
318                 ret = -EINVAL;
319                 break;
320         }
321
322         if (!strcomp_status)
323                 return ret;
324
325         if (reg & PIO_NON_POSTED_REQ)
326                 str_posted = "Non-posted";
327         else
328                 str_posted = "Posted";
329
330         dev_err(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
331                 str_posted, strcomp_status, reg,
332                 advk_readl(pcie, PIO_ADDR_LS));
333
334         return ret;
335 }
336
337 /**
338  * pcie_advk_read_config() - Read from configuration space
339  *
340  * @bus: Pointer to the PCI bus
341  * @bdf: Identifies the PCIe device to access
342  * @offset: The offset into the device's configuration space
343  * @valuep: A pointer at which to store the read value
344  * @size: Indicates the size of access to perform
345  *
346  * Read a value of size @size from offset @offset within the configuration
347  * space of the device identified by the bus, device & function numbers in @bdf
348  * on the PCI bus @bus.
349  *
350  * Return: 0 on success
351  */
352 static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf,
353                                  uint offset, ulong *valuep,
354                                  enum pci_size_t size)
355 {
356         struct pcie_advk *pcie = dev_get_priv(bus);
357         int retry_count;
358         bool allow_crs;
359         uint reg;
360         int ret;
361
362         dev_dbg(pcie->dev, "PCIE CFG read:  (b,d,f)=(%2d,%2d,%2d) ",
363                 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
364
365         if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
366                 dev_dbg(pcie->dev, "- out of range\n");
367                 *valuep = pci_get_ff(size);
368                 return 0;
369         }
370
371         /*
372          * Returning fabricated CRS value (0xFFFF0001) by PCIe Root Complex to
373          * OS is allowed only for 4-byte PCI_VENDOR_ID config read request and
374          * only when CRSSVE bit in Root Port PCIe device is enabled. In all
375          * other error PCIe Root Complex must return all-ones.
376          * Aardvark HW does not have Root Port PCIe device and U-Boot does not
377          * implement emulation of this device.
378          * U-Boot currently does not support handling of CRS return value for
379          * PCI_VENDOR_ID config read request and also does not set CRSSVE bit.
380          * Therefore disable returning CRS response for now.
381          */
382         allow_crs = false;
383
384         if (advk_readl(pcie, PIO_START)) {
385                 dev_err(pcie->dev,
386                         "Previous PIO read/write transfer is still running\n");
387                 if (allow_crs) {
388                         *valuep = CFG_RD_CRS_VAL;
389                         return 0;
390                 }
391                 *valuep = pci_get_ff(size);
392                 return -EAGAIN;
393         }
394
395         /* Program the control register */
396         reg = advk_readl(pcie, PIO_CTRL);
397         reg &= ~PIO_CTRL_TYPE_MASK;
398         if (PCI_BUS(bdf) == pcie->first_busno)
399                 reg |= PCIE_CONFIG_RD_TYPE0;
400         else
401                 reg |= PCIE_CONFIG_RD_TYPE1;
402         advk_writel(pcie, reg, PIO_CTRL);
403
404         /* Program the address registers */
405         reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
406         advk_writel(pcie, reg, PIO_ADDR_LS);
407         advk_writel(pcie, 0, PIO_ADDR_MS);
408
409         retry_count = 0;
410
411 retry:
412         /* Start the transfer */
413         advk_writel(pcie, 1, PIO_ISR);
414         advk_writel(pcie, 1, PIO_START);
415
416         ret = pcie_advk_wait_pio(pcie);
417         if (ret < 0) {
418                 if (allow_crs) {
419                         *valuep = CFG_RD_CRS_VAL;
420                         return 0;
421                 }
422                 *valuep = pci_get_ff(size);
423                 return ret;
424         }
425
426         retry_count += ret;
427
428         /* Check PIO status and get the read result */
429         ret = pcie_advk_check_pio_status(pcie, allow_crs, &reg);
430         if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
431                 goto retry;
432         if (ret) {
433                 *valuep = pci_get_ff(size);
434                 return ret;
435         }
436
437         dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
438                 offset, size, reg);
439         *valuep = pci_conv_32_to_size(reg, offset, size);
440
441         return 0;
442 }
443
444 /**
445  * pcie_calc_datastrobe() - Calculate data strobe
446  *
447  * @offset: The offset into the device's configuration space
448  * @size: Indicates the size of access to perform
449  *
450  * Calculate data strobe according to offset and size
451  *
452  */
453 static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size)
454 {
455         uint bytes, data_strobe;
456
457         switch (size) {
458         case PCI_SIZE_8:
459                 bytes = 1;
460                 break;
461         case PCI_SIZE_16:
462                 bytes = 2;
463                 break;
464         default:
465                 bytes = 4;
466         }
467
468         data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3);
469
470         return data_strobe;
471 }
472
473 /**
474  * pcie_advk_write_config() - Write to configuration space
475  *
476  * @bus: Pointer to the PCI bus
477  * @bdf: Identifies the PCIe device to access
478  * @offset: The offset into the device's configuration space
479  * @value: The value to write
480  * @size: Indicates the size of access to perform
481  *
482  * Write the value @value of size @size from offset @offset within the
483  * configuration space of the device identified by the bus, device & function
484  * numbers in @bdf on the PCI bus @bus.
485  *
486  * Return: 0 on success
487  */
488 static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf,
489                                   uint offset, ulong value,
490                                   enum pci_size_t size)
491 {
492         struct pcie_advk *pcie = dev_get_priv(bus);
493         int retry_count;
494         uint reg;
495         int ret;
496
497         dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
498                 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
499         dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
500                 offset, size, value);
501
502         if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
503                 dev_dbg(pcie->dev, "- out of range\n");
504                 return 0;
505         }
506
507         if (advk_readl(pcie, PIO_START)) {
508                 dev_err(pcie->dev,
509                         "Previous PIO read/write transfer is still running\n");
510                 return -EAGAIN;
511         }
512
513         /* Program the control register */
514         reg = advk_readl(pcie, PIO_CTRL);
515         reg &= ~PIO_CTRL_TYPE_MASK;
516         if (PCI_BUS(bdf) == pcie->first_busno)
517                 reg |= PCIE_CONFIG_WR_TYPE0;
518         else
519                 reg |= PCIE_CONFIG_WR_TYPE1;
520         advk_writel(pcie, reg, PIO_CTRL);
521
522         /* Program the address registers */
523         reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
524         advk_writel(pcie, reg, PIO_ADDR_LS);
525         advk_writel(pcie, 0, PIO_ADDR_MS);
526         dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg);
527
528         /* Program the data register */
529         reg = pci_conv_size_to_32(0, value, offset, size);
530         advk_writel(pcie, reg, PIO_WR_DATA);
531         dev_dbg(pcie->dev, "\tPIO req. - val  = 0x%08x\n", reg);
532
533         /* Program the data strobe */
534         reg = pcie_calc_datastrobe(offset, size);
535         advk_writel(pcie, reg, PIO_WR_DATA_STRB);
536         dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg);
537
538         retry_count = 0;
539
540 retry:
541         /* Start the transfer */
542         advk_writel(pcie, 1, PIO_ISR);
543         advk_writel(pcie, 1, PIO_START);
544
545         ret = pcie_advk_wait_pio(pcie);
546         if (ret < 0)
547                 return ret;
548
549         retry_count += ret;
550
551         /* Check PIO status */
552         ret = pcie_advk_check_pio_status(pcie, false, NULL);
553         if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
554                 goto retry;
555         return ret;
556 }
557
558 /**
559  * pcie_advk_link_up() - Check if PCIe link is up or not
560  *
561  * @pcie: The PCI device to access
562  *
563  * Return 1 (true) on link up.
564  * Return 0 (false) on link down.
565  */
566 static int pcie_advk_link_up(struct pcie_advk *pcie)
567 {
568         u32 val, ltssm_state;
569
570         val = advk_readl(pcie, CFG_REG);
571         ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
572         return ltssm_state >= LTSSM_L0;
573 }
574
575 /**
576  * pcie_advk_wait_for_link() - Wait for link training to be accomplished
577  *
578  * @pcie: The PCI device to access
579  *
580  * Wait up to 1 second for link training to be accomplished.
581  *
582  * Return 1 (true) if link training ends up with link up success.
583  * Return 0 (false) if link training ends up with link up failure.
584  */
585 static int pcie_advk_wait_for_link(struct pcie_advk *pcie)
586 {
587         int retries;
588
589         /* check if the link is up or not */
590         for (retries = 0; retries < LINK_MAX_RETRIES; retries++) {
591                 if (pcie_advk_link_up(pcie)) {
592                         printf("PCIE-%d: Link up\n", pcie->first_busno);
593                         return 0;
594                 }
595
596                 udelay(LINK_WAIT_TIMEOUT);
597         }
598
599         printf("PCIE-%d: Link down\n", pcie->first_busno);
600
601         return -ETIMEDOUT;
602 }
603
604 /*
605  * Set PCIe address window register which could be used for memory
606  * mapping.
607  */
608 static void pcie_advk_set_ob_win(struct pcie_advk *pcie, u8 win_num,
609                                  phys_addr_t match, phys_addr_t remap,
610                                  phys_addr_t mask, u32 actions)
611 {
612         advk_writel(pcie, OB_WIN_ENABLE |
613                           lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
614         advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
615         advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
616         advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
617         advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
618         advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
619         advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
620 }
621
622 static void pcie_advk_disable_ob_win(struct pcie_advk *pcie, u8 win_num)
623 {
624         advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
625         advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
626         advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
627         advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
628         advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
629         advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
630         advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
631 }
632
633 static void pcie_advk_set_ob_region(struct pcie_advk *pcie, int *wins,
634                                     struct pci_region *region, u32 actions)
635 {
636         phys_addr_t phys_start = region->phys_start;
637         pci_addr_t bus_start = region->bus_start;
638         pci_size_t size = region->size;
639         phys_addr_t win_mask;
640         u64 win_size;
641
642         if (*wins == -1)
643                 return;
644
645         /*
646          * The n-th PCIe window is configured by tuple (match, remap, mask)
647          * and an access to address A uses this window if A matches the
648          * match with given mask.
649          * So every PCIe window size must be a power of two and every start
650          * address must be aligned to window size. Minimal size is 64 KiB
651          * because lower 16 bits of mask must be zero. Remapped address
652          * may have set only bits from the mask.
653          */
654         while (*wins < OB_WIN_COUNT && size > 0) {
655                 /* Calculate the largest aligned window size */
656                 win_size = (1ULL << (fls64(size) - 1)) |
657                            (phys_start ? (1ULL << __ffs64(phys_start)) : 0);
658                 win_size = 1ULL << __ffs64(win_size);
659                 win_mask = ~(win_size - 1);
660                 if (win_size < 0x10000 || (bus_start & ~win_mask))
661                         break;
662
663                 dev_dbg(pcie->dev,
664                         "Configuring PCIe window %d: [0x%llx-0x%llx] as 0x%x\n",
665                         *wins, (u64)phys_start, (u64)phys_start + win_size,
666                         actions);
667                 pcie_advk_set_ob_win(pcie, *wins, phys_start, bus_start,
668                                      win_mask, actions);
669
670                 phys_start += win_size;
671                 bus_start += win_size;
672                 size -= win_size;
673                 (*wins)++;
674         }
675
676         if (size > 0) {
677                 *wins = -1;
678                 dev_err(pcie->dev,
679                         "Invalid PCIe region [0x%llx-0x%llx]\n",
680                         (u64)region->phys_start,
681                         (u64)region->phys_start + region->size);
682         }
683 }
684
685 /**
686  * pcie_advk_setup_hw() - PCIe initailzation
687  *
688  * @pcie: The PCI device to access
689  *
690  * Return: 0 on success
691  */
692 static int pcie_advk_setup_hw(struct pcie_advk *pcie)
693 {
694         struct pci_region *io, *mem, *pref;
695         int i, wins;
696         u32 reg;
697
698         /* Set to Direct mode */
699         reg = advk_readl(pcie, CTRL_CONFIG_REG);
700         reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
701         reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
702         advk_writel(pcie, reg, CTRL_CONFIG_REG);
703
704         /* Set PCI global control register to RC mode */
705         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
706         reg |= (IS_RC_MSK << IS_RC_SHIFT);
707         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
708
709         /*
710          * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
711          * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
712          * id in high 16 bits. Updating this register changes readback value of
713          * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
714          * for erratum 4.1: "The value of device and vendor ID is incorrect".
715          */
716         advk_writel(pcie, 0x11ab11ab, VENDOR_ID_REG);
717
718         /* Set Advanced Error Capabilities and Control PF0 register */
719         reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
720                 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
721                 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK |
722                 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV;
723         advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
724
725         /* Set PCIe Device Control and Status 1 PF0 register */
726         reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
727                 (PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE <<
728                  PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT) |
729                 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE <<
730                  PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT) |
731                 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE;
732         advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
733
734         /* Program PCIe Control 2 to disable strict ordering */
735         reg = PCIE_CORE_CTRL2_RESERVED |
736                 PCIE_CORE_CTRL2_TD_ENABLE;
737         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
738
739         /* Set GEN2 */
740         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
741         reg &= ~PCIE_GEN_SEL_MSK;
742         reg |= SPEED_GEN_2;
743         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
744
745         /* Set lane X1 */
746         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
747         reg &= ~LANE_CNT_MSK;
748         reg |= LANE_COUNT_1;
749         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
750
751         /* Enable link training */
752         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
753         reg |= LINK_TRAINING_EN;
754         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
755
756         /*
757          * Enable AXI address window location generation:
758          * When it is enabled, the default outbound window
759          * configurations (Default User Field: 0xD0074CFC)
760          * are used to transparent address translation for
761          * the outbound transactions. Thus, PCIe address
762          * windows are not required for transparent memory
763          * access when default outbound window configuration
764          * is set for memory access.
765          */
766         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
767         reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE;
768         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
769
770         /*
771          * Bypass the address window mapping for PIO:
772          * Since PIO access already contains all required
773          * info over AXI interface by PIO registers, the
774          * address window is not required.
775          */
776         reg = advk_readl(pcie, PIO_CTRL);
777         reg |= PIO_CTRL_ADDR_WIN_DISABLE;
778         advk_writel(pcie, reg, PIO_CTRL);
779
780         /*
781          * Set memory access in Default User Field so it
782          * is not required to configure PCIe address for
783          * transparent memory access.
784          */
785         advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
786
787         /*
788          * Configure PCIe address windows for non-memory or
789          * non-transparent access as by default PCIe uses
790          * transparent memory access.
791          */
792         wins = 0;
793         pci_get_regions(pcie->dev, &io, &mem, &pref);
794         if (io)
795                 pcie_advk_set_ob_region(pcie, &wins, io, OB_WIN_TYPE_IO);
796         if (mem && mem->phys_start != mem->bus_start)
797                 pcie_advk_set_ob_region(pcie, &wins, mem, OB_WIN_TYPE_MEM);
798         if (pref && pref->phys_start != pref->bus_start)
799                 pcie_advk_set_ob_region(pcie, &wins, pref, OB_WIN_TYPE_MEM);
800
801         /* Disable remaining PCIe outbound windows */
802         for (i = ((wins >= 0) ? wins : 0); i < OB_WIN_COUNT; i++)
803                 pcie_advk_disable_ob_win(pcie, i);
804
805         if (wins == -1)
806                 return -EINVAL;
807
808         /* Wait for PCIe link up */
809         if (pcie_advk_wait_for_link(pcie))
810                 return -ENXIO;
811
812         reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
813         reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
814                 PCIE_CORE_CMD_IO_ACCESS_EN |
815                 PCIE_CORE_CMD_MEM_IO_REQ_EN;
816         advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
817
818         return 0;
819 }
820
821 /**
822  * pcie_advk_probe() - Probe the PCIe bus for active link
823  *
824  * @dev: A pointer to the device being operated on
825  *
826  * Probe for an active link on the PCIe bus and configure the controller
827  * to enable this port.
828  *
829  * Return: 0 on success, else -ENODEV
830  */
831 static int pcie_advk_probe(struct udevice *dev)
832 {
833         struct pcie_advk *pcie = dev_get_priv(dev);
834
835         gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio,
836                              GPIOD_IS_OUT);
837         /*
838          * Issue reset to add-in card through the dedicated GPIO.
839          * Some boards are connecting the card reset pin to common system
840          * reset wire and others are using separate GPIO port.
841          * In the last case we have to release a reset of the addon card
842          * using this GPIO.
843          *
844          * FIX-ME:
845          *     The PCIe RESET signal is not supposed to be released along
846          *     with the SOC RESET signal. It should be lowered as early as
847          *     possible before PCIe PHY initialization. Moreover, the PCIe
848          *     clock should be gated as well.
849          */
850         if (dm_gpio_is_valid(&pcie->reset_gpio)) {
851                 dev_dbg(dev, "Toggle PCIE Reset GPIO ...\n");
852                 dm_gpio_set_value(&pcie->reset_gpio, 1);
853                 mdelay(200);
854                 dm_gpio_set_value(&pcie->reset_gpio, 0);
855         } else {
856                 dev_warn(dev, "PCIE Reset on GPIO support is missing\n");
857         }
858
859         pcie->first_busno = dev_seq(dev);
860         pcie->dev = pci_get_controller(dev);
861
862         return pcie_advk_setup_hw(pcie);
863 }
864
865 static int pcie_advk_remove(struct udevice *dev)
866 {
867         struct pcie_advk *pcie = dev_get_priv(dev);
868         u32 reg;
869         int i;
870
871         for (i = 0; i < OB_WIN_COUNT; i++)
872                 pcie_advk_disable_ob_win(pcie, i);
873
874         reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
875         reg &= ~(PCIE_CORE_CMD_MEM_ACCESS_EN |
876                  PCIE_CORE_CMD_IO_ACCESS_EN |
877                  PCIE_CORE_CMD_MEM_IO_REQ_EN);
878         advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
879
880         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
881         reg &= ~LINK_TRAINING_EN;
882         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
883
884         return 0;
885 }
886
887 /**
888  * pcie_advk_of_to_plat() - Translate from DT to device state
889  *
890  * @dev: A pointer to the device being operated on
891  *
892  * Translate relevant data from the device tree pertaining to device @dev into
893  * state that the driver will later make use of. This state is stored in the
894  * device's private data structure.
895  *
896  * Return: 0 on success, else -EINVAL
897  */
898 static int pcie_advk_of_to_plat(struct udevice *dev)
899 {
900         struct pcie_advk *pcie = dev_get_priv(dev);
901
902         /* Get the register base address */
903         pcie->base = (void *)dev_read_addr_index(dev, 0);
904         if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE)
905                 return -EINVAL;
906
907         return 0;
908 }
909
910 static const struct dm_pci_ops pcie_advk_ops = {
911         .read_config    = pcie_advk_read_config,
912         .write_config   = pcie_advk_write_config,
913 };
914
915 static const struct udevice_id pcie_advk_ids[] = {
916         { .compatible = "marvell,armada-3700-pcie" },
917         { }
918 };
919
920 U_BOOT_DRIVER(pcie_advk) = {
921         .name                   = "pcie_advk",
922         .id                     = UCLASS_PCI,
923         .of_match               = pcie_advk_ids,
924         .ops                    = &pcie_advk_ops,
925         .of_to_plat     = pcie_advk_of_to_plat,
926         .probe                  = pcie_advk_probe,
927         .remove                 = pcie_advk_remove,
928         .flags                  = DM_FLAG_OS_PREPARE,
929         .priv_auto      = sizeof(struct pcie_advk),
930 };