xref: /linux/drivers/mcb/mcb-pci.c (revision bf25c19979b2ee1019201b809e93b6bca9947cfd)
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");
54*bf25c199SAlexey Khoroshilov 		ret = -ENODEV;
55a48742bcSJohannes Thumshirn 		goto out_disable;
56b71bb863SJohannes Thumshirn 	}
57b71bb863SJohannes Thumshirn 
587b7c5491SJohannes Thumshirn 	res = request_mem_region(priv->mapbase, CHAM_HEADER_SIZE,
597b7c5491SJohannes Thumshirn 				 KBUILD_MODNAME);
60d86fb45bSDan Carpenter 	if (!res) {
617b7c5491SJohannes Thumshirn 		dev_err(&pdev->dev, "Failed to request PCI memory\n");
62d86fb45bSDan Carpenter 		ret = -EBUSY;
63a48742bcSJohannes Thumshirn 		goto out_disable;
64b71bb863SJohannes Thumshirn 	}
65b71bb863SJohannes Thumshirn 
667b7c5491SJohannes Thumshirn 	priv->base = ioremap(priv->mapbase, CHAM_HEADER_SIZE);
67b71bb863SJohannes Thumshirn 	if (!priv->base) {
68b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev, "Cannot ioremap\n");
69b71bb863SJohannes Thumshirn 		ret = -ENOMEM;
70a48742bcSJohannes Thumshirn 		goto out_release;
71b71bb863SJohannes Thumshirn 	}
72b71bb863SJohannes Thumshirn 
73b71bb863SJohannes Thumshirn 	flags = pci_resource_flags(pdev, 0);
74b71bb863SJohannes Thumshirn 	if (flags & IORESOURCE_IO) {
75b71bb863SJohannes Thumshirn 		ret = -ENOTSUPP;
76b71bb863SJohannes Thumshirn 		dev_err(&pdev->dev,
77b71bb863SJohannes Thumshirn 			"IO mapped PCI devices are not supported\n");
7841ada9dfSAlexey Khoroshilov 		goto out_iounmap;
79b71bb863SJohannes Thumshirn 	}
80b71bb863SJohannes Thumshirn 
81b71bb863SJohannes Thumshirn 	pci_set_drvdata(pdev, priv);
82b71bb863SJohannes Thumshirn 
834ec65b77SJohannes Thumshirn 	priv->bus = mcb_alloc_bus(&pdev->dev);
844ec65b77SJohannes Thumshirn 	if (IS_ERR(priv->bus)) {
854ec65b77SJohannes Thumshirn 		ret = PTR_ERR(priv->bus);
86a48742bcSJohannes Thumshirn 		goto out_iounmap;
874ec65b77SJohannes Thumshirn 	}
884ec65b77SJohannes Thumshirn 
894ec65b77SJohannes Thumshirn 	priv->bus->get_irq = mcb_pci_get_irq;
90b71bb863SJohannes Thumshirn 
917b7c5491SJohannes Thumshirn 	ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
92b71bb863SJohannes Thumshirn 	if (ret < 0)
9341ada9dfSAlexey Khoroshilov 		goto out_mcb_bus;
94b71bb863SJohannes Thumshirn 	num_cells = ret;
95b71bb863SJohannes Thumshirn 
96b71bb863SJohannes Thumshirn 	dev_dbg(&pdev->dev, "Found %d cells\n", num_cells);
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_iounmap:
1057b7c5491SJohannes Thumshirn 	iounmap(priv->base);
106a48742bcSJohannes Thumshirn out_release:
107b71bb863SJohannes Thumshirn 	pci_release_region(pdev, 0);
108a48742bcSJohannes Thumshirn out_disable:
109b71bb863SJohannes Thumshirn 	pci_disable_device(pdev);
110b71bb863SJohannes Thumshirn 	return ret;
111b71bb863SJohannes Thumshirn }
112b71bb863SJohannes Thumshirn 
113b71bb863SJohannes Thumshirn static void mcb_pci_remove(struct pci_dev *pdev)
114b71bb863SJohannes Thumshirn {
115b71bb863SJohannes Thumshirn 	struct priv *priv = pci_get_drvdata(pdev);
116b71bb863SJohannes Thumshirn 
117b71bb863SJohannes Thumshirn 	mcb_release_bus(priv->bus);
1187b7c5491SJohannes Thumshirn 
1197b7c5491SJohannes Thumshirn 	iounmap(priv->base);
1207b7c5491SJohannes Thumshirn 	release_region(priv->mapbase, CHAM_HEADER_SIZE);
1217b7c5491SJohannes Thumshirn 	pci_disable_device(pdev);
122b71bb863SJohannes Thumshirn }
123b71bb863SJohannes Thumshirn 
124b71bb863SJohannes Thumshirn static const struct pci_device_id mcb_pci_tbl[] = {
125b71bb863SJohannes Thumshirn 	{ PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
126b71bb863SJohannes Thumshirn 	{ 0 },
127b71bb863SJohannes Thumshirn };
128b71bb863SJohannes Thumshirn MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
129b71bb863SJohannes Thumshirn 
130b71bb863SJohannes Thumshirn static struct pci_driver mcb_pci_driver = {
131b71bb863SJohannes Thumshirn 	.name = "mcb-pci",
132b71bb863SJohannes Thumshirn 	.id_table = mcb_pci_tbl,
133b71bb863SJohannes Thumshirn 	.probe = mcb_pci_probe,
134b71bb863SJohannes Thumshirn 	.remove = mcb_pci_remove,
135b71bb863SJohannes Thumshirn };
136b71bb863SJohannes Thumshirn 
137b71bb863SJohannes Thumshirn module_pci_driver(mcb_pci_driver);
138b71bb863SJohannes Thumshirn 
139b71bb863SJohannes Thumshirn MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
140b71bb863SJohannes Thumshirn MODULE_LICENSE("GPL");
141b71bb863SJohannes Thumshirn MODULE_DESCRIPTION("MCB over PCI support");
142