blob: 3bba87af0b6b2324591b4118c5c82e0cbf6c5877 [file] [log] [blame]
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +05301/*
2 * PCIe host controller driver for NWL PCIe Bridge
3 * Based on pcie-xilinx.c, pci-tegra.c
4 *
5 * (C) Copyright 2014 - 2015, Xilinx, Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/delay.h>
14#include <linux/interrupt.h>
15#include <linux/irq.h>
16#include <linux/irqdomain.h>
17#include <linux/kernel.h>
Paul Gortmakerff187e772016-08-24 16:57:50 -040018#include <linux/init.h>
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +053019#include <linux/msi.h>
20#include <linux/of_address.h>
21#include <linux/of_pci.h>
22#include <linux/of_platform.h>
23#include <linux/of_irq.h>
24#include <linux/pci.h>
25#include <linux/platform_device.h>
26#include <linux/irqchip/chained_irq.h>
27
28/* Bridge core config registers */
29#define BRCFG_PCIE_RX0 0x00000000
30#define BRCFG_INTERRUPT 0x00000010
31#define BRCFG_PCIE_RX_MSG_FILTER 0x00000020
32
33/* Egress - Bridge translation registers */
34#define E_BREG_CAPABILITIES 0x00000200
35#define E_BREG_CONTROL 0x00000208
36#define E_BREG_BASE_LO 0x00000210
37#define E_BREG_BASE_HI 0x00000214
38#define E_ECAM_CAPABILITIES 0x00000220
39#define E_ECAM_CONTROL 0x00000228
40#define E_ECAM_BASE_LO 0x00000230
41#define E_ECAM_BASE_HI 0x00000234
42
43/* Ingress - address translations */
44#define I_MSII_CAPABILITIES 0x00000300
45#define I_MSII_CONTROL 0x00000308
46#define I_MSII_BASE_LO 0x00000310
47#define I_MSII_BASE_HI 0x00000314
48
49#define I_ISUB_CONTROL 0x000003E8
50#define SET_ISUB_CONTROL BIT(0)
51/* Rxed msg fifo - Interrupt status registers */
52#define MSGF_MISC_STATUS 0x00000400
53#define MSGF_MISC_MASK 0x00000404
54#define MSGF_LEG_STATUS 0x00000420
55#define MSGF_LEG_MASK 0x00000424
56#define MSGF_MSI_STATUS_LO 0x00000440
57#define MSGF_MSI_STATUS_HI 0x00000444
58#define MSGF_MSI_MASK_LO 0x00000448
59#define MSGF_MSI_MASK_HI 0x0000044C
60
61/* Msg filter mask bits */
62#define CFG_ENABLE_PM_MSG_FWD BIT(1)
63#define CFG_ENABLE_INT_MSG_FWD BIT(2)
64#define CFG_ENABLE_ERR_MSG_FWD BIT(3)
65#define CFG_ENABLE_SLT_MSG_FWD BIT(5)
66#define CFG_ENABLE_VEN_MSG_FWD BIT(7)
67#define CFG_ENABLE_OTH_MSG_FWD BIT(13)
68#define CFG_ENABLE_VEN_MSG_EN BIT(14)
69#define CFG_ENABLE_VEN_MSG_VEN_INV BIT(15)
70#define CFG_ENABLE_VEN_MSG_VEN_ID GENMASK(31, 16)
71#define CFG_ENABLE_MSG_FILTER_MASK (CFG_ENABLE_PM_MSG_FWD | \
72 CFG_ENABLE_INT_MSG_FWD | \
73 CFG_ENABLE_ERR_MSG_FWD | \
74 CFG_ENABLE_SLT_MSG_FWD | \
75 CFG_ENABLE_VEN_MSG_FWD | \
76 CFG_ENABLE_OTH_MSG_FWD | \
77 CFG_ENABLE_VEN_MSG_EN | \
78 CFG_ENABLE_VEN_MSG_VEN_INV | \
79 CFG_ENABLE_VEN_MSG_VEN_ID)
80
81/* Misc interrupt status mask bits */
82#define MSGF_MISC_SR_RXMSG_AVAIL BIT(0)
83#define MSGF_MISC_SR_RXMSG_OVER BIT(1)
84#define MSGF_MISC_SR_SLAVE_ERR BIT(4)
85#define MSGF_MISC_SR_MASTER_ERR BIT(5)
86#define MSGF_MISC_SR_I_ADDR_ERR BIT(6)
87#define MSGF_MISC_SR_E_ADDR_ERR BIT(7)
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +053088#define MSGF_MISC_SR_FATAL_AER BIT(16)
89#define MSGF_MISC_SR_NON_FATAL_AER BIT(17)
90#define MSGF_MISC_SR_CORR_AER BIT(18)
91#define MSGF_MISC_SR_UR_DETECT BIT(20)
92#define MSGF_MISC_SR_NON_FATAL_DEV BIT(22)
93#define MSGF_MISC_SR_FATAL_DEV BIT(23)
94#define MSGF_MISC_SR_LINK_DOWN BIT(24)
95#define MSGF_MSIC_SR_LINK_AUTO_BWIDTH BIT(25)
96#define MSGF_MSIC_SR_LINK_BWIDTH BIT(26)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +053097
98#define MSGF_MISC_SR_MASKALL (MSGF_MISC_SR_RXMSG_AVAIL | \
99 MSGF_MISC_SR_RXMSG_OVER | \
100 MSGF_MISC_SR_SLAVE_ERR | \
101 MSGF_MISC_SR_MASTER_ERR | \
102 MSGF_MISC_SR_I_ADDR_ERR | \
103 MSGF_MISC_SR_E_ADDR_ERR | \
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530104 MSGF_MISC_SR_FATAL_AER | \
105 MSGF_MISC_SR_NON_FATAL_AER | \
106 MSGF_MISC_SR_CORR_AER | \
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530107 MSGF_MISC_SR_UR_DETECT | \
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530108 MSGF_MISC_SR_NON_FATAL_DEV | \
109 MSGF_MISC_SR_FATAL_DEV | \
110 MSGF_MISC_SR_LINK_DOWN | \
111 MSGF_MSIC_SR_LINK_AUTO_BWIDTH | \
112 MSGF_MSIC_SR_LINK_BWIDTH)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530113
114/* Legacy interrupt status mask bits */
115#define MSGF_LEG_SR_INTA BIT(0)
116#define MSGF_LEG_SR_INTB BIT(1)
117#define MSGF_LEG_SR_INTC BIT(2)
118#define MSGF_LEG_SR_INTD BIT(3)
119#define MSGF_LEG_SR_MASKALL (MSGF_LEG_SR_INTA | MSGF_LEG_SR_INTB | \
120 MSGF_LEG_SR_INTC | MSGF_LEG_SR_INTD)
121
122/* MSI interrupt status mask bits */
Bharat Kumar Gogadaf665bd12016-08-30 16:09:17 +0530123#define MSGF_MSI_SR_LO_MASK GENMASK(31, 0)
124#define MSGF_MSI_SR_HI_MASK GENMASK(31, 0)
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530125
126#define MSII_PRESENT BIT(0)
127#define MSII_ENABLE BIT(0)
128#define MSII_STATUS_ENABLE BIT(15)
129
130/* Bridge config interrupt mask */
131#define BRCFG_INTERRUPT_MASK BIT(0)
132#define BREG_PRESENT BIT(0)
133#define BREG_ENABLE BIT(0)
134#define BREG_ENABLE_FORCE BIT(1)
135
136/* E_ECAM status mask bits */
137#define E_ECAM_PRESENT BIT(0)
138#define E_ECAM_CR_ENABLE BIT(0)
139#define E_ECAM_SIZE_LOC GENMASK(20, 16)
140#define E_ECAM_SIZE_SHIFT 16
141#define ECAM_BUS_LOC_SHIFT 20
142#define ECAM_DEV_LOC_SHIFT 12
143#define NWL_ECAM_VALUE_DEFAULT 12
144
145#define CFG_DMA_REG_BAR GENMASK(2, 0)
146
147#define INT_PCI_MSI_NR (2 * 32)
148#define INTX_NUM 4
149
150/* Readin the PS_LINKUP */
151#define PS_LINKUP_OFFSET 0x00000238
152#define PCIE_PHY_LINKUP_BIT BIT(0)
153#define PHY_RDY_LINKUP_BIT BIT(1)
154
155/* Parameters for the waiting for link up routine */
156#define LINK_WAIT_MAX_RETRIES 10
157#define LINK_WAIT_USLEEP_MIN 90000
158#define LINK_WAIT_USLEEP_MAX 100000
159
160struct nwl_msi { /* MSI information */
161 struct irq_domain *msi_domain;
162 unsigned long *bitmap;
163 struct irq_domain *dev_domain;
164 struct mutex lock; /* protect bitmap variable */
165 int irq_msi0;
166 int irq_msi1;
167};
168
169struct nwl_pcie {
170 struct device *dev;
171 void __iomem *breg_base;
172 void __iomem *pcireg_base;
173 void __iomem *ecam_base;
174 phys_addr_t phys_breg_base; /* Physical Bridge Register Base */
175 phys_addr_t phys_pcie_reg_base; /* Physical PCIe Controller Base */
176 phys_addr_t phys_ecam_base; /* Physical Configuration Base */
177 u32 breg_size;
178 u32 pcie_reg_size;
179 u32 ecam_size;
180 int irq_intx;
181 int irq_misc;
182 u32 ecam_value;
183 u8 last_busno;
184 u8 root_busno;
185 struct nwl_msi msi;
186 struct irq_domain *legacy_irq_domain;
187};
188
189static inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
190{
191 return readl(pcie->breg_base + off);
192}
193
194static inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
195{
196 writel(val, pcie->breg_base + off);
197}
198
199static bool nwl_pcie_link_up(struct nwl_pcie *pcie)
200{
201 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
202 return true;
203 return false;
204}
205
206static bool nwl_phy_link_up(struct nwl_pcie *pcie)
207{
208 if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
209 return true;
210 return false;
211}
212
213static int nwl_wait_for_link(struct nwl_pcie *pcie)
214{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500215 struct device *dev = pcie->dev;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530216 int retries;
217
218 /* check if the link is up or not */
219 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
220 if (nwl_phy_link_up(pcie))
221 return 0;
222 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
223 }
224
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500225 dev_err(dev, "PHY link never came up\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530226 return -ETIMEDOUT;
227}
228
229static bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
230{
231 struct nwl_pcie *pcie = bus->sysdata;
232
233 /* Check link before accessing downstream ports */
234 if (bus->number != pcie->root_busno) {
235 if (!nwl_pcie_link_up(pcie))
236 return false;
237 }
238
239 /* Only one device down on each root port */
240 if (bus->number == pcie->root_busno && devfn > 0)
241 return false;
242
243 return true;
244}
245
246/**
247 * nwl_pcie_map_bus - Get configuration base
248 *
249 * @bus: Bus structure of current bus
250 * @devfn: Device/function
251 * @where: Offset from base
252 *
253 * Return: Base address of the configuration space needed to be
254 * accessed.
255 */
256static void __iomem *nwl_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
257 int where)
258{
259 struct nwl_pcie *pcie = bus->sysdata;
260 int relbus;
261
262 if (!nwl_pcie_valid_device(bus, devfn))
263 return NULL;
264
265 relbus = (bus->number << ECAM_BUS_LOC_SHIFT) |
266 (devfn << ECAM_DEV_LOC_SHIFT);
267
268 return pcie->ecam_base + relbus + where;
269}
270
271/* PCIe operations */
272static struct pci_ops nwl_pcie_ops = {
273 .map_bus = nwl_pcie_map_bus,
274 .read = pci_generic_config_read,
275 .write = pci_generic_config_write,
276};
277
278static irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
279{
280 struct nwl_pcie *pcie = data;
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500281 struct device *dev = pcie->dev;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530282 u32 misc_stat;
283
284 /* Checking for misc interrupts */
285 misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
286 MSGF_MISC_SR_MASKALL;
287 if (!misc_stat)
288 return IRQ_NONE;
289
290 if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500291 dev_err(dev, "Received Message FIFO Overflow\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530292
293 if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500294 dev_err(dev, "Slave error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530295
296 if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500297 dev_err(dev, "Master error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530298
299 if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500300 dev_err(dev, "In Misc Ingress address translation error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530301
302 if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500303 dev_err(dev, "In Misc Egress address translation error\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530304
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530305 if (misc_stat & MSGF_MISC_SR_FATAL_AER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500306 dev_err(dev, "Fatal Error in AER Capability\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530307
308 if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500309 dev_err(dev, "Non-Fatal Error in AER Capability\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530310
311 if (misc_stat & MSGF_MISC_SR_CORR_AER)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500312 dev_err(dev, "Correctable Error in AER Capability\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530313
314 if (misc_stat & MSGF_MISC_SR_UR_DETECT)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500315 dev_err(dev, "Unsupported request Detected\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530316
317 if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500318 dev_err(dev, "Non-Fatal Error Detected\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530319
320 if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500321 dev_err(dev, "Fatal Error Detected\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530322
323 if (misc_stat & MSGF_MSIC_SR_LINK_AUTO_BWIDTH)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500324 dev_info(dev, "Link Autonomous Bandwidth Management Status bit set\n");
Bharat Kumar Gogadac2a7ff12016-08-30 16:09:16 +0530325
326 if (misc_stat & MSGF_MSIC_SR_LINK_BWIDTH)
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500327 dev_info(dev, "Link Bandwidth Management Status bit set\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530328
329 /* Clear misc interrupt status */
330 nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
331
332 return IRQ_HANDLED;
333}
334
335static void nwl_pcie_leg_handler(struct irq_desc *desc)
336{
337 struct irq_chip *chip = irq_desc_get_chip(desc);
338 struct nwl_pcie *pcie;
339 unsigned long status;
340 u32 bit;
341 u32 virq;
342
343 chained_irq_enter(chip, desc);
344 pcie = irq_desc_get_handler_data(desc);
345
346 while ((status = nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
347 MSGF_LEG_SR_MASKALL) != 0) {
348 for_each_set_bit(bit, &status, INTX_NUM) {
349 virq = irq_find_mapping(pcie->legacy_irq_domain,
350 bit + 1);
351 if (virq)
352 generic_handle_irq(virq);
353 }
354 }
355
356 chained_irq_exit(chip, desc);
357}
358
359static void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
360{
361 struct nwl_msi *msi;
362 unsigned long status;
363 u32 bit;
364 u32 virq;
365
366 msi = &pcie->msi;
367
368 while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
369 for_each_set_bit(bit, &status, 32) {
370 nwl_bridge_writel(pcie, 1 << bit, status_reg);
371 virq = irq_find_mapping(msi->dev_domain, bit);
372 if (virq)
373 generic_handle_irq(virq);
374 }
375 }
376}
377
378static void nwl_pcie_msi_handler_high(struct irq_desc *desc)
379{
380 struct irq_chip *chip = irq_desc_get_chip(desc);
381 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
382
383 chained_irq_enter(chip, desc);
384 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
385 chained_irq_exit(chip, desc);
386}
387
388static void nwl_pcie_msi_handler_low(struct irq_desc *desc)
389{
390 struct irq_chip *chip = irq_desc_get_chip(desc);
391 struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
392
393 chained_irq_enter(chip, desc);
394 nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
395 chained_irq_exit(chip, desc);
396}
397
398static int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
399 irq_hw_number_t hwirq)
400{
401 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
402 irq_set_chip_data(irq, domain->host_data);
403
404 return 0;
405}
406
407static const struct irq_domain_ops legacy_domain_ops = {
408 .map = nwl_legacy_map,
409};
410
411#ifdef CONFIG_PCI_MSI
412static struct irq_chip nwl_msi_irq_chip = {
413 .name = "nwl_pcie:msi",
414 .irq_enable = unmask_msi_irq,
415 .irq_disable = mask_msi_irq,
416 .irq_mask = mask_msi_irq,
417 .irq_unmask = unmask_msi_irq,
418
419};
420
421static struct msi_domain_info nwl_msi_domain_info = {
422 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
423 MSI_FLAG_MULTI_PCI_MSI),
424 .chip = &nwl_msi_irq_chip,
425};
426#endif
427
428static void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
429{
430 struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
431 phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
432
433 msg->address_lo = lower_32_bits(msi_addr);
434 msg->address_hi = upper_32_bits(msi_addr);
435 msg->data = data->hwirq;
436}
437
438static int nwl_msi_set_affinity(struct irq_data *irq_data,
439 const struct cpumask *mask, bool force)
440{
441 return -EINVAL;
442}
443
444static struct irq_chip nwl_irq_chip = {
445 .name = "Xilinx MSI",
446 .irq_compose_msi_msg = nwl_compose_msi_msg,
447 .irq_set_affinity = nwl_msi_set_affinity,
448};
449
450static int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
451 unsigned int nr_irqs, void *args)
452{
453 struct nwl_pcie *pcie = domain->host_data;
454 struct nwl_msi *msi = &pcie->msi;
455 int bit;
456 int i;
457
458 mutex_lock(&msi->lock);
Bharat Kumar Gogada5086e472019-06-12 15:47:59 +0530459 bit = bitmap_find_free_region(msi->bitmap, INT_PCI_MSI_NR,
460 get_count_order(nr_irqs));
461 if (bit < 0) {
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530462 mutex_unlock(&msi->lock);
463 return -ENOSPC;
464 }
465
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530466 for (i = 0; i < nr_irqs; i++) {
467 irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
468 domain->host_data, handle_simple_irq,
469 NULL, NULL);
470 }
471 mutex_unlock(&msi->lock);
472 return 0;
473}
474
475static void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
476 unsigned int nr_irqs)
477{
478 struct irq_data *data = irq_domain_get_irq_data(domain, virq);
479 struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
480 struct nwl_msi *msi = &pcie->msi;
481
482 mutex_lock(&msi->lock);
Bharat Kumar Gogada5086e472019-06-12 15:47:59 +0530483 bitmap_release_region(msi->bitmap, data->hwirq,
484 get_count_order(nr_irqs));
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530485 mutex_unlock(&msi->lock);
486}
487
488static const struct irq_domain_ops dev_msi_domain_ops = {
489 .alloc = nwl_irq_domain_alloc,
490 .free = nwl_irq_domain_free,
491};
492
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530493static int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
494{
495#ifdef CONFIG_PCI_MSI
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500496 struct device *dev = pcie->dev;
497 struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530498 struct nwl_msi *msi = &pcie->msi;
499
500 msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
501 &dev_msi_domain_ops, pcie);
502 if (!msi->dev_domain) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500503 dev_err(dev, "failed to create dev IRQ domain\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530504 return -ENOMEM;
505 }
506 msi->msi_domain = pci_msi_create_irq_domain(fwnode,
507 &nwl_msi_domain_info,
508 msi->dev_domain);
509 if (!msi->msi_domain) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500510 dev_err(dev, "failed to create msi IRQ domain\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530511 irq_domain_remove(msi->dev_domain);
512 return -ENOMEM;
513 }
514#endif
515 return 0;
516}
517
518static int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
519{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500520 struct device *dev = pcie->dev;
521 struct device_node *node = dev->of_node;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530522 struct device_node *legacy_intc_node;
523
524 legacy_intc_node = of_get_next_child(node, NULL);
525 if (!legacy_intc_node) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500526 dev_err(dev, "No legacy intc node found\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530527 return -EINVAL;
528 }
529
530 pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
531 INTX_NUM,
532 &legacy_domain_ops,
533 pcie);
Nicholas Mc Guire3b8eeae2018-06-29 13:50:10 -0500534 of_node_put(legacy_intc_node);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530535 if (!pcie->legacy_irq_domain) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500536 dev_err(dev, "failed to create IRQ domain\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530537 return -ENOMEM;
538 }
539
540 nwl_pcie_init_msi_irq_domain(pcie);
541 return 0;
542}
543
544static int nwl_pcie_enable_msi(struct nwl_pcie *pcie, struct pci_bus *bus)
545{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500546 struct device *dev = pcie->dev;
547 struct platform_device *pdev = to_platform_device(dev);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530548 struct nwl_msi *msi = &pcie->msi;
549 unsigned long base;
550 int ret;
551 int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
552
553 mutex_init(&msi->lock);
554
555 msi->bitmap = kzalloc(size, GFP_KERNEL);
556 if (!msi->bitmap)
557 return -ENOMEM;
558
559 /* Get msi_1 IRQ number */
560 msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
561 if (msi->irq_msi1 < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500562 dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi1);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530563 ret = -EINVAL;
564 goto err;
565 }
566
567 irq_set_chained_handler_and_data(msi->irq_msi1,
568 nwl_pcie_msi_handler_high, pcie);
569
570 /* Get msi_0 IRQ number */
571 msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
572 if (msi->irq_msi0 < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500573 dev_err(dev, "failed to get IRQ#%d\n", msi->irq_msi0);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530574 ret = -EINVAL;
575 goto err;
576 }
577
578 irq_set_chained_handler_and_data(msi->irq_msi0,
579 nwl_pcie_msi_handler_low, pcie);
580
581 /* Check for msii_present bit */
582 ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
583 if (!ret) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500584 dev_err(dev, "MSI not present\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530585 ret = -EIO;
586 goto err;
587 }
588
589 /* Enable MSII */
590 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
591 MSII_ENABLE, I_MSII_CONTROL);
592
593 /* Enable MSII status */
594 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
595 MSII_STATUS_ENABLE, I_MSII_CONTROL);
596
597 /* setup AFI/FPCI range */
598 base = pcie->phys_pcie_reg_base;
599 nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
600 nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
601
602 /*
603 * For high range MSI interrupts: disable, clear any pending,
604 * and enable
605 */
606 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
607
608 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_HI) &
609 MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
610
611 nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
612
613 /*
614 * For low range MSI interrupts: disable, clear any pending,
615 * and enable
616 */
617 nwl_bridge_writel(pcie, (u32)~MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
618
619 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
620 MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
621
622 nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
623
624 return 0;
625err:
626 kfree(msi->bitmap);
627 msi->bitmap = NULL;
628 return ret;
629}
630
631static int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
632{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500633 struct device *dev = pcie->dev;
634 struct platform_device *pdev = to_platform_device(dev);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530635 u32 breg_val, ecam_val, first_busno = 0;
636 int err;
637
638 breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
639 if (!breg_val) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500640 dev_err(dev, "BREG is not present\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530641 return breg_val;
642 }
643
644 /* Write bridge_off to breg base */
645 nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
646 E_BREG_BASE_LO);
647 nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
648 E_BREG_BASE_HI);
649
650 /* Enable BREG */
651 nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
652 E_BREG_CONTROL);
653
654 /* Disable DMA channel registers */
655 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
656 CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
657
658 /* Enable Ingress subtractive decode translation */
659 nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
660
661 /* Enable msg filtering details */
662 nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
663 BRCFG_PCIE_RX_MSG_FILTER);
664
665 err = nwl_wait_for_link(pcie);
666 if (err)
667 return err;
668
669 ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
670 if (!ecam_val) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500671 dev_err(dev, "ECAM is not present\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530672 return ecam_val;
673 }
674
675 /* Enable ECAM */
676 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
677 E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
678
679 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
680 (pcie->ecam_value << E_ECAM_SIZE_SHIFT),
681 E_ECAM_CONTROL);
682
683 nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
684 E_ECAM_BASE_LO);
685 nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
686 E_ECAM_BASE_HI);
687
688 /* Get bus range */
689 ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
690 pcie->last_busno = (ecam_val & E_ECAM_SIZE_LOC) >> E_ECAM_SIZE_SHIFT;
691 /* Write primary, secondary and subordinate bus numbers */
692 ecam_val = first_busno;
693 ecam_val |= (first_busno + 1) << 8;
694 ecam_val |= (pcie->last_busno << E_ECAM_SIZE_SHIFT);
695 writel(ecam_val, (pcie->ecam_base + PCI_PRIMARY_BUS));
696
697 if (nwl_pcie_link_up(pcie))
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500698 dev_info(dev, "Link is UP\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530699 else
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500700 dev_info(dev, "Link is DOWN\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530701
702 /* Get misc IRQ number */
703 pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
704 if (pcie->irq_misc < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500705 dev_err(dev, "failed to get misc IRQ %d\n",
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530706 pcie->irq_misc);
707 return -EINVAL;
708 }
709
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500710 err = devm_request_irq(dev, pcie->irq_misc,
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530711 nwl_pcie_misc_handler, IRQF_SHARED,
712 "nwl_pcie:misc", pcie);
713 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500714 dev_err(dev, "fail to register misc IRQ#%d\n",
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530715 pcie->irq_misc);
716 return err;
717 }
718
719 /* Disable all misc interrupts */
720 nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
721
722 /* Clear pending misc interrupts */
723 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
724 MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
725
726 /* Enable all misc interrupts */
727 nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
728
729
730 /* Disable all legacy interrupts */
731 nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
732
733 /* Clear pending legacy interrupts */
734 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
735 MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
736
737 /* Enable all legacy interrupts */
738 nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
739
740 /* Enable the bridge config interrupt */
741 nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
742 BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
743
744 return 0;
745}
746
747static int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
748 struct platform_device *pdev)
749{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500750 struct device *dev = pcie->dev;
751 struct device_node *node = dev->of_node;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530752 struct resource *res;
753 const char *type;
754
755 /* Check for device type */
756 type = of_get_property(node, "device_type", NULL);
757 if (!type || strcmp(type, "pci")) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500758 dev_err(dev, "invalid \"device_type\" %s\n", type);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530759 return -EINVAL;
760 }
761
762 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500763 pcie->breg_base = devm_ioremap_resource(dev, res);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530764 if (IS_ERR(pcie->breg_base))
765 return PTR_ERR(pcie->breg_base);
766 pcie->phys_breg_base = res->start;
767
768 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500769 pcie->pcireg_base = devm_ioremap_resource(dev, res);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530770 if (IS_ERR(pcie->pcireg_base))
771 return PTR_ERR(pcie->pcireg_base);
772 pcie->phys_pcie_reg_base = res->start;
773
774 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500775 pcie->ecam_base = devm_ioremap_resource(dev, res);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530776 if (IS_ERR(pcie->ecam_base))
777 return PTR_ERR(pcie->ecam_base);
778 pcie->phys_ecam_base = res->start;
779
780 /* Get intx IRQ number */
781 pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
782 if (pcie->irq_intx < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500783 dev_err(dev, "failed to get intx IRQ %d\n", pcie->irq_intx);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530784 return -EINVAL;
785 }
786
787 irq_set_chained_handler_and_data(pcie->irq_intx,
788 nwl_pcie_leg_handler, pcie);
789
790 return 0;
791}
792
793static const struct of_device_id nwl_pcie_of_match[] = {
794 { .compatible = "xlnx,nwl-pcie-2.11", },
795 {}
796};
797
798static int nwl_pcie_probe(struct platform_device *pdev)
799{
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500800 struct device *dev = &pdev->dev;
801 struct device_node *node = dev->of_node;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530802 struct nwl_pcie *pcie;
803 struct pci_bus *bus;
804 struct pci_bus *child;
805 int err;
806 resource_size_t iobase = 0;
807 LIST_HEAD(res);
808
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500809 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530810 if (!pcie)
811 return -ENOMEM;
812
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500813 pcie->dev = dev;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530814 pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
815
816 err = nwl_pcie_parse_dt(pcie, pdev);
817 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500818 dev_err(dev, "Parsing DT failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530819 return err;
820 }
821
822 err = nwl_pcie_bridge_init(pcie);
823 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500824 dev_err(dev, "HW Initialization failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530825 return err;
826 }
827
828 err = of_pci_get_host_bridge_resources(node, 0, 0xff, &res, &iobase);
829 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500830 dev_err(dev, "Getting bridge resources failed\n");
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530831 return err;
832 }
833
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500834 err = devm_request_pci_bus_resources(dev, &res);
Bjorn Helgaas21f7fc22016-05-28 18:24:36 -0500835 if (err)
836 goto error;
837
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530838 err = nwl_pcie_init_irq_domain(pcie);
839 if (err) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500840 dev_err(dev, "Failed creating IRQ Domain\n");
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500841 goto error;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530842 }
843
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500844 bus = pci_create_root_bus(dev, pcie->root_busno,
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530845 &nwl_pcie_ops, pcie, &res);
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500846 if (!bus) {
847 err = -ENOMEM;
848 goto error;
849 }
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530850
851 if (IS_ENABLED(CONFIG_PCI_MSI)) {
852 err = nwl_pcie_enable_msi(pcie, bus);
853 if (err < 0) {
Bjorn Helgaasadf9e282016-10-06 13:44:43 -0500854 dev_err(dev, "failed to enable MSI support: %d\n", err);
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500855 goto error;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530856 }
857 }
858 pci_scan_child_bus(bus);
859 pci_assign_unassigned_bus_resources(bus);
860 list_for_each_entry(child, &bus->children, node)
861 pcie_bus_configure_settings(child);
862 pci_bus_add_devices(bus);
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530863 return 0;
Bjorn Helgaas0bb01302016-05-31 11:26:01 -0500864
865error:
866 pci_free_resource_list(&res);
867 return err;
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530868}
869
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530870static struct platform_driver nwl_pcie_driver = {
871 .driver = {
872 .name = "nwl-pcie",
Paul Gortmakerff187e772016-08-24 16:57:50 -0400873 .suppress_bind_attrs = true,
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530874 .of_match_table = nwl_pcie_of_match,
875 },
876 .probe = nwl_pcie_probe,
Bharat Kumar Gogadaab597d32016-03-06 22:02:14 +0530877};
Paul Gortmakerff187e772016-08-24 16:57:50 -0400878builtin_platform_driver(nwl_pcie_driver);