Commit | Line | Data |
---|---|---|
b71bb863 JT |
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; | |
7b7c5491 | 20 | phys_addr_t mapbase; |
b71bb863 JT |
21 | void __iomem *base; |
22 | }; | |
23 | ||
4ec65b77 JT |
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 | ||
b71bb863 JT |
33 | static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
34 | { | |
7b7c5491 | 35 | struct resource *res; |
b71bb863 | 36 | struct priv *priv; |
b71bb863 | 37 | int ret; |
b71bb863 JT |
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 | ||
7b7c5491 JT |
50 | priv->mapbase = pci_resource_start(pdev, 0); |
51 | if (!priv->mapbase) { | |
b71bb863 | 52 | dev_err(&pdev->dev, "No PCI resource\n"); |
bf25c199 | 53 | ret = -ENODEV; |
a48742bc | 54 | goto out_disable; |
b71bb863 JT |
55 | } |
56 | ||
d19366f1 AW |
57 | res = devm_request_mem_region(&pdev->dev, priv->mapbase, |
58 | CHAM_HEADER_SIZE, | |
59 | KBUILD_MODNAME); | |
d86fb45b | 60 | if (!res) { |
7b7c5491 | 61 | dev_err(&pdev->dev, "Failed to request PCI memory\n"); |
d86fb45b | 62 | ret = -EBUSY; |
a48742bc | 63 | goto out_disable; |
b71bb863 JT |
64 | } |
65 | ||
d19366f1 | 66 | priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE); |
b71bb863 JT |
67 | if (!priv->base) { |
68 | dev_err(&pdev->dev, "Cannot ioremap\n"); | |
69 | ret = -ENOMEM; | |
d19366f1 | 70 | goto out_disable; |
b71bb863 JT |
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"); | |
d19366f1 | 78 | goto out_disable; |
b71bb863 JT |
79 | } |
80 | ||
81 | pci_set_drvdata(pdev, priv); | |
82 | ||
4ec65b77 JT |
83 | priv->bus = mcb_alloc_bus(&pdev->dev); |
84 | if (IS_ERR(priv->bus)) { | |
85 | ret = PTR_ERR(priv->bus); | |
d19366f1 | 86 | goto out_disable; |
4ec65b77 JT |
87 | } |
88 | ||
89 | priv->bus->get_irq = mcb_pci_get_irq; | |
b71bb863 | 90 | |
7b7c5491 | 91 | ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base); |
b71bb863 | 92 | if (ret < 0) |
41ada9df | 93 | goto out_mcb_bus; |
b71bb863 | 94 | |
8cd1f9d0 | 95 | dev_dbg(&pdev->dev, "Found %d cells\n", ret); |
b71bb863 JT |
96 | |
97 | mcb_bus_add_devices(priv->bus); | |
98 | ||
7b7c5491 JT |
99 | return 0; |
100 | ||
41ada9df AK |
101 | out_mcb_bus: |
102 | mcb_release_bus(priv->bus); | |
a48742bc | 103 | out_disable: |
b71bb863 JT |
104 | pci_disable_device(pdev); |
105 | return ret; | |
106 | } | |
107 | ||
108 | static void mcb_pci_remove(struct pci_dev *pdev) | |
109 | { | |
110 | struct priv *priv = pci_get_drvdata(pdev); | |
111 | ||
112 | mcb_release_bus(priv->bus); | |
7b7c5491 | 113 | |
7b7c5491 | 114 | pci_disable_device(pdev); |
b71bb863 JT |
115 | } |
116 | ||
117 | static const struct pci_device_id mcb_pci_tbl[] = { | |
118 | { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) }, | |
119 | { 0 }, | |
120 | }; | |
121 | MODULE_DEVICE_TABLE(pci, mcb_pci_tbl); | |
122 | ||
123 | static struct pci_driver mcb_pci_driver = { | |
124 | .name = "mcb-pci", | |
125 | .id_table = mcb_pci_tbl, | |
126 | .probe = mcb_pci_probe, | |
127 | .remove = mcb_pci_remove, | |
128 | }; | |
129 | ||
130 | module_pci_driver(mcb_pci_driver); | |
131 | ||
132 | MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>"); | |
133 | MODULE_LICENSE("GPL"); | |
134 | MODULE_DESCRIPTION("MCB over PCI support"); |