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