Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 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; |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 20 | phys_addr_t mapbase; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 21 | void __iomem *base; |
| 22 | }; |
| 23 | |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 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 | |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 33 | static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 34 | { |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 35 | struct resource *res; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 36 | struct priv *priv; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 37 | 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 Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 51 | priv->mapbase = pci_resource_start(pdev, 0); |
| 52 | if (!priv->mapbase) { |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 53 | dev_err(&pdev->dev, "No PCI resource\n"); |
Alexey Khoroshilov | bf25c19 | 2015-10-28 08:22:15 +0100 | [diff] [blame] | 54 | ret = -ENODEV; |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 55 | goto out_disable; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 58 | res = request_mem_region(priv->mapbase, CHAM_HEADER_SIZE, |
| 59 | KBUILD_MODNAME); |
Dan Carpenter | d86fb45 | 2015-03-26 22:12:49 +0300 | [diff] [blame] | 60 | if (!res) { |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 61 | dev_err(&pdev->dev, "Failed to request PCI memory\n"); |
Dan Carpenter | d86fb45 | 2015-03-26 22:12:49 +0300 | [diff] [blame] | 62 | ret = -EBUSY; |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 63 | goto out_disable; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 66 | priv->base = ioremap(priv->mapbase, CHAM_HEADER_SIZE); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 67 | if (!priv->base) { |
| 68 | dev_err(&pdev->dev, "Cannot ioremap\n"); |
| 69 | ret = -ENOMEM; |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 70 | goto out_release; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | flags = pci_resource_flags(pdev, 0); |
| 74 | if (flags & IORESOURCE_IO) { |
| 75 | ret = -ENOTSUPP; |
| 76 | dev_err(&pdev->dev, |
| 77 | "IO mapped PCI devices are not supported\n"); |
Alexey Khoroshilov | 41ada9d | 2015-08-24 10:18:38 +0200 | [diff] [blame] | 78 | goto out_iounmap; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | pci_set_drvdata(pdev, priv); |
| 82 | |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 83 | priv->bus = mcb_alloc_bus(&pdev->dev); |
| 84 | if (IS_ERR(priv->bus)) { |
| 85 | ret = PTR_ERR(priv->bus); |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 86 | goto out_iounmap; |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | priv->bus->get_irq = mcb_pci_get_irq; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 90 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 91 | ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 92 | if (ret < 0) |
Alexey Khoroshilov | 41ada9d | 2015-08-24 10:18:38 +0200 | [diff] [blame] | 93 | goto out_mcb_bus; |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 94 | num_cells = ret; |
| 95 | |
| 96 | dev_dbg(&pdev->dev, "Found %d cells\n", num_cells); |
| 97 | |
| 98 | mcb_bus_add_devices(priv->bus); |
| 99 | |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 100 | return 0; |
| 101 | |
Alexey Khoroshilov | 41ada9d | 2015-08-24 10:18:38 +0200 | [diff] [blame] | 102 | out_mcb_bus: |
| 103 | mcb_release_bus(priv->bus); |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 104 | out_iounmap: |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 105 | iounmap(priv->base); |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 106 | out_release: |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 107 | pci_release_region(pdev, 0); |
Johannes Thumshirn | a48742b | 2015-01-12 16:26:32 +0100 | [diff] [blame] | 108 | out_disable: |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 109 | pci_disable_device(pdev); |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | static void mcb_pci_remove(struct pci_dev *pdev) |
| 114 | { |
| 115 | struct priv *priv = pci_get_drvdata(pdev); |
| 116 | |
| 117 | mcb_release_bus(priv->bus); |
Johannes Thumshirn | 7b7c549 | 2014-12-16 10:09:20 +0100 | [diff] [blame] | 118 | |
| 119 | iounmap(priv->base); |
| 120 | release_region(priv->mapbase, CHAM_HEADER_SIZE); |
| 121 | pci_disable_device(pdev); |
Johannes Thumshirn | b71bb86 | 2014-02-26 17:29:06 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static const struct pci_device_id mcb_pci_tbl[] = { |
| 125 | { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) }, |
| 126 | { 0 }, |
| 127 | }; |
| 128 | MODULE_DEVICE_TABLE(pci, mcb_pci_tbl); |
| 129 | |
| 130 | static struct pci_driver mcb_pci_driver = { |
| 131 | .name = "mcb-pci", |
| 132 | .id_table = mcb_pci_tbl, |
| 133 | .probe = mcb_pci_probe, |
| 134 | .remove = mcb_pci_remove, |
| 135 | }; |
| 136 | |
| 137 | module_pci_driver(mcb_pci_driver); |
| 138 | |
| 139 | MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>"); |
| 140 | MODULE_LICENSE("GPL"); |
| 141 | MODULE_DESCRIPTION("MCB over PCI support"); |