Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b71bb863 JT |
2 | /* |
3 | * MEN Chameleon Bus. | |
4 | * | |
5 | * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de) | |
6 | * Author: Johannes Thumshirn <johannes.thumshirn@men.de> | |
b71bb863 JT |
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; | |
7b7c5491 | 17 | phys_addr_t mapbase; |
b71bb863 JT |
18 | void __iomem *base; |
19 | }; | |
20 | ||
4ec65b77 JT |
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 | ||
b71bb863 JT |
30 | static int mcb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
31 | { | |
7b7c5491 | 32 | struct resource *res; |
b71bb863 | 33 | struct priv *priv; |
9be24faa | 34 | int ret, table_size; |
b71bb863 JT |
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 | } | |
15bb81d3 | 46 | pci_set_master(pdev); |
b71bb863 | 47 | |
7b7c5491 JT |
48 | priv->mapbase = pci_resource_start(pdev, 0); |
49 | if (!priv->mapbase) { | |
b71bb863 | 50 | dev_err(&pdev->dev, "No PCI resource\n"); |
bf25c199 | 51 | ret = -ENODEV; |
a48742bc | 52 | goto out_disable; |
b71bb863 JT |
53 | } |
54 | ||
d19366f1 AW |
55 | res = devm_request_mem_region(&pdev->dev, priv->mapbase, |
56 | CHAM_HEADER_SIZE, | |
57 | KBUILD_MODNAME); | |
d86fb45b | 58 | if (!res) { |
7b7c5491 | 59 | dev_err(&pdev->dev, "Failed to request PCI memory\n"); |
d86fb45b | 60 | ret = -EBUSY; |
a48742bc | 61 | goto out_disable; |
b71bb863 JT |
62 | } |
63 | ||
d19366f1 | 64 | priv->base = devm_ioremap(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE); |
b71bb863 JT |
65 | if (!priv->base) { |
66 | dev_err(&pdev->dev, "Cannot ioremap\n"); | |
67 | ret = -ENOMEM; | |
d19366f1 | 68 | goto out_disable; |
b71bb863 JT |
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"); | |
d19366f1 | 76 | goto out_disable; |
b71bb863 JT |
77 | } |
78 | ||
79 | pci_set_drvdata(pdev, priv); | |
80 | ||
4ec65b77 JT |
81 | priv->bus = mcb_alloc_bus(&pdev->dev); |
82 | if (IS_ERR(priv->bus)) { | |
83 | ret = PTR_ERR(priv->bus); | |
d19366f1 | 84 | goto out_disable; |
4ec65b77 JT |
85 | } |
86 | ||
87 | priv->bus->get_irq = mcb_pci_get_irq; | |
b71bb863 | 88 | |
7b7c5491 | 89 | ret = chameleon_parse_cells(priv->bus, priv->mapbase, priv->base); |
b71bb863 | 90 | if (ret < 0) |
41ada9df | 91 | goto out_mcb_bus; |
b71bb863 | 92 | |
9be24faa RBJJ |
93 | table_size = ret; |
94 | ||
95 | if (table_size < CHAM_HEADER_SIZE) { | |
96 | /* Release the previous resources */ | |
97 | devm_iounmap(&pdev->dev, priv->base); | |
98 | devm_release_mem_region(&pdev->dev, priv->mapbase, CHAM_HEADER_SIZE); | |
99 | ||
100 | /* Then, allocate it again with the actual chameleon table size */ | |
101 | res = devm_request_mem_region(&pdev->dev, priv->mapbase, | |
102 | table_size, | |
103 | KBUILD_MODNAME); | |
104 | if (!res) { | |
105 | dev_err(&pdev->dev, "Failed to request PCI memory\n"); | |
106 | ret = -EBUSY; | |
107 | goto out_mcb_bus; | |
108 | } | |
109 | ||
110 | priv->base = devm_ioremap(&pdev->dev, priv->mapbase, table_size); | |
111 | if (!priv->base) { | |
112 | dev_err(&pdev->dev, "Cannot ioremap\n"); | |
113 | ret = -ENOMEM; | |
114 | goto out_mcb_bus; | |
115 | } | |
116 | } | |
b71bb863 JT |
117 | |
118 | mcb_bus_add_devices(priv->bus); | |
119 | ||
7b7c5491 JT |
120 | return 0; |
121 | ||
41ada9df AK |
122 | out_mcb_bus: |
123 | mcb_release_bus(priv->bus); | |
a48742bc | 124 | out_disable: |
b71bb863 JT |
125 | pci_disable_device(pdev); |
126 | return ret; | |
127 | } | |
128 | ||
129 | static void mcb_pci_remove(struct pci_dev *pdev) | |
130 | { | |
131 | struct priv *priv = pci_get_drvdata(pdev); | |
132 | ||
133 | mcb_release_bus(priv->bus); | |
7b7c5491 | 134 | |
7b7c5491 | 135 | pci_disable_device(pdev); |
b71bb863 JT |
136 | } |
137 | ||
138 | static const struct pci_device_id mcb_pci_tbl[] = { | |
139 | { PCI_DEVICE(PCI_VENDOR_ID_MEN, PCI_DEVICE_ID_MEN_CHAMELEON) }, | |
29ea6be3 | 140 | { PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_MEN_CHAMELEON) }, |
b71bb863 JT |
141 | { 0 }, |
142 | }; | |
143 | MODULE_DEVICE_TABLE(pci, mcb_pci_tbl); | |
144 | ||
145 | static struct pci_driver mcb_pci_driver = { | |
146 | .name = "mcb-pci", | |
147 | .id_table = mcb_pci_tbl, | |
148 | .probe = mcb_pci_probe, | |
149 | .remove = mcb_pci_remove, | |
150 | }; | |
151 | ||
152 | module_pci_driver(mcb_pci_driver); | |
153 | ||
154 | MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>"); | |
155 | MODULE_LICENSE("GPL"); | |
156 | MODULE_DESCRIPTION("MCB over PCI support"); | |
891e6036 | 157 | MODULE_IMPORT_NS(MCB); |