]> Git Repo - J-u-boot.git/blob - drivers/pci/pci-aardvark.c
6d73aab03f92be90e561ec46d7101468d98f7b48
[J-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 <[email protected]>
23  *         Hezi Shahmoon <[email protected]>
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_REV_REG                                   0x8
43 #define PCIE_CORE_EXP_ROM_BAR_REG                               0x30
44 #define PCIE_CORE_PCIEXP_CAP_OFF                                0xc0
45 #define PCIE_CORE_DEV_CTRL_STATS_REG                            0xc8
46 #define     PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE        (0 << 4)
47 #define     PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE              (0 << 11)
48 #define     PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE           0x2
49 #define     PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT     5
50 #define     PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE            0x2
51 #define     PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT      12
52 #define PCIE_CORE_LINK_CTRL_STAT_REG                            0xd0
53 #define     PCIE_CORE_LINK_TRAINING                             BIT(5)
54 #define PCIE_CORE_ERR_CAPCTL_REG                                0x118
55 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX                    BIT(5)
56 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN                 BIT(6)
57 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHECK                     BIT(7)
58 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV                 BIT(8)
59
60 /* PIO registers base address and register offsets */
61 #define PIO_BASE_ADDR                           0x4000
62 #define PIO_CTRL                                (PIO_BASE_ADDR + 0x0)
63 #define   PIO_CTRL_TYPE_MASK                    GENMASK(3, 0)
64 #define   PIO_CTRL_ADDR_WIN_DISABLE             BIT(24)
65 #define PIO_STAT                                (PIO_BASE_ADDR + 0x4)
66 #define   PIO_COMPLETION_STATUS_SHIFT           7
67 #define   PIO_COMPLETION_STATUS_MASK            GENMASK(9, 7)
68 #define   PIO_COMPLETION_STATUS_OK              0
69 #define   PIO_COMPLETION_STATUS_UR              1
70 #define   PIO_COMPLETION_STATUS_CRS             2
71 #define   PIO_COMPLETION_STATUS_CA              4
72 #define   PIO_NON_POSTED_REQ                    BIT(10)
73 #define   PIO_ERR_STATUS                        BIT(11)
74 #define PIO_ADDR_LS                             (PIO_BASE_ADDR + 0x8)
75 #define PIO_ADDR_MS                             (PIO_BASE_ADDR + 0xc)
76 #define PIO_WR_DATA                             (PIO_BASE_ADDR + 0x10)
77 #define PIO_WR_DATA_STRB                        (PIO_BASE_ADDR + 0x14)
78 #define PIO_RD_DATA                             (PIO_BASE_ADDR + 0x18)
79 #define PIO_START                               (PIO_BASE_ADDR + 0x1c)
80 #define PIO_ISR                                 (PIO_BASE_ADDR + 0x20)
81
82 /* Aardvark Control registers */
83 #define CONTROL_BASE_ADDR                       0x4800
84 #define PCIE_CORE_CTRL0_REG                     (CONTROL_BASE_ADDR + 0x0)
85 #define     PCIE_GEN_SEL_MSK                    0x3
86 #define     PCIE_GEN_SEL_SHIFT                  0x0
87 #define     SPEED_GEN_1                         0
88 #define     SPEED_GEN_2                         1
89 #define     SPEED_GEN_3                         2
90 #define     IS_RC_MSK                           1
91 #define     IS_RC_SHIFT                         2
92 #define     LANE_CNT_MSK                        0x18
93 #define     LANE_CNT_SHIFT                      0x3
94 #define     LANE_COUNT_1                        (0 << LANE_CNT_SHIFT)
95 #define     LANE_COUNT_2                        (1 << LANE_CNT_SHIFT)
96 #define     LANE_COUNT_4                        (2 << LANE_CNT_SHIFT)
97 #define     LANE_COUNT_8                        (3 << LANE_CNT_SHIFT)
98 #define     LINK_TRAINING_EN                    BIT(6)
99 #define PCIE_CORE_CTRL2_REG                     (CONTROL_BASE_ADDR + 0x8)
100 #define     PCIE_CORE_CTRL2_RESERVED            0x7
101 #define     PCIE_CORE_CTRL2_TD_ENABLE           BIT(4)
102 #define     PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
103 #define     PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE  BIT(6)
104
105 /* PCIe window configuration */
106 #define OB_WIN_BASE_ADDR                        0x4c00
107 #define OB_WIN_BLOCK_SIZE                       0x20
108 #define OB_WIN_COUNT                            8
109 #define OB_WIN_REG_ADDR(win, offset)            (OB_WIN_BASE_ADDR + \
110                                                  OB_WIN_BLOCK_SIZE * (win) + \
111                                                  (offset))
112 #define OB_WIN_MATCH_LS(win)                    OB_WIN_REG_ADDR(win, 0x00)
113 #define     OB_WIN_ENABLE                       BIT(0)
114 #define OB_WIN_MATCH_MS(win)                    OB_WIN_REG_ADDR(win, 0x04)
115 #define OB_WIN_REMAP_LS(win)                    OB_WIN_REG_ADDR(win, 0x08)
116 #define OB_WIN_REMAP_MS(win)                    OB_WIN_REG_ADDR(win, 0x0c)
117 #define OB_WIN_MASK_LS(win)                     OB_WIN_REG_ADDR(win, 0x10)
118 #define OB_WIN_MASK_MS(win)                     OB_WIN_REG_ADDR(win, 0x14)
119 #define OB_WIN_ACTIONS(win)                     OB_WIN_REG_ADDR(win, 0x18)
120 #define OB_WIN_DEFAULT_ACTIONS                  (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
121 #define     OB_WIN_FUNC_NUM_MASK                GENMASK(31, 24)
122 #define     OB_WIN_FUNC_NUM_SHIFT               24
123 #define     OB_WIN_FUNC_NUM_ENABLE              BIT(23)
124 #define     OB_WIN_BUS_NUM_BITS_MASK            GENMASK(22, 20)
125 #define     OB_WIN_BUS_NUM_BITS_SHIFT           20
126 #define     OB_WIN_MSG_CODE_ENABLE              BIT(22)
127 #define     OB_WIN_MSG_CODE_MASK                GENMASK(21, 14)
128 #define     OB_WIN_MSG_CODE_SHIFT               14
129 #define     OB_WIN_MSG_PAYLOAD_LEN              BIT(12)
130 #define     OB_WIN_ATTR_ENABLE                  BIT(11)
131 #define     OB_WIN_ATTR_TC_MASK                 GENMASK(10, 8)
132 #define     OB_WIN_ATTR_TC_SHIFT                8
133 #define     OB_WIN_ATTR_RELAXED                 BIT(7)
134 #define     OB_WIN_ATTR_NOSNOOP                 BIT(6)
135 #define     OB_WIN_ATTR_POISON                  BIT(5)
136 #define     OB_WIN_ATTR_IDO                     BIT(4)
137 #define     OB_WIN_TYPE_MASK                    GENMASK(3, 0)
138 #define     OB_WIN_TYPE_SHIFT                   0
139 #define     OB_WIN_TYPE_MEM                     0x0
140 #define     OB_WIN_TYPE_IO                      0x4
141 #define     OB_WIN_TYPE_CONFIG_TYPE0            0x8
142 #define     OB_WIN_TYPE_CONFIG_TYPE1            0x9
143 #define     OB_WIN_TYPE_MSG                     0xc
144
145 /* LMI registers base address and register offsets */
146 #define LMI_BASE_ADDR                           0x6000
147 #define CFG_REG                                 (LMI_BASE_ADDR + 0x0)
148 #define     LTSSM_SHIFT                         24
149 #define     LTSSM_MASK                          0x3f
150 #define     LTSSM_L0                            0x10
151 #define     LTSSM_DISABLED                      0x20
152 #define VENDOR_ID_REG                           (LMI_BASE_ADDR + 0x44)
153
154 /* PCIe core controller registers */
155 #define CTRL_CORE_BASE_ADDR                     0x18000
156 #define CTRL_CONFIG_REG                         (CTRL_CORE_BASE_ADDR + 0x0)
157 #define     CTRL_MODE_SHIFT                     0x0
158 #define     CTRL_MODE_MASK                      0x1
159 #define     PCIE_CORE_MODE_DIRECT               0x0
160 #define     PCIE_CORE_MODE_COMMAND              0x1
161
162 /* Transaction types */
163 #define PCIE_CONFIG_RD_TYPE0                    0x8
164 #define PCIE_CONFIG_RD_TYPE1                    0x9
165 #define PCIE_CONFIG_WR_TYPE0                    0xa
166 #define PCIE_CONFIG_WR_TYPE1                    0xb
167
168 /* PCIe Retries & Timeout definitions */
169 #define PIO_MAX_RETRIES                         1500
170 #define PIO_WAIT_TIMEOUT                        1000
171 #define LINK_MAX_RETRIES                        10
172 #define LINK_WAIT_TIMEOUT                       100000
173
174 #define CFG_RD_CRS_VAL                  0xFFFF0001
175
176 /**
177  * struct pcie_advk - Advk PCIe controller state
178  *
179  * @base:        The base address of the register space.
180  * @first_busno: Bus number of the PCIe root-port.
181  *               This may vary depending on the PCIe setup.
182  * @sec_busno:   Bus number for the device behind the PCIe root-port.
183  * @dev:         The pointer to PCI uclass device.
184  * @reset_gpio:  GPIO descriptor for PERST.
185  * @cfgcache:    Buffer for emulation of PCIe Root Port's PCI Bridge registers
186  *               that are not available on Aardvark.
187  * @cfgcrssve:   For CRSSVE emulation.
188  */
189 struct pcie_advk {
190         void                    *base;
191         int                     first_busno;
192         int                     sec_busno;
193         struct udevice          *dev;
194         struct gpio_desc        reset_gpio;
195         u32                     cfgcache[0x34 - 0x10];
196         bool                    cfgcrssve;
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  * @pcie: Pointer to the PCI bus
213  * @busno: Bus number of PCI device
214  * @dev: Device number of PCI device
215  * @func: Function number of PCI device
216  * @bdf: The PCI device to access
217  *
218  * Return: true on valid, false on invalid
219  */
220 static bool pcie_advk_addr_valid(struct pcie_advk *pcie,
221                                  int busno, u8 dev, u8 func)
222 {
223         /* On the primary (local) bus there is only one PCI Bridge */
224         if (busno == pcie->first_busno && (dev != 0 || func != 0))
225                 return false;
226
227         /*
228          * In PCI-E only a single device (0) can exist on the secondary bus.
229          * Beyond the secondary bus, there might be a Switch and anything is
230          * possible.
231          */
232         if (busno == pcie->sec_busno && dev != 0)
233                 return false;
234
235         return true;
236 }
237
238 /**
239  * pcie_advk_wait_pio() - Wait for PIO access to be accomplished
240  *
241  * @pcie: The PCI device to access
242  *
243  * Wait up to 1.5 seconds for PIO access to be accomplished.
244  *
245  * Return positive - retry count if PIO access is accomplished.
246  * Return negative - error if PIO access is timed out.
247  */
248 static int pcie_advk_wait_pio(struct pcie_advk *pcie)
249 {
250         uint start, isr;
251         uint count;
252
253         for (count = 1; count <= PIO_MAX_RETRIES; count++) {
254                 start = advk_readl(pcie, PIO_START);
255                 isr = advk_readl(pcie, PIO_ISR);
256                 if (!start && isr)
257                         return count;
258                 /*
259                  * Do not check the PIO state too frequently,
260                  * 100us delay is appropriate.
261                  */
262                 udelay(PIO_WAIT_TIMEOUT);
263         }
264
265         dev_err(pcie->dev, "PIO read/write transfer time out\n");
266         return -ETIMEDOUT;
267 }
268
269 /**
270  * pcie_advk_check_pio_status() - Validate PIO status and get the read result
271  *
272  * @pcie: Pointer to the PCI bus
273  * @allow_crs: Only for read requests, if CRS response is allowed
274  * @read_val: Pointer to the read result
275  *
276  * Return: 0 on success
277  */
278 static int pcie_advk_check_pio_status(struct pcie_advk *pcie,
279                                       bool allow_crs,
280                                       uint *read_val)
281 {
282         int ret;
283         uint reg;
284         unsigned int status;
285         char *strcomp_status, *str_posted;
286
287         reg = advk_readl(pcie, PIO_STAT);
288         status = (reg & PIO_COMPLETION_STATUS_MASK) >>
289                 PIO_COMPLETION_STATUS_SHIFT;
290
291         switch (status) {
292         case PIO_COMPLETION_STATUS_OK:
293                 if (reg & PIO_ERR_STATUS) {
294                         strcomp_status = "COMP_ERR";
295                         ret = -EFAULT;
296                         break;
297                 }
298                 /* Get the read result */
299                 if (read_val)
300                         *read_val = advk_readl(pcie, PIO_RD_DATA);
301                 /* No error */
302                 strcomp_status = NULL;
303                 ret = 0;
304                 break;
305         case PIO_COMPLETION_STATUS_UR:
306                 strcomp_status = "UR";
307                 ret = -EOPNOTSUPP;
308                 break;
309         case PIO_COMPLETION_STATUS_CRS:
310                 if (allow_crs && read_val) {
311                         /* For reading, CRS is not an error status. */
312                         *read_val = CFG_RD_CRS_VAL;
313                         strcomp_status = NULL;
314                         ret = 0;
315                 } else {
316                         strcomp_status = "CRS";
317                         ret = -EAGAIN;
318                 }
319                 break;
320         case PIO_COMPLETION_STATUS_CA:
321                 strcomp_status = "CA";
322                 ret = -ECANCELED;
323                 break;
324         default:
325                 strcomp_status = "Unknown";
326                 ret = -EINVAL;
327                 break;
328         }
329
330         if (!strcomp_status)
331                 return ret;
332
333         if (reg & PIO_NON_POSTED_REQ)
334                 str_posted = "Non-posted";
335         else
336                 str_posted = "Posted";
337
338         dev_dbg(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
339                 str_posted, strcomp_status, reg,
340                 advk_readl(pcie, PIO_ADDR_LS));
341
342         return ret;
343 }
344
345 /**
346  * pcie_advk_read_config() - Read from configuration space
347  *
348  * @bus: Pointer to the PCI bus
349  * @bdf: Identifies the PCIe device to access
350  * @offset: The offset into the device's configuration space
351  * @valuep: A pointer at which to store the read value
352  * @size: Indicates the size of access to perform
353  *
354  * Read a value of size @size from offset @offset within the configuration
355  * space of the device identified by the bus, device & function numbers in @bdf
356  * on the PCI bus @bus.
357  *
358  * Return: 0 on success
359  */
360 static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf,
361                                  uint offset, ulong *valuep,
362                                  enum pci_size_t size)
363 {
364         struct pcie_advk *pcie = dev_get_priv(bus);
365         int busno = PCI_BUS(bdf) - dev_seq(bus);
366         int retry_count;
367         bool allow_crs;
368         ulong data;
369         uint reg;
370         int ret;
371
372         dev_dbg(pcie->dev, "PCIE CFG read:  (b,d,f)=(%2d,%2d,%2d) ",
373                 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
374
375         if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
376                 dev_dbg(pcie->dev, "- out of range\n");
377                 *valuep = pci_get_ff(size);
378                 return 0;
379         }
380
381         /*
382          * The configuration space of the PCI Bridge on primary (local) bus is
383          * not accessible via PIO transfers like all other PCIe devices. PCI
384          * Bridge config registers are available directly in Aardvark memory
385          * space starting at offset zero. Moreover PCI Bridge registers in the
386          * range 0x10 - 0x34 are not available and register 0x38 (Expansion ROM
387          * Base Address) is at offset 0x30.
388          * We therefore read configuration space content of the primary PCI
389          * Bridge from our virtual cache.
390          */
391         if (busno == pcie->first_busno) {
392                 if (offset >= 0x10 && offset < 0x34)
393                         data = pcie->cfgcache[(offset - 0x10) / 4];
394                 else if ((offset & ~3) == PCI_ROM_ADDRESS1)
395                         data = advk_readl(pcie, PCIE_CORE_EXP_ROM_BAR_REG);
396                 else
397                         data = advk_readl(pcie, offset & ~3);
398
399                 if ((offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
400                         /*
401                          * Change Header Type of PCI Bridge device to Type 1
402                          * (0x01, used by PCI Bridges) because hardwired value
403                          * is Type 0 (0x00, used by Endpoint devices).
404                          */
405                         data &= ~0x007f0000;
406                         data |= PCI_HEADER_TYPE_BRIDGE << 16;
407                 }
408
409                 if ((offset & ~3) == PCIE_CORE_PCIEXP_CAP_OFF + PCI_EXP_RTCTL) {
410                         /* CRSSVE bit is stored only in cache */
411                         if (pcie->cfgcrssve)
412                                 data |= PCI_EXP_RTCTL_CRSSVE;
413                 }
414
415                 if ((offset & ~3) == PCIE_CORE_PCIEXP_CAP_OFF +
416                                      (PCI_EXP_RTCAP & ~3)) {
417                         /* CRS is emulated below, so set CRSVIS capability */
418                         data |= PCI_EXP_RTCAP_CRSVIS << 16;
419                 }
420
421                 *valuep = pci_conv_32_to_size(data, offset, size);
422
423                 return 0;
424         }
425
426         /*
427          * Returning fabricated CRS value (0xFFFF0001) by PCIe Root Complex to
428          * OS is allowed only for 4-byte PCI_VENDOR_ID config read request and
429          * only when CRSSVE bit in Root Port PCIe device is enabled. In all
430          * other error PCIe Root Complex must return all-ones.
431          *
432          * U-Boot currently does not support handling of CRS return value for
433          * PCI_VENDOR_ID config read request and also does not set CRSSVE bit.
434          * So it means that pcie->cfgcrssve is false. But the code is prepared
435          * for returning CRS, so that if U-Boot does support CRS in the future,
436          * it will work for Aardvark.
437          */
438         allow_crs = (offset == PCI_VENDOR_ID) && (size == PCI_SIZE_32) && pcie->cfgcrssve;
439
440         if (advk_readl(pcie, PIO_START)) {
441                 dev_err(pcie->dev,
442                         "Previous PIO read/write transfer is still running\n");
443                 if (allow_crs) {
444                         *valuep = CFG_RD_CRS_VAL;
445                         return 0;
446                 }
447                 *valuep = pci_get_ff(size);
448                 return -EAGAIN;
449         }
450
451         /* Program the control register */
452         reg = advk_readl(pcie, PIO_CTRL);
453         reg &= ~PIO_CTRL_TYPE_MASK;
454         if (busno == pcie->sec_busno)
455                 reg |= PCIE_CONFIG_RD_TYPE0;
456         else
457                 reg |= PCIE_CONFIG_RD_TYPE1;
458         advk_writel(pcie, reg, PIO_CTRL);
459
460         /* Program the address registers */
461         reg = PCIE_ECAM_OFFSET(busno, PCI_DEV(bdf), PCI_FUNC(bdf), (offset & ~0x3));
462         advk_writel(pcie, reg, PIO_ADDR_LS);
463         advk_writel(pcie, 0, PIO_ADDR_MS);
464
465         /* Program the data strobe */
466         advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
467
468         retry_count = 0;
469
470 retry:
471         /* Start the transfer */
472         advk_writel(pcie, 1, PIO_ISR);
473         advk_writel(pcie, 1, PIO_START);
474
475         ret = pcie_advk_wait_pio(pcie);
476         if (ret < 0) {
477                 if (allow_crs) {
478                         *valuep = CFG_RD_CRS_VAL;
479                         return 0;
480                 }
481                 *valuep = pci_get_ff(size);
482                 return ret;
483         }
484
485         retry_count += ret;
486
487         /* Check PIO status and get the read result */
488         ret = pcie_advk_check_pio_status(pcie, allow_crs, &reg);
489         if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
490                 goto retry;
491         if (ret) {
492                 *valuep = pci_get_ff(size);
493                 return ret;
494         }
495
496         dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
497                 offset, size, reg);
498         *valuep = pci_conv_32_to_size(reg, offset, size);
499
500         return 0;
501 }
502
503 /**
504  * pcie_calc_datastrobe() - Calculate data strobe
505  *
506  * @offset: The offset into the device's configuration space
507  * @size: Indicates the size of access to perform
508  *
509  * Calculate data strobe according to offset and size
510  *
511  */
512 static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size)
513 {
514         uint bytes, data_strobe;
515
516         switch (size) {
517         case PCI_SIZE_8:
518                 bytes = 1;
519                 break;
520         case PCI_SIZE_16:
521                 bytes = 2;
522                 break;
523         default:
524                 bytes = 4;
525         }
526
527         data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3);
528
529         return data_strobe;
530 }
531
532 /**
533  * pcie_advk_write_config() - Write to configuration space
534  *
535  * @bus: Pointer to the PCI bus
536  * @bdf: Identifies the PCIe device to access
537  * @offset: The offset into the device's configuration space
538  * @value: The value to write
539  * @size: Indicates the size of access to perform
540  *
541  * Write the value @value of size @size from offset @offset within the
542  * configuration space of the device identified by the bus, device & function
543  * numbers in @bdf on the PCI bus @bus.
544  *
545  * Return: 0 on success
546  */
547 static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf,
548                                   uint offset, ulong value,
549                                   enum pci_size_t size)
550 {
551         struct pcie_advk *pcie = dev_get_priv(bus);
552         int busno = PCI_BUS(bdf) - dev_seq(bus);
553         int retry_count;
554         ulong data;
555         uint reg;
556         int ret;
557
558         dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
559                 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
560         dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
561                 offset, size, value);
562
563         if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
564                 dev_dbg(pcie->dev, "- out of range\n");
565                 return 0;
566         }
567
568         /*
569          * As explained in pcie_advk_read_config(), for the configuration
570          * space of the primary PCI Bridge, we write the content into virtual
571          * cache.
572          */
573         if (busno == pcie->first_busno) {
574                 if (offset >= 0x10 && offset < 0x34) {
575                         data = pcie->cfgcache[(offset - 0x10) / 4];
576                         data = pci_conv_size_to_32(data, value, offset, size);
577                         /* This PCI bridge does not have configurable bars */
578                         if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
579                             (offset & ~3) == PCI_BASE_ADDRESS_1)
580                                 data = 0x0;
581                         pcie->cfgcache[(offset - 0x10) / 4] = data;
582                 } else if ((offset & ~3) == PCI_ROM_ADDRESS1) {
583                         data = advk_readl(pcie, PCIE_CORE_EXP_ROM_BAR_REG);
584                         data = pci_conv_size_to_32(data, value, offset, size);
585                         advk_writel(pcie, data, PCIE_CORE_EXP_ROM_BAR_REG);
586                 } else {
587                         data = advk_readl(pcie, offset & ~3);
588                         data = pci_conv_size_to_32(data, value, offset, size);
589                         advk_writel(pcie, data, offset & ~3);
590                 }
591
592                 if (offset == PCI_PRIMARY_BUS)
593                         pcie->first_busno = data & 0xff;
594
595                 if (offset == PCI_SECONDARY_BUS ||
596                     (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8))
597                         pcie->sec_busno = (data >> 8) & 0xff;
598
599                 if ((offset & ~3) == PCIE_CORE_PCIEXP_CAP_OFF + PCI_EXP_RTCTL)
600                         pcie->cfgcrssve = data & PCI_EXP_RTCTL_CRSSVE;
601
602                 return 0;
603         }
604
605         if (advk_readl(pcie, PIO_START)) {
606                 dev_err(pcie->dev,
607                         "Previous PIO read/write transfer is still running\n");
608                 return -EAGAIN;
609         }
610
611         /* Program the control register */
612         reg = advk_readl(pcie, PIO_CTRL);
613         reg &= ~PIO_CTRL_TYPE_MASK;
614         if (busno == pcie->sec_busno)
615                 reg |= PCIE_CONFIG_WR_TYPE0;
616         else
617                 reg |= PCIE_CONFIG_WR_TYPE1;
618         advk_writel(pcie, reg, PIO_CTRL);
619
620         /* Program the address registers */
621         reg = PCIE_ECAM_OFFSET(busno, PCI_DEV(bdf), PCI_FUNC(bdf), (offset & ~0x3));
622         advk_writel(pcie, reg, PIO_ADDR_LS);
623         advk_writel(pcie, 0, PIO_ADDR_MS);
624         dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg);
625
626         /* Program the data register */
627         reg = pci_conv_size_to_32(0, value, offset, size);
628         advk_writel(pcie, reg, PIO_WR_DATA);
629         dev_dbg(pcie->dev, "\tPIO req. - val  = 0x%08x\n", reg);
630
631         /* Program the data strobe */
632         reg = pcie_calc_datastrobe(offset, size);
633         advk_writel(pcie, reg, PIO_WR_DATA_STRB);
634         dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg);
635
636         retry_count = 0;
637
638 retry:
639         /* Start the transfer */
640         advk_writel(pcie, 1, PIO_ISR);
641         advk_writel(pcie, 1, PIO_START);
642
643         ret = pcie_advk_wait_pio(pcie);
644         if (ret < 0)
645                 return ret;
646
647         retry_count += ret;
648
649         /* Check PIO status */
650         ret = pcie_advk_check_pio_status(pcie, false, NULL);
651         if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES)
652                 goto retry;
653         return ret;
654 }
655
656 /**
657  * pcie_advk_link_up() - Check if PCIe link is up or not
658  *
659  * @pcie: The PCI device to access
660  *
661  * Return 1 (true) on link up.
662  * Return 0 (false) on link down.
663  */
664 static int pcie_advk_link_up(struct pcie_advk *pcie)
665 {
666         u32 val, ltssm_state;
667
668         val = advk_readl(pcie, CFG_REG);
669         ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
670         return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
671 }
672
673 /**
674  * pcie_advk_wait_for_link() - Wait for link training to be accomplished
675  *
676  * @pcie: The PCI device to access
677  *
678  * Wait up to 1 second for link training to be accomplished.
679  *
680  * Return 1 (true) if link training ends up with link up success.
681  * Return 0 (false) if link training ends up with link up failure.
682  */
683 static int pcie_advk_wait_for_link(struct pcie_advk *pcie)
684 {
685         int retries;
686
687         /* check if the link is up or not */
688         for (retries = 0; retries < LINK_MAX_RETRIES; retries++) {
689                 if (pcie_advk_link_up(pcie)) {
690                         printf("PCIe: Link up\n");
691                         return 0;
692                 }
693
694                 udelay(LINK_WAIT_TIMEOUT);
695         }
696
697         printf("PCIe: Link down\n");
698
699         return -ETIMEDOUT;
700 }
701
702 /*
703  * Set PCIe address window register which could be used for memory
704  * mapping.
705  */
706 static void pcie_advk_set_ob_win(struct pcie_advk *pcie, u8 win_num,
707                                  phys_addr_t match, phys_addr_t remap,
708                                  phys_addr_t mask, u32 actions)
709 {
710         advk_writel(pcie, OB_WIN_ENABLE |
711                           lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
712         advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
713         advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
714         advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
715         advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
716         advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
717         advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
718 }
719
720 static void pcie_advk_disable_ob_win(struct pcie_advk *pcie, u8 win_num)
721 {
722         advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
723         advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
724         advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
725         advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
726         advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
727         advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
728         advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
729 }
730
731 static void pcie_advk_set_ob_region(struct pcie_advk *pcie, int *wins,
732                                     struct pci_region *region, u32 actions)
733 {
734         phys_addr_t phys_start = region->phys_start;
735         pci_addr_t bus_start = region->bus_start;
736         pci_size_t size = region->size;
737         phys_addr_t win_mask;
738         u64 win_size;
739
740         if (*wins == -1)
741                 return;
742
743         /*
744          * The n-th PCIe window is configured by tuple (match, remap, mask)
745          * and an access to address A uses this window if A matches the
746          * match with given mask.
747          * So every PCIe window size must be a power of two and every start
748          * address must be aligned to window size. Minimal size is 64 KiB
749          * because lower 16 bits of mask must be zero. Remapped address
750          * may have set only bits from the mask.
751          */
752         while (*wins < OB_WIN_COUNT && size > 0) {
753                 /* Calculate the largest aligned window size */
754                 win_size = (1ULL << (fls64(size) - 1)) |
755                            (phys_start ? (1ULL << __ffs64(phys_start)) : 0);
756                 win_size = 1ULL << __ffs64(win_size);
757                 win_mask = ~(win_size - 1);
758                 if (win_size < 0x10000 || (bus_start & ~win_mask))
759                         break;
760
761                 dev_dbg(pcie->dev,
762                         "Configuring PCIe window %d: [0x%llx-0x%llx] as 0x%x\n",
763                         *wins, (u64)phys_start, (u64)phys_start + win_size,
764                         actions);
765                 pcie_advk_set_ob_win(pcie, *wins, phys_start, bus_start,
766                                      win_mask, actions);
767
768                 phys_start += win_size;
769                 bus_start += win_size;
770                 size -= win_size;
771                 (*wins)++;
772         }
773
774         if (size > 0) {
775                 *wins = -1;
776                 dev_err(pcie->dev,
777                         "Invalid PCIe region [0x%llx-0x%llx]\n",
778                         (u64)region->phys_start,
779                         (u64)region->phys_start + region->size);
780         }
781 }
782
783 /**
784  * pcie_advk_setup_hw() - PCIe initailzation
785  *
786  * @pcie: The PCI device to access
787  *
788  * Return: 0 on success
789  */
790 static int pcie_advk_setup_hw(struct pcie_advk *pcie)
791 {
792         struct pci_region *io, *mem, *pref;
793         int i, wins;
794         u32 reg;
795
796         /* Set to Direct mode */
797         reg = advk_readl(pcie, CTRL_CONFIG_REG);
798         reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
799         reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
800         advk_writel(pcie, reg, CTRL_CONFIG_REG);
801
802         /* Set PCI global control register to RC mode */
803         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
804         reg |= (IS_RC_MSK << IS_RC_SHIFT);
805         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
806
807         /*
808          * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
809          * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
810          * id in high 16 bits. Updating this register changes readback value of
811          * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
812          * for erratum 4.1: "The value of device and vendor ID is incorrect".
813          */
814         advk_writel(pcie, 0x11ab11ab, VENDOR_ID_REG);
815
816         /*
817          * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
818          * because default value is Mass Storage Controller (0x010400), causing
819          * U-Boot to fail to recognize it as P2P Bridge.
820          *
821          * Note that this Aardvark PCI Bridge does not have a compliant Type 1
822          * Configuration Space and it even cannot be accessed via Aardvark's
823          * PCI config space access method. Something like config space is
824          * available in internal Aardvark registers starting at offset 0x0
825          * and is reported as Type 0. In range 0x10 - 0x34 it has totally
826          * different registers. So our driver reports Header Type as Type 1 and
827          * for the above mentioned range redirects access to the virtual
828          * cfgcache[] buffer, which avoids changing internal Aardvark registers.
829          */
830         reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
831         reg &= ~0xffffff00;
832         reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
833         advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
834
835         /* Set Advanced Error Capabilities and Control PF0 register */
836         reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
837                 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
838                 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK |
839                 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV;
840         advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
841
842         /* Set PCIe Device Control and Status 1 PF0 register */
843         reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
844                 (PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE <<
845                  PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT) |
846                 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE <<
847                  PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT) |
848                 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE;
849         advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
850
851         /* Program PCIe Control 2 to disable strict ordering */
852         reg = PCIE_CORE_CTRL2_RESERVED |
853                 PCIE_CORE_CTRL2_TD_ENABLE;
854         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
855
856         /* Set GEN2 */
857         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
858         reg &= ~PCIE_GEN_SEL_MSK;
859         reg |= SPEED_GEN_2;
860         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
861
862         /* Set lane X1 */
863         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
864         reg &= ~LANE_CNT_MSK;
865         reg |= LANE_COUNT_1;
866         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
867
868         /* Enable link training */
869         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
870         reg |= LINK_TRAINING_EN;
871         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
872
873         /*
874          * Enable AXI address window location generation:
875          * When it is enabled, the default outbound window
876          * configurations (Default User Field: 0xD0074CFC)
877          * are used to transparent address translation for
878          * the outbound transactions. Thus, PCIe address
879          * windows are not required for transparent memory
880          * access when default outbound window configuration
881          * is set for memory access.
882          */
883         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
884         reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE;
885         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
886
887         /*
888          * Bypass the address window mapping for PIO:
889          * Since PIO access already contains all required
890          * info over AXI interface by PIO registers, the
891          * address window is not required.
892          */
893         reg = advk_readl(pcie, PIO_CTRL);
894         reg |= PIO_CTRL_ADDR_WIN_DISABLE;
895         advk_writel(pcie, reg, PIO_CTRL);
896
897         /*
898          * Set memory access in Default User Field so it
899          * is not required to configure PCIe address for
900          * transparent memory access.
901          */
902         advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
903
904         /*
905          * Configure PCIe address windows for non-memory or
906          * non-transparent access as by default PCIe uses
907          * transparent memory access.
908          */
909         wins = 0;
910         pci_get_regions(pcie->dev, &io, &mem, &pref);
911         if (io)
912                 pcie_advk_set_ob_region(pcie, &wins, io, OB_WIN_TYPE_IO);
913         if (mem && mem->phys_start != mem->bus_start)
914                 pcie_advk_set_ob_region(pcie, &wins, mem, OB_WIN_TYPE_MEM);
915         if (pref && pref->phys_start != pref->bus_start)
916                 pcie_advk_set_ob_region(pcie, &wins, pref, OB_WIN_TYPE_MEM);
917
918         /* Disable remaining PCIe outbound windows */
919         for (i = ((wins >= 0) ? wins : 0); i < OB_WIN_COUNT; i++)
920                 pcie_advk_disable_ob_win(pcie, i);
921
922         if (wins == -1)
923                 return -EINVAL;
924
925         /* Wait for PCIe link up */
926         if (pcie_advk_wait_for_link(pcie))
927                 return -ENXIO;
928
929         return 0;
930 }
931
932 /**
933  * pcie_advk_probe() - Probe the PCIe bus for active link
934  *
935  * @dev: A pointer to the device being operated on
936  *
937  * Probe for an active link on the PCIe bus and configure the controller
938  * to enable this port.
939  *
940  * Return: 0 on success, else -ENODEV
941  */
942 static int pcie_advk_probe(struct udevice *dev)
943 {
944         struct pcie_advk *pcie = dev_get_priv(dev);
945
946         gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio,
947                              GPIOD_IS_OUT);
948         /*
949          * Issue reset to add-in card through the dedicated GPIO.
950          * Some boards are connecting the card reset pin to common system
951          * reset wire and others are using separate GPIO port.
952          * In the last case we have to release a reset of the addon card
953          * using this GPIO.
954          *
955          * FIX-ME:
956          *     The PCIe RESET signal is not supposed to be released along
957          *     with the SOC RESET signal. It should be lowered as early as
958          *     possible before PCIe PHY initialization. Moreover, the PCIe
959          *     clock should be gated as well.
960          */
961         if (dm_gpio_is_valid(&pcie->reset_gpio)) {
962                 dev_dbg(dev, "Toggle PCIE Reset GPIO ...\n");
963                 dm_gpio_set_value(&pcie->reset_gpio, 1);
964                 mdelay(200);
965                 dm_gpio_set_value(&pcie->reset_gpio, 0);
966         } else {
967                 dev_warn(dev, "PCIE Reset on GPIO support is missing\n");
968         }
969
970         pcie->dev = pci_get_controller(dev);
971
972         /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
973         pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
974                 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
975         pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
976                 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
977
978         return pcie_advk_setup_hw(pcie);
979 }
980
981 static int pcie_advk_remove(struct udevice *dev)
982 {
983         struct pcie_advk *pcie = dev_get_priv(dev);
984         u32 reg;
985         int i;
986
987         for (i = 0; i < OB_WIN_COUNT; i++)
988                 pcie_advk_disable_ob_win(pcie, i);
989
990         reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
991         reg &= ~(PCIE_CORE_CMD_MEM_ACCESS_EN |
992                  PCIE_CORE_CMD_IO_ACCESS_EN |
993                  PCIE_CORE_CMD_MEM_IO_REQ_EN);
994         advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
995
996         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
997         reg &= ~LINK_TRAINING_EN;
998         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
999
1000         return 0;
1001 }
1002
1003 /**
1004  * pcie_advk_of_to_plat() - Translate from DT to device state
1005  *
1006  * @dev: A pointer to the device being operated on
1007  *
1008  * Translate relevant data from the device tree pertaining to device @dev into
1009  * state that the driver will later make use of. This state is stored in the
1010  * device's private data structure.
1011  *
1012  * Return: 0 on success, else -EINVAL
1013  */
1014 static int pcie_advk_of_to_plat(struct udevice *dev)
1015 {
1016         struct pcie_advk *pcie = dev_get_priv(dev);
1017
1018         /* Get the register base address */
1019         pcie->base = (void *)dev_read_addr_index(dev, 0);
1020         if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE)
1021                 return -EINVAL;
1022
1023         return 0;
1024 }
1025
1026 static const struct dm_pci_ops pcie_advk_ops = {
1027         .read_config    = pcie_advk_read_config,
1028         .write_config   = pcie_advk_write_config,
1029 };
1030
1031 static const struct udevice_id pcie_advk_ids[] = {
1032         { .compatible = "marvell,armada-3700-pcie" },
1033         { }
1034 };
1035
1036 U_BOOT_DRIVER(pcie_advk) = {
1037         .name                   = "pcie_advk",
1038         .id                     = UCLASS_PCI,
1039         .of_match               = pcie_advk_ids,
1040         .ops                    = &pcie_advk_ops,
1041         .of_to_plat     = pcie_advk_of_to_plat,
1042         .probe                  = pcie_advk_probe,
1043         .remove                 = pcie_advk_remove,
1044         .flags                  = DM_FLAG_OS_PREPARE,
1045         .priv_auto      = sizeof(struct pcie_advk),
1046 };
This page took 0.073829 seconds and 2 git commands to generate.