blob: e8aa78faa16df31becd8f717e0cdcee94c42e391 [file] [log] [blame]
Will Deaconce292992013-11-22 16:14:41 +00001/*
2 * Simple, generic PCI host controller driver targetting firmware-initialised
3 * systems and virtual machines (e.g. the PCI emulation provided by kvmtool).
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright (C) 2014 ARM Limited
18 *
19 * Author: Will Deacon <will.deacon@arm.com>
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/of_address.h>
25#include <linux/of_pci.h>
26#include <linux/platform_device.h>
27
David Daney7149b9f2016-03-11 15:18:38 -060028#include "pci-host-common.h"
Will Deaconce292992013-11-22 16:14:41 +000029
30static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus,
31 unsigned int devfn,
32 int where)
33{
Jayachandran C499733e2015-08-05 02:23:38 +053034 struct gen_pci *pci = bus->sysdata;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +010035 resource_size_t idx = bus->number - pci->cfg.bus_range->start;
Will Deaconce292992013-11-22 16:14:41 +000036
37 return pci->cfg.win[idx] + ((devfn << 8) | where);
38}
39
40static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = {
41 .bus_shift = 16,
David Daney9a280332015-10-08 10:15:10 -050042 .ops = {
43 .map_bus = gen_pci_map_cfg_bus_cam,
44 .read = pci_generic_config_read,
45 .write = pci_generic_config_write,
46 }
Will Deaconce292992013-11-22 16:14:41 +000047};
48
49static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
50 unsigned int devfn,
51 int where)
52{
Jayachandran C499733e2015-08-05 02:23:38 +053053 struct gen_pci *pci = bus->sysdata;
Lorenzo Pieralisidbf98262014-10-23 16:23:07 +010054 resource_size_t idx = bus->number - pci->cfg.bus_range->start;
Will Deaconce292992013-11-22 16:14:41 +000055
56 return pci->cfg.win[idx] + ((devfn << 12) | where);
57}
58
59static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = {
60 .bus_shift = 20,
David Daney9a280332015-10-08 10:15:10 -050061 .ops = {
62 .map_bus = gen_pci_map_cfg_bus_ecam,
63 .read = pci_generic_config_read,
64 .write = pci_generic_config_write,
65 }
Will Deaconce292992013-11-22 16:14:41 +000066};
67
68static const struct of_device_id gen_pci_of_match[] = {
69 { .compatible = "pci-host-cam-generic",
70 .data = &gen_pci_cfg_cam_bus_ops },
71
72 { .compatible = "pci-host-ecam-generic",
73 .data = &gen_pci_cfg_ecam_bus_ops },
74
75 { },
76};
77MODULE_DEVICE_TABLE(of, gen_pci_of_match);
78
David Daneyd51b37102016-03-11 15:25:13 -060079static int gen_pci_probe(struct platform_device *pdev)
80{
81 struct device *dev = &pdev->dev;
82 const struct of_device_id *of_id;
83 struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
84
85 if (!pci)
86 return -ENOMEM;
87
88 of_id = of_match_node(gen_pci_of_match, dev->of_node);
89 pci->cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data;
90
91 return pci_host_common_probe(pdev, pci);
92}
93
Will Deaconce292992013-11-22 16:14:41 +000094static struct platform_driver gen_pci_driver = {
95 .driver = {
96 .name = "pci-host-generic",
Will Deaconce292992013-11-22 16:14:41 +000097 .of_match_table = gen_pci_of_match,
98 },
99 .probe = gen_pci_probe,
100};
101module_platform_driver(gen_pci_driver);
102
103MODULE_DESCRIPTION("Generic PCI host driver");
104MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
Bjorn Helgaaseed65422014-07-15 15:07:46 -0600105MODULE_LICENSE("GPL v2");