xref: /linux/drivers/mcb/mcb-pci.c (revision d86fb45b5c52d8f2a3e78e81afd85a2a9d4478d7)
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;
207b7c5491SJohannes Thumshirn 	phys_addr_t mapbase;
21b71bb863SJohannes Thumshirn 	void __iomem *base;
22b71bb863SJohannes Thumshirn };
23b71bb863SJohannes Thumshirn 
244ec65b77SJohannes Thumshirn static int mcb_pci_get_irq(struct mcb_device *mdev)
254ec65b77SJohannes Thumshirn {
264ec65b77SJohannes Thumshirn 	struct mcb_bus *mbus = mdev->bus;
274ec65b77SJohannes Thumshirn 	struct device *dev = mbus->carrier;
284ec65b77SJohannes Thumshirn 	struct pci_dev *pdev = to_pci_dev(dev);
294ec65b77SJohannes Thumshirn 
304ec65b77SJohannes Thumshirn 	return pdev->irq;
314ec65b77SJohannes Thumshirn }
324ec65b77SJohannes Thumshirn 
33b71bb863SJohannes Thumshirn static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
34b71bb863SJohannes Thumshirn {
357b7c5491SJohannes Thumshirn 	struct resource *res;
36b71bb863SJohannes Thumshirn 	struct priv *priv;
37b71bb863SJohannes Thumshirn 	int ret;
38b71bb863SJohannes Thumshirn 	int num_cells;
39b71bb863SJohannes Thumshirn 	unsigned long flags;
40b71bb863SJohannes Thumshirn 
41b71bb863SJohannes Thumshirn 	priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
42b71bb863SJohannes Thumshirn 	if (!priv)
43b71bb863SJohannes Thumshirn 		return -ENOMEM;
44b71bb863SJohannes Thumshirn 
45b71bb863SJohannes Thumshirn 	ret = pci_enable_device(pdev);
46b71bb863SJohannes Thumshirn 	if (ret) {
47b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
48b71bb863SJohannes Thumshirn 		return -ENODEV;
49b71bb863SJohannes Thumshirn 	}
50b71bb863SJohannes Thumshirn 
517b7c5491SJohannes Thumshirn 	priv->mapbase = pci_resource_start(pdev, 0);
527b7c5491SJohannes Thumshirn 	if (!priv->mapbase) {
53b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "No PCI resource\n");
54a48742bcSJohannes Thumshirn 		goto out_disable;
55b71bb863SJohannes Thumshirn 	}
56b71bb863SJohannes Thumshirn 
577b7c5491SJohannes Thumshirn 	res = request_mem_region(priv->mapbase, CHAM_HEADER_SIZE,
587b7c5491SJohannes Thumshirn 				 KBUILD_MODNAME);
59*d86fb45bSDan Carpenter 	if (!res) {
607b7c5491SJohannes Thumshirn 		dev_err(&pdev->dev, "Failed to request PCI memory\n");
61*d86fb45bSDan Carpenter 		ret = -EBUSY;
62a48742bcSJohannes Thumshirn 		goto out_disable;
63b71bb863SJohannes Thumshirn 	}
64b71bb863SJohannes Thumshirn 
657b7c5491SJohannes Thumshirn 	priv->base = ioremap(priv->mapbase, CHAM_HEADER_SIZE);
66b71bb863SJohannes Thumshirn 	if (!priv->base) {
67b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "Cannot ioremap\n");
68b71bb863SJohannes Thumshirn 		ret = -ENOMEM;
69a48742bcSJohannes Thumshirn 		goto out_release;
70b71bb863SJohannes Thumshirn 	}
71b71bb863SJohannes Thumshirn 
72b71bb863SJohannes Thumshirn 	flags = pci_resource_flags(pdev, 0);
73b71bb863SJohannes Thumshirn 	if (flags & IORESOURCE_IO) {
74b71bb863SJohannes Thumshirn 		ret = -ENOTSUPP;
75b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev,
76b71bb863SJohannes Thumshirn 			"IO mapped PCI devices are not supported\n");
77a48742bcSJohannes Thumshirn 		goto out_release;
78b71bb863SJohannes Thumshirn 	}
79b71bb863SJohannes Thumshirn 
80b71bb863SJohannes Thumshirn 	pci_set_drvdata(pdev, priv);
81b71bb863SJohannes Thumshirn 
824ec65b77SJohannes Thumshirn 	priv->bus = mcb_alloc_bus(&pdev->dev);
834ec65b77SJohannes Thumshirn 	if (IS_ERR(priv->bus)) {
844ec65b77SJohannes Thumshirn 		ret = PTR_ERR(priv->bus);
85a48742bcSJohannes Thumshirn 		goto out_iounmap;
864ec65b77SJohannes Thumshirn 	}
874ec65b77SJohannes Thumshirn 
884ec65b77SJohannes Thumshirn 	priv->bus->get_irq = mcb_pci_get_irq;
89b71bb863SJohannes Thumshirn 
907b7c5491SJohannes Thumshirn 	ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
91b71bb863SJohannes Thumshirn 	if (ret < 0)
92a48742bcSJohannes Thumshirn 		goto out_iounmap;
93b71bb863SJohannes Thumshirn 	num_cells = ret;
94b71bb863SJohannes Thumshirn 
95b71bb863SJohannes Thumshirn 	dev_dbg(&pdev->dev, "Found %d cells\n", num_cells);
96b71bb863SJohannes Thumshirn 
97b71bb863SJohannes Thumshirn 	mcb_bus_add_devices(priv->bus);
98b71bb863SJohannes Thumshirn 
997b7c5491SJohannes Thumshirn 	return 0;
1007b7c5491SJohannes Thumshirn 
101a48742bcSJohannes Thumshirn out_iounmap:
1027b7c5491SJohannes Thumshirn 	iounmap(priv->base);
103a48742bcSJohannes Thumshirn out_release:
104b71bb863SJohannes Thumshirn 	pci_release_region(pdev, 0);
105a48742bcSJohannes Thumshirn out_disable:
106b71bb863SJohannes Thumshirn 	pci_disable_device(pdev);
107b71bb863SJohannes Thumshirn 	return ret;
108b71bb863SJohannes Thumshirn }
109b71bb863SJohannes Thumshirn 
110b71bb863SJohannes Thumshirn static void mcb_pci_remove(struct pci_dev *pdev)
111b71bb863SJohannes Thumshirn {
112b71bb863SJohannes Thumshirn 	struct priv *priv = pci_get_drvdata(pdev);
113b71bb863SJohannes Thumshirn 
114b71bb863SJohannes Thumshirn 	mcb_release_bus(priv->bus);
1157b7c5491SJohannes Thumshirn 
1167b7c5491SJohannes Thumshirn 	iounmap(priv->base);
1177b7c5491SJohannes Thumshirn 	release_region(priv->mapbase, CHAM_HEADER_SIZE);
1187b7c5491SJohannes Thumshirn 	pci_disable_device(pdev);
119b71bb863SJohannes Thumshirn }
120b71bb863SJohannes Thumshirn 
121b71bb863SJohannes Thumshirn static const struct pci_device_id mcb_pci_tbl[] = {
122b71bb863SJohannes Thumshirn 	{ PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
123b71bb863SJohannes Thumshirn 	{ 0 },
124b71bb863SJohannes Thumshirn };
125b71bb863SJohannes Thumshirn MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
126b71bb863SJohannes Thumshirn 
127b71bb863SJohannes Thumshirn static struct pci_driver mcb_pci_driver = {
128b71bb863SJohannes Thumshirn 	.name = "mcb-pci",
129b71bb863SJohannes Thumshirn 	.id_table = mcb_pci_tbl,
130b71bb863SJohannes Thumshirn 	.probe = mcb_pci_probe,
131b71bb863SJohannes Thumshirn 	.remove = mcb_pci_remove,
132b71bb863SJohannes Thumshirn };
133b71bb863SJohannes Thumshirn 
134b71bb863SJohannes Thumshirn module_pci_driver(mcb_pci_driver);
135b71bb863SJohannes Thumshirn 
136b71bb863SJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
137b71bb863SJohannes Thumshirn MODULE_LICENSE("GPL");
138b71bb863SJohannes Thumshirn MODULE_DESCRIPTION("MCB over PCI support");
139