xref: /linux/drivers/mcb/mcb-pci.c (revision b886d83c5b621abc84ff9616f14c529be3f6b147)
1*b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b71bb863SJohannes Thumshirn /*
3b71bb863SJohannes Thumshirn  * MEN Chameleon Bus.
4b71bb863SJohannes Thumshirn  *
5b71bb863SJohannes Thumshirn  * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
6b71bb863SJohannes Thumshirn  * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
7b71bb863SJohannes Thumshirn  */
8b71bb863SJohannes Thumshirn 
9b71bb863SJohannes Thumshirn #include <linux/module.h>
10b71bb863SJohannes Thumshirn #include <linux/pci.h>
11b71bb863SJohannes Thumshirn #include <linux/mcb.h>
12b71bb863SJohannes Thumshirn 
13b71bb863SJohannes Thumshirn #include "mcb-internal.h"
14b71bb863SJohannes Thumshirn 
15b71bb863SJohannes Thumshirn struct priv {
16b71bb863SJohannes Thumshirn 	struct mcb_bus *bus;
177b7c5491SJohannes Thumshirn 	phys_addr_t mapbase;
18b71bb863SJohannes Thumshirn 	void __iomem *base;
19b71bb863SJohannes Thumshirn };
20b71bb863SJohannes Thumshirn 
214ec65b77SJohannes Thumshirn static int mcb_pci_get_irq(struct mcb_device *mdev)
224ec65b77SJohannes Thumshirn {
234ec65b77SJohannes Thumshirn 	struct mcb_bus *mbus = mdev->bus;
244ec65b77SJohannes Thumshirn 	struct device *dev = mbus->carrier;
254ec65b77SJohannes Thumshirn 	struct pci_dev *pdev = to_pci_dev(dev);
264ec65b77SJohannes Thumshirn 
274ec65b77SJohannes Thumshirn 	return pdev->irq;
284ec65b77SJohannes Thumshirn }
294ec65b77SJohannes Thumshirn 
30b71bb863SJohannes Thumshirn static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
31b71bb863SJohannes Thumshirn {
327b7c5491SJohannes Thumshirn 	struct resource *res;
33b71bb863SJohannes Thumshirn 	struct priv *priv;
34b71bb863SJohannes Thumshirn 	int ret;
35b71bb863SJohannes Thumshirn 	unsigned long flags;
36b71bb863SJohannes Thumshirn 
37b71bb863SJohannes Thumshirn 	priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
38b71bb863SJohannes Thumshirn 	if (!priv)
39b71bb863SJohannes Thumshirn 		return -ENOMEM;
40b71bb863SJohannes Thumshirn 
41b71bb863SJohannes Thumshirn 	ret = pci_enable_device(pdev);
42b71bb863SJohannes Thumshirn 	if (ret) {
43b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
44b71bb863SJohannes Thumshirn 		return -ENODEV;
45b71bb863SJohannes Thumshirn 	}
4615bb81d3SMichael Moese 	pci_set_master(pdev);
47b71bb863SJohannes Thumshirn 
487b7c5491SJohannes Thumshirn 	priv->mapbase = pci_resource_start(pdev, 0);
497b7c5491SJohannes Thumshirn 	if (!priv->mapbase) {
50b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "No PCI resource\n");
51bf25c199SAlexey Khoroshilov 		ret = -ENODEV;
52a48742bcSJohannes Thumshirn 		goto out_disable;
53b71bb863SJohannes Thumshirn 	}
54b71bb863SJohannes Thumshirn 
55d19366f1SAndreas Werner 	res = devm_request_mem_region(&pdev->dev, priv->mapbase,
56d19366f1SAndreas Werner 				      CHAM_HEADER_SIZE,
577b7c5491SJohannes Thumshirn 				      KBUILD_MODNAME);
58d86fb45bSDan Carpenter 	if (!res) {
597b7c5491SJohannes Thumshirn 		dev_err(&pdev->dev, "Failed to request PCI memory\n");
60d86fb45bSDan Carpenter 		ret = -EBUSY;
61a48742bcSJohannes Thumshirn 		goto out_disable;
62b71bb863SJohannes Thumshirn 	}
63b71bb863SJohannes Thumshirn 
64d19366f1SAndreas Werner 	priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
65b71bb863SJohannes Thumshirn 	if (!priv->base) {
66b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "Cannot ioremap\n");
67b71bb863SJohannes Thumshirn 		ret = -ENOMEM;
68d19366f1SAndreas Werner 		goto out_disable;
69b71bb863SJohannes Thumshirn 	}
70b71bb863SJohannes Thumshirn 
71b71bb863SJohannes Thumshirn 	flags = pci_resource_flags(pdev, 0);
72b71bb863SJohannes Thumshirn 	if (flags & IORESOURCE_IO) {
73b71bb863SJohannes Thumshirn 		ret = -ENOTSUPP;
74b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev,
75b71bb863SJohannes Thumshirn 			"IO mapped PCI devices are not supported\n");
76d19366f1SAndreas Werner 		goto out_disable;
77b71bb863SJohannes Thumshirn 	}
78b71bb863SJohannes Thumshirn 
79b71bb863SJohannes Thumshirn 	pci_set_drvdata(pdev, priv);
80b71bb863SJohannes Thumshirn 
814ec65b77SJohannes Thumshirn 	priv->bus = mcb_alloc_bus(&pdev->dev);
824ec65b77SJohannes Thumshirn 	if (IS_ERR(priv->bus)) {
834ec65b77SJohannes Thumshirn 		ret = PTR_ERR(priv->bus);
84d19366f1SAndreas Werner 		goto out_disable;
854ec65b77SJohannes Thumshirn 	}
864ec65b77SJohannes Thumshirn 
874ec65b77SJohannes Thumshirn 	priv->bus->get_irq = mcb_pci_get_irq;
88b71bb863SJohannes Thumshirn 
897b7c5491SJohannes Thumshirn 	ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
90b71bb863SJohannes Thumshirn 	if (ret < 0)
9141ada9dfSAlexey Khoroshilov 		goto out_mcb_bus;
92b71bb863SJohannes Thumshirn 
938cd1f9d0SAndreas Werner 	dev_dbg(&pdev->dev, "Found %d cells\n", ret);
94b71bb863SJohannes Thumshirn 
95b71bb863SJohannes Thumshirn 	mcb_bus_add_devices(priv->bus);
96b71bb863SJohannes Thumshirn 
977b7c5491SJohannes Thumshirn 	return 0;
987b7c5491SJohannes Thumshirn 
9941ada9dfSAlexey Khoroshilov out_mcb_bus:
10041ada9dfSAlexey Khoroshilov 	mcb_release_bus(priv->bus);
101a48742bcSJohannes Thumshirn out_disable:
102b71bb863SJohannes Thumshirn 	pci_disable_device(pdev);
103b71bb863SJohannes Thumshirn 	return ret;
104b71bb863SJohannes Thumshirn }
105b71bb863SJohannes Thumshirn 
106b71bb863SJohannes Thumshirn static void mcb_pci_remove(struct pci_dev *pdev)
107b71bb863SJohannes Thumshirn {
108b71bb863SJohannes Thumshirn 	struct priv *priv = pci_get_drvdata(pdev);
109b71bb863SJohannes Thumshirn 
110b71bb863SJohannes Thumshirn 	mcb_release_bus(priv->bus);
1117b7c5491SJohannes Thumshirn 
1127b7c5491SJohannes Thumshirn 	pci_disable_device(pdev);
113b71bb863SJohannes Thumshirn }
114b71bb863SJohannes Thumshirn 
115b71bb863SJohannes Thumshirn static const struct pci_device_id mcb_pci_tbl[] = {
116b71bb863SJohannes Thumshirn 	{ PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
11729ea6be3SJohannes Thumshirn 	{ PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) },
118b71bb863SJohannes Thumshirn 	{ 0 },
119b71bb863SJohannes Thumshirn };
120b71bb863SJohannes Thumshirn MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
121b71bb863SJohannes Thumshirn 
122b71bb863SJohannes Thumshirn static struct pci_driver mcb_pci_driver = {
123b71bb863SJohannes Thumshirn 	.name = "mcb-pci",
124b71bb863SJohannes Thumshirn 	.id_table = mcb_pci_tbl,
125b71bb863SJohannes Thumshirn 	.probe = mcb_pci_probe,
126b71bb863SJohannes Thumshirn 	.remove = mcb_pci_remove,
127b71bb863SJohannes Thumshirn };
128b71bb863SJohannes Thumshirn 
129b71bb863SJohannes Thumshirn module_pci_driver(mcb_pci_driver);
130b71bb863SJohannes Thumshirn 
131b71bb863SJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
132b71bb863SJohannes Thumshirn MODULE_LICENSE("GPL");
133b71bb863SJohannes Thumshirn MODULE_DESCRIPTION("MCB over PCI support");
134