GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / mcb / mcb-pci.c
1 /*
2  * MEN Chameleon Bus.
3  *
4  * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
5  * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; version 2 of the License.
10  */
11
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/mcb.h>
15
16 #include "mcb-internal.h"
17
18 struct priv {
19         struct mcb_bus *bus;
20         phys_addr_t mapbase;
21         void __iomem *base;
22 };
23
24 static int mcb_pci_get_irq(struct mcb_device *mdev)
25 {
26         struct mcb_bus *mbus = mdev->bus;
27         struct device *dev = mbus->carrier;
28         struct pci_dev *pdev = to_pci_dev(dev);
29
30         return pdev->irq;
31 }
32
33 static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
34 {
35         struct resource *res;
36         struct priv *priv;
37         int ret, table_size;
38         unsigned long flags;
39
40         priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
41         if (!priv)
42                 return -ENOMEM;
43
44         ret = pci_enable_device(pdev);
45         if (ret) {
46                 dev_err(&pdev->dev, "Failed to enable PCI device\n");
47                 return -ENODEV;
48         }
49         pci_set_master(pdev);
50
51         priv->mapbase = pci_resource_start(pdev, 0);
52         if (!priv->mapbase) {
53                 dev_err(&pdev->dev, "No PCI resource\n");
54                 ret = -ENODEV;
55                 goto out_disable;
56         }
57
58         res = devm_request_mem_region(&pdev->dev, priv->mapbase,
59                                       CHAM_HEADER_SIZE,
60                                       KBUILD_MODNAME);
61         if (!res) {
62                 dev_err(&pdev->dev, "Failed to request PCI memory\n");
63                 ret = -EBUSY;
64                 goto out_disable;
65         }
66
67         priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
68         if (!priv->base) {
69                 dev_err(&pdev->dev, "Cannot ioremap\n");
70                 ret = -ENOMEM;
71                 goto out_disable;
72         }
73
74         flags = pci_resource_flags(pdev, 0);
75         if (flags & IORESOURCE_IO) {
76                 ret = -ENOTSUPP;
77                 dev_err(&pdev->dev,
78                         "IO mapped PCI devices are not supported\n");
79                 goto out_disable;
80         }
81
82         pci_set_drvdata(pdev, priv);
83
84         priv->bus = mcb_alloc_bus(&pdev->dev);
85         if (IS_ERR(priv->bus)) {
86                 ret = PTR_ERR(priv->bus);
87                 goto out_disable;
88         }
89
90         priv->bus->get_irq = mcb_pci_get_irq;
91
92         ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
93         if (ret < 0)
94                 goto out_mcb_bus;
95
96         table_size = ret;
97
98         if (table_size < CHAM_HEADER_SIZE) {
99                 /* Release the previous resources */
100                 devm_iounmap(&pdev->dev, priv->base);
101                 devm_release_mem_region(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
102
103                 /* Then, allocate it again with the actual chameleon table size */
104                 res = devm_request_mem_region(&pdev->dev, priv->mapbase,
105                                                 table_size,
106                                                 KBUILD_MODNAME);
107                 if (!res) {
108                         dev_err(&pdev->dev, "Failed to request PCI memory\n");
109                         ret = -EBUSY;
110                         goto out_mcb_bus;
111                 }
112
113                 priv->base = devm_ioremap(&pdev->dev, priv->mapbase, table_size);
114                 if (!priv->base) {
115                         dev_err(&pdev->dev, "Cannot ioremap\n");
116                         ret = -ENOMEM;
117                         goto out_mcb_bus;
118                 }
119         }
120
121         mcb_bus_add_devices(priv->bus);
122
123         return 0;
124
125 out_mcb_bus:
126         mcb_release_bus(priv->bus);
127 out_disable:
128         pci_disable_device(pdev);
129         return ret;
130 }
131
132 static void mcb_pci_remove(struct pci_dev *pdev)
133 {
134         struct priv *priv = pci_get_drvdata(pdev);
135
136         mcb_release_bus(priv->bus);
137
138         pci_disable_device(pdev);
139 }
140
141 static const struct pci_device_id mcb_pci_tbl[] = {
142         { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
143         { PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) },
144         { 0 },
145 };
146 MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
147
148 static struct pci_driver mcb_pci_driver = {
149         .name = "mcb-pci",
150         .id_table = mcb_pci_tbl,
151         .probe = mcb_pci_probe,
152         .remove = mcb_pci_remove,
153 };
154
155 module_pci_driver(mcb_pci_driver);
156
157 MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
158 MODULE_LICENSE("GPL");
159 MODULE_DESCRIPTION("MCB over PCI support");