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