Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 1 | /* |
| 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 Daney | 7149b9f | 2016-03-11 15:18:38 -0600 | [diff] [blame] | 28 | #include "pci-host-common.h" |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 29 | |
| 30 | static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus, |
| 31 | unsigned int devfn, |
| 32 | int where) |
| 33 | { |
Jayachandran C | 499733e | 2015-08-05 02:23:38 +0530 | [diff] [blame] | 34 | struct gen_pci *pci = bus->sysdata; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 35 | resource_size_t idx = bus->number - pci->cfg.bus_range->start; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 36 | |
| 37 | return pci->cfg.win[idx] + ((devfn << 8) | where); |
| 38 | } |
| 39 | |
| 40 | static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = { |
| 41 | .bus_shift = 16, |
David Daney | 9a28033 | 2015-10-08 10:15:10 -0500 | [diff] [blame] | 42 | .ops = { |
| 43 | .map_bus = gen_pci_map_cfg_bus_cam, |
| 44 | .read = pci_generic_config_read, |
| 45 | .write = pci_generic_config_write, |
| 46 | } |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus, |
| 50 | unsigned int devfn, |
| 51 | int where) |
| 52 | { |
Jayachandran C | 499733e | 2015-08-05 02:23:38 +0530 | [diff] [blame] | 53 | struct gen_pci *pci = bus->sysdata; |
Lorenzo Pieralisi | dbf9826 | 2014-10-23 16:23:07 +0100 | [diff] [blame] | 54 | resource_size_t idx = bus->number - pci->cfg.bus_range->start; |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 55 | |
| 56 | return pci->cfg.win[idx] + ((devfn << 12) | where); |
| 57 | } |
| 58 | |
| 59 | static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = { |
| 60 | .bus_shift = 20, |
David Daney | 9a28033 | 2015-10-08 10:15:10 -0500 | [diff] [blame] | 61 | .ops = { |
| 62 | .map_bus = gen_pci_map_cfg_bus_ecam, |
| 63 | .read = pci_generic_config_read, |
| 64 | .write = pci_generic_config_write, |
| 65 | } |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static 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 | }; |
| 77 | MODULE_DEVICE_TABLE(of, gen_pci_of_match); |
| 78 | |
David Daney | d51b3710 | 2016-03-11 15:25:13 -0600 | [diff] [blame] | 79 | static 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 Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 94 | static struct platform_driver gen_pci_driver = { |
| 95 | .driver = { |
| 96 | .name = "pci-host-generic", |
Will Deacon | ce29299 | 2013-11-22 16:14:41 +0000 | [diff] [blame] | 97 | .of_match_table = gen_pci_of_match, |
| 98 | }, |
| 99 | .probe = gen_pci_probe, |
| 100 | }; |
| 101 | module_platform_driver(gen_pci_driver); |
| 102 | |
| 103 | MODULE_DESCRIPTION("Generic PCI host driver"); |
| 104 | MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>"); |
Bjorn Helgaas | eed6542 | 2014-07-15 15:07:46 -0600 | [diff] [blame] | 105 | MODULE_LICENSE("GPL v2"); |