xref: /linux/drivers/mcb/mcb-pci.c (revision c5aec4c76af1a2d89ee2f2d4d5463b2ad2d85de5)
1 /*
2  * MEN Chameleon Bus.
3  *
4  * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
5  * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; version 2 of the License.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/mcb.h>
15 
16 #include "mcb-internal.h"
17 
18 struct priv {
19 	struct mcb_bus *bus;
20 	void __iomem *base;
21 };
22 
23 static int mcb_pci_get_irq(struct mcb_device *mdev)
24 {
25 	struct mcb_bus *mbus = mdev->bus;
26 	struct device *dev = mbus->carrier;
27 	struct pci_dev *pdev = to_pci_dev(dev);
28 
29 	return pdev->irq;
30 }
31 
32 static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
33 {
34 	struct priv *priv;
35 	phys_addr_t mapbase;
36 	int ret;
37 	int num_cells;
38 	unsigned long flags;
39 
40 	priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
41 	if (!priv)
42 		return -ENOMEM;
43 
44 	ret = pci_enable_device(pdev);
45 	if (ret) {
46 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
47 		return -ENODEV;
48 	}
49 
50 	mapbase = pci_resource_start(pdev, 0);
51 	if (!mapbase) {
52 		dev_err(&pdev->dev, "No PCI resource\n");
53 		goto err_start;
54 	}
55 
56 	ret = pci_request_region(pdev, 0, KBUILD_MODNAME);
57 	if (ret) {
58 		dev_err(&pdev->dev, "Failed to request PCI BARs\n");
59 		goto err_start;
60 	}
61 
62 	priv->base = pci_iomap(pdev, 0, 0);
63 	if (!priv->base) {
64 		dev_err(&pdev->dev, "Cannot ioremap\n");
65 		ret = -ENOMEM;
66 		goto err_ioremap;
67 	}
68 
69 	flags = pci_resource_flags(pdev, 0);
70 	if (flags & IORESOURCE_IO) {
71 		ret = -ENOTSUPP;
72 		dev_err(&pdev->dev,
73 			"IO mapped PCI devices are not supported\n");
74 		goto err_ioremap;
75 	}
76 
77 	pci_set_drvdata(pdev, priv);
78 
79 	priv->bus = mcb_alloc_bus(&pdev->dev);
80 	if (IS_ERR(priv->bus)) {
81 		ret = PTR_ERR(priv->bus);
82 		goto err_drvdata;
83 	}
84 
85 	priv->bus->get_irq = mcb_pci_get_irq;
86 
87 	ret = chameleon_parse_cells(priv->bus, mapbase, priv->base);
88 	if (ret < 0)
89 		goto err_drvdata;
90 	num_cells = ret;
91 
92 	dev_dbg(&pdev->dev, "Found %d cells\n", num_cells);
93 
94 	mcb_bus_add_devices(priv->bus);
95 
96 err_drvdata:
97 	pci_iounmap(pdev, priv->base);
98 err_ioremap:
99 	pci_release_region(pdev, 0);
100 err_start:
101 	pci_disable_device(pdev);
102 	return ret;
103 }
104 
105 static void mcb_pci_remove(struct pci_dev *pdev)
106 {
107 	struct priv *priv = pci_get_drvdata(pdev);
108 
109 	mcb_release_bus(priv->bus);
110 }
111 
112 static const struct pci_device_id mcb_pci_tbl[] = {
113 	{ PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
114 	{ 0 },
115 };
116 MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
117 
118 static struct pci_driver mcb_pci_driver = {
119 	.name = "mcb-pci",
120 	.id_table = mcb_pci_tbl,
121 	.probe = mcb_pci_probe,
122 	.remove = mcb_pci_remove,
123 };
124 
125 module_pci_driver(mcb_pci_driver);
126 
127 MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
128 MODULE_LICENSE("GPL");
129 MODULE_DESCRIPTION("MCB over PCI support");
130