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