Thomas Gleixner | b886d83 | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 2 | /* |
| 3 | * MEN Chameleon Bus. |
| 4 | * |
| 5 | * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de) |
| 6 | * Author: Johannes Thumshirn <johannes.thumshirn@men.de> |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/pci.h> |
| 11 | #include <linux/mcb.h> |
| 12 | |
| 13 | #include "mcb-internal.h" |
| 14 | |
| 15 | struct priv { |
| 16 | struct mcb_bus *bus; |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 17 | phys_addr_t mapbase; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 18 | void __iomem *base; |
| 19 | }; |
| 20 | |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 21 | static 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 Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 30 | static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 31 | { |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 32 | struct resource *res; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 33 | struct priv *priv; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 34 | int ret; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 35 | 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 Moese | 15bb81d | 2016-09-14 12:05:23 +0200 | [diff] [blame] | 46 | pci_set_master(pdev); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 47 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 48 | priv->mapbase = pci_resource_start(pdev, 0); |
| 49 | if (!priv->mapbase) { |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 50 | dev_err(&pdev->dev, "No PCI resource\n"); |
Alexey Khoroshilov | bf25c19 | 2015-10-28 08:22:15 +0100 | [diff] [blame] | 51 | ret = -ENODEV; |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 52 | goto out_disable; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Andreas Werner | d19366f | 2016-05-03 12:42:01 +0200 | [diff] [blame] | 55 | res = devm_request_mem_region(&pdev->dev, priv->mapbase, |
| 56 | CHAM_HEADER_SIZE, |
| 57 | KBUILD_MODNAME); |
Dan Carpenter | d86fb45 | 2015-03-26 22:12:49 +0300 | [diff] [blame] | 58 | if (!res) { |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 59 | dev_err(&pdev->dev, "Failed to request PCI memory\n"); |
Dan Carpenter | d86fb45 | 2015-03-26 22:12:49 +0300 | [diff] [blame] | 60 | ret = -EBUSY; |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 61 | goto out_disable; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Andreas Werner | d19366f | 2016-05-03 12:42:01 +0200 | [diff] [blame] | 64 | priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 65 | if (!priv->base) { |
| 66 | dev_err(&pdev->dev, "Cannot ioremap\n"); |
| 67 | ret = -ENOMEM; |
Andreas Werner | d19366f | 2016-05-03 12:42:01 +0200 | [diff] [blame] | 68 | goto out_disable; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 69 | } |
| 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 Werner | d19366f | 2016-05-03 12:42:01 +0200 | [diff] [blame] | 76 | goto out_disable; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | pci_set_drvdata(pdev, priv); |
| 80 | |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 81 | priv->bus = mcb_alloc_bus(&pdev->dev); |
| 82 | if (IS_ERR(priv->bus)) { |
| 83 | ret = PTR_ERR(priv->bus); |
Andreas Werner | d19366f | 2016-05-03 12:42:01 +0200 | [diff] [blame] | 84 | goto out_disable; |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | priv->bus->get_irq = mcb_pci_get_irq; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 88 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 89 | ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 90 | if (ret < 0) |
Alexey Khoroshilov | 41ada9d | 2015-08-24 10:18:38 +0200 | [diff] [blame] | 91 | goto out_mcb_bus; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 92 | |
Andreas Werner | 8cd1f9d | 2016-05-03 12:42:02 +0200 | [diff] [blame] | 93 | dev_dbg(&pdev->dev, "Found %d cells\n", ret); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 94 | |
| 95 | mcb_bus_add_devices(priv->bus); |
| 96 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 97 | return 0; |
| 98 | |
Alexey Khoroshilov | 41ada9d | 2015-08-24 10:18:38 +0200 | [diff] [blame] | 99 | out_mcb_bus: |
| 100 | mcb_release_bus(priv->bus); |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 101 | out_disable: |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 102 | pci_disable_device(pdev); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | static 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 Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 111 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 112 | pci_disable_device(pdev); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static const struct pci_device_id mcb_pci_tbl[] = { |
| 116 | { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) }, |
Johannes Thumshirn | 29ea6be | 2018-03-12 10:41:19 +0100 | [diff] [blame] | 117 | { PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) }, |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 118 | { 0 }, |
| 119 | }; |
| 120 | MODULE_DEVICE_TABLE(pci, mcb_pci_tbl); |
| 121 | |
| 122 | static 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 | |
| 129 | module_pci_driver(mcb_pci_driver); |
| 130 | |
| 131 | MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>"); |
| 132 | MODULE_LICENSE("GPL"); |
| 133 | MODULE_DESCRIPTION("MCB over PCI support"); |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 134 | MODULE_IMPORT_NS(MCB); |