]> Pileus Git - ~andy/linux/blob - arch/arm/mach-mv78xx0/pcie.c
ARM: Orion: mbus_dram_info consolidation
[~andy/linux] / arch / arm / mach-mv78xx0 / pcie.c
1 /*
2  * arch/arm/mach-mv78xx0/pcie.c
3  *
4  * PCIe functions for Marvell MV78xx0 SoCs
5  *
6  * This file is licensed under the terms of the GNU General Public
7  * License version 2.  This program is licensed "as is" without any
8  * warranty of any kind, whether express or implied.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/mbus.h>
14 #include <video/vga.h>
15 #include <asm/irq.h>
16 #include <asm/mach/pci.h>
17 #include <plat/pcie.h>
18 #include <plat/addr-map.h>
19 #include "common.h"
20
21 struct pcie_port {
22         u8                      maj;
23         u8                      min;
24         u8                      root_bus_nr;
25         void __iomem            *base;
26         spinlock_t              conf_lock;
27         char                    io_space_name[16];
28         char                    mem_space_name[16];
29         struct resource         res[2];
30 };
31
32 static struct pcie_port pcie_port[8];
33 static int num_pcie_ports;
34 static struct resource pcie_io_space;
35 static struct resource pcie_mem_space;
36
37
38 void __init mv78xx0_pcie_id(u32 *dev, u32 *rev)
39 {
40         *dev = orion_pcie_dev_id((void __iomem *)PCIE00_VIRT_BASE);
41         *rev = orion_pcie_rev((void __iomem *)PCIE00_VIRT_BASE);
42 }
43
44 static void __init mv78xx0_pcie_preinit(void)
45 {
46         int i;
47         u32 size_each;
48         u32 start;
49         int win;
50
51         pcie_io_space.name = "PCIe I/O Space";
52         pcie_io_space.start = MV78XX0_PCIE_IO_PHYS_BASE(0);
53         pcie_io_space.end =
54                 MV78XX0_PCIE_IO_PHYS_BASE(0) + MV78XX0_PCIE_IO_SIZE * 8 - 1;
55         pcie_io_space.flags = IORESOURCE_IO;
56         if (request_resource(&iomem_resource, &pcie_io_space))
57                 panic("can't allocate PCIe I/O space");
58
59         pcie_mem_space.name = "PCIe MEM Space";
60         pcie_mem_space.start = MV78XX0_PCIE_MEM_PHYS_BASE;
61         pcie_mem_space.end =
62                 MV78XX0_PCIE_MEM_PHYS_BASE + MV78XX0_PCIE_MEM_SIZE - 1;
63         pcie_mem_space.flags = IORESOURCE_MEM;
64         if (request_resource(&iomem_resource, &pcie_mem_space))
65                 panic("can't allocate PCIe MEM space");
66
67         for (i = 0; i < num_pcie_ports; i++) {
68                 struct pcie_port *pp = pcie_port + i;
69
70                 snprintf(pp->io_space_name, sizeof(pp->io_space_name),
71                         "PCIe %d.%d I/O", pp->maj, pp->min);
72                 pp->io_space_name[sizeof(pp->io_space_name) - 1] = 0;
73                 pp->res[0].name = pp->io_space_name;
74                 pp->res[0].start = MV78XX0_PCIE_IO_PHYS_BASE(i);
75                 pp->res[0].end = pp->res[0].start + MV78XX0_PCIE_IO_SIZE - 1;
76                 pp->res[0].flags = IORESOURCE_IO;
77
78                 snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
79                         "PCIe %d.%d MEM", pp->maj, pp->min);
80                 pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
81                 pp->res[1].name = pp->mem_space_name;
82                 pp->res[1].flags = IORESOURCE_MEM;
83         }
84
85         switch (num_pcie_ports) {
86         case 0:
87                 size_each = 0;
88                 break;
89
90         case 1:
91                 size_each = 0x30000000;
92                 break;
93
94         case 2 ... 3:
95                 size_each = 0x10000000;
96                 break;
97
98         case 4 ... 6:
99                 size_each = 0x08000000;
100                 break;
101
102         case 7:
103                 size_each = 0x04000000;
104                 break;
105
106         default:
107                 panic("invalid number of PCIe ports");
108         }
109
110         start = MV78XX0_PCIE_MEM_PHYS_BASE;
111         for (i = 0; i < num_pcie_ports; i++) {
112                 struct pcie_port *pp = pcie_port + i;
113
114                 pp->res[1].start = start;
115                 pp->res[1].end = start + size_each - 1;
116                 start += size_each;
117         }
118
119         for (i = 0; i < num_pcie_ports; i++) {
120                 struct pcie_port *pp = pcie_port + i;
121
122                 if (request_resource(&pcie_io_space, &pp->res[0]))
123                         panic("can't allocate PCIe I/O sub-space");
124
125                 if (request_resource(&pcie_mem_space, &pp->res[1]))
126                         panic("can't allocate PCIe MEM sub-space");
127         }
128
129         win = 0;
130         for (i = 0; i < num_pcie_ports; i++) {
131                 struct pcie_port *pp = pcie_port + i;
132
133                 mv78xx0_setup_pcie_io_win(win++, pp->res[0].start,
134                                           resource_size(&pp->res[0]),
135                                           pp->maj, pp->min);
136
137                 mv78xx0_setup_pcie_mem_win(win++, pp->res[1].start,
138                                            resource_size(&pp->res[1]),
139                                            pp->maj, pp->min);
140         }
141 }
142
143 static int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys)
144 {
145         struct pcie_port *pp;
146
147         if (nr >= num_pcie_ports)
148                 return 0;
149
150         pp = &pcie_port[nr];
151         pp->root_bus_nr = sys->busnr;
152
153         /*
154          * Generic PCIe unit setup.
155          */
156         orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
157         orion_pcie_setup(pp->base, &orion_mbus_dram_info);
158
159         sys->resource[0] = &pp->res[0];
160         sys->resource[1] = &pp->res[1];
161         sys->resource[2] = NULL;
162
163         return 1;
164 }
165
166 static struct pcie_port *bus_to_port(int bus)
167 {
168         int i;
169
170         for (i = num_pcie_ports - 1; i >= 0; i--) {
171                 int rbus = pcie_port[i].root_bus_nr;
172                 if (rbus != -1 && rbus <= bus)
173                         break;
174         }
175
176         return i >= 0 ? pcie_port + i : NULL;
177 }
178
179 static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
180 {
181         /*
182          * Don't go out when trying to access nonexisting devices
183          * on the local bus.
184          */
185         if (bus == pp->root_bus_nr && dev > 1)
186                 return 0;
187
188         return 1;
189 }
190
191 static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
192                         int size, u32 *val)
193 {
194         struct pcie_port *pp = bus_to_port(bus->number);
195         unsigned long flags;
196         int ret;
197
198         if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
199                 *val = 0xffffffff;
200                 return PCIBIOS_DEVICE_NOT_FOUND;
201         }
202
203         spin_lock_irqsave(&pp->conf_lock, flags);
204         ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
205         spin_unlock_irqrestore(&pp->conf_lock, flags);
206
207         return ret;
208 }
209
210 static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
211                         int where, int size, u32 val)
212 {
213         struct pcie_port *pp = bus_to_port(bus->number);
214         unsigned long flags;
215         int ret;
216
217         if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
218                 return PCIBIOS_DEVICE_NOT_FOUND;
219
220         spin_lock_irqsave(&pp->conf_lock, flags);
221         ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
222         spin_unlock_irqrestore(&pp->conf_lock, flags);
223
224         return ret;
225 }
226
227 static struct pci_ops pcie_ops = {
228         .read = pcie_rd_conf,
229         .write = pcie_wr_conf,
230 };
231
232 static void __devinit rc_pci_fixup(struct pci_dev *dev)
233 {
234         /*
235          * Prevent enumeration of root complex.
236          */
237         if (dev->bus->parent == NULL && dev->devfn == 0) {
238                 int i;
239
240                 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
241                         dev->resource[i].start = 0;
242                         dev->resource[i].end   = 0;
243                         dev->resource[i].flags = 0;
244                 }
245         }
246 }
247 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
248
249 static struct pci_bus __init *
250 mv78xx0_pcie_scan_bus(int nr, struct pci_sys_data *sys)
251 {
252         struct pci_bus *bus;
253
254         if (nr < num_pcie_ports) {
255                 bus = pci_scan_bus(sys->busnr, &pcie_ops, sys);
256         } else {
257                 bus = NULL;
258                 BUG();
259         }
260
261         return bus;
262 }
263
264 static int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot,
265         u8 pin)
266 {
267         struct pcie_port *pp = bus_to_port(dev->bus->number);
268
269         return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min;
270 }
271
272 static struct hw_pci mv78xx0_pci __initdata = {
273         .nr_controllers = 8,
274         .preinit        = mv78xx0_pcie_preinit,
275         .swizzle        = pci_std_swizzle,
276         .setup          = mv78xx0_pcie_setup,
277         .scan           = mv78xx0_pcie_scan_bus,
278         .map_irq        = mv78xx0_pcie_map_irq,
279 };
280
281 static void __init add_pcie_port(int maj, int min, unsigned long base)
282 {
283         printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min);
284
285         if (orion_pcie_link_up((void __iomem *)base)) {
286                 struct pcie_port *pp = &pcie_port[num_pcie_ports++];
287
288                 printk("link up\n");
289
290                 pp->maj = maj;
291                 pp->min = min;
292                 pp->root_bus_nr = -1;
293                 pp->base = (void __iomem *)base;
294                 spin_lock_init(&pp->conf_lock);
295                 memset(pp->res, 0, sizeof(pp->res));
296         } else {
297                 printk("link down, ignoring\n");
298         }
299 }
300
301 void __init mv78xx0_pcie_init(int init_port0, int init_port1)
302 {
303         vga_base = MV78XX0_PCIE_MEM_PHYS_BASE;
304
305         if (init_port0) {
306                 add_pcie_port(0, 0, PCIE00_VIRT_BASE);
307                 if (!orion_pcie_x4_mode((void __iomem *)PCIE00_VIRT_BASE)) {
308                         add_pcie_port(0, 1, PCIE01_VIRT_BASE);
309                         add_pcie_port(0, 2, PCIE02_VIRT_BASE);
310                         add_pcie_port(0, 3, PCIE03_VIRT_BASE);
311                 }
312         }
313
314         if (init_port1) {
315                 add_pcie_port(1, 0, PCIE10_VIRT_BASE);
316                 if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) {
317                         add_pcie_port(1, 1, PCIE11_VIRT_BASE);
318                         add_pcie_port(1, 2, PCIE12_VIRT_BASE);
319                         add_pcie_port(1, 3, PCIE13_VIRT_BASE);
320                 }
321         }
322
323         pci_common_init(&mv78xx0_pci);
324 }