xref: /linux/drivers/mcb/mcb-pci.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
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 	phys_addr_t mapbase;
21 	void __iomem *base;
22 };
23 
24 static int mcb_pci_get_irq(struct mcb_device *mdev)
25 {
26 	struct mcb_bus *mbus = mdev->bus;
27 	struct device *dev = mbus->carrier;
28 	struct pci_dev *pdev = to_pci_dev(dev);
29 
30 	return pdev->irq;
31 }
32 
33 static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
34 {
35 	struct resource *res;
36 	struct priv *priv;
37 	int ret;
38 	int num_cells;
39 	unsigned long flags;
40 
41 	priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
42 	if (!priv)
43 		return -ENOMEM;
44 
45 	ret = pci_enable_device(pdev);
46 	if (ret) {
47 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
48 		return -ENODEV;
49 	}
50 
51 	priv->mapbase = pci_resource_start(pdev, 0);
52 	if (!priv->mapbase) {
53 		dev_err(&pdev->dev, "No PCI resource\n");
54 		goto out_disable;
55 	}
56 
57 	res = request_mem_region(priv->mapbase, CHAM_HEADER_SIZE,
58 				 KBUILD_MODNAME);
59 	if (!res) {
60 		dev_err(&pdev->dev, "Failed to request PCI memory\n");
61 		ret = -EBUSY;
62 		goto out_disable;
63 	}
64 
65 	priv->base = ioremap(priv->mapbase, CHAM_HEADER_SIZE);
66 	if (!priv->base) {
67 		dev_err(&pdev->dev, "Cannot ioremap\n");
68 		ret = -ENOMEM;
69 		goto out_release;
70 	}
71 
72 	flags = pci_resource_flags(pdev, 0);
73 	if (flags & IORESOURCE_IO) {
74 		ret = -ENOTSUPP;
75 		dev_err(&pdev->dev,
76 			"IO mapped PCI devices are not supported\n");
77 		goto out_iounmap;
78 	}
79 
80 	pci_set_drvdata(pdev, priv);
81 
82 	priv->bus = mcb_alloc_bus(&pdev->dev);
83 	if (IS_ERR(priv->bus)) {
84 		ret = PTR_ERR(priv->bus);
85 		goto out_iounmap;
86 	}
87 
88 	priv->bus->get_irq = mcb_pci_get_irq;
89 
90 	ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
91 	if (ret < 0)
92 		goto out_mcb_bus;
93 	num_cells = ret;
94 
95 	dev_dbg(&pdev->dev, "Found %d cells\n", num_cells);
96 
97 	mcb_bus_add_devices(priv->bus);
98 
99 	return 0;
100 
101 out_mcb_bus:
102 	mcb_release_bus(priv->bus);
103 out_iounmap:
104 	iounmap(priv->base);
105 out_release:
106 	pci_release_region(pdev, 0);
107 out_disable:
108 	pci_disable_device(pdev);
109 	return ret;
110 }
111 
112 static void mcb_pci_remove(struct pci_dev *pdev)
113 {
114 	struct priv *priv = pci_get_drvdata(pdev);
115 
116 	mcb_release_bus(priv->bus);
117 
118 	iounmap(priv->base);
119 	release_region(priv->mapbase, CHAM_HEADER_SIZE);
120 	pci_disable_device(pdev);
121 }
122 
123 static const struct pci_device_id mcb_pci_tbl[] = {
124 	{ PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
125 	{ 0 },
126 };
127 MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
128 
129 static struct pci_driver mcb_pci_driver = {
130 	.name = "mcb-pci",
131 	.id_table = mcb_pci_tbl,
132 	.probe = mcb_pci_probe,
133 	.remove = mcb_pci_remove,
134 };
135 
136 module_pci_driver(mcb_pci_driver);
137 
138 MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
139 MODULE_LICENSE("GPL");
140 MODULE_DESCRIPTION("MCB over PCI support");
141