blob: e23f7383ac33c25055edbb591ada8d4ff53302f9 [file] [log] [blame]
Marc Gonzalezd76bdce2017-09-26 12:27:39 +02001#include <linux/irqchip/chained_irq.h>
2#include <linux/irqdomain.h>
Marc Gonzalez5e14e9f2017-06-20 10:17:40 +02003#include <linux/pci-ecam.h>
4#include <linux/delay.h>
Marc Gonzalezd76bdce2017-09-26 12:27:39 +02005#include <linux/msi.h>
6#include <linux/of_address.h>
7
8#define MSI_MAX 256
Marc Gonzalez5e14e9f2017-06-20 10:17:40 +02009
10#define SMP8759_MUX 0x48
11#define SMP8759_TEST_OUT 0x74
Marc Gonzalezd76bdce2017-09-26 12:27:39 +020012#define SMP8759_DOORBELL 0x7c
13#define SMP8759_STATUS 0x80
14#define SMP8759_ENABLE 0xa0
Marc Gonzalez5e14e9f2017-06-20 10:17:40 +020015
16struct tango_pcie {
Marc Gonzalezd76bdce2017-09-26 12:27:39 +020017 DECLARE_BITMAP(used_msi, MSI_MAX);
18 u64 msi_doorbell;
19 spinlock_t used_msi_lock;
20 void __iomem *base;
21 struct irq_domain *dom;
22};
23
24static void tango_msi_isr(struct irq_desc *desc)
25{
26 struct irq_chip *chip = irq_desc_get_chip(desc);
27 struct tango_pcie *pcie = irq_desc_get_handler_data(desc);
28 unsigned long status, base, virq, idx, pos = 0;
29
30 chained_irq_enter(chip, desc);
31 spin_lock(&pcie->used_msi_lock);
32
33 while ((pos = find_next_bit(pcie->used_msi, MSI_MAX, pos)) < MSI_MAX) {
34 base = round_down(pos, 32);
35 status = readl_relaxed(pcie->base + SMP8759_STATUS + base / 8);
36 for_each_set_bit(idx, &status, 32) {
37 virq = irq_find_mapping(pcie->dom, base + idx);
38 generic_handle_irq(virq);
39 }
40 pos = base + 32;
41 }
42
43 spin_unlock(&pcie->used_msi_lock);
44 chained_irq_exit(chip, desc);
45}
46
47static void tango_ack(struct irq_data *d)
48{
49 struct tango_pcie *pcie = d->chip_data;
50 u32 offset = (d->hwirq / 32) * 4;
51 u32 bit = BIT(d->hwirq % 32);
52
53 writel_relaxed(bit, pcie->base + SMP8759_STATUS + offset);
54}
55
56static void update_msi_enable(struct irq_data *d, bool unmask)
57{
58 unsigned long flags;
59 struct tango_pcie *pcie = d->chip_data;
60 u32 offset = (d->hwirq / 32) * 4;
61 u32 bit = BIT(d->hwirq % 32);
62 u32 val;
63
64 spin_lock_irqsave(&pcie->used_msi_lock, flags);
65 val = readl_relaxed(pcie->base + SMP8759_ENABLE + offset);
66 val = unmask ? val | bit : val & ~bit;
67 writel_relaxed(val, pcie->base + SMP8759_ENABLE + offset);
68 spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
69}
70
71static void tango_mask(struct irq_data *d)
72{
73 update_msi_enable(d, false);
74}
75
76static void tango_unmask(struct irq_data *d)
77{
78 update_msi_enable(d, true);
79}
80
81static int tango_set_affinity(struct irq_data *d, const struct cpumask *mask,
82 bool force)
83{
84 return -EINVAL;
85}
86
87static void tango_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
88{
89 struct tango_pcie *pcie = d->chip_data;
90 msg->address_lo = lower_32_bits(pcie->msi_doorbell);
91 msg->address_hi = upper_32_bits(pcie->msi_doorbell);
92 msg->data = d->hwirq;
93}
94
95static struct irq_chip tango_chip = {
96 .irq_ack = tango_ack,
97 .irq_mask = tango_mask,
98 .irq_unmask = tango_unmask,
99 .irq_set_affinity = tango_set_affinity,
100 .irq_compose_msi_msg = tango_compose_msi_msg,
101};
102
103static void msi_ack(struct irq_data *d)
104{
105 irq_chip_ack_parent(d);
106}
107
108static void msi_mask(struct irq_data *d)
109{
110 pci_msi_mask_irq(d);
111 irq_chip_mask_parent(d);
112}
113
114static void msi_unmask(struct irq_data *d)
115{
116 pci_msi_unmask_irq(d);
117 irq_chip_unmask_parent(d);
118}
119
120static struct irq_chip msi_chip = {
121 .name = "MSI",
122 .irq_ack = msi_ack,
123 .irq_mask = msi_mask,
124 .irq_unmask = msi_unmask,
125};
126
127static struct msi_domain_info msi_dom_info = {
128 .flags = MSI_FLAG_PCI_MSIX
129 | MSI_FLAG_USE_DEF_DOM_OPS
130 | MSI_FLAG_USE_DEF_CHIP_OPS,
131 .chip = &msi_chip,
132};
133
134static int tango_irq_domain_alloc(struct irq_domain *dom, unsigned int virq,
135 unsigned int nr_irqs, void *args)
136{
137 struct tango_pcie *pcie = dom->host_data;
138 unsigned long flags;
139 int pos;
140
141 spin_lock_irqsave(&pcie->used_msi_lock, flags);
142 pos = find_first_zero_bit(pcie->used_msi, MSI_MAX);
143 if (pos >= MSI_MAX) {
144 spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
145 return -ENOSPC;
146 }
147 __set_bit(pos, pcie->used_msi);
148 spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
149 irq_domain_set_info(dom, virq, pos, &tango_chip,
150 pcie, handle_edge_irq, NULL, NULL);
151
152 return 0;
153}
154
155static void tango_irq_domain_free(struct irq_domain *dom, unsigned int virq,
156 unsigned int nr_irqs)
157{
158 unsigned long flags;
159 struct irq_data *d = irq_domain_get_irq_data(dom, virq);
160 struct tango_pcie *pcie = d->chip_data;
161
162 spin_lock_irqsave(&pcie->used_msi_lock, flags);
163 __clear_bit(d->hwirq, pcie->used_msi);
164 spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
165}
166
167static const struct irq_domain_ops dom_ops = {
168 .alloc = tango_irq_domain_alloc,
169 .free = tango_irq_domain_free,
Marc Gonzalez5e14e9f2017-06-20 10:17:40 +0200170};
171
172static int smp8759_config_read(struct pci_bus *bus, unsigned int devfn,
173 int where, int size, u32 *val)
174{
175 struct pci_config_window *cfg = bus->sysdata;
176 struct tango_pcie *pcie = dev_get_drvdata(cfg->parent);
177 int ret;
178
179 /* Reads in configuration space outside devfn 0 return garbage */
180 if (devfn != 0)
181 return PCIBIOS_FUNC_NOT_SUPPORTED;
182
183 /*
184 * PCI config and MMIO accesses are muxed. Linux doesn't have a
185 * mutual exclusion mechanism for config vs. MMIO accesses, so
186 * concurrent accesses may cause corruption.
187 */
188 writel_relaxed(1, pcie->base + SMP8759_MUX);
189 ret = pci_generic_config_read(bus, devfn, where, size, val);
190 writel_relaxed(0, pcie->base + SMP8759_MUX);
191
192 return ret;
193}
194
195static int smp8759_config_write(struct pci_bus *bus, unsigned int devfn,
196 int where, int size, u32 val)
197{
198 struct pci_config_window *cfg = bus->sysdata;
199 struct tango_pcie *pcie = dev_get_drvdata(cfg->parent);
200 int ret;
201
202 writel_relaxed(1, pcie->base + SMP8759_MUX);
203 ret = pci_generic_config_write(bus, devfn, where, size, val);
204 writel_relaxed(0, pcie->base + SMP8759_MUX);
205
206 return ret;
207}
208
209static struct pci_ecam_ops smp8759_ecam_ops = {
210 .bus_shift = 20,
211 .pci_ops = {
212 .map_bus = pci_ecam_map_bus,
213 .read = smp8759_config_read,
214 .write = smp8759_config_write,
215 }
216};
217
218static int tango_pcie_link_up(struct tango_pcie *pcie)
219{
220 void __iomem *test_out = pcie->base + SMP8759_TEST_OUT;
221 int i;
222
223 writel_relaxed(16, test_out);
224 for (i = 0; i < 10; ++i) {
225 u32 ltssm_state = readl_relaxed(test_out) >> 8;
226 if ((ltssm_state & 0x1f) == 0xf) /* L0 */
227 return 1;
228 usleep_range(3000, 4000);
229 }
230
231 return 0;
232}
233
234static int tango_pcie_probe(struct platform_device *pdev)
235{
236 struct device *dev = &pdev->dev;
237 struct tango_pcie *pcie;
238 struct resource *res;
Marc Gonzalezd76bdce2017-09-26 12:27:39 +0200239 struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
240 struct irq_domain *msi_dom, *irq_dom;
241 struct of_pci_range_parser parser;
242 struct of_pci_range range;
243 int virq, offset;
Marc Gonzalez5e14e9f2017-06-20 10:17:40 +0200244
245 dev_warn(dev, "simultaneous PCI config and MMIO accesses may cause data corruption\n");
246 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
247
248 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
249 if (!pcie)
250 return -ENOMEM;
251
252 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
253 pcie->base = devm_ioremap_resource(dev, res);
254 if (IS_ERR(pcie->base))
255 return PTR_ERR(pcie->base);
256
257 platform_set_drvdata(pdev, pcie);
258
259 if (!tango_pcie_link_up(pcie))
260 return -ENODEV;
261
Marc Gonzalezd76bdce2017-09-26 12:27:39 +0200262 if (of_pci_dma_range_parser_init(&parser, dev->of_node) < 0)
263 return -ENOENT;
264
265 if (of_pci_range_parser_one(&parser, &range) == NULL)
266 return -ENOENT;
267
268 range.pci_addr += range.size;
269 pcie->msi_doorbell = range.pci_addr + res->start + SMP8759_DOORBELL;
270
271 for (offset = 0; offset < MSI_MAX / 8; offset += 4)
272 writel_relaxed(0, pcie->base + SMP8759_ENABLE + offset);
273
274 virq = platform_get_irq(pdev, 1);
275 if (virq <= 0) {
276 dev_err(dev, "Failed to map IRQ\n");
277 return -ENXIO;
278 }
279
280 irq_dom = irq_domain_create_linear(fwnode, MSI_MAX, &dom_ops, pcie);
281 if (!irq_dom) {
282 dev_err(dev, "Failed to create IRQ domain\n");
283 return -ENOMEM;
284 }
285
286 msi_dom = pci_msi_create_irq_domain(fwnode, &msi_dom_info, irq_dom);
287 if (!msi_dom) {
288 dev_err(dev, "Failed to create MSI domain\n");
289 irq_domain_remove(irq_dom);
290 return -ENOMEM;
291 }
292
293 pcie->dom = irq_dom;
294 spin_lock_init(&pcie->used_msi_lock);
295 irq_set_chained_handler_and_data(virq, tango_msi_isr, pcie);
296
Marc Gonzalez5e14e9f2017-06-20 10:17:40 +0200297 return pci_host_common_probe(pdev, &smp8759_ecam_ops);
298}
299
300static const struct of_device_id tango_pcie_ids[] = {
301 { .compatible = "sigma,smp8759-pcie" },
302 { },
303};
304
305static struct platform_driver tango_pcie_driver = {
306 .probe = tango_pcie_probe,
307 .driver = {
308 .name = KBUILD_MODNAME,
309 .of_match_table = tango_pcie_ids,
310 .suppress_bind_attrs = true,
311 },
312};
313builtin_platform_driver(tango_pcie_driver);
314
315/*
316 * The root complex advertises the wrong device class.
317 * Header Type 1 is for PCI-to-PCI bridges.
318 */
319static void tango_fixup_class(struct pci_dev *dev)
320{
321 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
322}
323DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0024, tango_fixup_class);
324DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0028, tango_fixup_class);
325
326/*
327 * The root complex exposes a "fake" BAR, which is used to filter
328 * bus-to-system accesses. Only accesses within the range defined by this
329 * BAR are forwarded to the host, others are ignored.
330 *
331 * By default, the DMA framework expects an identity mapping, and DRAM0 is
332 * mapped at 0x80000000.
333 */
334static void tango_fixup_bar(struct pci_dev *dev)
335{
336 dev->non_compliant_bars = true;
337 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0x80000000);
338}
339DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0024, tango_fixup_bar);
340DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0028, tango_fixup_bar);