blob: dc88232d9af83c1694d6e00d122d45cecc1eb45b [file] [log] [blame]
Thomas Gleixnerb886d832019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Johannes Thumshirnb71bb862014-02-26 17:29:06 +01002/*
3 * MEN Chameleon Bus.
4 *
5 * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
6 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
Johannes Thumshirnb71bb862014-02-26 17:29:06 +01007 */
8
9#include <linux/module.h>
10#include <linux/pci.h>
11#include <linux/mcb.h>
12
13#include "mcb-internal.h"
14
15struct priv {
16 struct mcb_bus *bus;
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010017 phys_addr_t mapbase;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010018 void __iomem *base;
19};
20
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +020021static int mcb_pci_get_irq(struct mcb_device *mdev)
22{
23 struct mcb_bus *mbus = mdev->bus;
24 struct device *dev = mbus->carrier;
25 struct pci_dev *pdev = to_pci_dev(dev);
26
27 return pdev->irq;
28}
29
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010030static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
31{
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010032 struct resource *res;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010033 struct priv *priv;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010034 int ret;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010035 unsigned long flags;
36
37 priv = devm_kzalloc(&pdev->dev, sizeof(struct priv), GFP_KERNEL);
38 if (!priv)
39 return -ENOMEM;
40
41 ret = pci_enable_device(pdev);
42 if (ret) {
43 dev_err(&pdev->dev, "Failed to enable PCI device\n");
44 return -ENODEV;
45 }
Michael Moese15bb81d2016-09-14 12:05:23 +020046 pci_set_master(pdev);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010047
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010048 priv->mapbase = pci_resource_start(pdev, 0);
49 if (!priv->mapbase) {
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010050 dev_err(&pdev->dev, "No PCI resource\n");
Alexey Khoroshilovbf25c192015-10-28 08:22:15 +010051 ret = -ENODEV;
Johannes Thumshirna48742b2015-01-12 16:26:32 +010052 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010053 }
54
Andreas Wernerd19366f2016-05-03 12:42:01 +020055 res = devm_request_mem_region(&pdev->dev, priv->mapbase,
56 CHAM_HEADER_SIZE,
57 KBUILD_MODNAME);
Dan Carpenterd86fb452015-03-26 22:12:49 +030058 if (!res) {
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010059 dev_err(&pdev->dev, "Failed to request PCI memory\n");
Dan Carpenterd86fb452015-03-26 22:12:49 +030060 ret = -EBUSY;
Johannes Thumshirna48742b2015-01-12 16:26:32 +010061 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010062 }
63
Andreas Wernerd19366f2016-05-03 12:42:01 +020064 priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010065 if (!priv->base) {
66 dev_err(&pdev->dev, "Cannot ioremap\n");
67 ret = -ENOMEM;
Andreas Wernerd19366f2016-05-03 12:42:01 +020068 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010069 }
70
71 flags = pci_resource_flags(pdev, 0);
72 if (flags & IORESOURCE_IO) {
73 ret = -ENOTSUPP;
74 dev_err(&pdev->dev,
75 "IO mapped PCI devices are not supported\n");
Andreas Wernerd19366f2016-05-03 12:42:01 +020076 goto out_disable;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010077 }
78
79 pci_set_drvdata(pdev, priv);
80
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +020081 priv->bus = mcb_alloc_bus(&pdev->dev);
82 if (IS_ERR(priv->bus)) {
83 ret = PTR_ERR(priv->bus);
Andreas Wernerd19366f2016-05-03 12:42:01 +020084 goto out_disable;
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +020085 }
86
87 priv->bus->get_irq = mcb_pci_get_irq;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010088
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010089 ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010090 if (ret < 0)
Alexey Khoroshilov41ada9d2015-08-24 10:18:38 +020091 goto out_mcb_bus;
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010092
Andreas Werner8cd1f9d2016-05-03 12:42:02 +020093 dev_dbg(&pdev->dev, "Found %d cells\n", ret);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +010094
95 mcb_bus_add_devices(priv->bus);
96
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +010097 return 0;
98
Alexey Khoroshilov41ada9d2015-08-24 10:18:38 +020099out_mcb_bus:
100 mcb_release_bus(priv->bus);
Johannes Thumshirna48742b2015-01-12 16:26:32 +0100101out_disable:
Johannes Thumshirnb71bb862014-02-26 17:29:06 +0100102 pci_disable_device(pdev);
103 return ret;
104}
105
106static void mcb_pci_remove(struct pci_dev *pdev)
107{
108 struct priv *priv = pci_get_drvdata(pdev);
109
110 mcb_release_bus(priv->bus);
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +0100111
Johannes Thumshirn7b7c5492014-12-16 10:09:20 +0100112 pci_disable_device(pdev);
Johannes Thumshirnb71bb862014-02-26 17:29:06 +0100113}
114
115static const struct pci_device_id mcb_pci_tbl[] = {
116 { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) },
Johannes Thumshirn29ea6be2018-03-12 10:41:19 +0100117 { PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) },
Johannes Thumshirnb71bb862014-02-26 17:29:06 +0100118 { 0 },
119};
120MODULE_DEVICE_TABLE(pci, mcb_pci_tbl);
121
122static struct pci_driver mcb_pci_driver = {
123 .name = "mcb-pci",
124 .id_table = mcb_pci_tbl,
125 .probe = mcb_pci_probe,
126 .remove = mcb_pci_remove,
127};
128
129module_pci_driver(mcb_pci_driver);
130
131MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
132MODULE_LICENSE("GPL");
133MODULE_DESCRIPTION("MCB over PCI support");
Johannes Thumshirn891e6032019-10-16 12:01:58 +0200134MODULE_IMPORT_NS(MCB);