blob: 99dd9db90a80bb250379d2c08902c2efcc7349c2 [file] [log] [blame]
Johannes Thumshirnb71bb862014-02-26 17:29:06 +01001/*
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
18struct priv {
19 struct mcb_bus *bus;
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010020 phys_addr_t mapbase;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010021 void __iomem *base;
22};
23
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +020024static 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
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010033static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
34{
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010035 struct resource *res;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010036 struct priv *priv;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010037 int ret;
38 int num_cells;
39 unsigned long flags;
40
41 priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
42 if (!priv)
43 return -ENOMEM;
44
45 ret = pci_enable_device(pdev);
46 if (ret) {
47 dev_err(&pdev->dev, "Failed to enable PCI device\n");
48 return -ENODEV;
49 }
50
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010051 priv->mapbase = pci_resource_start(pdev, 0);
52 if (!priv->mapbase) {
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010053 dev_err(&pdev->dev, "No PCI resource\n");
Alexey Khoroshilovbf25c192015-10-28 08:22:15 +010054 ret = -ENODEV;
Johannes Thumshirna48742b2015-01-12 16:26:32 +010055 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010056 }
57
Andreas Wernerd19366f2016-05-03 12:42:01 +020058 res = devm_request_mem_region(&pdev->dev, priv->mapbase,
59 CHAM_HEADER_SIZE,
60 KBUILD_MODNAME);
Dan Carpenterd86fb452015-03-26 22:12:49 +030061 if (!res) {
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010062 dev_err(&pdev->dev, "Failed to request PCI memory\n");
Dan Carpenterd86fb452015-03-26 22:12:49 +030063 ret = -EBUSY;
Johannes Thumshirna48742b2015-01-12 16:26:32 +010064 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010065 }
66
Andreas Wernerd19366f2016-05-03 12:42:01 +020067 priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010068 if (!priv->base) {
69 dev_err(&pdev->dev, "Cannot ioremap\n");
70 ret = -ENOMEM;
Andreas Wernerd19366f2016-05-03 12:42:01 +020071 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010072 }
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");
Andreas Wernerd19366f2016-05-03 12:42:01 +020079 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010080 }
81
82 pci_set_drvdata(pdev, priv);
83
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +020084 priv->bus = mcb_alloc_bus(&pdev->dev);
85 if (IS_ERR(priv->bus)) {
86 ret = PTR_ERR(priv->bus);
Andreas Wernerd19366f2016-05-03 12:42:01 +020087 goto out_disable;
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +020088 }
89
90 priv->bus->get_irq = mcb_pci_get_irq;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010091
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010092 ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010093 if (ret < 0)
Alexey Khoroshilov41ada9d2015-08-24 10:18:38 +020094 goto out_mcb_bus;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010095 num_cells = ret;
96
97 dev_dbg(&pdev->dev, "Found %d cells\n", num_cells);
98
99 mcb_bus_add_devices(priv->bus);
100
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +0100101 return 0;
102
Alexey Khoroshilov41ada9d2015-08-24 10:18:38 +0200103out_mcb_bus:
104 mcb_release_bus(priv->bus);
Johannes Thumshirna48742b2015-01-12 16:26:32 +0100105out_disable:
Johannes Thumshirnb71bb862014-02-26 17:29:06 +0100106 pci_disable_device(pdev);
107 return ret;
108}
109
110static void mcb_pci_remove(struct pci_dev *pdev)
111{
112 struct priv *priv = pci_get_drvdata(pdev);
113
114 mcb_release_bus(priv->bus);
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +0100115
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +0100116 pci_disable_device(pdev);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +0100117}
118
119static const struct pci_device_id mcb_pci_tbl[] = {
120 { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
121 { 0 },
122};
123MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
124
125static struct pci_driver mcb_pci_driver = {
126 .name = "mcb-pci",
127 .id_table = mcb_pci_tbl,
128 .probe = mcb_pci_probe,
129 .remove = mcb_pci_remove,
130};
131
132module_pci_driver(mcb_pci_driver);
133
134MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
135MODULE_LICENSE("GPL");
136MODULE_DESCRIPTION("MCB over PCI support");