]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/pci/pcie_layerscape.c
buildman: Make -V (verbose_build) really be verbose
[karo-tx-uboot.git] / drivers / pci / pcie_layerscape.c
1 /*
2  * Copyright 2014-2015 Freescale Semiconductor, Inc.
3  * Layerscape PCIe driver
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <asm/arch/fsl_serdes.h>
10 #include <pci.h>
11 #include <asm/io.h>
12 #include <errno.h>
13 #include <malloc.h>
14 #include <asm/pcie_layerscape.h>
15
16 #ifndef CONFIG_SYS_PCI_MEMORY_BUS
17 #define CONFIG_SYS_PCI_MEMORY_BUS CONFIG_SYS_SDRAM_BASE
18 #endif
19
20 #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
21 #define CONFIG_SYS_PCI_MEMORY_PHYS CONFIG_SYS_SDRAM_BASE
22 #endif
23
24 #ifndef CONFIG_SYS_PCI_MEMORY_SIZE
25 #define CONFIG_SYS_PCI_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL) /* 2G */
26 #endif
27
28 /* iATU registers */
29 #define PCIE_ATU_VIEWPORT               0x900
30 #define PCIE_ATU_REGION_INBOUND         (0x1 << 31)
31 #define PCIE_ATU_REGION_OUTBOUND        (0x0 << 31)
32 #define PCIE_ATU_REGION_INDEX0          (0x0 << 0)
33 #define PCIE_ATU_REGION_INDEX1          (0x1 << 0)
34 #define PCIE_ATU_REGION_INDEX2          (0x2 << 0)
35 #define PCIE_ATU_REGION_INDEX3          (0x3 << 0)
36 #define PCIE_ATU_CR1                    0x904
37 #define PCIE_ATU_TYPE_MEM               (0x0 << 0)
38 #define PCIE_ATU_TYPE_IO                (0x2 << 0)
39 #define PCIE_ATU_TYPE_CFG0              (0x4 << 0)
40 #define PCIE_ATU_TYPE_CFG1              (0x5 << 0)
41 #define PCIE_ATU_CR2                    0x908
42 #define PCIE_ATU_ENABLE                 (0x1 << 31)
43 #define PCIE_ATU_BAR_MODE_ENABLE        (0x1 << 30)
44 #define PCIE_ATU_LOWER_BASE             0x90C
45 #define PCIE_ATU_UPPER_BASE             0x910
46 #define PCIE_ATU_LIMIT                  0x914
47 #define PCIE_ATU_LOWER_TARGET           0x918
48 #define PCIE_ATU_BUS(x)                 (((x) & 0xff) << 24)
49 #define PCIE_ATU_DEV(x)                 (((x) & 0x1f) << 19)
50 #define PCIE_ATU_FUNC(x)                (((x) & 0x7) << 16)
51 #define PCIE_ATU_UPPER_TARGET           0x91C
52
53 #define PCIE_LINK_CAP           0x7c
54 #define PCIE_LINK_SPEED_MASK    0xf
55 #define PCIE_LINK_STA           0x82
56
57 #define PCIE_DBI_SIZE           (4 * 1024) /* 4K */
58
59 struct ls_pcie {
60         int idx;
61         void __iomem *dbi;
62         void __iomem *va_cfg0;
63         void __iomem *va_cfg1;
64         struct pci_controller hose;
65 };
66
67 struct ls_pcie_info {
68         unsigned long regs;
69         int pci_num;
70         u64 cfg0_phys;
71         u64 cfg0_size;
72         u64 cfg1_phys;
73         u64 cfg1_size;
74         u64 mem_bus;
75         u64 mem_phys;
76         u64 mem_size;
77         u64 io_bus;
78         u64 io_phys;
79         u64 io_size;
80 };
81
82 #define SET_LS_PCIE_INFO(x, num)                        \
83 {                                                       \
84         x.regs = CONFIG_SYS_PCIE##num##_ADDR;           \
85         x.cfg0_phys = CONFIG_SYS_PCIE_CFG0_PHYS_OFF +   \
86                       CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
87         x.cfg0_size = CONFIG_SYS_PCIE_CFG0_SIZE;        \
88         x.cfg1_phys = CONFIG_SYS_PCIE_CFG1_PHYS_OFF +   \
89                       CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
90         x.cfg1_size = CONFIG_SYS_PCIE_CFG1_SIZE;        \
91         x.mem_bus = CONFIG_SYS_PCIE_MEM_BUS;            \
92         x.mem_phys = CONFIG_SYS_PCIE_MEM_PHYS_OFF +     \
93                      CONFIG_SYS_PCIE##num##_PHYS_ADDR;  \
94         x.mem_size = CONFIG_SYS_PCIE_MEM_SIZE;          \
95         x.io_bus = CONFIG_SYS_PCIE_IO_BUS;              \
96         x.io_phys = CONFIG_SYS_PCIE_IO_PHYS_OFF +       \
97                     CONFIG_SYS_PCIE##num##_PHYS_ADDR;   \
98         x.io_size = CONFIG_SYS_PCIE_IO_SIZE;            \
99         x.pci_num = num;                                \
100 }
101
102 #ifdef CONFIG_LS102XA
103 #include <asm/arch/immap_ls102xa.h>
104
105 /* PEX1/2 Misc Ports Status Register */
106 #define LTSSM_STATE_SHIFT       20
107 #define LTSSM_STATE_MASK        0x3f
108 #define LTSSM_PCIE_L0           0x11 /* L0 state */
109
110 static int ls_pcie_link_state(struct ls_pcie *pcie)
111 {
112         u32 state;
113         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
114
115         state = in_be32(&scfg->pexmscportsr[pcie->idx]);
116         state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
117         if (state < LTSSM_PCIE_L0) {
118                 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
119                 return 0;
120         }
121
122         return 1;
123 }
124 #else
125 #define PCIE_LDBG 0x7FC
126
127 static int ls_pcie_link_state(struct ls_pcie *pcie)
128 {
129         u32 state;
130
131         state = readl(pcie->dbi + PCIE_LDBG);
132         if (state)
133                 return 1;
134
135         debug("....PCIe link error.\n");
136         return 0;
137 }
138 #endif
139
140 static int ls_pcie_link_up(struct ls_pcie *pcie)
141 {
142         int state;
143         u32 cap;
144
145         state = ls_pcie_link_state(pcie);
146         if (state)
147                 return state;
148
149         /* Try to download speed to gen1 */
150         cap = readl(pcie->dbi + PCIE_LINK_CAP);
151         writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
152         udelay(2000);
153         state = ls_pcie_link_state(pcie);
154         if (state)
155                 return state;
156
157         writel(cap, pcie->dbi + PCIE_LINK_CAP);
158
159         return 0;
160 }
161
162 static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
163 {
164         writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
165                pcie->dbi + PCIE_ATU_VIEWPORT);
166         writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
167 }
168
169 static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
170 {
171         writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
172                pcie->dbi + PCIE_ATU_VIEWPORT);
173         writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
174 }
175
176 static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
177                                       u64 phys, u64 bus_addr, pci_size_t size)
178 {
179         writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
180         writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
181         writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
182         writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
183         writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
184         writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
185         writel(type, pcie->dbi + PCIE_ATU_CR1);
186         writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
187 }
188
189 static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
190 {
191 #ifdef DEBUG
192         int i;
193 #endif
194
195         /* ATU 0 : OUTBOUND : CFG0 */
196         ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
197                                   PCIE_ATU_TYPE_CFG0,
198                                   info->cfg0_phys,
199                                   0,
200                                   info->cfg0_size);
201         /* ATU 1 : OUTBOUND : CFG1 */
202         ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
203                                   PCIE_ATU_TYPE_CFG1,
204                                   info->cfg1_phys,
205                                   0,
206                                   info->cfg1_size);
207         /* ATU 2 : OUTBOUND : MEM */
208         ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
209                                   PCIE_ATU_TYPE_MEM,
210                                   info->mem_phys,
211                                   info->mem_bus,
212                                   info->mem_size);
213         /* ATU 3 : OUTBOUND : IO */
214         ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
215                                   PCIE_ATU_TYPE_IO,
216                                   info->io_phys,
217                                   info->io_bus,
218                                   info->io_size);
219
220 #ifdef DEBUG
221         for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
222                 writel(PCIE_ATU_REGION_OUTBOUND | i,
223                        pcie->dbi + PCIE_ATU_VIEWPORT);
224                 debug("iATU%d:\n", i);
225                 debug("\tLOWER PHYS 0x%08x\n",
226                       readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
227                 debug("\tUPPER PHYS 0x%08x\n",
228                       readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
229                 debug("\tLOWER BUS  0x%08x\n",
230                       readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
231                 debug("\tUPPER BUS  0x%08x\n",
232                       readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
233                 debug("\tLIMIT      0x%08x\n",
234                       readl(pcie->dbi + PCIE_ATU_LIMIT));
235                 debug("\tCR1        0x%08x\n",
236                       readl(pcie->dbi + PCIE_ATU_CR1));
237                 debug("\tCR2        0x%08x\n",
238                       readl(pcie->dbi + PCIE_ATU_CR2));
239         }
240 #endif
241 }
242
243 int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
244 {
245         /* Do not skip controller */
246         return 0;
247 }
248
249 static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
250 {
251         if (PCI_DEV(d) > 0)
252                 return -EINVAL;
253
254         return 0;
255 }
256
257 static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
258                                int where, u32 *val)
259 {
260         struct ls_pcie *pcie = hose->priv_data;
261         u32 busdev, *addr;
262
263         if (ls_pcie_addr_valid(hose, d)) {
264                 *val = 0xffffffff;
265                 return -EINVAL;
266         }
267
268         if (PCI_BUS(d) == hose->first_busno) {
269                 addr = pcie->dbi + (where & ~0x3);
270         } else {
271                 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
272                          PCIE_ATU_DEV(PCI_DEV(d)) |
273                          PCIE_ATU_FUNC(PCI_FUNC(d));
274
275                 if (PCI_BUS(d) == hose->first_busno + 1) {
276                         ls_pcie_cfg0_set_busdev(pcie, busdev);
277                         addr = pcie->va_cfg0 + (where & ~0x3);
278                 } else {
279                         ls_pcie_cfg1_set_busdev(pcie, busdev);
280                         addr = pcie->va_cfg1 + (where & ~0x3);
281                 }
282         }
283
284         *val = readl(addr);
285
286         return 0;
287 }
288
289 static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
290                                 int where, u32 val)
291 {
292         struct ls_pcie *pcie = hose->priv_data;
293         u32 busdev, *addr;
294
295         if (ls_pcie_addr_valid(hose, d))
296                 return -EINVAL;
297
298         if (PCI_BUS(d) == hose->first_busno) {
299                 addr = pcie->dbi + (where & ~0x3);
300         } else {
301                 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
302                          PCIE_ATU_DEV(PCI_DEV(d)) |
303                          PCIE_ATU_FUNC(PCI_FUNC(d));
304
305                 if (PCI_BUS(d) == hose->first_busno + 1) {
306                         ls_pcie_cfg0_set_busdev(pcie, busdev);
307                         addr = pcie->va_cfg0 + (where & ~0x3);
308                 } else {
309                         ls_pcie_cfg1_set_busdev(pcie, busdev);
310                         addr = pcie->va_cfg1 + (where & ~0x3);
311                 }
312         }
313
314         writel(val, addr);
315
316         return 0;
317 }
318
319 static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
320                                struct ls_pcie_info *info)
321 {
322         struct pci_controller *hose = &pcie->hose;
323         pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
324
325         ls_pcie_setup_atu(pcie, info);
326
327         pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
328
329         /* program correct class for RC */
330         pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
331                                    PCI_CLASS_BRIDGE_PCI);
332 }
333
334 int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
335 {
336         struct ls_pcie *pcie;
337         struct pci_controller *hose;
338         int num = dev - PCIE1;
339         pci_dev_t pdev = PCI_BDF(busno, 0, 0);
340         int i, linkup, ep_mode;
341         u8 header_type;
342         u16 temp16;
343
344         if (!is_serdes_configured(dev)) {
345                 printf("PCIe%d: disabled\n", num + 1);
346                 return busno;
347         }
348
349         pcie = malloc(sizeof(*pcie));
350         if (!pcie)
351                 return busno;
352         memset(pcie, 0, sizeof(*pcie));
353
354         hose = &pcie->hose;
355         hose->priv_data = pcie;
356         hose->first_busno = busno;
357         pcie->idx = num;
358         pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
359         pcie->va_cfg0 = map_physmem(info->cfg0_phys,
360                                     info->cfg0_size,
361                                     MAP_NOCACHE);
362         pcie->va_cfg1 = map_physmem(info->cfg1_phys,
363                                     info->cfg1_size,
364                                     MAP_NOCACHE);
365
366         /* outbound memory */
367         pci_set_region(&hose->regions[0],
368                        (pci_size_t)info->mem_bus,
369                        (phys_size_t)info->mem_phys,
370                        (pci_size_t)info->mem_size,
371                        PCI_REGION_MEM);
372
373         /* outbound io */
374         pci_set_region(&hose->regions[1],
375                        (pci_size_t)info->io_bus,
376                        (phys_size_t)info->io_phys,
377                        (pci_size_t)info->io_size,
378                        PCI_REGION_IO);
379
380         /* System memory space */
381         pci_set_region(&hose->regions[2],
382                        CONFIG_SYS_PCI_MEMORY_BUS,
383                        CONFIG_SYS_PCI_MEMORY_PHYS,
384                        CONFIG_SYS_PCI_MEMORY_SIZE,
385                        PCI_REGION_SYS_MEMORY);
386
387         hose->region_count = 3;
388
389         for (i = 0; i < hose->region_count; i++)
390                 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
391                       i,
392                       (u64)hose->regions[i].phys_start,
393                       (u64)hose->regions[i].bus_start,
394                       (u64)hose->regions[i].size,
395                       hose->regions[i].flags);
396
397         pci_set_ops(hose,
398                     pci_hose_read_config_byte_via_dword,
399                     pci_hose_read_config_word_via_dword,
400                     ls_pcie_read_config,
401                     pci_hose_write_config_byte_via_dword,
402                     pci_hose_write_config_word_via_dword,
403                     ls_pcie_write_config);
404
405         pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
406         ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
407         printf("PCIe%u: %s ", info->pci_num,
408                ep_mode ? "Endpoint" : "Root Complex");
409
410         linkup = ls_pcie_link_up(pcie);
411
412         if (!linkup) {
413                 /* Let the user know there's no PCIe link */
414                 printf("no link, regs @ 0x%lx\n", info->regs);
415                 hose->last_busno = hose->first_busno;
416                 return busno;
417         }
418
419         /* Print the negotiated PCIe link width */
420         pci_hose_read_config_word(hose, dev, PCIE_LINK_STA, &temp16);
421                 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
422                        (temp16 & 0xf), info->regs);
423
424         if (ep_mode)
425                 return busno;
426
427         ls_pcie_setup_ctrl(pcie, info);
428
429         pci_register_hose(hose);
430
431         hose->last_busno = pci_hose_scan(hose);
432
433         printf("PCIe%x: Bus %02x - %02x\n",
434                info->pci_num, hose->first_busno, hose->last_busno);
435
436         return hose->last_busno + 1;
437 }
438
439 int ls_pcie_init_board(int busno)
440 {
441         struct ls_pcie_info info;
442
443 #ifdef CONFIG_PCIE1
444         SET_LS_PCIE_INFO(info, 1);
445         busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
446 #endif
447
448 #ifdef CONFIG_PCIE2
449         SET_LS_PCIE_INFO(info, 2);
450         busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
451 #endif
452
453 #ifdef CONFIG_PCIE3
454         SET_LS_PCIE_INFO(info, 3);
455         busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
456 #endif
457
458 #ifdef CONFIG_PCIE4
459         SET_LS_PCIE_INFO(info, 4);
460         busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
461 #endif
462
463         return busno;
464 }
465
466 void pci_init_board(void)
467 {
468         ls_pcie_init_board(0);
469 }
470
471 #ifdef CONFIG_OF_BOARD_SETUP
472 #include <libfdt.h>
473 #include <fdt_support.h>
474
475 static void ft_pcie_ls_setup(void *blob, const char *pci_compat,
476                              unsigned long ctrl_addr, enum srds_prtcl dev)
477 {
478         int off;
479
480         off = fdt_node_offset_by_compat_reg(blob, pci_compat,
481                                             (phys_addr_t)ctrl_addr);
482         if (off < 0)
483                 return;
484
485         if (!is_serdes_configured(dev))
486                 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
487 }
488
489 void ft_pcie_setup(void *blob, bd_t *bd)
490 {
491         #ifdef CONFIG_PCIE1
492         ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE1_ADDR, PCIE1);
493         #endif
494
495         #ifdef CONFIG_PCIE2
496         ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE2_ADDR, PCIE2);
497         #endif
498
499         #ifdef CONFIG_PCIE3
500         ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE3_ADDR, PCIE3);
501         #endif
502
503         #ifdef CONFIG_PCIE4
504         ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE4_ADDR, PCIE4);
505         #endif
506 }
507
508 #else
509 void ft_pcie_setup(void *blob, bd_t *bd)
510 {
511 }
512 #endif