pci: layerscape: Split the EP and RC driver
[platform/kernel/u-boot.git] / drivers / pci / pcie_layerscape_ep.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2020 NXP
4  * Layerscape PCIe EP driver
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <dm/devres.h>
10 #include <errno.h>
11 #include <pci_ep.h>
12 #include <asm/io.h>
13 #include <linux/sizes.h>
14 #include <linux/log2.h>
15 #include "pcie_layerscape.h"
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 static void ls_pcie_ep_enable_cfg(struct ls_pcie_ep *pcie_ep)
20 {
21         struct ls_pcie *pcie = pcie_ep->pcie;
22         u32 config;
23
24         config = ctrl_readl(pcie,  PCIE_PF_CONFIG);
25         config |= PCIE_CONFIG_READY;
26         ctrl_writel(pcie, config, PCIE_PF_CONFIG);
27 }
28
29 static int ls_ep_set_bar(struct udevice *dev, uint fn, struct pci_bar *ep_bar)
30 {
31         struct ls_pcie_ep *pcie_ep = dev_get_priv(dev);
32         struct ls_pcie *pcie = pcie_ep->pcie;
33         dma_addr_t bar_phys = ep_bar->phys_addr;
34         enum pci_barno bar = ep_bar->barno;
35         u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
36         int flags = ep_bar->flags;
37         int type, idx;
38         u64 size;
39
40         idx  = bar;
41         /* BAR size is 2^(aperture + 11) */
42         size = max_t(size_t, ep_bar->size, FSL_PCIE_EP_MIN_APERTURE);
43
44         if (!(flags & PCI_BASE_ADDRESS_SPACE))
45                 type = PCIE_ATU_TYPE_MEM;
46         else
47                 type = PCIE_ATU_TYPE_IO;
48
49         ls_pcie_atu_inbound_set(pcie, idx, bar, bar_phys, type);
50
51         dbi_writel(pcie, lower_32_bits(size - 1), reg + PCIE_NO_SRIOV_BAR_BASE);
52         dbi_writel(pcie, flags, reg);
53
54         if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
55                 dbi_writel(pcie, upper_32_bits(size - 1),
56                            reg + 4 + PCIE_NO_SRIOV_BAR_BASE);
57                 dbi_writel(pcie, 0, reg + 4);
58         }
59
60         return 0;
61 }
62
63 static struct pci_ep_ops ls_pcie_ep_ops = {
64         .set_bar = ls_ep_set_bar,
65 };
66
67 static void ls_pcie_ep_setup_atu(struct ls_pcie_ep *pcie_ep)
68 {
69         struct ls_pcie *pcie = pcie_ep->pcie;
70         u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
71
72         /* ATU 0 : INBOUND : map BAR0 */
73         ls_pcie_atu_inbound_set(pcie, 0, PCIE_ATU_TYPE_MEM, 0, phys);
74         /* ATU 1 : INBOUND : map BAR1 */
75         phys += PCIE_BAR1_SIZE;
76         ls_pcie_atu_inbound_set(pcie, 1, PCIE_ATU_TYPE_MEM, 1, phys);
77         /* ATU 2 : INBOUND : map BAR2 */
78         phys += PCIE_BAR2_SIZE;
79         ls_pcie_atu_inbound_set(pcie, 2, PCIE_ATU_TYPE_MEM, 2, phys);
80         /* ATU 3 : INBOUND : map BAR4 */
81         phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
82         ls_pcie_atu_inbound_set(pcie, 3, PCIE_ATU_TYPE_MEM, 4, phys);
83
84         /* ATU 0 : OUTBOUND : map MEM */
85         ls_pcie_atu_outbound_set(pcie, 0,
86                                  PCIE_ATU_TYPE_MEM,
87                                  pcie_ep->addr_res.start,
88                                  0,
89                                  CONFIG_SYS_PCI_MEMORY_SIZE);
90 }
91
92 /* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
93 static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
94 {
95         /* The least inbound window is 4KiB */
96         if (size < 4 * 1024)
97                 return;
98
99         switch (bar) {
100         case 0:
101                 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
102                 break;
103         case 1:
104                 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
105                 break;
106         case 2:
107                 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
108                 writel(0, bar_base + PCI_BASE_ADDRESS_3);
109                 break;
110         case 4:
111                 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
112                 writel(0, bar_base + PCI_BASE_ADDRESS_5);
113                 break;
114         default:
115                 break;
116         }
117 }
118
119 static void ls_pcie_ep_setup_bars(void *bar_base)
120 {
121         /* BAR0 - 32bit - 4K configuration */
122         ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
123         /* BAR1 - 32bit - 8K MSIX */
124         ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
125         /* BAR2 - 64bit - 4K MEM descriptor */
126         ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
127         /* BAR4 - 64bit - 1M MEM */
128         ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
129 }
130
131 static void ls_pcie_setup_ep(struct ls_pcie_ep *pcie_ep)
132 {
133         u32 sriov;
134         struct ls_pcie *pcie = pcie_ep->pcie;
135
136         sriov = readl(pcie->dbi + PCIE_SRIOV);
137         if (PCI_EXT_CAP_ID(sriov) == PCI_EXT_CAP_ID_SRIOV) {
138                 int pf, vf;
139
140                 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
141                         for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
142                                 ctrl_writel(pcie, PCIE_LCTRL0_VAL(pf, vf),
143                                             PCIE_PF_VF_CTRL);
144
145                                 ls_pcie_ep_setup_bars(pcie->dbi);
146                                 ls_pcie_ep_setup_atu(pcie_ep);
147                         }
148                 }
149                 /* Disable CFG2 */
150                 ctrl_writel(pcie, 0, PCIE_PF_VF_CTRL);
151         } else {
152                 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
153                 ls_pcie_ep_setup_atu(pcie_ep);
154         }
155
156         ls_pcie_ep_enable_cfg(pcie_ep);
157 }
158
159 static int ls_pcie_ep_probe(struct udevice *dev)
160 {
161         struct ls_pcie_ep *pcie_ep = dev_get_priv(dev);
162         struct ls_pcie *pcie;
163         u16 link_sta;
164         int ret;
165
166         pcie = devm_kmalloc(dev, sizeof(*pcie), GFP_KERNEL);
167         if (!pcie)
168                 return -ENOMEM;
169
170         pcie_ep->pcie = pcie;
171
172         pcie->dbi = (void __iomem *)devfdt_get_addr_index(dev, 0);
173         if (!pcie->dbi)
174                 return -ENOMEM;
175
176         pcie->ctrl = (void __iomem *)devfdt_get_addr_index(dev, 1);
177         if (!pcie->ctrl)
178                 return -ENOMEM;
179
180         ret = fdt_get_named_resource(gd->fdt_blob, dev_of_offset(dev),
181                                      "reg", "reg-names",
182                                      "addr_space", &pcie_ep->addr_res);
183         if (ret) {
184                 printf("%s: resource \"addr_space\" not found\n", dev->name);
185                 return ret;
186         }
187
188         pcie->idx = ((unsigned long)pcie->dbi - PCIE_SYS_BASE_ADDR) /
189                     PCIE_CCSR_SIZE;
190
191         pcie->big_endian = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
192                                            "big-endian");
193
194         pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f;
195         if (pcie->mode != PCI_HEADER_TYPE_NORMAL)
196                 return 0;
197
198         pcie_ep->max_functions = fdtdec_get_int(gd->fdt_blob,
199                                                 dev_of_offset(dev),
200                                                 "max-functions", 1);
201         pcie_ep->num_ib_wins = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
202                                               "num-ib-windows", 8);
203         pcie_ep->num_ob_wins = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
204                                               "num-ob-windows", 8);
205
206         printf("PCIe%u: %s %s", pcie->idx, dev->name, "Endpoint");
207         ls_pcie_setup_ep(pcie_ep);
208
209         if (!ls_pcie_link_up(pcie)) {
210                 /* Let the user know there's no PCIe link */
211                 printf(": no link\n");
212                 return 0;
213         }
214
215         /* Print the negotiated PCIe link width */
216         link_sta = readw(pcie->dbi + PCIE_LINK_STA);
217         printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
218                link_sta & PCIE_LINK_SPEED_MASK);
219
220         return 0;
221 }
222
223 static int ls_pcie_ep_remove(struct udevice *dev)
224 {
225         return 0;
226 }
227
228 const struct udevice_id ls_pcie_ep_ids[] = {
229         { .compatible = "fsl,ls-pcie-ep" },
230         { }
231 };
232
233 U_BOOT_DRIVER(pci_layerscape_ep) = {
234         .name = "pci_layerscape_ep",
235         .id     = UCLASS_PCI_EP,
236         .of_match = ls_pcie_ep_ids,
237         .ops = &ls_pcie_ep_ops,
238         .probe = ls_pcie_ep_probe,
239         .remove = ls_pcie_ep_remove,
240         .priv_auto_alloc_size = sizeof(struct ls_pcie_ep),
241 };