xref: /linux/drivers/mcb/mcb-pci.c (revision 29ea6be366560f9dba60d0995ae18e54c8b6a04f)
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 	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 	}
4915bb81d3SMichael Moese 	pci_set_master(pdev);
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");
54bf25c199SAlexey Khoroshilov 		ret = -ENODEV;
55a48742bcSJohannes Thumshirn 		goto out_disable;
56b71bb863SJohannes Thumshirn 	}
57b71bb863SJohannes Thumshirn 
58d19366f1SAndreas Werner 	res = devm_request_mem_region(&pdev->dev, priv->mapbase,
59d19366f1SAndreas Werner 				      CHAM_HEADER_SIZE,
607b7c5491SJohannes Thumshirn 				      KBUILD_MODNAME);
61d86fb45bSDan Carpenter 	if (!res) {
627b7c5491SJohannes Thumshirn 		dev_err(&pdev->dev, "Failed to request PCI memory\n");
63d86fb45bSDan Carpenter 		ret = -EBUSY;
64a48742bcSJohannes Thumshirn 		goto out_disable;
65b71bb863SJohannes Thumshirn 	}
66b71bb863SJohannes Thumshirn 
67d19366f1SAndreas Werner 	priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
68b71bb863SJohannes Thumshirn 	if (!priv->base) {
69b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "Cannot ioremap\n");
70b71bb863SJohannes Thumshirn 		ret = -ENOMEM;
71d19366f1SAndreas Werner 		goto out_disable;
72b71bb863SJohannes Thumshirn 	}
73b71bb863SJohannes Thumshirn 
74b71bb863SJohannes Thumshirn 	flags = pci_resource_flags(pdev, 0);
75b71bb863SJohannes Thumshirn 	if (flags & IORESOURCE_IO) {
76b71bb863SJohannes Thumshirn 		ret = -ENOTSUPP;
77b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev,
78b71bb863SJohannes Thumshirn 			"IO mapped PCI devices are not supported\n");
79d19366f1SAndreas Werner 		goto out_disable;
80b71bb863SJohannes Thumshirn 	}
81b71bb863SJohannes Thumshirn 
82b71bb863SJohannes Thumshirn 	pci_set_drvdata(pdev, priv);
83b71bb863SJohannes Thumshirn 
844ec65b77SJohannes Thumshirn 	priv->bus = mcb_alloc_bus(&pdev->dev);
854ec65b77SJohannes Thumshirn 	if (IS_ERR(priv->bus)) {
864ec65b77SJohannes Thumshirn 		ret = PTR_ERR(priv->bus);
87d19366f1SAndreas Werner 		goto out_disable;
884ec65b77SJohannes Thumshirn 	}
894ec65b77SJohannes Thumshirn 
904ec65b77SJohannes Thumshirn 	priv->bus->get_irq = mcb_pci_get_irq;
91b71bb863SJohannes Thumshirn 
927b7c5491SJohannes Thumshirn 	ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
93b71bb863SJohannes Thumshirn 	if (ret < 0)
9441ada9dfSAlexey Khoroshilov 		goto out_mcb_bus;
95b71bb863SJohannes Thumshirn 
968cd1f9d0SAndreas Werner 	dev_dbg(&pdev->dev, "Found %d cells\n", ret);
97b71bb863SJohannes Thumshirn 
98b71bb863SJohannes Thumshirn 	mcb_bus_add_devices(priv->bus);
99b71bb863SJohannes Thumshirn 
1007b7c5491SJohannes Thumshirn 	return 0;
1017b7c5491SJohannes Thumshirn 
10241ada9dfSAlexey Khoroshilov out_mcb_bus:
10341ada9dfSAlexey Khoroshilov 	mcb_release_bus(priv->bus);
104a48742bcSJohannes Thumshirn out_disable:
105b71bb863SJohannes Thumshirn 	pci_disable_device(pdev);
106b71bb863SJohannes Thumshirn 	return ret;
107b71bb863SJohannes Thumshirn }
108b71bb863SJohannes Thumshirn 
109b71bb863SJohannes Thumshirn static void mcb_pci_remove(struct pci_dev *pdev)
110b71bb863SJohannes Thumshirn {
111b71bb863SJohannes Thumshirn 	struct priv *priv = pci_get_drvdata(pdev);
112b71bb863SJohannes Thumshirn 
113b71bb863SJohannes Thumshirn 	mcb_release_bus(priv->bus);
1147b7c5491SJohannes Thumshirn 
1157b7c5491SJohannes Thumshirn 	pci_disable_device(pdev);
116b71bb863SJohannes Thumshirn }
117b71bb863SJohannes Thumshirn 
118b71bb863SJohannes Thumshirn static const struct pci_device_id mcb_pci_tbl[] = {
119b71bb863SJohannes Thumshirn 	{ PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
120*29ea6be3SJohannes Thumshirn 	{ PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) },
121b71bb863SJohannes Thumshirn 	{ 0 },
122b71bb863SJohannes Thumshirn };
123b71bb863SJohannes Thumshirn MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
124b71bb863SJohannes Thumshirn 
125b71bb863SJohannes Thumshirn static struct pci_driver mcb_pci_driver = {
126b71bb863SJohannes Thumshirn 	.name = "mcb-pci",
127b71bb863SJohannes Thumshirn 	.id_table = mcb_pci_tbl,
128b71bb863SJohannes Thumshirn 	.probe = mcb_pci_probe,
129b71bb863SJohannes Thumshirn 	.remove = mcb_pci_remove,
130b71bb863SJohannes Thumshirn };
131b71bb863SJohannes Thumshirn 
132b71bb863SJohannes Thumshirn module_pci_driver(mcb_pci_driver);
133b71bb863SJohannes Thumshirn 
134b71bb863SJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
135b71bb863SJohannes Thumshirn MODULE_LICENSE("GPL");
136b71bb863SJohannes Thumshirn MODULE_DESCRIPTION("MCB over PCI support");
137