2 * Copyright 2004 Koninklijke Philips Electronics NV
4 * Conversion to platform driver and DT:
5 * Copyright 2014 Linaro Ltd.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * 14/04/2005 Initial version, colin.king@philips.com
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_pci.h>
22 #include <linux/of_platform.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
26 static void __iomem
*versatile_pci_base
;
27 static void __iomem
*versatile_cfg_base
[2];
29 #define PCI_IMAP(m) (versatile_pci_base + ((m) * 4))
30 #define PCI_SMAP(m) (versatile_pci_base + 0x14 + ((m) * 4))
31 #define PCI_SELFID (versatile_pci_base + 0xc)
33 #define VP_PCI_DEVICE_ID 0x030010ee
34 #define VP_PCI_CLASS_ID 0x0b400000
36 static u32 pci_slot_ignore
;
38 static int __init
versatile_pci_slot_ignore(char *str
)
43 while ((retval
= get_option(&str
, &slot
))) {
44 if ((slot
< 0) || (slot
> 31))
45 pr_err("Illegal slot value: %d\n", slot
);
47 pci_slot_ignore
|= (1 << slot
);
51 __setup("pci_slot_ignore=", versatile_pci_slot_ignore
);
54 static void __iomem
*versatile_map_bus(struct pci_bus
*bus
,
55 unsigned int devfn
, int offset
)
57 unsigned int busnr
= bus
->number
;
59 if (pci_slot_ignore
& (1 << PCI_SLOT(devfn
)))
62 return versatile_cfg_base
[1] + ((busnr
<< 16) | (devfn
<< 8) | offset
);
65 static struct pci_ops pci_versatile_ops
= {
66 .map_bus
= versatile_map_bus
,
67 .read
= pci_generic_config_read32
,
68 .write
= pci_generic_config_write
,
71 static int versatile_pci_parse_request_of_pci_ranges(struct device
*dev
,
72 struct list_head
*res
)
74 int err
, mem
= 1, res_valid
= 0;
75 struct device_node
*np
= dev
->of_node
;
76 resource_size_t iobase
;
77 struct resource_entry
*win
;
79 err
= of_pci_get_host_bridge_resources(np
, 0, 0xff, res
, &iobase
);
83 resource_list_for_each_entry(win
, res
) {
84 struct resource
*parent
, *res
= win
->res
;
86 switch (resource_type(res
)) {
88 parent
= &ioport_resource
;
89 err
= pci_remap_iospace(res
, iobase
);
91 dev_warn(dev
, "error %d: failed to map resource %pR\n",
97 parent
= &iomem_resource
;
98 res_valid
|= !(res
->flags
& IORESOURCE_PREFETCH
);
100 writel(res
->start
>> 28, PCI_IMAP(mem
));
101 writel(PHYS_OFFSET
>> 28, PCI_SMAP(mem
));
110 err
= devm_request_resource(dev
, parent
, res
);
112 goto out_release_res
;
116 dev_err(dev
, "non-prefetchable memory resource required\n");
118 goto out_release_res
;
124 pci_free_resource_list(res
);
128 /* Unused, temporary to satisfy ARM arch code */
129 struct pci_sys_data sys
;
131 static int versatile_pci_probe(struct platform_device
*pdev
)
133 struct resource
*res
;
134 int ret
, i
, myslot
= -1;
136 void __iomem
*local_pci_cfg_base
;
140 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
141 versatile_pci_base
= devm_ioremap_resource(&pdev
->dev
, res
);
142 if (IS_ERR(versatile_pci_base
))
143 return PTR_ERR(versatile_pci_base
);
145 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
146 versatile_cfg_base
[0] = devm_ioremap_resource(&pdev
->dev
, res
);
147 if (IS_ERR(versatile_cfg_base
[0]))
148 return PTR_ERR(versatile_cfg_base
[0]);
150 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
151 versatile_cfg_base
[1] = devm_ioremap_resource(&pdev
->dev
, res
);
152 if (IS_ERR(versatile_cfg_base
[1]))
153 return PTR_ERR(versatile_cfg_base
[1]);
155 ret
= versatile_pci_parse_request_of_pci_ranges(&pdev
->dev
, &pci_res
);
160 * We need to discover the PCI core first to configure itself
161 * before the main PCI probing is performed
163 for (i
= 0; i
< 32; i
++) {
164 if ((readl(versatile_cfg_base
[0] + (i
<< 11) + PCI_VENDOR_ID
) == VP_PCI_DEVICE_ID
) &&
165 (readl(versatile_cfg_base
[0] + (i
<< 11) + PCI_CLASS_REVISION
) == VP_PCI_CLASS_ID
)) {
171 dev_err(&pdev
->dev
, "Cannot find PCI core!\n");
175 * Do not to map Versatile FPGA PCI device into memory space
177 pci_slot_ignore
|= (1 << myslot
);
179 dev_info(&pdev
->dev
, "PCI core found (slot %d)\n", myslot
);
181 writel(myslot
, PCI_SELFID
);
182 local_pci_cfg_base
= versatile_cfg_base
[1] + (myslot
<< 11);
184 val
= readl(local_pci_cfg_base
+ PCI_COMMAND
);
185 val
|= PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
| PCI_COMMAND_INVALIDATE
;
186 writel(val
, local_pci_cfg_base
+ PCI_COMMAND
);
189 * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM
191 writel(PHYS_OFFSET
, local_pci_cfg_base
+ PCI_BASE_ADDRESS_0
);
192 writel(PHYS_OFFSET
, local_pci_cfg_base
+ PCI_BASE_ADDRESS_1
);
193 writel(PHYS_OFFSET
, local_pci_cfg_base
+ PCI_BASE_ADDRESS_2
);
196 * For many years the kernel and QEMU were symbiotically buggy
197 * in that they both assumed the same broken IRQ mapping.
198 * QEMU therefore attempts to auto-detect old broken kernels
199 * so that they still work on newer QEMU as they did on old
200 * QEMU. Since we now use the correct (ie matching-hardware)
201 * IRQ mapping we write a definitely different value to a
202 * PCI_INTERRUPT_LINE register to tell QEMU that we expect
203 * real hardware behaviour and it need not be backwards
204 * compatible for us. This write is harmless on real hardware.
206 writel(0, versatile_cfg_base
[0] + PCI_INTERRUPT_LINE
);
208 pci_add_flags(PCI_ENABLE_PROC_DOMAINS
);
209 pci_add_flags(PCI_REASSIGN_ALL_BUS
| PCI_REASSIGN_ALL_RSRC
);
211 bus
= pci_scan_root_bus(&pdev
->dev
, 0, &pci_versatile_ops
, &sys
, &pci_res
);
215 pci_fixup_irqs(pci_common_swizzle
, of_irq_parse_and_map_pci
);
216 pci_assign_unassigned_bus_resources(bus
);
217 pci_bus_add_devices(bus
);
222 static const struct of_device_id versatile_pci_of_match
[] = {
223 { .compatible
= "arm,versatile-pci", },
226 MODULE_DEVICE_TABLE(of
, versatile_pci_of_match
);
228 static struct platform_driver versatile_pci_driver
= {
230 .name
= "versatile-pci",
231 .of_match_table
= versatile_pci_of_match
,
233 .probe
= versatile_pci_probe
,
235 module_platform_driver(versatile_pci_driver
);
237 MODULE_DESCRIPTION("Versatile PCI driver");
238 MODULE_LICENSE("GPL v2");