GNU Linux-libre 5.15.137-gnu
[releases.git] / arch / arm / mach-mv78xx0 / pcie.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * arch/arm/mach-mv78xx0/pcie.c
4  *
5  * PCIe functions for Marvell MV78xx0 SoCs
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/pci.h>
10 #include <linux/mbus.h>
11 #include <video/vga.h>
12 #include <asm/irq.h>
13 #include <asm/mach/pci.h>
14 #include <plat/pcie.h>
15 #include "mv78xx0.h"
16 #include "common.h"
17
18 #define MV78XX0_MBUS_PCIE_MEM_TARGET(port, lane) ((port) ? 8 : 4)
19 #define MV78XX0_MBUS_PCIE_MEM_ATTR(port, lane)   (0xf8 & ~(0x10 << (lane)))
20 #define MV78XX0_MBUS_PCIE_IO_TARGET(port, lane)  ((port) ? 8 : 4)
21 #define MV78XX0_MBUS_PCIE_IO_ATTR(port, lane)    (0xf0 & ~(0x10 << (lane)))
22
23 struct pcie_port {
24         u8                      maj;
25         u8                      min;
26         u8                      root_bus_nr;
27         void __iomem            *base;
28         spinlock_t              conf_lock;
29         char                    mem_space_name[20];
30         struct resource         res;
31 };
32
33 static struct pcie_port pcie_port[8];
34 static int num_pcie_ports;
35 static struct resource pcie_io_space;
36
37 void __init mv78xx0_pcie_id(u32 *dev, u32 *rev)
38 {
39         *dev = orion_pcie_dev_id(PCIE00_VIRT_BASE);
40         *rev = orion_pcie_rev(PCIE00_VIRT_BASE);
41 }
42
43 u32 pcie_port_size[8] = {
44         0,
45         0x30000000,
46         0x10000000,
47         0x10000000,
48         0x08000000,
49         0x08000000,
50         0x08000000,
51         0x04000000,
52 };
53
54 static void __init mv78xx0_pcie_preinit(void)
55 {
56         int i;
57         u32 size_each;
58         u32 start;
59
60         pcie_io_space.name = "PCIe I/O Space";
61         pcie_io_space.start = MV78XX0_PCIE_IO_PHYS_BASE(0);
62         pcie_io_space.end =
63                 MV78XX0_PCIE_IO_PHYS_BASE(0) + MV78XX0_PCIE_IO_SIZE * 8 - 1;
64         pcie_io_space.flags = IORESOURCE_MEM;
65         if (request_resource(&iomem_resource, &pcie_io_space))
66                 panic("can't allocate PCIe I/O space");
67
68         if (num_pcie_ports > 7)
69                 panic("invalid number of PCIe ports");
70
71         size_each = pcie_port_size[num_pcie_ports];
72
73         start = MV78XX0_PCIE_MEM_PHYS_BASE;
74         for (i = 0; i < num_pcie_ports; i++) {
75                 struct pcie_port *pp = pcie_port + i;
76
77                 snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
78                         "PCIe %d.%d MEM", pp->maj, pp->min);
79                 pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
80                 pp->res.name = pp->mem_space_name;
81                 pp->res.flags = IORESOURCE_MEM;
82                 pp->res.start = start;
83                 pp->res.end = start + size_each - 1;
84                 start += size_each;
85
86                 if (request_resource(&iomem_resource, &pp->res))
87                         panic("can't allocate PCIe MEM sub-space");
88
89                 mvebu_mbus_add_window_by_id(MV78XX0_MBUS_PCIE_MEM_TARGET(pp->maj, pp->min),
90                                             MV78XX0_MBUS_PCIE_MEM_ATTR(pp->maj, pp->min),
91                                             pp->res.start, resource_size(&pp->res));
92                 mvebu_mbus_add_window_remap_by_id(MV78XX0_MBUS_PCIE_IO_TARGET(pp->maj, pp->min),
93                                                   MV78XX0_MBUS_PCIE_IO_ATTR(pp->maj, pp->min),
94                                                   i * SZ_64K, SZ_64K, 0);
95         }
96 }
97
98 static int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys)
99 {
100         struct pcie_port *pp;
101
102         if (nr >= num_pcie_ports)
103                 return 0;
104
105         pp = &pcie_port[nr];
106         sys->private_data = pp;
107         pp->root_bus_nr = sys->busnr;
108
109         /*
110          * Generic PCIe unit setup.
111          */
112         orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
113         orion_pcie_setup(pp->base);
114
115         pci_ioremap_io(nr * SZ_64K, MV78XX0_PCIE_IO_PHYS_BASE(nr));
116
117         pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
118
119         return 1;
120 }
121
122 static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
123 {
124         /*
125          * Don't go out when trying to access nonexisting devices
126          * on the local bus.
127          */
128         if (bus == pp->root_bus_nr && dev > 1)
129                 return 0;
130
131         return 1;
132 }
133
134 static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
135                         int size, u32 *val)
136 {
137         struct pci_sys_data *sys = bus->sysdata;
138         struct pcie_port *pp = sys->private_data;
139         unsigned long flags;
140         int ret;
141
142         if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
143                 *val = 0xffffffff;
144                 return PCIBIOS_DEVICE_NOT_FOUND;
145         }
146
147         spin_lock_irqsave(&pp->conf_lock, flags);
148         ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
149         spin_unlock_irqrestore(&pp->conf_lock, flags);
150
151         return ret;
152 }
153
154 static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
155                         int where, int size, u32 val)
156 {
157         struct pci_sys_data *sys = bus->sysdata;
158         struct pcie_port *pp = sys->private_data;
159         unsigned long flags;
160         int ret;
161
162         if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
163                 return PCIBIOS_DEVICE_NOT_FOUND;
164
165         spin_lock_irqsave(&pp->conf_lock, flags);
166         ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
167         spin_unlock_irqrestore(&pp->conf_lock, flags);
168
169         return ret;
170 }
171
172 static struct pci_ops pcie_ops = {
173         .read = pcie_rd_conf,
174         .write = pcie_wr_conf,
175 };
176
177 static void rc_pci_fixup(struct pci_dev *dev)
178 {
179         /*
180          * Prevent enumeration of root complex.
181          */
182         if (dev->bus->parent == NULL && dev->devfn == 0) {
183                 int i;
184
185                 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
186                         dev->resource[i].start = 0;
187                         dev->resource[i].end   = 0;
188                         dev->resource[i].flags = 0;
189                 }
190         }
191 }
192 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
193
194 static int __init mv78xx0_pcie_scan_bus(int nr, struct pci_host_bridge *bridge)
195 {
196         struct pci_sys_data *sys = pci_host_bridge_priv(bridge);
197
198         if (nr >= num_pcie_ports) {
199                 BUG();
200                 return -EINVAL;
201         }
202
203         list_splice_init(&sys->resources, &bridge->windows);
204         bridge->dev.parent = NULL;
205         bridge->sysdata = sys;
206         bridge->busnr = sys->busnr;
207         bridge->ops = &pcie_ops;
208
209         return pci_scan_root_bus_bridge(bridge);
210 }
211
212 static int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot,
213         u8 pin)
214 {
215         struct pci_sys_data *sys = dev->bus->sysdata;
216         struct pcie_port *pp = sys->private_data;
217
218         return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min;
219 }
220
221 static struct hw_pci mv78xx0_pci __initdata = {
222         .nr_controllers = 8,
223         .preinit        = mv78xx0_pcie_preinit,
224         .setup          = mv78xx0_pcie_setup,
225         .scan           = mv78xx0_pcie_scan_bus,
226         .map_irq        = mv78xx0_pcie_map_irq,
227 };
228
229 static void __init add_pcie_port(int maj, int min, void __iomem *base)
230 {
231         printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min);
232
233         if (orion_pcie_link_up(base)) {
234                 struct pcie_port *pp = &pcie_port[num_pcie_ports++];
235
236                 printk("link up\n");
237
238                 pp->maj = maj;
239                 pp->min = min;
240                 pp->root_bus_nr = -1;
241                 pp->base = base;
242                 spin_lock_init(&pp->conf_lock);
243                 memset(&pp->res, 0, sizeof(pp->res));
244         } else {
245                 printk("link down, ignoring\n");
246         }
247 }
248
249 void __init mv78xx0_pcie_init(int init_port0, int init_port1)
250 {
251         vga_base = MV78XX0_PCIE_MEM_PHYS_BASE;
252
253         if (init_port0) {
254                 add_pcie_port(0, 0, PCIE00_VIRT_BASE);
255                 if (!orion_pcie_x4_mode(PCIE00_VIRT_BASE)) {
256                         add_pcie_port(0, 1, PCIE01_VIRT_BASE);
257                         add_pcie_port(0, 2, PCIE02_VIRT_BASE);
258                         add_pcie_port(0, 3, PCIE03_VIRT_BASE);
259                 }
260         }
261
262         if (init_port1) {
263                 add_pcie_port(1, 0, PCIE10_VIRT_BASE);
264                 if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) {
265                         add_pcie_port(1, 1, PCIE11_VIRT_BASE);
266                         add_pcie_port(1, 2, PCIE12_VIRT_BASE);
267                         add_pcie_port(1, 3, PCIE13_VIRT_BASE);
268                 }
269         }
270
271         pci_common_init(&mv78xx0_pci);
272 }